tevent:signal: fix -O3 error unused result of write
[sfrench/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 /* maximum number of SA_SIGINFO signals to hold in the queue.
34   NB. This *MUST* be a power of 2, in order for the ring buffer
35   wrap to work correctly. Thanks to Petr Vandrovec <petr@vandrovec.name>
36   for this. */
37
38 #define TEVENT_SA_INFO_QUEUE_COUNT 256
39
40 size_t tevent_num_signals(void)
41 {
42         return TEVENT_NUM_SIGNALS;
43 }
44
45 size_t tevent_sa_info_queue_count(void)
46 {
47         return TEVENT_SA_INFO_QUEUE_COUNT;
48 }
49
50 struct tevent_sigcounter {
51         uint32_t count;
52         uint32_t seen;
53 };
54
55 #if defined(HAVE___SYNC_FETCH_AND_ADD)
56 #define TEVENT_SIG_INCREMENT(s) __sync_fetch_and_add(&((s).count), 1)
57 #elif defined(HAVE_ATOMIC_ADD_32)
58 #define TEVENT_SIG_INCREMENT(s) atomic_add_32(&((s).count), 1)
59 #else
60 #define TEVENT_SIG_INCREMENT(s) (s).count++
61 #endif
62 #define TEVENT_SIG_SEEN(s, n) (s).seen += (n)
63 #define TEVENT_SIG_PENDING(s) ((s).seen != (s).count)
64
65 struct tevent_common_signal_list {
66         struct tevent_common_signal_list *prev, *next;
67         struct tevent_signal *se;
68 };
69
70 /*
71   the poor design of signals means that this table must be static global
72 */
73 static struct tevent_sig_state {
74         struct tevent_common_signal_list *sig_handlers[TEVENT_NUM_SIGNALS+1];
75         struct sigaction *oldact[TEVENT_NUM_SIGNALS+1];
76         struct tevent_sigcounter signal_count[TEVENT_NUM_SIGNALS+1];
77         struct tevent_sigcounter got_signal;
78 #ifdef SA_SIGINFO
79         /* with SA_SIGINFO we get quite a lot of info per signal */
80         siginfo_t *sig_info[TEVENT_NUM_SIGNALS+1];
81         struct tevent_sigcounter sig_blocked[TEVENT_NUM_SIGNALS+1];
82 #endif
83 } *sig_state;
84
85 /*
86   return number of sigcounter events not processed yet
87 */
88 static uint32_t tevent_sig_count(struct tevent_sigcounter s)
89 {
90         return s.count - s.seen;
91 }
92
93 /*
94   signal handler - redirects to registered signals
95 */
96 static void tevent_common_signal_handler(int signum)
97 {
98         char c = 0;
99         struct tevent_common_signal_list *sl;
100         struct tevent_context *ev = NULL;
101         int saved_errno = errno;
102
103         TEVENT_SIG_INCREMENT(sig_state->signal_count[signum]);
104         TEVENT_SIG_INCREMENT(sig_state->got_signal);
105
106         /* Write to each unique event context. */
107         for (sl = sig_state->sig_handlers[signum]; sl; sl = sl->next) {
108                 if (sl->se->event_ctx && sl->se->event_ctx != ev) {
109                         ssize_t ret;
110
111                         ev = sl->se->event_ctx;
112                         /* doesn't matter if this pipe overflows */
113                         do {
114                                 ret = write(ev->pipe_fds[1], &c, 1);
115                         } while (ret == -1 && errno == EINTR);
116                 }
117         }
118
119         errno = saved_errno;
120 }
121
122 #ifdef SA_SIGINFO
123 /*
124   signal handler with SA_SIGINFO - redirects to registered signals
125 */
126 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
127                                               void *uctx)
128 {
129         uint32_t count = tevent_sig_count(sig_state->signal_count[signum]);
130         /* sig_state->signal_count[signum].seen % TEVENT_SA_INFO_QUEUE_COUNT
131          * is the base of the unprocessed signals in the ringbuffer. */
132         uint32_t ofs = (sig_state->signal_count[signum].seen + count) %
133                                 TEVENT_SA_INFO_QUEUE_COUNT;
134         sig_state->sig_info[signum][ofs] = *info;
135
136         tevent_common_signal_handler(signum);
137
138         /* handle SA_SIGINFO */
139         if (count+1 == TEVENT_SA_INFO_QUEUE_COUNT) {
140                 /* we've filled the info array - block this signal until
141                    these ones are delivered */
142 #ifdef HAVE_UCONTEXT_T
143                 /*
144                  * This is the only way for this to work.
145                  * By default signum is blocked inside this
146                  * signal handler using a temporary mask,
147                  * but what we really need to do now is
148                  * block it in the callers mask, so it
149                  * stays blocked when the temporary signal
150                  * handler mask is replaced when we return
151                  * from here. The callers mask can be found
152                  * in the ucontext_t passed in as the
153                  * void *uctx argument.
154                  */
155                 ucontext_t *ucp = (ucontext_t *)uctx;
156                 sigaddset(&ucp->uc_sigmask, signum);
157 #else
158                 /*
159                  * WARNING !!! WARNING !!!!
160                  *
161                  * This code doesn't work.
162                  * By default signum is blocked inside this
163                  * signal handler, but calling sigprocmask
164                  * modifies the temporary signal mask being
165                  * used *inside* this handler, which will be
166                  * replaced by the callers signal mask once
167                  * we return from here. See Samba
168                  * bug #9550 for details.
169                  */
170                 sigset_t set;
171                 sigemptyset(&set);
172                 sigaddset(&set, signum);
173                 sigprocmask(SIG_BLOCK, &set, NULL);
174 #endif
175                 TEVENT_SIG_INCREMENT(sig_state->sig_blocked[signum]);
176         }
177 }
178 #endif
179
180 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
181 {
182         if (sig_state->sig_handlers[sl->se->signum]) {
183                 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
184         }
185         return 0;
186 }
187
188 /*
189   destroy a signal event
190 */
191 static int tevent_signal_destructor(struct tevent_signal *se)
192 {
193         struct tevent_common_signal_list *sl =
194                 talloc_get_type_abort(se->additional_data,
195                 struct tevent_common_signal_list);
196
197         if (se->event_ctx) {
198                 struct tevent_context *ev = se->event_ctx;
199
200                 DLIST_REMOVE(ev->signal_events, se);
201
202                 if (ev->signal_events == NULL && ev->pipe_fde != NULL) {
203                         /*
204                          * This was the last signal. Destroy the pipe.
205                          */
206                         TALLOC_FREE(ev->pipe_fde);
207
208                         close(ev->pipe_fds[0]);
209                         close(ev->pipe_fds[1]);
210                 }
211         }
212
213         talloc_free(sl);
214
215         if (sig_state->sig_handlers[se->signum] == NULL) {
216                 /* restore old handler, if any */
217                 if (sig_state->oldact[se->signum]) {
218                         sigaction(se->signum, sig_state->oldact[se->signum], NULL);
219                         talloc_free(sig_state->oldact[se->signum]);
220                         sig_state->oldact[se->signum] = NULL;
221                 }
222 #ifdef SA_SIGINFO
223                 if (se->sa_flags & SA_SIGINFO) {
224                         if (sig_state->sig_info[se->signum]) {
225                                 talloc_free(sig_state->sig_info[se->signum]);
226                                 sig_state->sig_info[se->signum] = NULL;
227                         }
228                 }
229 #endif
230         }
231
232         return 0;
233 }
234
235 /*
236   this is part of the pipe hack needed to avoid the signal race condition
237 */
238 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde, 
239                                 uint16_t flags, void *_private)
240 {
241         char c[16];
242         /* its non-blocking, doesn't matter if we read too much */
243         (void) read(fde->fd, c, sizeof(c));
244 }
245
246 /*
247   add a signal event
248   return NULL on failure (memory allocation error)
249 */
250 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
251                                                TALLOC_CTX *mem_ctx,
252                                                int signum,
253                                                int sa_flags,
254                                                tevent_signal_handler_t handler,
255                                                void *private_data,
256                                                const char *handler_name,
257                                                const char *location)
258 {
259         struct tevent_signal *se;
260         struct tevent_common_signal_list *sl;
261         sigset_t set, oldset;
262
263         if (signum >= TEVENT_NUM_SIGNALS) {
264                 errno = EINVAL;
265                 return NULL;
266         }
267
268         /* the sig_state needs to be on a global context as it can last across
269            multiple event contexts */
270         if (sig_state == NULL) {
271                 sig_state = talloc_zero(NULL, struct tevent_sig_state);
272                 if (sig_state == NULL) {
273                         return NULL;
274                 }
275         }
276
277         se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
278         if (se == NULL) return NULL;
279
280         se->event_ctx           = ev;
281         se->signum              = signum;
282         se->sa_flags            = sa_flags;
283         se->handler             = handler;
284         se->private_data        = private_data;
285         se->handler_name        = handler_name;
286         se->location            = location;
287         se->additional_data     = NULL;
288
289         sl = talloc(se, struct tevent_common_signal_list);
290         if (!sl) {
291                 talloc_free(se);
292                 return NULL;
293         }
294         sl->se = se;
295         se->additional_data     = sl;
296
297         /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
298         if (!talloc_reference(se, sig_state)) {
299                 talloc_free(se);
300                 return NULL;
301         }
302
303         /* we need to setup the pipe hack handler if not already
304            setup */
305         if (ev->pipe_fde == NULL) {
306                 if (pipe(ev->pipe_fds) == -1) {
307                         talloc_free(se);
308                         return NULL;
309                 }
310                 ev_set_blocking(ev->pipe_fds[0], false);
311                 ev_set_blocking(ev->pipe_fds[1], false);
312                 ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
313                                              TEVENT_FD_READ,
314                                              signal_pipe_handler, NULL);
315                 if (!ev->pipe_fde) {
316                         close(ev->pipe_fds[0]);
317                         close(ev->pipe_fds[1]);
318                         talloc_free(se);
319                         return NULL;
320                 }
321         }
322
323         /* only install a signal handler if not already installed */
324         if (sig_state->sig_handlers[signum] == NULL) {
325                 struct sigaction act;
326                 ZERO_STRUCT(act);
327                 act.sa_handler = tevent_common_signal_handler;
328                 act.sa_flags = sa_flags;
329 #ifdef SA_SIGINFO
330                 if (sa_flags & SA_SIGINFO) {
331                         act.sa_handler   = NULL;
332                         act.sa_sigaction = tevent_common_signal_handler_info;
333                         if (sig_state->sig_info[signum] == NULL) {
334                                 sig_state->sig_info[signum] =
335                                         talloc_zero_array(sig_state, siginfo_t,
336                                                           TEVENT_SA_INFO_QUEUE_COUNT);
337                                 if (sig_state->sig_info[signum] == NULL) {
338                                         talloc_free(se);
339                                         return NULL;
340                                 }
341                         }
342                 }
343 #endif
344                 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
345                 if (sig_state->oldact[signum] == NULL) {
346                         talloc_free(se);
347                         return NULL;
348                 }
349                 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
350                         talloc_free(sig_state->oldact[signum]);
351                         sig_state->oldact[signum] = NULL;
352                         talloc_free(se);
353                         return NULL;
354                 }
355         }
356
357         DLIST_ADD(se->event_ctx->signal_events, se);
358
359         /* Make sure the signal doesn't come in while we're mangling list. */
360         sigemptyset(&set);
361         sigaddset(&set, signum);
362         sigprocmask(SIG_BLOCK, &set, &oldset);
363         DLIST_ADD(sig_state->sig_handlers[signum], sl);
364         sigprocmask(SIG_SETMASK, &oldset, NULL);
365
366         talloc_set_destructor(se, tevent_signal_destructor);
367         talloc_set_destructor(sl, tevent_common_signal_list_destructor);
368
369         return se;
370 }
371
372 struct tevent_se_exists {
373         struct tevent_se_exists **myself;
374 };
375
376 static int tevent_se_exists_destructor(struct tevent_se_exists *s)
377 {
378         *s->myself = NULL;
379         return 0;
380 }
381
382 /*
383   check if a signal is pending
384   return != 0 if a signal was pending
385 */
386 int tevent_common_check_signal(struct tevent_context *ev)
387 {
388         int i;
389
390         if (!sig_state || !TEVENT_SIG_PENDING(sig_state->got_signal)) {
391                 return 0;
392         }
393
394         for (i=0;i<TEVENT_NUM_SIGNALS+1;i++) {
395                 struct tevent_common_signal_list *sl, *next;
396                 struct tevent_sigcounter counter = sig_state->signal_count[i];
397                 uint32_t count = tevent_sig_count(counter);
398 #ifdef SA_SIGINFO
399                 /* Ensure we null out any stored siginfo_t entries
400                  * after processing for debugging purposes. */
401                 bool clear_processed_siginfo = false;
402 #endif
403
404                 if (count == 0) {
405                         continue;
406                 }
407                 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
408                         struct tevent_signal *se = sl->se;
409                         struct tevent_se_exists *exists;
410
411                         next = sl->next;
412
413                         /*
414                          * We have to be careful to not touch "se"
415                          * after it was deleted in its handler. Thus
416                          * we allocate a child whose destructor will
417                          * tell by nulling out itself that its parent
418                          * is gone.
419                          */
420                         exists = talloc(se, struct tevent_se_exists);
421                         if (exists == NULL) {
422                                 continue;
423                         }
424                         exists->myself = &exists;
425                         talloc_set_destructor(
426                                 exists, tevent_se_exists_destructor);
427
428 #ifdef SA_SIGINFO
429                         if (se->sa_flags & SA_SIGINFO) {
430                                 uint32_t j;
431
432                                 clear_processed_siginfo = true;
433
434                                 for (j=0;j<count;j++) {
435                                         /* sig_state->signal_count[i].seen
436                                          * % TEVENT_SA_INFO_QUEUE_COUNT is
437                                          * the base position of the unprocessed
438                                          * signals in the ringbuffer. */
439                                         uint32_t ofs = (counter.seen + j)
440                                                 % TEVENT_SA_INFO_QUEUE_COUNT;
441                                         se->handler(ev, se, i, 1,
442                                                     (void*)&sig_state->sig_info[i][ofs],
443                                                     se->private_data);
444                                         if (!exists) {
445                                                 break;
446                                         }
447                                 }
448 #ifdef SA_RESETHAND
449                                 if (exists && (se->sa_flags & SA_RESETHAND)) {
450                                         talloc_free(se);
451                                 }
452 #endif
453                                 talloc_free(exists);
454                                 continue;
455                         }
456 #endif
457                         se->handler(ev, se, i, count, NULL, se->private_data);
458 #ifdef SA_RESETHAND
459                         if (exists && (se->sa_flags & SA_RESETHAND)) {
460                                 talloc_free(se);
461                         }
462 #endif
463                         talloc_free(exists);
464                 }
465
466 #ifdef SA_SIGINFO
467                 if (clear_processed_siginfo && sig_state->sig_info[i] != NULL) {
468                         uint32_t j;
469                         for (j=0;j<count;j++) {
470                                 uint32_t ofs = (counter.seen + j)
471                                         % TEVENT_SA_INFO_QUEUE_COUNT;
472                                 memset((void*)&sig_state->sig_info[i][ofs],
473                                         '\0',
474                                         sizeof(siginfo_t));
475                         }
476                 }
477 #endif
478
479                 TEVENT_SIG_SEEN(sig_state->signal_count[i], count);
480                 TEVENT_SIG_SEEN(sig_state->got_signal, count);
481
482 #ifdef SA_SIGINFO
483                 if (TEVENT_SIG_PENDING(sig_state->sig_blocked[i])) {
484                         /* We'd filled the queue, unblock the
485                            signal now the queue is empty again.
486                            Note we MUST do this after the
487                            TEVENT_SIG_SEEN(sig_state->signal_count[i], count)
488                            call to prevent a new signal running
489                            out of room in the sig_state->sig_info[i][]
490                            ring buffer. */
491                         sigset_t set;
492                         sigemptyset(&set);
493                         sigaddset(&set, i);
494                         TEVENT_SIG_SEEN(sig_state->sig_blocked[i],
495                                  tevent_sig_count(sig_state->sig_blocked[i]));
496                         sigprocmask(SIG_UNBLOCK, &set, NULL);
497                 }
498 #endif
499         }
500
501         return 1;
502 }
503
504 void tevent_cleanup_pending_signal_handlers(struct tevent_signal *se)
505 {
506         struct tevent_common_signal_list *sl =
507                 talloc_get_type_abort(se->additional_data,
508                 struct tevent_common_signal_list);
509
510         tevent_common_signal_list_destructor(sl);
511
512         if (sig_state->sig_handlers[se->signum] == NULL) {
513                 if (sig_state->oldact[se->signum]) {
514                         sigaction(se->signum, sig_state->oldact[se->signum], NULL);
515                         talloc_free(sig_state->oldact[se->signum]);
516                         sig_state->oldact[se->signum] = NULL;
517                 }
518         }
519         return;
520 }