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 5*4570Sraf * Common Development and Distribution License (the "License"). 6*4570Sraf * 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 /* 23*4570Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 300Sstevel@tonic-gate /* All Rights Reserved */ 310Sstevel@tonic-gate 321219Sraf #pragma weak atexit = _atexit 330Sstevel@tonic-gate 341219Sraf #include "synonyms.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 700Sstevel@tonic-gate * cannot use it with lmutex_lock()/lmutex_unlock() and we must use 710Sstevel@tonic-gate * rmutex_lock()/rmutex_unlock() (which are defined to be simply 720Sstevel@tonic-gate * mutex_lock()/mutex_unlock()). This means that atexit() and 730Sstevel@tonic-gate * exit() are not async-signal-safe. We make them fork1-safe 740Sstevel@tonic-gate * via the atexit_locks()/atexit_unlocks() functions, called from 750Sstevel@tonic-gate * libc_prepare_atfork()/libc_child_atfork()/libc_parent_atfork() 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * atexit_locks() and atexit_unlocks() are called on every link map. 800Sstevel@tonic-gate * Do not use curthread->ul_uberdata->atexit_root for these. 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate void 830Sstevel@tonic-gate atexit_locks() 840Sstevel@tonic-gate { 850Sstevel@tonic-gate (void) rmutex_lock(&__uberdata.atexit_root.exitfns_lock); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate void 890Sstevel@tonic-gate atexit_unlocks() 900Sstevel@tonic-gate { 910Sstevel@tonic-gate (void) rmutex_unlock(&__uberdata.atexit_root.exitfns_lock); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* 950Sstevel@tonic-gate * atexit() is called before the primordial thread is fully set up. 960Sstevel@tonic-gate * Be careful about dereferencing self->ul_uberdata->atexit_root. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate int 990Sstevel@tonic-gate _atexit(void (*func)(void)) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate ulwp_t *self; 1020Sstevel@tonic-gate atexit_root_t *arp; 1030Sstevel@tonic-gate _exthdlr_t *p; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if ((p = lmalloc(sizeof (_exthdlr_t))) == NULL) 1060Sstevel@tonic-gate return (-1); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if ((self = __curthread()) == NULL) 1090Sstevel@tonic-gate arp = &__uberdata.atexit_root; 1100Sstevel@tonic-gate else { 1110Sstevel@tonic-gate arp = &self->ul_uberdata->atexit_root; 1120Sstevel@tonic-gate (void) rmutex_lock(&arp->exitfns_lock); 1130Sstevel@tonic-gate } 1140Sstevel@tonic-gate p->hdlr = func; 1150Sstevel@tonic-gate p->next = arp->head; 1160Sstevel@tonic-gate arp->head = p; 1170Sstevel@tonic-gate if (self != NULL) 1180Sstevel@tonic-gate (void) rmutex_unlock(&arp->exitfns_lock); 1190Sstevel@tonic-gate return (0); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate void 1230Sstevel@tonic-gate _exithandle(void) 1240Sstevel@tonic-gate { 1250Sstevel@tonic-gate atexit_root_t *arp = &curthread->ul_uberdata->atexit_root; 1260Sstevel@tonic-gate _exthdlr_t *p; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate (void) rmutex_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 } 1370Sstevel@tonic-gate (void) rmutex_unlock(&arp->exitfns_lock); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* 1410Sstevel@tonic-gate * _get_exit_frame_monitor is called by the C++ runtimes. 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate void * 1440Sstevel@tonic-gate _get_exit_frame_monitor(void) 1450Sstevel@tonic-gate { 1460Sstevel@tonic-gate atexit_root_t *arp = &curthread->ul_uberdata->atexit_root; 1470Sstevel@tonic-gate return (&arp->exit_frame_monitor); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * The following is a routine which the loader (ld.so.1) calls when it 1520Sstevel@tonic-gate * processes a dlclose call on an object. It resets all signal handlers 1530Sstevel@tonic-gate * which fall within the union of the ranges specified by the elements 1540Sstevel@tonic-gate * of the array range to SIG_DFL. 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate static void 1570Sstevel@tonic-gate _preexec_sig_unload(Lc_addr_range_t range[], uint_t count) 1580Sstevel@tonic-gate { 1590Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 1600Sstevel@tonic-gate int sig; 161*4570Sraf rwlock_t *rwlp; 1620Sstevel@tonic-gate struct sigaction *sap; 1630Sstevel@tonic-gate struct sigaction oact; 1640Sstevel@tonic-gate void (*handler)(); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate for (sig = 1; sig < NSIG; sig++) { 1670Sstevel@tonic-gate sap = (struct sigaction *)&udp->siguaction[sig].sig_uaction; 1680Sstevel@tonic-gate again: 1690Sstevel@tonic-gate handler = sap->sa_handler; 1700Sstevel@tonic-gate if (handler != SIG_DFL && handler != SIG_IGN && 1710Sstevel@tonic-gate in_range(handler, range, count)) { 172*4570Sraf rwlp = &udp->siguaction[sig].sig_lock; 173*4570Sraf lrw_wrlock(rwlp); 1740Sstevel@tonic-gate if (handler != sap->sa_handler) { 175*4570Sraf lrw_unlock(rwlp); 1760Sstevel@tonic-gate goto again; 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate sap->sa_handler = SIG_DFL; 1790Sstevel@tonic-gate sap->sa_flags = SA_SIGINFO; 1800Sstevel@tonic-gate (void) sigemptyset(&sap->sa_mask); 1810Sstevel@tonic-gate if (__sigaction(sig, NULL, &oact) == 0 && 1820Sstevel@tonic-gate oact.sa_handler != SIG_DFL && 1830Sstevel@tonic-gate oact.sa_handler != SIG_IGN) 1840Sstevel@tonic-gate (void) __sigaction(sig, sap, NULL); 185*4570Sraf lrw_unlock(rwlp); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * The following is a routine which the loader (ld.so.1) calls when it 1920Sstevel@tonic-gate * processes a dlclose call on an object. It cancels all atfork() entries 1930Sstevel@tonic-gate * whose prefork, parent postfork, or child postfork functions fall within 1940Sstevel@tonic-gate * the union of the ranges specified by the elements of the array range. 1950Sstevel@tonic-gate */ 1960Sstevel@tonic-gate static void 1970Sstevel@tonic-gate _preexec_atfork_unload(Lc_addr_range_t range[], uint_t count) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate uberdata_t *udp = curthread->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 int error; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate error = fork_lock_enter(NULL); 2080Sstevel@tonic-gate if ((atfork_q = udp->atforklist) != NULL) { 2090Sstevel@tonic-gate atfp = atfork_q; 2100Sstevel@tonic-gate do { 2110Sstevel@tonic-gate next = atfp->forw; 2120Sstevel@tonic-gate start_again = 0; 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate if (((func = atfp->prepare) != NULL && 2150Sstevel@tonic-gate in_range(func, range, count)) || 2160Sstevel@tonic-gate ((func = atfp->parent) != NULL && 2170Sstevel@tonic-gate in_range(func, range, count)) || 2180Sstevel@tonic-gate ((func = atfp->child) != NULL && 2190Sstevel@tonic-gate in_range(func, range, count))) { 2200Sstevel@tonic-gate if (error) { 2210Sstevel@tonic-gate /* 2220Sstevel@tonic-gate * dlclose() called from a fork handler. 2230Sstevel@tonic-gate * Deleting the entry would wreak havoc. 2240Sstevel@tonic-gate * Just null out the function pointers 2250Sstevel@tonic-gate * and leave the entry in place. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate atfp->prepare = NULL; 2280Sstevel@tonic-gate atfp->parent = NULL; 2290Sstevel@tonic-gate atfp->child = NULL; 2300Sstevel@tonic-gate continue; 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate if (atfp == atfork_q) { 2330Sstevel@tonic-gate /* deleting the list head member */ 2340Sstevel@tonic-gate udp->atforklist = atfork_q = next; 2350Sstevel@tonic-gate start_again = 1; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate atfp->forw->back = atfp->back; 2380Sstevel@tonic-gate atfp->back->forw = atfp->forw; 2390Sstevel@tonic-gate lfree(atfp, sizeof (atfork_t)); 2400Sstevel@tonic-gate if (atfp == atfork_q) { 2410Sstevel@tonic-gate /* we deleted the whole list */ 2420Sstevel@tonic-gate udp->atforklist = NULL; 2430Sstevel@tonic-gate break; 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate } while ((atfp = next) != atfork_q || start_again); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate fork_lock_exit(); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * The following is a routine which the loader (ld.so.1) calls when it 2530Sstevel@tonic-gate * processes a dlclose call on an object. It sets the destructor 2540Sstevel@tonic-gate * function pointer to NULL for all keys whose destructors fall within 2550Sstevel@tonic-gate * the union of the ranges specified by the elements of the array range. 2560Sstevel@tonic-gate * We don't assign TSD_UNALLOCATED (the equivalent of pthread_key_destroy()) 2570Sstevel@tonic-gate * because the thread may use the key's TSD further on in fini processing. 2580Sstevel@tonic-gate */ 2590Sstevel@tonic-gate static void 2600Sstevel@tonic-gate _preexec_tsd_unload(Lc_addr_range_t range[], uint_t count) 2610Sstevel@tonic-gate { 2620Sstevel@tonic-gate tsd_metadata_t *tsdm = &curthread->ul_uberdata->tsd_metadata; 2630Sstevel@tonic-gate void (*func)(void *); 2640Sstevel@tonic-gate int key; 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate lmutex_lock(&tsdm->tsdm_lock); 2670Sstevel@tonic-gate for (key = 1; key < tsdm->tsdm_nused; key++) { 2680Sstevel@tonic-gate if ((func = tsdm->tsdm_destro[key]) != NULL && 2690Sstevel@tonic-gate func != TSD_UNALLOCATED && 2700Sstevel@tonic-gate in_range((_exithdlr_func_t)func, range, count)) 2710Sstevel@tonic-gate tsdm->tsdm_destro[key] = NULL; 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate lmutex_unlock(&tsdm->tsdm_lock); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * The following is a routine which the loader (ld.so.1) calls when it 2780Sstevel@tonic-gate * processes dlclose calls on objects with atexit registrations. It 2790Sstevel@tonic-gate * executes the exit handlers that fall within the union of the ranges 2800Sstevel@tonic-gate * specified by the elements of the array range in the REVERSE ORDER of 2810Sstevel@tonic-gate * their registration. Do not change this characteristic; it is REQUIRED 2820Sstevel@tonic-gate * BEHAVIOR. 2830Sstevel@tonic-gate */ 2840Sstevel@tonic-gate int 2850Sstevel@tonic-gate _preexec_exit_handlers(Lc_addr_range_t range[], uint_t count) 2860Sstevel@tonic-gate { 2870Sstevel@tonic-gate atexit_root_t *arp = &curthread->ul_uberdata->atexit_root; 2880Sstevel@tonic-gate _exthdlr_t *o; /* previous node */ 2890Sstevel@tonic-gate _exthdlr_t *p; /* this node */ 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate (void) rmutex_lock(&arp->exitfns_lock); 2920Sstevel@tonic-gate o = NULL; 2930Sstevel@tonic-gate p = arp->head; 2940Sstevel@tonic-gate while (p != NULL) { 2950Sstevel@tonic-gate if (in_range(p->hdlr, range, count)) { 2960Sstevel@tonic-gate /* We need to execute this one */ 2970Sstevel@tonic-gate if (o != NULL) 2980Sstevel@tonic-gate o->next = p->next; 2990Sstevel@tonic-gate else 3000Sstevel@tonic-gate arp->head = p->next; 3010Sstevel@tonic-gate p->hdlr(); 3020Sstevel@tonic-gate lfree(p, sizeof (_exthdlr_t)); 3030Sstevel@tonic-gate o = NULL; 3040Sstevel@tonic-gate p = arp->head; 3050Sstevel@tonic-gate } else { 3060Sstevel@tonic-gate o = p; 3070Sstevel@tonic-gate p = p->next; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate (void) rmutex_unlock(&arp->exitfns_lock); 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate _preexec_tsd_unload(range, count); 3130Sstevel@tonic-gate _preexec_atfork_unload(range, count); 3140Sstevel@tonic-gate _preexec_sig_unload(range, count); 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate return (0); 3170Sstevel@tonic-gate } 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate static int 3200Sstevel@tonic-gate in_range(_exithdlr_func_t addr, Lc_addr_range_t ranges[], uint_t count) 3210Sstevel@tonic-gate { 3220Sstevel@tonic-gate uint_t idx; 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate for (idx = 0; idx < count; idx++) { 3250Sstevel@tonic-gate if ((void *)addr >= ranges[idx].lb && 3260Sstevel@tonic-gate (void *)addr < ranges[idx].ub) { 3270Sstevel@tonic-gate return (1); 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate return (0); 3320Sstevel@tonic-gate } 333