Skip to content

Commit

Permalink
qemu-nbd: properly report error on error in dup2() after qemu_daemon()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Denis V. Lunev authored and ebblake committed Jul 19, 2023
1 parent 1dc8215 commit e0892ce
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion qemu-nbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e0892ce

Please sign in to comment.