27RCSID(
"$Id: 0d01316db7156718e553006a672ab0d9425c98e6 $")
29#include <freeradius-devel/util/debug.h>
30#include <freeradius-devel/util/dlist.h>
31#include <freeradius-devel/util/atexit.h>
36#if defined(DEBUG_ATEXIT) && !defined(NDEBUG)
37# define ATEXIT_DEBUG FR_FAULT_LOG
39# define ATEXIT_DEBUG(...)
72static pthread_mutex_t fr_atexit_global_mutex = PTHREAD_MUTEX_INITIALIZER;
84 ATEXIT_DEBUG(
"%s - Thread %u freeing %p/%p func=%p, uctx=%p (alloced %s:%d)",
85 __FUNCTION__, (
unsigned int)pthread_self(),
116 ATEXIT_DEBUG(
"%s - Thread %u arming %p/%p func=%p, uctx=%p (alloced %s:%d)",
117 __FUNCTION__, (
unsigned int)pthread_self(),
131 ATEXIT_DEBUG(
"%s - Freeing destructor list %p", __FUNCTION__, list);
143 pthread_mutex_lock(&fr_atexit_global_mutex);
150 pthread_mutex_unlock(&fr_atexit_global_mutex);
151 TALLOC_FREE(fr_atexit_threads);
173 if (
unlikely(!fr_atexit_threads))
return -1;
175 ATEXIT_DEBUG(
"%s - Alloced threads destructor list %p", __FUNCTION__, fr_atexit_threads);
187#define CHECK_GLOBAL_SETUP() \
190 pthread_mutex_lock(&fr_atexit_global_mutex); \
191 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
192 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
193 pthread_mutex_unlock(&fr_atexit_global_mutex); \
194 if (_ret < 0) return _ret; \
197#define CHECK_GLOBAL_SETUP() \
200 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
201 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
202 if (_ret < 0) return _ret; \
231 unsigned int count = 0;
236 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
238 ATEXIT_DEBUG(
"%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
244 talloc_set_destructor(disarm, NULL);
265 ATEXIT_DEBUG(
"%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
269 talloc_set_destructor(e, NULL);
288 unsigned int count = 0;
296 ATEXIT_DEBUG(
"%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
307 to_free->func, to_free->uctx,
308 to_free->file, to_free->line);
337 unsigned int count = 0;
345 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
347 ATEXIT_DEBUG(
"%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
358 to_free->func, to_free->uctx,
359 to_free->file, to_free->line);
367 if (!fr_atexit_threads)
return 0;
375 if (!e->
func)
continue;
380 if ((ee->
func != func) || ((ee->
uctx != uctx) && uctx_scope))
continue;
382 ATEXIT_DEBUG(
"%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
384 (
unsigned int)pthread_self(),
394 to_free->func, to_free->uctx,
395 to_free->file, to_free->line);
414 bool save_is_exiting;
416 pthread_mutex_lock(&fr_atexit_global_mutex);
418 pthread_mutex_unlock(&fr_atexit_global_mutex);
420 return save_is_exiting;
432 ATEXIT_DEBUG(
"%s - Freeing _Thread_local destructor list %p", __FUNCTION__, list);
435 pthread_mutex_lock(&fr_atexit_global_mutex);
436 list->
e->
func = NULL;
437 pthread_mutex_unlock(&fr_atexit_global_mutex);
445static void _thread_local_pthread_free(
void *list)
454static int _thread_local_free(
void *list)
466int _fr_atexit_thread_local(
char const *
file,
int line,
485 ATEXIT_DEBUG(
"%s - Thread %u alloced _Thread_local destructor list %p",
487 (
unsigned int)pthread_self(), list);
490 (void) pthread_key_create(&list->
key, _thread_local_pthread_free);
499 (void) pthread_setspecific(list->
key, list);
500 talloc_set_destructor(list, _thread_local_list_free);
509 pthread_mutex_lock(&fr_atexit_global_mutex);
515 pthread_mutex_unlock(&fr_atexit_global_mutex);
541 unsigned int count = 0;
548 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
550 ATEXIT_DEBUG(
"%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
552 (
unsigned int)pthread_self(),
556 talloc_set_destructor(disarm, NULL);
577 ATEXIT_DEBUG(
"%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
579 (
unsigned int)pthread_self(),
581 talloc_set_destructor(e, NULL);
603 unsigned int count = 0;
611 if (!e->
func)
continue;
616 ATEXIT_DEBUG(
"%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
618 (
unsigned int)pthread_self(),
619 list, ee, ee->func, ee->uctx, ee->file, ee->line);
628 to_free->func, to_free->uctx,
629 to_free->file, to_free->line
642bool fr_atexit_thread_is_exiting(
void)
unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
Remove a specific global destructor (without executing it)
static fr_atexit_list_t * fr_atexit_global
char const * file
File where this exit handler was added.
#define CHECK_GLOBAL_SETUP()
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
fr_dlist_t entry
Entry in the handler dlist.
static _Thread_local bool thread_is_exiting
pthread_key_t key
Key used to trigger thread local destructors.
fr_atexit_t func
Function to call.
#define ATEXIT_DEBUG(...)
fr_dlist_head_t head
Head of the list of destructors.
static fr_atexit_entry_t * atexit_entry_alloc(char const *file, int line, fr_atexit_list_t *list, fr_atexit_t func, void const *uctx)
Allocate a new exit handler entry.
void fr_atexit_global_disarm_all(void)
Remove all global destructors (without executing them)
fr_atexit_entry_t * e
Inserted into the global exit handler list to ensure this memory is cleaned up.
static int _atexit_entry_free(fr_atexit_entry_t *e)
Call the exit handler.
int line
Line where this exit handler was added.
static void _global_free(void)
Free any thread-local exit handler lists that pthread_key failed to fre.
bool fr_atexit_is_exiting(void)
Return whether we're currently in the teardown phase.
static int _destructor_list_free(fr_atexit_list_t *list)
Talloc destructor for freeing list elements in order.
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
int fr_atexit_trigger(bool uctx_scope, fr_atexit_t func, void const *uctx)
Iterates through all thread local destructor lists, causing destructor to be triggered.
int _atexit_global(char const *file, int line, fr_atexit_t func, void const *uctx)
Add a free function to be called when the process exits.
fr_atexit_list_t * list
List this entry is in.
Entry in exit handler list.
Head of a list of exit handlers.
int(* fr_atexit_t)(void *uctx)
Destructor callback.
#define fr_atexit_thread_trigger_all(...)
#define fr_atexit_thread_local_disarm(...)
#define fr_atexit_thread_local_disarm_all(...)
#define fr_atexit_thread_local(_name, _free, _uctx)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
static bool fr_dlist_entry_in_list(fr_dlist_t const *entry)
Check if a list entry is part of a list.
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
static int fr_dlist_insert_head(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the head of a list.
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Head of a doubly linked list.
Entry in a doubly linked list.
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.