diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 143 | 
1 files changed, 104 insertions, 39 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index 4da1eb0d9c..4fd2c57318 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c  | |||
| @@ -1,68 +1,133 @@ | |||
| 1 | /*- | 1 | /* | 
| 2 | * Copyright (c) 1990 The Regents of the University of California. | 2 | * Copyright (c) 2002 Daniel Hartmeier | 
| 3 | * All rights reserved. | 3 | * All rights reserved. | 
| 4 | * | 4 | * | 
| 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 | 5 | * Redistribution and use in source and binary forms, with or without | 
| 9 | * modification, are permitted provided that the following conditions | 6 | * modification, are permitted provided that the following conditions | 
| 10 | * are met: | 7 | * 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 | * | 8 | * | 
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 9 | * - Redistributions of source code must retain the above copyright | 
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 10 | * notice, this list of conditions and the following disclaimer. | 
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 11 | * - Redistributions in binary form must reproduce the above | 
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 12 | * copyright notice, this list of conditions and the following | 
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 13 | * disclaimer in the documentation and/or other materials provided | 
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 14 | * with the distribution. | 
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 15 | * | 
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 17 | * "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 | 18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
| 34 | * SUCH DAMAGE. | 19 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
| 20 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
| 22 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
| 26 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 35 | */ | 29 | */ | 
| 36 | 30 | ||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | 31 | #if defined(LIBC_SCCS) && !defined(lint) | 
| 38 | /*static char *sccsid = "from: @(#)atexit.c 5.2 (Berkeley) 11/14/90";*/ | 32 | static char *rcsid = "$OpenBSD: atexit.c,v 1.8 2005/03/30 18:51:49 pat Exp $"; | 
| 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 */ | 33 | #endif /* LIBC_SCCS and not lint */ | 
| 41 | 34 | ||
| 35 | #include <sys/types.h> | ||
| 36 | #include <sys/mman.h> | ||
| 42 | #include <stdlib.h> | 37 | #include <stdlib.h> | 
| 38 | #include <unistd.h> | ||
| 43 | #include "atexit.h" | 39 | #include "atexit.h" | 
| 44 | 40 | ||
| 41 | int __atexit_invalid = 1; | ||
| 45 | struct atexit *__atexit; | 42 | struct atexit *__atexit; | 
| 46 | 43 | ||
| 47 | /* | 44 | /* | 
| 45 | * Function pointers are stored in a linked list of pages. The list | ||
| 46 | * is initially empty, and pages are allocated on demand. The first | ||
| 47 | * function pointer in the first allocated page (the last one in | ||
| 48 | * the linked list) is reserved for the cleanup function. | ||
| 49 | * | ||
| 50 | * Outside the following two functions, all pages are mprotect()'ed | ||
| 51 | * to prevent unintentional/malicious corruption. | ||
| 52 | * | ||
| 53 | * The free(malloc(1)) is a workaround causing malloc_init() to | ||
| 54 | * ensure that malloc.c gets the first mmap() call for its sbrk() | ||
| 55 | * games. | ||
| 56 | */ | ||
| 57 | |||
| 58 | /* | ||
| 48 | * Register a function to be performed at exit. | 59 | * Register a function to be performed at exit. | 
| 49 | */ | 60 | */ | 
| 50 | int | 61 | int | 
| 51 | atexit(fn) | 62 | atexit(void (*fn)(void)) | 
| 52 | void (*fn)(); | ||
| 53 | { | 63 | { | 
| 54 | static struct atexit __atexit0; /* one guaranteed table */ | 64 | struct atexit *p = __atexit; | 
| 55 | register struct atexit *p; | 65 | int pgsize = getpagesize(); | 
| 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 | if (p != NULL) { | 
| 60 | if ((p = malloc(sizeof(*p))) == NULL) | 70 | if (p->ind + 1 >= p->max) | 
| 71 | p = NULL; | ||
| 72 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 61 | return (-1); | 73 | return (-1); | 
| 62 | p->ind = 0; | 74 | } | 
| 75 | if (p == NULL) { | ||
| 76 | if (__atexit_invalid) { | ||
| 77 | free(malloc(1)); | ||
| 78 | __atexit_invalid = 0; | ||
| 79 | } | ||
| 80 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 81 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 82 | if (p == MAP_FAILED) | ||
| 83 | return (-1); | ||
| 84 | if (__atexit == NULL) { | ||
| 85 | p->fns[0] = NULL; | ||
| 86 | p->ind = 1; | ||
| 87 | } else | ||
| 88 | p->ind = 0; | ||
| 89 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 90 | sizeof(p->fns[0]); | ||
| 63 | p->next = __atexit; | 91 | p->next = __atexit; | 
| 64 | __atexit = p; | 92 | __atexit = p; | 
| 65 | } | 93 | } | 
| 66 | p->fns[p->ind++] = fn; | 94 | p->fns[p->ind++] = fn; | 
| 95 | if (mprotect(p, pgsize, PROT_READ)) | ||
| 96 | return (-1); | ||
| 67 | return (0); | 97 | return (0); | 
| 68 | } | 98 | } | 
| 99 | |||
| 100 | /* | ||
| 101 | * Register the cleanup function | ||
| 102 | */ | ||
| 103 | void | ||
| 104 | __atexit_register_cleanup(void (*fn)(void)) | ||
| 105 | { | ||
| 106 | struct atexit *p = __atexit; | ||
| 107 | int pgsize = getpagesize(); | ||
| 108 | |||
| 109 | if (pgsize < sizeof(*p)) | ||
| 110 | return; | ||
| 111 | while (p != NULL && p->next != NULL) | ||
| 112 | p = p->next; | ||
| 113 | if (p == NULL) { | ||
| 114 | if (__atexit_invalid) { | ||
| 115 | free(malloc(1)); | ||
| 116 | __atexit_invalid = 0; | ||
| 117 | } | ||
| 118 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 119 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 120 | if (p == MAP_FAILED) | ||
| 121 | return; | ||
| 122 | p->ind = 1; | ||
| 123 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 124 | sizeof(p->fns[0]); | ||
| 125 | p->next = NULL; | ||
| 126 | __atexit = p; | ||
| 127 | } else { | ||
| 128 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | p->fns[0] = fn; | ||
| 132 | mprotect(p, pgsize, PROT_READ); | ||
| 133 | } | ||
