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