OSDN Git Service

- adds several config-options to allow for turning off certain features
[uclinux-h8/uClibc.git] / libc / stdlib / _atexit.c
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 /*
7  * Dec 2000          Manuel Novoa III
8  *
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.
12  *
13  * Feb 2001          Manuel Novoa III
14  *
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)
20  *      _stdio_term.
21  *
22  * Jul 2001          Steve Thayer
23  *
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
28  *   them calls exit().
29  *   As a side effect of these changes, abort() no longer calls the exit
30  *   functions (it now matches the gnu libc definition).
31  *
32  * August 2002    Erik Andersen
33  *   Added locking so atexit and friends can be thread safe
34  *
35  * August 2005    Stephen Warren
36  *   Added __cxa_atexit and __cxa_finalize support
37  *
38  */
39
40 #include <features.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <atomic.h>
45
46 #include <bits/uClibc_mutex.h>
47 __UCLIBC_MUTEX_EXTERN(__atexit_lock);
48
49 libc_hidden_proto(exit)
50 libc_hidden_proto(_exit)
51
52
53 typedef void (*aefuncp) (void);         /* atexit function pointer */
54 typedef void (*oefuncp) (int, void *);  /* on_exit function pointer */
55 typedef void (*cxaefuncp) (void *);     /* __cxa_atexit function pointer */
56 typedef enum {
57     ef_free,
58     ef_in_use,
59     ef_on_exit,
60     ef_cxa_atexit
61 } ef_type; /* exit function types */
62
63 /* this is in the L_exit object */
64 extern void (*__exit_cleanup) (int) attribute_hidden;
65
66 /* these are in the L___do_exit object */
67 extern int __exit_slots attribute_hidden;
68 extern int __exit_count attribute_hidden;
69 extern void __exit_handler(int) attribute_hidden;
70 struct exit_function {
71         /*
72          * 'type' should be of type of the 'enum ef_type' above but since we
73          * need this element in an atomic operation we have to use 'long int'.
74          */
75         long int type; /* enum ef_type */
76         union {
77                 struct {
78                         oefuncp func;
79                         void *arg;
80                 } on_exit;
81                 struct {
82                         cxaefuncp func;
83                         void *arg;
84                         void* dso_handle;
85                 } cxa_atexit;
86         } funcs;
87 };
88 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
89 extern struct exit_function *__exit_function_table attribute_hidden;
90 #else
91 extern struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT] attribute_hidden;
92 #endif
93 extern struct exit_function *__new_exitfn (void) attribute_hidden;
94
95 /* this is in the L___cxa_atexit object */
96 extern int __cxa_atexit (cxaefuncp, void *arg, void *dso_handle);
97
98
99 /* remove old_atexit after 0.9.29 */
100 #if defined(L_atexit) || defined(L_old_atexit)
101 extern void *__dso_handle __attribute__ ((__weak__));
102
103 /*
104  * register a function to be called at normal program termination
105  * (the registered function takes no arguments)
106  */
107 #ifdef L_atexit
108 int attribute_hidden atexit(aefuncp func)
109 #else
110 int old_atexit(aefuncp func);
111 int old_atexit(aefuncp func)
112 #endif
113 {
114     /*
115      * glibc casts aefuncp to cxaefuncp.
116      * This seems dodgy, but I guess calling a function with more
117      * parameters than it needs will work everywhere?
118      */
119     return __cxa_atexit((cxaefuncp)func, NULL,
120                         &__dso_handle == NULL ? NULL : __dso_handle);
121 }
122 #ifndef L_atexit
123 weak_alias(old_atexit,atexit)
124 #endif
125 #endif
126
127 #ifdef L_on_exit
128 /*
129  * register a function to be called at normal program termination
130  * the registered function takes two arguments:
131  *     status - the exit status that was passed to the exit() function
132  *     arg - generic argument
133  */
134 int on_exit(oefuncp func, void *arg)
135 {
136     struct exit_function *efp;
137
138     if (func == NULL) {
139         return 0;
140     }
141
142     efp = __new_exitfn();
143     if (efp == NULL) {
144         return -1;
145     }
146
147     efp->funcs.on_exit.func = func;
148     efp->funcs.on_exit.arg = arg;
149     /* assign last for thread safety, since we're now unlocked */
150     efp->type = ef_on_exit;
151
152     return 0;
153 }
154 #endif
155
156 #ifdef L___cxa_atexit
157 libc_hidden_proto(__cxa_atexit)
158 int __cxa_atexit (cxaefuncp func, void *arg, void *dso_handle)
159 {
160     struct exit_function *efp;
161
162     if (func == NULL) {
163         return 0;
164     }
165
166     efp = __new_exitfn();
167     if (efp == NULL) {
168         return -1;
169     }
170
171     efp->funcs.cxa_atexit.func = func;
172     efp->funcs.cxa_atexit.arg = arg;
173     efp->funcs.cxa_atexit.dso_handle = dso_handle;
174     /* assign last for thread safety, since we're now unlocked */
175     efp->type = ef_cxa_atexit;
176
177     return 0;
178 }
179 libc_hidden_def(__cxa_atexit)
180 #endif
181
182 #ifdef L___cxa_finalize
183 /*
184  * If D is non-NULL, call all functions registered with `__cxa_atexit'
185  *  with the same dso handle.  Otherwise, if D is NULL, call all of the
186  *  registered handlers.
187  */
188 void __cxa_finalize (void *dso_handle);
189 void __cxa_finalize (void *dso_handle)
190 {
191     struct exit_function *efp;
192     int exit_count_snapshot = __exit_count;
193
194     /* In reverse order */
195     while (exit_count_snapshot) {
196         efp = &__exit_function_table[--exit_count_snapshot];
197
198         /*
199          * We check dso_handle match before we verify the type of the union entry.
200          * However, the atomic_exchange will validate that we were really "allowed"
201          * to read dso_handle...
202          */
203         if ((dso_handle == NULL || dso_handle == efp->funcs.cxa_atexit.dso_handle)
204             /* We don't want to run this cleanup more than once. */
205             && !atomic_compare_and_exchange_bool_acq(&efp->type, ef_free, ef_cxa_atexit)
206            ) {
207             /* glibc passes status (0) too, but that's not in the prototype */
208             (*efp->funcs.cxa_atexit.func)(efp->funcs.cxa_atexit.arg);
209         }
210     }
211
212 #if 0 /* haven't looked into this yet... */
213     /*
214      * Remove the registered fork handlers. We do not have to
215      * unregister anything if the program is going to terminate anyway.
216      */
217 #ifdef UNREGISTER_ATFORK
218     if (d != NULL) {
219         UNREGISTER_ATFORK (d);
220     }
221 #endif
222 #endif
223 }
224 #endif
225
226 #ifdef L___exit_handler
227 int __exit_count = 0; /* Number of registered exit functions */
228 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
229 struct exit_function *__exit_function_table = NULL;
230 int __exit_slots = 0; /* Size of __exit_function_table */
231 #else
232 struct exit_function __exit_function_table[__UCLIBC_MAX_ATEXIT];
233 #endif
234
235 /*
236  * Find and return a new exit_function pointer, for atexit,
237  * onexit and __cxa_atexit to initialize
238  */
239 struct exit_function attribute_hidden *__new_exitfn(void)
240 {
241     struct exit_function *efp;
242
243     __UCLIBC_MUTEX_LOCK(__atexit_lock);
244
245 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
246     /* If we are out of function table slots, make some more */
247     if (__exit_slots < __exit_count+1) {
248         efp=realloc(__exit_function_table,
249                     (__exit_slots+20)*sizeof(struct exit_function));
250         if (efp == NULL) {
251             __set_errno(ENOMEM);
252             goto DONE;
253         }
254         __exit_function_table = efp;
255         __exit_slots += 20;
256     }
257 #else
258     if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
259         __set_errno(ENOMEM);
260         efp = NULL;
261         goto DONE;
262     }
263 #endif
264
265     __exit_cleanup = __exit_handler; /* enable cleanup */
266     efp = &__exit_function_table[__exit_count++];
267     efp->type = ef_in_use;
268
269 DONE:
270     __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
271     return efp;
272 }
273
274 /*
275  * Handle the work of executing the registered exit functions
276  * This is called while we are locked, so no additional locking
277  * is needed...
278  */
279 void __exit_handler(int status)
280 {
281         struct exit_function *efp;
282
283         /* In reverse order */
284         while ( __exit_count ) {
285                 efp = &__exit_function_table[--__exit_count];
286                 switch (efp->type) {
287                 case ef_on_exit:
288                         if (efp->funcs.on_exit.func) {
289                                 (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
290                         }
291                         break;
292                 case ef_cxa_atexit:
293                         if (efp->funcs.cxa_atexit.func) {
294                                 /* glibc passes status too, but that's not in the prototype */
295                                 (efp->funcs.cxa_atexit.func) (efp->funcs.cxa_atexit.arg);
296                         }
297                         break;
298                 }
299         }
300 #ifdef __UCLIBC_DYNAMIC_ATEXIT__
301         /* Free up memory used by the __exit_function_table structure */
302         free(__exit_function_table);
303 #endif
304 }
305 #endif
306
307 #ifdef L_exit
308 extern void weak_function _stdio_term(void) attribute_hidden;
309 attribute_hidden void (*__exit_cleanup) (int) = 0;
310 __UCLIBC_MUTEX_INIT(__atexit_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
311
312 extern void __uClibc_fini(void);
313 libc_hidden_proto(__uClibc_fini)
314
315 /*
316  * Normal program termination
317  */
318 void exit(int rv)
319 {
320         /* Perform exit-specific cleanup (atexit and on_exit) */
321         __UCLIBC_MUTEX_LOCK(__atexit_lock);
322         if (__exit_cleanup) {
323                 __exit_cleanup(rv);
324         }
325         __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
326
327         __uClibc_fini();
328
329     /* If we are using stdio, try to shut it down.  At the very least,
330          * this will attempt to commit all buffered writes.  It may also
331          * unbuffer all writable files, or close them outright.
332          * Check the stdio routines for details. */
333         if (_stdio_term)
334             _stdio_term();
335
336         _exit(rv);
337 }
338 libc_hidden_def(exit)
339 #endif