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