Fix arguments to printf in usage message.
[sliver-openvswitch.git] / lib / fatal-signal.c
1 #include "fatal-signal.h"
2 #include <assert.h>
3 #include <errno.h>
4 #include <signal.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "util.h"
11
12 /* Signals to catch. */
13 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP };
14
15 /* Signals to catch as a sigset_t. */
16 static sigset_t fatal_signal_set;
17
18 /* Hooks to call upon catching a signal */
19 struct hook {
20     void (*func)(void *aux);
21     void *aux;
22 };
23 #define MAX_HOOKS 32
24 static struct hook hooks[MAX_HOOKS];
25 static size_t n_hooks;
26
27 /* Number of nesting signal blockers. */
28 static int block_level = 0;
29
30 /* Signal mask saved by outermost signal blocker. */
31 static sigset_t saved_signal_mask;
32
33 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
34 static void signal_handler(int sig_nr);
35
36 /* Registers 'hook' to be called when a process termination signal is
37  * raised. */
38 void
39 fatal_signal_add_hook(void (*func)(void *aux), void *aux)
40 {
41     fatal_signal_block();
42     assert(n_hooks < MAX_HOOKS);
43     hooks[n_hooks].func = func;
44     hooks[n_hooks].aux = aux;
45     n_hooks++;
46     fatal_signal_unblock();
47 }
48
49 /* Blocks program termination signals until fatal_signal_unblock() is called.
50  * May be called multiple times with nesting; if so, fatal_signal_unblock()
51  * must be called the same number of times to unblock signals.
52  *
53  * This is needed while adjusting a data structure that will be accessed by a
54  * fatal signal hook, so that the hook is not invoked while the data structure
55  * is in an inconsistent state. */
56 void
57 fatal_signal_block()
58 {
59     static bool inited = false;
60     if (!inited) {
61         size_t i;
62
63         inited = true;
64         sigemptyset(&fatal_signal_set);
65         for (i = 0; i < ARRAY_SIZE(fatal_signals); i++) {
66             int sig_nr = fatal_signals[i];
67             sigaddset(&fatal_signal_set, sig_nr);
68             if (signal(sig_nr, signal_handler) == SIG_IGN) {
69                 signal(sig_nr, SIG_IGN);
70             }
71         }
72     }
73
74     if (++block_level == 1) {
75         call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
76     }
77 }
78
79 /* Unblocks program termination signals blocked by fatal_signal_block() is
80  * called.  If multiple calls to fatal_signal_block() are nested,
81  * fatal_signal_unblock() must be called the same number of times to unblock
82  * signals. */
83 void
84 fatal_signal_unblock()
85 {
86     assert(block_level > 0);
87     if (--block_level == 0) {
88         call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
89     }
90 }
91 \f
92 static char **files;
93 static size_t n_files, max_files;
94
95 static void unlink_files(void *aux);
96 static void do_unlink_files(void);
97
98 /* Registers 'file' to be unlinked when the program terminates via exit() or a
99  * fatal signal. */
100 void
101 fatal_signal_add_file_to_unlink(const char *file)
102 {
103     static bool added_hook = false;
104     if (!added_hook) {
105         added_hook = true;
106         fatal_signal_add_hook(unlink_files, NULL);
107         atexit(do_unlink_files);
108     }
109
110     fatal_signal_block();
111     if (n_files >= max_files) {
112         max_files = max_files * 2 + 1;
113         files = xrealloc(files, sizeof *files * max_files);
114     }
115     files[n_files++] = xstrdup(file);
116     fatal_signal_unblock();
117 }
118
119 /* Unregisters 'file' from being unlinked when the program terminates via
120  * exit() or a fatal signal. */
121 void
122 fatal_signal_remove_file_to_unlink(const char *file)
123 {
124     size_t i;
125
126     fatal_signal_block();
127     for (i = 0; i < n_files; i++) {
128         if (!strcmp(files[i], file)) {
129             free(files[i]);
130             files[i] = files[--n_files];
131             break;
132         }
133     }
134     fatal_signal_unblock();
135 }
136
137 static void
138 unlink_files(void *aux UNUSED)
139 {
140     do_unlink_files();
141 }
142
143 static void
144 do_unlink_files(void)
145 {
146     size_t i;
147
148     for (i = 0; i < n_files; i++) {
149         unlink(files[i]);
150     }
151 }
152 \f
153 static void
154 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
155 {
156     int error = sigprocmask(how, new_set, old_set);
157     if (error) {
158         fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
159     }
160 }
161
162 static void
163 signal_handler(int sig_nr)
164 {
165     volatile sig_atomic_t recurse = 0;
166     if (!recurse) {
167         size_t i;
168
169         recurse = 1;
170
171         /* Call all the hooks. */
172         for (i = 0; i < n_hooks; i++) {
173             hooks[i].func(hooks[i].aux);
174         }
175     }
176
177     /* Re-raise the signal with the default handling so that the program
178      * termination status reflects that we were killed by this signal */
179     signal(sig_nr, SIG_DFL);
180     raise(sig_nr);
181 }