atexit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_close_all.
  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. */
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <errno.h>
  35. #define __MAX_EXIT __UCLIBC_MAX_ATEXIT
  36. typedef void (*aefuncp) (void); /* atexit function pointer */
  37. typedef void (*oefuncp) (int, void *); /* on_exit function pointer */
  38. typedef enum {
  39. ef_atexit,
  40. ef_on_exit
  41. } ef_type; /* exit function types */
  42. extern void __stdio_close_all(void);
  43. /* this is in the L_exit object */
  44. extern void (*__exit_cleanup) (int);
  45. /* these are in the L___do_exit object */
  46. extern int __exit_count;
  47. extern void __exit_handler(int);
  48. extern struct exit_function {
  49. ef_type type; /* ef_atexit or ef_on_exit */
  50. union {
  51. aefuncp atexit;
  52. struct {
  53. oefuncp func;
  54. void *arg;
  55. } on_exit;
  56. } funcs;
  57. } __exit_function_table[__MAX_EXIT];
  58. #ifdef L_atexit
  59. /*
  60. * register a function to be called at normal program termination
  61. * (the registered function takes no arguments)
  62. */
  63. int atexit(aefuncp func)
  64. {
  65. struct exit_function *efp;
  66. if (__exit_count >= __MAX_EXIT) {
  67. __set_errno(ENOMEM);
  68. return -1;
  69. }
  70. if (func) {
  71. __exit_cleanup = __exit_handler; /* enable cleanup */
  72. efp = &__exit_function_table[__exit_count++];
  73. efp->type = ef_atexit;
  74. efp->funcs.atexit = func;
  75. }
  76. return 0;
  77. }
  78. #endif
  79. #ifdef L_on_exit
  80. /*
  81. * register a function to be called at normal program termination
  82. * the registered function takes two arguments:
  83. * status - the exit status that was passed to the exit() function
  84. * arg - generic argument
  85. */
  86. int on_exit(oefuncp func, void *arg)
  87. {
  88. struct exit_function *efp;
  89. if (__exit_count >= __MAX_EXIT) {
  90. __set_errno(ENOMEM);
  91. return -1;
  92. }
  93. if (func) {
  94. __exit_cleanup = __exit_handler; /* enable cleanup */
  95. efp = &__exit_function_table[__exit_count++];
  96. efp->type = ef_on_exit;
  97. efp->funcs.on_exit.func = func;
  98. efp->funcs.on_exit.arg = arg;
  99. }
  100. return 0;
  101. }
  102. #endif
  103. #ifdef L___exit_handler
  104. struct exit_function __exit_function_table[__MAX_EXIT];
  105. int __exit_count = 0; /* Number of registered exit functions */
  106. /*
  107. * Handle the work of executing the registered exit functions
  108. */
  109. void __exit_handler(int status)
  110. {
  111. struct exit_function *efp;
  112. /* In reverse order */
  113. for ( ; __exit_count-- ; ) {
  114. efp = &__exit_function_table[__exit_count];
  115. switch (efp->type) {
  116. case ef_on_exit:
  117. if (efp->funcs.on_exit.func) {
  118. (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
  119. }
  120. break;
  121. case ef_atexit:
  122. if (efp->funcs.atexit) {
  123. (efp->funcs.atexit) ();
  124. }
  125. break;
  126. }
  127. }
  128. if (__stdio_close_all)
  129. __stdio_close_all();
  130. }
  131. #endif
  132. #ifdef L_exit
  133. extern void (*__uClibc_cleanup) (void);
  134. void (*__exit_cleanup) (int) = 0;
  135. /*
  136. * Normal program termination
  137. */
  138. void exit(int rv)
  139. {
  140. /* Perform exit-specific cleanup (atexit and on_exit) */
  141. if (__exit_cleanup) {
  142. __exit_cleanup(rv);
  143. }
  144. /* Clean up everything else */
  145. if (__uClibc_cleanup) {
  146. __uClibc_cleanup();
  147. _exit(rv);
  148. }
  149. #endif