diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
| -rw-r--r-- | src/lib/libc/stdlib/atexit.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c new file mode 100644 index 0000000000..da5a0ddda0 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2002 Daniel Hartmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * - Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * - Redistributions in binary form must reproduce the above | ||
| 12 | * copyright notice, this list of conditions and the following | ||
| 13 | * disclaimer in the documentation and/or other materials provided | ||
| 14 | * with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
| 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 | * | ||
| 29 | */ | ||
| 30 | |||
| 31 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 32 | static char *rcsid = "$OpenBSD: atexit.c,v 1.6 2002/09/06 22:48:34 henning Exp $"; | ||
| 33 | #endif /* LIBC_SCCS and not lint */ | ||
| 34 | |||
| 35 | #include <sys/types.h> | ||
| 36 | #include <sys/mman.h> | ||
| 37 | #include <stdlib.h> | ||
| 38 | #include <unistd.h> | ||
| 39 | #include "atexit.h" | ||
| 40 | |||
| 41 | int __atexit_invalid = 1; | ||
| 42 | struct atexit *__atexit; | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Register a function to be performed at exit. | ||
| 46 | */ | ||
| 47 | int | ||
| 48 | atexit(fn) | ||
| 49 | void (*fn)(); | ||
| 50 | { | ||
| 51 | register struct atexit *p = __atexit; | ||
| 52 | register int pgsize = getpagesize(); | ||
| 53 | |||
| 54 | if (pgsize < sizeof(*p)) | ||
| 55 | return (-1); | ||
| 56 | if (p != NULL) { | ||
| 57 | if (p->ind + 1 >= p->max) | ||
| 58 | p = NULL; | ||
| 59 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
| 60 | return (-1); | ||
| 61 | } | ||
| 62 | if (p == NULL) { | ||
| 63 | if (__atexit_invalid) { | ||
| 64 | /* malloc.c wants the first mmap() for sbrk() | ||
| 65 | games ('nice hack'), so enforce | ||
| 66 | malloc_init() with a dummy call. */ | ||
| 67 | free(malloc(1)); | ||
| 68 | __atexit_invalid = 0; | ||
| 69 | } | ||
| 70 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
| 71 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 72 | if (p == MAP_FAILED) | ||
| 73 | return (-1); | ||
| 74 | p->ind = 0; | ||
| 75 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
| 76 | sizeof(p->fns[0]); | ||
| 77 | p->next = __atexit; | ||
| 78 | __atexit = p; | ||
| 79 | } | ||
| 80 | p->fns[p->ind++] = fn; | ||
| 81 | if (mprotect(p, pgsize, PROT_READ)) | ||
| 82 | return (-1); | ||
| 83 | return (0); | ||
| 84 | } | ||
