blob: b6ed1a60e0c61296f4cf0b3aaf4844f66007a871 [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
William Lallemand6e1796e2018-06-07 11:23:40 +020016#include <common/hathreads.h>
17
Willy Tarreau8f38bd02009-05-10 08:53:33 +020018#include <proto/signal.h>
19#include <proto/log.h>
Willy Tarreau24f4efa2010-08-27 17:56:48 +020020#include <proto/task.h>
Willy Tarreau8f38bd02009-05-10 08:53:33 +020021
22/* Principle : we keep an in-order list of the first occurrence of all received
23 * signals. All occurrences of a same signal are grouped though. The signal
24 * queue does not need to be deeper than the number of signals we can handle.
25 * The handlers will be called asynchronously with the signal number. They can
26 * check themselves the number of calls by checking the descriptor this signal.
27 */
28
29int signal_queue_len; /* length of signal queue, <= MAX_SIGNAL (1 entry per signal max) */
30int signal_queue[MAX_SIGNAL]; /* in-order queue of received signals */
31struct signal_descriptor signal_state[MAX_SIGNAL];
32sigset_t blocked_sig;
Willy Tarreaud0807c32010-08-27 18:26:11 +020033int signal_pending = 0; /* non-zero if t least one signal remains unprocessed */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020034
Willy Tarreau2455ceb2018-11-26 15:57:34 +010035DECLARE_STATIC_POOL(pool_head_sig_handlers, "sig_handlers", sizeof(struct sig_handler));
Christopher Fauletb79a94c2017-05-30 15:34:30 +020036
Willy Tarreaud0807c32010-08-27 18:26:11 +020037/* Common signal handler, used by all signals. Received signals are queued.
38 * Signal number zero has a specific status, as it cannot be delivered by the
39 * system, any function may call it to perform asynchronous signal delivery.
40 */
41void signal_handler(int sig)
Willy Tarreau8f38bd02009-05-10 08:53:33 +020042{
Willy Tarreau1a53b5e2013-01-24 02:06:05 +010043 if (sig < 0 || sig >= MAX_SIGNAL) {
Willy Tarreau8f38bd02009-05-10 08:53:33 +020044 /* unhandled signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020045 signal(sig, SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020046 qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020047 return;
48 }
49
50 if (!signal_state[sig].count) {
51 /* signal was not queued yet */
52 if (signal_queue_len < MAX_SIGNAL)
53 signal_queue[signal_queue_len++] = sig;
54 else
55 qfprintf(stderr, "Signal %d : signal queue is unexpectedly full.\n", sig);
56 }
Willy Tarreaud0807c32010-08-27 18:26:11 +020057
Willy Tarreau8f38bd02009-05-10 08:53:33 +020058 signal_state[sig].count++;
Willy Tarreaud0807c32010-08-27 18:26:11 +020059 if (sig)
60 signal(sig, signal_handler); /* re-arm signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020061}
62
Willy Tarreau8f38bd02009-05-10 08:53:33 +020063/* Call handlers of all pending signals and clear counts and queue length. The
64 * handlers may unregister themselves by calling signal_register() while they
65 * are called, just like it is done with normal signal handlers.
66 * Note that it is more efficient to call the inline version which checks the
67 * queue length before getting here.
68 */
69void __signal_process_queue()
70{
71 int sig, cur_pos = 0;
72 struct signal_descriptor *desc;
73 sigset_t old_sig;
74
75 /* block signal delivery during processing */
William Lallemand6e1796e2018-06-07 11:23:40 +020076 ha_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020077
Willy Tarreaud0807c32010-08-27 18:26:11 +020078 /* It is important that we scan the queue forwards so that we can
79 * catch any signal that would have been queued by another signal
80 * handler. That allows real signal handlers to redistribute signals
81 * to tasks subscribed to signal zero.
82 */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020083 for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) {
84 sig = signal_queue[cur_pos];
85 desc = &signal_state[sig];
86 if (desc->count) {
Willy Tarreau24f4efa2010-08-27 17:56:48 +020087 struct sig_handler *sh, *shb;
88 list_for_each_entry_safe(sh, shb, &desc->handlers, list) {
89 if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler)
90 ((void (*)(struct sig_handler *))sh->handler)(sh);
91 else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler)
Willy Tarreauad8bd242018-07-26 16:11:33 +020092 task_wakeup(sh->handler, TASK_WOKEN_SIGNAL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020093 }
Willy Tarreau8f38bd02009-05-10 08:53:33 +020094 desc->count = 0;
95 }
96 }
97 signal_queue_len = 0;
98
99 /* restore signal delivery */
William Lallemand6e1796e2018-06-07 11:23:40 +0200100 ha_sigmask(SIG_SETMASK, &old_sig, NULL);
Willy Tarreau8f38bd02009-05-10 08:53:33 +0200101}
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200102
103/* perform minimal intializations, report 0 in case of error, 1 if OK. */
104int signal_init()
105{
106 int sig;
107
108 signal_queue_len = 0;
109 memset(signal_queue, 0, sizeof(signal_queue));
110 memset(signal_state, 0, sizeof(signal_state));
Willy Tarreaud50b4ac2016-04-20 10:33:15 +0200111
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200112 sigfillset(&blocked_sig);
Willy Tarreau6747e272013-01-04 16:20:20 +0100113 sigdelset(&blocked_sig, SIGPROF);
William Lallemand933642c2018-06-07 09:49:04 +0200114 /* man sigprocmask: If SIGBUS, SIGFPE, SIGILL, or SIGSEGV are
115 generated while they are blocked, the result is undefined, unless
116 the signal was generated by kill(2),
117 sigqueue(3), or raise(3) */
118 sigdelset(&blocked_sig, SIGBUS);
119 sigdelset(&blocked_sig, SIGFPE);
120 sigdelset(&blocked_sig, SIGILL);
121 sigdelset(&blocked_sig, SIGSEGV);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200122 for (sig = 0; sig < MAX_SIGNAL; sig++)
123 LIST_INIT(&signal_state[sig].handlers);
124
Willy Tarreau8ceae722018-11-26 11:58:30 +0100125 return 1;
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200126}
127
William Lallemandd3801c12018-09-11 10:06:23 +0200128/*
129 * This function should be called to unblock all signals
130 */
131void haproxy_unblock_signals()
132{
133 sigset_t set;
134
135 /* Ensure signals are not blocked. Some shells or service managers may
136 * accidently block all of our signals unfortunately, causing lots of
137 * zombie processes to remain in the background during reloads.
138 */
139 sigemptyset(&set);
140 ha_sigmask(SIG_SETMASK, &set, NULL);
141}
142
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200143/* releases all registered signal handlers */
144void deinit_signals()
145{
146 int sig;
147 struct sig_handler *sh, *shb;
148
149 for (sig = 0; sig < MAX_SIGNAL; sig++) {
Willy Tarreau6747e272013-01-04 16:20:20 +0100150 if (sig != SIGPROF)
151 signal(sig, SIG_DFL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200152 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
153 LIST_DEL(&sh->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100154 pool_free(pool_head_sig_handlers, sh);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200155 }
156 }
157}
158
159/* Register a function and an integer argument on a signal. A pointer to the
160 * newly allocated sig_handler is returned, or NULL in case of any error. The
161 * caller is responsible for unregistering the function when not used anymore.
162 * Note that passing a NULL as the function pointer enables interception of the
Willy Tarreaud0807c32010-08-27 18:26:11 +0200163 * signal without processing, which is identical to SIG_IGN. If the signal is
164 * zero (which the system cannot deliver), only internal functions will be able
165 * to notify the registered functions.
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200166 */
167struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
168{
169 struct sig_handler *sh;
170
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100171 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200172 return NULL;
173
Willy Tarreaud0807c32010-08-27 18:26:11 +0200174 if (sig)
Willy Tarreauc39b0d12012-10-04 19:19:36 +0200175 signal(sig, fct ? signal_handler : SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200176
177 if (!fct)
178 return NULL;
179
Willy Tarreaubafbe012017-11-24 17:34:44 +0100180 sh = pool_alloc(pool_head_sig_handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200181 if (!sh)
182 return NULL;
183
184 sh->handler = fct;
185 sh->arg = arg;
186 sh->flags = SIG_F_TYPE_FCT;
187 LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
188 return sh;
189}
190
191/* Register a task and a wake-up reason on a signal. A pointer to the newly
192 * allocated sig_handler is returned, or NULL in case of any error. The caller
193 * is responsible for unregistering the task when not used anymore. Note that
194 * passing a NULL as the task pointer enables interception of the signal
Willy Tarreaud0807c32010-08-27 18:26:11 +0200195 * without processing, which is identical to SIG_IGN. If the signal is zero
196 * (which the system cannot deliver), only internal functions will be able to
197 * notify the registered functions.
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200198 */
199struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
200{
201 struct sig_handler *sh;
202
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100203 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200204 return NULL;
205
Willy Tarreaud0807c32010-08-27 18:26:11 +0200206 if (sig)
207 signal(sig, signal_handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200208
209 if (!task)
210 return NULL;
211
Willy Tarreaubafbe012017-11-24 17:34:44 +0100212 sh = pool_alloc(pool_head_sig_handlers);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200213 if (!sh)
214 return NULL;
215
216 sh->handler = task;
217 sh->arg = reason & ~TASK_WOKEN_ANY;
218 sh->flags = SIG_F_TYPE_TASK;
219 LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
220 return sh;
221}
222
223/* Immediately unregister a handler so that no further signals may be delivered
224 * to it. The struct is released so the caller may not reference it anymore.
225 */
226void signal_unregister_handler(struct sig_handler *handler)
227{
228 LIST_DEL(&handler->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100229 pool_free(pool_head_sig_handlers, handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200230}
231
232/* Immediately unregister a handler so that no further signals may be delivered
233 * to it. The handler struct does not need to be known, only the function or
234 * task pointer. This method is expensive because it scans all the list, so it
235 * should only be used for rare cases (eg: exit). The struct is released so the
236 * caller may not reference it anymore.
237 */
238void signal_unregister_target(int sig, void *target)
239{
240 struct sig_handler *sh, *shb;
241
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100242 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200243 return;
244
245 if (!target)
246 return;
247
248 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
249 if (sh->handler == target) {
250 LIST_DEL(&sh->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100251 pool_free(pool_head_sig_handlers, sh);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200252 break;
253 }
254 }
255}
William Lallemand31a1c1d2018-11-20 17:36:52 +0100256
257/*
258 * Immedialtely unregister every handler assigned to a signal <sig>.
259 * Once the handler list is empty, the signal is ignored with SIG_IGN.
260 */
261
262void signal_unregister(int sig)
263{
264 struct sig_handler *sh, *shb;
265
266 if (sig < 0 || sig >= MAX_SIGNAL)
267 return;
268
269 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
270 LIST_DEL(&sh->list);
271 pool_free(pool_head_sig_handlers, sh);
272 }
273
274 signal(sig, SIG_IGN);
275}