2 Unix SMB/CIFS implementation.
4 common events code for signal events
6 Copyright (C) Andrew Tridgell 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
24 #include "system/wait.h"
26 #include "tevent_internal.h"
27 #include "tevent_util.h"
29 #define NUM_SIGNALS 64
31 /* maximum number of SA_SIGINFO signals to hold in the queue */
32 #define SA_INFO_QUEUE_COUNT 100
39 #define SIG_INCREMENT(s) (s).count++
40 #define SIG_SEEN(s, n) (s).seen += (n)
41 #define SIG_PENDING(s) ((s).seen != (s).count)
43 struct tevent_common_signal_list {
44 struct tevent_common_signal_list *prev, *next;
45 struct tevent_signal *se;
49 the poor design of signals means that this table must be static global
51 static struct sig_state {
52 struct tevent_common_signal_list *sig_handlers[NUM_SIGNALS+1];
53 struct sigaction *oldact[NUM_SIGNALS+1];
54 struct sigcounter signal_count[NUM_SIGNALS+1];
55 struct sigcounter got_signal;
58 /* with SA_SIGINFO we get quite a lot of info per signal */
59 siginfo_t *sig_info[NUM_SIGNALS+1];
60 struct sigcounter sig_blocked[NUM_SIGNALS+1];
65 return number of sigcounter events not processed yet
67 static uint32_t sig_count(struct sigcounter s)
69 if (s.count >= s.seen) {
70 return s.count - s.seen;
72 return 1 + (0xFFFFFFFF & ~(s.seen - s.count));
76 signal handler - redirects to registered signals
78 static void tevent_common_signal_handler(int signum)
82 SIG_INCREMENT(sig_state->signal_count[signum]);
83 SIG_INCREMENT(sig_state->got_signal);
84 /* doesn't matter if this pipe overflows */
85 res = write(sig_state->pipe_hack[1], &c, 1);
90 signal handler with SA_SIGINFO - redirects to registered signals
92 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
95 uint32_t count = sig_count(sig_state->signal_count[signum]);
96 sig_state->sig_info[signum][count] = *info;
98 tevent_common_signal_handler(signum);
100 /* handle SA_SIGINFO */
101 if (count+1 == SA_INFO_QUEUE_COUNT) {
102 /* we've filled the info array - block this signal until
103 these ones are delivered */
106 sigaddset(&set, signum);
107 sigprocmask(SIG_BLOCK, &set, NULL);
108 SIG_INCREMENT(sig_state->sig_blocked[signum]);
113 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
115 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
120 destroy a signal event
122 static int tevent_signal_destructor(struct tevent_signal *se)
124 struct tevent_common_signal_list *sl;
125 sl = talloc_get_type(se->additional_data,
126 struct tevent_common_signal_list);
129 DLIST_REMOVE(se->event_ctx->signal_events, se);
134 if (sig_state->sig_handlers[se->signum] == NULL) {
135 /* restore old handler, if any */
136 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
137 sig_state->oldact[se->signum] = NULL;
139 if (se->sa_flags & SA_SIGINFO) {
140 talloc_free(sig_state->sig_info[se->signum]);
141 sig_state->sig_info[se->signum] = NULL;
150 this is part of the pipe hack needed to avoid the signal race condition
152 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
153 uint16_t flags, void *private)
157 /* its non-blocking, doesn't matter if we read too much */
158 res = read(sig_state->pipe_hack[0], c, sizeof(c));
163 return NULL on failure (memory allocation error)
165 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
169 tevent_signal_handler_t handler,
171 const char *handler_name,
172 const char *location)
174 struct tevent_signal *se;
175 struct tevent_common_signal_list *sl;
177 if (signum >= NUM_SIGNALS) {
182 /* the sig_state needs to be on a global context as it can last across
183 multiple event contexts */
184 if (sig_state == NULL) {
185 sig_state = talloc_zero(talloc_autofree_context(), struct sig_state);
186 if (sig_state == NULL) {
191 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
192 if (se == NULL) return NULL;
196 se->sa_flags = sa_flags;
197 se->handler = handler;
198 se->private_data = private_data;
199 se->handler_name = handler_name;
200 se->location = location;
201 se->additional_data = NULL;
203 sl = talloc(se, struct tevent_common_signal_list);
209 se->additional_data = sl;
211 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
212 if (!talloc_reference(se, sig_state)) {
217 /* only install a signal handler if not already installed */
218 if (sig_state->sig_handlers[signum] == NULL) {
219 struct sigaction act;
221 act.sa_handler = tevent_common_signal_handler;
222 act.sa_flags = sa_flags;
224 if (sa_flags & SA_SIGINFO) {
225 act.sa_handler = NULL;
226 act.sa_sigaction = tevent_common_signal_handler_info;
227 if (sig_state->sig_info[signum] == NULL) {
228 sig_state->sig_info[signum] = talloc_array(sig_state, siginfo_t, SA_INFO_QUEUE_COUNT);
229 if (sig_state->sig_info[signum] == NULL) {
236 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
237 if (sig_state->oldact[signum] == NULL) {
241 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
247 DLIST_ADD(se->event_ctx->signal_events, se);
248 DLIST_ADD(sig_state->sig_handlers[signum], sl);
250 talloc_set_destructor(se, tevent_signal_destructor);
251 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
253 /* we need to setup the pipe hack handler if not already
255 if (ev->pipe_fde == NULL) {
256 if (sig_state->pipe_hack[0] == 0 &&
257 sig_state->pipe_hack[1] == 0) {
258 if (pipe(sig_state->pipe_hack) == -1) {
262 ev_set_blocking(sig_state->pipe_hack[0], false);
263 ev_set_blocking(sig_state->pipe_hack[1], false);
265 ev->pipe_fde = tevent_add_fd(ev, ev, sig_state->pipe_hack[0],
266 TEVENT_FD_READ, signal_pipe_handler, NULL);
278 check if a signal is pending
279 return != 0 if a signal was pending
281 int tevent_common_check_signal(struct tevent_context *ev)
285 if (!sig_state || !SIG_PENDING(sig_state->got_signal)) {
289 for (i=0;i<NUM_SIGNALS+1;i++) {
290 struct tevent_common_signal_list *sl, *next;
291 struct sigcounter counter = sig_state->signal_count[i];
292 uint32_t count = sig_count(counter);
297 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
298 struct tevent_signal *se = sl->se;
301 if (se->sa_flags & SA_SIGINFO) {
303 for (j=0;j<count;j++) {
304 /* note the use of the sig_info array as a
306 int ofs = ((count-1) + j) % SA_INFO_QUEUE_COUNT;
307 se->handler(ev, se, i, 1,
308 (void*)&sig_state->sig_info[i][ofs],
311 if (SIG_PENDING(sig_state->sig_blocked[i])) {
312 /* we'd filled the queue, unblock the
317 SIG_SEEN(sig_state->sig_blocked[i],
318 sig_count(sig_state->sig_blocked[i]));
319 sigprocmask(SIG_UNBLOCK, &set, NULL);
321 if (se->sa_flags & SA_RESETHAND) {
327 se->handler(ev, se, i, count, NULL, se->private_data);
328 if (se->sa_flags & SA_RESETHAND) {
332 SIG_SEEN(sig_state->signal_count[i], count);
333 SIG_SEEN(sig_state->got_signal, count);