From 95d6600a0668b8abac53cbe2085236b31d652b66 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 31 Aug 2023 18:09:28 +0200 Subject: [PATCH] tevent: split out a tevent_common_fd_disarm() helper It means tevent_trace_fd_callback(TEVENT_EVENT_TRACE_DETACH) is always called and similar future changes are only needed in one place. Signed-off-by: Stefan Metzmacher Reviewed-by: Ralph Boehme --- lib/tevent/tevent.c | 5 +---- lib/tevent/tevent_epoll.c | 32 ++++++++------------------------ lib/tevent/tevent_internal.h | 13 +++++++++++++ lib/tevent/tevent_poll.c | 8 ++------ lib/tevent/tevent_wrapper.c | 4 +--- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c index 63ef4dbd7bab..9f4a91953ae3 100644 --- a/lib/tevent/tevent.c +++ b/lib/tevent/tevent.c @@ -410,10 +410,7 @@ int tevent_common_context_destructor(struct tevent_context *ev) for (fd = ev->fd_events; fd; fd = fn) { fn = fd->next; - tevent_trace_fd_callback(fd->event_ctx, fd, TEVENT_EVENT_TRACE_DETACH); - fd->wrapper = NULL; - fd->event_ctx = NULL; - DLIST_REMOVE(ev->fd_events, fd); + tevent_common_fd_disarm(fd); } ev->last_zero_timer = NULL; diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c index 2771a83fdd26..54231076e3ac 100644 --- a/lib/tevent/tevent_epoll.c +++ b/lib/tevent/tevent_epoll.c @@ -316,12 +316,8 @@ static int epoll_add_multiplex_fd(struct epoll_event_context *epoll_ev, "EPOLL_CTL_MOD EBADF for " "add_fde[%p] mpx_fde[%p] fd[%d] - disabling\n", add_fde, mpx_fde, add_fde->fd); - DLIST_REMOVE(epoll_ev->ev->fd_events, mpx_fde); - mpx_fde->wrapper = NULL; - mpx_fde->event_ctx = NULL; - DLIST_REMOVE(epoll_ev->ev->fd_events, add_fde); - add_fde->wrapper = NULL; - add_fde->event_ctx = NULL; + tevent_common_fd_disarm(mpx_fde); + tevent_common_fd_disarm(add_fde); return 0; } else if (ret != 0) { return ret; @@ -382,13 +378,9 @@ static void epoll_add_event(struct epoll_event_context *epoll_ev, struct tevent_ "EPOLL_CTL_ADD EBADF for " "fde[%p] mpx_fde[%p] fd[%d] - disabling\n", fde, mpx_fde, fde->fd); - DLIST_REMOVE(epoll_ev->ev->fd_events, fde); - fde->wrapper = NULL; - fde->event_ctx = NULL; + tevent_common_fd_disarm(fde); if (mpx_fde != NULL) { - DLIST_REMOVE(epoll_ev->ev->fd_events, mpx_fde); - mpx_fde->wrapper = NULL; - mpx_fde->event_ctx = NULL; + tevent_common_fd_disarm(mpx_fde); } return; } else if (ret != 0 && errno == EEXIST && mpx_fde == NULL) { @@ -459,13 +451,9 @@ static void epoll_del_event(struct epoll_event_context *epoll_ev, struct tevent_ "EPOLL_CTL_DEL EBADF for " "fde[%p] mpx_fde[%p] fd[%d] - disabling\n", fde, mpx_fde, fde->fd); - DLIST_REMOVE(epoll_ev->ev->fd_events, fde); - fde->wrapper = NULL; - fde->event_ctx = NULL; + tevent_common_fd_disarm(fde); if (mpx_fde != NULL) { - DLIST_REMOVE(epoll_ev->ev->fd_events, mpx_fde); - mpx_fde->wrapper = NULL; - mpx_fde->event_ctx = NULL; + tevent_common_fd_disarm(mpx_fde); } return; } else if (ret != 0) { @@ -510,13 +498,9 @@ static void epoll_mod_event(struct epoll_event_context *epoll_ev, struct tevent_ "EPOLL_CTL_MOD EBADF for " "fde[%p] mpx_fde[%p] fd[%d] - disabling\n", fde, mpx_fde, fde->fd); - DLIST_REMOVE(epoll_ev->ev->fd_events, fde); - fde->wrapper = NULL; - fde->event_ctx = NULL; + tevent_common_fd_disarm(fde); if (mpx_fde != NULL) { - DLIST_REMOVE(epoll_ev->ev->fd_events, mpx_fde); - mpx_fde->wrapper = NULL; - mpx_fde->event_ctx = NULL; + tevent_common_fd_disarm(mpx_fde); } return; } else if (ret != 0) { diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h index e3321deab793..6d290b3e4d51 100644 --- a/lib/tevent/tevent_internal.h +++ b/lib/tevent/tevent_internal.h @@ -561,3 +561,16 @@ void tevent_trace_immediate_callback(struct tevent_context *ev, void tevent_trace_queue_callback(struct tevent_context *ev, struct tevent_queue_entry *qe, enum tevent_event_trace_point); + +#include "tevent_dlinklist.h" + +static inline void tevent_common_fd_disarm(struct tevent_fd *fde) +{ + if (fde->event_ctx != NULL) { + tevent_trace_fd_callback(fde->event_ctx, fde, + TEVENT_EVENT_TRACE_DETACH); + DLIST_REMOVE(fde->event_ctx->fd_events, fde); + fde->event_ctx = NULL; + } + fde->wrapper = NULL; +} diff --git a/lib/tevent/tevent_poll.c b/lib/tevent/tevent_poll.c index f4c6c2dbe80f..b27817007de7 100644 --- a/lib/tevent/tevent_poll.c +++ b/lib/tevent/tevent_poll.c @@ -534,9 +534,7 @@ static int poll_event_loop_poll(struct tevent_context *ev, fde, pfd->fd); poll_ev->fdes[idx] = NULL; poll_ev->deleted = true; - DLIST_REMOVE(ev->fd_events, fde); - fde->wrapper = NULL; - fde->event_ctx = NULL; + tevent_common_fd_disarm(fde); continue; } @@ -586,9 +584,7 @@ static int poll_event_loop_poll(struct tevent_context *ev, poll_ev->fdes[i] = NULL; poll_ev->deleted = true; if (fde != NULL) { - DLIST_REMOVE(ev->fd_events, fde); - fde->wrapper = NULL; - fde->event_ctx = NULL; + tevent_common_fd_disarm(fde); } } } diff --git a/lib/tevent/tevent_wrapper.c b/lib/tevent/tevent_wrapper.c index 4f35dfda24f3..3870d4fbb43a 100644 --- a/lib/tevent/tevent_wrapper.c +++ b/lib/tevent/tevent_wrapper.c @@ -276,9 +276,7 @@ static int tevent_wrapper_context_destructor(struct tevent_context *wrap_ev) tevent_fd_set_flags(fd, 0); - fd->wrapper = NULL; - fd->event_ctx = NULL; - DLIST_REMOVE(main_ev->fd_events, fd); + tevent_common_fd_disarm(fd); } for (te = main_ev->timer_events; te; te = tn) {