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
37 #include "opt_capsicum.h"
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/ctype.h>
43 #include <sys/systm.h>
44 #include <sys/signalvar.h>
45 #include <sys/vnode.h>
47 #include <sys/capsicum.h>
48 #include <sys/compressor.h>
49 #include <sys/condvar.h>
50 #include <sys/devctl.h>
51 #include <sys/event.h>
52 #include <sys/fcntl.h>
53 #include <sys/imgact.h>
55 #include <sys/kernel.h>
57 #include <sys/ktrace.h>
58 #include <sys/limits.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/refcount.h>
63 #include <sys/namei.h>
65 #include <sys/procdesc.h>
66 #include <sys/ptrace.h>
67 #include <sys/posix4.h>
68 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
72 #include <sys/sleepqueue.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysctl.h>
79 #include <sys/sysent.h>
80 #include <sys/syslog.h>
81 #include <sys/sysproto.h>
82 #include <sys/timers.h>
83 #include <sys/unistd.h>
84 #include <sys/vmmeter.h>
87 #include <vm/vm_extern.h>
90 #include <machine/cpu.h>
92 #include <security/audit/audit.h>
94 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
96 SDT_PROVIDER_DECLARE(proc);
97 SDT_PROBE_DEFINE3(proc, , , signal__send,
98 "struct thread *", "struct proc *", "int");
99 SDT_PROBE_DEFINE2(proc, , , signal__clear,
100 "int", "ksiginfo_t *");
101 SDT_PROBE_DEFINE3(proc, , , signal__discard,
102 "struct thread *", "struct proc *", "int");
104 static int coredump(struct thread *);
105 static int killpg1(struct thread *td, int sig, int pgid, int all,
107 static int issignal(struct thread *td);
108 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
109 static int sigprop(int sig);
110 static void tdsigwakeup(struct thread *, int, sig_t, int);
111 static int sig_suspend_threads(struct thread *, struct proc *);
112 static int filt_sigattach(struct knote *kn);
113 static void filt_sigdetach(struct knote *kn);
114 static int filt_signal(struct knote *kn, long hint);
115 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
116 static void sigqueue_start(void);
117 static void sigfastblock_setpend(struct thread *td, bool resched);
119 static uma_zone_t ksiginfo_zone = NULL;
120 struct filterops sig_filtops = {
122 .f_attach = filt_sigattach,
123 .f_detach = filt_sigdetach,
124 .f_event = filt_signal,
127 static int kern_logsigexit = 1;
128 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
130 "Log processes quitting on abnormal signals to syslog(3)");
132 static int kern_forcesigexit = 1;
133 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
134 &kern_forcesigexit, 0, "Force trap signal to be handled");
136 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
137 "POSIX real time signal");
139 static int max_pending_per_proc = 128;
140 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
141 &max_pending_per_proc, 0, "Max pending signals per proc");
143 static int preallocate_siginfo = 1024;
144 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
145 &preallocate_siginfo, 0, "Preallocated signal memory size");
147 static int signal_overflow = 0;
148 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
149 &signal_overflow, 0, "Number of signals overflew");
151 static int signal_alloc_fail = 0;
152 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
153 &signal_alloc_fail, 0, "signals failed to be allocated");
155 static int kern_lognosys = 0;
156 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
157 "Log invalid syscalls");
159 static int kern_signosys = 1;
160 SYSCTL_INT(_kern, OID_AUTO, signosys, CTLFLAG_RWTUN, &kern_signosys, 0,
161 "Send SIGSYS on return from invalid syscall");
163 __read_frequently bool sigfastblock_fetch_always = false;
164 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
165 &sigfastblock_fetch_always, 0,
166 "Fetch sigfastblock word on each syscall entry for proper "
167 "blocking semantic");
169 static bool kern_sig_discard_ign = true;
170 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
171 &kern_sig_discard_ign, 0,
172 "Discard ignored signals on delivery, otherwise queue them to "
175 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
178 * Policy -- Can ucred cr1 send SIGIO to process cr2?
179 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
180 * in the right situations.
182 #define CANSIGIO(cr1, cr2) \
183 ((cr1)->cr_uid == 0 || \
184 (cr1)->cr_ruid == (cr2)->cr_ruid || \
185 (cr1)->cr_uid == (cr2)->cr_ruid || \
186 (cr1)->cr_ruid == (cr2)->cr_uid || \
187 (cr1)->cr_uid == (cr2)->cr_uid)
189 static int sugid_coredump;
190 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
191 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
193 static int capmode_coredump;
194 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
195 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
197 static int do_coredump = 1;
198 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
199 &do_coredump, 0, "Enable/Disable coredumps");
201 static int set_core_nodump_flag = 0;
202 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
203 0, "Enable setting the NODUMP flag on coredump files");
205 static int coredump_devctl = 0;
206 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
207 0, "Generate a devctl notification when processes coredump");
210 * Signal properties and actions.
211 * The array below categorizes the signals and their default actions
212 * according to the following properties:
214 #define SIGPROP_KILL 0x01 /* terminates process by default */
215 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
216 #define SIGPROP_STOP 0x04 /* suspend process */
217 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
218 #define SIGPROP_IGNORE 0x10 /* ignore by default */
219 #define SIGPROP_CONT 0x20 /* continue if suspended */
221 static const int sigproptbl[NSIG] = {
222 [SIGHUP] = SIGPROP_KILL,
223 [SIGINT] = SIGPROP_KILL,
224 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
225 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
226 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
227 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
228 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
229 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
230 [SIGKILL] = SIGPROP_KILL,
231 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
232 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
233 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
234 [SIGPIPE] = SIGPROP_KILL,
235 [SIGALRM] = SIGPROP_KILL,
236 [SIGTERM] = SIGPROP_KILL,
237 [SIGURG] = SIGPROP_IGNORE,
238 [SIGSTOP] = SIGPROP_STOP,
239 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
240 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
241 [SIGCHLD] = SIGPROP_IGNORE,
242 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
243 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
244 [SIGIO] = SIGPROP_IGNORE,
245 [SIGXCPU] = SIGPROP_KILL,
246 [SIGXFSZ] = SIGPROP_KILL,
247 [SIGVTALRM] = SIGPROP_KILL,
248 [SIGPROF] = SIGPROP_KILL,
249 [SIGWINCH] = SIGPROP_IGNORE,
250 [SIGINFO] = SIGPROP_IGNORE,
251 [SIGUSR1] = SIGPROP_KILL,
252 [SIGUSR2] = SIGPROP_KILL,
255 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
259 int __sig = ffs(__bits); \
260 __bits &= ~(1u << (__sig - 1)); \
261 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
265 if (++__i == _SIG_WORDS) { \
269 __bits = (set)->__bits[__i]; \
274 #define SIG_FOREACH(i, set) \
275 for (int32_t __i = -1, __bits = 0; \
276 _SIG_FOREACH_ADVANCE(i, set); ) \
278 static sigset_t fastblock_mask;
281 ast_sig(struct thread *td, int tda)
284 int old_boundary, sig;
290 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
291 TDAI(TDA_AST))) == 0) {
295 * Note that TDA_SIG should be re-read from
296 * td_ast, since signal might have been delivered
297 * after we cleared td_flags above. This is one of
298 * the reason for looping check for AST condition.
299 * See comment in userret() about P_PPWAIT.
301 if ((p->p_flag & P_PPWAIT) == 0 &&
302 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
303 if (SIGPENDING(td) && ((tda | td->td_ast) &
304 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
305 thread_unlock(td); /* fix dumps */
307 "failed2 to set signal flags for ast p %p "
308 "td %p tda %#x td_ast %#x fl %#x",
309 p, td, tda, td->td_ast, td->td_flags);
318 * Check for signals. Unlocked reads of p_pendingcnt or
319 * p_siglist might cause process-directed signal to be handled
322 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
323 !SIGISEMPTY(p->p_siglist)) {
324 sigfastblock_fetch(td);
326 old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY);
327 td->td_dbgflags |= TDB_BOUNDARY;
328 mtx_lock(&p->p_sigacts->ps_mtx);
329 while ((sig = cursig(td)) != 0) {
330 KASSERT(sig >= 0, ("sig %d", sig));
333 mtx_unlock(&p->p_sigacts->ps_mtx);
334 td->td_dbgflags &= old_boundary;
338 resched_sigs = false;
342 * Handle deferred update of the fast sigblock value, after
343 * the postsig() loop was performed.
345 sigfastblock_setpend(td, resched_sigs);
349 ast_sigsuspend(struct thread *td, int tda __unused)
351 MPASS((td->td_pflags & TDP_OLDMASK) != 0);
352 td->td_pflags &= ~TDP_OLDMASK;
353 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
359 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
360 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
361 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
362 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
363 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
364 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
365 SIGFILLSET(fastblock_mask);
366 SIG_CANTMASK(fastblock_mask);
367 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
368 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
369 TDP_OLDMASK, ast_sigsuspend);
373 ksiginfo_alloc(int mwait)
375 MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
377 if (ksiginfo_zone == NULL)
379 return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
383 ksiginfo_free(ksiginfo_t *ksi)
385 uma_zfree(ksiginfo_zone, ksi);
389 ksiginfo_tryfree(ksiginfo_t *ksi)
391 if ((ksi->ksi_flags & KSI_EXT) == 0) {
392 uma_zfree(ksiginfo_zone, ksi);
399 sigqueue_init(sigqueue_t *list, struct proc *p)
401 SIGEMPTYSET(list->sq_signals);
402 SIGEMPTYSET(list->sq_kill);
403 SIGEMPTYSET(list->sq_ptrace);
404 TAILQ_INIT(&list->sq_list);
406 list->sq_flags = SQ_INIT;
410 * Get a signal's ksiginfo.
412 * 0 - signal not found
413 * others - signal number
416 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
418 struct proc *p = sq->sq_proc;
419 struct ksiginfo *ksi, *next;
422 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
424 if (!SIGISMEMBER(sq->sq_signals, signo))
427 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
429 SIGDELSET(sq->sq_ptrace, signo);
430 si->ksi_flags |= KSI_PTRACE;
432 if (SIGISMEMBER(sq->sq_kill, signo)) {
435 SIGDELSET(sq->sq_kill, signo);
438 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
439 if (ksi->ksi_signo == signo) {
441 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
442 ksi->ksi_sigq = NULL;
443 ksiginfo_copy(ksi, si);
444 if (ksiginfo_tryfree(ksi) && p != NULL)
453 SIGDELSET(sq->sq_signals, signo);
454 si->ksi_signo = signo;
459 sigqueue_take(ksiginfo_t *ksi)
465 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
469 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
470 ksi->ksi_sigq = NULL;
471 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
474 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
475 kp = TAILQ_NEXT(kp, ksi_link)) {
476 if (kp->ksi_signo == ksi->ksi_signo)
479 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
480 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
481 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
485 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
487 struct proc *p = sq->sq_proc;
488 struct ksiginfo *ksi;
491 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
494 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
497 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
498 SIGADDSET(sq->sq_kill, signo);
502 /* directly insert the ksi, don't copy it */
503 if (si->ksi_flags & KSI_INS) {
504 if (si->ksi_flags & KSI_HEAD)
505 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
507 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
512 if (__predict_false(ksiginfo_zone == NULL)) {
513 SIGADDSET(sq->sq_kill, signo);
517 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
520 } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
526 ksiginfo_copy(si, ksi);
527 ksi->ksi_signo = signo;
528 if (si->ksi_flags & KSI_HEAD)
529 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
531 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
536 if ((si->ksi_flags & KSI_PTRACE) != 0) {
537 SIGADDSET(sq->sq_ptrace, signo);
540 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
541 (si->ksi_flags & KSI_SIGQ) == 0) {
542 SIGADDSET(sq->sq_kill, signo);
550 SIGADDSET(sq->sq_signals, signo);
555 sigqueue_flush(sigqueue_t *sq)
557 struct proc *p = sq->sq_proc;
560 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
563 PROC_LOCK_ASSERT(p, MA_OWNED);
565 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
566 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
567 ksi->ksi_sigq = NULL;
568 if (ksiginfo_tryfree(ksi) && p != NULL)
572 SIGEMPTYSET(sq->sq_signals);
573 SIGEMPTYSET(sq->sq_kill);
574 SIGEMPTYSET(sq->sq_ptrace);
578 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
581 struct proc *p1, *p2;
582 ksiginfo_t *ksi, *next;
584 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
585 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
588 /* Move siginfo to target list */
589 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
590 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
591 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
594 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
601 /* Move pending bits to target list */
603 SIGSETAND(tmp, *set);
604 SIGSETOR(dst->sq_kill, tmp);
605 SIGSETNAND(src->sq_kill, tmp);
607 tmp = src->sq_ptrace;
608 SIGSETAND(tmp, *set);
609 SIGSETOR(dst->sq_ptrace, tmp);
610 SIGSETNAND(src->sq_ptrace, tmp);
612 tmp = src->sq_signals;
613 SIGSETAND(tmp, *set);
614 SIGSETOR(dst->sq_signals, tmp);
615 SIGSETNAND(src->sq_signals, tmp);
620 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
625 SIGADDSET(set, signo);
626 sigqueue_move_set(src, dst, &set);
631 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
633 struct proc *p = sq->sq_proc;
634 ksiginfo_t *ksi, *next;
636 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
638 /* Remove siginfo queue */
639 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
640 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
641 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
642 ksi->ksi_sigq = NULL;
643 if (ksiginfo_tryfree(ksi) && p != NULL)
647 SIGSETNAND(sq->sq_kill, *set);
648 SIGSETNAND(sq->sq_ptrace, *set);
649 SIGSETNAND(sq->sq_signals, *set);
653 sigqueue_delete(sigqueue_t *sq, int signo)
658 SIGADDSET(set, signo);
659 sigqueue_delete_set(sq, &set);
662 /* Remove a set of signals for a process */
664 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
669 PROC_LOCK_ASSERT(p, MA_OWNED);
671 sigqueue_init(&worklist, NULL);
672 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
674 FOREACH_THREAD_IN_PROC(p, td0)
675 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
677 sigqueue_flush(&worklist);
681 sigqueue_delete_proc(struct proc *p, int signo)
686 SIGADDSET(set, signo);
687 sigqueue_delete_set_proc(p, &set);
691 sigqueue_delete_stopmask_proc(struct proc *p)
696 SIGADDSET(set, SIGSTOP);
697 SIGADDSET(set, SIGTSTP);
698 SIGADDSET(set, SIGTTIN);
699 SIGADDSET(set, SIGTTOU);
700 sigqueue_delete_set_proc(p, &set);
704 * Determine signal that should be delivered to thread td, the current
705 * thread, 0 if none. If there is a pending stop signal with default
706 * action, the process stops in issignal().
709 cursig(struct thread *td)
711 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
712 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
713 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
714 return (SIGPENDING(td) ? issignal(td) : 0);
718 * Arrange for ast() to handle unmasked pending signals on return to user
719 * mode. This must be called whenever a signal is added to td_sigqueue or
720 * unmasked in td_sigmask.
723 signotify(struct thread *td)
726 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
729 ast_sched(td, TDA_SIG);
733 * Returns 1 (true) if altstack is configured for the thread, and the
734 * passed stack bottom address falls into the altstack range. Handles
735 * the 43 compat special case where the alt stack size is zero.
738 sigonstack(size_t sp)
743 if ((td->td_pflags & TDP_ALTSTACK) == 0)
745 #if defined(COMPAT_43)
746 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
747 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
749 return (sp >= (size_t)td->td_sigstk.ss_sp &&
750 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
757 if (sig > 0 && sig < nitems(sigproptbl))
758 return (sigproptbl[sig]);
763 sigact_flag_test(const struct sigaction *act, int flag)
767 * SA_SIGINFO is reset when signal disposition is set to
768 * ignore or default. Other flags are kept according to user
771 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
772 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
773 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
783 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
784 struct sigaction *oact, int flags)
787 struct proc *p = td->td_proc;
789 if (!_SIG_VALID(sig))
791 if (act != NULL && act->sa_handler != SIG_DFL &&
792 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
793 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
794 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
799 mtx_lock(&ps->ps_mtx);
801 memset(oact, 0, sizeof(*oact));
802 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
803 if (SIGISMEMBER(ps->ps_sigonstack, sig))
804 oact->sa_flags |= SA_ONSTACK;
805 if (!SIGISMEMBER(ps->ps_sigintr, sig))
806 oact->sa_flags |= SA_RESTART;
807 if (SIGISMEMBER(ps->ps_sigreset, sig))
808 oact->sa_flags |= SA_RESETHAND;
809 if (SIGISMEMBER(ps->ps_signodefer, sig))
810 oact->sa_flags |= SA_NODEFER;
811 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
812 oact->sa_flags |= SA_SIGINFO;
814 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
816 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
817 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
818 oact->sa_flags |= SA_NOCLDSTOP;
819 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
820 oact->sa_flags |= SA_NOCLDWAIT;
823 if ((sig == SIGKILL || sig == SIGSTOP) &&
824 act->sa_handler != SIG_DFL) {
825 mtx_unlock(&ps->ps_mtx);
831 * Change setting atomically.
834 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
835 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
836 if (sigact_flag_test(act, SA_SIGINFO)) {
837 ps->ps_sigact[_SIG_IDX(sig)] =
838 (__sighandler_t *)act->sa_sigaction;
839 SIGADDSET(ps->ps_siginfo, sig);
841 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
842 SIGDELSET(ps->ps_siginfo, sig);
844 if (!sigact_flag_test(act, SA_RESTART))
845 SIGADDSET(ps->ps_sigintr, sig);
847 SIGDELSET(ps->ps_sigintr, sig);
848 if (sigact_flag_test(act, SA_ONSTACK))
849 SIGADDSET(ps->ps_sigonstack, sig);
851 SIGDELSET(ps->ps_sigonstack, sig);
852 if (sigact_flag_test(act, SA_RESETHAND))
853 SIGADDSET(ps->ps_sigreset, sig);
855 SIGDELSET(ps->ps_sigreset, sig);
856 if (sigact_flag_test(act, SA_NODEFER))
857 SIGADDSET(ps->ps_signodefer, sig);
859 SIGDELSET(ps->ps_signodefer, sig);
860 if (sig == SIGCHLD) {
861 if (act->sa_flags & SA_NOCLDSTOP)
862 ps->ps_flag |= PS_NOCLDSTOP;
864 ps->ps_flag &= ~PS_NOCLDSTOP;
865 if (act->sa_flags & SA_NOCLDWAIT) {
867 * Paranoia: since SA_NOCLDWAIT is implemented
868 * by reparenting the dying child to PID 1 (and
869 * trust it to reap the zombie), PID 1 itself
870 * is forbidden to set SA_NOCLDWAIT.
873 ps->ps_flag &= ~PS_NOCLDWAIT;
875 ps->ps_flag |= PS_NOCLDWAIT;
877 ps->ps_flag &= ~PS_NOCLDWAIT;
878 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
879 ps->ps_flag |= PS_CLDSIGIGN;
881 ps->ps_flag &= ~PS_CLDSIGIGN;
884 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
885 * and for signals set to SIG_DFL where the default is to
886 * ignore. However, don't put SIGCONT in ps_sigignore, as we
887 * have to restart the process.
889 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
890 (sigprop(sig) & SIGPROP_IGNORE &&
891 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
892 /* never to be seen again */
893 sigqueue_delete_proc(p, sig);
895 /* easier in psignal */
896 SIGADDSET(ps->ps_sigignore, sig);
897 SIGDELSET(ps->ps_sigcatch, sig);
899 SIGDELSET(ps->ps_sigignore, sig);
900 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
901 SIGDELSET(ps->ps_sigcatch, sig);
903 SIGADDSET(ps->ps_sigcatch, sig);
905 #ifdef COMPAT_FREEBSD4
906 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
907 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
908 (flags & KSA_FREEBSD4) == 0)
909 SIGDELSET(ps->ps_freebsd4, sig);
911 SIGADDSET(ps->ps_freebsd4, sig);
914 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
915 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
916 (flags & KSA_OSIGSET) == 0)
917 SIGDELSET(ps->ps_osigset, sig);
919 SIGADDSET(ps->ps_osigset, sig);
922 mtx_unlock(&ps->ps_mtx);
927 #ifndef _SYS_SYSPROTO_H_
928 struct sigaction_args {
930 struct sigaction *act;
931 struct sigaction *oact;
935 sys_sigaction(struct thread *td, struct sigaction_args *uap)
937 struct sigaction act, oact;
938 struct sigaction *actp, *oactp;
941 actp = (uap->act != NULL) ? &act : NULL;
942 oactp = (uap->oact != NULL) ? &oact : NULL;
944 error = copyin(uap->act, actp, sizeof(act));
948 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
950 error = copyout(oactp, uap->oact, sizeof(oact));
954 #ifdef COMPAT_FREEBSD4
955 #ifndef _SYS_SYSPROTO_H_
956 struct freebsd4_sigaction_args {
958 struct sigaction *act;
959 struct sigaction *oact;
963 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
965 struct sigaction act, oact;
966 struct sigaction *actp, *oactp;
969 actp = (uap->act != NULL) ? &act : NULL;
970 oactp = (uap->oact != NULL) ? &oact : NULL;
972 error = copyin(uap->act, actp, sizeof(act));
976 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
978 error = copyout(oactp, uap->oact, sizeof(oact));
981 #endif /* COMAPT_FREEBSD4 */
983 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
984 #ifndef _SYS_SYSPROTO_H_
985 struct osigaction_args {
987 struct osigaction *nsa;
988 struct osigaction *osa;
992 osigaction(struct thread *td, struct osigaction_args *uap)
994 struct osigaction sa;
995 struct sigaction nsa, osa;
996 struct sigaction *nsap, *osap;
999 if (uap->signum <= 0 || uap->signum >= ONSIG)
1002 nsap = (uap->nsa != NULL) ? &nsa : NULL;
1003 osap = (uap->osa != NULL) ? &osa : NULL;
1006 error = copyin(uap->nsa, &sa, sizeof(sa));
1009 nsap->sa_handler = sa.sa_handler;
1010 nsap->sa_flags = sa.sa_flags;
1011 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
1013 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1014 if (osap && !error) {
1015 sa.sa_handler = osap->sa_handler;
1016 sa.sa_flags = osap->sa_flags;
1017 SIG2OSIG(osap->sa_mask, sa.sa_mask);
1018 error = copyout(&sa, uap->osa, sizeof(sa));
1023 #if !defined(__i386__)
1024 /* Avoid replicating the same stub everywhere */
1026 osigreturn(struct thread *td, struct osigreturn_args *uap)
1029 return (nosys(td, (struct nosys_args *)uap));
1032 #endif /* COMPAT_43 */
1035 * Initialize signal state for process 0;
1036 * set to ignore signals that are ignored by default.
1039 siginit(struct proc *p)
1046 mtx_lock(&ps->ps_mtx);
1047 for (i = 1; i <= NSIG; i++) {
1048 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
1049 SIGADDSET(ps->ps_sigignore, i);
1052 mtx_unlock(&ps->ps_mtx);
1057 * Reset specified signal to the default disposition.
1060 sigdflt(struct sigacts *ps, int sig)
1063 mtx_assert(&ps->ps_mtx, MA_OWNED);
1064 SIGDELSET(ps->ps_sigcatch, sig);
1065 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1066 SIGADDSET(ps->ps_sigignore, sig);
1067 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1068 SIGDELSET(ps->ps_siginfo, sig);
1072 * Reset signals for an exec of the specified process.
1075 execsigs(struct proc *p)
1081 * Reset caught signals. Held signals remain held
1082 * through td_sigmask (unless they were caught,
1083 * and are now ignored by default).
1085 PROC_LOCK_ASSERT(p, MA_OWNED);
1087 mtx_lock(&ps->ps_mtx);
1091 * Reset stack state to the user stack.
1092 * Clear set of signals caught on the signal stack.
1095 MPASS(td->td_proc == p);
1096 td->td_sigstk.ss_flags = SS_DISABLE;
1097 td->td_sigstk.ss_size = 0;
1098 td->td_sigstk.ss_sp = 0;
1099 td->td_pflags &= ~TDP_ALTSTACK;
1101 * Reset no zombies if child dies flag as Solaris does.
1103 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1104 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1105 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1106 mtx_unlock(&ps->ps_mtx);
1110 * kern_sigprocmask()
1112 * Manipulate signal mask.
1115 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1118 sigset_t new_block, oset1;
1123 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1124 PROC_LOCK_ASSERT(p, MA_OWNED);
1127 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1128 ? MA_OWNED : MA_NOTOWNED);
1130 *oset = td->td_sigmask;
1137 oset1 = td->td_sigmask;
1138 SIGSETOR(td->td_sigmask, *set);
1139 new_block = td->td_sigmask;
1140 SIGSETNAND(new_block, oset1);
1143 SIGSETNAND(td->td_sigmask, *set);
1148 oset1 = td->td_sigmask;
1149 if (flags & SIGPROCMASK_OLD)
1150 SIGSETLO(td->td_sigmask, *set);
1152 td->td_sigmask = *set;
1153 new_block = td->td_sigmask;
1154 SIGSETNAND(new_block, oset1);
1163 * The new_block set contains signals that were not previously
1164 * blocked, but are blocked now.
1166 * In case we block any signal that was not previously blocked
1167 * for td, and process has the signal pending, try to schedule
1168 * signal delivery to some thread that does not block the
1169 * signal, possibly waking it up.
1171 if (p->p_numthreads != 1)
1172 reschedule_signals(p, new_block, flags);
1176 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1181 #ifndef _SYS_SYSPROTO_H_
1182 struct sigprocmask_args {
1184 const sigset_t *set;
1189 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1192 sigset_t *setp, *osetp;
1195 setp = (uap->set != NULL) ? &set : NULL;
1196 osetp = (uap->oset != NULL) ? &oset : NULL;
1198 error = copyin(uap->set, setp, sizeof(set));
1202 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1203 if (osetp && !error) {
1204 error = copyout(osetp, uap->oset, sizeof(oset));
1209 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1210 #ifndef _SYS_SYSPROTO_H_
1211 struct osigprocmask_args {
1217 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1222 OSIG2SIG(uap->mask, set);
1223 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1224 SIG2OSIG(oset, td->td_retval[0]);
1227 #endif /* COMPAT_43 */
1230 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1236 error = copyin(uap->set, &set, sizeof(set));
1238 td->td_retval[0] = error;
1242 error = kern_sigtimedwait(td, set, &ksi, NULL);
1245 * sigwait() function shall not return EINTR, but
1246 * the syscall does. Non-ancient libc provides the
1247 * wrapper which hides EINTR. Otherwise, EINTR return
1248 * is used by libthr to handle required cancellation
1249 * point in the sigwait().
1251 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1253 td->td_retval[0] = error;
1257 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1258 td->td_retval[0] = error;
1263 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1266 struct timespec *timeout;
1272 error = copyin(uap->timeout, &ts, sizeof(ts));
1280 error = copyin(uap->set, &set, sizeof(set));
1284 error = kern_sigtimedwait(td, set, &ksi, timeout);
1289 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1292 td->td_retval[0] = ksi.ksi_signo;
1297 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1303 error = copyin(uap->set, &set, sizeof(set));
1307 error = kern_sigtimedwait(td, set, &ksi, NULL);
1312 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1315 td->td_retval[0] = ksi.ksi_signo;
1320 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1324 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1328 thr->td_si.si_signo = 0;
1333 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1334 struct timespec *timeout)
1337 sigset_t saved_mask, new_block;
1339 int error, sig, timevalid = 0;
1340 sbintime_t sbt, precision, tsbt;
1348 /* Ensure the sigfastblock value is up to date. */
1349 sigfastblock_fetch(td);
1351 if (timeout != NULL) {
1352 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1355 if (ts.tv_sec < INT32_MAX / 2) {
1358 precision >>= tc_precexp;
1359 if (TIMESEL(&sbt, tsbt))
1363 precision = sbt = 0;
1366 precision = sbt = 0;
1368 /* Some signals can not be waited for. */
1369 SIG_CANTMASK(waitset);
1372 saved_mask = td->td_sigmask;
1373 SIGSETNAND(td->td_sigmask, waitset);
1374 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1375 !kern_sig_discard_ign) {
1377 td->td_flags |= TDF_SIGWAIT;
1381 mtx_lock(&ps->ps_mtx);
1383 mtx_unlock(&ps->ps_mtx);
1384 KASSERT(sig >= 0, ("sig %d", sig));
1385 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1386 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1387 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1397 * POSIX says this must be checked after looking for pending
1400 if (timeout != NULL && !timevalid) {
1410 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1411 "sigwait", sbt, precision, C_ABSOLUTE);
1413 /* The syscalls can not be restarted. */
1414 if (error == ERESTART)
1418 * If PTRACE_SCE or PTRACE_SCX were set after
1419 * userspace entered the syscall, return spurious
1420 * EINTR after wait was done. Only do this as last
1421 * resort after rechecking for possible queued signals
1422 * and expired timeouts.
1424 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1428 td->td_flags &= ~TDF_SIGWAIT;
1431 new_block = saved_mask;
1432 SIGSETNAND(new_block, td->td_sigmask);
1433 td->td_sigmask = saved_mask;
1435 * Fewer signals can be delivered to us, reschedule signal
1438 if (p->p_numthreads != 1)
1439 reschedule_signals(p, new_block, 0);
1442 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1444 if (ksi->ksi_code == SI_TIMER)
1445 itimer_accept(p, ksi->ksi_timerid, ksi);
1448 if (KTRPOINT(td, KTR_PSIG)) {
1451 mtx_lock(&ps->ps_mtx);
1452 action = ps->ps_sigact[_SIG_IDX(sig)];
1453 mtx_unlock(&ps->ps_mtx);
1454 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1457 if (sig == SIGKILL) {
1458 proc_td_siginfo_capture(td, &ksi->ksi_info);
1466 #ifndef _SYS_SYSPROTO_H_
1467 struct sigpending_args {
1472 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1474 struct proc *p = td->td_proc;
1478 pending = p->p_sigqueue.sq_signals;
1479 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1481 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1484 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1485 #ifndef _SYS_SYSPROTO_H_
1486 struct osigpending_args {
1491 osigpending(struct thread *td, struct osigpending_args *uap)
1493 struct proc *p = td->td_proc;
1497 pending = p->p_sigqueue.sq_signals;
1498 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1500 SIG2OSIG(pending, td->td_retval[0]);
1503 #endif /* COMPAT_43 */
1505 #if defined(COMPAT_43)
1507 * Generalized interface signal handler, 4.3-compatible.
1509 #ifndef _SYS_SYSPROTO_H_
1510 struct osigvec_args {
1518 osigvec(struct thread *td, struct osigvec_args *uap)
1521 struct sigaction nsa, osa;
1522 struct sigaction *nsap, *osap;
1525 if (uap->signum <= 0 || uap->signum >= ONSIG)
1527 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1528 osap = (uap->osv != NULL) ? &osa : NULL;
1530 error = copyin(uap->nsv, &vec, sizeof(vec));
1533 nsap->sa_handler = vec.sv_handler;
1534 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1535 nsap->sa_flags = vec.sv_flags;
1536 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1538 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1539 if (osap && !error) {
1540 vec.sv_handler = osap->sa_handler;
1541 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1542 vec.sv_flags = osap->sa_flags;
1543 vec.sv_flags &= ~SA_NOCLDWAIT;
1544 vec.sv_flags ^= SA_RESTART;
1545 error = copyout(&vec, uap->osv, sizeof(vec));
1550 #ifndef _SYS_SYSPROTO_H_
1551 struct osigblock_args {
1556 osigblock(struct thread *td, struct osigblock_args *uap)
1560 OSIG2SIG(uap->mask, set);
1561 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1562 SIG2OSIG(oset, td->td_retval[0]);
1566 #ifndef _SYS_SYSPROTO_H_
1567 struct osigsetmask_args {
1572 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1576 OSIG2SIG(uap->mask, set);
1577 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1578 SIG2OSIG(oset, td->td_retval[0]);
1581 #endif /* COMPAT_43 */
1584 * Suspend calling thread until signal, providing mask to be set in the
1587 #ifndef _SYS_SYSPROTO_H_
1588 struct sigsuspend_args {
1589 const sigset_t *sigmask;
1594 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1599 error = copyin(uap->sigmask, &mask, sizeof(mask));
1602 return (kern_sigsuspend(td, mask));
1606 kern_sigsuspend(struct thread *td, sigset_t mask)
1608 struct proc *p = td->td_proc;
1611 /* Ensure the sigfastblock value is up to date. */
1612 sigfastblock_fetch(td);
1615 * When returning from sigsuspend, we want
1616 * the old mask to be restored after the
1617 * signal handler has finished. Thus, we
1618 * save it here and mark the sigacts structure
1622 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1623 SIGPROCMASK_PROC_LOCKED);
1624 td->td_pflags |= TDP_OLDMASK;
1625 ast_sched(td, TDA_SIGSUSPEND);
1628 * Process signals now. Otherwise, we can get spurious wakeup
1629 * due to signal entered process queue, but delivered to other
1630 * thread. But sigsuspend should return only on signal
1633 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1634 for (has_sig = 0; !has_sig;) {
1635 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1638 thread_suspend_check(0);
1639 mtx_lock(&p->p_sigacts->ps_mtx);
1640 while ((sig = cursig(td)) != 0) {
1641 KASSERT(sig >= 0, ("sig %d", sig));
1642 has_sig += postsig(sig);
1644 mtx_unlock(&p->p_sigacts->ps_mtx);
1647 * If PTRACE_SCE or PTRACE_SCX were set after
1648 * userspace entered the syscall, return spurious
1651 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1655 td->td_errno = EINTR;
1656 td->td_pflags |= TDP_NERRNO;
1657 return (EJUSTRETURN);
1660 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1662 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1663 * convention: libc stub passes mask, not pointer, to save a copyin.
1665 #ifndef _SYS_SYSPROTO_H_
1666 struct osigsuspend_args {
1672 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1676 OSIG2SIG(uap->mask, mask);
1677 return (kern_sigsuspend(td, mask));
1679 #endif /* COMPAT_43 */
1681 #if defined(COMPAT_43)
1682 #ifndef _SYS_SYSPROTO_H_
1683 struct osigstack_args {
1684 struct sigstack *nss;
1685 struct sigstack *oss;
1690 osigstack(struct thread *td, struct osigstack_args *uap)
1692 struct sigstack nss, oss;
1695 if (uap->nss != NULL) {
1696 error = copyin(uap->nss, &nss, sizeof(nss));
1700 oss.ss_sp = td->td_sigstk.ss_sp;
1701 oss.ss_onstack = sigonstack(cpu_getstack(td));
1702 if (uap->nss != NULL) {
1703 td->td_sigstk.ss_sp = nss.ss_sp;
1704 td->td_sigstk.ss_size = 0;
1705 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1706 td->td_pflags |= TDP_ALTSTACK;
1708 if (uap->oss != NULL)
1709 error = copyout(&oss, uap->oss, sizeof(oss));
1713 #endif /* COMPAT_43 */
1715 #ifndef _SYS_SYSPROTO_H_
1716 struct sigaltstack_args {
1723 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1728 if (uap->ss != NULL) {
1729 error = copyin(uap->ss, &ss, sizeof(ss));
1733 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1734 (uap->oss != NULL) ? &oss : NULL);
1737 if (uap->oss != NULL)
1738 error = copyout(&oss, uap->oss, sizeof(stack_t));
1743 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1745 struct proc *p = td->td_proc;
1748 oonstack = sigonstack(cpu_getstack(td));
1751 *oss = td->td_sigstk;
1752 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1753 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1759 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1761 if (!(ss->ss_flags & SS_DISABLE)) {
1762 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1765 td->td_sigstk = *ss;
1766 td->td_pflags |= TDP_ALTSTACK;
1768 td->td_pflags &= ~TDP_ALTSTACK;
1774 struct killpg1_ctx {
1784 killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
1788 err = p_cansignal(arg->td, p, arg->sig);
1789 if (err == 0 && arg->sig != 0)
1790 pksignal(p, arg->sig, arg->ksi);
1795 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1800 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1803 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1804 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1808 killpg1_sendsig_locked(p, arg);
1813 kill_processes_prison_cb(struct proc *p, void *arg)
1815 struct killpg1_ctx *ctx = arg;
1817 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1818 (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
1821 killpg1_sendsig_locked(p, ctx);
1825 * Common code for kill process group/broadcast kill.
1826 * td is the calling thread, as usual.
1829 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1833 struct killpg1_ctx arg;
1845 prison_proc_iterate(td->td_ucred->cr_prison,
1846 kill_processes_prison_cb, &arg);
1849 sx_slock(&proctree_lock);
1852 * zero pgid means send to my process group.
1854 pgrp = td->td_proc->p_pgrp;
1857 pgrp = pgfind(pgid);
1859 sx_sunlock(&proctree_lock);
1863 sx_sunlock(&proctree_lock);
1864 if (!sx_try_xlock(&pgrp->pg_killsx)) {
1866 sx_xlock(&pgrp->pg_killsx);
1867 sx_xunlock(&pgrp->pg_killsx);
1870 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1871 killpg1_sendsig(p, false, &arg);
1874 sx_xunlock(&pgrp->pg_killsx);
1876 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1877 if (arg.ret == 0 && !arg.sent)
1878 arg.ret = arg.found ? EPERM : ESRCH;
1882 #ifndef _SYS_SYSPROTO_H_
1890 sys_kill(struct thread *td, struct kill_args *uap)
1893 return (kern_kill(td, uap->pid, uap->signum));
1897 kern_kill(struct thread *td, pid_t pid, int signum)
1904 * A process in capability mode can send signals only to himself.
1905 * The main rationale behind this is that abort(3) is implemented as
1906 * kill(getpid(), SIGABRT).
1908 if (pid != td->td_proc->p_pid) {
1909 if (CAP_TRACING(td))
1910 ktrcapfail(CAPFAIL_SIGNAL, &signum);
1911 if (IN_CAPABILITY_MODE(td))
1915 AUDIT_ARG_SIGNUM(signum);
1917 if ((u_int)signum > _SIG_MAXSIG)
1920 ksiginfo_init(&ksi);
1921 ksi.ksi_signo = signum;
1922 ksi.ksi_code = SI_USER;
1923 ksi.ksi_pid = td->td_proc->p_pid;
1924 ksi.ksi_uid = td->td_ucred->cr_ruid;
1927 /* kill single process */
1928 if ((p = pfind_any(pid)) == NULL)
1930 AUDIT_ARG_PROCESS(p);
1931 error = p_cansignal(td, p, signum);
1932 if (error == 0 && signum)
1933 pksignal(p, signum, &ksi);
1938 case -1: /* broadcast signal */
1939 return (killpg1(td, signum, 0, 1, &ksi));
1940 case 0: /* signal own process group */
1941 return (killpg1(td, signum, 0, 0, &ksi));
1942 default: /* negative explicit process group */
1943 return (killpg1(td, signum, -pid, 0, &ksi));
1949 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1954 AUDIT_ARG_SIGNUM(uap->signum);
1955 AUDIT_ARG_FD(uap->fd);
1956 if ((u_int)uap->signum > _SIG_MAXSIG)
1959 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1962 AUDIT_ARG_PROCESS(p);
1963 error = p_cansignal(td, p, uap->signum);
1964 if (error == 0 && uap->signum)
1965 kern_psignal(p, uap->signum);
1970 #if defined(COMPAT_43)
1971 #ifndef _SYS_SYSPROTO_H_
1972 struct okillpg_args {
1979 okillpg(struct thread *td, struct okillpg_args *uap)
1983 AUDIT_ARG_SIGNUM(uap->signum);
1984 AUDIT_ARG_PID(uap->pgid);
1985 if ((u_int)uap->signum > _SIG_MAXSIG)
1988 ksiginfo_init(&ksi);
1989 ksi.ksi_signo = uap->signum;
1990 ksi.ksi_code = SI_USER;
1991 ksi.ksi_pid = td->td_proc->p_pid;
1992 ksi.ksi_uid = td->td_ucred->cr_ruid;
1993 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1995 #endif /* COMPAT_43 */
1997 #ifndef _SYS_SYSPROTO_H_
1998 struct sigqueue_args {
2001 /* union sigval */ void *value;
2005 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
2009 sv.sival_ptr = uap->value;
2011 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2015 kern_sigqueue(struct thread *td, pid_t pid, int signumf, union sigval *value)
2023 signum = signumf & ~__SIGQUEUE_TID;
2024 if (signum > _SIG_MAXSIG)
2028 * Specification says sigqueue can only send signal to
2034 if ((signumf & __SIGQUEUE_TID) == 0) {
2035 if ((p = pfind_any(pid)) == NULL)
2040 td2 = tdfind((lwpid_t)pid, p->p_pid);
2045 error = p_cansignal(td, p, signum);
2046 if (error == 0 && signum != 0) {
2047 ksiginfo_init(&ksi);
2048 ksi.ksi_flags = KSI_SIGQ;
2049 ksi.ksi_signo = signum;
2050 ksi.ksi_code = SI_QUEUE;
2051 ksi.ksi_pid = td->td_proc->p_pid;
2052 ksi.ksi_uid = td->td_ucred->cr_ruid;
2053 ksi.ksi_value = *value;
2054 error = tdsendsignal(p, td2, ksi.ksi_signo, &ksi);
2061 * Send a signal to a process group. If checktty is 1,
2062 * limit to members which have a controlling terminal.
2065 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2070 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2071 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2073 if (p->p_state == PRS_NORMAL &&
2074 (checkctty == 0 || p->p_flag & P_CONTROLT))
2075 pksignal(p, sig, ksi);
2082 * Recalculate the signal mask and reset the signal disposition after
2083 * usermode frame for delivery is formed. Should be called after
2084 * mach-specific routine, because sysent->sv_sendsig() needs correct
2085 * ps_siginfo and signal mask.
2088 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2092 mtx_assert(&ps->ps_mtx, MA_OWNED);
2093 td->td_ru.ru_nsignals++;
2094 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2095 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2096 SIGADDSET(mask, sig);
2097 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2098 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2099 if (SIGISMEMBER(ps->ps_sigreset, sig))
2104 * Send a signal caused by a trap to the current thread. If it will be
2105 * caught immediately, deliver it with correct code. Otherwise, post it
2109 trapsignal(struct thread *td, ksiginfo_t *ksi)
2117 sig = ksi->ksi_signo;
2118 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2120 sigfastblock_fetch(td);
2123 mtx_lock(&ps->ps_mtx);
2124 sigmask = td->td_sigmask;
2125 if (td->td_sigblock_val != 0)
2126 SIGSETOR(sigmask, fastblock_mask);
2127 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2128 !SIGISMEMBER(sigmask, sig)) {
2130 if (KTRPOINT(curthread, KTR_PSIG))
2131 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2132 &td->td_sigmask, ksi->ksi_code);
2134 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2135 ksi, &td->td_sigmask);
2136 postsig_done(sig, td, ps);
2137 mtx_unlock(&ps->ps_mtx);
2140 * Avoid a possible infinite loop if the thread
2141 * masking the signal or process is ignoring the
2144 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2145 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2146 SIGDELSET(td->td_sigmask, sig);
2147 SIGDELSET(ps->ps_sigcatch, sig);
2148 SIGDELSET(ps->ps_sigignore, sig);
2149 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2150 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2151 td->td_sigblock_val = 0;
2153 mtx_unlock(&ps->ps_mtx);
2154 p->p_sig = sig; /* XXX to verify code */
2155 tdsendsignal(p, td, sig, ksi);
2160 static struct thread *
2161 sigtd(struct proc *p, int sig, bool fast_sigblock)
2163 struct thread *td, *signal_td;
2165 PROC_LOCK_ASSERT(p, MA_OWNED);
2166 MPASS(!fast_sigblock || p == curproc);
2169 * Check if current thread can handle the signal without
2170 * switching context to another thread.
2172 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2173 (!fast_sigblock || curthread->td_sigblock_val == 0))
2176 /* Find a non-stopped thread that does not mask the signal. */
2178 FOREACH_THREAD_IN_PROC(p, td) {
2179 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2180 td != curthread || td->td_sigblock_val == 0) &&
2181 (td->td_flags & TDF_BOUNDARY) == 0) {
2186 /* Select random (first) thread if no better match was found. */
2187 if (signal_td == NULL)
2188 signal_td = FIRST_THREAD_IN_PROC(p);
2193 * Send the signal to the process. If the signal has an action, the action
2194 * is usually performed by the target process rather than the caller; we add
2195 * the signal to the set of pending signals for the process.
2198 * o When a stop signal is sent to a sleeping process that takes the
2199 * default action, the process is stopped without awakening it.
2200 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2201 * regardless of the signal action (eg, blocked or ignored).
2203 * Other ignored signals are discarded immediately.
2205 * NB: This function may be entered from the debugger via the "kill" DDB
2206 * command. There is little that can be done to mitigate the possibly messy
2207 * side effects of this unwise possibility.
2210 kern_psignal(struct proc *p, int sig)
2214 ksiginfo_init(&ksi);
2215 ksi.ksi_signo = sig;
2216 ksi.ksi_code = SI_KERNEL;
2217 (void) tdsendsignal(p, NULL, sig, &ksi);
2221 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2224 return (tdsendsignal(p, NULL, sig, ksi));
2227 /* Utility function for finding a thread to send signal event to. */
2229 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2233 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2234 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2246 tdsignal(struct thread *td, int sig)
2250 ksiginfo_init(&ksi);
2251 ksi.ksi_signo = sig;
2252 ksi.ksi_code = SI_KERNEL;
2253 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2257 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2260 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2264 sig_sleepq_abort(struct thread *td, int intrval)
2266 THREAD_LOCK_ASSERT(td, MA_OWNED);
2268 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2272 return (sleepq_abort(td, intrval));
2276 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2279 sigqueue_t *sigqueue;
2286 MPASS(td == NULL || p == td->td_proc);
2287 PROC_LOCK_ASSERT(p, MA_OWNED);
2289 if (!_SIG_VALID(sig))
2290 panic("%s(): invalid signal %d", __func__, sig);
2292 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2295 * IEEE Std 1003.1-2001: return success when killing a zombie.
2297 if (p->p_state == PRS_ZOMBIE) {
2298 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2299 ksiginfo_tryfree(ksi);
2304 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2305 prop = sigprop(sig);
2308 td = sigtd(p, sig, false);
2309 sigqueue = &p->p_sigqueue;
2311 sigqueue = &td->td_sigqueue;
2313 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2316 * If the signal is being ignored, then we forget about it
2317 * immediately, except when the target process executes
2318 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2319 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2321 mtx_lock(&ps->ps_mtx);
2322 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2323 if (kern_sig_discard_ign &&
2324 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2325 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2327 mtx_unlock(&ps->ps_mtx);
2328 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2329 ksiginfo_tryfree(ksi);
2336 if (SIGISMEMBER(td->td_sigmask, sig))
2338 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2342 if (SIGISMEMBER(ps->ps_sigintr, sig))
2347 mtx_unlock(&ps->ps_mtx);
2349 if (prop & SIGPROP_CONT)
2350 sigqueue_delete_stopmask_proc(p);
2351 else if (prop & SIGPROP_STOP) {
2353 * If sending a tty stop signal to a member of an orphaned
2354 * process group, discard the signal here if the action
2355 * is default; don't stop the process below if sleeping,
2356 * and don't clear any pending SIGCONT.
2358 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2359 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2360 action == SIG_DFL) {
2361 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2362 ksiginfo_tryfree(ksi);
2365 sigqueue_delete_proc(p, SIGCONT);
2366 if (p->p_flag & P_CONTINUED) {
2367 p->p_flag &= ~P_CONTINUED;
2368 PROC_LOCK(p->p_pptr);
2369 sigqueue_take(p->p_ksi);
2370 PROC_UNLOCK(p->p_pptr);
2374 ret = sigqueue_add(sigqueue, sig, ksi);
2379 * Defer further processing for signals which are held,
2380 * except that stopped processes must be continued by SIGCONT.
2382 if (action == SIG_HOLD &&
2383 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2389 * Some signals have a process-wide effect and a per-thread
2390 * component. Most processing occurs when the process next
2391 * tries to cross the user boundary, however there are some
2392 * times when processing needs to be done immediately, such as
2393 * waking up threads so that they can cross the user boundary.
2394 * We try to do the per-process part here.
2396 if (P_SHOULDSTOP(p)) {
2397 KASSERT(!(p->p_flag & P_WEXIT),
2398 ("signal to stopped but exiting process"));
2399 if (sig == SIGKILL) {
2401 * If traced process is already stopped,
2402 * then no further action is necessary.
2404 if (p->p_flag & P_TRACED)
2407 * SIGKILL sets process running.
2408 * It will die elsewhere.
2409 * All threads must be restarted.
2411 p->p_flag &= ~P_STOPPED_SIG;
2415 if (prop & SIGPROP_CONT) {
2417 * If traced process is already stopped,
2418 * then no further action is necessary.
2420 if (p->p_flag & P_TRACED)
2423 * If SIGCONT is default (or ignored), we continue the
2424 * process but don't leave the signal in sigqueue as
2425 * it has no further action. If SIGCONT is held, we
2426 * continue the process and leave the signal in
2427 * sigqueue. If the process catches SIGCONT, let it
2428 * handle the signal itself. If it isn't waiting on
2429 * an event, it goes back to run state.
2430 * Otherwise, process goes back to sleep state.
2432 p->p_flag &= ~P_STOPPED_SIG;
2434 if (p->p_numthreads == p->p_suspcount) {
2436 p->p_flag |= P_CONTINUED;
2437 p->p_xsig = SIGCONT;
2438 PROC_LOCK(p->p_pptr);
2439 childproc_continued(p);
2440 PROC_UNLOCK(p->p_pptr);
2443 if (action == SIG_DFL) {
2444 thread_unsuspend(p);
2446 sigqueue_delete(sigqueue, sig);
2449 if (action == SIG_CATCH) {
2451 * The process wants to catch it so it needs
2452 * to run at least one thread, but which one?
2458 * The signal is not ignored or caught.
2460 thread_unsuspend(p);
2465 if (prop & SIGPROP_STOP) {
2467 * If traced process is already stopped,
2468 * then no further action is necessary.
2470 if (p->p_flag & P_TRACED)
2473 * Already stopped, don't need to stop again
2474 * (If we did the shell could get confused).
2475 * Just make sure the signal STOP bit set.
2477 p->p_flag |= P_STOPPED_SIG;
2478 sigqueue_delete(sigqueue, sig);
2483 * All other kinds of signals:
2484 * If a thread is sleeping interruptibly, simulate a
2485 * wakeup so that when it is continued it will be made
2486 * runnable and can look at the signal. However, don't make
2487 * the PROCESS runnable, leave it stopped.
2488 * It may run a bit until it hits a thread_suspend_check().
2492 if (TD_CAN_ABORT(td))
2493 wakeup_swapper = sig_sleepq_abort(td, intrval);
2499 * Mutexes are short lived. Threads waiting on them will
2500 * hit thread_suspend_check() soon.
2502 } else if (p->p_state == PRS_NORMAL) {
2503 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2504 tdsigwakeup(td, sig, action, intrval);
2508 MPASS(action == SIG_DFL);
2510 if (prop & SIGPROP_STOP) {
2511 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2513 p->p_flag |= P_STOPPED_SIG;
2516 wakeup_swapper = sig_suspend_threads(td, p);
2517 if (p->p_numthreads == p->p_suspcount) {
2519 * only thread sending signal to another
2520 * process can reach here, if thread is sending
2521 * signal to its process, because thread does
2522 * not suspend itself here, p_numthreads
2523 * should never be equal to p_suspcount.
2527 sigqueue_delete_proc(p, p->p_xsig);
2533 /* Not in "NORMAL" state. discard the signal. */
2534 sigqueue_delete(sigqueue, sig);
2539 * The process is not stopped so we need to apply the signal to all the
2543 tdsigwakeup(td, sig, action, intrval);
2545 thread_unsuspend(p);
2548 itimer_proc_continue(p);
2549 kqtimer_proc_continue(p);
2551 /* If we jump here, proc slock should not be owned. */
2552 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2560 * The force of a signal has been directed against a single
2561 * thread. We need to see what we can do about knocking it
2562 * out of any sleep it may be in etc.
2565 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2567 struct proc *p = td->td_proc;
2568 int prop, wakeup_swapper;
2570 PROC_LOCK_ASSERT(p, MA_OWNED);
2571 prop = sigprop(sig);
2576 * Bring the priority of a thread up if we want it to get
2577 * killed in this lifetime. Be careful to avoid bumping the
2578 * priority of the idle thread, since we still allow to signal
2581 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2582 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2583 sched_prio(td, PUSER);
2584 if (TD_ON_SLEEPQ(td)) {
2586 * If thread is sleeping uninterruptibly
2587 * we can't interrupt the sleep... the signal will
2588 * be noticed when the process returns through
2589 * trap() or syscall().
2591 if ((td->td_flags & TDF_SINTR) == 0)
2594 * If SIGCONT is default (or ignored) and process is
2595 * asleep, we are finished; the process should not
2598 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2601 sigqueue_delete(&p->p_sigqueue, sig);
2603 * It may be on either list in this state.
2604 * Remove from both for now.
2606 sigqueue_delete(&td->td_sigqueue, sig);
2611 * Don't awaken a sleeping thread for SIGSTOP if the
2612 * STOP signal is deferred.
2614 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2615 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2619 * Give low priority threads a better chance to run.
2621 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2622 sched_prio(td, PUSER);
2624 wakeup_swapper = sig_sleepq_abort(td, intrval);
2632 * Other states do nothing with the signal immediately,
2633 * other than kicking ourselves if we are running.
2634 * It will either never be noticed, or noticed very soon.
2637 if (TD_IS_RUNNING(td) && td != curthread)
2647 ptrace_coredumpreq(struct thread *td, struct proc *p,
2648 struct thr_coredump_req *tcq)
2652 if (p->p_sysent->sv_coredump == NULL) {
2653 tcq->tc_error = ENOSYS;
2657 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2658 tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2659 tcq->tc_limit, tcq->tc_flags);
2660 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2664 ptrace_syscallreq(struct thread *td, struct proc *p,
2665 struct thr_syscall_req *tsr)
2667 struct sysentvec *sv;
2669 register_t rv_saved[2];
2672 bool audited, sy_thr_static;
2675 if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2676 tsr->ts_ret.sr_error = ENOSYS;
2680 sc = tsr->ts_sa.code;
2681 if (sc == SYS_syscall || sc == SYS___syscall) {
2682 sc = tsr->ts_sa.args[0];
2683 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2684 sizeof(register_t) * (tsr->ts_nargs - 1));
2687 tsr->ts_sa.callp = se = &sv->sv_table[sc];
2689 VM_CNT_INC(v_syscall);
2691 if (__predict_false(td->td_cowgen != atomic_load_int(
2692 &td->td_proc->p_cowgen)))
2693 thread_cow_update(td);
2695 td->td_sa = tsr->ts_sa;
2697 #ifdef CAPABILITY_MODE
2698 if ((se->sy_flags & SYF_CAPENABLED) == 0) {
2699 if (CAP_TRACING(td))
2700 ktrcapfail(CAPFAIL_SYSCALL, NULL);
2701 if (IN_CAPABILITY_MODE(td)) {
2702 tsr->ts_ret.sr_error = ECAPMODE;
2708 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2709 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2711 if (!sy_thr_static) {
2712 error = syscall_thread_enter(td, &se);
2713 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2715 tsr->ts_ret.sr_error = error;
2720 rv_saved[0] = td->td_retval[0];
2721 rv_saved[1] = td->td_retval[1];
2722 nerror = td->td_errno;
2723 td->td_retval[0] = 0;
2724 td->td_retval[1] = 0;
2726 #ifdef KDTRACE_HOOKS
2727 if (se->sy_entry != 0)
2728 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2730 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2731 #ifdef KDTRACE_HOOKS
2732 if (se->sy_return != 0)
2733 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2734 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2737 tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2738 tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2739 td->td_retval[0] = rv_saved[0];
2740 td->td_retval[1] = rv_saved[1];
2741 td->td_errno = nerror;
2744 AUDIT_SYSCALL_EXIT(error, td);
2746 syscall_thread_exit(td, se);
2750 ptrace_remotereq(struct thread *td, int flag)
2754 MPASS(td == curthread);
2756 PROC_LOCK_ASSERT(p, MA_OWNED);
2757 if ((td->td_dbgflags & flag) == 0)
2759 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2760 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2764 case TDB_COREDUMPREQ:
2765 ptrace_coredumpreq(td, p, td->td_remotereq);
2767 case TDB_SCREMOTEREQ:
2768 ptrace_syscallreq(td, p, td->td_remotereq);
2775 MPASS((td->td_dbgflags & flag) != 0);
2776 td->td_dbgflags &= ~flag;
2777 td->td_remotereq = NULL;
2782 sig_suspend_threads(struct thread *td, struct proc *p)
2787 PROC_LOCK_ASSERT(p, MA_OWNED);
2788 PROC_SLOCK_ASSERT(p, MA_OWNED);
2791 FOREACH_THREAD_IN_PROC(p, td2) {
2793 ast_sched_locked(td2, TDA_SUSPEND);
2794 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2795 (td2->td_flags & TDF_SINTR)) {
2796 if (td2->td_flags & TDF_SBDRY) {
2798 * Once a thread is asleep with
2799 * TDF_SBDRY and without TDF_SERESTART
2800 * or TDF_SEINTR set, it should never
2801 * become suspended due to this check.
2803 KASSERT(!TD_IS_SUSPENDED(td2),
2804 ("thread with deferred stops suspended"));
2805 if (TD_SBDRY_INTR(td2)) {
2806 wakeup_swapper |= sleepq_abort(td2,
2807 TD_SBDRY_ERRNO(td2));
2810 } else if (!TD_IS_SUSPENDED(td2))
2811 thread_suspend_one(td2);
2812 } else if (!TD_IS_SUSPENDED(td2)) {
2814 if (TD_IS_RUNNING(td2) && td2 != td)
2815 forward_signal(td2);
2820 return (wakeup_swapper);
2824 * Stop the process for an event deemed interesting to the debugger. If si is
2825 * non-NULL, this is a signal exchange; the new signal requested by the
2826 * debugger will be returned for handling. If si is NULL, this is some other
2827 * type of interesting event. The debugger may request a signal be delivered in
2828 * that case as well, however it will be deferred until it can be handled.
2831 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2833 struct proc *p = td->td_proc;
2837 PROC_LOCK_ASSERT(p, MA_OWNED);
2838 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2839 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2840 &p->p_mtx.lock_object, "Stopping for traced signal");
2844 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2845 td->td_dbgflags |= TDB_XSIG;
2846 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2847 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2849 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2852 * Ensure that, if we've been PT_KILLed, the
2853 * exit status reflects that. Another thread
2854 * may also be in ptracestop(), having just
2855 * received the SIGKILL, but this thread was
2856 * unsuspended first.
2858 td->td_dbgflags &= ~TDB_XSIG;
2859 td->td_xsig = SIGKILL;
2863 if (p->p_flag & P_SINGLE_EXIT &&
2864 !(td->td_dbgflags & TDB_EXIT)) {
2866 * Ignore ptrace stops except for thread exit
2867 * events when the process exits.
2869 td->td_dbgflags &= ~TDB_XSIG;
2875 * Make wait(2) work. Ensure that right after the
2876 * attach, the thread which was decided to become the
2877 * leader of attach gets reported to the waiter.
2878 * Otherwise, just avoid overwriting another thread's
2879 * assignment to p_xthread. If another thread has
2880 * already set p_xthread, the current thread will get
2881 * a chance to report itself upon the next iteration.
2883 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2884 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2885 p->p_xthread == NULL)) {
2890 * If we are on sleepqueue already,
2891 * let sleepqueue code decide if it
2892 * needs to go sleep after attach.
2894 if (td->td_wchan == NULL)
2895 td->td_dbgflags &= ~TDB_FSTP;
2897 p->p_flag2 &= ~P2_PTRACE_FSTP;
2898 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2899 sig_suspend_threads(td, p);
2901 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2902 td->td_dbgflags &= ~TDB_STOPATFORK;
2905 td->td_dbgflags |= TDB_SSWITCH;
2906 thread_suspend_switch(td, p);
2907 td->td_dbgflags &= ~TDB_SSWITCH;
2908 if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2909 TDB_SCREMOTEREQ)) != 0) {
2910 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2911 TDB_SCREMOTEREQ)) !=
2912 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2914 ptrace_remotereq(td, td->td_dbgflags &
2915 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2919 if (p->p_xthread == td)
2920 p->p_xthread = NULL;
2921 if (!(p->p_flag & P_TRACED))
2923 if (td->td_dbgflags & TDB_SUSPEND) {
2924 if (p->p_flag & P_SINGLE_EXIT)
2932 if (si != NULL && sig == td->td_xsig) {
2933 /* Parent wants us to take the original signal unchanged. */
2934 si->ksi_flags |= KSI_HEAD;
2935 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2937 } else if (td->td_xsig != 0) {
2939 * If parent wants us to take a new signal, then it will leave
2940 * it in td->td_xsig; otherwise we just look for signals again.
2942 ksiginfo_init(&ksi);
2943 ksi.ksi_signo = td->td_xsig;
2944 ksi.ksi_flags |= KSI_PTRACE;
2945 td2 = sigtd(p, td->td_xsig, false);
2946 tdsendsignal(p, td2, td->td_xsig, &ksi);
2951 return (td->td_xsig);
2955 reschedule_signals(struct proc *p, sigset_t block, int flags)
2960 bool fastblk, pslocked;
2962 PROC_LOCK_ASSERT(p, MA_OWNED);
2964 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2965 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2966 if (SIGISEMPTY(p->p_siglist))
2968 SIGSETAND(block, p->p_siglist);
2969 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2970 SIG_FOREACH(sig, &block) {
2971 td = sigtd(p, sig, fastblk);
2974 * If sigtd() selected us despite sigfastblock is
2975 * blocking, do not activate AST or wake us, to avoid
2976 * loop in AST handler.
2978 if (fastblk && td == curthread)
2983 mtx_lock(&ps->ps_mtx);
2984 if (p->p_flag & P_TRACED ||
2985 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2986 !SIGISMEMBER(td->td_sigmask, sig))) {
2987 tdsigwakeup(td, sig, SIG_CATCH,
2988 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2992 mtx_unlock(&ps->ps_mtx);
2997 tdsigcleanup(struct thread *td)
3003 PROC_LOCK_ASSERT(p, MA_OWNED);
3005 sigqueue_flush(&td->td_sigqueue);
3006 if (p->p_numthreads == 1)
3010 * Since we cannot handle signals, notify signal post code
3011 * about this by filling the sigmask.
3013 * Also, if needed, wake up thread(s) that do not block the
3014 * same signals as the exiting thread, since the thread might
3015 * have been selected for delivery and woken up.
3017 SIGFILLSET(unblocked);
3018 SIGSETNAND(unblocked, td->td_sigmask);
3019 SIGFILLSET(td->td_sigmask);
3020 reschedule_signals(p, unblocked, 0);
3025 sigdeferstop_curr_flags(int cflags)
3028 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
3029 (cflags & TDF_SBDRY) != 0);
3030 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3034 * Defer the delivery of SIGSTOP for the current thread, according to
3035 * the requested mode. Returns previous flags, which must be restored
3036 * by sigallowstop().
3038 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
3039 * cleared by the current thread, which allow the lock-less read-only
3043 sigdeferstop_impl(int mode)
3049 cflags = sigdeferstop_curr_flags(td->td_flags);
3051 case SIGDEFERSTOP_NOP:
3054 case SIGDEFERSTOP_OFF:
3057 case SIGDEFERSTOP_SILENT:
3058 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
3060 case SIGDEFERSTOP_EINTR:
3061 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
3063 case SIGDEFERSTOP_ERESTART:
3064 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
3067 panic("sigdeferstop: invalid mode %x", mode);
3070 if (cflags == nflags)
3071 return (SIGDEFERSTOP_VAL_NCHG);
3073 td->td_flags = (td->td_flags & ~cflags) | nflags;
3079 * Restores the STOP handling mode, typically permitting the delivery
3080 * of SIGSTOP for the current thread. This does not immediately
3081 * suspend if a stop was posted. Instead, the thread will suspend
3082 * either via ast() or a subsequent interruptible sleep.
3085 sigallowstop_impl(int prev)
3090 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3091 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3092 ("sigallowstop: incorrect previous mode %x", prev));
3094 cflags = sigdeferstop_curr_flags(td->td_flags);
3095 if (cflags != prev) {
3097 td->td_flags = (td->td_flags & ~cflags) | prev;
3106 SIGSTATUS_SBDRY_STOP,
3110 * The thread has signal "sig" pending. Figure out what to do with it:
3112 * _HANDLE -> the caller should handle the signal
3113 * _HANDLED -> handled internally, reload pending signal set
3114 * _IGNORE -> ignored, remove from the set of pending signals and try the
3115 * next pending signal
3116 * _SBDRY_STOP -> the signal should stop the thread but this is not
3117 * permitted in the current context
3119 static enum sigstatus
3120 sigprocess(struct thread *td, int sig)
3124 struct sigqueue *queue;
3128 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3132 mtx_assert(&ps->ps_mtx, MA_OWNED);
3133 PROC_LOCK_ASSERT(p, MA_OWNED);
3136 * We should allow pending but ignored signals below
3137 * if there is sigwait() active, or P_TRACED was
3138 * on when they were posted.
3140 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3141 (p->p_flag & P_TRACED) == 0 &&
3142 (td->td_flags & TDF_SIGWAIT) == 0) {
3143 return (SIGSTATUS_IGNORE);
3147 * If the process is going to single-thread mode to prepare
3148 * for exit, there is no sense in delivering any signal
3149 * to usermode. Another important consequence is that
3150 * msleep(..., PCATCH, ...) now is only interruptible by a
3153 if ((p->p_flag2 & P2_WEXIT) != 0)
3154 return (SIGSTATUS_IGNORE);
3156 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3158 * If traced, always stop.
3159 * Remove old signal from queue before the stop.
3160 * XXX shrug off debugger, it causes siginfo to
3163 queue = &td->td_sigqueue;
3164 ksiginfo_init(&ksi);
3165 if (sigqueue_get(queue, sig, &ksi) == 0) {
3166 queue = &p->p_sigqueue;
3167 sigqueue_get(queue, sig, &ksi);
3169 td->td_si = ksi.ksi_info;
3171 mtx_unlock(&ps->ps_mtx);
3172 sig = ptracestop(td, sig, &ksi);
3173 mtx_lock(&ps->ps_mtx);
3175 td->td_si.si_signo = 0;
3178 * Keep looking if the debugger discarded or
3179 * replaced the signal.
3182 return (SIGSTATUS_HANDLED);
3185 * If the signal became masked, re-queue it.
3187 if (SIGISMEMBER(td->td_sigmask, sig)) {
3188 ksi.ksi_flags |= KSI_HEAD;
3189 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3190 return (SIGSTATUS_HANDLED);
3194 * If the traced bit got turned off, requeue the signal and
3195 * reload the set of pending signals. This ensures that p_sig*
3196 * and p_sigact are consistent.
3198 if ((p->p_flag & P_TRACED) == 0) {
3199 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3200 ksi.ksi_flags |= KSI_HEAD;
3201 sigqueue_add(queue, sig, &ksi);
3203 return (SIGSTATUS_HANDLED);
3208 * Decide whether the signal should be returned.
3209 * Return the signal's number, or fall through
3210 * to clear it from the pending mask.
3212 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3213 case (intptr_t)SIG_DFL:
3215 * Don't take default actions on system processes.
3217 if (p->p_pid <= 1) {
3220 * Are you sure you want to ignore SIGSEGV
3223 printf("Process (pid %lu) got signal %d\n",
3224 (u_long)p->p_pid, sig);
3226 return (SIGSTATUS_IGNORE);
3230 * If there is a pending stop signal to process with
3231 * default action, stop here, then clear the signal.
3232 * Traced or exiting processes should ignore stops.
3233 * Additionally, a member of an orphaned process group
3234 * should ignore tty stops.
3236 prop = sigprop(sig);
3237 if (prop & SIGPROP_STOP) {
3238 mtx_unlock(&ps->ps_mtx);
3239 if ((p->p_flag & (P_TRACED | P_WEXIT |
3240 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3241 pg_flags & PGRP_ORPHANED) != 0 &&
3242 (prop & SIGPROP_TTYSTOP) != 0)) {
3243 mtx_lock(&ps->ps_mtx);
3244 return (SIGSTATUS_IGNORE);
3246 if (TD_SBDRY_INTR(td)) {
3247 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3248 ("lost TDF_SBDRY"));
3249 mtx_lock(&ps->ps_mtx);
3250 return (SIGSTATUS_SBDRY_STOP);
3252 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3253 &p->p_mtx.lock_object, "Catching SIGSTOP");
3254 sigqueue_delete(&td->td_sigqueue, sig);
3255 sigqueue_delete(&p->p_sigqueue, sig);
3256 p->p_flag |= P_STOPPED_SIG;
3259 sig_suspend_threads(td, p);
3260 thread_suspend_switch(td, p);
3262 mtx_lock(&ps->ps_mtx);
3263 return (SIGSTATUS_HANDLED);
3264 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3265 (td->td_flags & TDF_SIGWAIT) == 0) {
3267 * Default action is to ignore; drop it if
3268 * not in kern_sigtimedwait().
3270 return (SIGSTATUS_IGNORE);
3272 return (SIGSTATUS_HANDLE);
3275 case (intptr_t)SIG_IGN:
3276 if ((td->td_flags & TDF_SIGWAIT) == 0)
3277 return (SIGSTATUS_IGNORE);
3279 return (SIGSTATUS_HANDLE);
3283 * This signal has an action, let postsig() process it.
3285 return (SIGSTATUS_HANDLE);
3290 * If the current process has received a signal (should be caught or cause
3291 * termination, should interrupt current syscall), return the signal number.
3292 * Stop signals with default action are processed immediately, then cleared;
3293 * they aren't returned. This is checked after each entry to the system for
3294 * a syscall or trap (though this can usually be done without calling
3295 * issignal by checking the pending signal masks in cursig.) The normal call
3298 * while (sig = cursig(curthread))
3302 issignal(struct thread *td)
3305 sigset_t sigpending;
3309 PROC_LOCK_ASSERT(p, MA_OWNED);
3312 sigpending = td->td_sigqueue.sq_signals;
3313 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3314 SIGSETNAND(sigpending, td->td_sigmask);
3316 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3317 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3318 SIG_STOPSIGMASK(sigpending);
3319 if (SIGISEMPTY(sigpending)) /* no signal to send */
3323 * Do fast sigblock if requested by usermode. Since
3324 * we do know that there was a signal pending at this
3325 * point, set the FAST_SIGBLOCK_PEND as indicator for
3326 * usermode to perform a dummy call to
3327 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3328 * delivery of postponed pending signal.
3330 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3331 if (td->td_sigblock_val != 0)
3332 SIGSETNAND(sigpending, fastblock_mask);
3333 if (SIGISEMPTY(sigpending)) {
3334 td->td_pflags |= TDP_SIGFASTPENDING;
3339 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3340 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3341 SIGISMEMBER(sigpending, SIGSTOP)) {
3343 * If debugger just attached, always consume
3344 * SIGSTOP from ptrace(PT_ATTACH) first, to
3345 * execute the debugger attach ritual in
3348 td->td_dbgflags |= TDB_FSTP;
3349 SIGEMPTYSET(sigpending);
3350 SIGADDSET(sigpending, SIGSTOP);
3353 SIG_FOREACH(sig, &sigpending) {
3354 switch (sigprocess(td, sig)) {
3355 case SIGSTATUS_HANDLE:
3357 case SIGSTATUS_HANDLED:
3359 case SIGSTATUS_IGNORE:
3360 sigqueue_delete(&td->td_sigqueue, sig);
3361 sigqueue_delete(&p->p_sigqueue, sig);
3363 case SIGSTATUS_SBDRY_STOP:
3372 thread_stopped(struct proc *p)
3376 PROC_LOCK_ASSERT(p, MA_OWNED);
3377 PROC_SLOCK_ASSERT(p, MA_OWNED);
3381 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3383 p->p_flag &= ~P_WAITED;
3384 PROC_LOCK(p->p_pptr);
3385 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3386 CLD_TRAPPED : CLD_STOPPED);
3387 PROC_UNLOCK(p->p_pptr);
3393 * Take the action for the specified signal
3394 * from the current set of pending signals.
3404 sigset_t returnmask;
3406 KASSERT(sig != 0, ("postsig"));
3410 PROC_LOCK_ASSERT(p, MA_OWNED);
3412 mtx_assert(&ps->ps_mtx, MA_OWNED);
3413 ksiginfo_init(&ksi);
3414 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3415 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3417 ksi.ksi_signo = sig;
3418 if (ksi.ksi_code == SI_TIMER)
3419 itimer_accept(p, ksi.ksi_timerid, &ksi);
3420 action = ps->ps_sigact[_SIG_IDX(sig)];
3422 if (KTRPOINT(td, KTR_PSIG))
3423 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3424 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3427 if (action == SIG_DFL) {
3429 * Default action, where the default is to kill
3430 * the process. (Other cases were ignored above.)
3432 mtx_unlock(&ps->ps_mtx);
3433 proc_td_siginfo_capture(td, &ksi.ksi_info);
3438 * If we get here, the signal must be caught.
3440 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3441 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3442 ("postsig action: blocked sig %d", sig));
3445 * Set the new mask value and also defer further
3446 * occurrences of this signal.
3448 * Special case: user has done a sigsuspend. Here the
3449 * current mask is not of interest, but rather the
3450 * mask from before the sigsuspend is what we want
3451 * restored after the signal processing is completed.
3453 if (td->td_pflags & TDP_OLDMASK) {
3454 returnmask = td->td_oldsigmask;
3455 td->td_pflags &= ~TDP_OLDMASK;
3457 returnmask = td->td_sigmask;
3459 if (p->p_sig == sig) {
3462 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3463 postsig_done(sig, td, ps);
3469 sig_ast_checksusp(struct thread *td)
3471 struct proc *p __diagused;
3475 PROC_LOCK_ASSERT(p, MA_OWNED);
3477 if (!td_ast_pending(td, TDA_SUSPEND))
3480 ret = thread_suspend_check(1);
3481 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3486 sig_ast_needsigchk(struct thread *td)
3493 PROC_LOCK_ASSERT(p, MA_OWNED);
3495 if (!td_ast_pending(td, TDA_SIG))
3499 mtx_lock(&ps->ps_mtx);
3502 mtx_unlock(&ps->ps_mtx);
3503 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3504 KASSERT(TD_SBDRY_INTR(td),
3505 ("lost TDF_SERESTART of TDF_SEINTR"));
3506 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3507 (TDF_SEINTR | TDF_SERESTART),
3508 ("both TDF_SEINTR and TDF_SERESTART"));
3509 ret = TD_SBDRY_ERRNO(td);
3510 } else if (sig != 0) {
3511 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3512 mtx_unlock(&ps->ps_mtx);
3514 mtx_unlock(&ps->ps_mtx);
3519 * Do not go into sleep if this thread was the ptrace(2)
3520 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3521 * but we usually act on the signal by interrupting sleep, and
3522 * should do that here as well.
3524 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3527 td->td_dbgflags &= ~TDB_FSTP;
3541 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3547 ret = sig_ast_checksusp(td);
3549 ret = sig_ast_needsigchk(td);
3555 curproc_sigkilled(void)
3563 if (!td_ast_pending(td, TDA_SIG))
3569 mtx_lock(&ps->ps_mtx);
3570 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3571 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3572 mtx_unlock(&ps->ps_mtx);
3578 proc_wkilled(struct proc *p)
3581 PROC_LOCK_ASSERT(p, MA_OWNED);
3582 if ((p->p_flag & P_WKILLED) == 0) {
3583 p->p_flag |= P_WKILLED;
3585 * Notify swapper that there is a process to swap in.
3586 * The notification is racy, at worst it would take 10
3587 * seconds for the swapper process to notice.
3589 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3595 * Kill the current process for stated reason.
3598 killproc(struct proc *p, const char *why)
3601 PROC_LOCK_ASSERT(p, MA_OWNED);
3602 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3604 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3605 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3606 p->p_ucred->cr_uid, why);
3608 kern_psignal(p, SIGKILL);
3612 * Force the current process to exit with the specified signal, dumping core
3613 * if appropriate. We bypass the normal tests for masked and caught signals,
3614 * allowing unrecoverable failures to terminate the process without changing
3615 * signal state. Mark the accounting record with the signal termination.
3616 * If dumping core, save the signal number for the debugger. Calls exit and
3620 sigexit(struct thread *td, int sig)
3622 struct proc *p = td->td_proc;
3623 const char *coreinfo;
3626 PROC_LOCK_ASSERT(p, MA_OWNED);
3627 proc_set_p2_wexit(p);
3629 p->p_acflag |= AXSIG;
3631 * We must be single-threading to generate a core dump. This
3632 * ensures that the registers in the core file are up-to-date.
3633 * Also, the ELF dump handler assumes that the thread list doesn't
3634 * change out from under it.
3636 * XXX If another thread attempts to single-thread before us
3637 * (e.g. via fork()), we won't get a dump at all.
3639 if ((sigprop(sig) & SIGPROP_CORE) &&
3640 thread_single(p, SINGLE_NO_EXIT) == 0) {
3643 * Log signals which would cause core dumps
3644 * (Log as LOG_INFO to appease those who don't want
3646 * XXX : Todo, as well as euid, write out ruid too
3647 * Note that coredump() drops proc lock.
3653 coreinfo = " (core dumped)";
3656 coreinfo = " (no core dump - bad address)";
3659 coreinfo = " (no core dump - invalid argument)";
3662 coreinfo = " (no core dump - too large)";
3665 coreinfo = " (no core dump - other error)";
3668 if (kern_logsigexit)
3670 "pid %d (%s), jid %d, uid %d: exited on "
3671 "signal %d%s\n", p->p_pid, p->p_comm,
3672 p->p_ucred->cr_prison->pr_id,
3673 td->td_ucred->cr_uid,
3674 sig &~ WCOREFLAG, coreinfo);
3682 * Send queued SIGCHLD to parent when child process's state
3686 sigparent(struct proc *p, int reason, int status)
3688 PROC_LOCK_ASSERT(p, MA_OWNED);
3689 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3691 if (p->p_ksi != NULL) {
3692 p->p_ksi->ksi_signo = SIGCHLD;
3693 p->p_ksi->ksi_code = reason;
3694 p->p_ksi->ksi_status = status;
3695 p->p_ksi->ksi_pid = p->p_pid;
3696 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3697 if (KSI_ONQ(p->p_ksi))
3700 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3704 childproc_jobstate(struct proc *p, int reason, int sig)
3708 PROC_LOCK_ASSERT(p, MA_OWNED);
3709 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3712 * Wake up parent sleeping in kern_wait(), also send
3713 * SIGCHLD to parent, but SIGCHLD does not guarantee
3714 * that parent will awake, because parent may masked
3717 p->p_pptr->p_flag |= P_STATCHILD;
3720 ps = p->p_pptr->p_sigacts;
3721 mtx_lock(&ps->ps_mtx);
3722 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3723 mtx_unlock(&ps->ps_mtx);
3724 sigparent(p, reason, sig);
3726 mtx_unlock(&ps->ps_mtx);
3730 childproc_stopped(struct proc *p, int reason)
3733 childproc_jobstate(p, reason, p->p_xsig);
3737 childproc_continued(struct proc *p)
3739 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3743 childproc_exited(struct proc *p)
3747 if (WCOREDUMP(p->p_xsig)) {
3748 reason = CLD_DUMPED;
3749 status = WTERMSIG(p->p_xsig);
3750 } else if (WIFSIGNALED(p->p_xsig)) {
3751 reason = CLD_KILLED;
3752 status = WTERMSIG(p->p_xsig);
3754 reason = CLD_EXITED;
3755 status = p->p_xexit;
3758 * XXX avoid calling wakeup(p->p_pptr), the work is
3761 sigparent(p, reason, status);
3764 #define MAX_NUM_CORE_FILES 100000
3765 #ifndef NUM_CORE_FILES
3766 #define NUM_CORE_FILES 5
3768 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3769 static int num_cores = NUM_CORE_FILES;
3772 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3777 new_val = num_cores;
3778 error = sysctl_handle_int(oidp, &new_val, 0, req);
3779 if (error != 0 || req->newptr == NULL)
3781 if (new_val > MAX_NUM_CORE_FILES)
3782 new_val = MAX_NUM_CORE_FILES;
3785 num_cores = new_val;
3788 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3789 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3790 sysctl_debug_num_cores_check, "I",
3791 "Maximum number of generated process corefiles while using index format");
3793 #define GZIP_SUFFIX ".gz"
3794 #define ZSTD_SUFFIX ".zst"
3796 int compress_user_cores = 0;
3799 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3803 val = compress_user_cores;
3804 error = sysctl_handle_int(oidp, &val, 0, req);
3805 if (error != 0 || req->newptr == NULL)
3807 if (val != 0 && !compressor_avail(val))
3809 compress_user_cores = val;
3812 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3813 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3814 sysctl_compress_user_cores, "I",
3815 "Enable compression of user corefiles ("
3816 __XSTRING(COMPRESS_GZIP) " = gzip, "
3817 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3819 int compress_user_cores_level = 6;
3820 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3821 &compress_user_cores_level, 0,
3822 "Corefile compression level");
3825 * Protect the access to corefilename[] by allproc_lock.
3827 #define corefilename_lock allproc_lock
3829 static char corefilename[MAXPATHLEN] = {"%N.core"};
3830 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3833 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3837 sx_xlock(&corefilename_lock);
3838 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3840 sx_xunlock(&corefilename_lock);
3844 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3845 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3846 "Process corefile name format string");
3849 vnode_close_locked(struct thread *td, struct vnode *vp)
3853 vn_close(vp, FWRITE, td->td_ucred, td);
3857 * If the core format has a %I in it, then we need to check
3858 * for existing corefiles before defining a name.
3859 * To do this we iterate over 0..ncores to find a
3860 * non-existing core file name to use. If all core files are
3861 * already used we choose the oldest one.
3864 corefile_open_last(struct thread *td, char *name, int indexpos,
3865 int indexlen, int ncores, struct vnode **vpp)
3867 struct vnode *oldvp, *nextvp, *vp;
3869 struct nameidata nd;
3870 int error, i, flags, oflags, cmode;
3872 struct timespec lasttime;
3874 nextvp = oldvp = NULL;
3875 cmode = S_IRUSR | S_IWUSR;
3876 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3877 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3879 for (i = 0; i < ncores; i++) {
3880 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3882 ch = name[indexpos + indexlen];
3883 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3885 name[indexpos + indexlen] = ch;
3887 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3888 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3895 if ((flags & O_CREAT) == O_CREAT) {
3900 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3902 vnode_close_locked(td, vp);
3906 if (oldvp == NULL ||
3907 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3908 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3909 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3911 vn_close(oldvp, FWRITE, td->td_ucred, td);
3914 lasttime = vattr.va_mtime;
3916 vnode_close_locked(td, vp);
3920 if (oldvp != NULL) {
3921 if (nextvp == NULL) {
3922 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3924 vn_close(oldvp, FWRITE, td->td_ucred, td);
3927 error = vn_lock(nextvp, LK_EXCLUSIVE);
3929 vn_close(nextvp, FWRITE, td->td_ucred,
3935 vn_close(oldvp, FWRITE, td->td_ucred, td);
3940 vnode_close_locked(td, oldvp);
3949 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3950 * Expand the name described in corefilename, using name, uid, and pid
3951 * and open/create core file.
3952 * corefilename is a printf-like string, with three format specifiers:
3953 * %N name of process ("name")
3954 * %P process id (pid)
3956 * For example, "%N.core" is the default; they can be disabled completely
3957 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3958 * This is controlled by the sysctl variable kern.corefile (see above).
3961 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3962 int compress, int signum, struct vnode **vpp, char **namep)
3965 struct nameidata nd;
3967 char *hostname, *name;
3968 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3971 format = corefilename;
3972 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3976 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3977 sx_slock(&corefilename_lock);
3978 for (i = 0; format[i] != '\0'; i++) {
3979 switch (format[i]) {
3980 case '%': /* Format character */
3982 switch (format[i]) {
3984 sbuf_putc(&sb, '%');
3986 case 'H': /* hostname */
3987 if (hostname == NULL) {
3988 hostname = malloc(MAXHOSTNAMELEN,
3991 getcredhostname(td->td_ucred, hostname,
3993 sbuf_cat(&sb, hostname);
3995 case 'I': /* autoincrementing index */
3996 if (indexpos != -1) {
3997 sbuf_printf(&sb, "%%I");
4001 indexpos = sbuf_len(&sb);
4002 sbuf_printf(&sb, "%u", ncores - 1);
4003 indexlen = sbuf_len(&sb) - indexpos;
4005 case 'N': /* process name */
4006 sbuf_printf(&sb, "%s", comm);
4008 case 'P': /* process id */
4009 sbuf_printf(&sb, "%u", pid);
4011 case 'S': /* signal number */
4012 sbuf_printf(&sb, "%i", signum);
4014 case 'U': /* user id */
4015 sbuf_printf(&sb, "%u", uid);
4019 "Unknown format character %c in "
4020 "corename `%s'\n", format[i], format);
4025 sbuf_putc(&sb, format[i]);
4029 sx_sunlock(&corefilename_lock);
4030 free(hostname, M_TEMP);
4031 if (compress == COMPRESS_GZIP)
4032 sbuf_cat(&sb, GZIP_SUFFIX);
4033 else if (compress == COMPRESS_ZSTD)
4034 sbuf_cat(&sb, ZSTD_SUFFIX);
4035 if (sbuf_error(&sb) != 0) {
4036 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
4037 "long\n", (long)pid, comm, (u_long)uid);
4045 if (indexpos != -1) {
4046 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
4050 "pid %d (%s), uid (%u): Path `%s' failed "
4051 "on initial open test, error = %d\n",
4052 pid, comm, uid, name, error);
4055 cmode = S_IRUSR | S_IWUSR;
4056 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
4057 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
4058 flags = O_CREAT | FWRITE | O_NOFOLLOW;
4059 if ((td->td_proc->p_flag & P_SUGID) != 0)
4062 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
4063 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
4073 audit_proc_coredump(td, name, error);
4083 * Dump a process' core. The main routine does some
4084 * policy checking, and creates the name of the coredump;
4085 * then it passes on a vnode and a size limit to the process-specific
4086 * coredump routine if there is one; if there _is not_ one, it returns
4087 * ENOSYS; otherwise it returns the error from the process-specific routine.
4091 coredump(struct thread *td)
4093 struct proc *p = td->td_proc;
4094 struct ucred *cred = td->td_ucred;
4098 size_t fullpathsize;
4099 int error, error1, locked;
4100 char *name; /* name of corefile */
4103 char *fullpath, *freepath = NULL;
4106 PROC_LOCK_ASSERT(p, MA_OWNED);
4107 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
4109 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
4110 (p->p_flag2 & P2_NOTRACE) != 0) {
4116 * Note that the bulk of limit checking is done after
4117 * the corefile is created. The exception is if the limit
4118 * for corefiles is 0, in which case we don't bother
4119 * creating the corefile at all. This layout means that
4120 * a corefile is truncated instead of not being created,
4121 * if it is larger than the limit.
4123 limit = (off_t)lim_cur(td, RLIMIT_CORE);
4124 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
4130 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
4131 compress_user_cores, p->p_sig, &vp, &name);
4136 * Don't dump to non-regular files or files with links.
4137 * Do not dump into system files. Effective user must own the corefile.
4139 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
4140 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
4141 vattr.va_uid != cred->cr_uid) {
4149 /* Postpone other writers, including core dumps of other processes. */
4150 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4152 lf.l_whence = SEEK_SET;
4155 lf.l_type = F_WRLCK;
4156 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
4160 if (set_core_nodump_flag)
4161 vattr.va_flags = UF_NODUMP;
4162 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4163 VOP_SETATTR(vp, &vattr, cred);
4166 p->p_acflag |= ACORE;
4169 if (p->p_sysent->sv_coredump != NULL) {
4170 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
4176 lf.l_type = F_UNLCK;
4177 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4179 vn_rangelock_unlock(vp, rl_cookie);
4182 * Notify the userland helper that a process triggered a core dump.
4183 * This allows the helper to run an automated debugging session.
4185 if (error != 0 || coredump_devctl == 0)
4187 sb = sbuf_new_auto();
4188 if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4190 sbuf_cat(sb, "comm=\"");
4191 devctl_safe_quote_sb(sb, fullpath);
4192 free(freepath, M_TEMP);
4193 sbuf_cat(sb, "\" core=\"");
4196 * We can't lookup core file vp directly. When we're replacing a core, and
4197 * other random times, we flush the name cache, so it will fail. Instead,
4198 * if the path of the core is relative, add the current dir in front if it.
4200 if (name[0] != '/') {
4201 fullpathsize = MAXPATHLEN;
4202 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4203 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
4204 free(freepath, M_TEMP);
4207 devctl_safe_quote_sb(sb, fullpath);
4208 free(freepath, M_TEMP);
4211 devctl_safe_quote_sb(sb, name);
4213 if (sbuf_finish(sb) == 0)
4214 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4218 error1 = vn_close(vp, FWRITE, cred, td);
4222 audit_proc_coredump(td, name, error);
4229 * Nonexistent system call-- signal process (may want to handle it). Flag
4230 * error in case process won't see signal immediately (blocked or ignored).
4232 #ifndef _SYS_SYSPROTO_H_
4239 nosys(struct thread *td, struct nosys_args *args)
4245 if (SV_PROC_FLAG(p, SV_SIGSYS) != 0 && kern_signosys) {
4247 tdsignal(td, SIGSYS);
4250 if (kern_lognosys == 1 || kern_lognosys == 3) {
4251 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4254 if (kern_lognosys == 2 || kern_lognosys == 3 ||
4255 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4256 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4263 * Send a SIGIO or SIGURG signal to a process or process group using stored
4264 * credentials rather than those of the current process.
4267 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4270 struct sigio *sigio;
4272 ksiginfo_init(&ksi);
4273 ksi.ksi_signo = sig;
4274 ksi.ksi_code = SI_KERNEL;
4278 if (sigio == NULL) {
4282 if (sigio->sio_pgid > 0) {
4283 PROC_LOCK(sigio->sio_proc);
4284 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4285 kern_psignal(sigio->sio_proc, sig);
4286 PROC_UNLOCK(sigio->sio_proc);
4287 } else if (sigio->sio_pgid < 0) {
4290 PGRP_LOCK(sigio->sio_pgrp);
4291 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4293 if (p->p_state == PRS_NORMAL &&
4294 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4295 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4296 kern_psignal(p, sig);
4299 PGRP_UNLOCK(sigio->sio_pgrp);
4305 filt_sigattach(struct knote *kn)
4307 struct proc *p = curproc;
4309 kn->kn_ptr.p_proc = p;
4310 kn->kn_flags |= EV_CLEAR; /* automatically set */
4312 knlist_add(p->p_klist, kn, 0);
4318 filt_sigdetach(struct knote *kn)
4320 knlist_remove(kn->kn_knlist, kn, 0);
4324 * signal knotes are shared with proc knotes, so we apply a mask to
4325 * the hint in order to differentiate them from process hints. This
4326 * could be avoided by using a signal-specific knote list, but probably
4327 * isn't worth the trouble.
4330 filt_signal(struct knote *kn, long hint)
4333 if (hint & NOTE_SIGNAL) {
4334 hint &= ~NOTE_SIGNAL;
4336 if (kn->kn_id == hint)
4339 return (kn->kn_data != 0);
4347 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4348 refcount_init(&ps->ps_refcnt, 1);
4349 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4354 sigacts_free(struct sigacts *ps)
4357 if (refcount_release(&ps->ps_refcnt) == 0)
4359 mtx_destroy(&ps->ps_mtx);
4360 free(ps, M_SUBPROC);
4364 sigacts_hold(struct sigacts *ps)
4367 refcount_acquire(&ps->ps_refcnt);
4372 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4375 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4376 mtx_lock(&src->ps_mtx);
4377 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4378 mtx_unlock(&src->ps_mtx);
4382 sigacts_shared(struct sigacts *ps)
4385 return (ps->ps_refcnt > 1);
4389 sig_drop_caught(struct proc *p)
4395 PROC_LOCK_ASSERT(p, MA_OWNED);
4396 mtx_assert(&ps->ps_mtx, MA_OWNED);
4397 SIG_FOREACH(sig, &ps->ps_sigcatch) {
4399 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4400 sigqueue_delete_proc(p, sig);
4405 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4410 * Prevent further fetches and SIGSEGVs, allowing thread to
4411 * issue syscalls despite corruption.
4413 sigfastblock_clear(td);
4417 ksiginfo_init_trap(&ksi);
4418 ksi.ksi_signo = SIGSEGV;
4419 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4420 ksi.ksi_addr = td->td_sigblock_ptr;
4421 trapsignal(td, &ksi);
4425 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4429 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4431 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4432 sigfastblock_failed(td, sendsig, false);
4436 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4441 sigfastblock_resched(struct thread *td, bool resched)
4448 reschedule_signals(p, td->td_sigmask, 0);
4451 ast_sched(td, TDA_SIG);
4455 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4464 case SIGFASTBLOCK_SETPTR:
4465 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4469 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4473 td->td_pflags |= TDP_SIGFASTBLOCK;
4474 td->td_sigblock_ptr = uap->ptr;
4477 case SIGFASTBLOCK_UNBLOCK:
4478 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4484 res = casueword32(td->td_sigblock_ptr,
4485 SIGFASTBLOCK_PEND, &oldval, 0);
4488 sigfastblock_failed(td, false, true);
4494 if (oldval != SIGFASTBLOCK_PEND) {
4498 error = thread_check_susp(td, false);
4506 * td_sigblock_val is cleared there, but not on a
4507 * syscall exit. The end effect is that a single
4508 * interruptible sleep, while user sigblock word is
4509 * set, might return EINTR or ERESTART to usermode
4510 * without delivering signal. All further sleeps,
4511 * until userspace clears the word and does
4512 * sigfastblock(UNBLOCK), observe current word and no
4513 * longer get interrupted. It is slight
4514 * non-conformance, with alternative to have read the
4515 * sigblock word on each syscall entry.
4517 td->td_sigblock_val = 0;
4520 * Rely on normal ast mechanism to deliver pending
4521 * signals to current thread. But notify others about
4524 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4528 case SIGFASTBLOCK_UNSETPTR:
4529 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4533 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4537 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4541 sigfastblock_clear(td);
4552 sigfastblock_clear(struct thread *td)
4556 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4558 td->td_sigblock_val = 0;
4559 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4561 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4562 sigfastblock_resched(td, resched);
4566 sigfastblock_fetch(struct thread *td)
4570 (void)sigfastblock_fetch_sig(td, true, &val);
4574 sigfastblock_setpend1(struct thread *td)
4579 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4581 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4583 sigfastblock_failed(td, true, false);
4587 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4588 oldval | SIGFASTBLOCK_PEND);
4590 sigfastblock_failed(td, true, true);
4594 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4595 td->td_pflags &= ~TDP_SIGFASTPENDING;
4599 if (thread_check_susp(td, false) != 0)
4605 sigfastblock_setpend(struct thread *td, bool resched)
4609 sigfastblock_setpend1(td);
4613 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);