1 /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
2 * This file is part of the Linux-8086 C library and is distributed
3 * under the GNU Library General Public License.
7 * Dec 2000 Manuel Novoa III
9 * Made atexit handling conform to standards... i.e. no args.
10 * Removed on_exit since it did not match gnu libc definition.
11 * Combined atexit and __do_exit into one object file.
13 * Feb 2001 Manuel Novoa III
15 * Reworked file after addition of __uClibc_main.
16 * Changed name of __do_exit to atexit_handler.
17 * Changed name of __cleanup to __uClibc_cleanup.
18 * Moved declaration of __uClibc_cleanup to __uClibc_main
19 * where it is initialized with (possibly weak alias)
22 * Jul 2001 Steve Thayer
24 * Added an on_exit implementation (that now matches gnu libc definition.)
25 * Pulled atexit_handler out of the atexit object since it is now required by
26 * on_exit as well. Renamed it to __exit_handler.
27 * Fixed a problem where exit functions stop getting called if one of
29 * As a side effect of these changes, abort() no longer calls the exit
30 * functions (it now matches the gnu libc definition).
32 * August 2002 Erik Andersen
33 * Added locking so atexit and friends can be thread safe
35 * August 2005 Stephen Warren
36 * Added __cxa_atexit and __cxa_finalize support
46 #if defined __UCLIBC_HAS_THREADS__ && defined __UCLIBC_HAS_THREADS_NATIVE__
50 #include <bits/uClibc_mutex.h>
51 __UCLIBC_MUTEX_EXTERN(__atexit_lock
) attribute_hidden
;
55 typedef void (*aefuncp
)(void); /* atexit function pointer */
56 typedef void (*oefuncp
)(int, void *); /* on_exit function pointer */
57 typedef void (*cxaefuncp
)(void *); /* __cxa_atexit function pointer */
63 } ef_type
; /* exit function types */
65 /* this is in the L_exit object */
66 extern void (*__exit_cleanup
)(int) attribute_hidden
;
68 /* these are in the L___do_exit object */
69 extern int __exit_slots attribute_hidden
;
70 extern int __exit_count attribute_hidden
;
71 extern void __exit_handler(int) attribute_hidden
;
72 struct exit_function
{
74 * 'type' should be of type of the 'enum ef_type' above but since we
75 * need this element in an atomic operation we have to use 'long int'.
77 long int type
; /* enum ef_type */
90 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
91 extern struct exit_function
*__exit_function_table attribute_hidden
;
93 extern struct exit_function __exit_function_table
[__UCLIBC_MAX_ATEXIT
] attribute_hidden
;
95 extern struct exit_function
*__new_exitfn(void) attribute_hidden
;
97 /* this is in the L___cxa_atexit object */
98 extern int __cxa_atexit(cxaefuncp
, void *arg
, void *dso_handle
);
100 #if defined(L_atexit)
101 extern void *__dso_handle
__attribute__ ((__weak__
));
104 * register a function to be called at normal program termination
105 * (the registered function takes no arguments)
108 int attribute_hidden
atexit(aefuncp func
)
112 * glibc casts aefuncp to cxaefuncp.
113 * This seems dodgy, but I guess calling a function with more
114 * parameters than it needs will work everywhere?
116 return __cxa_atexit((cxaefuncp
)func
, NULL
,
117 &__dso_handle
== NULL
? NULL
: __dso_handle
);
123 * register a function to be called at normal program termination
124 * the registered function takes two arguments:
125 * status - the exit status that was passed to the exit() function
126 * arg - generic argument
128 int on_exit(oefuncp func
, void *arg
)
130 struct exit_function
*efp
;
136 efp
= __new_exitfn();
141 efp
->funcs
.on_exit
.func
= func
;
142 efp
->funcs
.on_exit
.arg
= arg
;
143 /* assign last for thread safety, since we're now unlocked */
144 efp
->type
= ef_on_exit
;
150 #ifdef L___cxa_atexit
151 libc_hidden_proto(__cxa_atexit
)
152 int __cxa_atexit(cxaefuncp func
, void *arg
, void *dso_handle
)
154 struct exit_function
*efp
;
160 efp
= __new_exitfn();
165 efp
->funcs
.cxa_atexit
.func
= func
;
166 efp
->funcs
.cxa_atexit
.arg
= arg
;
167 efp
->funcs
.cxa_atexit
.dso_handle
= dso_handle
;
168 /* assign last for thread safety, since we're now unlocked */
169 efp
->type
= ef_cxa_atexit
;
173 libc_hidden_def(__cxa_atexit
)
176 #ifdef L___cxa_finalize
178 * If D is non-NULL, call all functions registered with `__cxa_atexit'
179 * with the same dso handle. Otherwise, if D is NULL, call all of the
180 * registered handlers.
182 void __cxa_finalize(void *dso_handle
);
183 void __cxa_finalize(void *dso_handle
)
185 struct exit_function
*efp
;
186 int exit_count_snapshot
= __exit_count
;
188 /* In reverse order */
189 while (exit_count_snapshot
) {
190 efp
= &__exit_function_table
[--exit_count_snapshot
];
193 * We check dso_handle match before we verify the type of the union entry.
194 * However, the atomic_exchange will validate that we were really "allowed"
195 * to read dso_handle...
197 if ((dso_handle
== NULL
|| dso_handle
== efp
->funcs
.cxa_atexit
.dso_handle
)
198 /* We don't want to run this cleanup more than once. */
199 && !atomic_compare_and_exchange_bool_acq(&efp
->type
, ef_free
, ef_cxa_atexit
)
201 /* glibc passes status (0) too, but that's not in the prototype */
202 (*efp
->funcs
.cxa_atexit
.func
)(efp
->funcs
.cxa_atexit
.arg
);
207 * Remove the registered fork handlers. We do not have to
208 * unregister anything if the program is going to terminate anyway.
210 #ifdef UNREGISTER_ATFORK
211 if (dso_handle
!= NULL
) {
212 UNREGISTER_ATFORK(dso_handle
);
218 #ifdef L___exit_handler
219 int __exit_count
= 0; /* Number of registered exit functions */
220 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
221 struct exit_function
*__exit_function_table
= NULL
;
222 int __exit_slots
= 0; /* Size of __exit_function_table */
224 struct exit_function __exit_function_table
[__UCLIBC_MAX_ATEXIT
];
228 * Find and return a new exit_function pointer, for atexit,
229 * onexit and __cxa_atexit to initialize
231 struct exit_function attribute_hidden
*__new_exitfn(void)
233 struct exit_function
*efp
;
235 __UCLIBC_MUTEX_LOCK(__atexit_lock
);
238 * Reuse free slots at the end of the list.
239 * This avoids eating memory when dlopen and dlclose modules multiple times.
241 while (__exit_count
> 0) {
242 if (__exit_function_table
[__exit_count
-1].type
== ef_free
) {
247 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
248 /* If we are out of function table slots, make some more */
249 if (__exit_slots
< __exit_count
+1) {
250 efp
= realloc(__exit_function_table
,
251 (__exit_slots
+20)*sizeof(struct exit_function
));
253 /* __set_errno(ENOMEM); */
256 __exit_function_table
= efp
;
260 if (__exit_count
>= __UCLIBC_MAX_ATEXIT
) {
267 __exit_cleanup
= __exit_handler
; /* enable cleanup */
268 efp
= &__exit_function_table
[__exit_count
++];
269 efp
->type
= ef_in_use
;
272 __UCLIBC_MUTEX_UNLOCK(__atexit_lock
);
277 * Handle the work of executing the registered exit functions
278 * This is called while we are locked, so no additional locking
281 void __exit_handler(int status
)
283 struct exit_function
*efp
;
285 /* In reverse order */
286 while (__exit_count
) {
287 efp
= &__exit_function_table
[--__exit_count
];
290 if (efp
->funcs
.on_exit
.func
) {
291 (efp
->funcs
.on_exit
.func
)(status
, efp
->funcs
.on_exit
.arg
);
295 if (efp
->funcs
.cxa_atexit
.func
) {
296 /* glibc passes status too, but that's not in the prototype */
297 (efp
->funcs
.cxa_atexit
.func
)(efp
->funcs
.cxa_atexit
.arg
);
302 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
303 /* Free up memory used by the __exit_function_table structure */
304 free(__exit_function_table
);
310 /* Defeat compiler optimization which assumes function addresses are never NULL */
311 static __always_inline
int not_null_ptr(const void *p
)
315 : "=r" (q
) /* output */
316 : "0" (p
) /* input */
321 extern void weak_function
_stdio_term(void) attribute_hidden
;
322 attribute_hidden
void (*__exit_cleanup
)(int) = 0;
323 __UCLIBC_MUTEX_INIT(__atexit_lock
, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
);
325 extern void __uClibc_fini(void) attribute_hidden
;
328 * Normal program termination
332 /* Perform exit-specific cleanup (atexit and on_exit) */
333 __UCLIBC_MUTEX_LOCK(__atexit_lock
);
334 if (not_null_ptr(__exit_cleanup
)) {
337 __UCLIBC_MUTEX_UNLOCK(__atexit_lock
);
341 /* If we are using stdio, try to shut it down. At the very least,
342 * this will attempt to commit all buffered writes. It may also
343 * unbuffer all writable files, or close them outright.
344 * Check the stdio routines for details. */
345 if (not_null_ptr(_stdio_term
))
350 libc_hidden_def(exit
)