xref: /illumos-gate/usr/src/uts/common/os/sig.c (revision 3d729aec)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f841f6adSraf  * Common Development and Distribution License (the "License").
6f841f6adSraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
23005d3febSMarek Pospisil  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2519d32b9aSRobert Mustacchi  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/poll_impl.h> /* only needed for kludge in sigwaiting_send() */
417c478bd9Sstevel@tonic-gate #include <sys/signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
437c478bd9Sstevel@tonic-gate #include <sys/fault.h>
447c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
457c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/wait.h>
477c478bd9Sstevel@tonic-gate #include <sys/class.h>
487c478bd9Sstevel@tonic-gate #include <sys/mman.h>
497c478bd9Sstevel@tonic-gate #include <sys/procset.h>
507c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
527c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <vm/as.h>
557c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
567c478bd9Sstevel@tonic-gate #include <c2/audit.h>
577c478bd9Sstevel@tonic-gate #include <sys/core.h>
587c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
597c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
60e0cf54a5SRoger A. Faulkner #include <sys/cyclic.h>
617c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
627c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
63*3d729aecSJerry Jelinek #include <sys/signalfd.h>
647c478bd9Sstevel@tonic-gate 
65bdf0047cSRoger A. Faulkner const k_sigset_t nullsmask = {0, 0, 0};
667c478bd9Sstevel@tonic-gate 
67bdf0047cSRoger A. Faulkner const k_sigset_t fillset =	/* MUST be contiguous */
68bdf0047cSRoger A. Faulkner 	{FILLSET0, FILLSET1, FILLSET2};
697c478bd9Sstevel@tonic-gate 
70bdf0047cSRoger A. Faulkner const k_sigset_t cantmask =
71bdf0047cSRoger A. Faulkner 	{CANTMASK0, CANTMASK1, CANTMASK2};
727c478bd9Sstevel@tonic-gate 
73bdf0047cSRoger A. Faulkner const k_sigset_t cantreset =
74bdf0047cSRoger A. Faulkner 	{(sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGPWR)), 0, 0};
757c478bd9Sstevel@tonic-gate 
76bdf0047cSRoger A. Faulkner const k_sigset_t ignoredefault =
77bdf0047cSRoger A. Faulkner 	{(sigmask(SIGCONT)|sigmask(SIGCLD)|sigmask(SIGPWR)
78bdf0047cSRoger A. Faulkner 	|sigmask(SIGWINCH)|sigmask(SIGURG)|sigmask(SIGWAITING)),
79bdf0047cSRoger A. Faulkner 	(sigmask(SIGLWP)|sigmask(SIGCANCEL)|sigmask(SIGFREEZE)
80bdf0047cSRoger A. Faulkner 	|sigmask(SIGTHAW)|sigmask(SIGXRES)|sigmask(SIGJVM1)
8119d32b9aSRobert Mustacchi 	|sigmask(SIGJVM2)|sigmask(SIGINFO)), 0};
827c478bd9Sstevel@tonic-gate 
83bdf0047cSRoger A. Faulkner const k_sigset_t stopdefault =
84bdf0047cSRoger A. Faulkner 	{(sigmask(SIGSTOP)|sigmask(SIGTSTP)|sigmask(SIGTTOU)|sigmask(SIGTTIN)),
85bdf0047cSRoger A. Faulkner 	0, 0};
867c478bd9Sstevel@tonic-gate 
87bdf0047cSRoger A. Faulkner const k_sigset_t coredefault =
88bdf0047cSRoger A. Faulkner 	{(sigmask(SIGQUIT)|sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGIOT)
89bdf0047cSRoger A. Faulkner 	|sigmask(SIGEMT)|sigmask(SIGFPE)|sigmask(SIGBUS)|sigmask(SIGSEGV)
90bdf0047cSRoger A. Faulkner 	|sigmask(SIGSYS)|sigmask(SIGXCPU)|sigmask(SIGXFSZ)), 0, 0};
917c478bd9Sstevel@tonic-gate 
92bdf0047cSRoger A. Faulkner const k_sigset_t holdvfork =
93bdf0047cSRoger A. Faulkner 	{(sigmask(SIGTTOU)|sigmask(SIGTTIN)|sigmask(SIGTSTP)), 0, 0};
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static	int	isjobstop(int);
967c478bd9Sstevel@tonic-gate static	void	post_sigcld(proc_t *, sigqueue_t *);
977c478bd9Sstevel@tonic-gate 
98*3d729aecSJerry Jelinek 
99*3d729aecSJerry Jelinek /*
100*3d729aecSJerry Jelinek  * signalfd helper function which is set when the signalfd driver loads.
101*3d729aecSJerry Jelinek  */
102*3d729aecSJerry Jelinek void (*sigfd_exit_helper)();
103*3d729aecSJerry Jelinek 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Internal variables for counting number of user thread stop requests posted.
1067c478bd9Sstevel@tonic-gate  * They may not be accurate at some special situation such as that a virtually
1077c478bd9Sstevel@tonic-gate  * stopped thread starts to run.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static int num_utstop;
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Internal variables for broadcasting an event when all thread stop requests
1127c478bd9Sstevel@tonic-gate  * are processed.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate static kcondvar_t utstop_cv;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static kmutex_t thread_stop_lock;
1177c478bd9Sstevel@tonic-gate void del_one_utstop(void);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Send the specified signal to the specified process.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate void
psignal(proc_t * p,int sig)1237c478bd9Sstevel@tonic-gate psignal(proc_t *p, int sig)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1267c478bd9Sstevel@tonic-gate 	sigtoproc(p, NULL, sig);
1277c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Send the specified signal to the specified thread.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate void
tsignal(kthread_t * t,int sig)1347c478bd9Sstevel@tonic-gate tsignal(kthread_t *t, int sig)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1397c478bd9Sstevel@tonic-gate 	sigtoproc(p, t, sig);
1407c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
signal_is_blocked(kthread_t * t,int sig)1447c478bd9Sstevel@tonic-gate signal_is_blocked(kthread_t *t, int sig)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	return (sigismember(&t->t_hold, sig) ||
1477c478bd9Sstevel@tonic-gate 	    (schedctl_sigblock(t) && !sigismember(&cantmask, sig)));
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Return true if the signal can safely be discarded on generation.
1527c478bd9Sstevel@tonic-gate  * That is, if there is no need for the signal on the receiving end.
1537c478bd9Sstevel@tonic-gate  * The answer is true if the process is a zombie or
1547c478bd9Sstevel@tonic-gate  * if all of these conditions are true:
1557c478bd9Sstevel@tonic-gate  *	the signal is being ignored
1567c478bd9Sstevel@tonic-gate  *	the process is single-threaded
1577c478bd9Sstevel@tonic-gate  *	the signal is not being traced by /proc
1587c478bd9Sstevel@tonic-gate  * 	the signal is not blocked by the process
1599524a69fSRoger A. Faulkner  *	the signal is not being accepted via sigwait()
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static int
sig_discardable(proc_t * p,int sig)1627c478bd9Sstevel@tonic-gate sig_discardable(proc_t *p, int sig)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	kthread_t *t = p->p_tlist;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (t == NULL ||		/* if zombie or ... */
1677c478bd9Sstevel@tonic-gate 	    (sigismember(&p->p_ignore, sig) &&	/* signal is ignored */
1687c478bd9Sstevel@tonic-gate 	    t->t_forw == t &&			/* and single-threaded */
1697c478bd9Sstevel@tonic-gate 	    !tracing(p, sig) &&			/* and no /proc tracing */
1709524a69fSRoger A. Faulkner 	    !signal_is_blocked(t, sig) &&	/* and signal not blocked */
1719524a69fSRoger A. Faulkner 	    !sigismember(&t->t_sigwait, sig)));	/* and not being accepted */
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Return true if this thread is going to eat this signal soon.
17697eda132Sraf  * Note that, if the signal is SIGKILL, we force stopped threads to be
17797eda132Sraf  * set running (to make SIGKILL be a sure kill), but only if the process
17897eda132Sraf  * is not currently locked by /proc (the P_PR_LOCK flag).  Code in /proc
17997eda132Sraf  * relies on the fact that a process will not change shape while P_PR_LOCK
18097eda132Sraf  * is set (it drops and reacquires p->p_lock while leaving P_PR_LOCK set).
18197eda132Sraf  * We wish that we could simply call prbarrier() below, in sigtoproc(), to
18297eda132Sraf  * ensure that the process is not locked by /proc, but prbarrier() drops
18397eda132Sraf  * and reacquires p->p_lock and dropping p->p_lock here would be damaging.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate int
eat_signal(kthread_t * t,int sig)1867c478bd9Sstevel@tonic-gate eat_signal(kthread_t *t, int sig)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	int rval = 0;
1897c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
1927c478bd9Sstevel@tonic-gate 	 * Do not do anything if the target thread has the signal blocked.
1937c478bd9Sstevel@tonic-gate 	 */
1947c478bd9Sstevel@tonic-gate 	if (!signal_is_blocked(t, sig)) {
1957c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;	/* have thread do an issig */
196c97ad5cdSakolb 		if (ISWAKEABLE(t) || ISWAITING(t)) {
1977c478bd9Sstevel@tonic-gate 			setrun_locked(t);
1987c478bd9Sstevel@tonic-gate 			rval = 1;
19997eda132Sraf 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL &&
20097eda132Sraf 		    !(ttoproc(t)->p_proc_flag & P_PR_LOCK)) {
2017c478bd9Sstevel@tonic-gate 			ttoproc(t)->p_stopsig = 0;
2027c478bd9Sstevel@tonic-gate 			t->t_dtrace_stop = 0;
2037c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_XSTART | TS_PSTART;
2047c478bd9Sstevel@tonic-gate 			setrun_locked(t);
2057c478bd9Sstevel@tonic-gate 		} else if (t != curthread && t->t_state == TS_ONPROC) {
206a8e07b22Smishra 			aston(t);	/* make it do issig promptly */
207a8e07b22Smishra 			if (t->t_cpu != CPU)
2087c478bd9Sstevel@tonic-gate 				poke_cpu(t->t_cpu->cpu_id);
2097c478bd9Sstevel@tonic-gate 			rval = 1;
2107c478bd9Sstevel@tonic-gate 		} else if (t->t_state == TS_RUN) {
2117c478bd9Sstevel@tonic-gate 			rval = 1;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return (rval);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Post a signal.
2207c478bd9Sstevel@tonic-gate  * If a non-null thread pointer is passed, then post the signal
2217c478bd9Sstevel@tonic-gate  * to the thread/lwp, otherwise post the signal to the process.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate void
sigtoproc(proc_t * p,kthread_t * t,int sig)2247c478bd9Sstevel@tonic-gate sigtoproc(proc_t *p, kthread_t *t, int sig)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	kthread_t *tt;
2277c478bd9Sstevel@tonic-gate 	int ext = !(curproc->p_flag & SSYS) &&
2287c478bd9Sstevel@tonic-gate 	    (curproc->p_ct_process != p->p_ct_process);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
2317c478bd9Sstevel@tonic-gate 
23235a5a358SJonathan Adams 	/* System processes don't get signals */
23335a5a358SJonathan Adams 	if (sig <= 0 || sig >= NSIG || (p->p_flag & SSYS))
2347c478bd9Sstevel@tonic-gate 		return;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Regardless of origin or directedness,
2387c478bd9Sstevel@tonic-gate 	 * SIGKILL kills all lwps in the process immediately
2397c478bd9Sstevel@tonic-gate 	 * and jobcontrol signals affect all lwps in the process.
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	if (sig == SIGKILL) {
2427c478bd9Sstevel@tonic-gate 		p->p_flag |= SKILLED | (ext ? SEXTKILLED : 0);
2437c478bd9Sstevel@tonic-gate 		t = NULL;
2447c478bd9Sstevel@tonic-gate 	} else if (sig == SIGCONT) {
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * The SSCONT flag will remain set until a stopping
2477c478bd9Sstevel@tonic-gate 		 * signal comes in (below).  This is harmless.
2487c478bd9Sstevel@tonic-gate 		 */
2497c478bd9Sstevel@tonic-gate 		p->p_flag |= SSCONT;
2507c478bd9Sstevel@tonic-gate 		sigdelq(p, NULL, SIGSTOP);
2517c478bd9Sstevel@tonic-gate 		sigdelq(p, NULL, SIGTSTP);
2527c478bd9Sstevel@tonic-gate 		sigdelq(p, NULL, SIGTTOU);
2537c478bd9Sstevel@tonic-gate 		sigdelq(p, NULL, SIGTTIN);
2547c478bd9Sstevel@tonic-gate 		sigdiffset(&p->p_sig, &stopdefault);
2557c478bd9Sstevel@tonic-gate 		sigdiffset(&p->p_extsig, &stopdefault);
2567c478bd9Sstevel@tonic-gate 		p->p_stopsig = 0;
2577c478bd9Sstevel@tonic-gate 		if ((tt = p->p_tlist) != NULL) {
2587c478bd9Sstevel@tonic-gate 			do {
2597c478bd9Sstevel@tonic-gate 				sigdelq(p, tt, SIGSTOP);
2607c478bd9Sstevel@tonic-gate 				sigdelq(p, tt, SIGTSTP);
2617c478bd9Sstevel@tonic-gate 				sigdelq(p, tt, SIGTTOU);
2627c478bd9Sstevel@tonic-gate 				sigdelq(p, tt, SIGTTIN);
2637c478bd9Sstevel@tonic-gate 				sigdiffset(&tt->t_sig, &stopdefault);
2647c478bd9Sstevel@tonic-gate 				sigdiffset(&tt->t_extsig, &stopdefault);
2657c478bd9Sstevel@tonic-gate 			} while ((tt = tt->t_forw) != p->p_tlist);
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		if ((tt = p->p_tlist) != NULL) {
2687c478bd9Sstevel@tonic-gate 			do {
2697c478bd9Sstevel@tonic-gate 				thread_lock(tt);
2707c478bd9Sstevel@tonic-gate 				if (tt->t_state == TS_STOPPED &&
2717c478bd9Sstevel@tonic-gate 				    tt->t_whystop == PR_JOBCONTROL) {
2727c478bd9Sstevel@tonic-gate 					tt->t_schedflag |= TS_XSTART;
2737c478bd9Sstevel@tonic-gate 					setrun_locked(tt);
2747c478bd9Sstevel@tonic-gate 				}
2757c478bd9Sstevel@tonic-gate 				thread_unlock(tt);
2767c478bd9Sstevel@tonic-gate 			} while ((tt = tt->t_forw) != p->p_tlist);
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 	} else if (sigismember(&stopdefault, sig)) {
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * This test has a race condition which we can't fix:
2817c478bd9Sstevel@tonic-gate 		 * By the time the stopping signal is received by
2827c478bd9Sstevel@tonic-gate 		 * the target process/thread, the signal handler
2837c478bd9Sstevel@tonic-gate 		 * and/or the detached state might have changed.
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
2867c478bd9Sstevel@tonic-gate 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
2877c478bd9Sstevel@tonic-gate 			p->p_flag &= ~SSCONT;
2887c478bd9Sstevel@tonic-gate 		sigdelq(p, NULL, SIGCONT);
2897c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_sig, SIGCONT);
2907c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_extsig, SIGCONT);
2917c478bd9Sstevel@tonic-gate 		if ((tt = p->p_tlist) != NULL) {
2927c478bd9Sstevel@tonic-gate 			do {
2937c478bd9Sstevel@tonic-gate 				sigdelq(p, tt, SIGCONT);
2947c478bd9Sstevel@tonic-gate 				sigdelset(&tt->t_sig, SIGCONT);
2957c478bd9Sstevel@tonic-gate 				sigdelset(&tt->t_extsig, SIGCONT);
2967c478bd9Sstevel@tonic-gate 			} while ((tt = tt->t_forw) != p->p_tlist);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (sig_discardable(p, sig)) {
3017c478bd9Sstevel@tonic-gate 		DTRACE_PROC3(signal__discard, kthread_t *, p->p_tlist,
3027c478bd9Sstevel@tonic-gate 		    proc_t *, p, int, sig);
3037c478bd9Sstevel@tonic-gate 		return;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (t != NULL) {
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * This is a directed signal, wake up the lwp.
3097c478bd9Sstevel@tonic-gate 		 */
3107c478bd9Sstevel@tonic-gate 		sigaddset(&t->t_sig, sig);
3117c478bd9Sstevel@tonic-gate 		if (ext)
3127c478bd9Sstevel@tonic-gate 			sigaddset(&t->t_extsig, sig);
3137c478bd9Sstevel@tonic-gate 		thread_lock(t);
3147c478bd9Sstevel@tonic-gate 		(void) eat_signal(t, sig);
3157c478bd9Sstevel@tonic-gate 		thread_unlock(t);
3167c478bd9Sstevel@tonic-gate 		DTRACE_PROC2(signal__send, kthread_t *, t, int, sig);
317*3d729aecSJerry Jelinek 		if (p->p_sigfd != NULL && ((sigfd_proc_state_t *)
318*3d729aecSJerry Jelinek 		    (p->p_sigfd))->sigfd_pollwake_cb != NULL)
319*3d729aecSJerry Jelinek 			(*((sigfd_proc_state_t *)(p->p_sigfd))->
320*3d729aecSJerry Jelinek 			    sigfd_pollwake_cb)(p, sig);
321*3d729aecSJerry Jelinek 
3227c478bd9Sstevel@tonic-gate 	} else if ((tt = p->p_tlist) != NULL) {
3237c478bd9Sstevel@tonic-gate 		/*
3247c478bd9Sstevel@tonic-gate 		 * Make sure that some lwp that already exists
3257c478bd9Sstevel@tonic-gate 		 * in the process fields the signal soon.
3267c478bd9Sstevel@tonic-gate 		 * Wake up an interruptibly sleeping lwp if necessary.
327cbdce38dSbpramod 		 * For SIGKILL make all of the lwps see the signal;
328cbdce38dSbpramod 		 * This is needed to guarantee a sure kill for processes
329cbdce38dSbpramod 		 * with a mix of realtime and non-realtime threads.
3307c478bd9Sstevel@tonic-gate 		 */
3317c478bd9Sstevel@tonic-gate 		int su = 0;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		sigaddset(&p->p_sig, sig);
3347c478bd9Sstevel@tonic-gate 		if (ext)
3357c478bd9Sstevel@tonic-gate 			sigaddset(&p->p_extsig, sig);
3367c478bd9Sstevel@tonic-gate 		do {
3377c478bd9Sstevel@tonic-gate 			thread_lock(tt);
338cbdce38dSbpramod 			if (eat_signal(tt, sig) && sig != SIGKILL) {
3397c478bd9Sstevel@tonic-gate 				thread_unlock(tt);
3407c478bd9Sstevel@tonic-gate 				break;
3417c478bd9Sstevel@tonic-gate 			}
342cbdce38dSbpramod 			if (SUSPENDED(tt))
3437c478bd9Sstevel@tonic-gate 				su++;
3447c478bd9Sstevel@tonic-gate 			thread_unlock(tt);
3457c478bd9Sstevel@tonic-gate 		} while ((tt = tt->t_forw) != p->p_tlist);
3467c478bd9Sstevel@tonic-gate 		/*
3477c478bd9Sstevel@tonic-gate 		 * If the process is deadlocked, make somebody run and die.
3487c478bd9Sstevel@tonic-gate 		 */
3497c478bd9Sstevel@tonic-gate 		if (sig == SIGKILL && p->p_stat != SIDL &&
35097eda132Sraf 		    p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
35197eda132Sraf 		    !(p->p_proc_flag & P_PR_LOCK)) {
3527c478bd9Sstevel@tonic-gate 			thread_lock(tt);
3537c478bd9Sstevel@tonic-gate 			p->p_lwprcnt++;
3547c478bd9Sstevel@tonic-gate 			tt->t_schedflag |= TS_CSTART;
3557c478bd9Sstevel@tonic-gate 			setrun_locked(tt);
3567c478bd9Sstevel@tonic-gate 			thread_unlock(tt);
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
360*3d729aecSJerry Jelinek 		if (p->p_sigfd != NULL && ((sigfd_proc_state_t *)
361*3d729aecSJerry Jelinek 		    (p->p_sigfd))->sigfd_pollwake_cb != NULL)
362*3d729aecSJerry Jelinek 			(*((sigfd_proc_state_t *)(p->p_sigfd))->
363*3d729aecSJerry Jelinek 			    sigfd_pollwake_cb)(p, sig);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int
isjobstop(int sig)3687c478bd9Sstevel@tonic-gate isjobstop(int sig)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
3737c478bd9Sstevel@tonic-gate 
374ae115bc7Smrj 	if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
375ae115bc7Smrj 	    sigismember(&stopdefault, sig)) {
3767c478bd9Sstevel@tonic-gate 		/*
3777c478bd9Sstevel@tonic-gate 		 * If SIGCONT has been posted since we promoted this signal
3787c478bd9Sstevel@tonic-gate 		 * from pending to current, then don't do a jobcontrol stop.
3797c478bd9Sstevel@tonic-gate 		 */
3807c478bd9Sstevel@tonic-gate 		if (!(p->p_flag & SSCONT) &&
3817c478bd9Sstevel@tonic-gate 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
3827c478bd9Sstevel@tonic-gate 		    curthread != p->p_agenttp) {
3837c478bd9Sstevel@tonic-gate 			sigqueue_t *sqp;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 			stop(PR_JOBCONTROL, sig);
3867c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
3877c478bd9Sstevel@tonic-gate 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
3887c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
3897c478bd9Sstevel@tonic-gate 			/*
3907c478bd9Sstevel@tonic-gate 			 * Only the first lwp to continue notifies the parent.
3917c478bd9Sstevel@tonic-gate 			 */
3927c478bd9Sstevel@tonic-gate 			if (p->p_pidflag & CLDCONT)
3937c478bd9Sstevel@tonic-gate 				siginfofree(sqp);
3947c478bd9Sstevel@tonic-gate 			else {
3957c478bd9Sstevel@tonic-gate 				p->p_pidflag |= CLDCONT;
3967c478bd9Sstevel@tonic-gate 				p->p_wcode = CLD_CONTINUED;
3977c478bd9Sstevel@tonic-gate 				p->p_wdata = SIGCONT;
3987c478bd9Sstevel@tonic-gate 				sigcld(p, sqp);
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
4017c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		return (1);
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 	return (0);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate /*
4097c478bd9Sstevel@tonic-gate  * Returns true if the current process has a signal to process, and
4107c478bd9Sstevel@tonic-gate  * the signal is not held.  The signal to process is put in p_cursig.
4117c478bd9Sstevel@tonic-gate  * This is asked at least once each time a process enters the system
4127c478bd9Sstevel@tonic-gate  * (though this can usually be done without actually calling issig by
4137c478bd9Sstevel@tonic-gate  * checking the pending signal masks).  A signal does not do anything
4147c478bd9Sstevel@tonic-gate  * directly to a process; it sets a flag that asks the process to do
4157c478bd9Sstevel@tonic-gate  * something to itself.
4167c478bd9Sstevel@tonic-gate  *
4177c478bd9Sstevel@tonic-gate  * The "why" argument indicates the allowable side-effects of the call:
4187c478bd9Sstevel@tonic-gate  *
4197c478bd9Sstevel@tonic-gate  * FORREAL:  Extract the next pending signal from p_sig into p_cursig;
4207c478bd9Sstevel@tonic-gate  * stop the process if a stop has been requested or if a traced signal
4217c478bd9Sstevel@tonic-gate  * is pending.
4227c478bd9Sstevel@tonic-gate  *
4237c478bd9Sstevel@tonic-gate  * JUSTLOOKING:  Don't stop the process, just indicate whether or not
4247c478bd9Sstevel@tonic-gate  * a signal might be pending (FORREAL is needed to tell for sure).
4257c478bd9Sstevel@tonic-gate  *
4267c478bd9Sstevel@tonic-gate  * XXX: Changes to the logic in these routines should be propagated
4277c478bd9Sstevel@tonic-gate  * to lm_sigispending().  See bug 1201594.
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static int issig_forreal(void);
4317c478bd9Sstevel@tonic-gate static int issig_justlooking(void);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate int
issig(int why)4347c478bd9Sstevel@tonic-gate issig(int why)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	ASSERT(why == FORREAL || why == JUSTLOOKING);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	return ((why == FORREAL)? issig_forreal() : issig_justlooking());
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate static int
issig_justlooking(void)4437c478bd9Sstevel@tonic-gate issig_justlooking(void)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
4467c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
4477c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
4487c478bd9Sstevel@tonic-gate 	k_sigset_t set;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * This function answers the question:
4527c478bd9Sstevel@tonic-gate 	 * "Is there any reason to call issig_forreal()?"
4537c478bd9Sstevel@tonic-gate 	 *
4547c478bd9Sstevel@tonic-gate 	 * We have to answer the question w/o grabbing any locks
4557c478bd9Sstevel@tonic-gate 	 * because we are (most likely) being called after we
4567c478bd9Sstevel@tonic-gate 	 * put ourselves on the sleep queue.
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (t->t_dtrace_stop | t->t_dtrace_sig)
4607c478bd9Sstevel@tonic-gate 		return (1);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * Another piece of complexity in this process.  When single-stepping a
4647c478bd9Sstevel@tonic-gate 	 * process, we don't want an intervening signal or TP_PAUSE request to
4657c478bd9Sstevel@tonic-gate 	 * suspend the current thread.  Otherwise, the controlling process will
4667c478bd9Sstevel@tonic-gate 	 * hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
4677c478bd9Sstevel@tonic-gate 	 * We will trigger any remaining signals when we re-enter the kernel on
4687c478bd9Sstevel@tonic-gate 	 * the single step trap.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
4717c478bd9Sstevel@tonic-gate 		return (0);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
4747c478bd9Sstevel@tonic-gate 	    (p->p_flag & (SEXITLWPS|SKILLED)) ||
4758548bf79Snr 	    (lwp->lwp_nostop == 0 &&
4768548bf79Snr 	    (p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
4778548bf79Snr 	    (t->t_proc_flag &
4788548bf79Snr 	    (TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
4797c478bd9Sstevel@tonic-gate 	    lwp->lwp_cursig)
4807c478bd9Sstevel@tonic-gate 		return (1);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (p->p_flag & SVFWAIT)
4837c478bd9Sstevel@tonic-gate 		return (0);
4847c478bd9Sstevel@tonic-gate 	set = p->p_sig;
4857c478bd9Sstevel@tonic-gate 	sigorset(&set, &t->t_sig);
4867c478bd9Sstevel@tonic-gate 	if (schedctl_sigblock(t))	/* all blockable signals blocked */
4877c478bd9Sstevel@tonic-gate 		sigandset(&set, &cantmask);
4887c478bd9Sstevel@tonic-gate 	else
4897c478bd9Sstevel@tonic-gate 		sigdiffset(&set, &t->t_hold);
4907c478bd9Sstevel@tonic-gate 	if (p->p_flag & SVFORK)
4917c478bd9Sstevel@tonic-gate 		sigdiffset(&set, &holdvfork);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (!sigisempty(&set)) {
4947c478bd9Sstevel@tonic-gate 		int sig;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		for (sig = 1; sig < NSIG; sig++) {
4977c478bd9Sstevel@tonic-gate 			if (sigismember(&set, sig) &&
4987c478bd9Sstevel@tonic-gate 			    (tracing(p, sig) ||
4999524a69fSRoger A. Faulkner 			    sigismember(&t->t_sigwait, sig) ||
5007c478bd9Sstevel@tonic-gate 			    !sigismember(&p->p_ignore, sig))) {
5017c478bd9Sstevel@tonic-gate 				/*
5027c478bd9Sstevel@tonic-gate 				 * Don't promote a signal that will stop
5037c478bd9Sstevel@tonic-gate 				 * the process when lwp_nostop is set.
5047c478bd9Sstevel@tonic-gate 				 */
5057c478bd9Sstevel@tonic-gate 				if (!lwp->lwp_nostop ||
5063f12bda8SRoger A. Faulkner 				    PTOU(p)->u_signal[sig-1] != SIG_DFL ||
5077c478bd9Sstevel@tonic-gate 				    !sigismember(&stopdefault, sig))
5087c478bd9Sstevel@tonic-gate 					return (1);
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	return (0);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static int
issig_forreal(void)5177c478bd9Sstevel@tonic-gate issig_forreal(void)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	int sig = 0, ext = 0;
5207c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
5217c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
5227c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
5237c478bd9Sstevel@tonic-gate 	int toproc = 0;
5247c478bd9Sstevel@tonic-gate 	int sigcld_found = 0;
5257c478bd9Sstevel@tonic-gate 	int nostop_break = 0;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5307c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (t->t_dtrace_stop | t->t_dtrace_sig) {
5337c478bd9Sstevel@tonic-gate 		if (t->t_dtrace_stop) {
5347c478bd9Sstevel@tonic-gate 			/*
5357c478bd9Sstevel@tonic-gate 			 * If DTrace's "stop" action has been invoked on us,
5367c478bd9Sstevel@tonic-gate 			 * set TP_PRSTOP.
5377c478bd9Sstevel@tonic-gate 			 */
5387c478bd9Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		if (t->t_dtrace_sig != 0) {
5427c478bd9Sstevel@tonic-gate 			k_siginfo_t info;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 			/*
5457c478bd9Sstevel@tonic-gate 			 * Post the signal generated as the result of
5467c478bd9Sstevel@tonic-gate 			 * DTrace's "raise" action as a normal signal before
5477c478bd9Sstevel@tonic-gate 			 * the full-fledged signal checking begins.
5487c478bd9Sstevel@tonic-gate 			 */
5497c478bd9Sstevel@tonic-gate 			bzero(&info, sizeof (info));
5507c478bd9Sstevel@tonic-gate 			info.si_signo = t->t_dtrace_sig;
5517c478bd9Sstevel@tonic-gate 			info.si_code = SI_DTRACE;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 			sigaddq(p, NULL, &info, KM_NOSLEEP);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 			t->t_dtrace_sig = 0;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	for (;;) {
5607c478bd9Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
5617c478bd9Sstevel@tonic-gate 			lwp->lwp_cursig = sig = SIGKILL;
5627c478bd9Sstevel@tonic-gate 			lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
563cbdce38dSbpramod 			t->t_sig_check = 1;
5647c478bd9Sstevel@tonic-gate 			break;
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * Another piece of complexity in this process.  When
5697c478bd9Sstevel@tonic-gate 		 * single-stepping a process, we don't want an intervening
5707c478bd9Sstevel@tonic-gate 		 * signal or TP_PAUSE request to suspend the current thread.
5717c478bd9Sstevel@tonic-gate 		 * Otherwise, the controlling process will hang beacuse we will
5727c478bd9Sstevel@tonic-gate 		 * be stopped with TS_PSTART set in t_schedflag.  We will
5737c478bd9Sstevel@tonic-gate 		 * trigger any remaining signals when we re-enter the kernel on
5747c478bd9Sstevel@tonic-gate 		 * the single step trap.
5757c478bd9Sstevel@tonic-gate 		 */
5767c478bd9Sstevel@tonic-gate 		if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
5777c478bd9Sstevel@tonic-gate 			sig = 0;
5787c478bd9Sstevel@tonic-gate 			break;
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		/*
5827c478bd9Sstevel@tonic-gate 		 * Hold the lwp here for watchpoint manipulation.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
5857c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
5867c478bd9Sstevel@tonic-gate 			continue;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
5907c478bd9Sstevel@tonic-gate 			if ((sig = lwp->lwp_cursig) != 0) {
5917c478bd9Sstevel@tonic-gate 				/*
5927c478bd9Sstevel@tonic-gate 				 * Make sure we call ISSIG() in post_syscall()
5937c478bd9Sstevel@tonic-gate 				 * to re-validate this current signal.
5947c478bd9Sstevel@tonic-gate 				 */
5957c478bd9Sstevel@tonic-gate 				t->t_sig_check = 1;
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			break;
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		/*
6017c478bd9Sstevel@tonic-gate 		 * If the request is PR_CHECKPOINT, ignore the rest of signals
6027c478bd9Sstevel@tonic-gate 		 * or requests.  Honor other stop requests or signals later.
6037c478bd9Sstevel@tonic-gate 		 * Go back to top of loop here to check if an exit or hold
6047c478bd9Sstevel@tonic-gate 		 * event has occurred while stopped.
6057c478bd9Sstevel@tonic-gate 		 */
6067c478bd9Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
6077c478bd9Sstevel@tonic-gate 			stop(PR_CHECKPOINT, 0);
6087c478bd9Sstevel@tonic-gate 			continue;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/*
6127c478bd9Sstevel@tonic-gate 		 * Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
6137c478bd9Sstevel@tonic-gate 		 * with signals or /proc.  Another lwp is executing fork1(),
6147c478bd9Sstevel@tonic-gate 		 * or is undergoing watchpoint activity (remapping a page),
6157c478bd9Sstevel@tonic-gate 		 * or is executing lwp_suspend() on this lwp.
6167c478bd9Sstevel@tonic-gate 		 * Again, go back to top of loop to check if an exit
6177c478bd9Sstevel@tonic-gate 		 * or hold event has occurred while stopped.
6187c478bd9Sstevel@tonic-gate 		 */
6197c478bd9Sstevel@tonic-gate 		if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
6208548bf79Snr 		    (t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
6217c478bd9Sstevel@tonic-gate 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
6227c478bd9Sstevel@tonic-gate 			continue;
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		/*
6267c478bd9Sstevel@tonic-gate 		 * Honor requested stop before dealing with the
6277c478bd9Sstevel@tonic-gate 		 * current signal; a debugger may change it.
6287c478bd9Sstevel@tonic-gate 		 * Do not want to go back to loop here since this is a special
6297c478bd9Sstevel@tonic-gate 		 * stop that means: make incremental progress before the next
6307c478bd9Sstevel@tonic-gate 		 * stop. The danger is that returning to top of loop would most
6317c478bd9Sstevel@tonic-gate 		 * likely drop the thread right back here to stop soon after it
6327c478bd9Sstevel@tonic-gate 		 * was continued, violating the incremental progress request.
6337c478bd9Sstevel@tonic-gate 		 */
6347c478bd9Sstevel@tonic-gate 		if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
6357c478bd9Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * If a debugger wants us to take a signal it will have
6397c478bd9Sstevel@tonic-gate 		 * left it in lwp->lwp_cursig.  If lwp_cursig has been cleared
6407c478bd9Sstevel@tonic-gate 		 * or if it's being ignored, we continue on looking for another
6417c478bd9Sstevel@tonic-gate 		 * signal.  Otherwise we return the specified signal, provided
6427c478bd9Sstevel@tonic-gate 		 * it's not a signal that causes a job control stop.
6437c478bd9Sstevel@tonic-gate 		 *
6447c478bd9Sstevel@tonic-gate 		 * When stopped on PR_JOBCONTROL, there is no current
6457c478bd9Sstevel@tonic-gate 		 * signal; we cancel lwp->lwp_cursig temporarily before
6467c478bd9Sstevel@tonic-gate 		 * calling isjobstop().  The current signal may be reset
6477c478bd9Sstevel@tonic-gate 		 * by a debugger while we are stopped in isjobstop().
6489524a69fSRoger A. Faulkner 		 *
6499524a69fSRoger A. Faulkner 		 * If the current thread is accepting the signal
6509524a69fSRoger A. Faulkner 		 * (via sigwait(), sigwaitinfo(), or sigtimedwait()),
6519524a69fSRoger A. Faulkner 		 * we allow the signal to be accepted, even if it is
6529524a69fSRoger A. Faulkner 		 * being ignored, and without causing a job control stop.
6537c478bd9Sstevel@tonic-gate 		 */
6547c478bd9Sstevel@tonic-gate 		if ((sig = lwp->lwp_cursig) != 0) {
6557c478bd9Sstevel@tonic-gate 			ext = lwp->lwp_extsig;
6567c478bd9Sstevel@tonic-gate 			lwp->lwp_cursig = 0;
6577c478bd9Sstevel@tonic-gate 			lwp->lwp_extsig = 0;
6589524a69fSRoger A. Faulkner 			if (sigismember(&t->t_sigwait, sig) ||
6599524a69fSRoger A. Faulkner 			    (!sigismember(&p->p_ignore, sig) &&
6609524a69fSRoger A. Faulkner 			    !isjobstop(sig))) {
6617c478bd9Sstevel@tonic-gate 				if (p->p_flag & (SEXITLWPS|SKILLED)) {
6627c478bd9Sstevel@tonic-gate 					sig = SIGKILL;
6637c478bd9Sstevel@tonic-gate 					ext = (p->p_flag & SEXTKILLED) != 0;
6647c478bd9Sstevel@tonic-gate 				}
6657c478bd9Sstevel@tonic-gate 				lwp->lwp_cursig = (uchar_t)sig;
6667c478bd9Sstevel@tonic-gate 				lwp->lwp_extsig = (uchar_t)ext;
6677c478bd9Sstevel@tonic-gate 				break;
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 			/*
6707c478bd9Sstevel@tonic-gate 			 * The signal is being ignored or it caused a
6717c478bd9Sstevel@tonic-gate 			 * job-control stop.  If another current signal
6727c478bd9Sstevel@tonic-gate 			 * has not been established, return the current
6737c478bd9Sstevel@tonic-gate 			 * siginfo, if any, to the memory manager.
6747c478bd9Sstevel@tonic-gate 			 */
6757c478bd9Sstevel@tonic-gate 			if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
6767c478bd9Sstevel@tonic-gate 				siginfofree(lwp->lwp_curinfo);
6777c478bd9Sstevel@tonic-gate 				lwp->lwp_curinfo = NULL;
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 			/*
6807c478bd9Sstevel@tonic-gate 			 * Loop around again in case we were stopped
6817c478bd9Sstevel@tonic-gate 			 * on a job control signal and a /proc stop
6827c478bd9Sstevel@tonic-gate 			 * request was posted or another current signal
6837c478bd9Sstevel@tonic-gate 			 * was established while we were stopped.
6847c478bd9Sstevel@tonic-gate 			 */
6857c478bd9Sstevel@tonic-gate 			continue;
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		if (p->p_stopsig && !lwp->lwp_nostop &&
6897c478bd9Sstevel@tonic-gate 		    curthread != p->p_agenttp) {
6907c478bd9Sstevel@tonic-gate 			/*
6917c478bd9Sstevel@tonic-gate 			 * Some lwp in the process has already stopped
6927c478bd9Sstevel@tonic-gate 			 * showing PR_JOBCONTROL.  This is a stop in
6937c478bd9Sstevel@tonic-gate 			 * sympathy with the other lwp, even if this
6947c478bd9Sstevel@tonic-gate 			 * lwp is blocking the stopping signal.
6957c478bd9Sstevel@tonic-gate 			 */
6967c478bd9Sstevel@tonic-gate 			stop(PR_JOBCONTROL, p->p_stopsig);
6977c478bd9Sstevel@tonic-gate 			continue;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		/*
7017c478bd9Sstevel@tonic-gate 		 * Loop on the pending signals until we find a
7027c478bd9Sstevel@tonic-gate 		 * non-held signal that is traced or not ignored.
7037c478bd9Sstevel@tonic-gate 		 * First check the signals pending for the lwp,
7047c478bd9Sstevel@tonic-gate 		 * then the signals pending for the process as a whole.
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 		for (;;) {
7079524a69fSRoger A. Faulkner 			if ((sig = fsig(&t->t_sig, t)) != 0) {
7087c478bd9Sstevel@tonic-gate 				toproc = 0;
7097c478bd9Sstevel@tonic-gate 				if (tracing(p, sig) ||
7109524a69fSRoger A. Faulkner 				    sigismember(&t->t_sigwait, sig) ||
7117c478bd9Sstevel@tonic-gate 				    !sigismember(&p->p_ignore, sig)) {
7127c478bd9Sstevel@tonic-gate 					if (sigismember(&t->t_extsig, sig))
7137c478bd9Sstevel@tonic-gate 						ext = 1;
7147c478bd9Sstevel@tonic-gate 					break;
7157c478bd9Sstevel@tonic-gate 				}
7167c478bd9Sstevel@tonic-gate 				sigdelset(&t->t_sig, sig);
7177c478bd9Sstevel@tonic-gate 				sigdelset(&t->t_extsig, sig);
7187c478bd9Sstevel@tonic-gate 				sigdelq(p, t, sig);
7197c478bd9Sstevel@tonic-gate 			} else if ((sig = fsig(&p->p_sig, t)) != 0) {
7207c478bd9Sstevel@tonic-gate 				if (sig == SIGCLD)
7217c478bd9Sstevel@tonic-gate 					sigcld_found = 1;
7227c478bd9Sstevel@tonic-gate 				toproc = 1;
7237c478bd9Sstevel@tonic-gate 				if (tracing(p, sig) ||
7249524a69fSRoger A. Faulkner 				    sigismember(&t->t_sigwait, sig) ||
7257c478bd9Sstevel@tonic-gate 				    !sigismember(&p->p_ignore, sig)) {
7267c478bd9Sstevel@tonic-gate 					if (sigismember(&p->p_extsig, sig))
7277c478bd9Sstevel@tonic-gate 						ext = 1;
7287c478bd9Sstevel@tonic-gate 					break;
7297c478bd9Sstevel@tonic-gate 				}
7307c478bd9Sstevel@tonic-gate 				sigdelset(&p->p_sig, sig);
7317c478bd9Sstevel@tonic-gate 				sigdelset(&p->p_extsig, sig);
7327c478bd9Sstevel@tonic-gate 				sigdelq(p, NULL, sig);
7337c478bd9Sstevel@tonic-gate 			} else {
7347c478bd9Sstevel@tonic-gate 				/* no signal was found */
7357c478bd9Sstevel@tonic-gate 				break;
7367c478bd9Sstevel@tonic-gate 			}
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		if (sig == 0) {	/* no signal was found */
7407c478bd9Sstevel@tonic-gate 			if (p->p_flag & (SEXITLWPS|SKILLED)) {
7417c478bd9Sstevel@tonic-gate 				lwp->lwp_cursig = SIGKILL;
7427c478bd9Sstevel@tonic-gate 				sig = SIGKILL;
7437c478bd9Sstevel@tonic-gate 				ext = (p->p_flag & SEXTKILLED) != 0;
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			break;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		/*
7497c478bd9Sstevel@tonic-gate 		 * If we have been informed not to stop (i.e., we are being
7507c478bd9Sstevel@tonic-gate 		 * called from within a network operation), then don't promote
7517c478bd9Sstevel@tonic-gate 		 * the signal at this time, just return the signal number.
7527c478bd9Sstevel@tonic-gate 		 * We will call issig() again later when it is safe.
7537c478bd9Sstevel@tonic-gate 		 *
7547c478bd9Sstevel@tonic-gate 		 * fsig() does not return a jobcontrol stopping signal
7557c478bd9Sstevel@tonic-gate 		 * with a default action of stopping the process if
7567c478bd9Sstevel@tonic-gate 		 * lwp_nostop is set, so we won't be causing a bogus
7577c478bd9Sstevel@tonic-gate 		 * EINTR by this action.  (Such a signal is eaten by
7587c478bd9Sstevel@tonic-gate 		 * isjobstop() when we loop around to do final checks.)
7597c478bd9Sstevel@tonic-gate 		 */
7607c478bd9Sstevel@tonic-gate 		if (lwp->lwp_nostop) {
7617c478bd9Sstevel@tonic-gate 			nostop_break = 1;
7627c478bd9Sstevel@tonic-gate 			break;
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		/*
7667c478bd9Sstevel@tonic-gate 		 * Promote the signal from pending to current.
7677c478bd9Sstevel@tonic-gate 		 *
7687c478bd9Sstevel@tonic-gate 		 * Note that sigdeq() will set lwp->lwp_curinfo to NULL
7697c478bd9Sstevel@tonic-gate 		 * if no siginfo_t exists for this signal.
7707c478bd9Sstevel@tonic-gate 		 */
7717c478bd9Sstevel@tonic-gate 		lwp->lwp_cursig = (uchar_t)sig;
7727c478bd9Sstevel@tonic-gate 		lwp->lwp_extsig = (uchar_t)ext;
7737c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;	/* so post_syscall will see signal */
7747c478bd9Sstevel@tonic-gate 		ASSERT(lwp->lwp_curinfo == NULL);
7757c478bd9Sstevel@tonic-gate 		sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		if (tracing(p, sig))
7787c478bd9Sstevel@tonic-gate 			stop(PR_SIGNALLED, sig);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		/*
7817c478bd9Sstevel@tonic-gate 		 * Loop around to check for requested stop before
7827c478bd9Sstevel@tonic-gate 		 * performing the usual current-signal actions.
7837c478bd9Sstevel@tonic-gate 		 */
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/*
7893f12bda8SRoger A. Faulkner 	 * If SIGCLD was dequeued from the process's signal queue,
7903f12bda8SRoger A. Faulkner 	 * search for other pending SIGCLD's from the list of children.
7917c478bd9Sstevel@tonic-gate 	 */
7923f12bda8SRoger A. Faulkner 	if (sigcld_found)
7937c478bd9Sstevel@tonic-gate 		sigcld_repost();
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (sig != 0)
7967c478bd9Sstevel@tonic-gate 		(void) undo_watch_step(NULL);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/*
7997c478bd9Sstevel@tonic-gate 	 * If we have been blocked since the p_lock was dropped off
8007c478bd9Sstevel@tonic-gate 	 * above, then this promoted signal might have been handled
8017c478bd9Sstevel@tonic-gate 	 * already when we were on the way back from sleep queue, so
8027c478bd9Sstevel@tonic-gate 	 * just ignore it.
8037c478bd9Sstevel@tonic-gate 	 * If we have been informed not to stop, just return the signal
8047c478bd9Sstevel@tonic-gate 	 * number. Also see comments above.
8057c478bd9Sstevel@tonic-gate 	 */
8067c478bd9Sstevel@tonic-gate 	if (!nostop_break) {
8077c478bd9Sstevel@tonic-gate 		sig = lwp->lwp_cursig;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	return (sig != 0);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * Return true if the process is currently stopped showing PR_JOBCONTROL.
8157c478bd9Sstevel@tonic-gate  * This is true only if all of the process's lwp's are so stopped.
8167c478bd9Sstevel@tonic-gate  * If this is asked by one of the lwps in the process, exclude that lwp.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate int
jobstopped(proc_t * p)8197c478bd9Sstevel@tonic-gate jobstopped(proc_t *p)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	kthread_t *t;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) == NULL)
8267c478bd9Sstevel@tonic-gate 		return (0);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	do {
8297c478bd9Sstevel@tonic-gate 		thread_lock(t);
8307c478bd9Sstevel@tonic-gate 		/* ignore current, zombie and suspended lwps in the test */
8317c478bd9Sstevel@tonic-gate 		if (!(t == curthread || t->t_state == TS_ZOMB ||
8327c478bd9Sstevel@tonic-gate 		    SUSPENDED(t)) &&
8337c478bd9Sstevel@tonic-gate 		    (t->t_state != TS_STOPPED ||
8347c478bd9Sstevel@tonic-gate 		    t->t_whystop != PR_JOBCONTROL)) {
8357c478bd9Sstevel@tonic-gate 			thread_unlock(t);
8367c478bd9Sstevel@tonic-gate 			return (0);
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 		thread_unlock(t);
8397c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	return (1);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Put ourself (curthread) into the stopped state and notify tracers.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate void
stop(int why,int what)8487c478bd9Sstevel@tonic-gate stop(int why, int what)
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;
8517c478bd9Sstevel@tonic-gate 	proc_t		*p = ttoproc(t);
8527c478bd9Sstevel@tonic-gate 	klwp_t		*lwp = ttolwp(t);
8537c478bd9Sstevel@tonic-gate 	kthread_t	*tx;
8547c478bd9Sstevel@tonic-gate 	lwpent_t	*lep;
8557c478bd9Sstevel@tonic-gate 	int		procstop;
8567c478bd9Sstevel@tonic-gate 	int		flags = TS_ALLSTART;
8577c478bd9Sstevel@tonic-gate 	hrtime_t	stoptime;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	/*
8607c478bd9Sstevel@tonic-gate 	 * Can't stop a system process.
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 	if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
8637c478bd9Sstevel@tonic-gate 		return;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
8687c478bd9Sstevel@tonic-gate 		/*
8697c478bd9Sstevel@tonic-gate 		 * Don't stop an lwp with SIGKILL pending.
8707c478bd9Sstevel@tonic-gate 		 * Don't stop if the process or lwp is exiting.
8717c478bd9Sstevel@tonic-gate 		 */
8727c478bd9Sstevel@tonic-gate 		if (lwp->lwp_cursig == SIGKILL ||
8737c478bd9Sstevel@tonic-gate 		    sigismember(&t->t_sig, SIGKILL) ||
8747c478bd9Sstevel@tonic-gate 		    sigismember(&p->p_sig, SIGKILL) ||
8757c478bd9Sstevel@tonic-gate 		    (t->t_proc_flag & TP_LWPEXIT) ||
8767c478bd9Sstevel@tonic-gate 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
8777c478bd9Sstevel@tonic-gate 			p->p_stopsig = 0;
8787c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
8797c478bd9Sstevel@tonic-gate 			return;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * Make sure we don't deadlock on a recursive call to prstop().
8858548bf79Snr 	 * prstop() sets the lwp_nostop flag.
8867c478bd9Sstevel@tonic-gate 	 */
8878548bf79Snr 	if (lwp->lwp_nostop)
8887c478bd9Sstevel@tonic-gate 		return;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Make sure the lwp is in an orderly state for inspection
8927c478bd9Sstevel@tonic-gate 	 * by a debugger through /proc or for dumping via core().
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
8957c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_STOPPING;	/* must set before dropping p_lock */
8967c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8977c478bd9Sstevel@tonic-gate 	stoptime = gethrtime();
8987c478bd9Sstevel@tonic-gate 	prstop(why, what);
8997c478bd9Sstevel@tonic-gate 	(void) undo_watch_step(NULL);
9007c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
9017c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_ONPROC);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	switch (why) {
9047c478bd9Sstevel@tonic-gate 	case PR_CHECKPOINT:
9057c478bd9Sstevel@tonic-gate 		/*
9067c478bd9Sstevel@tonic-gate 		 * The situation may have changed since we dropped
9077c478bd9Sstevel@tonic-gate 		 * and reacquired p->p_lock. Double-check now
9087c478bd9Sstevel@tonic-gate 		 * whether we should stop or not.
9097c478bd9Sstevel@tonic-gate 		 */
9107c478bd9Sstevel@tonic-gate 		if (!(t->t_proc_flag & TP_CHKPT)) {
9117c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~TP_STOPPING;
9127c478bd9Sstevel@tonic-gate 			return;
9137c478bd9Sstevel@tonic-gate 		}
9147c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_CHKPT;
9157c478bd9Sstevel@tonic-gate 		flags &= ~TS_RESUME;
9167c478bd9Sstevel@tonic-gate 		break;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	case PR_JOBCONTROL:
9197c478bd9Sstevel@tonic-gate 		ASSERT(what == SIGSTOP || what == SIGTSTP ||
920cbdce38dSbpramod 		    what == SIGTTIN || what == SIGTTOU);
9217c478bd9Sstevel@tonic-gate 		flags &= ~TS_XSTART;
9227c478bd9Sstevel@tonic-gate 		break;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	case PR_SUSPENDED:
9257c478bd9Sstevel@tonic-gate 		ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
9267c478bd9Sstevel@tonic-gate 		/*
9277c478bd9Sstevel@tonic-gate 		 * The situation may have changed since we dropped
9287c478bd9Sstevel@tonic-gate 		 * and reacquired p->p_lock.  Double-check now
9297c478bd9Sstevel@tonic-gate 		 * whether we should stop or not.
9307c478bd9Sstevel@tonic-gate 		 */
9317c478bd9Sstevel@tonic-gate 		if (what == SUSPEND_PAUSE) {
9327c478bd9Sstevel@tonic-gate 			if (!(t->t_proc_flag & TP_PAUSE)) {
9337c478bd9Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_STOPPING;
9347c478bd9Sstevel@tonic-gate 				return;
9357c478bd9Sstevel@tonic-gate 			}
9367c478bd9Sstevel@tonic-gate 			flags &= ~TS_UNPAUSE;
9377c478bd9Sstevel@tonic-gate 		} else {
9387c478bd9Sstevel@tonic-gate 			if (!((t->t_proc_flag & TP_HOLDLWP) ||
9397c478bd9Sstevel@tonic-gate 			    (p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
9407c478bd9Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_STOPPING;
9417c478bd9Sstevel@tonic-gate 				return;
9427c478bd9Sstevel@tonic-gate 			}
9437c478bd9Sstevel@tonic-gate 			/*
9447c478bd9Sstevel@tonic-gate 			 * If SHOLDFORK is in effect and we are stopping
9457c478bd9Sstevel@tonic-gate 			 * while asleep (not at the top of the stack),
9467c478bd9Sstevel@tonic-gate 			 * we return now to allow the hold to take effect
9477c478bd9Sstevel@tonic-gate 			 * when we reach the top of the kernel stack.
9487c478bd9Sstevel@tonic-gate 			 */
9497c478bd9Sstevel@tonic-gate 			if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
9507c478bd9Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_STOPPING;
9517c478bd9Sstevel@tonic-gate 				return;
9527c478bd9Sstevel@tonic-gate 			}
9537c478bd9Sstevel@tonic-gate 			flags &= ~TS_CSTART;
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 		break;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	default:	/* /proc stop */
9587c478bd9Sstevel@tonic-gate 		flags &= ~TS_PSTART;
9597c478bd9Sstevel@tonic-gate 		/*
9607c478bd9Sstevel@tonic-gate 		 * Do synchronous stop unless the async-stop flag is set.
9617c478bd9Sstevel@tonic-gate 		 * If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
9627c478bd9Sstevel@tonic-gate 		 * then no debugger is present and we also do synchronous stop.
9637c478bd9Sstevel@tonic-gate 		 */
9647c478bd9Sstevel@tonic-gate 		if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
9657c478bd9Sstevel@tonic-gate 		    !(p->p_proc_flag & P_PR_ASYNC)) {
9667c478bd9Sstevel@tonic-gate 			int notify;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 			for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
9697c478bd9Sstevel@tonic-gate 				notify = 0;
9707c478bd9Sstevel@tonic-gate 				thread_lock(tx);
9717c478bd9Sstevel@tonic-gate 				if (ISTOPPED(tx) ||
9727c478bd9Sstevel@tonic-gate 				    (tx->t_proc_flag & TP_PRSTOP)) {
9737c478bd9Sstevel@tonic-gate 					thread_unlock(tx);
9747c478bd9Sstevel@tonic-gate 					continue;
9757c478bd9Sstevel@tonic-gate 				}
9767c478bd9Sstevel@tonic-gate 				tx->t_proc_flag |= TP_PRSTOP;
9777c478bd9Sstevel@tonic-gate 				tx->t_sig_check = 1;
9787c478bd9Sstevel@tonic-gate 				if (tx->t_state == TS_SLEEP &&
9797c478bd9Sstevel@tonic-gate 				    (tx->t_flag & T_WAKEABLE)) {
9807c478bd9Sstevel@tonic-gate 					/*
9817c478bd9Sstevel@tonic-gate 					 * Don't actually wake it up if it's
9827c478bd9Sstevel@tonic-gate 					 * in one of the lwp_*() syscalls.
9837c478bd9Sstevel@tonic-gate 					 * Mark it virtually stopped and
9847c478bd9Sstevel@tonic-gate 					 * notify /proc waiters (below).
9857c478bd9Sstevel@tonic-gate 					 */
9867c478bd9Sstevel@tonic-gate 					if (tx->t_wchan0 == NULL)
9877c478bd9Sstevel@tonic-gate 						setrun_locked(tx);
9887c478bd9Sstevel@tonic-gate 					else {
9897c478bd9Sstevel@tonic-gate 						tx->t_proc_flag |= TP_PRVSTOP;
9907c478bd9Sstevel@tonic-gate 						tx->t_stoptime = stoptime;
9917c478bd9Sstevel@tonic-gate 						notify = 1;
9927c478bd9Sstevel@tonic-gate 					}
9937c478bd9Sstevel@tonic-gate 				}
994c97ad5cdSakolb 
995c97ad5cdSakolb 				/* Move waiting thread to run queue */
996c97ad5cdSakolb 				if (ISWAITING(tx))
997c97ad5cdSakolb 					setrun_locked(tx);
998c97ad5cdSakolb 
9997c478bd9Sstevel@tonic-gate 				/*
10007c478bd9Sstevel@tonic-gate 				 * force the thread into the kernel
10017c478bd9Sstevel@tonic-gate 				 * if it is not already there.
10027c478bd9Sstevel@tonic-gate 				 */
10037c478bd9Sstevel@tonic-gate 				if (tx->t_state == TS_ONPROC &&
10047c478bd9Sstevel@tonic-gate 				    tx->t_cpu != CPU)
10057c478bd9Sstevel@tonic-gate 					poke_cpu(tx->t_cpu->cpu_id);
10067c478bd9Sstevel@tonic-gate 				thread_unlock(tx);
10077c478bd9Sstevel@tonic-gate 				lep = p->p_lwpdir[tx->t_dslot].ld_entry;
10087c478bd9Sstevel@tonic-gate 				if (notify && lep->le_trace)
10097c478bd9Sstevel@tonic-gate 					prnotify(lep->le_trace);
10107c478bd9Sstevel@tonic-gate 			}
10117c478bd9Sstevel@tonic-gate 			/*
10127c478bd9Sstevel@tonic-gate 			 * We do this just in case one of the threads we asked
10137c478bd9Sstevel@tonic-gate 			 * to stop is in holdlwps() (called from cfork()) or
10147c478bd9Sstevel@tonic-gate 			 * lwp_suspend().
10157c478bd9Sstevel@tonic-gate 			 */
10167c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		break;
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	t->t_stoptime = stoptime;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	if (why == PR_JOBCONTROL || (why == PR_SUSPENDED && p->p_stopsig)) {
10247c478bd9Sstevel@tonic-gate 		/*
10257c478bd9Sstevel@tonic-gate 		 * Determine if the whole process is jobstopped.
10267c478bd9Sstevel@tonic-gate 		 */
10277c478bd9Sstevel@tonic-gate 		if (jobstopped(p)) {
10287c478bd9Sstevel@tonic-gate 			sigqueue_t *sqp;
10297c478bd9Sstevel@tonic-gate 			int sig;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 			if ((sig = p->p_stopsig) == 0)
10327c478bd9Sstevel@tonic-gate 				p->p_stopsig = (uchar_t)(sig = what);
10337c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
10347c478bd9Sstevel@tonic-gate 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
10357c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
10367c478bd9Sstevel@tonic-gate 			/*
10377c478bd9Sstevel@tonic-gate 			 * The last lwp to stop notifies the parent.
10387c478bd9Sstevel@tonic-gate 			 * Turn off the CLDCONT flag now so the first
10397c478bd9Sstevel@tonic-gate 			 * lwp to continue knows what to do.
10407c478bd9Sstevel@tonic-gate 			 */
10417c478bd9Sstevel@tonic-gate 			p->p_pidflag &= ~CLDCONT;
10427c478bd9Sstevel@tonic-gate 			p->p_wcode = CLD_STOPPED;
10437c478bd9Sstevel@tonic-gate 			p->p_wdata = sig;
10447c478bd9Sstevel@tonic-gate 			sigcld(p, sqp);
10457c478bd9Sstevel@tonic-gate 			/*
10467c478bd9Sstevel@tonic-gate 			 * Grab p->p_lock before releasing pidlock so the
10477c478bd9Sstevel@tonic-gate 			 * parent and the child don't have a race condition.
10487c478bd9Sstevel@tonic-gate 			 */
10497c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
10507c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
10517c478bd9Sstevel@tonic-gate 			p->p_stopsig = 0;
10527c478bd9Sstevel@tonic-gate 		} else if (why == PR_JOBCONTROL && p->p_stopsig == 0) {
10537c478bd9Sstevel@tonic-gate 			/*
10547c478bd9Sstevel@tonic-gate 			 * Set p->p_stopsig and wake up sleeping lwps
10557c478bd9Sstevel@tonic-gate 			 * so they will stop in sympathy with this lwp.
10567c478bd9Sstevel@tonic-gate 			 */
10577c478bd9Sstevel@tonic-gate 			p->p_stopsig = (uchar_t)what;
10587c478bd9Sstevel@tonic-gate 			pokelwps(p);
10597c478bd9Sstevel@tonic-gate 			/*
10607c478bd9Sstevel@tonic-gate 			 * We do this just in case one of the threads we asked
10617c478bd9Sstevel@tonic-gate 			 * to stop is in holdlwps() (called from cfork()) or
10627c478bd9Sstevel@tonic-gate 			 * lwp_suspend().
10637c478bd9Sstevel@tonic-gate 			 */
10647c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
10657c478bd9Sstevel@tonic-gate 		}
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (why != PR_JOBCONTROL && why != PR_CHECKPOINT) {
10697c478bd9Sstevel@tonic-gate 		/*
10707c478bd9Sstevel@tonic-gate 		 * Do process-level notification when all lwps are
10717c478bd9Sstevel@tonic-gate 		 * either stopped on events of interest to /proc
10727c478bd9Sstevel@tonic-gate 		 * or are stopped showing PR_SUSPENDED or are zombies.
10737c478bd9Sstevel@tonic-gate 		 */
10747c478bd9Sstevel@tonic-gate 		procstop = 1;
10757c478bd9Sstevel@tonic-gate 		for (tx = t->t_forw; procstop && tx != t; tx = tx->t_forw) {
10767c478bd9Sstevel@tonic-gate 			if (VSTOPPED(tx))
10777c478bd9Sstevel@tonic-gate 				continue;
10787c478bd9Sstevel@tonic-gate 			thread_lock(tx);
10797c478bd9Sstevel@tonic-gate 			switch (tx->t_state) {
10807c478bd9Sstevel@tonic-gate 			case TS_ZOMB:
10817c478bd9Sstevel@tonic-gate 				break;
10827c478bd9Sstevel@tonic-gate 			case TS_STOPPED:
10837c478bd9Sstevel@tonic-gate 				/* neither ISTOPPED nor SUSPENDED? */
10847c478bd9Sstevel@tonic-gate 				if ((tx->t_schedflag &
10857c478bd9Sstevel@tonic-gate 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART)) ==
10867c478bd9Sstevel@tonic-gate 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART))
10877c478bd9Sstevel@tonic-gate 					procstop = 0;
10887c478bd9Sstevel@tonic-gate 				break;
10897c478bd9Sstevel@tonic-gate 			case TS_SLEEP:
10907c478bd9Sstevel@tonic-gate 				/* not paused for watchpoints? */
10917c478bd9Sstevel@tonic-gate 				if (!(tx->t_flag & T_WAKEABLE) ||
10927c478bd9Sstevel@tonic-gate 				    tx->t_wchan0 == NULL ||
10937c478bd9Sstevel@tonic-gate 				    !(tx->t_proc_flag & TP_PAUSE))
10947c478bd9Sstevel@tonic-gate 					procstop = 0;
10957c478bd9Sstevel@tonic-gate 				break;
10967c478bd9Sstevel@tonic-gate 			default:
10977c478bd9Sstevel@tonic-gate 				procstop = 0;
10987c478bd9Sstevel@tonic-gate 				break;
10997c478bd9Sstevel@tonic-gate 			}
11007c478bd9Sstevel@tonic-gate 			thread_unlock(tx);
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 		if (procstop) {
11037c478bd9Sstevel@tonic-gate 			/* there must not be any remapped watched pages now */
11047c478bd9Sstevel@tonic-gate 			ASSERT(p->p_mapcnt == 0);
11057c478bd9Sstevel@tonic-gate 			if (p->p_proc_flag & P_PR_PTRACE) {
11067c478bd9Sstevel@tonic-gate 				/* ptrace() compatibility */
11077c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
11087c478bd9Sstevel@tonic-gate 				mutex_enter(&pidlock);
11097c478bd9Sstevel@tonic-gate 				p->p_wcode = CLD_TRAPPED;
11107c478bd9Sstevel@tonic-gate 				p->p_wdata = (why == PR_SIGNALLED)?
11117c478bd9Sstevel@tonic-gate 				    what : SIGTRAP;
11127c478bd9Sstevel@tonic-gate 				cv_broadcast(&p->p_parent->p_cv);
11137c478bd9Sstevel@tonic-gate 				/*
11147c478bd9Sstevel@tonic-gate 				 * Grab p->p_lock before releasing pidlock so
11157c478bd9Sstevel@tonic-gate 				 * parent and child don't have a race condition.
11167c478bd9Sstevel@tonic-gate 				 */
11177c478bd9Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
11187c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
11197c478bd9Sstevel@tonic-gate 			}
11207c478bd9Sstevel@tonic-gate 			if (p->p_trace)			/* /proc */
11217c478bd9Sstevel@tonic-gate 				prnotify(p->p_trace);
11227c478bd9Sstevel@tonic-gate 			cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */
11237c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);	/* holdwatch() */
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 		if (why != PR_SUSPENDED) {
11267c478bd9Sstevel@tonic-gate 			lep = p->p_lwpdir[t->t_dslot].ld_entry;
11277c478bd9Sstevel@tonic-gate 			if (lep->le_trace)		/* /proc */
11287c478bd9Sstevel@tonic-gate 				prnotify(lep->le_trace);
11297c478bd9Sstevel@tonic-gate 			/*
11307c478bd9Sstevel@tonic-gate 			 * Special notification for creation of the agent lwp.
11317c478bd9Sstevel@tonic-gate 			 */
11327c478bd9Sstevel@tonic-gate 			if (t == p->p_agenttp &&
11337c478bd9Sstevel@tonic-gate 			    (t->t_proc_flag & TP_PRSTOP) &&
11347c478bd9Sstevel@tonic-gate 			    p->p_trace)
11357c478bd9Sstevel@tonic-gate 				prnotify(p->p_trace);
11367c478bd9Sstevel@tonic-gate 			/*
11377c478bd9Sstevel@tonic-gate 			 * The situation may have changed since we dropped
11387c478bd9Sstevel@tonic-gate 			 * and reacquired p->p_lock. Double-check now
11397c478bd9Sstevel@tonic-gate 			 * whether we should stop or not.
11407c478bd9Sstevel@tonic-gate 			 */
11417c478bd9Sstevel@tonic-gate 			if (!(t->t_proc_flag & TP_STOPPING)) {
11427c478bd9Sstevel@tonic-gate 				if (t->t_proc_flag & TP_PRSTOP)
11437c478bd9Sstevel@tonic-gate 					t->t_proc_flag |= TP_STOPPING;
11447c478bd9Sstevel@tonic-gate 			}
11457c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
11467c478bd9Sstevel@tonic-gate 			prnostep(lwp);
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	if (why == PR_SUSPENDED) {
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 		/*
11537c478bd9Sstevel@tonic-gate 		 * We always broadcast in the case of SUSPEND_PAUSE.  This is
11547c478bd9Sstevel@tonic-gate 		 * because checks for TP_PAUSE take precedence over checks for
11557c478bd9Sstevel@tonic-gate 		 * SHOLDWATCH.  If a thread is trying to stop because of
11567c478bd9Sstevel@tonic-gate 		 * SUSPEND_PAUSE and tries to do a holdwatch(), it will be
11577c478bd9Sstevel@tonic-gate 		 * waiting for the rest of the threads to enter a stopped state.
11587c478bd9Sstevel@tonic-gate 		 * If we are stopping for a SUSPEND_PAUSE, we may be the last
11597c478bd9Sstevel@tonic-gate 		 * lwp and not know it, so broadcast just in case.
11607c478bd9Sstevel@tonic-gate 		 */
11617c478bd9Sstevel@tonic-gate 		if (what == SUSPEND_PAUSE ||
11627c478bd9Sstevel@tonic-gate 		    --p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP))
11637c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * Need to do this here (rather than after the thread is officially
11697c478bd9Sstevel@tonic-gate 	 * stopped) because we can't call mutex_enter from a stopped thread.
11707c478bd9Sstevel@tonic-gate 	 */
11717c478bd9Sstevel@tonic-gate 	if (why == PR_CHECKPOINT)
11727c478bd9Sstevel@tonic-gate 		del_one_utstop();
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	thread_lock(t);
11757c478bd9Sstevel@tonic-gate 	ASSERT((t->t_schedflag & TS_ALLSTART) == 0);
11767c478bd9Sstevel@tonic-gate 	t->t_schedflag |= flags;
11777c478bd9Sstevel@tonic-gate 	t->t_whystop = (short)why;
11787c478bd9Sstevel@tonic-gate 	t->t_whatstop = (short)what;
11797c478bd9Sstevel@tonic-gate 	CL_STOP(t, why, what);
11807c478bd9Sstevel@tonic-gate 	(void) new_mstate(t, LMS_STOPPED);
11817c478bd9Sstevel@tonic-gate 	thread_stop(t);			/* set stop state and drop lock */
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
11847c478bd9Sstevel@tonic-gate 		/*
11857c478bd9Sstevel@tonic-gate 		 * We may have gotten a SIGKILL or a SIGCONT when
11867c478bd9Sstevel@tonic-gate 		 * we released p->p_lock; make one last check.
11877c478bd9Sstevel@tonic-gate 		 * Also check for a /proc run-on-last-close.
11887c478bd9Sstevel@tonic-gate 		 */
11897c478bd9Sstevel@tonic-gate 		if (sigismember(&t->t_sig, SIGKILL) ||
11907c478bd9Sstevel@tonic-gate 		    sigismember(&p->p_sig, SIGKILL) ||
11917c478bd9Sstevel@tonic-gate 		    (t->t_proc_flag & TP_LWPEXIT) ||
11927c478bd9Sstevel@tonic-gate 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
11937c478bd9Sstevel@tonic-gate 			p->p_stopsig = 0;
11947c478bd9Sstevel@tonic-gate 			thread_lock(t);
11957c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_XSTART | TS_PSTART;
11967c478bd9Sstevel@tonic-gate 			setrun_locked(t);
11977c478bd9Sstevel@tonic-gate 			thread_unlock_nopreempt(t);
11987c478bd9Sstevel@tonic-gate 		} else if (why == PR_JOBCONTROL) {
11997c478bd9Sstevel@tonic-gate 			if (p->p_flag & SSCONT) {
12007c478bd9Sstevel@tonic-gate 				/*
12017c478bd9Sstevel@tonic-gate 				 * This resulted from a SIGCONT posted
12027c478bd9Sstevel@tonic-gate 				 * while we were not holding p->p_lock.
12037c478bd9Sstevel@tonic-gate 				 */
12047c478bd9Sstevel@tonic-gate 				p->p_stopsig = 0;
12057c478bd9Sstevel@tonic-gate 				thread_lock(t);
12067c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_XSTART;
12077c478bd9Sstevel@tonic-gate 				setrun_locked(t);
12087c478bd9Sstevel@tonic-gate 				thread_unlock_nopreempt(t);
12097c478bd9Sstevel@tonic-gate 			}
12107c478bd9Sstevel@tonic-gate 		} else if (!(t->t_proc_flag & TP_STOPPING)) {
12117c478bd9Sstevel@tonic-gate 			/*
12127c478bd9Sstevel@tonic-gate 			 * This resulted from a /proc run-on-last-close.
12137c478bd9Sstevel@tonic-gate 			 */
12147c478bd9Sstevel@tonic-gate 			thread_lock(t);
12157c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_PSTART;
12167c478bd9Sstevel@tonic-gate 			setrun_locked(t);
12177c478bd9Sstevel@tonic-gate 			thread_unlock_nopreempt(t);
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_STOPPING;
12227c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	swtch();
12257c478bd9Sstevel@tonic-gate 	setallwatch();	/* reestablish any watchpoints set while stopped */
12267c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
12277c478bd9Sstevel@tonic-gate 	prbarrier(p);	/* barrier against /proc locking */
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate /* Interface for resetting user thread stop count. */
12317c478bd9Sstevel@tonic-gate void
utstop_init(void)12327c478bd9Sstevel@tonic-gate utstop_init(void)
12337c478bd9Sstevel@tonic-gate {
12347c478bd9Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12357c478bd9Sstevel@tonic-gate 	num_utstop = 0;
12367c478bd9Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /* Interface for registering a user thread stop request. */
12407c478bd9Sstevel@tonic-gate void
add_one_utstop(void)12417c478bd9Sstevel@tonic-gate add_one_utstop(void)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12447c478bd9Sstevel@tonic-gate 	num_utstop++;
12457c478bd9Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate /* Interface for cancelling a user thread stop request */
12497c478bd9Sstevel@tonic-gate void
del_one_utstop(void)12507c478bd9Sstevel@tonic-gate del_one_utstop(void)
12517c478bd9Sstevel@tonic-gate {
12527c478bd9Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12537c478bd9Sstevel@tonic-gate 	num_utstop--;
12547c478bd9Sstevel@tonic-gate 	if (num_utstop == 0)
12557c478bd9Sstevel@tonic-gate 		cv_broadcast(&utstop_cv);
12567c478bd9Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate /* Interface to wait for all user threads to be stopped */
12607c478bd9Sstevel@tonic-gate void
utstop_timedwait(clock_t ticks)12617c478bd9Sstevel@tonic-gate utstop_timedwait(clock_t ticks)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	mutex_enter(&thread_stop_lock);
12647c478bd9Sstevel@tonic-gate 	if (num_utstop > 0)
1265d3d50737SRafael Vanoni 		(void) cv_reltimedwait(&utstop_cv, &thread_stop_lock, ticks,
1266d3d50737SRafael Vanoni 		    TR_CLOCK_TICK);
12677c478bd9Sstevel@tonic-gate 	mutex_exit(&thread_stop_lock);
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate /*
12717c478bd9Sstevel@tonic-gate  * Perform the action specified by the current signal.
12727c478bd9Sstevel@tonic-gate  * The usual sequence is:
12737c478bd9Sstevel@tonic-gate  * 	if (issig())
12747c478bd9Sstevel@tonic-gate  * 		psig();
12757c478bd9Sstevel@tonic-gate  * The signal bit has already been cleared by issig(),
12767c478bd9Sstevel@tonic-gate  * the current signal number has been stored in lwp_cursig,
12777c478bd9Sstevel@tonic-gate  * and the current siginfo is now referenced by lwp_curinfo.
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate void
psig(void)12807c478bd9Sstevel@tonic-gate psig(void)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
12837c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
12847c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
12857c478bd9Sstevel@tonic-gate 	void (*func)();
12867c478bd9Sstevel@tonic-gate 	int sig, rc, code, ext;
12877c478bd9Sstevel@tonic-gate 	pid_t pid = -1;
12887c478bd9Sstevel@tonic-gate 	id_t ctid = 0;
12897c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = -1;
12907c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = NULL;
1291005d3febSMarek Pospisil 	uint32_t auditing = AU_AUDITING();
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
12947c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
12957c478bd9Sstevel@tonic-gate 	code = CLD_KILLED;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS) {
12987c478bd9Sstevel@tonic-gate 		lwp_exit();
12997c478bd9Sstevel@tonic-gate 		return;			/* not reached */
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 	sig = lwp->lwp_cursig;
13027c478bd9Sstevel@tonic-gate 	ext = lwp->lwp_extsig;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	ASSERT(sig < NSIG);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/*
13077c478bd9Sstevel@tonic-gate 	 * Re-check lwp_cursig after we acquire p_lock.  Since p_lock was
13087c478bd9Sstevel@tonic-gate 	 * dropped between issig() and psig(), a debugger may have cleared
13097c478bd9Sstevel@tonic-gate 	 * lwp_cursig via /proc in the intervening window.
13107c478bd9Sstevel@tonic-gate 	 */
13117c478bd9Sstevel@tonic-gate 	if (sig == 0) {
13127c478bd9Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
13137c478bd9Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
13147c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
13157c478bd9Sstevel@tonic-gate 		}
13167c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
13177c478bd9Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
13187c478bd9Sstevel@tonic-gate 			t->t_hold = lwp->lwp_sigoldmask;
13197c478bd9Sstevel@tonic-gate 		}
13207c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13217c478bd9Sstevel@tonic-gate 		return;
13227c478bd9Sstevel@tonic-gate 	}
1323ae115bc7Smrj 	func = PTOU(curproc)->u_signal[sig-1];
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	/*
13267c478bd9Sstevel@tonic-gate 	 * The signal disposition could have changed since we promoted
13277c478bd9Sstevel@tonic-gate 	 * this signal from pending to current (we dropped p->p_lock).
13287c478bd9Sstevel@tonic-gate 	 * This can happen only in a multi-threaded process.
13297c478bd9Sstevel@tonic-gate 	 */
13307c478bd9Sstevel@tonic-gate 	if (sigismember(&p->p_ignore, sig) ||
13317c478bd9Sstevel@tonic-gate 	    (func == SIG_DFL && sigismember(&stopdefault, sig))) {
13327c478bd9Sstevel@tonic-gate 		lwp->lwp_cursig = 0;
13337c478bd9Sstevel@tonic-gate 		lwp->lwp_extsig = 0;
13347c478bd9Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
13357c478bd9Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
13367c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
13397c478bd9Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
13407c478bd9Sstevel@tonic-gate 			t->t_hold = lwp->lwp_sigoldmask;
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13437c478bd9Sstevel@tonic-gate 		return;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	/*
13477c478bd9Sstevel@tonic-gate 	 * We check lwp_curinfo first since pr_setsig can actually
13487c478bd9Sstevel@tonic-gate 	 * stuff a sigqueue_t there for SIGKILL.
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
13517c478bd9Sstevel@tonic-gate 		sqp = lwp->lwp_curinfo;
13527c478bd9Sstevel@tonic-gate 	} else if (sig == SIGKILL && p->p_killsqp) {
13537c478bd9Sstevel@tonic-gate 		sqp = p->p_killsqp;
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (sqp != NULL) {
13577c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(&sqp->sq_info)) {
13587c478bd9Sstevel@tonic-gate 			pid = sqp->sq_info.si_pid;
13597c478bd9Sstevel@tonic-gate 			ctid = sqp->sq_info.si_ctid;
13607c478bd9Sstevel@tonic-gate 			zoneid = sqp->sq_info.si_zoneid;
13617c478bd9Sstevel@tonic-gate 		}
13627c478bd9Sstevel@tonic-gate 		/*
13637c478bd9Sstevel@tonic-gate 		 * If we have a sigqueue_t, its sq_external value
13647c478bd9Sstevel@tonic-gate 		 * trumps the lwp_extsig value.  It is theoretically
13657c478bd9Sstevel@tonic-gate 		 * possible to make lwp_extsig reflect reality, but it
13667c478bd9Sstevel@tonic-gate 		 * would unnecessarily complicate things elsewhere.
13677c478bd9Sstevel@tonic-gate 		 */
13687c478bd9Sstevel@tonic-gate 		ext = sqp->sq_external;
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	if (func == SIG_DFL) {
13727c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13737c478bd9Sstevel@tonic-gate 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
13747c478bd9Sstevel@tonic-gate 		    NULL, void (*)(void), func);
13757c478bd9Sstevel@tonic-gate 	} else {
13767c478bd9Sstevel@tonic-gate 		k_siginfo_t *sip = NULL;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 		/*
13797c478bd9Sstevel@tonic-gate 		 * If DTrace user-land tracing is active, give DTrace a
13807c478bd9Sstevel@tonic-gate 		 * chance to defer the signal until after tracing is
13817c478bd9Sstevel@tonic-gate 		 * complete.
13827c478bd9Sstevel@tonic-gate 		 */
13837c478bd9Sstevel@tonic-gate 		if (t->t_dtrace_on && dtrace_safe_defer_signal()) {
13847c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
13857c478bd9Sstevel@tonic-gate 			return;
13867c478bd9Sstevel@tonic-gate 		}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 		/*
13897c478bd9Sstevel@tonic-gate 		 * save siginfo pointer here, in case the
13907c478bd9Sstevel@tonic-gate 		 * the signal's reset bit is on
13917c478bd9Sstevel@tonic-gate 		 *
13927c478bd9Sstevel@tonic-gate 		 * The presence of a current signal prevents paging
13937c478bd9Sstevel@tonic-gate 		 * from succeeding over a network.  We copy the current
13947c478bd9Sstevel@tonic-gate 		 * signal information to the side and cancel the current
13957c478bd9Sstevel@tonic-gate 		 * signal so that sendsig() will succeed.
13967c478bd9Sstevel@tonic-gate 		 */
13977c478bd9Sstevel@tonic-gate 		if (sigismember(&p->p_siginfo, sig)) {
1398ee01d137Sraf 			sip = &lwp->lwp_siginfo;
13997c478bd9Sstevel@tonic-gate 			if (sqp) {
1400ee01d137Sraf 				bcopy(&sqp->sq_info, sip, sizeof (*sip));
1401a574db85Sraf 				/*
1402a574db85Sraf 				 * If we were interrupted out of a system call
1403a574db85Sraf 				 * due to pthread_cancel(), inform libc.
1404a574db85Sraf 				 */
1405a574db85Sraf 				if (sig == SIGCANCEL &&
1406a574db85Sraf 				    sip->si_code == SI_LWP &&
1407a574db85Sraf 				    t->t_sysnum != 0)
1408a574db85Sraf 					schedctl_cancel_eintr();
1409ee01d137Sraf 			} else if (sig == SIGPROF && sip->si_signo == SIGPROF &&
1410ee01d137Sraf 			    t->t_rprof != NULL && t->t_rprof->rp_anystate) {
1411ee01d137Sraf 				/* EMPTY */;
1412ee01d137Sraf 			} else {
1413ee01d137Sraf 				bzero(sip, sizeof (*sip));
1414ee01d137Sraf 				sip->si_signo = sig;
1415ee01d137Sraf 				sip->si_code = SI_NOINFO;
14167c478bd9Sstevel@tonic-gate 			}
14177c478bd9Sstevel@tonic-gate 		}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 		if (t->t_flag & T_TOMASK)
14207c478bd9Sstevel@tonic-gate 			t->t_flag &= ~T_TOMASK;
14217c478bd9Sstevel@tonic-gate 		else
14227c478bd9Sstevel@tonic-gate 			lwp->lwp_sigoldmask = t->t_hold;
1423ae115bc7Smrj 		sigorset(&t->t_hold, &PTOU(curproc)->u_sigmask[sig-1]);
1424ae115bc7Smrj 		if (!sigismember(&PTOU(curproc)->u_signodefer, sig))
14257c478bd9Sstevel@tonic-gate 			sigaddset(&t->t_hold, sig);
1426ae115bc7Smrj 		if (sigismember(&PTOU(curproc)->u_sigresethand, sig))
1427bdf0047cSRoger A. Faulkner 			setsigact(sig, SIG_DFL, &nullsmask, 0);
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
14307c478bd9Sstevel@tonic-gate 		    sip, void (*)(void), func);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 		lwp->lwp_cursig = 0;
14337c478bd9Sstevel@tonic-gate 		lwp->lwp_extsig = 0;
14347c478bd9Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
14357c478bd9Sstevel@tonic-gate 			/* p->p_killsqp is freed by freeproc */
14367c478bd9Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
14377c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
14387c478bd9Sstevel@tonic-gate 		}
14397c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14407c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.nsignals++;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		if (p->p_model == DATAMODEL_NATIVE)
14437c478bd9Sstevel@tonic-gate 			rc = sendsig(sig, sip, func);
14447c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
14457c478bd9Sstevel@tonic-gate 		else
14467c478bd9Sstevel@tonic-gate 			rc = sendsig32(sig, sip, func);
14477c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
14487c478bd9Sstevel@tonic-gate 		if (rc)
14497c478bd9Sstevel@tonic-gate 			return;
14507c478bd9Sstevel@tonic-gate 		sig = lwp->lwp_cursig = SIGSEGV;
14517c478bd9Sstevel@tonic-gate 		ext = 0;	/* lwp_extsig was set above */
14527c478bd9Sstevel@tonic-gate 		pid = -1;
14537c478bd9Sstevel@tonic-gate 		ctid = 0;
14547c478bd9Sstevel@tonic-gate 	}
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	if (sigismember(&coredefault, sig)) {
14577c478bd9Sstevel@tonic-gate 		/*
14587c478bd9Sstevel@tonic-gate 		 * Terminate all LWPs but don't discard them.
14597c478bd9Sstevel@tonic-gate 		 * If another lwp beat us to the punch by calling exit(),
14607c478bd9Sstevel@tonic-gate 		 * evaporate now.
14617c478bd9Sstevel@tonic-gate 		 */
146297eda132Sraf 		proc_is_exiting(p);
14637c478bd9Sstevel@tonic-gate 		if (exitlwps(1) != 0) {
14647c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
14657c478bd9Sstevel@tonic-gate 			lwp_exit();
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 		/* if we got a SIGKILL from anywhere, no core dump */
14687c478bd9Sstevel@tonic-gate 		if (p->p_flag & SKILLED) {
14697c478bd9Sstevel@tonic-gate 			sig = SIGKILL;
14707c478bd9Sstevel@tonic-gate 			ext = (p->p_flag & SEXTKILLED) != 0;
14717c478bd9Sstevel@tonic-gate 		} else {
1472005d3febSMarek Pospisil 			if (auditing)		/* audit core dump */
14737c478bd9Sstevel@tonic-gate 				audit_core_start(sig);
14747c478bd9Sstevel@tonic-gate 			if (core(sig, ext) == 0)
14757c478bd9Sstevel@tonic-gate 				code = CLD_DUMPED;
1476005d3febSMarek Pospisil 			if (auditing)		/* audit core dump */
14777c478bd9Sstevel@tonic-gate 				audit_core_finish(code);
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 	}
1480cbdce38dSbpramod 
1481cbdce38dSbpramod 	/*
1482cbdce38dSbpramod 	 * Generate a contract event once if the process is killed
1483cbdce38dSbpramod 	 * by a signal.
1484cbdce38dSbpramod 	 */
1485cbdce38dSbpramod 	if (ext) {
1486cbdce38dSbpramod 		proc_is_exiting(p);
1487cbdce38dSbpramod 		if (exitlwps(0) != 0) {
1488cbdce38dSbpramod 			mutex_enter(&p->p_lock);
1489cbdce38dSbpramod 			lwp_exit();
1490cbdce38dSbpramod 		}
14917c478bd9Sstevel@tonic-gate 		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
14927c478bd9Sstevel@tonic-gate 		    zoneid);
1493cbdce38dSbpramod 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	exit(code, sig);
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate /*
14997c478bd9Sstevel@tonic-gate  * Find next unheld signal in ssp for thread t.
15007c478bd9Sstevel@tonic-gate  */
15017c478bd9Sstevel@tonic-gate int
fsig(k_sigset_t * ssp,kthread_t * t)15027c478bd9Sstevel@tonic-gate fsig(k_sigset_t *ssp, kthread_t *t)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
15057c478bd9Sstevel@tonic-gate 	user_t *up = PTOU(p);
15067c478bd9Sstevel@tonic-gate 	int i;
15077c478bd9Sstevel@tonic-gate 	k_sigset_t temp;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/*
15127c478bd9Sstevel@tonic-gate 	 * Don't promote any signals for the parent of a vfork()d
15137c478bd9Sstevel@tonic-gate 	 * child that hasn't yet released the parent's memory.
15147c478bd9Sstevel@tonic-gate 	 */
15157c478bd9Sstevel@tonic-gate 	if (p->p_flag & SVFWAIT)
15167c478bd9Sstevel@tonic-gate 		return (0);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	temp = *ssp;
15197c478bd9Sstevel@tonic-gate 	sigdiffset(&temp, &t->t_hold);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	/*
15227c478bd9Sstevel@tonic-gate 	 * Don't promote stopping signals (except SIGSTOP) for a child
15237c478bd9Sstevel@tonic-gate 	 * of vfork() that hasn't yet released the parent's memory.
15247c478bd9Sstevel@tonic-gate 	 */
15257c478bd9Sstevel@tonic-gate 	if (p->p_flag & SVFORK)
15267c478bd9Sstevel@tonic-gate 		sigdiffset(&temp, &holdvfork);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	/*
15297c478bd9Sstevel@tonic-gate 	 * Don't promote a signal that will stop
15307c478bd9Sstevel@tonic-gate 	 * the process when lwp_nostop is set.
15317c478bd9Sstevel@tonic-gate 	 */
15327c478bd9Sstevel@tonic-gate 	if (ttolwp(t)->lwp_nostop) {
15337c478bd9Sstevel@tonic-gate 		sigdelset(&temp, SIGSTOP);
15347c478bd9Sstevel@tonic-gate 		if (!p->p_pgidp->pid_pgorphaned) {
15357c478bd9Sstevel@tonic-gate 			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
15367c478bd9Sstevel@tonic-gate 				sigdelset(&temp, SIGTSTP);
15377c478bd9Sstevel@tonic-gate 			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
15387c478bd9Sstevel@tonic-gate 				sigdelset(&temp, SIGTTIN);
15397c478bd9Sstevel@tonic-gate 			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
15407c478bd9Sstevel@tonic-gate 				sigdelset(&temp, SIGTTOU);
15417c478bd9Sstevel@tonic-gate 		}
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	/*
15457c478bd9Sstevel@tonic-gate 	 * Choose SIGKILL and SIGPROF before all other pending signals.
15467c478bd9Sstevel@tonic-gate 	 * The rest are promoted in signal number order.
15477c478bd9Sstevel@tonic-gate 	 */
15487c478bd9Sstevel@tonic-gate 	if (sigismember(&temp, SIGKILL))
15497c478bd9Sstevel@tonic-gate 		return (SIGKILL);
15507c478bd9Sstevel@tonic-gate 	if (sigismember(&temp, SIGPROF))
15517c478bd9Sstevel@tonic-gate 		return (SIGPROF);
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
15547c478bd9Sstevel@tonic-gate 		if (temp.__sigbits[i])
15557c478bd9Sstevel@tonic-gate 			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
15567c478bd9Sstevel@tonic-gate 			    lowbit(temp.__sigbits[i]));
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	return (0);
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate void
setsigact(int sig,void (* disp)(),const k_sigset_t * mask,int flags)1563bdf0047cSRoger A. Faulkner setsigact(int sig, void (*disp)(), const k_sigset_t *mask, int flags)
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
15667c478bd9Sstevel@tonic-gate 	kthread_t *t;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
15697c478bd9Sstevel@tonic-gate 
1570ae115bc7Smrj 	PTOU(curproc)->u_signal[sig - 1] = disp;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	/*
15737c478bd9Sstevel@tonic-gate 	 * Honor the SA_SIGINFO flag if the signal is being caught.
15747c478bd9Sstevel@tonic-gate 	 * Force the SA_SIGINFO flag if the signal is not being caught.
15757c478bd9Sstevel@tonic-gate 	 * This is necessary to make sigqueue() and sigwaitinfo() work
15767c478bd9Sstevel@tonic-gate 	 * properly together when the signal is set to default or is
15777c478bd9Sstevel@tonic-gate 	 * being temporarily ignored.
15787c478bd9Sstevel@tonic-gate 	 */
15797c478bd9Sstevel@tonic-gate 	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
15807c478bd9Sstevel@tonic-gate 		sigaddset(&p->p_siginfo, sig);
15817c478bd9Sstevel@tonic-gate 	else
15827c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_siginfo, sig);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (disp != SIG_DFL && disp != SIG_IGN) {
15857c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_ignore, sig);
1586bdf0047cSRoger A. Faulkner 		PTOU(curproc)->u_sigmask[sig - 1] = *mask;
15877c478bd9Sstevel@tonic-gate 		if (!sigismember(&cantreset, sig)) {
15887c478bd9Sstevel@tonic-gate 			if (flags & SA_RESETHAND)
1589ae115bc7Smrj 				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
15907c478bd9Sstevel@tonic-gate 			else
1591ae115bc7Smrj 				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
15927c478bd9Sstevel@tonic-gate 		}
15937c478bd9Sstevel@tonic-gate 		if (flags & SA_NODEFER)
1594ae115bc7Smrj 			sigaddset(&PTOU(curproc)->u_signodefer, sig);
15957c478bd9Sstevel@tonic-gate 		else
1596ae115bc7Smrj 			sigdelset(&PTOU(curproc)->u_signodefer, sig);
15977c478bd9Sstevel@tonic-gate 		if (flags & SA_RESTART)
1598ae115bc7Smrj 			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
15997c478bd9Sstevel@tonic-gate 		else
1600ae115bc7Smrj 			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
16017c478bd9Sstevel@tonic-gate 		if (flags & SA_ONSTACK)
1602ae115bc7Smrj 			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
16037c478bd9Sstevel@tonic-gate 		else
1604ae115bc7Smrj 			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
16057c478bd9Sstevel@tonic-gate 	} else if (disp == SIG_IGN ||
16067c478bd9Sstevel@tonic-gate 	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
16077c478bd9Sstevel@tonic-gate 		/*
16087c478bd9Sstevel@tonic-gate 		 * Setting the signal action to SIG_IGN results in the
16097c478bd9Sstevel@tonic-gate 		 * discarding of all pending signals of that signal number.
16107c478bd9Sstevel@tonic-gate 		 * Setting the signal action to SIG_DFL does the same *only*
16117c478bd9Sstevel@tonic-gate 		 * if the signal's default behavior is to be ignored.
16127c478bd9Sstevel@tonic-gate 		 */
16137c478bd9Sstevel@tonic-gate 		sigaddset(&p->p_ignore, sig);
16147c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_sig, sig);
16157c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_extsig, sig);
16167c478bd9Sstevel@tonic-gate 		sigdelq(p, NULL, sig);
16177c478bd9Sstevel@tonic-gate 		t = p->p_tlist;
16187c478bd9Sstevel@tonic-gate 		do {
16197c478bd9Sstevel@tonic-gate 			sigdelset(&t->t_sig, sig);
16207c478bd9Sstevel@tonic-gate 			sigdelset(&t->t_extsig, sig);
16217c478bd9Sstevel@tonic-gate 			sigdelq(p, t, sig);
16227c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
16237c478bd9Sstevel@tonic-gate 	} else {
16247c478bd9Sstevel@tonic-gate 		/*
16257c478bd9Sstevel@tonic-gate 		 * The signal action is being set to SIG_DFL and the default
16267c478bd9Sstevel@tonic-gate 		 * behavior is to do something: make sure it is not ignored.
16277c478bd9Sstevel@tonic-gate 		 */
16287c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_ignore, sig);
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	if (sig == SIGCLD) {
16327c478bd9Sstevel@tonic-gate 		if (flags & SA_NOCLDWAIT)
16337c478bd9Sstevel@tonic-gate 			p->p_flag |= SNOWAIT;
16347c478bd9Sstevel@tonic-gate 		else
16357c478bd9Sstevel@tonic-gate 			p->p_flag &= ~SNOWAIT;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 		if (flags & SA_NOCLDSTOP)
16387c478bd9Sstevel@tonic-gate 			p->p_flag &= ~SJCTL;
16397c478bd9Sstevel@tonic-gate 		else
16407c478bd9Sstevel@tonic-gate 			p->p_flag |= SJCTL;
16417c478bd9Sstevel@tonic-gate 
1642657b1f3dSraf 		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
16437c478bd9Sstevel@tonic-gate 			proc_t *cp, *tp;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
16467c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
16477c478bd9Sstevel@tonic-gate 			for (cp = p->p_child; cp != NULL; cp = tp) {
16487c478bd9Sstevel@tonic-gate 				tp = cp->p_sibling;
1649657b1f3dSraf 				if (cp->p_stat == SZOMB &&
1650657b1f3dSraf 				    !(cp->p_pidflag & CLDWAITPID))
16517c478bd9Sstevel@tonic-gate 					freeproc(cp);
16527c478bd9Sstevel@tonic-gate 			}
16537c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
16547c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
16557c478bd9Sstevel@tonic-gate 		}
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate /*
16607c478bd9Sstevel@tonic-gate  * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
16617c478bd9Sstevel@tonic-gate  * Called from exec_common() for a process undergoing execve()
16627c478bd9Sstevel@tonic-gate  * and from cfork() for a newly-created child of vfork().
16637c478bd9Sstevel@tonic-gate  * In the vfork() case, 'p' is not the current process.
16647c478bd9Sstevel@tonic-gate  * In both cases, there is only one thread in the process.
16657c478bd9Sstevel@tonic-gate  */
16667c478bd9Sstevel@tonic-gate void
sigdefault(proc_t * p)16677c478bd9Sstevel@tonic-gate sigdefault(proc_t *p)
16687c478bd9Sstevel@tonic-gate {
16697c478bd9Sstevel@tonic-gate 	kthread_t *t = p->p_tlist;
16707c478bd9Sstevel@tonic-gate 	struct user *up = PTOU(p);
16717c478bd9Sstevel@tonic-gate 	int sig;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	for (sig = 1; sig < NSIG; sig++) {
16767c478bd9Sstevel@tonic-gate 		if (up->u_signal[sig - 1] != SIG_DFL &&
16777c478bd9Sstevel@tonic-gate 		    up->u_signal[sig - 1] != SIG_IGN) {
16787c478bd9Sstevel@tonic-gate 			up->u_signal[sig - 1] = SIG_DFL;
16797c478bd9Sstevel@tonic-gate 			sigemptyset(&up->u_sigmask[sig - 1]);
16807c478bd9Sstevel@tonic-gate 			if (sigismember(&ignoredefault, sig)) {
16817c478bd9Sstevel@tonic-gate 				sigdelq(p, NULL, sig);
16827c478bd9Sstevel@tonic-gate 				sigdelq(p, t, sig);
16837c478bd9Sstevel@tonic-gate 			}
16847c478bd9Sstevel@tonic-gate 			if (sig == SIGCLD)
16857c478bd9Sstevel@tonic-gate 				p->p_flag &= ~(SNOWAIT|SJCTL);
16867c478bd9Sstevel@tonic-gate 		}
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 	sigorset(&p->p_ignore, &ignoredefault);
16897c478bd9Sstevel@tonic-gate 	sigfillset(&p->p_siginfo);
16907c478bd9Sstevel@tonic-gate 	sigdiffset(&p->p_siginfo, &cantmask);
16917c478bd9Sstevel@tonic-gate 	sigdiffset(&p->p_sig, &ignoredefault);
16927c478bd9Sstevel@tonic-gate 	sigdiffset(&p->p_extsig, &ignoredefault);
16937c478bd9Sstevel@tonic-gate 	sigdiffset(&t->t_sig, &ignoredefault);
16947c478bd9Sstevel@tonic-gate 	sigdiffset(&t->t_extsig, &ignoredefault);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate void
sigcld(proc_t * cp,sigqueue_t * sqp)16987c478bd9Sstevel@tonic-gate sigcld(proc_t *cp, sigqueue_t *sqp)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	proc_t *pp = cp->p_parent;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	switch (cp->p_wcode) {
17057c478bd9Sstevel@tonic-gate 	case CLD_EXITED:
17067c478bd9Sstevel@tonic-gate 	case CLD_DUMPED:
17077c478bd9Sstevel@tonic-gate 	case CLD_KILLED:
17087c478bd9Sstevel@tonic-gate 		ASSERT(cp->p_stat == SZOMB);
17097c478bd9Sstevel@tonic-gate 		/*
17107c478bd9Sstevel@tonic-gate 		 * The broadcast on p_srwchan_cv is a kludge to
17117c478bd9Sstevel@tonic-gate 		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
17127c478bd9Sstevel@tonic-gate 		 */
17137c478bd9Sstevel@tonic-gate 		cv_broadcast(&cp->p_srwchan_cv);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 		/*
17167c478bd9Sstevel@tonic-gate 		 * Add to newstate list of the parent
17177c478bd9Sstevel@tonic-gate 		 */
17187c478bd9Sstevel@tonic-gate 		add_ns(pp, cp);
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 		cv_broadcast(&pp->p_cv);
17217c478bd9Sstevel@tonic-gate 		if ((pp->p_flag & SNOWAIT) ||
1722657b1f3dSraf 		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
1723657b1f3dSraf 			if (!(cp->p_pidflag & CLDWAITPID))
1724657b1f3dSraf 				freeproc(cp);
1725657b1f3dSraf 		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
17267c478bd9Sstevel@tonic-gate 			post_sigcld(cp, sqp);
17277c478bd9Sstevel@tonic-gate 			sqp = NULL;
17287c478bd9Sstevel@tonic-gate 		}
17297c478bd9Sstevel@tonic-gate 		break;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	case CLD_STOPPED:
17327c478bd9Sstevel@tonic-gate 	case CLD_CONTINUED:
17337c478bd9Sstevel@tonic-gate 		cv_broadcast(&pp->p_cv);
17347c478bd9Sstevel@tonic-gate 		if (pp->p_flag & SJCTL) {
17357c478bd9Sstevel@tonic-gate 			post_sigcld(cp, sqp);
17367c478bd9Sstevel@tonic-gate 			sqp = NULL;
17377c478bd9Sstevel@tonic-gate 		}
17387c478bd9Sstevel@tonic-gate 		break;
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	if (sqp)
17427c478bd9Sstevel@tonic-gate 		siginfofree(sqp);
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /*
17463f12bda8SRoger A. Faulkner  * Common code called from sigcld() and from
17473f12bda8SRoger A. Faulkner  * waitid() and issig_forreal() via sigcld_repost().
17487c478bd9Sstevel@tonic-gate  * Give the parent process a SIGCLD if it does not have one pending,
17497c478bd9Sstevel@tonic-gate  * else mark the child process so a SIGCLD can be posted later.
17507c478bd9Sstevel@tonic-gate  */
17517c478bd9Sstevel@tonic-gate static void
post_sigcld(proc_t * cp,sigqueue_t * sqp)17527c478bd9Sstevel@tonic-gate post_sigcld(proc_t *cp, sigqueue_t *sqp)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	proc_t *pp = cp->p_parent;
17557c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
17587c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/*
17613f12bda8SRoger A. Faulkner 	 * If a SIGCLD is pending, then just mark the child process
17623f12bda8SRoger A. Faulkner 	 * so that its SIGCLD will be posted later, when the first
17633f12bda8SRoger A. Faulkner 	 * SIGCLD is taken off the queue or when the parent is ready
17643f12bda8SRoger A. Faulkner 	 * to receive it or accept it, if ever.
17657c478bd9Sstevel@tonic-gate 	 */
17663f12bda8SRoger A. Faulkner 	if (sigismember(&pp->p_sig, SIGCLD)) {
17677c478bd9Sstevel@tonic-gate 		cp->p_pidflag |= CLDPEND;
17683f12bda8SRoger A. Faulkner 	} else {
17697c478bd9Sstevel@tonic-gate 		cp->p_pidflag &= ~CLDPEND;
17707c478bd9Sstevel@tonic-gate 		if (sqp == NULL) {
17717c478bd9Sstevel@tonic-gate 			/*
17727c478bd9Sstevel@tonic-gate 			 * This can only happen when the parent is init.
17737c478bd9Sstevel@tonic-gate 			 * (See call to sigcld(q, NULL) in exit().)
17747c478bd9Sstevel@tonic-gate 			 * Use KM_NOSLEEP to avoid deadlock.
17757c478bd9Sstevel@tonic-gate 			 */
17767c478bd9Sstevel@tonic-gate 			ASSERT(pp == proc_init);
17777c478bd9Sstevel@tonic-gate 			winfo(cp, &info, 0);
17787c478bd9Sstevel@tonic-gate 			sigaddq(pp, NULL, &info, KM_NOSLEEP);
17797c478bd9Sstevel@tonic-gate 		} else {
17807c478bd9Sstevel@tonic-gate 			winfo(cp, &sqp->sq_info, 0);
17817c478bd9Sstevel@tonic-gate 			sigaddqa(pp, NULL, sqp);
17827c478bd9Sstevel@tonic-gate 			sqp = NULL;
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	if (sqp)
17897c478bd9Sstevel@tonic-gate 		siginfofree(sqp);
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate  * Search for a child that has a pending SIGCLD for us, the parent.
17947c478bd9Sstevel@tonic-gate  * The queue of SIGCLD signals is implied by the list of children.
17957c478bd9Sstevel@tonic-gate  * We post the SIGCLD signals one at a time so they don't get lost.
17967c478bd9Sstevel@tonic-gate  * When one is dequeued, another is enqueued, until there are no more.
17977c478bd9Sstevel@tonic-gate  */
17987c478bd9Sstevel@tonic-gate void
sigcld_repost()17997c478bd9Sstevel@tonic-gate sigcld_repost()
18007c478bd9Sstevel@tonic-gate {
18017c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
18027c478bd9Sstevel@tonic-gate 	proc_t *cp;
18037c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
18067c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
18077c478bd9Sstevel@tonic-gate 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
18087c478bd9Sstevel@tonic-gate 		if (cp->p_pidflag & CLDPEND) {
18097c478bd9Sstevel@tonic-gate 			post_sigcld(cp, sqp);
18107c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
18117c478bd9Sstevel@tonic-gate 			return;
18127c478bd9Sstevel@tonic-gate 		}
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
18157c478bd9Sstevel@tonic-gate 	kmem_free(sqp, sizeof (sigqueue_t));
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate /*
18197c478bd9Sstevel@tonic-gate  * count number of sigqueue send by sigaddqa()
18207c478bd9Sstevel@tonic-gate  */
18217c478bd9Sstevel@tonic-gate void
sigqsend(int cmd,proc_t * p,kthread_t * t,sigqueue_t * sigqp)18227c478bd9Sstevel@tonic-gate sigqsend(int cmd, proc_t *p, kthread_t *t, sigqueue_t *sigqp)
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	sigqhdr_t *sqh;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	sqh = (sigqhdr_t *)sigqp->sq_backptr;
18277c478bd9Sstevel@tonic-gate 	ASSERT(sqh);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	mutex_enter(&sqh->sqb_lock);
18307c478bd9Sstevel@tonic-gate 	sqh->sqb_sent++;
18317c478bd9Sstevel@tonic-gate 	mutex_exit(&sqh->sqb_lock);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	if (cmd == SN_SEND)
18347c478bd9Sstevel@tonic-gate 		sigaddqa(p, t, sigqp);
18357c478bd9Sstevel@tonic-gate 	else
18367c478bd9Sstevel@tonic-gate 		siginfofree(sigqp);
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate int
sigsendproc(proc_t * p,sigsend_t * pv)18407c478bd9Sstevel@tonic-gate sigsendproc(proc_t *p, sigsend_t *pv)
18417c478bd9Sstevel@tonic-gate {
18427c478bd9Sstevel@tonic-gate 	struct cred *cr;
18437c478bd9Sstevel@tonic-gate 	proc_t *myprocp = curproc;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if (p->p_pid == 1 && pv->sig && sigismember(&cantmask, pv->sig))
18487c478bd9Sstevel@tonic-gate 		return (EPERM);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	cr = CRED();
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	if (pv->checkperm == 0 ||
18537c478bd9Sstevel@tonic-gate 	    (pv->sig == SIGCONT && p->p_sessp == myprocp->p_sessp) ||
18547c478bd9Sstevel@tonic-gate 	    prochasprocperm(p, myprocp, cr)) {
18557c478bd9Sstevel@tonic-gate 		pv->perm++;
18567c478bd9Sstevel@tonic-gate 		if (pv->sig) {
18577c478bd9Sstevel@tonic-gate 			/* Make sure we should be setting si_pid and friends */
18587c478bd9Sstevel@tonic-gate 			ASSERT(pv->sicode <= 0);
18597c478bd9Sstevel@tonic-gate 			if (SI_CANQUEUE(pv->sicode)) {
18607c478bd9Sstevel@tonic-gate 				sigqueue_t *sqp;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 				mutex_enter(&myprocp->p_lock);
18637c478bd9Sstevel@tonic-gate 				sqp = sigqalloc(myprocp->p_sigqhdr);
18647c478bd9Sstevel@tonic-gate 				mutex_exit(&myprocp->p_lock);
18657c478bd9Sstevel@tonic-gate 				if (sqp == NULL)
18667c478bd9Sstevel@tonic-gate 					return (EAGAIN);
18677c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_signo = pv->sig;
18687c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_code = pv->sicode;
18697c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_pid = myprocp->p_pid;
18707c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_ctid = PRCTID(myprocp);
18717c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_zoneid = getzoneid();
18727c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_uid = crgetruid(cr);
18737c478bd9Sstevel@tonic-gate 				sqp->sq_info.si_value = pv->value;
18747c478bd9Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
18757c478bd9Sstevel@tonic-gate 				sigqsend(SN_SEND, p, NULL, sqp);
18767c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
18777c478bd9Sstevel@tonic-gate 			} else {
18787c478bd9Sstevel@tonic-gate 				k_siginfo_t info;
18797c478bd9Sstevel@tonic-gate 				bzero(&info, sizeof (info));
18807c478bd9Sstevel@tonic-gate 				info.si_signo = pv->sig;
18817c478bd9Sstevel@tonic-gate 				info.si_code = pv->sicode;
18827c478bd9Sstevel@tonic-gate 				info.si_pid = myprocp->p_pid;
18837c478bd9Sstevel@tonic-gate 				info.si_ctid = PRCTID(myprocp);
18847c478bd9Sstevel@tonic-gate 				info.si_zoneid = getzoneid();
18857c478bd9Sstevel@tonic-gate 				info.si_uid = crgetruid(cr);
18867c478bd9Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
18877c478bd9Sstevel@tonic-gate 				/*
18887c478bd9Sstevel@tonic-gate 				 * XXX: Should be KM_SLEEP but
18897c478bd9Sstevel@tonic-gate 				 * we have to avoid deadlock.
18907c478bd9Sstevel@tonic-gate 				 */
18917c478bd9Sstevel@tonic-gate 				sigaddq(p, NULL, &info, KM_NOSLEEP);
18927c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
18937c478bd9Sstevel@tonic-gate 			}
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	return (0);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate int
sigsendset(procset_t * psp,sigsend_t * pv)19017c478bd9Sstevel@tonic-gate sigsendset(procset_t *psp, sigsend_t *pv)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	int error;
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	error = dotoprocs(psp, sigsendproc, (char *)pv);
19067c478bd9Sstevel@tonic-gate 	if (error == 0 && pv->perm == 0)
19077c478bd9Sstevel@tonic-gate 		return (EPERM);
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	return (error);
19107c478bd9Sstevel@tonic-gate }
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate /*
19137c478bd9Sstevel@tonic-gate  * Dequeue a queued siginfo structure.
19147c478bd9Sstevel@tonic-gate  * If a non-null thread pointer is passed then dequeue from
19157c478bd9Sstevel@tonic-gate  * the thread queue, otherwise dequeue from the process queue.
19167c478bd9Sstevel@tonic-gate  */
19177c478bd9Sstevel@tonic-gate void
sigdeq(proc_t * p,kthread_t * t,int sig,sigqueue_t ** qpp)19187c478bd9Sstevel@tonic-gate sigdeq(proc_t *p, kthread_t *t, int sig, sigqueue_t **qpp)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate 	sigqueue_t **psqp, *sqp;
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	*qpp = NULL;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	if (t != NULL) {
19277c478bd9Sstevel@tonic-gate 		sigdelset(&t->t_sig, sig);
19287c478bd9Sstevel@tonic-gate 		sigdelset(&t->t_extsig, sig);
19297c478bd9Sstevel@tonic-gate 		psqp = &t->t_sigqueue;
19307c478bd9Sstevel@tonic-gate 	} else {
19317c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_sig, sig);
19327c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_extsig, sig);
19337c478bd9Sstevel@tonic-gate 		psqp = &p->p_sigqueue;
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	for (;;) {
19377c478bd9Sstevel@tonic-gate 		if ((sqp = *psqp) == NULL)
19387c478bd9Sstevel@tonic-gate 			return;
19397c478bd9Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == sig)
19407c478bd9Sstevel@tonic-gate 			break;
19417c478bd9Sstevel@tonic-gate 		else
19427c478bd9Sstevel@tonic-gate 			psqp = &sqp->sq_next;
19437c478bd9Sstevel@tonic-gate 	}
19447c478bd9Sstevel@tonic-gate 	*qpp = sqp;
19457c478bd9Sstevel@tonic-gate 	*psqp = sqp->sq_next;
19467c478bd9Sstevel@tonic-gate 	for (sqp = *psqp; sqp; sqp = sqp->sq_next) {
19477c478bd9Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == sig) {
19487c478bd9Sstevel@tonic-gate 			if (t != (kthread_t *)NULL) {
19497c478bd9Sstevel@tonic-gate 				sigaddset(&t->t_sig, sig);
19507c478bd9Sstevel@tonic-gate 				t->t_sig_check = 1;
19517c478bd9Sstevel@tonic-gate 			} else {
19527c478bd9Sstevel@tonic-gate 				sigaddset(&p->p_sig, sig);
19537c478bd9Sstevel@tonic-gate 				set_proc_ast(p);
19547c478bd9Sstevel@tonic-gate 			}
19557c478bd9Sstevel@tonic-gate 			break;
19567c478bd9Sstevel@tonic-gate 		}
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate }
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate /*
19617c478bd9Sstevel@tonic-gate  * Delete a queued SIGCLD siginfo structure matching the k_siginfo_t argument.
19627c478bd9Sstevel@tonic-gate  */
19637c478bd9Sstevel@tonic-gate void
sigcld_delete(k_siginfo_t * ip)19647c478bd9Sstevel@tonic-gate sigcld_delete(k_siginfo_t *ip)
19657c478bd9Sstevel@tonic-gate {
19667c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
19677c478bd9Sstevel@tonic-gate 	int another_sigcld = 0;
19687c478bd9Sstevel@tonic-gate 	sigqueue_t **psqp, *sqp;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	ASSERT(ip->si_signo == SIGCLD);
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if (!sigismember(&p->p_sig, SIGCLD)) {
19757c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
19767c478bd9Sstevel@tonic-gate 		return;
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	psqp = &p->p_sigqueue;
19807c478bd9Sstevel@tonic-gate 	for (;;) {
19817c478bd9Sstevel@tonic-gate 		if ((sqp = *psqp) == NULL) {
19827c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
19837c478bd9Sstevel@tonic-gate 			return;
19847c478bd9Sstevel@tonic-gate 		}
19857c478bd9Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == SIGCLD) {
19867c478bd9Sstevel@tonic-gate 			if (sqp->sq_info.si_pid == ip->si_pid &&
19877c478bd9Sstevel@tonic-gate 			    sqp->sq_info.si_code == ip->si_code &&
19887c478bd9Sstevel@tonic-gate 			    sqp->sq_info.si_status == ip->si_status)
19897c478bd9Sstevel@tonic-gate 				break;
19907c478bd9Sstevel@tonic-gate 			another_sigcld = 1;
19917c478bd9Sstevel@tonic-gate 		}
19927c478bd9Sstevel@tonic-gate 		psqp = &sqp->sq_next;
19937c478bd9Sstevel@tonic-gate 	}
19947c478bd9Sstevel@tonic-gate 	*psqp = sqp->sq_next;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	siginfofree(sqp);
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	for (sqp = *psqp; !another_sigcld && sqp; sqp = sqp->sq_next) {
19997c478bd9Sstevel@tonic-gate 		if (sqp->sq_info.si_signo == SIGCLD)
20007c478bd9Sstevel@tonic-gate 			another_sigcld = 1;
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	if (!another_sigcld) {
20047c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_sig, SIGCLD);
20057c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_extsig, SIGCLD);
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate /*
20127c478bd9Sstevel@tonic-gate  * Delete queued siginfo structures.
20137c478bd9Sstevel@tonic-gate  * If a non-null thread pointer is passed then delete from
20147c478bd9Sstevel@tonic-gate  * the thread queue, otherwise delete from the process queue.
20157c478bd9Sstevel@tonic-gate  */
20167c478bd9Sstevel@tonic-gate void
sigdelq(proc_t * p,kthread_t * t,int sig)20177c478bd9Sstevel@tonic-gate sigdelq(proc_t *p, kthread_t *t, int sig)
20187c478bd9Sstevel@tonic-gate {
20197c478bd9Sstevel@tonic-gate 	sigqueue_t **psqp, *sqp;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	/*
20227c478bd9Sstevel@tonic-gate 	 * We must be holding p->p_lock unless the process is
20237c478bd9Sstevel@tonic-gate 	 * being reaped or has failed to get started on fork.
20247c478bd9Sstevel@tonic-gate 	 */
20257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock) ||
20267c478bd9Sstevel@tonic-gate 	    p->p_stat == SIDL || p->p_stat == SZOMB);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	if (t != (kthread_t *)NULL)
20297c478bd9Sstevel@tonic-gate 		psqp = &t->t_sigqueue;
20307c478bd9Sstevel@tonic-gate 	else
20317c478bd9Sstevel@tonic-gate 		psqp = &p->p_sigqueue;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	while (*psqp) {
20347c478bd9Sstevel@tonic-gate 		sqp = *psqp;
20357c478bd9Sstevel@tonic-gate 		if (sig == 0 || sqp->sq_info.si_signo == sig) {
20367c478bd9Sstevel@tonic-gate 			*psqp = sqp->sq_next;
20377c478bd9Sstevel@tonic-gate 			siginfofree(sqp);
20387c478bd9Sstevel@tonic-gate 		} else
20397c478bd9Sstevel@tonic-gate 			psqp = &sqp->sq_next;
20407c478bd9Sstevel@tonic-gate 	}
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * Insert a siginfo structure into a queue.
20457c478bd9Sstevel@tonic-gate  * If a non-null thread pointer is passed then add to the thread queue,
20467c478bd9Sstevel@tonic-gate  * otherwise add to the process queue.
20477c478bd9Sstevel@tonic-gate  *
20487c478bd9Sstevel@tonic-gate  * The function sigaddqins() is called with sigqueue already allocated.
20497c478bd9Sstevel@tonic-gate  * It is called from sigaddqa() and sigaddq() below.
20507c478bd9Sstevel@tonic-gate  *
20517c478bd9Sstevel@tonic-gate  * The value of si_code implicitly indicates whether sigp is to be
20527c478bd9Sstevel@tonic-gate  * explicitly queued, or to be queued to depth one.
20537c478bd9Sstevel@tonic-gate  */
20547c478bd9Sstevel@tonic-gate static void
sigaddqins(proc_t * p,kthread_t * t,sigqueue_t * sigqp)20557c478bd9Sstevel@tonic-gate sigaddqins(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate 	sigqueue_t **psqp;
20587c478bd9Sstevel@tonic-gate 	int sig = sigqp->sq_info.si_signo;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	sigqp->sq_external = (curproc != &p0) &&
20617c478bd9Sstevel@tonic-gate 	    (curproc->p_ct_process != p->p_ct_process);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	/*
20647c478bd9Sstevel@tonic-gate 	 * issig_forreal() doesn't bother dequeueing signals if SKILLED
20657c478bd9Sstevel@tonic-gate 	 * is set, and even if it did, we would want to avoid situation
20667c478bd9Sstevel@tonic-gate 	 * (which would be unique to SIGKILL) where one thread dequeued
20677c478bd9Sstevel@tonic-gate 	 * the sigqueue_t and another executed psig().  So we create a
20687c478bd9Sstevel@tonic-gate 	 * separate stash for SIGKILL's sigqueue_t.  Because a second
20697c478bd9Sstevel@tonic-gate 	 * SIGKILL can set SEXTKILLED, we overwrite the existing entry
20707c478bd9Sstevel@tonic-gate 	 * if (and only if) it was non-extracontractual.
20717c478bd9Sstevel@tonic-gate 	 */
20727c478bd9Sstevel@tonic-gate 	if (sig == SIGKILL) {
20737c478bd9Sstevel@tonic-gate 		if (p->p_killsqp == NULL || !p->p_killsqp->sq_external) {
20747c478bd9Sstevel@tonic-gate 			if (p->p_killsqp != NULL)
20757c478bd9Sstevel@tonic-gate 				siginfofree(p->p_killsqp);
20767c478bd9Sstevel@tonic-gate 			p->p_killsqp = sigqp;
20777c478bd9Sstevel@tonic-gate 			sigqp->sq_next = NULL;
20787c478bd9Sstevel@tonic-gate 		} else {
20797c478bd9Sstevel@tonic-gate 			siginfofree(sigqp);
20807c478bd9Sstevel@tonic-gate 		}
20817c478bd9Sstevel@tonic-gate 		return;
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	ASSERT(sig >= 1 && sig < NSIG);
20857c478bd9Sstevel@tonic-gate 	if (t != NULL)	/* directed to a thread */
20867c478bd9Sstevel@tonic-gate 		psqp = &t->t_sigqueue;
20877c478bd9Sstevel@tonic-gate 	else 		/* directed to a process */
20887c478bd9Sstevel@tonic-gate 		psqp = &p->p_sigqueue;
20897c478bd9Sstevel@tonic-gate 	if (SI_CANQUEUE(sigqp->sq_info.si_code) &&
20907c478bd9Sstevel@tonic-gate 	    sigismember(&p->p_siginfo, sig)) {
20917c478bd9Sstevel@tonic-gate 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next)
20927c478bd9Sstevel@tonic-gate 				;
20937c478bd9Sstevel@tonic-gate 	} else {
20947c478bd9Sstevel@tonic-gate 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next) {
20957c478bd9Sstevel@tonic-gate 			if ((*psqp)->sq_info.si_signo == sig) {
20967c478bd9Sstevel@tonic-gate 				siginfofree(sigqp);
20977c478bd9Sstevel@tonic-gate 				return;
20987c478bd9Sstevel@tonic-gate 			}
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 	*psqp = sigqp;
21027c478bd9Sstevel@tonic-gate 	sigqp->sq_next = NULL;
21037c478bd9Sstevel@tonic-gate }
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate /*
21067c478bd9Sstevel@tonic-gate  * The function sigaddqa() is called with sigqueue already allocated.
21077c478bd9Sstevel@tonic-gate  * If signal is ignored, discard but guarantee KILL and generation semantics.
21087c478bd9Sstevel@tonic-gate  * It is called from sigqueue() and other places.
21097c478bd9Sstevel@tonic-gate  */
21107c478bd9Sstevel@tonic-gate void
sigaddqa(proc_t * p,kthread_t * t,sigqueue_t * sigqp)21117c478bd9Sstevel@tonic-gate sigaddqa(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
21127c478bd9Sstevel@tonic-gate {
21137c478bd9Sstevel@tonic-gate 	int sig = sigqp->sq_info.si_signo;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
21167c478bd9Sstevel@tonic-gate 	ASSERT(sig >= 1 && sig < NSIG);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	if (sig_discardable(p, sig))
21197c478bd9Sstevel@tonic-gate 		siginfofree(sigqp);
21207c478bd9Sstevel@tonic-gate 	else
21217c478bd9Sstevel@tonic-gate 		sigaddqins(p, t, sigqp);
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	sigtoproc(p, t, sig);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate /*
21277c478bd9Sstevel@tonic-gate  * Allocate the sigqueue_t structure and call sigaddqins().
21287c478bd9Sstevel@tonic-gate  */
21297c478bd9Sstevel@tonic-gate void
sigaddq(proc_t * p,kthread_t * t,k_siginfo_t * infop,int km_flags)21307c478bd9Sstevel@tonic-gate sigaddq(proc_t *p, kthread_t *t, k_siginfo_t *infop, int km_flags)
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp;
21337c478bd9Sstevel@tonic-gate 	int sig = infop->si_signo;
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
21367c478bd9Sstevel@tonic-gate 	ASSERT(sig >= 1 && sig < NSIG);
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	/*
21397c478bd9Sstevel@tonic-gate 	 * If the signal will be discarded by sigtoproc() or
21407c478bd9Sstevel@tonic-gate 	 * if the process isn't requesting siginfo and it isn't
21417c478bd9Sstevel@tonic-gate 	 * blocking the signal (it *could* change it's mind while
21427c478bd9Sstevel@tonic-gate 	 * the signal is pending) then don't bother creating one.
21437c478bd9Sstevel@tonic-gate 	 */
21447c478bd9Sstevel@tonic-gate 	if (!sig_discardable(p, sig) &&
21457c478bd9Sstevel@tonic-gate 	    (sigismember(&p->p_siginfo, sig) ||
21467c478bd9Sstevel@tonic-gate 	    (curproc->p_ct_process != p->p_ct_process) ||
21477c478bd9Sstevel@tonic-gate 	    (sig == SIGCLD && SI_FROMKERNEL(infop))) &&
21487c478bd9Sstevel@tonic-gate 	    ((sqp = kmem_alloc(sizeof (sigqueue_t), km_flags)) != NULL)) {
21497c478bd9Sstevel@tonic-gate 		bcopy(infop, &sqp->sq_info, sizeof (k_siginfo_t));
21507c478bd9Sstevel@tonic-gate 		sqp->sq_func = NULL;
21517c478bd9Sstevel@tonic-gate 		sqp->sq_next = NULL;
21527c478bd9Sstevel@tonic-gate 		sigaddqins(p, t, sqp);
21537c478bd9Sstevel@tonic-gate 	}
21547c478bd9Sstevel@tonic-gate 	sigtoproc(p, t, sig);
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate /*
21587c478bd9Sstevel@tonic-gate  * Handle stop-on-fault processing for the debugger.  Returns 0
21597c478bd9Sstevel@tonic-gate  * if the fault is cleared during the stop, nonzero if it isn't.
21607c478bd9Sstevel@tonic-gate  */
21617c478bd9Sstevel@tonic-gate int
stop_on_fault(uint_t fault,k_siginfo_t * sip)21627c478bd9Sstevel@tonic-gate stop_on_fault(uint_t fault, k_siginfo_t *sip)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
21657c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	ASSERT(prismember(&p->p_fltmask, fault));
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * Record current fault and siginfo structure so debugger can
21717c478bd9Sstevel@tonic-gate 	 * find it.
21727c478bd9Sstevel@tonic-gate 	 */
21737c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
21747c478bd9Sstevel@tonic-gate 	lwp->lwp_curflt = (uchar_t)fault;
21757c478bd9Sstevel@tonic-gate 	lwp->lwp_siginfo = *sip;
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	stop(PR_FAULTED, fault);
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	fault = lwp->lwp_curflt;
21807c478bd9Sstevel@tonic-gate 	lwp->lwp_curflt = 0;
21817c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
21827c478bd9Sstevel@tonic-gate 	return (fault);
21837c478bd9Sstevel@tonic-gate }
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate void
sigorset(k_sigset_t * s1,const k_sigset_t * s2)2186bdf0047cSRoger A. Faulkner sigorset(k_sigset_t *s1, const k_sigset_t *s2)
21877c478bd9Sstevel@tonic-gate {
21887c478bd9Sstevel@tonic-gate 	s1->__sigbits[0] |= s2->__sigbits[0];
21897c478bd9Sstevel@tonic-gate 	s1->__sigbits[1] |= s2->__sigbits[1];
2190bdf0047cSRoger A. Faulkner 	s1->__sigbits[2] |= s2->__sigbits[2];
21917c478bd9Sstevel@tonic-gate }
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate void
sigandset(k_sigset_t * s1,const k_sigset_t * s2)2194bdf0047cSRoger A. Faulkner sigandset(k_sigset_t *s1, const k_sigset_t *s2)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	s1->__sigbits[0] &= s2->__sigbits[0];
21977c478bd9Sstevel@tonic-gate 	s1->__sigbits[1] &= s2->__sigbits[1];
2198bdf0047cSRoger A. Faulkner 	s1->__sigbits[2] &= s2->__sigbits[2];
21997c478bd9Sstevel@tonic-gate }
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate void
sigdiffset(k_sigset_t * s1,const k_sigset_t * s2)2202bdf0047cSRoger A. Faulkner sigdiffset(k_sigset_t *s1, const k_sigset_t *s2)
22037c478bd9Sstevel@tonic-gate {
22047c478bd9Sstevel@tonic-gate 	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
22057c478bd9Sstevel@tonic-gate 	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
2206bdf0047cSRoger A. Faulkner 	s1->__sigbits[2] &= ~(s2->__sigbits[2]);
22077c478bd9Sstevel@tonic-gate }
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate /*
22107c478bd9Sstevel@tonic-gate  * Return non-zero if curthread->t_sig_check should be set to 1, that is,
22117c478bd9Sstevel@tonic-gate  * if there are any signals the thread might take on return from the kernel.
22127c478bd9Sstevel@tonic-gate  * If ksigset_t's were a single word, we would do:
22137c478bd9Sstevel@tonic-gate  *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
22147c478bd9Sstevel@tonic-gate  */
22157c478bd9Sstevel@tonic-gate int
sigcheck(proc_t * p,kthread_t * t)22167c478bd9Sstevel@tonic-gate sigcheck(proc_t *p, kthread_t *t)
22177c478bd9Sstevel@tonic-gate {
22187c478bd9Sstevel@tonic-gate 	sc_shared_t *tdp = t->t_schedctl;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	/*
22217c478bd9Sstevel@tonic-gate 	 * If signals are blocked via the schedctl interface
22227c478bd9Sstevel@tonic-gate 	 * then we only check for the unmaskable signals.
2223bdf0047cSRoger A. Faulkner 	 * The unmaskable signal numbers should all be contained
2224bdf0047cSRoger A. Faulkner 	 * in __sigbits[0] and we assume this for speed.
22257c478bd9Sstevel@tonic-gate 	 */
2226bdf0047cSRoger A. Faulkner #if (CANTMASK1 == 0 && CANTMASK2 == 0)
22277c478bd9Sstevel@tonic-gate 	if (tdp != NULL && tdp->sc_sigblock)
22287c478bd9Sstevel@tonic-gate 		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
22297c478bd9Sstevel@tonic-gate 		    CANTMASK0);
2230bdf0047cSRoger A. Faulkner #else
2231bdf0047cSRoger A. Faulkner #error "fix me: CANTMASK1 and CANTMASK2 are not zero"
2232bdf0047cSRoger A. Faulkner #endif
22337c478bd9Sstevel@tonic-gate 
2234bdf0047cSRoger A. Faulkner /* see uts/common/sys/signal.h for why this must be true */
2235bdf0047cSRoger A. Faulkner #if ((MAXSIG > (2 * 32)) && (MAXSIG <= (3 * 32)))
22367c478bd9Sstevel@tonic-gate 	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2237cbdce38dSbpramod 	    ~t->t_hold.__sigbits[0]) |
2238bdf0047cSRoger A. Faulkner 	    ((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
2239bdf0047cSRoger A. Faulkner 	    ~t->t_hold.__sigbits[1]) |
2240bdf0047cSRoger A. Faulkner 	    (((p->p_sig.__sigbits[2] | t->t_sig.__sigbits[2]) &
2241bdf0047cSRoger A. Faulkner 	    ~t->t_hold.__sigbits[2]) & FILLSET2));
2242bdf0047cSRoger A. Faulkner #else
2243bdf0047cSRoger A. Faulkner #error "fix me: MAXSIG out of bounds"
2244bdf0047cSRoger A. Faulkner #endif
22457c478bd9Sstevel@tonic-gate }
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate void
sigintr(k_sigset_t * smask,int intable)22487c478bd9Sstevel@tonic-gate sigintr(k_sigset_t *smask, int intable)
22497c478bd9Sstevel@tonic-gate {
22507c478bd9Sstevel@tonic-gate 	proc_t *p;
22517c478bd9Sstevel@tonic-gate 	int owned;
22527c478bd9Sstevel@tonic-gate 	k_sigset_t lmask;		/* local copy of cantmask */
22537c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	/*
22567c478bd9Sstevel@tonic-gate 	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
22577c478bd9Sstevel@tonic-gate 	 *    and SIGTERM. (Preserving the existing masks).
22587c478bd9Sstevel@tonic-gate 	 *    This function supports the -intr nfs and ufs mount option.
22597c478bd9Sstevel@tonic-gate 	 */
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	/*
22627c478bd9Sstevel@tonic-gate 	 * don't do kernel threads
22637c478bd9Sstevel@tonic-gate 	 */
22647c478bd9Sstevel@tonic-gate 	if (lwp == NULL)
22657c478bd9Sstevel@tonic-gate 		return;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * get access to signal mask
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	p = ttoproc(curthread);
22717c478bd9Sstevel@tonic-gate 	owned = mutex_owned(&p->p_lock);	/* this is filthy */
22727c478bd9Sstevel@tonic-gate 	if (!owned)
22737c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	/*
22767c478bd9Sstevel@tonic-gate 	 * remember the current mask
22777c478bd9Sstevel@tonic-gate 	 */
22787c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
22797c478bd9Sstevel@tonic-gate 	*smask = curthread->t_hold;
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	/*
22827c478bd9Sstevel@tonic-gate 	 * mask out all signals
22837c478bd9Sstevel@tonic-gate 	 */
22847c478bd9Sstevel@tonic-gate 	sigfillset(&curthread->t_hold);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	/*
22877c478bd9Sstevel@tonic-gate 	 * Unmask the non-maskable signals (e.g., KILL), as long as
22887c478bd9Sstevel@tonic-gate 	 * they aren't already masked (which could happen at exit).
22897c478bd9Sstevel@tonic-gate 	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
22907c478bd9Sstevel@tonic-gate 	 * second sets the current hold mask to (~0 & ~lmask), which reduces
22917c478bd9Sstevel@tonic-gate 	 * to (~cantmask | curhold).
22927c478bd9Sstevel@tonic-gate 	 */
22937c478bd9Sstevel@tonic-gate 	lmask = cantmask;
22947c478bd9Sstevel@tonic-gate 	sigdiffset(&lmask, smask);
22957c478bd9Sstevel@tonic-gate 	sigdiffset(&curthread->t_hold, &lmask);
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	/*
22987c478bd9Sstevel@tonic-gate 	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
22997c478bd9Sstevel@tonic-gate 	 * Re-enable INT if it's originally enabled and the NFS mount option
23007c478bd9Sstevel@tonic-gate 	 * nointr is not set.
23017c478bd9Sstevel@tonic-gate 	 */
23027c478bd9Sstevel@tonic-gate 	if (!sigismember(smask, SIGHUP))
23037c478bd9Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGHUP);
23047c478bd9Sstevel@tonic-gate 	if (!sigismember(smask, SIGINT) && intable)
23057c478bd9Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGINT);
23067c478bd9Sstevel@tonic-gate 	if (!sigismember(smask, SIGQUIT))
23077c478bd9Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGQUIT);
23087c478bd9Sstevel@tonic-gate 	if (!sigismember(smask, SIGTERM))
23097c478bd9Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, SIGTERM);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	/*
23127c478bd9Sstevel@tonic-gate 	 * release access to signal mask
23137c478bd9Sstevel@tonic-gate 	 */
23147c478bd9Sstevel@tonic-gate 	if (!owned)
23157c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	/*
23187c478bd9Sstevel@tonic-gate 	 * Indicate that this lwp is not to be stopped.
23197c478bd9Sstevel@tonic-gate 	 */
23207c478bd9Sstevel@tonic-gate 	lwp->lwp_nostop++;
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate void
sigunintr(k_sigset_t * smask)23257c478bd9Sstevel@tonic-gate sigunintr(k_sigset_t *smask)
23267c478bd9Sstevel@tonic-gate {
23277c478bd9Sstevel@tonic-gate 	proc_t *p;
23287c478bd9Sstevel@tonic-gate 	int owned;
23297c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	/*
23327c478bd9Sstevel@tonic-gate 	 * Reset previous mask (See sigintr() above)
23337c478bd9Sstevel@tonic-gate 	 */
23347c478bd9Sstevel@tonic-gate 	if (lwp != NULL) {
23357c478bd9Sstevel@tonic-gate 		lwp->lwp_nostop--;	/* restore lwp stoppability */
23367c478bd9Sstevel@tonic-gate 		p = ttoproc(curthread);
23377c478bd9Sstevel@tonic-gate 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
23387c478bd9Sstevel@tonic-gate 		if (!owned)
23397c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
23407c478bd9Sstevel@tonic-gate 		curthread->t_hold = *smask;
23417c478bd9Sstevel@tonic-gate 		/* so unmasked signals will be seen */
23427c478bd9Sstevel@tonic-gate 		curthread->t_sig_check = 1;
23437c478bd9Sstevel@tonic-gate 		if (!owned)
23447c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate void
sigreplace(k_sigset_t * newmask,k_sigset_t * oldmask)23497c478bd9Sstevel@tonic-gate sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
23507c478bd9Sstevel@tonic-gate {
23517c478bd9Sstevel@tonic-gate 	proc_t	*p;
23527c478bd9Sstevel@tonic-gate 	int owned;
23537c478bd9Sstevel@tonic-gate 	/*
23547c478bd9Sstevel@tonic-gate 	 * Save current signal mask in oldmask, then
23557c478bd9Sstevel@tonic-gate 	 * set it to newmask.
23567c478bd9Sstevel@tonic-gate 	 */
23577c478bd9Sstevel@tonic-gate 	if (ttolwp(curthread) != NULL) {
23587c478bd9Sstevel@tonic-gate 		p = ttoproc(curthread);
23597c478bd9Sstevel@tonic-gate 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
23607c478bd9Sstevel@tonic-gate 		if (!owned)
23617c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
23627c478bd9Sstevel@tonic-gate 		schedctl_finish_sigblock(curthread);
23637c478bd9Sstevel@tonic-gate 		if (oldmask != NULL)
23647c478bd9Sstevel@tonic-gate 			*oldmask = curthread->t_hold;
23657c478bd9Sstevel@tonic-gate 		curthread->t_hold = *newmask;
23667c478bd9Sstevel@tonic-gate 		curthread->t_sig_check = 1;
23677c478bd9Sstevel@tonic-gate 		if (!owned)
23687c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
23697c478bd9Sstevel@tonic-gate 	}
23707c478bd9Sstevel@tonic-gate }
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate /*
23737c478bd9Sstevel@tonic-gate  * Return true if the signal number is in range
23747c478bd9Sstevel@tonic-gate  * and the signal code specifies signal queueing.
23757c478bd9Sstevel@tonic-gate  */
23767c478bd9Sstevel@tonic-gate int
sigwillqueue(int sig,int code)23777c478bd9Sstevel@tonic-gate sigwillqueue(int sig, int code)
23787c478bd9Sstevel@tonic-gate {
23797c478bd9Sstevel@tonic-gate 	if (sig >= 0 && sig < NSIG) {
23807c478bd9Sstevel@tonic-gate 		switch (code) {
23817c478bd9Sstevel@tonic-gate 		case SI_QUEUE:
23827c478bd9Sstevel@tonic-gate 		case SI_TIMER:
23837c478bd9Sstevel@tonic-gate 		case SI_ASYNCIO:
23847c478bd9Sstevel@tonic-gate 		case SI_MESGQ:
23857c478bd9Sstevel@tonic-gate 			return (1);
23867c478bd9Sstevel@tonic-gate 		}
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 	return (0);
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate /*
23925e989a96SDavid Höppner  * The pre-allocated pool (with _SIGQUEUE_PREALLOC entries) is
23935e989a96SDavid Höppner  * allocated at the first sigqueue/signotify call.
23947c478bd9Sstevel@tonic-gate  */
23957c478bd9Sstevel@tonic-gate sigqhdr_t *
sigqhdralloc(size_t size,uint_t maxcount)23967c478bd9Sstevel@tonic-gate sigqhdralloc(size_t size, uint_t maxcount)
23977c478bd9Sstevel@tonic-gate {
23987c478bd9Sstevel@tonic-gate 	size_t i;
23997c478bd9Sstevel@tonic-gate 	sigqueue_t *sq, *next;
24007c478bd9Sstevel@tonic-gate 	sigqhdr_t *sqh;
24017c478bd9Sstevel@tonic-gate 
24025e989a96SDavid Höppner 	/*
24035e989a96SDavid Höppner 	 * Before the introduction of process.max-sigqueue-size
24045e989a96SDavid Höppner 	 * _SC_SIGQUEUE_MAX had this static value.
24055e989a96SDavid Höppner 	 */
24065e989a96SDavid Höppner #define	_SIGQUEUE_PREALLOC	32
24075e989a96SDavid Höppner 
24085e989a96SDavid Höppner 	i = (_SIGQUEUE_PREALLOC * size) + sizeof (sigqhdr_t);
24095e989a96SDavid Höppner 	ASSERT(maxcount <= INT_MAX);
24107c478bd9Sstevel@tonic-gate 	sqh = kmem_alloc(i, KM_SLEEP);
24115e989a96SDavid Höppner 	sqh->sqb_count = maxcount;
24125e989a96SDavid Höppner 	sqh->sqb_maxcount = maxcount;
24135e989a96SDavid Höppner 	sqh->sqb_size = i;
24147c478bd9Sstevel@tonic-gate 	sqh->sqb_pexited = 0;
24157c478bd9Sstevel@tonic-gate 	sqh->sqb_sent = 0;
24167c478bd9Sstevel@tonic-gate 	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
24175e989a96SDavid Höppner 	for (i = _SIGQUEUE_PREALLOC - 1; i != 0; i--) {
24187c478bd9Sstevel@tonic-gate 		next = (sigqueue_t *)((uintptr_t)sq + size);
24197c478bd9Sstevel@tonic-gate 		sq->sq_next = next;
24207c478bd9Sstevel@tonic-gate 		sq = next;
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 	sq->sq_next = NULL;
2423f841f6adSraf 	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
24247c478bd9Sstevel@tonic-gate 	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
24257c478bd9Sstevel@tonic-gate 	return (sqh);
24267c478bd9Sstevel@tonic-gate }
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate static void sigqrel(sigqueue_t *);
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate /*
24315e989a96SDavid Höppner  * Allocate a sigqueue/signotify structure from the per process
24325e989a96SDavid Höppner  * pre-allocated pool or allocate a new sigqueue/signotify structure
24335e989a96SDavid Höppner  * if the pre-allocated pool is exhausted.
24347c478bd9Sstevel@tonic-gate  */
24357c478bd9Sstevel@tonic-gate sigqueue_t *
sigqalloc(sigqhdr_t * sqh)24367c478bd9Sstevel@tonic-gate sigqalloc(sigqhdr_t *sqh)
24377c478bd9Sstevel@tonic-gate {
24387c478bd9Sstevel@tonic-gate 	sigqueue_t *sq = NULL;
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&curproc->p_lock));
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	if (sqh != NULL) {
24437c478bd9Sstevel@tonic-gate 		mutex_enter(&sqh->sqb_lock);
24447c478bd9Sstevel@tonic-gate 		if (sqh->sqb_count > 0) {
24457c478bd9Sstevel@tonic-gate 			sqh->sqb_count--;
24465e989a96SDavid Höppner 			if (sqh->sqb_free == NULL) {
24475e989a96SDavid Höppner 				/*
24485e989a96SDavid Höppner 				 * The pre-allocated pool is exhausted.
24495e989a96SDavid Höppner 				 */
24505e989a96SDavid Höppner 				sq = kmem_alloc(sizeof (sigqueue_t), KM_SLEEP);
24515e989a96SDavid Höppner 				sq->sq_func = NULL;
24525e989a96SDavid Höppner 			} else {
24535e989a96SDavid Höppner 				sq = sqh->sqb_free;
24545e989a96SDavid Höppner 				sq->sq_func = sigqrel;
24555e989a96SDavid Höppner 				sqh->sqb_free = sq->sq_next;
24565e989a96SDavid Höppner 			}
24577c478bd9Sstevel@tonic-gate 			mutex_exit(&sqh->sqb_lock);
24587c478bd9Sstevel@tonic-gate 			bzero(&sq->sq_info, sizeof (k_siginfo_t));
24597c478bd9Sstevel@tonic-gate 			sq->sq_backptr = sqh;
24607c478bd9Sstevel@tonic-gate 			sq->sq_next = NULL;
24617c478bd9Sstevel@tonic-gate 			sq->sq_external = 0;
24627c478bd9Sstevel@tonic-gate 		} else {
24637c478bd9Sstevel@tonic-gate 			mutex_exit(&sqh->sqb_lock);
24647c478bd9Sstevel@tonic-gate 		}
24657c478bd9Sstevel@tonic-gate 	}
24667c478bd9Sstevel@tonic-gate 	return (sq);
24677c478bd9Sstevel@tonic-gate }
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate /*
24707c478bd9Sstevel@tonic-gate  * Return a sigqueue structure back to the pre-allocated pool.
24717c478bd9Sstevel@tonic-gate  */
24727c478bd9Sstevel@tonic-gate static void
sigqrel(sigqueue_t * sq)24737c478bd9Sstevel@tonic-gate sigqrel(sigqueue_t *sq)
24747c478bd9Sstevel@tonic-gate {
24757c478bd9Sstevel@tonic-gate 	sigqhdr_t *sqh;
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	/* make sure that p_lock of the affected process is held */
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	sqh = (sigqhdr_t *)sq->sq_backptr;
24807c478bd9Sstevel@tonic-gate 	mutex_enter(&sqh->sqb_lock);
24817c478bd9Sstevel@tonic-gate 	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
24827c478bd9Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
2483f841f6adSraf 		cv_destroy(&sqh->sqb_cv);
24847c478bd9Sstevel@tonic-gate 		mutex_destroy(&sqh->sqb_lock);
24857c478bd9Sstevel@tonic-gate 		kmem_free(sqh, sqh->sqb_size);
24867c478bd9Sstevel@tonic-gate 	} else {
24877c478bd9Sstevel@tonic-gate 		sqh->sqb_count++;
24887c478bd9Sstevel@tonic-gate 		sqh->sqb_sent--;
24897c478bd9Sstevel@tonic-gate 		sq->sq_next = sqh->sqb_free;
24907c478bd9Sstevel@tonic-gate 		sq->sq_backptr = NULL;
24917c478bd9Sstevel@tonic-gate 		sqh->sqb_free = sq;
2492f841f6adSraf 		cv_signal(&sqh->sqb_cv);
24937c478bd9Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate /*
24987c478bd9Sstevel@tonic-gate  * Free up the pre-allocated sigqueue headers of sigqueue pool
24997c478bd9Sstevel@tonic-gate  * and signotify pool, if possible.
25007c478bd9Sstevel@tonic-gate  * Called only by the owning process during exec() and exit().
25017c478bd9Sstevel@tonic-gate  */
25027c478bd9Sstevel@tonic-gate void
sigqfree(proc_t * p)25037c478bd9Sstevel@tonic-gate sigqfree(proc_t *p)
25047c478bd9Sstevel@tonic-gate {
25057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
25087c478bd9Sstevel@tonic-gate 		sigqhdrfree(p->p_sigqhdr);
25097c478bd9Sstevel@tonic-gate 		p->p_sigqhdr = NULL;
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 	if (p->p_signhdr != NULL) {	/* signotify pool */
25127c478bd9Sstevel@tonic-gate 		sigqhdrfree(p->p_signhdr);
25137c478bd9Sstevel@tonic-gate 		p->p_signhdr = NULL;
25147c478bd9Sstevel@tonic-gate 	}
25157c478bd9Sstevel@tonic-gate }
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate /*
25187c478bd9Sstevel@tonic-gate  * Free up the pre-allocated header and sigq pool if possible.
25197c478bd9Sstevel@tonic-gate  */
25207c478bd9Sstevel@tonic-gate void
sigqhdrfree(sigqhdr_t * sqh)25217c478bd9Sstevel@tonic-gate sigqhdrfree(sigqhdr_t *sqh)
25227c478bd9Sstevel@tonic-gate {
25237c478bd9Sstevel@tonic-gate 	mutex_enter(&sqh->sqb_lock);
25247c478bd9Sstevel@tonic-gate 	if (sqh->sqb_sent == 0) {
25257c478bd9Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
2526f841f6adSraf 		cv_destroy(&sqh->sqb_cv);
25277c478bd9Sstevel@tonic-gate 		mutex_destroy(&sqh->sqb_lock);
25287c478bd9Sstevel@tonic-gate 		kmem_free(sqh, sqh->sqb_size);
25297c478bd9Sstevel@tonic-gate 	} else {
25307c478bd9Sstevel@tonic-gate 		sqh->sqb_pexited = 1;
25317c478bd9Sstevel@tonic-gate 		mutex_exit(&sqh->sqb_lock);
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate }
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate /*
25367c478bd9Sstevel@tonic-gate  * Free up a single sigqueue structure.
25377c478bd9Sstevel@tonic-gate  * No other code should free a sigqueue directly.
25387c478bd9Sstevel@tonic-gate  */
25397c478bd9Sstevel@tonic-gate void
siginfofree(sigqueue_t * sqp)25407c478bd9Sstevel@tonic-gate siginfofree(sigqueue_t *sqp)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	if (sqp != NULL) {
25437c478bd9Sstevel@tonic-gate 		if (sqp->sq_func != NULL)
25447c478bd9Sstevel@tonic-gate 			(sqp->sq_func)(sqp);
25457c478bd9Sstevel@tonic-gate 		else
25467c478bd9Sstevel@tonic-gate 			kmem_free(sqp, sizeof (sigqueue_t));
25477c478bd9Sstevel@tonic-gate 	}
25487c478bd9Sstevel@tonic-gate }
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate /*
25517c478bd9Sstevel@tonic-gate  * Generate a synchronous signal caused by a hardware
25527c478bd9Sstevel@tonic-gate  * condition encountered by an lwp.  Called from trap().
25537c478bd9Sstevel@tonic-gate  */
25547c478bd9Sstevel@tonic-gate void
trapsig(k_siginfo_t * ip,int restartable)25557c478bd9Sstevel@tonic-gate trapsig(k_siginfo_t *ip, int restartable)
25567c478bd9Sstevel@tonic-gate {
25577c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
25587c478bd9Sstevel@tonic-gate 	int sig = ip->si_signo;
25597c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
25607c478bd9Sstevel@tonic-gate 
25617c478bd9Sstevel@tonic-gate 	ASSERT(sig > 0 && sig < NSIG);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	if (curthread->t_dtrace_on)
25647c478bd9Sstevel@tonic-gate 		dtrace_safe_synchronous_signal();
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
25677c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
25687c478bd9Sstevel@tonic-gate 	/*
25697c478bd9Sstevel@tonic-gate 	 * Avoid a possible infinite loop if the lwp is holding the
25707c478bd9Sstevel@tonic-gate 	 * signal generated by a trap of a restartable instruction or
25717c478bd9Sstevel@tonic-gate 	 * if the signal so generated is being ignored by the process.
25727c478bd9Sstevel@tonic-gate 	 */
25737c478bd9Sstevel@tonic-gate 	if (restartable &&
25747c478bd9Sstevel@tonic-gate 	    (sigismember(&curthread->t_hold, sig) ||
25757c478bd9Sstevel@tonic-gate 	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
25767c478bd9Sstevel@tonic-gate 		sigdelset(&curthread->t_hold, sig);
25777c478bd9Sstevel@tonic-gate 		p->p_user.u_signal[sig-1] = SIG_DFL;
25787c478bd9Sstevel@tonic-gate 		sigdelset(&p->p_ignore, sig);
25797c478bd9Sstevel@tonic-gate 	}
25807c478bd9Sstevel@tonic-gate 	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
25817c478bd9Sstevel@tonic-gate 	sigaddqa(p, curthread, sqp);
25827c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
25837c478bd9Sstevel@tonic-gate }
25847c478bd9Sstevel@tonic-gate 
2585e0cf54a5SRoger A. Faulkner /*
25867be238fcSRoger A. Faulkner  * Dispatch the real time profiling signal in the traditional way,
25877be238fcSRoger A. Faulkner  * honoring all of the /proc tracing mechanism built into issig().
2588e0cf54a5SRoger A. Faulkner  */
25897be238fcSRoger A. Faulkner static void
realsigprof_slow(int sysnum,int nsysarg,int error)25907be238fcSRoger A. Faulkner realsigprof_slow(int sysnum, int nsysarg, int error)
2591e0cf54a5SRoger A. Faulkner {
25927be238fcSRoger A. Faulkner 	kthread_t *t = curthread;
25937be238fcSRoger A. Faulkner 	proc_t *p = ttoproc(t);
25947be238fcSRoger A. Faulkner 	klwp_t *lwp = ttolwp(t);
25957be238fcSRoger A. Faulkner 	k_siginfo_t *sip = &lwp->lwp_siginfo;
25967be238fcSRoger A. Faulkner 	void (*func)();
2597e0cf54a5SRoger A. Faulkner 
2598e0cf54a5SRoger A. Faulkner 	mutex_enter(&p->p_lock);
25997be238fcSRoger A. Faulkner 	func = PTOU(p)->u_signal[SIGPROF - 1];
26007be238fcSRoger A. Faulkner 	if (p->p_rprof_cyclic == CYCLIC_NONE ||
26017be238fcSRoger A. Faulkner 	    func == SIG_DFL || func == SIG_IGN) {
26027be238fcSRoger A. Faulkner 		bzero(t->t_rprof, sizeof (*t->t_rprof));
2603e0cf54a5SRoger A. Faulkner 		mutex_exit(&p->p_lock);
2604e0cf54a5SRoger A. Faulkner 		return;
2605e0cf54a5SRoger A. Faulkner 	}
26067be238fcSRoger A. Faulkner 	if (sigismember(&t->t_hold, SIGPROF)) {
2607e0cf54a5SRoger A. Faulkner 		mutex_exit(&p->p_lock);
2608e0cf54a5SRoger A. Faulkner 		return;
2609e0cf54a5SRoger A. Faulkner 	}
26107be238fcSRoger A. Faulkner 	sip->si_signo = SIGPROF;
26117be238fcSRoger A. Faulkner 	sip->si_code = PROF_SIG;
26127be238fcSRoger A. Faulkner 	sip->si_errno = error;
26137be238fcSRoger A. Faulkner 	hrt2ts(gethrtime(), &sip->si_tstamp);
26147be238fcSRoger A. Faulkner 	sip->si_syscall = sysnum;
26157be238fcSRoger A. Faulkner 	sip->si_nsysarg = nsysarg;
26167be238fcSRoger A. Faulkner 	sip->si_fault = lwp->lwp_lastfault;
26177be238fcSRoger A. Faulkner 	sip->si_faddr = lwp->lwp_lastfaddr;
2618e0cf54a5SRoger A. Faulkner 	lwp->lwp_lastfault = 0;
2619e0cf54a5SRoger A. Faulkner 	lwp->lwp_lastfaddr = NULL;
26207be238fcSRoger A. Faulkner 	sigtoproc(p, t, SIGPROF);
2621e0cf54a5SRoger A. Faulkner 	mutex_exit(&p->p_lock);
2622e0cf54a5SRoger A. Faulkner 	ASSERT(lwp->lwp_cursig == 0);
2623e0cf54a5SRoger A. Faulkner 	if (issig(FORREAL))
2624e0cf54a5SRoger A. Faulkner 		psig();
26257be238fcSRoger A. Faulkner 	sip->si_signo = 0;
26267be238fcSRoger A. Faulkner 	bzero(t->t_rprof, sizeof (*t->t_rprof));
26277be238fcSRoger A. Faulkner }
26287be238fcSRoger A. Faulkner 
26297be238fcSRoger A. Faulkner /*
26307be238fcSRoger A. Faulkner  * We are not tracing the SIGPROF signal, or doing any other unnatural
26317be238fcSRoger A. Faulkner  * acts, like watchpoints, so dispatch the real time profiling signal
26327be238fcSRoger A. Faulkner  * directly, bypassing all of the overhead built into issig().
26337be238fcSRoger A. Faulkner  */
26347be238fcSRoger A. Faulkner static void
realsigprof_fast(int sysnum,int nsysarg,int error)26357be238fcSRoger A. Faulkner realsigprof_fast(int sysnum, int nsysarg, int error)
26367be238fcSRoger A. Faulkner {
26377be238fcSRoger A. Faulkner 	kthread_t *t = curthread;
26387be238fcSRoger A. Faulkner 	proc_t *p = ttoproc(t);
26397be238fcSRoger A. Faulkner 	klwp_t *lwp = ttolwp(t);
26407be238fcSRoger A. Faulkner 	k_siginfo_t *sip = &lwp->lwp_siginfo;
26417be238fcSRoger A. Faulkner 	void (*func)();
26427be238fcSRoger A. Faulkner 	int rc;
26437be238fcSRoger A. Faulkner 	int code;
26447be238fcSRoger A. Faulkner 
26457be238fcSRoger A. Faulkner 	/*
26467be238fcSRoger A. Faulkner 	 * We don't need to acquire p->p_lock here;
26477be238fcSRoger A. Faulkner 	 * we are manipulating thread-private data.
26487be238fcSRoger A. Faulkner 	 */
26497be238fcSRoger A. Faulkner 	func = PTOU(p)->u_signal[SIGPROF - 1];
26507be238fcSRoger A. Faulkner 	if (p->p_rprof_cyclic == CYCLIC_NONE ||
26517be238fcSRoger A. Faulkner 	    func == SIG_DFL || func == SIG_IGN) {
26527be238fcSRoger A. Faulkner 		bzero(t->t_rprof, sizeof (*t->t_rprof));
26537be238fcSRoger A. Faulkner 		return;
26547be238fcSRoger A. Faulkner 	}
26557be238fcSRoger A. Faulkner 	if (lwp->lwp_cursig != 0 ||
26567be238fcSRoger A. Faulkner 	    lwp->lwp_curinfo != NULL ||
26577be238fcSRoger A. Faulkner 	    sigismember(&t->t_hold, SIGPROF)) {
26587be238fcSRoger A. Faulkner 		return;
26597be238fcSRoger A. Faulkner 	}
26607be238fcSRoger A. Faulkner 	sip->si_signo = SIGPROF;
26617be238fcSRoger A. Faulkner 	sip->si_code = PROF_SIG;
26627be238fcSRoger A. Faulkner 	sip->si_errno = error;
26637be238fcSRoger A. Faulkner 	hrt2ts(gethrtime(), &sip->si_tstamp);
26647be238fcSRoger A. Faulkner 	sip->si_syscall = sysnum;
26657be238fcSRoger A. Faulkner 	sip->si_nsysarg = nsysarg;
26667be238fcSRoger A. Faulkner 	sip->si_fault = lwp->lwp_lastfault;
26677be238fcSRoger A. Faulkner 	sip->si_faddr = lwp->lwp_lastfaddr;
26687be238fcSRoger A. Faulkner 	lwp->lwp_lastfault = 0;
26697be238fcSRoger A. Faulkner 	lwp->lwp_lastfaddr = NULL;
26707be238fcSRoger A. Faulkner 	if (t->t_flag & T_TOMASK)
26717be238fcSRoger A. Faulkner 		t->t_flag &= ~T_TOMASK;
26727be238fcSRoger A. Faulkner 	else
26737be238fcSRoger A. Faulkner 		lwp->lwp_sigoldmask = t->t_hold;
26747be238fcSRoger A. Faulkner 	sigorset(&t->t_hold, &PTOU(p)->u_sigmask[SIGPROF - 1]);
26757be238fcSRoger A. Faulkner 	if (!sigismember(&PTOU(p)->u_signodefer, SIGPROF))
26767be238fcSRoger A. Faulkner 		sigaddset(&t->t_hold, SIGPROF);
26777be238fcSRoger A. Faulkner 	lwp->lwp_extsig = 0;
26787be238fcSRoger A. Faulkner 	lwp->lwp_ru.nsignals++;
26797be238fcSRoger A. Faulkner 	if (p->p_model == DATAMODEL_NATIVE)
26807be238fcSRoger A. Faulkner 		rc = sendsig(SIGPROF, sip, func);
26817be238fcSRoger A. Faulkner #ifdef _SYSCALL32_IMPL
26827be238fcSRoger A. Faulkner 	else
26837be238fcSRoger A. Faulkner 		rc = sendsig32(SIGPROF, sip, func);
26847be238fcSRoger A. Faulkner #endif	/* _SYSCALL32_IMPL */
26857be238fcSRoger A. Faulkner 	sip->si_signo = 0;
26867be238fcSRoger A. Faulkner 	bzero(t->t_rprof, sizeof (*t->t_rprof));
26877be238fcSRoger A. Faulkner 	if (rc == 0) {
26887be238fcSRoger A. Faulkner 		/*
26897be238fcSRoger A. Faulkner 		 * sendsig() failed; we must dump core with a SIGSEGV.
26907be238fcSRoger A. Faulkner 		 * See psig().  This code is copied from there.
26917be238fcSRoger A. Faulkner 		 */
26927be238fcSRoger A. Faulkner 		lwp->lwp_cursig = SIGSEGV;
26937be238fcSRoger A. Faulkner 		code = CLD_KILLED;
26947be238fcSRoger A. Faulkner 		proc_is_exiting(p);
26957be238fcSRoger A. Faulkner 		if (exitlwps(1) != 0) {
26967be238fcSRoger A. Faulkner 			mutex_enter(&p->p_lock);
26977be238fcSRoger A. Faulkner 			lwp_exit();
26987be238fcSRoger A. Faulkner 		}
2699005d3febSMarek Pospisil 		if (audit_active == C2AUDIT_LOADED)
27007be238fcSRoger A. Faulkner 			audit_core_start(SIGSEGV);
27017be238fcSRoger A. Faulkner 		if (core(SIGSEGV, 0) == 0)
27027be238fcSRoger A. Faulkner 			code = CLD_DUMPED;
2703005d3febSMarek Pospisil 		if (audit_active == C2AUDIT_LOADED)
27047be238fcSRoger A. Faulkner 			audit_core_finish(code);
27057be238fcSRoger A. Faulkner 		exit(code, SIGSEGV);
27067be238fcSRoger A. Faulkner 	}
27077be238fcSRoger A. Faulkner }
27087be238fcSRoger A. Faulkner 
27097be238fcSRoger A. Faulkner /*
27107be238fcSRoger A. Faulkner  * Arrange for the real time profiling signal to be dispatched.
27117be238fcSRoger A. Faulkner  */
27127be238fcSRoger A. Faulkner void
realsigprof(int sysnum,int nsysarg,int error)27137be238fcSRoger A. Faulkner realsigprof(int sysnum, int nsysarg, int error)
27147be238fcSRoger A. Faulkner {
27157be238fcSRoger A. Faulkner 	kthread_t *t = curthread;
27167be238fcSRoger A. Faulkner 	proc_t *p = ttoproc(t);
27177be238fcSRoger A. Faulkner 
27187be238fcSRoger A. Faulkner 	if (t->t_rprof->rp_anystate == 0)
27197be238fcSRoger A. Faulkner 		return;
27207be238fcSRoger A. Faulkner 
27217be238fcSRoger A. Faulkner 	schedctl_finish_sigblock(t);
27227be238fcSRoger A. Faulkner 
27237be238fcSRoger A. Faulkner 	/* test for any activity that requires p->p_lock */
27247be238fcSRoger A. Faulkner 	if (tracing(p, SIGPROF) || pr_watch_active(p) ||
27257be238fcSRoger A. Faulkner 	    sigismember(&PTOU(p)->u_sigresethand, SIGPROF)) {
27267be238fcSRoger A. Faulkner 		/* do it the classic slow way */
27277be238fcSRoger A. Faulkner 		realsigprof_slow(sysnum, nsysarg, error);
27287be238fcSRoger A. Faulkner 	} else {
27297be238fcSRoger A. Faulkner 		/* do it the cheating-a-little fast way */
27307be238fcSRoger A. Faulkner 		realsigprof_fast(sysnum, nsysarg, error);
27317be238fcSRoger A. Faulkner 	}
2732e0cf54a5SRoger A. Faulkner }
2733e0cf54a5SRoger A. Faulkner 
27347c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate /*
27377c478bd9Sstevel@tonic-gate  * It's tricky to transmit a sigval between 32-bit and 64-bit
27387c478bd9Sstevel@tonic-gate  * process, since in the 64-bit world, a pointer and an integer
27397c478bd9Sstevel@tonic-gate  * are different sizes.  Since we're constrained by the standards
27407c478bd9Sstevel@tonic-gate  * world not to change the types, and it's unclear how useful it is
27417c478bd9Sstevel@tonic-gate  * to send pointers between address spaces this way, we preserve
27427c478bd9Sstevel@tonic-gate  * the 'int' interpretation for 32-bit processes interoperating
27437c478bd9Sstevel@tonic-gate  * with 64-bit processes.  The full semantics (pointers or integers)
27447c478bd9Sstevel@tonic-gate  * are available for N-bit processes interoperating with N-bit
27457c478bd9Sstevel@tonic-gate  * processes.
27467c478bd9Sstevel@tonic-gate  */
27477c478bd9Sstevel@tonic-gate void
siginfo_kto32(const k_siginfo_t * src,siginfo32_t * dest)27487c478bd9Sstevel@tonic-gate siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
27497c478bd9Sstevel@tonic-gate {
27507c478bd9Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	/*
27537c478bd9Sstevel@tonic-gate 	 * The absolute minimum content is si_signo and si_code.
27547c478bd9Sstevel@tonic-gate 	 */
27557c478bd9Sstevel@tonic-gate 	dest->si_signo = src->si_signo;
27567c478bd9Sstevel@tonic-gate 	if ((dest->si_code = src->si_code) == SI_NOINFO)
27577c478bd9Sstevel@tonic-gate 		return;
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 	/*
27607c478bd9Sstevel@tonic-gate 	 * A siginfo generated by user level is structured
27617c478bd9Sstevel@tonic-gate 	 * differently from one generated by the kernel.
27627c478bd9Sstevel@tonic-gate 	 */
27637c478bd9Sstevel@tonic-gate 	if (SI_FROMUSER(src)) {
27647c478bd9Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
27657c478bd9Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
27667c478bd9Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
27677c478bd9Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
27687c478bd9Sstevel@tonic-gate 		if (SI_CANQUEUE(src->si_code))
27697c478bd9Sstevel@tonic-gate 			dest->si_value.sival_int =
2770cbdce38dSbpramod 			    (int32_t)src->si_value.sival_int;
27717c478bd9Sstevel@tonic-gate 		return;
27727c478bd9Sstevel@tonic-gate 	}
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	dest->si_errno = src->si_errno;
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	switch (src->si_signo) {
27777c478bd9Sstevel@tonic-gate 	default:
27787c478bd9Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
27797c478bd9Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
27807c478bd9Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
27817c478bd9Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
27827c478bd9Sstevel@tonic-gate 		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
27837c478bd9Sstevel@tonic-gate 		break;
27847c478bd9Sstevel@tonic-gate 	case SIGCLD:
27857c478bd9Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
27867c478bd9Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
27877c478bd9Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
27887c478bd9Sstevel@tonic-gate 		dest->si_status = src->si_status;
27897c478bd9Sstevel@tonic-gate 		dest->si_stime = src->si_stime;
27907c478bd9Sstevel@tonic-gate 		dest->si_utime = src->si_utime;
27917c478bd9Sstevel@tonic-gate 		break;
27927c478bd9Sstevel@tonic-gate 	case SIGSEGV:
27937c478bd9Sstevel@tonic-gate 	case SIGBUS:
27947c478bd9Sstevel@tonic-gate 	case SIGILL:
27957c478bd9Sstevel@tonic-gate 	case SIGTRAP:
27967c478bd9Sstevel@tonic-gate 	case SIGFPE:
27977c478bd9Sstevel@tonic-gate 	case SIGEMT:
27987c478bd9Sstevel@tonic-gate 		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
27997c478bd9Sstevel@tonic-gate 		dest->si_trapno = src->si_trapno;
28007c478bd9Sstevel@tonic-gate 		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
28017c478bd9Sstevel@tonic-gate 		break;
28027c478bd9Sstevel@tonic-gate 	case SIGPOLL:
28037c478bd9Sstevel@tonic-gate 	case SIGXFSZ:
28047c478bd9Sstevel@tonic-gate 		dest->si_fd = src->si_fd;
28057c478bd9Sstevel@tonic-gate 		dest->si_band = src->si_band;
28067c478bd9Sstevel@tonic-gate 		break;
28077c478bd9Sstevel@tonic-gate 	case SIGPROF:
28087c478bd9Sstevel@tonic-gate 		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
28097c478bd9Sstevel@tonic-gate 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
28107c478bd9Sstevel@tonic-gate 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
28117c478bd9Sstevel@tonic-gate 		dest->si_syscall = src->si_syscall;
28127c478bd9Sstevel@tonic-gate 		dest->si_nsysarg = src->si_nsysarg;
28137c478bd9Sstevel@tonic-gate 		dest->si_fault = src->si_fault;
28147c478bd9Sstevel@tonic-gate 		break;
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate }
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate void
siginfo_32tok(const siginfo32_t * src,k_siginfo_t * dest)28197c478bd9Sstevel@tonic-gate siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
28207c478bd9Sstevel@tonic-gate {
28217c478bd9Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	/*
28247c478bd9Sstevel@tonic-gate 	 * The absolute minimum content is si_signo and si_code.
28257c478bd9Sstevel@tonic-gate 	 */
28267c478bd9Sstevel@tonic-gate 	dest->si_signo = src->si_signo;
28277c478bd9Sstevel@tonic-gate 	if ((dest->si_code = src->si_code) == SI_NOINFO)
28287c478bd9Sstevel@tonic-gate 		return;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	/*
28317c478bd9Sstevel@tonic-gate 	 * A siginfo generated by user level is structured
28327c478bd9Sstevel@tonic-gate 	 * differently from one generated by the kernel.
28337c478bd9Sstevel@tonic-gate 	 */
28347c478bd9Sstevel@tonic-gate 	if (SI_FROMUSER(src)) {
28357c478bd9Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
28367c478bd9Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
28377c478bd9Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
28387c478bd9Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
28397c478bd9Sstevel@tonic-gate 		if (SI_CANQUEUE(src->si_code))
28407c478bd9Sstevel@tonic-gate 			dest->si_value.sival_int =
2841cbdce38dSbpramod 			    (int)src->si_value.sival_int;
28427c478bd9Sstevel@tonic-gate 		return;
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	dest->si_errno = src->si_errno;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	switch (src->si_signo) {
28487c478bd9Sstevel@tonic-gate 	default:
28497c478bd9Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
28507c478bd9Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
28517c478bd9Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
28527c478bd9Sstevel@tonic-gate 		dest->si_uid = src->si_uid;
28537c478bd9Sstevel@tonic-gate 		dest->si_value.sival_int = (int)src->si_value.sival_int;
28547c478bd9Sstevel@tonic-gate 		break;
28557c478bd9Sstevel@tonic-gate 	case SIGCLD:
28567c478bd9Sstevel@tonic-gate 		dest->si_pid = src->si_pid;
28577c478bd9Sstevel@tonic-gate 		dest->si_ctid = src->si_ctid;
28587c478bd9Sstevel@tonic-gate 		dest->si_zoneid = src->si_zoneid;
28597c478bd9Sstevel@tonic-gate 		dest->si_status = src->si_status;
28607c478bd9Sstevel@tonic-gate 		dest->si_stime = src->si_stime;
28617c478bd9Sstevel@tonic-gate 		dest->si_utime = src->si_utime;
28627c478bd9Sstevel@tonic-gate 		break;
28637c478bd9Sstevel@tonic-gate 	case SIGSEGV:
28647c478bd9Sstevel@tonic-gate 	case SIGBUS:
28657c478bd9Sstevel@tonic-gate 	case SIGILL:
28667c478bd9Sstevel@tonic-gate 	case SIGTRAP:
28677c478bd9Sstevel@tonic-gate 	case SIGFPE:
28687c478bd9Sstevel@tonic-gate 	case SIGEMT:
28697c478bd9Sstevel@tonic-gate 		dest->si_addr = (void *)(uintptr_t)src->si_addr;
28707c478bd9Sstevel@tonic-gate 		dest->si_trapno = src->si_trapno;
28717c478bd9Sstevel@tonic-gate 		dest->si_pc = (void *)(uintptr_t)src->si_pc;
28727c478bd9Sstevel@tonic-gate 		break;
28737c478bd9Sstevel@tonic-gate 	case SIGPOLL:
28747c478bd9Sstevel@tonic-gate 	case SIGXFSZ:
28757c478bd9Sstevel@tonic-gate 		dest->si_fd = src->si_fd;
28767c478bd9Sstevel@tonic-gate 		dest->si_band = src->si_band;
28777c478bd9Sstevel@tonic-gate 		break;
28787c478bd9Sstevel@tonic-gate 	case SIGPROF:
28797c478bd9Sstevel@tonic-gate 		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
28807c478bd9Sstevel@tonic-gate 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
28817c478bd9Sstevel@tonic-gate 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
28827c478bd9Sstevel@tonic-gate 		dest->si_syscall = src->si_syscall;
28837c478bd9Sstevel@tonic-gate 		dest->si_nsysarg = src->si_nsysarg;
28847c478bd9Sstevel@tonic-gate 		dest->si_fault = src->si_fault;
28857c478bd9Sstevel@tonic-gate 		break;
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate }
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2890