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_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/capsicum.h>
47 #include <sys/ctype.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
52 #include <sys/capsicum.h>
53 #include <sys/compressor.h>
54 #include <sys/condvar.h>
55 #include <sys/devctl.h>
56 #include <sys/event.h>
57 #include <sys/fcntl.h>
58 #include <sys/imgact.h>
59 #include <sys/kernel.h>
61 #include <sys/ktrace.h>
62 #include <sys/limits.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
66 #include <sys/refcount.h>
67 #include <sys/namei.h>
69 #include <sys/procdesc.h>
70 #include <sys/ptrace.h>
71 #include <sys/posix4.h>
72 #include <sys/racct.h>
73 #include <sys/resourcevar.h>
76 #include <sys/sleepqueue.h>
80 #include <sys/syscall.h>
81 #include <sys/syscallsubr.h>
82 #include <sys/sysctl.h>
83 #include <sys/sysent.h>
84 #include <sys/syslog.h>
85 #include <sys/sysproto.h>
86 #include <sys/timers.h>
87 #include <sys/unistd.h>
88 #include <sys/vmmeter.h>
91 #include <vm/vm_extern.h>
96 #include <machine/cpu.h>
98 #include <security/audit/audit.h>
100 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
102 SDT_PROVIDER_DECLARE(proc);
103 SDT_PROBE_DEFINE3(proc, , , signal__send,
104 "struct thread *", "struct proc *", "int");
105 SDT_PROBE_DEFINE2(proc, , , signal__clear,
106 "int", "ksiginfo_t *");
107 SDT_PROBE_DEFINE3(proc, , , signal__discard,
108 "struct thread *", "struct proc *", "int");
110 static int coredump(struct thread *);
111 static int killpg1(struct thread *td, int sig, int pgid, int all,
113 static int issignal(struct thread *td);
114 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
115 static int sigprop(int sig);
116 static void tdsigwakeup(struct thread *, int, sig_t, int);
117 static int sig_suspend_threads(struct thread *, struct proc *);
118 static int filt_sigattach(struct knote *kn);
119 static void filt_sigdetach(struct knote *kn);
120 static int filt_signal(struct knote *kn, long hint);
121 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
122 static void sigqueue_start(void);
123 static void sigfastblock_setpend(struct thread *td, bool resched);
125 static uma_zone_t ksiginfo_zone = NULL;
126 struct filterops sig_filtops = {
128 .f_attach = filt_sigattach,
129 .f_detach = filt_sigdetach,
130 .f_event = filt_signal,
133 static int kern_logsigexit = 1;
134 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
136 "Log processes quitting on abnormal signals to syslog(3)");
138 static int kern_forcesigexit = 1;
139 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
140 &kern_forcesigexit, 0, "Force trap signal to be handled");
142 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
143 "POSIX real time signal");
145 static int max_pending_per_proc = 128;
146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
147 &max_pending_per_proc, 0, "Max pending signals per proc");
149 static int preallocate_siginfo = 1024;
150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
151 &preallocate_siginfo, 0, "Preallocated signal memory size");
153 static int signal_overflow = 0;
154 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
155 &signal_overflow, 0, "Number of signals overflew");
157 static int signal_alloc_fail = 0;
158 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
159 &signal_alloc_fail, 0, "signals failed to be allocated");
161 static int kern_lognosys = 0;
162 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
163 "Log invalid syscalls");
165 __read_frequently bool sigfastblock_fetch_always = false;
166 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
167 &sigfastblock_fetch_always, 0,
168 "Fetch sigfastblock word on each syscall entry for proper "
169 "blocking semantic");
171 static bool kern_sig_discard_ign = true;
172 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
173 &kern_sig_discard_ign, 0,
174 "Discard ignored signals on delivery, otherwise queue them to "
177 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
180 * Policy -- Can ucred cr1 send SIGIO to process cr2?
181 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
182 * in the right situations.
184 #define CANSIGIO(cr1, cr2) \
185 ((cr1)->cr_uid == 0 || \
186 (cr1)->cr_ruid == (cr2)->cr_ruid || \
187 (cr1)->cr_uid == (cr2)->cr_ruid || \
188 (cr1)->cr_ruid == (cr2)->cr_uid || \
189 (cr1)->cr_uid == (cr2)->cr_uid)
191 static int sugid_coredump;
192 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
193 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
195 static int capmode_coredump;
196 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
197 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
199 static int do_coredump = 1;
200 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
201 &do_coredump, 0, "Enable/Disable coredumps");
203 static int set_core_nodump_flag = 0;
204 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
205 0, "Enable setting the NODUMP flag on coredump files");
207 static int coredump_devctl = 0;
208 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
209 0, "Generate a devctl notification when processes coredump");
212 * Signal properties and actions.
213 * The array below categorizes the signals and their default actions
214 * according to the following properties:
216 #define SIGPROP_KILL 0x01 /* terminates process by default */
217 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
218 #define SIGPROP_STOP 0x04 /* suspend process */
219 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
220 #define SIGPROP_IGNORE 0x10 /* ignore by default */
221 #define SIGPROP_CONT 0x20 /* continue if suspended */
223 static int sigproptbl[NSIG] = {
224 [SIGHUP] = SIGPROP_KILL,
225 [SIGINT] = SIGPROP_KILL,
226 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
227 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
228 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
229 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
230 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
231 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
232 [SIGKILL] = SIGPROP_KILL,
233 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
234 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
235 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
236 [SIGPIPE] = SIGPROP_KILL,
237 [SIGALRM] = SIGPROP_KILL,
238 [SIGTERM] = SIGPROP_KILL,
239 [SIGURG] = SIGPROP_IGNORE,
240 [SIGSTOP] = SIGPROP_STOP,
241 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
242 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
243 [SIGCHLD] = SIGPROP_IGNORE,
244 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
245 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
246 [SIGIO] = SIGPROP_IGNORE,
247 [SIGXCPU] = SIGPROP_KILL,
248 [SIGXFSZ] = SIGPROP_KILL,
249 [SIGVTALRM] = SIGPROP_KILL,
250 [SIGPROF] = SIGPROP_KILL,
251 [SIGWINCH] = SIGPROP_IGNORE,
252 [SIGINFO] = SIGPROP_IGNORE,
253 [SIGUSR1] = SIGPROP_KILL,
254 [SIGUSR2] = SIGPROP_KILL,
257 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
261 int __sig = ffs(__bits); \
262 __bits &= ~(1u << (__sig - 1)); \
263 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
267 if (++__i == _SIG_WORDS) { \
271 __bits = (set)->__bits[__i]; \
276 #define SIG_FOREACH(i, set) \
277 for (int32_t __i = -1, __bits = 0; \
278 _SIG_FOREACH_ADVANCE(i, set); ) \
280 static sigset_t fastblock_mask;
283 ast_sig(struct thread *td, int tda)
286 int old_boundary, sig;
292 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
293 TDAI(TDA_AST))) == 0) {
297 * Note that TDA_SIG should be re-read from
298 * td_ast, since signal might have been delivered
299 * after we cleared td_flags above. This is one of
300 * the reason for looping check for AST condition.
301 * See comment in userret() about P_PPWAIT.
303 if ((p->p_flag & P_PPWAIT) == 0 &&
304 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
305 if (SIGPENDING(td) && ((tda | td->td_ast) &
306 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
307 thread_unlock(td); /* fix dumps */
309 "failed2 to set signal flags for ast p %p "
310 "td %p tda %#x td_ast %#x fl %#x",
311 p, td, tda, td->td_ast, td->td_flags);
320 * Check for signals. Unlocked reads of p_pendingcnt or
321 * p_siglist might cause process-directed signal to be handled
324 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
325 !SIGISEMPTY(p->p_siglist)) {
326 sigfastblock_fetch(td);
328 old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY);
329 td->td_dbgflags |= TDB_BOUNDARY;
330 mtx_lock(&p->p_sigacts->ps_mtx);
331 while ((sig = cursig(td)) != 0) {
332 KASSERT(sig >= 0, ("sig %d", sig));
335 mtx_unlock(&p->p_sigacts->ps_mtx);
336 td->td_dbgflags &= old_boundary;
340 resched_sigs = false;
344 * Handle deferred update of the fast sigblock value, after
345 * the postsig() loop was performed.
347 sigfastblock_setpend(td, resched_sigs);
351 ast_sigsuspend(struct thread *td, int tda __unused)
353 MPASS((td->td_pflags & TDP_OLDMASK) != 0);
354 td->td_pflags &= ~TDP_OLDMASK;
355 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
361 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
362 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
363 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
364 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
365 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
366 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
367 SIGFILLSET(fastblock_mask);
368 SIG_CANTMASK(fastblock_mask);
369 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
370 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
371 TDP_OLDMASK, ast_sigsuspend);
375 ksiginfo_alloc(int mwait)
377 MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
379 if (ksiginfo_zone == NULL)
381 return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
385 ksiginfo_free(ksiginfo_t *ksi)
387 uma_zfree(ksiginfo_zone, ksi);
391 ksiginfo_tryfree(ksiginfo_t *ksi)
393 if ((ksi->ksi_flags & KSI_EXT) == 0) {
394 uma_zfree(ksiginfo_zone, ksi);
401 sigqueue_init(sigqueue_t *list, struct proc *p)
403 SIGEMPTYSET(list->sq_signals);
404 SIGEMPTYSET(list->sq_kill);
405 SIGEMPTYSET(list->sq_ptrace);
406 TAILQ_INIT(&list->sq_list);
408 list->sq_flags = SQ_INIT;
412 * Get a signal's ksiginfo.
414 * 0 - signal not found
415 * others - signal number
418 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
420 struct proc *p = sq->sq_proc;
421 struct ksiginfo *ksi, *next;
424 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
426 if (!SIGISMEMBER(sq->sq_signals, signo))
429 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
431 SIGDELSET(sq->sq_ptrace, signo);
432 si->ksi_flags |= KSI_PTRACE;
434 if (SIGISMEMBER(sq->sq_kill, signo)) {
437 SIGDELSET(sq->sq_kill, signo);
440 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
441 if (ksi->ksi_signo == signo) {
443 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
444 ksi->ksi_sigq = NULL;
445 ksiginfo_copy(ksi, si);
446 if (ksiginfo_tryfree(ksi) && p != NULL)
455 SIGDELSET(sq->sq_signals, signo);
456 si->ksi_signo = signo;
461 sigqueue_take(ksiginfo_t *ksi)
467 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
471 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
472 ksi->ksi_sigq = NULL;
473 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
476 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
477 kp = TAILQ_NEXT(kp, ksi_link)) {
478 if (kp->ksi_signo == ksi->ksi_signo)
481 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
482 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
483 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
487 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
489 struct proc *p = sq->sq_proc;
490 struct ksiginfo *ksi;
493 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
496 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
499 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
500 SIGADDSET(sq->sq_kill, signo);
504 /* directly insert the ksi, don't copy it */
505 if (si->ksi_flags & KSI_INS) {
506 if (si->ksi_flags & KSI_HEAD)
507 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
509 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
514 if (__predict_false(ksiginfo_zone == NULL)) {
515 SIGADDSET(sq->sq_kill, signo);
519 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
522 } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
528 ksiginfo_copy(si, ksi);
529 ksi->ksi_signo = signo;
530 if (si->ksi_flags & KSI_HEAD)
531 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
533 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
538 if ((si->ksi_flags & KSI_PTRACE) != 0) {
539 SIGADDSET(sq->sq_ptrace, signo);
542 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
543 (si->ksi_flags & KSI_SIGQ) == 0) {
544 SIGADDSET(sq->sq_kill, signo);
552 SIGADDSET(sq->sq_signals, signo);
557 sigqueue_flush(sigqueue_t *sq)
559 struct proc *p = sq->sq_proc;
562 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
565 PROC_LOCK_ASSERT(p, MA_OWNED);
567 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
568 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
569 ksi->ksi_sigq = NULL;
570 if (ksiginfo_tryfree(ksi) && p != NULL)
574 SIGEMPTYSET(sq->sq_signals);
575 SIGEMPTYSET(sq->sq_kill);
576 SIGEMPTYSET(sq->sq_ptrace);
580 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
583 struct proc *p1, *p2;
584 ksiginfo_t *ksi, *next;
586 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
587 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
590 /* Move siginfo to target list */
591 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
592 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
593 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
596 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
603 /* Move pending bits to target list */
605 SIGSETAND(tmp, *set);
606 SIGSETOR(dst->sq_kill, tmp);
607 SIGSETNAND(src->sq_kill, tmp);
609 tmp = src->sq_ptrace;
610 SIGSETAND(tmp, *set);
611 SIGSETOR(dst->sq_ptrace, tmp);
612 SIGSETNAND(src->sq_ptrace, tmp);
614 tmp = src->sq_signals;
615 SIGSETAND(tmp, *set);
616 SIGSETOR(dst->sq_signals, tmp);
617 SIGSETNAND(src->sq_signals, tmp);
622 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
627 SIGADDSET(set, signo);
628 sigqueue_move_set(src, dst, &set);
633 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
635 struct proc *p = sq->sq_proc;
636 ksiginfo_t *ksi, *next;
638 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
640 /* Remove siginfo queue */
641 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
642 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
643 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
644 ksi->ksi_sigq = NULL;
645 if (ksiginfo_tryfree(ksi) && p != NULL)
649 SIGSETNAND(sq->sq_kill, *set);
650 SIGSETNAND(sq->sq_ptrace, *set);
651 SIGSETNAND(sq->sq_signals, *set);
655 sigqueue_delete(sigqueue_t *sq, int signo)
660 SIGADDSET(set, signo);
661 sigqueue_delete_set(sq, &set);
664 /* Remove a set of signals for a process */
666 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
671 PROC_LOCK_ASSERT(p, MA_OWNED);
673 sigqueue_init(&worklist, NULL);
674 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
676 FOREACH_THREAD_IN_PROC(p, td0)
677 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
679 sigqueue_flush(&worklist);
683 sigqueue_delete_proc(struct proc *p, int signo)
688 SIGADDSET(set, signo);
689 sigqueue_delete_set_proc(p, &set);
693 sigqueue_delete_stopmask_proc(struct proc *p)
698 SIGADDSET(set, SIGSTOP);
699 SIGADDSET(set, SIGTSTP);
700 SIGADDSET(set, SIGTTIN);
701 SIGADDSET(set, SIGTTOU);
702 sigqueue_delete_set_proc(p, &set);
706 * Determine signal that should be delivered to thread td, the current
707 * thread, 0 if none. If there is a pending stop signal with default
708 * action, the process stops in issignal().
711 cursig(struct thread *td)
713 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
714 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
715 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
716 return (SIGPENDING(td) ? issignal(td) : 0);
720 * Arrange for ast() to handle unmasked pending signals on return to user
721 * mode. This must be called whenever a signal is added to td_sigqueue or
722 * unmasked in td_sigmask.
725 signotify(struct thread *td)
728 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
731 ast_sched(td, TDA_SIG);
735 * Returns 1 (true) if altstack is configured for the thread, and the
736 * passed stack bottom address falls into the altstack range. Handles
737 * the 43 compat special case where the alt stack size is zero.
740 sigonstack(size_t sp)
745 if ((td->td_pflags & TDP_ALTSTACK) == 0)
747 #if defined(COMPAT_43)
748 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
749 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
751 return (sp >= (size_t)td->td_sigstk.ss_sp &&
752 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
759 if (sig > 0 && sig < nitems(sigproptbl))
760 return (sigproptbl[sig]);
765 sigact_flag_test(const struct sigaction *act, int flag)
769 * SA_SIGINFO is reset when signal disposition is set to
770 * ignore or default. Other flags are kept according to user
773 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
774 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
775 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
785 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
786 struct sigaction *oact, int flags)
789 struct proc *p = td->td_proc;
791 if (!_SIG_VALID(sig))
793 if (act != NULL && act->sa_handler != SIG_DFL &&
794 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
795 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
796 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
801 mtx_lock(&ps->ps_mtx);
803 memset(oact, 0, sizeof(*oact));
804 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
805 if (SIGISMEMBER(ps->ps_sigonstack, sig))
806 oact->sa_flags |= SA_ONSTACK;
807 if (!SIGISMEMBER(ps->ps_sigintr, sig))
808 oact->sa_flags |= SA_RESTART;
809 if (SIGISMEMBER(ps->ps_sigreset, sig))
810 oact->sa_flags |= SA_RESETHAND;
811 if (SIGISMEMBER(ps->ps_signodefer, sig))
812 oact->sa_flags |= SA_NODEFER;
813 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
814 oact->sa_flags |= SA_SIGINFO;
816 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
818 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
819 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
820 oact->sa_flags |= SA_NOCLDSTOP;
821 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
822 oact->sa_flags |= SA_NOCLDWAIT;
825 if ((sig == SIGKILL || sig == SIGSTOP) &&
826 act->sa_handler != SIG_DFL) {
827 mtx_unlock(&ps->ps_mtx);
833 * Change setting atomically.
836 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
837 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
838 if (sigact_flag_test(act, SA_SIGINFO)) {
839 ps->ps_sigact[_SIG_IDX(sig)] =
840 (__sighandler_t *)act->sa_sigaction;
841 SIGADDSET(ps->ps_siginfo, sig);
843 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
844 SIGDELSET(ps->ps_siginfo, sig);
846 if (!sigact_flag_test(act, SA_RESTART))
847 SIGADDSET(ps->ps_sigintr, sig);
849 SIGDELSET(ps->ps_sigintr, sig);
850 if (sigact_flag_test(act, SA_ONSTACK))
851 SIGADDSET(ps->ps_sigonstack, sig);
853 SIGDELSET(ps->ps_sigonstack, sig);
854 if (sigact_flag_test(act, SA_RESETHAND))
855 SIGADDSET(ps->ps_sigreset, sig);
857 SIGDELSET(ps->ps_sigreset, sig);
858 if (sigact_flag_test(act, SA_NODEFER))
859 SIGADDSET(ps->ps_signodefer, sig);
861 SIGDELSET(ps->ps_signodefer, sig);
862 if (sig == SIGCHLD) {
863 if (act->sa_flags & SA_NOCLDSTOP)
864 ps->ps_flag |= PS_NOCLDSTOP;
866 ps->ps_flag &= ~PS_NOCLDSTOP;
867 if (act->sa_flags & SA_NOCLDWAIT) {
869 * Paranoia: since SA_NOCLDWAIT is implemented
870 * by reparenting the dying child to PID 1 (and
871 * trust it to reap the zombie), PID 1 itself
872 * is forbidden to set SA_NOCLDWAIT.
875 ps->ps_flag &= ~PS_NOCLDWAIT;
877 ps->ps_flag |= PS_NOCLDWAIT;
879 ps->ps_flag &= ~PS_NOCLDWAIT;
880 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
881 ps->ps_flag |= PS_CLDSIGIGN;
883 ps->ps_flag &= ~PS_CLDSIGIGN;
886 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
887 * and for signals set to SIG_DFL where the default is to
888 * ignore. However, don't put SIGCONT in ps_sigignore, as we
889 * have to restart the process.
891 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
892 (sigprop(sig) & SIGPROP_IGNORE &&
893 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
894 /* never to be seen again */
895 sigqueue_delete_proc(p, sig);
897 /* easier in psignal */
898 SIGADDSET(ps->ps_sigignore, sig);
899 SIGDELSET(ps->ps_sigcatch, sig);
901 SIGDELSET(ps->ps_sigignore, sig);
902 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
903 SIGDELSET(ps->ps_sigcatch, sig);
905 SIGADDSET(ps->ps_sigcatch, sig);
907 #ifdef COMPAT_FREEBSD4
908 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
909 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
910 (flags & KSA_FREEBSD4) == 0)
911 SIGDELSET(ps->ps_freebsd4, sig);
913 SIGADDSET(ps->ps_freebsd4, sig);
916 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
917 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
918 (flags & KSA_OSIGSET) == 0)
919 SIGDELSET(ps->ps_osigset, sig);
921 SIGADDSET(ps->ps_osigset, sig);
924 mtx_unlock(&ps->ps_mtx);
929 #ifndef _SYS_SYSPROTO_H_
930 struct sigaction_args {
932 struct sigaction *act;
933 struct sigaction *oact;
937 sys_sigaction(struct thread *td, struct sigaction_args *uap)
939 struct sigaction act, oact;
940 struct sigaction *actp, *oactp;
943 actp = (uap->act != NULL) ? &act : NULL;
944 oactp = (uap->oact != NULL) ? &oact : NULL;
946 error = copyin(uap->act, actp, sizeof(act));
950 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
952 error = copyout(oactp, uap->oact, sizeof(oact));
956 #ifdef COMPAT_FREEBSD4
957 #ifndef _SYS_SYSPROTO_H_
958 struct freebsd4_sigaction_args {
960 struct sigaction *act;
961 struct sigaction *oact;
965 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
967 struct sigaction act, oact;
968 struct sigaction *actp, *oactp;
971 actp = (uap->act != NULL) ? &act : NULL;
972 oactp = (uap->oact != NULL) ? &oact : NULL;
974 error = copyin(uap->act, actp, sizeof(act));
978 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
980 error = copyout(oactp, uap->oact, sizeof(oact));
983 #endif /* COMAPT_FREEBSD4 */
985 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
986 #ifndef _SYS_SYSPROTO_H_
987 struct osigaction_args {
989 struct osigaction *nsa;
990 struct osigaction *osa;
994 osigaction(struct thread *td, struct osigaction_args *uap)
996 struct osigaction sa;
997 struct sigaction nsa, osa;
998 struct sigaction *nsap, *osap;
1001 if (uap->signum <= 0 || uap->signum >= ONSIG)
1004 nsap = (uap->nsa != NULL) ? &nsa : NULL;
1005 osap = (uap->osa != NULL) ? &osa : NULL;
1008 error = copyin(uap->nsa, &sa, sizeof(sa));
1011 nsap->sa_handler = sa.sa_handler;
1012 nsap->sa_flags = sa.sa_flags;
1013 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
1015 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1016 if (osap && !error) {
1017 sa.sa_handler = osap->sa_handler;
1018 sa.sa_flags = osap->sa_flags;
1019 SIG2OSIG(osap->sa_mask, sa.sa_mask);
1020 error = copyout(&sa, uap->osa, sizeof(sa));
1025 #if !defined(__i386__)
1026 /* Avoid replicating the same stub everywhere */
1028 osigreturn(struct thread *td, struct osigreturn_args *uap)
1031 return (nosys(td, (struct nosys_args *)uap));
1034 #endif /* COMPAT_43 */
1037 * Initialize signal state for process 0;
1038 * set to ignore signals that are ignored by default.
1041 siginit(struct proc *p)
1048 mtx_lock(&ps->ps_mtx);
1049 for (i = 1; i <= NSIG; i++) {
1050 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
1051 SIGADDSET(ps->ps_sigignore, i);
1054 mtx_unlock(&ps->ps_mtx);
1059 * Reset specified signal to the default disposition.
1062 sigdflt(struct sigacts *ps, int sig)
1065 mtx_assert(&ps->ps_mtx, MA_OWNED);
1066 SIGDELSET(ps->ps_sigcatch, sig);
1067 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1068 SIGADDSET(ps->ps_sigignore, sig);
1069 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1070 SIGDELSET(ps->ps_siginfo, sig);
1074 * Reset signals for an exec of the specified process.
1077 execsigs(struct proc *p)
1083 * Reset caught signals. Held signals remain held
1084 * through td_sigmask (unless they were caught,
1085 * and are now ignored by default).
1087 PROC_LOCK_ASSERT(p, MA_OWNED);
1089 mtx_lock(&ps->ps_mtx);
1093 * Reset stack state to the user stack.
1094 * Clear set of signals caught on the signal stack.
1097 MPASS(td->td_proc == p);
1098 td->td_sigstk.ss_flags = SS_DISABLE;
1099 td->td_sigstk.ss_size = 0;
1100 td->td_sigstk.ss_sp = 0;
1101 td->td_pflags &= ~TDP_ALTSTACK;
1103 * Reset no zombies if child dies flag as Solaris does.
1105 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1106 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1107 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1108 mtx_unlock(&ps->ps_mtx);
1112 * kern_sigprocmask()
1114 * Manipulate signal mask.
1117 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1120 sigset_t new_block, oset1;
1125 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1126 PROC_LOCK_ASSERT(p, MA_OWNED);
1129 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1130 ? MA_OWNED : MA_NOTOWNED);
1132 *oset = td->td_sigmask;
1139 oset1 = td->td_sigmask;
1140 SIGSETOR(td->td_sigmask, *set);
1141 new_block = td->td_sigmask;
1142 SIGSETNAND(new_block, oset1);
1145 SIGSETNAND(td->td_sigmask, *set);
1150 oset1 = td->td_sigmask;
1151 if (flags & SIGPROCMASK_OLD)
1152 SIGSETLO(td->td_sigmask, *set);
1154 td->td_sigmask = *set;
1155 new_block = td->td_sigmask;
1156 SIGSETNAND(new_block, oset1);
1165 * The new_block set contains signals that were not previously
1166 * blocked, but are blocked now.
1168 * In case we block any signal that was not previously blocked
1169 * for td, and process has the signal pending, try to schedule
1170 * signal delivery to some thread that does not block the
1171 * signal, possibly waking it up.
1173 if (p->p_numthreads != 1)
1174 reschedule_signals(p, new_block, flags);
1178 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1183 #ifndef _SYS_SYSPROTO_H_
1184 struct sigprocmask_args {
1186 const sigset_t *set;
1191 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1194 sigset_t *setp, *osetp;
1197 setp = (uap->set != NULL) ? &set : NULL;
1198 osetp = (uap->oset != NULL) ? &oset : NULL;
1200 error = copyin(uap->set, setp, sizeof(set));
1204 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1205 if (osetp && !error) {
1206 error = copyout(osetp, uap->oset, sizeof(oset));
1211 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1212 #ifndef _SYS_SYSPROTO_H_
1213 struct osigprocmask_args {
1219 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1224 OSIG2SIG(uap->mask, set);
1225 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1226 SIG2OSIG(oset, td->td_retval[0]);
1229 #endif /* COMPAT_43 */
1232 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1238 error = copyin(uap->set, &set, sizeof(set));
1240 td->td_retval[0] = error;
1244 error = kern_sigtimedwait(td, set, &ksi, NULL);
1247 * sigwait() function shall not return EINTR, but
1248 * the syscall does. Non-ancient libc provides the
1249 * wrapper which hides EINTR. Otherwise, EINTR return
1250 * is used by libthr to handle required cancellation
1251 * point in the sigwait().
1253 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1255 td->td_retval[0] = error;
1259 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1260 td->td_retval[0] = error;
1265 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1268 struct timespec *timeout;
1274 error = copyin(uap->timeout, &ts, sizeof(ts));
1282 error = copyin(uap->set, &set, sizeof(set));
1286 error = kern_sigtimedwait(td, set, &ksi, timeout);
1291 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1294 td->td_retval[0] = ksi.ksi_signo;
1299 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1305 error = copyin(uap->set, &set, sizeof(set));
1309 error = kern_sigtimedwait(td, set, &ksi, NULL);
1314 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1317 td->td_retval[0] = ksi.ksi_signo;
1322 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1326 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1330 thr->td_si.si_signo = 0;
1335 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1336 struct timespec *timeout)
1339 sigset_t saved_mask, new_block;
1341 int error, sig, timevalid = 0;
1342 sbintime_t sbt, precision, tsbt;
1350 /* Ensure the sigfastblock value is up to date. */
1351 sigfastblock_fetch(td);
1353 if (timeout != NULL) {
1354 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1357 if (ts.tv_sec < INT32_MAX / 2) {
1360 precision >>= tc_precexp;
1361 if (TIMESEL(&sbt, tsbt))
1365 precision = sbt = 0;
1368 precision = sbt = 0;
1370 /* Some signals can not be waited for. */
1371 SIG_CANTMASK(waitset);
1374 saved_mask = td->td_sigmask;
1375 SIGSETNAND(td->td_sigmask, waitset);
1376 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1377 !kern_sig_discard_ign) {
1379 td->td_flags |= TDF_SIGWAIT;
1383 mtx_lock(&ps->ps_mtx);
1385 mtx_unlock(&ps->ps_mtx);
1386 KASSERT(sig >= 0, ("sig %d", sig));
1387 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1388 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1389 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1399 * POSIX says this must be checked after looking for pending
1402 if (timeout != NULL && !timevalid) {
1412 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1413 "sigwait", sbt, precision, C_ABSOLUTE);
1415 /* The syscalls can not be restarted. */
1416 if (error == ERESTART)
1420 * If PTRACE_SCE or PTRACE_SCX were set after
1421 * userspace entered the syscall, return spurious
1422 * EINTR after wait was done. Only do this as last
1423 * resort after rechecking for possible queued signals
1424 * and expired timeouts.
1426 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1430 td->td_flags &= ~TDF_SIGWAIT;
1433 new_block = saved_mask;
1434 SIGSETNAND(new_block, td->td_sigmask);
1435 td->td_sigmask = saved_mask;
1437 * Fewer signals can be delivered to us, reschedule signal
1440 if (p->p_numthreads != 1)
1441 reschedule_signals(p, new_block, 0);
1444 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1446 if (ksi->ksi_code == SI_TIMER)
1447 itimer_accept(p, ksi->ksi_timerid, ksi);
1450 if (KTRPOINT(td, KTR_PSIG)) {
1453 mtx_lock(&ps->ps_mtx);
1454 action = ps->ps_sigact[_SIG_IDX(sig)];
1455 mtx_unlock(&ps->ps_mtx);
1456 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1459 if (sig == SIGKILL) {
1460 proc_td_siginfo_capture(td, &ksi->ksi_info);
1468 #ifndef _SYS_SYSPROTO_H_
1469 struct sigpending_args {
1474 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1476 struct proc *p = td->td_proc;
1480 pending = p->p_sigqueue.sq_signals;
1481 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1483 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1486 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1487 #ifndef _SYS_SYSPROTO_H_
1488 struct osigpending_args {
1493 osigpending(struct thread *td, struct osigpending_args *uap)
1495 struct proc *p = td->td_proc;
1499 pending = p->p_sigqueue.sq_signals;
1500 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1502 SIG2OSIG(pending, td->td_retval[0]);
1505 #endif /* COMPAT_43 */
1507 #if defined(COMPAT_43)
1509 * Generalized interface signal handler, 4.3-compatible.
1511 #ifndef _SYS_SYSPROTO_H_
1512 struct osigvec_args {
1520 osigvec(struct thread *td, struct osigvec_args *uap)
1523 struct sigaction nsa, osa;
1524 struct sigaction *nsap, *osap;
1527 if (uap->signum <= 0 || uap->signum >= ONSIG)
1529 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1530 osap = (uap->osv != NULL) ? &osa : NULL;
1532 error = copyin(uap->nsv, &vec, sizeof(vec));
1535 nsap->sa_handler = vec.sv_handler;
1536 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1537 nsap->sa_flags = vec.sv_flags;
1538 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1540 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1541 if (osap && !error) {
1542 vec.sv_handler = osap->sa_handler;
1543 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1544 vec.sv_flags = osap->sa_flags;
1545 vec.sv_flags &= ~SA_NOCLDWAIT;
1546 vec.sv_flags ^= SA_RESTART;
1547 error = copyout(&vec, uap->osv, sizeof(vec));
1552 #ifndef _SYS_SYSPROTO_H_
1553 struct osigblock_args {
1558 osigblock(struct thread *td, struct osigblock_args *uap)
1562 OSIG2SIG(uap->mask, set);
1563 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1564 SIG2OSIG(oset, td->td_retval[0]);
1568 #ifndef _SYS_SYSPROTO_H_
1569 struct osigsetmask_args {
1574 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1578 OSIG2SIG(uap->mask, set);
1579 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1580 SIG2OSIG(oset, td->td_retval[0]);
1583 #endif /* COMPAT_43 */
1586 * Suspend calling thread until signal, providing mask to be set in the
1589 #ifndef _SYS_SYSPROTO_H_
1590 struct sigsuspend_args {
1591 const sigset_t *sigmask;
1596 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1601 error = copyin(uap->sigmask, &mask, sizeof(mask));
1604 return (kern_sigsuspend(td, mask));
1608 kern_sigsuspend(struct thread *td, sigset_t mask)
1610 struct proc *p = td->td_proc;
1613 /* Ensure the sigfastblock value is up to date. */
1614 sigfastblock_fetch(td);
1617 * When returning from sigsuspend, we want
1618 * the old mask to be restored after the
1619 * signal handler has finished. Thus, we
1620 * save it here and mark the sigacts structure
1624 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1625 SIGPROCMASK_PROC_LOCKED);
1626 td->td_pflags |= TDP_OLDMASK;
1627 ast_sched(td, TDA_SIGSUSPEND);
1630 * Process signals now. Otherwise, we can get spurious wakeup
1631 * due to signal entered process queue, but delivered to other
1632 * thread. But sigsuspend should return only on signal
1635 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1636 for (has_sig = 0; !has_sig;) {
1637 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1640 thread_suspend_check(0);
1641 mtx_lock(&p->p_sigacts->ps_mtx);
1642 while ((sig = cursig(td)) != 0) {
1643 KASSERT(sig >= 0, ("sig %d", sig));
1644 has_sig += postsig(sig);
1646 mtx_unlock(&p->p_sigacts->ps_mtx);
1649 * If PTRACE_SCE or PTRACE_SCX were set after
1650 * userspace entered the syscall, return spurious
1653 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1657 td->td_errno = EINTR;
1658 td->td_pflags |= TDP_NERRNO;
1659 return (EJUSTRETURN);
1662 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1664 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1665 * convention: libc stub passes mask, not pointer, to save a copyin.
1667 #ifndef _SYS_SYSPROTO_H_
1668 struct osigsuspend_args {
1674 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1678 OSIG2SIG(uap->mask, mask);
1679 return (kern_sigsuspend(td, mask));
1681 #endif /* COMPAT_43 */
1683 #if defined(COMPAT_43)
1684 #ifndef _SYS_SYSPROTO_H_
1685 struct osigstack_args {
1686 struct sigstack *nss;
1687 struct sigstack *oss;
1692 osigstack(struct thread *td, struct osigstack_args *uap)
1694 struct sigstack nss, oss;
1697 if (uap->nss != NULL) {
1698 error = copyin(uap->nss, &nss, sizeof(nss));
1702 oss.ss_sp = td->td_sigstk.ss_sp;
1703 oss.ss_onstack = sigonstack(cpu_getstack(td));
1704 if (uap->nss != NULL) {
1705 td->td_sigstk.ss_sp = nss.ss_sp;
1706 td->td_sigstk.ss_size = 0;
1707 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1708 td->td_pflags |= TDP_ALTSTACK;
1710 if (uap->oss != NULL)
1711 error = copyout(&oss, uap->oss, sizeof(oss));
1715 #endif /* COMPAT_43 */
1717 #ifndef _SYS_SYSPROTO_H_
1718 struct sigaltstack_args {
1725 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1730 if (uap->ss != NULL) {
1731 error = copyin(uap->ss, &ss, sizeof(ss));
1735 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1736 (uap->oss != NULL) ? &oss : NULL);
1739 if (uap->oss != NULL)
1740 error = copyout(&oss, uap->oss, sizeof(stack_t));
1745 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1747 struct proc *p = td->td_proc;
1750 oonstack = sigonstack(cpu_getstack(td));
1753 *oss = td->td_sigstk;
1754 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1755 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1761 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1763 if (!(ss->ss_flags & SS_DISABLE)) {
1764 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1767 td->td_sigstk = *ss;
1768 td->td_pflags |= TDP_ALTSTACK;
1770 td->td_pflags &= ~TDP_ALTSTACK;
1776 struct killpg1_ctx {
1786 killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
1790 err = p_cansignal(arg->td, p, arg->sig);
1791 if (err == 0 && arg->sig != 0)
1792 pksignal(p, arg->sig, arg->ksi);
1797 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1802 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1805 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1806 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1810 killpg1_sendsig_locked(p, arg);
1815 kill_processes_prison_cb(struct proc *p, void *arg)
1817 struct killpg1_ctx *ctx = arg;
1819 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1820 (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
1823 killpg1_sendsig_locked(p, ctx);
1827 * Common code for kill process group/broadcast kill.
1828 * cp is calling process.
1831 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1835 struct killpg1_ctx arg;
1847 prison_proc_iterate(td->td_ucred->cr_prison,
1848 kill_processes_prison_cb, &arg);
1850 sx_slock(&proctree_lock);
1853 * zero pgid means send to my process group.
1855 pgrp = td->td_proc->p_pgrp;
1858 pgrp = pgfind(pgid);
1860 sx_sunlock(&proctree_lock);
1864 sx_sunlock(&proctree_lock);
1865 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1866 killpg1_sendsig(p, false, &arg);
1870 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1871 if (arg.ret == 0 && !arg.sent)
1872 arg.ret = arg.found ? EPERM : ESRCH;
1876 #ifndef _SYS_SYSPROTO_H_
1884 sys_kill(struct thread *td, struct kill_args *uap)
1887 return (kern_kill(td, uap->pid, uap->signum));
1891 kern_kill(struct thread *td, pid_t pid, int signum)
1898 * A process in capability mode can send signals only to himself.
1899 * The main rationale behind this is that abort(3) is implemented as
1900 * kill(getpid(), SIGABRT).
1902 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1905 AUDIT_ARG_SIGNUM(signum);
1907 if ((u_int)signum > _SIG_MAXSIG)
1910 ksiginfo_init(&ksi);
1911 ksi.ksi_signo = signum;
1912 ksi.ksi_code = SI_USER;
1913 ksi.ksi_pid = td->td_proc->p_pid;
1914 ksi.ksi_uid = td->td_ucred->cr_ruid;
1917 /* kill single process */
1918 if ((p = pfind_any(pid)) == NULL)
1920 AUDIT_ARG_PROCESS(p);
1921 error = p_cansignal(td, p, signum);
1922 if (error == 0 && signum)
1923 pksignal(p, signum, &ksi);
1928 case -1: /* broadcast signal */
1929 return (killpg1(td, signum, 0, 1, &ksi));
1930 case 0: /* signal own process group */
1931 return (killpg1(td, signum, 0, 0, &ksi));
1932 default: /* negative explicit process group */
1933 return (killpg1(td, signum, -pid, 0, &ksi));
1939 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1944 AUDIT_ARG_SIGNUM(uap->signum);
1945 AUDIT_ARG_FD(uap->fd);
1946 if ((u_int)uap->signum > _SIG_MAXSIG)
1949 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1952 AUDIT_ARG_PROCESS(p);
1953 error = p_cansignal(td, p, uap->signum);
1954 if (error == 0 && uap->signum)
1955 kern_psignal(p, uap->signum);
1960 #if defined(COMPAT_43)
1961 #ifndef _SYS_SYSPROTO_H_
1962 struct okillpg_args {
1969 okillpg(struct thread *td, struct okillpg_args *uap)
1973 AUDIT_ARG_SIGNUM(uap->signum);
1974 AUDIT_ARG_PID(uap->pgid);
1975 if ((u_int)uap->signum > _SIG_MAXSIG)
1978 ksiginfo_init(&ksi);
1979 ksi.ksi_signo = uap->signum;
1980 ksi.ksi_code = SI_USER;
1981 ksi.ksi_pid = td->td_proc->p_pid;
1982 ksi.ksi_uid = td->td_ucred->cr_ruid;
1983 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1985 #endif /* COMPAT_43 */
1987 #ifndef _SYS_SYSPROTO_H_
1988 struct sigqueue_args {
1991 /* union sigval */ void *value;
1995 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1999 sv.sival_ptr = uap->value;
2001 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2005 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
2011 if ((u_int)signum > _SIG_MAXSIG)
2015 * Specification says sigqueue can only send signal to
2021 if ((p = pfind_any(pid)) == NULL)
2023 error = p_cansignal(td, p, signum);
2024 if (error == 0 && signum != 0) {
2025 ksiginfo_init(&ksi);
2026 ksi.ksi_flags = KSI_SIGQ;
2027 ksi.ksi_signo = signum;
2028 ksi.ksi_code = SI_QUEUE;
2029 ksi.ksi_pid = td->td_proc->p_pid;
2030 ksi.ksi_uid = td->td_ucred->cr_ruid;
2031 ksi.ksi_value = *value;
2032 error = pksignal(p, ksi.ksi_signo, &ksi);
2039 * Send a signal to a process group.
2042 gsignal(int pgid, int sig, ksiginfo_t *ksi)
2047 sx_slock(&proctree_lock);
2048 pgrp = pgfind(pgid);
2049 sx_sunlock(&proctree_lock);
2051 pgsignal(pgrp, sig, 0, ksi);
2058 * Send a signal to a process group. If checktty is 1,
2059 * limit to members which have a controlling terminal.
2062 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2067 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2068 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2070 if (p->p_state == PRS_NORMAL &&
2071 (checkctty == 0 || p->p_flag & P_CONTROLT))
2072 pksignal(p, sig, ksi);
2079 * Recalculate the signal mask and reset the signal disposition after
2080 * usermode frame for delivery is formed. Should be called after
2081 * mach-specific routine, because sysent->sv_sendsig() needs correct
2082 * ps_siginfo and signal mask.
2085 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2089 mtx_assert(&ps->ps_mtx, MA_OWNED);
2090 td->td_ru.ru_nsignals++;
2091 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2092 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2093 SIGADDSET(mask, sig);
2094 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2095 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2096 if (SIGISMEMBER(ps->ps_sigreset, sig))
2101 * Send a signal caused by a trap to the current thread. If it will be
2102 * caught immediately, deliver it with correct code. Otherwise, post it
2106 trapsignal(struct thread *td, ksiginfo_t *ksi)
2114 sig = ksi->ksi_signo;
2115 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2117 sigfastblock_fetch(td);
2120 mtx_lock(&ps->ps_mtx);
2121 sigmask = td->td_sigmask;
2122 if (td->td_sigblock_val != 0)
2123 SIGSETOR(sigmask, fastblock_mask);
2124 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2125 !SIGISMEMBER(sigmask, sig)) {
2127 if (KTRPOINT(curthread, KTR_PSIG))
2128 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2129 &td->td_sigmask, ksi->ksi_code);
2131 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2132 ksi, &td->td_sigmask);
2133 postsig_done(sig, td, ps);
2134 mtx_unlock(&ps->ps_mtx);
2137 * Avoid a possible infinite loop if the thread
2138 * masking the signal or process is ignoring the
2141 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2142 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2143 SIGDELSET(td->td_sigmask, sig);
2144 SIGDELSET(ps->ps_sigcatch, sig);
2145 SIGDELSET(ps->ps_sigignore, sig);
2146 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2147 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2148 td->td_sigblock_val = 0;
2150 mtx_unlock(&ps->ps_mtx);
2151 p->p_sig = sig; /* XXX to verify code */
2152 tdsendsignal(p, td, sig, ksi);
2157 static struct thread *
2158 sigtd(struct proc *p, int sig, bool fast_sigblock)
2160 struct thread *td, *signal_td;
2162 PROC_LOCK_ASSERT(p, MA_OWNED);
2163 MPASS(!fast_sigblock || p == curproc);
2166 * Check if current thread can handle the signal without
2167 * switching context to another thread.
2169 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2170 (!fast_sigblock || curthread->td_sigblock_val == 0))
2173 FOREACH_THREAD_IN_PROC(p, td) {
2174 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2175 td != curthread || td->td_sigblock_val == 0)) {
2180 if (signal_td == NULL)
2181 signal_td = FIRST_THREAD_IN_PROC(p);
2186 * Send the signal to the process. If the signal has an action, the action
2187 * is usually performed by the target process rather than the caller; we add
2188 * the signal to the set of pending signals for the process.
2191 * o When a stop signal is sent to a sleeping process that takes the
2192 * default action, the process is stopped without awakening it.
2193 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2194 * regardless of the signal action (eg, blocked or ignored).
2196 * Other ignored signals are discarded immediately.
2198 * NB: This function may be entered from the debugger via the "kill" DDB
2199 * command. There is little that can be done to mitigate the possibly messy
2200 * side effects of this unwise possibility.
2203 kern_psignal(struct proc *p, int sig)
2207 ksiginfo_init(&ksi);
2208 ksi.ksi_signo = sig;
2209 ksi.ksi_code = SI_KERNEL;
2210 (void) tdsendsignal(p, NULL, sig, &ksi);
2214 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2217 return (tdsendsignal(p, NULL, sig, ksi));
2220 /* Utility function for finding a thread to send signal event to. */
2222 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2226 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2227 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2239 tdsignal(struct thread *td, int sig)
2243 ksiginfo_init(&ksi);
2244 ksi.ksi_signo = sig;
2245 ksi.ksi_code = SI_KERNEL;
2246 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2250 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2253 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2257 sig_sleepq_abort(struct thread *td, int intrval)
2259 THREAD_LOCK_ASSERT(td, MA_OWNED);
2261 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2265 return (sleepq_abort(td, intrval));
2269 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2272 sigqueue_t *sigqueue;
2279 MPASS(td == NULL || p == td->td_proc);
2280 PROC_LOCK_ASSERT(p, MA_OWNED);
2282 if (!_SIG_VALID(sig))
2283 panic("%s(): invalid signal %d", __func__, sig);
2285 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2288 * IEEE Std 1003.1-2001: return success when killing a zombie.
2290 if (p->p_state == PRS_ZOMBIE) {
2291 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2292 ksiginfo_tryfree(ksi);
2297 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2298 prop = sigprop(sig);
2301 td = sigtd(p, sig, false);
2302 sigqueue = &p->p_sigqueue;
2304 sigqueue = &td->td_sigqueue;
2306 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2309 * If the signal is being ignored, then we forget about it
2310 * immediately, except when the target process executes
2311 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2312 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2314 mtx_lock(&ps->ps_mtx);
2315 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2316 if (kern_sig_discard_ign &&
2317 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2318 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2320 mtx_unlock(&ps->ps_mtx);
2321 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2322 ksiginfo_tryfree(ksi);
2329 if (SIGISMEMBER(td->td_sigmask, sig))
2331 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2335 if (SIGISMEMBER(ps->ps_sigintr, sig))
2340 mtx_unlock(&ps->ps_mtx);
2342 if (prop & SIGPROP_CONT)
2343 sigqueue_delete_stopmask_proc(p);
2344 else if (prop & SIGPROP_STOP) {
2346 * If sending a tty stop signal to a member of an orphaned
2347 * process group, discard the signal here if the action
2348 * is default; don't stop the process below if sleeping,
2349 * and don't clear any pending SIGCONT.
2351 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2352 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2353 action == SIG_DFL) {
2354 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2355 ksiginfo_tryfree(ksi);
2358 sigqueue_delete_proc(p, SIGCONT);
2359 if (p->p_flag & P_CONTINUED) {
2360 p->p_flag &= ~P_CONTINUED;
2361 PROC_LOCK(p->p_pptr);
2362 sigqueue_take(p->p_ksi);
2363 PROC_UNLOCK(p->p_pptr);
2367 ret = sigqueue_add(sigqueue, sig, ksi);
2372 * Defer further processing for signals which are held,
2373 * except that stopped processes must be continued by SIGCONT.
2375 if (action == SIG_HOLD &&
2376 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2382 * Some signals have a process-wide effect and a per-thread
2383 * component. Most processing occurs when the process next
2384 * tries to cross the user boundary, however there are some
2385 * times when processing needs to be done immediately, such as
2386 * waking up threads so that they can cross the user boundary.
2387 * We try to do the per-process part here.
2389 if (P_SHOULDSTOP(p)) {
2390 KASSERT(!(p->p_flag & P_WEXIT),
2391 ("signal to stopped but exiting process"));
2392 if (sig == SIGKILL) {
2394 * If traced process is already stopped,
2395 * then no further action is necessary.
2397 if (p->p_flag & P_TRACED)
2400 * SIGKILL sets process running.
2401 * It will die elsewhere.
2402 * All threads must be restarted.
2404 p->p_flag &= ~P_STOPPED_SIG;
2408 if (prop & SIGPROP_CONT) {
2410 * If traced process is already stopped,
2411 * then no further action is necessary.
2413 if (p->p_flag & P_TRACED)
2416 * If SIGCONT is default (or ignored), we continue the
2417 * process but don't leave the signal in sigqueue as
2418 * it has no further action. If SIGCONT is held, we
2419 * continue the process and leave the signal in
2420 * sigqueue. If the process catches SIGCONT, let it
2421 * handle the signal itself. If it isn't waiting on
2422 * an event, it goes back to run state.
2423 * Otherwise, process goes back to sleep state.
2425 p->p_flag &= ~P_STOPPED_SIG;
2427 if (p->p_numthreads == p->p_suspcount) {
2429 p->p_flag |= P_CONTINUED;
2430 p->p_xsig = SIGCONT;
2431 PROC_LOCK(p->p_pptr);
2432 childproc_continued(p);
2433 PROC_UNLOCK(p->p_pptr);
2436 if (action == SIG_DFL) {
2437 thread_unsuspend(p);
2439 sigqueue_delete(sigqueue, sig);
2442 if (action == SIG_CATCH) {
2444 * The process wants to catch it so it needs
2445 * to run at least one thread, but which one?
2451 * The signal is not ignored or caught.
2453 thread_unsuspend(p);
2458 if (prop & SIGPROP_STOP) {
2460 * If traced process is already stopped,
2461 * then no further action is necessary.
2463 if (p->p_flag & P_TRACED)
2466 * Already stopped, don't need to stop again
2467 * (If we did the shell could get confused).
2468 * Just make sure the signal STOP bit set.
2470 p->p_flag |= P_STOPPED_SIG;
2471 sigqueue_delete(sigqueue, sig);
2476 * All other kinds of signals:
2477 * If a thread is sleeping interruptibly, simulate a
2478 * wakeup so that when it is continued it will be made
2479 * runnable and can look at the signal. However, don't make
2480 * the PROCESS runnable, leave it stopped.
2481 * It may run a bit until it hits a thread_suspend_check().
2485 if (TD_CAN_ABORT(td))
2486 wakeup_swapper = sig_sleepq_abort(td, intrval);
2492 * Mutexes are short lived. Threads waiting on them will
2493 * hit thread_suspend_check() soon.
2495 } else if (p->p_state == PRS_NORMAL) {
2496 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2497 tdsigwakeup(td, sig, action, intrval);
2501 MPASS(action == SIG_DFL);
2503 if (prop & SIGPROP_STOP) {
2504 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2506 p->p_flag |= P_STOPPED_SIG;
2509 wakeup_swapper = sig_suspend_threads(td, p);
2510 if (p->p_numthreads == p->p_suspcount) {
2512 * only thread sending signal to another
2513 * process can reach here, if thread is sending
2514 * signal to its process, because thread does
2515 * not suspend itself here, p_numthreads
2516 * should never be equal to p_suspcount.
2520 sigqueue_delete_proc(p, p->p_xsig);
2526 /* Not in "NORMAL" state. discard the signal. */
2527 sigqueue_delete(sigqueue, sig);
2532 * The process is not stopped so we need to apply the signal to all the
2536 tdsigwakeup(td, sig, action, intrval);
2538 thread_unsuspend(p);
2541 itimer_proc_continue(p);
2542 kqtimer_proc_continue(p);
2544 /* If we jump here, proc slock should not be owned. */
2545 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2553 * The force of a signal has been directed against a single
2554 * thread. We need to see what we can do about knocking it
2555 * out of any sleep it may be in etc.
2558 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2560 struct proc *p = td->td_proc;
2561 int prop, wakeup_swapper;
2563 PROC_LOCK_ASSERT(p, MA_OWNED);
2564 prop = sigprop(sig);
2569 * Bring the priority of a thread up if we want it to get
2570 * killed in this lifetime. Be careful to avoid bumping the
2571 * priority of the idle thread, since we still allow to signal
2574 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2575 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2576 sched_prio(td, PUSER);
2577 if (TD_ON_SLEEPQ(td)) {
2579 * If thread is sleeping uninterruptibly
2580 * we can't interrupt the sleep... the signal will
2581 * be noticed when the process returns through
2582 * trap() or syscall().
2584 if ((td->td_flags & TDF_SINTR) == 0)
2587 * If SIGCONT is default (or ignored) and process is
2588 * asleep, we are finished; the process should not
2591 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2594 sigqueue_delete(&p->p_sigqueue, sig);
2596 * It may be on either list in this state.
2597 * Remove from both for now.
2599 sigqueue_delete(&td->td_sigqueue, sig);
2604 * Don't awaken a sleeping thread for SIGSTOP if the
2605 * STOP signal is deferred.
2607 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2608 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2612 * Give low priority threads a better chance to run.
2614 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2615 sched_prio(td, PUSER);
2617 wakeup_swapper = sig_sleepq_abort(td, intrval);
2625 * Other states do nothing with the signal immediately,
2626 * other than kicking ourselves if we are running.
2627 * It will either never be noticed, or noticed very soon.
2630 if (TD_IS_RUNNING(td) && td != curthread)
2640 ptrace_coredumpreq(struct thread *td, struct proc *p,
2641 struct thr_coredump_req *tcq)
2645 if (p->p_sysent->sv_coredump == NULL) {
2646 tcq->tc_error = ENOSYS;
2650 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2651 tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2652 tcq->tc_limit, tcq->tc_flags);
2653 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2657 ptrace_syscallreq(struct thread *td, struct proc *p,
2658 struct thr_syscall_req *tsr)
2660 struct sysentvec *sv;
2662 register_t rv_saved[2];
2665 bool audited, sy_thr_static;
2668 if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2669 tsr->ts_ret.sr_error = ENOSYS;
2673 sc = tsr->ts_sa.code;
2674 if (sc == SYS_syscall || sc == SYS___syscall) {
2675 sc = tsr->ts_sa.args[0];
2676 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2677 sizeof(register_t) * (tsr->ts_nargs - 1));
2680 tsr->ts_sa.callp = se = &sv->sv_table[sc];
2682 VM_CNT_INC(v_syscall);
2684 if (__predict_false(td->td_cowgen != atomic_load_int(
2685 &td->td_proc->p_cowgen)))
2686 thread_cow_update(td);
2688 #ifdef CAPABILITY_MODE
2689 if (IN_CAPABILITY_MODE(td) && (se->sy_flags & SYF_CAPENABLED) == 0) {
2690 tsr->ts_ret.sr_error = ECAPMODE;
2695 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2696 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2698 if (!sy_thr_static) {
2699 error = syscall_thread_enter(td, se);
2701 tsr->ts_ret.sr_error = error;
2706 rv_saved[0] = td->td_retval[0];
2707 rv_saved[1] = td->td_retval[1];
2708 nerror = td->td_errno;
2709 td->td_retval[0] = 0;
2710 td->td_retval[1] = 0;
2712 #ifdef KDTRACE_HOOKS
2713 if (se->sy_entry != 0)
2714 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2716 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2717 #ifdef KDTRACE_HOOKS
2718 if (se->sy_return != 0)
2719 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2720 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2723 tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2724 tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2725 td->td_retval[0] = rv_saved[0];
2726 td->td_retval[1] = rv_saved[1];
2727 td->td_errno = nerror;
2730 AUDIT_SYSCALL_EXIT(error, td);
2732 syscall_thread_exit(td, se);
2736 ptrace_remotereq(struct thread *td, int flag)
2740 MPASS(td == curthread);
2742 PROC_LOCK_ASSERT(p, MA_OWNED);
2743 if ((td->td_dbgflags & flag) == 0)
2745 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2746 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2750 case TDB_COREDUMPREQ:
2751 ptrace_coredumpreq(td, p, td->td_remotereq);
2753 case TDB_SCREMOTEREQ:
2754 ptrace_syscallreq(td, p, td->td_remotereq);
2761 MPASS((td->td_dbgflags & flag) != 0);
2762 td->td_dbgflags &= ~flag;
2763 td->td_remotereq = NULL;
2768 sig_suspend_threads(struct thread *td, struct proc *p)
2773 PROC_LOCK_ASSERT(p, MA_OWNED);
2774 PROC_SLOCK_ASSERT(p, MA_OWNED);
2777 FOREACH_THREAD_IN_PROC(p, td2) {
2779 ast_sched_locked(td2, TDA_SUSPEND);
2780 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2781 (td2->td_flags & TDF_SINTR)) {
2782 if (td2->td_flags & TDF_SBDRY) {
2784 * Once a thread is asleep with
2785 * TDF_SBDRY and without TDF_SERESTART
2786 * or TDF_SEINTR set, it should never
2787 * become suspended due to this check.
2789 KASSERT(!TD_IS_SUSPENDED(td2),
2790 ("thread with deferred stops suspended"));
2791 if (TD_SBDRY_INTR(td2)) {
2792 wakeup_swapper |= sleepq_abort(td2,
2793 TD_SBDRY_ERRNO(td2));
2796 } else if (!TD_IS_SUSPENDED(td2))
2797 thread_suspend_one(td2);
2798 } else if (!TD_IS_SUSPENDED(td2)) {
2800 if (TD_IS_RUNNING(td2) && td2 != td)
2801 forward_signal(td2);
2806 return (wakeup_swapper);
2810 * Stop the process for an event deemed interesting to the debugger. If si is
2811 * non-NULL, this is a signal exchange; the new signal requested by the
2812 * debugger will be returned for handling. If si is NULL, this is some other
2813 * type of interesting event. The debugger may request a signal be delivered in
2814 * that case as well, however it will be deferred until it can be handled.
2817 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2819 struct proc *p = td->td_proc;
2823 PROC_LOCK_ASSERT(p, MA_OWNED);
2824 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2825 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2826 &p->p_mtx.lock_object, "Stopping for traced signal");
2830 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2831 td->td_dbgflags |= TDB_XSIG;
2832 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2833 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2835 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2838 * Ensure that, if we've been PT_KILLed, the
2839 * exit status reflects that. Another thread
2840 * may also be in ptracestop(), having just
2841 * received the SIGKILL, but this thread was
2842 * unsuspended first.
2844 td->td_dbgflags &= ~TDB_XSIG;
2845 td->td_xsig = SIGKILL;
2849 if (p->p_flag & P_SINGLE_EXIT &&
2850 !(td->td_dbgflags & TDB_EXIT)) {
2852 * Ignore ptrace stops except for thread exit
2853 * events when the process exits.
2855 td->td_dbgflags &= ~TDB_XSIG;
2861 * Make wait(2) work. Ensure that right after the
2862 * attach, the thread which was decided to become the
2863 * leader of attach gets reported to the waiter.
2864 * Otherwise, just avoid overwriting another thread's
2865 * assignment to p_xthread. If another thread has
2866 * already set p_xthread, the current thread will get
2867 * a chance to report itself upon the next iteration.
2869 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2870 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2871 p->p_xthread == NULL)) {
2876 * If we are on sleepqueue already,
2877 * let sleepqueue code decide if it
2878 * needs to go sleep after attach.
2880 if (td->td_wchan == NULL)
2881 td->td_dbgflags &= ~TDB_FSTP;
2883 p->p_flag2 &= ~P2_PTRACE_FSTP;
2884 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2885 sig_suspend_threads(td, p);
2887 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2888 td->td_dbgflags &= ~TDB_STOPATFORK;
2891 td->td_dbgflags |= TDB_SSWITCH;
2892 thread_suspend_switch(td, p);
2893 td->td_dbgflags &= ~TDB_SSWITCH;
2894 if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2895 TDB_SCREMOTEREQ)) != 0) {
2896 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2897 TDB_SCREMOTEREQ)) !=
2898 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2900 ptrace_remotereq(td, td->td_dbgflags &
2901 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2905 if (p->p_xthread == td)
2906 p->p_xthread = NULL;
2907 if (!(p->p_flag & P_TRACED))
2909 if (td->td_dbgflags & TDB_SUSPEND) {
2910 if (p->p_flag & P_SINGLE_EXIT)
2918 if (si != NULL && sig == td->td_xsig) {
2919 /* Parent wants us to take the original signal unchanged. */
2920 si->ksi_flags |= KSI_HEAD;
2921 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2923 } else if (td->td_xsig != 0) {
2925 * If parent wants us to take a new signal, then it will leave
2926 * it in td->td_xsig; otherwise we just look for signals again.
2928 ksiginfo_init(&ksi);
2929 ksi.ksi_signo = td->td_xsig;
2930 ksi.ksi_flags |= KSI_PTRACE;
2931 td2 = sigtd(p, td->td_xsig, false);
2932 tdsendsignal(p, td2, td->td_xsig, &ksi);
2937 return (td->td_xsig);
2941 reschedule_signals(struct proc *p, sigset_t block, int flags)
2946 bool fastblk, pslocked;
2948 PROC_LOCK_ASSERT(p, MA_OWNED);
2950 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2951 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2952 if (SIGISEMPTY(p->p_siglist))
2954 SIGSETAND(block, p->p_siglist);
2955 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2956 SIG_FOREACH(sig, &block) {
2957 td = sigtd(p, sig, fastblk);
2960 * If sigtd() selected us despite sigfastblock is
2961 * blocking, do not activate AST or wake us, to avoid
2962 * loop in AST handler.
2964 if (fastblk && td == curthread)
2969 mtx_lock(&ps->ps_mtx);
2970 if (p->p_flag & P_TRACED ||
2971 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2972 !SIGISMEMBER(td->td_sigmask, sig))) {
2973 tdsigwakeup(td, sig, SIG_CATCH,
2974 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2978 mtx_unlock(&ps->ps_mtx);
2983 tdsigcleanup(struct thread *td)
2989 PROC_LOCK_ASSERT(p, MA_OWNED);
2991 sigqueue_flush(&td->td_sigqueue);
2992 if (p->p_numthreads == 1)
2996 * Since we cannot handle signals, notify signal post code
2997 * about this by filling the sigmask.
2999 * Also, if needed, wake up thread(s) that do not block the
3000 * same signals as the exiting thread, since the thread might
3001 * have been selected for delivery and woken up.
3003 SIGFILLSET(unblocked);
3004 SIGSETNAND(unblocked, td->td_sigmask);
3005 SIGFILLSET(td->td_sigmask);
3006 reschedule_signals(p, unblocked, 0);
3011 sigdeferstop_curr_flags(int cflags)
3014 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
3015 (cflags & TDF_SBDRY) != 0);
3016 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3020 * Defer the delivery of SIGSTOP for the current thread, according to
3021 * the requested mode. Returns previous flags, which must be restored
3022 * by sigallowstop().
3024 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
3025 * cleared by the current thread, which allow the lock-less read-only
3029 sigdeferstop_impl(int mode)
3035 cflags = sigdeferstop_curr_flags(td->td_flags);
3037 case SIGDEFERSTOP_NOP:
3040 case SIGDEFERSTOP_OFF:
3043 case SIGDEFERSTOP_SILENT:
3044 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
3046 case SIGDEFERSTOP_EINTR:
3047 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
3049 case SIGDEFERSTOP_ERESTART:
3050 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
3053 panic("sigdeferstop: invalid mode %x", mode);
3056 if (cflags == nflags)
3057 return (SIGDEFERSTOP_VAL_NCHG);
3059 td->td_flags = (td->td_flags & ~cflags) | nflags;
3065 * Restores the STOP handling mode, typically permitting the delivery
3066 * of SIGSTOP for the current thread. This does not immediately
3067 * suspend if a stop was posted. Instead, the thread will suspend
3068 * either via ast() or a subsequent interruptible sleep.
3071 sigallowstop_impl(int prev)
3076 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3077 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3078 ("sigallowstop: incorrect previous mode %x", prev));
3080 cflags = sigdeferstop_curr_flags(td->td_flags);
3081 if (cflags != prev) {
3083 td->td_flags = (td->td_flags & ~cflags) | prev;
3092 SIGSTATUS_SBDRY_STOP,
3096 * The thread has signal "sig" pending. Figure out what to do with it:
3098 * _HANDLE -> the caller should handle the signal
3099 * _HANDLED -> handled internally, reload pending signal set
3100 * _IGNORE -> ignored, remove from the set of pending signals and try the
3101 * next pending signal
3102 * _SBDRY_STOP -> the signal should stop the thread but this is not
3103 * permitted in the current context
3105 static enum sigstatus
3106 sigprocess(struct thread *td, int sig)
3110 struct sigqueue *queue;
3114 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3118 mtx_assert(&ps->ps_mtx, MA_OWNED);
3119 PROC_LOCK_ASSERT(p, MA_OWNED);
3122 * We should allow pending but ignored signals below
3123 * if there is sigwait() active, or P_TRACED was
3124 * on when they were posted.
3126 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3127 (p->p_flag & P_TRACED) == 0 &&
3128 (td->td_flags & TDF_SIGWAIT) == 0) {
3129 return (SIGSTATUS_IGNORE);
3133 * If the process is going to single-thread mode to prepare
3134 * for exit, there is no sense in delivering any signal
3135 * to usermode. Another important consequence is that
3136 * msleep(..., PCATCH, ...) now is only interruptible by a
3139 if ((p->p_flag2 & P2_WEXIT) != 0)
3140 return (SIGSTATUS_IGNORE);
3142 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3144 * If traced, always stop.
3145 * Remove old signal from queue before the stop.
3146 * XXX shrug off debugger, it causes siginfo to
3149 queue = &td->td_sigqueue;
3150 ksiginfo_init(&ksi);
3151 if (sigqueue_get(queue, sig, &ksi) == 0) {
3152 queue = &p->p_sigqueue;
3153 sigqueue_get(queue, sig, &ksi);
3155 td->td_si = ksi.ksi_info;
3157 mtx_unlock(&ps->ps_mtx);
3158 sig = ptracestop(td, sig, &ksi);
3159 mtx_lock(&ps->ps_mtx);
3161 td->td_si.si_signo = 0;
3164 * Keep looking if the debugger discarded or
3165 * replaced the signal.
3168 return (SIGSTATUS_HANDLED);
3171 * If the signal became masked, re-queue it.
3173 if (SIGISMEMBER(td->td_sigmask, sig)) {
3174 ksi.ksi_flags |= KSI_HEAD;
3175 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3176 return (SIGSTATUS_HANDLED);
3180 * If the traced bit got turned off, requeue the signal and
3181 * reload the set of pending signals. This ensures that p_sig*
3182 * and p_sigact are consistent.
3184 if ((p->p_flag & P_TRACED) == 0) {
3185 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3186 ksi.ksi_flags |= KSI_HEAD;
3187 sigqueue_add(queue, sig, &ksi);
3189 return (SIGSTATUS_HANDLED);
3194 * Decide whether the signal should be returned.
3195 * Return the signal's number, or fall through
3196 * to clear it from the pending mask.
3198 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3199 case (intptr_t)SIG_DFL:
3201 * Don't take default actions on system processes.
3203 if (p->p_pid <= 1) {
3206 * Are you sure you want to ignore SIGSEGV
3209 printf("Process (pid %lu) got signal %d\n",
3210 (u_long)p->p_pid, sig);
3212 return (SIGSTATUS_IGNORE);
3216 * If there is a pending stop signal to process with
3217 * default action, stop here, then clear the signal.
3218 * Traced or exiting processes should ignore stops.
3219 * Additionally, a member of an orphaned process group
3220 * should ignore tty stops.
3222 prop = sigprop(sig);
3223 if (prop & SIGPROP_STOP) {
3224 mtx_unlock(&ps->ps_mtx);
3225 if ((p->p_flag & (P_TRACED | P_WEXIT |
3226 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3227 pg_flags & PGRP_ORPHANED) != 0 &&
3228 (prop & SIGPROP_TTYSTOP) != 0)) {
3229 mtx_lock(&ps->ps_mtx);
3230 return (SIGSTATUS_IGNORE);
3232 if (TD_SBDRY_INTR(td)) {
3233 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3234 ("lost TDF_SBDRY"));
3235 mtx_lock(&ps->ps_mtx);
3236 return (SIGSTATUS_SBDRY_STOP);
3238 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3239 &p->p_mtx.lock_object, "Catching SIGSTOP");
3240 sigqueue_delete(&td->td_sigqueue, sig);
3241 sigqueue_delete(&p->p_sigqueue, sig);
3242 p->p_flag |= P_STOPPED_SIG;
3245 sig_suspend_threads(td, p);
3246 thread_suspend_switch(td, p);
3248 mtx_lock(&ps->ps_mtx);
3249 return (SIGSTATUS_HANDLED);
3250 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3251 (td->td_flags & TDF_SIGWAIT) == 0) {
3253 * Default action is to ignore; drop it if
3254 * not in kern_sigtimedwait().
3256 return (SIGSTATUS_IGNORE);
3258 return (SIGSTATUS_HANDLE);
3261 case (intptr_t)SIG_IGN:
3262 if ((td->td_flags & TDF_SIGWAIT) == 0)
3263 return (SIGSTATUS_IGNORE);
3265 return (SIGSTATUS_HANDLE);
3269 * This signal has an action, let postsig() process it.
3271 return (SIGSTATUS_HANDLE);
3276 * If the current process has received a signal (should be caught or cause
3277 * termination, should interrupt current syscall), return the signal number.
3278 * Stop signals with default action are processed immediately, then cleared;
3279 * they aren't returned. This is checked after each entry to the system for
3280 * a syscall or trap (though this can usually be done without calling
3281 * issignal by checking the pending signal masks in cursig.) The normal call
3284 * while (sig = cursig(curthread))
3288 issignal(struct thread *td)
3291 sigset_t sigpending;
3295 PROC_LOCK_ASSERT(p, MA_OWNED);
3298 sigpending = td->td_sigqueue.sq_signals;
3299 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3300 SIGSETNAND(sigpending, td->td_sigmask);
3302 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3303 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3304 SIG_STOPSIGMASK(sigpending);
3305 if (SIGISEMPTY(sigpending)) /* no signal to send */
3309 * Do fast sigblock if requested by usermode. Since
3310 * we do know that there was a signal pending at this
3311 * point, set the FAST_SIGBLOCK_PEND as indicator for
3312 * usermode to perform a dummy call to
3313 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3314 * delivery of postponed pending signal.
3316 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3317 if (td->td_sigblock_val != 0)
3318 SIGSETNAND(sigpending, fastblock_mask);
3319 if (SIGISEMPTY(sigpending)) {
3320 td->td_pflags |= TDP_SIGFASTPENDING;
3325 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3326 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3327 SIGISMEMBER(sigpending, SIGSTOP)) {
3329 * If debugger just attached, always consume
3330 * SIGSTOP from ptrace(PT_ATTACH) first, to
3331 * execute the debugger attach ritual in
3334 td->td_dbgflags |= TDB_FSTP;
3335 SIGEMPTYSET(sigpending);
3336 SIGADDSET(sigpending, SIGSTOP);
3339 SIG_FOREACH(sig, &sigpending) {
3340 switch (sigprocess(td, sig)) {
3341 case SIGSTATUS_HANDLE:
3343 case SIGSTATUS_HANDLED:
3345 case SIGSTATUS_IGNORE:
3346 sigqueue_delete(&td->td_sigqueue, sig);
3347 sigqueue_delete(&p->p_sigqueue, sig);
3349 case SIGSTATUS_SBDRY_STOP:
3358 thread_stopped(struct proc *p)
3362 PROC_LOCK_ASSERT(p, MA_OWNED);
3363 PROC_SLOCK_ASSERT(p, MA_OWNED);
3367 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3369 p->p_flag &= ~P_WAITED;
3370 PROC_LOCK(p->p_pptr);
3371 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3372 CLD_TRAPPED : CLD_STOPPED);
3373 PROC_UNLOCK(p->p_pptr);
3379 * Take the action for the specified signal
3380 * from the current set of pending signals.
3390 sigset_t returnmask;
3392 KASSERT(sig != 0, ("postsig"));
3396 PROC_LOCK_ASSERT(p, MA_OWNED);
3398 mtx_assert(&ps->ps_mtx, MA_OWNED);
3399 ksiginfo_init(&ksi);
3400 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3401 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3403 ksi.ksi_signo = sig;
3404 if (ksi.ksi_code == SI_TIMER)
3405 itimer_accept(p, ksi.ksi_timerid, &ksi);
3406 action = ps->ps_sigact[_SIG_IDX(sig)];
3408 if (KTRPOINT(td, KTR_PSIG))
3409 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3410 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3413 if (action == SIG_DFL) {
3415 * Default action, where the default is to kill
3416 * the process. (Other cases were ignored above.)
3418 mtx_unlock(&ps->ps_mtx);
3419 proc_td_siginfo_capture(td, &ksi.ksi_info);
3424 * If we get here, the signal must be caught.
3426 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3427 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3428 ("postsig action: blocked sig %d", sig));
3431 * Set the new mask value and also defer further
3432 * occurrences of this signal.
3434 * Special case: user has done a sigsuspend. Here the
3435 * current mask is not of interest, but rather the
3436 * mask from before the sigsuspend is what we want
3437 * restored after the signal processing is completed.
3439 if (td->td_pflags & TDP_OLDMASK) {
3440 returnmask = td->td_oldsigmask;
3441 td->td_pflags &= ~TDP_OLDMASK;
3443 returnmask = td->td_sigmask;
3445 if (p->p_sig == sig) {
3448 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3449 postsig_done(sig, td, ps);
3455 sig_ast_checksusp(struct thread *td)
3457 struct proc *p __diagused;
3461 PROC_LOCK_ASSERT(p, MA_OWNED);
3463 if (!td_ast_pending(td, TDA_SUSPEND))
3466 ret = thread_suspend_check(1);
3467 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3472 sig_ast_needsigchk(struct thread *td)
3479 PROC_LOCK_ASSERT(p, MA_OWNED);
3481 if (!td_ast_pending(td, TDA_SIG))
3485 mtx_lock(&ps->ps_mtx);
3488 mtx_unlock(&ps->ps_mtx);
3489 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3490 KASSERT(TD_SBDRY_INTR(td),
3491 ("lost TDF_SERESTART of TDF_SEINTR"));
3492 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3493 (TDF_SEINTR | TDF_SERESTART),
3494 ("both TDF_SEINTR and TDF_SERESTART"));
3495 ret = TD_SBDRY_ERRNO(td);
3496 } else if (sig != 0) {
3497 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3498 mtx_unlock(&ps->ps_mtx);
3500 mtx_unlock(&ps->ps_mtx);
3505 * Do not go into sleep if this thread was the ptrace(2)
3506 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3507 * but we usually act on the signal by interrupting sleep, and
3508 * should do that here as well.
3510 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3513 td->td_dbgflags &= ~TDB_FSTP;
3527 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3533 ret = sig_ast_checksusp(td);
3535 ret = sig_ast_needsigchk(td);
3541 curproc_sigkilled(void)
3549 if (!td_ast_pending(td, TDA_SIG))
3555 mtx_lock(&ps->ps_mtx);
3556 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3557 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3558 mtx_unlock(&ps->ps_mtx);
3564 proc_wkilled(struct proc *p)
3567 PROC_LOCK_ASSERT(p, MA_OWNED);
3568 if ((p->p_flag & P_WKILLED) == 0) {
3569 p->p_flag |= P_WKILLED;
3571 * Notify swapper that there is a process to swap in.
3572 * The notification is racy, at worst it would take 10
3573 * seconds for the swapper process to notice.
3575 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3581 * Kill the current process for stated reason.
3584 killproc(struct proc *p, const char *why)
3587 PROC_LOCK_ASSERT(p, MA_OWNED);
3588 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3590 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3591 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3592 p->p_ucred->cr_uid, why);
3594 kern_psignal(p, SIGKILL);
3598 * Force the current process to exit with the specified signal, dumping core
3599 * if appropriate. We bypass the normal tests for masked and caught signals,
3600 * allowing unrecoverable failures to terminate the process without changing
3601 * signal state. Mark the accounting record with the signal termination.
3602 * If dumping core, save the signal number for the debugger. Calls exit and
3606 sigexit(struct thread *td, int sig)
3608 struct proc *p = td->td_proc;
3610 PROC_LOCK_ASSERT(p, MA_OWNED);
3611 proc_set_p2_wexit(p);
3613 p->p_acflag |= AXSIG;
3615 * We must be single-threading to generate a core dump. This
3616 * ensures that the registers in the core file are up-to-date.
3617 * Also, the ELF dump handler assumes that the thread list doesn't
3618 * change out from under it.
3620 * XXX If another thread attempts to single-thread before us
3621 * (e.g. via fork()), we won't get a dump at all.
3623 if ((sigprop(sig) & SIGPROP_CORE) &&
3624 thread_single(p, SINGLE_NO_EXIT) == 0) {
3627 * Log signals which would cause core dumps
3628 * (Log as LOG_INFO to appease those who don't want
3630 * XXX : Todo, as well as euid, write out ruid too
3631 * Note that coredump() drops proc lock.
3633 if (coredump(td) == 0)
3635 if (kern_logsigexit)
3637 "pid %d (%s), jid %d, uid %d: exited on "
3638 "signal %d%s\n", p->p_pid, p->p_comm,
3639 p->p_ucred->cr_prison->pr_id,
3640 td->td_ucred->cr_uid,
3642 sig & WCOREFLAG ? " (core dumped)" : "");
3650 * Send queued SIGCHLD to parent when child process's state
3654 sigparent(struct proc *p, int reason, int status)
3656 PROC_LOCK_ASSERT(p, MA_OWNED);
3657 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3659 if (p->p_ksi != NULL) {
3660 p->p_ksi->ksi_signo = SIGCHLD;
3661 p->p_ksi->ksi_code = reason;
3662 p->p_ksi->ksi_status = status;
3663 p->p_ksi->ksi_pid = p->p_pid;
3664 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3665 if (KSI_ONQ(p->p_ksi))
3668 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3672 childproc_jobstate(struct proc *p, int reason, int sig)
3676 PROC_LOCK_ASSERT(p, MA_OWNED);
3677 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3680 * Wake up parent sleeping in kern_wait(), also send
3681 * SIGCHLD to parent, but SIGCHLD does not guarantee
3682 * that parent will awake, because parent may masked
3685 p->p_pptr->p_flag |= P_STATCHILD;
3688 ps = p->p_pptr->p_sigacts;
3689 mtx_lock(&ps->ps_mtx);
3690 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3691 mtx_unlock(&ps->ps_mtx);
3692 sigparent(p, reason, sig);
3694 mtx_unlock(&ps->ps_mtx);
3698 childproc_stopped(struct proc *p, int reason)
3701 childproc_jobstate(p, reason, p->p_xsig);
3705 childproc_continued(struct proc *p)
3707 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3711 childproc_exited(struct proc *p)
3715 if (WCOREDUMP(p->p_xsig)) {
3716 reason = CLD_DUMPED;
3717 status = WTERMSIG(p->p_xsig);
3718 } else if (WIFSIGNALED(p->p_xsig)) {
3719 reason = CLD_KILLED;
3720 status = WTERMSIG(p->p_xsig);
3722 reason = CLD_EXITED;
3723 status = p->p_xexit;
3726 * XXX avoid calling wakeup(p->p_pptr), the work is
3729 sigparent(p, reason, status);
3732 #define MAX_NUM_CORE_FILES 100000
3733 #ifndef NUM_CORE_FILES
3734 #define NUM_CORE_FILES 5
3736 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3737 static int num_cores = NUM_CORE_FILES;
3740 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3745 new_val = num_cores;
3746 error = sysctl_handle_int(oidp, &new_val, 0, req);
3747 if (error != 0 || req->newptr == NULL)
3749 if (new_val > MAX_NUM_CORE_FILES)
3750 new_val = MAX_NUM_CORE_FILES;
3753 num_cores = new_val;
3756 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3757 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3758 sysctl_debug_num_cores_check, "I",
3759 "Maximum number of generated process corefiles while using index format");
3761 #define GZIP_SUFFIX ".gz"
3762 #define ZSTD_SUFFIX ".zst"
3764 int compress_user_cores = 0;
3767 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3771 val = compress_user_cores;
3772 error = sysctl_handle_int(oidp, &val, 0, req);
3773 if (error != 0 || req->newptr == NULL)
3775 if (val != 0 && !compressor_avail(val))
3777 compress_user_cores = val;
3780 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3781 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3782 sysctl_compress_user_cores, "I",
3783 "Enable compression of user corefiles ("
3784 __XSTRING(COMPRESS_GZIP) " = gzip, "
3785 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3787 int compress_user_cores_level = 6;
3788 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3789 &compress_user_cores_level, 0,
3790 "Corefile compression level");
3793 * Protect the access to corefilename[] by allproc_lock.
3795 #define corefilename_lock allproc_lock
3797 static char corefilename[MAXPATHLEN] = {"%N.core"};
3798 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3801 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3805 sx_xlock(&corefilename_lock);
3806 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3808 sx_xunlock(&corefilename_lock);
3812 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3813 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3814 "Process corefile name format string");
3817 vnode_close_locked(struct thread *td, struct vnode *vp)
3821 vn_close(vp, FWRITE, td->td_ucred, td);
3825 * If the core format has a %I in it, then we need to check
3826 * for existing corefiles before defining a name.
3827 * To do this we iterate over 0..ncores to find a
3828 * non-existing core file name to use. If all core files are
3829 * already used we choose the oldest one.
3832 corefile_open_last(struct thread *td, char *name, int indexpos,
3833 int indexlen, int ncores, struct vnode **vpp)
3835 struct vnode *oldvp, *nextvp, *vp;
3837 struct nameidata nd;
3838 int error, i, flags, oflags, cmode;
3840 struct timespec lasttime;
3842 nextvp = oldvp = NULL;
3843 cmode = S_IRUSR | S_IWUSR;
3844 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3845 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3847 for (i = 0; i < ncores; i++) {
3848 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3850 ch = name[indexpos + indexlen];
3851 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3853 name[indexpos + indexlen] = ch;
3855 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3856 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3863 if ((flags & O_CREAT) == O_CREAT) {
3868 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3870 vnode_close_locked(td, vp);
3874 if (oldvp == NULL ||
3875 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3876 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3877 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3879 vn_close(oldvp, FWRITE, td->td_ucred, td);
3882 lasttime = vattr.va_mtime;
3884 vnode_close_locked(td, vp);
3888 if (oldvp != NULL) {
3889 if (nextvp == NULL) {
3890 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3892 vn_close(oldvp, FWRITE, td->td_ucred, td);
3895 error = vn_lock(nextvp, LK_EXCLUSIVE);
3897 vn_close(nextvp, FWRITE, td->td_ucred,
3903 vn_close(oldvp, FWRITE, td->td_ucred, td);
3908 vnode_close_locked(td, oldvp);
3917 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3918 * Expand the name described in corefilename, using name, uid, and pid
3919 * and open/create core file.
3920 * corefilename is a printf-like string, with three format specifiers:
3921 * %N name of process ("name")
3922 * %P process id (pid)
3924 * For example, "%N.core" is the default; they can be disabled completely
3925 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3926 * This is controlled by the sysctl variable kern.corefile (see above).
3929 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3930 int compress, int signum, struct vnode **vpp, char **namep)
3933 struct nameidata nd;
3935 char *hostname, *name;
3936 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3939 format = corefilename;
3940 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3944 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3945 sx_slock(&corefilename_lock);
3946 for (i = 0; format[i] != '\0'; i++) {
3947 switch (format[i]) {
3948 case '%': /* Format character */
3950 switch (format[i]) {
3952 sbuf_putc(&sb, '%');
3954 case 'H': /* hostname */
3955 if (hostname == NULL) {
3956 hostname = malloc(MAXHOSTNAMELEN,
3959 getcredhostname(td->td_ucred, hostname,
3961 sbuf_printf(&sb, "%s", hostname);
3963 case 'I': /* autoincrementing index */
3964 if (indexpos != -1) {
3965 sbuf_printf(&sb, "%%I");
3969 indexpos = sbuf_len(&sb);
3970 sbuf_printf(&sb, "%u", ncores - 1);
3971 indexlen = sbuf_len(&sb) - indexpos;
3973 case 'N': /* process name */
3974 sbuf_printf(&sb, "%s", comm);
3976 case 'P': /* process id */
3977 sbuf_printf(&sb, "%u", pid);
3979 case 'S': /* signal number */
3980 sbuf_printf(&sb, "%i", signum);
3982 case 'U': /* user id */
3983 sbuf_printf(&sb, "%u", uid);
3987 "Unknown format character %c in "
3988 "corename `%s'\n", format[i], format);
3993 sbuf_putc(&sb, format[i]);
3997 sx_sunlock(&corefilename_lock);
3998 free(hostname, M_TEMP);
3999 if (compress == COMPRESS_GZIP)
4000 sbuf_printf(&sb, GZIP_SUFFIX);
4001 else if (compress == COMPRESS_ZSTD)
4002 sbuf_printf(&sb, ZSTD_SUFFIX);
4003 if (sbuf_error(&sb) != 0) {
4004 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
4005 "long\n", (long)pid, comm, (u_long)uid);
4013 if (indexpos != -1) {
4014 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
4018 "pid %d (%s), uid (%u): Path `%s' failed "
4019 "on initial open test, error = %d\n",
4020 pid, comm, uid, name, error);
4023 cmode = S_IRUSR | S_IWUSR;
4024 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
4025 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
4026 flags = O_CREAT | FWRITE | O_NOFOLLOW;
4027 if ((td->td_proc->p_flag & P_SUGID) != 0)
4030 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
4031 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
4041 audit_proc_coredump(td, name, error);
4051 * Dump a process' core. The main routine does some
4052 * policy checking, and creates the name of the coredump;
4053 * then it passes on a vnode and a size limit to the process-specific
4054 * coredump routine if there is one; if there _is not_ one, it returns
4055 * ENOSYS; otherwise it returns the error from the process-specific routine.
4059 coredump(struct thread *td)
4061 struct proc *p = td->td_proc;
4062 struct ucred *cred = td->td_ucred;
4066 size_t fullpathsize;
4067 int error, error1, locked;
4068 char *name; /* name of corefile */
4071 char *fullpath, *freepath = NULL;
4074 PROC_LOCK_ASSERT(p, MA_OWNED);
4075 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
4077 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
4078 (p->p_flag2 & P2_NOTRACE) != 0) {
4084 * Note that the bulk of limit checking is done after
4085 * the corefile is created. The exception is if the limit
4086 * for corefiles is 0, in which case we don't bother
4087 * creating the corefile at all. This layout means that
4088 * a corefile is truncated instead of not being created,
4089 * if it is larger than the limit.
4091 limit = (off_t)lim_cur(td, RLIMIT_CORE);
4092 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
4098 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
4099 compress_user_cores, p->p_sig, &vp, &name);
4104 * Don't dump to non-regular files or files with links.
4105 * Do not dump into system files. Effective user must own the corefile.
4107 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
4108 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
4109 vattr.va_uid != cred->cr_uid) {
4117 /* Postpone other writers, including core dumps of other processes. */
4118 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4120 lf.l_whence = SEEK_SET;
4123 lf.l_type = F_WRLCK;
4124 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
4128 if (set_core_nodump_flag)
4129 vattr.va_flags = UF_NODUMP;
4130 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4131 VOP_SETATTR(vp, &vattr, cred);
4134 p->p_acflag |= ACORE;
4137 if (p->p_sysent->sv_coredump != NULL) {
4138 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
4144 lf.l_type = F_UNLCK;
4145 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4147 vn_rangelock_unlock(vp, rl_cookie);
4150 * Notify the userland helper that a process triggered a core dump.
4151 * This allows the helper to run an automated debugging session.
4153 if (error != 0 || coredump_devctl == 0)
4155 sb = sbuf_new_auto();
4156 if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4158 sbuf_printf(sb, "comm=\"");
4159 devctl_safe_quote_sb(sb, fullpath);
4160 free(freepath, M_TEMP);
4161 sbuf_printf(sb, "\" core=\"");
4164 * We can't lookup core file vp directly. When we're replacing a core, and
4165 * other random times, we flush the name cache, so it will fail. Instead,
4166 * if the path of the core is relative, add the current dir in front if it.
4168 if (name[0] != '/') {
4169 fullpathsize = MAXPATHLEN;
4170 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4171 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
4172 free(freepath, M_TEMP);
4175 devctl_safe_quote_sb(sb, fullpath);
4176 free(freepath, M_TEMP);
4179 devctl_safe_quote_sb(sb, name);
4180 sbuf_printf(sb, "\"");
4181 if (sbuf_finish(sb) == 0)
4182 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4186 error1 = vn_close(vp, FWRITE, cred, td);
4190 audit_proc_coredump(td, name, error);
4197 * Nonexistent system call-- signal process (may want to handle it). Flag
4198 * error in case process won't see signal immediately (blocked or ignored).
4200 #ifndef _SYS_SYSPROTO_H_
4207 nosys(struct thread *td, struct nosys_args *args)
4214 tdsignal(td, SIGSYS);
4216 if (kern_lognosys == 1 || kern_lognosys == 3) {
4217 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4220 if (kern_lognosys == 2 || kern_lognosys == 3 ||
4221 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4222 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4229 * Send a SIGIO or SIGURG signal to a process or process group using stored
4230 * credentials rather than those of the current process.
4233 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4236 struct sigio *sigio;
4238 ksiginfo_init(&ksi);
4239 ksi.ksi_signo = sig;
4240 ksi.ksi_code = SI_KERNEL;
4244 if (sigio == NULL) {
4248 if (sigio->sio_pgid > 0) {
4249 PROC_LOCK(sigio->sio_proc);
4250 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4251 kern_psignal(sigio->sio_proc, sig);
4252 PROC_UNLOCK(sigio->sio_proc);
4253 } else if (sigio->sio_pgid < 0) {
4256 PGRP_LOCK(sigio->sio_pgrp);
4257 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4259 if (p->p_state == PRS_NORMAL &&
4260 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4261 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4262 kern_psignal(p, sig);
4265 PGRP_UNLOCK(sigio->sio_pgrp);
4271 filt_sigattach(struct knote *kn)
4273 struct proc *p = curproc;
4275 kn->kn_ptr.p_proc = p;
4276 kn->kn_flags |= EV_CLEAR; /* automatically set */
4278 knlist_add(p->p_klist, kn, 0);
4284 filt_sigdetach(struct knote *kn)
4286 struct proc *p = kn->kn_ptr.p_proc;
4288 knlist_remove(p->p_klist, kn, 0);
4292 * signal knotes are shared with proc knotes, so we apply a mask to
4293 * the hint in order to differentiate them from process hints. This
4294 * could be avoided by using a signal-specific knote list, but probably
4295 * isn't worth the trouble.
4298 filt_signal(struct knote *kn, long hint)
4301 if (hint & NOTE_SIGNAL) {
4302 hint &= ~NOTE_SIGNAL;
4304 if (kn->kn_id == hint)
4307 return (kn->kn_data != 0);
4315 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4316 refcount_init(&ps->ps_refcnt, 1);
4317 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4322 sigacts_free(struct sigacts *ps)
4325 if (refcount_release(&ps->ps_refcnt) == 0)
4327 mtx_destroy(&ps->ps_mtx);
4328 free(ps, M_SUBPROC);
4332 sigacts_hold(struct sigacts *ps)
4335 refcount_acquire(&ps->ps_refcnt);
4340 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4343 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4344 mtx_lock(&src->ps_mtx);
4345 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4346 mtx_unlock(&src->ps_mtx);
4350 sigacts_shared(struct sigacts *ps)
4353 return (ps->ps_refcnt > 1);
4357 sig_drop_caught(struct proc *p)
4363 PROC_LOCK_ASSERT(p, MA_OWNED);
4364 mtx_assert(&ps->ps_mtx, MA_OWNED);
4365 SIG_FOREACH(sig, &ps->ps_sigcatch) {
4367 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4368 sigqueue_delete_proc(p, sig);
4373 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4378 * Prevent further fetches and SIGSEGVs, allowing thread to
4379 * issue syscalls despite corruption.
4381 sigfastblock_clear(td);
4385 ksiginfo_init_trap(&ksi);
4386 ksi.ksi_signo = SIGSEGV;
4387 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4388 ksi.ksi_addr = td->td_sigblock_ptr;
4389 trapsignal(td, &ksi);
4393 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4397 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4399 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4400 sigfastblock_failed(td, sendsig, false);
4404 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4409 sigfastblock_resched(struct thread *td, bool resched)
4416 reschedule_signals(p, td->td_sigmask, 0);
4419 ast_sched(td, TDA_SIG);
4423 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4432 case SIGFASTBLOCK_SETPTR:
4433 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4437 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4441 td->td_pflags |= TDP_SIGFASTBLOCK;
4442 td->td_sigblock_ptr = uap->ptr;
4445 case SIGFASTBLOCK_UNBLOCK:
4446 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4452 res = casueword32(td->td_sigblock_ptr,
4453 SIGFASTBLOCK_PEND, &oldval, 0);
4456 sigfastblock_failed(td, false, true);
4462 if (oldval != SIGFASTBLOCK_PEND) {
4466 error = thread_check_susp(td, false);
4474 * td_sigblock_val is cleared there, but not on a
4475 * syscall exit. The end effect is that a single
4476 * interruptible sleep, while user sigblock word is
4477 * set, might return EINTR or ERESTART to usermode
4478 * without delivering signal. All further sleeps,
4479 * until userspace clears the word and does
4480 * sigfastblock(UNBLOCK), observe current word and no
4481 * longer get interrupted. It is slight
4482 * non-conformance, with alternative to have read the
4483 * sigblock word on each syscall entry.
4485 td->td_sigblock_val = 0;
4488 * Rely on normal ast mechanism to deliver pending
4489 * signals to current thread. But notify others about
4492 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4496 case SIGFASTBLOCK_UNSETPTR:
4497 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4501 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4505 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4509 sigfastblock_clear(td);
4520 sigfastblock_clear(struct thread *td)
4524 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4526 td->td_sigblock_val = 0;
4527 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4529 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4530 sigfastblock_resched(td, resched);
4534 sigfastblock_fetch(struct thread *td)
4538 (void)sigfastblock_fetch_sig(td, true, &val);
4542 sigfastblock_setpend1(struct thread *td)
4547 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4549 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4551 sigfastblock_failed(td, true, false);
4555 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4556 oldval | SIGFASTBLOCK_PEND);
4558 sigfastblock_failed(td, true, true);
4562 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4563 td->td_pflags &= ~TDP_SIGFASTPENDING;
4567 if (thread_check_susp(td, false) != 0)
4573 sigfastblock_setpend(struct thread *td, bool resched)
4577 sigfastblock_setpend1(td);
4581 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);