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