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