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: 47f0261a68d4435fc31f0afb4871471a5ec49a5b $")
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)) return -1;
194
195 ATEXIT_DEBUG("%s - Alloced threads destructor list %p", __FUNCTION__, fr_atexit_threads);
196
197 fr_dlist_talloc_init(&fr_atexit_threads->head, fr_atexit_entry_t, entry);
198 talloc_set_destructor(fr_atexit_threads, _destructor_list_free);
199#endif
200
201 atexit(_global_free); /* Call all remaining destructors at process exit */
202
203 return 0;
204}
205
206#ifdef HAVE_PTHREADS
207#define CHECK_GLOBAL_SETUP() \
208do { \
209 int _ret = 0; \
210 pthread_mutex_lock(&fr_atexit_global_mutex); \
211 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
212 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
213 pthread_mutex_unlock(&fr_atexit_global_mutex); \
214 if (_ret < 0) return _ret; \
215} while(0)
216#else
217#define CHECK_GLOBAL_SETUP() \
218do { \
219 int _ret = 0; \
220 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
221 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
222 if (_ret < 0) return _ret; \
223} while(0)
224#endif
225
226/** Add a free function to be called when the process exits
227 *
228 */
229int _atexit_global(char const *file, int line, fr_atexit_t func, void const *uctx)
230{
232
233 if (unlikely(atexit_entry_alloc(file, line, fr_atexit_global, func, uctx) == NULL)) return -1;
234
235 return 0;
236}
237
238/** Remove a specific global destructor (without executing it)
239 *
240 * @note This function's primary purpose is to help diagnose issues with destructors
241 * from within a debugger.
242 *
243 * @param[in] uctx_scope Only process entries where the func and scope both match.
244 * @param[in] func Entries matching this function will be disarmed.
245 * @param[in] uctx associated with the entry.
246 * @return How many global destructors were disarmed.
247 */
248unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
249{
250 fr_atexit_entry_t *e = NULL;
251 unsigned int count = 0;
252
253 while ((e = fr_dlist_next(&fr_atexit_global->head, e))) {
254 fr_atexit_entry_t *disarm;
255
256 if ((e->func != func) || ((e->uctx != uctx) && uctx_scope)) continue;
257
258 ATEXIT_DEBUG("%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
259 __FUNCTION__,
260 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
261
262 disarm = e;
264 talloc_set_destructor(disarm, NULL);
265 talloc_free(disarm);
266
267 count++;
268 }
269
270 return count;
271}
272
273/** Remove all global destructors (without executing them)
274 *
275 * @note This function's primary purpose is to help diagnose issues with destructors
276 * from within a debugger.
277 */
279{
280 fr_atexit_entry_t *e = NULL;
281
282 if (!fr_atexit_global) return;
283
284 while ((e = fr_dlist_pop_head(&fr_atexit_global->head))) {
285 ATEXIT_DEBUG("%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
286 __FUNCTION__,
287 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
288
289 talloc_set_destructor(e, NULL);
290 talloc_free(e);
291 }
292}
293
294/** Cause all global free triggers to fire
295 *
296 * This is necessary when libraries (perl) register their own
297 * atexit handlers using the normal POSIX mechanism, and we need
298 * to ensure all our atexit handlers fire before so any global
299 * deinit is done explicitly by us.
300 *
301 * @return
302 * - >= 0 The number of atexit handlers triggered on success.
303 * - <0 the return code from any atexit handlers that returned an error.
304 */
306{
307 fr_atexit_entry_t *e = NULL, *to_free;
308 unsigned int count = 0;
309
310 /*
311 * Iterate over the list of thread local
312 * destructor lists running the
313 * destructors.
314 */
315 while ((e = fr_dlist_next(&fr_atexit_global->head, e))) {
316 ATEXIT_DEBUG("%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
317 __FUNCTION__,
318 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
319
320 count++;
321 to_free = e;
323 if (talloc_free(to_free) < 0) {
324 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
325 " (alloced %s:%d)",
326 fr_atexit_global, to_free,
327 to_free->func, to_free->uctx,
328 to_free->file, to_free->line);
329 return -1;
330 }
331 }
332
333 return count;
334}
335
336/** Iterates through all thread local destructor lists, causing destructor to be triggered
337 *
338 * This should only be called by the main process not by threads.
339 *
340 * The main purpose of the function is to force cleanups at a specific time for problematic
341 * destructors.
342 *
343 * @param[in] uctx_scope Only process entries where the func and scope both match.
344 * @param[in] func Entries matching this function will be triggered.
345 * @param[in] uctx associated with the entry.
346 * @return
347 * - >= 0 The number of atexit handlers triggered on success.
348 * - <0 the return code from any atexit handlers that returned an error.
349 */
350int fr_atexit_trigger(bool uctx_scope, fr_atexit_t func, void const *uctx)
351{
352 fr_atexit_entry_t *e = NULL, *to_free;
353#ifdef HAVE_PTHREADS
355 fr_atexit_list_t *list;
356#endif
357 unsigned int count = 0;
358
359 if (!fr_atexit_global) goto do_threads;
360
361 /*
362 * Iterate over the global destructors
363 */
364 while ((e = fr_dlist_next(&fr_atexit_global->head, e))) {
365 if ((e->func != func) || ((e->uctx != uctx) && uctx_scope)) continue;
366
367 ATEXIT_DEBUG("%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
368 __FUNCTION__,
369 fr_atexit_global, e, e->func, e->uctx, e->file, e->line);
370
371 count++;
372 to_free = e;
374 if (talloc_free(to_free) < 0) {
375 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
376 " (alloced %s:%d)",
377 fr_atexit_global, to_free,
378 to_free->func, to_free->uctx,
379 to_free->file, to_free->line);
380 return -1;
381 }
382 }
383 e = NULL;
384
385do_threads:
386#ifdef HAVE_PTHREADS
387 if (!fr_atexit_threads) return 0;
388
389 /*
390 * Iterate over the list of thread local
391 * destructor lists running the
392 * destructors.
393 */
394 while ((e = fr_dlist_next(&fr_atexit_threads->head, e))) {
395 if (!e->func) continue; /* thread already joined */
396
397 list = talloc_get_type_abort(e->uctx, fr_atexit_list_t);
398 ee = NULL;
399 while ((ee = fr_dlist_next(&list->head, ee))) {
400 if ((ee->func != func) || ((ee->uctx != uctx) && uctx_scope)) continue;
401
402 ATEXIT_DEBUG("%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
403 __FUNCTION__,
404 (unsigned int)pthread_self(),
405 list, ee, ee->func, ee->uctx, ee->file, ee->line);
406
407 count++;
408 to_free = ee;
409 ee = fr_dlist_remove(&list->head, ee);
410 if (talloc_free(to_free) < 0) {
411 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
412 " (alloced %s:%d)",
413 list, to_free,
414 to_free->func, to_free->uctx,
415 to_free->file, to_free->line);
416 return -1;
417 }
418 }
419 }
420#endif
421
422 return count;
423}
424
425
426/** Disable lazy allocation of thread-local caches for the rest of the process
427 *
428 * Call this from main *before* `fr_atexit_thread_trigger_all()`. After it
429 * returns, every TLS-pool initialiser that consults
430 * `fr_atexit_thread_local_alloc_disabled()` falls back to the un-cached path,
431 * so `_Thread_local` slots in other threads aren't read after the trigger
432 * frees their backing memory.
433 */
438
439/** Has @ref fr_atexit_thread_local_disable_alloc been called yet
440 *
441 */
446
447/** Return whether we're currently in the teardown phase
448 *
449 * When this function returns true no more thread local or global
450 * destructors can be added.
451 */
453{
454#ifdef HAVE_PTHREADS
455 bool save_is_exiting;
456
457 pthread_mutex_lock(&fr_atexit_global_mutex);
458 save_is_exiting = is_exiting;
459 pthread_mutex_unlock(&fr_atexit_global_mutex);
460
461 return save_is_exiting;
462#else
463 return is_exiting;
464#endif
465}
466
467#ifdef HAVE_PTHREADS
468/** Talloc destructor for freeing list elements in order
469 *
470 */
471static int _thread_local_list_free(fr_atexit_list_t *list)
472{
473 ATEXIT_DEBUG("%s - Freeing _Thread_local destructor list %p", __FUNCTION__, list);
474
475 fr_dlist_talloc_free(&list->head); /* Free in order */
476 pthread_mutex_lock(&fr_atexit_global_mutex);
477 list->e->func = NULL; /* Disarm the global entry that'd free the thread-specific list */
478 pthread_mutex_unlock(&fr_atexit_global_mutex);
479 return 0;
480}
481
482/** Run all the thread local destructors
483 *
484 * @param[in] list The thread-specific exit handler list.
485 */
486static void _thread_local_pthread_free(void *list)
487{
488 talloc_free(list);
489}
490
491/** Run all the thread local destructors
492 *
493 * @param[in] list The thread-specific exit handler list.
494 */
495static int _thread_local_free(void *list)
496{
497 thread_is_exiting = true;
498 return talloc_free(list);
499}
500
501/** Add a new destructor
502 *
503 * @return
504 * - 0 on success.
505 * - -1 on memory allocation failure;
506 */
507int _fr_atexit_thread_local(char const *file, int line,
508 fr_atexit_t func, void const *uctx)
509{
511
512 /*
513 * Initialise the thread local list, just for pthread_exit().
514 */
516 fr_atexit_list_t *list;
517
518 /*
519 * Must be heap allocated, because thread local
520 * structures can be freed before the key
521 * destructor is run (depending on platform).
522 */
523 list = talloc_zero(NULL, fr_atexit_list_t);
524 if (unlikely(!list)) return -1;
525
526 ATEXIT_DEBUG("%s - Thread %u alloced _Thread_local destructor list %p",
527 __FUNCTION__,
528 (unsigned int)pthread_self(), list);
529
531 (void) pthread_key_create(&list->key, _thread_local_pthread_free);
532
533 /*
534 * We need to pass in a pointer to the heap
535 * memory because, again, the thread local
536 * indirection table may have disappeared
537 * by the time the thread destructor is
538 * called.
539 */
540 (void) pthread_setspecific(list->key, list);
541 talloc_set_destructor(list, _thread_local_list_free);
542
543 /*
544 * Add a destructor for the thread-local list
545 * The pthread based destructor will disarm
546 * this if it fires, but leave it enabled if
547 * it doesn't, thus ensuring the memory is
548 * *always* freed one way or another.
549 */
550 pthread_mutex_lock(&fr_atexit_global_mutex);
551 list->e = atexit_entry_alloc(file, line,
552 fr_atexit_threads,
553 _thread_local_free,
554 list);
555
556 pthread_mutex_unlock(&fr_atexit_global_mutex);
557
559 }
560
561 /*
562 * Now allocate the actual atexit handler entry
563 */
564 if (atexit_entry_alloc(file, line, fr_atexit_thread_local, func, uctx) == NULL) return -1;
565
566 return 0;
567}
568
569/** Remove a specific destructor for this thread (without executing them)
570 *
571 * @note This function's primary purpose is to help diagnose issues with destructors
572 * from within a debugger.
573 *
574 * @param[in] uctx_scope Only process entries where the func and scope both match.
575 * @param[in] func Entries matching this function will be disarmed.
576 * @param[in] uctx associated with the entry.
577 * @return How many destructors were disarmed.
578 */
579unsigned int fr_atexit_thread_local_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
580{
581 fr_atexit_entry_t *e = NULL;
582 unsigned int count = 0;
583
584 if (!fr_atexit_thread_local) return -1;
585
586 while ((e = fr_dlist_next(&fr_atexit_thread_local->head, e))) {
587 fr_atexit_entry_t *disarm;
588
589 if ((e->func != func) || ((e->uctx != uctx) && uctx_scope)) continue;
590
591 ATEXIT_DEBUG("%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
592 __FUNCTION__,
593 (unsigned int)pthread_self(),
594 fr_atexit_thread_local, e, e->func, e->uctx, e->file, e->line);
595 disarm = e;
597 talloc_set_destructor(disarm, NULL);
598 talloc_free(disarm);
599
600 count++;
601 }
602
603 return count;
604}
605
606/** Remove all destructors for this thread (without executing them)
607 *
608 * @note This function's primary purpose is to help diagnose issues with destructors
609 * from within a debugger.
610 */
612{
613 fr_atexit_entry_t *e = NULL;
614
615 if (!fr_atexit_thread_local) return;
616
617 while ((e = fr_dlist_pop_head(&fr_atexit_thread_local->head))) {
618 ATEXIT_DEBUG("%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
619 __FUNCTION__,
620 (unsigned int)pthread_self(),
621 fr_atexit_thread_local, e, e->func, e->uctx, e->file, e->line);
622 talloc_set_destructor(e, NULL);
623 talloc_free(e);
624 }
625}
626
627/** Cause all thread local free triggers to fire
628 *
629 * This is necessary when we're running in single threaded mode
630 * to ensure all "thread-local" memory (which isn't actually thread local)
631 * is cleaned up.
632 *
633 * One example is the OpenSSL log BIOs which must be cleaned up
634 * before fr_openssl_free is called.
635 *
636 * @return
637 * - >= 0 The number of atexit handlers triggered on success.
638 * - <0 the return code from any atexit handlers that returned an error.
639 */
641{
642 fr_atexit_entry_t *e = NULL, *ee, *to_free;
643 fr_atexit_list_t *list;
644 unsigned int count = 0;
645
646 /*
647 * Disable TLS-cached pool handouts before we start freeing
648 * any of them. Threads we don't manage (librdkafka's bg
649 * threads, etc.) keep dangling pointers in their
650 * `_Thread_local` slots once the chunks here are reaped, so
651 * any TLS-pool initialiser that consults the flag falls back
652 * to `talloc_*(NULL, ...)` from now on.
653 */
655
656 /*
657 * Iterate over the list of thread local
658 * destructor lists running the
659 * destructors.
660 */
661 while ((e = fr_dlist_next(&fr_atexit_threads->head, e))) {
662 if (!e->func) continue; /* thread already joined */
663
664 list = talloc_get_type_abort(e->uctx, fr_atexit_list_t);
665 ee = NULL;
666 while ((ee = fr_dlist_next(&list->head, ee))) {
667 ATEXIT_DEBUG("%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
668 __FUNCTION__,
669 (unsigned int)pthread_self(),
670 list, ee, ee->func, ee->uctx, ee->file, ee->line);
671
672 count++;
673 to_free = ee;
674 ee = fr_dlist_remove(&list->head, ee);
675 if (talloc_free(to_free) < 0) {
676 fr_strerror_printf_push("atexit handler failed %p/%p func=%p, uctx=%p"
677 " (alloced %s:%d)",
678 list, to_free,
679 to_free->func, to_free->uctx,
680 to_free->file, to_free->line
681 );
682 return -1;
683 }
684 }
685 }
686
687 return count;
688}
689
690/** Return whether the thread is currently being cleaned up
691 *
692 */
693bool fr_atexit_thread_is_exiting(void)
694{
695 return thread_is_exiting;
696}
697#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:442
void fr_atexit_thread_local_disable_alloc(void)
Disable lazy allocation of thread-local caches for the rest of the process.
Definition atexit.c:434
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:248
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:217
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:278
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:452
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:305
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:350
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:229
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