From nobody Tue Feb 10 05:26:53 2026 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 (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1520859283292524.5001005540242; Mon, 12 Mar 2018 05:54:43 -0700 (PDT) Received: from localhost ([::1]:58546 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evMyQ-0000pu-Eo for importer@patchew.org; Mon, 12 Mar 2018 08:54:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41770) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1evMtz-0005E2-7c for qemu-devel@nongnu.org; Mon, 12 Mar 2018 08:50:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1evMts-0007wB-PJ for qemu-devel@nongnu.org; Mon, 12 Mar 2018 08:50:07 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48450 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1evMts-0007vH-Jn for qemu-devel@nongnu.org; Mon, 12 Mar 2018 08:50:00 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2AE26401C9AE for ; Mon, 12 Mar 2018 12:50:00 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 450A410B0F3F; Mon, 12 Mar 2018 12:49:59 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Date: Mon, 12 Mar 2018 12:49:39 +0000 Message-Id: <20180312124939.20562-10-berrange@redhat.com> In-Reply-To: <20180312124939.20562-1-berrange@redhat.com> References: <20180312124939.20562-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Mon, 12 Mar 2018 12:50:00 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Mon, 12 Mar 2018 12:50:00 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'berrange@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v5 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: Markus Armbruster , Gerd Hoffmann , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= 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 a916ef4012..0c8d6d430a 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -1008,13 +1008,14 @@ static void qemu_chr_parse_socket(QemuOpts *opts, C= hardevBackend *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 @@ -1029,6 +1030,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, Ch= ardevBackend *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(); } @@ -1069,6 +1076,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 5d7b079ef0..f7e0d37f24 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -807,6 +807,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 b3a77af085..dffb354202 100644 --- a/tests/test-char.c +++ b/tests/test-char.c @@ -302,9 +302,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; @@ -359,6 +358,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) { @@ -775,7 +815,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