123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- #define _GNU_SOURCE
- #include <features.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <atomic.h>
- #ifdef __UCLIBC_HAS_THREADS__
- #include <pthread.h>
- extern pthread_mutex_t mylock;
- # define LOCK __pthread_mutex_lock(&mylock)
- # define UNLOCK __pthread_mutex_unlock(&mylock);
- #else
- # define LOCK
- # define UNLOCK
- #endif
- 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);
- extern int __exit_slots;
- extern int __exit_count;
- extern void __exit_handler(int);
- 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;
- #else
- extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
- #endif
- extern struct exit_function *__new_exitfn (void);
- extern int __cxa_atexit (cxaefuncp, void *arg, void *dso_handle);
- #if defined(L_atexit) || defined(L_old_atexit)
- extern void *__dso_handle __attribute__ ((__weak__));
- #ifdef L_atexit
- int attribute_hidden atexit(aefuncp func)
- #else
- int old_atexit(aefuncp func)
- #endif
- {
-
- return __cxa_atexit((cxaefuncp)func, NULL,
- &__dso_handle == NULL ? NULL : __dso_handle);
- }
- #ifndef L_atexit
- weak_alias(old_atexit,atexit);
- #endif
- #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
- extern 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;
- }
- #endif
- #ifdef L___cxa_finalize
- 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);
- }
- }
- #if 0
-
- #ifdef UNREGISTER_ATFORK
- if (d != NULL) {
- UNREGISTER_ATFORK (d);
- }
- #endif
- #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 *__new_exitfn(void)
- {
- struct exit_function *efp;
- LOCK;
- #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) {
- UNLOCK;
- __set_errno(ENOMEM);
- return 0;
- }
- __exit_function_table = efp;
- __exit_slots += 20;
- }
- #else
- if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
- UNLOCK;
- __set_errno(ENOMEM);
- return 0;
- }
- #endif
- __exit_cleanup = __exit_handler;
- efp = &__exit_function_table[__exit_count++];
- efp->type = ef_in_use;
- UNLOCK;
- 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__
-
- if (__exit_function_table)
- free(__exit_function_table);
- #endif
- }
- #endif
- #ifdef L_exit
- extern void weak_function _stdio_term(void);
- void (*__exit_cleanup) (int) = 0;
- #ifdef __UCLIBC_HAS_THREADS__
- pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
- #endif
- #ifdef __UCLIBC_CTOR_DTOR__
- extern void (*__app_fini)(void);
- #endif
- extern void (*__rtld_fini)(void);
- void exit(int rv)
- {
-
- LOCK;
- if (__exit_cleanup) {
- __exit_cleanup(rv);
- }
- UNLOCK;
- #ifdef __UCLIBC_CTOR_DTOR__
- if (__app_fini != NULL)
- (__app_fini)();
- #endif
- if (__rtld_fini != NULL)
- (__rtld_fini)();
-
- if (_stdio_term)
- _stdio_term();
- _exit(rv);
- }
- #endif
|