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