fatal-signal: Run signal hooks outside of actual signal handlers.
[sliver-openvswitch.git] / lib / fatal-signal.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <config.h>
17 #include "fatal-signal.h"
18 #include <assert.h>
19 #include <errno.h>
20 #include <signal.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include "poll-loop.h"
28 #include "shash.h"
29 #include "socket-util.h"
30 #include "util.h"
31
32 #define THIS_MODULE VLM_fatal_signal
33 #include "vlog.h"
34
35 /* Signals to catch. */
36 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
37
38 /* Signals to catch as a sigset_t. */
39 static sigset_t fatal_signal_set;
40
41 /* Hooks to call upon catching a signal */
42 struct hook {
43     void (*func)(void *aux);
44     void *aux;
45     bool run_at_exit;
46 };
47 #define MAX_HOOKS 32
48 static struct hook hooks[MAX_HOOKS];
49 static size_t n_hooks;
50
51 static int signal_fds[2];
52 static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX;
53
54 /* Disabled by fatal_signal_fork()? */
55 static bool disabled;
56
57 static void fatal_signal_init(void);
58 static void atexit_handler(void);
59 static void call_hooks(int sig_nr);
60
61 static void
62 fatal_signal_init(void)
63 {
64     static bool inited = false;
65
66     if (!inited) {
67         size_t i;
68
69         inited = true;
70
71         if (pipe(signal_fds)) {
72             ovs_fatal(errno, "could not create pipe");
73         }
74         set_nonblocking(signal_fds[0]);
75         set_nonblocking(signal_fds[1]);
76
77         sigemptyset(&fatal_signal_set);
78         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
79             int sig_nr = fatal_signals[i];
80             struct sigaction old_sa;
81
82             sigaddset(&fatal_signal_set, sig_nr);
83             if (sigaction(sig_nr, NULL, &old_sa)) {
84                 ovs_fatal(errno, "sigaction");
85             }
86             if (old_sa.sa_handler == SIG_DFL
87                 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
88                 ovs_fatal(errno, "signal");
89             }
90         }
91         atexit(atexit_handler);
92     }
93 }
94
95 /* Registers 'hook' to be called when a process termination signal is raised.
96  * If 'run_at_exit' is true, 'hook' is also called during normal process
97  * termination, e.g. when exit() is called or when main() returns.
98  *
99  * The hook is not called immediately from the signal handler but rather the
100  * next time the poll loop iterates, so it is freed from the usual restrictions
101  * on signal handler functions. */
102 void
103 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
104 {
105     fatal_signal_init();
106     assert(n_hooks < MAX_HOOKS);
107     hooks[n_hooks].func = func;
108     hooks[n_hooks].aux = aux;
109     hooks[n_hooks].run_at_exit = run_at_exit;
110     n_hooks++;
111 }
112
113 /* Handles fatal signal number 'sig_nr'.
114  *
115  * Ordinarily this is the actual signal handler.  When other code needs to
116  * handle one of our signals, however, it can register for that signal and, if
117  * and when necessary, call this function to do fatal signal processing for it
118  * and terminate the process.  Currently only timeval.c does this, for SIGALRM.
119  * (It is not important whether the other code sets up its signal handler
120  * before or after this file, because this file will only set up a signal
121  * handler in the case where the signal has its default handling.)  */
122 void
123 fatal_signal_handler(int sig_nr)
124 {
125     ignore(write(signal_fds[1], "", 1));
126     stored_sig_nr = sig_nr;
127 }
128
129 void
130 fatal_signal_run(void)
131 {
132     int sig_nr = stored_sig_nr;
133
134     if (sig_nr != SIG_ATOMIC_MAX) {
135         call_hooks(sig_nr);
136
137         /* Re-raise the signal with the default handling so that the program
138          * termination status reflects that we were killed by this signal */
139         signal(sig_nr, SIG_DFL);
140         raise(sig_nr);
141     }
142 }
143
144 void
145 fatal_signal_wait(void)
146 {
147     poll_fd_wait(signal_fds[0], POLLIN);
148 }
149
150 static void
151 atexit_handler(void)
152 {
153     if (!disabled) {
154         call_hooks(0);
155     }
156 }
157
158 static void
159 call_hooks(int sig_nr)
160 {
161     static volatile sig_atomic_t recurse = 0;
162     if (!recurse) {
163         size_t i;
164
165         recurse = 1;
166
167         for (i = 0; i < n_hooks; i++) {
168             struct hook *h = &hooks[i];
169             if (sig_nr || h->run_at_exit) {
170                 h->func(h->aux);
171             }
172         }
173     }
174 }
175 \f
176 static struct shash files = SHASH_INITIALIZER(&files);
177
178 static void unlink_files(void *aux);
179 static void do_unlink_files(void);
180
181 /* Registers 'file' to be unlinked when the program terminates via exit() or a
182  * fatal signal. */
183 void
184 fatal_signal_add_file_to_unlink(const char *file)
185 {
186     static bool added_hook = false;
187     if (!added_hook) {
188         added_hook = true;
189         fatal_signal_add_hook(unlink_files, NULL, true);
190     }
191
192     if (!shash_find(&files, file)) {
193         shash_add(&files, file, NULL);
194     }
195 }
196
197 /* Unregisters 'file' from being unlinked when the program terminates via
198  * exit() or a fatal signal. */
199 void
200 fatal_signal_remove_file_to_unlink(const char *file)
201 {
202     struct shash_node *node;
203
204     node = shash_find(&files, file);
205     if (node) {
206         shash_delete(&files, node);
207     }
208 }
209
210 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
211  * Returns 0 if successful, otherwise a positive errno value. */
212 int
213 fatal_signal_unlink_file_now(const char *file)
214 {
215     int error = unlink(file) ? errno : 0;
216     if (error) {
217         VLOG_WARN("could not unlink \"%s\" (%s)", file, strerror(error));
218     }
219
220     fatal_signal_remove_file_to_unlink(file);
221
222     return error;
223 }
224
225 static void
226 unlink_files(void *aux UNUSED)
227 {
228     do_unlink_files(); 
229 }
230
231 static void
232 do_unlink_files(void)
233 {
234     struct shash_node *node;
235
236     SHASH_FOR_EACH (node, &files) {
237         unlink(node->name);
238     }
239 }
240 \f
241 /* Disables the fatal signal hook mechanism.  Following a fork, one of the
242  * resulting processes can call this function to allow it to terminate without
243  * triggering fatal signal processing or removing files.  Fatal signal
244  * processing is still enabled in the other process. */
245 void
246 fatal_signal_fork(void)
247 {
248     size_t i;
249
250     disabled = true;
251
252     for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
253         int sig_nr = fatal_signals[i];
254         if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
255             signal(sig_nr, SIG_IGN);
256         }
257     }
258
259     /* Raise any signals that we have already received with the default
260      * handler. */
261     if (stored_sig_nr != SIG_ATOMIC_MAX) {
262         raise(stored_sig_nr);
263     }
264 }