1 #include "fatal-signal.h"
12 /* Signals to catch. */
13 static const int fatal_signals[] = { SIGTERM, SIGINT, SIGHUP };
15 /* Signals to catch as a sigset_t. */
16 static sigset_t fatal_signal_set;
18 /* Hooks to call upon catching a signal */
20 void (*func)(void *aux);
24 static struct hook hooks[MAX_HOOKS];
25 static size_t n_hooks;
27 /* Number of nesting signal blockers. */
28 static int block_level = 0;
30 /* Signal mask saved by outermost signal blocker. */
31 static sigset_t saved_signal_mask;
33 static void call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set);
34 static void signal_handler(int sig_nr);
36 /* Registers 'hook' to be called when a process termination signal is
39 fatal_signal_add_hook(void (*func)(void *aux), void *aux)
42 assert(n_hooks < MAX_HOOKS);
43 hooks[n_hooks].func = func;
44 hooks[n_hooks].aux = aux;
46 fatal_signal_unblock();
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.
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. */
59 static bool inited = false;
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);
74 if (++block_level == 1) {
75 call_sigprocmask(SIG_BLOCK, &fatal_signal_set, &saved_signal_mask);
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
84 fatal_signal_unblock()
86 assert(block_level > 0);
87 if (--block_level == 0) {
88 call_sigprocmask(SIG_SETMASK, &saved_signal_mask, NULL);
93 static size_t n_files, max_files;
95 static void unlink_files(void *aux);
96 static void do_unlink_files(void);
98 /* Registers 'file' to be unlinked when the program terminates via exit() or a
101 fatal_signal_add_file_to_unlink(const char *file)
103 static bool added_hook = false;
106 fatal_signal_add_hook(unlink_files, NULL);
107 atexit(do_unlink_files);
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);
115 files[n_files++] = xstrdup(file);
116 fatal_signal_unblock();
119 /* Unregisters 'file' from being unlinked when the program terminates via
120 * exit() or a fatal signal. */
122 fatal_signal_remove_file_to_unlink(const char *file)
126 fatal_signal_block();
127 for (i = 0; i < n_files; i++) {
128 if (!strcmp(files[i], file)) {
130 files[i] = files[--n_files];
134 fatal_signal_unblock();
138 unlink_files(void *aux UNUSED)
144 do_unlink_files(void)
148 for (i = 0; i < n_files; i++) {
154 call_sigprocmask(int how, sigset_t* new_set, sigset_t* old_set)
156 int error = sigprocmask(how, new_set, old_set);
158 fprintf(stderr, "sigprocmask: %s\n", strerror(errno));
163 signal_handler(int sig_nr)
165 volatile sig_atomic_t recurse = 0;
171 /* Call all the hooks. */
172 for (i = 0; i < n_hooks; i++) {
173 hooks[i].func(hooks[i].aux);
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);