summaryrefslogtreecommitdiff
path: root/source3/lib/events.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/events.c')
-rw-r--r--source3/lib/events.c219
1 files changed, 197 insertions, 22 deletions
diff --git a/source3/lib/events.c b/source3/lib/events.c
index 66aefa3b52..ef52faef01 100644
--- a/source3/lib/events.c
+++ b/source3/lib/events.c
@@ -21,13 +21,50 @@
#include "includes.h"
-static struct timed_event *timed_events;
+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;
+};
static int timed_event_destructor(struct timed_event *te)
{
DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
te->event_name));
- DLIST_REMOVE(timed_events, te);
+ DLIST_REMOVE(te->event_ctx->timed_events, te);
return 0;
}
@@ -37,10 +74,12 @@ static int timed_event_destructor(struct timed_event *te)
handed to it.
****************************************************************************/
-struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
+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 timed_event *te,
+ void (*handler)(struct event_context *event_ctx,
+ struct timed_event *te,
const struct timeval *now,
void *private_data),
void *private_data)
@@ -53,6 +92,7 @@ struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
return NULL;
}
+ te->event_ctx = event_ctx;
te->when = when;
te->event_name = event_name;
te->handler = handler;
@@ -61,16 +101,16 @@ struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
/* keep the list ordered - this is NOT guarenteed as event times
may be changed after insertion */
last_te = NULL;
- for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
+ for (cur_te = event_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) {
+ if (!timeval_is_zero(&cur_te->when)
+ && timeval_compare(&te->when, &cur_te->when) < 0) {
break;
}
last_te = cur_te;
}
- DLIST_ADD_AFTER(timed_events, te, last_te);
+ DLIST_ADD_AFTER(event_ctx->timed_events, te, last_te);
talloc_set_destructor(te, timed_event_destructor);
DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
@@ -78,38 +118,166 @@ struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
return te;
}
-void run_events(void)
+static int fd_event_destructor(struct fd_event *fde)
+{
+ struct event_context *event_ctx = fde->event_ctx;
+
+ DLIST_REMOVE(event_ctx->fd_events, fde);
+ 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;
+
+ if (!(fde = TALLOC_P(mem_ctx, struct fd_event))) {
+ return NULL;
+ }
+
+ 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;
+}
+
+void event_fd_set_writeable(struct fd_event *fde)
+{
+ fde->flags |= EVENT_FD_WRITE;
+}
+
+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;
+}
+
+void event_fd_set_not_readable(struct fd_event *fde)
+{
+ fde->flags &= ~EVENT_FD_READ;
+}
+
+void 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;
+
+ for (fde = event_ctx->fd_events; fde; fde = fde->next) {
+ if (fde->flags & EVENT_FD_READ) {
+ FD_SET(fde->fd, read_fds);
+ }
+ if (fde->flags & EVENT_FD_WRITE) {
+ FD_SET(fde->fd, write_fds);
+ }
+
+ if ((fde->flags & (EVENT_FD_READ|EVENT_FD_WRITE))
+ && (fde->fd > *maxfd)) {
+ *maxfd = fde->fd;
+ }
+ }
+
+ if (event_ctx->timed_events == NULL) {
+ return;
+ }
+
+ diff = timeval_until(now, &event_ctx->timed_events->when);
+ *timeout = timeval_min(timeout, &diff);
+}
+
+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;
+
/* Run all events that are pending, not just one (as we
did previously. */
- while (timed_events) {
+ while (event_ctx->timed_events) {
struct timeval now;
GetTimeOfDay(&now);
- if (timeval_compare(&now, &timed_events->when) < 0) {
+ if (timeval_compare(
+ &now, &event_ctx->timed_events->when) < 0) {
/* Nothing to do yet */
DEBUG(11, ("run_events: Nothing to do\n"));
- return;
+ break;
}
- DEBUG(10, ("Running event \"%s\" %lx\n", timed_events->event_name,
- (unsigned long)timed_events));
+ DEBUG(10, ("Running event \"%s\" %lx\n",
+ event_ctx->timed_events->event_name,
+ (unsigned long)event_ctx->timed_events));
+
+ event_ctx->timed_events->handler(
+ event_ctx,
+ event_ctx->timed_events, &now,
+ event_ctx->timed_events->private_data);
- timed_events->handler(timed_events, &now, timed_events->private_data);
+ fired = True;
}
+
+ if (fired) {
+ /*
+ * We might have changed the socket status during the timed
+ * events, return to run select again.
+ */
+ return True;
+ }
+
+ if (selrtn == 0) {
+ /*
+ * No fd ready
+ */
+ return fired;
+ }
+
+ for (fde = event_ctx->fd_events; fde; fde = next) {
+ 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 (flags) {
+ fde->handler(event_ctx, fde, flags, fde->private_data);
+ fired = True;
+ }
+ }
+
+ return fired;
}
-struct timeval *get_timed_events_timeout(struct timeval *to_ret)
+
+struct timeval *get_timed_events_timeout(struct event_context *event_ctx,
+ struct timeval *to_ret)
{
struct timeval now;
- if (timed_events == NULL) {
+ if (event_ctx->timed_events == NULL) {
return NULL;
}
now = timeval_current();
- *to_ret = timeval_until(&now, &timed_events->when);
+ *to_ret = timeval_until(&now, &event_ctx->timed_events->when);
DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
(int)to_ret->tv_usec));
@@ -117,12 +285,18 @@ struct timeval *get_timed_events_timeout(struct timeval *to_ret)
return to_ret;
}
-int set_event_dispatch_time(const char *event_name, struct timeval when)
+struct event_context *event_context_init(TALLOC_CTX *mem_ctx)
+{
+ return TALLOC_ZERO_P(NULL, struct event_context);
+}
+
+int set_event_dispatch_time(struct event_context *event_ctx,
+ const char *event_name, struct timeval when)
{
int num_events = 0;
struct timed_event *te;
- for (te = timed_events; te; te = te->next) {
+ for (te = event_ctx->timed_events; te; te = te->next) {
if (strcmp(event_name, te->event_name) == 0) {
te->when = when;
num_events++;
@@ -133,11 +307,12 @@ int set_event_dispatch_time(const char *event_name, struct timeval when)
/* Returns 1 if event was found and cancelled, 0 otherwise. */
-int cancel_named_event(const char *event_name)
+int cancel_named_event(struct event_context *event_ctx,
+ const char *event_name)
{
struct timed_event *te;
- for (te = timed_events; te; te = te->next) {
+ for (te = event_ctx->timed_events; te; te = te->next) {
if (strcmp(event_name, te->event_name) == 0) {
TALLOC_FREE(te);
return 1;