source: trunk/server/lib/tevent/tevent_signal.c

Last change on this file was 862, checked in by Silvan Scherrer, 11 years ago

Samba Server: update trunk to 3.6.23

File size: 12.1 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 common events code for signal events
5
6 Copyright (C) Andrew Tridgell 2007
7
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
11
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.
16
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.
21
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/>.
24*/
25
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"
32#ifdef __OS2__
33#define pipe(A) os2_pipe(A)
34#endif
35
36#define TEVENT_NUM_SIGNALS 64
37
38/* maximum number of SA_SIGINFO signals to hold in the queue.
39 NB. This *MUST* be a power of 2, in order for the ring buffer
40 wrap to work correctly. Thanks to Petr Vandrovec <petr@vandrovec.name>
41 for this. */
42
43#define TEVENT_SA_INFO_QUEUE_COUNT 64
44
45struct tevent_sigcounter {
46 uint32_t count;
47 uint32_t seen;
48};
49
50#define TEVENT_SIG_INCREMENT(s) (s).count++
51#define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
52#define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
53
54struct tevent_common_signal_list {
55 struct tevent_common_signal_list *prev, *next;
56 struct tevent_signal *se;
57};
58
59/*
60 the poor design of signals means that this table must be static global
61*/
62static struct tevent_sig_state {
63 struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
64 struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
65 struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
66 struct tevent_sigcounter got_signal;
67#ifdef SA_SIGINFO
68 /* with SA_SIGINFO we get quite a lot of info per signal */
69 siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
70 struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
71#endif
72} *sig_state;
73
74/*
75 return number of sigcounter events not processed yet
76*/
77static uint32_t tevent_sig_count(struct tevent_sigcounter s)
78{
79 return s.count - s.seen;
80}
81
82/*
83 signal handler - redirects to registered signals
84*/
85static void tevent_common_signal_handler(int signum)
86{
87 char c = 0;
88 ssize_t res;
89 struct tevent_common_signal_list *sl;
90 struct tevent_context *ev = NULL;
91 int saved_errno = errno;
92
93 TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
94 TEVENT_SIG_INCREMENT(sig_state->got_signal);
95
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 res = write(ev->pipe_fds[1], &c, 1);
102 }
103 }
104
105 errno = saved_errno;
106}
107
108#ifdef SA_SIGINFO
109/*
110 signal handler with SA_SIGINFO - redirects to registered signals
111*/
112static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
113 void *uctx)
114{
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;
121
122 tevent_common_signal_handler(signum);
123
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
129 /*
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.
140 */
141 ucontext_t *ucp = (ucontext_t *)uctx;
142 sigaddset(&ucp->uc_sigmask, signum);
143#else
144 /*
145 * WARNING !!! WARNING !!!!
146 *
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.
155 */
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]);
162 }
163}
164#endif
165
166static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
167{
168 if (sig_state->sig_handlers[sl->se->signum]) {
169 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
170 }
171 return 0;
172}
173
174/*
175 destroy a signal event
176*/
177static int tevent_signal_destructor(struct tevent_signal *se)
178{
179 struct tevent_common_signal_list *sl;
180 sl = talloc_get_type(se->additional_data,
181 struct tevent_common_signal_list);
182
183 if (se->event_ctx) {
184 DLIST_REMOVE(se->event_ctx->signal_events, se);
185 }
186
187 talloc_free(sl);
188
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;
194 }
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;
200 }
201 }
202#endif
203 }
204
205 return 0;
206}
207
208/*
209 this is part of the pipe hack needed to avoid the signal race condition
210*/
211static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
212 uint16_t flags, void *_private)
213{
214 char c[16];
215 ssize_t res;
216 /* its non-blocking, doesn't matter if we read too much */
217 res = read(fde->fd, c, sizeof(c));
218}
219
220/*
221 add a signal event
222 return NULL on failure (memory allocation error)
223*/
224struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
225 TALLOC_CTX *mem_ctx,
226 int signum,
227 int sa_flags,
228 tevent_signal_handler_t handler,
229 void *private_data,
230 const char *handler_name,
231 const char *location)
232{
233 struct tevent_signal *se;
234 struct tevent_common_signal_list *sl;
235 sigset_t set, oldset;
236
237 if (signum >= TEVENT_NUM_SIGNALS) {
238 errno = EINVAL;
239 return NULL;
240 }
241
242 /* the sig_state needs to be on a global context as it can last across
243 multiple event contexts */
244 if (sig_state == NULL) {
245 sig_state = talloc_zero(NULL, struct tevent_sig_state);
246 if (sig_state == NULL) {
247 return NULL;
248 }
249 }
250
251 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
252 if (se == NULL) return NULL;
253
254 se->event_ctx = ev;
255 se->signum = signum;
256 se->sa_flags = sa_flags;
257 se->handler = handler;
258 se->private_data = private_data;
259 se->handler_name = handler_name;
260 se->location = location;
261 se->additional_data = NULL;
262
263 sl = talloc(se, struct tevent_common_signal_list);
264 if (!sl) {
265 talloc_free(se);
266 return NULL;
267 }
268 sl->se = se;
269 se->additional_data = sl;
270
271 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
272 if (!talloc_reference(se, sig_state)) {
273 talloc_free(se);
274 return NULL;
275 }
276
277 /* we need to setup the pipe hack handler if not already
278 setup */
279 if (ev->pipe_fde == NULL) {
280 if (pipe(ev->pipe_fds) == -1) {
281 talloc_free(se);
282 return NULL;
283 }
284 ev_set_blocking(ev->pipe_fds[0], false);
285 ev_set_blocking(ev->pipe_fds[1], false);
286 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
287 TEVENT_FD_READ,
288 signal_pipe_handler, NULL);
289 if (!ev->pipe_fde) {
290 close(ev->pipe_fds[0]);
291 close(ev->pipe_fds[1]);
292 talloc_free(se);
293 return NULL;
294 }
295 }
296
297 /* only install a signal handler if not already installed */
298 if (sig_state->sig_handlers[signum] == NULL) {
299 struct sigaction act;
300 ZERO_STRUCT(act);
301 act.sa_handler = tevent_common_signal_handler;
302 act.sa_flags = sa_flags;
303#ifdef SA_SIGINFO
304 if (sa_flags & SA_SIGINFO) {
305 act.sa_handler = NULL;
306 act.sa_sigaction = tevent_common_signal_handler_info;
307 if (sig_state->sig_info[signum] == NULL) {
308 sig_state->sig_info[signum] =
309 talloc_zero_array(sig_state, siginfo_t,
310 TEVENT_SA_INFO_QUEUE_COUNT);
311 if (sig_state->sig_info[signum] == NULL) {
312 talloc_free(se);
313 return NULL;
314 }
315 }
316 }
317#endif
318 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
319 if (sig_state->oldact[signum] == NULL) {
320 talloc_free(se);
321 return NULL;
322 }
323 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
324 talloc_free(se);
325 return NULL;
326 }
327 }
328
329 DLIST_ADD(se->event_ctx->signal_events, se);
330
331 /* Make sure the signal doesn't come in while we're mangling list. */
332 sigemptyset(&set);
333 sigaddset(&set, signum);
334 sigprocmask(SIG_BLOCK, &set, &oldset);
335 DLIST_ADD(sig_state->sig_handlers[signum], sl);
336 sigprocmask(SIG_SETMASK, &oldset, NULL);
337
338 talloc_set_destructor(se, tevent_signal_destructor);
339 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
340
341 return se;
342}
343
344
345/*
346 check if a signal is pending
347 return != 0 if a signal was pending
348*/
349int tevent_common_check_signal(struct tevent_context *ev)
350{
351 int i;
352
353 if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
354 return 0;
355 }
356
357 for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
358 struct tevent_common_signal_list *sl, *next;
359 struct tevent_sigcounter counter = sig_state->signal_count[i];
360 uint32_t count = tevent_sig_count(counter);
361#ifdef SA_SIGINFO
362 /* Ensure we null out any stored siginfo_t entries
363 * after processing for debugging purposes. */
364 bool clear_processed_siginfo = false;
365#endif
366
367 if (count == 0) {
368 continue;
369 }
370 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
371 struct tevent_signal *se = sl->se;
372 next = sl->next;
373#ifdef SA_SIGINFO
374 if (se->sa_flags & SA_SIGINFO) {
375 uint32_t j;
376
377 clear_processed_siginfo = true;
378
379 for (j=0;j<count;j++) {
380 /* sig_state->signal_count[i].seen
381 * % TEVENT_SA_INFO_QUEUE_COUNT is
382 * the base position of the unprocessed
383 * signals in the ringbuffer. */
384 uint32_t ofs = (counter.seen + j)
385 % TEVENT_SA_INFO_QUEUE_COUNT;
386 se->handler(ev, se, i, 1,
387 (void*)&sig_state->sig_info[i][ofs],
388 se->private_data);
389 }
390#ifdef SA_RESETHAND
391 if (se->sa_flags & SA_RESETHAND) {
392 talloc_free(se);
393 }
394#endif
395 continue;
396 }
397#endif
398 se->handler(ev, se, i, count, NULL, se->private_data);
399#ifdef SA_RESETHAND
400 if (se->sa_flags & SA_RESETHAND) {
401 talloc_free(se);
402 }
403#endif
404 }
405
406#ifdef SA_SIGINFO
407 if (clear_processed_siginfo) {
408 uint32_t j;
409 for (j=0;j<count;j++) {
410 uint32_t ofs = (counter.seen + j)
411 % TEVENT_SA_INFO_QUEUE_COUNT;
412 memset((void*)&sig_state->sig_info[i][ofs],
413 '\0',
414 sizeof(siginfo_t));
415 }
416 }
417#endif
418
419 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
420 TEVENT_SIG_SEEN(sig_state->got_signal, count);
421
422#ifdef SA_SIGINFO
423 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
424 /* We'd filled the queue, unblock the
425 signal now the queue is empty again.
426 Note we MUST do this after the
427 TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
428 call to prevent a new signal running
429 out of room in the sig_state->sig_info[i][]
430 ring buffer. */
431 sigset_t set;
432 sigemptyset(&set);
433 sigaddset(&set, i);
434 TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
435 tevent_sig_count(sig_state->sig_blocked[i]));
436 sigprocmask(SIG_UNBLOCK, &set, NULL);
437 }
438#endif
439 }
440
441 return 1;
442}
443
444void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
445{
446 struct tevent_common_signal_list *sl;
447 sl = talloc_get_type(se->additional_data,
448 struct tevent_common_signal_list);
449
450 tevent_common_signal_list_destructor(sl);
451
452 if (sig_state->sig_handlers[se->signum] == NULL) {
453 if (sig_state->oldact[se->signum]) {
454 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
455 sig_state->oldact[se->signum] = NULL;
456 }
457 }
458 return;
459}
Note: See TracBrowser for help on using the repository browser.