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);