2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 #include <sys/cdefs.h>
40 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/capsicum.h>
45 #include <sys/ctype.h>
46 #include <sys/systm.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
50 #include <sys/capsicum.h>
51 #include <sys/compressor.h>
52 #include <sys/condvar.h>
53 #include <sys/devctl.h>
54 #include <sys/event.h>
55 #include <sys/fcntl.h>
56 #include <sys/imgact.h>
58 #include <sys/kernel.h>
60 #include <sys/ktrace.h>
61 #include <sys/limits.h>
63 #include <sys/malloc.h>
64 #include <sys/mutex.h>
65 #include <sys/refcount.h>
66 #include <sys/namei.h>
68 #include <sys/procdesc.h>
69 #include <sys/ptrace.h>
70 #include <sys/posix4.h>
71 #include <sys/racct.h>
72 #include <sys/resourcevar.h>
75 #include <sys/sleepqueue.h>
79 #include <sys/syscall.h>
80 #include <sys/syscallsubr.h>
81 #include <sys/sysctl.h>
82 #include <sys/sysent.h>
83 #include <sys/syslog.h>
84 #include <sys/sysproto.h>
85 #include <sys/timers.h>
86 #include <sys/unistd.h>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
93 #include <machine/cpu.h>
95 #include <security/audit/audit.h>
97 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
99 SDT_PROVIDER_DECLARE(proc);
100 SDT_PROBE_DEFINE3(proc, , , signal__send,
101 "struct thread *", "struct proc *", "int");
102 SDT_PROBE_DEFINE2(proc, , , signal__clear,
103 "int", "ksiginfo_t *");
104 SDT_PROBE_DEFINE3(proc, , , signal__discard,
105 "struct thread *", "struct proc *", "int");
107 static int coredump(struct thread *);
108 static int killpg1(struct thread *td, int sig, int pgid, int all,
110 static int issignal(struct thread *td);
111 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
112 static int sigprop(int sig);
113 static void tdsigwakeup(struct thread *, int, sig_t, int);
114 static int sig_suspend_threads(struct thread *, struct proc *);
115 static int filt_sigattach(struct knote *kn);
116 static void filt_sigdetach(struct knote *kn);
117 static int filt_signal(struct knote *kn, long hint);
118 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
119 static void sigqueue_start(void);
120 static void sigfastblock_setpend(struct thread *td, bool resched);
122 static uma_zone_t ksiginfo_zone = NULL;
123 struct filterops sig_filtops = {
125 .f_attach = filt_sigattach,
126 .f_detach = filt_sigdetach,
127 .f_event = filt_signal,
130 static int kern_logsigexit = 1;
131 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
133 "Log processes quitting on abnormal signals to syslog(3)");
135 static int kern_forcesigexit = 1;
136 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
137 &kern_forcesigexit, 0, "Force trap signal to be handled");
139 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
140 "POSIX real time signal");
142 static int max_pending_per_proc = 128;
143 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
144 &max_pending_per_proc, 0, "Max pending signals per proc");
146 static int preallocate_siginfo = 1024;
147 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
148 &preallocate_siginfo, 0, "Preallocated signal memory size");
150 static int signal_overflow = 0;
151 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
152 &signal_overflow, 0, "Number of signals overflew");
154 static int signal_alloc_fail = 0;
155 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
156 &signal_alloc_fail, 0, "signals failed to be allocated");
158 static int kern_lognosys = 0;
159 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
160 "Log invalid syscalls");
162 __read_frequently bool sigfastblock_fetch_always = false;
163 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
164 &sigfastblock_fetch_always, 0,
165 "Fetch sigfastblock word on each syscall entry for proper "
166 "blocking semantic");
168 static bool kern_sig_discard_ign = true;
169 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
170 &kern_sig_discard_ign, 0,
171 "Discard ignored signals on delivery, otherwise queue them to "
174 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
177 * Policy -- Can ucred cr1 send SIGIO to process cr2?
178 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
179 * in the right situations.
181 #define CANSIGIO(cr1, cr2) \
182 ((cr1)->cr_uid == 0 || \
183 (cr1)->cr_ruid == (cr2)->cr_ruid || \
184 (cr1)->cr_uid == (cr2)->cr_ruid || \
185 (cr1)->cr_ruid == (cr2)->cr_uid || \
186 (cr1)->cr_uid == (cr2)->cr_uid)
188 static int sugid_coredump;
189 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
190 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
192 static int capmode_coredump;
193 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
194 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
196 static int do_coredump = 1;
197 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
198 &do_coredump, 0, "Enable/Disable coredumps");
200 static int set_core_nodump_flag = 0;
201 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
202 0, "Enable setting the NODUMP flag on coredump files");
204 static int coredump_devctl = 0;
205 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
206 0, "Generate a devctl notification when processes coredump");
209 * Signal properties and actions.
210 * The array below categorizes the signals and their default actions
211 * according to the following properties:
213 #define SIGPROP_KILL 0x01 /* terminates process by default */
214 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
215 #define SIGPROP_STOP 0x04 /* suspend process */
216 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
217 #define SIGPROP_IGNORE 0x10 /* ignore by default */
218 #define SIGPROP_CONT 0x20 /* continue if suspended */
220 static const int sigproptbl[NSIG] = {
221 [SIGHUP] = SIGPROP_KILL,
222 [SIGINT] = SIGPROP_KILL,
223 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
224 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
225 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
226 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
227 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
228 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
229 [SIGKILL] = SIGPROP_KILL,
230 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
231 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
232 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
233 [SIGPIPE] = SIGPROP_KILL,
234 [SIGALRM] = SIGPROP_KILL,
235 [SIGTERM] = SIGPROP_KILL,
236 [SIGURG] = SIGPROP_IGNORE,
237 [SIGSTOP] = SIGPROP_STOP,
238 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
239 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
240 [SIGCHLD] = SIGPROP_IGNORE,
241 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
242 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
243 [SIGIO] = SIGPROP_IGNORE,
244 [SIGXCPU] = SIGPROP_KILL,
245 [SIGXFSZ] = SIGPROP_KILL,
246 [SIGVTALRM] = SIGPROP_KILL,
247 [SIGPROF] = SIGPROP_KILL,
248 [SIGWINCH] = SIGPROP_IGNORE,
249 [SIGINFO] = SIGPROP_IGNORE,
250 [SIGUSR1] = SIGPROP_KILL,
251 [SIGUSR2] = SIGPROP_KILL,
254 #define _SIG_FOREACH_ADVANCE(i, set) ({ \
258 int __sig = ffs(__bits); \
259 __bits &= ~(1u << (__sig - 1)); \
260 sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
264 if (++__i == _SIG_WORDS) { \
268 __bits = (set)->__bits[__i]; \
273 #define SIG_FOREACH(i, set) \
274 for (int32_t __i = -1, __bits = 0; \
275 _SIG_FOREACH_ADVANCE(i, set); ) \
277 static sigset_t fastblock_mask;
280 ast_sig(struct thread *td, int tda)
283 int old_boundary, sig;
289 if (p->p_numthreads == 1 && (tda & (TDAI(TDA_SIG) |
290 TDAI(TDA_AST))) == 0) {
294 * Note that TDA_SIG should be re-read from
295 * td_ast, since signal might have been delivered
296 * after we cleared td_flags above. This is one of
297 * the reason for looping check for AST condition.
298 * See comment in userret() about P_PPWAIT.
300 if ((p->p_flag & P_PPWAIT) == 0 &&
301 (td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
302 if (SIGPENDING(td) && ((tda | td->td_ast) &
303 (TDAI(TDA_SIG) | TDAI(TDA_AST))) == 0) {
304 thread_unlock(td); /* fix dumps */
306 "failed2 to set signal flags for ast p %p "
307 "td %p tda %#x td_ast %#x fl %#x",
308 p, td, tda, td->td_ast, td->td_flags);
317 * Check for signals. Unlocked reads of p_pendingcnt or
318 * p_siglist might cause process-directed signal to be handled
321 if ((tda & TDAI(TDA_SIG)) != 0 || p->p_pendingcnt > 0 ||
322 !SIGISEMPTY(p->p_siglist)) {
323 sigfastblock_fetch(td);
325 old_boundary = ~TDB_BOUNDARY | (td->td_dbgflags & TDB_BOUNDARY);
326 td->td_dbgflags |= TDB_BOUNDARY;
327 mtx_lock(&p->p_sigacts->ps_mtx);
328 while ((sig = cursig(td)) != 0) {
329 KASSERT(sig >= 0, ("sig %d", sig));
332 mtx_unlock(&p->p_sigacts->ps_mtx);
333 td->td_dbgflags &= old_boundary;
337 resched_sigs = false;
341 * Handle deferred update of the fast sigblock value, after
342 * the postsig() loop was performed.
344 sigfastblock_setpend(td, resched_sigs);
348 ast_sigsuspend(struct thread *td, int tda __unused)
350 MPASS((td->td_pflags & TDP_OLDMASK) != 0);
351 td->td_pflags &= ~TDP_OLDMASK;
352 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
358 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
359 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
360 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
361 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
362 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
363 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
364 SIGFILLSET(fastblock_mask);
365 SIG_CANTMASK(fastblock_mask);
366 ast_register(TDA_SIG, ASTR_UNCOND, 0, ast_sig);
367 ast_register(TDA_SIGSUSPEND, ASTR_ASTF_REQUIRED | ASTR_TDP,
368 TDP_OLDMASK, ast_sigsuspend);
372 ksiginfo_alloc(int mwait)
374 MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
376 if (ksiginfo_zone == NULL)
378 return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
382 ksiginfo_free(ksiginfo_t *ksi)
384 uma_zfree(ksiginfo_zone, ksi);
388 ksiginfo_tryfree(ksiginfo_t *ksi)
390 if ((ksi->ksi_flags & KSI_EXT) == 0) {
391 uma_zfree(ksiginfo_zone, ksi);
398 sigqueue_init(sigqueue_t *list, struct proc *p)
400 SIGEMPTYSET(list->sq_signals);
401 SIGEMPTYSET(list->sq_kill);
402 SIGEMPTYSET(list->sq_ptrace);
403 TAILQ_INIT(&list->sq_list);
405 list->sq_flags = SQ_INIT;
409 * Get a signal's ksiginfo.
411 * 0 - signal not found
412 * others - signal number
415 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
417 struct proc *p = sq->sq_proc;
418 struct ksiginfo *ksi, *next;
421 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
423 if (!SIGISMEMBER(sq->sq_signals, signo))
426 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
428 SIGDELSET(sq->sq_ptrace, signo);
429 si->ksi_flags |= KSI_PTRACE;
431 if (SIGISMEMBER(sq->sq_kill, signo)) {
434 SIGDELSET(sq->sq_kill, signo);
437 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
438 if (ksi->ksi_signo == signo) {
440 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
441 ksi->ksi_sigq = NULL;
442 ksiginfo_copy(ksi, si);
443 if (ksiginfo_tryfree(ksi) && p != NULL)
452 SIGDELSET(sq->sq_signals, signo);
453 si->ksi_signo = signo;
458 sigqueue_take(ksiginfo_t *ksi)
464 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
468 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
469 ksi->ksi_sigq = NULL;
470 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
473 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
474 kp = TAILQ_NEXT(kp, ksi_link)) {
475 if (kp->ksi_signo == ksi->ksi_signo)
478 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
479 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
480 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
484 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
486 struct proc *p = sq->sq_proc;
487 struct ksiginfo *ksi;
490 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
493 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
496 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
497 SIGADDSET(sq->sq_kill, signo);
501 /* directly insert the ksi, don't copy it */
502 if (si->ksi_flags & KSI_INS) {
503 if (si->ksi_flags & KSI_HEAD)
504 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
506 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
511 if (__predict_false(ksiginfo_zone == NULL)) {
512 SIGADDSET(sq->sq_kill, signo);
516 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
519 } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
525 ksiginfo_copy(si, ksi);
526 ksi->ksi_signo = signo;
527 if (si->ksi_flags & KSI_HEAD)
528 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
530 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
535 if ((si->ksi_flags & KSI_PTRACE) != 0) {
536 SIGADDSET(sq->sq_ptrace, signo);
539 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
540 (si->ksi_flags & KSI_SIGQ) == 0) {
541 SIGADDSET(sq->sq_kill, signo);
549 SIGADDSET(sq->sq_signals, signo);
554 sigqueue_flush(sigqueue_t *sq)
556 struct proc *p = sq->sq_proc;
559 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
562 PROC_LOCK_ASSERT(p, MA_OWNED);
564 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
565 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
566 ksi->ksi_sigq = NULL;
567 if (ksiginfo_tryfree(ksi) && p != NULL)
571 SIGEMPTYSET(sq->sq_signals);
572 SIGEMPTYSET(sq->sq_kill);
573 SIGEMPTYSET(sq->sq_ptrace);
577 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
580 struct proc *p1, *p2;
581 ksiginfo_t *ksi, *next;
583 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
584 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
587 /* Move siginfo to target list */
588 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
589 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
590 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
593 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
600 /* Move pending bits to target list */
602 SIGSETAND(tmp, *set);
603 SIGSETOR(dst->sq_kill, tmp);
604 SIGSETNAND(src->sq_kill, tmp);
606 tmp = src->sq_ptrace;
607 SIGSETAND(tmp, *set);
608 SIGSETOR(dst->sq_ptrace, tmp);
609 SIGSETNAND(src->sq_ptrace, tmp);
611 tmp = src->sq_signals;
612 SIGSETAND(tmp, *set);
613 SIGSETOR(dst->sq_signals, tmp);
614 SIGSETNAND(src->sq_signals, tmp);
619 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
624 SIGADDSET(set, signo);
625 sigqueue_move_set(src, dst, &set);
630 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
632 struct proc *p = sq->sq_proc;
633 ksiginfo_t *ksi, *next;
635 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
637 /* Remove siginfo queue */
638 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
639 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
640 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
641 ksi->ksi_sigq = NULL;
642 if (ksiginfo_tryfree(ksi) && p != NULL)
646 SIGSETNAND(sq->sq_kill, *set);
647 SIGSETNAND(sq->sq_ptrace, *set);
648 SIGSETNAND(sq->sq_signals, *set);
652 sigqueue_delete(sigqueue_t *sq, int signo)
657 SIGADDSET(set, signo);
658 sigqueue_delete_set(sq, &set);
661 /* Remove a set of signals for a process */
663 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
668 PROC_LOCK_ASSERT(p, MA_OWNED);
670 sigqueue_init(&worklist, NULL);
671 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
673 FOREACH_THREAD_IN_PROC(p, td0)
674 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
676 sigqueue_flush(&worklist);
680 sigqueue_delete_proc(struct proc *p, int signo)
685 SIGADDSET(set, signo);
686 sigqueue_delete_set_proc(p, &set);
690 sigqueue_delete_stopmask_proc(struct proc *p)
695 SIGADDSET(set, SIGSTOP);
696 SIGADDSET(set, SIGTSTP);
697 SIGADDSET(set, SIGTTIN);
698 SIGADDSET(set, SIGTTOU);
699 sigqueue_delete_set_proc(p, &set);
703 * Determine signal that should be delivered to thread td, the current
704 * thread, 0 if none. If there is a pending stop signal with default
705 * action, the process stops in issignal().
708 cursig(struct thread *td)
710 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
711 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
712 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
713 return (SIGPENDING(td) ? issignal(td) : 0);
717 * Arrange for ast() to handle unmasked pending signals on return to user
718 * mode. This must be called whenever a signal is added to td_sigqueue or
719 * unmasked in td_sigmask.
722 signotify(struct thread *td)
725 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
728 ast_sched(td, TDA_SIG);
732 * Returns 1 (true) if altstack is configured for the thread, and the
733 * passed stack bottom address falls into the altstack range. Handles
734 * the 43 compat special case where the alt stack size is zero.
737 sigonstack(size_t sp)
742 if ((td->td_pflags & TDP_ALTSTACK) == 0)
744 #if defined(COMPAT_43)
745 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
746 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
748 return (sp >= (size_t)td->td_sigstk.ss_sp &&
749 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
756 if (sig > 0 && sig < nitems(sigproptbl))
757 return (sigproptbl[sig]);
762 sigact_flag_test(const struct sigaction *act, int flag)
766 * SA_SIGINFO is reset when signal disposition is set to
767 * ignore or default. Other flags are kept according to user
770 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
771 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
772 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
782 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
783 struct sigaction *oact, int flags)
786 struct proc *p = td->td_proc;
788 if (!_SIG_VALID(sig))
790 if (act != NULL && act->sa_handler != SIG_DFL &&
791 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
792 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
793 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
798 mtx_lock(&ps->ps_mtx);
800 memset(oact, 0, sizeof(*oact));
801 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
802 if (SIGISMEMBER(ps->ps_sigonstack, sig))
803 oact->sa_flags |= SA_ONSTACK;
804 if (!SIGISMEMBER(ps->ps_sigintr, sig))
805 oact->sa_flags |= SA_RESTART;
806 if (SIGISMEMBER(ps->ps_sigreset, sig))
807 oact->sa_flags |= SA_RESETHAND;
808 if (SIGISMEMBER(ps->ps_signodefer, sig))
809 oact->sa_flags |= SA_NODEFER;
810 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
811 oact->sa_flags |= SA_SIGINFO;
813 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
815 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
816 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
817 oact->sa_flags |= SA_NOCLDSTOP;
818 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
819 oact->sa_flags |= SA_NOCLDWAIT;
822 if ((sig == SIGKILL || sig == SIGSTOP) &&
823 act->sa_handler != SIG_DFL) {
824 mtx_unlock(&ps->ps_mtx);
830 * Change setting atomically.
833 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
834 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
835 if (sigact_flag_test(act, SA_SIGINFO)) {
836 ps->ps_sigact[_SIG_IDX(sig)] =
837 (__sighandler_t *)act->sa_sigaction;
838 SIGADDSET(ps->ps_siginfo, sig);
840 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
841 SIGDELSET(ps->ps_siginfo, sig);
843 if (!sigact_flag_test(act, SA_RESTART))
844 SIGADDSET(ps->ps_sigintr, sig);
846 SIGDELSET(ps->ps_sigintr, sig);
847 if (sigact_flag_test(act, SA_ONSTACK))
848 SIGADDSET(ps->ps_sigonstack, sig);
850 SIGDELSET(ps->ps_sigonstack, sig);
851 if (sigact_flag_test(act, SA_RESETHAND))
852 SIGADDSET(ps->ps_sigreset, sig);
854 SIGDELSET(ps->ps_sigreset, sig);
855 if (sigact_flag_test(act, SA_NODEFER))
856 SIGADDSET(ps->ps_signodefer, sig);
858 SIGDELSET(ps->ps_signodefer, sig);
859 if (sig == SIGCHLD) {
860 if (act->sa_flags & SA_NOCLDSTOP)
861 ps->ps_flag |= PS_NOCLDSTOP;
863 ps->ps_flag &= ~PS_NOCLDSTOP;
864 if (act->sa_flags & SA_NOCLDWAIT) {
866 * Paranoia: since SA_NOCLDWAIT is implemented
867 * by reparenting the dying child to PID 1 (and
868 * trust it to reap the zombie), PID 1 itself
869 * is forbidden to set SA_NOCLDWAIT.
872 ps->ps_flag &= ~PS_NOCLDWAIT;
874 ps->ps_flag |= PS_NOCLDWAIT;
876 ps->ps_flag &= ~PS_NOCLDWAIT;
877 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
878 ps->ps_flag |= PS_CLDSIGIGN;
880 ps->ps_flag &= ~PS_CLDSIGIGN;
883 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
884 * and for signals set to SIG_DFL where the default is to
885 * ignore. However, don't put SIGCONT in ps_sigignore, as we
886 * have to restart the process.
888 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
889 (sigprop(sig) & SIGPROP_IGNORE &&
890 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
891 /* never to be seen again */
892 sigqueue_delete_proc(p, sig);
894 /* easier in psignal */
895 SIGADDSET(ps->ps_sigignore, sig);
896 SIGDELSET(ps->ps_sigcatch, sig);
898 SIGDELSET(ps->ps_sigignore, sig);
899 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
900 SIGDELSET(ps->ps_sigcatch, sig);
902 SIGADDSET(ps->ps_sigcatch, sig);
904 #ifdef COMPAT_FREEBSD4
905 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
906 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
907 (flags & KSA_FREEBSD4) == 0)
908 SIGDELSET(ps->ps_freebsd4, sig);
910 SIGADDSET(ps->ps_freebsd4, sig);
913 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
914 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
915 (flags & KSA_OSIGSET) == 0)
916 SIGDELSET(ps->ps_osigset, sig);
918 SIGADDSET(ps->ps_osigset, sig);
921 mtx_unlock(&ps->ps_mtx);
926 #ifndef _SYS_SYSPROTO_H_
927 struct sigaction_args {
929 struct sigaction *act;
930 struct sigaction *oact;
934 sys_sigaction(struct thread *td, struct sigaction_args *uap)
936 struct sigaction act, oact;
937 struct sigaction *actp, *oactp;
940 actp = (uap->act != NULL) ? &act : NULL;
941 oactp = (uap->oact != NULL) ? &oact : NULL;
943 error = copyin(uap->act, actp, sizeof(act));
947 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
949 error = copyout(oactp, uap->oact, sizeof(oact));
953 #ifdef COMPAT_FREEBSD4
954 #ifndef _SYS_SYSPROTO_H_
955 struct freebsd4_sigaction_args {
957 struct sigaction *act;
958 struct sigaction *oact;
962 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
964 struct sigaction act, oact;
965 struct sigaction *actp, *oactp;
968 actp = (uap->act != NULL) ? &act : NULL;
969 oactp = (uap->oact != NULL) ? &oact : NULL;
971 error = copyin(uap->act, actp, sizeof(act));
975 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
977 error = copyout(oactp, uap->oact, sizeof(oact));
980 #endif /* COMAPT_FREEBSD4 */
982 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
983 #ifndef _SYS_SYSPROTO_H_
984 struct osigaction_args {
986 struct osigaction *nsa;
987 struct osigaction *osa;
991 osigaction(struct thread *td, struct osigaction_args *uap)
993 struct osigaction sa;
994 struct sigaction nsa, osa;
995 struct sigaction *nsap, *osap;
998 if (uap->signum <= 0 || uap->signum >= ONSIG)
1001 nsap = (uap->nsa != NULL) ? &nsa : NULL;
1002 osap = (uap->osa != NULL) ? &osa : NULL;
1005 error = copyin(uap->nsa, &sa, sizeof(sa));
1008 nsap->sa_handler = sa.sa_handler;
1009 nsap->sa_flags = sa.sa_flags;
1010 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
1012 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1013 if (osap && !error) {
1014 sa.sa_handler = osap->sa_handler;
1015 sa.sa_flags = osap->sa_flags;
1016 SIG2OSIG(osap->sa_mask, sa.sa_mask);
1017 error = copyout(&sa, uap->osa, sizeof(sa));
1022 #if !defined(__i386__)
1023 /* Avoid replicating the same stub everywhere */
1025 osigreturn(struct thread *td, struct osigreturn_args *uap)
1028 return (nosys(td, (struct nosys_args *)uap));
1031 #endif /* COMPAT_43 */
1034 * Initialize signal state for process 0;
1035 * set to ignore signals that are ignored by default.
1038 siginit(struct proc *p)
1045 mtx_lock(&ps->ps_mtx);
1046 for (i = 1; i <= NSIG; i++) {
1047 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
1048 SIGADDSET(ps->ps_sigignore, i);
1051 mtx_unlock(&ps->ps_mtx);
1056 * Reset specified signal to the default disposition.
1059 sigdflt(struct sigacts *ps, int sig)
1062 mtx_assert(&ps->ps_mtx, MA_OWNED);
1063 SIGDELSET(ps->ps_sigcatch, sig);
1064 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
1065 SIGADDSET(ps->ps_sigignore, sig);
1066 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1067 SIGDELSET(ps->ps_siginfo, sig);
1071 * Reset signals for an exec of the specified process.
1074 execsigs(struct proc *p)
1080 * Reset caught signals. Held signals remain held
1081 * through td_sigmask (unless they were caught,
1082 * and are now ignored by default).
1084 PROC_LOCK_ASSERT(p, MA_OWNED);
1086 mtx_lock(&ps->ps_mtx);
1090 * Reset stack state to the user stack.
1091 * Clear set of signals caught on the signal stack.
1094 MPASS(td->td_proc == p);
1095 td->td_sigstk.ss_flags = SS_DISABLE;
1096 td->td_sigstk.ss_size = 0;
1097 td->td_sigstk.ss_sp = 0;
1098 td->td_pflags &= ~TDP_ALTSTACK;
1100 * Reset no zombies if child dies flag as Solaris does.
1102 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1103 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1104 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1105 mtx_unlock(&ps->ps_mtx);
1109 * kern_sigprocmask()
1111 * Manipulate signal mask.
1114 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1117 sigset_t new_block, oset1;
1122 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1123 PROC_LOCK_ASSERT(p, MA_OWNED);
1126 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1127 ? MA_OWNED : MA_NOTOWNED);
1129 *oset = td->td_sigmask;
1136 oset1 = td->td_sigmask;
1137 SIGSETOR(td->td_sigmask, *set);
1138 new_block = td->td_sigmask;
1139 SIGSETNAND(new_block, oset1);
1142 SIGSETNAND(td->td_sigmask, *set);
1147 oset1 = td->td_sigmask;
1148 if (flags & SIGPROCMASK_OLD)
1149 SIGSETLO(td->td_sigmask, *set);
1151 td->td_sigmask = *set;
1152 new_block = td->td_sigmask;
1153 SIGSETNAND(new_block, oset1);
1162 * The new_block set contains signals that were not previously
1163 * blocked, but are blocked now.
1165 * In case we block any signal that was not previously blocked
1166 * for td, and process has the signal pending, try to schedule
1167 * signal delivery to some thread that does not block the
1168 * signal, possibly waking it up.
1170 if (p->p_numthreads != 1)
1171 reschedule_signals(p, new_block, flags);
1175 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1180 #ifndef _SYS_SYSPROTO_H_
1181 struct sigprocmask_args {
1183 const sigset_t *set;
1188 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1191 sigset_t *setp, *osetp;
1194 setp = (uap->set != NULL) ? &set : NULL;
1195 osetp = (uap->oset != NULL) ? &oset : NULL;
1197 error = copyin(uap->set, setp, sizeof(set));
1201 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1202 if (osetp && !error) {
1203 error = copyout(osetp, uap->oset, sizeof(oset));
1208 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1209 #ifndef _SYS_SYSPROTO_H_
1210 struct osigprocmask_args {
1216 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1221 OSIG2SIG(uap->mask, set);
1222 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1223 SIG2OSIG(oset, td->td_retval[0]);
1226 #endif /* COMPAT_43 */
1229 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1235 error = copyin(uap->set, &set, sizeof(set));
1237 td->td_retval[0] = error;
1241 error = kern_sigtimedwait(td, set, &ksi, NULL);
1244 * sigwait() function shall not return EINTR, but
1245 * the syscall does. Non-ancient libc provides the
1246 * wrapper which hides EINTR. Otherwise, EINTR return
1247 * is used by libthr to handle required cancellation
1248 * point in the sigwait().
1250 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1252 td->td_retval[0] = error;
1256 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1257 td->td_retval[0] = error;
1262 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1265 struct timespec *timeout;
1271 error = copyin(uap->timeout, &ts, sizeof(ts));
1279 error = copyin(uap->set, &set, sizeof(set));
1283 error = kern_sigtimedwait(td, set, &ksi, timeout);
1288 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1291 td->td_retval[0] = ksi.ksi_signo;
1296 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1302 error = copyin(uap->set, &set, sizeof(set));
1306 error = kern_sigtimedwait(td, set, &ksi, NULL);
1311 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1314 td->td_retval[0] = ksi.ksi_signo;
1319 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1323 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1327 thr->td_si.si_signo = 0;
1332 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1333 struct timespec *timeout)
1336 sigset_t saved_mask, new_block;
1338 int error, sig, timevalid = 0;
1339 sbintime_t sbt, precision, tsbt;
1347 /* Ensure the sigfastblock value is up to date. */
1348 sigfastblock_fetch(td);
1350 if (timeout != NULL) {
1351 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1354 if (ts.tv_sec < INT32_MAX / 2) {
1357 precision >>= tc_precexp;
1358 if (TIMESEL(&sbt, tsbt))
1362 precision = sbt = 0;
1365 precision = sbt = 0;
1367 /* Some signals can not be waited for. */
1368 SIG_CANTMASK(waitset);
1371 saved_mask = td->td_sigmask;
1372 SIGSETNAND(td->td_sigmask, waitset);
1373 if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1374 !kern_sig_discard_ign) {
1376 td->td_flags |= TDF_SIGWAIT;
1380 mtx_lock(&ps->ps_mtx);
1382 mtx_unlock(&ps->ps_mtx);
1383 KASSERT(sig >= 0, ("sig %d", sig));
1384 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1385 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1386 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1396 * POSIX says this must be checked after looking for pending
1399 if (timeout != NULL && !timevalid) {
1409 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1410 "sigwait", sbt, precision, C_ABSOLUTE);
1412 /* The syscalls can not be restarted. */
1413 if (error == ERESTART)
1417 * If PTRACE_SCE or PTRACE_SCX were set after
1418 * userspace entered the syscall, return spurious
1419 * EINTR after wait was done. Only do this as last
1420 * resort after rechecking for possible queued signals
1421 * and expired timeouts.
1423 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1427 td->td_flags &= ~TDF_SIGWAIT;
1430 new_block = saved_mask;
1431 SIGSETNAND(new_block, td->td_sigmask);
1432 td->td_sigmask = saved_mask;
1434 * Fewer signals can be delivered to us, reschedule signal
1437 if (p->p_numthreads != 1)
1438 reschedule_signals(p, new_block, 0);
1441 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1443 if (ksi->ksi_code == SI_TIMER)
1444 itimer_accept(p, ksi->ksi_timerid, ksi);
1447 if (KTRPOINT(td, KTR_PSIG)) {
1450 mtx_lock(&ps->ps_mtx);
1451 action = ps->ps_sigact[_SIG_IDX(sig)];
1452 mtx_unlock(&ps->ps_mtx);
1453 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1456 if (sig == SIGKILL) {
1457 proc_td_siginfo_capture(td, &ksi->ksi_info);
1465 #ifndef _SYS_SYSPROTO_H_
1466 struct sigpending_args {
1471 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1473 struct proc *p = td->td_proc;
1477 pending = p->p_sigqueue.sq_signals;
1478 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1480 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1483 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1484 #ifndef _SYS_SYSPROTO_H_
1485 struct osigpending_args {
1490 osigpending(struct thread *td, struct osigpending_args *uap)
1492 struct proc *p = td->td_proc;
1496 pending = p->p_sigqueue.sq_signals;
1497 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1499 SIG2OSIG(pending, td->td_retval[0]);
1502 #endif /* COMPAT_43 */
1504 #if defined(COMPAT_43)
1506 * Generalized interface signal handler, 4.3-compatible.
1508 #ifndef _SYS_SYSPROTO_H_
1509 struct osigvec_args {
1517 osigvec(struct thread *td, struct osigvec_args *uap)
1520 struct sigaction nsa, osa;
1521 struct sigaction *nsap, *osap;
1524 if (uap->signum <= 0 || uap->signum >= ONSIG)
1526 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1527 osap = (uap->osv != NULL) ? &osa : NULL;
1529 error = copyin(uap->nsv, &vec, sizeof(vec));
1532 nsap->sa_handler = vec.sv_handler;
1533 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1534 nsap->sa_flags = vec.sv_flags;
1535 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1537 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1538 if (osap && !error) {
1539 vec.sv_handler = osap->sa_handler;
1540 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1541 vec.sv_flags = osap->sa_flags;
1542 vec.sv_flags &= ~SA_NOCLDWAIT;
1543 vec.sv_flags ^= SA_RESTART;
1544 error = copyout(&vec, uap->osv, sizeof(vec));
1549 #ifndef _SYS_SYSPROTO_H_
1550 struct osigblock_args {
1555 osigblock(struct thread *td, struct osigblock_args *uap)
1559 OSIG2SIG(uap->mask, set);
1560 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1561 SIG2OSIG(oset, td->td_retval[0]);
1565 #ifndef _SYS_SYSPROTO_H_
1566 struct osigsetmask_args {
1571 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1575 OSIG2SIG(uap->mask, set);
1576 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1577 SIG2OSIG(oset, td->td_retval[0]);
1580 #endif /* COMPAT_43 */
1583 * Suspend calling thread until signal, providing mask to be set in the
1586 #ifndef _SYS_SYSPROTO_H_
1587 struct sigsuspend_args {
1588 const sigset_t *sigmask;
1593 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1598 error = copyin(uap->sigmask, &mask, sizeof(mask));
1601 return (kern_sigsuspend(td, mask));
1605 kern_sigsuspend(struct thread *td, sigset_t mask)
1607 struct proc *p = td->td_proc;
1610 /* Ensure the sigfastblock value is up to date. */
1611 sigfastblock_fetch(td);
1614 * When returning from sigsuspend, we want
1615 * the old mask to be restored after the
1616 * signal handler has finished. Thus, we
1617 * save it here and mark the sigacts structure
1621 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1622 SIGPROCMASK_PROC_LOCKED);
1623 td->td_pflags |= TDP_OLDMASK;
1624 ast_sched(td, TDA_SIGSUSPEND);
1627 * Process signals now. Otherwise, we can get spurious wakeup
1628 * due to signal entered process queue, but delivered to other
1629 * thread. But sigsuspend should return only on signal
1632 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1633 for (has_sig = 0; !has_sig;) {
1634 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1637 thread_suspend_check(0);
1638 mtx_lock(&p->p_sigacts->ps_mtx);
1639 while ((sig = cursig(td)) != 0) {
1640 KASSERT(sig >= 0, ("sig %d", sig));
1641 has_sig += postsig(sig);
1643 mtx_unlock(&p->p_sigacts->ps_mtx);
1646 * If PTRACE_SCE or PTRACE_SCX were set after
1647 * userspace entered the syscall, return spurious
1650 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1654 td->td_errno = EINTR;
1655 td->td_pflags |= TDP_NERRNO;
1656 return (EJUSTRETURN);
1659 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1661 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1662 * convention: libc stub passes mask, not pointer, to save a copyin.
1664 #ifndef _SYS_SYSPROTO_H_
1665 struct osigsuspend_args {
1671 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1675 OSIG2SIG(uap->mask, mask);
1676 return (kern_sigsuspend(td, mask));
1678 #endif /* COMPAT_43 */
1680 #if defined(COMPAT_43)
1681 #ifndef _SYS_SYSPROTO_H_
1682 struct osigstack_args {
1683 struct sigstack *nss;
1684 struct sigstack *oss;
1689 osigstack(struct thread *td, struct osigstack_args *uap)
1691 struct sigstack nss, oss;
1694 if (uap->nss != NULL) {
1695 error = copyin(uap->nss, &nss, sizeof(nss));
1699 oss.ss_sp = td->td_sigstk.ss_sp;
1700 oss.ss_onstack = sigonstack(cpu_getstack(td));
1701 if (uap->nss != NULL) {
1702 td->td_sigstk.ss_sp = nss.ss_sp;
1703 td->td_sigstk.ss_size = 0;
1704 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1705 td->td_pflags |= TDP_ALTSTACK;
1707 if (uap->oss != NULL)
1708 error = copyout(&oss, uap->oss, sizeof(oss));
1712 #endif /* COMPAT_43 */
1714 #ifndef _SYS_SYSPROTO_H_
1715 struct sigaltstack_args {
1722 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1727 if (uap->ss != NULL) {
1728 error = copyin(uap->ss, &ss, sizeof(ss));
1732 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1733 (uap->oss != NULL) ? &oss : NULL);
1736 if (uap->oss != NULL)
1737 error = copyout(&oss, uap->oss, sizeof(stack_t));
1742 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1744 struct proc *p = td->td_proc;
1747 oonstack = sigonstack(cpu_getstack(td));
1750 *oss = td->td_sigstk;
1751 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1752 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1758 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1760 if (!(ss->ss_flags & SS_DISABLE)) {
1761 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1764 td->td_sigstk = *ss;
1765 td->td_pflags |= TDP_ALTSTACK;
1767 td->td_pflags &= ~TDP_ALTSTACK;
1773 struct killpg1_ctx {
1783 killpg1_sendsig_locked(struct proc *p, struct killpg1_ctx *arg)
1787 err = p_cansignal(arg->td, p, arg->sig);
1788 if (err == 0 && arg->sig != 0)
1789 pksignal(p, arg->sig, arg->ksi);
1794 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1799 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1802 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1803 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1807 killpg1_sendsig_locked(p, arg);
1812 kill_processes_prison_cb(struct proc *p, void *arg)
1814 struct killpg1_ctx *ctx = arg;
1816 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1817 (p == ctx->td->td_proc) || p->p_state == PRS_NEW)
1820 killpg1_sendsig_locked(p, ctx);
1824 * Common code for kill process group/broadcast kill.
1825 * td is the calling thread, as usual.
1828 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1832 struct killpg1_ctx arg;
1844 prison_proc_iterate(td->td_ucred->cr_prison,
1845 kill_processes_prison_cb, &arg);
1848 sx_slock(&proctree_lock);
1851 * zero pgid means send to my process group.
1853 pgrp = td->td_proc->p_pgrp;
1856 pgrp = pgfind(pgid);
1858 sx_sunlock(&proctree_lock);
1862 sx_sunlock(&proctree_lock);
1863 if (!sx_try_xlock(&pgrp->pg_killsx)) {
1865 sx_xlock(&pgrp->pg_killsx);
1866 sx_xunlock(&pgrp->pg_killsx);
1869 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1870 killpg1_sendsig(p, false, &arg);
1873 sx_xunlock(&pgrp->pg_killsx);
1875 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1876 if (arg.ret == 0 && !arg.sent)
1877 arg.ret = arg.found ? EPERM : ESRCH;
1881 #ifndef _SYS_SYSPROTO_H_
1889 sys_kill(struct thread *td, struct kill_args *uap)
1892 return (kern_kill(td, uap->pid, uap->signum));
1896 kern_kill(struct thread *td, pid_t pid, int signum)
1903 * A process in capability mode can send signals only to himself.
1904 * The main rationale behind this is that abort(3) is implemented as
1905 * kill(getpid(), SIGABRT).
1907 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1910 AUDIT_ARG_SIGNUM(signum);
1912 if ((u_int)signum > _SIG_MAXSIG)
1915 ksiginfo_init(&ksi);
1916 ksi.ksi_signo = signum;
1917 ksi.ksi_code = SI_USER;
1918 ksi.ksi_pid = td->td_proc->p_pid;
1919 ksi.ksi_uid = td->td_ucred->cr_ruid;
1922 /* kill single process */
1923 if ((p = pfind_any(pid)) == NULL)
1925 AUDIT_ARG_PROCESS(p);
1926 error = p_cansignal(td, p, signum);
1927 if (error == 0 && signum)
1928 pksignal(p, signum, &ksi);
1933 case -1: /* broadcast signal */
1934 return (killpg1(td, signum, 0, 1, &ksi));
1935 case 0: /* signal own process group */
1936 return (killpg1(td, signum, 0, 0, &ksi));
1937 default: /* negative explicit process group */
1938 return (killpg1(td, signum, -pid, 0, &ksi));
1944 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1949 AUDIT_ARG_SIGNUM(uap->signum);
1950 AUDIT_ARG_FD(uap->fd);
1951 if ((u_int)uap->signum > _SIG_MAXSIG)
1954 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1957 AUDIT_ARG_PROCESS(p);
1958 error = p_cansignal(td, p, uap->signum);
1959 if (error == 0 && uap->signum)
1960 kern_psignal(p, uap->signum);
1965 #if defined(COMPAT_43)
1966 #ifndef _SYS_SYSPROTO_H_
1967 struct okillpg_args {
1974 okillpg(struct thread *td, struct okillpg_args *uap)
1978 AUDIT_ARG_SIGNUM(uap->signum);
1979 AUDIT_ARG_PID(uap->pgid);
1980 if ((u_int)uap->signum > _SIG_MAXSIG)
1983 ksiginfo_init(&ksi);
1984 ksi.ksi_signo = uap->signum;
1985 ksi.ksi_code = SI_USER;
1986 ksi.ksi_pid = td->td_proc->p_pid;
1987 ksi.ksi_uid = td->td_ucred->cr_ruid;
1988 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1990 #endif /* COMPAT_43 */
1992 #ifndef _SYS_SYSPROTO_H_
1993 struct sigqueue_args {
1996 /* union sigval */ void *value;
2000 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
2004 sv.sival_ptr = uap->value;
2006 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
2010 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
2016 if ((u_int)signum > _SIG_MAXSIG)
2020 * Specification says sigqueue can only send signal to
2026 if ((p = pfind_any(pid)) == NULL)
2028 error = p_cansignal(td, p, signum);
2029 if (error == 0 && signum != 0) {
2030 ksiginfo_init(&ksi);
2031 ksi.ksi_flags = KSI_SIGQ;
2032 ksi.ksi_signo = signum;
2033 ksi.ksi_code = SI_QUEUE;
2034 ksi.ksi_pid = td->td_proc->p_pid;
2035 ksi.ksi_uid = td->td_ucred->cr_ruid;
2036 ksi.ksi_value = *value;
2037 error = pksignal(p, ksi.ksi_signo, &ksi);
2044 * Send a signal to a process group. If checktty is 1,
2045 * limit to members which have a controlling terminal.
2048 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
2053 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
2054 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
2056 if (p->p_state == PRS_NORMAL &&
2057 (checkctty == 0 || p->p_flag & P_CONTROLT))
2058 pksignal(p, sig, ksi);
2065 * Recalculate the signal mask and reset the signal disposition after
2066 * usermode frame for delivery is formed. Should be called after
2067 * mach-specific routine, because sysent->sv_sendsig() needs correct
2068 * ps_siginfo and signal mask.
2071 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2075 mtx_assert(&ps->ps_mtx, MA_OWNED);
2076 td->td_ru.ru_nsignals++;
2077 mask = ps->ps_catchmask[_SIG_IDX(sig)];
2078 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2079 SIGADDSET(mask, sig);
2080 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2081 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2082 if (SIGISMEMBER(ps->ps_sigreset, sig))
2087 * Send a signal caused by a trap to the current thread. If it will be
2088 * caught immediately, deliver it with correct code. Otherwise, post it
2092 trapsignal(struct thread *td, ksiginfo_t *ksi)
2100 sig = ksi->ksi_signo;
2101 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2103 sigfastblock_fetch(td);
2106 mtx_lock(&ps->ps_mtx);
2107 sigmask = td->td_sigmask;
2108 if (td->td_sigblock_val != 0)
2109 SIGSETOR(sigmask, fastblock_mask);
2110 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2111 !SIGISMEMBER(sigmask, sig)) {
2113 if (KTRPOINT(curthread, KTR_PSIG))
2114 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2115 &td->td_sigmask, ksi->ksi_code);
2117 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2118 ksi, &td->td_sigmask);
2119 postsig_done(sig, td, ps);
2120 mtx_unlock(&ps->ps_mtx);
2123 * Avoid a possible infinite loop if the thread
2124 * masking the signal or process is ignoring the
2127 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2128 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2129 SIGDELSET(td->td_sigmask, sig);
2130 SIGDELSET(ps->ps_sigcatch, sig);
2131 SIGDELSET(ps->ps_sigignore, sig);
2132 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2133 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2134 td->td_sigblock_val = 0;
2136 mtx_unlock(&ps->ps_mtx);
2137 p->p_sig = sig; /* XXX to verify code */
2138 tdsendsignal(p, td, sig, ksi);
2143 static struct thread *
2144 sigtd(struct proc *p, int sig, bool fast_sigblock)
2146 struct thread *td, *signal_td;
2148 PROC_LOCK_ASSERT(p, MA_OWNED);
2149 MPASS(!fast_sigblock || p == curproc);
2152 * Check if current thread can handle the signal without
2153 * switching context to another thread.
2155 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2156 (!fast_sigblock || curthread->td_sigblock_val == 0))
2159 /* Find a non-stopped thread that does not mask the signal. */
2161 FOREACH_THREAD_IN_PROC(p, td) {
2162 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2163 td != curthread || td->td_sigblock_val == 0) &&
2164 (td->td_flags & TDF_BOUNDARY) == 0) {
2169 /* Select random (first) thread if no better match was found. */
2170 if (signal_td == NULL)
2171 signal_td = FIRST_THREAD_IN_PROC(p);
2176 * Send the signal to the process. If the signal has an action, the action
2177 * is usually performed by the target process rather than the caller; we add
2178 * the signal to the set of pending signals for the process.
2181 * o When a stop signal is sent to a sleeping process that takes the
2182 * default action, the process is stopped without awakening it.
2183 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2184 * regardless of the signal action (eg, blocked or ignored).
2186 * Other ignored signals are discarded immediately.
2188 * NB: This function may be entered from the debugger via the "kill" DDB
2189 * command. There is little that can be done to mitigate the possibly messy
2190 * side effects of this unwise possibility.
2193 kern_psignal(struct proc *p, int sig)
2197 ksiginfo_init(&ksi);
2198 ksi.ksi_signo = sig;
2199 ksi.ksi_code = SI_KERNEL;
2200 (void) tdsendsignal(p, NULL, sig, &ksi);
2204 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2207 return (tdsendsignal(p, NULL, sig, ksi));
2210 /* Utility function for finding a thread to send signal event to. */
2212 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2216 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2217 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2229 tdsignal(struct thread *td, int sig)
2233 ksiginfo_init(&ksi);
2234 ksi.ksi_signo = sig;
2235 ksi.ksi_code = SI_KERNEL;
2236 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2240 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2243 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2247 sig_sleepq_abort(struct thread *td, int intrval)
2249 THREAD_LOCK_ASSERT(td, MA_OWNED);
2251 if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2255 return (sleepq_abort(td, intrval));
2259 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2262 sigqueue_t *sigqueue;
2269 MPASS(td == NULL || p == td->td_proc);
2270 PROC_LOCK_ASSERT(p, MA_OWNED);
2272 if (!_SIG_VALID(sig))
2273 panic("%s(): invalid signal %d", __func__, sig);
2275 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2278 * IEEE Std 1003.1-2001: return success when killing a zombie.
2280 if (p->p_state == PRS_ZOMBIE) {
2281 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2282 ksiginfo_tryfree(ksi);
2287 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2288 prop = sigprop(sig);
2291 td = sigtd(p, sig, false);
2292 sigqueue = &p->p_sigqueue;
2294 sigqueue = &td->td_sigqueue;
2296 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2299 * If the signal is being ignored, then we forget about it
2300 * immediately, except when the target process executes
2301 * sigwait(). (Note: we don't set SIGCONT in ps_sigignore,
2302 * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2304 mtx_lock(&ps->ps_mtx);
2305 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2306 if (kern_sig_discard_ign &&
2307 (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2308 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2310 mtx_unlock(&ps->ps_mtx);
2311 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2312 ksiginfo_tryfree(ksi);
2319 if (SIGISMEMBER(td->td_sigmask, sig))
2321 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2325 if (SIGISMEMBER(ps->ps_sigintr, sig))
2330 mtx_unlock(&ps->ps_mtx);
2332 if (prop & SIGPROP_CONT)
2333 sigqueue_delete_stopmask_proc(p);
2334 else if (prop & SIGPROP_STOP) {
2336 * If sending a tty stop signal to a member of an orphaned
2337 * process group, discard the signal here if the action
2338 * is default; don't stop the process below if sleeping,
2339 * and don't clear any pending SIGCONT.
2341 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2342 (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2343 action == SIG_DFL) {
2344 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2345 ksiginfo_tryfree(ksi);
2348 sigqueue_delete_proc(p, SIGCONT);
2349 if (p->p_flag & P_CONTINUED) {
2350 p->p_flag &= ~P_CONTINUED;
2351 PROC_LOCK(p->p_pptr);
2352 sigqueue_take(p->p_ksi);
2353 PROC_UNLOCK(p->p_pptr);
2357 ret = sigqueue_add(sigqueue, sig, ksi);
2362 * Defer further processing for signals which are held,
2363 * except that stopped processes must be continued by SIGCONT.
2365 if (action == SIG_HOLD &&
2366 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2372 * Some signals have a process-wide effect and a per-thread
2373 * component. Most processing occurs when the process next
2374 * tries to cross the user boundary, however there are some
2375 * times when processing needs to be done immediately, such as
2376 * waking up threads so that they can cross the user boundary.
2377 * We try to do the per-process part here.
2379 if (P_SHOULDSTOP(p)) {
2380 KASSERT(!(p->p_flag & P_WEXIT),
2381 ("signal to stopped but exiting process"));
2382 if (sig == SIGKILL) {
2384 * If traced process is already stopped,
2385 * then no further action is necessary.
2387 if (p->p_flag & P_TRACED)
2390 * SIGKILL sets process running.
2391 * It will die elsewhere.
2392 * All threads must be restarted.
2394 p->p_flag &= ~P_STOPPED_SIG;
2398 if (prop & SIGPROP_CONT) {
2400 * If traced process is already stopped,
2401 * then no further action is necessary.
2403 if (p->p_flag & P_TRACED)
2406 * If SIGCONT is default (or ignored), we continue the
2407 * process but don't leave the signal in sigqueue as
2408 * it has no further action. If SIGCONT is held, we
2409 * continue the process and leave the signal in
2410 * sigqueue. If the process catches SIGCONT, let it
2411 * handle the signal itself. If it isn't waiting on
2412 * an event, it goes back to run state.
2413 * Otherwise, process goes back to sleep state.
2415 p->p_flag &= ~P_STOPPED_SIG;
2417 if (p->p_numthreads == p->p_suspcount) {
2419 p->p_flag |= P_CONTINUED;
2420 p->p_xsig = SIGCONT;
2421 PROC_LOCK(p->p_pptr);
2422 childproc_continued(p);
2423 PROC_UNLOCK(p->p_pptr);
2426 if (action == SIG_DFL) {
2427 thread_unsuspend(p);
2429 sigqueue_delete(sigqueue, sig);
2432 if (action == SIG_CATCH) {
2434 * The process wants to catch it so it needs
2435 * to run at least one thread, but which one?
2441 * The signal is not ignored or caught.
2443 thread_unsuspend(p);
2448 if (prop & SIGPROP_STOP) {
2450 * If traced process is already stopped,
2451 * then no further action is necessary.
2453 if (p->p_flag & P_TRACED)
2456 * Already stopped, don't need to stop again
2457 * (If we did the shell could get confused).
2458 * Just make sure the signal STOP bit set.
2460 p->p_flag |= P_STOPPED_SIG;
2461 sigqueue_delete(sigqueue, sig);
2466 * All other kinds of signals:
2467 * If a thread is sleeping interruptibly, simulate a
2468 * wakeup so that when it is continued it will be made
2469 * runnable and can look at the signal. However, don't make
2470 * the PROCESS runnable, leave it stopped.
2471 * It may run a bit until it hits a thread_suspend_check().
2475 if (TD_CAN_ABORT(td))
2476 wakeup_swapper = sig_sleepq_abort(td, intrval);
2482 * Mutexes are short lived. Threads waiting on them will
2483 * hit thread_suspend_check() soon.
2485 } else if (p->p_state == PRS_NORMAL) {
2486 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2487 tdsigwakeup(td, sig, action, intrval);
2491 MPASS(action == SIG_DFL);
2493 if (prop & SIGPROP_STOP) {
2494 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2496 p->p_flag |= P_STOPPED_SIG;
2499 wakeup_swapper = sig_suspend_threads(td, p);
2500 if (p->p_numthreads == p->p_suspcount) {
2502 * only thread sending signal to another
2503 * process can reach here, if thread is sending
2504 * signal to its process, because thread does
2505 * not suspend itself here, p_numthreads
2506 * should never be equal to p_suspcount.
2510 sigqueue_delete_proc(p, p->p_xsig);
2516 /* Not in "NORMAL" state. discard the signal. */
2517 sigqueue_delete(sigqueue, sig);
2522 * The process is not stopped so we need to apply the signal to all the
2526 tdsigwakeup(td, sig, action, intrval);
2528 thread_unsuspend(p);
2531 itimer_proc_continue(p);
2532 kqtimer_proc_continue(p);
2534 /* If we jump here, proc slock should not be owned. */
2535 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2543 * The force of a signal has been directed against a single
2544 * thread. We need to see what we can do about knocking it
2545 * out of any sleep it may be in etc.
2548 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2550 struct proc *p = td->td_proc;
2551 int prop, wakeup_swapper;
2553 PROC_LOCK_ASSERT(p, MA_OWNED);
2554 prop = sigprop(sig);
2559 * Bring the priority of a thread up if we want it to get
2560 * killed in this lifetime. Be careful to avoid bumping the
2561 * priority of the idle thread, since we still allow to signal
2564 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2565 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2566 sched_prio(td, PUSER);
2567 if (TD_ON_SLEEPQ(td)) {
2569 * If thread is sleeping uninterruptibly
2570 * we can't interrupt the sleep... the signal will
2571 * be noticed when the process returns through
2572 * trap() or syscall().
2574 if ((td->td_flags & TDF_SINTR) == 0)
2577 * If SIGCONT is default (or ignored) and process is
2578 * asleep, we are finished; the process should not
2581 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2584 sigqueue_delete(&p->p_sigqueue, sig);
2586 * It may be on either list in this state.
2587 * Remove from both for now.
2589 sigqueue_delete(&td->td_sigqueue, sig);
2594 * Don't awaken a sleeping thread for SIGSTOP if the
2595 * STOP signal is deferred.
2597 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2598 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2602 * Give low priority threads a better chance to run.
2604 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2605 sched_prio(td, PUSER);
2607 wakeup_swapper = sig_sleepq_abort(td, intrval);
2615 * Other states do nothing with the signal immediately,
2616 * other than kicking ourselves if we are running.
2617 * It will either never be noticed, or noticed very soon.
2620 if (TD_IS_RUNNING(td) && td != curthread)
2630 ptrace_coredumpreq(struct thread *td, struct proc *p,
2631 struct thr_coredump_req *tcq)
2635 if (p->p_sysent->sv_coredump == NULL) {
2636 tcq->tc_error = ENOSYS;
2640 rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2641 tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2642 tcq->tc_limit, tcq->tc_flags);
2643 vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2647 ptrace_syscallreq(struct thread *td, struct proc *p,
2648 struct thr_syscall_req *tsr)
2650 struct sysentvec *sv;
2652 register_t rv_saved[2];
2655 bool audited, sy_thr_static;
2658 if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2659 tsr->ts_ret.sr_error = ENOSYS;
2663 sc = tsr->ts_sa.code;
2664 if (sc == SYS_syscall || sc == SYS___syscall) {
2665 sc = tsr->ts_sa.args[0];
2666 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2667 sizeof(register_t) * (tsr->ts_nargs - 1));
2670 tsr->ts_sa.callp = se = &sv->sv_table[sc];
2672 VM_CNT_INC(v_syscall);
2674 if (__predict_false(td->td_cowgen != atomic_load_int(
2675 &td->td_proc->p_cowgen)))
2676 thread_cow_update(td);
2678 #ifdef CAPABILITY_MODE
2679 if (IN_CAPABILITY_MODE(td) && (se->sy_flags & SYF_CAPENABLED) == 0) {
2680 tsr->ts_ret.sr_error = ECAPMODE;
2685 sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2686 audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2688 if (!sy_thr_static) {
2689 error = syscall_thread_enter(td, se);
2691 tsr->ts_ret.sr_error = error;
2696 rv_saved[0] = td->td_retval[0];
2697 rv_saved[1] = td->td_retval[1];
2698 nerror = td->td_errno;
2699 td->td_retval[0] = 0;
2700 td->td_retval[1] = 0;
2702 #ifdef KDTRACE_HOOKS
2703 if (se->sy_entry != 0)
2704 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2706 tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2707 #ifdef KDTRACE_HOOKS
2708 if (se->sy_return != 0)
2709 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2710 tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2713 tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2714 tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2715 td->td_retval[0] = rv_saved[0];
2716 td->td_retval[1] = rv_saved[1];
2717 td->td_errno = nerror;
2720 AUDIT_SYSCALL_EXIT(error, td);
2722 syscall_thread_exit(td, se);
2726 ptrace_remotereq(struct thread *td, int flag)
2730 MPASS(td == curthread);
2732 PROC_LOCK_ASSERT(p, MA_OWNED);
2733 if ((td->td_dbgflags & flag) == 0)
2735 KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2736 KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2740 case TDB_COREDUMPREQ:
2741 ptrace_coredumpreq(td, p, td->td_remotereq);
2743 case TDB_SCREMOTEREQ:
2744 ptrace_syscallreq(td, p, td->td_remotereq);
2751 MPASS((td->td_dbgflags & flag) != 0);
2752 td->td_dbgflags &= ~flag;
2753 td->td_remotereq = NULL;
2758 sig_suspend_threads(struct thread *td, struct proc *p)
2763 PROC_LOCK_ASSERT(p, MA_OWNED);
2764 PROC_SLOCK_ASSERT(p, MA_OWNED);
2767 FOREACH_THREAD_IN_PROC(p, td2) {
2769 ast_sched_locked(td2, TDA_SUSPEND);
2770 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2771 (td2->td_flags & TDF_SINTR)) {
2772 if (td2->td_flags & TDF_SBDRY) {
2774 * Once a thread is asleep with
2775 * TDF_SBDRY and without TDF_SERESTART
2776 * or TDF_SEINTR set, it should never
2777 * become suspended due to this check.
2779 KASSERT(!TD_IS_SUSPENDED(td2),
2780 ("thread with deferred stops suspended"));
2781 if (TD_SBDRY_INTR(td2)) {
2782 wakeup_swapper |= sleepq_abort(td2,
2783 TD_SBDRY_ERRNO(td2));
2786 } else if (!TD_IS_SUSPENDED(td2))
2787 thread_suspend_one(td2);
2788 } else if (!TD_IS_SUSPENDED(td2)) {
2790 if (TD_IS_RUNNING(td2) && td2 != td)
2791 forward_signal(td2);
2796 return (wakeup_swapper);
2800 * Stop the process for an event deemed interesting to the debugger. If si is
2801 * non-NULL, this is a signal exchange; the new signal requested by the
2802 * debugger will be returned for handling. If si is NULL, this is some other
2803 * type of interesting event. The debugger may request a signal be delivered in
2804 * that case as well, however it will be deferred until it can be handled.
2807 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2809 struct proc *p = td->td_proc;
2813 PROC_LOCK_ASSERT(p, MA_OWNED);
2814 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2815 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2816 &p->p_mtx.lock_object, "Stopping for traced signal");
2820 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2821 td->td_dbgflags |= TDB_XSIG;
2822 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2823 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2825 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2828 * Ensure that, if we've been PT_KILLed, the
2829 * exit status reflects that. Another thread
2830 * may also be in ptracestop(), having just
2831 * received the SIGKILL, but this thread was
2832 * unsuspended first.
2834 td->td_dbgflags &= ~TDB_XSIG;
2835 td->td_xsig = SIGKILL;
2839 if (p->p_flag & P_SINGLE_EXIT &&
2840 !(td->td_dbgflags & TDB_EXIT)) {
2842 * Ignore ptrace stops except for thread exit
2843 * events when the process exits.
2845 td->td_dbgflags &= ~TDB_XSIG;
2851 * Make wait(2) work. Ensure that right after the
2852 * attach, the thread which was decided to become the
2853 * leader of attach gets reported to the waiter.
2854 * Otherwise, just avoid overwriting another thread's
2855 * assignment to p_xthread. If another thread has
2856 * already set p_xthread, the current thread will get
2857 * a chance to report itself upon the next iteration.
2859 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2860 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2861 p->p_xthread == NULL)) {
2866 * If we are on sleepqueue already,
2867 * let sleepqueue code decide if it
2868 * needs to go sleep after attach.
2870 if (td->td_wchan == NULL)
2871 td->td_dbgflags &= ~TDB_FSTP;
2873 p->p_flag2 &= ~P2_PTRACE_FSTP;
2874 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2875 sig_suspend_threads(td, p);
2877 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2878 td->td_dbgflags &= ~TDB_STOPATFORK;
2881 td->td_dbgflags |= TDB_SSWITCH;
2882 thread_suspend_switch(td, p);
2883 td->td_dbgflags &= ~TDB_SSWITCH;
2884 if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2885 TDB_SCREMOTEREQ)) != 0) {
2886 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2887 TDB_SCREMOTEREQ)) !=
2888 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2890 ptrace_remotereq(td, td->td_dbgflags &
2891 (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2895 if (p->p_xthread == td)
2896 p->p_xthread = NULL;
2897 if (!(p->p_flag & P_TRACED))
2899 if (td->td_dbgflags & TDB_SUSPEND) {
2900 if (p->p_flag & P_SINGLE_EXIT)
2908 if (si != NULL && sig == td->td_xsig) {
2909 /* Parent wants us to take the original signal unchanged. */
2910 si->ksi_flags |= KSI_HEAD;
2911 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2913 } else if (td->td_xsig != 0) {
2915 * If parent wants us to take a new signal, then it will leave
2916 * it in td->td_xsig; otherwise we just look for signals again.
2918 ksiginfo_init(&ksi);
2919 ksi.ksi_signo = td->td_xsig;
2920 ksi.ksi_flags |= KSI_PTRACE;
2921 td2 = sigtd(p, td->td_xsig, false);
2922 tdsendsignal(p, td2, td->td_xsig, &ksi);
2927 return (td->td_xsig);
2931 reschedule_signals(struct proc *p, sigset_t block, int flags)
2936 bool fastblk, pslocked;
2938 PROC_LOCK_ASSERT(p, MA_OWNED);
2940 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2941 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2942 if (SIGISEMPTY(p->p_siglist))
2944 SIGSETAND(block, p->p_siglist);
2945 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2946 SIG_FOREACH(sig, &block) {
2947 td = sigtd(p, sig, fastblk);
2950 * If sigtd() selected us despite sigfastblock is
2951 * blocking, do not activate AST or wake us, to avoid
2952 * loop in AST handler.
2954 if (fastblk && td == curthread)
2959 mtx_lock(&ps->ps_mtx);
2960 if (p->p_flag & P_TRACED ||
2961 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2962 !SIGISMEMBER(td->td_sigmask, sig))) {
2963 tdsigwakeup(td, sig, SIG_CATCH,
2964 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2968 mtx_unlock(&ps->ps_mtx);
2973 tdsigcleanup(struct thread *td)
2979 PROC_LOCK_ASSERT(p, MA_OWNED);
2981 sigqueue_flush(&td->td_sigqueue);
2982 if (p->p_numthreads == 1)
2986 * Since we cannot handle signals, notify signal post code
2987 * about this by filling the sigmask.
2989 * Also, if needed, wake up thread(s) that do not block the
2990 * same signals as the exiting thread, since the thread might
2991 * have been selected for delivery and woken up.
2993 SIGFILLSET(unblocked);
2994 SIGSETNAND(unblocked, td->td_sigmask);
2995 SIGFILLSET(td->td_sigmask);
2996 reschedule_signals(p, unblocked, 0);
3001 sigdeferstop_curr_flags(int cflags)
3004 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
3005 (cflags & TDF_SBDRY) != 0);
3006 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
3010 * Defer the delivery of SIGSTOP for the current thread, according to
3011 * the requested mode. Returns previous flags, which must be restored
3012 * by sigallowstop().
3014 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
3015 * cleared by the current thread, which allow the lock-less read-only
3019 sigdeferstop_impl(int mode)
3025 cflags = sigdeferstop_curr_flags(td->td_flags);
3027 case SIGDEFERSTOP_NOP:
3030 case SIGDEFERSTOP_OFF:
3033 case SIGDEFERSTOP_SILENT:
3034 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
3036 case SIGDEFERSTOP_EINTR:
3037 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
3039 case SIGDEFERSTOP_ERESTART:
3040 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
3043 panic("sigdeferstop: invalid mode %x", mode);
3046 if (cflags == nflags)
3047 return (SIGDEFERSTOP_VAL_NCHG);
3049 td->td_flags = (td->td_flags & ~cflags) | nflags;
3055 * Restores the STOP handling mode, typically permitting the delivery
3056 * of SIGSTOP for the current thread. This does not immediately
3057 * suspend if a stop was posted. Instead, the thread will suspend
3058 * either via ast() or a subsequent interruptible sleep.
3061 sigallowstop_impl(int prev)
3066 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3067 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3068 ("sigallowstop: incorrect previous mode %x", prev));
3070 cflags = sigdeferstop_curr_flags(td->td_flags);
3071 if (cflags != prev) {
3073 td->td_flags = (td->td_flags & ~cflags) | prev;
3082 SIGSTATUS_SBDRY_STOP,
3086 * The thread has signal "sig" pending. Figure out what to do with it:
3088 * _HANDLE -> the caller should handle the signal
3089 * _HANDLED -> handled internally, reload pending signal set
3090 * _IGNORE -> ignored, remove from the set of pending signals and try the
3091 * next pending signal
3092 * _SBDRY_STOP -> the signal should stop the thread but this is not
3093 * permitted in the current context
3095 static enum sigstatus
3096 sigprocess(struct thread *td, int sig)
3100 struct sigqueue *queue;
3104 KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3108 mtx_assert(&ps->ps_mtx, MA_OWNED);
3109 PROC_LOCK_ASSERT(p, MA_OWNED);
3112 * We should allow pending but ignored signals below
3113 * if there is sigwait() active, or P_TRACED was
3114 * on when they were posted.
3116 if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3117 (p->p_flag & P_TRACED) == 0 &&
3118 (td->td_flags & TDF_SIGWAIT) == 0) {
3119 return (SIGSTATUS_IGNORE);
3123 * If the process is going to single-thread mode to prepare
3124 * for exit, there is no sense in delivering any signal
3125 * to usermode. Another important consequence is that
3126 * msleep(..., PCATCH, ...) now is only interruptible by a
3129 if ((p->p_flag2 & P2_WEXIT) != 0)
3130 return (SIGSTATUS_IGNORE);
3132 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3134 * If traced, always stop.
3135 * Remove old signal from queue before the stop.
3136 * XXX shrug off debugger, it causes siginfo to
3139 queue = &td->td_sigqueue;
3140 ksiginfo_init(&ksi);
3141 if (sigqueue_get(queue, sig, &ksi) == 0) {
3142 queue = &p->p_sigqueue;
3143 sigqueue_get(queue, sig, &ksi);
3145 td->td_si = ksi.ksi_info;
3147 mtx_unlock(&ps->ps_mtx);
3148 sig = ptracestop(td, sig, &ksi);
3149 mtx_lock(&ps->ps_mtx);
3151 td->td_si.si_signo = 0;
3154 * Keep looking if the debugger discarded or
3155 * replaced the signal.
3158 return (SIGSTATUS_HANDLED);
3161 * If the signal became masked, re-queue it.
3163 if (SIGISMEMBER(td->td_sigmask, sig)) {
3164 ksi.ksi_flags |= KSI_HEAD;
3165 sigqueue_add(&p->p_sigqueue, sig, &ksi);
3166 return (SIGSTATUS_HANDLED);
3170 * If the traced bit got turned off, requeue the signal and
3171 * reload the set of pending signals. This ensures that p_sig*
3172 * and p_sigact are consistent.
3174 if ((p->p_flag & P_TRACED) == 0) {
3175 if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3176 ksi.ksi_flags |= KSI_HEAD;
3177 sigqueue_add(queue, sig, &ksi);
3179 return (SIGSTATUS_HANDLED);
3184 * Decide whether the signal should be returned.
3185 * Return the signal's number, or fall through
3186 * to clear it from the pending mask.
3188 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3189 case (intptr_t)SIG_DFL:
3191 * Don't take default actions on system processes.
3193 if (p->p_pid <= 1) {
3196 * Are you sure you want to ignore SIGSEGV
3199 printf("Process (pid %lu) got signal %d\n",
3200 (u_long)p->p_pid, sig);
3202 return (SIGSTATUS_IGNORE);
3206 * If there is a pending stop signal to process with
3207 * default action, stop here, then clear the signal.
3208 * Traced or exiting processes should ignore stops.
3209 * Additionally, a member of an orphaned process group
3210 * should ignore tty stops.
3212 prop = sigprop(sig);
3213 if (prop & SIGPROP_STOP) {
3214 mtx_unlock(&ps->ps_mtx);
3215 if ((p->p_flag & (P_TRACED | P_WEXIT |
3216 P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3217 pg_flags & PGRP_ORPHANED) != 0 &&
3218 (prop & SIGPROP_TTYSTOP) != 0)) {
3219 mtx_lock(&ps->ps_mtx);
3220 return (SIGSTATUS_IGNORE);
3222 if (TD_SBDRY_INTR(td)) {
3223 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3224 ("lost TDF_SBDRY"));
3225 mtx_lock(&ps->ps_mtx);
3226 return (SIGSTATUS_SBDRY_STOP);
3228 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3229 &p->p_mtx.lock_object, "Catching SIGSTOP");
3230 sigqueue_delete(&td->td_sigqueue, sig);
3231 sigqueue_delete(&p->p_sigqueue, sig);
3232 p->p_flag |= P_STOPPED_SIG;
3235 sig_suspend_threads(td, p);
3236 thread_suspend_switch(td, p);
3238 mtx_lock(&ps->ps_mtx);
3239 return (SIGSTATUS_HANDLED);
3240 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3241 (td->td_flags & TDF_SIGWAIT) == 0) {
3243 * Default action is to ignore; drop it if
3244 * not in kern_sigtimedwait().
3246 return (SIGSTATUS_IGNORE);
3248 return (SIGSTATUS_HANDLE);
3251 case (intptr_t)SIG_IGN:
3252 if ((td->td_flags & TDF_SIGWAIT) == 0)
3253 return (SIGSTATUS_IGNORE);
3255 return (SIGSTATUS_HANDLE);
3259 * This signal has an action, let postsig() process it.
3261 return (SIGSTATUS_HANDLE);
3266 * If the current process has received a signal (should be caught or cause
3267 * termination, should interrupt current syscall), return the signal number.
3268 * Stop signals with default action are processed immediately, then cleared;
3269 * they aren't returned. This is checked after each entry to the system for
3270 * a syscall or trap (though this can usually be done without calling
3271 * issignal by checking the pending signal masks in cursig.) The normal call
3274 * while (sig = cursig(curthread))
3278 issignal(struct thread *td)
3281 sigset_t sigpending;
3285 PROC_LOCK_ASSERT(p, MA_OWNED);
3288 sigpending = td->td_sigqueue.sq_signals;
3289 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3290 SIGSETNAND(sigpending, td->td_sigmask);
3292 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3293 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3294 SIG_STOPSIGMASK(sigpending);
3295 if (SIGISEMPTY(sigpending)) /* no signal to send */
3299 * Do fast sigblock if requested by usermode. Since
3300 * we do know that there was a signal pending at this
3301 * point, set the FAST_SIGBLOCK_PEND as indicator for
3302 * usermode to perform a dummy call to
3303 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3304 * delivery of postponed pending signal.
3306 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3307 if (td->td_sigblock_val != 0)
3308 SIGSETNAND(sigpending, fastblock_mask);
3309 if (SIGISEMPTY(sigpending)) {
3310 td->td_pflags |= TDP_SIGFASTPENDING;
3315 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3316 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3317 SIGISMEMBER(sigpending, SIGSTOP)) {
3319 * If debugger just attached, always consume
3320 * SIGSTOP from ptrace(PT_ATTACH) first, to
3321 * execute the debugger attach ritual in
3324 td->td_dbgflags |= TDB_FSTP;
3325 SIGEMPTYSET(sigpending);
3326 SIGADDSET(sigpending, SIGSTOP);
3329 SIG_FOREACH(sig, &sigpending) {
3330 switch (sigprocess(td, sig)) {
3331 case SIGSTATUS_HANDLE:
3333 case SIGSTATUS_HANDLED:
3335 case SIGSTATUS_IGNORE:
3336 sigqueue_delete(&td->td_sigqueue, sig);
3337 sigqueue_delete(&p->p_sigqueue, sig);
3339 case SIGSTATUS_SBDRY_STOP:
3348 thread_stopped(struct proc *p)
3352 PROC_LOCK_ASSERT(p, MA_OWNED);
3353 PROC_SLOCK_ASSERT(p, MA_OWNED);
3357 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3359 p->p_flag &= ~P_WAITED;
3360 PROC_LOCK(p->p_pptr);
3361 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3362 CLD_TRAPPED : CLD_STOPPED);
3363 PROC_UNLOCK(p->p_pptr);
3369 * Take the action for the specified signal
3370 * from the current set of pending signals.
3380 sigset_t returnmask;
3382 KASSERT(sig != 0, ("postsig"));
3386 PROC_LOCK_ASSERT(p, MA_OWNED);
3388 mtx_assert(&ps->ps_mtx, MA_OWNED);
3389 ksiginfo_init(&ksi);
3390 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3391 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3393 ksi.ksi_signo = sig;
3394 if (ksi.ksi_code == SI_TIMER)
3395 itimer_accept(p, ksi.ksi_timerid, &ksi);
3396 action = ps->ps_sigact[_SIG_IDX(sig)];
3398 if (KTRPOINT(td, KTR_PSIG))
3399 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3400 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3403 if (action == SIG_DFL) {
3405 * Default action, where the default is to kill
3406 * the process. (Other cases were ignored above.)
3408 mtx_unlock(&ps->ps_mtx);
3409 proc_td_siginfo_capture(td, &ksi.ksi_info);
3414 * If we get here, the signal must be caught.
3416 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3417 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3418 ("postsig action: blocked sig %d", sig));
3421 * Set the new mask value and also defer further
3422 * occurrences of this signal.
3424 * Special case: user has done a sigsuspend. Here the
3425 * current mask is not of interest, but rather the
3426 * mask from before the sigsuspend is what we want
3427 * restored after the signal processing is completed.
3429 if (td->td_pflags & TDP_OLDMASK) {
3430 returnmask = td->td_oldsigmask;
3431 td->td_pflags &= ~TDP_OLDMASK;
3433 returnmask = td->td_sigmask;
3435 if (p->p_sig == sig) {
3438 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3439 postsig_done(sig, td, ps);
3445 sig_ast_checksusp(struct thread *td)
3447 struct proc *p __diagused;
3451 PROC_LOCK_ASSERT(p, MA_OWNED);
3453 if (!td_ast_pending(td, TDA_SUSPEND))
3456 ret = thread_suspend_check(1);
3457 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3462 sig_ast_needsigchk(struct thread *td)
3469 PROC_LOCK_ASSERT(p, MA_OWNED);
3471 if (!td_ast_pending(td, TDA_SIG))
3475 mtx_lock(&ps->ps_mtx);
3478 mtx_unlock(&ps->ps_mtx);
3479 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3480 KASSERT(TD_SBDRY_INTR(td),
3481 ("lost TDF_SERESTART of TDF_SEINTR"));
3482 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3483 (TDF_SEINTR | TDF_SERESTART),
3484 ("both TDF_SEINTR and TDF_SERESTART"));
3485 ret = TD_SBDRY_ERRNO(td);
3486 } else if (sig != 0) {
3487 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3488 mtx_unlock(&ps->ps_mtx);
3490 mtx_unlock(&ps->ps_mtx);
3495 * Do not go into sleep if this thread was the ptrace(2)
3496 * attach leader. cursig() consumed SIGSTOP from PT_ATTACH,
3497 * but we usually act on the signal by interrupting sleep, and
3498 * should do that here as well.
3500 if ((td->td_dbgflags & TDB_FSTP) != 0) {
3503 td->td_dbgflags &= ~TDB_FSTP;
3517 if (!td_ast_pending(td, TDA_SIG) && !td_ast_pending(td, TDA_SUSPEND))
3523 ret = sig_ast_checksusp(td);
3525 ret = sig_ast_needsigchk(td);
3531 curproc_sigkilled(void)
3539 if (!td_ast_pending(td, TDA_SIG))
3545 mtx_lock(&ps->ps_mtx);
3546 res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3547 SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3548 mtx_unlock(&ps->ps_mtx);
3554 proc_wkilled(struct proc *p)
3557 PROC_LOCK_ASSERT(p, MA_OWNED);
3558 if ((p->p_flag & P_WKILLED) == 0) {
3559 p->p_flag |= P_WKILLED;
3561 * Notify swapper that there is a process to swap in.
3562 * The notification is racy, at worst it would take 10
3563 * seconds for the swapper process to notice.
3565 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3571 * Kill the current process for stated reason.
3574 killproc(struct proc *p, const char *why)
3577 PROC_LOCK_ASSERT(p, MA_OWNED);
3578 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3580 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3581 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3582 p->p_ucred->cr_uid, why);
3584 kern_psignal(p, SIGKILL);
3588 * Force the current process to exit with the specified signal, dumping core
3589 * if appropriate. We bypass the normal tests for masked and caught signals,
3590 * allowing unrecoverable failures to terminate the process without changing
3591 * signal state. Mark the accounting record with the signal termination.
3592 * If dumping core, save the signal number for the debugger. Calls exit and
3596 sigexit(struct thread *td, int sig)
3598 struct proc *p = td->td_proc;
3599 const char *coreinfo;
3602 PROC_LOCK_ASSERT(p, MA_OWNED);
3603 proc_set_p2_wexit(p);
3605 p->p_acflag |= AXSIG;
3607 * We must be single-threading to generate a core dump. This
3608 * ensures that the registers in the core file are up-to-date.
3609 * Also, the ELF dump handler assumes that the thread list doesn't
3610 * change out from under it.
3612 * XXX If another thread attempts to single-thread before us
3613 * (e.g. via fork()), we won't get a dump at all.
3615 if ((sigprop(sig) & SIGPROP_CORE) &&
3616 thread_single(p, SINGLE_NO_EXIT) == 0) {
3619 * Log signals which would cause core dumps
3620 * (Log as LOG_INFO to appease those who don't want
3622 * XXX : Todo, as well as euid, write out ruid too
3623 * Note that coredump() drops proc lock.
3629 coreinfo = " (core dumped)";
3632 coreinfo = " (no core dump - bad address)";
3635 coreinfo = " (no core dump - invalid argument)";
3638 coreinfo = " (no core dump - too large)";
3641 coreinfo = " (no core dump - other error)";
3644 if (kern_logsigexit)
3646 "pid %d (%s), jid %d, uid %d: exited on "
3647 "signal %d%s\n", p->p_pid, p->p_comm,
3648 p->p_ucred->cr_prison->pr_id,
3649 td->td_ucred->cr_uid,
3650 sig &~ WCOREFLAG, coreinfo);
3658 * Send queued SIGCHLD to parent when child process's state
3662 sigparent(struct proc *p, int reason, int status)
3664 PROC_LOCK_ASSERT(p, MA_OWNED);
3665 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3667 if (p->p_ksi != NULL) {
3668 p->p_ksi->ksi_signo = SIGCHLD;
3669 p->p_ksi->ksi_code = reason;
3670 p->p_ksi->ksi_status = status;
3671 p->p_ksi->ksi_pid = p->p_pid;
3672 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3673 if (KSI_ONQ(p->p_ksi))
3676 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3680 childproc_jobstate(struct proc *p, int reason, int sig)
3684 PROC_LOCK_ASSERT(p, MA_OWNED);
3685 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3688 * Wake up parent sleeping in kern_wait(), also send
3689 * SIGCHLD to parent, but SIGCHLD does not guarantee
3690 * that parent will awake, because parent may masked
3693 p->p_pptr->p_flag |= P_STATCHILD;
3696 ps = p->p_pptr->p_sigacts;
3697 mtx_lock(&ps->ps_mtx);
3698 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3699 mtx_unlock(&ps->ps_mtx);
3700 sigparent(p, reason, sig);
3702 mtx_unlock(&ps->ps_mtx);
3706 childproc_stopped(struct proc *p, int reason)
3709 childproc_jobstate(p, reason, p->p_xsig);
3713 childproc_continued(struct proc *p)
3715 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3719 childproc_exited(struct proc *p)
3723 if (WCOREDUMP(p->p_xsig)) {
3724 reason = CLD_DUMPED;
3725 status = WTERMSIG(p->p_xsig);
3726 } else if (WIFSIGNALED(p->p_xsig)) {
3727 reason = CLD_KILLED;
3728 status = WTERMSIG(p->p_xsig);
3730 reason = CLD_EXITED;
3731 status = p->p_xexit;
3734 * XXX avoid calling wakeup(p->p_pptr), the work is
3737 sigparent(p, reason, status);
3740 #define MAX_NUM_CORE_FILES 100000
3741 #ifndef NUM_CORE_FILES
3742 #define NUM_CORE_FILES 5
3744 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3745 static int num_cores = NUM_CORE_FILES;
3748 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3753 new_val = num_cores;
3754 error = sysctl_handle_int(oidp, &new_val, 0, req);
3755 if (error != 0 || req->newptr == NULL)
3757 if (new_val > MAX_NUM_CORE_FILES)
3758 new_val = MAX_NUM_CORE_FILES;
3761 num_cores = new_val;
3764 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3765 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3766 sysctl_debug_num_cores_check, "I",
3767 "Maximum number of generated process corefiles while using index format");
3769 #define GZIP_SUFFIX ".gz"
3770 #define ZSTD_SUFFIX ".zst"
3772 int compress_user_cores = 0;
3775 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3779 val = compress_user_cores;
3780 error = sysctl_handle_int(oidp, &val, 0, req);
3781 if (error != 0 || req->newptr == NULL)
3783 if (val != 0 && !compressor_avail(val))
3785 compress_user_cores = val;
3788 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3789 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3790 sysctl_compress_user_cores, "I",
3791 "Enable compression of user corefiles ("
3792 __XSTRING(COMPRESS_GZIP) " = gzip, "
3793 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3795 int compress_user_cores_level = 6;
3796 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3797 &compress_user_cores_level, 0,
3798 "Corefile compression level");
3801 * Protect the access to corefilename[] by allproc_lock.
3803 #define corefilename_lock allproc_lock
3805 static char corefilename[MAXPATHLEN] = {"%N.core"};
3806 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3809 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3813 sx_xlock(&corefilename_lock);
3814 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3816 sx_xunlock(&corefilename_lock);
3820 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3821 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3822 "Process corefile name format string");
3825 vnode_close_locked(struct thread *td, struct vnode *vp)
3829 vn_close(vp, FWRITE, td->td_ucred, td);
3833 * If the core format has a %I in it, then we need to check
3834 * for existing corefiles before defining a name.
3835 * To do this we iterate over 0..ncores to find a
3836 * non-existing core file name to use. If all core files are
3837 * already used we choose the oldest one.
3840 corefile_open_last(struct thread *td, char *name, int indexpos,
3841 int indexlen, int ncores, struct vnode **vpp)
3843 struct vnode *oldvp, *nextvp, *vp;
3845 struct nameidata nd;
3846 int error, i, flags, oflags, cmode;
3848 struct timespec lasttime;
3850 nextvp = oldvp = NULL;
3851 cmode = S_IRUSR | S_IWUSR;
3852 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3853 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3855 for (i = 0; i < ncores; i++) {
3856 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3858 ch = name[indexpos + indexlen];
3859 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3861 name[indexpos + indexlen] = ch;
3863 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
3864 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3871 if ((flags & O_CREAT) == O_CREAT) {
3876 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3878 vnode_close_locked(td, vp);
3882 if (oldvp == NULL ||
3883 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3884 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3885 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3887 vn_close(oldvp, FWRITE, td->td_ucred, td);
3890 lasttime = vattr.va_mtime;
3892 vnode_close_locked(td, vp);
3896 if (oldvp != NULL) {
3897 if (nextvp == NULL) {
3898 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3900 vn_close(oldvp, FWRITE, td->td_ucred, td);
3903 error = vn_lock(nextvp, LK_EXCLUSIVE);
3905 vn_close(nextvp, FWRITE, td->td_ucred,
3911 vn_close(oldvp, FWRITE, td->td_ucred, td);
3916 vnode_close_locked(td, oldvp);
3925 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3926 * Expand the name described in corefilename, using name, uid, and pid
3927 * and open/create core file.
3928 * corefilename is a printf-like string, with three format specifiers:
3929 * %N name of process ("name")
3930 * %P process id (pid)
3932 * For example, "%N.core" is the default; they can be disabled completely
3933 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3934 * This is controlled by the sysctl variable kern.corefile (see above).
3937 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3938 int compress, int signum, struct vnode **vpp, char **namep)
3941 struct nameidata nd;
3943 char *hostname, *name;
3944 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3947 format = corefilename;
3948 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3952 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3953 sx_slock(&corefilename_lock);
3954 for (i = 0; format[i] != '\0'; i++) {
3955 switch (format[i]) {
3956 case '%': /* Format character */
3958 switch (format[i]) {
3960 sbuf_putc(&sb, '%');
3962 case 'H': /* hostname */
3963 if (hostname == NULL) {
3964 hostname = malloc(MAXHOSTNAMELEN,
3967 getcredhostname(td->td_ucred, hostname,
3969 sbuf_printf(&sb, "%s", hostname);
3971 case 'I': /* autoincrementing index */
3972 if (indexpos != -1) {
3973 sbuf_printf(&sb, "%%I");
3977 indexpos = sbuf_len(&sb);
3978 sbuf_printf(&sb, "%u", ncores - 1);
3979 indexlen = sbuf_len(&sb) - indexpos;
3981 case 'N': /* process name */
3982 sbuf_printf(&sb, "%s", comm);
3984 case 'P': /* process id */
3985 sbuf_printf(&sb, "%u", pid);
3987 case 'S': /* signal number */
3988 sbuf_printf(&sb, "%i", signum);
3990 case 'U': /* user id */
3991 sbuf_printf(&sb, "%u", uid);
3995 "Unknown format character %c in "
3996 "corename `%s'\n", format[i], format);
4001 sbuf_putc(&sb, format[i]);
4005 sx_sunlock(&corefilename_lock);
4006 free(hostname, M_TEMP);
4007 if (compress == COMPRESS_GZIP)
4008 sbuf_printf(&sb, GZIP_SUFFIX);
4009 else if (compress == COMPRESS_ZSTD)
4010 sbuf_printf(&sb, ZSTD_SUFFIX);
4011 if (sbuf_error(&sb) != 0) {
4012 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
4013 "long\n", (long)pid, comm, (u_long)uid);
4021 if (indexpos != -1) {
4022 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
4026 "pid %d (%s), uid (%u): Path `%s' failed "
4027 "on initial open test, error = %d\n",
4028 pid, comm, uid, name, error);
4031 cmode = S_IRUSR | S_IWUSR;
4032 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
4033 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
4034 flags = O_CREAT | FWRITE | O_NOFOLLOW;
4035 if ((td->td_proc->p_flag & P_SUGID) != 0)
4038 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name);
4039 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
4049 audit_proc_coredump(td, name, error);
4059 * Dump a process' core. The main routine does some
4060 * policy checking, and creates the name of the coredump;
4061 * then it passes on a vnode and a size limit to the process-specific
4062 * coredump routine if there is one; if there _is not_ one, it returns
4063 * ENOSYS; otherwise it returns the error from the process-specific routine.
4067 coredump(struct thread *td)
4069 struct proc *p = td->td_proc;
4070 struct ucred *cred = td->td_ucred;
4074 size_t fullpathsize;
4075 int error, error1, locked;
4076 char *name; /* name of corefile */
4079 char *fullpath, *freepath = NULL;
4082 PROC_LOCK_ASSERT(p, MA_OWNED);
4083 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
4085 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
4086 (p->p_flag2 & P2_NOTRACE) != 0) {
4092 * Note that the bulk of limit checking is done after
4093 * the corefile is created. The exception is if the limit
4094 * for corefiles is 0, in which case we don't bother
4095 * creating the corefile at all. This layout means that
4096 * a corefile is truncated instead of not being created,
4097 * if it is larger than the limit.
4099 limit = (off_t)lim_cur(td, RLIMIT_CORE);
4100 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
4106 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
4107 compress_user_cores, p->p_sig, &vp, &name);
4112 * Don't dump to non-regular files or files with links.
4113 * Do not dump into system files. Effective user must own the corefile.
4115 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
4116 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
4117 vattr.va_uid != cred->cr_uid) {
4125 /* Postpone other writers, including core dumps of other processes. */
4126 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4128 lf.l_whence = SEEK_SET;
4131 lf.l_type = F_WRLCK;
4132 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
4136 if (set_core_nodump_flag)
4137 vattr.va_flags = UF_NODUMP;
4138 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4139 VOP_SETATTR(vp, &vattr, cred);
4142 p->p_acflag |= ACORE;
4145 if (p->p_sysent->sv_coredump != NULL) {
4146 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
4152 lf.l_type = F_UNLCK;
4153 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4155 vn_rangelock_unlock(vp, rl_cookie);
4158 * Notify the userland helper that a process triggered a core dump.
4159 * This allows the helper to run an automated debugging session.
4161 if (error != 0 || coredump_devctl == 0)
4163 sb = sbuf_new_auto();
4164 if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4166 sbuf_printf(sb, "comm=\"");
4167 devctl_safe_quote_sb(sb, fullpath);
4168 free(freepath, M_TEMP);
4169 sbuf_printf(sb, "\" core=\"");
4172 * We can't lookup core file vp directly. When we're replacing a core, and
4173 * other random times, we flush the name cache, so it will fail. Instead,
4174 * if the path of the core is relative, add the current dir in front if it.
4176 if (name[0] != '/') {
4177 fullpathsize = MAXPATHLEN;
4178 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4179 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
4180 free(freepath, M_TEMP);
4183 devctl_safe_quote_sb(sb, fullpath);
4184 free(freepath, M_TEMP);
4187 devctl_safe_quote_sb(sb, name);
4188 sbuf_printf(sb, "\"");
4189 if (sbuf_finish(sb) == 0)
4190 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4194 error1 = vn_close(vp, FWRITE, cred, td);
4198 audit_proc_coredump(td, name, error);
4205 * Nonexistent system call-- signal process (may want to handle it). Flag
4206 * error in case process won't see signal immediately (blocked or ignored).
4208 #ifndef _SYS_SYSPROTO_H_
4215 nosys(struct thread *td, struct nosys_args *args)
4222 tdsignal(td, SIGSYS);
4224 if (kern_lognosys == 1 || kern_lognosys == 3) {
4225 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4228 if (kern_lognosys == 2 || kern_lognosys == 3 ||
4229 (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4230 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4237 * Send a SIGIO or SIGURG signal to a process or process group using stored
4238 * credentials rather than those of the current process.
4241 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4244 struct sigio *sigio;
4246 ksiginfo_init(&ksi);
4247 ksi.ksi_signo = sig;
4248 ksi.ksi_code = SI_KERNEL;
4252 if (sigio == NULL) {
4256 if (sigio->sio_pgid > 0) {
4257 PROC_LOCK(sigio->sio_proc);
4258 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4259 kern_psignal(sigio->sio_proc, sig);
4260 PROC_UNLOCK(sigio->sio_proc);
4261 } else if (sigio->sio_pgid < 0) {
4264 PGRP_LOCK(sigio->sio_pgrp);
4265 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4267 if (p->p_state == PRS_NORMAL &&
4268 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4269 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4270 kern_psignal(p, sig);
4273 PGRP_UNLOCK(sigio->sio_pgrp);
4279 filt_sigattach(struct knote *kn)
4281 struct proc *p = curproc;
4283 kn->kn_ptr.p_proc = p;
4284 kn->kn_flags |= EV_CLEAR; /* automatically set */
4286 knlist_add(p->p_klist, kn, 0);
4292 filt_sigdetach(struct knote *kn)
4294 struct proc *p = kn->kn_ptr.p_proc;
4296 knlist_remove(p->p_klist, kn, 0);
4300 * signal knotes are shared with proc knotes, so we apply a mask to
4301 * the hint in order to differentiate them from process hints. This
4302 * could be avoided by using a signal-specific knote list, but probably
4303 * isn't worth the trouble.
4306 filt_signal(struct knote *kn, long hint)
4309 if (hint & NOTE_SIGNAL) {
4310 hint &= ~NOTE_SIGNAL;
4312 if (kn->kn_id == hint)
4315 return (kn->kn_data != 0);
4323 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4324 refcount_init(&ps->ps_refcnt, 1);
4325 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4330 sigacts_free(struct sigacts *ps)
4333 if (refcount_release(&ps->ps_refcnt) == 0)
4335 mtx_destroy(&ps->ps_mtx);
4336 free(ps, M_SUBPROC);
4340 sigacts_hold(struct sigacts *ps)
4343 refcount_acquire(&ps->ps_refcnt);
4348 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4351 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4352 mtx_lock(&src->ps_mtx);
4353 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4354 mtx_unlock(&src->ps_mtx);
4358 sigacts_shared(struct sigacts *ps)
4361 return (ps->ps_refcnt > 1);
4365 sig_drop_caught(struct proc *p)
4371 PROC_LOCK_ASSERT(p, MA_OWNED);
4372 mtx_assert(&ps->ps_mtx, MA_OWNED);
4373 SIG_FOREACH(sig, &ps->ps_sigcatch) {
4375 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4376 sigqueue_delete_proc(p, sig);
4381 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4386 * Prevent further fetches and SIGSEGVs, allowing thread to
4387 * issue syscalls despite corruption.
4389 sigfastblock_clear(td);
4393 ksiginfo_init_trap(&ksi);
4394 ksi.ksi_signo = SIGSEGV;
4395 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4396 ksi.ksi_addr = td->td_sigblock_ptr;
4397 trapsignal(td, &ksi);
4401 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4405 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4407 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4408 sigfastblock_failed(td, sendsig, false);
4412 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4417 sigfastblock_resched(struct thread *td, bool resched)
4424 reschedule_signals(p, td->td_sigmask, 0);
4427 ast_sched(td, TDA_SIG);
4431 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4440 case SIGFASTBLOCK_SETPTR:
4441 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4445 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4449 td->td_pflags |= TDP_SIGFASTBLOCK;
4450 td->td_sigblock_ptr = uap->ptr;
4453 case SIGFASTBLOCK_UNBLOCK:
4454 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4460 res = casueword32(td->td_sigblock_ptr,
4461 SIGFASTBLOCK_PEND, &oldval, 0);
4464 sigfastblock_failed(td, false, true);
4470 if (oldval != SIGFASTBLOCK_PEND) {
4474 error = thread_check_susp(td, false);
4482 * td_sigblock_val is cleared there, but not on a
4483 * syscall exit. The end effect is that a single
4484 * interruptible sleep, while user sigblock word is
4485 * set, might return EINTR or ERESTART to usermode
4486 * without delivering signal. All further sleeps,
4487 * until userspace clears the word and does
4488 * sigfastblock(UNBLOCK), observe current word and no
4489 * longer get interrupted. It is slight
4490 * non-conformance, with alternative to have read the
4491 * sigblock word on each syscall entry.
4493 td->td_sigblock_val = 0;
4496 * Rely on normal ast mechanism to deliver pending
4497 * signals to current thread. But notify others about
4500 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4504 case SIGFASTBLOCK_UNSETPTR:
4505 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4509 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4513 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4517 sigfastblock_clear(td);
4528 sigfastblock_clear(struct thread *td)
4532 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4534 td->td_sigblock_val = 0;
4535 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4537 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4538 sigfastblock_resched(td, resched);
4542 sigfastblock_fetch(struct thread *td)
4546 (void)sigfastblock_fetch_sig(td, true, &val);
4550 sigfastblock_setpend1(struct thread *td)
4555 if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4557 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4559 sigfastblock_failed(td, true, false);
4563 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4564 oldval | SIGFASTBLOCK_PEND);
4566 sigfastblock_failed(td, true, true);
4570 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4571 td->td_pflags &= ~TDP_SIGFASTPENDING;
4575 if (thread_check_susp(td, false) != 0)
4581 sigfastblock_setpend(struct thread *td, bool resched)
4585 sigfastblock_setpend1(td);
4589 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);