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