]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
jail: Actually connect stdout/stderr to logger
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Aug 2022 16:39:33 +0000 (16:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Aug 2022 16:39:33 +0000 (16:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/jail.c

index 4a3fe004b98a18486d76c298701d92245fbb85f3..221d08da1cc6b181055d92e5ccbef1b963c2aa11 100644 (file)
@@ -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)