f71d9b36b0e365431f11215fc90ba3892c29c25f
[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    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 100
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         ssize_t res;
82         SIG_INCREMENT(sig_state->signal_count[signum]);
83         SIG_INCREMENT(sig_state->got_signal);
84         /* doesn't matter if this pipe overflows */
85         res = write(sig_state->pipe_hack[1], &c, 1);
86 }
87
88 #ifdef SA_SIGINFO
89 /*
90   signal handler with SA_SIGINFO - redirects to registered signals
91 */
92 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
93                                               void *uctx)
94 {
95         uint32_t count = sig_count(sig_state->signal_count[signum]);
96         sig_state->sig_info[signum][count] = *info;
97
98         tevent_common_signal_handler(signum);
99
100         /* handle SA_SIGINFO */
101         if (count+1 == SA_INFO_QUEUE_COUNT) {
102                 /* we've filled the info array - block this signal until
103                    these ones are delivered */
104                 sigset_t set;
105                 sigemptyset(&set);
106                 sigaddset(&set, signum);
107                 sigprocmask(SIG_BLOCK, &set, NULL);
108                 SIG_INCREMENT(sig_state->sig_blocked[signum]);
109         }
110 }
111 #endif
112
113 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
114 {
115         DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
116         return 0;
117 }
118
119 /*
120   destroy a signal event
121 */
122 static int tevent_signal_destructor(struct tevent_signal *se)
123 {
124         struct tevent_common_signal_list *sl;
125         sl = talloc_get_type(se->additional_data,
126                              struct tevent_common_signal_list);
127
128         if (se->event_ctx) {
129                 DLIST_REMOVE(se->event_ctx->signal_events, se);
130         }
131
132         talloc_free(sl);
133
134         if (sig_state->sig_handlers[se->signum] == NULL) {
135                 /* restore old handler, if any */
136                 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
137                 sig_state->oldact[se->signum] = NULL;
138 #ifdef SA_SIGINFO
139                 if (se->sa_flags & SA_SIGINFO) {
140                         talloc_free(sig_state->sig_info[se->signum]);
141                         sig_state->sig_info[se->signum] = NULL;
142                 }
143 #endif
144         }
145
146         return 0;
147 }
148
149 /*
150   this is part of the pipe hack needed to avoid the signal race condition
151 */
152 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde, 
153                                 uint16_t flags, void *private)
154 {
155         char c[16];
156         ssize_t res;
157         /* its non-blocking, doesn't matter if we read too much */
158         res = read(sig_state->pipe_hack[0], c, sizeof(c));
159 }
160
161 /*
162   add a signal event
163   return NULL on failure (memory allocation error)
164 */
165 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
166                                                TALLOC_CTX *mem_ctx,
167                                                int signum,
168                                                int sa_flags,
169                                                tevent_signal_handler_t handler,
170                                                void *private_data,
171                                                const char *handler_name,
172                                                const char *location)
173 {
174         struct tevent_signal *se;
175         struct tevent_common_signal_list *sl;
176
177         if (signum >= NUM_SIGNALS) {
178                 errno = EINVAL;
179                 return NULL;
180         }
181
182         /* the sig_state needs to be on a global context as it can last across
183            multiple event contexts */
184         if (sig_state == NULL) {
185                 sig_state = talloc_zero(talloc_autofree_context(), struct sig_state);
186                 if (sig_state == NULL) {
187                         return NULL;
188                 }
189         }
190
191         se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
192         if (se == NULL) return NULL;
193
194         se->event_ctx           = ev;
195         se->signum              = signum;
196         se->sa_flags            = sa_flags;
197         se->handler             = handler;
198         se->private_data        = private_data;
199         se->handler_name        = handler_name;
200         se->location            = location;
201         se->additional_data     = NULL;
202
203         sl = talloc(se, struct tevent_common_signal_list);
204         if (!sl) {
205                 talloc_free(se);
206                 return NULL;
207         }
208         sl->se = se;
209         se->additional_data     = sl;
210
211         /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
212         if (!talloc_reference(se, sig_state)) {
213                 talloc_free(se);
214                 return NULL;
215         }
216
217         /* only install a signal handler if not already installed */
218         if (sig_state->sig_handlers[signum] == NULL) {
219                 struct sigaction act;
220                 ZERO_STRUCT(act);
221                 act.sa_handler = tevent_common_signal_handler;
222                 act.sa_flags = sa_flags;
223 #ifdef SA_SIGINFO
224                 if (sa_flags & SA_SIGINFO) {
225                         act.sa_handler   = NULL;
226                         act.sa_sigaction = tevent_common_signal_handler_info;
227                         if (sig_state->sig_info[signum] == NULL) {
228                                 sig_state->sig_info[signum] = talloc_array(sig_state, siginfo_t, SA_INFO_QUEUE_COUNT);
229                                 if (sig_state->sig_info[signum] == NULL) {
230                                         talloc_free(se);
231                                         return NULL;
232                                 }
233                         }
234                 }
235 #endif
236                 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
237                 if (sig_state->oldact[signum] == NULL) {
238                         talloc_free(se);
239                         return NULL;                    
240                 }
241                 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
242                         talloc_free(se);
243                         return NULL;
244                 }
245         }
246
247         DLIST_ADD(se->event_ctx->signal_events, se);
248         DLIST_ADD(sig_state->sig_handlers[signum], sl);
249
250         talloc_set_destructor(se, tevent_signal_destructor);
251         talloc_set_destructor(sl, tevent_common_signal_list_destructor);
252
253         /* we need to setup the pipe hack handler if not already
254            setup */
255         if (ev->pipe_fde == NULL) {
256                 if (sig_state->pipe_hack[0] == 0 && 
257                     sig_state->pipe_hack[1] == 0) {
258                         if (pipe(sig_state->pipe_hack) == -1) {
259                                 talloc_free(se);
260                                 return NULL;
261                         }
262                         ev_set_blocking(sig_state->pipe_hack[0], false);
263                         ev_set_blocking(sig_state->pipe_hack[1], false);
264                 }
265                 ev->pipe_fde = tevent_add_fd(ev, ev, sig_state->pipe_hack[0],
266                                              TEVENT_FD_READ, signal_pipe_handler, NULL);
267                 if (!ev->pipe_fde) {
268                         talloc_free(se);
269                         return NULL;
270                 }
271         }
272
273         return se;
274 }
275
276
277 /*
278   check if a signal is pending
279   return != 0 if a signal was pending
280 */
281 int tevent_common_check_signal(struct tevent_context *ev)
282 {
283         int i;
284
285         if (!sig_state || !SIG_PENDING(sig_state->got_signal)) {
286                 return 0;
287         }
288         
289         for (i=0;i<NUM_SIGNALS+1;i++) {
290                 struct tevent_common_signal_list *sl, *next;
291                 struct sigcounter counter = sig_state->signal_count[i];
292                 uint32_t count = sig_count(counter);
293
294                 if (count == 0) {
295                         continue;
296                 }
297                 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
298                         struct tevent_signal *se = sl->se;
299                         next = sl->next;
300 #ifdef SA_SIGINFO
301                         if (se->sa_flags & SA_SIGINFO) {
302                                 int j;
303                                 for (j=0;j<count;j++) {
304                                         /* note the use of the sig_info array as a
305                                            ring buffer */
306                                         int ofs = ((count-1) + j) % SA_INFO_QUEUE_COUNT;
307                                         se->handler(ev, se, i, 1, 
308                                                     (void*)&sig_state->sig_info[i][ofs], 
309                                                     se->private_data);
310                                 }
311                                 if (SIG_PENDING(sig_state->sig_blocked[i])) {
312                                         /* we'd filled the queue, unblock the
313                                            signal now */
314                                         sigset_t set;
315                                         sigemptyset(&set);
316                                         sigaddset(&set, i);
317                                         SIG_SEEN(sig_state->sig_blocked[i], 
318                                                  sig_count(sig_state->sig_blocked[i]));
319                                         sigprocmask(SIG_UNBLOCK, &set, NULL);
320                                 }
321                                 if (se->sa_flags & SA_RESETHAND) {
322                                         talloc_free(se);
323                                 }
324                                 continue;
325                         }
326 #endif
327                         se->handler(ev, se, i, count, NULL, se->private_data);
328                         if (se->sa_flags & SA_RESETHAND) {
329                                 talloc_free(se);
330                         }
331                 }
332                 SIG_SEEN(sig_state->signal_count[i], count);
333                 SIG_SEEN(sig_state->got_signal, count);
334         }
335
336         return 1;
337 }