]> git.ipfire.org Git - pakfire.git/commitdiff
jail: Cleanly terminate on SIGINT
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Mar 2025 11:06:46 +0000 (11:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Mar 2025 11:06:46 +0000 (11:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/jail.c

index 81e6844f6ae766cee370e50e0088e97a246544c3..d02f93623a2c4e282b0a808b727b0d04296f0b37 100644 (file)
@@ -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);