Skip to content

Commit

Permalink
Move xlat instance freeing to the atexit handler, and remove server free
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed May 15, 2024
1 parent e42947b commit f2da64d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
2 changes: 0 additions & 2 deletions src/bin/radiusd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,6 @@ int main(int argc, char *argv[])
*/
fr_atexit_thread_trigger_all();

server_free();

#ifdef WITH_TLS
fr_openssl_free(); /* Cleanup any memory alloced by OpenSSL and placed into globals */
#endif
Expand Down
2 changes: 0 additions & 2 deletions src/bin/unit_test_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,6 @@ int main(int argc, char *argv[])
*/
fr_atexit_thread_trigger_all();

server_free();

/*
* Virtual servers need to be freed before modules
* as state entries containing data with module-specific
Expand Down
12 changes: 0 additions & 12 deletions src/lib/server/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,3 @@ int server_init(CONF_SECTION *cs)

return 0;
}

/** Free src/lib/server/
*
* This is just so that the callers don't need to call a million functions.
*/
void server_free(void)
{
/*
* Free xlat instance data, and call any detach methods
*/
xlat_instances_free();
}
1 change: 0 additions & 1 deletion src/lib/server/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ RCSIDH(base_h, "$Id$")
#include <freeradius-devel/util/base.h>

int server_init(CONF_SECTION *cs);
void server_free(void);
50 changes: 29 additions & 21 deletions src/lib/unlang/xlat_inst.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,41 @@ void xlat_thread_detach(void)
TALLOC_FREE(xlat_thread_inst_tree);
}

/** Walk over all registered instance data and free them explicitly
*
* This must be called before any modules or xlats are deregistered/unloaded and before
* the mainconfig is freed, as the xlat_t need to still exist in order to call
* the detach functions within them.
*/
static int _xlat_instances_free(UNUSED void *uctx)
{
xlat_inst_t *xi;

/*
* When we get to zero instances the heap
* is freed, so we need to check there's
* still a heap to pass to fr_heap_pop.
*/
while (xlat_inst_tree && (xi = fr_heap_pop(&xlat_inst_tree))) talloc_free(xi);

return 0;
}

static int _xlat_instances_init(UNUSED void *uctx)
{
MEM(xlat_inst_tree = fr_heap_talloc_alloc(NULL, _xlat_inst_cmp, xlat_inst_t, idx, 0));

return 0;
}

/** Initialise the xlat instance data code
*
*/
static int xlat_instantiate_init(void)
{
if (unlikely(xlat_inst_tree != NULL)) return 0;
int ret;

xlat_inst_tree = fr_heap_talloc_alloc(NULL, _xlat_inst_cmp, xlat_inst_t, idx, 0);
if (!xlat_inst_tree) return -1;
fr_atexit_global_once_ret(&ret, _xlat_instances_init, _xlat_instances_free, NULL);

return 0;
}
Expand Down Expand Up @@ -694,21 +720,3 @@ int xlat_finalize(xlat_exp_head_t *head, fr_event_list_t *runtime_el)
}
return xlat_instantiate_ephemeral(head, runtime_el);
}

/** Walk over all registered instance data and free them explicitly
*
* This must be called before any modules or xlats are deregistered/unloaded and before
* the mainconfig is freed, as the xlat_t need to still exist in order to call
* the detach functions within them.
*/
void xlat_instances_free(void)
{
xlat_inst_t *xi;

/*
* When we get to zero instances the heap
* is freed, so we need to check there's
* still a heap to pass to fr_heap_pop.
*/
while (xlat_inst_tree && (xi = fr_heap_pop(&xlat_inst_tree))) talloc_free(xi);
}

0 comments on commit f2da64d

Please sign in to comment.