From: Michael Tremer Date: Wed, 3 Aug 2022 16:39:33 +0000 (+0000) Subject: jail: Actually connect stdout/stderr to logger X-Git-Tag: 0.9.28~592 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ebfb7cb293d35acdec5526c023650e8951c6db6;p=pakfire.git jail: Actually connect stdout/stderr to logger Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/jail.c b/src/libpakfire/jail.c index 4a3fe004b..221d08da1 100644 --- a/src/libpakfire/jail.c +++ b/src/libpakfire/jail.c @@ -434,10 +434,21 @@ static int pakfire_jail_wait(struct pakfire_jail* jail, struct pakfire_jail_exec int r = 0; // Fetch file descriptors from context - const int stdout = ctx->pipes.stdout[1]; - const int stderr = ctx->pipes.stderr[1]; + const int stdout = ctx->pipes.stdout[0]; + const int stderr = ctx->pipes.stderr[0]; const int pidfd = ctx->pidfd; + // Close any unused file descriptors + if (ctx->pipes.stdout[1]) { + close(ctx->pipes.stdout[1]); + ctx->pipes.stdout[1] = 0; + } + if (ctx->pipes.stderr[1]) { + close(ctx->pipes.stderr[1]); + ctx->pipes.stderr[1] = 0; + } + + // Make a list of all file descriptors we are interested in int fds[] = { stdout, stderr, pidfd, }; @@ -1027,6 +1038,29 @@ static int pakfire_jail_child(struct pakfire_jail* jail, struct pakfire_jail_exe } } + // Connect standard output and error + if (ctx->pipes.stdout[1] && ctx->pipes.stderr[1]) { + r = dup2(ctx->pipes.stdout[1], STDOUT_FILENO); + if (r < 0) { + ERROR(jail->pakfire, "Could not connect fd %d to stdout: %m\n", + ctx->pipes.stdout[1]); + + return 1; + } + + r = dup2(ctx->pipes.stderr[1], STDERR_FILENO); + if (r < 0) { + ERROR(jail->pakfire, "Could not connect fd %d to stderr: %m\n", + ctx->pipes.stderr[1]); + + return 1; + } + + // Close the reading sides of the pipe + close(ctx->pipes.stdout[0]); + close(ctx->pipes.stderr[0]); + } + // Reset open file limit (http://0pointer.net/blog/file-descriptor-limits.html) r = pakfire_rlimit_reset_nofile(jail->pakfire); if (r)