xref: /freebsd/sys/kern/kern_sig.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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 proc *cp, 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 
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 		PGRPSESS_SLOCK();
1005 		if (pgid == 0) {
1006 			/*
1007 			 * zero pgid means send to my process group.
1008 			 */
1009 			pgrp = cp->p_pgrp;
1010 			PGRP_LOCK(pgrp);
1011 		} else {
1012 			pgrp = pgfind(pgid);
1013 			if (pgrp == NULL) {
1014 				PGRPSESS_SUNLOCK();
1015 				return (ESRCH);
1016 			}
1017 		}
1018 		PGRPSESS_SUNLOCK();
1019 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1020 			PROC_LOCK(p);
1021 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1022 				PROC_UNLOCK(p);
1023 				continue;
1024 			}
1025 			mtx_lock_spin(&sched_lock);
1026 			if (p->p_stat == SZOMB) {
1027 				mtx_unlock_spin(&sched_lock);
1028 				PROC_UNLOCK(p);
1029 				continue;
1030 			}
1031 			mtx_unlock_spin(&sched_lock);
1032 			if (p_cansignal(cp, p, sig) == 0) {
1033 				nfound++;
1034 				if (sig)
1035 					psignal(p, sig);
1036 			}
1037 			PROC_UNLOCK(p);
1038 		}
1039 		PGRP_UNLOCK(pgrp);
1040 	}
1041 	return (nfound ? 0 : ESRCH);
1042 }
1043 
1044 #ifndef _SYS_SYSPROTO_H_
1045 struct kill_args {
1046 	int	pid;
1047 	int	signum;
1048 };
1049 #endif
1050 /*
1051  * MPSAFE
1052  */
1053 /* ARGSUSED */
1054 int
1055 kill(td, uap)
1056 	register struct thread *td;
1057 	register struct kill_args *uap;
1058 {
1059 	register struct proc *cp = td->td_proc;
1060 	register struct proc *p;
1061 	int error = 0;
1062 
1063 	if ((u_int)uap->signum > _SIG_MAXSIG)
1064 		return (EINVAL);
1065 
1066 	mtx_lock(&Giant);
1067 	if (uap->pid > 0) {
1068 		/* kill single process */
1069 		if ((p = pfind(uap->pid)) == NULL) {
1070 			error = ESRCH;
1071 		} else if (p_cansignal(cp, p, uap->signum)) {
1072 			PROC_UNLOCK(p);
1073 			error = EPERM;
1074 		} else {
1075 			if (uap->signum)
1076 				psignal(p, uap->signum);
1077 			PROC_UNLOCK(p);
1078 			error = 0;
1079 		}
1080 	} else {
1081 		switch (uap->pid) {
1082 		case -1:		/* broadcast signal */
1083 			error = killpg1(cp, uap->signum, 0, 1);
1084 			break;
1085 		case 0:			/* signal own process group */
1086 			error = killpg1(cp, uap->signum, 0, 0);
1087 			break;
1088 		default:		/* negative explicit process group */
1089 			error = killpg1(cp, uap->signum, -uap->pid, 0);
1090 			break;
1091 		}
1092 	}
1093 	mtx_unlock(&Giant);
1094 	return(error);
1095 }
1096 
1097 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1098 #ifndef _SYS_SYSPROTO_H_
1099 struct okillpg_args {
1100 	int	pgid;
1101 	int	signum;
1102 };
1103 #endif
1104 /*
1105  * MPSAFE
1106  */
1107 /* ARGSUSED */
1108 int
1109 okillpg(td, uap)
1110 	struct thread *td;
1111 	register struct okillpg_args *uap;
1112 {
1113 	int error;
1114 
1115 	if ((u_int)uap->signum > _SIG_MAXSIG)
1116 		return (EINVAL);
1117 	mtx_lock(&Giant);
1118 	error = killpg1(td->td_proc, uap->signum, uap->pgid, 0);
1119 	mtx_unlock(&Giant);
1120 	return (error);
1121 }
1122 #endif /* COMPAT_43 || COMPAT_SUNOS */
1123 
1124 /*
1125  * Send a signal to a process group.
1126  */
1127 void
1128 gsignal(pgid, sig)
1129 	int pgid, sig;
1130 {
1131 	struct pgrp *pgrp;
1132 
1133 	if (pgid != 0) {
1134 		PGRPSESS_SLOCK();
1135 		pgrp = pgfind(pgid);
1136 		PGRPSESS_SUNLOCK();
1137 		if (pgrp != NULL) {
1138 			pgsignal(pgrp, sig, 0);
1139 			PGRP_UNLOCK(pgrp);
1140 		}
1141 	}
1142 }
1143 
1144 /*
1145  * Send a signal to a process group.  If checktty is 1,
1146  * limit to members which have a controlling terminal.
1147  */
1148 void
1149 pgsignal(pgrp, sig, checkctty)
1150 	struct pgrp *pgrp;
1151 	int sig, checkctty;
1152 {
1153 	register struct proc *p;
1154 
1155 	if (pgrp) {
1156 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1157 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1158 			PROC_LOCK(p);
1159 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
1160 				psignal(p, sig);
1161 			PROC_UNLOCK(p);
1162 		}
1163 	}
1164 }
1165 
1166 /*
1167  * Send a signal caused by a trap to the current process.
1168  * If it will be caught immediately, deliver it with correct code.
1169  * Otherwise, post it normally.
1170  *
1171  * MPSAFE
1172  */
1173 void
1174 trapsignal(p, sig, code)
1175 	struct proc *p;
1176 	register int sig;
1177 	u_long code;
1178 {
1179 	register struct sigacts *ps = p->p_sigacts;
1180 
1181 	mtx_lock(&Giant);
1182 	PROC_LOCK(p);
1183 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(p->p_sigcatch, sig) &&
1184 	    !SIGISMEMBER(p->p_sigmask, sig)) {
1185 		p->p_stats->p_ru.ru_nsignals++;
1186 #ifdef KTRACE
1187 		if (KTRPOINT(p, KTR_PSIG))
1188 			ktrpsig(p->p_tracep, sig, ps->ps_sigact[_SIG_IDX(sig)],
1189 				&p->p_sigmask, code);
1190 #endif
1191 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)], sig,
1192 						&p->p_sigmask, code);
1193 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1194 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1195 			SIGADDSET(p->p_sigmask, sig);
1196 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1197 			/*
1198 			 * See do_sigaction() for origin of this code.
1199 			 */
1200 			SIGDELSET(p->p_sigcatch, sig);
1201 			if (sig != SIGCONT &&
1202 			    sigprop(sig) & SA_IGNORE)
1203 				SIGADDSET(p->p_sigignore, sig);
1204 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1205 		}
1206 	} else {
1207 		p->p_code = code;	/* XXX for core dump/debugger */
1208 		p->p_sig = sig;		/* XXX to verify code */
1209 		psignal(p, sig);
1210 	}
1211 	PROC_UNLOCK(p);
1212 	mtx_unlock(&Giant);
1213 }
1214 
1215 /*
1216  * Send the signal to the process.  If the signal has an action, the action
1217  * is usually performed by the target process rather than the caller; we add
1218  * the signal to the set of pending signals for the process.
1219  *
1220  * Exceptions:
1221  *   o When a stop signal is sent to a sleeping process that takes the
1222  *     default action, the process is stopped without awakening it.
1223  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1224  *     regardless of the signal action (eg, blocked or ignored).
1225  *
1226  * Other ignored signals are discarded immediately.
1227  */
1228 void
1229 psignal(p, sig)
1230 	register struct proc *p;
1231 	register int sig;
1232 {
1233 	register int prop;
1234 	register sig_t action;
1235 	struct thread *td;
1236 #ifdef SMP
1237 	struct ksegrp *kg;
1238 #endif
1239 
1240 	KASSERT(_SIG_VALID(sig),
1241 	    ("psignal(): invalid signal %d\n", sig));
1242 
1243 	PROC_LOCK_ASSERT(p, MA_OWNED);
1244 	KNOTE(&p->p_klist, NOTE_SIGNAL | sig);
1245 
1246 	prop = sigprop(sig);
1247 
1248 	/*
1249 	 * If proc is traced, always give parent a chance;
1250 	 * if signal event is tracked by procfs, give *that*
1251 	 * a chance, as well.
1252 	 */
1253 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1254 		action = SIG_DFL;
1255 	} else {
1256 		/*
1257 		 * If the signal is being ignored,
1258 		 * then we forget about it immediately.
1259 		 * (Note: we don't set SIGCONT in p_sigignore,
1260 		 * and if it is set to SIG_IGN,
1261 		 * action will be SIG_DFL here.)
1262 		 */
1263 		if (SIGISMEMBER(p->p_sigignore, sig) || (p->p_flag & P_WEXIT))
1264 			return;
1265 		if (SIGISMEMBER(p->p_sigmask, sig))
1266 			action = SIG_HOLD;
1267 		else if (SIGISMEMBER(p->p_sigcatch, sig))
1268 			action = SIG_CATCH;
1269 		else
1270 			action = SIG_DFL;
1271 	}
1272 
1273 	/*
1274 	 * bring the priority of a process up if we want it to get
1275 	 * killed in this lifetime.
1276 	 * XXXKSE think if a better way to do this.
1277 	 *
1278 	 * What we need to do is see if there is a thread that will
1279 	 * be able to accept the signal. e.g.
1280 	 * FOREACH_THREAD_IN_PROC() {
1281 	 *	if runnable, we're done
1282 	 *	else pick one at random.
1283 	 * }
1284 	 */
1285 	/* XXXKSE
1286 	 * For now there is one thread per proc.
1287 	 * Effectively select one sucker thread..
1288 	 */
1289 	td = FIRST_THREAD_IN_PROC(p);
1290 	mtx_lock_spin(&sched_lock);
1291 	if ((p->p_ksegrp.kg_nice > NZERO) && (action == SIG_DFL) &&
1292 	    (prop & SA_KILL) && ((p->p_flag & P_TRACED) == 0))
1293 		p->p_ksegrp.kg_nice = NZERO; /* XXXKSE */
1294 	mtx_unlock_spin(&sched_lock);
1295 
1296 	if (prop & SA_CONT)
1297 		SIG_STOPSIGMASK(p->p_siglist);
1298 
1299 	if (prop & SA_STOP) {
1300 		/*
1301 		 * If sending a tty stop signal to a member of an orphaned
1302 		 * process group, discard the signal here if the action
1303 		 * is default; don't stop the process below if sleeping,
1304 		 * and don't clear any pending SIGCONT.
1305 		 */
1306 		if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
1307 		    action == SIG_DFL)
1308 		        return;
1309 		SIG_CONTSIGMASK(p->p_siglist);
1310 	}
1311 	SIGADDSET(p->p_siglist, sig);
1312 
1313 	/*
1314 	 * Defer further processing for signals which are held,
1315 	 * except that stopped processes must be continued by SIGCONT.
1316 	 */
1317 	mtx_lock_spin(&sched_lock);
1318 	if (action == SIG_HOLD && (!(prop & SA_CONT) || p->p_stat != SSTOP)) {
1319 		mtx_unlock_spin(&sched_lock);
1320 		return;
1321 	}
1322 
1323 	switch (p->p_stat) {
1324 
1325 	case SSLEEP:
1326 		/*
1327 		 * If process is sleeping uninterruptibly
1328 		 * we can't interrupt the sleep... the signal will
1329 		 * be noticed when the process returns through
1330 		 * trap() or syscall().
1331 		 */
1332 		if ((td->td_flags & TDF_SINTR) == 0) {
1333 			mtx_unlock_spin(&sched_lock);
1334 			goto out;
1335 		}
1336 		/*
1337 		 * Process is sleeping and traced... make it runnable
1338 		 * so it can discover the signal in issignal() and stop
1339 		 * for the parent.
1340 		 */
1341 		if (p->p_flag & P_TRACED)
1342 			goto run;
1343 		mtx_unlock_spin(&sched_lock);
1344 		/*
1345 		 * If SIGCONT is default (or ignored) and process is
1346 		 * asleep, we are finished; the process should not
1347 		 * be awakened.
1348 		 */
1349 		if ((prop & SA_CONT) && action == SIG_DFL) {
1350 			SIGDELSET(p->p_siglist, sig);
1351 			goto out;
1352 		}
1353 		/*
1354 		 * When a sleeping process receives a stop
1355 		 * signal, process immediately if possible.
1356 		 * All other (caught or default) signals
1357 		 * cause the process to run.
1358 		 */
1359 		if (prop & SA_STOP) {
1360 			if (action != SIG_DFL)
1361 				goto runfast;
1362 			/*
1363 			 * If a child holding parent blocked,
1364 			 * stopping could cause deadlock.
1365 			 */
1366 			if (p->p_flag & P_PPWAIT)
1367 				goto out;
1368 			SIGDELSET(p->p_siglist, sig);
1369 			p->p_xstat = sig;
1370 			PROC_LOCK(p->p_pptr);
1371 			if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1372 				psignal(p->p_pptr, SIGCHLD);
1373 			PROC_UNLOCK(p->p_pptr);
1374 			mtx_lock_spin(&sched_lock);
1375 			stop(p);
1376 			mtx_unlock_spin(&sched_lock);
1377 			goto out;
1378 		} else
1379 			goto runfast;
1380 		/* NOTREACHED */
1381 
1382 	case SSTOP:
1383 		mtx_unlock_spin(&sched_lock);
1384 		/*
1385 		 * If traced process is already stopped,
1386 		 * then no further action is necessary.
1387 		 */
1388 		if (p->p_flag & P_TRACED)
1389 			goto out;
1390 
1391 		/*
1392 		 * Kill signal always sets processes running.
1393 		 */
1394 		if (sig == SIGKILL)
1395 			goto runfast;
1396 
1397 		if (prop & SA_CONT) {
1398 			/*
1399 			 * If SIGCONT is default (or ignored), we continue the
1400 			 * process but don't leave the signal in p_siglist, as
1401 			 * it has no further action.  If SIGCONT is held, we
1402 			 * continue the process and leave the signal in
1403 			 * p_siglist.  If the process catches SIGCONT, let it
1404 			 * handle the signal itself.  If it isn't waiting on
1405 			 * an event, then it goes back to run state.
1406 			 * Otherwise, process goes back to sleep state.
1407 			 */
1408 			if (action == SIG_DFL)
1409 				SIGDELSET(p->p_siglist, sig);
1410 			if (action == SIG_CATCH)
1411 				goto runfast;
1412 			mtx_lock_spin(&sched_lock);
1413 			/*
1414 			 * XXXKSE
1415 			 * do this for each thread.
1416 			 */
1417 			if (p->p_flag & P_KSES) {
1418 				mtx_assert(&sched_lock,
1419 				    MA_OWNED | MA_NOTRECURSED);
1420 				FOREACH_THREAD_IN_PROC(p, td) {
1421 					if (td->td_wchan == NULL) {
1422 						setrunnable(td); /* XXXKSE */
1423 					} else {
1424 						/* mark it as sleeping */
1425 					}
1426 				}
1427 				mtx_unlock_spin(&sched_lock);
1428 			} else {
1429 				if (td->td_wchan == NULL)
1430 					goto run;
1431 				p->p_stat = SSLEEP;
1432 				mtx_unlock_spin(&sched_lock);
1433 			}
1434 			goto out;
1435 		}
1436 
1437 		if (prop & SA_STOP) {
1438 			/*
1439 			 * Already stopped, don't need to stop again.
1440 			 * (If we did the shell could get confused.)
1441 			 */
1442 			SIGDELSET(p->p_siglist, sig);
1443 			goto out;
1444 		}
1445 
1446 		/*
1447 		 * If process is sleeping interruptibly, then simulate a
1448 		 * wakeup so that when it is continued, it will be made
1449 		 * runnable and can look at the signal.  But don't make
1450 		 * the process runnable, leave it stopped.
1451 		 * XXXKSE should we wake ALL blocked threads?
1452 		 */
1453 		mtx_lock_spin(&sched_lock);
1454 		if (p->p_flag & P_KSES) {
1455 			FOREACH_THREAD_IN_PROC(p, td) {
1456 				if (td->td_wchan && (td->td_flags & TDF_SINTR)){
1457 					if (td->td_flags & TDF_CVWAITQ)
1458 						cv_waitq_remove(td);
1459 					else
1460 						unsleep(td); /* XXXKSE */
1461 				}
1462 			}
1463 		} else {
1464 			if (td->td_wchan && td->td_flags & TDF_SINTR) {
1465 				if (td->td_flags & TDF_CVWAITQ)
1466 					cv_waitq_remove(td);
1467 				else
1468 					unsleep(td); /* XXXKSE */
1469 			}
1470 		}
1471 		mtx_unlock_spin(&sched_lock);
1472 		goto out;
1473 
1474 	default:
1475 		/*
1476 		 * SRUN, SIDL, SZOMB do nothing with the signal,
1477 		 * other than kicking ourselves if we are running.
1478 		 * It will either never be noticed, or noticed very soon.
1479 		 */
1480 		if (p->p_stat == SRUN) {
1481 #ifdef SMP
1482 			struct kse *ke;
1483 			struct thread *td = curthread;
1484 			signotify(&p->p_kse);  /* XXXKSE */
1485 /* we should only deliver to one thread.. but which one? */
1486 			FOREACH_KSEGRP_IN_PROC(p, kg) {
1487 				FOREACH_KSE_IN_GROUP(kg, ke) {
1488 					if (ke->ke_thread == td) {
1489 						continue;
1490 					}
1491 					forward_signal(ke->ke_thread);
1492 				}
1493 			}
1494 #else
1495 			signotify(&p->p_kse);  /* XXXKSE */
1496 #endif
1497 		}
1498 		mtx_unlock_spin(&sched_lock);
1499 		goto out;
1500 	}
1501 	/*NOTREACHED*/
1502 
1503 runfast:
1504 	/*
1505 	 * Raise priority to at least PUSER.
1506 	 * XXXKSE Should we make them all run fast?
1507 	 * Maybe just one would be enough?
1508 	 */
1509 	mtx_lock_spin(&sched_lock);
1510 
1511 	if (FIRST_THREAD_IN_PROC(p)->td_priority > PUSER) {
1512 		FIRST_THREAD_IN_PROC(p)->td_priority = PUSER;
1513 	}
1514 run:
1515 	/* If we jump here, sched_lock has to be owned. */
1516 	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
1517 	setrunnable(td); /* XXXKSE */
1518 	mtx_unlock_spin(&sched_lock);
1519 out:
1520 	/* If we jump here, sched_lock should not be owned. */
1521 	mtx_assert(&sched_lock, MA_NOTOWNED);
1522 }
1523 
1524 /*
1525  * If the current process has received a signal (should be caught or cause
1526  * termination, should interrupt current syscall), return the signal number.
1527  * Stop signals with default action are processed immediately, then cleared;
1528  * they aren't returned.  This is checked after each entry to the system for
1529  * a syscall or trap (though this can usually be done without calling issignal
1530  * by checking the pending signal masks in the CURSIG macro.) The normal call
1531  * sequence is
1532  *
1533  *	while (sig = CURSIG(curproc))
1534  *		postsig(sig);
1535  */
1536 int
1537 issignal(p)
1538 	register struct proc *p;
1539 {
1540 	sigset_t mask;
1541 	register int sig, prop;
1542 
1543 	PROC_LOCK_ASSERT(p, MA_OWNED);
1544 	for (;;) {
1545 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
1546 
1547 		mask = p->p_siglist;
1548 		SIGSETNAND(mask, p->p_sigmask);
1549 		if (p->p_flag & P_PPWAIT)
1550 			SIG_STOPSIGMASK(mask);
1551 		if (!SIGNOTEMPTY(mask))		/* no signal to send */
1552 			return (0);
1553 		sig = sig_ffs(&mask);
1554 		prop = sigprop(sig);
1555 
1556 		_STOPEVENT(p, S_SIG, sig);
1557 
1558 		/*
1559 		 * We should see pending but ignored signals
1560 		 * only if P_TRACED was on when they were posted.
1561 		 */
1562 		if (SIGISMEMBER(p->p_sigignore, sig) && (traced == 0)) {
1563 			SIGDELSET(p->p_siglist, sig);
1564 			continue;
1565 		}
1566 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1567 			/*
1568 			 * If traced, always stop, and stay
1569 			 * stopped until released by the parent.
1570 			 */
1571 			p->p_xstat = sig;
1572 			PROC_LOCK(p->p_pptr);
1573 			psignal(p->p_pptr, SIGCHLD);
1574 			PROC_UNLOCK(p->p_pptr);
1575 			do {
1576 				mtx_lock_spin(&sched_lock);
1577 				stop(p);
1578 				PROC_UNLOCK(p);
1579 				DROP_GIANT();
1580 				p->p_stats->p_ru.ru_nivcsw++;
1581 				mi_switch();
1582 				mtx_unlock_spin(&sched_lock);
1583 				PICKUP_GIANT();
1584 				PROC_LOCK(p);
1585 			} while (!trace_req(p)
1586 				 && p->p_flag & P_TRACED);
1587 
1588 			/*
1589 			 * If the traced bit got turned off, go back up
1590 			 * to the top to rescan signals.  This ensures
1591 			 * that p_sig* and ps_sigact are consistent.
1592 			 */
1593 			if ((p->p_flag & P_TRACED) == 0)
1594 				continue;
1595 
1596 			/*
1597 			 * If parent wants us to take the signal,
1598 			 * then it will leave it in p->p_xstat;
1599 			 * otherwise we just look for signals again.
1600 			 */
1601 			SIGDELSET(p->p_siglist, sig);	/* clear old signal */
1602 			sig = p->p_xstat;
1603 			if (sig == 0)
1604 				continue;
1605 
1606 			/*
1607 			 * Put the new signal into p_siglist.  If the
1608 			 * signal is being masked, look for other signals.
1609 			 */
1610 			SIGADDSET(p->p_siglist, sig);
1611 			if (SIGISMEMBER(p->p_sigmask, sig))
1612 				continue;
1613 		}
1614 
1615 		/*
1616 		 * Decide whether the signal should be returned.
1617 		 * Return the signal's number, or fall through
1618 		 * to clear it from the pending mask.
1619 		 */
1620 		switch ((int)(intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
1621 
1622 		case (int)SIG_DFL:
1623 			/*
1624 			 * Don't take default actions on system processes.
1625 			 */
1626 			if (p->p_pid <= 1) {
1627 #ifdef DIAGNOSTIC
1628 				/*
1629 				 * Are you sure you want to ignore SIGSEGV
1630 				 * in init? XXX
1631 				 */
1632 				printf("Process (pid %lu) got signal %d\n",
1633 					(u_long)p->p_pid, sig);
1634 #endif
1635 				break;		/* == ignore */
1636 			}
1637 			/*
1638 			 * If there is a pending stop signal to process
1639 			 * with default action, stop here,
1640 			 * then clear the signal.  However,
1641 			 * if process is member of an orphaned
1642 			 * process group, ignore tty stop signals.
1643 			 */
1644 			if (prop & SA_STOP) {
1645 				if (p->p_flag & P_TRACED ||
1646 		    		    (p->p_pgrp->pg_jobc == 0 &&
1647 				     prop & SA_TTYSTOP))
1648 					break;	/* == ignore */
1649 				p->p_xstat = sig;
1650 				PROC_LOCK(p->p_pptr);
1651 				if ((p->p_pptr->p_procsig->ps_flag & PS_NOCLDSTOP) == 0)
1652 					psignal(p->p_pptr, SIGCHLD);
1653 				PROC_UNLOCK(p->p_pptr);
1654 				mtx_lock_spin(&sched_lock);
1655 				stop(p);
1656 				PROC_UNLOCK(p);
1657 				DROP_GIANT();
1658 				p->p_stats->p_ru.ru_nivcsw++;
1659 				mi_switch();
1660 				mtx_unlock_spin(&sched_lock);
1661 				PICKUP_GIANT();
1662 				PROC_LOCK(p);
1663 				break;
1664 			} else if (prop & SA_IGNORE) {
1665 				/*
1666 				 * Except for SIGCONT, shouldn't get here.
1667 				 * Default action is to ignore; drop it.
1668 				 */
1669 				break;		/* == ignore */
1670 			} else
1671 				return (sig);
1672 			/*NOTREACHED*/
1673 
1674 		case (int)SIG_IGN:
1675 			/*
1676 			 * Masking above should prevent us ever trying
1677 			 * to take action on an ignored signal other
1678 			 * than SIGCONT, unless process is traced.
1679 			 */
1680 			if ((prop & SA_CONT) == 0 &&
1681 			    (p->p_flag & P_TRACED) == 0)
1682 				printf("issignal\n");
1683 			break;		/* == ignore */
1684 
1685 		default:
1686 			/*
1687 			 * This signal has an action, let
1688 			 * postsig() process it.
1689 			 */
1690 			return (sig);
1691 		}
1692 		SIGDELSET(p->p_siglist, sig);		/* take the signal! */
1693 	}
1694 	/* NOTREACHED */
1695 }
1696 
1697 /*
1698  * Put the argument process into the stopped state and notify the parent
1699  * via wakeup.  Signals are handled elsewhere.  The process must not be
1700  * on the run queue.  Must be called with the proc p locked and the scheduler
1701  * lock held.
1702  */
1703 static void
1704 stop(p)
1705 	register struct proc *p;
1706 {
1707 
1708 	PROC_LOCK_ASSERT(p, MA_OWNED);
1709 	mtx_assert(&sched_lock, MA_OWNED);
1710 	p->p_stat = SSTOP;
1711 	p->p_flag &= ~P_WAITED;
1712 	wakeup((caddr_t)p->p_pptr);
1713 }
1714 
1715 /*
1716  * Take the action for the specified signal
1717  * from the current set of pending signals.
1718  */
1719 void
1720 postsig(sig)
1721 	register int sig;
1722 {
1723 	struct thread *td = curthread;
1724 	register struct proc *p = td->td_proc;
1725 	struct sigacts *ps;
1726 	sig_t action;
1727 	sigset_t returnmask;
1728 	int code;
1729 
1730 	KASSERT(sig != 0, ("postsig"));
1731 
1732 	PROC_LOCK_ASSERT(p, MA_OWNED);
1733 	ps = p->p_sigacts;
1734 	SIGDELSET(p->p_siglist, sig);
1735 	action = ps->ps_sigact[_SIG_IDX(sig)];
1736 #ifdef KTRACE
1737 	if (KTRPOINT(p, KTR_PSIG))
1738 		ktrpsig(p->p_tracep, sig, action, p->p_flag & P_OLDMASK ?
1739 		    &p->p_oldsigmask : &p->p_sigmask, 0);
1740 #endif
1741 	_STOPEVENT(p, S_SIG, sig);
1742 
1743 	if (action == SIG_DFL) {
1744 		/*
1745 		 * Default action, where the default is to kill
1746 		 * the process.  (Other cases were ignored above.)
1747 		 */
1748 		sigexit(td, sig);
1749 		/* NOTREACHED */
1750 	} else {
1751 		/*
1752 		 * If we get here, the signal must be caught.
1753 		 */
1754 		KASSERT(action != SIG_IGN && !SIGISMEMBER(p->p_sigmask, sig),
1755 		    ("postsig action"));
1756 		/*
1757 		 * Set the new mask value and also defer further
1758 		 * occurrences of this signal.
1759 		 *
1760 		 * Special case: user has done a sigsuspend.  Here the
1761 		 * current mask is not of interest, but rather the
1762 		 * mask from before the sigsuspend is what we want
1763 		 * restored after the signal processing is completed.
1764 		 */
1765 		if (p->p_flag & P_OLDMASK) {
1766 			returnmask = p->p_oldsigmask;
1767 			p->p_flag &= ~P_OLDMASK;
1768 		} else
1769 			returnmask = p->p_sigmask;
1770 
1771 		SIGSETOR(p->p_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1772 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1773 			SIGADDSET(p->p_sigmask, sig);
1774 
1775 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1776 			/*
1777 			 * See do_sigaction() for origin of this code.
1778 			 */
1779 			SIGDELSET(p->p_sigcatch, sig);
1780 			if (sig != SIGCONT &&
1781 			    sigprop(sig) & SA_IGNORE)
1782 				SIGADDSET(p->p_sigignore, sig);
1783 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1784 		}
1785 		p->p_stats->p_ru.ru_nsignals++;
1786 		if (p->p_sig != sig) {
1787 			code = 0;
1788 		} else {
1789 			code = p->p_code;
1790 			p->p_code = 0;
1791 			p->p_sig = 0;
1792 		}
1793 		(*p->p_sysent->sv_sendsig)(action, sig, &returnmask, code);
1794 	}
1795 }
1796 
1797 /*
1798  * Kill the current process for stated reason.
1799  */
1800 void
1801 killproc(p, why)
1802 	struct proc *p;
1803 	char *why;
1804 {
1805 
1806 	PROC_LOCK_ASSERT(p, MA_OWNED);
1807 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
1808 		p, p->p_pid, p->p_comm);
1809 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
1810 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
1811 	psignal(p, SIGKILL);
1812 }
1813 
1814 /*
1815  * Force the current process to exit with the specified signal, dumping core
1816  * if appropriate.  We bypass the normal tests for masked and caught signals,
1817  * allowing unrecoverable failures to terminate the process without changing
1818  * signal state.  Mark the accounting record with the signal termination.
1819  * If dumping core, save the signal number for the debugger.  Calls exit and
1820  * does not return.
1821  */
1822 void
1823 sigexit(td, sig)
1824 	struct thread *td;
1825 	int sig;
1826 {
1827 	struct proc *p = td->td_proc;
1828 
1829 	PROC_LOCK_ASSERT(p, MA_OWNED);
1830 	p->p_acflag |= AXSIG;
1831 	if (sigprop(sig) & SA_CORE) {
1832 		p->p_sig = sig;
1833 		/*
1834 		 * Log signals which would cause core dumps
1835 		 * (Log as LOG_INFO to appease those who don't want
1836 		 * these messages.)
1837 		 * XXX : Todo, as well as euid, write out ruid too
1838 		 */
1839 		PROC_UNLOCK(p);
1840 		if (!mtx_owned(&Giant))
1841 			mtx_lock(&Giant);
1842 		if (coredump(td) == 0)
1843 			sig |= WCOREFLAG;
1844 		if (kern_logsigexit)
1845 			log(LOG_INFO,
1846 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
1847 			    p->p_pid, p->p_comm,
1848 			    p->p_ucred ? p->p_ucred->cr_uid : -1,
1849 			    sig &~ WCOREFLAG,
1850 			    sig & WCOREFLAG ? " (core dumped)" : "");
1851 	} else {
1852 		PROC_UNLOCK(p);
1853 		if (!mtx_owned(&Giant))
1854 			mtx_lock(&Giant);
1855 	}
1856 	exit1(td, W_EXITCODE(0, sig));
1857 	/* NOTREACHED */
1858 }
1859 
1860 static char corefilename[MAXPATHLEN+1] = {"%N.core"};
1861 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
1862 	      sizeof(corefilename), "process corefile name format string");
1863 
1864 /*
1865  * expand_name(name, uid, pid)
1866  * Expand the name described in corefilename, using name, uid, and pid.
1867  * corefilename is a printf-like string, with three format specifiers:
1868  *	%N	name of process ("name")
1869  *	%P	process id (pid)
1870  *	%U	user id (uid)
1871  * For example, "%N.core" is the default; they can be disabled completely
1872  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
1873  * This is controlled by the sysctl variable kern.corefile (see above).
1874  */
1875 
1876 static char *
1877 expand_name(name, uid, pid)
1878 const char *name; uid_t uid; pid_t pid; {
1879 	char *temp;
1880 	char buf[11];		/* Buffer for pid/uid -- max 4B */
1881 	int i, n;
1882 	char *format = corefilename;
1883 	size_t namelen;
1884 
1885 	temp = malloc(MAXPATHLEN + 1, M_TEMP, M_NOWAIT);
1886 	if (temp == NULL)
1887 		return NULL;
1888 	namelen = strlen(name);
1889 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
1890 		int l;
1891 		switch (format[i]) {
1892 		case '%':	/* Format character */
1893 			i++;
1894 			switch (format[i]) {
1895 			case '%':
1896 				temp[n++] = '%';
1897 				break;
1898 			case 'N':	/* process name */
1899 				if ((n + namelen) > MAXPATHLEN) {
1900 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1901 					    pid, name, uid, temp, name);
1902 					free(temp, M_TEMP);
1903 					return NULL;
1904 				}
1905 				memcpy(temp+n, name, namelen);
1906 				n += namelen;
1907 				break;
1908 			case 'P':	/* process id */
1909 				l = sprintf(buf, "%u", pid);
1910 				if ((n + l) > MAXPATHLEN) {
1911 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1912 					    pid, name, uid, temp, name);
1913 					free(temp, M_TEMP);
1914 					return NULL;
1915 				}
1916 				memcpy(temp+n, buf, l);
1917 				n += l;
1918 				break;
1919 			case 'U':	/* user id */
1920 				l = sprintf(buf, "%u", uid);
1921 				if ((n + l) > MAXPATHLEN) {
1922 					log(LOG_ERR, "pid %d (%s), uid (%u):  Path `%s%s' is too long\n",
1923 					    pid, name, uid, temp, name);
1924 					free(temp, M_TEMP);
1925 					return NULL;
1926 				}
1927 				memcpy(temp+n, buf, l);
1928 				n += l;
1929 				break;
1930 			default:
1931 			  	log(LOG_ERR, "Unknown format character %c in `%s'\n", format[i], format);
1932 			}
1933 			break;
1934 		default:
1935 			temp[n++] = format[i];
1936 		}
1937 	}
1938 	temp[n] = '\0';
1939 	return temp;
1940 }
1941 
1942 /*
1943  * Dump a process' core.  The main routine does some
1944  * policy checking, and creates the name of the coredump;
1945  * then it passes on a vnode and a size limit to the process-specific
1946  * coredump routine if there is one; if there _is not_ one, it returns
1947  * ENOSYS; otherwise it returns the error from the process-specific routine.
1948  *
1949  * XXX: VOP_GETATTR() here requires holding the vnode lock.
1950  */
1951 
1952 static int
1953 coredump(struct thread *td)
1954 {
1955 	struct proc *p = td->td_proc;
1956 	register struct vnode *vp;
1957 	register struct ucred *cred = p->p_ucred;
1958 	struct flock lf;
1959 	struct nameidata nd;
1960 	struct vattr vattr;
1961 	int error, error1, flags;
1962 	struct mount *mp;
1963 	char *name;			/* name of corefile */
1964 	off_t limit;
1965 
1966 	PROC_LOCK(p);
1967 	_STOPEVENT(p, S_CORE, 0);
1968 
1969 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
1970 		PROC_UNLOCK(p);
1971 		return (EFAULT);
1972 	}
1973 
1974 	/*
1975 	 * Note that the bulk of limit checking is done after
1976 	 * the corefile is created.  The exception is if the limit
1977 	 * for corefiles is 0, in which case we don't bother
1978 	 * creating the corefile at all.  This layout means that
1979 	 * a corefile is truncated instead of not being created,
1980 	 * if it is larger than the limit.
1981 	 */
1982 	limit = p->p_rlimit[RLIMIT_CORE].rlim_cur;
1983 	if (limit == 0) {
1984 		PROC_UNLOCK(p);
1985 		return 0;
1986 	}
1987 	PROC_UNLOCK(p);
1988 
1989 restart:
1990 	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
1991 	if (name == NULL)
1992 		return (EINVAL);
1993 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
1994 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
1995 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR);
1996 	free(name, M_TEMP);
1997 	if (error)
1998 		return (error);
1999 	NDFREE(&nd, NDF_ONLY_PNBUF);
2000 	vp = nd.ni_vp;
2001 
2002 	VOP_UNLOCK(vp, 0, td);
2003 	lf.l_whence = SEEK_SET;
2004 	lf.l_start = 0;
2005 	lf.l_len = 0;
2006 	lf.l_type = F_WRLCK;
2007 	error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK);
2008 	if (error)
2009 		goto out2;
2010 
2011 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2012 		lf.l_type = F_UNLCK;
2013 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2014 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2015 			return (error);
2016 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2017 			return (error);
2018 		goto restart;
2019 	}
2020 
2021 	/* Don't dump to non-regular files or files with links. */
2022 	if (vp->v_type != VREG ||
2023 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2024 		error = EFAULT;
2025 		goto out1;
2026 	}
2027 	VATTR_NULL(&vattr);
2028 	vattr.va_size = 0;
2029 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2030 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2031 	VOP_SETATTR(vp, &vattr, cred, td);
2032 	VOP_UNLOCK(vp, 0, td);
2033 	PROC_LOCK(p);
2034 	p->p_acflag |= ACORE;
2035 	PROC_UNLOCK(p);
2036 
2037 	error = p->p_sysent->sv_coredump ?
2038 	  p->p_sysent->sv_coredump(td, vp, limit) :
2039 	  ENOSYS;
2040 
2041 out1:
2042 	lf.l_type = F_UNLCK;
2043 	VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2044 	vn_finished_write(mp);
2045 out2:
2046 	error1 = vn_close(vp, FWRITE, cred, td);
2047 	if (error == 0)
2048 		error = error1;
2049 	return (error);
2050 }
2051 
2052 /*
2053  * Nonexistent system call-- signal process (may want to handle it).
2054  * Flag error in case process won't see signal immediately (blocked or ignored).
2055  */
2056 #ifndef _SYS_SYSPROTO_H_
2057 struct nosys_args {
2058 	int	dummy;
2059 };
2060 #endif
2061 /*
2062  * MPSAFE
2063  */
2064 /* ARGSUSED */
2065 int
2066 nosys(td, args)
2067 	struct thread *td;
2068 	struct nosys_args *args;
2069 {
2070 	struct proc *p = td->td_proc;
2071 
2072 	mtx_lock(&Giant);
2073 	PROC_LOCK(p);
2074 	psignal(p, SIGSYS);
2075 	PROC_UNLOCK(p);
2076 	mtx_unlock(&Giant);
2077 	return (EINVAL);
2078 }
2079 
2080 /*
2081  * Send a SIGIO or SIGURG signal to a process or process group using
2082  * stored credentials rather than those of the current process.
2083  */
2084 void
2085 pgsigio(sigio, sig, checkctty)
2086 	struct sigio *sigio;
2087 	int sig, checkctty;
2088 {
2089 	if (sigio == NULL)
2090 		return;
2091 
2092 	if (sigio->sio_pgid > 0) {
2093 		PROC_LOCK(sigio->sio_proc);
2094 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2095 			psignal(sigio->sio_proc, sig);
2096 		PROC_UNLOCK(sigio->sio_proc);
2097 	} else if (sigio->sio_pgid < 0) {
2098 		struct proc *p;
2099 
2100 		PGRP_LOCK(sigio->sio_pgrp);
2101 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2102 			PROC_LOCK(p);
2103 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2104 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2105 				psignal(p, sig);
2106 			PROC_UNLOCK(p);
2107 		}
2108 		PGRP_UNLOCK(sigio->sio_pgrp);
2109 	}
2110 }
2111 
2112 static int
2113 filt_sigattach(struct knote *kn)
2114 {
2115 	struct proc *p = curproc;
2116 
2117 	kn->kn_ptr.p_proc = p;
2118 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2119 
2120 	PROC_LOCK(p);
2121 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2122 	PROC_UNLOCK(p);
2123 
2124 	return (0);
2125 }
2126 
2127 static void
2128 filt_sigdetach(struct knote *kn)
2129 {
2130 	struct proc *p = kn->kn_ptr.p_proc;
2131 
2132 	PROC_LOCK(p);
2133 	SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2134 	PROC_UNLOCK(p);
2135 }
2136 
2137 /*
2138  * signal knotes are shared with proc knotes, so we apply a mask to
2139  * the hint in order to differentiate them from process hints.  This
2140  * could be avoided by using a signal-specific knote list, but probably
2141  * isn't worth the trouble.
2142  */
2143 static int
2144 filt_signal(struct knote *kn, long hint)
2145 {
2146 
2147 	if (hint & NOTE_SIGNAL) {
2148 		hint &= ~NOTE_SIGNAL;
2149 
2150 		if (kn->kn_id == hint)
2151 			kn->kn_data++;
2152 	}
2153 	return (kn->kn_data != 0);
2154 }
2155