server: remove duplicate snum_is_shared_printer()
[Samba.git] / lib / tevent / tevent_signal.c
blob95a099d6ab58961008038a16bf6e3856068e53ab
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 #if defined(HAVE___SYNC_FETCH_AND_ADD)
46 #define TEVENT_SIG_INCREMENT(s) __sync_fetch_and_add(&((s).count), 1)
47 #elif defined(HAVE_ATOMIC_ADD_32)
48 #define TEVENT_SIG_INCREMENT(s) atomic_add_32(&((s).count), 1)
49 #else
50 #define TEVENT_SIG_INCREMENT(s) (s).count++
51 #endif
52 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
53 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
55 struct tevent_common_signal_list {
56 struct tevent_common_signal_list *prev, *next;
57 struct tevent_signal *se;
61 the poor design of signals means that this table must be static global
63 static struct tevent_sig_state {
64 struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
65 struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
66 struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
67 struct tevent_sigcounter got_signal;
68 #ifdef SA_SIGINFO
69 /* with SA_SIGINFO we get quite a lot of info per signal */
70 siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
71 struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
72 #endif
73 } *sig_state;
76 return number of sigcounter events not processed yet
78 static uint32_t tevent_sig_count(struct tevent_sigcounter s)
80 return s.count - s.seen;
84 signal handler - redirects to registered signals
86 static void tevent_common_signal_handler(int signum)
88 char c = 0;
89 struct tevent_common_signal_list *sl;
90 struct tevent_context *ev = NULL;
91 int saved_errno = errno;
93 TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
94 TEVENT_SIG_INCREMENT(sig_state->got_signal);
96 /* Write to each unique event context. */
97 for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
98 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
99 ev = sl->se->event_ctx;
100 /* doesn't matter if this pipe overflows */
101 (void) write(ev->pipe_fds[1], &c, 1);
105 errno = saved_errno;
108 #ifdef SA_SIGINFO
110 signal handler with SA_SIGINFO - redirects to registered signals
112 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
113 void *uctx)
115 uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
116 /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
117 * is the base of the unprocessed signals in the ringbuffer. */
118 uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
119 TEVENT_SA_INFO_QUEUE_COUNT;
120 sig_state->sig_info[signum][ofs] = *info;
122 tevent_common_signal_handler(signum);
124 /* handle SA_SIGINFO */
125 if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
126 /* we've filled the info array - block this signal until
127 these ones are delivered */
128 #ifdef HAVE_UCONTEXT_T
130 * This is the only way for this to work.
131 * By default signum is blocked inside this
132 * signal handler using a temporary mask,
133 * but what we really need to do now is
134 * block it in the callers mask, so it
135 * stays blocked when the temporary signal
136 * handler mask is replaced when we return
137 * from here. The callers mask can be found
138 * in the ucontext_t passed in as the
139 * void *uctx argument.
141 ucontext_t *ucp = (ucontext_t *)uctx;
142 sigaddset(&ucp->uc_sigmask, signum);
143 #else
145 * WARNING !!! WARNING !!!!
147 * This code doesn't work.
148 * By default signum is blocked inside this
149 * signal handler, but calling sigprocmask
150 * modifies the temporary signal mask being
151 * used *inside* this handler, which will be
152 * replaced by the callers signal mask once
153 * we return from here. See Samba
154 * bug #9550 for details.
156 sigset_t set;
157 sigemptyset(&set);
158 sigaddset(&set, signum);
159 sigprocmask(SIG_BLOCK, &set, NULL);
160 #endif
161 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
164 #endif
166 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
168 if (sig_state->sig_handlers[sl->se->signum]) {
169 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
171 return 0;
175 destroy a signal event
177 static int tevent_signal_destructor(struct tevent_signal *se)
179 struct tevent_common_signal_list *sl;
180 sl = talloc_get_type(se->additional_data,
181 struct tevent_common_signal_list);
183 if (se->event_ctx) {
184 DLIST_REMOVE(se->event_ctx->signal_events, se);
187 talloc_free(sl);
189 if (sig_state->sig_handlers[se->signum] == NULL) {
190 /* restore old handler, if any */
191 if (sig_state->oldact[se->signum]) {
192 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
193 sig_state->oldact[se->signum] = NULL;
195 #ifdef SA_SIGINFO
196 if (se->sa_flags & SA_SIGINFO) {
197 if (sig_state->sig_info[se->signum]) {
198 talloc_free(sig_state->sig_info[se->signum]);
199 sig_state->sig_info[se->signum] = NULL;
202 #endif
205 return 0;
209 this is part of the pipe hack needed to avoid the signal race condition
211 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
212 uint16_t flags, void *_private)
214 char c[16];
215 /* its non-blocking, doesn't matter if we read too much */
216 (void) read(fde->fd, c, sizeof(c));
220 add a signal event
221 return NULL on failure (memory allocation error)
223 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
224 TALLOC_CTX *mem_ctx,
225 int signum,
226 int sa_flags,
227 tevent_signal_handler_t handler,
228 void *private_data,
229 const char *handler_name,
230 const char *location)
232 struct tevent_signal *se;
233 struct tevent_common_signal_list *sl;
234 sigset_t set, oldset;
236 if (signum >= TEVENT_NUM_SIGNALS) {
237 errno = EINVAL;
238 return NULL;
241 /* the sig_state needs to be on a global context as it can last across
242 multiple event contexts */
243 if (sig_state == NULL) {
244 sig_state = talloc_zero(NULL, struct tevent_sig_state);
245 if (sig_state == NULL) {
246 return NULL;
250 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
251 if (se == NULL) return NULL;
253 se->event_ctx = ev;
254 se->signum = signum;
255 se->sa_flags = sa_flags;
256 se->handler = handler;
257 se->private_data = private_data;
258 se->handler_name = handler_name;
259 se->location = location;
260 se->additional_data = NULL;
262 sl = talloc(se, struct tevent_common_signal_list);
263 if (!sl) {
264 talloc_free(se);
265 return NULL;
267 sl->se = se;
268 se->additional_data = sl;
270 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
271 if (!talloc_reference(se, sig_state)) {
272 talloc_free(se);
273 return NULL;
276 /* we need to setup the pipe hack handler if not already
277 setup */
278 if (ev->pipe_fde == NULL) {
279 if (pipe(ev->pipe_fds) == -1) {
280 talloc_free(se);
281 return NULL;
283 ev_set_blocking(ev->pipe_fds[0], false);
284 ev_set_blocking(ev->pipe_fds[1], false);
285 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
286 TEVENT_FD_READ,
287 signal_pipe_handler, NULL);
288 if (!ev->pipe_fde) {
289 close(ev->pipe_fds[0]);
290 close(ev->pipe_fds[1]);
291 talloc_free(se);
292 return NULL;
296 /* only install a signal handler if not already installed */
297 if (sig_state->sig_handlers[signum] == NULL) {
298 struct sigaction act;
299 ZERO_STRUCT(act);
300 act.sa_handler = tevent_common_signal_handler;
301 act.sa_flags = sa_flags;
302 #ifdef SA_SIGINFO
303 if (sa_flags & SA_SIGINFO) {
304 act.sa_handler = NULL;
305 act.sa_sigaction = tevent_common_signal_handler_info;
306 if (sig_state->sig_info[signum] == NULL) {
307 sig_state->sig_info[signum] =
308 talloc_zero_array(sig_state, siginfo_t,
309 TEVENT_SA_INFO_QUEUE_COUNT);
310 if (sig_state->sig_info[signum] == NULL) {
311 talloc_free(se);
312 return NULL;
316 #endif
317 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
318 if (sig_state->oldact[signum] == NULL) {
319 talloc_free(se);
320 return NULL;
322 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
323 talloc_free(se);
324 return NULL;
328 DLIST_ADD(se->event_ctx->signal_events, se);
330 /* Make sure the signal doesn't come in while we're mangling list. */
331 sigemptyset(&set);
332 sigaddset(&set, signum);
333 sigprocmask(SIG_BLOCK, &set, &oldset);
334 DLIST_ADD(sig_state->sig_handlers[signum], sl);
335 sigprocmask(SIG_SETMASK, &oldset, NULL);
337 talloc_set_destructor(se, tevent_signal_destructor);
338 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
340 return se;
343 struct tevent_se_exists {
344 struct tevent_se_exists **myself;
347 static int tevent_se_exists_destructor(struct tevent_se_exists *s)
349 *s->myself = NULL;
350 return 0;
354 check if a signal is pending
355 return != 0 if a signal was pending
357 int tevent_common_check_signal(struct tevent_context *ev)
359 int i;
361 if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
362 return 0;
365 for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
366 struct tevent_common_signal_list *sl, *next;
367 struct tevent_sigcounter counter = sig_state->signal_count[i];
368 uint32_t count = tevent_sig_count(counter);
369 #ifdef SA_SIGINFO
370 /* Ensure we null out any stored siginfo_t entries
371 * after processing for debugging purposes. */
372 bool clear_processed_siginfo = false;
373 #endif
375 if (count == 0) {
376 continue;
378 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
379 struct tevent_signal *se = sl->se;
380 struct tevent_se_exists *exists;
382 next = sl->next;
385 * We have to be careful to not touch "se"
386 * after it was deleted in its handler. Thus
387 * we allocate a child whose destructor will
388 * tell by nulling out itself that its parent
389 * is gone.
391 exists = talloc(se, struct tevent_se_exists);
392 if (exists == NULL) {
393 continue;
395 exists->myself = &exists;
396 talloc_set_destructor(
397 exists, tevent_se_exists_destructor);
399 #ifdef SA_SIGINFO
400 if (se->sa_flags & SA_SIGINFO) {
401 uint32_t j;
403 clear_processed_siginfo = true;
405 for (j=0;j<count;j++) {
406 /* sig_state->signal_count[i].seen
407 * % TEVENT_SA_INFO_QUEUE_COUNT is
408 * the base position of the unprocessed
409 * signals in the ringbuffer. */
410 uint32_t ofs = (counter.seen + j)
411 % TEVENT_SA_INFO_QUEUE_COUNT;
412 se->handler(ev, se, i, 1,
413 (void*)&sig_state->sig_info[i][ofs],
414 se->private_data);
415 if (!exists) {
416 break;
419 #ifdef SA_RESETHAND
420 if (exists && (se->sa_flags & SA_RESETHAND)) {
421 talloc_free(se);
423 #endif
424 talloc_free(exists);
425 continue;
427 #endif
428 se->handler(ev, se, i, count, NULL, se->private_data);
429 #ifdef SA_RESETHAND
430 if (exists && (se->sa_flags & SA_RESETHAND)) {
431 talloc_free(se);
433 #endif
434 talloc_free(exists);
437 #ifdef SA_SIGINFO
438 if (clear_processed_siginfo) {
439 uint32_t j;
440 for (j=0;j<count;j++) {
441 uint32_t ofs = (counter.seen + j)
442 % TEVENT_SA_INFO_QUEUE_COUNT;
443 memset((void*)&sig_state->sig_info[i][ofs],
444 '\0',
445 sizeof(siginfo_t));
448 #endif
450 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
451 TEVENT_SIG_SEEN(sig_state->got_signal, count);
453 #ifdef SA_SIGINFO
454 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
455 /* We'd filled the queue, unblock the
456 signal now the queue is empty again.
457 Note we MUST do this after the
458 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
459 call to prevent a new signal running
460 out of room in the sig_state->sig_info[i][]
461 ring buffer. */
462 sigset_t set;
463 sigemptyset(&set);
464 sigaddset(&set, i);
465 TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
466 tevent_sig_count(sig_state->sig_blocked[i]));
467 sigprocmask(SIG_UNBLOCK, &set, NULL);
469 #endif
472 return 1;
475 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
477 struct tevent_common_signal_list *sl;
478 sl = talloc_get_type(se->additional_data,
479 struct tevent_common_signal_list);
481 tevent_common_signal_list_destructor(sl);
483 if (sig_state->sig_handlers[se->signum] == NULL) {
484 if (sig_state->oldact[se->signum]) {
485 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
486 sig_state->oldact[se->signum] = NULL;
489 return;