s3:libsmb: Plumb cli_smb2_dskattr() inside cli_dskattr().
[Samba.git] / lib / tevent / tevent_signal.c
blob8e13d7351f8e5a42869ae9531239f629613a6e31
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 64
40 struct tevent_sigcounter {
41 uint32_t count;
42 uint32_t seen;
45 #define TEVENT_SIG_INCREMENT(s) (s).count++
46 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
47 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
49 struct tevent_common_signal_list {
50 struct tevent_common_signal_list *prev, *next;
51 struct tevent_signal *se;
55 the poor design of signals means that this table must be static global
57 static struct tevent_sig_state {
58 struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
59 struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
60 struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
61 struct tevent_sigcounter got_signal;
62 #ifdef SA_SIGINFO
63 /* with SA_SIGINFO we get quite a lot of info per signal */
64 siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
65 struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
66 #endif
67 } *sig_state;
70 return number of sigcounter events not processed yet
72 static uint32_t tevent_sig_count(struct tevent_sigcounter s)
74 return s.count - s.seen;
78 signal handler - redirects to registered signals
80 static void tevent_common_signal_handler(int signum)
82 char c = 0;
83 struct tevent_common_signal_list *sl;
84 struct tevent_context *ev = NULL;
85 int saved_errno = errno;
87 TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
88 TEVENT_SIG_INCREMENT(sig_state->got_signal);
90 /* Write to each unique event context. */
91 for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
92 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
93 ev = sl->se->event_ctx;
94 /* doesn't matter if this pipe overflows */
95 (void) write(ev->pipe_fds[1], &c, 1);
99 errno = saved_errno;
102 #ifdef SA_SIGINFO
104 signal handler with SA_SIGINFO - redirects to registered signals
106 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
107 void *uctx)
109 uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
110 /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
111 * is the base of the unprocessed signals in the ringbuffer. */
112 uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
113 TEVENT_SA_INFO_QUEUE_COUNT;
114 sig_state->sig_info[signum][ofs] = *info;
116 tevent_common_signal_handler(signum);
118 /* handle SA_SIGINFO */
119 if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
120 /* we've filled the info array - block this signal until
121 these ones are delivered */
122 #ifdef HAVE_UCONTEXT_T
124 * This is the only way for this to work.
125 * By default signum is blocked inside this
126 * signal handler using a temporary mask,
127 * but what we really need to do now is
128 * block it in the callers mask, so it
129 * stays blocked when the temporary signal
130 * handler mask is replaced when we return
131 * from here. The callers mask can be found
132 * in the ucontext_t passed in as the
133 * void *uctx argument.
135 ucontext_t *ucp = (ucontext_t *)uctx;
136 sigaddset(&ucp->uc_sigmask, signum);
137 #else
139 * WARNING !!! WARNING !!!!
141 * This code doesn't work.
142 * By default signum is blocked inside this
143 * signal handler, but calling sigprocmask
144 * modifies the temporary signal mask being
145 * used *inside* this handler, which will be
146 * replaced by the callers signal mask once
147 * we return from here. See Samba
148 * bug #9550 for details.
150 sigset_t set;
151 sigemptyset(&set);
152 sigaddset(&set, signum);
153 sigprocmask(SIG_BLOCK, &set, NULL);
154 #endif
155 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
158 #endif
160 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
162 if (sig_state->sig_handlers[sl->se->signum]) {
163 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
165 return 0;
169 destroy a signal event
171 static int tevent_signal_destructor(struct tevent_signal *se)
173 struct tevent_common_signal_list *sl;
174 sl = talloc_get_type(se->additional_data,
175 struct tevent_common_signal_list);
177 if (se->event_ctx) {
178 struct tevent_context *ev = se->event_ctx;
180 DLIST_REMOVE(ev->signal_events, se);
182 if (ev->signal_events == NULL && ev->pipe_fde != NULL) {
184 * This was the last signal. Destroy the pipe.
186 TALLOC_FREE(ev->pipe_fde);
188 close(ev->pipe_fds[0]);
189 close(ev->pipe_fds[1]);
193 talloc_free(sl);
195 if (sig_state->sig_handlers[se->signum] == NULL) {
196 /* restore old handler, if any */
197 if (sig_state->oldact[se->signum]) {
198 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
199 sig_state->oldact[se->signum] = NULL;
201 #ifdef SA_SIGINFO
202 if (se->sa_flags & SA_SIGINFO) {
203 if (sig_state->sig_info[se->signum]) {
204 talloc_free(sig_state->sig_info[se->signum]);
205 sig_state->sig_info[se->signum] = NULL;
208 #endif
211 return 0;
215 this is part of the pipe hack needed to avoid the signal race condition
217 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
218 uint16_t flags, void *_private)
220 char c[16];
221 /* its non-blocking, doesn't matter if we read too much */
222 (void) read(fde->fd, c, sizeof(c));
226 add a signal event
227 return NULL on failure (memory allocation error)
229 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
230 TALLOC_CTX *mem_ctx,
231 int signum,
232 int sa_flags,
233 tevent_signal_handler_t handler,
234 void *private_data,
235 const char *handler_name,
236 const char *location)
238 struct tevent_signal *se;
239 struct tevent_common_signal_list *sl;
240 sigset_t set, oldset;
242 if (signum >= TEVENT_NUM_SIGNALS) {
243 errno = EINVAL;
244 return NULL;
247 /* the sig_state needs to be on a global context as it can last across
248 multiple event contexts */
249 if (sig_state == NULL) {
250 sig_state = talloc_zero(NULL, struct tevent_sig_state);
251 if (sig_state == NULL) {
252 return NULL;
256 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
257 if (se == NULL) return NULL;
259 se->event_ctx = ev;
260 se->signum = signum;
261 se->sa_flags = sa_flags;
262 se->handler = handler;
263 se->private_data = private_data;
264 se->handler_name = handler_name;
265 se->location = location;
266 se->additional_data = NULL;
268 sl = talloc(se, struct tevent_common_signal_list);
269 if (!sl) {
270 talloc_free(se);
271 return NULL;
273 sl->se = se;
274 se->additional_data = sl;
276 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
277 if (!talloc_reference(se, sig_state)) {
278 talloc_free(se);
279 return NULL;
282 /* we need to setup the pipe hack handler if not already
283 setup */
284 if (ev->pipe_fde == NULL) {
285 if (pipe(ev->pipe_fds) == -1) {
286 talloc_free(se);
287 return NULL;
289 ev_set_blocking(ev->pipe_fds[0], false);
290 ev_set_blocking(ev->pipe_fds[1], false);
291 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
292 TEVENT_FD_READ,
293 signal_pipe_handler, NULL);
294 if (!ev->pipe_fde) {
295 close(ev->pipe_fds[0]);
296 close(ev->pipe_fds[1]);
297 talloc_free(se);
298 return NULL;
302 /* only install a signal handler if not already installed */
303 if (sig_state->sig_handlers[signum] == NULL) {
304 struct sigaction act;
305 ZERO_STRUCT(act);
306 act.sa_handler = tevent_common_signal_handler;
307 act.sa_flags = sa_flags;
308 #ifdef SA_SIGINFO
309 if (sa_flags & SA_SIGINFO) {
310 act.sa_handler = NULL;
311 act.sa_sigaction = tevent_common_signal_handler_info;
312 if (sig_state->sig_info[signum] == NULL) {
313 sig_state->sig_info[signum] =
314 talloc_zero_array(sig_state, siginfo_t,
315 TEVENT_SA_INFO_QUEUE_COUNT);
316 if (sig_state->sig_info[signum] == NULL) {
317 talloc_free(se);
318 return NULL;
322 #endif
323 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
324 if (sig_state->oldact[signum] == NULL) {
325 talloc_free(se);
326 return NULL;
328 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
329 talloc_free(se);
330 return NULL;
334 DLIST_ADD(se->event_ctx->signal_events, se);
336 /* Make sure the signal doesn't come in while we're mangling list. */
337 sigemptyset(&set);
338 sigaddset(&set, signum);
339 sigprocmask(SIG_BLOCK, &set, &oldset);
340 DLIST_ADD(sig_state->sig_handlers[signum], sl);
341 sigprocmask(SIG_SETMASK, &oldset, NULL);
343 talloc_set_destructor(se, tevent_signal_destructor);
344 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
346 return se;
349 struct tevent_se_exists {
350 struct tevent_se_exists **myself;
353 static int tevent_se_exists_destructor(struct tevent_se_exists *s)
355 *s->myself = NULL;
356 return 0;
360 check if a signal is pending
361 return != 0 if a signal was pending
363 int tevent_common_check_signal(struct tevent_context *ev)
365 int i;
367 if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
368 return 0;
371 for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
372 struct tevent_common_signal_list *sl, *next;
373 struct tevent_sigcounter counter = sig_state->signal_count[i];
374 uint32_t count = tevent_sig_count(counter);
375 #ifdef SA_SIGINFO
376 /* Ensure we null out any stored siginfo_t entries
377 * after processing for debugging purposes. */
378 bool clear_processed_siginfo = false;
379 #endif
381 if (count == 0) {
382 continue;
384 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
385 struct tevent_signal *se = sl->se;
386 struct tevent_se_exists *exists;
388 next = sl->next;
391 * We have to be careful to not touch "se"
392 * after it was deleted in its handler. Thus
393 * we allocate a child whose destructor will
394 * tell by nulling out itself that its parent
395 * is gone.
397 exists = talloc(se, struct tevent_se_exists);
398 if (exists == NULL) {
399 continue;
401 exists->myself = &exists;
402 talloc_set_destructor(
403 exists, tevent_se_exists_destructor);
405 #ifdef SA_SIGINFO
406 if (se->sa_flags & SA_SIGINFO) {
407 uint32_t j;
409 clear_processed_siginfo = true;
411 for (j=0;j<count;j++) {
412 /* sig_state->signal_count[i].seen
413 * % TEVENT_SA_INFO_QUEUE_COUNT is
414 * the base position of the unprocessed
415 * signals in the ringbuffer. */
416 uint32_t ofs = (counter.seen + j)
417 % TEVENT_SA_INFO_QUEUE_COUNT;
418 se->handler(ev, se, i, 1,
419 (void*)&sig_state->sig_info[i][ofs],
420 se->private_data);
421 if (!exists) {
422 break;
425 #ifdef SA_RESETHAND
426 if (exists && (se->sa_flags & SA_RESETHAND)) {
427 talloc_free(se);
429 #endif
430 talloc_free(exists);
431 continue;
433 #endif
434 se->handler(ev, se, i, count, NULL, se->private_data);
435 #ifdef SA_RESETHAND
436 if (exists && (se->sa_flags & SA_RESETHAND)) {
437 talloc_free(se);
439 #endif
440 talloc_free(exists);
443 #ifdef SA_SIGINFO
444 if (clear_processed_siginfo) {
445 uint32_t j;
446 for (j=0;j<count;j++) {
447 uint32_t ofs = (counter.seen + j)
448 % TEVENT_SA_INFO_QUEUE_COUNT;
449 memset((void*)&sig_state->sig_info[i][ofs],
450 '\0',
451 sizeof(siginfo_t));
454 #endif
456 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
457 TEVENT_SIG_SEEN(sig_state->got_signal, count);
459 #ifdef SA_SIGINFO
460 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
461 /* We'd filled the queue, unblock the
462 signal now the queue is empty again.
463 Note we MUST do this after the
464 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
465 call to prevent a new signal running
466 out of room in the sig_state->sig_info[i][]
467 ring buffer. */
468 sigset_t set;
469 sigemptyset(&set);
470 sigaddset(&set, i);
471 TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
472 tevent_sig_count(sig_state->sig_blocked[i]));
473 sigprocmask(SIG_UNBLOCK, &set, NULL);
475 #endif
478 return 1;
481 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
483 struct tevent_common_signal_list *sl;
484 sl = talloc_get_type(se->additional_data,
485 struct tevent_common_signal_list);
487 tevent_common_signal_list_destructor(sl);
489 if (sig_state->sig_handlers[se->signum] == NULL) {
490 if (sig_state->oldact[se->signum]) {
491 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
492 sig_state->oldact[se->signum] = NULL;
495 return;