123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- #include <features.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <atomic.h>
- #if defined __UCLIBC_HAS_THREADS__ && defined __UCLIBC_HAS_THREADS_NATIVE__
- # include <fork.h>
- #endif
- #include <bits/uClibc_mutex.h>
- __UCLIBC_MUTEX_EXTERN(__atexit_lock) attribute_hidden;
- typedef void (*aefuncp)(void);
- typedef void (*oefuncp)(int, void *);
- typedef void (*cxaefuncp)(void *);
- typedef enum {
- ef_free,
- ef_in_use,
- ef_on_exit,
- ef_cxa_atexit
- } ef_type;
- extern void (*__exit_cleanup)(int) attribute_hidden;
- extern int __exit_slots attribute_hidden;
- extern int __exit_count attribute_hidden;
- extern void __exit_handler(int) attribute_hidden;
- struct exit_function {
-
- long int type;
- union {
- struct {
- oefuncp func;
- void *arg;
- } on_exit;
- struct {
- cxaefuncp func;
- void *arg;
- void* dso_handle;
- } cxa_atexit;
- } funcs;
- };
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
- extern struct exit_function *__exit_function_table attribute_hidden;
- #else
- extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT] attribute_hidden;
- #endif
- extern struct exit_function *__new_exitfn(void) attribute_hidden;
- extern int __cxa_atexit(cxaefuncp, void *arg, void *dso_handle);
- #if defined(L_atexit)
- extern void *__dso_handle __attribute__ ((__weak__));
- #ifdef L_atexit
- int attribute_hidden atexit(aefuncp func)
- #endif
- {
-
- return __cxa_atexit((cxaefuncp)func, NULL,
- &__dso_handle == NULL ? NULL : __dso_handle);
- }
- #endif
- #ifdef L_on_exit
- int on_exit(oefuncp func, void *arg)
- {
- struct exit_function *efp;
- if (func == NULL) {
- return 0;
- }
- efp = __new_exitfn();
- if (efp == NULL) {
- return -1;
- }
- efp->funcs.on_exit.func = func;
- efp->funcs.on_exit.arg = arg;
-
- efp->type = ef_on_exit;
- return 0;
- }
- #endif
- #ifdef L___cxa_atexit
- libc_hidden_proto(__cxa_atexit)
- int __cxa_atexit(cxaefuncp func, void *arg, void *dso_handle)
- {
- struct exit_function *efp;
- if (func == NULL) {
- return 0;
- }
- efp = __new_exitfn();
- if (efp == NULL) {
- return -1;
- }
- efp->funcs.cxa_atexit.func = func;
- efp->funcs.cxa_atexit.arg = arg;
- efp->funcs.cxa_atexit.dso_handle = dso_handle;
-
- efp->type = ef_cxa_atexit;
- return 0;
- }
- libc_hidden_def(__cxa_atexit)
- #endif
- #ifdef L___cxa_finalize
- void __cxa_finalize(void *dso_handle);
- void __cxa_finalize(void *dso_handle)
- {
- struct exit_function *efp;
- int exit_count_snapshot = __exit_count;
-
- while (exit_count_snapshot) {
- efp = &__exit_function_table[--exit_count_snapshot];
-
- if ((dso_handle == NULL || dso_handle == efp->funcs.cxa_atexit.dso_handle)
-
- && !atomic_compare_and_exchange_bool_acq(&efp->type, ef_free, ef_cxa_atexit)
- ) {
-
- (*efp->funcs.cxa_atexit.func)(efp->funcs.cxa_atexit.arg);
- }
- }
-
- #ifdef UNREGISTER_ATFORK
- if (dso_handle != NULL) {
- UNREGISTER_ATFORK(dso_handle);
- }
- #endif
- }
- #endif
- #ifdef L___exit_handler
- int __exit_count = 0;
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
- struct exit_function *__exit_function_table = NULL;
- int __exit_slots = 0;
- #else
- struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
- #endif
- struct exit_function attribute_hidden *__new_exitfn(void)
- {
- struct exit_function *efp;
- __UCLIBC_MUTEX_LOCK(__atexit_lock);
-
- while (__exit_count > 0) {
- if (__exit_function_table[__exit_count-1].type == ef_free) {
- --__exit_count;
- } else break;
- }
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
-
- if (__exit_slots < __exit_count+1) {
- efp = realloc(__exit_function_table,
- (__exit_slots+20)*sizeof(struct exit_function));
- if (efp == NULL) {
-
- goto DONE;
- }
- __exit_function_table = efp;
- __exit_slots += 20;
- }
- #else
- if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
- __set_errno(ENOMEM);
- efp = NULL;
- goto DONE;
- }
- #endif
- __exit_cleanup = __exit_handler;
- efp = &__exit_function_table[__exit_count++];
- efp->type = ef_in_use;
- DONE:
- __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
- return efp;
- }
- void __exit_handler(int status)
- {
- struct exit_function *efp;
-
- while (__exit_count) {
- efp = &__exit_function_table[--__exit_count];
- switch (efp->type) {
- case ef_on_exit:
- if (efp->funcs.on_exit.func) {
- (efp->funcs.on_exit.func)(status, efp->funcs.on_exit.arg);
- }
- break;
- case ef_cxa_atexit:
- if (efp->funcs.cxa_atexit.func) {
-
- (efp->funcs.cxa_atexit.func)(efp->funcs.cxa_atexit.arg);
- }
- break;
- }
- }
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
-
- free(__exit_function_table);
- #endif
- }
- #endif
- #ifdef L_exit
- static __always_inline int not_null_ptr(const void *p)
- {
- const void *q;
- __asm__ (""
- : "=r" (q)
- : "0" (p)
- );
- return q != 0;
- }
- extern void weak_function _stdio_term(void) attribute_hidden;
- attribute_hidden void (*__exit_cleanup)(int) = 0;
- __UCLIBC_MUTEX_INIT(__atexit_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
- extern void __uClibc_fini(void) attribute_hidden;
- void exit(int rv)
- {
-
- __UCLIBC_MUTEX_LOCK(__atexit_lock);
- if (not_null_ptr(__exit_cleanup)) {
- __exit_cleanup(rv);
- }
- __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
- __uClibc_fini();
-
- if (not_null_ptr(_stdio_term))
- _stdio_term();
- _exit(rv);
- }
- libc_hidden_def(exit)
- #endif
|