From e34065b0aee5130f384622cb73f411a0f69cbaac Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 21 May 2015 22:28:14 +0200 Subject: [PATCH] lib/async_req: remove the tevent_fd as early as possible via a wait_for_read_cleanup() hook BUG: https://bugzilla.samba.org/show_bug.cgi?id=11316 Signed-off-by: Stefan Metzmacher Reviewed-by: Volker Lendecke (cherry picked from commit 64640cc99c7b8543ee8d35ca243c57c048cdb490) --- lib/async_req/async_sock.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/async_req/async_sock.c b/lib/async_req/async_sock.c index 8e04a08cbd8..2f3225dee12 100644 --- a/lib/async_req/async_sock.c +++ b/lib/async_req/async_sock.c @@ -538,10 +538,11 @@ ssize_t read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx, } struct wait_for_read_state { - struct tevent_req *req; struct tevent_fd *fde; }; +static void wait_for_read_cleanup(struct tevent_req *req, + enum tevent_req_state req_state); static void wait_for_read_done(struct tevent_context *ev, struct tevent_fd *fde, uint16_t flags, @@ -558,36 +559,47 @@ struct tevent_req *wait_for_read_send(TALLOC_CTX *mem_ctx, if (req == NULL) { return NULL; } - state->req = req; + + tevent_req_set_cleanup_fn(req, wait_for_read_cleanup); + state->fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, - wait_for_read_done, state); + wait_for_read_done, req); if (tevent_req_nomem(state->fde, req)) { return tevent_req_post(req, ev); } return req; } +static void wait_for_read_cleanup(struct tevent_req *req, + enum tevent_req_state req_state) +{ + struct wait_for_read_state *state = + tevent_req_data(req, struct wait_for_read_state); + + TALLOC_FREE(state->fde); +} + static void wait_for_read_done(struct tevent_context *ev, struct tevent_fd *fde, uint16_t flags, void *private_data) { - struct wait_for_read_state *state = talloc_get_type_abort( - private_data, struct wait_for_read_state); + struct tevent_req *req = talloc_get_type_abort( + private_data, struct tevent_req); if (flags & TEVENT_FD_READ) { - TALLOC_FREE(state->fde); - tevent_req_done(state->req); + tevent_req_done(req); } } bool wait_for_read_recv(struct tevent_req *req, int *perr) { - int err; + int err = tevent_req_simple_recv_unix(req); - if (tevent_req_is_unix_error(req, &err)) { + if (err != 0) { *perr = err; return false; } + return true; } -- 2.11.4.GIT