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