atexit.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.
  4. */
  5. /*
  6. * Dec 2000 Manuel Novoa III
  7. *
  8. * Made atexit handling conform to standards... i.e. no args.
  9. * Removed on_exit since it did not match gnu libc definition.
  10. * Combined atexit and __do_exit into one object file.
  11. *
  12. * Feb 2001 Manuel Novoa III
  13. *
  14. * Reworked file after addition of __uClibc_main.
  15. * Changed name of __do_exit to atexit_handler.
  16. * Changed name of __cleanup to __uClibc_cleanup.
  17. * Moved declaration of __uClibc_cleanup to __uClibc_main
  18. * where it is initialized with (possibly weak alias)
  19. * _stdio_term.
  20. *
  21. * Jul 2001 Steve Thayer
  22. *
  23. * Added an on_exit implementation (that now matches gnu libc definition.)
  24. * Pulled atexit_handler out of the atexit object since it is now required by
  25. * on_exit as well. Renamed it to __exit_handler.
  26. * Fixed a problem where exit functions stop getting called if one of
  27. * them calls exit().
  28. * As a side effect of these changes, abort() no longer calls the exit
  29. * functions (it now matches the gnu libc definition).
  30. *
  31. * August 2002 Erik Andersen
  32. * Added locking so atexit and friends can be thread safe
  33. *
  34. */
  35. #define _GNU_SOURCE
  36. #include <features.h>
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #ifdef __UCLIBC_HAS_THREADS__
  41. #include <pthread.h>
  42. extern pthread_mutex_t mylock;
  43. # define LOCK __pthread_mutex_lock(&mylock)
  44. # define UNLOCK __pthread_mutex_unlock(&mylock);
  45. #else
  46. # define LOCK
  47. # define UNLOCK
  48. #endif
  49. typedef void (*aefuncp) (void); /* atexit function pointer */
  50. typedef void (*oefuncp) (int, void *); /* on_exit function pointer */
  51. typedef enum {
  52. ef_atexit,
  53. ef_on_exit
  54. } ef_type; /* exit function types */
  55. /* this is in the L_exit object */
  56. extern void (*__exit_cleanup) (int);
  57. /* these are in the L___do_exit object */
  58. extern int __exit_slots;
  59. extern int __exit_count;
  60. extern void __exit_handler(int);
  61. struct exit_function {
  62. ef_type type; /* ef_atexit or ef_on_exit */
  63. union {
  64. aefuncp atexit;
  65. struct {
  66. oefuncp func;
  67. void *arg;
  68. } on_exit;
  69. } funcs;
  70. };
  71. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  72. extern struct exit_function *__exit_function_table;
  73. #else
  74. extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
  75. #endif
  76. #ifdef L_atexit
  77. /*
  78. * register a function to be called at normal program termination
  79. * (the registered function takes no arguments)
  80. */
  81. int atexit(aefuncp func)
  82. {
  83. struct exit_function *efp;
  84. LOCK;
  85. if (func) {
  86. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  87. /* If we are out of function table slots, make some more */
  88. if (__exit_slots < __exit_count+1) {
  89. efp=realloc(__exit_function_table,
  90. (__exit_slots+20)*sizeof(struct exit_function));
  91. if (efp==NULL) {
  92. UNLOCK;
  93. __set_errno(ENOMEM);
  94. return -1;
  95. }
  96. __exit_function_table = efp;
  97. __exit_slots+=20;
  98. }
  99. #else
  100. if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
  101. UNLOCK;
  102. __set_errno(ENOMEM);
  103. return -1;
  104. }
  105. #endif
  106. __exit_cleanup = __exit_handler; /* enable cleanup */
  107. efp = &__exit_function_table[__exit_count++];
  108. efp->type = ef_atexit;
  109. efp->funcs.atexit = func;
  110. }
  111. UNLOCK;
  112. return 0;
  113. }
  114. #endif
  115. #ifdef L_on_exit
  116. /*
  117. * register a function to be called at normal program termination
  118. * the registered function takes two arguments:
  119. * status - the exit status that was passed to the exit() function
  120. * arg - generic argument
  121. */
  122. int on_exit(oefuncp func, void *arg)
  123. {
  124. struct exit_function *efp;
  125. LOCK;
  126. if (func) {
  127. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  128. /* If we are out of function table slots, make some more */
  129. if (__exit_slots < __exit_count+1) {
  130. efp=realloc(__exit_function_table,
  131. (__exit_slots+20)*sizeof(struct exit_function));
  132. if (efp==NULL) {
  133. UNLOCK;
  134. __set_errno(ENOMEM);
  135. return -1;
  136. }
  137. __exit_function_table=efp;
  138. __exit_slots+=20;
  139. }
  140. #else
  141. if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
  142. UNLOCK;
  143. __set_errno(ENOMEM);
  144. return -1;
  145. }
  146. #endif
  147. __exit_cleanup = __exit_handler; /* enable cleanup */
  148. efp = &__exit_function_table[__exit_count++];
  149. efp->type = ef_on_exit;
  150. efp->funcs.on_exit.func = func;
  151. efp->funcs.on_exit.arg = arg;
  152. }
  153. UNLOCK;
  154. return 0;
  155. }
  156. #endif
  157. #ifdef L___exit_handler
  158. int __exit_count = 0; /* Number of registered exit functions */
  159. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  160. struct exit_function *__exit_function_table = NULL;
  161. int __exit_slots = 0; /* Size of __exit_function_table */
  162. #else
  163. struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
  164. #endif
  165. /*
  166. * Handle the work of executing the registered exit functions
  167. * This is called while we are locked, so no additional locking
  168. * is needed...
  169. */
  170. void __exit_handler(int status)
  171. {
  172. struct exit_function *efp;
  173. /* In reverse order */
  174. while ( __exit_count ) {
  175. efp = &__exit_function_table[--__exit_count];
  176. switch (efp->type) {
  177. case ef_on_exit:
  178. if (efp->funcs.on_exit.func) {
  179. (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
  180. }
  181. break;
  182. case ef_atexit:
  183. if (efp->funcs.atexit) {
  184. (efp->funcs.atexit) ();
  185. }
  186. break;
  187. }
  188. }
  189. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  190. /* Free up memory used by the __exit_function_table structure */
  191. if (__exit_function_table)
  192. free(__exit_function_table);
  193. #endif
  194. }
  195. #endif
  196. #ifdef L_exit
  197. extern void weak_function _stdio_term(void);
  198. void (*__exit_cleanup) (int) = 0;
  199. #ifdef __UCLIBC_HAS_THREADS__
  200. pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  201. #endif
  202. #ifdef __UCLIBC_CTOR_DTOR__
  203. extern void (*__app_fini)(void);
  204. #endif
  205. extern void (*__rtld_fini)(void);
  206. /*
  207. * Normal program termination
  208. */
  209. void exit(int rv)
  210. {
  211. /* Perform exit-specific cleanup (atexit and on_exit) */
  212. LOCK;
  213. if (__exit_cleanup) {
  214. __exit_cleanup(rv);
  215. }
  216. UNLOCK;
  217. #ifdef __UCLIBC_CTOR_DTOR__
  218. if (__app_fini != NULL)
  219. (__app_fini)();
  220. #endif
  221. #ifndef _DL_FINI_CRT_COMPAT
  222. if (__rtld_fini != NULL)
  223. (__rtld_fini)();
  224. #endif
  225. /* If we are using stdio, try to shut it down. At the very least,
  226. * this will attempt to commit all buffered writes. It may also
  227. * unbuffer all writable files, or close them outright.
  228. * Check the stdio routines for details. */
  229. if (_stdio_term)
  230. _stdio_term();
  231. _exit(rv);
  232. }
  233. #endif