From 33402c0dfde3f0130b760b67889c825baf0008bc Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 25 Mar 2025 10:42:19 +0000 Subject: [PATCH] jail: Drain the pipes after the process has terminated Signed-off-by: Michael Tremer --- src/pakfire/jail.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/pakfire/jail.c b/src/pakfire/jail.c index 8e82f497..edb32b2d 100644 --- a/src/pakfire/jail.c +++ b/src/pakfire/jail.c @@ -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) -- 2.39.5