xref: /openbsd-src/sys/kern/kern_sig.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: kern_sig.c,v 1.64 2003/07/21 22:44:50 tedu 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(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_sigaltstack(p, v, retval)
511 	struct proc *p;
512 	void *v;
513 	register_t *retval;
514 {
515 	register struct sys_sigaltstack_args /* {
516 		syscallarg(struct sigaltstack *) nss;
517 		syscallarg(struct sigaltstack *) oss;
518 	} */ *uap = v;
519 	struct sigacts *psp;
520 	struct sigaltstack 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) && (error = copyout(&psp->ps_sigstk,
527 	    SCARG(uap, oss), sizeof (struct sigaltstack))))
528 		return (error);
529 	if (SCARG(uap, nss) == NULL)
530 		return (0);
531 	error = copyin(SCARG(uap, nss), &ss, sizeof (ss));
532 	if (error)
533 		return (error);
534 	if (ss.ss_flags & SS_DISABLE) {
535 		if (psp->ps_sigstk.ss_flags & SS_ONSTACK)
536 			return (EINVAL);
537 		psp->ps_flags &= ~SAS_ALTSTACK;
538 		psp->ps_sigstk.ss_flags = ss.ss_flags;
539 		return (0);
540 	}
541 	if (ss.ss_size < MINSIGSTKSZ)
542 		return (ENOMEM);
543 	psp->ps_flags |= SAS_ALTSTACK;
544 	psp->ps_sigstk = ss;
545 	return (0);
546 }
547 
548 /* ARGSUSED */
549 int
550 sys_kill(cp, v, retval)
551 	register struct proc *cp;
552 	void *v;
553 	register_t *retval;
554 {
555 	register struct sys_kill_args /* {
556 		syscallarg(int) pid;
557 		syscallarg(int) signum;
558 	} */ *uap = v;
559 	register struct proc *p;
560 	register struct pcred *pc = cp->p_cred;
561 
562 	if ((u_int)SCARG(uap, signum) >= NSIG)
563 		return (EINVAL);
564 	if (SCARG(uap, pid) > 0) {
565 		/* kill single process */
566 		if ((p = pfind(SCARG(uap, pid))) == NULL)
567 			return (ESRCH);
568 		if (!cansignal(cp, pc, p, SCARG(uap, signum)))
569 			return (EPERM);
570 		if (SCARG(uap, signum))
571 			psignal(p, SCARG(uap, signum));
572 		return (0);
573 	}
574 	switch (SCARG(uap, pid)) {
575 	case -1:		/* broadcast signal */
576 		return (killpg1(cp, SCARG(uap, signum), 0, 1));
577 	case 0:			/* signal own process group */
578 		return (killpg1(cp, SCARG(uap, signum), 0, 0));
579 	default:		/* negative explicit process group */
580 		return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
581 	}
582 	/* NOTREACHED */
583 }
584 
585 /*
586  * Common code for kill process group/broadcast kill.
587  * cp is calling process.
588  */
589 int
590 killpg1(cp, signum, pgid, all)
591 	register struct proc *cp;
592 	int signum, pgid, all;
593 {
594 	register struct proc *p;
595 	register struct pcred *pc = cp->p_cred;
596 	struct pgrp *pgrp;
597 	int nfound = 0;
598 
599 	if (all)
600 		/*
601 		 * broadcast
602 		 */
603 		for (p = LIST_FIRST(&allproc); p; p = LIST_NEXT(p, p_list)) {
604 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
605 			    p == cp || !cansignal(cp, pc, p, signum))
606 				continue;
607 			nfound++;
608 			if (signum)
609 				psignal(p, signum);
610 		}
611 	else {
612 		if (pgid == 0)
613 			/*
614 			 * zero pgid means send to my process group.
615 			 */
616 			pgrp = cp->p_pgrp;
617 		else {
618 			pgrp = pgfind(pgid);
619 			if (pgrp == NULL)
620 				return (ESRCH);
621 		}
622 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
623 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
624 			    !cansignal(cp, pc, p, signum))
625 				continue;
626 			nfound++;
627 			if (signum && P_ZOMBIE(p) == 0)
628 				psignal(p, signum);
629 		}
630 	}
631 	return (nfound ? 0 : ESRCH);
632 }
633 
634 #define CANDELIVER(uid, euid, p) \
635 	(euid == 0 || \
636 	(uid) == (p)->p_cred->p_ruid || \
637 	(uid) == (p)->p_cred->p_svuid || \
638 	(uid) == (p)->p_ucred->cr_uid || \
639 	(euid) == (p)->p_cred->p_ruid || \
640 	(euid) == (p)->p_cred->p_svuid || \
641 	(euid) == (p)->p_ucred->cr_uid)
642 
643 /*
644  * Deliver signum to pgid, but first check uid/euid against each
645  * process and see if it is permitted.
646  */
647 void
648 csignal(pgid, signum, uid, euid)
649 	pid_t pgid;
650 	int signum;
651 	uid_t uid, euid;
652 {
653 	struct pgrp *pgrp;
654 	struct proc *p;
655 
656 	if (pgid == 0)
657 		return;
658 	if (pgid < 0) {
659 		pgid = -pgid;
660 		if ((pgrp = pgfind(pgid)) == NULL)
661 			return;
662 		for (p = pgrp->pg_members.lh_first; p;
663 		    p = p->p_pglist.le_next)
664 			if (CANDELIVER(uid, euid, p))
665 				psignal(p, signum);
666 	} else {
667 		if ((p = pfind(pgid)) == NULL)
668 			return;
669 		if (CANDELIVER(uid, euid, p))
670 			psignal(p, signum);
671 	}
672 }
673 
674 /*
675  * Send a signal to a process group.
676  */
677 void
678 gsignal(pgid, signum)
679 	int pgid, signum;
680 {
681 	struct pgrp *pgrp;
682 
683 	if (pgid && (pgrp = pgfind(pgid)))
684 		pgsignal(pgrp, signum, 0);
685 }
686 
687 /*
688  * Send a signal to a process group.  If checktty is 1,
689  * limit to members which have a controlling terminal.
690  */
691 void
692 pgsignal(pgrp, signum, checkctty)
693 	struct pgrp *pgrp;
694 	int signum, checkctty;
695 {
696 	register struct proc *p;
697 
698 	if (pgrp)
699 		for (p = pgrp->pg_members.lh_first; p != 0; p = p->p_pglist.le_next)
700 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
701 				psignal(p, signum);
702 }
703 
704 /*
705  * Send a signal caused by a trap to the current process.
706  * If it will be caught immediately, deliver it with correct code.
707  * Otherwise, post it normally.
708  */
709 void
710 trapsignal(p, signum, code, type, sigval)
711 	struct proc *p;
712 	register int signum;
713 	u_long code;
714 	int type;
715 	union sigval sigval;
716 {
717 	register struct sigacts *ps = p->p_sigacts;
718 	int mask;
719 
720 #ifdef KTRACE
721 	if (KTRPOINT(p, KTR_PSIG)) {
722 		siginfo_t si;
723 
724 		initsiginfo(&si, signum, code, type, sigval);
725 		ktrpsig(p, signum, ps->ps_sigact[signum],
726 		    p->p_sigmask, code, &si);
727 	}
728 #endif
729 	mask = sigmask(signum);
730 	if ((p->p_flag & P_TRACED) == 0 && (p->p_sigcatch & mask) != 0 &&
731 	    (p->p_sigmask & mask) == 0) {
732 		p->p_stats->p_ru.ru_nsignals++;
733 		(*p->p_emul->e_sendsig)(ps->ps_sigact[signum], signum,
734 		    p->p_sigmask, code, type, sigval);
735 		p->p_sigmask |= ps->ps_catchmask[signum];
736 		if ((ps->ps_sigreset & mask) != 0) {
737 			p->p_sigcatch &= ~mask;
738 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
739 				p->p_sigignore |= mask;
740 			ps->ps_sigact[signum] = SIG_DFL;
741 		}
742 	} else {
743 		ps->ps_code = code;	/* XXX for core dump/debugger */
744 		psignal(p, signum);
745 	}
746 }
747 
748 /*
749  * Send the signal to the process.  If the signal has an action, the action
750  * is usually performed by the target process rather than the caller; we add
751  * the signal to the set of pending signals for the process.
752  *
753  * Exceptions:
754  *   o When a stop signal is sent to a sleeping process that takes the
755  *     default action, the process is stopped without awakening it.
756  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
757  *     regardless of the signal action (eg, blocked or ignored).
758  *
759  * Other ignored signals are discarded immediately.
760  */
761 void
762 psignal(p, signum)
763 	register struct proc *p;
764 	register int signum;
765 {
766 	register int s, prop;
767 	register sig_t action;
768 	int mask;
769 
770 	if ((u_int)signum >= NSIG || signum == 0)
771 		panic("psignal signal number");
772 
773 	/* Ignore signal if we are exiting */
774 	if (p->p_flag & P_WEXIT)
775 		return;
776 
777 	KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
778 
779 	mask = sigmask(signum);
780 	prop = sigprop[signum];
781 
782 	/*
783 	 * If proc is traced, always give parent a chance.
784 	 */
785 	if (p->p_flag & P_TRACED)
786 		action = SIG_DFL;
787 	else {
788 		/*
789 		 * If the signal is being ignored,
790 		 * then we forget about it immediately.
791 		 * (Note: we don't set SIGCONT in p_sigignore,
792 		 * and if it is set to SIG_IGN,
793 		 * action will be SIG_DFL here.)
794 		 */
795 		if (p->p_sigignore & mask)
796 			return;
797 		if (p->p_sigmask & mask)
798 			action = SIG_HOLD;
799 		else if (p->p_sigcatch & mask)
800 			action = SIG_CATCH;
801 		else {
802 			action = SIG_DFL;
803 
804 			if (prop & SA_KILL && p->p_nice > NZERO)
805 				p->p_nice = NZERO;
806 
807 			/*
808 			 * If sending a tty stop signal to a member of an
809 			 * orphaned process group, discard the signal here if
810 			 * the action is default; don't stop the process below
811 			 * if sleeping, and don't clear any pending SIGCONT.
812 			 */
813 			if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
814 				return;
815 		}
816 	}
817 
818 	if (prop & SA_CONT)
819 		p->p_siglist &= ~stopsigmask;
820 
821 	if (prop & SA_STOP)
822 		p->p_siglist &= ~contsigmask;
823 
824 	p->p_siglist |= mask;
825 
826 	/*
827 	 * Defer further processing for signals which are held,
828 	 * except that stopped processes must be continued by SIGCONT.
829 	 */
830 	if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
831 		return;
832 	s = splhigh();
833 	switch (p->p_stat) {
834 
835 	case SSLEEP:
836 		/*
837 		 * If process is sleeping uninterruptibly
838 		 * we can't interrupt the sleep... the signal will
839 		 * be noticed when the process returns through
840 		 * trap() or syscall().
841 		 */
842 		if ((p->p_flag & P_SINTR) == 0)
843 			goto out;
844 		/*
845 		 * Process is sleeping and traced... make it runnable
846 		 * so it can discover the signal in issignal() and stop
847 		 * for the parent.
848 		 */
849 		if (p->p_flag & P_TRACED)
850 			goto run;
851 		/*
852 		 * If SIGCONT is default (or ignored) and process is
853 		 * asleep, we are finished; the process should not
854 		 * be awakened.
855 		 */
856 		if ((prop & SA_CONT) && action == SIG_DFL) {
857 			p->p_siglist &= ~mask;
858 			goto out;
859 		}
860 		/*
861 		 * When a sleeping process receives a stop
862 		 * signal, process immediately if possible.
863 		 */
864 		if ((prop & SA_STOP) && action == SIG_DFL) {
865 			/*
866 			 * If a child holding parent blocked,
867 			 * stopping could cause deadlock.
868 			 */
869 			if (p->p_flag & P_PPWAIT)
870 				goto out;
871 			p->p_siglist &= ~mask;
872 			p->p_xstat = signum;
873 			if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
874 				psignal(p->p_pptr, SIGCHLD);
875 			proc_stop(p);
876 			goto out;
877 		}
878 		/*
879 		 * All other (caught or default) signals
880 		 * cause the process to run.
881 		 */
882 		goto runfast;
883 		/*NOTREACHED*/
884 
885 	case SSTOP:
886 		/*
887 		 * If traced process is already stopped,
888 		 * then no further action is necessary.
889 		 */
890 		if (p->p_flag & P_TRACED)
891 			goto out;
892 
893 		/*
894 		 * Kill signal always sets processes running.
895 		 */
896 		if (signum == SIGKILL)
897 			goto runfast;
898 
899 		if (prop & SA_CONT) {
900 			/*
901 			 * If SIGCONT is default (or ignored), we continue the
902 			 * process but don't leave the signal in p_siglist, as
903 			 * it has no further action.  If SIGCONT is held, we
904 			 * continue the process and leave the signal in
905 			 * p_siglist.  If the process catches SIGCONT, let it
906 			 * handle the signal itself.  If it isn't waiting on
907 			 * an event, then it goes back to run state.
908 			 * Otherwise, process goes back to sleep state.
909 			 */
910 			if (action == SIG_DFL)
911 				p->p_siglist &= ~mask;
912 			if (action == SIG_CATCH)
913 				goto runfast;
914 			if (p->p_wchan == 0)
915 				goto run;
916 			p->p_stat = SSLEEP;
917 			goto out;
918 		}
919 
920 		if (prop & SA_STOP) {
921 			/*
922 			 * Already stopped, don't need to stop again.
923 			 * (If we did the shell could get confused.)
924 			 */
925 			p->p_siglist &= ~mask;		/* take it away */
926 			goto out;
927 		}
928 
929 		/*
930 		 * If process is sleeping interruptibly, then simulate a
931 		 * wakeup so that when it is continued, it will be made
932 		 * runnable and can look at the signal.  But don't make
933 		 * the process runnable, leave it stopped.
934 		 */
935 		if (p->p_wchan && p->p_flag & P_SINTR)
936 			unsleep(p);
937 		goto out;
938 
939 	default:
940 		/*
941 		 * SRUN, SIDL, SZOMB do nothing with the signal,
942 		 * other than kicking ourselves if we are running.
943 		 * It will either never be noticed, or noticed very soon.
944 		 */
945 		if (p == curproc)
946 			signotify(p);
947 		goto out;
948 	}
949 	/*NOTREACHED*/
950 
951 runfast:
952 	/*
953 	 * Raise priority to at least PUSER.
954 	 */
955 	if (p->p_priority > PUSER)
956 		p->p_priority = PUSER;
957 run:
958 	setrunnable(p);
959 out:
960 	splx(s);
961 }
962 
963 /*
964  * If the current process has received a signal (should be caught or cause
965  * termination, should interrupt current syscall), return the signal number.
966  * Stop signals with default action are processed immediately, then cleared;
967  * they aren't returned.  This is checked after each entry to the system for
968  * a syscall or trap (though this can usually be done without calling issignal
969  * by checking the pending signal masks in the CURSIG macro.) The normal call
970  * sequence is
971  *
972  *	while (signum = CURSIG(curproc))
973  *		postsig(signum);
974  */
975 int
976 issignal(struct proc *p)
977 {
978 	int signum, mask, prop;
979 	int s;
980 
981 	for (;;) {
982 		mask = p->p_siglist & ~p->p_sigmask;
983 		if (p->p_flag & P_PPWAIT)
984 			mask &= ~stopsigmask;
985 		if (mask == 0)	 	/* no signal to send */
986 			return (0);
987 		signum = ffs((long)mask);
988 		mask = sigmask(signum);
989 		p->p_siglist &= ~mask;		/* take the signal! */
990 
991 		/*
992 		 * We should see pending but ignored signals
993 		 * only if P_TRACED was on when they were posted.
994 		 */
995 		if (mask & p->p_sigignore && (p->p_flag & P_TRACED) == 0)
996 			continue;
997 
998 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
999 			/*
1000 			 * If traced, always stop, and stay
1001 			 * stopped until released by the debugger.
1002 			 */
1003 			p->p_xstat = signum;
1004 
1005 			s = splstatclock();	/* protect mi_switch */
1006 			if (p->p_flag & P_FSTRACE) {
1007 #ifdef	PROCFS
1008 				/* procfs debugging */
1009 				p->p_stat = SSTOP;
1010 				wakeup(p);
1011 				mi_switch();
1012 #else
1013 				panic("procfs debugging");
1014 #endif
1015 			} else {
1016 				/* ptrace debugging */
1017 				psignal(p->p_pptr, SIGCHLD);
1018 				proc_stop(p);
1019 				mi_switch();
1020 			}
1021 			splx(s);
1022 
1023 			/*
1024 			 * If we are no longer being traced, or the parent
1025 			 * didn't give us a signal, look for more signals.
1026 			 */
1027 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1028 				continue;
1029 
1030 			/*
1031 			 * If the new signal is being masked, look for other
1032 			 * signals.
1033 			 */
1034 			signum = p->p_xstat;
1035 			mask = sigmask(signum);
1036 			if ((p->p_sigmask & mask) != 0)
1037 				continue;
1038 			p->p_siglist &= ~mask;		/* take the signal! */
1039 		}
1040 
1041 		prop = sigprop[signum];
1042 
1043 		/*
1044 		 * Decide whether the signal should be returned.
1045 		 * Return the signal's number, or fall through
1046 		 * to clear it from the pending mask.
1047 		 */
1048 		switch ((long)p->p_sigacts->ps_sigact[signum]) {
1049 
1050 		case (long)SIG_DFL:
1051 			/*
1052 			 * Don't take default actions on system processes.
1053 			 */
1054 			if (p->p_pid <= 1) {
1055 #ifdef DIAGNOSTIC
1056 				/*
1057 				 * Are you sure you want to ignore SIGSEGV
1058 				 * in init? XXX
1059 				 */
1060 				printf("Process (pid %d) got signal %d\n",
1061 				    p->p_pid, signum);
1062 #endif
1063 				break;		/* == ignore */
1064 			}
1065 			/*
1066 			 * If there is a pending stop signal to process
1067 			 * with default action, stop here,
1068 			 * then clear the signal.  However,
1069 			 * if process is member of an orphaned
1070 			 * process group, ignore tty stop signals.
1071 			 */
1072 			if (prop & SA_STOP) {
1073 				if (p->p_flag & P_TRACED ||
1074 		    		    (p->p_pgrp->pg_jobc == 0 &&
1075 				    prop & SA_TTYSTOP))
1076 					break;	/* == ignore */
1077 				p->p_xstat = signum;
1078 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1079 					psignal(p->p_pptr, SIGCHLD);
1080 				proc_stop(p);
1081 				s = splstatclock();
1082 				mi_switch();
1083 				splx(s);
1084 				break;
1085 			} else if (prop & SA_IGNORE) {
1086 				/*
1087 				 * Except for SIGCONT, shouldn't get here.
1088 				 * Default action is to ignore; drop it.
1089 				 */
1090 				break;		/* == ignore */
1091 			} else
1092 				goto keep;
1093 			/*NOTREACHED*/
1094 
1095 		case (long)SIG_IGN:
1096 			/*
1097 			 * Masking above should prevent us ever trying
1098 			 * to take action on an ignored signal other
1099 			 * than SIGCONT, unless process is traced.
1100 			 */
1101 			if ((prop & SA_CONT) == 0 &&
1102 			    (p->p_flag & P_TRACED) == 0)
1103 				printf("issignal\n");
1104 			break;		/* == ignore */
1105 
1106 		default:
1107 			/*
1108 			 * This signal has an action, let
1109 			 * postsig() process it.
1110 			 */
1111 			goto keep;
1112 		}
1113 	}
1114 	/* NOTREACHED */
1115 
1116 keep:
1117 	p->p_siglist |= mask;		/* leave the signal for later */
1118 	return (signum);
1119 }
1120 
1121 /*
1122  * Put the argument process into the stopped state and notify the parent
1123  * via wakeup.  Signals are handled elsewhere.  The process must not be
1124  * on the run queue.
1125  */
1126 void
1127 proc_stop(p)
1128 	struct proc *p;
1129 {
1130 
1131 	p->p_stat = SSTOP;
1132 	p->p_flag &= ~P_WAITED;
1133 	wakeup(p->p_pptr);
1134 }
1135 
1136 /*
1137  * Take the action for the specified signal
1138  * from the current set of pending signals.
1139  */
1140 void
1141 postsig(signum)
1142 	register int signum;
1143 {
1144 	struct proc *p = curproc;
1145 	struct sigacts *ps = p->p_sigacts;
1146 	sig_t action;
1147 	u_long code;
1148 	int mask, returnmask;
1149 	union sigval null_sigval;
1150 	int s;
1151 
1152 #ifdef DIAGNOSTIC
1153 	if (signum == 0)
1154 		panic("postsig");
1155 #endif
1156 	mask = sigmask(signum);
1157 	p->p_siglist &= ~mask;
1158 	action = ps->ps_sigact[signum];
1159 
1160 	if (ps->ps_sig != signum) {
1161 		code = 0;
1162 	} else {
1163 		code = ps->ps_code;
1164 	}
1165 
1166 #ifdef KTRACE
1167 	if (KTRPOINT(p, KTR_PSIG)) {
1168 		siginfo_t si;
1169 
1170 		null_sigval.sival_ptr = 0;
1171 		initsiginfo(&si, signum, 0, SI_USER, null_sigval);
1172 		ktrpsig(p, signum, action, ps->ps_flags & SAS_OLDMASK ?
1173 		    ps->ps_oldmask : p->p_sigmask, code, &si);
1174 	}
1175 #endif
1176 	if (action == SIG_DFL) {
1177 		/*
1178 		 * Default action, where the default is to kill
1179 		 * the process.  (Other cases were ignored above.)
1180 		 */
1181 		sigexit(p, signum);
1182 		/* NOTREACHED */
1183 	} else {
1184 		/*
1185 		 * If we get here, the signal must be caught.
1186 		 */
1187 #ifdef DIAGNOSTIC
1188 		if (action == SIG_IGN || (p->p_sigmask & mask))
1189 			panic("postsig action");
1190 #endif
1191 		/*
1192 		 * Set the new mask value and also defer further
1193 		 * occurences of this signal.
1194 		 *
1195 		 * Special case: user has done a sigpause.  Here the
1196 		 * current mask is not of interest, but rather the
1197 		 * mask from before the sigpause is what we want
1198 		 * restored after the signal processing is completed.
1199 		 */
1200 		s = splhigh();
1201 		if (ps->ps_flags & SAS_OLDMASK) {
1202 			returnmask = ps->ps_oldmask;
1203 			ps->ps_flags &= ~SAS_OLDMASK;
1204 		} else
1205 			returnmask = p->p_sigmask;
1206 		p->p_sigmask |= ps->ps_catchmask[signum];
1207 		if ((ps->ps_sigreset & mask) != 0) {
1208 			p->p_sigcatch &= ~mask;
1209 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1210 				p->p_sigignore |= mask;
1211 			ps->ps_sigact[signum] = SIG_DFL;
1212 		}
1213 		splx(s);
1214 		p->p_stats->p_ru.ru_nsignals++;
1215 		if (ps->ps_sig == signum) {
1216 			ps->ps_code = 0;
1217 		}
1218 		null_sigval.sival_ptr = 0;
1219 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code,
1220 		    SI_USER, null_sigval);
1221 	}
1222 }
1223 
1224 /*
1225  * Kill the current process for stated reason.
1226  */
1227 void
1228 killproc(p, why)
1229 	struct proc *p;
1230 	char *why;
1231 {
1232 
1233 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1234 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1235 	psignal(p, SIGKILL);
1236 }
1237 
1238 /*
1239  * Force the current process to exit with the specified signal, dumping core
1240  * if appropriate.  We bypass the normal tests for masked and caught signals,
1241  * allowing unrecoverable failures to terminate the process without changing
1242  * signal state.  Mark the accounting record with the signal termination.
1243  * If dumping core, save the signal number for the debugger.  Calls exit and
1244  * does not return.
1245  */
1246 void
1247 sigexit(p, signum)
1248 	register struct proc *p;
1249 	int signum;
1250 {
1251 
1252 	/* Mark process as going away */
1253 	p->p_flag |= P_WEXIT;
1254 
1255 	p->p_acflag |= AXSIG;
1256 	if (sigprop[signum] & SA_CORE) {
1257 		p->p_sigacts->ps_sig = signum;
1258 		if (coredump(p) == 0)
1259 			signum |= WCOREFLAG;
1260 	}
1261 	exit1(p, W_EXITCODE(0, signum));
1262 	/* NOTREACHED */
1263 }
1264 
1265 int nosuidcoredump = 1;
1266 
1267 /*
1268  * Dump core, into a file named "progname.core", unless the process was
1269  * setuid/setgid.
1270  */
1271 int
1272 coredump(p)
1273 	register struct proc *p;
1274 {
1275 	register struct vnode *vp;
1276 	register struct ucred *cred = p->p_ucred;
1277 	register struct vmspace *vm = p->p_vmspace;
1278 	struct nameidata nd;
1279 	struct vattr vattr;
1280 	int error, error1;
1281 	char name[MAXCOMLEN+6];		/* progname.core */
1282 	struct core core;
1283 
1284 	/*
1285 	 * Don't dump if not root and the process has used set user or
1286 	 * group privileges.
1287 	 */
1288 	if ((p->p_flag & P_SUGID) &&
1289 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
1290 		return (error);
1291 	if ((p->p_flag & P_SUGID) && nosuidcoredump)
1292 		return (EPERM);
1293 
1294 	/* Don't dump if will exceed file size limit. */
1295 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1296 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1297 		return (EFBIG);
1298 
1299 	/*
1300 	 * ... but actually write it as UID
1301 	 */
1302 	cred = crdup(cred);
1303 	cred->cr_uid = p->p_cred->p_ruid;
1304 	cred->cr_gid = p->p_cred->p_rgid;
1305 
1306 	snprintf(name, sizeof name, "%s.core", p->p_comm);
1307 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1308 
1309 	error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1310 
1311 	if (error) {
1312 		crfree(cred);
1313 		return (error);
1314 	}
1315 
1316 	/*
1317 	 * Don't dump to non-regular files, files with links, or files
1318 	 * owned by someone else.
1319 	 */
1320 	vp = nd.ni_vp;
1321 	if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0)
1322 		goto out;
1323 	/* Don't dump to non-regular files or files with links. */
1324 	if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1325 	    vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6)) {
1326 		error = EACCES;
1327 		goto out;
1328 	}
1329 	VATTR_NULL(&vattr);
1330 	vattr.va_size = 0;
1331 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1332 	VOP_SETATTR(vp, &vattr, cred, p);
1333 	p->p_acflag |= ACORE;
1334 	bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
1335 	fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
1336 
1337 	core.c_midmag = 0;
1338 	strncpy(core.c_name, p->p_comm, MAXCOMLEN);
1339 	core.c_nseg = 0;
1340 	core.c_signo = p->p_sigacts->ps_sig;
1341 	core.c_ucode = p->p_sigacts->ps_code;
1342 	core.c_cpusize = 0;
1343 	core.c_tsize = (u_long)ctob(vm->vm_tsize);
1344 	core.c_dsize = (u_long)ctob(vm->vm_dsize);
1345 	core.c_ssize = (u_long)round_page(ctob(vm->vm_ssize));
1346 	error = cpu_coredump(p, vp, cred, &core);
1347 	if (error)
1348 		goto out;
1349 	if (core.c_midmag == 0) {
1350 		/* XXX
1351 		 * cpu_coredump() didn't bother to set the magic; assume
1352 		 * this is a request to do a traditional dump. cpu_coredump()
1353 		 * is still responsible for setting sensible values in
1354 		 * the core header.
1355 		 */
1356 		if (core.c_cpusize == 0)
1357 			core.c_cpusize = USPACE; /* Just in case */
1358 		error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
1359 		    (int)core.c_dsize,
1360 		    (off_t)core.c_cpusize, UIO_USERSPACE,
1361 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1362 		if (error)
1363 			goto out;
1364 		error = vn_rdwr(UIO_WRITE, vp,
1365 #ifdef MACHINE_STACK_GROWS_UP
1366 		    (caddr_t) USRSTACK,
1367 #else
1368 		    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1369 #endif
1370 		    core.c_ssize,
1371 		    (off_t)(core.c_cpusize + core.c_dsize), UIO_USERSPACE,
1372 		    IO_NODELOCKED|IO_UNIT, cred, NULL, p);
1373 	} else {
1374 		/*
1375 		 * vm_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 	}
1385 out:
1386 	VOP_UNLOCK(vp, 0, p);
1387 	error1 = vn_close(vp, FWRITE, cred, p);
1388 	crfree(cred);
1389 	if (error == 0)
1390 		error = error1;
1391 	return (error);
1392 }
1393 
1394 /*
1395  * Nonexistent system call-- signal process (may want to handle it).
1396  * Flag error in case process won't see signal immediately (blocked or ignored).
1397  */
1398 /* ARGSUSED */
1399 int
1400 sys_nosys(p, v, retval)
1401 	struct proc *p;
1402 	void *v;
1403 	register_t *retval;
1404 {
1405 
1406 	psignal(p, SIGSYS);
1407 	return (ENOSYS);
1408 }
1409 
1410 void
1411 initsiginfo(si, sig, code, type, val)
1412 	siginfo_t *si;
1413 	int sig;
1414 	u_long code;
1415 	int type;
1416 	union sigval val;
1417 {
1418 	bzero(si, sizeof *si);
1419 
1420 	si->si_signo = sig;
1421 	si->si_code = type;
1422 	if (type == SI_USER) {
1423 		si->si_value = val;
1424 	} else {
1425 		switch (sig) {
1426 		case SIGSEGV:
1427 		case SIGILL:
1428 		case SIGBUS:
1429 		case SIGFPE:
1430 			si->si_addr = val.sival_ptr;
1431 			si->si_trapno = code;
1432 			break;
1433 		case SIGXFSZ:
1434 			break;
1435 		}
1436 	}
1437 }
1438 
1439 int
1440 filt_sigattach(struct knote *kn)
1441 {
1442 	struct proc *p = curproc;
1443 
1444 	kn->kn_ptr.p_proc = p;
1445 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
1446 
1447 	/* XXX lock the proc here while adding to the list? */
1448 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1449 
1450 	return (0);
1451 }
1452 
1453 void
1454 filt_sigdetach(struct knote *kn)
1455 {
1456 	struct proc *p = kn->kn_ptr.p_proc;
1457 
1458 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1459 }
1460 
1461 /*
1462  * signal knotes are shared with proc knotes, so we apply a mask to
1463  * the hint in order to differentiate them from process hints.  This
1464  * could be avoided by using a signal-specific knote list, but probably
1465  * isn't worth the trouble.
1466  */
1467 int
1468 filt_signal(struct knote *kn, long hint)
1469 {
1470 
1471 	if (hint & NOTE_SIGNAL) {
1472 		hint &= ~NOTE_SIGNAL;
1473 
1474 		if (kn->kn_id == hint)
1475 			kn->kn_data++;
1476 	}
1477 	return (kn->kn_data != 0);
1478 }
1479