123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #define _GNU_SOURCE
- #include <features.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.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 enum {
- ef_atexit,
- ef_on_exit
- } ef_type;
- extern void (*__exit_cleanup) (int);
- extern int __exit_slots;
- extern int __exit_count;
- extern void __exit_handler(int);
- struct exit_function {
- ef_type type;
- union {
- aefuncp atexit;
- struct {
- oefuncp func;
- void *arg;
- } on_exit;
- } funcs;
- };
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
- extern struct exit_function *__exit_function_table;
- #else
- extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
- #endif
- #ifdef L_atexit
-
- int atexit(aefuncp func)
- {
- struct exit_function *efp;
- LOCK;
- if (func) {
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
-
- if (__exit_slots < __exit_count+1) {
- __exit_function_table=realloc(__exit_function_table,
- (__exit_slots+20)*sizeof(struct exit_function));
- if (__exit_function_table==NULL) {
- UNLOCK;
- __set_errno(ENOMEM);
- return -1;
- }
- __exit_slots+=20;
- }
- #else
- if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
- UNLOCK;
- __set_errno(ENOMEM);
- return -1;
- }
- #endif
- __exit_cleanup = __exit_handler;
- efp = &__exit_function_table[__exit_count++];
- efp->type = ef_atexit;
- efp->funcs.atexit = func;
- }
- UNLOCK;
- return 0;
- }
- #endif
- #ifdef L_on_exit
- int on_exit(oefuncp func, void *arg)
- {
- struct exit_function *efp;
- LOCK;
- if (func) {
- #ifdef __UCLIBC_DYNAMIC_ATEXIT__
-
- if (__exit_slots < __exit_count+1) {
- __exit_function_table=realloc(__exit_function_table,
- (__exit_slots+20)*sizeof(struct exit_function));
- if (__exit_function_table==NULL) {
- UNLOCK;
- __set_errno(ENOMEM);
- return -1;
- }
- __exit_slots+=20;
- }
- #else
- if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
- UNLOCK;
- __set_errno(ENOMEM);
- return -1;
- }
- #endif
- __exit_cleanup = __exit_handler;
- efp = &__exit_function_table[__exit_count++];
- efp->type = ef_on_exit;
- efp->funcs.on_exit.func = func;
- efp->funcs.on_exit.arg = arg;
- }
- UNLOCK;
- return 0;
- }
- #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
- 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_atexit:
- if (efp->funcs.atexit) {
- (efp->funcs.atexit) ();
- }
- 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
- void exit(int rv)
- {
-
- LOCK;
- if (__exit_cleanup) {
- __exit_cleanup(rv);
- }
- UNLOCK;
-
- if (_stdio_term)
- _stdio_term();
- _exit(rv);
- }
- #endif
|