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