From: Arran Cudbard-Bell Date: Wed, 15 May 2024 01:52:11 +0000 (-0600) Subject: Move trigger free to the atexit handlers X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4ce6a703aeeb7ec9a79faf80076aef2061bc1eae;p=thirdparty%2Ffreeradius-server.git Move trigger free to the atexit handlers --- diff --git a/src/lib/server/base.c b/src/lib/server/base.c index ab5ad9e729..e680d4c517 100644 --- a/src/lib/server/base.c +++ b/src/lib/server/base.c @@ -119,10 +119,4 @@ void server_free(void) * Free xlat instance data, and call any detach methods */ xlat_instances_free(); - - /* - * Now we're sure no more triggers can fire, free the - * trigger tree. - */ - trigger_exec_free(); } diff --git a/src/lib/server/trigger.c b/src/lib/server/trigger.c index 82d354c97f..16be837eb8 100644 --- a/src/lib/server/trigger.c +++ b/src/lib/server/trigger.c @@ -22,15 +22,18 @@ * * @copyright 2015 The FreeRADIUS server project */ - RCSID("$Id$") #include #include #include +#include #include #include + +#include #include + #include /** Whether triggers are enabled globally @@ -102,12 +105,6 @@ xlat_action_t trigger_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, return XLAT_ACTION_DONE; } -static int _mutex_free(pthread_mutex_t *mutex) -{ - pthread_mutex_destroy(mutex); - return 0; -} - static void _trigger_last_fired_free(void *data) { talloc_free(data); @@ -126,56 +123,6 @@ static int8_t _trigger_last_fired_cmp(void const *one, void const *two) return CMP(a->ci, b->ci); } -/** Set the global trigger section trigger_exec will search in, and register xlats - * - * This function exists because triggers are used by the connection pool, which - * is used in the server library which may not have the mainconfig available. - * Additionally, utilities may want to set their own root config sections. - * - * We don't register the trigger xlat here, as we may inadvertently initialise - * the xlat code, which is annoying when this is called from a utility. - * - * @param[in] cs to use as global trigger section. - * @return - * - 0 on success. - * - -1 on failure. - */ -int trigger_exec_init(CONF_SECTION const *cs) -{ - if (!cs) { - ERROR("%s - Pointer to main_config was NULL", __FUNCTION__); - return -1; - } - - trigger_exec_main = cs; - trigger_exec_subcs = cf_section_find(cs, "trigger", NULL); - - if (!trigger_exec_subcs) { - WARN("trigger { ... } subsection not found, triggers will be disabled"); - return 0; - } - - MEM(trigger_last_fired_tree = fr_rb_inline_talloc_alloc(talloc_null_ctx(), - trigger_last_fired_t, node, - _trigger_last_fired_cmp, _trigger_last_fired_free)); - - trigger_mutex = talloc(talloc_null_ctx(), pthread_mutex_t); - pthread_mutex_init(trigger_mutex, 0); - talloc_set_destructor(trigger_mutex, _mutex_free); - triggers_init = true; - - return 0; -} - -/** Free trigger resources - * - */ -void trigger_exec_free(void) -{ - TALLOC_FREE(trigger_last_fired_tree); - TALLOC_FREE(trigger_mutex); -} - /** Return whether triggers are enabled * */ @@ -524,3 +471,71 @@ void trigger_args_afrom_server(TALLOC_CTX *ctx, fr_pair_list_t *list, char const vp->vp_uint16 = port; fr_pair_append(list, vp); } + +static int _mutex_free(pthread_mutex_t *mutex) +{ + pthread_mutex_destroy(mutex); + return 0; +} + +/** Free trigger resources + * + */ +static int _trigger_exec_free(UNUSED void *uctx) +{ + TALLOC_FREE(trigger_last_fired_tree); + TALLOC_FREE(trigger_mutex); + + return 0; +} + +/** Set the global trigger section trigger_exec will search in, and register xlats + * + * This function exists because triggers are used by the connection pool, which + * is used in the server library which may not have the mainconfig available. + * Additionally, utilities may want to set their own root config sections. + * + * We don't register the trigger xlat here, as we may inadvertently initialise + * the xlat code, which is annoying when this is called from a utility. + * + * @param[in] cs_arg to use as global trigger section. + * @return + * - 0 on success. + * - -1 on failure. + */ +static int _trigger_exec_init(void *cs_arg) +{ + CONF_SECTION *cs = talloc_get_type_abort(cs_arg, CONF_SECTION); + if (!cs) { + ERROR("%s - Pointer to main_config was NULL", __FUNCTION__); + return -1; + } + + trigger_exec_main = cs; + trigger_exec_subcs = cf_section_find(cs, "trigger", NULL); + + if (!trigger_exec_subcs) { + WARN("trigger { ... } subsection not found, triggers will be disabled"); + return 0; + } + + MEM(trigger_last_fired_tree = fr_rb_inline_talloc_alloc(talloc_null_ctx(), + trigger_last_fired_t, node, + _trigger_last_fired_cmp, _trigger_last_fired_free)); + + trigger_mutex = talloc(talloc_null_ctx(), pthread_mutex_t); + pthread_mutex_init(trigger_mutex, 0); + talloc_set_destructor(trigger_mutex, _mutex_free); + triggers_init = true; + + return 0; +} + +int trigger_exec_init(CONF_SECTION const *cs) +{ + int ret; + + fr_atexit_global_once_ret(&ret, _trigger_exec_init, _trigger_exec_free, UNCONST(CONF_SECTION *, cs)); + + return ret; +} diff --git a/src/lib/server/trigger.h b/src/lib/server/trigger.h index d8b65a15ae..5756656992 100644 --- a/src/lib/server/trigger.h +++ b/src/lib/server/trigger.h @@ -48,8 +48,6 @@ int trigger_exec(unlang_interpret_t *intp, CONF_SECTION const *cs, char const *name, bool rate_limit, fr_pair_list_t *args) CC_HINT(nonnull(3)); -void trigger_exec_free(void); - bool trigger_enabled(void); void trigger_args_afrom_server(TALLOC_CTX *ctx, fr_pair_list_t *list, char const *server, uint16_t port);