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