xref: /titanic_44/usr/src/lib/libc/port/gen/atexit.c (revision 164c0dd6f561db19bdaf1d0b7f2a8dec44355b69)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*	Copyright (c) 1988 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 #pragma weak atexit = _atexit
34 
35 #include "synonyms.h"
36 #include "thr_uberdata.h"
37 #include "libc_int.h"
38 #include "atexit.h"
39 #include "stdiom.h"
40 
41 /*
42  * Note that memory is managed by lmalloc()/lfree().
43  *
44  * Among other reasons, this is occasioned by the insistence of our
45  * brothers sh(1) and csh(1) that they can do malloc, etc., better than
46  * libc can.  Those programs define their own malloc routines, and
47  * initialize the underlying mechanism in main().  This means that calls
48  * to malloc occuring before main will crash.  The loader calls atexit(3C)
49  * before calling main, so we'd better avoid malloc() when it does.
50  *
51  * Another reason for using lmalloc()/lfree() is that the atexit()
52  * list must transcend all link maps.  See the Linker and Libraries
53  * Guide for information on alternate link maps.
54  *
55  * See "thr_uberdata.h" for the definitions of structures used here.
56  */
57 
58 static int in_range(_exithdlr_func_t, Lc_addr_range_t[], uint_t count);
59 
60 extern	caddr_t	_getfp(void);
61 
62 /*
63  * exitfns_lock is declared to be a recursive mutex so that we
64  * can hold it while calling out to the registered functions.
65  * If they call back to us, we are self-consistent and everything
66  * works, even the case of calling exit() from functions called
67  * by _exithandle() (recursive exit()).  All that is required is
68  * that the registered functions actually return (no longjmp()s).
69  *
70  * Because exitfns_lock is declared to be a recursive mutex, we
71  * cannot use it with lmutex_lock()/lmutex_unlock() and we must use
72  * rmutex_lock()/rmutex_unlock() (which are defined to be simply
73  * mutex_lock()/mutex_unlock()).  This means that atexit() and
74  * exit() are not async-signal-safe.  We make them fork1-safe
75  * via the atexit_locks()/atexit_unlocks() functions, called from
76  * libc_prepare_atfork()/libc_child_atfork()/libc_parent_atfork()
77  */
78 
79 /*
80  * atexit_locks() and atexit_unlocks() are called on every link map.
81  * Do not use curthread->ul_uberdata->atexit_root for these.
82  */
83 void
84 atexit_locks()
85 {
86 	(void) rmutex_lock(&__uberdata.atexit_root.exitfns_lock);
87 }
88 
89 void
90 atexit_unlocks()
91 {
92 	(void) rmutex_unlock(&__uberdata.atexit_root.exitfns_lock);
93 }
94 
95 /*
96  * atexit() is called before the primordial thread is fully set up.
97  * Be careful about dereferencing self->ul_uberdata->atexit_root.
98  */
99 int
100 _atexit(void (*func)(void))
101 {
102 	ulwp_t *self;
103 	atexit_root_t *arp;
104 	_exthdlr_t *p;
105 
106 	if ((p = lmalloc(sizeof (_exthdlr_t))) == NULL)
107 		return (-1);
108 
109 	if ((self = __curthread()) == NULL)
110 		arp = &__uberdata.atexit_root;
111 	else {
112 		arp = &self->ul_uberdata->atexit_root;
113 		(void) rmutex_lock(&arp->exitfns_lock);
114 	}
115 	p->hdlr = func;
116 	p->next = arp->head;
117 	arp->head = p;
118 	if (self != NULL)
119 		(void) rmutex_unlock(&arp->exitfns_lock);
120 	return (0);
121 }
122 
123 void
124 _exithandle(void)
125 {
126 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
127 	_exthdlr_t *p;
128 
129 	(void) rmutex_lock(&arp->exitfns_lock);
130 	arp->exit_frame_monitor = _getfp() + STACK_BIAS;
131 	p = arp->head;
132 	while (p != NULL) {
133 		arp->head = p->next;
134 		p->hdlr();
135 		lfree(p, sizeof (_exthdlr_t));
136 		p = arp->head;
137 	}
138 	(void) rmutex_unlock(&arp->exitfns_lock);
139 }
140 
141 /*
142  * _get_exit_frame_monitor is called by the C++ runtimes.
143  */
144 void *
145 _get_exit_frame_monitor(void)
146 {
147 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
148 	return (&arp->exit_frame_monitor);
149 }
150 
151 /*
152  * The following is a routine which the loader (ld.so.1) calls when it
153  * processes a dlclose call on an object.  It resets all signal handlers
154  * which fall within the union of the ranges specified by the elements
155  * of the array range to SIG_DFL.
156  */
157 static void
158 _preexec_sig_unload(Lc_addr_range_t range[], uint_t count)
159 {
160 	uberdata_t *udp = curthread->ul_uberdata;
161 	int sig;
162 	mutex_t *mp;
163 	struct sigaction *sap;
164 	struct sigaction oact;
165 	void (*handler)();
166 
167 	for (sig = 1; sig < NSIG; sig++) {
168 		sap = (struct sigaction *)&udp->siguaction[sig].sig_uaction;
169 again:
170 		handler = sap->sa_handler;
171 		if (handler != SIG_DFL && handler != SIG_IGN &&
172 		    in_range(handler, range, count)) {
173 			mp = &udp->siguaction[sig].sig_lock;
174 			lmutex_lock(mp);
175 			if (handler != sap->sa_handler) {
176 				lmutex_unlock(mp);
177 				goto again;
178 			}
179 			sap->sa_handler = SIG_DFL;
180 			sap->sa_flags = SA_SIGINFO;
181 			(void) sigemptyset(&sap->sa_mask);
182 			if (__sigaction(sig, NULL, &oact) == 0 &&
183 			    oact.sa_handler != SIG_DFL &&
184 			    oact.sa_handler != SIG_IGN)
185 				(void) __sigaction(sig, sap, NULL);
186 			lmutex_unlock(mp);
187 		}
188 	}
189 }
190 
191 /*
192  * The following is a routine which the loader (ld.so.1) calls when it
193  * processes a dlclose call on an object.  It cancels all atfork() entries
194  * whose prefork, parent postfork, or child postfork functions fall within
195  * the union of the ranges specified by the elements of the array range.
196  */
197 static void
198 _preexec_atfork_unload(Lc_addr_range_t range[], uint_t count)
199 {
200 	uberdata_t *udp = curthread->ul_uberdata;
201 	atfork_t *atfork_q;
202 	atfork_t *atfp;
203 	atfork_t *next;
204 	void (*func)(void);
205 	int start_again;
206 	int error;
207 
208 	error = fork_lock_enter(NULL);
209 	if ((atfork_q = udp->atforklist) != NULL) {
210 		atfp = atfork_q;
211 		do {
212 			next = atfp->forw;
213 			start_again = 0;
214 
215 			if (((func = atfp->prepare) != NULL &&
216 			    in_range(func, range, count)) ||
217 			    ((func = atfp->parent) != NULL &&
218 			    in_range(func, range, count)) ||
219 			    ((func = atfp->child) != NULL &&
220 			    in_range(func, range, count))) {
221 				if (error) {
222 					/*
223 					 * dlclose() called from a fork handler.
224 					 * Deleting the entry would wreak havoc.
225 					 * Just null out the function pointers
226 					 * and leave the entry in place.
227 					 */
228 					atfp->prepare = NULL;
229 					atfp->parent = NULL;
230 					atfp->child = NULL;
231 					continue;
232 				}
233 				if (atfp == atfork_q) {
234 					/* deleting the list head member */
235 					udp->atforklist = atfork_q = next;
236 					start_again = 1;
237 				}
238 				atfp->forw->back = atfp->back;
239 				atfp->back->forw = atfp->forw;
240 				lfree(atfp, sizeof (atfork_t));
241 				if (atfp == atfork_q) {
242 					/* we deleted the whole list */
243 					udp->atforklist = NULL;
244 					break;
245 				}
246 			}
247 		} while ((atfp = next) != atfork_q || start_again);
248 	}
249 	fork_lock_exit();
250 }
251 
252 /*
253  * The following is a routine which the loader (ld.so.1) calls when it
254  * processes a dlclose call on an object.  It sets the destructor
255  * function pointer to NULL for all keys whose destructors fall within
256  * the union of the ranges specified by the elements of the array range.
257  * We don't assign TSD_UNALLOCATED (the equivalent of pthread_key_destroy())
258  * because the thread may use the key's TSD further on in fini processing.
259  */
260 static void
261 _preexec_tsd_unload(Lc_addr_range_t range[], uint_t count)
262 {
263 	tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
264 	void (*func)(void *);
265 	int key;
266 
267 	lmutex_lock(&tsdm->tsdm_lock);
268 	for (key = 1; key < tsdm->tsdm_nused; key++) {
269 		if ((func = tsdm->tsdm_destro[key]) != NULL &&
270 		    func != TSD_UNALLOCATED &&
271 		    in_range((_exithdlr_func_t)func, range, count))
272 			tsdm->tsdm_destro[key] = NULL;
273 	}
274 	lmutex_unlock(&tsdm->tsdm_lock);
275 }
276 
277 /*
278  * The following is a routine which the loader (ld.so.1) calls when it
279  * processes dlclose calls on objects with atexit registrations.  It
280  * executes the exit handlers that fall within the union of the ranges
281  * specified by the elements of the array range in the REVERSE ORDER of
282  * their registration.  Do not change this characteristic; it is REQUIRED
283  * BEHAVIOR.
284  */
285 int
286 _preexec_exit_handlers(Lc_addr_range_t range[], uint_t count)
287 {
288 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
289 	_exthdlr_t *o;		/* previous node */
290 	_exthdlr_t *p;		/* this node */
291 
292 	(void) rmutex_lock(&arp->exitfns_lock);
293 	o = NULL;
294 	p = arp->head;
295 	while (p != NULL) {
296 		if (in_range(p->hdlr, range, count)) {
297 			/* We need to execute this one */
298 			if (o != NULL)
299 				o->next = p->next;
300 			else
301 				arp->head = p->next;
302 			p->hdlr();
303 			lfree(p, sizeof (_exthdlr_t));
304 			o = NULL;
305 			p = arp->head;
306 		} else {
307 			o = p;
308 			p = p->next;
309 		}
310 	}
311 	(void) rmutex_unlock(&arp->exitfns_lock);
312 
313 	_preexec_tsd_unload(range, count);
314 	_preexec_atfork_unload(range, count);
315 	_preexec_sig_unload(range, count);
316 
317 	return (0);
318 }
319 
320 static int
321 in_range(_exithdlr_func_t addr, Lc_addr_range_t ranges[], uint_t count)
322 {
323 	uint_t idx;
324 
325 	for (idx = 0; idx < count; idx++) {
326 		if ((void *)addr >= ranges[idx].lb &&
327 		    (void *)addr < ranges[idx].ub) {
328 			return (1);
329 		}
330 	}
331 
332 	return (0);
333 }
334