For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / lib / fatal-signal.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #include <config.h>
34 #include "fatal-signal.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "util.h"
44
45 /* Signals to catch. */
46 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP, SIGALRM };
47
48 /* Signals to catch as a sigset_t. */
49 static sigset_t fatal_signal_set;
50
51 /* Hooks to call upon catching a signal */
52 struct hook {
53     void (*func)(void *aux);
54     void *aux;
55     bool run_at_exit;
56 };
57 #define MAX_HOOKS 32
58 static struct hook hooks[MAX_HOOKS];
59 static size_t n_hooks;
60
61 /* Number of nesting signal blockers. */
62 static int block_level = 0;
63
64 /* Signal mask saved by outermost signal blocker. */
65 static sigset_t saved_signal_mask;
66
67 /* Disabled by fatal_signal_fork()? */
68 static bool disabled;
69
70 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
71 static void atexit_handler(void);
72 static void call_hooks(int sig_nr);
73
74 /* Registers 'hook' to be called when a process termination signal is raised.
75  * If 'run_at_exit' is true, 'hook' is also called during normal process
76  * termination, e.g. when exit() is called or when main() returns. */
77 void
78 fatal_signal_add_hook(void (*func)(void *aux), void *aux, bool run_at_exit)
79 {
80     fatal_signal_block();
81     assert(n_hooks < MAX_HOOKS);
82     hooks[n_hooks].func = func;
83     hooks[n_hooks].aux = aux;
84     hooks[n_hooks].run_at_exit = run_at_exit;
85     n_hooks++;
86     fatal_signal_unblock();
87 }
88
89 /* Blocks program termination signals until fatal_signal_unblock() is called.
90  * May be called multiple times with nesting; if so, fatal_signal_unblock()
91  * must be called the same number of times to unblock signals.
92  *
93  * This is needed while adjusting a data structure that will be accessed by a
94  * fatal signal hook, so that the hook is not invoked while the data structure
95  * is in an inconsistent state. */
96 void
97 fatal_signal_block()
98 {
99     static bool inited = false;
100     if (!inited) {
101         size_t i;
102
103         inited = true;
104         sigemptyset(&fatal_signal_set);
105         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
106             int sig_nr = fatal_signals[i];
107             struct sigaction old_sa;
108
109             sigaddset(&fatal_signal_set, sig_nr);
110             if (sigaction(sig_nr, NULL, &old_sa)) {
111                 ofp_fatal(errno, "sigaction");
112             }
113             if (old_sa.sa_handler == SIG_DFL
114                 && signal(sig_nr, fatal_signal_handler) == SIG_ERR) {
115                 ofp_fatal(errno, "signal");
116             }
117         }
118         atexit(atexit_handler);
119     }
120
121     if (++block_level == 1) {
122         call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
123     }
124 }
125
126 /* Unblocks program termination signals blocked by fatal_signal_block() is
127  * called.  If multiple calls to fatal_signal_block() are nested,
128  * fatal_signal_unblock() must be called the same number of times to unblock
129  * signals. */
130 void
131 fatal_signal_unblock()
132 {
133     assert(block_level > 0);
134     if (--block_level == 0) {
135         call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
136     }
137 }
138
139 /* Handles fatal signal number 'sig_nr'.
140  *
141  * Ordinarily this is the actual signal handler.  When other code needs to
142  * handle one of our signals, however, it can register for that signal and, if
143  * and when necessary, call this function to do fatal signal processing for it
144  * and terminate the process.  Currently only timeval.c does this, for SIGALRM.
145  * (It is not important whether the other code sets up its signal handler
146  * before or after this file, because this file will only set up a signal
147  * handler in the case where the signal has its default handling.)  */
148 void
149 fatal_signal_handler(int sig_nr)
150 {
151     call_hooks(sig_nr);
152
153     /* Re-raise the signal with the default handling so that the program
154      * termination status reflects that we were killed by this signal */
155     signal(sig_nr, SIG_DFL);
156     raise(sig_nr);
157 }
158
159 static void
160 atexit_handler(void)
161 {
162     if (!disabled) {
163         call_hooks(0);
164     }
165 }
166
167 static void
168 call_hooks(int sig_nr)
169 {
170     static volatile sig_atomic_t recurse = 0;
171     if (!recurse) {
172         size_t i;
173
174         recurse = 1;
175
176         for (i = 0; i < n_hooks; i++) {
177             struct hook *h = &hooks[i];
178             if (sig_nr || h->run_at_exit) {
179                 h->func(h->aux);
180             }
181         }
182     }
183 }
184 \f
185 static char **files;
186 static size_t n_files, max_files;
187
188 static void unlink_files(void *aux);
189 static void do_unlink_files(void);
190
191 /* Registers 'file' to be unlinked when the program terminates via exit() or a
192  * fatal signal. */
193 void
194 fatal_signal_add_file_to_unlink(const char *file)
195 {
196     static bool added_hook = false;
197     if (!added_hook) {
198         added_hook = true;
199         fatal_signal_add_hook(unlink_files, NULL, true);
200     }
201
202     fatal_signal_block();
203     if (n_files >= max_files) {
204         max_files = max_files * 2 + 1;
205         files = xrealloc(files, sizeof *files * max_files);
206     }
207     files[n_files++] = xstrdup(file);
208     fatal_signal_unblock();
209 }
210
211 /* Unregisters 'file' from being unlinked when the program terminates via
212  * exit() or a fatal signal. */
213 void
214 fatal_signal_remove_file_to_unlink(const char *file)
215 {
216     size_t i;
217
218     fatal_signal_block();
219     for (i = 0; i < n_files; i++) {
220         if (!strcmp(files[i], file)) {
221             free(files[i]);
222             files[i] = files[--n_files];
223             break;
224         }
225     }
226     fatal_signal_unblock();
227 }
228
229 static void
230 unlink_files(void *aux UNUSED)
231 {
232     do_unlink_files(); 
233 }
234
235 static void
236 do_unlink_files(void)
237 {
238     size_t i;
239
240     for (i = 0; i < n_files; i++) {
241         unlink(files[i]);
242     }
243 }
244 \f
245 /* Disables the fatal signal hook mechanism.  Following a fork, one of the
246  * resulting processes can call this function to allow it to terminate without
247  * triggering fatal signal processing or removing files.  Fatal signal
248  * processing is still enabled in the other process. */
249 void
250 fatal_signal_fork(void)
251 {
252     size_t i;
253
254     disabled = true;
255
256     for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
257         int sig_nr = fatal_signals[i];
258         if (signal(sig_nr, SIG_DFL) == SIG_IGN) {
259             signal(sig_nr, SIG_IGN);
260         }
261     }
262 }
263 \f
264 static void
265 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
266 {
267     int error = sigprocmask(how, new_set, old_set);
268     if (error) {
269         fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
270     }
271 }