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.
7 * Dec 2000 Manuel Novoa III
9 * Made atexit handling conform to standards... i.e. no args.
10 * Removed on_exit since it did not match gnu libc definition.
11 * Combined atexit and __do_exit into one object file.
13 * Feb 2001 Manuel Novoa III
15 * Reworked file after addition of __uClibc_main.
16 * Changed name of __do_exit to atexit_handler.
17 * Changed name of __cleanup to __uClibc_cleanup.
18 * Moved declaration of __uClibc_cleanup to __uClibc_main
19 * where it is initialized with (possibly weak alias)
22 * Jul 2001 Steve Thayer
24 * Added an on_exit implementation (that now matches gnu libc definition.)
25 * Pulled atexit_handler out of the atexit object since it is now required by
26 * on_exit as well. Renamed it to __exit_handler.
27 * Fixed a problem where exit functions stop getting called if one of
29 * As a side effect of these changes, abort() no longer calls the exit
30 * functions (it now matches the gnu libc definition).
32 * August 2002 Erik Andersen
33 * Added locking so atexit and friends can be thread safe
44 #ifdef __UCLIBC_HAS_THREADS__
46 extern pthread_mutex_t mylock
;
47 # define LOCK pthread_mutex_lock(&mylock)
48 # define UNLOCK pthread_mutex_unlock(&mylock);
55 typedef void (*aefuncp
) (void); /* atexit function pointer */
56 typedef void (*oefuncp
) (int, void *); /* on_exit function pointer */
60 } ef_type
; /* exit function types */
62 /* this is in the L_exit object */
63 extern void (*__exit_cleanup
) (int);
65 /* these are in the L___do_exit object */
66 extern int __exit_slots
;
67 extern int __exit_count
;
68 extern void __exit_handler(int);
69 struct exit_function
{
70 ef_type type
; /* ef_atexit or ef_on_exit */
79 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
80 extern struct exit_function
*__exit_function_table
;
82 extern struct exit_function __exit_function_table
[__UCLIBC_MAX_ATEXIT
];
87 * register a function to be called at normal program termination
88 * (the registered function takes no arguments)
90 int atexit(aefuncp func
)
92 struct exit_function
*efp
;
96 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
97 /* If we are out of function table slots, make some more */
98 if (__exit_slots
< __exit_count
+1) {
99 __exit_function_table
=realloc(__exit_function_table
,
100 (__exit_slots
+20)*sizeof(struct exit_function
));
101 if (__exit_function_table
==NULL
) {
109 if (__exit_count
>= __UCLIBC_MAX_ATEXIT
) {
115 __exit_cleanup
= __exit_handler
; /* enable cleanup */
116 efp
= &__exit_function_table
[__exit_count
++];
117 efp
->type
= ef_atexit
;
118 efp
->funcs
.atexit
= func
;
127 * register a function to be called at normal program termination
128 * the registered function takes two arguments:
129 * status - the exit status that was passed to the exit() function
130 * arg - generic argument
132 int on_exit(oefuncp func
, void *arg
)
134 struct exit_function
*efp
;
138 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
139 /* If we are out of function table slots, make some more */
140 if (__exit_slots
< __exit_count
+1) {
141 __exit_function_table
=realloc(__exit_function_table
,
142 (__exit_slots
+20)*sizeof(struct exit_function
));
143 if (__exit_function_table
==NULL
) {
151 if (__exit_count
>= __UCLIBC_MAX_ATEXIT
) {
158 __exit_cleanup
= __exit_handler
; /* enable cleanup */
159 efp
= &__exit_function_table
[__exit_count
++];
160 efp
->type
= ef_on_exit
;
161 efp
->funcs
.on_exit
.func
= func
;
162 efp
->funcs
.on_exit
.arg
= arg
;
169 #ifdef L___exit_handler
170 int __exit_count
= 0; /* Number of registered exit functions */
171 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
172 struct exit_function
*__exit_function_table
= NULL
;
173 int __exit_slots
= 0; /* Size of __exit_function_table */
175 struct exit_function __exit_function_table
[__UCLIBC_MAX_ATEXIT
];
180 * Handle the work of executing the registered exit functions
181 * This is called while we are locked, so no additional locking
184 void __exit_handler(int status
)
186 struct exit_function
*efp
;
188 /* In reverse order */
189 while ( __exit_count
) {
190 efp
= &__exit_function_table
[--__exit_count
];
193 if (efp
->funcs
.on_exit
.func
) {
194 (efp
->funcs
.on_exit
.func
) (status
, efp
->funcs
.on_exit
.arg
);
198 if (efp
->funcs
.atexit
) {
199 (efp
->funcs
.atexit
) ();
204 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
205 /* Free up memory used by the __exit_function_table structure */
206 if (__exit_function_table
)
207 free(__exit_function_table
);
213 extern void weak_function
_stdio_term(void);
214 void (*__exit_cleanup
) (int) = 0;
215 #ifdef __UCLIBC_HAS_THREADS__
216 pthread_mutex_t mylock
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
220 * Normal program termination
224 /* Perform exit-specific cleanup (atexit and on_exit) */
226 if (__exit_cleanup
) {
231 /* If we are using stdio, try to shut it down. At the very least,
232 * this will attempt to commit all buffered writes. It may also
233 * unbuffer all writable files, or close them outright.
234 * Check the stdio routines for details. */