^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include "subcmd-util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "sigchain.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define SIGCHAIN_MAX_SIGNALS 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) struct sigchain_signal {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) sigchain_fun *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) int alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static struct sigchain_signal signals[SIGCHAIN_MAX_SIGNALS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static void check_signum(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (sig < 1 || sig >= SIGCHAIN_MAX_SIGNALS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) die("BUG: signal out of range: %d", sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static int sigchain_push(int sig, sigchain_fun f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct sigchain_signal *s = signals + sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) check_signum(sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ALLOC_GROW(s->old, s->n + 1, s->alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) s->old[s->n] = signal(sig, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (s->old[s->n] == SIG_ERR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) s->n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int sigchain_pop(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct sigchain_signal *s = signals + sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) check_signum(sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (s->n < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (signal(sig, s->old[s->n - 1]) == SIG_ERR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) s->n--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void sigchain_push_common(sigchain_fun f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) sigchain_push(SIGINT, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) sigchain_push(SIGHUP, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sigchain_push(SIGTERM, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) sigchain_push(SIGQUIT, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) sigchain_push(SIGPIPE, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }