_atexit.c 8.6 KB

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