diff mbox series

[PULL,03/14] qemu-nbd: properly report error if qemu_daemon() is failed

Message ID 20230719202736.2675295-19-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>

errno has been overwritten by dup2() just below qemu_daemon() and thus
improperly returned to the caller. Fix accordingly.

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-5-den@openvz.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: reorder patch series]
Signed-off-by: Eric Blake <eblake@redhat.com>
---
 qemu-nbd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/qemu-nbd.c b/qemu-nbd.c
index 186ce9474c3..5a8ae1f7472 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -932,14 +932,17 @@  int main(int argc, char **argv)
             error_report("Failed to fork: %s", strerror(errno));
             exit(EXIT_FAILURE);
         } else if (pid == 0) {
+            int saved_errno;
+
             close(stderr_fd[0]);

             ret = qemu_daemon(1, 0);
+            saved_errno = errno;    /* dup2 will overwrite error below */

             /* Temporarily redirect stderr to the parent's pipe...  */
             dup2(stderr_fd[1], STDERR_FILENO);
             if (ret < 0) {
-                error_report("Failed to daemonize: %s", strerror(errno));
+                error_report("Failed to daemonize: %s", strerror(saved_errno));
                 exit(EXIT_FAILURE);
             }