xref: /onnv-gate/usr/src/lib/libc/port/gen/atexit.c (revision 6812:febeba71273d)
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 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma weak _atexit = atexit
33*6812Sraf 
34*6812Sraf #include "lint.h"
350Sstevel@tonic-gate #include "thr_uberdata.h"
360Sstevel@tonic-gate #include "libc_int.h"
370Sstevel@tonic-gate #include "atexit.h"
380Sstevel@tonic-gate #include "stdiom.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * Note that memory is managed by lmalloc()/lfree().
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * Among other reasons, this is occasioned by the insistence of our
440Sstevel@tonic-gate  * brothers sh(1) and csh(1) that they can do malloc, etc., better than
450Sstevel@tonic-gate  * libc can.  Those programs define their own malloc routines, and
460Sstevel@tonic-gate  * initialize the underlying mechanism in main().  This means that calls
470Sstevel@tonic-gate  * to malloc occuring before main will crash.  The loader calls atexit(3C)
480Sstevel@tonic-gate  * before calling main, so we'd better avoid malloc() when it does.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Another reason for using lmalloc()/lfree() is that the atexit()
510Sstevel@tonic-gate  * list must transcend all link maps.  See the Linker and Libraries
520Sstevel@tonic-gate  * Guide for information on alternate link maps.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * See "thr_uberdata.h" for the definitions of structures used here.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static int in_range(_exithdlr_func_t, Lc_addr_range_t[], uint_t count);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate extern	caddr_t	_getfp(void);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * exitfns_lock is declared to be a recursive mutex so that we
630Sstevel@tonic-gate  * can hold it while calling out to the registered functions.
640Sstevel@tonic-gate  * If they call back to us, we are self-consistent and everything
650Sstevel@tonic-gate  * works, even the case of calling exit() from functions called
660Sstevel@tonic-gate  * by _exithandle() (recursive exit()).  All that is required is
670Sstevel@tonic-gate  * that the registered functions actually return (no longjmp()s).
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Because exitfns_lock is declared to be a recursive mutex, we
705891Sraf  * cannot use it with lmutex_lock()/lmutex_unlock() and we must
715891Sraf  * use mutex_lock()/mutex_unlock().  This means that atexit()
725891Sraf  * and exit() are not async-signal-safe.  We make them fork1-safe
730Sstevel@tonic-gate  * via the atexit_locks()/atexit_unlocks() functions, called from
740Sstevel@tonic-gate  * libc_prepare_atfork()/libc_child_atfork()/libc_parent_atfork()
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * atexit_locks() and atexit_unlocks() are called on every link map.
790Sstevel@tonic-gate  * Do not use curthread->ul_uberdata->atexit_root for these.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate void
820Sstevel@tonic-gate atexit_locks()
830Sstevel@tonic-gate {
846515Sraf 	(void) mutex_lock(&__uberdata.atexit_root.exitfns_lock);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate void
880Sstevel@tonic-gate atexit_unlocks()
890Sstevel@tonic-gate {
906515Sraf 	(void) mutex_unlock(&__uberdata.atexit_root.exitfns_lock);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * atexit() is called before the primordial thread is fully set up.
950Sstevel@tonic-gate  * Be careful about dereferencing self->ul_uberdata->atexit_root.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate int
98*6812Sraf atexit(void (*func)(void))
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	ulwp_t *self;
1010Sstevel@tonic-gate 	atexit_root_t *arp;
1020Sstevel@tonic-gate 	_exthdlr_t *p;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if ((p = lmalloc(sizeof (_exthdlr_t))) == NULL)
1050Sstevel@tonic-gate 		return (-1);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if ((self = __curthread()) == NULL)
1080Sstevel@tonic-gate 		arp = &__uberdata.atexit_root;
1090Sstevel@tonic-gate 	else {
1100Sstevel@tonic-gate 		arp = &self->ul_uberdata->atexit_root;
1116515Sraf 		(void) mutex_lock(&arp->exitfns_lock);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	p->hdlr = func;
1140Sstevel@tonic-gate 	p->next = arp->head;
1150Sstevel@tonic-gate 	arp->head = p;
1160Sstevel@tonic-gate 	if (self != NULL)
1176515Sraf 		(void) mutex_unlock(&arp->exitfns_lock);
1180Sstevel@tonic-gate 	return (0);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate void
1220Sstevel@tonic-gate _exithandle(void)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
1250Sstevel@tonic-gate 	_exthdlr_t *p;
1260Sstevel@tonic-gate 
1276515Sraf 	(void) mutex_lock(&arp->exitfns_lock);
1280Sstevel@tonic-gate 	arp->exit_frame_monitor = _getfp() + STACK_BIAS;
1290Sstevel@tonic-gate 	p = arp->head;
1300Sstevel@tonic-gate 	while (p != NULL) {
1310Sstevel@tonic-gate 		arp->head = p->next;
1320Sstevel@tonic-gate 		p->hdlr();
1330Sstevel@tonic-gate 		lfree(p, sizeof (_exthdlr_t));
1340Sstevel@tonic-gate 		p = arp->head;
1350Sstevel@tonic-gate 	}
1366515Sraf 	(void) mutex_unlock(&arp->exitfns_lock);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * _get_exit_frame_monitor is called by the C++ runtimes.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate void *
1430Sstevel@tonic-gate _get_exit_frame_monitor(void)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
1460Sstevel@tonic-gate 	return (&arp->exit_frame_monitor);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
1510Sstevel@tonic-gate  * processes a dlclose call on an object.  It resets all signal handlers
1520Sstevel@tonic-gate  * which fall within the union of the ranges specified by the elements
1530Sstevel@tonic-gate  * of the array range to SIG_DFL.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate static void
1560Sstevel@tonic-gate _preexec_sig_unload(Lc_addr_range_t range[], uint_t count)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
1590Sstevel@tonic-gate 	int sig;
1604570Sraf 	rwlock_t *rwlp;
1610Sstevel@tonic-gate 	struct sigaction *sap;
1620Sstevel@tonic-gate 	struct sigaction oact;
1630Sstevel@tonic-gate 	void (*handler)();
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	for (sig = 1; sig < NSIG; sig++) {
1660Sstevel@tonic-gate 		sap = (struct sigaction *)&udp->siguaction[sig].sig_uaction;
1670Sstevel@tonic-gate again:
1680Sstevel@tonic-gate 		handler = sap->sa_handler;
1690Sstevel@tonic-gate 		if (handler != SIG_DFL && handler != SIG_IGN &&
1700Sstevel@tonic-gate 		    in_range(handler, range, count)) {
1714570Sraf 			rwlp = &udp->siguaction[sig].sig_lock;
1724570Sraf 			lrw_wrlock(rwlp);
1730Sstevel@tonic-gate 			if (handler != sap->sa_handler) {
1744570Sraf 				lrw_unlock(rwlp);
1750Sstevel@tonic-gate 				goto again;
1760Sstevel@tonic-gate 			}
1770Sstevel@tonic-gate 			sap->sa_handler = SIG_DFL;
1780Sstevel@tonic-gate 			sap->sa_flags = SA_SIGINFO;
1790Sstevel@tonic-gate 			(void) sigemptyset(&sap->sa_mask);
1800Sstevel@tonic-gate 			if (__sigaction(sig, NULL, &oact) == 0 &&
1810Sstevel@tonic-gate 			    oact.sa_handler != SIG_DFL &&
1820Sstevel@tonic-gate 			    oact.sa_handler != SIG_IGN)
1830Sstevel@tonic-gate 				(void) __sigaction(sig, sap, NULL);
1844570Sraf 			lrw_unlock(rwlp);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
1910Sstevel@tonic-gate  * processes a dlclose call on an object.  It cancels all atfork() entries
1920Sstevel@tonic-gate  * whose prefork, parent postfork, or child postfork functions fall within
1930Sstevel@tonic-gate  * the union of the ranges specified by the elements of the array range.
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate static void
1960Sstevel@tonic-gate _preexec_atfork_unload(Lc_addr_range_t range[], uint_t count)
1970Sstevel@tonic-gate {
1984843Sraf 	ulwp_t *self = curthread;
1994843Sraf 	uberdata_t *udp = self->ul_uberdata;
2000Sstevel@tonic-gate 	atfork_t *atfork_q;
2010Sstevel@tonic-gate 	atfork_t *atfp;
2020Sstevel@tonic-gate 	atfork_t *next;
2030Sstevel@tonic-gate 	void (*func)(void);
2040Sstevel@tonic-gate 	int start_again;
2050Sstevel@tonic-gate 
2066515Sraf 	(void) mutex_lock(&udp->atfork_lock);
2070Sstevel@tonic-gate 	if ((atfork_q = udp->atforklist) != NULL) {
2080Sstevel@tonic-gate 		atfp = atfork_q;
2090Sstevel@tonic-gate 		do {
2100Sstevel@tonic-gate 			next = atfp->forw;
2110Sstevel@tonic-gate 			start_again = 0;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 			if (((func = atfp->prepare) != NULL &&
2140Sstevel@tonic-gate 			    in_range(func, range, count)) ||
2150Sstevel@tonic-gate 			    ((func = atfp->parent) != NULL &&
2160Sstevel@tonic-gate 			    in_range(func, range, count)) ||
2170Sstevel@tonic-gate 			    ((func = atfp->child) != NULL &&
2180Sstevel@tonic-gate 			    in_range(func, range, count))) {
2194843Sraf 				if (self->ul_fork) {
2200Sstevel@tonic-gate 					/*
2210Sstevel@tonic-gate 					 * dlclose() called from a fork handler.
2220Sstevel@tonic-gate 					 * Deleting the entry would wreak havoc.
2230Sstevel@tonic-gate 					 * Just null out the function pointers
2240Sstevel@tonic-gate 					 * and leave the entry in place.
2250Sstevel@tonic-gate 					 */
2260Sstevel@tonic-gate 					atfp->prepare = NULL;
2270Sstevel@tonic-gate 					atfp->parent = NULL;
2280Sstevel@tonic-gate 					atfp->child = NULL;
2290Sstevel@tonic-gate 					continue;
2300Sstevel@tonic-gate 				}
2310Sstevel@tonic-gate 				if (atfp == atfork_q) {
2320Sstevel@tonic-gate 					/* deleting the list head member */
2330Sstevel@tonic-gate 					udp->atforklist = atfork_q = next;
2340Sstevel@tonic-gate 					start_again = 1;
2350Sstevel@tonic-gate 				}
2360Sstevel@tonic-gate 				atfp->forw->back = atfp->back;
2370Sstevel@tonic-gate 				atfp->back->forw = atfp->forw;
2380Sstevel@tonic-gate 				lfree(atfp, sizeof (atfork_t));
2390Sstevel@tonic-gate 				if (atfp == atfork_q) {
2400Sstevel@tonic-gate 					/* we deleted the whole list */
2410Sstevel@tonic-gate 					udp->atforklist = NULL;
2420Sstevel@tonic-gate 					break;
2430Sstevel@tonic-gate 				}
2440Sstevel@tonic-gate 			}
2450Sstevel@tonic-gate 		} while ((atfp = next) != atfork_q || start_again);
2460Sstevel@tonic-gate 	}
2476515Sraf 	(void) mutex_unlock(&udp->atfork_lock);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
2520Sstevel@tonic-gate  * processes a dlclose call on an object.  It sets the destructor
2530Sstevel@tonic-gate  * function pointer to NULL for all keys whose destructors fall within
2540Sstevel@tonic-gate  * the union of the ranges specified by the elements of the array range.
2550Sstevel@tonic-gate  * We don't assign TSD_UNALLOCATED (the equivalent of pthread_key_destroy())
2560Sstevel@tonic-gate  * because the thread may use the key's TSD further on in fini processing.
2570Sstevel@tonic-gate  */
2580Sstevel@tonic-gate static void
2590Sstevel@tonic-gate _preexec_tsd_unload(Lc_addr_range_t range[], uint_t count)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata;
2620Sstevel@tonic-gate 	void (*func)(void *);
2630Sstevel@tonic-gate 	int key;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	lmutex_lock(&tsdm->tsdm_lock);
2660Sstevel@tonic-gate 	for (key = 1; key < tsdm->tsdm_nused; key++) {
2670Sstevel@tonic-gate 		if ((func = tsdm->tsdm_destro[key]) != NULL &&
2680Sstevel@tonic-gate 		    func != TSD_UNALLOCATED &&
2690Sstevel@tonic-gate 		    in_range((_exithdlr_func_t)func, range, count))
2700Sstevel@tonic-gate 			tsdm->tsdm_destro[key] = NULL;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	lmutex_unlock(&tsdm->tsdm_lock);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * The following is a routine which the loader (ld.so.1) calls when it
2770Sstevel@tonic-gate  * processes dlclose calls on objects with atexit registrations.  It
2780Sstevel@tonic-gate  * executes the exit handlers that fall within the union of the ranges
2790Sstevel@tonic-gate  * specified by the elements of the array range in the REVERSE ORDER of
2800Sstevel@tonic-gate  * their registration.  Do not change this characteristic; it is REQUIRED
2810Sstevel@tonic-gate  * BEHAVIOR.
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate int
2840Sstevel@tonic-gate _preexec_exit_handlers(Lc_addr_range_t range[], uint_t count)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	atexit_root_t *arp = &curthread->ul_uberdata->atexit_root;
2870Sstevel@tonic-gate 	_exthdlr_t *o;		/* previous node */
2880Sstevel@tonic-gate 	_exthdlr_t *p;		/* this node */
2890Sstevel@tonic-gate 
2906515Sraf 	(void) mutex_lock(&arp->exitfns_lock);
2910Sstevel@tonic-gate 	o = NULL;
2920Sstevel@tonic-gate 	p = arp->head;
2930Sstevel@tonic-gate 	while (p != NULL) {
2940Sstevel@tonic-gate 		if (in_range(p->hdlr, range, count)) {
2950Sstevel@tonic-gate 			/* We need to execute this one */
2960Sstevel@tonic-gate 			if (o != NULL)
2970Sstevel@tonic-gate 				o->next = p->next;
2980Sstevel@tonic-gate 			else
2990Sstevel@tonic-gate 				arp->head = p->next;
3000Sstevel@tonic-gate 			p->hdlr();
3010Sstevel@tonic-gate 			lfree(p, sizeof (_exthdlr_t));
3020Sstevel@tonic-gate 			o = NULL;
3030Sstevel@tonic-gate 			p = arp->head;
3040Sstevel@tonic-gate 		} else {
3050Sstevel@tonic-gate 			o = p;
3060Sstevel@tonic-gate 			p = p->next;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 	}
3096515Sraf 	(void) mutex_unlock(&arp->exitfns_lock);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	_preexec_tsd_unload(range, count);
3120Sstevel@tonic-gate 	_preexec_atfork_unload(range, count);
3130Sstevel@tonic-gate 	_preexec_sig_unload(range, count);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	return (0);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate static int
3190Sstevel@tonic-gate in_range(_exithdlr_func_t addr, Lc_addr_range_t ranges[], uint_t count)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	uint_t idx;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	for (idx = 0; idx < count; idx++) {
3240Sstevel@tonic-gate 		if ((void *)addr >= ranges[idx].lb &&
3250Sstevel@tonic-gate 		    (void *)addr < ranges[idx].ub) {
3260Sstevel@tonic-gate 			return (1);
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	return (0);
3310Sstevel@tonic-gate }
332