atexit.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. */
  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. /* this is in the L_exit object */
  43. extern void (*__exit_cleanup) (int);
  44. /* these are in the L___do_exit object */
  45. extern int __exit_count;
  46. extern void __exit_handler(int);
  47. extern struct exit_function {
  48. ef_type type; /* ef_atexit or ef_on_exit */
  49. union {
  50. aefuncp atexit;
  51. struct {
  52. oefuncp func;
  53. void *arg;
  54. } on_exit;
  55. } funcs;
  56. } __exit_function_table[__MAX_EXIT];
  57. #ifdef L_atexit
  58. /*
  59. * register a function to be called at normal program termination
  60. * (the registered function takes no arguments)
  61. */
  62. int atexit(aefuncp func)
  63. {
  64. struct exit_function *efp;
  65. if (__exit_count >= __MAX_EXIT) {
  66. __set_errno(ENOMEM);
  67. return -1;
  68. }
  69. if (func) {
  70. __exit_cleanup = __exit_handler; /* enable cleanup */
  71. efp = &__exit_function_table[__exit_count++];
  72. efp->type = ef_atexit;
  73. efp->funcs.atexit = func;
  74. }
  75. return 0;
  76. }
  77. #endif
  78. #ifdef L_on_exit
  79. /*
  80. * register a function to be called at normal program termination
  81. * the registered function takes two arguments:
  82. * status - the exit status that was passed to the exit() function
  83. * arg - generic argument
  84. */
  85. int on_exit(oefuncp func, void *arg)
  86. {
  87. struct exit_function *efp;
  88. if (__exit_count >= __MAX_EXIT) {
  89. __set_errno(ENOMEM);
  90. return -1;
  91. }
  92. if (func) {
  93. __exit_cleanup = __exit_handler; /* enable cleanup */
  94. efp = &__exit_function_table[__exit_count++];
  95. efp->type = ef_on_exit;
  96. efp->funcs.on_exit.func = func;
  97. efp->funcs.on_exit.arg = arg;
  98. }
  99. return 0;
  100. }
  101. #endif
  102. #ifdef L___exit_handler
  103. struct exit_function __exit_function_table[__MAX_EXIT];
  104. int __exit_count = 0; /* Number of registered exit functions */
  105. /*
  106. * Handle the work of executing the registered exit functions
  107. */
  108. void __exit_handler(int status)
  109. {
  110. struct exit_function *efp;
  111. /* In reverse order */
  112. for ( ; __exit_count-- ; ) {
  113. efp = &__exit_function_table[__exit_count];
  114. switch (efp->type) {
  115. case ef_on_exit:
  116. if (efp->funcs.on_exit.func) {
  117. (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
  118. }
  119. break;
  120. case ef_atexit:
  121. if (efp->funcs.atexit) {
  122. (efp->funcs.atexit) ();
  123. }
  124. break;
  125. }
  126. }
  127. }
  128. #endif
  129. #ifdef L_exit
  130. extern void weak_function _stdio_term(void);
  131. void (*__exit_cleanup) (int) = 0;
  132. /*
  133. * Normal program termination
  134. */
  135. void exit(int rv)
  136. {
  137. /* Perform exit-specific cleanup (atexit and on_exit) */
  138. if (__exit_cleanup) {
  139. __exit_cleanup(rv);
  140. }
  141. /* If we are using stdio, try to shut it down. At the very least,
  142. * this will attempt to commit all buffered writes. It may also
  143. * unbuffer all writable files, or close them outright.
  144. * Check the stdio routines for details. */
  145. if (_stdio_term)
  146. _stdio_term();
  147. _exit(rv);
  148. }
  149. #endif