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