atexit.c revision 1.14 1 /* $NetBSD: atexit.c,v 1.14 2003/03/01 04:19:37 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "reentrant.h"
40
41 #include <assert.h>
42 #include <stdlib.h>
43
44 #include "atexit.h"
45
46 struct atexit_handler {
47 struct atexit_handler *ah_next;
48 union {
49 void (*fun_atexit)(void);
50 void (*fun_cxa_atexit)(void *);
51 } ah_fun;
52 #define ah_atexit ah_fun.fun_atexit
53 #define ah_cxa_atexit ah_fun.fun_cxa_atexit
54
55 void *ah_arg; /* argument for cxa_atexit handlers */
56 void *ah_dso; /* home DSO for cxa_atexit handlers */
57 };
58
59 /*
60 * There must be at least 32 to guarantee ANSI conformance, plus
61 * 3 additional ones for the benefit of the startup code, which
62 * may use them to register the dynamic loader's cleanup routine,
63 * the profiling cleanup routine, and the global destructor routine.
64 */
65 #define NSTATIC_HANDLERS (32 + 3)
66 static struct atexit_handler atexit_handler0[NSTATIC_HANDLERS];
67
68 #define STATIC_HANDLER_P(ah) \
69 (ah >= &atexit_handler0[0] && ah < &atexit_handler0[NSTATIC_HANDLERS])
70
71 /*
72 * Stack of atexit handlers. Handlers must be called in the opposite
73 * order they were registered.
74 */
75 static struct atexit_handler *atexit_handler_stack;
76
77 #ifdef _REENTRANT
78 /* ..and a mutex to protect it all. */
79 static mutex_t atexit_mutex = MUTEX_INITIALIZER;
80 #endif /* _REENTRANT */
81
82 /*
83 * Allocate an atexit handler descriptor. If "dso" is NULL, it indicates
84 * a normal atexit handler, which must be allocated from the static pool,
85 * if possible. cxa_atexit handlers are never allocated from the static
86 * pool.
87 *
88 * atexit_mutex must be held.
89 */
90 static struct atexit_handler *
91 atexit_handler_alloc(void *dso)
92 {
93 struct atexit_handler *ah;
94 int i;
95
96 if (dso == NULL) {
97 for (i = 0; i < NSTATIC_HANDLERS; i++) {
98 ah = &atexit_handler0[i];
99 if (ah->ah_atexit == NULL) {
100 /* Slot is free. */
101 return (ah);
102 }
103 }
104 }
105
106 /*
107 * Either no static slot was free, or this is a cxa_atexit
108 * handler. Allocate a new one. We keep the atexit_mutex
109 * held to prevent handlers from being run while we (potentially)
110 * block in malloc().
111 */
112 ah = malloc(sizeof(*ah));
113 return (ah);
114 }
115
116 /*
117 * Register an atexit routine. This is suitable either for a cxa_atexit
118 * or normal atexit type handler. The __cxa_atexit() name and arguments
119 * are specified by the C++ ABI. See:
120 *
121 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
122 */
123 int
124 __cxa_atexit(void (*func)(void *), void *arg, void *dso)
125 {
126 struct atexit_handler *ah;
127
128 _DIAGASSERT(func != NULL);
129
130 mutex_lock(&atexit_mutex);
131
132 ah = atexit_handler_alloc(dso);
133 if (ah == NULL) {
134 mutex_unlock(&atexit_mutex);
135 return (-1);
136 }
137
138 ah->ah_cxa_atexit = func;
139 ah->ah_arg = arg;
140 ah->ah_dso = dso;
141
142 ah->ah_next = atexit_handler_stack;
143 atexit_handler_stack = ah;
144
145 mutex_unlock(&atexit_mutex);
146 return (0);
147 }
148
149 /*
150 * Run the list of atexit handlers. If dso is NULL, run all of them,
151 * otherwise run only those matching the specified dso.
152 */
153 void
154 __cxa_finalize(void *dso)
155 {
156 struct atexit_handler *ah, *dead_handlers = NULL, **prevp;
157
158 mutex_lock(&atexit_mutex);
159
160 for (prevp = &atexit_handler_stack; (ah = (*prevp)) != NULL;) {
161 if (dso == NULL || dso == ah->ah_dso) {
162 if (ah->ah_dso != NULL)
163 (*ah->ah_cxa_atexit)(ah->ah_arg);
164 else
165 (*ah->ah_atexit)();
166
167 *prevp = ah->ah_next;
168 if (STATIC_HANDLER_P(ah))
169 ah->ah_atexit = NULL; /* mark it free */
170 else {
171 ah->ah_next = dead_handlers;
172 dead_handlers = ah;
173 }
174 } else
175 prevp = &ah->ah_next;
176 }
177
178 mutex_unlock(&atexit_mutex);
179
180 /*
181 * Now free any dead handlers. Do this even if we're about to
182 * exit, in case a leak-detecting malloc is being used.
183 */
184 while ((ah = dead_handlers) != NULL) {
185 dead_handlers = ah->ah_next;
186 free(ah);
187 }
188 }
189
190 /*
191 * Register a function to be performed at exit.
192 */
193 int
194 atexit(void (*func)(void))
195 {
196
197 return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
198 }
199