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