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 *, int);
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);
120 static uma_zone_t ksiginfo_zone = NULL;
121 struct filterops sig_filtops = {
123 .f_attach = filt_sigattach,
124 .f_detach = filt_sigdetach,
125 .f_event = filt_signal,
128 static int kern_logsigexit = 1;
129 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
131 "Log processes quitting on abnormal signals to syslog(3)");
133 static int kern_forcesigexit = 1;
134 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
135 &kern_forcesigexit, 0, "Force trap signal to be handled");
137 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
138 "POSIX real time signal");
140 static int max_pending_per_proc = 128;
141 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
142 &max_pending_per_proc, 0, "Max pending signals per proc");
144 static int preallocate_siginfo = 1024;
145 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
146 &preallocate_siginfo, 0, "Preallocated signal memory size");
148 static int signal_overflow = 0;
149 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
150 &signal_overflow, 0, "Number of signals overflew");
152 static int signal_alloc_fail = 0;
153 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
154 &signal_alloc_fail, 0, "signals failed to be allocated");
156 static int kern_lognosys = 0;
157 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
158 "Log invalid syscalls");
160 __read_frequently bool sigfastblock_fetch_always = false;
161 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
162 &sigfastblock_fetch_always, 0,
163 "Fetch sigfastblock word on each syscall entry for proper "
164 "blocking semantic");
166 static bool kern_sig_discard_ign = true;
167 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
168 &kern_sig_discard_ign, 0,
169 "Discard ignored signals on delivery, otherwise queue them to "
172 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
175 * Policy -- Can ucred cr1 send SIGIO to process cr2?
176 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
177 * in the right situations.
179 #define CANSIGIO(cr1, cr2) \
180 ((cr1)->cr_uid == 0 || \
181 (cr1)->cr_ruid == (cr2)->cr_ruid || \
182 (cr1)->cr_uid == (cr2)->cr_ruid || \
183 (cr1)->cr_ruid == (cr2)->cr_uid || \
184 (cr1)->cr_uid == (cr2)->cr_uid)
186 static int sugid_coredump;
187 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
188 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
190 static int capmode_coredump;
191 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
192 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
194 static int do_coredump = 1;
195 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
196 &do_coredump, 0, "Enable/Disable coredumps");
198 static int set_core_nodump_flag = 0;
199 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
200 0, "Enable setting the NODUMP flag on coredump files");
202 static int coredump_devctl = 0;
203 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
204 0, "Generate a devctl notification when processes coredump");
207 * Signal properties and actions.
208 * The array below categorizes the signals and their default actions
209 * according to the following properties:
211 #define SIGPROP_KILL 0x01 /* terminates process by default */
212 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
213 #define SIGPROP_STOP 0x04 /* suspend process */
214 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
215 #define SIGPROP_IGNORE 0x10 /* ignore by default */
216 #define SIGPROP_CONT 0x20 /* continue if suspended */
218 static int sigproptbl[NSIG] = {
219 [SIGHUP] = SIGPROP_KILL,
220 [SIGINT] = SIGPROP_KILL,
221 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
222 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
223 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
224 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
225 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
226 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
227 [SIGKILL] = SIGPROP_KILL,
228 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
229 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
230 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
231 [SIGPIPE] = SIGPROP_KILL,
232 [SIGALRM] = SIGPROP_KILL,
233 [SIGTERM] = SIGPROP_KILL,
234 [SIGURG] = SIGPROP_IGNORE,
235 [SIGSTOP] = SIGPROP_STOP,
236 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
237 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
238 [SIGCHLD] = SIGPROP_IGNORE,
239 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
240 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
241 [SIGIO] = SIGPROP_IGNORE,
242 [SIGXCPU] = SIGPROP_KILL,
243 [SIGXFSZ] = SIGPROP_KILL,
244 [SIGVTALRM] = SIGPROP_KILL,
245 [SIGPROF] = SIGPROP_KILL,
246 [SIGWINCH] = SIGPROP_IGNORE,
247 [SIGINFO] = SIGPROP_IGNORE,
248 [SIGUSR1] = SIGPROP_KILL,
249 [SIGUSR2] = SIGPROP_KILL,
252 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
256 int __sig = ffs(__bits); \
257 __bits &= ~(1u << (__sig - 1)); \
258 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
262 if (++__i == _SIG_WORDS) { \
266 __bits = (set)->__bits[__i]; \
271 #define SIG_FOREACH(i, set) \
272 for (int32_t __i = -1, __bits = 0; \
273 _SIG_FOREACH_ADVANCE(i, set); ) \
275 sigset_t fastblock_mask;
280 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
281 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
282 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
283 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
284 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
285 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
286 SIGFILLSET(fastblock_mask);
287 SIG_CANTMASK(fastblock_mask);
291 ksiginfo_alloc(int wait)
295 flags = M_ZERO | (wait ? M_WAITOK : M_NOWAIT);
296 if (ksiginfo_zone != NULL)
297 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
302 ksiginfo_free(ksiginfo_t *ksi)
304 uma_zfree(ksiginfo_zone, ksi);
308 ksiginfo_tryfree(ksiginfo_t *ksi)
310 if (!(ksi->ksi_flags & KSI_EXT)) {
311 uma_zfree(ksiginfo_zone, ksi);
318 sigqueue_init(sigqueue_t *list, struct proc *p)
320 SIGEMPTYSET(list->sq_signals);
321 SIGEMPTYSET(list->sq_kill);
322 SIGEMPTYSET(list->sq_ptrace);
323 TAILQ_INIT(&list->sq_list);
325 list->sq_flags = SQ_INIT;
329 * Get a signal's ksiginfo.
331 * 0 - signal not found
332 * others - signal number
335 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
337 struct proc *p = sq->sq_proc;
338 struct ksiginfo *ksi, *next;
341 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
343 if (!SIGISMEMBER(sq->sq_signals, signo))
346 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
348 SIGDELSET(sq->sq_ptrace, signo);
349 si->ksi_flags |= KSI_PTRACE;
351 if (SIGISMEMBER(sq->sq_kill, signo)) {
354 SIGDELSET(sq->sq_kill, signo);
357 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
358 if (ksi->ksi_signo == signo) {
360 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
361 ksi->ksi_sigq = NULL;
362 ksiginfo_copy(ksi, si);
363 if (ksiginfo_tryfree(ksi) && p != NULL)
372 SIGDELSET(sq->sq_signals, signo);
373 si->ksi_signo = signo;
378 sigqueue_take(ksiginfo_t *ksi)
384 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
388 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
389 ksi->ksi_sigq = NULL;
390 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
393 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
394 kp = TAILQ_NEXT(kp, ksi_link)) {
395 if (kp->ksi_signo == ksi->ksi_signo)
398 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
399 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
400 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
404 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
406 struct proc *p = sq->sq_proc;
407 struct ksiginfo *ksi;
410 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
413 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
416 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
417 SIGADDSET(sq->sq_kill, signo);
421 /* directly insert the ksi, don't copy it */
422 if (si->ksi_flags & KSI_INS) {
423 if (si->ksi_flags & KSI_HEAD)
424 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
426 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
431 if (__predict_false(ksiginfo_zone == NULL)) {
432 SIGADDSET(sq->sq_kill, signo);
436 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
439 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
445 ksiginfo_copy(si, ksi);
446 ksi->ksi_signo = signo;
447 if (si->ksi_flags & KSI_HEAD)
448 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
450 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
455 if ((si->ksi_flags & KSI_PTRACE) != 0) {
456 SIGADDSET(sq->sq_ptrace, signo);
459 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
460 (si->ksi_flags & KSI_SIGQ) == 0) {
461 SIGADDSET(sq->sq_kill, signo);
469 SIGADDSET(sq->sq_signals, signo);
474 sigqueue_flush(sigqueue_t *sq)
476 struct proc *p = sq->sq_proc;
479 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
482 PROC_LOCK_ASSERT(p, MA_OWNED);
484 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
485 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
486 ksi->ksi_sigq = NULL;
487 if (ksiginfo_tryfree(ksi) && p != NULL)
491 SIGEMPTYSET(sq->sq_signals);
492 SIGEMPTYSET(sq->sq_kill);
493 SIGEMPTYSET(sq->sq_ptrace);
497 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
500 struct proc *p1, *p2;
501 ksiginfo_t *ksi, *next;
503 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
504 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
507 /* Move siginfo to target list */
508 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
509 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
510 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
513 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
520 /* Move pending bits to target list */
522 SIGSETAND(tmp, *set);
523 SIGSETOR(dst->sq_kill, tmp);
524 SIGSETNAND(src->sq_kill, tmp);
526 tmp = src->sq_ptrace;
527 SIGSETAND(tmp, *set);
528 SIGSETOR(dst->sq_ptrace, tmp);
529 SIGSETNAND(src->sq_ptrace, tmp);
531 tmp = src->sq_signals;
532 SIGSETAND(tmp, *set);
533 SIGSETOR(dst->sq_signals, tmp);
534 SIGSETNAND(src->sq_signals, tmp);
539 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
544 SIGADDSET(set, signo);
545 sigqueue_move_set(src, dst, &set);
550 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
552 struct proc *p = sq->sq_proc;
553 ksiginfo_t *ksi, *next;
555 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
557 /* Remove siginfo queue */
558 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
559 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
560 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
561 ksi->ksi_sigq = NULL;
562 if (ksiginfo_tryfree(ksi) && p != NULL)
566 SIGSETNAND(sq->sq_kill, *set);
567 SIGSETNAND(sq->sq_ptrace, *set);
568 SIGSETNAND(sq->sq_signals, *set);
572 sigqueue_delete(sigqueue_t *sq, int signo)
577 SIGADDSET(set, signo);
578 sigqueue_delete_set(sq, &set);
581 /* Remove a set of signals for a process */
583 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
588 PROC_LOCK_ASSERT(p, MA_OWNED);
590 sigqueue_init(&worklist, NULL);
591 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
593 FOREACH_THREAD_IN_PROC(p, td0)
594 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
596 sigqueue_flush(&worklist);
600 sigqueue_delete_proc(struct proc *p, int signo)
605 SIGADDSET(set, signo);
606 sigqueue_delete_set_proc(p, &set);
610 sigqueue_delete_stopmask_proc(struct proc *p)
615 SIGADDSET(set, SIGSTOP);
616 SIGADDSET(set, SIGTSTP);
617 SIGADDSET(set, SIGTTIN);
618 SIGADDSET(set, SIGTTOU);
619 sigqueue_delete_set_proc(p, &set);
623 * Determine signal that should be delivered to thread td, the current
624 * thread, 0 if none. If there is a pending stop signal with default
625 * action, the process stops in issignal().
628 cursig(struct thread *td)
630 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
631 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
632 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
633 return (SIGPENDING(td) ? issignal(td) : 0);
637 * Arrange for ast() to handle unmasked pending signals on return to user
638 * mode. This must be called whenever a signal is added to td_sigqueue or
639 * unmasked in td_sigmask.
642 signotify(struct thread *td)
645 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
647 if (SIGPENDING(td)) {
649 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
655 * Returns 1 (true) if altstack is configured for the thread, and the
656 * passed stack bottom address falls into the altstack range. Handles
657 * the 43 compat special case where the alt stack size is zero.
660 sigonstack(size_t sp)
665 if ((td->td_pflags & TDP_ALTSTACK) == 0)
667 #if defined(COMPAT_43)
668 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
669 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
671 return (sp >= (size_t)td->td_sigstk.ss_sp &&
672 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
679 if (sig > 0 && sig < nitems(sigproptbl))
680 return (sigproptbl[sig]);
685 sigact_flag_test(const struct sigaction *act, int flag)
689 * SA_SIGINFO is reset when signal disposition is set to
690 * ignore or default. Other flags are kept according to user
693 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
694 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
695 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
705 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
706 struct sigaction *oact, int flags)
709 struct proc *p = td->td_proc;
711 if (!_SIG_VALID(sig))
713 if (act != NULL && act->sa_handler != SIG_DFL &&
714 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
715 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
716 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
721 mtx_lock(&ps->ps_mtx);
723 memset(oact, 0, sizeof(*oact));
724 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
725 if (SIGISMEMBER(ps->ps_sigonstack, sig))
726 oact->sa_flags |= SA_ONSTACK;
727 if (!SIGISMEMBER(ps->ps_sigintr, sig))
728 oact->sa_flags |= SA_RESTART;
729 if (SIGISMEMBER(ps->ps_sigreset, sig))
730 oact->sa_flags |= SA_RESETHAND;
731 if (SIGISMEMBER(ps->ps_signodefer, sig))
732 oact->sa_flags |= SA_NODEFER;
733 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
734 oact->sa_flags |= SA_SIGINFO;
736 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
738 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
739 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
740 oact->sa_flags |= SA_NOCLDSTOP;
741 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
742 oact->sa_flags |= SA_NOCLDWAIT;
745 if ((sig == SIGKILL || sig == SIGSTOP) &&
746 act->sa_handler != SIG_DFL) {
747 mtx_unlock(&ps->ps_mtx);
753 * Change setting atomically.
756 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
757 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
758 if (sigact_flag_test(act, SA_SIGINFO)) {
759 ps->ps_sigact[_SIG_IDX(sig)] =
760 (__sighandler_t *)act->sa_sigaction;
761 SIGADDSET(ps->ps_siginfo, sig);
763 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
764 SIGDELSET(ps->ps_siginfo, sig);
766 if (!sigact_flag_test(act, SA_RESTART))
767 SIGADDSET(ps->ps_sigintr, sig);
769 SIGDELSET(ps->ps_sigintr, sig);
770 if (sigact_flag_test(act, SA_ONSTACK))
771 SIGADDSET(ps->ps_sigonstack, sig);
773 SIGDELSET(ps->ps_sigonstack, sig);
774 if (sigact_flag_test(act, SA_RESETHAND))
775 SIGADDSET(ps->ps_sigreset, sig);
777 SIGDELSET(ps->ps_sigreset, sig);
778 if (sigact_flag_test(act, SA_NODEFER))
779 SIGADDSET(ps->ps_signodefer, sig);
781 SIGDELSET(ps->ps_signodefer, sig);
782 if (sig == SIGCHLD) {
783 if (act->sa_flags & SA_NOCLDSTOP)
784 ps->ps_flag |= PS_NOCLDSTOP;
786 ps->ps_flag &= ~PS_NOCLDSTOP;
787 if (act->sa_flags & SA_NOCLDWAIT) {
789 * Paranoia: since SA_NOCLDWAIT is implemented
790 * by reparenting the dying child to PID 1 (and
791 * trust it to reap the zombie), PID 1 itself
792 * is forbidden to set SA_NOCLDWAIT.
795 ps->ps_flag &= ~PS_NOCLDWAIT;
797 ps->ps_flag |= PS_NOCLDWAIT;
799 ps->ps_flag &= ~PS_NOCLDWAIT;
800 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
801 ps->ps_flag |= PS_CLDSIGIGN;
803 ps->ps_flag &= ~PS_CLDSIGIGN;
806 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
807 * and for signals set to SIG_DFL where the default is to
808 * ignore. However, don't put SIGCONT in ps_sigignore, as we
809 * have to restart the process.
811 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
812 (sigprop(sig) & SIGPROP_IGNORE &&
813 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
814 /* never to be seen again */
815 sigqueue_delete_proc(p, sig);
817 /* easier in psignal */
818 SIGADDSET(ps->ps_sigignore, sig);
819 SIGDELSET(ps->ps_sigcatch, sig);
821 SIGDELSET(ps->ps_sigignore, sig);
822 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
823 SIGDELSET(ps->ps_sigcatch, sig);
825 SIGADDSET(ps->ps_sigcatch, sig);
827 #ifdef COMPAT_FREEBSD4
828 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
829 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
830 (flags & KSA_FREEBSD4) == 0)
831 SIGDELSET(ps->ps_freebsd4, sig);
833 SIGADDSET(ps->ps_freebsd4, sig);
836 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
837 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
838 (flags & KSA_OSIGSET) == 0)
839 SIGDELSET(ps->ps_osigset, sig);
841 SIGADDSET(ps->ps_osigset, sig);
844 mtx_unlock(&ps->ps_mtx);
849 #ifndef _SYS_SYSPROTO_H_
850 struct sigaction_args {
852 struct sigaction *act;
853 struct sigaction *oact;
857 sys_sigaction(struct thread *td, struct sigaction_args *uap)
859 struct sigaction act, oact;
860 struct sigaction *actp, *oactp;
863 actp = (uap->act != NULL) ? &act : NULL;
864 oactp = (uap->oact != NULL) ? &oact : NULL;
866 error = copyin(uap->act, actp, sizeof(act));
870 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
872 error = copyout(oactp, uap->oact, sizeof(oact));
876 #ifdef COMPAT_FREEBSD4
877 #ifndef _SYS_SYSPROTO_H_
878 struct freebsd4_sigaction_args {
880 struct sigaction *act;
881 struct sigaction *oact;
885 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
887 struct sigaction act, oact;
888 struct sigaction *actp, *oactp;
891 actp = (uap->act != NULL) ? &act : NULL;
892 oactp = (uap->oact != NULL) ? &oact : NULL;
894 error = copyin(uap->act, actp, sizeof(act));
898 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
900 error = copyout(oactp, uap->oact, sizeof(oact));
903 #endif /* COMAPT_FREEBSD4 */
905 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
906 #ifndef _SYS_SYSPROTO_H_
907 struct osigaction_args {
909 struct osigaction *nsa;
910 struct osigaction *osa;
914 osigaction(struct thread *td, struct osigaction_args *uap)
916 struct osigaction sa;
917 struct sigaction nsa, osa;
918 struct sigaction *nsap, *osap;
921 if (uap->signum <= 0 || uap->signum >= ONSIG)
924 nsap = (uap->nsa != NULL) ? &nsa : NULL;
925 osap = (uap->osa != NULL) ? &osa : NULL;
928 error = copyin(uap->nsa, &sa, sizeof(sa));
931 nsap->sa_handler = sa.sa_handler;
932 nsap->sa_flags = sa.sa_flags;
933 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
935 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
936 if (osap && !error) {
937 sa.sa_handler = osap->sa_handler;
938 sa.sa_flags = osap->sa_flags;
939 SIG2OSIG(osap->sa_mask, sa.sa_mask);
940 error = copyout(&sa, uap->osa, sizeof(sa));
945 #if !defined(__i386__)
946 /* Avoid replicating the same stub everywhere */
948 osigreturn(struct thread *td, struct osigreturn_args *uap)
951 return (nosys(td, (struct nosys_args *)uap));
954 #endif /* COMPAT_43 */
957 * Initialize signal state for process 0;
958 * set to ignore signals that are ignored by default.
961 siginit(struct proc *p)
968 mtx_lock(&ps->ps_mtx);
969 for (i = 1; i <= NSIG; i++) {
970 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
971 SIGADDSET(ps->ps_sigignore, i);
974 mtx_unlock(&ps->ps_mtx);
979 * Reset specified signal to the default disposition.
982 sigdflt(struct sigacts *ps, int sig)
985 mtx_assert(&ps->ps_mtx, MA_OWNED);
986 SIGDELSET(ps->ps_sigcatch, sig);
987 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
988 SIGADDSET(ps->ps_sigignore, sig);
989 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
990 SIGDELSET(ps->ps_siginfo, sig);
994 * Reset signals for an exec of the specified process.
997 execsigs(struct proc *p)
1003 * Reset caught signals. Held signals remain held
1004 * through td_sigmask (unless they were caught,
1005 * and are now ignored by default).
1007 PROC_LOCK_ASSERT(p, MA_OWNED);
1009 mtx_lock(&ps->ps_mtx);
1013 * Reset stack state to the user stack.
1014 * Clear set of signals caught on the signal stack.
1017 MPASS(td->td_proc == p);
1018 td->td_sigstk.ss_flags = SS_DISABLE;
1019 td->td_sigstk.ss_size = 0;
1020 td->td_sigstk.ss_sp = 0;
1021 td->td_pflags &= ~TDP_ALTSTACK;
1023 * Reset no zombies if child dies flag as Solaris does.
1025 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1026 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1027 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1028 mtx_unlock(&ps->ps_mtx);
1032 * kern_sigprocmask()
1034 * Manipulate signal mask.
1037 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1040 sigset_t new_block, oset1;
1045 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1046 PROC_LOCK_ASSERT(p, MA_OWNED);
1049 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1050 ? MA_OWNED : MA_NOTOWNED);
1052 *oset = td->td_sigmask;
1059 oset1 = td->td_sigmask;
1060 SIGSETOR(td->td_sigmask, *set);
1061 new_block = td->td_sigmask;
1062 SIGSETNAND(new_block, oset1);
1065 SIGSETNAND(td->td_sigmask, *set);
1070 oset1 = td->td_sigmask;
1071 if (flags & SIGPROCMASK_OLD)
1072 SIGSETLO(td->td_sigmask, *set);
1074 td->td_sigmask = *set;
1075 new_block = td->td_sigmask;
1076 SIGSETNAND(new_block, oset1);
1085 * The new_block set contains signals that were not previously
1086 * blocked, but are blocked now.
1088 * In case we block any signal that was not previously blocked
1089 * for td, and process has the signal pending, try to schedule
1090 * signal delivery to some thread that does not block the
1091 * signal, possibly waking it up.
1093 if (p->p_numthreads != 1)
1094 reschedule_signals(p, new_block, flags);
1098 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1103 #ifndef _SYS_SYSPROTO_H_
1104 struct sigprocmask_args {
1106 const sigset_t *set;
1111 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1114 sigset_t *setp, *osetp;
1117 setp = (uap->set != NULL) ? &set : NULL;
1118 osetp = (uap->oset != NULL) ? &oset : NULL;
1120 error = copyin(uap->set, setp, sizeof(set));
1124 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1125 if (osetp && !error) {
1126 error = copyout(osetp, uap->oset, sizeof(oset));
1131 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1132 #ifndef _SYS_SYSPROTO_H_
1133 struct osigprocmask_args {
1139 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1144 OSIG2SIG(uap->mask, set);
1145 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1146 SIG2OSIG(oset, td->td_retval[0]);
1149 #endif /* COMPAT_43 */
1152 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1158 error = copyin(uap->set, &set, sizeof(set));
1160 td->td_retval[0] = error;
1164 error = kern_sigtimedwait(td, set, &ksi, NULL);
1167 * sigwait() function shall not return EINTR, but
1168 * the syscall does. Non-ancient libc provides the
1169 * wrapper which hides EINTR. Otherwise, EINTR return
1170 * is used by libthr to handle required cancellation
1171 * point in the sigwait().
1173 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1175 td->td_retval[0] = error;
1179 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1180 td->td_retval[0] = error;
1185 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1188 struct timespec *timeout;
1194 error = copyin(uap->timeout, &ts, sizeof(ts));
1202 error = copyin(uap->set, &set, sizeof(set));
1206 error = kern_sigtimedwait(td, set, &ksi, timeout);
1211 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1214 td->td_retval[0] = ksi.ksi_signo;
1219 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1225 error = copyin(uap->set, &set, sizeof(set));
1229 error = kern_sigtimedwait(td, set, &ksi, NULL);
1234 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1237 td->td_retval[0] = ksi.ksi_signo;
1242 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1246 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1250 thr->td_si.si_signo = 0;
1255 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1256 struct timespec *timeout)
1259 sigset_t saved_mask, new_block;
1261 int error, sig, timo, timevalid = 0;
1262 struct timespec rts, ets, ts;
1272 /* Ensure the sigfastblock value is up to date. */
1273 sigfastblock_fetch(td);
1275 if (timeout != NULL) {
1276 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1278 getnanouptime(&rts);
1279 timespecadd(&rts, timeout, &ets);
1283 /* Some signals can not be waited for. */
1284 SIG_CANTMASK(waitset);
1287 saved_mask = td->td_sigmask;
1288 SIGSETNAND(td->td_sigmask, waitset);
1289 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1290 !kern_sig_discard_ign) {
1292 td->td_flags |= TDF_SIGWAIT;
1296 mtx_lock(&ps->ps_mtx);
1298 mtx_unlock(&ps->ps_mtx);
1299 KASSERT(sig >= 0, ("sig %d", sig));
1300 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1301 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1302 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1312 * POSIX says this must be checked after looking for pending
1315 if (timeout != NULL) {
1320 getnanouptime(&rts);
1321 if (timespeccmp(&rts, &ets, >=)) {
1325 timespecsub(&ets, &rts, &ts);
1326 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1337 error = msleep(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1340 /* The syscalls can not be restarted. */
1341 if (error == ERESTART)
1344 /* We will calculate timeout by ourself. */
1345 if (timeout != NULL && error == EAGAIN)
1349 * If PTRACE_SCE or PTRACE_SCX were set after
1350 * userspace entered the syscall, return spurious
1351 * EINTR after wait was done. Only do this as last
1352 * resort after rechecking for possible queued signals
1353 * and expired timeouts.
1355 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1359 td->td_flags &= ~TDF_SIGWAIT;
1362 new_block = saved_mask;
1363 SIGSETNAND(new_block, td->td_sigmask);
1364 td->td_sigmask = saved_mask;
1366 * Fewer signals can be delivered to us, reschedule signal
1369 if (p->p_numthreads != 1)
1370 reschedule_signals(p, new_block, 0);
1373 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1375 if (ksi->ksi_code == SI_TIMER)
1376 itimer_accept(p, ksi->ksi_timerid, ksi);
1379 if (KTRPOINT(td, KTR_PSIG)) {
1382 mtx_lock(&ps->ps_mtx);
1383 action = ps->ps_sigact[_SIG_IDX(sig)];
1384 mtx_unlock(&ps->ps_mtx);
1385 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1388 if (sig == SIGKILL) {
1389 proc_td_siginfo_capture(td, &ksi->ksi_info);
1397 #ifndef _SYS_SYSPROTO_H_
1398 struct sigpending_args {
1403 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1405 struct proc *p = td->td_proc;
1409 pending = p->p_sigqueue.sq_signals;
1410 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1412 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1415 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1416 #ifndef _SYS_SYSPROTO_H_
1417 struct osigpending_args {
1422 osigpending(struct thread *td, struct osigpending_args *uap)
1424 struct proc *p = td->td_proc;
1428 pending = p->p_sigqueue.sq_signals;
1429 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1431 SIG2OSIG(pending, td->td_retval[0]);
1434 #endif /* COMPAT_43 */
1436 #if defined(COMPAT_43)
1438 * Generalized interface signal handler, 4.3-compatible.
1440 #ifndef _SYS_SYSPROTO_H_
1441 struct osigvec_args {
1449 osigvec(struct thread *td, struct osigvec_args *uap)
1452 struct sigaction nsa, osa;
1453 struct sigaction *nsap, *osap;
1456 if (uap->signum <= 0 || uap->signum >= ONSIG)
1458 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1459 osap = (uap->osv != NULL) ? &osa : NULL;
1461 error = copyin(uap->nsv, &vec, sizeof(vec));
1464 nsap->sa_handler = vec.sv_handler;
1465 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1466 nsap->sa_flags = vec.sv_flags;
1467 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1469 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1470 if (osap && !error) {
1471 vec.sv_handler = osap->sa_handler;
1472 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1473 vec.sv_flags = osap->sa_flags;
1474 vec.sv_flags &= ~SA_NOCLDWAIT;
1475 vec.sv_flags ^= SA_RESTART;
1476 error = copyout(&vec, uap->osv, sizeof(vec));
1481 #ifndef _SYS_SYSPROTO_H_
1482 struct osigblock_args {
1487 osigblock(struct thread *td, struct osigblock_args *uap)
1491 OSIG2SIG(uap->mask, set);
1492 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1493 SIG2OSIG(oset, td->td_retval[0]);
1497 #ifndef _SYS_SYSPROTO_H_
1498 struct osigsetmask_args {
1503 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1507 OSIG2SIG(uap->mask, set);
1508 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1509 SIG2OSIG(oset, td->td_retval[0]);
1512 #endif /* COMPAT_43 */
1515 * Suspend calling thread until signal, providing mask to be set in the
1518 #ifndef _SYS_SYSPROTO_H_
1519 struct sigsuspend_args {
1520 const sigset_t *sigmask;
1525 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1530 error = copyin(uap->sigmask, &mask, sizeof(mask));
1533 return (kern_sigsuspend(td, mask));
1537 kern_sigsuspend(struct thread *td, sigset_t mask)
1539 struct proc *p = td->td_proc;
1542 /* Ensure the sigfastblock value is up to date. */
1543 sigfastblock_fetch(td);
1546 * When returning from sigsuspend, we want
1547 * the old mask to be restored after the
1548 * signal handler has finished. Thus, we
1549 * save it here and mark the sigacts structure
1553 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1554 SIGPROCMASK_PROC_LOCKED);
1555 td->td_pflags |= TDP_OLDMASK;
1558 * Process signals now. Otherwise, we can get spurious wakeup
1559 * due to signal entered process queue, but delivered to other
1560 * thread. But sigsuspend should return only on signal
1563 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1564 for (has_sig = 0; !has_sig;) {
1565 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1568 thread_suspend_check(0);
1569 mtx_lock(&p->p_sigacts->ps_mtx);
1570 while ((sig = cursig(td)) != 0) {
1571 KASSERT(sig >= 0, ("sig %d", sig));
1572 has_sig += postsig(sig);
1574 mtx_unlock(&p->p_sigacts->ps_mtx);
1577 * If PTRACE_SCE or PTRACE_SCX were set after
1578 * userspace entered the syscall, return spurious
1581 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1585 td->td_errno = EINTR;
1586 td->td_pflags |= TDP_NERRNO;
1587 return (EJUSTRETURN);
1590 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1592 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1593 * convention: libc stub passes mask, not pointer, to save a copyin.
1595 #ifndef _SYS_SYSPROTO_H_
1596 struct osigsuspend_args {
1602 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1606 OSIG2SIG(uap->mask, mask);
1607 return (kern_sigsuspend(td, mask));
1609 #endif /* COMPAT_43 */
1611 #if defined(COMPAT_43)
1612 #ifndef _SYS_SYSPROTO_H_
1613 struct osigstack_args {
1614 struct sigstack *nss;
1615 struct sigstack *oss;
1620 osigstack(struct thread *td, struct osigstack_args *uap)
1622 struct sigstack nss, oss;
1625 if (uap->nss != NULL) {
1626 error = copyin(uap->nss, &nss, sizeof(nss));
1630 oss.ss_sp = td->td_sigstk.ss_sp;
1631 oss.ss_onstack = sigonstack(cpu_getstack(td));
1632 if (uap->nss != NULL) {
1633 td->td_sigstk.ss_sp = nss.ss_sp;
1634 td->td_sigstk.ss_size = 0;
1635 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1636 td->td_pflags |= TDP_ALTSTACK;
1638 if (uap->oss != NULL)
1639 error = copyout(&oss, uap->oss, sizeof(oss));
1643 #endif /* COMPAT_43 */
1645 #ifndef _SYS_SYSPROTO_H_
1646 struct sigaltstack_args {
1653 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1658 if (uap->ss != NULL) {
1659 error = copyin(uap->ss, &ss, sizeof(ss));
1663 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1664 (uap->oss != NULL) ? &oss : NULL);
1667 if (uap->oss != NULL)
1668 error = copyout(&oss, uap->oss, sizeof(stack_t));
1673 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1675 struct proc *p = td->td_proc;
1678 oonstack = sigonstack(cpu_getstack(td));
1681 *oss = td->td_sigstk;
1682 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1683 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1689 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1691 if (!(ss->ss_flags & SS_DISABLE)) {
1692 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1695 td->td_sigstk = *ss;
1696 td->td_pflags |= TDP_ALTSTACK;
1698 td->td_pflags &= ~TDP_ALTSTACK;
1704 struct killpg1_ctx {
1714 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1718 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1719 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1722 err = p_cansignal(arg->td, p, arg->sig);
1723 if (err == 0 && arg->sig != 0)
1724 pksignal(p, arg->sig, arg->ksi);
1730 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1735 * Common code for kill process group/broadcast kill.
1736 * cp is calling process.
1739 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1743 struct killpg1_ctx arg;
1755 sx_slock(&allproc_lock);
1756 FOREACH_PROC_IN_SYSTEM(p) {
1757 killpg1_sendsig(p, true, &arg);
1759 sx_sunlock(&allproc_lock);
1761 sx_slock(&proctree_lock);
1764 * zero pgid means send to my process group.
1766 pgrp = td->td_proc->p_pgrp;
1769 pgrp = pgfind(pgid);
1771 sx_sunlock(&proctree_lock);
1775 sx_sunlock(&proctree_lock);
1776 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1777 killpg1_sendsig(p, false, &arg);
1781 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1782 if (arg.ret == 0 && !arg.sent)
1783 arg.ret = arg.found ? EPERM : ESRCH;
1787 #ifndef _SYS_SYSPROTO_H_
1795 sys_kill(struct thread *td, struct kill_args *uap)
1798 return (kern_kill(td, uap->pid, uap->signum));
1802 kern_kill(struct thread *td, pid_t pid, int signum)
1809 * A process in capability mode can send signals only to himself.
1810 * The main rationale behind this is that abort(3) is implemented as
1811 * kill(getpid(), SIGABRT).
1813 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1816 AUDIT_ARG_SIGNUM(signum);
1818 if ((u_int)signum > _SIG_MAXSIG)
1821 ksiginfo_init(&ksi);
1822 ksi.ksi_signo = signum;
1823 ksi.ksi_code = SI_USER;
1824 ksi.ksi_pid = td->td_proc->p_pid;
1825 ksi.ksi_uid = td->td_ucred->cr_ruid;
1828 /* kill single process */
1829 if ((p = pfind_any(pid)) == NULL)
1831 AUDIT_ARG_PROCESS(p);
1832 error = p_cansignal(td, p, signum);
1833 if (error == 0 && signum)
1834 pksignal(p, signum, &ksi);
1839 case -1: /* broadcast signal */
1840 return (killpg1(td, signum, 0, 1, &ksi));
1841 case 0: /* signal own process group */
1842 return (killpg1(td, signum, 0, 0, &ksi));
1843 default: /* negative explicit process group */
1844 return (killpg1(td, signum, -pid, 0, &ksi));
1850 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1855 AUDIT_ARG_SIGNUM(uap->signum);
1856 AUDIT_ARG_FD(uap->fd);
1857 if ((u_int)uap->signum > _SIG_MAXSIG)
1860 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1863 AUDIT_ARG_PROCESS(p);
1864 error = p_cansignal(td, p, uap->signum);
1865 if (error == 0 && uap->signum)
1866 kern_psignal(p, uap->signum);
1871 #if defined(COMPAT_43)
1872 #ifndef _SYS_SYSPROTO_H_
1873 struct okillpg_args {
1880 okillpg(struct thread *td, struct okillpg_args *uap)
1884 AUDIT_ARG_SIGNUM(uap->signum);
1885 AUDIT_ARG_PID(uap->pgid);
1886 if ((u_int)uap->signum > _SIG_MAXSIG)
1889 ksiginfo_init(&ksi);
1890 ksi.ksi_signo = uap->signum;
1891 ksi.ksi_code = SI_USER;
1892 ksi.ksi_pid = td->td_proc->p_pid;
1893 ksi.ksi_uid = td->td_ucred->cr_ruid;
1894 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1896 #endif /* COMPAT_43 */
1898 #ifndef _SYS_SYSPROTO_H_
1899 struct sigqueue_args {
1902 /* union sigval */ void *value;
1906 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1910 sv.sival_ptr = uap->value;
1912 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1916 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1922 if ((u_int)signum > _SIG_MAXSIG)
1926 * Specification says sigqueue can only send signal to
1932 if ((p = pfind_any(pid)) == NULL)
1934 error = p_cansignal(td, p, signum);
1935 if (error == 0 && signum != 0) {
1936 ksiginfo_init(&ksi);
1937 ksi.ksi_flags = KSI_SIGQ;
1938 ksi.ksi_signo = signum;
1939 ksi.ksi_code = SI_QUEUE;
1940 ksi.ksi_pid = td->td_proc->p_pid;
1941 ksi.ksi_uid = td->td_ucred->cr_ruid;
1942 ksi.ksi_value = *value;
1943 error = pksignal(p, ksi.ksi_signo, &ksi);
1950 * Send a signal to a process group.
1953 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1958 sx_slock(&proctree_lock);
1959 pgrp = pgfind(pgid);
1960 sx_sunlock(&proctree_lock);
1962 pgsignal(pgrp, sig, 0, ksi);
1969 * Send a signal to a process group. If checktty is 1,
1970 * limit to members which have a controlling terminal.
1973 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1978 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1979 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1981 if (p->p_state == PRS_NORMAL &&
1982 (checkctty == 0 || p->p_flag & P_CONTROLT))
1983 pksignal(p, sig, ksi);
1990 * Recalculate the signal mask and reset the signal disposition after
1991 * usermode frame for delivery is formed. Should be called after
1992 * mach-specific routine, because sysent->sv_sendsig() needs correct
1993 * ps_siginfo and signal mask.
1996 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2000 mtx_assert(&ps->ps_mtx, MA_OWNED);
2001 td->td_ru.ru_nsignals++;
2002 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2003 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2004 SIGADDSET(mask, sig);
2005 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2006 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2007 if (SIGISMEMBER(ps->ps_sigreset, sig))
2012 * Send a signal caused by a trap to the current thread. If it will be
2013 * caught immediately, deliver it with correct code. Otherwise, post it
2017 trapsignal(struct thread *td, ksiginfo_t *ksi)
2025 sig = ksi->ksi_signo;
2026 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2028 sigfastblock_fetch(td);
2031 mtx_lock(&ps->ps_mtx);
2032 sigmask = td->td_sigmask;
2033 if (td->td_sigblock_val != 0)
2034 SIGSETOR(sigmask, fastblock_mask);
2035 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2036 !SIGISMEMBER(sigmask, sig)) {
2038 if (KTRPOINT(curthread, KTR_PSIG))
2039 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2040 &td->td_sigmask, ksi->ksi_code);
2042 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2043 ksi, &td->td_sigmask);
2044 postsig_done(sig, td, ps);
2045 mtx_unlock(&ps->ps_mtx);
2048 * Avoid a possible infinite loop if the thread
2049 * masking the signal or process is ignoring the
2052 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2053 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2054 SIGDELSET(td->td_sigmask, sig);
2055 SIGDELSET(ps->ps_sigcatch, sig);
2056 SIGDELSET(ps->ps_sigignore, sig);
2057 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2058 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2059 td->td_sigblock_val = 0;
2061 mtx_unlock(&ps->ps_mtx);
2062 p->p_sig = sig; /* XXX to verify code */
2063 tdsendsignal(p, td, sig, ksi);
2068 static struct thread *
2069 sigtd(struct proc *p, int sig, bool fast_sigblock)
2071 struct thread *td, *signal_td;
2073 PROC_LOCK_ASSERT(p, MA_OWNED);
2074 MPASS(!fast_sigblock || p == curproc);
2077 * Check if current thread can handle the signal without
2078 * switching context to another thread.
2080 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2081 (!fast_sigblock || curthread->td_sigblock_val == 0))
2084 FOREACH_THREAD_IN_PROC(p, td) {
2085 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2086 td != curthread || td->td_sigblock_val == 0)) {
2091 if (signal_td == NULL)
2092 signal_td = FIRST_THREAD_IN_PROC(p);
2097 * Send the signal to the process. If the signal has an action, the action
2098 * is usually performed by the target process rather than the caller; we add
2099 * the signal to the set of pending signals for the process.
2102 * o When a stop signal is sent to a sleeping process that takes the
2103 * default action, the process is stopped without awakening it.
2104 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2105 * regardless of the signal action (eg, blocked or ignored).
2107 * Other ignored signals are discarded immediately.
2109 * NB: This function may be entered from the debugger via the "kill" DDB
2110 * command. There is little that can be done to mitigate the possibly messy
2111 * side effects of this unwise possibility.
2114 kern_psignal(struct proc *p, int sig)
2118 ksiginfo_init(&ksi);
2119 ksi.ksi_signo = sig;
2120 ksi.ksi_code = SI_KERNEL;
2121 (void) tdsendsignal(p, NULL, sig, &ksi);
2125 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2128 return (tdsendsignal(p, NULL, sig, ksi));
2131 /* Utility function for finding a thread to send signal event to. */
2133 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2137 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2138 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2150 tdsignal(struct thread *td, int sig)
2154 ksiginfo_init(&ksi);
2155 ksi.ksi_signo = sig;
2156 ksi.ksi_code = SI_KERNEL;
2157 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2161 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2164 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2168 sig_sleepq_abort(struct thread *td, int intrval)
2170 THREAD_LOCK_ASSERT(td, MA_OWNED);
2172 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2176 return (sleepq_abort(td, intrval));
2180 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2183 sigqueue_t *sigqueue;
2190 MPASS(td == NULL || p == td->td_proc);
2191 PROC_LOCK_ASSERT(p, MA_OWNED);
2193 if (!_SIG_VALID(sig))
2194 panic("%s(): invalid signal %d", __func__, sig);
2196 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2199 * IEEE Std 1003.1-2001: return success when killing a zombie.
2201 if (p->p_state == PRS_ZOMBIE) {
2202 if (ksi && (ksi->ksi_flags & KSI_INS))
2203 ksiginfo_tryfree(ksi);
2208 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2209 prop = sigprop(sig);
2212 td = sigtd(p, sig, false);
2213 sigqueue = &p->p_sigqueue;
2215 sigqueue = &td->td_sigqueue;
2217 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2220 * If the signal is being ignored, then we forget about it
2221 * immediately, except when the target process executes
2222 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2223 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2225 mtx_lock(&ps->ps_mtx);
2226 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2227 if (kern_sig_discard_ign &&
2228 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2229 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2231 mtx_unlock(&ps->ps_mtx);
2232 if (ksi && (ksi->ksi_flags & KSI_INS))
2233 ksiginfo_tryfree(ksi);
2240 if (SIGISMEMBER(td->td_sigmask, sig))
2242 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2246 if (SIGISMEMBER(ps->ps_sigintr, sig))
2251 mtx_unlock(&ps->ps_mtx);
2253 if (prop & SIGPROP_CONT)
2254 sigqueue_delete_stopmask_proc(p);
2255 else if (prop & SIGPROP_STOP) {
2257 * If sending a tty stop signal to a member of an orphaned
2258 * process group, discard the signal here if the action
2259 * is default; don't stop the process below if sleeping,
2260 * and don't clear any pending SIGCONT.
2262 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2263 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2264 action == SIG_DFL) {
2265 if (ksi && (ksi->ksi_flags & KSI_INS))
2266 ksiginfo_tryfree(ksi);
2269 sigqueue_delete_proc(p, SIGCONT);
2270 if (p->p_flag & P_CONTINUED) {
2271 p->p_flag &= ~P_CONTINUED;
2272 PROC_LOCK(p->p_pptr);
2273 sigqueue_take(p->p_ksi);
2274 PROC_UNLOCK(p->p_pptr);
2278 ret = sigqueue_add(sigqueue, sig, ksi);
2283 * Defer further processing for signals which are held,
2284 * except that stopped processes must be continued by SIGCONT.
2286 if (action == SIG_HOLD &&
2287 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2293 * Some signals have a process-wide effect and a per-thread
2294 * component. Most processing occurs when the process next
2295 * tries to cross the user boundary, however there are some
2296 * times when processing needs to be done immediately, such as
2297 * waking up threads so that they can cross the user boundary.
2298 * We try to do the per-process part here.
2300 if (P_SHOULDSTOP(p)) {
2301 KASSERT(!(p->p_flag & P_WEXIT),
2302 ("signal to stopped but exiting process"));
2303 if (sig == SIGKILL) {
2305 * If traced process is already stopped,
2306 * then no further action is necessary.
2308 if (p->p_flag & P_TRACED)
2311 * SIGKILL sets process running.
2312 * It will die elsewhere.
2313 * All threads must be restarted.
2315 p->p_flag &= ~P_STOPPED_SIG;
2319 if (prop & SIGPROP_CONT) {
2321 * If traced process is already stopped,
2322 * then no further action is necessary.
2324 if (p->p_flag & P_TRACED)
2327 * If SIGCONT is default (or ignored), we continue the
2328 * process but don't leave the signal in sigqueue as
2329 * it has no further action. If SIGCONT is held, we
2330 * continue the process and leave the signal in
2331 * sigqueue. If the process catches SIGCONT, let it
2332 * handle the signal itself. If it isn't waiting on
2333 * an event, it goes back to run state.
2334 * Otherwise, process goes back to sleep state.
2336 p->p_flag &= ~P_STOPPED_SIG;
2338 if (p->p_numthreads == p->p_suspcount) {
2340 p->p_flag |= P_CONTINUED;
2341 p->p_xsig = SIGCONT;
2342 PROC_LOCK(p->p_pptr);
2343 childproc_continued(p);
2344 PROC_UNLOCK(p->p_pptr);
2347 if (action == SIG_DFL) {
2348 thread_unsuspend(p);
2350 sigqueue_delete(sigqueue, sig);
2353 if (action == SIG_CATCH) {
2355 * The process wants to catch it so it needs
2356 * to run at least one thread, but which one?
2362 * The signal is not ignored or caught.
2364 thread_unsuspend(p);
2369 if (prop & SIGPROP_STOP) {
2371 * If traced process is already stopped,
2372 * then no further action is necessary.
2374 if (p->p_flag & P_TRACED)
2377 * Already stopped, don't need to stop again
2378 * (If we did the shell could get confused).
2379 * Just make sure the signal STOP bit set.
2381 p->p_flag |= P_STOPPED_SIG;
2382 sigqueue_delete(sigqueue, sig);
2387 * All other kinds of signals:
2388 * If a thread is sleeping interruptibly, simulate a
2389 * wakeup so that when it is continued it will be made
2390 * runnable and can look at the signal. However, don't make
2391 * the PROCESS runnable, leave it stopped.
2392 * It may run a bit until it hits a thread_suspend_check().
2396 if (TD_CAN_ABORT(td))
2397 wakeup_swapper = sig_sleepq_abort(td, intrval);
2403 * Mutexes are short lived. Threads waiting on them will
2404 * hit thread_suspend_check() soon.
2406 } else if (p->p_state == PRS_NORMAL) {
2407 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2408 tdsigwakeup(td, sig, action, intrval);
2412 MPASS(action == SIG_DFL);
2414 if (prop & SIGPROP_STOP) {
2415 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2417 p->p_flag |= P_STOPPED_SIG;
2420 wakeup_swapper = sig_suspend_threads(td, p, 1);
2421 if (p->p_numthreads == p->p_suspcount) {
2423 * only thread sending signal to another
2424 * process can reach here, if thread is sending
2425 * signal to its process, because thread does
2426 * not suspend itself here, p_numthreads
2427 * should never be equal to p_suspcount.
2431 sigqueue_delete_proc(p, p->p_xsig);
2437 /* Not in "NORMAL" state. discard the signal. */
2438 sigqueue_delete(sigqueue, sig);
2443 * The process is not stopped so we need to apply the signal to all the
2447 tdsigwakeup(td, sig, action, intrval);
2449 thread_unsuspend(p);
2452 itimer_proc_continue(p);
2453 kqtimer_proc_continue(p);
2455 /* If we jump here, proc slock should not be owned. */
2456 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2464 * The force of a signal has been directed against a single
2465 * thread. We need to see what we can do about knocking it
2466 * out of any sleep it may be in etc.
2469 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2471 struct proc *p = td->td_proc;
2472 int prop, wakeup_swapper;
2474 PROC_LOCK_ASSERT(p, MA_OWNED);
2475 prop = sigprop(sig);
2480 * Bring the priority of a thread up if we want it to get
2481 * killed in this lifetime. Be careful to avoid bumping the
2482 * priority of the idle thread, since we still allow to signal
2485 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2486 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2487 sched_prio(td, PUSER);
2488 if (TD_ON_SLEEPQ(td)) {
2490 * If thread is sleeping uninterruptibly
2491 * we can't interrupt the sleep... the signal will
2492 * be noticed when the process returns through
2493 * trap() or syscall().
2495 if ((td->td_flags & TDF_SINTR) == 0)
2498 * If SIGCONT is default (or ignored) and process is
2499 * asleep, we are finished; the process should not
2502 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2505 sigqueue_delete(&p->p_sigqueue, sig);
2507 * It may be on either list in this state.
2508 * Remove from both for now.
2510 sigqueue_delete(&td->td_sigqueue, sig);
2515 * Don't awaken a sleeping thread for SIGSTOP if the
2516 * STOP signal is deferred.
2518 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2519 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2523 * Give low priority threads a better chance to run.
2525 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2526 sched_prio(td, PUSER);
2528 wakeup_swapper = sig_sleepq_abort(td, intrval);
2536 * Other states do nothing with the signal immediately,
2537 * other than kicking ourselves if we are running.
2538 * It will either never be noticed, or noticed very soon.
2541 if (TD_IS_RUNNING(td) && td != curthread)
2551 ptrace_coredump(struct thread *td)
2554 struct thr_coredump_req *tcq;
2557 MPASS(td == curthread);
2559 PROC_LOCK_ASSERT(p, MA_OWNED);
2560 if ((td->td_dbgflags & TDB_COREDUMPRQ) == 0)
2562 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2564 tcq = td->td_coredump;
2565 KASSERT(tcq != NULL, ("td_coredump is NULL"));
2567 if (p->p_sysent->sv_coredump == NULL) {
2568 tcq->tc_error = ENOSYS;
2573 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2575 tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2576 tcq->tc_limit, tcq->tc_flags);
2578 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2581 td->td_dbgflags &= ~TDB_COREDUMPRQ;
2582 td->td_coredump = NULL;
2587 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2592 PROC_LOCK_ASSERT(p, MA_OWNED);
2593 PROC_SLOCK_ASSERT(p, MA_OWNED);
2594 MPASS(sending || td == curthread);
2597 FOREACH_THREAD_IN_PROC(p, td2) {
2599 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2600 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2601 (td2->td_flags & TDF_SINTR)) {
2602 if (td2->td_flags & TDF_SBDRY) {
2604 * Once a thread is asleep with
2605 * TDF_SBDRY and without TDF_SERESTART
2606 * or TDF_SEINTR set, it should never
2607 * become suspended due to this check.
2609 KASSERT(!TD_IS_SUSPENDED(td2),
2610 ("thread with deferred stops suspended"));
2611 if (TD_SBDRY_INTR(td2)) {
2612 wakeup_swapper |= sleepq_abort(td2,
2613 TD_SBDRY_ERRNO(td2));
2616 } else if (!TD_IS_SUSPENDED(td2))
2617 thread_suspend_one(td2);
2618 } else if (!TD_IS_SUSPENDED(td2)) {
2619 if (sending || td != td2)
2620 td2->td_flags |= TDF_ASTPENDING;
2622 if (TD_IS_RUNNING(td2) && td2 != td)
2623 forward_signal(td2);
2628 return (wakeup_swapper);
2632 * Stop the process for an event deemed interesting to the debugger. If si is
2633 * non-NULL, this is a signal exchange; the new signal requested by the
2634 * debugger will be returned for handling. If si is NULL, this is some other
2635 * type of interesting event. The debugger may request a signal be delivered in
2636 * that case as well, however it will be deferred until it can be handled.
2639 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2641 struct proc *p = td->td_proc;
2645 PROC_LOCK_ASSERT(p, MA_OWNED);
2646 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2647 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2648 &p->p_mtx.lock_object, "Stopping for traced signal");
2652 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2653 td->td_dbgflags |= TDB_XSIG;
2654 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2655 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2657 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2660 * Ensure that, if we've been PT_KILLed, the
2661 * exit status reflects that. Another thread
2662 * may also be in ptracestop(), having just
2663 * received the SIGKILL, but this thread was
2664 * unsuspended first.
2666 td->td_dbgflags &= ~TDB_XSIG;
2667 td->td_xsig = SIGKILL;
2671 if (p->p_flag & P_SINGLE_EXIT &&
2672 !(td->td_dbgflags & TDB_EXIT)) {
2674 * Ignore ptrace stops except for thread exit
2675 * events when the process exits.
2677 td->td_dbgflags &= ~TDB_XSIG;
2683 * Make wait(2) work. Ensure that right after the
2684 * attach, the thread which was decided to become the
2685 * leader of attach gets reported to the waiter.
2686 * Otherwise, just avoid overwriting another thread's
2687 * assignment to p_xthread. If another thread has
2688 * already set p_xthread, the current thread will get
2689 * a chance to report itself upon the next iteration.
2691 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2692 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2693 p->p_xthread == NULL)) {
2698 * If we are on sleepqueue already,
2699 * let sleepqueue code decide if it
2700 * needs to go sleep after attach.
2702 if (td->td_wchan == NULL)
2703 td->td_dbgflags &= ~TDB_FSTP;
2705 p->p_flag2 &= ~P2_PTRACE_FSTP;
2706 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2707 sig_suspend_threads(td, p, 0);
2709 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2710 td->td_dbgflags &= ~TDB_STOPATFORK;
2713 td->td_dbgflags |= TDB_SSWITCH;
2714 thread_suspend_switch(td, p);
2715 td->td_dbgflags &= ~TDB_SSWITCH;
2716 if ((td->td_dbgflags & TDB_COREDUMPRQ) != 0) {
2718 ptrace_coredump(td);
2722 if (p->p_xthread == td)
2723 p->p_xthread = NULL;
2724 if (!(p->p_flag & P_TRACED))
2726 if (td->td_dbgflags & TDB_SUSPEND) {
2727 if (p->p_flag & P_SINGLE_EXIT)
2735 if (si != NULL && sig == td->td_xsig) {
2736 /* Parent wants us to take the original signal unchanged. */
2737 si->ksi_flags |= KSI_HEAD;
2738 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2740 } else if (td->td_xsig != 0) {
2742 * If parent wants us to take a new signal, then it will leave
2743 * it in td->td_xsig; otherwise we just look for signals again.
2745 ksiginfo_init(&ksi);
2746 ksi.ksi_signo = td->td_xsig;
2747 ksi.ksi_flags |= KSI_PTRACE;
2748 td2 = sigtd(p, td->td_xsig, false);
2749 tdsendsignal(p, td2, td->td_xsig, &ksi);
2754 return (td->td_xsig);
2758 reschedule_signals(struct proc *p, sigset_t block, int flags)
2763 bool fastblk, pslocked;
2765 PROC_LOCK_ASSERT(p, MA_OWNED);
2767 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2768 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2769 if (SIGISEMPTY(p->p_siglist))
2771 SIGSETAND(block, p->p_siglist);
2772 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2773 SIG_FOREACH(sig, &block) {
2774 td = sigtd(p, sig, fastblk);
2777 * If sigtd() selected us despite sigfastblock is
2778 * blocking, do not activate AST or wake us, to avoid
2779 * loop in AST handler.
2781 if (fastblk && td == curthread)
2786 mtx_lock(&ps->ps_mtx);
2787 if (p->p_flag & P_TRACED ||
2788 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2789 !SIGISMEMBER(td->td_sigmask, sig))) {
2790 tdsigwakeup(td, sig, SIG_CATCH,
2791 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2795 mtx_unlock(&ps->ps_mtx);
2800 tdsigcleanup(struct thread *td)
2806 PROC_LOCK_ASSERT(p, MA_OWNED);
2808 sigqueue_flush(&td->td_sigqueue);
2809 if (p->p_numthreads == 1)
2813 * Since we cannot handle signals, notify signal post code
2814 * about this by filling the sigmask.
2816 * Also, if needed, wake up thread(s) that do not block the
2817 * same signals as the exiting thread, since the thread might
2818 * have been selected for delivery and woken up.
2820 SIGFILLSET(unblocked);
2821 SIGSETNAND(unblocked, td->td_sigmask);
2822 SIGFILLSET(td->td_sigmask);
2823 reschedule_signals(p, unblocked, 0);
2828 sigdeferstop_curr_flags(int cflags)
2831 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
2832 (cflags & TDF_SBDRY) != 0);
2833 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2837 * Defer the delivery of SIGSTOP for the current thread, according to
2838 * the requested mode. Returns previous flags, which must be restored
2839 * by sigallowstop().
2841 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
2842 * cleared by the current thread, which allow the lock-less read-only
2846 sigdeferstop_impl(int mode)
2852 cflags = sigdeferstop_curr_flags(td->td_flags);
2854 case SIGDEFERSTOP_NOP:
2857 case SIGDEFERSTOP_OFF:
2860 case SIGDEFERSTOP_SILENT:
2861 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
2863 case SIGDEFERSTOP_EINTR:
2864 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
2866 case SIGDEFERSTOP_ERESTART:
2867 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
2870 panic("sigdeferstop: invalid mode %x", mode);
2873 if (cflags == nflags)
2874 return (SIGDEFERSTOP_VAL_NCHG);
2876 td->td_flags = (td->td_flags & ~cflags) | nflags;
2882 * Restores the STOP handling mode, typically permitting the delivery
2883 * of SIGSTOP for the current thread. This does not immediately
2884 * suspend if a stop was posted. Instead, the thread will suspend
2885 * either via ast() or a subsequent interruptible sleep.
2888 sigallowstop_impl(int prev)
2893 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
2894 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
2895 ("sigallowstop: incorrect previous mode %x", prev));
2897 cflags = sigdeferstop_curr_flags(td->td_flags);
2898 if (cflags != prev) {
2900 td->td_flags = (td->td_flags & ~cflags) | prev;
2909 SIGSTATUS_SBDRY_STOP,
2913 * The thread has signal "sig" pending. Figure out what to do with it:
2915 * _HANDLE -> the caller should handle the signal
2916 * _HANDLED -> handled internally, reload pending signal set
2917 * _IGNORE -> ignored, remove from the set of pending signals and try the
2918 * next pending signal
2919 * _SBDRY_STOP -> the signal should stop the thread but this is not
2920 * permitted in the current context
2922 static enum sigstatus
2923 sigprocess(struct thread *td, int sig)
2927 struct sigqueue *queue;
2931 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
2935 mtx_assert(&ps->ps_mtx, MA_OWNED);
2936 PROC_LOCK_ASSERT(p, MA_OWNED);
2939 * We should allow pending but ignored signals below
2940 * only if there is sigwait() active, or P_TRACED was
2941 * on when they were posted.
2943 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
2944 (p->p_flag & P_TRACED) == 0 &&
2945 (td->td_flags & TDF_SIGWAIT) == 0) {
2946 return (SIGSTATUS_IGNORE);
2949 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
2951 * If traced, always stop.
2952 * Remove old signal from queue before the stop.
2953 * XXX shrug off debugger, it causes siginfo to
2956 queue = &td->td_sigqueue;
2957 ksiginfo_init(&ksi);
2958 if (sigqueue_get(queue, sig, &ksi) == 0) {
2959 queue = &p->p_sigqueue;
2960 sigqueue_get(queue, sig, &ksi);
2962 td->td_si = ksi.ksi_info;
2964 mtx_unlock(&ps->ps_mtx);
2965 sig = ptracestop(td, sig, &ksi);
2966 mtx_lock(&ps->ps_mtx);
2968 td->td_si.si_signo = 0;
2971 * Keep looking if the debugger discarded or
2972 * replaced the signal.
2975 return (SIGSTATUS_HANDLED);
2978 * If the signal became masked, re-queue it.
2980 if (SIGISMEMBER(td->td_sigmask, sig)) {
2981 ksi.ksi_flags |= KSI_HEAD;
2982 sigqueue_add(&p->p_sigqueue, sig, &ksi);
2983 return (SIGSTATUS_HANDLED);
2987 * If the traced bit got turned off, requeue the signal and
2988 * reload the set of pending signals. This ensures that p_sig*
2989 * and p_sigact are consistent.
2991 if ((p->p_flag & P_TRACED) == 0) {
2992 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
2993 ksi.ksi_flags |= KSI_HEAD;
2994 sigqueue_add(queue, sig, &ksi);
2996 return (SIGSTATUS_HANDLED);
3001 * Decide whether the signal should be returned.
3002 * Return the signal's number, or fall through
3003 * to clear it from the pending mask.
3005 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3006 case (intptr_t)SIG_DFL:
3008 * Don't take default actions on system processes.
3010 if (p->p_pid <= 1) {
3013 * Are you sure you want to ignore SIGSEGV
3016 printf("Process (pid %lu) got signal %d\n",
3017 (u_long)p->p_pid, sig);
3019 return (SIGSTATUS_IGNORE);
3023 * If there is a pending stop signal to process with
3024 * default action, stop here, then clear the signal.
3025 * Traced or exiting processes should ignore stops.
3026 * Additionally, a member of an orphaned process group
3027 * should ignore tty stops.
3029 prop = sigprop(sig);
3030 if (prop & SIGPROP_STOP) {
3031 mtx_unlock(&ps->ps_mtx);
3032 if ((p->p_flag & (P_TRACED | P_WEXIT |
3033 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3034 pg_flags & PGRP_ORPHANED) != 0 &&
3035 (prop & SIGPROP_TTYSTOP) != 0)) {
3036 mtx_lock(&ps->ps_mtx);
3037 return (SIGSTATUS_IGNORE);
3039 if (TD_SBDRY_INTR(td)) {
3040 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3041 ("lost TDF_SBDRY"));
3042 mtx_lock(&ps->ps_mtx);
3043 return (SIGSTATUS_SBDRY_STOP);
3045 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3046 &p->p_mtx.lock_object, "Catching SIGSTOP");
3047 sigqueue_delete(&td->td_sigqueue, sig);
3048 sigqueue_delete(&p->p_sigqueue, sig);
3049 p->p_flag |= P_STOPPED_SIG;
3052 sig_suspend_threads(td, p, 0);
3053 thread_suspend_switch(td, p);
3055 mtx_lock(&ps->ps_mtx);
3056 return (SIGSTATUS_HANDLED);
3057 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3058 (td->td_flags & TDF_SIGWAIT) == 0) {
3060 * Default action is to ignore; drop it if
3061 * not in kern_sigtimedwait().
3063 return (SIGSTATUS_IGNORE);
3065 return (SIGSTATUS_HANDLE);
3068 case (intptr_t)SIG_IGN:
3069 if ((td->td_flags & TDF_SIGWAIT) == 0)
3070 return (SIGSTATUS_IGNORE);
3072 return (SIGSTATUS_HANDLE);
3076 * This signal has an action, let postsig() process it.
3078 return (SIGSTATUS_HANDLE);
3083 * If the current process has received a signal (should be caught or cause
3084 * termination, should interrupt current syscall), return the signal number.
3085 * Stop signals with default action are processed immediately, then cleared;
3086 * they aren't returned. This is checked after each entry to the system for
3087 * a syscall or trap (though this can usually be done without calling
3088 * issignal by checking the pending signal masks in cursig.) The normal call
3091 * while (sig = cursig(curthread))
3095 issignal(struct thread *td)
3098 sigset_t sigpending;
3102 PROC_LOCK_ASSERT(p, MA_OWNED);
3105 sigpending = td->td_sigqueue.sq_signals;
3106 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3107 SIGSETNAND(sigpending, td->td_sigmask);
3109 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3110 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3111 SIG_STOPSIGMASK(sigpending);
3112 if (SIGISEMPTY(sigpending)) /* no signal to send */
3116 * Do fast sigblock if requested by usermode. Since
3117 * we do know that there was a signal pending at this
3118 * point, set the FAST_SIGBLOCK_PEND as indicator for
3119 * usermode to perform a dummy call to
3120 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3121 * delivery of postponed pending signal.
3123 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3124 if (td->td_sigblock_val != 0)
3125 SIGSETNAND(sigpending, fastblock_mask);
3126 if (SIGISEMPTY(sigpending)) {
3127 td->td_pflags |= TDP_SIGFASTPENDING;
3132 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3133 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3134 SIGISMEMBER(sigpending, SIGSTOP)) {
3136 * If debugger just attached, always consume
3137 * SIGSTOP from ptrace(PT_ATTACH) first, to
3138 * execute the debugger attach ritual in
3141 td->td_dbgflags |= TDB_FSTP;
3142 SIGEMPTYSET(sigpending);
3143 SIGADDSET(sigpending, SIGSTOP);
3146 SIG_FOREACH(sig, &sigpending) {
3147 switch (sigprocess(td, sig)) {
3148 case SIGSTATUS_HANDLE:
3150 case SIGSTATUS_HANDLED:
3152 case SIGSTATUS_IGNORE:
3153 sigqueue_delete(&td->td_sigqueue, sig);
3154 sigqueue_delete(&p->p_sigqueue, sig);
3156 case SIGSTATUS_SBDRY_STOP:
3165 thread_stopped(struct proc *p)
3169 PROC_LOCK_ASSERT(p, MA_OWNED);
3170 PROC_SLOCK_ASSERT(p, MA_OWNED);
3174 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3176 p->p_flag &= ~P_WAITED;
3177 PROC_LOCK(p->p_pptr);
3178 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3179 CLD_TRAPPED : CLD_STOPPED);
3180 PROC_UNLOCK(p->p_pptr);
3186 * Take the action for the specified signal
3187 * from the current set of pending signals.
3197 sigset_t returnmask;
3199 KASSERT(sig != 0, ("postsig"));
3203 PROC_LOCK_ASSERT(p, MA_OWNED);
3205 mtx_assert(&ps->ps_mtx, MA_OWNED);
3206 ksiginfo_init(&ksi);
3207 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3208 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3210 ksi.ksi_signo = sig;
3211 if (ksi.ksi_code == SI_TIMER)
3212 itimer_accept(p, ksi.ksi_timerid, &ksi);
3213 action = ps->ps_sigact[_SIG_IDX(sig)];
3215 if (KTRPOINT(td, KTR_PSIG))
3216 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3217 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3220 if (action == SIG_DFL) {
3222 * Default action, where the default is to kill
3223 * the process. (Other cases were ignored above.)
3225 mtx_unlock(&ps->ps_mtx);
3226 proc_td_siginfo_capture(td, &ksi.ksi_info);
3231 * If we get here, the signal must be caught.
3233 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3234 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3235 ("postsig action: blocked sig %d", sig));
3238 * Set the new mask value and also defer further
3239 * occurrences of this signal.
3241 * Special case: user has done a sigsuspend. Here the
3242 * current mask is not of interest, but rather the
3243 * mask from before the sigsuspend is what we want
3244 * restored after the signal processing is completed.
3246 if (td->td_pflags & TDP_OLDMASK) {
3247 returnmask = td->td_oldsigmask;
3248 td->td_pflags &= ~TDP_OLDMASK;
3250 returnmask = td->td_sigmask;
3252 if (p->p_sig == sig) {
3255 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3256 postsig_done(sig, td, ps);
3262 sig_ast_checksusp(struct thread *td)
3264 struct proc *p __diagused;
3268 PROC_LOCK_ASSERT(p, MA_OWNED);
3270 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
3273 ret = thread_suspend_check(1);
3274 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3279 sig_ast_needsigchk(struct thread *td)
3286 PROC_LOCK_ASSERT(p, MA_OWNED);
3288 if ((td->td_flags & TDF_NEEDSIGCHK) == 0)
3292 mtx_lock(&ps->ps_mtx);
3295 mtx_unlock(&ps->ps_mtx);
3296 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3297 KASSERT(TD_SBDRY_INTR(td),
3298 ("lost TDF_SERESTART of TDF_SEINTR"));
3299 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3300 (TDF_SEINTR | TDF_SERESTART),
3301 ("both TDF_SEINTR and TDF_SERESTART"));
3302 ret = TD_SBDRY_ERRNO(td);
3303 } else if (sig != 0) {
3304 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3305 mtx_unlock(&ps->ps_mtx);
3307 mtx_unlock(&ps->ps_mtx);
3312 * Do not go into sleep if this thread was the ptrace(2)
3313 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3314 * but we usually act on the signal by interrupting sleep, and
3315 * should do that here as well.
3317 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3320 td->td_dbgflags &= ~TDB_FSTP;
3334 if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0)
3340 ret = sig_ast_checksusp(td);
3342 ret = sig_ast_needsigchk(td);
3348 curproc_sigkilled(void)
3356 if ((td->td_flags & TDF_NEEDSIGCHK) == 0)
3362 mtx_lock(&ps->ps_mtx);
3363 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3364 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3365 mtx_unlock(&ps->ps_mtx);
3371 proc_wkilled(struct proc *p)
3374 PROC_LOCK_ASSERT(p, MA_OWNED);
3375 if ((p->p_flag & P_WKILLED) == 0) {
3376 p->p_flag |= P_WKILLED;
3378 * Notify swapper that there is a process to swap in.
3379 * The notification is racy, at worst it would take 10
3380 * seconds for the swapper process to notice.
3382 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3388 * Kill the current process for stated reason.
3391 killproc(struct proc *p, const char *why)
3394 PROC_LOCK_ASSERT(p, MA_OWNED);
3395 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3397 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3398 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3399 p->p_ucred->cr_uid, why);
3401 kern_psignal(p, SIGKILL);
3405 * Force the current process to exit with the specified signal, dumping core
3406 * if appropriate. We bypass the normal tests for masked and caught signals,
3407 * allowing unrecoverable failures to terminate the process without changing
3408 * signal state. Mark the accounting record with the signal termination.
3409 * If dumping core, save the signal number for the debugger. Calls exit and
3413 sigexit(struct thread *td, int sig)
3415 struct proc *p = td->td_proc;
3417 PROC_LOCK_ASSERT(p, MA_OWNED);
3418 p->p_acflag |= AXSIG;
3420 * We must be single-threading to generate a core dump. This
3421 * ensures that the registers in the core file are up-to-date.
3422 * Also, the ELF dump handler assumes that the thread list doesn't
3423 * change out from under it.
3425 * XXX If another thread attempts to single-thread before us
3426 * (e.g. via fork()), we won't get a dump at all.
3428 if ((sigprop(sig) & SIGPROP_CORE) &&
3429 thread_single(p, SINGLE_NO_EXIT) == 0) {
3432 * Log signals which would cause core dumps
3433 * (Log as LOG_INFO to appease those who don't want
3435 * XXX : Todo, as well as euid, write out ruid too
3436 * Note that coredump() drops proc lock.
3438 if (coredump(td) == 0)
3440 if (kern_logsigexit)
3442 "pid %d (%s), jid %d, uid %d: exited on "
3443 "signal %d%s\n", p->p_pid, p->p_comm,
3444 p->p_ucred->cr_prison->pr_id,
3445 td->td_ucred->cr_uid,
3447 sig & WCOREFLAG ? " (core dumped)" : "");
3455 * Send queued SIGCHLD to parent when child process's state
3459 sigparent(struct proc *p, int reason, int status)
3461 PROC_LOCK_ASSERT(p, MA_OWNED);
3462 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3464 if (p->p_ksi != NULL) {
3465 p->p_ksi->ksi_signo = SIGCHLD;
3466 p->p_ksi->ksi_code = reason;
3467 p->p_ksi->ksi_status = status;
3468 p->p_ksi->ksi_pid = p->p_pid;
3469 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3470 if (KSI_ONQ(p->p_ksi))
3473 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3477 childproc_jobstate(struct proc *p, int reason, int sig)
3481 PROC_LOCK_ASSERT(p, MA_OWNED);
3482 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3485 * Wake up parent sleeping in kern_wait(), also send
3486 * SIGCHLD to parent, but SIGCHLD does not guarantee
3487 * that parent will awake, because parent may masked
3490 p->p_pptr->p_flag |= P_STATCHILD;
3493 ps = p->p_pptr->p_sigacts;
3494 mtx_lock(&ps->ps_mtx);
3495 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3496 mtx_unlock(&ps->ps_mtx);
3497 sigparent(p, reason, sig);
3499 mtx_unlock(&ps->ps_mtx);
3503 childproc_stopped(struct proc *p, int reason)
3506 childproc_jobstate(p, reason, p->p_xsig);
3510 childproc_continued(struct proc *p)
3512 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3516 childproc_exited(struct proc *p)
3520 if (WCOREDUMP(p->p_xsig)) {
3521 reason = CLD_DUMPED;
3522 status = WTERMSIG(p->p_xsig);
3523 } else if (WIFSIGNALED(p->p_xsig)) {
3524 reason = CLD_KILLED;
3525 status = WTERMSIG(p->p_xsig);
3527 reason = CLD_EXITED;
3528 status = p->p_xexit;
3531 * XXX avoid calling wakeup(p->p_pptr), the work is
3534 sigparent(p, reason, status);
3537 #define MAX_NUM_CORE_FILES 100000
3538 #ifndef NUM_CORE_FILES
3539 #define NUM_CORE_FILES 5
3541 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3542 static int num_cores = NUM_CORE_FILES;
3545 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3550 new_val = num_cores;
3551 error = sysctl_handle_int(oidp, &new_val, 0, req);
3552 if (error != 0 || req->newptr == NULL)
3554 if (new_val > MAX_NUM_CORE_FILES)
3555 new_val = MAX_NUM_CORE_FILES;
3558 num_cores = new_val;
3561 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3562 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3563 sysctl_debug_num_cores_check, "I",
3564 "Maximum number of generated process corefiles while using index format");
3566 #define GZIP_SUFFIX ".gz"
3567 #define ZSTD_SUFFIX ".zst"
3569 int compress_user_cores = 0;
3572 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3576 val = compress_user_cores;
3577 error = sysctl_handle_int(oidp, &val, 0, req);
3578 if (error != 0 || req->newptr == NULL)
3580 if (val != 0 && !compressor_avail(val))
3582 compress_user_cores = val;
3585 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3586 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3587 sysctl_compress_user_cores, "I",
3588 "Enable compression of user corefiles ("
3589 __XSTRING(COMPRESS_GZIP) " = gzip, "
3590 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3592 int compress_user_cores_level = 6;
3593 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3594 &compress_user_cores_level, 0,
3595 "Corefile compression level");
3598 * Protect the access to corefilename[] by allproc_lock.
3600 #define corefilename_lock allproc_lock
3602 static char corefilename[MAXPATHLEN] = {"%N.core"};
3603 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3606 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3610 sx_xlock(&corefilename_lock);
3611 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3613 sx_xunlock(&corefilename_lock);
3617 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3618 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3619 "Process corefile name format string");
3622 vnode_close_locked(struct thread *td, struct vnode *vp)
3626 vn_close(vp, FWRITE, td->td_ucred, td);
3630 * If the core format has a %I in it, then we need to check
3631 * for existing corefiles before defining a name.
3632 * To do this we iterate over 0..ncores to find a
3633 * non-existing core file name to use. If all core files are
3634 * already used we choose the oldest one.
3637 corefile_open_last(struct thread *td, char *name, int indexpos,
3638 int indexlen, int ncores, struct vnode **vpp)
3640 struct vnode *oldvp, *nextvp, *vp;
3642 struct nameidata nd;
3643 int error, i, flags, oflags, cmode;
3645 struct timespec lasttime;
3647 nextvp = oldvp = NULL;
3648 cmode = S_IRUSR | S_IWUSR;
3649 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3650 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3652 for (i = 0; i < ncores; i++) {
3653 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3655 ch = name[indexpos + indexlen];
3656 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3658 name[indexpos + indexlen] = ch;
3660 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3661 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3668 if ((flags & O_CREAT) == O_CREAT) {
3673 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3675 vnode_close_locked(td, vp);
3679 if (oldvp == NULL ||
3680 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3681 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3682 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3684 vn_close(oldvp, FWRITE, td->td_ucred, td);
3687 lasttime = vattr.va_mtime;
3689 vnode_close_locked(td, vp);
3693 if (oldvp != NULL) {
3694 if (nextvp == NULL) {
3695 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3697 vn_close(oldvp, FWRITE, td->td_ucred, td);
3700 error = vn_lock(nextvp, LK_EXCLUSIVE);
3702 vn_close(nextvp, FWRITE, td->td_ucred,
3708 vn_close(oldvp, FWRITE, td->td_ucred, td);
3713 vnode_close_locked(td, oldvp);
3722 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3723 * Expand the name described in corefilename, using name, uid, and pid
3724 * and open/create core file.
3725 * corefilename is a printf-like string, with three format specifiers:
3726 * %N name of process ("name")
3727 * %P process id (pid)
3729 * For example, "%N.core" is the default; they can be disabled completely
3730 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3731 * This is controlled by the sysctl variable kern.corefile (see above).
3734 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3735 int compress, int signum, struct vnode **vpp, char **namep)
3738 struct nameidata nd;
3740 char *hostname, *name;
3741 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3744 format = corefilename;
3745 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3749 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3750 sx_slock(&corefilename_lock);
3751 for (i = 0; format[i] != '\0'; i++) {
3752 switch (format[i]) {
3753 case '%': /* Format character */
3755 switch (format[i]) {
3757 sbuf_putc(&sb, '%');
3759 case 'H': /* hostname */
3760 if (hostname == NULL) {
3761 hostname = malloc(MAXHOSTNAMELEN,
3764 getcredhostname(td->td_ucred, hostname,
3766 sbuf_printf(&sb, "%s", hostname);
3768 case 'I': /* autoincrementing index */
3769 if (indexpos != -1) {
3770 sbuf_printf(&sb, "%%I");
3774 indexpos = sbuf_len(&sb);
3775 sbuf_printf(&sb, "%u", ncores - 1);
3776 indexlen = sbuf_len(&sb) - indexpos;
3778 case 'N': /* process name */
3779 sbuf_printf(&sb, "%s", comm);
3781 case 'P': /* process id */
3782 sbuf_printf(&sb, "%u", pid);
3784 case 'S': /* signal number */
3785 sbuf_printf(&sb, "%i", signum);
3787 case 'U': /* user id */
3788 sbuf_printf(&sb, "%u", uid);
3792 "Unknown format character %c in "
3793 "corename `%s'\n", format[i], format);
3798 sbuf_putc(&sb, format[i]);
3802 sx_sunlock(&corefilename_lock);
3803 free(hostname, M_TEMP);
3804 if (compress == COMPRESS_GZIP)
3805 sbuf_printf(&sb, GZIP_SUFFIX);
3806 else if (compress == COMPRESS_ZSTD)
3807 sbuf_printf(&sb, ZSTD_SUFFIX);
3808 if (sbuf_error(&sb) != 0) {
3809 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3810 "long\n", (long)pid, comm, (u_long)uid);
3818 if (indexpos != -1) {
3819 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3823 "pid %d (%s), uid (%u): Path `%s' failed "
3824 "on initial open test, error = %d\n",
3825 pid, comm, uid, name, error);
3828 cmode = S_IRUSR | S_IWUSR;
3829 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3830 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3831 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3832 if ((td->td_proc->p_flag & P_SUGID) != 0)
3835 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3836 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3846 audit_proc_coredump(td, name, error);
3856 * Dump a process' core. The main routine does some
3857 * policy checking, and creates the name of the coredump;
3858 * then it passes on a vnode and a size limit to the process-specific
3859 * coredump routine if there is one; if there _is not_ one, it returns
3860 * ENOSYS; otherwise it returns the error from the process-specific routine.
3864 coredump(struct thread *td)
3866 struct proc *p = td->td_proc;
3867 struct ucred *cred = td->td_ucred;
3871 size_t fullpathsize;
3872 int error, error1, locked;
3873 char *name; /* name of corefile */
3876 char *fullpath, *freepath = NULL;
3879 PROC_LOCK_ASSERT(p, MA_OWNED);
3880 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3882 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3883 (p->p_flag2 & P2_NOTRACE) != 0) {
3889 * Note that the bulk of limit checking is done after
3890 * the corefile is created. The exception is if the limit
3891 * for corefiles is 0, in which case we don't bother
3892 * creating the corefile at all. This layout means that
3893 * a corefile is truncated instead of not being created,
3894 * if it is larger than the limit.
3896 limit = (off_t)lim_cur(td, RLIMIT_CORE);
3897 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3903 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
3904 compress_user_cores, p->p_sig, &vp, &name);
3909 * Don't dump to non-regular files or files with links.
3910 * Do not dump into system files. Effective user must own the corefile.
3912 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3913 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
3914 vattr.va_uid != cred->cr_uid) {
3922 /* Postpone other writers, including core dumps of other processes. */
3923 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3925 lf.l_whence = SEEK_SET;
3928 lf.l_type = F_WRLCK;
3929 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3933 if (set_core_nodump_flag)
3934 vattr.va_flags = UF_NODUMP;
3935 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3936 VOP_SETATTR(vp, &vattr, cred);
3939 p->p_acflag |= ACORE;
3942 if (p->p_sysent->sv_coredump != NULL) {
3943 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
3949 lf.l_type = F_UNLCK;
3950 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3952 vn_rangelock_unlock(vp, rl_cookie);
3955 * Notify the userland helper that a process triggered a core dump.
3956 * This allows the helper to run an automated debugging session.
3958 if (error != 0 || coredump_devctl == 0)
3960 sb = sbuf_new_auto();
3961 if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
3963 sbuf_printf(sb, "comm=\"");
3964 devctl_safe_quote_sb(sb, fullpath);
3965 free(freepath, M_TEMP);
3966 sbuf_printf(sb, "\" core=\"");
3969 * We can't lookup core file vp directly. When we're replacing a core, and
3970 * other random times, we flush the name cache, so it will fail. Instead,
3971 * if the path of the core is relative, add the current dir in front if it.
3973 if (name[0] != '/') {
3974 fullpathsize = MAXPATHLEN;
3975 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
3976 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
3977 free(freepath, M_TEMP);
3980 devctl_safe_quote_sb(sb, fullpath);
3981 free(freepath, M_TEMP);
3984 devctl_safe_quote_sb(sb, name);
3985 sbuf_printf(sb, "\"");
3986 if (sbuf_finish(sb) == 0)
3987 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
3991 error1 = vn_close(vp, FWRITE, cred, td);
3995 audit_proc_coredump(td, name, error);
4002 * Nonexistent system call-- signal process (may want to handle it). Flag
4003 * error in case process won't see signal immediately (blocked or ignored).
4005 #ifndef _SYS_SYSPROTO_H_
4012 nosys(struct thread *td, struct nosys_args *args)
4019 tdsignal(td, SIGSYS);
4021 if (kern_lognosys == 1 || kern_lognosys == 3) {
4022 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4025 if (kern_lognosys == 2 || kern_lognosys == 3 ||
4026 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4027 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4034 * Send a SIGIO or SIGURG signal to a process or process group using stored
4035 * credentials rather than those of the current process.
4038 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4041 struct sigio *sigio;
4043 ksiginfo_init(&ksi);
4044 ksi.ksi_signo = sig;
4045 ksi.ksi_code = SI_KERNEL;
4049 if (sigio == NULL) {
4053 if (sigio->sio_pgid > 0) {
4054 PROC_LOCK(sigio->sio_proc);
4055 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4056 kern_psignal(sigio->sio_proc, sig);
4057 PROC_UNLOCK(sigio->sio_proc);
4058 } else if (sigio->sio_pgid < 0) {
4061 PGRP_LOCK(sigio->sio_pgrp);
4062 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4064 if (p->p_state == PRS_NORMAL &&
4065 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4066 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4067 kern_psignal(p, sig);
4070 PGRP_UNLOCK(sigio->sio_pgrp);
4076 filt_sigattach(struct knote *kn)
4078 struct proc *p = curproc;
4080 kn->kn_ptr.p_proc = p;
4081 kn->kn_flags |= EV_CLEAR; /* automatically set */
4083 knlist_add(p->p_klist, kn, 0);
4089 filt_sigdetach(struct knote *kn)
4091 struct proc *p = kn->kn_ptr.p_proc;
4093 knlist_remove(p->p_klist, kn, 0);
4097 * signal knotes are shared with proc knotes, so we apply a mask to
4098 * the hint in order to differentiate them from process hints. This
4099 * could be avoided by using a signal-specific knote list, but probably
4100 * isn't worth the trouble.
4103 filt_signal(struct knote *kn, long hint)
4106 if (hint & NOTE_SIGNAL) {
4107 hint &= ~NOTE_SIGNAL;
4109 if (kn->kn_id == hint)
4112 return (kn->kn_data != 0);
4120 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4121 refcount_init(&ps->ps_refcnt, 1);
4122 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4127 sigacts_free(struct sigacts *ps)
4130 if (refcount_release(&ps->ps_refcnt) == 0)
4132 mtx_destroy(&ps->ps_mtx);
4133 free(ps, M_SUBPROC);
4137 sigacts_hold(struct sigacts *ps)
4140 refcount_acquire(&ps->ps_refcnt);
4145 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4148 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4149 mtx_lock(&src->ps_mtx);
4150 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4151 mtx_unlock(&src->ps_mtx);
4155 sigacts_shared(struct sigacts *ps)
4158 return (ps->ps_refcnt > 1);
4162 sig_drop_caught(struct proc *p)
4168 PROC_LOCK_ASSERT(p, MA_OWNED);
4169 mtx_assert(&ps->ps_mtx, MA_OWNED);
4170 SIG_FOREACH(sig, &ps->ps_sigcatch) {
4172 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4173 sigqueue_delete_proc(p, sig);
4178 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4183 * Prevent further fetches and SIGSEGVs, allowing thread to
4184 * issue syscalls despite corruption.
4186 sigfastblock_clear(td);
4190 ksiginfo_init_trap(&ksi);
4191 ksi.ksi_signo = SIGSEGV;
4192 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4193 ksi.ksi_addr = td->td_sigblock_ptr;
4194 trapsignal(td, &ksi);
4198 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4202 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4204 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4205 sigfastblock_failed(td, sendsig, false);
4209 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4214 sigfastblock_resched(struct thread *td, bool resched)
4221 reschedule_signals(p, td->td_sigmask, 0);
4225 td->td_flags |= TDF_ASTPENDING | TDF_NEEDSIGCHK;
4230 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4239 case SIGFASTBLOCK_SETPTR:
4240 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4244 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4248 td->td_pflags |= TDP_SIGFASTBLOCK;
4249 td->td_sigblock_ptr = uap->ptr;
4252 case SIGFASTBLOCK_UNBLOCK:
4253 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4259 res = casueword32(td->td_sigblock_ptr,
4260 SIGFASTBLOCK_PEND, &oldval, 0);
4263 sigfastblock_failed(td, false, true);
4269 if (oldval != SIGFASTBLOCK_PEND) {
4273 error = thread_check_susp(td, false);
4281 * td_sigblock_val is cleared there, but not on a
4282 * syscall exit. The end effect is that a single
4283 * interruptible sleep, while user sigblock word is
4284 * set, might return EINTR or ERESTART to usermode
4285 * without delivering signal. All further sleeps,
4286 * until userspace clears the word and does
4287 * sigfastblock(UNBLOCK), observe current word and no
4288 * longer get interrupted. It is slight
4289 * non-conformance, with alternative to have read the
4290 * sigblock word on each syscall entry.
4292 td->td_sigblock_val = 0;
4295 * Rely on normal ast mechanism to deliver pending
4296 * signals to current thread. But notify others about
4299 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4303 case SIGFASTBLOCK_UNSETPTR:
4304 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4308 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4312 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4316 sigfastblock_clear(td);
4327 sigfastblock_clear(struct thread *td)
4331 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4333 td->td_sigblock_val = 0;
4334 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4336 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4337 sigfastblock_resched(td, resched);
4341 sigfastblock_fetch(struct thread *td)
4345 (void)sigfastblock_fetch_sig(td, true, &val);
4349 sigfastblock_setpend1(struct thread *td)
4354 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4356 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4358 sigfastblock_failed(td, true, false);
4362 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4363 oldval | SIGFASTBLOCK_PEND);
4365 sigfastblock_failed(td, true, true);
4369 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4370 td->td_pflags &= ~TDP_SIGFASTPENDING;
4374 if (thread_check_susp(td, false) != 0)
4380 sigfastblock_setpend(struct thread *td, bool resched)
4384 sigfastblock_setpend1(td);
4388 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);