OSDN Git Service

Doh! missed a spot.
[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  */
36
37 #define _GNU_SOURCE
38 #include <features.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <errno.h>
42
43
44 #ifdef __UCLIBC_HAS_THREADS__
45 #include <pthread.h>
46 extern pthread_mutex_t mylock;
47 # define LOCK   pthread_mutex_lock(&mylock)
48 # define UNLOCK pthread_mutex_unlock(&mylock);
49 #else
50 # define LOCK
51 # define UNLOCK
52 #endif
53
54
55 #define __MAX_EXIT __UCLIBC_MAX_ATEXIT
56
57 typedef void (*aefuncp) (void);         /* atexit function pointer */
58 typedef void (*oefuncp) (int, void *);  /* on_exit function pointer */
59 typedef enum {
60         ef_atexit,
61         ef_on_exit
62 } ef_type; /* exit function types */
63
64 /* this is in the L_exit object */
65 extern void (*__exit_cleanup) (int);
66
67 /* these are in the L___do_exit object */
68 extern int __exit_count;
69 extern void __exit_handler(int);
70 extern struct exit_function {
71         ef_type type;   /* ef_atexit or ef_on_exit */
72         union {
73                 aefuncp atexit;
74                 struct {
75                         oefuncp func;
76                         void *arg;
77                 } on_exit;
78         } funcs;
79 } __exit_function_table[__MAX_EXIT];
80
81 #ifdef L_atexit
82         /*
83  * register a function to be called at normal program termination
84  * (the registered function takes no arguments)
85          */
86 int atexit(aefuncp func)
87 {
88         struct exit_function *efp;
89
90         LOCK;
91         if (__exit_count >= __MAX_EXIT) {
92                 UNLOCK;
93                 __set_errno(ENOMEM);
94                 return -1;
95         }
96         if (func) {
97                 __exit_cleanup = __exit_handler; /* enable cleanup */
98                 efp = &__exit_function_table[__exit_count++];
99                 efp->type = ef_atexit;
100                 efp->funcs.atexit = func;
101         }
102         UNLOCK;
103         return 0;
104 }
105 #endif
106
107 #ifdef L_on_exit
108 /*
109  * register a function to be called at normal program termination
110  * the registered function takes two arguments:
111  *     status - the exit status that was passed to the exit() function
112  *     arg - generic argument
113  */
114 int on_exit(oefuncp func, void *arg)
115 {
116         struct exit_function *efp;
117
118         LOCK;
119         if (__exit_count >= __MAX_EXIT) {
120                 UNLOCK;
121                 __set_errno(ENOMEM);
122                 return -1;
123         }
124         if (func) {
125                 __exit_cleanup = __exit_handler; /* enable cleanup */
126                 efp = &__exit_function_table[__exit_count++];
127                 efp->type = ef_on_exit;
128                 efp->funcs.on_exit.func = func;
129                 efp->funcs.on_exit.arg = arg;
130         }
131         UNLOCK;
132         return 0;
133 }
134 #endif
135
136 #ifdef L___exit_handler
137 struct exit_function __exit_function_table[__MAX_EXIT];
138 int __exit_count = 0; /* Number of registered exit functions */
139
140 /*
141  * Handle the work of executing the registered exit functions
142  * This is called while we are locked, so no additional locking
143  * is needed...
144  */
145 void __exit_handler(int status)
146 {
147         struct exit_function *efp;
148
149         /* In reverse order */
150         for ( ; __exit_count-- ; ) {
151                 efp = &__exit_function_table[__exit_count];
152                 switch (efp->type) {
153                 case ef_on_exit:
154                         if (efp->funcs.on_exit.func) {
155                                 (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
156                         }
157                         break;
158                 case ef_atexit:
159                         if (efp->funcs.atexit) {
160                                 (efp->funcs.atexit) ();
161                         }
162                         break;
163                 }
164         }
165 }
166 #endif
167
168 #ifdef L_exit
169 extern void weak_function _stdio_term(void);
170 void (*__exit_cleanup) (int) = 0;
171 #ifdef __UCLIBC_HAS_THREADS__
172 pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
173 #endif
174
175 /*
176  * Normal program termination
177  */
178 void exit(int rv)
179 {
180         /* Perform exit-specific cleanup (atexit and on_exit) */
181         LOCK;
182         if (__exit_cleanup) {
183                 __exit_cleanup(rv);
184         }
185         UNLOCK;
186
187     /* If we are using stdio, try to shut it down.  At the very least,
188          * this will attempt to commit all buffered writes.  It may also
189          * unbuffer all writable files, or close them outright.
190          * Check the stdio routines for details. */
191         if (_stdio_term) 
192             _stdio_term();
193
194         _exit(rv);
195 }
196 #endif