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