xref: /openbsd-src/sys/kern/kern_sig.c (revision d4c5fc9dc00f5a9cadd8c2de4e52d85d3c1c6003)
1 /*	$OpenBSD: kern_sig.c,v 1.220 2018/04/28 03:13:04 visa Exp $	*/
2 /*	$NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Theo de Raadt. All rights reserved.
6  * Copyright (c) 1982, 1986, 1989, 1991, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
39  */
40 
41 #define	SIGPROP		/* include signal properties table */
42 #include <sys/param.h>
43 #include <sys/signalvar.h>
44 #include <sys/resourcevar.h>
45 #include <sys/queue.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/event.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/acct.h>
52 #include <sys/fcntl.h>
53 #include <sys/filedesc.h>
54 #include <sys/kernel.h>
55 #include <sys/wait.h>
56 #include <sys/ktrace.h>
57 #include <sys/stat.h>
58 #include <sys/core.h>
59 #include <sys/malloc.h>
60 #include <sys/pool.h>
61 #include <sys/ptrace.h>
62 #include <sys/sched.h>
63 #include <sys/user.h>
64 #include <sys/syslog.h>
65 #include <sys/pledge.h>
66 #include <sys/witness.h>
67 
68 #include <sys/mount.h>
69 #include <sys/syscallargs.h>
70 
71 #include <uvm/uvm_extern.h>
72 #include <machine/tcb.h>
73 
74 int	filt_sigattach(struct knote *kn);
75 void	filt_sigdetach(struct knote *kn);
76 int	filt_signal(struct knote *kn, long hint);
77 
78 struct filterops sig_filtops =
79 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
80 
81 void proc_stop(struct proc *p, int);
82 void proc_stop_sweep(void *);
83 struct timeout proc_stop_to;
84 
85 void postsig(struct proc *, int);
86 int cansignal(struct proc *, struct process *, int);
87 
88 struct pool sigacts_pool;	/* memory pool for sigacts structures */
89 
90 /*
91  * Can thread p, send the signal signum to process qr?
92  */
93 int
94 cansignal(struct proc *p, struct process *qr, int signum)
95 {
96 	struct process *pr = p->p_p;
97 	struct ucred *uc = p->p_ucred;
98 	struct ucred *quc = qr->ps_ucred;
99 
100 	if (uc->cr_uid == 0)
101 		return (1);		/* root can always signal */
102 
103 	if (pr == qr)
104 		return (1);		/* process can always signal itself */
105 
106 	/* optimization: if the same creds then the tests below will pass */
107 	if (uc == quc)
108 		return (1);
109 
110 	if (signum == SIGCONT && qr->ps_session == pr->ps_session)
111 		return (1);		/* SIGCONT in session */
112 
113 	/*
114 	 * Using kill(), only certain signals can be sent to setugid
115 	 * child processes
116 	 */
117 	if (qr->ps_flags & PS_SUGID) {
118 		switch (signum) {
119 		case 0:
120 		case SIGKILL:
121 		case SIGINT:
122 		case SIGTERM:
123 		case SIGALRM:
124 		case SIGSTOP:
125 		case SIGTTIN:
126 		case SIGTTOU:
127 		case SIGTSTP:
128 		case SIGHUP:
129 		case SIGUSR1:
130 		case SIGUSR2:
131 			if (uc->cr_ruid == quc->cr_ruid ||
132 			    uc->cr_uid == quc->cr_ruid)
133 				return (1);
134 		}
135 		return (0);
136 	}
137 
138 	if (uc->cr_ruid == quc->cr_ruid ||
139 	    uc->cr_ruid == quc->cr_svuid ||
140 	    uc->cr_uid == quc->cr_ruid ||
141 	    uc->cr_uid == quc->cr_svuid)
142 		return (1);
143 	return (0);
144 }
145 
146 /*
147  * Initialize signal-related data structures.
148  */
149 void
150 signal_init(void)
151 {
152 	timeout_set(&proc_stop_to, proc_stop_sweep, NULL);
153 
154 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE,
155 	    PR_WAITOK, "sigapl", NULL);
156 }
157 
158 /*
159  * Create an initial sigacts structure, using the same signal state
160  * as p.
161  */
162 struct sigacts *
163 sigactsinit(struct process *pr)
164 {
165 	struct sigacts *ps;
166 
167 	ps = pool_get(&sigacts_pool, PR_WAITOK);
168 	memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts));
169 	ps->ps_refcnt = 1;
170 	return (ps);
171 }
172 
173 /*
174  * Share a sigacts structure.
175  */
176 struct sigacts *
177 sigactsshare(struct process *pr)
178 {
179 	struct sigacts *ps = pr->ps_sigacts;
180 
181 	ps->ps_refcnt++;
182 	return ps;
183 }
184 
185 /*
186  * Initialize a new sigaltstack structure.
187  */
188 void
189 sigstkinit(struct sigaltstack *ss)
190 {
191 	ss->ss_flags = SS_DISABLE;
192 	ss->ss_size = 0;
193 	ss->ss_sp = 0;
194 }
195 
196 /*
197  * Make this process not share its sigacts, maintaining all
198  * signal state.
199  */
200 void
201 sigactsunshare(struct process *pr)
202 {
203 	struct sigacts *newps;
204 
205 	if (pr->ps_sigacts->ps_refcnt == 1)
206 		return;
207 
208 	newps = sigactsinit(pr);
209 	sigactsfree(pr);
210 	pr->ps_sigacts = newps;
211 }
212 
213 /*
214  * Release a sigacts structure.
215  */
216 void
217 sigactsfree(struct process *pr)
218 {
219 	struct sigacts *ps = pr->ps_sigacts;
220 
221 	if (--ps->ps_refcnt > 0)
222 		return;
223 
224 	pr->ps_sigacts = NULL;
225 
226 	pool_put(&sigacts_pool, ps);
227 }
228 
229 int
230 sys_sigaction(struct proc *p, void *v, register_t *retval)
231 {
232 	struct sys_sigaction_args /* {
233 		syscallarg(int) signum;
234 		syscallarg(const struct sigaction *) nsa;
235 		syscallarg(struct sigaction *) osa;
236 	} */ *uap = v;
237 	struct sigaction vec;
238 #ifdef KTRACE
239 	struct sigaction ovec;
240 #endif
241 	struct sigaction *sa;
242 	const struct sigaction *nsa;
243 	struct sigaction *osa;
244 	struct sigacts *ps = p->p_p->ps_sigacts;
245 	int signum;
246 	int bit, error;
247 
248 	signum = SCARG(uap, signum);
249 	nsa = SCARG(uap, nsa);
250 	osa = SCARG(uap, osa);
251 
252 	if (signum <= 0 || signum >= NSIG ||
253 	    (nsa && (signum == SIGKILL || signum == SIGSTOP)))
254 		return (EINVAL);
255 	sa = &vec;
256 	if (osa) {
257 		sa->sa_handler = ps->ps_sigact[signum];
258 		sa->sa_mask = ps->ps_catchmask[signum];
259 		bit = sigmask(signum);
260 		sa->sa_flags = 0;
261 		if ((ps->ps_sigonstack & bit) != 0)
262 			sa->sa_flags |= SA_ONSTACK;
263 		if ((ps->ps_sigintr & bit) == 0)
264 			sa->sa_flags |= SA_RESTART;
265 		if ((ps->ps_sigreset & bit) != 0)
266 			sa->sa_flags |= SA_RESETHAND;
267 		if ((ps->ps_siginfo & bit) != 0)
268 			sa->sa_flags |= SA_SIGINFO;
269 		if (signum == SIGCHLD) {
270 			if ((ps->ps_flags & SAS_NOCLDSTOP) != 0)
271 				sa->sa_flags |= SA_NOCLDSTOP;
272 			if ((ps->ps_flags & SAS_NOCLDWAIT) != 0)
273 				sa->sa_flags |= SA_NOCLDWAIT;
274 		}
275 		if ((sa->sa_mask & bit) == 0)
276 			sa->sa_flags |= SA_NODEFER;
277 		sa->sa_mask &= ~bit;
278 		error = copyout(sa, osa, sizeof (vec));
279 		if (error)
280 			return (error);
281 #ifdef KTRACE
282 		if (KTRPOINT(p, KTR_STRUCT))
283 			ovec = vec;
284 #endif
285 	}
286 	if (nsa) {
287 		error = copyin(nsa, sa, sizeof (vec));
288 		if (error)
289 			return (error);
290 #ifdef KTRACE
291 		if (KTRPOINT(p, KTR_STRUCT))
292 			ktrsigaction(p, sa);
293 #endif
294 		setsigvec(p, signum, sa);
295 	}
296 #ifdef KTRACE
297 	if (osa && KTRPOINT(p, KTR_STRUCT))
298 		ktrsigaction(p, &ovec);
299 #endif
300 	return (0);
301 }
302 
303 void
304 setsigvec(struct proc *p, int signum, struct sigaction *sa)
305 {
306 	struct sigacts *ps = p->p_p->ps_sigacts;
307 	int bit;
308 	int s;
309 
310 	bit = sigmask(signum);
311 	/*
312 	 * Change setting atomically.
313 	 */
314 	s = splhigh();
315 	ps->ps_sigact[signum] = sa->sa_handler;
316 	if ((sa->sa_flags & SA_NODEFER) == 0)
317 		sa->sa_mask |= sigmask(signum);
318 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
319 	if (signum == SIGCHLD) {
320 		if (sa->sa_flags & SA_NOCLDSTOP)
321 			atomic_setbits_int(&ps->ps_flags, SAS_NOCLDSTOP);
322 		else
323 			atomic_clearbits_int(&ps->ps_flags, SAS_NOCLDSTOP);
324 		/*
325 		 * If the SA_NOCLDWAIT flag is set or the handler
326 		 * is SIG_IGN we reparent the dying child to PID 1
327 		 * (init) which will reap the zombie.  Because we use
328 		 * init to do our dirty work we never set SAS_NOCLDWAIT
329 		 * for PID 1.
330 		 * XXX exit1 rework means this is unnecessary?
331 		 */
332 		if (initprocess->ps_sigacts != ps &&
333 		    ((sa->sa_flags & SA_NOCLDWAIT) ||
334 		    sa->sa_handler == SIG_IGN))
335 			atomic_setbits_int(&ps->ps_flags, SAS_NOCLDWAIT);
336 		else
337 			atomic_clearbits_int(&ps->ps_flags, SAS_NOCLDWAIT);
338 	}
339 	if ((sa->sa_flags & SA_RESETHAND) != 0)
340 		ps->ps_sigreset |= bit;
341 	else
342 		ps->ps_sigreset &= ~bit;
343 	if ((sa->sa_flags & SA_SIGINFO) != 0)
344 		ps->ps_siginfo |= bit;
345 	else
346 		ps->ps_siginfo &= ~bit;
347 	if ((sa->sa_flags & SA_RESTART) == 0)
348 		ps->ps_sigintr |= bit;
349 	else
350 		ps->ps_sigintr &= ~bit;
351 	if ((sa->sa_flags & SA_ONSTACK) != 0)
352 		ps->ps_sigonstack |= bit;
353 	else
354 		ps->ps_sigonstack &= ~bit;
355 	/*
356 	 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
357 	 * and for signals set to SIG_DFL where the default is to ignore.
358 	 * However, don't put SIGCONT in ps_sigignore,
359 	 * as we have to restart the process.
360 	 */
361 	if (sa->sa_handler == SIG_IGN ||
362 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
363 		atomic_clearbits_int(&p->p_siglist, bit);
364 		if (signum != SIGCONT)
365 			ps->ps_sigignore |= bit;	/* easier in psignal */
366 		ps->ps_sigcatch &= ~bit;
367 	} else {
368 		ps->ps_sigignore &= ~bit;
369 		if (sa->sa_handler == SIG_DFL)
370 			ps->ps_sigcatch &= ~bit;
371 		else
372 			ps->ps_sigcatch |= bit;
373 	}
374 	splx(s);
375 }
376 
377 /*
378  * Initialize signal state for process 0;
379  * set to ignore signals that are ignored by default.
380  */
381 void
382 siginit(struct process *pr)
383 {
384 	struct sigacts *ps = pr->ps_sigacts;
385 	int i;
386 
387 	for (i = 0; i < NSIG; i++)
388 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
389 			ps->ps_sigignore |= sigmask(i);
390 	ps->ps_flags = SAS_NOCLDWAIT | SAS_NOCLDSTOP;
391 }
392 
393 /*
394  * Reset signals for an exec by the specified thread.
395  */
396 void
397 execsigs(struct proc *p)
398 {
399 	struct sigacts *ps;
400 	int nc, mask;
401 
402 	sigactsunshare(p->p_p);
403 	ps = p->p_p->ps_sigacts;
404 
405 	/*
406 	 * Reset caught signals.  Held signals remain held
407 	 * through p_sigmask (unless they were caught,
408 	 * and are now ignored by default).
409 	 */
410 	while (ps->ps_sigcatch) {
411 		nc = ffs((long)ps->ps_sigcatch);
412 		mask = sigmask(nc);
413 		ps->ps_sigcatch &= ~mask;
414 		if (sigprop[nc] & SA_IGNORE) {
415 			if (nc != SIGCONT)
416 				ps->ps_sigignore |= mask;
417 			atomic_clearbits_int(&p->p_siglist, mask);
418 		}
419 		ps->ps_sigact[nc] = SIG_DFL;
420 	}
421 	/*
422 	 * Reset stack state to the user stack.
423 	 * Clear set of signals caught on the signal stack.
424 	 */
425 	sigstkinit(&p->p_sigstk);
426 	ps->ps_flags &= ~SAS_NOCLDWAIT;
427 	if (ps->ps_sigact[SIGCHLD] == SIG_IGN)
428 		ps->ps_sigact[SIGCHLD] = SIG_DFL;
429 }
430 
431 /*
432  * Manipulate signal mask.
433  * Note that we receive new mask, not pointer,
434  * and return old mask as return value;
435  * the library stub does the rest.
436  */
437 int
438 sys_sigprocmask(struct proc *p, void *v, register_t *retval)
439 {
440 	struct sys_sigprocmask_args /* {
441 		syscallarg(int) how;
442 		syscallarg(sigset_t) mask;
443 	} */ *uap = v;
444 	int error = 0;
445 	sigset_t mask;
446 
447 	*retval = p->p_sigmask;
448 	mask = SCARG(uap, mask) &~ sigcantmask;
449 
450 	switch (SCARG(uap, how)) {
451 	case SIG_BLOCK:
452 		atomic_setbits_int(&p->p_sigmask, mask);
453 		break;
454 	case SIG_UNBLOCK:
455 		atomic_clearbits_int(&p->p_sigmask, mask);
456 		break;
457 	case SIG_SETMASK:
458 		p->p_sigmask = mask;
459 		break;
460 	default:
461 		error = EINVAL;
462 		break;
463 	}
464 	return (error);
465 }
466 
467 int
468 sys_sigpending(struct proc *p, void *v, register_t *retval)
469 {
470 
471 	*retval = p->p_siglist;
472 	return (0);
473 }
474 
475 /*
476  * Temporarily replace calling proc's signal mask for the duration of a
477  * system call.  Original signal mask will be restored by userret().
478  */
479 void
480 dosigsuspend(struct proc *p, sigset_t newmask)
481 {
482 	KASSERT(p == curproc);
483 
484 	p->p_oldmask = p->p_sigmask;
485 	atomic_setbits_int(&p->p_flag, P_SIGSUSPEND);
486 	p->p_sigmask = newmask;
487 }
488 
489 /*
490  * Suspend process until signal, providing mask to be set
491  * in the meantime.  Note nonstandard calling convention:
492  * libc stub passes mask, not pointer, to save a copyin.
493  */
494 int
495 sys_sigsuspend(struct proc *p, void *v, register_t *retval)
496 {
497 	struct sys_sigsuspend_args /* {
498 		syscallarg(int) mask;
499 	} */ *uap = v;
500 	struct process *pr = p->p_p;
501 	struct sigacts *ps = pr->ps_sigacts;
502 
503 	dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask);
504 	while (tsleep(ps, PPAUSE|PCATCH, "pause", 0) == 0)
505 		/* void */;
506 	/* always return EINTR rather than ERESTART... */
507 	return (EINTR);
508 }
509 
510 int
511 sigonstack(size_t stack)
512 {
513 	const struct sigaltstack *ss = &curproc->p_sigstk;
514 
515 	return (ss->ss_flags & SS_DISABLE ? 0 :
516 	    (stack - (size_t)ss->ss_sp < ss->ss_size));
517 }
518 
519 int
520 sys_sigaltstack(struct proc *p, void *v, register_t *retval)
521 {
522 	struct sys_sigaltstack_args /* {
523 		syscallarg(const struct sigaltstack *) nss;
524 		syscallarg(struct sigaltstack *) oss;
525 	} */ *uap = v;
526 	struct sigaltstack ss;
527 	const struct sigaltstack *nss;
528 	struct sigaltstack *oss;
529 	int onstack = sigonstack(PROC_STACK(p));
530 	int error;
531 
532 	nss = SCARG(uap, nss);
533 	oss = SCARG(uap, oss);
534 
535 	if (oss != NULL) {
536 		ss = p->p_sigstk;
537 		if (onstack)
538 			ss.ss_flags |= SS_ONSTACK;
539 		if ((error = copyout(&ss, oss, sizeof(ss))))
540 			return (error);
541 	}
542 	if (nss == NULL)
543 		return (0);
544 	error = copyin(nss, &ss, sizeof(ss));
545 	if (error)
546 		return (error);
547 	if (onstack)
548 		return (EPERM);
549 	if (ss.ss_flags & ~SS_DISABLE)
550 		return (EINVAL);
551 	if (ss.ss_flags & SS_DISABLE) {
552 		p->p_sigstk.ss_flags = ss.ss_flags;
553 		return (0);
554 	}
555 	if (ss.ss_size < MINSIGSTKSZ)
556 		return (ENOMEM);
557 
558 	error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size);
559 	if (error)
560 		return (error);
561 
562 	p->p_sigstk = ss;
563 	return (0);
564 }
565 
566 int
567 sys_kill(struct proc *cp, void *v, register_t *retval)
568 {
569 	struct sys_kill_args /* {
570 		syscallarg(int) pid;
571 		syscallarg(int) signum;
572 	} */ *uap = v;
573 	struct process *pr;
574 	int pid = SCARG(uap, pid);
575 	int signum = SCARG(uap, signum);
576 	int error;
577 	int zombie = 0;
578 
579 	if ((error = pledge_kill(cp, pid)) != 0)
580 		return (error);
581 	if (((u_int)signum) >= NSIG)
582 		return (EINVAL);
583 	if (pid > 0) {
584 		if ((pr = prfind(pid)) == NULL) {
585 			if ((pr = zombiefind(pid)) == NULL)
586 				return (ESRCH);
587 			else
588 				zombie = 1;
589 		}
590 		if (!cansignal(cp, pr, signum))
591 			return (EPERM);
592 
593 		/* kill single process */
594 		if (signum && !zombie)
595 			prsignal(pr, signum);
596 		return (0);
597 	}
598 	switch (pid) {
599 	case -1:		/* broadcast signal */
600 		return (killpg1(cp, signum, 0, 1));
601 	case 0:			/* signal own process group */
602 		return (killpg1(cp, signum, 0, 0));
603 	default:		/* negative explicit process group */
604 		return (killpg1(cp, signum, -pid, 0));
605 	}
606 }
607 
608 int
609 sys_thrkill(struct proc *cp, void *v, register_t *retval)
610 {
611 	struct sys_thrkill_args /* {
612 		syscallarg(pid_t) tid;
613 		syscallarg(int) signum;
614 		syscallarg(void *) tcb;
615 	} */ *uap = v;
616 	struct proc *p;
617 	int tid = SCARG(uap, tid);
618 	int signum = SCARG(uap, signum);
619 	void *tcb;
620 
621 	if (((u_int)signum) >= NSIG)
622 		return (EINVAL);
623 	if (tid > THREAD_PID_OFFSET) {
624 		if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL)
625 			return (ESRCH);
626 
627 		/* can only kill threads in the same process */
628 		if (p->p_p != cp->p_p)
629 			return (ESRCH);
630 	} else if (tid == 0)
631 		p = cp;
632 	else
633 		return (EINVAL);
634 
635 	/* optionally require the target thread to have the given tcb addr */
636 	tcb = SCARG(uap, tcb);
637 	if (tcb != NULL && tcb != TCB_GET(p))
638 		return (ESRCH);
639 
640 	if (signum)
641 		ptsignal(p, signum, STHREAD);
642 	return (0);
643 }
644 
645 /*
646  * Common code for kill process group/broadcast kill.
647  * cp is calling process.
648  */
649 int
650 killpg1(struct proc *cp, int signum, int pgid, int all)
651 {
652 	struct process *pr;
653 	struct pgrp *pgrp;
654 	int nfound = 0;
655 
656 	if (all) {
657 		/*
658 		 * broadcast
659 		 */
660 		LIST_FOREACH(pr, &allprocess, ps_list) {
661 			if (pr->ps_pid <= 1 ||
662 			    pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) ||
663 			    pr == cp->p_p || !cansignal(cp, pr, signum))
664 				continue;
665 			nfound++;
666 			if (signum)
667 				prsignal(pr, signum);
668 		}
669 	} else {
670 		if (pgid == 0)
671 			/*
672 			 * zero pgid means send to my process group.
673 			 */
674 			pgrp = cp->p_p->ps_pgrp;
675 		else {
676 			pgrp = pgfind(pgid);
677 			if (pgrp == NULL)
678 				return (ESRCH);
679 		}
680 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
681 			if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM ||
682 			    !cansignal(cp, pr, signum))
683 				continue;
684 			nfound++;
685 			if (signum)
686 				prsignal(pr, signum);
687 		}
688 	}
689 	return (nfound ? 0 : ESRCH);
690 }
691 
692 #define CANDELIVER(uid, euid, pr) \
693 	(euid == 0 || \
694 	(uid) == (pr)->ps_ucred->cr_ruid || \
695 	(uid) == (pr)->ps_ucred->cr_svuid || \
696 	(uid) == (pr)->ps_ucred->cr_uid || \
697 	(euid) == (pr)->ps_ucred->cr_ruid || \
698 	(euid) == (pr)->ps_ucred->cr_svuid || \
699 	(euid) == (pr)->ps_ucred->cr_uid)
700 
701 /*
702  * Deliver signum to pgid, but first check uid/euid against each
703  * process and see if it is permitted.
704  */
705 void
706 csignal(pid_t pgid, int signum, uid_t uid, uid_t euid)
707 {
708 	struct pgrp *pgrp;
709 	struct process *pr;
710 
711 	if (pgid == 0)
712 		return;
713 	if (pgid < 0) {
714 		pgid = -pgid;
715 		if ((pgrp = pgfind(pgid)) == NULL)
716 			return;
717 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
718 			if (CANDELIVER(uid, euid, pr))
719 				prsignal(pr, signum);
720 	} else {
721 		if ((pr = prfind(pgid)) == NULL)
722 			return;
723 		if (CANDELIVER(uid, euid, pr))
724 			prsignal(pr, signum);
725 	}
726 }
727 
728 /*
729  * Send a signal to a process group.
730  */
731 void
732 gsignal(int pgid, int signum)
733 {
734 	struct pgrp *pgrp;
735 
736 	if (pgid && (pgrp = pgfind(pgid)))
737 		pgsignal(pgrp, signum, 0);
738 }
739 
740 /*
741  * Send a signal to a process group.  If checktty is 1,
742  * limit to members which have a controlling terminal.
743  */
744 void
745 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
746 {
747 	struct process *pr;
748 
749 	if (pgrp)
750 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
751 			if (checkctty == 0 || pr->ps_flags & PS_CONTROLT)
752 				prsignal(pr, signum);
753 }
754 
755 /*
756  * Recalculate the signal mask and reset the signal disposition after
757  * usermode frame for delivery is formed.
758  */
759 void
760 postsig_done(struct proc *p, int signum, struct sigacts *ps)
761 {
762 	int mask = sigmask(signum);
763 
764 	KERNEL_ASSERT_LOCKED();
765 
766 	p->p_ru.ru_nsignals++;
767 	atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]);
768 	if ((ps->ps_sigreset & mask) != 0) {
769 		ps->ps_sigcatch &= ~mask;
770 		if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
771 			ps->ps_sigignore |= mask;
772 		ps->ps_sigact[signum] = SIG_DFL;
773 	}
774 }
775 
776 /*
777  * Send a signal caused by a trap to the current thread
778  * If it will be caught immediately, deliver it with correct code.
779  * Otherwise, post it normally.
780  */
781 void
782 trapsignal(struct proc *p, int signum, u_long trapno, int code,
783     union sigval sigval)
784 {
785 	struct process *pr = p->p_p;
786 	struct sigacts *ps = pr->ps_sigacts;
787 	int mask;
788 
789 	switch (signum) {
790 	case SIGILL:
791 	case SIGBUS:
792 	case SIGSEGV:
793 		pr->ps_acflag |= ATRAP;
794 		break;
795 	}
796 
797 	mask = sigmask(signum);
798 	if ((pr->ps_flags & PS_TRACED) == 0 &&
799 	    (ps->ps_sigcatch & mask) != 0 &&
800 	    (p->p_sigmask & mask) == 0) {
801 #ifdef KTRACE
802 		if (KTRPOINT(p, KTR_PSIG)) {
803 			siginfo_t si;
804 
805 			initsiginfo(&si, signum, trapno, code, sigval);
806 			ktrpsig(p, signum, ps->ps_sigact[signum],
807 			    p->p_sigmask, code, &si);
808 		}
809 #endif
810 		(*pr->ps_emul->e_sendsig)(ps->ps_sigact[signum], signum,
811 		    p->p_sigmask, trapno, code, sigval);
812 		postsig_done(p, signum, ps);
813 	} else {
814 		p->p_sisig = signum;
815 		p->p_sitrapno = trapno;	/* XXX for core dump/debugger */
816 		p->p_sicode = code;
817 		p->p_sigval = sigval;
818 
819 		/*
820 		 * Signals like SIGBUS and SIGSEGV should not, when
821 		 * generated by the kernel, be ignorable or blockable.
822 		 * If it is and we're not being traced, then just kill
823 		 * the process.
824 		 */
825 		if ((pr->ps_flags & PS_TRACED) == 0 &&
826 		    (sigprop[signum] & SA_KILL) &&
827 		    ((p->p_sigmask & mask) || (ps->ps_sigignore & mask)))
828 			sigexit(p, signum);
829 		ptsignal(p, signum, STHREAD);
830 	}
831 }
832 
833 /*
834  * Send the signal to the process.  If the signal has an action, the action
835  * is usually performed by the target process rather than the caller; we add
836  * the signal to the set of pending signals for the process.
837  *
838  * Exceptions:
839  *   o When a stop signal is sent to a sleeping process that takes the
840  *     default action, the process is stopped without awakening it.
841  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
842  *     regardless of the signal action (eg, blocked or ignored).
843  *
844  * Other ignored signals are discarded immediately.
845  */
846 void
847 psignal(struct proc *p, int signum)
848 {
849 	ptsignal(p, signum, SPROCESS);
850 }
851 
852 /*
853  * type = SPROCESS	process signal, can be diverted (sigwait())
854  *	XXX if blocked in all threads, mark as pending in struct process
855  * type = STHREAD	thread signal, but should be propagated if unhandled
856  * type = SPROPAGATED	propagated to this thread, so don't propagate again
857  */
858 void
859 ptsignal(struct proc *p, int signum, enum signal_type type)
860 {
861 	int s, prop;
862 	sig_t action;
863 	int mask;
864 	struct process *pr = p->p_p;
865 	struct proc *q;
866 	int wakeparent = 0;
867 
868 #ifdef DIAGNOSTIC
869 	if ((u_int)signum >= NSIG || signum == 0)
870 		panic("psignal signal number");
871 #endif
872 
873 	/* Ignore signal if the target process is exiting */
874 	if (pr->ps_flags & PS_EXITING)
875 		return;
876 
877 	mask = sigmask(signum);
878 
879 	if (type == SPROCESS) {
880 		/* Accept SIGKILL to coredumping processes */
881 		if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) {
882 			if (pr->ps_single != NULL)
883 				p = pr->ps_single;
884 			atomic_setbits_int(&p->p_siglist, mask);
885 			return;
886 		}
887 
888 		/*
889 		 * If the current thread can process the signal
890 		 * immediately (it's unblocked) then have it take it.
891 		 */
892 		q = curproc;
893 		if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 &&
894 		    (q->p_sigmask & mask) == 0)
895 			p = q;
896 		else {
897 			/*
898 			 * A process-wide signal can be diverted to a
899 			 * different thread that's in sigwait() for this
900 			 * signal.  If there isn't such a thread, then
901 			 * pick a thread that doesn't have it blocked so
902 			 * that the stop/kill consideration isn't
903 			 * delayed.  Otherwise, mark it pending on the
904 			 * main thread.
905 			 */
906 			TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
907 				/* ignore exiting threads */
908 				if (q->p_flag & P_WEXIT)
909 					continue;
910 
911 				/* skip threads that have the signal blocked */
912 				if ((q->p_sigmask & mask) != 0)
913 					continue;
914 
915 				/* okay, could send to this thread */
916 				p = q;
917 
918 				/*
919 				 * sigsuspend, sigwait, ppoll/pselect, etc?
920 				 * Definitely go to this thread, as it's
921 				 * already blocked in the kernel.
922 				 */
923 				if (q->p_flag & P_SIGSUSPEND)
924 					break;
925 			}
926 		}
927 	}
928 
929 	if (type != SPROPAGATED)
930 		KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum);
931 
932 	prop = sigprop[signum];
933 
934 	/*
935 	 * If proc is traced, always give parent a chance.
936 	 */
937 	if (pr->ps_flags & PS_TRACED) {
938 		action = SIG_DFL;
939 		atomic_setbits_int(&p->p_siglist, mask);
940 	} else {
941 		/*
942 		 * If the signal is being ignored,
943 		 * then we forget about it immediately.
944 		 * (Note: we don't set SIGCONT in ps_sigignore,
945 		 * and if it is set to SIG_IGN,
946 		 * action will be SIG_DFL here.)
947 		 */
948 		if (pr->ps_sigacts->ps_sigignore & mask)
949 			return;
950 		if (p->p_sigmask & mask) {
951 			action = SIG_HOLD;
952 		} else if (pr->ps_sigacts->ps_sigcatch & mask) {
953 			action = SIG_CATCH;
954 		} else {
955 			action = SIG_DFL;
956 
957 			if (prop & SA_KILL && pr->ps_nice > NZERO)
958 				 pr->ps_nice = NZERO;
959 
960 			/*
961 			 * If sending a tty stop signal to a member of an
962 			 * orphaned process group, discard the signal here if
963 			 * the action is default; don't stop the process below
964 			 * if sleeping, and don't clear any pending SIGCONT.
965 			 */
966 			if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0)
967 				return;
968 		}
969 
970 		atomic_setbits_int(&p->p_siglist, mask);
971 	}
972 
973 	if (prop & SA_CONT)
974 		atomic_clearbits_int(&p->p_siglist, stopsigmask);
975 
976 	if (prop & SA_STOP) {
977 		atomic_clearbits_int(&p->p_siglist, contsigmask);
978 		atomic_clearbits_int(&p->p_flag, P_CONTINUED);
979 	}
980 
981 	/*
982 	 * XXX delay processing of SA_STOP signals unless action == SIG_DFL?
983 	 */
984 	if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED)
985 		TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link)
986 			if (q != p)
987 				ptsignal(q, signum, SPROPAGATED);
988 
989 	/*
990 	 * Defer further processing for signals which are held,
991 	 * except that stopped processes must be continued by SIGCONT.
992 	 */
993 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
994 		return;
995 
996 	SCHED_LOCK(s);
997 
998 	switch (p->p_stat) {
999 
1000 	case SSLEEP:
1001 		/*
1002 		 * If process is sleeping uninterruptibly
1003 		 * we can't interrupt the sleep... the signal will
1004 		 * be noticed when the process returns through
1005 		 * trap() or syscall().
1006 		 */
1007 		if ((p->p_flag & P_SINTR) == 0)
1008 			goto out;
1009 		/*
1010 		 * Process is sleeping and traced... make it runnable
1011 		 * so it can discover the signal in issignal() and stop
1012 		 * for the parent.
1013 		 */
1014 		if (pr->ps_flags & PS_TRACED)
1015 			goto run;
1016 		/*
1017 		 * If SIGCONT is default (or ignored) and process is
1018 		 * asleep, we are finished; the process should not
1019 		 * be awakened.
1020 		 */
1021 		if ((prop & SA_CONT) && action == SIG_DFL) {
1022 			atomic_clearbits_int(&p->p_siglist, mask);
1023 			goto out;
1024 		}
1025 		/*
1026 		 * When a sleeping process receives a stop
1027 		 * signal, process immediately if possible.
1028 		 */
1029 		if ((prop & SA_STOP) && action == SIG_DFL) {
1030 			/*
1031 			 * If a child holding parent blocked,
1032 			 * stopping could cause deadlock.
1033 			 */
1034 			if (pr->ps_flags & PS_PPWAIT)
1035 				goto out;
1036 			atomic_clearbits_int(&p->p_siglist, mask);
1037 			p->p_xstat = signum;
1038 			proc_stop(p, 0);
1039 			goto out;
1040 		}
1041 		/*
1042 		 * All other (caught or default) signals
1043 		 * cause the process to run.
1044 		 */
1045 		goto runfast;
1046 		/*NOTREACHED*/
1047 
1048 	case SSTOP:
1049 		/*
1050 		 * If traced process is already stopped,
1051 		 * then no further action is necessary.
1052 		 */
1053 		if (pr->ps_flags & PS_TRACED)
1054 			goto out;
1055 
1056 		/*
1057 		 * Kill signal always sets processes running.
1058 		 */
1059 		if (signum == SIGKILL) {
1060 			atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1061 			goto runfast;
1062 		}
1063 
1064 		if (prop & SA_CONT) {
1065 			/*
1066 			 * If SIGCONT is default (or ignored), we continue the
1067 			 * process but don't leave the signal in p_siglist, as
1068 			 * it has no further action.  If SIGCONT is held, we
1069 			 * continue the process and leave the signal in
1070 			 * p_siglist.  If the process catches SIGCONT, let it
1071 			 * handle the signal itself.  If it isn't waiting on
1072 			 * an event, then it goes back to run state.
1073 			 * Otherwise, process goes back to sleep state.
1074 			 */
1075 			atomic_setbits_int(&p->p_flag, P_CONTINUED);
1076 			atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1077 			wakeparent = 1;
1078 			if (action == SIG_DFL)
1079 				atomic_clearbits_int(&p->p_siglist, mask);
1080 			if (action == SIG_CATCH)
1081 				goto runfast;
1082 			if (p->p_wchan == 0)
1083 				goto run;
1084 			p->p_stat = SSLEEP;
1085 			goto out;
1086 		}
1087 
1088 		if (prop & SA_STOP) {
1089 			/*
1090 			 * Already stopped, don't need to stop again.
1091 			 * (If we did the shell could get confused.)
1092 			 */
1093 			atomic_clearbits_int(&p->p_siglist, mask);
1094 			goto out;
1095 		}
1096 
1097 		/*
1098 		 * If process is sleeping interruptibly, then simulate a
1099 		 * wakeup so that when it is continued, it will be made
1100 		 * runnable and can look at the signal.  But don't make
1101 		 * the process runnable, leave it stopped.
1102 		 */
1103 		if (p->p_wchan && p->p_flag & P_SINTR)
1104 			unsleep(p);
1105 		goto out;
1106 
1107 	case SONPROC:
1108 		signotify(p);
1109 		/* FALLTHROUGH */
1110 	default:
1111 		/*
1112 		 * SRUN, SIDL, SDEAD do nothing with the signal,
1113 		 * other than kicking ourselves if we are running.
1114 		 * It will either never be noticed, or noticed very soon.
1115 		 */
1116 		goto out;
1117 	}
1118 	/*NOTREACHED*/
1119 
1120 runfast:
1121 	/*
1122 	 * Raise priority to at least PUSER.
1123 	 */
1124 	if (p->p_priority > PUSER)
1125 		p->p_priority = PUSER;
1126 run:
1127 	setrunnable(p);
1128 out:
1129 	SCHED_UNLOCK(s);
1130 	if (wakeparent)
1131 		wakeup(pr->ps_pptr);
1132 }
1133 
1134 /*
1135  * If the current process has received a signal (should be caught or cause
1136  * termination, should interrupt current syscall), return the signal number.
1137  * Stop signals with default action are processed immediately, then cleared;
1138  * they aren't returned.  This is checked after each entry to the system for
1139  * a syscall or trap (though this can usually be done without calling issignal
1140  * by checking the pending signal masks in the CURSIG macro.) The normal call
1141  * sequence is
1142  *
1143  *	while (signum = CURSIG(curproc))
1144  *		postsig(signum);
1145  *
1146  * Assumes that if the P_SINTR flag is set, we're holding both the
1147  * kernel and scheduler locks.
1148  */
1149 int
1150 issignal(struct proc *p)
1151 {
1152 	struct process *pr = p->p_p;
1153 	int signum, mask, prop;
1154 	int dolock = (p->p_flag & P_SINTR) == 0;
1155 	int s;
1156 
1157 	for (;;) {
1158 		mask = p->p_siglist & ~p->p_sigmask;
1159 		if (pr->ps_flags & PS_PPWAIT)
1160 			mask &= ~stopsigmask;
1161 		if (mask == 0)	 	/* no signal to send */
1162 			return (0);
1163 		signum = ffs((long)mask);
1164 		mask = sigmask(signum);
1165 		atomic_clearbits_int(&p->p_siglist, mask);
1166 
1167 		/*
1168 		 * We should see pending but ignored signals
1169 		 * only if PS_TRACED was on when they were posted.
1170 		 */
1171 		if (mask & pr->ps_sigacts->ps_sigignore &&
1172 		    (pr->ps_flags & PS_TRACED) == 0)
1173 			continue;
1174 
1175 		/*
1176 		 * If traced, always stop, and stay stopped until released
1177 		 * by the debugger.  If our parent process is waiting for
1178 		 * us, don't hang as we could deadlock.
1179 		 */
1180 		if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) &&
1181 		    signum != SIGKILL) {
1182 			p->p_xstat = signum;
1183 
1184 			if (dolock)
1185 				KERNEL_LOCK();
1186 			single_thread_set(p, SINGLE_PTRACE, 0);
1187 			if (dolock)
1188 				KERNEL_UNLOCK();
1189 
1190 			if (dolock)
1191 				SCHED_LOCK(s);
1192 			proc_stop(p, 1);
1193 			if (dolock)
1194 				SCHED_UNLOCK(s);
1195 
1196 			if (dolock)
1197 				KERNEL_LOCK();
1198 			single_thread_clear(p, 0);
1199 			if (dolock)
1200 				KERNEL_UNLOCK();
1201 
1202 			/*
1203 			 * If we are no longer being traced, or the parent
1204 			 * didn't give us a signal, look for more signals.
1205 			 */
1206 			if ((pr->ps_flags & PS_TRACED) == 0 || p->p_xstat == 0)
1207 				continue;
1208 
1209 			/*
1210 			 * If the new signal is being masked, look for other
1211 			 * signals.
1212 			 */
1213 			signum = p->p_xstat;
1214 			mask = sigmask(signum);
1215 			if ((p->p_sigmask & mask) != 0)
1216 				continue;
1217 
1218 			/* take the signal! */
1219 			atomic_clearbits_int(&p->p_siglist, mask);
1220 		}
1221 
1222 		prop = sigprop[signum];
1223 
1224 		/*
1225 		 * Decide whether the signal should be returned.
1226 		 * Return the signal's number, or fall through
1227 		 * to clear it from the pending mask.
1228 		 */
1229 		switch ((long)pr->ps_sigacts->ps_sigact[signum]) {
1230 		case (long)SIG_DFL:
1231 			/*
1232 			 * Don't take default actions on system processes.
1233 			 */
1234 			if (pr->ps_pid <= 1) {
1235 #ifdef DIAGNOSTIC
1236 				/*
1237 				 * Are you sure you want to ignore SIGSEGV
1238 				 * in init? XXX
1239 				 */
1240 				printf("Process (pid %d) got signal"
1241 				    " %d\n", pr->ps_pid, signum);
1242 #endif
1243 				break;		/* == ignore */
1244 			}
1245 			/*
1246 			 * If there is a pending stop signal to process
1247 			 * with default action, stop here,
1248 			 * then clear the signal.  However,
1249 			 * if process is member of an orphaned
1250 			 * process group, ignore tty stop signals.
1251 			 */
1252 			if (prop & SA_STOP) {
1253 				if (pr->ps_flags & PS_TRACED ||
1254 		    		    (pr->ps_pgrp->pg_jobc == 0 &&
1255 				    prop & SA_TTYSTOP))
1256 					break;	/* == ignore */
1257 				p->p_xstat = signum;
1258 				if (dolock)
1259 					SCHED_LOCK(s);
1260 				proc_stop(p, 1);
1261 				if (dolock)
1262 					SCHED_UNLOCK(s);
1263 				break;
1264 			} else if (prop & SA_IGNORE) {
1265 				/*
1266 				 * Except for SIGCONT, shouldn't get here.
1267 				 * Default action is to ignore; drop it.
1268 				 */
1269 				break;		/* == ignore */
1270 			} else
1271 				goto keep;
1272 			/*NOTREACHED*/
1273 		case (long)SIG_IGN:
1274 			/*
1275 			 * Masking above should prevent us ever trying
1276 			 * to take action on an ignored signal other
1277 			 * than SIGCONT, unless process is traced.
1278 			 */
1279 			if ((prop & SA_CONT) == 0 &&
1280 			    (pr->ps_flags & PS_TRACED) == 0)
1281 				printf("issignal\n");
1282 			break;		/* == ignore */
1283 		default:
1284 			/*
1285 			 * This signal has an action, let
1286 			 * postsig() process it.
1287 			 */
1288 			goto keep;
1289 		}
1290 	}
1291 	/* NOTREACHED */
1292 
1293 keep:
1294 	atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */
1295 	return (signum);
1296 }
1297 
1298 /*
1299  * Put the argument process into the stopped state and notify the parent
1300  * via wakeup.  Signals are handled elsewhere.  The process must not be
1301  * on the run queue.
1302  */
1303 void
1304 proc_stop(struct proc *p, int sw)
1305 {
1306 	struct process *pr = p->p_p;
1307 	extern void *softclock_si;
1308 
1309 #ifdef MULTIPROCESSOR
1310 	SCHED_ASSERT_LOCKED();
1311 #endif
1312 
1313 	p->p_stat = SSTOP;
1314 	atomic_clearbits_int(&pr->ps_flags, PS_WAITED);
1315 	atomic_setbits_int(&pr->ps_flags, PS_STOPPED);
1316 	atomic_setbits_int(&p->p_flag, P_SUSPSIG);
1317 	if (!timeout_pending(&proc_stop_to)) {
1318 		timeout_add(&proc_stop_to, 0);
1319 		/*
1320 		 * We need this soft interrupt to be handled fast.
1321 		 * Extra calls to softclock don't hurt.
1322 		 */
1323                 softintr_schedule(softclock_si);
1324 	}
1325 	if (sw)
1326 		mi_switch();
1327 }
1328 
1329 /*
1330  * Called from a timeout to send signals to the parents of stopped processes.
1331  * We can't do this in proc_stop because it's called with nasty locks held
1332  * and we would need recursive scheduler lock to deal with that.
1333  */
1334 void
1335 proc_stop_sweep(void *v)
1336 {
1337 	struct process *pr;
1338 
1339 	LIST_FOREACH(pr, &allprocess, ps_list) {
1340 		if ((pr->ps_flags & PS_STOPPED) == 0)
1341 			continue;
1342 		atomic_clearbits_int(&pr->ps_flags, PS_STOPPED);
1343 
1344 		if ((pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDSTOP) == 0)
1345 			prsignal(pr->ps_pptr, SIGCHLD);
1346 		wakeup(pr->ps_pptr);
1347 	}
1348 }
1349 
1350 /*
1351  * Take the action for the specified signal
1352  * from the current set of pending signals.
1353  */
1354 void
1355 postsig(struct proc *p, int signum)
1356 {
1357 	struct process *pr = p->p_p;
1358 	struct sigacts *ps = pr->ps_sigacts;
1359 	sig_t action;
1360 	u_long trapno;
1361 	int mask, returnmask;
1362 	union sigval sigval;
1363 	int s, code;
1364 
1365 	KASSERT(signum != 0);
1366 	KERNEL_ASSERT_LOCKED();
1367 
1368 	mask = sigmask(signum);
1369 	atomic_clearbits_int(&p->p_siglist, mask);
1370 	action = ps->ps_sigact[signum];
1371 	sigval.sival_ptr = 0;
1372 
1373 	if (p->p_sisig != signum) {
1374 		trapno = 0;
1375 		code = SI_USER;
1376 		sigval.sival_ptr = 0;
1377 	} else {
1378 		trapno = p->p_sitrapno;
1379 		code = p->p_sicode;
1380 		sigval = p->p_sigval;
1381 	}
1382 
1383 #ifdef KTRACE
1384 	if (KTRPOINT(p, KTR_PSIG)) {
1385 		siginfo_t si;
1386 
1387 		initsiginfo(&si, signum, trapno, code, sigval);
1388 		ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ?
1389 		    p->p_oldmask : p->p_sigmask, code, &si);
1390 	}
1391 #endif
1392 	if (action == SIG_DFL) {
1393 		/*
1394 		 * Default action, where the default is to kill
1395 		 * the process.  (Other cases were ignored above.)
1396 		 */
1397 		sigexit(p, signum);
1398 		/* NOTREACHED */
1399 	} else {
1400 		/*
1401 		 * If we get here, the signal must be caught.
1402 		 */
1403 #ifdef DIAGNOSTIC
1404 		if (action == SIG_IGN || (p->p_sigmask & mask))
1405 			panic("postsig action");
1406 #endif
1407 		/*
1408 		 * Set the new mask value and also defer further
1409 		 * occurrences of this signal.
1410 		 *
1411 		 * Special case: user has done a sigpause.  Here the
1412 		 * current mask is not of interest, but rather the
1413 		 * mask from before the sigpause is what we want
1414 		 * restored after the signal processing is completed.
1415 		 */
1416 #ifdef MULTIPROCESSOR
1417 		s = splsched();
1418 #else
1419 		s = splhigh();
1420 #endif
1421 		if (p->p_flag & P_SIGSUSPEND) {
1422 			atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1423 			returnmask = p->p_oldmask;
1424 		} else {
1425 			returnmask = p->p_sigmask;
1426 		}
1427 		if (p->p_sisig == signum) {
1428 			p->p_sisig = 0;
1429 			p->p_sitrapno = 0;
1430 			p->p_sicode = SI_USER;
1431 			p->p_sigval.sival_ptr = NULL;
1432 		}
1433 
1434 		(*pr->ps_emul->e_sendsig)(action, signum, returnmask, trapno,
1435 		    code, sigval);
1436 		postsig_done(p, signum, ps);
1437 		splx(s);
1438 	}
1439 }
1440 
1441 /*
1442  * Force the current process to exit with the specified signal, dumping core
1443  * if appropriate.  We bypass the normal tests for masked and caught signals,
1444  * allowing unrecoverable failures to terminate the process without changing
1445  * signal state.  Mark the accounting record with the signal termination.
1446  * If dumping core, save the signal number for the debugger.  Calls exit and
1447  * does not return.
1448  */
1449 void
1450 sigexit(struct proc *p, int signum)
1451 {
1452 	/* Mark process as going away */
1453 	atomic_setbits_int(&p->p_flag, P_WEXIT);
1454 
1455 	p->p_p->ps_acflag |= AXSIG;
1456 	if (sigprop[signum] & SA_CORE) {
1457 		p->p_sisig = signum;
1458 
1459 		/* if there are other threads, pause them */
1460 		if (P_HASSIBLING(p))
1461 			single_thread_set(p, SINGLE_SUSPEND, 0);
1462 
1463 		if (coredump(p) == 0)
1464 			signum |= WCOREFLAG;
1465 	}
1466 	exit1(p, W_EXITCODE(0, signum), EXIT_NORMAL);
1467 	/* NOTREACHED */
1468 }
1469 
1470 int nosuidcoredump = 1;
1471 
1472 struct coredump_iostate {
1473 	struct proc *io_proc;
1474 	struct vnode *io_vp;
1475 	struct ucred *io_cred;
1476 	off_t io_offset;
1477 };
1478 
1479 /*
1480  * Dump core, into a file named "progname.core", unless the process was
1481  * setuid/setgid.
1482  */
1483 int
1484 coredump(struct proc *p)
1485 {
1486 #ifdef SMALL_KERNEL
1487 	return EPERM;
1488 #else
1489 	struct process *pr = p->p_p;
1490 	struct vnode *vp;
1491 	struct ucred *cred = p->p_ucred;
1492 	struct vmspace *vm = p->p_vmspace;
1493 	struct nameidata nd;
1494 	struct vattr vattr;
1495 	struct coredump_iostate	io;
1496 	int error, len, incrash = 0;
1497 	char name[MAXPATHLEN];
1498 	const char *dir = "/var/crash";
1499 
1500 	if (pr->ps_emul->e_coredump == NULL)
1501 		return (EINVAL);
1502 
1503 	pr->ps_flags |= PS_COREDUMP;
1504 
1505 	/*
1506 	 * If the process has inconsistent uids, nosuidcoredump
1507 	 * determines coredump placement policy.
1508 	 */
1509 	if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) ||
1510 	   ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) {
1511 		if (nosuidcoredump == 3 || nosuidcoredump == 2)
1512 			incrash = 1;
1513 		else
1514 			return (EPERM);
1515 	}
1516 
1517 	/* Don't dump if will exceed file size limit. */
1518 	if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >=
1519 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1520 		return (EFBIG);
1521 
1522 	if (incrash && nosuidcoredump == 3) {
1523 		/*
1524 		 * If the program directory does not exist, dumps of
1525 		 * that core will silently fail.
1526 		 */
1527 		len = snprintf(name, sizeof(name), "%s/%s/%u.core",
1528 		    dir, pr->ps_comm, pr->ps_pid);
1529 	} else if (incrash && nosuidcoredump == 2)
1530 		len = snprintf(name, sizeof(name), "%s/%s.core",
1531 		    dir, pr->ps_comm);
1532 	else
1533 		len = snprintf(name, sizeof(name), "%s.core", pr->ps_comm);
1534 	if (len >= sizeof(name))
1535 		return (EACCES);
1536 
1537 	/*
1538 	 * Control the UID used to write out.  The normal case uses
1539 	 * the real UID.  If the sugid case is going to write into the
1540 	 * controlled directory, we do so as root.
1541 	 */
1542 	if (incrash == 0) {
1543 		cred = crdup(cred);
1544 		cred->cr_uid = cred->cr_ruid;
1545 		cred->cr_gid = cred->cr_rgid;
1546 	} else {
1547 		if (p->p_fd->fd_rdir) {
1548 			vrele(p->p_fd->fd_rdir);
1549 			p->p_fd->fd_rdir = NULL;
1550 		}
1551 		p->p_ucred = crdup(p->p_ucred);
1552 		crfree(cred);
1553 		cred = p->p_ucred;
1554 		crhold(cred);
1555 		cred->cr_uid = 0;
1556 		cred->cr_gid = 0;
1557 	}
1558 
1559 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1560 
1561 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1562 
1563 	if (error)
1564 		goto out;
1565 
1566 	/*
1567 	 * Don't dump to non-regular files, files with links, or files
1568 	 * owned by someone else.
1569 	 */
1570 	vp = nd.ni_vp;
1571 	if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) {
1572 		VOP_UNLOCK(vp);
1573 		vn_close(vp, FWRITE, cred, p);
1574 		goto out;
1575 	}
1576 	if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1577 	    vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) ||
1578 	    vattr.va_uid != cred->cr_uid) {
1579 		error = EACCES;
1580 		VOP_UNLOCK(vp);
1581 		vn_close(vp, FWRITE, cred, p);
1582 		goto out;
1583 	}
1584 	VATTR_NULL(&vattr);
1585 	vattr.va_size = 0;
1586 	VOP_SETATTR(vp, &vattr, cred, p);
1587 	pr->ps_acflag |= ACORE;
1588 
1589 	io.io_proc = p;
1590 	io.io_vp = vp;
1591 	io.io_cred = cred;
1592 	io.io_offset = 0;
1593 	VOP_UNLOCK(vp);
1594 	vref(vp);
1595 	error = vn_close(vp, FWRITE, cred, p);
1596 	if (error == 0)
1597 		error = (*pr->ps_emul->e_coredump)(p, &io);
1598 	vrele(vp);
1599 out:
1600 	crfree(cred);
1601 	return (error);
1602 #endif
1603 }
1604 
1605 #ifndef SMALL_KERNEL
1606 int
1607 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
1608 {
1609 	struct coredump_iostate *io = cookie;
1610 	off_t coffset = 0;
1611 	size_t csize;
1612 	int chunk, error;
1613 
1614 	csize = len;
1615 	do {
1616 		if (io->io_proc->p_siglist & sigmask(SIGKILL))
1617 			return (EINTR);
1618 
1619 		/* Rest of the loop sleeps with lock held, so... */
1620 		yield();
1621 
1622 		chunk = MIN(csize, MAXPHYS);
1623 		error = vn_rdwr(UIO_WRITE, io->io_vp,
1624 		    (caddr_t)data + coffset, chunk,
1625 		    io->io_offset + coffset, segflg,
1626 		    IO_UNIT, io->io_cred, NULL, io->io_proc);
1627 		if (error) {
1628 			struct process *pr = io->io_proc->p_p;
1629 			if (error == ENOSPC)
1630 				log(LOG_ERR, "coredump of %s(%d) failed, filesystem full\n",
1631 				    pr->ps_comm, pr->ps_pid);
1632 			else
1633 				log(LOG_ERR, "coredump of %s(%d), write failed: errno %d\n",
1634 				    pr->ps_comm, pr->ps_pid, error);
1635 			return (error);
1636 		}
1637 
1638 		coffset += chunk;
1639 		csize -= chunk;
1640 	} while (csize > 0);
1641 
1642 	io->io_offset += len;
1643 	return (0);
1644 }
1645 
1646 void
1647 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end)
1648 {
1649 	struct coredump_iostate *io = cookie;
1650 
1651 	uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end);
1652 }
1653 
1654 #endif	/* !SMALL_KERNEL */
1655 
1656 /*
1657  * Nonexistent system call-- signal process (may want to handle it).
1658  * Flag error in case process won't see signal immediately (blocked or ignored).
1659  */
1660 int
1661 sys_nosys(struct proc *p, void *v, register_t *retval)
1662 {
1663 
1664 	ptsignal(p, SIGSYS, STHREAD);
1665 	return (ENOSYS);
1666 }
1667 
1668 int
1669 sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
1670 {
1671 	static int sigwaitsleep;
1672 	struct sys___thrsigdivert_args /* {
1673 		syscallarg(sigset_t) sigmask;
1674 		syscallarg(siginfo_t *) info;
1675 		syscallarg(const struct timespec *) timeout;
1676 	} */ *uap = v;
1677 	struct process *pr = p->p_p;
1678 	sigset_t *m;
1679 	sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask;
1680 	siginfo_t si;
1681 	uint64_t to_ticks = 0;
1682 	int timeinvalid = 0;
1683 	int error = 0;
1684 
1685 	memset(&si, 0, sizeof(si));
1686 
1687 	if (SCARG(uap, timeout) != NULL) {
1688 		struct timespec ts;
1689 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1690 			return (error);
1691 #ifdef KTRACE
1692 		if (KTRPOINT(p, KTR_STRUCT))
1693 			ktrreltimespec(p, &ts);
1694 #endif
1695 		if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1696 			timeinvalid = 1;
1697 		else {
1698 			to_ticks = (uint64_t)hz * ts.tv_sec +
1699 			    ts.tv_nsec / (tick * 1000);
1700 			if (to_ticks > INT_MAX)
1701 				to_ticks = INT_MAX;
1702 			if (to_ticks == 0 && ts.tv_nsec)
1703 				to_ticks = 1;
1704 		}
1705 	}
1706 
1707 	dosigsuspend(p, p->p_sigmask &~ mask);
1708 	for (;;) {
1709 		si.si_signo = CURSIG(p);
1710 		if (si.si_signo != 0) {
1711 			sigset_t smask = sigmask(si.si_signo);
1712 			if (smask & mask) {
1713 				if (p->p_siglist & smask)
1714 					m = &p->p_siglist;
1715 				else if (pr->ps_mainproc->p_siglist & smask)
1716 					m = &pr->ps_mainproc->p_siglist;
1717 				else {
1718 					/* signal got eaten by someone else? */
1719 					continue;
1720 				}
1721 				atomic_clearbits_int(m, smask);
1722 				error = 0;
1723 				break;
1724 			}
1725 		}
1726 
1727 		/* per-POSIX, delay this error until after the above */
1728 		if (timeinvalid)
1729 			error = EINVAL;
1730 
1731 		if (SCARG(uap, timeout) != NULL && to_ticks == 0)
1732 			error = EAGAIN;
1733 
1734 		if (error != 0)
1735 			break;
1736 
1737 		error = tsleep(&sigwaitsleep, PPAUSE|PCATCH, "sigwait",
1738 		    (int)to_ticks);
1739 	}
1740 
1741 	if (error == 0) {
1742 		*retval = si.si_signo;
1743 		if (SCARG(uap, info) != NULL)
1744 			error = copyout(&si, SCARG(uap, info), sizeof(si));
1745 	} else if (error == ERESTART && SCARG(uap, timeout) != NULL) {
1746 		/*
1747 		 * Restarting is wrong if there's a timeout, as it'll be
1748 		 * for the same interval again
1749 		 */
1750 		error = EINTR;
1751 	}
1752 
1753 	return (error);
1754 }
1755 
1756 void
1757 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val)
1758 {
1759 	memset(si, 0, sizeof(*si));
1760 
1761 	si->si_signo = sig;
1762 	si->si_code = code;
1763 	if (code == SI_USER) {
1764 		si->si_value = val;
1765 	} else {
1766 		switch (sig) {
1767 		case SIGSEGV:
1768 		case SIGILL:
1769 		case SIGBUS:
1770 		case SIGFPE:
1771 			si->si_addr = val.sival_ptr;
1772 			si->si_trapno = trapno;
1773 			break;
1774 		case SIGXFSZ:
1775 			break;
1776 		}
1777 	}
1778 }
1779 
1780 int
1781 filt_sigattach(struct knote *kn)
1782 {
1783 	struct process *pr = curproc->p_p;
1784 
1785 	if (kn->kn_id >= NSIG)
1786 		return EINVAL;
1787 
1788 	kn->kn_ptr.p_process = pr;
1789 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1790 
1791 	/* XXX lock the proc here while adding to the list? */
1792 	SLIST_INSERT_HEAD(&pr->ps_klist, kn, kn_selnext);
1793 
1794 	return (0);
1795 }
1796 
1797 void
1798 filt_sigdetach(struct knote *kn)
1799 {
1800 	struct process *pr = kn->kn_ptr.p_process;
1801 
1802 	SLIST_REMOVE(&pr->ps_klist, kn, knote, kn_selnext);
1803 }
1804 
1805 /*
1806  * signal knotes are shared with proc knotes, so we apply a mask to
1807  * the hint in order to differentiate them from process hints.  This
1808  * could be avoided by using a signal-specific knote list, but probably
1809  * isn't worth the trouble.
1810  */
1811 int
1812 filt_signal(struct knote *kn, long hint)
1813 {
1814 
1815 	if (hint & NOTE_SIGNAL) {
1816 		hint &= ~NOTE_SIGNAL;
1817 
1818 		if (kn->kn_id == hint)
1819 			kn->kn_data++;
1820 	}
1821 	return (kn->kn_data != 0);
1822 }
1823 
1824 void
1825 userret(struct proc *p)
1826 {
1827 	int signum;
1828 
1829 	/* send SIGPROF or SIGVTALRM if their timers interrupted this thread */
1830 	if (p->p_flag & P_PROFPEND) {
1831 		atomic_clearbits_int(&p->p_flag, P_PROFPEND);
1832 		KERNEL_LOCK();
1833 		psignal(p, SIGPROF);
1834 		KERNEL_UNLOCK();
1835 	}
1836 	if (p->p_flag & P_ALRMPEND) {
1837 		atomic_clearbits_int(&p->p_flag, P_ALRMPEND);
1838 		KERNEL_LOCK();
1839 		psignal(p, SIGVTALRM);
1840 		KERNEL_UNLOCK();
1841 	}
1842 
1843 	if (SIGPENDING(p)) {
1844 		KERNEL_LOCK();
1845 		while ((signum = CURSIG(p)) != 0)
1846 			postsig(p, signum);
1847 		KERNEL_UNLOCK();
1848 	}
1849 
1850 	/*
1851 	 * If P_SIGSUSPEND is still set here, then we still need to restore
1852 	 * the original sigmask before returning to userspace.  Also, this
1853 	 * might unmask some pending signals, so we need to check a second
1854 	 * time for signals to post.
1855 	 */
1856 	if (p->p_flag & P_SIGSUSPEND) {
1857 		atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1858 		p->p_sigmask = p->p_oldmask;
1859 
1860 		KERNEL_LOCK();
1861 		while ((signum = CURSIG(p)) != 0)
1862 			postsig(p, signum);
1863 		KERNEL_UNLOCK();
1864 	}
1865 
1866 	if (p->p_flag & P_SUSPSINGLE) {
1867 		KERNEL_LOCK();
1868 		single_thread_check(p, 0);
1869 		KERNEL_UNLOCK();
1870 	}
1871 
1872 	WITNESS_WARN(WARN_PANIC, NULL, "userret: returning");
1873 
1874 	p->p_cpu->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
1875 }
1876 
1877 int
1878 single_thread_check(struct proc *p, int deep)
1879 {
1880 	struct process *pr = p->p_p;
1881 
1882 	if (pr->ps_single != NULL && pr->ps_single != p) {
1883 		do {
1884 			int s;
1885 
1886 			/* if we're in deep, we need to unwind to the edge */
1887 			if (deep) {
1888 				if (pr->ps_flags & PS_SINGLEUNWIND)
1889 					return (ERESTART);
1890 				if (pr->ps_flags & PS_SINGLEEXIT)
1891 					return (EINTR);
1892 			}
1893 
1894 			if (--pr->ps_singlecount == 0)
1895 				wakeup(&pr->ps_singlecount);
1896 			if (pr->ps_flags & PS_SINGLEEXIT)
1897 				exit1(p, 0, EXIT_THREAD_NOCHECK);
1898 
1899 			/* not exiting and don't need to unwind, so suspend */
1900 			SCHED_LOCK(s);
1901 			p->p_stat = SSTOP;
1902 			mi_switch();
1903 			SCHED_UNLOCK(s);
1904 		} while (pr->ps_single != NULL);
1905 	}
1906 
1907 	return (0);
1908 }
1909 
1910 /*
1911  * Stop other threads in the process.  The mode controls how and
1912  * where the other threads should stop:
1913  *  - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit
1914  *    (by setting to SINGLE_EXIT) or be released (via single_thread_clear())
1915  *  - SINGLE_PTRACE: stop wherever they are, will wait for them to stop
1916  *    later (via single_thread_wait()) and released as with SINGLE_SUSPEND
1917  *  - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit
1918  *    or released as with SINGLE_SUSPEND
1919  *  - SINGLE_EXIT: unwind to kernel boundary and exit
1920  */
1921 int
1922 single_thread_set(struct proc *p, enum single_thread_mode mode, int deep)
1923 {
1924 	struct process *pr = p->p_p;
1925 	struct proc *q;
1926 	int error;
1927 
1928 	KERNEL_ASSERT_LOCKED();
1929 
1930 	if ((error = single_thread_check(p, deep)))
1931 		return error;
1932 
1933 	switch (mode) {
1934 	case SINGLE_SUSPEND:
1935 	case SINGLE_PTRACE:
1936 		break;
1937 	case SINGLE_UNWIND:
1938 		atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
1939 		break;
1940 	case SINGLE_EXIT:
1941 		atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT);
1942 		atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
1943 		break;
1944 #ifdef DIAGNOSTIC
1945 	default:
1946 		panic("single_thread_mode = %d", mode);
1947 #endif
1948 	}
1949 	pr->ps_single = p;
1950 	pr->ps_singlecount = 0;
1951 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
1952 		int s;
1953 
1954 		if (q == p)
1955 			continue;
1956 		if (q->p_flag & P_WEXIT) {
1957 			if (mode == SINGLE_EXIT) {
1958 				SCHED_LOCK(s);
1959 				if (q->p_stat == SSTOP) {
1960 					setrunnable(q);
1961 					pr->ps_singlecount++;
1962 				}
1963 				SCHED_UNLOCK(s);
1964 			}
1965 			continue;
1966 		}
1967 		SCHED_LOCK(s);
1968 		atomic_setbits_int(&q->p_flag, P_SUSPSINGLE);
1969 		switch (q->p_stat) {
1970 		case SIDL:
1971 		case SRUN:
1972 			pr->ps_singlecount++;
1973 			break;
1974 		case SSLEEP:
1975 			/* if it's not interruptible, then just have to wait */
1976 			if (q->p_flag & P_SINTR) {
1977 				/* merely need to suspend?  just stop it */
1978 				if (mode == SINGLE_SUSPEND ||
1979 				    mode == SINGLE_PTRACE) {
1980 					q->p_stat = SSTOP;
1981 					break;
1982 				}
1983 				/* need to unwind or exit, so wake it */
1984 				setrunnable(q);
1985 			}
1986 			pr->ps_singlecount++;
1987 			break;
1988 		case SSTOP:
1989 			if (mode == SINGLE_EXIT) {
1990 				setrunnable(q);
1991 				pr->ps_singlecount++;
1992 			}
1993 			break;
1994 		case SDEAD:
1995 			break;
1996 		case SONPROC:
1997 			pr->ps_singlecount++;
1998 			signotify(q);
1999 			break;
2000 		}
2001 		SCHED_UNLOCK(s);
2002 	}
2003 
2004 	if (mode != SINGLE_PTRACE)
2005 		single_thread_wait(pr);
2006 
2007 	return 0;
2008 }
2009 
2010 void
2011 single_thread_wait(struct process *pr)
2012 {
2013 	/* wait until they're all suspended */
2014 	while (pr->ps_singlecount > 0)
2015 		tsleep(&pr->ps_singlecount, PUSER, "suspend", 0);
2016 }
2017 
2018 void
2019 single_thread_clear(struct proc *p, int flag)
2020 {
2021 	struct process *pr = p->p_p;
2022 	struct proc *q;
2023 
2024 	KASSERT(pr->ps_single == p);
2025 	KERNEL_ASSERT_LOCKED();
2026 
2027 	pr->ps_single = NULL;
2028 	atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT);
2029 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2030 		int s;
2031 
2032 		if (q == p || (q->p_flag & P_SUSPSINGLE) == 0)
2033 			continue;
2034 		atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE);
2035 
2036 		/*
2037 		 * if the thread was only stopped for single threading
2038 		 * then clearing that either makes it runnable or puts
2039 		 * it back into some sleep queue
2040 		 */
2041 		SCHED_LOCK(s);
2042 		if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) {
2043 			if (q->p_wchan == 0)
2044 				setrunnable(q);
2045 			else
2046 				q->p_stat = SSLEEP;
2047 		}
2048 		SCHED_UNLOCK(s);
2049 	}
2050 }
2051