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