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