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