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