2 * UCW Library -- Catching of signals and calling callback functions
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
5 * (c) 2006 Martin Mares <mj@ucw.cz>
9 #include "ucw/threads.h"
15 static int sig_handler_nest[NSIG];
16 static struct sigaction sig_handler_old[NSIG];
19 signal_handler_internal(int sig)
21 struct ucwlib_context *ctx = ucwlib_thread_context();
22 if (!ctx->signal_handlers || !ctx->signal_handlers[sig] || ctx->signal_handlers[sig](sig))
27 handle_signal(int signum)
30 if (!sig_handler_nest[signum]++)
33 bzero(&act, sizeof(act));
34 act.sa_handler = signal_handler_internal;
35 act.sa_flags = SA_NODEFER;
36 if (sigaction(signum, &act, &sig_handler_old[signum]) < 0)
43 unhandle_signal(int signum)
46 ASSERT(sig_handler_nest[signum]);
47 if (!--sig_handler_nest[signum])
49 if (sigaction(signum, &sig_handler_old[signum], NULL) < 0)
56 set_signal_handler(int signum, ucw_sighandler_t new)
58 struct ucwlib_context *ctx = ucwlib_thread_context();
59 if (!ctx->signal_handlers)
60 ctx->signal_handlers = xmalloc_zero(NSIG * sizeof(ucw_sighandler_t));
61 ucw_sighandler_t old = ctx->signal_handlers[signum];
62 ctx->signal_handlers[signum] = new;