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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*1219Sraf 230Sstevel@tonic-gate /* 24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 310Sstevel@tonic-gate /* All Rights Reserved */ 320Sstevel@tonic-gate 33*1219Sraf #pragma weak atexit = _atexit 340Sstevel@tonic-gate 35*1219Sraf #include "synonyms.h" 360Sstevel@tonic-gate #include "thr_uberdata.h" 370Sstevel@tonic-gate #include "libc_int.h" 380Sstevel@tonic-gate #include "atexit.h" 390Sstevel@tonic-gate #include "stdiom.h" 400Sstevel@tonic-gate 410Sstevel@tonic-gate /* 420Sstevel@tonic-gate * Note that memory is managed by lmalloc()/lfree(). 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * Among other reasons, this is occasioned by the insistence of our 450Sstevel@tonic-gate * brothers sh(1) and csh(1) that they can do malloc, etc., better than 460Sstevel@tonic-gate * libc can. Those programs define their own malloc routines, and 470Sstevel@tonic-gate * initialize the underlying mechanism in main(). This means that calls 480Sstevel@tonic-gate * to malloc occuring before main will crash. The loader calls atexit(3C) 490Sstevel@tonic-gate * before calling main, so we'd better avoid malloc() when it does. 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * Another reason for using lmalloc()/lfree() is that the atexit() 520Sstevel@tonic-gate * list must transcend all link maps. See the Linker and Libraries 530Sstevel@tonic-gate * Guide for information on alternate link maps. 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * See "thr_uberdata.h" for the definitions of structures used here. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate 580Sstevel@tonic-gate static int in_range(_exithdlr_func_t, Lc_addr_range_t[], uint_t count); 590Sstevel@tonic-gate 600Sstevel@tonic-gate extern caddr_t _getfp(void); 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * exitfns_lock is declared to be a recursive mutex so that we 640Sstevel@tonic-gate * can hold it while calling out to the registered functions. 650Sstevel@tonic-gate * If they call back to us, we are self-consistent and everything 660Sstevel@tonic-gate * works, even the case of calling exit() from functions called 670Sstevel@tonic-gate * by _exithandle() (recursive exit()). All that is required is 680Sstevel@tonic-gate * that the registered functions actually return (no longjmp()s). 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * Because exitfns_lock is declared to be a recursive mutex, we 710Sstevel@tonic-gate * cannot use it with lmutex_lock()/lmutex_unlock() and we must use 720Sstevel@tonic-gate * rmutex_lock()/rmutex_unlock() (which are defined to be simply 730Sstevel@tonic-gate * mutex_lock()/mutex_unlock()). This means that atexit() and 740Sstevel@tonic-gate * exit() are not async-signal-safe. We make them fork1-safe 750Sstevel@tonic-gate * via the atexit_locks()/atexit_unlocks() functions, called from 760Sstevel@tonic-gate * libc_prepare_atfork()/libc_child_atfork()/libc_parent_atfork() 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* 800Sstevel@tonic-gate * atexit_locks() and atexit_unlocks() are called on every link map. 810Sstevel@tonic-gate * Do not use curthread->ul_uberdata->atexit_root for these. 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate void 840Sstevel@tonic-gate atexit_locks() 850Sstevel@tonic-gate { 860Sstevel@tonic-gate (void) rmutex_lock(&__uberdata.atexit_root.exitfns_lock); 870Sstevel@tonic-gate } 880Sstevel@tonic-gate 890Sstevel@tonic-gate void 900Sstevel@tonic-gate atexit_unlocks() 910Sstevel@tonic-gate { 920Sstevel@tonic-gate (void) rmutex_unlock(&__uberdata.atexit_root.exitfns_lock); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * atexit() is called before the primordial thread is fully set up. 970Sstevel@tonic-gate * Be careful about dereferencing self->ul_uberdata->atexit_root. 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate int 1000Sstevel@tonic-gate _atexit(void (*func)(void)) 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate ulwp_t *self; 1030Sstevel@tonic-gate atexit_root_t *arp; 1040Sstevel@tonic-gate _exthdlr_t *p; 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate if ((p = lmalloc(sizeof (_exthdlr_t))) == NULL) 1070Sstevel@tonic-gate return (-1); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if ((self = __curthread()) == NULL) 1100Sstevel@tonic-gate arp = &__uberdata.atexit_root; 1110Sstevel@tonic-gate else { 1120Sstevel@tonic-gate arp = &self->ul_uberdata->atexit_root; 1130Sstevel@tonic-gate (void) rmutex_lock(&arp->exitfns_lock); 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate p->hdlr = func; 1160Sstevel@tonic-gate p->next = arp->head; 1170Sstevel@tonic-gate arp->head = p; 1180Sstevel@tonic-gate if (self != NULL) 1190Sstevel@tonic-gate (void) rmutex_unlock(&arp->exitfns_lock); 1200Sstevel@tonic-gate return (0); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate void 1240Sstevel@tonic-gate _exithandle(void) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate atexit_root_t *arp = &curthread->ul_uberdata->atexit_root; 1270Sstevel@tonic-gate _exthdlr_t *p; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate (void) rmutex_lock(&arp->exitfns_lock); 1300Sstevel@tonic-gate arp->exit_frame_monitor = _getfp() + STACK_BIAS; 1310Sstevel@tonic-gate p = arp->head; 1320Sstevel@tonic-gate while (p != NULL) { 1330Sstevel@tonic-gate arp->head = p->next; 1340Sstevel@tonic-gate p->hdlr(); 1350Sstevel@tonic-gate lfree(p, sizeof (_exthdlr_t)); 1360Sstevel@tonic-gate p = arp->head; 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate (void) rmutex_unlock(&arp->exitfns_lock); 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 * 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 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; 1620Sstevel@tonic-gate mutex_t *mp; 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)) { 1730Sstevel@tonic-gate mp = &udp->siguaction[sig].sig_lock; 1740Sstevel@tonic-gate lmutex_lock(mp); 1750Sstevel@tonic-gate if (handler != sap->sa_handler) { 1760Sstevel@tonic-gate lmutex_unlock(mp); 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); 1860Sstevel@tonic-gate lmutex_unlock(mp); 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 1980Sstevel@tonic-gate _preexec_atfork_unload(Lc_addr_range_t range[], uint_t count) 1990Sstevel@tonic-gate { 2000Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 2010Sstevel@tonic-gate atfork_t *atfork_q; 2020Sstevel@tonic-gate atfork_t *atfp; 2030Sstevel@tonic-gate atfork_t *next; 2040Sstevel@tonic-gate void (*func)(void); 2050Sstevel@tonic-gate int start_again; 2060Sstevel@tonic-gate int error; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate error = fork_lock_enter(NULL); 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))) { 2210Sstevel@tonic-gate if (error) { 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 } 2490Sstevel@tonic-gate fork_lock_exit(); 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 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 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 */ 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate (void) rmutex_lock(&arp->exitfns_lock); 2930Sstevel@tonic-gate o = NULL; 2940Sstevel@tonic-gate p = arp->head; 2950Sstevel@tonic-gate while (p != NULL) { 2960Sstevel@tonic-gate if (in_range(p->hdlr, range, count)) { 2970Sstevel@tonic-gate /* We need to execute this one */ 2980Sstevel@tonic-gate if (o != NULL) 2990Sstevel@tonic-gate o->next = p->next; 3000Sstevel@tonic-gate else 3010Sstevel@tonic-gate arp->head = p->next; 3020Sstevel@tonic-gate p->hdlr(); 3030Sstevel@tonic-gate lfree(p, sizeof (_exthdlr_t)); 3040Sstevel@tonic-gate o = NULL; 3050Sstevel@tonic-gate p = arp->head; 3060Sstevel@tonic-gate } else { 3070Sstevel@tonic-gate o = p; 3080Sstevel@tonic-gate p = p->next; 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate (void) rmutex_unlock(&arp->exitfns_lock); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate _preexec_tsd_unload(range, count); 3140Sstevel@tonic-gate _preexec_atfork_unload(range, count); 3150Sstevel@tonic-gate _preexec_sig_unload(range, count); 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate return (0); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate static int 3210Sstevel@tonic-gate in_range(_exithdlr_func_t addr, Lc_addr_range_t ranges[], uint_t count) 3220Sstevel@tonic-gate { 3230Sstevel@tonic-gate uint_t idx; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate for (idx = 0; idx < count; idx++) { 3260Sstevel@tonic-gate if ((void *)addr >= ranges[idx].lb && 3270Sstevel@tonic-gate (void *)addr < ranges[idx].ub) { 3280Sstevel@tonic-gate return (1); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate return (0); 3330Sstevel@tonic-gate } 334