fatal-signal: New function fatal_signal_unlink_file_now().
[sliver-openvswitch.git] / lib / fatal-signal.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
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 <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include "shash.h"
27 #include "util.h"
28
29 #define THIS_MODULE VLM_fatal_signal
30 #include "vlog.h"
31
32 /* Signals to catch. */
33 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
34
35 /* Signals to catch as a sigset_t. */
36 static sigset_t fatal_signal_set;
37
38 /* Hooks to call upon catching a signal */
39 struct hook {
40     void (*func)(void *aux);
41     void *aux;
42     bool run_at_exit;
43 };
44 #define MAX_HOOKS 32
45 static struct hook hooks[MAX_HOOKS];
46 static size_t n_hooks;
47
48 /* Number of nesting signal blockers. */
49 static int block_level = 0;
50
51 /* Signal mask saved by outermost signal blocker. */
52 static sigset_t saved_signal_mask;
53
54 /* Disabled by fatal_signal_fork()? */
55 static bool disabled;
56
57 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
58 static void atexit_handler(void);
59 static void call_hooks(int sig_nr);
60
61 /* Registers 'hook' to be called when a process termination signal is raised.
62  * If 'run_at_exit' is true, 'hook' is also called during normal process
63  * termination, e.g. when exit() is called or when main() returns. */
64 void
65 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
66 {
67     fatal_signal_block();
68     assert(n_hooks < MAX_HOOKS);
69     hooks[n_hooks].func = func;
70     hooks[n_hooks].aux = aux;
71     hooks[n_hooks].run_at_exit = run_at_exit;
72     n_hooks++;
73     fatal_signal_unblock();
74 }
75
76 /* Blocks program termination signals until fatal_signal_unblock() is called.
77  * May be called multiple times with nesting; if so, fatal_signal_unblock()
78  * must be called the same number of times to unblock signals.
79  *
80  * This is needed while adjusting a data structure that will be accessed by a
81  * fatal signal hook, so that the hook is not invoked while the data structure
82  * is in an inconsistent state. */
83 void
84 fatal_signal_block(void)
85 {
86     static bool inited = false;
87     if (!inited) {
88         size_t i;
89
90         inited = true;
91         sigemptyset(&fatal_signal_set);
92         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
93             int sig_nr = fatal_signals[i];
94             struct sigaction old_sa;
95
96             sigaddset(&fatal_signal_set, sig_nr);
97             if (sigaction(sig_nr, NULL, &old_sa)) {
98                 ovs_fatal(errno, "sigaction");
99             }
100             if (old_sa.sa_handler == SIG_DFL
101                 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
102                 ovs_fatal(errno, "signal");
103             }
104         }
105         atexit(atexit_handler);
106     }
107
108     if (++block_level == 1) {
109         call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
110     }
111 }
112
113 /* Unblocks program termination signals blocked by fatal_signal_block() is
114  * called.  If multiple calls to fatal_signal_block() are nested,
115  * fatal_signal_unblock() must be called the same number of times to unblock
116  * signals. */
117 void
118 fatal_signal_unblock(void)
119 {
120     assert(block_level > 0);
121     if (--block_level == 0) {
122         call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
123     }
124 }
125
126 /* Handles fatal signal number 'sig_nr'.
127  *
128  * Ordinarily this is the actual signal handler.  When other code needs to
129  * handle one of our signals, however, it can register for that signal and, if
130  * and when necessary, call this function to do fatal signal processing for it
131  * and terminate the process.  Currently only timeval.c does this, for SIGALRM.
132  * (It is not important whether the other code sets up its signal handler
133  * before or after this file, because this file will only set up a signal
134  * handler in the case where the signal has its default handling.)  */
135 void
136 fatal_signal_handler(int sig_nr)
137 {
138     call_hooks(sig_nr);
139
140     /* Re-raise the signal with the default handling so that the program
141      * termination status reflects that we were killed by this signal */
142     signal(sig_nr, SIG_DFL);
143     raise(sig_nr);
144 }
145
146 static void
147 atexit_handler(void)
148 {
149     if (!disabled) {
150         call_hooks(0);
151     }
152 }
153
154 static void
155 call_hooks(int sig_nr)
156 {
157     static volatile sig_atomic_t recurse = 0;
158     if (!recurse) {
159         size_t i;
160
161         recurse = 1;
162
163         for (i = 0; i < n_hooks; i++) {
164             struct hook *h = &hooks[i];
165             if (sig_nr || h->run_at_exit) {
166                 h->func(h->aux);
167             }
168         }
169     }
170 }
171 \f
172 static struct shash files = SHASH_INITIALIZER(&files);
173
174 static void unlink_files(void *aux);
175 static void do_unlink_files(void);
176
177 /* Registers 'file' to be unlinked when the program terminates via exit() or a
178  * fatal signal. */
179 void
180 fatal_signal_add_file_to_unlink(const char *file)
181 {
182     static bool added_hook = false;
183     if (!added_hook) {
184         added_hook = true;
185         fatal_signal_add_hook(unlink_files, NULL, true);
186     }
187
188     fatal_signal_block();
189     if (!shash_find(&files, file)) {
190         shash_add(&files, file, NULL);
191     }
192     fatal_signal_unblock();
193 }
194
195 /* Unregisters 'file' from being unlinked when the program terminates via
196  * exit() or a fatal signal. */
197 void
198 fatal_signal_remove_file_to_unlink(const char *file)
199 {
200     struct shash_node *node;
201
202     fatal_signal_block();
203     node = shash_find(&files, file);
204     if (node) {
205         shash_delete(&files, node);
206     }
207     fatal_signal_unblock();
208 }
209
210 /* Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
211  * Returns 0 if successful, otherwise a positive errno value. */
212 int
213 fatal_signal_unlink_file_now(const char *file)
214 {
215     int error = unlink(file) ? errno : 0;
216     if (error) {
217         VLOG_WARN("could not unlink \"%s\" (%s)", file, strerror(error));
218     }
219
220     fatal_signal_remove_file_to_unlink(file);
221
222     return error;
223 }
224
225 static void
226 unlink_files(void *aux UNUSED)
227 {
228     do_unlink_files(); 
229 }
230
231 static void
232 do_unlink_files(void)
233 {
234     struct shash_node *node;
235
236     SHASH_FOR_EACH (node, &files) {
237         unlink(node->name);
238     }
239 }
240 \f
241 /* Disables the fatal signal hook mechanism.  Following a fork, one of the
242  * resulting processes can call this function to allow it to terminate without
243  * triggering fatal signal processing or removing files.  Fatal signal
244  * processing is still enabled in the other process. */
245 void
246 fatal_signal_fork(void)
247 {
248     size_t i;
249
250     disabled = true;
251
252     for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
253         int sig_nr = fatal_signals[i];
254         if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
255             signal(sig_nr, SIG_IGN);
256         }
257     }
258 }
259 \f
260 static void
261 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
262 {
263     int error = sigprocmask(how, new_set, old_set);
264     if (error) {
265         fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
266     }
267 }