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