blob: 3d7a9a89897e05fbdeabe0f737449f380194b173 [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 */
Matthias Wirtheea152e2022-09-09 10:21:00 +020059
60 /* If the thread is TH_FL_SLEEPING we need to wake it */
61 wake_thread(tid);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020062}
63
Willy Tarreau8f38bd02009-05-10 08:53:33 +020064/* 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 */
70void __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 Lallemand6e1796e2018-06-07 11:23:40 +020077 ha_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020078
Willy Tarreaud0807c32010-08-27 18:26:11 +020079 /* 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 Tarreau8f38bd02009-05-10 08:53:33 +020084 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 Tarreau24f4efa2010-08-27 17:56:48 +020088 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 Tarreauad8bd242018-07-26 16:11:33 +020093 task_wakeup(sh->handler, TASK_WOKEN_SIGNAL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020094 }
Willy Tarreau8f38bd02009-05-10 08:53:33 +020095 desc->count = 0;
96 }
97 }
98 signal_queue_len = 0;
99
100 /* restore signal delivery */
William Lallemand6e1796e2018-06-07 11:23:40 +0200101 ha_sigmask(SIG_SETMASK, &old_sig, NULL);
Willy Tarreau8f38bd02009-05-10 08:53:33 +0200102}
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200103
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100104/* perform minimal intializations */
105static void signal_init()
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200106{
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 Tarreaud50b4ac2016-04-20 10:33:15 +0200112
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200113 sigfillset(&blocked_sig);
Willy Tarreau6747e272013-01-04 16:20:20 +0100114 sigdelset(&blocked_sig, SIGPROF);
William Lallemand933642c2018-06-07 09:49:04 +0200115 /* 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 Houchardb0198cc2020-03-18 13:10:05 +0100118 sigqueue(3), or raise(3).
119 Do not ignore WDTSIG or DEBUGSIG either, or it may deadlock the
120 watchdog */
William Lallemand933642c2018-06-07 09:49:04 +0200121 sigdelset(&blocked_sig, SIGBUS);
122 sigdelset(&blocked_sig, SIGFPE);
123 sigdelset(&blocked_sig, SIGILL);
124 sigdelset(&blocked_sig, SIGSEGV);
Olivier Houchardb0198cc2020-03-18 13:10:05 +0100125#ifdef DEBUGSIG
126 sigdelset(&blocked_sig, DEBUGSIG);
127#endif
128#ifdef WDTSIG
129 sigdelset(&blocked_sig, WDTSIG);
130#endif
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200131 for (sig = 0; sig < MAX_SIGNAL; sig++)
132 LIST_INIT(&signal_state[sig].handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200133}
134
William Lallemandd3801c12018-09-11 10:06:23 +0200135/*
136 * This function should be called to unblock all signals
137 */
138void haproxy_unblock_signals()
139{
140 sigset_t set;
141
142 /* Ensure signals are not blocked. Some shells or service managers may
Joseph Herlantf6989ca2018-11-25 11:19:40 -0800143 * accidentally block all of our signals unfortunately, causing lots of
William Lallemandd3801c12018-09-11 10:06:23 +0200144 * zombie processes to remain in the background during reloads.
145 */
146 sigemptyset(&set);
147 ha_sigmask(SIG_SETMASK, &set, NULL);
148}
149
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200150/* releases all registered signal handlers */
151void deinit_signals()
152{
153 int sig;
154 struct sig_handler *sh, *shb;
155
156 for (sig = 0; sig < MAX_SIGNAL; sig++) {
Willy Tarreau6747e272013-01-04 16:20:20 +0100157 if (sig != SIGPROF)
158 signal(sig, SIG_DFL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200159 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200160 LIST_DELETE(&sh->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100161 pool_free(pool_head_sig_handlers, sh);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200162 }
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 Tarreaud0807c32010-08-27 18:26:11 +0200170 * 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 Tarreau24f4efa2010-08-27 17:56:48 +0200173 */
174struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
175{
176 struct sig_handler *sh;
177
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100178 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200179 return NULL;
180
Willy Tarreaud0807c32010-08-27 18:26:11 +0200181 if (sig)
Willy Tarreauc39b0d12012-10-04 19:19:36 +0200182 signal(sig, fct ? signal_handler : SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200183
184 if (!fct)
185 return NULL;
186
Willy Tarreaubafbe012017-11-24 17:34:44 +0100187 sh = pool_alloc(pool_head_sig_handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200188 if (!sh)
189 return NULL;
190
191 sh->handler = fct;
192 sh->arg = arg;
193 sh->flags = SIG_F_TYPE_FCT;
Willy Tarreau2b718102021-04-21 07:32:39 +0200194 LIST_APPEND(&signal_state[sig].handlers, &sh->list);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200195 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 Tarreaud0807c32010-08-27 18:26:11 +0200202 * 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 Tarreau24f4efa2010-08-27 17:56:48 +0200205 */
206struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
207{
208 struct sig_handler *sh;
209
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100210 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200211 return NULL;
212
Willy Tarreaud0807c32010-08-27 18:26:11 +0200213 if (sig)
214 signal(sig, signal_handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200215
216 if (!task)
217 return NULL;
218
Willy Tarreaubafbe012017-11-24 17:34:44 +0100219 sh = pool_alloc(pool_head_sig_handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200220 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 Tarreau2b718102021-04-21 07:32:39 +0200226 LIST_APPEND(&signal_state[sig].handlers, &sh->list);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200227 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 */
233void signal_unregister_handler(struct sig_handler *handler)
234{
Willy Tarreau2b718102021-04-21 07:32:39 +0200235 LIST_DELETE(&handler->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100236 pool_free(pool_head_sig_handlers, handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200237}
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 */
245void signal_unregister_target(int sig, void *target)
246{
247 struct sig_handler *sh, *shb;
248
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100249 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200250 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 Tarreau2b718102021-04-21 07:32:39 +0200257 LIST_DELETE(&sh->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100258 pool_free(pool_head_sig_handlers, sh);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200259 break;
260 }
261 }
262}
William Lallemand31a1c1d2018-11-20 17:36:52 +0100263
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
269void 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 Tarreau2b718102021-04-21 07:32:39 +0200277 LIST_DELETE(&sh->list);
William Lallemand31a1c1d2018-11-20 17:36:52 +0100278 pool_free(pool_head_sig_handlers, sh);
279 }
280
281 signal(sig, SIG_IGN);
282}
Willy Tarreaub6b3df32018-11-26 16:31:20 +0100283
284INITCALL0(STG_PREPARE, signal_init);