ofp-util: Work on decoding OF1.1 flow_mods.
[openvswitch] / lib / fatal-signal.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "sset.h"
30 #include "signals.h"
31 #include "socket-util.h"
32 #include "util.h"
33 #include "vlog.h"
34
35 #include "type-props.h"
36
37 #ifndef SIG_ATOMIC_MAX
38 #define SIG_ATOMIC_MAX TYPE_MAXIMUM(sig_atomic_t)
39 #endif
40
41 VLOG_DEFINE_THIS_MODULE(fatal_signal);
42
43 /* Signals to catch. */
44 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
45
46 /* Signals to catch as a sigset_t. */
47 static sigset_t fatal_signal_set;
48
49 /* Hooks to call upon catching a signal */
50 struct hook {
51     void (*hook_cb)(void *aux);
52     void (*cancel_cb)(void *aux);
53     void *aux;
54     bool run_at_exit;
55 };
56 #define MAX_HOOKS 32
57 static struct hook hooks[MAX_HOOKS];
58 static size_t n_hooks;
59
60 static int signal_fds[2];
61 static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX;
62
63 static void fatal_signal_init(void);
64 static void atexit_handler(void);
65 static void call_hooks(int sig_nr);
66
67 static void
68 fatal_signal_init(void)
69 {
70     static bool inited = false;
71
72     if (!inited) {
73         size_t i;
74
75         inited = true;
76
77         xpipe(signal_fds);
78         xset_nonblocking(signal_fds[0]);
79         xset_nonblocking(signal_fds[1]);
80
81         sigemptyset(&fatal_signal_set);
82         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
83             int sig_nr = fatal_signals[i];
84             struct sigaction old_sa;
85
86             sigaddset(&fatal_signal_set, sig_nr);
87             xsigaction(sig_nr, NULL, &old_sa);
88             if (old_sa.sa_handler == SIG_DFL
89                 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
90                 VLOG_FATAL("signal failed (%s)", strerror(errno));
91             }
92         }
93         atexit(atexit_handler);
94     }
95 }
96
97 /* Registers 'hook_cb' to be called when a process termination signal is
98  * raised.  If 'run_at_exit' is true, 'hook_cb' is also called during normal
99  * process termination, e.g. when exit() is called or when main() returns.
100  *
101  * 'hook_cb' is not called immediately from the signal handler but rather the
102  * next time the poll loop iterates, so it is freed from the usual restrictions
103  * on signal handler functions.
104  *
105  * If the current process forks, fatal_signal_fork() may be called to clear the
106  * parent process's fatal signal hooks, so that 'hook_cb' is only called when
107  * the child terminates, not when the parent does.  When fatal_signal_fork() is
108  * called, it calls the 'cancel_cb' function if it is nonnull, passing 'aux',
109  * to notify that the hook has been canceled.  This allows the hook to free
110  * memory, etc. */
111 void
112 fatal_signal_add_hook(void (*hook_cb)(void *aux), void (*cancel_cb)(void *aux),
113                       void *aux, bool run_at_exit)
114 {
115     fatal_signal_init();
116
117     assert(n_hooks < MAX_HOOKS);
118     hooks[n_hooks].hook_cb = hook_cb;
119     hooks[n_hooks].cancel_cb = cancel_cb;
120     hooks[n_hooks].aux = aux;
121     hooks[n_hooks].run_at_exit = run_at_exit;
122     n_hooks++;
123 }
124
125 /* Handles fatal signal number 'sig_nr'.
126  *
127  * Ordinarily this is the actual signal handler.  When other code needs to
128  * handle one of our signals, however, it can register for that signal and, if
129  * and when necessary, call this function to do fatal signal processing for it
130  * and terminate the process.  Currently only timeval.c does this, for SIGALRM.
131  * (It is not important whether the other code sets up its signal handler
132  * before or after this file, because this file will only set up a signal
133  * handler in the case where the signal has its default handling.)  */
134 void
135 fatal_signal_handler(int sig_nr)
136 {
137     ignore(write(signal_fds[1], "", 1));
138     stored_sig_nr = sig_nr;
139 }
140
141 /* Check whether a fatal signal has occurred and, if so, call the fatal signal
142  * hooks and exit.
143  *
144  * This function is called automatically by poll_block(), but specialized
145  * programs that may not always call poll_block() on a regular basis should
146  * also call it periodically.  (Therefore, any function with "block" in its
147  * name should call fatal_signal_run() each time it is called, either directly
148  * or through poll_block(), because such functions can only used by specialized
149  * programs that can afford to block outside their main loop around
150  * poll_block().)
151  */
152 void
153 fatal_signal_run(void)
154 {
155     sig_atomic_t sig_nr;
156
157     fatal_signal_init();
158
159     sig_nr = stored_sig_nr;
160     if (sig_nr != SIG_ATOMIC_MAX) {
161         VLOG_WARN("terminating with signal %d (%s)",
162                   (int)sig_nr, signal_name(sig_nr));
163         call_hooks(sig_nr);
164
165         /* Re-raise the signal with the default handling so that the program
166          * termination status reflects that we were killed by this signal */
167         signal(sig_nr, SIG_DFL);
168         raise(sig_nr);
169     }
170 }
171
172 void
173 fatal_signal_wait(void)
174 {
175     fatal_signal_init();
176     poll_fd_wait(signal_fds[0], POLLIN);
177 }
178
179 static void
180 atexit_handler(void)
181 {
182     call_hooks(0);
183 }
184
185 static void
186 call_hooks(int sig_nr)
187 {
188     static volatile sig_atomic_t recurse = 0;
189     if (!recurse) {
190         size_t i;
191
192         recurse = 1;
193
194         for (i = 0; i < n_hooks; i++) {
195             struct hook *h = &hooks[i];
196             if (sig_nr || h->run_at_exit) {
197                 h->hook_cb(h->aux);
198             }
199         }
200     }
201 }
202 \f
203 /* Files to delete on exit. */
204 static struct sset files = SSET_INITIALIZER(&files);
205
206 /* Has a hook function been registered with fatal_signal_add_hook() (and not
207  * cleared by fatal_signal_fork())? */
208 static bool added_hook;
209
210 static void unlink_files(void *aux);
211 static void cancel_files(void *aux);
212 static void do_unlink_files(void);
213
214 /* Registers 'file' to be unlinked when the program terminates via exit() or a
215  * fatal signal. */
216 void
217 fatal_signal_add_file_to_unlink(const char *file)
218 {
219     if (!added_hook) {
220         added_hook = true;
221         fatal_signal_add_hook(unlink_files, cancel_files, NULL, true);
222     }
223
224     sset_add(&files, file);
225 }
226
227 /* Unregisters 'file' from being unlinked when the program terminates via
228  * exit() or a fatal signal. */
229 void
230 fatal_signal_remove_file_to_unlink(const char *file)
231 {
232     sset_find_and_delete(&files, file);
233 }
234
235 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
236  * Returns 0 if successful, otherwise a positive errno value. */
237 int
238 fatal_signal_unlink_file_now(const char *file)
239 {
240     int error = unlink(file) ? errno : 0;
241     if (error) {
242         VLOG_WARN("could not unlink \"%s\" (%s)", file, strerror(error));
243     }
244
245     fatal_signal_remove_file_to_unlink(file);
246
247     return error;
248 }
249
250 static void
251 unlink_files(void *aux OVS_UNUSED)
252 {
253     do_unlink_files();
254 }
255
256 static void
257 cancel_files(void *aux OVS_UNUSED)
258 {
259     sset_clear(&files);
260     added_hook = false;
261 }
262
263 static void
264 do_unlink_files(void)
265 {
266     const char *file;
267
268     SSET_FOR_EACH (file, &files) {
269         unlink(file);
270     }
271 }
272 \f
273 /* Clears all of the fatal signal hooks without executing them.  If any of the
274  * hooks passed a 'cancel_cb' function to fatal_signal_add_hook(), then those
275  * functions will be called, allowing them to free resources, etc.
276  *
277  * Following a fork, one of the resulting processes can call this function to
278  * allow it to terminate without calling the hooks registered before calling
279  * this function.  New hooks registered after calling this function will take
280  * effect normally. */
281 void
282 fatal_signal_fork(void)
283 {
284     size_t i;
285
286     for (i = 0; i < n_hooks; i++) {
287         struct hook *h = &hooks[i];
288         if (h->cancel_cb) {
289             h->cancel_cb(h->aux);
290         }
291     }
292     n_hooks = 0;
293
294     /* Raise any signals that we have already received with the default
295      * handler. */
296     if (stored_sig_nr != SIG_ATOMIC_MAX) {
297         raise(stored_sig_nr);
298     }
299 }