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 64
40 struct tevent_sigcounter
{
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
;
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];
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
)
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);
104 signal handler with SA_SIGINFO - redirects to registered signals
106 static void tevent_common_signal_handler_info(int signum
, siginfo_t
*info
,
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
);
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.
152 sigaddset(&set
, signum
);
153 sigprocmask(SIG_BLOCK
, &set
, NULL
);
155 TEVENT_SIG_INCREMENT(sig_state
->sig_blocked
[signum
]);
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
);
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
);
178 DLIST_REMOVE(se
->event_ctx
->signal_events
, se
);
183 if (sig_state
->sig_handlers
[se
->signum
] == NULL
) {
184 /* restore old handler, if any */
185 if (sig_state
->oldact
[se
->signum
]) {
186 sigaction(se
->signum
, sig_state
->oldact
[se
->signum
], NULL
);
187 sig_state
->oldact
[se
->signum
] = NULL
;
190 if (se
->sa_flags
& SA_SIGINFO
) {
191 if (sig_state
->sig_info
[se
->signum
]) {
192 talloc_free(sig_state
->sig_info
[se
->signum
]);
193 sig_state
->sig_info
[se
->signum
] = NULL
;
203 this is part of the pipe hack needed to avoid the signal race condition
205 static void signal_pipe_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
206 uint16_t flags
, void *_private
)
209 /* its non-blocking, doesn't matter if we read too much */
210 (void) read(fde
->fd
, c
, sizeof(c
));
215 return NULL on failure (memory allocation error)
217 struct tevent_signal
*tevent_common_add_signal(struct tevent_context
*ev
,
221 tevent_signal_handler_t handler
,
223 const char *handler_name
,
224 const char *location
)
226 struct tevent_signal
*se
;
227 struct tevent_common_signal_list
*sl
;
228 sigset_t set
, oldset
;
230 if (signum
>= TEVENT_NUM_SIGNALS
) {
235 /* the sig_state needs to be on a global context as it can last across
236 multiple event contexts */
237 if (sig_state
== NULL
) {
238 sig_state
= talloc_zero(NULL
, struct tevent_sig_state
);
239 if (sig_state
== NULL
) {
244 se
= talloc(mem_ctx
?mem_ctx
:ev
, struct tevent_signal
);
245 if (se
== NULL
) return NULL
;
249 se
->sa_flags
= sa_flags
;
250 se
->handler
= handler
;
251 se
->private_data
= private_data
;
252 se
->handler_name
= handler_name
;
253 se
->location
= location
;
254 se
->additional_data
= NULL
;
256 sl
= talloc(se
, struct tevent_common_signal_list
);
262 se
->additional_data
= sl
;
264 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
265 if (!talloc_reference(se
, sig_state
)) {
270 /* we need to setup the pipe hack handler if not already
272 if (ev
->pipe_fde
== NULL
) {
273 if (pipe(ev
->pipe_fds
) == -1) {
277 ev_set_blocking(ev
->pipe_fds
[0], false);
278 ev_set_blocking(ev
->pipe_fds
[1], false);
279 ev
->pipe_fde
= tevent_add_fd(ev
, ev
, ev
->pipe_fds
[0],
281 signal_pipe_handler
, NULL
);
283 close(ev
->pipe_fds
[0]);
284 close(ev
->pipe_fds
[1]);
290 /* only install a signal handler if not already installed */
291 if (sig_state
->sig_handlers
[signum
] == NULL
) {
292 struct sigaction act
;
294 act
.sa_handler
= tevent_common_signal_handler
;
295 act
.sa_flags
= sa_flags
;
297 if (sa_flags
& SA_SIGINFO
) {
298 act
.sa_handler
= NULL
;
299 act
.sa_sigaction
= tevent_common_signal_handler_info
;
300 if (sig_state
->sig_info
[signum
] == NULL
) {
301 sig_state
->sig_info
[signum
] =
302 talloc_zero_array(sig_state
, siginfo_t
,
303 TEVENT_SA_INFO_QUEUE_COUNT
);
304 if (sig_state
->sig_info
[signum
] == NULL
) {
311 sig_state
->oldact
[signum
] = talloc(sig_state
, struct sigaction
);
312 if (sig_state
->oldact
[signum
] == NULL
) {
316 if (sigaction(signum
, &act
, sig_state
->oldact
[signum
]) == -1) {
322 DLIST_ADD(se
->event_ctx
->signal_events
, se
);
324 /* Make sure the signal doesn't come in while we're mangling list. */
326 sigaddset(&set
, signum
);
327 sigprocmask(SIG_BLOCK
, &set
, &oldset
);
328 DLIST_ADD(sig_state
->sig_handlers
[signum
], sl
);
329 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
331 talloc_set_destructor(se
, tevent_signal_destructor
);
332 talloc_set_destructor(sl
, tevent_common_signal_list_destructor
);
337 struct tevent_se_exists
{
338 struct tevent_se_exists
**myself
;
341 static int tevent_se_exists_destructor(struct tevent_se_exists
*s
)
348 check if a signal is pending
349 return != 0 if a signal was pending
351 int tevent_common_check_signal(struct tevent_context
*ev
)
355 if (!sig_state
|| !TEVENT_SIG_PENDING(sig_state
->got_signal
)) {
359 for (i
=0;i
<TEVENT_NUM_SIGNALS
+1;i
++) {
360 struct tevent_common_signal_list
*sl
, *next
;
361 struct tevent_sigcounter counter
= sig_state
->signal_count
[i
];
362 uint32_t count
= tevent_sig_count(counter
);
364 /* Ensure we null out any stored siginfo_t entries
365 * after processing for debugging purposes. */
366 bool clear_processed_siginfo
= false;
372 for (sl
=sig_state
->sig_handlers
[i
];sl
;sl
=next
) {
373 struct tevent_signal
*se
= sl
->se
;
374 struct tevent_se_exists
*exists
;
379 * We have to be careful to not touch "se"
380 * after it was deleted in its handler. Thus
381 * we allocate a child whose destructor will
382 * tell by nulling out itself that its parent
385 exists
= talloc(se
, struct tevent_se_exists
);
386 if (exists
== NULL
) {
389 exists
->myself
= &exists
;
390 talloc_set_destructor(
391 exists
, tevent_se_exists_destructor
);
394 if (se
->sa_flags
& SA_SIGINFO
) {
397 clear_processed_siginfo
= true;
399 for (j
=0;j
<count
;j
++) {
400 /* sig_state->signal_count[i].seen
401 * % TEVENT_SA_INFO_QUEUE_COUNT is
402 * the base position of the unprocessed
403 * signals in the ringbuffer. */
404 uint32_t ofs
= (counter
.seen
+ j
)
405 % TEVENT_SA_INFO_QUEUE_COUNT
;
406 se
->handler(ev
, se
, i
, 1,
407 (void*)&sig_state
->sig_info
[i
][ofs
],
414 if (exists
&& (se
->sa_flags
& SA_RESETHAND
)) {
422 se
->handler(ev
, se
, i
, count
, NULL
, se
->private_data
);
424 if (exists
&& (se
->sa_flags
& SA_RESETHAND
)) {
432 if (clear_processed_siginfo
) {
434 for (j
=0;j
<count
;j
++) {
435 uint32_t ofs
= (counter
.seen
+ j
)
436 % TEVENT_SA_INFO_QUEUE_COUNT
;
437 memset((void*)&sig_state
->sig_info
[i
][ofs
],
444 TEVENT_SIG_SEEN(sig_state
->signal_count
[i
], count
);
445 TEVENT_SIG_SEEN(sig_state
->got_signal
, count
);
448 if (TEVENT_SIG_PENDING(sig_state
->sig_blocked
[i
])) {
449 /* We'd filled the queue, unblock the
450 signal now the queue is empty again.
451 Note we MUST do this after the
452 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
453 call to prevent a new signal running
454 out of room in the sig_state->sig_info[i][]
459 TEVENT_SIG_SEEN(sig_state
->sig_blocked
[i
],
460 tevent_sig_count(sig_state
->sig_blocked
[i
]));
461 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
469 void tevent_cleanup_pending_signal_handlers(struct tevent_signal
*se
)
471 struct tevent_common_signal_list
*sl
;
472 sl
= talloc_get_type(se
->additional_data
,
473 struct tevent_common_signal_list
);
475 tevent_common_signal_list_destructor(sl
);
477 if (sig_state
->sig_handlers
[se
->signum
] == NULL
) {
478 if (sig_state
->oldact
[se
->signum
]) {
479 sigaction(se
->signum
, sig_state
->oldact
[se
->signum
], NULL
);
480 sig_state
->oldact
[se
->signum
] = NULL
;