]> diplodocus.org Git - nmh/blob - sbr/signals.c
Fix invalid pointer arithmetic.
[nmh] / sbr / signals.c
1 /* signals.c -- general signals interface for nmh
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/signals.h>
10 #include "m_mktemp.h"
11
12
13 /*
14 * A version of the function `signal' that uses reliable
15 * signals, if the machine supports them. Also, (assuming
16 * OS support), it restarts interrupted system calls for all
17 * signals except SIGALRM.
18 *
19 * Since we are now assuming POSIX signal support everywhere, we always
20 * use reliable signals.
21 */
22
23 SIGNAL_HANDLER
24 SIGNAL (int sig, SIGNAL_HANDLER func)
25 {
26 struct sigaction act, oact;
27
28 act.sa_handler = func;
29 sigemptyset(&act.sa_mask);
30 act.sa_flags = 0;
31
32 if (sig == SIGALRM) {
33 # ifdef SA_INTERRUPT
34 act.sa_flags |= SA_INTERRUPT; /* SunOS */
35 # endif
36 } else {
37 # ifdef SA_RESTART
38 act.sa_flags |= SA_RESTART; /* SVR4, BSD4.4 */
39 # endif
40 }
41 if (sigaction(sig, &act, &oact) < 0)
42 return SIG_ERR;
43 return oact.sa_handler;
44 }
45
46
47 /*
48 * A version of the function `signal' that will set
49 * the handler of `sig' to `func' if the signal is
50 * not currently set to SIG_IGN. Also uses reliable
51 * signals if available.
52 */
53 SIGNAL_HANDLER
54 SIGNAL2 (int sig, SIGNAL_HANDLER func)
55 {
56 struct sigaction act, oact;
57
58 if (sigaction(sig, NULL, &oact) < 0)
59 return SIG_ERR;
60 if (oact.sa_handler != SIG_IGN) {
61 act.sa_handler = func;
62 sigemptyset(&act.sa_mask);
63 act.sa_flags = 0;
64
65 if (sig == SIGALRM) {
66 # ifdef SA_INTERRUPT
67 act.sa_flags |= SA_INTERRUPT; /* SunOS */
68 # endif
69 } else {
70 # ifdef SA_RESTART
71 act.sa_flags |= SA_RESTART; /* SVR4, BSD4.4 */
72 # endif
73 }
74 if (sigaction(sig, &act, &oact) < 0)
75 return SIG_ERR;
76 }
77 return oact.sa_handler;
78 }
79
80
81 /*
82 * For use by nmh_init().
83 */
84 int
85 setup_signal_handlers(void) {
86 /*
87 * Catch HUP, INT, QUIT, and TERM so that we can clean up tmp
88 * files when the user terminates the process early. And also a
89 * few other common signals that can be thrown due to bugs, stack
90 * overflow, etc.
91 */
92
93 if (SIGNAL(SIGHUP, remove_registered_files) == SIG_ERR ||
94 SIGNAL(SIGINT, remove_registered_files) == SIG_ERR ||
95 SIGNAL(SIGQUIT, remove_registered_files) == SIG_ERR ||
96 SIGNAL(SIGTERM, remove_registered_files) == SIG_ERR ||
97 SIGNAL(SIGILL, remove_registered_files) == SIG_ERR ||
98 # ifdef SIGBUS
99 SIGNAL(SIGBUS, remove_registered_files) == SIG_ERR ||
100 # endif
101 SIGNAL(SIGSEGV, remove_registered_files) == SIG_ERR) {
102 return NOTOK;
103 }
104
105 return OK;
106 }