]> git.proxmox.com Git - ovs.git/blame - lib/fatal-signal.c
Rename NOT_REACHED to OVS_NOT_REACHED
[ovs.git] / lib / fatal-signal.c
CommitLineData
064af421 1/*
10a89ef0 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16#include <config.h>
17#include "fatal-signal.h"
064af421
BP
18#include <errno.h>
19#include <signal.h>
20#include <stdbool.h>
21#include <stdio.h>
d8b30702 22#include <stdint.h>
064af421
BP
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
b847adc6 26#include "ovs-thread.h"
d8b30702 27#include "poll-loop.h"
411baaac 28#include "shash.h"
b3c01ed3 29#include "sset.h"
279c9e03 30#include "signals.h"
d8b30702 31#include "socket-util.h"
064af421 32#include "util.h"
6a0061cb
BP
33#include "vlog.h"
34
0c2c9057
SH
35#include "type-props.h"
36
37#ifndef SIG_ATOMIC_MAX
38#define SIG_ATOMIC_MAX TYPE_MAXIMUM(sig_atomic_t)
39#endif
40
d98e6007 41VLOG_DEFINE_THIS_MODULE(fatal_signal);
5136ce49 42
064af421
BP
43/* Signals to catch. */
44static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
45
064af421
BP
46/* Hooks to call upon catching a signal */
47struct hook {
e3830e90
BP
48 void (*hook_cb)(void *aux);
49 void (*cancel_cb)(void *aux);
064af421
BP
50 void *aux;
51 bool run_at_exit;
52};
53#define MAX_HOOKS 32
54static struct hook hooks[MAX_HOOKS];
55static size_t n_hooks;
56
d8b30702
JG
57static int signal_fds[2];
58static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX;
064af421 59
97be1538 60static struct ovs_mutex mutex;
b847adc6 61
064af421
BP
62static void atexit_handler(void);
63static void call_hooks(int sig_nr);
64
b847adc6
BP
65/* Initializes the fatal signal handling module. Calling this function is
66 * optional, because calling any other function in the module will also
67 * initialize it. However, in a multithreaded program, the module must be
68 * initialized while the process is still single-threaded. */
69void
d8b30702 70fatal_signal_init(void)
064af421
BP
71{
72 static bool inited = false;
d8b30702 73
064af421
BP
74 if (!inited) {
75 size_t i;
76
b847adc6 77 assert_single_threaded();
064af421 78 inited = true;
d8b30702 79
834d6caf 80 ovs_mutex_init_recursive(&mutex);
c0d95206 81 xpipe_nonblocking(signal_fds);
d8b30702 82
064af421
BP
83 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
84 int sig_nr = fatal_signals[i];
85 struct sigaction old_sa;
86
279c9e03 87 xsigaction(sig_nr, NULL, &old_sa);
064af421
BP
88 if (old_sa.sa_handler == SIG_DFL
89 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
10a89ef0 90 VLOG_FATAL("signal failed (%s)", ovs_strerror(errno));
064af421
BP
91 }
92 }
93 atexit(atexit_handler);
94 }
064af421
BP
95}
96
b847adc6
BP
97/* Registers 'hook_cb' to be called from inside poll_block() following a fatal
98 * signal. 'hook_cb' does not need to be async-signal-safe. In a
99 * multithreaded program 'hook_cb' might be called from any thread, with
100 * threads other than the one running 'hook_cb' in unknown states.
d8b30702 101 *
b847adc6
BP
102 * If 'run_at_exit' is true, 'hook_cb' is also called during normal process
103 * termination, e.g. when exit() is called or when main() returns.
e3830e90
BP
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. */
064af421 111void
e3830e90
BP
112fatal_signal_add_hook(void (*hook_cb)(void *aux), void (*cancel_cb)(void *aux),
113 void *aux, bool run_at_exit)
064af421 114{
d8b30702 115 fatal_signal_init();
e3830e90 116
97be1538 117 ovs_mutex_lock(&mutex);
cb22974d 118 ovs_assert(n_hooks < MAX_HOOKS);
e3830e90
BP
119 hooks[n_hooks].hook_cb = hook_cb;
120 hooks[n_hooks].cancel_cb = cancel_cb;
d8b30702
JG
121 hooks[n_hooks].aux = aux;
122 hooks[n_hooks].run_at_exit = run_at_exit;
123 n_hooks++;
97be1538 124 ovs_mutex_unlock(&mutex);
064af421
BP
125}
126
127/* Handles fatal signal number 'sig_nr'.
128 *
129 * Ordinarily this is the actual signal handler. When other code needs to
130 * handle one of our signals, however, it can register for that signal and, if
131 * and when necessary, call this function to do fatal signal processing for it
132 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
133 * (It is not important whether the other code sets up its signal handler
134 * before or after this file, because this file will only set up a signal
135 * handler in the case where the signal has its default handling.) */
136void
137fatal_signal_handler(int sig_nr)
138{
d8b30702
JG
139 ignore(write(signal_fds[1], "", 1));
140 stored_sig_nr = sig_nr;
141}
142
b302749b
BP
143/* Check whether a fatal signal has occurred and, if so, call the fatal signal
144 * hooks and exit.
145 *
146 * This function is called automatically by poll_block(), but specialized
147 * programs that may not always call poll_block() on a regular basis should
148 * also call it periodically. (Therefore, any function with "block" in its
149 * name should call fatal_signal_run() each time it is called, either directly
150 * or through poll_block(), because such functions can only used by specialized
151 * programs that can afford to block outside their main loop around
152 * poll_block().)
153 */
d8b30702
JG
154void
155fatal_signal_run(void)
156{
bf82917b 157 sig_atomic_t sig_nr;
064af421 158
c874f17f
BP
159 fatal_signal_init();
160
161 sig_nr = stored_sig_nr;
d8b30702 162 if (sig_nr != SIG_ATOMIC_MAX) {
eee8089c
BP
163 char namebuf[SIGNAL_NAME_BUFSIZE];
164
97be1538 165 ovs_mutex_lock(&mutex);
b847adc6 166
b67b2b0a 167 VLOG_WARN("terminating with signal %d (%s)",
eee8089c 168 (int)sig_nr, signal_name(sig_nr, namebuf, sizeof namebuf));
d8b30702
JG
169 call_hooks(sig_nr);
170
171 /* Re-raise the signal with the default handling so that the program
172 * termination status reflects that we were killed by this signal */
173 signal(sig_nr, SIG_DFL);
174 raise(sig_nr);
b847adc6 175
97be1538 176 ovs_mutex_unlock(&mutex);
428b2edd 177 OVS_NOT_REACHED();
d8b30702
JG
178 }
179}
180
181void
182fatal_signal_wait(void)
183{
c874f17f 184 fatal_signal_init();
d8b30702 185 poll_fd_wait(signal_fds[0], POLLIN);
064af421
BP
186}
187
188static void
189atexit_handler(void)
190{
e3830e90 191 call_hooks(0);
064af421
BP
192}
193
194static void
195call_hooks(int sig_nr)
196{
197 static volatile sig_atomic_t recurse = 0;
198 if (!recurse) {
199 size_t i;
200
201 recurse = 1;
202
203 for (i = 0; i < n_hooks; i++) {
204 struct hook *h = &hooks[i];
205 if (sig_nr || h->run_at_exit) {
e3830e90 206 h->hook_cb(h->aux);
064af421
BP
207 }
208 }
209 }
210}
211\f
b3c01ed3
BP
212/* Files to delete on exit. */
213static struct sset files = SSET_INITIALIZER(&files);
064af421 214
e3830e90
BP
215/* Has a hook function been registered with fatal_signal_add_hook() (and not
216 * cleared by fatal_signal_fork())? */
217static bool added_hook;
218
064af421 219static void unlink_files(void *aux);
e3830e90 220static void cancel_files(void *aux);
064af421
BP
221static void do_unlink_files(void);
222
223/* Registers 'file' to be unlinked when the program terminates via exit() or a
224 * fatal signal. */
225void
226fatal_signal_add_file_to_unlink(const char *file)
227{
b847adc6
BP
228 fatal_signal_init();
229
97be1538 230 ovs_mutex_lock(&mutex);
064af421
BP
231 if (!added_hook) {
232 added_hook = true;
e3830e90 233 fatal_signal_add_hook(unlink_files, cancel_files, NULL, true);
064af421
BP
234 }
235
b3c01ed3 236 sset_add(&files, file);
97be1538 237 ovs_mutex_unlock(&mutex);
064af421
BP
238}
239
240/* Unregisters 'file' from being unlinked when the program terminates via
241 * exit() or a fatal signal. */
242void
243fatal_signal_remove_file_to_unlink(const char *file)
244{
b847adc6
BP
245 fatal_signal_init();
246
97be1538 247 ovs_mutex_lock(&mutex);
b3c01ed3 248 sset_find_and_delete(&files, file);
97be1538 249 ovs_mutex_unlock(&mutex);
064af421
BP
250}
251
6a0061cb
BP
252/* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
253 * Returns 0 if successful, otherwise a positive errno value. */
254int
255fatal_signal_unlink_file_now(const char *file)
256{
b847adc6
BP
257 int error;
258
259 fatal_signal_init();
260
97be1538 261 ovs_mutex_lock(&mutex);
b847adc6
BP
262
263 error = unlink(file) ? errno : 0;
6a0061cb 264 if (error) {
10a89ef0 265 VLOG_WARN("could not unlink \"%s\" (%s)", file, ovs_strerror(error));
6a0061cb
BP
266 }
267
268 fatal_signal_remove_file_to_unlink(file);
269
97be1538 270 ovs_mutex_unlock(&mutex);
b847adc6 271
6a0061cb
BP
272 return error;
273}
274
064af421 275static void
67a4917b 276unlink_files(void *aux OVS_UNUSED)
064af421 277{
d295e8e9 278 do_unlink_files();
064af421
BP
279}
280
e3830e90 281static void
c69ee87c 282cancel_files(void *aux OVS_UNUSED)
e3830e90 283{
b3c01ed3 284 sset_clear(&files);
e3830e90
BP
285 added_hook = false;
286}
287
064af421
BP
288static void
289do_unlink_files(void)
290{
b3c01ed3 291 const char *file;
064af421 292
b3c01ed3
BP
293 SSET_FOR_EACH (file, &files) {
294 unlink(file);
064af421
BP
295 }
296}
297\f
e3830e90
BP
298/* Clears all of the fatal signal hooks without executing them. If any of the
299 * hooks passed a 'cancel_cb' function to fatal_signal_add_hook(), then those
300 * functions will be called, allowing them to free resources, etc.
301 *
302 * Following a fork, one of the resulting processes can call this function to
303 * allow it to terminate without calling the hooks registered before calling
304 * this function. New hooks registered after calling this function will take
305 * effect normally. */
064af421
BP
306void
307fatal_signal_fork(void)
308{
309 size_t i;
310
b847adc6
BP
311 assert_single_threaded();
312
e3830e90
BP
313 for (i = 0; i < n_hooks; i++) {
314 struct hook *h = &hooks[i];
315 if (h->cancel_cb) {
316 h->cancel_cb(h->aux);
064af421
BP
317 }
318 }
e3830e90 319 n_hooks = 0;
d8b30702
JG
320
321 /* Raise any signals that we have already received with the default
322 * handler. */
323 if (stored_sig_nr != SIG_ATOMIC_MAX) {
324 raise(stored_sig_nr);
064af421
BP
325 }
326}