blob: df2b1b5d5c77bdbe144f7fffc34eeb7dd4aca091 [file] [log] [blame]
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -07001/* $OpenBSD: atexit.c,v 1.20 2014/07/11 09:51:37 kettenis 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"
Dmitriy Ivanovea295f62014-11-20 20:47:02 -080038#include "private/thread_private.h"
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070039
40struct atexit *__atexit;
41static int restartloop;
42
Dmitriy Ivanovea295f62014-11-20 20:47:02 -080043/* BEGIN android-changed: __unregister_atfork is used by __cxa_finalize */
44extern void __unregister_atfork(void* dso);
45/* END android-changed */
46
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070047/*
48 * Function pointers are stored in a linked list of pages. The list
49 * is initially empty, and pages are allocated on demand. The first
50 * function pointer in the first allocated page (the last one in
51 * the linked list) is reserved for the cleanup function.
52 *
53 * Outside the following functions, all pages are mprotect()'ed
54 * to prevent unintentional/malicious corruption.
55 */
56
57/*
58 * Register a function to be performed at exit or when a shared object
59 * with the given dso handle is unloaded dynamically. Also used as
60 * the backend for atexit(). For more info on this API, see:
61 *
62 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
63 */
64int
65__cxa_atexit(void (*func)(void *), void *arg, void *dso)
66{
67 struct atexit *p = __atexit;
68 struct atexit_fn *fnp;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -080069 size_t pgsize = getpagesize();
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -070070 int ret = -1;
71
72 if (pgsize < sizeof(*p))
73 return (-1);
74 _ATEXIT_LOCK();
75 p = __atexit;
76 if (p != NULL) {
77 if (p->ind + 1 >= p->max)
78 p = NULL;
79 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
80 goto unlock;
81 }
82 if (p == NULL) {
83 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
84 MAP_ANON | MAP_PRIVATE, -1, 0);
85 if (p == MAP_FAILED)
86 goto unlock;
87 if (__atexit == NULL) {
88 memset(&p->fns[0], 0, sizeof(p->fns[0]));
89 p->ind = 1;
90 } else
91 p->ind = 0;
92 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
93 sizeof(p->fns[0]);
94 p->next = __atexit;
95 __atexit = p;
96 }
97 fnp = &p->fns[p->ind++];
98 fnp->fn_ptr = func;
99 fnp->fn_arg = arg;
100 fnp->fn_dso = dso;
101 if (mprotect(p, pgsize, PROT_READ))
102 goto unlock;
103 restartloop = 1;
104 ret = 0;
105unlock:
106 _ATEXIT_UNLOCK();
107 return (ret);
108}
109
110/*
111 * Call all handlers registered with __cxa_atexit() for the shared
112 * object owning 'dso'.
113 * Note: if 'dso' is NULL, then all remaining handlers are called.
114 */
115void
116__cxa_finalize(void *dso)
117{
118 struct atexit *p, *q;
119 struct atexit_fn fn;
120 int n, pgsize = getpagesize();
121 static int call_depth;
122
123 _ATEXIT_LOCK();
124 call_depth++;
125
126restart:
127 restartloop = 0;
128 for (p = __atexit; p != NULL; p = p->next) {
129 for (n = p->ind; --n >= 0;) {
130 if (p->fns[n].fn_ptr == NULL)
131 continue; /* already called */
132 if (dso != NULL && dso != p->fns[n].fn_dso)
133 continue; /* wrong DSO */
134
135 /*
136 * Mark handler as having been already called to avoid
137 * dupes and loops, then call the appropriate function.
138 */
139 fn = p->fns[n];
140 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
141 p->fns[n].fn_ptr = NULL;
142 mprotect(p, pgsize, PROT_READ);
143 }
144 _ATEXIT_UNLOCK();
145 (*fn.fn_ptr)(fn.fn_arg);
146 _ATEXIT_LOCK();
147 if (restartloop)
148 goto restart;
149 }
150 }
151
152 call_depth--;
153
154 /*
155 * If called via exit(), unmap the pages since we have now run
156 * all the handlers. We defer this until calldepth == 0 so that
157 * we don't unmap things prematurely if called recursively.
158 */
159 if (dso == NULL && call_depth == 0) {
160 for (p = __atexit; p != NULL; ) {
161 q = p;
162 p = p->next;
163 munmap(q, pgsize);
164 }
165 __atexit = NULL;
166 }
167 _ATEXIT_UNLOCK();
Dmitriy Ivanovea295f62014-11-20 20:47:02 -0800168
169 /* BEGIN android-changed: call __unregister_atfork if dso is not null */
170 if (dso != NULL) {
171 __unregister_atfork(dso);
172 }
173 /* END android-changed */
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -0700174}
175
176/*
177 * Register the cleanup function
178 */
179void
180__atexit_register_cleanup(void (*func)(void))
181{
182 struct atexit *p;
Dmitriy Ivanovea295f62014-11-20 20:47:02 -0800183 size_t pgsize = getpagesize();
Dmitriy Ivanov53c3c272014-07-11 12:59:16 -0700184
185 if (pgsize < sizeof(*p))
186 return;
187 _ATEXIT_LOCK();
188 p = __atexit;
189 while (p != NULL && p->next != NULL)
190 p = p->next;
191 if (p == NULL) {
192 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
193 MAP_ANON | MAP_PRIVATE, -1, 0);
194 if (p == MAP_FAILED)
195 goto unlock;
196 p->ind = 1;
197 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
198 sizeof(p->fns[0]);
199 p->next = NULL;
200 __atexit = p;
201 } else {
202 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
203 goto unlock;
204 }
205 p->fns[0].fn_ptr = (void (*)(void *))func;
206 p->fns[0].fn_arg = NULL;
207 p->fns[0].fn_dso = NULL;
208 mprotect(p, pgsize, PROT_READ);
209 restartloop = 1;
210unlock:
211 _ATEXIT_UNLOCK();
212}