xref: /illumos-gate/usr/src/uts/common/os/sig.c (revision e38a713ad4e0a9c42f8cccd9350412b2c6ccccdb)
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 			if (audit_active)		/* audit core dump */
1443 				audit_core_start(sig);
1444 			if (core(sig, ext) == 0)
1445 				code = CLD_DUMPED;
1446 			if (audit_active)		/* audit core dump */
1447 				audit_core_finish(code);
1448 		}
1449 	}
1450 
1451 	/*
1452 	 * Generate a contract event once if the process is killed
1453 	 * by a signal.
1454 	 */
1455 	if (ext) {
1456 		proc_is_exiting(p);
1457 		if (exitlwps(0) != 0) {
1458 			mutex_enter(&p->p_lock);
1459 			lwp_exit();
1460 		}
1461 		contract_process_sig(p->p_ct_process, p, sig, pid, ctid,
1462 		    zoneid);
1463 	}
1464 
1465 	exit(code, sig);
1466 }
1467 
1468 /*
1469  * Find next unheld signal in ssp for thread t.
1470  */
1471 int
1472 fsig(k_sigset_t *ssp, kthread_t *t)
1473 {
1474 	proc_t *p = ttoproc(t);
1475 	user_t *up = PTOU(p);
1476 	int i;
1477 	k_sigset_t temp;
1478 
1479 	ASSERT(MUTEX_HELD(&p->p_lock));
1480 
1481 	/*
1482 	 * Don't promote any signals for the parent of a vfork()d
1483 	 * child that hasn't yet released the parent's memory.
1484 	 */
1485 	if (p->p_flag & SVFWAIT)
1486 		return (0);
1487 
1488 	temp = *ssp;
1489 	sigdiffset(&temp, &t->t_hold);
1490 
1491 	/*
1492 	 * Don't promote stopping signals (except SIGSTOP) for a child
1493 	 * of vfork() that hasn't yet released the parent's memory.
1494 	 */
1495 	if (p->p_flag & SVFORK)
1496 		sigdiffset(&temp, &holdvfork);
1497 
1498 	/*
1499 	 * Don't promote a signal that will stop
1500 	 * the process when lwp_nostop is set.
1501 	 */
1502 	if (ttolwp(t)->lwp_nostop) {
1503 		sigdelset(&temp, SIGSTOP);
1504 		if (!p->p_pgidp->pid_pgorphaned) {
1505 			if (up->u_signal[SIGTSTP-1] == SIG_DFL)
1506 				sigdelset(&temp, SIGTSTP);
1507 			if (up->u_signal[SIGTTIN-1] == SIG_DFL)
1508 				sigdelset(&temp, SIGTTIN);
1509 			if (up->u_signal[SIGTTOU-1] == SIG_DFL)
1510 				sigdelset(&temp, SIGTTOU);
1511 		}
1512 	}
1513 
1514 	/*
1515 	 * Choose SIGKILL and SIGPROF before all other pending signals.
1516 	 * The rest are promoted in signal number order.
1517 	 */
1518 	if (sigismember(&temp, SIGKILL))
1519 		return (SIGKILL);
1520 	if (sigismember(&temp, SIGPROF))
1521 		return (SIGPROF);
1522 
1523 	for (i = 0; i < sizeof (temp) / sizeof (temp.__sigbits[0]); i++) {
1524 		if (temp.__sigbits[i])
1525 			return ((i * NBBY * sizeof (temp.__sigbits[0])) +
1526 			    lowbit(temp.__sigbits[i]));
1527 	}
1528 
1529 	return (0);
1530 }
1531 
1532 void
1533 setsigact(int sig, void (*disp)(), k_sigset_t mask, int flags)
1534 {
1535 	proc_t *p = ttoproc(curthread);
1536 	kthread_t *t;
1537 
1538 	ASSERT(MUTEX_HELD(&p->p_lock));
1539 
1540 	PTOU(curproc)->u_signal[sig - 1] = disp;
1541 
1542 	/*
1543 	 * Honor the SA_SIGINFO flag if the signal is being caught.
1544 	 * Force the SA_SIGINFO flag if the signal is not being caught.
1545 	 * This is necessary to make sigqueue() and sigwaitinfo() work
1546 	 * properly together when the signal is set to default or is
1547 	 * being temporarily ignored.
1548 	 */
1549 	if ((flags & SA_SIGINFO) || disp == SIG_DFL || disp == SIG_IGN)
1550 		sigaddset(&p->p_siginfo, sig);
1551 	else
1552 		sigdelset(&p->p_siginfo, sig);
1553 
1554 	if (disp != SIG_DFL && disp != SIG_IGN) {
1555 		sigdelset(&p->p_ignore, sig);
1556 		PTOU(curproc)->u_sigmask[sig - 1] = mask;
1557 		if (!sigismember(&cantreset, sig)) {
1558 			if (flags & SA_RESETHAND)
1559 				sigaddset(&PTOU(curproc)->u_sigresethand, sig);
1560 			else
1561 				sigdelset(&PTOU(curproc)->u_sigresethand, sig);
1562 		}
1563 		if (flags & SA_NODEFER)
1564 			sigaddset(&PTOU(curproc)->u_signodefer, sig);
1565 		else
1566 			sigdelset(&PTOU(curproc)->u_signodefer, sig);
1567 		if (flags & SA_RESTART)
1568 			sigaddset(&PTOU(curproc)->u_sigrestart, sig);
1569 		else
1570 			sigdelset(&PTOU(curproc)->u_sigrestart, sig);
1571 		if (flags & SA_ONSTACK)
1572 			sigaddset(&PTOU(curproc)->u_sigonstack, sig);
1573 		else
1574 			sigdelset(&PTOU(curproc)->u_sigonstack, sig);
1575 
1576 	} else if (disp == SIG_IGN ||
1577 	    (disp == SIG_DFL && sigismember(&ignoredefault, sig))) {
1578 		/*
1579 		 * Setting the signal action to SIG_IGN results in the
1580 		 * discarding of all pending signals of that signal number.
1581 		 * Setting the signal action to SIG_DFL does the same *only*
1582 		 * if the signal's default behavior is to be ignored.
1583 		 */
1584 		sigaddset(&p->p_ignore, sig);
1585 		sigdelset(&p->p_sig, sig);
1586 		sigdelset(&p->p_extsig, sig);
1587 		sigdelq(p, NULL, sig);
1588 		t = p->p_tlist;
1589 		do {
1590 			sigdelset(&t->t_sig, sig);
1591 			sigdelset(&t->t_extsig, sig);
1592 			sigdelq(p, t, sig);
1593 		} while ((t = t->t_forw) != p->p_tlist);
1594 
1595 	} else {
1596 		/*
1597 		 * The signal action is being set to SIG_DFL and the default
1598 		 * behavior is to do something: make sure it is not ignored.
1599 		 */
1600 		sigdelset(&p->p_ignore, sig);
1601 	}
1602 
1603 	if (sig == SIGCLD) {
1604 		if (flags & SA_NOCLDWAIT)
1605 			p->p_flag |= SNOWAIT;
1606 		else
1607 			p->p_flag &= ~SNOWAIT;
1608 
1609 		if (flags & SA_NOCLDSTOP)
1610 			p->p_flag &= ~SJCTL;
1611 		else
1612 			p->p_flag |= SJCTL;
1613 
1614 		if ((p->p_flag & SNOWAIT) || disp == SIG_IGN) {
1615 			proc_t *cp, *tp;
1616 
1617 			mutex_exit(&p->p_lock);
1618 			mutex_enter(&pidlock);
1619 			for (cp = p->p_child; cp != NULL; cp = tp) {
1620 				tp = cp->p_sibling;
1621 				if (cp->p_stat == SZOMB &&
1622 				    !(cp->p_pidflag & CLDWAITPID))
1623 					freeproc(cp);
1624 			}
1625 			mutex_exit(&pidlock);
1626 			mutex_enter(&p->p_lock);
1627 		}
1628 	}
1629 }
1630 
1631 /*
1632  * Set all signal actions not already set to SIG_DFL or SIG_IGN to SIG_DFL.
1633  * Called from exec_common() for a process undergoing execve()
1634  * and from cfork() for a newly-created child of vfork().
1635  * In the vfork() case, 'p' is not the current process.
1636  * In both cases, there is only one thread in the process.
1637  */
1638 void
1639 sigdefault(proc_t *p)
1640 {
1641 	kthread_t *t = p->p_tlist;
1642 	struct user *up = PTOU(p);
1643 	int sig;
1644 
1645 	ASSERT(MUTEX_HELD(&p->p_lock));
1646 
1647 	for (sig = 1; sig < NSIG; sig++) {
1648 		if (up->u_signal[sig - 1] != SIG_DFL &&
1649 		    up->u_signal[sig - 1] != SIG_IGN) {
1650 			up->u_signal[sig - 1] = SIG_DFL;
1651 			sigemptyset(&up->u_sigmask[sig - 1]);
1652 			if (sigismember(&ignoredefault, sig)) {
1653 				sigdelq(p, NULL, sig);
1654 				sigdelq(p, t, sig);
1655 			}
1656 			if (sig == SIGCLD)
1657 				p->p_flag &= ~(SNOWAIT|SJCTL);
1658 		}
1659 	}
1660 	sigorset(&p->p_ignore, &ignoredefault);
1661 	sigfillset(&p->p_siginfo);
1662 	sigdiffset(&p->p_siginfo, &cantmask);
1663 	sigdiffset(&p->p_sig, &ignoredefault);
1664 	sigdiffset(&p->p_extsig, &ignoredefault);
1665 	sigdiffset(&t->t_sig, &ignoredefault);
1666 	sigdiffset(&t->t_extsig, &ignoredefault);
1667 }
1668 
1669 void
1670 sigcld(proc_t *cp, sigqueue_t *sqp)
1671 {
1672 	proc_t *pp = cp->p_parent;
1673 
1674 	ASSERT(MUTEX_HELD(&pidlock));
1675 
1676 	switch (cp->p_wcode) {
1677 	case CLD_EXITED:
1678 	case CLD_DUMPED:
1679 	case CLD_KILLED:
1680 		ASSERT(cp->p_stat == SZOMB);
1681 		/*
1682 		 * The broadcast on p_srwchan_cv is a kludge to
1683 		 * wakeup a possible thread in uadmin(A_SHUTDOWN).
1684 		 */
1685 		cv_broadcast(&cp->p_srwchan_cv);
1686 
1687 		/*
1688 		 * Add to newstate list of the parent
1689 		 */
1690 		add_ns(pp, cp);
1691 
1692 		cv_broadcast(&pp->p_cv);
1693 		if ((pp->p_flag & SNOWAIT) ||
1694 		    PTOU(pp)->u_signal[SIGCLD - 1] == SIG_IGN) {
1695 			if (!(cp->p_pidflag & CLDWAITPID))
1696 				freeproc(cp);
1697 		} else if (!(cp->p_pidflag & CLDNOSIGCHLD)) {
1698 			post_sigcld(cp, sqp);
1699 			sqp = NULL;
1700 		}
1701 		break;
1702 
1703 	case CLD_STOPPED:
1704 	case CLD_CONTINUED:
1705 		cv_broadcast(&pp->p_cv);
1706 		if (pp->p_flag & SJCTL) {
1707 			post_sigcld(cp, sqp);
1708 			sqp = NULL;
1709 		}
1710 		break;
1711 	}
1712 
1713 	if (sqp)
1714 		siginfofree(sqp);
1715 }
1716 
1717 /*
1718  * Common code called from sigcld() and issig_forreal()
1719  * Give the parent process a SIGCLD if it does not have one pending,
1720  * else mark the child process so a SIGCLD can be posted later.
1721  */
1722 static void
1723 post_sigcld(proc_t *cp, sigqueue_t *sqp)
1724 {
1725 	proc_t *pp = cp->p_parent;
1726 	void (*handler)() = PTOU(pp)->u_signal[SIGCLD - 1];
1727 	k_siginfo_t info;
1728 
1729 	ASSERT(MUTEX_HELD(&pidlock));
1730 	mutex_enter(&pp->p_lock);
1731 
1732 	/*
1733 	 * If a SIGCLD is pending, or if SIGCLD is not now being caught,
1734 	 * then just mark the child process so that its SIGCLD will
1735 	 * be posted later, when the first SIGCLD is taken off the
1736 	 * queue or when the parent is ready to receive it, if ever.
1737 	 */
1738 	if (handler == SIG_DFL || handler == SIG_IGN ||
1739 	    sigismember(&pp->p_sig, SIGCLD))
1740 		cp->p_pidflag |= CLDPEND;
1741 	else {
1742 		cp->p_pidflag &= ~CLDPEND;
1743 		if (sqp == NULL) {
1744 			/*
1745 			 * This can only happen when the parent is init.
1746 			 * (See call to sigcld(q, NULL) in exit().)
1747 			 * Use KM_NOSLEEP to avoid deadlock.
1748 			 */
1749 			ASSERT(pp == proc_init);
1750 			winfo(cp, &info, 0);
1751 			sigaddq(pp, NULL, &info, KM_NOSLEEP);
1752 		} else {
1753 			winfo(cp, &sqp->sq_info, 0);
1754 			sigaddqa(pp, NULL, sqp);
1755 			sqp = NULL;
1756 		}
1757 	}
1758 
1759 	mutex_exit(&pp->p_lock);
1760 
1761 	if (sqp)
1762 		siginfofree(sqp);
1763 }
1764 
1765 /*
1766  * Search for a child that has a pending SIGCLD for us, the parent.
1767  * The queue of SIGCLD signals is implied by the list of children.
1768  * We post the SIGCLD signals one at a time so they don't get lost.
1769  * When one is dequeued, another is enqueued, until there are no more.
1770  */
1771 void
1772 sigcld_repost()
1773 {
1774 	proc_t *pp = curproc;
1775 	proc_t *cp;
1776 	void (*handler)() = PTOU(pp)->u_signal[SIGCLD - 1];
1777 	sigqueue_t *sqp;
1778 
1779 	/*
1780 	 * Don't bother if SIGCLD is not now being caught.
1781 	 */
1782 	if (handler == SIG_DFL || handler == SIG_IGN)
1783 		return;
1784 
1785 	sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1786 	mutex_enter(&pidlock);
1787 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
1788 		if (cp->p_pidflag & CLDPEND) {
1789 			post_sigcld(cp, sqp);
1790 			mutex_exit(&pidlock);
1791 			return;
1792 		}
1793 	}
1794 	mutex_exit(&pidlock);
1795 	kmem_free(sqp, sizeof (sigqueue_t));
1796 }
1797 
1798 /*
1799  * count number of sigqueue send by sigaddqa()
1800  */
1801 void
1802 sigqsend(int cmd, proc_t *p, kthread_t *t, sigqueue_t *sigqp)
1803 {
1804 	sigqhdr_t *sqh;
1805 
1806 	sqh = (sigqhdr_t *)sigqp->sq_backptr;
1807 	ASSERT(sqh);
1808 
1809 	mutex_enter(&sqh->sqb_lock);
1810 	sqh->sqb_sent++;
1811 	mutex_exit(&sqh->sqb_lock);
1812 
1813 	if (cmd == SN_SEND)
1814 		sigaddqa(p, t, sigqp);
1815 	else
1816 		siginfofree(sigqp);
1817 }
1818 
1819 int
1820 sigsendproc(proc_t *p, sigsend_t *pv)
1821 {
1822 	struct cred *cr;
1823 	proc_t *myprocp = curproc;
1824 
1825 	ASSERT(MUTEX_HELD(&pidlock));
1826 
1827 	if (p->p_pid == 1 && pv->sig && sigismember(&cantmask, pv->sig))
1828 		return (EPERM);
1829 
1830 	cr = CRED();
1831 
1832 	if (pv->checkperm == 0 ||
1833 	    (pv->sig == SIGCONT && p->p_sessp == myprocp->p_sessp) ||
1834 	    prochasprocperm(p, myprocp, cr)) {
1835 		pv->perm++;
1836 		if (pv->sig) {
1837 			/* Make sure we should be setting si_pid and friends */
1838 			ASSERT(pv->sicode <= 0);
1839 			if (SI_CANQUEUE(pv->sicode)) {
1840 				sigqueue_t *sqp;
1841 
1842 				mutex_enter(&myprocp->p_lock);
1843 				sqp = sigqalloc(myprocp->p_sigqhdr);
1844 				mutex_exit(&myprocp->p_lock);
1845 				if (sqp == NULL)
1846 					return (EAGAIN);
1847 				sqp->sq_info.si_signo = pv->sig;
1848 				sqp->sq_info.si_code = pv->sicode;
1849 				sqp->sq_info.si_pid = myprocp->p_pid;
1850 				sqp->sq_info.si_ctid = PRCTID(myprocp);
1851 				sqp->sq_info.si_zoneid = getzoneid();
1852 				sqp->sq_info.si_uid = crgetruid(cr);
1853 				sqp->sq_info.si_value = pv->value;
1854 				mutex_enter(&p->p_lock);
1855 				sigqsend(SN_SEND, p, NULL, sqp);
1856 				mutex_exit(&p->p_lock);
1857 			} else {
1858 				k_siginfo_t info;
1859 				bzero(&info, sizeof (info));
1860 				info.si_signo = pv->sig;
1861 				info.si_code = pv->sicode;
1862 				info.si_pid = myprocp->p_pid;
1863 				info.si_ctid = PRCTID(myprocp);
1864 				info.si_zoneid = getzoneid();
1865 				info.si_uid = crgetruid(cr);
1866 				mutex_enter(&p->p_lock);
1867 				/*
1868 				 * XXX: Should be KM_SLEEP but
1869 				 * we have to avoid deadlock.
1870 				 */
1871 				sigaddq(p, NULL, &info, KM_NOSLEEP);
1872 				mutex_exit(&p->p_lock);
1873 			}
1874 		}
1875 	}
1876 
1877 	return (0);
1878 }
1879 
1880 int
1881 sigsendset(procset_t *psp, sigsend_t *pv)
1882 {
1883 	int error;
1884 
1885 	error = dotoprocs(psp, sigsendproc, (char *)pv);
1886 	if (error == 0 && pv->perm == 0)
1887 		return (EPERM);
1888 
1889 	return (error);
1890 }
1891 
1892 /*
1893  * Dequeue a queued siginfo structure.
1894  * If a non-null thread pointer is passed then dequeue from
1895  * the thread queue, otherwise dequeue from the process queue.
1896  */
1897 void
1898 sigdeq(proc_t *p, kthread_t *t, int sig, sigqueue_t **qpp)
1899 {
1900 	sigqueue_t **psqp, *sqp;
1901 
1902 	ASSERT(MUTEX_HELD(&p->p_lock));
1903 
1904 	*qpp = NULL;
1905 
1906 	if (t != NULL) {
1907 		sigdelset(&t->t_sig, sig);
1908 		sigdelset(&t->t_extsig, sig);
1909 		psqp = &t->t_sigqueue;
1910 	} else {
1911 		sigdelset(&p->p_sig, sig);
1912 		sigdelset(&p->p_extsig, sig);
1913 		psqp = &p->p_sigqueue;
1914 	}
1915 
1916 	for (;;) {
1917 		if ((sqp = *psqp) == NULL)
1918 			return;
1919 		if (sqp->sq_info.si_signo == sig)
1920 			break;
1921 		else
1922 			psqp = &sqp->sq_next;
1923 	}
1924 	*qpp = sqp;
1925 	*psqp = sqp->sq_next;
1926 	for (sqp = *psqp; sqp; sqp = sqp->sq_next) {
1927 		if (sqp->sq_info.si_signo == sig) {
1928 			if (t != (kthread_t *)NULL) {
1929 				sigaddset(&t->t_sig, sig);
1930 				t->t_sig_check = 1;
1931 			} else {
1932 				sigaddset(&p->p_sig, sig);
1933 				set_proc_ast(p);
1934 			}
1935 			break;
1936 		}
1937 	}
1938 }
1939 
1940 /*
1941  * Delete a queued SIGCLD siginfo structure matching the k_siginfo_t argument.
1942  */
1943 void
1944 sigcld_delete(k_siginfo_t *ip)
1945 {
1946 	proc_t *p = curproc;
1947 	int another_sigcld = 0;
1948 	sigqueue_t **psqp, *sqp;
1949 
1950 	ASSERT(ip->si_signo == SIGCLD);
1951 
1952 	mutex_enter(&p->p_lock);
1953 
1954 	if (!sigismember(&p->p_sig, SIGCLD)) {
1955 		mutex_exit(&p->p_lock);
1956 		return;
1957 	}
1958 
1959 	psqp = &p->p_sigqueue;
1960 	for (;;) {
1961 		if ((sqp = *psqp) == NULL) {
1962 			mutex_exit(&p->p_lock);
1963 			return;
1964 		}
1965 		if (sqp->sq_info.si_signo == SIGCLD) {
1966 			if (sqp->sq_info.si_pid == ip->si_pid &&
1967 			    sqp->sq_info.si_code == ip->si_code &&
1968 			    sqp->sq_info.si_status == ip->si_status)
1969 				break;
1970 			another_sigcld = 1;
1971 		}
1972 		psqp = &sqp->sq_next;
1973 	}
1974 	*psqp = sqp->sq_next;
1975 
1976 	siginfofree(sqp);
1977 
1978 	for (sqp = *psqp; !another_sigcld && sqp; sqp = sqp->sq_next) {
1979 		if (sqp->sq_info.si_signo == SIGCLD)
1980 			another_sigcld = 1;
1981 	}
1982 
1983 	if (!another_sigcld) {
1984 		sigdelset(&p->p_sig, SIGCLD);
1985 		sigdelset(&p->p_extsig, SIGCLD);
1986 	}
1987 
1988 	mutex_exit(&p->p_lock);
1989 }
1990 
1991 /*
1992  * Delete queued siginfo structures.
1993  * If a non-null thread pointer is passed then delete from
1994  * the thread queue, otherwise delete from the process queue.
1995  */
1996 void
1997 sigdelq(proc_t *p, kthread_t *t, int sig)
1998 {
1999 	sigqueue_t **psqp, *sqp;
2000 
2001 	/*
2002 	 * We must be holding p->p_lock unless the process is
2003 	 * being reaped or has failed to get started on fork.
2004 	 */
2005 	ASSERT(MUTEX_HELD(&p->p_lock) ||
2006 	    p->p_stat == SIDL || p->p_stat == SZOMB);
2007 
2008 	if (t != (kthread_t *)NULL)
2009 		psqp = &t->t_sigqueue;
2010 	else
2011 		psqp = &p->p_sigqueue;
2012 
2013 	while (*psqp) {
2014 		sqp = *psqp;
2015 		if (sig == 0 || sqp->sq_info.si_signo == sig) {
2016 			*psqp = sqp->sq_next;
2017 			siginfofree(sqp);
2018 		} else
2019 			psqp = &sqp->sq_next;
2020 	}
2021 }
2022 
2023 /*
2024  * Insert a siginfo structure into a queue.
2025  * If a non-null thread pointer is passed then add to the thread queue,
2026  * otherwise add to the process queue.
2027  *
2028  * The function sigaddqins() is called with sigqueue already allocated.
2029  * It is called from sigaddqa() and sigaddq() below.
2030  *
2031  * The value of si_code implicitly indicates whether sigp is to be
2032  * explicitly queued, or to be queued to depth one.
2033  */
2034 static void
2035 sigaddqins(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
2036 {
2037 	sigqueue_t **psqp;
2038 	int sig = sigqp->sq_info.si_signo;
2039 
2040 	sigqp->sq_external = (curproc != &p0) &&
2041 	    (curproc->p_ct_process != p->p_ct_process);
2042 
2043 	/*
2044 	 * issig_forreal() doesn't bother dequeueing signals if SKILLED
2045 	 * is set, and even if it did, we would want to avoid situation
2046 	 * (which would be unique to SIGKILL) where one thread dequeued
2047 	 * the sigqueue_t and another executed psig().  So we create a
2048 	 * separate stash for SIGKILL's sigqueue_t.  Because a second
2049 	 * SIGKILL can set SEXTKILLED, we overwrite the existing entry
2050 	 * if (and only if) it was non-extracontractual.
2051 	 */
2052 	if (sig == SIGKILL) {
2053 		if (p->p_killsqp == NULL || !p->p_killsqp->sq_external) {
2054 			if (p->p_killsqp != NULL)
2055 				siginfofree(p->p_killsqp);
2056 			p->p_killsqp = sigqp;
2057 			sigqp->sq_next = NULL;
2058 		} else {
2059 			siginfofree(sigqp);
2060 		}
2061 		return;
2062 	}
2063 
2064 	ASSERT(sig >= 1 && sig < NSIG);
2065 	if (t != NULL)	/* directed to a thread */
2066 		psqp = &t->t_sigqueue;
2067 	else 		/* directed to a process */
2068 		psqp = &p->p_sigqueue;
2069 	if (SI_CANQUEUE(sigqp->sq_info.si_code) &&
2070 	    sigismember(&p->p_siginfo, sig)) {
2071 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next)
2072 				;
2073 	} else {
2074 		for (; *psqp != NULL; psqp = &(*psqp)->sq_next) {
2075 			if ((*psqp)->sq_info.si_signo == sig) {
2076 				siginfofree(sigqp);
2077 				return;
2078 			}
2079 		}
2080 	}
2081 	*psqp = sigqp;
2082 	sigqp->sq_next = NULL;
2083 }
2084 
2085 /*
2086  * The function sigaddqa() is called with sigqueue already allocated.
2087  * If signal is ignored, discard but guarantee KILL and generation semantics.
2088  * It is called from sigqueue() and other places.
2089  */
2090 void
2091 sigaddqa(proc_t *p, kthread_t *t, sigqueue_t *sigqp)
2092 {
2093 	int sig = sigqp->sq_info.si_signo;
2094 
2095 	ASSERT(MUTEX_HELD(&p->p_lock));
2096 	ASSERT(sig >= 1 && sig < NSIG);
2097 
2098 	if (sig_discardable(p, sig))
2099 		siginfofree(sigqp);
2100 	else
2101 		sigaddqins(p, t, sigqp);
2102 
2103 	sigtoproc(p, t, sig);
2104 }
2105 
2106 /*
2107  * Allocate the sigqueue_t structure and call sigaddqins().
2108  */
2109 void
2110 sigaddq(proc_t *p, kthread_t *t, k_siginfo_t *infop, int km_flags)
2111 {
2112 	sigqueue_t *sqp;
2113 	int sig = infop->si_signo;
2114 
2115 	ASSERT(MUTEX_HELD(&p->p_lock));
2116 	ASSERT(sig >= 1 && sig < NSIG);
2117 
2118 	/*
2119 	 * If the signal will be discarded by sigtoproc() or
2120 	 * if the process isn't requesting siginfo and it isn't
2121 	 * blocking the signal (it *could* change it's mind while
2122 	 * the signal is pending) then don't bother creating one.
2123 	 */
2124 	if (!sig_discardable(p, sig) &&
2125 	    (sigismember(&p->p_siginfo, sig) ||
2126 	    (curproc->p_ct_process != p->p_ct_process) ||
2127 	    (sig == SIGCLD && SI_FROMKERNEL(infop))) &&
2128 	    ((sqp = kmem_alloc(sizeof (sigqueue_t), km_flags)) != NULL)) {
2129 		bcopy(infop, &sqp->sq_info, sizeof (k_siginfo_t));
2130 		sqp->sq_func = NULL;
2131 		sqp->sq_next = NULL;
2132 		sigaddqins(p, t, sqp);
2133 	}
2134 	sigtoproc(p, t, sig);
2135 }
2136 
2137 /*
2138  * Handle stop-on-fault processing for the debugger.  Returns 0
2139  * if the fault is cleared during the stop, nonzero if it isn't.
2140  */
2141 int
2142 stop_on_fault(uint_t fault, k_siginfo_t *sip)
2143 {
2144 	proc_t *p = ttoproc(curthread);
2145 	klwp_t *lwp = ttolwp(curthread);
2146 
2147 	ASSERT(prismember(&p->p_fltmask, fault));
2148 
2149 	/*
2150 	 * Record current fault and siginfo structure so debugger can
2151 	 * find it.
2152 	 */
2153 	mutex_enter(&p->p_lock);
2154 	lwp->lwp_curflt = (uchar_t)fault;
2155 	lwp->lwp_siginfo = *sip;
2156 
2157 	stop(PR_FAULTED, fault);
2158 
2159 	fault = lwp->lwp_curflt;
2160 	lwp->lwp_curflt = 0;
2161 	mutex_exit(&p->p_lock);
2162 	return (fault);
2163 }
2164 
2165 void
2166 sigorset(k_sigset_t *s1, k_sigset_t *s2)
2167 {
2168 	s1->__sigbits[0] |= s2->__sigbits[0];
2169 	s1->__sigbits[1] |= s2->__sigbits[1];
2170 }
2171 
2172 void
2173 sigandset(k_sigset_t *s1, k_sigset_t *s2)
2174 {
2175 	s1->__sigbits[0] &= s2->__sigbits[0];
2176 	s1->__sigbits[1] &= s2->__sigbits[1];
2177 }
2178 
2179 void
2180 sigdiffset(k_sigset_t *s1, k_sigset_t *s2)
2181 {
2182 	s1->__sigbits[0] &= ~(s2->__sigbits[0]);
2183 	s1->__sigbits[1] &= ~(s2->__sigbits[1]);
2184 }
2185 
2186 /*
2187  * Return non-zero if curthread->t_sig_check should be set to 1, that is,
2188  * if there are any signals the thread might take on return from the kernel.
2189  * If ksigset_t's were a single word, we would do:
2190  *	return (((p->p_sig | t->t_sig) & ~t->t_hold) & fillset);
2191  */
2192 int
2193 sigcheck(proc_t *p, kthread_t *t)
2194 {
2195 	sc_shared_t *tdp = t->t_schedctl;
2196 
2197 	/*
2198 	 * If signals are blocked via the schedctl interface
2199 	 * then we only check for the unmaskable signals.
2200 	 */
2201 	if (tdp != NULL && tdp->sc_sigblock)
2202 		return ((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2203 		    CANTMASK0);
2204 
2205 	return (((p->p_sig.__sigbits[0] | t->t_sig.__sigbits[0]) &
2206 	    ~t->t_hold.__sigbits[0]) |
2207 	    (((p->p_sig.__sigbits[1] | t->t_sig.__sigbits[1]) &
2208 	    ~t->t_hold.__sigbits[1]) & FILLSET1));
2209 }
2210 
2211 /* ONC_PLUS EXTRACT START */
2212 void
2213 sigintr(k_sigset_t *smask, int intable)
2214 {
2215 	proc_t *p;
2216 	int owned;
2217 	k_sigset_t lmask;		/* local copy of cantmask */
2218 	klwp_t *lwp = ttolwp(curthread);
2219 
2220 	/*
2221 	 * Mask out all signals except SIGHUP, SIGINT, SIGQUIT
2222 	 *    and SIGTERM. (Preserving the existing masks).
2223 	 *    This function supports the -intr nfs and ufs mount option.
2224 	 */
2225 
2226 	/*
2227 	 * don't do kernel threads
2228 	 */
2229 	if (lwp == NULL)
2230 		return;
2231 
2232 	/*
2233 	 * get access to signal mask
2234 	 */
2235 	p = ttoproc(curthread);
2236 	owned = mutex_owned(&p->p_lock);	/* this is filthy */
2237 	if (!owned)
2238 		mutex_enter(&p->p_lock);
2239 
2240 	/*
2241 	 * remember the current mask
2242 	 */
2243 	schedctl_finish_sigblock(curthread);
2244 	*smask = curthread->t_hold;
2245 
2246 	/*
2247 	 * mask out all signals
2248 	 */
2249 	sigfillset(&curthread->t_hold);
2250 
2251 	/*
2252 	 * Unmask the non-maskable signals (e.g., KILL), as long as
2253 	 * they aren't already masked (which could happen at exit).
2254 	 * The first sigdiffset sets lmask to (cantmask & ~curhold).  The
2255 	 * second sets the current hold mask to (~0 & ~lmask), which reduces
2256 	 * to (~cantmask | curhold).
2257 	 */
2258 	lmask = cantmask;
2259 	sigdiffset(&lmask, smask);
2260 	sigdiffset(&curthread->t_hold, &lmask);
2261 
2262 	/*
2263 	 * Re-enable HUP, QUIT, and TERM iff they were originally enabled
2264 	 * Re-enable INT if it's originally enabled and the NFS mount option
2265 	 * nointr is not set.
2266 	 */
2267 	if (!sigismember(smask, SIGHUP))
2268 		sigdelset(&curthread->t_hold, SIGHUP);
2269 	if (!sigismember(smask, SIGINT) && intable)
2270 		sigdelset(&curthread->t_hold, SIGINT);
2271 	if (!sigismember(smask, SIGQUIT))
2272 		sigdelset(&curthread->t_hold, SIGQUIT);
2273 	if (!sigismember(smask, SIGTERM))
2274 		sigdelset(&curthread->t_hold, SIGTERM);
2275 
2276 	/*
2277 	 * release access to signal mask
2278 	 */
2279 	if (!owned)
2280 		mutex_exit(&p->p_lock);
2281 
2282 	/*
2283 	 * Indicate that this lwp is not to be stopped.
2284 	 */
2285 	lwp->lwp_nostop++;
2286 
2287 }
2288 /* ONC_PLUS EXTRACT END */
2289 
2290 void
2291 sigunintr(k_sigset_t *smask)
2292 {
2293 	proc_t *p;
2294 	int owned;
2295 	klwp_t *lwp = ttolwp(curthread);
2296 
2297 	/*
2298 	 * Reset previous mask (See sigintr() above)
2299 	 */
2300 	if (lwp != NULL) {
2301 		lwp->lwp_nostop--;	/* restore lwp stoppability */
2302 		p = ttoproc(curthread);
2303 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2304 		if (!owned)
2305 			mutex_enter(&p->p_lock);
2306 		curthread->t_hold = *smask;
2307 		/* so unmasked signals will be seen */
2308 		curthread->t_sig_check = 1;
2309 		if (!owned)
2310 			mutex_exit(&p->p_lock);
2311 	}
2312 }
2313 
2314 void
2315 sigreplace(k_sigset_t *newmask, k_sigset_t *oldmask)
2316 {
2317 	proc_t	*p;
2318 	int owned;
2319 	/*
2320 	 * Save current signal mask in oldmask, then
2321 	 * set it to newmask.
2322 	 */
2323 	if (ttolwp(curthread) != NULL) {
2324 		p = ttoproc(curthread);
2325 		owned = mutex_owned(&p->p_lock);	/* this is filthy */
2326 		if (!owned)
2327 			mutex_enter(&p->p_lock);
2328 		schedctl_finish_sigblock(curthread);
2329 		if (oldmask != NULL)
2330 			*oldmask = curthread->t_hold;
2331 		curthread->t_hold = *newmask;
2332 		curthread->t_sig_check = 1;
2333 		if (!owned)
2334 			mutex_exit(&p->p_lock);
2335 	}
2336 }
2337 
2338 /*
2339  * Return true if the signal number is in range
2340  * and the signal code specifies signal queueing.
2341  */
2342 int
2343 sigwillqueue(int sig, int code)
2344 {
2345 	if (sig >= 0 && sig < NSIG) {
2346 		switch (code) {
2347 		case SI_QUEUE:
2348 		case SI_TIMER:
2349 		case SI_ASYNCIO:
2350 		case SI_MESGQ:
2351 			return (1);
2352 		}
2353 	}
2354 	return (0);
2355 }
2356 
2357 #ifndef	UCHAR_MAX
2358 #define	UCHAR_MAX	255
2359 #endif
2360 
2361 /*
2362  * The entire pool (with maxcount entries) is pre-allocated at
2363  * the first sigqueue/signotify call.
2364  */
2365 sigqhdr_t *
2366 sigqhdralloc(size_t size, uint_t maxcount)
2367 {
2368 	size_t i;
2369 	sigqueue_t *sq, *next;
2370 	sigqhdr_t *sqh;
2371 
2372 	i = (maxcount * size) + sizeof (sigqhdr_t);
2373 	ASSERT(maxcount <= UCHAR_MAX && i <= USHRT_MAX);
2374 	sqh = kmem_alloc(i, KM_SLEEP);
2375 	sqh->sqb_count = (uchar_t)maxcount;
2376 	sqh->sqb_maxcount = (uchar_t)maxcount;
2377 	sqh->sqb_size = (ushort_t)i;
2378 	sqh->sqb_pexited = 0;
2379 	sqh->sqb_sent = 0;
2380 	sqh->sqb_free = sq = (sigqueue_t *)(sqh + 1);
2381 	for (i = maxcount - 1; i != 0; i--) {
2382 		next = (sigqueue_t *)((uintptr_t)sq + size);
2383 		sq->sq_next = next;
2384 		sq = next;
2385 	}
2386 	sq->sq_next = NULL;
2387 	cv_init(&sqh->sqb_cv, NULL, CV_DEFAULT, NULL);
2388 	mutex_init(&sqh->sqb_lock, NULL, MUTEX_DEFAULT, NULL);
2389 	return (sqh);
2390 }
2391 
2392 static void sigqrel(sigqueue_t *);
2393 
2394 /*
2395  * allocate a sigqueue/signotify structure from the per process
2396  * pre-allocated pool.
2397  */
2398 sigqueue_t *
2399 sigqalloc(sigqhdr_t *sqh)
2400 {
2401 	sigqueue_t *sq = NULL;
2402 
2403 	ASSERT(MUTEX_HELD(&curproc->p_lock));
2404 
2405 	if (sqh != NULL) {
2406 		mutex_enter(&sqh->sqb_lock);
2407 		if (sqh->sqb_count > 0) {
2408 			sqh->sqb_count--;
2409 			sq = sqh->sqb_free;
2410 			sqh->sqb_free = sq->sq_next;
2411 			mutex_exit(&sqh->sqb_lock);
2412 			bzero(&sq->sq_info, sizeof (k_siginfo_t));
2413 			sq->sq_backptr = sqh;
2414 			sq->sq_func = sigqrel;
2415 			sq->sq_next = NULL;
2416 			sq->sq_external = 0;
2417 		} else {
2418 			mutex_exit(&sqh->sqb_lock);
2419 		}
2420 	}
2421 	return (sq);
2422 }
2423 
2424 /*
2425  * Return a sigqueue structure back to the pre-allocated pool.
2426  */
2427 static void
2428 sigqrel(sigqueue_t *sq)
2429 {
2430 	sigqhdr_t *sqh;
2431 
2432 	/* make sure that p_lock of the affected process is held */
2433 
2434 	sqh = (sigqhdr_t *)sq->sq_backptr;
2435 	mutex_enter(&sqh->sqb_lock);
2436 	if (sqh->sqb_pexited && sqh->sqb_sent == 1) {
2437 		mutex_exit(&sqh->sqb_lock);
2438 		cv_destroy(&sqh->sqb_cv);
2439 		mutex_destroy(&sqh->sqb_lock);
2440 		kmem_free(sqh, sqh->sqb_size);
2441 	} else {
2442 		sqh->sqb_count++;
2443 		sqh->sqb_sent--;
2444 		sq->sq_next = sqh->sqb_free;
2445 		sq->sq_backptr = NULL;
2446 		sqh->sqb_free = sq;
2447 		cv_signal(&sqh->sqb_cv);
2448 		mutex_exit(&sqh->sqb_lock);
2449 	}
2450 }
2451 
2452 /*
2453  * Free up the pre-allocated sigqueue headers of sigqueue pool
2454  * and signotify pool, if possible.
2455  * Called only by the owning process during exec() and exit().
2456  */
2457 void
2458 sigqfree(proc_t *p)
2459 {
2460 	ASSERT(MUTEX_HELD(&p->p_lock));
2461 
2462 	if (p->p_sigqhdr != NULL) {	/* sigqueue pool */
2463 		sigqhdrfree(p->p_sigqhdr);
2464 		p->p_sigqhdr = NULL;
2465 	}
2466 	if (p->p_signhdr != NULL) {	/* signotify pool */
2467 		sigqhdrfree(p->p_signhdr);
2468 		p->p_signhdr = NULL;
2469 	}
2470 }
2471 
2472 /*
2473  * Free up the pre-allocated header and sigq pool if possible.
2474  */
2475 void
2476 sigqhdrfree(sigqhdr_t *sqh)
2477 {
2478 	mutex_enter(&sqh->sqb_lock);
2479 	if (sqh->sqb_sent == 0) {
2480 		mutex_exit(&sqh->sqb_lock);
2481 		cv_destroy(&sqh->sqb_cv);
2482 		mutex_destroy(&sqh->sqb_lock);
2483 		kmem_free(sqh, sqh->sqb_size);
2484 	} else {
2485 		sqh->sqb_pexited = 1;
2486 		mutex_exit(&sqh->sqb_lock);
2487 	}
2488 }
2489 
2490 /*
2491  * Free up a single sigqueue structure.
2492  * No other code should free a sigqueue directly.
2493  */
2494 void
2495 siginfofree(sigqueue_t *sqp)
2496 {
2497 	if (sqp != NULL) {
2498 		if (sqp->sq_func != NULL)
2499 			(sqp->sq_func)(sqp);
2500 		else
2501 			kmem_free(sqp, sizeof (sigqueue_t));
2502 	}
2503 }
2504 
2505 /*
2506  * Generate a synchronous signal caused by a hardware
2507  * condition encountered by an lwp.  Called from trap().
2508  */
2509 void
2510 trapsig(k_siginfo_t *ip, int restartable)
2511 {
2512 	proc_t *p = ttoproc(curthread);
2513 	int sig = ip->si_signo;
2514 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2515 
2516 	ASSERT(sig > 0 && sig < NSIG);
2517 
2518 	if (curthread->t_dtrace_on)
2519 		dtrace_safe_synchronous_signal();
2520 
2521 	mutex_enter(&p->p_lock);
2522 	schedctl_finish_sigblock(curthread);
2523 	/*
2524 	 * Avoid a possible infinite loop if the lwp is holding the
2525 	 * signal generated by a trap of a restartable instruction or
2526 	 * if the signal so generated is being ignored by the process.
2527 	 */
2528 	if (restartable &&
2529 	    (sigismember(&curthread->t_hold, sig) ||
2530 	    p->p_user.u_signal[sig-1] == SIG_IGN)) {
2531 		sigdelset(&curthread->t_hold, sig);
2532 		p->p_user.u_signal[sig-1] = SIG_DFL;
2533 		sigdelset(&p->p_ignore, sig);
2534 	}
2535 	bcopy(ip, &sqp->sq_info, sizeof (k_siginfo_t));
2536 	sigaddqa(p, curthread, sqp);
2537 	mutex_exit(&p->p_lock);
2538 }
2539 
2540 #ifdef _SYSCALL32_IMPL
2541 
2542 /*
2543  * It's tricky to transmit a sigval between 32-bit and 64-bit
2544  * process, since in the 64-bit world, a pointer and an integer
2545  * are different sizes.  Since we're constrained by the standards
2546  * world not to change the types, and it's unclear how useful it is
2547  * to send pointers between address spaces this way, we preserve
2548  * the 'int' interpretation for 32-bit processes interoperating
2549  * with 64-bit processes.  The full semantics (pointers or integers)
2550  * are available for N-bit processes interoperating with N-bit
2551  * processes.
2552  */
2553 void
2554 siginfo_kto32(const k_siginfo_t *src, siginfo32_t *dest)
2555 {
2556 	bzero(dest, sizeof (*dest));
2557 
2558 	/*
2559 	 * The absolute minimum content is si_signo and si_code.
2560 	 */
2561 	dest->si_signo = src->si_signo;
2562 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2563 		return;
2564 
2565 	/*
2566 	 * A siginfo generated by user level is structured
2567 	 * differently from one generated by the kernel.
2568 	 */
2569 	if (SI_FROMUSER(src)) {
2570 		dest->si_pid = src->si_pid;
2571 		dest->si_ctid = src->si_ctid;
2572 		dest->si_zoneid = src->si_zoneid;
2573 		dest->si_uid = src->si_uid;
2574 		if (SI_CANQUEUE(src->si_code))
2575 			dest->si_value.sival_int =
2576 			    (int32_t)src->si_value.sival_int;
2577 		return;
2578 	}
2579 
2580 	dest->si_errno = src->si_errno;
2581 
2582 	switch (src->si_signo) {
2583 	default:
2584 		dest->si_pid = src->si_pid;
2585 		dest->si_ctid = src->si_ctid;
2586 		dest->si_zoneid = src->si_zoneid;
2587 		dest->si_uid = src->si_uid;
2588 		dest->si_value.sival_int = (int32_t)src->si_value.sival_int;
2589 		break;
2590 	case SIGCLD:
2591 		dest->si_pid = src->si_pid;
2592 		dest->si_ctid = src->si_ctid;
2593 		dest->si_zoneid = src->si_zoneid;
2594 		dest->si_status = src->si_status;
2595 		dest->si_stime = src->si_stime;
2596 		dest->si_utime = src->si_utime;
2597 		break;
2598 	case SIGSEGV:
2599 	case SIGBUS:
2600 	case SIGILL:
2601 	case SIGTRAP:
2602 	case SIGFPE:
2603 	case SIGEMT:
2604 		dest->si_addr = (caddr32_t)(uintptr_t)src->si_addr;
2605 		dest->si_trapno = src->si_trapno;
2606 		dest->si_pc = (caddr32_t)(uintptr_t)src->si_pc;
2607 		break;
2608 	case SIGPOLL:
2609 	case SIGXFSZ:
2610 		dest->si_fd = src->si_fd;
2611 		dest->si_band = src->si_band;
2612 		break;
2613 	case SIGPROF:
2614 		dest->si_faddr = (caddr32_t)(uintptr_t)src->si_faddr;
2615 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2616 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2617 		dest->si_syscall = src->si_syscall;
2618 		dest->si_nsysarg = src->si_nsysarg;
2619 		dest->si_fault = src->si_fault;
2620 		break;
2621 	}
2622 }
2623 
2624 void
2625 siginfo_32tok(const siginfo32_t *src, k_siginfo_t *dest)
2626 {
2627 	bzero(dest, sizeof (*dest));
2628 
2629 	/*
2630 	 * The absolute minimum content is si_signo and si_code.
2631 	 */
2632 	dest->si_signo = src->si_signo;
2633 	if ((dest->si_code = src->si_code) == SI_NOINFO)
2634 		return;
2635 
2636 	/*
2637 	 * A siginfo generated by user level is structured
2638 	 * differently from one generated by the kernel.
2639 	 */
2640 	if (SI_FROMUSER(src)) {
2641 		dest->si_pid = src->si_pid;
2642 		dest->si_ctid = src->si_ctid;
2643 		dest->si_zoneid = src->si_zoneid;
2644 		dest->si_uid = src->si_uid;
2645 		if (SI_CANQUEUE(src->si_code))
2646 			dest->si_value.sival_int =
2647 			    (int)src->si_value.sival_int;
2648 		return;
2649 	}
2650 
2651 	dest->si_errno = src->si_errno;
2652 
2653 	switch (src->si_signo) {
2654 	default:
2655 		dest->si_pid = src->si_pid;
2656 		dest->si_ctid = src->si_ctid;
2657 		dest->si_zoneid = src->si_zoneid;
2658 		dest->si_uid = src->si_uid;
2659 		dest->si_value.sival_int = (int)src->si_value.sival_int;
2660 		break;
2661 	case SIGCLD:
2662 		dest->si_pid = src->si_pid;
2663 		dest->si_ctid = src->si_ctid;
2664 		dest->si_zoneid = src->si_zoneid;
2665 		dest->si_status = src->si_status;
2666 		dest->si_stime = src->si_stime;
2667 		dest->si_utime = src->si_utime;
2668 		break;
2669 	case SIGSEGV:
2670 	case SIGBUS:
2671 	case SIGILL:
2672 	case SIGTRAP:
2673 	case SIGFPE:
2674 	case SIGEMT:
2675 		dest->si_addr = (void *)(uintptr_t)src->si_addr;
2676 		dest->si_trapno = src->si_trapno;
2677 		dest->si_pc = (void *)(uintptr_t)src->si_pc;
2678 		break;
2679 	case SIGPOLL:
2680 	case SIGXFSZ:
2681 		dest->si_fd = src->si_fd;
2682 		dest->si_band = src->si_band;
2683 		break;
2684 	case SIGPROF:
2685 		dest->si_faddr = (void *)(uintptr_t)src->si_faddr;
2686 		dest->si_tstamp.tv_sec = src->si_tstamp.tv_sec;
2687 		dest->si_tstamp.tv_nsec = src->si_tstamp.tv_nsec;
2688 		dest->si_syscall = src->si_syscall;
2689 		dest->si_nsysarg = src->si_nsysarg;
2690 		dest->si_fault = src->si_fault;
2691 		break;
2692 	}
2693 }
2694 
2695 #endif /* _SYSCALL32_IMPL */
2696