diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 153 |
1 files changed, 109 insertions, 44 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index 4da1eb0d9c..50f8ec9372 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -1,68 +1,133 @@ | |||
| 1 | /*- | 1 | /* $OpenBSD: atexit.c,v 1.12 2006/02/22 07:16:32 otto 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 <unistd.h> | ||
| 43 | #include "atexit.h" | 36 | #include "atexit.h" |
| 37 | #include "thread_private.h" | ||
| 44 | 38 | ||
| 39 | int __atexit_invalid = 1; | ||
| 45 | struct atexit *__atexit; | 40 | struct atexit *__atexit; |
| 46 | 41 | ||
| 47 | /* | 42 | /* |
| 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 two functions, all pages are mprotect()'ed | ||
| 49 | * to prevent unintentional/malicious corruption. | ||
| 50 | */ | ||
| 51 | |||
| 52 | /* | ||
| 48 | * Register a function to be performed at exit. | 53 | * Register a function to be performed at exit. |
| 49 | */ | 54 | */ |
| 50 | int | 55 | int |
| 51 | atexit(fn) | 56 | atexit(void (*fn)(void)) |
| 52 | void (*fn)(); | ||
| 53 | { | 57 | { |
| 54 | static struct atexit __atexit0; /* one guaranteed table */ | 58 | struct atexit *p; |
| 55 | register struct atexit *p; | 59 | int pgsize = getpagesize(); |
| 60 | int ret = -1; | ||
| 56 | 61 | ||
| 57 | if ((p = __atexit) == NULL) | 62 | if (pgsize < sizeof(*p)) |
| 58 | __atexit = p = &__atexit0; | 63 | return (-1); |
| 59 | else if (p->ind >= ATEXIT_SIZE) { | 64 | _ATEXIT_LOCK(); |
| 60 | if ((p = malloc(sizeof(*p))) == NULL) | 65 | p = __atexit; |
| 61 | return (-1); | 66 | if (p != NULL) { |
| 62 | p->ind = 0; | 67 | if (p->ind + 1 >= p->max) |
| 68 | p = NULL; | ||
| 69 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 70 | goto unlock; | ||
| 71 | } | ||
| 72 | if (p == NULL) { | ||
| 73 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 74 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 75 | if (p == MAP_FAILED) | ||
| 76 | goto unlock; | ||
| 77 | if (__atexit == NULL) { | ||
| 78 | p->fns[0] = NULL; | ||
| 79 | p->ind = 1; | ||
| 80 | } else | ||
| 81 | p->ind = 0; | ||
| 82 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 83 | sizeof(p->fns[0]); | ||
| 63 | p->next = __atexit; | 84 | p->next = __atexit; |
| 64 | __atexit = p; | 85 | __atexit = p; |
| 86 | if (__atexit_invalid) | ||
| 87 | __atexit_invalid = 0; | ||
| 65 | } | 88 | } |
| 66 | p->fns[p->ind++] = fn; | 89 | p->fns[p->ind++] = fn; |
| 67 | return (0); | 90 | if (mprotect(p, pgsize, PROT_READ)) |
| 91 | goto unlock; | ||
| 92 | ret = 0; | ||
| 93 | unlock: | ||
| 94 | _ATEXIT_UNLOCK(); | ||
| 95 | return (ret); | ||
| 96 | } | ||
| 97 | |||
| 98 | /* | ||
| 99 | * Register the cleanup function | ||
| 100 | */ | ||
| 101 | void | ||
| 102 | __atexit_register_cleanup(void (*fn)(void)) | ||
| 103 | { | ||
| 104 | struct atexit *p; | ||
| 105 | int pgsize = getpagesize(); | ||
| 106 | |||
| 107 | if (pgsize < sizeof(*p)) | ||
| 108 | return; | ||
| 109 | _ATEXIT_LOCK(); | ||
| 110 | p = __atexit; | ||
| 111 | while (p != NULL && p->next != NULL) | ||
| 112 | p = p->next; | ||
| 113 | if (p == NULL) { | ||
| 114 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 115 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 116 | if (p == MAP_FAILED) | ||
| 117 | goto unlock; | ||
| 118 | p->ind = 1; | ||
| 119 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 120 | sizeof(p->fns[0]); | ||
| 121 | p->next = NULL; | ||
| 122 | __atexit = p; | ||
| 123 | if (__atexit_invalid) | ||
| 124 | __atexit_invalid = 0; | ||
| 125 | } else { | ||
| 126 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 127 | goto unlock; | ||
| 128 | } | ||
| 129 | p->fns[0] = fn; | ||
| 130 | mprotect(p, pgsize, PROT_READ); | ||
| 131 | unlock: | ||
| 132 | _ATEXIT_UNLOCK(); | ||
| 68 | } | 133 | } |
