1 /* $OpenBSD: atexit.c,v 1.10 2005/08/08 08:05:36 espie Exp $ */
2 /*
3 * Copyright (c) 2002 Daniel Hartmeier
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
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 *
30 */
31
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include "atexit.h"
37
38 int __atexit_invalid = 1;
39 struct atexit *__atexit;
40
41 /*
42 * Function pointers are stored in a linked list of pages. The list
43 * is initially empty, and pages are allocated on demand. The first
44 * function pointer in the first allocated page (the last one in
45 * the linked list) is reserved for the cleanup function.
46 *
47 * Outside the following two functions, all pages are mprotect()'ed
48 * to prevent unintentional/malicious corruption.
49 *
50 * The free(malloc(1)) is a workaround causing malloc_init() to
51 * ensure that malloc.c gets the first mmap() call for its sbrk()
52 * games.
53 */
54
55 /*
56 * Register a function to be performed at exit.
57 */
58 int
atexit(void (* fn)(void))59 atexit(void (*fn)(void))
60 {
61 struct atexit *p = __atexit;
62 size_t pgsize = getpagesize();
63
64 if (pgsize < sizeof(*p))
65 return (-1);
66 if (p != NULL) {
67 if (p->ind + 1 >= p->max)
68 p = NULL;
69 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
70 return (-1);
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 return (-1);
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]);
84 p->next = __atexit;
85 __atexit = p;
86 if (__atexit_invalid)
87 __atexit_invalid = 0;
88 }
89 p->fns[p->ind++] = fn;
90 if (mprotect(p, pgsize, PROT_READ))
91 return (-1);
92 return (0);
93 }
94
95 /*
96 * Register the cleanup function
97 */
98 void __atexit_register_cleanup(void (*fn)(void));
99
100 void
__atexit_register_cleanup(void (* fn)(void))101 __atexit_register_cleanup(void (*fn)(void))
102 {
103 struct atexit *p = __atexit;
104 size_t pgsize = getpagesize();
105
106 if (pgsize < sizeof(*p))
107 return;
108 while (p != NULL && p->next != NULL)
109 p = p->next;
110 if (p == NULL) {
111 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
112 MAP_ANON | MAP_PRIVATE, -1, 0);
113 if (p == MAP_FAILED)
114 return;
115 p->ind = 1;
116 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
117 sizeof(p->fns[0]);
118 p->next = NULL;
119 __atexit = p;
120 if (__atexit_invalid)
121 __atexit_invalid = 0;
122 } else {
123 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
124 return;
125 }
126 p->fns[0] = fn;
127 mprotect(p, pgsize, PROT_READ);
128 }
129