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