diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 218 |
1 files changed, 172 insertions, 46 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index 4da1eb0d9c..049da3261d 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -1,68 +1,194 @@ | |||
| 1 | /*- | 1 | /* $OpenBSD: atexit.c,v 1.17 2013/12/28 18:38:42 kettenis 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 | ||
| 45 | struct atexit *__atexit; | 40 | struct atexit *__atexit; |
| 46 | 41 | ||
| 47 | /* | 42 | /* |
| 48 | * Register a function to be performed at exit. | 43 | * Function pointers are stored in a linked list of pages. The list |
| 44 | * is initially empty, and pages are allocated on demand. The first | ||
| 45 | * function pointer in the first allocated page (the last one in | ||
| 46 | * the linked list) is reserved for the cleanup function. | ||
| 47 | * | ||
| 48 | * Outside the following functions, all pages are mprotect()'ed | ||
| 49 | * to prevent unintentional/malicious corruption. | ||
| 50 | */ | ||
| 51 | |||
| 52 | /* | ||
| 53 | * Register a function to be performed at exit or when a shared object | ||
| 54 | * with the given dso handle is unloaded dynamically. Also used as | ||
| 55 | * the backend for atexit(). For more info on this API, see: | ||
| 56 | * | ||
| 57 | * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor | ||
| 49 | */ | 58 | */ |
| 50 | int | 59 | int |
| 51 | atexit(fn) | 60 | __cxa_atexit(void (*func)(void *), void *arg, void *dso) |
| 52 | void (*fn)(); | ||
| 53 | { | 61 | { |
| 54 | static struct atexit __atexit0; /* one guaranteed table */ | 62 | struct atexit *p = __atexit; |
| 55 | register struct atexit *p; | 63 | struct atexit_fn *fnp; |
| 64 | int pgsize = getpagesize(); | ||
| 65 | int ret = -1; | ||
| 56 | 66 | ||
| 57 | if ((p = __atexit) == NULL) | 67 | if (pgsize < sizeof(*p)) |
| 58 | __atexit = p = &__atexit0; | 68 | return (-1); |
| 59 | else if (p->ind >= ATEXIT_SIZE) { | 69 | _ATEXIT_LOCK(); |
| 60 | if ((p = malloc(sizeof(*p))) == NULL) | 70 | p = __atexit; |
| 61 | return (-1); | 71 | if (p != NULL) { |
| 62 | p->ind = 0; | 72 | if (p->ind + 1 >= p->max) |
| 73 | p = NULL; | ||
| 74 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 75 | goto unlock; | ||
| 76 | } | ||
| 77 | if (p == NULL) { | ||
| 78 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 79 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 80 | if (p == MAP_FAILED) | ||
| 81 | goto unlock; | ||
| 82 | if (__atexit == NULL) { | ||
| 83 | memset(&p->fns[0], 0, sizeof(p->fns[0])); | ||
| 84 | p->ind = 1; | ||
| 85 | } else | ||
| 86 | p->ind = 0; | ||
| 87 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 88 | sizeof(p->fns[0]); | ||
| 63 | p->next = __atexit; | 89 | p->next = __atexit; |
| 64 | __atexit = p; | 90 | __atexit = p; |
| 65 | } | 91 | } |
| 66 | p->fns[p->ind++] = fn; | 92 | fnp = &p->fns[p->ind++]; |
| 67 | return (0); | 93 | fnp->fn_ptr.cxa_func = func; |
| 94 | fnp->fn_arg = arg; | ||
| 95 | fnp->fn_dso = dso; | ||
| 96 | if (mprotect(p, pgsize, PROT_READ)) | ||
| 97 | goto unlock; | ||
| 98 | ret = 0; | ||
| 99 | unlock: | ||
| 100 | _ATEXIT_UNLOCK(); | ||
| 101 | return (ret); | ||
| 102 | } | ||
| 103 | |||
| 104 | /* | ||
| 105 | * Call all handlers registered with __cxa_atexit() for the shared | ||
| 106 | * object owning 'dso'. | ||
| 107 | * Note: if 'dso' is NULL, then all remaining handlers are called. | ||
| 108 | */ | ||
| 109 | void | ||
| 110 | __cxa_finalize(void *dso) | ||
| 111 | { | ||
| 112 | struct atexit *p, *q; | ||
| 113 | struct atexit_fn fn; | ||
| 114 | int n, pgsize = getpagesize(); | ||
| 115 | static int call_depth; | ||
| 116 | |||
| 117 | call_depth++; | ||
| 118 | |||
| 119 | for (p = __atexit; p != NULL; p = p->next) { | ||
| 120 | for (n = p->ind; --n >= 0;) { | ||
| 121 | if (p->fns[n].fn_ptr.cxa_func == NULL) | ||
| 122 | continue; /* already called */ | ||
| 123 | if (dso != NULL && dso != p->fns[n].fn_dso) | ||
| 124 | continue; /* wrong DSO */ | ||
| 125 | |||
| 126 | /* | ||
| 127 | * Mark handler as having been already called to avoid | ||
| 128 | * dupes and loops, then call the appropriate function. | ||
| 129 | */ | ||
| 130 | fn = p->fns[n]; | ||
| 131 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) { | ||
| 132 | p->fns[n].fn_ptr.cxa_func = NULL; | ||
| 133 | mprotect(p, pgsize, PROT_READ); | ||
| 134 | } | ||
| 135 | if (fn.fn_dso != NULL) | ||
| 136 | (*fn.fn_ptr.cxa_func)(fn.fn_arg); | ||
| 137 | else | ||
| 138 | (*fn.fn_ptr.std_func)(); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | call_depth--; | ||
| 143 | |||
| 144 | /* | ||
| 145 | * If called via exit(), unmap the pages since we have now run | ||
| 146 | * all the handlers. We defer this until calldepth == 0 so that | ||
| 147 | * we don't unmap things prematurely if called recursively. | ||
| 148 | */ | ||
| 149 | if (dso == NULL && call_depth == 0) { | ||
| 150 | for (p = __atexit; p != NULL; ) { | ||
| 151 | q = p; | ||
| 152 | p = p->next; | ||
| 153 | munmap(q, pgsize); | ||
| 154 | } | ||
| 155 | __atexit = NULL; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | /* | ||
| 160 | * Register the cleanup function | ||
| 161 | */ | ||
| 162 | void | ||
| 163 | __atexit_register_cleanup(void (*func)(void)) | ||
| 164 | { | ||
| 165 | struct atexit *p; | ||
| 166 | int pgsize = getpagesize(); | ||
| 167 | |||
| 168 | if (pgsize < sizeof(*p)) | ||
| 169 | return; | ||
| 170 | _ATEXIT_LOCK(); | ||
| 171 | p = __atexit; | ||
| 172 | while (p != NULL && p->next != NULL) | ||
| 173 | p = p->next; | ||
| 174 | if (p == NULL) { | ||
| 175 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 176 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 177 | if (p == MAP_FAILED) | ||
| 178 | goto unlock; | ||
| 179 | p->ind = 1; | ||
| 180 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 181 | sizeof(p->fns[0]); | ||
| 182 | p->next = NULL; | ||
| 183 | __atexit = p; | ||
| 184 | } else { | ||
| 185 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 186 | goto unlock; | ||
| 187 | } | ||
| 188 | p->fns[0].fn_ptr.std_func = func; | ||
| 189 | p->fns[0].fn_arg = NULL; | ||
| 190 | p->fns[0].fn_dso = NULL; | ||
| 191 | mprotect(p, pgsize, PROT_READ); | ||
| 192 | unlock: | ||
| 193 | _ATEXIT_UNLOCK(); | ||
| 68 | } | 194 | } |
