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