r20928: added signal events to lib/events
[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 /*
33   the poor design of signals means that this table must be static global
34 */
35 static struct {
36         struct signal_event *sig_handlers[NUM_SIGNALS];
37         uint32_t signal_count[NUM_SIGNALS];
38         uint32_t got_signal;
39         int pipe_hack[2];
40 } sig_state;
41
42
43 /*
44   signal handler - redirects to registered signals
45 */
46 static void signal_handler(int signum)
47 {
48         char c = 0;
49         sig_state.signal_count[signum]++;
50         sig_state.got_signal++;
51         /* doesn't matter if this pipe overflows */
52         write(sig_state.pipe_hack[1], &c, 1);
53 }
54
55
56 /*
57   destroy a signal event
58 */
59 static int signal_event_destructor(struct signal_event *se)
60 {
61         se->event_ctx->num_signal_handlers--;
62         DLIST_REMOVE(sig_state.sig_handlers[se->signum], se);
63         if (sig_state.sig_handlers[se->signum] == NULL) {
64                 signal(se->signum, SIG_DFL);
65         }
66         return 0;
67 }
68
69 /*
70   this is part of the pipe hack needed to avoid the signal race condition
71 */
72 static void signal_pipe_handler(struct event_context *ev, struct fd_event *fde, 
73                                 uint16_t flags, void *private)
74 {
75         char c[16];
76         /* its non-blocking, doesn't matter if we read too much */
77         read(sig_state.pipe_hack[0], c, sizeof(c));
78 }
79
80 /*
81   add a signal event
82   return NULL on failure (memory allocation error)
83 */
84 struct signal_event *common_event_add_signal(struct event_context *ev, 
85                                             TALLOC_CTX *mem_ctx,
86                                             int signum,
87                                             event_signal_handler_t handler, 
88                                             void *private_data) 
89 {
90         struct signal_event *se;
91
92         if (signum >= NUM_SIGNALS) {
93                 return NULL;
94         }
95
96         se = talloc(mem_ctx?mem_ctx:ev, struct signal_event);
97         if (se == NULL) return NULL;
98
99         se->event_ctx           = ev;
100         se->handler             = handler;
101         se->private_data        = private_data;
102         se->signum              = signum;
103
104         if (sig_state.sig_handlers[signum] == NULL) {
105                 signal(signum, signal_handler);
106         }
107
108         DLIST_ADD(sig_state.sig_handlers[signum], se);
109
110         talloc_set_destructor(se, signal_event_destructor);
111
112         if (ev->pipe_fde == NULL) {
113                 if (sig_state.pipe_hack[0] == 0 && 
114                     sig_state.pipe_hack[1] == 0) {
115                         pipe(sig_state.pipe_hack);
116                         set_blocking(sig_state.pipe_hack[0], False);
117                         set_blocking(sig_state.pipe_hack[1], False);
118                 }
119                 ev->pipe_fde = event_add_fd(ev, ev, sig_state.pipe_hack[0],
120                                             EVENT_FD_READ, signal_pipe_handler, NULL);
121         }
122         ev->num_signal_handlers++;
123
124         return se;
125 }
126
127
128 /*
129   check if a signal is pending
130   return != 0 if a signal was pending
131 */
132 int common_event_check_signal(struct event_context *ev)
133 {
134         int i;
135         if (sig_state.got_signal == 0) {
136                 return 0;
137         }
138         
139         for (i=0;i<NUM_SIGNALS+1;i++) {
140                 uint32_t count = sig_state.signal_count[i];
141                 if (count != 0) {
142                         struct signal_event *se, *next;
143                         for (se=sig_state.sig_handlers[i];se;se=next) {
144                                 next = se->next;
145                                 se->handler(ev, se, i, count, se->private_data);
146                         }
147                         sig_state.signal_count[i] -= count;
148                         sig_state.got_signal -= count;
149                 }
150         }
151
152         return 1;
153 }