OSDN Git Service

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