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