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