2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/ctype.h>
46 #include <sys/systm.h>
47 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
51 #include <sys/capsicum.h>
52 #include <sys/compressor.h>
53 #include <sys/condvar.h>
54 #include <sys/event.h>
55 #include <sys/fcntl.h>
56 #include <sys/imgact.h>
57 #include <sys/kernel.h>
59 #include <sys/ktrace.h>
60 #include <sys/limits.h>
62 #include <sys/malloc.h>
63 #include <sys/mutex.h>
64 #include <sys/refcount.h>
65 #include <sys/namei.h>
67 #include <sys/procdesc.h>
68 #include <sys/ptrace.h>
69 #include <sys/posix4.h>
70 #include <sys/pioctl.h>
71 #include <sys/racct.h>
72 #include <sys/resourcevar.h>
75 #include <sys/sleepqueue.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
82 #include <sys/syslog.h>
83 #include <sys/sysproto.h>
84 #include <sys/timers.h>
85 #include <sys/unistd.h>
88 #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 int sigprop(int sig);
112 static void tdsigwakeup(struct thread *, int, sig_t, int);
113 static int sig_suspend_threads(struct thread *, struct proc *, int);
114 static int filt_sigattach(struct knote *kn);
115 static void filt_sigdetach(struct knote *kn);
116 static int filt_signal(struct knote *kn, long hint);
117 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
118 static void sigqueue_start(void);
120 static uma_zone_t ksiginfo_zone = NULL;
121 struct filterops sig_filtops = {
123 .f_attach = filt_sigattach,
124 .f_detach = filt_sigdetach,
125 .f_event = filt_signal,
128 static int kern_logsigexit = 1;
129 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
131 "Log processes quitting on abnormal signals to syslog(3)");
133 static int kern_forcesigexit = 1;
134 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
135 &kern_forcesigexit, 0, "Force trap signal to be handled");
137 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
138 "POSIX real time signal");
140 static int max_pending_per_proc = 128;
141 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
142 &max_pending_per_proc, 0, "Max pending signals per proc");
144 static int preallocate_siginfo = 1024;
145 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
146 &preallocate_siginfo, 0, "Preallocated signal memory size");
148 static int signal_overflow = 0;
149 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
150 &signal_overflow, 0, "Number of signals overflew");
152 static int signal_alloc_fail = 0;
153 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
154 &signal_alloc_fail, 0, "signals failed to be allocated");
156 static int kern_lognosys = 0;
157 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
158 "Log invalid syscalls");
160 __read_frequently bool sigfastblock_fetch_always = false;
161 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
162 &sigfastblock_fetch_always, 0,
163 "Fetch sigfastblock word on each syscall entry for proper "
164 "blocking semantic");
166 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
169 * Policy -- Can ucred cr1 send SIGIO to process cr2?
170 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
171 * in the right situations.
173 #define CANSIGIO(cr1, cr2) \
174 ((cr1)->cr_uid == 0 || \
175 (cr1)->cr_ruid == (cr2)->cr_ruid || \
176 (cr1)->cr_uid == (cr2)->cr_ruid || \
177 (cr1)->cr_ruid == (cr2)->cr_uid || \
178 (cr1)->cr_uid == (cr2)->cr_uid)
180 static int sugid_coredump;
181 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
182 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
184 static int capmode_coredump;
185 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
186 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
188 static int do_coredump = 1;
189 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
190 &do_coredump, 0, "Enable/Disable coredumps");
192 static int set_core_nodump_flag = 0;
193 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
194 0, "Enable setting the NODUMP flag on coredump files");
196 static int coredump_devctl = 0;
197 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
198 0, "Generate a devctl notification when processes coredump");
201 * Signal properties and actions.
202 * The array below categorizes the signals and their default actions
203 * according to the following properties:
205 #define SIGPROP_KILL 0x01 /* terminates process by default */
206 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
207 #define SIGPROP_STOP 0x04 /* suspend process */
208 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
209 #define SIGPROP_IGNORE 0x10 /* ignore by default */
210 #define SIGPROP_CONT 0x20 /* continue if suspended */
211 #define SIGPROP_CANTMASK 0x40 /* non-maskable, catchable */
213 static int sigproptbl[NSIG] = {
214 [SIGHUP] = SIGPROP_KILL,
215 [SIGINT] = SIGPROP_KILL,
216 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
217 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
218 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
219 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
220 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
221 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
222 [SIGKILL] = SIGPROP_KILL,
223 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
224 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
225 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
226 [SIGPIPE] = SIGPROP_KILL,
227 [SIGALRM] = SIGPROP_KILL,
228 [SIGTERM] = SIGPROP_KILL,
229 [SIGURG] = SIGPROP_IGNORE,
230 [SIGSTOP] = SIGPROP_STOP,
231 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
232 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
233 [SIGCHLD] = SIGPROP_IGNORE,
234 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
235 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
236 [SIGIO] = SIGPROP_IGNORE,
237 [SIGXCPU] = SIGPROP_KILL,
238 [SIGXFSZ] = SIGPROP_KILL,
239 [SIGVTALRM] = SIGPROP_KILL,
240 [SIGPROF] = SIGPROP_KILL,
241 [SIGWINCH] = SIGPROP_IGNORE,
242 [SIGINFO] = SIGPROP_IGNORE,
243 [SIGUSR1] = SIGPROP_KILL,
244 [SIGUSR2] = SIGPROP_KILL,
247 sigset_t fastblock_mask;
252 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
253 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
254 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
255 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
256 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
257 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
258 SIGFILLSET(fastblock_mask);
259 SIG_CANTMASK(fastblock_mask);
263 ksiginfo_alloc(int wait)
270 if (ksiginfo_zone != NULL)
271 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
276 ksiginfo_free(ksiginfo_t *ksi)
278 uma_zfree(ksiginfo_zone, ksi);
282 ksiginfo_tryfree(ksiginfo_t *ksi)
284 if (!(ksi->ksi_flags & KSI_EXT)) {
285 uma_zfree(ksiginfo_zone, ksi);
292 sigqueue_init(sigqueue_t *list, struct proc *p)
294 SIGEMPTYSET(list->sq_signals);
295 SIGEMPTYSET(list->sq_kill);
296 SIGEMPTYSET(list->sq_ptrace);
297 TAILQ_INIT(&list->sq_list);
299 list->sq_flags = SQ_INIT;
303 * Get a signal's ksiginfo.
305 * 0 - signal not found
306 * others - signal number
309 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
311 struct proc *p = sq->sq_proc;
312 struct ksiginfo *ksi, *next;
315 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
317 if (!SIGISMEMBER(sq->sq_signals, signo))
320 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
322 SIGDELSET(sq->sq_ptrace, signo);
323 si->ksi_flags |= KSI_PTRACE;
325 if (SIGISMEMBER(sq->sq_kill, signo)) {
328 SIGDELSET(sq->sq_kill, signo);
331 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
332 if (ksi->ksi_signo == signo) {
334 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
335 ksi->ksi_sigq = NULL;
336 ksiginfo_copy(ksi, si);
337 if (ksiginfo_tryfree(ksi) && p != NULL)
346 SIGDELSET(sq->sq_signals, signo);
347 si->ksi_signo = signo;
352 sigqueue_take(ksiginfo_t *ksi)
358 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
362 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
363 ksi->ksi_sigq = NULL;
364 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
367 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
368 kp = TAILQ_NEXT(kp, ksi_link)) {
369 if (kp->ksi_signo == ksi->ksi_signo)
372 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
373 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
374 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
378 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
380 struct proc *p = sq->sq_proc;
381 struct ksiginfo *ksi;
384 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
387 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
390 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
391 SIGADDSET(sq->sq_kill, signo);
395 /* directly insert the ksi, don't copy it */
396 if (si->ksi_flags & KSI_INS) {
397 if (si->ksi_flags & KSI_HEAD)
398 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
400 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
405 if (__predict_false(ksiginfo_zone == NULL)) {
406 SIGADDSET(sq->sq_kill, signo);
410 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
413 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
419 ksiginfo_copy(si, ksi);
420 ksi->ksi_signo = signo;
421 if (si->ksi_flags & KSI_HEAD)
422 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
424 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
429 if ((si->ksi_flags & KSI_PTRACE) != 0) {
430 SIGADDSET(sq->sq_ptrace, signo);
433 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
434 (si->ksi_flags & KSI_SIGQ) == 0) {
435 SIGADDSET(sq->sq_kill, signo);
443 SIGADDSET(sq->sq_signals, signo);
448 sigqueue_flush(sigqueue_t *sq)
450 struct proc *p = sq->sq_proc;
453 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
456 PROC_LOCK_ASSERT(p, MA_OWNED);
458 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
459 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
460 ksi->ksi_sigq = NULL;
461 if (ksiginfo_tryfree(ksi) && p != NULL)
465 SIGEMPTYSET(sq->sq_signals);
466 SIGEMPTYSET(sq->sq_kill);
467 SIGEMPTYSET(sq->sq_ptrace);
471 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
474 struct proc *p1, *p2;
475 ksiginfo_t *ksi, *next;
477 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
478 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
481 /* Move siginfo to target list */
482 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
483 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
484 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
487 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
494 /* Move pending bits to target list */
496 SIGSETAND(tmp, *set);
497 SIGSETOR(dst->sq_kill, tmp);
498 SIGSETNAND(src->sq_kill, tmp);
500 tmp = src->sq_ptrace;
501 SIGSETAND(tmp, *set);
502 SIGSETOR(dst->sq_ptrace, tmp);
503 SIGSETNAND(src->sq_ptrace, tmp);
505 tmp = src->sq_signals;
506 SIGSETAND(tmp, *set);
507 SIGSETOR(dst->sq_signals, tmp);
508 SIGSETNAND(src->sq_signals, tmp);
513 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
518 SIGADDSET(set, signo);
519 sigqueue_move_set(src, dst, &set);
524 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
526 struct proc *p = sq->sq_proc;
527 ksiginfo_t *ksi, *next;
529 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
531 /* Remove siginfo queue */
532 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
533 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
534 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
535 ksi->ksi_sigq = NULL;
536 if (ksiginfo_tryfree(ksi) && p != NULL)
540 SIGSETNAND(sq->sq_kill, *set);
541 SIGSETNAND(sq->sq_ptrace, *set);
542 SIGSETNAND(sq->sq_signals, *set);
546 sigqueue_delete(sigqueue_t *sq, int signo)
551 SIGADDSET(set, signo);
552 sigqueue_delete_set(sq, &set);
555 /* Remove a set of signals for a process */
557 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
562 PROC_LOCK_ASSERT(p, MA_OWNED);
564 sigqueue_init(&worklist, NULL);
565 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
567 FOREACH_THREAD_IN_PROC(p, td0)
568 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
570 sigqueue_flush(&worklist);
574 sigqueue_delete_proc(struct proc *p, int signo)
579 SIGADDSET(set, signo);
580 sigqueue_delete_set_proc(p, &set);
584 sigqueue_delete_stopmask_proc(struct proc *p)
589 SIGADDSET(set, SIGSTOP);
590 SIGADDSET(set, SIGTSTP);
591 SIGADDSET(set, SIGTTIN);
592 SIGADDSET(set, SIGTTOU);
593 sigqueue_delete_set_proc(p, &set);
597 * Determine signal that should be delivered to thread td, the current
598 * thread, 0 if none. If there is a pending stop signal with default
599 * action, the process stops in issignal().
602 cursig(struct thread *td)
604 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
605 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
606 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
607 return (SIGPENDING(td) ? issignal(td) : 0);
611 * Arrange for ast() to handle unmasked pending signals on return to user
612 * mode. This must be called whenever a signal is added to td_sigqueue or
613 * unmasked in td_sigmask.
616 signotify(struct thread *td)
619 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
621 if (SIGPENDING(td)) {
623 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
629 * Returns 1 (true) if altstack is configured for the thread, and the
630 * passed stack bottom address falls into the altstack range. Handles
631 * the 43 compat special case where the alt stack size is zero.
634 sigonstack(size_t sp)
639 if ((td->td_pflags & TDP_ALTSTACK) == 0)
641 #if defined(COMPAT_43)
642 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
643 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
645 return (sp >= (size_t)td->td_sigstk.ss_sp &&
646 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
653 if (sig > 0 && sig < nitems(sigproptbl))
654 return (sigproptbl[sig]);
659 sig_ffs(sigset_t *set)
663 for (i = 0; i < _SIG_WORDS; i++)
665 return (ffs(set->__bits[i]) + (i * 32));
670 sigact_flag_test(const struct sigaction *act, int flag)
674 * SA_SIGINFO is reset when signal disposition is set to
675 * ignore or default. Other flags are kept according to user
678 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
679 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
680 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
690 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
691 struct sigaction *oact, int flags)
694 struct proc *p = td->td_proc;
696 if (!_SIG_VALID(sig))
698 if (act != NULL && act->sa_handler != SIG_DFL &&
699 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
700 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
701 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
706 mtx_lock(&ps->ps_mtx);
708 memset(oact, 0, sizeof(*oact));
709 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
710 if (SIGISMEMBER(ps->ps_sigonstack, sig))
711 oact->sa_flags |= SA_ONSTACK;
712 if (!SIGISMEMBER(ps->ps_sigintr, sig))
713 oact->sa_flags |= SA_RESTART;
714 if (SIGISMEMBER(ps->ps_sigreset, sig))
715 oact->sa_flags |= SA_RESETHAND;
716 if (SIGISMEMBER(ps->ps_signodefer, sig))
717 oact->sa_flags |= SA_NODEFER;
718 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
719 oact->sa_flags |= SA_SIGINFO;
721 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
723 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
724 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
725 oact->sa_flags |= SA_NOCLDSTOP;
726 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
727 oact->sa_flags |= SA_NOCLDWAIT;
730 if ((sig == SIGKILL || sig == SIGSTOP) &&
731 act->sa_handler != SIG_DFL) {
732 mtx_unlock(&ps->ps_mtx);
738 * Change setting atomically.
741 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
742 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
743 if (sigact_flag_test(act, SA_SIGINFO)) {
744 ps->ps_sigact[_SIG_IDX(sig)] =
745 (__sighandler_t *)act->sa_sigaction;
746 SIGADDSET(ps->ps_siginfo, sig);
748 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
749 SIGDELSET(ps->ps_siginfo, sig);
751 if (!sigact_flag_test(act, SA_RESTART))
752 SIGADDSET(ps->ps_sigintr, sig);
754 SIGDELSET(ps->ps_sigintr, sig);
755 if (sigact_flag_test(act, SA_ONSTACK))
756 SIGADDSET(ps->ps_sigonstack, sig);
758 SIGDELSET(ps->ps_sigonstack, sig);
759 if (sigact_flag_test(act, SA_RESETHAND))
760 SIGADDSET(ps->ps_sigreset, sig);
762 SIGDELSET(ps->ps_sigreset, sig);
763 if (sigact_flag_test(act, SA_NODEFER))
764 SIGADDSET(ps->ps_signodefer, sig);
766 SIGDELSET(ps->ps_signodefer, sig);
767 if (sig == SIGCHLD) {
768 if (act->sa_flags & SA_NOCLDSTOP)
769 ps->ps_flag |= PS_NOCLDSTOP;
771 ps->ps_flag &= ~PS_NOCLDSTOP;
772 if (act->sa_flags & SA_NOCLDWAIT) {
774 * Paranoia: since SA_NOCLDWAIT is implemented
775 * by reparenting the dying child to PID 1 (and
776 * trust it to reap the zombie), PID 1 itself
777 * is forbidden to set SA_NOCLDWAIT.
780 ps->ps_flag &= ~PS_NOCLDWAIT;
782 ps->ps_flag |= PS_NOCLDWAIT;
784 ps->ps_flag &= ~PS_NOCLDWAIT;
785 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
786 ps->ps_flag |= PS_CLDSIGIGN;
788 ps->ps_flag &= ~PS_CLDSIGIGN;
791 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
792 * and for signals set to SIG_DFL where the default is to
793 * ignore. However, don't put SIGCONT in ps_sigignore, as we
794 * have to restart the process.
796 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
797 (sigprop(sig) & SIGPROP_IGNORE &&
798 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
799 /* never to be seen again */
800 sigqueue_delete_proc(p, sig);
802 /* easier in psignal */
803 SIGADDSET(ps->ps_sigignore, sig);
804 SIGDELSET(ps->ps_sigcatch, sig);
806 SIGDELSET(ps->ps_sigignore, sig);
807 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
808 SIGDELSET(ps->ps_sigcatch, sig);
810 SIGADDSET(ps->ps_sigcatch, sig);
812 #ifdef COMPAT_FREEBSD4
813 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
814 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
815 (flags & KSA_FREEBSD4) == 0)
816 SIGDELSET(ps->ps_freebsd4, sig);
818 SIGADDSET(ps->ps_freebsd4, sig);
821 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
822 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
823 (flags & KSA_OSIGSET) == 0)
824 SIGDELSET(ps->ps_osigset, sig);
826 SIGADDSET(ps->ps_osigset, sig);
829 mtx_unlock(&ps->ps_mtx);
834 #ifndef _SYS_SYSPROTO_H_
835 struct sigaction_args {
837 struct sigaction *act;
838 struct sigaction *oact;
842 sys_sigaction(struct thread *td, struct sigaction_args *uap)
844 struct sigaction act, oact;
845 struct sigaction *actp, *oactp;
848 actp = (uap->act != NULL) ? &act : NULL;
849 oactp = (uap->oact != NULL) ? &oact : NULL;
851 error = copyin(uap->act, actp, sizeof(act));
855 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
857 error = copyout(oactp, uap->oact, sizeof(oact));
861 #ifdef COMPAT_FREEBSD4
862 #ifndef _SYS_SYSPROTO_H_
863 struct freebsd4_sigaction_args {
865 struct sigaction *act;
866 struct sigaction *oact;
870 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
872 struct sigaction act, oact;
873 struct sigaction *actp, *oactp;
876 actp = (uap->act != NULL) ? &act : NULL;
877 oactp = (uap->oact != NULL) ? &oact : NULL;
879 error = copyin(uap->act, actp, sizeof(act));
883 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
885 error = copyout(oactp, uap->oact, sizeof(oact));
888 #endif /* COMAPT_FREEBSD4 */
890 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
891 #ifndef _SYS_SYSPROTO_H_
892 struct osigaction_args {
894 struct osigaction *nsa;
895 struct osigaction *osa;
899 osigaction(struct thread *td, struct osigaction_args *uap)
901 struct osigaction sa;
902 struct sigaction nsa, osa;
903 struct sigaction *nsap, *osap;
906 if (uap->signum <= 0 || uap->signum >= ONSIG)
909 nsap = (uap->nsa != NULL) ? &nsa : NULL;
910 osap = (uap->osa != NULL) ? &osa : NULL;
913 error = copyin(uap->nsa, &sa, sizeof(sa));
916 nsap->sa_handler = sa.sa_handler;
917 nsap->sa_flags = sa.sa_flags;
918 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
920 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
921 if (osap && !error) {
922 sa.sa_handler = osap->sa_handler;
923 sa.sa_flags = osap->sa_flags;
924 SIG2OSIG(osap->sa_mask, sa.sa_mask);
925 error = copyout(&sa, uap->osa, sizeof(sa));
930 #if !defined(__i386__)
931 /* Avoid replicating the same stub everywhere */
933 osigreturn(struct thread *td, struct osigreturn_args *uap)
936 return (nosys(td, (struct nosys_args *)uap));
939 #endif /* COMPAT_43 */
942 * Initialize signal state for process 0;
943 * set to ignore signals that are ignored by default.
946 siginit(struct proc *p)
953 mtx_lock(&ps->ps_mtx);
954 for (i = 1; i <= NSIG; i++) {
955 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
956 SIGADDSET(ps->ps_sigignore, i);
959 mtx_unlock(&ps->ps_mtx);
964 * Reset specified signal to the default disposition.
967 sigdflt(struct sigacts *ps, int sig)
970 mtx_assert(&ps->ps_mtx, MA_OWNED);
971 SIGDELSET(ps->ps_sigcatch, sig);
972 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
973 SIGADDSET(ps->ps_sigignore, sig);
974 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
975 SIGDELSET(ps->ps_siginfo, sig);
979 * Reset signals for an exec of the specified process.
982 execsigs(struct proc *p)
990 * Reset caught signals. Held signals remain held
991 * through td_sigmask (unless they were caught,
992 * and are now ignored by default).
994 PROC_LOCK_ASSERT(p, MA_OWNED);
996 mtx_lock(&ps->ps_mtx);
1000 * As CloudABI processes cannot modify signal handlers, fully
1001 * reset all signals to their default behavior. Do ignore
1002 * SIGPIPE, as it would otherwise be impossible to recover from
1003 * writes to broken pipes and sockets.
1005 if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) {
1006 osigignore = ps->ps_sigignore;
1007 while (SIGNOTEMPTY(osigignore)) {
1008 sig = sig_ffs(&osigignore);
1009 SIGDELSET(osigignore, sig);
1013 SIGADDSET(ps->ps_sigignore, SIGPIPE);
1017 * Reset stack state to the user stack.
1018 * Clear set of signals caught on the signal stack.
1021 MPASS(td->td_proc == p);
1022 td->td_sigstk.ss_flags = SS_DISABLE;
1023 td->td_sigstk.ss_size = 0;
1024 td->td_sigstk.ss_sp = 0;
1025 td->td_pflags &= ~TDP_ALTSTACK;
1027 * Reset no zombies if child dies flag as Solaris does.
1029 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1030 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1031 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1032 mtx_unlock(&ps->ps_mtx);
1036 * kern_sigprocmask()
1038 * Manipulate signal mask.
1041 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1044 sigset_t new_block, oset1;
1049 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1050 PROC_LOCK_ASSERT(p, MA_OWNED);
1053 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1054 ? MA_OWNED : MA_NOTOWNED);
1056 *oset = td->td_sigmask;
1063 oset1 = td->td_sigmask;
1064 SIGSETOR(td->td_sigmask, *set);
1065 new_block = td->td_sigmask;
1066 SIGSETNAND(new_block, oset1);
1069 SIGSETNAND(td->td_sigmask, *set);
1074 oset1 = td->td_sigmask;
1075 if (flags & SIGPROCMASK_OLD)
1076 SIGSETLO(td->td_sigmask, *set);
1078 td->td_sigmask = *set;
1079 new_block = td->td_sigmask;
1080 SIGSETNAND(new_block, oset1);
1089 * The new_block set contains signals that were not previously
1090 * blocked, but are blocked now.
1092 * In case we block any signal that was not previously blocked
1093 * for td, and process has the signal pending, try to schedule
1094 * signal delivery to some thread that does not block the
1095 * signal, possibly waking it up.
1097 if (p->p_numthreads != 1)
1098 reschedule_signals(p, new_block, flags);
1102 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1107 #ifndef _SYS_SYSPROTO_H_
1108 struct sigprocmask_args {
1110 const sigset_t *set;
1115 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1118 sigset_t *setp, *osetp;
1121 setp = (uap->set != NULL) ? &set : NULL;
1122 osetp = (uap->oset != NULL) ? &oset : NULL;
1124 error = copyin(uap->set, setp, sizeof(set));
1128 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1129 if (osetp && !error) {
1130 error = copyout(osetp, uap->oset, sizeof(oset));
1135 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1136 #ifndef _SYS_SYSPROTO_H_
1137 struct osigprocmask_args {
1143 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1148 OSIG2SIG(uap->mask, set);
1149 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1150 SIG2OSIG(oset, td->td_retval[0]);
1153 #endif /* COMPAT_43 */
1156 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1162 error = copyin(uap->set, &set, sizeof(set));
1164 td->td_retval[0] = error;
1168 error = kern_sigtimedwait(td, set, &ksi, NULL);
1170 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1172 if (error == ERESTART)
1174 td->td_retval[0] = error;
1178 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1179 td->td_retval[0] = error;
1184 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1187 struct timespec *timeout;
1193 error = copyin(uap->timeout, &ts, sizeof(ts));
1201 error = copyin(uap->set, &set, sizeof(set));
1205 error = kern_sigtimedwait(td, set, &ksi, timeout);
1210 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1213 td->td_retval[0] = ksi.ksi_signo;
1218 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1224 error = copyin(uap->set, &set, sizeof(set));
1228 error = kern_sigtimedwait(td, set, &ksi, NULL);
1233 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1236 td->td_retval[0] = ksi.ksi_signo;
1241 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1245 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1249 thr->td_si.si_signo = 0;
1254 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1255 struct timespec *timeout)
1258 sigset_t saved_mask, new_block;
1260 int error, sig, timo, timevalid = 0;
1261 struct timespec rts, ets, ts;
1271 if (timeout != NULL) {
1272 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1274 getnanouptime(&rts);
1275 timespecadd(&rts, timeout, &ets);
1279 /* Some signals can not be waited for. */
1280 SIG_CANTMASK(waitset);
1283 saved_mask = td->td_sigmask;
1284 SIGSETNAND(td->td_sigmask, waitset);
1286 mtx_lock(&ps->ps_mtx);
1288 mtx_unlock(&ps->ps_mtx);
1289 KASSERT(sig >= 0, ("sig %d", sig));
1290 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1291 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1292 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1302 * POSIX says this must be checked after looking for pending
1305 if (timeout != NULL) {
1310 getnanouptime(&rts);
1311 if (timespeccmp(&rts, &ets, >=)) {
1315 timespecsub(&ets, &rts, &ts);
1316 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1327 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1329 if (timeout != NULL) {
1330 if (error == ERESTART) {
1331 /* Timeout can not be restarted. */
1333 } else if (error == EAGAIN) {
1334 /* We will calculate timeout by ourself. */
1340 * If PTRACE_SCE or PTRACE_SCX were set after
1341 * userspace entered the syscall, return spurious
1342 * EINTR after wait was done. Only do this as last
1343 * resort after rechecking for possible queued signals
1344 * and expired timeouts.
1346 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1350 new_block = saved_mask;
1351 SIGSETNAND(new_block, td->td_sigmask);
1352 td->td_sigmask = saved_mask;
1354 * Fewer signals can be delivered to us, reschedule signal
1357 if (p->p_numthreads != 1)
1358 reschedule_signals(p, new_block, 0);
1361 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1363 if (ksi->ksi_code == SI_TIMER)
1364 itimer_accept(p, ksi->ksi_timerid, ksi);
1367 if (KTRPOINT(td, KTR_PSIG)) {
1370 mtx_lock(&ps->ps_mtx);
1371 action = ps->ps_sigact[_SIG_IDX(sig)];
1372 mtx_unlock(&ps->ps_mtx);
1373 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1376 if (sig == SIGKILL) {
1377 proc_td_siginfo_capture(td, &ksi->ksi_info);
1385 #ifndef _SYS_SYSPROTO_H_
1386 struct sigpending_args {
1391 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1393 struct proc *p = td->td_proc;
1397 pending = p->p_sigqueue.sq_signals;
1398 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1400 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1403 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1404 #ifndef _SYS_SYSPROTO_H_
1405 struct osigpending_args {
1410 osigpending(struct thread *td, struct osigpending_args *uap)
1412 struct proc *p = td->td_proc;
1416 pending = p->p_sigqueue.sq_signals;
1417 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1419 SIG2OSIG(pending, td->td_retval[0]);
1422 #endif /* COMPAT_43 */
1424 #if defined(COMPAT_43)
1426 * Generalized interface signal handler, 4.3-compatible.
1428 #ifndef _SYS_SYSPROTO_H_
1429 struct osigvec_args {
1437 osigvec(struct thread *td, struct osigvec_args *uap)
1440 struct sigaction nsa, osa;
1441 struct sigaction *nsap, *osap;
1444 if (uap->signum <= 0 || uap->signum >= ONSIG)
1446 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1447 osap = (uap->osv != NULL) ? &osa : NULL;
1449 error = copyin(uap->nsv, &vec, sizeof(vec));
1452 nsap->sa_handler = vec.sv_handler;
1453 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1454 nsap->sa_flags = vec.sv_flags;
1455 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1457 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1458 if (osap && !error) {
1459 vec.sv_handler = osap->sa_handler;
1460 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1461 vec.sv_flags = osap->sa_flags;
1462 vec.sv_flags &= ~SA_NOCLDWAIT;
1463 vec.sv_flags ^= SA_RESTART;
1464 error = copyout(&vec, uap->osv, sizeof(vec));
1469 #ifndef _SYS_SYSPROTO_H_
1470 struct osigblock_args {
1475 osigblock(struct thread *td, struct osigblock_args *uap)
1479 OSIG2SIG(uap->mask, set);
1480 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1481 SIG2OSIG(oset, td->td_retval[0]);
1485 #ifndef _SYS_SYSPROTO_H_
1486 struct osigsetmask_args {
1491 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1495 OSIG2SIG(uap->mask, set);
1496 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1497 SIG2OSIG(oset, td->td_retval[0]);
1500 #endif /* COMPAT_43 */
1503 * Suspend calling thread until signal, providing mask to be set in the
1506 #ifndef _SYS_SYSPROTO_H_
1507 struct sigsuspend_args {
1508 const sigset_t *sigmask;
1513 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1518 error = copyin(uap->sigmask, &mask, sizeof(mask));
1521 return (kern_sigsuspend(td, mask));
1525 kern_sigsuspend(struct thread *td, sigset_t mask)
1527 struct proc *p = td->td_proc;
1531 * When returning from sigsuspend, we want
1532 * the old mask to be restored after the
1533 * signal handler has finished. Thus, we
1534 * save it here and mark the sigacts structure
1538 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1539 SIGPROCMASK_PROC_LOCKED);
1540 td->td_pflags |= TDP_OLDMASK;
1543 * Process signals now. Otherwise, we can get spurious wakeup
1544 * due to signal entered process queue, but delivered to other
1545 * thread. But sigsuspend should return only on signal
1548 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1549 for (has_sig = 0; !has_sig;) {
1550 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1553 thread_suspend_check(0);
1554 mtx_lock(&p->p_sigacts->ps_mtx);
1555 while ((sig = cursig(td)) != 0) {
1556 KASSERT(sig >= 0, ("sig %d", sig));
1557 has_sig += postsig(sig);
1559 mtx_unlock(&p->p_sigacts->ps_mtx);
1562 * If PTRACE_SCE or PTRACE_SCX were set after
1563 * userspace entered the syscall, return spurious
1566 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1570 td->td_errno = EINTR;
1571 td->td_pflags |= TDP_NERRNO;
1572 return (EJUSTRETURN);
1575 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1577 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1578 * convention: libc stub passes mask, not pointer, to save a copyin.
1580 #ifndef _SYS_SYSPROTO_H_
1581 struct osigsuspend_args {
1587 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1591 OSIG2SIG(uap->mask, mask);
1592 return (kern_sigsuspend(td, mask));
1594 #endif /* COMPAT_43 */
1596 #if defined(COMPAT_43)
1597 #ifndef _SYS_SYSPROTO_H_
1598 struct osigstack_args {
1599 struct sigstack *nss;
1600 struct sigstack *oss;
1605 osigstack(struct thread *td, struct osigstack_args *uap)
1607 struct sigstack nss, oss;
1610 if (uap->nss != NULL) {
1611 error = copyin(uap->nss, &nss, sizeof(nss));
1615 oss.ss_sp = td->td_sigstk.ss_sp;
1616 oss.ss_onstack = sigonstack(cpu_getstack(td));
1617 if (uap->nss != NULL) {
1618 td->td_sigstk.ss_sp = nss.ss_sp;
1619 td->td_sigstk.ss_size = 0;
1620 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1621 td->td_pflags |= TDP_ALTSTACK;
1623 if (uap->oss != NULL)
1624 error = copyout(&oss, uap->oss, sizeof(oss));
1628 #endif /* COMPAT_43 */
1630 #ifndef _SYS_SYSPROTO_H_
1631 struct sigaltstack_args {
1638 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1643 if (uap->ss != NULL) {
1644 error = copyin(uap->ss, &ss, sizeof(ss));
1648 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1649 (uap->oss != NULL) ? &oss : NULL);
1652 if (uap->oss != NULL)
1653 error = copyout(&oss, uap->oss, sizeof(stack_t));
1658 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1660 struct proc *p = td->td_proc;
1663 oonstack = sigonstack(cpu_getstack(td));
1666 *oss = td->td_sigstk;
1667 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1668 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1674 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1676 if (!(ss->ss_flags & SS_DISABLE)) {
1677 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1680 td->td_sigstk = *ss;
1681 td->td_pflags |= TDP_ALTSTACK;
1683 td->td_pflags &= ~TDP_ALTSTACK;
1689 struct killpg1_ctx {
1699 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1703 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1704 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1707 err = p_cansignal(arg->td, p, arg->sig);
1708 if (err == 0 && arg->sig != 0)
1709 pksignal(p, arg->sig, arg->ksi);
1715 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1720 * Common code for kill process group/broadcast kill.
1721 * cp is calling process.
1724 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1728 struct killpg1_ctx arg;
1740 sx_slock(&allproc_lock);
1741 FOREACH_PROC_IN_SYSTEM(p) {
1742 killpg1_sendsig(p, true, &arg);
1744 sx_sunlock(&allproc_lock);
1746 sx_slock(&proctree_lock);
1749 * zero pgid means send to my process group.
1751 pgrp = td->td_proc->p_pgrp;
1754 pgrp = pgfind(pgid);
1756 sx_sunlock(&proctree_lock);
1760 sx_sunlock(&proctree_lock);
1761 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1762 killpg1_sendsig(p, false, &arg);
1766 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1767 if (arg.ret == 0 && !arg.sent)
1768 arg.ret = arg.found ? EPERM : ESRCH;
1772 #ifndef _SYS_SYSPROTO_H_
1780 sys_kill(struct thread *td, struct kill_args *uap)
1783 return (kern_kill(td, uap->pid, uap->signum));
1787 kern_kill(struct thread *td, pid_t pid, int signum)
1794 * A process in capability mode can send signals only to himself.
1795 * The main rationale behind this is that abort(3) is implemented as
1796 * kill(getpid(), SIGABRT).
1798 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1801 AUDIT_ARG_SIGNUM(signum);
1803 if ((u_int)signum > _SIG_MAXSIG)
1806 ksiginfo_init(&ksi);
1807 ksi.ksi_signo = signum;
1808 ksi.ksi_code = SI_USER;
1809 ksi.ksi_pid = td->td_proc->p_pid;
1810 ksi.ksi_uid = td->td_ucred->cr_ruid;
1813 /* kill single process */
1814 if ((p = pfind_any(pid)) == NULL)
1816 AUDIT_ARG_PROCESS(p);
1817 error = p_cansignal(td, p, signum);
1818 if (error == 0 && signum)
1819 pksignal(p, signum, &ksi);
1824 case -1: /* broadcast signal */
1825 return (killpg1(td, signum, 0, 1, &ksi));
1826 case 0: /* signal own process group */
1827 return (killpg1(td, signum, 0, 0, &ksi));
1828 default: /* negative explicit process group */
1829 return (killpg1(td, signum, -pid, 0, &ksi));
1835 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1840 AUDIT_ARG_SIGNUM(uap->signum);
1841 AUDIT_ARG_FD(uap->fd);
1842 if ((u_int)uap->signum > _SIG_MAXSIG)
1845 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1848 AUDIT_ARG_PROCESS(p);
1849 error = p_cansignal(td, p, uap->signum);
1850 if (error == 0 && uap->signum)
1851 kern_psignal(p, uap->signum);
1856 #if defined(COMPAT_43)
1857 #ifndef _SYS_SYSPROTO_H_
1858 struct okillpg_args {
1865 okillpg(struct thread *td, struct okillpg_args *uap)
1869 AUDIT_ARG_SIGNUM(uap->signum);
1870 AUDIT_ARG_PID(uap->pgid);
1871 if ((u_int)uap->signum > _SIG_MAXSIG)
1874 ksiginfo_init(&ksi);
1875 ksi.ksi_signo = uap->signum;
1876 ksi.ksi_code = SI_USER;
1877 ksi.ksi_pid = td->td_proc->p_pid;
1878 ksi.ksi_uid = td->td_ucred->cr_ruid;
1879 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1881 #endif /* COMPAT_43 */
1883 #ifndef _SYS_SYSPROTO_H_
1884 struct sigqueue_args {
1887 /* union sigval */ void *value;
1891 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1895 sv.sival_ptr = uap->value;
1897 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1901 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1907 if ((u_int)signum > _SIG_MAXSIG)
1911 * Specification says sigqueue can only send signal to
1917 if ((p = pfind_any(pid)) == NULL)
1919 error = p_cansignal(td, p, signum);
1920 if (error == 0 && signum != 0) {
1921 ksiginfo_init(&ksi);
1922 ksi.ksi_flags = KSI_SIGQ;
1923 ksi.ksi_signo = signum;
1924 ksi.ksi_code = SI_QUEUE;
1925 ksi.ksi_pid = td->td_proc->p_pid;
1926 ksi.ksi_uid = td->td_ucred->cr_ruid;
1927 ksi.ksi_value = *value;
1928 error = pksignal(p, ksi.ksi_signo, &ksi);
1935 * Send a signal to a process group.
1938 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1943 sx_slock(&proctree_lock);
1944 pgrp = pgfind(pgid);
1945 sx_sunlock(&proctree_lock);
1947 pgsignal(pgrp, sig, 0, ksi);
1954 * Send a signal to a process group. If checktty is 1,
1955 * limit to members which have a controlling terminal.
1958 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1963 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1964 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1966 if (p->p_state == PRS_NORMAL &&
1967 (checkctty == 0 || p->p_flag & P_CONTROLT))
1968 pksignal(p, sig, ksi);
1975 * Recalculate the signal mask and reset the signal disposition after
1976 * usermode frame for delivery is formed. Should be called after
1977 * mach-specific routine, because sysent->sv_sendsig() needs correct
1978 * ps_siginfo and signal mask.
1981 postsig_done(int sig, struct thread *td, struct sigacts *ps)
1985 mtx_assert(&ps->ps_mtx, MA_OWNED);
1986 td->td_ru.ru_nsignals++;
1987 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1988 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1989 SIGADDSET(mask, sig);
1990 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1991 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1992 if (SIGISMEMBER(ps->ps_sigreset, sig))
1997 * Send a signal caused by a trap to the current thread. If it will be
1998 * caught immediately, deliver it with correct code. Otherwise, post it
2002 trapsignal(struct thread *td, ksiginfo_t *ksi)
2010 sig = ksi->ksi_signo;
2011 code = ksi->ksi_code;
2012 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2014 sigfastblock_fetch(td);
2017 mtx_lock(&ps->ps_mtx);
2018 sigmask = td->td_sigmask;
2019 if (td->td_sigblock_val != 0)
2020 SIGSETOR(sigmask, fastblock_mask);
2021 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2022 !SIGISMEMBER(sigmask, sig)) {
2024 if (KTRPOINT(curthread, KTR_PSIG))
2025 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2026 &td->td_sigmask, code);
2028 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2029 ksi, &td->td_sigmask);
2030 postsig_done(sig, td, ps);
2031 mtx_unlock(&ps->ps_mtx);
2034 * Avoid a possible infinite loop if the thread
2035 * masking the signal or process is ignoring the
2038 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2039 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2040 SIGDELSET(td->td_sigmask, sig);
2041 SIGDELSET(ps->ps_sigcatch, sig);
2042 SIGDELSET(ps->ps_sigignore, sig);
2043 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2044 td->td_pflags &= ~TDP_SIGFASTBLOCK;
2045 td->td_sigblock_val = 0;
2047 mtx_unlock(&ps->ps_mtx);
2048 p->p_sig = sig; /* XXX to verify code */
2049 tdsendsignal(p, td, sig, ksi);
2054 static struct thread *
2055 sigtd(struct proc *p, int sig, bool fast_sigblock)
2057 struct thread *td, *signal_td;
2059 PROC_LOCK_ASSERT(p, MA_OWNED);
2060 MPASS(!fast_sigblock || p == curproc);
2063 * Check if current thread can handle the signal without
2064 * switching context to another thread.
2066 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2067 (!fast_sigblock || curthread->td_sigblock_val == 0))
2070 FOREACH_THREAD_IN_PROC(p, td) {
2071 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2072 td != curthread || td->td_sigblock_val == 0)) {
2077 if (signal_td == NULL)
2078 signal_td = FIRST_THREAD_IN_PROC(p);
2083 * Send the signal to the process. If the signal has an action, the action
2084 * is usually performed by the target process rather than the caller; we add
2085 * the signal to the set of pending signals for the process.
2088 * o When a stop signal is sent to a sleeping process that takes the
2089 * default action, the process is stopped without awakening it.
2090 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2091 * regardless of the signal action (eg, blocked or ignored).
2093 * Other ignored signals are discarded immediately.
2095 * NB: This function may be entered from the debugger via the "kill" DDB
2096 * command. There is little that can be done to mitigate the possibly messy
2097 * side effects of this unwise possibility.
2100 kern_psignal(struct proc *p, int sig)
2104 ksiginfo_init(&ksi);
2105 ksi.ksi_signo = sig;
2106 ksi.ksi_code = SI_KERNEL;
2107 (void) tdsendsignal(p, NULL, sig, &ksi);
2111 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2114 return (tdsendsignal(p, NULL, sig, ksi));
2117 /* Utility function for finding a thread to send signal event to. */
2119 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
2123 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2124 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2136 tdsignal(struct thread *td, int sig)
2140 ksiginfo_init(&ksi);
2141 ksi.ksi_signo = sig;
2142 ksi.ksi_code = SI_KERNEL;
2143 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2147 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2150 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2154 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2157 sigqueue_t *sigqueue;
2164 MPASS(td == NULL || p == td->td_proc);
2165 PROC_LOCK_ASSERT(p, MA_OWNED);
2167 if (!_SIG_VALID(sig))
2168 panic("%s(): invalid signal %d", __func__, sig);
2170 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2173 * IEEE Std 1003.1-2001: return success when killing a zombie.
2175 if (p->p_state == PRS_ZOMBIE) {
2176 if (ksi && (ksi->ksi_flags & KSI_INS))
2177 ksiginfo_tryfree(ksi);
2182 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2183 prop = sigprop(sig);
2186 td = sigtd(p, sig, false);
2187 sigqueue = &p->p_sigqueue;
2189 sigqueue = &td->td_sigqueue;
2191 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2194 * If the signal is being ignored,
2195 * then we forget about it immediately.
2196 * (Note: we don't set SIGCONT in ps_sigignore,
2197 * and if it is set to SIG_IGN,
2198 * action will be SIG_DFL here.)
2200 mtx_lock(&ps->ps_mtx);
2201 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2202 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2204 mtx_unlock(&ps->ps_mtx);
2205 if (ksi && (ksi->ksi_flags & KSI_INS))
2206 ksiginfo_tryfree(ksi);
2209 if (SIGISMEMBER(td->td_sigmask, sig))
2211 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2215 if (SIGISMEMBER(ps->ps_sigintr, sig))
2219 mtx_unlock(&ps->ps_mtx);
2221 if (prop & SIGPROP_CONT)
2222 sigqueue_delete_stopmask_proc(p);
2223 else if (prop & SIGPROP_STOP) {
2225 * If sending a tty stop signal to a member of an orphaned
2226 * process group, discard the signal here if the action
2227 * is default; don't stop the process below if sleeping,
2228 * and don't clear any pending SIGCONT.
2230 if ((prop & SIGPROP_TTYSTOP) &&
2231 (p->p_pgrp->pg_jobc == 0) &&
2232 (action == SIG_DFL)) {
2233 if (ksi && (ksi->ksi_flags & KSI_INS))
2234 ksiginfo_tryfree(ksi);
2237 sigqueue_delete_proc(p, SIGCONT);
2238 if (p->p_flag & P_CONTINUED) {
2239 p->p_flag &= ~P_CONTINUED;
2240 PROC_LOCK(p->p_pptr);
2241 sigqueue_take(p->p_ksi);
2242 PROC_UNLOCK(p->p_pptr);
2246 ret = sigqueue_add(sigqueue, sig, ksi);
2251 * Defer further processing for signals which are held,
2252 * except that stopped processes must be continued by SIGCONT.
2254 if (action == SIG_HOLD &&
2255 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2258 /* SIGKILL: Remove procfs STOPEVENTs. */
2259 if (sig == SIGKILL) {
2260 /* from procfs_ioctl.c: PIOCBIC */
2262 /* from procfs_ioctl.c: PIOCCONT */
2269 * Some signals have a process-wide effect and a per-thread
2270 * component. Most processing occurs when the process next
2271 * tries to cross the user boundary, however there are some
2272 * times when processing needs to be done immediately, such as
2273 * waking up threads so that they can cross the user boundary.
2274 * We try to do the per-process part here.
2276 if (P_SHOULDSTOP(p)) {
2277 KASSERT(!(p->p_flag & P_WEXIT),
2278 ("signal to stopped but exiting process"));
2279 if (sig == SIGKILL) {
2281 * If traced process is already stopped,
2282 * then no further action is necessary.
2284 if (p->p_flag & P_TRACED)
2287 * SIGKILL sets process running.
2288 * It will die elsewhere.
2289 * All threads must be restarted.
2291 p->p_flag &= ~P_STOPPED_SIG;
2295 if (prop & SIGPROP_CONT) {
2297 * If traced process is already stopped,
2298 * then no further action is necessary.
2300 if (p->p_flag & P_TRACED)
2303 * If SIGCONT is default (or ignored), we continue the
2304 * process but don't leave the signal in sigqueue as
2305 * it has no further action. If SIGCONT is held, we
2306 * continue the process and leave the signal in
2307 * sigqueue. If the process catches SIGCONT, let it
2308 * handle the signal itself. If it isn't waiting on
2309 * an event, it goes back to run state.
2310 * Otherwise, process goes back to sleep state.
2312 p->p_flag &= ~P_STOPPED_SIG;
2314 if (p->p_numthreads == p->p_suspcount) {
2316 p->p_flag |= P_CONTINUED;
2317 p->p_xsig = SIGCONT;
2318 PROC_LOCK(p->p_pptr);
2319 childproc_continued(p);
2320 PROC_UNLOCK(p->p_pptr);
2323 if (action == SIG_DFL) {
2324 thread_unsuspend(p);
2326 sigqueue_delete(sigqueue, sig);
2329 if (action == SIG_CATCH) {
2331 * The process wants to catch it so it needs
2332 * to run at least one thread, but which one?
2338 * The signal is not ignored or caught.
2340 thread_unsuspend(p);
2345 if (prop & SIGPROP_STOP) {
2347 * If traced process is already stopped,
2348 * then no further action is necessary.
2350 if (p->p_flag & P_TRACED)
2353 * Already stopped, don't need to stop again
2354 * (If we did the shell could get confused).
2355 * Just make sure the signal STOP bit set.
2357 p->p_flag |= P_STOPPED_SIG;
2358 sigqueue_delete(sigqueue, sig);
2363 * All other kinds of signals:
2364 * If a thread is sleeping interruptibly, simulate a
2365 * wakeup so that when it is continued it will be made
2366 * runnable and can look at the signal. However, don't make
2367 * the PROCESS runnable, leave it stopped.
2368 * It may run a bit until it hits a thread_suspend_check().
2372 if (TD_CAN_ABORT(td))
2373 wakeup_swapper = sleepq_abort(td, intrval);
2379 * Mutexes are short lived. Threads waiting on them will
2380 * hit thread_suspend_check() soon.
2382 } else if (p->p_state == PRS_NORMAL) {
2383 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2384 tdsigwakeup(td, sig, action, intrval);
2388 MPASS(action == SIG_DFL);
2390 if (prop & SIGPROP_STOP) {
2391 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2393 p->p_flag |= P_STOPPED_SIG;
2396 wakeup_swapper = sig_suspend_threads(td, p, 1);
2397 if (p->p_numthreads == p->p_suspcount) {
2399 * only thread sending signal to another
2400 * process can reach here, if thread is sending
2401 * signal to its process, because thread does
2402 * not suspend itself here, p_numthreads
2403 * should never be equal to p_suspcount.
2407 sigqueue_delete_proc(p, p->p_xsig);
2413 /* Not in "NORMAL" state. discard the signal. */
2414 sigqueue_delete(sigqueue, sig);
2419 * The process is not stopped so we need to apply the signal to all the
2423 tdsigwakeup(td, sig, action, intrval);
2425 thread_unsuspend(p);
2428 /* If we jump here, proc slock should not be owned. */
2429 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2437 * The force of a signal has been directed against a single
2438 * thread. We need to see what we can do about knocking it
2439 * out of any sleep it may be in etc.
2442 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2444 struct proc *p = td->td_proc;
2445 int prop, wakeup_swapper;
2447 PROC_LOCK_ASSERT(p, MA_OWNED);
2448 prop = sigprop(sig);
2453 * Bring the priority of a thread up if we want it to get
2454 * killed in this lifetime. Be careful to avoid bumping the
2455 * priority of the idle thread, since we still allow to signal
2458 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2459 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2460 sched_prio(td, PUSER);
2461 if (TD_ON_SLEEPQ(td)) {
2463 * If thread is sleeping uninterruptibly
2464 * we can't interrupt the sleep... the signal will
2465 * be noticed when the process returns through
2466 * trap() or syscall().
2468 if ((td->td_flags & TDF_SINTR) == 0)
2471 * If SIGCONT is default (or ignored) and process is
2472 * asleep, we are finished; the process should not
2475 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2478 sigqueue_delete(&p->p_sigqueue, sig);
2480 * It may be on either list in this state.
2481 * Remove from both for now.
2483 sigqueue_delete(&td->td_sigqueue, sig);
2488 * Don't awaken a sleeping thread for SIGSTOP if the
2489 * STOP signal is deferred.
2491 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2492 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2496 * Give low priority threads a better chance to run.
2498 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2499 sched_prio(td, PUSER);
2501 wakeup_swapper = sleepq_abort(td, intrval);
2509 * Other states do nothing with the signal immediately,
2510 * other than kicking ourselves if we are running.
2511 * It will either never be noticed, or noticed very soon.
2514 if (TD_IS_RUNNING(td) && td != curthread)
2524 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2529 PROC_LOCK_ASSERT(p, MA_OWNED);
2530 PROC_SLOCK_ASSERT(p, MA_OWNED);
2531 MPASS(sending || td == curthread);
2534 FOREACH_THREAD_IN_PROC(p, td2) {
2536 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2537 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2538 (td2->td_flags & TDF_SINTR)) {
2539 if (td2->td_flags & TDF_SBDRY) {
2541 * Once a thread is asleep with
2542 * TDF_SBDRY and without TDF_SERESTART
2543 * or TDF_SEINTR set, it should never
2544 * become suspended due to this check.
2546 KASSERT(!TD_IS_SUSPENDED(td2),
2547 ("thread with deferred stops suspended"));
2548 if (TD_SBDRY_INTR(td2)) {
2549 wakeup_swapper |= sleepq_abort(td2,
2550 TD_SBDRY_ERRNO(td2));
2553 } else if (!TD_IS_SUSPENDED(td2))
2554 thread_suspend_one(td2);
2555 } else if (!TD_IS_SUSPENDED(td2)) {
2556 if (sending || td != td2)
2557 td2->td_flags |= TDF_ASTPENDING;
2559 if (TD_IS_RUNNING(td2) && td2 != td)
2560 forward_signal(td2);
2565 return (wakeup_swapper);
2569 * Stop the process for an event deemed interesting to the debugger. If si is
2570 * non-NULL, this is a signal exchange; the new signal requested by the
2571 * debugger will be returned for handling. If si is NULL, this is some other
2572 * type of interesting event. The debugger may request a signal be delivered in
2573 * that case as well, however it will be deferred until it can be handled.
2576 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2578 struct proc *p = td->td_proc;
2582 PROC_LOCK_ASSERT(p, MA_OWNED);
2583 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2584 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2585 &p->p_mtx.lock_object, "Stopping for traced signal");
2589 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2590 td->td_dbgflags |= TDB_XSIG;
2591 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2592 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2594 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2597 * Ensure that, if we've been PT_KILLed, the
2598 * exit status reflects that. Another thread
2599 * may also be in ptracestop(), having just
2600 * received the SIGKILL, but this thread was
2601 * unsuspended first.
2603 td->td_dbgflags &= ~TDB_XSIG;
2604 td->td_xsig = SIGKILL;
2608 if (p->p_flag & P_SINGLE_EXIT &&
2609 !(td->td_dbgflags & TDB_EXIT)) {
2611 * Ignore ptrace stops except for thread exit
2612 * events when the process exits.
2614 td->td_dbgflags &= ~TDB_XSIG;
2620 * Make wait(2) work. Ensure that right after the
2621 * attach, the thread which was decided to become the
2622 * leader of attach gets reported to the waiter.
2623 * Otherwise, just avoid overwriting another thread's
2624 * assignment to p_xthread. If another thread has
2625 * already set p_xthread, the current thread will get
2626 * a chance to report itself upon the next iteration.
2628 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2629 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2630 p->p_xthread == NULL)) {
2635 * If we are on sleepqueue already,
2636 * let sleepqueue code decide if it
2637 * needs to go sleep after attach.
2639 if (td->td_wchan == NULL)
2640 td->td_dbgflags &= ~TDB_FSTP;
2642 p->p_flag2 &= ~P2_PTRACE_FSTP;
2643 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2644 sig_suspend_threads(td, p, 0);
2646 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2647 td->td_dbgflags &= ~TDB_STOPATFORK;
2650 thread_suspend_switch(td, p);
2651 if (p->p_xthread == td)
2652 p->p_xthread = NULL;
2653 if (!(p->p_flag & P_TRACED))
2655 if (td->td_dbgflags & TDB_SUSPEND) {
2656 if (p->p_flag & P_SINGLE_EXIT)
2664 if (si != NULL && sig == td->td_xsig) {
2665 /* Parent wants us to take the original signal unchanged. */
2666 si->ksi_flags |= KSI_HEAD;
2667 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2669 } else if (td->td_xsig != 0) {
2671 * If parent wants us to take a new signal, then it will leave
2672 * it in td->td_xsig; otherwise we just look for signals again.
2674 ksiginfo_init(&ksi);
2675 ksi.ksi_signo = td->td_xsig;
2676 ksi.ksi_flags |= KSI_PTRACE;
2677 td2 = sigtd(p, td->td_xsig, false);
2678 tdsendsignal(p, td2, td->td_xsig, &ksi);
2683 return (td->td_xsig);
2687 reschedule_signals(struct proc *p, sigset_t block, int flags)
2692 bool fastblk, pslocked;
2694 PROC_LOCK_ASSERT(p, MA_OWNED);
2696 pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2697 mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2698 if (SIGISEMPTY(p->p_siglist))
2700 SIGSETAND(block, p->p_siglist);
2701 fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2702 while ((sig = sig_ffs(&block)) != 0) {
2703 SIGDELSET(block, sig);
2704 td = sigtd(p, sig, fastblk);
2707 * If sigtd() selected us despite sigfastblock is
2708 * blocking, do not activate AST or wake us, to avoid
2709 * loop in AST handler.
2711 if (fastblk && td == curthread)
2716 mtx_lock(&ps->ps_mtx);
2717 if (p->p_flag & P_TRACED ||
2718 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2719 !SIGISMEMBER(td->td_sigmask, sig))) {
2720 tdsigwakeup(td, sig, SIG_CATCH,
2721 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2725 mtx_unlock(&ps->ps_mtx);
2730 tdsigcleanup(struct thread *td)
2736 PROC_LOCK_ASSERT(p, MA_OWNED);
2738 sigqueue_flush(&td->td_sigqueue);
2739 if (p->p_numthreads == 1)
2743 * Since we cannot handle signals, notify signal post code
2744 * about this by filling the sigmask.
2746 * Also, if needed, wake up thread(s) that do not block the
2747 * same signals as the exiting thread, since the thread might
2748 * have been selected for delivery and woken up.
2750 SIGFILLSET(unblocked);
2751 SIGSETNAND(unblocked, td->td_sigmask);
2752 SIGFILLSET(td->td_sigmask);
2753 reschedule_signals(p, unblocked, 0);
2758 sigdeferstop_curr_flags(int cflags)
2761 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
2762 (cflags & TDF_SBDRY) != 0);
2763 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2767 * Defer the delivery of SIGSTOP for the current thread, according to
2768 * the requested mode. Returns previous flags, which must be restored
2769 * by sigallowstop().
2771 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
2772 * cleared by the current thread, which allow the lock-less read-only
2776 sigdeferstop_impl(int mode)
2782 cflags = sigdeferstop_curr_flags(td->td_flags);
2784 case SIGDEFERSTOP_NOP:
2787 case SIGDEFERSTOP_OFF:
2790 case SIGDEFERSTOP_SILENT:
2791 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
2793 case SIGDEFERSTOP_EINTR:
2794 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
2796 case SIGDEFERSTOP_ERESTART:
2797 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
2800 panic("sigdeferstop: invalid mode %x", mode);
2803 if (cflags == nflags)
2804 return (SIGDEFERSTOP_VAL_NCHG);
2806 td->td_flags = (td->td_flags & ~cflags) | nflags;
2812 * Restores the STOP handling mode, typically permitting the delivery
2813 * of SIGSTOP for the current thread. This does not immediately
2814 * suspend if a stop was posted. Instead, the thread will suspend
2815 * either via ast() or a subsequent interruptible sleep.
2818 sigallowstop_impl(int prev)
2823 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
2824 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
2825 ("sigallowstop: incorrect previous mode %x", prev));
2827 cflags = sigdeferstop_curr_flags(td->td_flags);
2828 if (cflags != prev) {
2830 td->td_flags = (td->td_flags & ~cflags) | prev;
2836 * If the current process has received a signal (should be caught or cause
2837 * termination, should interrupt current syscall), return the signal number.
2838 * Stop signals with default action are processed immediately, then cleared;
2839 * they aren't returned. This is checked after each entry to the system for
2840 * a syscall or trap (though this can usually be done without calling issignal
2841 * by checking the pending signal masks in cursig.) The normal call
2844 * while (sig = cursig(curthread))
2848 issignal(struct thread *td)
2852 struct sigqueue *queue;
2853 sigset_t sigpending;
2855 int prop, sig, traced;
2859 mtx_assert(&ps->ps_mtx, MA_OWNED);
2860 PROC_LOCK_ASSERT(p, MA_OWNED);
2862 traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2864 sigpending = td->td_sigqueue.sq_signals;
2865 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2866 SIGSETNAND(sigpending, td->td_sigmask);
2868 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
2869 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2870 SIG_STOPSIGMASK(sigpending);
2871 if (SIGISEMPTY(sigpending)) /* no signal to send */
2875 * Do fast sigblock if requested by usermode. Since
2876 * we do know that there was a signal pending at this
2877 * point, set the FAST_SIGBLOCK_PEND as indicator for
2878 * usermode to perform a dummy call to
2879 * FAST_SIGBLOCK_UNBLOCK, which causes immediate
2880 * delivery of postponed pending signal.
2882 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
2883 if (td->td_sigblock_val != 0)
2884 SIGSETNAND(sigpending, fastblock_mask);
2885 if (SIGISEMPTY(sigpending)) {
2886 td->td_pflags |= TDP_SIGFASTPENDING;
2891 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
2892 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
2893 SIGISMEMBER(sigpending, SIGSTOP)) {
2895 * If debugger just attached, always consume
2896 * SIGSTOP from ptrace(PT_ATTACH) first, to
2897 * execute the debugger attach ritual in
2901 td->td_dbgflags |= TDB_FSTP;
2903 sig = sig_ffs(&sigpending);
2906 if (p->p_stops & S_SIG) {
2907 mtx_unlock(&ps->ps_mtx);
2908 stopevent(p, S_SIG, sig);
2909 mtx_lock(&ps->ps_mtx);
2913 * We should see pending but ignored signals
2914 * only if P_TRACED was on when they were posted.
2916 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2917 sigqueue_delete(&td->td_sigqueue, sig);
2918 sigqueue_delete(&p->p_sigqueue, sig);
2921 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
2923 * If traced, always stop.
2924 * Remove old signal from queue before the stop.
2925 * XXX shrug off debugger, it causes siginfo to
2928 queue = &td->td_sigqueue;
2929 ksiginfo_init(&ksi);
2930 if (sigqueue_get(queue, sig, &ksi) == 0) {
2931 queue = &p->p_sigqueue;
2932 sigqueue_get(queue, sig, &ksi);
2934 td->td_si = ksi.ksi_info;
2936 mtx_unlock(&ps->ps_mtx);
2937 sig = ptracestop(td, sig, &ksi);
2938 mtx_lock(&ps->ps_mtx);
2940 td->td_si.si_signo = 0;
2943 * Keep looking if the debugger discarded or
2944 * replaced the signal.
2950 * If the signal became masked, re-queue it.
2952 if (SIGISMEMBER(td->td_sigmask, sig)) {
2953 ksi.ksi_flags |= KSI_HEAD;
2954 sigqueue_add(&p->p_sigqueue, sig, &ksi);
2959 * If the traced bit got turned off, requeue
2960 * the signal and go back up to the top to
2961 * rescan signals. This ensures that p_sig*
2962 * and p_sigact are consistent.
2964 if ((p->p_flag & P_TRACED) == 0) {
2965 ksi.ksi_flags |= KSI_HEAD;
2966 sigqueue_add(queue, sig, &ksi);
2971 prop = sigprop(sig);
2974 * Decide whether the signal should be returned.
2975 * Return the signal's number, or fall through
2976 * to clear it from the pending mask.
2978 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2980 case (intptr_t)SIG_DFL:
2982 * Don't take default actions on system processes.
2984 if (p->p_pid <= 1) {
2987 * Are you sure you want to ignore SIGSEGV
2990 printf("Process (pid %lu) got signal %d\n",
2991 (u_long)p->p_pid, sig);
2993 break; /* == ignore */
2996 * If there is a pending stop signal to process with
2997 * default action, stop here, then clear the signal.
2998 * Traced or exiting processes should ignore stops.
2999 * Additionally, a member of an orphaned process group
3000 * should ignore tty stops.
3002 if (prop & SIGPROP_STOP) {
3004 (P_TRACED | P_WEXIT | P_SINGLE_EXIT) ||
3005 (p->p_pgrp->pg_jobc == 0 &&
3006 prop & SIGPROP_TTYSTOP))
3007 break; /* == ignore */
3008 if (TD_SBDRY_INTR(td)) {
3009 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3010 ("lost TDF_SBDRY"));
3013 mtx_unlock(&ps->ps_mtx);
3014 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3015 &p->p_mtx.lock_object, "Catching SIGSTOP");
3016 sigqueue_delete(&td->td_sigqueue, sig);
3017 sigqueue_delete(&p->p_sigqueue, sig);
3018 p->p_flag |= P_STOPPED_SIG;
3021 sig_suspend_threads(td, p, 0);
3022 thread_suspend_switch(td, p);
3024 mtx_lock(&ps->ps_mtx);
3026 } else if (prop & SIGPROP_IGNORE) {
3028 * Except for SIGCONT, shouldn't get here.
3029 * Default action is to ignore; drop it.
3031 break; /* == ignore */
3036 case (intptr_t)SIG_IGN:
3038 * Masking above should prevent us ever trying
3039 * to take action on an ignored signal other
3040 * than SIGCONT, unless process is traced.
3042 if ((prop & SIGPROP_CONT) == 0 &&
3043 (p->p_flag & P_TRACED) == 0)
3044 printf("issignal\n");
3045 break; /* == ignore */
3049 * This signal has an action, let
3050 * postsig() process it.
3054 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
3055 sigqueue_delete(&p->p_sigqueue, sig);
3062 thread_stopped(struct proc *p)
3066 PROC_LOCK_ASSERT(p, MA_OWNED);
3067 PROC_SLOCK_ASSERT(p, MA_OWNED);
3071 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3073 p->p_flag &= ~P_WAITED;
3074 PROC_LOCK(p->p_pptr);
3075 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3076 CLD_TRAPPED : CLD_STOPPED);
3077 PROC_UNLOCK(p->p_pptr);
3083 * Take the action for the specified signal
3084 * from the current set of pending signals.
3094 sigset_t returnmask;
3096 KASSERT(sig != 0, ("postsig"));
3100 PROC_LOCK_ASSERT(p, MA_OWNED);
3102 mtx_assert(&ps->ps_mtx, MA_OWNED);
3103 ksiginfo_init(&ksi);
3104 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3105 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3107 ksi.ksi_signo = sig;
3108 if (ksi.ksi_code == SI_TIMER)
3109 itimer_accept(p, ksi.ksi_timerid, &ksi);
3110 action = ps->ps_sigact[_SIG_IDX(sig)];
3112 if (KTRPOINT(td, KTR_PSIG))
3113 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3114 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3116 if ((p->p_stops & S_SIG) != 0) {
3117 mtx_unlock(&ps->ps_mtx);
3118 stopevent(p, S_SIG, sig);
3119 mtx_lock(&ps->ps_mtx);
3122 if (action == SIG_DFL) {
3124 * Default action, where the default is to kill
3125 * the process. (Other cases were ignored above.)
3127 mtx_unlock(&ps->ps_mtx);
3128 proc_td_siginfo_capture(td, &ksi.ksi_info);
3133 * If we get here, the signal must be caught.
3135 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3136 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3137 ("postsig action: blocked sig %d", sig));
3140 * Set the new mask value and also defer further
3141 * occurrences of this signal.
3143 * Special case: user has done a sigsuspend. Here the
3144 * current mask is not of interest, but rather the
3145 * mask from before the sigsuspend is what we want
3146 * restored after the signal processing is completed.
3148 if (td->td_pflags & TDP_OLDMASK) {
3149 returnmask = td->td_oldsigmask;
3150 td->td_pflags &= ~TDP_OLDMASK;
3152 returnmask = td->td_sigmask;
3154 if (p->p_sig == sig) {
3157 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3158 postsig_done(sig, td, ps);
3164 proc_wkilled(struct proc *p)
3167 PROC_LOCK_ASSERT(p, MA_OWNED);
3168 if ((p->p_flag & P_WKILLED) == 0) {
3169 p->p_flag |= P_WKILLED;
3171 * Notify swapper that there is a process to swap in.
3172 * The notification is racy, at worst it would take 10
3173 * seconds for the swapper process to notice.
3175 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3181 * Kill the current process for stated reason.
3184 killproc(struct proc *p, char *why)
3187 PROC_LOCK_ASSERT(p, MA_OWNED);
3188 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3190 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3191 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3192 p->p_ucred->cr_uid, why);
3194 kern_psignal(p, SIGKILL);
3198 * Force the current process to exit with the specified signal, dumping core
3199 * if appropriate. We bypass the normal tests for masked and caught signals,
3200 * allowing unrecoverable failures to terminate the process without changing
3201 * signal state. Mark the accounting record with the signal termination.
3202 * If dumping core, save the signal number for the debugger. Calls exit and
3206 sigexit(struct thread *td, int sig)
3208 struct proc *p = td->td_proc;
3210 PROC_LOCK_ASSERT(p, MA_OWNED);
3211 p->p_acflag |= AXSIG;
3213 * We must be single-threading to generate a core dump. This
3214 * ensures that the registers in the core file are up-to-date.
3215 * Also, the ELF dump handler assumes that the thread list doesn't
3216 * change out from under it.
3218 * XXX If another thread attempts to single-thread before us
3219 * (e.g. via fork()), we won't get a dump at all.
3221 if ((sigprop(sig) & SIGPROP_CORE) &&
3222 thread_single(p, SINGLE_NO_EXIT) == 0) {
3225 * Log signals which would cause core dumps
3226 * (Log as LOG_INFO to appease those who don't want
3228 * XXX : Todo, as well as euid, write out ruid too
3229 * Note that coredump() drops proc lock.
3231 if (coredump(td) == 0)
3233 if (kern_logsigexit)
3235 "pid %d (%s), jid %d, uid %d: exited on "
3236 "signal %d%s\n", p->p_pid, p->p_comm,
3237 p->p_ucred->cr_prison->pr_id,
3238 td->td_ucred->cr_uid,
3240 sig & WCOREFLAG ? " (core dumped)" : "");
3248 * Send queued SIGCHLD to parent when child process's state
3252 sigparent(struct proc *p, int reason, int status)
3254 PROC_LOCK_ASSERT(p, MA_OWNED);
3255 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3257 if (p->p_ksi != NULL) {
3258 p->p_ksi->ksi_signo = SIGCHLD;
3259 p->p_ksi->ksi_code = reason;
3260 p->p_ksi->ksi_status = status;
3261 p->p_ksi->ksi_pid = p->p_pid;
3262 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3263 if (KSI_ONQ(p->p_ksi))
3266 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3270 childproc_jobstate(struct proc *p, int reason, int sig)
3274 PROC_LOCK_ASSERT(p, MA_OWNED);
3275 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3278 * Wake up parent sleeping in kern_wait(), also send
3279 * SIGCHLD to parent, but SIGCHLD does not guarantee
3280 * that parent will awake, because parent may masked
3283 p->p_pptr->p_flag |= P_STATCHILD;
3286 ps = p->p_pptr->p_sigacts;
3287 mtx_lock(&ps->ps_mtx);
3288 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3289 mtx_unlock(&ps->ps_mtx);
3290 sigparent(p, reason, sig);
3292 mtx_unlock(&ps->ps_mtx);
3296 childproc_stopped(struct proc *p, int reason)
3299 childproc_jobstate(p, reason, p->p_xsig);
3303 childproc_continued(struct proc *p)
3305 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3309 childproc_exited(struct proc *p)
3313 if (WCOREDUMP(p->p_xsig)) {
3314 reason = CLD_DUMPED;
3315 status = WTERMSIG(p->p_xsig);
3316 } else if (WIFSIGNALED(p->p_xsig)) {
3317 reason = CLD_KILLED;
3318 status = WTERMSIG(p->p_xsig);
3320 reason = CLD_EXITED;
3321 status = p->p_xexit;
3324 * XXX avoid calling wakeup(p->p_pptr), the work is
3327 sigparent(p, reason, status);
3330 #define MAX_NUM_CORE_FILES 100000
3331 #ifndef NUM_CORE_FILES
3332 #define NUM_CORE_FILES 5
3334 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3335 static int num_cores = NUM_CORE_FILES;
3338 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3343 new_val = num_cores;
3344 error = sysctl_handle_int(oidp, &new_val, 0, req);
3345 if (error != 0 || req->newptr == NULL)
3347 if (new_val > MAX_NUM_CORE_FILES)
3348 new_val = MAX_NUM_CORE_FILES;
3351 num_cores = new_val;
3354 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3355 0, sizeof(int), sysctl_debug_num_cores_check, "I",
3356 "Maximum number of generated process corefiles while using index format");
3358 #define GZIP_SUFFIX ".gz"
3359 #define ZSTD_SUFFIX ".zst"
3361 int compress_user_cores = 0;
3364 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3368 val = compress_user_cores;
3369 error = sysctl_handle_int(oidp, &val, 0, req);
3370 if (error != 0 || req->newptr == NULL)
3372 if (val != 0 && !compressor_avail(val))
3374 compress_user_cores = val;
3377 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores, CTLTYPE_INT | CTLFLAG_RWTUN,
3378 0, sizeof(int), sysctl_compress_user_cores, "I",
3379 "Enable compression of user corefiles ("
3380 __XSTRING(COMPRESS_GZIP) " = gzip, "
3381 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3383 int compress_user_cores_level = 6;
3384 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3385 &compress_user_cores_level, 0,
3386 "Corefile compression level");
3389 * Protect the access to corefilename[] by allproc_lock.
3391 #define corefilename_lock allproc_lock
3393 static char corefilename[MAXPATHLEN] = {"%N.core"};
3394 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3397 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3401 sx_xlock(&corefilename_lock);
3402 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3404 sx_xunlock(&corefilename_lock);
3408 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3409 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3410 "Process corefile name format string");
3413 vnode_close_locked(struct thread *td, struct vnode *vp)
3417 vn_close(vp, FWRITE, td->td_ucred, td);
3421 * If the core format has a %I in it, then we need to check
3422 * for existing corefiles before defining a name.
3423 * To do this we iterate over 0..ncores to find a
3424 * non-existing core file name to use. If all core files are
3425 * already used we choose the oldest one.
3428 corefile_open_last(struct thread *td, char *name, int indexpos,
3429 int indexlen, int ncores, struct vnode **vpp)
3431 struct vnode *oldvp, *nextvp, *vp;
3433 struct nameidata nd;
3434 int error, i, flags, oflags, cmode;
3436 struct timespec lasttime;
3438 nextvp = oldvp = NULL;
3439 cmode = S_IRUSR | S_IWUSR;
3440 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3441 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3443 for (i = 0; i < ncores; i++) {
3444 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3446 ch = name[indexpos + indexlen];
3447 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3449 name[indexpos + indexlen] = ch;
3451 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3452 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3458 NDFREE(&nd, NDF_ONLY_PNBUF);
3459 if ((flags & O_CREAT) == O_CREAT) {
3464 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3466 vnode_close_locked(td, vp);
3470 if (oldvp == NULL ||
3471 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3472 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3473 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3475 vnode_close_locked(td, oldvp);
3477 lasttime = vattr.va_mtime;
3479 vnode_close_locked(td, vp);
3483 if (oldvp != NULL) {
3484 if (nextvp == NULL) {
3485 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3487 vnode_close_locked(td, oldvp);
3492 vnode_close_locked(td, oldvp);
3497 vnode_close_locked(td, oldvp);
3506 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3507 * Expand the name described in corefilename, using name, uid, and pid
3508 * and open/create core file.
3509 * corefilename is a printf-like string, with three format specifiers:
3510 * %N name of process ("name")
3511 * %P process id (pid)
3513 * For example, "%N.core" is the default; they can be disabled completely
3514 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3515 * This is controlled by the sysctl variable kern.corefile (see above).
3518 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3519 int compress, int signum, struct vnode **vpp, char **namep)
3522 struct nameidata nd;
3524 char *hostname, *name;
3525 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3528 format = corefilename;
3529 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3533 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3534 sx_slock(&corefilename_lock);
3535 for (i = 0; format[i] != '\0'; i++) {
3536 switch (format[i]) {
3537 case '%': /* Format character */
3539 switch (format[i]) {
3541 sbuf_putc(&sb, '%');
3543 case 'H': /* hostname */
3544 if (hostname == NULL) {
3545 hostname = malloc(MAXHOSTNAMELEN,
3548 getcredhostname(td->td_ucred, hostname,
3550 sbuf_printf(&sb, "%s", hostname);
3552 case 'I': /* autoincrementing index */
3553 if (indexpos != -1) {
3554 sbuf_printf(&sb, "%%I");
3558 indexpos = sbuf_len(&sb);
3559 sbuf_printf(&sb, "%u", ncores - 1);
3560 indexlen = sbuf_len(&sb) - indexpos;
3562 case 'N': /* process name */
3563 sbuf_printf(&sb, "%s", comm);
3565 case 'P': /* process id */
3566 sbuf_printf(&sb, "%u", pid);
3568 case 'S': /* signal number */
3569 sbuf_printf(&sb, "%i", signum);
3571 case 'U': /* user id */
3572 sbuf_printf(&sb, "%u", uid);
3576 "Unknown format character %c in "
3577 "corename `%s'\n", format[i], format);
3582 sbuf_putc(&sb, format[i]);
3586 sx_sunlock(&corefilename_lock);
3587 free(hostname, M_TEMP);
3588 if (compress == COMPRESS_GZIP)
3589 sbuf_printf(&sb, GZIP_SUFFIX);
3590 else if (compress == COMPRESS_ZSTD)
3591 sbuf_printf(&sb, ZSTD_SUFFIX);
3592 if (sbuf_error(&sb) != 0) {
3593 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3594 "long\n", (long)pid, comm, (u_long)uid);
3602 if (indexpos != -1) {
3603 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3607 "pid %d (%s), uid (%u): Path `%s' failed "
3608 "on initial open test, error = %d\n",
3609 pid, comm, uid, name, error);
3612 cmode = S_IRUSR | S_IWUSR;
3613 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3614 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3615 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3616 if ((td->td_proc->p_flag & P_SUGID) != 0)
3619 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3620 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3624 NDFREE(&nd, NDF_ONLY_PNBUF);
3630 audit_proc_coredump(td, name, error);
3640 * Dump a process' core. The main routine does some
3641 * policy checking, and creates the name of the coredump;
3642 * then it passes on a vnode and a size limit to the process-specific
3643 * coredump routine if there is one; if there _is not_ one, it returns
3644 * ENOSYS; otherwise it returns the error from the process-specific routine.
3648 coredump(struct thread *td)
3650 struct proc *p = td->td_proc;
3651 struct ucred *cred = td->td_ucred;
3655 size_t fullpathsize;
3656 int error, error1, locked;
3657 char *name; /* name of corefile */
3660 char *fullpath, *freepath = NULL;
3663 PROC_LOCK_ASSERT(p, MA_OWNED);
3664 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3665 _STOPEVENT(p, S_CORE, 0);
3667 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3668 (p->p_flag2 & P2_NOTRACE) != 0) {
3674 * Note that the bulk of limit checking is done after
3675 * the corefile is created. The exception is if the limit
3676 * for corefiles is 0, in which case we don't bother
3677 * creating the corefile at all. This layout means that
3678 * a corefile is truncated instead of not being created,
3679 * if it is larger than the limit.
3681 limit = (off_t)lim_cur(td, RLIMIT_CORE);
3682 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3688 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
3689 compress_user_cores, p->p_sig, &vp, &name);
3694 * Don't dump to non-regular files or files with links.
3695 * Do not dump into system files. Effective user must own the corefile.
3697 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3698 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
3699 vattr.va_uid != cred->cr_uid) {
3707 /* Postpone other writers, including core dumps of other processes. */
3708 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3710 lf.l_whence = SEEK_SET;
3713 lf.l_type = F_WRLCK;
3714 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3718 if (set_core_nodump_flag)
3719 vattr.va_flags = UF_NODUMP;
3720 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3721 VOP_SETATTR(vp, &vattr, cred);
3724 p->p_acflag |= ACORE;
3727 if (p->p_sysent->sv_coredump != NULL) {
3728 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
3734 lf.l_type = F_UNLCK;
3735 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3737 vn_rangelock_unlock(vp, rl_cookie);
3740 * Notify the userland helper that a process triggered a core dump.
3741 * This allows the helper to run an automated debugging session.
3743 if (error != 0 || coredump_devctl == 0)
3745 sb = sbuf_new_auto();
3746 if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0)
3748 sbuf_printf(sb, "comm=\"");
3749 devctl_safe_quote_sb(sb, fullpath);
3750 free(freepath, M_TEMP);
3751 sbuf_printf(sb, "\" core=\"");
3754 * We can't lookup core file vp directly. When we're replacing a core, and
3755 * other random times, we flush the name cache, so it will fail. Instead,
3756 * if the path of the core is relative, add the current dir in front if it.
3758 if (name[0] != '/') {
3759 fullpathsize = MAXPATHLEN;
3760 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
3761 if (vn_getcwd(td, freepath, &fullpath, &fullpathsize) != 0) {
3762 free(freepath, M_TEMP);
3765 devctl_safe_quote_sb(sb, fullpath);
3766 free(freepath, M_TEMP);
3769 devctl_safe_quote_sb(sb, name);
3770 sbuf_printf(sb, "\"");
3771 if (sbuf_finish(sb) == 0)
3772 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
3776 error1 = vn_close(vp, FWRITE, cred, td);
3780 audit_proc_coredump(td, name, error);
3787 * Nonexistent system call-- signal process (may want to handle it). Flag
3788 * error in case process won't see signal immediately (blocked or ignored).
3790 #ifndef _SYS_SYSPROTO_H_
3797 nosys(struct thread *td, struct nosys_args *args)
3804 tdsignal(td, SIGSYS);
3806 if (kern_lognosys == 1 || kern_lognosys == 3) {
3807 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3810 if (kern_lognosys == 2 || kern_lognosys == 3) {
3811 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3818 * Send a SIGIO or SIGURG signal to a process or process group using stored
3819 * credentials rather than those of the current process.
3822 pgsigio(struct sigio **sigiop, int sig, int checkctty)
3825 struct sigio *sigio;
3827 ksiginfo_init(&ksi);
3828 ksi.ksi_signo = sig;
3829 ksi.ksi_code = SI_KERNEL;
3833 if (sigio == NULL) {
3837 if (sigio->sio_pgid > 0) {
3838 PROC_LOCK(sigio->sio_proc);
3839 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3840 kern_psignal(sigio->sio_proc, sig);
3841 PROC_UNLOCK(sigio->sio_proc);
3842 } else if (sigio->sio_pgid < 0) {
3845 PGRP_LOCK(sigio->sio_pgrp);
3846 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3848 if (p->p_state == PRS_NORMAL &&
3849 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3850 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3851 kern_psignal(p, sig);
3854 PGRP_UNLOCK(sigio->sio_pgrp);
3860 filt_sigattach(struct knote *kn)
3862 struct proc *p = curproc;
3864 kn->kn_ptr.p_proc = p;
3865 kn->kn_flags |= EV_CLEAR; /* automatically set */
3867 knlist_add(p->p_klist, kn, 0);
3873 filt_sigdetach(struct knote *kn)
3875 struct proc *p = kn->kn_ptr.p_proc;
3877 knlist_remove(p->p_klist, kn, 0);
3881 * signal knotes are shared with proc knotes, so we apply a mask to
3882 * the hint in order to differentiate them from process hints. This
3883 * could be avoided by using a signal-specific knote list, but probably
3884 * isn't worth the trouble.
3887 filt_signal(struct knote *kn, long hint)
3890 if (hint & NOTE_SIGNAL) {
3891 hint &= ~NOTE_SIGNAL;
3893 if (kn->kn_id == hint)
3896 return (kn->kn_data != 0);
3904 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3905 refcount_init(&ps->ps_refcnt, 1);
3906 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3911 sigacts_free(struct sigacts *ps)
3914 if (refcount_release(&ps->ps_refcnt) == 0)
3916 mtx_destroy(&ps->ps_mtx);
3917 free(ps, M_SUBPROC);
3921 sigacts_hold(struct sigacts *ps)
3924 refcount_acquire(&ps->ps_refcnt);
3929 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3932 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3933 mtx_lock(&src->ps_mtx);
3934 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3935 mtx_unlock(&src->ps_mtx);
3939 sigacts_shared(struct sigacts *ps)
3942 return (ps->ps_refcnt > 1);
3946 sig_drop_caught(struct proc *p)
3952 PROC_LOCK_ASSERT(p, MA_OWNED);
3953 mtx_assert(&ps->ps_mtx, MA_OWNED);
3954 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
3955 sig = sig_ffs(&ps->ps_sigcatch);
3957 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
3958 sigqueue_delete_proc(p, sig);
3963 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
3968 * Prevent further fetches and SIGSEGVs, allowing thread to
3969 * issue syscalls despite corruption.
3971 sigfastblock_clear(td);
3975 ksiginfo_init_trap(&ksi);
3976 ksi.ksi_signo = SIGSEGV;
3977 ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
3978 ksi.ksi_addr = td->td_sigblock_ptr;
3979 trapsignal(td, &ksi);
3983 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
3987 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
3989 if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
3990 sigfastblock_failed(td, sendsig, false);
3994 td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
3999 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4008 case SIGFASTBLOCK_SETPTR:
4009 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4013 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4017 td->td_pflags |= TDP_SIGFASTBLOCK;
4018 td->td_sigblock_ptr = uap->ptr;
4021 case SIGFASTBLOCK_UNBLOCK:
4022 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4028 res = casueword32(td->td_sigblock_ptr,
4029 SIGFASTBLOCK_PEND, &oldval, 0);
4032 sigfastblock_failed(td, false, true);
4038 if (oldval != SIGFASTBLOCK_PEND) {
4042 error = thread_check_susp(td, false);
4050 * td_sigblock_val is cleared there, but not on a
4051 * syscall exit. The end effect is that a single
4052 * interruptible sleep, while user sigblock word is
4053 * set, might return EINTR or ERESTART to usermode
4054 * without delivering signal. All further sleeps,
4055 * until userspace clears the word and does
4056 * sigfastblock(UNBLOCK), observe current word and no
4057 * longer get interrupted. It is slight
4058 * non-conformance, with alternative to have read the
4059 * sigblock word on each syscall entry.
4061 td->td_sigblock_val = 0;
4064 * Rely on normal ast mechanism to deliver pending
4065 * signals to current thread. But notify others about
4068 if (error == 0 && p->p_numthreads != 1) {
4070 reschedule_signals(p, td->td_sigmask, 0);
4075 case SIGFASTBLOCK_UNSETPTR:
4076 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4080 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4084 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4088 sigfastblock_clear(td);
4099 sigfastblock_clear(struct thread *td)
4104 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4106 td->td_sigblock_val = 0;
4107 resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0;
4108 td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4112 reschedule_signals(p, td->td_sigmask, 0);
4118 sigfastblock_fetch(struct thread *td)
4122 (void)sigfastblock_fetch_sig(td, true, &val);
4126 sigfastblock_setpend(struct thread *td)
4131 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4133 res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4135 sigfastblock_failed(td, true, false);
4139 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4140 oldval | SIGFASTBLOCK_PEND);
4142 sigfastblock_failed(td, true, true);
4146 td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4147 td->td_pflags &= ~TDP_SIGFASTPENDING;
4151 if (thread_check_susp(td, false) != 0)