xref: /freebsd/sys/kern/kern_sig.c (revision 7afc53b8dfcc7d5897920ce6cc7e842fbb4ab813)
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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_sig.c	8.7 (Berkeley) 4/18/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_compat.h"
41 #include "opt_ktrace.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47 #include <sys/acct.h>
48 #include <sys/condvar.h>
49 #include <sys/event.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
52 #include <sys/kse.h>
53 #include <sys/ktr.h>
54 #include <sys/ktrace.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/pioctl.h>
61 #include <sys/resourcevar.h>
62 #include <sys/sched.h>
63 #include <sys/sleepqueue.h>
64 #include <sys/smp.h>
65 #include <sys/stat.h>
66 #include <sys/sx.h>
67 #include <sys/syscallsubr.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
70 #include <sys/syslog.h>
71 #include <sys/sysproto.h>
72 #include <sys/unistd.h>
73 #include <sys/wait.h>
74 
75 #include <machine/cpu.h>
76 
77 #if defined (__alpha__) && !defined(COMPAT_43)
78 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
79 #endif
80 
81 #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
82 
83 static int	coredump(struct thread *);
84 static char	*expand_name(const char *, uid_t, pid_t);
85 static int	killpg1(struct thread *td, int sig, int pgid, int all);
86 static int	issignal(struct thread *p);
87 static int	sigprop(int sig);
88 static void	tdsigwakeup(struct thread *td, int sig, sig_t action);
89 static int	filt_sigattach(struct knote *kn);
90 static void	filt_sigdetach(struct knote *kn);
91 static int	filt_signal(struct knote *kn, long hint);
92 static struct thread *sigtd(struct proc *p, int sig, int prop);
93 static int	kern_sigtimedwait(struct thread *td, sigset_t set,
94 				siginfo_t *info, struct timespec *timeout);
95 static void	do_tdsignal(struct thread *td, int sig, sigtarget_t target);
96 
97 struct filterops sig_filtops =
98 	{ 0, filt_sigattach, filt_sigdetach, filt_signal };
99 
100 static int	kern_logsigexit = 1;
101 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
102     &kern_logsigexit, 0,
103     "Log processes quitting on abnormal signals to syslog(3)");
104 
105 /*
106  * Policy -- Can ucred cr1 send SIGIO to process cr2?
107  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
108  * in the right situations.
109  */
110 #define CANSIGIO(cr1, cr2) \
111 	((cr1)->cr_uid == 0 || \
112 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
113 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
114 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
115 	    (cr1)->cr_uid == (cr2)->cr_uid)
116 
117 int sugid_coredump;
118 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
119     &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
120 
121 static int	do_coredump = 1;
122 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
123 	&do_coredump, 0, "Enable/Disable coredumps");
124 
125 static int	set_core_nodump_flag = 0;
126 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
127 	0, "Enable setting the NODUMP flag on coredump files");
128 
129 /*
130  * Signal properties and actions.
131  * The array below categorizes the signals and their default actions
132  * according to the following properties:
133  */
134 #define	SA_KILL		0x01		/* terminates process by default */
135 #define	SA_CORE		0x02		/* ditto and coredumps */
136 #define	SA_STOP		0x04		/* suspend process */
137 #define	SA_TTYSTOP	0x08		/* ditto, from tty */
138 #define	SA_IGNORE	0x10		/* ignore by default */
139 #define	SA_CONT		0x20		/* continue if suspended */
140 #define	SA_CANTMASK	0x40		/* non-maskable, catchable */
141 #define	SA_PROC		0x80		/* deliverable to any thread */
142 
143 static int sigproptbl[NSIG] = {
144         SA_KILL|SA_PROC,		/* SIGHUP */
145         SA_KILL|SA_PROC,		/* SIGINT */
146         SA_KILL|SA_CORE|SA_PROC,	/* SIGQUIT */
147         SA_KILL|SA_CORE,		/* SIGILL */
148         SA_KILL|SA_CORE,		/* SIGTRAP */
149         SA_KILL|SA_CORE,		/* SIGABRT */
150         SA_KILL|SA_CORE|SA_PROC,	/* SIGEMT */
151         SA_KILL|SA_CORE,		/* SIGFPE */
152         SA_KILL|SA_PROC,		/* SIGKILL */
153         SA_KILL|SA_CORE,		/* SIGBUS */
154         SA_KILL|SA_CORE,		/* SIGSEGV */
155         SA_KILL|SA_CORE,		/* SIGSYS */
156         SA_KILL|SA_PROC,		/* SIGPIPE */
157         SA_KILL|SA_PROC,		/* SIGALRM */
158         SA_KILL|SA_PROC,		/* SIGTERM */
159         SA_IGNORE|SA_PROC,		/* SIGURG */
160         SA_STOP|SA_PROC,		/* SIGSTOP */
161         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTSTP */
162         SA_IGNORE|SA_CONT|SA_PROC,	/* SIGCONT */
163         SA_IGNORE|SA_PROC,		/* SIGCHLD */
164         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTIN */
165         SA_STOP|SA_TTYSTOP|SA_PROC,	/* SIGTTOU */
166         SA_IGNORE|SA_PROC,		/* SIGIO */
167         SA_KILL,			/* SIGXCPU */
168         SA_KILL,			/* SIGXFSZ */
169         SA_KILL|SA_PROC,		/* SIGVTALRM */
170         SA_KILL|SA_PROC,		/* SIGPROF */
171         SA_IGNORE|SA_PROC,		/* SIGWINCH  */
172         SA_IGNORE|SA_PROC,		/* SIGINFO */
173         SA_KILL|SA_PROC,		/* SIGUSR1 */
174         SA_KILL|SA_PROC,		/* SIGUSR2 */
175 };
176 
177 /*
178  * Determine signal that should be delivered to process p, the current
179  * process, 0 if none.  If there is a pending stop signal with default
180  * action, the process stops in issignal().
181  * XXXKSE   the check for a pending stop is not done under KSE
182  *
183  * MP SAFE.
184  */
185 int
186 cursig(struct thread *td)
187 {
188 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
189 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
190 	mtx_assert(&sched_lock, MA_NOTOWNED);
191 	return (SIGPENDING(td) ? issignal(td) : 0);
192 }
193 
194 /*
195  * Arrange for ast() to handle unmasked pending signals on return to user
196  * mode.  This must be called whenever a signal is added to td_siglist or
197  * unmasked in td_sigmask.
198  */
199 void
200 signotify(struct thread *td)
201 {
202 	struct proc *p;
203 	sigset_t set, saved;
204 
205 	p = td->td_proc;
206 
207 	PROC_LOCK_ASSERT(p, MA_OWNED);
208 
209 	/*
210 	 * If our mask changed we may have to move signal that were
211 	 * previously masked by all threads to our siglist.
212 	 */
213 	set = p->p_siglist;
214 	if (p->p_flag & P_SA)
215 		saved = p->p_siglist;
216 	SIGSETNAND(set, td->td_sigmask);
217 	SIGSETNAND(p->p_siglist, set);
218 	SIGSETOR(td->td_siglist, set);
219 
220 	if (SIGPENDING(td)) {
221 		mtx_lock_spin(&sched_lock);
222 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
223 		mtx_unlock_spin(&sched_lock);
224 	}
225 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
226 		if (!SIGSETEQ(saved, p->p_siglist)) {
227 			/* pending set changed */
228 			p->p_flag |= P_SIGEVENT;
229 			wakeup(&p->p_siglist);
230 		}
231 	}
232 }
233 
234 int
235 sigonstack(size_t sp)
236 {
237 	struct thread *td = curthread;
238 
239 	return ((td->td_pflags & TDP_ALTSTACK) ?
240 #if defined(COMPAT_43)
241 	    ((td->td_sigstk.ss_size == 0) ?
242 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
243 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
244 #else
245 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
246 #endif
247 	    : 0);
248 }
249 
250 static __inline int
251 sigprop(int sig)
252 {
253 
254 	if (sig > 0 && sig < NSIG)
255 		return (sigproptbl[_SIG_IDX(sig)]);
256 	return (0);
257 }
258 
259 int
260 sig_ffs(sigset_t *set)
261 {
262 	int i;
263 
264 	for (i = 0; i < _SIG_WORDS; i++)
265 		if (set->__bits[i])
266 			return (ffs(set->__bits[i]) + (i * 32));
267 	return (0);
268 }
269 
270 /*
271  * kern_sigaction
272  * sigaction
273  * freebsd4_sigaction
274  * osigaction
275  *
276  * MPSAFE
277  */
278 int
279 kern_sigaction(td, sig, act, oact, flags)
280 	struct thread *td;
281 	register int sig;
282 	struct sigaction *act, *oact;
283 	int flags;
284 {
285 	struct sigacts *ps;
286 	struct thread *td0;
287 	struct proc *p = td->td_proc;
288 
289 	if (!_SIG_VALID(sig))
290 		return (EINVAL);
291 
292 	PROC_LOCK(p);
293 	ps = p->p_sigacts;
294 	mtx_lock(&ps->ps_mtx);
295 	if (oact) {
296 		oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
297 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
298 		oact->sa_flags = 0;
299 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
300 			oact->sa_flags |= SA_ONSTACK;
301 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
302 			oact->sa_flags |= SA_RESTART;
303 		if (SIGISMEMBER(ps->ps_sigreset, sig))
304 			oact->sa_flags |= SA_RESETHAND;
305 		if (SIGISMEMBER(ps->ps_signodefer, sig))
306 			oact->sa_flags |= SA_NODEFER;
307 		if (SIGISMEMBER(ps->ps_siginfo, sig))
308 			oact->sa_flags |= SA_SIGINFO;
309 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
310 			oact->sa_flags |= SA_NOCLDSTOP;
311 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
312 			oact->sa_flags |= SA_NOCLDWAIT;
313 	}
314 	if (act) {
315 		if ((sig == SIGKILL || sig == SIGSTOP) &&
316 		    act->sa_handler != SIG_DFL) {
317 			mtx_unlock(&ps->ps_mtx);
318 			PROC_UNLOCK(p);
319 			return (EINVAL);
320 		}
321 
322 		/*
323 		 * Change setting atomically.
324 		 */
325 
326 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
327 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
328 		if (act->sa_flags & SA_SIGINFO) {
329 			ps->ps_sigact[_SIG_IDX(sig)] =
330 			    (__sighandler_t *)act->sa_sigaction;
331 			SIGADDSET(ps->ps_siginfo, sig);
332 		} else {
333 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
334 			SIGDELSET(ps->ps_siginfo, sig);
335 		}
336 		if (!(act->sa_flags & SA_RESTART))
337 			SIGADDSET(ps->ps_sigintr, sig);
338 		else
339 			SIGDELSET(ps->ps_sigintr, sig);
340 		if (act->sa_flags & SA_ONSTACK)
341 			SIGADDSET(ps->ps_sigonstack, sig);
342 		else
343 			SIGDELSET(ps->ps_sigonstack, sig);
344 		if (act->sa_flags & SA_RESETHAND)
345 			SIGADDSET(ps->ps_sigreset, sig);
346 		else
347 			SIGDELSET(ps->ps_sigreset, sig);
348 		if (act->sa_flags & SA_NODEFER)
349 			SIGADDSET(ps->ps_signodefer, sig);
350 		else
351 			SIGDELSET(ps->ps_signodefer, sig);
352 		if (sig == SIGCHLD) {
353 			if (act->sa_flags & SA_NOCLDSTOP)
354 				ps->ps_flag |= PS_NOCLDSTOP;
355 			else
356 				ps->ps_flag &= ~PS_NOCLDSTOP;
357 			if (act->sa_flags & SA_NOCLDWAIT) {
358 				/*
359 				 * Paranoia: since SA_NOCLDWAIT is implemented
360 				 * by reparenting the dying child to PID 1 (and
361 				 * trust it to reap the zombie), PID 1 itself
362 				 * is forbidden to set SA_NOCLDWAIT.
363 				 */
364 				if (p->p_pid == 1)
365 					ps->ps_flag &= ~PS_NOCLDWAIT;
366 				else
367 					ps->ps_flag |= PS_NOCLDWAIT;
368 			} else
369 				ps->ps_flag &= ~PS_NOCLDWAIT;
370 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
371 				ps->ps_flag |= PS_CLDSIGIGN;
372 			else
373 				ps->ps_flag &= ~PS_CLDSIGIGN;
374 		}
375 		/*
376 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
377 		 * and for signals set to SIG_DFL where the default is to
378 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
379 		 * have to restart the process.
380 		 */
381 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
382 		    (sigprop(sig) & SA_IGNORE &&
383 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
384 			if ((p->p_flag & P_SA) &&
385 			     SIGISMEMBER(p->p_siglist, sig)) {
386 				p->p_flag |= P_SIGEVENT;
387 				wakeup(&p->p_siglist);
388 			}
389 			/* never to be seen again */
390 			SIGDELSET(p->p_siglist, sig);
391 			mtx_lock_spin(&sched_lock);
392 			FOREACH_THREAD_IN_PROC(p, td0)
393 				SIGDELSET(td0->td_siglist, sig);
394 			mtx_unlock_spin(&sched_lock);
395 			if (sig != SIGCONT)
396 				/* easier in psignal */
397 				SIGADDSET(ps->ps_sigignore, sig);
398 			SIGDELSET(ps->ps_sigcatch, sig);
399 		} else {
400 			SIGDELSET(ps->ps_sigignore, sig);
401 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
402 				SIGDELSET(ps->ps_sigcatch, sig);
403 			else
404 				SIGADDSET(ps->ps_sigcatch, sig);
405 		}
406 #ifdef COMPAT_FREEBSD4
407 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
408 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
409 		    (flags & KSA_FREEBSD4) == 0)
410 			SIGDELSET(ps->ps_freebsd4, sig);
411 		else
412 			SIGADDSET(ps->ps_freebsd4, sig);
413 #endif
414 #ifdef COMPAT_43
415 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
416 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
417 		    (flags & KSA_OSIGSET) == 0)
418 			SIGDELSET(ps->ps_osigset, sig);
419 		else
420 			SIGADDSET(ps->ps_osigset, sig);
421 #endif
422 	}
423 	mtx_unlock(&ps->ps_mtx);
424 	PROC_UNLOCK(p);
425 	return (0);
426 }
427 
428 #ifndef _SYS_SYSPROTO_H_
429 struct sigaction_args {
430 	int	sig;
431 	struct	sigaction *act;
432 	struct	sigaction *oact;
433 };
434 #endif
435 /*
436  * MPSAFE
437  */
438 int
439 sigaction(td, uap)
440 	struct thread *td;
441 	register struct sigaction_args *uap;
442 {
443 	struct sigaction act, oact;
444 	register struct sigaction *actp, *oactp;
445 	int error;
446 
447 	actp = (uap->act != NULL) ? &act : NULL;
448 	oactp = (uap->oact != NULL) ? &oact : NULL;
449 	if (actp) {
450 		error = copyin(uap->act, actp, sizeof(act));
451 		if (error)
452 			return (error);
453 	}
454 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
455 	if (oactp && !error)
456 		error = copyout(oactp, uap->oact, sizeof(oact));
457 	return (error);
458 }
459 
460 #ifdef COMPAT_FREEBSD4
461 #ifndef _SYS_SYSPROTO_H_
462 struct freebsd4_sigaction_args {
463 	int	sig;
464 	struct	sigaction *act;
465 	struct	sigaction *oact;
466 };
467 #endif
468 /*
469  * MPSAFE
470  */
471 int
472 freebsd4_sigaction(td, uap)
473 	struct thread *td;
474 	register struct freebsd4_sigaction_args *uap;
475 {
476 	struct sigaction act, oact;
477 	register struct sigaction *actp, *oactp;
478 	int error;
479 
480 
481 	actp = (uap->act != NULL) ? &act : NULL;
482 	oactp = (uap->oact != NULL) ? &oact : NULL;
483 	if (actp) {
484 		error = copyin(uap->act, actp, sizeof(act));
485 		if (error)
486 			return (error);
487 	}
488 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
489 	if (oactp && !error)
490 		error = copyout(oactp, uap->oact, sizeof(oact));
491 	return (error);
492 }
493 #endif	/* COMAPT_FREEBSD4 */
494 
495 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
496 #ifndef _SYS_SYSPROTO_H_
497 struct osigaction_args {
498 	int	signum;
499 	struct	osigaction *nsa;
500 	struct	osigaction *osa;
501 };
502 #endif
503 /*
504  * MPSAFE
505  */
506 int
507 osigaction(td, uap)
508 	struct thread *td;
509 	register struct osigaction_args *uap;
510 {
511 	struct osigaction sa;
512 	struct sigaction nsa, osa;
513 	register struct sigaction *nsap, *osap;
514 	int error;
515 
516 	if (uap->signum <= 0 || uap->signum >= ONSIG)
517 		return (EINVAL);
518 
519 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
520 	osap = (uap->osa != NULL) ? &osa : NULL;
521 
522 	if (nsap) {
523 		error = copyin(uap->nsa, &sa, sizeof(sa));
524 		if (error)
525 			return (error);
526 		nsap->sa_handler = sa.sa_handler;
527 		nsap->sa_flags = sa.sa_flags;
528 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
529 	}
530 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
531 	if (osap && !error) {
532 		sa.sa_handler = osap->sa_handler;
533 		sa.sa_flags = osap->sa_flags;
534 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
535 		error = copyout(&sa, uap->osa, sizeof(sa));
536 	}
537 	return (error);
538 }
539 
540 #if !defined(__i386__) && !defined(__alpha__)
541 /* Avoid replicating the same stub everywhere */
542 int
543 osigreturn(td, uap)
544 	struct thread *td;
545 	struct osigreturn_args *uap;
546 {
547 
548 	return (nosys(td, (struct nosys_args *)uap));
549 }
550 #endif
551 #endif /* COMPAT_43 */
552 
553 /*
554  * Initialize signal state for process 0;
555  * set to ignore signals that are ignored by default.
556  */
557 void
558 siginit(p)
559 	struct proc *p;
560 {
561 	register int i;
562 	struct sigacts *ps;
563 
564 	PROC_LOCK(p);
565 	ps = p->p_sigacts;
566 	mtx_lock(&ps->ps_mtx);
567 	for (i = 1; i <= NSIG; i++)
568 		if (sigprop(i) & SA_IGNORE && i != SIGCONT)
569 			SIGADDSET(ps->ps_sigignore, i);
570 	mtx_unlock(&ps->ps_mtx);
571 	PROC_UNLOCK(p);
572 }
573 
574 /*
575  * Reset signals for an exec of the specified process.
576  */
577 void
578 execsigs(struct proc *p)
579 {
580 	struct sigacts *ps;
581 	int sig;
582 	struct thread *td;
583 
584 	/*
585 	 * Reset caught signals.  Held signals remain held
586 	 * through td_sigmask (unless they were caught,
587 	 * and are now ignored by default).
588 	 */
589 	PROC_LOCK_ASSERT(p, MA_OWNED);
590 	td = FIRST_THREAD_IN_PROC(p);
591 	ps = p->p_sigacts;
592 	mtx_lock(&ps->ps_mtx);
593 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
594 		sig = sig_ffs(&ps->ps_sigcatch);
595 		SIGDELSET(ps->ps_sigcatch, sig);
596 		if (sigprop(sig) & SA_IGNORE) {
597 			if (sig != SIGCONT)
598 				SIGADDSET(ps->ps_sigignore, sig);
599 			SIGDELSET(p->p_siglist, sig);
600 			/*
601 			 * There is only one thread at this point.
602 			 */
603 			SIGDELSET(td->td_siglist, sig);
604 		}
605 		ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
606 	}
607 	/*
608 	 * Reset stack state to the user stack.
609 	 * Clear set of signals caught on the signal stack.
610 	 */
611 	td->td_sigstk.ss_flags = SS_DISABLE;
612 	td->td_sigstk.ss_size = 0;
613 	td->td_sigstk.ss_sp = 0;
614 	td->td_pflags &= ~TDP_ALTSTACK;
615 	/*
616 	 * Reset no zombies if child dies flag as Solaris does.
617 	 */
618 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
619 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
620 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
621 	mtx_unlock(&ps->ps_mtx);
622 }
623 
624 /*
625  * kern_sigprocmask()
626  *
627  *	Manipulate signal mask.
628  */
629 int
630 kern_sigprocmask(td, how, set, oset, old)
631 	struct thread *td;
632 	int how;
633 	sigset_t *set, *oset;
634 	int old;
635 {
636 	int error;
637 
638 	PROC_LOCK(td->td_proc);
639 	if (oset != NULL)
640 		*oset = td->td_sigmask;
641 
642 	error = 0;
643 	if (set != NULL) {
644 		switch (how) {
645 		case SIG_BLOCK:
646 			SIG_CANTMASK(*set);
647 			SIGSETOR(td->td_sigmask, *set);
648 			break;
649 		case SIG_UNBLOCK:
650 			SIGSETNAND(td->td_sigmask, *set);
651 			signotify(td);
652 			break;
653 		case SIG_SETMASK:
654 			SIG_CANTMASK(*set);
655 			if (old)
656 				SIGSETLO(td->td_sigmask, *set);
657 			else
658 				td->td_sigmask = *set;
659 			signotify(td);
660 			break;
661 		default:
662 			error = EINVAL;
663 			break;
664 		}
665 	}
666 	PROC_UNLOCK(td->td_proc);
667 	return (error);
668 }
669 
670 /*
671  * sigprocmask() - MP SAFE
672  */
673 
674 #ifndef _SYS_SYSPROTO_H_
675 struct sigprocmask_args {
676 	int	how;
677 	const sigset_t *set;
678 	sigset_t *oset;
679 };
680 #endif
681 int
682 sigprocmask(td, uap)
683 	register struct thread *td;
684 	struct sigprocmask_args *uap;
685 {
686 	sigset_t set, oset;
687 	sigset_t *setp, *osetp;
688 	int error;
689 
690 	setp = (uap->set != NULL) ? &set : NULL;
691 	osetp = (uap->oset != NULL) ? &oset : NULL;
692 	if (setp) {
693 		error = copyin(uap->set, setp, sizeof(set));
694 		if (error)
695 			return (error);
696 	}
697 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
698 	if (osetp && !error) {
699 		error = copyout(osetp, uap->oset, sizeof(oset));
700 	}
701 	return (error);
702 }
703 
704 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
705 /*
706  * osigprocmask() - MP SAFE
707  */
708 #ifndef _SYS_SYSPROTO_H_
709 struct osigprocmask_args {
710 	int	how;
711 	osigset_t mask;
712 };
713 #endif
714 int
715 osigprocmask(td, uap)
716 	register struct thread *td;
717 	struct osigprocmask_args *uap;
718 {
719 	sigset_t set, oset;
720 	int error;
721 
722 	OSIG2SIG(uap->mask, set);
723 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
724 	SIG2OSIG(oset, td->td_retval[0]);
725 	return (error);
726 }
727 #endif /* COMPAT_43 */
728 
729 #ifndef _SYS_SYSPROTO_H_
730 struct sigpending_args {
731 	sigset_t	*set;
732 };
733 #endif
734 /*
735  * MPSAFE
736  */
737 int
738 sigwait(struct thread *td, struct sigwait_args *uap)
739 {
740 	siginfo_t info;
741 	sigset_t set;
742 	int error;
743 
744 	error = copyin(uap->set, &set, sizeof(set));
745 	if (error) {
746 		td->td_retval[0] = error;
747 		return (0);
748 	}
749 
750 	error = kern_sigtimedwait(td, set, &info, NULL);
751 	if (error) {
752 		if (error == ERESTART)
753 			return (error);
754 		td->td_retval[0] = error;
755 		return (0);
756 	}
757 
758 	error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
759 	/* Repost if we got an error. */
760 	if (error && info.si_signo) {
761 		PROC_LOCK(td->td_proc);
762 		tdsignal(td, info.si_signo, SIGTARGET_TD);
763 		PROC_UNLOCK(td->td_proc);
764 	}
765 	td->td_retval[0] = error;
766 	return (0);
767 }
768 /*
769  * MPSAFE
770  */
771 int
772 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
773 {
774 	struct timespec ts;
775 	struct timespec *timeout;
776 	sigset_t set;
777 	siginfo_t info;
778 	int error;
779 
780 	if (uap->timeout) {
781 		error = copyin(uap->timeout, &ts, sizeof(ts));
782 		if (error)
783 			return (error);
784 
785 		timeout = &ts;
786 	} else
787 		timeout = NULL;
788 
789 	error = copyin(uap->set, &set, sizeof(set));
790 	if (error)
791 		return (error);
792 
793 	error = kern_sigtimedwait(td, set, &info, timeout);
794 	if (error)
795 		return (error);
796 
797 	if (uap->info)
798 		error = copyout(&info, uap->info, sizeof(info));
799 	/* Repost if we got an error. */
800 	if (error && info.si_signo) {
801 		PROC_LOCK(td->td_proc);
802 		tdsignal(td, info.si_signo, SIGTARGET_TD);
803 		PROC_UNLOCK(td->td_proc);
804 	} else {
805 		td->td_retval[0] = info.si_signo;
806 	}
807 	return (error);
808 }
809 
810 /*
811  * MPSAFE
812  */
813 int
814 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
815 {
816 	siginfo_t info;
817 	sigset_t set;
818 	int error;
819 
820 	error = copyin(uap->set, &set, sizeof(set));
821 	if (error)
822 		return (error);
823 
824 	error = kern_sigtimedwait(td, set, &info, NULL);
825 	if (error)
826 		return (error);
827 
828 	if (uap->info)
829 		error = copyout(&info, uap->info, sizeof(info));
830 	/* Repost if we got an error. */
831 	if (error && info.si_signo) {
832 		PROC_LOCK(td->td_proc);
833 		tdsignal(td, info.si_signo, SIGTARGET_TD);
834 		PROC_UNLOCK(td->td_proc);
835 	} else {
836 		td->td_retval[0] = info.si_signo;
837 	}
838 	return (error);
839 }
840 
841 static int
842 kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
843     struct timespec *timeout)
844 {
845 	struct sigacts *ps;
846 	sigset_t savedmask;
847 	struct proc *p;
848 	int error, sig, hz, i, timevalid = 0;
849 	struct timespec rts, ets, ts;
850 	struct timeval tv;
851 
852 	p = td->td_proc;
853 	error = 0;
854 	sig = 0;
855 	SIG_CANTMASK(waitset);
856 
857 	PROC_LOCK(p);
858 	ps = p->p_sigacts;
859 	savedmask = td->td_sigmask;
860 	if (timeout) {
861 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
862 			timevalid = 1;
863 			getnanouptime(&rts);
864 		 	ets = rts;
865 			timespecadd(&ets, timeout);
866 		}
867 	}
868 
869 again:
870 	for (i = 1; i <= _SIG_MAXSIG; ++i) {
871 		if (!SIGISMEMBER(waitset, i))
872 			continue;
873 		if (SIGISMEMBER(td->td_siglist, i)) {
874 			SIGFILLSET(td->td_sigmask);
875 			SIG_CANTMASK(td->td_sigmask);
876 			SIGDELSET(td->td_sigmask, i);
877 			mtx_lock(&ps->ps_mtx);
878 			sig = cursig(td);
879 			i = 0;
880 			mtx_unlock(&ps->ps_mtx);
881 		} else if (SIGISMEMBER(p->p_siglist, i)) {
882 			if (p->p_flag & P_SA) {
883 				p->p_flag |= P_SIGEVENT;
884 				wakeup(&p->p_siglist);
885 			}
886 			SIGDELSET(p->p_siglist, i);
887 			SIGADDSET(td->td_siglist, i);
888 			SIGFILLSET(td->td_sigmask);
889 			SIG_CANTMASK(td->td_sigmask);
890 			SIGDELSET(td->td_sigmask, i);
891 			mtx_lock(&ps->ps_mtx);
892 			sig = cursig(td);
893 			i = 0;
894 			mtx_unlock(&ps->ps_mtx);
895 		}
896 		if (sig)
897 			goto out;
898 	}
899 	if (error)
900 		goto out;
901 
902 	/*
903 	 * POSIX says this must be checked after looking for pending
904 	 * signals.
905 	 */
906 	if (timeout) {
907 		if (!timevalid) {
908 			error = EINVAL;
909 			goto out;
910 		}
911 		getnanouptime(&rts);
912 		if (timespeccmp(&rts, &ets, >=)) {
913 			error = EAGAIN;
914 			goto out;
915 		}
916 		ts = ets;
917 		timespecsub(&ts, &rts);
918 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
919 		hz = tvtohz(&tv);
920 	} else
921 		hz = 0;
922 
923 	td->td_sigmask = savedmask;
924 	SIGSETNAND(td->td_sigmask, waitset);
925 	signotify(td);
926 	error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
927 	if (timeout) {
928 		if (error == ERESTART) {
929 			/* timeout can not be restarted. */
930 			error = EINTR;
931 		} else if (error == EAGAIN) {
932 			/* will calculate timeout by ourself. */
933 			error = 0;
934 		}
935 	}
936 	goto again;
937 
938 out:
939 	td->td_sigmask = savedmask;
940 	signotify(td);
941 	if (sig) {
942 		sig_t action;
943 
944 		error = 0;
945 		mtx_lock(&ps->ps_mtx);
946 		action = ps->ps_sigact[_SIG_IDX(sig)];
947 		mtx_unlock(&ps->ps_mtx);
948 #ifdef KTRACE
949 		if (KTRPOINT(td, KTR_PSIG))
950 			ktrpsig(sig, action, &td->td_sigmask, 0);
951 #endif
952 		_STOPEVENT(p, S_SIG, sig);
953 
954 		SIGDELSET(td->td_siglist, sig);
955 		bzero(info, sizeof(*info));
956 		info->si_signo = sig;
957 		info->si_code = 0;
958 	}
959 	PROC_UNLOCK(p);
960 	return (error);
961 }
962 
963 /*
964  * MPSAFE
965  */
966 int
967 sigpending(td, uap)
968 	struct thread *td;
969 	struct sigpending_args *uap;
970 {
971 	struct proc *p = td->td_proc;
972 	sigset_t siglist;
973 
974 	PROC_LOCK(p);
975 	siglist = p->p_siglist;
976 	SIGSETOR(siglist, td->td_siglist);
977 	PROC_UNLOCK(p);
978 	return (copyout(&siglist, uap->set, sizeof(sigset_t)));
979 }
980 
981 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
982 #ifndef _SYS_SYSPROTO_H_
983 struct osigpending_args {
984 	int	dummy;
985 };
986 #endif
987 /*
988  * MPSAFE
989  */
990 int
991 osigpending(td, uap)
992 	struct thread *td;
993 	struct osigpending_args *uap;
994 {
995 	struct proc *p = td->td_proc;
996 	sigset_t siglist;
997 
998 	PROC_LOCK(p);
999 	siglist = p->p_siglist;
1000 	SIGSETOR(siglist, td->td_siglist);
1001 	PROC_UNLOCK(p);
1002 	SIG2OSIG(siglist, td->td_retval[0]);
1003 	return (0);
1004 }
1005 #endif /* COMPAT_43 */
1006 
1007 #if defined(COMPAT_43)
1008 /*
1009  * Generalized interface signal handler, 4.3-compatible.
1010  */
1011 #ifndef _SYS_SYSPROTO_H_
1012 struct osigvec_args {
1013 	int	signum;
1014 	struct	sigvec *nsv;
1015 	struct	sigvec *osv;
1016 };
1017 #endif
1018 /*
1019  * MPSAFE
1020  */
1021 /* ARGSUSED */
1022 int
1023 osigvec(td, uap)
1024 	struct thread *td;
1025 	register struct osigvec_args *uap;
1026 {
1027 	struct sigvec vec;
1028 	struct sigaction nsa, osa;
1029 	register struct sigaction *nsap, *osap;
1030 	int error;
1031 
1032 	if (uap->signum <= 0 || uap->signum >= ONSIG)
1033 		return (EINVAL);
1034 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
1035 	osap = (uap->osv != NULL) ? &osa : NULL;
1036 	if (nsap) {
1037 		error = copyin(uap->nsv, &vec, sizeof(vec));
1038 		if (error)
1039 			return (error);
1040 		nsap->sa_handler = vec.sv_handler;
1041 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1042 		nsap->sa_flags = vec.sv_flags;
1043 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1044 	}
1045 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1046 	if (osap && !error) {
1047 		vec.sv_handler = osap->sa_handler;
1048 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
1049 		vec.sv_flags = osap->sa_flags;
1050 		vec.sv_flags &= ~SA_NOCLDWAIT;
1051 		vec.sv_flags ^= SA_RESTART;
1052 		error = copyout(&vec, uap->osv, sizeof(vec));
1053 	}
1054 	return (error);
1055 }
1056 
1057 #ifndef _SYS_SYSPROTO_H_
1058 struct osigblock_args {
1059 	int	mask;
1060 };
1061 #endif
1062 /*
1063  * MPSAFE
1064  */
1065 int
1066 osigblock(td, uap)
1067 	register struct thread *td;
1068 	struct osigblock_args *uap;
1069 {
1070 	struct proc *p = td->td_proc;
1071 	sigset_t set;
1072 
1073 	OSIG2SIG(uap->mask, set);
1074 	SIG_CANTMASK(set);
1075 	PROC_LOCK(p);
1076 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1077 	SIGSETOR(td->td_sigmask, set);
1078 	PROC_UNLOCK(p);
1079 	return (0);
1080 }
1081 
1082 #ifndef _SYS_SYSPROTO_H_
1083 struct osigsetmask_args {
1084 	int	mask;
1085 };
1086 #endif
1087 /*
1088  * MPSAFE
1089  */
1090 int
1091 osigsetmask(td, uap)
1092 	struct thread *td;
1093 	struct osigsetmask_args *uap;
1094 {
1095 	struct proc *p = td->td_proc;
1096 	sigset_t set;
1097 
1098 	OSIG2SIG(uap->mask, set);
1099 	SIG_CANTMASK(set);
1100 	PROC_LOCK(p);
1101 	SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1102 	SIGSETLO(td->td_sigmask, set);
1103 	signotify(td);
1104 	PROC_UNLOCK(p);
1105 	return (0);
1106 }
1107 #endif /* COMPAT_43 */
1108 
1109 /*
1110  * Suspend process until signal, providing mask to be set
1111  * in the meantime.
1112  ***** XXXKSE this doesn't make sense under KSE.
1113  ***** Do we suspend the thread or all threads in the process?
1114  ***** How do we suspend threads running NOW on another processor?
1115  */
1116 #ifndef _SYS_SYSPROTO_H_
1117 struct sigsuspend_args {
1118 	const sigset_t *sigmask;
1119 };
1120 #endif
1121 /*
1122  * MPSAFE
1123  */
1124 /* ARGSUSED */
1125 int
1126 sigsuspend(td, uap)
1127 	struct thread *td;
1128 	struct sigsuspend_args *uap;
1129 {
1130 	sigset_t mask;
1131 	int error;
1132 
1133 	error = copyin(uap->sigmask, &mask, sizeof(mask));
1134 	if (error)
1135 		return (error);
1136 	return (kern_sigsuspend(td, mask));
1137 }
1138 
1139 int
1140 kern_sigsuspend(struct thread *td, sigset_t mask)
1141 {
1142 	struct proc *p = td->td_proc;
1143 
1144 	/*
1145 	 * When returning from sigsuspend, we want
1146 	 * the old mask to be restored after the
1147 	 * signal handler has finished.  Thus, we
1148 	 * save it here and mark the sigacts structure
1149 	 * to indicate this.
1150 	 */
1151 	PROC_LOCK(p);
1152 	td->td_oldsigmask = td->td_sigmask;
1153 	td->td_pflags |= TDP_OLDMASK;
1154 	SIG_CANTMASK(mask);
1155 	td->td_sigmask = mask;
1156 	signotify(td);
1157 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1158 		/* void */;
1159 	PROC_UNLOCK(p);
1160 	/* always return EINTR rather than ERESTART... */
1161 	return (EINTR);
1162 }
1163 
1164 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
1165 /*
1166  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
1167  * convention: libc stub passes mask, not pointer, to save a copyin.
1168  */
1169 #ifndef _SYS_SYSPROTO_H_
1170 struct osigsuspend_args {
1171 	osigset_t mask;
1172 };
1173 #endif
1174 /*
1175  * MPSAFE
1176  */
1177 /* ARGSUSED */
1178 int
1179 osigsuspend(td, uap)
1180 	struct thread *td;
1181 	struct osigsuspend_args *uap;
1182 {
1183 	struct proc *p = td->td_proc;
1184 	sigset_t mask;
1185 
1186 	PROC_LOCK(p);
1187 	td->td_oldsigmask = td->td_sigmask;
1188 	td->td_pflags |= TDP_OLDMASK;
1189 	OSIG2SIG(uap->mask, mask);
1190 	SIG_CANTMASK(mask);
1191 	SIGSETLO(td->td_sigmask, mask);
1192 	signotify(td);
1193 	while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1194 		/* void */;
1195 	PROC_UNLOCK(p);
1196 	/* always return EINTR rather than ERESTART... */
1197 	return (EINTR);
1198 }
1199 #endif /* COMPAT_43 */
1200 
1201 #if defined(COMPAT_43)
1202 #ifndef _SYS_SYSPROTO_H_
1203 struct osigstack_args {
1204 	struct	sigstack *nss;
1205 	struct	sigstack *oss;
1206 };
1207 #endif
1208 /*
1209  * MPSAFE
1210  */
1211 /* ARGSUSED */
1212 int
1213 osigstack(td, uap)
1214 	struct thread *td;
1215 	register struct osigstack_args *uap;
1216 {
1217 	struct sigstack nss, oss;
1218 	int error = 0;
1219 
1220 	if (uap->nss != NULL) {
1221 		error = copyin(uap->nss, &nss, sizeof(nss));
1222 		if (error)
1223 			return (error);
1224 	}
1225 	oss.ss_sp = td->td_sigstk.ss_sp;
1226 	oss.ss_onstack = sigonstack(cpu_getstack(td));
1227 	if (uap->nss != NULL) {
1228 		td->td_sigstk.ss_sp = nss.ss_sp;
1229 		td->td_sigstk.ss_size = 0;
1230 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1231 		td->td_pflags |= TDP_ALTSTACK;
1232 	}
1233 	if (uap->oss != NULL)
1234 		error = copyout(&oss, uap->oss, sizeof(oss));
1235 
1236 	return (error);
1237 }
1238 #endif /* COMPAT_43 */
1239 
1240 #ifndef _SYS_SYSPROTO_H_
1241 struct sigaltstack_args {
1242 	stack_t	*ss;
1243 	stack_t	*oss;
1244 };
1245 #endif
1246 /*
1247  * MPSAFE
1248  */
1249 /* ARGSUSED */
1250 int
1251 sigaltstack(td, uap)
1252 	struct thread *td;
1253 	register struct sigaltstack_args *uap;
1254 {
1255 	stack_t ss, oss;
1256 	int error;
1257 
1258 	if (uap->ss != NULL) {
1259 		error = copyin(uap->ss, &ss, sizeof(ss));
1260 		if (error)
1261 			return (error);
1262 	}
1263 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1264 	    (uap->oss != NULL) ? &oss : NULL);
1265 	if (error)
1266 		return (error);
1267 	if (uap->oss != NULL)
1268 		error = copyout(&oss, uap->oss, sizeof(stack_t));
1269 	return (error);
1270 }
1271 
1272 int
1273 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1274 {
1275 	struct proc *p = td->td_proc;
1276 	int oonstack;
1277 
1278 	oonstack = sigonstack(cpu_getstack(td));
1279 
1280 	if (oss != NULL) {
1281 		*oss = td->td_sigstk;
1282 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1283 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1284 	}
1285 
1286 	if (ss != NULL) {
1287 		if (oonstack)
1288 			return (EPERM);
1289 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1290 			return (EINVAL);
1291 		if (!(ss->ss_flags & SS_DISABLE)) {
1292 			if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1293 				return (ENOMEM);
1294 			}
1295 			td->td_sigstk = *ss;
1296 			td->td_pflags |= TDP_ALTSTACK;
1297 		} else {
1298 			td->td_pflags &= ~TDP_ALTSTACK;
1299 		}
1300 	}
1301 	return (0);
1302 }
1303 
1304 /*
1305  * Common code for kill process group/broadcast kill.
1306  * cp is calling process.
1307  */
1308 static int
1309 killpg1(td, sig, pgid, all)
1310 	register struct thread *td;
1311 	int sig, pgid, all;
1312 {
1313 	register struct proc *p;
1314 	struct pgrp *pgrp;
1315 	int nfound = 0;
1316 
1317 	if (all) {
1318 		/*
1319 		 * broadcast
1320 		 */
1321 		sx_slock(&allproc_lock);
1322 		LIST_FOREACH(p, &allproc, p_list) {
1323 			PROC_LOCK(p);
1324 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1325 			    p == td->td_proc) {
1326 				PROC_UNLOCK(p);
1327 				continue;
1328 			}
1329 			if (p_cansignal(td, p, sig) == 0) {
1330 				nfound++;
1331 				if (sig)
1332 					psignal(p, sig);
1333 			}
1334 			PROC_UNLOCK(p);
1335 		}
1336 		sx_sunlock(&allproc_lock);
1337 	} else {
1338 		sx_slock(&proctree_lock);
1339 		if (pgid == 0) {
1340 			/*
1341 			 * zero pgid means send to my process group.
1342 			 */
1343 			pgrp = td->td_proc->p_pgrp;
1344 			PGRP_LOCK(pgrp);
1345 		} else {
1346 			pgrp = pgfind(pgid);
1347 			if (pgrp == NULL) {
1348 				sx_sunlock(&proctree_lock);
1349 				return (ESRCH);
1350 			}
1351 		}
1352 		sx_sunlock(&proctree_lock);
1353 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1354 			PROC_LOCK(p);
1355 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM) {
1356 				PROC_UNLOCK(p);
1357 				continue;
1358 			}
1359 			if (p_cansignal(td, p, sig) == 0) {
1360 				nfound++;
1361 				if (sig)
1362 					psignal(p, sig);
1363 			}
1364 			PROC_UNLOCK(p);
1365 		}
1366 		PGRP_UNLOCK(pgrp);
1367 	}
1368 	return (nfound ? 0 : ESRCH);
1369 }
1370 
1371 #ifndef _SYS_SYSPROTO_H_
1372 struct kill_args {
1373 	int	pid;
1374 	int	signum;
1375 };
1376 #endif
1377 /*
1378  * MPSAFE
1379  */
1380 /* ARGSUSED */
1381 int
1382 kill(td, uap)
1383 	register struct thread *td;
1384 	register struct kill_args *uap;
1385 {
1386 	register struct proc *p;
1387 	int error;
1388 
1389 	if ((u_int)uap->signum > _SIG_MAXSIG)
1390 		return (EINVAL);
1391 
1392 	if (uap->pid > 0) {
1393 		/* kill single process */
1394 		if ((p = pfind(uap->pid)) == NULL) {
1395 			if ((p = zpfind(uap->pid)) == NULL)
1396 				return (ESRCH);
1397 		}
1398 		error = p_cansignal(td, p, uap->signum);
1399 		if (error == 0 && uap->signum)
1400 			psignal(p, uap->signum);
1401 		PROC_UNLOCK(p);
1402 		return (error);
1403 	}
1404 	switch (uap->pid) {
1405 	case -1:		/* broadcast signal */
1406 		return (killpg1(td, uap->signum, 0, 1));
1407 	case 0:			/* signal own process group */
1408 		return (killpg1(td, uap->signum, 0, 0));
1409 	default:		/* negative explicit process group */
1410 		return (killpg1(td, uap->signum, -uap->pid, 0));
1411 	}
1412 	/* NOTREACHED */
1413 }
1414 
1415 #if defined(COMPAT_43)
1416 #ifndef _SYS_SYSPROTO_H_
1417 struct okillpg_args {
1418 	int	pgid;
1419 	int	signum;
1420 };
1421 #endif
1422 /*
1423  * MPSAFE
1424  */
1425 /* ARGSUSED */
1426 int
1427 okillpg(td, uap)
1428 	struct thread *td;
1429 	register struct okillpg_args *uap;
1430 {
1431 
1432 	if ((u_int)uap->signum > _SIG_MAXSIG)
1433 		return (EINVAL);
1434 	return (killpg1(td, uap->signum, uap->pgid, 0));
1435 }
1436 #endif /* COMPAT_43 */
1437 
1438 /*
1439  * Send a signal to a process group.
1440  */
1441 void
1442 gsignal(pgid, sig)
1443 	int pgid, sig;
1444 {
1445 	struct pgrp *pgrp;
1446 
1447 	if (pgid != 0) {
1448 		sx_slock(&proctree_lock);
1449 		pgrp = pgfind(pgid);
1450 		sx_sunlock(&proctree_lock);
1451 		if (pgrp != NULL) {
1452 			pgsignal(pgrp, sig, 0);
1453 			PGRP_UNLOCK(pgrp);
1454 		}
1455 	}
1456 }
1457 
1458 /*
1459  * Send a signal to a process group.  If checktty is 1,
1460  * limit to members which have a controlling terminal.
1461  */
1462 void
1463 pgsignal(pgrp, sig, checkctty)
1464 	struct pgrp *pgrp;
1465 	int sig, checkctty;
1466 {
1467 	register struct proc *p;
1468 
1469 	if (pgrp) {
1470 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1471 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1472 			PROC_LOCK(p);
1473 			if (checkctty == 0 || p->p_flag & P_CONTROLT)
1474 				psignal(p, sig);
1475 			PROC_UNLOCK(p);
1476 		}
1477 	}
1478 }
1479 
1480 /*
1481  * Send a signal caused by a trap to the current thread.
1482  * If it will be caught immediately, deliver it with correct code.
1483  * Otherwise, post it normally.
1484  *
1485  * MPSAFE
1486  */
1487 void
1488 trapsignal(struct thread *td, int sig, u_long code)
1489 {
1490 	struct sigacts *ps;
1491 	struct proc *p;
1492 	siginfo_t siginfo;
1493 	int error;
1494 
1495 	p = td->td_proc;
1496 	if (td->td_pflags & TDP_SA) {
1497 		if (td->td_mailbox == NULL)
1498 			thread_user_enter(td);
1499 		PROC_LOCK(p);
1500 		SIGDELSET(td->td_sigmask, sig);
1501 		mtx_lock_spin(&sched_lock);
1502 		/*
1503 		 * Force scheduling an upcall, so UTS has chance to
1504 		 * process the signal before thread runs again in
1505 		 * userland.
1506 		 */
1507 		if (td->td_upcall)
1508 			td->td_upcall->ku_flags |= KUF_DOUPCALL;
1509 		mtx_unlock_spin(&sched_lock);
1510 	} else {
1511 		PROC_LOCK(p);
1512 	}
1513 	ps = p->p_sigacts;
1514 	mtx_lock(&ps->ps_mtx);
1515 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1516 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1517 		p->p_stats->p_ru.ru_nsignals++;
1518 #ifdef KTRACE
1519 		if (KTRPOINT(curthread, KTR_PSIG))
1520 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1521 			    &td->td_sigmask, code);
1522 #endif
1523 		if (!(td->td_pflags & TDP_SA))
1524 			(*p->p_sysent->sv_sendsig)(
1525 				ps->ps_sigact[_SIG_IDX(sig)], sig,
1526 				&td->td_sigmask, code);
1527 		else if (td->td_mailbox == NULL) {
1528 			mtx_unlock(&ps->ps_mtx);
1529 			/* UTS caused a sync signal */
1530 			p->p_code = code;	/* XXX for core dump/debugger */
1531 			p->p_sig = sig;		/* XXX to verify code */
1532 			sigexit(td, sig);
1533 		} else {
1534 			cpu_thread_siginfo(sig, code, &siginfo);
1535 			mtx_unlock(&ps->ps_mtx);
1536 			SIGADDSET(td->td_sigmask, sig);
1537 			PROC_UNLOCK(p);
1538 			error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
1539 			    sizeof(siginfo));
1540 			PROC_LOCK(p);
1541 			/* UTS memory corrupted */
1542 			if (error)
1543 				sigexit(td, SIGSEGV);
1544 			mtx_lock(&ps->ps_mtx);
1545 		}
1546 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1547 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
1548 			SIGADDSET(td->td_sigmask, sig);
1549 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1550 			/*
1551 			 * See kern_sigaction() for origin of this code.
1552 			 */
1553 			SIGDELSET(ps->ps_sigcatch, sig);
1554 			if (sig != SIGCONT &&
1555 			    sigprop(sig) & SA_IGNORE)
1556 				SIGADDSET(ps->ps_sigignore, sig);
1557 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1558 		}
1559 		mtx_unlock(&ps->ps_mtx);
1560 	} else {
1561 		mtx_unlock(&ps->ps_mtx);
1562 		p->p_code = code;	/* XXX for core dump/debugger */
1563 		p->p_sig = sig;		/* XXX to verify code */
1564 		tdsignal(td, sig, SIGTARGET_TD);
1565 	}
1566 	PROC_UNLOCK(p);
1567 }
1568 
1569 static struct thread *
1570 sigtd(struct proc *p, int sig, int prop)
1571 {
1572 	struct thread *td, *signal_td;
1573 
1574 	PROC_LOCK_ASSERT(p, MA_OWNED);
1575 
1576 	/*
1577 	 * Check if current thread can handle the signal without
1578 	 * switching conetxt to another thread.
1579 	 */
1580 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1581 		return (curthread);
1582 	signal_td = NULL;
1583 	mtx_lock_spin(&sched_lock);
1584 	FOREACH_THREAD_IN_PROC(p, td) {
1585 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
1586 			signal_td = td;
1587 			break;
1588 		}
1589 	}
1590 	if (signal_td == NULL)
1591 		signal_td = FIRST_THREAD_IN_PROC(p);
1592 	mtx_unlock_spin(&sched_lock);
1593 	return (signal_td);
1594 }
1595 
1596 /*
1597  * Send the signal to the process.  If the signal has an action, the action
1598  * is usually performed by the target process rather than the caller; we add
1599  * the signal to the set of pending signals for the process.
1600  *
1601  * Exceptions:
1602  *   o When a stop signal is sent to a sleeping process that takes the
1603  *     default action, the process is stopped without awakening it.
1604  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
1605  *     regardless of the signal action (eg, blocked or ignored).
1606  *
1607  * Other ignored signals are discarded immediately.
1608  *
1609  * MPSAFE
1610  */
1611 void
1612 psignal(struct proc *p, int sig)
1613 {
1614 	struct thread *td;
1615 	int prop;
1616 
1617 	if (!_SIG_VALID(sig))
1618 		panic("psignal(): invalid signal");
1619 
1620 	PROC_LOCK_ASSERT(p, MA_OWNED);
1621 	/*
1622 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
1623 	 */
1624 	if (p->p_state == PRS_ZOMBIE)
1625 		return;
1626 	prop = sigprop(sig);
1627 
1628 	/*
1629 	 * Find a thread to deliver the signal to.
1630 	 */
1631 	td = sigtd(p, sig, prop);
1632 
1633 	tdsignal(td, sig, SIGTARGET_P);
1634 }
1635 
1636 /*
1637  * MPSAFE
1638  */
1639 void
1640 tdsignal(struct thread *td, int sig, sigtarget_t target)
1641 {
1642 	sigset_t saved;
1643 	struct proc *p = td->td_proc;
1644 
1645 	if (p->p_flag & P_SA)
1646 		saved = p->p_siglist;
1647 	do_tdsignal(td, sig, target);
1648 	if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
1649 		if (!SIGSETEQ(saved, p->p_siglist)) {
1650 			/* pending set changed */
1651 			p->p_flag |= P_SIGEVENT;
1652 			wakeup(&p->p_siglist);
1653 		}
1654 	}
1655 }
1656 
1657 static void
1658 do_tdsignal(struct thread *td, int sig, sigtarget_t target)
1659 {
1660 	struct proc *p;
1661 	register sig_t action;
1662 	sigset_t *siglist;
1663 	struct thread *td0;
1664 	register int prop;
1665 	struct sigacts *ps;
1666 
1667 	if (!_SIG_VALID(sig))
1668 		panic("do_tdsignal(): invalid signal");
1669 
1670 	p = td->td_proc;
1671 	ps = p->p_sigacts;
1672 
1673 	PROC_LOCK_ASSERT(p, MA_OWNED);
1674 	KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1675 
1676 	prop = sigprop(sig);
1677 
1678 	/*
1679 	 * If the signal is blocked and not destined for this thread, then
1680 	 * assign it to the process so that we can find it later in the first
1681 	 * thread that unblocks it.  Otherwise, assign it to this thread now.
1682 	 */
1683 	if (target == SIGTARGET_TD) {
1684 		siglist = &td->td_siglist;
1685 	} else {
1686 		if (!SIGISMEMBER(td->td_sigmask, sig))
1687 			siglist = &td->td_siglist;
1688 		else
1689 			siglist = &p->p_siglist;
1690 	}
1691 
1692 	/*
1693 	 * If proc is traced, always give parent a chance;
1694 	 * if signal event is tracked by procfs, give *that*
1695 	 * a chance, as well.
1696 	 */
1697 	if ((p->p_flag & P_TRACED) || (p->p_stops & S_SIG)) {
1698 		action = SIG_DFL;
1699 	} else {
1700 		/*
1701 		 * If the signal is being ignored,
1702 		 * then we forget about it immediately.
1703 		 * (Note: we don't set SIGCONT in ps_sigignore,
1704 		 * and if it is set to SIG_IGN,
1705 		 * action will be SIG_DFL here.)
1706 		 */
1707 		mtx_lock(&ps->ps_mtx);
1708 		if (SIGISMEMBER(ps->ps_sigignore, sig) ||
1709 		    (p->p_flag & P_WEXIT)) {
1710 			mtx_unlock(&ps->ps_mtx);
1711 			return;
1712 		}
1713 		if (SIGISMEMBER(td->td_sigmask, sig))
1714 			action = SIG_HOLD;
1715 		else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1716 			action = SIG_CATCH;
1717 		else
1718 			action = SIG_DFL;
1719 		mtx_unlock(&ps->ps_mtx);
1720 	}
1721 
1722 	if (prop & SA_CONT) {
1723 		SIG_STOPSIGMASK(p->p_siglist);
1724 		/*
1725 		 * XXX Should investigate leaving STOP and CONT sigs only in
1726 		 * the proc's siglist.
1727 		 */
1728 		mtx_lock_spin(&sched_lock);
1729 		FOREACH_THREAD_IN_PROC(p, td0)
1730 			SIG_STOPSIGMASK(td0->td_siglist);
1731 		mtx_unlock_spin(&sched_lock);
1732 	}
1733 
1734 	if (prop & SA_STOP) {
1735 		/*
1736 		 * If sending a tty stop signal to a member of an orphaned
1737 		 * process group, discard the signal here if the action
1738 		 * is default; don't stop the process below if sleeping,
1739 		 * and don't clear any pending SIGCONT.
1740 		 */
1741 		if ((prop & SA_TTYSTOP) &&
1742 		    (p->p_pgrp->pg_jobc == 0) &&
1743 		    (action == SIG_DFL))
1744 		        return;
1745 		SIG_CONTSIGMASK(p->p_siglist);
1746 		mtx_lock_spin(&sched_lock);
1747 		FOREACH_THREAD_IN_PROC(p, td0)
1748 			SIG_CONTSIGMASK(td0->td_siglist);
1749 		mtx_unlock_spin(&sched_lock);
1750 		p->p_flag &= ~P_CONTINUED;
1751 	}
1752 
1753 	SIGADDSET(*siglist, sig);
1754 	signotify(td);			/* uses schedlock */
1755 	/*
1756 	 * Defer further processing for signals which are held,
1757 	 * except that stopped processes must be continued by SIGCONT.
1758 	 */
1759 	if (action == SIG_HOLD &&
1760 	    !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
1761 		return;
1762 	/*
1763 	 * SIGKILL: Remove procfs STOPEVENTs.
1764 	 */
1765 	if (sig == SIGKILL) {
1766 		/* from procfs_ioctl.c: PIOCBIC */
1767 		p->p_stops = 0;
1768 		/* from procfs_ioctl.c: PIOCCONT */
1769 		p->p_step = 0;
1770 		wakeup(&p->p_step);
1771 	}
1772 	/*
1773 	 * Some signals have a process-wide effect and a per-thread
1774 	 * component.  Most processing occurs when the process next
1775 	 * tries to cross the user boundary, however there are some
1776 	 * times when processing needs to be done immediatly, such as
1777 	 * waking up threads so that they can cross the user boundary.
1778 	 * We try do the per-process part here.
1779 	 */
1780 	if (P_SHOULDSTOP(p)) {
1781 		/*
1782 		 * The process is in stopped mode. All the threads should be
1783 		 * either winding down or already on the suspended queue.
1784 		 */
1785 		if (p->p_flag & P_TRACED) {
1786 			/*
1787 			 * The traced process is already stopped,
1788 			 * so no further action is necessary.
1789 			 * No signal can restart us.
1790 			 */
1791 			goto out;
1792 		}
1793 
1794 		if (sig == SIGKILL) {
1795 			/*
1796 			 * SIGKILL sets process running.
1797 			 * It will die elsewhere.
1798 			 * All threads must be restarted.
1799 			 */
1800 			p->p_flag &= ~P_STOPPED_SIG;
1801 			goto runfast;
1802 		}
1803 
1804 		if (prop & SA_CONT) {
1805 			/*
1806 			 * If SIGCONT is default (or ignored), we continue the
1807 			 * process but don't leave the signal in siglist as
1808 			 * it has no further action.  If SIGCONT is held, we
1809 			 * continue the process and leave the signal in
1810 			 * siglist.  If the process catches SIGCONT, let it
1811 			 * handle the signal itself.  If it isn't waiting on
1812 			 * an event, it goes back to run state.
1813 			 * Otherwise, process goes back to sleep state.
1814 			 */
1815 			p->p_flag &= ~P_STOPPED_SIG;
1816 			p->p_flag |= P_CONTINUED;
1817 			if (action == SIG_DFL) {
1818 				SIGDELSET(*siglist, sig);
1819 			} else if (action == SIG_CATCH) {
1820 				/*
1821 				 * The process wants to catch it so it needs
1822 				 * to run at least one thread, but which one?
1823 				 * It would seem that the answer would be to
1824 				 * run an upcall in the next KSE to run, and
1825 				 * deliver the signal that way. In a NON KSE
1826 				 * process, we need to make sure that the
1827 				 * single thread is runnable asap.
1828 				 * XXXKSE for now however, make them all run.
1829 				 */
1830 				goto runfast;
1831 			}
1832 			/*
1833 			 * The signal is not ignored or caught.
1834 			 */
1835 			mtx_lock_spin(&sched_lock);
1836 			thread_unsuspend(p);
1837 			mtx_unlock_spin(&sched_lock);
1838 			goto out;
1839 		}
1840 
1841 		if (prop & SA_STOP) {
1842 			/*
1843 			 * Already stopped, don't need to stop again
1844 			 * (If we did the shell could get confused).
1845 			 * Just make sure the signal STOP bit set.
1846 			 */
1847 			p->p_flag |= P_STOPPED_SIG;
1848 			SIGDELSET(*siglist, sig);
1849 			goto out;
1850 		}
1851 
1852 		/*
1853 		 * All other kinds of signals:
1854 		 * If a thread is sleeping interruptibly, simulate a
1855 		 * wakeup so that when it is continued it will be made
1856 		 * runnable and can look at the signal.  However, don't make
1857 		 * the PROCESS runnable, leave it stopped.
1858 		 * It may run a bit until it hits a thread_suspend_check().
1859 		 */
1860 		mtx_lock_spin(&sched_lock);
1861 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
1862 			sleepq_abort(td);
1863 		mtx_unlock_spin(&sched_lock);
1864 		goto out;
1865 		/*
1866 		 * Mutexes are short lived. Threads waiting on them will
1867 		 * hit thread_suspend_check() soon.
1868 		 */
1869 	}  else if (p->p_state == PRS_NORMAL) {
1870 		if ((p->p_flag & P_TRACED) || (action != SIG_DFL) ||
1871 			!(prop & SA_STOP)) {
1872 			mtx_lock_spin(&sched_lock);
1873 			tdsigwakeup(td, sig, action);
1874 			mtx_unlock_spin(&sched_lock);
1875 			goto out;
1876 		}
1877 		if (prop & SA_STOP) {
1878 			if (p->p_flag & P_PPWAIT)
1879 				goto out;
1880 			p->p_flag |= P_STOPPED_SIG;
1881 			p->p_xstat = sig;
1882 			p->p_xthread = td;
1883 			mtx_lock_spin(&sched_lock);
1884 			FOREACH_THREAD_IN_PROC(p, td0) {
1885 				if (TD_IS_SLEEPING(td0) &&
1886 				    (td0->td_flags & TDF_SINTR) &&
1887 				    !TD_IS_SUSPENDED(td0)) {
1888 					thread_suspend_one(td0);
1889 				} else if (td != td0) {
1890 					td0->td_flags |= TDF_ASTPENDING;
1891 				}
1892 			}
1893 			thread_stopped(p);
1894 			if (p->p_numthreads == p->p_suspcount) {
1895 				SIGDELSET(p->p_siglist, p->p_xstat);
1896 				FOREACH_THREAD_IN_PROC(p, td0)
1897 					SIGDELSET(td0->td_siglist, p->p_xstat);
1898 			}
1899 			mtx_unlock_spin(&sched_lock);
1900 			goto out;
1901 		}
1902 		else
1903 			goto runfast;
1904 		/* NOTREACHED */
1905 	} else {
1906 		/* Not in "NORMAL" state. discard the signal. */
1907 		SIGDELSET(*siglist, sig);
1908 		goto out;
1909 	}
1910 
1911 	/*
1912 	 * The process is not stopped so we need to apply the signal to all the
1913 	 * running threads.
1914 	 */
1915 
1916 runfast:
1917 	mtx_lock_spin(&sched_lock);
1918 	tdsigwakeup(td, sig, action);
1919 	thread_unsuspend(p);
1920 	mtx_unlock_spin(&sched_lock);
1921 out:
1922 	/* If we jump here, sched_lock should not be owned. */
1923 	mtx_assert(&sched_lock, MA_NOTOWNED);
1924 }
1925 
1926 /*
1927  * The force of a signal has been directed against a single
1928  * thread.  We need to see what we can do about knocking it
1929  * out of any sleep it may be in etc.
1930  */
1931 static void
1932 tdsigwakeup(struct thread *td, int sig, sig_t action)
1933 {
1934 	struct proc *p = td->td_proc;
1935 	register int prop;
1936 
1937 	PROC_LOCK_ASSERT(p, MA_OWNED);
1938 	mtx_assert(&sched_lock, MA_OWNED);
1939 	prop = sigprop(sig);
1940 
1941 	/*
1942 	 * Bring the priority of a thread up if we want it to get
1943 	 * killed in this lifetime.
1944 	 */
1945 	if (action == SIG_DFL && (prop & SA_KILL)) {
1946 		if (p->p_nice > 0)
1947 			sched_nice(td->td_proc, 0);
1948 		if (td->td_priority > PUSER)
1949 			sched_prio(td, PUSER);
1950 	}
1951 
1952 	if (TD_ON_SLEEPQ(td)) {
1953 		/*
1954 		 * If thread is sleeping uninterruptibly
1955 		 * we can't interrupt the sleep... the signal will
1956 		 * be noticed when the process returns through
1957 		 * trap() or syscall().
1958 		 */
1959 		if ((td->td_flags & TDF_SINTR) == 0)
1960 			return;
1961 		/*
1962 		 * Process is sleeping and traced.  Make it runnable
1963 		 * so it can discover the signal in issignal() and stop
1964 		 * for its parent.
1965 		 */
1966 		if (p->p_flag & P_TRACED) {
1967 			p->p_flag &= ~P_STOPPED_TRACE;
1968 		} else {
1969 			/*
1970 			 * If SIGCONT is default (or ignored) and process is
1971 			 * asleep, we are finished; the process should not
1972 			 * be awakened.
1973 			 */
1974 			if ((prop & SA_CONT) && action == SIG_DFL) {
1975 				SIGDELSET(p->p_siglist, sig);
1976 				/*
1977 				 * It may be on either list in this state.
1978 				 * Remove from both for now.
1979 				 */
1980 				SIGDELSET(td->td_siglist, sig);
1981 				return;
1982 			}
1983 
1984 			/*
1985 			 * Give low priority threads a better chance to run.
1986 			 */
1987 			if (td->td_priority > PUSER)
1988 				sched_prio(td, PUSER);
1989 		}
1990 		sleepq_abort(td);
1991 	} else {
1992 		/*
1993 		 * Other states do nothing with the signal immediately,
1994 		 * other than kicking ourselves if we are running.
1995 		 * It will either never be noticed, or noticed very soon.
1996 		 */
1997 #ifdef SMP
1998 		if (TD_IS_RUNNING(td) && td != curthread)
1999 			forward_signal(td);
2000 #endif
2001 	}
2002 }
2003 
2004 int
2005 ptracestop(struct thread *td, int sig)
2006 {
2007 	struct proc *p = td->td_proc;
2008 	struct thread *td0;
2009 
2010 	PROC_LOCK_ASSERT(p, MA_OWNED);
2011 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2012 	    &p->p_mtx.mtx_object, "Stopping for traced signal");
2013 
2014 	mtx_lock_spin(&sched_lock);
2015 	td->td_flags |= TDF_XSIG;
2016 	mtx_unlock_spin(&sched_lock);
2017 	td->td_xsig = sig;
2018 	while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2019 		if (p->p_flag & P_SINGLE_EXIT) {
2020 			mtx_lock_spin(&sched_lock);
2021 			td->td_flags &= ~TDF_XSIG;
2022 			mtx_unlock_spin(&sched_lock);
2023 			return (sig);
2024 		}
2025 		/*
2026 		 * Just make wait() to work, the last stopped thread
2027 		 * will win.
2028 		 */
2029 		p->p_xstat = sig;
2030 		p->p_xthread = td;
2031 		p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2032 		mtx_lock_spin(&sched_lock);
2033 		FOREACH_THREAD_IN_PROC(p, td0) {
2034 			if (TD_IS_SLEEPING(td0) &&
2035 			    (td0->td_flags & TDF_SINTR) &&
2036 			    !TD_IS_SUSPENDED(td0)) {
2037 				thread_suspend_one(td0);
2038 			} else if (td != td0) {
2039 				td0->td_flags |= TDF_ASTPENDING;
2040 			}
2041 		}
2042 stopme:
2043 		thread_stopped(p);
2044 		thread_suspend_one(td);
2045 		PROC_UNLOCK(p);
2046 		DROP_GIANT();
2047 		mi_switch(SW_VOL, NULL);
2048 		mtx_unlock_spin(&sched_lock);
2049 		PICKUP_GIANT();
2050 		PROC_LOCK(p);
2051 		if (!(p->p_flag & P_TRACED))
2052 			break;
2053 		if (td->td_flags & TDF_DBSUSPEND) {
2054 			if (p->p_flag & P_SINGLE_EXIT)
2055 				break;
2056 			mtx_lock_spin(&sched_lock);
2057 			goto stopme;
2058 		}
2059 	}
2060 	return (td->td_xsig);
2061 }
2062 
2063 /*
2064  * If the current process has received a signal (should be caught or cause
2065  * termination, should interrupt current syscall), return the signal number.
2066  * Stop signals with default action are processed immediately, then cleared;
2067  * they aren't returned.  This is checked after each entry to the system for
2068  * a syscall or trap (though this can usually be done without calling issignal
2069  * by checking the pending signal masks in cursig.) The normal call
2070  * sequence is
2071  *
2072  *	while (sig = cursig(curthread))
2073  *		postsig(sig);
2074  */
2075 static int
2076 issignal(td)
2077 	struct thread *td;
2078 {
2079 	struct proc *p;
2080 	struct sigacts *ps;
2081 	sigset_t sigpending;
2082 	int sig, prop, newsig;
2083 	struct thread *td0;
2084 
2085 	p = td->td_proc;
2086 	ps = p->p_sigacts;
2087 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2088 	PROC_LOCK_ASSERT(p, MA_OWNED);
2089 	for (;;) {
2090 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2091 
2092 		sigpending = td->td_siglist;
2093 		SIGSETNAND(sigpending, td->td_sigmask);
2094 
2095 		if (p->p_flag & P_PPWAIT)
2096 			SIG_STOPSIGMASK(sigpending);
2097 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2098 			return (0);
2099 		sig = sig_ffs(&sigpending);
2100 
2101 		if (p->p_stops & S_SIG) {
2102 			mtx_unlock(&ps->ps_mtx);
2103 			stopevent(p, S_SIG, sig);
2104 			mtx_lock(&ps->ps_mtx);
2105 		}
2106 
2107 		/*
2108 		 * We should see pending but ignored signals
2109 		 * only if P_TRACED was on when they were posted.
2110 		 */
2111 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2112 			SIGDELSET(td->td_siglist, sig);
2113 			if (td->td_pflags & TDP_SA)
2114 				SIGADDSET(td->td_sigmask, sig);
2115 			continue;
2116 		}
2117 		if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2118 			/*
2119 			 * If traced, always stop.
2120 			 */
2121 			mtx_unlock(&ps->ps_mtx);
2122 			newsig = ptracestop(td, sig);
2123 			mtx_lock(&ps->ps_mtx);
2124 
2125 			/*
2126 			 * If parent wants us to take the signal,
2127 			 * then it will leave it in p->p_xstat;
2128 			 * otherwise we just look for signals again.
2129 			 */
2130 			SIGDELSET(td->td_siglist, sig);	/* clear old signal */
2131 			if (td->td_pflags & TDP_SA)
2132 				SIGADDSET(td->td_sigmask, sig);
2133 			if (newsig == 0)
2134 				continue;
2135 			sig = newsig;
2136 			/*
2137 			 * If the traced bit got turned off, go back up
2138 			 * to the top to rescan signals.  This ensures
2139 			 * that p_sig* and p_sigact are consistent.
2140 			 */
2141 			if ((p->p_flag & P_TRACED) == 0)
2142 				continue;
2143 
2144 			/*
2145 			 * Put the new signal into td_siglist.  If the
2146 			 * signal is being masked, look for other signals.
2147 			 */
2148 			SIGADDSET(td->td_siglist, sig);
2149 			if (td->td_pflags & TDP_SA)
2150 				SIGDELSET(td->td_sigmask, sig);
2151 			if (SIGISMEMBER(td->td_sigmask, sig))
2152 				continue;
2153 			signotify(td);
2154 		}
2155 
2156 		prop = sigprop(sig);
2157 
2158 		/*
2159 		 * Decide whether the signal should be returned.
2160 		 * Return the signal's number, or fall through
2161 		 * to clear it from the pending mask.
2162 		 */
2163 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2164 
2165 		case (intptr_t)SIG_DFL:
2166 			/*
2167 			 * Don't take default actions on system processes.
2168 			 */
2169 			if (p->p_pid <= 1) {
2170 #ifdef DIAGNOSTIC
2171 				/*
2172 				 * Are you sure you want to ignore SIGSEGV
2173 				 * in init? XXX
2174 				 */
2175 				printf("Process (pid %lu) got signal %d\n",
2176 					(u_long)p->p_pid, sig);
2177 #endif
2178 				break;		/* == ignore */
2179 			}
2180 			/*
2181 			 * If there is a pending stop signal to process
2182 			 * with default action, stop here,
2183 			 * then clear the signal.  However,
2184 			 * if process is member of an orphaned
2185 			 * process group, ignore tty stop signals.
2186 			 */
2187 			if (prop & SA_STOP) {
2188 				if (p->p_flag & P_TRACED ||
2189 		    		    (p->p_pgrp->pg_jobc == 0 &&
2190 				     prop & SA_TTYSTOP))
2191 					break;	/* == ignore */
2192 				mtx_unlock(&ps->ps_mtx);
2193 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2194 				    &p->p_mtx.mtx_object, "Catching SIGSTOP");
2195 				p->p_flag |= P_STOPPED_SIG;
2196 				p->p_xstat = sig;
2197 				p->p_xthread = td;
2198 				mtx_lock_spin(&sched_lock);
2199 				FOREACH_THREAD_IN_PROC(p, td0) {
2200 					if (TD_IS_SLEEPING(td0) &&
2201 					    (td0->td_flags & TDF_SINTR) &&
2202 					    !TD_IS_SUSPENDED(td0)) {
2203 						thread_suspend_one(td0);
2204 					} else if (td != td0) {
2205 						td0->td_flags |= TDF_ASTPENDING;
2206 					}
2207 				}
2208 				thread_stopped(p);
2209 				thread_suspend_one(td);
2210 				PROC_UNLOCK(p);
2211 				DROP_GIANT();
2212 				mi_switch(SW_INVOL, NULL);
2213 				mtx_unlock_spin(&sched_lock);
2214 				PICKUP_GIANT();
2215 				PROC_LOCK(p);
2216 				mtx_lock(&ps->ps_mtx);
2217 				break;
2218 			} else if (prop & SA_IGNORE) {
2219 				/*
2220 				 * Except for SIGCONT, shouldn't get here.
2221 				 * Default action is to ignore; drop it.
2222 				 */
2223 				break;		/* == ignore */
2224 			} else
2225 				return (sig);
2226 			/*NOTREACHED*/
2227 
2228 		case (intptr_t)SIG_IGN:
2229 			/*
2230 			 * Masking above should prevent us ever trying
2231 			 * to take action on an ignored signal other
2232 			 * than SIGCONT, unless process is traced.
2233 			 */
2234 			if ((prop & SA_CONT) == 0 &&
2235 			    (p->p_flag & P_TRACED) == 0)
2236 				printf("issignal\n");
2237 			break;		/* == ignore */
2238 
2239 		default:
2240 			/*
2241 			 * This signal has an action, let
2242 			 * postsig() process it.
2243 			 */
2244 			return (sig);
2245 		}
2246 		SIGDELSET(td->td_siglist, sig);		/* take the signal! */
2247 	}
2248 	/* NOTREACHED */
2249 }
2250 
2251 /*
2252  * MPSAFE
2253  */
2254 void
2255 thread_stopped(struct proc *p)
2256 {
2257 	struct proc *p1 = curthread->td_proc;
2258 	struct sigacts *ps;
2259 	int n;
2260 
2261 	PROC_LOCK_ASSERT(p, MA_OWNED);
2262 	mtx_assert(&sched_lock, MA_OWNED);
2263 	n = p->p_suspcount;
2264 	if (p == p1)
2265 		n++;
2266 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2267 		mtx_unlock_spin(&sched_lock);
2268 		p->p_flag &= ~P_WAITED;
2269 		PROC_LOCK(p->p_pptr);
2270 		/*
2271 		 * Wake up parent sleeping in kern_wait(), also send
2272 		 * SIGCHLD to parent, but SIGCHLD does not guarantee
2273 		 * that parent will awake, because parent may masked
2274 		 * the signal.
2275 		 */
2276 		p->p_pptr->p_flag |= P_STATCHILD;
2277 		wakeup(p->p_pptr);
2278 		ps = p->p_pptr->p_sigacts;
2279 		mtx_lock(&ps->ps_mtx);
2280 		if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2281 			mtx_unlock(&ps->ps_mtx);
2282 			psignal(p->p_pptr, SIGCHLD);
2283 		} else
2284 			mtx_unlock(&ps->ps_mtx);
2285 		PROC_UNLOCK(p->p_pptr);
2286 		mtx_lock_spin(&sched_lock);
2287 	}
2288 }
2289 
2290 /*
2291  * Take the action for the specified signal
2292  * from the current set of pending signals.
2293  */
2294 void
2295 postsig(sig)
2296 	register int sig;
2297 {
2298 	struct thread *td = curthread;
2299 	register struct proc *p = td->td_proc;
2300 	struct sigacts *ps;
2301 	sig_t action;
2302 	sigset_t returnmask;
2303 	int code;
2304 
2305 	KASSERT(sig != 0, ("postsig"));
2306 
2307 	PROC_LOCK_ASSERT(p, MA_OWNED);
2308 	ps = p->p_sigacts;
2309 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2310 	SIGDELSET(td->td_siglist, sig);
2311 	action = ps->ps_sigact[_SIG_IDX(sig)];
2312 #ifdef KTRACE
2313 	if (KTRPOINT(td, KTR_PSIG))
2314 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2315 		    &td->td_oldsigmask : &td->td_sigmask, 0);
2316 #endif
2317 	if (p->p_stops & S_SIG) {
2318 		mtx_unlock(&ps->ps_mtx);
2319 		stopevent(p, S_SIG, sig);
2320 		mtx_lock(&ps->ps_mtx);
2321 	}
2322 
2323 	if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2324 		/*
2325 		 * Default action, where the default is to kill
2326 		 * the process.  (Other cases were ignored above.)
2327 		 */
2328 		mtx_unlock(&ps->ps_mtx);
2329 		sigexit(td, sig);
2330 		/* NOTREACHED */
2331 	} else {
2332 		if (td->td_pflags & TDP_SA) {
2333 			if (sig == SIGKILL) {
2334 				mtx_unlock(&ps->ps_mtx);
2335 				sigexit(td, sig);
2336 			}
2337 		}
2338 
2339 		/*
2340 		 * If we get here, the signal must be caught.
2341 		 */
2342 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2343 		    ("postsig action"));
2344 		/*
2345 		 * Set the new mask value and also defer further
2346 		 * occurrences of this signal.
2347 		 *
2348 		 * Special case: user has done a sigsuspend.  Here the
2349 		 * current mask is not of interest, but rather the
2350 		 * mask from before the sigsuspend is what we want
2351 		 * restored after the signal processing is completed.
2352 		 */
2353 		if (td->td_pflags & TDP_OLDMASK) {
2354 			returnmask = td->td_oldsigmask;
2355 			td->td_pflags &= ~TDP_OLDMASK;
2356 		} else
2357 			returnmask = td->td_sigmask;
2358 
2359 		SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2360 		if (!SIGISMEMBER(ps->ps_signodefer, sig))
2361 			SIGADDSET(td->td_sigmask, sig);
2362 
2363 		if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2364 			/*
2365 			 * See kern_sigaction() for origin of this code.
2366 			 */
2367 			SIGDELSET(ps->ps_sigcatch, sig);
2368 			if (sig != SIGCONT &&
2369 			    sigprop(sig) & SA_IGNORE)
2370 				SIGADDSET(ps->ps_sigignore, sig);
2371 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2372 		}
2373 		p->p_stats->p_ru.ru_nsignals++;
2374 		if (p->p_sig != sig) {
2375 			code = 0;
2376 		} else {
2377 			code = p->p_code;
2378 			p->p_code = 0;
2379 			p->p_sig = 0;
2380 		}
2381 		if (td->td_pflags & TDP_SA)
2382 			thread_signal_add(curthread, sig);
2383 		else
2384 			(*p->p_sysent->sv_sendsig)(action, sig,
2385 			    &returnmask, code);
2386 	}
2387 }
2388 
2389 /*
2390  * Kill the current process for stated reason.
2391  */
2392 void
2393 killproc(p, why)
2394 	struct proc *p;
2395 	char *why;
2396 {
2397 
2398 	PROC_LOCK_ASSERT(p, MA_OWNED);
2399 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2400 		p, p->p_pid, p->p_comm);
2401 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2402 		p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2403 	psignal(p, SIGKILL);
2404 }
2405 
2406 /*
2407  * Force the current process to exit with the specified signal, dumping core
2408  * if appropriate.  We bypass the normal tests for masked and caught signals,
2409  * allowing unrecoverable failures to terminate the process without changing
2410  * signal state.  Mark the accounting record with the signal termination.
2411  * If dumping core, save the signal number for the debugger.  Calls exit and
2412  * does not return.
2413  *
2414  * MPSAFE
2415  */
2416 void
2417 sigexit(td, sig)
2418 	struct thread *td;
2419 	int sig;
2420 {
2421 	struct proc *p = td->td_proc;
2422 
2423 	PROC_LOCK_ASSERT(p, MA_OWNED);
2424 	p->p_acflag |= AXSIG;
2425 	/*
2426 	 * We must be single-threading to generate a core dump.  This
2427 	 * ensures that the registers in the core file are up-to-date.
2428 	 * Also, the ELF dump handler assumes that the thread list doesn't
2429 	 * change out from under it.
2430 	 *
2431 	 * XXX If another thread attempts to single-thread before us
2432 	 *     (e.g. via fork()), we won't get a dump at all.
2433 	 */
2434 	if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2435 		p->p_sig = sig;
2436 		/*
2437 		 * Log signals which would cause core dumps
2438 		 * (Log as LOG_INFO to appease those who don't want
2439 		 * these messages.)
2440 		 * XXX : Todo, as well as euid, write out ruid too
2441 		 * Note that coredump() drops proc lock.
2442 		 */
2443 		if (coredump(td) == 0)
2444 			sig |= WCOREFLAG;
2445 		if (kern_logsigexit)
2446 			log(LOG_INFO,
2447 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
2448 			    p->p_pid, p->p_comm,
2449 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
2450 			    sig &~ WCOREFLAG,
2451 			    sig & WCOREFLAG ? " (core dumped)" : "");
2452 	} else
2453 		PROC_UNLOCK(p);
2454 	exit1(td, W_EXITCODE(0, sig));
2455 	/* NOTREACHED */
2456 }
2457 
2458 static char corefilename[MAXPATHLEN] = {"%N.core"};
2459 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2460 	      sizeof(corefilename), "process corefile name format string");
2461 
2462 /*
2463  * expand_name(name, uid, pid)
2464  * Expand the name described in corefilename, using name, uid, and pid.
2465  * corefilename is a printf-like string, with three format specifiers:
2466  *	%N	name of process ("name")
2467  *	%P	process id (pid)
2468  *	%U	user id (uid)
2469  * For example, "%N.core" is the default; they can be disabled completely
2470  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2471  * This is controlled by the sysctl variable kern.corefile (see above).
2472  */
2473 
2474 static char *
2475 expand_name(name, uid, pid)
2476 	const char *name;
2477 	uid_t uid;
2478 	pid_t pid;
2479 {
2480 	const char *format, *appendstr;
2481 	char *temp;
2482 	char buf[11];		/* Buffer for pid/uid -- max 4B */
2483 	size_t i, l, n;
2484 
2485 	format = corefilename;
2486 	temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2487 	if (temp == NULL)
2488 		return (NULL);
2489 	for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2490 		switch (format[i]) {
2491 		case '%':	/* Format character */
2492 			i++;
2493 			switch (format[i]) {
2494 			case '%':
2495 				appendstr = "%";
2496 				break;
2497 			case 'N':	/* process name */
2498 				appendstr = name;
2499 				break;
2500 			case 'P':	/* process id */
2501 				sprintf(buf, "%u", pid);
2502 				appendstr = buf;
2503 				break;
2504 			case 'U':	/* user id */
2505 				sprintf(buf, "%u", uid);
2506 				appendstr = buf;
2507 				break;
2508 			default:
2509 				appendstr = "";
2510 			  	log(LOG_ERR,
2511 				    "Unknown format character %c in `%s'\n",
2512 				    format[i], format);
2513 			}
2514 			l = strlen(appendstr);
2515 			if ((n + l) >= MAXPATHLEN)
2516 				goto toolong;
2517 			memcpy(temp + n, appendstr, l);
2518 			n += l;
2519 			break;
2520 		default:
2521 			temp[n++] = format[i];
2522 		}
2523 	}
2524 	if (format[i] != '\0')
2525 		goto toolong;
2526 	return (temp);
2527 toolong:
2528 	log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2529 	    (long)pid, name, (u_long)uid);
2530 	free(temp, M_TEMP);
2531 	return (NULL);
2532 }
2533 
2534 /*
2535  * Dump a process' core.  The main routine does some
2536  * policy checking, and creates the name of the coredump;
2537  * then it passes on a vnode and a size limit to the process-specific
2538  * coredump routine if there is one; if there _is not_ one, it returns
2539  * ENOSYS; otherwise it returns the error from the process-specific routine.
2540  */
2541 
2542 static int
2543 coredump(struct thread *td)
2544 {
2545 	struct proc *p = td->td_proc;
2546 	register struct vnode *vp;
2547 	register struct ucred *cred = td->td_ucred;
2548 	struct flock lf;
2549 	struct nameidata nd;
2550 	struct vattr vattr;
2551 	int error, error1, flags, locked;
2552 	struct mount *mp;
2553 	char *name;			/* name of corefile */
2554 	off_t limit;
2555 
2556 	PROC_LOCK_ASSERT(p, MA_OWNED);
2557 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
2558 	_STOPEVENT(p, S_CORE, 0);
2559 
2560 	if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2561 		PROC_UNLOCK(p);
2562 		return (EFAULT);
2563 	}
2564 
2565 	/*
2566 	 * Note that the bulk of limit checking is done after
2567 	 * the corefile is created.  The exception is if the limit
2568 	 * for corefiles is 0, in which case we don't bother
2569 	 * creating the corefile at all.  This layout means that
2570 	 * a corefile is truncated instead of not being created,
2571 	 * if it is larger than the limit.
2572 	 */
2573 	limit = (off_t)lim_cur(p, RLIMIT_CORE);
2574 	PROC_UNLOCK(p);
2575 	if (limit == 0)
2576 		return (EFBIG);
2577 
2578 	mtx_lock(&Giant);
2579 restart:
2580 	name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2581 	if (name == NULL) {
2582 		mtx_unlock(&Giant);
2583 		return (EINVAL);
2584 	}
2585 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2586 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
2587 	error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2588 	free(name, M_TEMP);
2589 	if (error) {
2590 		mtx_unlock(&Giant);
2591 		return (error);
2592 	}
2593 	NDFREE(&nd, NDF_ONLY_PNBUF);
2594 	vp = nd.ni_vp;
2595 
2596 	/* Don't dump to non-regular files or files with links. */
2597 	if (vp->v_type != VREG ||
2598 	    VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2599 		VOP_UNLOCK(vp, 0, td);
2600 		error = EFAULT;
2601 		goto out;
2602 	}
2603 
2604 	VOP_UNLOCK(vp, 0, td);
2605 	lf.l_whence = SEEK_SET;
2606 	lf.l_start = 0;
2607 	lf.l_len = 0;
2608 	lf.l_type = F_WRLCK;
2609 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
2610 
2611 	if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2612 		lf.l_type = F_UNLCK;
2613 		if (locked)
2614 			VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2615 		if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2616 			return (error);
2617 		if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2618 			return (error);
2619 		goto restart;
2620 	}
2621 
2622 	VATTR_NULL(&vattr);
2623 	vattr.va_size = 0;
2624 	if (set_core_nodump_flag)
2625 		vattr.va_flags = UF_NODUMP;
2626 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2627 	VOP_LEASE(vp, td, cred, LEASE_WRITE);
2628 	VOP_SETATTR(vp, &vattr, cred, td);
2629 	VOP_UNLOCK(vp, 0, td);
2630 	PROC_LOCK(p);
2631 	p->p_acflag |= ACORE;
2632 	PROC_UNLOCK(p);
2633 
2634 	error = p->p_sysent->sv_coredump ?
2635 	  p->p_sysent->sv_coredump(td, vp, limit) :
2636 	  ENOSYS;
2637 
2638 	if (locked) {
2639 		lf.l_type = F_UNLCK;
2640 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2641 	}
2642 	vn_finished_write(mp);
2643 out:
2644 	error1 = vn_close(vp, FWRITE, cred, td);
2645 	mtx_unlock(&Giant);
2646 	if (error == 0)
2647 		error = error1;
2648 	return (error);
2649 }
2650 
2651 /*
2652  * Nonexistent system call-- signal process (may want to handle it).
2653  * Flag error in case process won't see signal immediately (blocked or ignored).
2654  */
2655 #ifndef _SYS_SYSPROTO_H_
2656 struct nosys_args {
2657 	int	dummy;
2658 };
2659 #endif
2660 /*
2661  * MPSAFE
2662  */
2663 /* ARGSUSED */
2664 int
2665 nosys(td, args)
2666 	struct thread *td;
2667 	struct nosys_args *args;
2668 {
2669 	struct proc *p = td->td_proc;
2670 
2671 	PROC_LOCK(p);
2672 	psignal(p, SIGSYS);
2673 	PROC_UNLOCK(p);
2674 	return (ENOSYS);
2675 }
2676 
2677 /*
2678  * Send a SIGIO or SIGURG signal to a process or process group using
2679  * stored credentials rather than those of the current process.
2680  */
2681 void
2682 pgsigio(sigiop, sig, checkctty)
2683 	struct sigio **sigiop;
2684 	int sig, checkctty;
2685 {
2686 	struct sigio *sigio;
2687 
2688 	SIGIO_LOCK();
2689 	sigio = *sigiop;
2690 	if (sigio == NULL) {
2691 		SIGIO_UNLOCK();
2692 		return;
2693 	}
2694 	if (sigio->sio_pgid > 0) {
2695 		PROC_LOCK(sigio->sio_proc);
2696 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2697 			psignal(sigio->sio_proc, sig);
2698 		PROC_UNLOCK(sigio->sio_proc);
2699 	} else if (sigio->sio_pgid < 0) {
2700 		struct proc *p;
2701 
2702 		PGRP_LOCK(sigio->sio_pgrp);
2703 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2704 			PROC_LOCK(p);
2705 			if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2706 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2707 				psignal(p, sig);
2708 			PROC_UNLOCK(p);
2709 		}
2710 		PGRP_UNLOCK(sigio->sio_pgrp);
2711 	}
2712 	SIGIO_UNLOCK();
2713 }
2714 
2715 static int
2716 filt_sigattach(struct knote *kn)
2717 {
2718 	struct proc *p = curproc;
2719 
2720 	kn->kn_ptr.p_proc = p;
2721 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
2722 
2723 	knlist_add(&p->p_klist, kn, 0);
2724 
2725 	return (0);
2726 }
2727 
2728 static void
2729 filt_sigdetach(struct knote *kn)
2730 {
2731 	struct proc *p = kn->kn_ptr.p_proc;
2732 
2733 	knlist_remove(&p->p_klist, kn, 0);
2734 }
2735 
2736 /*
2737  * signal knotes are shared with proc knotes, so we apply a mask to
2738  * the hint in order to differentiate them from process hints.  This
2739  * could be avoided by using a signal-specific knote list, but probably
2740  * isn't worth the trouble.
2741  */
2742 static int
2743 filt_signal(struct knote *kn, long hint)
2744 {
2745 
2746 	if (hint & NOTE_SIGNAL) {
2747 		hint &= ~NOTE_SIGNAL;
2748 
2749 		if (kn->kn_id == hint)
2750 			kn->kn_data++;
2751 	}
2752 	return (kn->kn_data != 0);
2753 }
2754 
2755 struct sigacts *
2756 sigacts_alloc(void)
2757 {
2758 	struct sigacts *ps;
2759 
2760 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
2761 	ps->ps_refcnt = 1;
2762 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
2763 	return (ps);
2764 }
2765 
2766 void
2767 sigacts_free(struct sigacts *ps)
2768 {
2769 
2770 	mtx_lock(&ps->ps_mtx);
2771 	ps->ps_refcnt--;
2772 	if (ps->ps_refcnt == 0) {
2773 		mtx_destroy(&ps->ps_mtx);
2774 		free(ps, M_SUBPROC);
2775 	} else
2776 		mtx_unlock(&ps->ps_mtx);
2777 }
2778 
2779 struct sigacts *
2780 sigacts_hold(struct sigacts *ps)
2781 {
2782 	mtx_lock(&ps->ps_mtx);
2783 	ps->ps_refcnt++;
2784 	mtx_unlock(&ps->ps_mtx);
2785 	return (ps);
2786 }
2787 
2788 void
2789 sigacts_copy(struct sigacts *dest, struct sigacts *src)
2790 {
2791 
2792 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
2793 	mtx_lock(&src->ps_mtx);
2794 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
2795 	mtx_unlock(&src->ps_mtx);
2796 }
2797 
2798 int
2799 sigacts_shared(struct sigacts *ps)
2800 {
2801 	int shared;
2802 
2803 	mtx_lock(&ps->ps_mtx);
2804 	shared = ps->ps_refcnt > 1;
2805 	mtx_unlock(&ps->ps_mtx);
2806 	return (shared);
2807 }
2808