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