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 | |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 16 | #include <common/hathreads.h> |
| 17 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 18 | #include <proto/signal.h> |
| 19 | #include <proto/log.h> |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 20 | #include <proto/task.h> |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 21 | |
| 22 | /* Principle : we keep an in-order list of the first occurrence of all received |
| 23 | * signals. All occurrences of a same signal are grouped though. The signal |
| 24 | * queue does not need to be deeper than the number of signals we can handle. |
| 25 | * The handlers will be called asynchronously with the signal number. They can |
| 26 | * check themselves the number of calls by checking the descriptor this signal. |
| 27 | */ |
| 28 | |
| 29 | int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */ |
| 30 | int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */ |
| 31 | struct signal_descriptor signal_state[MAX_SIGNAL]; |
| 32 | sigset_t blocked_sig; |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 33 | 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] | 34 | |
Willy Tarreau | 2455ceb | 2018-11-26 15:57:34 +0100 | [diff] [blame] | 35 | DECLARE_STATIC_POOL(pool_head_sig_handlers, "sig_handlers", sizeof(struct sig_handler)); |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 37 | /* Common signal handler, used by all signals. Received signals are queued. |
| 38 | * Signal number zero has a specific status, as it cannot be delivered by the |
| 39 | * system, any function may call it to perform asynchronous signal delivery. |
| 40 | */ |
| 41 | void signal_handler(int sig) |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 42 | { |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 43 | if (sig < 0 || sig >= MAX_SIGNAL) { |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 44 | /* unhandled signal */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 45 | signal(sig, SIG_IGN); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 46 | qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig); |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (!signal_state[sig].count) { |
| 51 | /* signal was not queued yet */ |
| 52 | if (signal_queue_len < MAX_SIGNAL) |
| 53 | signal_queue[signal_queue_len++] = sig; |
| 54 | else |
| 55 | qfprintf(stderr, "Signal %d : signal queue is unexpectedly full.\n", sig); |
| 56 | } |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 58 | signal_state[sig].count++; |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 59 | if (sig) |
| 60 | signal(sig, signal_handler); /* re-arm signal */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 63 | /* Call handlers of all pending signals and clear counts and queue length. The |
| 64 | * handlers may unregister themselves by calling signal_register() while they |
| 65 | * are called, just like it is done with normal signal handlers. |
| 66 | * Note that it is more efficient to call the inline version which checks the |
| 67 | * queue length before getting here. |
| 68 | */ |
| 69 | void __signal_process_queue() |
| 70 | { |
| 71 | int sig, cur_pos = 0; |
| 72 | struct signal_descriptor *desc; |
| 73 | sigset_t old_sig; |
| 74 | |
| 75 | /* block signal delivery during processing */ |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 76 | ha_sigmask(SIG_SETMASK, &blocked_sig, &old_sig); |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 77 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 78 | /* It is important that we scan the queue forwards so that we can |
| 79 | * catch any signal that would have been queued by another signal |
| 80 | * handler. That allows real signal handlers to redistribute signals |
| 81 | * to tasks subscribed to signal zero. |
| 82 | */ |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 83 | for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) { |
| 84 | sig = signal_queue[cur_pos]; |
| 85 | desc = &signal_state[sig]; |
| 86 | if (desc->count) { |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 87 | struct sig_handler *sh, *shb; |
| 88 | list_for_each_entry_safe(sh, shb, &desc->handlers, list) { |
| 89 | if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler) |
| 90 | ((void (*)(struct sig_handler *))sh->handler)(sh); |
| 91 | else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler) |
Willy Tarreau | ad8bd24 | 2018-07-26 16:11:33 +0200 | [diff] [blame] | 92 | task_wakeup(sh->handler, TASK_WOKEN_SIGNAL); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 93 | } |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 94 | desc->count = 0; |
| 95 | } |
| 96 | } |
| 97 | signal_queue_len = 0; |
| 98 | |
| 99 | /* restore signal delivery */ |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 100 | ha_sigmask(SIG_SETMASK, &old_sig, NULL); |
Willy Tarreau | 8f38bd0 | 2009-05-10 08:53:33 +0200 | [diff] [blame] | 101 | } |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 103 | /* perform minimal intializations */ |
| 104 | static void signal_init() |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 105 | { |
| 106 | int sig; |
| 107 | |
| 108 | signal_queue_len = 0; |
| 109 | memset(signal_queue, 0, sizeof(signal_queue)); |
| 110 | memset(signal_state, 0, sizeof(signal_state)); |
Willy Tarreau | d50b4ac | 2016-04-20 10:33:15 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 112 | sigfillset(&blocked_sig); |
Willy Tarreau | 6747e27 | 2013-01-04 16:20:20 +0100 | [diff] [blame] | 113 | sigdelset(&blocked_sig, SIGPROF); |
William Lallemand | 933642c | 2018-06-07 09:49:04 +0200 | [diff] [blame] | 114 | /* man sigprocmask: If SIGBUS, SIGFPE, SIGILL, or SIGSEGV are |
| 115 | generated while they are blocked, the result is undefined, unless |
| 116 | the signal was generated by kill(2), |
Olivier Houchard | b0198cc | 2020-03-18 13:10:05 +0100 | [diff] [blame] | 117 | sigqueue(3), or raise(3). |
| 118 | Do not ignore WDTSIG or DEBUGSIG either, or it may deadlock the |
| 119 | watchdog */ |
William Lallemand | 933642c | 2018-06-07 09:49:04 +0200 | [diff] [blame] | 120 | sigdelset(&blocked_sig, SIGBUS); |
| 121 | sigdelset(&blocked_sig, SIGFPE); |
| 122 | sigdelset(&blocked_sig, SIGILL); |
| 123 | sigdelset(&blocked_sig, SIGSEGV); |
Olivier Houchard | b0198cc | 2020-03-18 13:10:05 +0100 | [diff] [blame] | 124 | #ifdef DEBUGSIG |
| 125 | sigdelset(&blocked_sig, DEBUGSIG); |
| 126 | #endif |
| 127 | #ifdef WDTSIG |
| 128 | sigdelset(&blocked_sig, WDTSIG); |
| 129 | #endif |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 130 | for (sig = 0; sig < MAX_SIGNAL; sig++) |
| 131 | LIST_INIT(&signal_state[sig].handlers); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 132 | } |
| 133 | |
William Lallemand | d3801c1 | 2018-09-11 10:06:23 +0200 | [diff] [blame] | 134 | /* |
| 135 | * This function should be called to unblock all signals |
| 136 | */ |
| 137 | void haproxy_unblock_signals() |
| 138 | { |
| 139 | sigset_t set; |
| 140 | |
| 141 | /* Ensure signals are not blocked. Some shells or service managers may |
Joseph Herlant | f6989ca | 2018-11-25 11:19:40 -0800 | [diff] [blame] | 142 | * accidentally block all of our signals unfortunately, causing lots of |
William Lallemand | d3801c1 | 2018-09-11 10:06:23 +0200 | [diff] [blame] | 143 | * zombie processes to remain in the background during reloads. |
| 144 | */ |
| 145 | sigemptyset(&set); |
| 146 | ha_sigmask(SIG_SETMASK, &set, NULL); |
| 147 | } |
| 148 | |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 149 | /* releases all registered signal handlers */ |
| 150 | void deinit_signals() |
| 151 | { |
| 152 | int sig; |
| 153 | struct sig_handler *sh, *shb; |
| 154 | |
| 155 | for (sig = 0; sig < MAX_SIGNAL; sig++) { |
Willy Tarreau | 6747e27 | 2013-01-04 16:20:20 +0100 | [diff] [blame] | 156 | if (sig != SIGPROF) |
| 157 | signal(sig, SIG_DFL); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 158 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 159 | LIST_DEL(&sh->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 160 | pool_free(pool_head_sig_handlers, sh); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* Register a function and an integer argument on a signal. A pointer to the |
| 166 | * newly allocated sig_handler is returned, or NULL in case of any error. The |
| 167 | * caller is responsible for unregistering the function when not used anymore. |
| 168 | * 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] | 169 | * signal without processing, which is identical to SIG_IGN. If the signal is |
| 170 | * zero (which the system cannot deliver), only internal functions will be able |
| 171 | * to notify the registered functions. |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 172 | */ |
| 173 | struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg) |
| 174 | { |
| 175 | struct sig_handler *sh; |
| 176 | |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 177 | if (sig < 0 || sig >= MAX_SIGNAL) |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 178 | return NULL; |
| 179 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 180 | if (sig) |
Willy Tarreau | c39b0d1 | 2012-10-04 19:19:36 +0200 | [diff] [blame] | 181 | signal(sig, fct ? signal_handler : SIG_IGN); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 182 | |
| 183 | if (!fct) |
| 184 | return NULL; |
| 185 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 186 | sh = pool_alloc(pool_head_sig_handlers); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 187 | if (!sh) |
| 188 | return NULL; |
| 189 | |
| 190 | sh->handler = fct; |
| 191 | sh->arg = arg; |
| 192 | sh->flags = SIG_F_TYPE_FCT; |
| 193 | LIST_ADDQ(&signal_state[sig].handlers, &sh->list); |
| 194 | return sh; |
| 195 | } |
| 196 | |
| 197 | /* Register a task and a wake-up reason on a signal. A pointer to the newly |
| 198 | * allocated sig_handler is returned, or NULL in case of any error. The caller |
| 199 | * is responsible for unregistering the task when not used anymore. Note that |
| 200 | * passing a NULL as the task pointer enables interception of the signal |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 201 | * without processing, which is identical to SIG_IGN. If the signal is zero |
| 202 | * (which the system cannot deliver), only internal functions will be able to |
| 203 | * notify the registered functions. |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 204 | */ |
| 205 | struct sig_handler *signal_register_task(int sig, struct task *task, int reason) |
| 206 | { |
| 207 | struct sig_handler *sh; |
| 208 | |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 209 | if (sig < 0 || sig >= MAX_SIGNAL) |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 210 | return NULL; |
| 211 | |
Willy Tarreau | d0807c3 | 2010-08-27 18:26:11 +0200 | [diff] [blame] | 212 | if (sig) |
| 213 | signal(sig, signal_handler); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 214 | |
| 215 | if (!task) |
| 216 | return NULL; |
| 217 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 218 | sh = pool_alloc(pool_head_sig_handlers); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 219 | if (!sh) |
| 220 | return NULL; |
| 221 | |
| 222 | sh->handler = task; |
| 223 | sh->arg = reason & ~TASK_WOKEN_ANY; |
| 224 | sh->flags = SIG_F_TYPE_TASK; |
| 225 | LIST_ADDQ(&signal_state[sig].handlers, &sh->list); |
| 226 | return sh; |
| 227 | } |
| 228 | |
| 229 | /* Immediately unregister a handler so that no further signals may be delivered |
| 230 | * to it. The struct is released so the caller may not reference it anymore. |
| 231 | */ |
| 232 | void signal_unregister_handler(struct sig_handler *handler) |
| 233 | { |
| 234 | LIST_DEL(&handler->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 235 | pool_free(pool_head_sig_handlers, handler); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* Immediately unregister a handler so that no further signals may be delivered |
| 239 | * to it. The handler struct does not need to be known, only the function or |
| 240 | * task pointer. This method is expensive because it scans all the list, so it |
| 241 | * should only be used for rare cases (eg: exit). The struct is released so the |
| 242 | * caller may not reference it anymore. |
| 243 | */ |
| 244 | void signal_unregister_target(int sig, void *target) |
| 245 | { |
| 246 | struct sig_handler *sh, *shb; |
| 247 | |
Willy Tarreau | 1a53b5e | 2013-01-24 02:06:05 +0100 | [diff] [blame] | 248 | if (sig < 0 || sig >= MAX_SIGNAL) |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 249 | return; |
| 250 | |
| 251 | if (!target) |
| 252 | return; |
| 253 | |
| 254 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 255 | if (sh->handler == target) { |
| 256 | LIST_DEL(&sh->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 257 | pool_free(pool_head_sig_handlers, sh); |
Willy Tarreau | 24f4efa | 2010-08-27 17:56:48 +0200 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
William Lallemand | 31a1c1d | 2018-11-20 17:36:52 +0100 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Immedialtely unregister every handler assigned to a signal <sig>. |
| 265 | * Once the handler list is empty, the signal is ignored with SIG_IGN. |
| 266 | */ |
| 267 | |
| 268 | void signal_unregister(int sig) |
| 269 | { |
| 270 | struct sig_handler *sh, *shb; |
| 271 | |
| 272 | if (sig < 0 || sig >= MAX_SIGNAL) |
| 273 | return; |
| 274 | |
| 275 | list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) { |
| 276 | LIST_DEL(&sh->list); |
| 277 | pool_free(pool_head_sig_handlers, sh); |
| 278 | } |
| 279 | |
| 280 | signal(sig, SIG_IGN); |
| 281 | } |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 282 | |
| 283 | INITCALL0(STG_PREPARE, signal_init); |