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