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