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