diff options
author | Marc-André Lureau | 2021-07-23 11:59:50 +0200 |
---|---|---|
committer | Marc-André Lureau | 2021-08-05 14:15:33 +0200 |
commit | 733ba020846ccd21d832f2e9b62387a86c5ab8f1 (patch) | |
tree | 3ae53a9587fed486137cee0cd6d13e9fbe3edf02 /chardev | |
parent | chardev: fix qemu_chr_open_fd() being called with fd=-1 (diff) | |
download | qemu-733ba020846ccd21d832f2e9b62387a86c5ab8f1.tar.gz qemu-733ba020846ccd21d832f2e9b62387a86c5ab8f1.tar.xz qemu-733ba020846ccd21d832f2e9b62387a86c5ab8f1.zip |
chardev: fix qemu_chr_open_fd() with fd_in==fd_out
The "serial" chardev calls qemu_chr_open_fd() with the same fd. This
may lead to double-close as each QIOChannel owns the fd.
Instead, share the reference to the same QIOChannel.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'chardev')
-rw-r--r-- | chardev/char-fd.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/chardev/char-fd.c b/chardev/char-fd.c index c11b1037f9..93c56913b4 100644 --- a/chardev/char-fd.c +++ b/chardev/char-fd.c @@ -211,20 +211,31 @@ void qemu_chr_open_fd(Chardev *chr, int fd_in, int fd_out) { FDChardev *s = FD_CHARDEV(chr); - char *name; + g_autofree char *name = NULL; + + if (fd_out >= 0) { + qemu_set_nonblock(fd_out); + } + + if (fd_out == fd_in && fd_in >= 0) { + s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in)); + name = g_strdup_printf("chardev-file-%s", chr->label); + qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name); + s->ioc_out = QIO_CHANNEL(object_ref(s->ioc_in)); + return; + } if (fd_in >= 0) { s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in)); name = g_strdup_printf("chardev-file-in-%s", chr->label); qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name); - g_free(name); } + if (fd_out >= 0) { s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out)); + g_free(name); name = g_strdup_printf("chardev-file-out-%s", chr->label); qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name); - g_free(name); - qemu_set_nonblock(fd_out); } } |