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