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