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