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