xref: /openbsd-src/sys/kern/kern_sig.c (revision d874cce4b1d9fe6b41c9e4f2117a77d8a4a37b92)
1 /*	$OpenBSD: kern_sig.c,v 1.99 2008/06/10 20:41:52 hshoexer 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/timeb.h>
52 #include <sys/times.h>
53 #include <sys/buf.h>
54 #include <sys/acct.h>
55 #include <sys/file.h>
56 #include <sys/kernel.h>
57 #include <sys/wait.h>
58 #include <sys/ktrace.h>
59 #include <sys/stat.h>
60 #include <sys/core.h>
61 #include <sys/malloc.h>
62 #include <sys/pool.h>
63 #include <sys/ptrace.h>
64 #include <sys/sched.h>
65 
66 #include <sys/mount.h>
67 #include <sys/syscallargs.h>
68 
69 #include <machine/cpu.h>
70 
71 #include <uvm/uvm_extern.h>
72 #include <sys/user.h>		/* for coredump */
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 int cansignal(struct proc *, struct pcred *, struct proc *, int);
86 
87 struct pool sigacts_pool;	/* memory pool for sigacts structures */
88 
89 /*
90  * Can process p, with pcred pc, send the signal signum to process q?
91  */
92 int
93 cansignal(struct proc *p, struct pcred *pc, struct proc *q, int signum)
94 {
95 	if (pc->pc_ucred->cr_uid == 0)
96 		return (1);		/* root can always signal */
97 
98 	if (p == q)
99 		return (1);		/* process can always signal itself */
100 
101 #ifdef RTHREADS
102 	/* a thread can only be signalled from within the same process */
103 	if (q->p_flag & P_THREAD) {
104 		return (p->p_p == q->p_p);
105 	}
106 #endif
107 
108 	if (signum == SIGCONT && q->p_session == p->p_session)
109 		return (1);		/* SIGCONT in session */
110 
111 	/*
112 	 * Using kill(), only certain signals can be sent to setugid
113 	 * child processes
114 	 */
115 	if (q->p_flag & P_SUGID) {
116 		switch (signum) {
117 		case 0:
118 		case SIGKILL:
119 		case SIGINT:
120 		case SIGTERM:
121 		case SIGALRM:
122 		case SIGSTOP:
123 		case SIGTTIN:
124 		case SIGTTOU:
125 		case SIGTSTP:
126 		case SIGHUP:
127 		case SIGUSR1:
128 		case SIGUSR2:
129 			if (pc->p_ruid == q->p_cred->p_ruid ||
130 			    pc->pc_ucred->cr_uid == q->p_cred->p_ruid)
131 				return (1);
132 		}
133 		return (0);
134 	}
135 
136 	if (pc->p_ruid == q->p_cred->p_ruid ||
137 	    pc->p_ruid == q->p_cred->p_svuid ||
138 	    pc->pc_ucred->cr_uid == q->p_cred->p_ruid ||
139 	    pc->pc_ucred->cr_uid == q->p_cred->p_svuid)
140 		return (1);
141 	return (0);
142 }
143 
144 /*
145  * Initialize signal-related data structures.
146  */
147 void
148 signal_init(void)
149 {
150 	timeout_set(&proc_stop_to, proc_stop_sweep, NULL);
151 
152 	pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
153 	    &pool_allocator_nointr);
154 }
155 
156 /*
157  * Create an initial sigacts structure, using the same signal state
158  * as p.
159  */
160 struct sigacts *
161 sigactsinit(struct proc *p)
162 {
163 	struct sigacts *ps;
164 
165 	ps = pool_get(&sigacts_pool, PR_WAITOK);
166 	memcpy(ps, p->p_sigacts, sizeof(struct sigacts));
167 	ps->ps_refcnt = 1;
168 	return (ps);
169 }
170 
171 /*
172  * Make p2 share p1's sigacts.
173  */
174 void
175 sigactsshare(struct proc *p1, struct proc *p2)
176 {
177 
178 	p2->p_sigacts = p1->p_sigacts;
179 	p1->p_sigacts->ps_refcnt++;
180 }
181 
182 /*
183  * Make this process not share its sigacts, maintaining all
184  * signal state.
185  */
186 void
187 sigactsunshare(struct proc *p)
188 {
189 	struct sigacts *newps;
190 
191 	if (p->p_sigacts->ps_refcnt == 1)
192 		return;
193 
194 	newps = sigactsinit(p);
195 	sigactsfree(p);
196 	p->p_sigacts = newps;
197 }
198 
199 /*
200  * Release a sigacts structure.
201  */
202 void
203 sigactsfree(struct proc *p)
204 {
205 	struct sigacts *ps = p->p_sigacts;
206 
207 	if (--ps->ps_refcnt > 0)
208 		return;
209 
210 	p->p_sigacts = NULL;
211 
212 	pool_put(&sigacts_pool, ps);
213 }
214 
215 /* ARGSUSED */
216 int
217 sys_sigaction(struct proc *p, void *v, register_t *retval)
218 {
219 	struct sys_sigaction_args /* {
220 		syscallarg(int) signum;
221 		syscallarg(const struct sigaction *) nsa;
222 		syscallarg(struct sigaction *) osa;
223 	} */ *uap = v;
224 	struct sigaction vec;
225 	struct sigaction *sa;
226 	struct sigacts *ps = p->p_sigacts;
227 	int signum;
228 	int bit, error;
229 
230 	signum = SCARG(uap, signum);
231 	if (signum <= 0 || signum >= NSIG ||
232 	    (SCARG(uap, nsa) && (signum == SIGKILL || signum == SIGSTOP)))
233 		return (EINVAL);
234 	sa = &vec;
235 	if (SCARG(uap, osa)) {
236 		sa->sa_handler = ps->ps_sigact[signum];
237 		sa->sa_mask = ps->ps_catchmask[signum];
238 		bit = sigmask(signum);
239 		sa->sa_flags = 0;
240 		if ((ps->ps_sigonstack & bit) != 0)
241 			sa->sa_flags |= SA_ONSTACK;
242 		if ((ps->ps_sigintr & bit) == 0)
243 			sa->sa_flags |= SA_RESTART;
244 		if ((ps->ps_sigreset & bit) != 0)
245 			sa->sa_flags |= SA_RESETHAND;
246 		if ((ps->ps_siginfo & bit) != 0)
247 			sa->sa_flags |= SA_SIGINFO;
248 		if (signum == SIGCHLD) {
249 			if ((p->p_flag & P_NOCLDSTOP) != 0)
250 				sa->sa_flags |= SA_NOCLDSTOP;
251 			if ((p->p_flag & P_NOCLDWAIT) != 0)
252 				sa->sa_flags |= SA_NOCLDWAIT;
253 		}
254 		if ((sa->sa_mask & bit) == 0)
255 			sa->sa_flags |= SA_NODEFER;
256 		sa->sa_mask &= ~bit;
257 		error = copyout(sa, SCARG(uap, osa), sizeof (vec));
258 		if (error)
259 			return (error);
260 	}
261 	if (SCARG(uap, nsa)) {
262 		error = copyin(SCARG(uap, nsa), sa, sizeof (vec));
263 		if (error)
264 			return (error);
265 		setsigvec(p, signum, sa);
266 	}
267 	return (0);
268 }
269 
270 void
271 setsigvec(struct proc *p, int signum, struct sigaction *sa)
272 {
273 	struct sigacts *ps = p->p_sigacts;
274 	int bit;
275 	int s;
276 
277 	bit = sigmask(signum);
278 	/*
279 	 * Change setting atomically.
280 	 */
281 	s = splhigh();
282 	ps->ps_sigact[signum] = sa->sa_handler;
283 	if ((sa->sa_flags & SA_NODEFER) == 0)
284 		sa->sa_mask |= sigmask(signum);
285 	ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
286 	if (signum == SIGCHLD) {
287 		if (sa->sa_flags & SA_NOCLDSTOP)
288 			atomic_setbits_int(&p->p_flag, P_NOCLDSTOP);
289 		else
290 			atomic_clearbits_int(&p->p_flag, P_NOCLDSTOP);
291 		/*
292 		 * If the SA_NOCLDWAIT flag is set or the handler
293 		 * is SIG_IGN we reparent the dying child to PID 1
294 		 * (init) which will reap the zombie.  Because we use
295 		 * init to do our dirty work we never set P_NOCLDWAIT
296 		 * for PID 1.
297 		 */
298 		if (p->p_pid != 1 && ((sa->sa_flags & SA_NOCLDWAIT) ||
299 		    sa->sa_handler == SIG_IGN))
300 			atomic_setbits_int(&p->p_flag, P_NOCLDWAIT);
301 		else
302 			atomic_clearbits_int(&p->p_flag, P_NOCLDWAIT);
303 	}
304 	if ((sa->sa_flags & SA_RESETHAND) != 0)
305 		ps->ps_sigreset |= bit;
306 	else
307 		ps->ps_sigreset &= ~bit;
308 	if ((sa->sa_flags & SA_SIGINFO) != 0)
309 		ps->ps_siginfo |= bit;
310 	else
311 		ps->ps_siginfo &= ~bit;
312 	if ((sa->sa_flags & SA_RESTART) == 0)
313 		ps->ps_sigintr |= bit;
314 	else
315 		ps->ps_sigintr &= ~bit;
316 	if ((sa->sa_flags & SA_ONSTACK) != 0)
317 		ps->ps_sigonstack |= bit;
318 	else
319 		ps->ps_sigonstack &= ~bit;
320 #ifdef COMPAT_SUNOS
321 	{
322 		extern struct emul emul_sunos;
323 		if (p->p_emul == &emul_sunos) {
324 			if (sa->sa_flags & SA_USERTRAMP)
325 				ps->ps_usertramp |= bit;
326 			else
327 				ps->ps_usertramp &= ~bit;
328 		}
329 	}
330 #endif
331 	/*
332 	 * Set bit in p_sigignore for signals that are set to SIG_IGN,
333 	 * and for signals set to SIG_DFL where the default is to ignore.
334 	 * However, don't put SIGCONT in p_sigignore,
335 	 * as we have to restart the process.
336 	 */
337 	if (sa->sa_handler == SIG_IGN ||
338 	    (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
339 		atomic_clearbits_int(&p->p_siglist, bit);
340 		if (signum != SIGCONT)
341 			p->p_sigignore |= bit;	/* easier in psignal */
342 		p->p_sigcatch &= ~bit;
343 	} else {
344 		p->p_sigignore &= ~bit;
345 		if (sa->sa_handler == SIG_DFL)
346 			p->p_sigcatch &= ~bit;
347 		else
348 			p->p_sigcatch |= bit;
349 	}
350 	splx(s);
351 }
352 
353 /*
354  * Initialize signal state for process 0;
355  * set to ignore signals that are ignored by default.
356  */
357 void
358 siginit(struct proc *p)
359 {
360 	int i;
361 
362 	for (i = 0; i < NSIG; i++)
363 		if (sigprop[i] & SA_IGNORE && i != SIGCONT)
364 			p->p_sigignore |= sigmask(i);
365 }
366 
367 /*
368  * Reset signals for an exec of the specified process.
369  */
370 void
371 execsigs(struct proc *p)
372 {
373 	struct sigacts *ps;
374 	int nc, mask;
375 
376 	sigactsunshare(p);
377 	ps = p->p_sigacts;
378 
379 	/*
380 	 * Reset caught signals.  Held signals remain held
381 	 * through p_sigmask (unless they were caught,
382 	 * and are now ignored by default).
383 	 */
384 	while (p->p_sigcatch) {
385 		nc = ffs((long)p->p_sigcatch);
386 		mask = sigmask(nc);
387 		p->p_sigcatch &= ~mask;
388 		if (sigprop[nc] & SA_IGNORE) {
389 			if (nc != SIGCONT)
390 				p->p_sigignore |= mask;
391 			atomic_clearbits_int(&p->p_siglist, mask);
392 		}
393 		ps->ps_sigact[nc] = SIG_DFL;
394 	}
395 	/*
396 	 * Reset stack state to the user stack.
397 	 * Clear set of signals caught on the signal stack.
398 	 */
399 	ps->ps_sigstk.ss_flags = SS_DISABLE;
400 	ps->ps_sigstk.ss_size = 0;
401 	ps->ps_sigstk.ss_sp = 0;
402 	ps->ps_flags = 0;
403 	atomic_clearbits_int(&p->p_flag, P_NOCLDWAIT);
404 	if (ps->ps_sigact[SIGCHLD] == SIG_IGN)
405 		ps->ps_sigact[SIGCHLD] = SIG_DFL;
406 }
407 
408 /*
409  * Manipulate signal mask.
410  * Note that we receive new mask, not pointer,
411  * and return old mask as return value;
412  * the library stub does the rest.
413  */
414 int
415 sys_sigprocmask(struct proc *p, void *v, register_t *retval)
416 {
417 	struct sys_sigprocmask_args /* {
418 		syscallarg(int) how;
419 		syscallarg(sigset_t) mask;
420 	} */ *uap = v;
421 	int error = 0;
422 	int s;
423 
424 	*retval = p->p_sigmask;
425 	s = splhigh();
426 
427 	switch (SCARG(uap, how)) {
428 	case SIG_BLOCK:
429 		p->p_sigmask |= SCARG(uap, mask) &~ sigcantmask;
430 		break;
431 
432 	case SIG_UNBLOCK:
433 		p->p_sigmask &= ~SCARG(uap, mask);
434 		break;
435 
436 	case SIG_SETMASK:
437 		p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
438 		break;
439 
440 	default:
441 		error = EINVAL;
442 		break;
443 	}
444 	splx(s);
445 	return (error);
446 }
447 
448 /* ARGSUSED */
449 int
450 sys_sigpending(struct proc *p, void *v, register_t *retval)
451 {
452 
453 	*retval = p->p_siglist;
454 	return (0);
455 }
456 
457 /*
458  * Suspend process until signal, providing mask to be set
459  * in the meantime.  Note nonstandard calling convention:
460  * libc stub passes mask, not pointer, to save a copyin.
461  */
462 /* ARGSUSED */
463 int
464 sys_sigsuspend(struct proc *p, void *v, register_t *retval)
465 {
466 	struct sys_sigsuspend_args /* {
467 		syscallarg(int) mask;
468 	} */ *uap = v;
469 	struct sigacts *ps = p->p_sigacts;
470 
471 	/*
472 	 * When returning from sigpause, we want
473 	 * the old mask to be restored after the
474 	 * signal handler has finished.  Thus, we
475 	 * save it here and mark the sigacts structure
476 	 * to indicate this.
477 	 */
478 	ps->ps_oldmask = p->p_sigmask;
479 	ps->ps_flags |= SAS_OLDMASK;
480 	p->p_sigmask = SCARG(uap, mask) &~ sigcantmask;
481 	while (tsleep(ps, PPAUSE|PCATCH, "pause", 0) == 0)
482 		/* void */;
483 	/* always return EINTR rather than ERESTART... */
484 	return (EINTR);
485 }
486 
487 /* ARGSUSED */
488 int
489 sys_osigaltstack(struct proc *p, void *v, register_t *retval)
490 {
491 	struct sys_osigaltstack_args /* {
492 		syscallarg(const struct osigaltstack *) nss;
493 		syscallarg(struct osigaltstack *) oss;
494 	} */ *uap = v;
495 	struct sigacts *psp;
496 	struct osigaltstack ss;
497 	int error;
498 
499 	psp = p->p_sigacts;
500 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
501 		psp->ps_sigstk.ss_flags |= SS_DISABLE;
502 	if (SCARG(uap, oss)) {
503 		ss.ss_sp = psp->ps_sigstk.ss_sp;
504 		ss.ss_size = psp->ps_sigstk.ss_size;
505 		ss.ss_flags = psp->ps_sigstk.ss_flags;
506 		if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))))
507 			return (error);
508 	}
509 	if (SCARG(uap, nss) == NULL)
510 		return (0);
511 	error = copyin(SCARG(uap, nss), &ss, sizeof(ss));
512 	if (error)
513 		return (error);
514 	if (ss.ss_flags & SS_DISABLE) {
515 		if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
516 			return (EINVAL);
517 		psp->ps_flags &= ~SAS_ALTSTACK;
518 		psp->ps_sigstk.ss_flags = ss.ss_flags;
519 		return (0);
520 	}
521 	if (ss.ss_size < MINSIGSTKSZ)
522 		return (ENOMEM);
523 	psp->ps_flags |= SAS_ALTSTACK;
524 	psp->ps_sigstk.ss_sp = ss.ss_sp;
525 	psp->ps_sigstk.ss_size = ss.ss_size;
526 	psp->ps_sigstk.ss_flags = ss.ss_flags;
527 	return (0);
528 }
529 
530 int
531 sys_sigaltstack(struct proc *p, void *v, register_t *retval)
532 {
533 	struct sys_sigaltstack_args /* {
534 		syscallarg(const struct sigaltstack *) nss;
535 		syscallarg(struct sigaltstack *) oss;
536 	} */ *uap = v;
537 	struct sigacts *psp;
538 	struct sigaltstack ss;
539 	int error;
540 
541 	psp = p->p_sigacts;
542 	if ((psp->ps_flags & SAS_ALTSTACK) == 0)
543 		psp->ps_sigstk.ss_flags |= SS_DISABLE;
544 	if (SCARG(uap, oss) && (error = copyout(&psp->ps_sigstk,
545 	    SCARG(uap, oss), sizeof(struct sigaltstack))))
546 		return (error);
547 	if (SCARG(uap, nss) == NULL)
548 		return (0);
549 	error = copyin(SCARG(uap, nss), &ss, sizeof(ss));
550 	if (error)
551 		return (error);
552 	if (ss.ss_flags & SS_DISABLE) {
553 		if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
554 			return (EINVAL);
555 		psp->ps_flags &= ~SAS_ALTSTACK;
556 		psp->ps_sigstk.ss_flags = ss.ss_flags;
557 		return (0);
558 	}
559 	if (ss.ss_size < MINSIGSTKSZ)
560 		return (ENOMEM);
561 	psp->ps_flags |= SAS_ALTSTACK;
562 	psp->ps_sigstk = ss;
563 	return (0);
564 }
565 
566 /* ARGSUSED */
567 int
568 sys_kill(struct proc *cp, void *v, register_t *retval)
569 {
570 	struct sys_kill_args /* {
571 		syscallarg(int) pid;
572 		syscallarg(int) signum;
573 	} */ *uap = v;
574 	struct proc *p;
575 	struct pcred *pc = cp->p_cred;
576 
577 	if ((u_int)SCARG(uap, signum) >= NSIG)
578 		return (EINVAL);
579 	if (SCARG(uap, pid) > 0) {
580 		/* kill single process */
581 		if ((p = pfind(SCARG(uap, pid))) == NULL)
582 			return (ESRCH);
583 		if (!cansignal(cp, pc, p, SCARG(uap, signum)))
584 			return (EPERM);
585 		if (SCARG(uap, signum))
586 			psignal(p, SCARG(uap, signum));
587 		return (0);
588 	}
589 	switch (SCARG(uap, pid)) {
590 	case -1:		/* broadcast signal */
591 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
592 	case 0:			/* signal own process group */
593 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
594 	default:		/* negative explicit process group */
595 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
596 	}
597 	/* NOTREACHED */
598 }
599 
600 /*
601  * Common code for kill process group/broadcast kill.
602  * cp is calling process.
603  */
604 int
605 killpg1(struct proc *cp, int signum, int pgid, int all)
606 {
607 	struct proc *p;
608 	struct pcred *pc = cp->p_cred;
609 	struct pgrp *pgrp;
610 	int nfound = 0;
611 
612 	if (all)
613 		/*
614 		 * broadcast
615 		 */
616 		LIST_FOREACH(p, &allproc, p_list) {
617 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
618 			    p == cp || !cansignal(cp, pc, p, signum))
619 				continue;
620 			nfound++;
621 			if (signum)
622 				psignal(p, signum);
623 		}
624 	else {
625 		if (pgid == 0)
626 			/*
627 			 * zero pgid means send to my process group.
628 			 */
629 			pgrp = cp->p_pgrp;
630 		else {
631 			pgrp = pgfind(pgid);
632 			if (pgrp == NULL)
633 				return (ESRCH);
634 		}
635 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
636 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
637 			    !cansignal(cp, pc, p, signum))
638 				continue;
639 			nfound++;
640 			if (signum && P_ZOMBIE(p) == 0)
641 				psignal(p, signum);
642 		}
643 	}
644 	return (nfound ? 0 : ESRCH);
645 }
646 
647 #define CANDELIVER(uid, euid, p) \
648 	(euid == 0 || \
649 	(uid) == (p)->p_cred->p_ruid || \
650 	(uid) == (p)->p_cred->p_svuid || \
651 	(uid) == (p)->p_ucred->cr_uid || \
652 	(euid) == (p)->p_cred->p_ruid || \
653 	(euid) == (p)->p_cred->p_svuid || \
654 	(euid) == (p)->p_ucred->cr_uid)
655 
656 /*
657  * Deliver signum to pgid, but first check uid/euid against each
658  * process and see if it is permitted.
659  */
660 void
661 csignal(pid_t pgid, int signum, uid_t uid, uid_t euid)
662 {
663 	struct pgrp *pgrp;
664 	struct proc *p;
665 
666 	if (pgid == 0)
667 		return;
668 	if (pgid < 0) {
669 		pgid = -pgid;
670 		if ((pgrp = pgfind(pgid)) == NULL)
671 			return;
672 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
673 			if (CANDELIVER(uid, euid, p))
674 				psignal(p, signum);
675 	} else {
676 		if ((p = pfind(pgid)) == NULL)
677 			return;
678 		if (CANDELIVER(uid, euid, p))
679 			psignal(p, signum);
680 	}
681 }
682 
683 /*
684  * Send a signal to a process group.
685  */
686 void
687 gsignal(int pgid, int signum)
688 {
689 	struct pgrp *pgrp;
690 
691 	if (pgid && (pgrp = pgfind(pgid)))
692 		pgsignal(pgrp, signum, 0);
693 }
694 
695 /*
696  * Send a signal to a process group.  If checktty is 1,
697  * limit to members which have a controlling terminal.
698  */
699 void
700 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
701 {
702 	struct proc *p;
703 
704 	if (pgrp)
705 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
706 			if ((checkctty == 0 || p->p_flag & P_CONTROLT) &&
707 			    (p->p_flag & P_THREAD) == 0)
708 				psignal(p, signum);
709 }
710 
711 /*
712  * Send a signal caused by a trap to the current process.
713  * If it will be caught immediately, deliver it with correct code.
714  * Otherwise, post it normally.
715  */
716 void
717 trapsignal(struct proc *p, int signum, u_long code, int type,
718     union sigval sigval)
719 {
720 	struct sigacts *ps = p->p_sigacts;
721 	int mask;
722 
723 	mask = sigmask(signum);
724 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
725 	    (p->p_sigmask & mask) == 0) {
726 #ifdef KTRACE
727 		if (KTRPOINT(p, KTR_PSIG)) {
728 			siginfo_t si;
729 
730 			initsiginfo(&si, signum, code, type, sigval);
731 			ktrpsig(p, signum, ps->ps_sigact[signum],
732 			    p->p_sigmask, type, &si);
733 		}
734 #endif
735 		p->p_stats->p_ru.ru_nsignals++;
736 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum,
737 		    p->p_sigmask, code, type, sigval);
738 		p->p_sigmask |= ps->ps_catchmask[signum];
739 		if ((ps->ps_sigreset & mask) != 0) {
740 			p->p_sigcatch &= ~mask;
741 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
742 				p->p_sigignore |= mask;
743 			ps->ps_sigact[signum] = SIG_DFL;
744 		}
745 	} else {
746 		ps->ps_sig = signum;
747 		ps->ps_code = code;	/* XXX for core dump/debugger */
748 		ps->ps_type = type;
749 		ps->ps_sigval = sigval;
750 		psignal(p, signum);
751 	}
752 }
753 
754 /*
755  * Send the signal to the process.  If the signal has an action, the action
756  * is usually performed by the target process rather than the caller; we add
757  * the signal to the set of pending signals for the process.
758  *
759  * Exceptions:
760  *   o When a stop signal is sent to a sleeping process that takes the
761  *     default action, the process is stopped without awakening it.
762  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
763  *     regardless of the signal action (eg, blocked or ignored).
764  *
765  * Other ignored signals are discarded immediately.
766  */
767 void
768 psignal(struct proc *p, int signum)
769 {
770 	int s, prop;
771 	sig_t action;
772 	int mask;
773 #ifdef RTHREADS
774 	struct proc *q;
775 #endif
776 	int wakeparent = 0;
777 
778 #ifdef DIAGNOSTIC
779 	if ((u_int)signum >= NSIG || signum == 0)
780 		panic("psignal signal number");
781 #endif
782 
783 	/* Ignore signal if we are exiting */
784 	if (p->p_flag & P_WEXIT)
785 		return;
786 
787 #ifdef RTHREADS
788 	TAILQ_FOREACH(q, &p->p_p->ps_threads, p_thr_link) {
789 		if (q == p)
790 			continue;
791 		if (q->p_sigdivert & (1 << signum)) {
792 			q->p_sigdivert = 0;
793 			psignal(q, signum);
794 			return;
795 		}
796 	}
797 #endif
798 
799 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
800 
801 	mask = sigmask(signum);
802 	prop = sigprop[signum];
803 
804 	/*
805 	 * If proc is traced, always give parent a chance.
806 	 */
807 	if (p->p_flag & P_TRACED)
808 		action = SIG_DFL;
809 	else {
810 		/*
811 		 * If the signal is being ignored,
812 		 * then we forget about it immediately.
813 		 * (Note: we don't set SIGCONT in p_sigignore,
814 		 * and if it is set to SIG_IGN,
815 		 * action will be SIG_DFL here.)
816 		 */
817 		if (p->p_sigignore & mask)
818 			return;
819 		if (p->p_sigmask & mask)
820 			action = SIG_HOLD;
821 		else if (p->p_sigcatch & mask)
822 			action = SIG_CATCH;
823 		else {
824 			action = SIG_DFL;
825 
826 			if (prop & SA_KILL && p->p_nice > NZERO)
827 				p->p_nice = NZERO;
828 
829 			/*
830 			 * If sending a tty stop signal to a member of an
831 			 * orphaned process group, discard the signal here if
832 			 * the action is default; don't stop the process below
833 			 * if sleeping, and don't clear any pending SIGCONT.
834 			 */
835 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
836 				return;
837 		}
838 	}
839 
840 	if (prop & SA_CONT) {
841 #ifdef RTHREADS
842 		TAILQ_FOREACH(q, &p->p_p->ps_threads, p_thr_link) {
843 			if (q != p)
844 				psignal(q, signum);
845 		 }
846 #endif
847 		atomic_clearbits_int(&p->p_siglist, stopsigmask);
848 	}
849 
850 	if (prop & SA_STOP) {
851 #ifdef RTHREADS
852 
853 		TAILQ_FOREACH(q, &p->p_p->ps_threads, p_thr_link) {
854 			if (q != p)
855 				psignal(q, signum);
856 		 }
857 #endif
858 		atomic_clearbits_int(&p->p_siglist, contsigmask);
859 		atomic_clearbits_int(&p->p_flag, P_CONTINUED);
860 	}
861 
862 	atomic_setbits_int(&p->p_siglist, mask);
863 
864 	/*
865 	 * Defer further processing for signals which are held,
866 	 * except that stopped processes must be continued by SIGCONT.
867 	 */
868 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
869 		return;
870 
871 	SCHED_LOCK(s);
872 
873 	switch (p->p_stat) {
874 
875 	case SSLEEP:
876 		/*
877 		 * If process is sleeping uninterruptibly
878 		 * we can't interrupt the sleep... the signal will
879 		 * be noticed when the process returns through
880 		 * trap() or syscall().
881 		 */
882 		if ((p->p_flag & P_SINTR) == 0)
883 			goto out;
884 		/*
885 		 * Process is sleeping and traced... make it runnable
886 		 * so it can discover the signal in issignal() and stop
887 		 * for the parent.
888 		 */
889 		if (p->p_flag & P_TRACED)
890 			goto run;
891 		/*
892 		 * If SIGCONT is default (or ignored) and process is
893 		 * asleep, we are finished; the process should not
894 		 * be awakened.
895 		 */
896 		if ((prop & SA_CONT) && action == SIG_DFL) {
897 			atomic_clearbits_int(&p->p_siglist, mask);
898 			goto out;
899 		}
900 		/*
901 		 * When a sleeping process receives a stop
902 		 * signal, process immediately if possible.
903 		 */
904 		if ((prop & SA_STOP) && action == SIG_DFL) {
905 			/*
906 			 * If a child holding parent blocked,
907 			 * stopping could cause deadlock.
908 			 */
909 			if (p->p_flag & P_PPWAIT)
910 				goto out;
911 			atomic_clearbits_int(&p->p_siglist, mask);
912 			p->p_xstat = signum;
913 			proc_stop(p, 0);
914 			goto out;
915 		}
916 		/*
917 		 * All other (caught or default) signals
918 		 * cause the process to run.
919 		 */
920 		goto runfast;
921 		/*NOTREACHED*/
922 
923 	case SSTOP:
924 		/*
925 		 * If traced process is already stopped,
926 		 * then no further action is necessary.
927 		 */
928 		if (p->p_flag & P_TRACED)
929 			goto out;
930 
931 		/*
932 		 * Kill signal always sets processes running.
933 		 */
934 		if (signum == SIGKILL)
935 			goto runfast;
936 
937 		if (prop & SA_CONT) {
938 			/*
939 			 * If SIGCONT is default (or ignored), we continue the
940 			 * process but don't leave the signal in p_siglist, as
941 			 * it has no further action.  If SIGCONT is held, we
942 			 * continue the process and leave the signal in
943 			 * p_siglist.  If the process catches SIGCONT, let it
944 			 * handle the signal itself.  If it isn't waiting on
945 			 * an event, then it goes back to run state.
946 			 * Otherwise, process goes back to sleep state.
947 			 */
948 			atomic_setbits_int(&p->p_flag, P_CONTINUED);
949 			wakeparent = 1;
950 			if (action == SIG_DFL)
951 				atomic_clearbits_int(&p->p_siglist, mask);
952 			if (action == SIG_CATCH)
953 				goto runfast;
954 			if (p->p_wchan == 0)
955 				goto run;
956 			p->p_stat = SSLEEP;
957 			goto out;
958 		}
959 
960 		if (prop & SA_STOP) {
961 			/*
962 			 * Already stopped, don't need to stop again.
963 			 * (If we did the shell could get confused.)
964 			 */
965 			atomic_clearbits_int(&p->p_siglist, mask);
966 			goto out;
967 		}
968 
969 		/*
970 		 * If process is sleeping interruptibly, then simulate a
971 		 * wakeup so that when it is continued, it will be made
972 		 * runnable and can look at the signal.  But don't make
973 		 * the process runnable, leave it stopped.
974 		 */
975 		if (p->p_wchan && p->p_flag & P_SINTR)
976 			unsleep(p);
977 		goto out;
978 
979 	case SONPROC:
980 		signotify(p);
981 		/* FALLTHROUGH */
982 	default:
983 		/*
984 		 * SRUN, SIDL, SZOMB do nothing with the signal,
985 		 * other than kicking ourselves if we are running.
986 		 * It will either never be noticed, or noticed very soon.
987 		 */
988 		goto out;
989 	}
990 	/*NOTREACHED*/
991 
992 runfast:
993 	/*
994 	 * Raise priority to at least PUSER.
995 	 */
996 	if (p->p_priority > PUSER)
997 		p->p_priority = PUSER;
998 run:
999 	setrunnable(p);
1000 out:
1001 	SCHED_UNLOCK(s);
1002 	if (wakeparent)
1003 		wakeup(p->p_pptr);
1004 }
1005 
1006 /*
1007  * If the current process has received a signal (should be caught or cause
1008  * termination, should interrupt current syscall), return the signal number.
1009  * Stop signals with default action are processed immediately, then cleared;
1010  * they aren't returned.  This is checked after each entry to the system for
1011  * a syscall or trap (though this can usually be done without calling issignal
1012  * by checking the pending signal masks in the CURSIG macro.) The normal call
1013  * sequence is
1014  *
1015  *	while (signum = CURSIG(curproc))
1016  *		postsig(signum);
1017  */
1018 int
1019 issignal(struct proc *p)
1020 {
1021 	int signum, mask, prop;
1022 	int dolock = (p->p_flag & P_SINTR) == 0;
1023 	int s;
1024 
1025 	for (;;) {
1026 		mask = p->p_siglist & ~p->p_sigmask;
1027 		if (p->p_flag & P_PPWAIT)
1028 			mask &= ~stopsigmask;
1029 		if (mask == 0)	 	/* no signal to send */
1030 			return (0);
1031 		signum = ffs((long)mask);
1032 		mask = sigmask(signum);
1033 		atomic_clearbits_int(&p->p_siglist, mask);
1034 
1035 		/*
1036 		 * We should see pending but ignored signals
1037 		 * only if P_TRACED was on when they were posted.
1038 		 */
1039 		if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0)
1040 			continue;
1041 
1042 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1043 			/*
1044 			 * If traced, always stop, and stay
1045 			 * stopped until released by the debugger.
1046 			 */
1047 			p->p_xstat = signum;
1048 
1049 			if (dolock)
1050 				SCHED_LOCK(s);
1051 			proc_stop(p, 1);
1052 			if (dolock)
1053 				SCHED_UNLOCK(s);
1054 
1055 			/*
1056 			 * If we are no longer being traced, or the parent
1057 			 * didn't give us a signal, look for more signals.
1058 			 */
1059 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1060 				continue;
1061 
1062 			/*
1063 			 * If the new signal is being masked, look for other
1064 			 * signals.
1065 			 */
1066 			signum = p->p_xstat;
1067 			mask = sigmask(signum);
1068 			if ((p->p_sigmask & mask) != 0)
1069 				continue;
1070 
1071 			/* take the signal! */
1072 			atomic_clearbits_int(&p->p_siglist, mask);
1073 		}
1074 
1075 		prop = sigprop[signum];
1076 
1077 		/*
1078 		 * Decide whether the signal should be returned.
1079 		 * Return the signal's number, or fall through
1080 		 * to clear it from the pending mask.
1081 		 */
1082 		switch ((long)p->p_sigacts->ps_sigact[signum]) {
1083 
1084 		case (long)SIG_DFL:
1085 			/*
1086 			 * Don't take default actions on system processes.
1087 			 */
1088 			if (p->p_pid <= 1) {
1089 #ifdef DIAGNOSTIC
1090 				/*
1091 				 * Are you sure you want to ignore SIGSEGV
1092 				 * in init? XXX
1093 				 */
1094 				printf("Process (pid %d) got signal %d\n",
1095 				    p->p_pid, signum);
1096 #endif
1097 				break;		/* == ignore */
1098 			}
1099 			/*
1100 			 * If there is a pending stop signal to process
1101 			 * with default action, stop here,
1102 			 * then clear the signal.  However,
1103 			 * if process is member of an orphaned
1104 			 * process group, ignore tty stop signals.
1105 			 */
1106 			if (prop & SA_STOP) {
1107 				if (p->p_flag & P_TRACED ||
1108 		    		    (p->p_pgrp->pg_jobc == 0 &&
1109 				    prop & SA_TTYSTOP))
1110 					break;	/* == ignore */
1111 				p->p_xstat = signum;
1112 				if (dolock)
1113 					SCHED_LOCK(s);
1114 				proc_stop(p, 1);
1115 				if (dolock)
1116 					SCHED_UNLOCK(s);
1117 				break;
1118 			} else if (prop & SA_IGNORE) {
1119 				/*
1120 				 * Except for SIGCONT, shouldn't get here.
1121 				 * Default action is to ignore; drop it.
1122 				 */
1123 				break;		/* == ignore */
1124 			} else
1125 				goto keep;
1126 			/*NOTREACHED*/
1127 
1128 		case (long)SIG_IGN:
1129 			/*
1130 			 * Masking above should prevent us ever trying
1131 			 * to take action on an ignored signal other
1132 			 * than SIGCONT, unless process is traced.
1133 			 */
1134 			if ((prop & SA_CONT) == 0 &&
1135 			    (p->p_flag & P_TRACED) == 0)
1136 				printf("issignal\n");
1137 			break;		/* == ignore */
1138 
1139 		default:
1140 			/*
1141 			 * This signal has an action, let
1142 			 * postsig() process it.
1143 			 */
1144 			goto keep;
1145 		}
1146 	}
1147 	/* NOTREACHED */
1148 
1149 keep:
1150 	atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */
1151 	return (signum);
1152 }
1153 
1154 /*
1155  * Put the argument process into the stopped state and notify the parent
1156  * via wakeup.  Signals are handled elsewhere.  The process must not be
1157  * on the run queue.
1158  */
1159 void
1160 proc_stop(struct proc *p, int sw)
1161 {
1162 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1163 	extern void *softclock_si;
1164 #endif
1165 #ifdef MULTIPROCESSOR
1166 	SCHED_ASSERT_LOCKED();
1167 #endif
1168 
1169 	p->p_stat = SSTOP;
1170 	atomic_clearbits_int(&p->p_flag, P_WAITED);
1171 	atomic_setbits_int(&p->p_flag, P_STOPPED);
1172 	if (!timeout_pending(&proc_stop_to)) {
1173 		timeout_add(&proc_stop_to, 0);
1174 		/*
1175 		 * We need this soft interrupt to be handled fast.
1176 		 * Extra calls to softclock don't hurt.
1177 		 */
1178 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1179                 softintr_schedule(softclock_si);
1180 #else
1181                 setsoftclock();
1182 #endif
1183 	}
1184 	if (sw)
1185 		mi_switch();
1186 }
1187 
1188 /*
1189  * Called from a timeout to send signals to the parents of stopped processes.
1190  * We can't do this in proc_stop because it's called with nasty locks held
1191  * and we would need recursive scheduler lock to deal with that.
1192  */
1193 void
1194 proc_stop_sweep(void *v)
1195 {
1196 	struct proc *p;
1197 
1198 	LIST_FOREACH(p, &allproc, p_list) {
1199 		if ((p->p_flag & P_STOPPED) == 0)
1200 			continue;
1201 		atomic_clearbits_int(&p->p_flag, P_STOPPED);
1202 
1203 		if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1204 			psignal(p->p_pptr, SIGCHLD);
1205 		wakeup(p->p_pptr);
1206 	}
1207 }
1208 
1209 /*
1210  * Take the action for the specified signal
1211  * from the current set of pending signals.
1212  */
1213 void
1214 postsig(int signum)
1215 {
1216 	struct proc *p = curproc;
1217 	struct sigacts *ps = p->p_sigacts;
1218 	sig_t action;
1219 	u_long code;
1220 	int mask, returnmask;
1221 	union sigval sigval;
1222 	int s, type;
1223 
1224 #ifdef DIAGNOSTIC
1225 	if (signum == 0)
1226 		panic("postsig");
1227 #endif
1228 
1229 	KERNEL_PROC_LOCK(p);
1230 
1231 	mask = sigmask(signum);
1232 	atomic_clearbits_int(&p->p_siglist, mask);
1233 	action = ps->ps_sigact[signum];
1234 	sigval.sival_ptr = 0;
1235 	type = SI_USER;
1236 
1237 	if (ps->ps_sig != signum) {
1238 		code = 0;
1239 		type = SI_USER;
1240 		sigval.sival_ptr = 0;
1241 	} else {
1242 		code = ps->ps_code;
1243 		type = ps->ps_type;
1244 		sigval = ps->ps_sigval;
1245 	}
1246 
1247 #ifdef KTRACE
1248 	if (KTRPOINT(p, KTR_PSIG)) {
1249 		siginfo_t si;
1250 
1251 		initsiginfo(&si, signum, code, type, sigval);
1252 		ktrpsig(p, signum, action, ps->ps_flags & SAS_OLDMASK ?
1253 		    ps->ps_oldmask : p->p_sigmask, type, &si);
1254 	}
1255 #endif
1256 	if (action == SIG_DFL) {
1257 		/*
1258 		 * Default action, where the default is to kill
1259 		 * the process.  (Other cases were ignored above.)
1260 		 */
1261 		sigexit(p, signum);
1262 		/* NOTREACHED */
1263 	} else {
1264 		/*
1265 		 * If we get here, the signal must be caught.
1266 		 */
1267 #ifdef DIAGNOSTIC
1268 		if (action == SIG_IGN || (p->p_sigmask & mask))
1269 			panic("postsig action");
1270 #endif
1271 		/*
1272 		 * Set the new mask value and also defer further
1273 		 * occurrences of this signal.
1274 		 *
1275 		 * Special case: user has done a sigpause.  Here the
1276 		 * current mask is not of interest, but rather the
1277 		 * mask from before the sigpause is what we want
1278 		 * restored after the signal processing is completed.
1279 		 */
1280 #ifdef MULTIPROCESSOR
1281 		s = splsched();
1282 #else
1283 		s = splhigh();
1284 #endif
1285 		if (ps->ps_flags & SAS_OLDMASK) {
1286 			returnmask = ps->ps_oldmask;
1287 			ps->ps_flags &= ~SAS_OLDMASK;
1288 		} else
1289 			returnmask = p->p_sigmask;
1290 		p->p_sigmask |= ps->ps_catchmask[signum];
1291 		if ((ps->ps_sigreset & mask) != 0) {
1292 			p->p_sigcatch &= ~mask;
1293 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1294 				p->p_sigignore |= mask;
1295 			ps->ps_sigact[signum] = SIG_DFL;
1296 		}
1297 		splx(s);
1298 		p->p_stats->p_ru.ru_nsignals++;
1299 		if (ps->ps_sig == signum) {
1300 			ps->ps_sig = 0;
1301 			ps->ps_code = 0;
1302 			ps->ps_type = SI_USER;
1303 			ps->ps_sigval.sival_ptr = NULL;
1304 		}
1305 
1306 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code,
1307 		    type, sigval);
1308 	}
1309 
1310 	KERNEL_PROC_UNLOCK(p);
1311 }
1312 
1313 /*
1314  * Force the current process to exit with the specified signal, dumping core
1315  * if appropriate.  We bypass the normal tests for masked and caught signals,
1316  * allowing unrecoverable failures to terminate the process without changing
1317  * signal state.  Mark the accounting record with the signal termination.
1318  * If dumping core, save the signal number for the debugger.  Calls exit and
1319  * does not return.
1320  */
1321 void
1322 sigexit(struct proc *p, int signum)
1323 {
1324 	/* Mark process as going away */
1325 	atomic_setbits_int(&p->p_flag, P_WEXIT);
1326 
1327 	p->p_acflag |= AXSIG;
1328 	if (sigprop[signum] & SA_CORE) {
1329 		p->p_sigacts->ps_sig = signum;
1330 		if (coredump(p) == 0)
1331 			signum |= WCOREFLAG;
1332 	}
1333 	exit1(p, W_EXITCODE(0, signum), EXIT_NORMAL);
1334 	/* NOTREACHED */
1335 }
1336 
1337 int nosuidcoredump = 1;
1338 
1339 /*
1340  * Dump core, into a file named "progname.core", unless the process was
1341  * setuid/setgid.
1342  */
1343 int
1344 coredump(struct proc *p)
1345 {
1346 	struct vnode *vp;
1347 	struct ucred *cred = p->p_ucred;
1348 	struct vmspace *vm = p->p_vmspace;
1349 	struct nameidata nd;
1350 	struct vattr vattr;
1351 	int error, error1, len;
1352 	char name[sizeof("/var/crash/") + MAXCOMLEN + sizeof(".core")];
1353 	char *dir = "";
1354 	struct core core;
1355 
1356 	/*
1357 	 * Don't dump if not root and the process has used set user or
1358 	 * group privileges, unless the nosuidcoredump sysctl is set to 2,
1359 	 * in which case dumps are put into /var/crash/.
1360 	 */
1361 	if (((p->p_flag & P_SUGID) && (error = suser(p, 0))) ||
1362 	   ((p->p_flag & P_SUGID) && nosuidcoredump)) {
1363 		if (nosuidcoredump == 2)
1364 			dir = "/var/crash/";
1365 		else
1366 			return (EPERM);
1367 	}
1368 
1369 	/* Don't dump if will exceed file size limit. */
1370 	if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >=
1371 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1372 		return (EFBIG);
1373 
1374 	len = snprintf(name, sizeof(name), "%s%s.core", dir, p->p_comm);
1375 	if (len >= sizeof(name))
1376 		return (EACCES);
1377 
1378 	/*
1379 	 * ... but actually write it as UID
1380 	 */
1381 	cred = crdup(cred);
1382 	cred->cr_uid = p->p_cred->p_ruid;
1383 	cred->cr_gid = p->p_cred->p_rgid;
1384 
1385 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1386 
1387 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1388 
1389 	if (error) {
1390 		crfree(cred);
1391 		return (error);
1392 	}
1393 
1394 	/*
1395 	 * Don't dump to non-regular files, files with links, or files
1396 	 * owned by someone else.
1397 	 */
1398 	vp = nd.ni_vp;
1399 	if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0)
1400 		goto out;
1401 	if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1402 	    vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) ||
1403 	    vattr.va_uid != cred->cr_uid) {
1404 		error = EACCES;
1405 		goto out;
1406 	}
1407 	VATTR_NULL(&vattr);
1408 	vattr.va_size = 0;
1409 	VOP_SETATTR(vp, &vattr, cred, p);
1410 	p->p_acflag |= ACORE;
1411 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1412 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1413 
1414 	core.c_midmag = 0;
1415 	strlcpy(core.c_name, p->p_comm, sizeof(core.c_name));
1416 	core.c_nseg = 0;
1417 	core.c_signo = p->p_sigacts->ps_sig;
1418 	core.c_ucode = p->p_sigacts->ps_code;
1419 	core.c_cpusize = 0;
1420 	core.c_tsize = (u_long)ptoa(vm->vm_tsize);
1421 	core.c_dsize = (u_long)ptoa(vm->vm_dsize);
1422 	core.c_ssize = (u_long)round_page(ptoa(vm->vm_ssize));
1423 	error = cpu_coredump(p, vp, cred, &core);
1424 	if (error)
1425 		goto out;
1426 	/*
1427 	 * uvm_coredump() spits out all appropriate segments.
1428 	 * All that's left to do is to write the core header.
1429 	 */
1430 	error = uvm_coredump(p, vp, cred, &core);
1431 	if (error)
1432 		goto out;
1433 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&core,
1434 	    (int)core.c_hdrsize, (off_t)0,
1435 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1436 out:
1437 	VOP_UNLOCK(vp, 0, p);
1438 	error1 = vn_close(vp, FWRITE, cred, p);
1439 	crfree(cred);
1440 	if (error == 0)
1441 		error = error1;
1442 	return (error);
1443 }
1444 
1445 /*
1446  * Nonexistent system call-- signal process (may want to handle it).
1447  * Flag error in case process won't see signal immediately (blocked or ignored).
1448  */
1449 /* ARGSUSED */
1450 int
1451 sys_nosys(struct proc *p, void *v, register_t *retval)
1452 {
1453 
1454 	psignal(p, SIGSYS);
1455 	return (ENOSYS);
1456 }
1457 
1458 #ifdef RTHREADS
1459 int
1460 sys_thrsigdivert(struct proc *p, void *v, register_t *retval)
1461 {
1462 	struct sys_thrsigdivert_args *uap = v;
1463 
1464 	p->p_sigdivert = SCARG(uap, sigmask);
1465 
1466 	return (0);
1467 }
1468 #endif
1469 
1470 void
1471 initsiginfo(siginfo_t *si, int sig, u_long code, int type, union sigval val)
1472 {
1473 	bzero(si, sizeof *si);
1474 
1475 	si->si_signo = sig;
1476 	si->si_code = type;
1477 	if (type == SI_USER) {
1478 		si->si_value = val;
1479 	} else {
1480 		switch (sig) {
1481 		case SIGSEGV:
1482 		case SIGILL:
1483 		case SIGBUS:
1484 		case SIGFPE:
1485 			si->si_addr = val.sival_ptr;
1486 			si->si_trapno = code;
1487 			break;
1488 		case SIGXFSZ:
1489 			break;
1490 		}
1491 	}
1492 }
1493 
1494 int
1495 filt_sigattach(struct knote *kn)
1496 {
1497 	struct proc *p = curproc;
1498 
1499 	kn->kn_ptr.p_proc = p;
1500 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1501 
1502 	/* XXX lock the proc here while adding to the list? */
1503 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1504 
1505 	return (0);
1506 }
1507 
1508 void
1509 filt_sigdetach(struct knote *kn)
1510 {
1511 	struct proc *p = kn->kn_ptr.p_proc;
1512 
1513 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1514 }
1515 
1516 /*
1517  * signal knotes are shared with proc knotes, so we apply a mask to
1518  * the hint in order to differentiate them from process hints.  This
1519  * could be avoided by using a signal-specific knote list, but probably
1520  * isn't worth the trouble.
1521  */
1522 int
1523 filt_signal(struct knote *kn, long hint)
1524 {
1525 
1526 	if (hint & NOTE_SIGNAL) {
1527 		hint &= ~NOTE_SIGNAL;
1528 
1529 		if (kn->kn_id == hint)
1530 			kn->kn_data++;
1531 	}
1532 	return (kn->kn_data != 0);
1533 }
1534