#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.413 2026/01/04 01:41:34 riastradh Exp $");
#include "opt_compat_netbsd.h"
#include "opt_compat_netbsd32.h"
#include "opt_compat_sunos.h"
#include "opt_dtrace.h"
#include "opt_execfmt.h"
#include "opt_pax.h"
#include "opt_ptrace.h"
#define SIGPROP
#include <sys/param.h>
#include <sys/types.h>
#include <sys/acct.h>
#include <sys/atomic.h>
#include <sys/callout.h>
#include <sys/compat_stub.h>
#include <sys/cpu.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kauth.h>
#include <sys/ktrace.h>
#include <sys/module.h>
#include <sys/pool.h>
#include <sys/proc.h>
#include <sys/ptrace.h>
#include <sys/sdt.h>
#include <sys/signalvar.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/ucontext.h>
#include <sys/wait.h>
#ifdef PAX_SEGVGUARD
#include <sys/pax.h>
#endif
#include <uvm/uvm_extern.h>
__CTASSERT(NSIG <= 128);
#define SIGQUEUE_MAX 32
static pool_cache_t sigacts_cache __read_mostly;
static pool_cache_t ksiginfo_cache __read_mostly;
static callout_t proc_stop_ch __cacheline_aligned;
sigset_t contsigmask __cacheline_aligned;
sigset_t stopsigmask __cacheline_aligned;
static sigset_t vforksigmask __cacheline_aligned;
sigset_t sigcantmask __cacheline_aligned;
static void proc_stop(struct proc *, int);
static void proc_stop_done(struct proc *, int);
static void proc_stop_callout(void *);
static int sigchecktrace(void);
static int sigpost(struct lwp *, sig_t, int, int);
static int sigput(sigpend_t *, struct proc *, ksiginfo_t *);
static int sigunwait(struct proc *, const ksiginfo_t *);
static void sigswitch(int, int, bool);
static void sigswitch_unlock_and_switch_away(struct lwp *);
static void sigacts_poolpage_free(struct pool *, void *);
static void *sigacts_poolpage_alloc(struct pool *, int);
SDT_PROVIDER_DECLARE(proc);
SDT_PROBE_DEFINE3(proc, kernel, , signal__send,
"struct lwp *",
"struct proc *",
"int");
SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
"struct lwp *",
"struct proc *",
"int");
SDT_PROBE_DEFINE3(proc, kernel, , signal__handle,
"int",
"ksiginfo_t *",
"void (*)(void)");
static struct pool_allocator sigactspool_allocator = {
.pa_alloc = sigacts_poolpage_alloc,
.pa_free = sigacts_poolpage_free
};
#ifdef DEBUG
int kern_logsigexit = 1;
#else
int kern_logsigexit = 0;
#endif
static const char logcoredump[] =
"pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
static const char lognocoredump[] =
"pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
static kauth_listener_t signal_listener;
static int
signal_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
void *arg0, void *arg1, void *arg2, void *arg3)
{
struct proc *p;
int result, signum;
result = KAUTH_RESULT_DEFER;
p = arg0;
signum = (int)(unsigned long)arg1;
if (action != KAUTH_PROCESS_SIGNAL)
return result;
if (kauth_cred_uidmatch(cred, p->p_cred) ||
(signum == SIGCONT && (curproc->p_session == p->p_session)))
result = KAUTH_RESULT_ALLOW;
return result;
}
static int
sigacts_ctor(void *arg __unused, void *obj, int flags __unused)
{
memset(obj, 0, sizeof(struct sigacts));
return 0;
}
void
signal_init(void)
{
sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0,
"sigacts", sizeof(struct sigacts) > PAGE_SIZE ?
&sigactspool_allocator : NULL, IPL_NONE, sigacts_ctor, NULL, NULL);
ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0,
"ksiginfo", NULL, IPL_VM, NULL, NULL, NULL);
callout_init(&proc_stop_ch, CALLOUT_MPSAFE);
callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
signal_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
signal_listener_cb, NULL);
}
static void *
sigacts_poolpage_alloc(struct pool *pp, int flags)
{
return (void *)uvm_km_alloc(kernel_map,
PAGE_SIZE * 2, PAGE_SIZE * 2,
((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
| UVM_KMF_WIRED);
}
static void
sigacts_poolpage_free(struct pool *pp, void *v)
{
uvm_km_free(kernel_map, (vaddr_t)v, PAGE_SIZE * 2, UVM_KMF_WIRED);
}
struct sigacts *
sigactsinit(struct proc *pp, int share)
{
struct sigacts *ps = pp->p_sigacts, *ps2;
if (__predict_false(share)) {
atomic_inc_uint(&ps->sa_refcnt);
return ps;
}
ps2 = pool_cache_get(sigacts_cache, PR_WAITOK);
mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
ps2->sa_refcnt = 1;
mutex_enter(&ps->sa_mutex);
memcpy(ps2->sa_sigdesc, ps->sa_sigdesc, sizeof(ps2->sa_sigdesc));
mutex_exit(&ps->sa_mutex);
return ps2;
}
void
sigactsunshare(struct proc *p)
{
struct sigacts *ps, *oldps = p->p_sigacts;
if (__predict_true(oldps->sa_refcnt == 1))
return;
ps = pool_cache_get(sigacts_cache, PR_WAITOK);
mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
memcpy(ps->sa_sigdesc, oldps->sa_sigdesc, sizeof(ps->sa_sigdesc));
ps->sa_refcnt = 1;
p->p_sigacts = ps;
sigactsfree(oldps);
}
void
sigactsfree(struct sigacts *ps)
{
membar_release();
if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) {
membar_acquire();
mutex_destroy(&ps->sa_mutex);
pool_cache_put(sigacts_cache, ps);
}
}
void
siginit(struct proc *p)
{
struct lwp *l;
struct sigacts *ps;
int signo, prop;
ps = p->p_sigacts;
sigemptyset(&contsigmask);
sigemptyset(&stopsigmask);
sigemptyset(&vforksigmask);
sigemptyset(&sigcantmask);
for (signo = 1; signo < NSIG; signo++) {
prop = sigprop[signo];
if (prop & SA_CONT)
sigaddset(&contsigmask, signo);
if (prop & SA_STOP)
sigaddset(&stopsigmask, signo);
if (prop & SA_STOP && signo != SIGSTOP)
sigaddset(&vforksigmask, signo);
if (prop & SA_CANTMASK)
sigaddset(&sigcantmask, signo);
if (prop & SA_IGNORE && signo != SIGCONT)
sigaddset(&p->p_sigctx.ps_sigignore, signo);
sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
}
sigemptyset(&p->p_sigctx.ps_sigcatch);
p->p_sflag &= ~PS_NOCLDSTOP;
ksiginfo_queue_init(&p->p_sigpend.sp_info);
sigemptyset(&p->p_sigpend.sp_set);
l = LIST_FIRST(&p->p_lwps);
l->l_sigwaited = NULL;
l->l_sigstk = SS_INIT;
ksiginfo_queue_init(&l->l_sigpend.sp_info);
sigemptyset(&l->l_sigpend.sp_set);
ps->sa_refcnt = 1;
}
void
execsigs(struct proc *p)
{
struct sigacts *ps;
struct lwp *l;
int signo, prop;
sigset_t tset;
ksiginfoq_t kq;
KASSERT(p->p_nlwps == 1);
sigactsunshare(p);
ps = p->p_sigacts;
sigemptyset(&tset);
for (signo = 1; signo < NSIG; signo++) {
if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
prop = sigprop[signo];
if (prop & SA_IGNORE) {
if ((prop & SA_CONT) == 0)
sigaddset(&p->p_sigctx.ps_sigignore,
signo);
sigaddset(&tset, signo);
}
SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
}
sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
}
ksiginfo_queue_init(&kq);
mutex_enter(p->p_lock);
sigclearall(p, &tset, &kq);
sigemptyset(&p->p_sigctx.ps_sigcatch);
p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN);
if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
l = LIST_FIRST(&p->p_lwps);
l->l_sigwaited = NULL;
l->l_sigstk = SS_INIT;
ksiginfo_queue_init(&l->l_sigpend.sp_info);
sigemptyset(&l->l_sigpend.sp_set);
mutex_exit(p->p_lock);
ksiginfo_queue_drain(&kq);
}
ksiginfo_t *
ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
{
ksiginfo_t *kp;
if (ok != NULL) {
if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
KSI_FROMPOOL)
return ok;
if (KSI_EMPTY_P(ok))
return ok;
}
kp = pool_cache_get(ksiginfo_cache, flags);
if (kp == NULL) {
#ifdef DIAGNOSTIC
printf("Out of memory allocating ksiginfo for pid %d\n",
p->p_pid);
#endif
return NULL;
}
if (ok != NULL) {
memcpy(kp, ok, sizeof(*kp));
kp->ksi_flags &= ~KSI_QUEUED;
} else
KSI_INIT_EMPTY(kp);
kp->ksi_flags |= KSI_FROMPOOL;
return kp;
}
void
ksiginfo_free(ksiginfo_t *kp)
{
if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
return;
pool_cache_put(ksiginfo_cache, kp);
}
void
ksiginfo_queue_drain0(ksiginfoq_t *kq)
{
ksiginfo_t *ksi;
KASSERT(!TAILQ_EMPTY(kq));
while (!TAILQ_EMPTY(kq)) {
ksi = TAILQ_FIRST(kq);
TAILQ_REMOVE(kq, ksi, ksi_list);
pool_cache_put(ksiginfo_cache, ksi);
}
}
static int
siggetinfo(sigpend_t *sp, ksiginfo_t *out, int signo)
{
ksiginfo_t *ksi, *nksi;
if (sp == NULL)
goto out;
int count = 0;
TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, nksi) {
if (ksi->ksi_signo != signo)
continue;
if (count++ > 0)
continue;
TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
ksi->ksi_flags &= ~KSI_QUEUED;
if (out != NULL) {
memcpy(out, ksi, sizeof(*out));
out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
}
ksiginfo_free(ksi);
}
if (count)
return count;
out:
if (out != NULL) {
KSI_INIT(out);
out->ksi_info._signo = signo;
out->ksi_info._code = SI_NOINFO;
}
return 0;
}
int
sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
{
sigset_t tset;
int count;
if (sp == NULL)
goto out;
if (signo == 0) {
if (mask != NULL) {
tset = *mask;
__sigandset(&sp->sp_set, &tset);
} else
tset = sp->sp_set;
if ((signo = firstsig(&tset)) == 0)
goto out;
} else {
KASSERT(sigismember(&sp->sp_set, signo));
}
sigdelset(&sp->sp_set, signo);
out:
count = siggetinfo(sp, out, signo);
if (count > 1)
sigaddset(&sp->sp_set, signo);
return signo;
}
static int
sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
{
ksiginfo_t *kp;
KASSERT(mutex_owned(p->p_lock));
KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
sigaddset(&sp->sp_set, ksi->ksi_signo);
if (KSI_EMPTY_P(ksi))
return 0;
KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
size_t count = 0;
TAILQ_FOREACH(kp, &sp->sp_info, ksi_list) {
count++;
if (ksi->ksi_signo >= SIGRTMIN && ksi->ksi_signo <= SIGRTMAX)
continue;
if (kp->ksi_signo == ksi->ksi_signo) {
KSI_COPY(ksi, kp);
kp->ksi_flags |= KSI_QUEUED;
return 0;
}
}
if (count >= SIGQUEUE_MAX) {
#ifdef DIAGNOSTIC
printf("%s(%d): Signal queue is full signal=%d\n",
p->p_comm, p->p_pid, ksi->ksi_signo);
#endif
return SET_ERROR(EAGAIN);
}
ksi->ksi_flags |= KSI_QUEUED;
TAILQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
return 0;
}
void
sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
{
ksiginfo_t *ksi, *next;
if (mask == NULL)
sigemptyset(&sp->sp_set);
else
sigminusset(mask, &sp->sp_set);
TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, next) {
if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
TAILQ_INSERT_TAIL(kq, ksi, ksi_list);
}
}
}
void
sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
{
struct lwp *l;
KASSERT(mutex_owned(p->p_lock));
sigclear(&p->p_sigpend, mask, kq);
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
sigclear(&l->l_sigpend, mask, kq);
}
}
int
sigispending(struct lwp *l, int signo)
{
struct proc *p = l->l_proc;
sigset_t tset;
membar_consumer();
tset = l->l_sigpend.sp_set;
sigplusset(&p->p_sigpend.sp_set, &tset);
sigminusset(&p->p_sigctx.ps_sigignore, &tset);
sigminusset(&l->l_sigmask, &tset);
if (signo == 0) {
return firstsig(&tset);
}
return sigismember(&tset, signo) ? signo : 0;
}
void
getucontext(struct lwp *l, ucontext_t *ucp)
{
struct proc *p = l->l_proc;
KASSERT(mutex_owned(p->p_lock));
ucp->uc_flags = 0;
ucp->uc_link = l->l_ctxlink;
ucp->uc_sigmask = l->l_sigmask;
ucp->uc_flags |= _UC_SIGMASK;
if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
ucp->uc_stack.ss_flags = 0;
} else {
ucp->uc_stack = l->l_sigstk;
}
ucp->uc_flags |= _UC_STACK;
mutex_exit(p->p_lock);
cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
mutex_enter(p->p_lock);
}
int
setucontext(struct lwp *l, const ucontext_t *ucp)
{
struct proc *p = l->l_proc;
int error;
KASSERT(mutex_owned(p->p_lock));
if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
if (error != 0)
return error;
}
mutex_exit(p->p_lock);
error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
mutex_enter(p->p_lock);
if (error != 0)
return (error);
l->l_ctxlink = ucp->uc_link;
if ((ucp->uc_flags & _UC_STACK) != 0) {
if (ucp->uc_stack.ss_flags & SS_ONSTACK)
l->l_sigstk.ss_flags |= SS_ONSTACK;
else
l->l_sigstk.ss_flags &= ~SS_ONSTACK;
}
return 0;
}
int
killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
{
struct proc *p, *cp;
kauth_cred_t pc;
struct pgrp *pgrp;
int nfound;
int signo = ksi->ksi_signo;
cp = l->l_proc;
pc = l->l_cred;
nfound = 0;
mutex_enter(&proc_lock);
if (all) {
PROCLIST_FOREACH(p, &allproc) {
if (p->p_pid <= 1 || p == cp ||
(p->p_flag & PK_SYSTEM) != 0)
continue;
mutex_enter(p->p_lock);
if (kauth_authorize_process(pc,
KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
NULL) == 0) {
nfound++;
if (signo)
kpsignal2(p, ksi);
}
mutex_exit(p->p_lock);
}
} else {
if (pgid == 0)
pgrp = cp->p_pgrp;
else {
pgrp = pgrp_find(pgid);
if (pgrp == NULL)
goto out;
}
LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
continue;
mutex_enter(p->p_lock);
if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
p, KAUTH_ARG(signo), NULL, NULL) == 0) {
nfound++;
if (signo && P_ZOMBIE(p) == 0)
kpsignal2(p, ksi);
}
mutex_exit(p->p_lock);
}
}
out:
mutex_exit(&proc_lock);
return nfound ? 0 : SET_ERROR(ESRCH);
}
void
pgsignal(struct pgrp *pgrp, int sig, int checkctty)
{
ksiginfo_t ksi;
KASSERT(!cpu_intr_p());
KASSERT(mutex_owned(&proc_lock));
KSI_INIT_EMPTY(&ksi);
ksi.ksi_signo = sig;
kpgsignal(pgrp, &ksi, NULL, checkctty);
}
void
kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
{
struct proc *p;
KASSERT(!cpu_intr_p());
KASSERT(mutex_owned(&proc_lock));
KASSERT(pgrp != NULL);
LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
kpsignal(p, ksi, data);
}
void
trapsignal(struct lwp *l, ksiginfo_t *ksi)
{
struct proc *p;
struct sigacts *ps;
int signo = ksi->ksi_signo;
sigset_t *mask;
sig_t action;
KASSERT(KSI_TRAP_P(ksi));
ksi->ksi_lid = l->l_lid;
p = l->l_proc;
KASSERT(!cpu_intr_p());
mutex_enter(&proc_lock);
mutex_enter(p->p_lock);
repeat:
if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
mutex_exit(p->p_lock);
mutex_exit(&proc_lock);
lwp_exit(l);
panic("trapsignal");
}
if ((p->p_sflag & PS_STOPPING) != 0) {
mutex_exit(&proc_lock);
sigswitch_unlock_and_switch_away(l);
mutex_enter(&proc_lock);
mutex_enter(p->p_lock);
goto repeat;
}
mask = &l->l_sigmask;
ps = p->p_sigacts;
action = SIGACTION_PS(ps, signo).sa_handler;
if (ISSET(p->p_slflag, PSL_TRACED) &&
!(p->p_pptr == p->p_opptr && ISSET(p->p_lflag, PL_PPWAIT)) &&
p->p_xsig != SIGKILL &&
!sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
p->p_xsig = signo;
p->p_sigctx.ps_faked = true;
p->p_sigctx.ps_lwp = ksi->ksi_lid;
p->p_sigctx.ps_info = ksi->ksi_info;
sigswitch(0, signo, true);
if (ktrpoint(KTR_PSIG)) {
if (p->p_emul->e_ktrpsig)
p->p_emul->e_ktrpsig(signo, action, mask, ksi);
else
ktrpsig(signo, action, mask, ksi);
}
return;
}
const bool caught = sigismember(&p->p_sigctx.ps_sigcatch, signo);
const bool masked = sigismember(mask, signo);
if (caught && !masked) {
mutex_exit(&proc_lock);
l->l_ru.ru_nsignals++;
kpsendsig(l, ksi, mask);
mutex_exit(p->p_lock);
if (ktrpoint(KTR_PSIG)) {
if (p->p_emul->e_ktrpsig)
p->p_emul->e_ktrpsig(signo, action, mask, ksi);
else
ktrpsig(signo, action, mask, ksi);
}
return;
}
const bool ignored = action == SIG_IGN;
if (masked || ignored) {
mutex_enter(&ps->sa_mutex);
sigdelset(mask, signo);
sigdelset(&p->p_sigctx.ps_sigcatch, signo);
sigdelset(&p->p_sigctx.ps_sigignore, signo);
sigdelset(&SIGACTION_PS(ps, signo).sa_mask, signo);
SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
mutex_exit(&ps->sa_mutex);
}
kpsignal2(p, ksi);
mutex_exit(p->p_lock);
mutex_exit(&proc_lock);
}
void
child_psignal(struct proc *p, int mask)
{
ksiginfo_t ksi;
struct proc *q;
int xsig;
KASSERT(mutex_owned(&proc_lock));
KASSERT(mutex_owned(p->p_lock));
xsig = p->p_xsig;
KSI_INIT(&ksi);
ksi.ksi_signo = SIGCHLD;
ksi.ksi_code = (xsig == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
ksi.ksi_pid = p->p_pid;
ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
ksi.ksi_status = xsig;
ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
q = p->p_pptr;
mutex_exit(p->p_lock);
mutex_enter(q->p_lock);
if ((q->p_sflag & mask) == 0)
kpsignal2(q, &ksi);
mutex_exit(q->p_lock);
mutex_enter(p->p_lock);
}
void
psignal(struct proc *p, int signo)
{
ksiginfo_t ksi;
KASSERT(!cpu_intr_p());
KASSERT(mutex_owned(&proc_lock));
KSI_INIT_EMPTY(&ksi);
ksi.ksi_signo = signo;
mutex_enter(p->p_lock);
kpsignal2(p, &ksi);
mutex_exit(p->p_lock);
}
void
kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
{
fdfile_t *ff;
file_t *fp;
fdtab_t *dt;
KASSERT(!cpu_intr_p());
KASSERT(mutex_owned(&proc_lock));
if ((p->p_sflag & PS_WEXIT) == 0 && data) {
size_t fd;
filedesc_t *fdp = p->p_fd;
ksi->ksi_fd = -1;
dt = atomic_load_consume(&fdp->fd_dt);
for (fd = 0; fd < dt->dt_nfiles; fd++) {
if ((ff = dt->dt_ff[fd]) == NULL)
continue;
if ((fp = atomic_load_consume(&ff->ff_file)) == NULL)
continue;
if (fp->f_data == data) {
ksi->ksi_fd = fd;
break;
}
}
}
mutex_enter(p->p_lock);
kpsignal2(p, ksi);
mutex_exit(p->p_lock);
}
int
sigismasked(struct lwp *l, int sig)
{
struct proc *p = l->l_proc;
return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
sigismember(&l->l_sigmask, sig);
}
static int
sigpost(struct lwp *l, sig_t action, int prop, int sig)
{
int rv, masked;
struct proc *p = l->l_proc;
KASSERT(mutex_owned(p->p_lock));
if (l->l_stat == LSZOMB)
return 0;
SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
lwp_lock(l);
if (__predict_false((l->l_flag & LW_DBGSUSPEND) != 0)) {
if ((prop & SA_KILL) != 0)
l->l_flag &= ~LW_DBGSUSPEND;
else {
lwp_unlock(l);
return 0;
}
}
signotify(l);
masked = sigismember(&l->l_sigmask, sig);
if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
lwp_unlock(l);
return 0;
}
if (__predict_false((prop & SA_KILL) != 0) &&
action == SIG_DFL && l->l_priority < MAXPRI_USER) {
KASSERT(l->l_class == SCHED_OTHER);
lwp_changepri(l, MAXPRI_USER);
}
rv = 0;
switch (l->l_stat) {
case LSRUN:
case LSONPROC:
rv = 1;
break;
case LSSLEEP:
if ((l->l_flag & LW_SINTR) != 0) {
setrunnable(l);
return 1;
}
break;
case LSSUSPENDED:
if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
lwp_continue(l);
return 1;
}
break;
case LSSTOP:
if ((prop & SA_STOP) != 0)
break;
if ((prop & SA_CONT) != 0) {
if (l->l_wchan != NULL) {
l->l_stat = LSSLEEP;
p->p_nrlwps++;
rv = 1;
break;
}
setrunnable(l);
return 1;
} else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
setrunnable(l);
return 1;
}
break;
default:
break;
}
lwp_unlock(l);
return rv;
}
void
signotify(struct lwp *l)
{
KASSERT(lwp_locked(l, NULL));
l->l_flag |= LW_PENDSIG;
lwp_need_userret(l);
}
static int
sigunwait(struct proc *p, const ksiginfo_t *ksi)
{
struct lwp *l;
int signo;
KASSERT(mutex_owned(p->p_lock));
signo = ksi->ksi_signo;
if (ksi->ksi_lid != 0) {
if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
return 0;
if (l->l_sigwaited == NULL ||
!sigismember(&l->l_sigwaitset, signo))
return 0;
} else {
LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
KASSERT(l->l_sigwaited != NULL);
if (sigismember(&l->l_sigwaitset, signo))
break;
}
}
if (l != NULL) {
l->l_sigwaited->ksi_info = ksi->ksi_info;
l->l_sigwaited = NULL;
LIST_REMOVE(l, l_sigwaiter);
cv_signal(&l->l_sigcv);
return 1;
}
return 0;
}
int
kpsignal2(struct proc *p, ksiginfo_t *ksi)
{
int prop, signo = ksi->ksi_signo;
struct lwp *l = NULL;
ksiginfo_t *kp;
lwpid_t lid;
sig_t action;
bool toall;
bool traced;
int error = 0;
KASSERT(!cpu_intr_p());
KASSERT(mutex_owned(&proc_lock));
KASSERT(mutex_owned(p->p_lock));
KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
KASSERT(signo > 0);
KASSERT(signo < NSIG);
if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
return 0;
KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
kp = NULL;
prop = sigprop[signo];
toall = ((prop & SA_TOALL) != 0);
lid = toall ? 0 : ksi->ksi_lid;
traced = ISSET(p->p_slflag, PSL_TRACED) &&
!sigismember(&p->p_sigctx.ps_sigpass, signo);
if (traced) {
action = SIG_DFL;
if (lid == 0) {
if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
goto discard;
if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
goto out;
}
} else {
if (sigismember(&p->p_sigctx.ps_sigignore, signo))
goto discard;
else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
action = SIG_CATCH;
else {
action = SIG_DFL;
if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
goto discard;
if (prop & SA_KILL && p->p_nice > NZERO)
p->p_nice = NZERO;
}
}
if ((prop & (SA_CONT | SA_STOP)) != 0) {
ksiginfoq_t kq;
ksiginfo_queue_init(&kq);
if ((prop & SA_CONT) != 0)
sigclear(&p->p_sigpend, &stopsigmask, &kq);
if ((prop & SA_STOP) != 0)
sigclear(&p->p_sigpend, &contsigmask, &kq);
ksiginfo_queue_drain(&kq);
}
if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
sigunwait(p, ksi))
goto discard;
if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
goto discard;
if (lid != 0) {
l = lwp_find(p, lid);
if (l != NULL) {
if ((error = sigput(&l->l_sigpend, p, kp)) != 0)
goto out;
membar_producer();
if (sigpost(l, action, prop, kp->ksi_signo) != 0)
signo = -1;
}
goto out;
}
if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
if (traced)
goto deliver;
if ((prop & SA_CONT) != 0 && action == SIG_DFL)
goto out;
} else {
if (traced && signo != SIGKILL) {
goto out;
}
if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
if (p->p_stat == SSTOP && !p->p_waited)
p->p_pptr->p_nstopchild--;
p->p_stat = SACTIVE;
p->p_sflag &= ~PS_STOPPING;
if (traced) {
KASSERT(signo == SIGKILL);
goto deliver;
}
if ((prop & SA_CONT) != 0) {
p->p_xsig = SIGCONT;
p->p_sflag |= PS_CONTINUED;
child_psignal(p, 0);
if (action == SIG_DFL) {
KASSERT(signo != SIGKILL);
goto deliver;
}
}
} else if ((prop & SA_STOP) != 0) {
goto out;
}
}
KASSERT(!traced);
if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
goto out;
deliver:
membar_producer();
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
break;
}
signo = -1;
out:
ksiginfo_free(kp);
if (signo == -1)
return error;
discard:
SDT_PROBE(proc, kernel, , signal__discard, l, p, signo, 0, 0);
return error;
}
void
kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
{
struct proc *p = l->l_proc;
KASSERT(mutex_owned(p->p_lock));
(*p->p_emul->e_sendsig)(ksi, mask);
}
static void
proc_stop_lwps(struct proc *p)
{
struct lwp *l;
KASSERT(mutex_owned(p->p_lock));
KASSERT((p->p_sflag & PS_STOPPING) != 0);
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
lwp_lock(l);
if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
l->l_stat = LSSTOP;
p->p_nrlwps--;
}
lwp_unlock(l);
}
}
static void
proc_stop_done(struct proc *p, int ppmask)
{
KASSERT(mutex_owned(&proc_lock));
KASSERT(mutex_owned(p->p_lock));
KASSERT((p->p_sflag & PS_STOPPING) != 0);
KASSERT(p->p_nrlwps == 0 || p->p_nrlwps == 1);
KASSERT(p->p_nrlwps == 0 || p == curproc);
p->p_sflag &= ~PS_STOPPING;
p->p_stat = SSTOP;
p->p_waited = 0;
p->p_pptr->p_nstopchild++;
child_psignal(p, ppmask);
cv_broadcast(&p->p_pptr->p_waitcv);
}
void
eventswitch(int code, int pe_report_event, int entity)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct sigacts *ps;
sigset_t *mask;
sig_t action;
ksiginfo_t ksi;
const int signo = SIGTRAP;
KASSERT(mutex_owned(&proc_lock));
KASSERT(mutex_owned(p->p_lock));
KASSERT(p->p_pptr != initproc);
KASSERT(l->l_stat == LSONPROC);
KASSERT(ISSET(p->p_slflag, PSL_TRACED));
KASSERT(!ISSET(l->l_flag, LW_SYSTEM));
KASSERT(p->p_nrlwps > 0);
KASSERT((code == TRAP_CHLD) || (code == TRAP_LWP) ||
(code == TRAP_EXEC));
KASSERT((code != TRAP_CHLD) || (entity > 1));
KASSERT((code != TRAP_LWP) || (entity > 0));
repeat:
if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
mutex_exit(p->p_lock);
mutex_exit(&proc_lock);
if (pe_report_event == PTRACE_LWP_EXIT) {
return;
}
lwp_exit(l);
panic("eventswitch");
}
if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
mutex_exit(p->p_lock);
mutex_exit(&proc_lock);
return;
}
if (p->p_xsig == SIGKILL ||
sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
mutex_exit(p->p_lock);
mutex_exit(&proc_lock);
return;
}
if ((p->p_sflag & PS_STOPPING) != 0) {
mutex_exit(&proc_lock);
sigswitch_unlock_and_switch_away(l);
mutex_enter(&proc_lock);
mutex_enter(p->p_lock);
goto repeat;
}
KSI_INIT_TRAP(&ksi);
ksi.ksi_lid = l->l_lid;
ksi.ksi_signo = signo;
ksi.ksi_code = code;
ksi.ksi_pe_report_event = pe_report_event;
CTASSERT(sizeof(ksi.ksi_pe_other_pid) == sizeof(ksi.ksi_pe_lwp));
ksi.ksi_pe_other_pid = entity;
ps = p->p_sigacts;
action = SIGACTION_PS(ps, signo).sa_handler;
mask = &l->l_sigmask;
p->p_xsig = signo;
p->p_sigctx.ps_faked = true;
p->p_sigctx.ps_lwp = ksi.ksi_lid;
p->p_sigctx.ps_info = ksi.ksi_info;
sigswitch(0, signo, true);
if (code == TRAP_CHLD) {
mutex_enter(&proc_lock);
while (l->l_vforkwaiting)
cv_wait(&l->l_waitcv, &proc_lock);
mutex_exit(&proc_lock);
}
if (ktrpoint(KTR_PSIG)) {
if (p->p_emul->e_ktrpsig)
p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
else
ktrpsig(signo, action, mask, &ksi);
}
}
void
eventswitchchild(struct proc *p, int code, int pe_report_event)
{
mutex_enter(&proc_lock);
mutex_enter(p->p_lock);
if ((p->p_slflag & (PSL_TRACED|PSL_TRACEDCHILD)) !=
(PSL_TRACED|PSL_TRACEDCHILD)) {
mutex_exit(p->p_lock);
mutex_exit(&proc_lock);
return;
}
eventswitch(code, pe_report_event, p->p_oppid);
}
static void
sigswitch(int ppmask, int signo, bool proc_lock_held)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
KASSERT(mutex_owned(p->p_lock));
KASSERT(l->l_stat == LSONPROC);
KASSERT(p->p_nrlwps > 0);
if (proc_lock_held) {
KASSERT(mutex_owned(&proc_lock));
} else {
KASSERT(!mutex_owned(&proc_lock));
}
if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
KASSERT(signo != 0);
proc_stop(p, signo);
KASSERT(p->p_nrlwps > 0);
}
if ((p->p_sflag & PS_STOPPING) != 0) {
if (!proc_lock_held && !mutex_tryenter(&proc_lock)) {
mutex_exit(p->p_lock);
mutex_enter(&proc_lock);
mutex_enter(p->p_lock);
}
if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
proc_stop_done(p, ppmask);
}
mutex_exit(&proc_lock);
}
sigswitch_unlock_and_switch_away(l);
}
static void
sigswitch_unlock_and_switch_away(struct lwp *l)
{
struct proc *p;
p = l->l_proc;
KASSERT(mutex_owned(p->p_lock));
KASSERT(!mutex_owned(&proc_lock));
KASSERT(l->l_stat == LSONPROC);
KASSERT(p->p_nrlwps > 0);
KASSERT(l->l_blcnt == 0);
if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
p->p_nrlwps--;
lwp_lock(l);
KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
l->l_stat = LSSTOP;
lwp_unlock(l);
}
mutex_exit(p->p_lock);
lwp_lock(l);
spc_lock(l->l_cpu);
mi_switch(l);
}
static int
sigchecktrace(void)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
int signo;
KASSERT(mutex_owned(p->p_lock));
if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
return 0;
if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xsig == 0 ||
(p->p_sflag & PS_STOPPING) != 0)
return 0;
signo = p->p_xsig;
p->p_xsig = 0;
if (sigismember(&l->l_sigmask, signo)) {
signo = 0;
}
return signo;
}
int
issignal(struct lwp *l)
{
struct proc *p;
int siglwp, signo, prop;
sigpend_t *sp;
sigset_t ss;
bool traced;
p = l->l_proc;
sp = NULL;
signo = 0;
KASSERT(p == curproc);
KASSERT(mutex_owned(p->p_lock));
for (;;) {
if (signo != 0) {
(void)sigget(sp, NULL, signo, NULL);
}
if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
sigswitch_unlock_and_switch_away(l);
mutex_enter(p->p_lock);
continue;
} else if (p->p_stat == SACTIVE)
signo = sigchecktrace();
else
signo = 0;
sp = NULL;
if (signo == 0) {
sp = &l->l_sigpend;
ss = sp->sp_set;
siglwp = l->l_lid;
if ((p->p_lflag & PL_PPWAIT) != 0)
sigminusset(&vforksigmask, &ss);
sigminusset(&l->l_sigmask, &ss);
if ((signo = firstsig(&ss)) == 0) {
sp = &p->p_sigpend;
ss = sp->sp_set;
siglwp = 0;
if ((p->p_lflag & PL_PPWAIT) != 0)
sigminusset(&vforksigmask, &ss);
sigminusset(&l->l_sigmask, &ss);
if ((signo = firstsig(&ss)) == 0) {
lwp_lock(l);
l->l_flag &= ~LW_PENDSIG;
lwp_unlock(l);
sp = NULL;
break;
}
}
}
traced = ISSET(p->p_slflag, PSL_TRACED) &&
!sigismember(&p->p_sigctx.ps_sigpass, signo);
if (sp) {
ksiginfo_t *ksi = TAILQ_FIRST(&sp->sp_info);
if (ksi) {
p->p_sigctx.ps_lwp = ksi->ksi_lid;
p->p_sigctx.ps_info = ksi->ksi_info;
} else {
p->p_sigctx.ps_lwp = siglwp;
memset(&p->p_sigctx.ps_info, 0,
sizeof(p->p_sigctx.ps_info));
p->p_sigctx.ps_info._signo = signo;
p->p_sigctx.ps_info._code = SI_NOINFO;
}
}
if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
!traced) {
continue;
}
if (traced && signo != SIGKILL &&
!(ISSET(p->p_lflag, PL_PPWAIT) &&
(p->p_pptr == p->p_opptr))) {
if (sp)
sigdelset(&sp->sp_set, signo);
p->p_xsig = signo;
sigswitch(0, signo, false);
mutex_enter(p->p_lock);
if ((signo = sigchecktrace()) == 0)
continue;
sp = NULL;
}
prop = sigprop[signo];
switch ((long)SIGACTION(p, signo).sa_handler) {
case (long)SIG_DFL:
if (p->p_pid <= 1) {
#ifdef DIAGNOSTIC
printf_nolog("Process (pid %d) got sig %d\n",
p->p_pid, signo);
#endif
continue;
}
if (prop & SA_STOP) {
if ((traced &&
!(ISSET(p->p_lflag, PL_PPWAIT) &&
(p->p_pptr == p->p_opptr))) ||
((p->p_lflag & PL_ORPHANPG) != 0 &&
prop & SA_TTYSTOP)) {
continue;
}
(void)sigget(sp, NULL, signo, NULL);
p->p_xsig = signo;
p->p_sflag &= ~PS_CONTINUED;
signo = 0;
sigswitch(PS_NOCLDSTOP, p->p_xsig, false);
mutex_enter(p->p_lock);
} else if (prop & SA_IGNORE) {
continue;
}
break;
case (long)SIG_IGN:
#ifdef DEBUG_ISSIGNAL
if ((prop & SA_CONT) == 0 && !traced)
printf_nolog("issignal\n");
#endif
continue;
default:
break;
}
break;
}
l->l_sigpendset = sp;
return signo;
}
void
postsig(int signo)
{
struct lwp *l;
struct proc *p;
struct sigacts *ps;
sig_t action;
sigset_t *returnmask;
ksiginfo_t ksi;
l = curlwp;
p = l->l_proc;
ps = p->p_sigacts;
KASSERT(mutex_owned(p->p_lock));
KASSERT(signo > 0);
if (l->l_sigrestore) {
returnmask = &l->l_sigoldmask;
l->l_sigrestore = 0;
} else
returnmask = &l->l_sigmask;
action = SIGACTION_PS(ps, signo).sa_handler;
l->l_ru.ru_nsignals++;
if (l->l_sigpendset == NULL) {
if (p->p_sigctx.ps_faked &&
signo == p->p_sigctx.ps_info._signo) {
KSI_INIT(&ksi);
ksi.ksi_info = p->p_sigctx.ps_info;
ksi.ksi_lid = p->p_sigctx.ps_lwp;
p->p_sigctx.ps_faked = false;
} else {
if (!siggetinfo(&l->l_sigpend, &ksi, signo))
(void)siggetinfo(&p->p_sigpend, &ksi, signo);
}
} else
sigget(l->l_sigpendset, &ksi, signo, NULL);
if (ktrpoint(KTR_PSIG)) {
mutex_exit(p->p_lock);
if (p->p_emul->e_ktrpsig)
p->p_emul->e_ktrpsig(signo, action,
returnmask, &ksi);
else
ktrpsig(signo, action, returnmask, &ksi);
mutex_enter(p->p_lock);
}
SDT_PROBE(proc, kernel, , signal__handle, signo, &ksi, action, 0, 0);
if (action == SIG_DFL) {
sigexit(l, signo);
return;
}
#ifdef DIAGNOSTIC
if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
panic("postsig action");
#endif
kpsendsig(l, &ksi, returnmask);
}
void
sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
{
struct sigacts *sa;
int sig;
sig = ksi->ksi_signo;
sa = curproc->p_sigacts;
switch (sa->sa_sigdesc[sig].sd_vers) {
case __SIGTRAMP_SIGCODE_VERSION:
#ifdef __HAVE_STRUCT_SIGCONTEXT
case __SIGTRAMP_SIGCONTEXT_VERSION_MIN ...
__SIGTRAMP_SIGCONTEXT_VERSION_MAX:
MODULE_HOOK_CALL_VOID(sendsig_sigcontext_16_hook, (ksi, mask),
break);
return;
#endif
case __SIGTRAMP_SIGINFO_VERSION_MIN ...
__SIGTRAMP_SIGINFO_VERSION_MAX:
sendsig_siginfo(ksi, mask);
return;
default:
break;
}
printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
sigexit(curlwp, SIGILL);
}
void
sendsig_reset(struct lwp *l, int signo)
{
struct proc *p = l->l_proc;
struct sigacts *ps = p->p_sigacts;
KASSERT(mutex_owned(p->p_lock));
p->p_sigctx.ps_lwp = 0;
memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
mutex_enter(&ps->sa_mutex);
sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
sigdelset(&p->p_sigctx.ps_sigcatch, signo);
if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
sigaddset(&p->p_sigctx.ps_sigignore, signo);
SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
}
mutex_exit(&ps->sa_mutex);
}
void
killproc(struct proc *p, const char *why)
{
KASSERT(mutex_owned(&proc_lock));
log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
psignal(p, SIGKILL);
}
void
sigexit(struct lwp *l, int signo)
{
int exitsig, error, docore;
struct proc *p;
struct lwp *t;
p = l->l_proc;
KASSERT(mutex_owned(p->p_lock));
KASSERT(l->l_blcnt == 0);
if ((p->p_sflag & PS_WCORE) != 0) {
lwp_lock(l);
l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
lwp_need_userret(l);
lwp_unlock(l);
mutex_exit(p->p_lock);
lwp_userret(l);
panic("sigexit 1");
}
if ((p->p_sflag & PS_WEXIT) != 0) {
mutex_exit(p->p_lock);
lwp_exit(l);
panic("sigexit 2");
}
if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
p->p_sflag |= PS_WCORE;
for (;;) {
LIST_FOREACH(t, &p->p_lwps, l_sibling) {
lwp_lock(t);
if (t == l) {
t->l_flag &=
~(LW_WSUSPEND | LW_DBGSUSPEND);
lwp_unlock(t);
continue;
}
t->l_flag |= (LW_WCORE | LW_WEXIT);
lwp_need_userret(t);
lwp_suspend(l, t);
}
if (p->p_nrlwps == 1)
break;
p->p_nlwpwait++;
cv_broadcast(&p->p_lwpcv);
cv_wait(&p->p_lwpcv, p->p_lock);
p->p_nlwpwait--;
}
}
exitsig = signo;
p->p_acflag |= AXSIG;
memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
p->p_sigctx.ps_info._signo = signo;
p->p_sigctx.ps_info._code = SI_NOINFO;
if (docore) {
mutex_exit(p->p_lock);
MODULE_HOOK_CALL(coredump_hook, (l, NULL), enosys(), error);
if (kern_logsigexit) {
int uid = l->l_cred ?
(int)kauth_cred_geteuid(l->l_cred) : -1;
if (error)
log(LOG_INFO, lognocoredump, p->p_pid,
p->p_comm, uid, signo, error);
else
log(LOG_INFO, logcoredump, p->p_pid,
p->p_comm, uid, signo);
}
#ifdef PAX_SEGVGUARD
rw_enter(&exec_lock, RW_WRITER);
pax_segvguard(l, p->p_textvp, p->p_comm, true);
rw_exit(&exec_lock);
#endif
mutex_enter(p->p_lock);
if (error == 0)
p->p_sflag |= PS_COREDUMP;
}
p->p_sflag &= ~PS_WCORE;
exit1(l, 0, exitsig);
}
int
coredump_netbsd(struct lwp *l, struct coredump_iostate *iocookie)
{
int retval;
MODULE_HOOK_CALL(coredump_netbsd_hook, (l, iocookie),
SET_ERROR(ENOSYS), retval);
return retval;
}
int
coredump_netbsd32(struct lwp *l, struct coredump_iostate *iocookie)
{
int retval;
MODULE_HOOK_CALL(coredump_netbsd32_hook, (l, iocookie),
SET_ERROR(ENOSYS), retval);
return retval;
}
int
coredump_elf32(struct lwp *l, struct coredump_iostate *iocookie)
{
int retval;
MODULE_HOOK_CALL(coredump_elf32_hook, (l, iocookie),
SET_ERROR(ENOSYS), retval);
return retval;
}
int
coredump_elf64(struct lwp *l, struct coredump_iostate *iocookie)
{
int retval;
MODULE_HOOK_CALL(coredump_elf64_hook, (l, iocookie),
SET_ERROR(ENOSYS), retval);
return retval;
}
void
proc_stop(struct proc *p, int signo)
{
struct lwp *l;
KASSERT(mutex_owned(p->p_lock));
p->p_sflag |= PS_STOPPING;
membar_producer();
proc_stop_lwps(p);
if (p->p_nrlwps == 0) {
proc_stop_done(p, PS_NOCLDSTOP);
} else {
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
sigpost(l, SIG_DFL, SA_STOP, signo);
}
callout_schedule(&proc_stop_ch, 1);
}
}
static void
proc_stop_callout(void *cookie)
{
bool more, restart;
struct proc *p;
(void)cookie;
do {
restart = false;
more = false;
mutex_enter(&proc_lock);
PROCLIST_FOREACH(p, &allproc) {
mutex_enter(p->p_lock);
if ((p->p_sflag & PS_STOPPING) == 0) {
mutex_exit(p->p_lock);
continue;
}
proc_stop_lwps(p);
if (p->p_nrlwps == 0) {
restart = true;
proc_stop_done(p, PS_NOCLDSTOP);
} else
more = true;
mutex_exit(p->p_lock);
if (restart)
break;
}
mutex_exit(&proc_lock);
} while (restart);
if (more)
callout_schedule(&proc_stop_ch, 1);
}
void
proc_unstop(struct proc *p)
{
struct lwp *l;
int sig;
KASSERT(mutex_owned(&proc_lock));
KASSERT(mutex_owned(p->p_lock));
p->p_stat = SACTIVE;
p->p_sflag &= ~PS_STOPPING;
sig = p->p_xsig;
if (!p->p_waited)
p->p_pptr->p_nstopchild--;
LIST_FOREACH(l, &p->p_lwps, l_sibling) {
lwp_lock(l);
if (l->l_stat != LSSTOP || (l->l_flag & LW_DBGSUSPEND) != 0) {
lwp_unlock(l);
continue;
}
if (l->l_wchan == NULL) {
setrunnable(l);
continue;
}
if (sig && (l->l_flag & LW_SINTR) != 0) {
setrunnable(l);
sig = 0;
} else {
l->l_stat = LSSLEEP;
p->p_nrlwps++;
lwp_unlock(l);
}
}
}
void
proc_stoptrace(int trapno, int sysnum, const register_t args[],
const register_t *ret, int error)
{
struct lwp *l = curlwp;
struct proc *p = l->l_proc;
struct sigacts *ps;
sigset_t *mask;
sig_t action;
ksiginfo_t ksi;
size_t i, sy_narg;
const int signo = SIGTRAP;
KASSERT((trapno == TRAP_SCE) || (trapno == TRAP_SCX));
KASSERT(p->p_pptr != initproc);
KASSERT(ISSET(p->p_slflag, PSL_TRACED));
KASSERT(ISSET(p->p_slflag, PSL_SYSCALL));
sy_narg = p->p_emul->e_sysent[sysnum].sy_narg;
KSI_INIT_TRAP(&ksi);
ksi.ksi_lid = l->l_lid;
ksi.ksi_signo = signo;
ksi.ksi_code = trapno;
ksi.ksi_sysnum = sysnum;
if (trapno == TRAP_SCE) {
ksi.ksi_retval[0] = 0;
ksi.ksi_retval[1] = 0;
ksi.ksi_error = 0;
} else {
ksi.ksi_retval[0] = ret[0];
ksi.ksi_retval[1] = ret[1];
ksi.ksi_error = error;
}
memset(ksi.ksi_args, 0, sizeof(ksi.ksi_args));
for (i = 0; i < sy_narg; i++)
ksi.ksi_args[i] = args[i];
mutex_enter(p->p_lock);
repeat:
if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
mutex_exit(p->p_lock);
lwp_exit(l);
panic("proc_stoptrace");
}
if (p->p_xsig == SIGKILL ||
sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
mutex_exit(p->p_lock);
return;
}
if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
mutex_exit(p->p_lock);
return;
}
if ((p->p_sflag & PS_STOPPING) != 0) {
sigswitch_unlock_and_switch_away(l);
mutex_enter(p->p_lock);
goto repeat;
}
ps = p->p_sigacts;
action = SIGACTION_PS(ps, signo).sa_handler;
mask = &l->l_sigmask;
p->p_xsig = signo;
p->p_sigctx.ps_lwp = ksi.ksi_lid;
p->p_sigctx.ps_info = ksi.ksi_info;
sigswitch(0, signo, false);
if (ktrpoint(KTR_PSIG)) {
if (p->p_emul->e_ktrpsig)
p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
else
ktrpsig(signo, action, mask, &ksi);
}
}
static int
filt_sigattach(struct knote *kn)
{
struct proc *p = curproc;
kn->kn_obj = p;
kn->kn_flags |= EV_CLEAR;
mutex_enter(p->p_lock);
klist_insert(&p->p_klist, kn);
mutex_exit(p->p_lock);
return 0;
}
static void
filt_sigdetach(struct knote *kn)
{
struct proc *p = kn->kn_obj;
mutex_enter(p->p_lock);
klist_remove(&p->p_klist, kn);
mutex_exit(p->p_lock);
}
static int
filt_signal(struct knote *kn, long hint)
{
if (hint & NOTE_SIGNAL) {
hint &= ~NOTE_SIGNAL;
if (kn->kn_id == hint)
kn->kn_data++;
}
return (kn->kn_data != 0);
}
const struct filterops sig_filtops = {
.f_flags = FILTEROP_MPSAFE,
.f_attach = filt_sigattach,
.f_detach = filt_sigdetach,
.f_event = filt_signal,
};