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