chiark / gitweb /
rm some todos
[inn-innduct.git] / lib / xsignal.c
1 /*  $Id: xsignal.c 5610 2002-08-18 22:22:52Z rra $
2 **
3 **  A reliable implementation of signal for System V systems.
4 **
5 **  Two functions are provided, xsignal and xsignal_norestart.  The former
6 **  attempts to set system calls to be restarted and the latter does not.
7 **
8 **  Be aware that there's weird declaration stuff going on here; a signal
9 **  handler is a pointer to a function taking an int and returning void.
10 **  We typedef this as sig_handler_type for clearer code.
11 */
12
13 #include "config.h"
14 #include "libinn.h"
15 #include <signal.h>
16
17 typedef void (*sig_handler_type)(int);
18
19 #ifdef HAVE_SIGACTION
20
21 sig_handler_type
22 xsignal(int signum, sig_handler_type sigfunc)
23 {
24     struct sigaction act, oact;
25
26     act.sa_handler = sigfunc;
27     sigemptyset(&act.sa_mask);
28
29     /* Try to restart system calls if possible. */
30 #ifdef SA_RESTART
31     act.sa_flags = SA_RESTART;
32 #else
33     act.sa_flags = 0;
34 #endif
35
36     if (sigaction(signum, &act, &oact) < 0)
37         return SIG_ERR;
38     return oact.sa_handler;
39 }
40
41 sig_handler_type
42 xsignal_norestart(int signum, sig_handler_type sigfunc)
43 {
44     struct sigaction act, oact;
45
46     act.sa_handler = sigfunc;
47     sigemptyset(&act.sa_mask);
48
49     /* Try not to restart system calls. */
50 #ifdef SA_INTERRUPT
51     act.sa_flags = SA_INTERRUPT;
52 #else
53     act.sa_flags = 0;
54 #endif
55
56     if (sigaction(signum, &act, &oact) < 0)
57         return SIG_ERR;
58     return oact.sa_handler;
59 }
60
61 #else /* !HAVE_SIGACTION */
62
63 sig_handler_type
64 xsignal(int signum, sig_handler_type sigfunc)
65 {
66     return signal(signum, sigfunc);
67 }
68
69 sig_handler_type
70 xsignal_norestart(int signum, sig_handler_type sigfunc)
71 {
72     return signal(signum, sigfunc);
73 }
74
75 #endif /* !HAVE_SIGACTION */