From: Michael Tremer Date: Tue, 25 Mar 2025 11:06:46 +0000 (+0000) Subject: jail: Cleanly terminate on SIGINT X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0949dfd54aa92b99f74886c3665db6409f85cc5d;p=pakfire.git jail: Cleanly terminate on SIGINT Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/jail.c b/src/pakfire/jail.c index 81e6844f..d02f9362 100644 --- a/src/pakfire/jail.c +++ b/src/pakfire/jail.c @@ -1142,27 +1142,30 @@ static int pakfire_jail_SIGCHLD( return 0; } -static int pakfire_jail_SIGTERM( - sd_event_source* source, const struct signalfd_siginfo* si, void* data) { - struct pakfire_jail_exec* ctx = data; - struct pakfire_jail* self = ctx->jail; +/* + Immediately terminates the process that is running inside the jail +*/ +static int pakfire_jail_terminate(sd_event_source* source, + const struct signalfd_siginfo* si, void* data) { + struct pakfire_jail_exec* exec = data; + struct pakfire_jail* self = exec->jail; int r; // Log action - DEBUG(self->ctx, "Received SIGTERM\n"); + DEBUG(self->ctx, "Received %s\n", strsignal(si->ssi_signo)); // Fail if we don't have a PID file descriptor - if (ctx->pidfd < 0) + if (exec->pidfd < 0) return -ENOTSUP; // Send a signal to the child process - r = pidfd_send_signal(ctx->pidfd, SIGKILL, NULL, 0); + r = pidfd_send_signal(exec->pidfd, SIGKILL, NULL, 0); if (r < 0) { ERROR(self->ctx, "Could not terminate jail: %m\n"); return -errno; } - return 0; + return sd_event_exit(exec->loop, 128 + si->ssi_signo); } /* @@ -1767,12 +1770,20 @@ static int __pakfire_jail_exec(struct pakfire_jail* jail, // Listen for SIGTERM r = sd_event_add_signal(ctx.loop, NULL, SIGTERM|SD_EVENT_SIGNAL_PROCMASK, - pakfire_jail_SIGTERM, &ctx); + pakfire_jail_terminate, &ctx); if (r < 0) { ERROR(jail->ctx, "Failed to register SIGTERM: %s\n", strerror(-r)); goto ERROR; } + // Listen for SIGINT + r = sd_event_add_signal(ctx.loop, NULL, SIGINT|SD_EVENT_SIGNAL_PROCMASK, + pakfire_jail_terminate, &ctx); + if (r < 0) { + ERROR(jail->ctx, "Failed to register SIGINT: %s\n", strerror(-r)); + goto ERROR; + } + // Set the size for the input buffer pakfire_buffer_set_max_length(&ctx.stdin, 4096);