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