Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Asynchronous signal delivery functions. |
| 3 | * |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <signal.h> |
Willy Tarreau | be8c736 | 2009-07-23 13:40:20 +0200 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 16 | #include <proto/signal.h> |
| 17 | #include <proto/log.h> |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 18 | #include <proto/task.h> |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 19 | |
| 20 | /* Principle : we keep an in-order list of the first occurrence of all received |
| 21 | * signals. All occurrences of a same signal are grouped though. The signal |
| 22 | * queue does not need to be deeper than the number of signals we can handle. |
| 23 | * The handlers will be called asynchronously with the signal number. They can |
| 24 | * check themselves the number of calls by checking the descriptor this signal. |
| 25 | */ |
| 26 | |
| 27 | int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */ |
| 28 | int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */ |
| 29 | struct signal_descriptor signal_state[MAX_SIGNAL]; |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 30 | struct pool_head *pool2_sig_handlers = NULL; |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 31 | sigset_t blocked_sig; |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 32 | int signal_pending = 0; /* non-zero if t least one signal remains unprocessed */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 33 | |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 34 | #ifdef USE_THREAD |
| 35 | HA_SPINLOCK_T signals_lock; |
| 36 | #endif |
| 37 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 38 | /* Common signal handler, used by all signals. Received signals are queued. |
| 39 | * Signal number zero has a specific status, as it cannot be delivered by the |
| 40 | * system, any function may call it to perform asynchronous signal delivery. |
| 41 | */ |
| 42 | void signal_handler(int sig) |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 43 | { |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 44 | if (sig < 0 || sig >= MAX_SIGNAL) { |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 45 | /* unhandled signal */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 46 | signal(sig, SIG_IGN); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 47 | qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig); |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | if (!signal_state[sig].count) { |
| 52 | /* signal was not queued yet */ |
| 53 | if (signal_queue_len < MAX_SIGNAL) |
| 54 | signal_queue[signal_queue_len++] = sig; |
| 55 | else |
| 56 | qfprintf(stderr, "Signal %d : signal queue is unexpectedly full.\n", sig); |
| 57 | } |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 59 | signal_state[sig].count++; |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 60 | if (sig) |
| 61 | signal(sig, signal_handler); /* re-arm signal */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 64 | /* Call handlers of all pending signals and clear counts and queue length. The |
| 65 | * handlers may unregister themselves by calling signal_register() while they |
| 66 | * are called, just like it is done with normal signal handlers. |
| 67 | * Note that it is more efficient to call the inline version which checks the |
| 68 | * queue length before getting here. |
| 69 | */ |
| 70 | void __signal_process_queue() |
| 71 | { |
| 72 | int sig, cur_pos = 0; |
| 73 | struct signal_descriptor *desc; |
| 74 | sigset_t old_sig; |
| 75 | |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 76 | if (SPIN_TRYLOCK(SIGNALS_LOCK, &signals_lock)) |
| 77 | return; |
| 78 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 79 | /* block signal delivery during processing */ |
| 80 | sigprocmask(SIG_SETMASK, &blocked_sig, &old_sig); |
| 81 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 82 | /* It is important that we scan the queue forwards so that we can |
| 83 | * catch any signal that would have been queued by another signal |
| 84 | * handler. That allows real signal handlers to redistribute signals |
| 85 | * to tasks subscribed to signal zero. |
| 86 | */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 87 | for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) { |
| 88 | sig = signal_queue[cur_pos]; |
| 89 | desc = &signal_state[sig]; |
| 90 | if (desc->count) { |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 91 | struct sig_handler *sh, *shb; |
| 92 | list_for_each_entry_safe(sh, shb, &desc->handlers, list) { |
| 93 | if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler) |
| 94 | ((void (*)(struct sig_handler *))sh->handler)(sh); |
| 95 | else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler) |
| 96 | task_wakeup(sh->handler, sh->arg | TASK_WOKEN_SIGNAL); |
| 97 | } |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 98 | desc->count = 0; |
| 99 | } |
| 100 | } |
| 101 | signal_queue_len = 0; |
| 102 | |
| 103 | /* restore signal delivery */ |
| 104 | sigprocmask(SIG_SETMASK, &old_sig, NULL); |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 105 | SPIN_UNLOCK(SIGNALS_LOCK, &signals_lock); |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 106 | } |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 107 | |
| 108 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 109 | int signal_init() |
| 110 | { |
| 111 | int sig; |
| 112 | |
| 113 | signal_queue_len = 0; |
| 114 | memset(signal_queue, 0, sizeof(signal_queue)); |
| 115 | memset(signal_state, 0, sizeof(signal_state)); |
Willy Tarreau | d50b4ac | 2016-04-20 10:33:15 +0200 | [diff] [blame] | 116 | |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 117 | SPIN_INIT(&signals_lock); |
| 118 | |
Willy Tarreau | d50b4ac | 2016-04-20 10:33:15 +0200 | [diff] [blame] | 119 | /* Ensure signals are not blocked. Some shells or service managers may |
| 120 | * accidently block all of our signals unfortunately, causing lots of |
| 121 | * zombie processes to remain in the background during reloads. |
| 122 | */ |
| 123 | sigemptyset(&blocked_sig); |
William Lallemand | 73b85e7 | 2017-06-01 17:38:51 +0200 | [diff] [blame] | 124 | /* Ensure that SIGUSR2 is blocked until the end of configuration |
| 125 | * parsing We don't want the process to be killed by an unregistered |
| 126 | * USR2 signal when the master-worker is reloading */ |
| 127 | sigaddset(&blocked_sig, SIGUSR2); |
Willy Tarreau | d50b4ac | 2016-04-20 10:33:15 +0200 | [diff] [blame] | 128 | sigprocmask(SIG_SETMASK, &blocked_sig, NULL); |
| 129 | |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 130 | sigfillset(&blocked_sig); |
Willy Tarreau | 6747e27 | 2013-01-04 16:20:20 +0100 | [diff] [blame] | 131 | sigdelset(&blocked_sig, SIGPROF); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 132 | for (sig = 0; sig < MAX_SIGNAL; sig++) |
| 133 | LIST_INIT(&signal_state[sig].handlers); |
| 134 | |
| 135 | pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler), MEM_F_SHARED); |
| 136 | return pool2_sig_handlers != NULL; |
| 137 | } |
| 138 | |
| 139 | /* releases all registered signal handlers */ |
| 140 | void deinit_signals() |
| 141 | { |
| 142 | int sig; |
| 143 | struct sig_handler *sh, *shb; |
| 144 | |
| 145 | for (sig = 0; sig < MAX_SIGNAL; sig++) { |
Willy Tarreau | 6747e27 | 2013-01-04 16:20:20 +0100 | [diff] [blame] | 146 | if (sig != SIGPROF) |
| 147 | signal(sig, SIG_DFL); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 148 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 149 | LIST_DEL(&sh->list); |
| 150 | pool_free2(pool2_sig_handlers, sh); |
| 151 | } |
| 152 | } |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 153 | SPIN_DESTROY(&signals_lock); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* Register a function and an integer argument on a signal. A pointer to the |
| 157 | * newly allocated sig_handler is returned, or NULL in case of any error. The |
| 158 | * caller is responsible for unregistering the function when not used anymore. |
| 159 | * Note that passing a NULL as the function pointer enables interception of the |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 160 | * signal without processing, which is identical to SIG_IGN. If the signal is |
| 161 | * zero (which the system cannot deliver), only internal functions will be able |
| 162 | * to notify the registered functions. |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 163 | */ |
| 164 | struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg) |
| 165 | { |
| 166 | struct sig_handler *sh; |
| 167 | |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 168 | if (sig < 0 || sig >= MAX_SIGNAL) |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 169 | return NULL; |
| 170 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 171 | if (sig) |
Willy Tarreau | c39b0d1 | 2012-10-04 19:19:36 +0200 | [diff] [blame] | 172 | signal(sig, fct ? signal_handler : SIG_IGN); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 173 | |
| 174 | if (!fct) |
| 175 | return NULL; |
| 176 | |
| 177 | sh = pool_alloc2(pool2_sig_handlers); |
| 178 | if (!sh) |
| 179 | return NULL; |
| 180 | |
| 181 | sh->handler = fct; |
| 182 | sh->arg = arg; |
| 183 | sh->flags = SIG_F_TYPE_FCT; |
| 184 | LIST_ADDQ(&signal_state[sig].handlers, &sh->list); |
| 185 | return sh; |
| 186 | } |
| 187 | |
| 188 | /* Register a task and a wake-up reason on a signal. A pointer to the newly |
| 189 | * allocated sig_handler is returned, or NULL in case of any error. The caller |
| 190 | * is responsible for unregistering the task when not used anymore. Note that |
| 191 | * passing a NULL as the task pointer enables interception of the signal |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 192 | * without processing, which is identical to SIG_IGN. If the signal is zero |
| 193 | * (which the system cannot deliver), only internal functions will be able to |
| 194 | * notify the registered functions. |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 195 | */ |
| 196 | struct sig_handler *signal_register_task(int sig, struct task *task, int reason) |
| 197 | { |
| 198 | struct sig_handler *sh; |
| 199 | |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 200 | if (sig < 0 || sig >= MAX_SIGNAL) |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 201 | return NULL; |
| 202 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 203 | if (sig) |
| 204 | signal(sig, signal_handler); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 205 | |
| 206 | if (!task) |
| 207 | return NULL; |
| 208 | |
| 209 | sh = pool_alloc2(pool2_sig_handlers); |
| 210 | if (!sh) |
| 211 | return NULL; |
| 212 | |
| 213 | sh->handler = task; |
| 214 | sh->arg = reason & ~TASK_WOKEN_ANY; |
| 215 | sh->flags = SIG_F_TYPE_TASK; |
| 216 | LIST_ADDQ(&signal_state[sig].handlers, &sh->list); |
| 217 | return sh; |
| 218 | } |
| 219 | |
| 220 | /* Immediately unregister a handler so that no further signals may be delivered |
| 221 | * to it. The struct is released so the caller may not reference it anymore. |
| 222 | */ |
| 223 | void signal_unregister_handler(struct sig_handler *handler) |
| 224 | { |
| 225 | LIST_DEL(&handler->list); |
| 226 | pool_free2(pool2_sig_handlers, handler); |
| 227 | } |
| 228 | |
| 229 | /* Immediately unregister a handler so that no further signals may be delivered |
| 230 | * to it. The handler struct does not need to be known, only the function or |
| 231 | * task pointer. This method is expensive because it scans all the list, so it |
| 232 | * should only be used for rare cases (eg: exit). The struct is released so the |
| 233 | * caller may not reference it anymore. |
| 234 | */ |
| 235 | void signal_unregister_target(int sig, void *target) |
| 236 | { |
| 237 | struct sig_handler *sh, *shb; |
| 238 | |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 239 | if (sig < 0 || sig >= MAX_SIGNAL) |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 240 | return; |
| 241 | |
| 242 | if (!target) |
| 243 | return; |
| 244 | |
| 245 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 246 | if (sh->handler == target) { |
| 247 | LIST_DEL(&sh->list); |
| 248 | pool_free2(pool2_sig_handlers, sh); |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |