xref: /onnv-gate/usr/src/lib/libc/port/gen/atexit.c (revision 8009:cbee40410e7d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54570Sraf  * Common Development and Distribution License (the "License").
64570Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
306812Sraf #pragma weak _atexit = atexit
316812Sraf 
326812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "thr_uberdata.h"
340Sstevel@tonic-gate #include "libc_int.h"
350Sstevel@tonic-gate #include "atexit.h"
360Sstevel@tonic-gate #include "stdiom.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Note that memory is managed by lmalloc()/lfree().
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * Among other reasons, this is occasioned by the insistence of our
420Sstevel@tonic-gate  * brothers sh(1) and csh(1) that they can do malloc, etc., better than
430Sstevel@tonic-gate  * libc can.  Those programs define their own malloc routines, and
440Sstevel@tonic-gate  * initialize the underlying mechanism in main().  This means that calls
450Sstevel@tonic-gate  * to malloc occuring before main will crash.  The loader calls atexit(3C)
460Sstevel@tonic-gate  * before calling main, so we'd better avoid malloc() when it does.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * Another reason for using lmalloc()/lfree() is that the atexit()
490Sstevel@tonic-gate  * list must transcend all link maps.  See the Linker and Libraries
500Sstevel@tonic-gate  * Guide for information on alternate link maps.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * See "thr_uberdata.h" for the definitions of structures used here.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static int in_range(_exithdlr_func_t, Lc_addr_range_t[], uint_t count);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate extern	caddr_t	_getfp(void);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * exitfns_lock is declared to be a recursive mutex so that we
610Sstevel@tonic-gate  * can hold it while calling out to the registered functions.
620Sstevel@tonic-gate  * If they call back to us, we are self-consistent and everything
630Sstevel@tonic-gate  * works, even the case of calling exit() from functions called
640Sstevel@tonic-gate  * by _exithandle() (recursive exit()).  All that is required is
650Sstevel@tonic-gate  * that the registered functions actually return (no longjmp()s).
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * Because exitfns_lock is declared to be a recursive mutex, we
685891Sraf  * cannot use it with lmutex_lock()/lmutex_unlock() and we must
695891Sraf  * use mutex_lock()/mutex_unlock().  This means that atexit()
705891Sraf  * and exit() are not async-signal-safe.  We make them fork1-safe
710Sstevel@tonic-gate  * via the atexit_locks()/atexit_unlocks() functions, called from
720Sstevel@tonic-gate  * libc_prepare_atfork()/libc_child_atfork()/libc_parent_atfork()
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * atexit_locks() and atexit_unlocks() are called on every link map.
770Sstevel@tonic-gate  * Do not use curthread->ul_uberdata->atexit_root for these.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate void
atexit_locks()800Sstevel@tonic-gate atexit_locks()
810Sstevel@tonic-gate {
826515Sraf 	(void) mutex_lock(&__uberdata.atexit_root.exitfns_lock);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate void
atexit_unlocks()860Sstevel@tonic-gate atexit_unlocks()
870Sstevel@tonic-gate {
886515Sraf 	(void) mutex_unlock(&__uberdata.atexit_root.exitfns_lock);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * atexit() is called before the primordial thread is fully set up.
930Sstevel@tonic-gate  * Be careful about dereferencing self->ul_uberdata->atexit_root.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate int
atexit(void (* func)(void))966812Sraf atexit(void (*func)(void))
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	ulwp_t *self;
990Sstevel@tonic-gate 	atexit_root_t *arp;
1000Sstevel@tonic-gate 	_exthdlr_t *p;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if ((p = lmalloc(sizeof (_exthdlr_t))) == NULL)
1030Sstevel@tonic-gate 		return (-1);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if ((self = __curthread()) == NULL)
1060Sstevel@tonic-gate 		arp = &__uberdata.atexit_root;
1070Sstevel@tonic-gate 	else {
1080Sstevel@tonic-gate 		arp = &self->ul_uberdata->atexit_root;
1096515Sraf 		(void) mutex_lock(&arp->exitfns_lock);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	p->hdlr = func;
1120Sstevel@tonic-gate 	p->next = arp->head;
1130Sstevel@tonic-gate 	arp->head = p;
1140Sstevel@tonic-gate 	if (self != NULL)
1156515Sraf 		(void) mutex_unlock(&arp->exitfns_lock);
1160Sstevel@tonic-gate 	return (0);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate void
_exithandle(void)1200Sstevel@tonic-gate _exithandle(void)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
1230Sstevel@tonic-gate 	_exthdlr_t *p;
124*8009SRoger.Faulkner@Sun.COM 	int cancel_state;
1250Sstevel@tonic-gate 
126*8009SRoger.Faulkner@Sun.COM 	/* disable cancellation while running atexit handlers */
127*8009SRoger.Faulkner@Sun.COM 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
1286515Sraf 	(void) mutex_lock(&arp->exitfns_lock);
1290Sstevel@tonic-gate 	arp->exit_frame_monitor = _getfp() + STACK_BIAS;
1300Sstevel@tonic-gate 	p = arp->head;
1310Sstevel@tonic-gate 	while (p != NULL) {
1320Sstevel@tonic-gate 		arp->head = p->next;
1330Sstevel@tonic-gate 		p->hdlr();
1340Sstevel@tonic-gate 		lfree(p, sizeof (_exthdlr_t));
1350Sstevel@tonic-gate 		p = arp->head;
1360Sstevel@tonic-gate 	}
1376515Sraf 	(void) mutex_unlock(&arp->exitfns_lock);
138*8009SRoger.Faulkner@Sun.COM 	(void) pthread_setcancelstate(cancel_state, NULL);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * _get_exit_frame_monitor is called by the C++ runtimes.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate void *
_get_exit_frame_monitor(void)1450Sstevel@tonic-gate _get_exit_frame_monitor(void)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
1480Sstevel@tonic-gate 	return (&arp->exit_frame_monitor);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
1530Sstevel@tonic-gate  * processes a dlclose call on an object.  It resets all signal handlers
1540Sstevel@tonic-gate  * which fall within the union of the ranges specified by the elements
1550Sstevel@tonic-gate  * of the array range to SIG_DFL.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate static void
_preexec_sig_unload(Lc_addr_range_t range[],uint_t count)1580Sstevel@tonic-gate _preexec_sig_unload(Lc_addr_range_t range[], uint_t count)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
1610Sstevel@tonic-gate 	int sig;
1624570Sraf 	rwlock_t *rwlp;
1630Sstevel@tonic-gate 	struct sigaction *sap;
1640Sstevel@tonic-gate 	struct sigaction oact;
1650Sstevel@tonic-gate 	void (*handler)();
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (sig = 1; sig < NSIG; sig++) {
1680Sstevel@tonic-gate 		sap = (struct sigaction *)&udp->siguaction[sig].sig_uaction;
1690Sstevel@tonic-gate again:
1700Sstevel@tonic-gate 		handler = sap->sa_handler;
1710Sstevel@tonic-gate 		if (handler != SIG_DFL && handler != SIG_IGN &&
1720Sstevel@tonic-gate 		    in_range(handler, range, count)) {
1734570Sraf 			rwlp = &udp->siguaction[sig].sig_lock;
1744570Sraf 			lrw_wrlock(rwlp);
1750Sstevel@tonic-gate 			if (handler != sap->sa_handler) {
1764570Sraf 				lrw_unlock(rwlp);
1770Sstevel@tonic-gate 				goto again;
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			sap->sa_handler = SIG_DFL;
1800Sstevel@tonic-gate 			sap->sa_flags = SA_SIGINFO;
1810Sstevel@tonic-gate 			(void) sigemptyset(&sap->sa_mask);
1820Sstevel@tonic-gate 			if (__sigaction(sig, NULL, &oact) == 0 &&
1830Sstevel@tonic-gate 			    oact.sa_handler != SIG_DFL &&
1840Sstevel@tonic-gate 			    oact.sa_handler != SIG_IGN)
1850Sstevel@tonic-gate 				(void) __sigaction(sig, sap, NULL);
1864570Sraf 			lrw_unlock(rwlp);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
1930Sstevel@tonic-gate  * processes a dlclose call on an object.  It cancels all atfork() entries
1940Sstevel@tonic-gate  * whose prefork, parent postfork, or child postfork functions fall within
1950Sstevel@tonic-gate  * the union of the ranges specified by the elements of the array range.
1960Sstevel@tonic-gate  */
1970Sstevel@tonic-gate static void
_preexec_atfork_unload(Lc_addr_range_t range[],uint_t count)1980Sstevel@tonic-gate _preexec_atfork_unload(Lc_addr_range_t range[], uint_t count)
1990Sstevel@tonic-gate {
2004843Sraf 	ulwp_t *self = curthread;
2014843Sraf 	uberdata_t *udp = self->ul_uberdata;
2020Sstevel@tonic-gate 	atfork_t *atfork_q;
2030Sstevel@tonic-gate 	atfork_t *atfp;
2040Sstevel@tonic-gate 	atfork_t *next;
2050Sstevel@tonic-gate 	void (*func)(void);
2060Sstevel@tonic-gate 	int start_again;
2070Sstevel@tonic-gate 
2086515Sraf 	(void) mutex_lock(&udp->atfork_lock);
2090Sstevel@tonic-gate 	if ((atfork_q = udp->atforklist) != NULL) {
2100Sstevel@tonic-gate 		atfp = atfork_q;
2110Sstevel@tonic-gate 		do {
2120Sstevel@tonic-gate 			next = atfp->forw;
2130Sstevel@tonic-gate 			start_again = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 			if (((func = atfp->prepare) != NULL &&
2160Sstevel@tonic-gate 			    in_range(func, range, count)) ||
2170Sstevel@tonic-gate 			    ((func = atfp->parent) != NULL &&
2180Sstevel@tonic-gate 			    in_range(func, range, count)) ||
2190Sstevel@tonic-gate 			    ((func = atfp->child) != NULL &&
2200Sstevel@tonic-gate 			    in_range(func, range, count))) {
2214843Sraf 				if (self->ul_fork) {
2220Sstevel@tonic-gate 					/*
2230Sstevel@tonic-gate 					 * dlclose() called from a fork handler.
2240Sstevel@tonic-gate 					 * Deleting the entry would wreak havoc.
2250Sstevel@tonic-gate 					 * Just null out the function pointers
2260Sstevel@tonic-gate 					 * and leave the entry in place.
2270Sstevel@tonic-gate 					 */
2280Sstevel@tonic-gate 					atfp->prepare = NULL;
2290Sstevel@tonic-gate 					atfp->parent = NULL;
2300Sstevel@tonic-gate 					atfp->child = NULL;
2310Sstevel@tonic-gate 					continue;
2320Sstevel@tonic-gate 				}
2330Sstevel@tonic-gate 				if (atfp == atfork_q) {
2340Sstevel@tonic-gate 					/* deleting the list head member */
2350Sstevel@tonic-gate 					udp->atforklist = atfork_q = next;
2360Sstevel@tonic-gate 					start_again = 1;
2370Sstevel@tonic-gate 				}
2380Sstevel@tonic-gate 				atfp->forw->back = atfp->back;
2390Sstevel@tonic-gate 				atfp->back->forw = atfp->forw;
2400Sstevel@tonic-gate 				lfree(atfp, sizeof (atfork_t));
2410Sstevel@tonic-gate 				if (atfp == atfork_q) {
2420Sstevel@tonic-gate 					/* we deleted the whole list */
2430Sstevel@tonic-gate 					udp->atforklist = NULL;
2440Sstevel@tonic-gate 					break;
2450Sstevel@tonic-gate 				}
2460Sstevel@tonic-gate 			}
2470Sstevel@tonic-gate 		} while ((atfp = next) != atfork_q || start_again);
2480Sstevel@tonic-gate 	}
2496515Sraf 	(void) mutex_unlock(&udp->atfork_lock);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
2540Sstevel@tonic-gate  * processes a dlclose call on an object.  It sets the destructor
2550Sstevel@tonic-gate  * function pointer to NULL for all keys whose destructors fall within
2560Sstevel@tonic-gate  * the union of the ranges specified by the elements of the array range.
2570Sstevel@tonic-gate  * We don't assign TSD_UNALLOCATED (the equivalent of pthread_key_destroy())
2580Sstevel@tonic-gate  * because the thread may use the key's TSD further on in fini processing.
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate static void
_preexec_tsd_unload(Lc_addr_range_t range[],uint_t count)2610Sstevel@tonic-gate _preexec_tsd_unload(Lc_addr_range_t range[], uint_t count)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
2640Sstevel@tonic-gate 	void (*func)(void *);
2650Sstevel@tonic-gate 	int key;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	lmutex_lock(&tsdm->tsdm_lock);
2680Sstevel@tonic-gate 	for (key = 1; key < tsdm->tsdm_nused; key++) {
2690Sstevel@tonic-gate 		if ((func = tsdm->tsdm_destro[key]) != NULL &&
2700Sstevel@tonic-gate 		    func != TSD_UNALLOCATED &&
2710Sstevel@tonic-gate 		    in_range((_exithdlr_func_t)func, range, count))
2720Sstevel@tonic-gate 			tsdm->tsdm_destro[key] = NULL;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	lmutex_unlock(&tsdm->tsdm_lock);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
2790Sstevel@tonic-gate  * processes dlclose calls on objects with atexit registrations.  It
2800Sstevel@tonic-gate  * executes the exit handlers that fall within the union of the ranges
2810Sstevel@tonic-gate  * specified by the elements of the array range in the REVERSE ORDER of
2820Sstevel@tonic-gate  * their registration.  Do not change this characteristic; it is REQUIRED
2830Sstevel@tonic-gate  * BEHAVIOR.
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate int
_preexec_exit_handlers(Lc_addr_range_t range[],uint_t count)2860Sstevel@tonic-gate _preexec_exit_handlers(Lc_addr_range_t range[], uint_t count)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
2890Sstevel@tonic-gate 	_exthdlr_t *o;		/* previous node */
2900Sstevel@tonic-gate 	_exthdlr_t *p;		/* this node */
291*8009SRoger.Faulkner@Sun.COM 	int cancel_state;
2920Sstevel@tonic-gate 
293*8009SRoger.Faulkner@Sun.COM 	/* disable cancellation while running atexit handlers */
294*8009SRoger.Faulkner@Sun.COM 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2956515Sraf 	(void) mutex_lock(&arp->exitfns_lock);
2960Sstevel@tonic-gate 	o = NULL;
2970Sstevel@tonic-gate 	p = arp->head;
2980Sstevel@tonic-gate 	while (p != NULL) {
2990Sstevel@tonic-gate 		if (in_range(p->hdlr, range, count)) {
3000Sstevel@tonic-gate 			/* We need to execute this one */
3010Sstevel@tonic-gate 			if (o != NULL)
3020Sstevel@tonic-gate 				o->next = p->next;
3030Sstevel@tonic-gate 			else
3040Sstevel@tonic-gate 				arp->head = p->next;
3050Sstevel@tonic-gate 			p->hdlr();
3060Sstevel@tonic-gate 			lfree(p, sizeof (_exthdlr_t));
3070Sstevel@tonic-gate 			o = NULL;
3080Sstevel@tonic-gate 			p = arp->head;
3090Sstevel@tonic-gate 		} else {
3100Sstevel@tonic-gate 			o = p;
3110Sstevel@tonic-gate 			p = p->next;
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 	}
3146515Sraf 	(void) mutex_unlock(&arp->exitfns_lock);
315*8009SRoger.Faulkner@Sun.COM 	(void) pthread_setcancelstate(cancel_state, NULL);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	_preexec_tsd_unload(range, count);
3180Sstevel@tonic-gate 	_preexec_atfork_unload(range, count);
3190Sstevel@tonic-gate 	_preexec_sig_unload(range, count);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	return (0);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate static int
in_range(_exithdlr_func_t addr,Lc_addr_range_t ranges[],uint_t count)3250Sstevel@tonic-gate in_range(_exithdlr_func_t addr, Lc_addr_range_t ranges[], uint_t count)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	uint_t idx;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	for (idx = 0; idx < count; idx++) {
3300Sstevel@tonic-gate 		if ((void *)addr >= ranges[idx].lb &&
3310Sstevel@tonic-gate 		    (void *)addr < ranges[idx].ub) {
3320Sstevel@tonic-gate 			return (1);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	return (0);
3370Sstevel@tonic-gate }
338