2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/ctype.h>
46 #include <sys/systm.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
50 #include <sys/capsicum.h>
51 #include <sys/compressor.h>
52 #include <sys/condvar.h>
53 #include <sys/devctl.h>
54 #include <sys/event.h>
55 #include <sys/fcntl.h>
56 #include <sys/imgact.h>
57 #include <sys/kernel.h>
59 #include <sys/ktrace.h>
60 #include <sys/limits.h>
62 #include <sys/malloc.h>
63 #include <sys/mutex.h>
64 #include <sys/refcount.h>
65 #include <sys/namei.h>
67 #include <sys/procdesc.h>
68 #include <sys/ptrace.h>
69 #include <sys/posix4.h>
70 #include <sys/racct.h>
71 #include <sys/resourcevar.h>
74 #include <sys/sleepqueue.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysent.h>
81 #include <sys/syslog.h>
82 #include <sys/sysproto.h>
83 #include <sys/timers.h>
84 #include <sys/unistd.h>
87 #include <vm/vm_extern.h>
92 #include <machine/cpu.h>
94 #include <security/audit/audit.h>
96 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
98 SDT_PROVIDER_DECLARE(proc);
99 SDT_PROBE_DEFINE3(proc, , , signal__send,
100 "struct thread *", "struct proc *", "int");
101 SDT_PROBE_DEFINE2(proc, , , signal__clear,
102 "int", "ksiginfo_t *");
103 SDT_PROBE_DEFINE3(proc, , , signal__discard,
104 "struct thread *", "struct proc *", "int");
106 static int coredump(struct thread *);
107 static int killpg1(struct thread *td, int sig, int pgid, int all,
109 static int issignal(struct thread *td);
110 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
111 static int sigprop(int sig);
112 static void tdsigwakeup(struct thread *, int, sig_t, int);
113 static int sig_suspend_threads(struct thread *, struct proc *);
114 static int filt_sigattach(struct knote *kn);
115 static void filt_sigdetach(struct knote *kn);
116 static int filt_signal(struct knote *kn, long hint);
117 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
118 static void sigqueue_start(void);
119 static void sigfastblock_setpend(struct thread *td, bool resched);
121 static uma_zone_t ksiginfo_zone = NULL;
122 struct filterops sig_filtops = {
124 .f_attach = filt_sigattach,
125 .f_detach = filt_sigdetach,
126 .f_event = filt_signal,
129 static int kern_logsigexit = 1;
130 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
132 "Log processes quitting on abnormal signals to syslog(3)");
134 static int kern_forcesigexit = 1;
135 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
136 &kern_forcesigexit, 0, "Force trap signal to be handled");
138 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
139 "POSIX real time signal");
141 static int max_pending_per_proc = 128;
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
143 &max_pending_per_proc, 0, "Max pending signals per proc");
145 static int preallocate_siginfo = 1024;
146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
147 &preallocate_siginfo, 0, "Preallocated signal memory size");
149 static int signal_overflow = 0;
150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
151 &signal_overflow, 0, "Number of signals overflew");
153 static int signal_alloc_fail = 0;
154 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
155 &signal_alloc_fail, 0, "signals failed to be allocated");
157 static int kern_lognosys = 0;
158 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
159 "Log invalid syscalls");
161 __read_frequently bool sigfastblock_fetch_always = false;
162 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
163 &sigfastblock_fetch_always, 0,
164 "Fetch sigfastblock word on each syscall entry for proper "
165 "blocking semantic");
167 static bool kern_sig_discard_ign = true;
168 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
169 &kern_sig_discard_ign, 0,
170 "Discard ignored signals on delivery, otherwise queue them to "
173 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
176 * Policy -- Can ucred cr1 send SIGIO to process cr2?
177 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
178 * in the right situations.
180 #define CANSIGIO(cr1, cr2) \
181 ((cr1)->cr_uid == 0 || \
182 (cr1)->cr_ruid == (cr2)->cr_ruid || \
183 (cr1)->cr_uid == (cr2)->cr_ruid || \
184 (cr1)->cr_ruid == (cr2)->cr_uid || \
185 (cr1)->cr_uid == (cr2)->cr_uid)
187 static int sugid_coredump;
188 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
189 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
191 static int capmode_coredump;
192 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
193 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
195 static int do_coredump = 1;
196 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
197 &do_coredump, 0, "Enable/Disable coredumps");
199 static int set_core_nodump_flag = 0;
200 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
201 0, "Enable setting the NODUMP flag on coredump files");
203 static int coredump_devctl = 0;
204 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
205 0, "Generate a devctl notification when processes coredump");
208 * Signal properties and actions.
209 * The array below categorizes the signals and their default actions
210 * according to the following properties:
212 #define SIGPROP_KILL 0x01 /* terminates process by default */
213 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
214 #define SIGPROP_STOP 0x04 /* suspend process */
215 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
216 #define SIGPROP_IGNORE 0x10 /* ignore by default */
217 #define SIGPROP_CONT 0x20 /* continue if suspended */
219 static int sigproptbl[NSIG] = {
220 [SIGHUP] = SIGPROP_KILL,
221 [SIGINT] = SIGPROP_KILL,
222 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
223 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
224 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
225 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
226 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
227 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
228 [SIGKILL] = SIGPROP_KILL,
229 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
230 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
231 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
232 [SIGPIPE] = SIGPROP_KILL,
233 [SIGALRM] = SIGPROP_KILL,
234 [SIGTERM] = SIGPROP_KILL,
235 [SIGURG] = SIGPROP_IGNORE,
236 [SIGSTOP] = SIGPROP_STOP,
237 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
238 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
239 [SIGCHLD] = SIGPROP_IGNORE,
240 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
241 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
242 [SIGIO] = SIGPROP_IGNORE,
243 [SIGXCPU] = SIGPROP_KILL,
244 [SIGXFSZ] = SIGPROP_KILL,
245 [SIGVTALRM] = SIGPROP_KILL,
246 [SIGPROF] = SIGPROP_KILL,
247 [SIGWINCH] = SIGPROP_IGNORE,
248 [SIGINFO] = SIGPROP_IGNORE,
249 [SIGUSR1] = SIGPROP_KILL,
250 [SIGUSR2] = SIGPROP_KILL,
253 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
257 int __sig = ffs(__bits); \
258 __bits &= ~(1u << (__sig - 1)); \
259 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
263 if (++__i == _SIG_WORDS) { \
267 __bits = (set)->__bits[__i]; \
272 #define SIG_FOREACH(i, set) \
273 for (int32_t __i = -1, __bits = 0; \
274 _SIG_FOREACH_ADVANCE(i, set); ) \
276 static sigset_t fastblock_mask;
279 ast_sig(struct thread *td, int tda)
288 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
289 TDAI(TDA_AST))) == 0) {
293 * Note that TDA_SIG should be re-read from
294 * td_ast, since signal might have been delivered
295 * after we cleared td_flags above. This is one of
296 * the reason for looping check for AST condition.
297 * See comment in userret() about P_PPWAIT.
299 if ((p->p_flag & P_PPWAIT) == 0 &&
300 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
301 if (SIGPENDING(td) && ((tda | td->td_ast) &
302 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
303 thread_unlock(td); /* fix dumps */
305 "failed2 to set signal flags for ast p %p "
306 "td %p tda %#x td_ast %#x fl %#x",
307 p, td, tda, td->td_ast, td->td_flags);
316 * Check for signals. Unlocked reads of p_pendingcnt or
317 * p_siglist might cause process-directed signal to be handled
320 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
321 !SIGISEMPTY(p->p_siglist)) {
322 sigfastblock_fetch(td);
324 mtx_lock(&p->p_sigacts->ps_mtx);
325 while ((sig = cursig(td)) != 0) {
326 KASSERT(sig >= 0, ("sig %d", sig));
329 mtx_unlock(&p->p_sigacts->ps_mtx);
333 resched_sigs = false;
337 * Handle deferred update of the fast sigblock value, after
338 * the postsig() loop was performed.
340 sigfastblock_setpend(td, resched_sigs);
344 ast_sigsuspend(struct thread *td, int tda __unused)
346 MPASS((td->td_pflags & TDP_OLDMASK) != 0);
347 td->td_pflags &= ~TDP_OLDMASK;
348 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
354 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
355 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
356 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
357 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
358 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
359 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
360 SIGFILLSET(fastblock_mask);
361 SIG_CANTMASK(fastblock_mask);
362 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
363 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
364 TDP_OLDMASK, ast_sigsuspend);
368 ksiginfo_alloc(int wait)
372 flags = M_ZERO | (wait ? M_WAITOK : M_NOWAIT);
373 if (ksiginfo_zone != NULL)
374 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
379 ksiginfo_free(ksiginfo_t *ksi)
381 uma_zfree(ksiginfo_zone, ksi);
385 ksiginfo_tryfree(ksiginfo_t *ksi)
387 if (!(ksi->ksi_flags & KSI_EXT)) {
388 uma_zfree(ksiginfo_zone, ksi);
395 sigqueue_init(sigqueue_t *list, struct proc *p)
397 SIGEMPTYSET(list->sq_signals);
398 SIGEMPTYSET(list->sq_kill);
399 SIGEMPTYSET(list->sq_ptrace);
400 TAILQ_INIT(&list->sq_list);
402 list->sq_flags = SQ_INIT;
406 * Get a signal's ksiginfo.
408 * 0 - signal not found
409 * others - signal number
412 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
414 struct proc *p = sq->sq_proc;
415 struct ksiginfo *ksi, *next;
418 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
420 if (!SIGISMEMBER(sq->sq_signals, signo))
423 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
425 SIGDELSET(sq->sq_ptrace, signo);
426 si->ksi_flags |= KSI_PTRACE;
428 if (SIGISMEMBER(sq->sq_kill, signo)) {
431 SIGDELSET(sq->sq_kill, signo);
434 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
435 if (ksi->ksi_signo == signo) {
437 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
438 ksi->ksi_sigq = NULL;
439 ksiginfo_copy(ksi, si);
440 if (ksiginfo_tryfree(ksi) && p != NULL)
449 SIGDELSET(sq->sq_signals, signo);
450 si->ksi_signo = signo;
455 sigqueue_take(ksiginfo_t *ksi)
461 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
465 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
466 ksi->ksi_sigq = NULL;
467 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
470 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
471 kp = TAILQ_NEXT(kp, ksi_link)) {
472 if (kp->ksi_signo == ksi->ksi_signo)
475 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
476 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
477 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
481 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
483 struct proc *p = sq->sq_proc;
484 struct ksiginfo *ksi;
487 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
490 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
493 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
494 SIGADDSET(sq->sq_kill, signo);
498 /* directly insert the ksi, don't copy it */
499 if (si->ksi_flags & KSI_INS) {
500 if (si->ksi_flags & KSI_HEAD)
501 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
503 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
508 if (__predict_false(ksiginfo_zone == NULL)) {
509 SIGADDSET(sq->sq_kill, signo);
513 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
516 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
522 ksiginfo_copy(si, ksi);
523 ksi->ksi_signo = signo;
524 if (si->ksi_flags & KSI_HEAD)
525 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
527 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
532 if ((si->ksi_flags & KSI_PTRACE) != 0) {
533 SIGADDSET(sq->sq_ptrace, signo);
536 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
537 (si->ksi_flags & KSI_SIGQ) == 0) {
538 SIGADDSET(sq->sq_kill, signo);
546 SIGADDSET(sq->sq_signals, signo);
551 sigqueue_flush(sigqueue_t *sq)
553 struct proc *p = sq->sq_proc;
556 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
559 PROC_LOCK_ASSERT(p, MA_OWNED);
561 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
562 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
563 ksi->ksi_sigq = NULL;
564 if (ksiginfo_tryfree(ksi) && p != NULL)
568 SIGEMPTYSET(sq->sq_signals);
569 SIGEMPTYSET(sq->sq_kill);
570 SIGEMPTYSET(sq->sq_ptrace);
574 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
577 struct proc *p1, *p2;
578 ksiginfo_t *ksi, *next;
580 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
581 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
584 /* Move siginfo to target list */
585 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
586 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
587 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
590 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
597 /* Move pending bits to target list */
599 SIGSETAND(tmp, *set);
600 SIGSETOR(dst->sq_kill, tmp);
601 SIGSETNAND(src->sq_kill, tmp);
603 tmp = src->sq_ptrace;
604 SIGSETAND(tmp, *set);
605 SIGSETOR(dst->sq_ptrace, tmp);
606 SIGSETNAND(src->sq_ptrace, tmp);
608 tmp = src->sq_signals;
609 SIGSETAND(tmp, *set);
610 SIGSETOR(dst->sq_signals, tmp);
611 SIGSETNAND(src->sq_signals, tmp);
616 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
621 SIGADDSET(set, signo);
622 sigqueue_move_set(src, dst, &set);
627 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
629 struct proc *p = sq->sq_proc;
630 ksiginfo_t *ksi, *next;
632 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
634 /* Remove siginfo queue */
635 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
636 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
637 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
638 ksi->ksi_sigq = NULL;
639 if (ksiginfo_tryfree(ksi) && p != NULL)
643 SIGSETNAND(sq->sq_kill, *set);
644 SIGSETNAND(sq->sq_ptrace, *set);
645 SIGSETNAND(sq->sq_signals, *set);
649 sigqueue_delete(sigqueue_t *sq, int signo)
654 SIGADDSET(set, signo);
655 sigqueue_delete_set(sq, &set);
658 /* Remove a set of signals for a process */
660 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
665 PROC_LOCK_ASSERT(p, MA_OWNED);
667 sigqueue_init(&worklist, NULL);
668 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
670 FOREACH_THREAD_IN_PROC(p, td0)
671 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
673 sigqueue_flush(&worklist);
677 sigqueue_delete_proc(struct proc *p, int signo)
682 SIGADDSET(set, signo);
683 sigqueue_delete_set_proc(p, &set);
687 sigqueue_delete_stopmask_proc(struct proc *p)
692 SIGADDSET(set, SIGSTOP);
693 SIGADDSET(set, SIGTSTP);
694 SIGADDSET(set, SIGTTIN);
695 SIGADDSET(set, SIGTTOU);
696 sigqueue_delete_set_proc(p, &set);
700 * Determine signal that should be delivered to thread td, the current
701 * thread, 0 if none. If there is a pending stop signal with default
702 * action, the process stops in issignal().
705 cursig(struct thread *td)
707 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
708 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
709 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
710 return (SIGPENDING(td) ? issignal(td) : 0);
714 * Arrange for ast() to handle unmasked pending signals on return to user
715 * mode. This must be called whenever a signal is added to td_sigqueue or
716 * unmasked in td_sigmask.
719 signotify(struct thread *td)
722 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
725 ast_sched(td, TDA_SIG);
729 * Returns 1 (true) if altstack is configured for the thread, and the
730 * passed stack bottom address falls into the altstack range. Handles
731 * the 43 compat special case where the alt stack size is zero.
734 sigonstack(size_t sp)
739 if ((td->td_pflags & TDP_ALTSTACK) == 0)
741 #if defined(COMPAT_43)
742 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
743 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
745 return (sp >= (size_t)td->td_sigstk.ss_sp &&
746 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
753 if (sig > 0 && sig < nitems(sigproptbl))
754 return (sigproptbl[sig]);
759 sigact_flag_test(const struct sigaction *act, int flag)
763 * SA_SIGINFO is reset when signal disposition is set to
764 * ignore or default. Other flags are kept according to user
767 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
768 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
769 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
779 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
780 struct sigaction *oact, int flags)
783 struct proc *p = td->td_proc;
785 if (!_SIG_VALID(sig))
787 if (act != NULL && act->sa_handler != SIG_DFL &&
788 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
789 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
790 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
795 mtx_lock(&ps->ps_mtx);
797 memset(oact, 0, sizeof(*oact));
798 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
799 if (SIGISMEMBER(ps->ps_sigonstack, sig))
800 oact->sa_flags |= SA_ONSTACK;
801 if (!SIGISMEMBER(ps->ps_sigintr, sig))
802 oact->sa_flags |= SA_RESTART;
803 if (SIGISMEMBER(ps->ps_sigreset, sig))
804 oact->sa_flags |= SA_RESETHAND;
805 if (SIGISMEMBER(ps->ps_signodefer, sig))
806 oact->sa_flags |= SA_NODEFER;
807 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
808 oact->sa_flags |= SA_SIGINFO;
810 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
812 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
813 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
814 oact->sa_flags |= SA_NOCLDSTOP;
815 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
816 oact->sa_flags |= SA_NOCLDWAIT;
819 if ((sig == SIGKILL || sig == SIGSTOP) &&
820 act->sa_handler != SIG_DFL) {
821 mtx_unlock(&ps->ps_mtx);
827 * Change setting atomically.
830 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
831 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
832 if (sigact_flag_test(act, SA_SIGINFO)) {
833 ps->ps_sigact[_SIG_IDX(sig)] =
834 (__sighandler_t *)act->sa_sigaction;
835 SIGADDSET(ps->ps_siginfo, sig);
837 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
838 SIGDELSET(ps->ps_siginfo, sig);
840 if (!sigact_flag_test(act, SA_RESTART))
841 SIGADDSET(ps->ps_sigintr, sig);
843 SIGDELSET(ps->ps_sigintr, sig);
844 if (sigact_flag_test(act, SA_ONSTACK))
845 SIGADDSET(ps->ps_sigonstack, sig);
847 SIGDELSET(ps->ps_sigonstack, sig);
848 if (sigact_flag_test(act, SA_RESETHAND))
849 SIGADDSET(ps->ps_sigreset, sig);
851 SIGDELSET(ps->ps_sigreset, sig);
852 if (sigact_flag_test(act, SA_NODEFER))
853 SIGADDSET(ps->ps_signodefer, sig);
855 SIGDELSET(ps->ps_signodefer, sig);
856 if (sig == SIGCHLD) {
857 if (act->sa_flags & SA_NOCLDSTOP)
858 ps->ps_flag |= PS_NOCLDSTOP;
860 ps->ps_flag &= ~PS_NOCLDSTOP;
861 if (act->sa_flags & SA_NOCLDWAIT) {
863 * Paranoia: since SA_NOCLDWAIT is implemented
864 * by reparenting the dying child to PID 1 (and
865 * trust it to reap the zombie), PID 1 itself
866 * is forbidden to set SA_NOCLDWAIT.
869 ps->ps_flag &= ~PS_NOCLDWAIT;
871 ps->ps_flag |= PS_NOCLDWAIT;
873 ps->ps_flag &= ~PS_NOCLDWAIT;
874 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
875 ps->ps_flag |= PS_CLDSIGIGN;
877 ps->ps_flag &= ~PS_CLDSIGIGN;
880 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
881 * and for signals set to SIG_DFL where the default is to
882 * ignore. However, don't put SIGCONT in ps_sigignore, as we
883 * have to restart the process.
885 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
886 (sigprop(sig) & SIGPROP_IGNORE &&
887 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
888 /* never to be seen again */
889 sigqueue_delete_proc(p, sig);
891 /* easier in psignal */
892 SIGADDSET(ps->ps_sigignore, sig);
893 SIGDELSET(ps->ps_sigcatch, sig);
895 SIGDELSET(ps->ps_sigignore, sig);
896 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
897 SIGDELSET(ps->ps_sigcatch, sig);
899 SIGADDSET(ps->ps_sigcatch, sig);
901 #ifdef COMPAT_FREEBSD4
902 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
903 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
904 (flags & KSA_FREEBSD4) == 0)
905 SIGDELSET(ps->ps_freebsd4, sig);
907 SIGADDSET(ps->ps_freebsd4, sig);
910 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
911 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
912 (flags & KSA_OSIGSET) == 0)
913 SIGDELSET(ps->ps_osigset, sig);
915 SIGADDSET(ps->ps_osigset, sig);
918 mtx_unlock(&ps->ps_mtx);
923 #ifndef _SYS_SYSPROTO_H_
924 struct sigaction_args {
926 struct sigaction *act;
927 struct sigaction *oact;
931 sys_sigaction(struct thread *td, struct sigaction_args *uap)
933 struct sigaction act, oact;
934 struct sigaction *actp, *oactp;
937 actp = (uap->act != NULL) ? &act : NULL;
938 oactp = (uap->oact != NULL) ? &oact : NULL;
940 error = copyin(uap->act, actp, sizeof(act));
944 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
946 error = copyout(oactp, uap->oact, sizeof(oact));
950 #ifdef COMPAT_FREEBSD4
951 #ifndef _SYS_SYSPROTO_H_
952 struct freebsd4_sigaction_args {
954 struct sigaction *act;
955 struct sigaction *oact;
959 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
961 struct sigaction act, oact;
962 struct sigaction *actp, *oactp;
965 actp = (uap->act != NULL) ? &act : NULL;
966 oactp = (uap->oact != NULL) ? &oact : NULL;
968 error = copyin(uap->act, actp, sizeof(act));
972 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
974 error = copyout(oactp, uap->oact, sizeof(oact));
977 #endif /* COMAPT_FREEBSD4 */
979 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
980 #ifndef _SYS_SYSPROTO_H_
981 struct osigaction_args {
983 struct osigaction *nsa;
984 struct osigaction *osa;
988 osigaction(struct thread *td, struct osigaction_args *uap)
990 struct osigaction sa;
991 struct sigaction nsa, osa;
992 struct sigaction *nsap, *osap;
995 if (uap->signum <= 0 || uap->signum >= ONSIG)
998 nsap = (uap->nsa != NULL) ? &nsa : NULL;
999 osap = (uap->osa != NULL) ? &osa : NULL;
1002 error = copyin(uap->nsa, &sa, sizeof(sa));
1005 nsap->sa_handler = sa.sa_handler;
1006 nsap->sa_flags = sa.sa_flags;
1007 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
1009 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1010 if (osap && !error) {
1011 sa.sa_handler = osap->sa_handler;
1012 sa.sa_flags = osap->sa_flags;
1013 SIG2OSIG(osap->sa_mask, sa.sa_mask);
1014 error = copyout(&sa, uap->osa, sizeof(sa));
1019 #if !defined(__i386__)
1020 /* Avoid replicating the same stub everywhere */
1022 osigreturn(struct thread *td, struct osigreturn_args *uap)
1025 return (nosys(td, (struct nosys_args *)uap));
1028 #endif /* COMPAT_43 */
1031 * Initialize signal state for process 0;
1032 * set to ignore signals that are ignored by default.
1035 siginit(struct proc *p)
1042 mtx_lock(&ps->ps_mtx);
1043 for (i = 1; i <= NSIG; i++) {
1044 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
1045 SIGADDSET(ps->ps_sigignore, i);
1048 mtx_unlock(&ps->ps_mtx);
1053 * Reset specified signal to the default disposition.
1056 sigdflt(struct sigacts *ps, int sig)
1059 mtx_assert(&ps->ps_mtx, MA_OWNED);
1060 SIGDELSET(ps->ps_sigcatch, sig);
1061 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1062 SIGADDSET(ps->ps_sigignore, sig);
1063 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1064 SIGDELSET(ps->ps_siginfo, sig);
1068 * Reset signals for an exec of the specified process.
1071 execsigs(struct proc *p)
1077 * Reset caught signals. Held signals remain held
1078 * through td_sigmask (unless they were caught,
1079 * and are now ignored by default).
1081 PROC_LOCK_ASSERT(p, MA_OWNED);
1083 mtx_lock(&ps->ps_mtx);
1087 * Reset stack state to the user stack.
1088 * Clear set of signals caught on the signal stack.
1091 MPASS(td->td_proc == p);
1092 td->td_sigstk.ss_flags = SS_DISABLE;
1093 td->td_sigstk.ss_size = 0;
1094 td->td_sigstk.ss_sp = 0;
1095 td->td_pflags &= ~TDP_ALTSTACK;
1097 * Reset no zombies if child dies flag as Solaris does.
1099 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1100 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1101 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1102 mtx_unlock(&ps->ps_mtx);
1106 * kern_sigprocmask()
1108 * Manipulate signal mask.
1111 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1114 sigset_t new_block, oset1;
1119 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1120 PROC_LOCK_ASSERT(p, MA_OWNED);
1123 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1124 ? MA_OWNED : MA_NOTOWNED);
1126 *oset = td->td_sigmask;
1133 oset1 = td->td_sigmask;
1134 SIGSETOR(td->td_sigmask, *set);
1135 new_block = td->td_sigmask;
1136 SIGSETNAND(new_block, oset1);
1139 SIGSETNAND(td->td_sigmask, *set);
1144 oset1 = td->td_sigmask;
1145 if (flags & SIGPROCMASK_OLD)
1146 SIGSETLO(td->td_sigmask, *set);
1148 td->td_sigmask = *set;
1149 new_block = td->td_sigmask;
1150 SIGSETNAND(new_block, oset1);
1159 * The new_block set contains signals that were not previously
1160 * blocked, but are blocked now.
1162 * In case we block any signal that was not previously blocked
1163 * for td, and process has the signal pending, try to schedule
1164 * signal delivery to some thread that does not block the
1165 * signal, possibly waking it up.
1167 if (p->p_numthreads != 1)
1168 reschedule_signals(p, new_block, flags);
1172 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1177 #ifndef _SYS_SYSPROTO_H_
1178 struct sigprocmask_args {
1180 const sigset_t *set;
1185 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1188 sigset_t *setp, *osetp;
1191 setp = (uap->set != NULL) ? &set : NULL;
1192 osetp = (uap->oset != NULL) ? &oset : NULL;
1194 error = copyin(uap->set, setp, sizeof(set));
1198 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1199 if (osetp && !error) {
1200 error = copyout(osetp, uap->oset, sizeof(oset));
1205 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1206 #ifndef _SYS_SYSPROTO_H_
1207 struct osigprocmask_args {
1213 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1218 OSIG2SIG(uap->mask, set);
1219 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1220 SIG2OSIG(oset, td->td_retval[0]);
1223 #endif /* COMPAT_43 */
1226 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1232 error = copyin(uap->set, &set, sizeof(set));
1234 td->td_retval[0] = error;
1238 error = kern_sigtimedwait(td, set, &ksi, NULL);
1241 * sigwait() function shall not return EINTR, but
1242 * the syscall does. Non-ancient libc provides the
1243 * wrapper which hides EINTR. Otherwise, EINTR return
1244 * is used by libthr to handle required cancellation
1245 * point in the sigwait().
1247 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1249 td->td_retval[0] = error;
1253 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1254 td->td_retval[0] = error;
1259 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1262 struct timespec *timeout;
1268 error = copyin(uap->timeout, &ts, sizeof(ts));
1276 error = copyin(uap->set, &set, sizeof(set));
1280 error = kern_sigtimedwait(td, set, &ksi, timeout);
1285 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1288 td->td_retval[0] = ksi.ksi_signo;
1293 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1299 error = copyin(uap->set, &set, sizeof(set));
1303 error = kern_sigtimedwait(td, set, &ksi, NULL);
1308 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1311 td->td_retval[0] = ksi.ksi_signo;
1316 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1320 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1324 thr->td_si.si_signo = 0;
1329 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1330 struct timespec *timeout)
1333 sigset_t saved_mask, new_block;
1335 int error, sig, timevalid = 0;
1336 sbintime_t sbt, precision, tsbt;
1344 /* Ensure the sigfastblock value is up to date. */
1345 sigfastblock_fetch(td);
1347 if (timeout != NULL) {
1348 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1351 if (ts.tv_sec < INT32_MAX / 2) {
1354 precision >>= tc_precexp;
1355 if (TIMESEL(&sbt, tsbt))
1359 precision = sbt = 0;
1362 precision = sbt = 0;
1364 /* Some signals can not be waited for. */
1365 SIG_CANTMASK(waitset);
1368 saved_mask = td->td_sigmask;
1369 SIGSETNAND(td->td_sigmask, waitset);
1370 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1371 !kern_sig_discard_ign) {
1373 td->td_flags |= TDF_SIGWAIT;
1377 mtx_lock(&ps->ps_mtx);
1379 mtx_unlock(&ps->ps_mtx);
1380 KASSERT(sig >= 0, ("sig %d", sig));
1381 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1382 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1383 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1393 * POSIX says this must be checked after looking for pending
1396 if (timeout != NULL && !timevalid) {
1406 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1407 "sigwait", sbt, precision, C_ABSOLUTE);
1409 /* The syscalls can not be restarted. */
1410 if (error == ERESTART)
1414 * If PTRACE_SCE or PTRACE_SCX were set after
1415 * userspace entered the syscall, return spurious
1416 * EINTR after wait was done. Only do this as last
1417 * resort after rechecking for possible queued signals
1418 * and expired timeouts.
1420 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1424 td->td_flags &= ~TDF_SIGWAIT;
1427 new_block = saved_mask;
1428 SIGSETNAND(new_block, td->td_sigmask);
1429 td->td_sigmask = saved_mask;
1431 * Fewer signals can be delivered to us, reschedule signal
1434 if (p->p_numthreads != 1)
1435 reschedule_signals(p, new_block, 0);
1438 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1440 if (ksi->ksi_code == SI_TIMER)
1441 itimer_accept(p, ksi->ksi_timerid, ksi);
1444 if (KTRPOINT(td, KTR_PSIG)) {
1447 mtx_lock(&ps->ps_mtx);
1448 action = ps->ps_sigact[_SIG_IDX(sig)];
1449 mtx_unlock(&ps->ps_mtx);
1450 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1453 if (sig == SIGKILL) {
1454 proc_td_siginfo_capture(td, &ksi->ksi_info);
1462 #ifndef _SYS_SYSPROTO_H_
1463 struct sigpending_args {
1468 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1470 struct proc *p = td->td_proc;
1474 pending = p->p_sigqueue.sq_signals;
1475 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1477 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1480 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1481 #ifndef _SYS_SYSPROTO_H_
1482 struct osigpending_args {
1487 osigpending(struct thread *td, struct osigpending_args *uap)
1489 struct proc *p = td->td_proc;
1493 pending = p->p_sigqueue.sq_signals;
1494 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1496 SIG2OSIG(pending, td->td_retval[0]);
1499 #endif /* COMPAT_43 */
1501 #if defined(COMPAT_43)
1503 * Generalized interface signal handler, 4.3-compatible.
1505 #ifndef _SYS_SYSPROTO_H_
1506 struct osigvec_args {
1514 osigvec(struct thread *td, struct osigvec_args *uap)
1517 struct sigaction nsa, osa;
1518 struct sigaction *nsap, *osap;
1521 if (uap->signum <= 0 || uap->signum >= ONSIG)
1523 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1524 osap = (uap->osv != NULL) ? &osa : NULL;
1526 error = copyin(uap->nsv, &vec, sizeof(vec));
1529 nsap->sa_handler = vec.sv_handler;
1530 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1531 nsap->sa_flags = vec.sv_flags;
1532 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1534 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1535 if (osap && !error) {
1536 vec.sv_handler = osap->sa_handler;
1537 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1538 vec.sv_flags = osap->sa_flags;
1539 vec.sv_flags &= ~SA_NOCLDWAIT;
1540 vec.sv_flags ^= SA_RESTART;
1541 error = copyout(&vec, uap->osv, sizeof(vec));
1546 #ifndef _SYS_SYSPROTO_H_
1547 struct osigblock_args {
1552 osigblock(struct thread *td, struct osigblock_args *uap)
1556 OSIG2SIG(uap->mask, set);
1557 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1558 SIG2OSIG(oset, td->td_retval[0]);
1562 #ifndef _SYS_SYSPROTO_H_
1563 struct osigsetmask_args {
1568 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1572 OSIG2SIG(uap->mask, set);
1573 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1574 SIG2OSIG(oset, td->td_retval[0]);
1577 #endif /* COMPAT_43 */
1580 * Suspend calling thread until signal, providing mask to be set in the
1583 #ifndef _SYS_SYSPROTO_H_
1584 struct sigsuspend_args {
1585 const sigset_t *sigmask;
1590 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1595 error = copyin(uap->sigmask, &mask, sizeof(mask));
1598 return (kern_sigsuspend(td, mask));
1602 kern_sigsuspend(struct thread *td, sigset_t mask)
1604 struct proc *p = td->td_proc;
1607 /* Ensure the sigfastblock value is up to date. */
1608 sigfastblock_fetch(td);
1611 * When returning from sigsuspend, we want
1612 * the old mask to be restored after the
1613 * signal handler has finished. Thus, we
1614 * save it here and mark the sigacts structure
1618 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1619 SIGPROCMASK_PROC_LOCKED);
1620 td->td_pflags |= TDP_OLDMASK;
1621 ast_sched(td, TDA_SIGSUSPEND);
1624 * Process signals now. Otherwise, we can get spurious wakeup
1625 * due to signal entered process queue, but delivered to other
1626 * thread. But sigsuspend should return only on signal
1629 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1630 for (has_sig = 0; !has_sig;) {
1631 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1634 thread_suspend_check(0);
1635 mtx_lock(&p->p_sigacts->ps_mtx);
1636 while ((sig = cursig(td)) != 0) {
1637 KASSERT(sig >= 0, ("sig %d", sig));
1638 has_sig += postsig(sig);
1640 mtx_unlock(&p->p_sigacts->ps_mtx);
1643 * If PTRACE_SCE or PTRACE_SCX were set after
1644 * userspace entered the syscall, return spurious
1647 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1651 td->td_errno = EINTR;
1652 td->td_pflags |= TDP_NERRNO;
1653 return (EJUSTRETURN);
1656 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1658 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1659 * convention: libc stub passes mask, not pointer, to save a copyin.
1661 #ifndef _SYS_SYSPROTO_H_
1662 struct osigsuspend_args {
1668 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1672 OSIG2SIG(uap->mask, mask);
1673 return (kern_sigsuspend(td, mask));
1675 #endif /* COMPAT_43 */
1677 #if defined(COMPAT_43)
1678 #ifndef _SYS_SYSPROTO_H_
1679 struct osigstack_args {
1680 struct sigstack *nss;
1681 struct sigstack *oss;
1686 osigstack(struct thread *td, struct osigstack_args *uap)
1688 struct sigstack nss, oss;
1691 if (uap->nss != NULL) {
1692 error = copyin(uap->nss, &nss, sizeof(nss));
1696 oss.ss_sp = td->td_sigstk.ss_sp;
1697 oss.ss_onstack = sigonstack(cpu_getstack(td));
1698 if (uap->nss != NULL) {
1699 td->td_sigstk.ss_sp = nss.ss_sp;
1700 td->td_sigstk.ss_size = 0;
1701 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1702 td->td_pflags |= TDP_ALTSTACK;
1704 if (uap->oss != NULL)
1705 error = copyout(&oss, uap->oss, sizeof(oss));
1709 #endif /* COMPAT_43 */
1711 #ifndef _SYS_SYSPROTO_H_
1712 struct sigaltstack_args {
1719 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1724 if (uap->ss != NULL) {
1725 error = copyin(uap->ss, &ss, sizeof(ss));
1729 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1730 (uap->oss != NULL) ? &oss : NULL);
1733 if (uap->oss != NULL)
1734 error = copyout(&oss, uap->oss, sizeof(stack_t));
1739 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1741 struct proc *p = td->td_proc;
1744 oonstack = sigonstack(cpu_getstack(td));
1747 *oss = td->td_sigstk;
1748 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1749 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1755 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1757 if (!(ss->ss_flags & SS_DISABLE)) {
1758 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1761 td->td_sigstk = *ss;
1762 td->td_pflags |= TDP_ALTSTACK;
1764 td->td_pflags &= ~TDP_ALTSTACK;
1770 struct killpg1_ctx {
1780 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1784 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1785 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1788 err = p_cansignal(arg->td, p, arg->sig);
1789 if (err == 0 && arg->sig != 0)
1790 pksignal(p, arg->sig, arg->ksi);
1796 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1801 * Common code for kill process group/broadcast kill.
1802 * cp is calling process.
1805 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1809 struct killpg1_ctx arg;
1821 sx_slock(&allproc_lock);
1822 FOREACH_PROC_IN_SYSTEM(p) {
1823 killpg1_sendsig(p, true, &arg);
1825 sx_sunlock(&allproc_lock);
1827 sx_slock(&proctree_lock);
1830 * zero pgid means send to my process group.
1832 pgrp = td->td_proc->p_pgrp;
1835 pgrp = pgfind(pgid);
1837 sx_sunlock(&proctree_lock);
1841 sx_sunlock(&proctree_lock);
1842 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1843 killpg1_sendsig(p, false, &arg);
1847 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1848 if (arg.ret == 0 && !arg.sent)
1849 arg.ret = arg.found ? EPERM : ESRCH;
1853 #ifndef _SYS_SYSPROTO_H_
1861 sys_kill(struct thread *td, struct kill_args *uap)
1864 return (kern_kill(td, uap->pid, uap->signum));
1868 kern_kill(struct thread *td, pid_t pid, int signum)
1875 * A process in capability mode can send signals only to himself.
1876 * The main rationale behind this is that abort(3) is implemented as
1877 * kill(getpid(), SIGABRT).
1879 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1882 AUDIT_ARG_SIGNUM(signum);
1884 if ((u_int)signum > _SIG_MAXSIG)
1887 ksiginfo_init(&ksi);
1888 ksi.ksi_signo = signum;
1889 ksi.ksi_code = SI_USER;
1890 ksi.ksi_pid = td->td_proc->p_pid;
1891 ksi.ksi_uid = td->td_ucred->cr_ruid;
1894 /* kill single process */
1895 if ((p = pfind_any(pid)) == NULL)
1897 AUDIT_ARG_PROCESS(p);
1898 error = p_cansignal(td, p, signum);
1899 if (error == 0 && signum)
1900 pksignal(p, signum, &ksi);
1905 case -1: /* broadcast signal */
1906 return (killpg1(td, signum, 0, 1, &ksi));
1907 case 0: /* signal own process group */
1908 return (killpg1(td, signum, 0, 0, &ksi));
1909 default: /* negative explicit process group */
1910 return (killpg1(td, signum, -pid, 0, &ksi));
1916 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1921 AUDIT_ARG_SIGNUM(uap->signum);
1922 AUDIT_ARG_FD(uap->fd);
1923 if ((u_int)uap->signum > _SIG_MAXSIG)
1926 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1929 AUDIT_ARG_PROCESS(p);
1930 error = p_cansignal(td, p, uap->signum);
1931 if (error == 0 && uap->signum)
1932 kern_psignal(p, uap->signum);
1937 #if defined(COMPAT_43)
1938 #ifndef _SYS_SYSPROTO_H_
1939 struct okillpg_args {
1946 okillpg(struct thread *td, struct okillpg_args *uap)
1950 AUDIT_ARG_SIGNUM(uap->signum);
1951 AUDIT_ARG_PID(uap->pgid);
1952 if ((u_int)uap->signum > _SIG_MAXSIG)
1955 ksiginfo_init(&ksi);
1956 ksi.ksi_signo = uap->signum;
1957 ksi.ksi_code = SI_USER;
1958 ksi.ksi_pid = td->td_proc->p_pid;
1959 ksi.ksi_uid = td->td_ucred->cr_ruid;
1960 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1962 #endif /* COMPAT_43 */
1964 #ifndef _SYS_SYSPROTO_H_
1965 struct sigqueue_args {
1968 /* union sigval */ void *value;
1972 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1976 sv.sival_ptr = uap->value;
1978 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1982 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1988 if ((u_int)signum > _SIG_MAXSIG)
1992 * Specification says sigqueue can only send signal to
1998 if ((p = pfind_any(pid)) == NULL)
2000 error = p_cansignal(td, p, signum);
2001 if (error == 0 && signum != 0) {
2002 ksiginfo_init(&ksi);
2003 ksi.ksi_flags = KSI_SIGQ;
2004 ksi.ksi_signo = signum;
2005 ksi.ksi_code = SI_QUEUE;
2006 ksi.ksi_pid = td->td_proc->p_pid;
2007 ksi.ksi_uid = td->td_ucred->cr_ruid;
2008 ksi.ksi_value = *value;
2009 error = pksignal(p, ksi.ksi_signo, &ksi);
2016 * Send a signal to a process group.
2019 gsignal(int pgid, int sig, ksiginfo_t *ksi)
2024 sx_slock(&proctree_lock);
2025 pgrp = pgfind(pgid);
2026 sx_sunlock(&proctree_lock);
2028 pgsignal(pgrp, sig, 0, ksi);
2035 * Send a signal to a process group. If checktty is 1,
2036 * limit to members which have a controlling terminal.
2039 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2044 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2045 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2047 if (p->p_state == PRS_NORMAL &&
2048 (checkctty == 0 || p->p_flag & P_CONTROLT))
2049 pksignal(p, sig, ksi);
2056 * Recalculate the signal mask and reset the signal disposition after
2057 * usermode frame for delivery is formed. Should be called after
2058 * mach-specific routine, because sysent->sv_sendsig() needs correct
2059 * ps_siginfo and signal mask.
2062 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2066 mtx_assert(&ps->ps_mtx, MA_OWNED);
2067 td->td_ru.ru_nsignals++;
2068 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2069 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2070 SIGADDSET(mask, sig);
2071 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2072 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2073 if (SIGISMEMBER(ps->ps_sigreset, sig))
2078 * Send a signal caused by a trap to the current thread. If it will be
2079 * caught immediately, deliver it with correct code. Otherwise, post it
2083 trapsignal(struct thread *td, ksiginfo_t *ksi)
2091 sig = ksi->ksi_signo;
2092 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2094 sigfastblock_fetch(td);
2097 mtx_lock(&ps->ps_mtx);
2098 sigmask = td->td_sigmask;
2099 if (td->td_sigblock_val != 0)
2100 SIGSETOR(sigmask, fastblock_mask);
2101 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2102 !SIGISMEMBER(sigmask, sig)) {
2104 if (KTRPOINT(curthread, KTR_PSIG))
2105 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2106 &td->td_sigmask, ksi->ksi_code);
2108 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2109 ksi, &td->td_sigmask);
2110 postsig_done(sig, td, ps);
2111 mtx_unlock(&ps->ps_mtx);
2114 * Avoid a possible infinite loop if the thread
2115 * masking the signal or process is ignoring the
2118 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2119 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2120 SIGDELSET(td->td_sigmask, sig);
2121 SIGDELSET(ps->ps_sigcatch, sig);
2122 SIGDELSET(ps->ps_sigignore, sig);
2123 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2124 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2125 td->td_sigblock_val = 0;
2127 mtx_unlock(&ps->ps_mtx);
2128 p->p_sig = sig; /* XXX to verify code */
2129 tdsendsignal(p, td, sig, ksi);
2134 static struct thread *
2135 sigtd(struct proc *p, int sig, bool fast_sigblock)
2137 struct thread *td, *signal_td;
2139 PROC_LOCK_ASSERT(p, MA_OWNED);
2140 MPASS(!fast_sigblock || p == curproc);
2143 * Check if current thread can handle the signal without
2144 * switching context to another thread.
2146 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2147 (!fast_sigblock || curthread->td_sigblock_val == 0))
2150 FOREACH_THREAD_IN_PROC(p, td) {
2151 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2152 td != curthread || td->td_sigblock_val == 0)) {
2157 if (signal_td == NULL)
2158 signal_td = FIRST_THREAD_IN_PROC(p);
2163 * Send the signal to the process. If the signal has an action, the action
2164 * is usually performed by the target process rather than the caller; we add
2165 * the signal to the set of pending signals for the process.
2168 * o When a stop signal is sent to a sleeping process that takes the
2169 * default action, the process is stopped without awakening it.
2170 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2171 * regardless of the signal action (eg, blocked or ignored).
2173 * Other ignored signals are discarded immediately.
2175 * NB: This function may be entered from the debugger via the "kill" DDB
2176 * command. There is little that can be done to mitigate the possibly messy
2177 * side effects of this unwise possibility.
2180 kern_psignal(struct proc *p, int sig)
2184 ksiginfo_init(&ksi);
2185 ksi.ksi_signo = sig;
2186 ksi.ksi_code = SI_KERNEL;
2187 (void) tdsendsignal(p, NULL, sig, &ksi);
2191 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2194 return (tdsendsignal(p, NULL, sig, ksi));
2197 /* Utility function for finding a thread to send signal event to. */
2199 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2203 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2204 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2216 tdsignal(struct thread *td, int sig)
2220 ksiginfo_init(&ksi);
2221 ksi.ksi_signo = sig;
2222 ksi.ksi_code = SI_KERNEL;
2223 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2227 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2230 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2234 sig_sleepq_abort(struct thread *td, int intrval)
2236 THREAD_LOCK_ASSERT(td, MA_OWNED);
2238 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2242 return (sleepq_abort(td, intrval));
2246 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2249 sigqueue_t *sigqueue;
2256 MPASS(td == NULL || p == td->td_proc);
2257 PROC_LOCK_ASSERT(p, MA_OWNED);
2259 if (!_SIG_VALID(sig))
2260 panic("%s(): invalid signal %d", __func__, sig);
2262 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2265 * IEEE Std 1003.1-2001: return success when killing a zombie.
2267 if (p->p_state == PRS_ZOMBIE) {
2268 if (ksi && (ksi->ksi_flags & KSI_INS))
2269 ksiginfo_tryfree(ksi);
2274 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2275 prop = sigprop(sig);
2278 td = sigtd(p, sig, false);
2279 sigqueue = &p->p_sigqueue;
2281 sigqueue = &td->td_sigqueue;
2283 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2286 * If the signal is being ignored, then we forget about it
2287 * immediately, except when the target process executes
2288 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2289 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2291 mtx_lock(&ps->ps_mtx);
2292 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2293 if (kern_sig_discard_ign &&
2294 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2295 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2297 mtx_unlock(&ps->ps_mtx);
2298 if (ksi && (ksi->ksi_flags & KSI_INS))
2299 ksiginfo_tryfree(ksi);
2306 if (SIGISMEMBER(td->td_sigmask, sig))
2308 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2312 if (SIGISMEMBER(ps->ps_sigintr, sig))
2317 mtx_unlock(&ps->ps_mtx);
2319 if (prop & SIGPROP_CONT)
2320 sigqueue_delete_stopmask_proc(p);
2321 else if (prop & SIGPROP_STOP) {
2323 * If sending a tty stop signal to a member of an orphaned
2324 * process group, discard the signal here if the action
2325 * is default; don't stop the process below if sleeping,
2326 * and don't clear any pending SIGCONT.
2328 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2329 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2330 action == SIG_DFL) {
2331 if (ksi && (ksi->ksi_flags & KSI_INS))
2332 ksiginfo_tryfree(ksi);
2335 sigqueue_delete_proc(p, SIGCONT);
2336 if (p->p_flag & P_CONTINUED) {
2337 p->p_flag &= ~P_CONTINUED;
2338 PROC_LOCK(p->p_pptr);
2339 sigqueue_take(p->p_ksi);
2340 PROC_UNLOCK(p->p_pptr);
2344 ret = sigqueue_add(sigqueue, sig, ksi);
2349 * Defer further processing for signals which are held,
2350 * except that stopped processes must be continued by SIGCONT.
2352 if (action == SIG_HOLD &&
2353 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2359 * Some signals have a process-wide effect and a per-thread
2360 * component. Most processing occurs when the process next
2361 * tries to cross the user boundary, however there are some
2362 * times when processing needs to be done immediately, such as
2363 * waking up threads so that they can cross the user boundary.
2364 * We try to do the per-process part here.
2366 if (P_SHOULDSTOP(p)) {
2367 KASSERT(!(p->p_flag & P_WEXIT),
2368 ("signal to stopped but exiting process"));
2369 if (sig == SIGKILL) {
2371 * If traced process is already stopped,
2372 * then no further action is necessary.
2374 if (p->p_flag & P_TRACED)
2377 * SIGKILL sets process running.
2378 * It will die elsewhere.
2379 * All threads must be restarted.
2381 p->p_flag &= ~P_STOPPED_SIG;
2385 if (prop & SIGPROP_CONT) {
2387 * If traced process is already stopped,
2388 * then no further action is necessary.
2390 if (p->p_flag & P_TRACED)
2393 * If SIGCONT is default (or ignored), we continue the
2394 * process but don't leave the signal in sigqueue as
2395 * it has no further action. If SIGCONT is held, we
2396 * continue the process and leave the signal in
2397 * sigqueue. If the process catches SIGCONT, let it
2398 * handle the signal itself. If it isn't waiting on
2399 * an event, it goes back to run state.
2400 * Otherwise, process goes back to sleep state.
2402 p->p_flag &= ~P_STOPPED_SIG;
2404 if (p->p_numthreads == p->p_suspcount) {
2406 p->p_flag |= P_CONTINUED;
2407 p->p_xsig = SIGCONT;
2408 PROC_LOCK(p->p_pptr);
2409 childproc_continued(p);
2410 PROC_UNLOCK(p->p_pptr);
2413 if (action == SIG_DFL) {
2414 thread_unsuspend(p);
2416 sigqueue_delete(sigqueue, sig);
2419 if (action == SIG_CATCH) {
2421 * The process wants to catch it so it needs
2422 * to run at least one thread, but which one?
2428 * The signal is not ignored or caught.
2430 thread_unsuspend(p);
2435 if (prop & SIGPROP_STOP) {
2437 * If traced process is already stopped,
2438 * then no further action is necessary.
2440 if (p->p_flag & P_TRACED)
2443 * Already stopped, don't need to stop again
2444 * (If we did the shell could get confused).
2445 * Just make sure the signal STOP bit set.
2447 p->p_flag |= P_STOPPED_SIG;
2448 sigqueue_delete(sigqueue, sig);
2453 * All other kinds of signals:
2454 * If a thread is sleeping interruptibly, simulate a
2455 * wakeup so that when it is continued it will be made
2456 * runnable and can look at the signal. However, don't make
2457 * the PROCESS runnable, leave it stopped.
2458 * It may run a bit until it hits a thread_suspend_check().
2462 if (TD_CAN_ABORT(td))
2463 wakeup_swapper = sig_sleepq_abort(td, intrval);
2469 * Mutexes are short lived. Threads waiting on them will
2470 * hit thread_suspend_check() soon.
2472 } else if (p->p_state == PRS_NORMAL) {
2473 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2474 tdsigwakeup(td, sig, action, intrval);
2478 MPASS(action == SIG_DFL);
2480 if (prop & SIGPROP_STOP) {
2481 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2483 p->p_flag |= P_STOPPED_SIG;
2486 wakeup_swapper = sig_suspend_threads(td, p);
2487 if (p->p_numthreads == p->p_suspcount) {
2489 * only thread sending signal to another
2490 * process can reach here, if thread is sending
2491 * signal to its process, because thread does
2492 * not suspend itself here, p_numthreads
2493 * should never be equal to p_suspcount.
2497 sigqueue_delete_proc(p, p->p_xsig);
2503 /* Not in "NORMAL" state. discard the signal. */
2504 sigqueue_delete(sigqueue, sig);
2509 * The process is not stopped so we need to apply the signal to all the
2513 tdsigwakeup(td, sig, action, intrval);
2515 thread_unsuspend(p);
2518 itimer_proc_continue(p);
2519 kqtimer_proc_continue(p);
2521 /* If we jump here, proc slock should not be owned. */
2522 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2530 * The force of a signal has been directed against a single
2531 * thread. We need to see what we can do about knocking it
2532 * out of any sleep it may be in etc.
2535 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2537 struct proc *p = td->td_proc;
2538 int prop, wakeup_swapper;
2540 PROC_LOCK_ASSERT(p, MA_OWNED);
2541 prop = sigprop(sig);
2546 * Bring the priority of a thread up if we want it to get
2547 * killed in this lifetime. Be careful to avoid bumping the
2548 * priority of the idle thread, since we still allow to signal
2551 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2552 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2553 sched_prio(td, PUSER);
2554 if (TD_ON_SLEEPQ(td)) {
2556 * If thread is sleeping uninterruptibly
2557 * we can't interrupt the sleep... the signal will
2558 * be noticed when the process returns through
2559 * trap() or syscall().
2561 if ((td->td_flags & TDF_SINTR) == 0)
2564 * If SIGCONT is default (or ignored) and process is
2565 * asleep, we are finished; the process should not
2568 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2571 sigqueue_delete(&p->p_sigqueue, sig);
2573 * It may be on either list in this state.
2574 * Remove from both for now.
2576 sigqueue_delete(&td->td_sigqueue, sig);
2581 * Don't awaken a sleeping thread for SIGSTOP if the
2582 * STOP signal is deferred.
2584 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2585 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2589 * Give low priority threads a better chance to run.
2591 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2592 sched_prio(td, PUSER);
2594 wakeup_swapper = sig_sleepq_abort(td, intrval);
2602 * Other states do nothing with the signal immediately,
2603 * other than kicking ourselves if we are running.
2604 * It will either never be noticed, or noticed very soon.
2607 if (TD_IS_RUNNING(td) && td != curthread)
2617 ptrace_coredump(struct thread *td)
2620 struct thr_coredump_req *tcq;
2623 MPASS(td == curthread);
2625 PROC_LOCK_ASSERT(p, MA_OWNED);
2626 if ((td->td_dbgflags & TDB_COREDUMPRQ) == 0)
2628 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2630 tcq = td->td_coredump;
2631 KASSERT(tcq != NULL, ("td_coredump is NULL"));
2633 if (p->p_sysent->sv_coredump == NULL) {
2634 tcq->tc_error = ENOSYS;
2639 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2641 tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2642 tcq->tc_limit, tcq->tc_flags);
2644 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2647 td->td_dbgflags &= ~TDB_COREDUMPRQ;
2648 td->td_coredump = NULL;
2653 sig_suspend_threads(struct thread *td, struct proc *p)
2658 PROC_LOCK_ASSERT(p, MA_OWNED);
2659 PROC_SLOCK_ASSERT(p, MA_OWNED);
2662 FOREACH_THREAD_IN_PROC(p, td2) {
2664 ast_sched_locked(td2, TDA_SUSPEND);
2665 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2666 (td2->td_flags & TDF_SINTR)) {
2667 if (td2->td_flags & TDF_SBDRY) {
2669 * Once a thread is asleep with
2670 * TDF_SBDRY and without TDF_SERESTART
2671 * or TDF_SEINTR set, it should never
2672 * become suspended due to this check.
2674 KASSERT(!TD_IS_SUSPENDED(td2),
2675 ("thread with deferred stops suspended"));
2676 if (TD_SBDRY_INTR(td2)) {
2677 wakeup_swapper |= sleepq_abort(td2,
2678 TD_SBDRY_ERRNO(td2));
2681 } else if (!TD_IS_SUSPENDED(td2))
2682 thread_suspend_one(td2);
2683 } else if (!TD_IS_SUSPENDED(td2)) {
2685 if (TD_IS_RUNNING(td2) && td2 != td)
2686 forward_signal(td2);
2691 return (wakeup_swapper);
2695 * Stop the process for an event deemed interesting to the debugger. If si is
2696 * non-NULL, this is a signal exchange; the new signal requested by the
2697 * debugger will be returned for handling. If si is NULL, this is some other
2698 * type of interesting event. The debugger may request a signal be delivered in
2699 * that case as well, however it will be deferred until it can be handled.
2702 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2704 struct proc *p = td->td_proc;
2708 PROC_LOCK_ASSERT(p, MA_OWNED);
2709 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2710 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2711 &p->p_mtx.lock_object, "Stopping for traced signal");
2715 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2716 td->td_dbgflags |= TDB_XSIG;
2717 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2718 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2720 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2723 * Ensure that, if we've been PT_KILLed, the
2724 * exit status reflects that. Another thread
2725 * may also be in ptracestop(), having just
2726 * received the SIGKILL, but this thread was
2727 * unsuspended first.
2729 td->td_dbgflags &= ~TDB_XSIG;
2730 td->td_xsig = SIGKILL;
2734 if (p->p_flag & P_SINGLE_EXIT &&
2735 !(td->td_dbgflags & TDB_EXIT)) {
2737 * Ignore ptrace stops except for thread exit
2738 * events when the process exits.
2740 td->td_dbgflags &= ~TDB_XSIG;
2746 * Make wait(2) work. Ensure that right after the
2747 * attach, the thread which was decided to become the
2748 * leader of attach gets reported to the waiter.
2749 * Otherwise, just avoid overwriting another thread's
2750 * assignment to p_xthread. If another thread has
2751 * already set p_xthread, the current thread will get
2752 * a chance to report itself upon the next iteration.
2754 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2755 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2756 p->p_xthread == NULL)) {
2761 * If we are on sleepqueue already,
2762 * let sleepqueue code decide if it
2763 * needs to go sleep after attach.
2765 if (td->td_wchan == NULL)
2766 td->td_dbgflags &= ~TDB_FSTP;
2768 p->p_flag2 &= ~P2_PTRACE_FSTP;
2769 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2770 sig_suspend_threads(td, p);
2772 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2773 td->td_dbgflags &= ~TDB_STOPATFORK;
2776 td->td_dbgflags |= TDB_SSWITCH;
2777 thread_suspend_switch(td, p);
2778 td->td_dbgflags &= ~TDB_SSWITCH;
2779 if ((td->td_dbgflags & TDB_COREDUMPRQ) != 0) {
2781 ptrace_coredump(td);
2785 if (p->p_xthread == td)
2786 p->p_xthread = NULL;
2787 if (!(p->p_flag & P_TRACED))
2789 if (td->td_dbgflags & TDB_SUSPEND) {
2790 if (p->p_flag & P_SINGLE_EXIT)
2798 if (si != NULL && sig == td->td_xsig) {
2799 /* Parent wants us to take the original signal unchanged. */
2800 si->ksi_flags |= KSI_HEAD;
2801 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2803 } else if (td->td_xsig != 0) {
2805 * If parent wants us to take a new signal, then it will leave
2806 * it in td->td_xsig; otherwise we just look for signals again.
2808 ksiginfo_init(&ksi);
2809 ksi.ksi_signo = td->td_xsig;
2810 ksi.ksi_flags |= KSI_PTRACE;
2811 td2 = sigtd(p, td->td_xsig, false);
2812 tdsendsignal(p, td2, td->td_xsig, &ksi);
2817 return (td->td_xsig);
2821 reschedule_signals(struct proc *p, sigset_t block, int flags)
2826 bool fastblk, pslocked;
2828 PROC_LOCK_ASSERT(p, MA_OWNED);
2830 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2831 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2832 if (SIGISEMPTY(p->p_siglist))
2834 SIGSETAND(block, p->p_siglist);
2835 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2836 SIG_FOREACH(sig, &block) {
2837 td = sigtd(p, sig, fastblk);
2840 * If sigtd() selected us despite sigfastblock is
2841 * blocking, do not activate AST or wake us, to avoid
2842 * loop in AST handler.
2844 if (fastblk && td == curthread)
2849 mtx_lock(&ps->ps_mtx);
2850 if (p->p_flag & P_TRACED ||
2851 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2852 !SIGISMEMBER(td->td_sigmask, sig))) {
2853 tdsigwakeup(td, sig, SIG_CATCH,
2854 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2858 mtx_unlock(&ps->ps_mtx);
2863 tdsigcleanup(struct thread *td)
2869 PROC_LOCK_ASSERT(p, MA_OWNED);
2871 sigqueue_flush(&td->td_sigqueue);
2872 if (p->p_numthreads == 1)
2876 * Since we cannot handle signals, notify signal post code
2877 * about this by filling the sigmask.
2879 * Also, if needed, wake up thread(s) that do not block the
2880 * same signals as the exiting thread, since the thread might
2881 * have been selected for delivery and woken up.
2883 SIGFILLSET(unblocked);
2884 SIGSETNAND(unblocked, td->td_sigmask);
2885 SIGFILLSET(td->td_sigmask);
2886 reschedule_signals(p, unblocked, 0);
2891 sigdeferstop_curr_flags(int cflags)
2894 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
2895 (cflags & TDF_SBDRY) != 0);
2896 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2900 * Defer the delivery of SIGSTOP for the current thread, according to
2901 * the requested mode. Returns previous flags, which must be restored
2902 * by sigallowstop().
2904 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
2905 * cleared by the current thread, which allow the lock-less read-only
2909 sigdeferstop_impl(int mode)
2915 cflags = sigdeferstop_curr_flags(td->td_flags);
2917 case SIGDEFERSTOP_NOP:
2920 case SIGDEFERSTOP_OFF:
2923 case SIGDEFERSTOP_SILENT:
2924 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
2926 case SIGDEFERSTOP_EINTR:
2927 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
2929 case SIGDEFERSTOP_ERESTART:
2930 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
2933 panic("sigdeferstop: invalid mode %x", mode);
2936 if (cflags == nflags)
2937 return (SIGDEFERSTOP_VAL_NCHG);
2939 td->td_flags = (td->td_flags & ~cflags) | nflags;
2945 * Restores the STOP handling mode, typically permitting the delivery
2946 * of SIGSTOP for the current thread. This does not immediately
2947 * suspend if a stop was posted. Instead, the thread will suspend
2948 * either via ast() or a subsequent interruptible sleep.
2951 sigallowstop_impl(int prev)
2956 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
2957 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
2958 ("sigallowstop: incorrect previous mode %x", prev));
2960 cflags = sigdeferstop_curr_flags(td->td_flags);
2961 if (cflags != prev) {
2963 td->td_flags = (td->td_flags & ~cflags) | prev;
2972 SIGSTATUS_SBDRY_STOP,
2976 * The thread has signal "sig" pending. Figure out what to do with it:
2978 * _HANDLE -> the caller should handle the signal
2979 * _HANDLED -> handled internally, reload pending signal set
2980 * _IGNORE -> ignored, remove from the set of pending signals and try the
2981 * next pending signal
2982 * _SBDRY_STOP -> the signal should stop the thread but this is not
2983 * permitted in the current context
2985 static enum sigstatus
2986 sigprocess(struct thread *td, int sig)
2990 struct sigqueue *queue;
2994 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
2998 mtx_assert(&ps->ps_mtx, MA_OWNED);
2999 PROC_LOCK_ASSERT(p, MA_OWNED);
3002 * We should allow pending but ignored signals below
3003 * if there is sigwait() active, or P_TRACED was
3004 * on when they were posted.
3006 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3007 (p->p_flag & P_TRACED) == 0 &&
3008 (td->td_flags & TDF_SIGWAIT) == 0) {
3009 return (SIGSTATUS_IGNORE);
3013 * If the process is going to single-thread mode to prepare
3014 * for exit, there is no sense in delivering any signal
3015 * to usermode. Another important consequence is that
3016 * msleep(..., PCATCH, ...) now is only interruptible by a
3019 if ((p->p_flag2 & P2_WEXIT) != 0)
3020 return (SIGSTATUS_IGNORE);
3022 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3024 * If traced, always stop.
3025 * Remove old signal from queue before the stop.
3026 * XXX shrug off debugger, it causes siginfo to
3029 queue = &td->td_sigqueue;
3030 ksiginfo_init(&ksi);
3031 if (sigqueue_get(queue, sig, &ksi) == 0) {
3032 queue = &p->p_sigqueue;
3033 sigqueue_get(queue, sig, &ksi);
3035 td->td_si = ksi.ksi_info;
3037 mtx_unlock(&ps->ps_mtx);
3038 sig = ptracestop(td, sig, &ksi);
3039 mtx_lock(&ps->ps_mtx);
3041 td->td_si.si_signo = 0;
3044 * Keep looking if the debugger discarded or
3045 * replaced the signal.
3048 return (SIGSTATUS_HANDLED);
3051 * If the signal became masked, re-queue it.
3053 if (SIGISMEMBER(td->td_sigmask, sig)) {
3054 ksi.ksi_flags |= KSI_HEAD;
3055 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3056 return (SIGSTATUS_HANDLED);
3060 * If the traced bit got turned off, requeue the signal and
3061 * reload the set of pending signals. This ensures that p_sig*
3062 * and p_sigact are consistent.
3064 if ((p->p_flag & P_TRACED) == 0) {
3065 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3066 ksi.ksi_flags |= KSI_HEAD;
3067 sigqueue_add(queue, sig, &ksi);
3069 return (SIGSTATUS_HANDLED);
3074 * Decide whether the signal should be returned.
3075 * Return the signal's number, or fall through
3076 * to clear it from the pending mask.
3078 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3079 case (intptr_t)SIG_DFL:
3081 * Don't take default actions on system processes.
3083 if (p->p_pid <= 1) {
3086 * Are you sure you want to ignore SIGSEGV
3089 printf("Process (pid %lu) got signal %d\n",
3090 (u_long)p->p_pid, sig);
3092 return (SIGSTATUS_IGNORE);
3096 * If there is a pending stop signal to process with
3097 * default action, stop here, then clear the signal.
3098 * Traced or exiting processes should ignore stops.
3099 * Additionally, a member of an orphaned process group
3100 * should ignore tty stops.
3102 prop = sigprop(sig);
3103 if (prop & SIGPROP_STOP) {
3104 mtx_unlock(&ps->ps_mtx);
3105 if ((p->p_flag & (P_TRACED | P_WEXIT |
3106 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3107 pg_flags & PGRP_ORPHANED) != 0 &&
3108 (prop & SIGPROP_TTYSTOP) != 0)) {
3109 mtx_lock(&ps->ps_mtx);
3110 return (SIGSTATUS_IGNORE);
3112 if (TD_SBDRY_INTR(td)) {
3113 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3114 ("lost TDF_SBDRY"));
3115 mtx_lock(&ps->ps_mtx);
3116 return (SIGSTATUS_SBDRY_STOP);
3118 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3119 &p->p_mtx.lock_object, "Catching SIGSTOP");
3120 sigqueue_delete(&td->td_sigqueue, sig);
3121 sigqueue_delete(&p->p_sigqueue, sig);
3122 p->p_flag |= P_STOPPED_SIG;
3125 sig_suspend_threads(td, p);
3126 thread_suspend_switch(td, p);
3128 mtx_lock(&ps->ps_mtx);
3129 return (SIGSTATUS_HANDLED);
3130 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3131 (td->td_flags & TDF_SIGWAIT) == 0) {
3133 * Default action is to ignore; drop it if
3134 * not in kern_sigtimedwait().
3136 return (SIGSTATUS_IGNORE);
3138 return (SIGSTATUS_HANDLE);
3141 case (intptr_t)SIG_IGN:
3142 if ((td->td_flags & TDF_SIGWAIT) == 0)
3143 return (SIGSTATUS_IGNORE);
3145 return (SIGSTATUS_HANDLE);
3149 * This signal has an action, let postsig() process it.
3151 return (SIGSTATUS_HANDLE);
3156 * If the current process has received a signal (should be caught or cause
3157 * termination, should interrupt current syscall), return the signal number.
3158 * Stop signals with default action are processed immediately, then cleared;
3159 * they aren't returned. This is checked after each entry to the system for
3160 * a syscall or trap (though this can usually be done without calling
3161 * issignal by checking the pending signal masks in cursig.) The normal call
3164 * while (sig = cursig(curthread))
3168 issignal(struct thread *td)
3171 sigset_t sigpending;
3175 PROC_LOCK_ASSERT(p, MA_OWNED);
3178 sigpending = td->td_sigqueue.sq_signals;
3179 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3180 SIGSETNAND(sigpending, td->td_sigmask);
3182 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3183 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3184 SIG_STOPSIGMASK(sigpending);
3185 if (SIGISEMPTY(sigpending)) /* no signal to send */
3189 * Do fast sigblock if requested by usermode. Since
3190 * we do know that there was a signal pending at this
3191 * point, set the FAST_SIGBLOCK_PEND as indicator for
3192 * usermode to perform a dummy call to
3193 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3194 * delivery of postponed pending signal.
3196 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3197 if (td->td_sigblock_val != 0)
3198 SIGSETNAND(sigpending, fastblock_mask);
3199 if (SIGISEMPTY(sigpending)) {
3200 td->td_pflags |= TDP_SIGFASTPENDING;
3205 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3206 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3207 SIGISMEMBER(sigpending, SIGSTOP)) {
3209 * If debugger just attached, always consume
3210 * SIGSTOP from ptrace(PT_ATTACH) first, to
3211 * execute the debugger attach ritual in
3214 td->td_dbgflags |= TDB_FSTP;
3215 SIGEMPTYSET(sigpending);
3216 SIGADDSET(sigpending, SIGSTOP);
3219 SIG_FOREACH(sig, &sigpending) {
3220 switch (sigprocess(td, sig)) {
3221 case SIGSTATUS_HANDLE:
3223 case SIGSTATUS_HANDLED:
3225 case SIGSTATUS_IGNORE:
3226 sigqueue_delete(&td->td_sigqueue, sig);
3227 sigqueue_delete(&p->p_sigqueue, sig);
3229 case SIGSTATUS_SBDRY_STOP:
3238 thread_stopped(struct proc *p)
3242 PROC_LOCK_ASSERT(p, MA_OWNED);
3243 PROC_SLOCK_ASSERT(p, MA_OWNED);
3247 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3249 p->p_flag &= ~P_WAITED;
3250 PROC_LOCK(p->p_pptr);
3251 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3252 CLD_TRAPPED : CLD_STOPPED);
3253 PROC_UNLOCK(p->p_pptr);
3259 * Take the action for the specified signal
3260 * from the current set of pending signals.
3270 sigset_t returnmask;
3272 KASSERT(sig != 0, ("postsig"));
3276 PROC_LOCK_ASSERT(p, MA_OWNED);
3278 mtx_assert(&ps->ps_mtx, MA_OWNED);
3279 ksiginfo_init(&ksi);
3280 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3281 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3283 ksi.ksi_signo = sig;
3284 if (ksi.ksi_code == SI_TIMER)
3285 itimer_accept(p, ksi.ksi_timerid, &ksi);
3286 action = ps->ps_sigact[_SIG_IDX(sig)];
3288 if (KTRPOINT(td, KTR_PSIG))
3289 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3290 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3293 if (action == SIG_DFL) {
3295 * Default action, where the default is to kill
3296 * the process. (Other cases were ignored above.)
3298 mtx_unlock(&ps->ps_mtx);
3299 proc_td_siginfo_capture(td, &ksi.ksi_info);
3304 * If we get here, the signal must be caught.
3306 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3307 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3308 ("postsig action: blocked sig %d", sig));
3311 * Set the new mask value and also defer further
3312 * occurrences of this signal.
3314 * Special case: user has done a sigsuspend. Here the
3315 * current mask is not of interest, but rather the
3316 * mask from before the sigsuspend is what we want
3317 * restored after the signal processing is completed.
3319 if (td->td_pflags & TDP_OLDMASK) {
3320 returnmask = td->td_oldsigmask;
3321 td->td_pflags &= ~TDP_OLDMASK;
3323 returnmask = td->td_sigmask;
3325 if (p->p_sig == sig) {
3328 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3329 postsig_done(sig, td, ps);
3335 sig_ast_checksusp(struct thread *td)
3337 struct proc *p __diagused;
3341 PROC_LOCK_ASSERT(p, MA_OWNED);
3343 if (!td_ast_pending(td, TDA_SUSPEND))
3346 ret = thread_suspend_check(1);
3347 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3352 sig_ast_needsigchk(struct thread *td)
3359 PROC_LOCK_ASSERT(p, MA_OWNED);
3361 if (!td_ast_pending(td, TDA_SIG))
3365 mtx_lock(&ps->ps_mtx);
3368 mtx_unlock(&ps->ps_mtx);
3369 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3370 KASSERT(TD_SBDRY_INTR(td),
3371 ("lost TDF_SERESTART of TDF_SEINTR"));
3372 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3373 (TDF_SEINTR | TDF_SERESTART),
3374 ("both TDF_SEINTR and TDF_SERESTART"));
3375 ret = TD_SBDRY_ERRNO(td);
3376 } else if (sig != 0) {
3377 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3378 mtx_unlock(&ps->ps_mtx);
3380 mtx_unlock(&ps->ps_mtx);
3385 * Do not go into sleep if this thread was the ptrace(2)
3386 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3387 * but we usually act on the signal by interrupting sleep, and
3388 * should do that here as well.
3390 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3393 td->td_dbgflags &= ~TDB_FSTP;
3407 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3413 ret = sig_ast_checksusp(td);
3415 ret = sig_ast_needsigchk(td);
3421 curproc_sigkilled(void)
3429 if (!td_ast_pending(td, TDA_SIG))
3435 mtx_lock(&ps->ps_mtx);
3436 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3437 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3438 mtx_unlock(&ps->ps_mtx);
3444 proc_wkilled(struct proc *p)
3447 PROC_LOCK_ASSERT(p, MA_OWNED);
3448 if ((p->p_flag & P_WKILLED) == 0) {
3449 p->p_flag |= P_WKILLED;
3451 * Notify swapper that there is a process to swap in.
3452 * The notification is racy, at worst it would take 10
3453 * seconds for the swapper process to notice.
3455 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3461 * Kill the current process for stated reason.
3464 killproc(struct proc *p, const char *why)
3467 PROC_LOCK_ASSERT(p, MA_OWNED);
3468 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3470 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3471 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3472 p->p_ucred->cr_uid, why);
3474 kern_psignal(p, SIGKILL);
3478 * Force the current process to exit with the specified signal, dumping core
3479 * if appropriate. We bypass the normal tests for masked and caught signals,
3480 * allowing unrecoverable failures to terminate the process without changing
3481 * signal state. Mark the accounting record with the signal termination.
3482 * If dumping core, save the signal number for the debugger. Calls exit and
3486 sigexit(struct thread *td, int sig)
3488 struct proc *p = td->td_proc;
3490 PROC_LOCK_ASSERT(p, MA_OWNED);
3491 proc_set_p2_wexit(p);
3493 p->p_acflag |= AXSIG;
3495 * We must be single-threading to generate a core dump. This
3496 * ensures that the registers in the core file are up-to-date.
3497 * Also, the ELF dump handler assumes that the thread list doesn't
3498 * change out from under it.
3500 * XXX If another thread attempts to single-thread before us
3501 * (e.g. via fork()), we won't get a dump at all.
3503 if ((sigprop(sig) & SIGPROP_CORE) &&
3504 thread_single(p, SINGLE_NO_EXIT) == 0) {
3507 * Log signals which would cause core dumps
3508 * (Log as LOG_INFO to appease those who don't want
3510 * XXX : Todo, as well as euid, write out ruid too
3511 * Note that coredump() drops proc lock.
3513 if (coredump(td) == 0)
3515 if (kern_logsigexit)
3517 "pid %d (%s), jid %d, uid %d: exited on "
3518 "signal %d%s\n", p->p_pid, p->p_comm,
3519 p->p_ucred->cr_prison->pr_id,
3520 td->td_ucred->cr_uid,
3522 sig & WCOREFLAG ? " (core dumped)" : "");
3530 * Send queued SIGCHLD to parent when child process's state
3534 sigparent(struct proc *p, int reason, int status)
3536 PROC_LOCK_ASSERT(p, MA_OWNED);
3537 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3539 if (p->p_ksi != NULL) {
3540 p->p_ksi->ksi_signo = SIGCHLD;
3541 p->p_ksi->ksi_code = reason;
3542 p->p_ksi->ksi_status = status;
3543 p->p_ksi->ksi_pid = p->p_pid;
3544 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3545 if (KSI_ONQ(p->p_ksi))
3548 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3552 childproc_jobstate(struct proc *p, int reason, int sig)
3556 PROC_LOCK_ASSERT(p, MA_OWNED);
3557 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3560 * Wake up parent sleeping in kern_wait(), also send
3561 * SIGCHLD to parent, but SIGCHLD does not guarantee
3562 * that parent will awake, because parent may masked
3565 p->p_pptr->p_flag |= P_STATCHILD;
3568 ps = p->p_pptr->p_sigacts;
3569 mtx_lock(&ps->ps_mtx);
3570 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3571 mtx_unlock(&ps->ps_mtx);
3572 sigparent(p, reason, sig);
3574 mtx_unlock(&ps->ps_mtx);
3578 childproc_stopped(struct proc *p, int reason)
3581 childproc_jobstate(p, reason, p->p_xsig);
3585 childproc_continued(struct proc *p)
3587 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3591 childproc_exited(struct proc *p)
3595 if (WCOREDUMP(p->p_xsig)) {
3596 reason = CLD_DUMPED;
3597 status = WTERMSIG(p->p_xsig);
3598 } else if (WIFSIGNALED(p->p_xsig)) {
3599 reason = CLD_KILLED;
3600 status = WTERMSIG(p->p_xsig);
3602 reason = CLD_EXITED;
3603 status = p->p_xexit;
3606 * XXX avoid calling wakeup(p->p_pptr), the work is
3609 sigparent(p, reason, status);
3612 #define MAX_NUM_CORE_FILES 100000
3613 #ifndef NUM_CORE_FILES
3614 #define NUM_CORE_FILES 5
3616 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3617 static int num_cores = NUM_CORE_FILES;
3620 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3625 new_val = num_cores;
3626 error = sysctl_handle_int(oidp, &new_val, 0, req);
3627 if (error != 0 || req->newptr == NULL)
3629 if (new_val > MAX_NUM_CORE_FILES)
3630 new_val = MAX_NUM_CORE_FILES;
3633 num_cores = new_val;
3636 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3637 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3638 sysctl_debug_num_cores_check, "I",
3639 "Maximum number of generated process corefiles while using index format");
3641 #define GZIP_SUFFIX ".gz"
3642 #define ZSTD_SUFFIX ".zst"
3644 int compress_user_cores = 0;
3647 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3651 val = compress_user_cores;
3652 error = sysctl_handle_int(oidp, &val, 0, req);
3653 if (error != 0 || req->newptr == NULL)
3655 if (val != 0 && !compressor_avail(val))
3657 compress_user_cores = val;
3660 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3661 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3662 sysctl_compress_user_cores, "I",
3663 "Enable compression of user corefiles ("
3664 __XSTRING(COMPRESS_GZIP) " = gzip, "
3665 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3667 int compress_user_cores_level = 6;
3668 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3669 &compress_user_cores_level, 0,
3670 "Corefile compression level");
3673 * Protect the access to corefilename[] by allproc_lock.
3675 #define corefilename_lock allproc_lock
3677 static char corefilename[MAXPATHLEN] = {"%N.core"};
3678 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3681 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3685 sx_xlock(&corefilename_lock);
3686 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3688 sx_xunlock(&corefilename_lock);
3692 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3693 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3694 "Process corefile name format string");
3697 vnode_close_locked(struct thread *td, struct vnode *vp)
3701 vn_close(vp, FWRITE, td->td_ucred, td);
3705 * If the core format has a %I in it, then we need to check
3706 * for existing corefiles before defining a name.
3707 * To do this we iterate over 0..ncores to find a
3708 * non-existing core file name to use. If all core files are
3709 * already used we choose the oldest one.
3712 corefile_open_last(struct thread *td, char *name, int indexpos,
3713 int indexlen, int ncores, struct vnode **vpp)
3715 struct vnode *oldvp, *nextvp, *vp;
3717 struct nameidata nd;
3718 int error, i, flags, oflags, cmode;
3720 struct timespec lasttime;
3722 nextvp = oldvp = NULL;
3723 cmode = S_IRUSR | S_IWUSR;
3724 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3725 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3727 for (i = 0; i < ncores; i++) {
3728 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3730 ch = name[indexpos + indexlen];
3731 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3733 name[indexpos + indexlen] = ch;
3735 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3736 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3743 if ((flags & O_CREAT) == O_CREAT) {
3748 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3750 vnode_close_locked(td, vp);
3754 if (oldvp == NULL ||
3755 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3756 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3757 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3759 vn_close(oldvp, FWRITE, td->td_ucred, td);
3762 lasttime = vattr.va_mtime;
3764 vnode_close_locked(td, vp);
3768 if (oldvp != NULL) {
3769 if (nextvp == NULL) {
3770 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3772 vn_close(oldvp, FWRITE, td->td_ucred, td);
3775 error = vn_lock(nextvp, LK_EXCLUSIVE);
3777 vn_close(nextvp, FWRITE, td->td_ucred,
3783 vn_close(oldvp, FWRITE, td->td_ucred, td);
3788 vnode_close_locked(td, oldvp);
3797 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3798 * Expand the name described in corefilename, using name, uid, and pid
3799 * and open/create core file.
3800 * corefilename is a printf-like string, with three format specifiers:
3801 * %N name of process ("name")
3802 * %P process id (pid)
3804 * For example, "%N.core" is the default; they can be disabled completely
3805 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3806 * This is controlled by the sysctl variable kern.corefile (see above).
3809 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3810 int compress, int signum, struct vnode **vpp, char **namep)
3813 struct nameidata nd;
3815 char *hostname, *name;
3816 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3819 format = corefilename;
3820 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3824 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3825 sx_slock(&corefilename_lock);
3826 for (i = 0; format[i] != '\0'; i++) {
3827 switch (format[i]) {
3828 case '%': /* Format character */
3830 switch (format[i]) {
3832 sbuf_putc(&sb, '%');
3834 case 'H': /* hostname */
3835 if (hostname == NULL) {
3836 hostname = malloc(MAXHOSTNAMELEN,
3839 getcredhostname(td->td_ucred, hostname,
3841 sbuf_printf(&sb, "%s", hostname);
3843 case 'I': /* autoincrementing index */
3844 if (indexpos != -1) {
3845 sbuf_printf(&sb, "%%I");
3849 indexpos = sbuf_len(&sb);
3850 sbuf_printf(&sb, "%u", ncores - 1);
3851 indexlen = sbuf_len(&sb) - indexpos;
3853 case 'N': /* process name */
3854 sbuf_printf(&sb, "%s", comm);
3856 case 'P': /* process id */
3857 sbuf_printf(&sb, "%u", pid);
3859 case 'S': /* signal number */
3860 sbuf_printf(&sb, "%i", signum);
3862 case 'U': /* user id */
3863 sbuf_printf(&sb, "%u", uid);
3867 "Unknown format character %c in "
3868 "corename `%s'\n", format[i], format);
3873 sbuf_putc(&sb, format[i]);
3877 sx_sunlock(&corefilename_lock);
3878 free(hostname, M_TEMP);
3879 if (compress == COMPRESS_GZIP)
3880 sbuf_printf(&sb, GZIP_SUFFIX);
3881 else if (compress == COMPRESS_ZSTD)
3882 sbuf_printf(&sb, ZSTD_SUFFIX);
3883 if (sbuf_error(&sb) != 0) {
3884 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3885 "long\n", (long)pid, comm, (u_long)uid);
3893 if (indexpos != -1) {
3894 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3898 "pid %d (%s), uid (%u): Path `%s' failed "
3899 "on initial open test, error = %d\n",
3900 pid, comm, uid, name, error);
3903 cmode = S_IRUSR | S_IWUSR;
3904 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3905 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3906 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3907 if ((td->td_proc->p_flag & P_SUGID) != 0)
3910 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3911 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3921 audit_proc_coredump(td, name, error);
3931 * Dump a process' core. The main routine does some
3932 * policy checking, and creates the name of the coredump;
3933 * then it passes on a vnode and a size limit to the process-specific
3934 * coredump routine if there is one; if there _is not_ one, it returns
3935 * ENOSYS; otherwise it returns the error from the process-specific routine.
3939 coredump(struct thread *td)
3941 struct proc *p = td->td_proc;
3942 struct ucred *cred = td->td_ucred;
3946 size_t fullpathsize;
3947 int error, error1, locked;
3948 char *name; /* name of corefile */
3951 char *fullpath, *freepath = NULL;
3954 PROC_LOCK_ASSERT(p, MA_OWNED);
3955 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3957 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3958 (p->p_flag2 & P2_NOTRACE) != 0) {
3964 * Note that the bulk of limit checking is done after
3965 * the corefile is created. The exception is if the limit
3966 * for corefiles is 0, in which case we don't bother
3967 * creating the corefile at all. This layout means that
3968 * a corefile is truncated instead of not being created,
3969 * if it is larger than the limit.
3971 limit = (off_t)lim_cur(td, RLIMIT_CORE);
3972 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3978 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
3979 compress_user_cores, p->p_sig, &vp, &name);
3984 * Don't dump to non-regular files or files with links.
3985 * Do not dump into system files. Effective user must own the corefile.
3987 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3988 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
3989 vattr.va_uid != cred->cr_uid) {
3997 /* Postpone other writers, including core dumps of other processes. */
3998 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4000 lf.l_whence = SEEK_SET;
4003 lf.l_type = F_WRLCK;
4004 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
4008 if (set_core_nodump_flag)
4009 vattr.va_flags = UF_NODUMP;
4010 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4011 VOP_SETATTR(vp, &vattr, cred);
4014 p->p_acflag |= ACORE;
4017 if (p->p_sysent->sv_coredump != NULL) {
4018 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
4024 lf.l_type = F_UNLCK;
4025 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4027 vn_rangelock_unlock(vp, rl_cookie);
4030 * Notify the userland helper that a process triggered a core dump.
4031 * This allows the helper to run an automated debugging session.
4033 if (error != 0 || coredump_devctl == 0)
4035 sb = sbuf_new_auto();
4036 if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4038 sbuf_printf(sb, "comm=\"");
4039 devctl_safe_quote_sb(sb, fullpath);
4040 free(freepath, M_TEMP);
4041 sbuf_printf(sb, "\" core=\"");
4044 * We can't lookup core file vp directly. When we're replacing a core, and
4045 * other random times, we flush the name cache, so it will fail. Instead,
4046 * if the path of the core is relative, add the current dir in front if it.
4048 if (name[0] != '/') {
4049 fullpathsize = MAXPATHLEN;
4050 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4051 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
4052 free(freepath, M_TEMP);
4055 devctl_safe_quote_sb(sb, fullpath);
4056 free(freepath, M_TEMP);
4059 devctl_safe_quote_sb(sb, name);
4060 sbuf_printf(sb, "\"");
4061 if (sbuf_finish(sb) == 0)
4062 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4066 error1 = vn_close(vp, FWRITE, cred, td);
4070 audit_proc_coredump(td, name, error);
4077 * Nonexistent system call-- signal process (may want to handle it). Flag
4078 * error in case process won't see signal immediately (blocked or ignored).
4080 #ifndef _SYS_SYSPROTO_H_
4087 nosys(struct thread *td, struct nosys_args *args)
4094 tdsignal(td, SIGSYS);
4096 if (kern_lognosys == 1 || kern_lognosys == 3) {
4097 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4100 if (kern_lognosys == 2 || kern_lognosys == 3 ||
4101 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4102 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4109 * Send a SIGIO or SIGURG signal to a process or process group using stored
4110 * credentials rather than those of the current process.
4113 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4116 struct sigio *sigio;
4118 ksiginfo_init(&ksi);
4119 ksi.ksi_signo = sig;
4120 ksi.ksi_code = SI_KERNEL;
4124 if (sigio == NULL) {
4128 if (sigio->sio_pgid > 0) {
4129 PROC_LOCK(sigio->sio_proc);
4130 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4131 kern_psignal(sigio->sio_proc, sig);
4132 PROC_UNLOCK(sigio->sio_proc);
4133 } else if (sigio->sio_pgid < 0) {
4136 PGRP_LOCK(sigio->sio_pgrp);
4137 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4139 if (p->p_state == PRS_NORMAL &&
4140 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4141 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4142 kern_psignal(p, sig);
4145 PGRP_UNLOCK(sigio->sio_pgrp);
4151 filt_sigattach(struct knote *kn)
4153 struct proc *p = curproc;
4155 kn->kn_ptr.p_proc = p;
4156 kn->kn_flags |= EV_CLEAR; /* automatically set */
4158 knlist_add(p->p_klist, kn, 0);
4164 filt_sigdetach(struct knote *kn)
4166 struct proc *p = kn->kn_ptr.p_proc;
4168 knlist_remove(p->p_klist, kn, 0);
4172 * signal knotes are shared with proc knotes, so we apply a mask to
4173 * the hint in order to differentiate them from process hints. This
4174 * could be avoided by using a signal-specific knote list, but probably
4175 * isn't worth the trouble.
4178 filt_signal(struct knote *kn, long hint)
4181 if (hint & NOTE_SIGNAL) {
4182 hint &= ~NOTE_SIGNAL;
4184 if (kn->kn_id == hint)
4187 return (kn->kn_data != 0);
4195 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4196 refcount_init(&ps->ps_refcnt, 1);
4197 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4202 sigacts_free(struct sigacts *ps)
4205 if (refcount_release(&ps->ps_refcnt) == 0)
4207 mtx_destroy(&ps->ps_mtx);
4208 free(ps, M_SUBPROC);
4212 sigacts_hold(struct sigacts *ps)
4215 refcount_acquire(&ps->ps_refcnt);
4220 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4223 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4224 mtx_lock(&src->ps_mtx);
4225 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4226 mtx_unlock(&src->ps_mtx);
4230 sigacts_shared(struct sigacts *ps)
4233 return (ps->ps_refcnt > 1);
4237 sig_drop_caught(struct proc *p)
4243 PROC_LOCK_ASSERT(p, MA_OWNED);
4244 mtx_assert(&ps->ps_mtx, MA_OWNED);
4245 SIG_FOREACH(sig, &ps->ps_sigcatch) {
4247 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4248 sigqueue_delete_proc(p, sig);
4253 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4258 * Prevent further fetches and SIGSEGVs, allowing thread to
4259 * issue syscalls despite corruption.
4261 sigfastblock_clear(td);
4265 ksiginfo_init_trap(&ksi);
4266 ksi.ksi_signo = SIGSEGV;
4267 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4268 ksi.ksi_addr = td->td_sigblock_ptr;
4269 trapsignal(td, &ksi);
4273 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4277 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4279 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4280 sigfastblock_failed(td, sendsig, false);
4284 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4289 sigfastblock_resched(struct thread *td, bool resched)
4296 reschedule_signals(p, td->td_sigmask, 0);
4299 ast_sched(td, TDA_SIG);
4303 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4312 case SIGFASTBLOCK_SETPTR:
4313 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4317 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4321 td->td_pflags |= TDP_SIGFASTBLOCK;
4322 td->td_sigblock_ptr = uap->ptr;
4325 case SIGFASTBLOCK_UNBLOCK:
4326 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4332 res = casueword32(td->td_sigblock_ptr,
4333 SIGFASTBLOCK_PEND, &oldval, 0);
4336 sigfastblock_failed(td, false, true);
4342 if (oldval != SIGFASTBLOCK_PEND) {
4346 error = thread_check_susp(td, false);
4354 * td_sigblock_val is cleared there, but not on a
4355 * syscall exit. The end effect is that a single
4356 * interruptible sleep, while user sigblock word is
4357 * set, might return EINTR or ERESTART to usermode
4358 * without delivering signal. All further sleeps,
4359 * until userspace clears the word and does
4360 * sigfastblock(UNBLOCK), observe current word and no
4361 * longer get interrupted. It is slight
4362 * non-conformance, with alternative to have read the
4363 * sigblock word on each syscall entry.
4365 td->td_sigblock_val = 0;
4368 * Rely on normal ast mechanism to deliver pending
4369 * signals to current thread. But notify others about
4372 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4376 case SIGFASTBLOCK_UNSETPTR:
4377 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4381 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4385 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4389 sigfastblock_clear(td);
4400 sigfastblock_clear(struct thread *td)
4404 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4406 td->td_sigblock_val = 0;
4407 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4409 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4410 sigfastblock_resched(td, resched);
4414 sigfastblock_fetch(struct thread *td)
4418 (void)sigfastblock_fetch_sig(td, true, &val);
4422 sigfastblock_setpend1(struct thread *td)
4427 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4429 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4431 sigfastblock_failed(td, true, false);
4435 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4436 oldval | SIGFASTBLOCK_PEND);
4438 sigfastblock_failed(td, true, true);
4442 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4443 td->td_pflags &= ~TDP_SIGFASTPENDING;
4447 if (thread_check_susp(td, false) != 0)
4453 sigfastblock_setpend(struct thread *td, bool resched)
4457 sigfastblock_setpend1(td);
4461 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);