char-stdio: avoid closing stdio fds

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 <filip.hejsek@gmail.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-ID: <20261017-fix-stdio-close-v3-1-cfff0122a2a4@gmail.com>
This commit is contained in:
Filip Hejsek authored and Marc-André Lureau committed 2026-11-01 16:06:27 +04:00
1 parent e562be56cc
commit aa523ea83d
1 file changed
+15 -1
+15 -1
View File
@@ -114,6 +114,7 @@ static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
StdioChardev *s = STDIO_CHARDEV(chr);
ChardevStdio *opts = backend->u.stdio.data;
struct sigaction act;
int fd_in, fd_out;
if (is_daemonized()) {
error_setg(errp, "cannot use stdio with -daemonize");
@@ -133,7 +134,20 @@ static bool stdio_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
return false;
}
if (!qemu_chr_open_fd(chr, 0, 1, errp)) {
fd_in = qemu_dup(0);
if (fd_in < 0) {
error_setg_errno(errp, errno, "Failed to dup stdin fd");
return false;
}
fd_out = qemu_dup(1);
if (fd_out < 0) {
error_setg_errno(errp, errno, "Failed to dup stdout fd");
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;
}