summary refs log tree commit diff stats
path: root/qemu-nbd.c
diff options
context:
space:
mode:
authorDenis V. Lunev <den@openvz.org>2023-07-17 16:55:42 +0200
committerEric Blake <eblake@redhat.com>2023-07-19 15:25:27 -0500
commite0892ced0534b1009d009e32c3e338456018ab15 (patch)
tree2be6d2e5d06253bc2d07bdb4513dc59efdcf49df /qemu-nbd.c
parent1dc8215118ca5b99669c3bf27082a365aef16ea6 (diff)
downloadfocaccia-qemu-e0892ced0534b1009d009e32c3e338456018ab15.tar.gz
focaccia-qemu-e0892ced0534b1009d009e32c3e338456018ab15.zip
qemu-nbd: properly report error on error in dup2() after qemu_daemon()
We are trying to temporarily redirect stderr of daemonized process to
a pipe to report a error and get failed. In that case we could not
use error_report() helper, but should write the message directly into
the problematic pipe.

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-4-den@openvz.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: rearrange patch series, fix typo]
Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'qemu-nbd.c')
-rw-r--r--qemu-nbd.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 5a8ae1f747..f27613cb57 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -940,7 +940,20 @@ int main(int argc, char **argv)
             saved_errno = errno;    /* dup2 will overwrite error below */
 
             /* Temporarily redirect stderr to the parent's pipe...  */
-            dup2(stderr_fd[1], STDERR_FILENO);
+            if (dup2(stderr_fd[1], STDERR_FILENO) < 0) {
+                char str[256];
+                snprintf(str, sizeof(str),
+                         "%s: Failed to link stderr to the pipe: %s\n",
+                         g_get_prgname(), strerror(errno));
+                /*
+                 * We are unable to use error_report() here as we need to get
+                 * stderr pointed to the parent's pipe. Write to that pipe
+                 * manually.
+                 */
+                ret = write(stderr_fd[1], str, strlen(str));
+                exit(EXIT_FAILURE);
+            }
+
             if (ret < 0) {
                 error_report("Failed to daemonize: %s", strerror(saved_errno));
                 exit(EXIT_FAILURE);