r20938: use a double counter trick to avoid the need for atomic increment
[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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "system/select.h"
26 #include "lib/util/dlinklist.h"
27 #include "lib/events/events.h"
28 #include "lib/events/events_internal.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 {
49         struct signal_event *sig_handlers[NUM_SIGNALS];
50         struct sigaction oldact[NUM_SIGNALS];
51         struct sigcounter signal_count[NUM_SIGNALS];
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][SA_INFO_QUEUE_COUNT];
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         }
103 }
104 #endif
105
106 /*
107   destroy a signal event
108 */
109 static int signal_event_destructor(struct signal_event *se)
110 {
111         se->event_ctx->num_signal_handlers--;
112         DLIST_REMOVE(sig_state.sig_handlers[se->signum], se);
113         if (sig_state.sig_handlers[se->signum] == NULL) {
114                 /* restore old handler, if any */
115                 sigaction(se->signum, &sig_state.oldact[se->signum], NULL);
116         }
117         return 0;
118 }
119
120 /*
121   this is part of the pipe hack needed to avoid the signal race condition
122 */
123 static void signal_pipe_handler(struct event_context *ev, struct fd_event *fde, 
124                                 uint16_t flags, void *private)
125 {
126         char c[16];
127         /* its non-blocking, doesn't matter if we read too much */
128         read(sig_state.pipe_hack[0], c, sizeof(c));
129 }
130
131 /*
132   add a signal event
133   return NULL on failure (memory allocation error)
134 */
135 struct signal_event *common_event_add_signal(struct event_context *ev, 
136                                              TALLOC_CTX *mem_ctx,
137                                              int signum,
138                                              int sa_flags,
139                                              event_signal_handler_t handler, 
140                                              void *private_data) 
141 {
142         struct signal_event *se;
143
144         if (signum >= NUM_SIGNALS) {
145                 return NULL;
146         }
147
148         se = talloc(mem_ctx?mem_ctx:ev, struct signal_event);
149         if (se == NULL) return NULL;
150
151         se->event_ctx           = ev;
152         se->handler             = handler;
153         se->private_data        = private_data;
154         se->signum              = signum;
155         se->sa_flags            = sa_flags;
156
157         /* only install a signal handler if not already installed */
158         if (sig_state.sig_handlers[signum] == NULL) {
159                 struct sigaction act;
160                 ZERO_STRUCT(act);
161                 act.sa_handler   = signal_handler;
162                 act.sa_flags = sa_flags;
163 #ifdef SA_SIGINFO
164                 if (sa_flags & SA_SIGINFO) {
165                         act.sa_handler   = NULL;
166                         act.sa_sigaction = signal_handler_info;
167                 }
168 #endif
169                 if (sigaction(signum, &act, &sig_state.oldact[signum]) == -1) {
170                         talloc_free(se);
171                         return NULL;
172                 }
173         }
174
175         DLIST_ADD(sig_state.sig_handlers[signum], se);
176
177         talloc_set_destructor(se, signal_event_destructor);
178
179         /* we need to setup the pipe hack handler if not already
180            setup */
181         if (ev->pipe_fde == NULL) {
182                 if (sig_state.pipe_hack[0] == 0 && 
183                     sig_state.pipe_hack[1] == 0) {
184                         pipe(sig_state.pipe_hack);
185                         set_blocking(sig_state.pipe_hack[0], False);
186                         set_blocking(sig_state.pipe_hack[1], False);
187                 }
188                 ev->pipe_fde = event_add_fd(ev, ev, sig_state.pipe_hack[0],
189                                             EVENT_FD_READ, signal_pipe_handler, NULL);
190         }
191         ev->num_signal_handlers++;
192
193         return se;
194 }
195
196
197 /*
198   check if a signal is pending
199   return != 0 if a signal was pending
200 */
201 int common_event_check_signal(struct event_context *ev)
202 {
203         int i;
204         if (!SIG_PENDING(sig_state.got_signal)) {
205                 return 0;
206         }
207         
208         for (i=0;i<NUM_SIGNALS+1;i++) {
209                 struct signal_event *se, *next;
210                 uint32_t count = sig_count(sig_state.signal_count[i]);
211
212                 if (count == 0) {
213                         continue;
214                 }
215                 for (se=sig_state.sig_handlers[i];se;se=next) {
216                         next = se->next;
217 #ifdef SA_SIGINFO
218                         if (se->sa_flags & SA_SIGINFO) {
219                                 int j;
220                                 for (j=0;j<count;j++) {
221                                         se->handler(ev, se, i, 1, 
222                                                     (void*)&sig_state.sig_info[i][j], 
223                                                     se->private_data);
224                                 }
225                                 if (count == SA_INFO_QUEUE_COUNT) {
226                                         /* we'd filled the queue, unblock the
227                                            signal now */
228                                         sigset_t set;
229                                         sigemptyset(&set);
230                                         sigaddset(&set, i);
231                                         sigprocmask(SIG_UNBLOCK, &set, NULL);
232                                 }
233                                 continue;
234                         }
235 #endif
236                         se->handler(ev, se, i, count, NULL, se->private_data);
237                         if (se->sa_flags & SA_RESETHAND) {
238                                 talloc_free(se);
239                         }
240                 }
241                 SIG_SEEN(sig_state.signal_count[i], count);
242                 SIG_SEEN(sig_state.got_signal, count);
243         }
244
245         return 1;
246 }