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