2004-09-10 03:46:54 +08:00
|
|
|
/*
|
|
|
|
* COmmon routine to call call registered atexit-like routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <reent.h>
|
2010-02-25 03:58:17 +08:00
|
|
|
#include <sys/lock.h>
|
2004-09-10 03:46:54 +08:00
|
|
|
#include "atexit.h"
|
|
|
|
|
2009-04-24 01:54:22 +08:00
|
|
|
/* Make this a weak reference to avoid pulling in free. */
|
2018-04-14 03:42:37 +08:00
|
|
|
#ifndef MALLOC_PROVIDED
|
2009-04-24 01:54:22 +08:00
|
|
|
void free(void *) _ATTRIBUTE((__weak__));
|
2018-04-14 03:42:37 +08:00
|
|
|
#endif
|
2009-04-24 01:54:22 +08:00
|
|
|
|
2017-01-30 19:23:00 +08:00
|
|
|
#ifndef __SINGLE_THREAD__
|
2017-01-29 17:27:17 +08:00
|
|
|
__LOCK_INIT_RECURSIVE(, __atexit_recursive_mutex);
|
2017-01-30 19:23:00 +08:00
|
|
|
#endif
|
2010-02-05 01:57:30 +08:00
|
|
|
|
2013-05-09 07:13:51 +08:00
|
|
|
#ifdef _REENT_GLOBAL_ATEXIT
|
|
|
|
struct _atexit *_global_atexit = _NULL;
|
|
|
|
#endif
|
|
|
|
|
2010-06-09 02:44:14 +08:00
|
|
|
#ifdef _WANT_REGISTER_FINI
|
|
|
|
|
2010-06-04 23:30:40 +08:00
|
|
|
/* If "__libc_fini" is defined, finalizers (either
|
|
|
|
"__libc_fini_array", or "_fini", as appropriate) will be run after
|
|
|
|
all user-specified atexit handlers. For example, you can define
|
|
|
|
"__libc_fini" to "_fini" in your linker script if you want the C
|
|
|
|
library, rather than startup code, to register finalizers. If you
|
|
|
|
do that, then your startup code need not contain references to
|
|
|
|
"atexit" or "exit". As a result, only applications that reference
|
|
|
|
"exit" explicitly will pull in finalization code.
|
|
|
|
|
|
|
|
The choice of whether to register finalizers from libc or from
|
|
|
|
startup code is deferred to link-time, rather than being a
|
|
|
|
configure-time option, so that the same C library binary can be
|
|
|
|
used with multiple BSPs, some of which register finalizers from
|
|
|
|
startup code, while others defer to the C library. */
|
|
|
|
extern char __libc_fini __attribute__((weak));
|
|
|
|
|
|
|
|
/* Register the application finalization function with atexit. These
|
|
|
|
finalizers should run last. Therefore, we want to call atexit as
|
|
|
|
soon as possible. */
|
|
|
|
static void
|
|
|
|
register_fini(void) __attribute__((constructor (0)));
|
|
|
|
|
|
|
|
static void
|
|
|
|
register_fini(void)
|
|
|
|
{
|
|
|
|
if (&__libc_fini) {
|
2022-01-18 11:20:20 +08:00
|
|
|
#ifdef _HAVE_INITFINI_ARRAY
|
2010-06-04 23:30:40 +08:00
|
|
|
extern void __libc_fini_array (void);
|
|
|
|
atexit (__libc_fini_array);
|
|
|
|
#else
|
|
|
|
extern void _fini (void);
|
|
|
|
atexit (_fini);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 02:44:14 +08:00
|
|
|
#endif /* _WANT_REGISTER_FINI */
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
/*
|
|
|
|
* Call registered exit handlers. If D is null then all handlers are called,
|
|
|
|
* otherwise only the handlers from that DSO are called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2017-12-04 11:43:30 +08:00
|
|
|
__call_exitprocs (int code, void *d)
|
2004-09-10 03:46:54 +08:00
|
|
|
{
|
|
|
|
register struct _atexit *p;
|
|
|
|
struct _atexit **lastp;
|
|
|
|
register struct _on_exit_args * args;
|
|
|
|
register int n;
|
|
|
|
int i;
|
|
|
|
void (*fn) (void);
|
|
|
|
|
2010-02-05 01:57:30 +08:00
|
|
|
|
|
|
|
#ifndef __SINGLE_THREAD__
|
2017-01-29 17:27:17 +08:00
|
|
|
__lock_acquire_recursive(__atexit_recursive_mutex);
|
2010-02-05 01:57:30 +08:00
|
|
|
#endif
|
|
|
|
|
2007-04-06 00:47:38 +08:00
|
|
|
restart:
|
|
|
|
|
2013-05-08 05:40:10 +08:00
|
|
|
p = _GLOBAL_ATEXIT;
|
|
|
|
lastp = &_GLOBAL_ATEXIT;
|
2004-09-10 03:46:54 +08:00
|
|
|
while (p)
|
|
|
|
{
|
|
|
|
#ifdef _REENT_SMALL
|
|
|
|
args = p->_on_exit_args_ptr;
|
|
|
|
#else
|
|
|
|
args = &p->_on_exit_args;
|
|
|
|
#endif
|
|
|
|
for (n = p->_ind - 1; n >= 0; n--)
|
|
|
|
{
|
2007-04-06 00:47:38 +08:00
|
|
|
int ind;
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
i = 1 << n;
|
|
|
|
|
|
|
|
/* Skip functions not from this dso. */
|
|
|
|
if (d && (!args || args->_dso_handle[n] != d))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Remove the function now to protect against the
|
|
|
|
function calling exit recursively. */
|
|
|
|
fn = p->_fns[n];
|
|
|
|
if (n == p->_ind - 1)
|
|
|
|
p->_ind--;
|
|
|
|
else
|
|
|
|
p->_fns[n] = NULL;
|
|
|
|
|
|
|
|
/* Skip functions that have already been called. */
|
|
|
|
if (!fn)
|
|
|
|
continue;
|
|
|
|
|
2007-04-06 00:47:38 +08:00
|
|
|
ind = p->_ind;
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
/* Call the function. */
|
|
|
|
if (!args || (args->_fntypes & i) == 0)
|
|
|
|
fn ();
|
|
|
|
else if ((args->_is_cxa & i) == 0)
|
2017-12-04 10:53:22 +08:00
|
|
|
(*((void (*)(int, void *)) fn))(code, args->_fnargs[n]);
|
2004-09-10 03:46:54 +08:00
|
|
|
else
|
2017-12-04 10:53:22 +08:00
|
|
|
(*((void (*)(void *)) fn))(args->_fnargs[n]);
|
2007-04-06 00:47:38 +08:00
|
|
|
|
|
|
|
/* The function we called call atexit and registered another
|
|
|
|
function (or functions). Call these new functions before
|
|
|
|
continuing with the already registered functions. */
|
|
|
|
if (ind != p->_ind || *lastp != p)
|
|
|
|
goto restart;
|
2004-09-10 03:46:54 +08:00
|
|
|
}
|
|
|
|
|
2018-08-08 16:42:48 +08:00
|
|
|
#if !defined (_ATEXIT_DYNAMIC_ALLOC) || !defined (MALLOC_PROVIDED)
|
2006-03-21 08:57:34 +08:00
|
|
|
break;
|
|
|
|
#else
|
2004-09-10 03:46:54 +08:00
|
|
|
/* Move to the next block. Free empty blocks except the last one,
|
|
|
|
which is part of _GLOBAL_REENT. */
|
|
|
|
if (p->_ind == 0 && p->_next)
|
|
|
|
{
|
|
|
|
/* Remove empty block from the list. */
|
|
|
|
*lastp = p->_next;
|
|
|
|
#ifdef _REENT_SMALL
|
|
|
|
if (args)
|
|
|
|
free (args);
|
|
|
|
#endif
|
|
|
|
free (p);
|
|
|
|
p = *lastp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lastp = &p->_next;
|
|
|
|
p = p->_next;
|
|
|
|
}
|
2006-03-21 08:57:34 +08:00
|
|
|
#endif
|
2004-09-10 03:46:54 +08:00
|
|
|
}
|
2010-02-05 01:57:30 +08:00
|
|
|
#ifndef __SINGLE_THREAD__
|
2017-01-29 17:27:17 +08:00
|
|
|
__lock_release_recursive(__atexit_recursive_mutex);
|
2010-02-05 01:57:30 +08:00
|
|
|
#endif
|
|
|
|
|
2004-09-10 03:46:54 +08:00
|
|
|
}
|