blob: 5ec71429c2df8e41adf249276c3663c58fed2fc0 [file] [log] [blame]
Willy Tarreau8f38bd02009-05-10 08:53:33 +02001/*
2 * Asynchronous signal delivery functions.
3 *
Willy Tarreau24f4efa2010-08-27 17:56:48 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau8f38bd02009-05-10 08:53:33 +02005 *
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 Tarreaube8c7362009-07-23 13:40:20 +020014#include <string.h>
15
Willy Tarreau36979d92020-06-05 17:27:29 +020016#include <haproxy/errors.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020017#include <haproxy/signal.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020018#include <haproxy/task.h>
Willy Tarreau8f38bd02009-05-10 08:53:33 +020019
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
27int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */
28int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */
29struct signal_descriptor signal_state[MAX_SIGNAL];
30sigset_t blocked_sig;
Willy Tarreaud0807c32010-08-27 18:26:11 +020031int signal_pending = 0; /* non-zero if t least one signal remains unprocessed */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020032
Willy Tarreau2455ceb2018-11-26 15:57:34 +010033DECLARE_STATIC_POOL(pool_head_sig_handlers, "sig_handlers", sizeof(struct sig_handler));
Christopher Fauletb79a94c2017-05-30 15:34:30 +020034
Willy Tarreaud0807c32010-08-27 18:26:11 +020035/* 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 */
39void signal_handler(int sig)
Willy Tarreau8f38bd02009-05-10 08:53:33 +020040{
Willy Tarreau1a53b5e2013-01-24 02:06:05 +010041 if (sig < 0 || sig >= MAX_SIGNAL) {
Willy Tarreau8f38bd02009-05-10 08:53:33 +020042 /* unhandled signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020043 signal(sig, SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020044 qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020045 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 Tarreaud0807c32010-08-27 18:26:11 +020055
Willy Tarreau8f38bd02009-05-10 08:53:33 +020056 signal_state[sig].count++;
Willy Tarreaud0807c32010-08-27 18:26:11 +020057 if (sig)
58 signal(sig, signal_handler); /* re-arm signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020059}
60
Willy Tarreau8f38bd02009-05-10 08:53:33 +020061/* Call handlers of all pending signals and clear counts and queue length. The
62 * handlers may unregister themselves by calling signal_register() while they
63 * are called, just like it is done with normal signal handlers.
64 * Note that it is more efficient to call the inline version which checks the
65 * queue length before getting here.
66 */
67void __signal_process_queue()
68{
69 int sig, cur_pos = 0;
70 struct signal_descriptor *desc;
71 sigset_t old_sig;
72
73 /* block signal delivery during processing */
William Lallemand6e1796e2018-06-07 11:23:40 +020074 ha_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020075
Willy Tarreaud0807c32010-08-27 18:26:11 +020076 /* It is important that we scan the queue forwards so that we can
77 * catch any signal that would have been queued by another signal
78 * handler. That allows real signal handlers to redistribute signals
79 * to tasks subscribed to signal zero.
80 */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020081 for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) {
82 sig = signal_queue[cur_pos];
83 desc = &signal_state[sig];
84 if (desc->count) {
Willy Tarreau24f4efa2010-08-27 17:56:48 +020085 struct sig_handler *sh, *shb;
86 list_for_each_entry_safe(sh, shb, &desc->handlers, list) {
87 if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler)
88 ((void (*)(struct sig_handler *))sh->handler)(sh);
89 else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler)
Willy Tarreauad8bd242018-07-26 16:11:33 +020090 task_wakeup(sh->handler, TASK_WOKEN_SIGNAL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020091 }
Willy Tarreau8f38bd02009-05-10 08:53:33 +020092 desc->count = 0;
93 }
94 }
95 signal_queue_len = 0;
96
97 /* restore signal delivery */
William Lallemand6e1796e2018-06-07 11:23:40 +020098 ha_sigmask(SIG_SETMASK, &old_sig, NULL);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020099}
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200100
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100101/* perform minimal intializations */
102static void signal_init()
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200103{
104 int sig;
105
106 signal_queue_len = 0;
107 memset(signal_queue, 0, sizeof(signal_queue));
108 memset(signal_state, 0, sizeof(signal_state));
Willy Tarreaud50b4ac2016-04-20 10:33:15 +0200109
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200110 sigfillset(&blocked_sig);
Willy Tarreau6747e272013-01-04 16:20:20 +0100111 sigdelset(&blocked_sig, SIGPROF);
William Lallemand933642c2018-06-07 09:49:04 +0200112 /* man sigprocmask: If SIGBUS, SIGFPE, SIGILL, or SIGSEGV are
113 generated while they are blocked, the result is undefined, unless
114 the signal was generated by kill(2),
Olivier Houchardb0198cc2020-03-18 13:10:05 +0100115 sigqueue(3), or raise(3).
116 Do not ignore WDTSIG or DEBUGSIG either, or it may deadlock the
117 watchdog */
William Lallemand933642c2018-06-07 09:49:04 +0200118 sigdelset(&blocked_sig, SIGBUS);
119 sigdelset(&blocked_sig, SIGFPE);
120 sigdelset(&blocked_sig, SIGILL);
121 sigdelset(&blocked_sig, SIGSEGV);
Olivier Houchardb0198cc2020-03-18 13:10:05 +0100122#ifdef DEBUGSIG
123 sigdelset(&blocked_sig, DEBUGSIG);
124#endif
125#ifdef WDTSIG
126 sigdelset(&blocked_sig, WDTSIG);
127#endif
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200128 for (sig = 0; sig < MAX_SIGNAL; sig++)
129 LIST_INIT(&signal_state[sig].handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200130}
131
William Lallemandd3801c12018-09-11 10:06:23 +0200132/*
133 * This function should be called to unblock all signals
134 */
135void haproxy_unblock_signals()
136{
137 sigset_t set;
138
139 /* Ensure signals are not blocked. Some shells or service managers may
Joseph Herlantf6989ca2018-11-25 11:19:40 -0800140 * accidentally block all of our signals unfortunately, causing lots of
William Lallemandd3801c12018-09-11 10:06:23 +0200141 * zombie processes to remain in the background during reloads.
142 */
143 sigemptyset(&set);
144 ha_sigmask(SIG_SETMASK, &set, NULL);
145}
146
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200147/* releases all registered signal handlers */
148void deinit_signals()
149{
150 int sig;
151 struct sig_handler *sh, *shb;
152
153 for (sig = 0; sig < MAX_SIGNAL; sig++) {
Willy Tarreau6747e272013-01-04 16:20:20 +0100154 if (sig != SIGPROF)
155 signal(sig, SIG_DFL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200156 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200157 LIST_DELETE(&sh->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100158 pool_free(pool_head_sig_handlers, sh);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200159 }
160 }
161}
162
163/* Register a function and an integer argument on a signal. A pointer to the
164 * newly allocated sig_handler is returned, or NULL in case of any error. The
165 * caller is responsible for unregistering the function when not used anymore.
166 * Note that passing a NULL as the function pointer enables interception of the
Willy Tarreaud0807c32010-08-27 18:26:11 +0200167 * signal without processing, which is identical to SIG_IGN. If the signal is
168 * zero (which the system cannot deliver), only internal functions will be able
169 * to notify the registered functions.
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200170 */
171struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
172{
173 struct sig_handler *sh;
174
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100175 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200176 return NULL;
177
Willy Tarreaud0807c32010-08-27 18:26:11 +0200178 if (sig)
Willy Tarreauc39b0d12012-10-04 19:19:36 +0200179 signal(sig, fct ? signal_handler : SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200180
181 if (!fct)
182 return NULL;
183
Willy Tarreaubafbe012017-11-24 17:34:44 +0100184 sh = pool_alloc(pool_head_sig_handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200185 if (!sh)
186 return NULL;
187
188 sh->handler = fct;
189 sh->arg = arg;
190 sh->flags = SIG_F_TYPE_FCT;
Willy Tarreau2b718102021-04-21 07:32:39 +0200191 LIST_APPEND(&signal_state[sig].handlers, &sh->list);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200192 return sh;
193}
194
195/* Register a task and a wake-up reason on a signal. A pointer to the newly
196 * allocated sig_handler is returned, or NULL in case of any error. The caller
197 * is responsible for unregistering the task when not used anymore. Note that
198 * passing a NULL as the task pointer enables interception of the signal
Willy Tarreaud0807c32010-08-27 18:26:11 +0200199 * without processing, which is identical to SIG_IGN. If the signal is zero
200 * (which the system cannot deliver), only internal functions will be able to
201 * notify the registered functions.
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200202 */
203struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
204{
205 struct sig_handler *sh;
206
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100207 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200208 return NULL;
209
Willy Tarreaud0807c32010-08-27 18:26:11 +0200210 if (sig)
211 signal(sig, signal_handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200212
213 if (!task)
214 return NULL;
215
Willy Tarreaubafbe012017-11-24 17:34:44 +0100216 sh = pool_alloc(pool_head_sig_handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200217 if (!sh)
218 return NULL;
219
220 sh->handler = task;
221 sh->arg = reason & ~TASK_WOKEN_ANY;
222 sh->flags = SIG_F_TYPE_TASK;
Willy Tarreau2b718102021-04-21 07:32:39 +0200223 LIST_APPEND(&signal_state[sig].handlers, &sh->list);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200224 return sh;
225}
226
227/* Immediately unregister a handler so that no further signals may be delivered
228 * to it. The struct is released so the caller may not reference it anymore.
229 */
230void signal_unregister_handler(struct sig_handler *handler)
231{
Willy Tarreau2b718102021-04-21 07:32:39 +0200232 LIST_DELETE(&handler->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100233 pool_free(pool_head_sig_handlers, handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200234}
235
236/* Immediately unregister a handler so that no further signals may be delivered
237 * to it. The handler struct does not need to be known, only the function or
238 * task pointer. This method is expensive because it scans all the list, so it
239 * should only be used for rare cases (eg: exit). The struct is released so the
240 * caller may not reference it anymore.
241 */
242void signal_unregister_target(int sig, void *target)
243{
244 struct sig_handler *sh, *shb;
245
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100246 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200247 return;
248
249 if (!target)
250 return;
251
252 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
253 if (sh->handler == target) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200254 LIST_DELETE(&sh->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100255 pool_free(pool_head_sig_handlers, sh);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200256 break;
257 }
258 }
259}
William Lallemand31a1c1d2018-11-20 17:36:52 +0100260
261/*
262 * Immedialtely unregister every handler assigned to a signal <sig>.
263 * Once the handler list is empty, the signal is ignored with SIG_IGN.
264 */
265
266void signal_unregister(int sig)
267{
268 struct sig_handler *sh, *shb;
269
270 if (sig < 0 || sig >= MAX_SIGNAL)
271 return;
272
273 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200274 LIST_DELETE(&sh->list);
William Lallemand31a1c1d2018-11-20 17:36:52 +0100275 pool_free(pool_head_sig_handlers, sh);
276 }
277
278 signal(sig, SIG_IGN);
279}
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100280
281INITCALL0(STG_PREPARE, signal_init);