r23792: convert Samba4 to GPLv3
[jelmer/samba4-debian.git] / source / lib / events / events_liboop.c
1 /* 
2    Unix SMB/CIFS implementation.
3    main select loop and event handling
4    wrapper for http://liboop.org/
5
6    Copyright (C) Stefan Metzmacher 2005
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 "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/events/events_internal.h"
25
26 #include <oop.h>
27
28 /*
29  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
30
31  NOTE: this code compiles fine, but is completly *UNTESTED*
32        and is only commited as example
33
34  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   
35 */
36
37 static int oop_event_context_destructor(struct event_context *ev)
38 {
39         oop_source_sys *oop_sys = ev->additional_data;
40
41         oop_sys_delete(oop_sys);
42
43         return 0;
44 }
45
46 /*
47   create a oop_event_context structure.
48 */
49 static int oop_event_context_init(struct event_context *ev, void *private_data)
50 {
51         oop_source_sys *oop_sys = private_data;
52
53         if (!oop_sys) {
54                 oop_sys = oop_sys_new();
55                 if (!oop_sys) {
56                         return -1;
57                 }
58
59                 talloc_set_destructor(ev, oop_event_context_destructor);
60         }
61
62         ev->additional_data = oop_sys;
63
64         return 0;
65 }
66
67 static void *oop_event_fd_handler(oop_source *oop, int fd, oop_event oop_type, void *ptr)
68 {
69         struct fd_event *fde = ptr;
70
71         if (fd != fde->fd) return OOP_ERROR;
72
73         switch(oop_type) {
74                 case OOP_READ:
75                         fde->handler(fde->event_ctx, fde, EVENT_FD_READ, fde->private_data);
76                         return OOP_CONTINUE;
77                 case OOP_WRITE:
78                         fde->handler(fde->event_ctx, fde, EVENT_FD_WRITE, fde->private_data);
79                         return OOP_CONTINUE;                    
80                 case OOP_EXCEPTION:
81                         return OOP_ERROR;
82                 case OOP_NUM_EVENTS:
83                         return OOP_ERROR;
84         }
85
86         return OOP_ERROR;
87 }
88
89 /*
90   destroy an fd_event
91 */
92 static int oop_event_fd_destructor(struct fd_event *fde)
93 {
94         struct event_context *ev = fde->event_ctx;
95         oop_source_sys *oop_sys = ev->additional_data;
96         oop_source *oop = oop_sys_source(oop_sys);
97
98         if (fde->flags & EVENT_FD_READ)
99                 oop->cancel_fd(oop, fde->fd, OOP_READ);
100         if (fde->flags & EVENT_FD_WRITE)
101                 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
102
103         if (fde->flags & EVENT_FD_AUTOCLOSE) {
104                 close(fde->fd);
105                 fde->fd = -1;
106         }
107
108         return 0;
109 }
110
111 /*
112   add a fd based event
113   return NULL on failure (memory allocation error)
114 */
115 static struct fd_event *oop_event_add_fd(struct event_context *ev, TALLOC_CTX *mem_ctx,
116                                          int fd, uint16_t flags,
117                                          event_fd_handler_t handler,
118                                          void *private_data)
119 {
120         struct fd_event *fde;
121         oop_source_sys *oop_sys = ev->additional_data;
122         oop_source *oop = oop_sys_source(oop_sys);
123         
124         fde = talloc(mem_ctx?mem_ctx:ev, struct fd_event);
125         if (!fde) return NULL;
126
127         fde->event_ctx          = ev;
128         fde->fd                 = fd;
129         fde->flags              = flags;
130         fde->handler            = handler;
131         fde->private_data       = private_data;
132         fde->additional_flags   = 0;
133         fde->additional_data    = NULL;
134
135         if (fde->flags & EVENT_FD_READ)
136                 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
137         if (fde->flags & EVENT_FD_WRITE)
138                 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
139
140         talloc_set_destructor(fde, oop_event_fd_destructor);
141
142         return fde;
143 }
144
145 /*
146   return the fd event flags
147 */
148 static uint16_t oop_event_get_fd_flags(struct fd_event *fde)
149 {
150         return fde->flags;
151 }
152
153 /*
154   set the fd event flags
155 */
156 static void oop_event_set_fd_flags(struct fd_event *fde, uint16_t flags)
157 {
158         oop_source_sys *oop_sys;
159         oop_source *oop;
160
161         oop_sys = fde->event_ctx->additional_data;
162         oop = oop_sys_source(oop_sys);
163
164         if ((fde->flags & EVENT_FD_READ)&&(!(flags & EVENT_FD_READ)))
165                 oop->cancel_fd(oop, fde->fd, OOP_READ);
166
167         if ((!(fde->flags & EVENT_FD_READ))&&(flags & EVENT_FD_READ))
168                 oop->on_fd(oop, fde->fd, OOP_READ, oop_event_fd_handler, fde);
169
170         if ((fde->flags & EVENT_FD_WRITE)&&(!(flags & EVENT_FD_WRITE)))
171                 oop->cancel_fd(oop, fde->fd, OOP_WRITE);
172
173         if ((!(fde->flags & EVENT_FD_WRITE))&&(flags & EVENT_FD_WRITE))
174                 oop->on_fd(oop, fde->fd, OOP_WRITE, oop_event_fd_handler, fde);
175
176         fde->flags = flags;
177 }
178
179 static int oop_event_timed_destructor(struct timed_event *te);
180
181 static int oop_event_timed_deny_destructor(struct timed_event *te)
182 {
183         return -1;
184 }
185
186 static void *oop_event_timed_handler(oop_source *oop, struct timeval t, void *ptr)
187 {
188         struct timed_event *te = ptr;
189
190         /* deny the handler to free the event */
191         talloc_set_destructor(te, oop_event_timed_deny_destructor);
192         te->handler(te->event_ctx, te, t, te->private_data);
193
194         talloc_set_destructor(te, oop_event_timed_destructor);
195         talloc_free(te);
196
197         return OOP_CONTINUE;
198 }
199
200 /*
201   destroy a timed event
202 */
203 static int oop_event_timed_destructor(struct timed_event *te)
204 {
205         struct event_context *ev = te->event_ctx;
206         oop_source_sys *oop_sys = ev->additional_data;
207         oop_source *oop = oop_sys_source(oop_sys);
208
209         oop->cancel_time(oop, te->next_event, oop_event_timed_handler, te);
210
211         return 0;
212 }
213
214 /*
215   add a timed event
216   return NULL on failure (memory allocation error)
217 */
218 static struct timed_event *oop_event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
219                                                struct timeval next_event, 
220                                                event_timed_handler_t handler, 
221                                                void *private_data) 
222 {
223         oop_source_sys *oop_sys = ev->additional_data;
224         oop_source *oop = oop_sys_source(oop_sys);
225         struct timed_event *te;
226
227         te = talloc(mem_ctx?mem_ctx:ev, struct timed_event);
228         if (te == NULL) return NULL;
229
230         te->event_ctx           = ev;
231         te->next_event          = next_event;
232         te->handler             = handler;
233         te->private_data        = private_data;
234         te->additional_data     = NULL;
235
236         oop->on_time(oop, te->next_event, oop_event_timed_handler, te);
237
238         talloc_set_destructor(te, oop_event_timed_destructor);
239
240         return te;
241 }
242
243 /*
244   do a single event loop using the events defined in ev 
245 */
246 static int oop_event_loop_once(struct event_context *ev)
247 {
248         void *oop_ret;
249         oop_source_sys *oop_sys = ev->additional_data;
250
251         oop_ret = oop_sys_run_once(oop_sys);
252         if (oop_ret == OOP_CONTINUE) {
253                 return 0;
254         }
255
256         return -1;
257 }
258
259 /*
260   return on failure or (with 0) if all fd events are removed
261 */
262 static int oop_event_loop_wait(struct event_context *ev)
263 {
264         void *oop_ret;
265         oop_source_sys *oop_sys = ev->additional_data;
266
267         oop_ret = oop_sys_run(oop_sys);
268         if (oop_ret == OOP_CONTINUE) {
269                 return 0;
270         }
271
272         return -1;
273 }
274
275 static const struct event_ops event_oop_ops = {
276         .context_init   = oop_event_context_init,
277         .add_fd         = oop_event_add_fd,
278         .get_fd_flags   = oop_event_get_fd_flags,
279         .set_fd_flags   = oop_event_set_fd_flags,
280         .add_timed      = oop_event_add_timed,
281         .add_signal     = common_event_add_signal,
282         .loop_once      = oop_event_loop_once,
283         .loop_wait      = oop_event_loop_wait,
284 };
285
286 const struct event_ops *event_liboop_get_ops(void)
287 {
288         return &event_oop_ops;
289 }