blob: 55b7132e62cfb6e7ff4f6433f098fe483121ba2c [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $OpenBSD: atexit.c,v 1.14 2007/09/05 20:47:47 chl Exp $ */
2/*
3 * Copyright (c) 2002 Daniel Hartmeier
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
16 *
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.
29 *
30 */
31
32#include <sys/types.h>
33#include <sys/mman.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include "atexit.h"
38#include "thread_private.h"
39
40int __atexit_invalid = 1;
41struct atexit *__atexit;
42
43/*
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.
48 *
49 * Outside the following functions, all pages are mprotect()'ed
50 * to prevent unintentional/malicious corruption.
51 */
52
53/*
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:
57 *
58 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
59 */
60int
61__cxa_atexit(void (*func)(void *), void *arg, void *dso)
62{
63 struct atexit *p = __atexit;
64 struct atexit_fn *fnp;
65 int pgsize = getpagesize();
66 int ret = -1;
67
68 if (pgsize < (int)sizeof(*p))
69 return (-1);
70 _ATEXIT_LOCK();
71 p = __atexit;
72 if (p != NULL) {
73 if (p->ind + 1 >= p->max)
74 p = NULL;
75 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
76 goto unlock;
77 }
78 if (p == NULL) {
79 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
80 MAP_ANON | MAP_PRIVATE, -1, 0);
81 if (p == MAP_FAILED)
82 goto unlock;
83 if (__atexit == NULL) {
84 memset(&p->fns[0], 0, sizeof(p->fns[0]));
85 p->ind = 1;
86 } else
87 p->ind = 0;
88 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
89 sizeof(p->fns[0]);
90 p->next = __atexit;
91 __atexit = p;
92 if (__atexit_invalid)
93 __atexit_invalid = 0;
94 }
95 fnp = &p->fns[p->ind++];
96 fnp->fn_ptr.cxa_func = func;
97 fnp->fn_arg = arg;
98 fnp->fn_dso = dso;
99 if (mprotect(p, pgsize, PROT_READ))
100 goto unlock;
101 ret = 0;
102unlock:
103 _ATEXIT_UNLOCK();
104 return (ret);
105}
106
Bruce Beare39640842011-06-20 10:29:50 -0700107#ifdef CRT_LEGACY_WORKAROUND
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108/*
109 * Register a function to be performed at exit.
110 */
111int
112atexit(void (*func)(void))
113{
114 return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
115}
Bruce Beare39640842011-06-20 10:29:50 -0700116#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117
118/*
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.
122 */
123void
124__cxa_finalize(void *dso)
125{
126 struct atexit *p, *q;
127 struct atexit_fn fn;
128 int n, pgsize = getpagesize();
129 static int call_depth;
130
131 if (__atexit_invalid)
132 return;
133
Srinavasa Nagaraju2270dfa2012-02-28 12:08:22 +0900134 _ATEXIT_LOCK();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135 call_depth++;
136
137 for (p = __atexit; p != NULL; p = p->next) {
138 for (n = p->ind; --n >= 0;) {
139 if (p->fns[n].fn_ptr.cxa_func == NULL)
140 continue; /* already called */
141 if (dso != NULL && dso != p->fns[n].fn_dso)
142 continue; /* wrong DSO */
143
144 /*
145 * Mark handler as having been already called to avoid
146 * dupes and loops, then call the appropriate function.
147 */
148 fn = p->fns[n];
149 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
150 p->fns[n].fn_ptr.cxa_func = NULL;
151 mprotect(p, pgsize, PROT_READ);
152 }
Srinavasa Nagaraju2270dfa2012-02-28 12:08:22 +0900153 _ATEXIT_UNLOCK();
David 'Digit' Turner0ba91ed2009-05-20 11:42:52 +0200154#if ANDROID
155 /* it looks like we should always call the function
156 * with an argument, even if dso is not NULL. Otherwise
157 * static destructors will not be called properly on
158 * the ARM.
159 */
160 (*fn.fn_ptr.cxa_func)(fn.fn_arg);
161#else /* !ANDROID */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162 if (dso != NULL)
163 (*fn.fn_ptr.cxa_func)(fn.fn_arg);
164 else
165 (*fn.fn_ptr.std_func)();
David 'Digit' Turner0ba91ed2009-05-20 11:42:52 +0200166#endif /* !ANDROID */
Srinavasa Nagaraju2270dfa2012-02-28 12:08:22 +0900167 _ATEXIT_LOCK();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168 }
169 }
170
171 /*
172 * If called via exit(), unmap the pages since we have now run
173 * all the handlers. We defer this until calldepth == 0 so that
174 * we don't unmap things prematurely if called recursively.
175 */
176 if (dso == NULL && --call_depth == 0) {
177 for (p = __atexit; p != NULL; ) {
178 q = p;
179 p = p->next;
180 munmap(q, pgsize);
181 }
182 __atexit = NULL;
183 }
Srinavasa Nagaraju2270dfa2012-02-28 12:08:22 +0900184 _ATEXIT_UNLOCK();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185}
186
187/*
188 * Register the cleanup function
189 */
190void
191__atexit_register_cleanup(void (*func)(void))
192{
193 struct atexit *p;
194 int pgsize = getpagesize();
195
196 if (pgsize < (int)sizeof(*p))
197 return;
198 _ATEXIT_LOCK();
199 p = __atexit;
200 while (p != NULL && p->next != NULL)
201 p = p->next;
202 if (p == NULL) {
203 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
204 MAP_ANON | MAP_PRIVATE, -1, 0);
205 if (p == MAP_FAILED)
206 goto unlock;
207 p->ind = 1;
208 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
209 sizeof(p->fns[0]);
210 p->next = NULL;
211 __atexit = p;
212 if (__atexit_invalid)
213 __atexit_invalid = 0;
214 } else {
215 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
216 goto unlock;
217 }
218 p->fns[0].fn_ptr.std_func = func;
219 p->fns[0].fn_arg = NULL;
220 p->fns[0].fn_dso = NULL;
221 mprotect(p, pgsize, PROT_READ);
222unlock:
223 _ATEXIT_UNLOCK();
224}