_atexit.c 9.5 KB

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