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