]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Move trigger free to the atexit handlers
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 15 May 2024 01:52:11 +0000 (19:52 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 15 May 2024 01:53:35 +0000 (19:53 -0600)
src/lib/server/base.c
src/lib/server/trigger.c
src/lib/server/trigger.h

index ab5ad9e729ec1780b240b8ffc235abe6aca9fb5c..e680d4c5175b3901cdb5201a2074ff00897c2fce 100644 (file)
@@ -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();
 }
index 82d354c97f627bd85a9d8975abdb6df274c1243f..16be837eb8bec08e434d8120d6c08e99357be578 100644 (file)
  *
  * @copyright 2015 The FreeRADIUS server project
  */
-
 RCSID("$Id$")
 
 #include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
 #include <freeradius-devel/server/base.h>
 #include <freeradius-devel/server/cf_parse.h>
+#include <freeradius-devel/server/cf_util.h>
 #include <freeradius-devel/unlang/function.h>
 #include <freeradius-devel/unlang/interpret.h>
+
+#include <freeradius-devel/util/atexit.h>
 #include <freeradius-devel/util/debug.h>
+
 #include <sys/wait.h>
 
 /** 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;
+}
index d8b65a15ae0437afcbb8709266207e15944d0577..5756656992b38b8e2fafae15a4c98e3fabf0d82f 100644 (file)
@@ -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);