blob: 7b726225729e67aeec6bb1144831ad2e86fe7a5f [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
Willy Tarreaud0807c32010-08-27 18:26:11 +020034/* 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 */
38void signal_handler(int sig)
Willy Tarreau8f38bd02009-05-10 08:53:33 +020039{
Willy Tarreau1a53b5e2013-01-24 02:06:05 +010040 if (sig < 0 || sig >= MAX_SIGNAL) {
Willy Tarreau8f38bd02009-05-10 08:53:33 +020041 /* unhandled signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020042 signal(sig, SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020043 qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020044 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 Tarreaud0807c32010-08-27 18:26:11 +020054
Willy Tarreau8f38bd02009-05-10 08:53:33 +020055 signal_state[sig].count++;
Willy Tarreaud0807c32010-08-27 18:26:11 +020056 if (sig)
57 signal(sig, signal_handler); /* re-arm signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020058}
59
Willy Tarreau8f38bd02009-05-10 08:53:33 +020060/* 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 */
66void __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 Tarreaud0807c32010-08-27 18:26:11 +020075 /* 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 Tarreau8f38bd02009-05-10 08:53:33 +020080 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 Tarreau24f4efa2010-08-27 17:56:48 +020084 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 Tarreau8f38bd02009-05-10 08:53:33 +020091 desc->count = 0;
92 }
93 }
94 signal_queue_len = 0;
95
96 /* restore signal delivery */
97 sigprocmask(SIG_SETMASK, &old_sig, NULL);
98}
Willy Tarreau24f4efa2010-08-27 17:56:48 +020099
100/* perform minimal intializations, report 0 in case of error, 1 if OK. */
101int 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 Tarreaud50b4ac2016-04-20 10:33:15 +0200108
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);
114 sigprocmask(SIG_SETMASK, &blocked_sig, NULL);
115
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200116 sigfillset(&blocked_sig);
Willy Tarreau6747e272013-01-04 16:20:20 +0100117 sigdelset(&blocked_sig, SIGPROF);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200118 for (sig = 0; sig < MAX_SIGNAL; sig++)
119 LIST_INIT(&signal_state[sig].handlers);
120
121 pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler), MEM_F_SHARED);
122 return pool2_sig_handlers != NULL;
123}
124
125/* releases all registered signal handlers */
126void deinit_signals()
127{
128 int sig;
129 struct sig_handler *sh, *shb;
130
131 for (sig = 0; sig < MAX_SIGNAL; sig++) {
Willy Tarreau6747e272013-01-04 16:20:20 +0100132 if (sig != SIGPROF)
133 signal(sig, SIG_DFL);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200134 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
135 LIST_DEL(&sh->list);
136 pool_free2(pool2_sig_handlers, sh);
137 }
138 }
139}
140
141/* Register a function and an integer argument on a signal. A pointer to the
142 * newly allocated sig_handler is returned, or NULL in case of any error. The
143 * caller is responsible for unregistering the function when not used anymore.
144 * Note that passing a NULL as the function pointer enables interception of the
Willy Tarreaud0807c32010-08-27 18:26:11 +0200145 * signal without processing, which is identical to SIG_IGN. If the signal is
146 * zero (which the system cannot deliver), only internal functions will be able
147 * to notify the registered functions.
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200148 */
149struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
150{
151 struct sig_handler *sh;
152
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100153 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200154 return NULL;
155
Willy Tarreaud0807c32010-08-27 18:26:11 +0200156 if (sig)
Willy Tarreauc39b0d12012-10-04 19:19:36 +0200157 signal(sig, fct ? signal_handler : SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200158
159 if (!fct)
160 return NULL;
161
162 sh = pool_alloc2(pool2_sig_handlers);
163 if (!sh)
164 return NULL;
165
166 sh->handler = fct;
167 sh->arg = arg;
168 sh->flags = SIG_F_TYPE_FCT;
169 LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
170 return sh;
171}
172
173/* Register a task and a wake-up reason on a signal. A pointer to the newly
174 * allocated sig_handler is returned, or NULL in case of any error. The caller
175 * is responsible for unregistering the task when not used anymore. Note that
176 * passing a NULL as the task pointer enables interception of the signal
Willy Tarreaud0807c32010-08-27 18:26:11 +0200177 * without processing, which is identical to SIG_IGN. If the signal is zero
178 * (which the system cannot deliver), only internal functions will be able to
179 * notify the registered functions.
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200180 */
181struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
182{
183 struct sig_handler *sh;
184
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100185 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200186 return NULL;
187
Willy Tarreaud0807c32010-08-27 18:26:11 +0200188 if (sig)
189 signal(sig, signal_handler);
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200190
191 if (!task)
192 return NULL;
193
194 sh = pool_alloc2(pool2_sig_handlers);
195 if (!sh)
196 return NULL;
197
198 sh->handler = task;
199 sh->arg = reason & ~TASK_WOKEN_ANY;
200 sh->flags = SIG_F_TYPE_TASK;
201 LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
202 return sh;
203}
204
205/* Immediately unregister a handler so that no further signals may be delivered
206 * to it. The struct is released so the caller may not reference it anymore.
207 */
208void signal_unregister_handler(struct sig_handler *handler)
209{
210 LIST_DEL(&handler->list);
211 pool_free2(pool2_sig_handlers, handler);
212}
213
214/* Immediately unregister a handler so that no further signals may be delivered
215 * to it. The handler struct does not need to be known, only the function or
216 * task pointer. This method is expensive because it scans all the list, so it
217 * should only be used for rare cases (eg: exit). The struct is released so the
218 * caller may not reference it anymore.
219 */
220void signal_unregister_target(int sig, void *target)
221{
222 struct sig_handler *sh, *shb;
223
Willy Tarreau1a53b5e2013-01-24 02:06:05 +0100224 if (sig < 0 || sig >= MAX_SIGNAL)
Willy Tarreau24f4efa2010-08-27 17:56:48 +0200225 return;
226
227 if (!target)
228 return;
229
230 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
231 if (sh->handler == target) {
232 LIST_DEL(&sh->list);
233 pool_free2(pool2_sig_handlers, sh);
234 break;
235 }
236 }
237}