From nobody Wed Dec 17 21:42:52 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517847862990796.029599509947; Mon, 5 Feb 2018 08:24:22 -0800 (PST) Received: from localhost ([::1]:58546 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eijZ2-0004Ha-Pd for importer@patchew.org; Mon, 05 Feb 2018 11:24:16 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55658) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eiift-0006vG-9M for qemu-devel@nongnu.org; Mon, 05 Feb 2018 10:27:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eiifp-00043e-4j for qemu-devel@nongnu.org; Mon, 05 Feb 2018 10:27:17 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56276) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eiifo-000424-SU for qemu-devel@nongnu.org; Mon, 05 Feb 2018 10:27:13 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0A3AD4902F for ; Mon, 5 Feb 2018 15:27:12 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id E22F45D6A2; Mon, 5 Feb 2018 15:27:04 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Mon, 5 Feb 2018 15:24:55 +0000 Message-Id: <20180205152455.12088-10-berrange@redhat.com> In-Reply-To: <20180205152455.12088-1-berrange@redhat.com> References: <20180205152455.12088-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 05 Feb 2018 15:27:12 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v4 9/9] char: allow passing pre-opened socket file descriptor at startup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Dr. David Alan Gilbert" , Markus Armbruster , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Daniel P. Berrange" When starting QEMU management apps will usually setup a monitor socket, and then open it immediately after startup. If not using QEMU's own -daemonize arg, this process can be troublesome to handle correctly. The mgmt app will need to repeatedly call connect() until it succeeds, because it does not know when QEMU has created the listener socket. If can't retry connect() forever though, because an error might have caused QEMU to exit before it even creates the monitor. The obvious way to fix this kind of problem is to just pass in a pre-opened socket file descriptor for the QEMU monitor to listen on. The management app can now immediately call connect() just once. If connect() fails it knows that QEMU has exited with an error. The SocketAddress(Legacy) structs allow for FD passing via the monitor, and now via inherited file descriptors from the process that spawned QEMU. The final missing piece is adding a 'fd' parameter in the socket chardev options. This allows both HMP usage, pass any FD number with SCM_RIGHTS, then running HMP commands: getfd myfd chardev-add socket,fd=3Dmyfd Note that numeric FDs cannot be referenced directly in HMP, only named FDs. And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then spawning QEMU with -chardev socket,fd=3D3,id=3Dmon -mon chardev=3Dmon,mode=3Dcontrol Note that named FDs cannot be referenced in CLI args, only numeric FDs. We do not wire this up in the legacy chardev syntax, so you cannot use FD passing with '-qmp', you must use the modern '-mon' + '-chardev' pair. When passing pre-opened FDs there is a restriction on use of TLS encryption. It can be used on a server socket chardev, but cannot be used for a client socket chardev. This is because when validating a server's certificate, the client needs to have a hostname available to match against the certificate identity. An illustrative example of usage is: #!/usr/bin/perl use IO::Socket::UNIX; use Fcntl; unlink "/tmp/qmp"; my $srv =3D IO::Socket::UNIX->new( Type =3D> SOCK_STREAM(), Local =3D> "/tmp/qmp", Listen =3D> 1, ); my $flags =3D fcntl $srv, F_GETFD, 0; fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC; my $fd =3D $srv->fileno(); exec "qemu-system-x86_64", \ "-chardev", "socket,fd=3D$fd,server,nowait,id=3Dmon", \ "-mon", "chardev=3Dmon,mode=3Dcontrol"; Signed-off-by: Daniel P. Berrange --- chardev/char-socket.c | 15 +++++++++++++-- chardev/char.c | 3 +++ tests/test-char.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 7d0e608e60..980b8f0815 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -978,13 +978,14 @@ static void qemu_chr_parse_socket(QemuOpts *opts, Cha= rdevBackend *backend, const char *path =3D qemu_opt_get(opts, "path"); const char *host =3D qemu_opt_get(opts, "host"); const char *port =3D qemu_opt_get(opts, "port"); + const char *fd =3D qemu_opt_get(opts, "fd"); const char *tls_creds =3D qemu_opt_get(opts, "tls-creds"); SocketAddressLegacy *addr; ChardevSocket *sock; =20 - if ((!!path + !!host) !=3D 1) { + if ((!!path + !!fd + !!host) !=3D 1) { error_setg(errp, - "Exactly one of 'path' or 'host' required"); + "Exactly one of 'path', 'fd' or 'host' required"); return; } =20 @@ -999,6 +1000,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, Cha= rdevBackend *backend, error_setg(errp, "chardev: socket: no port given"); return; } + } else if (fd) { + /* We don't know what host to validate against when in client mode= */ + if (tls_creds && !is_listen) { + error_setg(errp, "TLS can not be used with pre-opened client F= D"); + return; + } } else { g_assert_not_reached(); } @@ -1039,6 +1046,10 @@ static void qemu_chr_parse_socket(QemuOpts *opts, Ch= ardevBackend *backend, .has_ipv6 =3D qemu_opt_get(opts, "ipv6"), .ipv6 =3D qemu_opt_get_bool(opts, "ipv6", 0), }; + } else if (fd) { + addr->type =3D SOCKET_ADDRESS_LEGACY_KIND_FD; + addr->u.fd.data =3D g_new(String, 1); + addr->u.fd.data->str =3D g_strdup(fd); } else { g_assert_not_reached(); } diff --git a/chardev/char.c b/chardev/char.c index 3e14de1920..4cdf240610 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -804,6 +804,9 @@ QemuOptsList qemu_chardev_opts =3D { },{ .name =3D "port", .type =3D QEMU_OPT_STRING, + },{ + .name =3D "fd", + .type =3D QEMU_OPT_STRING, },{ .name =3D "localaddr", .type =3D QEMU_OPT_STRING, diff --git a/tests/test-char.c b/tests/test-char.c index 911e3f6e8d..ee9df3d171 100644 --- a/tests/test-char.c +++ b/tests/test-char.c @@ -301,9 +301,8 @@ static int socket_can_read_hello(void *opaque) return 10; } =20 -static void char_socket_test(void) +static void char_socket_test_common(Chardev *chr) { - Chardev *chr =3D qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait= "); Chardev *chr_client; QObject *addr; QDict *qdict; @@ -358,6 +357,47 @@ static void char_socket_test(void) object_unparent(OBJECT(chr)); } =20 + +static void char_socket_basic_test(void) +{ + Chardev *chr =3D qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait= "); + + char_socket_test_common(chr); +} + + +static void char_socket_fdpass_test(void) +{ + Chardev *chr; + char *optstr; + QemuOpts *opts; + int fd; + SocketAddress *addr =3D g_new0(SocketAddress, 1); + + addr->type =3D SOCKET_ADDRESS_TYPE_INET; + addr->u.inet.host =3D g_strdup("127.0.0.1"); + addr->u.inet.port =3D g_strdup("0"); + + fd =3D socket_listen(addr, &error_abort); + g_assert(fd >=3D 0); + + qapi_free_SocketAddress(addr); + + optstr =3D g_strdup_printf("socket,id=3Dcdev,fd=3D%d,server,nowait", f= d); + + opts =3D qemu_opts_parse_noisily(qemu_find_opts("chardev"), + optstr, true); + g_free(optstr); + g_assert_nonnull(opts); + + chr =3D qemu_chr_new_from_opts(opts, &error_abort); + + qemu_opts_del(opts); + + char_socket_test_common(chr); +} + + #ifndef _WIN32 static void char_pipe_test(void) { @@ -774,7 +814,8 @@ int main(int argc, char **argv) #ifndef _WIN32 g_test_add_func("/char/file-fifo", char_file_fifo_test); #endif - g_test_add_func("/char/socket", char_socket_test); + g_test_add_func("/char/socket/basic", char_socket_basic_test); + g_test_add_func("/char/socket/fdpass", char_socket_fdpass_test); g_test_add_func("/char/udp", char_udp_test); #ifdef HAVE_CHARDEV_SERIAL g_test_add_func("/char/serial", char_serial_test); --=20 2.14.3