_atexit.c 10.0 KB

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