The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
atexit.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /** Functions to help with cleanup
19  *
20  * Simplifies cleaning up thread local and global resources
21  *
22  * @file lib/util/atexit.h
23  *
24  * @copyright 2020-2022 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25  * @copyright 2013-2016 The FreeRADIUS server project
26  */
27 RCSIDH(atexit_h, "$Id: 0b1721146657499c29e423cbfcebad73f41bdec4 $")
28 
29 #include <stdbool.h>
30 #include <stdatomic.h>
31 #include <pthread.h>
32 #include <freeradius-devel/util/talloc.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /** Destructor callback
39  *
40  * @param[in] uctx to free.
41  * @return
42  * - 0 on success.
43  * - -1 on failure.
44  */
45 typedef int(*fr_atexit_t)(void *uctx);
46 
47 int fr_atexit_global_setup(void);
48 
49 int _atexit_global(char const *file, int line, fr_atexit_t func, void const *uctx);
50 
51 /** Add a free function to the global free list
52  *
53  * @param[in] _func to call.
54  * @param[in] _uctx to pass to func.
55  * @return
56  * - 0 on success.
57  * - -1 on failure.
58  */
59 #define fr_atexit_global(_func, _uctx) _atexit_global(__FILE__, __LINE__, _func, _uctx)
60 
61 unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx);
62 
64 
66 
67 int fr_atexit_trigger(bool uctx_scope, fr_atexit_t func, void const *uctx);
68 
69 bool fr_atexit_is_exiting(void);
70 
71 #ifdef HAVE_PTHREADS
72 /*
73  * Because GCC only added support in 2013 *sigh*
74  */
75 #ifdef TLS_STORAGE_CLASS
76 # define _Thread_local TLS_STORAGE_CLASS
77 #endif
78 
79 /*
80  * Stop complaints of...:
81  *
82  * error: the address of 'x' will always evaluate as 'true' [-Werror=address]
83  *
84  * ...when functions are tested directly in the fr_atexit_global_once macro
85  */
86 static inline int _fr_atexit_global_once_funcs(fr_atexit_t init_func, fr_atexit_t free_func, void *uctx)
87 {
88  if (init_func) if (init_func(uctx) < 0) return -1;
89  if (free_func) fr_atexit_global(free_func, uctx);
90 
91  return 0;
92 }
93 
94 /** A generic function to free talloc chunks. Compatible with the fr_atexit_t type
95  *
96  * @param[in] to_free talloc chunk to free.
97  * @return the return code returned by talloc_free.
98  */
99 static inline int fr_atexit_talloc_free(void *to_free)
100 {
101  return talloc_free(to_free);
102 }
103 
104 /** Setup pair of global init/free functions, returning errors from the specified init function
105  *
106  * Simplifies setting up data structures the first time a given function
107  * is called.
108  *
109  * Should be used in the body of the function before any initialisation
110  * dependent code.
111  *
112  * Will not share init status outside of the function.
113  *
114  * @param[out] _ret A pointer to where to write the result
115  * of the init function if called.
116  * @param[in] _init function to call. Will be called once
117  * during the process lifetime.
118  * May be NULL.
119  * @param[in] _free function to call. Will be called once
120  * at exit.
121  * May be NULL.
122  * Pass fr_atexit_talloc_free if _uctx is
123  * just a talloc chunk and no special logic
124  * is needed.
125  * @param[in] _uctx data to be passed to free function.
126  */
127 #define fr_atexit_global_once_ret(_ret, _init, _free, _uctx) \
128 do { \
129  static atomic_bool _init_done = false; \
130  static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER; \
131  void *_our_uctx = _uctx; /* stop _uctx being evaluated multiple times, it may be a call to malloc() */ \
132  *(_ret) = 0; \
133  if (unlikely(!atomic_load(&_init_done))) { \
134  pthread_mutex_lock(&_init_mutex); \
135  if (!atomic_load(&_init_done)) { \
136  if (_fr_atexit_global_once_funcs(_init, _free, _our_uctx) < 0) { \
137  *(_ret) = -1; \
138  } \
139  atomic_store(&_init_done, true); \
140  } \
141  pthread_mutex_unlock(&_init_mutex); \
142  } \
143 } while (0)
144 
145 /** Setup pair of global init/free functions
146  *
147  * Simplifies setting up data structures the first time a given function
148  * is called.
149  *
150  * Should be used in the body of the function before any initialisation
151  * dependent code.
152  *
153  * Will not share init status outside of the function.
154  *
155  * @param[in] _init function to call. Will be called once
156  * during the process lifetime.
157  * May be NULL.
158  * @param[in] _free function to call. Will be called once
159  * at exit.
160  * May be NULL.
161  * Pass fr_atexit_talloc_free if _uctx is
162  * just a talloc chunk and no special logic
163  * is needed.
164  * @param[in] _uctx data to be passed to free function.
165  */
166 #define fr_atexit_global_once(_init, _free, _uctx) \
167  fr_atexit_global_once_ret(&(int){ 0 }, _init, _free, _uctx)
168 
169 /** Set a destructor for thread local storage to free the memory on thread exit
170  *
171  * @note Pointers to thread local storage seem to become unusable as threads are
172  * destroyed. So we need to store the address of the memory to free, not
173  * the address of the thread local variable.
174  *
175  * @param[in] _name Name of variable e.g. 'my_tls'.
176  * @param[in] _free Destructor, called when the thread exits to clean up any data.
177  * Pass fr_atexit_talloc_free if _uctx is
178  * just a talloc chunk and no special logic
179  * is needed.
180  * @param[in] _uctx Memory to free.
181  */
182 # define fr_atexit_thread_local(_name, _free, _uctx) \
183 do { \
184  void *_our_uctx = _uctx; /* stop _uctx being evaluated multiple times, it may be a call to malloc() */ \
185  _fr_atexit_thread_local(__FILE__, __LINE__, _free, _our_uctx); \
186  _name = _our_uctx; \
187 } while (0)
188 
189 int _fr_atexit_thread_local(char const *file, int line,
190  fr_atexit_t func, void const *uctx);
191 
192 unsigned int fr_atexit_thread_local_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx);
193 
195 
197 
198 bool fr_atexit_thread_is_exiting(void);
199 
200 /*
201  * If we're building without threading support,
202  * all this becomes much easier, and we just map
203  * all thread local cleanup entries to the global
204  * list.
205  */
206 #else
207 /*
208  * Don't emit a _Thread_local_storage qualifier
209  */
210 # define __Thread_local
211 # define fr_atexit_global_once(_init, _free, _uctx) \
212 do { \
213  static bool _init_done = false; \
214  void * _our_uctx = _uctx; /* stop _uctx being evaluated multiple times, it may be a call to malloc() */ \
215  if (unlikely(!_init_done)) { \
216  _init(_our_uctx); \
217  fr_atexit_global(_free, _our_uctx); \
218  _init_done = true; \
219  } \
220 } while(0);
221 # define fr_atexit_thread_local(_name, _free, _uctx) \
222 do { \
223  static bool _init_done = false; \
224  void * _our_uctx = _uctx; /* stop _uctx being evaluated multiple times, it may be a call to malloc() */ \
225  if (unlikely(!_init_done)) { \
226  fr_atexit_global(_free, _our_uctx); \
227  _init_done = true; \
228  } \
229  _name = _our_uctx; \
230 } while(0);
231 # define fr_atexit_thread_local_disarm(...) fr_atexit_global_disarm(__VA_ARGS__)
232 # define fr_atexit_thread_local_disarm_all(...) fr_atexit_global_disarm_all(__VA_ARGS__)
233 # define fr_atexit_thread_trigger_all(...)
234 #endif
235 
236 #ifdef __cplusplus
237 }
238 #endif
int const char * file
Definition: acutest.h:702
int const char int line
Definition: acutest.h:702
int(* fr_atexit_t)(void *uctx)
Destructor callback.
Definition: atexit.h:45
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:229
#define fr_atexit_thread_trigger_all(...)
Definition: atexit.h:233
#define fr_atexit_thread_local_disarm(...)
Definition: atexit.h:231
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition: atexit.c:160
void fr_atexit_global_disarm_all(void)
Remove all global destructors (without executing them)
Definition: atexit.c:259
#define fr_atexit_thread_local_disarm_all(...)
Definition: atexit.h:232
#define fr_atexit_global(_func, _uctx)
Add a free function to the global free list.
Definition: atexit.h:59
bool fr_atexit_is_exiting(void)
Return whether we're currently in the teardown phase.
Definition: atexit.c:412
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition: atexit.c:286
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:331
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:210
#define RCSIDH(h, id)
Definition: build.h:445
talloc_free(reap)