atexit.c revision 1.23.8.2 1 1.23.8.2 martin /* $NetBSD: atexit.c,v 1.23.8.2 2008/04/28 20:23:01 martin Exp $ */
2 1.23.8.2 martin
3 1.23.8.2 martin /*-
4 1.23.8.2 martin * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.23.8.2 martin * All rights reserved.
6 1.23.8.2 martin *
7 1.23.8.2 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.23.8.2 martin * by Jason R. Thorpe.
9 1.23.8.2 martin *
10 1.23.8.2 martin * Redistribution and use in source and binary forms, with or without
11 1.23.8.2 martin * modification, are permitted provided that the following conditions
12 1.23.8.2 martin * are met:
13 1.23.8.2 martin * 1. Redistributions of source code must retain the above copyright
14 1.23.8.2 martin * notice, this list of conditions and the following disclaimer.
15 1.23.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.23.8.2 martin * notice, this list of conditions and the following disclaimer in the
17 1.23.8.2 martin * documentation and/or other materials provided with the distribution.
18 1.23.8.2 martin *
19 1.23.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.23.8.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.23.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.23.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.23.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.23.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.23.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.23.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.23.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.23.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.23.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.23.8.2 martin */
31 1.23.8.2 martin
32 1.23.8.2 martin #include <sys/cdefs.h>
33 1.23.8.2 martin #if defined(LIBC_SCCS) && !defined(lint)
34 1.23.8.2 martin __RCSID("$NetBSD: atexit.c,v 1.23.8.2 2008/04/28 20:23:01 martin Exp $");
35 1.23.8.2 martin #endif /* LIBC_SCCS and not lint */
36 1.23.8.2 martin
37 1.23.8.2 martin #include "reentrant.h"
38 1.23.8.2 martin
39 1.23.8.2 martin #include <assert.h>
40 1.23.8.2 martin #include <stdlib.h>
41 1.23.8.2 martin
42 1.23.8.2 martin #include "atexit.h"
43 1.23.8.2 martin
44 1.23.8.2 martin struct atexit_handler {
45 1.23.8.2 martin struct atexit_handler *ah_next;
46 1.23.8.2 martin union {
47 1.23.8.2 martin void (*fun_atexit)(void);
48 1.23.8.2 martin void (*fun_cxa_atexit)(void *);
49 1.23.8.2 martin } ah_fun;
50 1.23.8.2 martin #define ah_atexit ah_fun.fun_atexit
51 1.23.8.2 martin #define ah_cxa_atexit ah_fun.fun_cxa_atexit
52 1.23.8.2 martin
53 1.23.8.2 martin void *ah_arg; /* argument for cxa_atexit handlers */
54 1.23.8.2 martin void *ah_dso; /* home DSO for cxa_atexit handlers */
55 1.23.8.2 martin };
56 1.23.8.2 martin
57 1.23.8.2 martin /*
58 1.23.8.2 martin * There must be at least 32 to guarantee ANSI conformance, plus
59 1.23.8.2 martin * 3 additional ones for the benefit of the startup code, which
60 1.23.8.2 martin * may use them to register the dynamic loader's cleanup routine,
61 1.23.8.2 martin * the profiling cleanup routine, and the global destructor routine.
62 1.23.8.2 martin */
63 1.23.8.2 martin #define NSTATIC_HANDLERS (32 + 3)
64 1.23.8.2 martin static struct atexit_handler atexit_handler0[NSTATIC_HANDLERS];
65 1.23.8.2 martin
66 1.23.8.2 martin #define STATIC_HANDLER_P(ah) \
67 1.23.8.2 martin (ah >= &atexit_handler0[0] && ah < &atexit_handler0[NSTATIC_HANDLERS])
68 1.23.8.2 martin
69 1.23.8.2 martin /*
70 1.23.8.2 martin * Stack of atexit handlers. Handlers must be called in the opposite
71 1.23.8.2 martin * order they were registered.
72 1.23.8.2 martin */
73 1.23.8.2 martin static struct atexit_handler *atexit_handler_stack;
74 1.23.8.2 martin
75 1.23.8.2 martin #ifdef _REENTRANT
76 1.23.8.2 martin /* ..and a mutex to protect it all. */
77 1.23.8.2 martin static mutex_t atexit_mutex;
78 1.23.8.2 martin #endif /* _REENTRANT */
79 1.23.8.2 martin
80 1.23.8.2 martin void __libc_atexit_init(void) __attribute__ ((visibility("hidden")));
81 1.23.8.2 martin
82 1.23.8.2 martin /*
83 1.23.8.2 martin * Allocate an atexit handler descriptor. If "dso" is NULL, it indicates
84 1.23.8.2 martin * a normal atexit handler, which must be allocated from the static pool,
85 1.23.8.2 martin * if possible. cxa_atexit handlers are never allocated from the static
86 1.23.8.2 martin * pool.
87 1.23.8.2 martin *
88 1.23.8.2 martin * atexit_mutex must be held.
89 1.23.8.2 martin */
90 1.23.8.2 martin static struct atexit_handler *
91 1.23.8.2 martin atexit_handler_alloc(void *dso)
92 1.23.8.2 martin {
93 1.23.8.2 martin struct atexit_handler *ah;
94 1.23.8.2 martin int i;
95 1.23.8.2 martin
96 1.23.8.2 martin if (dso == NULL) {
97 1.23.8.2 martin for (i = 0; i < NSTATIC_HANDLERS; i++) {
98 1.23.8.2 martin ah = &atexit_handler0[i];
99 1.23.8.2 martin if (ah->ah_atexit == NULL && ah->ah_next == NULL) {
100 1.23.8.2 martin /* Slot is free. */
101 1.23.8.2 martin return (ah);
102 1.23.8.2 martin }
103 1.23.8.2 martin }
104 1.23.8.2 martin }
105 1.23.8.2 martin
106 1.23.8.2 martin /*
107 1.23.8.2 martin * Either no static slot was free, or this is a cxa_atexit
108 1.23.8.2 martin * handler. Allocate a new one. We keep the atexit_mutex
109 1.23.8.2 martin * held to prevent handlers from being run while we (potentially)
110 1.23.8.2 martin * block in malloc().
111 1.23.8.2 martin */
112 1.23.8.2 martin ah = malloc(sizeof(*ah));
113 1.23.8.2 martin return (ah);
114 1.23.8.2 martin }
115 1.23.8.2 martin
116 1.23.8.2 martin /*
117 1.23.8.2 martin * Initialize atexit_mutex with the PTHREAD_MUTEX_RECURSIVE attribute.
118 1.23.8.2 martin * Note that __cxa_finalize may generate calls to __cxa_atexit.
119 1.23.8.2 martin */
120 1.23.8.2 martin void
121 1.23.8.2 martin __libc_atexit_init(void)
122 1.23.8.2 martin {
123 1.23.8.2 martin mutexattr_t atexit_mutex_attr;
124 1.23.8.2 martin mutexattr_init(&atexit_mutex_attr);
125 1.23.8.2 martin mutexattr_settype(&atexit_mutex_attr, PTHREAD_MUTEX_RECURSIVE);
126 1.23.8.2 martin mutex_init(&atexit_mutex, &atexit_mutex_attr);
127 1.23.8.2 martin }
128 1.23.8.2 martin
129 1.23.8.2 martin /*
130 1.23.8.2 martin * Register an atexit routine. This is suitable either for a cxa_atexit
131 1.23.8.2 martin * or normal atexit type handler. The __cxa_atexit() name and arguments
132 1.23.8.2 martin * are specified by the C++ ABI. See:
133 1.23.8.2 martin *
134 1.23.8.2 martin * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
135 1.23.8.2 martin */
136 1.23.8.2 martin int
137 1.23.8.2 martin __cxa_atexit(void (*func)(void *), void *arg, void *dso)
138 1.23.8.2 martin {
139 1.23.8.2 martin struct atexit_handler *ah;
140 1.23.8.2 martin
141 1.23.8.2 martin _DIAGASSERT(func != NULL);
142 1.23.8.2 martin
143 1.23.8.2 martin mutex_lock(&atexit_mutex);
144 1.23.8.2 martin
145 1.23.8.2 martin ah = atexit_handler_alloc(dso);
146 1.23.8.2 martin if (ah == NULL) {
147 1.23.8.2 martin mutex_unlock(&atexit_mutex);
148 1.23.8.2 martin return (-1);
149 1.23.8.2 martin }
150 1.23.8.2 martin
151 1.23.8.2 martin ah->ah_cxa_atexit = func;
152 1.23.8.2 martin ah->ah_arg = arg;
153 1.23.8.2 martin ah->ah_dso = dso;
154 1.23.8.2 martin
155 1.23.8.2 martin ah->ah_next = atexit_handler_stack;
156 1.23.8.2 martin atexit_handler_stack = ah;
157 1.23.8.2 martin
158 1.23.8.2 martin mutex_unlock(&atexit_mutex);
159 1.23.8.2 martin return (0);
160 1.23.8.2 martin }
161 1.23.8.2 martin
162 1.23.8.2 martin /*
163 1.23.8.2 martin * Run the list of atexit handlers. If dso is NULL, run all of them,
164 1.23.8.2 martin * otherwise run only those matching the specified dso.
165 1.23.8.2 martin *
166 1.23.8.2 martin * Note that we can be recursively invoked; rtld cleanup is via an
167 1.23.8.2 martin * atexit handler, and rtld cleanup invokes _fini() for DSOs, which
168 1.23.8.2 martin * in turn invokes __cxa_finalize() for the DSO.
169 1.23.8.2 martin */
170 1.23.8.2 martin void
171 1.23.8.2 martin __cxa_finalize(void *dso)
172 1.23.8.2 martin {
173 1.23.8.2 martin static u_int call_depth;
174 1.23.8.2 martin struct atexit_handler *ah, *dead_handlers = NULL, **prevp;
175 1.23.8.2 martin void (*cxa_func)(void *);
176 1.23.8.2 martin void (*atexit_func)(void);
177 1.23.8.2 martin
178 1.23.8.2 martin mutex_lock(&atexit_mutex);
179 1.23.8.2 martin call_depth++;
180 1.23.8.2 martin
181 1.23.8.2 martin /*
182 1.23.8.2 martin * If we are at call depth 1 (which is usually the "do everything"
183 1.23.8.2 martin * call from exit(3)), we go ahead and remove elements from the
184 1.23.8.2 martin * list as we call them. This will prevent any nested calls from
185 1.23.8.2 martin * having to traverse elements we've already processed. If we are
186 1.23.8.2 martin * at call depth > 1, we simply mark elements we process as unused.
187 1.23.8.2 martin * When the depth 1 caller sees those, it will simply unlink them
188 1.23.8.2 martin * for us.
189 1.23.8.2 martin */
190 1.23.8.2 martin again:
191 1.23.8.2 martin for (prevp = &atexit_handler_stack; (ah = (*prevp)) != NULL;) {
192 1.23.8.2 martin if (dso == NULL || dso == ah->ah_dso || ah->ah_atexit == NULL) {
193 1.23.8.2 martin if (ah->ah_atexit != NULL) {
194 1.23.8.2 martin void *p = atexit_handler_stack;
195 1.23.8.2 martin if (ah->ah_dso != NULL) {
196 1.23.8.2 martin cxa_func = ah->ah_cxa_atexit;
197 1.23.8.2 martin ah->ah_cxa_atexit = NULL;
198 1.23.8.2 martin (*cxa_func)(ah->ah_arg);
199 1.23.8.2 martin } else {
200 1.23.8.2 martin atexit_func = ah->ah_atexit;
201 1.23.8.2 martin ah->ah_atexit = NULL;
202 1.23.8.2 martin (*atexit_func)();
203 1.23.8.2 martin }
204 1.23.8.2 martin /* Restart if new atexit handler was added. */
205 1.23.8.2 martin if (p != atexit_handler_stack)
206 1.23.8.2 martin goto again;
207 1.23.8.2 martin }
208 1.23.8.2 martin
209 1.23.8.2 martin if (call_depth == 1) {
210 1.23.8.2 martin *prevp = ah->ah_next;
211 1.23.8.2 martin if (STATIC_HANDLER_P(ah))
212 1.23.8.2 martin ah->ah_next = NULL;
213 1.23.8.2 martin else {
214 1.23.8.2 martin ah->ah_next = dead_handlers;
215 1.23.8.2 martin dead_handlers = ah;
216 1.23.8.2 martin }
217 1.23.8.2 martin } else
218 1.23.8.2 martin prevp = &ah->ah_next;
219 1.23.8.2 martin } else
220 1.23.8.2 martin prevp = &ah->ah_next;
221 1.23.8.2 martin }
222 1.23.8.2 martin
223 1.23.8.2 martin call_depth--;
224 1.23.8.2 martin
225 1.23.8.2 martin if (call_depth > 0)
226 1.23.8.2 martin return;
227 1.23.8.2 martin
228 1.23.8.2 martin mutex_unlock(&atexit_mutex);
229 1.23.8.2 martin
230 1.23.8.2 martin /*
231 1.23.8.2 martin * Now free any dead handlers. Do this even if we're about to
232 1.23.8.2 martin * exit, in case a leak-detecting malloc is being used.
233 1.23.8.2 martin */
234 1.23.8.2 martin while ((ah = dead_handlers) != NULL) {
235 1.23.8.2 martin dead_handlers = ah->ah_next;
236 1.23.8.2 martin free(ah);
237 1.23.8.2 martin }
238 1.23.8.2 martin }
239 1.23.8.2 martin
240 1.23.8.2 martin /*
241 1.23.8.2 martin * Register a function to be performed at exit.
242 1.23.8.2 martin */
243 1.23.8.2 martin int
244 1.23.8.2 martin atexit(void (*func)(void))
245 1.23.8.2 martin {
246 1.23.8.2 martin
247 1.23.8.2 martin return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
248 1.23.8.2 martin }
249