xref: /openbsd-src/sys/kern/kern_sig.c (revision 879edb36e9a953e453e7b19ac6c72ad3ba21655e)
1 /*	$OpenBSD: kern_sig.c,v 1.216 2018/02/26 13:33:25 mpi 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 	p->p_sigstk = ss;
558 	return (0);
559 }
560 
561 int
562 sys_kill(struct proc *cp, void *v, register_t *retval)
563 {
564 	struct sys_kill_args /* {
565 		syscallarg(int) pid;
566 		syscallarg(int) signum;
567 	} */ *uap = v;
568 	struct process *pr;
569 	int pid = SCARG(uap, pid);
570 	int signum = SCARG(uap, signum);
571 	int error;
572 	int zombie = 0;
573 
574 	if ((error = pledge_kill(cp, pid)) != 0)
575 		return (error);
576 	if (((u_int)signum) >= NSIG)
577 		return (EINVAL);
578 	if (pid > 0) {
579 		if ((pr = prfind(pid)) == NULL) {
580 			if ((pr = zombiefind(pid)) == NULL)
581 				return (ESRCH);
582 			else
583 				zombie = 1;
584 		}
585 		if (!cansignal(cp, pr, signum))
586 			return (EPERM);
587 
588 		/* kill single process */
589 		if (signum && !zombie)
590 			prsignal(pr, signum);
591 		return (0);
592 	}
593 	switch (pid) {
594 	case -1:		/* broadcast signal */
595 		return (killpg1(cp, signum, 0, 1));
596 	case 0:			/* signal own process group */
597 		return (killpg1(cp, signum, 0, 0));
598 	default:		/* negative explicit process group */
599 		return (killpg1(cp, signum, -pid, 0));
600 	}
601 }
602 
603 int
604 sys_thrkill(struct proc *cp, void *v, register_t *retval)
605 {
606 	struct sys_thrkill_args /* {
607 		syscallarg(pid_t) tid;
608 		syscallarg(int) signum;
609 		syscallarg(void *) tcb;
610 	} */ *uap = v;
611 	struct proc *p;
612 	int tid = SCARG(uap, tid);
613 	int signum = SCARG(uap, signum);
614 	void *tcb;
615 
616 	if (((u_int)signum) >= NSIG)
617 		return (EINVAL);
618 	if (tid > THREAD_PID_OFFSET) {
619 		if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL)
620 			return (ESRCH);
621 
622 		/* can only kill threads in the same process */
623 		if (p->p_p != cp->p_p)
624 			return (ESRCH);
625 	} else if (tid == 0)
626 		p = cp;
627 	else
628 		return (EINVAL);
629 
630 	/* optionally require the target thread to have the given tcb addr */
631 	tcb = SCARG(uap, tcb);
632 	if (tcb != NULL && tcb != TCB_GET(p))
633 		return (ESRCH);
634 
635 	if (signum)
636 		ptsignal(p, signum, STHREAD);
637 	return (0);
638 }
639 
640 /*
641  * Common code for kill process group/broadcast kill.
642  * cp is calling process.
643  */
644 int
645 killpg1(struct proc *cp, int signum, int pgid, int all)
646 {
647 	struct process *pr;
648 	struct pgrp *pgrp;
649 	int nfound = 0;
650 
651 	if (all) {
652 		/*
653 		 * broadcast
654 		 */
655 		LIST_FOREACH(pr, &allprocess, ps_list) {
656 			if (pr->ps_pid <= 1 ||
657 			    pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) ||
658 			    pr == cp->p_p || !cansignal(cp, pr, signum))
659 				continue;
660 			nfound++;
661 			if (signum)
662 				prsignal(pr, signum);
663 		}
664 	} else {
665 		if (pgid == 0)
666 			/*
667 			 * zero pgid means send to my process group.
668 			 */
669 			pgrp = cp->p_p->ps_pgrp;
670 		else {
671 			pgrp = pgfind(pgid);
672 			if (pgrp == NULL)
673 				return (ESRCH);
674 		}
675 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
676 			if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM ||
677 			    !cansignal(cp, pr, signum))
678 				continue;
679 			nfound++;
680 			if (signum)
681 				prsignal(pr, signum);
682 		}
683 	}
684 	return (nfound ? 0 : ESRCH);
685 }
686 
687 #define CANDELIVER(uid, euid, pr) \
688 	(euid == 0 || \
689 	(uid) == (pr)->ps_ucred->cr_ruid || \
690 	(uid) == (pr)->ps_ucred->cr_svuid || \
691 	(uid) == (pr)->ps_ucred->cr_uid || \
692 	(euid) == (pr)->ps_ucred->cr_ruid || \
693 	(euid) == (pr)->ps_ucred->cr_svuid || \
694 	(euid) == (pr)->ps_ucred->cr_uid)
695 
696 /*
697  * Deliver signum to pgid, but first check uid/euid against each
698  * process and see if it is permitted.
699  */
700 void
701 csignal(pid_t pgid, int signum, uid_t uid, uid_t euid)
702 {
703 	struct pgrp *pgrp;
704 	struct process *pr;
705 
706 	if (pgid == 0)
707 		return;
708 	if (pgid < 0) {
709 		pgid = -pgid;
710 		if ((pgrp = pgfind(pgid)) == NULL)
711 			return;
712 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
713 			if (CANDELIVER(uid, euid, pr))
714 				prsignal(pr, signum);
715 	} else {
716 		if ((pr = prfind(pgid)) == NULL)
717 			return;
718 		if (CANDELIVER(uid, euid, pr))
719 			prsignal(pr, signum);
720 	}
721 }
722 
723 /*
724  * Send a signal to a process group.
725  */
726 void
727 gsignal(int pgid, int signum)
728 {
729 	struct pgrp *pgrp;
730 
731 	if (pgid && (pgrp = pgfind(pgid)))
732 		pgsignal(pgrp, signum, 0);
733 }
734 
735 /*
736  * Send a signal to a process group.  If checktty is 1,
737  * limit to members which have a controlling terminal.
738  */
739 void
740 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
741 {
742 	struct process *pr;
743 
744 	if (pgrp)
745 		LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
746 			if (checkctty == 0 || pr->ps_flags & PS_CONTROLT)
747 				prsignal(pr, signum);
748 }
749 
750 /*
751  * Recalculate the signal mask and reset the signal disposition after
752  * usermode frame for delivery is formed.
753  */
754 void
755 postsig_done(struct proc *p, int signum, struct sigacts *ps)
756 {
757 	int mask = sigmask(signum);
758 
759 	KERNEL_ASSERT_LOCKED();
760 
761 	p->p_ru.ru_nsignals++;
762 	atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]);
763 	if ((ps->ps_sigreset & mask) != 0) {
764 		ps->ps_sigcatch &= ~mask;
765 		if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
766 			ps->ps_sigignore |= mask;
767 		ps->ps_sigact[signum] = SIG_DFL;
768 	}
769 }
770 
771 /*
772  * Send a signal caused by a trap to the current thread
773  * If it will be caught immediately, deliver it with correct code.
774  * Otherwise, post it normally.
775  */
776 void
777 trapsignal(struct proc *p, int signum, u_long trapno, int code,
778     union sigval sigval)
779 {
780 	struct process *pr = p->p_p;
781 	struct sigacts *ps = pr->ps_sigacts;
782 	int mask;
783 
784 	switch (signum) {
785 	case SIGILL:
786 	case SIGBUS:
787 	case SIGSEGV:
788 		pr->ps_acflag |= ATRAP;
789 		break;
790 	}
791 
792 	mask = sigmask(signum);
793 	if ((pr->ps_flags & PS_TRACED) == 0 &&
794 	    (ps->ps_sigcatch & mask) != 0 &&
795 	    (p->p_sigmask & mask) == 0) {
796 #ifdef KTRACE
797 		if (KTRPOINT(p, KTR_PSIG)) {
798 			siginfo_t si;
799 
800 			initsiginfo(&si, signum, trapno, code, sigval);
801 			ktrpsig(p, signum, ps->ps_sigact[signum],
802 			    p->p_sigmask, code, &si);
803 		}
804 #endif
805 		(*pr->ps_emul->e_sendsig)(ps->ps_sigact[signum], signum,
806 		    p->p_sigmask, trapno, code, sigval);
807 		postsig_done(p, signum, ps);
808 	} else {
809 		p->p_sisig = signum;
810 		p->p_sitrapno = trapno;	/* XXX for core dump/debugger */
811 		p->p_sicode = code;
812 		p->p_sigval = sigval;
813 
814 		/*
815 		 * Signals like SIGBUS and SIGSEGV should not, when
816 		 * generated by the kernel, be ignorable or blockable.
817 		 * If it is and we're not being traced, then just kill
818 		 * the process.
819 		 */
820 		if ((pr->ps_flags & PS_TRACED) == 0 &&
821 		    (sigprop[signum] & SA_KILL) &&
822 		    ((p->p_sigmask & mask) || (ps->ps_sigignore & mask)))
823 			sigexit(p, signum);
824 		ptsignal(p, signum, STHREAD);
825 	}
826 }
827 
828 /*
829  * Send the signal to the process.  If the signal has an action, the action
830  * is usually performed by the target process rather than the caller; we add
831  * the signal to the set of pending signals for the process.
832  *
833  * Exceptions:
834  *   o When a stop signal is sent to a sleeping process that takes the
835  *     default action, the process is stopped without awakening it.
836  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
837  *     regardless of the signal action (eg, blocked or ignored).
838  *
839  * Other ignored signals are discarded immediately.
840  */
841 void
842 psignal(struct proc *p, int signum)
843 {
844 	ptsignal(p, signum, SPROCESS);
845 }
846 
847 /*
848  * type = SPROCESS	process signal, can be diverted (sigwait())
849  *	XXX if blocked in all threads, mark as pending in struct process
850  * type = STHREAD	thread signal, but should be propagated if unhandled
851  * type = SPROPAGATED	propagated to this thread, so don't propagate again
852  */
853 void
854 ptsignal(struct proc *p, int signum, enum signal_type type)
855 {
856 	int s, prop;
857 	sig_t action;
858 	int mask;
859 	struct process *pr = p->p_p;
860 	struct proc *q;
861 	int wakeparent = 0;
862 
863 #ifdef DIAGNOSTIC
864 	if ((u_int)signum >= NSIG || signum == 0)
865 		panic("psignal signal number");
866 #endif
867 
868 	/* Ignore signal if the target process is exiting */
869 	if (pr->ps_flags & PS_EXITING)
870 		return;
871 
872 	mask = sigmask(signum);
873 
874 	if (type == SPROCESS) {
875 		/* Accept SIGKILL to coredumping processes */
876 		if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) {
877 			if (pr->ps_single != NULL)
878 				p = pr->ps_single;
879 			atomic_setbits_int(&p->p_siglist, mask);
880 			return;
881 		}
882 
883 		/*
884 		 * If the current thread can process the signal
885 		 * immediately (it's unblocked) then have it take it.
886 		 */
887 		q = curproc;
888 		if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 &&
889 		    (q->p_sigmask & mask) == 0)
890 			p = q;
891 		else {
892 			/*
893 			 * A process-wide signal can be diverted to a
894 			 * different thread that's in sigwait() for this
895 			 * signal.  If there isn't such a thread, then
896 			 * pick a thread that doesn't have it blocked so
897 			 * that the stop/kill consideration isn't
898 			 * delayed.  Otherwise, mark it pending on the
899 			 * main thread.
900 			 */
901 			TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
902 				/* ignore exiting threads */
903 				if (q->p_flag & P_WEXIT)
904 					continue;
905 
906 				/* skip threads that have the signal blocked */
907 				if ((q->p_sigmask & mask) != 0)
908 					continue;
909 
910 				/* okay, could send to this thread */
911 				p = q;
912 
913 				/*
914 				 * sigsuspend, sigwait, ppoll/pselect, etc?
915 				 * Definitely go to this thread, as it's
916 				 * already blocked in the kernel.
917 				 */
918 				if (q->p_flag & P_SIGSUSPEND)
919 					break;
920 			}
921 		}
922 	}
923 
924 	if (type != SPROPAGATED)
925 		KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum);
926 
927 	prop = sigprop[signum];
928 
929 	/*
930 	 * If proc is traced, always give parent a chance.
931 	 */
932 	if (pr->ps_flags & PS_TRACED) {
933 		action = SIG_DFL;
934 		atomic_setbits_int(&p->p_siglist, mask);
935 	} else {
936 		/*
937 		 * If the signal is being ignored,
938 		 * then we forget about it immediately.
939 		 * (Note: we don't set SIGCONT in ps_sigignore,
940 		 * and if it is set to SIG_IGN,
941 		 * action will be SIG_DFL here.)
942 		 */
943 		if (pr->ps_sigacts->ps_sigignore & mask)
944 			return;
945 		if (p->p_sigmask & mask) {
946 			action = SIG_HOLD;
947 		} else if (pr->ps_sigacts->ps_sigcatch & mask) {
948 			action = SIG_CATCH;
949 		} else {
950 			action = SIG_DFL;
951 
952 			if (prop & SA_KILL && pr->ps_nice > NZERO)
953 				 pr->ps_nice = NZERO;
954 
955 			/*
956 			 * If sending a tty stop signal to a member of an
957 			 * orphaned process group, discard the signal here if
958 			 * the action is default; don't stop the process below
959 			 * if sleeping, and don't clear any pending SIGCONT.
960 			 */
961 			if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0)
962 				return;
963 		}
964 
965 		atomic_setbits_int(&p->p_siglist, mask);
966 	}
967 
968 	if (prop & SA_CONT)
969 		atomic_clearbits_int(&p->p_siglist, stopsigmask);
970 
971 	if (prop & SA_STOP) {
972 		atomic_clearbits_int(&p->p_siglist, contsigmask);
973 		atomic_clearbits_int(&p->p_flag, P_CONTINUED);
974 	}
975 
976 	/*
977 	 * XXX delay processing of SA_STOP signals unless action == SIG_DFL?
978 	 */
979 	if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED)
980 		TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link)
981 			if (q != p)
982 				ptsignal(q, signum, SPROPAGATED);
983 
984 	/*
985 	 * Defer further processing for signals which are held,
986 	 * except that stopped processes must be continued by SIGCONT.
987 	 */
988 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
989 		return;
990 
991 	SCHED_LOCK(s);
992 
993 	switch (p->p_stat) {
994 
995 	case SSLEEP:
996 		/*
997 		 * If process is sleeping uninterruptibly
998 		 * we can't interrupt the sleep... the signal will
999 		 * be noticed when the process returns through
1000 		 * trap() or syscall().
1001 		 */
1002 		if ((p->p_flag & P_SINTR) == 0)
1003 			goto out;
1004 		/*
1005 		 * Process is sleeping and traced... make it runnable
1006 		 * so it can discover the signal in issignal() and stop
1007 		 * for the parent.
1008 		 */
1009 		if (pr->ps_flags & PS_TRACED)
1010 			goto run;
1011 		/*
1012 		 * If SIGCONT is default (or ignored) and process is
1013 		 * asleep, we are finished; the process should not
1014 		 * be awakened.
1015 		 */
1016 		if ((prop & SA_CONT) && action == SIG_DFL) {
1017 			atomic_clearbits_int(&p->p_siglist, mask);
1018 			goto out;
1019 		}
1020 		/*
1021 		 * When a sleeping process receives a stop
1022 		 * signal, process immediately if possible.
1023 		 */
1024 		if ((prop & SA_STOP) && action == SIG_DFL) {
1025 			/*
1026 			 * If a child holding parent blocked,
1027 			 * stopping could cause deadlock.
1028 			 */
1029 			if (pr->ps_flags & PS_PPWAIT)
1030 				goto out;
1031 			atomic_clearbits_int(&p->p_siglist, mask);
1032 			p->p_xstat = signum;
1033 			proc_stop(p, 0);
1034 			goto out;
1035 		}
1036 		/*
1037 		 * All other (caught or default) signals
1038 		 * cause the process to run.
1039 		 */
1040 		goto runfast;
1041 		/*NOTREACHED*/
1042 
1043 	case SSTOP:
1044 		/*
1045 		 * If traced process is already stopped,
1046 		 * then no further action is necessary.
1047 		 */
1048 		if (pr->ps_flags & PS_TRACED)
1049 			goto out;
1050 
1051 		/*
1052 		 * Kill signal always sets processes running.
1053 		 */
1054 		if (signum == SIGKILL) {
1055 			atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1056 			goto runfast;
1057 		}
1058 
1059 		if (prop & SA_CONT) {
1060 			/*
1061 			 * If SIGCONT is default (or ignored), we continue the
1062 			 * process but don't leave the signal in p_siglist, as
1063 			 * it has no further action.  If SIGCONT is held, we
1064 			 * continue the process and leave the signal in
1065 			 * p_siglist.  If the process catches SIGCONT, let it
1066 			 * handle the signal itself.  If it isn't waiting on
1067 			 * an event, then it goes back to run state.
1068 			 * Otherwise, process goes back to sleep state.
1069 			 */
1070 			atomic_setbits_int(&p->p_flag, P_CONTINUED);
1071 			atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1072 			wakeparent = 1;
1073 			if (action == SIG_DFL)
1074 				atomic_clearbits_int(&p->p_siglist, mask);
1075 			if (action == SIG_CATCH)
1076 				goto runfast;
1077 			if (p->p_wchan == 0)
1078 				goto run;
1079 			p->p_stat = SSLEEP;
1080 			goto out;
1081 		}
1082 
1083 		if (prop & SA_STOP) {
1084 			/*
1085 			 * Already stopped, don't need to stop again.
1086 			 * (If we did the shell could get confused.)
1087 			 */
1088 			atomic_clearbits_int(&p->p_siglist, mask);
1089 			goto out;
1090 		}
1091 
1092 		/*
1093 		 * If process is sleeping interruptibly, then simulate a
1094 		 * wakeup so that when it is continued, it will be made
1095 		 * runnable and can look at the signal.  But don't make
1096 		 * the process runnable, leave it stopped.
1097 		 */
1098 		if (p->p_wchan && p->p_flag & P_SINTR)
1099 			unsleep(p);
1100 		goto out;
1101 
1102 	case SONPROC:
1103 		signotify(p);
1104 		/* FALLTHROUGH */
1105 	default:
1106 		/*
1107 		 * SRUN, SIDL, SDEAD do nothing with the signal,
1108 		 * other than kicking ourselves if we are running.
1109 		 * It will either never be noticed, or noticed very soon.
1110 		 */
1111 		goto out;
1112 	}
1113 	/*NOTREACHED*/
1114 
1115 runfast:
1116 	/*
1117 	 * Raise priority to at least PUSER.
1118 	 */
1119 	if (p->p_priority > PUSER)
1120 		p->p_priority = PUSER;
1121 run:
1122 	setrunnable(p);
1123 out:
1124 	SCHED_UNLOCK(s);
1125 	if (wakeparent)
1126 		wakeup(pr->ps_pptr);
1127 }
1128 
1129 /*
1130  * If the current process has received a signal (should be caught or cause
1131  * termination, should interrupt current syscall), return the signal number.
1132  * Stop signals with default action are processed immediately, then cleared;
1133  * they aren't returned.  This is checked after each entry to the system for
1134  * a syscall or trap (though this can usually be done without calling issignal
1135  * by checking the pending signal masks in the CURSIG macro.) The normal call
1136  * sequence is
1137  *
1138  *	while (signum = CURSIG(curproc))
1139  *		postsig(signum);
1140  *
1141  * Assumes that if the P_SINTR flag is set, we're holding both the
1142  * kernel and scheduler locks.
1143  */
1144 int
1145 issignal(struct proc *p)
1146 {
1147 	struct process *pr = p->p_p;
1148 	int signum, mask, prop;
1149 	int dolock = (p->p_flag & P_SINTR) == 0;
1150 	int s;
1151 
1152 	for (;;) {
1153 		mask = p->p_siglist & ~p->p_sigmask;
1154 		if (pr->ps_flags & PS_PPWAIT)
1155 			mask &= ~stopsigmask;
1156 		if (mask == 0)	 	/* no signal to send */
1157 			return (0);
1158 		signum = ffs((long)mask);
1159 		mask = sigmask(signum);
1160 		atomic_clearbits_int(&p->p_siglist, mask);
1161 
1162 		/*
1163 		 * We should see pending but ignored signals
1164 		 * only if PS_TRACED was on when they were posted.
1165 		 */
1166 		if (mask & pr->ps_sigacts->ps_sigignore &&
1167 		    (pr->ps_flags & PS_TRACED) == 0)
1168 			continue;
1169 
1170 		if ((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) {
1171 			/*
1172 			 * If traced, always stop, and stay
1173 			 * stopped until released by the debugger.
1174 			 */
1175 			p->p_xstat = signum;
1176 
1177 			if (dolock)
1178 				KERNEL_LOCK();
1179 			single_thread_set(p, SINGLE_PTRACE, 0);
1180 			if (dolock)
1181 				KERNEL_UNLOCK();
1182 
1183 			if (dolock)
1184 				SCHED_LOCK(s);
1185 			proc_stop(p, 1);
1186 			if (dolock)
1187 				SCHED_UNLOCK(s);
1188 
1189 			if (dolock)
1190 				KERNEL_LOCK();
1191 			single_thread_clear(p, 0);
1192 			if (dolock)
1193 				KERNEL_UNLOCK();
1194 
1195 			/*
1196 			 * If we are no longer being traced, or the parent
1197 			 * didn't give us a signal, look for more signals.
1198 			 */
1199 			if ((pr->ps_flags & PS_TRACED) == 0 || p->p_xstat == 0)
1200 				continue;
1201 
1202 			/*
1203 			 * If the new signal is being masked, look for other
1204 			 * signals.
1205 			 */
1206 			signum = p->p_xstat;
1207 			mask = sigmask(signum);
1208 			if ((p->p_sigmask & mask) != 0)
1209 				continue;
1210 
1211 			/* take the signal! */
1212 			atomic_clearbits_int(&p->p_siglist, mask);
1213 		}
1214 
1215 		prop = sigprop[signum];
1216 
1217 		/*
1218 		 * Decide whether the signal should be returned.
1219 		 * Return the signal's number, or fall through
1220 		 * to clear it from the pending mask.
1221 		 */
1222 		switch ((long)pr->ps_sigacts->ps_sigact[signum]) {
1223 		case (long)SIG_DFL:
1224 			/*
1225 			 * Don't take default actions on system processes.
1226 			 */
1227 			if (pr->ps_pid <= 1) {
1228 #ifdef DIAGNOSTIC
1229 				/*
1230 				 * Are you sure you want to ignore SIGSEGV
1231 				 * in init? XXX
1232 				 */
1233 				printf("Process (pid %d) got signal"
1234 				    " %d\n", pr->ps_pid, signum);
1235 #endif
1236 				break;		/* == ignore */
1237 			}
1238 			/*
1239 			 * If there is a pending stop signal to process
1240 			 * with default action, stop here,
1241 			 * then clear the signal.  However,
1242 			 * if process is member of an orphaned
1243 			 * process group, ignore tty stop signals.
1244 			 */
1245 			if (prop & SA_STOP) {
1246 				if (pr->ps_flags & PS_TRACED ||
1247 		    		    (pr->ps_pgrp->pg_jobc == 0 &&
1248 				    prop & SA_TTYSTOP))
1249 					break;	/* == ignore */
1250 				p->p_xstat = signum;
1251 				if (dolock)
1252 					SCHED_LOCK(s);
1253 				proc_stop(p, 1);
1254 				if (dolock)
1255 					SCHED_UNLOCK(s);
1256 				break;
1257 			} else if (prop & SA_IGNORE) {
1258 				/*
1259 				 * Except for SIGCONT, shouldn't get here.
1260 				 * Default action is to ignore; drop it.
1261 				 */
1262 				break;		/* == ignore */
1263 			} else
1264 				goto keep;
1265 			/*NOTREACHED*/
1266 		case (long)SIG_IGN:
1267 			/*
1268 			 * Masking above should prevent us ever trying
1269 			 * to take action on an ignored signal other
1270 			 * than SIGCONT, unless process is traced.
1271 			 */
1272 			if ((prop & SA_CONT) == 0 &&
1273 			    (pr->ps_flags & PS_TRACED) == 0)
1274 				printf("issignal\n");
1275 			break;		/* == ignore */
1276 		default:
1277 			/*
1278 			 * This signal has an action, let
1279 			 * postsig() process it.
1280 			 */
1281 			goto keep;
1282 		}
1283 	}
1284 	/* NOTREACHED */
1285 
1286 keep:
1287 	atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */
1288 	return (signum);
1289 }
1290 
1291 /*
1292  * Put the argument process into the stopped state and notify the parent
1293  * via wakeup.  Signals are handled elsewhere.  The process must not be
1294  * on the run queue.
1295  */
1296 void
1297 proc_stop(struct proc *p, int sw)
1298 {
1299 	struct process *pr = p->p_p;
1300 	extern void *softclock_si;
1301 
1302 #ifdef MULTIPROCESSOR
1303 	SCHED_ASSERT_LOCKED();
1304 #endif
1305 
1306 	p->p_stat = SSTOP;
1307 	atomic_clearbits_int(&pr->ps_flags, PS_WAITED);
1308 	atomic_setbits_int(&pr->ps_flags, PS_STOPPED);
1309 	atomic_setbits_int(&p->p_flag, P_SUSPSIG);
1310 	if (!timeout_pending(&proc_stop_to)) {
1311 		timeout_add(&proc_stop_to, 0);
1312 		/*
1313 		 * We need this soft interrupt to be handled fast.
1314 		 * Extra calls to softclock don't hurt.
1315 		 */
1316                 softintr_schedule(softclock_si);
1317 	}
1318 	if (sw)
1319 		mi_switch();
1320 }
1321 
1322 /*
1323  * Called from a timeout to send signals to the parents of stopped processes.
1324  * We can't do this in proc_stop because it's called with nasty locks held
1325  * and we would need recursive scheduler lock to deal with that.
1326  */
1327 void
1328 proc_stop_sweep(void *v)
1329 {
1330 	struct process *pr;
1331 
1332 	LIST_FOREACH(pr, &allprocess, ps_list) {
1333 		if ((pr->ps_flags & PS_STOPPED) == 0)
1334 			continue;
1335 		atomic_clearbits_int(&pr->ps_flags, PS_STOPPED);
1336 
1337 		if ((pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDSTOP) == 0)
1338 			prsignal(pr->ps_pptr, SIGCHLD);
1339 		wakeup(pr->ps_pptr);
1340 	}
1341 }
1342 
1343 /*
1344  * Take the action for the specified signal
1345  * from the current set of pending signals.
1346  */
1347 void
1348 postsig(struct proc *p, int signum)
1349 {
1350 	struct process *pr = p->p_p;
1351 	struct sigacts *ps = pr->ps_sigacts;
1352 	sig_t action;
1353 	u_long trapno;
1354 	int mask, returnmask;
1355 	union sigval sigval;
1356 	int s, code;
1357 
1358 	KASSERT(signum != 0);
1359 	KERNEL_ASSERT_LOCKED();
1360 
1361 	mask = sigmask(signum);
1362 	atomic_clearbits_int(&p->p_siglist, mask);
1363 	action = ps->ps_sigact[signum];
1364 	sigval.sival_ptr = 0;
1365 
1366 	if (p->p_sisig != signum) {
1367 		trapno = 0;
1368 		code = SI_USER;
1369 		sigval.sival_ptr = 0;
1370 	} else {
1371 		trapno = p->p_sitrapno;
1372 		code = p->p_sicode;
1373 		sigval = p->p_sigval;
1374 	}
1375 
1376 #ifdef KTRACE
1377 	if (KTRPOINT(p, KTR_PSIG)) {
1378 		siginfo_t si;
1379 
1380 		initsiginfo(&si, signum, trapno, code, sigval);
1381 		ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ?
1382 		    p->p_oldmask : p->p_sigmask, code, &si);
1383 	}
1384 #endif
1385 	if (action == SIG_DFL) {
1386 		/*
1387 		 * Default action, where the default is to kill
1388 		 * the process.  (Other cases were ignored above.)
1389 		 */
1390 		sigexit(p, signum);
1391 		/* NOTREACHED */
1392 	} else {
1393 		/*
1394 		 * If we get here, the signal must be caught.
1395 		 */
1396 #ifdef DIAGNOSTIC
1397 		if (action == SIG_IGN || (p->p_sigmask & mask))
1398 			panic("postsig action");
1399 #endif
1400 		/*
1401 		 * Set the new mask value and also defer further
1402 		 * occurrences of this signal.
1403 		 *
1404 		 * Special case: user has done a sigpause.  Here the
1405 		 * current mask is not of interest, but rather the
1406 		 * mask from before the sigpause is what we want
1407 		 * restored after the signal processing is completed.
1408 		 */
1409 #ifdef MULTIPROCESSOR
1410 		s = splsched();
1411 #else
1412 		s = splhigh();
1413 #endif
1414 		if (p->p_flag & P_SIGSUSPEND) {
1415 			atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1416 			returnmask = p->p_oldmask;
1417 		} else {
1418 			returnmask = p->p_sigmask;
1419 		}
1420 		if (p->p_sisig == signum) {
1421 			p->p_sisig = 0;
1422 			p->p_sitrapno = 0;
1423 			p->p_sicode = SI_USER;
1424 			p->p_sigval.sival_ptr = NULL;
1425 		}
1426 
1427 		(*pr->ps_emul->e_sendsig)(action, signum, returnmask, trapno,
1428 		    code, sigval);
1429 		postsig_done(p, signum, ps);
1430 		splx(s);
1431 	}
1432 }
1433 
1434 /*
1435  * Force the current process to exit with the specified signal, dumping core
1436  * if appropriate.  We bypass the normal tests for masked and caught signals,
1437  * allowing unrecoverable failures to terminate the process without changing
1438  * signal state.  Mark the accounting record with the signal termination.
1439  * If dumping core, save the signal number for the debugger.  Calls exit and
1440  * does not return.
1441  */
1442 void
1443 sigexit(struct proc *p, int signum)
1444 {
1445 	/* Mark process as going away */
1446 	atomic_setbits_int(&p->p_flag, P_WEXIT);
1447 
1448 	p->p_p->ps_acflag |= AXSIG;
1449 	if (sigprop[signum] & SA_CORE) {
1450 		p->p_sisig = signum;
1451 
1452 		/* if there are other threads, pause them */
1453 		if (P_HASSIBLING(p))
1454 			single_thread_set(p, SINGLE_SUSPEND, 0);
1455 
1456 		if (coredump(p) == 0)
1457 			signum |= WCOREFLAG;
1458 	}
1459 	exit1(p, W_EXITCODE(0, signum), EXIT_NORMAL);
1460 	/* NOTREACHED */
1461 }
1462 
1463 int nosuidcoredump = 1;
1464 
1465 struct coredump_iostate {
1466 	struct proc *io_proc;
1467 	struct vnode *io_vp;
1468 	struct ucred *io_cred;
1469 	off_t io_offset;
1470 };
1471 
1472 /*
1473  * Dump core, into a file named "progname.core", unless the process was
1474  * setuid/setgid.
1475  */
1476 int
1477 coredump(struct proc *p)
1478 {
1479 #ifdef SMALL_KERNEL
1480 	return EPERM;
1481 #else
1482 	struct process *pr = p->p_p;
1483 	struct vnode *vp;
1484 	struct ucred *cred = p->p_ucred;
1485 	struct vmspace *vm = p->p_vmspace;
1486 	struct nameidata nd;
1487 	struct vattr vattr;
1488 	struct coredump_iostate	io;
1489 	int error, len, incrash = 0;
1490 	char name[MAXPATHLEN];
1491 	const char *dir = "/var/crash";
1492 
1493 	if (pr->ps_emul->e_coredump == NULL)
1494 		return (EINVAL);
1495 
1496 	pr->ps_flags |= PS_COREDUMP;
1497 
1498 	/*
1499 	 * If the process has inconsistent uids, nosuidcoredump
1500 	 * determines coredump placement policy.
1501 	 */
1502 	if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) ||
1503 	   ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) {
1504 		if (nosuidcoredump == 3 || nosuidcoredump == 2)
1505 			incrash = 1;
1506 		else
1507 			return (EPERM);
1508 	}
1509 
1510 	/* Don't dump if will exceed file size limit. */
1511 	if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >=
1512 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1513 		return (EFBIG);
1514 
1515 	if (incrash && nosuidcoredump == 3) {
1516 		/*
1517 		 * If the program directory does not exist, dumps of
1518 		 * that core will silently fail.
1519 		 */
1520 		len = snprintf(name, sizeof(name), "%s/%s/%u.core",
1521 		    dir, pr->ps_comm, pr->ps_pid);
1522 	} else if (incrash && nosuidcoredump == 2)
1523 		len = snprintf(name, sizeof(name), "%s/%s.core",
1524 		    dir, pr->ps_comm);
1525 	else
1526 		len = snprintf(name, sizeof(name), "%s.core", pr->ps_comm);
1527 	if (len >= sizeof(name))
1528 		return (EACCES);
1529 
1530 	/*
1531 	 * Control the UID used to write out.  The normal case uses
1532 	 * the real UID.  If the sugid case is going to write into the
1533 	 * controlled directory, we do so as root.
1534 	 */
1535 	if (incrash == 0) {
1536 		cred = crdup(cred);
1537 		cred->cr_uid = cred->cr_ruid;
1538 		cred->cr_gid = cred->cr_rgid;
1539 	} else {
1540 		if (p->p_fd->fd_rdir) {
1541 			vrele(p->p_fd->fd_rdir);
1542 			p->p_fd->fd_rdir = NULL;
1543 		}
1544 		p->p_ucred = crdup(p->p_ucred);
1545 		crfree(cred);
1546 		cred = p->p_ucred;
1547 		crhold(cred);
1548 		cred->cr_uid = 0;
1549 		cred->cr_gid = 0;
1550 	}
1551 
1552 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1553 
1554 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1555 
1556 	if (error)
1557 		goto out;
1558 
1559 	/*
1560 	 * Don't dump to non-regular files, files with links, or files
1561 	 * owned by someone else.
1562 	 */
1563 	vp = nd.ni_vp;
1564 	if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) {
1565 		VOP_UNLOCK(vp, p);
1566 		vn_close(vp, FWRITE, cred, p);
1567 		goto out;
1568 	}
1569 	if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1570 	    vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) ||
1571 	    vattr.va_uid != cred->cr_uid) {
1572 		error = EACCES;
1573 		VOP_UNLOCK(vp, p);
1574 		vn_close(vp, FWRITE, cred, p);
1575 		goto out;
1576 	}
1577 	VATTR_NULL(&vattr);
1578 	vattr.va_size = 0;
1579 	VOP_SETATTR(vp, &vattr, cred, p);
1580 	pr->ps_acflag |= ACORE;
1581 
1582 	io.io_proc = p;
1583 	io.io_vp = vp;
1584 	io.io_cred = cred;
1585 	io.io_offset = 0;
1586 	VOP_UNLOCK(vp, p);
1587 	vref(vp);
1588 	error = vn_close(vp, FWRITE, cred, p);
1589 	if (error == 0)
1590 		error = (*pr->ps_emul->e_coredump)(p, &io);
1591 	vrele(vp);
1592 out:
1593 	crfree(cred);
1594 	return (error);
1595 #endif
1596 }
1597 
1598 #ifndef SMALL_KERNEL
1599 int
1600 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
1601 {
1602 	struct coredump_iostate *io = cookie;
1603 	off_t coffset = 0;
1604 	size_t csize;
1605 	int chunk, error;
1606 
1607 	csize = len;
1608 	do {
1609 		if (io->io_proc->p_siglist & sigmask(SIGKILL))
1610 			return (EINTR);
1611 
1612 		/* Rest of the loop sleeps with lock held, so... */
1613 		yield();
1614 
1615 		chunk = MIN(csize, MAXPHYS);
1616 		error = vn_rdwr(UIO_WRITE, io->io_vp,
1617 		    (caddr_t)data + coffset, chunk,
1618 		    io->io_offset + coffset, segflg,
1619 		    IO_UNIT, io->io_cred, NULL, io->io_proc);
1620 		if (error) {
1621 			struct process *pr = io->io_proc->p_p;
1622 			if (error == ENOSPC)
1623 				log(LOG_ERR, "coredump of %s(%d) failed, filesystem full\n",
1624 				    pr->ps_comm, pr->ps_pid);
1625 			else
1626 				log(LOG_ERR, "coredump of %s(%d), write failed: errno %d\n",
1627 				    pr->ps_comm, pr->ps_pid, error);
1628 			return (error);
1629 		}
1630 
1631 		coffset += chunk;
1632 		csize -= chunk;
1633 	} while (csize > 0);
1634 
1635 	io->io_offset += len;
1636 	return (0);
1637 }
1638 
1639 void
1640 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end)
1641 {
1642 	struct coredump_iostate *io = cookie;
1643 
1644 	uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end);
1645 }
1646 
1647 #endif	/* !SMALL_KERNEL */
1648 
1649 /*
1650  * Nonexistent system call-- signal process (may want to handle it).
1651  * Flag error in case process won't see signal immediately (blocked or ignored).
1652  */
1653 int
1654 sys_nosys(struct proc *p, void *v, register_t *retval)
1655 {
1656 
1657 	ptsignal(p, SIGSYS, STHREAD);
1658 	return (ENOSYS);
1659 }
1660 
1661 int
1662 sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
1663 {
1664 	static int sigwaitsleep;
1665 	struct sys___thrsigdivert_args /* {
1666 		syscallarg(sigset_t) sigmask;
1667 		syscallarg(siginfo_t *) info;
1668 		syscallarg(const struct timespec *) timeout;
1669 	} */ *uap = v;
1670 	struct process *pr = p->p_p;
1671 	sigset_t *m;
1672 	sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask;
1673 	siginfo_t si;
1674 	uint64_t to_ticks = 0;
1675 	int timeinvalid = 0;
1676 	int error = 0;
1677 
1678 	memset(&si, 0, sizeof(si));
1679 
1680 	if (SCARG(uap, timeout) != NULL) {
1681 		struct timespec ts;
1682 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1683 			return (error);
1684 #ifdef KTRACE
1685 		if (KTRPOINT(p, KTR_STRUCT))
1686 			ktrreltimespec(p, &ts);
1687 #endif
1688 		if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1689 			timeinvalid = 1;
1690 		else {
1691 			to_ticks = (uint64_t)hz * ts.tv_sec +
1692 			    ts.tv_nsec / (tick * 1000);
1693 			if (to_ticks > INT_MAX)
1694 				to_ticks = INT_MAX;
1695 			if (to_ticks == 0 && ts.tv_nsec)
1696 				to_ticks = 1;
1697 		}
1698 	}
1699 
1700 	dosigsuspend(p, p->p_sigmask &~ mask);
1701 	for (;;) {
1702 		si.si_signo = CURSIG(p);
1703 		if (si.si_signo != 0) {
1704 			sigset_t smask = sigmask(si.si_signo);
1705 			if (smask & mask) {
1706 				if (p->p_siglist & smask)
1707 					m = &p->p_siglist;
1708 				else if (pr->ps_mainproc->p_siglist & smask)
1709 					m = &pr->ps_mainproc->p_siglist;
1710 				else {
1711 					/* signal got eaten by someone else? */
1712 					continue;
1713 				}
1714 				atomic_clearbits_int(m, smask);
1715 				error = 0;
1716 				break;
1717 			}
1718 		}
1719 
1720 		/* per-POSIX, delay this error until after the above */
1721 		if (timeinvalid)
1722 			error = EINVAL;
1723 
1724 		if (SCARG(uap, timeout) != NULL && to_ticks == 0)
1725 			error = EAGAIN;
1726 
1727 		if (error != 0)
1728 			break;
1729 
1730 		error = tsleep(&sigwaitsleep, PPAUSE|PCATCH, "sigwait",
1731 		    (int)to_ticks);
1732 	}
1733 
1734 	if (error == 0) {
1735 		*retval = si.si_signo;
1736 		if (SCARG(uap, info) != NULL)
1737 			error = copyout(&si, SCARG(uap, info), sizeof(si));
1738 	} else if (error == ERESTART && SCARG(uap, timeout) != NULL) {
1739 		/*
1740 		 * Restarting is wrong if there's a timeout, as it'll be
1741 		 * for the same interval again
1742 		 */
1743 		error = EINTR;
1744 	}
1745 
1746 	return (error);
1747 }
1748 
1749 void
1750 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val)
1751 {
1752 	memset(si, 0, sizeof(*si));
1753 
1754 	si->si_signo = sig;
1755 	si->si_code = code;
1756 	if (code == SI_USER) {
1757 		si->si_value = val;
1758 	} else {
1759 		switch (sig) {
1760 		case SIGSEGV:
1761 		case SIGILL:
1762 		case SIGBUS:
1763 		case SIGFPE:
1764 			si->si_addr = val.sival_ptr;
1765 			si->si_trapno = trapno;
1766 			break;
1767 		case SIGXFSZ:
1768 			break;
1769 		}
1770 	}
1771 }
1772 
1773 int
1774 filt_sigattach(struct knote *kn)
1775 {
1776 	struct process *pr = curproc->p_p;
1777 
1778 	if (kn->kn_id >= NSIG)
1779 		return EINVAL;
1780 
1781 	kn->kn_ptr.p_process = pr;
1782 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1783 
1784 	/* XXX lock the proc here while adding to the list? */
1785 	SLIST_INSERT_HEAD(&pr->ps_klist, kn, kn_selnext);
1786 
1787 	return (0);
1788 }
1789 
1790 void
1791 filt_sigdetach(struct knote *kn)
1792 {
1793 	struct process *pr = kn->kn_ptr.p_process;
1794 
1795 	SLIST_REMOVE(&pr->ps_klist, kn, knote, kn_selnext);
1796 }
1797 
1798 /*
1799  * signal knotes are shared with proc knotes, so we apply a mask to
1800  * the hint in order to differentiate them from process hints.  This
1801  * could be avoided by using a signal-specific knote list, but probably
1802  * isn't worth the trouble.
1803  */
1804 int
1805 filt_signal(struct knote *kn, long hint)
1806 {
1807 
1808 	if (hint & NOTE_SIGNAL) {
1809 		hint &= ~NOTE_SIGNAL;
1810 
1811 		if (kn->kn_id == hint)
1812 			kn->kn_data++;
1813 	}
1814 	return (kn->kn_data != 0);
1815 }
1816 
1817 void
1818 userret(struct proc *p)
1819 {
1820 	int signum;
1821 
1822 	/* send SIGPROF or SIGVTALRM if their timers interrupted this thread */
1823 	if (p->p_flag & P_PROFPEND) {
1824 		atomic_clearbits_int(&p->p_flag, P_PROFPEND);
1825 		KERNEL_LOCK();
1826 		psignal(p, SIGPROF);
1827 		KERNEL_UNLOCK();
1828 	}
1829 	if (p->p_flag & P_ALRMPEND) {
1830 		atomic_clearbits_int(&p->p_flag, P_ALRMPEND);
1831 		KERNEL_LOCK();
1832 		psignal(p, SIGVTALRM);
1833 		KERNEL_UNLOCK();
1834 	}
1835 
1836 	if (CURSIG(p) != 0) {
1837 		KERNEL_LOCK();
1838 		while ((signum = CURSIG(p)) != 0)
1839 			postsig(p, signum);
1840 		KERNEL_UNLOCK();
1841 	}
1842 
1843 	/*
1844 	 * If P_SIGSUSPEND is still set here, then we still need to restore
1845 	 * the original sigmask before returning to userspace.  Also, this
1846 	 * might unmask some pending signals, so we need to check a second
1847 	 * time for signals to post.
1848 	 */
1849 	if (p->p_flag & P_SIGSUSPEND) {
1850 		atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1851 		p->p_sigmask = p->p_oldmask;
1852 
1853 		KERNEL_LOCK();
1854 		while ((signum = CURSIG(p)) != 0)
1855 			postsig(p, signum);
1856 		KERNEL_UNLOCK();
1857 	}
1858 
1859 	if (p->p_flag & P_SUSPSINGLE) {
1860 		KERNEL_LOCK();
1861 		single_thread_check(p, 0);
1862 		KERNEL_UNLOCK();
1863 	}
1864 
1865 	WITNESS_WARN(WARN_PANIC, NULL, "userret: returning");
1866 
1867 	p->p_cpu->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
1868 }
1869 
1870 int
1871 single_thread_check(struct proc *p, int deep)
1872 {
1873 	struct process *pr = p->p_p;
1874 
1875 	if (pr->ps_single != NULL && pr->ps_single != p) {
1876 		do {
1877 			int s;
1878 
1879 			/* if we're in deep, we need to unwind to the edge */
1880 			if (deep) {
1881 				if (pr->ps_flags & PS_SINGLEUNWIND)
1882 					return (ERESTART);
1883 				if (pr->ps_flags & PS_SINGLEEXIT)
1884 					return (EINTR);
1885 			}
1886 
1887 			if (--pr->ps_singlecount == 0)
1888 				wakeup(&pr->ps_singlecount);
1889 			if (pr->ps_flags & PS_SINGLEEXIT)
1890 				exit1(p, 0, EXIT_THREAD_NOCHECK);
1891 
1892 			/* not exiting and don't need to unwind, so suspend */
1893 			SCHED_LOCK(s);
1894 			p->p_stat = SSTOP;
1895 			mi_switch();
1896 			SCHED_UNLOCK(s);
1897 		} while (pr->ps_single != NULL);
1898 	}
1899 
1900 	return (0);
1901 }
1902 
1903 /*
1904  * Stop other threads in the process.  The mode controls how and
1905  * where the other threads should stop:
1906  *  - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit
1907  *    (by setting to SINGLE_EXIT) or be released (via single_thread_clear())
1908  *  - SINGLE_PTRACE: stop wherever they are, will wait for them to stop
1909  *    later (via single_thread_wait()) and released as with SINGLE_SUSPEND
1910  *  - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit
1911  *    or released as with SINGLE_SUSPEND
1912  *  - SINGLE_EXIT: unwind to kernel boundary and exit
1913  */
1914 int
1915 single_thread_set(struct proc *p, enum single_thread_mode mode, int deep)
1916 {
1917 	struct process *pr = p->p_p;
1918 	struct proc *q;
1919 	int error;
1920 
1921 	KERNEL_ASSERT_LOCKED();
1922 
1923 	if ((error = single_thread_check(p, deep)))
1924 		return error;
1925 
1926 	switch (mode) {
1927 	case SINGLE_SUSPEND:
1928 	case SINGLE_PTRACE:
1929 		break;
1930 	case SINGLE_UNWIND:
1931 		atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
1932 		break;
1933 	case SINGLE_EXIT:
1934 		atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT);
1935 		atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
1936 		break;
1937 #ifdef DIAGNOSTIC
1938 	default:
1939 		panic("single_thread_mode = %d", mode);
1940 #endif
1941 	}
1942 	pr->ps_single = p;
1943 	pr->ps_singlecount = 0;
1944 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
1945 		int s;
1946 
1947 		if (q == p)
1948 			continue;
1949 		if (q->p_flag & P_WEXIT) {
1950 			if (mode == SINGLE_EXIT) {
1951 				SCHED_LOCK(s);
1952 				if (q->p_stat == SSTOP) {
1953 					setrunnable(q);
1954 					pr->ps_singlecount++;
1955 				}
1956 				SCHED_UNLOCK(s);
1957 			}
1958 			continue;
1959 		}
1960 		SCHED_LOCK(s);
1961 		atomic_setbits_int(&q->p_flag, P_SUSPSINGLE);
1962 		switch (q->p_stat) {
1963 		case SIDL:
1964 		case SRUN:
1965 			pr->ps_singlecount++;
1966 			break;
1967 		case SSLEEP:
1968 			/* if it's not interruptible, then just have to wait */
1969 			if (q->p_flag & P_SINTR) {
1970 				/* merely need to suspend?  just stop it */
1971 				if (mode == SINGLE_SUSPEND ||
1972 				    mode == SINGLE_PTRACE) {
1973 					q->p_stat = SSTOP;
1974 					break;
1975 				}
1976 				/* need to unwind or exit, so wake it */
1977 				setrunnable(q);
1978 			}
1979 			pr->ps_singlecount++;
1980 			break;
1981 		case SSTOP:
1982 			if (mode == SINGLE_EXIT) {
1983 				setrunnable(q);
1984 				pr->ps_singlecount++;
1985 			}
1986 			break;
1987 		case SDEAD:
1988 			break;
1989 		case SONPROC:
1990 			pr->ps_singlecount++;
1991 			signotify(q);
1992 			break;
1993 		}
1994 		SCHED_UNLOCK(s);
1995 	}
1996 
1997 	if (mode != SINGLE_PTRACE)
1998 		single_thread_wait(pr);
1999 
2000 	return 0;
2001 }
2002 
2003 void
2004 single_thread_wait(struct process *pr)
2005 {
2006 	/* wait until they're all suspended */
2007 	while (pr->ps_singlecount > 0)
2008 		tsleep(&pr->ps_singlecount, PUSER, "suspend", 0);
2009 }
2010 
2011 void
2012 single_thread_clear(struct proc *p, int flag)
2013 {
2014 	struct process *pr = p->p_p;
2015 	struct proc *q;
2016 
2017 	KASSERT(pr->ps_single == p);
2018 	KERNEL_ASSERT_LOCKED();
2019 
2020 	pr->ps_single = NULL;
2021 	atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT);
2022 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2023 		int s;
2024 
2025 		if (q == p || (q->p_flag & P_SUSPSINGLE) == 0)
2026 			continue;
2027 		atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE);
2028 
2029 		/*
2030 		 * if the thread was only stopped for single threading
2031 		 * then clearing that either makes it runnable or puts
2032 		 * it back into some sleep queue
2033 		 */
2034 		SCHED_LOCK(s);
2035 		if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) {
2036 			if (q->p_wchan == 0)
2037 				setrunnable(q);
2038 			else
2039 				q->p_stat = SSLEEP;
2040 		}
2041 		SCHED_UNLOCK(s);
2042 	}
2043 }
2044