diff mbox series

[PULL,05/14] qemu-nbd: handle dup2() error when qemu-nbd finished setup process

Message ID 20230719202736.2675295-21-eblake@redhat.com (mailing list archive)
State New, archived
Headers show
Series [PULL,01/14] qemu-nbd: pass structure into nbd_client_thread instead of plain char* | expand

Commit Message

Eric Blake July 19, 2023, 8:27 p.m. UTC
From: "Denis V. Lunev" <den@openvz.org>

Fail on error, we are in trouble.

Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Eric Blake <eblake@redhat.com>
CC: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-ID: <20230717145544.194786-6-den@openvz.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: avoid intermediate variable]
Signed-off-by: Eric Blake <eblake@redhat.com>
---
 qemu-nbd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/qemu-nbd.c b/qemu-nbd.c
index f27613cb572..e30c9ac1793 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -323,7 +323,11 @@  static void *nbd_client_thread(void *arg)
                 opts->device, srcpath);
     } else {
         /* Close stderr so that the qemu-nbd process exits.  */
-        dup2(STDOUT_FILENO, STDERR_FILENO);
+        if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
+            error_report("Could not set stderr to /dev/null: %s",
+                         strerror(errno));
+            exit(EXIT_FAILURE);
+        }
     }

     if (nbd_client(fd) < 0) {
@@ -1171,7 +1175,11 @@  int main(int argc, char **argv)
     }

     if (fork_process) {
-        dup2(STDOUT_FILENO, STDERR_FILENO);
+        if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
+            error_report("Could not set stderr to /dev/null: %s",
+                         strerror(errno));
+            exit(EXIT_FAILURE);
+        }
     }

     state = RUNNING;