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
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/>.
27 #include "system/filesys.h"
28 #include "system/wait.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>
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
{
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)
60 #define TEVENT_SIG_INCREMENT(s) (s).count++
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
;
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];
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
)
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
) {
111 ev
= sl
->se
->event_ctx
;
112 /* doesn't matter if this pipe overflows */
114 ret
= write(ev
->pipe_fds
[1], &c
, 1);
115 } while (ret
== -1 && errno
== EINTR
);
124 signal handler with SA_SIGINFO - redirects to registered signals
126 static void tevent_common_signal_handler_info(int signum
, siginfo_t
*info
,
129 uint32_t count
= tevent_sig_count(sig_state
->signal_count
[signum
]);
130 /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
131 * is the base of the unprocessed signals in the ringbuffer. */
132 uint32_t ofs
= (sig_state
->signal_count
[signum
].seen
+ count
) %
133 TEVENT_SA_INFO_QUEUE_COUNT
;
134 sig_state
->sig_info
[signum
][ofs
] = *info
;
136 tevent_common_signal_handler(signum
);
138 /* handle SA_SIGINFO */
139 if (count
+1 == TEVENT_SA_INFO_QUEUE_COUNT
) {
140 /* we've filled the info array - block this signal until
141 these ones are delivered */
142 #ifdef HAVE_UCONTEXT_T
144 * This is the only way for this to work.
145 * By default signum is blocked inside this
146 * signal handler using a temporary mask,
147 * but what we really need to do now is
148 * block it in the callers mask, so it
149 * stays blocked when the temporary signal
150 * handler mask is replaced when we return
151 * from here. The callers mask can be found
152 * in the ucontext_t passed in as the
153 * void *uctx argument.
155 ucontext_t
*ucp
= (ucontext_t
*)uctx
;
156 sigaddset(&ucp
->uc_sigmask
, signum
);
159 * WARNING !!! WARNING !!!!
161 * This code doesn't work.
162 * By default signum is blocked inside this
163 * signal handler, but calling sigprocmask
164 * modifies the temporary signal mask being
165 * used *inside* this handler, which will be
166 * replaced by the callers signal mask once
167 * we return from here. See Samba
168 * bug #9550 for details.
172 sigaddset(&set
, signum
);
173 sigprocmask(SIG_BLOCK
, &set
, NULL
);
175 TEVENT_SIG_INCREMENT(sig_state
->sig_blocked
[signum
]);
180 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list
*sl
)
182 if (sig_state
->sig_handlers
[sl
->se
->signum
]) {
183 DLIST_REMOVE(sig_state
->sig_handlers
[sl
->se
->signum
], sl
);
189 destroy a signal event
191 static int tevent_signal_destructor(struct tevent_signal
*se
)
193 struct tevent_common_signal_list
*sl
=
194 talloc_get_type_abort(se
->additional_data
,
195 struct tevent_common_signal_list
);
198 struct tevent_context
*ev
= se
->event_ctx
;
200 DLIST_REMOVE(ev
->signal_events
, se
);
202 if (ev
->signal_events
== NULL
&& ev
->pipe_fde
!= NULL
) {
204 * This was the last signal. Destroy the pipe.
206 TALLOC_FREE(ev
->pipe_fde
);
208 close(ev
->pipe_fds
[0]);
209 close(ev
->pipe_fds
[1]);
215 if (sig_state
->sig_handlers
[se
->signum
] == NULL
) {
216 /* restore old handler, if any */
217 if (sig_state
->oldact
[se
->signum
]) {
218 sigaction(se
->signum
, sig_state
->oldact
[se
->signum
], NULL
);
219 talloc_free(sig_state
->oldact
[se
->signum
]);
220 sig_state
->oldact
[se
->signum
] = NULL
;
223 if (se
->sa_flags
& SA_SIGINFO
) {
224 if (sig_state
->sig_info
[se
->signum
]) {
225 talloc_free(sig_state
->sig_info
[se
->signum
]);
226 sig_state
->sig_info
[se
->signum
] = NULL
;
236 this is part of the pipe hack needed to avoid the signal race condition
238 static void signal_pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
239 uint16_t flags
, void *_private
)
244 /* its non-blocking, doesn't matter if we read too much */
246 ret
= read(fde
->fd
, c
, sizeof(c
));
247 } while (ret
== -1 && errno
== EINTR
);
252 return NULL on failure (memory allocation error)
254 struct tevent_signal
*tevent_common_add_signal(struct tevent_context
*ev
,
258 tevent_signal_handler_t handler
,
260 const char *handler_name
,
261 const char *location
)
263 struct tevent_signal
*se
;
264 struct tevent_common_signal_list
*sl
;
265 sigset_t set
, oldset
;
267 if (signum
>= TEVENT_NUM_SIGNALS
) {
272 /* the sig_state needs to be on a global context as it can last across
273 multiple event contexts */
274 if (sig_state
== NULL
) {
275 sig_state
= talloc_zero(NULL
, struct tevent_sig_state
);
276 if (sig_state
== NULL
) {
281 se
= talloc(mem_ctx
?mem_ctx
:ev
, struct tevent_signal
);
282 if (se
== NULL
) return NULL
;
286 se
->sa_flags
= sa_flags
;
287 se
->handler
= handler
;
288 se
->private_data
= private_data
;
289 se
->handler_name
= handler_name
;
290 se
->location
= location
;
291 se
->additional_data
= NULL
;
293 sl
= talloc(se
, struct tevent_common_signal_list
);
299 se
->additional_data
= sl
;
301 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
302 if (!talloc_reference(se
, sig_state
)) {
307 /* we need to setup the pipe hack handler if not already
309 if (ev
->pipe_fde
== NULL
) {
310 if (pipe(ev
->pipe_fds
) == -1) {
314 ev_set_blocking(ev
->pipe_fds
[0], false);
315 ev_set_blocking(ev
->pipe_fds
[1], false);
316 ev
->pipe_fde
= tevent_add_fd(ev
, ev
, ev
->pipe_fds
[0],
318 signal_pipe_handler
, NULL
);
320 close(ev
->pipe_fds
[0]);
321 close(ev
->pipe_fds
[1]);
327 /* only install a signal handler if not already installed */
328 if (sig_state
->sig_handlers
[signum
] == NULL
) {
329 struct sigaction act
;
331 act
.sa_handler
= tevent_common_signal_handler
;
332 act
.sa_flags
= sa_flags
;
334 if (sa_flags
& SA_SIGINFO
) {
335 act
.sa_handler
= NULL
;
336 act
.sa_sigaction
= tevent_common_signal_handler_info
;
337 if (sig_state
->sig_info
[signum
] == NULL
) {
338 sig_state
->sig_info
[signum
] =
339 talloc_zero_array(sig_state
, siginfo_t
,
340 TEVENT_SA_INFO_QUEUE_COUNT
);
341 if (sig_state
->sig_info
[signum
] == NULL
) {
348 sig_state
->oldact
[signum
] = talloc(sig_state
, struct sigaction
);
349 if (sig_state
->oldact
[signum
] == NULL
) {
353 if (sigaction(signum
, &act
, sig_state
->oldact
[signum
]) == -1) {
354 talloc_free(sig_state
->oldact
[signum
]);
355 sig_state
->oldact
[signum
] = NULL
;
361 DLIST_ADD(se
->event_ctx
->signal_events
, se
);
363 /* Make sure the signal doesn't come in while we're mangling list. */
365 sigaddset(&set
, signum
);
366 sigprocmask(SIG_BLOCK
, &set
, &oldset
);
367 DLIST_ADD(sig_state
->sig_handlers
[signum
], sl
);
368 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
370 talloc_set_destructor(se
, tevent_signal_destructor
);
371 talloc_set_destructor(sl
, tevent_common_signal_list_destructor
);
376 struct tevent_se_exists
{
377 struct tevent_se_exists
**myself
;
380 static int tevent_se_exists_destructor(struct tevent_se_exists
*s
)
387 check if a signal is pending
388 return != 0 if a signal was pending
390 int tevent_common_check_signal(struct tevent_context
*ev
)
394 if (!sig_state
|| !TEVENT_SIG_PENDING(sig_state
->got_signal
)) {
398 for (i
=0;i
<TEVENT_NUM_SIGNALS
+1;i
++) {
399 struct tevent_common_signal_list
*sl
, *next
;
400 struct tevent_sigcounter counter
= sig_state
->signal_count
[i
];
401 uint32_t count
= tevent_sig_count(counter
);
403 /* Ensure we null out any stored siginfo_t entries
404 * after processing for debugging purposes. */
405 bool clear_processed_siginfo
= false;
411 for (sl
=sig_state
->sig_handlers
[i
];sl
;sl
=next
) {
412 struct tevent_signal
*se
= sl
->se
;
413 struct tevent_se_exists
*exists
;
418 * We have to be careful to not touch "se"
419 * after it was deleted in its handler. Thus
420 * we allocate a child whose destructor will
421 * tell by nulling out itself that its parent
424 exists
= talloc(se
, struct tevent_se_exists
);
425 if (exists
== NULL
) {
428 exists
->myself
= &exists
;
429 talloc_set_destructor(
430 exists
, tevent_se_exists_destructor
);
433 if (se
->sa_flags
& SA_SIGINFO
) {
436 clear_processed_siginfo
= true;
438 for (j
=0;j
<count
;j
++) {
439 /* sig_state->signal_count[i].seen
440 * % TEVENT_SA_INFO_QUEUE_COUNT is
441 * the base position of the unprocessed
442 * signals in the ringbuffer. */
443 uint32_t ofs
= (counter
.seen
+ j
)
444 % TEVENT_SA_INFO_QUEUE_COUNT
;
445 se
->handler(ev
, se
, i
, 1,
446 (void*)&sig_state
->sig_info
[i
][ofs
],
453 if (exists
&& (se
->sa_flags
& SA_RESETHAND
)) {
461 se
->handler(ev
, se
, i
, count
, NULL
, se
->private_data
);
463 if (exists
&& (se
->sa_flags
& SA_RESETHAND
)) {
471 if (clear_processed_siginfo
&& sig_state
->sig_info
[i
] != NULL
) {
473 for (j
=0;j
<count
;j
++) {
474 uint32_t ofs
= (counter
.seen
+ j
)
475 % TEVENT_SA_INFO_QUEUE_COUNT
;
476 memset((void*)&sig_state
->sig_info
[i
][ofs
],
483 TEVENT_SIG_SEEN(sig_state
->signal_count
[i
], count
);
484 TEVENT_SIG_SEEN(sig_state
->got_signal
, count
);
487 if (TEVENT_SIG_PENDING(sig_state
->sig_blocked
[i
])) {
488 /* We'd filled the queue, unblock the
489 signal now the queue is empty again.
490 Note we MUST do this after the
491 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
492 call to prevent a new signal running
493 out of room in the sig_state->sig_info[i][]
498 TEVENT_SIG_SEEN(sig_state
->sig_blocked
[i
],
499 tevent_sig_count(sig_state
->sig_blocked
[i
]));
500 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
508 void tevent_cleanup_pending_signal_handlers(struct tevent_signal
*se
)
510 struct tevent_common_signal_list
*sl
=
511 talloc_get_type_abort(se
->additional_data
,
512 struct tevent_common_signal_list
);
514 tevent_common_signal_list_destructor(sl
);
516 if (sig_state
->sig_handlers
[se
->signum
] == NULL
) {
517 if (sig_state
->oldact
[se
->signum
]) {
518 sigaction(se
->signum
, sig_state
->oldact
[se
->signum
], NULL
);
519 talloc_free(sig_state
->oldact
[se
->signum
]);
520 sig_state
->oldact
[se
->signum
] = NULL
;