Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[metze/samba/wip.git] / source / lib / events / events_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 #if _SAMBA_BUILD_
23 #include "includes.h"
24 #include "lib/util/dlinklist.h"
25 #else
26 #include <signal.h>
27 #include "replace.h"
28 #include "events_util.h"
29 #endif
30 #include "system/filesys.h"
31 #include "system/select.h"
32 #include "events.h"
33 #include "events_internal.h"
34
35 #define NUM_SIGNALS 64
36
37 /* maximum number of SA_SIGINFO signals to hold in the queue */
38 #define SA_INFO_QUEUE_COUNT 10
39
40 struct sigcounter {
41         uint32_t count;
42         uint32_t seen;
43 };
44
45 #define SIG_INCREMENT(s) (s).count++
46 #define SIG_SEEN(s, n) (s).seen += (n)
47 #define SIG_PENDING(s) ((s).seen != (s).count)
48
49
50 /*
51   the poor design of signals means that this table must be static global
52 */
53 static struct sig_state {
54         struct signal_event *sig_handlers[NUM_SIGNALS];
55         struct sigaction *oldact[NUM_SIGNALS];
56         struct sigcounter signal_count[NUM_SIGNALS];
57         struct sigcounter got_signal;
58         int pipe_hack[2];
59 #ifdef SA_SIGINFO
60         /* with SA_SIGINFO we get quite a lot of info per signal */
61         siginfo_t *sig_info[NUM_SIGNALS];
62         struct sigcounter sig_blocked[NUM_SIGNALS];
63 #endif
64 } *sig_state;
65
66 /*
67   return number of sigcounter events not processed yet
68 */
69 static uint32_t sig_count(struct sigcounter s)
70 {
71         if (s.count >= s.seen) {
72                 return s.count - s.seen;
73         }
74         return 1 + (0xFFFFFFFF & ~(s.seen - s.count));
75 }
76
77 /*
78   signal handler - redirects to registered signals
79 */
80 static void signal_handler(int signum)
81 {
82         char c = 0;
83         SIG_INCREMENT(sig_state->signal_count[signum]);
84         SIG_INCREMENT(sig_state->got_signal);
85         /* doesn't matter if this pipe overflows */
86         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 signal_handler_info(int signum, siginfo_t *info, void *uctx)
94 {
95         uint32_t count = sig_count(sig_state->signal_count[signum]);
96         sig_state->sig_info[signum][count] = *info;
97
98         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 /*
114   destroy a signal event
115 */
116 static int signal_event_destructor(struct signal_event *se)
117 {
118         se->event_ctx->num_signal_handlers--;
119         DLIST_REMOVE(sig_state->sig_handlers[se->signum], se);
120         if (sig_state->sig_handlers[se->signum] == NULL) {
121                 /* restore old handler, if any */
122                 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
123                 sig_state->oldact[se->signum] = NULL;
124 #ifdef SA_SIGINFO
125                 if (se->sa_flags & SA_SIGINFO) {
126                         talloc_free(sig_state->sig_info[se->signum]);
127                         sig_state->sig_info[se->signum] = NULL;
128                 }
129 #endif
130         }
131         return 0;
132 }
133
134 /*
135   this is part of the pipe hack needed to avoid the signal race condition
136 */
137 static void signal_pipe_handler(struct event_context *ev, struct fd_event *fde, 
138                                 uint16_t flags, void *private)
139 {
140         char c[16];
141         /* its non-blocking, doesn't matter if we read too much */
142         read(sig_state->pipe_hack[0], c, sizeof(c));
143 }
144
145 /*
146   add a signal event
147   return NULL on failure (memory allocation error)
148 */
149 struct signal_event *common_event_add_signal(struct event_context *ev, 
150                                              TALLOC_CTX *mem_ctx,
151                                              int signum,
152                                              int sa_flags,
153                                              event_signal_handler_t handler, 
154                                              void *private_data) 
155 {
156         struct signal_event *se;
157
158         if (signum >= NUM_SIGNALS) {
159                 return NULL;
160         }
161
162         /* the sig_state needs to be on a global context as it can last across
163            multiple event contexts */
164         if (sig_state == NULL) {
165                 sig_state = talloc_zero(talloc_autofree_context(), struct sig_state);
166                 if (sig_state == NULL) {
167                         return NULL;
168                 }
169         }
170
171         se = talloc(mem_ctx?mem_ctx:ev, struct signal_event);
172         if (se == NULL) return NULL;
173
174         se->event_ctx           = ev;
175         se->handler             = handler;
176         se->private_data        = private_data;
177         se->signum              = signum;
178         se->sa_flags            = sa_flags;
179         
180         /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
181         if (!talloc_reference(se, sig_state)) {
182                 return NULL;
183         }
184
185         /* only install a signal handler if not already installed */
186         if (sig_state->sig_handlers[signum] == NULL) {
187                 struct sigaction act;
188                 ZERO_STRUCT(act);
189                 act.sa_handler   = signal_handler;
190                 act.sa_flags = sa_flags;
191 #ifdef SA_SIGINFO
192                 if (sa_flags & SA_SIGINFO) {
193                         act.sa_handler   = NULL;
194                         act.sa_sigaction = signal_handler_info;
195                         if (sig_state->sig_info[signum] == NULL) {
196                                 sig_state->sig_info[signum] = talloc_array(sig_state, siginfo_t, SA_INFO_QUEUE_COUNT);
197                                 if (sig_state->sig_info[signum] == NULL) {
198                                         talloc_free(se);
199                                         return NULL;
200                                 }
201                         }
202                 }
203 #endif
204                 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
205                 if (sig_state->oldact[signum] == NULL) {
206                         talloc_free(se);
207                         return NULL;                    
208                 }
209                 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
210                         talloc_free(se);
211                         return NULL;
212                 }
213         }
214
215         DLIST_ADD(sig_state->sig_handlers[signum], se);
216
217         talloc_set_destructor(se, signal_event_destructor);
218
219         /* we need to setup the pipe hack handler if not already
220            setup */
221         if (ev->pipe_fde == NULL) {
222                 if (sig_state->pipe_hack[0] == 0 && 
223                     sig_state->pipe_hack[1] == 0) {
224                         pipe(sig_state->pipe_hack);
225                         set_blocking(sig_state->pipe_hack[0], false);
226                         set_blocking(sig_state->pipe_hack[1], false);
227                 }
228                 ev->pipe_fde = event_add_fd(ev, ev, sig_state->pipe_hack[0],
229                                             EVENT_FD_READ, signal_pipe_handler, NULL);
230         }
231         ev->num_signal_handlers++;
232
233         return se;
234 }
235
236
237 /*
238   check if a signal is pending
239   return != 0 if a signal was pending
240 */
241 int common_event_check_signal(struct event_context *ev)
242 {
243         int i;
244
245         if (!sig_state || !SIG_PENDING(sig_state->got_signal)) {
246                 return 0;
247         }
248         
249         for (i=0;i<NUM_SIGNALS+1;i++) {
250                 struct signal_event *se, *next;
251                 struct sigcounter counter = sig_state->signal_count[i];
252                 uint32_t count = sig_count(counter);
253
254                 if (count == 0) {
255                         continue;
256                 }
257                 for (se=sig_state->sig_handlers[i];se;se=next) {
258                         next = se->next;
259 #ifdef SA_SIGINFO
260                         if (se->sa_flags & SA_SIGINFO) {
261                                 int j;
262                                 for (j=0;j<count;j++) {
263                                         /* note the use of the sig_info array as a
264                                            ring buffer */
265                                         int ofs = ((count-1) + j) % SA_INFO_QUEUE_COUNT;
266                                         se->handler(ev, se, i, 1, 
267                                                     (void*)&sig_state->sig_info[i][ofs], 
268                                                     se->private_data);
269                                 }
270                                 if (SIG_PENDING(sig_state->sig_blocked[i])) {
271                                         /* we'd filled the queue, unblock the
272                                            signal now */
273                                         sigset_t set;
274                                         sigemptyset(&set);
275                                         sigaddset(&set, i);
276                                         SIG_SEEN(sig_state->sig_blocked[i], 
277                                                  sig_count(sig_state->sig_blocked[i]));
278                                         sigprocmask(SIG_UNBLOCK, &set, NULL);
279                                 }
280                                 if (se->sa_flags & SA_RESETHAND) {
281                                         talloc_free(se);
282                                 }
283                                 continue;
284                         }
285 #endif
286                         se->handler(ev, se, i, count, NULL, se->private_data);
287                         if (se->sa_flags & SA_RESETHAND) {
288                                 talloc_free(se);
289                         }
290                 }
291                 SIG_SEEN(sig_state->signal_count[i], count);
292                 SIG_SEEN(sig_state->got_signal, count);
293         }
294
295         return 1;
296 }