blob: e996c46d6d03566e4e3a32feea03faa7789bd6de [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 Tarreau8f38bd02009-05-10 08:53:33 +020016#include <proto/signal.h>
17#include <proto/log.h>
Willy Tarreau24f4efa2010-08-27 17:56:48 +020018#include <proto/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];
Willy Tarreau24f4efa2010-08-27 17:56:48 +020030struct pool_head *pool2_sig_handlers = NULL;
Willy Tarreau8f38bd02009-05-10 08:53:33 +020031sigset_t blocked_sig;
Willy Tarreaud0807c32010-08-27 18:26:11 +020032int signal_pending = 0; /* non-zero if t least one signal remains unprocessed */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020033
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010034__decl_hathreads(HA_SPINLOCK_T signals_lock);
Christopher Fauletb79a94c2017-05-30 15:34:30 +020035
Willy Tarreaud0807c32010-08-27 18:26:11 +020036/* 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 */
40void signal_handler(int sig)
Willy Tarreau8f38bd02009-05-10 08:53:33 +020041{
Willy Tarreau1a53b5e2013-01-24 02:06:05 +010042 if (sig < 0 || sig >= MAX_SIGNAL) {
Willy Tarreau8f38bd02009-05-10 08:53:33 +020043 /* unhandled signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020044 signal(sig, SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020045 qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020046 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 Tarreaud0807c32010-08-27 18:26:11 +020056
Willy Tarreau8f38bd02009-05-10 08:53:33 +020057 signal_state[sig].count++;
Willy Tarreaud0807c32010-08-27 18:26:11 +020058 if (sig)
59 signal(sig, signal_handler); /* re-arm signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020060}
61
Willy Tarreau8f38bd02009-05-10 08:53:33 +020062/* 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 */
68void __signal_process_queue()
69{
70 int sig, cur_pos = 0;
71 struct signal_descriptor *desc;
72 sigset_t old_sig;
73
Christopher Faulet2a944ee2017-11-07 10:42:54 +010074 if (HA_SPIN_TRYLOCK(SIGNALS_LOCK, &signals_lock))
Christopher Fauletb79a94c2017-05-30 15:34:30 +020075 return;
76
Willy Tarreau8f38bd02009-05-10 08:53:33 +020077 /* block signal delivery during processing */
78 sigprocmask(SIG_SETMASK, &blocked_sig, &old_sig);
79
Willy Tarreaud0807c32010-08-27 18:26:11 +020080 /* 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 Tarreau8f38bd02009-05-10 08:53:33 +020085 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 Tarreau24f4efa2010-08-27 17:56:48 +020089 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 Tarreau8f38bd02009-05-10 08:53:33 +020096 desc->count = 0;
97 }
98 }
99 signal_queue_len = 0;
100
101 /* restore signal delivery */
102 sigprocmask(SIG_SETMASK, &old_sig, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100103 HA_SPIN_UNLOCK(SIGNALS_LOCK, &signals_lock);
Willy Tarreau8f38bd02009-05-10 08:53:33 +0200104}
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200105
106/* perform minimal intializations, report 0 in case of error, 1 if OK. */
107int 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 Tarreaud50b4ac2016-04-20 10:33:15 +0200114
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100115 HA_SPIN_INIT(&signals_lock);
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200116
Willy Tarreaud50b4ac2016-04-20 10:33:15 +0200117 /* 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 Lallemand73b85e72017-06-01 17:38:51 +0200122 /* 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 Tarreaud50b4ac2016-04-20 10:33:15 +0200126 sigprocmask(SIG_SETMASK, &blocked_sig, NULL);
127
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200128 sigfillset(&blocked_sig);
Willy Tarreau6747e272013-01-04 16:20:20 +0100129 sigdelset(&blocked_sig, SIGPROF);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200130 for (sig = 0; sig < MAX_SIGNAL; sig++)
131 LIST_INIT(&signal_state[sig].handlers);
132
133 pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler), MEM_F_SHARED);
134 return pool2_sig_handlers != NULL;
135}
136
137/* releases all registered signal handlers */
138void deinit_signals()
139{
140 int sig;
141 struct sig_handler *sh, *shb;
142
143 for (sig = 0; sig < MAX_SIGNAL; sig++) {
Willy Tarreau6747e272013-01-04 16:20:20 +0100144 if (sig != SIGPROF)
145 signal(sig, SIG_DFL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200146 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
147 LIST_DEL(&sh->list);
148 pool_free2(pool2_sig_handlers, sh);
149 }
150 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100151 HA_SPIN_DESTROY(&signals_lock);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200152}
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 Tarreaud0807c32010-08-27 18:26:11 +0200158 * 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 Tarreau24f4efa2010-08-27 17:56:48 +0200161 */
162struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
163{
164 struct sig_handler *sh;
165
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100166 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200167 return NULL;
168
Willy Tarreaud0807c32010-08-27 18:26:11 +0200169 if (sig)
Willy Tarreauc39b0d12012-10-04 19:19:36 +0200170 signal(sig, fct ? signal_handler : SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200171
172 if (!fct)
173 return NULL;
174
175 sh = pool_alloc2(pool2_sig_handlers);
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 Tarreaud0807c32010-08-27 18:26:11 +0200190 * 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 Tarreau24f4efa2010-08-27 17:56:48 +0200193 */
194struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
195{
196 struct sig_handler *sh;
197
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100198 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200199 return NULL;
200
Willy Tarreaud0807c32010-08-27 18:26:11 +0200201 if (sig)
202 signal(sig, signal_handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200203
204 if (!task)
205 return NULL;
206
207 sh = pool_alloc2(pool2_sig_handlers);
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 */
221void signal_unregister_handler(struct sig_handler *handler)
222{
223 LIST_DEL(&handler->list);
224 pool_free2(pool2_sig_handlers, handler);
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 */
233void signal_unregister_target(int sig, void *target)
234{
235 struct sig_handler *sh, *shb;
236
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100237 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200238 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);
246 pool_free2(pool2_sig_handlers, sh);
247 break;
248 }
249 }
250}