* docs/MAIL.FILTERING: added note on removing procmail -f or
[mmh] / sbr / signals.c
1
2 /*
3  * signals.c -- general signals interface for nmh
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <h/signals.h>
14
15
16 int
17 SIGPROCMASK (int how, const sigset_t *set, sigset_t *oset)
18 {
19 #ifdef POSIX_SIGNALS
20     return sigprocmask(how, set, oset);
21 #else
22 # ifdef BSD_SIGNALS
23     switch(how) {
24     case SIG_BLOCK:
25         *oset = sigblock(*set);
26         break;
27     case SIG_UNBLOCK:
28         sigfillset(*oset);
29         *oset = sigsetmask(*oset);
30         sigsetmask(*oset & ~(*set));
31         break;
32     case SIG_SETMASK:
33         *oset = sigsetmask(*set);
34         break;
35     default:
36         adios(NULL, "unknown flag in SIGPROCMASK");
37         break;
38     }
39     return 0;
40 # endif
41 #endif
42 }
43
44
45 /*
46  * A version of the function `signal' that uses reliable
47  * signals, if the machine supports them.  Also, (assuming
48  * OS support), it restarts interrupted system calls for all
49  * signals except SIGALRM.
50  */
51
52 SIGNAL_HANDLER
53 SIGNAL (int sig, SIGNAL_HANDLER func)
54 {
55 #ifdef POSIX_SIGNALS
56     struct sigaction act, oact;
57
58     act.sa_handler = func;
59     sigemptyset(&act.sa_mask);
60     act.sa_flags = 0;
61
62     if (sig == SIGALRM) {
63 # ifdef SA_INTERRUPT
64         act.sa_flags |= SA_INTERRUPT;     /* SunOS */
65 # endif
66     } else {
67 # ifdef SA_RESTART
68         act.sa_flags |= SA_RESTART;       /* SVR4, BSD4.4 */
69 # endif
70     }
71     if (sigaction(sig, &act, &oact) < 0)
72         return (SIG_ERR);
73     return (oact.sa_handler);
74 #else
75     return signal (sig, func);
76 #endif
77 }
78
79
80 /*
81  * A version of the function `signal' that will set
82  * the handler of `sig' to `func' if the signal is
83  * not currently set to SIG_IGN.  Also uses reliable
84  * signals if available.
85  */
86 SIGNAL_HANDLER
87 SIGNAL2 (int sig, SIGNAL_HANDLER func)
88 {
89 #ifdef POSIX_SIGNALS
90     struct sigaction act, oact;
91
92     if (sigaction(sig, NULL, &oact) < 0)
93         return (SIG_ERR);
94     if (oact.sa_handler != SIG_IGN) {
95         act.sa_handler = func;
96         sigemptyset(&act.sa_mask);
97         act.sa_flags = 0;
98
99         if (sig == SIGALRM) {
100 # ifdef SA_INTERRUPT
101             act.sa_flags |= SA_INTERRUPT;     /* SunOS */
102 # endif
103         } else {
104 # ifdef SA_RESTART
105             act.sa_flags |= SA_RESTART;       /* SVR4, BSD4.4 */
106 # endif
107         }
108         if (sigaction(sig, &act, &oact) < 0)
109             return (SIG_ERR);
110     }
111     return (oact.sa_handler);
112 #else
113     SIGNAL_HANDLER ofunc;
114
115     if ((ofunc = signal (sig, SIG_IGN)) != SIG_IGN)
116         signal (sig, func);
117     return ofunc;
118 #endif
119 }
120