diff options
Diffstat (limited to 'src/lib/libc/stdlib/atexit.c')
-rw-r--r-- | src/lib/libc/stdlib/atexit.c | 83 |
1 files changed, 83 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..92c4884f28 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.c | |||
@@ -0,0 +1,83 @@ | |||
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.5 2002/08/30 07:58:07 dhartmei 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 "atexit.h" | ||
39 | |||
40 | int __atexit_invalid = 1; | ||
41 | struct atexit *__atexit; | ||
42 | |||
43 | /* | ||
44 | * Register a function to be performed at exit. | ||
45 | */ | ||
46 | int | ||
47 | atexit(fn) | ||
48 | void (*fn)(); | ||
49 | { | ||
50 | register struct atexit *p = __atexit; | ||
51 | register int pgsize = getpagesize(); | ||
52 | |||
53 | if (pgsize < sizeof(*p)) | ||
54 | return (-1); | ||
55 | if (p != NULL) { | ||
56 | if (p->ind + 1 >= p->max) | ||
57 | p = NULL; | ||
58 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
59 | return (-1); | ||
60 | } | ||
61 | if (p == NULL) { | ||
62 | if (__atexit_invalid) { | ||
63 | /* malloc.c wants the first mmap() for sbrk() | ||
64 | games ('nice hack'), so enforce | ||
65 | malloc_init() with a dummy call. */ | ||
66 | free(malloc(1)); | ||
67 | __atexit_invalid = 0; | ||
68 | } | ||
69 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
70 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
71 | if (p == MAP_FAILED) | ||
72 | return (-1); | ||
73 | p->ind = 0; | ||
74 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
75 | sizeof(p->fns[0]); | ||
76 | p->next = __atexit; | ||
77 | __atexit = p; | ||
78 | } | ||
79 | p->fns[p->ind++] = fn; | ||
80 | if (mprotect(p, pgsize, PROT_READ)) | ||
81 | return (-1); | ||
82 | return (0); | ||
83 | } | ||