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