Merge branch 'master' of ssh://git.samba.org/data/git/samba
[metze/samba/wip.git] / source3 / lib / events.c
1 /*
2    Unix SMB/CIFS implementation.
3    Timed event library.
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Volker Lendecke 2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include <tevent_internal.h>
23
24 struct s3_event_context {
25         struct tevent_context *ev;
26         struct tevent_fd *fd_events;
27 };
28
29 static int s3_event_timer_destructor(struct tevent_timer *te)
30 {
31         DEBUG(10, ("Destroying timer event %p \"%s\"\n",
32                   te, te->handler_name));
33         if (te->event_ctx != NULL) {
34                 DLIST_REMOVE(te->event_ctx->timer_events, te);
35         }
36         return 0;
37 }
38
39 /****************************************************************************
40  Add te by time.
41 ****************************************************************************/
42
43 static void add_event_by_time(struct tevent_timer *te)
44 {
45         struct tevent_context *ctx = te->event_ctx;
46         struct tevent_timer *last_te, *cur_te;
47
48         /* Keep the list ordered by time. We must preserve this. */
49         last_te = NULL;
50         for (cur_te = ctx->timer_events; cur_te; cur_te = cur_te->next) {
51                 /* if the new event comes before the current one break */
52                 if (!timeval_is_zero(&cur_te->next_event) &&
53                     timeval_compare(&te->next_event, &cur_te->next_event) < 0) {
54                         break;
55                 }
56                 last_te = cur_te;
57         }
58
59         DLIST_ADD_AFTER(ctx->timer_events, te, last_te);
60 }
61
62 /****************************************************************************
63  Schedule a function for future calling, cancel with TALLOC_FREE().
64  It's the responsibility of the handler to call TALLOC_FREE() on the event
65  handed to it.
66 ****************************************************************************/
67
68 static struct tevent_timer *s3_event_add_timer(struct tevent_context *event_ctx,
69                                                TALLOC_CTX *mem_ctx,
70                                                struct timeval when,
71                                                tevent_timer_handler_t handler,
72                                                void *private_data,
73                                                const char *handler_name,
74                                                const char *location)
75 {
76         struct tevent_timer *te;
77
78         te = TALLOC_P(mem_ctx, struct tevent_timer);
79         if (te == NULL) {
80                 DEBUG(0, ("talloc failed\n"));
81                 return NULL;
82         }
83
84         te->event_ctx = event_ctx;
85         te->next_event = when;
86         te->handler = handler;
87         te->private_data = private_data;
88         te->handler_name = handler_name;
89         te->location = location;
90         te->additional_data = NULL;
91
92         add_event_by_time(te);
93
94         talloc_set_destructor(te, s3_event_timer_destructor);
95
96         DEBUG(10, ("Added timed event \"%s\": %p\n", handler_name, te));
97         return te;
98 }
99
100 static int s3_event_fd_destructor(struct tevent_fd *fde)
101 {
102         if (fde->event_ctx != NULL) {
103                 struct s3_event_context *ev3;
104                 ev3 = talloc_get_type(fde->event_ctx->additional_data,
105                                       struct s3_event_context);
106                 DLIST_REMOVE(ev3->fd_events, fde);
107         }
108         if (fde->close_fn) {
109                 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
110                 fde->fd = -1;
111         }
112         return 0;
113 }
114
115 static struct tevent_fd *s3_event_add_fd(struct tevent_context *ev,
116                                          TALLOC_CTX *mem_ctx,
117                                          int fd,
118                                          uint16_t flags,
119                                          tevent_fd_handler_t handler,
120                                          void *private_data,
121                                          const char *handler_name,
122                                          const char *location)
123 {
124         struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
125                                                        struct s3_event_context);
126         struct tevent_fd *fde;
127
128         if (!(fde = TALLOC_P(mem_ctx, struct tevent_fd))) {
129                 return NULL;
130         }
131
132         fde->event_ctx = ev;
133         fde->fd = fd;
134         fde->flags = flags;
135         fde->handler = handler;
136         fde->close_fn = NULL;
137         fde->private_data = private_data;
138         fde->handler_name = handler_name;
139         fde->location = location;
140
141         DLIST_ADD(ev3->fd_events, fde);
142
143         talloc_set_destructor(fde, s3_event_fd_destructor);
144         return fde;
145 }
146
147 void event_fd_set_writeable(struct tevent_fd *fde)
148 {
149         TEVENT_FD_WRITEABLE(fde);
150 }
151
152 void event_fd_set_not_writeable(struct tevent_fd *fde)
153 {
154         TEVENT_FD_NOT_WRITEABLE(fde);
155 }
156
157 void event_fd_set_readable(struct tevent_fd *fde)
158 {
159         TEVENT_FD_READABLE(fde);
160 }
161
162 void event_fd_set_not_readable(struct tevent_fd *fde)
163 {
164         TEVENT_FD_NOT_READABLE(fde);
165 }
166
167 /*
168  * Return if there's something in the queue
169  */
170
171 bool event_add_to_select_args(struct tevent_context *ev,
172                               const struct timeval *now,
173                               fd_set *read_fds, fd_set *write_fds,
174                               struct timeval *timeout, int *maxfd)
175 {
176         struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
177                                                        struct s3_event_context);
178         struct tevent_fd *fde;
179         struct timeval diff;
180         bool ret = false;
181
182         for (fde = ev3->fd_events; fde; fde = fde->next) {
183                 if (fde->flags & EVENT_FD_READ) {
184                         FD_SET(fde->fd, read_fds);
185                         ret = true;
186                 }
187                 if (fde->flags & EVENT_FD_WRITE) {
188                         FD_SET(fde->fd, write_fds);
189                         ret = true;
190                 }
191
192                 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
193                     && (fde->fd > *maxfd)) {
194                         *maxfd = fde->fd;
195                 }
196         }
197
198         if (ev->timer_events == NULL) {
199                 return ret;
200         }
201
202         diff = timeval_until(now, &ev->timer_events->next_event);
203         *timeout = timeval_min(timeout, &diff);
204
205         return true;
206 }
207
208 bool run_events(struct tevent_context *ev,
209                 int selrtn, fd_set *read_fds, fd_set *write_fds)
210 {
211         struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
212                                                        struct s3_event_context);
213         bool fired = false;
214         struct tevent_fd *fde, *next;
215
216         /* Run all events that are pending, not just one (as we
217            did previously. */
218
219         while (ev->timer_events) {
220                 struct timeval now;
221                 GetTimeOfDay(&now);
222
223                 if (timeval_compare(
224                             &now, &ev->timer_events->next_event) < 0) {
225                         /* Nothing to do yet */
226                         DEBUG(11, ("run_events: Nothing to do\n"));
227                         break;
228                 }
229
230                 DEBUG(10, ("Running event \"%s\" %p\n",
231                            ev->timer_events->handler_name,
232                            ev->timer_events));
233
234                 ev->timer_events->handler(
235                         ev,
236                         ev->timer_events, now,
237                         ev->timer_events->private_data);
238
239                 fired = true;
240         }
241
242         if (fired) {
243                 /*
244                  * We might have changed the socket status during the timed
245                  * events, return to run select again.
246                  */
247                 return true;
248         }
249
250         if (selrtn == 0) {
251                 /*
252                  * No fd ready
253                  */
254                 return fired;
255         }
256
257         for (fde = ev3->fd_events; fde; fde = next) {
258                 uint16 flags = 0;
259
260                 next = fde->next;
261                 if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
262                 if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
263
264                 if (flags & fde->flags) {
265                         fde->handler(ev, fde, flags, fde->private_data);
266                         fired = true;
267                 }
268         }
269
270         return fired;
271 }
272
273
274 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
275                                          struct timeval *to_ret)
276 {
277         struct timeval now;
278
279         if (ev->timer_events == NULL) {
280                 return NULL;
281         }
282
283         now = timeval_current();
284         *to_ret = timeval_until(&now, &ev->timer_events->next_event);
285
286         DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
287                 (int)to_ret->tv_usec));
288
289         return to_ret;
290 }
291
292 static int s3_event_loop_once(struct tevent_context *ev)
293 {
294         struct timeval now, to;
295         fd_set r_fds, w_fds;
296         int maxfd = 0;
297         int ret;
298
299         FD_ZERO(&r_fds);
300         FD_ZERO(&w_fds);
301
302         to.tv_sec = 9999;       /* Max timeout */
303         to.tv_usec = 0;
304
305         GetTimeOfDay(&now);
306
307         if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
308                 return -1;
309         }
310
311         if (timeval_is_zero(&to)) {
312                 run_events(ev, 0, NULL, NULL);
313                 return 0;
314         }
315
316         ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
317
318         if (ret == -1 && errno != EINTR) {
319                 return -1;
320         }
321
322         run_events(ev, ret, &r_fds, &w_fds);
323         return 0;
324 }
325
326 static int s3_event_loop_wait(struct tevent_context *ev)
327 {
328         int ret = 0;
329
330         while (ret == 0) {
331                 ret = s3_event_loop_once(ev);
332         }
333
334         return ret;
335 }
336
337 static int s3_event_context_destructor(struct tevent_context *ev)
338 {
339         struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
340                                                        struct s3_event_context);
341         while (ev3->fd_events != NULL) {
342                 ev3->fd_events->event_ctx = NULL;
343                 DLIST_REMOVE(ev3->fd_events, ev3->fd_events);
344         }
345         while (ev->timer_events != NULL) {
346                 ev->timer_events->event_ctx = NULL;
347                 DLIST_REMOVE(ev->timer_events, ev3->ev->timer_events);
348         }
349         return 0;
350 }
351
352 void event_context_reinit(struct tevent_context *ev)
353 {
354         s3_event_context_destructor(ev);
355         return;
356 }
357
358 static int s3_event_context_init(struct tevent_context *ev)
359 {
360         struct s3_event_context *ev3;
361
362         ev3 = talloc_zero(ev, struct s3_event_context);
363         if (!ev3) return -1;
364         ev3->ev = ev;
365
366         ev->additional_data = ev3;
367         talloc_set_destructor(ev, s3_event_context_destructor);
368         return 0;
369 }
370
371 void dump_event_list(struct tevent_context *ev)
372 {
373         struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
374                                                        struct s3_event_context);
375         struct tevent_timer *te;
376         struct tevent_fd *fe;
377         struct timeval evt, now;
378
379         if (!ev) {
380                 return;
381         }
382
383         now = timeval_current();
384
385         DEBUG(10,("dump_event_list:\n"));
386
387         for (te = ev->timer_events; te; te = te->next) {
388
389                 evt = timeval_until(&now, &te->next_event);
390
391                 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
392                            te->handler_name,
393                            te,
394                            (int)evt.tv_sec,
395                            http_timestring(talloc_tos(), te->next_event.tv_sec)));
396         }
397
398         for (fe = ev3->fd_events; fe; fe = fe->next) {
399
400                 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
401                            fe->fd,
402                            fe,
403                            fe->flags));
404         }
405 }
406
407 static const struct tevent_ops s3_event_ops = {
408         .context_init   = s3_event_context_init,
409         .add_fd         = s3_event_add_fd,
410         .set_fd_close_fn= tevent_common_fd_set_close_fn,
411         .get_fd_flags   = tevent_common_fd_get_flags,
412         .set_fd_flags   = tevent_common_fd_set_flags,
413         .add_timer      = s3_event_add_timer,
414         .loop_once      = s3_event_loop_once,
415         .loop_wait      = s3_event_loop_wait,
416 };
417
418 static bool s3_tevent_init(void)
419 {
420         static bool initialized;
421         if (initialized) {
422                 return true;
423         }
424         initialized = tevent_register_backend("s3", &s3_event_ops);
425         tevent_set_default_backend("s3");
426         return initialized;
427 }
428
429 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
430 {
431         s3_tevent_init();
432         return tevent_context_init_byname(mem_ctx, "s3");
433 }