Fixed explanation of why the unset SHELL test is uselessif /bin/sh is bash.
[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 /*
15  * A version of the function `signal' that uses reliable
16  * signals, if the machine supports them.  Also, (assuming
17  * OS support), it restarts interrupted system calls for all
18  * signals except SIGALRM.
19  *
20  * Since we are now assuming POSIX signal support everywhere, we always
21  * use reliable signals.
22  */
23
24 SIGNAL_HANDLER
25 SIGNAL (int sig, SIGNAL_HANDLER func)
26 {
27     struct sigaction act, oact;
28
29     act.sa_handler = func;
30     sigemptyset(&act.sa_mask);
31     act.sa_flags = 0;
32
33     if (sig == SIGALRM) {
34 # ifdef SA_INTERRUPT
35         act.sa_flags |= SA_INTERRUPT;     /* SunOS */
36 # endif
37     } else {
38 # ifdef SA_RESTART
39         act.sa_flags |= SA_RESTART;       /* SVR4, BSD4.4 */
40 # endif
41     }
42     if (sigaction(sig, &act, &oact) < 0)
43         return (SIG_ERR);
44     return (oact.sa_handler);
45 }
46
47
48 /*
49  * A version of the function `signal' that will set
50  * the handler of `sig' to `func' if the signal is
51  * not currently set to SIG_IGN.  Also uses reliable
52  * signals if available.
53  */
54 SIGNAL_HANDLER
55 SIGNAL2 (int sig, SIGNAL_HANDLER func)
56 {
57     struct sigaction act, oact;
58
59     if (sigaction(sig, NULL, &oact) < 0)
60         return (SIG_ERR);
61     if (oact.sa_handler != SIG_IGN) {
62         act.sa_handler = func;
63         sigemptyset(&act.sa_mask);
64         act.sa_flags = 0;
65
66         if (sig == SIGALRM) {
67 # ifdef SA_INTERRUPT
68             act.sa_flags |= SA_INTERRUPT;     /* SunOS */
69 # endif
70         } else {
71 # ifdef SA_RESTART
72             act.sa_flags |= SA_RESTART;       /* SVR4, BSD4.4 */
73 # endif
74         }
75         if (sigaction(sig, &act, &oact) < 0)
76             return (SIG_ERR);
77     }
78     return (oact.sa_handler);
79 }
80