2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "fatal-signal.h"
26 #include "ovs-thread.h"
27 #include "poll-loop.h"
31 #include "socket-util.h"
35 #include "type-props.h"
37 #ifndef SIG_ATOMIC_MAX
38 #define SIG_ATOMIC_MAX TYPE_MAXIMUM(sig_atomic_t)
41 VLOG_DEFINE_THIS_MODULE(fatal_signal);
43 /* Signals to catch. */
44 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
46 /* Hooks to call upon catching a signal */
48 void (*hook_cb)(void *aux);
49 void (*cancel_cb)(void *aux);
54 static struct hook hooks[MAX_HOOKS];
55 static size_t n_hooks;
57 static int signal_fds[2];
58 static volatile sig_atomic_t stored_sig_nr = SIG_ATOMIC_MAX;
60 static pthread_mutex_t mutex;
62 static void atexit_handler(void);
63 static void call_hooks(int sig_nr);
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. */
70 fatal_signal_init(void)
72 static bool inited = false;
75 pthread_mutexattr_t attr;
78 assert_single_threaded();
81 xpthread_mutexattr_init(&attr);
82 xpthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
83 xpthread_mutex_init(&mutex, &attr);
84 xpthread_mutexattr_destroy(&attr);
86 xpipe_nonblocking(signal_fds);
88 for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
89 int sig_nr = fatal_signals[i];
90 struct sigaction old_sa;
92 xsigaction(sig_nr, NULL, &old_sa);
93 if (old_sa.sa_handler == SIG_DFL
94 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
95 VLOG_FATAL("signal failed (%s)", ovs_strerror(errno));
98 atexit(atexit_handler);
102 /* Registers 'hook_cb' to be called from inside poll_block() following a fatal
103 * signal. 'hook_cb' does not need to be async-signal-safe. In a
104 * multithreaded program 'hook_cb' might be called from any thread, with
105 * threads other than the one running 'hook_cb' in unknown states.
107 * If 'run_at_exit' is true, 'hook_cb' is also called during normal process
108 * termination, e.g. when exit() is called or when main() returns.
110 * If the current process forks, fatal_signal_fork() may be called to clear the
111 * parent process's fatal signal hooks, so that 'hook_cb' is only called when
112 * the child terminates, not when the parent does. When fatal_signal_fork() is
113 * called, it calls the 'cancel_cb' function if it is nonnull, passing 'aux',
114 * to notify that the hook has been canceled. This allows the hook to free
117 fatal_signal_add_hook(void (*hook_cb)(void *aux), void (*cancel_cb)(void *aux),
118 void *aux, bool run_at_exit)
122 xpthread_mutex_lock(&mutex);
123 ovs_assert(n_hooks < MAX_HOOKS);
124 hooks[n_hooks].hook_cb = hook_cb;
125 hooks[n_hooks].cancel_cb = cancel_cb;
126 hooks[n_hooks].aux = aux;
127 hooks[n_hooks].run_at_exit = run_at_exit;
129 xpthread_mutex_unlock(&mutex);
132 /* Handles fatal signal number 'sig_nr'.
134 * Ordinarily this is the actual signal handler. When other code needs to
135 * handle one of our signals, however, it can register for that signal and, if
136 * and when necessary, call this function to do fatal signal processing for it
137 * and terminate the process. Currently only timeval.c does this, for SIGALRM.
138 * (It is not important whether the other code sets up its signal handler
139 * before or after this file, because this file will only set up a signal
140 * handler in the case where the signal has its default handling.) */
142 fatal_signal_handler(int sig_nr)
144 ignore(write(signal_fds[1], "", 1));
145 stored_sig_nr = sig_nr;
148 /* Check whether a fatal signal has occurred and, if so, call the fatal signal
151 * This function is called automatically by poll_block(), but specialized
152 * programs that may not always call poll_block() on a regular basis should
153 * also call it periodically. (Therefore, any function with "block" in its
154 * name should call fatal_signal_run() each time it is called, either directly
155 * or through poll_block(), because such functions can only used by specialized
156 * programs that can afford to block outside their main loop around
160 fatal_signal_run(void)
166 sig_nr = stored_sig_nr;
167 if (sig_nr != SIG_ATOMIC_MAX) {
168 char namebuf[SIGNAL_NAME_BUFSIZE];
170 xpthread_mutex_lock(&mutex);
172 VLOG_WARN("terminating with signal %d (%s)",
173 (int)sig_nr, signal_name(sig_nr, namebuf, sizeof namebuf));
176 /* Re-raise the signal with the default handling so that the program
177 * termination status reflects that we were killed by this signal */
178 signal(sig_nr, SIG_DFL);
181 xpthread_mutex_unlock(&mutex);
187 fatal_signal_wait(void)
190 poll_fd_wait(signal_fds[0], POLLIN);
200 call_hooks(int sig_nr)
202 static volatile sig_atomic_t recurse = 0;
208 for (i = 0; i < n_hooks; i++) {
209 struct hook *h = &hooks[i];
210 if (sig_nr || h->run_at_exit) {
217 /* Files to delete on exit. */
218 static struct sset files = SSET_INITIALIZER(&files);
220 /* Has a hook function been registered with fatal_signal_add_hook() (and not
221 * cleared by fatal_signal_fork())? */
222 static bool added_hook;
224 static void unlink_files(void *aux);
225 static void cancel_files(void *aux);
226 static void do_unlink_files(void);
228 /* Registers 'file' to be unlinked when the program terminates via exit() or a
231 fatal_signal_add_file_to_unlink(const char *file)
235 xpthread_mutex_lock(&mutex);
238 fatal_signal_add_hook(unlink_files, cancel_files, NULL, true);
241 sset_add(&files, file);
242 xpthread_mutex_unlock(&mutex);
245 /* Unregisters 'file' from being unlinked when the program terminates via
246 * exit() or a fatal signal. */
248 fatal_signal_remove_file_to_unlink(const char *file)
252 xpthread_mutex_lock(&mutex);
253 sset_find_and_delete(&files, file);
254 xpthread_mutex_unlock(&mutex);
257 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
258 * Returns 0 if successful, otherwise a positive errno value. */
260 fatal_signal_unlink_file_now(const char *file)
266 xpthread_mutex_lock(&mutex);
268 error = unlink(file) ? errno : 0;
270 VLOG_WARN("could not unlink \"%s\" (%s)", file, ovs_strerror(error));
273 fatal_signal_remove_file_to_unlink(file);
275 xpthread_mutex_unlock(&mutex);
281 unlink_files(void *aux OVS_UNUSED)
287 cancel_files(void *aux OVS_UNUSED)
294 do_unlink_files(void)
298 SSET_FOR_EACH (file, &files) {
303 /* Clears all of the fatal signal hooks without executing them. If any of the
304 * hooks passed a 'cancel_cb' function to fatal_signal_add_hook(), then those
305 * functions will be called, allowing them to free resources, etc.
307 * Following a fork, one of the resulting processes can call this function to
308 * allow it to terminate without calling the hooks registered before calling
309 * this function. New hooks registered after calling this function will take
310 * effect normally. */
312 fatal_signal_fork(void)
316 assert_single_threaded();
318 for (i = 0; i < n_hooks; i++) {
319 struct hook *h = &hooks[i];
321 h->cancel_cb(h->aux);
326 /* Raise any signals that we have already received with the default
328 if (stored_sig_nr != SIG_ATOMIC_MAX) {
329 raise(stored_sig_nr);