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