The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
atexit.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Macros to abstract Thread Local Storage
18 *
19 * Simplifies calling thread local destructors (called when the thread exits).
20 *
21 * @file lib/util/atexit.c
22 *
23 * @copyright 2020-2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2020 The FreeRADIUS server project
25 */
26
27RCSID("$Id: bce89e6227bf06fa14d7a8ac276465d94adee505 $")
28
29#include <freeradius-devel/util/debug.h>
30#include <freeradius-devel/util/dlist.h>
31#include <freeradius-devel/util/atexit.h>
32
33#include <stdatomic.h>
34
35#ifdef HAVE_PTHREADS
36#endif
37
38#if defined(DEBUG_ATEXIT) && !defined(NDEBUG)
39# define ATEXIT_DEBUG FR_FAULT_LOG
40#else
41# define ATEXIT_DEBUG(...)
42#endif
43
45
46/** Entry in exit handler list
47 *
48 */
49typedef struct {
50 fr_dlist_t entry; //!< Entry in the handler dlist.
51 fr_atexit_list_t *list; //!< List this entry is in.
52
53 fr_atexit_t func; //!< Function to call.
54 void *uctx; //!< uctx to pass.
55
56 char const *file; //!< File where this exit handler was added.
57 int line; //!< Line where this exit handler was added.
59
60/** Head of a list of exit handlers
61 *
62 */
64 fr_dlist_head_t head; //!< Head of the list of destructors
65
66 pthread_key_t key; //!< Key used to trigger thread local destructors.
67 fr_atexit_entry_t *e; //!< Inserted into the global exit handler list
68 ///< to ensure this memory is cleaned up.
69};
70
71#ifdef HAVE_PTHREADS
72static _Thread_local fr_atexit_list_t *fr_atexit_thread_local = NULL;
73static fr_atexit_list_t *fr_atexit_threads = NULL;
74static pthread_mutex_t fr_atexit_global_mutex = PTHREAD_MUTEX_INITIALIZER;
75#endif
76
78static bool is_exiting;
79static _Thread_local bool thread_is_exiting;
80
81/** Latched once main has decided no more thread-local pools should be handed out
82 *
83 * `fr_atexit_thread_trigger_all()` runs every registered thread destructor on
84 * the calling (main) thread, including ones that free `_Thread_local` pools
85 * owned by threads we don't manage (librdkafka's bg threads, perl, etc.). We
86 * can free the underlying chunks but can't reset another thread's TLS slot,
87 * so those threads keep a dangling pointer in their per-thread cache. Code
88 * that lazily allocates a TLS-cached pool (log.c, sbuff.c, strerror.c, ...)
89 * checks this flag *before* reading its TLS slot and falls back to
90 * `talloc_*(NULL, ...)` (or returns NULL) when set, side-stepping the
91 * dangling pointer entirely.
92 *
93 * Set once, never cleared. Relaxed atomic because this is a one-shot signal
94 * with no other state synchronised through it - readers tolerate observing
95 * the old value briefly.
96 */
97static atomic_bool thread_local_alloc_disabled;
98
99/** Call the exit handler
100 *
101 */
103{
104 ATEXIT_DEBUG("%s - Thread %u freeing %p/%p func=%p, uctx=%p (alloced %s:%d)",
105 __FUNCTION__, (unsigned int)pthread_self(),
106 e->list, e, e->func, e->uctx, e->file, e->line);
107
109
110 /*
111 * If the exit handler wasn't disarmed, call it...
112 */
113 if (e->func) e->func(e->uctx);
114
115 return 0;
116}
117
118/** Allocate a new exit handler entry
119 *
120 */
122 fr_atexit_list_t *list,
123 fr_atexit_t func, void const *uctx)
124{
126
127 e = talloc_zero(list, fr_atexit_entry_t);
128 if (unlikely(!e)) return NULL;
129
130 e->list = list;
131 e->func = func;
132 e->uctx = UNCONST(void *, uctx);
133 e->file = file;
134 e->line = line;
135
136 ATEXIT_DEBUG("%s - Thread %u arming %p/%p func=%p, uctx=%p (alloced %s:%d)",
137 __FUNCTION__, (unsigned int)pthread_self(),
138 list, e, e->func, e->uctx, e->file, e->line);
139
140 fr_dlist_insert_head(&list->head, e);
141 talloc_set_destructor(e, _atexit_entry_free);
142
143 return e;
144}
145
146/** Talloc destructor for freeing list elements in order
147 *
148 */
150{
151 ATEXIT_DEBUG("%s - Freeing destructor list %p", __FUNCTION__, list);
152
153 fr_dlist_talloc_free(&list->head); /* Free in order */
154 return 0;
155}
156
157/** Free any thread-local exit handler lists that pthread_key failed to fre
158 *
159 */
160static void _global_free(void)
161{
162#ifdef HAVE_PTHREADS
163 pthread_mutex_lock(&fr_atexit_global_mutex);
164#endif
165
166 fr_cond_assert_msg(!is_exiting, "Global free function called multiple times");
167 is_exiting = true;
168
169#ifdef HAVE_PTHREADS
170 pthread_mutex_unlock(&fr_atexit_global_mutex);
171 TALLOC_FREE(fr_atexit_threads); /* Forcefully cleanup any thread-specific memory */
172#endif
173 TALLOC_FREE(fr_atexit_global);
174}
175
176/** Setup the atexit handler, should be called at the start of a program's execution
177 *
178 */
180{
181 if (fr_atexit_global) return 0;
182
183 fr_atexit_global = talloc_zero(NULL, fr_atexit_list_t);
184 if (unlikely(!fr_atexit_global)) return -1;
185
186 ATEXIT_DEBUG("%s - Alloced global destructor list %p", __FUNCTION__, fr_atexit_global);
187
189 talloc_set_destructor(fr_atexit_global, _destructor_list_free);
190
191#ifdef HAVE_PTHREADS
192 fr_atexit_threads = talloc_zero(NULL, fr_atexit_list_t);
193 if (unlikely(!fr_atexit_threads)) {
194 TALLOC_FREE(fr_atexit_global);
195 return -1;
196 }
197
198 ATEXIT_DEBUG("%s - Alloced threads destructor list %p", __FUNCTION__, fr_atexit_threads);
199
200 fr_dlist_talloc_init(&fr_atexit_threads->head, fr_atexit_entry_t, entry);
201 talloc_set_destructor(fr_atexit_threads, _destructor_list_free);
202#endif
203
204 atexit(_global_free); /* Call all remaining destructors at process exit */
205
206 return 0;
207}
208
209#ifdef HAVE_PTHREADS
210#define CHECK_GLOBAL_SETUP() \
211do { \
212 int _ret = 0; \
213 pthread_mutex_lock(&fr_atexit_global_mutex); \
214 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
215 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
216 pthread_mutex_unlock(&fr_atexit_global_mutex); \
217 if (_ret < 0) return _ret; \
218} while(0)
219#else
220#define CHECK_GLOBAL_SETUP() \
221do { \
222 int _ret = 0; \
223 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
224 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
225 if (_ret < 0) return _ret; \
226} while(0)
227#endif
228
229/** Add a free function to be called when the process exits
230 *
231 */
232int _atexit_global(char const *file, int line, fr_atexit_t func, void const *uctx)
233{
235
236 if (unlikely(atexit_entry_alloc(file, line, fr_atexit_global, func, uctx) == NULL)) return -1;
237
238 return 0;
239}
240
241/** Remove a specific global destructor (without executing it)
242 *
243 * @note This function's primary purpose is to help diagnose issues with destructors
244 * from within a debugger.
245 *
246 * @param[in] uctx_scope Only process entries where the func and scope both match.
247 * @param[in] func Entries matching this function will be disarmed.
248 * @param[in] uctx associated with the entry.
249 * @return How many global destructors were disarmed.
250 */
251unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
252{
253 fr_atexit_entry_t *e = NULL;
254 unsigned int count = 0;
255
256 while ((e = fr_dlist_next(&fr_atexit_global->head, e))) {
257 fr_atexit_entry_t *disarm;
258
259 if ((e->func != func) || ((e->uctx != uctx) && uctx_scope)) continue;
260
261 ATEXIT_DEBUG("%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
262 __FUNCTION__,
263 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
264
265 disarm = e;
267 talloc_set_destructor(disarm, NULL);
268 talloc_free(disarm);
269
270 count++;
271 }
272
273 return count;
274}
275
276/** Remove all global destructors (without executing them)
277 *
278 * @note This function's primary purpose is to help diagnose issues with destructors
279 * from within a debugger.
280 */
282{
283 fr_atexit_entry_t *e = NULL;
284
285 if (!fr_atexit_global) return;
286
287 while ((e = fr_dlist_pop_head(&fr_atexit_global->head))) {
288 ATEXIT_DEBUG("%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
289 __FUNCTION__,
290 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
291
292 talloc_set_destructor(e, NULL);
293 talloc_free(e);
294 }
295}
296
297/** Cause all global free triggers to fire
298 *
299 * This is necessary when libraries (perl) register their own
300 * atexit handlers using the normal POSIX mechanism, and we need
301 * to ensure all our atexit handlers fire before so any global
302 * deinit is done explicitly by us.
303 *
304 * @return
305 * - >= 0 The number of atexit handlers triggered on success.
306 * - <0 the return code from any atexit handlers that returned an error.
307 */
309{
310 fr_atexit_entry_t *e = NULL, *to_free;
311 unsigned int count = 0;
312
313 /*
314 * Iterate over the list of thread local
315 * destructor lists running the
316 * destructors.
317 */
318 while ((e = fr_dlist_next(&fr_atexit_global->head, e))) {
319 ATEXIT_DEBUG("%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
320 __FUNCTION__,
321 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
322
323 count++;
324 to_free = e;
326 if (talloc_free(to_free) < 0) {
327 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
328 " (alloced %s:%d)",
329 fr_atexit_global, to_free,
330 to_free->func, to_free->uctx,
331 to_free->file, to_free->line);
332 return -1;
333 }
334 }
335
336 return count;
337}
338
339/** Iterates through all thread local destructor lists, causing destructor to be triggered
340 *
341 * This should only be called by the main process not by threads.
342 *
343 * The main purpose of the function is to force cleanups at a specific time for problematic
344 * destructors.
345 *
346 * @param[in] uctx_scope Only process entries where the func and scope both match.
347 * @param[in] func Entries matching this function will be triggered.
348 * @param[in] uctx associated with the entry.
349 * @return
350 * - >= 0 The number of atexit handlers triggered on success.
351 * - <0 the return code from any atexit handlers that returned an error.
352 */
353int fr_atexit_trigger(bool uctx_scope, fr_atexit_t func, void const *uctx)
354{
355 fr_atexit_entry_t *e = NULL, *to_free;
356#ifdef HAVE_PTHREADS
358 fr_atexit_list_t *list;
359#endif
360 unsigned int count = 0;
361
362 if (!fr_atexit_global) goto do_threads;
363
364 /*
365 * Iterate over the global destructors
366 */
367 while ((e = fr_dlist_next(&fr_atexit_global->head, e))) {
368 if ((e->func != func) || ((e->uctx != uctx) && uctx_scope)) continue;
369
370 ATEXIT_DEBUG("%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
371 __FUNCTION__,
372 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
373
374 count++;
375 to_free = e;
377 if (talloc_free(to_free) < 0) {
378 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
379 " (alloced %s:%d)",
380 fr_atexit_global, to_free,
381 to_free->func, to_free->uctx,
382 to_free->file, to_free->line);
383 return -1;
384 }
385 }
386 e = NULL;
387
388do_threads:
389#ifdef HAVE_PTHREADS
390 if (!fr_atexit_threads) return 0;
391
392 /*
393 * Iterate over the list of thread local
394 * destructor lists running the
395 * destructors.
396 */
397 while ((e = fr_dlist_next(&fr_atexit_threads->head, e))) {
398 if (!e->func) continue; /* thread already joined */
399
400 list = talloc_get_type_abort(e->uctx, fr_atexit_list_t);
401 ee = NULL;
402 while ((ee = fr_dlist_next(&list->head, ee))) {
403 if ((ee->func != func) || ((ee->uctx != uctx) && uctx_scope)) continue;
404
405 ATEXIT_DEBUG("%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
406 __FUNCTION__,
407 (unsigned int)pthread_self(),
408 list, ee, ee->func, ee->uctx, ee->file, ee->line);
409
410 count++;
411 to_free = ee;
412 ee = fr_dlist_remove(&list->head, ee);
413 if (talloc_free(to_free) < 0) {
414 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
415 " (alloced %s:%d)",
416 list, to_free,
417 to_free->func, to_free->uctx,
418 to_free->file, to_free->line);
419 return -1;
420 }
421 }
422 }
423#endif
424
425 return count;
426}
427
428
429/** Disable lazy allocation of thread-local caches for the rest of the process
430 *
431 * Call this from main *before* `fr_atexit_thread_trigger_all()`. After it
432 * returns, every TLS-pool initialiser that consults
433 * `fr_atexit_thread_local_alloc_disabled()` falls back to the un-cached path,
434 * so `_Thread_local` slots in other threads aren't read after the trigger
435 * frees their backing memory.
436 */
441
442/** Has @ref fr_atexit_thread_local_disable_alloc been called yet
443 *
444 */
449
450/** Return whether we're currently in the teardown phase
451 *
452 * When this function returns true no more thread local or global
453 * destructors can be added.
454 */
456{
457#ifdef HAVE_PTHREADS
458 bool save_is_exiting;
459
460 pthread_mutex_lock(&fr_atexit_global_mutex);
461 save_is_exiting = is_exiting;
462 pthread_mutex_unlock(&fr_atexit_global_mutex);
463
464 return save_is_exiting;
465#else
466 return is_exiting;
467#endif
468}
469
470#ifdef HAVE_PTHREADS
471/** Talloc destructor for freeing list elements in order
472 *
473 */
474static int _thread_local_list_free(fr_atexit_list_t *list)
475{
476 ATEXIT_DEBUG("%s - Freeing _Thread_local destructor list %p", __FUNCTION__, list);
477
478 fr_dlist_talloc_free(&list->head); /* Free in order */
479 pthread_mutex_lock(&fr_atexit_global_mutex);
480 list->e->func = NULL; /* Disarm the global entry that'd free the thread-specific list */
481 pthread_mutex_unlock(&fr_atexit_global_mutex);
482 return 0;
483}
484
485/** Run all the thread local destructors
486 *
487 * @param[in] list The thread-specific exit handler list.
488 */
489static void _thread_local_pthread_free(void *list)
490{
491 talloc_free(list);
492}
493
494/** Run all the thread local destructors
495 *
496 * @param[in] list The thread-specific exit handler list.
497 */
498static int _thread_local_free(void *list)
499{
500 thread_is_exiting = true;
501 return talloc_free(list);
502}
503
504/** Add a new destructor
505 *
506 * @return
507 * - 0 on success.
508 * - -1 on memory allocation failure;
509 */
510int _fr_atexit_thread_local(char const *file, int line,
511 fr_atexit_t func, void const *uctx)
512{
514
515 /*
516 * Initialise the thread local list, just for pthread_exit().
517 */
519 fr_atexit_list_t *list;
520
521 /*
522 * Must be heap allocated, because thread local
523 * structures can be freed before the key
524 * destructor is run (depending on platform).
525 */
526 list = talloc_zero(NULL, fr_atexit_list_t);
527 if (unlikely(!list)) return -1;
528
529 ATEXIT_DEBUG("%s - Thread %u alloced _Thread_local destructor list %p",
530 __FUNCTION__,
531 (unsigned int)pthread_self(), list);
532
534 (void) pthread_key_create(&list->key, _thread_local_pthread_free);
535
536 /*
537 * We need to pass in a pointer to the heap
538 * memory because, again, the thread local
539 * indirection table may have disappeared
540 * by the time the thread destructor is
541 * called.
542 */
543 (void) pthread_setspecific(list->key, list);
544 talloc_set_destructor(list, _thread_local_list_free);
545
546 /*
547 * Add a destructor for the thread-local list
548 * The pthread based destructor will disarm
549 * this if it fires, but leave it enabled if
550 * it doesn't, thus ensuring the memory is
551 * *always* freed one way or another.
552 */
553 pthread_mutex_lock(&fr_atexit_global_mutex);
554 list->e = atexit_entry_alloc(file, line,
555 fr_atexit_threads,
556 _thread_local_free,
557 list);
558
559 pthread_mutex_unlock(&fr_atexit_global_mutex);
560
562 }
563
564 /*
565 * Now allocate the actual atexit handler entry
566 */
567 if (atexit_entry_alloc(file, line, fr_atexit_thread_local, func, uctx) == NULL) return -1;
568
569 return 0;
570}
571
572/** Remove a specific destructor for this thread (without executing them)
573 *
574 * @note This function's primary purpose is to help diagnose issues with destructors
575 * from within a debugger.
576 *
577 * @param[in] uctx_scope Only process entries where the func and scope both match.
578 * @param[in] func Entries matching this function will be disarmed.
579 * @param[in] uctx associated with the entry.
580 * @return How many destructors were disarmed.
581 */
582unsigned int fr_atexit_thread_local_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
583{
584 fr_atexit_entry_t *e = NULL;
585 unsigned int count = 0;
586
587 if (!fr_atexit_thread_local) return 0;
588
589 while ((e = fr_dlist_next(&fr_atexit_thread_local->head, e))) {
590 fr_atexit_entry_t *disarm;
591
592 if ((e->func != func) || ((e->uctx != uctx) && uctx_scope)) continue;
593
594 ATEXIT_DEBUG("%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
595 __FUNCTION__,
596 (unsigned int)pthread_self(),
597 fr_atexit_thread_local, e, e->func, e->uctx, e->file, e->line);
598 disarm = e;
600 talloc_set_destructor(disarm, NULL);
601 talloc_free(disarm);
602
603 count++;
604 }
605
606 return count;
607}
608
609/** Remove all destructors for this thread (without executing them)
610 *
611 * @note This function's primary purpose is to help diagnose issues with destructors
612 * from within a debugger.
613 */
615{
616 fr_atexit_entry_t *e = NULL;
617
618 if (!fr_atexit_thread_local) return;
619
620 while ((e = fr_dlist_pop_head(&fr_atexit_thread_local->head))) {
621 ATEXIT_DEBUG("%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
622 __FUNCTION__,
623 (unsigned int)pthread_self(),
624 fr_atexit_thread_local, e, e->func, e->uctx, e->file, e->line);
625 talloc_set_destructor(e, NULL);
626 talloc_free(e);
627 }
628}
629
630/** Cause all thread local free triggers to fire
631 *
632 * This is necessary when we're running in single threaded mode
633 * to ensure all "thread-local" memory (which isn't actually thread local)
634 * is cleaned up.
635 *
636 * One example is the OpenSSL log BIOs which must be cleaned up
637 * before fr_openssl_free is called.
638 *
639 * @return
640 * - >= 0 The number of atexit handlers triggered on success.
641 * - <0 the return code from any atexit handlers that returned an error.
642 */
644{
645 fr_atexit_entry_t *e = NULL, *ee, *to_free;
646 fr_atexit_list_t *list;
647 unsigned int count = 0;
648
649 /*
650 * Disable TLS-cached pool handouts before we start freeing
651 * any of them. Threads we don't manage (librdkafka's bg
652 * threads, etc.) keep dangling pointers in their
653 * `_Thread_local` slots once the chunks here are reaped, so
654 * any TLS-pool initialiser that consults the flag falls back
655 * to `talloc_*(NULL, ...)` from now on.
656 */
658
659 /*
660 * Iterate over the list of thread local
661 * destructor lists running the
662 * destructors.
663 */
664 while ((e = fr_dlist_next(&fr_atexit_threads->head, e))) {
665 if (!e->func) continue; /* thread already joined */
666
667 list = talloc_get_type_abort(e->uctx, fr_atexit_list_t);
668 ee = NULL;
669 while ((ee = fr_dlist_next(&list->head, ee))) {
670 ATEXIT_DEBUG("%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
671 __FUNCTION__,
672 (unsigned int)pthread_self(),
673 list, ee, ee->func, ee->uctx, ee->file, ee->line);
674
675 count++;
676 to_free = ee;
677 ee = fr_dlist_remove(&list->head, ee);
678 if (talloc_free(to_free) < 0) {
679 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
680 " (alloced %s:%d)",
681 list, to_free,
682 to_free->func, to_free->uctx,
683 to_free->file, to_free->line
684 );
685 return -1;
686 }
687 }
688 }
689
690 return count;
691}
692
693/** Return whether the thread is currently being cleaned up
694 *
695 */
696bool fr_atexit_thread_is_exiting(void)
697{
698 return thread_is_exiting;
699}
700#endif
int const char * file
Definition acutest.h:702
int const char int line
Definition acutest.h:702
bool fr_atexit_thread_local_alloc_disabled(void)
Has fr_atexit_thread_local_disable_alloc been called yet.
Definition atexit.c:445
void fr_atexit_thread_local_disable_alloc(void)
Disable lazy allocation of thread-local caches for the rest of the process.
Definition atexit.c:437
unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
Remove a specific global destructor (without executing it)
Definition atexit.c:251
static fr_atexit_list_t * fr_atexit_global
Definition atexit.c:77
char const * file
File where this exit handler was added.
Definition atexit.c:56
#define CHECK_GLOBAL_SETUP()
Definition atexit.c:220
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition atexit.c:179
fr_dlist_t entry
Entry in the handler dlist.
Definition atexit.c:50
static _Thread_local bool thread_is_exiting
Definition atexit.c:79
pthread_key_t key
Key used to trigger thread local destructors.
Definition atexit.c:66
fr_atexit_t func
Function to call.
Definition atexit.c:53
#define ATEXIT_DEBUG(...)
Definition atexit.c:41
fr_dlist_head_t head
Head of the list of destructors.
Definition atexit.c:64
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.
Definition atexit.c:121
void fr_atexit_global_disarm_all(void)
Remove all global destructors (without executing them)
Definition atexit.c:281
fr_atexit_entry_t * e
Inserted into the global exit handler list to ensure this memory is cleaned up.
Definition atexit.c:67
static int _atexit_entry_free(fr_atexit_entry_t *e)
Call the exit handler.
Definition atexit.c:102
static atomic_bool thread_local_alloc_disabled
Latched once main has decided no more thread-local pools should be handed out.
Definition atexit.c:97
static bool is_exiting
Definition atexit.c:78
void * uctx
uctx to pass.
Definition atexit.c:54
int line
Line where this exit handler was added.
Definition atexit.c:57
static void _global_free(void)
Free any thread-local exit handler lists that pthread_key failed to fre.
Definition atexit.c:160
bool fr_atexit_is_exiting(void)
Return whether we're currently in the teardown phase.
Definition atexit.c:455
static int _destructor_list_free(fr_atexit_list_t *list)
Talloc destructor for freeing list elements in order.
Definition atexit.c:149
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition atexit.c:308
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.
Definition atexit.c:353
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.
Definition atexit.c:232
fr_atexit_list_t * list
List this entry is in.
Definition atexit.c:51
Entry in exit handler list.
Definition atexit.c:49
Head of a list of exit handlers.
Definition atexit.c:63
int(* fr_atexit_t)(void *uctx)
Destructor callback.
Definition atexit.h:44
#define fr_atexit_thread_trigger_all(...)
Definition atexit.h:236
#define fr_atexit_thread_local_disarm(...)
Definition atexit.h:234
#define fr_atexit_thread_local_disarm_all(...)
Definition atexit.h:235
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:224
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:512
#define unlikely(_x)
Definition build.h:407
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:148
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
static bool fr_dlist_entry_in_list(fr_dlist_t const *entry)
Check if a list entry is part of a list.
Definition dlist.h:145
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
Definition dlist.h:892
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
Definition dlist.h:654
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
static int fr_dlist_insert_head(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the head of a list.
Definition dlist.h:320
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
talloc_free(hp)
return count
Definition module.c:155
@ memory_order_relaxed
Definition stdatomic.h:127
#define atomic_load_explicit(object, order)
Definition stdatomic.h:312
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:314
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84