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