tdb: always open internal databases with incompatible hash.
[Samba.git] / lib / tevent / tevent_signal.c
blob1ff487256e937e7f9c9f83b9f941a619b04dcff0
1 /*
2 Unix SMB/CIFS implementation.
4 common events code for signal events
6 Copyright (C) Andrew Tridgell 2007
8 ** NOTE! The following LGPL license applies to the tevent
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "replace.h"
27 #include "system/filesys.h"
28 #include "system/wait.h"
29 #include "tevent.h"
30 #include "tevent_internal.h"
31 #include "tevent_util.h"
33 /* maximum number of SA_SIGINFO signals to hold in the queue.
34 NB. This *MUST* be a power of 2, in order for the ring buffer
35 wrap to work correctly. Thanks to Petr Vandrovec <petr@vandrovec.name>
36 for this. */
38 #define TEVENT_SA_INFO_QUEUE_COUNT 256
40 size_t tevent_num_signals(void)
42 return TEVENT_NUM_SIGNALS;
45 size_t tevent_sa_info_queue_count(void)
47 return TEVENT_SA_INFO_QUEUE_COUNT;
50 struct tevent_sigcounter {
51 uint32_t count;
52 uint32_t seen;
55 #define TEVENT_SIG_INCREMENT(s) (s).count++
56 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
57 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
59 struct tevent_common_signal_list {
60 struct tevent_common_signal_list *prev, *next;
61 struct tevent_signal *se;
65 the poor design of signals means that this table must be static global
67 static struct tevent_sig_state {
68 struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
69 struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
70 struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
71 struct tevent_sigcounter got_signal;
72 #ifdef SA_SIGINFO
73 /* with SA_SIGINFO we get quite a lot of info per signal */
74 siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
75 struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
76 #endif
77 } *sig_state;
80 return number of sigcounter events not processed yet
82 static uint32_t tevent_sig_count(struct tevent_sigcounter s)
84 return s.count - s.seen;
88 signal handler - redirects to registered signals
90 static void tevent_common_signal_handler(int signum)
92 char c = 0;
93 struct tevent_common_signal_list *sl;
94 struct tevent_context *ev = NULL;
95 int saved_errno = errno;
97 TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
98 TEVENT_SIG_INCREMENT(sig_state->got_signal);
100 /* Write to each unique event context. */
101 for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
102 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
103 ev = sl->se->event_ctx;
104 /* doesn't matter if this pipe overflows */
105 (void) write(ev->pipe_fds[1], &c, 1);
109 errno = saved_errno;
112 #ifdef SA_SIGINFO
114 signal handler with SA_SIGINFO - redirects to registered signals
116 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
117 void *uctx)
119 uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
120 /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
121 * is the base of the unprocessed signals in the ringbuffer. */
122 uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
123 TEVENT_SA_INFO_QUEUE_COUNT;
124 sig_state->sig_info[signum][ofs] = *info;
126 tevent_common_signal_handler(signum);
128 /* handle SA_SIGINFO */
129 if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
130 /* we've filled the info array - block this signal until
131 these ones are delivered */
132 #ifdef HAVE_UCONTEXT_T
134 * This is the only way for this to work.
135 * By default signum is blocked inside this
136 * signal handler using a temporary mask,
137 * but what we really need to do now is
138 * block it in the callers mask, so it
139 * stays blocked when the temporary signal
140 * handler mask is replaced when we return
141 * from here. The callers mask can be found
142 * in the ucontext_t passed in as the
143 * void *uctx argument.
145 ucontext_t *ucp = (ucontext_t *)uctx;
146 sigaddset(&ucp->uc_sigmask, signum);
147 #else
149 * WARNING !!! WARNING !!!!
151 * This code doesn't work.
152 * By default signum is blocked inside this
153 * signal handler, but calling sigprocmask
154 * modifies the temporary signal mask being
155 * used *inside* this handler, which will be
156 * replaced by the callers signal mask once
157 * we return from here. See Samba
158 * bug #9550 for details.
160 sigset_t set;
161 sigemptyset(&set);
162 sigaddset(&set, signum);
163 sigprocmask(SIG_BLOCK, &set, NULL);
164 #endif
165 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
168 #endif
170 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
172 if (sig_state->sig_handlers[sl->se->signum]) {
173 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
175 return 0;
179 destroy a signal event
181 static int tevent_signal_destructor(struct tevent_signal *se)
183 struct tevent_common_signal_list *sl =
184 talloc_get_type_abort(se->additional_data,
185 struct tevent_common_signal_list);
187 if (se->event_ctx) {
188 struct tevent_context *ev = se->event_ctx;
190 DLIST_REMOVE(ev->signal_events, se);
192 if (ev->signal_events == NULL && ev->pipe_fde != NULL) {
194 * This was the last signal. Destroy the pipe.
196 TALLOC_FREE(ev->pipe_fde);
198 close(ev->pipe_fds[0]);
199 close(ev->pipe_fds[1]);
203 talloc_free(sl);
205 if (sig_state->sig_handlers[se->signum] == NULL) {
206 /* restore old handler, if any */
207 if (sig_state->oldact[se->signum]) {
208 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
209 sig_state->oldact[se->signum] = NULL;
211 #ifdef SA_SIGINFO
212 if (se->sa_flags & SA_SIGINFO) {
213 if (sig_state->sig_info[se->signum]) {
214 talloc_free(sig_state->sig_info[se->signum]);
215 sig_state->sig_info[se->signum] = NULL;
218 #endif
221 return 0;
225 this is part of the pipe hack needed to avoid the signal race condition
227 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
228 uint16_t flags, void *_private)
230 char c[16];
231 /* its non-blocking, doesn't matter if we read too much */
232 (void) read(fde->fd, c, sizeof(c));
236 add a signal event
237 return NULL on failure (memory allocation error)
239 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
240 TALLOC_CTX *mem_ctx,
241 int signum,
242 int sa_flags,
243 tevent_signal_handler_t handler,
244 void *private_data,
245 const char *handler_name,
246 const char *location)
248 struct tevent_signal *se;
249 struct tevent_common_signal_list *sl;
250 sigset_t set, oldset;
252 if (signum >= TEVENT_NUM_SIGNALS) {
253 errno = EINVAL;
254 return NULL;
257 /* the sig_state needs to be on a global context as it can last across
258 multiple event contexts */
259 if (sig_state == NULL) {
260 sig_state = talloc_zero(NULL, struct tevent_sig_state);
261 if (sig_state == NULL) {
262 return NULL;
266 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
267 if (se == NULL) return NULL;
269 se->event_ctx = ev;
270 se->signum = signum;
271 se->sa_flags = sa_flags;
272 se->handler = handler;
273 se->private_data = private_data;
274 se->handler_name = handler_name;
275 se->location = location;
276 se->additional_data = NULL;
278 sl = talloc(se, struct tevent_common_signal_list);
279 if (!sl) {
280 talloc_free(se);
281 return NULL;
283 sl->se = se;
284 se->additional_data = sl;
286 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
287 if (!talloc_reference(se, sig_state)) {
288 talloc_free(se);
289 return NULL;
292 /* we need to setup the pipe hack handler if not already
293 setup */
294 if (ev->pipe_fde == NULL) {
295 if (pipe(ev->pipe_fds) == -1) {
296 talloc_free(se);
297 return NULL;
299 ev_set_blocking(ev->pipe_fds[0], false);
300 ev_set_blocking(ev->pipe_fds[1], false);
301 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
302 TEVENT_FD_READ,
303 signal_pipe_handler, NULL);
304 if (!ev->pipe_fde) {
305 close(ev->pipe_fds[0]);
306 close(ev->pipe_fds[1]);
307 talloc_free(se);
308 return NULL;
312 /* only install a signal handler if not already installed */
313 if (sig_state->sig_handlers[signum] == NULL) {
314 struct sigaction act;
315 ZERO_STRUCT(act);
316 act.sa_handler = tevent_common_signal_handler;
317 act.sa_flags = sa_flags;
318 #ifdef SA_SIGINFO
319 if (sa_flags & SA_SIGINFO) {
320 act.sa_handler = NULL;
321 act.sa_sigaction = tevent_common_signal_handler_info;
322 if (sig_state->sig_info[signum] == NULL) {
323 sig_state->sig_info[signum] =
324 talloc_zero_array(sig_state, siginfo_t,
325 TEVENT_SA_INFO_QUEUE_COUNT);
326 if (sig_state->sig_info[signum] == NULL) {
327 talloc_free(se);
328 return NULL;
332 #endif
333 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
334 if (sig_state->oldact[signum] == NULL) {
335 talloc_free(se);
336 return NULL;
338 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
339 talloc_free(se);
340 return NULL;
344 DLIST_ADD(se->event_ctx->signal_events, se);
346 /* Make sure the signal doesn't come in while we're mangling list. */
347 sigemptyset(&set);
348 sigaddset(&set, signum);
349 sigprocmask(SIG_BLOCK, &set, &oldset);
350 DLIST_ADD(sig_state->sig_handlers[signum], sl);
351 sigprocmask(SIG_SETMASK, &oldset, NULL);
353 talloc_set_destructor(se, tevent_signal_destructor);
354 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
356 return se;
359 struct tevent_se_exists {
360 struct tevent_se_exists **myself;
363 static int tevent_se_exists_destructor(struct tevent_se_exists *s)
365 *s->myself = NULL;
366 return 0;
370 check if a signal is pending
371 return != 0 if a signal was pending
373 int tevent_common_check_signal(struct tevent_context *ev)
375 int i;
377 if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
378 return 0;
381 for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
382 struct tevent_common_signal_list *sl, *next;
383 struct tevent_sigcounter counter = sig_state->signal_count[i];
384 uint32_t count = tevent_sig_count(counter);
385 #ifdef SA_SIGINFO
386 /* Ensure we null out any stored siginfo_t entries
387 * after processing for debugging purposes. */
388 bool clear_processed_siginfo = false;
389 #endif
391 if (count == 0) {
392 continue;
394 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
395 struct tevent_signal *se = sl->se;
396 struct tevent_se_exists *exists;
398 next = sl->next;
401 * We have to be careful to not touch "se"
402 * after it was deleted in its handler. Thus
403 * we allocate a child whose destructor will
404 * tell by nulling out itself that its parent
405 * is gone.
407 exists = talloc(se, struct tevent_se_exists);
408 if (exists == NULL) {
409 continue;
411 exists->myself = &exists;
412 talloc_set_destructor(
413 exists, tevent_se_exists_destructor);
415 #ifdef SA_SIGINFO
416 if (se->sa_flags & SA_SIGINFO) {
417 uint32_t j;
419 clear_processed_siginfo = true;
421 for (j=0;j<count;j++) {
422 /* sig_state->signal_count[i].seen
423 * % TEVENT_SA_INFO_QUEUE_COUNT is
424 * the base position of the unprocessed
425 * signals in the ringbuffer. */
426 uint32_t ofs = (counter.seen + j)
427 % TEVENT_SA_INFO_QUEUE_COUNT;
428 se->handler(ev, se, i, 1,
429 (void*)&sig_state->sig_info[i][ofs],
430 se->private_data);
431 if (!exists) {
432 break;
435 #ifdef SA_RESETHAND
436 if (exists && (se->sa_flags & SA_RESETHAND)) {
437 talloc_free(se);
439 #endif
440 talloc_free(exists);
441 continue;
443 #endif
444 se->handler(ev, se, i, count, NULL, se->private_data);
445 #ifdef SA_RESETHAND
446 if (exists && (se->sa_flags & SA_RESETHAND)) {
447 talloc_free(se);
449 #endif
450 talloc_free(exists);
453 #ifdef SA_SIGINFO
454 if (clear_processed_siginfo) {
455 uint32_t j;
456 for (j=0;j<count;j++) {
457 uint32_t ofs = (counter.seen + j)
458 % TEVENT_SA_INFO_QUEUE_COUNT;
459 memset((void*)&sig_state->sig_info[i][ofs],
460 '\0',
461 sizeof(siginfo_t));
464 #endif
466 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
467 TEVENT_SIG_SEEN(sig_state->got_signal, count);
469 #ifdef SA_SIGINFO
470 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
471 /* We'd filled the queue, unblock the
472 signal now the queue is empty again.
473 Note we MUST do this after the
474 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
475 call to prevent a new signal running
476 out of room in the sig_state->sig_info[i][]
477 ring buffer. */
478 sigset_t set;
479 sigemptyset(&set);
480 sigaddset(&set, i);
481 TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
482 tevent_sig_count(sig_state->sig_blocked[i]));
483 sigprocmask(SIG_UNBLOCK, &set, NULL);
485 #endif
488 return 1;
491 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
493 struct tevent_common_signal_list *sl =
494 talloc_get_type_abort(se->additional_data,
495 struct tevent_common_signal_list);
497 tevent_common_signal_list_destructor(sl);
499 if (sig_state->sig_handlers[se->signum] == NULL) {
500 if (sig_state->oldact[se->signum]) {
501 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
502 sig_state->oldact[se->signum] = NULL;
505 return;