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