xref: /freebsd/sys/kern/kern_sig.c (revision af6a5351a1fdb1130f18be6c782c4d48916eb971)
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. 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_gzio.h"
42 #include "opt_ktrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/ctype.h>
46 #include <sys/systm.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
49 #include <sys/acct.h>
50 #include <sys/bus.h>
51 #include <sys/capsicum.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/imgact.h>
56 #include <sys/kernel.h>
57 #include <sys/ktr.h>
58 #include <sys/ktrace.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/refcount.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/procdesc.h>
66 #include <sys/posix4.h>
67 #include <sys/pioctl.h>
68 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/sdt.h>
71 #include <sys/sbuf.h>
72 #include <sys/sleepqueue.h>
73 #include <sys/smp.h>
74 #include <sys/stat.h>
75 #include <sys/sx.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysctl.h>
78 #include <sys/sysent.h>
79 #include <sys/syslog.h>
80 #include <sys/sysproto.h>
81 #include <sys/timers.h>
82 #include <sys/unistd.h>
83 #include <sys/wait.h>
84 #include <vm/vm.h>
85 #include <vm/vm_extern.h>
86 #include <vm/uma.h>
87 
88 #include <sys/jail.h>
89 
90 #include <machine/cpu.h>
91 
92 #include <security/audit/audit.h>
93 
94 #define	ONSIG	32		/* NSIG for osig* syscalls.  XXX. */
95 
96 SDT_PROVIDER_DECLARE(proc);
97 SDT_PROBE_DEFINE3(proc, , , signal__send,
98     "struct thread *", "struct proc *", "int");
99 SDT_PROBE_DEFINE2(proc, , , signal__clear,
100     "int", "ksiginfo_t *");
101 SDT_PROBE_DEFINE3(proc, , , signal__discard,
102     "struct thread *", "struct proc *", "int");
103 
104 static int	coredump(struct thread *);
105 static int	killpg1(struct thread *td, int sig, int pgid, int all,
106 		    ksiginfo_t *ksi);
107 static int	issignal(struct thread *td);
108 static int	sigprop(int sig);
109 static void	tdsigwakeup(struct thread *, int, sig_t, int);
110 static int	sig_suspend_threads(struct thread *, struct proc *, int);
111 static int	filt_sigattach(struct knote *kn);
112 static void	filt_sigdetach(struct knote *kn);
113 static int	filt_signal(struct knote *kn, long hint);
114 static struct thread *sigtd(struct proc *p, int sig, int prop);
115 static void	sigqueue_start(void);
116 
117 static uma_zone_t	ksiginfo_zone = NULL;
118 struct filterops sig_filtops = {
119 	.f_isfd = 0,
120 	.f_attach = filt_sigattach,
121 	.f_detach = filt_sigdetach,
122 	.f_event = filt_signal,
123 };
124 
125 static int	kern_logsigexit = 1;
126 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
127     &kern_logsigexit, 0,
128     "Log processes quitting on abnormal signals to syslog(3)");
129 
130 static int	kern_forcesigexit = 1;
131 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
132     &kern_forcesigexit, 0, "Force trap signal to be handled");
133 
134 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
135     "POSIX real time signal");
136 
137 static int	max_pending_per_proc = 128;
138 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
139     &max_pending_per_proc, 0, "Max pending signals per proc");
140 
141 static int	preallocate_siginfo = 1024;
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
143     &preallocate_siginfo, 0, "Preallocated signal memory size");
144 
145 static int	signal_overflow = 0;
146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
147     &signal_overflow, 0, "Number of signals overflew");
148 
149 static int	signal_alloc_fail = 0;
150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
151     &signal_alloc_fail, 0, "signals failed to be allocated");
152 
153 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
154 
155 /*
156  * Policy -- Can ucred cr1 send SIGIO to process cr2?
157  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
158  * in the right situations.
159  */
160 #define CANSIGIO(cr1, cr2) \
161 	((cr1)->cr_uid == 0 || \
162 	    (cr1)->cr_ruid == (cr2)->cr_ruid || \
163 	    (cr1)->cr_uid == (cr2)->cr_ruid || \
164 	    (cr1)->cr_ruid == (cr2)->cr_uid || \
165 	    (cr1)->cr_uid == (cr2)->cr_uid)
166 
167 static int	sugid_coredump;
168 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
169     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
170 
171 static int	capmode_coredump;
172 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
173     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
174 
175 static int	do_coredump = 1;
176 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
177 	&do_coredump, 0, "Enable/Disable coredumps");
178 
179 static int	set_core_nodump_flag = 0;
180 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
181 	0, "Enable setting the NODUMP flag on coredump files");
182 
183 static int	coredump_devctl = 0;
184 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
185 	0, "Generate a devctl notification when processes coredump");
186 
187 /*
188  * Signal properties and actions.
189  * The array below categorizes the signals and their default actions
190  * according to the following properties:
191  */
192 #define	SIGPROP_KILL		0x01	/* terminates process by default */
193 #define	SIGPROP_CORE		0x02	/* ditto and coredumps */
194 #define	SIGPROP_STOP		0x04	/* suspend process */
195 #define	SIGPROP_TTYSTOP		0x08	/* ditto, from tty */
196 #define	SIGPROP_IGNORE		0x10	/* ignore by default */
197 #define	SIGPROP_CONT		0x20	/* continue if suspended */
198 #define	SIGPROP_CANTMASK	0x40	/* non-maskable, catchable */
199 
200 static int sigproptbl[NSIG] = {
201 	[SIGHUP] =	SIGPROP_KILL,
202 	[SIGINT] =	SIGPROP_KILL,
203 	[SIGQUIT] =	SIGPROP_KILL | SIGPROP_CORE,
204 	[SIGILL] =	SIGPROP_KILL | SIGPROP_CORE,
205 	[SIGTRAP] =	SIGPROP_KILL | SIGPROP_CORE,
206 	[SIGABRT] =	SIGPROP_KILL | SIGPROP_CORE,
207 	[SIGEMT] =	SIGPROP_KILL | SIGPROP_CORE,
208 	[SIGFPE] =	SIGPROP_KILL | SIGPROP_CORE,
209 	[SIGKILL] =	SIGPROP_KILL,
210 	[SIGBUS] =	SIGPROP_KILL | SIGPROP_CORE,
211 	[SIGSEGV] =	SIGPROP_KILL | SIGPROP_CORE,
212 	[SIGSYS] =	SIGPROP_KILL | SIGPROP_CORE,
213 	[SIGPIPE] =	SIGPROP_KILL,
214 	[SIGALRM] =	SIGPROP_KILL,
215 	[SIGTERM] =	SIGPROP_KILL,
216 	[SIGURG] =	SIGPROP_IGNORE,
217 	[SIGSTOP] =	SIGPROP_STOP,
218 	[SIGTSTP] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
219 	[SIGCONT] =	SIGPROP_IGNORE | SIGPROP_CONT,
220 	[SIGCHLD] =	SIGPROP_IGNORE,
221 	[SIGTTIN] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
222 	[SIGTTOU] =	SIGPROP_STOP | SIGPROP_TTYSTOP,
223 	[SIGIO] =	SIGPROP_IGNORE,
224 	[SIGXCPU] =	SIGPROP_KILL,
225 	[SIGXFSZ] =	SIGPROP_KILL,
226 	[SIGVTALRM] =	SIGPROP_KILL,
227 	[SIGPROF] =	SIGPROP_KILL,
228 	[SIGWINCH] =	SIGPROP_IGNORE,
229 	[SIGINFO] =	SIGPROP_IGNORE,
230 	[SIGUSR1] =	SIGPROP_KILL,
231 	[SIGUSR2] =	SIGPROP_KILL,
232 };
233 
234 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
235 
236 static void
237 sigqueue_start(void)
238 {
239 	ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
240 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
241 	uma_prealloc(ksiginfo_zone, preallocate_siginfo);
242 	p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
243 	p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
244 	p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
245 }
246 
247 ksiginfo_t *
248 ksiginfo_alloc(int wait)
249 {
250 	int flags;
251 
252 	flags = M_ZERO;
253 	if (! wait)
254 		flags |= M_NOWAIT;
255 	if (ksiginfo_zone != NULL)
256 		return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
257 	return (NULL);
258 }
259 
260 void
261 ksiginfo_free(ksiginfo_t *ksi)
262 {
263 	uma_zfree(ksiginfo_zone, ksi);
264 }
265 
266 static __inline int
267 ksiginfo_tryfree(ksiginfo_t *ksi)
268 {
269 	if (!(ksi->ksi_flags & KSI_EXT)) {
270 		uma_zfree(ksiginfo_zone, ksi);
271 		return (1);
272 	}
273 	return (0);
274 }
275 
276 void
277 sigqueue_init(sigqueue_t *list, struct proc *p)
278 {
279 	SIGEMPTYSET(list->sq_signals);
280 	SIGEMPTYSET(list->sq_kill);
281 	SIGEMPTYSET(list->sq_ptrace);
282 	TAILQ_INIT(&list->sq_list);
283 	list->sq_proc = p;
284 	list->sq_flags = SQ_INIT;
285 }
286 
287 /*
288  * Get a signal's ksiginfo.
289  * Return:
290  *	0	-	signal not found
291  *	others	-	signal number
292  */
293 static int
294 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
295 {
296 	struct proc *p = sq->sq_proc;
297 	struct ksiginfo *ksi, *next;
298 	int count = 0;
299 
300 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
301 
302 	if (!SIGISMEMBER(sq->sq_signals, signo))
303 		return (0);
304 
305 	if (SIGISMEMBER(sq->sq_ptrace, signo)) {
306 		count++;
307 		SIGDELSET(sq->sq_ptrace, signo);
308 		si->ksi_flags |= KSI_PTRACE;
309 	}
310 	if (SIGISMEMBER(sq->sq_kill, signo)) {
311 		count++;
312 		if (count == 1)
313 			SIGDELSET(sq->sq_kill, signo);
314 	}
315 
316 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
317 		if (ksi->ksi_signo == signo) {
318 			if (count == 0) {
319 				TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
320 				ksi->ksi_sigq = NULL;
321 				ksiginfo_copy(ksi, si);
322 				if (ksiginfo_tryfree(ksi) && p != NULL)
323 					p->p_pendingcnt--;
324 			}
325 			if (++count > 1)
326 				break;
327 		}
328 	}
329 
330 	if (count <= 1)
331 		SIGDELSET(sq->sq_signals, signo);
332 	si->ksi_signo = signo;
333 	return (signo);
334 }
335 
336 void
337 sigqueue_take(ksiginfo_t *ksi)
338 {
339 	struct ksiginfo *kp;
340 	struct proc	*p;
341 	sigqueue_t	*sq;
342 
343 	if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
344 		return;
345 
346 	p = sq->sq_proc;
347 	TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
348 	ksi->ksi_sigq = NULL;
349 	if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
350 		p->p_pendingcnt--;
351 
352 	for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
353 	     kp = TAILQ_NEXT(kp, ksi_link)) {
354 		if (kp->ksi_signo == ksi->ksi_signo)
355 			break;
356 	}
357 	if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
358 	    !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
359 		SIGDELSET(sq->sq_signals, ksi->ksi_signo);
360 }
361 
362 static int
363 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
364 {
365 	struct proc *p = sq->sq_proc;
366 	struct ksiginfo *ksi;
367 	int ret = 0;
368 
369 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
370 
371 	/*
372 	 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
373 	 * for these signals.
374 	 */
375 	if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
376 		SIGADDSET(sq->sq_kill, signo);
377 		goto out_set_bit;
378 	}
379 
380 	/* directly insert the ksi, don't copy it */
381 	if (si->ksi_flags & KSI_INS) {
382 		if (si->ksi_flags & KSI_HEAD)
383 			TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
384 		else
385 			TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
386 		si->ksi_sigq = sq;
387 		goto out_set_bit;
388 	}
389 
390 	if (__predict_false(ksiginfo_zone == NULL)) {
391 		SIGADDSET(sq->sq_kill, signo);
392 		goto out_set_bit;
393 	}
394 
395 	if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
396 		signal_overflow++;
397 		ret = EAGAIN;
398 	} else if ((ksi = ksiginfo_alloc(0)) == NULL) {
399 		signal_alloc_fail++;
400 		ret = EAGAIN;
401 	} else {
402 		if (p != NULL)
403 			p->p_pendingcnt++;
404 		ksiginfo_copy(si, ksi);
405 		ksi->ksi_signo = signo;
406 		if (si->ksi_flags & KSI_HEAD)
407 			TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
408 		else
409 			TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
410 		ksi->ksi_sigq = sq;
411 	}
412 
413 	if (ret != 0) {
414 		if ((si->ksi_flags & KSI_PTRACE) != 0) {
415 			SIGADDSET(sq->sq_ptrace, signo);
416 			ret = 0;
417 			goto out_set_bit;
418 		} else if ((si->ksi_flags & KSI_TRAP) != 0 ||
419 		    (si->ksi_flags & KSI_SIGQ) == 0) {
420 			SIGADDSET(sq->sq_kill, signo);
421 			ret = 0;
422 			goto out_set_bit;
423 		}
424 		return (ret);
425 	}
426 
427 out_set_bit:
428 	SIGADDSET(sq->sq_signals, signo);
429 	return (ret);
430 }
431 
432 void
433 sigqueue_flush(sigqueue_t *sq)
434 {
435 	struct proc *p = sq->sq_proc;
436 	ksiginfo_t *ksi;
437 
438 	KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
439 
440 	if (p != NULL)
441 		PROC_LOCK_ASSERT(p, MA_OWNED);
442 
443 	while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
444 		TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
445 		ksi->ksi_sigq = NULL;
446 		if (ksiginfo_tryfree(ksi) && p != NULL)
447 			p->p_pendingcnt--;
448 	}
449 
450 	SIGEMPTYSET(sq->sq_signals);
451 	SIGEMPTYSET(sq->sq_kill);
452 	SIGEMPTYSET(sq->sq_ptrace);
453 }
454 
455 static void
456 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
457 {
458 	sigset_t tmp;
459 	struct proc *p1, *p2;
460 	ksiginfo_t *ksi, *next;
461 
462 	KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
463 	KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
464 	p1 = src->sq_proc;
465 	p2 = dst->sq_proc;
466 	/* Move siginfo to target list */
467 	TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
468 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
469 			TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
470 			if (p1 != NULL)
471 				p1->p_pendingcnt--;
472 			TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
473 			ksi->ksi_sigq = dst;
474 			if (p2 != NULL)
475 				p2->p_pendingcnt++;
476 		}
477 	}
478 
479 	/* Move pending bits to target list */
480 	tmp = src->sq_kill;
481 	SIGSETAND(tmp, *set);
482 	SIGSETOR(dst->sq_kill, tmp);
483 	SIGSETNAND(src->sq_kill, tmp);
484 
485 	tmp = src->sq_ptrace;
486 	SIGSETAND(tmp, *set);
487 	SIGSETOR(dst->sq_ptrace, tmp);
488 	SIGSETNAND(src->sq_ptrace, tmp);
489 
490 	tmp = src->sq_signals;
491 	SIGSETAND(tmp, *set);
492 	SIGSETOR(dst->sq_signals, tmp);
493 	SIGSETNAND(src->sq_signals, tmp);
494 }
495 
496 #if 0
497 static void
498 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
499 {
500 	sigset_t set;
501 
502 	SIGEMPTYSET(set);
503 	SIGADDSET(set, signo);
504 	sigqueue_move_set(src, dst, &set);
505 }
506 #endif
507 
508 static void
509 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
510 {
511 	struct proc *p = sq->sq_proc;
512 	ksiginfo_t *ksi, *next;
513 
514 	KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
515 
516 	/* Remove siginfo queue */
517 	TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
518 		if (SIGISMEMBER(*set, ksi->ksi_signo)) {
519 			TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
520 			ksi->ksi_sigq = NULL;
521 			if (ksiginfo_tryfree(ksi) && p != NULL)
522 				p->p_pendingcnt--;
523 		}
524 	}
525 	SIGSETNAND(sq->sq_kill, *set);
526 	SIGSETNAND(sq->sq_ptrace, *set);
527 	SIGSETNAND(sq->sq_signals, *set);
528 }
529 
530 void
531 sigqueue_delete(sigqueue_t *sq, int signo)
532 {
533 	sigset_t set;
534 
535 	SIGEMPTYSET(set);
536 	SIGADDSET(set, signo);
537 	sigqueue_delete_set(sq, &set);
538 }
539 
540 /* Remove a set of signals for a process */
541 static void
542 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
543 {
544 	sigqueue_t worklist;
545 	struct thread *td0;
546 
547 	PROC_LOCK_ASSERT(p, MA_OWNED);
548 
549 	sigqueue_init(&worklist, NULL);
550 	sigqueue_move_set(&p->p_sigqueue, &worklist, set);
551 
552 	FOREACH_THREAD_IN_PROC(p, td0)
553 		sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
554 
555 	sigqueue_flush(&worklist);
556 }
557 
558 void
559 sigqueue_delete_proc(struct proc *p, int signo)
560 {
561 	sigset_t set;
562 
563 	SIGEMPTYSET(set);
564 	SIGADDSET(set, signo);
565 	sigqueue_delete_set_proc(p, &set);
566 }
567 
568 static void
569 sigqueue_delete_stopmask_proc(struct proc *p)
570 {
571 	sigset_t set;
572 
573 	SIGEMPTYSET(set);
574 	SIGADDSET(set, SIGSTOP);
575 	SIGADDSET(set, SIGTSTP);
576 	SIGADDSET(set, SIGTTIN);
577 	SIGADDSET(set, SIGTTOU);
578 	sigqueue_delete_set_proc(p, &set);
579 }
580 
581 /*
582  * Determine signal that should be delivered to thread td, the current
583  * thread, 0 if none.  If there is a pending stop signal with default
584  * action, the process stops in issignal().
585  */
586 int
587 cursig(struct thread *td)
588 {
589 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
590 	mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
591 	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
592 	return (SIGPENDING(td) ? issignal(td) : 0);
593 }
594 
595 /*
596  * Arrange for ast() to handle unmasked pending signals on return to user
597  * mode.  This must be called whenever a signal is added to td_sigqueue or
598  * unmasked in td_sigmask.
599  */
600 void
601 signotify(struct thread *td)
602 {
603 	struct proc *p;
604 
605 	p = td->td_proc;
606 
607 	PROC_LOCK_ASSERT(p, MA_OWNED);
608 
609 	if (SIGPENDING(td)) {
610 		thread_lock(td);
611 		td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
612 		thread_unlock(td);
613 	}
614 }
615 
616 int
617 sigonstack(size_t sp)
618 {
619 	struct thread *td = curthread;
620 
621 	return ((td->td_pflags & TDP_ALTSTACK) ?
622 #if defined(COMPAT_43)
623 	    ((td->td_sigstk.ss_size == 0) ?
624 		(td->td_sigstk.ss_flags & SS_ONSTACK) :
625 		((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
626 #else
627 	    ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
628 #endif
629 	    : 0);
630 }
631 
632 static __inline int
633 sigprop(int sig)
634 {
635 
636 	if (sig > 0 && sig < nitems(sigproptbl))
637 		return (sigproptbl[sig]);
638 	return (0);
639 }
640 
641 int
642 sig_ffs(sigset_t *set)
643 {
644 	int i;
645 
646 	for (i = 0; i < _SIG_WORDS; i++)
647 		if (set->__bits[i])
648 			return (ffs(set->__bits[i]) + (i * 32));
649 	return (0);
650 }
651 
652 static bool
653 sigact_flag_test(const struct sigaction *act, int flag)
654 {
655 
656 	/*
657 	 * SA_SIGINFO is reset when signal disposition is set to
658 	 * ignore or default.  Other flags are kept according to user
659 	 * settings.
660 	 */
661 	return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
662 	    ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
663 	    (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
664 }
665 
666 /*
667  * kern_sigaction
668  * sigaction
669  * freebsd4_sigaction
670  * osigaction
671  */
672 int
673 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
674     struct sigaction *oact, int flags)
675 {
676 	struct sigacts *ps;
677 	struct proc *p = td->td_proc;
678 
679 	if (!_SIG_VALID(sig))
680 		return (EINVAL);
681 	if (act != NULL && act->sa_handler != SIG_DFL &&
682 	    act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
683 	    SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
684 	    SA_NOCLDWAIT | SA_SIGINFO)) != 0)
685 		return (EINVAL);
686 
687 	PROC_LOCK(p);
688 	ps = p->p_sigacts;
689 	mtx_lock(&ps->ps_mtx);
690 	if (oact) {
691 		oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
692 		oact->sa_flags = 0;
693 		if (SIGISMEMBER(ps->ps_sigonstack, sig))
694 			oact->sa_flags |= SA_ONSTACK;
695 		if (!SIGISMEMBER(ps->ps_sigintr, sig))
696 			oact->sa_flags |= SA_RESTART;
697 		if (SIGISMEMBER(ps->ps_sigreset, sig))
698 			oact->sa_flags |= SA_RESETHAND;
699 		if (SIGISMEMBER(ps->ps_signodefer, sig))
700 			oact->sa_flags |= SA_NODEFER;
701 		if (SIGISMEMBER(ps->ps_siginfo, sig)) {
702 			oact->sa_flags |= SA_SIGINFO;
703 			oact->sa_sigaction =
704 			    (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
705 		} else
706 			oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
707 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
708 			oact->sa_flags |= SA_NOCLDSTOP;
709 		if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
710 			oact->sa_flags |= SA_NOCLDWAIT;
711 	}
712 	if (act) {
713 		if ((sig == SIGKILL || sig == SIGSTOP) &&
714 		    act->sa_handler != SIG_DFL) {
715 			mtx_unlock(&ps->ps_mtx);
716 			PROC_UNLOCK(p);
717 			return (EINVAL);
718 		}
719 
720 		/*
721 		 * Change setting atomically.
722 		 */
723 
724 		ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
725 		SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
726 		if (sigact_flag_test(act, SA_SIGINFO)) {
727 			ps->ps_sigact[_SIG_IDX(sig)] =
728 			    (__sighandler_t *)act->sa_sigaction;
729 			SIGADDSET(ps->ps_siginfo, sig);
730 		} else {
731 			ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
732 			SIGDELSET(ps->ps_siginfo, sig);
733 		}
734 		if (!sigact_flag_test(act, SA_RESTART))
735 			SIGADDSET(ps->ps_sigintr, sig);
736 		else
737 			SIGDELSET(ps->ps_sigintr, sig);
738 		if (sigact_flag_test(act, SA_ONSTACK))
739 			SIGADDSET(ps->ps_sigonstack, sig);
740 		else
741 			SIGDELSET(ps->ps_sigonstack, sig);
742 		if (sigact_flag_test(act, SA_RESETHAND))
743 			SIGADDSET(ps->ps_sigreset, sig);
744 		else
745 			SIGDELSET(ps->ps_sigreset, sig);
746 		if (sigact_flag_test(act, SA_NODEFER))
747 			SIGADDSET(ps->ps_signodefer, sig);
748 		else
749 			SIGDELSET(ps->ps_signodefer, sig);
750 		if (sig == SIGCHLD) {
751 			if (act->sa_flags & SA_NOCLDSTOP)
752 				ps->ps_flag |= PS_NOCLDSTOP;
753 			else
754 				ps->ps_flag &= ~PS_NOCLDSTOP;
755 			if (act->sa_flags & SA_NOCLDWAIT) {
756 				/*
757 				 * Paranoia: since SA_NOCLDWAIT is implemented
758 				 * by reparenting the dying child to PID 1 (and
759 				 * trust it to reap the zombie), PID 1 itself
760 				 * is forbidden to set SA_NOCLDWAIT.
761 				 */
762 				if (p->p_pid == 1)
763 					ps->ps_flag &= ~PS_NOCLDWAIT;
764 				else
765 					ps->ps_flag |= PS_NOCLDWAIT;
766 			} else
767 				ps->ps_flag &= ~PS_NOCLDWAIT;
768 			if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
769 				ps->ps_flag |= PS_CLDSIGIGN;
770 			else
771 				ps->ps_flag &= ~PS_CLDSIGIGN;
772 		}
773 		/*
774 		 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
775 		 * and for signals set to SIG_DFL where the default is to
776 		 * ignore. However, don't put SIGCONT in ps_sigignore, as we
777 		 * have to restart the process.
778 		 */
779 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
780 		    (sigprop(sig) & SIGPROP_IGNORE &&
781 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
782 			/* never to be seen again */
783 			sigqueue_delete_proc(p, sig);
784 			if (sig != SIGCONT)
785 				/* easier in psignal */
786 				SIGADDSET(ps->ps_sigignore, sig);
787 			SIGDELSET(ps->ps_sigcatch, sig);
788 		} else {
789 			SIGDELSET(ps->ps_sigignore, sig);
790 			if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
791 				SIGDELSET(ps->ps_sigcatch, sig);
792 			else
793 				SIGADDSET(ps->ps_sigcatch, sig);
794 		}
795 #ifdef COMPAT_FREEBSD4
796 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
797 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
798 		    (flags & KSA_FREEBSD4) == 0)
799 			SIGDELSET(ps->ps_freebsd4, sig);
800 		else
801 			SIGADDSET(ps->ps_freebsd4, sig);
802 #endif
803 #ifdef COMPAT_43
804 		if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
805 		    ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
806 		    (flags & KSA_OSIGSET) == 0)
807 			SIGDELSET(ps->ps_osigset, sig);
808 		else
809 			SIGADDSET(ps->ps_osigset, sig);
810 #endif
811 	}
812 	mtx_unlock(&ps->ps_mtx);
813 	PROC_UNLOCK(p);
814 	return (0);
815 }
816 
817 #ifndef _SYS_SYSPROTO_H_
818 struct sigaction_args {
819 	int	sig;
820 	struct	sigaction *act;
821 	struct	sigaction *oact;
822 };
823 #endif
824 int
825 sys_sigaction(td, uap)
826 	struct thread *td;
827 	register struct sigaction_args *uap;
828 {
829 	struct sigaction act, oact;
830 	register struct sigaction *actp, *oactp;
831 	int error;
832 
833 	actp = (uap->act != NULL) ? &act : NULL;
834 	oactp = (uap->oact != NULL) ? &oact : NULL;
835 	if (actp) {
836 		error = copyin(uap->act, actp, sizeof(act));
837 		if (error)
838 			return (error);
839 	}
840 	error = kern_sigaction(td, uap->sig, actp, oactp, 0);
841 	if (oactp && !error)
842 		error = copyout(oactp, uap->oact, sizeof(oact));
843 	return (error);
844 }
845 
846 #ifdef COMPAT_FREEBSD4
847 #ifndef _SYS_SYSPROTO_H_
848 struct freebsd4_sigaction_args {
849 	int	sig;
850 	struct	sigaction *act;
851 	struct	sigaction *oact;
852 };
853 #endif
854 int
855 freebsd4_sigaction(td, uap)
856 	struct thread *td;
857 	register struct freebsd4_sigaction_args *uap;
858 {
859 	struct sigaction act, oact;
860 	register struct sigaction *actp, *oactp;
861 	int error;
862 
863 
864 	actp = (uap->act != NULL) ? &act : NULL;
865 	oactp = (uap->oact != NULL) ? &oact : NULL;
866 	if (actp) {
867 		error = copyin(uap->act, actp, sizeof(act));
868 		if (error)
869 			return (error);
870 	}
871 	error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
872 	if (oactp && !error)
873 		error = copyout(oactp, uap->oact, sizeof(oact));
874 	return (error);
875 }
876 #endif	/* COMAPT_FREEBSD4 */
877 
878 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
879 #ifndef _SYS_SYSPROTO_H_
880 struct osigaction_args {
881 	int	signum;
882 	struct	osigaction *nsa;
883 	struct	osigaction *osa;
884 };
885 #endif
886 int
887 osigaction(td, uap)
888 	struct thread *td;
889 	register struct osigaction_args *uap;
890 {
891 	struct osigaction sa;
892 	struct sigaction nsa, osa;
893 	register struct sigaction *nsap, *osap;
894 	int error;
895 
896 	if (uap->signum <= 0 || uap->signum >= ONSIG)
897 		return (EINVAL);
898 
899 	nsap = (uap->nsa != NULL) ? &nsa : NULL;
900 	osap = (uap->osa != NULL) ? &osa : NULL;
901 
902 	if (nsap) {
903 		error = copyin(uap->nsa, &sa, sizeof(sa));
904 		if (error)
905 			return (error);
906 		nsap->sa_handler = sa.sa_handler;
907 		nsap->sa_flags = sa.sa_flags;
908 		OSIG2SIG(sa.sa_mask, nsap->sa_mask);
909 	}
910 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
911 	if (osap && !error) {
912 		sa.sa_handler = osap->sa_handler;
913 		sa.sa_flags = osap->sa_flags;
914 		SIG2OSIG(osap->sa_mask, sa.sa_mask);
915 		error = copyout(&sa, uap->osa, sizeof(sa));
916 	}
917 	return (error);
918 }
919 
920 #if !defined(__i386__)
921 /* Avoid replicating the same stub everywhere */
922 int
923 osigreturn(td, uap)
924 	struct thread *td;
925 	struct osigreturn_args *uap;
926 {
927 
928 	return (nosys(td, (struct nosys_args *)uap));
929 }
930 #endif
931 #endif /* COMPAT_43 */
932 
933 /*
934  * Initialize signal state for process 0;
935  * set to ignore signals that are ignored by default.
936  */
937 void
938 siginit(p)
939 	struct proc *p;
940 {
941 	register int i;
942 	struct sigacts *ps;
943 
944 	PROC_LOCK(p);
945 	ps = p->p_sigacts;
946 	mtx_lock(&ps->ps_mtx);
947 	for (i = 1; i <= NSIG; i++) {
948 		if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
949 			SIGADDSET(ps->ps_sigignore, i);
950 		}
951 	}
952 	mtx_unlock(&ps->ps_mtx);
953 	PROC_UNLOCK(p);
954 }
955 
956 /*
957  * Reset specified signal to the default disposition.
958  */
959 static void
960 sigdflt(struct sigacts *ps, int sig)
961 {
962 
963 	mtx_assert(&ps->ps_mtx, MA_OWNED);
964 	SIGDELSET(ps->ps_sigcatch, sig);
965 	if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
966 		SIGADDSET(ps->ps_sigignore, sig);
967 	ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
968 	SIGDELSET(ps->ps_siginfo, sig);
969 }
970 
971 /*
972  * Reset signals for an exec of the specified process.
973  */
974 void
975 execsigs(struct proc *p)
976 {
977 	sigset_t osigignore;
978 	struct sigacts *ps;
979 	int sig;
980 	struct thread *td;
981 
982 	/*
983 	 * Reset caught signals.  Held signals remain held
984 	 * through td_sigmask (unless they were caught,
985 	 * and are now ignored by default).
986 	 */
987 	PROC_LOCK_ASSERT(p, MA_OWNED);
988 	td = FIRST_THREAD_IN_PROC(p);
989 	ps = p->p_sigacts;
990 	mtx_lock(&ps->ps_mtx);
991 	while (SIGNOTEMPTY(ps->ps_sigcatch)) {
992 		sig = sig_ffs(&ps->ps_sigcatch);
993 		sigdflt(ps, sig);
994 		if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
995 			sigqueue_delete_proc(p, sig);
996 	}
997 
998 	/*
999 	 * As CloudABI processes cannot modify signal handlers, fully
1000 	 * reset all signals to their default behavior. Do ignore
1001 	 * SIGPIPE, as it would otherwise be impossible to recover from
1002 	 * writes to broken pipes and sockets.
1003 	 */
1004 	if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) {
1005 		osigignore = ps->ps_sigignore;
1006 		while (SIGNOTEMPTY(osigignore)) {
1007 			sig = sig_ffs(&osigignore);
1008 			SIGDELSET(osigignore, sig);
1009 			if (sig != SIGPIPE)
1010 				sigdflt(ps, sig);
1011 		}
1012 		SIGADDSET(ps->ps_sigignore, SIGPIPE);
1013 	}
1014 
1015 	/*
1016 	 * Reset stack state to the user stack.
1017 	 * Clear set of signals caught on the signal stack.
1018 	 */
1019 	td->td_sigstk.ss_flags = SS_DISABLE;
1020 	td->td_sigstk.ss_size = 0;
1021 	td->td_sigstk.ss_sp = 0;
1022 	td->td_pflags &= ~TDP_ALTSTACK;
1023 	/*
1024 	 * Reset no zombies if child dies flag as Solaris does.
1025 	 */
1026 	ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1027 	if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1028 		ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1029 	mtx_unlock(&ps->ps_mtx);
1030 }
1031 
1032 /*
1033  * kern_sigprocmask()
1034  *
1035  *	Manipulate signal mask.
1036  */
1037 int
1038 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1039     int flags)
1040 {
1041 	sigset_t new_block, oset1;
1042 	struct proc *p;
1043 	int error;
1044 
1045 	p = td->td_proc;
1046 	if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1047 		PROC_LOCK_ASSERT(p, MA_OWNED);
1048 	else
1049 		PROC_LOCK(p);
1050 	mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1051 	    ? MA_OWNED : MA_NOTOWNED);
1052 	if (oset != NULL)
1053 		*oset = td->td_sigmask;
1054 
1055 	error = 0;
1056 	if (set != NULL) {
1057 		switch (how) {
1058 		case SIG_BLOCK:
1059 			SIG_CANTMASK(*set);
1060 			oset1 = td->td_sigmask;
1061 			SIGSETOR(td->td_sigmask, *set);
1062 			new_block = td->td_sigmask;
1063 			SIGSETNAND(new_block, oset1);
1064 			break;
1065 		case SIG_UNBLOCK:
1066 			SIGSETNAND(td->td_sigmask, *set);
1067 			signotify(td);
1068 			goto out;
1069 		case SIG_SETMASK:
1070 			SIG_CANTMASK(*set);
1071 			oset1 = td->td_sigmask;
1072 			if (flags & SIGPROCMASK_OLD)
1073 				SIGSETLO(td->td_sigmask, *set);
1074 			else
1075 				td->td_sigmask = *set;
1076 			new_block = td->td_sigmask;
1077 			SIGSETNAND(new_block, oset1);
1078 			signotify(td);
1079 			break;
1080 		default:
1081 			error = EINVAL;
1082 			goto out;
1083 		}
1084 
1085 		/*
1086 		 * The new_block set contains signals that were not previously
1087 		 * blocked, but are blocked now.
1088 		 *
1089 		 * In case we block any signal that was not previously blocked
1090 		 * for td, and process has the signal pending, try to schedule
1091 		 * signal delivery to some thread that does not block the
1092 		 * signal, possibly waking it up.
1093 		 */
1094 		if (p->p_numthreads != 1)
1095 			reschedule_signals(p, new_block, flags);
1096 	}
1097 
1098 out:
1099 	if (!(flags & SIGPROCMASK_PROC_LOCKED))
1100 		PROC_UNLOCK(p);
1101 	return (error);
1102 }
1103 
1104 #ifndef _SYS_SYSPROTO_H_
1105 struct sigprocmask_args {
1106 	int	how;
1107 	const sigset_t *set;
1108 	sigset_t *oset;
1109 };
1110 #endif
1111 int
1112 sys_sigprocmask(td, uap)
1113 	register struct thread *td;
1114 	struct sigprocmask_args *uap;
1115 {
1116 	sigset_t set, oset;
1117 	sigset_t *setp, *osetp;
1118 	int error;
1119 
1120 	setp = (uap->set != NULL) ? &set : NULL;
1121 	osetp = (uap->oset != NULL) ? &oset : NULL;
1122 	if (setp) {
1123 		error = copyin(uap->set, setp, sizeof(set));
1124 		if (error)
1125 			return (error);
1126 	}
1127 	error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1128 	if (osetp && !error) {
1129 		error = copyout(osetp, uap->oset, sizeof(oset));
1130 	}
1131 	return (error);
1132 }
1133 
1134 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
1135 #ifndef _SYS_SYSPROTO_H_
1136 struct osigprocmask_args {
1137 	int	how;
1138 	osigset_t mask;
1139 };
1140 #endif
1141 int
1142 osigprocmask(td, uap)
1143 	register struct thread *td;
1144 	struct osigprocmask_args *uap;
1145 {
1146 	sigset_t set, oset;
1147 	int error;
1148 
1149 	OSIG2SIG(uap->mask, set);
1150 	error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1151 	SIG2OSIG(oset, td->td_retval[0]);
1152 	return (error);
1153 }
1154 #endif /* COMPAT_43 */
1155 
1156 int
1157 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1158 {
1159 	ksiginfo_t ksi;
1160 	sigset_t set;
1161 	int error;
1162 
1163 	error = copyin(uap->set, &set, sizeof(set));
1164 	if (error) {
1165 		td->td_retval[0] = error;
1166 		return (0);
1167 	}
1168 
1169 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1170 	if (error) {
1171 		if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1172 			error = ERESTART;
1173 		if (error == ERESTART)
1174 			return (error);
1175 		td->td_retval[0] = error;
1176 		return (0);
1177 	}
1178 
1179 	error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1180 	td->td_retval[0] = error;
1181 	return (0);
1182 }
1183 
1184 int
1185 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1186 {
1187 	struct timespec ts;
1188 	struct timespec *timeout;
1189 	sigset_t set;
1190 	ksiginfo_t ksi;
1191 	int error;
1192 
1193 	if (uap->timeout) {
1194 		error = copyin(uap->timeout, &ts, sizeof(ts));
1195 		if (error)
1196 			return (error);
1197 
1198 		timeout = &ts;
1199 	} else
1200 		timeout = NULL;
1201 
1202 	error = copyin(uap->set, &set, sizeof(set));
1203 	if (error)
1204 		return (error);
1205 
1206 	error = kern_sigtimedwait(td, set, &ksi, timeout);
1207 	if (error)
1208 		return (error);
1209 
1210 	if (uap->info)
1211 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1212 
1213 	if (error == 0)
1214 		td->td_retval[0] = ksi.ksi_signo;
1215 	return (error);
1216 }
1217 
1218 int
1219 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1220 {
1221 	ksiginfo_t ksi;
1222 	sigset_t set;
1223 	int error;
1224 
1225 	error = copyin(uap->set, &set, sizeof(set));
1226 	if (error)
1227 		return (error);
1228 
1229 	error = kern_sigtimedwait(td, set, &ksi, NULL);
1230 	if (error)
1231 		return (error);
1232 
1233 	if (uap->info)
1234 		error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1235 
1236 	if (error == 0)
1237 		td->td_retval[0] = ksi.ksi_signo;
1238 	return (error);
1239 }
1240 
1241 int
1242 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1243 	struct timespec *timeout)
1244 {
1245 	struct sigacts *ps;
1246 	sigset_t saved_mask, new_block;
1247 	struct proc *p;
1248 	int error, sig, timo, timevalid = 0;
1249 	struct timespec rts, ets, ts;
1250 	struct timeval tv;
1251 
1252 	p = td->td_proc;
1253 	error = 0;
1254 	ets.tv_sec = 0;
1255 	ets.tv_nsec = 0;
1256 
1257 	if (timeout != NULL) {
1258 		if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1259 			timevalid = 1;
1260 			getnanouptime(&rts);
1261 			ets = rts;
1262 			timespecadd(&ets, timeout);
1263 		}
1264 	}
1265 	ksiginfo_init(ksi);
1266 	/* Some signals can not be waited for. */
1267 	SIG_CANTMASK(waitset);
1268 	ps = p->p_sigacts;
1269 	PROC_LOCK(p);
1270 	saved_mask = td->td_sigmask;
1271 	SIGSETNAND(td->td_sigmask, waitset);
1272 	for (;;) {
1273 		mtx_lock(&ps->ps_mtx);
1274 		sig = cursig(td);
1275 		mtx_unlock(&ps->ps_mtx);
1276 		KASSERT(sig >= 0, ("sig %d", sig));
1277 		if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1278 			if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1279 			    sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1280 				error = 0;
1281 				break;
1282 			}
1283 		}
1284 
1285 		if (error != 0)
1286 			break;
1287 
1288 		/*
1289 		 * POSIX says this must be checked after looking for pending
1290 		 * signals.
1291 		 */
1292 		if (timeout != NULL) {
1293 			if (!timevalid) {
1294 				error = EINVAL;
1295 				break;
1296 			}
1297 			getnanouptime(&rts);
1298 			if (timespeccmp(&rts, &ets, >=)) {
1299 				error = EAGAIN;
1300 				break;
1301 			}
1302 			ts = ets;
1303 			timespecsub(&ts, &rts);
1304 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1305 			timo = tvtohz(&tv);
1306 		} else {
1307 			timo = 0;
1308 		}
1309 
1310 		error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1311 
1312 		if (timeout != NULL) {
1313 			if (error == ERESTART) {
1314 				/* Timeout can not be restarted. */
1315 				error = EINTR;
1316 			} else if (error == EAGAIN) {
1317 				/* We will calculate timeout by ourself. */
1318 				error = 0;
1319 			}
1320 		}
1321 	}
1322 
1323 	new_block = saved_mask;
1324 	SIGSETNAND(new_block, td->td_sigmask);
1325 	td->td_sigmask = saved_mask;
1326 	/*
1327 	 * Fewer signals can be delivered to us, reschedule signal
1328 	 * notification.
1329 	 */
1330 	if (p->p_numthreads != 1)
1331 		reschedule_signals(p, new_block, 0);
1332 
1333 	if (error == 0) {
1334 		SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1335 
1336 		if (ksi->ksi_code == SI_TIMER)
1337 			itimer_accept(p, ksi->ksi_timerid, ksi);
1338 
1339 #ifdef KTRACE
1340 		if (KTRPOINT(td, KTR_PSIG)) {
1341 			sig_t action;
1342 
1343 			mtx_lock(&ps->ps_mtx);
1344 			action = ps->ps_sigact[_SIG_IDX(sig)];
1345 			mtx_unlock(&ps->ps_mtx);
1346 			ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1347 		}
1348 #endif
1349 		if (sig == SIGKILL)
1350 			sigexit(td, sig);
1351 	}
1352 	PROC_UNLOCK(p);
1353 	return (error);
1354 }
1355 
1356 #ifndef _SYS_SYSPROTO_H_
1357 struct sigpending_args {
1358 	sigset_t	*set;
1359 };
1360 #endif
1361 int
1362 sys_sigpending(td, uap)
1363 	struct thread *td;
1364 	struct sigpending_args *uap;
1365 {
1366 	struct proc *p = td->td_proc;
1367 	sigset_t pending;
1368 
1369 	PROC_LOCK(p);
1370 	pending = p->p_sigqueue.sq_signals;
1371 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1372 	PROC_UNLOCK(p);
1373 	return (copyout(&pending, uap->set, sizeof(sigset_t)));
1374 }
1375 
1376 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
1377 #ifndef _SYS_SYSPROTO_H_
1378 struct osigpending_args {
1379 	int	dummy;
1380 };
1381 #endif
1382 int
1383 osigpending(td, uap)
1384 	struct thread *td;
1385 	struct osigpending_args *uap;
1386 {
1387 	struct proc *p = td->td_proc;
1388 	sigset_t pending;
1389 
1390 	PROC_LOCK(p);
1391 	pending = p->p_sigqueue.sq_signals;
1392 	SIGSETOR(pending, td->td_sigqueue.sq_signals);
1393 	PROC_UNLOCK(p);
1394 	SIG2OSIG(pending, td->td_retval[0]);
1395 	return (0);
1396 }
1397 #endif /* COMPAT_43 */
1398 
1399 #if defined(COMPAT_43)
1400 /*
1401  * Generalized interface signal handler, 4.3-compatible.
1402  */
1403 #ifndef _SYS_SYSPROTO_H_
1404 struct osigvec_args {
1405 	int	signum;
1406 	struct	sigvec *nsv;
1407 	struct	sigvec *osv;
1408 };
1409 #endif
1410 /* ARGSUSED */
1411 int
1412 osigvec(td, uap)
1413 	struct thread *td;
1414 	register struct osigvec_args *uap;
1415 {
1416 	struct sigvec vec;
1417 	struct sigaction nsa, osa;
1418 	register struct sigaction *nsap, *osap;
1419 	int error;
1420 
1421 	if (uap->signum <= 0 || uap->signum >= ONSIG)
1422 		return (EINVAL);
1423 	nsap = (uap->nsv != NULL) ? &nsa : NULL;
1424 	osap = (uap->osv != NULL) ? &osa : NULL;
1425 	if (nsap) {
1426 		error = copyin(uap->nsv, &vec, sizeof(vec));
1427 		if (error)
1428 			return (error);
1429 		nsap->sa_handler = vec.sv_handler;
1430 		OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1431 		nsap->sa_flags = vec.sv_flags;
1432 		nsap->sa_flags ^= SA_RESTART;	/* opposite of SV_INTERRUPT */
1433 	}
1434 	error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1435 	if (osap && !error) {
1436 		vec.sv_handler = osap->sa_handler;
1437 		SIG2OSIG(osap->sa_mask, vec.sv_mask);
1438 		vec.sv_flags = osap->sa_flags;
1439 		vec.sv_flags &= ~SA_NOCLDWAIT;
1440 		vec.sv_flags ^= SA_RESTART;
1441 		error = copyout(&vec, uap->osv, sizeof(vec));
1442 	}
1443 	return (error);
1444 }
1445 
1446 #ifndef _SYS_SYSPROTO_H_
1447 struct osigblock_args {
1448 	int	mask;
1449 };
1450 #endif
1451 int
1452 osigblock(td, uap)
1453 	register struct thread *td;
1454 	struct osigblock_args *uap;
1455 {
1456 	sigset_t set, oset;
1457 
1458 	OSIG2SIG(uap->mask, set);
1459 	kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1460 	SIG2OSIG(oset, td->td_retval[0]);
1461 	return (0);
1462 }
1463 
1464 #ifndef _SYS_SYSPROTO_H_
1465 struct osigsetmask_args {
1466 	int	mask;
1467 };
1468 #endif
1469 int
1470 osigsetmask(td, uap)
1471 	struct thread *td;
1472 	struct osigsetmask_args *uap;
1473 {
1474 	sigset_t set, oset;
1475 
1476 	OSIG2SIG(uap->mask, set);
1477 	kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1478 	SIG2OSIG(oset, td->td_retval[0]);
1479 	return (0);
1480 }
1481 #endif /* COMPAT_43 */
1482 
1483 /*
1484  * Suspend calling thread until signal, providing mask to be set in the
1485  * meantime.
1486  */
1487 #ifndef _SYS_SYSPROTO_H_
1488 struct sigsuspend_args {
1489 	const sigset_t *sigmask;
1490 };
1491 #endif
1492 /* ARGSUSED */
1493 int
1494 sys_sigsuspend(td, uap)
1495 	struct thread *td;
1496 	struct sigsuspend_args *uap;
1497 {
1498 	sigset_t mask;
1499 	int error;
1500 
1501 	error = copyin(uap->sigmask, &mask, sizeof(mask));
1502 	if (error)
1503 		return (error);
1504 	return (kern_sigsuspend(td, mask));
1505 }
1506 
1507 int
1508 kern_sigsuspend(struct thread *td, sigset_t mask)
1509 {
1510 	struct proc *p = td->td_proc;
1511 	int has_sig, sig;
1512 
1513 	/*
1514 	 * When returning from sigsuspend, we want
1515 	 * the old mask to be restored after the
1516 	 * signal handler has finished.  Thus, we
1517 	 * save it here and mark the sigacts structure
1518 	 * to indicate this.
1519 	 */
1520 	PROC_LOCK(p);
1521 	kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1522 	    SIGPROCMASK_PROC_LOCKED);
1523 	td->td_pflags |= TDP_OLDMASK;
1524 
1525 	/*
1526 	 * Process signals now. Otherwise, we can get spurious wakeup
1527 	 * due to signal entered process queue, but delivered to other
1528 	 * thread. But sigsuspend should return only on signal
1529 	 * delivery.
1530 	 */
1531 	(p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1532 	for (has_sig = 0; !has_sig;) {
1533 		while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1534 			0) == 0)
1535 			/* void */;
1536 		thread_suspend_check(0);
1537 		mtx_lock(&p->p_sigacts->ps_mtx);
1538 		while ((sig = cursig(td)) != 0) {
1539 			KASSERT(sig >= 0, ("sig %d", sig));
1540 			has_sig += postsig(sig);
1541 		}
1542 		mtx_unlock(&p->p_sigacts->ps_mtx);
1543 	}
1544 	PROC_UNLOCK(p);
1545 	td->td_errno = EINTR;
1546 	td->td_pflags |= TDP_NERRNO;
1547 	return (EJUSTRETURN);
1548 }
1549 
1550 #ifdef COMPAT_43	/* XXX - COMPAT_FBSD3 */
1551 /*
1552  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
1553  * convention: libc stub passes mask, not pointer, to save a copyin.
1554  */
1555 #ifndef _SYS_SYSPROTO_H_
1556 struct osigsuspend_args {
1557 	osigset_t mask;
1558 };
1559 #endif
1560 /* ARGSUSED */
1561 int
1562 osigsuspend(td, uap)
1563 	struct thread *td;
1564 	struct osigsuspend_args *uap;
1565 {
1566 	sigset_t mask;
1567 
1568 	OSIG2SIG(uap->mask, mask);
1569 	return (kern_sigsuspend(td, mask));
1570 }
1571 #endif /* COMPAT_43 */
1572 
1573 #if defined(COMPAT_43)
1574 #ifndef _SYS_SYSPROTO_H_
1575 struct osigstack_args {
1576 	struct	sigstack *nss;
1577 	struct	sigstack *oss;
1578 };
1579 #endif
1580 /* ARGSUSED */
1581 int
1582 osigstack(td, uap)
1583 	struct thread *td;
1584 	register struct osigstack_args *uap;
1585 {
1586 	struct sigstack nss, oss;
1587 	int error = 0;
1588 
1589 	if (uap->nss != NULL) {
1590 		error = copyin(uap->nss, &nss, sizeof(nss));
1591 		if (error)
1592 			return (error);
1593 	}
1594 	oss.ss_sp = td->td_sigstk.ss_sp;
1595 	oss.ss_onstack = sigonstack(cpu_getstack(td));
1596 	if (uap->nss != NULL) {
1597 		td->td_sigstk.ss_sp = nss.ss_sp;
1598 		td->td_sigstk.ss_size = 0;
1599 		td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1600 		td->td_pflags |= TDP_ALTSTACK;
1601 	}
1602 	if (uap->oss != NULL)
1603 		error = copyout(&oss, uap->oss, sizeof(oss));
1604 
1605 	return (error);
1606 }
1607 #endif /* COMPAT_43 */
1608 
1609 #ifndef _SYS_SYSPROTO_H_
1610 struct sigaltstack_args {
1611 	stack_t	*ss;
1612 	stack_t	*oss;
1613 };
1614 #endif
1615 /* ARGSUSED */
1616 int
1617 sys_sigaltstack(td, uap)
1618 	struct thread *td;
1619 	register struct sigaltstack_args *uap;
1620 {
1621 	stack_t ss, oss;
1622 	int error;
1623 
1624 	if (uap->ss != NULL) {
1625 		error = copyin(uap->ss, &ss, sizeof(ss));
1626 		if (error)
1627 			return (error);
1628 	}
1629 	error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1630 	    (uap->oss != NULL) ? &oss : NULL);
1631 	if (error)
1632 		return (error);
1633 	if (uap->oss != NULL)
1634 		error = copyout(&oss, uap->oss, sizeof(stack_t));
1635 	return (error);
1636 }
1637 
1638 int
1639 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1640 {
1641 	struct proc *p = td->td_proc;
1642 	int oonstack;
1643 
1644 	oonstack = sigonstack(cpu_getstack(td));
1645 
1646 	if (oss != NULL) {
1647 		*oss = td->td_sigstk;
1648 		oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1649 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1650 	}
1651 
1652 	if (ss != NULL) {
1653 		if (oonstack)
1654 			return (EPERM);
1655 		if ((ss->ss_flags & ~SS_DISABLE) != 0)
1656 			return (EINVAL);
1657 		if (!(ss->ss_flags & SS_DISABLE)) {
1658 			if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1659 				return (ENOMEM);
1660 
1661 			td->td_sigstk = *ss;
1662 			td->td_pflags |= TDP_ALTSTACK;
1663 		} else {
1664 			td->td_pflags &= ~TDP_ALTSTACK;
1665 		}
1666 	}
1667 	return (0);
1668 }
1669 
1670 /*
1671  * Common code for kill process group/broadcast kill.
1672  * cp is calling process.
1673  */
1674 static int
1675 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1676 {
1677 	struct proc *p;
1678 	struct pgrp *pgrp;
1679 	int err;
1680 	int ret;
1681 
1682 	ret = ESRCH;
1683 	if (all) {
1684 		/*
1685 		 * broadcast
1686 		 */
1687 		sx_slock(&allproc_lock);
1688 		FOREACH_PROC_IN_SYSTEM(p) {
1689 			PROC_LOCK(p);
1690 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1691 			    p == td->td_proc || p->p_state == PRS_NEW) {
1692 				PROC_UNLOCK(p);
1693 				continue;
1694 			}
1695 			err = p_cansignal(td, p, sig);
1696 			if (err == 0) {
1697 				if (sig)
1698 					pksignal(p, sig, ksi);
1699 				ret = err;
1700 			}
1701 			else if (ret == ESRCH)
1702 				ret = err;
1703 			PROC_UNLOCK(p);
1704 		}
1705 		sx_sunlock(&allproc_lock);
1706 	} else {
1707 		sx_slock(&proctree_lock);
1708 		if (pgid == 0) {
1709 			/*
1710 			 * zero pgid means send to my process group.
1711 			 */
1712 			pgrp = td->td_proc->p_pgrp;
1713 			PGRP_LOCK(pgrp);
1714 		} else {
1715 			pgrp = pgfind(pgid);
1716 			if (pgrp == NULL) {
1717 				sx_sunlock(&proctree_lock);
1718 				return (ESRCH);
1719 			}
1720 		}
1721 		sx_sunlock(&proctree_lock);
1722 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1723 			PROC_LOCK(p);
1724 			if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1725 			    p->p_state == PRS_NEW) {
1726 				PROC_UNLOCK(p);
1727 				continue;
1728 			}
1729 			err = p_cansignal(td, p, sig);
1730 			if (err == 0) {
1731 				if (sig)
1732 					pksignal(p, sig, ksi);
1733 				ret = err;
1734 			}
1735 			else if (ret == ESRCH)
1736 				ret = err;
1737 			PROC_UNLOCK(p);
1738 		}
1739 		PGRP_UNLOCK(pgrp);
1740 	}
1741 	return (ret);
1742 }
1743 
1744 #ifndef _SYS_SYSPROTO_H_
1745 struct kill_args {
1746 	int	pid;
1747 	int	signum;
1748 };
1749 #endif
1750 /* ARGSUSED */
1751 int
1752 sys_kill(struct thread *td, struct kill_args *uap)
1753 {
1754 	ksiginfo_t ksi;
1755 	struct proc *p;
1756 	int error;
1757 
1758 	/*
1759 	 * A process in capability mode can send signals only to himself.
1760 	 * The main rationale behind this is that abort(3) is implemented as
1761 	 * kill(getpid(), SIGABRT).
1762 	 */
1763 	if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid)
1764 		return (ECAPMODE);
1765 
1766 	AUDIT_ARG_SIGNUM(uap->signum);
1767 	AUDIT_ARG_PID(uap->pid);
1768 	if ((u_int)uap->signum > _SIG_MAXSIG)
1769 		return (EINVAL);
1770 
1771 	ksiginfo_init(&ksi);
1772 	ksi.ksi_signo = uap->signum;
1773 	ksi.ksi_code = SI_USER;
1774 	ksi.ksi_pid = td->td_proc->p_pid;
1775 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1776 
1777 	if (uap->pid > 0) {
1778 		/* kill single process */
1779 		if ((p = pfind(uap->pid)) == NULL) {
1780 			if ((p = zpfind(uap->pid)) == NULL)
1781 				return (ESRCH);
1782 		}
1783 		AUDIT_ARG_PROCESS(p);
1784 		error = p_cansignal(td, p, uap->signum);
1785 		if (error == 0 && uap->signum)
1786 			pksignal(p, uap->signum, &ksi);
1787 		PROC_UNLOCK(p);
1788 		return (error);
1789 	}
1790 	switch (uap->pid) {
1791 	case -1:		/* broadcast signal */
1792 		return (killpg1(td, uap->signum, 0, 1, &ksi));
1793 	case 0:			/* signal own process group */
1794 		return (killpg1(td, uap->signum, 0, 0, &ksi));
1795 	default:		/* negative explicit process group */
1796 		return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1797 	}
1798 	/* NOTREACHED */
1799 }
1800 
1801 int
1802 sys_pdkill(td, uap)
1803 	struct thread *td;
1804 	struct pdkill_args *uap;
1805 {
1806 	struct proc *p;
1807 	cap_rights_t rights;
1808 	int error;
1809 
1810 	AUDIT_ARG_SIGNUM(uap->signum);
1811 	AUDIT_ARG_FD(uap->fd);
1812 	if ((u_int)uap->signum > _SIG_MAXSIG)
1813 		return (EINVAL);
1814 
1815 	error = procdesc_find(td, uap->fd,
1816 	    cap_rights_init(&rights, CAP_PDKILL), &p);
1817 	if (error)
1818 		return (error);
1819 	AUDIT_ARG_PROCESS(p);
1820 	error = p_cansignal(td, p, uap->signum);
1821 	if (error == 0 && uap->signum)
1822 		kern_psignal(p, uap->signum);
1823 	PROC_UNLOCK(p);
1824 	return (error);
1825 }
1826 
1827 #if defined(COMPAT_43)
1828 #ifndef _SYS_SYSPROTO_H_
1829 struct okillpg_args {
1830 	int	pgid;
1831 	int	signum;
1832 };
1833 #endif
1834 /* ARGSUSED */
1835 int
1836 okillpg(struct thread *td, struct okillpg_args *uap)
1837 {
1838 	ksiginfo_t ksi;
1839 
1840 	AUDIT_ARG_SIGNUM(uap->signum);
1841 	AUDIT_ARG_PID(uap->pgid);
1842 	if ((u_int)uap->signum > _SIG_MAXSIG)
1843 		return (EINVAL);
1844 
1845 	ksiginfo_init(&ksi);
1846 	ksi.ksi_signo = uap->signum;
1847 	ksi.ksi_code = SI_USER;
1848 	ksi.ksi_pid = td->td_proc->p_pid;
1849 	ksi.ksi_uid = td->td_ucred->cr_ruid;
1850 	return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1851 }
1852 #endif /* COMPAT_43 */
1853 
1854 #ifndef _SYS_SYSPROTO_H_
1855 struct sigqueue_args {
1856 	pid_t pid;
1857 	int signum;
1858 	/* union sigval */ void *value;
1859 };
1860 #endif
1861 int
1862 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1863 {
1864 	ksiginfo_t ksi;
1865 	struct proc *p;
1866 	int error;
1867 
1868 	if ((u_int)uap->signum > _SIG_MAXSIG)
1869 		return (EINVAL);
1870 
1871 	/*
1872 	 * Specification says sigqueue can only send signal to
1873 	 * single process.
1874 	 */
1875 	if (uap->pid <= 0)
1876 		return (EINVAL);
1877 
1878 	if ((p = pfind(uap->pid)) == NULL) {
1879 		if ((p = zpfind(uap->pid)) == NULL)
1880 			return (ESRCH);
1881 	}
1882 	error = p_cansignal(td, p, uap->signum);
1883 	if (error == 0 && uap->signum != 0) {
1884 		ksiginfo_init(&ksi);
1885 		ksi.ksi_flags = KSI_SIGQ;
1886 		ksi.ksi_signo = uap->signum;
1887 		ksi.ksi_code = SI_QUEUE;
1888 		ksi.ksi_pid = td->td_proc->p_pid;
1889 		ksi.ksi_uid = td->td_ucred->cr_ruid;
1890 		ksi.ksi_value.sival_ptr = uap->value;
1891 		error = pksignal(p, ksi.ksi_signo, &ksi);
1892 	}
1893 	PROC_UNLOCK(p);
1894 	return (error);
1895 }
1896 
1897 /*
1898  * Send a signal to a process group.
1899  */
1900 void
1901 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1902 {
1903 	struct pgrp *pgrp;
1904 
1905 	if (pgid != 0) {
1906 		sx_slock(&proctree_lock);
1907 		pgrp = pgfind(pgid);
1908 		sx_sunlock(&proctree_lock);
1909 		if (pgrp != NULL) {
1910 			pgsignal(pgrp, sig, 0, ksi);
1911 			PGRP_UNLOCK(pgrp);
1912 		}
1913 	}
1914 }
1915 
1916 /*
1917  * Send a signal to a process group.  If checktty is 1,
1918  * limit to members which have a controlling terminal.
1919  */
1920 void
1921 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1922 {
1923 	struct proc *p;
1924 
1925 	if (pgrp) {
1926 		PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1927 		LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1928 			PROC_LOCK(p);
1929 			if (p->p_state == PRS_NORMAL &&
1930 			    (checkctty == 0 || p->p_flag & P_CONTROLT))
1931 				pksignal(p, sig, ksi);
1932 			PROC_UNLOCK(p);
1933 		}
1934 	}
1935 }
1936 
1937 
1938 /*
1939  * Recalculate the signal mask and reset the signal disposition after
1940  * usermode frame for delivery is formed.  Should be called after
1941  * mach-specific routine, because sysent->sv_sendsig() needs correct
1942  * ps_siginfo and signal mask.
1943  */
1944 static void
1945 postsig_done(int sig, struct thread *td, struct sigacts *ps)
1946 {
1947 	sigset_t mask;
1948 
1949 	mtx_assert(&ps->ps_mtx, MA_OWNED);
1950 	td->td_ru.ru_nsignals++;
1951 	mask = ps->ps_catchmask[_SIG_IDX(sig)];
1952 	if (!SIGISMEMBER(ps->ps_signodefer, sig))
1953 		SIGADDSET(mask, sig);
1954 	kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1955 	    SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1956 	if (SIGISMEMBER(ps->ps_sigreset, sig))
1957 		sigdflt(ps, sig);
1958 }
1959 
1960 
1961 /*
1962  * Send a signal caused by a trap to the current thread.  If it will be
1963  * caught immediately, deliver it with correct code.  Otherwise, post it
1964  * normally.
1965  */
1966 void
1967 trapsignal(struct thread *td, ksiginfo_t *ksi)
1968 {
1969 	struct sigacts *ps;
1970 	struct proc *p;
1971 	int sig;
1972 	int code;
1973 
1974 	p = td->td_proc;
1975 	sig = ksi->ksi_signo;
1976 	code = ksi->ksi_code;
1977 	KASSERT(_SIG_VALID(sig), ("invalid signal"));
1978 
1979 	PROC_LOCK(p);
1980 	ps = p->p_sigacts;
1981 	mtx_lock(&ps->ps_mtx);
1982 	if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1983 	    !SIGISMEMBER(td->td_sigmask, sig)) {
1984 #ifdef KTRACE
1985 		if (KTRPOINT(curthread, KTR_PSIG))
1986 			ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1987 			    &td->td_sigmask, code);
1988 #endif
1989 		(*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1990 				ksi, &td->td_sigmask);
1991 		postsig_done(sig, td, ps);
1992 		mtx_unlock(&ps->ps_mtx);
1993 	} else {
1994 		/*
1995 		 * Avoid a possible infinite loop if the thread
1996 		 * masking the signal or process is ignoring the
1997 		 * signal.
1998 		 */
1999 		if (kern_forcesigexit &&
2000 		    (SIGISMEMBER(td->td_sigmask, sig) ||
2001 		     ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2002 			SIGDELSET(td->td_sigmask, sig);
2003 			SIGDELSET(ps->ps_sigcatch, sig);
2004 			SIGDELSET(ps->ps_sigignore, sig);
2005 			ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2006 		}
2007 		mtx_unlock(&ps->ps_mtx);
2008 		p->p_code = code;	/* XXX for core dump/debugger */
2009 		p->p_sig = sig;		/* XXX to verify code */
2010 		tdsendsignal(p, td, sig, ksi);
2011 	}
2012 	PROC_UNLOCK(p);
2013 }
2014 
2015 static struct thread *
2016 sigtd(struct proc *p, int sig, int prop)
2017 {
2018 	struct thread *td, *signal_td;
2019 
2020 	PROC_LOCK_ASSERT(p, MA_OWNED);
2021 
2022 	/*
2023 	 * Check if current thread can handle the signal without
2024 	 * switching context to another thread.
2025 	 */
2026 	if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
2027 		return (curthread);
2028 	signal_td = NULL;
2029 	FOREACH_THREAD_IN_PROC(p, td) {
2030 		if (!SIGISMEMBER(td->td_sigmask, sig)) {
2031 			signal_td = td;
2032 			break;
2033 		}
2034 	}
2035 	if (signal_td == NULL)
2036 		signal_td = FIRST_THREAD_IN_PROC(p);
2037 	return (signal_td);
2038 }
2039 
2040 /*
2041  * Send the signal to the process.  If the signal has an action, the action
2042  * is usually performed by the target process rather than the caller; we add
2043  * the signal to the set of pending signals for the process.
2044  *
2045  * Exceptions:
2046  *   o When a stop signal is sent to a sleeping process that takes the
2047  *     default action, the process is stopped without awakening it.
2048  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
2049  *     regardless of the signal action (eg, blocked or ignored).
2050  *
2051  * Other ignored signals are discarded immediately.
2052  *
2053  * NB: This function may be entered from the debugger via the "kill" DDB
2054  * command.  There is little that can be done to mitigate the possibly messy
2055  * side effects of this unwise possibility.
2056  */
2057 void
2058 kern_psignal(struct proc *p, int sig)
2059 {
2060 	ksiginfo_t ksi;
2061 
2062 	ksiginfo_init(&ksi);
2063 	ksi.ksi_signo = sig;
2064 	ksi.ksi_code = SI_KERNEL;
2065 	(void) tdsendsignal(p, NULL, sig, &ksi);
2066 }
2067 
2068 int
2069 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2070 {
2071 
2072 	return (tdsendsignal(p, NULL, sig, ksi));
2073 }
2074 
2075 /* Utility function for finding a thread to send signal event to. */
2076 int
2077 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
2078 {
2079 	struct thread *td;
2080 
2081 	if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2082 		td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2083 		if (td == NULL)
2084 			return (ESRCH);
2085 		*ttd = td;
2086 	} else {
2087 		*ttd = NULL;
2088 		PROC_LOCK(p);
2089 	}
2090 	return (0);
2091 }
2092 
2093 void
2094 tdsignal(struct thread *td, int sig)
2095 {
2096 	ksiginfo_t ksi;
2097 
2098 	ksiginfo_init(&ksi);
2099 	ksi.ksi_signo = sig;
2100 	ksi.ksi_code = SI_KERNEL;
2101 	(void) tdsendsignal(td->td_proc, td, sig, &ksi);
2102 }
2103 
2104 void
2105 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2106 {
2107 
2108 	(void) tdsendsignal(td->td_proc, td, sig, ksi);
2109 }
2110 
2111 int
2112 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2113 {
2114 	sig_t action;
2115 	sigqueue_t *sigqueue;
2116 	int prop;
2117 	struct sigacts *ps;
2118 	int intrval;
2119 	int ret = 0;
2120 	int wakeup_swapper;
2121 
2122 	MPASS(td == NULL || p == td->td_proc);
2123 	PROC_LOCK_ASSERT(p, MA_OWNED);
2124 
2125 	if (!_SIG_VALID(sig))
2126 		panic("%s(): invalid signal %d", __func__, sig);
2127 
2128 	KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2129 
2130 	/*
2131 	 * IEEE Std 1003.1-2001: return success when killing a zombie.
2132 	 */
2133 	if (p->p_state == PRS_ZOMBIE) {
2134 		if (ksi && (ksi->ksi_flags & KSI_INS))
2135 			ksiginfo_tryfree(ksi);
2136 		return (ret);
2137 	}
2138 
2139 	ps = p->p_sigacts;
2140 	KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2141 	prop = sigprop(sig);
2142 
2143 	if (td == NULL) {
2144 		td = sigtd(p, sig, prop);
2145 		sigqueue = &p->p_sigqueue;
2146 	} else
2147 		sigqueue = &td->td_sigqueue;
2148 
2149 	SDT_PROBE3(proc, , , signal__send, td, p, sig);
2150 
2151 	/*
2152 	 * If the signal is being ignored,
2153 	 * then we forget about it immediately.
2154 	 * (Note: we don't set SIGCONT in ps_sigignore,
2155 	 * and if it is set to SIG_IGN,
2156 	 * action will be SIG_DFL here.)
2157 	 */
2158 	mtx_lock(&ps->ps_mtx);
2159 	if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2160 		SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2161 
2162 		mtx_unlock(&ps->ps_mtx);
2163 		if (ksi && (ksi->ksi_flags & KSI_INS))
2164 			ksiginfo_tryfree(ksi);
2165 		return (ret);
2166 	}
2167 	if (SIGISMEMBER(td->td_sigmask, sig))
2168 		action = SIG_HOLD;
2169 	else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2170 		action = SIG_CATCH;
2171 	else
2172 		action = SIG_DFL;
2173 	if (SIGISMEMBER(ps->ps_sigintr, sig))
2174 		intrval = EINTR;
2175 	else
2176 		intrval = ERESTART;
2177 	mtx_unlock(&ps->ps_mtx);
2178 
2179 	if (prop & SIGPROP_CONT)
2180 		sigqueue_delete_stopmask_proc(p);
2181 	else if (prop & SIGPROP_STOP) {
2182 		/*
2183 		 * If sending a tty stop signal to a member of an orphaned
2184 		 * process group, discard the signal here if the action
2185 		 * is default; don't stop the process below if sleeping,
2186 		 * and don't clear any pending SIGCONT.
2187 		 */
2188 		if ((prop & SIGPROP_TTYSTOP) &&
2189 		    (p->p_pgrp->pg_jobc == 0) &&
2190 		    (action == SIG_DFL)) {
2191 			if (ksi && (ksi->ksi_flags & KSI_INS))
2192 				ksiginfo_tryfree(ksi);
2193 			return (ret);
2194 		}
2195 		sigqueue_delete_proc(p, SIGCONT);
2196 		if (p->p_flag & P_CONTINUED) {
2197 			p->p_flag &= ~P_CONTINUED;
2198 			PROC_LOCK(p->p_pptr);
2199 			sigqueue_take(p->p_ksi);
2200 			PROC_UNLOCK(p->p_pptr);
2201 		}
2202 	}
2203 
2204 	ret = sigqueue_add(sigqueue, sig, ksi);
2205 	if (ret != 0)
2206 		return (ret);
2207 	signotify(td);
2208 	/*
2209 	 * Defer further processing for signals which are held,
2210 	 * except that stopped processes must be continued by SIGCONT.
2211 	 */
2212 	if (action == SIG_HOLD &&
2213 	    !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2214 		return (ret);
2215 	/*
2216 	 * SIGKILL: Remove procfs STOPEVENTs and ptrace events.
2217 	 */
2218 	if (sig == SIGKILL) {
2219 		p->p_ptevents = 0;
2220 		/* from procfs_ioctl.c: PIOCBIC */
2221 		p->p_stops = 0;
2222 		/* from procfs_ioctl.c: PIOCCONT */
2223 		p->p_step = 0;
2224 		wakeup(&p->p_step);
2225 	}
2226 	/*
2227 	 * Some signals have a process-wide effect and a per-thread
2228 	 * component.  Most processing occurs when the process next
2229 	 * tries to cross the user boundary, however there are some
2230 	 * times when processing needs to be done immediately, such as
2231 	 * waking up threads so that they can cross the user boundary.
2232 	 * We try to do the per-process part here.
2233 	 */
2234 	if (P_SHOULDSTOP(p)) {
2235 		KASSERT(!(p->p_flag & P_WEXIT),
2236 		    ("signal to stopped but exiting process"));
2237 		if (sig == SIGKILL) {
2238 			/*
2239 			 * If traced process is already stopped,
2240 			 * then no further action is necessary.
2241 			 */
2242 			if (p->p_flag & P_TRACED)
2243 				goto out;
2244 			/*
2245 			 * SIGKILL sets process running.
2246 			 * It will die elsewhere.
2247 			 * All threads must be restarted.
2248 			 */
2249 			p->p_flag &= ~P_STOPPED_SIG;
2250 			goto runfast;
2251 		}
2252 
2253 		if (prop & SIGPROP_CONT) {
2254 			/*
2255 			 * If traced process is already stopped,
2256 			 * then no further action is necessary.
2257 			 */
2258 			if (p->p_flag & P_TRACED)
2259 				goto out;
2260 			/*
2261 			 * If SIGCONT is default (or ignored), we continue the
2262 			 * process but don't leave the signal in sigqueue as
2263 			 * it has no further action.  If SIGCONT is held, we
2264 			 * continue the process and leave the signal in
2265 			 * sigqueue.  If the process catches SIGCONT, let it
2266 			 * handle the signal itself.  If it isn't waiting on
2267 			 * an event, it goes back to run state.
2268 			 * Otherwise, process goes back to sleep state.
2269 			 */
2270 			p->p_flag &= ~P_STOPPED_SIG;
2271 			PROC_SLOCK(p);
2272 			if (p->p_numthreads == p->p_suspcount) {
2273 				PROC_SUNLOCK(p);
2274 				p->p_flag |= P_CONTINUED;
2275 				p->p_xsig = SIGCONT;
2276 				PROC_LOCK(p->p_pptr);
2277 				childproc_continued(p);
2278 				PROC_UNLOCK(p->p_pptr);
2279 				PROC_SLOCK(p);
2280 			}
2281 			if (action == SIG_DFL) {
2282 				thread_unsuspend(p);
2283 				PROC_SUNLOCK(p);
2284 				sigqueue_delete(sigqueue, sig);
2285 				goto out;
2286 			}
2287 			if (action == SIG_CATCH) {
2288 				/*
2289 				 * The process wants to catch it so it needs
2290 				 * to run at least one thread, but which one?
2291 				 */
2292 				PROC_SUNLOCK(p);
2293 				goto runfast;
2294 			}
2295 			/*
2296 			 * The signal is not ignored or caught.
2297 			 */
2298 			thread_unsuspend(p);
2299 			PROC_SUNLOCK(p);
2300 			goto out;
2301 		}
2302 
2303 		if (prop & SIGPROP_STOP) {
2304 			/*
2305 			 * If traced process is already stopped,
2306 			 * then no further action is necessary.
2307 			 */
2308 			if (p->p_flag & P_TRACED)
2309 				goto out;
2310 			/*
2311 			 * Already stopped, don't need to stop again
2312 			 * (If we did the shell could get confused).
2313 			 * Just make sure the signal STOP bit set.
2314 			 */
2315 			p->p_flag |= P_STOPPED_SIG;
2316 			sigqueue_delete(sigqueue, sig);
2317 			goto out;
2318 		}
2319 
2320 		/*
2321 		 * All other kinds of signals:
2322 		 * If a thread is sleeping interruptibly, simulate a
2323 		 * wakeup so that when it is continued it will be made
2324 		 * runnable and can look at the signal.  However, don't make
2325 		 * the PROCESS runnable, leave it stopped.
2326 		 * It may run a bit until it hits a thread_suspend_check().
2327 		 */
2328 		wakeup_swapper = 0;
2329 		PROC_SLOCK(p);
2330 		thread_lock(td);
2331 		if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2332 			wakeup_swapper = sleepq_abort(td, intrval);
2333 		thread_unlock(td);
2334 		PROC_SUNLOCK(p);
2335 		if (wakeup_swapper)
2336 			kick_proc0();
2337 		goto out;
2338 		/*
2339 		 * Mutexes are short lived. Threads waiting on them will
2340 		 * hit thread_suspend_check() soon.
2341 		 */
2342 	} else if (p->p_state == PRS_NORMAL) {
2343 		if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2344 			tdsigwakeup(td, sig, action, intrval);
2345 			goto out;
2346 		}
2347 
2348 		MPASS(action == SIG_DFL);
2349 
2350 		if (prop & SIGPROP_STOP) {
2351 			if (p->p_flag & (P_PPWAIT|P_WEXIT))
2352 				goto out;
2353 			p->p_flag |= P_STOPPED_SIG;
2354 			p->p_xsig = sig;
2355 			PROC_SLOCK(p);
2356 			wakeup_swapper = sig_suspend_threads(td, p, 1);
2357 			if (p->p_numthreads == p->p_suspcount) {
2358 				/*
2359 				 * only thread sending signal to another
2360 				 * process can reach here, if thread is sending
2361 				 * signal to its process, because thread does
2362 				 * not suspend itself here, p_numthreads
2363 				 * should never be equal to p_suspcount.
2364 				 */
2365 				thread_stopped(p);
2366 				PROC_SUNLOCK(p);
2367 				sigqueue_delete_proc(p, p->p_xsig);
2368 			} else
2369 				PROC_SUNLOCK(p);
2370 			if (wakeup_swapper)
2371 				kick_proc0();
2372 			goto out;
2373 		}
2374 	} else {
2375 		/* Not in "NORMAL" state. discard the signal. */
2376 		sigqueue_delete(sigqueue, sig);
2377 		goto out;
2378 	}
2379 
2380 	/*
2381 	 * The process is not stopped so we need to apply the signal to all the
2382 	 * running threads.
2383 	 */
2384 runfast:
2385 	tdsigwakeup(td, sig, action, intrval);
2386 	PROC_SLOCK(p);
2387 	thread_unsuspend(p);
2388 	PROC_SUNLOCK(p);
2389 out:
2390 	/* If we jump here, proc slock should not be owned. */
2391 	PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2392 	return (ret);
2393 }
2394 
2395 /*
2396  * The force of a signal has been directed against a single
2397  * thread.  We need to see what we can do about knocking it
2398  * out of any sleep it may be in etc.
2399  */
2400 static void
2401 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2402 {
2403 	struct proc *p = td->td_proc;
2404 	register int prop;
2405 	int wakeup_swapper;
2406 
2407 	wakeup_swapper = 0;
2408 	PROC_LOCK_ASSERT(p, MA_OWNED);
2409 	prop = sigprop(sig);
2410 
2411 	PROC_SLOCK(p);
2412 	thread_lock(td);
2413 	/*
2414 	 * Bring the priority of a thread up if we want it to get
2415 	 * killed in this lifetime.  Be careful to avoid bumping the
2416 	 * priority of the idle thread, since we still allow to signal
2417 	 * kernel processes.
2418 	 */
2419 	if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2420 	    td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2421 		sched_prio(td, PUSER);
2422 	if (TD_ON_SLEEPQ(td)) {
2423 		/*
2424 		 * If thread is sleeping uninterruptibly
2425 		 * we can't interrupt the sleep... the signal will
2426 		 * be noticed when the process returns through
2427 		 * trap() or syscall().
2428 		 */
2429 		if ((td->td_flags & TDF_SINTR) == 0)
2430 			goto out;
2431 		/*
2432 		 * If SIGCONT is default (or ignored) and process is
2433 		 * asleep, we are finished; the process should not
2434 		 * be awakened.
2435 		 */
2436 		if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2437 			thread_unlock(td);
2438 			PROC_SUNLOCK(p);
2439 			sigqueue_delete(&p->p_sigqueue, sig);
2440 			/*
2441 			 * It may be on either list in this state.
2442 			 * Remove from both for now.
2443 			 */
2444 			sigqueue_delete(&td->td_sigqueue, sig);
2445 			return;
2446 		}
2447 
2448 		/*
2449 		 * Don't awaken a sleeping thread for SIGSTOP if the
2450 		 * STOP signal is deferred.
2451 		 */
2452 		if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2453 		    TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2454 			goto out;
2455 
2456 		/*
2457 		 * Give low priority threads a better chance to run.
2458 		 */
2459 		if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2460 			sched_prio(td, PUSER);
2461 
2462 		wakeup_swapper = sleepq_abort(td, intrval);
2463 	} else {
2464 		/*
2465 		 * Other states do nothing with the signal immediately,
2466 		 * other than kicking ourselves if we are running.
2467 		 * It will either never be noticed, or noticed very soon.
2468 		 */
2469 #ifdef SMP
2470 		if (TD_IS_RUNNING(td) && td != curthread)
2471 			forward_signal(td);
2472 #endif
2473 	}
2474 out:
2475 	PROC_SUNLOCK(p);
2476 	thread_unlock(td);
2477 	if (wakeup_swapper)
2478 		kick_proc0();
2479 }
2480 
2481 static int
2482 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2483 {
2484 	struct thread *td2;
2485 	int wakeup_swapper;
2486 
2487 	PROC_LOCK_ASSERT(p, MA_OWNED);
2488 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2489 
2490 	wakeup_swapper = 0;
2491 	FOREACH_THREAD_IN_PROC(p, td2) {
2492 		thread_lock(td2);
2493 		td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2494 		if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2495 		    (td2->td_flags & TDF_SINTR)) {
2496 			if (td2->td_flags & TDF_SBDRY) {
2497 				/*
2498 				 * Once a thread is asleep with
2499 				 * TDF_SBDRY and without TDF_SERESTART
2500 				 * or TDF_SEINTR set, it should never
2501 				 * become suspended due to this check.
2502 				 */
2503 				KASSERT(!TD_IS_SUSPENDED(td2),
2504 				    ("thread with deferred stops suspended"));
2505 				if (TD_SBDRY_INTR(td2) && sending) {
2506 					wakeup_swapper |= sleepq_abort(td2,
2507 					    TD_SBDRY_ERRNO(td2));
2508 				}
2509 			} else if (!TD_IS_SUSPENDED(td2)) {
2510 				thread_suspend_one(td2);
2511 			}
2512 		} else if (!TD_IS_SUSPENDED(td2)) {
2513 			if (sending || td != td2)
2514 				td2->td_flags |= TDF_ASTPENDING;
2515 #ifdef SMP
2516 			if (TD_IS_RUNNING(td2) && td2 != td)
2517 				forward_signal(td2);
2518 #endif
2519 		}
2520 		thread_unlock(td2);
2521 	}
2522 	return (wakeup_swapper);
2523 }
2524 
2525 /*
2526  * Stop the process for an event deemed interesting to the debugger. If si is
2527  * non-NULL, this is a signal exchange; the new signal requested by the
2528  * debugger will be returned for handling. If si is NULL, this is some other
2529  * type of interesting event. The debugger may request a signal be delivered in
2530  * that case as well, however it will be deferred until it can be handled.
2531  */
2532 int
2533 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2534 {
2535 	struct proc *p = td->td_proc;
2536 	struct thread *td2;
2537 	ksiginfo_t ksi;
2538 	int prop;
2539 
2540 	PROC_LOCK_ASSERT(p, MA_OWNED);
2541 	KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2542 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2543 	    &p->p_mtx.lock_object, "Stopping for traced signal");
2544 
2545 	td->td_xsig = sig;
2546 
2547 	if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2548 		td->td_dbgflags |= TDB_XSIG;
2549 		CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2550 		    td->td_tid, p->p_pid, td->td_dbgflags, sig);
2551 		PROC_SLOCK(p);
2552 		while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2553 			if (P_KILLED(p)) {
2554 				/*
2555 				 * Ensure that, if we've been PT_KILLed, the
2556 				 * exit status reflects that. Another thread
2557 				 * may also be in ptracestop(), having just
2558 				 * received the SIGKILL, but this thread was
2559 				 * unsuspended first.
2560 				 */
2561 				td->td_dbgflags &= ~TDB_XSIG;
2562 				td->td_xsig = SIGKILL;
2563 				p->p_ptevents = 0;
2564 				break;
2565 			}
2566 			if (p->p_flag & P_SINGLE_EXIT &&
2567 			    !(td->td_dbgflags & TDB_EXIT)) {
2568 				/*
2569 				 * Ignore ptrace stops except for thread exit
2570 				 * events when the process exits.
2571 				 */
2572 				td->td_dbgflags &= ~TDB_XSIG;
2573 				PROC_SUNLOCK(p);
2574 				return (0);
2575 			}
2576 
2577 			/*
2578 			 * Make wait(2) work.  Ensure that right after the
2579 			 * attach, the thread which was decided to become the
2580 			 * leader of attach gets reported to the waiter.
2581 			 * Otherwise, just avoid overwriting another thread's
2582 			 * assignment to p_xthread.  If another thread has
2583 			 * already set p_xthread, the current thread will get
2584 			 * a chance to report itself upon the next iteration.
2585 			 */
2586 			if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2587 			    ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2588 			    p->p_xthread == NULL)) {
2589 				p->p_xsig = sig;
2590 				p->p_xthread = td;
2591 				td->td_dbgflags &= ~TDB_FSTP;
2592 				p->p_flag2 &= ~P2_PTRACE_FSTP;
2593 				p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2594 				sig_suspend_threads(td, p, 0);
2595 			}
2596 			if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2597 				td->td_dbgflags &= ~TDB_STOPATFORK;
2598 				cv_broadcast(&p->p_dbgwait);
2599 			}
2600 stopme:
2601 			thread_suspend_switch(td, p);
2602 			if (p->p_xthread == td)
2603 				p->p_xthread = NULL;
2604 			if (!(p->p_flag & P_TRACED))
2605 				break;
2606 			if (td->td_dbgflags & TDB_SUSPEND) {
2607 				if (p->p_flag & P_SINGLE_EXIT)
2608 					break;
2609 				goto stopme;
2610 			}
2611 		}
2612 		PROC_SUNLOCK(p);
2613 	}
2614 
2615 	if (si != NULL && sig == td->td_xsig) {
2616 		/* Parent wants us to take the original signal unchanged. */
2617 		si->ksi_flags |= KSI_HEAD;
2618 		if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2619 			si->ksi_signo = 0;
2620 	} else if (td->td_xsig != 0) {
2621 		/*
2622 		 * If parent wants us to take a new signal, then it will leave
2623 		 * it in td->td_xsig; otherwise we just look for signals again.
2624 		 */
2625 		ksiginfo_init(&ksi);
2626 		ksi.ksi_signo = td->td_xsig;
2627 		ksi.ksi_flags |= KSI_PTRACE;
2628 		prop = sigprop(td->td_xsig);
2629 		td2 = sigtd(p, td->td_xsig, prop);
2630 		tdsendsignal(p, td2, td->td_xsig, &ksi);
2631 		if (td != td2)
2632 			return (0);
2633 	}
2634 
2635 	return (td->td_xsig);
2636 }
2637 
2638 static void
2639 reschedule_signals(struct proc *p, sigset_t block, int flags)
2640 {
2641 	struct sigacts *ps;
2642 	struct thread *td;
2643 	int sig;
2644 
2645 	PROC_LOCK_ASSERT(p, MA_OWNED);
2646 	ps = p->p_sigacts;
2647 	mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ?
2648 	    MA_OWNED : MA_NOTOWNED);
2649 	if (SIGISEMPTY(p->p_siglist))
2650 		return;
2651 	SIGSETAND(block, p->p_siglist);
2652 	while ((sig = sig_ffs(&block)) != 0) {
2653 		SIGDELSET(block, sig);
2654 		td = sigtd(p, sig, 0);
2655 		signotify(td);
2656 		if (!(flags & SIGPROCMASK_PS_LOCKED))
2657 			mtx_lock(&ps->ps_mtx);
2658 		if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2659 			tdsigwakeup(td, sig, SIG_CATCH,
2660 			    (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2661 			     ERESTART));
2662 		if (!(flags & SIGPROCMASK_PS_LOCKED))
2663 			mtx_unlock(&ps->ps_mtx);
2664 	}
2665 }
2666 
2667 void
2668 tdsigcleanup(struct thread *td)
2669 {
2670 	struct proc *p;
2671 	sigset_t unblocked;
2672 
2673 	p = td->td_proc;
2674 	PROC_LOCK_ASSERT(p, MA_OWNED);
2675 
2676 	sigqueue_flush(&td->td_sigqueue);
2677 	if (p->p_numthreads == 1)
2678 		return;
2679 
2680 	/*
2681 	 * Since we cannot handle signals, notify signal post code
2682 	 * about this by filling the sigmask.
2683 	 *
2684 	 * Also, if needed, wake up thread(s) that do not block the
2685 	 * same signals as the exiting thread, since the thread might
2686 	 * have been selected for delivery and woken up.
2687 	 */
2688 	SIGFILLSET(unblocked);
2689 	SIGSETNAND(unblocked, td->td_sigmask);
2690 	SIGFILLSET(td->td_sigmask);
2691 	reschedule_signals(p, unblocked, 0);
2692 
2693 }
2694 
2695 static int
2696 sigdeferstop_curr_flags(int cflags)
2697 {
2698 
2699 	MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
2700 	    (cflags & TDF_SBDRY) != 0);
2701 	return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2702 }
2703 
2704 /*
2705  * Defer the delivery of SIGSTOP for the current thread, according to
2706  * the requested mode.  Returns previous flags, which must be restored
2707  * by sigallowstop().
2708  *
2709  * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
2710  * cleared by the current thread, which allow the lock-less read-only
2711  * accesses below.
2712  */
2713 int
2714 sigdeferstop_impl(int mode)
2715 {
2716 	struct thread *td;
2717 	int cflags, nflags;
2718 
2719 	td = curthread;
2720 	cflags = sigdeferstop_curr_flags(td->td_flags);
2721 	switch (mode) {
2722 	case SIGDEFERSTOP_NOP:
2723 		nflags = cflags;
2724 		break;
2725 	case SIGDEFERSTOP_OFF:
2726 		nflags = 0;
2727 		break;
2728 	case SIGDEFERSTOP_SILENT:
2729 		nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
2730 		break;
2731 	case SIGDEFERSTOP_EINTR:
2732 		nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
2733 		break;
2734 	case SIGDEFERSTOP_ERESTART:
2735 		nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
2736 		break;
2737 	default:
2738 		panic("sigdeferstop: invalid mode %x", mode);
2739 		break;
2740 	}
2741 	if (cflags == nflags)
2742 		return (SIGDEFERSTOP_VAL_NCHG);
2743 	thread_lock(td);
2744 	td->td_flags = (td->td_flags & ~cflags) | nflags;
2745 	thread_unlock(td);
2746 	return (cflags);
2747 }
2748 
2749 /*
2750  * Restores the STOP handling mode, typically permitting the delivery
2751  * of SIGSTOP for the current thread.  This does not immediately
2752  * suspend if a stop was posted.  Instead, the thread will suspend
2753  * either via ast() or a subsequent interruptible sleep.
2754  */
2755 void
2756 sigallowstop_impl(int prev)
2757 {
2758 	struct thread *td;
2759 	int cflags;
2760 
2761 	KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
2762 	KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
2763 	    ("sigallowstop: incorrect previous mode %x", prev));
2764 	td = curthread;
2765 	cflags = sigdeferstop_curr_flags(td->td_flags);
2766 	if (cflags != prev) {
2767 		thread_lock(td);
2768 		td->td_flags = (td->td_flags & ~cflags) | prev;
2769 		thread_unlock(td);
2770 	}
2771 }
2772 
2773 /*
2774  * If the current process has received a signal (should be caught or cause
2775  * termination, should interrupt current syscall), return the signal number.
2776  * Stop signals with default action are processed immediately, then cleared;
2777  * they aren't returned.  This is checked after each entry to the system for
2778  * a syscall or trap (though this can usually be done without calling issignal
2779  * by checking the pending signal masks in cursig.) The normal call
2780  * sequence is
2781  *
2782  *	while (sig = cursig(curthread))
2783  *		postsig(sig);
2784  */
2785 static int
2786 issignal(struct thread *td)
2787 {
2788 	struct proc *p;
2789 	struct sigacts *ps;
2790 	struct sigqueue *queue;
2791 	sigset_t sigpending;
2792 	int sig, prop;
2793 
2794 	p = td->td_proc;
2795 	ps = p->p_sigacts;
2796 	mtx_assert(&ps->ps_mtx, MA_OWNED);
2797 	PROC_LOCK_ASSERT(p, MA_OWNED);
2798 	for (;;) {
2799 		int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2800 
2801 		sigpending = td->td_sigqueue.sq_signals;
2802 		SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2803 		SIGSETNAND(sigpending, td->td_sigmask);
2804 
2805 		if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
2806 		    (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2807 			SIG_STOPSIGMASK(sigpending);
2808 		if (SIGISEMPTY(sigpending))	/* no signal to send */
2809 			return (0);
2810 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
2811 		    (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
2812 		    SIGISMEMBER(sigpending, SIGSTOP)) {
2813 			/*
2814 			 * If debugger just attached, always consume
2815 			 * SIGSTOP from ptrace(PT_ATTACH) first, to
2816 			 * execute the debugger attach ritual in
2817 			 * order.
2818 			 */
2819 			sig = SIGSTOP;
2820 			td->td_dbgflags |= TDB_FSTP;
2821 		} else {
2822 			sig = sig_ffs(&sigpending);
2823 		}
2824 
2825 		if (p->p_stops & S_SIG) {
2826 			mtx_unlock(&ps->ps_mtx);
2827 			stopevent(p, S_SIG, sig);
2828 			mtx_lock(&ps->ps_mtx);
2829 		}
2830 
2831 		/*
2832 		 * We should see pending but ignored signals
2833 		 * only if P_TRACED was on when they were posted.
2834 		 */
2835 		if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2836 			sigqueue_delete(&td->td_sigqueue, sig);
2837 			sigqueue_delete(&p->p_sigqueue, sig);
2838 			continue;
2839 		}
2840 		if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
2841 			/*
2842 			 * If traced, always stop.
2843 			 * Remove old signal from queue before the stop.
2844 			 * XXX shrug off debugger, it causes siginfo to
2845 			 * be thrown away.
2846 			 */
2847 			queue = &td->td_sigqueue;
2848 			td->td_dbgksi.ksi_signo = 0;
2849 			if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2850 				queue = &p->p_sigqueue;
2851 				sigqueue_get(queue, sig, &td->td_dbgksi);
2852 			}
2853 
2854 			mtx_unlock(&ps->ps_mtx);
2855 			sig = ptracestop(td, sig, &td->td_dbgksi);
2856 			mtx_lock(&ps->ps_mtx);
2857 
2858 			/*
2859 			 * Keep looking if the debugger discarded the signal
2860 			 * or replaced it with a masked signal.
2861 			 *
2862 			 * If the traced bit got turned off, go back up
2863 			 * to the top to rescan signals.  This ensures
2864 			 * that p_sig* and p_sigact are consistent.
2865 			 */
2866 			if (sig == 0 || (p->p_flag & P_TRACED) == 0)
2867 				continue;
2868 		}
2869 
2870 		prop = sigprop(sig);
2871 
2872 		/*
2873 		 * Decide whether the signal should be returned.
2874 		 * Return the signal's number, or fall through
2875 		 * to clear it from the pending mask.
2876 		 */
2877 		switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2878 
2879 		case (intptr_t)SIG_DFL:
2880 			/*
2881 			 * Don't take default actions on system processes.
2882 			 */
2883 			if (p->p_pid <= 1) {
2884 #ifdef DIAGNOSTIC
2885 				/*
2886 				 * Are you sure you want to ignore SIGSEGV
2887 				 * in init? XXX
2888 				 */
2889 				printf("Process (pid %lu) got signal %d\n",
2890 					(u_long)p->p_pid, sig);
2891 #endif
2892 				break;		/* == ignore */
2893 			}
2894 			/*
2895 			 * If there is a pending stop signal to process
2896 			 * with default action, stop here,
2897 			 * then clear the signal.  However,
2898 			 * if process is member of an orphaned
2899 			 * process group, ignore tty stop signals.
2900 			 */
2901 			if (prop & SIGPROP_STOP) {
2902 				if (p->p_flag & (P_TRACED|P_WEXIT) ||
2903 				    (p->p_pgrp->pg_jobc == 0 &&
2904 				     prop & SIGPROP_TTYSTOP))
2905 					break;	/* == ignore */
2906 				if (TD_SBDRY_INTR(td)) {
2907 					KASSERT((td->td_flags & TDF_SBDRY) != 0,
2908 					    ("lost TDF_SBDRY"));
2909 					return (-1);
2910 				}
2911 				mtx_unlock(&ps->ps_mtx);
2912 				WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2913 				    &p->p_mtx.lock_object, "Catching SIGSTOP");
2914 				sigqueue_delete(&td->td_sigqueue, sig);
2915 				sigqueue_delete(&p->p_sigqueue, sig);
2916 				p->p_flag |= P_STOPPED_SIG;
2917 				p->p_xsig = sig;
2918 				PROC_SLOCK(p);
2919 				sig_suspend_threads(td, p, 0);
2920 				thread_suspend_switch(td, p);
2921 				PROC_SUNLOCK(p);
2922 				mtx_lock(&ps->ps_mtx);
2923 				goto next;
2924 			} else if (prop & SIGPROP_IGNORE) {
2925 				/*
2926 				 * Except for SIGCONT, shouldn't get here.
2927 				 * Default action is to ignore; drop it.
2928 				 */
2929 				break;		/* == ignore */
2930 			} else
2931 				return (sig);
2932 			/*NOTREACHED*/
2933 
2934 		case (intptr_t)SIG_IGN:
2935 			/*
2936 			 * Masking above should prevent us ever trying
2937 			 * to take action on an ignored signal other
2938 			 * than SIGCONT, unless process is traced.
2939 			 */
2940 			if ((prop & SIGPROP_CONT) == 0 &&
2941 			    (p->p_flag & P_TRACED) == 0)
2942 				printf("issignal\n");
2943 			break;		/* == ignore */
2944 
2945 		default:
2946 			/*
2947 			 * This signal has an action, let
2948 			 * postsig() process it.
2949 			 */
2950 			return (sig);
2951 		}
2952 		sigqueue_delete(&td->td_sigqueue, sig);	/* take the signal! */
2953 		sigqueue_delete(&p->p_sigqueue, sig);
2954 next:;
2955 	}
2956 	/* NOTREACHED */
2957 }
2958 
2959 void
2960 thread_stopped(struct proc *p)
2961 {
2962 	int n;
2963 
2964 	PROC_LOCK_ASSERT(p, MA_OWNED);
2965 	PROC_SLOCK_ASSERT(p, MA_OWNED);
2966 	n = p->p_suspcount;
2967 	if (p == curproc)
2968 		n++;
2969 	if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2970 		PROC_SUNLOCK(p);
2971 		p->p_flag &= ~P_WAITED;
2972 		PROC_LOCK(p->p_pptr);
2973 		childproc_stopped(p, (p->p_flag & P_TRACED) ?
2974 			CLD_TRAPPED : CLD_STOPPED);
2975 		PROC_UNLOCK(p->p_pptr);
2976 		PROC_SLOCK(p);
2977 	}
2978 }
2979 
2980 /*
2981  * Take the action for the specified signal
2982  * from the current set of pending signals.
2983  */
2984 int
2985 postsig(sig)
2986 	register int sig;
2987 {
2988 	struct thread *td = curthread;
2989 	register struct proc *p = td->td_proc;
2990 	struct sigacts *ps;
2991 	sig_t action;
2992 	ksiginfo_t ksi;
2993 	sigset_t returnmask;
2994 
2995 	KASSERT(sig != 0, ("postsig"));
2996 
2997 	PROC_LOCK_ASSERT(p, MA_OWNED);
2998 	ps = p->p_sigacts;
2999 	mtx_assert(&ps->ps_mtx, MA_OWNED);
3000 	ksiginfo_init(&ksi);
3001 	if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3002 	    sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3003 		return (0);
3004 	ksi.ksi_signo = sig;
3005 	if (ksi.ksi_code == SI_TIMER)
3006 		itimer_accept(p, ksi.ksi_timerid, &ksi);
3007 	action = ps->ps_sigact[_SIG_IDX(sig)];
3008 #ifdef KTRACE
3009 	if (KTRPOINT(td, KTR_PSIG))
3010 		ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3011 		    &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3012 #endif
3013 	if (p->p_stops & S_SIG) {
3014 		mtx_unlock(&ps->ps_mtx);
3015 		stopevent(p, S_SIG, sig);
3016 		mtx_lock(&ps->ps_mtx);
3017 	}
3018 
3019 	if (action == SIG_DFL) {
3020 		/*
3021 		 * Default action, where the default is to kill
3022 		 * the process.  (Other cases were ignored above.)
3023 		 */
3024 		mtx_unlock(&ps->ps_mtx);
3025 		sigexit(td, sig);
3026 		/* NOTREACHED */
3027 	} else {
3028 		/*
3029 		 * If we get here, the signal must be caught.
3030 		 */
3031 		KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
3032 		    ("postsig action"));
3033 		/*
3034 		 * Set the new mask value and also defer further
3035 		 * occurrences of this signal.
3036 		 *
3037 		 * Special case: user has done a sigsuspend.  Here the
3038 		 * current mask is not of interest, but rather the
3039 		 * mask from before the sigsuspend is what we want
3040 		 * restored after the signal processing is completed.
3041 		 */
3042 		if (td->td_pflags & TDP_OLDMASK) {
3043 			returnmask = td->td_oldsigmask;
3044 			td->td_pflags &= ~TDP_OLDMASK;
3045 		} else
3046 			returnmask = td->td_sigmask;
3047 
3048 		if (p->p_sig == sig) {
3049 			p->p_code = 0;
3050 			p->p_sig = 0;
3051 		}
3052 		(*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3053 		postsig_done(sig, td, ps);
3054 	}
3055 	return (1);
3056 }
3057 
3058 /*
3059  * Kill the current process for stated reason.
3060  */
3061 void
3062 killproc(p, why)
3063 	struct proc *p;
3064 	char *why;
3065 {
3066 
3067 	PROC_LOCK_ASSERT(p, MA_OWNED);
3068 	CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3069 	    p->p_comm);
3070 	log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid,
3071 	    p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why);
3072 	p->p_flag |= P_WKILLED;
3073 	kern_psignal(p, SIGKILL);
3074 }
3075 
3076 /*
3077  * Force the current process to exit with the specified signal, dumping core
3078  * if appropriate.  We bypass the normal tests for masked and caught signals,
3079  * allowing unrecoverable failures to terminate the process without changing
3080  * signal state.  Mark the accounting record with the signal termination.
3081  * If dumping core, save the signal number for the debugger.  Calls exit and
3082  * does not return.
3083  */
3084 void
3085 sigexit(td, sig)
3086 	struct thread *td;
3087 	int sig;
3088 {
3089 	struct proc *p = td->td_proc;
3090 
3091 	PROC_LOCK_ASSERT(p, MA_OWNED);
3092 	p->p_acflag |= AXSIG;
3093 	/*
3094 	 * We must be single-threading to generate a core dump.  This
3095 	 * ensures that the registers in the core file are up-to-date.
3096 	 * Also, the ELF dump handler assumes that the thread list doesn't
3097 	 * change out from under it.
3098 	 *
3099 	 * XXX If another thread attempts to single-thread before us
3100 	 *     (e.g. via fork()), we won't get a dump at all.
3101 	 */
3102 	if ((sigprop(sig) & SIGPROP_CORE) &&
3103 	    thread_single(p, SINGLE_NO_EXIT) == 0) {
3104 		p->p_sig = sig;
3105 		/*
3106 		 * Log signals which would cause core dumps
3107 		 * (Log as LOG_INFO to appease those who don't want
3108 		 * these messages.)
3109 		 * XXX : Todo, as well as euid, write out ruid too
3110 		 * Note that coredump() drops proc lock.
3111 		 */
3112 		if (coredump(td) == 0)
3113 			sig |= WCOREFLAG;
3114 		if (kern_logsigexit)
3115 			log(LOG_INFO,
3116 			    "pid %d (%s), uid %d: exited on signal %d%s\n",
3117 			    p->p_pid, p->p_comm,
3118 			    td->td_ucred ? td->td_ucred->cr_uid : -1,
3119 			    sig &~ WCOREFLAG,
3120 			    sig & WCOREFLAG ? " (core dumped)" : "");
3121 	} else
3122 		PROC_UNLOCK(p);
3123 	exit1(td, 0, sig);
3124 	/* NOTREACHED */
3125 }
3126 
3127 /*
3128  * Send queued SIGCHLD to parent when child process's state
3129  * is changed.
3130  */
3131 static void
3132 sigparent(struct proc *p, int reason, int status)
3133 {
3134 	PROC_LOCK_ASSERT(p, MA_OWNED);
3135 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3136 
3137 	if (p->p_ksi != NULL) {
3138 		p->p_ksi->ksi_signo  = SIGCHLD;
3139 		p->p_ksi->ksi_code   = reason;
3140 		p->p_ksi->ksi_status = status;
3141 		p->p_ksi->ksi_pid    = p->p_pid;
3142 		p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
3143 		if (KSI_ONQ(p->p_ksi))
3144 			return;
3145 	}
3146 	pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3147 }
3148 
3149 static void
3150 childproc_jobstate(struct proc *p, int reason, int sig)
3151 {
3152 	struct sigacts *ps;
3153 
3154 	PROC_LOCK_ASSERT(p, MA_OWNED);
3155 	PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3156 
3157 	/*
3158 	 * Wake up parent sleeping in kern_wait(), also send
3159 	 * SIGCHLD to parent, but SIGCHLD does not guarantee
3160 	 * that parent will awake, because parent may masked
3161 	 * the signal.
3162 	 */
3163 	p->p_pptr->p_flag |= P_STATCHILD;
3164 	wakeup(p->p_pptr);
3165 
3166 	ps = p->p_pptr->p_sigacts;
3167 	mtx_lock(&ps->ps_mtx);
3168 	if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3169 		mtx_unlock(&ps->ps_mtx);
3170 		sigparent(p, reason, sig);
3171 	} else
3172 		mtx_unlock(&ps->ps_mtx);
3173 }
3174 
3175 void
3176 childproc_stopped(struct proc *p, int reason)
3177 {
3178 
3179 	childproc_jobstate(p, reason, p->p_xsig);
3180 }
3181 
3182 void
3183 childproc_continued(struct proc *p)
3184 {
3185 	childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3186 }
3187 
3188 void
3189 childproc_exited(struct proc *p)
3190 {
3191 	int reason, status;
3192 
3193 	if (WCOREDUMP(p->p_xsig)) {
3194 		reason = CLD_DUMPED;
3195 		status = WTERMSIG(p->p_xsig);
3196 	} else if (WIFSIGNALED(p->p_xsig)) {
3197 		reason = CLD_KILLED;
3198 		status = WTERMSIG(p->p_xsig);
3199 	} else {
3200 		reason = CLD_EXITED;
3201 		status = p->p_xexit;
3202 	}
3203 	/*
3204 	 * XXX avoid calling wakeup(p->p_pptr), the work is
3205 	 * done in exit1().
3206 	 */
3207 	sigparent(p, reason, status);
3208 }
3209 
3210 /*
3211  * We only have 1 character for the core count in the format
3212  * string, so the range will be 0-9
3213  */
3214 #define	MAX_NUM_CORE_FILES 10
3215 #ifndef NUM_CORE_FILES
3216 #define	NUM_CORE_FILES 5
3217 #endif
3218 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3219 static int num_cores = NUM_CORE_FILES;
3220 
3221 static int
3222 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3223 {
3224 	int error;
3225 	int new_val;
3226 
3227 	new_val = num_cores;
3228 	error = sysctl_handle_int(oidp, &new_val, 0, req);
3229 	if (error != 0 || req->newptr == NULL)
3230 		return (error);
3231 	if (new_val > MAX_NUM_CORE_FILES)
3232 		new_val = MAX_NUM_CORE_FILES;
3233 	if (new_val < 0)
3234 		new_val = 0;
3235 	num_cores = new_val;
3236 	return (0);
3237 }
3238 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3239 	    0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
3240 
3241 #define	GZ_SUFFIX	".gz"
3242 
3243 #ifdef GZIO
3244 static int compress_user_cores = 1;
3245 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RWTUN,
3246     &compress_user_cores, 0, "Compression of user corefiles");
3247 
3248 int compress_user_cores_gzlevel = 6;
3249 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RWTUN,
3250     &compress_user_cores_gzlevel, 0, "Corefile gzip compression level");
3251 #else
3252 static int compress_user_cores = 0;
3253 #endif
3254 
3255 /*
3256  * Protect the access to corefilename[] by allproc_lock.
3257  */
3258 #define	corefilename_lock	allproc_lock
3259 
3260 static char corefilename[MAXPATHLEN] = {"%N.core"};
3261 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3262 
3263 static int
3264 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3265 {
3266 	int error;
3267 
3268 	sx_xlock(&corefilename_lock);
3269 	error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3270 	    req);
3271 	sx_xunlock(&corefilename_lock);
3272 
3273 	return (error);
3274 }
3275 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3276     CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3277     "Process corefile name format string");
3278 
3279 /*
3280  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3281  * Expand the name described in corefilename, using name, uid, and pid
3282  * and open/create core file.
3283  * corefilename is a printf-like string, with three format specifiers:
3284  *	%N	name of process ("name")
3285  *	%P	process id (pid)
3286  *	%U	user id (uid)
3287  * For example, "%N.core" is the default; they can be disabled completely
3288  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3289  * This is controlled by the sysctl variable kern.corefile (see above).
3290  */
3291 static int
3292 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3293     int compress, struct vnode **vpp, char **namep)
3294 {
3295 	struct nameidata nd;
3296 	struct sbuf sb;
3297 	const char *format;
3298 	char *hostname, *name;
3299 	int indexpos, i, error, cmode, flags, oflags;
3300 
3301 	hostname = NULL;
3302 	format = corefilename;
3303 	name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3304 	indexpos = -1;
3305 	(void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3306 	sx_slock(&corefilename_lock);
3307 	for (i = 0; format[i] != '\0'; i++) {
3308 		switch (format[i]) {
3309 		case '%':	/* Format character */
3310 			i++;
3311 			switch (format[i]) {
3312 			case '%':
3313 				sbuf_putc(&sb, '%');
3314 				break;
3315 			case 'H':	/* hostname */
3316 				if (hostname == NULL) {
3317 					hostname = malloc(MAXHOSTNAMELEN,
3318 					    M_TEMP, M_WAITOK);
3319 				}
3320 				getcredhostname(td->td_ucred, hostname,
3321 				    MAXHOSTNAMELEN);
3322 				sbuf_printf(&sb, "%s", hostname);
3323 				break;
3324 			case 'I':	/* autoincrementing index */
3325 				sbuf_printf(&sb, "0");
3326 				indexpos = sbuf_len(&sb) - 1;
3327 				break;
3328 			case 'N':	/* process name */
3329 				sbuf_printf(&sb, "%s", comm);
3330 				break;
3331 			case 'P':	/* process id */
3332 				sbuf_printf(&sb, "%u", pid);
3333 				break;
3334 			case 'U':	/* user id */
3335 				sbuf_printf(&sb, "%u", uid);
3336 				break;
3337 			default:
3338 				log(LOG_ERR,
3339 				    "Unknown format character %c in "
3340 				    "corename `%s'\n", format[i], format);
3341 				break;
3342 			}
3343 			break;
3344 		default:
3345 			sbuf_putc(&sb, format[i]);
3346 			break;
3347 		}
3348 	}
3349 	sx_sunlock(&corefilename_lock);
3350 	free(hostname, M_TEMP);
3351 	if (compress)
3352 		sbuf_printf(&sb, GZ_SUFFIX);
3353 	if (sbuf_error(&sb) != 0) {
3354 		log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3355 		    "long\n", (long)pid, comm, (u_long)uid);
3356 		sbuf_delete(&sb);
3357 		free(name, M_TEMP);
3358 		return (ENOMEM);
3359 	}
3360 	sbuf_finish(&sb);
3361 	sbuf_delete(&sb);
3362 
3363 	cmode = S_IRUSR | S_IWUSR;
3364 	oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3365 	    (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3366 
3367 	/*
3368 	 * If the core format has a %I in it, then we need to check
3369 	 * for existing corefiles before returning a name.
3370 	 * To do this we iterate over 0..num_cores to find a
3371 	 * non-existing core file name to use.
3372 	 */
3373 	if (indexpos != -1) {
3374 		for (i = 0; i < num_cores; i++) {
3375 			flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3376 			name[indexpos] = '0' + i;
3377 			NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3378 			error = vn_open_cred(&nd, &flags, cmode, oflags,
3379 			    td->td_ucred, NULL);
3380 			if (error) {
3381 				if (error == EEXIST)
3382 					continue;
3383 				log(LOG_ERR,
3384 				    "pid %d (%s), uid (%u):  Path `%s' failed "
3385 				    "on initial open test, error = %d\n",
3386 				    pid, comm, uid, name, error);
3387 			}
3388 			goto out;
3389 		}
3390 	}
3391 
3392 	flags = O_CREAT | FWRITE | O_NOFOLLOW;
3393 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3394 	error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL);
3395 out:
3396 	if (error) {
3397 #ifdef AUDIT
3398 		audit_proc_coredump(td, name, error);
3399 #endif
3400 		free(name, M_TEMP);
3401 		return (error);
3402 	}
3403 	NDFREE(&nd, NDF_ONLY_PNBUF);
3404 	*vpp = nd.ni_vp;
3405 	*namep = name;
3406 	return (0);
3407 }
3408 
3409 static int
3410 coredump_sanitise_path(const char *path)
3411 {
3412 	size_t i;
3413 
3414 	/*
3415 	 * Only send a subset of ASCII to devd(8) because it
3416 	 * might pass these strings to sh -c.
3417 	 */
3418 	for (i = 0; path[i]; i++)
3419 		if (!(isalpha(path[i]) || isdigit(path[i])) &&
3420 		    path[i] != '/' && path[i] != '.' &&
3421 		    path[i] != '-')
3422 			return (0);
3423 
3424 	return (1);
3425 }
3426 
3427 /*
3428  * Dump a process' core.  The main routine does some
3429  * policy checking, and creates the name of the coredump;
3430  * then it passes on a vnode and a size limit to the process-specific
3431  * coredump routine if there is one; if there _is not_ one, it returns
3432  * ENOSYS; otherwise it returns the error from the process-specific routine.
3433  */
3434 
3435 static int
3436 coredump(struct thread *td)
3437 {
3438 	struct proc *p = td->td_proc;
3439 	struct ucred *cred = td->td_ucred;
3440 	struct vnode *vp;
3441 	struct flock lf;
3442 	struct vattr vattr;
3443 	int error, error1, locked;
3444 	char *name;			/* name of corefile */
3445 	void *rl_cookie;
3446 	off_t limit;
3447 	char *data = NULL;
3448 	char *fullpath, *freepath = NULL;
3449 	size_t len;
3450 	static const char comm_name[] = "comm=";
3451 	static const char core_name[] = "core=";
3452 
3453 	PROC_LOCK_ASSERT(p, MA_OWNED);
3454 	MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3455 	_STOPEVENT(p, S_CORE, 0);
3456 
3457 	if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3458 	    (p->p_flag2 & P2_NOTRACE) != 0) {
3459 		PROC_UNLOCK(p);
3460 		return (EFAULT);
3461 	}
3462 
3463 	/*
3464 	 * Note that the bulk of limit checking is done after
3465 	 * the corefile is created.  The exception is if the limit
3466 	 * for corefiles is 0, in which case we don't bother
3467 	 * creating the corefile at all.  This layout means that
3468 	 * a corefile is truncated instead of not being created,
3469 	 * if it is larger than the limit.
3470 	 */
3471 	limit = (off_t)lim_cur(td, RLIMIT_CORE);
3472 	if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3473 		PROC_UNLOCK(p);
3474 		return (EFBIG);
3475 	}
3476 	PROC_UNLOCK(p);
3477 
3478 	error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
3479 	    compress_user_cores, &vp, &name);
3480 	if (error != 0)
3481 		return (error);
3482 
3483 	/*
3484 	 * Don't dump to non-regular files or files with links.
3485 	 * Do not dump into system files.
3486 	 */
3487 	if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3488 	    vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0) {
3489 		VOP_UNLOCK(vp, 0);
3490 		error = EFAULT;
3491 		goto out;
3492 	}
3493 
3494 	VOP_UNLOCK(vp, 0);
3495 
3496 	/* Postpone other writers, including core dumps of other processes. */
3497 	rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3498 
3499 	lf.l_whence = SEEK_SET;
3500 	lf.l_start = 0;
3501 	lf.l_len = 0;
3502 	lf.l_type = F_WRLCK;
3503 	locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3504 
3505 	VATTR_NULL(&vattr);
3506 	vattr.va_size = 0;
3507 	if (set_core_nodump_flag)
3508 		vattr.va_flags = UF_NODUMP;
3509 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3510 	VOP_SETATTR(vp, &vattr, cred);
3511 	VOP_UNLOCK(vp, 0);
3512 	PROC_LOCK(p);
3513 	p->p_acflag |= ACORE;
3514 	PROC_UNLOCK(p);
3515 
3516 	if (p->p_sysent->sv_coredump != NULL) {
3517 		error = p->p_sysent->sv_coredump(td, vp, limit,
3518 		    compress_user_cores ? IMGACT_CORE_COMPRESS : 0);
3519 	} else {
3520 		error = ENOSYS;
3521 	}
3522 
3523 	if (locked) {
3524 		lf.l_type = F_UNLCK;
3525 		VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3526 	}
3527 	vn_rangelock_unlock(vp, rl_cookie);
3528 
3529 	/*
3530 	 * Notify the userland helper that a process triggered a core dump.
3531 	 * This allows the helper to run an automated debugging session.
3532 	 */
3533 	if (error != 0 || coredump_devctl == 0)
3534 		goto out;
3535 	len = MAXPATHLEN * 2 + sizeof(comm_name) - 1 +
3536 	    sizeof(' ') + sizeof(core_name) - 1;
3537 	data = malloc(len, M_TEMP, M_WAITOK);
3538 	if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0)
3539 		goto out;
3540 	if (!coredump_sanitise_path(fullpath))
3541 		goto out;
3542 	snprintf(data, len, "%s%s ", comm_name, fullpath);
3543 	free(freepath, M_TEMP);
3544 	freepath = NULL;
3545 	if (vn_fullpath_global(td, vp, &fullpath, &freepath) != 0)
3546 		goto out;
3547 	if (!coredump_sanitise_path(fullpath))
3548 		goto out;
3549 	strlcat(data, core_name, len);
3550 	strlcat(data, fullpath, len);
3551 	devctl_notify("kernel", "signal", "coredump", data);
3552 out:
3553 	error1 = vn_close(vp, FWRITE, cred, td);
3554 	if (error == 0)
3555 		error = error1;
3556 #ifdef AUDIT
3557 	audit_proc_coredump(td, name, error);
3558 #endif
3559 	free(freepath, M_TEMP);
3560 	free(data, M_TEMP);
3561 	free(name, M_TEMP);
3562 	return (error);
3563 }
3564 
3565 /*
3566  * Nonexistent system call-- signal process (may want to handle it).  Flag
3567  * error in case process won't see signal immediately (blocked or ignored).
3568  */
3569 #ifndef _SYS_SYSPROTO_H_
3570 struct nosys_args {
3571 	int	dummy;
3572 };
3573 #endif
3574 /* ARGSUSED */
3575 int
3576 nosys(td, args)
3577 	struct thread *td;
3578 	struct nosys_args *args;
3579 {
3580 	struct proc *p = td->td_proc;
3581 
3582 	PROC_LOCK(p);
3583 	tdsignal(td, SIGSYS);
3584 	PROC_UNLOCK(p);
3585 	return (ENOSYS);
3586 }
3587 
3588 /*
3589  * Send a SIGIO or SIGURG signal to a process or process group using stored
3590  * credentials rather than those of the current process.
3591  */
3592 void
3593 pgsigio(sigiop, sig, checkctty)
3594 	struct sigio **sigiop;
3595 	int sig, checkctty;
3596 {
3597 	ksiginfo_t ksi;
3598 	struct sigio *sigio;
3599 
3600 	ksiginfo_init(&ksi);
3601 	ksi.ksi_signo = sig;
3602 	ksi.ksi_code = SI_KERNEL;
3603 
3604 	SIGIO_LOCK();
3605 	sigio = *sigiop;
3606 	if (sigio == NULL) {
3607 		SIGIO_UNLOCK();
3608 		return;
3609 	}
3610 	if (sigio->sio_pgid > 0) {
3611 		PROC_LOCK(sigio->sio_proc);
3612 		if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3613 			kern_psignal(sigio->sio_proc, sig);
3614 		PROC_UNLOCK(sigio->sio_proc);
3615 	} else if (sigio->sio_pgid < 0) {
3616 		struct proc *p;
3617 
3618 		PGRP_LOCK(sigio->sio_pgrp);
3619 		LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3620 			PROC_LOCK(p);
3621 			if (p->p_state == PRS_NORMAL &&
3622 			    CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3623 			    (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3624 				kern_psignal(p, sig);
3625 			PROC_UNLOCK(p);
3626 		}
3627 		PGRP_UNLOCK(sigio->sio_pgrp);
3628 	}
3629 	SIGIO_UNLOCK();
3630 }
3631 
3632 static int
3633 filt_sigattach(struct knote *kn)
3634 {
3635 	struct proc *p = curproc;
3636 
3637 	kn->kn_ptr.p_proc = p;
3638 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
3639 
3640 	knlist_add(p->p_klist, kn, 0);
3641 
3642 	return (0);
3643 }
3644 
3645 static void
3646 filt_sigdetach(struct knote *kn)
3647 {
3648 	struct proc *p = kn->kn_ptr.p_proc;
3649 
3650 	knlist_remove(p->p_klist, kn, 0);
3651 }
3652 
3653 /*
3654  * signal knotes are shared with proc knotes, so we apply a mask to
3655  * the hint in order to differentiate them from process hints.  This
3656  * could be avoided by using a signal-specific knote list, but probably
3657  * isn't worth the trouble.
3658  */
3659 static int
3660 filt_signal(struct knote *kn, long hint)
3661 {
3662 
3663 	if (hint & NOTE_SIGNAL) {
3664 		hint &= ~NOTE_SIGNAL;
3665 
3666 		if (kn->kn_id == hint)
3667 			kn->kn_data++;
3668 	}
3669 	return (kn->kn_data != 0);
3670 }
3671 
3672 struct sigacts *
3673 sigacts_alloc(void)
3674 {
3675 	struct sigacts *ps;
3676 
3677 	ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3678 	refcount_init(&ps->ps_refcnt, 1);
3679 	mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3680 	return (ps);
3681 }
3682 
3683 void
3684 sigacts_free(struct sigacts *ps)
3685 {
3686 
3687 	if (refcount_release(&ps->ps_refcnt) == 0)
3688 		return;
3689 	mtx_destroy(&ps->ps_mtx);
3690 	free(ps, M_SUBPROC);
3691 }
3692 
3693 struct sigacts *
3694 sigacts_hold(struct sigacts *ps)
3695 {
3696 
3697 	refcount_acquire(&ps->ps_refcnt);
3698 	return (ps);
3699 }
3700 
3701 void
3702 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3703 {
3704 
3705 	KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3706 	mtx_lock(&src->ps_mtx);
3707 	bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3708 	mtx_unlock(&src->ps_mtx);
3709 }
3710 
3711 int
3712 sigacts_shared(struct sigacts *ps)
3713 {
3714 
3715 	return (ps->ps_refcnt > 1);
3716 }
3717