2 Unix SMB/CIFS implementation.
4 common events code for signal events
6 Copyright (C) Andrew Tridgell 2007
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.
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.
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/>.
23 #include "system/filesys.h"
24 #include "system/wait.h"
26 #include "tevent_internal.h"
27 #include "tevent_util.h"
29 #define NUM_SIGNALS 64
31 /* maximum number of SA_SIGINFO signals to hold in the queue */
32 #define SA_INFO_QUEUE_COUNT 100
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)
43 struct tevent_common_signal_list {
44 struct tevent_common_signal_list *prev, *next;
45 struct tevent_signal *se;
49 the poor design of signals means that this table must be static global
51 static struct sig_state {
52 struct tevent_common_signal_list *sig_handlers[NUM_SIGNALS+1];
53 struct sigaction *oldact[NUM_SIGNALS+1];
54 struct sigcounter signal_count[NUM_SIGNALS+1];
55 struct sigcounter got_signal;
58 /* with SA_SIGINFO we get quite a lot of info per signal */
59 siginfo_t *sig_info[NUM_SIGNALS+1];
60 struct sigcounter sig_blocked[NUM_SIGNALS+1];
65 return number of sigcounter events not processed yet
67 static uint32_t sig_count(struct sigcounter s)
69 if (s.count >= s.seen) {
70 return s.count - s.seen;
72 return 1 + (0xFFFFFFFF & ~(s.seen - s.count));
76 signal handler - redirects to registered signals
78 static void tevent_common_signal_handler(int signum)
81 SIG_INCREMENT(sig_state->signal_count[signum]);
82 SIG_INCREMENT(sig_state->got_signal);
83 /* doesn't matter if this pipe overflows */
84 write(sig_state->pipe_hack[1], &c, 1);
89 signal handler with SA_SIGINFO - redirects to registered signals
91 static void tevent_common_signal_handler_info(int signum, siginfo_t *info,
94 uint32_t count = sig_count(sig_state->signal_count[signum]);
95 sig_state->sig_info[signum][count] = *info;
97 tevent_common_signal_handler(signum);
99 /* handle SA_SIGINFO */
100 if (count+1 == SA_INFO_QUEUE_COUNT) {
101 /* we've filled the info array - block this signal until
102 these ones are delivered */
105 sigaddset(&set, signum);
106 sigprocmask(SIG_BLOCK, &set, NULL);
107 SIG_INCREMENT(sig_state->sig_blocked[signum]);
112 static int tevent_common_signal_list_destructor(struct tevent_common_signal_list *sl)
114 DLIST_REMOVE(sig_state->sig_handlers[sl->se->signum], sl);
119 destroy a signal event
121 static int tevent_signal_destructor(struct tevent_signal *se)
123 struct tevent_common_signal_list *sl;
124 sl = talloc_get_type(se->additional_data,
125 struct tevent_common_signal_list);
128 DLIST_REMOVE(se->event_ctx->signal_events, se);
133 if (sig_state->sig_handlers[se->signum] == NULL) {
134 /* restore old handler, if any */
135 sigaction(se->signum, sig_state->oldact[se->signum], NULL);
136 sig_state->oldact[se->signum] = NULL;
138 if (se->sa_flags & SA_SIGINFO) {
139 talloc_free(sig_state->sig_info[se->signum]);
140 sig_state->sig_info[se->signum] = NULL;
149 this is part of the pipe hack needed to avoid the signal race condition
151 static void signal_pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
152 uint16_t flags, void *private)
155 /* its non-blocking, doesn't matter if we read too much */
156 read(sig_state->pipe_hack[0], c, sizeof(c));
161 return NULL on failure (memory allocation error)
163 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
167 tevent_signal_handler_t handler,
169 const char *handler_name,
170 const char *location)
172 struct tevent_signal *se;
173 struct tevent_common_signal_list *sl;
175 if (signum >= NUM_SIGNALS) {
180 /* the sig_state needs to be on a global context as it can last across
181 multiple event contexts */
182 if (sig_state == NULL) {
183 sig_state = talloc_zero(talloc_autofree_context(), struct sig_state);
184 if (sig_state == NULL) {
189 se = talloc(mem_ctx?mem_ctx:ev, struct tevent_signal);
190 if (se == NULL) return NULL;
194 se->sa_flags = sa_flags;
195 se->handler = handler;
196 se->private_data = private_data;
197 se->handler_name = handler_name;
198 se->location = location;
199 se->additional_data = NULL;
201 sl = talloc(se, struct tevent_common_signal_list);
207 se->additional_data = sl;
209 /* Ensure, no matter the destruction order, that we always have a handle on the global sig_state */
210 if (!talloc_reference(se, sig_state)) {
215 /* only install a signal handler if not already installed */
216 if (sig_state->sig_handlers[signum] == NULL) {
217 struct sigaction act;
219 act.sa_handler = tevent_common_signal_handler;
220 act.sa_flags = sa_flags;
222 if (sa_flags & SA_SIGINFO) {
223 act.sa_handler = NULL;
224 act.sa_sigaction = tevent_common_signal_handler_info;
225 if (sig_state->sig_info[signum] == NULL) {
226 sig_state->sig_info[signum] = talloc_array(sig_state, siginfo_t, SA_INFO_QUEUE_COUNT);
227 if (sig_state->sig_info[signum] == NULL) {
234 sig_state->oldact[signum] = talloc(sig_state, struct sigaction);
235 if (sig_state->oldact[signum] == NULL) {
239 if (sigaction(signum, &act, sig_state->oldact[signum]) == -1) {
245 DLIST_ADD(se->event_ctx->signal_events, se);
246 DLIST_ADD(sig_state->sig_handlers[signum], sl);
248 talloc_set_destructor(se, tevent_signal_destructor);
249 talloc_set_destructor(sl, tevent_common_signal_list_destructor);
251 /* we need to setup the pipe hack handler if not already
253 if (ev->pipe_fde == NULL) {
254 if (sig_state->pipe_hack[0] == 0 &&
255 sig_state->pipe_hack[1] == 0) {
256 pipe(sig_state->pipe_hack);
257 ev_set_blocking(sig_state->pipe_hack[0], false);
258 ev_set_blocking(sig_state->pipe_hack[1], false);
260 ev->pipe_fde = tevent_add_fd(ev, ev, sig_state->pipe_hack[0],
261 TEVENT_FD_READ, signal_pipe_handler, NULL);
273 check if a signal is pending
274 return != 0 if a signal was pending
276 int tevent_common_check_signal(struct tevent_context *ev)
280 if (!sig_state || !SIG_PENDING(sig_state->got_signal)) {
284 for (i=0;i<NUM_SIGNALS+1;i++) {
285 struct tevent_common_signal_list *sl, *next;
286 struct sigcounter counter = sig_state->signal_count[i];
287 uint32_t count = sig_count(counter);
292 for (sl=sig_state->sig_handlers[i];sl;sl=next) {
293 struct tevent_signal *se = sl->se;
296 if (se->sa_flags & SA_SIGINFO) {
298 for (j=0;j<count;j++) {
299 /* note the use of the sig_info array as a
301 int ofs = ((count-1) + j) % SA_INFO_QUEUE_COUNT;
302 se->handler(ev, se, i, 1,
303 (void*)&sig_state->sig_info[i][ofs],
306 if (SIG_PENDING(sig_state->sig_blocked[i])) {
307 /* we'd filled the queue, unblock the
312 SIG_SEEN(sig_state->sig_blocked[i],
313 sig_count(sig_state->sig_blocked[i]));
314 sigprocmask(SIG_UNBLOCK, &set, NULL);
316 if (se->sa_flags & SA_RESETHAND) {
322 se->handler(ev, se, i, count, NULL, se->private_data);
323 if (se->sa_flags & SA_RESETHAND) {
327 SIG_SEEN(sig_state->signal_count[i], count);
328 SIG_SEEN(sig_state->got_signal, count);