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