]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
jail: Drain the pipes after the process has terminated
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Mar 2025 10:42:19 +0000 (10:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Mar 2025 10:42:19 +0000 (10:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/jail.c

index 8e82f49719630beed24e1999eb4f107f2621fb00..edb32b2dc57fe2966896faf2ecc3e776b08b0808 100644 (file)
@@ -507,6 +507,30 @@ static int pakfire_jail_stderr(sd_event_source* source, int fd, unsigned int eve
        return pakfire_jail_output(exec->jail, fd, events, exec->streams.stderr);
 }
 
+static int pakfire_jail_drain(sd_event_source* source, void* data) {
+       struct pakfire_jail_exec* exec = data;
+       struct pakfire_jail* self = exec->jail;
+       int r;
+
+       DEBUG(self->ctx, "Draining the pipes...\n");
+
+       // Drain standard output
+       if (exec->pipes.stdout[0] >= 0) {
+               r = pakfire_jail_stdout(source, exec->pipes.stdout[0], EPOLLIN, data);
+               if (r < 0)
+                       return r;
+       }
+
+       // Drain standard error
+       if (exec->pipes.stderr[0] >= 0) {
+               r = pakfire_jail_stderr(source, exec->pipes.stderr[0], EPOLLIN, data);
+               if (r < 0)
+                       return r;
+       }
+
+       return 0;
+}
+
 /*
        This function replaces any logging in the child process.
 
@@ -1331,6 +1355,13 @@ static int pakfire_jail_parent(struct pakfire_jail* jail, struct pakfire_jail_ex
                }
        }
 
+       // Drain the pipes when exiting the loop
+       r = sd_event_add_exit(ctx->loop, NULL, pakfire_jail_drain, ctx);
+       if (r < 0) {
+               ERROR(jail->ctx, "Failed to register the drain event: %s\n", strerror(-r));
+               return r;
+       }
+
        // Setup logging
        r = pakfire_log_stream_in_parent(ctx->streams.INFO, ctx->loop);
        if (r)