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>
10 #include <ucw/sighandler.h>
16 static int sig_handler_nest[NSIG];
17 static struct sigaction sig_handler_old[NSIG];
20 signal_handler_internal(int sig)
22 struct ucwlib_context *ctx = ucwlib_thread_context();
23 if (!ctx->signal_handlers || !ctx->signal_handlers[sig] || ctx->signal_handlers[sig](sig))
28 handle_signal(int signum)
31 if (!sig_handler_nest[signum]++)
34 bzero(&act, sizeof(act));
35 act.sa_handler = signal_handler_internal;
36 act.sa_flags = SA_NODEFER;
37 if (sigaction(signum, &act, &sig_handler_old[signum]) < 0)
44 unhandle_signal(int signum)
47 ASSERT(sig_handler_nest[signum]);
48 if (!--sig_handler_nest[signum])
50 if (sigaction(signum, &sig_handler_old[signum], NULL) < 0)
57 set_signal_handler(int signum, ucw_sighandler_t newh)
59 struct ucwlib_context *ctx = ucwlib_thread_context();
60 if (!ctx->signal_handlers)
61 ctx->signal_handlers = xmalloc_zero(NSIG * sizeof(ucw_sighandler_t));
62 ucw_sighandler_t old = ctx->signal_handlers[signum];
63 ctx->signal_handlers[signum] = newh;