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