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