1#include "cache.h"
2#include "sigchain.h"
34
#define SIGCHAIN_MAX_SIGNALS 32
56
struct sigchain_signal {
7sigchain_fun *old;
8int n;
9int alloc;
10};
11static struct sigchain_signal signals[SIGCHAIN_MAX_SIGNALS];
1213
static void check_signum(int sig)
14{
15if (sig < 1 || sig >= SIGCHAIN_MAX_SIGNALS)
16BUG("signal out of range: %d", sig);
17}
1819
int sigchain_push(int sig, sigchain_fun f)
20{
21struct sigchain_signal *s = signals + sig;
22check_signum(sig);
2324
ALLOC_GROW(s->old, s->n + 1, s->alloc);
25s->old[s->n] = signal(sig, f);
26if (s->old[s->n] == SIG_ERR)
27return -1;
28s->n++;
29return 0;
30}
3132
int sigchain_pop(int sig)
33{
34struct sigchain_signal *s = signals + sig;
35check_signum(sig);
36if (s->n < 1)
37return 0;
3839
if (signal(sig, s->old[s->n - 1]) == SIG_ERR)
40return -1;
41s->n--;
42return 0;
43}
4445
void sigchain_push_common(sigchain_fun f)
46{
47sigchain_push(SIGINT, f);
48sigchain_push(SIGHUP, f);
49sigchain_push(SIGTERM, f);
50sigchain_push(SIGQUIT, f);
51sigchain_push(SIGPIPE, f);
52}
5354
void sigchain_pop_common(void)
55{
56sigchain_pop(SIGPIPE);
57sigchain_pop(SIGQUIT);
58sigchain_pop(SIGTERM);
59sigchain_pop(SIGHUP);
60sigchain_pop(SIGINT);
61}