xref: /titanic_44/usr/src/uts/common/os/sig.c (revision 90c85bf889e3af34323084f00e344a82f120b409)
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 		 * For SIGKILL make all of the lwps see the signal;
310 		 * This is needed to guarantee a sure kill for processes
311 		 * with a mix of realtime and non-realtime threads.
312 		 */
313 		int su = 0;
314 
315 		sigaddset(&p->p_sig, sig);
316 		if (ext)
317 			sigaddset(&p->p_extsig, sig);
318 		do {
319 			thread_lock(tt);
320 			if (eat_signal(tt, sig) && sig != SIGKILL) {
321 				thread_unlock(tt);
322 				break;
323 			}
324 			if (SUSPENDED(tt))
325 				su++;
326 			thread_unlock(tt);
327 		} while ((tt = tt->t_forw) != p->p_tlist);
328 		/*
329 		 * If the process is deadlocked, make somebody run and die.
330 		 */
331 		if (sig == SIGKILL && p->p_stat != SIDL &&
332 		    p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
333 		    !(p->p_proc_flag & P_PR_LOCK)) {
334 			thread_lock(tt);
335 			p->p_lwprcnt++;
336 			tt->t_schedflag |= TS_CSTART;
337 			setrun_locked(tt);
338 			thread_unlock(tt);
339 		}
340 
341 		DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
342 	}
343 }
344 
345 static int
346 isjobstop(int sig)
347 {
348 	proc_t *p = ttoproc(curthread);
349 
350 	ASSERT(MUTEX_HELD(&p->p_lock));
351 
352 	if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
353 	    sigismember(&stopdefault, sig)) {
354 		/*
355 		 * If SIGCONT has been posted since we promoted this signal
356 		 * from pending to current, then don't do a jobcontrol stop.
357 		 */
358 		if (!(p->p_flag & SSCONT) &&
359 		    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
360 		    curthread != p->p_agenttp) {
361 			sigqueue_t *sqp;
362 
363 			stop(PR_JOBCONTROL, sig);
364 			mutex_exit(&p->p_lock);
365 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
366 			mutex_enter(&pidlock);
367 			/*
368 			 * Only the first lwp to continue notifies the parent.
369 			 */
370 			if (p->p_pidflag & CLDCONT)
371 				siginfofree(sqp);
372 			else {
373 				p->p_pidflag |= CLDCONT;
374 				p->p_wcode = CLD_CONTINUED;
375 				p->p_wdata = SIGCONT;
376 				sigcld(p, sqp);
377 			}
378 			mutex_exit(&pidlock);
379 			mutex_enter(&p->p_lock);
380 		}
381 		return (1);
382 	}
383 	return (0);
384 }
385 
386 /*
387  * Returns true if the current process has a signal to process, and
388  * the signal is not held.  The signal to process is put in p_cursig.
389  * This is asked at least once each time a process enters the system
390  * (though this can usually be done without actually calling issig by
391  * checking the pending signal masks).  A signal does not do anything
392  * directly to a process; it sets a flag that asks the process to do
393  * something to itself.
394  *
395  * The "why" argument indicates the allowable side-effects of the call:
396  *
397  * FORREAL:  Extract the next pending signal from p_sig into p_cursig;
398  * stop the process if a stop has been requested or if a traced signal
399  * is pending.
400  *
401  * JUSTLOOKING:  Don't stop the process, just indicate whether or not
402  * a signal might be pending (FORREAL is needed to tell for sure).
403  *
404  * XXX: Changes to the logic in these routines should be propagated
405  * to lm_sigispending().  See bug 1201594.
406  */
407 
408 static int issig_forreal(void);
409 static int issig_justlooking(void);
410 
411 int
412 issig(int why)
413 {
414 	ASSERT(why == FORREAL || why == JUSTLOOKING);
415 
416 	return ((why == FORREAL)? issig_forreal() : issig_justlooking());
417 }
418 
419 
420 static int
421 issig_justlooking(void)
422 {
423 	kthread_t *t = curthread;
424 	klwp_t *lwp = ttolwp(t);
425 	proc_t *p = ttoproc(t);
426 	k_sigset_t set;
427 
428 	/*
429 	 * This function answers the question:
430 	 * "Is there any reason to call issig_forreal()?"
431 	 *
432 	 * We have to answer the question w/o grabbing any locks
433 	 * because we are (most likely) being called after we
434 	 * put ourselves on the sleep queue.
435 	 */
436 
437 	if (t->t_dtrace_stop | t->t_dtrace_sig)
438 		return (1);
439 
440 	/*
441 	 * Another piece of complexity in this process.  When single-stepping a
442 	 * process, we don't want an intervening signal or TP_PAUSE request to
443 	 * suspend the current thread.  Otherwise, the controlling process will
444 	 * hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
445 	 * We will trigger any remaining signals when we re-enter the kernel on
446 	 * the single step trap.
447 	 */
448 	if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
449 		return (0);
450 
451 	if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
452 	    (p->p_flag & (SEXITLWPS|SKILLED)) ||
453 	    (lwp->lwp_nostop == 0 &&
454 	    (p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
455 	    (t->t_proc_flag &
456 	    (TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
457 	    lwp->lwp_cursig)
458 		return (1);
459 
460 	if (p->p_flag & SVFWAIT)
461 		return (0);
462 	set = p->p_sig;
463 	sigorset(&set, &t->t_sig);
464 	if (schedctl_sigblock(t))	/* all blockable signals blocked */
465 		sigandset(&set, &cantmask);
466 	else
467 		sigdiffset(&set, &t->t_hold);
468 	if (p->p_flag & SVFORK)
469 		sigdiffset(&set, &holdvfork);
470 
471 	if (!sigisempty(&set)) {
472 		int sig;
473 
474 		for (sig = 1; sig < NSIG; sig++) {
475 			if (sigismember(&set, sig) &&
476 			    (tracing(p, sig) ||
477 			    !sigismember(&p->p_ignore, sig))) {
478 				/*
479 				 * Don't promote a signal that will stop
480 				 * the process when lwp_nostop is set.
481 				 */
482 				if (!lwp->lwp_nostop ||
483 				    PTOU(curproc)->u_signal[sig-1] != SIG_DFL ||
484 				    !sigismember(&stopdefault, sig))
485 					return (1);
486 			}
487 		}
488 	}
489 
490 	return (0);
491 }
492 
493 static int
494 issig_forreal(void)
495 {
496 	int sig = 0, ext = 0;
497 	kthread_t *t = curthread;
498 	klwp_t *lwp = ttolwp(t);
499 	proc_t *p = ttoproc(t);
500 	int toproc = 0;
501 	int sigcld_found = 0;
502 	int nostop_break = 0;
503 
504 	ASSERT(t->t_state == TS_ONPROC);
505 
506 	mutex_enter(&p->p_lock);
507 	schedctl_finish_sigblock(t);
508 
509 	if (t->t_dtrace_stop | t->t_dtrace_sig) {
510 		if (t->t_dtrace_stop) {
511 			/*
512 			 * If DTrace's "stop" action has been invoked on us,
513 			 * set TP_PRSTOP.
514 			 */
515 			t->t_proc_flag |= TP_PRSTOP;
516 		}
517 
518 		if (t->t_dtrace_sig != 0) {
519 			k_siginfo_t info;
520 
521 			/*
522 			 * Post the signal generated as the result of
523 			 * DTrace's "raise" action as a normal signal before
524 			 * the full-fledged signal checking begins.
525 			 */
526 			bzero(&info, sizeof (info));
527 			info.si_signo = t->t_dtrace_sig;
528 			info.si_code = SI_DTRACE;
529 
530 			sigaddq(p, NULL, &info, KM_NOSLEEP);
531 
532 			t->t_dtrace_sig = 0;
533 		}
534 	}
535 
536 	for (;;) {
537 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
538 			lwp->lwp_cursig = sig = SIGKILL;
539 			lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
540 			t->t_sig_check = 1;
541 			break;
542 		}
543 
544 		/*
545 		 * Another piece of complexity in this process.  When
546 		 * single-stepping a process, we don't want an intervening
547 		 * signal or TP_PAUSE request to suspend the current thread.
548 		 * Otherwise, the controlling process will hang beacuse we will
549 		 * be stopped with TS_PSTART set in t_schedflag.  We will
550 		 * trigger any remaining signals when we re-enter the kernel on
551 		 * the single step trap.
552 		 */
553 		if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
554 			sig = 0;
555 			break;
556 		}
557 
558 		/*
559 		 * Hold the lwp here for watchpoint manipulation.
560 		 */
561 		if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
562 			stop(PR_SUSPENDED, SUSPEND_PAUSE);
563 			continue;
564 		}
565 
566 		if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
567 			if ((sig = lwp->lwp_cursig) != 0) {
568 				/*
569 				 * Make sure we call ISSIG() in post_syscall()
570 				 * to re-validate this current signal.
571 				 */
572 				t->t_sig_check = 1;
573 			}
574 			break;
575 		}
576 
577 		/*
578 		 * If the request is PR_CHECKPOINT, ignore the rest of signals
579 		 * or requests.  Honor other stop requests or signals later.
580 		 * Go back to top of loop here to check if an exit or hold
581 		 * event has occurred while stopped.
582 		 */
583 		if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
584 			stop(PR_CHECKPOINT, 0);
585 			continue;
586 		}
587 
588 		/*
589 		 * Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
590 		 * with signals or /proc.  Another lwp is executing fork1(),
591 		 * or is undergoing watchpoint activity (remapping a page),
592 		 * or is executing lwp_suspend() on this lwp.
593 		 * Again, go back to top of loop to check if an exit
594 		 * or hold event has occurred while stopped.
595 		 */
596 		if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
597 		    (t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
598 			stop(PR_SUSPENDED, SUSPEND_NORMAL);
599 			continue;
600 		}
601 
602 		/*
603 		 * Honor requested stop before dealing with the
604 		 * current signal; a debugger may change it.
605 		 * Do not want to go back to loop here since this is a special
606 		 * stop that means: make incremental progress before the next
607 		 * stop. The danger is that returning to top of loop would most
608 		 * likely drop the thread right back here to stop soon after it
609 		 * was continued, violating the incremental progress request.
610 		 */
611 		if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
612 			stop(PR_REQUESTED, 0);
613 
614 		/*
615 		 * If a debugger wants us to take a signal it will have
616 		 * left it in lwp->lwp_cursig.  If lwp_cursig has been cleared
617 		 * or if it's being ignored, we continue on looking for another
618 		 * signal.  Otherwise we return the specified signal, provided
619 		 * it's not a signal that causes a job control stop.
620 		 *
621 		 * When stopped on PR_JOBCONTROL, there is no current
622 		 * signal; we cancel lwp->lwp_cursig temporarily before
623 		 * calling isjobstop().  The current signal may be reset
624 		 * by a debugger while we are stopped in isjobstop().
625 		 */
626 		if ((sig = lwp->lwp_cursig) != 0) {
627 			ext = lwp->lwp_extsig;
628 			lwp->lwp_cursig = 0;
629 			lwp->lwp_extsig = 0;
630 			if (!sigismember(&p->p_ignore, sig) &&
631 			    !isjobstop(sig)) {
632 				if (p->p_flag & (SEXITLWPS|SKILLED)) {
633 					sig = SIGKILL;
634 					ext = (p->p_flag & SEXTKILLED) != 0;
635 				}
636 				lwp->lwp_cursig = (uchar_t)sig;
637 				lwp->lwp_extsig = (uchar_t)ext;
638 				break;
639 			}
640 			/*
641 			 * The signal is being ignored or it caused a
642 			 * job-control stop.  If another current signal
643 			 * has not been established, return the current
644 			 * siginfo, if any, to the memory manager.
645 			 */
646 			if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
647 				siginfofree(lwp->lwp_curinfo);
648 				lwp->lwp_curinfo = NULL;
649 			}
650 			/*
651 			 * Loop around again in case we were stopped
652 			 * on a job control signal and a /proc stop
653 			 * request was posted or another current signal
654 			 * was established while we were stopped.
655 			 */
656 			continue;
657 		}
658 
659 		if (p->p_stopsig && !lwp->lwp_nostop &&
660 		    curthread != p->p_agenttp) {
661 			/*
662 			 * Some lwp in the process has already stopped
663 			 * showing PR_JOBCONTROL.  This is a stop in
664 			 * sympathy with the other lwp, even if this
665 			 * lwp is blocking the stopping signal.
666 			 */
667 			stop(PR_JOBCONTROL, p->p_stopsig);
668 			continue;
669 		}
670 
671 		/*
672 		 * Loop on the pending signals until we find a
673 		 * non-held signal that is traced or not ignored.
674 		 * First check the signals pending for the lwp,
675 		 * then the signals pending for the process as a whole.
676 		 */
677 		for (;;) {
678 			k_sigset_t tsig;
679 
680 			tsig = t->t_sig;
681 			if ((sig = fsig(&tsig, t)) != 0) {
682 				if (sig == SIGCLD)
683 					sigcld_found = 1;
684 				toproc = 0;
685 				if (tracing(p, sig) ||
686 				    !sigismember(&p->p_ignore, sig)) {
687 					if (sigismember(&t->t_extsig, sig))
688 						ext = 1;
689 					break;
690 				}
691 				sigdelset(&t->t_sig, sig);
692 				sigdelset(&t->t_extsig, sig);
693 				sigdelq(p, t, sig);
694 			} else if ((sig = fsig(&p->p_sig, t)) != 0) {
695 				if (sig == SIGCLD)
696 					sigcld_found = 1;
697 				toproc = 1;
698 				if (tracing(p, sig) ||
699 				    !sigismember(&p->p_ignore, sig)) {
700 					if (sigismember(&p->p_extsig, sig))
701 						ext = 1;
702 					break;
703 				}
704 				sigdelset(&p->p_sig, sig);
705 				sigdelset(&p->p_extsig, sig);
706 				sigdelq(p, NULL, sig);
707 			} else {
708 				/* no signal was found */
709 				break;
710 			}
711 		}
712 
713 		if (sig == 0) {	/* no signal was found */
714 			if (p->p_flag & (SEXITLWPS|SKILLED)) {
715 				lwp->lwp_cursig = SIGKILL;
716 				sig = SIGKILL;
717 				ext = (p->p_flag & SEXTKILLED) != 0;
718 			}
719 			break;
720 		}
721 
722 		/*
723 		 * If we have been informed not to stop (i.e., we are being
724 		 * called from within a network operation), then don't promote
725 		 * the signal at this time, just return the signal number.
726 		 * We will call issig() again later when it is safe.
727 		 *
728 		 * fsig() does not return a jobcontrol stopping signal
729 		 * with a default action of stopping the process if
730 		 * lwp_nostop is set, so we won't be causing a bogus
731 		 * EINTR by this action.  (Such a signal is eaten by
732 		 * isjobstop() when we loop around to do final checks.)
733 		 */
734 		if (lwp->lwp_nostop) {
735 			nostop_break = 1;
736 			break;
737 		}
738 
739 		/*
740 		 * Promote the signal from pending to current.
741 		 *
742 		 * Note that sigdeq() will set lwp->lwp_curinfo to NULL
743 		 * if no siginfo_t exists for this signal.
744 		 */
745 		lwp->lwp_cursig = (uchar_t)sig;
746 		lwp->lwp_extsig = (uchar_t)ext;
747 		t->t_sig_check = 1;	/* so post_syscall will see signal */
748 		ASSERT(lwp->lwp_curinfo == NULL);
749 		sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);
750 
751 		if (tracing(p, sig))
752 			stop(PR_SIGNALLED, sig);
753 
754 		/*
755 		 * Loop around to check for requested stop before
756 		 * performing the usual current-signal actions.
757 		 */
758 	}
759 
760 	mutex_exit(&p->p_lock);
761 
762 	/*
763 	 * If SIGCLD was dequeued, search for other pending SIGCLD's.
764 	 * Don't do it if we are returning SIGCLD and the signal
765 	 * handler will be reset by psig(); this enables reliable
766 	 * delivery of SIGCLD even when using the old, broken
767 	 * signal() interface for setting the signal handler.
768 	 */
769 	if (sigcld_found &&
770 	    (sig != SIGCLD || !sigismember(&PTOU(curproc)->u_sigresethand,
771 	    SIGCLD)))
772 		sigcld_repost();
773 
774 	if (sig != 0)
775 		(void) undo_watch_step(NULL);
776 
777 	/*
778 	 * If we have been blocked since the p_lock was dropped off
779 	 * above, then this promoted signal might have been handled
780 	 * already when we were on the way back from sleep queue, so
781 	 * just ignore it.
782 	 * If we have been informed not to stop, just return the signal
783 	 * number. Also see comments above.
784 	 */
785 	if (!nostop_break) {
786 		sig = lwp->lwp_cursig;
787 	}
788 
789 	return (sig != 0);
790 }
791 
792 /*
793  * Return true if the process is currently stopped showing PR_JOBCONTROL.
794  * This is true only if all of the process's lwp's are so stopped.
795  * If this is asked by one of the lwps in the process, exclude that lwp.
796  */
797 int
798 jobstopped(proc_t *p)
799 {
800 	kthread_t *t;
801 
802 	ASSERT(MUTEX_HELD(&p->p_lock));
803 
804 	if ((t = p->p_tlist) == NULL)
805 		return (0);
806 
807 	do {
808 		thread_lock(t);
809 		/* ignore current, zombie and suspended lwps in the test */
810 		if (!(t == curthread || t->t_state == TS_ZOMB ||
811 		    SUSPENDED(t)) &&
812 		    (t->t_state != TS_STOPPED ||
813 		    t->t_whystop != PR_JOBCONTROL)) {
814 			thread_unlock(t);
815 			return (0);
816 		}
817 		thread_unlock(t);
818 	} while ((t = t->t_forw) != p->p_tlist);
819 
820 	return (1);
821 }
822 
823 /*
824  * Put ourself (curthread) into the stopped state and notify tracers.
825  */
826 void
827 stop(int why, int what)
828 {
829 	kthread_t	*t = curthread;
830 	proc_t		*p = ttoproc(t);
831 	klwp_t		*lwp = ttolwp(t);
832 	kthread_t	*tx;
833 	lwpent_t	*lep;
834 	int		procstop;
835 	int		flags = TS_ALLSTART;
836 	hrtime_t	stoptime;
837 
838 	/*
839 	 * Can't stop a system process.
840 	 */
841 	if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
842 		return;
843 
844 	ASSERT(MUTEX_HELD(&p->p_lock));
845 
846 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
847 		/*
848 		 * Don't stop an lwp with SIGKILL pending.
849 		 * Don't stop if the process or lwp is exiting.
850 		 */
851 		if (lwp->lwp_cursig == SIGKILL ||
852 		    sigismember(&t->t_sig, SIGKILL) ||
853 		    sigismember(&p->p_sig, SIGKILL) ||
854 		    (t->t_proc_flag & TP_LWPEXIT) ||
855 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
856 			p->p_stopsig = 0;
857 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
858 			return;
859 		}
860 	}
861 
862 	/*
863 	 * Make sure we don't deadlock on a recursive call to prstop().
864 	 * prstop() sets the lwp_nostop flag.
865 	 */
866 	if (lwp->lwp_nostop)
867 		return;
868 
869 	/*
870 	 * Make sure the lwp is in an orderly state for inspection
871 	 * by a debugger through /proc or for dumping via core().
872 	 */
873 	schedctl_finish_sigblock(t);
874 	t->t_proc_flag |= TP_STOPPING;	/* must set before dropping p_lock */
875 	mutex_exit(&p->p_lock);
876 	stoptime = gethrtime();
877 	prstop(why, what);
878 	(void) undo_watch_step(NULL);
879 	mutex_enter(&p->p_lock);
880 	ASSERT(t->t_state == TS_ONPROC);
881 
882 	switch (why) {
883 	case PR_CHECKPOINT:
884 		/*
885 		 * The situation may have changed since we dropped
886 		 * and reacquired p->p_lock. Double-check now
887 		 * whether we should stop or not.
888 		 */
889 		if (!(t->t_proc_flag & TP_CHKPT)) {
890 			t->t_proc_flag &= ~TP_STOPPING;
891 			return;
892 		}
893 		t->t_proc_flag &= ~TP_CHKPT;
894 		flags &= ~TS_RESUME;
895 		break;
896 
897 	case PR_JOBCONTROL:
898 		ASSERT(what == SIGSTOP || what == SIGTSTP ||
899 		    what == SIGTTIN || what == SIGTTOU);
900 		flags &= ~TS_XSTART;
901 		break;
902 
903 	case PR_SUSPENDED:
904 		ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
905 		/*
906 		 * The situation may have changed since we dropped
907 		 * and reacquired p->p_lock.  Double-check now
908 		 * whether we should stop or not.
909 		 */
910 		if (what == SUSPEND_PAUSE) {
911 			if (!(t->t_proc_flag & TP_PAUSE)) {
912 				t->t_proc_flag &= ~TP_STOPPING;
913 				return;
914 			}
915 			flags &= ~TS_UNPAUSE;
916 		} else {
917 			if (!((t->t_proc_flag & TP_HOLDLWP) ||
918 			    (p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
919 				t->t_proc_flag &= ~TP_STOPPING;
920 				return;
921 			}
922 			/*
923 			 * If SHOLDFORK is in effect and we are stopping
924 			 * while asleep (not at the top of the stack),
925 			 * we return now to allow the hold to take effect
926 			 * when we reach the top of the kernel stack.
927 			 */
928 			if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
929 				t->t_proc_flag &= ~TP_STOPPING;
930 				return;
931 			}
932 			flags &= ~TS_CSTART;
933 		}
934 		break;
935 
936 	default:	/* /proc stop */
937 		flags &= ~TS_PSTART;
938 		/*
939 		 * Do synchronous stop unless the async-stop flag is set.
940 		 * If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
941 		 * then no debugger is present and we also do synchronous stop.
942 		 */
943 		if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
944 		    !(p->p_proc_flag & P_PR_ASYNC)) {
945 			int notify;
946 
947 			for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
948 				notify = 0;
949 				thread_lock(tx);
950 				if (ISTOPPED(tx) ||
951 				    (tx->t_proc_flag & TP_PRSTOP)) {
952 					thread_unlock(tx);
953 					continue;
954 				}
955 				tx->t_proc_flag |= TP_PRSTOP;
956 				tx->t_sig_check = 1;
957 				if (tx->t_state == TS_SLEEP &&
958 				    (tx->t_flag & T_WAKEABLE)) {
959 					/*
960 					 * Don't actually wake it up if it's
961 					 * in one of the lwp_*() syscalls.
962 					 * Mark it virtually stopped and
963 					 * notify /proc waiters (below).
964 					 */
965 					if (tx->t_wchan0 == NULL)
966 						setrun_locked(tx);
967 					else {
968 						tx->t_proc_flag |= TP_PRVSTOP;
969 						tx->t_stoptime = stoptime;
970 						notify = 1;
971 					}
972 				}
973 
974 				/* Move waiting thread to run queue */
975 				if (ISWAITING(tx))
976 					setrun_locked(tx);
977 
978 				/*
979 				 * force the thread into the kernel
980 				 * if it is not already there.
981 				 */
982 				if (tx->t_state == TS_ONPROC &&
983 				    tx->t_cpu != CPU)
984 					poke_cpu(tx->t_cpu->cpu_id);
985 				thread_unlock(tx);
986 				lep = p->p_lwpdir[tx->t_dslot].ld_entry;
987 				if (notify && lep->le_trace)
988 					prnotify(lep->le_trace);
989 			}
990 			/*
991 			 * We do this just in case one of the threads we asked
992 			 * to stop is in holdlwps() (called from cfork()) or
993 			 * lwp_suspend().
994 			 */
995 			cv_broadcast(&p->p_holdlwps);
996 		}
997 		break;
998 	}
999 
1000 	t->t_stoptime = stoptime;
1001 
1002 	if (why == PR_JOBCONTROL || (why == PR_SUSPENDED && p->p_stopsig)) {
1003 		/*
1004 		 * Determine if the whole process is jobstopped.
1005 		 */
1006 		if (jobstopped(p)) {
1007 			sigqueue_t *sqp;
1008 			int sig;
1009 
1010 			if ((sig = p->p_stopsig) == 0)
1011 				p->p_stopsig = (uchar_t)(sig = what);
1012 			mutex_exit(&p->p_lock);
1013 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1014 			mutex_enter(&pidlock);
1015 			/*
1016 			 * The last lwp to stop notifies the parent.
1017 			 * Turn off the CLDCONT flag now so the first
1018 			 * lwp to continue knows what to do.
1019 			 */
1020 			p->p_pidflag &= ~CLDCONT;
1021 			p->p_wcode = CLD_STOPPED;
1022 			p->p_wdata = sig;
1023 			sigcld(p, sqp);
1024 			/*
1025 			 * Grab p->p_lock before releasing pidlock so the
1026 			 * parent and the child don't have a race condition.
1027 			 */
1028 			mutex_enter(&p->p_lock);
1029 			mutex_exit(&pidlock);
1030 			p->p_stopsig = 0;
1031 		} else if (why == PR_JOBCONTROL && p->p_stopsig == 0) {
1032 			/*
1033 			 * Set p->p_stopsig and wake up sleeping lwps
1034 			 * so they will stop in sympathy with this lwp.
1035 			 */
1036 			p->p_stopsig = (uchar_t)what;
1037 			pokelwps(p);
1038 			/*
1039 			 * We do this just in case one of the threads we asked
1040 			 * to stop is in holdlwps() (called from cfork()) or
1041 			 * lwp_suspend().
1042 			 */
1043 			cv_broadcast(&p->p_holdlwps);
1044 		}
1045 	}
1046 
1047 	if (why != PR_JOBCONTROL && why != PR_CHECKPOINT) {
1048 		/*
1049 		 * Do process-level notification when all lwps are
1050 		 * either stopped on events of interest to /proc
1051 		 * or are stopped showing PR_SUSPENDED or are zombies.
1052 		 */
1053 		procstop = 1;
1054 		for (tx = t->t_forw; procstop && tx != t; tx = tx->t_forw) {
1055 			if (VSTOPPED(tx))
1056 				continue;
1057 			thread_lock(tx);
1058 			switch (tx->t_state) {
1059 			case TS_ZOMB:
1060 				break;
1061 			case TS_STOPPED:
1062 				/* neither ISTOPPED nor SUSPENDED? */
1063 				if ((tx->t_schedflag &
1064 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART)) ==
1065 				    (TS_CSTART | TS_UNPAUSE | TS_PSTART))
1066 					procstop = 0;
1067 				break;
1068 			case TS_SLEEP:
1069 				/* not paused for watchpoints? */
1070 				if (!(tx->t_flag & T_WAKEABLE) ||
1071 				    tx->t_wchan0 == NULL ||
1072 				    !(tx->t_proc_flag & TP_PAUSE))
1073 					procstop = 0;
1074 				break;
1075 			default:
1076 				procstop = 0;
1077 				break;
1078 			}
1079 			thread_unlock(tx);
1080 		}
1081 		if (procstop) {
1082 			/* there must not be any remapped watched pages now */
1083 			ASSERT(p->p_mapcnt == 0);
1084 			if (p->p_proc_flag & P_PR_PTRACE) {
1085 				/* ptrace() compatibility */
1086 				mutex_exit(&p->p_lock);
1087 				mutex_enter(&pidlock);
1088 				p->p_wcode = CLD_TRAPPED;
1089 				p->p_wdata = (why == PR_SIGNALLED)?
1090 				    what : SIGTRAP;
1091 				cv_broadcast(&p->p_parent->p_cv);
1092 				/*
1093 				 * Grab p->p_lock before releasing pidlock so
1094 				 * parent and child don't have a race condition.
1095 				 */
1096 				mutex_enter(&p->p_lock);
1097 				mutex_exit(&pidlock);
1098 			}
1099 			if (p->p_trace)			/* /proc */
1100 				prnotify(p->p_trace);
1101 			cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */
1102 			cv_broadcast(&p->p_holdlwps);	/* holdwatch() */
1103 		}
1104 		if (why != PR_SUSPENDED) {
1105 			lep = p->p_lwpdir[t->t_dslot].ld_entry;
1106 			if (lep->le_trace)		/* /proc */
1107 				prnotify(lep->le_trace);
1108 			/*
1109 			 * Special notification for creation of the agent lwp.
1110 			 */
1111 			if (t == p->p_agenttp &&
1112 			    (t->t_proc_flag & TP_PRSTOP) &&
1113 			    p->p_trace)
1114 				prnotify(p->p_trace);
1115 			/*
1116 			 * The situation may have changed since we dropped
1117 			 * and reacquired p->p_lock. Double-check now
1118 			 * whether we should stop or not.
1119 			 */
1120 			if (!(t->t_proc_flag & TP_STOPPING)) {
1121 				if (t->t_proc_flag & TP_PRSTOP)
1122 					t->t_proc_flag |= TP_STOPPING;
1123 			}
1124 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
1125 			prnostep(lwp);
1126 		}
1127 	}
1128 
1129 	if (why == PR_SUSPENDED) {
1130 
1131 		/*
1132 		 * We always broadcast in the case of SUSPEND_PAUSE.  This is
1133 		 * because checks for TP_PAUSE take precedence over checks for
1134 		 * SHOLDWATCH.  If a thread is trying to stop because of
1135 		 * SUSPEND_PAUSE and tries to do a holdwatch(), it will be
1136 		 * waiting for the rest of the threads to enter a stopped state.
1137 		 * If we are stopping for a SUSPEND_PAUSE, we may be the last
1138 		 * lwp and not know it, so broadcast just in case.
1139 		 */
1140 		if (what == SUSPEND_PAUSE ||
1141 		    --p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP))
1142 			cv_broadcast(&p->p_holdlwps);
1143 
1144 	}
1145 
1146 	/*
1147 	 * Need to do this here (rather than after the thread is officially
1148 	 * stopped) because we can't call mutex_enter from a stopped thread.
1149 	 */
1150 	if (why == PR_CHECKPOINT)
1151 		del_one_utstop();
1152 
1153 	thread_lock(t);
1154 	ASSERT((t->t_schedflag & TS_ALLSTART) == 0);
1155 	t->t_schedflag |= flags;
1156 	t->t_whystop = (short)why;
1157 	t->t_whatstop = (short)what;
1158 	CL_STOP(t, why, what);
1159 	(void) new_mstate(t, LMS_STOPPED);
1160 	thread_stop(t);			/* set stop state and drop lock */
1161 
1162 	if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
1163 		/*
1164 		 * We may have gotten a SIGKILL or a SIGCONT when
1165 		 * we released p->p_lock; make one last check.
1166 		 * Also check for a /proc run-on-last-close.
1167 		 */
1168 		if (sigismember(&t->t_sig, SIGKILL) ||
1169 		    sigismember(&p->p_sig, SIGKILL) ||
1170 		    (t->t_proc_flag & TP_LWPEXIT) ||
1171 		    (p->p_flag & (SEXITLWPS|SKILLED))) {
1172 			p->p_stopsig = 0;
1173 			thread_lock(t);
1174 			t->t_schedflag |= TS_XSTART | TS_PSTART;
1175 			setrun_locked(t);
1176 			thread_unlock_nopreempt(t);
1177 		} else if (why == PR_JOBCONTROL) {
1178 			if (p->p_flag & SSCONT) {
1179 				/*
1180 				 * This resulted from a SIGCONT posted
1181 				 * while we were not holding p->p_lock.
1182 				 */
1183 				p->p_stopsig = 0;
1184 				thread_lock(t);
1185 				t->t_schedflag |= TS_XSTART;
1186 				setrun_locked(t);
1187 				thread_unlock_nopreempt(t);
1188 			}
1189 		} else if (!(t->t_proc_flag & TP_STOPPING)) {
1190 			/*
1191 			 * This resulted from a /proc run-on-last-close.
1192 			 */
1193 			thread_lock(t);
1194 			t->t_schedflag |= TS_PSTART;
1195 			setrun_locked(t);
1196 			thread_unlock_nopreempt(t);
1197 		}
1198 	}
1199 
1200 	t->t_proc_flag &= ~TP_STOPPING;
1201 	mutex_exit(&p->p_lock);
1202 
1203 	swtch();
1204 	setallwatch();	/* reestablish any watchpoints set while stopped */
1205 	mutex_enter(&p->p_lock);
1206 	prbarrier(p);	/* barrier against /proc locking */
1207 }
1208 
1209 /* Interface for resetting user thread stop count. */
1210 void
1211 utstop_init(void)
1212 {
1213 	mutex_enter(&thread_stop_lock);
1214 	num_utstop = 0;
1215 	mutex_exit(&thread_stop_lock);
1216 }
1217 
1218 /* Interface for registering a user thread stop request. */
1219 void
1220 add_one_utstop(void)
1221 {
1222 	mutex_enter(&thread_stop_lock);
1223 	num_utstop++;
1224 	mutex_exit(&thread_stop_lock);
1225 }
1226 
1227 /* Interface for cancelling a user thread stop request */
1228 void
1229 del_one_utstop(void)
1230 {
1231 	mutex_enter(&thread_stop_lock);
1232 	num_utstop--;
1233 	if (num_utstop == 0)
1234 		cv_broadcast(&utstop_cv);
1235 	mutex_exit(&thread_stop_lock);
1236 }
1237 
1238 /* Interface to wait for all user threads to be stopped */
1239 void
1240 utstop_timedwait(clock_t ticks)
1241 {
1242 	mutex_enter(&thread_stop_lock);
1243 	if (num_utstop > 0)
1244 		(void) cv_timedwait(&utstop_cv, &thread_stop_lock,
1245 		    ticks + lbolt);
1246 	mutex_exit(&thread_stop_lock);
1247 }
1248 
1249 /*
1250  * Perform the action specified by the current signal.
1251  * The usual sequence is:
1252  * 	if (issig())
1253  * 		psig();
1254  * The signal bit has already been cleared by issig(),
1255  * the current signal number has been stored in lwp_cursig,
1256  * and the current siginfo is now referenced by lwp_curinfo.
1257  */
1258 void
1259 psig(void)
1260 {
1261 	kthread_t *t = curthread;
1262 	proc_t *p = ttoproc(t);
1263 	klwp_t *lwp = ttolwp(t);
1264 	void (*func)();
1265 	int sig, rc, code, ext;
1266 	pid_t pid = -1;
1267 	id_t ctid = 0;
1268 	zoneid_t zoneid = -1;
1269 	sigqueue_t *sqp = NULL;
1270 
1271 	mutex_enter(&p->p_lock);
1272 	schedctl_finish_sigblock(t);
1273 	code = CLD_KILLED;
1274 
1275 	if (p->p_flag & SEXITLWPS) {
1276 		lwp_exit();
1277 		return;			/* not reached */
1278 	}
1279 	sig = lwp->lwp_cursig;
1280 	ext = lwp->lwp_extsig;
1281 
1282 	ASSERT(sig < NSIG);
1283 
1284 	/*
1285 	 * Re-check lwp_cursig after we acquire p_lock.  Since p_lock was
1286 	 * dropped between issig() and psig(), a debugger may have cleared
1287 	 * lwp_cursig via /proc in the intervening window.
1288 	 */
1289 	if (sig == 0) {
1290 		if (lwp->lwp_curinfo) {
1291 			siginfofree(lwp->lwp_curinfo);
1292 			lwp->lwp_curinfo = NULL;
1293 		}
1294 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
1295 			t->t_flag &= ~T_TOMASK;
1296 			t->t_hold = lwp->lwp_sigoldmask;
1297 		}
1298 		mutex_exit(&p->p_lock);
1299 		return;
1300 	}
1301 	func = PTOU(curproc)->u_signal[sig-1];
1302 
1303 	/*
1304 	 * The signal disposition could have changed since we promoted
1305 	 * this signal from pending to current (we dropped p->p_lock).
1306 	 * This can happen only in a multi-threaded process.
1307 	 */
1308 	if (sigismember(&p->p_ignore, sig) ||
1309 	    (func == SIG_DFL && sigismember(&stopdefault, sig))) {
1310 		lwp->lwp_cursig = 0;
1311 		lwp->lwp_extsig = 0;
1312 		if (lwp->lwp_curinfo) {
1313 			siginfofree(lwp->lwp_curinfo);
1314 			lwp->lwp_curinfo = NULL;
1315 		}
1316 		if (t->t_flag & T_TOMASK) {	/* sigsuspend or pollsys */
1317 			t->t_flag &= ~T_TOMASK;
1318 			t->t_hold = lwp->lwp_sigoldmask;
1319 		}
1320 		mutex_exit(&p->p_lock);
1321 		return;
1322 	}
1323 
1324 	/*
1325 	 * We check lwp_curinfo first since pr_setsig can actually
1326 	 * stuff a sigqueue_t there for SIGKILL.
1327 	 */
1328 	if (lwp->lwp_curinfo) {
1329 		sqp = lwp->lwp_curinfo;
1330 	} else if (sig == SIGKILL && p->p_killsqp) {
1331 		sqp = p->p_killsqp;
1332 	}
1333 
1334 	if (sqp != NULL) {
1335 		if (SI_FROMUSER(&sqp->sq_info)) {
1336 			pid = sqp->sq_info.si_pid;
1337 			ctid = sqp->sq_info.si_ctid;
1338 			zoneid = sqp->sq_info.si_zoneid;
1339 		}
1340 		/*
1341 		 * If we have a sigqueue_t, its sq_external value
1342 		 * trumps the lwp_extsig value.  It is theoretically
1343 		 * possible to make lwp_extsig reflect reality, but it
1344 		 * would unnecessarily complicate things elsewhere.
1345 		 */
1346 		ext = sqp->sq_external;
1347 	}
1348 
1349 	if (func == SIG_DFL) {
1350 		mutex_exit(&p->p_lock);
1351 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
1352 		    NULL, void (*)(void), func);
1353 	} else {
1354 		k_siginfo_t *sip = NULL;
1355 
1356 		/*
1357 		 * If DTrace user-land tracing is active, give DTrace a
1358 		 * chance to defer the signal until after tracing is
1359 		 * complete.
1360 		 */
1361 		if (t->t_dtrace_on && dtrace_safe_defer_signal()) {
1362 			mutex_exit(&p->p_lock);
1363 			return;
1364 		}
1365 
1366 		/*
1367 		 * save siginfo pointer here, in case the
1368 		 * the signal's reset bit is on
1369 		 *
1370 		 * The presence of a current signal prevents paging
1371 		 * from succeeding over a network.  We copy the current
1372 		 * signal information to the side and cancel the current
1373 		 * signal so that sendsig() will succeed.
1374 		 */
1375 		if (sigismember(&p->p_siginfo, sig)) {
1376 			sip = &lwp->lwp_siginfo;
1377 			if (sqp) {
1378 				bcopy(&sqp->sq_info, sip, sizeof (*sip));
1379 			} else if (sig == SIGPROF && sip->si_signo == SIGPROF &&
1380 			    t->t_rprof != NULL && t->t_rprof->rp_anystate) {
1381 				/* EMPTY */;
1382 			} else {
1383 				bzero(sip, sizeof (*sip));
1384 				sip->si_signo = sig;
1385 				sip->si_code = SI_NOINFO;
1386 			}
1387 		}
1388 
1389 		if (t->t_flag & T_TOMASK)
1390 			t->t_flag &= ~T_TOMASK;
1391 		else
1392 			lwp->lwp_sigoldmask = t->t_hold;
1393 		sigorset(&t->t_hold, &PTOU(curproc)->u_sigmask[sig-1]);
1394 		if (!sigismember(&PTOU(curproc)->u_signodefer, sig))
1395 			sigaddset(&t->t_hold, sig);
1396 		if (sigismember(&PTOU(curproc)->u_sigresethand, sig))
1397 			setsigact(sig, SIG_DFL, nullsmask, 0);
1398 
1399 		DTRACE_PROC3(signal__handle, int, sig, k_siginfo_t *,
1400 		    sip, void (*)(void), func);
1401 
1402 		lwp->lwp_cursig = 0;
1403 		lwp->lwp_extsig = 0;
1404 		if (lwp->lwp_curinfo) {
1405 			/* p->p_killsqp is freed by freeproc */
1406 			siginfofree(lwp->lwp_curinfo);
1407 			lwp->lwp_curinfo = NULL;
1408 		}
1409 		mutex_exit(&p->p_lock);
1410 		lwp->lwp_ru.nsignals++;
1411 
1412 		if (p->p_model == DATAMODEL_NATIVE)
1413 			rc = sendsig(sig, sip, func);
1414 #ifdef _SYSCALL32_IMPL
1415 		else
1416 			rc = sendsig32(sig, sip, func);
1417 #endif	/* _SYSCALL32_IMPL */
1418 		if (rc)
1419 			return;
1420 		sig = lwp->lwp_cursig = SIGSEGV;
1421 		ext = 0;	/* lwp_extsig was set above */
1422 		pid = -1;
1423 		ctid = 0;
1424 	}
1425 
1426 	if (sigismember(&coredefault, sig)) {
1427 		/*
1428 		 * Terminate all LWPs but don't discard them.
1429 		 * If another lwp beat us to the punch by calling exit(),
1430 		 * evaporate now.
1431 		 */
1432 		proc_is_exiting(p);
1433 		if (exitlwps(1) != 0) {
1434 			mutex_enter(&p->p_lock);
1435 			lwp_exit();
1436 		}
1437 		/* if we got a SIGKILL from anywhere, no core dump */
1438 		if (p->p_flag & SKILLED) {
1439 			sig = SIGKILL;
1440 			ext = (p->p_flag & SEXTKILLED) != 0;
1441 		} else {
1442 #ifdef C2_AUDIT
1443 			if (audit_active)		/* audit core dump */
1444 				audit_core_start(sig);
1445 #endif
1446 			if (core(sig, ext) == 0)
1447 				code = CLD_DUMPED;
1448 #ifdef C2_AUDIT
1449 			if (audit_active)		/* audit core dump */
1450 				audit_core_finish(code);
1451 #endif
1452 		}
1453 	}
1454 
1455 	/*
1456 	 * Generate a contract event once if the process is killed
1457 	 * by a signal.
1458 	 */
1459 	if (ext) {
1460 		proc_is_exiting(p);
1461 		if (exitlwps(0) != 0) {
1462 			mutex_enter(&p->p_lock);
1463 			lwp_exit();
1464 		}
1465 		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
1466 		    zoneid);
1467 	}
1468 
1469 	exit(code, sig);
1470 }
1471 
1472 /*
1473  * Find next unheld signal in ssp for thread t.
1474  */
1475 int
1476 fsig(k_sigset_t *ssp, kthread_t *t)
1477 {
1478 	proc_t *p = ttoproc(t);
1479 	user_t *up = PTOU(p);
1480 	int i;
1481 	k_sigset_t temp;
1482 
1483 	ASSERT(MUTEX_HELD(&p->p_lock));
1484 
1485 	/*
1486 	 * Don't promote any signals for the parent of a vfork()d
1487 	 * child that hasn't yet released the parent's memory.
1488 	 */
1489 	if (p->p_flag & SVFWAIT)
1490 		return (0);
1491 
1492 	temp = *ssp;
1493 	sigdiffset(&temp, &t->t_hold);
1494 
1495 	/*
1496 	 * Don't promote stopping signals (except SIGSTOP) for a child
1497 	 * of vfork() that hasn't yet released the parent's memory.
1498 	 */
1499 	if (p->p_flag & SVFORK)
1500 		sigdiffset(&temp, &holdvfork);
1501 
1502 	/*
1503 	 * Don't promote a signal that will stop
1504 	 * the process when lwp_nostop is set.
1505 	 */
1506 	if (ttolwp(t)->lwp_nostop) {
1507 		sigdelset(&temp, SIGSTOP);
1508 		if (!p->p_pgidp->pid_pgorphaned) {
1509 			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
1510 				sigdelset(&temp, SIGTSTP);
1511 			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
1512 				sigdelset(&temp, SIGTTIN);
1513 			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
1514 				sigdelset(&temp, SIGTTOU);
1515 		}
1516 	}
1517 
1518 	/*
1519 	 * Choose SIGKILL and SIGPROF before all other pending signals.
1520 	 * The rest are promoted in signal number order.
1521 	 */
1522 	if (sigismember(&temp, SIGKILL))
1523 		return (SIGKILL);
1524 	if (sigismember(&temp, SIGPROF))
1525 		return (SIGPROF);
1526 
1527 	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
1528 		if (temp.__sigbits[i])
1529 			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
1530 			    lowbit(temp.__sigbits[i]));
1531 	}
1532 
1533 	return (0);
1534 }
1535 
1536 void
1537 setsigact(int sig, void (*disp)(), k_sigset_t mask, int flags)
1538 {
1539 	proc_t *p = ttoproc(curthread);
1540 	kthread_t *t;
1541 
1542 	ASSERT(MUTEX_HELD(&p->p_lock));
1543 
1544 	PTOU(curproc)->u_signal[sig - 1] = disp;
1545 
1546 	/*
1547 	 * Honor the SA_SIGINFO flag if the signal is being caught.
1548 	 * Force the SA_SIGINFO flag if the signal is not being caught.
1549 	 * This is necessary to make sigqueue() and sigwaitinfo() work
1550 	 * properly together when the signal is set to default or is
1551 	 * being temporarily ignored.
1552 	 */
1553 	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
1554 		sigaddset(&p->p_siginfo, sig);
1555 	else
1556 		sigdelset(&p->p_siginfo, sig);
1557 
1558 	if (disp != SIG_DFL && disp != SIG_IGN) {
1559 		sigdelset(&p->p_ignore, sig);
1560 		PTOU(curproc)->u_sigmask[sig - 1] = mask;
1561 		if (!sigismember(&cantreset, sig)) {
1562 			if (flags & SA_RESETHAND)
1563 				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
1564 			else
1565 				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
1566 		}
1567 		if (flags & SA_NODEFER)
1568 			sigaddset(&PTOU(curproc)->u_signodefer, sig);
1569 		else
1570 			sigdelset(&PTOU(curproc)->u_signodefer, sig);
1571 		if (flags & SA_RESTART)
1572 			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
1573 		else
1574 			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
1575 		if (flags & SA_ONSTACK)
1576 			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
1577 		else
1578 			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
1579 
1580 	} else if (disp == SIG_IGN ||
1581 	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
1582 		/*
1583 		 * Setting the signal action to SIG_IGN results in the
1584 		 * discarding of all pending signals of that signal number.
1585 		 * Setting the signal action to SIG_DFL does the same *only*
1586 		 * if the signal's default behavior is to be ignored.
1587 		 */
1588 		sigaddset(&p->p_ignore, sig);
1589 		sigdelset(&p->p_sig, sig);
1590 		sigdelset(&p->p_extsig, sig);
1591 		sigdelq(p, NULL, sig);
1592 		t = p->p_tlist;
1593 		do {
1594 			sigdelset(&t->t_sig, sig);
1595 			sigdelset(&t->t_extsig, sig);
1596 			sigdelq(p, t, sig);
1597 		} while ((t = t->t_forw) != p->p_tlist);
1598 
1599 	} else {
1600 		/*
1601 		 * The signal action is being set to SIG_DFL and the default
1602 		 * behavior is to do something: make sure it is not ignored.
1603 		 */
1604 		sigdelset(&p->p_ignore, sig);
1605 	}
1606 
1607 	if (sig == SIGCLD) {
1608 		if (flags & SA_NOCLDWAIT)
1609 			p->p_flag |= SNOWAIT;
1610 		else
1611 			p->p_flag &= ~SNOWAIT;
1612 
1613 		if (flags & SA_NOCLDSTOP)
1614 			p->p_flag &= ~SJCTL;
1615 		else
1616 			p->p_flag |= SJCTL;
1617 
1618 		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
1619 			proc_t *cp, *tp;
1620 
1621 			mutex_exit(&p->p_lock);
1622 			mutex_enter(&pidlock);
1623 			for (cp = p->p_child; cp != NULL; cp = tp) {
1624 				tp = cp->p_sibling;
1625 				if (cp->p_stat == SZOMB &&
1626 				    !(cp->p_pidflag & CLDWAITPID))
1627 					freeproc(cp);
1628 			}
1629 			mutex_exit(&pidlock);
1630 			mutex_enter(&p->p_lock);
1631 		}
1632 	}
1633 }
1634 
1635 /*
1636  * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
1637  * Called from exec_common() for a process undergoing execve()
1638  * and from cfork() for a newly-created child of vfork().
1639  * In the vfork() case, 'p' is not the current process.
1640  * In both cases, there is only one thread in the process.
1641  */
1642 void
1643 sigdefault(proc_t *p)
1644 {
1645 	kthread_t *t = p->p_tlist;
1646 	struct user *up = PTOU(p);
1647 	int sig;
1648 
1649 	ASSERT(MUTEX_HELD(&p->p_lock));
1650 
1651 	for (sig = 1; sig < NSIG; sig++) {
1652 		if (up->u_signal[sig - 1] != SIG_DFL &&
1653 		    up->u_signal[sig - 1] != SIG_IGN) {
1654 			up->u_signal[sig - 1] = SIG_DFL;
1655 			sigemptyset(&up->u_sigmask[sig - 1]);
1656 			if (sigismember(&ignoredefault, sig)) {
1657 				sigdelq(p, NULL, sig);
1658 				sigdelq(p, t, sig);
1659 			}
1660 			if (sig == SIGCLD)
1661 				p->p_flag &= ~(SNOWAIT|SJCTL);
1662 		}
1663 	}
1664 	sigorset(&p->p_ignore, &ignoredefault);
1665 	sigfillset(&p->p_siginfo);
1666 	sigdiffset(&p->p_siginfo, &cantmask);
1667 	sigdiffset(&p->p_sig, &ignoredefault);
1668 	sigdiffset(&p->p_extsig, &ignoredefault);
1669 	sigdiffset(&t->t_sig, &ignoredefault);
1670 	sigdiffset(&t->t_extsig, &ignoredefault);
1671 }
1672 
1673 void
1674 sigcld(proc_t *cp, sigqueue_t *sqp)
1675 {
1676 	proc_t *pp = cp->p_parent;
1677 
1678 	ASSERT(MUTEX_HELD(&pidlock));
1679 
1680 	switch (cp->p_wcode) {
1681 	case CLD_EXITED:
1682 	case CLD_DUMPED:
1683 	case CLD_KILLED:
1684 		ASSERT(cp->p_stat == SZOMB);
1685 		/*
1686 		 * The broadcast on p_srwchan_cv is a kludge to
1687 		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
1688 		 */
1689 		cv_broadcast(&cp->p_srwchan_cv);
1690 
1691 		/*
1692 		 * Add to newstate list of the parent
1693 		 */
1694 		add_ns(pp, cp);
1695 
1696 		cv_broadcast(&pp->p_cv);
1697 		if ((pp->p_flag & SNOWAIT) ||
1698 		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
1699 			if (!(cp->p_pidflag & CLDWAITPID))
1700 				freeproc(cp);
1701 		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
1702 			post_sigcld(cp, sqp);
1703 			sqp = NULL;
1704 		}
1705 		break;
1706 
1707 	case CLD_STOPPED:
1708 	case CLD_CONTINUED:
1709 		cv_broadcast(&pp->p_cv);
1710 		if (pp->p_flag & SJCTL) {
1711 			post_sigcld(cp, sqp);
1712 			sqp = NULL;
1713 		}
1714 		break;
1715 	}
1716 
1717 	if (sqp)
1718 		siginfofree(sqp);
1719 }
1720 
1721 /*
1722  * Common code called from sigcld() and issig_forreal()
1723  * Give the parent process a SIGCLD if it does not have one pending,
1724  * else mark the child process so a SIGCLD can be posted later.
1725  */
1726 static void
1727 post_sigcld(proc_t *cp, sigqueue_t *sqp)
1728 {
1729 	proc_t *pp = cp->p_parent;
1730 	void (*handler)() = PTOU(pp)->u_signal[SIGCLD - 1];
1731 	k_siginfo_t info;
1732 
1733 	ASSERT(MUTEX_HELD(&pidlock));
1734 	mutex_enter(&pp->p_lock);
1735 
1736 	/*
1737 	 * If a SIGCLD is pending, or if SIGCLD is not now being caught,
1738 	 * then just mark the child process so that its SIGCLD will
1739 	 * be posted later, when the first SIGCLD is taken off the
1740 	 * queue or when the parent is ready to receive it, if ever.
1741 	 */
1742 	if (handler == SIG_DFL || handler == SIG_IGN ||
1743 	    sigismember(&pp->p_sig, SIGCLD))
1744 		cp->p_pidflag |= CLDPEND;
1745 	else {
1746 		cp->p_pidflag &= ~CLDPEND;
1747 		if (sqp == NULL) {
1748 			/*
1749 			 * This can only happen when the parent is init.
1750 			 * (See call to sigcld(q, NULL) in exit().)
1751 			 * Use KM_NOSLEEP to avoid deadlock.
1752 			 */
1753 			ASSERT(pp == proc_init);
1754 			winfo(cp, &info, 0);
1755 			sigaddq(pp, NULL, &info, KM_NOSLEEP);
1756 		} else {
1757 			winfo(cp, &sqp->sq_info, 0);
1758 			sigaddqa(pp, NULL, sqp);
1759 			sqp = NULL;
1760 		}
1761 	}
1762 
1763 	mutex_exit(&pp->p_lock);
1764 
1765 	if (sqp)
1766 		siginfofree(sqp);
1767 }
1768 
1769 /*
1770  * Search for a child that has a pending SIGCLD for us, the parent.
1771  * The queue of SIGCLD signals is implied by the list of children.
1772  * We post the SIGCLD signals one at a time so they don't get lost.
1773  * When one is dequeued, another is enqueued, until there are no more.
1774  */
1775 void
1776 sigcld_repost()
1777 {
1778 	proc_t *pp = curproc;
1779 	proc_t *cp;
1780 	void (*handler)() = PTOU(pp)->u_signal[SIGCLD - 1];
1781 	sigqueue_t *sqp;
1782 
1783 	/*
1784 	 * Don't bother if SIGCLD is not now being caught.
1785 	 */
1786 	if (handler == SIG_DFL || handler == SIG_IGN)
1787 		return;
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, k_sigset_t *s2)
2171 {
2172 	s1->__sigbits[0] |= s2->__sigbits[0];
2173 	s1->__sigbits[1] |= s2->__sigbits[1];
2174 }
2175 
2176 void
2177 sigandset(k_sigset_t *s1, k_sigset_t *s2)
2178 {
2179 	s1->__sigbits[0] &= s2->__sigbits[0];
2180 	s1->__sigbits[1] &= s2->__sigbits[1];
2181 }
2182 
2183 void
2184 sigdiffset(k_sigset_t *s1, k_sigset_t *s2)
2185 {
2186 	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
2187 	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
2188 }
2189 
2190 /*
2191  * Return non-zero if curthread->t_sig_check should be set to 1, that is,
2192  * if there are any signals the thread might take on return from the kernel.
2193  * If ksigset_t's were a single word, we would do:
2194  *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
2195  */
2196 int
2197 sigcheck(proc_t *p, kthread_t *t)
2198 {
2199 	sc_shared_t *tdp = t->t_schedctl;
2200 
2201 	/*
2202 	 * If signals are blocked via the schedctl interface
2203 	 * then we only check for the unmaskable signals.
2204 	 */
2205 	if (tdp != NULL && tdp->sc_sigblock)
2206 		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2207 		    CANTMASK0);
2208 
2209 	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2210 	    ~t->t_hold.__sigbits[0]) |
2211 	    (((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
2212 	    ~t->t_hold.__sigbits[1]) & FILLSET1));
2213 }
2214 
2215 /* ONC_PLUS EXTRACT START */
2216 void
2217 sigintr(k_sigset_t *smask, int intable)
2218 {
2219 	proc_t *p;
2220 	int owned;
2221 	k_sigset_t lmask;		/* local copy of cantmask */
2222 	klwp_t *lwp = ttolwp(curthread);
2223 
2224 	/*
2225 	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
2226 	 *    and SIGTERM. (Preserving the existing masks).
2227 	 *    This function supports the -intr nfs and ufs mount option.
2228 	 */
2229 
2230 	/*
2231 	 * don't do kernel threads
2232 	 */
2233 	if (lwp == NULL)
2234 		return;
2235 
2236 	/*
2237 	 * get access to signal mask
2238 	 */
2239 	p = ttoproc(curthread);
2240 	owned = mutex_owned(&p->p_lock);	/* this is filthy */
2241 	if (!owned)
2242 		mutex_enter(&p->p_lock);
2243 
2244 	/*
2245 	 * remember the current mask
2246 	 */
2247 	schedctl_finish_sigblock(curthread);
2248 	*smask = curthread->t_hold;
2249 
2250 	/*
2251 	 * mask out all signals
2252 	 */
2253 	sigfillset(&curthread->t_hold);
2254 
2255 	/*
2256 	 * Unmask the non-maskable signals (e.g., KILL), as long as
2257 	 * they aren't already masked (which could happen at exit).
2258 	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
2259 	 * second sets the current hold mask to (~0 & ~lmask), which reduces
2260 	 * to (~cantmask | curhold).
2261 	 */
2262 	lmask = cantmask;
2263 	sigdiffset(&lmask, smask);
2264 	sigdiffset(&curthread->t_hold, &lmask);
2265 
2266 	/*
2267 	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
2268 	 * Re-enable INT if it's originally enabled and the NFS mount option
2269 	 * nointr is not set.
2270 	 */
2271 	if (!sigismember(smask, SIGHUP))
2272 		sigdelset(&curthread->t_hold, SIGHUP);
2273 	if (!sigismember(smask, SIGINT) && intable)
2274 		sigdelset(&curthread->t_hold, SIGINT);
2275 	if (!sigismember(smask, SIGQUIT))
2276 		sigdelset(&curthread->t_hold, SIGQUIT);
2277 	if (!sigismember(smask, SIGTERM))
2278 		sigdelset(&curthread->t_hold, SIGTERM);
2279 
2280 	/*
2281 	 * release access to signal mask
2282 	 */
2283 	if (!owned)
2284 		mutex_exit(&p->p_lock);
2285 
2286 	/*
2287 	 * Indicate that this lwp is not to be stopped.
2288 	 */
2289 	lwp->lwp_nostop++;
2290 
2291 }
2292 /* ONC_PLUS EXTRACT END */
2293 
2294 void
2295 sigunintr(k_sigset_t *smask)
2296 {
2297 	proc_t *p;
2298 	int owned;
2299 	klwp_t *lwp = ttolwp(curthread);
2300 
2301 	/*
2302 	 * Reset previous mask (See sigintr() above)
2303 	 */
2304 	if (lwp != NULL) {
2305 		lwp->lwp_nostop--;	/* restore lwp stoppability */
2306 		p = ttoproc(curthread);
2307 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2308 		if (!owned)
2309 			mutex_enter(&p->p_lock);
2310 		curthread->t_hold = *smask;
2311 		/* so unmasked signals will be seen */
2312 		curthread->t_sig_check = 1;
2313 		if (!owned)
2314 			mutex_exit(&p->p_lock);
2315 	}
2316 }
2317 
2318 void
2319 sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
2320 {
2321 	proc_t	*p;
2322 	int owned;
2323 	/*
2324 	 * Save current signal mask in oldmask, then
2325 	 * set it to newmask.
2326 	 */
2327 	if (ttolwp(curthread) != NULL) {
2328 		p = ttoproc(curthread);
2329 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2330 		if (!owned)
2331 			mutex_enter(&p->p_lock);
2332 		schedctl_finish_sigblock(curthread);
2333 		if (oldmask != NULL)
2334 			*oldmask = curthread->t_hold;
2335 		curthread->t_hold = *newmask;
2336 		curthread->t_sig_check = 1;
2337 		if (!owned)
2338 			mutex_exit(&p->p_lock);
2339 	}
2340 }
2341 
2342 /*
2343  * Return true if the signal number is in range
2344  * and the signal code specifies signal queueing.
2345  */
2346 int
2347 sigwillqueue(int sig, int code)
2348 {
2349 	if (sig >= 0 && sig < NSIG) {
2350 		switch (code) {
2351 		case SI_QUEUE:
2352 		case SI_TIMER:
2353 		case SI_ASYNCIO:
2354 		case SI_MESGQ:
2355 			return (1);
2356 		}
2357 	}
2358 	return (0);
2359 }
2360 
2361 #ifndef	UCHAR_MAX
2362 #define	UCHAR_MAX	255
2363 #endif
2364 
2365 /*
2366  * The entire pool (with maxcount entries) is pre-allocated at
2367  * the first sigqueue/signotify call.
2368  */
2369 sigqhdr_t *
2370 sigqhdralloc(size_t size, uint_t maxcount)
2371 {
2372 	size_t i;
2373 	sigqueue_t *sq, *next;
2374 	sigqhdr_t *sqh;
2375 
2376 	i = (maxcount * size) + sizeof (sigqhdr_t);
2377 	ASSERT(maxcount <= UCHAR_MAX && i <= USHRT_MAX);
2378 	sqh = kmem_alloc(i, KM_SLEEP);
2379 	sqh->sqb_count = (uchar_t)maxcount;
2380 	sqh->sqb_maxcount = (uchar_t)maxcount;
2381 	sqh->sqb_size = (ushort_t)i;
2382 	sqh->sqb_pexited = 0;
2383 	sqh->sqb_sent = 0;
2384 	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
2385 	for (i = maxcount - 1; i != 0; i--) {
2386 		next = (sigqueue_t *)((uintptr_t)sq + size);
2387 		sq->sq_next = next;
2388 		sq = next;
2389 	}
2390 	sq->sq_next = NULL;
2391 	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
2392 	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
2393 	return (sqh);
2394 }
2395 
2396 static void sigqrel(sigqueue_t *);
2397 
2398 /*
2399  * allocate a sigqueue/signotify structure from the per process
2400  * pre-allocated pool.
2401  */
2402 sigqueue_t *
2403 sigqalloc(sigqhdr_t *sqh)
2404 {
2405 	sigqueue_t *sq = NULL;
2406 
2407 	ASSERT(MUTEX_HELD(&curproc->p_lock));
2408 
2409 	if (sqh != NULL) {
2410 		mutex_enter(&sqh->sqb_lock);
2411 		if (sqh->sqb_count > 0) {
2412 			sqh->sqb_count--;
2413 			sq = sqh->sqb_free;
2414 			sqh->sqb_free = sq->sq_next;
2415 			mutex_exit(&sqh->sqb_lock);
2416 			bzero(&sq->sq_info, sizeof (k_siginfo_t));
2417 			sq->sq_backptr = sqh;
2418 			sq->sq_func = sigqrel;
2419 			sq->sq_next = NULL;
2420 			sq->sq_external = 0;
2421 		} else {
2422 			mutex_exit(&sqh->sqb_lock);
2423 		}
2424 	}
2425 	return (sq);
2426 }
2427 
2428 /*
2429  * Return a sigqueue structure back to the pre-allocated pool.
2430  */
2431 static void
2432 sigqrel(sigqueue_t *sq)
2433 {
2434 	sigqhdr_t *sqh;
2435 
2436 	/* make sure that p_lock of the affected process is held */
2437 
2438 	sqh = (sigqhdr_t *)sq->sq_backptr;
2439 	mutex_enter(&sqh->sqb_lock);
2440 	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
2441 		mutex_exit(&sqh->sqb_lock);
2442 		cv_destroy(&sqh->sqb_cv);
2443 		mutex_destroy(&sqh->sqb_lock);
2444 		kmem_free(sqh, sqh->sqb_size);
2445 	} else {
2446 		sqh->sqb_count++;
2447 		sqh->sqb_sent--;
2448 		sq->sq_next = sqh->sqb_free;
2449 		sq->sq_backptr = NULL;
2450 		sqh->sqb_free = sq;
2451 		cv_signal(&sqh->sqb_cv);
2452 		mutex_exit(&sqh->sqb_lock);
2453 	}
2454 }
2455 
2456 /*
2457  * Free up the pre-allocated sigqueue headers of sigqueue pool
2458  * and signotify pool, if possible.
2459  * Called only by the owning process during exec() and exit().
2460  */
2461 void
2462 sigqfree(proc_t *p)
2463 {
2464 	ASSERT(MUTEX_HELD(&p->p_lock));
2465 
2466 	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
2467 		sigqhdrfree(p->p_sigqhdr);
2468 		p->p_sigqhdr = NULL;
2469 	}
2470 	if (p->p_signhdr != NULL) {	/* signotify pool */
2471 		sigqhdrfree(p->p_signhdr);
2472 		p->p_signhdr = NULL;
2473 	}
2474 }
2475 
2476 /*
2477  * Free up the pre-allocated header and sigq pool if possible.
2478  */
2479 void
2480 sigqhdrfree(sigqhdr_t *sqh)
2481 {
2482 	mutex_enter(&sqh->sqb_lock);
2483 	if (sqh->sqb_sent == 0) {
2484 		mutex_exit(&sqh->sqb_lock);
2485 		cv_destroy(&sqh->sqb_cv);
2486 		mutex_destroy(&sqh->sqb_lock);
2487 		kmem_free(sqh, sqh->sqb_size);
2488 	} else {
2489 		sqh->sqb_pexited = 1;
2490 		mutex_exit(&sqh->sqb_lock);
2491 	}
2492 }
2493 
2494 /*
2495  * Free up a single sigqueue structure.
2496  * No other code should free a sigqueue directly.
2497  */
2498 void
2499 siginfofree(sigqueue_t *sqp)
2500 {
2501 	if (sqp != NULL) {
2502 		if (sqp->sq_func != NULL)
2503 			(sqp->sq_func)(sqp);
2504 		else
2505 			kmem_free(sqp, sizeof (sigqueue_t));
2506 	}
2507 }
2508 
2509 /*
2510  * Generate a synchronous signal caused by a hardware
2511  * condition encountered by an lwp.  Called from trap().
2512  */
2513 void
2514 trapsig(k_siginfo_t *ip, int restartable)
2515 {
2516 	proc_t *p = ttoproc(curthread);
2517 	int sig = ip->si_signo;
2518 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2519 
2520 	ASSERT(sig > 0 && sig < NSIG);
2521 
2522 	if (curthread->t_dtrace_on)
2523 		dtrace_safe_synchronous_signal();
2524 
2525 	mutex_enter(&p->p_lock);
2526 	schedctl_finish_sigblock(curthread);
2527 	/*
2528 	 * Avoid a possible infinite loop if the lwp is holding the
2529 	 * signal generated by a trap of a restartable instruction or
2530 	 * if the signal so generated is being ignored by the process.
2531 	 */
2532 	if (restartable &&
2533 	    (sigismember(&curthread->t_hold, sig) ||
2534 	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
2535 		sigdelset(&curthread->t_hold, sig);
2536 		p->p_user.u_signal[sig-1] = SIG_DFL;
2537 		sigdelset(&p->p_ignore, sig);
2538 	}
2539 	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
2540 	sigaddqa(p, curthread, sqp);
2541 	mutex_exit(&p->p_lock);
2542 }
2543 
2544 #ifdef _SYSCALL32_IMPL
2545 
2546 /*
2547  * It's tricky to transmit a sigval between 32-bit and 64-bit
2548  * process, since in the 64-bit world, a pointer and an integer
2549  * are different sizes.  Since we're constrained by the standards
2550  * world not to change the types, and it's unclear how useful it is
2551  * to send pointers between address spaces this way, we preserve
2552  * the 'int' interpretation for 32-bit processes interoperating
2553  * with 64-bit processes.  The full semantics (pointers or integers)
2554  * are available for N-bit processes interoperating with N-bit
2555  * processes.
2556  */
2557 void
2558 siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
2559 {
2560 	bzero(dest, sizeof (*dest));
2561 
2562 	/*
2563 	 * The absolute minimum content is si_signo and si_code.
2564 	 */
2565 	dest->si_signo = src->si_signo;
2566 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2567 		return;
2568 
2569 	/*
2570 	 * A siginfo generated by user level is structured
2571 	 * differently from one generated by the kernel.
2572 	 */
2573 	if (SI_FROMUSER(src)) {
2574 		dest->si_pid = src->si_pid;
2575 		dest->si_ctid = src->si_ctid;
2576 		dest->si_zoneid = src->si_zoneid;
2577 		dest->si_uid = src->si_uid;
2578 		if (SI_CANQUEUE(src->si_code))
2579 			dest->si_value.sival_int =
2580 			    (int32_t)src->si_value.sival_int;
2581 		return;
2582 	}
2583 
2584 	dest->si_errno = src->si_errno;
2585 
2586 	switch (src->si_signo) {
2587 	default:
2588 		dest->si_pid = src->si_pid;
2589 		dest->si_ctid = src->si_ctid;
2590 		dest->si_zoneid = src->si_zoneid;
2591 		dest->si_uid = src->si_uid;
2592 		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
2593 		break;
2594 	case SIGCLD:
2595 		dest->si_pid = src->si_pid;
2596 		dest->si_ctid = src->si_ctid;
2597 		dest->si_zoneid = src->si_zoneid;
2598 		dest->si_status = src->si_status;
2599 		dest->si_stime = src->si_stime;
2600 		dest->si_utime = src->si_utime;
2601 		break;
2602 	case SIGSEGV:
2603 	case SIGBUS:
2604 	case SIGILL:
2605 	case SIGTRAP:
2606 	case SIGFPE:
2607 	case SIGEMT:
2608 		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
2609 		dest->si_trapno = src->si_trapno;
2610 		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
2611 		break;
2612 	case SIGPOLL:
2613 	case SIGXFSZ:
2614 		dest->si_fd = src->si_fd;
2615 		dest->si_band = src->si_band;
2616 		break;
2617 	case SIGPROF:
2618 		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
2619 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2620 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2621 		dest->si_syscall = src->si_syscall;
2622 		dest->si_nsysarg = src->si_nsysarg;
2623 		dest->si_fault = src->si_fault;
2624 		break;
2625 	}
2626 }
2627 
2628 void
2629 siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
2630 {
2631 	bzero(dest, sizeof (*dest));
2632 
2633 	/*
2634 	 * The absolute minimum content is si_signo and si_code.
2635 	 */
2636 	dest->si_signo = src->si_signo;
2637 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2638 		return;
2639 
2640 	/*
2641 	 * A siginfo generated by user level is structured
2642 	 * differently from one generated by the kernel.
2643 	 */
2644 	if (SI_FROMUSER(src)) {
2645 		dest->si_pid = src->si_pid;
2646 		dest->si_ctid = src->si_ctid;
2647 		dest->si_zoneid = src->si_zoneid;
2648 		dest->si_uid = src->si_uid;
2649 		if (SI_CANQUEUE(src->si_code))
2650 			dest->si_value.sival_int =
2651 			    (int)src->si_value.sival_int;
2652 		return;
2653 	}
2654 
2655 	dest->si_errno = src->si_errno;
2656 
2657 	switch (src->si_signo) {
2658 	default:
2659 		dest->si_pid = src->si_pid;
2660 		dest->si_ctid = src->si_ctid;
2661 		dest->si_zoneid = src->si_zoneid;
2662 		dest->si_uid = src->si_uid;
2663 		dest->si_value.sival_int = (int)src->si_value.sival_int;
2664 		break;
2665 	case SIGCLD:
2666 		dest->si_pid = src->si_pid;
2667 		dest->si_ctid = src->si_ctid;
2668 		dest->si_zoneid = src->si_zoneid;
2669 		dest->si_status = src->si_status;
2670 		dest->si_stime = src->si_stime;
2671 		dest->si_utime = src->si_utime;
2672 		break;
2673 	case SIGSEGV:
2674 	case SIGBUS:
2675 	case SIGILL:
2676 	case SIGTRAP:
2677 	case SIGFPE:
2678 	case SIGEMT:
2679 		dest->si_addr = (void *)(uintptr_t)src->si_addr;
2680 		dest->si_trapno = src->si_trapno;
2681 		dest->si_pc = (void *)(uintptr_t)src->si_pc;
2682 		break;
2683 	case SIGPOLL:
2684 	case SIGXFSZ:
2685 		dest->si_fd = src->si_fd;
2686 		dest->si_band = src->si_band;
2687 		break;
2688 	case SIGPROF:
2689 		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
2690 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2691 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2692 		dest->si_syscall = src->si_syscall;
2693 		dest->si_nsysarg = src->si_nsysarg;
2694 		dest->si_fault = src->si_fault;
2695 		break;
2696 	}
2697 }
2698 
2699 #endif /* _SYSCALL32_IMPL */
2700