xref: /titanic_41/usr/src/uts/common/os/sig.c (revision 653d2d91a355b2a5df7db09e17c965ddc5d3ea47)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/bitmap.h>
34 #include <sys/sysmacros.h>
35 #include <sys/systm.h>
36 #include <sys/cred.h>
37 #include <sys/user.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/poll_impl.h> /* only needed for kludge in sigwaiting_send() */
41 #include <sys/signal.h>
42 #include <sys/siginfo.h>
43 #include <sys/fault.h>
44 #include <sys/ucontext.h>
45 #include <sys/procfs.h>
46 #include <sys/wait.h>
47 #include <sys/class.h>
48 #include <sys/mman.h>
49 #include <sys/procset.h>
50 #include <sys/kmem.h>
51 #include <sys/cpuvar.h>
52 #include <sys/prsystm.h>
53 #include <sys/debug.h>
54 #include <vm/as.h>
55 #include <sys/bitmap.h>
56 #include <c2/audit.h>
57 #include <sys/core.h>
58 #include <sys/schedctl.h>
59 #include <sys/contract/process_impl.h>
60 #include <sys/cyclic.h>
61 #include <sys/dtrace.h>
62 #include <sys/sdt.h>
63 
64 const k_sigset_t nullsmask = {0, 0, 0};
65 
66 const k_sigset_t fillset =	/* MUST be contiguous */
67 	{FILLSET0, FILLSET1, FILLSET2};
68 
69 const k_sigset_t cantmask =
70 	{CANTMASK0, CANTMASK1, CANTMASK2};
71 
72 const k_sigset_t cantreset =
73 	{(sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGPWR)), 0, 0};
74 
75 const k_sigset_t ignoredefault =
76 	{(sigmask(SIGCONT)|sigmask(SIGCLD)|sigmask(SIGPWR)
77 	|sigmask(SIGWINCH)|sigmask(SIGURG)|sigmask(SIGWAITING)),
78 	(sigmask(SIGLWP)|sigmask(SIGCANCEL)|sigmask(SIGFREEZE)
79 	|sigmask(SIGTHAW)|sigmask(SIGXRES)|sigmask(SIGJVM1)
80 	|sigmask(SIGJVM2)|sigmask(SIGINFO)), 0};
81 
82 const k_sigset_t stopdefault =
83 	{(sigmask(SIGSTOP)|sigmask(SIGTSTP)|sigmask(SIGTTOU)|sigmask(SIGTTIN)),
84 	0, 0};
85 
86 const k_sigset_t coredefault =
87 	{(sigmask(SIGQUIT)|sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGIOT)
88 	|sigmask(SIGEMT)|sigmask(SIGFPE)|sigmask(SIGBUS)|sigmask(SIGSEGV)
89 	|sigmask(SIGSYS)|sigmask(SIGXCPU)|sigmask(SIGXFSZ)), 0, 0};
90 
91 const k_sigset_t holdvfork =
92 	{(sigmask(SIGTTOU)|sigmask(SIGTTIN)|sigmask(SIGTSTP)), 0, 0};
93 
94 static	int	isjobstop(int);
95 static	void	post_sigcld(proc_t *, sigqueue_t *);
96 
97 /*
98  * Internal variables for counting number of user thread stop requests posted.
99  * They may not be accurate at some special situation such as that a virtually
100  * stopped thread starts to run.
101  */
102 static int num_utstop;
103 /*
104  * Internal variables for broadcasting an event when all thread stop requests
105  * are processed.
106  */
107 static kcondvar_t utstop_cv;
108 
109 static kmutex_t thread_stop_lock;
110 void del_one_utstop(void);
111 
112 /*
113  * Send the specified signal to the specified process.
114  */
115 void
116 psignal(proc_t *p, int sig)
117 {
118 	mutex_enter(&p->p_lock);
119 	sigtoproc(p, NULL, sig);
120 	mutex_exit(&p->p_lock);
121 }
122 
123 /*
124  * Send the specified signal to the specified thread.
125  */
126 void
127 tsignal(kthread_t *t, int sig)
128 {
129 	proc_t *p = ttoproc(t);
130 
131 	mutex_enter(&p->p_lock);
132 	sigtoproc(p, t, sig);
133 	mutex_exit(&p->p_lock);
134 }
135 
136 int
137 signal_is_blocked(kthread_t *t, int sig)
138 {
139 	return (sigismember(&t->t_hold, sig) ||
140 	    (schedctl_sigblock(t) && !sigismember(&cantmask, sig)));
141 }
142 
143 /*
144  * Return true if the signal can safely be discarded on generation.
145  * That is, if there is no need for the signal on the receiving end.
146  * The answer is true if the process is a zombie or
147  * if all of these conditions are true:
148  *	the signal is being ignored
149  *	the process is single-threaded
150  *	the signal is not being traced by /proc
151  * 	the signal is not blocked by the process
152  *	the signal is not being accepted via sigwait()
153  */
154 static int
155 sig_discardable(proc_t *p, int sig)
156 {
157 	kthread_t *t = p->p_tlist;
158 
159 	return (t == NULL ||		/* if zombie or ... */
160 	    (sigismember(&p->p_ignore, sig) &&	/* signal is ignored */
161 	    t->t_forw == t &&			/* and single-threaded */
162 	    !tracing(p, sig) &&			/* and no /proc tracing */
163 	    !signal_is_blocked(t, sig) &&	/* and signal not blocked */
164 	    !sigismember(&t->t_sigwait, sig)));	/* and not being accepted */
165 }
166 
167 /*
168  * Return true if this thread is going to eat this signal soon.
169  * Note that, if the signal is SIGKILL, we force stopped threads to be
170  * set running (to make SIGKILL be a sure kill), but only if the process
171  * is not currently locked by /proc (the P_PR_LOCK flag).  Code in /proc
172  * relies on the fact that a process will not change shape while P_PR_LOCK
173  * is set (it drops and reacquires p->p_lock while leaving P_PR_LOCK set).
174  * We wish that we could simply call prbarrier() below, in sigtoproc(), to
175  * ensure that the process is not locked by /proc, but prbarrier() drops
176  * and reacquires p->p_lock and dropping p->p_lock here would be damaging.
177  */
178 int
179 eat_signal(kthread_t *t, int sig)
180 {
181 	int rval = 0;
182 	ASSERT(THREAD_LOCK_HELD(t));
183 
184 	/*
185 	 * Do not do anything if the target thread has the signal blocked.
186 	 */
187 	if (!signal_is_blocked(t, sig)) {
188 		t->t_sig_check = 1;	/* have thread do an issig */
189 		if (ISWAKEABLE(t) || ISWAITING(t)) {
190 			setrun_locked(t);
191 			rval = 1;
192 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL &&
193 		    !(ttoproc(t)->p_proc_flag & P_PR_LOCK)) {
194 			ttoproc(t)->p_stopsig = 0;
195 			t->t_dtrace_stop = 0;
196 			t->t_schedflag |= TS_XSTART | TS_PSTART;
197 			setrun_locked(t);
198 		} else if (t != curthread && t->t_state == TS_ONPROC) {
199 			aston(t);	/* make it do issig promptly */
200 			if (t->t_cpu != CPU)
201 				poke_cpu(t->t_cpu->cpu_id);
202 			rval = 1;
203 		} else if (t->t_state == TS_RUN) {
204 			rval = 1;
205 		}
206 	}
207 
208 	return (rval);
209 }
210 
211 /*
212  * Post a signal.
213  * If a non-null thread pointer is passed, then post the signal
214  * to the thread/lwp, otherwise post the signal to the process.
215  */
216 void
217 sigtoproc(proc_t *p, kthread_t *t, int sig)
218 {
219 	kthread_t *tt;
220 	int ext = !(curproc->p_flag & SSYS) &&
221 	    (curproc->p_ct_process != p->p_ct_process);
222 
223 	ASSERT(MUTEX_HELD(&p->p_lock));
224 
225 	/* System processes don't get signals */
226 	if (sig <= 0 || sig >= NSIG || (p->p_flag & SSYS))
227 		return;
228 
229 	/*
230 	 * Regardless of origin or directedness,
231 	 * SIGKILL kills all lwps in the process immediately
232 	 * and jobcontrol signals affect all lwps in the process.
233 	 */
234 	if (sig == SIGKILL) {
235 		p->p_flag |= SKILLED | (ext ? SEXTKILLED : 0);
236 		t = NULL;
237 	} else if (sig == SIGCONT) {
238 		/*
239 		 * The SSCONT flag will remain set until a stopping
240 		 * signal comes in (below).  This is harmless.
241 		 */
242 		p->p_flag |= SSCONT;
243 		sigdelq(p, NULL, SIGSTOP);
244 		sigdelq(p, NULL, SIGTSTP);
245 		sigdelq(p, NULL, SIGTTOU);
246 		sigdelq(p, NULL, SIGTTIN);
247 		sigdiffset(&p->p_sig, &stopdefault);
248 		sigdiffset(&p->p_extsig, &stopdefault);
249 		p->p_stopsig = 0;
250 		if ((tt = p->p_tlist) != NULL) {
251 			do {
252 				sigdelq(p, tt, SIGSTOP);
253 				sigdelq(p, tt, SIGTSTP);
254 				sigdelq(p, tt, SIGTTOU);
255 				sigdelq(p, tt, SIGTTIN);
256 				sigdiffset(&tt->t_sig, &stopdefault);
257 				sigdiffset(&tt->t_extsig, &stopdefault);
258 			} while ((tt = tt->t_forw) != p->p_tlist);
259 		}
260 		if ((tt = p->p_tlist) != NULL) {
261 			do {
262 				thread_lock(tt);
263 				if (tt->t_state == TS_STOPPED &&
264 				    tt->t_whystop == PR_JOBCONTROL) {
265 					tt->t_schedflag |= TS_XSTART;
266 					setrun_locked(tt);
267 				}
268 				thread_unlock(tt);
269 			} while ((tt = tt->t_forw) != p->p_tlist);
270 		}
271 	} else if (sigismember(&stopdefault, sig)) {
272 		/*
273 		 * This test has a race condition which we can't fix:
274 		 * By the time the stopping signal is received by
275 		 * the target process/thread, the signal handler
276 		 * and/or the detached state might have changed.
277 		 */
278 		if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
279 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
280 			p->p_flag &= ~SSCONT;
281 		sigdelq(p, NULL, SIGCONT);
282 		sigdelset(&p->p_sig, SIGCONT);
283 		sigdelset(&p->p_extsig, SIGCONT);
284 		if ((tt = p->p_tlist) != NULL) {
285 			do {
286 				sigdelq(p, tt, SIGCONT);
287 				sigdelset(&tt->t_sig, SIGCONT);
288 				sigdelset(&tt->t_extsig, SIGCONT);
289 			} while ((tt = tt->t_forw) != p->p_tlist);
290 		}
291 	}
292 
293 	if (sig_discardable(p, sig)) {
294 		DTRACE_PROC3(signal__discard, kthread_t *, p->p_tlist,
295 		    proc_t *, p, int, sig);
296 		return;
297 	}
298 
299 	if (t != NULL) {
300 		/*
301 		 * This is a directed signal, wake up the lwp.
302 		 */
303 		sigaddset(&t->t_sig, sig);
304 		if (ext)
305 			sigaddset(&t->t_extsig, sig);
306 		thread_lock(t);
307 		(void) eat_signal(t, sig);
308 		thread_unlock(t);
309 		DTRACE_PROC2(signal__send, kthread_t *, t, int, sig);
310 	} else if ((tt = p->p_tlist) != NULL) {
311 		/*
312 		 * Make sure that some lwp that already exists
313 		 * in the process fields the signal soon.
314 		 * Wake up an interruptibly sleeping lwp if necessary.
315 		 * For SIGKILL make all of the lwps see the signal;
316 		 * This is needed to guarantee a sure kill for processes
317 		 * with a mix of realtime and non-realtime threads.
318 		 */
319 		int su = 0;
320 
321 		sigaddset(&p->p_sig, sig);
322 		if (ext)
323 			sigaddset(&p->p_extsig, sig);
324 		do {
325 			thread_lock(tt);
326 			if (eat_signal(tt, sig) && sig != SIGKILL) {
327 				thread_unlock(tt);
328 				break;
329 			}
330 			if (SUSPENDED(tt))
331 				su++;
332 			thread_unlock(tt);
333 		} while ((tt = tt->t_forw) != p->p_tlist);
334 		/*
335 		 * If the process is deadlocked, make somebody run and die.
336 		 */
337 		if (sig == SIGKILL && p->p_stat != SIDL &&
338 		    p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
339 		    !(p->p_proc_flag & P_PR_LOCK)) {
340 			thread_lock(tt);
341 			p->p_lwprcnt++;
342 			tt->t_schedflag |= TS_CSTART;
343 			setrun_locked(tt);
344 			thread_unlock(tt);
345 		}
346 
347 		DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
348 	}
349 }
350 
351 static int
352 isjobstop(int sig)
353 {
354 	proc_t *p = ttoproc(curthread);
355 
356 	ASSERT(MUTEX_HELD(&p->p_lock));
357 
358 	if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
359 	    sigismember(&stopdefault, sig)) {
360 		/*
361 		 * If SIGCONT has been posted since we promoted this signal
362 		 * from pending to current, then don't do a jobcontrol stop.
363 		 */
364 		if (!(p->p_flag & SSCONT) &&
365 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
366 		    curthread != p->p_agenttp) {
367 			sigqueue_t *sqp;
368 
369 			stop(PR_JOBCONTROL, sig);
370 			mutex_exit(&p->p_lock);
371 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
372 			mutex_enter(&pidlock);
373 			/*
374 			 * Only the first lwp to continue notifies the parent.
375 			 */
376 			if (p->p_pidflag & CLDCONT)
377 				siginfofree(sqp);
378 			else {
379 				p->p_pidflag |= CLDCONT;
380 				p->p_wcode = CLD_CONTINUED;
381 				p->p_wdata = SIGCONT;
382 				sigcld(p, sqp);
383 			}
384 			mutex_exit(&pidlock);
385 			mutex_enter(&p->p_lock);
386 		}
387 		return (1);
388 	}
389 	return (0);
390 }
391 
392 /*
393  * Returns true if the current process has a signal to process, and
394  * the signal is not held.  The signal to process is put in p_cursig.
395  * This is asked at least once each time a process enters the system
396  * (though this can usually be done without actually calling issig by
397  * checking the pending signal masks).  A signal does not do anything
398  * directly to a process; it sets a flag that asks the process to do
399  * something to itself.
400  *
401  * The "why" argument indicates the allowable side-effects of the call:
402  *
403  * FORREAL:  Extract the next pending signal from p_sig into p_cursig;
404  * stop the process if a stop has been requested or if a traced signal
405  * is pending.
406  *
407  * JUSTLOOKING:  Don't stop the process, just indicate whether or not
408  * a signal might be pending (FORREAL is needed to tell for sure).
409  *
410  * XXX: Changes to the logic in these routines should be propagated
411  * to lm_sigispending().  See bug 1201594.
412  */
413 
414 static int issig_forreal(void);
415 static int issig_justlooking(void);
416 
417 int
418 issig(int why)
419 {
420 	ASSERT(why == FORREAL || why == JUSTLOOKING);
421 
422 	return ((why == FORREAL)? issig_forreal() : issig_justlooking());
423 }
424 
425 
426 static int
427 issig_justlooking(void)
428 {
429 	kthread_t *t = curthread;
430 	klwp_t *lwp = ttolwp(t);
431 	proc_t *p = ttoproc(t);
432 	k_sigset_t set;
433 
434 	/*
435 	 * This function answers the question:
436 	 * "Is there any reason to call issig_forreal()?"
437 	 *
438 	 * We have to answer the question w/o grabbing any locks
439 	 * because we are (most likely) being called after we
440 	 * put ourselves on the sleep queue.
441 	 */
442 
443 	if (t->t_dtrace_stop | t->t_dtrace_sig)
444 		return (1);
445 
446 	/*
447 	 * Another piece of complexity in this process.  When single-stepping a
448 	 * process, we don't want an intervening signal or TP_PAUSE request to
449 	 * suspend the current thread.  Otherwise, the controlling process will
450 	 * hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
451 	 * We will trigger any remaining signals when we re-enter the kernel on
452 	 * the single step trap.
453 	 */
454 	if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
455 		return (0);
456 
457 	if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
458 	    (p->p_flag & (SEXITLWPS|SKILLED)) ||
459 	    (lwp->lwp_nostop == 0 &&
460 	    (p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
461 	    (t->t_proc_flag &
462 	    (TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
463 	    lwp->lwp_cursig)
464 		return (1);
465 
466 	if (p->p_flag & SVFWAIT)
467 		return (0);
468 	set = p->p_sig;
469 	sigorset(&set, &t->t_sig);
470 	if (schedctl_sigblock(t))	/* all blockable signals blocked */
471 		sigandset(&set, &cantmask);
472 	else
473 		sigdiffset(&set, &t->t_hold);
474 	if (p->p_flag & SVFORK)
475 		sigdiffset(&set, &holdvfork);
476 
477 	if (!sigisempty(&set)) {
478 		int sig;
479 
480 		for (sig = 1; sig < NSIG; sig++) {
481 			if (sigismember(&set, sig) &&
482 			    (tracing(p, sig) ||
483 			    sigismember(&t->t_sigwait, sig) ||
484 			    !sigismember(&p->p_ignore, sig))) {
485 				/*
486 				 * Don't promote a signal that will stop
487 				 * the process when lwp_nostop is set.
488 				 */
489 				if (!lwp->lwp_nostop ||
490 				    PTOU(p)->u_signal[sig-1] != SIG_DFL ||
491 				    !sigismember(&stopdefault, sig))
492 					return (1);
493 			}
494 		}
495 	}
496 
497 	return (0);
498 }
499 
500 static int
501 issig_forreal(void)
502 {
503 	int sig = 0, ext = 0;
504 	kthread_t *t = curthread;
505 	klwp_t *lwp = ttolwp(t);
506 	proc_t *p = ttoproc(t);
507 	int toproc = 0;
508 	int sigcld_found = 0;
509 	int nostop_break = 0;
510 
511 	ASSERT(t->t_state == TS_ONPROC);
512 
513 	mutex_enter(&p->p_lock);
514 	schedctl_finish_sigblock(t);
515 
516 	if (t->t_dtrace_stop | t->t_dtrace_sig) {
517 		if (t->t_dtrace_stop) {
518 			/*
519 			 * If DTrace's "stop" action has been invoked on us,
520 			 * set TP_PRSTOP.
521 			 */
522 			t->t_proc_flag |= TP_PRSTOP;
523 		}
524 
525 		if (t->t_dtrace_sig != 0) {
526 			k_siginfo_t info;
527 
528 			/*
529 			 * Post the signal generated as the result of
530 			 * DTrace's "raise" action as a normal signal before
531 			 * the full-fledged signal checking begins.
532 			 */
533 			bzero(&info, sizeof (info));
534 			info.si_signo = t->t_dtrace_sig;
535 			info.si_code = SI_DTRACE;
536 
537 			sigaddq(p, NULL, &info, KM_NOSLEEP);
538 
539 			t->t_dtrace_sig = 0;
540 		}
541 	}
542 
543 	for (;;) {
544 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
545 			lwp->lwp_cursig = sig = SIGKILL;
546 			lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
547 			t->t_sig_check = 1;
548 			break;
549 		}
550 
551 		/*
552 		 * Another piece of complexity in this process.  When
553 		 * single-stepping a process, we don't want an intervening
554 		 * signal or TP_PAUSE request to suspend the current thread.
555 		 * Otherwise, the controlling process will hang beacuse we will
556 		 * be stopped with TS_PSTART set in t_schedflag.  We will
557 		 * trigger any remaining signals when we re-enter the kernel on
558 		 * the single step trap.
559 		 */
560 		if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
561 			sig = 0;
562 			break;
563 		}
564 
565 		/*
566 		 * Hold the lwp here for watchpoint manipulation.
567 		 */
568 		if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
569 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
570 			continue;
571 		}
572 
573 		if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
574 			if ((sig = lwp->lwp_cursig) != 0) {
575 				/*
576 				 * Make sure we call ISSIG() in post_syscall()
577 				 * to re-validate this current signal.
578 				 */
579 				t->t_sig_check = 1;
580 			}
581 			break;
582 		}
583 
584 		/*
585 		 * If the request is PR_CHECKPOINT, ignore the rest of signals
586 		 * or requests.  Honor other stop requests or signals later.
587 		 * Go back to top of loop here to check if an exit or hold
588 		 * event has occurred while stopped.
589 		 */
590 		if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
591 			stop(PR_CHECKPOINT, 0);
592 			continue;
593 		}
594 
595 		/*
596 		 * Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
597 		 * with signals or /proc.  Another lwp is executing fork1(),
598 		 * or is undergoing watchpoint activity (remapping a page),
599 		 * or is executing lwp_suspend() on this lwp.
600 		 * Again, go back to top of loop to check if an exit
601 		 * or hold event has occurred while stopped.
602 		 */
603 		if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
604 		    (t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
605 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
606 			continue;
607 		}
608 
609 		/*
610 		 * Honor requested stop before dealing with the
611 		 * current signal; a debugger may change it.
612 		 * Do not want to go back to loop here since this is a special
613 		 * stop that means: make incremental progress before the next
614 		 * stop. The danger is that returning to top of loop would most
615 		 * likely drop the thread right back here to stop soon after it
616 		 * was continued, violating the incremental progress request.
617 		 */
618 		if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
619 			stop(PR_REQUESTED, 0);
620 
621 		/*
622 		 * If a debugger wants us to take a signal it will have
623 		 * left it in lwp->lwp_cursig.  If lwp_cursig has been cleared
624 		 * or if it's being ignored, we continue on looking for another
625 		 * signal.  Otherwise we return the specified signal, provided
626 		 * it's not a signal that causes a job control stop.
627 		 *
628 		 * When stopped on PR_JOBCONTROL, there is no current
629 		 * signal; we cancel lwp->lwp_cursig temporarily before
630 		 * calling isjobstop().  The current signal may be reset
631 		 * by a debugger while we are stopped in isjobstop().
632 		 *
633 		 * If the current thread is accepting the signal
634 		 * (via sigwait(), sigwaitinfo(), or sigtimedwait()),
635 		 * we allow the signal to be accepted, even if it is
636 		 * being ignored, and without causing a job control stop.
637 		 */
638 		if ((sig = lwp->lwp_cursig) != 0) {
639 			ext = lwp->lwp_extsig;
640 			lwp->lwp_cursig = 0;
641 			lwp->lwp_extsig = 0;
642 			if (sigismember(&t->t_sigwait, sig) ||
643 			    (!sigismember(&p->p_ignore, sig) &&
644 			    !isjobstop(sig))) {
645 				if (p->p_flag & (SEXITLWPS|SKILLED)) {
646 					sig = SIGKILL;
647 					ext = (p->p_flag & SEXTKILLED) != 0;
648 				}
649 				lwp->lwp_cursig = (uchar_t)sig;
650 				lwp->lwp_extsig = (uchar_t)ext;
651 				break;
652 			}
653 			/*
654 			 * The signal is being ignored or it caused a
655 			 * job-control stop.  If another current signal
656 			 * has not been established, return the current
657 			 * siginfo, if any, to the memory manager.
658 			 */
659 			if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
660 				siginfofree(lwp->lwp_curinfo);
661 				lwp->lwp_curinfo = NULL;
662 			}
663 			/*
664 			 * Loop around again in case we were stopped
665 			 * on a job control signal and a /proc stop
666 			 * request was posted or another current signal
667 			 * was established while we were stopped.
668 			 */
669 			continue;
670 		}
671 
672 		if (p->p_stopsig && !lwp->lwp_nostop &&
673 		    curthread != p->p_agenttp) {
674 			/*
675 			 * Some lwp in the process has already stopped
676 			 * showing PR_JOBCONTROL.  This is a stop in
677 			 * sympathy with the other lwp, even if this
678 			 * lwp is blocking the stopping signal.
679 			 */
680 			stop(PR_JOBCONTROL, p->p_stopsig);
681 			continue;
682 		}
683 
684 		/*
685 		 * Loop on the pending signals until we find a
686 		 * non-held signal that is traced or not ignored.
687 		 * First check the signals pending for the lwp,
688 		 * then the signals pending for the process as a whole.
689 		 */
690 		for (;;) {
691 			if ((sig = fsig(&t->t_sig, t)) != 0) {
692 				toproc = 0;
693 				if (tracing(p, sig) ||
694 				    sigismember(&t->t_sigwait, sig) ||
695 				    !sigismember(&p->p_ignore, sig)) {
696 					if (sigismember(&t->t_extsig, sig))
697 						ext = 1;
698 					break;
699 				}
700 				sigdelset(&t->t_sig, sig);
701 				sigdelset(&t->t_extsig, sig);
702 				sigdelq(p, t, sig);
703 			} else if ((sig = fsig(&p->p_sig, t)) != 0) {
704 				if (sig == SIGCLD)
705 					sigcld_found = 1;
706 				toproc = 1;
707 				if (tracing(p, sig) ||
708 				    sigismember(&t->t_sigwait, sig) ||
709 				    !sigismember(&p->p_ignore, sig)) {
710 					if (sigismember(&p->p_extsig, sig))
711 						ext = 1;
712 					break;
713 				}
714 				sigdelset(&p->p_sig, sig);
715 				sigdelset(&p->p_extsig, sig);
716 				sigdelq(p, NULL, sig);
717 			} else {
718 				/* no signal was found */
719 				break;
720 			}
721 		}
722 
723 		if (sig == 0) {	/* no signal was found */
724 			if (p->p_flag & (SEXITLWPS|SKILLED)) {
725 				lwp->lwp_cursig = SIGKILL;
726 				sig = SIGKILL;
727 				ext = (p->p_flag & SEXTKILLED) != 0;
728 			}
729 			break;
730 		}
731 
732 		/*
733 		 * If we have been informed not to stop (i.e., we are being
734 		 * called from within a network operation), then don't promote
735 		 * the signal at this time, just return the signal number.
736 		 * We will call issig() again later when it is safe.
737 		 *
738 		 * fsig() does not return a jobcontrol stopping signal
739 		 * with a default action of stopping the process if
740 		 * lwp_nostop is set, so we won't be causing a bogus
741 		 * EINTR by this action.  (Such a signal is eaten by
742 		 * isjobstop() when we loop around to do final checks.)
743 		 */
744 		if (lwp->lwp_nostop) {
745 			nostop_break = 1;
746 			break;
747 		}
748 
749 		/*
750 		 * Promote the signal from pending to current.
751 		 *
752 		 * Note that sigdeq() will set lwp->lwp_curinfo to NULL
753 		 * if no siginfo_t exists for this signal.
754 		 */
755 		lwp->lwp_cursig = (uchar_t)sig;
756 		lwp->lwp_extsig = (uchar_t)ext;
757 		t->t_sig_check = 1;	/* so post_syscall will see signal */
758 		ASSERT(lwp->lwp_curinfo == NULL);
759 		sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);
760 
761 		if (tracing(p, sig))
762 			stop(PR_SIGNALLED, sig);
763 
764 		/*
765 		 * Loop around to check for requested stop before
766 		 * performing the usual current-signal actions.
767 		 */
768 	}
769 
770 	mutex_exit(&p->p_lock);
771 
772 	/*
773 	 * If SIGCLD was dequeued from the process's signal queue,
774 	 * search for other pending SIGCLD's from the list of children.
775 	 */
776 	if (sigcld_found)
777 		sigcld_repost();
778 
779 	if (sig != 0)
780 		(void) undo_watch_step(NULL);
781 
782 	/*
783 	 * If we have been blocked since the p_lock was dropped off
784 	 * above, then this promoted signal might have been handled
785 	 * already when we were on the way back from sleep queue, so
786 	 * just ignore it.
787 	 * If we have been informed not to stop, just return the signal
788 	 * number. Also see comments above.
789 	 */
790 	if (!nostop_break) {
791 		sig = lwp->lwp_cursig;
792 	}
793 
794 	return (sig != 0);
795 }
796 
797 /*
798  * Return true if the process is currently stopped showing PR_JOBCONTROL.
799  * This is true only if all of the process's lwp's are so stopped.
800  * If this is asked by one of the lwps in the process, exclude that lwp.
801  */
802 int
803 jobstopped(proc_t *p)
804 {
805 	kthread_t *t;
806 
807 	ASSERT(MUTEX_HELD(&p->p_lock));
808 
809 	if ((t = p->p_tlist) == NULL)
810 		return (0);
811 
812 	do {
813 		thread_lock(t);
814 		/* ignore current, zombie and suspended lwps in the test */
815 		if (!(t == curthread || t->t_state == TS_ZOMB ||
816 		    SUSPENDED(t)) &&
817 		    (t->t_state != TS_STOPPED ||
818 		    t->t_whystop != PR_JOBCONTROL)) {
819 			thread_unlock(t);
820 			return (0);
821 		}
822 		thread_unlock(t);
823 	} while ((t = t->t_forw) != p->p_tlist);
824 
825 	return (1);
826 }
827 
828 /*
829  * Put ourself (curthread) into the stopped state and notify tracers.
830  */
831 void
832 stop(int why, int what)
833 {
834 	kthread_t	*t = curthread;
835 	proc_t		*p = ttoproc(t);
836 	klwp_t		*lwp = ttolwp(t);
837 	kthread_t	*tx;
838 	lwpent_t	*lep;
839 	int		procstop;
840 	int		flags = TS_ALLSTART;
841 	hrtime_t	stoptime;
842 
843 	/*
844 	 * Can't stop a system process.
845 	 */
846 	if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
847 		return;
848 
849 	ASSERT(MUTEX_HELD(&p->p_lock));
850 
851 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
852 		/*
853 		 * Don't stop an lwp with SIGKILL pending.
854 		 * Don't stop if the process or lwp is exiting.
855 		 */
856 		if (lwp->lwp_cursig == SIGKILL ||
857 		    sigismember(&t->t_sig, SIGKILL) ||
858 		    sigismember(&p->p_sig, SIGKILL) ||
859 		    (t->t_proc_flag & TP_LWPEXIT) ||
860 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
861 			p->p_stopsig = 0;
862 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
863 			return;
864 		}
865 	}
866 
867 	/*
868 	 * Make sure we don't deadlock on a recursive call to prstop().
869 	 * prstop() sets the lwp_nostop flag.
870 	 */
871 	if (lwp->lwp_nostop)
872 		return;
873 
874 	/*
875 	 * Make sure the lwp is in an orderly state for inspection
876 	 * by a debugger through /proc or for dumping via core().
877 	 */
878 	schedctl_finish_sigblock(t);
879 	t->t_proc_flag |= TP_STOPPING;	/* must set before dropping p_lock */
880 	mutex_exit(&p->p_lock);
881 	stoptime = gethrtime();
882 	prstop(why, what);
883 	(void) undo_watch_step(NULL);
884 	mutex_enter(&p->p_lock);
885 	ASSERT(t->t_state == TS_ONPROC);
886 
887 	switch (why) {
888 	case PR_CHECKPOINT:
889 		/*
890 		 * The situation may have changed since we dropped
891 		 * and reacquired p->p_lock. Double-check now
892 		 * whether we should stop or not.
893 		 */
894 		if (!(t->t_proc_flag & TP_CHKPT)) {
895 			t->t_proc_flag &= ~TP_STOPPING;
896 			return;
897 		}
898 		t->t_proc_flag &= ~TP_CHKPT;
899 		flags &= ~TS_RESUME;
900 		break;
901 
902 	case PR_JOBCONTROL:
903 		ASSERT(what == SIGSTOP || what == SIGTSTP ||
904 		    what == SIGTTIN || what == SIGTTOU);
905 		flags &= ~TS_XSTART;
906 		break;
907 
908 	case PR_SUSPENDED:
909 		ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
910 		/*
911 		 * The situation may have changed since we dropped
912 		 * and reacquired p->p_lock.  Double-check now
913 		 * whether we should stop or not.
914 		 */
915 		if (what == SUSPEND_PAUSE) {
916 			if (!(t->t_proc_flag & TP_PAUSE)) {
917 				t->t_proc_flag &= ~TP_STOPPING;
918 				return;
919 			}
920 			flags &= ~TS_UNPAUSE;
921 		} else {
922 			if (!((t->t_proc_flag & TP_HOLDLWP) ||
923 			    (p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
924 				t->t_proc_flag &= ~TP_STOPPING;
925 				return;
926 			}
927 			/*
928 			 * If SHOLDFORK is in effect and we are stopping
929 			 * while asleep (not at the top of the stack),
930 			 * we return now to allow the hold to take effect
931 			 * when we reach the top of the kernel stack.
932 			 */
933 			if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
934 				t->t_proc_flag &= ~TP_STOPPING;
935 				return;
936 			}
937 			flags &= ~TS_CSTART;
938 		}
939 		break;
940 
941 	default:	/* /proc stop */
942 		flags &= ~TS_PSTART;
943 		/*
944 		 * Do synchronous stop unless the async-stop flag is set.
945 		 * If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
946 		 * then no debugger is present and we also do synchronous stop.
947 		 */
948 		if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
949 		    !(p->p_proc_flag & P_PR_ASYNC)) {
950 			int notify;
951 
952 			for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
953 				notify = 0;
954 				thread_lock(tx);
955 				if (ISTOPPED(tx) ||
956 				    (tx->t_proc_flag & TP_PRSTOP)) {
957 					thread_unlock(tx);
958 					continue;
959 				}
960 				tx->t_proc_flag |= TP_PRSTOP;
961 				tx->t_sig_check = 1;
962 				if (tx->t_state == TS_SLEEP &&
963 				    (tx->t_flag & T_WAKEABLE)) {
964 					/*
965 					 * Don't actually wake it up if it's
966 					 * in one of the lwp_*() syscalls.
967 					 * Mark it virtually stopped and
968 					 * notify /proc waiters (below).
969 					 */
970 					if (tx->t_wchan0 == NULL)
971 						setrun_locked(tx);
972 					else {
973 						tx->t_proc_flag |= TP_PRVSTOP;
974 						tx->t_stoptime = stoptime;
975 						notify = 1;
976 					}
977 				}
978 
979 				/* Move waiting thread to run queue */
980 				if (ISWAITING(tx))
981 					setrun_locked(tx);
982 
983 				/*
984 				 * force the thread into the kernel
985 				 * if it is not already there.
986 				 */
987 				if (tx->t_state == TS_ONPROC &&
988 				    tx->t_cpu != CPU)
989 					poke_cpu(tx->t_cpu->cpu_id);
990 				thread_unlock(tx);
991 				lep = p->p_lwpdir[tx->t_dslot].ld_entry;
992 				if (notify && lep->le_trace)
993 					prnotify(lep->le_trace);
994 			}
995 			/*
996 			 * We do this just in case one of the threads we asked
997 			 * to stop is in holdlwps() (called from cfork()) or
998 			 * lwp_suspend().
999 			 */
1000 			cv_broadcast(&p->p_holdlwps);
1001 		}
1002 		break;
1003 	}
1004 
1005 	t->t_stoptime = stoptime;
1006 
1007 	if (why == PR_JOBCONTROL || (why == PR_SUSPENDED && p->p_stopsig)) {
1008 		/*
1009 		 * Determine if the whole process is jobstopped.
1010 		 */
1011 		if (jobstopped(p)) {
1012 			sigqueue_t *sqp;
1013 			int sig;
1014 
1015 			if ((sig = p->p_stopsig) == 0)
1016 				p->p_stopsig = (uchar_t)(sig = what);
1017 			mutex_exit(&p->p_lock);
1018 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1019 			mutex_enter(&pidlock);
1020 			/*
1021 			 * The last lwp to stop notifies the parent.
1022 			 * Turn off the CLDCONT flag now so the first
1023 			 * lwp to continue knows what to do.
1024 			 */
1025 			p->p_pidflag &= ~CLDCONT;
1026 			p->p_wcode = CLD_STOPPED;
1027 			p->p_wdata = sig;
1028 			sigcld(p, sqp);
1029 			/*
1030 			 * Grab p->p_lock before releasing pidlock so the
1031 			 * parent and the child don't have a race condition.
1032 			 */
1033 			mutex_enter(&p->p_lock);
1034 			mutex_exit(&pidlock);
1035 			p->p_stopsig = 0;
1036 		} else if (why == PR_JOBCONTROL && p->p_stopsig == 0) {
1037 			/*
1038 			 * Set p->p_stopsig and wake up sleeping lwps
1039 			 * so they will stop in sympathy with this lwp.
1040 			 */
1041 			p->p_stopsig = (uchar_t)what;
1042 			pokelwps(p);
1043 			/*
1044 			 * We do this just in case one of the threads we asked
1045 			 * to stop is in holdlwps() (called from cfork()) or
1046 			 * lwp_suspend().
1047 			 */
1048 			cv_broadcast(&p->p_holdlwps);
1049 		}
1050 	}
1051 
1052 	if (why != PR_JOBCONTROL && why != PR_CHECKPOINT) {
1053 		/*
1054 		 * Do process-level notification when all lwps are
1055 		 * either stopped on events of interest to /proc
1056 		 * or are stopped showing PR_SUSPENDED or are zombies.
1057 		 */
1058 		procstop = 1;
1059 		for (tx = t->t_forw; procstop && tx != t; tx = tx->t_forw) {
1060 			if (VSTOPPED(tx))
1061 				continue;
1062 			thread_lock(tx);
1063 			switch (tx->t_state) {
1064 			case TS_ZOMB:
1065 				break;
1066 			case TS_STOPPED:
1067 				/* neither ISTOPPED nor SUSPENDED? */
1068 				if ((tx->t_schedflag &
1069 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART)) ==
1070 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART))
1071 					procstop = 0;
1072 				break;
1073 			case TS_SLEEP:
1074 				/* not paused for watchpoints? */
1075 				if (!(tx->t_flag & T_WAKEABLE) ||
1076 				    tx->t_wchan0 == NULL ||
1077 				    !(tx->t_proc_flag & TP_PAUSE))
1078 					procstop = 0;
1079 				break;
1080 			default:
1081 				procstop = 0;
1082 				break;
1083 			}
1084 			thread_unlock(tx);
1085 		}
1086 		if (procstop) {
1087 			/* there must not be any remapped watched pages now */
1088 			ASSERT(p->p_mapcnt == 0);
1089 			if (p->p_proc_flag & P_PR_PTRACE) {
1090 				/* ptrace() compatibility */
1091 				mutex_exit(&p->p_lock);
1092 				mutex_enter(&pidlock);
1093 				p->p_wcode = CLD_TRAPPED;
1094 				p->p_wdata = (why == PR_SIGNALLED)?
1095 				    what : SIGTRAP;
1096 				cv_broadcast(&p->p_parent->p_cv);
1097 				/*
1098 				 * Grab p->p_lock before releasing pidlock so
1099 				 * parent and child don't have a race condition.
1100 				 */
1101 				mutex_enter(&p->p_lock);
1102 				mutex_exit(&pidlock);
1103 			}
1104 			if (p->p_trace)			/* /proc */
1105 				prnotify(p->p_trace);
1106 			cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */
1107 			cv_broadcast(&p->p_holdlwps);	/* holdwatch() */
1108 		}
1109 		if (why != PR_SUSPENDED) {
1110 			lep = p->p_lwpdir[t->t_dslot].ld_entry;
1111 			if (lep->le_trace)		/* /proc */
1112 				prnotify(lep->le_trace);
1113 			/*
1114 			 * Special notification for creation of the agent lwp.
1115 			 */
1116 			if (t == p->p_agenttp &&
1117 			    (t->t_proc_flag & TP_PRSTOP) &&
1118 			    p->p_trace)
1119 				prnotify(p->p_trace);
1120 			/*
1121 			 * The situation may have changed since we dropped
1122 			 * and reacquired p->p_lock. Double-check now
1123 			 * whether we should stop or not.
1124 			 */
1125 			if (!(t->t_proc_flag & TP_STOPPING)) {
1126 				if (t->t_proc_flag & TP_PRSTOP)
1127 					t->t_proc_flag |= TP_STOPPING;
1128 			}
1129 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
1130 			prnostep(lwp);
1131 		}
1132 	}
1133 
1134 	if (why == PR_SUSPENDED) {
1135 
1136 		/*
1137 		 * We always broadcast in the case of SUSPEND_PAUSE.  This is
1138 		 * because checks for TP_PAUSE take precedence over checks for
1139 		 * SHOLDWATCH.  If a thread is trying to stop because of
1140 		 * SUSPEND_PAUSE and tries to do a holdwatch(), it will be
1141 		 * waiting for the rest of the threads to enter a stopped state.
1142 		 * If we are stopping for a SUSPEND_PAUSE, we may be the last
1143 		 * lwp and not know it, so broadcast just in case.
1144 		 */
1145 		if (what == SUSPEND_PAUSE ||
1146 		    --p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP))
1147 			cv_broadcast(&p->p_holdlwps);
1148 
1149 	}
1150 
1151 	/*
1152 	 * Need to do this here (rather than after the thread is officially
1153 	 * stopped) because we can't call mutex_enter from a stopped thread.
1154 	 */
1155 	if (why == PR_CHECKPOINT)
1156 		del_one_utstop();
1157 
1158 	thread_lock(t);
1159 	ASSERT((t->t_schedflag & TS_ALLSTART) == 0);
1160 	t->t_schedflag |= flags;
1161 	t->t_whystop = (short)why;
1162 	t->t_whatstop = (short)what;
1163 	CL_STOP(t, why, what);
1164 	(void) new_mstate(t, LMS_STOPPED);
1165 	thread_stop(t);			/* set stop state and drop lock */
1166 
1167 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
1168 		/*
1169 		 * We may have gotten a SIGKILL or a SIGCONT when
1170 		 * we released p->p_lock; make one last check.
1171 		 * Also check for a /proc run-on-last-close.
1172 		 */
1173 		if (sigismember(&t->t_sig, SIGKILL) ||
1174 		    sigismember(&p->p_sig, SIGKILL) ||
1175 		    (t->t_proc_flag & TP_LWPEXIT) ||
1176 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
1177 			p->p_stopsig = 0;
1178 			thread_lock(t);
1179 			t->t_schedflag |= TS_XSTART | TS_PSTART;
1180 			setrun_locked(t);
1181 			thread_unlock_nopreempt(t);
1182 		} else if (why == PR_JOBCONTROL) {
1183 			if (p->p_flag & SSCONT) {
1184 				/*
1185 				 * This resulted from a SIGCONT posted
1186 				 * while we were not holding p->p_lock.
1187 				 */
1188 				p->p_stopsig = 0;
1189 				thread_lock(t);
1190 				t->t_schedflag |= TS_XSTART;
1191 				setrun_locked(t);
1192 				thread_unlock_nopreempt(t);
1193 			}
1194 		} else if (!(t->t_proc_flag & TP_STOPPING)) {
1195 			/*
1196 			 * This resulted from a /proc run-on-last-close.
1197 			 */
1198 			thread_lock(t);
1199 			t->t_schedflag |= TS_PSTART;
1200 			setrun_locked(t);
1201 			thread_unlock_nopreempt(t);
1202 		}
1203 	}
1204 
1205 	t->t_proc_flag &= ~TP_STOPPING;
1206 	mutex_exit(&p->p_lock);
1207 
1208 	swtch();
1209 	setallwatch();	/* reestablish any watchpoints set while stopped */
1210 	mutex_enter(&p->p_lock);
1211 	prbarrier(p);	/* barrier against /proc locking */
1212 }
1213 
1214 /* Interface for resetting user thread stop count. */
1215 void
1216 utstop_init(void)
1217 {
1218 	mutex_enter(&thread_stop_lock);
1219 	num_utstop = 0;
1220 	mutex_exit(&thread_stop_lock);
1221 }
1222 
1223 /* Interface for registering a user thread stop request. */
1224 void
1225 add_one_utstop(void)
1226 {
1227 	mutex_enter(&thread_stop_lock);
1228 	num_utstop++;
1229 	mutex_exit(&thread_stop_lock);
1230 }
1231 
1232 /* Interface for cancelling a user thread stop request */
1233 void
1234 del_one_utstop(void)
1235 {
1236 	mutex_enter(&thread_stop_lock);
1237 	num_utstop--;
1238 	if (num_utstop == 0)
1239 		cv_broadcast(&utstop_cv);
1240 	mutex_exit(&thread_stop_lock);
1241 }
1242 
1243 /* Interface to wait for all user threads to be stopped */
1244 void
1245 utstop_timedwait(clock_t ticks)
1246 {
1247 	mutex_enter(&thread_stop_lock);
1248 	if (num_utstop > 0)
1249 		(void) cv_reltimedwait(&utstop_cv, &thread_stop_lock, ticks,
1250 		    TR_CLOCK_TICK);
1251 	mutex_exit(&thread_stop_lock);
1252 }
1253 
1254 /*
1255  * Perform the action specified by the current signal.
1256  * The usual sequence is:
1257  * 	if (issig())
1258  * 		psig();
1259  * The signal bit has already been cleared by issig(),
1260  * the current signal number has been stored in lwp_cursig,
1261  * and the current siginfo is now referenced by lwp_curinfo.
1262  */
1263 void
1264 psig(void)
1265 {
1266 	kthread_t *t = curthread;
1267 	proc_t *p = ttoproc(t);
1268 	klwp_t *lwp = ttolwp(t);
1269 	void (*func)();
1270 	int sig, rc, code, ext;
1271 	pid_t pid = -1;
1272 	id_t ctid = 0;
1273 	zoneid_t zoneid = -1;
1274 	sigqueue_t *sqp = NULL;
1275 	uint32_t auditing = AU_AUDITING();
1276 
1277 	mutex_enter(&p->p_lock);
1278 	schedctl_finish_sigblock(t);
1279 	code = CLD_KILLED;
1280 
1281 	if (p->p_flag & SEXITLWPS) {
1282 		lwp_exit();
1283 		return;			/* not reached */
1284 	}
1285 	sig = lwp->lwp_cursig;
1286 	ext = lwp->lwp_extsig;
1287 
1288 	ASSERT(sig < NSIG);
1289 
1290 	/*
1291 	 * Re-check lwp_cursig after we acquire p_lock.  Since p_lock was
1292 	 * dropped between issig() and psig(), a debugger may have cleared
1293 	 * lwp_cursig via /proc in the intervening window.
1294 	 */
1295 	if (sig == 0) {
1296 		if (lwp->lwp_curinfo) {
1297 			siginfofree(lwp->lwp_curinfo);
1298 			lwp->lwp_curinfo = NULL;
1299 		}
1300 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
1301 			t->t_flag &= ~T_TOMASK;
1302 			t->t_hold = lwp->lwp_sigoldmask;
1303 		}
1304 		mutex_exit(&p->p_lock);
1305 		return;
1306 	}
1307 	func = PTOU(curproc)->u_signal[sig-1];
1308 
1309 	/*
1310 	 * The signal disposition could have changed since we promoted
1311 	 * this signal from pending to current (we dropped p->p_lock).
1312 	 * This can happen only in a multi-threaded process.
1313 	 */
1314 	if (sigismember(&p->p_ignore, sig) ||
1315 	    (func == SIG_DFL && sigismember(&stopdefault, sig))) {
1316 		lwp->lwp_cursig = 0;
1317 		lwp->lwp_extsig = 0;
1318 		if (lwp->lwp_curinfo) {
1319 			siginfofree(lwp->lwp_curinfo);
1320 			lwp->lwp_curinfo = NULL;
1321 		}
1322 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
1323 			t->t_flag &= ~T_TOMASK;
1324 			t->t_hold = lwp->lwp_sigoldmask;
1325 		}
1326 		mutex_exit(&p->p_lock);
1327 		return;
1328 	}
1329 
1330 	/*
1331 	 * We check lwp_curinfo first since pr_setsig can actually
1332 	 * stuff a sigqueue_t there for SIGKILL.
1333 	 */
1334 	if (lwp->lwp_curinfo) {
1335 		sqp = lwp->lwp_curinfo;
1336 	} else if (sig == SIGKILL && p->p_killsqp) {
1337 		sqp = p->p_killsqp;
1338 	}
1339 
1340 	if (sqp != NULL) {
1341 		if (SI_FROMUSER(&sqp->sq_info)) {
1342 			pid = sqp->sq_info.si_pid;
1343 			ctid = sqp->sq_info.si_ctid;
1344 			zoneid = sqp->sq_info.si_zoneid;
1345 		}
1346 		/*
1347 		 * If we have a sigqueue_t, its sq_external value
1348 		 * trumps the lwp_extsig value.  It is theoretically
1349 		 * possible to make lwp_extsig reflect reality, but it
1350 		 * would unnecessarily complicate things elsewhere.
1351 		 */
1352 		ext = sqp->sq_external;
1353 	}
1354 
1355 	if (func == SIG_DFL) {
1356 		mutex_exit(&p->p_lock);
1357 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
1358 		    NULL, void (*)(void), func);
1359 	} else {
1360 		k_siginfo_t *sip = NULL;
1361 
1362 		/*
1363 		 * If DTrace user-land tracing is active, give DTrace a
1364 		 * chance to defer the signal until after tracing is
1365 		 * complete.
1366 		 */
1367 		if (t->t_dtrace_on && dtrace_safe_defer_signal()) {
1368 			mutex_exit(&p->p_lock);
1369 			return;
1370 		}
1371 
1372 		/*
1373 		 * save siginfo pointer here, in case the
1374 		 * the signal's reset bit is on
1375 		 *
1376 		 * The presence of a current signal prevents paging
1377 		 * from succeeding over a network.  We copy the current
1378 		 * signal information to the side and cancel the current
1379 		 * signal so that sendsig() will succeed.
1380 		 */
1381 		if (sigismember(&p->p_siginfo, sig)) {
1382 			sip = &lwp->lwp_siginfo;
1383 			if (sqp) {
1384 				bcopy(&sqp->sq_info, sip, sizeof (*sip));
1385 				/*
1386 				 * If we were interrupted out of a system call
1387 				 * due to pthread_cancel(), inform libc.
1388 				 */
1389 				if (sig == SIGCANCEL &&
1390 				    sip->si_code == SI_LWP &&
1391 				    t->t_sysnum != 0)
1392 					schedctl_cancel_eintr();
1393 			} else if (sig == SIGPROF && sip->si_signo == SIGPROF &&
1394 			    t->t_rprof != NULL && t->t_rprof->rp_anystate) {
1395 				/* EMPTY */;
1396 			} else {
1397 				bzero(sip, sizeof (*sip));
1398 				sip->si_signo = sig;
1399 				sip->si_code = SI_NOINFO;
1400 			}
1401 		}
1402 
1403 		if (t->t_flag & T_TOMASK)
1404 			t->t_flag &= ~T_TOMASK;
1405 		else
1406 			lwp->lwp_sigoldmask = t->t_hold;
1407 		sigorset(&t->t_hold, &PTOU(curproc)->u_sigmask[sig-1]);
1408 		if (!sigismember(&PTOU(curproc)->u_signodefer, sig))
1409 			sigaddset(&t->t_hold, sig);
1410 		if (sigismember(&PTOU(curproc)->u_sigresethand, sig))
1411 			setsigact(sig, SIG_DFL, &nullsmask, 0);
1412 
1413 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
1414 		    sip, void (*)(void), func);
1415 
1416 		lwp->lwp_cursig = 0;
1417 		lwp->lwp_extsig = 0;
1418 		if (lwp->lwp_curinfo) {
1419 			/* p->p_killsqp is freed by freeproc */
1420 			siginfofree(lwp->lwp_curinfo);
1421 			lwp->lwp_curinfo = NULL;
1422 		}
1423 		mutex_exit(&p->p_lock);
1424 		lwp->lwp_ru.nsignals++;
1425 
1426 		if (p->p_model == DATAMODEL_NATIVE)
1427 			rc = sendsig(sig, sip, func);
1428 #ifdef _SYSCALL32_IMPL
1429 		else
1430 			rc = sendsig32(sig, sip, func);
1431 #endif	/* _SYSCALL32_IMPL */
1432 		if (rc)
1433 			return;
1434 		sig = lwp->lwp_cursig = SIGSEGV;
1435 		ext = 0;	/* lwp_extsig was set above */
1436 		pid = -1;
1437 		ctid = 0;
1438 	}
1439 
1440 	if (sigismember(&coredefault, sig)) {
1441 		/*
1442 		 * Terminate all LWPs but don't discard them.
1443 		 * If another lwp beat us to the punch by calling exit(),
1444 		 * evaporate now.
1445 		 */
1446 		proc_is_exiting(p);
1447 		if (exitlwps(1) != 0) {
1448 			mutex_enter(&p->p_lock);
1449 			lwp_exit();
1450 		}
1451 		/* if we got a SIGKILL from anywhere, no core dump */
1452 		if (p->p_flag & SKILLED) {
1453 			sig = SIGKILL;
1454 			ext = (p->p_flag & SEXTKILLED) != 0;
1455 		} else {
1456 			if (auditing)		/* audit core dump */
1457 				audit_core_start(sig);
1458 			if (core(sig, ext) == 0)
1459 				code = CLD_DUMPED;
1460 			if (auditing)		/* audit core dump */
1461 				audit_core_finish(code);
1462 		}
1463 	}
1464 
1465 	/*
1466 	 * Generate a contract event once if the process is killed
1467 	 * by a signal.
1468 	 */
1469 	if (ext) {
1470 		proc_is_exiting(p);
1471 		if (exitlwps(0) != 0) {
1472 			mutex_enter(&p->p_lock);
1473 			lwp_exit();
1474 		}
1475 		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
1476 		    zoneid);
1477 	}
1478 
1479 	exit(code, sig);
1480 }
1481 
1482 /*
1483  * Find next unheld signal in ssp for thread t.
1484  */
1485 int
1486 fsig(k_sigset_t *ssp, kthread_t *t)
1487 {
1488 	proc_t *p = ttoproc(t);
1489 	user_t *up = PTOU(p);
1490 	int i;
1491 	k_sigset_t temp;
1492 
1493 	ASSERT(MUTEX_HELD(&p->p_lock));
1494 
1495 	/*
1496 	 * Don't promote any signals for the parent of a vfork()d
1497 	 * child that hasn't yet released the parent's memory.
1498 	 */
1499 	if (p->p_flag & SVFWAIT)
1500 		return (0);
1501 
1502 	temp = *ssp;
1503 	sigdiffset(&temp, &t->t_hold);
1504 
1505 	/*
1506 	 * Don't promote stopping signals (except SIGSTOP) for a child
1507 	 * of vfork() that hasn't yet released the parent's memory.
1508 	 */
1509 	if (p->p_flag & SVFORK)
1510 		sigdiffset(&temp, &holdvfork);
1511 
1512 	/*
1513 	 * Don't promote a signal that will stop
1514 	 * the process when lwp_nostop is set.
1515 	 */
1516 	if (ttolwp(t)->lwp_nostop) {
1517 		sigdelset(&temp, SIGSTOP);
1518 		if (!p->p_pgidp->pid_pgorphaned) {
1519 			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
1520 				sigdelset(&temp, SIGTSTP);
1521 			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
1522 				sigdelset(&temp, SIGTTIN);
1523 			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
1524 				sigdelset(&temp, SIGTTOU);
1525 		}
1526 	}
1527 
1528 	/*
1529 	 * Choose SIGKILL and SIGPROF before all other pending signals.
1530 	 * The rest are promoted in signal number order.
1531 	 */
1532 	if (sigismember(&temp, SIGKILL))
1533 		return (SIGKILL);
1534 	if (sigismember(&temp, SIGPROF))
1535 		return (SIGPROF);
1536 
1537 	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
1538 		if (temp.__sigbits[i])
1539 			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
1540 			    lowbit(temp.__sigbits[i]));
1541 	}
1542 
1543 	return (0);
1544 }
1545 
1546 void
1547 setsigact(int sig, void (*disp)(), const k_sigset_t *mask, int flags)
1548 {
1549 	proc_t *p = ttoproc(curthread);
1550 	kthread_t *t;
1551 
1552 	ASSERT(MUTEX_HELD(&p->p_lock));
1553 
1554 	PTOU(curproc)->u_signal[sig - 1] = disp;
1555 
1556 	/*
1557 	 * Honor the SA_SIGINFO flag if the signal is being caught.
1558 	 * Force the SA_SIGINFO flag if the signal is not being caught.
1559 	 * This is necessary to make sigqueue() and sigwaitinfo() work
1560 	 * properly together when the signal is set to default or is
1561 	 * being temporarily ignored.
1562 	 */
1563 	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
1564 		sigaddset(&p->p_siginfo, sig);
1565 	else
1566 		sigdelset(&p->p_siginfo, sig);
1567 
1568 	if (disp != SIG_DFL && disp != SIG_IGN) {
1569 		sigdelset(&p->p_ignore, sig);
1570 		PTOU(curproc)->u_sigmask[sig - 1] = *mask;
1571 		if (!sigismember(&cantreset, sig)) {
1572 			if (flags & SA_RESETHAND)
1573 				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
1574 			else
1575 				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
1576 		}
1577 		if (flags & SA_NODEFER)
1578 			sigaddset(&PTOU(curproc)->u_signodefer, sig);
1579 		else
1580 			sigdelset(&PTOU(curproc)->u_signodefer, sig);
1581 		if (flags & SA_RESTART)
1582 			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
1583 		else
1584 			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
1585 		if (flags & SA_ONSTACK)
1586 			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
1587 		else
1588 			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
1589 	} else if (disp == SIG_IGN ||
1590 	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
1591 		/*
1592 		 * Setting the signal action to SIG_IGN results in the
1593 		 * discarding of all pending signals of that signal number.
1594 		 * Setting the signal action to SIG_DFL does the same *only*
1595 		 * if the signal's default behavior is to be ignored.
1596 		 */
1597 		sigaddset(&p->p_ignore, sig);
1598 		sigdelset(&p->p_sig, sig);
1599 		sigdelset(&p->p_extsig, sig);
1600 		sigdelq(p, NULL, sig);
1601 		t = p->p_tlist;
1602 		do {
1603 			sigdelset(&t->t_sig, sig);
1604 			sigdelset(&t->t_extsig, sig);
1605 			sigdelq(p, t, sig);
1606 		} while ((t = t->t_forw) != p->p_tlist);
1607 	} else {
1608 		/*
1609 		 * The signal action is being set to SIG_DFL and the default
1610 		 * behavior is to do something: make sure it is not ignored.
1611 		 */
1612 		sigdelset(&p->p_ignore, sig);
1613 	}
1614 
1615 	if (sig == SIGCLD) {
1616 		if (flags & SA_NOCLDWAIT)
1617 			p->p_flag |= SNOWAIT;
1618 		else
1619 			p->p_flag &= ~SNOWAIT;
1620 
1621 		if (flags & SA_NOCLDSTOP)
1622 			p->p_flag &= ~SJCTL;
1623 		else
1624 			p->p_flag |= SJCTL;
1625 
1626 		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
1627 			proc_t *cp, *tp;
1628 
1629 			mutex_exit(&p->p_lock);
1630 			mutex_enter(&pidlock);
1631 			for (cp = p->p_child; cp != NULL; cp = tp) {
1632 				tp = cp->p_sibling;
1633 				if (cp->p_stat == SZOMB &&
1634 				    !(cp->p_pidflag & CLDWAITPID))
1635 					freeproc(cp);
1636 			}
1637 			mutex_exit(&pidlock);
1638 			mutex_enter(&p->p_lock);
1639 		}
1640 	}
1641 }
1642 
1643 /*
1644  * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
1645  * Called from exec_common() for a process undergoing execve()
1646  * and from cfork() for a newly-created child of vfork().
1647  * In the vfork() case, 'p' is not the current process.
1648  * In both cases, there is only one thread in the process.
1649  */
1650 void
1651 sigdefault(proc_t *p)
1652 {
1653 	kthread_t *t = p->p_tlist;
1654 	struct user *up = PTOU(p);
1655 	int sig;
1656 
1657 	ASSERT(MUTEX_HELD(&p->p_lock));
1658 
1659 	for (sig = 1; sig < NSIG; sig++) {
1660 		if (up->u_signal[sig - 1] != SIG_DFL &&
1661 		    up->u_signal[sig - 1] != SIG_IGN) {
1662 			up->u_signal[sig - 1] = SIG_DFL;
1663 			sigemptyset(&up->u_sigmask[sig - 1]);
1664 			if (sigismember(&ignoredefault, sig)) {
1665 				sigdelq(p, NULL, sig);
1666 				sigdelq(p, t, sig);
1667 			}
1668 			if (sig == SIGCLD)
1669 				p->p_flag &= ~(SNOWAIT|SJCTL);
1670 		}
1671 	}
1672 	sigorset(&p->p_ignore, &ignoredefault);
1673 	sigfillset(&p->p_siginfo);
1674 	sigdiffset(&p->p_siginfo, &cantmask);
1675 	sigdiffset(&p->p_sig, &ignoredefault);
1676 	sigdiffset(&p->p_extsig, &ignoredefault);
1677 	sigdiffset(&t->t_sig, &ignoredefault);
1678 	sigdiffset(&t->t_extsig, &ignoredefault);
1679 }
1680 
1681 void
1682 sigcld(proc_t *cp, sigqueue_t *sqp)
1683 {
1684 	proc_t *pp = cp->p_parent;
1685 
1686 	ASSERT(MUTEX_HELD(&pidlock));
1687 
1688 	switch (cp->p_wcode) {
1689 	case CLD_EXITED:
1690 	case CLD_DUMPED:
1691 	case CLD_KILLED:
1692 		ASSERT(cp->p_stat == SZOMB);
1693 		/*
1694 		 * The broadcast on p_srwchan_cv is a kludge to
1695 		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
1696 		 */
1697 		cv_broadcast(&cp->p_srwchan_cv);
1698 
1699 		/*
1700 		 * Add to newstate list of the parent
1701 		 */
1702 		add_ns(pp, cp);
1703 
1704 		cv_broadcast(&pp->p_cv);
1705 		if ((pp->p_flag & SNOWAIT) ||
1706 		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
1707 			if (!(cp->p_pidflag & CLDWAITPID))
1708 				freeproc(cp);
1709 		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
1710 			post_sigcld(cp, sqp);
1711 			sqp = NULL;
1712 		}
1713 		break;
1714 
1715 	case CLD_STOPPED:
1716 	case CLD_CONTINUED:
1717 		cv_broadcast(&pp->p_cv);
1718 		if (pp->p_flag & SJCTL) {
1719 			post_sigcld(cp, sqp);
1720 			sqp = NULL;
1721 		}
1722 		break;
1723 	}
1724 
1725 	if (sqp)
1726 		siginfofree(sqp);
1727 }
1728 
1729 /*
1730  * Common code called from sigcld() and from
1731  * waitid() and issig_forreal() via sigcld_repost().
1732  * Give the parent process a SIGCLD if it does not have one pending,
1733  * else mark the child process so a SIGCLD can be posted later.
1734  */
1735 static void
1736 post_sigcld(proc_t *cp, sigqueue_t *sqp)
1737 {
1738 	proc_t *pp = cp->p_parent;
1739 	k_siginfo_t info;
1740 
1741 	ASSERT(MUTEX_HELD(&pidlock));
1742 	mutex_enter(&pp->p_lock);
1743 
1744 	/*
1745 	 * If a SIGCLD is pending, then just mark the child process
1746 	 * so that its SIGCLD will be posted later, when the first
1747 	 * SIGCLD is taken off the queue or when the parent is ready
1748 	 * to receive it or accept it, if ever.
1749 	 */
1750 	if (sigismember(&pp->p_sig, SIGCLD)) {
1751 		cp->p_pidflag |= CLDPEND;
1752 	} else {
1753 		cp->p_pidflag &= ~CLDPEND;
1754 		if (sqp == NULL) {
1755 			/*
1756 			 * This can only happen when the parent is init.
1757 			 * (See call to sigcld(q, NULL) in exit().)
1758 			 * Use KM_NOSLEEP to avoid deadlock.
1759 			 */
1760 			ASSERT(pp == proc_init);
1761 			winfo(cp, &info, 0);
1762 			sigaddq(pp, NULL, &info, KM_NOSLEEP);
1763 		} else {
1764 			winfo(cp, &sqp->sq_info, 0);
1765 			sigaddqa(pp, NULL, sqp);
1766 			sqp = NULL;
1767 		}
1768 	}
1769 
1770 	mutex_exit(&pp->p_lock);
1771 
1772 	if (sqp)
1773 		siginfofree(sqp);
1774 }
1775 
1776 /*
1777  * Search for a child that has a pending SIGCLD for us, the parent.
1778  * The queue of SIGCLD signals is implied by the list of children.
1779  * We post the SIGCLD signals one at a time so they don't get lost.
1780  * When one is dequeued, another is enqueued, until there are no more.
1781  */
1782 void
1783 sigcld_repost()
1784 {
1785 	proc_t *pp = curproc;
1786 	proc_t *cp;
1787 	sigqueue_t *sqp;
1788 
1789 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1790 	mutex_enter(&pidlock);
1791 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
1792 		if (cp->p_pidflag & CLDPEND) {
1793 			post_sigcld(cp, sqp);
1794 			mutex_exit(&pidlock);
1795 			return;
1796 		}
1797 	}
1798 	mutex_exit(&pidlock);
1799 	kmem_free(sqp, sizeof (sigqueue_t));
1800 }
1801 
1802 /*
1803  * count number of sigqueue send by sigaddqa()
1804  */
1805 void
1806 sigqsend(int cmd, proc_t *p, kthread_t *t, sigqueue_t *sigqp)
1807 {
1808 	sigqhdr_t *sqh;
1809 
1810 	sqh = (sigqhdr_t *)sigqp->sq_backptr;
1811 	ASSERT(sqh);
1812 
1813 	mutex_enter(&sqh->sqb_lock);
1814 	sqh->sqb_sent++;
1815 	mutex_exit(&sqh->sqb_lock);
1816 
1817 	if (cmd == SN_SEND)
1818 		sigaddqa(p, t, sigqp);
1819 	else
1820 		siginfofree(sigqp);
1821 }
1822 
1823 int
1824 sigsendproc(proc_t *p, sigsend_t *pv)
1825 {
1826 	struct cred *cr;
1827 	proc_t *myprocp = curproc;
1828 
1829 	ASSERT(MUTEX_HELD(&pidlock));
1830 
1831 	if (p->p_pid == 1 && pv->sig && sigismember(&cantmask, pv->sig))
1832 		return (EPERM);
1833 
1834 	cr = CRED();
1835 
1836 	if (pv->checkperm == 0 ||
1837 	    (pv->sig == SIGCONT && p->p_sessp == myprocp->p_sessp) ||
1838 	    prochasprocperm(p, myprocp, cr)) {
1839 		pv->perm++;
1840 		if (pv->sig) {
1841 			/* Make sure we should be setting si_pid and friends */
1842 			ASSERT(pv->sicode <= 0);
1843 			if (SI_CANQUEUE(pv->sicode)) {
1844 				sigqueue_t *sqp;
1845 
1846 				mutex_enter(&myprocp->p_lock);
1847 				sqp = sigqalloc(myprocp->p_sigqhdr);
1848 				mutex_exit(&myprocp->p_lock);
1849 				if (sqp == NULL)
1850 					return (EAGAIN);
1851 				sqp->sq_info.si_signo = pv->sig;
1852 				sqp->sq_info.si_code = pv->sicode;
1853 				sqp->sq_info.si_pid = myprocp->p_pid;
1854 				sqp->sq_info.si_ctid = PRCTID(myprocp);
1855 				sqp->sq_info.si_zoneid = getzoneid();
1856 				sqp->sq_info.si_uid = crgetruid(cr);
1857 				sqp->sq_info.si_value = pv->value;
1858 				mutex_enter(&p->p_lock);
1859 				sigqsend(SN_SEND, p, NULL, sqp);
1860 				mutex_exit(&p->p_lock);
1861 			} else {
1862 				k_siginfo_t info;
1863 				bzero(&info, sizeof (info));
1864 				info.si_signo = pv->sig;
1865 				info.si_code = pv->sicode;
1866 				info.si_pid = myprocp->p_pid;
1867 				info.si_ctid = PRCTID(myprocp);
1868 				info.si_zoneid = getzoneid();
1869 				info.si_uid = crgetruid(cr);
1870 				mutex_enter(&p->p_lock);
1871 				/*
1872 				 * XXX: Should be KM_SLEEP but
1873 				 * we have to avoid deadlock.
1874 				 */
1875 				sigaddq(p, NULL, &info, KM_NOSLEEP);
1876 				mutex_exit(&p->p_lock);
1877 			}
1878 		}
1879 	}
1880 
1881 	return (0);
1882 }
1883 
1884 int
1885 sigsendset(procset_t *psp, sigsend_t *pv)
1886 {
1887 	int error;
1888 
1889 	error = dotoprocs(psp, sigsendproc, (char *)pv);
1890 	if (error == 0 && pv->perm == 0)
1891 		return (EPERM);
1892 
1893 	return (error);
1894 }
1895 
1896 /*
1897  * Dequeue a queued siginfo structure.
1898  * If a non-null thread pointer is passed then dequeue from
1899  * the thread queue, otherwise dequeue from the process queue.
1900  */
1901 void
1902 sigdeq(proc_t *p, kthread_t *t, int sig, sigqueue_t **qpp)
1903 {
1904 	sigqueue_t **psqp, *sqp;
1905 
1906 	ASSERT(MUTEX_HELD(&p->p_lock));
1907 
1908 	*qpp = NULL;
1909 
1910 	if (t != NULL) {
1911 		sigdelset(&t->t_sig, sig);
1912 		sigdelset(&t->t_extsig, sig);
1913 		psqp = &t->t_sigqueue;
1914 	} else {
1915 		sigdelset(&p->p_sig, sig);
1916 		sigdelset(&p->p_extsig, sig);
1917 		psqp = &p->p_sigqueue;
1918 	}
1919 
1920 	for (;;) {
1921 		if ((sqp = *psqp) == NULL)
1922 			return;
1923 		if (sqp->sq_info.si_signo == sig)
1924 			break;
1925 		else
1926 			psqp = &sqp->sq_next;
1927 	}
1928 	*qpp = sqp;
1929 	*psqp = sqp->sq_next;
1930 	for (sqp = *psqp; sqp; sqp = sqp->sq_next) {
1931 		if (sqp->sq_info.si_signo == sig) {
1932 			if (t != (kthread_t *)NULL) {
1933 				sigaddset(&t->t_sig, sig);
1934 				t->t_sig_check = 1;
1935 			} else {
1936 				sigaddset(&p->p_sig, sig);
1937 				set_proc_ast(p);
1938 			}
1939 			break;
1940 		}
1941 	}
1942 }
1943 
1944 /*
1945  * Delete a queued SIGCLD siginfo structure matching the k_siginfo_t argument.
1946  */
1947 void
1948 sigcld_delete(k_siginfo_t *ip)
1949 {
1950 	proc_t *p = curproc;
1951 	int another_sigcld = 0;
1952 	sigqueue_t **psqp, *sqp;
1953 
1954 	ASSERT(ip->si_signo == SIGCLD);
1955 
1956 	mutex_enter(&p->p_lock);
1957 
1958 	if (!sigismember(&p->p_sig, SIGCLD)) {
1959 		mutex_exit(&p->p_lock);
1960 		return;
1961 	}
1962 
1963 	psqp = &p->p_sigqueue;
1964 	for (;;) {
1965 		if ((sqp = *psqp) == NULL) {
1966 			mutex_exit(&p->p_lock);
1967 			return;
1968 		}
1969 		if (sqp->sq_info.si_signo == SIGCLD) {
1970 			if (sqp->sq_info.si_pid == ip->si_pid &&
1971 			    sqp->sq_info.si_code == ip->si_code &&
1972 			    sqp->sq_info.si_status == ip->si_status)
1973 				break;
1974 			another_sigcld = 1;
1975 		}
1976 		psqp = &sqp->sq_next;
1977 	}
1978 	*psqp = sqp->sq_next;
1979 
1980 	siginfofree(sqp);
1981 
1982 	for (sqp = *psqp; !another_sigcld && sqp; sqp = sqp->sq_next) {
1983 		if (sqp->sq_info.si_signo == SIGCLD)
1984 			another_sigcld = 1;
1985 	}
1986 
1987 	if (!another_sigcld) {
1988 		sigdelset(&p->p_sig, SIGCLD);
1989 		sigdelset(&p->p_extsig, SIGCLD);
1990 	}
1991 
1992 	mutex_exit(&p->p_lock);
1993 }
1994 
1995 /*
1996  * Delete queued siginfo structures.
1997  * If a non-null thread pointer is passed then delete from
1998  * the thread queue, otherwise delete from the process queue.
1999  */
2000 void
2001 sigdelq(proc_t *p, kthread_t *t, int sig)
2002 {
2003 	sigqueue_t **psqp, *sqp;
2004 
2005 	/*
2006 	 * We must be holding p->p_lock unless the process is
2007 	 * being reaped or has failed to get started on fork.
2008 	 */
2009 	ASSERT(MUTEX_HELD(&p->p_lock) ||
2010 	    p->p_stat == SIDL || p->p_stat == SZOMB);
2011 
2012 	if (t != (kthread_t *)NULL)
2013 		psqp = &t->t_sigqueue;
2014 	else
2015 		psqp = &p->p_sigqueue;
2016 
2017 	while (*psqp) {
2018 		sqp = *psqp;
2019 		if (sig == 0 || sqp->sq_info.si_signo == sig) {
2020 			*psqp = sqp->sq_next;
2021 			siginfofree(sqp);
2022 		} else
2023 			psqp = &sqp->sq_next;
2024 	}
2025 }
2026 
2027 /*
2028  * Insert a siginfo structure into a queue.
2029  * If a non-null thread pointer is passed then add to the thread queue,
2030  * otherwise add to the process queue.
2031  *
2032  * The function sigaddqins() is called with sigqueue already allocated.
2033  * It is called from sigaddqa() and sigaddq() below.
2034  *
2035  * The value of si_code implicitly indicates whether sigp is to be
2036  * explicitly queued, or to be queued to depth one.
2037  */
2038 static void
2039 sigaddqins(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
2040 {
2041 	sigqueue_t **psqp;
2042 	int sig = sigqp->sq_info.si_signo;
2043 
2044 	sigqp->sq_external = (curproc != &p0) &&
2045 	    (curproc->p_ct_process != p->p_ct_process);
2046 
2047 	/*
2048 	 * issig_forreal() doesn't bother dequeueing signals if SKILLED
2049 	 * is set, and even if it did, we would want to avoid situation
2050 	 * (which would be unique to SIGKILL) where one thread dequeued
2051 	 * the sigqueue_t and another executed psig().  So we create a
2052 	 * separate stash for SIGKILL's sigqueue_t.  Because a second
2053 	 * SIGKILL can set SEXTKILLED, we overwrite the existing entry
2054 	 * if (and only if) it was non-extracontractual.
2055 	 */
2056 	if (sig == SIGKILL) {
2057 		if (p->p_killsqp == NULL || !p->p_killsqp->sq_external) {
2058 			if (p->p_killsqp != NULL)
2059 				siginfofree(p->p_killsqp);
2060 			p->p_killsqp = sigqp;
2061 			sigqp->sq_next = NULL;
2062 		} else {
2063 			siginfofree(sigqp);
2064 		}
2065 		return;
2066 	}
2067 
2068 	ASSERT(sig >= 1 && sig < NSIG);
2069 	if (t != NULL)	/* directed to a thread */
2070 		psqp = &t->t_sigqueue;
2071 	else 		/* directed to a process */
2072 		psqp = &p->p_sigqueue;
2073 	if (SI_CANQUEUE(sigqp->sq_info.si_code) &&
2074 	    sigismember(&p->p_siginfo, sig)) {
2075 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next)
2076 				;
2077 	} else {
2078 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next) {
2079 			if ((*psqp)->sq_info.si_signo == sig) {
2080 				siginfofree(sigqp);
2081 				return;
2082 			}
2083 		}
2084 	}
2085 	*psqp = sigqp;
2086 	sigqp->sq_next = NULL;
2087 }
2088 
2089 /*
2090  * The function sigaddqa() is called with sigqueue already allocated.
2091  * If signal is ignored, discard but guarantee KILL and generation semantics.
2092  * It is called from sigqueue() and other places.
2093  */
2094 void
2095 sigaddqa(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
2096 {
2097 	int sig = sigqp->sq_info.si_signo;
2098 
2099 	ASSERT(MUTEX_HELD(&p->p_lock));
2100 	ASSERT(sig >= 1 && sig < NSIG);
2101 
2102 	if (sig_discardable(p, sig))
2103 		siginfofree(sigqp);
2104 	else
2105 		sigaddqins(p, t, sigqp);
2106 
2107 	sigtoproc(p, t, sig);
2108 }
2109 
2110 /*
2111  * Allocate the sigqueue_t structure and call sigaddqins().
2112  */
2113 void
2114 sigaddq(proc_t *p, kthread_t *t, k_siginfo_t *infop, int km_flags)
2115 {
2116 	sigqueue_t *sqp;
2117 	int sig = infop->si_signo;
2118 
2119 	ASSERT(MUTEX_HELD(&p->p_lock));
2120 	ASSERT(sig >= 1 && sig < NSIG);
2121 
2122 	/*
2123 	 * If the signal will be discarded by sigtoproc() or
2124 	 * if the process isn't requesting siginfo and it isn't
2125 	 * blocking the signal (it *could* change it's mind while
2126 	 * the signal is pending) then don't bother creating one.
2127 	 */
2128 	if (!sig_discardable(p, sig) &&
2129 	    (sigismember(&p->p_siginfo, sig) ||
2130 	    (curproc->p_ct_process != p->p_ct_process) ||
2131 	    (sig == SIGCLD && SI_FROMKERNEL(infop))) &&
2132 	    ((sqp = kmem_alloc(sizeof (sigqueue_t), km_flags)) != NULL)) {
2133 		bcopy(infop, &sqp->sq_info, sizeof (k_siginfo_t));
2134 		sqp->sq_func = NULL;
2135 		sqp->sq_next = NULL;
2136 		sigaddqins(p, t, sqp);
2137 	}
2138 	sigtoproc(p, t, sig);
2139 }
2140 
2141 /*
2142  * Handle stop-on-fault processing for the debugger.  Returns 0
2143  * if the fault is cleared during the stop, nonzero if it isn't.
2144  */
2145 int
2146 stop_on_fault(uint_t fault, k_siginfo_t *sip)
2147 {
2148 	proc_t *p = ttoproc(curthread);
2149 	klwp_t *lwp = ttolwp(curthread);
2150 
2151 	ASSERT(prismember(&p->p_fltmask, fault));
2152 
2153 	/*
2154 	 * Record current fault and siginfo structure so debugger can
2155 	 * find it.
2156 	 */
2157 	mutex_enter(&p->p_lock);
2158 	lwp->lwp_curflt = (uchar_t)fault;
2159 	lwp->lwp_siginfo = *sip;
2160 
2161 	stop(PR_FAULTED, fault);
2162 
2163 	fault = lwp->lwp_curflt;
2164 	lwp->lwp_curflt = 0;
2165 	mutex_exit(&p->p_lock);
2166 	return (fault);
2167 }
2168 
2169 void
2170 sigorset(k_sigset_t *s1, const k_sigset_t *s2)
2171 {
2172 	s1->__sigbits[0] |= s2->__sigbits[0];
2173 	s1->__sigbits[1] |= s2->__sigbits[1];
2174 	s1->__sigbits[2] |= s2->__sigbits[2];
2175 }
2176 
2177 void
2178 sigandset(k_sigset_t *s1, const k_sigset_t *s2)
2179 {
2180 	s1->__sigbits[0] &= s2->__sigbits[0];
2181 	s1->__sigbits[1] &= s2->__sigbits[1];
2182 	s1->__sigbits[2] &= s2->__sigbits[2];
2183 }
2184 
2185 void
2186 sigdiffset(k_sigset_t *s1, const k_sigset_t *s2)
2187 {
2188 	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
2189 	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
2190 	s1->__sigbits[2] &= ~(s2->__sigbits[2]);
2191 }
2192 
2193 /*
2194  * Return non-zero if curthread->t_sig_check should be set to 1, that is,
2195  * if there are any signals the thread might take on return from the kernel.
2196  * If ksigset_t's were a single word, we would do:
2197  *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
2198  */
2199 int
2200 sigcheck(proc_t *p, kthread_t *t)
2201 {
2202 	sc_shared_t *tdp = t->t_schedctl;
2203 
2204 	/*
2205 	 * If signals are blocked via the schedctl interface
2206 	 * then we only check for the unmaskable signals.
2207 	 * The unmaskable signal numbers should all be contained
2208 	 * in __sigbits[0] and we assume this for speed.
2209 	 */
2210 #if (CANTMASK1 == 0 && CANTMASK2 == 0)
2211 	if (tdp != NULL && tdp->sc_sigblock)
2212 		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2213 		    CANTMASK0);
2214 #else
2215 #error "fix me: CANTMASK1 and CANTMASK2 are not zero"
2216 #endif
2217 
2218 /* see uts/common/sys/signal.h for why this must be true */
2219 #if ((MAXSIG > (2 * 32)) && (MAXSIG <= (3 * 32)))
2220 	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2221 	    ~t->t_hold.__sigbits[0]) |
2222 	    ((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
2223 	    ~t->t_hold.__sigbits[1]) |
2224 	    (((p->p_sig.__sigbits[2] | t->t_sig.__sigbits[2]) &
2225 	    ~t->t_hold.__sigbits[2]) & FILLSET2));
2226 #else
2227 #error "fix me: MAXSIG out of bounds"
2228 #endif
2229 }
2230 
2231 void
2232 sigintr(k_sigset_t *smask, int intable)
2233 {
2234 	proc_t *p;
2235 	int owned;
2236 	k_sigset_t lmask;		/* local copy of cantmask */
2237 	klwp_t *lwp = ttolwp(curthread);
2238 
2239 	/*
2240 	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
2241 	 *    and SIGTERM. (Preserving the existing masks).
2242 	 *    This function supports the -intr nfs and ufs mount option.
2243 	 */
2244 
2245 	/*
2246 	 * don't do kernel threads
2247 	 */
2248 	if (lwp == NULL)
2249 		return;
2250 
2251 	/*
2252 	 * get access to signal mask
2253 	 */
2254 	p = ttoproc(curthread);
2255 	owned = mutex_owned(&p->p_lock);	/* this is filthy */
2256 	if (!owned)
2257 		mutex_enter(&p->p_lock);
2258 
2259 	/*
2260 	 * remember the current mask
2261 	 */
2262 	schedctl_finish_sigblock(curthread);
2263 	*smask = curthread->t_hold;
2264 
2265 	/*
2266 	 * mask out all signals
2267 	 */
2268 	sigfillset(&curthread->t_hold);
2269 
2270 	/*
2271 	 * Unmask the non-maskable signals (e.g., KILL), as long as
2272 	 * they aren't already masked (which could happen at exit).
2273 	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
2274 	 * second sets the current hold mask to (~0 & ~lmask), which reduces
2275 	 * to (~cantmask | curhold).
2276 	 */
2277 	lmask = cantmask;
2278 	sigdiffset(&lmask, smask);
2279 	sigdiffset(&curthread->t_hold, &lmask);
2280 
2281 	/*
2282 	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
2283 	 * Re-enable INT if it's originally enabled and the NFS mount option
2284 	 * nointr is not set.
2285 	 */
2286 	if (!sigismember(smask, SIGHUP))
2287 		sigdelset(&curthread->t_hold, SIGHUP);
2288 	if (!sigismember(smask, SIGINT) && intable)
2289 		sigdelset(&curthread->t_hold, SIGINT);
2290 	if (!sigismember(smask, SIGQUIT))
2291 		sigdelset(&curthread->t_hold, SIGQUIT);
2292 	if (!sigismember(smask, SIGTERM))
2293 		sigdelset(&curthread->t_hold, SIGTERM);
2294 
2295 	/*
2296 	 * release access to signal mask
2297 	 */
2298 	if (!owned)
2299 		mutex_exit(&p->p_lock);
2300 
2301 	/*
2302 	 * Indicate that this lwp is not to be stopped.
2303 	 */
2304 	lwp->lwp_nostop++;
2305 
2306 }
2307 
2308 void
2309 sigunintr(k_sigset_t *smask)
2310 {
2311 	proc_t *p;
2312 	int owned;
2313 	klwp_t *lwp = ttolwp(curthread);
2314 
2315 	/*
2316 	 * Reset previous mask (See sigintr() above)
2317 	 */
2318 	if (lwp != NULL) {
2319 		lwp->lwp_nostop--;	/* restore lwp stoppability */
2320 		p = ttoproc(curthread);
2321 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2322 		if (!owned)
2323 			mutex_enter(&p->p_lock);
2324 		curthread->t_hold = *smask;
2325 		/* so unmasked signals will be seen */
2326 		curthread->t_sig_check = 1;
2327 		if (!owned)
2328 			mutex_exit(&p->p_lock);
2329 	}
2330 }
2331 
2332 void
2333 sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
2334 {
2335 	proc_t	*p;
2336 	int owned;
2337 	/*
2338 	 * Save current signal mask in oldmask, then
2339 	 * set it to newmask.
2340 	 */
2341 	if (ttolwp(curthread) != NULL) {
2342 		p = ttoproc(curthread);
2343 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2344 		if (!owned)
2345 			mutex_enter(&p->p_lock);
2346 		schedctl_finish_sigblock(curthread);
2347 		if (oldmask != NULL)
2348 			*oldmask = curthread->t_hold;
2349 		curthread->t_hold = *newmask;
2350 		curthread->t_sig_check = 1;
2351 		if (!owned)
2352 			mutex_exit(&p->p_lock);
2353 	}
2354 }
2355 
2356 /*
2357  * Return true if the signal number is in range
2358  * and the signal code specifies signal queueing.
2359  */
2360 int
2361 sigwillqueue(int sig, int code)
2362 {
2363 	if (sig >= 0 && sig < NSIG) {
2364 		switch (code) {
2365 		case SI_QUEUE:
2366 		case SI_TIMER:
2367 		case SI_ASYNCIO:
2368 		case SI_MESGQ:
2369 			return (1);
2370 		}
2371 	}
2372 	return (0);
2373 }
2374 
2375 /*
2376  * The pre-allocated pool (with _SIGQUEUE_PREALLOC entries) is
2377  * allocated at the first sigqueue/signotify call.
2378  */
2379 sigqhdr_t *
2380 sigqhdralloc(size_t size, uint_t maxcount)
2381 {
2382 	size_t i;
2383 	sigqueue_t *sq, *next;
2384 	sigqhdr_t *sqh;
2385 
2386 	/*
2387 	 * Before the introduction of process.max-sigqueue-size
2388 	 * _SC_SIGQUEUE_MAX had this static value.
2389 	 */
2390 #define	_SIGQUEUE_PREALLOC	32
2391 
2392 	i = (_SIGQUEUE_PREALLOC * size) + sizeof (sigqhdr_t);
2393 	ASSERT(maxcount <= INT_MAX);
2394 	sqh = kmem_alloc(i, KM_SLEEP);
2395 	sqh->sqb_count = maxcount;
2396 	sqh->sqb_maxcount = maxcount;
2397 	sqh->sqb_size = i;
2398 	sqh->sqb_pexited = 0;
2399 	sqh->sqb_sent = 0;
2400 	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
2401 	for (i = _SIGQUEUE_PREALLOC - 1; i != 0; i--) {
2402 		next = (sigqueue_t *)((uintptr_t)sq + size);
2403 		sq->sq_next = next;
2404 		sq = next;
2405 	}
2406 	sq->sq_next = NULL;
2407 	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
2408 	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
2409 	return (sqh);
2410 }
2411 
2412 static void sigqrel(sigqueue_t *);
2413 
2414 /*
2415  * Allocate a sigqueue/signotify structure from the per process
2416  * pre-allocated pool or allocate a new sigqueue/signotify structure
2417  * if the pre-allocated pool is exhausted.
2418  */
2419 sigqueue_t *
2420 sigqalloc(sigqhdr_t *sqh)
2421 {
2422 	sigqueue_t *sq = NULL;
2423 
2424 	ASSERT(MUTEX_HELD(&curproc->p_lock));
2425 
2426 	if (sqh != NULL) {
2427 		mutex_enter(&sqh->sqb_lock);
2428 		if (sqh->sqb_count > 0) {
2429 			sqh->sqb_count--;
2430 			if (sqh->sqb_free == NULL) {
2431 				/*
2432 				 * The pre-allocated pool is exhausted.
2433 				 */
2434 				sq = kmem_alloc(sizeof (sigqueue_t), KM_SLEEP);
2435 				sq->sq_func = NULL;
2436 			} else {
2437 				sq = sqh->sqb_free;
2438 				sq->sq_func = sigqrel;
2439 				sqh->sqb_free = sq->sq_next;
2440 			}
2441 			mutex_exit(&sqh->sqb_lock);
2442 			bzero(&sq->sq_info, sizeof (k_siginfo_t));
2443 			sq->sq_backptr = sqh;
2444 			sq->sq_next = NULL;
2445 			sq->sq_external = 0;
2446 		} else {
2447 			mutex_exit(&sqh->sqb_lock);
2448 		}
2449 	}
2450 	return (sq);
2451 }
2452 
2453 /*
2454  * Return a sigqueue structure back to the pre-allocated pool.
2455  */
2456 static void
2457 sigqrel(sigqueue_t *sq)
2458 {
2459 	sigqhdr_t *sqh;
2460 
2461 	/* make sure that p_lock of the affected process is held */
2462 
2463 	sqh = (sigqhdr_t *)sq->sq_backptr;
2464 	mutex_enter(&sqh->sqb_lock);
2465 	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
2466 		mutex_exit(&sqh->sqb_lock);
2467 		cv_destroy(&sqh->sqb_cv);
2468 		mutex_destroy(&sqh->sqb_lock);
2469 		kmem_free(sqh, sqh->sqb_size);
2470 	} else {
2471 		sqh->sqb_count++;
2472 		sqh->sqb_sent--;
2473 		sq->sq_next = sqh->sqb_free;
2474 		sq->sq_backptr = NULL;
2475 		sqh->sqb_free = sq;
2476 		cv_signal(&sqh->sqb_cv);
2477 		mutex_exit(&sqh->sqb_lock);
2478 	}
2479 }
2480 
2481 /*
2482  * Free up the pre-allocated sigqueue headers of sigqueue pool
2483  * and signotify pool, if possible.
2484  * Called only by the owning process during exec() and exit().
2485  */
2486 void
2487 sigqfree(proc_t *p)
2488 {
2489 	ASSERT(MUTEX_HELD(&p->p_lock));
2490 
2491 	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
2492 		sigqhdrfree(p->p_sigqhdr);
2493 		p->p_sigqhdr = NULL;
2494 	}
2495 	if (p->p_signhdr != NULL) {	/* signotify pool */
2496 		sigqhdrfree(p->p_signhdr);
2497 		p->p_signhdr = NULL;
2498 	}
2499 }
2500 
2501 /*
2502  * Free up the pre-allocated header and sigq pool if possible.
2503  */
2504 void
2505 sigqhdrfree(sigqhdr_t *sqh)
2506 {
2507 	mutex_enter(&sqh->sqb_lock);
2508 	if (sqh->sqb_sent == 0) {
2509 		mutex_exit(&sqh->sqb_lock);
2510 		cv_destroy(&sqh->sqb_cv);
2511 		mutex_destroy(&sqh->sqb_lock);
2512 		kmem_free(sqh, sqh->sqb_size);
2513 	} else {
2514 		sqh->sqb_pexited = 1;
2515 		mutex_exit(&sqh->sqb_lock);
2516 	}
2517 }
2518 
2519 /*
2520  * Free up a single sigqueue structure.
2521  * No other code should free a sigqueue directly.
2522  */
2523 void
2524 siginfofree(sigqueue_t *sqp)
2525 {
2526 	if (sqp != NULL) {
2527 		if (sqp->sq_func != NULL)
2528 			(sqp->sq_func)(sqp);
2529 		else
2530 			kmem_free(sqp, sizeof (sigqueue_t));
2531 	}
2532 }
2533 
2534 /*
2535  * Generate a synchronous signal caused by a hardware
2536  * condition encountered by an lwp.  Called from trap().
2537  */
2538 void
2539 trapsig(k_siginfo_t *ip, int restartable)
2540 {
2541 	proc_t *p = ttoproc(curthread);
2542 	int sig = ip->si_signo;
2543 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2544 
2545 	ASSERT(sig > 0 && sig < NSIG);
2546 
2547 	if (curthread->t_dtrace_on)
2548 		dtrace_safe_synchronous_signal();
2549 
2550 	mutex_enter(&p->p_lock);
2551 	schedctl_finish_sigblock(curthread);
2552 	/*
2553 	 * Avoid a possible infinite loop if the lwp is holding the
2554 	 * signal generated by a trap of a restartable instruction or
2555 	 * if the signal so generated is being ignored by the process.
2556 	 */
2557 	if (restartable &&
2558 	    (sigismember(&curthread->t_hold, sig) ||
2559 	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
2560 		sigdelset(&curthread->t_hold, sig);
2561 		p->p_user.u_signal[sig-1] = SIG_DFL;
2562 		sigdelset(&p->p_ignore, sig);
2563 	}
2564 	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
2565 	sigaddqa(p, curthread, sqp);
2566 	mutex_exit(&p->p_lock);
2567 }
2568 
2569 /*
2570  * Dispatch the real time profiling signal in the traditional way,
2571  * honoring all of the /proc tracing mechanism built into issig().
2572  */
2573 static void
2574 realsigprof_slow(int sysnum, int nsysarg, int error)
2575 {
2576 	kthread_t *t = curthread;
2577 	proc_t *p = ttoproc(t);
2578 	klwp_t *lwp = ttolwp(t);
2579 	k_siginfo_t *sip = &lwp->lwp_siginfo;
2580 	void (*func)();
2581 
2582 	mutex_enter(&p->p_lock);
2583 	func = PTOU(p)->u_signal[SIGPROF - 1];
2584 	if (p->p_rprof_cyclic == CYCLIC_NONE ||
2585 	    func == SIG_DFL || func == SIG_IGN) {
2586 		bzero(t->t_rprof, sizeof (*t->t_rprof));
2587 		mutex_exit(&p->p_lock);
2588 		return;
2589 	}
2590 	if (sigismember(&t->t_hold, SIGPROF)) {
2591 		mutex_exit(&p->p_lock);
2592 		return;
2593 	}
2594 	sip->si_signo = SIGPROF;
2595 	sip->si_code = PROF_SIG;
2596 	sip->si_errno = error;
2597 	hrt2ts(gethrtime(), &sip->si_tstamp);
2598 	sip->si_syscall = sysnum;
2599 	sip->si_nsysarg = nsysarg;
2600 	sip->si_fault = lwp->lwp_lastfault;
2601 	sip->si_faddr = lwp->lwp_lastfaddr;
2602 	lwp->lwp_lastfault = 0;
2603 	lwp->lwp_lastfaddr = NULL;
2604 	sigtoproc(p, t, SIGPROF);
2605 	mutex_exit(&p->p_lock);
2606 	ASSERT(lwp->lwp_cursig == 0);
2607 	if (issig(FORREAL))
2608 		psig();
2609 	sip->si_signo = 0;
2610 	bzero(t->t_rprof, sizeof (*t->t_rprof));
2611 }
2612 
2613 /*
2614  * We are not tracing the SIGPROF signal, or doing any other unnatural
2615  * acts, like watchpoints, so dispatch the real time profiling signal
2616  * directly, bypassing all of the overhead built into issig().
2617  */
2618 static void
2619 realsigprof_fast(int sysnum, int nsysarg, int error)
2620 {
2621 	kthread_t *t = curthread;
2622 	proc_t *p = ttoproc(t);
2623 	klwp_t *lwp = ttolwp(t);
2624 	k_siginfo_t *sip = &lwp->lwp_siginfo;
2625 	void (*func)();
2626 	int rc;
2627 	int code;
2628 
2629 	/*
2630 	 * We don't need to acquire p->p_lock here;
2631 	 * we are manipulating thread-private data.
2632 	 */
2633 	func = PTOU(p)->u_signal[SIGPROF - 1];
2634 	if (p->p_rprof_cyclic == CYCLIC_NONE ||
2635 	    func == SIG_DFL || func == SIG_IGN) {
2636 		bzero(t->t_rprof, sizeof (*t->t_rprof));
2637 		return;
2638 	}
2639 	if (lwp->lwp_cursig != 0 ||
2640 	    lwp->lwp_curinfo != NULL ||
2641 	    sigismember(&t->t_hold, SIGPROF)) {
2642 		return;
2643 	}
2644 	sip->si_signo = SIGPROF;
2645 	sip->si_code = PROF_SIG;
2646 	sip->si_errno = error;
2647 	hrt2ts(gethrtime(), &sip->si_tstamp);
2648 	sip->si_syscall = sysnum;
2649 	sip->si_nsysarg = nsysarg;
2650 	sip->si_fault = lwp->lwp_lastfault;
2651 	sip->si_faddr = lwp->lwp_lastfaddr;
2652 	lwp->lwp_lastfault = 0;
2653 	lwp->lwp_lastfaddr = NULL;
2654 	if (t->t_flag & T_TOMASK)
2655 		t->t_flag &= ~T_TOMASK;
2656 	else
2657 		lwp->lwp_sigoldmask = t->t_hold;
2658 	sigorset(&t->t_hold, &PTOU(p)->u_sigmask[SIGPROF - 1]);
2659 	if (!sigismember(&PTOU(p)->u_signodefer, SIGPROF))
2660 		sigaddset(&t->t_hold, SIGPROF);
2661 	lwp->lwp_extsig = 0;
2662 	lwp->lwp_ru.nsignals++;
2663 	if (p->p_model == DATAMODEL_NATIVE)
2664 		rc = sendsig(SIGPROF, sip, func);
2665 #ifdef _SYSCALL32_IMPL
2666 	else
2667 		rc = sendsig32(SIGPROF, sip, func);
2668 #endif	/* _SYSCALL32_IMPL */
2669 	sip->si_signo = 0;
2670 	bzero(t->t_rprof, sizeof (*t->t_rprof));
2671 	if (rc == 0) {
2672 		/*
2673 		 * sendsig() failed; we must dump core with a SIGSEGV.
2674 		 * See psig().  This code is copied from there.
2675 		 */
2676 		lwp->lwp_cursig = SIGSEGV;
2677 		code = CLD_KILLED;
2678 		proc_is_exiting(p);
2679 		if (exitlwps(1) != 0) {
2680 			mutex_enter(&p->p_lock);
2681 			lwp_exit();
2682 		}
2683 		if (audit_active == C2AUDIT_LOADED)
2684 			audit_core_start(SIGSEGV);
2685 		if (core(SIGSEGV, 0) == 0)
2686 			code = CLD_DUMPED;
2687 		if (audit_active == C2AUDIT_LOADED)
2688 			audit_core_finish(code);
2689 		exit(code, SIGSEGV);
2690 	}
2691 }
2692 
2693 /*
2694  * Arrange for the real time profiling signal to be dispatched.
2695  */
2696 void
2697 realsigprof(int sysnum, int nsysarg, int error)
2698 {
2699 	kthread_t *t = curthread;
2700 	proc_t *p = ttoproc(t);
2701 
2702 	if (t->t_rprof->rp_anystate == 0)
2703 		return;
2704 
2705 	schedctl_finish_sigblock(t);
2706 
2707 	/* test for any activity that requires p->p_lock */
2708 	if (tracing(p, SIGPROF) || pr_watch_active(p) ||
2709 	    sigismember(&PTOU(p)->u_sigresethand, SIGPROF)) {
2710 		/* do it the classic slow way */
2711 		realsigprof_slow(sysnum, nsysarg, error);
2712 	} else {
2713 		/* do it the cheating-a-little fast way */
2714 		realsigprof_fast(sysnum, nsysarg, error);
2715 	}
2716 }
2717 
2718 #ifdef _SYSCALL32_IMPL
2719 
2720 /*
2721  * It's tricky to transmit a sigval between 32-bit and 64-bit
2722  * process, since in the 64-bit world, a pointer and an integer
2723  * are different sizes.  Since we're constrained by the standards
2724  * world not to change the types, and it's unclear how useful it is
2725  * to send pointers between address spaces this way, we preserve
2726  * the 'int' interpretation for 32-bit processes interoperating
2727  * with 64-bit processes.  The full semantics (pointers or integers)
2728  * are available for N-bit processes interoperating with N-bit
2729  * processes.
2730  */
2731 void
2732 siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
2733 {
2734 	bzero(dest, sizeof (*dest));
2735 
2736 	/*
2737 	 * The absolute minimum content is si_signo and si_code.
2738 	 */
2739 	dest->si_signo = src->si_signo;
2740 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2741 		return;
2742 
2743 	/*
2744 	 * A siginfo generated by user level is structured
2745 	 * differently from one generated by the kernel.
2746 	 */
2747 	if (SI_FROMUSER(src)) {
2748 		dest->si_pid = src->si_pid;
2749 		dest->si_ctid = src->si_ctid;
2750 		dest->si_zoneid = src->si_zoneid;
2751 		dest->si_uid = src->si_uid;
2752 		if (SI_CANQUEUE(src->si_code))
2753 			dest->si_value.sival_int =
2754 			    (int32_t)src->si_value.sival_int;
2755 		return;
2756 	}
2757 
2758 	dest->si_errno = src->si_errno;
2759 
2760 	switch (src->si_signo) {
2761 	default:
2762 		dest->si_pid = src->si_pid;
2763 		dest->si_ctid = src->si_ctid;
2764 		dest->si_zoneid = src->si_zoneid;
2765 		dest->si_uid = src->si_uid;
2766 		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
2767 		break;
2768 	case SIGCLD:
2769 		dest->si_pid = src->si_pid;
2770 		dest->si_ctid = src->si_ctid;
2771 		dest->si_zoneid = src->si_zoneid;
2772 		dest->si_status = src->si_status;
2773 		dest->si_stime = src->si_stime;
2774 		dest->si_utime = src->si_utime;
2775 		break;
2776 	case SIGSEGV:
2777 	case SIGBUS:
2778 	case SIGILL:
2779 	case SIGTRAP:
2780 	case SIGFPE:
2781 	case SIGEMT:
2782 		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
2783 		dest->si_trapno = src->si_trapno;
2784 		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
2785 		break;
2786 	case SIGPOLL:
2787 	case SIGXFSZ:
2788 		dest->si_fd = src->si_fd;
2789 		dest->si_band = src->si_band;
2790 		break;
2791 	case SIGPROF:
2792 		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
2793 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2794 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2795 		dest->si_syscall = src->si_syscall;
2796 		dest->si_nsysarg = src->si_nsysarg;
2797 		dest->si_fault = src->si_fault;
2798 		break;
2799 	}
2800 }
2801 
2802 void
2803 siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
2804 {
2805 	bzero(dest, sizeof (*dest));
2806 
2807 	/*
2808 	 * The absolute minimum content is si_signo and si_code.
2809 	 */
2810 	dest->si_signo = src->si_signo;
2811 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2812 		return;
2813 
2814 	/*
2815 	 * A siginfo generated by user level is structured
2816 	 * differently from one generated by the kernel.
2817 	 */
2818 	if (SI_FROMUSER(src)) {
2819 		dest->si_pid = src->si_pid;
2820 		dest->si_ctid = src->si_ctid;
2821 		dest->si_zoneid = src->si_zoneid;
2822 		dest->si_uid = src->si_uid;
2823 		if (SI_CANQUEUE(src->si_code))
2824 			dest->si_value.sival_int =
2825 			    (int)src->si_value.sival_int;
2826 		return;
2827 	}
2828 
2829 	dest->si_errno = src->si_errno;
2830 
2831 	switch (src->si_signo) {
2832 	default:
2833 		dest->si_pid = src->si_pid;
2834 		dest->si_ctid = src->si_ctid;
2835 		dest->si_zoneid = src->si_zoneid;
2836 		dest->si_uid = src->si_uid;
2837 		dest->si_value.sival_int = (int)src->si_value.sival_int;
2838 		break;
2839 	case SIGCLD:
2840 		dest->si_pid = src->si_pid;
2841 		dest->si_ctid = src->si_ctid;
2842 		dest->si_zoneid = src->si_zoneid;
2843 		dest->si_status = src->si_status;
2844 		dest->si_stime = src->si_stime;
2845 		dest->si_utime = src->si_utime;
2846 		break;
2847 	case SIGSEGV:
2848 	case SIGBUS:
2849 	case SIGILL:
2850 	case SIGTRAP:
2851 	case SIGFPE:
2852 	case SIGEMT:
2853 		dest->si_addr = (void *)(uintptr_t)src->si_addr;
2854 		dest->si_trapno = src->si_trapno;
2855 		dest->si_pc = (void *)(uintptr_t)src->si_pc;
2856 		break;
2857 	case SIGPOLL:
2858 	case SIGXFSZ:
2859 		dest->si_fd = src->si_fd;
2860 		dest->si_band = src->si_band;
2861 		break;
2862 	case SIGPROF:
2863 		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
2864 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2865 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2866 		dest->si_syscall = src->si_syscall;
2867 		dest->si_nsysarg = src->si_nsysarg;
2868 		dest->si_fault = src->si_fault;
2869 		break;
2870 	}
2871 }
2872 
2873 #endif /* _SYSCALL32_IMPL */
2874