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