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