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