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]>
---
 chardev/char-stdio.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/chardev/char-stdio.c b/chardev/char-stdio.c
index fe1cbb2d88..79092c4a75 100644
--- a/chardev/char-stdio.c
+++ b/chardev/char-stdio.c
@@ -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;
     }
 

-- 
2.56.0


Reply via email to