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