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