atexit.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * August 2005 Stephen Warren
  35. * Added __cxa_atexit and __cxa_finalize support
  36. *
  37. */
  38. #define _GNU_SOURCE
  39. #include <features.h>
  40. #include <unistd.h>
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include <atomic.h>
  44. #ifdef __UCLIBC_HAS_THREADS__
  45. #include <pthread.h>
  46. extern pthread_mutex_t mylock;
  47. # define LOCK __pthread_mutex_lock(&mylock)
  48. # define UNLOCK __pthread_mutex_unlock(&mylock);
  49. #else
  50. # define LOCK
  51. # define UNLOCK
  52. #endif
  53. typedef void (*aefuncp) (void); /* atexit function pointer */
  54. typedef void (*oefuncp) (int, void *); /* on_exit function pointer */
  55. typedef void (*cxaefuncp) (void *); /* __cxa_atexit function pointer */
  56. typedef enum {
  57. ef_free,
  58. ef_in_use,
  59. ef_on_exit,
  60. ef_cxa_atexit
  61. } ef_type; /* exit function types */
  62. /* this is in the L_exit object */
  63. extern void (*__exit_cleanup) (int);
  64. /* these are in the L___do_exit object */
  65. extern int __exit_slots;
  66. extern int __exit_count;
  67. extern void __exit_handler(int);
  68. struct exit_function {
  69. /*
  70. * 'type' should be of type of the 'enum ef_type' above but since we
  71. * need this element in an atomic operation we have to use 'long int'.
  72. */
  73. long int type; /* enum ef_type */
  74. union {
  75. struct {
  76. oefuncp func;
  77. void *arg;
  78. } on_exit;
  79. struct {
  80. cxaefuncp func;
  81. void *arg;
  82. void* dso_handle;
  83. } cxa_atexit;
  84. } funcs;
  85. };
  86. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  87. extern struct exit_function *__exit_function_table;
  88. #else
  89. extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
  90. #endif
  91. extern struct exit_function *__new_exitfn (void);
  92. /* this is in the L___cxa_atexit object */
  93. extern int __cxa_atexit (cxaefuncp, void *arg, void *dso_handle);
  94. /* remove old_atexit after 0.9.29 */
  95. #if defined(L_atexit) || defined(L_old_atexit)
  96. extern void *__dso_handle __attribute__ ((__weak__));
  97. /*
  98. * register a function to be called at normal program termination
  99. * (the registered function takes no arguments)
  100. */
  101. #ifdef L_atexit
  102. int attribute_hidden atexit(aefuncp func)
  103. #else
  104. int old_atexit(aefuncp func)
  105. #endif
  106. {
  107. /*
  108. * glibc casts aefuncp to cxaefuncp.
  109. * This seems dodgy, but I guess callling a function with more
  110. * parameters than it needs will work everywhere?
  111. */
  112. return __cxa_atexit((cxaefuncp)func, NULL,
  113. &__dso_handle == NULL ? NULL : __dso_handle);
  114. }
  115. #ifndef L_atexit
  116. weak_alias(old_atexit,atexit);
  117. #endif
  118. #endif
  119. #ifdef L_on_exit
  120. /*
  121. * register a function to be called at normal program termination
  122. * the registered function takes two arguments:
  123. * status - the exit status that was passed to the exit() function
  124. * arg - generic argument
  125. */
  126. int on_exit(oefuncp func, void *arg)
  127. {
  128. struct exit_function *efp;
  129. if (func == NULL) {
  130. return 0;
  131. }
  132. efp = __new_exitfn();
  133. if (efp == NULL) {
  134. return -1;
  135. }
  136. efp->funcs.on_exit.func = func;
  137. efp->funcs.on_exit.arg = arg;
  138. /* assign last for thread safety, since we're now unlocked */
  139. efp->type = ef_on_exit;
  140. return 0;
  141. }
  142. #endif
  143. #ifdef L___cxa_atexit
  144. extern int __cxa_atexit (cxaefuncp func, void *arg, void *dso_handle)
  145. {
  146. struct exit_function *efp;
  147. if (func == NULL) {
  148. return 0;
  149. }
  150. efp = __new_exitfn();
  151. if (efp == NULL) {
  152. return -1;
  153. }
  154. efp->funcs.cxa_atexit.func = func;
  155. efp->funcs.cxa_atexit.arg = arg;
  156. efp->funcs.cxa_atexit.dso_handle = dso_handle;
  157. /* assign last for thread safety, since we're now unlocked */
  158. efp->type = ef_cxa_atexit;
  159. return 0;
  160. }
  161. #endif
  162. #ifdef L___cxa_finalize
  163. /*
  164. * If D is non-NULL, call all functions registered with `__cxa_atexit'
  165. * with the same dso handle. Otherwise, if D is NULL, call all of the
  166. * registered handlers.
  167. */
  168. void __cxa_finalize (void *dso_handle)
  169. {
  170. struct exit_function *efp;
  171. int exit_count_snapshot = __exit_count;
  172. /* In reverse order */
  173. while (exit_count_snapshot) {
  174. efp = &__exit_function_table[--exit_count_snapshot];
  175. /*
  176. * We check dso_handle match before we verify the type of the union entry.
  177. * However, the atomic_exchange will validate that we were really "allowed"
  178. * to read dso_handle...
  179. */
  180. if ((dso_handle == NULL || dso_handle == efp->funcs.cxa_atexit.dso_handle)
  181. /* We don't want to run this cleanup more than once. */
  182. && !atomic_compare_and_exchange_bool_acq(&efp->type, ef_free, ef_cxa_atexit)
  183. ) {
  184. /* glibc passes status (0) too, but that's not in the prototype */
  185. (*efp->funcs.cxa_atexit.func)(efp->funcs.cxa_atexit.arg);
  186. }
  187. }
  188. #if 0 /* haven't looked into this yet... */
  189. /*
  190. * Remove the registered fork handlers. We do not have to
  191. * unregister anything if the program is going to terminate anyway.
  192. */
  193. #ifdef UNREGISTER_ATFORK
  194. if (d != NULL) {
  195. UNREGISTER_ATFORK (d);
  196. }
  197. #endif
  198. #endif
  199. }
  200. #endif
  201. #ifdef L___exit_handler
  202. int __exit_count = 0; /* Number of registered exit functions */
  203. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  204. struct exit_function *__exit_function_table = NULL;
  205. int __exit_slots = 0; /* Size of __exit_function_table */
  206. #else
  207. struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
  208. #endif
  209. /*
  210. * Find and return a new exit_function pointer, for atexit,
  211. * onexit and __cxa_atexit to initialize
  212. */
  213. struct exit_function *__new_exitfn(void)
  214. {
  215. struct exit_function *efp;
  216. LOCK;
  217. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  218. /* If we are out of function table slots, make some more */
  219. if (__exit_slots < __exit_count+1) {
  220. efp=realloc(__exit_function_table,
  221. (__exit_slots+20)*sizeof(struct exit_function));
  222. if (efp == NULL) {
  223. UNLOCK;
  224. __set_errno(ENOMEM);
  225. return 0;
  226. }
  227. __exit_function_table = efp;
  228. __exit_slots += 20;
  229. }
  230. #else
  231. if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
  232. UNLOCK;
  233. __set_errno(ENOMEM);
  234. return 0;
  235. }
  236. #endif
  237. __exit_cleanup = __exit_handler; /* enable cleanup */
  238. efp = &__exit_function_table[__exit_count++];
  239. efp->type = ef_in_use;
  240. UNLOCK;
  241. return efp;
  242. }
  243. /*
  244. * Handle the work of executing the registered exit functions
  245. * This is called while we are locked, so no additional locking
  246. * is needed...
  247. */
  248. void __exit_handler(int status)
  249. {
  250. struct exit_function *efp;
  251. /* In reverse order */
  252. while ( __exit_count ) {
  253. efp = &__exit_function_table[--__exit_count];
  254. switch (efp->type) {
  255. case ef_on_exit:
  256. if (efp->funcs.on_exit.func) {
  257. (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
  258. }
  259. break;
  260. case ef_cxa_atexit:
  261. if (efp->funcs.cxa_atexit.func) {
  262. /* glibc passes status too, but that's not in the prototype */
  263. (efp->funcs.cxa_atexit.func) (efp->funcs.cxa_atexit.arg);
  264. }
  265. break;
  266. }
  267. }
  268. #ifdef __UCLIBC_DYNAMIC_ATEXIT__
  269. /* Free up memory used by the __exit_function_table structure */
  270. if (__exit_function_table)
  271. free(__exit_function_table);
  272. #endif
  273. }
  274. #endif
  275. #ifdef L_exit
  276. extern void weak_function _stdio_term(void);
  277. void (*__exit_cleanup) (int) = 0;
  278. #ifdef __UCLIBC_HAS_THREADS__
  279. pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  280. #endif
  281. #ifdef __UCLIBC_CTOR_DTOR__
  282. extern void (*__app_fini)(void);
  283. #endif
  284. extern void (*__rtld_fini)(void);
  285. /*
  286. * Normal program termination
  287. */
  288. void exit(int rv)
  289. {
  290. /* Perform exit-specific cleanup (atexit and on_exit) */
  291. LOCK;
  292. if (__exit_cleanup) {
  293. __exit_cleanup(rv);
  294. }
  295. UNLOCK;
  296. #ifdef __UCLIBC_CTOR_DTOR__
  297. if (__app_fini != NULL)
  298. (__app_fini)();
  299. #endif
  300. if (__rtld_fini != NULL)
  301. (__rtld_fini)();
  302. /* If we are using stdio, try to shut it down. At the very least,
  303. * this will attempt to commit all buffered writes. It may also
  304. * unbuffer all writable files, or close them outright.
  305. * Check the stdio routines for details. */
  306. if (_stdio_term)
  307. _stdio_term();
  308. _exit(rv);
  309. }
  310. #endif