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 <sys/cdefs.h>
38 #include "opt_capsicum.h"
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/capsicum.h>
43 #include <sys/ctype.h>
44 #include <sys/systm.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
48 #include <sys/capsicum.h>
49 #include <sys/compressor.h>
50 #include <sys/condvar.h>
51 #include <sys/devctl.h>
52 #include <sys/event.h>
53 #include <sys/fcntl.h>
54 #include <sys/imgact.h>
56 #include <sys/kernel.h>
58 #include <sys/ktrace.h>
59 #include <sys/limits.h>
61 #include <sys/malloc.h>
62 #include <sys/mutex.h>
63 #include <sys/refcount.h>
64 #include <sys/namei.h>
66 #include <sys/procdesc.h>
67 #include <sys/ptrace.h>
68 #include <sys/posix4.h>
69 #include <sys/racct.h>
70 #include <sys/resourcevar.h>
73 #include <sys/sleepqueue.h>
77 #include <sys/syscall.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysent.h>
81 #include <sys/syslog.h>
82 #include <sys/sysproto.h>
83 #include <sys/timers.h>
84 #include <sys/unistd.h>
85 #include <sys/vmmeter.h>
88 #include <vm/vm_extern.h>
91 #include <machine/cpu.h>
93 #include <security/audit/audit.h>
95 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
97 SDT_PROVIDER_DECLARE(proc);
98 SDT_PROBE_DEFINE3(proc, , , signal__send,
99 "struct thread *", "struct proc *", "int");
100 SDT_PROBE_DEFINE2(proc, , , signal__clear,
101 "int", "ksiginfo_t *");
102 SDT_PROBE_DEFINE3(proc, , , signal__discard,
103 "struct thread *", "struct proc *", "int");
105 static int coredump(struct thread *);
106 static int killpg1(struct thread *td, int sig, int pgid, int all,
108 static int issignal(struct thread *td);
109 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
110 static int sigprop(int sig);
111 static void tdsigwakeup(struct thread *, int, sig_t, int);
112 static int sig_suspend_threads(struct thread *, struct proc *);
113 static int filt_sigattach(struct knote *kn);
114 static void filt_sigdetach(struct knote *kn);
115 static int filt_signal(struct knote *kn, long hint);
116 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
117 static void sigqueue_start(void);
118 static void sigfastblock_setpend(struct thread *td, bool resched);
120 static uma_zone_t ksiginfo_zone = NULL;
121 struct filterops sig_filtops = {
123 .f_attach = filt_sigattach,
124 .f_detach = filt_sigdetach,
125 .f_event = filt_signal,
128 static int kern_logsigexit = 1;
129 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
131 "Log processes quitting on abnormal signals to syslog(3)");
133 static int kern_forcesigexit = 1;
134 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
135 &kern_forcesigexit, 0, "Force trap signal to be handled");
137 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
138 "POSIX real time signal");
140 static int max_pending_per_proc = 128;
141 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
142 &max_pending_per_proc, 0, "Max pending signals per proc");
144 static int preallocate_siginfo = 1024;
145 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
146 &preallocate_siginfo, 0, "Preallocated signal memory size");
148 static int signal_overflow = 0;
149 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
150 &signal_overflow, 0, "Number of signals overflew");
152 static int signal_alloc_fail = 0;
153 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
154 &signal_alloc_fail, 0, "signals failed to be allocated");
156 static int kern_lognosys = 0;
157 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
158 "Log invalid syscalls");
160 static int kern_signosys = 1;
161 SYSCTL_INT(_kern, OID_AUTO, signosys, CTLFLAG_RWTUN, &kern_signosys, 0,
162 "Send SIGSYS on return from invalid syscall");
164 __read_frequently bool sigfastblock_fetch_always = false;
165 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
166 &sigfastblock_fetch_always, 0,
167 "Fetch sigfastblock word on each syscall entry for proper "
168 "blocking semantic");
170 static bool kern_sig_discard_ign = true;
171 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
172 &kern_sig_discard_ign, 0,
173 "Discard ignored signals on delivery, otherwise queue them to "
176 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
179 * Policy -- Can ucred cr1 send SIGIO to process cr2?
180 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
181 * in the right situations.
183 #define CANSIGIO(cr1, cr2) \
184 ((cr1)->cr_uid == 0 || \
185 (cr1)->cr_ruid == (cr2)->cr_ruid || \
186 (cr1)->cr_uid == (cr2)->cr_ruid || \
187 (cr1)->cr_ruid == (cr2)->cr_uid || \
188 (cr1)->cr_uid == (cr2)->cr_uid)
190 static int sugid_coredump;
191 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
192 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
194 static int capmode_coredump;
195 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
196 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
198 static int do_coredump = 1;
199 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
200 &do_coredump, 0, "Enable/Disable coredumps");
202 static int set_core_nodump_flag = 0;
203 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
204 0, "Enable setting the NODUMP flag on coredump files");
206 static int coredump_devctl = 0;
207 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
208 0, "Generate a devctl notification when processes coredump");
211 * Signal properties and actions.
212 * The array below categorizes the signals and their default actions
213 * according to the following properties:
215 #define SIGPROP_KILL 0x01 /* terminates process by default */
216 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
217 #define SIGPROP_STOP 0x04 /* suspend process */
218 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
219 #define SIGPROP_IGNORE 0x10 /* ignore by default */
220 #define SIGPROP_CONT 0x20 /* continue if suspended */
222 static const int sigproptbl[NSIG] = {
223 [SIGHUP] = SIGPROP_KILL,
224 [SIGINT] = SIGPROP_KILL,
225 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
226 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
227 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
228 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
229 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
230 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
231 [SIGKILL] = SIGPROP_KILL,
232 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
233 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
234 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
235 [SIGPIPE] = SIGPROP_KILL,
236 [SIGALRM] = SIGPROP_KILL,
237 [SIGTERM] = SIGPROP_KILL,
238 [SIGURG] = SIGPROP_IGNORE,
239 [SIGSTOP] = SIGPROP_STOP,
240 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
241 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
242 [SIGCHLD] = SIGPROP_IGNORE,
243 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
244 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
245 [SIGIO] = SIGPROP_IGNORE,
246 [SIGXCPU] = SIGPROP_KILL,
247 [SIGXFSZ] = SIGPROP_KILL,
248 [SIGVTALRM] = SIGPROP_KILL,
249 [SIGPROF] = SIGPROP_KILL,
250 [SIGWINCH] = SIGPROP_IGNORE,
251 [SIGINFO] = SIGPROP_IGNORE,
252 [SIGUSR1] = SIGPROP_KILL,
253 [SIGUSR2] = SIGPROP_KILL,
256 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
260 int __sig = ffs(__bits); \
261 __bits &= ~(1u << (__sig - 1)); \
262 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
266 if (++__i == _SIG_WORDS) { \
270 __bits = (set)->__bits[__i]; \
275 #define SIG_FOREACH(i, set) \
276 for (int32_t __i = -1, __bits = 0; \
277 _SIG_FOREACH_ADVANCE(i, set); ) \
279 static sigset_t fastblock_mask;
282 ast_sig(struct thread *td, int tda)
285 int old_boundary, sig;
291 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
292 TDAI(TDA_AST))) == 0) {
296 * Note that TDA_SIG should be re-read from
297 * td_ast, since signal might have been delivered
298 * after we cleared td_flags above. This is one of
299 * the reason for looping check for AST condition.
300 * See comment in userret() about P_PPWAIT.
302 if ((p->p_flag & P_PPWAIT) == 0 &&
303 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
304 if (SIGPENDING(td) && ((tda | td->td_ast) &
305 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
306 thread_unlock(td); /* fix dumps */
308 "failed2 to set signal flags for ast p %p "
309 "td %p tda %#x td_ast %#x fl %#x",
310 p, td, tda, td->td_ast, td->td_flags);
319 * Check for signals. Unlocked reads of p_pendingcnt or
320 * p_siglist might cause process-directed signal to be handled
323 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
324 !SIGISEMPTY(p->p_siglist)) {
325 sigfastblock_fetch(td);
327 old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY);
328 td->td_dbgflags |= TDB_BOUNDARY;
329 mtx_lock(&p->p_sigacts->ps_mtx);
330 while ((sig = cursig(td)) != 0) {
331 KASSERT(sig >= 0, ("sig %d", sig));
334 mtx_unlock(&p->p_sigacts->ps_mtx);
335 td->td_dbgflags &= old_boundary;
339 resched_sigs = false;
343 * Handle deferred update of the fast sigblock value, after
344 * the postsig() loop was performed.
346 sigfastblock_setpend(td, resched_sigs);
350 ast_sigsuspend(struct thread *td, int tda __unused)
352 MPASS((td->td_pflags & TDP_OLDMASK) != 0);
353 td->td_pflags &= ~TDP_OLDMASK;
354 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
360 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
361 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
362 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
363 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
364 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
365 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
366 SIGFILLSET(fastblock_mask);
367 SIG_CANTMASK(fastblock_mask);
368 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
369 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
370 TDP_OLDMASK, ast_sigsuspend);
374 ksiginfo_alloc(int mwait)
376 MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
378 if (ksiginfo_zone == NULL)
380 return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
384 ksiginfo_free(ksiginfo_t *ksi)
386 uma_zfree(ksiginfo_zone, ksi);
390 ksiginfo_tryfree(ksiginfo_t *ksi)
392 if ((ksi->ksi_flags & KSI_EXT) == 0) {
393 uma_zfree(ksiginfo_zone, ksi);
400 sigqueue_init(sigqueue_t *list, struct proc *p)
402 SIGEMPTYSET(list->sq_signals);
403 SIGEMPTYSET(list->sq_kill);
404 SIGEMPTYSET(list->sq_ptrace);
405 TAILQ_INIT(&list->sq_list);
407 list->sq_flags = SQ_INIT;
411 * Get a signal's ksiginfo.
413 * 0 - signal not found
414 * others - signal number
417 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
419 struct proc *p = sq->sq_proc;
420 struct ksiginfo *ksi, *next;
423 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
425 if (!SIGISMEMBER(sq->sq_signals, signo))
428 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
430 SIGDELSET(sq->sq_ptrace, signo);
431 si->ksi_flags |= KSI_PTRACE;
433 if (SIGISMEMBER(sq->sq_kill, signo)) {
436 SIGDELSET(sq->sq_kill, signo);
439 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
440 if (ksi->ksi_signo == signo) {
442 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
443 ksi->ksi_sigq = NULL;
444 ksiginfo_copy(ksi, si);
445 if (ksiginfo_tryfree(ksi) && p != NULL)
454 SIGDELSET(sq->sq_signals, signo);
455 si->ksi_signo = signo;
460 sigqueue_take(ksiginfo_t *ksi)
466 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
470 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
471 ksi->ksi_sigq = NULL;
472 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
475 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
476 kp = TAILQ_NEXT(kp, ksi_link)) {
477 if (kp->ksi_signo == ksi->ksi_signo)
480 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
481 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
482 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
486 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
488 struct proc *p = sq->sq_proc;
489 struct ksiginfo *ksi;
492 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
495 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
498 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
499 SIGADDSET(sq->sq_kill, signo);
503 /* directly insert the ksi, don't copy it */
504 if (si->ksi_flags & KSI_INS) {
505 if (si->ksi_flags & KSI_HEAD)
506 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
508 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
513 if (__predict_false(ksiginfo_zone == NULL)) {
514 SIGADDSET(sq->sq_kill, signo);
518 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
521 } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
527 ksiginfo_copy(si, ksi);
528 ksi->ksi_signo = signo;
529 if (si->ksi_flags & KSI_HEAD)
530 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
532 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
537 if ((si->ksi_flags & KSI_PTRACE) != 0) {
538 SIGADDSET(sq->sq_ptrace, signo);
541 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
542 (si->ksi_flags & KSI_SIGQ) == 0) {
543 SIGADDSET(sq->sq_kill, signo);
551 SIGADDSET(sq->sq_signals, signo);
556 sigqueue_flush(sigqueue_t *sq)
558 struct proc *p = sq->sq_proc;
561 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
564 PROC_LOCK_ASSERT(p, MA_OWNED);
566 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
567 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
568 ksi->ksi_sigq = NULL;
569 if (ksiginfo_tryfree(ksi) && p != NULL)
573 SIGEMPTYSET(sq->sq_signals);
574 SIGEMPTYSET(sq->sq_kill);
575 SIGEMPTYSET(sq->sq_ptrace);
579 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
582 struct proc *p1, *p2;
583 ksiginfo_t *ksi, *next;
585 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
586 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
589 /* Move siginfo to target list */
590 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
591 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
592 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
595 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
602 /* Move pending bits to target list */
604 SIGSETAND(tmp, *set);
605 SIGSETOR(dst->sq_kill, tmp);
606 SIGSETNAND(src->sq_kill, tmp);
608 tmp = src->sq_ptrace;
609 SIGSETAND(tmp, *set);
610 SIGSETOR(dst->sq_ptrace, tmp);
611 SIGSETNAND(src->sq_ptrace, tmp);
613 tmp = src->sq_signals;
614 SIGSETAND(tmp, *set);
615 SIGSETOR(dst->sq_signals, tmp);
616 SIGSETNAND(src->sq_signals, tmp);
621 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
626 SIGADDSET(set, signo);
627 sigqueue_move_set(src, dst, &set);
632 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
634 struct proc *p = sq->sq_proc;
635 ksiginfo_t *ksi, *next;
637 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
639 /* Remove siginfo queue */
640 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
641 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
642 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
643 ksi->ksi_sigq = NULL;
644 if (ksiginfo_tryfree(ksi) && p != NULL)
648 SIGSETNAND(sq->sq_kill, *set);
649 SIGSETNAND(sq->sq_ptrace, *set);
650 SIGSETNAND(sq->sq_signals, *set);
654 sigqueue_delete(sigqueue_t *sq, int signo)
659 SIGADDSET(set, signo);
660 sigqueue_delete_set(sq, &set);
663 /* Remove a set of signals for a process */
665 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
670 PROC_LOCK_ASSERT(p, MA_OWNED);
672 sigqueue_init(&worklist, NULL);
673 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
675 FOREACH_THREAD_IN_PROC(p, td0)
676 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
678 sigqueue_flush(&worklist);
682 sigqueue_delete_proc(struct proc *p, int signo)
687 SIGADDSET(set, signo);
688 sigqueue_delete_set_proc(p, &set);
692 sigqueue_delete_stopmask_proc(struct proc *p)
697 SIGADDSET(set, SIGSTOP);
698 SIGADDSET(set, SIGTSTP);
699 SIGADDSET(set, SIGTTIN);
700 SIGADDSET(set, SIGTTOU);
701 sigqueue_delete_set_proc(p, &set);
705 * Determine signal that should be delivered to thread td, the current
706 * thread, 0 if none. If there is a pending stop signal with default
707 * action, the process stops in issignal().
710 cursig(struct thread *td)
712 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
713 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
714 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
715 return (SIGPENDING(td) ? issignal(td) : 0);
719 * Arrange for ast() to handle unmasked pending signals on return to user
720 * mode. This must be called whenever a signal is added to td_sigqueue or
721 * unmasked in td_sigmask.
724 signotify(struct thread *td)
727 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
730 ast_sched(td, TDA_SIG);
734 * Returns 1 (true) if altstack is configured for the thread, and the
735 * passed stack bottom address falls into the altstack range. Handles
736 * the 43 compat special case where the alt stack size is zero.
739 sigonstack(size_t sp)
744 if ((td->td_pflags & TDP_ALTSTACK) == 0)
746 #if defined(COMPAT_43)
747 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
748 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
750 return (sp >= (size_t)td->td_sigstk.ss_sp &&
751 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
758 if (sig > 0 && sig < nitems(sigproptbl))
759 return (sigproptbl[sig]);
764 sigact_flag_test(const struct sigaction *act, int flag)
768 * SA_SIGINFO is reset when signal disposition is set to
769 * ignore or default. Other flags are kept according to user
772 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
773 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
774 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
784 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
785 struct sigaction *oact, int flags)
788 struct proc *p = td->td_proc;
790 if (!_SIG_VALID(sig))
792 if (act != NULL && act->sa_handler != SIG_DFL &&
793 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
794 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
795 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
800 mtx_lock(&ps->ps_mtx);
802 memset(oact, 0, sizeof(*oact));
803 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
804 if (SIGISMEMBER(ps->ps_sigonstack, sig))
805 oact->sa_flags |= SA_ONSTACK;
806 if (!SIGISMEMBER(ps->ps_sigintr, sig))
807 oact->sa_flags |= SA_RESTART;
808 if (SIGISMEMBER(ps->ps_sigreset, sig))
809 oact->sa_flags |= SA_RESETHAND;
810 if (SIGISMEMBER(ps->ps_signodefer, sig))
811 oact->sa_flags |= SA_NODEFER;
812 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
813 oact->sa_flags |= SA_SIGINFO;
815 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
817 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
818 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
819 oact->sa_flags |= SA_NOCLDSTOP;
820 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
821 oact->sa_flags |= SA_NOCLDWAIT;
824 if ((sig == SIGKILL || sig == SIGSTOP) &&
825 act->sa_handler != SIG_DFL) {
826 mtx_unlock(&ps->ps_mtx);
832 * Change setting atomically.
835 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
836 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
837 if (sigact_flag_test(act, SA_SIGINFO)) {
838 ps->ps_sigact[_SIG_IDX(sig)] =
839 (__sighandler_t *)act->sa_sigaction;
840 SIGADDSET(ps->ps_siginfo, sig);
842 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
843 SIGDELSET(ps->ps_siginfo, sig);
845 if (!sigact_flag_test(act, SA_RESTART))
846 SIGADDSET(ps->ps_sigintr, sig);
848 SIGDELSET(ps->ps_sigintr, sig);
849 if (sigact_flag_test(act, SA_ONSTACK))
850 SIGADDSET(ps->ps_sigonstack, sig);
852 SIGDELSET(ps->ps_sigonstack, sig);
853 if (sigact_flag_test(act, SA_RESETHAND))
854 SIGADDSET(ps->ps_sigreset, sig);
856 SIGDELSET(ps->ps_sigreset, sig);
857 if (sigact_flag_test(act, SA_NODEFER))
858 SIGADDSET(ps->ps_signodefer, sig);
860 SIGDELSET(ps->ps_signodefer, sig);
861 if (sig == SIGCHLD) {
862 if (act->sa_flags & SA_NOCLDSTOP)
863 ps->ps_flag |= PS_NOCLDSTOP;
865 ps->ps_flag &= ~PS_NOCLDSTOP;
866 if (act->sa_flags & SA_NOCLDWAIT) {
868 * Paranoia: since SA_NOCLDWAIT is implemented
869 * by reparenting the dying child to PID 1 (and
870 * trust it to reap the zombie), PID 1 itself
871 * is forbidden to set SA_NOCLDWAIT.
874 ps->ps_flag &= ~PS_NOCLDWAIT;
876 ps->ps_flag |= PS_NOCLDWAIT;
878 ps->ps_flag &= ~PS_NOCLDWAIT;
879 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
880 ps->ps_flag |= PS_CLDSIGIGN;
882 ps->ps_flag &= ~PS_CLDSIGIGN;
885 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
886 * and for signals set to SIG_DFL where the default is to
887 * ignore. However, don't put SIGCONT in ps_sigignore, as we
888 * have to restart the process.
890 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
891 (sigprop(sig) & SIGPROP_IGNORE &&
892 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
893 /* never to be seen again */
894 sigqueue_delete_proc(p, sig);
896 /* easier in psignal */
897 SIGADDSET(ps->ps_sigignore, sig);
898 SIGDELSET(ps->ps_sigcatch, sig);
900 SIGDELSET(ps->ps_sigignore, sig);
901 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
902 SIGDELSET(ps->ps_sigcatch, sig);
904 SIGADDSET(ps->ps_sigcatch, sig);
906 #ifdef COMPAT_FREEBSD4
907 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
908 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
909 (flags & KSA_FREEBSD4) == 0)
910 SIGDELSET(ps->ps_freebsd4, sig);
912 SIGADDSET(ps->ps_freebsd4, sig);
915 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
916 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
917 (flags & KSA_OSIGSET) == 0)
918 SIGDELSET(ps->ps_osigset, sig);
920 SIGADDSET(ps->ps_osigset, sig);
923 mtx_unlock(&ps->ps_mtx);
928 #ifndef _SYS_SYSPROTO_H_
929 struct sigaction_args {
931 struct sigaction *act;
932 struct sigaction *oact;
936 sys_sigaction(struct thread *td, struct sigaction_args *uap)
938 struct sigaction act, oact;
939 struct sigaction *actp, *oactp;
942 actp = (uap->act != NULL) ? &act : NULL;
943 oactp = (uap->oact != NULL) ? &oact : NULL;
945 error = copyin(uap->act, actp, sizeof(act));
949 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
951 error = copyout(oactp, uap->oact, sizeof(oact));
955 #ifdef COMPAT_FREEBSD4
956 #ifndef _SYS_SYSPROTO_H_
957 struct freebsd4_sigaction_args {
959 struct sigaction *act;
960 struct sigaction *oact;
964 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
966 struct sigaction act, oact;
967 struct sigaction *actp, *oactp;
970 actp = (uap->act != NULL) ? &act : NULL;
971 oactp = (uap->oact != NULL) ? &oact : NULL;
973 error = copyin(uap->act, actp, sizeof(act));
977 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
979 error = copyout(oactp, uap->oact, sizeof(oact));
982 #endif /* COMAPT_FREEBSD4 */
984 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
985 #ifndef _SYS_SYSPROTO_H_
986 struct osigaction_args {
988 struct osigaction *nsa;
989 struct osigaction *osa;
993 osigaction(struct thread *td, struct osigaction_args *uap)
995 struct osigaction sa;
996 struct sigaction nsa, osa;
997 struct sigaction *nsap, *osap;
1000 if (uap->signum <= 0 || uap->signum >= ONSIG)
1003 nsap = (uap->nsa != NULL) ? &nsa : NULL;
1004 osap = (uap->osa != NULL) ? &osa : NULL;
1007 error = copyin(uap->nsa, &sa, sizeof(sa));
1010 nsap->sa_handler = sa.sa_handler;
1011 nsap->sa_flags = sa.sa_flags;
1012 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
1014 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1015 if (osap && !error) {
1016 sa.sa_handler = osap->sa_handler;
1017 sa.sa_flags = osap->sa_flags;
1018 SIG2OSIG(osap->sa_mask, sa.sa_mask);
1019 error = copyout(&sa, uap->osa, sizeof(sa));
1024 #if !defined(__i386__)
1025 /* Avoid replicating the same stub everywhere */
1027 osigreturn(struct thread *td, struct osigreturn_args *uap)
1030 return (nosys(td, (struct nosys_args *)uap));
1033 #endif /* COMPAT_43 */
1036 * Initialize signal state for process 0;
1037 * set to ignore signals that are ignored by default.
1040 siginit(struct proc *p)
1047 mtx_lock(&ps->ps_mtx);
1048 for (i = 1; i <= NSIG; i++) {
1049 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
1050 SIGADDSET(ps->ps_sigignore, i);
1053 mtx_unlock(&ps->ps_mtx);
1058 * Reset specified signal to the default disposition.
1061 sigdflt(struct sigacts *ps, int sig)
1064 mtx_assert(&ps->ps_mtx, MA_OWNED);
1065 SIGDELSET(ps->ps_sigcatch, sig);
1066 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1067 SIGADDSET(ps->ps_sigignore, sig);
1068 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1069 SIGDELSET(ps->ps_siginfo, sig);
1073 * Reset signals for an exec of the specified process.
1076 execsigs(struct proc *p)
1082 * Reset caught signals. Held signals remain held
1083 * through td_sigmask (unless they were caught,
1084 * and are now ignored by default).
1086 PROC_LOCK_ASSERT(p, MA_OWNED);
1088 mtx_lock(&ps->ps_mtx);
1092 * Reset stack state to the user stack.
1093 * Clear set of signals caught on the signal stack.
1096 MPASS(td->td_proc == p);
1097 td->td_sigstk.ss_flags = SS_DISABLE;
1098 td->td_sigstk.ss_size = 0;
1099 td->td_sigstk.ss_sp = 0;
1100 td->td_pflags &= ~TDP_ALTSTACK;
1102 * Reset no zombies if child dies flag as Solaris does.
1104 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1105 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1106 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1107 mtx_unlock(&ps->ps_mtx);
1111 * kern_sigprocmask()
1113 * Manipulate signal mask.
1116 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1119 sigset_t new_block, oset1;
1124 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1125 PROC_LOCK_ASSERT(p, MA_OWNED);
1128 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1129 ? MA_OWNED : MA_NOTOWNED);
1131 *oset = td->td_sigmask;
1138 oset1 = td->td_sigmask;
1139 SIGSETOR(td->td_sigmask, *set);
1140 new_block = td->td_sigmask;
1141 SIGSETNAND(new_block, oset1);
1144 SIGSETNAND(td->td_sigmask, *set);
1149 oset1 = td->td_sigmask;
1150 if (flags & SIGPROCMASK_OLD)
1151 SIGSETLO(td->td_sigmask, *set);
1153 td->td_sigmask = *set;
1154 new_block = td->td_sigmask;
1155 SIGSETNAND(new_block, oset1);
1164 * The new_block set contains signals that were not previously
1165 * blocked, but are blocked now.
1167 * In case we block any signal that was not previously blocked
1168 * for td, and process has the signal pending, try to schedule
1169 * signal delivery to some thread that does not block the
1170 * signal, possibly waking it up.
1172 if (p->p_numthreads != 1)
1173 reschedule_signals(p, new_block, flags);
1177 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1182 #ifndef _SYS_SYSPROTO_H_
1183 struct sigprocmask_args {
1185 const sigset_t *set;
1190 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1193 sigset_t *setp, *osetp;
1196 setp = (uap->set != NULL) ? &set : NULL;
1197 osetp = (uap->oset != NULL) ? &oset : NULL;
1199 error = copyin(uap->set, setp, sizeof(set));
1203 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1204 if (osetp && !error) {
1205 error = copyout(osetp, uap->oset, sizeof(oset));
1210 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1211 #ifndef _SYS_SYSPROTO_H_
1212 struct osigprocmask_args {
1218 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1223 OSIG2SIG(uap->mask, set);
1224 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1225 SIG2OSIG(oset, td->td_retval[0]);
1228 #endif /* COMPAT_43 */
1231 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1237 error = copyin(uap->set, &set, sizeof(set));
1239 td->td_retval[0] = error;
1243 error = kern_sigtimedwait(td, set, &ksi, NULL);
1246 * sigwait() function shall not return EINTR, but
1247 * the syscall does. Non-ancient libc provides the
1248 * wrapper which hides EINTR. Otherwise, EINTR return
1249 * is used by libthr to handle required cancellation
1250 * point in the sigwait().
1252 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1254 td->td_retval[0] = error;
1258 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1259 td->td_retval[0] = error;
1264 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1267 struct timespec *timeout;
1273 error = copyin(uap->timeout, &ts, sizeof(ts));
1281 error = copyin(uap->set, &set, sizeof(set));
1285 error = kern_sigtimedwait(td, set, &ksi, timeout);
1290 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1293 td->td_retval[0] = ksi.ksi_signo;
1298 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1304 error = copyin(uap->set, &set, sizeof(set));
1308 error = kern_sigtimedwait(td, set, &ksi, NULL);
1313 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1316 td->td_retval[0] = ksi.ksi_signo;
1321 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1325 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1329 thr->td_si.si_signo = 0;
1334 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1335 struct timespec *timeout)
1338 sigset_t saved_mask, new_block;
1340 int error, sig, timevalid = 0;
1341 sbintime_t sbt, precision, tsbt;
1349 /* Ensure the sigfastblock value is up to date. */
1350 sigfastblock_fetch(td);
1352 if (timeout != NULL) {
1353 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1356 if (ts.tv_sec < INT32_MAX / 2) {
1359 precision >>= tc_precexp;
1360 if (TIMESEL(&sbt, tsbt))
1364 precision = sbt = 0;
1367 precision = sbt = 0;
1369 /* Some signals can not be waited for. */
1370 SIG_CANTMASK(waitset);
1373 saved_mask = td->td_sigmask;
1374 SIGSETNAND(td->td_sigmask, waitset);
1375 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1376 !kern_sig_discard_ign) {
1378 td->td_flags |= TDF_SIGWAIT;
1382 mtx_lock(&ps->ps_mtx);
1384 mtx_unlock(&ps->ps_mtx);
1385 KASSERT(sig >= 0, ("sig %d", sig));
1386 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1387 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1388 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1398 * POSIX says this must be checked after looking for pending
1401 if (timeout != NULL && !timevalid) {
1411 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1412 "sigwait", sbt, precision, C_ABSOLUTE);
1414 /* The syscalls can not be restarted. */
1415 if (error == ERESTART)
1419 * If PTRACE_SCE or PTRACE_SCX were set after
1420 * userspace entered the syscall, return spurious
1421 * EINTR after wait was done. Only do this as last
1422 * resort after rechecking for possible queued signals
1423 * and expired timeouts.
1425 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1429 td->td_flags &= ~TDF_SIGWAIT;
1432 new_block = saved_mask;
1433 SIGSETNAND(new_block, td->td_sigmask);
1434 td->td_sigmask = saved_mask;
1436 * Fewer signals can be delivered to us, reschedule signal
1439 if (p->p_numthreads != 1)
1440 reschedule_signals(p, new_block, 0);
1443 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1445 if (ksi->ksi_code == SI_TIMER)
1446 itimer_accept(p, ksi->ksi_timerid, ksi);
1449 if (KTRPOINT(td, KTR_PSIG)) {
1452 mtx_lock(&ps->ps_mtx);
1453 action = ps->ps_sigact[_SIG_IDX(sig)];
1454 mtx_unlock(&ps->ps_mtx);
1455 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1458 if (sig == SIGKILL) {
1459 proc_td_siginfo_capture(td, &ksi->ksi_info);
1467 #ifndef _SYS_SYSPROTO_H_
1468 struct sigpending_args {
1473 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1475 struct proc *p = td->td_proc;
1479 pending = p->p_sigqueue.sq_signals;
1480 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1482 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1485 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1486 #ifndef _SYS_SYSPROTO_H_
1487 struct osigpending_args {
1492 osigpending(struct thread *td, struct osigpending_args *uap)
1494 struct proc *p = td->td_proc;
1498 pending = p->p_sigqueue.sq_signals;
1499 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1501 SIG2OSIG(pending, td->td_retval[0]);
1504 #endif /* COMPAT_43 */
1506 #if defined(COMPAT_43)
1508 * Generalized interface signal handler, 4.3-compatible.
1510 #ifndef _SYS_SYSPROTO_H_
1511 struct osigvec_args {
1519 osigvec(struct thread *td, struct osigvec_args *uap)
1522 struct sigaction nsa, osa;
1523 struct sigaction *nsap, *osap;
1526 if (uap->signum <= 0 || uap->signum >= ONSIG)
1528 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1529 osap = (uap->osv != NULL) ? &osa : NULL;
1531 error = copyin(uap->nsv, &vec, sizeof(vec));
1534 nsap->sa_handler = vec.sv_handler;
1535 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1536 nsap->sa_flags = vec.sv_flags;
1537 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1539 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1540 if (osap && !error) {
1541 vec.sv_handler = osap->sa_handler;
1542 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1543 vec.sv_flags = osap->sa_flags;
1544 vec.sv_flags &= ~SA_NOCLDWAIT;
1545 vec.sv_flags ^= SA_RESTART;
1546 error = copyout(&vec, uap->osv, sizeof(vec));
1551 #ifndef _SYS_SYSPROTO_H_
1552 struct osigblock_args {
1557 osigblock(struct thread *td, struct osigblock_args *uap)
1561 OSIG2SIG(uap->mask, set);
1562 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1563 SIG2OSIG(oset, td->td_retval[0]);
1567 #ifndef _SYS_SYSPROTO_H_
1568 struct osigsetmask_args {
1573 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1577 OSIG2SIG(uap->mask, set);
1578 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1579 SIG2OSIG(oset, td->td_retval[0]);
1582 #endif /* COMPAT_43 */
1585 * Suspend calling thread until signal, providing mask to be set in the
1588 #ifndef _SYS_SYSPROTO_H_
1589 struct sigsuspend_args {
1590 const sigset_t *sigmask;
1595 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1600 error = copyin(uap->sigmask, &mask, sizeof(mask));
1603 return (kern_sigsuspend(td, mask));
1607 kern_sigsuspend(struct thread *td, sigset_t mask)
1609 struct proc *p = td->td_proc;
1612 /* Ensure the sigfastblock value is up to date. */
1613 sigfastblock_fetch(td);
1616 * When returning from sigsuspend, we want
1617 * the old mask to be restored after the
1618 * signal handler has finished. Thus, we
1619 * save it here and mark the sigacts structure
1623 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1624 SIGPROCMASK_PROC_LOCKED);
1625 td->td_pflags |= TDP_OLDMASK;
1626 ast_sched(td, TDA_SIGSUSPEND);
1629 * Process signals now. Otherwise, we can get spurious wakeup
1630 * due to signal entered process queue, but delivered to other
1631 * thread. But sigsuspend should return only on signal
1634 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1635 for (has_sig = 0; !has_sig;) {
1636 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1639 thread_suspend_check(0);
1640 mtx_lock(&p->p_sigacts->ps_mtx);
1641 while ((sig = cursig(td)) != 0) {
1642 KASSERT(sig >= 0, ("sig %d", sig));
1643 has_sig += postsig(sig);
1645 mtx_unlock(&p->p_sigacts->ps_mtx);
1648 * If PTRACE_SCE or PTRACE_SCX were set after
1649 * userspace entered the syscall, return spurious
1652 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1656 td->td_errno = EINTR;
1657 td->td_pflags |= TDP_NERRNO;
1658 return (EJUSTRETURN);
1661 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1663 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1664 * convention: libc stub passes mask, not pointer, to save a copyin.
1666 #ifndef _SYS_SYSPROTO_H_
1667 struct osigsuspend_args {
1673 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1677 OSIG2SIG(uap->mask, mask);
1678 return (kern_sigsuspend(td, mask));
1680 #endif /* COMPAT_43 */
1682 #if defined(COMPAT_43)
1683 #ifndef _SYS_SYSPROTO_H_
1684 struct osigstack_args {
1685 struct sigstack *nss;
1686 struct sigstack *oss;
1691 osigstack(struct thread *td, struct osigstack_args *uap)
1693 struct sigstack nss, oss;
1696 if (uap->nss != NULL) {
1697 error = copyin(uap->nss, &nss, sizeof(nss));
1701 oss.ss_sp = td->td_sigstk.ss_sp;
1702 oss.ss_onstack = sigonstack(cpu_getstack(td));
1703 if (uap->nss != NULL) {
1704 td->td_sigstk.ss_sp = nss.ss_sp;
1705 td->td_sigstk.ss_size = 0;
1706 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1707 td->td_pflags |= TDP_ALTSTACK;
1709 if (uap->oss != NULL)
1710 error = copyout(&oss, uap->oss, sizeof(oss));
1714 #endif /* COMPAT_43 */
1716 #ifndef _SYS_SYSPROTO_H_
1717 struct sigaltstack_args {
1724 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1729 if (uap->ss != NULL) {
1730 error = copyin(uap->ss, &ss, sizeof(ss));
1734 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1735 (uap->oss != NULL) ? &oss : NULL);
1738 if (uap->oss != NULL)
1739 error = copyout(&oss, uap->oss, sizeof(stack_t));
1744 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1746 struct proc *p = td->td_proc;
1749 oonstack = sigonstack(cpu_getstack(td));
1752 *oss = td->td_sigstk;
1753 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1754 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1760 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1762 if (!(ss->ss_flags & SS_DISABLE)) {
1763 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1766 td->td_sigstk = *ss;
1767 td->td_pflags |= TDP_ALTSTACK;
1769 td->td_pflags &= ~TDP_ALTSTACK;
1775 struct killpg1_ctx {
1785 killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
1789 err = p_cansignal(arg->td, p, arg->sig);
1790 if (err == 0 && arg->sig != 0)
1791 pksignal(p, arg->sig, arg->ksi);
1796 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1801 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1804 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1805 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1809 killpg1_sendsig_locked(p, arg);
1814 kill_processes_prison_cb(struct proc *p, void *arg)
1816 struct killpg1_ctx *ctx = arg;
1818 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1819 (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
1822 killpg1_sendsig_locked(p, ctx);
1826 * Common code for kill process group/broadcast kill.
1827 * td is the calling thread, as usual.
1830 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1834 struct killpg1_ctx arg;
1846 prison_proc_iterate(td->td_ucred->cr_prison,
1847 kill_processes_prison_cb, &arg);
1850 sx_slock(&proctree_lock);
1853 * zero pgid means send to my process group.
1855 pgrp = td->td_proc->p_pgrp;
1858 pgrp = pgfind(pgid);
1860 sx_sunlock(&proctree_lock);
1864 sx_sunlock(&proctree_lock);
1865 if (!sx_try_xlock(&pgrp->pg_killsx)) {
1867 sx_xlock(&pgrp->pg_killsx);
1868 sx_xunlock(&pgrp->pg_killsx);
1871 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1872 killpg1_sendsig(p, false, &arg);
1875 sx_xunlock(&pgrp->pg_killsx);
1877 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1878 if (arg.ret == 0 && !arg.sent)
1879 arg.ret = arg.found ? EPERM : ESRCH;
1883 #ifndef _SYS_SYSPROTO_H_
1891 sys_kill(struct thread *td, struct kill_args *uap)
1894 return (kern_kill(td, uap->pid, uap->signum));
1898 kern_kill(struct thread *td, pid_t pid, int signum)
1905 * A process in capability mode can send signals only to himself.
1906 * The main rationale behind this is that abort(3) is implemented as
1907 * kill(getpid(), SIGABRT).
1909 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1912 AUDIT_ARG_SIGNUM(signum);
1914 if ((u_int)signum > _SIG_MAXSIG)
1917 ksiginfo_init(&ksi);
1918 ksi.ksi_signo = signum;
1919 ksi.ksi_code = SI_USER;
1920 ksi.ksi_pid = td->td_proc->p_pid;
1921 ksi.ksi_uid = td->td_ucred->cr_ruid;
1924 /* kill single process */
1925 if ((p = pfind_any(pid)) == NULL)
1927 AUDIT_ARG_PROCESS(p);
1928 error = p_cansignal(td, p, signum);
1929 if (error == 0 && signum)
1930 pksignal(p, signum, &ksi);
1935 case -1: /* broadcast signal */
1936 return (killpg1(td, signum, 0, 1, &ksi));
1937 case 0: /* signal own process group */
1938 return (killpg1(td, signum, 0, 0, &ksi));
1939 default: /* negative explicit process group */
1940 return (killpg1(td, signum, -pid, 0, &ksi));
1946 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1951 AUDIT_ARG_SIGNUM(uap->signum);
1952 AUDIT_ARG_FD(uap->fd);
1953 if ((u_int)uap->signum > _SIG_MAXSIG)
1956 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1959 AUDIT_ARG_PROCESS(p);
1960 error = p_cansignal(td, p, uap->signum);
1961 if (error == 0 && uap->signum)
1962 kern_psignal(p, uap->signum);
1967 #if defined(COMPAT_43)
1968 #ifndef _SYS_SYSPROTO_H_
1969 struct okillpg_args {
1976 okillpg(struct thread *td, struct okillpg_args *uap)
1980 AUDIT_ARG_SIGNUM(uap->signum);
1981 AUDIT_ARG_PID(uap->pgid);
1982 if ((u_int)uap->signum > _SIG_MAXSIG)
1985 ksiginfo_init(&ksi);
1986 ksi.ksi_signo = uap->signum;
1987 ksi.ksi_code = SI_USER;
1988 ksi.ksi_pid = td->td_proc->p_pid;
1989 ksi.ksi_uid = td->td_ucred->cr_ruid;
1990 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1992 #endif /* COMPAT_43 */
1994 #ifndef _SYS_SYSPROTO_H_
1995 struct sigqueue_args {
1998 /* union sigval */ void *value;
2002 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
2006 sv.sival_ptr = uap->value;
2008 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2012 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
2018 if ((u_int)signum > _SIG_MAXSIG)
2022 * Specification says sigqueue can only send signal to
2028 if ((p = pfind_any(pid)) == NULL)
2030 error = p_cansignal(td, p, signum);
2031 if (error == 0 && signum != 0) {
2032 ksiginfo_init(&ksi);
2033 ksi.ksi_flags = KSI_SIGQ;
2034 ksi.ksi_signo = signum;
2035 ksi.ksi_code = SI_QUEUE;
2036 ksi.ksi_pid = td->td_proc->p_pid;
2037 ksi.ksi_uid = td->td_ucred->cr_ruid;
2038 ksi.ksi_value = *value;
2039 error = pksignal(p, ksi.ksi_signo, &ksi);
2046 * Send a signal to a process group. If checktty is 1,
2047 * limit to members which have a controlling terminal.
2050 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2055 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2056 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2058 if (p->p_state == PRS_NORMAL &&
2059 (checkctty == 0 || p->p_flag & P_CONTROLT))
2060 pksignal(p, sig, ksi);
2067 * Recalculate the signal mask and reset the signal disposition after
2068 * usermode frame for delivery is formed. Should be called after
2069 * mach-specific routine, because sysent->sv_sendsig() needs correct
2070 * ps_siginfo and signal mask.
2073 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2077 mtx_assert(&ps->ps_mtx, MA_OWNED);
2078 td->td_ru.ru_nsignals++;
2079 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2080 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2081 SIGADDSET(mask, sig);
2082 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2083 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2084 if (SIGISMEMBER(ps->ps_sigreset, sig))
2089 * Send a signal caused by a trap to the current thread. If it will be
2090 * caught immediately, deliver it with correct code. Otherwise, post it
2094 trapsignal(struct thread *td, ksiginfo_t *ksi)
2102 sig = ksi->ksi_signo;
2103 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2105 sigfastblock_fetch(td);
2108 mtx_lock(&ps->ps_mtx);
2109 sigmask = td->td_sigmask;
2110 if (td->td_sigblock_val != 0)
2111 SIGSETOR(sigmask, fastblock_mask);
2112 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2113 !SIGISMEMBER(sigmask, sig)) {
2115 if (KTRPOINT(curthread, KTR_PSIG))
2116 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2117 &td->td_sigmask, ksi->ksi_code);
2119 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2120 ksi, &td->td_sigmask);
2121 postsig_done(sig, td, ps);
2122 mtx_unlock(&ps->ps_mtx);
2125 * Avoid a possible infinite loop if the thread
2126 * masking the signal or process is ignoring the
2129 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2130 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2131 SIGDELSET(td->td_sigmask, sig);
2132 SIGDELSET(ps->ps_sigcatch, sig);
2133 SIGDELSET(ps->ps_sigignore, sig);
2134 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2135 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2136 td->td_sigblock_val = 0;
2138 mtx_unlock(&ps->ps_mtx);
2139 p->p_sig = sig; /* XXX to verify code */
2140 tdsendsignal(p, td, sig, ksi);
2145 static struct thread *
2146 sigtd(struct proc *p, int sig, bool fast_sigblock)
2148 struct thread *td, *signal_td;
2150 PROC_LOCK_ASSERT(p, MA_OWNED);
2151 MPASS(!fast_sigblock || p == curproc);
2154 * Check if current thread can handle the signal without
2155 * switching context to another thread.
2157 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2158 (!fast_sigblock || curthread->td_sigblock_val == 0))
2161 /* Find a non-stopped thread that does not mask the signal. */
2163 FOREACH_THREAD_IN_PROC(p, td) {
2164 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2165 td != curthread || td->td_sigblock_val == 0) &&
2166 (td->td_flags & TDF_BOUNDARY) == 0) {
2171 /* Select random (first) thread if no better match was found. */
2172 if (signal_td == NULL)
2173 signal_td = FIRST_THREAD_IN_PROC(p);
2178 * Send the signal to the process. If the signal has an action, the action
2179 * is usually performed by the target process rather than the caller; we add
2180 * the signal to the set of pending signals for the process.
2183 * o When a stop signal is sent to a sleeping process that takes the
2184 * default action, the process is stopped without awakening it.
2185 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2186 * regardless of the signal action (eg, blocked or ignored).
2188 * Other ignored signals are discarded immediately.
2190 * NB: This function may be entered from the debugger via the "kill" DDB
2191 * command. There is little that can be done to mitigate the possibly messy
2192 * side effects of this unwise possibility.
2195 kern_psignal(struct proc *p, int sig)
2199 ksiginfo_init(&ksi);
2200 ksi.ksi_signo = sig;
2201 ksi.ksi_code = SI_KERNEL;
2202 (void) tdsendsignal(p, NULL, sig, &ksi);
2206 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2209 return (tdsendsignal(p, NULL, sig, ksi));
2212 /* Utility function for finding a thread to send signal event to. */
2214 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2218 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2219 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2231 tdsignal(struct thread *td, int sig)
2235 ksiginfo_init(&ksi);
2236 ksi.ksi_signo = sig;
2237 ksi.ksi_code = SI_KERNEL;
2238 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2242 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2245 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2249 sig_sleepq_abort(struct thread *td, int intrval)
2251 THREAD_LOCK_ASSERT(td, MA_OWNED);
2253 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2257 return (sleepq_abort(td, intrval));
2261 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2264 sigqueue_t *sigqueue;
2271 MPASS(td == NULL || p == td->td_proc);
2272 PROC_LOCK_ASSERT(p, MA_OWNED);
2274 if (!_SIG_VALID(sig))
2275 panic("%s(): invalid signal %d", __func__, sig);
2277 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2280 * IEEE Std 1003.1-2001: return success when killing a zombie.
2282 if (p->p_state == PRS_ZOMBIE) {
2283 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2284 ksiginfo_tryfree(ksi);
2289 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2290 prop = sigprop(sig);
2293 td = sigtd(p, sig, false);
2294 sigqueue = &p->p_sigqueue;
2296 sigqueue = &td->td_sigqueue;
2298 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2301 * If the signal is being ignored, then we forget about it
2302 * immediately, except when the target process executes
2303 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2304 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2306 mtx_lock(&ps->ps_mtx);
2307 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2308 if (kern_sig_discard_ign &&
2309 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2310 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2312 mtx_unlock(&ps->ps_mtx);
2313 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2314 ksiginfo_tryfree(ksi);
2321 if (SIGISMEMBER(td->td_sigmask, sig))
2323 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2327 if (SIGISMEMBER(ps->ps_sigintr, sig))
2332 mtx_unlock(&ps->ps_mtx);
2334 if (prop & SIGPROP_CONT)
2335 sigqueue_delete_stopmask_proc(p);
2336 else if (prop & SIGPROP_STOP) {
2338 * If sending a tty stop signal to a member of an orphaned
2339 * process group, discard the signal here if the action
2340 * is default; don't stop the process below if sleeping,
2341 * and don't clear any pending SIGCONT.
2343 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2344 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2345 action == SIG_DFL) {
2346 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2347 ksiginfo_tryfree(ksi);
2350 sigqueue_delete_proc(p, SIGCONT);
2351 if (p->p_flag & P_CONTINUED) {
2352 p->p_flag &= ~P_CONTINUED;
2353 PROC_LOCK(p->p_pptr);
2354 sigqueue_take(p->p_ksi);
2355 PROC_UNLOCK(p->p_pptr);
2359 ret = sigqueue_add(sigqueue, sig, ksi);
2364 * Defer further processing for signals which are held,
2365 * except that stopped processes must be continued by SIGCONT.
2367 if (action == SIG_HOLD &&
2368 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2374 * Some signals have a process-wide effect and a per-thread
2375 * component. Most processing occurs when the process next
2376 * tries to cross the user boundary, however there are some
2377 * times when processing needs to be done immediately, such as
2378 * waking up threads so that they can cross the user boundary.
2379 * We try to do the per-process part here.
2381 if (P_SHOULDSTOP(p)) {
2382 KASSERT(!(p->p_flag & P_WEXIT),
2383 ("signal to stopped but exiting process"));
2384 if (sig == SIGKILL) {
2386 * If traced process is already stopped,
2387 * then no further action is necessary.
2389 if (p->p_flag & P_TRACED)
2392 * SIGKILL sets process running.
2393 * It will die elsewhere.
2394 * All threads must be restarted.
2396 p->p_flag &= ~P_STOPPED_SIG;
2400 if (prop & SIGPROP_CONT) {
2402 * If traced process is already stopped,
2403 * then no further action is necessary.
2405 if (p->p_flag & P_TRACED)
2408 * If SIGCONT is default (or ignored), we continue the
2409 * process but don't leave the signal in sigqueue as
2410 * it has no further action. If SIGCONT is held, we
2411 * continue the process and leave the signal in
2412 * sigqueue. If the process catches SIGCONT, let it
2413 * handle the signal itself. If it isn't waiting on
2414 * an event, it goes back to run state.
2415 * Otherwise, process goes back to sleep state.
2417 p->p_flag &= ~P_STOPPED_SIG;
2419 if (p->p_numthreads == p->p_suspcount) {
2421 p->p_flag |= P_CONTINUED;
2422 p->p_xsig = SIGCONT;
2423 PROC_LOCK(p->p_pptr);
2424 childproc_continued(p);
2425 PROC_UNLOCK(p->p_pptr);
2428 if (action == SIG_DFL) {
2429 thread_unsuspend(p);
2431 sigqueue_delete(sigqueue, sig);
2434 if (action == SIG_CATCH) {
2436 * The process wants to catch it so it needs
2437 * to run at least one thread, but which one?
2443 * The signal is not ignored or caught.
2445 thread_unsuspend(p);
2450 if (prop & SIGPROP_STOP) {
2452 * If traced process is already stopped,
2453 * then no further action is necessary.
2455 if (p->p_flag & P_TRACED)
2458 * Already stopped, don't need to stop again
2459 * (If we did the shell could get confused).
2460 * Just make sure the signal STOP bit set.
2462 p->p_flag |= P_STOPPED_SIG;
2463 sigqueue_delete(sigqueue, sig);
2468 * All other kinds of signals:
2469 * If a thread is sleeping interruptibly, simulate a
2470 * wakeup so that when it is continued it will be made
2471 * runnable and can look at the signal. However, don't make
2472 * the PROCESS runnable, leave it stopped.
2473 * It may run a bit until it hits a thread_suspend_check().
2477 if (TD_CAN_ABORT(td))
2478 wakeup_swapper = sig_sleepq_abort(td, intrval);
2484 * Mutexes are short lived. Threads waiting on them will
2485 * hit thread_suspend_check() soon.
2487 } else if (p->p_state == PRS_NORMAL) {
2488 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2489 tdsigwakeup(td, sig, action, intrval);
2493 MPASS(action == SIG_DFL);
2495 if (prop & SIGPROP_STOP) {
2496 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2498 p->p_flag |= P_STOPPED_SIG;
2501 wakeup_swapper = sig_suspend_threads(td, p);
2502 if (p->p_numthreads == p->p_suspcount) {
2504 * only thread sending signal to another
2505 * process can reach here, if thread is sending
2506 * signal to its process, because thread does
2507 * not suspend itself here, p_numthreads
2508 * should never be equal to p_suspcount.
2512 sigqueue_delete_proc(p, p->p_xsig);
2518 /* Not in "NORMAL" state. discard the signal. */
2519 sigqueue_delete(sigqueue, sig);
2524 * The process is not stopped so we need to apply the signal to all the
2528 tdsigwakeup(td, sig, action, intrval);
2530 thread_unsuspend(p);
2533 itimer_proc_continue(p);
2534 kqtimer_proc_continue(p);
2536 /* If we jump here, proc slock should not be owned. */
2537 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2545 * The force of a signal has been directed against a single
2546 * thread. We need to see what we can do about knocking it
2547 * out of any sleep it may be in etc.
2550 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2552 struct proc *p = td->td_proc;
2553 int prop, wakeup_swapper;
2555 PROC_LOCK_ASSERT(p, MA_OWNED);
2556 prop = sigprop(sig);
2561 * Bring the priority of a thread up if we want it to get
2562 * killed in this lifetime. Be careful to avoid bumping the
2563 * priority of the idle thread, since we still allow to signal
2566 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2567 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2568 sched_prio(td, PUSER);
2569 if (TD_ON_SLEEPQ(td)) {
2571 * If thread is sleeping uninterruptibly
2572 * we can't interrupt the sleep... the signal will
2573 * be noticed when the process returns through
2574 * trap() or syscall().
2576 if ((td->td_flags & TDF_SINTR) == 0)
2579 * If SIGCONT is default (or ignored) and process is
2580 * asleep, we are finished; the process should not
2583 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2586 sigqueue_delete(&p->p_sigqueue, sig);
2588 * It may be on either list in this state.
2589 * Remove from both for now.
2591 sigqueue_delete(&td->td_sigqueue, sig);
2596 * Don't awaken a sleeping thread for SIGSTOP if the
2597 * STOP signal is deferred.
2599 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2600 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2604 * Give low priority threads a better chance to run.
2606 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2607 sched_prio(td, PUSER);
2609 wakeup_swapper = sig_sleepq_abort(td, intrval);
2617 * Other states do nothing with the signal immediately,
2618 * other than kicking ourselves if we are running.
2619 * It will either never be noticed, or noticed very soon.
2622 if (TD_IS_RUNNING(td) && td != curthread)
2632 ptrace_coredumpreq(struct thread *td, struct proc *p,
2633 struct thr_coredump_req *tcq)
2637 if (p->p_sysent->sv_coredump == NULL) {
2638 tcq->tc_error = ENOSYS;
2642 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2643 tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2644 tcq->tc_limit, tcq->tc_flags);
2645 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2649 ptrace_syscallreq(struct thread *td, struct proc *p,
2650 struct thr_syscall_req *tsr)
2652 struct sysentvec *sv;
2654 register_t rv_saved[2];
2657 bool audited, sy_thr_static;
2660 if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2661 tsr->ts_ret.sr_error = ENOSYS;
2665 sc = tsr->ts_sa.code;
2666 if (sc == SYS_syscall || sc == SYS___syscall) {
2667 sc = tsr->ts_sa.args[0];
2668 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2669 sizeof(register_t) * (tsr->ts_nargs - 1));
2672 tsr->ts_sa.callp = se = &sv->sv_table[sc];
2674 VM_CNT_INC(v_syscall);
2676 if (__predict_false(td->td_cowgen != atomic_load_int(
2677 &td->td_proc->p_cowgen)))
2678 thread_cow_update(td);
2680 #ifdef CAPABILITY_MODE
2681 if (IN_CAPABILITY_MODE(td) && (se->sy_flags & SYF_CAPENABLED) == 0) {
2682 tsr->ts_ret.sr_error = ECAPMODE;
2687 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2688 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2690 if (!sy_thr_static) {
2691 error = syscall_thread_enter(td, &se);
2692 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2694 tsr->ts_ret.sr_error = error;
2699 rv_saved[0] = td->td_retval[0];
2700 rv_saved[1] = td->td_retval[1];
2701 nerror = td->td_errno;
2702 td->td_retval[0] = 0;
2703 td->td_retval[1] = 0;
2705 #ifdef KDTRACE_HOOKS
2706 if (se->sy_entry != 0)
2707 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2709 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2710 #ifdef KDTRACE_HOOKS
2711 if (se->sy_return != 0)
2712 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2713 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2716 tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2717 tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2718 td->td_retval[0] = rv_saved[0];
2719 td->td_retval[1] = rv_saved[1];
2720 td->td_errno = nerror;
2723 AUDIT_SYSCALL_EXIT(error, td);
2725 syscall_thread_exit(td, se);
2729 ptrace_remotereq(struct thread *td, int flag)
2733 MPASS(td == curthread);
2735 PROC_LOCK_ASSERT(p, MA_OWNED);
2736 if ((td->td_dbgflags & flag) == 0)
2738 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2739 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2743 case TDB_COREDUMPREQ:
2744 ptrace_coredumpreq(td, p, td->td_remotereq);
2746 case TDB_SCREMOTEREQ:
2747 ptrace_syscallreq(td, p, td->td_remotereq);
2754 MPASS((td->td_dbgflags & flag) != 0);
2755 td->td_dbgflags &= ~flag;
2756 td->td_remotereq = NULL;
2761 sig_suspend_threads(struct thread *td, struct proc *p)
2766 PROC_LOCK_ASSERT(p, MA_OWNED);
2767 PROC_SLOCK_ASSERT(p, MA_OWNED);
2770 FOREACH_THREAD_IN_PROC(p, td2) {
2772 ast_sched_locked(td2, TDA_SUSPEND);
2773 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2774 (td2->td_flags & TDF_SINTR)) {
2775 if (td2->td_flags & TDF_SBDRY) {
2777 * Once a thread is asleep with
2778 * TDF_SBDRY and without TDF_SERESTART
2779 * or TDF_SEINTR set, it should never
2780 * become suspended due to this check.
2782 KASSERT(!TD_IS_SUSPENDED(td2),
2783 ("thread with deferred stops suspended"));
2784 if (TD_SBDRY_INTR(td2)) {
2785 wakeup_swapper |= sleepq_abort(td2,
2786 TD_SBDRY_ERRNO(td2));
2789 } else if (!TD_IS_SUSPENDED(td2))
2790 thread_suspend_one(td2);
2791 } else if (!TD_IS_SUSPENDED(td2)) {
2793 if (TD_IS_RUNNING(td2) && td2 != td)
2794 forward_signal(td2);
2799 return (wakeup_swapper);
2803 * Stop the process for an event deemed interesting to the debugger. If si is
2804 * non-NULL, this is a signal exchange; the new signal requested by the
2805 * debugger will be returned for handling. If si is NULL, this is some other
2806 * type of interesting event. The debugger may request a signal be delivered in
2807 * that case as well, however it will be deferred until it can be handled.
2810 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2812 struct proc *p = td->td_proc;
2816 PROC_LOCK_ASSERT(p, MA_OWNED);
2817 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2818 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2819 &p->p_mtx.lock_object, "Stopping for traced signal");
2823 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2824 td->td_dbgflags |= TDB_XSIG;
2825 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2826 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2828 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2831 * Ensure that, if we've been PT_KILLed, the
2832 * exit status reflects that. Another thread
2833 * may also be in ptracestop(), having just
2834 * received the SIGKILL, but this thread was
2835 * unsuspended first.
2837 td->td_dbgflags &= ~TDB_XSIG;
2838 td->td_xsig = SIGKILL;
2842 if (p->p_flag & P_SINGLE_EXIT &&
2843 !(td->td_dbgflags & TDB_EXIT)) {
2845 * Ignore ptrace stops except for thread exit
2846 * events when the process exits.
2848 td->td_dbgflags &= ~TDB_XSIG;
2854 * Make wait(2) work. Ensure that right after the
2855 * attach, the thread which was decided to become the
2856 * leader of attach gets reported to the waiter.
2857 * Otherwise, just avoid overwriting another thread's
2858 * assignment to p_xthread. If another thread has
2859 * already set p_xthread, the current thread will get
2860 * a chance to report itself upon the next iteration.
2862 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2863 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2864 p->p_xthread == NULL)) {
2869 * If we are on sleepqueue already,
2870 * let sleepqueue code decide if it
2871 * needs to go sleep after attach.
2873 if (td->td_wchan == NULL)
2874 td->td_dbgflags &= ~TDB_FSTP;
2876 p->p_flag2 &= ~P2_PTRACE_FSTP;
2877 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2878 sig_suspend_threads(td, p);
2880 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2881 td->td_dbgflags &= ~TDB_STOPATFORK;
2884 td->td_dbgflags |= TDB_SSWITCH;
2885 thread_suspend_switch(td, p);
2886 td->td_dbgflags &= ~TDB_SSWITCH;
2887 if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2888 TDB_SCREMOTEREQ)) != 0) {
2889 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2890 TDB_SCREMOTEREQ)) !=
2891 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2893 ptrace_remotereq(td, td->td_dbgflags &
2894 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2898 if (p->p_xthread == td)
2899 p->p_xthread = NULL;
2900 if (!(p->p_flag & P_TRACED))
2902 if (td->td_dbgflags & TDB_SUSPEND) {
2903 if (p->p_flag & P_SINGLE_EXIT)
2911 if (si != NULL && sig == td->td_xsig) {
2912 /* Parent wants us to take the original signal unchanged. */
2913 si->ksi_flags |= KSI_HEAD;
2914 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2916 } else if (td->td_xsig != 0) {
2918 * If parent wants us to take a new signal, then it will leave
2919 * it in td->td_xsig; otherwise we just look for signals again.
2921 ksiginfo_init(&ksi);
2922 ksi.ksi_signo = td->td_xsig;
2923 ksi.ksi_flags |= KSI_PTRACE;
2924 td2 = sigtd(p, td->td_xsig, false);
2925 tdsendsignal(p, td2, td->td_xsig, &ksi);
2930 return (td->td_xsig);
2934 reschedule_signals(struct proc *p, sigset_t block, int flags)
2939 bool fastblk, pslocked;
2941 PROC_LOCK_ASSERT(p, MA_OWNED);
2943 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2944 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2945 if (SIGISEMPTY(p->p_siglist))
2947 SIGSETAND(block, p->p_siglist);
2948 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2949 SIG_FOREACH(sig, &block) {
2950 td = sigtd(p, sig, fastblk);
2953 * If sigtd() selected us despite sigfastblock is
2954 * blocking, do not activate AST or wake us, to avoid
2955 * loop in AST handler.
2957 if (fastblk && td == curthread)
2962 mtx_lock(&ps->ps_mtx);
2963 if (p->p_flag & P_TRACED ||
2964 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2965 !SIGISMEMBER(td->td_sigmask, sig))) {
2966 tdsigwakeup(td, sig, SIG_CATCH,
2967 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2971 mtx_unlock(&ps->ps_mtx);
2976 tdsigcleanup(struct thread *td)
2982 PROC_LOCK_ASSERT(p, MA_OWNED);
2984 sigqueue_flush(&td->td_sigqueue);
2985 if (p->p_numthreads == 1)
2989 * Since we cannot handle signals, notify signal post code
2990 * about this by filling the sigmask.
2992 * Also, if needed, wake up thread(s) that do not block the
2993 * same signals as the exiting thread, since the thread might
2994 * have been selected for delivery and woken up.
2996 SIGFILLSET(unblocked);
2997 SIGSETNAND(unblocked, td->td_sigmask);
2998 SIGFILLSET(td->td_sigmask);
2999 reschedule_signals(p, unblocked, 0);
3004 sigdeferstop_curr_flags(int cflags)
3007 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
3008 (cflags & TDF_SBDRY) != 0);
3009 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3013 * Defer the delivery of SIGSTOP for the current thread, according to
3014 * the requested mode. Returns previous flags, which must be restored
3015 * by sigallowstop().
3017 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
3018 * cleared by the current thread, which allow the lock-less read-only
3022 sigdeferstop_impl(int mode)
3028 cflags = sigdeferstop_curr_flags(td->td_flags);
3030 case SIGDEFERSTOP_NOP:
3033 case SIGDEFERSTOP_OFF:
3036 case SIGDEFERSTOP_SILENT:
3037 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
3039 case SIGDEFERSTOP_EINTR:
3040 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
3042 case SIGDEFERSTOP_ERESTART:
3043 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
3046 panic("sigdeferstop: invalid mode %x", mode);
3049 if (cflags == nflags)
3050 return (SIGDEFERSTOP_VAL_NCHG);
3052 td->td_flags = (td->td_flags & ~cflags) | nflags;
3058 * Restores the STOP handling mode, typically permitting the delivery
3059 * of SIGSTOP for the current thread. This does not immediately
3060 * suspend if a stop was posted. Instead, the thread will suspend
3061 * either via ast() or a subsequent interruptible sleep.
3064 sigallowstop_impl(int prev)
3069 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3070 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3071 ("sigallowstop: incorrect previous mode %x", prev));
3073 cflags = sigdeferstop_curr_flags(td->td_flags);
3074 if (cflags != prev) {
3076 td->td_flags = (td->td_flags & ~cflags) | prev;
3085 SIGSTATUS_SBDRY_STOP,
3089 * The thread has signal "sig" pending. Figure out what to do with it:
3091 * _HANDLE -> the caller should handle the signal
3092 * _HANDLED -> handled internally, reload pending signal set
3093 * _IGNORE -> ignored, remove from the set of pending signals and try the
3094 * next pending signal
3095 * _SBDRY_STOP -> the signal should stop the thread but this is not
3096 * permitted in the current context
3098 static enum sigstatus
3099 sigprocess(struct thread *td, int sig)
3103 struct sigqueue *queue;
3107 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3111 mtx_assert(&ps->ps_mtx, MA_OWNED);
3112 PROC_LOCK_ASSERT(p, MA_OWNED);
3115 * We should allow pending but ignored signals below
3116 * if there is sigwait() active, or P_TRACED was
3117 * on when they were posted.
3119 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3120 (p->p_flag & P_TRACED) == 0 &&
3121 (td->td_flags & TDF_SIGWAIT) == 0) {
3122 return (SIGSTATUS_IGNORE);
3126 * If the process is going to single-thread mode to prepare
3127 * for exit, there is no sense in delivering any signal
3128 * to usermode. Another important consequence is that
3129 * msleep(..., PCATCH, ...) now is only interruptible by a
3132 if ((p->p_flag2 & P2_WEXIT) != 0)
3133 return (SIGSTATUS_IGNORE);
3135 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3137 * If traced, always stop.
3138 * Remove old signal from queue before the stop.
3139 * XXX shrug off debugger, it causes siginfo to
3142 queue = &td->td_sigqueue;
3143 ksiginfo_init(&ksi);
3144 if (sigqueue_get(queue, sig, &ksi) == 0) {
3145 queue = &p->p_sigqueue;
3146 sigqueue_get(queue, sig, &ksi);
3148 td->td_si = ksi.ksi_info;
3150 mtx_unlock(&ps->ps_mtx);
3151 sig = ptracestop(td, sig, &ksi);
3152 mtx_lock(&ps->ps_mtx);
3154 td->td_si.si_signo = 0;
3157 * Keep looking if the debugger discarded or
3158 * replaced the signal.
3161 return (SIGSTATUS_HANDLED);
3164 * If the signal became masked, re-queue it.
3166 if (SIGISMEMBER(td->td_sigmask, sig)) {
3167 ksi.ksi_flags |= KSI_HEAD;
3168 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3169 return (SIGSTATUS_HANDLED);
3173 * If the traced bit got turned off, requeue the signal and
3174 * reload the set of pending signals. This ensures that p_sig*
3175 * and p_sigact are consistent.
3177 if ((p->p_flag & P_TRACED) == 0) {
3178 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3179 ksi.ksi_flags |= KSI_HEAD;
3180 sigqueue_add(queue, sig, &ksi);
3182 return (SIGSTATUS_HANDLED);
3187 * Decide whether the signal should be returned.
3188 * Return the signal's number, or fall through
3189 * to clear it from the pending mask.
3191 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3192 case (intptr_t)SIG_DFL:
3194 * Don't take default actions on system processes.
3196 if (p->p_pid <= 1) {
3199 * Are you sure you want to ignore SIGSEGV
3202 printf("Process (pid %lu) got signal %d\n",
3203 (u_long)p->p_pid, sig);
3205 return (SIGSTATUS_IGNORE);
3209 * If there is a pending stop signal to process with
3210 * default action, stop here, then clear the signal.
3211 * Traced or exiting processes should ignore stops.
3212 * Additionally, a member of an orphaned process group
3213 * should ignore tty stops.
3215 prop = sigprop(sig);
3216 if (prop & SIGPROP_STOP) {
3217 mtx_unlock(&ps->ps_mtx);
3218 if ((p->p_flag & (P_TRACED | P_WEXIT |
3219 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3220 pg_flags & PGRP_ORPHANED) != 0 &&
3221 (prop & SIGPROP_TTYSTOP) != 0)) {
3222 mtx_lock(&ps->ps_mtx);
3223 return (SIGSTATUS_IGNORE);
3225 if (TD_SBDRY_INTR(td)) {
3226 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3227 ("lost TDF_SBDRY"));
3228 mtx_lock(&ps->ps_mtx);
3229 return (SIGSTATUS_SBDRY_STOP);
3231 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3232 &p->p_mtx.lock_object, "Catching SIGSTOP");
3233 sigqueue_delete(&td->td_sigqueue, sig);
3234 sigqueue_delete(&p->p_sigqueue, sig);
3235 p->p_flag |= P_STOPPED_SIG;
3238 sig_suspend_threads(td, p);
3239 thread_suspend_switch(td, p);
3241 mtx_lock(&ps->ps_mtx);
3242 return (SIGSTATUS_HANDLED);
3243 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3244 (td->td_flags & TDF_SIGWAIT) == 0) {
3246 * Default action is to ignore; drop it if
3247 * not in kern_sigtimedwait().
3249 return (SIGSTATUS_IGNORE);
3251 return (SIGSTATUS_HANDLE);
3254 case (intptr_t)SIG_IGN:
3255 if ((td->td_flags & TDF_SIGWAIT) == 0)
3256 return (SIGSTATUS_IGNORE);
3258 return (SIGSTATUS_HANDLE);
3262 * This signal has an action, let postsig() process it.
3264 return (SIGSTATUS_HANDLE);
3269 * If the current process has received a signal (should be caught or cause
3270 * termination, should interrupt current syscall), return the signal number.
3271 * Stop signals with default action are processed immediately, then cleared;
3272 * they aren't returned. This is checked after each entry to the system for
3273 * a syscall or trap (though this can usually be done without calling
3274 * issignal by checking the pending signal masks in cursig.) The normal call
3277 * while (sig = cursig(curthread))
3281 issignal(struct thread *td)
3284 sigset_t sigpending;
3288 PROC_LOCK_ASSERT(p, MA_OWNED);
3291 sigpending = td->td_sigqueue.sq_signals;
3292 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3293 SIGSETNAND(sigpending, td->td_sigmask);
3295 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3296 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3297 SIG_STOPSIGMASK(sigpending);
3298 if (SIGISEMPTY(sigpending)) /* no signal to send */
3302 * Do fast sigblock if requested by usermode. Since
3303 * we do know that there was a signal pending at this
3304 * point, set the FAST_SIGBLOCK_PEND as indicator for
3305 * usermode to perform a dummy call to
3306 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3307 * delivery of postponed pending signal.
3309 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3310 if (td->td_sigblock_val != 0)
3311 SIGSETNAND(sigpending, fastblock_mask);
3312 if (SIGISEMPTY(sigpending)) {
3313 td->td_pflags |= TDP_SIGFASTPENDING;
3318 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3319 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3320 SIGISMEMBER(sigpending, SIGSTOP)) {
3322 * If debugger just attached, always consume
3323 * SIGSTOP from ptrace(PT_ATTACH) first, to
3324 * execute the debugger attach ritual in
3327 td->td_dbgflags |= TDB_FSTP;
3328 SIGEMPTYSET(sigpending);
3329 SIGADDSET(sigpending, SIGSTOP);
3332 SIG_FOREACH(sig, &sigpending) {
3333 switch (sigprocess(td, sig)) {
3334 case SIGSTATUS_HANDLE:
3336 case SIGSTATUS_HANDLED:
3338 case SIGSTATUS_IGNORE:
3339 sigqueue_delete(&td->td_sigqueue, sig);
3340 sigqueue_delete(&p->p_sigqueue, sig);
3342 case SIGSTATUS_SBDRY_STOP:
3351 thread_stopped(struct proc *p)
3355 PROC_LOCK_ASSERT(p, MA_OWNED);
3356 PROC_SLOCK_ASSERT(p, MA_OWNED);
3360 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3362 p->p_flag &= ~P_WAITED;
3363 PROC_LOCK(p->p_pptr);
3364 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3365 CLD_TRAPPED : CLD_STOPPED);
3366 PROC_UNLOCK(p->p_pptr);
3372 * Take the action for the specified signal
3373 * from the current set of pending signals.
3383 sigset_t returnmask;
3385 KASSERT(sig != 0, ("postsig"));
3389 PROC_LOCK_ASSERT(p, MA_OWNED);
3391 mtx_assert(&ps->ps_mtx, MA_OWNED);
3392 ksiginfo_init(&ksi);
3393 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3394 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3396 ksi.ksi_signo = sig;
3397 if (ksi.ksi_code == SI_TIMER)
3398 itimer_accept(p, ksi.ksi_timerid, &ksi);
3399 action = ps->ps_sigact[_SIG_IDX(sig)];
3401 if (KTRPOINT(td, KTR_PSIG))
3402 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3403 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3406 if (action == SIG_DFL) {
3408 * Default action, where the default is to kill
3409 * the process. (Other cases were ignored above.)
3411 mtx_unlock(&ps->ps_mtx);
3412 proc_td_siginfo_capture(td, &ksi.ksi_info);
3417 * If we get here, the signal must be caught.
3419 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3420 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3421 ("postsig action: blocked sig %d", sig));
3424 * Set the new mask value and also defer further
3425 * occurrences of this signal.
3427 * Special case: user has done a sigsuspend. Here the
3428 * current mask is not of interest, but rather the
3429 * mask from before the sigsuspend is what we want
3430 * restored after the signal processing is completed.
3432 if (td->td_pflags & TDP_OLDMASK) {
3433 returnmask = td->td_oldsigmask;
3434 td->td_pflags &= ~TDP_OLDMASK;
3436 returnmask = td->td_sigmask;
3438 if (p->p_sig == sig) {
3441 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3442 postsig_done(sig, td, ps);
3448 sig_ast_checksusp(struct thread *td)
3450 struct proc *p __diagused;
3454 PROC_LOCK_ASSERT(p, MA_OWNED);
3456 if (!td_ast_pending(td, TDA_SUSPEND))
3459 ret = thread_suspend_check(1);
3460 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3465 sig_ast_needsigchk(struct thread *td)
3472 PROC_LOCK_ASSERT(p, MA_OWNED);
3474 if (!td_ast_pending(td, TDA_SIG))
3478 mtx_lock(&ps->ps_mtx);
3481 mtx_unlock(&ps->ps_mtx);
3482 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3483 KASSERT(TD_SBDRY_INTR(td),
3484 ("lost TDF_SERESTART of TDF_SEINTR"));
3485 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3486 (TDF_SEINTR | TDF_SERESTART),
3487 ("both TDF_SEINTR and TDF_SERESTART"));
3488 ret = TD_SBDRY_ERRNO(td);
3489 } else if (sig != 0) {
3490 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3491 mtx_unlock(&ps->ps_mtx);
3493 mtx_unlock(&ps->ps_mtx);
3498 * Do not go into sleep if this thread was the ptrace(2)
3499 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3500 * but we usually act on the signal by interrupting sleep, and
3501 * should do that here as well.
3503 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3506 td->td_dbgflags &= ~TDB_FSTP;
3520 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3526 ret = sig_ast_checksusp(td);
3528 ret = sig_ast_needsigchk(td);
3534 curproc_sigkilled(void)
3542 if (!td_ast_pending(td, TDA_SIG))
3548 mtx_lock(&ps->ps_mtx);
3549 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3550 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3551 mtx_unlock(&ps->ps_mtx);
3557 proc_wkilled(struct proc *p)
3560 PROC_LOCK_ASSERT(p, MA_OWNED);
3561 if ((p->p_flag & P_WKILLED) == 0) {
3562 p->p_flag |= P_WKILLED;
3564 * Notify swapper that there is a process to swap in.
3565 * The notification is racy, at worst it would take 10
3566 * seconds for the swapper process to notice.
3568 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3574 * Kill the current process for stated reason.
3577 killproc(struct proc *p, const char *why)
3580 PROC_LOCK_ASSERT(p, MA_OWNED);
3581 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3583 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3584 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3585 p->p_ucred->cr_uid, why);
3587 kern_psignal(p, SIGKILL);
3591 * Force the current process to exit with the specified signal, dumping core
3592 * if appropriate. We bypass the normal tests for masked and caught signals,
3593 * allowing unrecoverable failures to terminate the process without changing
3594 * signal state. Mark the accounting record with the signal termination.
3595 * If dumping core, save the signal number for the debugger. Calls exit and
3599 sigexit(struct thread *td, int sig)
3601 struct proc *p = td->td_proc;
3602 const char *coreinfo;
3605 PROC_LOCK_ASSERT(p, MA_OWNED);
3606 proc_set_p2_wexit(p);
3608 p->p_acflag |= AXSIG;
3610 * We must be single-threading to generate a core dump. This
3611 * ensures that the registers in the core file are up-to-date.
3612 * Also, the ELF dump handler assumes that the thread list doesn't
3613 * change out from under it.
3615 * XXX If another thread attempts to single-thread before us
3616 * (e.g. via fork()), we won't get a dump at all.
3618 if ((sigprop(sig) & SIGPROP_CORE) &&
3619 thread_single(p, SINGLE_NO_EXIT) == 0) {
3622 * Log signals which would cause core dumps
3623 * (Log as LOG_INFO to appease those who don't want
3625 * XXX : Todo, as well as euid, write out ruid too
3626 * Note that coredump() drops proc lock.
3632 coreinfo = " (core dumped)";
3635 coreinfo = " (no core dump - bad address)";
3638 coreinfo = " (no core dump - invalid argument)";
3641 coreinfo = " (no core dump - too large)";
3644 coreinfo = " (no core dump - other error)";
3647 if (kern_logsigexit)
3649 "pid %d (%s), jid %d, uid %d: exited on "
3650 "signal %d%s\n", p->p_pid, p->p_comm,
3651 p->p_ucred->cr_prison->pr_id,
3652 td->td_ucred->cr_uid,
3653 sig &~ WCOREFLAG, coreinfo);
3661 * Send queued SIGCHLD to parent when child process's state
3665 sigparent(struct proc *p, int reason, int status)
3667 PROC_LOCK_ASSERT(p, MA_OWNED);
3668 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3670 if (p->p_ksi != NULL) {
3671 p->p_ksi->ksi_signo = SIGCHLD;
3672 p->p_ksi->ksi_code = reason;
3673 p->p_ksi->ksi_status = status;
3674 p->p_ksi->ksi_pid = p->p_pid;
3675 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3676 if (KSI_ONQ(p->p_ksi))
3679 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3683 childproc_jobstate(struct proc *p, int reason, int sig)
3687 PROC_LOCK_ASSERT(p, MA_OWNED);
3688 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3691 * Wake up parent sleeping in kern_wait(), also send
3692 * SIGCHLD to parent, but SIGCHLD does not guarantee
3693 * that parent will awake, because parent may masked
3696 p->p_pptr->p_flag |= P_STATCHILD;
3699 ps = p->p_pptr->p_sigacts;
3700 mtx_lock(&ps->ps_mtx);
3701 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3702 mtx_unlock(&ps->ps_mtx);
3703 sigparent(p, reason, sig);
3705 mtx_unlock(&ps->ps_mtx);
3709 childproc_stopped(struct proc *p, int reason)
3712 childproc_jobstate(p, reason, p->p_xsig);
3716 childproc_continued(struct proc *p)
3718 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3722 childproc_exited(struct proc *p)
3726 if (WCOREDUMP(p->p_xsig)) {
3727 reason = CLD_DUMPED;
3728 status = WTERMSIG(p->p_xsig);
3729 } else if (WIFSIGNALED(p->p_xsig)) {
3730 reason = CLD_KILLED;
3731 status = WTERMSIG(p->p_xsig);
3733 reason = CLD_EXITED;
3734 status = p->p_xexit;
3737 * XXX avoid calling wakeup(p->p_pptr), the work is
3740 sigparent(p, reason, status);
3743 #define MAX_NUM_CORE_FILES 100000
3744 #ifndef NUM_CORE_FILES
3745 #define NUM_CORE_FILES 5
3747 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3748 static int num_cores = NUM_CORE_FILES;
3751 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3756 new_val = num_cores;
3757 error = sysctl_handle_int(oidp, &new_val, 0, req);
3758 if (error != 0 || req->newptr == NULL)
3760 if (new_val > MAX_NUM_CORE_FILES)
3761 new_val = MAX_NUM_CORE_FILES;
3764 num_cores = new_val;
3767 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3768 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3769 sysctl_debug_num_cores_check, "I",
3770 "Maximum number of generated process corefiles while using index format");
3772 #define GZIP_SUFFIX ".gz"
3773 #define ZSTD_SUFFIX ".zst"
3775 int compress_user_cores = 0;
3778 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3782 val = compress_user_cores;
3783 error = sysctl_handle_int(oidp, &val, 0, req);
3784 if (error != 0 || req->newptr == NULL)
3786 if (val != 0 && !compressor_avail(val))
3788 compress_user_cores = val;
3791 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3792 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3793 sysctl_compress_user_cores, "I",
3794 "Enable compression of user corefiles ("
3795 __XSTRING(COMPRESS_GZIP) " = gzip, "
3796 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3798 int compress_user_cores_level = 6;
3799 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3800 &compress_user_cores_level, 0,
3801 "Corefile compression level");
3804 * Protect the access to corefilename[] by allproc_lock.
3806 #define corefilename_lock allproc_lock
3808 static char corefilename[MAXPATHLEN] = {"%N.core"};
3809 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3812 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3816 sx_xlock(&corefilename_lock);
3817 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3819 sx_xunlock(&corefilename_lock);
3823 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3824 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3825 "Process corefile name format string");
3828 vnode_close_locked(struct thread *td, struct vnode *vp)
3832 vn_close(vp, FWRITE, td->td_ucred, td);
3836 * If the core format has a %I in it, then we need to check
3837 * for existing corefiles before defining a name.
3838 * To do this we iterate over 0..ncores to find a
3839 * non-existing core file name to use. If all core files are
3840 * already used we choose the oldest one.
3843 corefile_open_last(struct thread *td, char *name, int indexpos,
3844 int indexlen, int ncores, struct vnode **vpp)
3846 struct vnode *oldvp, *nextvp, *vp;
3848 struct nameidata nd;
3849 int error, i, flags, oflags, cmode;
3851 struct timespec lasttime;
3853 nextvp = oldvp = NULL;
3854 cmode = S_IRUSR | S_IWUSR;
3855 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3856 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3858 for (i = 0; i < ncores; i++) {
3859 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3861 ch = name[indexpos + indexlen];
3862 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3864 name[indexpos + indexlen] = ch;
3866 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3867 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3874 if ((flags & O_CREAT) == O_CREAT) {
3879 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3881 vnode_close_locked(td, vp);
3885 if (oldvp == NULL ||
3886 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3887 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3888 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3890 vn_close(oldvp, FWRITE, td->td_ucred, td);
3893 lasttime = vattr.va_mtime;
3895 vnode_close_locked(td, vp);
3899 if (oldvp != NULL) {
3900 if (nextvp == NULL) {
3901 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3903 vn_close(oldvp, FWRITE, td->td_ucred, td);
3906 error = vn_lock(nextvp, LK_EXCLUSIVE);
3908 vn_close(nextvp, FWRITE, td->td_ucred,
3914 vn_close(oldvp, FWRITE, td->td_ucred, td);
3919 vnode_close_locked(td, oldvp);
3928 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3929 * Expand the name described in corefilename, using name, uid, and pid
3930 * and open/create core file.
3931 * corefilename is a printf-like string, with three format specifiers:
3932 * %N name of process ("name")
3933 * %P process id (pid)
3935 * For example, "%N.core" is the default; they can be disabled completely
3936 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3937 * This is controlled by the sysctl variable kern.corefile (see above).
3940 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3941 int compress, int signum, struct vnode **vpp, char **namep)
3944 struct nameidata nd;
3946 char *hostname, *name;
3947 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3950 format = corefilename;
3951 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3955 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3956 sx_slock(&corefilename_lock);
3957 for (i = 0; format[i] != '\0'; i++) {
3958 switch (format[i]) {
3959 case '%': /* Format character */
3961 switch (format[i]) {
3963 sbuf_putc(&sb, '%');
3965 case 'H': /* hostname */
3966 if (hostname == NULL) {
3967 hostname = malloc(MAXHOSTNAMELEN,
3970 getcredhostname(td->td_ucred, hostname,
3972 sbuf_cat(&sb, hostname);
3974 case 'I': /* autoincrementing index */
3975 if (indexpos != -1) {
3976 sbuf_printf(&sb, "%%I");
3980 indexpos = sbuf_len(&sb);
3981 sbuf_printf(&sb, "%u", ncores - 1);
3982 indexlen = sbuf_len(&sb) - indexpos;
3984 case 'N': /* process name */
3985 sbuf_printf(&sb, "%s", comm);
3987 case 'P': /* process id */
3988 sbuf_printf(&sb, "%u", pid);
3990 case 'S': /* signal number */
3991 sbuf_printf(&sb, "%i", signum);
3993 case 'U': /* user id */
3994 sbuf_printf(&sb, "%u", uid);
3998 "Unknown format character %c in "
3999 "corename `%s'\n", format[i], format);
4004 sbuf_putc(&sb, format[i]);
4008 sx_sunlock(&corefilename_lock);
4009 free(hostname, M_TEMP);
4010 if (compress == COMPRESS_GZIP)
4011 sbuf_cat(&sb, GZIP_SUFFIX);
4012 else if (compress == COMPRESS_ZSTD)
4013 sbuf_cat(&sb, ZSTD_SUFFIX);
4014 if (sbuf_error(&sb) != 0) {
4015 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
4016 "long\n", (long)pid, comm, (u_long)uid);
4024 if (indexpos != -1) {
4025 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
4029 "pid %d (%s), uid (%u): Path `%s' failed "
4030 "on initial open test, error = %d\n",
4031 pid, comm, uid, name, error);
4034 cmode = S_IRUSR | S_IWUSR;
4035 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
4036 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
4037 flags = O_CREAT | FWRITE | O_NOFOLLOW;
4038 if ((td->td_proc->p_flag & P_SUGID) != 0)
4041 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
4042 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
4052 audit_proc_coredump(td, name, error);
4062 * Dump a process' core. The main routine does some
4063 * policy checking, and creates the name of the coredump;
4064 * then it passes on a vnode and a size limit to the process-specific
4065 * coredump routine if there is one; if there _is not_ one, it returns
4066 * ENOSYS; otherwise it returns the error from the process-specific routine.
4070 coredump(struct thread *td)
4072 struct proc *p = td->td_proc;
4073 struct ucred *cred = td->td_ucred;
4077 size_t fullpathsize;
4078 int error, error1, locked;
4079 char *name; /* name of corefile */
4082 char *fullpath, *freepath = NULL;
4085 PROC_LOCK_ASSERT(p, MA_OWNED);
4086 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
4088 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
4089 (p->p_flag2 & P2_NOTRACE) != 0) {
4095 * Note that the bulk of limit checking is done after
4096 * the corefile is created. The exception is if the limit
4097 * for corefiles is 0, in which case we don't bother
4098 * creating the corefile at all. This layout means that
4099 * a corefile is truncated instead of not being created,
4100 * if it is larger than the limit.
4102 limit = (off_t)lim_cur(td, RLIMIT_CORE);
4103 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
4109 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
4110 compress_user_cores, p->p_sig, &vp, &name);
4115 * Don't dump to non-regular files or files with links.
4116 * Do not dump into system files. Effective user must own the corefile.
4118 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
4119 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
4120 vattr.va_uid != cred->cr_uid) {
4128 /* Postpone other writers, including core dumps of other processes. */
4129 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4131 lf.l_whence = SEEK_SET;
4134 lf.l_type = F_WRLCK;
4135 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
4139 if (set_core_nodump_flag)
4140 vattr.va_flags = UF_NODUMP;
4141 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4142 VOP_SETATTR(vp, &vattr, cred);
4145 p->p_acflag |= ACORE;
4148 if (p->p_sysent->sv_coredump != NULL) {
4149 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
4155 lf.l_type = F_UNLCK;
4156 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4158 vn_rangelock_unlock(vp, rl_cookie);
4161 * Notify the userland helper that a process triggered a core dump.
4162 * This allows the helper to run an automated debugging session.
4164 if (error != 0 || coredump_devctl == 0)
4166 sb = sbuf_new_auto();
4167 if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4169 sbuf_cat(sb, "comm=\"");
4170 devctl_safe_quote_sb(sb, fullpath);
4171 free(freepath, M_TEMP);
4172 sbuf_cat(sb, "\" core=\"");
4175 * We can't lookup core file vp directly. When we're replacing a core, and
4176 * other random times, we flush the name cache, so it will fail. Instead,
4177 * if the path of the core is relative, add the current dir in front if it.
4179 if (name[0] != '/') {
4180 fullpathsize = MAXPATHLEN;
4181 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4182 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
4183 free(freepath, M_TEMP);
4186 devctl_safe_quote_sb(sb, fullpath);
4187 free(freepath, M_TEMP);
4190 devctl_safe_quote_sb(sb, name);
4192 if (sbuf_finish(sb) == 0)
4193 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4197 error1 = vn_close(vp, FWRITE, cred, td);
4201 audit_proc_coredump(td, name, error);
4208 * Nonexistent system call-- signal process (may want to handle it). Flag
4209 * error in case process won't see signal immediately (blocked or ignored).
4211 #ifndef _SYS_SYSPROTO_H_
4218 nosys(struct thread *td, struct nosys_args *args)
4224 if (SV_PROC_FLAG(p, SV_SIGSYS) != 0 && kern_signosys) {
4226 tdsignal(td, SIGSYS);
4229 if (kern_lognosys == 1 || kern_lognosys == 3) {
4230 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4233 if (kern_lognosys == 2 || kern_lognosys == 3 ||
4234 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4235 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4242 * Send a SIGIO or SIGURG signal to a process or process group using stored
4243 * credentials rather than those of the current process.
4246 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4249 struct sigio *sigio;
4251 ksiginfo_init(&ksi);
4252 ksi.ksi_signo = sig;
4253 ksi.ksi_code = SI_KERNEL;
4257 if (sigio == NULL) {
4261 if (sigio->sio_pgid > 0) {
4262 PROC_LOCK(sigio->sio_proc);
4263 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4264 kern_psignal(sigio->sio_proc, sig);
4265 PROC_UNLOCK(sigio->sio_proc);
4266 } else if (sigio->sio_pgid < 0) {
4269 PGRP_LOCK(sigio->sio_pgrp);
4270 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4272 if (p->p_state == PRS_NORMAL &&
4273 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4274 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4275 kern_psignal(p, sig);
4278 PGRP_UNLOCK(sigio->sio_pgrp);
4284 filt_sigattach(struct knote *kn)
4286 struct proc *p = curproc;
4288 kn->kn_ptr.p_proc = p;
4289 kn->kn_flags |= EV_CLEAR; /* automatically set */
4291 knlist_add(p->p_klist, kn, 0);
4297 filt_sigdetach(struct knote *kn)
4299 knlist_remove(kn->kn_knlist, kn, 0);
4303 * signal knotes are shared with proc knotes, so we apply a mask to
4304 * the hint in order to differentiate them from process hints. This
4305 * could be avoided by using a signal-specific knote list, but probably
4306 * isn't worth the trouble.
4309 filt_signal(struct knote *kn, long hint)
4312 if (hint & NOTE_SIGNAL) {
4313 hint &= ~NOTE_SIGNAL;
4315 if (kn->kn_id == hint)
4318 return (kn->kn_data != 0);
4326 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4327 refcount_init(&ps->ps_refcnt, 1);
4328 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4333 sigacts_free(struct sigacts *ps)
4336 if (refcount_release(&ps->ps_refcnt) == 0)
4338 mtx_destroy(&ps->ps_mtx);
4339 free(ps, M_SUBPROC);
4343 sigacts_hold(struct sigacts *ps)
4346 refcount_acquire(&ps->ps_refcnt);
4351 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4354 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4355 mtx_lock(&src->ps_mtx);
4356 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4357 mtx_unlock(&src->ps_mtx);
4361 sigacts_shared(struct sigacts *ps)
4364 return (ps->ps_refcnt > 1);
4368 sig_drop_caught(struct proc *p)
4374 PROC_LOCK_ASSERT(p, MA_OWNED);
4375 mtx_assert(&ps->ps_mtx, MA_OWNED);
4376 SIG_FOREACH(sig, &ps->ps_sigcatch) {
4378 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4379 sigqueue_delete_proc(p, sig);
4384 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4389 * Prevent further fetches and SIGSEGVs, allowing thread to
4390 * issue syscalls despite corruption.
4392 sigfastblock_clear(td);
4396 ksiginfo_init_trap(&ksi);
4397 ksi.ksi_signo = SIGSEGV;
4398 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4399 ksi.ksi_addr = td->td_sigblock_ptr;
4400 trapsignal(td, &ksi);
4404 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4408 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4410 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4411 sigfastblock_failed(td, sendsig, false);
4415 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4420 sigfastblock_resched(struct thread *td, bool resched)
4427 reschedule_signals(p, td->td_sigmask, 0);
4430 ast_sched(td, TDA_SIG);
4434 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4443 case SIGFASTBLOCK_SETPTR:
4444 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4448 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4452 td->td_pflags |= TDP_SIGFASTBLOCK;
4453 td->td_sigblock_ptr = uap->ptr;
4456 case SIGFASTBLOCK_UNBLOCK:
4457 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4463 res = casueword32(td->td_sigblock_ptr,
4464 SIGFASTBLOCK_PEND, &oldval, 0);
4467 sigfastblock_failed(td, false, true);
4473 if (oldval != SIGFASTBLOCK_PEND) {
4477 error = thread_check_susp(td, false);
4485 * td_sigblock_val is cleared there, but not on a
4486 * syscall exit. The end effect is that a single
4487 * interruptible sleep, while user sigblock word is
4488 * set, might return EINTR or ERESTART to usermode
4489 * without delivering signal. All further sleeps,
4490 * until userspace clears the word and does
4491 * sigfastblock(UNBLOCK), observe current word and no
4492 * longer get interrupted. It is slight
4493 * non-conformance, with alternative to have read the
4494 * sigblock word on each syscall entry.
4496 td->td_sigblock_val = 0;
4499 * Rely on normal ast mechanism to deliver pending
4500 * signals to current thread. But notify others about
4503 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4507 case SIGFASTBLOCK_UNSETPTR:
4508 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4512 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4516 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4520 sigfastblock_clear(td);
4531 sigfastblock_clear(struct thread *td)
4535 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4537 td->td_sigblock_val = 0;
4538 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4540 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4541 sigfastblock_resched(td, resched);
4545 sigfastblock_fetch(struct thread *td)
4549 (void)sigfastblock_fetch_sig(td, true, &val);
4553 sigfastblock_setpend1(struct thread *td)
4558 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4560 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4562 sigfastblock_failed(td, true, false);
4566 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4567 oldval | SIGFASTBLOCK_PEND);
4569 sigfastblock_failed(td, true, true);
4573 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4574 td->td_pflags &= ~TDP_SIGFASTPENDING;
4578 if (thread_check_susp(td, false) != 0)
4584 sigfastblock_setpend(struct thread *td, bool resched)
4588 sigfastblock_setpend1(td);
4592 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);