xref: /netbsd/sys/kern/kern_sig.c (revision bf9ec67e)
1 /*	$NetBSD: kern_sig.c,v 1.120 2002/03/08 20:48:40 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.120 2002/03/08 20:48:40 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 	    &pool_allocator_nointr);
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 	int		dolock = (p->p_flag & P_SINTR) == 0, locked = !dolock;
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 			if (locked && dolock)
1018 				SCHED_LOCK(s);
1019 			return (0);
1020 		}
1021 							/* take the signal! */
1022 		sigdelset(&p->p_sigctx.ps_siglist, signum);
1023 
1024 		/*
1025 		 * We should see pending but ignored signals
1026 		 * only if P_TRACED was on when they were posted.
1027 		 */
1028 		if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1029 		    (p->p_flag & P_TRACED) == 0)
1030 			continue;
1031 
1032 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1033 			/*
1034 			 * If traced, always stop, and stay
1035 			 * stopped until released by the debugger.
1036 			 */
1037 			p->p_xstat = signum;
1038 			if ((p->p_flag & P_FSTRACE) == 0)
1039 				psignal1(p->p_pptr, SIGCHLD, dolock);
1040 			if (dolock)
1041 				SCHED_LOCK(s);
1042 			proc_stop(p);
1043 			mi_switch(p);
1044 			SCHED_ASSERT_UNLOCKED();
1045 			if (dolock)
1046 				splx(s);
1047 			else
1048 				dolock = 1;
1049 
1050 			/*
1051 			 * If we are no longer being traced, or the parent
1052 			 * didn't give us a signal, look for more signals.
1053 			 */
1054 			if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1055 				continue;
1056 
1057 			/*
1058 			 * If the new signal is being masked, look for other
1059 			 * signals.
1060 			 */
1061 			signum = p->p_xstat;
1062 			/*
1063 			 * `p->p_sigctx.ps_siglist |= mask' is done
1064 			 * in setrunnable().
1065 			 */
1066 			if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1067 				continue;
1068 							/* take the signal! */
1069 			sigdelset(&p->p_sigctx.ps_siglist, signum);
1070 		}
1071 
1072 		prop = sigprop[signum];
1073 
1074 		/*
1075 		 * Decide whether the signal should be returned.
1076 		 * Return the signal's number, or fall through
1077 		 * to clear it from the pending mask.
1078 		 */
1079 		switch ((long)SIGACTION(p, signum).sa_handler) {
1080 
1081 		case (long)SIG_DFL:
1082 			/*
1083 			 * Don't take default actions on system processes.
1084 			 */
1085 			if (p->p_pid <= 1) {
1086 #ifdef DIAGNOSTIC
1087 				/*
1088 				 * Are you sure you want to ignore SIGSEGV
1089 				 * in init? XXX
1090 				 */
1091 				printf("Process (pid %d) got signal %d\n",
1092 				    p->p_pid, signum);
1093 #endif
1094 				break;		/* == ignore */
1095 			}
1096 			/*
1097 			 * If there is a pending stop signal to process
1098 			 * with default action, stop here,
1099 			 * then clear the signal.  However,
1100 			 * if process is member of an orphaned
1101 			 * process group, ignore tty stop signals.
1102 			 */
1103 			if (prop & SA_STOP) {
1104 				if (p->p_flag & P_TRACED ||
1105 		    		    (p->p_pgrp->pg_jobc == 0 &&
1106 				    prop & SA_TTYSTOP))
1107 					break;	/* == ignore */
1108 				p->p_xstat = signum;
1109 				if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1110 					psignal1(p->p_pptr, SIGCHLD, dolock);
1111 				if (dolock)
1112 					SCHED_LOCK(s);
1113 				proc_stop(p);
1114 				mi_switch(p);
1115 				SCHED_ASSERT_UNLOCKED();
1116 				if (dolock)
1117 					splx(s);
1118 				else
1119 					dolock = 1;
1120 				break;
1121 			} else if (prop & SA_IGNORE) {
1122 				/*
1123 				 * Except for SIGCONT, shouldn't get here.
1124 				 * Default action is to ignore; drop it.
1125 				 */
1126 				break;		/* == ignore */
1127 			} else
1128 				goto keep;
1129 			/*NOTREACHED*/
1130 
1131 		case (long)SIG_IGN:
1132 			/*
1133 			 * Masking above should prevent us ever trying
1134 			 * to take action on an ignored signal other
1135 			 * than SIGCONT, unless process is traced.
1136 			 */
1137 			if ((prop & SA_CONT) == 0 &&
1138 			    (p->p_flag & P_TRACED) == 0)
1139 				printf("issignal\n");
1140 			break;		/* == ignore */
1141 
1142 		default:
1143 			/*
1144 			 * This signal has an action, let
1145 			 * postsig() process it.
1146 			 */
1147 			goto keep;
1148 		}
1149 	}
1150 	/* NOTREACHED */
1151 
1152  keep:
1153 						/* leave the signal for later */
1154 	sigaddset(&p->p_sigctx.ps_siglist, signum);
1155 	CHECKSIGS(p);
1156 	if (locked && dolock)
1157 		SCHED_LOCK(s);
1158 	return (signum);
1159 }
1160 
1161 /*
1162  * Put the argument process into the stopped state and notify the parent
1163  * via wakeup.  Signals are handled elsewhere.  The process must not be
1164  * on the run queue.
1165  */
1166 static void
1167 proc_stop(struct proc *p)
1168 {
1169 
1170 	SCHED_ASSERT_LOCKED();
1171 
1172 	p->p_stat = SSTOP;
1173 	p->p_flag &= ~P_WAITED;
1174 	sched_wakeup((caddr_t)p->p_pptr);
1175 }
1176 
1177 /*
1178  * Take the action for the specified signal
1179  * from the current set of pending signals.
1180  */
1181 void
1182 postsig(int signum)
1183 {
1184 	struct proc	*p;
1185 	struct sigacts	*ps;
1186 	sig_t		action;
1187 	u_long		code;
1188 	sigset_t	*returnmask;
1189 
1190 	p = curproc;
1191 	ps = p->p_sigacts;
1192 #ifdef DIAGNOSTIC
1193 	if (signum == 0)
1194 		panic("postsig");
1195 #endif
1196 
1197 	KERNEL_PROC_LOCK(p);
1198 
1199 	sigdelset(&p->p_sigctx.ps_siglist, signum);
1200 	action = SIGACTION_PS(ps, signum).sa_handler;
1201 #ifdef KTRACE
1202 	if (KTRPOINT(p, KTR_PSIG))
1203 		ktrpsig(p,
1204 		    signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1205 		    &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1206 #endif
1207 	if (action == SIG_DFL) {
1208 		/*
1209 		 * Default action, where the default is to kill
1210 		 * the process.  (Other cases were ignored above.)
1211 		 */
1212 		sigexit(p, signum);
1213 		/* NOTREACHED */
1214 	} else {
1215 		/*
1216 		 * If we get here, the signal must be caught.
1217 		 */
1218 #ifdef DIAGNOSTIC
1219 		if (action == SIG_IGN ||
1220 		    sigismember(&p->p_sigctx.ps_sigmask, signum))
1221 			panic("postsig action");
1222 #endif
1223 		/*
1224 		 * Set the new mask value and also defer further
1225 		 * occurences of this signal.
1226 		 *
1227 		 * Special case: user has done a sigpause.  Here the
1228 		 * current mask is not of interest, but rather the
1229 		 * mask from before the sigpause is what we want
1230 		 * restored after the signal processing is completed.
1231 		 */
1232 		if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1233 			returnmask = &p->p_sigctx.ps_oldmask;
1234 			p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1235 		} else
1236 			returnmask = &p->p_sigctx.ps_sigmask;
1237 		p->p_stats->p_ru.ru_nsignals++;
1238 		if (p->p_sigctx.ps_sig != signum) {
1239 			code = 0;
1240 		} else {
1241 			code = p->p_sigctx.ps_code;
1242 			p->p_sigctx.ps_code = 0;
1243 			p->p_sigctx.ps_sig = 0;
1244 		}
1245 		(*p->p_emul->e_sendsig)(action, signum, returnmask, code);
1246 		(void) splsched();	/* XXXSMP */
1247 		sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1248 		    &p->p_sigctx.ps_sigmask);
1249 		if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1250 			sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1251 			if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1252 				sigaddset(&p->p_sigctx.ps_sigignore, signum);
1253 			SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1254 		}
1255 		(void) spl0();		/* XXXSMP */
1256 	}
1257 
1258 	KERNEL_PROC_UNLOCK(p);
1259 }
1260 
1261 /*
1262  * Kill the current process for stated reason.
1263  */
1264 void
1265 killproc(struct proc *p, char *why)
1266 {
1267 
1268 	log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1269 	uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1270 	psignal(p, SIGKILL);
1271 }
1272 
1273 /*
1274  * Force the current process to exit with the specified signal, dumping core
1275  * if appropriate.  We bypass the normal tests for masked and caught signals,
1276  * allowing unrecoverable failures to terminate the process without changing
1277  * signal state.  Mark the accounting record with the signal termination.
1278  * If dumping core, save the signal number for the debugger.  Calls exit and
1279  * does not return.
1280  */
1281 
1282 #if defined(DEBUG)
1283 int	kern_logsigexit = 1;	/* not static to make public for sysctl */
1284 #else
1285 int	kern_logsigexit = 0;	/* not static to make public for sysctl */
1286 #endif
1287 
1288 static	const char logcoredump[] =
1289 	"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1290 static	const char lognocoredump[] =
1291 	"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1292 
1293 void
1294 sigexit(struct proc *p, int signum)
1295 {
1296 	int	error, exitsig;
1297 
1298 	exitsig = signum;
1299 	p->p_acflag |= AXSIG;
1300 	if (sigprop[signum] & SA_CORE) {
1301 		p->p_sigctx.ps_sig = signum;
1302 		if ((error = coredump(p)) == 0)
1303 			exitsig |= WCOREFLAG;
1304 
1305 		if (kern_logsigexit) {
1306 			int uid = p->p_cred && p->p_ucred ?
1307 				p->p_ucred->cr_uid : -1;
1308 
1309 			if (error)
1310 				log(LOG_INFO, lognocoredump, p->p_pid,
1311 				    p->p_comm, uid, signum, error);
1312 			else
1313 				log(LOG_INFO, logcoredump, p->p_pid,
1314 				    p->p_comm, uid, signum);
1315 		}
1316 
1317 	}
1318 
1319 	exit1(p, W_EXITCODE(0, exitsig));
1320 	/* NOTREACHED */
1321 }
1322 
1323 /*
1324  * Dump core, into a file named "progname.core" or "core" (depending on the
1325  * value of shortcorename), unless the process was setuid/setgid.
1326  */
1327 int
1328 coredump(struct proc *p)
1329 {
1330 	struct vnode		*vp;
1331 	struct vmspace		*vm;
1332 	struct ucred		*cred;
1333 	struct nameidata	nd;
1334 	struct vattr		vattr;
1335 	int			error, error1;
1336 	char			name[MAXPATHLEN];
1337 
1338 	vm = p->p_vmspace;
1339 	cred = p->p_cred->pc_ucred;
1340 
1341 	/*
1342 	 * Make sure the process has not set-id, to prevent data leaks.
1343 	 */
1344 	if (p->p_flag & P_SUGID)
1345 		return (EPERM);
1346 
1347 	/*
1348 	 * Refuse to core if the data + stack + user size is larger than
1349 	 * the core dump limit.  XXX THIS IS WRONG, because of mapped
1350 	 * data.
1351 	 */
1352 	if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1353 	    p->p_rlimit[RLIMIT_CORE].rlim_cur)
1354 		return (EFBIG);		/* better error code? */
1355 
1356 	/*
1357 	 * The core dump will go in the current working directory.  Make
1358 	 * sure that the directory is still there and that the mount flags
1359 	 * allow us to write core dumps there.
1360 	 */
1361 	vp = p->p_cwdi->cwdi_cdir;
1362 	if (vp->v_mount == NULL ||
1363 	    (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1364 		return (EPERM);
1365 
1366 	error = build_corename(p, name);
1367 	if (error)
1368 		return error;
1369 
1370 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1371 	error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
1372 	if (error)
1373 		return (error);
1374 	vp = nd.ni_vp;
1375 
1376 	/* Don't dump to non-regular files or files with links. */
1377 	if (vp->v_type != VREG ||
1378 	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1379 		error = EINVAL;
1380 		goto out;
1381 	}
1382 	VATTR_NULL(&vattr);
1383 	vattr.va_size = 0;
1384 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
1385 	VOP_SETATTR(vp, &vattr, cred, p);
1386 	p->p_acflag |= ACORE;
1387 
1388 	/* Now dump the actual core file. */
1389 	error = (*p->p_execsw->es_coredump)(p, vp, cred);
1390  out:
1391 	VOP_UNLOCK(vp, 0);
1392 	error1 = vn_close(vp, FWRITE, cred, p);
1393 	if (error == 0)
1394 		error = error1;
1395 	return (error);
1396 }
1397 
1398 /*
1399  * Nonexistent system call-- signal process (may want to handle it).
1400  * Flag error in case process won't see signal immediately (blocked or ignored).
1401  */
1402 /* ARGSUSED */
1403 int
1404 sys_nosys(struct proc *p, void *v, register_t *retval)
1405 {
1406 
1407 	psignal(p, SIGSYS);
1408 	return (ENOSYS);
1409 }
1410 
1411 static int
1412 build_corename(struct proc *p, char dst[MAXPATHLEN])
1413 {
1414 	const char	*s;
1415 	char		*d, *end;
1416 	int		i;
1417 
1418 	for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1419 	    *s != '\0'; s++) {
1420 		if (*s == '%') {
1421 			switch (*(s + 1)) {
1422 			case 'n':
1423 				i = snprintf(d, end - d, "%s", p->p_comm);
1424 				break;
1425 			case 'p':
1426 				i = snprintf(d, end - d, "%d", p->p_pid);
1427 				break;
1428 			case 'u':
1429 				i = snprintf(d, end - d, "%s",
1430 				    p->p_pgrp->pg_session->s_login);
1431 				break;
1432 			case 't':
1433 				i = snprintf(d, end - d, "%ld",
1434 				    p->p_stats->p_start.tv_sec);
1435 				break;
1436 			default:
1437 				goto copy;
1438 			}
1439 			d += i;
1440 			s++;
1441 		} else {
1442  copy:			*d = *s;
1443 			d++;
1444 		}
1445 		if (d >= end)
1446 			return (ENAMETOOLONG);
1447 	}
1448 	*d = '\0';
1449 	return (0);
1450 }
1451 
1452 /*
1453  * Returns true if signal is ignored or masked for passed process.
1454  */
1455 int
1456 sigismasked(struct proc *p, int sig)
1457 {
1458 
1459 	return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1460 	    sigismember(&p->p_sigctx.ps_sigmask, sig));
1461 }
1462