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