tevent: use struct initializers for tevent_signal
[Samba.git] / lib / tevent / tevent_signal.c
blob1283aab4821159704150469e04bbde73be095f1f
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 struct tevent_common_signal_list *sl;
99 struct tevent_context *ev = NULL;
100 int saved_errno = errno;
102 TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
103 TEVENT_SIG_INCREMENT(sig_state->got_signal);
105 /* Write to each unique event context. */
106 for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
107 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
108 ev = sl->se->event_ctx;
109 tevent_common_wakeup(ev);
113 errno = saved_errno;
116 #ifdef SA_SIGINFO
118 signal handler with SA_SIGINFO - redirects to registered signals
120 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
121 void *uctx)
123 uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
124 /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
125 * is the base of the unprocessed signals in the ringbuffer. */
126 uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
127 TEVENT_SA_INFO_QUEUE_COUNT;
128 sig_state->sig_info[signum][ofs] = *info;
130 tevent_common_signal_handler(signum);
132 /* handle SA_SIGINFO */
133 if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
134 /* we've filled the info array - block this signal until
135 these ones are delivered */
136 #ifdef HAVE_UCONTEXT_T
138 * This is the only way for this to work.
139 * By default signum is blocked inside this
140 * signal handler using a temporary mask,
141 * but what we really need to do now is
142 * block it in the callers mask, so it
143 * stays blocked when the temporary signal
144 * handler mask is replaced when we return
145 * from here. The callers mask can be found
146 * in the ucontext_t passed in as the
147 * void *uctx argument.
149 ucontext_t *ucp = (ucontext_t *)uctx;
150 sigaddset(&ucp->uc_sigmask, signum);
151 #else
153 * WARNING !!! WARNING !!!!
155 * This code doesn't work.
156 * By default signum is blocked inside this
157 * signal handler, but calling sigprocmask
158 * modifies the temporary signal mask being
159 * used *inside* this handler, which will be
160 * replaced by the callers signal mask once
161 * we return from here. See Samba
162 * bug #9550 for details.
164 sigset_t set;
165 sigemptyset(&set);
166 sigaddset(&set, signum);
167 sigprocmask(SIG_BLOCK, &set, NULL);
168 #endif
169 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
172 #endif
174 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
176 if (sig_state->sig_handlers[sl->se->signum]) {
177 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
179 return 0;
183 destroy a signal event
185 static int tevent_signal_destructor(struct tevent_signal *se)
187 struct tevent_common_signal_list *sl =
188 talloc_get_type_abort(se->additional_data,
189 struct tevent_common_signal_list);
191 if (se->event_ctx) {
192 struct tevent_context *ev = se->event_ctx;
194 DLIST_REMOVE(ev->signal_events, se);
197 talloc_free(sl);
199 if (sig_state->sig_handlers[se->signum] == NULL) {
200 /* restore old handler, if any */
201 if (sig_state->oldact[se->signum]) {
202 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
203 talloc_free(sig_state->oldact[se->signum]);
204 sig_state->oldact[se->signum] = NULL;
206 #ifdef SA_SIGINFO
207 if (se->sa_flags & SA_SIGINFO) {
208 if (sig_state->sig_info[se->signum]) {
209 talloc_free(sig_state->sig_info[se->signum]);
210 sig_state->sig_info[se->signum] = NULL;
213 #endif
216 return 0;
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;
235 int ret;
237 ret = tevent_common_wakeup_init(ev);
238 if (ret != 0) {
239 errno = ret;
240 return NULL;
243 if (signum >= TEVENT_NUM_SIGNALS) {
244 errno = EINVAL;
245 return NULL;
248 /* the sig_state needs to be on a global context as it can last across
249 multiple event contexts */
250 if (sig_state == NULL) {
251 sig_state = talloc_zero(NULL, struct tevent_sig_state);
252 if (sig_state == NULL) {
253 return NULL;
257 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
258 if (se == NULL) return NULL;
260 sl = talloc(se, struct tevent_common_signal_list);
261 if (!sl) {
262 talloc_free(se);
263 return NULL;
265 sl->se = se;
267 *se = (struct tevent_signal) {
268 .event_ctx = ev,
269 .signum = signum,
270 .sa_flags = sa_flags,
271 .handler = handler,
272 .private_data = private_data,
273 .handler_name = handler_name,
274 .location = location,
275 .additional_data= sl,
278 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
279 if (!talloc_reference(se, sig_state)) {
280 talloc_free(se);
281 return NULL;
284 /* only install a signal handler if not already installed */
285 if (sig_state->sig_handlers[signum] == NULL) {
286 struct sigaction act;
287 ZERO_STRUCT(act);
288 act.sa_handler = tevent_common_signal_handler;
289 act.sa_flags = sa_flags;
290 #ifdef SA_SIGINFO
291 if (sa_flags & SA_SIGINFO) {
292 act.sa_handler = NULL;
293 act.sa_sigaction = tevent_common_signal_handler_info;
294 if (sig_state->sig_info[signum] == NULL) {
295 sig_state->sig_info[signum] =
296 talloc_zero_array(sig_state, siginfo_t,
297 TEVENT_SA_INFO_QUEUE_COUNT);
298 if (sig_state->sig_info[signum] == NULL) {
299 talloc_free(se);
300 return NULL;
304 #endif
305 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
306 if (sig_state->oldact[signum] == NULL) {
307 talloc_free(se);
308 return NULL;
310 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
311 talloc_free(sig_state->oldact[signum]);
312 sig_state->oldact[signum] = NULL;
313 talloc_free(se);
314 return NULL;
318 DLIST_ADD(se->event_ctx->signal_events, se);
320 /* Make sure the signal doesn't come in while we're mangling list. */
321 sigemptyset(&set);
322 sigaddset(&set, signum);
323 sigprocmask(SIG_BLOCK, &set, &oldset);
324 DLIST_ADD(sig_state->sig_handlers[signum], sl);
325 sigprocmask(SIG_SETMASK, &oldset, NULL);
327 talloc_set_destructor(se, tevent_signal_destructor);
328 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
330 return se;
333 struct tevent_se_exists {
334 struct tevent_se_exists **myself;
337 static int tevent_se_exists_destructor(struct tevent_se_exists *s)
339 *s->myself = NULL;
340 return 0;
344 check if a signal is pending
345 return != 0 if a signal was pending
347 int tevent_common_check_signal(struct tevent_context *ev)
349 int i;
351 if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
352 return 0;
355 for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
356 struct tevent_common_signal_list *sl, *next;
357 struct tevent_sigcounter counter = sig_state->signal_count[i];
358 uint32_t count = tevent_sig_count(counter);
359 #ifdef SA_SIGINFO
360 /* Ensure we null out any stored siginfo_t entries
361 * after processing for debugging purposes. */
362 bool clear_processed_siginfo = false;
363 #endif
365 if (count == 0) {
366 continue;
368 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
369 struct tevent_signal *se = sl->se;
370 struct tevent_se_exists *exists;
372 next = sl->next;
375 * We have to be careful to not touch "se"
376 * after it was deleted in its handler. Thus
377 * we allocate a child whose destructor will
378 * tell by nulling out itself that its parent
379 * is gone.
381 exists = talloc(se, struct tevent_se_exists);
382 if (exists == NULL) {
383 continue;
385 exists->myself = &exists;
386 talloc_set_destructor(
387 exists, tevent_se_exists_destructor);
389 #ifdef SA_SIGINFO
390 if (se->sa_flags & SA_SIGINFO) {
391 uint32_t j;
393 clear_processed_siginfo = true;
395 for (j=0;j<count;j++) {
396 /* sig_state->signal_count[i].seen
397 * % TEVENT_SA_INFO_QUEUE_COUNT is
398 * the base position of the unprocessed
399 * signals in the ringbuffer. */
400 uint32_t ofs = (counter.seen + j)
401 % TEVENT_SA_INFO_QUEUE_COUNT;
402 se->handler(ev, se, i, 1,
403 (void*)&sig_state->sig_info[i][ofs],
404 se->private_data);
405 if (!exists) {
406 break;
409 #ifdef SA_RESETHAND
410 if (exists && (se->sa_flags & SA_RESETHAND)) {
411 talloc_free(se);
413 #endif
414 talloc_free(exists);
415 continue;
417 #endif
418 se->handler(ev, se, i, count, NULL, se->private_data);
419 #ifdef SA_RESETHAND
420 if (exists && (se->sa_flags & SA_RESETHAND)) {
421 talloc_free(se);
423 #endif
424 talloc_free(exists);
427 #ifdef SA_SIGINFO
428 if (clear_processed_siginfo && sig_state->sig_info[i] != NULL) {
429 uint32_t j;
430 for (j=0;j<count;j++) {
431 uint32_t ofs = (counter.seen + j)
432 % TEVENT_SA_INFO_QUEUE_COUNT;
433 memset((void*)&sig_state->sig_info[i][ofs],
434 '\0',
435 sizeof(siginfo_t));
438 #endif
440 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
441 TEVENT_SIG_SEEN(sig_state->got_signal, count);
443 #ifdef SA_SIGINFO
444 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
445 /* We'd filled the queue, unblock the
446 signal now the queue is empty again.
447 Note we MUST do this after the
448 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
449 call to prevent a new signal running
450 out of room in the sig_state->sig_info[i][]
451 ring buffer. */
452 sigset_t set;
453 sigemptyset(&set);
454 sigaddset(&set, i);
455 TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
456 tevent_sig_count(sig_state->sig_blocked[i]));
457 sigprocmask(SIG_UNBLOCK, &set, NULL);
459 #endif
462 return 1;
465 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
467 struct tevent_common_signal_list *sl =
468 talloc_get_type_abort(se->additional_data,
469 struct tevent_common_signal_list);
471 tevent_common_signal_list_destructor(sl);
473 if (sig_state->sig_handlers[se->signum] == NULL) {
474 if (sig_state->oldact[se->signum]) {
475 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
476 talloc_free(sig_state->oldact[se->signum]);
477 sig_state->oldact[se->signum] = NULL;
480 return;