sigchain.con commit Merge branch 'jk/xdiff-clamp-funcname-context-index' (c3d4c20)
   1#include "cache.h"
   2#include "sigchain.h"
   3
   4#define SIGCHAIN_MAX_SIGNALS 32
   5
   6struct sigchain_signal {
   7        sigchain_fun *old;
   8        int n;
   9        int alloc;
  10};
  11static struct sigchain_signal signals[SIGCHAIN_MAX_SIGNALS];
  12
  13static void check_signum(int sig)
  14{
  15        if (sig < 1 || sig >= SIGCHAIN_MAX_SIGNALS)
  16                BUG("signal out of range: %d", sig);
  17}
  18
  19int sigchain_push(int sig, sigchain_fun f)
  20{
  21        struct sigchain_signal *s = signals + sig;
  22        check_signum(sig);
  23
  24        ALLOC_GROW(s->old, s->n + 1, s->alloc);
  25        s->old[s->n] = signal(sig, f);
  26        if (s->old[s->n] == SIG_ERR)
  27                return -1;
  28        s->n++;
  29        return 0;
  30}
  31
  32int sigchain_pop(int sig)
  33{
  34        struct sigchain_signal *s = signals + sig;
  35        check_signum(sig);
  36        if (s->n < 1)
  37                return 0;
  38
  39        if (signal(sig, s->old[s->n - 1]) == SIG_ERR)
  40                return -1;
  41        s->n--;
  42        return 0;
  43}
  44
  45void sigchain_push_common(sigchain_fun f)
  46{
  47        sigchain_push(SIGINT, f);
  48        sigchain_push(SIGHUP, f);
  49        sigchain_push(SIGTERM, f);
  50        sigchain_push(SIGQUIT, f);
  51        sigchain_push(SIGPIPE, f);
  52}
  53
  54void sigchain_pop_common(void)
  55{
  56        sigchain_pop(SIGPIPE);
  57        sigchain_pop(SIGQUIT);
  58        sigchain_pop(SIGTERM);
  59        sigchain_pop(SIGHUP);
  60        sigchain_pop(SIGINT);
  61}