diff options
author | Daniel P. Berrange <berrange@redhat.com> | 2017-12-21 12:57:54 +0000 |
---|---|---|
committer | Daniel P. Berrangé <berrange@redhat.com> | 2018-03-13 18:06:06 +0000 |
commit | 0935700f8544033ebbd41e1f13cd528f8a58d24d (patch) | |
tree | 31ceebc8098909d052f6a961f4231579a6ed87ff /chardev/char.c | |
parent | 9bb4060c998e56976b36ee628ce7e0ecbd8ffb49 (diff) | |
download | qemu-0935700f8544033ebbd41e1f13cd528f8a58d24d.zip qemu-0935700f8544033ebbd41e1f13cd528f8a58d24d.tar.gz qemu-0935700f8544033ebbd41e1f13cd528f8a58d24d.tar.bz2 |
char: allow passing pre-opened socket file descriptor at startup
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=myfd
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=3,id=mon
-mon chardev=mon,mode=control
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 = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Diffstat (limited to 'chardev/char.c')
-rw-r--r-- | chardev/char.c | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/chardev/char.c b/chardev/char.c index 5d7b079..f7e0d37 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -808,6 +808,9 @@ QemuOptsList qemu_chardev_opts = { .name = "port", .type = QEMU_OPT_STRING, },{ + .name = "fd", + .type = QEMU_OPT_STRING, + },{ .name = "localaddr", .type = QEMU_OPT_STRING, },{ |