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, int prop);
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 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
163 * Policy -- Can ucred cr1 send SIGIO to process cr2?
164 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
165 * in the right situations.
167 #define CANSIGIO(cr1, cr2) \
168 ((cr1)->cr_uid == 0 || \
169 (cr1)->cr_ruid == (cr2)->cr_ruid || \
170 (cr1)->cr_uid == (cr2)->cr_ruid || \
171 (cr1)->cr_ruid == (cr2)->cr_uid || \
172 (cr1)->cr_uid == (cr2)->cr_uid)
174 static int sugid_coredump;
175 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
176 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
178 static int capmode_coredump;
179 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
180 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
182 static int do_coredump = 1;
183 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
184 &do_coredump, 0, "Enable/Disable coredumps");
186 static int set_core_nodump_flag = 0;
187 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
188 0, "Enable setting the NODUMP flag on coredump files");
190 static int coredump_devctl = 0;
191 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
192 0, "Generate a devctl notification when processes coredump");
195 * Signal properties and actions.
196 * The array below categorizes the signals and their default actions
197 * according to the following properties:
199 #define SIGPROP_KILL 0x01 /* terminates process by default */
200 #define SIGPROP_CORE 0x02 /* ditto and coredumps */
201 #define SIGPROP_STOP 0x04 /* suspend process */
202 #define SIGPROP_TTYSTOP 0x08 /* ditto, from tty */
203 #define SIGPROP_IGNORE 0x10 /* ignore by default */
204 #define SIGPROP_CONT 0x20 /* continue if suspended */
205 #define SIGPROP_CANTMASK 0x40 /* non-maskable, catchable */
207 static int sigproptbl[NSIG] = {
208 [SIGHUP] = SIGPROP_KILL,
209 [SIGINT] = SIGPROP_KILL,
210 [SIGQUIT] = SIGPROP_KILL | SIGPROP_CORE,
211 [SIGILL] = SIGPROP_KILL | SIGPROP_CORE,
212 [SIGTRAP] = SIGPROP_KILL | SIGPROP_CORE,
213 [SIGABRT] = SIGPROP_KILL | SIGPROP_CORE,
214 [SIGEMT] = SIGPROP_KILL | SIGPROP_CORE,
215 [SIGFPE] = SIGPROP_KILL | SIGPROP_CORE,
216 [SIGKILL] = SIGPROP_KILL,
217 [SIGBUS] = SIGPROP_KILL | SIGPROP_CORE,
218 [SIGSEGV] = SIGPROP_KILL | SIGPROP_CORE,
219 [SIGSYS] = SIGPROP_KILL | SIGPROP_CORE,
220 [SIGPIPE] = SIGPROP_KILL,
221 [SIGALRM] = SIGPROP_KILL,
222 [SIGTERM] = SIGPROP_KILL,
223 [SIGURG] = SIGPROP_IGNORE,
224 [SIGSTOP] = SIGPROP_STOP,
225 [SIGTSTP] = SIGPROP_STOP | SIGPROP_TTYSTOP,
226 [SIGCONT] = SIGPROP_IGNORE | SIGPROP_CONT,
227 [SIGCHLD] = SIGPROP_IGNORE,
228 [SIGTTIN] = SIGPROP_STOP | SIGPROP_TTYSTOP,
229 [SIGTTOU] = SIGPROP_STOP | SIGPROP_TTYSTOP,
230 [SIGIO] = SIGPROP_IGNORE,
231 [SIGXCPU] = SIGPROP_KILL,
232 [SIGXFSZ] = SIGPROP_KILL,
233 [SIGVTALRM] = SIGPROP_KILL,
234 [SIGPROF] = SIGPROP_KILL,
235 [SIGWINCH] = SIGPROP_IGNORE,
236 [SIGINFO] = SIGPROP_IGNORE,
237 [SIGUSR1] = SIGPROP_KILL,
238 [SIGUSR2] = SIGPROP_KILL,
241 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
246 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
247 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
248 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
249 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
250 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
251 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
255 ksiginfo_alloc(int wait)
262 if (ksiginfo_zone != NULL)
263 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
268 ksiginfo_free(ksiginfo_t *ksi)
270 uma_zfree(ksiginfo_zone, ksi);
274 ksiginfo_tryfree(ksiginfo_t *ksi)
276 if (!(ksi->ksi_flags & KSI_EXT)) {
277 uma_zfree(ksiginfo_zone, ksi);
284 sigqueue_init(sigqueue_t *list, struct proc *p)
286 SIGEMPTYSET(list->sq_signals);
287 SIGEMPTYSET(list->sq_kill);
288 SIGEMPTYSET(list->sq_ptrace);
289 TAILQ_INIT(&list->sq_list);
291 list->sq_flags = SQ_INIT;
295 * Get a signal's ksiginfo.
297 * 0 - signal not found
298 * others - signal number
301 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
303 struct proc *p = sq->sq_proc;
304 struct ksiginfo *ksi, *next;
307 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
309 if (!SIGISMEMBER(sq->sq_signals, signo))
312 if (SIGISMEMBER(sq->sq_ptrace, signo)) {
314 SIGDELSET(sq->sq_ptrace, signo);
315 si->ksi_flags |= KSI_PTRACE;
317 if (SIGISMEMBER(sq->sq_kill, signo)) {
320 SIGDELSET(sq->sq_kill, signo);
323 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
324 if (ksi->ksi_signo == signo) {
326 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
327 ksi->ksi_sigq = NULL;
328 ksiginfo_copy(ksi, si);
329 if (ksiginfo_tryfree(ksi) && p != NULL)
338 SIGDELSET(sq->sq_signals, signo);
339 si->ksi_signo = signo;
344 sigqueue_take(ksiginfo_t *ksi)
350 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
354 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
355 ksi->ksi_sigq = NULL;
356 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
359 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
360 kp = TAILQ_NEXT(kp, ksi_link)) {
361 if (kp->ksi_signo == ksi->ksi_signo)
364 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
365 !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
366 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
370 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
372 struct proc *p = sq->sq_proc;
373 struct ksiginfo *ksi;
376 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
379 * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
382 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
383 SIGADDSET(sq->sq_kill, signo);
387 /* directly insert the ksi, don't copy it */
388 if (si->ksi_flags & KSI_INS) {
389 if (si->ksi_flags & KSI_HEAD)
390 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
392 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
397 if (__predict_false(ksiginfo_zone == NULL)) {
398 SIGADDSET(sq->sq_kill, signo);
402 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
405 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
411 ksiginfo_copy(si, ksi);
412 ksi->ksi_signo = signo;
413 if (si->ksi_flags & KSI_HEAD)
414 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
416 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
421 if ((si->ksi_flags & KSI_PTRACE) != 0) {
422 SIGADDSET(sq->sq_ptrace, signo);
425 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
426 (si->ksi_flags & KSI_SIGQ) == 0) {
427 SIGADDSET(sq->sq_kill, signo);
435 SIGADDSET(sq->sq_signals, signo);
440 sigqueue_flush(sigqueue_t *sq)
442 struct proc *p = sq->sq_proc;
445 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
448 PROC_LOCK_ASSERT(p, MA_OWNED);
450 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
451 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
452 ksi->ksi_sigq = NULL;
453 if (ksiginfo_tryfree(ksi) && p != NULL)
457 SIGEMPTYSET(sq->sq_signals);
458 SIGEMPTYSET(sq->sq_kill);
459 SIGEMPTYSET(sq->sq_ptrace);
463 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
466 struct proc *p1, *p2;
467 ksiginfo_t *ksi, *next;
469 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
470 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
473 /* Move siginfo to target list */
474 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
475 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
476 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
479 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
486 /* Move pending bits to target list */
488 SIGSETAND(tmp, *set);
489 SIGSETOR(dst->sq_kill, tmp);
490 SIGSETNAND(src->sq_kill, tmp);
492 tmp = src->sq_ptrace;
493 SIGSETAND(tmp, *set);
494 SIGSETOR(dst->sq_ptrace, tmp);
495 SIGSETNAND(src->sq_ptrace, tmp);
497 tmp = src->sq_signals;
498 SIGSETAND(tmp, *set);
499 SIGSETOR(dst->sq_signals, tmp);
500 SIGSETNAND(src->sq_signals, tmp);
505 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
510 SIGADDSET(set, signo);
511 sigqueue_move_set(src, dst, &set);
516 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
518 struct proc *p = sq->sq_proc;
519 ksiginfo_t *ksi, *next;
521 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
523 /* Remove siginfo queue */
524 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
525 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
526 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
527 ksi->ksi_sigq = NULL;
528 if (ksiginfo_tryfree(ksi) && p != NULL)
532 SIGSETNAND(sq->sq_kill, *set);
533 SIGSETNAND(sq->sq_ptrace, *set);
534 SIGSETNAND(sq->sq_signals, *set);
538 sigqueue_delete(sigqueue_t *sq, int signo)
543 SIGADDSET(set, signo);
544 sigqueue_delete_set(sq, &set);
547 /* Remove a set of signals for a process */
549 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
554 PROC_LOCK_ASSERT(p, MA_OWNED);
556 sigqueue_init(&worklist, NULL);
557 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
559 FOREACH_THREAD_IN_PROC(p, td0)
560 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
562 sigqueue_flush(&worklist);
566 sigqueue_delete_proc(struct proc *p, int signo)
571 SIGADDSET(set, signo);
572 sigqueue_delete_set_proc(p, &set);
576 sigqueue_delete_stopmask_proc(struct proc *p)
581 SIGADDSET(set, SIGSTOP);
582 SIGADDSET(set, SIGTSTP);
583 SIGADDSET(set, SIGTTIN);
584 SIGADDSET(set, SIGTTOU);
585 sigqueue_delete_set_proc(p, &set);
589 * Determine signal that should be delivered to thread td, the current
590 * thread, 0 if none. If there is a pending stop signal with default
591 * action, the process stops in issignal().
594 cursig(struct thread *td)
596 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
597 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
598 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
599 return (SIGPENDING(td) ? issignal(td) : 0);
603 * Arrange for ast() to handle unmasked pending signals on return to user
604 * mode. This must be called whenever a signal is added to td_sigqueue or
605 * unmasked in td_sigmask.
608 signotify(struct thread *td)
611 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
613 if (SIGPENDING(td)) {
615 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
621 * Returns 1 (true) if altstack is configured for the thread, and the
622 * passed stack bottom address falls into the altstack range. Handles
623 * the 43 compat special case where the alt stack size is zero.
626 sigonstack(size_t sp)
631 if ((td->td_pflags & TDP_ALTSTACK) == 0)
633 #if defined(COMPAT_43)
634 if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
635 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
637 return (sp >= (size_t)td->td_sigstk.ss_sp &&
638 sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
645 if (sig > 0 && sig < nitems(sigproptbl))
646 return (sigproptbl[sig]);
651 sig_ffs(sigset_t *set)
655 for (i = 0; i < _SIG_WORDS; i++)
657 return (ffs(set->__bits[i]) + (i * 32));
662 sigact_flag_test(const struct sigaction *act, int flag)
666 * SA_SIGINFO is reset when signal disposition is set to
667 * ignore or default. Other flags are kept according to user
670 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
671 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
672 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
682 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
683 struct sigaction *oact, int flags)
686 struct proc *p = td->td_proc;
688 if (!_SIG_VALID(sig))
690 if (act != NULL && act->sa_handler != SIG_DFL &&
691 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
692 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
693 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
698 mtx_lock(&ps->ps_mtx);
700 memset(oact, 0, sizeof(*oact));
701 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
702 if (SIGISMEMBER(ps->ps_sigonstack, sig))
703 oact->sa_flags |= SA_ONSTACK;
704 if (!SIGISMEMBER(ps->ps_sigintr, sig))
705 oact->sa_flags |= SA_RESTART;
706 if (SIGISMEMBER(ps->ps_sigreset, sig))
707 oact->sa_flags |= SA_RESETHAND;
708 if (SIGISMEMBER(ps->ps_signodefer, sig))
709 oact->sa_flags |= SA_NODEFER;
710 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
711 oact->sa_flags |= SA_SIGINFO;
713 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
715 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
716 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
717 oact->sa_flags |= SA_NOCLDSTOP;
718 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
719 oact->sa_flags |= SA_NOCLDWAIT;
722 if ((sig == SIGKILL || sig == SIGSTOP) &&
723 act->sa_handler != SIG_DFL) {
724 mtx_unlock(&ps->ps_mtx);
730 * Change setting atomically.
733 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
734 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
735 if (sigact_flag_test(act, SA_SIGINFO)) {
736 ps->ps_sigact[_SIG_IDX(sig)] =
737 (__sighandler_t *)act->sa_sigaction;
738 SIGADDSET(ps->ps_siginfo, sig);
740 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
741 SIGDELSET(ps->ps_siginfo, sig);
743 if (!sigact_flag_test(act, SA_RESTART))
744 SIGADDSET(ps->ps_sigintr, sig);
746 SIGDELSET(ps->ps_sigintr, sig);
747 if (sigact_flag_test(act, SA_ONSTACK))
748 SIGADDSET(ps->ps_sigonstack, sig);
750 SIGDELSET(ps->ps_sigonstack, sig);
751 if (sigact_flag_test(act, SA_RESETHAND))
752 SIGADDSET(ps->ps_sigreset, sig);
754 SIGDELSET(ps->ps_sigreset, sig);
755 if (sigact_flag_test(act, SA_NODEFER))
756 SIGADDSET(ps->ps_signodefer, sig);
758 SIGDELSET(ps->ps_signodefer, sig);
759 if (sig == SIGCHLD) {
760 if (act->sa_flags & SA_NOCLDSTOP)
761 ps->ps_flag |= PS_NOCLDSTOP;
763 ps->ps_flag &= ~PS_NOCLDSTOP;
764 if (act->sa_flags & SA_NOCLDWAIT) {
766 * Paranoia: since SA_NOCLDWAIT is implemented
767 * by reparenting the dying child to PID 1 (and
768 * trust it to reap the zombie), PID 1 itself
769 * is forbidden to set SA_NOCLDWAIT.
772 ps->ps_flag &= ~PS_NOCLDWAIT;
774 ps->ps_flag |= PS_NOCLDWAIT;
776 ps->ps_flag &= ~PS_NOCLDWAIT;
777 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
778 ps->ps_flag |= PS_CLDSIGIGN;
780 ps->ps_flag &= ~PS_CLDSIGIGN;
783 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
784 * and for signals set to SIG_DFL where the default is to
785 * ignore. However, don't put SIGCONT in ps_sigignore, as we
786 * have to restart the process.
788 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
789 (sigprop(sig) & SIGPROP_IGNORE &&
790 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
791 /* never to be seen again */
792 sigqueue_delete_proc(p, sig);
794 /* easier in psignal */
795 SIGADDSET(ps->ps_sigignore, sig);
796 SIGDELSET(ps->ps_sigcatch, sig);
798 SIGDELSET(ps->ps_sigignore, sig);
799 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
800 SIGDELSET(ps->ps_sigcatch, sig);
802 SIGADDSET(ps->ps_sigcatch, sig);
804 #ifdef COMPAT_FREEBSD4
805 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
806 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
807 (flags & KSA_FREEBSD4) == 0)
808 SIGDELSET(ps->ps_freebsd4, sig);
810 SIGADDSET(ps->ps_freebsd4, sig);
813 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
814 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
815 (flags & KSA_OSIGSET) == 0)
816 SIGDELSET(ps->ps_osigset, sig);
818 SIGADDSET(ps->ps_osigset, sig);
821 mtx_unlock(&ps->ps_mtx);
826 #ifndef _SYS_SYSPROTO_H_
827 struct sigaction_args {
829 struct sigaction *act;
830 struct sigaction *oact;
834 sys_sigaction(struct thread *td, struct sigaction_args *uap)
836 struct sigaction act, oact;
837 struct sigaction *actp, *oactp;
840 actp = (uap->act != NULL) ? &act : NULL;
841 oactp = (uap->oact != NULL) ? &oact : NULL;
843 error = copyin(uap->act, actp, sizeof(act));
847 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
849 error = copyout(oactp, uap->oact, sizeof(oact));
853 #ifdef COMPAT_FREEBSD4
854 #ifndef _SYS_SYSPROTO_H_
855 struct freebsd4_sigaction_args {
857 struct sigaction *act;
858 struct sigaction *oact;
862 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
864 struct sigaction act, oact;
865 struct sigaction *actp, *oactp;
869 actp = (uap->act != NULL) ? &act : NULL;
870 oactp = (uap->oact != NULL) ? &oact : NULL;
872 error = copyin(uap->act, actp, sizeof(act));
876 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
878 error = copyout(oactp, uap->oact, sizeof(oact));
881 #endif /* COMAPT_FREEBSD4 */
883 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
884 #ifndef _SYS_SYSPROTO_H_
885 struct osigaction_args {
887 struct osigaction *nsa;
888 struct osigaction *osa;
892 osigaction(struct thread *td, struct osigaction_args *uap)
894 struct osigaction sa;
895 struct sigaction nsa, osa;
896 struct sigaction *nsap, *osap;
899 if (uap->signum <= 0 || uap->signum >= ONSIG)
902 nsap = (uap->nsa != NULL) ? &nsa : NULL;
903 osap = (uap->osa != NULL) ? &osa : NULL;
906 error = copyin(uap->nsa, &sa, sizeof(sa));
909 nsap->sa_handler = sa.sa_handler;
910 nsap->sa_flags = sa.sa_flags;
911 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
913 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
914 if (osap && !error) {
915 sa.sa_handler = osap->sa_handler;
916 sa.sa_flags = osap->sa_flags;
917 SIG2OSIG(osap->sa_mask, sa.sa_mask);
918 error = copyout(&sa, uap->osa, sizeof(sa));
923 #if !defined(__i386__)
924 /* Avoid replicating the same stub everywhere */
926 osigreturn(struct thread *td, struct osigreturn_args *uap)
929 return (nosys(td, (struct nosys_args *)uap));
932 #endif /* COMPAT_43 */
935 * Initialize signal state for process 0;
936 * set to ignore signals that are ignored by default.
939 siginit(struct proc *p)
946 mtx_lock(&ps->ps_mtx);
947 for (i = 1; i <= NSIG; i++) {
948 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
949 SIGADDSET(ps->ps_sigignore, i);
952 mtx_unlock(&ps->ps_mtx);
957 * Reset specified signal to the default disposition.
960 sigdflt(struct sigacts *ps, int sig)
963 mtx_assert(&ps->ps_mtx, MA_OWNED);
964 SIGDELSET(ps->ps_sigcatch, sig);
965 if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
966 SIGADDSET(ps->ps_sigignore, sig);
967 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
968 SIGDELSET(ps->ps_siginfo, sig);
972 * Reset signals for an exec of the specified process.
975 execsigs(struct proc *p)
983 * Reset caught signals. Held signals remain held
984 * through td_sigmask (unless they were caught,
985 * and are now ignored by default).
987 PROC_LOCK_ASSERT(p, MA_OWNED);
989 mtx_lock(&ps->ps_mtx);
993 * As CloudABI processes cannot modify signal handlers, fully
994 * reset all signals to their default behavior. Do ignore
995 * SIGPIPE, as it would otherwise be impossible to recover from
996 * writes to broken pipes and sockets.
998 if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) {
999 osigignore = ps->ps_sigignore;
1000 while (SIGNOTEMPTY(osigignore)) {
1001 sig = sig_ffs(&osigignore);
1002 SIGDELSET(osigignore, sig);
1006 SIGADDSET(ps->ps_sigignore, SIGPIPE);
1010 * Reset stack state to the user stack.
1011 * Clear set of signals caught on the signal stack.
1014 MPASS(td->td_proc == p);
1015 td->td_sigstk.ss_flags = SS_DISABLE;
1016 td->td_sigstk.ss_size = 0;
1017 td->td_sigstk.ss_sp = 0;
1018 td->td_pflags &= ~TDP_ALTSTACK;
1020 * Reset no zombies if child dies flag as Solaris does.
1022 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1023 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1024 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1025 mtx_unlock(&ps->ps_mtx);
1029 * kern_sigprocmask()
1031 * Manipulate signal mask.
1034 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1037 sigset_t new_block, oset1;
1042 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1043 PROC_LOCK_ASSERT(p, MA_OWNED);
1046 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1047 ? MA_OWNED : MA_NOTOWNED);
1049 *oset = td->td_sigmask;
1056 oset1 = td->td_sigmask;
1057 SIGSETOR(td->td_sigmask, *set);
1058 new_block = td->td_sigmask;
1059 SIGSETNAND(new_block, oset1);
1062 SIGSETNAND(td->td_sigmask, *set);
1067 oset1 = td->td_sigmask;
1068 if (flags & SIGPROCMASK_OLD)
1069 SIGSETLO(td->td_sigmask, *set);
1071 td->td_sigmask = *set;
1072 new_block = td->td_sigmask;
1073 SIGSETNAND(new_block, oset1);
1082 * The new_block set contains signals that were not previously
1083 * blocked, but are blocked now.
1085 * In case we block any signal that was not previously blocked
1086 * for td, and process has the signal pending, try to schedule
1087 * signal delivery to some thread that does not block the
1088 * signal, possibly waking it up.
1090 if (p->p_numthreads != 1)
1091 reschedule_signals(p, new_block, flags);
1095 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1100 #ifndef _SYS_SYSPROTO_H_
1101 struct sigprocmask_args {
1103 const sigset_t *set;
1108 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1111 sigset_t *setp, *osetp;
1114 setp = (uap->set != NULL) ? &set : NULL;
1115 osetp = (uap->oset != NULL) ? &oset : NULL;
1117 error = copyin(uap->set, setp, sizeof(set));
1121 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1122 if (osetp && !error) {
1123 error = copyout(osetp, uap->oset, sizeof(oset));
1128 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1129 #ifndef _SYS_SYSPROTO_H_
1130 struct osigprocmask_args {
1136 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1141 OSIG2SIG(uap->mask, set);
1142 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1143 SIG2OSIG(oset, td->td_retval[0]);
1146 #endif /* COMPAT_43 */
1149 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1155 error = copyin(uap->set, &set, sizeof(set));
1157 td->td_retval[0] = error;
1161 error = kern_sigtimedwait(td, set, &ksi, NULL);
1163 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1165 if (error == ERESTART)
1167 td->td_retval[0] = error;
1171 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1172 td->td_retval[0] = error;
1177 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1180 struct timespec *timeout;
1186 error = copyin(uap->timeout, &ts, sizeof(ts));
1194 error = copyin(uap->set, &set, sizeof(set));
1198 error = kern_sigtimedwait(td, set, &ksi, timeout);
1203 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1206 td->td_retval[0] = ksi.ksi_signo;
1211 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1217 error = copyin(uap->set, &set, sizeof(set));
1221 error = kern_sigtimedwait(td, set, &ksi, NULL);
1226 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1229 td->td_retval[0] = ksi.ksi_signo;
1234 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1238 FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1242 thr->td_si.si_signo = 0;
1247 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1248 struct timespec *timeout)
1251 sigset_t saved_mask, new_block;
1253 int error, sig, timo, timevalid = 0;
1254 struct timespec rts, ets, ts;
1264 if (timeout != NULL) {
1265 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1267 getnanouptime(&rts);
1268 timespecadd(&rts, timeout, &ets);
1272 /* Some signals can not be waited for. */
1273 SIG_CANTMASK(waitset);
1276 saved_mask = td->td_sigmask;
1277 SIGSETNAND(td->td_sigmask, waitset);
1279 mtx_lock(&ps->ps_mtx);
1281 mtx_unlock(&ps->ps_mtx);
1282 KASSERT(sig >= 0, ("sig %d", sig));
1283 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1284 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1285 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1295 * POSIX says this must be checked after looking for pending
1298 if (timeout != NULL) {
1303 getnanouptime(&rts);
1304 if (timespeccmp(&rts, &ets, >=)) {
1308 timespecsub(&ets, &rts, &ts);
1309 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1320 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1322 if (timeout != NULL) {
1323 if (error == ERESTART) {
1324 /* Timeout can not be restarted. */
1326 } else if (error == EAGAIN) {
1327 /* We will calculate timeout by ourself. */
1333 * If PTRACE_SCE or PTRACE_SCX were set after
1334 * userspace entered the syscall, return spurious
1335 * EINTR after wait was done. Only do this as last
1336 * resort after rechecking for possible queued signals
1337 * and expired timeouts.
1339 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1343 new_block = saved_mask;
1344 SIGSETNAND(new_block, td->td_sigmask);
1345 td->td_sigmask = saved_mask;
1347 * Fewer signals can be delivered to us, reschedule signal
1350 if (p->p_numthreads != 1)
1351 reschedule_signals(p, new_block, 0);
1354 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1356 if (ksi->ksi_code == SI_TIMER)
1357 itimer_accept(p, ksi->ksi_timerid, ksi);
1360 if (KTRPOINT(td, KTR_PSIG)) {
1363 mtx_lock(&ps->ps_mtx);
1364 action = ps->ps_sigact[_SIG_IDX(sig)];
1365 mtx_unlock(&ps->ps_mtx);
1366 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1369 if (sig == SIGKILL) {
1370 proc_td_siginfo_capture(td, &ksi->ksi_info);
1378 #ifndef _SYS_SYSPROTO_H_
1379 struct sigpending_args {
1384 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1386 struct proc *p = td->td_proc;
1390 pending = p->p_sigqueue.sq_signals;
1391 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1393 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1396 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1397 #ifndef _SYS_SYSPROTO_H_
1398 struct osigpending_args {
1403 osigpending(struct thread *td, struct osigpending_args *uap)
1405 struct proc *p = td->td_proc;
1409 pending = p->p_sigqueue.sq_signals;
1410 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1412 SIG2OSIG(pending, td->td_retval[0]);
1415 #endif /* COMPAT_43 */
1417 #if defined(COMPAT_43)
1419 * Generalized interface signal handler, 4.3-compatible.
1421 #ifndef _SYS_SYSPROTO_H_
1422 struct osigvec_args {
1430 osigvec(struct thread *td, struct osigvec_args *uap)
1433 struct sigaction nsa, osa;
1434 struct sigaction *nsap, *osap;
1437 if (uap->signum <= 0 || uap->signum >= ONSIG)
1439 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1440 osap = (uap->osv != NULL) ? &osa : NULL;
1442 error = copyin(uap->nsv, &vec, sizeof(vec));
1445 nsap->sa_handler = vec.sv_handler;
1446 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1447 nsap->sa_flags = vec.sv_flags;
1448 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1450 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1451 if (osap && !error) {
1452 vec.sv_handler = osap->sa_handler;
1453 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1454 vec.sv_flags = osap->sa_flags;
1455 vec.sv_flags &= ~SA_NOCLDWAIT;
1456 vec.sv_flags ^= SA_RESTART;
1457 error = copyout(&vec, uap->osv, sizeof(vec));
1462 #ifndef _SYS_SYSPROTO_H_
1463 struct osigblock_args {
1468 osigblock(struct thread *td, struct osigblock_args *uap)
1472 OSIG2SIG(uap->mask, set);
1473 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1474 SIG2OSIG(oset, td->td_retval[0]);
1478 #ifndef _SYS_SYSPROTO_H_
1479 struct osigsetmask_args {
1484 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1488 OSIG2SIG(uap->mask, set);
1489 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1490 SIG2OSIG(oset, td->td_retval[0]);
1493 #endif /* COMPAT_43 */
1496 * Suspend calling thread until signal, providing mask to be set in the
1499 #ifndef _SYS_SYSPROTO_H_
1500 struct sigsuspend_args {
1501 const sigset_t *sigmask;
1506 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1511 error = copyin(uap->sigmask, &mask, sizeof(mask));
1514 return (kern_sigsuspend(td, mask));
1518 kern_sigsuspend(struct thread *td, sigset_t mask)
1520 struct proc *p = td->td_proc;
1524 * When returning from sigsuspend, we want
1525 * the old mask to be restored after the
1526 * signal handler has finished. Thus, we
1527 * save it here and mark the sigacts structure
1531 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1532 SIGPROCMASK_PROC_LOCKED);
1533 td->td_pflags |= TDP_OLDMASK;
1536 * Process signals now. Otherwise, we can get spurious wakeup
1537 * due to signal entered process queue, but delivered to other
1538 * thread. But sigsuspend should return only on signal
1541 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1542 for (has_sig = 0; !has_sig;) {
1543 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1546 thread_suspend_check(0);
1547 mtx_lock(&p->p_sigacts->ps_mtx);
1548 while ((sig = cursig(td)) != 0) {
1549 KASSERT(sig >= 0, ("sig %d", sig));
1550 has_sig += postsig(sig);
1552 mtx_unlock(&p->p_sigacts->ps_mtx);
1555 * If PTRACE_SCE or PTRACE_SCX were set after
1556 * userspace entered the syscall, return spurious
1559 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1563 td->td_errno = EINTR;
1564 td->td_pflags |= TDP_NERRNO;
1565 return (EJUSTRETURN);
1568 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1570 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1571 * convention: libc stub passes mask, not pointer, to save a copyin.
1573 #ifndef _SYS_SYSPROTO_H_
1574 struct osigsuspend_args {
1580 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1584 OSIG2SIG(uap->mask, mask);
1585 return (kern_sigsuspend(td, mask));
1587 #endif /* COMPAT_43 */
1589 #if defined(COMPAT_43)
1590 #ifndef _SYS_SYSPROTO_H_
1591 struct osigstack_args {
1592 struct sigstack *nss;
1593 struct sigstack *oss;
1598 osigstack(struct thread *td, struct osigstack_args *uap)
1600 struct sigstack nss, oss;
1603 if (uap->nss != NULL) {
1604 error = copyin(uap->nss, &nss, sizeof(nss));
1608 oss.ss_sp = td->td_sigstk.ss_sp;
1609 oss.ss_onstack = sigonstack(cpu_getstack(td));
1610 if (uap->nss != NULL) {
1611 td->td_sigstk.ss_sp = nss.ss_sp;
1612 td->td_sigstk.ss_size = 0;
1613 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1614 td->td_pflags |= TDP_ALTSTACK;
1616 if (uap->oss != NULL)
1617 error = copyout(&oss, uap->oss, sizeof(oss));
1621 #endif /* COMPAT_43 */
1623 #ifndef _SYS_SYSPROTO_H_
1624 struct sigaltstack_args {
1631 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1636 if (uap->ss != NULL) {
1637 error = copyin(uap->ss, &ss, sizeof(ss));
1641 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1642 (uap->oss != NULL) ? &oss : NULL);
1645 if (uap->oss != NULL)
1646 error = copyout(&oss, uap->oss, sizeof(stack_t));
1651 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1653 struct proc *p = td->td_proc;
1656 oonstack = sigonstack(cpu_getstack(td));
1659 *oss = td->td_sigstk;
1660 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1661 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1667 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1669 if (!(ss->ss_flags & SS_DISABLE)) {
1670 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1673 td->td_sigstk = *ss;
1674 td->td_pflags |= TDP_ALTSTACK;
1676 td->td_pflags &= ~TDP_ALTSTACK;
1682 struct killpg1_ctx {
1692 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1696 if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1697 (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1700 err = p_cansignal(arg->td, p, arg->sig);
1701 if (err == 0 && arg->sig != 0)
1702 pksignal(p, arg->sig, arg->ksi);
1708 else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1713 * Common code for kill process group/broadcast kill.
1714 * cp is calling process.
1717 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1721 struct killpg1_ctx arg;
1733 sx_slock(&allproc_lock);
1734 FOREACH_PROC_IN_SYSTEM(p) {
1735 killpg1_sendsig(p, true, &arg);
1737 sx_sunlock(&allproc_lock);
1739 sx_slock(&proctree_lock);
1742 * zero pgid means send to my process group.
1744 pgrp = td->td_proc->p_pgrp;
1747 pgrp = pgfind(pgid);
1749 sx_sunlock(&proctree_lock);
1753 sx_sunlock(&proctree_lock);
1754 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1755 killpg1_sendsig(p, false, &arg);
1759 MPASS(arg.ret != 0 || arg.found || !arg.sent);
1760 if (arg.ret == 0 && !arg.sent)
1761 arg.ret = arg.found ? EPERM : ESRCH;
1765 #ifndef _SYS_SYSPROTO_H_
1773 sys_kill(struct thread *td, struct kill_args *uap)
1776 return (kern_kill(td, uap->pid, uap->signum));
1780 kern_kill(struct thread *td, pid_t pid, int signum)
1787 * A process in capability mode can send signals only to himself.
1788 * The main rationale behind this is that abort(3) is implemented as
1789 * kill(getpid(), SIGABRT).
1791 if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1794 AUDIT_ARG_SIGNUM(signum);
1796 if ((u_int)signum > _SIG_MAXSIG)
1799 ksiginfo_init(&ksi);
1800 ksi.ksi_signo = signum;
1801 ksi.ksi_code = SI_USER;
1802 ksi.ksi_pid = td->td_proc->p_pid;
1803 ksi.ksi_uid = td->td_ucred->cr_ruid;
1806 /* kill single process */
1807 if ((p = pfind_any(pid)) == NULL)
1809 AUDIT_ARG_PROCESS(p);
1810 error = p_cansignal(td, p, signum);
1811 if (error == 0 && signum)
1812 pksignal(p, signum, &ksi);
1817 case -1: /* broadcast signal */
1818 return (killpg1(td, signum, 0, 1, &ksi));
1819 case 0: /* signal own process group */
1820 return (killpg1(td, signum, 0, 0, &ksi));
1821 default: /* negative explicit process group */
1822 return (killpg1(td, signum, -pid, 0, &ksi));
1828 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1833 AUDIT_ARG_SIGNUM(uap->signum);
1834 AUDIT_ARG_FD(uap->fd);
1835 if ((u_int)uap->signum > _SIG_MAXSIG)
1838 error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1841 AUDIT_ARG_PROCESS(p);
1842 error = p_cansignal(td, p, uap->signum);
1843 if (error == 0 && uap->signum)
1844 kern_psignal(p, uap->signum);
1849 #if defined(COMPAT_43)
1850 #ifndef _SYS_SYSPROTO_H_
1851 struct okillpg_args {
1858 okillpg(struct thread *td, struct okillpg_args *uap)
1862 AUDIT_ARG_SIGNUM(uap->signum);
1863 AUDIT_ARG_PID(uap->pgid);
1864 if ((u_int)uap->signum > _SIG_MAXSIG)
1867 ksiginfo_init(&ksi);
1868 ksi.ksi_signo = uap->signum;
1869 ksi.ksi_code = SI_USER;
1870 ksi.ksi_pid = td->td_proc->p_pid;
1871 ksi.ksi_uid = td->td_ucred->cr_ruid;
1872 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1874 #endif /* COMPAT_43 */
1876 #ifndef _SYS_SYSPROTO_H_
1877 struct sigqueue_args {
1880 /* union sigval */ void *value;
1884 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1888 sv.sival_ptr = uap->value;
1890 return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1894 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1900 if ((u_int)signum > _SIG_MAXSIG)
1904 * Specification says sigqueue can only send signal to
1910 if ((p = pfind_any(pid)) == NULL)
1912 error = p_cansignal(td, p, signum);
1913 if (error == 0 && signum != 0) {
1914 ksiginfo_init(&ksi);
1915 ksi.ksi_flags = KSI_SIGQ;
1916 ksi.ksi_signo = signum;
1917 ksi.ksi_code = SI_QUEUE;
1918 ksi.ksi_pid = td->td_proc->p_pid;
1919 ksi.ksi_uid = td->td_ucred->cr_ruid;
1920 ksi.ksi_value = *value;
1921 error = pksignal(p, ksi.ksi_signo, &ksi);
1928 * Send a signal to a process group.
1931 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1936 sx_slock(&proctree_lock);
1937 pgrp = pgfind(pgid);
1938 sx_sunlock(&proctree_lock);
1940 pgsignal(pgrp, sig, 0, ksi);
1947 * Send a signal to a process group. If checktty is 1,
1948 * limit to members which have a controlling terminal.
1951 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1956 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1957 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1959 if (p->p_state == PRS_NORMAL &&
1960 (checkctty == 0 || p->p_flag & P_CONTROLT))
1961 pksignal(p, sig, ksi);
1969 * Recalculate the signal mask and reset the signal disposition after
1970 * usermode frame for delivery is formed. Should be called after
1971 * mach-specific routine, because sysent->sv_sendsig() needs correct
1972 * ps_siginfo and signal mask.
1975 postsig_done(int sig, struct thread *td, struct sigacts *ps)
1979 mtx_assert(&ps->ps_mtx, MA_OWNED);
1980 td->td_ru.ru_nsignals++;
1981 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1982 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1983 SIGADDSET(mask, sig);
1984 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1985 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1986 if (SIGISMEMBER(ps->ps_sigreset, sig))
1992 * Send a signal caused by a trap to the current thread. If it will be
1993 * caught immediately, deliver it with correct code. Otherwise, post it
1997 trapsignal(struct thread *td, ksiginfo_t *ksi)
2005 sig = ksi->ksi_signo;
2006 code = ksi->ksi_code;
2007 KASSERT(_SIG_VALID(sig), ("invalid signal"));
2011 mtx_lock(&ps->ps_mtx);
2012 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2013 !SIGISMEMBER(td->td_sigmask, sig)) {
2015 if (KTRPOINT(curthread, KTR_PSIG))
2016 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2017 &td->td_sigmask, code);
2019 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2020 ksi, &td->td_sigmask);
2021 postsig_done(sig, td, ps);
2022 mtx_unlock(&ps->ps_mtx);
2025 * Avoid a possible infinite loop if the thread
2026 * masking the signal or process is ignoring the
2029 if (kern_forcesigexit &&
2030 (SIGISMEMBER(td->td_sigmask, sig) ||
2031 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2032 SIGDELSET(td->td_sigmask, sig);
2033 SIGDELSET(ps->ps_sigcatch, sig);
2034 SIGDELSET(ps->ps_sigignore, sig);
2035 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2037 mtx_unlock(&ps->ps_mtx);
2038 p->p_sig = sig; /* XXX to verify code */
2039 tdsendsignal(p, td, sig, ksi);
2044 static struct thread *
2045 sigtd(struct proc *p, int sig, int prop)
2047 struct thread *td, *signal_td;
2049 PROC_LOCK_ASSERT(p, MA_OWNED);
2052 * Check if current thread can handle the signal without
2053 * switching context to another thread.
2055 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
2058 FOREACH_THREAD_IN_PROC(p, td) {
2059 if (!SIGISMEMBER(td->td_sigmask, sig)) {
2064 if (signal_td == NULL)
2065 signal_td = FIRST_THREAD_IN_PROC(p);
2070 * Send the signal to the process. If the signal has an action, the action
2071 * is usually performed by the target process rather than the caller; we add
2072 * the signal to the set of pending signals for the process.
2075 * o When a stop signal is sent to a sleeping process that takes the
2076 * default action, the process is stopped without awakening it.
2077 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2078 * regardless of the signal action (eg, blocked or ignored).
2080 * Other ignored signals are discarded immediately.
2082 * NB: This function may be entered from the debugger via the "kill" DDB
2083 * command. There is little that can be done to mitigate the possibly messy
2084 * side effects of this unwise possibility.
2087 kern_psignal(struct proc *p, int sig)
2091 ksiginfo_init(&ksi);
2092 ksi.ksi_signo = sig;
2093 ksi.ksi_code = SI_KERNEL;
2094 (void) tdsendsignal(p, NULL, sig, &ksi);
2098 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2101 return (tdsendsignal(p, NULL, sig, ksi));
2104 /* Utility function for finding a thread to send signal event to. */
2106 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
2110 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2111 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2123 tdsignal(struct thread *td, int sig)
2127 ksiginfo_init(&ksi);
2128 ksi.ksi_signo = sig;
2129 ksi.ksi_code = SI_KERNEL;
2130 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2134 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2137 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2141 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2144 sigqueue_t *sigqueue;
2151 MPASS(td == NULL || p == td->td_proc);
2152 PROC_LOCK_ASSERT(p, MA_OWNED);
2154 if (!_SIG_VALID(sig))
2155 panic("%s(): invalid signal %d", __func__, sig);
2157 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2160 * IEEE Std 1003.1-2001: return success when killing a zombie.
2162 if (p->p_state == PRS_ZOMBIE) {
2163 if (ksi && (ksi->ksi_flags & KSI_INS))
2164 ksiginfo_tryfree(ksi);
2169 KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2170 prop = sigprop(sig);
2173 td = sigtd(p, sig, prop);
2174 sigqueue = &p->p_sigqueue;
2176 sigqueue = &td->td_sigqueue;
2178 SDT_PROBE3(proc, , , signal__send, td, p, sig);
2181 * If the signal is being ignored,
2182 * then we forget about it immediately.
2183 * (Note: we don't set SIGCONT in ps_sigignore,
2184 * and if it is set to SIG_IGN,
2185 * action will be SIG_DFL here.)
2187 mtx_lock(&ps->ps_mtx);
2188 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2189 SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2191 mtx_unlock(&ps->ps_mtx);
2192 if (ksi && (ksi->ksi_flags & KSI_INS))
2193 ksiginfo_tryfree(ksi);
2196 if (SIGISMEMBER(td->td_sigmask, sig))
2198 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2202 if (SIGISMEMBER(ps->ps_sigintr, sig))
2206 mtx_unlock(&ps->ps_mtx);
2208 if (prop & SIGPROP_CONT)
2209 sigqueue_delete_stopmask_proc(p);
2210 else if (prop & SIGPROP_STOP) {
2212 * If sending a tty stop signal to a member of an orphaned
2213 * process group, discard the signal here if the action
2214 * is default; don't stop the process below if sleeping,
2215 * and don't clear any pending SIGCONT.
2217 if ((prop & SIGPROP_TTYSTOP) &&
2218 (p->p_pgrp->pg_jobc == 0) &&
2219 (action == SIG_DFL)) {
2220 if (ksi && (ksi->ksi_flags & KSI_INS))
2221 ksiginfo_tryfree(ksi);
2224 sigqueue_delete_proc(p, SIGCONT);
2225 if (p->p_flag & P_CONTINUED) {
2226 p->p_flag &= ~P_CONTINUED;
2227 PROC_LOCK(p->p_pptr);
2228 sigqueue_take(p->p_ksi);
2229 PROC_UNLOCK(p->p_pptr);
2233 ret = sigqueue_add(sigqueue, sig, ksi);
2238 * Defer further processing for signals which are held,
2239 * except that stopped processes must be continued by SIGCONT.
2241 if (action == SIG_HOLD &&
2242 !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2245 /* SIGKILL: Remove procfs STOPEVENTs. */
2246 if (sig == SIGKILL) {
2247 /* from procfs_ioctl.c: PIOCBIC */
2249 /* from procfs_ioctl.c: PIOCCONT */
2256 * Some signals have a process-wide effect and a per-thread
2257 * component. Most processing occurs when the process next
2258 * tries to cross the user boundary, however there are some
2259 * times when processing needs to be done immediately, such as
2260 * waking up threads so that they can cross the user boundary.
2261 * We try to do the per-process part here.
2263 if (P_SHOULDSTOP(p)) {
2264 KASSERT(!(p->p_flag & P_WEXIT),
2265 ("signal to stopped but exiting process"));
2266 if (sig == SIGKILL) {
2268 * If traced process is already stopped,
2269 * then no further action is necessary.
2271 if (p->p_flag & P_TRACED)
2274 * SIGKILL sets process running.
2275 * It will die elsewhere.
2276 * All threads must be restarted.
2278 p->p_flag &= ~P_STOPPED_SIG;
2282 if (prop & SIGPROP_CONT) {
2284 * If traced process is already stopped,
2285 * then no further action is necessary.
2287 if (p->p_flag & P_TRACED)
2290 * If SIGCONT is default (or ignored), we continue the
2291 * process but don't leave the signal in sigqueue as
2292 * it has no further action. If SIGCONT is held, we
2293 * continue the process and leave the signal in
2294 * sigqueue. If the process catches SIGCONT, let it
2295 * handle the signal itself. If it isn't waiting on
2296 * an event, it goes back to run state.
2297 * Otherwise, process goes back to sleep state.
2299 p->p_flag &= ~P_STOPPED_SIG;
2301 if (p->p_numthreads == p->p_suspcount) {
2303 p->p_flag |= P_CONTINUED;
2304 p->p_xsig = SIGCONT;
2305 PROC_LOCK(p->p_pptr);
2306 childproc_continued(p);
2307 PROC_UNLOCK(p->p_pptr);
2310 if (action == SIG_DFL) {
2311 thread_unsuspend(p);
2313 sigqueue_delete(sigqueue, sig);
2316 if (action == SIG_CATCH) {
2318 * The process wants to catch it so it needs
2319 * to run at least one thread, but which one?
2325 * The signal is not ignored or caught.
2327 thread_unsuspend(p);
2332 if (prop & SIGPROP_STOP) {
2334 * If traced process is already stopped,
2335 * then no further action is necessary.
2337 if (p->p_flag & P_TRACED)
2340 * Already stopped, don't need to stop again
2341 * (If we did the shell could get confused).
2342 * Just make sure the signal STOP bit set.
2344 p->p_flag |= P_STOPPED_SIG;
2345 sigqueue_delete(sigqueue, sig);
2350 * All other kinds of signals:
2351 * If a thread is sleeping interruptibly, simulate a
2352 * wakeup so that when it is continued it will be made
2353 * runnable and can look at the signal. However, don't make
2354 * the PROCESS runnable, leave it stopped.
2355 * It may run a bit until it hits a thread_suspend_check().
2359 if (TD_CAN_ABORT(td))
2360 wakeup_swapper = sleepq_abort(td, intrval);
2366 * Mutexes are short lived. Threads waiting on them will
2367 * hit thread_suspend_check() soon.
2369 } else if (p->p_state == PRS_NORMAL) {
2370 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2371 tdsigwakeup(td, sig, action, intrval);
2375 MPASS(action == SIG_DFL);
2377 if (prop & SIGPROP_STOP) {
2378 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2380 p->p_flag |= P_STOPPED_SIG;
2383 wakeup_swapper = sig_suspend_threads(td, p, 1);
2384 if (p->p_numthreads == p->p_suspcount) {
2386 * only thread sending signal to another
2387 * process can reach here, if thread is sending
2388 * signal to its process, because thread does
2389 * not suspend itself here, p_numthreads
2390 * should never be equal to p_suspcount.
2394 sigqueue_delete_proc(p, p->p_xsig);
2400 /* Not in "NORMAL" state. discard the signal. */
2401 sigqueue_delete(sigqueue, sig);
2406 * The process is not stopped so we need to apply the signal to all the
2410 tdsigwakeup(td, sig, action, intrval);
2412 thread_unsuspend(p);
2415 /* If we jump here, proc slock should not be owned. */
2416 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2424 * The force of a signal has been directed against a single
2425 * thread. We need to see what we can do about knocking it
2426 * out of any sleep it may be in etc.
2429 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2431 struct proc *p = td->td_proc;
2432 int prop, wakeup_swapper;
2434 PROC_LOCK_ASSERT(p, MA_OWNED);
2435 prop = sigprop(sig);
2440 * Bring the priority of a thread up if we want it to get
2441 * killed in this lifetime. Be careful to avoid bumping the
2442 * priority of the idle thread, since we still allow to signal
2445 if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2446 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2447 sched_prio(td, PUSER);
2448 if (TD_ON_SLEEPQ(td)) {
2450 * If thread is sleeping uninterruptibly
2451 * we can't interrupt the sleep... the signal will
2452 * be noticed when the process returns through
2453 * trap() or syscall().
2455 if ((td->td_flags & TDF_SINTR) == 0)
2458 * If SIGCONT is default (or ignored) and process is
2459 * asleep, we are finished; the process should not
2462 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2465 sigqueue_delete(&p->p_sigqueue, sig);
2467 * It may be on either list in this state.
2468 * Remove from both for now.
2470 sigqueue_delete(&td->td_sigqueue, sig);
2475 * Don't awaken a sleeping thread for SIGSTOP if the
2476 * STOP signal is deferred.
2478 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2479 TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2483 * Give low priority threads a better chance to run.
2485 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2486 sched_prio(td, PUSER);
2488 wakeup_swapper = sleepq_abort(td, intrval);
2496 * Other states do nothing with the signal immediately,
2497 * other than kicking ourselves if we are running.
2498 * It will either never be noticed, or noticed very soon.
2501 if (TD_IS_RUNNING(td) && td != curthread)
2511 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2516 PROC_LOCK_ASSERT(p, MA_OWNED);
2517 PROC_SLOCK_ASSERT(p, MA_OWNED);
2518 MPASS(sending || td == curthread);
2521 FOREACH_THREAD_IN_PROC(p, td2) {
2523 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2524 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2525 (td2->td_flags & TDF_SINTR)) {
2526 if (td2->td_flags & TDF_SBDRY) {
2528 * Once a thread is asleep with
2529 * TDF_SBDRY and without TDF_SERESTART
2530 * or TDF_SEINTR set, it should never
2531 * become suspended due to this check.
2533 KASSERT(!TD_IS_SUSPENDED(td2),
2534 ("thread with deferred stops suspended"));
2535 if (TD_SBDRY_INTR(td2)) {
2536 wakeup_swapper |= sleepq_abort(td2,
2537 TD_SBDRY_ERRNO(td2));
2540 } else if (!TD_IS_SUSPENDED(td2))
2541 thread_suspend_one(td2);
2542 } else if (!TD_IS_SUSPENDED(td2)) {
2543 if (sending || td != td2)
2544 td2->td_flags |= TDF_ASTPENDING;
2546 if (TD_IS_RUNNING(td2) && td2 != td)
2547 forward_signal(td2);
2552 return (wakeup_swapper);
2556 * Stop the process for an event deemed interesting to the debugger. If si is
2557 * non-NULL, this is a signal exchange; the new signal requested by the
2558 * debugger will be returned for handling. If si is NULL, this is some other
2559 * type of interesting event. The debugger may request a signal be delivered in
2560 * that case as well, however it will be deferred until it can be handled.
2563 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2565 struct proc *p = td->td_proc;
2570 PROC_LOCK_ASSERT(p, MA_OWNED);
2571 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2572 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2573 &p->p_mtx.lock_object, "Stopping for traced signal");
2577 if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2578 td->td_dbgflags |= TDB_XSIG;
2579 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2580 td->td_tid, p->p_pid, td->td_dbgflags, sig);
2582 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2585 * Ensure that, if we've been PT_KILLed, the
2586 * exit status reflects that. Another thread
2587 * may also be in ptracestop(), having just
2588 * received the SIGKILL, but this thread was
2589 * unsuspended first.
2591 td->td_dbgflags &= ~TDB_XSIG;
2592 td->td_xsig = SIGKILL;
2596 if (p->p_flag & P_SINGLE_EXIT &&
2597 !(td->td_dbgflags & TDB_EXIT)) {
2599 * Ignore ptrace stops except for thread exit
2600 * events when the process exits.
2602 td->td_dbgflags &= ~TDB_XSIG;
2608 * Make wait(2) work. Ensure that right after the
2609 * attach, the thread which was decided to become the
2610 * leader of attach gets reported to the waiter.
2611 * Otherwise, just avoid overwriting another thread's
2612 * assignment to p_xthread. If another thread has
2613 * already set p_xthread, the current thread will get
2614 * a chance to report itself upon the next iteration.
2616 if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2617 ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2618 p->p_xthread == NULL)) {
2623 * If we are on sleepqueue already,
2624 * let sleepqueue code decide if it
2625 * needs to go sleep after attach.
2627 if (td->td_wchan == NULL)
2628 td->td_dbgflags &= ~TDB_FSTP;
2630 p->p_flag2 &= ~P2_PTRACE_FSTP;
2631 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2632 sig_suspend_threads(td, p, 0);
2634 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2635 td->td_dbgflags &= ~TDB_STOPATFORK;
2638 thread_suspend_switch(td, p);
2639 if (p->p_xthread == td)
2640 p->p_xthread = NULL;
2641 if (!(p->p_flag & P_TRACED))
2643 if (td->td_dbgflags & TDB_SUSPEND) {
2644 if (p->p_flag & P_SINGLE_EXIT)
2652 if (si != NULL && sig == td->td_xsig) {
2653 /* Parent wants us to take the original signal unchanged. */
2654 si->ksi_flags |= KSI_HEAD;
2655 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2657 } else if (td->td_xsig != 0) {
2659 * If parent wants us to take a new signal, then it will leave
2660 * it in td->td_xsig; otherwise we just look for signals again.
2662 ksiginfo_init(&ksi);
2663 ksi.ksi_signo = td->td_xsig;
2664 ksi.ksi_flags |= KSI_PTRACE;
2665 prop = sigprop(td->td_xsig);
2666 td2 = sigtd(p, td->td_xsig, prop);
2667 tdsendsignal(p, td2, td->td_xsig, &ksi);
2672 return (td->td_xsig);
2676 reschedule_signals(struct proc *p, sigset_t block, int flags)
2682 PROC_LOCK_ASSERT(p, MA_OWNED);
2684 mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ?
2685 MA_OWNED : MA_NOTOWNED);
2686 if (SIGISEMPTY(p->p_siglist))
2688 SIGSETAND(block, p->p_siglist);
2689 while ((sig = sig_ffs(&block)) != 0) {
2690 SIGDELSET(block, sig);
2691 td = sigtd(p, sig, 0);
2693 if (!(flags & SIGPROCMASK_PS_LOCKED))
2694 mtx_lock(&ps->ps_mtx);
2695 if (p->p_flag & P_TRACED ||
2696 (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2697 !SIGISMEMBER(td->td_sigmask, sig)))
2698 tdsigwakeup(td, sig, SIG_CATCH,
2699 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2701 if (!(flags & SIGPROCMASK_PS_LOCKED))
2702 mtx_unlock(&ps->ps_mtx);
2707 tdsigcleanup(struct thread *td)
2713 PROC_LOCK_ASSERT(p, MA_OWNED);
2715 sigqueue_flush(&td->td_sigqueue);
2716 if (p->p_numthreads == 1)
2720 * Since we cannot handle signals, notify signal post code
2721 * about this by filling the sigmask.
2723 * Also, if needed, wake up thread(s) that do not block the
2724 * same signals as the exiting thread, since the thread might
2725 * have been selected for delivery and woken up.
2727 SIGFILLSET(unblocked);
2728 SIGSETNAND(unblocked, td->td_sigmask);
2729 SIGFILLSET(td->td_sigmask);
2730 reschedule_signals(p, unblocked, 0);
2735 sigdeferstop_curr_flags(int cflags)
2738 MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
2739 (cflags & TDF_SBDRY) != 0);
2740 return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2744 * Defer the delivery of SIGSTOP for the current thread, according to
2745 * the requested mode. Returns previous flags, which must be restored
2746 * by sigallowstop().
2748 * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
2749 * cleared by the current thread, which allow the lock-less read-only
2753 sigdeferstop_impl(int mode)
2759 cflags = sigdeferstop_curr_flags(td->td_flags);
2761 case SIGDEFERSTOP_NOP:
2764 case SIGDEFERSTOP_OFF:
2767 case SIGDEFERSTOP_SILENT:
2768 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
2770 case SIGDEFERSTOP_EINTR:
2771 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
2773 case SIGDEFERSTOP_ERESTART:
2774 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
2777 panic("sigdeferstop: invalid mode %x", mode);
2780 if (cflags == nflags)
2781 return (SIGDEFERSTOP_VAL_NCHG);
2783 td->td_flags = (td->td_flags & ~cflags) | nflags;
2789 * Restores the STOP handling mode, typically permitting the delivery
2790 * of SIGSTOP for the current thread. This does not immediately
2791 * suspend if a stop was posted. Instead, the thread will suspend
2792 * either via ast() or a subsequent interruptible sleep.
2795 sigallowstop_impl(int prev)
2800 KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
2801 KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
2802 ("sigallowstop: incorrect previous mode %x", prev));
2804 cflags = sigdeferstop_curr_flags(td->td_flags);
2805 if (cflags != prev) {
2807 td->td_flags = (td->td_flags & ~cflags) | prev;
2813 * If the current process has received a signal (should be caught or cause
2814 * termination, should interrupt current syscall), return the signal number.
2815 * Stop signals with default action are processed immediately, then cleared;
2816 * they aren't returned. This is checked after each entry to the system for
2817 * a syscall or trap (though this can usually be done without calling issignal
2818 * by checking the pending signal masks in cursig.) The normal call
2821 * while (sig = cursig(curthread))
2825 issignal(struct thread *td)
2829 struct sigqueue *queue;
2830 sigset_t sigpending;
2832 int prop, sig, traced;
2836 mtx_assert(&ps->ps_mtx, MA_OWNED);
2837 PROC_LOCK_ASSERT(p, MA_OWNED);
2839 traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2841 sigpending = td->td_sigqueue.sq_signals;
2842 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2843 SIGSETNAND(sigpending, td->td_sigmask);
2845 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
2846 (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2847 SIG_STOPSIGMASK(sigpending);
2848 if (SIGISEMPTY(sigpending)) /* no signal to send */
2850 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
2851 (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
2852 SIGISMEMBER(sigpending, SIGSTOP)) {
2854 * If debugger just attached, always consume
2855 * SIGSTOP from ptrace(PT_ATTACH) first, to
2856 * execute the debugger attach ritual in
2860 td->td_dbgflags |= TDB_FSTP;
2862 sig = sig_ffs(&sigpending);
2865 if (p->p_stops & S_SIG) {
2866 mtx_unlock(&ps->ps_mtx);
2867 stopevent(p, S_SIG, sig);
2868 mtx_lock(&ps->ps_mtx);
2872 * We should see pending but ignored signals
2873 * only if P_TRACED was on when they were posted.
2875 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2876 sigqueue_delete(&td->td_sigqueue, sig);
2877 sigqueue_delete(&p->p_sigqueue, sig);
2880 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
2882 * If traced, always stop.
2883 * Remove old signal from queue before the stop.
2884 * XXX shrug off debugger, it causes siginfo to
2887 queue = &td->td_sigqueue;
2888 ksiginfo_init(&ksi);
2889 if (sigqueue_get(queue, sig, &ksi) == 0) {
2890 queue = &p->p_sigqueue;
2891 sigqueue_get(queue, sig, &ksi);
2893 td->td_si = ksi.ksi_info;
2895 mtx_unlock(&ps->ps_mtx);
2896 sig = ptracestop(td, sig, &ksi);
2897 mtx_lock(&ps->ps_mtx);
2899 td->td_si.si_signo = 0;
2902 * Keep looking if the debugger discarded or
2903 * replaced the signal.
2909 * If the signal became masked, re-queue it.
2911 if (SIGISMEMBER(td->td_sigmask, sig)) {
2912 ksi.ksi_flags |= KSI_HEAD;
2913 sigqueue_add(&p->p_sigqueue, sig, &ksi);
2918 * If the traced bit got turned off, requeue
2919 * the signal and go back up to the top to
2920 * rescan signals. This ensures that p_sig*
2921 * and p_sigact are consistent.
2923 if ((p->p_flag & P_TRACED) == 0) {
2924 ksi.ksi_flags |= KSI_HEAD;
2925 sigqueue_add(queue, sig, &ksi);
2930 prop = sigprop(sig);
2933 * Decide whether the signal should be returned.
2934 * Return the signal's number, or fall through
2935 * to clear it from the pending mask.
2937 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2939 case (intptr_t)SIG_DFL:
2941 * Don't take default actions on system processes.
2943 if (p->p_pid <= 1) {
2946 * Are you sure you want to ignore SIGSEGV
2949 printf("Process (pid %lu) got signal %d\n",
2950 (u_long)p->p_pid, sig);
2952 break; /* == ignore */
2955 * If there is a pending stop signal to process with
2956 * default action, stop here, then clear the signal.
2957 * Traced or exiting processes should ignore stops.
2958 * Additionally, a member of an orphaned process group
2959 * should ignore tty stops.
2961 if (prop & SIGPROP_STOP) {
2963 (P_TRACED | P_WEXIT | P_SINGLE_EXIT) ||
2964 (p->p_pgrp->pg_jobc == 0 &&
2965 prop & SIGPROP_TTYSTOP))
2966 break; /* == ignore */
2967 if (TD_SBDRY_INTR(td)) {
2968 KASSERT((td->td_flags & TDF_SBDRY) != 0,
2969 ("lost TDF_SBDRY"));
2972 mtx_unlock(&ps->ps_mtx);
2973 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2974 &p->p_mtx.lock_object, "Catching SIGSTOP");
2975 sigqueue_delete(&td->td_sigqueue, sig);
2976 sigqueue_delete(&p->p_sigqueue, sig);
2977 p->p_flag |= P_STOPPED_SIG;
2980 sig_suspend_threads(td, p, 0);
2981 thread_suspend_switch(td, p);
2983 mtx_lock(&ps->ps_mtx);
2985 } else if (prop & SIGPROP_IGNORE) {
2987 * Except for SIGCONT, shouldn't get here.
2988 * Default action is to ignore; drop it.
2990 break; /* == ignore */
2995 case (intptr_t)SIG_IGN:
2997 * Masking above should prevent us ever trying
2998 * to take action on an ignored signal other
2999 * than SIGCONT, unless process is traced.
3001 if ((prop & SIGPROP_CONT) == 0 &&
3002 (p->p_flag & P_TRACED) == 0)
3003 printf("issignal\n");
3004 break; /* == ignore */
3008 * This signal has an action, let
3009 * postsig() process it.
3013 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
3014 sigqueue_delete(&p->p_sigqueue, sig);
3021 thread_stopped(struct proc *p)
3025 PROC_LOCK_ASSERT(p, MA_OWNED);
3026 PROC_SLOCK_ASSERT(p, MA_OWNED);
3030 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3032 p->p_flag &= ~P_WAITED;
3033 PROC_LOCK(p->p_pptr);
3034 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3035 CLD_TRAPPED : CLD_STOPPED);
3036 PROC_UNLOCK(p->p_pptr);
3042 * Take the action for the specified signal
3043 * from the current set of pending signals.
3053 sigset_t returnmask;
3055 KASSERT(sig != 0, ("postsig"));
3059 PROC_LOCK_ASSERT(p, MA_OWNED);
3061 mtx_assert(&ps->ps_mtx, MA_OWNED);
3062 ksiginfo_init(&ksi);
3063 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3064 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3066 ksi.ksi_signo = sig;
3067 if (ksi.ksi_code == SI_TIMER)
3068 itimer_accept(p, ksi.ksi_timerid, &ksi);
3069 action = ps->ps_sigact[_SIG_IDX(sig)];
3071 if (KTRPOINT(td, KTR_PSIG))
3072 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3073 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3075 if ((p->p_stops & S_SIG) != 0) {
3076 mtx_unlock(&ps->ps_mtx);
3077 stopevent(p, S_SIG, sig);
3078 mtx_lock(&ps->ps_mtx);
3081 if (action == SIG_DFL) {
3083 * Default action, where the default is to kill
3084 * the process. (Other cases were ignored above.)
3086 mtx_unlock(&ps->ps_mtx);
3087 proc_td_siginfo_capture(td, &ksi.ksi_info);
3092 * If we get here, the signal must be caught.
3094 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3095 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3096 ("postsig action: blocked sig %d", sig));
3099 * Set the new mask value and also defer further
3100 * occurrences of this signal.
3102 * Special case: user has done a sigsuspend. Here the
3103 * current mask is not of interest, but rather the
3104 * mask from before the sigsuspend is what we want
3105 * restored after the signal processing is completed.
3107 if (td->td_pflags & TDP_OLDMASK) {
3108 returnmask = td->td_oldsigmask;
3109 td->td_pflags &= ~TDP_OLDMASK;
3111 returnmask = td->td_sigmask;
3113 if (p->p_sig == sig) {
3116 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3117 postsig_done(sig, td, ps);
3123 proc_wkilled(struct proc *p)
3126 PROC_LOCK_ASSERT(p, MA_OWNED);
3127 if ((p->p_flag & P_WKILLED) == 0) {
3128 p->p_flag |= P_WKILLED;
3130 * Notify swapper that there is a process to swap in.
3131 * The notification is racy, at worst it would take 10
3132 * seconds for the swapper process to notice.
3134 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3140 * Kill the current process for stated reason.
3143 killproc(struct proc *p, char *why)
3146 PROC_LOCK_ASSERT(p, MA_OWNED);
3147 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3149 log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3150 p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3151 p->p_ucred->cr_uid, why);
3153 kern_psignal(p, SIGKILL);
3157 * Force the current process to exit with the specified signal, dumping core
3158 * if appropriate. We bypass the normal tests for masked and caught signals,
3159 * allowing unrecoverable failures to terminate the process without changing
3160 * signal state. Mark the accounting record with the signal termination.
3161 * If dumping core, save the signal number for the debugger. Calls exit and
3165 sigexit(struct thread *td, int sig)
3167 struct proc *p = td->td_proc;
3169 PROC_LOCK_ASSERT(p, MA_OWNED);
3170 p->p_acflag |= AXSIG;
3172 * We must be single-threading to generate a core dump. This
3173 * ensures that the registers in the core file are up-to-date.
3174 * Also, the ELF dump handler assumes that the thread list doesn't
3175 * change out from under it.
3177 * XXX If another thread attempts to single-thread before us
3178 * (e.g. via fork()), we won't get a dump at all.
3180 if ((sigprop(sig) & SIGPROP_CORE) &&
3181 thread_single(p, SINGLE_NO_EXIT) == 0) {
3184 * Log signals which would cause core dumps
3185 * (Log as LOG_INFO to appease those who don't want
3187 * XXX : Todo, as well as euid, write out ruid too
3188 * Note that coredump() drops proc lock.
3190 if (coredump(td) == 0)
3192 if (kern_logsigexit)
3194 "pid %d (%s), jid %d, uid %d: exited on "
3195 "signal %d%s\n", p->p_pid, p->p_comm,
3196 p->p_ucred->cr_prison->pr_id,
3197 td->td_ucred->cr_uid,
3199 sig & WCOREFLAG ? " (core dumped)" : "");
3207 * Send queued SIGCHLD to parent when child process's state
3211 sigparent(struct proc *p, int reason, int status)
3213 PROC_LOCK_ASSERT(p, MA_OWNED);
3214 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3216 if (p->p_ksi != NULL) {
3217 p->p_ksi->ksi_signo = SIGCHLD;
3218 p->p_ksi->ksi_code = reason;
3219 p->p_ksi->ksi_status = status;
3220 p->p_ksi->ksi_pid = p->p_pid;
3221 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
3222 if (KSI_ONQ(p->p_ksi))
3225 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3229 childproc_jobstate(struct proc *p, int reason, int sig)
3233 PROC_LOCK_ASSERT(p, MA_OWNED);
3234 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3237 * Wake up parent sleeping in kern_wait(), also send
3238 * SIGCHLD to parent, but SIGCHLD does not guarantee
3239 * that parent will awake, because parent may masked
3242 p->p_pptr->p_flag |= P_STATCHILD;
3245 ps = p->p_pptr->p_sigacts;
3246 mtx_lock(&ps->ps_mtx);
3247 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3248 mtx_unlock(&ps->ps_mtx);
3249 sigparent(p, reason, sig);
3251 mtx_unlock(&ps->ps_mtx);
3255 childproc_stopped(struct proc *p, int reason)
3258 childproc_jobstate(p, reason, p->p_xsig);
3262 childproc_continued(struct proc *p)
3264 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3268 childproc_exited(struct proc *p)
3272 if (WCOREDUMP(p->p_xsig)) {
3273 reason = CLD_DUMPED;
3274 status = WTERMSIG(p->p_xsig);
3275 } else if (WIFSIGNALED(p->p_xsig)) {
3276 reason = CLD_KILLED;
3277 status = WTERMSIG(p->p_xsig);
3279 reason = CLD_EXITED;
3280 status = p->p_xexit;
3283 * XXX avoid calling wakeup(p->p_pptr), the work is
3286 sigparent(p, reason, status);
3289 #define MAX_NUM_CORE_FILES 100000
3290 #ifndef NUM_CORE_FILES
3291 #define NUM_CORE_FILES 5
3293 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3294 static int num_cores = NUM_CORE_FILES;
3297 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3302 new_val = num_cores;
3303 error = sysctl_handle_int(oidp, &new_val, 0, req);
3304 if (error != 0 || req->newptr == NULL)
3306 if (new_val > MAX_NUM_CORE_FILES)
3307 new_val = MAX_NUM_CORE_FILES;
3310 num_cores = new_val;
3313 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3314 0, sizeof(int), sysctl_debug_num_cores_check, "I",
3315 "Maximum number of generated process corefiles while using index format");
3317 #define GZIP_SUFFIX ".gz"
3318 #define ZSTD_SUFFIX ".zst"
3320 int compress_user_cores = 0;
3323 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3327 val = compress_user_cores;
3328 error = sysctl_handle_int(oidp, &val, 0, req);
3329 if (error != 0 || req->newptr == NULL)
3331 if (val != 0 && !compressor_avail(val))
3333 compress_user_cores = val;
3336 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores, CTLTYPE_INT | CTLFLAG_RWTUN,
3337 0, sizeof(int), sysctl_compress_user_cores, "I",
3338 "Enable compression of user corefiles ("
3339 __XSTRING(COMPRESS_GZIP) " = gzip, "
3340 __XSTRING(COMPRESS_ZSTD) " = zstd)");
3342 int compress_user_cores_level = 6;
3343 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3344 &compress_user_cores_level, 0,
3345 "Corefile compression level");
3348 * Protect the access to corefilename[] by allproc_lock.
3350 #define corefilename_lock allproc_lock
3352 static char corefilename[MAXPATHLEN] = {"%N.core"};
3353 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3356 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3360 sx_xlock(&corefilename_lock);
3361 error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3363 sx_xunlock(&corefilename_lock);
3367 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3368 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3369 "Process corefile name format string");
3372 vnode_close_locked(struct thread *td, struct vnode *vp)
3376 vn_close(vp, FWRITE, td->td_ucred, td);
3380 * If the core format has a %I in it, then we need to check
3381 * for existing corefiles before defining a name.
3382 * To do this we iterate over 0..ncores to find a
3383 * non-existing core file name to use. If all core files are
3384 * already used we choose the oldest one.
3387 corefile_open_last(struct thread *td, char *name, int indexpos,
3388 int indexlen, int ncores, struct vnode **vpp)
3390 struct vnode *oldvp, *nextvp, *vp;
3392 struct nameidata nd;
3393 int error, i, flags, oflags, cmode;
3395 struct timespec lasttime;
3397 nextvp = oldvp = NULL;
3398 cmode = S_IRUSR | S_IWUSR;
3399 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3400 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3402 for (i = 0; i < ncores; i++) {
3403 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3405 ch = name[indexpos + indexlen];
3406 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3408 name[indexpos + indexlen] = ch;
3410 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3411 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3417 NDFREE(&nd, NDF_ONLY_PNBUF);
3418 if ((flags & O_CREAT) == O_CREAT) {
3423 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3425 vnode_close_locked(td, vp);
3429 if (oldvp == NULL ||
3430 lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3431 (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3432 lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3434 vnode_close_locked(td, oldvp);
3436 lasttime = vattr.va_mtime;
3438 vnode_close_locked(td, vp);
3442 if (oldvp != NULL) {
3443 if (nextvp == NULL) {
3444 if ((td->td_proc->p_flag & P_SUGID) != 0) {
3446 vnode_close_locked(td, oldvp);
3451 vnode_close_locked(td, oldvp);
3456 vnode_close_locked(td, oldvp);
3465 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3466 * Expand the name described in corefilename, using name, uid, and pid
3467 * and open/create core file.
3468 * corefilename is a printf-like string, with three format specifiers:
3469 * %N name of process ("name")
3470 * %P process id (pid)
3472 * For example, "%N.core" is the default; they can be disabled completely
3473 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3474 * This is controlled by the sysctl variable kern.corefile (see above).
3477 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3478 int compress, int signum, struct vnode **vpp, char **namep)
3481 struct nameidata nd;
3483 char *hostname, *name;
3484 int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3487 format = corefilename;
3488 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3492 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3493 sx_slock(&corefilename_lock);
3494 for (i = 0; format[i] != '\0'; i++) {
3495 switch (format[i]) {
3496 case '%': /* Format character */
3498 switch (format[i]) {
3500 sbuf_putc(&sb, '%');
3502 case 'H': /* hostname */
3503 if (hostname == NULL) {
3504 hostname = malloc(MAXHOSTNAMELEN,
3507 getcredhostname(td->td_ucred, hostname,
3509 sbuf_printf(&sb, "%s", hostname);
3511 case 'I': /* autoincrementing index */
3512 if (indexpos != -1) {
3513 sbuf_printf(&sb, "%%I");
3517 indexpos = sbuf_len(&sb);
3518 sbuf_printf(&sb, "%u", ncores - 1);
3519 indexlen = sbuf_len(&sb) - indexpos;
3521 case 'N': /* process name */
3522 sbuf_printf(&sb, "%s", comm);
3524 case 'P': /* process id */
3525 sbuf_printf(&sb, "%u", pid);
3527 case 'S': /* signal number */
3528 sbuf_printf(&sb, "%i", signum);
3530 case 'U': /* user id */
3531 sbuf_printf(&sb, "%u", uid);
3535 "Unknown format character %c in "
3536 "corename `%s'\n", format[i], format);
3541 sbuf_putc(&sb, format[i]);
3545 sx_sunlock(&corefilename_lock);
3546 free(hostname, M_TEMP);
3547 if (compress == COMPRESS_GZIP)
3548 sbuf_printf(&sb, GZIP_SUFFIX);
3549 else if (compress == COMPRESS_ZSTD)
3550 sbuf_printf(&sb, ZSTD_SUFFIX);
3551 if (sbuf_error(&sb) != 0) {
3552 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3553 "long\n", (long)pid, comm, (u_long)uid);
3561 if (indexpos != -1) {
3562 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3566 "pid %d (%s), uid (%u): Path `%s' failed "
3567 "on initial open test, error = %d\n",
3568 pid, comm, uid, name, error);
3571 cmode = S_IRUSR | S_IWUSR;
3572 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3573 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3574 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3575 if ((td->td_proc->p_flag & P_SUGID) != 0)
3578 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3579 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3583 NDFREE(&nd, NDF_ONLY_PNBUF);
3589 audit_proc_coredump(td, name, error);
3599 * Dump a process' core. The main routine does some
3600 * policy checking, and creates the name of the coredump;
3601 * then it passes on a vnode and a size limit to the process-specific
3602 * coredump routine if there is one; if there _is not_ one, it returns
3603 * ENOSYS; otherwise it returns the error from the process-specific routine.
3607 coredump(struct thread *td)
3609 struct proc *p = td->td_proc;
3610 struct ucred *cred = td->td_ucred;
3614 int error, error1, locked;
3615 char *name; /* name of corefile */
3618 char *fullpath, *freepath = NULL;
3621 PROC_LOCK_ASSERT(p, MA_OWNED);
3622 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3623 _STOPEVENT(p, S_CORE, 0);
3625 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3626 (p->p_flag2 & P2_NOTRACE) != 0) {
3632 * Note that the bulk of limit checking is done after
3633 * the corefile is created. The exception is if the limit
3634 * for corefiles is 0, in which case we don't bother
3635 * creating the corefile at all. This layout means that
3636 * a corefile is truncated instead of not being created,
3637 * if it is larger than the limit.
3639 limit = (off_t)lim_cur(td, RLIMIT_CORE);
3640 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3646 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
3647 compress_user_cores, p->p_sig, &vp, &name);
3652 * Don't dump to non-regular files or files with links.
3653 * Do not dump into system files. Effective user must own the corefile.
3655 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3656 vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
3657 vattr.va_uid != cred->cr_uid) {
3665 /* Postpone other writers, including core dumps of other processes. */
3666 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3668 lf.l_whence = SEEK_SET;
3671 lf.l_type = F_WRLCK;
3672 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3676 if (set_core_nodump_flag)
3677 vattr.va_flags = UF_NODUMP;
3678 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3679 VOP_SETATTR(vp, &vattr, cred);
3682 p->p_acflag |= ACORE;
3685 if (p->p_sysent->sv_coredump != NULL) {
3686 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
3692 lf.l_type = F_UNLCK;
3693 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3695 vn_rangelock_unlock(vp, rl_cookie);
3698 * Notify the userland helper that a process triggered a core dump.
3699 * This allows the helper to run an automated debugging session.
3701 if (error != 0 || coredump_devctl == 0)
3703 sb = sbuf_new_auto();
3704 if (vn_fullpath_global(td, p->p_textvp, &fullpath, &freepath) != 0)
3706 sbuf_printf(sb, "comm=\"");
3707 devctl_safe_quote_sb(sb, fullpath);
3708 free(freepath, M_TEMP);
3709 sbuf_printf(sb, "\" core=\"");
3712 * We can't lookup core file vp directly. When we're replacing a core, and
3713 * other random times, we flush the name cache, so it will fail. Instead,
3714 * if the path of the core is relative, add the current dir in front if it.
3716 if (name[0] != '/') {
3717 fullpath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
3718 if (kern___getcwd(td, fullpath, UIO_SYSSPACE, MAXPATHLEN, MAXPATHLEN) != 0) {
3719 free(fullpath, M_TEMP);
3722 devctl_safe_quote_sb(sb, fullpath);
3723 free(fullpath, M_TEMP);
3726 devctl_safe_quote_sb(sb, name);
3727 sbuf_printf(sb, "\"");
3728 if (sbuf_finish(sb) == 0)
3729 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
3733 error1 = vn_close(vp, FWRITE, cred, td);
3737 audit_proc_coredump(td, name, error);
3744 * Nonexistent system call-- signal process (may want to handle it). Flag
3745 * error in case process won't see signal immediately (blocked or ignored).
3747 #ifndef _SYS_SYSPROTO_H_
3754 nosys(struct thread *td, struct nosys_args *args)
3761 tdsignal(td, SIGSYS);
3763 if (kern_lognosys == 1 || kern_lognosys == 3) {
3764 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3767 if (kern_lognosys == 2 || kern_lognosys == 3) {
3768 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
3775 * Send a SIGIO or SIGURG signal to a process or process group using stored
3776 * credentials rather than those of the current process.
3779 pgsigio(struct sigio **sigiop, int sig, int checkctty)
3782 struct sigio *sigio;
3784 ksiginfo_init(&ksi);
3785 ksi.ksi_signo = sig;
3786 ksi.ksi_code = SI_KERNEL;
3790 if (sigio == NULL) {
3794 if (sigio->sio_pgid > 0) {
3795 PROC_LOCK(sigio->sio_proc);
3796 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3797 kern_psignal(sigio->sio_proc, sig);
3798 PROC_UNLOCK(sigio->sio_proc);
3799 } else if (sigio->sio_pgid < 0) {
3802 PGRP_LOCK(sigio->sio_pgrp);
3803 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3805 if (p->p_state == PRS_NORMAL &&
3806 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3807 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3808 kern_psignal(p, sig);
3811 PGRP_UNLOCK(sigio->sio_pgrp);
3817 filt_sigattach(struct knote *kn)
3819 struct proc *p = curproc;
3821 kn->kn_ptr.p_proc = p;
3822 kn->kn_flags |= EV_CLEAR; /* automatically set */
3824 knlist_add(p->p_klist, kn, 0);
3830 filt_sigdetach(struct knote *kn)
3832 struct proc *p = kn->kn_ptr.p_proc;
3834 knlist_remove(p->p_klist, kn, 0);
3838 * signal knotes are shared with proc knotes, so we apply a mask to
3839 * the hint in order to differentiate them from process hints. This
3840 * could be avoided by using a signal-specific knote list, but probably
3841 * isn't worth the trouble.
3844 filt_signal(struct knote *kn, long hint)
3847 if (hint & NOTE_SIGNAL) {
3848 hint &= ~NOTE_SIGNAL;
3850 if (kn->kn_id == hint)
3853 return (kn->kn_data != 0);
3861 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3862 refcount_init(&ps->ps_refcnt, 1);
3863 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3868 sigacts_free(struct sigacts *ps)
3871 if (refcount_release(&ps->ps_refcnt) == 0)
3873 mtx_destroy(&ps->ps_mtx);
3874 free(ps, M_SUBPROC);
3878 sigacts_hold(struct sigacts *ps)
3881 refcount_acquire(&ps->ps_refcnt);
3886 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3889 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3890 mtx_lock(&src->ps_mtx);
3891 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3892 mtx_unlock(&src->ps_mtx);
3896 sigacts_shared(struct sigacts *ps)
3899 return (ps->ps_refcnt > 1);
3903 sig_drop_caught(struct proc *p)
3909 PROC_LOCK_ASSERT(p, MA_OWNED);
3910 mtx_assert(&ps->ps_mtx, MA_OWNED);
3911 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
3912 sig = sig_ffs(&ps->ps_sigcatch);
3914 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
3915 sigqueue_delete_proc(p, sig);