From: Arran Cudbard-Bell Date: Sun, 15 Dec 2019 06:34:44 +0000 (+0700) Subject: Use destructor to enforce absolute free order X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b69d99792e2f27ab7043deda13606a49111d5d29;p=thirdparty%2Ffreeradius-server.git Use destructor to enforce absolute free order --- diff --git a/src/lib/io/control.c b/src/lib/io/control.c index c8c791fb9d9..8a4f145257d 100644 --- a/src/lib/io/control.c +++ b/src/lib/io/control.c @@ -84,7 +84,6 @@ struct fr_control_t { fr_control_ctx_t type[FR_CONTROL_MAX_TYPES]; //!< callbacks }; - static void pipe_read(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx) { fr_control_t *c = talloc_get_type_abort(uctx, fr_control_t); @@ -113,6 +112,24 @@ static void pipe_read(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void } } +/** Free a control structure + * + * This function really only calls the underlying "garbage collect". + * + * @param[in] c the control structure + */ +static int _control_free(fr_control_t *c) +{ + (void) talloc_get_type_abort(c, fr_control_t); + + (void) fr_event_fd_delete(c->el, c->pipe[0], FR_EVENT_FILTER_IO); + + close(c->pipe[0]); + close(c->pipe[1]); + + return 0; +} + /** Create a control-plane signaling path. * * @param[in] ctx the talloc context @@ -131,7 +148,6 @@ fr_control_t *fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_atomic_ fr_strerror_printf("Failed allocating memory"); return NULL; } - c->el = el; c->aq = aq; @@ -140,6 +156,7 @@ fr_control_t *fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_atomic_ fr_strerror_printf("Failed opening pipe for control socket: %s", fr_syserror(errno)); return NULL; } + talloc_set_destructor(c, _control_free); /* * We don't want reads from the pipe to be blocking. @@ -147,8 +164,6 @@ fr_control_t *fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_atomic_ (void) fr_nonblock(c->pipe[0]); if (fr_event_fd_insert(c, el, c->pipe[0], pipe_read, NULL, NULL, c) < 0) { - close(c->pipe[0]); - close(c->pipe[1]); talloc_free(c); fr_strerror_printf("Failed adding FD to event list control socket: %s", fr_strerror()); return NULL; @@ -209,23 +224,6 @@ int fr_control_gc(UNUSED fr_control_t *c, fr_ring_buffer_t *rb) return 0; } -/** Free a control structure - * - * This function really only calls the underlying "garbage collect". - * - * @param[in] c the control structure - */ -void fr_control_free(fr_control_t *c) -{ - (void) talloc_get_type_abort(c, fr_control_t); - - (void) fr_event_fd_delete(c->el, c->pipe[0], FR_EVENT_FILTER_IO); - - close(c->pipe[0]); - close(c->pipe[1]); - - talloc_free(c); -} /** Allocate a control message diff --git a/src/lib/io/control.h b/src/lib/io/control.h index 614c474453d..1657a069948 100644 --- a/src/lib/io/control.h +++ b/src/lib/io/control.h @@ -60,7 +60,6 @@ typedef void (*fr_control_callback_t)(void *ctx, void const *data, size_t data_s #define FR_CONTROL_ID_INJECT (5) fr_control_t *fr_control_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_atomic_queue_t *aq) CC_HINT(nonnull(3)); -void fr_control_free(fr_control_t *c) CC_HINT(nonnull); int fr_control_gc(fr_control_t *c, fr_ring_buffer_t *rb) CC_HINT(nonnull); diff --git a/src/lib/io/network.c b/src/lib/io/network.c index 095a9350eff..70f3a68c0f2 100644 --- a/src/lib/io/network.c +++ b/src/lib/io/network.c @@ -980,7 +980,7 @@ fr_network_t *fr_network_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_log_t c if (!nr->rb) { fr_strerror_printf_push("Failed creating ring buffer"); fail2: - fr_control_free(nr->control); + talloc_free(nr->control); goto fail; }