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*1111Sraf 230Sstevel@tonic-gate /* 240Sstevel@tonic-gate * Copyright 2005 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 #include "lint.h" 310Sstevel@tonic-gate #include "thr_uberdata.h" 320Sstevel@tonic-gate #include <signal.h> 330Sstevel@tonic-gate #include <siginfo.h> 340Sstevel@tonic-gate #include <ucontext.h> 350Sstevel@tonic-gate #include <sys/systm.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate const sigset_t maskset = {MASKSET0, MASKSET1, 0, 0}; /* maskable signals */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * Return true if the valid signal bits in both sets are the same. 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate int 430Sstevel@tonic-gate sigequalset(const sigset_t *s1, const sigset_t *s2) 440Sstevel@tonic-gate { 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * We only test valid signal bits, not rubbish following MAXSIG 470Sstevel@tonic-gate * (for speed). Algorithm: 480Sstevel@tonic-gate * if (s1 & fillset) == (s2 & fillset) then (s1 ^ s2) & fillset == 0 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate return (!((s1->__sigbits[0] ^ s2->__sigbits[0]) | 510Sstevel@tonic-gate ((s1->__sigbits[1] ^ s2->__sigbits[1]) & FILLSET1))); 520Sstevel@tonic-gate } 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * Common code for calling the user-specified signal handler. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate void 580Sstevel@tonic-gate call_user_handler(int sig, siginfo_t *sip, ucontext_t *ucp) 590Sstevel@tonic-gate { 600Sstevel@tonic-gate ulwp_t *self = curthread; 610Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 620Sstevel@tonic-gate struct sigaction uact; 630Sstevel@tonic-gate volatile struct sigaction *sap; 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * If we are taking a signal while parked or about to be parked 670Sstevel@tonic-gate * on __lwp_park() then remove ourself from the sleep queue so 680Sstevel@tonic-gate * that we can grab locks. The code in mutex_lock_queue() and 690Sstevel@tonic-gate * cond_wait_common() will detect this and deal with it when 700Sstevel@tonic-gate * __lwp_park() returns. 710Sstevel@tonic-gate */ 720Sstevel@tonic-gate unsleep_self(); 730Sstevel@tonic-gate set_parking_flag(self, 0); 740Sstevel@tonic-gate 750Sstevel@tonic-gate if (__td_event_report(self, TD_CATCHSIG, udp)) { 760Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_CATCHSIG; 770Sstevel@tonic-gate self->ul_td_evbuf.eventdata = (void *)(intptr_t)sig; 780Sstevel@tonic-gate tdb_event(TD_CATCHSIG, udp); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate /* 820Sstevel@tonic-gate * Get a self-consistent set of flags, handler, and mask 830Sstevel@tonic-gate * while holding the sig's sig_lock for the least possible time. 840Sstevel@tonic-gate * We must acquire the sig's sig_lock because some thread running 850Sstevel@tonic-gate * in sigaction() might be establishing a new signal handler. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * Locking exceptions: 880Sstevel@tonic-gate * No locking for a child of vfork(). 890Sstevel@tonic-gate * If the signal is SIGPROF with an si_code of PROF_SIG, 900Sstevel@tonic-gate * then we assume that this signal was generated by 910Sstevel@tonic-gate * setitimer(ITIMER_REALPROF) set up by the dbx collector. 920Sstevel@tonic-gate * If the signal is SIGEMT with an si_code of EMT_CPCOVF, 930Sstevel@tonic-gate * then we assume that the signal was generated by 940Sstevel@tonic-gate * a hardware performance counter overflow. 950Sstevel@tonic-gate * In these cases, assume that we need no locking. It is the 960Sstevel@tonic-gate * monitoring program's responsibility to ensure correctness. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate sap = &udp->siguaction[sig].sig_uaction; 990Sstevel@tonic-gate if (self->ul_vfork || 1000Sstevel@tonic-gate (sip != NULL && 1010Sstevel@tonic-gate ((sig == SIGPROF && sip->si_code == PROF_SIG) || 1020Sstevel@tonic-gate (sig == SIGEMT && sip->si_code == EMT_CPCOVF)))) { 1030Sstevel@tonic-gate /* we wish this assignment could be atomic */ 104*1111Sraf (void) _private_memcpy(&uact, (void *)sap, sizeof (uact)); 1050Sstevel@tonic-gate } else { 1060Sstevel@tonic-gate mutex_t *mp = &udp->siguaction[sig].sig_lock; 1070Sstevel@tonic-gate lmutex_lock(mp); 108*1111Sraf (void) _private_memcpy(&uact, (void *)sap, sizeof (uact)); 1090Sstevel@tonic-gate if (sig == SIGCANCEL && (sap->sa_flags & SA_RESETHAND)) 1100Sstevel@tonic-gate sap->sa_sigaction = SIG_DFL; 1110Sstevel@tonic-gate lmutex_unlock(mp); 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * Set the proper signal mask and call the user's signal handler. 1160Sstevel@tonic-gate * (We overrode the user-requested signal mask with maskset 1170Sstevel@tonic-gate * so we currently have all blockable signals blocked.) 1180Sstevel@tonic-gate * 1190Sstevel@tonic-gate * We would like to ASSERT() that the signal is not a member of the 1200Sstevel@tonic-gate * signal mask at the previous level (ucp->uc_sigmask) or the specified 1210Sstevel@tonic-gate * signal mask for sigsuspend() or pollsys() (self->ul_tmpmask) but 1220Sstevel@tonic-gate * /proc can override this via PCSSIG, so we don't bother. 1230Sstevel@tonic-gate * 1240Sstevel@tonic-gate * We would also like to ASSERT() that the signal mask at the previous 1250Sstevel@tonic-gate * level equals self->ul_sigmask (maskset for sigsuspend() / pollsys()), 1260Sstevel@tonic-gate * but /proc can change the thread's signal mask via PCSHOLD, so we 1270Sstevel@tonic-gate * don't bother with that either. 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate ASSERT(ucp->uc_flags & UC_SIGMASK); 1300Sstevel@tonic-gate if (self->ul_sigsuspend) { 1310Sstevel@tonic-gate ucp->uc_sigmask = self->ul_sigmask; 1320Sstevel@tonic-gate self->ul_sigsuspend = 0; 1330Sstevel@tonic-gate /* the sigsuspend() or pollsys() signal mask */ 1340Sstevel@tonic-gate sigorset(&uact.sa_mask, &self->ul_tmpmask); 1350Sstevel@tonic-gate } else { 1360Sstevel@tonic-gate /* the signal mask at the previous level */ 1370Sstevel@tonic-gate sigorset(&uact.sa_mask, &ucp->uc_sigmask); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate if (!(uact.sa_flags & SA_NODEFER)) /* add current signal */ 1400Sstevel@tonic-gate (void) _private_sigaddset(&uact.sa_mask, sig); 1410Sstevel@tonic-gate self->ul_sigmask = uact.sa_mask; 1420Sstevel@tonic-gate self->ul_siglink = ucp; 1430Sstevel@tonic-gate (void) __lwp_sigmask(SIG_SETMASK, &uact.sa_mask, NULL); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * If this thread has been sent SIGCANCEL from the kernel 1470Sstevel@tonic-gate * or from pthread_cancel(), it is being asked to exit. 1480Sstevel@tonic-gate * The kernel may send SIGCANCEL without a siginfo struct. 1490Sstevel@tonic-gate * If the SIGCANCEL is process-directed (from kill() or 1500Sstevel@tonic-gate * sigqueue()), treat it as an ordinary signal. 1510Sstevel@tonic-gate */ 1520Sstevel@tonic-gate if (sig == SIGCANCEL) { 1530Sstevel@tonic-gate if (sip == NULL || SI_FROMKERNEL(sip) || 1540Sstevel@tonic-gate sip->si_code == SI_LWP) { 1550Sstevel@tonic-gate do_sigcancel(); 1560Sstevel@tonic-gate goto out; 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate if (uact.sa_sigaction == SIG_DFL || 1590Sstevel@tonic-gate uact.sa_sigaction == SIG_IGN) 1600Sstevel@tonic-gate goto out; 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (!(uact.sa_flags & SA_SIGINFO)) 1640Sstevel@tonic-gate sip = NULL; 1650Sstevel@tonic-gate __sighndlr(sig, sip, ucp, uact.sa_sigaction); 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 1680Sstevel@tonic-gate /* 1690Sstevel@tonic-gate * If this is a floating point exception and the queue 1700Sstevel@tonic-gate * is non-empty, pop the top entry from the queue. This 1710Sstevel@tonic-gate * is to maintain expected behavior. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate if (sig == SIGFPE && ucp->uc_mcontext.fpregs.fpu_qcnt) { 1740Sstevel@tonic-gate fpregset_t *fp = &ucp->uc_mcontext.fpregs; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate if (--fp->fpu_qcnt > 0) { 1770Sstevel@tonic-gate unsigned char i; 1780Sstevel@tonic-gate struct fq *fqp; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate fqp = fp->fpu_q; 1810Sstevel@tonic-gate for (i = 0; i < fp->fpu_qcnt; i++) 1820Sstevel@tonic-gate fqp[i] = fqp[i+1]; 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate #endif /* sparc */ 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate out: 1880Sstevel@tonic-gate (void) _private_setcontext(ucp); 1890Sstevel@tonic-gate thr_panic("call_user_handler(): _setcontext() returned"); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * take_deferred_signal() is called when ul_critical and ul_sigdefer become 1940Sstevel@tonic-gate * zero and a deferred signal has been recorded on the current thread. 1950Sstevel@tonic-gate * We are out of the critical region and are ready to take a signal. 1960Sstevel@tonic-gate * The kernel has all signals blocked on this lwp, but our value of 1970Sstevel@tonic-gate * ul_sigmask is the correct signal mask for the previous context. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate void 2000Sstevel@tonic-gate take_deferred_signal(int sig) 2010Sstevel@tonic-gate { 2020Sstevel@tonic-gate ulwp_t *self = curthread; 2030Sstevel@tonic-gate siginfo_t siginfo; 2040Sstevel@tonic-gate siginfo_t *sip; 2050Sstevel@tonic-gate ucontext_t uc; 2060Sstevel@tonic-gate volatile int returning; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate ASSERT(self->ul_critical == 0); 2090Sstevel@tonic-gate ASSERT(self->ul_sigdefer == 0); 2100Sstevel@tonic-gate ASSERT(self->ul_cursig == 0); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate returning = 0; 2130Sstevel@tonic-gate uc.uc_flags = UC_ALL; 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * We call _private_getcontext (a libc-private synonym for 2160Sstevel@tonic-gate * _getcontext) rather than _getcontext because we need to 2170Sstevel@tonic-gate * avoid the dynamic linker and link auditing problems here. 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate (void) _private_getcontext(&uc); 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * If the application signal handler calls setcontext() on 2220Sstevel@tonic-gate * the ucontext we give it, it returns here, then we return. 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate if (returning) 2250Sstevel@tonic-gate return; 2260Sstevel@tonic-gate returning = 1; 2270Sstevel@tonic-gate ASSERT(sigequalset(&uc.uc_sigmask, &maskset)); 2280Sstevel@tonic-gate if (self->ul_siginfo.si_signo == 0) 2290Sstevel@tonic-gate sip = NULL; 2300Sstevel@tonic-gate else { 231*1111Sraf (void) _private_memcpy(&siginfo, 232*1111Sraf &self->ul_siginfo, sizeof (siginfo)); 2330Sstevel@tonic-gate sip = &siginfo; 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate uc.uc_sigmask = self->ul_sigmask; 2360Sstevel@tonic-gate call_user_handler(sig, sip, &uc); 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate void 2400Sstevel@tonic-gate sigacthandler(int sig, siginfo_t *sip, void *uvp) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate ucontext_t *ucp = uvp; 2430Sstevel@tonic-gate ulwp_t *self = curthread; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * Do this in case we took a signal while in a cancelable system call. 2470Sstevel@tonic-gate * It does no harm if we were not in such a system call. 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate self->ul_sp = 0; 2500Sstevel@tonic-gate if (sig != SIGCANCEL) 2510Sstevel@tonic-gate self->ul_cancel_async = self->ul_save_async; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * If we are not in a critical region and are 2550Sstevel@tonic-gate * not deferring signals, take the signal now. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate if ((self->ul_critical + self->ul_sigdefer) == 0) { 2580Sstevel@tonic-gate call_user_handler(sig, sip, ucp); 2590Sstevel@tonic-gate return; /* call_user_handler() cannot return */ 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate /* 2630Sstevel@tonic-gate * We are in a critical region or we are deferring signals. When 2640Sstevel@tonic-gate * we emerge from the region we will call take_deferred_signal(). 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate ASSERT(self->ul_cursig == 0); 2670Sstevel@tonic-gate self->ul_cursig = (char)sig; 2680Sstevel@tonic-gate if (sip != NULL) 269*1111Sraf (void) _private_memcpy(&self->ul_siginfo, 270*1111Sraf sip, sizeof (siginfo_t)); 2710Sstevel@tonic-gate else 2720Sstevel@tonic-gate self->ul_siginfo.si_signo = 0; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate /* 2750Sstevel@tonic-gate * Make sure that if we return to a call to __lwp_park() 2760Sstevel@tonic-gate * or ___lwp_cond_wait() that it returns right away 2770Sstevel@tonic-gate * (giving us a spurious wakeup but not a deadlock). 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate set_parking_flag(self, 0); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * Return to the previous context with all signals blocked. 2830Sstevel@tonic-gate * We will restore the signal mask in take_deferred_signal(). 2840Sstevel@tonic-gate * Note that we are calling the system call trap here, not 2850Sstevel@tonic-gate * the _setcontext() wrapper. We don't want to change the 2860Sstevel@tonic-gate * thread's ul_sigmask by this operation. 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate ucp->uc_sigmask = maskset; 2890Sstevel@tonic-gate (void) __setcontext_syscall(ucp); 2900Sstevel@tonic-gate thr_panic("sigacthandler(): __setcontext() returned"); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate #pragma weak sigaction = _sigaction 2940Sstevel@tonic-gate int 2950Sstevel@tonic-gate _sigaction(int sig, const struct sigaction *nact, struct sigaction *oact) 2960Sstevel@tonic-gate { 2970Sstevel@tonic-gate ulwp_t *self = curthread; 2980Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata; 2990Sstevel@tonic-gate struct sigaction oaction; 3000Sstevel@tonic-gate struct sigaction tact; 3010Sstevel@tonic-gate struct sigaction *tactp = NULL; 3020Sstevel@tonic-gate int rv; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate if (sig <= 0 || sig >= NSIG) { 3050Sstevel@tonic-gate errno = EINVAL; 3060Sstevel@tonic-gate return (-1); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate if (!self->ul_vfork) 3100Sstevel@tonic-gate lmutex_lock(&udp->siguaction[sig].sig_lock); 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate oaction = udp->siguaction[sig].sig_uaction; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (nact != NULL) { 3150Sstevel@tonic-gate tact = *nact; /* make a copy so we can modify it */ 3160Sstevel@tonic-gate tactp = &tact; 3170Sstevel@tonic-gate delete_reserved_signals(&tact.sa_mask); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate #if !defined(_LP64) 3200Sstevel@tonic-gate tact.sa_resv[0] = tact.sa_resv[1] = 0; /* cleanliness */ 3210Sstevel@tonic-gate #endif 3220Sstevel@tonic-gate /* 3230Sstevel@tonic-gate * To be compatible with the behavior of SunOS 4.x: 3240Sstevel@tonic-gate * If the new signal handler is SIG_IGN or SIG_DFL, do 3250Sstevel@tonic-gate * not change the signal's entry in the siguaction array. 3260Sstevel@tonic-gate * This allows a child of vfork(2) to set signal handlers 3270Sstevel@tonic-gate * to SIG_IGN or SIG_DFL without affecting the parent. 3280Sstevel@tonic-gate * 3290Sstevel@tonic-gate * This also covers a race condition with some thread 3300Sstevel@tonic-gate * setting the signal action to SIG_DFL or SIG_IGN 3310Sstevel@tonic-gate * when the thread has also received and deferred 3320Sstevel@tonic-gate * that signal. When the thread takes the deferred 3330Sstevel@tonic-gate * signal, even though it has set the action to SIG_DFL 3340Sstevel@tonic-gate * or SIG_IGN, it will execute the old signal handler 3350Sstevel@tonic-gate * anyway. This is an inherent signaling race condition 3360Sstevel@tonic-gate * and is not a bug. 3370Sstevel@tonic-gate * 3380Sstevel@tonic-gate * A child of vfork() is not allowed to change signal 3390Sstevel@tonic-gate * handlers to anything other than SIG_DFL or SIG_IGN. 3400Sstevel@tonic-gate */ 3410Sstevel@tonic-gate if (self->ul_vfork) { 3420Sstevel@tonic-gate if (tact.sa_sigaction != SIG_IGN) 3430Sstevel@tonic-gate tact.sa_sigaction = SIG_DFL; 3440Sstevel@tonic-gate } else if (sig == SIGCANCEL) { 3450Sstevel@tonic-gate /* 3460Sstevel@tonic-gate * Always catch SIGCANCEL. 3470Sstevel@tonic-gate * We need it for pthread_cancel() to work. 3480Sstevel@tonic-gate */ 3490Sstevel@tonic-gate udp->siguaction[sig].sig_uaction = tact; 3500Sstevel@tonic-gate if (tact.sa_sigaction == SIG_DFL || 3510Sstevel@tonic-gate tact.sa_sigaction == SIG_IGN) 3520Sstevel@tonic-gate tact.sa_flags = SA_SIGINFO; 3530Sstevel@tonic-gate else { 3540Sstevel@tonic-gate tact.sa_flags |= SA_SIGINFO; 3550Sstevel@tonic-gate tact.sa_flags &= ~(SA_NODEFER | SA_RESETHAND); 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate tact.sa_sigaction = udp->sigacthandler; 3580Sstevel@tonic-gate tact.sa_mask = maskset; 3590Sstevel@tonic-gate } else if (tact.sa_sigaction != SIG_DFL && 3600Sstevel@tonic-gate tact.sa_sigaction != SIG_IGN) { 3610Sstevel@tonic-gate udp->siguaction[sig].sig_uaction = tact; 3620Sstevel@tonic-gate tact.sa_flags &= ~SA_NODEFER; 3630Sstevel@tonic-gate tact.sa_sigaction = udp->sigacthandler; 3640Sstevel@tonic-gate tact.sa_mask = maskset; 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate if ((rv = __sigaction(sig, tactp, oact)) != 0) 3690Sstevel@tonic-gate udp->siguaction[sig].sig_uaction = oaction; 3700Sstevel@tonic-gate else if (oact != NULL && 3710Sstevel@tonic-gate oact->sa_sigaction != SIG_DFL && 3720Sstevel@tonic-gate oact->sa_sigaction != SIG_IGN) 3730Sstevel@tonic-gate *oact = oaction; 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate if (!self->ul_vfork) 3760Sstevel@tonic-gate lmutex_unlock(&udp->siguaction[sig].sig_lock); 3770Sstevel@tonic-gate return (rv); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * Calling set_parking_flag(curthread, 1) informs the kernel that we are 3820Sstevel@tonic-gate * calling __lwp_park or ___lwp_cond_wait(). If we take a signal in 3830Sstevel@tonic-gate * the unprotected (from signals) interval before reaching the kernel, 3840Sstevel@tonic-gate * sigacthandler() will call set_parking_flag(curthread, 0) to inform 3850Sstevel@tonic-gate * the kernel to return immediately from these system calls, giving us 3860Sstevel@tonic-gate * a spurious wakeup but not a deadlock. 3870Sstevel@tonic-gate */ 3880Sstevel@tonic-gate void 3890Sstevel@tonic-gate set_parking_flag(ulwp_t *self, int park) 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate volatile sc_shared_t *scp; 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate enter_critical(self); 3940Sstevel@tonic-gate if ((scp = self->ul_schedctl) != NULL || 3950Sstevel@tonic-gate (scp = setup_schedctl()) != NULL) 3960Sstevel@tonic-gate scp->sc_park = park; 3970Sstevel@tonic-gate else if (park == 0) /* schedctl failed, do it the long way */ 3980Sstevel@tonic-gate __lwp_unpark(self->ul_lwpid); 3990Sstevel@tonic-gate exit_critical(self); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* 4030Sstevel@tonic-gate * Tell the kernel to block all signals. 4040Sstevel@tonic-gate * Use the schedctl interface, or failing that, use __lwp_sigmask(). 4050Sstevel@tonic-gate * This action can be rescinded only by making a system call that 4060Sstevel@tonic-gate * sets the signal mask: 4070Sstevel@tonic-gate * __lwp_sigmask(), __sigprocmask(), __setcontext(), 4080Sstevel@tonic-gate * __sigsuspend() or __pollsys(). 4090Sstevel@tonic-gate * In particular, this action cannot be reversed by assigning 4100Sstevel@tonic-gate * scp->sc_sigblock = 0. That would be a way to lose signals. 4110Sstevel@tonic-gate * See the definition of restore_signals(self). 4120Sstevel@tonic-gate */ 4130Sstevel@tonic-gate void 4140Sstevel@tonic-gate block_all_signals(ulwp_t *self) 4150Sstevel@tonic-gate { 4160Sstevel@tonic-gate volatile sc_shared_t *scp; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate enter_critical(self); 4190Sstevel@tonic-gate if ((scp = self->ul_schedctl) != NULL || 4200Sstevel@tonic-gate (scp = setup_schedctl()) != NULL) 4210Sstevel@tonic-gate scp->sc_sigblock = 1; 4220Sstevel@tonic-gate else 4230Sstevel@tonic-gate (void) __lwp_sigmask(SIG_SETMASK, &maskset, NULL); 4240Sstevel@tonic-gate exit_critical(self); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate #pragma weak setcontext = _private_setcontext 4280Sstevel@tonic-gate #pragma weak _setcontext = _private_setcontext 4290Sstevel@tonic-gate int 4300Sstevel@tonic-gate _private_setcontext(const ucontext_t *ucp) 4310Sstevel@tonic-gate { 4320Sstevel@tonic-gate ulwp_t *self = curthread; 4330Sstevel@tonic-gate int ret; 4340Sstevel@tonic-gate ucontext_t uc; 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate /* 4370Sstevel@tonic-gate * Returning from the main context (uc_link == NULL) causes 4380Sstevel@tonic-gate * the thread to exit. See setcontext(2) and makecontext(3C). 4390Sstevel@tonic-gate */ 4400Sstevel@tonic-gate if (ucp == NULL) 4410Sstevel@tonic-gate _thr_exit(NULL); 442*1111Sraf (void) _private_memcpy(&uc, ucp, sizeof (uc)); 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * Restore previous signal mask and context link. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate if (uc.uc_flags & UC_SIGMASK) { 4480Sstevel@tonic-gate block_all_signals(self); 4490Sstevel@tonic-gate delete_reserved_signals(&uc.uc_sigmask); 4500Sstevel@tonic-gate self->ul_sigmask = uc.uc_sigmask; 4510Sstevel@tonic-gate if (self->ul_cursig) { 4520Sstevel@tonic-gate /* 4530Sstevel@tonic-gate * We have a deferred signal present. 4540Sstevel@tonic-gate * The signal mask will be set when the 4550Sstevel@tonic-gate * signal is taken in take_deferred_signal(). 4560Sstevel@tonic-gate */ 4570Sstevel@tonic-gate ASSERT(self->ul_critical + self->ul_sigdefer != 0); 4580Sstevel@tonic-gate uc.uc_flags &= ~UC_SIGMASK; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate self->ul_siglink = uc.uc_link; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * We don't know where this context structure has been. 4650Sstevel@tonic-gate * Preserve the curthread pointer, at least. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate #if defined(__sparc) 4680Sstevel@tonic-gate uc.uc_mcontext.gregs[REG_G7] = (greg_t)self; 4690Sstevel@tonic-gate #elif defined(__amd64) 4700Sstevel@tonic-gate uc.uc_mcontext.gregs[REG_FS] = (greg_t)self->ul_gs; 4710Sstevel@tonic-gate #elif defined(__i386) 4720Sstevel@tonic-gate uc.uc_mcontext.gregs[GS] = (greg_t)self->ul_gs; 4730Sstevel@tonic-gate #else 4740Sstevel@tonic-gate #error "none of __sparc, __amd64, __i386 defined" 4750Sstevel@tonic-gate #endif 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * Make sure that if we return to a call to __lwp_park() 4780Sstevel@tonic-gate * or ___lwp_cond_wait() that it returns right away 4790Sstevel@tonic-gate * (giving us a spurious wakeup but not a deadlock). 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate set_parking_flag(self, 0); 4820Sstevel@tonic-gate self->ul_sp = 0; 4830Sstevel@tonic-gate ret = __setcontext_syscall(&uc); 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* 4860Sstevel@tonic-gate * It is OK for setcontext() to return if the user has not specified 4870Sstevel@tonic-gate * UC_CPU. 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate if (uc.uc_flags & UC_CPU) 4900Sstevel@tonic-gate thr_panic("setcontext(): __setcontext() returned"); 4910Sstevel@tonic-gate return (ret); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate #pragma weak thr_sigsetmask = _thr_sigsetmask 4950Sstevel@tonic-gate #pragma weak pthread_sigmask = _thr_sigsetmask 4960Sstevel@tonic-gate #pragma weak _pthread_sigmask = _thr_sigsetmask 4970Sstevel@tonic-gate int 4980Sstevel@tonic-gate _thr_sigsetmask(int how, const sigset_t *set, sigset_t *oset) 4990Sstevel@tonic-gate { 5000Sstevel@tonic-gate ulwp_t *self = curthread; 5010Sstevel@tonic-gate sigset_t saveset; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate if (set == NULL) { 5040Sstevel@tonic-gate enter_critical(self); 5050Sstevel@tonic-gate if (oset != NULL) 5060Sstevel@tonic-gate *oset = self->ul_sigmask; 5070Sstevel@tonic-gate exit_critical(self); 5080Sstevel@tonic-gate } else { 5090Sstevel@tonic-gate switch (how) { 5100Sstevel@tonic-gate case SIG_BLOCK: 5110Sstevel@tonic-gate case SIG_UNBLOCK: 5120Sstevel@tonic-gate case SIG_SETMASK: 5130Sstevel@tonic-gate break; 5140Sstevel@tonic-gate default: 5150Sstevel@tonic-gate return (EINVAL); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * The assignments to self->ul_sigmask must be protected from 5200Sstevel@tonic-gate * signals. The nuances of this code are subtle. Be careful. 5210Sstevel@tonic-gate */ 5220Sstevel@tonic-gate block_all_signals(self); 5230Sstevel@tonic-gate if (oset != NULL) 5240Sstevel@tonic-gate saveset = self->ul_sigmask; 5250Sstevel@tonic-gate switch (how) { 5260Sstevel@tonic-gate case SIG_BLOCK: 5270Sstevel@tonic-gate self->ul_sigmask.__sigbits[0] |= set->__sigbits[0]; 5280Sstevel@tonic-gate self->ul_sigmask.__sigbits[1] |= set->__sigbits[1]; 5290Sstevel@tonic-gate break; 5300Sstevel@tonic-gate case SIG_UNBLOCK: 5310Sstevel@tonic-gate self->ul_sigmask.__sigbits[0] &= ~set->__sigbits[0]; 5320Sstevel@tonic-gate self->ul_sigmask.__sigbits[1] &= ~set->__sigbits[1]; 5330Sstevel@tonic-gate break; 5340Sstevel@tonic-gate case SIG_SETMASK: 5350Sstevel@tonic-gate self->ul_sigmask.__sigbits[0] = set->__sigbits[0]; 5360Sstevel@tonic-gate self->ul_sigmask.__sigbits[1] = set->__sigbits[1]; 5370Sstevel@tonic-gate break; 5380Sstevel@tonic-gate } 5390Sstevel@tonic-gate delete_reserved_signals(&self->ul_sigmask); 5400Sstevel@tonic-gate if (oset != NULL) 5410Sstevel@tonic-gate *oset = saveset; 5420Sstevel@tonic-gate restore_signals(self); 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate return (0); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate #pragma weak sigprocmask = _sigprocmask 5490Sstevel@tonic-gate int 5500Sstevel@tonic-gate _sigprocmask(int how, const sigset_t *set, sigset_t *oset) 5510Sstevel@tonic-gate { 5520Sstevel@tonic-gate int error; 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* 5550Sstevel@tonic-gate * Guard against children of vfork(). 5560Sstevel@tonic-gate */ 5570Sstevel@tonic-gate if (curthread->ul_vfork) 5580Sstevel@tonic-gate return (__lwp_sigmask(how, set, oset)); 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate if ((error = _thr_sigsetmask(how, set, oset)) != 0) { 5610Sstevel@tonic-gate errno = error; 5620Sstevel@tonic-gate return (-1); 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate return (0); 5660Sstevel@tonic-gate } 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* 5690Sstevel@tonic-gate * Called at library initialization to set up signal handling. 5700Sstevel@tonic-gate * All we really do is initialize the sig_lock mutexes. 5710Sstevel@tonic-gate * All signal handlers are either SIG_DFL or SIG_IGN on exec(). 5720Sstevel@tonic-gate * However, if any signal handlers were established on alternate 5730Sstevel@tonic-gate * link maps before the primary link map has been initialized, 5740Sstevel@tonic-gate * then inform the kernel of the new sigacthandler. 5750Sstevel@tonic-gate */ 5760Sstevel@tonic-gate void 5770Sstevel@tonic-gate signal_init() 5780Sstevel@tonic-gate { 5790Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 5800Sstevel@tonic-gate struct sigaction *sap; 5810Sstevel@tonic-gate struct sigaction act; 5820Sstevel@tonic-gate int sig; 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate for (sig = 0; sig < NSIG; sig++) { 5850Sstevel@tonic-gate udp->siguaction[sig].sig_lock.mutex_magic = MUTEX_MAGIC; 5860Sstevel@tonic-gate sap = &udp->siguaction[sig].sig_uaction; 5870Sstevel@tonic-gate if (sap->sa_sigaction != SIG_DFL && 5880Sstevel@tonic-gate sap->sa_sigaction != SIG_IGN && 5890Sstevel@tonic-gate __sigaction(sig, NULL, &act) == 0 && 5900Sstevel@tonic-gate act.sa_sigaction != SIG_DFL && 5910Sstevel@tonic-gate act.sa_sigaction != SIG_IGN) { 5920Sstevel@tonic-gate act = *sap; 5930Sstevel@tonic-gate act.sa_flags &= ~SA_NODEFER; 5940Sstevel@tonic-gate act.sa_sigaction = udp->sigacthandler; 5950Sstevel@tonic-gate act.sa_mask = maskset; 5960Sstevel@tonic-gate (void) __sigaction(sig, &act, NULL); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate } 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * Common code for cancelling self in _sigcancel() and pthread_cancel(). 6030Sstevel@tonic-gate * If the thread is at a cancellation point (ul_cancelable) then just 6040Sstevel@tonic-gate * return and let _canceloff() do the exit, else exit immediately if 6050Sstevel@tonic-gate * async mode is in effect. 6060Sstevel@tonic-gate */ 6070Sstevel@tonic-gate void 6080Sstevel@tonic-gate do_sigcancel() 6090Sstevel@tonic-gate { 6100Sstevel@tonic-gate ulwp_t *self = curthread; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate ASSERT(self->ul_critical == 0); 6130Sstevel@tonic-gate ASSERT(self->ul_sigdefer == 0); 6140Sstevel@tonic-gate self->ul_cancel_pending = 1; 6150Sstevel@tonic-gate if (self->ul_cancel_async && 6160Sstevel@tonic-gate !self->ul_cancel_disabled && 6170Sstevel@tonic-gate !self->ul_cancelable) 6180Sstevel@tonic-gate _pthread_exit(PTHREAD_CANCELED); 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate /* 6220Sstevel@tonic-gate * Set up the SIGCANCEL handler for threads cancellation 6230Sstevel@tonic-gate * (needed only when we have more than one thread). 6240Sstevel@tonic-gate * We need no locks here because we are called from 6250Sstevel@tonic-gate * finish_init() while still single-threaded. 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate void 6280Sstevel@tonic-gate init_sigcancel() 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata; 6310Sstevel@tonic-gate struct sigaction act; 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate act = udp->siguaction[SIGCANCEL].sig_uaction; 6340Sstevel@tonic-gate if (act.sa_sigaction == SIG_DFL || 6350Sstevel@tonic-gate act.sa_sigaction == SIG_IGN) 6360Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 6370Sstevel@tonic-gate else { 6380Sstevel@tonic-gate act.sa_flags |= SA_SIGINFO; 6390Sstevel@tonic-gate act.sa_flags &= ~(SA_NODEFER | SA_RESETHAND); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate act.sa_sigaction = udp->sigacthandler; 6420Sstevel@tonic-gate act.sa_mask = maskset; 6430Sstevel@tonic-gate (void) __sigaction(SIGCANCEL, &act, NULL); 6440Sstevel@tonic-gate } 645