xref: /openbsd-src/lib/libc/stdlib/atexit.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: atexit.c,v 1.12 2006/02/22 07:16:32 otto 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 #include "thread_private.h"
38 
39 int __atexit_invalid = 1;
40 struct atexit *__atexit;
41 
42 /*
43  * Function pointers are stored in a linked list of pages. The list
44  * is initially empty, and pages are allocated on demand. The first
45  * function pointer in the first allocated page (the last one in
46  * the linked list) is reserved for the cleanup function.
47  *
48  * Outside the following two functions, all pages are mprotect()'ed
49  * to prevent unintentional/malicious corruption.
50  */
51 
52 /*
53  * Register a function to be performed at exit.
54  */
55 int
56 atexit(void (*fn)(void))
57 {
58 	struct atexit *p;
59 	int pgsize = getpagesize();
60 	int ret = -1;
61 
62 	if (pgsize < sizeof(*p))
63 		return (-1);
64 	_ATEXIT_LOCK();
65 	p = __atexit;
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 			goto unlock;
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 			goto unlock;
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 		goto unlock;
92 	ret = 0;
93 unlock:
94 	_ATEXIT_UNLOCK();
95 	return (ret);
96 }
97 
98 /*
99  * Register the cleanup function
100  */
101 void
102 __atexit_register_cleanup(void (*fn)(void))
103 {
104 	struct atexit *p;
105 	int pgsize = getpagesize();
106 
107 	if (pgsize < sizeof(*p))
108 		return;
109 	_ATEXIT_LOCK();
110 	p = __atexit;
111 	while (p != NULL && p->next != NULL)
112 		p = p->next;
113 	if (p == NULL) {
114 		p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
115 		    MAP_ANON | MAP_PRIVATE, -1, 0);
116 		if (p == MAP_FAILED)
117 			goto unlock;
118 		p->ind = 1;
119 		p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
120 		    sizeof(p->fns[0]);
121 		p->next = NULL;
122 		__atexit = p;
123 		if (__atexit_invalid)
124 			__atexit_invalid = 0;
125 	} else {
126 		if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
127 			goto unlock;
128 	}
129 	p->fns[0] = fn;
130 	mprotect(p, pgsize, PROT_READ);
131 unlock:
132 	_ATEXIT_UNLOCK();
133 }
134