s3:lib/events: make use of samba_tevent_set_debug()
[metze/samba/wip.git] / source3 / lib / events.c
index 7d4cdc209bd1f97e13799e94776a9ed1ef9453b0..0a8039ac6ea8c3d1ab8cb136f0e96343583bbccd 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/CIFS implementation.
    Timed event library.
    Copyright (C) Andrew Tridgell 1992-1998
-   Copyright (C) Volker Lendecke 2005
+   Copyright (C) Volker Lendecke 2005-2007
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 */
 
 #include "includes.h"
-
-struct timed_event {
-       struct timed_event *next, *prev;
-       struct event_context *event_ctx;
-       struct timeval when;
-       const char *event_name;
-       void (*handler)(struct event_context *event_ctx,
-                       struct timed_event *te,
-                       const struct timeval *now,
-                       void *private_data);
-       void *private_data;
-};
-
-struct fd_event {
-       struct fd_event *prev, *next;
-       struct event_context *event_ctx;
-       int fd;
-       uint16_t flags; /* see EVENT_FD_* flags */
-       void (*handler)(struct event_context *event_ctx,
-                       struct fd_event *event,
-                       uint16 flags,
-                       void *private_data);
-       void *private_data;
-};
-
-#define EVENT_FD_WRITEABLE(fde) \
-       event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_WRITE)
-#define EVENT_FD_READABLE(fde) \
-       event_set_fd_flags(fde, event_get_fd_flags(fde) | EVENT_FD_READ)
-
-#define EVENT_FD_NOT_WRITEABLE(fde) \
-       event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_WRITE)
-#define EVENT_FD_NOT_READABLE(fde) \
-       event_set_fd_flags(fde, event_get_fd_flags(fde) & ~EVENT_FD_READ)
-
-struct event_context {
-       struct timed_event *timed_events;
-       struct fd_event *fd_events;
+#include "lib/tevent/tevent_internal.h"
+#include "../lib/util/select.h"
+#include "system/select.h"
+
+struct tevent_poll_private {
+       /*
+        * Index from file descriptor into the pollfd array
+        */
+       int *pollfd_idx;
+
+       /*
+        * Cache for s3_event_loop_once to avoid reallocs
+        */
+       struct pollfd *pfds;
 };
 
-static int timed_event_destructor(struct timed_event *te)
+static struct tevent_poll_private *tevent_get_poll_private(
+       struct tevent_context *ev)
 {
-       DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
-               te->event_name));
-       if (te->event_ctx != NULL) {
-               DLIST_REMOVE(te->event_ctx->timed_events, te);
+       struct tevent_poll_private *state;
+
+       state = (struct tevent_poll_private *)ev->additional_data;
+       if (state == NULL) {
+               state = talloc_zero(ev, struct tevent_poll_private);
+               ev->additional_data = (void *)state;
+               if (state == NULL) {
+                       DEBUG(10, ("talloc failed\n"));
+               }
        }
-       return 0;
+       return state;
 }
 
-/****************************************************************************
- Add te by time.
-****************************************************************************/
-
-static void add_event_by_time(struct timed_event *te)
+static void count_fds(struct tevent_context *ev,
+                     int *pnum_fds, int *pmax_fd)
 {
-       struct event_context *ctx = te->event_ctx;
-       struct timed_event *last_te, *cur_te;
-
-       /* Keep the list ordered by time. We must preserve this. */
-       last_te = NULL;
-       for (cur_te = ctx->timed_events; cur_te; cur_te = cur_te->next) {
-               /* if the new event comes before the current one break */
-               if (!timeval_is_zero(&cur_te->when) &&
-                               timeval_compare(&te->when, &cur_te->when) < 0) {
-                       break;
+       struct tevent_fd *fde;
+       int num_fds = 0;
+       int max_fd = 0;
+
+       for (fde = ev->fd_events; fde != NULL; fde = fde->next) {
+               if (fde->flags & (TEVENT_FD_READ|TEVENT_FD_WRITE)) {
+                       num_fds += 1;
+                       if (fde->fd > max_fd) {
+                               max_fd = fde->fd;
+                       }
                }
-               last_te = cur_te;
        }
-
-       DLIST_ADD_AFTER(ctx->timed_events, te, last_te);
+       *pnum_fds = num_fds;
+       *pmax_fd = max_fd;
 }
 
-/****************************************************************************
- Schedule a function for future calling, cancel with TALLOC_FREE().
- It's the responsibility of the handler to call TALLOC_FREE() on the event
- handed to it.
-****************************************************************************/
-
-struct timed_event *event_add_timed(struct event_context *event_ctx,
-                               TALLOC_CTX *mem_ctx,
-                               struct timeval when,
-                               const char *event_name,
-                               void (*handler)(struct event_context *event_ctx,
-                                               struct timed_event *te,
-                                               const struct timeval *now,
-                                               void *private_data),
-                               void *private_data)
+bool event_add_to_poll_args(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
+                           struct pollfd **pfds, int *pnum_pfds,
+                           int *ptimeout)
 {
-       struct timed_event *te;
-
-       te = TALLOC_P(mem_ctx, struct timed_event);
-       if (te == NULL) {
-               DEBUG(0, ("talloc failed\n"));
-               return NULL;
+       struct tevent_poll_private *state;
+       struct tevent_fd *fde;
+       int i, num_fds, max_fd, num_pollfds, idx_len;
+       struct pollfd *fds;
+       struct timeval now, diff;
+       int timeout;
+
+       state = tevent_get_poll_private(ev);
+       if (state == NULL) {
+               return false;
        }
+       count_fds(ev, &num_fds, &max_fd);
 
-       te->event_ctx = event_ctx;
-       te->when = when;
-       te->event_name = event_name;
-       te->handler = handler;
-       te->private_data = private_data;
-
-       add_event_by_time(te);
+       idx_len = max_fd+1;
 
-       talloc_set_destructor(te, timed_event_destructor);
-
-       DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
-                       (unsigned long)te));
-       return te;
-}
-
-static int fd_event_destructor(struct fd_event *fde)
-{
-       if (fde->event_ctx != NULL) {
-               DLIST_REMOVE(fde->event_ctx->fd_events, fde);
+       if (talloc_array_length(state->pollfd_idx) < idx_len) {
+               state->pollfd_idx = talloc_realloc(
+                       state, state->pollfd_idx, int, idx_len);
+               if (state->pollfd_idx == NULL) {
+                       DEBUG(10, ("talloc_realloc failed\n"));
+                       return false;
+               }
        }
-       return 0;
-}
 
-struct fd_event *event_add_fd(struct event_context *event_ctx,
-                             TALLOC_CTX *mem_ctx,
-                             int fd, uint16_t flags,
-                             void (*handler)(struct event_context *event_ctx,
-                                             struct fd_event *event,
-                                             uint16 flags,
-                                             void *private_data),
-                             void *private_data)
-{
-       struct fd_event *fde;
+       fds = *pfds;
+       num_pollfds = *pnum_pfds;
 
-       if (!(fde = TALLOC_P(mem_ctx, struct fd_event))) {
-               return NULL;
+       if (talloc_array_length(fds) < num_pollfds + num_fds) {
+               fds = talloc_realloc(mem_ctx, fds, struct pollfd,
+                                          num_pollfds + num_fds);
+               if (fds == NULL) {
+                       DEBUG(10, ("talloc_realloc failed\n"));
+                       return false;
+               }
        }
 
-       fde->event_ctx = event_ctx;
-       fde->fd = fd;
-       fde->flags = flags;
-       fde->handler = handler;
-       fde->private_data = private_data;
-
-       DLIST_ADD(event_ctx->fd_events, fde);
-
-       talloc_set_destructor(fde, fd_event_destructor);
-       return fde;
-}
+       memset(&fds[num_pollfds], 0, sizeof(struct pollfd) * num_fds);
+
+       /*
+        * This needs tuning. We need to cope with multiple fde's for a file
+        * descriptor. The problem is that we need to re-use pollfd_idx across
+        * calls for efficiency. One way would be a direct bitmask that might
+        * be initialized quicker, but our bitmap_init implementation is
+        * pretty heavy-weight as well.
+        */
+       for (i=0; i<idx_len; i++) {
+               state->pollfd_idx[i] = -1;
+       }
 
-void event_fd_set_writeable(struct fd_event *fde)
-{
-       fde->flags |= EVENT_FD_WRITE;
-}
+       for (fde = ev->fd_events; fde; fde = fde->next) {
+               struct pollfd *pfd;
 
-void event_fd_set_not_writeable(struct fd_event *fde)
-{
-       fde->flags &= ~EVENT_FD_WRITE;
-}
-
-void event_fd_set_readable(struct fd_event *fde)
-{
-       fde->flags |= EVENT_FD_READ;
-}
+               if ((fde->flags & (TEVENT_FD_READ|TEVENT_FD_WRITE)) == 0) {
+                       continue;
+               }
 
-void event_fd_set_not_readable(struct fd_event *fde)
-{
-       fde->flags &= ~EVENT_FD_READ;
-}
+               if (state->pollfd_idx[fde->fd] == -1) {
+                       /*
+                        * We haven't seen this fd yet. Allocate a new pollfd.
+                        */
+                       state->pollfd_idx[fde->fd] = num_pollfds;
+                       pfd = &fds[num_pollfds];
+                       num_pollfds += 1;
+               } else {
+                       /*
+                        * We have already seen this fd. OR in the flags.
+                        */
+                       pfd = &fds[state->pollfd_idx[fde->fd]];
+               }
 
-/*
- * Return if there's something in the queue
- */
+               pfd->fd = fde->fd;
 
-bool event_add_to_select_args(struct event_context *event_ctx,
-                             const struct timeval *now,
-                             fd_set *read_fds, fd_set *write_fds,
-                             struct timeval *timeout, int *maxfd)
-{
-       struct fd_event *fde;
-       struct timeval diff;
-       bool ret = False;
-
-       for (fde = event_ctx->fd_events; fde; fde = fde->next) {
-               if (fde->flags & EVENT_FD_READ) {
-                       FD_SET(fde->fd, read_fds);
-                       ret = True;
-               }
-               if (fde->flags & EVENT_FD_WRITE) {
-                       FD_SET(fde->fd, write_fds);
-                       ret = True;
+               if (fde->flags & TEVENT_FD_READ) {
+                       pfd->events |= (POLLIN|POLLHUP);
                }
-
-               if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
-                   && (fde->fd > *maxfd)) {
-                       *maxfd = fde->fd;
+               if (fde->flags & TEVENT_FD_WRITE) {
+                       pfd->events |= POLLOUT;
                }
        }
+       *pfds = fds;
+       *pnum_pfds = num_pollfds;
 
-       if (event_ctx->timed_events == NULL) {
-               return ret;
+       if (ev->immediate_events != NULL) {
+               *ptimeout = 0;
+               return true;
+       }
+       if (ev->timer_events == NULL) {
+               *ptimeout = MIN(*ptimeout, INT_MAX);
+               return true;
        }
 
-       diff = timeval_until(now, &event_ctx->timed_events->when);
-       *timeout = timeval_min(timeout, &diff);
+       now = timeval_current();
+       diff = timeval_until(&now, &ev->timer_events->next_event);
+       timeout = timeval_to_msec(diff);
+
+       if (timeout < *ptimeout) {
+               *ptimeout = timeout;
+       }
 
-       return True;
+       return true;
 }
 
-bool events_pending(struct event_context *event_ctx)
+bool run_events_poll(struct tevent_context *ev, int pollrtn,
+                    struct pollfd *pfds, int num_pfds)
 {
-       struct fd_event *fde;
+       struct tevent_poll_private *state;
+       int *pollfd_idx;
+       struct tevent_fd *fde;
+       struct timeval now;
 
-       if (event_ctx->timed_events != NULL) {
-               return True;
+       if (ev->signal_events &&
+           tevent_common_check_signal(ev)) {
+               return true;
        }
-       for (fde = event_ctx->fd_events; fde; fde = fde->next) {
-               if (fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE)) {
-                       return True;
-               }
-       }
-       return False;
-}
 
-bool run_events(struct event_context *event_ctx,
-               int selrtn, fd_set *read_fds, fd_set *write_fds)
-{
-       bool fired = False;
-       struct fd_event *fde, *next;
+       if (ev->immediate_events &&
+           tevent_common_loop_immediate(ev)) {
+               return true;
+       }
 
-       /* Run all events that are pending, not just one (as we
-          did previously. */
+       GetTimeOfDay(&now);
 
-       while (event_ctx->timed_events) {
-               struct timeval now;
-               GetTimeOfDay(&now);
+       if ((ev->timer_events != NULL)
+           && (timeval_compare(&now, &ev->timer_events->next_event) >= 0)) {
+               /* this older events system did not auto-free timed
+                  events on running them, and had a race condition
+                  where the event could be called twice if the
+                  talloc_free of the te happened after the callback
+                  made a call which invoked the event loop. To avoid
+                  this while still allowing old code which frees the
+                  te, we need to create a temporary context which
+                  will be used to ensure the te is freed. We also
+                  remove the te from the timed event list before we
+                  call the handler, to ensure we can't loop */
 
-               if (timeval_compare(
-                           &now, &event_ctx->timed_events->when) < 0) {
-                       /* Nothing to do yet */
-                       DEBUG(11, ("run_events: Nothing to do\n"));
-                       break;
-               }
+               struct tevent_timer *te = ev->timer_events;
+               TALLOC_CTX *tmp_ctx = talloc_new(ev);
 
-               DEBUG(10, ("Running event \"%s\" %lx\n",
-                          event_ctx->timed_events->event_name,
-                          (unsigned long)event_ctx->timed_events));
+               DEBUG(10, ("Running timed event \"%s\" %p\n",
+                          ev->timer_events->handler_name, ev->timer_events));
 
-               event_ctx->timed_events->handler(
-                       event_ctx,
-                       event_ctx->timed_events, &now,
-                       event_ctx->timed_events->private_data);
+               DLIST_REMOVE(ev->timer_events, te);
+               talloc_steal(tmp_ctx, te);
 
-               fired = True;
-       }
+               te->handler(ev, te, now, te->private_data);
 
-       if (fired) {
-               /*
-                * We might have changed the socket status during the timed
-                * events, return to run select again.
-                */
-               return True;
+               talloc_free(tmp_ctx);
+               return true;
        }
 
-       if (selrtn == 0) {
+       if (pollrtn <= 0) {
                /*
                 * No fd ready
                 */
-               return fired;
+               return false;
        }
 
-       for (fde = event_ctx->fd_events; fde; fde = next) {
+       state = (struct tevent_poll_private *)ev->additional_data;
+       pollfd_idx = state->pollfd_idx;
+
+       for (fde = ev->fd_events; fde; fde = fde->next) {
+               struct pollfd *pfd;
                uint16 flags = 0;
 
-               next = fde->next;
-               if (FD_ISSET(fde->fd, read_fds)) flags |= EVENT_FD_READ;
-               if (FD_ISSET(fde->fd, write_fds)) flags |= EVENT_FD_WRITE;
+               if ((fde->flags & (TEVENT_FD_READ|TEVENT_FD_WRITE)) == 0) {
+                       continue;
+               }
+
+               if (pollfd_idx[fde->fd] >= num_pfds) {
+                       DEBUG(1, ("internal error: pollfd_idx[fde->fd] (%d) "
+                                 ">= num_pfds (%d)\n", pollfd_idx[fde->fd],
+                                 num_pfds));
+                       return false;
+               }
+               pfd = &pfds[pollfd_idx[fde->fd]];
+
+               if (pfd->fd != fde->fd) {
+                       DEBUG(1, ("internal error: pfd->fd (%d) "
+                                 "!= fde->fd (%d)\n", pollfd_idx[fde->fd],
+                                  num_pfds));
+                       return false;
+               }
 
+               if (pfd->revents & (POLLHUP|POLLERR)) {
+                       /* If we only wait for TEVENT_FD_WRITE, we
+                          should not tell the event handler about it,
+                          and remove the writable flag, as we only
+                          report errors when waiting for read events
+                          to match the select behavior. */
+                       if (!(fde->flags & TEVENT_FD_READ)) {
+                               TEVENT_FD_NOT_WRITEABLE(fde);
+                               continue;
+                       }
+                       flags |= TEVENT_FD_READ;
+               }
+
+               if (pfd->revents & POLLIN) {
+                       flags |= TEVENT_FD_READ;
+               }
+               if (pfd->revents & POLLOUT) {
+                       flags |= TEVENT_FD_WRITE;
+               }
                if (flags & fde->flags) {
-                       fde->handler(event_ctx, fde, flags, fde->private_data);
-                       fired = True;
+                       DLIST_DEMOTE(ev->fd_events, fde, struct tevent_fd);
+                       fde->handler(ev, fde, flags, fde->private_data);
+                       return true;
                }
        }
 
-       return fired;
+       return false;
 }
 
-
-struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
+struct timeval *get_timed_events_timeout(struct tevent_context *ev,
                                         struct timeval *to_ret)
 {
        struct timeval now;
 
-       if (event_ctx->timed_events == NULL) {
+       if ((ev->timer_events == NULL) && (ev->immediate_events == NULL)) {
                return NULL;
        }
+       if (ev->immediate_events != NULL) {
+               *to_ret = timeval_zero();
+               return to_ret;
+       }
 
        now = timeval_current();
-       *to_ret = timeval_until(&now, &event_ctx->timed_events->when);
+       *to_ret = timeval_until(&now, &ev->timer_events->next_event);
 
        DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
                (int)to_ret->tv_usec));
@@ -322,134 +308,203 @@ struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
        return to_ret;
 }
 
-int event_loop_once(struct event_context *ev)
+static int s3_event_loop_once(struct tevent_context *ev, const char *location)
 {
-       struct timeval now, to;
-       fd_set r_fds, w_fds;
-       int maxfd = 0;
+       struct tevent_poll_private *state;
+       int timeout;
+       int num_pfds;
        int ret;
+       int poll_errno;
 
-       FD_ZERO(&r_fds);
-       FD_ZERO(&w_fds);
-
-       to.tv_sec = 9999;       /* Max timeout */
-       to.tv_usec = 0;
-
-       GetTimeOfDay(&now);
+       timeout = INT_MAX;
 
-       if (!event_add_to_select_args(ev, &now, &r_fds, &w_fds, &to, &maxfd)) {
+       state = tevent_get_poll_private(ev);
+       if (state == NULL) {
+               errno = ENOMEM;
                return -1;
        }
 
-       if (timeval_is_zero(&to)) {
-               run_events(ev, 0, NULL, NULL);
+       if (run_events_poll(ev, 0, NULL, 0)) {
                return 0;
        }
 
-       ret = sys_select(maxfd+1, &r_fds, &w_fds, NULL, &to);
+       num_pfds = 0;
+       if (!event_add_to_poll_args(ev, state,
+                                   &state->pfds, &num_pfds, &timeout)) {
+               return -1;
+       }
+
+       tevent_trace_point_callback(ev, TEVENT_TRACE_BEFORE_WAIT);
+       ret = poll(state->pfds, num_pfds, timeout);
+       poll_errno = errno;
+       tevent_trace_point_callback(ev, TEVENT_TRACE_AFTER_WAIT);
+       errno = poll_errno;
 
        if (ret == -1 && errno != EINTR) {
+               tevent_debug(ev, TEVENT_DEBUG_FATAL,
+                            "poll() failed: %d:%s\n",
+                            errno, strerror(errno));
                return -1;
        }
 
-       run_events(ev, ret, &r_fds, &w_fds);
+       run_events_poll(ev, ret, state->pfds, num_pfds);
        return 0;
 }
 
-static int event_context_destructor(struct event_context *ev)
+static int s3_event_context_init(struct tevent_context *ev)
 {
-       while (ev->fd_events != NULL) {
-               ev->fd_events->event_ctx = NULL;
-               DLIST_REMOVE(ev->fd_events, ev->fd_events);
-       }
-       while (ev->timed_events != NULL) {
-               ev->timed_events->event_ctx = NULL;
-               DLIST_REMOVE(ev->timed_events, ev->timed_events);
-       }
        return 0;
 }
 
-void event_context_reinit(struct event_context *ev)
+void dump_event_list(struct tevent_context *ev)
 {
-       event_context_destructor(ev);
-       return;
-}
+       struct tevent_timer *te;
+       struct tevent_fd *fe;
+       struct timeval evt, now;
 
-struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
-{
-       struct event_context *result;
+       if (!ev) {
+               return;
+       }
 
-       result = TALLOC_ZERO_P(mem_ctx, struct event_context);
-       if (result == NULL) {
-               return NULL;
+       now = timeval_current();
+
+       DEBUG(10,("dump_event_list:\n"));
+
+       for (te = ev->timer_events; te; te = te->next) {
+
+               evt = timeval_until(&now, &te->next_event);
+
+               DEBUGADD(10,("Timed Event \"%s\" %p handled in %d seconds (at %s)\n",
+                          te->handler_name,
+                          te,
+                          (int)evt.tv_sec,
+                          http_timestring(talloc_tos(), te->next_event.tv_sec)));
        }
 
-       talloc_set_destructor(result, event_context_destructor);
-       return result;
+       for (fe = ev->fd_events; fe; fe = fe->next) {
+
+               DEBUGADD(10,("FD Event %d %p, flags: 0x%04x\n",
+                          fe->fd,
+                          fe,
+                          fe->flags));
+       }
 }
 
-int set_event_dispatch_time(struct event_context *event_ctx,
-                           const char *event_name, struct timeval when)
+static const struct tevent_ops s3_event_ops = {
+       .context_init           = s3_event_context_init,
+       .add_fd                 = tevent_common_add_fd,
+       .set_fd_close_fn        = tevent_common_fd_set_close_fn,
+       .get_fd_flags           = tevent_common_fd_get_flags,
+       .set_fd_flags           = tevent_common_fd_set_flags,
+       .add_timer              = tevent_common_add_timer,
+       .schedule_immediate     = tevent_common_schedule_immediate,
+       .add_signal             = tevent_common_add_signal,
+       .loop_once              = s3_event_loop_once,
+       .loop_wait              = tevent_common_loop_wait,
+};
+
+static bool s3_tevent_init(void)
 {
-       struct timed_event *te;
-
-       for (te = event_ctx->timed_events; te; te = te->next) {
-               if (strcmp(event_name, te->event_name) == 0) {
-                       DLIST_REMOVE(event_ctx->timed_events, te);
-                       te->when = when;
-                       add_event_by_time(te);
-                       return 1;
-               }
+       static bool initialized;
+       if (initialized) {
+               return true;
        }
-       return 0;
+       initialized = tevent_register_backend("s3", &s3_event_ops);
+       tevent_set_default_backend("s3");
+       return initialized;
 }
 
-/* Returns 1 if event was found and cancelled, 0 otherwise. */
-
-int cancel_named_event(struct event_context *event_ctx,
-                      const char *event_name)
+struct tevent_context *s3_tevent_context_init(TALLOC_CTX *mem_ctx)
 {
-       struct timed_event *te;
+       struct tevent_context *ev;
 
-       for (te = event_ctx->timed_events; te; te = te->next) {
-               if (strcmp(event_name, te->event_name) == 0) {
-                       TALLOC_FREE(te);
-                       return 1;
-               }
+       s3_tevent_init();
+
+       ev = tevent_context_init_byname(mem_ctx, "s3");
+       if (ev) {
+               samba_tevent_set_debug(ev, "s3_tevent");
        }
-       return 0;
+
+       return ev;
 }
 
-void dump_event_list(struct event_context *event_ctx)
+struct idle_event {
+       struct tevent_timer *te;
+       struct timeval interval;
+       char *name;
+       bool (*handler)(const struct timeval *now, void *private_data);
+       void *private_data;
+};
+
+static void smbd_idle_event_handler(struct tevent_context *ctx,
+                                   struct tevent_timer *te,
+                                   struct timeval now,
+                                   void *private_data)
 {
-       struct timed_event *te;
-       struct fd_event *fe;
-       struct timeval evt, now;
+       struct idle_event *event =
+               talloc_get_type_abort(private_data, struct idle_event);
 
-       if (!event_ctx) {
+       TALLOC_FREE(event->te);
+
+       DEBUG(10,("smbd_idle_event_handler: %s %p called\n",
+                 event->name, event->te));
+
+       if (!event->handler(&now, event->private_data)) {
+               DEBUG(10,("smbd_idle_event_handler: %s %p stopped\n",
+                         event->name, event->te));
+               /* Don't repeat, delete ourselves */
+               TALLOC_FREE(event);
                return;
        }
 
-       now = timeval_current();
+       DEBUG(10,("smbd_idle_event_handler: %s %p rescheduled\n",
+                 event->name, event->te));
 
-       DEBUG(10,("dump_event_list:\n"));
+       event->te = tevent_add_timer(ctx, event,
+                                    timeval_sum(&now, &event->interval),
+                                    smbd_idle_event_handler, event);
 
-       for (te = event_ctx->timed_events; te; te = te->next) {
+       /* We can't do much but fail here. */
+       SMB_ASSERT(event->te != NULL);
+}
 
-               evt = timeval_until(&now, &te->when);
+struct idle_event *event_add_idle(struct tevent_context *event_ctx,
+                                 TALLOC_CTX *mem_ctx,
+                                 struct timeval interval,
+                                 const char *name,
+                                 bool (*handler)(const struct timeval *now,
+                                                 void *private_data),
+                                 void *private_data)
+{
+       struct idle_event *result;
+       struct timeval now = timeval_current();
 
-               DEBUGADD(10,("Timed Event \"%s\" %lx handled in %d seconds (at %s)\n",
-                          te->event_name,
-                          (unsigned long)te,
-                          (int)evt.tv_sec,
-                          http_timestring(talloc_tos(), te->when.tv_sec)));
+       result = talloc(mem_ctx, struct idle_event);
+       if (result == NULL) {
+               DEBUG(0, ("talloc failed\n"));
+               return NULL;
        }
 
-       for (fe = event_ctx->fd_events; fe; fe = fe->next) {
+       result->interval = interval;
+       result->handler = handler;
+       result->private_data = private_data;
 
-               DEBUGADD(10,("FD Event %d %lx, flags: 0x%04x\n",
-                          fe->fd,
-                          (unsigned long)fe,
-                          fe->flags));
+       if (!(result->name = talloc_asprintf(result, "idle_evt(%s)", name))) {
+               DEBUG(0, ("talloc failed\n"));
+               TALLOC_FREE(result);
+               return NULL;
+       }
+
+       result->te = tevent_add_timer(event_ctx, result,
+                                     timeval_sum(&now, &interval),
+                                     smbd_idle_event_handler, result);
+       if (result->te == NULL) {
+               DEBUG(0, ("event_add_timed failed\n"));
+               TALLOC_FREE(result);
+               return NULL;
        }
+
+       DEBUG(10,("event_add_idle: %s %p\n", result->name, result->te));
+       return result;
 }
+