2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
22 #include <tevent_internal.h>
24 struct s3_event_context {
25 struct tevent_context *ev;
26 struct tevent_fd *fd_events;
29 static int s3_event_timer_destructor(struct tevent_timer *te)
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);
39 /****************************************************************************
41 ****************************************************************************/
43 static void add_event_by_time(struct tevent_timer *te)
45 struct tevent_context *ctx = te->event_ctx;
46 struct tevent_timer *last_te, *cur_te;
48 /* Keep the list ordered by time. We must preserve this. */
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) {
59 DLIST_ADD_AFTER(ctx->timer_events, te, last_te);
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
66 ****************************************************************************/
68 static struct tevent_timer *s3_event_add_timer(struct tevent_context *event_ctx,
71 tevent_timer_handler_t handler,
73 const char *handler_name,
76 struct tevent_timer *te;
78 te = TALLOC_P(mem_ctx, struct tevent_timer);
80 DEBUG(0, ("talloc failed\n"));
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;
92 add_event_by_time(te);
94 talloc_set_destructor(te, s3_event_timer_destructor);
96 DEBUG(10, ("Added timed event \"%s\": %p\n", handler_name, te));
100 static int s3_event_fd_destructor(struct tevent_fd *fde)
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);
109 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
115 static struct tevent_fd *s3_event_add_fd(struct tevent_context *ev,
119 tevent_fd_handler_t handler,
121 const char *handler_name,
122 const char *location)
124 struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
125 struct s3_event_context);
126 struct tevent_fd *fde;
128 if (!(fde = TALLOC_P(mem_ctx, struct tevent_fd))) {
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;
141 DLIST_ADD(ev3->fd_events, fde);
143 talloc_set_destructor(fde, s3_event_fd_destructor);
147 void event_fd_set_writeable(struct tevent_fd *fde)
149 TEVENT_FD_WRITEABLE(fde);
152 void event_fd_set_not_writeable(struct tevent_fd *fde)
154 TEVENT_FD_NOT_WRITEABLE(fde);
157 void event_fd_set_readable(struct tevent_fd *fde)
159 TEVENT_FD_READABLE(fde);
162 void event_fd_set_not_readable(struct tevent_fd *fde)
164 TEVENT_FD_NOT_READABLE(fde);
168 * Return if there's something in the queue
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)
176 struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
177 struct s3_event_context);
178 struct tevent_fd *fde;
182 for (fde = ev3->fd_events; fde; fde = fde->next) {
183 if (fde->flags & EVENT_FD_READ) {
184 FD_SET(fde->fd, read_fds);
187 if (fde->flags & EVENT_FD_WRITE) {
188 FD_SET(fde->fd, write_fds);
192 if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
193 && (fde->fd > *maxfd)) {
198 if (ev->timer_events == NULL) {
202 diff = timeval_until(now, &ev->timer_events->next_event);
203 *timeout = timeval_min(timeout, &diff);
208 bool run_events(struct tevent_context *ev,
209 int selrtn, fd_set *read_fds, fd_set *write_fds)
211 struct s3_event_context *ev3 = talloc_get_type(ev->additional_data,
212 struct s3_event_context);
214 struct tevent_fd *fde, *next;
216 /* Run all events that are pending, not just one (as we
219 while (ev->timer_events) {
224 &now, &ev->timer_events->next_event) < 0) {
225 /* Nothing to do yet */
226 DEBUG(11, ("run_events: Nothing to do\n"));
230 DEBUG(10, ("Running event \"%s\" %p\n",
231 ev->timer_events->handler_name,
234 ev->timer_events->handler(
236 ev->timer_events, now,
237 ev->timer_events->private_data);
244 * We might have changed the socket status during the timed
245 * events, return to run select again.
257 for (fde = ev3->fd_events; fde; 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;
264 if (flags & fde->flags) {
265 fde->handler(ev, fde, flags, fde->private_data);
274 struct timeval *get_timed_events_timeout(struct tevent_context *ev,
275 struct timeval *to_ret)
279 if (ev->timer_events == NULL) {
283 now = timeval_current();
284 *to_ret = timeval_until(&now, &ev->timer_events->next_event);
286 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
287 (int)to_ret->tv_usec));
292 static int s3_event_loop_once(struct tevent_context *ev)
294 struct timeval now, to;
302 to.tv_sec = 9999; /* Max timeout */
307 if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
311 if (timeval_is_zero(&to)) {
312 run_events(ev, 0, NULL, NULL);
316 ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
318 if (ret == -1 && errno != EINTR) {
322 run_events(ev, ret, &r_fds, &w_fds);
326 static int s3_event_loop_wait(struct tevent_context *ev)
331 ret = s3_event_loop_once(ev);
337 static int s3_event_context_destructor(struct tevent_context *ev)
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);
345 while (ev->timer_events != NULL) {
346 ev->timer_events->event_ctx = NULL;
347 DLIST_REMOVE(ev->timer_events, ev3->ev->timer_events);
352 void event_context_reinit(struct tevent_context *ev)
354 s3_event_context_destructor(ev);
358 static int s3_event_context_init(struct tevent_context *ev)
360 struct s3_event_context *ev3;
362 ev3 = talloc_zero(ev, struct s3_event_context);
366 ev->additional_data = ev3;
367 talloc_set_destructor(ev, s3_event_context_destructor);
371 void dump_event_list(struct tevent_context *ev)
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;
383 now = timeval_current();
385 DEBUG(10,("dump_event_list:\n"));
387 for (te = ev->timer_events; te; te = te->next) {
389 evt = timeval_until(&now, &te->next_event);
391 DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
395 http_timestring(talloc_tos(), te->next_event.tv_sec)));
398 for (fe = ev3->fd_events; fe; fe = fe->next) {
400 DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
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,
418 static bool s3_tevent_init(void)
420 static bool initialized;
424 initialized = tevent_register_backend("s3", &s3_event_ops);
425 tevent_set_default_backend("s3");
429 struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
432 return tevent_context_init_byname(mem_ctx, "s3");