diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 81 |
1 files changed, 35 insertions, 46 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index 554253362f..449417b162 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -1,43 +1,46 @@ | |||
| 1 | /* | 1 | /*- |
| 2 | * Copyright (c) 2002 Daniel Hartmeier | 2 | * Copyright (c) 1990 The Regents of the University of California. |
| 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 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | 8 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions | 9 | * modification, are permitted provided that the following conditions |
| 7 | * are met: | 10 | * 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. | ||
| 8 | * | 23 | * |
| 9 | * - Redistributions of source code must retain the above copyright | 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 10 | * notice, this list of conditions and the following disclaimer. | 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 11 | * - Redistributions in binary form must reproduce the above | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 12 | * copyright notice, this list of conditions and the following | 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13 | * disclaimer in the documentation and/or other materials provided | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 14 | * with the distribution. | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 15 | * | 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 19 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 34 | * SUCH DAMAGE. |
| 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 | * | ||
| 29 | */ | 35 | */ |
| 30 | 36 | ||
| 31 | #if defined(LIBC_SCCS) && !defined(lint) | 37 | #if defined(LIBC_SCCS) && !defined(lint) |
| 32 | static char *rcsid = "$OpenBSD: atexit.c,v 1.3 2002/07/29 19:54:42 dhartmei Exp $"; | 38 | static char *rcsid = "$OpenBSD: atexit.c,v 1.4 2002/07/31 18:13:16 dhartmei Exp $"; |
| 33 | #endif /* LIBC_SCCS and not lint */ | 39 | #endif /* LIBC_SCCS and not lint */ |
| 34 | 40 | ||
| 35 | #include <sys/types.h> | ||
| 36 | #include <sys/mman.h> | ||
| 37 | #include <stdlib.h> | 41 | #include <stdlib.h> |
| 38 | #include "atexit.h" | 42 | #include "atexit.h" |
| 39 | 43 | ||
| 40 | int __atexit_invalid = 1; | ||
| 41 | struct atexit *__atexit; | 44 | struct atexit *__atexit; |
| 42 | 45 | ||
| 43 | /* | 46 | /* |
| @@ -47,32 +50,18 @@ int | |||
| 47 | atexit(fn) | 50 | atexit(fn) |
| 48 | void (*fn)(); | 51 | void (*fn)(); |
| 49 | { | 52 | { |
| 50 | register struct atexit *p = __atexit; | 53 | static struct atexit __atexit0; /* one guaranteed table */ |
| 51 | register int pgsize = getpagesize(); | 54 | register struct atexit *p; |
| 52 | 55 | ||
| 53 | if (pgsize < sizeof(*p)) | 56 | if ((p = __atexit) == NULL) |
| 54 | return (-1); | 57 | __atexit = p = &__atexit0; |
| 55 | if (p != NULL) { | 58 | else if (p->ind >= ATEXIT_SIZE) { |
| 56 | if (p->ind + 1 >= p->max) | 59 | if ((p = malloc(sizeof(*p))) == NULL) |
| 57 | p = NULL; | ||
| 58 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 59 | return (-1); | ||
| 60 | } | ||
| 61 | if (p == NULL) { | ||
| 62 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 63 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 64 | if (p == MAP_FAILED) | ||
| 65 | return (-1); | 60 | return (-1); |
| 66 | p->ind = 0; | 61 | p->ind = 0; |
| 67 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 68 | sizeof(p->fns[0]); | ||
| 69 | p->next = __atexit; | 62 | p->next = __atexit; |
| 70 | __atexit = p; | 63 | __atexit = p; |
| 71 | if (__atexit_invalid) | ||
| 72 | __atexit_invalid = 0; | ||
| 73 | } | 64 | } |
| 74 | p->fns[p->ind++] = fn; | 65 | p->fns[p->ind++] = fn; |
| 75 | if (mprotect(p, pgsize, PROT_READ)) | ||
| 76 | return (-1); | ||
| 77 | return (0); | 66 | return (0); |
| 78 | } | 67 | } |
