diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 233 |
1 files changed, 187 insertions, 46 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index 4da1eb0d9c..e28ccf29d0 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -1,68 +1,209 @@ | |||
| 1 | /*- | 1 | /* $OpenBSD: atexit.c,v 1.15 2011/03/02 18:34:05 matthew Exp $ */ |
| 2 | * Copyright (c) 1990 The Regents of the University of California. | 2 | /* |
| 3 | * Copyright (c) 2002 Daniel Hartmeier | ||
| 3 | * All rights reserved. | 4 | * All rights reserved. |
| 4 | * | 5 | * |
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions | 7 | * modification, are permitted provided that the following conditions |
| 10 | * are met: | 8 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | 9 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 10 | * - Redistributions of source code must retain the above copyright |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 11 | * notice, this list of conditions and the following disclaimer. |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 12 | * - Redistributions in binary form must reproduce the above |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 13 | * copyright notice, this list of conditions and the following |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 14 | * disclaimer in the documentation and/or other materials provided |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 15 | * with the distribution. |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 16 | * |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 34 | * SUCH DAMAGE. | 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 | * | ||
| 35 | */ | 30 | */ |
| 36 | 31 | ||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | 32 | #include <sys/types.h> |
| 38 | /*static char *sccsid = "from: @(#)atexit.c 5.2 (Berkeley) 11/14/90";*/ | 33 | #include <sys/mman.h> |
| 39 | static char *rcsid = "$Id: atexit.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <stdlib.h> | 34 | #include <stdlib.h> |
| 35 | #include <string.h> | ||
| 36 | #include <unistd.h> | ||
| 43 | #include "atexit.h" | 37 | #include "atexit.h" |
| 38 | #include "thread_private.h" | ||
| 44 | 39 | ||
| 40 | int __atexit_invalid = 1; | ||
| 45 | struct atexit *__atexit; | 41 | struct atexit *__atexit; |
| 46 | 42 | ||
| 47 | /* | 43 | /* |
| 48 | * Register a function to be performed at exit. | 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 | ||
| 49 | */ | 59 | */ |
| 50 | int | 60 | int |
| 51 | atexit(fn) | 61 | __cxa_atexit(void (*func)(void *), void *arg, void *dso) |
| 52 | void (*fn)(); | ||
| 53 | { | 62 | { |
| 54 | static struct atexit __atexit0; /* one guaranteed table */ | 63 | struct atexit *p = __atexit; |
| 55 | register struct atexit *p; | 64 | struct atexit_fn *fnp; |
| 65 | int pgsize = getpagesize(); | ||
| 66 | int ret = -1; | ||
| 56 | 67 | ||
| 57 | if ((p = __atexit) == NULL) | 68 | if (pgsize < sizeof(*p)) |
| 58 | __atexit = p = &__atexit0; | 69 | return (-1); |
| 59 | else if (p->ind >= ATEXIT_SIZE) { | 70 | _ATEXIT_LOCK(); |
| 60 | if ((p = malloc(sizeof(*p))) == NULL) | 71 | p = __atexit; |
| 61 | return (-1); | 72 | if (p != NULL) { |
| 62 | p->ind = 0; | 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]); | ||
| 63 | p->next = __atexit; | 90 | p->next = __atexit; |
| 64 | __atexit = p; | 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; | ||
| 102 | unlock: | ||
| 103 | _ATEXIT_UNLOCK(); | ||
| 104 | return (ret); | ||
| 105 | } | ||
| 106 | |||
| 107 | /* | ||
| 108 | * Register a function to be performed at exit. | ||
| 109 | */ | ||
| 110 | int | ||
| 111 | atexit(void (*func)(void)) | ||
| 112 | { | ||
| 113 | return (__cxa_atexit((void (*)(void *))func, NULL, NULL)); | ||
| 114 | } | ||
| 115 | |||
| 116 | /* | ||
| 117 | * Call all handlers registered with __cxa_atexit() for the shared | ||
| 118 | * object owning 'dso'. | ||
| 119 | * Note: if 'dso' is NULL, then all remaining handlers are called. | ||
| 120 | */ | ||
| 121 | void | ||
| 122 | __cxa_finalize(void *dso) | ||
| 123 | { | ||
| 124 | struct atexit *p, *q; | ||
| 125 | struct atexit_fn fn; | ||
| 126 | int n, pgsize = getpagesize(); | ||
| 127 | static int call_depth; | ||
| 128 | |||
| 129 | if (__atexit_invalid) | ||
| 130 | return; | ||
| 131 | |||
| 132 | call_depth++; | ||
| 133 | |||
| 134 | for (p = __atexit; p != NULL; p = p->next) { | ||
| 135 | for (n = p->ind; --n >= 0;) { | ||
| 136 | if (p->fns[n].fn_ptr.cxa_func == NULL) | ||
| 137 | continue; /* already called */ | ||
| 138 | if (dso != NULL && dso != p->fns[n].fn_dso) | ||
| 139 | continue; /* wrong DSO */ | ||
| 140 | |||
| 141 | /* | ||
| 142 | * Mark handler as having been already called to avoid | ||
| 143 | * dupes and loops, then call the appropriate function. | ||
| 144 | */ | ||
| 145 | fn = p->fns[n]; | ||
| 146 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) { | ||
| 147 | p->fns[n].fn_ptr.cxa_func = NULL; | ||
| 148 | mprotect(p, pgsize, PROT_READ); | ||
| 149 | } | ||
| 150 | if (fn.fn_dso != NULL) | ||
| 151 | (*fn.fn_ptr.cxa_func)(fn.fn_arg); | ||
| 152 | else | ||
| 153 | (*fn.fn_ptr.std_func)(); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | /* | ||
| 158 | * If called via exit(), unmap the pages since we have now run | ||
| 159 | * all the handlers. We defer this until calldepth == 0 so that | ||
| 160 | * we don't unmap things prematurely if called recursively. | ||
| 161 | */ | ||
| 162 | if (dso == NULL && --call_depth == 0) { | ||
| 163 | for (p = __atexit; p != NULL; ) { | ||
| 164 | q = p; | ||
| 165 | p = p->next; | ||
| 166 | munmap(q, pgsize); | ||
| 167 | } | ||
| 168 | __atexit = NULL; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | /* | ||
| 173 | * Register the cleanup function | ||
| 174 | */ | ||
| 175 | void | ||
| 176 | __atexit_register_cleanup(void (*func)(void)) | ||
| 177 | { | ||
| 178 | struct atexit *p; | ||
| 179 | int pgsize = getpagesize(); | ||
| 180 | |||
| 181 | if (pgsize < sizeof(*p)) | ||
| 182 | return; | ||
| 183 | _ATEXIT_LOCK(); | ||
| 184 | p = __atexit; | ||
| 185 | while (p != NULL && p->next != NULL) | ||
| 186 | p = p->next; | ||
| 187 | if (p == NULL) { | ||
| 188 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 189 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 190 | if (p == MAP_FAILED) | ||
| 191 | goto unlock; | ||
| 192 | p->ind = 1; | ||
| 193 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 194 | sizeof(p->fns[0]); | ||
| 195 | p->next = NULL; | ||
| 196 | __atexit = p; | ||
| 197 | if (__atexit_invalid) | ||
| 198 | __atexit_invalid = 0; | ||
| 199 | } else { | ||
| 200 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 201 | goto unlock; | ||
| 65 | } | 202 | } |
| 66 | p->fns[p->ind++] = fn; | 203 | p->fns[0].fn_ptr.std_func = func; |
| 67 | return (0); | 204 | p->fns[0].fn_arg = NULL; |
| 205 | p->fns[0].fn_dso = NULL; | ||
| 206 | mprotect(p, pgsize, PROT_READ); | ||
| 207 | unlock: | ||
| 208 | _ATEXIT_UNLOCK(); | ||
| 68 | } | 209 | } |
