blob: 746ae9e0c3fe63977e9db63a9cf92ecbb76df188 [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;
32
Willy Tarreau24f4efa2010-08-27 17:56:48 +020033/* Common signal handler, used by all signals. Received signals are queued. */
34static void signal_handler(int sig)
Willy Tarreau8f38bd02009-05-10 08:53:33 +020035{
Willy Tarreau24f4efa2010-08-27 17:56:48 +020036 if (sig < 0 || sig > MAX_SIGNAL) {
Willy Tarreau8f38bd02009-05-10 08:53:33 +020037 /* unhandled signal */
Willy Tarreau8f38bd02009-05-10 08:53:33 +020038 signal(sig, SIG_IGN);
Willy Tarreau24f4efa2010-08-27 17:56:48 +020039 qfprintf(stderr, "Received unhandled signal %d. Signal has been disabled.\n", sig);
Willy Tarreau8f38bd02009-05-10 08:53:33 +020040 return;
41 }
42
43 if (!signal_state[sig].count) {
44 /* signal was not queued yet */
45 if (signal_queue_len < MAX_SIGNAL)
46 signal_queue[signal_queue_len++] = sig;
47 else
48 qfprintf(stderr, "Signal %d : signal queue is unexpectedly full.\n", sig);
49 }
50 signal_state[sig].count++;
51 signal(sig, signal_handler); /* re-arm signal */
52}
53
Willy Tarreau8f38bd02009-05-10 08:53:33 +020054/* Call handlers of all pending signals and clear counts and queue length. The
55 * handlers may unregister themselves by calling signal_register() while they
56 * are called, just like it is done with normal signal handlers.
57 * Note that it is more efficient to call the inline version which checks the
58 * queue length before getting here.
59 */
60void __signal_process_queue()
61{
62 int sig, cur_pos = 0;
63 struct signal_descriptor *desc;
64 sigset_t old_sig;
65
66 /* block signal delivery during processing */
67 sigprocmask(SIG_SETMASK, &blocked_sig, &old_sig);
68
69 for (cur_pos = 0; cur_pos < signal_queue_len; cur_pos++) {
70 sig = signal_queue[cur_pos];
71 desc = &signal_state[sig];
72 if (desc->count) {
Willy Tarreau24f4efa2010-08-27 17:56:48 +020073 struct sig_handler *sh, *shb;
74 list_for_each_entry_safe(sh, shb, &desc->handlers, list) {
75 if ((sh->flags & SIG_F_TYPE_FCT) && sh->handler)
76 ((void (*)(struct sig_handler *))sh->handler)(sh);
77 else if ((sh->flags & SIG_F_TYPE_TASK) && sh->handler)
78 task_wakeup(sh->handler, sh->arg | TASK_WOKEN_SIGNAL);
79 }
Willy Tarreau8f38bd02009-05-10 08:53:33 +020080 desc->count = 0;
81 }
82 }
83 signal_queue_len = 0;
84
85 /* restore signal delivery */
86 sigprocmask(SIG_SETMASK, &old_sig, NULL);
87}
Willy Tarreau24f4efa2010-08-27 17:56:48 +020088
89/* perform minimal intializations, report 0 in case of error, 1 if OK. */
90int signal_init()
91{
92 int sig;
93
94 signal_queue_len = 0;
95 memset(signal_queue, 0, sizeof(signal_queue));
96 memset(signal_state, 0, sizeof(signal_state));
97 sigfillset(&blocked_sig);
98 for (sig = 0; sig < MAX_SIGNAL; sig++)
99 LIST_INIT(&signal_state[sig].handlers);
100
101 pool2_sig_handlers = create_pool("sig_handlers", sizeof(struct sig_handler), MEM_F_SHARED);
102 return pool2_sig_handlers != NULL;
103}
104
105/* releases all registered signal handlers */
106void deinit_signals()
107{
108 int sig;
109 struct sig_handler *sh, *shb;
110
111 for (sig = 0; sig < MAX_SIGNAL; sig++) {
112 signal(sig, SIG_DFL);
113 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
114 LIST_DEL(&sh->list);
115 pool_free2(pool2_sig_handlers, sh);
116 }
117 }
118}
119
120/* Register a function and an integer argument on a signal. A pointer to the
121 * newly allocated sig_handler is returned, or NULL in case of any error. The
122 * caller is responsible for unregistering the function when not used anymore.
123 * Note that passing a NULL as the function pointer enables interception of the
124 * signal without processing, which is identical to SIG_IGN.
125 */
126struct sig_handler *signal_register_fct(int sig, void (*fct)(struct sig_handler *), int arg)
127{
128 struct sig_handler *sh;
129
130 if (sig < 0 || sig > MAX_SIGNAL)
131 return NULL;
132
133 signal(sig, signal_handler);
134
135 if (!fct)
136 return NULL;
137
138 sh = pool_alloc2(pool2_sig_handlers);
139 if (!sh)
140 return NULL;
141
142 sh->handler = fct;
143 sh->arg = arg;
144 sh->flags = SIG_F_TYPE_FCT;
145 LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
146 return sh;
147}
148
149/* Register a task and a wake-up reason on a signal. A pointer to the newly
150 * allocated sig_handler is returned, or NULL in case of any error. The caller
151 * is responsible for unregistering the task when not used anymore. Note that
152 * passing a NULL as the task pointer enables interception of the signal
153 * without processing, which is identical to SIG_IGN.
154 */
155struct sig_handler *signal_register_task(int sig, struct task *task, int reason)
156{
157 struct sig_handler *sh;
158
159 if (sig < 0 || sig > MAX_SIGNAL)
160 return NULL;
161
162 signal(sig, signal_handler);
163
164 if (!task)
165 return NULL;
166
167 sh = pool_alloc2(pool2_sig_handlers);
168 if (!sh)
169 return NULL;
170
171 sh->handler = task;
172 sh->arg = reason & ~TASK_WOKEN_ANY;
173 sh->flags = SIG_F_TYPE_TASK;
174 LIST_ADDQ(&signal_state[sig].handlers, &sh->list);
175 return sh;
176}
177
178/* Immediately unregister a handler so that no further signals may be delivered
179 * to it. The struct is released so the caller may not reference it anymore.
180 */
181void signal_unregister_handler(struct sig_handler *handler)
182{
183 LIST_DEL(&handler->list);
184 pool_free2(pool2_sig_handlers, handler);
185}
186
187/* Immediately unregister a handler so that no further signals may be delivered
188 * to it. The handler struct does not need to be known, only the function or
189 * task pointer. This method is expensive because it scans all the list, so it
190 * should only be used for rare cases (eg: exit). The struct is released so the
191 * caller may not reference it anymore.
192 */
193void signal_unregister_target(int sig, void *target)
194{
195 struct sig_handler *sh, *shb;
196
197 if (sig < 0 || sig > MAX_SIGNAL)
198 return;
199
200 if (!target)
201 return;
202
203 list_for_each_entry_safe(sh, shb, &signal_state[sig].handlers, list) {
204 if (sh->handler == target) {
205 LIST_DEL(&sh->list);
206 pool_free2(pool2_sig_handlers, sh);
207 break;
208 }
209 }
210}