1
/* $OpenBSD: atexit.c,v 1.14 2007/09/05 20:47:47 chl Exp $ */
3
* Copyright (c) 2002 Daniel Hartmeier
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
10
* - Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* - Redistributions in binary form must reproduce the above
13
* copyright notice, this list of conditions and the following
14
* disclaimer in the documentation and/or other materials provided
15
* with the distribution.
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
* POSSIBILITY OF SUCH DAMAGE.
32
#include <sys/types.h>
38
#include "thread_private.h"
40
int __atexit_invalid = 1;
41
struct atexit *__atexit;
44
* Function pointers are stored in a linked list of pages. The list
45
* is initially empty, and pages are allocated on demand. The first
46
* function pointer in the first allocated page (the last one in
47
* the linked list) is reserved for the cleanup function.
49
* Outside the following functions, all pages are mprotect()'ed
50
* to prevent unintentional/malicious corruption.
54
* Register a function to be performed at exit or when a shared object
55
* with the given dso handle is unloaded dynamically. Also used as
56
* the backend for atexit(). For more info on this API, see:
58
* http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
61
__cxa_atexit(void (*func)(void *), void *arg, void *dso)
63
struct atexit *p = __atexit;
64
struct atexit_fn *fnp;
65
int pgsize = getpagesize();
68
if (pgsize < (int)sizeof(*p))
73
if (p->ind + 1 >= p->max)
75
else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
79
p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
80
MAP_ANON | MAP_PRIVATE, -1, 0);
83
if (__atexit == NULL) {
84
memset(&p->fns[0], 0, sizeof(p->fns[0]));
88
p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
95
fnp = &p->fns[p->ind++];
96
fnp->fn_ptr.cxa_func = func;
99
if (mprotect(p, pgsize, PROT_READ))
107
#ifdef CRT_LEGACY_WORKAROUND
109
* Register a function to be performed at exit.
112
atexit(void (*func)(void))
114
return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
119
* Call all handlers registered with __cxa_atexit() for the shared
120
* object owning 'dso'.
121
* Note: if 'dso' is NULL, then all remaining handlers are called.
124
__cxa_finalize(void *dso)
126
struct atexit *p, *q;
128
int n, pgsize = getpagesize();
129
static int call_depth;
131
if (__atexit_invalid)
136
for (p = __atexit; p != NULL; p = p->next) {
137
for (n = p->ind; --n >= 0;) {
138
if (p->fns[n].fn_ptr.cxa_func == NULL)
139
continue; /* already called */
140
if (dso != NULL && dso != p->fns[n].fn_dso)
141
continue; /* wrong DSO */
144
* Mark handler as having been already called to avoid
145
* dupes and loops, then call the appropriate function.
148
if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
149
p->fns[n].fn_ptr.cxa_func = NULL;
150
mprotect(p, pgsize, PROT_READ);
153
/* it looks like we should always call the function
154
* with an argument, even if dso is not NULL. Otherwise
155
* static destructors will not be called properly on
158
(*fn.fn_ptr.cxa_func)(fn.fn_arg);
161
(*fn.fn_ptr.cxa_func)(fn.fn_arg);
163
(*fn.fn_ptr.std_func)();
164
#endif /* !ANDROID */
169
* If called via exit(), unmap the pages since we have now run
170
* all the handlers. We defer this until calldepth == 0 so that
171
* we don't unmap things prematurely if called recursively.
173
if (dso == NULL && --call_depth == 0) {
174
for (p = __atexit; p != NULL; ) {
184
* Register the cleanup function
187
__atexit_register_cleanup(void (*func)(void))
190
int pgsize = getpagesize();
192
if (pgsize < (int)sizeof(*p))
196
while (p != NULL && p->next != NULL)
199
p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
200
MAP_ANON | MAP_PRIVATE, -1, 0);
204
p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
208
if (__atexit_invalid)
209
__atexit_invalid = 0;
211
if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
214
p->fns[0].fn_ptr.std_func = func;
215
p->fns[0].fn_arg = NULL;
216
p->fns[0].fn_dso = NULL;
217
mprotect(p, pgsize, PROT_READ);