tevent: rename signal handler functions to start with tevent_common_
[tprouty/samba.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    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "replace.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "tevent.h"
26 #include "tevent_internal.h"
27 #include "tevent_util.h"
28
29 #define NUM_SIGNALS 64
30
31 /* maximum number of SA_SIGINFO signals to hold in the queue */
32 #define SA_INFO_QUEUE_COUNT 10
33
34 struct sigcounter {
35         uint32_t count;
36         uint32_t seen;
37 };
38
39 #define SIG_INCREMENT(s) (s).count++
40 #define SIG_SEEN(s, n) (s).seen += (n)
41 #define SIG_PENDING(s) ((s).seen != (s).count)
42
43 struct tevent_common_signal_list {
44         struct tevent_common_signal_list *prev, *next;
45         struct tevent_signal *se;
46 };
47
48 /*
49   the poor design of signals means that this table must be static global
50 */
51 static struct sig_state {
52         struct tevent_common_signal_list *sig_handlers[NUM_SIGNALS+1];
53         struct sigaction *oldact[NUM_SIGNALS+1];
54         struct sigcounter signal_count[NUM_SIGNALS+1];
55         struct sigcounter got_signal;
56         int pipe_hack[2];
57 #ifdef SA_SIGINFO
58         /* with SA_SIGINFO we get quite a lot of info per signal */
59         siginfo_t *sig_info[NUM_SIGNALS+1];
60         struct sigcounter sig_blocked[NUM_SIGNALS+1];
61 #endif
62 } *sig_state;
63
64 /*
65   return number of sigcounter events not processed yet
66 */
67 static uint32_t sig_count(struct sigcounter s)
68 {
69         if (s.count >= s.seen) {
70                 return s.count - s.seen;
71         }
72         return 1 + (0xFFFFFFFF & ~(s.seen - s.count));
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         SIG_INCREMENT(sig_state->signal_count[signum]);
82         SIG_INCREMENT(sig_state->got_signal);
83         /* doesn't matter if this pipe overflows */
84         write(sig_state->pipe_hack[1], &c, 1);
85 }
86
87 #ifdef SA_SIGINFO
88 /*
89   signal handler with SA_SIGINFO - redirects to registered signals
90 */
91 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
92                                               void *uctx)
93 {
94         uint32_t count = sig_count(sig_state->signal_count[signum]);
95         sig_state->sig_info[signum][count] = *info;
96
97         tevent_common_signal_handler(signum);
98
99         /* handle SA_SIGINFO */
100         if (count+1 == SA_INFO_QUEUE_COUNT) {
101                 /* we've filled the info array - block this signal until
102                    these ones are delivered */
103                 sigset_t set;
104                 sigemptyset(&set);
105                 sigaddset(&set, signum);
106                 sigprocmask(SIG_BLOCK, &set, NULL);
107                 SIG_INCREMENT(sig_state->sig_blocked[signum]);
108         }
109 }
110 #endif
111
112 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
113 {
114         DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
115         return 0;
116 }
117
118 /*
119   destroy a signal event
120 */
121 static int tevent_signal_destructor(struct tevent_signal *se)
122 {
123         struct tevent_common_signal_list *sl;
124         sl = talloc_get_type(se->additional_data,
125                              struct tevent_common_signal_list);
126
127         if (se->event_ctx) {
128                 DLIST_REMOVE(se->event_ctx->signal_events, se);
129         }
130
131         talloc_free(sl);
132
133         if (sig_state->sig_handlers[se->signum] == NULL) {
134                 /* restore old handler, if any */
135                 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
136                 sig_state->oldact[se->signum] = NULL;
137 #ifdef SA_SIGINFO
138                 if (se->sa_flags & SA_SIGINFO) {
139                         talloc_free(sig_state->sig_info[se->signum]);
140                         sig_state->sig_info[se->signum] = NULL;
141                 }
142 #endif
143         }
144
145         return 0;
146 }
147
148 /*
149   this is part of the pipe hack needed to avoid the signal race condition
150 */
151 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde, 
152                                 uint16_t flags, void *private)
153 {
154         char c[16];
155         /* its non-blocking, doesn't matter if we read too much */
156         read(sig_state->pipe_hack[0], c, sizeof(c));
157 }
158
159 /*
160   add a signal event
161   return NULL on failure (memory allocation error)
162 */
163 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
164                                                TALLOC_CTX *mem_ctx,
165                                                int signum,
166                                                int sa_flags,
167                                                tevent_signal_handler_t handler,
168                                                void *private_data,
169                                                const char *handler_name,
170                                                const char *location)
171 {
172         struct tevent_signal *se;
173         struct tevent_common_signal_list *sl;
174
175         if (signum >= NUM_SIGNALS) {
176                 errno = EINVAL;
177                 return NULL;
178         }
179
180         /* the sig_state needs to be on a global context as it can last across
181            multiple event contexts */
182         if (sig_state == NULL) {
183                 sig_state = talloc_zero(talloc_autofree_context(), struct sig_state);
184                 if (sig_state == NULL) {
185                         return NULL;
186                 }
187         }
188
189         se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
190         if (se == NULL) return NULL;
191
192         se->event_ctx           = ev;
193         se->signum              = signum;
194         se->sa_flags            = sa_flags;
195         se->handler             = handler;
196         se->private_data        = private_data;
197         se->handler_name        = handler_name;
198         se->location            = location;
199         se->additional_data     = NULL;
200
201         sl = talloc(se, struct tevent_common_signal_list);
202         if (!sl) {
203                 talloc_free(se);
204                 return NULL;
205         }
206         sl->se = se;
207         se->additional_data     = sl;
208
209         /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
210         if (!talloc_reference(se, sig_state)) {
211                 talloc_free(se);
212                 return NULL;
213         }
214
215         /* only install a signal handler if not already installed */
216         if (sig_state->sig_handlers[signum] == NULL) {
217                 struct sigaction act;
218                 ZERO_STRUCT(act);
219                 act.sa_handler = tevent_common_signal_handler;
220                 act.sa_flags = sa_flags;
221 #ifdef SA_SIGINFO
222                 if (sa_flags & SA_SIGINFO) {
223                         act.sa_handler   = NULL;
224                         act.sa_sigaction = tevent_common_signal_handler_info;
225                         if (sig_state->sig_info[signum] == NULL) {
226                                 sig_state->sig_info[signum] = talloc_array(sig_state, siginfo_t, SA_INFO_QUEUE_COUNT);
227                                 if (sig_state->sig_info[signum] == NULL) {
228                                         talloc_free(se);
229                                         return NULL;
230                                 }
231                         }
232                 }
233 #endif
234                 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
235                 if (sig_state->oldact[signum] == NULL) {
236                         talloc_free(se);
237                         return NULL;                    
238                 }
239                 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
240                         talloc_free(se);
241                         return NULL;
242                 }
243         }
244
245         DLIST_ADD(se->event_ctx->signal_events, se);
246         DLIST_ADD(sig_state->sig_handlers[signum], sl);
247
248         talloc_set_destructor(se, tevent_signal_destructor);
249         talloc_set_destructor(sl, tevent_common_signal_list_destructor);
250
251         /* we need to setup the pipe hack handler if not already
252            setup */
253         if (ev->pipe_fde == NULL) {
254                 if (sig_state->pipe_hack[0] == 0 && 
255                     sig_state->pipe_hack[1] == 0) {
256                         pipe(sig_state->pipe_hack);
257                         ev_set_blocking(sig_state->pipe_hack[0], false);
258                         ev_set_blocking(sig_state->pipe_hack[1], false);
259                 }
260                 ev->pipe_fde = tevent_add_fd(ev, ev, sig_state->pipe_hack[0],
261                                              TEVENT_FD_READ, signal_pipe_handler, NULL);
262                 if (!ev->pipe_fde) {
263                         talloc_free(se);
264                         return NULL;
265                 }
266         }
267
268         return se;
269 }
270
271
272 /*
273   check if a signal is pending
274   return != 0 if a signal was pending
275 */
276 int tevent_common_check_signal(struct tevent_context *ev)
277 {
278         int i;
279
280         if (!sig_state || !SIG_PENDING(sig_state->got_signal)) {
281                 return 0;
282         }
283         
284         for (i=0;i<NUM_SIGNALS+1;i++) {
285                 struct tevent_common_signal_list *sl, *next;
286                 struct sigcounter counter = sig_state->signal_count[i];
287                 uint32_t count = sig_count(counter);
288
289                 if (count == 0) {
290                         continue;
291                 }
292                 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
293                         struct tevent_signal *se = sl->se;
294                         next = sl->next;
295 #ifdef SA_SIGINFO
296                         if (se->sa_flags & SA_SIGINFO) {
297                                 int j;
298                                 for (j=0;j<count;j++) {
299                                         /* note the use of the sig_info array as a
300                                            ring buffer */
301                                         int ofs = ((count-1) + j) % SA_INFO_QUEUE_COUNT;
302                                         se->handler(ev, se, i, 1, 
303                                                     (void*)&sig_state->sig_info[i][ofs], 
304                                                     se->private_data);
305                                 }
306                                 if (SIG_PENDING(sig_state->sig_blocked[i])) {
307                                         /* we'd filled the queue, unblock the
308                                            signal now */
309                                         sigset_t set;
310                                         sigemptyset(&set);
311                                         sigaddset(&set, i);
312                                         SIG_SEEN(sig_state->sig_blocked[i], 
313                                                  sig_count(sig_state->sig_blocked[i]));
314                                         sigprocmask(SIG_UNBLOCK, &set, NULL);
315                                 }
316                                 if (se->sa_flags & SA_RESETHAND) {
317                                         talloc_free(se);
318                                 }
319                                 continue;
320                         }
321 #endif
322                         se->handler(ev, se, i, count, NULL, se->private_data);
323                         if (se->sa_flags & SA_RESETHAND) {
324                                 talloc_free(se);
325                         }
326                 }
327                 SIG_SEEN(sig_state->signal_count[i], count);
328                 SIG_SEEN(sig_state->got_signal, count);
329         }
330
331         return 1;
332 }