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