s4-selftest: add functions which create with desired access
[Samba.git] / lib / tevent / tevent_signal.c
blob9bc11edca42f5d3760a3035a507da73a70f3a780
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 #if defined(HAVE___SYNC_FETCH_AND_ADD)
56 #define TEVENT_SIG_INCREMENT(s) __sync_fetch_and_add(&((s).count), 1)
57 #elif defined(HAVE_ATOMIC_ADD_32)
58 #define TEVENT_SIG_INCREMENT(s) atomic_add_32(&((s).count), 1)
59 #else
60 #define TEVENT_SIG_INCREMENT(s) (s).count++
61 #endif
62 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
63 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
65 struct tevent_common_signal_list {
66 struct tevent_common_signal_list *prev, *next;
67 struct tevent_signal *se;
71 the poor design of signals means that this table must be static global
73 static struct tevent_sig_state {
74 struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
75 struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
76 struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
77 struct tevent_sigcounter got_signal;
78 #ifdef SA_SIGINFO
79 /* with SA_SIGINFO we get quite a lot of info per signal */
80 siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
81 struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
82 #endif
83 } *sig_state;
86 return number of sigcounter events not processed yet
88 static uint32_t tevent_sig_count(struct tevent_sigcounter s)
90 return s.count - s.seen;
94 signal handler - redirects to registered signals
96 static void tevent_common_signal_handler(int signum)
98 char c = 0;
99 struct tevent_common_signal_list *sl;
100 struct tevent_context *ev = NULL;
101 int saved_errno = errno;
103 TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
104 TEVENT_SIG_INCREMENT(sig_state->got_signal);
106 /* Write to each unique event context. */
107 for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
108 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
109 ev = sl->se->event_ctx;
110 /* doesn't matter if this pipe overflows */
111 (void) write(ev->pipe_fds[1], &c, 1);
115 errno = saved_errno;
118 #ifdef SA_SIGINFO
120 signal handler with SA_SIGINFO - redirects to registered signals
122 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
123 void *uctx)
125 uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
126 /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
127 * is the base of the unprocessed signals in the ringbuffer. */
128 uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
129 TEVENT_SA_INFO_QUEUE_COUNT;
130 sig_state->sig_info[signum][ofs] = *info;
132 tevent_common_signal_handler(signum);
134 /* handle SA_SIGINFO */
135 if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
136 /* we've filled the info array - block this signal until
137 these ones are delivered */
138 #ifdef HAVE_UCONTEXT_T
140 * This is the only way for this to work.
141 * By default signum is blocked inside this
142 * signal handler using a temporary mask,
143 * but what we really need to do now is
144 * block it in the callers mask, so it
145 * stays blocked when the temporary signal
146 * handler mask is replaced when we return
147 * from here. The callers mask can be found
148 * in the ucontext_t passed in as the
149 * void *uctx argument.
151 ucontext_t *ucp = (ucontext_t *)uctx;
152 sigaddset(&ucp->uc_sigmask, signum);
153 #else
155 * WARNING !!! WARNING !!!!
157 * This code doesn't work.
158 * By default signum is blocked inside this
159 * signal handler, but calling sigprocmask
160 * modifies the temporary signal mask being
161 * used *inside* this handler, which will be
162 * replaced by the callers signal mask once
163 * we return from here. See Samba
164 * bug #9550 for details.
166 sigset_t set;
167 sigemptyset(&set);
168 sigaddset(&set, signum);
169 sigprocmask(SIG_BLOCK, &set, NULL);
170 #endif
171 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
174 #endif
176 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
178 if (sig_state->sig_handlers[sl->se->signum]) {
179 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
181 return 0;
185 destroy a signal event
187 static int tevent_signal_destructor(struct tevent_signal *se)
189 struct tevent_common_signal_list *sl =
190 talloc_get_type_abort(se->additional_data,
191 struct tevent_common_signal_list);
193 if (se->event_ctx) {
194 struct tevent_context *ev = se->event_ctx;
196 DLIST_REMOVE(ev->signal_events, se);
198 if (ev->signal_events == NULL && ev->pipe_fde != NULL) {
200 * This was the last signal. Destroy the pipe.
202 TALLOC_FREE(ev->pipe_fde);
204 close(ev->pipe_fds[0]);
205 close(ev->pipe_fds[1]);
209 talloc_free(sl);
211 if (sig_state->sig_handlers[se->signum] == NULL) {
212 /* restore old handler, if any */
213 if (sig_state->oldact[se->signum]) {
214 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
215 talloc_free(sig_state->oldact[se->signum]);
216 sig_state->oldact[se->signum] = NULL;
218 #ifdef SA_SIGINFO
219 if (se->sa_flags & SA_SIGINFO) {
220 if (sig_state->sig_info[se->signum]) {
221 talloc_free(sig_state->sig_info[se->signum]);
222 sig_state->sig_info[se->signum] = NULL;
225 #endif
228 return 0;
232 this is part of the pipe hack needed to avoid the signal race condition
234 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
235 uint16_t flags, void *_private)
237 char c[16];
238 /* its non-blocking, doesn't matter if we read too much */
239 (void) read(fde->fd, c, sizeof(c));
243 add a signal event
244 return NULL on failure (memory allocation error)
246 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
247 TALLOC_CTX *mem_ctx,
248 int signum,
249 int sa_flags,
250 tevent_signal_handler_t handler,
251 void *private_data,
252 const char *handler_name,
253 const char *location)
255 struct tevent_signal *se;
256 struct tevent_common_signal_list *sl;
257 sigset_t set, oldset;
259 if (signum >= TEVENT_NUM_SIGNALS) {
260 errno = EINVAL;
261 return NULL;
264 /* the sig_state needs to be on a global context as it can last across
265 multiple event contexts */
266 if (sig_state == NULL) {
267 sig_state = talloc_zero(NULL, struct tevent_sig_state);
268 if (sig_state == NULL) {
269 return NULL;
273 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
274 if (se == NULL) return NULL;
276 se->event_ctx = ev;
277 se->signum = signum;
278 se->sa_flags = sa_flags;
279 se->handler = handler;
280 se->private_data = private_data;
281 se->handler_name = handler_name;
282 se->location = location;
283 se->additional_data = NULL;
285 sl = talloc(se, struct tevent_common_signal_list);
286 if (!sl) {
287 talloc_free(se);
288 return NULL;
290 sl->se = se;
291 se->additional_data = sl;
293 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
294 if (!talloc_reference(se, sig_state)) {
295 talloc_free(se);
296 return NULL;
299 /* we need to setup the pipe hack handler if not already
300 setup */
301 if (ev->pipe_fde == NULL) {
302 if (pipe(ev->pipe_fds) == -1) {
303 talloc_free(se);
304 return NULL;
306 ev_set_blocking(ev->pipe_fds[0], false);
307 ev_set_blocking(ev->pipe_fds[1], false);
308 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
309 TEVENT_FD_READ,
310 signal_pipe_handler, NULL);
311 if (!ev->pipe_fde) {
312 close(ev->pipe_fds[0]);
313 close(ev->pipe_fds[1]);
314 talloc_free(se);
315 return NULL;
319 /* only install a signal handler if not already installed */
320 if (sig_state->sig_handlers[signum] == NULL) {
321 struct sigaction act;
322 ZERO_STRUCT(act);
323 act.sa_handler = tevent_common_signal_handler;
324 act.sa_flags = sa_flags;
325 #ifdef SA_SIGINFO
326 if (sa_flags & SA_SIGINFO) {
327 act.sa_handler = NULL;
328 act.sa_sigaction = tevent_common_signal_handler_info;
329 if (sig_state->sig_info[signum] == NULL) {
330 sig_state->sig_info[signum] =
331 talloc_zero_array(sig_state, siginfo_t,
332 TEVENT_SA_INFO_QUEUE_COUNT);
333 if (sig_state->sig_info[signum] == NULL) {
334 talloc_free(se);
335 return NULL;
339 #endif
340 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
341 if (sig_state->oldact[signum] == NULL) {
342 talloc_free(se);
343 return NULL;
345 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
346 talloc_free(sig_state->oldact[signum]);
347 sig_state->oldact[signum] = NULL;
348 talloc_free(se);
349 return NULL;
353 DLIST_ADD(se->event_ctx->signal_events, se);
355 /* Make sure the signal doesn't come in while we're mangling list. */
356 sigemptyset(&set);
357 sigaddset(&set, signum);
358 sigprocmask(SIG_BLOCK, &set, &oldset);
359 DLIST_ADD(sig_state->sig_handlers[signum], sl);
360 sigprocmask(SIG_SETMASK, &oldset, NULL);
362 talloc_set_destructor(se, tevent_signal_destructor);
363 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
365 return se;
368 struct tevent_se_exists {
369 struct tevent_se_exists **myself;
372 static int tevent_se_exists_destructor(struct tevent_se_exists *s)
374 *s->myself = NULL;
375 return 0;
379 check if a signal is pending
380 return != 0 if a signal was pending
382 int tevent_common_check_signal(struct tevent_context *ev)
384 int i;
386 if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
387 return 0;
390 for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
391 struct tevent_common_signal_list *sl, *next;
392 struct tevent_sigcounter counter = sig_state->signal_count[i];
393 uint32_t count = tevent_sig_count(counter);
394 #ifdef SA_SIGINFO
395 /* Ensure we null out any stored siginfo_t entries
396 * after processing for debugging purposes. */
397 bool clear_processed_siginfo = false;
398 #endif
400 if (count == 0) {
401 continue;
403 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
404 struct tevent_signal *se = sl->se;
405 struct tevent_se_exists *exists;
407 next = sl->next;
410 * We have to be careful to not touch "se"
411 * after it was deleted in its handler. Thus
412 * we allocate a child whose destructor will
413 * tell by nulling out itself that its parent
414 * is gone.
416 exists = talloc(se, struct tevent_se_exists);
417 if (exists == NULL) {
418 continue;
420 exists->myself = &exists;
421 talloc_set_destructor(
422 exists, tevent_se_exists_destructor);
424 #ifdef SA_SIGINFO
425 if (se->sa_flags & SA_SIGINFO) {
426 uint32_t j;
428 clear_processed_siginfo = true;
430 for (j=0;j<count;j++) {
431 /* sig_state->signal_count[i].seen
432 * % TEVENT_SA_INFO_QUEUE_COUNT is
433 * the base position of the unprocessed
434 * signals in the ringbuffer. */
435 uint32_t ofs = (counter.seen + j)
436 % TEVENT_SA_INFO_QUEUE_COUNT;
437 se->handler(ev, se, i, 1,
438 (void*)&sig_state->sig_info[i][ofs],
439 se->private_data);
440 if (!exists) {
441 break;
444 #ifdef SA_RESETHAND
445 if (exists && (se->sa_flags & SA_RESETHAND)) {
446 talloc_free(se);
448 #endif
449 talloc_free(exists);
450 continue;
452 #endif
453 se->handler(ev, se, i, count, NULL, se->private_data);
454 #ifdef SA_RESETHAND
455 if (exists && (se->sa_flags & SA_RESETHAND)) {
456 talloc_free(se);
458 #endif
459 talloc_free(exists);
462 #ifdef SA_SIGINFO
463 if (clear_processed_siginfo && sig_state->sig_info[i] != NULL) {
464 uint32_t j;
465 for (j=0;j<count;j++) {
466 uint32_t ofs = (counter.seen + j)
467 % TEVENT_SA_INFO_QUEUE_COUNT;
468 memset((void*)&sig_state->sig_info[i][ofs],
469 '\0',
470 sizeof(siginfo_t));
473 #endif
475 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
476 TEVENT_SIG_SEEN(sig_state->got_signal, count);
478 #ifdef SA_SIGINFO
479 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
480 /* We'd filled the queue, unblock the
481 signal now the queue is empty again.
482 Note we MUST do this after the
483 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
484 call to prevent a new signal running
485 out of room in the sig_state->sig_info[i][]
486 ring buffer. */
487 sigset_t set;
488 sigemptyset(&set);
489 sigaddset(&set, i);
490 TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
491 tevent_sig_count(sig_state->sig_blocked[i]));
492 sigprocmask(SIG_UNBLOCK, &set, NULL);
494 #endif
497 return 1;
500 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
502 struct tevent_common_signal_list *sl =
503 talloc_get_type_abort(se->additional_data,
504 struct tevent_common_signal_list);
506 tevent_common_signal_list_destructor(sl);
508 if (sig_state->sig_handlers[se->signum] == NULL) {
509 if (sig_state->oldact[se->signum]) {
510 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
511 talloc_free(sig_state->oldact[se->signum]);
512 sig_state->oldact[se->signum] = NULL;
515 return;