io: make sure we don't close stdio fds
PATCH 1/2 char-stdio: avoid closing stdio fds
In reply to Filip HejsekUse a duplicate of stdin and stdout fds to avoid closing the standard descriptors when the stdio chardev is destroyed.
--- chardev/char-stdio.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
chardev/char-stdio.c +12 -1
@@ -108,7 +108,18 @@ static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)return false; } -if (!qemu_chr_open_fd (chr,0 ,1 , errp)) { +int fd_in =qemu_dup (0 ); +if (fd_in <0 ) { +return false; + } +int fd_out =qemu_dup (1 ); +if (fd_out <0 ) { +qemu_close (fd_in); +return false; + } +if (!qemu_chr_open_fd (chr, fd_in, fd_out, errp)) { +qemu_close (fd_in); +qemu_close (fd_out);return false; }
Signature · 1 line
2.56.0
PATCH 2/2 io: report a warning when a file channel is created from a stdio fd
In reply to Filip HejsekA channel closes its fd when the last reference to it is dropped. Closing stdio fds is typically unwanted, so warn when they are passed to qio_channel_file_new_fd().
--- io/channel-file.c | 6 ++++++ 1 file changed, 6 insertions(+)
io/channel-file.c +6 -0
@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "io/channel-file.h" #include "io/channel-util.h" #include "io/channel-watch.h"
@@ -32,6 +33,11 @@ qio_channel_file_new_fd(int fd) {QIOChannelFile *ioc; +if (fd <=STDERR_FILENO ) { +warn_report ("potential bug: creating a channel from stdio fd %d; " +"the fd will be closed when the channel is destroyed" , fd); + } + ioc =QIO_CHANNEL_FILE (object_new (TYPE_QIO_CHANNEL_FILE )); ioc->fd = fd;
Signature · 1 line
2.56.0
cover io: make sure we don't close stdio fds
The stdio chardev currently closes the stdin and stdout fds when it is destroyed. This means that, assuming char0 is a stdio chardev, invoking the HMP command
chardev-change char0 null
will cause QEMU to run with descriptors 0 and 1 closed. That's not a great situation, as:
- the stdio chardev cannot be recreated
- code assuming fds 0 and 1 are open will misbehave
- the fds 0 and 1 will get assigned to newly opened files, possibly causing them to be confused for actual stdin/stdout
The fist patch fixes the problem by duplicating the fds before they get passed to qio_channel_file_new_fd(), the second patch adds a warning that is printed when qio_channel_file_new_fd() is called with a stdio fd.
---
Filip Hejsek (2):
char-stdio: avoid closing stdio fds
io: report a warning when a file channel is created from a stdio fd
chardev/char-stdio.c | 13 ++++++++++++- io/channel-file.c | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-)
---
base-commit: 615ece3c406b262996e569f29ea6eed0d81c4d8d
change-id: 20261012-fix-stdio-close-98864055d473
Best regards,
Signature · 1 line
Filip Hejsek <[email protected]>
Re: [PATCH 2/2] io: report a warning when a file channel is created from a stdio fd
In reply to Filip HejsekQuoted 12 lines
A channel closes its fd when the last reference to it is dropped. Closing stdio fds is typically unwanted, so warn when they are passed to
qio_channel_file_new_fd().Signed-off-by Filip Hejsek <[email protected]>Message-ID: <[email protected]>
io/channel-file.c +0
-0
io/channel-file.c line 22
Quoted 4 lines
io/channel-file.c +1
-0@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/error-report.h"
place it below, with other qemu/ headers to preserve alphabetical ordering
Quoted 5 lines
io/channel-file.c +0
-0#include "io/channel-file.h" #include "io/channel-util.h" #include "io/channel-watch.h" @@ -32,6 +33,11 @@ qio_channel_file_new_fd(int fd) {
io/channel-file.c qio_channel_file_new_fd(int fd) · line 43
Quoted 10 lines
io/channel-file.c +5
-0QIOChannelFile *ioc; +if (fd <=STDERR_FILENO ) { +warn_report ("potential bug: creating a channel from stdio fd %d; " +"the fd will be closed when the channel is destroyed" , fd); + } + ioc =QIO_CHANNEL_FILE (object_new (TYPE_QIO_CHANNEL_FILE )); ioc->fd = fd;
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [PATCH 1/2] char-stdio: avoid closing stdio fds
In reply to Filip HejsekQuoted 11 lines
Use a duplicate of stdin and stdout fds to avoid closing the standard descriptors when the stdio chardev is destroyed.
Signed-off-by Filip Hejsek <[email protected]>Message-ID: <[email protected]>
chardev/char-stdio.c +0
-0
chardev/char-stdio.c static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) · line 111
Quoted 5 lines
chardev/char-stdio.c +1
-1@@ -108,7 +108,18 @@ static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)return false; } -if (!qemu_chr_open_fd (chr,0 ,1 , errp)) { +int fd_in =qemu_dup (0 );
QEMU style requires declarations at the beginning of blocks. Same applies to fd_out below.
chardev/char-stdio.c static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) · line 113
Quoted 2 lines
chardev/char-stdio.c +2
-0+if (fd_in <0 ) { +return false;
qemu_dup() can fail (EMFILE, EBADF). This returns false without setting *errp. Should be:
error_setg_errno(errp, errno, "Failed to dup stdin fd");
chardev/char-stdio.c static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp) · line 118
Quoted 5 lines
chardev/char-stdio.c +5
-0+ } +int fd_out =qemu_dup (1 ); +if (fd_out <0 ) { +qemu_close (fd_in); +return false;
Same
Signature · 1 line
Marc-André Lureau <[email protected]>