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