]> git.ozlabs.org Git - ccan-lca-2011.git/blob - ccan/tevent/tevent_signal.c
lca2011: hacky import of tevent.
[ccan-lca-2011.git] / ccan / tevent / tevent_signal.c
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 <ccan/tevent/tevent.h>
27 #include <ccan/tevent/tevent_internal.h>
28 #include <ccan/tevent/tevent_util.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #define TEVENT_NUM_SIGNALS 64
35
36 /* maximum number of SA_SIGINFO signals to hold in the queue.
37   NB. This *MUST* be a power of 2, in order for the ring buffer
38   wrap to work correctly. Thanks to Petr Vandrovec <petr@vandrovec.name>
39   for this. */
40
41 #define TEVENT_SA_INFO_QUEUE_COUNT 64
42
43 struct tevent_sigcounter {
44         uint32_t count;
45         uint32_t seen;
46 };
47
48 #define TEVENT_SIG_INCREMENT(s) (s).count++
49 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
50 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
51
52 struct tevent_common_signal_list {
53         struct tevent_common_signal_list *prev, *next;
54         struct tevent_signal *se;
55 };
56
57 /*
58   the poor design of signals means that this table must be static global
59 */
60 static struct tevent_sig_state {
61         struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
62         struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
63         struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
64         struct tevent_sigcounter got_signal;
65 #ifdef SA_SIGINFO
66         /* with SA_SIGINFO we get quite a lot of info per signal */
67         siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
68         struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
69 #endif
70 } *sig_state;
71
72 /*
73   return number of sigcounter events not processed yet
74 */
75 static uint32_t tevent_sig_count(struct tevent_sigcounter s)
76 {
77         return s.count - s.seen;
78 }
79
80 /*
81   signal handler - redirects to registered signals
82 */
83 static void tevent_common_signal_handler(int signum)
84 {
85         char c = 0;
86         ssize_t res;
87         struct tevent_common_signal_list *sl;
88         struct tevent_context *ev = NULL;
89         int saved_errno = errno;
90
91         TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
92         TEVENT_SIG_INCREMENT(sig_state->got_signal);
93
94         /* Write to each unique event context. */
95         for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
96                 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
97                         ev = sl->se->event_ctx;
98                         /* doesn't matter if this pipe overflows */
99                         res = write(ev->pipe_fds[1], &c, 1);
100                 }
101         }
102
103         errno = saved_errno;
104 }
105
106 #ifdef SA_SIGINFO
107 /*
108   signal handler with SA_SIGINFO - redirects to registered signals
109 */
110 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
111                                               void *uctx)
112 {
113         uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
114         /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
115          * is the base of the unprocessed signals in the ringbuffer. */
116         uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
117                                 TEVENT_SA_INFO_QUEUE_COUNT;
118         sig_state->sig_info[signum][ofs] = *info;
119
120         tevent_common_signal_handler(signum);
121
122         /* handle SA_SIGINFO */
123         if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
124                 /* we've filled the info array - block this signal until
125                    these ones are delivered */
126                 sigset_t set;
127                 sigemptyset(&set);
128                 sigaddset(&set, signum);
129                 sigprocmask(SIG_BLOCK, &set, NULL);
130                 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
131         }
132 }
133 #endif
134
135 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
136 {
137         if (sig_state->sig_handlers[sl->se->signum]) {
138                 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
139         }
140         return 0;
141 }
142
143 /*
144   destroy a signal event
145 */
146 static int tevent_signal_destructor(struct tevent_signal *se)
147 {
148         struct tevent_common_signal_list *sl;
149         sl = talloc_get_type(se->additional_data,
150                              struct tevent_common_signal_list);
151
152         if (se->event_ctx) {
153                 DLIST_REMOVE(se->event_ctx->signal_events, se);
154         }
155
156         talloc_free(sl);
157
158         if (sig_state->sig_handlers[se->signum] == NULL) {
159                 /* restore old handler, if any */
160                 if (sig_state->oldact[se->signum]) {
161                         sigaction(se->signum, sig_state->oldact[se->signum], NULL);
162                         sig_state->oldact[se->signum] = NULL;
163                 }
164 #ifdef SA_SIGINFO
165                 if (se->sa_flags & SA_SIGINFO) {
166                         if (sig_state->sig_info[se->signum]) {
167                                 talloc_free(sig_state->sig_info[se->signum]);
168                                 sig_state->sig_info[se->signum] = NULL;
169                         }
170                 }
171 #endif
172         }
173
174         return 0;
175 }
176
177 /*
178   this is part of the pipe hack needed to avoid the signal race condition
179 */
180 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde, 
181                                 uint16_t flags, void *_private)
182 {
183         char c[16];
184         ssize_t res;
185         /* its non-blocking, doesn't matter if we read too much */
186         res = read(fde->fd, c, sizeof(c));
187 }
188
189 /*
190   add a signal event
191   return NULL on failure (memory allocation error)
192 */
193 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
194                                                TALLOC_CTX *mem_ctx,
195                                                int signum,
196                                                int sa_flags,
197                                                tevent_signal_handler_t handler,
198                                                void *private_data,
199                                                const char *handler_name,
200                                                const char *location)
201 {
202         struct tevent_signal *se;
203         struct tevent_common_signal_list *sl;
204         sigset_t set, oldset;
205
206         if (signum >= TEVENT_NUM_SIGNALS) {
207                 errno = EINVAL;
208                 return NULL;
209         }
210
211         /* the sig_state needs to be on a global context as it can last across
212            multiple event contexts */
213         if (sig_state == NULL) {
214                 sig_state = talloc_zero(NULL, struct tevent_sig_state);
215                 if (sig_state == NULL) {
216                         return NULL;
217                 }
218         }
219
220         se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
221         if (se == NULL) return NULL;
222
223         se->event_ctx           = ev;
224         se->signum              = signum;
225         se->sa_flags            = sa_flags;
226         se->handler             = handler;
227         se->private_data        = private_data;
228         se->handler_name        = handler_name;
229         se->location            = location;
230         se->additional_data     = NULL;
231
232         sl = talloc(se, struct tevent_common_signal_list);
233         if (!sl) {
234                 talloc_free(se);
235                 return NULL;
236         }
237         sl->se = se;
238         se->additional_data     = sl;
239
240         /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
241         if (!talloc_reference(se, sig_state)) {
242                 talloc_free(se);
243                 return NULL;
244         }
245
246         /* we need to setup the pipe hack handler if not already
247            setup */
248         if (ev->pipe_fde == NULL) {
249                 if (pipe(ev->pipe_fds) == -1) {
250                         talloc_free(se);
251                         return NULL;
252                 }
253                 ev_set_blocking(ev->pipe_fds[0], false);
254                 ev_set_blocking(ev->pipe_fds[1], false);
255                 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
256                                              TEVENT_FD_READ,
257                                              signal_pipe_handler, NULL);
258                 if (!ev->pipe_fde) {
259                         close(ev->pipe_fds[0]);
260                         close(ev->pipe_fds[1]);
261                         talloc_free(se);
262                         return NULL;
263                 }
264         }
265
266         /* only install a signal handler if not already installed */
267         if (sig_state->sig_handlers[signum] == NULL) {
268                 struct sigaction act;
269                 ZERO_STRUCT(act);
270                 act.sa_handler = tevent_common_signal_handler;
271                 act.sa_flags = sa_flags;
272 #ifdef SA_SIGINFO
273                 if (sa_flags & SA_SIGINFO) {
274                         act.sa_handler   = NULL;
275                         act.sa_sigaction = tevent_common_signal_handler_info;
276                         if (sig_state->sig_info[signum] == NULL) {
277                                 sig_state->sig_info[signum] =
278                                         talloc_zero_array(sig_state, siginfo_t,
279                                                           TEVENT_SA_INFO_QUEUE_COUNT);
280                                 if (sig_state->sig_info[signum] == NULL) {
281                                         talloc_free(se);
282                                         return NULL;
283                                 }
284                         }
285                 }
286 #endif
287                 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
288                 if (sig_state->oldact[signum] == NULL) {
289                         talloc_free(se);
290                         return NULL;                    
291                 }
292                 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
293                         talloc_free(se);
294                         return NULL;
295                 }
296         }
297
298         DLIST_ADD(se->event_ctx->signal_events, se);
299
300         /* Make sure the signal doesn't come in while we're mangling list. */
301         sigemptyset(&set);
302         sigaddset(&set, signum);
303         sigprocmask(SIG_BLOCK, &set, &oldset);
304         DLIST_ADD(sig_state->sig_handlers[signum], sl);
305         sigprocmask(SIG_SETMASK, &oldset, NULL);
306
307         talloc_set_destructor(se, tevent_signal_destructor);
308         talloc_set_destructor(sl, tevent_common_signal_list_destructor);
309
310         return se;
311 }
312
313
314 /*
315   check if a signal is pending
316   return != 0 if a signal was pending
317 */
318 int tevent_common_check_signal(struct tevent_context *ev)
319 {
320         int i;
321
322         if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
323                 return 0;
324         }
325         
326         for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
327                 struct tevent_common_signal_list *sl, *next;
328                 struct tevent_sigcounter counter = sig_state->signal_count[i];
329                 uint32_t count = tevent_sig_count(counter);
330 #ifdef SA_SIGINFO
331                 /* Ensure we null out any stored siginfo_t entries
332                  * after processing for debugging purposes. */
333                 bool clear_processed_siginfo = false;
334 #endif
335
336                 if (count == 0) {
337                         continue;
338                 }
339                 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
340                         struct tevent_signal *se = sl->se;
341                         next = sl->next;
342 #ifdef SA_SIGINFO
343                         if (se->sa_flags & SA_SIGINFO) {
344                                 uint32_t j;
345
346                                 clear_processed_siginfo = true;
347
348                                 for (j=0;j<count;j++) {
349                                         /* sig_state->signal_count[i].seen
350                                          * % TEVENT_SA_INFO_QUEUE_COUNT is
351                                          * the base position of the unprocessed
352                                          * signals in the ringbuffer. */
353                                         uint32_t ofs = (counter.seen + j)
354                                                 % TEVENT_SA_INFO_QUEUE_COUNT;
355                                         se->handler(ev, se, i, 1,
356                                                     (void*)&sig_state->sig_info[i][ofs], 
357                                                     se->private_data);
358                                 }
359                                 if (se->sa_flags & SA_RESETHAND) {
360                                         talloc_free(se);
361                                 }
362                                 continue;
363                         }
364 #endif
365                         se->handler(ev, se, i, count, NULL, se->private_data);
366                         if (se->sa_flags & SA_RESETHAND) {
367                                 talloc_free(se);
368                         }
369                 }
370
371 #ifdef SA_SIGINFO
372                 if (clear_processed_siginfo) {
373                         uint32_t j;
374                         for (j=0;j<count;j++) {
375                                 uint32_t ofs = (counter.seen + j)
376                                         % TEVENT_SA_INFO_QUEUE_COUNT;
377                                 memset((void*)&sig_state->sig_info[i][ofs],
378                                         '\0',
379                                         sizeof(siginfo_t));
380                         }
381                 }
382 #endif
383
384                 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
385                 TEVENT_SIG_SEEN(sig_state->got_signal, count);
386
387 #ifdef SA_SIGINFO
388                 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
389                         /* We'd filled the queue, unblock the
390                            signal now the queue is empty again.
391                            Note we MUST do this after the
392                            TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
393                            call to prevent a new signal running
394                            out of room in the sig_state->sig_info[i][]
395                            ring buffer. */
396                         sigset_t set;
397                         sigemptyset(&set);
398                         sigaddset(&set, i);
399                         TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
400                                  tevent_sig_count(sig_state->sig_blocked[i]));
401                         sigprocmask(SIG_UNBLOCK, &set, NULL);
402                 }
403 #endif
404         }
405
406         return 1;
407 }
408
409 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
410 {
411         struct tevent_common_signal_list *sl;
412         sl = talloc_get_type(se->additional_data,
413                              struct tevent_common_signal_list);
414
415         tevent_common_signal_list_destructor(sl);
416
417         if (sig_state->sig_handlers[se->signum] == NULL) {
418                 if (sig_state->oldact[se->signum]) {
419                         sigaction(se->signum, sig_state->oldact[se->signum], NULL);
420                         sig_state->oldact[se->signum] = NULL;
421                 }
422         }
423         return;
424 }