From 12ee6b832c7ff09010fc67122aaefcfce1cb712f Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sat, 9 Aug 2008 21:39:18 +0200 Subject: Make events robust against their event_context being freed (This used to be commit 3d4e7b29c235e329aaea4fa2c2078df0ce3e59eb) --- source3/lib/events.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'source3/lib') diff --git a/source3/lib/events.c b/source3/lib/events.c index 7750176d8e..f03138708b 100644 --- a/source3/lib/events.c +++ b/source3/lib/events.c @@ -63,7 +63,9 @@ 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(te->event_ctx->timed_events, te); + if (te->event_ctx != NULL) { + DLIST_REMOVE(te->event_ctx->timed_events, te); + } return 0; } @@ -131,9 +133,9 @@ struct timed_event *event_add_timed(struct event_context *event_ctx, static int fd_event_destructor(struct fd_event *fde) { - struct event_context *event_ctx = fde->event_ctx; - - DLIST_REMOVE(event_ctx->fd_events, fde); + if (fde->event_ctx != NULL) { + DLIST_REMOVE(fde->event_ctx->fd_events, fde); + } return 0; } @@ -354,9 +356,30 @@ int event_loop_once(struct event_context *ev) return 0; } +static int event_context_destructor(struct event_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; +} + struct event_context *event_context_init(TALLOC_CTX *mem_ctx) { - return TALLOC_ZERO_P(mem_ctx, struct event_context); + struct event_context *result; + + result = TALLOC_ZERO_P(mem_ctx, struct event_context); + if (result == NULL) { + return NULL; + } + + talloc_set_destructor(result, event_context_destructor); + return result; } int set_event_dispatch_time(struct event_context *event_ctx, -- cgit