xref: /onnv-gate/usr/src/uts/common/os/sig.c (revision 11913:283e725df792)
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
52248Sraf  * Common Development and Distribution License (the "License").
62248Sraf  * 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  */
21390Sraf 
220Sstevel@tonic-gate /*
2311861SMarek.Pospisil@Sun.COM  * Copyright 2010 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) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/bitmap.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/user.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/proc.h>
390Sstevel@tonic-gate #include <sys/poll_impl.h> /* only needed for kludge in sigwaiting_send() */
400Sstevel@tonic-gate #include <sys/signal.h>
410Sstevel@tonic-gate #include <sys/siginfo.h>
420Sstevel@tonic-gate #include <sys/fault.h>
430Sstevel@tonic-gate #include <sys/ucontext.h>
440Sstevel@tonic-gate #include <sys/procfs.h>
450Sstevel@tonic-gate #include <sys/wait.h>
460Sstevel@tonic-gate #include <sys/class.h>
470Sstevel@tonic-gate #include <sys/mman.h>
480Sstevel@tonic-gate #include <sys/procset.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/cpuvar.h>
510Sstevel@tonic-gate #include <sys/prsystm.h>
520Sstevel@tonic-gate #include <sys/debug.h>
530Sstevel@tonic-gate #include <vm/as.h>
540Sstevel@tonic-gate #include <sys/bitmap.h>
550Sstevel@tonic-gate #include <c2/audit.h>
560Sstevel@tonic-gate #include <sys/core.h>
570Sstevel@tonic-gate #include <sys/schedctl.h>
580Sstevel@tonic-gate #include <sys/contract/process_impl.h>
599870SRoger.Faulkner@Sun.COM #include <sys/cyclic.h>
600Sstevel@tonic-gate #include <sys/dtrace.h>
610Sstevel@tonic-gate #include <sys/sdt.h>
620Sstevel@tonic-gate 
63*11913SRoger.Faulkner@Sun.COM const k_sigset_t nullsmask = {0, 0, 0};
64*11913SRoger.Faulkner@Sun.COM 
65*11913SRoger.Faulkner@Sun.COM const k_sigset_t fillset =	/* MUST be contiguous */
66*11913SRoger.Faulkner@Sun.COM 	{FILLSET0, FILLSET1, FILLSET2};
670Sstevel@tonic-gate 
68*11913SRoger.Faulkner@Sun.COM const k_sigset_t cantmask =
69*11913SRoger.Faulkner@Sun.COM 	{CANTMASK0, CANTMASK1, CANTMASK2};
700Sstevel@tonic-gate 
71*11913SRoger.Faulkner@Sun.COM const k_sigset_t cantreset =
72*11913SRoger.Faulkner@Sun.COM 	{(sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGPWR)), 0, 0};
730Sstevel@tonic-gate 
74*11913SRoger.Faulkner@Sun.COM const k_sigset_t ignoredefault =
75*11913SRoger.Faulkner@Sun.COM 	{(sigmask(SIGCONT)|sigmask(SIGCLD)|sigmask(SIGPWR)
76*11913SRoger.Faulkner@Sun.COM 	|sigmask(SIGWINCH)|sigmask(SIGURG)|sigmask(SIGWAITING)),
77*11913SRoger.Faulkner@Sun.COM 	(sigmask(SIGLWP)|sigmask(SIGCANCEL)|sigmask(SIGFREEZE)
78*11913SRoger.Faulkner@Sun.COM 	|sigmask(SIGTHAW)|sigmask(SIGXRES)|sigmask(SIGJVM1)
79*11913SRoger.Faulkner@Sun.COM 	|sigmask(SIGJVM2)), 0};
800Sstevel@tonic-gate 
81*11913SRoger.Faulkner@Sun.COM const k_sigset_t stopdefault =
82*11913SRoger.Faulkner@Sun.COM 	{(sigmask(SIGSTOP)|sigmask(SIGTSTP)|sigmask(SIGTTOU)|sigmask(SIGTTIN)),
83*11913SRoger.Faulkner@Sun.COM 	0, 0};
840Sstevel@tonic-gate 
85*11913SRoger.Faulkner@Sun.COM const k_sigset_t coredefault =
86*11913SRoger.Faulkner@Sun.COM 	{(sigmask(SIGQUIT)|sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGIOT)
87*11913SRoger.Faulkner@Sun.COM 	|sigmask(SIGEMT)|sigmask(SIGFPE)|sigmask(SIGBUS)|sigmask(SIGSEGV)
88*11913SRoger.Faulkner@Sun.COM 	|sigmask(SIGSYS)|sigmask(SIGXCPU)|sigmask(SIGXFSZ)), 0, 0};
890Sstevel@tonic-gate 
90*11913SRoger.Faulkner@Sun.COM const k_sigset_t holdvfork =
91*11913SRoger.Faulkner@Sun.COM 	{(sigmask(SIGTTOU)|sigmask(SIGTTIN)|sigmask(SIGTSTP)), 0, 0};
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static	int	isjobstop(int);
940Sstevel@tonic-gate static	void	post_sigcld(proc_t *, sigqueue_t *);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Internal variables for counting number of user thread stop requests posted.
980Sstevel@tonic-gate  * They may not be accurate at some special situation such as that a virtually
990Sstevel@tonic-gate  * stopped thread starts to run.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate static int num_utstop;
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * Internal variables for broadcasting an event when all thread stop requests
1040Sstevel@tonic-gate  * are processed.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate static kcondvar_t utstop_cv;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static kmutex_t thread_stop_lock;
1090Sstevel@tonic-gate void del_one_utstop(void);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Send the specified signal to the specified process.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate void
psignal(proc_t * p,int sig)1150Sstevel@tonic-gate psignal(proc_t *p, int sig)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1180Sstevel@tonic-gate 	sigtoproc(p, NULL, sig);
1190Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * Send the specified signal to the specified thread.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate void
tsignal(kthread_t * t,int sig)1260Sstevel@tonic-gate tsignal(kthread_t *t, int sig)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1310Sstevel@tonic-gate 	sigtoproc(p, t, sig);
1320Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate int
signal_is_blocked(kthread_t * t,int sig)1360Sstevel@tonic-gate signal_is_blocked(kthread_t *t, int sig)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	return (sigismember(&t->t_hold, sig) ||
1390Sstevel@tonic-gate 	    (schedctl_sigblock(t) && !sigismember(&cantmask, sig)));
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * Return true if the signal can safely be discarded on generation.
1440Sstevel@tonic-gate  * That is, if there is no need for the signal on the receiving end.
1450Sstevel@tonic-gate  * The answer is true if the process is a zombie or
1460Sstevel@tonic-gate  * if all of these conditions are true:
1470Sstevel@tonic-gate  *	the signal is being ignored
1480Sstevel@tonic-gate  *	the process is single-threaded
1490Sstevel@tonic-gate  *	the signal is not being traced by /proc
1500Sstevel@tonic-gate  * 	the signal is not blocked by the process
1519385SRoger.Faulkner@Sun.COM  *	the signal is not being accepted via sigwait()
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static int
sig_discardable(proc_t * p,int sig)1540Sstevel@tonic-gate sig_discardable(proc_t *p, int sig)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	kthread_t *t = p->p_tlist;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	return (t == NULL ||		/* if zombie or ... */
1590Sstevel@tonic-gate 	    (sigismember(&p->p_ignore, sig) &&	/* signal is ignored */
1600Sstevel@tonic-gate 	    t->t_forw == t &&			/* and single-threaded */
1610Sstevel@tonic-gate 	    !tracing(p, sig) &&			/* and no /proc tracing */
1629385SRoger.Faulkner@Sun.COM 	    !signal_is_blocked(t, sig) &&	/* and signal not blocked */
1639385SRoger.Faulkner@Sun.COM 	    !sigismember(&t->t_sigwait, sig)));	/* and not being accepted */
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Return true if this thread is going to eat this signal soon.
168390Sraf  * Note that, if the signal is SIGKILL, we force stopped threads to be
169390Sraf  * set running (to make SIGKILL be a sure kill), but only if the process
170390Sraf  * is not currently locked by /proc (the P_PR_LOCK flag).  Code in /proc
171390Sraf  * relies on the fact that a process will not change shape while P_PR_LOCK
172390Sraf  * is set (it drops and reacquires p->p_lock while leaving P_PR_LOCK set).
173390Sraf  * We wish that we could simply call prbarrier() below, in sigtoproc(), to
174390Sraf  * ensure that the process is not locked by /proc, but prbarrier() drops
175390Sraf  * and reacquires p->p_lock and dropping p->p_lock here would be damaging.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate int
eat_signal(kthread_t * t,int sig)1780Sstevel@tonic-gate eat_signal(kthread_t *t, int sig)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	int rval = 0;
1810Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	/*
1840Sstevel@tonic-gate 	 * Do not do anything if the target thread has the signal blocked.
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate 	if (!signal_is_blocked(t, sig)) {
1870Sstevel@tonic-gate 		t->t_sig_check = 1;	/* have thread do an issig */
1883792Sakolb 		if (ISWAKEABLE(t) || ISWAITING(t)) {
1890Sstevel@tonic-gate 			setrun_locked(t);
1900Sstevel@tonic-gate 			rval = 1;
191390Sraf 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL &&
192390Sraf 		    !(ttoproc(t)->p_proc_flag & P_PR_LOCK)) {
1930Sstevel@tonic-gate 			ttoproc(t)->p_stopsig = 0;
1940Sstevel@tonic-gate 			t->t_dtrace_stop = 0;
1950Sstevel@tonic-gate 			t->t_schedflag |= TS_XSTART | TS_PSTART;
1960Sstevel@tonic-gate 			setrun_locked(t);
1970Sstevel@tonic-gate 		} else if (t != curthread && t->t_state == TS_ONPROC) {
198139Smishra 			aston(t);	/* make it do issig promptly */
199139Smishra 			if (t->t_cpu != CPU)
2000Sstevel@tonic-gate 				poke_cpu(t->t_cpu->cpu_id);
2010Sstevel@tonic-gate 			rval = 1;
2020Sstevel@tonic-gate 		} else if (t->t_state == TS_RUN) {
2030Sstevel@tonic-gate 			rval = 1;
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	return (rval);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * Post a signal.
2120Sstevel@tonic-gate  * If a non-null thread pointer is passed, then post the signal
2130Sstevel@tonic-gate  * to the thread/lwp, otherwise post the signal to the process.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate void
sigtoproc(proc_t * p,kthread_t * t,int sig)2160Sstevel@tonic-gate sigtoproc(proc_t *p, kthread_t *t, int sig)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	kthread_t *tt;
2190Sstevel@tonic-gate 	int ext = !(curproc->p_flag & SSYS) &&
2200Sstevel@tonic-gate 	    (curproc->p_ct_process != p->p_ct_process);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
2230Sstevel@tonic-gate 
22411173SJonathan.Adams@Sun.COM 	/* System processes don't get signals */
22511173SJonathan.Adams@Sun.COM 	if (sig <= 0 || sig >= NSIG || (p->p_flag & SSYS))
2260Sstevel@tonic-gate 		return;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Regardless of origin or directedness,
2300Sstevel@tonic-gate 	 * SIGKILL kills all lwps in the process immediately
2310Sstevel@tonic-gate 	 * and jobcontrol signals affect all lwps in the process.
2320Sstevel@tonic-gate 	 */
2330Sstevel@tonic-gate 	if (sig == SIGKILL) {
2340Sstevel@tonic-gate 		p->p_flag |= SKILLED | (ext ? SEXTKILLED : 0);
2350Sstevel@tonic-gate 		t = NULL;
2360Sstevel@tonic-gate 	} else if (sig == SIGCONT) {
2370Sstevel@tonic-gate 		/*
2380Sstevel@tonic-gate 		 * The SSCONT flag will remain set until a stopping
2390Sstevel@tonic-gate 		 * signal comes in (below).  This is harmless.
2400Sstevel@tonic-gate 		 */
2410Sstevel@tonic-gate 		p->p_flag |= SSCONT;
2420Sstevel@tonic-gate 		sigdelq(p, NULL, SIGSTOP);
2430Sstevel@tonic-gate 		sigdelq(p, NULL, SIGTSTP);
2440Sstevel@tonic-gate 		sigdelq(p, NULL, SIGTTOU);
2450Sstevel@tonic-gate 		sigdelq(p, NULL, SIGTTIN);
2460Sstevel@tonic-gate 		sigdiffset(&p->p_sig, &stopdefault);
2470Sstevel@tonic-gate 		sigdiffset(&p->p_extsig, &stopdefault);
2480Sstevel@tonic-gate 		p->p_stopsig = 0;
2490Sstevel@tonic-gate 		if ((tt = p->p_tlist) != NULL) {
2500Sstevel@tonic-gate 			do {
2510Sstevel@tonic-gate 				sigdelq(p, tt, SIGSTOP);
2520Sstevel@tonic-gate 				sigdelq(p, tt, SIGTSTP);
2530Sstevel@tonic-gate 				sigdelq(p, tt, SIGTTOU);
2540Sstevel@tonic-gate 				sigdelq(p, tt, SIGTTIN);
2550Sstevel@tonic-gate 				sigdiffset(&tt->t_sig, &stopdefault);
2560Sstevel@tonic-gate 				sigdiffset(&tt->t_extsig, &stopdefault);
2570Sstevel@tonic-gate 			} while ((tt = tt->t_forw) != p->p_tlist);
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 		if ((tt = p->p_tlist) != NULL) {
2600Sstevel@tonic-gate 			do {
2610Sstevel@tonic-gate 				thread_lock(tt);
2620Sstevel@tonic-gate 				if (tt->t_state == TS_STOPPED &&
2630Sstevel@tonic-gate 				    tt->t_whystop == PR_JOBCONTROL) {
2640Sstevel@tonic-gate 					tt->t_schedflag |= TS_XSTART;
2650Sstevel@tonic-gate 					setrun_locked(tt);
2660Sstevel@tonic-gate 				}
2670Sstevel@tonic-gate 				thread_unlock(tt);
2680Sstevel@tonic-gate 			} while ((tt = tt->t_forw) != p->p_tlist);
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 	} else if (sigismember(&stopdefault, sig)) {
2710Sstevel@tonic-gate 		/*
2720Sstevel@tonic-gate 		 * This test has a race condition which we can't fix:
2730Sstevel@tonic-gate 		 * By the time the stopping signal is received by
2740Sstevel@tonic-gate 		 * the target process/thread, the signal handler
2750Sstevel@tonic-gate 		 * and/or the detached state might have changed.
2760Sstevel@tonic-gate 		 */
2770Sstevel@tonic-gate 		if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
2780Sstevel@tonic-gate 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
2790Sstevel@tonic-gate 			p->p_flag &= ~SSCONT;
2800Sstevel@tonic-gate 		sigdelq(p, NULL, SIGCONT);
2810Sstevel@tonic-gate 		sigdelset(&p->p_sig, SIGCONT);
2820Sstevel@tonic-gate 		sigdelset(&p->p_extsig, SIGCONT);
2830Sstevel@tonic-gate 		if ((tt = p->p_tlist) != NULL) {
2840Sstevel@tonic-gate 			do {
2850Sstevel@tonic-gate 				sigdelq(p, tt, SIGCONT);
2860Sstevel@tonic-gate 				sigdelset(&tt->t_sig, SIGCONT);
2870Sstevel@tonic-gate 				sigdelset(&tt->t_extsig, SIGCONT);
2880Sstevel@tonic-gate 			} while ((tt = tt->t_forw) != p->p_tlist);
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (sig_discardable(p, sig)) {
2930Sstevel@tonic-gate 		DTRACE_PROC3(signal__discard, kthread_t *, p->p_tlist,
2940Sstevel@tonic-gate 		    proc_t *, p, int, sig);
2950Sstevel@tonic-gate 		return;
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (t != NULL) {
2990Sstevel@tonic-gate 		/*
3000Sstevel@tonic-gate 		 * This is a directed signal, wake up the lwp.
3010Sstevel@tonic-gate 		 */
3020Sstevel@tonic-gate 		sigaddset(&t->t_sig, sig);
3030Sstevel@tonic-gate 		if (ext)
3040Sstevel@tonic-gate 			sigaddset(&t->t_extsig, sig);
3050Sstevel@tonic-gate 		thread_lock(t);
3060Sstevel@tonic-gate 		(void) eat_signal(t, sig);
3070Sstevel@tonic-gate 		thread_unlock(t);
3080Sstevel@tonic-gate 		DTRACE_PROC2(signal__send, kthread_t *, t, int, sig);
3090Sstevel@tonic-gate 	} else if ((tt = p->p_tlist) != NULL) {
3100Sstevel@tonic-gate 		/*
3110Sstevel@tonic-gate 		 * Make sure that some lwp that already exists
3120Sstevel@tonic-gate 		 * in the process fields the signal soon.
3130Sstevel@tonic-gate 		 * Wake up an interruptibly sleeping lwp if necessary.
3145297Sbpramod 		 * For SIGKILL make all of the lwps see the signal;
3155297Sbpramod 		 * This is needed to guarantee a sure kill for processes
3165297Sbpramod 		 * with a mix of realtime and non-realtime threads.
3170Sstevel@tonic-gate 		 */
3180Sstevel@tonic-gate 		int su = 0;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		sigaddset(&p->p_sig, sig);
3210Sstevel@tonic-gate 		if (ext)
3220Sstevel@tonic-gate 			sigaddset(&p->p_extsig, sig);
3230Sstevel@tonic-gate 		do {
3240Sstevel@tonic-gate 			thread_lock(tt);
3255297Sbpramod 			if (eat_signal(tt, sig) && sig != SIGKILL) {
3260Sstevel@tonic-gate 				thread_unlock(tt);
3270Sstevel@tonic-gate 				break;
3280Sstevel@tonic-gate 			}
3295297Sbpramod 			if (SUSPENDED(tt))
3300Sstevel@tonic-gate 				su++;
3310Sstevel@tonic-gate 			thread_unlock(tt);
3320Sstevel@tonic-gate 		} while ((tt = tt->t_forw) != p->p_tlist);
3330Sstevel@tonic-gate 		/*
3340Sstevel@tonic-gate 		 * If the process is deadlocked, make somebody run and die.
3350Sstevel@tonic-gate 		 */
3360Sstevel@tonic-gate 		if (sig == SIGKILL && p->p_stat != SIDL &&
337390Sraf 		    p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
338390Sraf 		    !(p->p_proc_flag & P_PR_LOCK)) {
3390Sstevel@tonic-gate 			thread_lock(tt);
3400Sstevel@tonic-gate 			p->p_lwprcnt++;
3410Sstevel@tonic-gate 			tt->t_schedflag |= TS_CSTART;
3420Sstevel@tonic-gate 			setrun_locked(tt);
3430Sstevel@tonic-gate 			thread_unlock(tt);
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
3470Sstevel@tonic-gate 	}
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate static int
isjobstop(int sig)3510Sstevel@tonic-gate isjobstop(int sig)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
3560Sstevel@tonic-gate 
3573446Smrj 	if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
3583446Smrj 	    sigismember(&stopdefault, sig)) {
3590Sstevel@tonic-gate 		/*
3600Sstevel@tonic-gate 		 * If SIGCONT has been posted since we promoted this signal
3610Sstevel@tonic-gate 		 * from pending to current, then don't do a jobcontrol stop.
3620Sstevel@tonic-gate 		 */
3630Sstevel@tonic-gate 		if (!(p->p_flag & SSCONT) &&
3640Sstevel@tonic-gate 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
3650Sstevel@tonic-gate 		    curthread != p->p_agenttp) {
3660Sstevel@tonic-gate 			sigqueue_t *sqp;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 			stop(PR_JOBCONTROL, sig);
3690Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3700Sstevel@tonic-gate 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
3710Sstevel@tonic-gate 			mutex_enter(&pidlock);
3720Sstevel@tonic-gate 			/*
3730Sstevel@tonic-gate 			 * Only the first lwp to continue notifies the parent.
3740Sstevel@tonic-gate 			 */
3750Sstevel@tonic-gate 			if (p->p_pidflag & CLDCONT)
3760Sstevel@tonic-gate 				siginfofree(sqp);
3770Sstevel@tonic-gate 			else {
3780Sstevel@tonic-gate 				p->p_pidflag |= CLDCONT;
3790Sstevel@tonic-gate 				p->p_wcode = CLD_CONTINUED;
3800Sstevel@tonic-gate 				p->p_wdata = SIGCONT;
3810Sstevel@tonic-gate 				sigcld(p, sqp);
3820Sstevel@tonic-gate 			}
3830Sstevel@tonic-gate 			mutex_exit(&pidlock);
3840Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		return (1);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 	return (0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Returns true if the current process has a signal to process, and
3930Sstevel@tonic-gate  * the signal is not held.  The signal to process is put in p_cursig.
3940Sstevel@tonic-gate  * This is asked at least once each time a process enters the system
3950Sstevel@tonic-gate  * (though this can usually be done without actually calling issig by
3960Sstevel@tonic-gate  * checking the pending signal masks).  A signal does not do anything
3970Sstevel@tonic-gate  * directly to a process; it sets a flag that asks the process to do
3980Sstevel@tonic-gate  * something to itself.
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  * The "why" argument indicates the allowable side-effects of the call:
4010Sstevel@tonic-gate  *
4020Sstevel@tonic-gate  * FORREAL:  Extract the next pending signal from p_sig into p_cursig;
4030Sstevel@tonic-gate  * stop the process if a stop has been requested or if a traced signal
4040Sstevel@tonic-gate  * is pending.
4050Sstevel@tonic-gate  *
4060Sstevel@tonic-gate  * JUSTLOOKING:  Don't stop the process, just indicate whether or not
4070Sstevel@tonic-gate  * a signal might be pending (FORREAL is needed to tell for sure).
4080Sstevel@tonic-gate  *
4090Sstevel@tonic-gate  * XXX: Changes to the logic in these routines should be propagated
4100Sstevel@tonic-gate  * to lm_sigispending().  See bug 1201594.
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate static int issig_forreal(void);
4140Sstevel@tonic-gate static int issig_justlooking(void);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate int
issig(int why)4170Sstevel@tonic-gate issig(int why)
4180Sstevel@tonic-gate {
4190Sstevel@tonic-gate 	ASSERT(why == FORREAL || why == JUSTLOOKING);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	return ((why == FORREAL)? issig_forreal() : issig_justlooking());
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate static int
issig_justlooking(void)4260Sstevel@tonic-gate issig_justlooking(void)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	kthread_t *t = curthread;
4290Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
4300Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4310Sstevel@tonic-gate 	k_sigset_t set;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/*
4340Sstevel@tonic-gate 	 * This function answers the question:
4350Sstevel@tonic-gate 	 * "Is there any reason to call issig_forreal()?"
4360Sstevel@tonic-gate 	 *
4370Sstevel@tonic-gate 	 * We have to answer the question w/o grabbing any locks
4380Sstevel@tonic-gate 	 * because we are (most likely) being called after we
4390Sstevel@tonic-gate 	 * put ourselves on the sleep queue.
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (t->t_dtrace_stop | t->t_dtrace_sig)
4430Sstevel@tonic-gate 		return (1);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * Another piece of complexity in this process.  When single-stepping a
4470Sstevel@tonic-gate 	 * process, we don't want an intervening signal or TP_PAUSE request to
4480Sstevel@tonic-gate 	 * suspend the current thread.  Otherwise, the controlling process will
4490Sstevel@tonic-gate 	 * hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
4500Sstevel@tonic-gate 	 * We will trigger any remaining signals when we re-enter the kernel on
4510Sstevel@tonic-gate 	 * the single step trap.
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
4540Sstevel@tonic-gate 		return (0);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
4570Sstevel@tonic-gate 	    (p->p_flag & (SEXITLWPS|SKILLED)) ||
4583930Snr123932 	    (lwp->lwp_nostop == 0 &&
4593930Snr123932 	    (p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
4603930Snr123932 	    (t->t_proc_flag &
4613930Snr123932 	    (TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
4620Sstevel@tonic-gate 	    lwp->lwp_cursig)
4630Sstevel@tonic-gate 		return (1);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (p->p_flag & SVFWAIT)
4660Sstevel@tonic-gate 		return (0);
4670Sstevel@tonic-gate 	set = p->p_sig;
4680Sstevel@tonic-gate 	sigorset(&set, &t->t_sig);
4690Sstevel@tonic-gate 	if (schedctl_sigblock(t))	/* all blockable signals blocked */
4700Sstevel@tonic-gate 		sigandset(&set, &cantmask);
4710Sstevel@tonic-gate 	else
4720Sstevel@tonic-gate 		sigdiffset(&set, &t->t_hold);
4730Sstevel@tonic-gate 	if (p->p_flag & SVFORK)
4740Sstevel@tonic-gate 		sigdiffset(&set, &holdvfork);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (!sigisempty(&set)) {
4770Sstevel@tonic-gate 		int sig;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		for (sig = 1; sig < NSIG; sig++) {
4800Sstevel@tonic-gate 			if (sigismember(&set, sig) &&
4810Sstevel@tonic-gate 			    (tracing(p, sig) ||
4829385SRoger.Faulkner@Sun.COM 			    sigismember(&t->t_sigwait, sig) ||
4830Sstevel@tonic-gate 			    !sigismember(&p->p_ignore, sig))) {
4840Sstevel@tonic-gate 				/*
4850Sstevel@tonic-gate 				 * Don't promote a signal that will stop
4860Sstevel@tonic-gate 				 * the process when lwp_nostop is set.
4870Sstevel@tonic-gate 				 */
4880Sstevel@tonic-gate 				if (!lwp->lwp_nostop ||
48910043SRoger.Faulkner@Sun.COM 				    PTOU(p)->u_signal[sig-1] != SIG_DFL ||
4900Sstevel@tonic-gate 				    !sigismember(&stopdefault, sig))
4910Sstevel@tonic-gate 					return (1);
4920Sstevel@tonic-gate 			}
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	return (0);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate static int
issig_forreal(void)5000Sstevel@tonic-gate issig_forreal(void)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	int sig = 0, ext = 0;
5030Sstevel@tonic-gate 	kthread_t *t = curthread;
5040Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5050Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5060Sstevel@tonic-gate 	int toproc = 0;
5070Sstevel@tonic-gate 	int sigcld_found = 0;
5080Sstevel@tonic-gate 	int nostop_break = 0;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5130Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (t->t_dtrace_stop | t->t_dtrace_sig) {
5160Sstevel@tonic-gate 		if (t->t_dtrace_stop) {
5170Sstevel@tonic-gate 			/*
5180Sstevel@tonic-gate 			 * If DTrace's "stop" action has been invoked on us,
5190Sstevel@tonic-gate 			 * set TP_PRSTOP.
5200Sstevel@tonic-gate 			 */
5210Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		if (t->t_dtrace_sig != 0) {
5250Sstevel@tonic-gate 			k_siginfo_t info;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 			/*
5280Sstevel@tonic-gate 			 * Post the signal generated as the result of
5290Sstevel@tonic-gate 			 * DTrace's "raise" action as a normal signal before
5300Sstevel@tonic-gate 			 * the full-fledged signal checking begins.
5310Sstevel@tonic-gate 			 */
5320Sstevel@tonic-gate 			bzero(&info, sizeof (info));
5330Sstevel@tonic-gate 			info.si_signo = t->t_dtrace_sig;
5340Sstevel@tonic-gate 			info.si_code = SI_DTRACE;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 			sigaddq(p, NULL, &info, KM_NOSLEEP);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 			t->t_dtrace_sig = 0;
5390Sstevel@tonic-gate 		}
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	for (;;) {
5430Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
5440Sstevel@tonic-gate 			lwp->lwp_cursig = sig = SIGKILL;
5450Sstevel@tonic-gate 			lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
5465297Sbpramod 			t->t_sig_check = 1;
5470Sstevel@tonic-gate 			break;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		/*
5510Sstevel@tonic-gate 		 * Another piece of complexity in this process.  When
5520Sstevel@tonic-gate 		 * single-stepping a process, we don't want an intervening
5530Sstevel@tonic-gate 		 * signal or TP_PAUSE request to suspend the current thread.
5540Sstevel@tonic-gate 		 * Otherwise, the controlling process will hang beacuse we will
5550Sstevel@tonic-gate 		 * be stopped with TS_PSTART set in t_schedflag.  We will
5560Sstevel@tonic-gate 		 * trigger any remaining signals when we re-enter the kernel on
5570Sstevel@tonic-gate 		 * the single step trap.
5580Sstevel@tonic-gate 		 */
5590Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
5600Sstevel@tonic-gate 			sig = 0;
5610Sstevel@tonic-gate 			break;
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		/*
5650Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
5660Sstevel@tonic-gate 		 */
5670Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
5680Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
5690Sstevel@tonic-gate 			continue;
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
5730Sstevel@tonic-gate 			if ((sig = lwp->lwp_cursig) != 0) {
5740Sstevel@tonic-gate 				/*
5750Sstevel@tonic-gate 				 * Make sure we call ISSIG() in post_syscall()
5760Sstevel@tonic-gate 				 * to re-validate this current signal.
5770Sstevel@tonic-gate 				 */
5780Sstevel@tonic-gate 				t->t_sig_check = 1;
5790Sstevel@tonic-gate 			}
5800Sstevel@tonic-gate 			break;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		/*
5840Sstevel@tonic-gate 		 * If the request is PR_CHECKPOINT, ignore the rest of signals
5850Sstevel@tonic-gate 		 * or requests.  Honor other stop requests or signals later.
5860Sstevel@tonic-gate 		 * Go back to top of loop here to check if an exit or hold
5870Sstevel@tonic-gate 		 * event has occurred while stopped.
5880Sstevel@tonic-gate 		 */
5890Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
5900Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
5910Sstevel@tonic-gate 			continue;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
5960Sstevel@tonic-gate 		 * with signals or /proc.  Another lwp is executing fork1(),
5970Sstevel@tonic-gate 		 * or is undergoing watchpoint activity (remapping a page),
5980Sstevel@tonic-gate 		 * or is executing lwp_suspend() on this lwp.
5990Sstevel@tonic-gate 		 * Again, go back to top of loop to check if an exit
6000Sstevel@tonic-gate 		 * or hold event has occurred while stopped.
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 		if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
6033930Snr123932 		    (t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
6040Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
6050Sstevel@tonic-gate 			continue;
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		/*
6090Sstevel@tonic-gate 		 * Honor requested stop before dealing with the
6100Sstevel@tonic-gate 		 * current signal; a debugger may change it.
6110Sstevel@tonic-gate 		 * Do not want to go back to loop here since this is a special
6120Sstevel@tonic-gate 		 * stop that means: make incremental progress before the next
6130Sstevel@tonic-gate 		 * stop. The danger is that returning to top of loop would most
6140Sstevel@tonic-gate 		 * likely drop the thread right back here to stop soon after it
6150Sstevel@tonic-gate 		 * was continued, violating the incremental progress request.
6160Sstevel@tonic-gate 		 */
6170Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
6180Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		/*
6210Sstevel@tonic-gate 		 * If a debugger wants us to take a signal it will have
6220Sstevel@tonic-gate 		 * left it in lwp->lwp_cursig.  If lwp_cursig has been cleared
6230Sstevel@tonic-gate 		 * or if it's being ignored, we continue on looking for another
6240Sstevel@tonic-gate 		 * signal.  Otherwise we return the specified signal, provided
6250Sstevel@tonic-gate 		 * it's not a signal that causes a job control stop.
6260Sstevel@tonic-gate 		 *
6270Sstevel@tonic-gate 		 * When stopped on PR_JOBCONTROL, there is no current
6280Sstevel@tonic-gate 		 * signal; we cancel lwp->lwp_cursig temporarily before
6290Sstevel@tonic-gate 		 * calling isjobstop().  The current signal may be reset
6300Sstevel@tonic-gate 		 * by a debugger while we are stopped in isjobstop().
6319385SRoger.Faulkner@Sun.COM 		 *
6329385SRoger.Faulkner@Sun.COM 		 * If the current thread is accepting the signal
6339385SRoger.Faulkner@Sun.COM 		 * (via sigwait(), sigwaitinfo(), or sigtimedwait()),
6349385SRoger.Faulkner@Sun.COM 		 * we allow the signal to be accepted, even if it is
6359385SRoger.Faulkner@Sun.COM 		 * being ignored, and without causing a job control stop.
6360Sstevel@tonic-gate 		 */
6370Sstevel@tonic-gate 		if ((sig = lwp->lwp_cursig) != 0) {
6380Sstevel@tonic-gate 			ext = lwp->lwp_extsig;
6390Sstevel@tonic-gate 			lwp->lwp_cursig = 0;
6400Sstevel@tonic-gate 			lwp->lwp_extsig = 0;
6419385SRoger.Faulkner@Sun.COM 			if (sigismember(&t->t_sigwait, sig) ||
6429385SRoger.Faulkner@Sun.COM 			    (!sigismember(&p->p_ignore, sig) &&
6439385SRoger.Faulkner@Sun.COM 			    !isjobstop(sig))) {
6440Sstevel@tonic-gate 				if (p->p_flag & (SEXITLWPS|SKILLED)) {
6450Sstevel@tonic-gate 					sig = SIGKILL;
6460Sstevel@tonic-gate 					ext = (p->p_flag & SEXTKILLED) != 0;
6470Sstevel@tonic-gate 				}
6480Sstevel@tonic-gate 				lwp->lwp_cursig = (uchar_t)sig;
6490Sstevel@tonic-gate 				lwp->lwp_extsig = (uchar_t)ext;
6500Sstevel@tonic-gate 				break;
6510Sstevel@tonic-gate 			}
6520Sstevel@tonic-gate 			/*
6530Sstevel@tonic-gate 			 * The signal is being ignored or it caused a
6540Sstevel@tonic-gate 			 * job-control stop.  If another current signal
6550Sstevel@tonic-gate 			 * has not been established, return the current
6560Sstevel@tonic-gate 			 * siginfo, if any, to the memory manager.
6570Sstevel@tonic-gate 			 */
6580Sstevel@tonic-gate 			if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
6590Sstevel@tonic-gate 				siginfofree(lwp->lwp_curinfo);
6600Sstevel@tonic-gate 				lwp->lwp_curinfo = NULL;
6610Sstevel@tonic-gate 			}
6620Sstevel@tonic-gate 			/*
6630Sstevel@tonic-gate 			 * Loop around again in case we were stopped
6640Sstevel@tonic-gate 			 * on a job control signal and a /proc stop
6650Sstevel@tonic-gate 			 * request was posted or another current signal
6660Sstevel@tonic-gate 			 * was established while we were stopped.
6670Sstevel@tonic-gate 			 */
6680Sstevel@tonic-gate 			continue;
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 		if (p->p_stopsig && !lwp->lwp_nostop &&
6720Sstevel@tonic-gate 		    curthread != p->p_agenttp) {
6730Sstevel@tonic-gate 			/*
6740Sstevel@tonic-gate 			 * Some lwp in the process has already stopped
6750Sstevel@tonic-gate 			 * showing PR_JOBCONTROL.  This is a stop in
6760Sstevel@tonic-gate 			 * sympathy with the other lwp, even if this
6770Sstevel@tonic-gate 			 * lwp is blocking the stopping signal.
6780Sstevel@tonic-gate 			 */
6790Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
6800Sstevel@tonic-gate 			continue;
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		/*
6840Sstevel@tonic-gate 		 * Loop on the pending signals until we find a
6850Sstevel@tonic-gate 		 * non-held signal that is traced or not ignored.
6860Sstevel@tonic-gate 		 * First check the signals pending for the lwp,
6870Sstevel@tonic-gate 		 * then the signals pending for the process as a whole.
6880Sstevel@tonic-gate 		 */
6890Sstevel@tonic-gate 		for (;;) {
6909385SRoger.Faulkner@Sun.COM 			if ((sig = fsig(&t->t_sig, t)) != 0) {
6910Sstevel@tonic-gate 				toproc = 0;
6920Sstevel@tonic-gate 				if (tracing(p, sig) ||
6939385SRoger.Faulkner@Sun.COM 				    sigismember(&t->t_sigwait, sig) ||
6940Sstevel@tonic-gate 				    !sigismember(&p->p_ignore, sig)) {
6950Sstevel@tonic-gate 					if (sigismember(&t->t_extsig, sig))
6960Sstevel@tonic-gate 						ext = 1;
6970Sstevel@tonic-gate 					break;
6980Sstevel@tonic-gate 				}
6990Sstevel@tonic-gate 				sigdelset(&t->t_sig, sig);
7000Sstevel@tonic-gate 				sigdelset(&t->t_extsig, sig);
7010Sstevel@tonic-gate 				sigdelq(p, t, sig);
7020Sstevel@tonic-gate 			} else if ((sig = fsig(&p->p_sig, t)) != 0) {
7030Sstevel@tonic-gate 				if (sig == SIGCLD)
7040Sstevel@tonic-gate 					sigcld_found = 1;
7050Sstevel@tonic-gate 				toproc = 1;
7060Sstevel@tonic-gate 				if (tracing(p, sig) ||
7079385SRoger.Faulkner@Sun.COM 				    sigismember(&t->t_sigwait, sig) ||
7080Sstevel@tonic-gate 				    !sigismember(&p->p_ignore, sig)) {
7090Sstevel@tonic-gate 					if (sigismember(&p->p_extsig, sig))
7100Sstevel@tonic-gate 						ext = 1;
7110Sstevel@tonic-gate 					break;
7120Sstevel@tonic-gate 				}
7130Sstevel@tonic-gate 				sigdelset(&p->p_sig, sig);
7140Sstevel@tonic-gate 				sigdelset(&p->p_extsig, sig);
7150Sstevel@tonic-gate 				sigdelq(p, NULL, sig);
7160Sstevel@tonic-gate 			} else {
7170Sstevel@tonic-gate 				/* no signal was found */
7180Sstevel@tonic-gate 				break;
7190Sstevel@tonic-gate 			}
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		if (sig == 0) {	/* no signal was found */
7230Sstevel@tonic-gate 			if (p->p_flag & (SEXITLWPS|SKILLED)) {
7240Sstevel@tonic-gate 				lwp->lwp_cursig = SIGKILL;
7250Sstevel@tonic-gate 				sig = SIGKILL;
7260Sstevel@tonic-gate 				ext = (p->p_flag & SEXTKILLED) != 0;
7270Sstevel@tonic-gate 			}
7280Sstevel@tonic-gate 			break;
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		/*
7320Sstevel@tonic-gate 		 * If we have been informed not to stop (i.e., we are being
7330Sstevel@tonic-gate 		 * called from within a network operation), then don't promote
7340Sstevel@tonic-gate 		 * the signal at this time, just return the signal number.
7350Sstevel@tonic-gate 		 * We will call issig() again later when it is safe.
7360Sstevel@tonic-gate 		 *
7370Sstevel@tonic-gate 		 * fsig() does not return a jobcontrol stopping signal
7380Sstevel@tonic-gate 		 * with a default action of stopping the process if
7390Sstevel@tonic-gate 		 * lwp_nostop is set, so we won't be causing a bogus
7400Sstevel@tonic-gate 		 * EINTR by this action.  (Such a signal is eaten by
7410Sstevel@tonic-gate 		 * isjobstop() when we loop around to do final checks.)
7420Sstevel@tonic-gate 		 */
7430Sstevel@tonic-gate 		if (lwp->lwp_nostop) {
7440Sstevel@tonic-gate 			nostop_break = 1;
7450Sstevel@tonic-gate 			break;
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * Promote the signal from pending to current.
7500Sstevel@tonic-gate 		 *
7510Sstevel@tonic-gate 		 * Note that sigdeq() will set lwp->lwp_curinfo to NULL
7520Sstevel@tonic-gate 		 * if no siginfo_t exists for this signal.
7530Sstevel@tonic-gate 		 */
7540Sstevel@tonic-gate 		lwp->lwp_cursig = (uchar_t)sig;
7550Sstevel@tonic-gate 		lwp->lwp_extsig = (uchar_t)ext;
7560Sstevel@tonic-gate 		t->t_sig_check = 1;	/* so post_syscall will see signal */
7570Sstevel@tonic-gate 		ASSERT(lwp->lwp_curinfo == NULL);
7580Sstevel@tonic-gate 		sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 		if (tracing(p, sig))
7610Sstevel@tonic-gate 			stop(PR_SIGNALLED, sig);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		/*
7640Sstevel@tonic-gate 		 * Loop around to check for requested stop before
7650Sstevel@tonic-gate 		 * performing the usual current-signal actions.
7660Sstevel@tonic-gate 		 */
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
77210043SRoger.Faulkner@Sun.COM 	 * If SIGCLD was dequeued from the process's signal queue,
77310043SRoger.Faulkner@Sun.COM 	 * search for other pending SIGCLD's from the list of children.
7740Sstevel@tonic-gate 	 */
77510043SRoger.Faulkner@Sun.COM 	if (sigcld_found)
7760Sstevel@tonic-gate 		sigcld_repost();
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (sig != 0)
7790Sstevel@tonic-gate 		(void) undo_watch_step(NULL);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * If we have been blocked since the p_lock was dropped off
7830Sstevel@tonic-gate 	 * above, then this promoted signal might have been handled
7840Sstevel@tonic-gate 	 * already when we were on the way back from sleep queue, so
7850Sstevel@tonic-gate 	 * just ignore it.
7860Sstevel@tonic-gate 	 * If we have been informed not to stop, just return the signal
7870Sstevel@tonic-gate 	 * number. Also see comments above.
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	if (!nostop_break) {
7900Sstevel@tonic-gate 		sig = lwp->lwp_cursig;
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	return (sig != 0);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate /*
7970Sstevel@tonic-gate  * Return true if the process is currently stopped showing PR_JOBCONTROL.
7980Sstevel@tonic-gate  * This is true only if all of the process's lwp's are so stopped.
7990Sstevel@tonic-gate  * If this is asked by one of the lwps in the process, exclude that lwp.
8000Sstevel@tonic-gate  */
8010Sstevel@tonic-gate int
jobstopped(proc_t * p)8020Sstevel@tonic-gate jobstopped(proc_t *p)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 	kthread_t *t;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if ((t = p->p_tlist) == NULL)
8090Sstevel@tonic-gate 		return (0);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	do {
8120Sstevel@tonic-gate 		thread_lock(t);
8130Sstevel@tonic-gate 		/* ignore current, zombie and suspended lwps in the test */
8140Sstevel@tonic-gate 		if (!(t == curthread || t->t_state == TS_ZOMB ||
8150Sstevel@tonic-gate 		    SUSPENDED(t)) &&
8160Sstevel@tonic-gate 		    (t->t_state != TS_STOPPED ||
8170Sstevel@tonic-gate 		    t->t_whystop != PR_JOBCONTROL)) {
8180Sstevel@tonic-gate 			thread_unlock(t);
8190Sstevel@tonic-gate 			return (0);
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 		thread_unlock(t);
8220Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	return (1);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate  * Put ourself (curthread) into the stopped state and notify tracers.
8290Sstevel@tonic-gate  */
8300Sstevel@tonic-gate void
stop(int why,int what)8310Sstevel@tonic-gate stop(int why, int what)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	kthread_t	*t = curthread;
8340Sstevel@tonic-gate 	proc_t		*p = ttoproc(t);
8350Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
8360Sstevel@tonic-gate 	kthread_t	*tx;
8370Sstevel@tonic-gate 	lwpent_t	*lep;
8380Sstevel@tonic-gate 	int		procstop;
8390Sstevel@tonic-gate 	int		flags = TS_ALLSTART;
8400Sstevel@tonic-gate 	hrtime_t	stoptime;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/*
8430Sstevel@tonic-gate 	 * Can't stop a system process.
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
8460Sstevel@tonic-gate 		return;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
8510Sstevel@tonic-gate 		/*
8520Sstevel@tonic-gate 		 * Don't stop an lwp with SIGKILL pending.
8530Sstevel@tonic-gate 		 * Don't stop if the process or lwp is exiting.
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		if (lwp->lwp_cursig == SIGKILL ||
8560Sstevel@tonic-gate 		    sigismember(&t->t_sig, SIGKILL) ||
8570Sstevel@tonic-gate 		    sigismember(&p->p_sig, SIGKILL) ||
8580Sstevel@tonic-gate 		    (t->t_proc_flag & TP_LWPEXIT) ||
8590Sstevel@tonic-gate 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
8600Sstevel@tonic-gate 			p->p_stopsig = 0;
8610Sstevel@tonic-gate 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
8620Sstevel@tonic-gate 			return;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/*
8670Sstevel@tonic-gate 	 * Make sure we don't deadlock on a recursive call to prstop().
8683930Snr123932 	 * prstop() sets the lwp_nostop flag.
8690Sstevel@tonic-gate 	 */
8703930Snr123932 	if (lwp->lwp_nostop)
8710Sstevel@tonic-gate 		return;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	/*
8740Sstevel@tonic-gate 	 * Make sure the lwp is in an orderly state for inspection
8750Sstevel@tonic-gate 	 * by a debugger through /proc or for dumping via core().
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
8780Sstevel@tonic-gate 	t->t_proc_flag |= TP_STOPPING;	/* must set before dropping p_lock */
8790Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8800Sstevel@tonic-gate 	stoptime = gethrtime();
8810Sstevel@tonic-gate 	prstop(why, what);
8820Sstevel@tonic-gate 	(void) undo_watch_step(NULL);
8830Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
8840Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	switch (why) {
8870Sstevel@tonic-gate 	case PR_CHECKPOINT:
8880Sstevel@tonic-gate 		/*
8890Sstevel@tonic-gate 		 * The situation may have changed since we dropped
8900Sstevel@tonic-gate 		 * and reacquired p->p_lock. Double-check now
8910Sstevel@tonic-gate 		 * whether we should stop or not.
8920Sstevel@tonic-gate 		 */
8930Sstevel@tonic-gate 		if (!(t->t_proc_flag & TP_CHKPT)) {
8940Sstevel@tonic-gate 			t->t_proc_flag &= ~TP_STOPPING;
8950Sstevel@tonic-gate 			return;
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_CHKPT;
8980Sstevel@tonic-gate 		flags &= ~TS_RESUME;
8990Sstevel@tonic-gate 		break;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	case PR_JOBCONTROL:
9020Sstevel@tonic-gate 		ASSERT(what == SIGSTOP || what == SIGTSTP ||
9035297Sbpramod 		    what == SIGTTIN || what == SIGTTOU);
9040Sstevel@tonic-gate 		flags &= ~TS_XSTART;
9050Sstevel@tonic-gate 		break;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	case PR_SUSPENDED:
9080Sstevel@tonic-gate 		ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
9090Sstevel@tonic-gate 		/*
9100Sstevel@tonic-gate 		 * The situation may have changed since we dropped
9110Sstevel@tonic-gate 		 * and reacquired p->p_lock.  Double-check now
9120Sstevel@tonic-gate 		 * whether we should stop or not.
9130Sstevel@tonic-gate 		 */
9140Sstevel@tonic-gate 		if (what == SUSPEND_PAUSE) {
9150Sstevel@tonic-gate 			if (!(t->t_proc_flag & TP_PAUSE)) {
9160Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_STOPPING;
9170Sstevel@tonic-gate 				return;
9180Sstevel@tonic-gate 			}
9190Sstevel@tonic-gate 			flags &= ~TS_UNPAUSE;
9200Sstevel@tonic-gate 		} else {
9210Sstevel@tonic-gate 			if (!((t->t_proc_flag & TP_HOLDLWP) ||
9220Sstevel@tonic-gate 			    (p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
9230Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_STOPPING;
9240Sstevel@tonic-gate 				return;
9250Sstevel@tonic-gate 			}
9260Sstevel@tonic-gate 			/*
9270Sstevel@tonic-gate 			 * If SHOLDFORK is in effect and we are stopping
9280Sstevel@tonic-gate 			 * while asleep (not at the top of the stack),
9290Sstevel@tonic-gate 			 * we return now to allow the hold to take effect
9300Sstevel@tonic-gate 			 * when we reach the top of the kernel stack.
9310Sstevel@tonic-gate 			 */
9320Sstevel@tonic-gate 			if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
9330Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_STOPPING;
9340Sstevel@tonic-gate 				return;
9350Sstevel@tonic-gate 			}
9360Sstevel@tonic-gate 			flags &= ~TS_CSTART;
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 		break;
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	default:	/* /proc stop */
9410Sstevel@tonic-gate 		flags &= ~TS_PSTART;
9420Sstevel@tonic-gate 		/*
9430Sstevel@tonic-gate 		 * Do synchronous stop unless the async-stop flag is set.
9440Sstevel@tonic-gate 		 * If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
9450Sstevel@tonic-gate 		 * then no debugger is present and we also do synchronous stop.
9460Sstevel@tonic-gate 		 */
9470Sstevel@tonic-gate 		if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
9480Sstevel@tonic-gate 		    !(p->p_proc_flag & P_PR_ASYNC)) {
9490Sstevel@tonic-gate 			int notify;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 			for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
9520Sstevel@tonic-gate 				notify = 0;
9530Sstevel@tonic-gate 				thread_lock(tx);
9540Sstevel@tonic-gate 				if (ISTOPPED(tx) ||
9550Sstevel@tonic-gate 				    (tx->t_proc_flag & TP_PRSTOP)) {
9560Sstevel@tonic-gate 					thread_unlock(tx);
9570Sstevel@tonic-gate 					continue;
9580Sstevel@tonic-gate 				}
9590Sstevel@tonic-gate 				tx->t_proc_flag |= TP_PRSTOP;
9600Sstevel@tonic-gate 				tx->t_sig_check = 1;
9610Sstevel@tonic-gate 				if (tx->t_state == TS_SLEEP &&
9620Sstevel@tonic-gate 				    (tx->t_flag & T_WAKEABLE)) {
9630Sstevel@tonic-gate 					/*
9640Sstevel@tonic-gate 					 * Don't actually wake it up if it's
9650Sstevel@tonic-gate 					 * in one of the lwp_*() syscalls.
9660Sstevel@tonic-gate 					 * Mark it virtually stopped and
9670Sstevel@tonic-gate 					 * notify /proc waiters (below).
9680Sstevel@tonic-gate 					 */
9690Sstevel@tonic-gate 					if (tx->t_wchan0 == NULL)
9700Sstevel@tonic-gate 						setrun_locked(tx);
9710Sstevel@tonic-gate 					else {
9720Sstevel@tonic-gate 						tx->t_proc_flag |= TP_PRVSTOP;
9730Sstevel@tonic-gate 						tx->t_stoptime = stoptime;
9740Sstevel@tonic-gate 						notify = 1;
9750Sstevel@tonic-gate 					}
9760Sstevel@tonic-gate 				}
9773792Sakolb 
9783792Sakolb 				/* Move waiting thread to run queue */
9793792Sakolb 				if (ISWAITING(tx))
9803792Sakolb 					setrun_locked(tx);
9813792Sakolb 
9820Sstevel@tonic-gate 				/*
9830Sstevel@tonic-gate 				 * force the thread into the kernel
9840Sstevel@tonic-gate 				 * if it is not already there.
9850Sstevel@tonic-gate 				 */
9860Sstevel@tonic-gate 				if (tx->t_state == TS_ONPROC &&
9870Sstevel@tonic-gate 				    tx->t_cpu != CPU)
9880Sstevel@tonic-gate 					poke_cpu(tx->t_cpu->cpu_id);
9890Sstevel@tonic-gate 				thread_unlock(tx);
9900Sstevel@tonic-gate 				lep = p->p_lwpdir[tx->t_dslot].ld_entry;
9910Sstevel@tonic-gate 				if (notify && lep->le_trace)
9920Sstevel@tonic-gate 					prnotify(lep->le_trace);
9930Sstevel@tonic-gate 			}
9940Sstevel@tonic-gate 			/*
9950Sstevel@tonic-gate 			 * We do this just in case one of the threads we asked
9960Sstevel@tonic-gate 			 * to stop is in holdlwps() (called from cfork()) or
9970Sstevel@tonic-gate 			 * lwp_suspend().
9980Sstevel@tonic-gate 			 */
9990Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
10000Sstevel@tonic-gate 		}
10010Sstevel@tonic-gate 		break;
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	t->t_stoptime = stoptime;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (why == PR_JOBCONTROL || (why == PR_SUSPENDED && p->p_stopsig)) {
10070Sstevel@tonic-gate 		/*
10080Sstevel@tonic-gate 		 * Determine if the whole process is jobstopped.
10090Sstevel@tonic-gate 		 */
10100Sstevel@tonic-gate 		if (jobstopped(p)) {
10110Sstevel@tonic-gate 			sigqueue_t *sqp;
10120Sstevel@tonic-gate 			int sig;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 			if ((sig = p->p_stopsig) == 0)
10150Sstevel@tonic-gate 				p->p_stopsig = (uchar_t)(sig = what);
10160Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
10170Sstevel@tonic-gate 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
10180Sstevel@tonic-gate 			mutex_enter(&pidlock);
10190Sstevel@tonic-gate 			/*
10200Sstevel@tonic-gate 			 * The last lwp to stop notifies the parent.
10210Sstevel@tonic-gate 			 * Turn off the CLDCONT flag now so the first
10220Sstevel@tonic-gate 			 * lwp to continue knows what to do.
10230Sstevel@tonic-gate 			 */
10240Sstevel@tonic-gate 			p->p_pidflag &= ~CLDCONT;
10250Sstevel@tonic-gate 			p->p_wcode = CLD_STOPPED;
10260Sstevel@tonic-gate 			p->p_wdata = sig;
10270Sstevel@tonic-gate 			sigcld(p, sqp);
10280Sstevel@tonic-gate 			/*
10290Sstevel@tonic-gate 			 * Grab p->p_lock before releasing pidlock so the
10300Sstevel@tonic-gate 			 * parent and the child don't have a race condition.
10310Sstevel@tonic-gate 			 */
10320Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
10330Sstevel@tonic-gate 			mutex_exit(&pidlock);
10340Sstevel@tonic-gate 			p->p_stopsig = 0;
10350Sstevel@tonic-gate 		} else if (why == PR_JOBCONTROL && p->p_stopsig == 0) {
10360Sstevel@tonic-gate 			/*
10370Sstevel@tonic-gate 			 * Set p->p_stopsig and wake up sleeping lwps
10380Sstevel@tonic-gate 			 * so they will stop in sympathy with this lwp.
10390Sstevel@tonic-gate 			 */
10400Sstevel@tonic-gate 			p->p_stopsig = (uchar_t)what;
10410Sstevel@tonic-gate 			pokelwps(p);
10420Sstevel@tonic-gate 			/*
10430Sstevel@tonic-gate 			 * We do this just in case one of the threads we asked
10440Sstevel@tonic-gate 			 * to stop is in holdlwps() (called from cfork()) or
10450Sstevel@tonic-gate 			 * lwp_suspend().
10460Sstevel@tonic-gate 			 */
10470Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	if (why != PR_JOBCONTROL && why != PR_CHECKPOINT) {
10520Sstevel@tonic-gate 		/*
10530Sstevel@tonic-gate 		 * Do process-level notification when all lwps are
10540Sstevel@tonic-gate 		 * either stopped on events of interest to /proc
10550Sstevel@tonic-gate 		 * or are stopped showing PR_SUSPENDED or are zombies.
10560Sstevel@tonic-gate 		 */
10570Sstevel@tonic-gate 		procstop = 1;
10580Sstevel@tonic-gate 		for (tx = t->t_forw; procstop && tx != t; tx = tx->t_forw) {
10590Sstevel@tonic-gate 			if (VSTOPPED(tx))
10600Sstevel@tonic-gate 				continue;
10610Sstevel@tonic-gate 			thread_lock(tx);
10620Sstevel@tonic-gate 			switch (tx->t_state) {
10630Sstevel@tonic-gate 			case TS_ZOMB:
10640Sstevel@tonic-gate 				break;
10650Sstevel@tonic-gate 			case TS_STOPPED:
10660Sstevel@tonic-gate 				/* neither ISTOPPED nor SUSPENDED? */
10670Sstevel@tonic-gate 				if ((tx->t_schedflag &
10680Sstevel@tonic-gate 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART)) ==
10690Sstevel@tonic-gate 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART))
10700Sstevel@tonic-gate 					procstop = 0;
10710Sstevel@tonic-gate 				break;
10720Sstevel@tonic-gate 			case TS_SLEEP:
10730Sstevel@tonic-gate 				/* not paused for watchpoints? */
10740Sstevel@tonic-gate 				if (!(tx->t_flag & T_WAKEABLE) ||
10750Sstevel@tonic-gate 				    tx->t_wchan0 == NULL ||
10760Sstevel@tonic-gate 				    !(tx->t_proc_flag & TP_PAUSE))
10770Sstevel@tonic-gate 					procstop = 0;
10780Sstevel@tonic-gate 				break;
10790Sstevel@tonic-gate 			default:
10800Sstevel@tonic-gate 				procstop = 0;
10810Sstevel@tonic-gate 				break;
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			thread_unlock(tx);
10840Sstevel@tonic-gate 		}
10850Sstevel@tonic-gate 		if (procstop) {
10860Sstevel@tonic-gate 			/* there must not be any remapped watched pages now */
10870Sstevel@tonic-gate 			ASSERT(p->p_mapcnt == 0);
10880Sstevel@tonic-gate 			if (p->p_proc_flag & P_PR_PTRACE) {
10890Sstevel@tonic-gate 				/* ptrace() compatibility */
10900Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
10910Sstevel@tonic-gate 				mutex_enter(&pidlock);
10920Sstevel@tonic-gate 				p->p_wcode = CLD_TRAPPED;
10930Sstevel@tonic-gate 				p->p_wdata = (why == PR_SIGNALLED)?
10940Sstevel@tonic-gate 				    what : SIGTRAP;
10950Sstevel@tonic-gate 				cv_broadcast(&p->p_parent->p_cv);
10960Sstevel@tonic-gate 				/*
10970Sstevel@tonic-gate 				 * Grab p->p_lock before releasing pidlock so
10980Sstevel@tonic-gate 				 * parent and child don't have a race condition.
10990Sstevel@tonic-gate 				 */
11000Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
11010Sstevel@tonic-gate 				mutex_exit(&pidlock);
11020Sstevel@tonic-gate 			}
11030Sstevel@tonic-gate 			if (p->p_trace)			/* /proc */
11040Sstevel@tonic-gate 				prnotify(p->p_trace);
11050Sstevel@tonic-gate 			cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */
11060Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);	/* holdwatch() */
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 		if (why != PR_SUSPENDED) {
11090Sstevel@tonic-gate 			lep = p->p_lwpdir[t->t_dslot].ld_entry;
11100Sstevel@tonic-gate 			if (lep->le_trace)		/* /proc */
11110Sstevel@tonic-gate 				prnotify(lep->le_trace);
11120Sstevel@tonic-gate 			/*
11130Sstevel@tonic-gate 			 * Special notification for creation of the agent lwp.
11140Sstevel@tonic-gate 			 */
11150Sstevel@tonic-gate 			if (t == p->p_agenttp &&
11160Sstevel@tonic-gate 			    (t->t_proc_flag & TP_PRSTOP) &&
11170Sstevel@tonic-gate 			    p->p_trace)
11180Sstevel@tonic-gate 				prnotify(p->p_trace);
11190Sstevel@tonic-gate 			/*
11200Sstevel@tonic-gate 			 * The situation may have changed since we dropped
11210Sstevel@tonic-gate 			 * and reacquired p->p_lock. Double-check now
11220Sstevel@tonic-gate 			 * whether we should stop or not.
11230Sstevel@tonic-gate 			 */
11240Sstevel@tonic-gate 			if (!(t->t_proc_flag & TP_STOPPING)) {
11250Sstevel@tonic-gate 				if (t->t_proc_flag & TP_PRSTOP)
11260Sstevel@tonic-gate 					t->t_proc_flag |= TP_STOPPING;
11270Sstevel@tonic-gate 			}
11280Sstevel@tonic-gate 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
11290Sstevel@tonic-gate 			prnostep(lwp);
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if (why == PR_SUSPENDED) {
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		/*
11360Sstevel@tonic-gate 		 * We always broadcast in the case of SUSPEND_PAUSE.  This is
11370Sstevel@tonic-gate 		 * because checks for TP_PAUSE take precedence over checks for
11380Sstevel@tonic-gate 		 * SHOLDWATCH.  If a thread is trying to stop because of
11390Sstevel@tonic-gate 		 * SUSPEND_PAUSE and tries to do a holdwatch(), it will be
11400Sstevel@tonic-gate 		 * waiting for the rest of the threads to enter a stopped state.
11410Sstevel@tonic-gate 		 * If we are stopping for a SUSPEND_PAUSE, we may be the last
11420Sstevel@tonic-gate 		 * lwp and not know it, so broadcast just in case.
11430Sstevel@tonic-gate 		 */
11440Sstevel@tonic-gate 		if (what == SUSPEND_PAUSE ||
11450Sstevel@tonic-gate 		    --p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP))
11460Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/*
11510Sstevel@tonic-gate 	 * Need to do this here (rather than after the thread is officially
11520Sstevel@tonic-gate 	 * stopped) because we can't call mutex_enter from a stopped thread.
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	if (why == PR_CHECKPOINT)
11550Sstevel@tonic-gate 		del_one_utstop();
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	thread_lock(t);
11580Sstevel@tonic-gate 	ASSERT((t->t_schedflag & TS_ALLSTART) == 0);
11590Sstevel@tonic-gate 	t->t_schedflag |= flags;
11600Sstevel@tonic-gate 	t->t_whystop = (short)why;
11610Sstevel@tonic-gate 	t->t_whatstop = (short)what;
11620Sstevel@tonic-gate 	CL_STOP(t, why, what);
11630Sstevel@tonic-gate 	(void) new_mstate(t, LMS_STOPPED);
11640Sstevel@tonic-gate 	thread_stop(t);			/* set stop state and drop lock */
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
11670Sstevel@tonic-gate 		/*
11680Sstevel@tonic-gate 		 * We may have gotten a SIGKILL or a SIGCONT when
11690Sstevel@tonic-gate 		 * we released p->p_lock; make one last check.
11700Sstevel@tonic-gate 		 * Also check for a /proc run-on-last-close.
11710Sstevel@tonic-gate 		 */
11720Sstevel@tonic-gate 		if (sigismember(&t->t_sig, SIGKILL) ||
11730Sstevel@tonic-gate 		    sigismember(&p->p_sig, SIGKILL) ||
11740Sstevel@tonic-gate 		    (t->t_proc_flag & TP_LWPEXIT) ||
11750Sstevel@tonic-gate 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
11760Sstevel@tonic-gate 			p->p_stopsig = 0;
11770Sstevel@tonic-gate 			thread_lock(t);
11780Sstevel@tonic-gate 			t->t_schedflag |= TS_XSTART | TS_PSTART;
11790Sstevel@tonic-gate 			setrun_locked(t);
11800Sstevel@tonic-gate 			thread_unlock_nopreempt(t);
11810Sstevel@tonic-gate 		} else if (why == PR_JOBCONTROL) {
11820Sstevel@tonic-gate 			if (p->p_flag & SSCONT) {
11830Sstevel@tonic-gate 				/*
11840Sstevel@tonic-gate 				 * This resulted from a SIGCONT posted
11850Sstevel@tonic-gate 				 * while we were not holding p->p_lock.
11860Sstevel@tonic-gate 				 */
11870Sstevel@tonic-gate 				p->p_stopsig = 0;
11880Sstevel@tonic-gate 				thread_lock(t);
11890Sstevel@tonic-gate 				t->t_schedflag |= TS_XSTART;
11900Sstevel@tonic-gate 				setrun_locked(t);
11910Sstevel@tonic-gate 				thread_unlock_nopreempt(t);
11920Sstevel@tonic-gate 			}
11930Sstevel@tonic-gate 		} else if (!(t->t_proc_flag & TP_STOPPING)) {
11940Sstevel@tonic-gate 			/*
11950Sstevel@tonic-gate 			 * This resulted from a /proc run-on-last-close.
11960Sstevel@tonic-gate 			 */
11970Sstevel@tonic-gate 			thread_lock(t);
11980Sstevel@tonic-gate 			t->t_schedflag |= TS_PSTART;
11990Sstevel@tonic-gate 			setrun_locked(t);
12000Sstevel@tonic-gate 			thread_unlock_nopreempt(t);
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_STOPPING;
12050Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	swtch();
12080Sstevel@tonic-gate 	setallwatch();	/* reestablish any watchpoints set while stopped */
12090Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
12100Sstevel@tonic-gate 	prbarrier(p);	/* barrier against /proc locking */
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate /* Interface for resetting user thread stop count. */
12140Sstevel@tonic-gate void
utstop_init(void)12150Sstevel@tonic-gate utstop_init(void)
12160Sstevel@tonic-gate {
12170Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12180Sstevel@tonic-gate 	num_utstop = 0;
12190Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate /* Interface for registering a user thread stop request. */
12230Sstevel@tonic-gate void
add_one_utstop(void)12240Sstevel@tonic-gate add_one_utstop(void)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12270Sstevel@tonic-gate 	num_utstop++;
12280Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /* Interface for cancelling a user thread stop request */
12320Sstevel@tonic-gate void
del_one_utstop(void)12330Sstevel@tonic-gate del_one_utstop(void)
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12360Sstevel@tonic-gate 	num_utstop--;
12370Sstevel@tonic-gate 	if (num_utstop == 0)
12380Sstevel@tonic-gate 		cv_broadcast(&utstop_cv);
12390Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate /* Interface to wait for all user threads to be stopped */
12430Sstevel@tonic-gate void
utstop_timedwait(clock_t ticks)12440Sstevel@tonic-gate utstop_timedwait(clock_t ticks)
12450Sstevel@tonic-gate {
12460Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12470Sstevel@tonic-gate 	if (num_utstop > 0)
124811066Srafael.vanoni@sun.com 		(void) cv_reltimedwait(&utstop_cv, &thread_stop_lock, ticks,
124911066Srafael.vanoni@sun.com 		    TR_CLOCK_TICK);
12500Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12510Sstevel@tonic-gate }
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate /*
12540Sstevel@tonic-gate  * Perform the action specified by the current signal.
12550Sstevel@tonic-gate  * The usual sequence is:
12560Sstevel@tonic-gate  * 	if (issig())
12570Sstevel@tonic-gate  * 		psig();
12580Sstevel@tonic-gate  * The signal bit has already been cleared by issig(),
12590Sstevel@tonic-gate  * the current signal number has been stored in lwp_cursig,
12600Sstevel@tonic-gate  * and the current siginfo is now referenced by lwp_curinfo.
12610Sstevel@tonic-gate  */
12620Sstevel@tonic-gate void
psig(void)12630Sstevel@tonic-gate psig(void)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	kthread_t *t = curthread;
12660Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
12670Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
12680Sstevel@tonic-gate 	void (*func)();
12690Sstevel@tonic-gate 	int sig, rc, code, ext;
12700Sstevel@tonic-gate 	pid_t pid = -1;
12710Sstevel@tonic-gate 	id_t ctid = 0;
12720Sstevel@tonic-gate 	zoneid_t zoneid = -1;
12730Sstevel@tonic-gate 	sigqueue_t *sqp = NULL;
127411861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
12770Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
12780Sstevel@tonic-gate 	code = CLD_KILLED;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS) {
12810Sstevel@tonic-gate 		lwp_exit();
12820Sstevel@tonic-gate 		return;			/* not reached */
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 	sig = lwp->lwp_cursig;
12850Sstevel@tonic-gate 	ext = lwp->lwp_extsig;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	ASSERT(sig < NSIG);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	/*
12900Sstevel@tonic-gate 	 * Re-check lwp_cursig after we acquire p_lock.  Since p_lock was
12910Sstevel@tonic-gate 	 * dropped between issig() and psig(), a debugger may have cleared
12920Sstevel@tonic-gate 	 * lwp_cursig via /proc in the intervening window.
12930Sstevel@tonic-gate 	 */
12940Sstevel@tonic-gate 	if (sig == 0) {
12950Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
12960Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
12970Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
12980Sstevel@tonic-gate 		}
12990Sstevel@tonic-gate 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
13000Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
13010Sstevel@tonic-gate 			t->t_hold = lwp->lwp_sigoldmask;
13020Sstevel@tonic-gate 		}
13030Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13040Sstevel@tonic-gate 		return;
13050Sstevel@tonic-gate 	}
13063446Smrj 	func = PTOU(curproc)->u_signal[sig-1];
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/*
13090Sstevel@tonic-gate 	 * The signal disposition could have changed since we promoted
13100Sstevel@tonic-gate 	 * this signal from pending to current (we dropped p->p_lock).
13110Sstevel@tonic-gate 	 * This can happen only in a multi-threaded process.
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	if (sigismember(&p->p_ignore, sig) ||
13140Sstevel@tonic-gate 	    (func == SIG_DFL && sigismember(&stopdefault, sig))) {
13150Sstevel@tonic-gate 		lwp->lwp_cursig = 0;
13160Sstevel@tonic-gate 		lwp->lwp_extsig = 0;
13170Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
13180Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
13190Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
13200Sstevel@tonic-gate 		}
13210Sstevel@tonic-gate 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
13220Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
13230Sstevel@tonic-gate 			t->t_hold = lwp->lwp_sigoldmask;
13240Sstevel@tonic-gate 		}
13250Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13260Sstevel@tonic-gate 		return;
13270Sstevel@tonic-gate 	}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	/*
13300Sstevel@tonic-gate 	 * We check lwp_curinfo first since pr_setsig can actually
13310Sstevel@tonic-gate 	 * stuff a sigqueue_t there for SIGKILL.
13320Sstevel@tonic-gate 	 */
13330Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
13340Sstevel@tonic-gate 		sqp = lwp->lwp_curinfo;
13350Sstevel@tonic-gate 	} else if (sig == SIGKILL && p->p_killsqp) {
13360Sstevel@tonic-gate 		sqp = p->p_killsqp;
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	if (sqp != NULL) {
13400Sstevel@tonic-gate 		if (SI_FROMUSER(&sqp->sq_info)) {
13410Sstevel@tonic-gate 			pid = sqp->sq_info.si_pid;
13420Sstevel@tonic-gate 			ctid = sqp->sq_info.si_ctid;
13430Sstevel@tonic-gate 			zoneid = sqp->sq_info.si_zoneid;
13440Sstevel@tonic-gate 		}
13450Sstevel@tonic-gate 		/*
13460Sstevel@tonic-gate 		 * If we have a sigqueue_t, its sq_external value
13470Sstevel@tonic-gate 		 * trumps the lwp_extsig value.  It is theoretically
13480Sstevel@tonic-gate 		 * possible to make lwp_extsig reflect reality, but it
13490Sstevel@tonic-gate 		 * would unnecessarily complicate things elsewhere.
13500Sstevel@tonic-gate 		 */
13510Sstevel@tonic-gate 		ext = sqp->sq_external;
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if (func == SIG_DFL) {
13550Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13560Sstevel@tonic-gate 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
13570Sstevel@tonic-gate 		    NULL, void (*)(void), func);
13580Sstevel@tonic-gate 	} else {
13590Sstevel@tonic-gate 		k_siginfo_t *sip = NULL;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		/*
13620Sstevel@tonic-gate 		 * If DTrace user-land tracing is active, give DTrace a
13630Sstevel@tonic-gate 		 * chance to defer the signal until after tracing is
13640Sstevel@tonic-gate 		 * complete.
13650Sstevel@tonic-gate 		 */
13660Sstevel@tonic-gate 		if (t->t_dtrace_on && dtrace_safe_defer_signal()) {
13670Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
13680Sstevel@tonic-gate 			return;
13690Sstevel@tonic-gate 		}
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		/*
13720Sstevel@tonic-gate 		 * save siginfo pointer here, in case the
13730Sstevel@tonic-gate 		 * the signal's reset bit is on
13740Sstevel@tonic-gate 		 *
13750Sstevel@tonic-gate 		 * The presence of a current signal prevents paging
13760Sstevel@tonic-gate 		 * from succeeding over a network.  We copy the current
13770Sstevel@tonic-gate 		 * signal information to the side and cancel the current
13780Sstevel@tonic-gate 		 * signal so that sendsig() will succeed.
13790Sstevel@tonic-gate 		 */
13800Sstevel@tonic-gate 		if (sigismember(&p->p_siginfo, sig)) {
13814626Sraf 			sip = &lwp->lwp_siginfo;
13820Sstevel@tonic-gate 			if (sqp) {
13834626Sraf 				bcopy(&sqp->sq_info, sip, sizeof (*sip));
13845891Sraf 				/*
13855891Sraf 				 * If we were interrupted out of a system call
13865891Sraf 				 * due to pthread_cancel(), inform libc.
13875891Sraf 				 */
13885891Sraf 				if (sig == SIGCANCEL &&
13895891Sraf 				    sip->si_code == SI_LWP &&
13905891Sraf 				    t->t_sysnum != 0)
13915891Sraf 					schedctl_cancel_eintr();
13924626Sraf 			} else if (sig == SIGPROF && sip->si_signo == SIGPROF &&
13934626Sraf 			    t->t_rprof != NULL && t->t_rprof->rp_anystate) {
13944626Sraf 				/* EMPTY */;
13954626Sraf 			} else {
13964626Sraf 				bzero(sip, sizeof (*sip));
13974626Sraf 				sip->si_signo = sig;
13984626Sraf 				sip->si_code = SI_NOINFO;
13990Sstevel@tonic-gate 			}
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		if (t->t_flag & T_TOMASK)
14030Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
14040Sstevel@tonic-gate 		else
14050Sstevel@tonic-gate 			lwp->lwp_sigoldmask = t->t_hold;
14063446Smrj 		sigorset(&t->t_hold, &PTOU(curproc)->u_sigmask[sig-1]);
14073446Smrj 		if (!sigismember(&PTOU(curproc)->u_signodefer, sig))
14080Sstevel@tonic-gate 			sigaddset(&t->t_hold, sig);
14093446Smrj 		if (sigismember(&PTOU(curproc)->u_sigresethand, sig))
1410*11913SRoger.Faulkner@Sun.COM 			setsigact(sig, SIG_DFL, &nullsmask, 0);
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
14130Sstevel@tonic-gate 		    sip, void (*)(void), func);
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 		lwp->lwp_cursig = 0;
14160Sstevel@tonic-gate 		lwp->lwp_extsig = 0;
14170Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
14180Sstevel@tonic-gate 			/* p->p_killsqp is freed by freeproc */
14190Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
14200Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
14210Sstevel@tonic-gate 		}
14220Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14230Sstevel@tonic-gate 		lwp->lwp_ru.nsignals++;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE)
14260Sstevel@tonic-gate 			rc = sendsig(sig, sip, func);
14270Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
14280Sstevel@tonic-gate 		else
14290Sstevel@tonic-gate 			rc = sendsig32(sig, sip, func);
14300Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
14310Sstevel@tonic-gate 		if (rc)
14320Sstevel@tonic-gate 			return;
14330Sstevel@tonic-gate 		sig = lwp->lwp_cursig = SIGSEGV;
14340Sstevel@tonic-gate 		ext = 0;	/* lwp_extsig was set above */
14350Sstevel@tonic-gate 		pid = -1;
14360Sstevel@tonic-gate 		ctid = 0;
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	if (sigismember(&coredefault, sig)) {
14400Sstevel@tonic-gate 		/*
14410Sstevel@tonic-gate 		 * Terminate all LWPs but don't discard them.
14420Sstevel@tonic-gate 		 * If another lwp beat us to the punch by calling exit(),
14430Sstevel@tonic-gate 		 * evaporate now.
14440Sstevel@tonic-gate 		 */
1445390Sraf 		proc_is_exiting(p);
14460Sstevel@tonic-gate 		if (exitlwps(1) != 0) {
14470Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
14480Sstevel@tonic-gate 			lwp_exit();
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 		/* if we got a SIGKILL from anywhere, no core dump */
14510Sstevel@tonic-gate 		if (p->p_flag & SKILLED) {
14520Sstevel@tonic-gate 			sig = SIGKILL;
14530Sstevel@tonic-gate 			ext = (p->p_flag & SEXTKILLED) != 0;
14540Sstevel@tonic-gate 		} else {
145511861SMarek.Pospisil@Sun.COM 			if (auditing)		/* audit core dump */
14560Sstevel@tonic-gate 				audit_core_start(sig);
14570Sstevel@tonic-gate 			if (core(sig, ext) == 0)
14580Sstevel@tonic-gate 				code = CLD_DUMPED;
145911861SMarek.Pospisil@Sun.COM 			if (auditing)		/* audit core dump */
14600Sstevel@tonic-gate 				audit_core_finish(code);
14610Sstevel@tonic-gate 		}
14620Sstevel@tonic-gate 	}
14635297Sbpramod 
14645297Sbpramod 	/*
14655297Sbpramod 	 * Generate a contract event once if the process is killed
14665297Sbpramod 	 * by a signal.
14675297Sbpramod 	 */
14685297Sbpramod 	if (ext) {
14695297Sbpramod 		proc_is_exiting(p);
14705297Sbpramod 		if (exitlwps(0) != 0) {
14715297Sbpramod 			mutex_enter(&p->p_lock);
14725297Sbpramod 			lwp_exit();
14735297Sbpramod 		}
14740Sstevel@tonic-gate 		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
14750Sstevel@tonic-gate 		    zoneid);
14765297Sbpramod 	}
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 	exit(code, sig);
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate /*
14820Sstevel@tonic-gate  * Find next unheld signal in ssp for thread t.
14830Sstevel@tonic-gate  */
14840Sstevel@tonic-gate int
fsig(k_sigset_t * ssp,kthread_t * t)14850Sstevel@tonic-gate fsig(k_sigset_t *ssp, kthread_t *t)
14860Sstevel@tonic-gate {
14870Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
14880Sstevel@tonic-gate 	user_t *up = PTOU(p);
14890Sstevel@tonic-gate 	int i;
14900Sstevel@tonic-gate 	k_sigset_t temp;
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	/*
14950Sstevel@tonic-gate 	 * Don't promote any signals for the parent of a vfork()d
14960Sstevel@tonic-gate 	 * child that hasn't yet released the parent's memory.
14970Sstevel@tonic-gate 	 */
14980Sstevel@tonic-gate 	if (p->p_flag & SVFWAIT)
14990Sstevel@tonic-gate 		return (0);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	temp = *ssp;
15020Sstevel@tonic-gate 	sigdiffset(&temp, &t->t_hold);
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Don't promote stopping signals (except SIGSTOP) for a child
15060Sstevel@tonic-gate 	 * of vfork() that hasn't yet released the parent's memory.
15070Sstevel@tonic-gate 	 */
15080Sstevel@tonic-gate 	if (p->p_flag & SVFORK)
15090Sstevel@tonic-gate 		sigdiffset(&temp, &holdvfork);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	/*
15120Sstevel@tonic-gate 	 * Don't promote a signal that will stop
15130Sstevel@tonic-gate 	 * the process when lwp_nostop is set.
15140Sstevel@tonic-gate 	 */
15150Sstevel@tonic-gate 	if (ttolwp(t)->lwp_nostop) {
15160Sstevel@tonic-gate 		sigdelset(&temp, SIGSTOP);
15170Sstevel@tonic-gate 		if (!p->p_pgidp->pid_pgorphaned) {
15180Sstevel@tonic-gate 			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
15190Sstevel@tonic-gate 				sigdelset(&temp, SIGTSTP);
15200Sstevel@tonic-gate 			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
15210Sstevel@tonic-gate 				sigdelset(&temp, SIGTTIN);
15220Sstevel@tonic-gate 			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
15230Sstevel@tonic-gate 				sigdelset(&temp, SIGTTOU);
15240Sstevel@tonic-gate 		}
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * Choose SIGKILL and SIGPROF before all other pending signals.
15290Sstevel@tonic-gate 	 * The rest are promoted in signal number order.
15300Sstevel@tonic-gate 	 */
15310Sstevel@tonic-gate 	if (sigismember(&temp, SIGKILL))
15320Sstevel@tonic-gate 		return (SIGKILL);
15330Sstevel@tonic-gate 	if (sigismember(&temp, SIGPROF))
15340Sstevel@tonic-gate 		return (SIGPROF);
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
15370Sstevel@tonic-gate 		if (temp.__sigbits[i])
15380Sstevel@tonic-gate 			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
15390Sstevel@tonic-gate 			    lowbit(temp.__sigbits[i]));
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	return (0);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate void
setsigact(int sig,void (* disp)(),const k_sigset_t * mask,int flags)1546*11913SRoger.Faulkner@Sun.COM setsigact(int sig, void (*disp)(), const k_sigset_t *mask, int flags)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
15490Sstevel@tonic-gate 	kthread_t *t;
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
15520Sstevel@tonic-gate 
15533446Smrj 	PTOU(curproc)->u_signal[sig - 1] = disp;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	/*
15560Sstevel@tonic-gate 	 * Honor the SA_SIGINFO flag if the signal is being caught.
15570Sstevel@tonic-gate 	 * Force the SA_SIGINFO flag if the signal is not being caught.
15580Sstevel@tonic-gate 	 * This is necessary to make sigqueue() and sigwaitinfo() work
15590Sstevel@tonic-gate 	 * properly together when the signal is set to default or is
15600Sstevel@tonic-gate 	 * being temporarily ignored.
15610Sstevel@tonic-gate 	 */
15620Sstevel@tonic-gate 	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
15630Sstevel@tonic-gate 		sigaddset(&p->p_siginfo, sig);
15640Sstevel@tonic-gate 	else
15650Sstevel@tonic-gate 		sigdelset(&p->p_siginfo, sig);
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	if (disp != SIG_DFL && disp != SIG_IGN) {
15680Sstevel@tonic-gate 		sigdelset(&p->p_ignore, sig);
1569*11913SRoger.Faulkner@Sun.COM 		PTOU(curproc)->u_sigmask[sig - 1] = *mask;
15700Sstevel@tonic-gate 		if (!sigismember(&cantreset, sig)) {
15710Sstevel@tonic-gate 			if (flags & SA_RESETHAND)
15723446Smrj 				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
15730Sstevel@tonic-gate 			else
15743446Smrj 				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 		if (flags & SA_NODEFER)
15773446Smrj 			sigaddset(&PTOU(curproc)->u_signodefer, sig);
15780Sstevel@tonic-gate 		else
15793446Smrj 			sigdelset(&PTOU(curproc)->u_signodefer, sig);
15800Sstevel@tonic-gate 		if (flags & SA_RESTART)
15813446Smrj 			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
15820Sstevel@tonic-gate 		else
15833446Smrj 			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
15840Sstevel@tonic-gate 		if (flags & SA_ONSTACK)
15853446Smrj 			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
15860Sstevel@tonic-gate 		else
15873446Smrj 			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
15880Sstevel@tonic-gate 	} else if (disp == SIG_IGN ||
15890Sstevel@tonic-gate 	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
15900Sstevel@tonic-gate 		/*
15910Sstevel@tonic-gate 		 * Setting the signal action to SIG_IGN results in the
15920Sstevel@tonic-gate 		 * discarding of all pending signals of that signal number.
15930Sstevel@tonic-gate 		 * Setting the signal action to SIG_DFL does the same *only*
15940Sstevel@tonic-gate 		 * if the signal's default behavior is to be ignored.
15950Sstevel@tonic-gate 		 */
15960Sstevel@tonic-gate 		sigaddset(&p->p_ignore, sig);
15970Sstevel@tonic-gate 		sigdelset(&p->p_sig, sig);
15980Sstevel@tonic-gate 		sigdelset(&p->p_extsig, sig);
15990Sstevel@tonic-gate 		sigdelq(p, NULL, sig);
16000Sstevel@tonic-gate 		t = p->p_tlist;
16010Sstevel@tonic-gate 		do {
16020Sstevel@tonic-gate 			sigdelset(&t->t_sig, sig);
16030Sstevel@tonic-gate 			sigdelset(&t->t_extsig, sig);
16040Sstevel@tonic-gate 			sigdelq(p, t, sig);
16050Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
16060Sstevel@tonic-gate 	} else {
16070Sstevel@tonic-gate 		/*
16080Sstevel@tonic-gate 		 * The signal action is being set to SIG_DFL and the default
16090Sstevel@tonic-gate 		 * behavior is to do something: make sure it is not ignored.
16100Sstevel@tonic-gate 		 */
16110Sstevel@tonic-gate 		sigdelset(&p->p_ignore, sig);
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	if (sig == SIGCLD) {
16150Sstevel@tonic-gate 		if (flags & SA_NOCLDWAIT)
16160Sstevel@tonic-gate 			p->p_flag |= SNOWAIT;
16170Sstevel@tonic-gate 		else
16180Sstevel@tonic-gate 			p->p_flag &= ~SNOWAIT;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 		if (flags & SA_NOCLDSTOP)
16210Sstevel@tonic-gate 			p->p_flag &= ~SJCTL;
16220Sstevel@tonic-gate 		else
16230Sstevel@tonic-gate 			p->p_flag |= SJCTL;
16240Sstevel@tonic-gate 
16253235Sraf 		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
16260Sstevel@tonic-gate 			proc_t *cp, *tp;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
16290Sstevel@tonic-gate 			mutex_enter(&pidlock);
16300Sstevel@tonic-gate 			for (cp = p->p_child; cp != NULL; cp = tp) {
16310Sstevel@tonic-gate 				tp = cp->p_sibling;
16323235Sraf 				if (cp->p_stat == SZOMB &&
16333235Sraf 				    !(cp->p_pidflag & CLDWAITPID))
16340Sstevel@tonic-gate 					freeproc(cp);
16350Sstevel@tonic-gate 			}
16360Sstevel@tonic-gate 			mutex_exit(&pidlock);
16370Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate /*
16430Sstevel@tonic-gate  * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
16440Sstevel@tonic-gate  * Called from exec_common() for a process undergoing execve()
16450Sstevel@tonic-gate  * and from cfork() for a newly-created child of vfork().
16460Sstevel@tonic-gate  * In the vfork() case, 'p' is not the current process.
16470Sstevel@tonic-gate  * In both cases, there is only one thread in the process.
16480Sstevel@tonic-gate  */
16490Sstevel@tonic-gate void
sigdefault(proc_t * p)16500Sstevel@tonic-gate sigdefault(proc_t *p)
16510Sstevel@tonic-gate {
16520Sstevel@tonic-gate 	kthread_t *t = p->p_tlist;
16530Sstevel@tonic-gate 	struct user *up = PTOU(p);
16540Sstevel@tonic-gate 	int sig;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	for (sig = 1; sig < NSIG; sig++) {
16590Sstevel@tonic-gate 		if (up->u_signal[sig - 1] != SIG_DFL &&
16600Sstevel@tonic-gate 		    up->u_signal[sig - 1] != SIG_IGN) {
16610Sstevel@tonic-gate 			up->u_signal[sig - 1] = SIG_DFL;
16620Sstevel@tonic-gate 			sigemptyset(&up->u_sigmask[sig - 1]);
16630Sstevel@tonic-gate 			if (sigismember(&ignoredefault, sig)) {
16640Sstevel@tonic-gate 				sigdelq(p, NULL, sig);
16650Sstevel@tonic-gate 				sigdelq(p, t, sig);
16660Sstevel@tonic-gate 			}
16670Sstevel@tonic-gate 			if (sig == SIGCLD)
16680Sstevel@tonic-gate 				p->p_flag &= ~(SNOWAIT|SJCTL);
16690Sstevel@tonic-gate 		}
16700Sstevel@tonic-gate 	}
16710Sstevel@tonic-gate 	sigorset(&p->p_ignore, &ignoredefault);
16720Sstevel@tonic-gate 	sigfillset(&p->p_siginfo);
16730Sstevel@tonic-gate 	sigdiffset(&p->p_siginfo, &cantmask);
16740Sstevel@tonic-gate 	sigdiffset(&p->p_sig, &ignoredefault);
16750Sstevel@tonic-gate 	sigdiffset(&p->p_extsig, &ignoredefault);
16760Sstevel@tonic-gate 	sigdiffset(&t->t_sig, &ignoredefault);
16770Sstevel@tonic-gate 	sigdiffset(&t->t_extsig, &ignoredefault);
16780Sstevel@tonic-gate }
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate void
sigcld(proc_t * cp,sigqueue_t * sqp)16810Sstevel@tonic-gate sigcld(proc_t *cp, sigqueue_t *sqp)
16820Sstevel@tonic-gate {
16830Sstevel@tonic-gate 	proc_t *pp = cp->p_parent;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	switch (cp->p_wcode) {
16880Sstevel@tonic-gate 	case CLD_EXITED:
16890Sstevel@tonic-gate 	case CLD_DUMPED:
16900Sstevel@tonic-gate 	case CLD_KILLED:
16910Sstevel@tonic-gate 		ASSERT(cp->p_stat == SZOMB);
16920Sstevel@tonic-gate 		/*
16930Sstevel@tonic-gate 		 * The broadcast on p_srwchan_cv is a kludge to
16940Sstevel@tonic-gate 		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
16950Sstevel@tonic-gate 		 */
16960Sstevel@tonic-gate 		cv_broadcast(&cp->p_srwchan_cv);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 		/*
16990Sstevel@tonic-gate 		 * Add to newstate list of the parent
17000Sstevel@tonic-gate 		 */
17010Sstevel@tonic-gate 		add_ns(pp, cp);
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 		cv_broadcast(&pp->p_cv);
17040Sstevel@tonic-gate 		if ((pp->p_flag & SNOWAIT) ||
17053235Sraf 		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
17063235Sraf 			if (!(cp->p_pidflag & CLDWAITPID))
17073235Sraf 				freeproc(cp);
17083235Sraf 		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
17090Sstevel@tonic-gate 			post_sigcld(cp, sqp);
17100Sstevel@tonic-gate 			sqp = NULL;
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 		break;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	case CLD_STOPPED:
17150Sstevel@tonic-gate 	case CLD_CONTINUED:
17160Sstevel@tonic-gate 		cv_broadcast(&pp->p_cv);
17170Sstevel@tonic-gate 		if (pp->p_flag & SJCTL) {
17180Sstevel@tonic-gate 			post_sigcld(cp, sqp);
17190Sstevel@tonic-gate 			sqp = NULL;
17200Sstevel@tonic-gate 		}
17210Sstevel@tonic-gate 		break;
17220Sstevel@tonic-gate 	}
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	if (sqp)
17250Sstevel@tonic-gate 		siginfofree(sqp);
17260Sstevel@tonic-gate }
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate /*
172910043SRoger.Faulkner@Sun.COM  * Common code called from sigcld() and from
173010043SRoger.Faulkner@Sun.COM  * waitid() and issig_forreal() via sigcld_repost().
17310Sstevel@tonic-gate  * Give the parent process a SIGCLD if it does not have one pending,
17320Sstevel@tonic-gate  * else mark the child process so a SIGCLD can be posted later.
17330Sstevel@tonic-gate  */
17340Sstevel@tonic-gate static void
post_sigcld(proc_t * cp,sigqueue_t * sqp)17350Sstevel@tonic-gate post_sigcld(proc_t *cp, sigqueue_t *sqp)
17360Sstevel@tonic-gate {
17370Sstevel@tonic-gate 	proc_t *pp = cp->p_parent;
17380Sstevel@tonic-gate 	k_siginfo_t info;
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
17410Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	/*
174410043SRoger.Faulkner@Sun.COM 	 * If a SIGCLD is pending, then just mark the child process
174510043SRoger.Faulkner@Sun.COM 	 * so that its SIGCLD will be posted later, when the first
174610043SRoger.Faulkner@Sun.COM 	 * SIGCLD is taken off the queue or when the parent is ready
174710043SRoger.Faulkner@Sun.COM 	 * to receive it or accept it, if ever.
17480Sstevel@tonic-gate 	 */
174910043SRoger.Faulkner@Sun.COM 	if (sigismember(&pp->p_sig, SIGCLD)) {
17500Sstevel@tonic-gate 		cp->p_pidflag |= CLDPEND;
175110043SRoger.Faulkner@Sun.COM 	} else {
17520Sstevel@tonic-gate 		cp->p_pidflag &= ~CLDPEND;
17530Sstevel@tonic-gate 		if (sqp == NULL) {
17540Sstevel@tonic-gate 			/*
17550Sstevel@tonic-gate 			 * This can only happen when the parent is init.
17560Sstevel@tonic-gate 			 * (See call to sigcld(q, NULL) in exit().)
17570Sstevel@tonic-gate 			 * Use KM_NOSLEEP to avoid deadlock.
17580Sstevel@tonic-gate 			 */
17590Sstevel@tonic-gate 			ASSERT(pp == proc_init);
17600Sstevel@tonic-gate 			winfo(cp, &info, 0);
17610Sstevel@tonic-gate 			sigaddq(pp, NULL, &info, KM_NOSLEEP);
17620Sstevel@tonic-gate 		} else {
17630Sstevel@tonic-gate 			winfo(cp, &sqp->sq_info, 0);
17640Sstevel@tonic-gate 			sigaddqa(pp, NULL, sqp);
17650Sstevel@tonic-gate 			sqp = NULL;
17660Sstevel@tonic-gate 		}
17670Sstevel@tonic-gate 	}
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	if (sqp)
17720Sstevel@tonic-gate 		siginfofree(sqp);
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate /*
17760Sstevel@tonic-gate  * Search for a child that has a pending SIGCLD for us, the parent.
17770Sstevel@tonic-gate  * The queue of SIGCLD signals is implied by the list of children.
17780Sstevel@tonic-gate  * We post the SIGCLD signals one at a time so they don't get lost.
17790Sstevel@tonic-gate  * When one is dequeued, another is enqueued, until there are no more.
17800Sstevel@tonic-gate  */
17810Sstevel@tonic-gate void
sigcld_repost()17820Sstevel@tonic-gate sigcld_repost()
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	proc_t *pp = curproc;
17850Sstevel@tonic-gate 	proc_t *cp;
17860Sstevel@tonic-gate 	sigqueue_t *sqp;
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
17890Sstevel@tonic-gate 	mutex_enter(&pidlock);
17900Sstevel@tonic-gate 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
17910Sstevel@tonic-gate 		if (cp->p_pidflag & CLDPEND) {
17920Sstevel@tonic-gate 			post_sigcld(cp, sqp);
17930Sstevel@tonic-gate 			mutex_exit(&pidlock);
17940Sstevel@tonic-gate 			return;
17950Sstevel@tonic-gate 		}
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate 	mutex_exit(&pidlock);
17980Sstevel@tonic-gate 	kmem_free(sqp, sizeof (sigqueue_t));
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate /*
18020Sstevel@tonic-gate  * count number of sigqueue send by sigaddqa()
18030Sstevel@tonic-gate  */
18040Sstevel@tonic-gate void
sigqsend(int cmd,proc_t * p,kthread_t * t,sigqueue_t * sigqp)18050Sstevel@tonic-gate sigqsend(int cmd, proc_t *p, kthread_t *t, sigqueue_t *sigqp)
18060Sstevel@tonic-gate {
18070Sstevel@tonic-gate 	sigqhdr_t *sqh;
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 	sqh = (sigqhdr_t *)sigqp->sq_backptr;
18100Sstevel@tonic-gate 	ASSERT(sqh);
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	mutex_enter(&sqh->sqb_lock);
18130Sstevel@tonic-gate 	sqh->sqb_sent++;
18140Sstevel@tonic-gate 	mutex_exit(&sqh->sqb_lock);
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	if (cmd == SN_SEND)
18170Sstevel@tonic-gate 		sigaddqa(p, t, sigqp);
18180Sstevel@tonic-gate 	else
18190Sstevel@tonic-gate 		siginfofree(sigqp);
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate int
sigsendproc(proc_t * p,sigsend_t * pv)18230Sstevel@tonic-gate sigsendproc(proc_t *p, sigsend_t *pv)
18240Sstevel@tonic-gate {
18250Sstevel@tonic-gate 	struct cred *cr;
18260Sstevel@tonic-gate 	proc_t *myprocp = curproc;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 	if (p->p_pid == 1 && pv->sig && sigismember(&cantmask, pv->sig))
18310Sstevel@tonic-gate 		return (EPERM);
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	cr = CRED();
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	if (pv->checkperm == 0 ||
18360Sstevel@tonic-gate 	    (pv->sig == SIGCONT && p->p_sessp == myprocp->p_sessp) ||
18370Sstevel@tonic-gate 	    prochasprocperm(p, myprocp, cr)) {
18380Sstevel@tonic-gate 		pv->perm++;
18390Sstevel@tonic-gate 		if (pv->sig) {
18400Sstevel@tonic-gate 			/* Make sure we should be setting si_pid and friends */
18410Sstevel@tonic-gate 			ASSERT(pv->sicode <= 0);
18420Sstevel@tonic-gate 			if (SI_CANQUEUE(pv->sicode)) {
18430Sstevel@tonic-gate 				sigqueue_t *sqp;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 				mutex_enter(&myprocp->p_lock);
18460Sstevel@tonic-gate 				sqp = sigqalloc(myprocp->p_sigqhdr);
18470Sstevel@tonic-gate 				mutex_exit(&myprocp->p_lock);
18480Sstevel@tonic-gate 				if (sqp == NULL)
18490Sstevel@tonic-gate 					return (EAGAIN);
18500Sstevel@tonic-gate 				sqp->sq_info.si_signo = pv->sig;
18510Sstevel@tonic-gate 				sqp->sq_info.si_code = pv->sicode;
18520Sstevel@tonic-gate 				sqp->sq_info.si_pid = myprocp->p_pid;
18530Sstevel@tonic-gate 				sqp->sq_info.si_ctid = PRCTID(myprocp);
18540Sstevel@tonic-gate 				sqp->sq_info.si_zoneid = getzoneid();
18550Sstevel@tonic-gate 				sqp->sq_info.si_uid = crgetruid(cr);
18560Sstevel@tonic-gate 				sqp->sq_info.si_value = pv->value;
18570Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
18580Sstevel@tonic-gate 				sigqsend(SN_SEND, p, NULL, sqp);
18590Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
18600Sstevel@tonic-gate 			} else {
18610Sstevel@tonic-gate 				k_siginfo_t info;
18620Sstevel@tonic-gate 				bzero(&info, sizeof (info));
18630Sstevel@tonic-gate 				info.si_signo = pv->sig;
18640Sstevel@tonic-gate 				info.si_code = pv->sicode;
18650Sstevel@tonic-gate 				info.si_pid = myprocp->p_pid;
18660Sstevel@tonic-gate 				info.si_ctid = PRCTID(myprocp);
18670Sstevel@tonic-gate 				info.si_zoneid = getzoneid();
18680Sstevel@tonic-gate 				info.si_uid = crgetruid(cr);
18690Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
18700Sstevel@tonic-gate 				/*
18710Sstevel@tonic-gate 				 * XXX: Should be KM_SLEEP but
18720Sstevel@tonic-gate 				 * we have to avoid deadlock.
18730Sstevel@tonic-gate 				 */
18740Sstevel@tonic-gate 				sigaddq(p, NULL, &info, KM_NOSLEEP);
18750Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
18760Sstevel@tonic-gate 			}
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 	}
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	return (0);
18810Sstevel@tonic-gate }
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate int
sigsendset(procset_t * psp,sigsend_t * pv)18840Sstevel@tonic-gate sigsendset(procset_t *psp, sigsend_t *pv)
18850Sstevel@tonic-gate {
18860Sstevel@tonic-gate 	int error;
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	error = dotoprocs(psp, sigsendproc, (char *)pv);
18890Sstevel@tonic-gate 	if (error == 0 && pv->perm == 0)
18900Sstevel@tonic-gate 		return (EPERM);
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	return (error);
18930Sstevel@tonic-gate }
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate /*
18960Sstevel@tonic-gate  * Dequeue a queued siginfo structure.
18970Sstevel@tonic-gate  * If a non-null thread pointer is passed then dequeue from
18980Sstevel@tonic-gate  * the thread queue, otherwise dequeue from the process queue.
18990Sstevel@tonic-gate  */
19000Sstevel@tonic-gate void
sigdeq(proc_t * p,kthread_t * t,int sig,sigqueue_t ** qpp)19010Sstevel@tonic-gate sigdeq(proc_t *p, kthread_t *t, int sig, sigqueue_t **qpp)
19020Sstevel@tonic-gate {
19030Sstevel@tonic-gate 	sigqueue_t **psqp, *sqp;
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	*qpp = NULL;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	if (t != NULL) {
19100Sstevel@tonic-gate 		sigdelset(&t->t_sig, sig);
19110Sstevel@tonic-gate 		sigdelset(&t->t_extsig, sig);
19120Sstevel@tonic-gate 		psqp = &t->t_sigqueue;
19130Sstevel@tonic-gate 	} else {
19140Sstevel@tonic-gate 		sigdelset(&p->p_sig, sig);
19150Sstevel@tonic-gate 		sigdelset(&p->p_extsig, sig);
19160Sstevel@tonic-gate 		psqp = &p->p_sigqueue;
19170Sstevel@tonic-gate 	}
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	for (;;) {
19200Sstevel@tonic-gate 		if ((sqp = *psqp) == NULL)
19210Sstevel@tonic-gate 			return;
19220Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == sig)
19230Sstevel@tonic-gate 			break;
19240Sstevel@tonic-gate 		else
19250Sstevel@tonic-gate 			psqp = &sqp->sq_next;
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 	*qpp = sqp;
19280Sstevel@tonic-gate 	*psqp = sqp->sq_next;
19290Sstevel@tonic-gate 	for (sqp = *psqp; sqp; sqp = sqp->sq_next) {
19300Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == sig) {
19310Sstevel@tonic-gate 			if (t != (kthread_t *)NULL) {
19320Sstevel@tonic-gate 				sigaddset(&t->t_sig, sig);
19330Sstevel@tonic-gate 				t->t_sig_check = 1;
19340Sstevel@tonic-gate 			} else {
19350Sstevel@tonic-gate 				sigaddset(&p->p_sig, sig);
19360Sstevel@tonic-gate 				set_proc_ast(p);
19370Sstevel@tonic-gate 			}
19380Sstevel@tonic-gate 			break;
19390Sstevel@tonic-gate 		}
19400Sstevel@tonic-gate 	}
19410Sstevel@tonic-gate }
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate /*
19440Sstevel@tonic-gate  * Delete a queued SIGCLD siginfo structure matching the k_siginfo_t argument.
19450Sstevel@tonic-gate  */
19460Sstevel@tonic-gate void
sigcld_delete(k_siginfo_t * ip)19470Sstevel@tonic-gate sigcld_delete(k_siginfo_t *ip)
19480Sstevel@tonic-gate {
19490Sstevel@tonic-gate 	proc_t *p = curproc;
19500Sstevel@tonic-gate 	int another_sigcld = 0;
19510Sstevel@tonic-gate 	sigqueue_t **psqp, *sqp;
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	ASSERT(ip->si_signo == SIGCLD);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	if (!sigismember(&p->p_sig, SIGCLD)) {
19580Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
19590Sstevel@tonic-gate 		return;
19600Sstevel@tonic-gate 	}
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 	psqp = &p->p_sigqueue;
19630Sstevel@tonic-gate 	for (;;) {
19640Sstevel@tonic-gate 		if ((sqp = *psqp) == NULL) {
19650Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
19660Sstevel@tonic-gate 			return;
19670Sstevel@tonic-gate 		}
19680Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == SIGCLD) {
19690Sstevel@tonic-gate 			if (sqp->sq_info.si_pid == ip->si_pid &&
19700Sstevel@tonic-gate 			    sqp->sq_info.si_code == ip->si_code &&
19710Sstevel@tonic-gate 			    sqp->sq_info.si_status == ip->si_status)
19720Sstevel@tonic-gate 				break;
19730Sstevel@tonic-gate 			another_sigcld = 1;
19740Sstevel@tonic-gate 		}
19750Sstevel@tonic-gate 		psqp = &sqp->sq_next;
19760Sstevel@tonic-gate 	}
19770Sstevel@tonic-gate 	*psqp = sqp->sq_next;
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	siginfofree(sqp);
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	for (sqp = *psqp; !another_sigcld && sqp; sqp = sqp->sq_next) {
19820Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == SIGCLD)
19830Sstevel@tonic-gate 			another_sigcld = 1;
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	if (!another_sigcld) {
19870Sstevel@tonic-gate 		sigdelset(&p->p_sig, SIGCLD);
19880Sstevel@tonic-gate 		sigdelset(&p->p_extsig, SIGCLD);
19890Sstevel@tonic-gate 	}
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
19920Sstevel@tonic-gate }
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate /*
19950Sstevel@tonic-gate  * Delete queued siginfo structures.
19960Sstevel@tonic-gate  * If a non-null thread pointer is passed then delete from
19970Sstevel@tonic-gate  * the thread queue, otherwise delete from the process queue.
19980Sstevel@tonic-gate  */
19990Sstevel@tonic-gate void
sigdelq(proc_t * p,kthread_t * t,int sig)20000Sstevel@tonic-gate sigdelq(proc_t *p, kthread_t *t, int sig)
20010Sstevel@tonic-gate {
20020Sstevel@tonic-gate 	sigqueue_t **psqp, *sqp;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	/*
20050Sstevel@tonic-gate 	 * We must be holding p->p_lock unless the process is
20060Sstevel@tonic-gate 	 * being reaped or has failed to get started on fork.
20070Sstevel@tonic-gate 	 */
20080Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock) ||
20090Sstevel@tonic-gate 	    p->p_stat == SIDL || p->p_stat == SZOMB);
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (t != (kthread_t *)NULL)
20120Sstevel@tonic-gate 		psqp = &t->t_sigqueue;
20130Sstevel@tonic-gate 	else
20140Sstevel@tonic-gate 		psqp = &p->p_sigqueue;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	while (*psqp) {
20170Sstevel@tonic-gate 		sqp = *psqp;
20180Sstevel@tonic-gate 		if (sig == 0 || sqp->sq_info.si_signo == sig) {
20190Sstevel@tonic-gate 			*psqp = sqp->sq_next;
20200Sstevel@tonic-gate 			siginfofree(sqp);
20210Sstevel@tonic-gate 		} else
20220Sstevel@tonic-gate 			psqp = &sqp->sq_next;
20230Sstevel@tonic-gate 	}
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * Insert a siginfo structure into a queue.
20280Sstevel@tonic-gate  * If a non-null thread pointer is passed then add to the thread queue,
20290Sstevel@tonic-gate  * otherwise add to the process queue.
20300Sstevel@tonic-gate  *
20310Sstevel@tonic-gate  * The function sigaddqins() is called with sigqueue already allocated.
20320Sstevel@tonic-gate  * It is called from sigaddqa() and sigaddq() below.
20330Sstevel@tonic-gate  *
20340Sstevel@tonic-gate  * The value of si_code implicitly indicates whether sigp is to be
20350Sstevel@tonic-gate  * explicitly queued, or to be queued to depth one.
20360Sstevel@tonic-gate  */
20370Sstevel@tonic-gate static void
sigaddqins(proc_t * p,kthread_t * t,sigqueue_t * sigqp)20380Sstevel@tonic-gate sigaddqins(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
20390Sstevel@tonic-gate {
20400Sstevel@tonic-gate 	sigqueue_t **psqp;
20410Sstevel@tonic-gate 	int sig = sigqp->sq_info.si_signo;
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	sigqp->sq_external = (curproc != &p0) &&
20440Sstevel@tonic-gate 	    (curproc->p_ct_process != p->p_ct_process);
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	/*
20470Sstevel@tonic-gate 	 * issig_forreal() doesn't bother dequeueing signals if SKILLED
20480Sstevel@tonic-gate 	 * is set, and even if it did, we would want to avoid situation
20490Sstevel@tonic-gate 	 * (which would be unique to SIGKILL) where one thread dequeued
20500Sstevel@tonic-gate 	 * the sigqueue_t and another executed psig().  So we create a
20510Sstevel@tonic-gate 	 * separate stash for SIGKILL's sigqueue_t.  Because a second
20520Sstevel@tonic-gate 	 * SIGKILL can set SEXTKILLED, we overwrite the existing entry
20530Sstevel@tonic-gate 	 * if (and only if) it was non-extracontractual.
20540Sstevel@tonic-gate 	 */
20550Sstevel@tonic-gate 	if (sig == SIGKILL) {
20560Sstevel@tonic-gate 		if (p->p_killsqp == NULL || !p->p_killsqp->sq_external) {
20570Sstevel@tonic-gate 			if (p->p_killsqp != NULL)
20580Sstevel@tonic-gate 				siginfofree(p->p_killsqp);
20590Sstevel@tonic-gate 			p->p_killsqp = sigqp;
20600Sstevel@tonic-gate 			sigqp->sq_next = NULL;
20610Sstevel@tonic-gate 		} else {
20620Sstevel@tonic-gate 			siginfofree(sigqp);
20630Sstevel@tonic-gate 		}
20640Sstevel@tonic-gate 		return;
20650Sstevel@tonic-gate 	}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	ASSERT(sig >= 1 && sig < NSIG);
20680Sstevel@tonic-gate 	if (t != NULL)	/* directed to a thread */
20690Sstevel@tonic-gate 		psqp = &t->t_sigqueue;
20700Sstevel@tonic-gate 	else 		/* directed to a process */
20710Sstevel@tonic-gate 		psqp = &p->p_sigqueue;
20720Sstevel@tonic-gate 	if (SI_CANQUEUE(sigqp->sq_info.si_code) &&
20730Sstevel@tonic-gate 	    sigismember(&p->p_siginfo, sig)) {
20740Sstevel@tonic-gate 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next)
20750Sstevel@tonic-gate 				;
20760Sstevel@tonic-gate 	} else {
20770Sstevel@tonic-gate 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next) {
20780Sstevel@tonic-gate 			if ((*psqp)->sq_info.si_signo == sig) {
20790Sstevel@tonic-gate 				siginfofree(sigqp);
20800Sstevel@tonic-gate 				return;
20810Sstevel@tonic-gate 			}
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate 	}
20840Sstevel@tonic-gate 	*psqp = sigqp;
20850Sstevel@tonic-gate 	sigqp->sq_next = NULL;
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate /*
20890Sstevel@tonic-gate  * The function sigaddqa() is called with sigqueue already allocated.
20900Sstevel@tonic-gate  * If signal is ignored, discard but guarantee KILL and generation semantics.
20910Sstevel@tonic-gate  * It is called from sigqueue() and other places.
20920Sstevel@tonic-gate  */
20930Sstevel@tonic-gate void
sigaddqa(proc_t * p,kthread_t * t,sigqueue_t * sigqp)20940Sstevel@tonic-gate sigaddqa(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
20950Sstevel@tonic-gate {
20960Sstevel@tonic-gate 	int sig = sigqp->sq_info.si_signo;
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
20990Sstevel@tonic-gate 	ASSERT(sig >= 1 && sig < NSIG);
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if (sig_discardable(p, sig))
21020Sstevel@tonic-gate 		siginfofree(sigqp);
21030Sstevel@tonic-gate 	else
21040Sstevel@tonic-gate 		sigaddqins(p, t, sigqp);
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	sigtoproc(p, t, sig);
21070Sstevel@tonic-gate }
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate /*
21100Sstevel@tonic-gate  * Allocate the sigqueue_t structure and call sigaddqins().
21110Sstevel@tonic-gate  */
21120Sstevel@tonic-gate void
sigaddq(proc_t * p,kthread_t * t,k_siginfo_t * infop,int km_flags)21130Sstevel@tonic-gate sigaddq(proc_t *p, kthread_t *t, k_siginfo_t *infop, int km_flags)
21140Sstevel@tonic-gate {
21150Sstevel@tonic-gate 	sigqueue_t *sqp;
21160Sstevel@tonic-gate 	int sig = infop->si_signo;
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
21190Sstevel@tonic-gate 	ASSERT(sig >= 1 && sig < NSIG);
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 	/*
21220Sstevel@tonic-gate 	 * If the signal will be discarded by sigtoproc() or
21230Sstevel@tonic-gate 	 * if the process isn't requesting siginfo and it isn't
21240Sstevel@tonic-gate 	 * blocking the signal (it *could* change it's mind while
21250Sstevel@tonic-gate 	 * the signal is pending) then don't bother creating one.
21260Sstevel@tonic-gate 	 */
21270Sstevel@tonic-gate 	if (!sig_discardable(p, sig) &&
21280Sstevel@tonic-gate 	    (sigismember(&p->p_siginfo, sig) ||
21290Sstevel@tonic-gate 	    (curproc->p_ct_process != p->p_ct_process) ||
21300Sstevel@tonic-gate 	    (sig == SIGCLD && SI_FROMKERNEL(infop))) &&
21310Sstevel@tonic-gate 	    ((sqp = kmem_alloc(sizeof (sigqueue_t), km_flags)) != NULL)) {
21320Sstevel@tonic-gate 		bcopy(infop, &sqp->sq_info, sizeof (k_siginfo_t));
21330Sstevel@tonic-gate 		sqp->sq_func = NULL;
21340Sstevel@tonic-gate 		sqp->sq_next = NULL;
21350Sstevel@tonic-gate 		sigaddqins(p, t, sqp);
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 	sigtoproc(p, t, sig);
21380Sstevel@tonic-gate }
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate /*
21410Sstevel@tonic-gate  * Handle stop-on-fault processing for the debugger.  Returns 0
21420Sstevel@tonic-gate  * if the fault is cleared during the stop, nonzero if it isn't.
21430Sstevel@tonic-gate  */
21440Sstevel@tonic-gate int
stop_on_fault(uint_t fault,k_siginfo_t * sip)21450Sstevel@tonic-gate stop_on_fault(uint_t fault, k_siginfo_t *sip)
21460Sstevel@tonic-gate {
21470Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
21480Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 	ASSERT(prismember(&p->p_fltmask, fault));
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	/*
21530Sstevel@tonic-gate 	 * Record current fault and siginfo structure so debugger can
21540Sstevel@tonic-gate 	 * find it.
21550Sstevel@tonic-gate 	 */
21560Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
21570Sstevel@tonic-gate 	lwp->lwp_curflt = (uchar_t)fault;
21580Sstevel@tonic-gate 	lwp->lwp_siginfo = *sip;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	stop(PR_FAULTED, fault);
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	fault = lwp->lwp_curflt;
21630Sstevel@tonic-gate 	lwp->lwp_curflt = 0;
21640Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
21650Sstevel@tonic-gate 	return (fault);
21660Sstevel@tonic-gate }
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate void
sigorset(k_sigset_t * s1,const k_sigset_t * s2)2169*11913SRoger.Faulkner@Sun.COM sigorset(k_sigset_t *s1, const k_sigset_t *s2)
21700Sstevel@tonic-gate {
21710Sstevel@tonic-gate 	s1->__sigbits[0] |= s2->__sigbits[0];
21720Sstevel@tonic-gate 	s1->__sigbits[1] |= s2->__sigbits[1];
2173*11913SRoger.Faulkner@Sun.COM 	s1->__sigbits[2] |= s2->__sigbits[2];
21740Sstevel@tonic-gate }
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate void
sigandset(k_sigset_t * s1,const k_sigset_t * s2)2177*11913SRoger.Faulkner@Sun.COM sigandset(k_sigset_t *s1, const k_sigset_t *s2)
21780Sstevel@tonic-gate {
21790Sstevel@tonic-gate 	s1->__sigbits[0] &= s2->__sigbits[0];
21800Sstevel@tonic-gate 	s1->__sigbits[1] &= s2->__sigbits[1];
2181*11913SRoger.Faulkner@Sun.COM 	s1->__sigbits[2] &= s2->__sigbits[2];
21820Sstevel@tonic-gate }
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate void
sigdiffset(k_sigset_t * s1,const k_sigset_t * s2)2185*11913SRoger.Faulkner@Sun.COM sigdiffset(k_sigset_t *s1, const k_sigset_t *s2)
21860Sstevel@tonic-gate {
21870Sstevel@tonic-gate 	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
21880Sstevel@tonic-gate 	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
2189*11913SRoger.Faulkner@Sun.COM 	s1->__sigbits[2] &= ~(s2->__sigbits[2]);
21900Sstevel@tonic-gate }
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate /*
21930Sstevel@tonic-gate  * Return non-zero if curthread->t_sig_check should be set to 1, that is,
21940Sstevel@tonic-gate  * if there are any signals the thread might take on return from the kernel.
21950Sstevel@tonic-gate  * If ksigset_t's were a single word, we would do:
21960Sstevel@tonic-gate  *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
21970Sstevel@tonic-gate  */
21980Sstevel@tonic-gate int
sigcheck(proc_t * p,kthread_t * t)21990Sstevel@tonic-gate sigcheck(proc_t *p, kthread_t *t)
22000Sstevel@tonic-gate {
22010Sstevel@tonic-gate 	sc_shared_t *tdp = t->t_schedctl;
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 	/*
22040Sstevel@tonic-gate 	 * If signals are blocked via the schedctl interface
22050Sstevel@tonic-gate 	 * then we only check for the unmaskable signals.
2206*11913SRoger.Faulkner@Sun.COM 	 * The unmaskable signal numbers should all be contained
2207*11913SRoger.Faulkner@Sun.COM 	 * in __sigbits[0] and we assume this for speed.
22080Sstevel@tonic-gate 	 */
2209*11913SRoger.Faulkner@Sun.COM #if (CANTMASK1 == 0 && CANTMASK2 == 0)
22100Sstevel@tonic-gate 	if (tdp != NULL && tdp->sc_sigblock)
22110Sstevel@tonic-gate 		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
22120Sstevel@tonic-gate 		    CANTMASK0);
2213*11913SRoger.Faulkner@Sun.COM #else
2214*11913SRoger.Faulkner@Sun.COM #error "fix me: CANTMASK1 and CANTMASK2 are not zero"
2215*11913SRoger.Faulkner@Sun.COM #endif
22160Sstevel@tonic-gate 
2217*11913SRoger.Faulkner@Sun.COM /* see uts/common/sys/signal.h for why this must be true */
2218*11913SRoger.Faulkner@Sun.COM #if ((MAXSIG > (2 * 32)) && (MAXSIG <= (3 * 32)))
22190Sstevel@tonic-gate 	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
22205297Sbpramod 	    ~t->t_hold.__sigbits[0]) |
2221*11913SRoger.Faulkner@Sun.COM 	    ((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
2222*11913SRoger.Faulkner@Sun.COM 	    ~t->t_hold.__sigbits[1]) |
2223*11913SRoger.Faulkner@Sun.COM 	    (((p->p_sig.__sigbits[2] | t->t_sig.__sigbits[2]) &
2224*11913SRoger.Faulkner@Sun.COM 	    ~t->t_hold.__sigbits[2]) & FILLSET2));
2225*11913SRoger.Faulkner@Sun.COM #else
2226*11913SRoger.Faulkner@Sun.COM #error "fix me: MAXSIG out of bounds"
2227*11913SRoger.Faulkner@Sun.COM #endif
22280Sstevel@tonic-gate }
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate /* ONC_PLUS EXTRACT START */
22310Sstevel@tonic-gate void
sigintr(k_sigset_t * smask,int intable)22320Sstevel@tonic-gate sigintr(k_sigset_t *smask, int intable)
22330Sstevel@tonic-gate {
22340Sstevel@tonic-gate 	proc_t *p;
22350Sstevel@tonic-gate 	int owned;
22360Sstevel@tonic-gate 	k_sigset_t lmask;		/* local copy of cantmask */
22370Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 	/*
22400Sstevel@tonic-gate 	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
22410Sstevel@tonic-gate 	 *    and SIGTERM. (Preserving the existing masks).
22420Sstevel@tonic-gate 	 *    This function supports the -intr nfs and ufs mount option.
22430Sstevel@tonic-gate 	 */
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	/*
22460Sstevel@tonic-gate 	 * don't do kernel threads
22470Sstevel@tonic-gate 	 */
22480Sstevel@tonic-gate 	if (lwp == NULL)
22490Sstevel@tonic-gate 		return;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * get access to signal mask
22530Sstevel@tonic-gate 	 */
22540Sstevel@tonic-gate 	p = ttoproc(curthread);
22550Sstevel@tonic-gate 	owned = mutex_owned(&p->p_lock);	/* this is filthy */
22560Sstevel@tonic-gate 	if (!owned)
22570Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * remember the current mask
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
22630Sstevel@tonic-gate 	*smask = curthread->t_hold;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	/*
22660Sstevel@tonic-gate 	 * mask out all signals
22670Sstevel@tonic-gate 	 */
22680Sstevel@tonic-gate 	sigfillset(&curthread->t_hold);
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	/*
22710Sstevel@tonic-gate 	 * Unmask the non-maskable signals (e.g., KILL), as long as
22720Sstevel@tonic-gate 	 * they aren't already masked (which could happen at exit).
22730Sstevel@tonic-gate 	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
22740Sstevel@tonic-gate 	 * second sets the current hold mask to (~0 & ~lmask), which reduces
22750Sstevel@tonic-gate 	 * to (~cantmask | curhold).
22760Sstevel@tonic-gate 	 */
22770Sstevel@tonic-gate 	lmask = cantmask;
22780Sstevel@tonic-gate 	sigdiffset(&lmask, smask);
22790Sstevel@tonic-gate 	sigdiffset(&curthread->t_hold, &lmask);
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	/*
22820Sstevel@tonic-gate 	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
22830Sstevel@tonic-gate 	 * Re-enable INT if it's originally enabled and the NFS mount option
22840Sstevel@tonic-gate 	 * nointr is not set.
22850Sstevel@tonic-gate 	 */
22860Sstevel@tonic-gate 	if (!sigismember(smask, SIGHUP))
22870Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGHUP);
22880Sstevel@tonic-gate 	if (!sigismember(smask, SIGINT) && intable)
22890Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGINT);
22900Sstevel@tonic-gate 	if (!sigismember(smask, SIGQUIT))
22910Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGQUIT);
22920Sstevel@tonic-gate 	if (!sigismember(smask, SIGTERM))
22930Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGTERM);
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	/*
22960Sstevel@tonic-gate 	 * release access to signal mask
22970Sstevel@tonic-gate 	 */
22980Sstevel@tonic-gate 	if (!owned)
22990Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	/*
23020Sstevel@tonic-gate 	 * Indicate that this lwp is not to be stopped.
23030Sstevel@tonic-gate 	 */
23040Sstevel@tonic-gate 	lwp->lwp_nostop++;
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate }
23070Sstevel@tonic-gate /* ONC_PLUS EXTRACT END */
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate void
sigunintr(k_sigset_t * smask)23100Sstevel@tonic-gate sigunintr(k_sigset_t *smask)
23110Sstevel@tonic-gate {
23120Sstevel@tonic-gate 	proc_t *p;
23130Sstevel@tonic-gate 	int owned;
23140Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	/*
23170Sstevel@tonic-gate 	 * Reset previous mask (See sigintr() above)
23180Sstevel@tonic-gate 	 */
23190Sstevel@tonic-gate 	if (lwp != NULL) {
23200Sstevel@tonic-gate 		lwp->lwp_nostop--;	/* restore lwp stoppability */
23210Sstevel@tonic-gate 		p = ttoproc(curthread);
23220Sstevel@tonic-gate 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
23230Sstevel@tonic-gate 		if (!owned)
23240Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
23250Sstevel@tonic-gate 		curthread->t_hold = *smask;
23260Sstevel@tonic-gate 		/* so unmasked signals will be seen */
23270Sstevel@tonic-gate 		curthread->t_sig_check = 1;
23280Sstevel@tonic-gate 		if (!owned)
23290Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate }
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate void
sigreplace(k_sigset_t * newmask,k_sigset_t * oldmask)23340Sstevel@tonic-gate sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
23350Sstevel@tonic-gate {
23360Sstevel@tonic-gate 	proc_t	*p;
23370Sstevel@tonic-gate 	int owned;
23380Sstevel@tonic-gate 	/*
23390Sstevel@tonic-gate 	 * Save current signal mask in oldmask, then
23400Sstevel@tonic-gate 	 * set it to newmask.
23410Sstevel@tonic-gate 	 */
23420Sstevel@tonic-gate 	if (ttolwp(curthread) != NULL) {
23430Sstevel@tonic-gate 		p = ttoproc(curthread);
23440Sstevel@tonic-gate 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
23450Sstevel@tonic-gate 		if (!owned)
23460Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
23470Sstevel@tonic-gate 		schedctl_finish_sigblock(curthread);
23480Sstevel@tonic-gate 		if (oldmask != NULL)
23490Sstevel@tonic-gate 			*oldmask = curthread->t_hold;
23500Sstevel@tonic-gate 		curthread->t_hold = *newmask;
23510Sstevel@tonic-gate 		curthread->t_sig_check = 1;
23520Sstevel@tonic-gate 		if (!owned)
23530Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
23540Sstevel@tonic-gate 	}
23550Sstevel@tonic-gate }
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate /*
23580Sstevel@tonic-gate  * Return true if the signal number is in range
23590Sstevel@tonic-gate  * and the signal code specifies signal queueing.
23600Sstevel@tonic-gate  */
23610Sstevel@tonic-gate int
sigwillqueue(int sig,int code)23620Sstevel@tonic-gate sigwillqueue(int sig, int code)
23630Sstevel@tonic-gate {
23640Sstevel@tonic-gate 	if (sig >= 0 && sig < NSIG) {
23650Sstevel@tonic-gate 		switch (code) {
23660Sstevel@tonic-gate 		case SI_QUEUE:
23670Sstevel@tonic-gate 		case SI_TIMER:
23680Sstevel@tonic-gate 		case SI_ASYNCIO:
23690Sstevel@tonic-gate 		case SI_MESGQ:
23700Sstevel@tonic-gate 			return (1);
23710Sstevel@tonic-gate 		}
23720Sstevel@tonic-gate 	}
23730Sstevel@tonic-gate 	return (0);
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate #ifndef	UCHAR_MAX
23770Sstevel@tonic-gate #define	UCHAR_MAX	255
23780Sstevel@tonic-gate #endif
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate /*
23810Sstevel@tonic-gate  * The entire pool (with maxcount entries) is pre-allocated at
23820Sstevel@tonic-gate  * the first sigqueue/signotify call.
23830Sstevel@tonic-gate  */
23840Sstevel@tonic-gate sigqhdr_t *
sigqhdralloc(size_t size,uint_t maxcount)23850Sstevel@tonic-gate sigqhdralloc(size_t size, uint_t maxcount)
23860Sstevel@tonic-gate {
23870Sstevel@tonic-gate 	size_t i;
23880Sstevel@tonic-gate 	sigqueue_t *sq, *next;
23890Sstevel@tonic-gate 	sigqhdr_t *sqh;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	i = (maxcount * size) + sizeof (sigqhdr_t);
23920Sstevel@tonic-gate 	ASSERT(maxcount <= UCHAR_MAX && i <= USHRT_MAX);
23930Sstevel@tonic-gate 	sqh = kmem_alloc(i, KM_SLEEP);
23940Sstevel@tonic-gate 	sqh->sqb_count = (uchar_t)maxcount;
23950Sstevel@tonic-gate 	sqh->sqb_maxcount = (uchar_t)maxcount;
23960Sstevel@tonic-gate 	sqh->sqb_size = (ushort_t)i;
23970Sstevel@tonic-gate 	sqh->sqb_pexited = 0;
23980Sstevel@tonic-gate 	sqh->sqb_sent = 0;
23990Sstevel@tonic-gate 	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
24000Sstevel@tonic-gate 	for (i = maxcount - 1; i != 0; i--) {
24010Sstevel@tonic-gate 		next = (sigqueue_t *)((uintptr_t)sq + size);
24020Sstevel@tonic-gate 		sq->sq_next = next;
24030Sstevel@tonic-gate 		sq = next;
24040Sstevel@tonic-gate 	}
24050Sstevel@tonic-gate 	sq->sq_next = NULL;
24062248Sraf 	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
24070Sstevel@tonic-gate 	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
24080Sstevel@tonic-gate 	return (sqh);
24090Sstevel@tonic-gate }
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate static void sigqrel(sigqueue_t *);
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate /*
24140Sstevel@tonic-gate  * allocate a sigqueue/signotify structure from the per process
24150Sstevel@tonic-gate  * pre-allocated pool.
24160Sstevel@tonic-gate  */
24170Sstevel@tonic-gate sigqueue_t *
sigqalloc(sigqhdr_t * sqh)24180Sstevel@tonic-gate sigqalloc(sigqhdr_t *sqh)
24190Sstevel@tonic-gate {
24200Sstevel@tonic-gate 	sigqueue_t *sq = NULL;
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&curproc->p_lock));
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	if (sqh != NULL) {
24250Sstevel@tonic-gate 		mutex_enter(&sqh->sqb_lock);
24260Sstevel@tonic-gate 		if (sqh->sqb_count > 0) {
24270Sstevel@tonic-gate 			sqh->sqb_count--;
24280Sstevel@tonic-gate 			sq = sqh->sqb_free;
24290Sstevel@tonic-gate 			sqh->sqb_free = sq->sq_next;
24300Sstevel@tonic-gate 			mutex_exit(&sqh->sqb_lock);
24310Sstevel@tonic-gate 			bzero(&sq->sq_info, sizeof (k_siginfo_t));
24320Sstevel@tonic-gate 			sq->sq_backptr = sqh;
24330Sstevel@tonic-gate 			sq->sq_func = sigqrel;
24340Sstevel@tonic-gate 			sq->sq_next = NULL;
24350Sstevel@tonic-gate 			sq->sq_external = 0;
24360Sstevel@tonic-gate 		} else {
24370Sstevel@tonic-gate 			mutex_exit(&sqh->sqb_lock);
24380Sstevel@tonic-gate 		}
24390Sstevel@tonic-gate 	}
24400Sstevel@tonic-gate 	return (sq);
24410Sstevel@tonic-gate }
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate /*
24440Sstevel@tonic-gate  * Return a sigqueue structure back to the pre-allocated pool.
24450Sstevel@tonic-gate  */
24460Sstevel@tonic-gate static void
sigqrel(sigqueue_t * sq)24470Sstevel@tonic-gate sigqrel(sigqueue_t *sq)
24480Sstevel@tonic-gate {
24490Sstevel@tonic-gate 	sigqhdr_t *sqh;
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	/* make sure that p_lock of the affected process is held */
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	sqh = (sigqhdr_t *)sq->sq_backptr;
24540Sstevel@tonic-gate 	mutex_enter(&sqh->sqb_lock);
24550Sstevel@tonic-gate 	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
24560Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
24572248Sraf 		cv_destroy(&sqh->sqb_cv);
24580Sstevel@tonic-gate 		mutex_destroy(&sqh->sqb_lock);
24590Sstevel@tonic-gate 		kmem_free(sqh, sqh->sqb_size);
24600Sstevel@tonic-gate 	} else {
24610Sstevel@tonic-gate 		sqh->sqb_count++;
24620Sstevel@tonic-gate 		sqh->sqb_sent--;
24630Sstevel@tonic-gate 		sq->sq_next = sqh->sqb_free;
24640Sstevel@tonic-gate 		sq->sq_backptr = NULL;
24650Sstevel@tonic-gate 		sqh->sqb_free = sq;
24662248Sraf 		cv_signal(&sqh->sqb_cv);
24670Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
24680Sstevel@tonic-gate 	}
24690Sstevel@tonic-gate }
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate /*
24720Sstevel@tonic-gate  * Free up the pre-allocated sigqueue headers of sigqueue pool
24730Sstevel@tonic-gate  * and signotify pool, if possible.
24740Sstevel@tonic-gate  * Called only by the owning process during exec() and exit().
24750Sstevel@tonic-gate  */
24760Sstevel@tonic-gate void
sigqfree(proc_t * p)24770Sstevel@tonic-gate sigqfree(proc_t *p)
24780Sstevel@tonic-gate {
24790Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
24820Sstevel@tonic-gate 		sigqhdrfree(p->p_sigqhdr);
24830Sstevel@tonic-gate 		p->p_sigqhdr = NULL;
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 	if (p->p_signhdr != NULL) {	/* signotify pool */
24860Sstevel@tonic-gate 		sigqhdrfree(p->p_signhdr);
24870Sstevel@tonic-gate 		p->p_signhdr = NULL;
24880Sstevel@tonic-gate 	}
24890Sstevel@tonic-gate }
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate /*
24920Sstevel@tonic-gate  * Free up the pre-allocated header and sigq pool if possible.
24930Sstevel@tonic-gate  */
24940Sstevel@tonic-gate void
sigqhdrfree(sigqhdr_t * sqh)24950Sstevel@tonic-gate sigqhdrfree(sigqhdr_t *sqh)
24960Sstevel@tonic-gate {
24970Sstevel@tonic-gate 	mutex_enter(&sqh->sqb_lock);
24980Sstevel@tonic-gate 	if (sqh->sqb_sent == 0) {
24990Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
25002248Sraf 		cv_destroy(&sqh->sqb_cv);
25010Sstevel@tonic-gate 		mutex_destroy(&sqh->sqb_lock);
25020Sstevel@tonic-gate 		kmem_free(sqh, sqh->sqb_size);
25030Sstevel@tonic-gate 	} else {
25040Sstevel@tonic-gate 		sqh->sqb_pexited = 1;
25050Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
25060Sstevel@tonic-gate 	}
25070Sstevel@tonic-gate }
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate /*
25100Sstevel@tonic-gate  * Free up a single sigqueue structure.
25110Sstevel@tonic-gate  * No other code should free a sigqueue directly.
25120Sstevel@tonic-gate  */
25130Sstevel@tonic-gate void
siginfofree(sigqueue_t * sqp)25140Sstevel@tonic-gate siginfofree(sigqueue_t *sqp)
25150Sstevel@tonic-gate {
25160Sstevel@tonic-gate 	if (sqp != NULL) {
25170Sstevel@tonic-gate 		if (sqp->sq_func != NULL)
25180Sstevel@tonic-gate 			(sqp->sq_func)(sqp);
25190Sstevel@tonic-gate 		else
25200Sstevel@tonic-gate 			kmem_free(sqp, sizeof (sigqueue_t));
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate }
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate /*
25250Sstevel@tonic-gate  * Generate a synchronous signal caused by a hardware
25260Sstevel@tonic-gate  * condition encountered by an lwp.  Called from trap().
25270Sstevel@tonic-gate  */
25280Sstevel@tonic-gate void
trapsig(k_siginfo_t * ip,int restartable)25290Sstevel@tonic-gate trapsig(k_siginfo_t *ip, int restartable)
25300Sstevel@tonic-gate {
25310Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
25320Sstevel@tonic-gate 	int sig = ip->si_signo;
25330Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	ASSERT(sig > 0 && sig < NSIG);
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	if (curthread->t_dtrace_on)
25380Sstevel@tonic-gate 		dtrace_safe_synchronous_signal();
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
25410Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
25420Sstevel@tonic-gate 	/*
25430Sstevel@tonic-gate 	 * Avoid a possible infinite loop if the lwp is holding the
25440Sstevel@tonic-gate 	 * signal generated by a trap of a restartable instruction or
25450Sstevel@tonic-gate 	 * if the signal so generated is being ignored by the process.
25460Sstevel@tonic-gate 	 */
25470Sstevel@tonic-gate 	if (restartable &&
25480Sstevel@tonic-gate 	    (sigismember(&curthread->t_hold, sig) ||
25490Sstevel@tonic-gate 	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
25500Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, sig);
25510Sstevel@tonic-gate 		p->p_user.u_signal[sig-1] = SIG_DFL;
25520Sstevel@tonic-gate 		sigdelset(&p->p_ignore, sig);
25530Sstevel@tonic-gate 	}
25540Sstevel@tonic-gate 	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
25550Sstevel@tonic-gate 	sigaddqa(p, curthread, sqp);
25560Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate 
25599870SRoger.Faulkner@Sun.COM /*
256010341SRoger.Faulkner@Sun.COM  * Dispatch the real time profiling signal in the traditional way,
256110341SRoger.Faulkner@Sun.COM  * honoring all of the /proc tracing mechanism built into issig().
256210341SRoger.Faulkner@Sun.COM  */
256310341SRoger.Faulkner@Sun.COM static void
realsigprof_slow(int sysnum,int nsysarg,int error)256410341SRoger.Faulkner@Sun.COM realsigprof_slow(int sysnum, int nsysarg, int error)
256510341SRoger.Faulkner@Sun.COM {
256610341SRoger.Faulkner@Sun.COM 	kthread_t *t = curthread;
256710341SRoger.Faulkner@Sun.COM 	proc_t *p = ttoproc(t);
256810341SRoger.Faulkner@Sun.COM 	klwp_t *lwp = ttolwp(t);
256910341SRoger.Faulkner@Sun.COM 	k_siginfo_t *sip = &lwp->lwp_siginfo;
257010341SRoger.Faulkner@Sun.COM 	void (*func)();
257110341SRoger.Faulkner@Sun.COM 
257210341SRoger.Faulkner@Sun.COM 	mutex_enter(&p->p_lock);
257310341SRoger.Faulkner@Sun.COM 	func = PTOU(p)->u_signal[SIGPROF - 1];
257410341SRoger.Faulkner@Sun.COM 	if (p->p_rprof_cyclic == CYCLIC_NONE ||
257510341SRoger.Faulkner@Sun.COM 	    func == SIG_DFL || func == SIG_IGN) {
257610341SRoger.Faulkner@Sun.COM 		bzero(t->t_rprof, sizeof (*t->t_rprof));
257710341SRoger.Faulkner@Sun.COM 		mutex_exit(&p->p_lock);
257810341SRoger.Faulkner@Sun.COM 		return;
257910341SRoger.Faulkner@Sun.COM 	}
258010341SRoger.Faulkner@Sun.COM 	if (sigismember(&t->t_hold, SIGPROF)) {
258110341SRoger.Faulkner@Sun.COM 		mutex_exit(&p->p_lock);
258210341SRoger.Faulkner@Sun.COM 		return;
258310341SRoger.Faulkner@Sun.COM 	}
258410341SRoger.Faulkner@Sun.COM 	sip->si_signo = SIGPROF;
258510341SRoger.Faulkner@Sun.COM 	sip->si_code = PROF_SIG;
258610341SRoger.Faulkner@Sun.COM 	sip->si_errno = error;
258710341SRoger.Faulkner@Sun.COM 	hrt2ts(gethrtime(), &sip->si_tstamp);
258810341SRoger.Faulkner@Sun.COM 	sip->si_syscall = sysnum;
258910341SRoger.Faulkner@Sun.COM 	sip->si_nsysarg = nsysarg;
259010341SRoger.Faulkner@Sun.COM 	sip->si_fault = lwp->lwp_lastfault;
259110341SRoger.Faulkner@Sun.COM 	sip->si_faddr = lwp->lwp_lastfaddr;
259210341SRoger.Faulkner@Sun.COM 	lwp->lwp_lastfault = 0;
259310341SRoger.Faulkner@Sun.COM 	lwp->lwp_lastfaddr = NULL;
259410341SRoger.Faulkner@Sun.COM 	sigtoproc(p, t, SIGPROF);
259510341SRoger.Faulkner@Sun.COM 	mutex_exit(&p->p_lock);
259610341SRoger.Faulkner@Sun.COM 	ASSERT(lwp->lwp_cursig == 0);
259710341SRoger.Faulkner@Sun.COM 	if (issig(FORREAL))
259810341SRoger.Faulkner@Sun.COM 		psig();
259910341SRoger.Faulkner@Sun.COM 	sip->si_signo = 0;
260010341SRoger.Faulkner@Sun.COM 	bzero(t->t_rprof, sizeof (*t->t_rprof));
260110341SRoger.Faulkner@Sun.COM }
260210341SRoger.Faulkner@Sun.COM 
260310341SRoger.Faulkner@Sun.COM /*
260410341SRoger.Faulkner@Sun.COM  * We are not tracing the SIGPROF signal, or doing any other unnatural
260510341SRoger.Faulkner@Sun.COM  * acts, like watchpoints, so dispatch the real time profiling signal
260610341SRoger.Faulkner@Sun.COM  * directly, bypassing all of the overhead built into issig().
260710341SRoger.Faulkner@Sun.COM  */
260810341SRoger.Faulkner@Sun.COM static void
realsigprof_fast(int sysnum,int nsysarg,int error)260910341SRoger.Faulkner@Sun.COM realsigprof_fast(int sysnum, int nsysarg, int error)
261010341SRoger.Faulkner@Sun.COM {
261110341SRoger.Faulkner@Sun.COM 	kthread_t *t = curthread;
261210341SRoger.Faulkner@Sun.COM 	proc_t *p = ttoproc(t);
261310341SRoger.Faulkner@Sun.COM 	klwp_t *lwp = ttolwp(t);
261410341SRoger.Faulkner@Sun.COM 	k_siginfo_t *sip = &lwp->lwp_siginfo;
261510341SRoger.Faulkner@Sun.COM 	void (*func)();
261610341SRoger.Faulkner@Sun.COM 	int rc;
261710341SRoger.Faulkner@Sun.COM 	int code;
261810341SRoger.Faulkner@Sun.COM 
261910341SRoger.Faulkner@Sun.COM 	/*
262010341SRoger.Faulkner@Sun.COM 	 * We don't need to acquire p->p_lock here;
262110341SRoger.Faulkner@Sun.COM 	 * we are manipulating thread-private data.
262210341SRoger.Faulkner@Sun.COM 	 */
262310341SRoger.Faulkner@Sun.COM 	func = PTOU(p)->u_signal[SIGPROF - 1];
262410341SRoger.Faulkner@Sun.COM 	if (p->p_rprof_cyclic == CYCLIC_NONE ||
262510341SRoger.Faulkner@Sun.COM 	    func == SIG_DFL || func == SIG_IGN) {
262610341SRoger.Faulkner@Sun.COM 		bzero(t->t_rprof, sizeof (*t->t_rprof));
262710341SRoger.Faulkner@Sun.COM 		return;
262810341SRoger.Faulkner@Sun.COM 	}
262910341SRoger.Faulkner@Sun.COM 	if (lwp->lwp_cursig != 0 ||
263010341SRoger.Faulkner@Sun.COM 	    lwp->lwp_curinfo != NULL ||
263110341SRoger.Faulkner@Sun.COM 	    sigismember(&t->t_hold, SIGPROF)) {
263210341SRoger.Faulkner@Sun.COM 		return;
263310341SRoger.Faulkner@Sun.COM 	}
263410341SRoger.Faulkner@Sun.COM 	sip->si_signo = SIGPROF;
263510341SRoger.Faulkner@Sun.COM 	sip->si_code = PROF_SIG;
263610341SRoger.Faulkner@Sun.COM 	sip->si_errno = error;
263710341SRoger.Faulkner@Sun.COM 	hrt2ts(gethrtime(), &sip->si_tstamp);
263810341SRoger.Faulkner@Sun.COM 	sip->si_syscall = sysnum;
263910341SRoger.Faulkner@Sun.COM 	sip->si_nsysarg = nsysarg;
264010341SRoger.Faulkner@Sun.COM 	sip->si_fault = lwp->lwp_lastfault;
264110341SRoger.Faulkner@Sun.COM 	sip->si_faddr = lwp->lwp_lastfaddr;
264210341SRoger.Faulkner@Sun.COM 	lwp->lwp_lastfault = 0;
264310341SRoger.Faulkner@Sun.COM 	lwp->lwp_lastfaddr = NULL;
264410341SRoger.Faulkner@Sun.COM 	if (t->t_flag & T_TOMASK)
264510341SRoger.Faulkner@Sun.COM 		t->t_flag &= ~T_TOMASK;
264610341SRoger.Faulkner@Sun.COM 	else
264710341SRoger.Faulkner@Sun.COM 		lwp->lwp_sigoldmask = t->t_hold;
264810341SRoger.Faulkner@Sun.COM 	sigorset(&t->t_hold, &PTOU(p)->u_sigmask[SIGPROF - 1]);
264910341SRoger.Faulkner@Sun.COM 	if (!sigismember(&PTOU(p)->u_signodefer, SIGPROF))
265010341SRoger.Faulkner@Sun.COM 		sigaddset(&t->t_hold, SIGPROF);
265110341SRoger.Faulkner@Sun.COM 	lwp->lwp_extsig = 0;
265210341SRoger.Faulkner@Sun.COM 	lwp->lwp_ru.nsignals++;
265310341SRoger.Faulkner@Sun.COM 	if (p->p_model == DATAMODEL_NATIVE)
265410341SRoger.Faulkner@Sun.COM 		rc = sendsig(SIGPROF, sip, func);
265510341SRoger.Faulkner@Sun.COM #ifdef _SYSCALL32_IMPL
265610341SRoger.Faulkner@Sun.COM 	else
265710341SRoger.Faulkner@Sun.COM 		rc = sendsig32(SIGPROF, sip, func);
265810341SRoger.Faulkner@Sun.COM #endif	/* _SYSCALL32_IMPL */
265910341SRoger.Faulkner@Sun.COM 	sip->si_signo = 0;
266010341SRoger.Faulkner@Sun.COM 	bzero(t->t_rprof, sizeof (*t->t_rprof));
266110341SRoger.Faulkner@Sun.COM 	if (rc == 0) {
266210341SRoger.Faulkner@Sun.COM 		/*
266310341SRoger.Faulkner@Sun.COM 		 * sendsig() failed; we must dump core with a SIGSEGV.
266410341SRoger.Faulkner@Sun.COM 		 * See psig().  This code is copied from there.
266510341SRoger.Faulkner@Sun.COM 		 */
266610341SRoger.Faulkner@Sun.COM 		lwp->lwp_cursig = SIGSEGV;
266710341SRoger.Faulkner@Sun.COM 		code = CLD_KILLED;
266810341SRoger.Faulkner@Sun.COM 		proc_is_exiting(p);
266910341SRoger.Faulkner@Sun.COM 		if (exitlwps(1) != 0) {
267010341SRoger.Faulkner@Sun.COM 			mutex_enter(&p->p_lock);
267110341SRoger.Faulkner@Sun.COM 			lwp_exit();
267210341SRoger.Faulkner@Sun.COM 		}
267311861SMarek.Pospisil@Sun.COM 		if (audit_active == C2AUDIT_LOADED)
267410341SRoger.Faulkner@Sun.COM 			audit_core_start(SIGSEGV);
267510341SRoger.Faulkner@Sun.COM 		if (core(SIGSEGV, 0) == 0)
267610341SRoger.Faulkner@Sun.COM 			code = CLD_DUMPED;
267711861SMarek.Pospisil@Sun.COM 		if (audit_active == C2AUDIT_LOADED)
267810341SRoger.Faulkner@Sun.COM 			audit_core_finish(code);
267910341SRoger.Faulkner@Sun.COM 		exit(code, SIGSEGV);
268010341SRoger.Faulkner@Sun.COM 	}
268110341SRoger.Faulkner@Sun.COM }
268210341SRoger.Faulkner@Sun.COM 
268310341SRoger.Faulkner@Sun.COM /*
26849870SRoger.Faulkner@Sun.COM  * Arrange for the real time profiling signal to be dispatched.
26859870SRoger.Faulkner@Sun.COM  */
26869870SRoger.Faulkner@Sun.COM void
realsigprof(int sysnum,int nsysarg,int error)26879870SRoger.Faulkner@Sun.COM realsigprof(int sysnum, int nsysarg, int error)
26889870SRoger.Faulkner@Sun.COM {
268910341SRoger.Faulkner@Sun.COM 	kthread_t *t = curthread;
269010341SRoger.Faulkner@Sun.COM 	proc_t *p = ttoproc(t);
26919870SRoger.Faulkner@Sun.COM 
269210341SRoger.Faulkner@Sun.COM 	if (t->t_rprof->rp_anystate == 0)
26939870SRoger.Faulkner@Sun.COM 		return;
269410341SRoger.Faulkner@Sun.COM 
269510341SRoger.Faulkner@Sun.COM 	schedctl_finish_sigblock(t);
269610341SRoger.Faulkner@Sun.COM 
269710341SRoger.Faulkner@Sun.COM 	/* test for any activity that requires p->p_lock */
269810341SRoger.Faulkner@Sun.COM 	if (tracing(p, SIGPROF) || pr_watch_active(p) ||
269910341SRoger.Faulkner@Sun.COM 	    sigismember(&PTOU(p)->u_sigresethand, SIGPROF)) {
270010341SRoger.Faulkner@Sun.COM 		/* do it the classic slow way */
270110341SRoger.Faulkner@Sun.COM 		realsigprof_slow(sysnum, nsysarg, error);
270210341SRoger.Faulkner@Sun.COM 	} else {
270310341SRoger.Faulkner@Sun.COM 		/* do it the cheating-a-little fast way */
270410341SRoger.Faulkner@Sun.COM 		realsigprof_fast(sysnum, nsysarg, error);
27059870SRoger.Faulkner@Sun.COM 	}
27069870SRoger.Faulkner@Sun.COM }
27079870SRoger.Faulkner@Sun.COM 
27080Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate /*
27110Sstevel@tonic-gate  * It's tricky to transmit a sigval between 32-bit and 64-bit
27120Sstevel@tonic-gate  * process, since in the 64-bit world, a pointer and an integer
27130Sstevel@tonic-gate  * are different sizes.  Since we're constrained by the standards
27140Sstevel@tonic-gate  * world not to change the types, and it's unclear how useful it is
27150Sstevel@tonic-gate  * to send pointers between address spaces this way, we preserve
27160Sstevel@tonic-gate  * the 'int' interpretation for 32-bit processes interoperating
27170Sstevel@tonic-gate  * with 64-bit processes.  The full semantics (pointers or integers)
27180Sstevel@tonic-gate  * are available for N-bit processes interoperating with N-bit
27190Sstevel@tonic-gate  * processes.
27200Sstevel@tonic-gate  */
27210Sstevel@tonic-gate void
siginfo_kto32(const k_siginfo_t * src,siginfo32_t * dest)27220Sstevel@tonic-gate siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
27230Sstevel@tonic-gate {
27240Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	/*
27270Sstevel@tonic-gate 	 * The absolute minimum content is si_signo and si_code.
27280Sstevel@tonic-gate 	 */
27290Sstevel@tonic-gate 	dest->si_signo = src->si_signo;
27300Sstevel@tonic-gate 	if ((dest->si_code = src->si_code) == SI_NOINFO)
27310Sstevel@tonic-gate 		return;
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	/*
27340Sstevel@tonic-gate 	 * A siginfo generated by user level is structured
27350Sstevel@tonic-gate 	 * differently from one generated by the kernel.
27360Sstevel@tonic-gate 	 */
27370Sstevel@tonic-gate 	if (SI_FROMUSER(src)) {
27380Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
27390Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
27400Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
27410Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
27420Sstevel@tonic-gate 		if (SI_CANQUEUE(src->si_code))
27430Sstevel@tonic-gate 			dest->si_value.sival_int =
27445297Sbpramod 			    (int32_t)src->si_value.sival_int;
27450Sstevel@tonic-gate 		return;
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 	dest->si_errno = src->si_errno;
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 	switch (src->si_signo) {
27510Sstevel@tonic-gate 	default:
27520Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
27530Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
27540Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
27550Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
27560Sstevel@tonic-gate 		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
27570Sstevel@tonic-gate 		break;
27580Sstevel@tonic-gate 	case SIGCLD:
27590Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
27600Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
27610Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
27620Sstevel@tonic-gate 		dest->si_status = src->si_status;
27630Sstevel@tonic-gate 		dest->si_stime = src->si_stime;
27640Sstevel@tonic-gate 		dest->si_utime = src->si_utime;
27650Sstevel@tonic-gate 		break;
27660Sstevel@tonic-gate 	case SIGSEGV:
27670Sstevel@tonic-gate 	case SIGBUS:
27680Sstevel@tonic-gate 	case SIGILL:
27690Sstevel@tonic-gate 	case SIGTRAP:
27700Sstevel@tonic-gate 	case SIGFPE:
27710Sstevel@tonic-gate 	case SIGEMT:
27720Sstevel@tonic-gate 		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
27730Sstevel@tonic-gate 		dest->si_trapno = src->si_trapno;
27740Sstevel@tonic-gate 		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
27750Sstevel@tonic-gate 		break;
27760Sstevel@tonic-gate 	case SIGPOLL:
27770Sstevel@tonic-gate 	case SIGXFSZ:
27780Sstevel@tonic-gate 		dest->si_fd = src->si_fd;
27790Sstevel@tonic-gate 		dest->si_band = src->si_band;
27800Sstevel@tonic-gate 		break;
27810Sstevel@tonic-gate 	case SIGPROF:
27820Sstevel@tonic-gate 		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
27830Sstevel@tonic-gate 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
27840Sstevel@tonic-gate 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
27850Sstevel@tonic-gate 		dest->si_syscall = src->si_syscall;
27860Sstevel@tonic-gate 		dest->si_nsysarg = src->si_nsysarg;
27870Sstevel@tonic-gate 		dest->si_fault = src->si_fault;
27880Sstevel@tonic-gate 		break;
27890Sstevel@tonic-gate 	}
27900Sstevel@tonic-gate }
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate void
siginfo_32tok(const siginfo32_t * src,k_siginfo_t * dest)27930Sstevel@tonic-gate siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
27940Sstevel@tonic-gate {
27950Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 	/*
27980Sstevel@tonic-gate 	 * The absolute minimum content is si_signo and si_code.
27990Sstevel@tonic-gate 	 */
28000Sstevel@tonic-gate 	dest->si_signo = src->si_signo;
28010Sstevel@tonic-gate 	if ((dest->si_code = src->si_code) == SI_NOINFO)
28020Sstevel@tonic-gate 		return;
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	/*
28050Sstevel@tonic-gate 	 * A siginfo generated by user level is structured
28060Sstevel@tonic-gate 	 * differently from one generated by the kernel.
28070Sstevel@tonic-gate 	 */
28080Sstevel@tonic-gate 	if (SI_FROMUSER(src)) {
28090Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
28100Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
28110Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
28120Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
28130Sstevel@tonic-gate 		if (SI_CANQUEUE(src->si_code))
28140Sstevel@tonic-gate 			dest->si_value.sival_int =
28155297Sbpramod 			    (int)src->si_value.sival_int;
28160Sstevel@tonic-gate 		return;
28170Sstevel@tonic-gate 	}
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 	dest->si_errno = src->si_errno;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	switch (src->si_signo) {
28220Sstevel@tonic-gate 	default:
28230Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
28240Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
28250Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
28260Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
28270Sstevel@tonic-gate 		dest->si_value.sival_int = (int)src->si_value.sival_int;
28280Sstevel@tonic-gate 		break;
28290Sstevel@tonic-gate 	case SIGCLD:
28300Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
28310Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
28320Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
28330Sstevel@tonic-gate 		dest->si_status = src->si_status;
28340Sstevel@tonic-gate 		dest->si_stime = src->si_stime;
28350Sstevel@tonic-gate 		dest->si_utime = src->si_utime;
28360Sstevel@tonic-gate 		break;
28370Sstevel@tonic-gate 	case SIGSEGV:
28380Sstevel@tonic-gate 	case SIGBUS:
28390Sstevel@tonic-gate 	case SIGILL:
28400Sstevel@tonic-gate 	case SIGTRAP:
28410Sstevel@tonic-gate 	case SIGFPE:
28420Sstevel@tonic-gate 	case SIGEMT:
28430Sstevel@tonic-gate 		dest->si_addr = (void *)(uintptr_t)src->si_addr;
28440Sstevel@tonic-gate 		dest->si_trapno = src->si_trapno;
28450Sstevel@tonic-gate 		dest->si_pc = (void *)(uintptr_t)src->si_pc;
28460Sstevel@tonic-gate 		break;
28470Sstevel@tonic-gate 	case SIGPOLL:
28480Sstevel@tonic-gate 	case SIGXFSZ:
28490Sstevel@tonic-gate 		dest->si_fd = src->si_fd;
28500Sstevel@tonic-gate 		dest->si_band = src->si_band;
28510Sstevel@tonic-gate 		break;
28520Sstevel@tonic-gate 	case SIGPROF:
28530Sstevel@tonic-gate 		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
28540Sstevel@tonic-gate 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
28550Sstevel@tonic-gate 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
28560Sstevel@tonic-gate 		dest->si_syscall = src->si_syscall;
28570Sstevel@tonic-gate 		dest->si_nsysarg = src->si_nsysarg;
28580Sstevel@tonic-gate 		dest->si_fault = src->si_fault;
28590Sstevel@tonic-gate 		break;
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate }
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2864