[BACK]Return to kern_sig.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/kern_sig.c, Revision 1.374

1.374   ! christos    1: /*     $NetBSD: kern_sig.c,v 1.373 2019/10/15 13:59:57 kamil Exp $     */
1.243     ad          2:
                      3: /*-
1.277     ad          4:  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
1.243     ad          5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Andrew Doran.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
1.29      cgd        31:
                     32: /*
                     33:  * Copyright (c) 1982, 1986, 1989, 1991, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
                     35:  * (c) UNIX System Laboratories, Inc.
                     36:  * All or some portions of this file are derived from material licensed
                     37:  * to the University of California by American Telephone and Telegraph
                     38:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     39:  * the permission of UNIX System Laboratories, Inc.
                     40:  *
                     41:  * Redistribution and use in source and binary forms, with or without
                     42:  * modification, are permitted provided that the following conditions
                     43:  * are met:
                     44:  * 1. Redistributions of source code must retain the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer.
                     46:  * 2. Redistributions in binary form must reproduce the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer in the
                     48:  *    documentation and/or other materials provided with the distribution.
1.146     agc        49:  * 3. Neither the name of the University nor the names of its contributors
1.29      cgd        50:  *    may be used to endorse or promote products derived from this software
                     51:  *    without specific prior written permission.
                     52:  *
                     53:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     54:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     55:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     56:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     57:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     58:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     59:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     60:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     61:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     62:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     63:  * SUCH DAMAGE.
                     64:  *
1.71      fvdl       65:  *     @(#)kern_sig.c  8.14 (Berkeley) 5/14/95
1.29      cgd        66:  */
1.116     lukem      67:
1.308     rmind      68: /*
                     69:  * Signal subsystem.
                     70:  */
                     71:
1.116     lukem      72: #include <sys/cdefs.h>
1.374   ! christos   73: __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.373 2019/10/15 13:59:57 kamil Exp $");
1.70      mrg        74:
1.227     matt       75: #include "opt_ptrace.h"
1.320     christos   76: #include "opt_dtrace.h"
1.74      thorpej    77: #include "opt_compat_sunos.h"
1.158     christos   78: #include "opt_compat_netbsd.h"
1.202     perry      79: #include "opt_compat_netbsd32.h"
1.240     elad       80: #include "opt_pax.h"
1.29      cgd        81:
                     82: #define        SIGPROP         /* include signal properties table */
                     83: #include <sys/param.h>
                     84: #include <sys/signalvar.h>
                     85: #include <sys/proc.h>
1.334     christos   86: #include <sys/ptrace.h>
1.29      cgd        87: #include <sys/systm.h>
                     88: #include <sys/wait.h>
                     89: #include <sys/ktrace.h>
                     90: #include <sys/syslog.h>
1.59      cgd        91: #include <sys/filedesc.h>
1.243     ad         92: #include <sys/file.h>
1.89      thorpej    93: #include <sys/pool.h>
1.130     thorpej    94: #include <sys/ucontext.h>
1.118     thorpej    95: #include <sys/exec.h>
1.220     elad       96: #include <sys/kauth.h>
1.243     ad         97: #include <sys/acct.h>
                     98: #include <sys/callout.h>
1.260     ad         99: #include <sys/atomic.h>
1.258     ad        100: #include <sys/cpu.h>
1.290     ad        101: #include <sys/module.h>
1.303     darran    102: #include <sys/sdt.h>
1.29      cgd       103:
1.240     elad      104: #ifdef PAX_SEGVGUARD
                    105: #include <sys/pax.h>
                    106: #endif /* PAX_SEGVGUARD */
                    107:
1.307     uebayasi  108: #include <uvm/uvm_extern.h>
1.69      mrg       109:
1.328     christos  110: #define        SIGQUEUE_MAX    32
1.308     rmind     111: static pool_cache_t    sigacts_cache   __read_mostly;
                    112: static pool_cache_t    ksiginfo_cache  __read_mostly;
                    113: static callout_t       proc_stop_ch    __cacheline_aligned;
                    114:
                    115: sigset_t               contsigmask     __cacheline_aligned;
1.347     kamil     116: sigset_t               stopsigmask     __cacheline_aligned;
1.345     kamil     117: static sigset_t                vforksigmask    __cacheline_aligned;
1.308     rmind     118: sigset_t               sigcantmask     __cacheline_aligned;
                    119:
1.243     ad        120: static void    ksiginfo_exechook(struct proc *, void *);
1.334     christos  121: static void    proc_stop(struct proc *, int);
1.342     kamil     122: static void    proc_stop_done(struct proc *, int);
1.243     ad        123: static void    proc_stop_callout(void *);
1.297     rmind     124: static int     sigchecktrace(void);
1.317     rmind     125: static int     sigpost(struct lwp *, sig_t, int, int);
1.328     christos  126: static int     sigput(sigpend_t *, struct proc *, ksiginfo_t *);
1.297     rmind     127: static int     sigunwait(struct proc *, const ksiginfo_t *);
1.369     kamil     128: static void    sigswitch(int, int, bool);
1.370     kamil     129: static void    sigswitch_unlock_and_switch_away(struct lwp *);
1.152     christos  130:
1.243     ad        131: static void    sigacts_poolpage_free(struct pool *, void *);
                    132: static void    *sigacts_poolpage_alloc(struct pool *, int);
1.196     skrll     133:
1.290     ad        134: void (*sendsig_sigcontext_vec)(const struct ksiginfo *, const sigset_t *);
                    135: int (*coredump_vec)(struct lwp *, const char *) =
1.374   ! christos  136:     (int (*)(struct lwp *, const char *))(void *)enosys;
1.290     ad        137:
1.303     darran    138: /*
                    139:  * DTrace SDT provider definitions
                    140:  */
1.320     christos  141: SDT_PROVIDER_DECLARE(proc);
                    142: SDT_PROBE_DEFINE3(proc, kernel, , signal__send,
                    143:     "struct lwp *",    /* target thread */
                    144:     "struct proc *",   /* target process */
                    145:     "int");            /* signal */
                    146: SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
                    147:     "struct lwp *",    /* target thread */
                    148:     "struct proc *",   /* target process */
                    149:     "int");            /* signal */
                    150: SDT_PROBE_DEFINE3(proc, kernel, , signal__handle,
                    151:     "int",             /* signal */
                    152:     "ksiginfo_t *",    /* signal info */
                    153:     "void (*)(void)"); /* handler address */
1.303     darran    154:
                    155:
1.196     skrll     156: static struct pool_allocator sigactspool_allocator = {
1.301     rmind     157:        .pa_alloc = sigacts_poolpage_alloc,
                    158:        .pa_free = sigacts_poolpage_free
1.196     skrll     159: };
                    160:
1.243     ad        161: #ifdef DEBUG
                    162: int    kern_logsigexit = 1;
                    163: #else
                    164: int    kern_logsigexit = 0;
                    165: #endif
1.89      thorpej   166:
1.301     rmind     167: static const char logcoredump[] =
1.243     ad        168:     "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1.301     rmind     169: static const char lognocoredump[] =
1.243     ad        170:     "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1.237     yamt      171:
1.299     elad      172: static kauth_listener_t signal_listener;
                    173:
                    174: static int
                    175: signal_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
                    176:     void *arg0, void *arg1, void *arg2, void *arg3)
                    177: {
                    178:        struct proc *p;
                    179:        int result, signum;
                    180:
                    181:        result = KAUTH_RESULT_DEFER;
                    182:        p = arg0;
                    183:        signum = (int)(unsigned long)arg1;
                    184:
                    185:        if (action != KAUTH_PROCESS_SIGNAL)
                    186:                return result;
                    187:
                    188:        if (kauth_cred_uidmatch(cred, p->p_cred) ||
                    189:            (signum == SIGCONT && (curproc->p_session == p->p_session)))
                    190:                result = KAUTH_RESULT_ALLOW;
                    191:
                    192:        return result;
                    193: }
                    194:
1.350     maxv      195: static int
                    196: sigacts_ctor(void *arg __unused, void *obj, int flags __unused)
                    197: {
                    198:        memset(obj, 0, sizeof(struct sigacts));
                    199:        return 0;
                    200: }
                    201:
1.29      cgd       202: /*
1.243     ad        203:  * signal_init:
                    204:  *
1.301     rmind     205:  *     Initialize global signal-related data structures.
1.152     christos  206:  */
1.243     ad        207: void
                    208: signal_init(void)
1.152     christos  209: {
                    210:
1.243     ad        211:        sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
1.152     christos  212:
1.279     ad        213:        sigacts_cache = pool_cache_init(sizeof(struct sigacts), 0, 0, 0,
                    214:            "sigacts", sizeof(struct sigacts) > PAGE_SIZE ?
1.350     maxv      215:            &sigactspool_allocator : NULL, IPL_NONE, sigacts_ctor, NULL, NULL);
1.286     ad        216:        ksiginfo_cache = pool_cache_init(sizeof(ksiginfo_t), 0, 0, 0,
                    217:            "ksiginfo", NULL, IPL_VM, NULL, NULL, NULL);
                    218:
1.243     ad        219:        exechook_establish(ksiginfo_exechook, NULL);
1.152     christos  220:
1.265     ad        221:        callout_init(&proc_stop_ch, CALLOUT_MPSAFE);
1.243     ad        222:        callout_setfunc(&proc_stop_ch, proc_stop_callout, NULL);
1.299     elad      223:
                    224:        signal_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
                    225:            signal_listener_cb, NULL);
1.152     christos  226: }
                    227:
                    228: /*
1.243     ad        229:  * sigacts_poolpage_alloc:
                    230:  *
1.301     rmind     231:  *     Allocate a page for the sigacts memory pool.
1.152     christos  232:  */
1.243     ad        233: static void *
                    234: sigacts_poolpage_alloc(struct pool *pp, int flags)
1.152     christos  235: {
                    236:
1.243     ad        237:        return (void *)uvm_km_alloc(kernel_map,
1.301     rmind     238:            PAGE_SIZE * 2, PAGE_SIZE * 2,
1.243     ad        239:            ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
                    240:            | UVM_KMF_WIRED);
1.152     christos  241: }
                    242:
                    243: /*
1.243     ad        244:  * sigacts_poolpage_free:
                    245:  *
1.301     rmind     246:  *     Free a page on behalf of the sigacts memory pool.
1.89      thorpej   247:  */
1.243     ad        248: static void
                    249: sigacts_poolpage_free(struct pool *pp, void *v)
1.89      thorpej   250: {
1.279     ad        251:
1.301     rmind     252:        uvm_km_free(kernel_map, (vaddr_t)v, PAGE_SIZE * 2, UVM_KMF_WIRED);
1.89      thorpej   253: }
                    254:
                    255: /*
1.243     ad        256:  * sigactsinit:
1.301     rmind     257:  *
                    258:  *     Create an initial sigacts structure, using the same signal state
                    259:  *     as of specified process.  If 'share' is set, share the sigacts by
                    260:  *     holding a reference, otherwise just copy it from parent.
1.89      thorpej   261:  */
1.243     ad        262: struct sigacts *
                    263: sigactsinit(struct proc *pp, int share)
1.89      thorpej   264: {
1.302     rmind     265:        struct sigacts *ps = pp->p_sigacts, *ps2;
1.89      thorpej   266:
1.302     rmind     267:        if (__predict_false(share)) {
1.279     ad        268:                atomic_inc_uint(&ps->sa_refcnt);
1.302     rmind     269:                return ps;
1.109     jdolecek  270:        }
1.302     rmind     271:        ps2 = pool_cache_get(sigacts_cache, PR_WAITOK);
                    272:        mutex_init(&ps2->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
                    273:        ps2->sa_refcnt = 1;
1.243     ad        274:
1.302     rmind     275:        mutex_enter(&ps->sa_mutex);
                    276:        memcpy(ps2->sa_sigdesc, ps->sa_sigdesc, sizeof(ps2->sa_sigdesc));
                    277:        mutex_exit(&ps->sa_mutex);
1.259     ad        278:        return ps2;
1.89      thorpej   279: }
                    280:
                    281: /*
1.243     ad        282:  * sigactsunshare:
1.301     rmind     283:  *
                    284:  *     Make this process not share its sigacts, maintaining all signal state.
1.89      thorpej   285:  */
                    286: void
1.112     lukem     287: sigactsunshare(struct proc *p)
1.89      thorpej   288: {
1.302     rmind     289:        struct sigacts *ps, *oldps = p->p_sigacts;
1.243     ad        290:
1.302     rmind     291:        if (__predict_true(oldps->sa_refcnt == 1))
1.89      thorpej   292:                return;
1.302     rmind     293:
1.279     ad        294:        ps = pool_cache_get(sigacts_cache, PR_WAITOK);
1.262     ad        295:        mutex_init(&ps->sa_mutex, MUTEX_DEFAULT, IPL_SCHED);
1.316     reinoud   296:        memcpy(ps->sa_sigdesc, oldps->sa_sigdesc, sizeof(ps->sa_sigdesc));
1.302     rmind     297:        ps->sa_refcnt = 1;
                    298:
1.243     ad        299:        p->p_sigacts = ps;
                    300:        sigactsfree(oldps);
1.89      thorpej   301: }
                    302:
                    303: /*
1.243     ad        304:  * sigactsfree;
                    305:  *
1.301     rmind     306:  *     Release a sigacts structure.
1.89      thorpej   307:  */
                    308: void
1.195     pk        309: sigactsfree(struct sigacts *ps)
1.89      thorpej   310: {
                    311:
1.279     ad        312:        if (atomic_dec_uint_nv(&ps->sa_refcnt) == 0) {
1.243     ad        313:                mutex_destroy(&ps->sa_mutex);
1.279     ad        314:                pool_cache_put(sigacts_cache, ps);
1.29      cgd       315:        }
                    316: }
                    317:
                    318: /*
1.243     ad        319:  * siginit:
                    320:  *
                    321:  *     Initialize signal state for process 0; set to ignore signals that
                    322:  *     are ignored by default and disable the signal stack.  Locking not
                    323:  *     required as the system is still cold.
1.29      cgd       324:  */
                    325: void
1.112     lukem     326: siginit(struct proc *p)
1.29      cgd       327: {
1.243     ad        328:        struct lwp *l;
                    329:        struct sigacts *ps;
                    330:        int signo, prop;
1.79      mycroft   331:
1.112     lukem     332:        ps = p->p_sigacts;
1.79      mycroft   333:        sigemptyset(&contsigmask);
                    334:        sigemptyset(&stopsigmask);
1.345     kamil     335:        sigemptyset(&vforksigmask);
1.79      mycroft   336:        sigemptyset(&sigcantmask);
1.243     ad        337:        for (signo = 1; signo < NSIG; signo++) {
                    338:                prop = sigprop[signo];
1.79      mycroft   339:                if (prop & SA_CONT)
1.243     ad        340:                        sigaddset(&contsigmask, signo);
1.79      mycroft   341:                if (prop & SA_STOP)
1.243     ad        342:                        sigaddset(&stopsigmask, signo);
1.345     kamil     343:                if (prop & SA_STOP && signo != SIGSTOP)
                    344:                        sigaddset(&vforksigmask, signo);
1.79      mycroft   345:                if (prop & SA_CANTMASK)
1.243     ad        346:                        sigaddset(&sigcantmask, signo);
                    347:                if (prop & SA_IGNORE && signo != SIGCONT)
                    348:                        sigaddset(&p->p_sigctx.ps_sigignore, signo);
                    349:                sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
                    350:                SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
1.79      mycroft   351:        }
1.109     jdolecek  352:        sigemptyset(&p->p_sigctx.ps_sigcatch);
1.243     ad        353:        p->p_sflag &= ~PS_NOCLDSTOP;
                    354:
                    355:        ksiginfo_queue_init(&p->p_sigpend.sp_info);
                    356:        sigemptyset(&p->p_sigpend.sp_set);
1.29      cgd       357:
1.79      mycroft   358:        /*
1.243     ad        359:         * Reset per LWP state.
1.79      mycroft   360:         */
1.243     ad        361:        l = LIST_FIRST(&p->p_lwps);
                    362:        l->l_sigwaited = NULL;
1.336     christos  363:        l->l_sigstk = SS_INIT;
1.243     ad        364:        ksiginfo_queue_init(&l->l_sigpend.sp_info);
                    365:        sigemptyset(&l->l_sigpend.sp_set);
1.89      thorpej   366:
                    367:        /* One reference. */
1.109     jdolecek  368:        ps->sa_refcnt = 1;
1.29      cgd       369: }
                    370:
                    371: /*
1.243     ad        372:  * execsigs:
                    373:  *
                    374:  *     Reset signals for an exec of the specified process.
1.29      cgd       375:  */
                    376: void
1.112     lukem     377: execsigs(struct proc *p)
1.29      cgd       378: {
1.243     ad        379:        struct sigacts *ps;
                    380:        struct lwp *l;
                    381:        int signo, prop;
                    382:        sigset_t tset;
                    383:        ksiginfoq_t kq;
                    384:
                    385:        KASSERT(p->p_nlwps == 1);
                    386:
1.115     thorpej   387:        sigactsunshare(p);
1.112     lukem     388:        ps = p->p_sigacts;
1.115     thorpej   389:
1.29      cgd       390:        /*
1.243     ad        391:         * Reset caught signals.  Held signals remain held through
                    392:         * l->l_sigmask (unless they were caught, and are now ignored
                    393:         * by default).
1.259     ad        394:         *
                    395:         * No need to lock yet, the process has only one LWP and
                    396:         * at this point the sigacts are private to the process.
1.243     ad        397:         */
                    398:        sigemptyset(&tset);
                    399:        for (signo = 1; signo < NSIG; signo++) {
                    400:                if (sigismember(&p->p_sigctx.ps_sigcatch, signo)) {
                    401:                        prop = sigprop[signo];
1.79      mycroft   402:                        if (prop & SA_IGNORE) {
                    403:                                if ((prop & SA_CONT) == 0)
1.112     lukem     404:                                        sigaddset(&p->p_sigctx.ps_sigignore,
1.243     ad        405:                                            signo);
                    406:                                sigaddset(&tset, signo);
1.79      mycroft   407:                        }
1.243     ad        408:                        SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1.29      cgd       409:                }
1.243     ad        410:                sigemptyset(&SIGACTION_PS(ps, signo).sa_mask);
                    411:                SIGACTION_PS(ps, signo).sa_flags = SA_RESTART;
1.29      cgd       412:        }
1.243     ad        413:        ksiginfo_queue_init(&kq);
1.259     ad        414:
1.277     ad        415:        mutex_enter(p->p_lock);
1.243     ad        416:        sigclearall(p, &tset, &kq);
1.109     jdolecek  417:        sigemptyset(&p->p_sigctx.ps_sigcatch);
1.205     christos  418:
                    419:        /*
                    420:         * Reset no zombies if child dies flag as Solaris does.
                    421:         */
1.246     pavel     422:        p->p_flag &= ~(PK_NOCLDWAIT | PK_CLDSIGIGN);
1.205     christos  423:        if (SIGACTION_PS(ps, SIGCHLD).sa_handler == SIG_IGN)
                    424:                SIGACTION_PS(ps, SIGCHLD).sa_handler = SIG_DFL;
1.79      mycroft   425:
1.29      cgd       426:        /*
1.243     ad        427:         * Reset per-LWP state.
1.29      cgd       428:         */
1.243     ad        429:        l = LIST_FIRST(&p->p_lwps);
                    430:        l->l_sigwaited = NULL;
1.336     christos  431:        l->l_sigstk = SS_INIT;
1.243     ad        432:        ksiginfo_queue_init(&l->l_sigpend.sp_info);
                    433:        sigemptyset(&l->l_sigpend.sp_set);
1.277     ad        434:        mutex_exit(p->p_lock);
1.243     ad        435:
                    436:        ksiginfo_queue_drain(&kq);
1.29      cgd       437: }
                    438:
1.243     ad        439: /*
                    440:  * ksiginfo_exechook:
                    441:  *
                    442:  *     Free all pending ksiginfo entries from a process on exec.
                    443:  *     Additionally, drain any unused ksiginfo structures in the
                    444:  *     system back to the pool.
                    445:  *
                    446:  *     XXX This should not be a hook, every process has signals.
                    447:  */
                    448: static void
                    449: ksiginfo_exechook(struct proc *p, void *v)
1.79      mycroft   450: {
1.243     ad        451:        ksiginfoq_t kq;
1.79      mycroft   452:
1.243     ad        453:        ksiginfo_queue_init(&kq);
1.79      mycroft   454:
1.277     ad        455:        mutex_enter(p->p_lock);
1.243     ad        456:        sigclearall(p, NULL, &kq);
1.277     ad        457:        mutex_exit(p->p_lock);
1.79      mycroft   458:
1.243     ad        459:        ksiginfo_queue_drain(&kq);
1.79      mycroft   460: }
1.202     perry     461:
1.29      cgd       462: /*
1.243     ad        463:  * ksiginfo_alloc:
                    464:  *
                    465:  *     Allocate a new ksiginfo structure from the pool, and optionally copy
                    466:  *     an existing one.  If the existing ksiginfo_t is from the pool, and
                    467:  *     has not been queued somewhere, then just return it.  Additionally,
                    468:  *     if the existing ksiginfo_t does not contain any information beyond
                    469:  *     the signal number, then just return it.
1.29      cgd       470:  */
1.243     ad        471: ksiginfo_t *
                    472: ksiginfo_alloc(struct proc *p, ksiginfo_t *ok, int flags)
1.48      thorpej   473: {
1.243     ad        474:        ksiginfo_t *kp;
1.29      cgd       475:
1.243     ad        476:        if (ok != NULL) {
                    477:                if ((ok->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) ==
                    478:                    KSI_FROMPOOL)
1.301     rmind     479:                        return ok;
1.243     ad        480:                if (KSI_EMPTY_P(ok))
                    481:                        return ok;
1.79      mycroft   482:        }
1.243     ad        483:
1.286     ad        484:        kp = pool_cache_get(ksiginfo_cache, flags);
1.243     ad        485:        if (kp == NULL) {
                    486: #ifdef DIAGNOSTIC
                    487:                printf("Out of memory allocating ksiginfo for pid %d\n",
                    488:                    p->p_pid);
                    489: #endif
                    490:                return NULL;
1.79      mycroft   491:        }
1.243     ad        492:
                    493:        if (ok != NULL) {
                    494:                memcpy(kp, ok, sizeof(*kp));
                    495:                kp->ksi_flags &= ~KSI_QUEUED;
                    496:        } else
                    497:                KSI_INIT_EMPTY(kp);
                    498:
                    499:        kp->ksi_flags |= KSI_FROMPOOL;
                    500:
                    501:        return kp;
1.79      mycroft   502: }
                    503:
1.243     ad        504: /*
                    505:  * ksiginfo_free:
                    506:  *
                    507:  *     If the given ksiginfo_t is from the pool and has not been queued,
                    508:  *     then free it.
                    509:  */
1.79      mycroft   510: void
1.243     ad        511: ksiginfo_free(ksiginfo_t *kp)
1.79      mycroft   512: {
1.29      cgd       513:
1.243     ad        514:        if ((kp->ksi_flags & (KSI_QUEUED | KSI_FROMPOOL)) != KSI_FROMPOOL)
                    515:                return;
1.286     ad        516:        pool_cache_put(ksiginfo_cache, kp);
1.29      cgd       517: }
                    518:
1.243     ad        519: /*
                    520:  * ksiginfo_queue_drain:
                    521:  *
                    522:  *     Drain a non-empty ksiginfo_t queue.
                    523:  */
                    524: void
                    525: ksiginfo_queue_drain0(ksiginfoq_t *kq)
1.29      cgd       526: {
1.243     ad        527:        ksiginfo_t *ksi;
1.79      mycroft   528:
1.319     christos  529:        KASSERT(!TAILQ_EMPTY(kq));
1.243     ad        530:
1.319     christos  531:        while (!TAILQ_EMPTY(kq)) {
                    532:                ksi = TAILQ_FIRST(kq);
                    533:                TAILQ_REMOVE(kq, ksi, ksi_list);
1.286     ad        534:                pool_cache_put(ksiginfo_cache, ksi);
1.243     ad        535:        }
1.79      mycroft   536: }
                    537:
1.328     christos  538: static int
1.314     christos  539: siggetinfo(sigpend_t *sp, ksiginfo_t *out, int signo)
                    540: {
1.329     hannken   541:        ksiginfo_t *ksi, *nksi;
1.314     christos  542:
                    543:        if (sp == NULL)
                    544:                goto out;
                    545:
                    546:        /* Find siginfo and copy it out. */
1.328     christos  547:        int count = 0;
1.329     hannken   548:        TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, nksi) {
1.314     christos  549:                if (ksi->ksi_signo != signo)
                    550:                        continue;
1.328     christos  551:                if (count++ > 0) /* Only remove the first, count all of them */
                    552:                        continue;
1.319     christos  553:                TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
1.314     christos  554:                KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
                    555:                KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
                    556:                ksi->ksi_flags &= ~KSI_QUEUED;
                    557:                if (out != NULL) {
                    558:                        memcpy(out, ksi, sizeof(*out));
                    559:                        out->ksi_flags &= ~(KSI_FROMPOOL | KSI_QUEUED);
                    560:                }
1.328     christos  561:                ksiginfo_free(ksi);
1.314     christos  562:        }
1.328     christos  563:        if (count)
                    564:                return count;
1.314     christos  565:
                    566: out:
                    567:        /* If there is no siginfo, then manufacture it. */
                    568:        if (out != NULL) {
                    569:                KSI_INIT(out);
                    570:                out->ksi_info._signo = signo;
                    571:                out->ksi_info._code = SI_NOINFO;
                    572:        }
1.328     christos  573:        return 0;
1.314     christos  574: }
                    575:
1.243     ad        576: /*
                    577:  * sigget:
                    578:  *
                    579:  *     Fetch the first pending signal from a set.  Optionally, also fetch
                    580:  *     or manufacture a ksiginfo element.  Returns the number of the first
                    581:  *     pending signal, or zero.
                    582:  */
                    583: int
1.270     yamt      584: sigget(sigpend_t *sp, ksiginfo_t *out, int signo, const sigset_t *mask)
1.243     ad        585: {
                    586:        sigset_t tset;
1.328     christos  587:        int count;
1.243     ad        588:
                    589:        /* If there's no pending set, the signal is from the debugger. */
1.287     christos  590:        if (sp == NULL)
                    591:                goto out;
1.243     ad        592:
                    593:        /* Construct mask from signo, and 'mask'. */
                    594:        if (signo == 0) {
                    595:                if (mask != NULL) {
                    596:                        tset = *mask;
                    597:                        __sigandset(&sp->sp_set, &tset);
                    598:                } else
                    599:                        tset = sp->sp_set;
1.301     rmind     600:
                    601:                /* If there are no signals pending - return. */
1.243     ad        602:                if ((signo = firstsig(&tset)) == 0)
1.287     christos  603:                        goto out;
1.243     ad        604:        } else {
                    605:                KASSERT(sigismember(&sp->sp_set, signo));
                    606:        }
                    607:
                    608:        sigdelset(&sp->sp_set, signo);
1.287     christos  609: out:
1.328     christos  610:        count = siggetinfo(sp, out, signo);
                    611:        if (count > 1)
                    612:                sigaddset(&sp->sp_set, signo);
1.243     ad        613:        return signo;
1.29      cgd       614: }
                    615:
                    616: /*
1.243     ad        617:  * sigput:
1.301     rmind     618:  *
1.296     drochner  619:  *     Append a new ksiginfo element to the list of pending ksiginfo's.
1.29      cgd       620:  */
1.328     christos  621: static int
1.243     ad        622: sigput(sigpend_t *sp, struct proc *p, ksiginfo_t *ksi)
1.48      thorpej   623: {
1.243     ad        624:        ksiginfo_t *kp;
                    625:
1.277     ad        626:        KASSERT(mutex_owned(p->p_lock));
1.243     ad        627:        KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
                    628:
                    629:        sigaddset(&sp->sp_set, ksi->ksi_signo);
                    630:
                    631:        /*
1.296     drochner  632:         * If there is no siginfo, we are done.
1.243     ad        633:         */
1.296     drochner  634:        if (KSI_EMPTY_P(ksi))
1.328     christos  635:                return 0;
1.243     ad        636:
                    637:        KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
1.79      mycroft   638:
1.328     christos  639:        size_t count = 0;
                    640:        TAILQ_FOREACH(kp, &sp->sp_info, ksi_list) {
                    641:                count++;
                    642:                if (ksi->ksi_signo >= SIGRTMIN && ksi->ksi_signo <= SIGRTMAX)
                    643:                        continue;
                    644:                if (kp->ksi_signo == ksi->ksi_signo) {
                    645:                        KSI_COPY(ksi, kp);
                    646:                        kp->ksi_flags |= KSI_QUEUED;
                    647:                        return 0;
                    648:                }
                    649:        }
                    650:
                    651:        if (count >= SIGQUEUE_MAX) {
                    652: #ifdef DIAGNOSTIC
                    653:                printf("%s(%d): Signal queue is full signal=%d\n",
                    654:                    p->p_comm, p->p_pid, ksi->ksi_signo);
1.243     ad        655: #endif
1.328     christos  656:                return EAGAIN;
1.79      mycroft   657:        }
1.243     ad        658:        ksi->ksi_flags |= KSI_QUEUED;
1.319     christos  659:        TAILQ_INSERT_TAIL(&sp->sp_info, ksi, ksi_list);
1.328     christos  660:
                    661:        return 0;
1.79      mycroft   662: }
                    663:
1.243     ad        664: /*
                    665:  * sigclear:
                    666:  *
                    667:  *     Clear all pending signals in the specified set.
                    668:  */
                    669: void
1.270     yamt      670: sigclear(sigpend_t *sp, const sigset_t *mask, ksiginfoq_t *kq)
1.79      mycroft   671: {
1.243     ad        672:        ksiginfo_t *ksi, *next;
1.112     lukem     673:
1.243     ad        674:        if (mask == NULL)
                    675:                sigemptyset(&sp->sp_set);
                    676:        else
                    677:                sigminusset(mask, &sp->sp_set);
1.79      mycroft   678:
1.319     christos  679:        TAILQ_FOREACH_SAFE(ksi, &sp->sp_info, ksi_list, next) {
1.243     ad        680:                if (mask == NULL || sigismember(mask, ksi->ksi_signo)) {
1.319     christos  681:                        TAILQ_REMOVE(&sp->sp_info, ksi, ksi_list);
1.243     ad        682:                        KASSERT((ksi->ksi_flags & KSI_FROMPOOL) != 0);
                    683:                        KASSERT((ksi->ksi_flags & KSI_QUEUED) != 0);
1.319     christos  684:                        TAILQ_INSERT_TAIL(kq, ksi, ksi_list);
1.79      mycroft   685:                }
                    686:        }
1.243     ad        687: }
                    688:
                    689: /*
                    690:  * sigclearall:
                    691:  *
                    692:  *     Clear all pending signals in the specified set from a process and
                    693:  *     its LWPs.
                    694:  */
                    695: void
1.270     yamt      696: sigclearall(struct proc *p, const sigset_t *mask, ksiginfoq_t *kq)
1.243     ad        697: {
                    698:        struct lwp *l;
                    699:
1.277     ad        700:        KASSERT(mutex_owned(p->p_lock));
1.79      mycroft   701:
1.243     ad        702:        sigclear(&p->p_sigpend, mask, kq);
                    703:
                    704:        LIST_FOREACH(l, &p->p_lwps, l_sibling) {
                    705:                sigclear(&l->l_sigpend, mask, kq);
                    706:        }
1.29      cgd       707: }
                    708:
1.243     ad        709: /*
                    710:  * sigispending:
                    711:  *
1.311     christos  712:  *     Return the first signal number if there are pending signals for the
                    713:  *     current LWP.  May be called unlocked provided that LW_PENDSIG is set,
                    714:  *     and that the signal has been posted to the appopriate queue before
                    715:  *     LW_PENDSIG is set.
1.243     ad        716:  */
1.52      christos  717: int
1.243     ad        718: sigispending(struct lwp *l, int signo)
1.48      thorpej   719: {
1.243     ad        720:        struct proc *p = l->l_proc;
                    721:        sigset_t tset;
                    722:
1.260     ad        723:        membar_consumer();
1.243     ad        724:
                    725:        tset = l->l_sigpend.sp_set;
                    726:        sigplusset(&p->p_sigpend.sp_set, &tset);
                    727:        sigminusset(&p->p_sigctx.ps_sigignore, &tset);
                    728:        sigminusset(&l->l_sigmask, &tset);
                    729:
                    730:        if (signo == 0) {
1.312     rmind     731:                return firstsig(&tset);
                    732:        }
                    733:        return sigismember(&tset, signo) ? signo : 0;
1.243     ad        734: }
                    735:
                    736: void
                    737: getucontext(struct lwp *l, ucontext_t *ucp)
                    738: {
                    739:        struct proc *p = l->l_proc;
                    740:
1.277     ad        741:        KASSERT(mutex_owned(p->p_lock));
1.243     ad        742:
                    743:        ucp->uc_flags = 0;
                    744:        ucp->uc_link = l->l_ctxlink;
1.317     rmind     745:        ucp->uc_sigmask = l->l_sigmask;
1.243     ad        746:        ucp->uc_flags |= _UC_SIGMASK;
                    747:
                    748:        /*
                    749:         * The (unsupplied) definition of the `current execution stack'
                    750:         * in the System V Interface Definition appears to allow returning
                    751:         * the main context stack.
                    752:         */
                    753:        if ((l->l_sigstk.ss_flags & SS_ONSTACK) == 0) {
1.263     christos  754:                ucp->uc_stack.ss_sp = (void *)l->l_proc->p_stackbase;
1.243     ad        755:                ucp->uc_stack.ss_size = ctob(l->l_proc->p_vmspace->vm_ssize);
                    756:                ucp->uc_stack.ss_flags = 0;     /* XXX, def. is Very Fishy */
                    757:        } else {
                    758:                /* Simply copy alternate signal execution stack. */
                    759:                ucp->uc_stack = l->l_sigstk;
1.79      mycroft   760:        }
1.243     ad        761:        ucp->uc_flags |= _UC_STACK;
1.277     ad        762:        mutex_exit(p->p_lock);
1.243     ad        763:        cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
1.277     ad        764:        mutex_enter(p->p_lock);
1.29      cgd       765: }
                    766:
                    767: int
1.243     ad        768: setucontext(struct lwp *l, const ucontext_t *ucp)
1.48      thorpej   769: {
1.243     ad        770:        struct proc *p = l->l_proc;
1.223     yamt      771:        int error;
1.29      cgd       772:
1.277     ad        773:        KASSERT(mutex_owned(p->p_lock));
1.243     ad        774:
                    775:        if ((ucp->uc_flags & _UC_SIGMASK) != 0) {
                    776:                error = sigprocmask1(l, SIG_SETMASK, &ucp->uc_sigmask, NULL);
                    777:                if (error != 0)
1.223     yamt      778:                        return error;
1.29      cgd       779:        }
1.243     ad        780:
1.277     ad        781:        mutex_exit(p->p_lock);
1.243     ad        782:        error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags);
1.277     ad        783:        mutex_enter(p->p_lock);
1.243     ad        784:        if (error != 0)
                    785:                return (error);
                    786:
                    787:        l->l_ctxlink = ucp->uc_link;
                    788:
                    789:        /*
                    790:         * If there was stack information, update whether or not we are
                    791:         * still running on an alternate signal stack.
                    792:         */
                    793:        if ((ucp->uc_flags & _UC_STACK) != 0) {
                    794:                if (ucp->uc_stack.ss_flags & SS_ONSTACK)
                    795:                        l->l_sigstk.ss_flags |= SS_ONSTACK;
                    796:                else
                    797:                        l->l_sigstk.ss_flags &= ~SS_ONSTACK;
                    798:        }
                    799:
                    800:        return 0;
1.29      cgd       801: }
                    802:
                    803: /*
1.301     rmind     804:  * killpg1: common code for kill process group/broadcast kill.
1.29      cgd       805:  */
1.52      christos  806: int
1.224     ad        807: killpg1(struct lwp *l, ksiginfo_t *ksi, int pgid, int all)
1.29      cgd       808: {
1.224     ad        809:        struct proc     *p, *cp;
1.220     elad      810:        kauth_cred_t    pc;
1.112     lukem     811:        struct pgrp     *pgrp;
                    812:        int             nfound;
1.243     ad        813:        int             signo = ksi->ksi_signo;
1.202     perry     814:
1.224     ad        815:        cp = l->l_proc;
                    816:        pc = l->l_cred;
1.112     lukem     817:        nfound = 0;
1.243     ad        818:
1.276     ad        819:        mutex_enter(proc_lock);
1.91      thorpej   820:        if (all) {
1.202     perry     821:                /*
1.301     rmind     822:                 * Broadcast.
1.29      cgd       823:                 */
1.199     yamt      824:                PROCLIST_FOREACH(p, &allproc) {
1.283     ad        825:                        if (p->p_pid <= 1 || p == cp ||
1.304     yamt      826:                            (p->p_flag & PK_SYSTEM) != 0)
1.29      cgd       827:                                continue;
1.277     ad        828:                        mutex_enter(p->p_lock);
1.243     ad        829:                        if (kauth_authorize_process(pc,
1.264     elad      830:                            KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signo), NULL,
                    831:                            NULL) == 0) {
1.243     ad        832:                                nfound++;
1.277     ad        833:                                if (signo)
1.243     ad        834:                                        kpsignal2(p, ksi);
                    835:                        }
1.277     ad        836:                        mutex_exit(p->p_lock);
1.29      cgd       837:                }
1.91      thorpej   838:        } else {
1.202     perry     839:                if (pgid == 0)
1.301     rmind     840:                        /* Zero pgid means send to my process group. */
1.29      cgd       841:                        pgrp = cp->p_pgrp;
                    842:                else {
1.306     rmind     843:                        pgrp = pgrp_find(pgid);
1.29      cgd       844:                        if (pgrp == NULL)
1.243     ad        845:                                goto out;
1.29      cgd       846:                }
1.124     matt      847:                LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1.246     pavel     848:                        if (p->p_pid <= 1 || p->p_flag & PK_SYSTEM)
1.29      cgd       849:                                continue;
1.277     ad        850:                        mutex_enter(p->p_lock);
1.264     elad      851:                        if (kauth_authorize_process(pc, KAUTH_PROCESS_SIGNAL,
                    852:                            p, KAUTH_ARG(signo), NULL, NULL) == 0) {
1.243     ad        853:                                nfound++;
1.277     ad        854:                                if (signo && P_ZOMBIE(p) == 0)
                    855:                                        kpsignal2(p, ksi);
1.243     ad        856:                        }
1.277     ad        857:                        mutex_exit(p->p_lock);
1.29      cgd       858:                }
                    859:        }
1.301     rmind     860: out:
1.276     ad        861:        mutex_exit(proc_lock);
1.301     rmind     862:        return nfound ? 0 : ESRCH;
1.29      cgd       863: }
                    864:
                    865: /*
1.301     rmind     866:  * Send a signal to a process group.  If checktty is set, limit to members
1.243     ad        867:  * which have a controlling terminal.
1.29      cgd       868:  */
                    869: void
1.243     ad        870: pgsignal(struct pgrp *pgrp, int sig, int checkctty)
1.29      cgd       871: {
1.148     christos  872:        ksiginfo_t ksi;
                    873:
1.276     ad        874:        KASSERT(!cpu_intr_p());
                    875:        KASSERT(mutex_owned(proc_lock));
1.29      cgd       876:
1.192     matt      877:        KSI_INIT_EMPTY(&ksi);
1.148     christos  878:        ksi.ksi_signo = sig;
                    879:        kpgsignal(pgrp, &ksi, NULL, checkctty);
                    880: }
                    881:
                    882: void
                    883: kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
1.29      cgd       884: {
1.98      augustss  885:        struct proc *p;
1.29      cgd       886:
1.276     ad        887:        KASSERT(!cpu_intr_p());
                    888:        KASSERT(mutex_owned(proc_lock));
1.301     rmind     889:        KASSERT(pgrp != NULL);
1.243     ad        890:
1.297     rmind     891:        LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
                    892:                if (checkctty == 0 || p->p_lflag & PL_CONTROLT)
                    893:                        kpsignal(p, ksi, data);
1.29      cgd       894: }
                    895:
                    896: /*
1.243     ad        897:  * Send a signal caused by a trap to the current LWP.  If it will be caught
                    898:  * immediately, deliver it with correct code.  Otherwise, post it normally.
1.29      cgd       899:  */
1.148     christos  900: void
1.243     ad        901: trapsignal(struct lwp *l, ksiginfo_t *ksi)
1.148     christos  902: {
1.130     thorpej   903:        struct proc     *p;
                    904:        struct sigacts  *ps;
1.243     ad        905:        int signo = ksi->ksi_signo;
1.288     wrstuden  906:        sigset_t *mask;
1.357     kamil     907:        sig_t action;
1.29      cgd       908:
1.166     thorpej   909:        KASSERT(KSI_TRAP_P(ksi));
                    910:
1.243     ad        911:        ksi->ksi_lid = l->l_lid;
1.130     thorpej   912:        p = l->l_proc;
1.243     ad        913:
1.276     ad        914:        KASSERT(!cpu_intr_p());
                    915:        mutex_enter(proc_lock);
1.277     ad        916:        mutex_enter(p->p_lock);
1.351     kamil     917:
1.367     kamil     918: repeat:
1.359     kamil     919:        /*
                    920:         * If we are exiting, demise now.
                    921:         *
                    922:         * This avoids notifying tracer and deadlocking.
                    923:         */
                    924:        if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
                    925:                mutex_exit(p->p_lock);
                    926:                mutex_exit(proc_lock);
                    927:                lwp_exit(l);
                    928:                panic("trapsignal");
                    929:                /* NOTREACHED */
                    930:        }
                    931:
1.367     kamil     932:        /*
                    933:         * The process is already stopping.
                    934:         */
                    935:        if ((p->p_sflag & PS_STOPPING) != 0) {
1.370     kamil     936:                mutex_exit(proc_lock);
                    937:                sigswitch_unlock_and_switch_away(l);
1.367     kamil     938:                mutex_enter(proc_lock);
                    939:                mutex_enter(p->p_lock);
1.370     kamil     940:                goto repeat;
1.367     kamil     941:        }
                    942:
1.357     kamil     943:        mask = &l->l_sigmask;
                    944:        ps = p->p_sigacts;
                    945:        action = SIGACTION_PS(ps, signo).sa_handler;
                    946:
1.351     kamil     947:        if (ISSET(p->p_slflag, PSL_TRACED) &&
1.356     kamil     948:            !(p->p_pptr == p->p_opptr && ISSET(p->p_lflag, PL_PPWAIT)) &&
                    949:            p->p_xsig != SIGKILL &&
                    950:            !sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
                    951:                p->p_xsig = signo;
                    952:                p->p_sigctx.ps_faked = true;
                    953:                p->p_sigctx.ps_lwp = ksi->ksi_lid;
                    954:                p->p_sigctx.ps_info = ksi->ksi_info;
1.369     kamil     955:                sigswitch(0, signo, true);
1.357     kamil     956:
                    957:                if (ktrpoint(KTR_PSIG)) {
                    958:                        if (p->p_emul->e_ktrpsig)
                    959:                                p->p_emul->e_ktrpsig(signo, action, mask, ksi);
                    960:                        else
                    961:                                ktrpsig(signo, action, mask, ksi);
                    962:                }
1.351     kamil     963:                return;
                    964:        }
                    965:
1.339     christos  966:        const bool caught = sigismember(&p->p_sigctx.ps_sigcatch, signo);
                    967:        const bool masked = sigismember(mask, signo);
1.351     kamil     968:        if (caught && !masked) {
1.276     ad        969:                mutex_exit(proc_lock);
1.275     ad        970:                l->l_ru.ru_nsignals++;
1.288     wrstuden  971:                kpsendsig(l, ksi, mask);
1.277     ad        972:                mutex_exit(p->p_lock);
1.357     kamil     973:
1.325     christos  974:                if (ktrpoint(KTR_PSIG)) {
1.330     martin    975:                        if (p->p_emul->e_ktrpsig)
1.357     kamil     976:                                p->p_emul->e_ktrpsig(signo, action, mask, ksi);
1.330     martin    977:                        else
1.357     kamil     978:                                ktrpsig(signo, action, mask, ksi);
1.325     christos  979:                }
1.339     christos  980:                return;
                    981:        }
                    982:
                    983:        /*
                    984:         * If the signal is masked or ignored, then unmask it and
                    985:         * reset it to the default action so that the process or
                    986:         * its tracer will be notified.
                    987:         */
1.357     kamil     988:        const bool ignored = action == SIG_IGN;
1.339     christos  989:        if (masked || ignored) {
                    990:                mutex_enter(&ps->sa_mutex);
                    991:                sigdelset(mask, signo);
                    992:                sigdelset(&p->p_sigctx.ps_sigcatch, signo);
                    993:                sigdelset(&p->p_sigctx.ps_sigignore, signo);
                    994:                sigdelset(&SIGACTION_PS(ps, signo).sa_mask, signo);
                    995:                SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
                    996:                mutex_exit(&ps->sa_mutex);
1.29      cgd       997:        }
1.339     christos  998:
                    999:        kpsignal2(p, ksi);
                   1000:        mutex_exit(p->p_lock);
                   1001:        mutex_exit(proc_lock);
1.29      cgd      1002: }
                   1003:
                   1004: /*
1.151     christos 1005:  * Fill in signal information and signal the parent for a child status change.
                   1006:  */
1.216     christos 1007: void
1.243     ad       1008: child_psignal(struct proc *p, int mask)
1.151     christos 1009: {
                   1010:        ksiginfo_t ksi;
1.243     ad       1011:        struct proc *q;
1.322     christos 1012:        int xsig;
1.243     ad       1013:
1.276     ad       1014:        KASSERT(mutex_owned(proc_lock));
1.277     ad       1015:        KASSERT(mutex_owned(p->p_lock));
1.243     ad       1016:
1.322     christos 1017:        xsig = p->p_xsig;
1.151     christos 1018:
1.191     matt     1019:        KSI_INIT(&ksi);
1.151     christos 1020:        ksi.ksi_signo = SIGCHLD;
1.322     christos 1021:        ksi.ksi_code = (xsig == SIGCONT ? CLD_CONTINUED : CLD_STOPPED);
1.151     christos 1022:        ksi.ksi_pid = p->p_pid;
1.220     elad     1023:        ksi.ksi_uid = kauth_cred_geteuid(p->p_cred);
1.322     christos 1024:        ksi.ksi_status = xsig;
1.151     christos 1025:        ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
                   1026:        ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1.243     ad       1027:
                   1028:        q = p->p_pptr;
                   1029:
1.277     ad       1030:        mutex_exit(p->p_lock);
                   1031:        mutex_enter(q->p_lock);
1.243     ad       1032:
                   1033:        if ((q->p_sflag & mask) == 0)
                   1034:                kpsignal2(q, &ksi);
                   1035:
1.277     ad       1036:        mutex_exit(q->p_lock);
                   1037:        mutex_enter(p->p_lock);
1.151     christos 1038: }
                   1039:
1.29      cgd      1040: void
1.243     ad       1041: psignal(struct proc *p, int signo)
1.148     christos 1042: {
1.165     thorpej  1043:        ksiginfo_t ksi;
                   1044:
1.276     ad       1045:        KASSERT(!cpu_intr_p());
                   1046:        KASSERT(mutex_owned(proc_lock));
1.243     ad       1047:
1.192     matt     1048:        KSI_INIT_EMPTY(&ksi);
1.243     ad       1049:        ksi.ksi_signo = signo;
1.277     ad       1050:        mutex_enter(p->p_lock);
1.234     yamt     1051:        kpsignal2(p, &ksi);
1.277     ad       1052:        mutex_exit(p->p_lock);
1.148     christos 1053: }
                   1054:
                   1055: void
1.234     yamt     1056: kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
1.160     christos 1057: {
1.274     ad       1058:        fdfile_t *ff;
                   1059:        file_t *fp;
1.298     ad       1060:        fdtab_t *dt;
1.165     thorpej  1061:
1.276     ad       1062:        KASSERT(!cpu_intr_p());
                   1063:        KASSERT(mutex_owned(proc_lock));
1.243     ad       1064:
                   1065:        if ((p->p_sflag & PS_WEXIT) == 0 && data) {
1.160     christos 1066:                size_t fd;
1.274     ad       1067:                filedesc_t *fdp = p->p_fd;
1.165     thorpej  1068:
1.274     ad       1069:                /* XXXSMP locking */
1.160     christos 1070:                ksi->ksi_fd = -1;
1.298     ad       1071:                dt = fdp->fd_dt;
                   1072:                for (fd = 0; fd < dt->dt_nfiles; fd++) {
                   1073:                        if ((ff = dt->dt_ff[fd]) == NULL)
1.274     ad       1074:                                continue;
                   1075:                        if ((fp = ff->ff_file) == NULL)
                   1076:                                continue;
                   1077:                        if (fp->f_data == data) {
1.160     christos 1078:                                ksi->ksi_fd = fd;
                   1079:                                break;
                   1080:                        }
                   1081:                }
                   1082:        }
1.277     ad       1083:        mutex_enter(p->p_lock);
1.234     yamt     1084:        kpsignal2(p, ksi);
1.277     ad       1085:        mutex_exit(p->p_lock);
1.160     christos 1086: }
                   1087:
1.243     ad       1088: /*
                   1089:  * sigismasked:
                   1090:  *
1.301     rmind    1091:  *     Returns true if signal is ignored or masked for the specified LWP.
1.243     ad       1092:  */
                   1093: int
                   1094: sigismasked(struct lwp *l, int sig)
1.29      cgd      1095: {
1.243     ad       1096:        struct proc *p = l->l_proc;
                   1097:
1.317     rmind    1098:        return sigismember(&p->p_sigctx.ps_sigignore, sig) ||
                   1099:            sigismember(&l->l_sigmask, sig);
1.243     ad       1100: }
1.29      cgd      1101:
1.243     ad       1102: /*
                   1103:  * sigpost:
                   1104:  *
1.301     rmind    1105:  *     Post a pending signal to an LWP.  Returns non-zero if the LWP may
                   1106:  *     be able to take the signal.
1.243     ad       1107:  */
1.297     rmind    1108: static int
1.317     rmind    1109: sigpost(struct lwp *l, sig_t action, int prop, int sig)
1.243     ad       1110: {
                   1111:        int rv, masked;
1.288     wrstuden 1112:        struct proc *p = l->l_proc;
1.148     christos 1113:
1.288     wrstuden 1114:        KASSERT(mutex_owned(p->p_lock));
1.148     christos 1115:
1.183     fvdl     1116:        /*
1.243     ad       1117:         * If the LWP is on the way out, sigclear() will be busy draining all
                   1118:         * pending signals.  Don't give it more.
1.126     jdolecek 1119:         */
1.243     ad       1120:        if (l->l_refcnt == 0)
                   1121:                return 0;
                   1122:
1.320     christos 1123:        SDT_PROBE(proc, kernel, , signal__send, l, p, sig, 0, 0);
1.303     darran   1124:
1.366     kamil    1125:        lwp_lock(l);
                   1126:        if (__predict_false((l->l_flag & LW_DBGSUSPEND) != 0)) {
                   1127:                if ((prop & SA_KILL) != 0)
                   1128:                        l->l_flag &= ~LW_DBGSUSPEND;
                   1129:                else {
                   1130:                        lwp_unlock(l);
                   1131:                        return 0;
                   1132:                }
                   1133:        }
                   1134:
1.289     wrstuden 1135:        /*
1.243     ad       1136:         * Have the LWP check for signals.  This ensures that even if no LWP
                   1137:         * is found to take the signal immediately, it should be taken soon.
                   1138:         */
1.246     pavel    1139:        l->l_flag |= LW_PENDSIG;
1.29      cgd      1140:
                   1141:        /*
1.297     rmind    1142:         * SIGCONT can be masked, but if LWP is stopped, it needs restart.
                   1143:         * Note: SIGKILL and SIGSTOP cannot be masked.
1.29      cgd      1144:         */
1.317     rmind    1145:        masked = sigismember(&l->l_sigmask, sig);
1.243     ad       1146:        if (masked && ((prop & SA_CONT) == 0 || l->l_stat != LSSTOP)) {
                   1147:                lwp_unlock(l);
                   1148:                return 0;
                   1149:        }
1.198     jdolecek 1150:
1.243     ad       1151:        /*
1.247     ad       1152:         * If killing the process, make it run fast.
                   1153:         */
                   1154:        if (__predict_false((prop & SA_KILL) != 0) &&
1.266     rmind    1155:            action == SIG_DFL && l->l_priority < MAXPRI_USER) {
                   1156:                KASSERT(l->l_class == SCHED_OTHER);
                   1157:                lwp_changepri(l, MAXPRI_USER);
                   1158:        }
1.247     ad       1159:
                   1160:        /*
1.243     ad       1161:         * If the LWP is running or on a run queue, then we win.  If it's
                   1162:         * sleeping interruptably, wake it and make it take the signal.  If
                   1163:         * the sleep isn't interruptable, then the chances are it will get
                   1164:         * to see the signal soon anyhow.  If suspended, it can't take the
                   1165:         * signal right now.  If it's LWP private or for all LWPs, save it
                   1166:         * for later; otherwise punt.
                   1167:         */
                   1168:        rv = 0;
                   1169:
                   1170:        switch (l->l_stat) {
                   1171:        case LSRUN:
                   1172:        case LSONPROC:
                   1173:                lwp_need_userret(l);
                   1174:                rv = 1;
                   1175:                break;
                   1176:
                   1177:        case LSSLEEP:
1.246     pavel    1178:                if ((l->l_flag & LW_SINTR) != 0) {
1.243     ad       1179:                        /* setrunnable() will release the lock. */
                   1180:                        setrunnable(l);
                   1181:                        return 1;
1.232     mrg      1182:                }
1.243     ad       1183:                break;
                   1184:
                   1185:        case LSSUSPENDED:
1.309     yamt     1186:                if ((prop & SA_KILL) != 0 && (l->l_flag & LW_WCORE) != 0) {
1.243     ad       1187:                        /* lwp_continue() will release the lock. */
                   1188:                        lwp_continue(l);
                   1189:                        return 1;
1.190     matt     1190:                }
1.243     ad       1191:                break;
                   1192:
                   1193:        case LSSTOP:
                   1194:                if ((prop & SA_STOP) != 0)
                   1195:                        break;
1.198     jdolecek 1196:
                   1197:                /*
1.243     ad       1198:                 * If the LWP is stopped and we are sending a continue
                   1199:                 * signal, then start it again.
1.198     jdolecek 1200:                 */
1.243     ad       1201:                if ((prop & SA_CONT) != 0) {
                   1202:                        if (l->l_wchan != NULL) {
                   1203:                                l->l_stat = LSSLEEP;
1.288     wrstuden 1204:                                p->p_nrlwps++;
1.243     ad       1205:                                rv = 1;
                   1206:                                break;
                   1207:                        }
                   1208:                        /* setrunnable() will release the lock. */
                   1209:                        setrunnable(l);
                   1210:                        return 1;
1.246     pavel    1211:                } else if (l->l_wchan == NULL || (l->l_flag & LW_SINTR) != 0) {
1.243     ad       1212:                        /* setrunnable() will release the lock. */
                   1213:                        setrunnable(l);
                   1214:                        return 1;
                   1215:                }
                   1216:                break;
1.198     jdolecek 1217:
1.243     ad       1218:        default:
                   1219:                break;
                   1220:        }
1.44      mycroft  1221:
1.243     ad       1222:        lwp_unlock(l);
                   1223:        return rv;
                   1224: }
1.29      cgd      1225:
1.243     ad       1226: /*
                   1227:  * Notify an LWP that it has a pending signal.
                   1228:  */
                   1229: void
                   1230: signotify(struct lwp *l)
                   1231: {
1.250     ad       1232:        KASSERT(lwp_locked(l, NULL));
1.29      cgd      1233:
1.246     pavel    1234:        l->l_flag |= LW_PENDSIG;
1.243     ad       1235:        lwp_need_userret(l);
                   1236: }
1.44      mycroft  1237:
1.243     ad       1238: /*
                   1239:  * Find an LWP within process p that is waiting on signal ksi, and hand
                   1240:  * it on.
                   1241:  */
1.297     rmind    1242: static int
1.243     ad       1243: sigunwait(struct proc *p, const ksiginfo_t *ksi)
                   1244: {
                   1245:        struct lwp *l;
                   1246:        int signo;
1.135     jdolecek 1247:
1.277     ad       1248:        KASSERT(mutex_owned(p->p_lock));
1.171     jdolecek 1249:
1.243     ad       1250:        signo = ksi->ksi_signo;
                   1251:
                   1252:        if (ksi->ksi_lid != 0) {
                   1253:                /*
                   1254:                 * Signal came via _lwp_kill().  Find the LWP and see if
                   1255:                 * it's interested.
                   1256:                 */
                   1257:                if ((l = lwp_find(p, ksi->ksi_lid)) == NULL)
                   1258:                        return 0;
                   1259:                if (l->l_sigwaited == NULL ||
                   1260:                    !sigismember(&l->l_sigwaitset, signo))
                   1261:                        return 0;
                   1262:        } else {
                   1263:                /*
                   1264:                 * Look for any LWP that may be interested.
                   1265:                 */
                   1266:                LIST_FOREACH(l, &p->p_sigwaiters, l_sigwaiter) {
                   1267:                        KASSERT(l->l_sigwaited != NULL);
                   1268:                        if (sigismember(&l->l_sigwaitset, signo))
                   1269:                                break;
                   1270:                }
                   1271:        }
                   1272:
                   1273:        if (l != NULL) {
                   1274:                l->l_sigwaited->ksi_info = ksi->ksi_info;
                   1275:                l->l_sigwaited = NULL;
                   1276:                LIST_REMOVE(l, l_sigwaiter);
                   1277:                cv_signal(&l->l_sigcv);
                   1278:                return 1;
                   1279:        }
                   1280:
                   1281:        return 0;
                   1282: }
                   1283:
                   1284: /*
                   1285:  * Send the signal to the process.  If the signal has an action, the action
                   1286:  * is usually performed by the target process rather than the caller; we add
                   1287:  * the signal to the set of pending signals for the process.
                   1288:  *
                   1289:  * Exceptions:
                   1290:  *   o When a stop signal is sent to a sleeping process that takes the
                   1291:  *     default action, the process is stopped without awakening it.
                   1292:  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
                   1293:  *     regardless of the signal action (eg, blocked or ignored).
                   1294:  *
                   1295:  * Other ignored signals are discarded immediately.
                   1296:  */
1.328     christos 1297: int
1.243     ad       1298: kpsignal2(struct proc *p, ksiginfo_t *ksi)
                   1299: {
1.301     rmind    1300:        int prop, signo = ksi->ksi_signo;
1.320     christos 1301:        struct lwp *l = NULL;
1.243     ad       1302:        ksiginfo_t *kp;
1.301     rmind    1303:        lwpid_t lid;
1.243     ad       1304:        sig_t action;
1.352     kamil    1305:        bool toall;
1.328     christos 1306:        int error = 0;
1.243     ad       1307:
1.276     ad       1308:        KASSERT(!cpu_intr_p());
                   1309:        KASSERT(mutex_owned(proc_lock));
1.277     ad       1310:        KASSERT(mutex_owned(p->p_lock));
1.243     ad       1311:        KASSERT((ksi->ksi_flags & KSI_QUEUED) == 0);
                   1312:        KASSERT(signo > 0 && signo < NSIG);
1.171     jdolecek 1313:
1.135     jdolecek 1314:        /*
1.243     ad       1315:         * If the process is being created by fork, is a zombie or is
                   1316:         * exiting, then just drop the signal here and bail out.
1.29      cgd      1317:         */
1.352     kamil    1318:        if (p->p_stat != SACTIVE && p->p_stat != SSTOP)
1.328     christos 1319:                return 0;
1.231     mrg      1320:
1.331     christos 1321:        /* XXX for core dump/debugger */
                   1322:        p->p_sigctx.ps_lwp = ksi->ksi_lid;
1.332     kamil    1323:        p->p_sigctx.ps_info = ksi->ksi_info;
1.331     christos 1324:
1.231     mrg      1325:        /*
1.243     ad       1326:         * Notify any interested parties of the signal.
1.301     rmind    1327:         */
1.243     ad       1328:        KNOTE(&p->p_klist, NOTE_SIGNAL | signo);
                   1329:
                   1330:        /*
                   1331:         * Some signals including SIGKILL must act on the entire process.
1.231     mrg      1332:         */
1.243     ad       1333:        kp = NULL;
                   1334:        prop = sigprop[signo];
                   1335:        toall = ((prop & SA_TOALL) != 0);
1.301     rmind    1336:        lid = toall ? 0 : ksi->ksi_lid;
1.231     mrg      1337:
1.243     ad       1338:        /*
                   1339:         * If proc is traced, always give parent a chance.
                   1340:         */
                   1341:        if (p->p_slflag & PSL_TRACED) {
                   1342:                action = SIG_DFL;
1.104     thorpej  1343:
1.243     ad       1344:                if (lid == 0) {
                   1345:                        /*
                   1346:                         * If the process is being traced and the signal
                   1347:                         * is being caught, make sure to save any ksiginfo.
                   1348:                         */
                   1349:                        if ((kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1.320     christos 1350:                                goto discard;
1.328     christos 1351:                        if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
                   1352:                                goto out;
1.243     ad       1353:                }
                   1354:        } else {
                   1355:
1.29      cgd      1356:                /*
1.243     ad       1357:                 * If the signal is being ignored, then drop it.  Note: we
                   1358:                 * don't set SIGCONT in ps_sigignore, and if it is set to
                   1359:                 * SIG_IGN, action will be SIG_DFL here.
1.29      cgd      1360:                 */
1.243     ad       1361:                if (sigismember(&p->p_sigctx.ps_sigignore, signo))
1.320     christos 1362:                        goto discard;
1.243     ad       1363:
                   1364:                else if (sigismember(&p->p_sigctx.ps_sigcatch, signo))
                   1365:                        action = SIG_CATCH;
                   1366:                else {
                   1367:                        action = SIG_DFL;
                   1368:
                   1369:                        /*
                   1370:                         * If sending a tty stop signal to a member of an
                   1371:                         * orphaned process group, discard the signal here if
                   1372:                         * the action is default; don't stop the process below
                   1373:                         * if sleeping, and don't clear any pending SIGCONT.
                   1374:                         */
1.276     ad       1375:                        if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1.320     christos 1376:                                goto discard;
1.243     ad       1377:
                   1378:                        if (prop & SA_KILL && p->p_nice > NZERO)
                   1379:                                p->p_nice = NZERO;
1.29      cgd      1380:                }
1.175     cl       1381:        }
                   1382:
1.243     ad       1383:        /*
                   1384:         * If stopping or continuing a process, discard any pending
                   1385:         * signals that would do the inverse.
                   1386:         */
                   1387:        if ((prop & (SA_CONT | SA_STOP)) != 0) {
1.301     rmind    1388:                ksiginfoq_t kq;
                   1389:
1.243     ad       1390:                ksiginfo_queue_init(&kq);
                   1391:                if ((prop & SA_CONT) != 0)
                   1392:                        sigclear(&p->p_sigpend, &stopsigmask, &kq);
                   1393:                if ((prop & SA_STOP) != 0)
                   1394:                        sigclear(&p->p_sigpend, &contsigmask, &kq);
                   1395:                ksiginfo_queue_drain(&kq);      /* XXXSMP */
                   1396:        }
                   1397:
1.317     rmind    1398:        /*
1.243     ad       1399:         * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
                   1400:         * please!), check if any LWPs are waiting on it.  If yes, pass on
                   1401:         * the signal info.  The signal won't be processed further here.
                   1402:         */
                   1403:        if ((prop & SA_CANTMASK) == 0 && !LIST_EMPTY(&p->p_sigwaiters) &&
                   1404:            p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0 &&
                   1405:            sigunwait(p, ksi))
1.320     christos 1406:                goto discard;
1.243     ad       1407:
                   1408:        /*
                   1409:         * XXXSMP Should be allocated by the caller, we're holding locks
                   1410:         * here.
                   1411:         */
                   1412:        if (kp == NULL && (kp = ksiginfo_alloc(p, ksi, PR_NOWAIT)) == NULL)
1.320     christos 1413:                goto discard;
1.243     ad       1414:
                   1415:        /*
                   1416:         * LWP private signals are easy - just find the LWP and post
                   1417:         * the signal to it.
                   1418:         */
                   1419:        if (lid != 0) {
1.352     kamil    1420:                l = lwp_find(p, lid);
1.243     ad       1421:                if (l != NULL) {
1.328     christos 1422:                        if ((error = sigput(&l->l_sigpend, p, kp)) != 0)
                   1423:                                goto out;
1.260     ad       1424:                        membar_producer();
1.339     christos 1425:                        if (sigpost(l, action, prop, kp->ksi_signo) != 0)
                   1426:                                signo = -1;
1.243     ad       1427:                }
                   1428:                goto out;
                   1429:        }
1.130     thorpej  1430:
1.243     ad       1431:        /*
1.288     wrstuden 1432:         * Some signals go to all LWPs, even if posted with _lwp_kill()
                   1433:         * or for an SA process.
1.243     ad       1434:         */
                   1435:        if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
                   1436:                if ((p->p_slflag & PSL_TRACED) != 0)
                   1437:                        goto deliver;
1.202     perry    1438:
1.176     cl       1439:                /*
                   1440:                 * If SIGCONT is default (or ignored) and process is
                   1441:                 * asleep, we are finished; the process should not
                   1442:                 * be awakened.
                   1443:                 */
1.243     ad       1444:                if ((prop & SA_CONT) != 0 && action == SIG_DFL)
                   1445:                        goto out;
                   1446:        } else {
1.176     cl       1447:                /*
1.300     rmind    1448:                 * Process is stopped or stopping.
                   1449:                 * - If traced, then no action is needed, unless killing.
                   1450:                 * - Run the process only if sending SIGCONT or SIGKILL.
1.176     cl       1451:                 */
1.300     rmind    1452:                if ((p->p_slflag & PSL_TRACED) != 0 && signo != SIGKILL) {
1.243     ad       1453:                        goto out;
1.300     rmind    1454:                }
1.297     rmind    1455:                if ((prop & SA_CONT) != 0 || signo == SIGKILL) {
1.243     ad       1456:                        /*
1.321     pgoyette 1457:                         * Re-adjust p_nstopchild if the process was
                   1458:                         * stopped but not yet collected by its parent.
1.243     ad       1459:                         */
1.321     pgoyette 1460:                        if (p->p_stat == SSTOP && !p->p_waited)
                   1461:                                p->p_pptr->p_nstopchild--;
1.243     ad       1462:                        p->p_stat = SACTIVE;
                   1463:                        p->p_sflag &= ~PS_STOPPING;
1.300     rmind    1464:                        if (p->p_slflag & PSL_TRACED) {
                   1465:                                KASSERT(signo == SIGKILL);
                   1466:                                goto deliver;
                   1467:                        }
1.29      cgd      1468:                        /*
1.297     rmind    1469:                         * Do not make signal pending if SIGCONT is default.
                   1470:                         *
                   1471:                         * If the process catches SIGCONT, let it handle the
                   1472:                         * signal itself (if waiting on event - process runs,
                   1473:                         * otherwise continues sleeping).
1.29      cgd      1474:                         */
1.324     christos 1475:                        if ((prop & SA_CONT) != 0) {
                   1476:                                p->p_xsig = SIGCONT;
1.326     christos 1477:                                p->p_sflag |= PS_CONTINUED;
1.324     christos 1478:                                child_psignal(p, 0);
                   1479:                                if (action == SIG_DFL) {
                   1480:                                        KASSERT(signo != SIGKILL);
                   1481:                                        goto deliver;
                   1482:                                }
1.297     rmind    1483:                        }
1.243     ad       1484:                } else if ((prop & SA_STOP) != 0) {
1.29      cgd      1485:                        /*
1.176     cl       1486:                         * Already stopped, don't need to stop again.
                   1487:                         * (If we did the shell could get confused.)
1.29      cgd      1488:                         */
1.243     ad       1489:                        goto out;
1.297     rmind    1490:                }
1.243     ad       1491:        }
1.297     rmind    1492:        /*
                   1493:         * Make signal pending.
                   1494:         */
1.300     rmind    1495:        KASSERT((p->p_slflag & PSL_TRACED) == 0);
1.328     christos 1496:        if ((error = sigput(&p->p_sigpend, p, kp)) != 0)
                   1497:                goto out;
1.301     rmind    1498: deliver:
1.243     ad       1499:        /*
1.271     yamt     1500:         * Before we set LW_PENDSIG on any LWP, ensure that the signal is
1.243     ad       1501:         * visible on the per process list (for sigispending()).  This
                   1502:         * is unlikely to be needed in practice, but...
                   1503:         */
1.260     ad       1504:        membar_producer();
1.29      cgd      1505:
                   1506:        /*
1.243     ad       1507:         * Try to find an LWP that can take the signal.
1.29      cgd      1508:         */
1.301     rmind    1509:        LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.317     rmind    1510:                if (sigpost(l, action, prop, kp->ksi_signo) && !toall)
1.301     rmind    1511:                        break;
1.288     wrstuden 1512:        }
1.320     christos 1513:        signo = -1;
1.301     rmind    1514: out:
                   1515:        /*
                   1516:         * If the ksiginfo wasn't used, then bin it.  XXXSMP freeing memory
                   1517:         * with locks held.  The caller should take care of this.
                   1518:         */
                   1519:        ksiginfo_free(kp);
1.320     christos 1520:        if (signo == -1)
1.328     christos 1521:                return error;
1.320     christos 1522: discard:
                   1523:        SDT_PROBE(proc, kernel, , signal__discard, l, p, signo, 0, 0);
1.328     christos 1524:        return error;
1.29      cgd      1525: }
                   1526:
1.243     ad       1527: void
                   1528: kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1.209     chs      1529: {
1.243     ad       1530:        struct proc *p = l->l_proc;
                   1531:
1.277     ad       1532:        KASSERT(mutex_owned(p->p_lock));
1.243     ad       1533:        (*p->p_emul->e_sendsig)(ksi, mask);
1.209     chs      1534: }
                   1535:
1.243     ad       1536: /*
1.272     yamt     1537:  * Stop any LWPs sleeping interruptably.
                   1538:  */
                   1539: static void
                   1540: proc_stop_lwps(struct proc *p)
                   1541: {
                   1542:        struct lwp *l;
                   1543:
1.277     ad       1544:        KASSERT(mutex_owned(p->p_lock));
1.272     yamt     1545:        KASSERT((p->p_sflag & PS_STOPPING) != 0);
                   1546:
                   1547:        LIST_FOREACH(l, &p->p_lwps, l_sibling) {
                   1548:                lwp_lock(l);
                   1549:                if (l->l_stat == LSSLEEP && (l->l_flag & LW_SINTR) != 0) {
                   1550:                        l->l_stat = LSSTOP;
                   1551:                        p->p_nrlwps--;
                   1552:                }
                   1553:                lwp_unlock(l);
                   1554:        }
                   1555: }
                   1556:
                   1557: /*
                   1558:  * Finish stopping of a process.  Mark it stopped and notify the parent.
                   1559:  *
1.362     kamil    1560:  * Drop p_lock briefly if ppsig is true.
1.272     yamt     1561:  */
                   1562: static void
1.340     kamil    1563: proc_stop_done(struct proc *p, int ppmask)
1.272     yamt     1564: {
                   1565:
1.276     ad       1566:        KASSERT(mutex_owned(proc_lock));
1.277     ad       1567:        KASSERT(mutex_owned(p->p_lock));
1.272     yamt     1568:        KASSERT((p->p_sflag & PS_STOPPING) != 0);
                   1569:        KASSERT(p->p_nrlwps == 0 || (p->p_nrlwps == 1 && p == curproc));
                   1570:
                   1571:        p->p_sflag &= ~PS_STOPPING;
                   1572:        p->p_stat = SSTOP;
                   1573:        p->p_waited = 0;
                   1574:        p->p_pptr->p_nstopchild++;
1.362     kamil    1575:
                   1576:        /* child_psignal drops p_lock briefly. */
                   1577:        child_psignal(p, ppmask);
                   1578:        cv_broadcast(&p->p_pptr->p_waitcv);
1.272     yamt     1579: }
                   1580:
1.357     kamil    1581: /*
                   1582:  * Stop the current process and switch away to the debugger notifying
                   1583:  * an event specific to a traced process only.
                   1584:  */
1.353     kamil    1585: void
1.365     kamil    1586: eventswitch(int code, int pe_report_event, int entity)
1.353     kamil    1587: {
                   1588:        struct lwp *l = curlwp;
                   1589:        struct proc *p = l->l_proc;
1.357     kamil    1590:        struct sigacts *ps;
                   1591:        sigset_t *mask;
                   1592:        sig_t action;
                   1593:        ksiginfo_t ksi;
                   1594:        const int signo = SIGTRAP;
1.353     kamil    1595:
                   1596:        KASSERT(mutex_owned(proc_lock));
                   1597:        KASSERT(mutex_owned(p->p_lock));
1.357     kamil    1598:        KASSERT(p->p_pptr != initproc);
1.353     kamil    1599:        KASSERT(l->l_stat == LSONPROC);
1.357     kamil    1600:        KASSERT(ISSET(p->p_slflag, PSL_TRACED));
                   1601:        KASSERT(!ISSET(l->l_flag, LW_SYSTEM));
1.353     kamil    1602:        KASSERT(p->p_nrlwps > 0);
1.357     kamil    1603:        KASSERT((code == TRAP_CHLD) || (code == TRAP_LWP) ||
                   1604:                (code == TRAP_EXEC));
1.368     kamil    1605:        KASSERT((code != TRAP_CHLD) || (entity > 1)); /* prevent pid1 */
                   1606:        KASSERT((code != TRAP_LWP) || (entity > 0));
1.353     kamil    1607:
1.367     kamil    1608: repeat:
1.354     kamil    1609:        /*
1.359     kamil    1610:         * If we are exiting, demise now.
                   1611:         *
                   1612:         * This avoids notifying tracer and deadlocking.
1.367     kamil    1613:         */
1.359     kamil    1614:        if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
                   1615:                mutex_exit(p->p_lock);
                   1616:                mutex_exit(proc_lock);
1.372     kamil    1617:
                   1618:                if (pe_report_event == PTRACE_LWP_EXIT) {
                   1619:                        /* Avoid double lwp_exit() and panic. */
                   1620:                        return;
                   1621:                }
                   1622:
1.359     kamil    1623:                lwp_exit(l);
                   1624:                panic("eventswitch");
                   1625:                /* NOTREACHED */
                   1626:        }
                   1627:
                   1628:        /*
1.367     kamil    1629:         * If we are no longer traced, abandon this event signal.
                   1630:         *
                   1631:         * This avoids killing a process after detaching the debugger.
                   1632:         */
                   1633:        if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
                   1634:                mutex_exit(p->p_lock);
                   1635:                mutex_exit(proc_lock);
                   1636:                return;
                   1637:        }
                   1638:
                   1639:        /*
1.354     kamil    1640:         * If there's a pending SIGKILL process it immediately.
                   1641:         */
                   1642:        if (p->p_xsig == SIGKILL ||
                   1643:            sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
1.357     kamil    1644:                mutex_exit(p->p_lock);
1.354     kamil    1645:                mutex_exit(proc_lock);
                   1646:                return;
                   1647:        }
                   1648:
1.367     kamil    1649:        /*
                   1650:         * The process is already stopping.
                   1651:         */
                   1652:        if ((p->p_sflag & PS_STOPPING) != 0) {
1.370     kamil    1653:                mutex_exit(proc_lock);
                   1654:                sigswitch_unlock_and_switch_away(l);
1.367     kamil    1655:                mutex_enter(proc_lock);
                   1656:                mutex_enter(p->p_lock);
1.370     kamil    1657:                goto repeat;
1.367     kamil    1658:        }
                   1659:
1.357     kamil    1660:        KSI_INIT_TRAP(&ksi);
                   1661:        ksi.ksi_lid = l->l_lid;
1.365     kamil    1662:        ksi.ksi_signo = signo;
                   1663:        ksi.ksi_code = code;
                   1664:        ksi.ksi_pe_report_event = pe_report_event;
                   1665:
                   1666:        CTASSERT(sizeof(ksi.ksi_pe_other_pid) == sizeof(ksi.ksi_pe_lwp));
                   1667:        ksi.ksi_pe_other_pid = entity;
1.357     kamil    1668:
                   1669:        /* Needed for ktrace */
                   1670:        ps = p->p_sigacts;
                   1671:        action = SIGACTION_PS(ps, signo).sa_handler;
                   1672:        mask = &l->l_sigmask;
                   1673:
1.353     kamil    1674:        p->p_xsig = signo;
                   1675:        p->p_sigctx.ps_faked = true;
1.357     kamil    1676:        p->p_sigctx.ps_lwp = ksi.ksi_lid;
                   1677:        p->p_sigctx.ps_info = ksi.ksi_info;
1.353     kamil    1678:
1.369     kamil    1679:        sigswitch(0, signo, true);
1.357     kamil    1680:
1.361     kamil    1681:        if (code == TRAP_CHLD) {
                   1682:                mutex_enter(proc_lock);
                   1683:                while (l->l_vforkwaiting)
                   1684:                        cv_wait(&l->l_waitcv, proc_lock);
                   1685:                mutex_exit(proc_lock);
                   1686:        }
1.357     kamil    1687:
                   1688:        if (ktrpoint(KTR_PSIG)) {
                   1689:                if (p->p_emul->e_ktrpsig)
                   1690:                        p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
                   1691:                else
                   1692:                        ktrpsig(signo, action, mask, &ksi);
                   1693:        }
1.353     kamil    1694: }
                   1695:
1.272     yamt     1696: /*
1.243     ad       1697:  * Stop the current process and switch away when being stopped or traced.
                   1698:  */
1.369     kamil    1699: static void
                   1700: sigswitch(int ppmask, int signo, bool proc_lock_held)
1.209     chs      1701: {
1.272     yamt     1702:        struct lwp *l = curlwp;
1.243     ad       1703:        struct proc *p = l->l_proc;
                   1704:
1.277     ad       1705:        KASSERT(mutex_owned(p->p_lock));
1.250     ad       1706:        KASSERT(l->l_stat == LSONPROC);
                   1707:        KASSERT(p->p_nrlwps > 0);
1.243     ad       1708:
1.369     kamil    1709:        if (proc_lock_held) {
                   1710:                KASSERT(mutex_owned(proc_lock));
                   1711:        } else {
                   1712:                KASSERT(!mutex_owned(proc_lock));
                   1713:        }
                   1714:
1.243     ad       1715:        /*
                   1716:         * On entry we know that the process needs to stop.  If it's
                   1717:         * the result of a 'sideways' stop signal that has been sourced
                   1718:         * through issignal(), then stop other LWPs in the process too.
                   1719:         */
                   1720:        if (p->p_stat == SACTIVE && (p->p_sflag & PS_STOPPING) == 0) {
                   1721:                KASSERT(signo != 0);
1.334     christos 1722:                proc_stop(p, signo);
1.250     ad       1723:                KASSERT(p->p_nrlwps > 0);
1.243     ad       1724:        }
                   1725:
                   1726:        /*
                   1727:         * If we are the last live LWP, and the stop was a result of
                   1728:         * a new signal, then signal the parent.
                   1729:         */
                   1730:        if ((p->p_sflag & PS_STOPPING) != 0) {
1.369     kamil    1731:                if (!proc_lock_held && !mutex_tryenter(proc_lock)) {
1.277     ad       1732:                        mutex_exit(p->p_lock);
1.276     ad       1733:                        mutex_enter(proc_lock);
1.277     ad       1734:                        mutex_enter(p->p_lock);
1.243     ad       1735:                }
                   1736:
                   1737:                if (p->p_nrlwps == 1 && (p->p_sflag & PS_STOPPING) != 0) {
1.272     yamt     1738:                        /*
                   1739:                         * Note that proc_stop_done() can drop
1.277     ad       1740:                         * p->p_lock briefly.
1.272     yamt     1741:                         */
1.340     kamil    1742:                        proc_stop_done(p, ppmask);
1.243     ad       1743:                }
                   1744:
1.276     ad       1745:                mutex_exit(proc_lock);
1.243     ad       1746:        }
                   1747:
1.370     kamil    1748:        sigswitch_unlock_and_switch_away(l);
                   1749: }
                   1750:
                   1751: /*
                   1752:  * Unlock and switch away.
                   1753:  */
                   1754: static void
                   1755: sigswitch_unlock_and_switch_away(struct lwp *l)
                   1756: {
                   1757:        struct proc *p;
                   1758:        int biglocks;
                   1759:
                   1760:        p = l->l_proc;
                   1761:
                   1762:        KASSERT(mutex_owned(p->p_lock));
1.369     kamil    1763:        KASSERT(!mutex_owned(proc_lock));
1.370     kamil    1764:
                   1765:        KASSERT(l->l_stat == LSONPROC);
                   1766:        KASSERT(p->p_nrlwps > 0);
                   1767:
1.245     ad       1768:        KERNEL_UNLOCK_ALL(l, &biglocks);
1.243     ad       1769:        if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
                   1770:                p->p_nrlwps--;
                   1771:                lwp_lock(l);
                   1772:                KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSLEEP);
                   1773:                l->l_stat = LSSTOP;
                   1774:                lwp_unlock(l);
                   1775:        }
                   1776:
1.277     ad       1777:        mutex_exit(p->p_lock);
1.243     ad       1778:        lwp_lock(l);
1.253     yamt     1779:        mi_switch(l);
1.245     ad       1780:        KERNEL_LOCK(biglocks, l);
1.209     chs      1781: }
                   1782:
1.243     ad       1783: /*
                   1784:  * Check for a signal from the debugger.
                   1785:  */
1.297     rmind    1786: static int
1.293     ad       1787: sigchecktrace(void)
1.130     thorpej  1788: {
1.243     ad       1789:        struct lwp *l = curlwp;
1.130     thorpej  1790:        struct proc *p = l->l_proc;
1.243     ad       1791:        int signo;
                   1792:
1.277     ad       1793:        KASSERT(mutex_owned(p->p_lock));
1.130     thorpej  1794:
1.292     ad       1795:        /* If there's a pending SIGKILL, process it immediately. */
                   1796:        if (sigismember(&p->p_sigpend.sp_set, SIGKILL))
                   1797:                return 0;
                   1798:
1.243     ad       1799:        /*
                   1800:         * If we are no longer being traced, or the parent didn't
1.305     christos 1801:         * give us a signal, or we're stopping, look for more signals.
1.243     ad       1802:         */
1.322     christos 1803:        if ((p->p_slflag & PSL_TRACED) == 0 || p->p_xsig == 0 ||
1.305     christos 1804:            (p->p_sflag & PS_STOPPING) != 0)
1.243     ad       1805:                return 0;
1.130     thorpej  1806:
1.243     ad       1807:        /*
                   1808:         * If the new signal is being masked, look for other signals.
                   1809:         * `p->p_sigctx.ps_siglist |= mask' is done in setrunnable().
                   1810:         */
1.322     christos 1811:        signo = p->p_xsig;
                   1812:        p->p_xsig = 0;
1.317     rmind    1813:        if (sigismember(&l->l_sigmask, signo)) {
1.243     ad       1814:                signo = 0;
1.317     rmind    1815:        }
1.243     ad       1816:        return signo;
1.79      mycroft  1817: }
                   1818:
1.29      cgd      1819: /*
                   1820:  * If the current process has received a signal (should be caught or cause
                   1821:  * termination, should interrupt current syscall), return the signal number.
1.243     ad       1822:  *
1.29      cgd      1823:  * Stop signals with default action are processed immediately, then cleared;
                   1824:  * they aren't returned.  This is checked after each entry to the system for
1.243     ad       1825:  * a syscall or trap.
                   1826:  *
                   1827:  * We will also return -1 if the process is exiting and the current LWP must
                   1828:  * follow suit.
1.29      cgd      1829:  */
                   1830: int
1.130     thorpej  1831: issignal(struct lwp *l)
1.29      cgd      1832: {
1.293     ad       1833:        struct proc *p;
                   1834:        int signo, prop;
                   1835:        sigpend_t *sp;
1.243     ad       1836:        sigset_t ss;
                   1837:
1.293     ad       1838:        p = l->l_proc;
                   1839:        sp = NULL;
                   1840:        signo = 0;
                   1841:
                   1842:        KASSERT(p == curproc);
1.277     ad       1843:        KASSERT(mutex_owned(p->p_lock));
1.29      cgd      1844:
1.243     ad       1845:        for (;;) {
                   1846:                /* Discard any signals that we have decided not to take. */
1.314     christos 1847:                if (signo != 0) {
1.243     ad       1848:                        (void)sigget(sp, NULL, signo, NULL);
1.314     christos 1849:                }
1.144     fvdl     1850:
1.243     ad       1851:                /*
                   1852:                 * If the process is stopped/stopping, then stop ourselves
                   1853:                 * now that we're on the kernel/userspace boundary.  When
                   1854:                 * we awaken, check for a signal from the debugger.
                   1855:                 */
                   1856:                if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
1.370     kamil    1857:                        sigswitch_unlock_and_switch_away(l);
1.357     kamil    1858:                        mutex_enter(p->p_lock);
1.371     kamil    1859:                        continue;
1.351     kamil    1860:                } else if (p->p_stat == SACTIVE)
                   1861:                        signo = sigchecktrace();
                   1862:                else
1.243     ad       1863:                        signo = 0;
1.238     ad       1864:
1.293     ad       1865:                /* Signals from the debugger are "out of band". */
                   1866:                sp = NULL;
                   1867:
1.130     thorpej  1868:                /*
1.243     ad       1869:                 * If the debugger didn't provide a signal, find a pending
                   1870:                 * signal from our set.  Check per-LWP signals first, and
                   1871:                 * then per-process.
                   1872:                 */
                   1873:                if (signo == 0) {
                   1874:                        sp = &l->l_sigpend;
                   1875:                        ss = sp->sp_set;
1.285     ad       1876:                        if ((p->p_lflag & PL_PPWAIT) != 0)
1.345     kamil    1877:                                sigminusset(&vforksigmask, &ss);
1.243     ad       1878:                        sigminusset(&l->l_sigmask, &ss);
                   1879:
                   1880:                        if ((signo = firstsig(&ss)) == 0) {
                   1881:                                sp = &p->p_sigpend;
                   1882:                                ss = sp->sp_set;
1.285     ad       1883:                                if ((p->p_lflag & PL_PPWAIT) != 0)
1.345     kamil    1884:                                        sigminusset(&vforksigmask, &ss);
1.243     ad       1885:                                sigminusset(&l->l_sigmask, &ss);
                   1886:
                   1887:                                if ((signo = firstsig(&ss)) == 0) {
                   1888:                                        /*
                   1889:                                         * No signal pending - clear the
                   1890:                                         * indicator and bail out.
                   1891:                                         */
                   1892:                                        lwp_lock(l);
1.246     pavel    1893:                                        l->l_flag &= ~LW_PENDSIG;
1.243     ad       1894:                                        lwp_unlock(l);
                   1895:                                        sp = NULL;
                   1896:                                        break;
                   1897:                                }
                   1898:                        }
1.79      mycroft  1899:                }
1.42      mycroft  1900:
1.29      cgd      1901:                /*
1.243     ad       1902:                 * We should see pending but ignored signals only if
                   1903:                 * we are being traced.
1.29      cgd      1904:                 */
1.243     ad       1905:                if (sigismember(&p->p_sigctx.ps_sigignore, signo) &&
                   1906:                    (p->p_slflag & PSL_TRACED) == 0) {
                   1907:                        /* Discard the signal. */
1.29      cgd      1908:                        continue;
1.243     ad       1909:                }
1.42      mycroft  1910:
1.243     ad       1911:                /*
                   1912:                 * If traced, always stop, and stay stopped until released
1.342     kamil    1913:                 * by the debugger.  If the our parent is our debugger waiting
                   1914:                 * for us and we vforked, don't hang as we could deadlock.
1.243     ad       1915:                 */
1.344     kamil    1916:                if (ISSET(p->p_slflag, PSL_TRACED) && signo != SIGKILL &&
                   1917:                    !(ISSET(p->p_lflag, PL_PPWAIT) &&
                   1918:                     (p->p_pptr == p->p_opptr))) {
1.313     christos 1919:                        /*
                   1920:                         * Take the signal, but don't remove it from the
                   1921:                         * siginfo queue, because the debugger can send
                   1922:                         * it later.
                   1923:                         */
1.315     christos 1924:                        if (sp)
                   1925:                                sigdelset(&sp->sp_set, signo);
1.322     christos 1926:                        p->p_xsig = signo;
1.184     manu     1927:
1.343     kamil    1928:                        /* Handling of signal trace */
1.369     kamil    1929:                        sigswitch(0, signo, false);
1.357     kamil    1930:                        mutex_enter(p->p_lock);
1.29      cgd      1931:
1.243     ad       1932:                        /* Check for a signal from the debugger. */
1.293     ad       1933:                        if ((signo = sigchecktrace()) == 0)
1.29      cgd      1934:                                continue;
1.293     ad       1935:
                   1936:                        /* Signals from the debugger are "out of band". */
                   1937:                        sp = NULL;
1.29      cgd      1938:                }
                   1939:
1.243     ad       1940:                prop = sigprop[signo];
1.42      mycroft  1941:
1.29      cgd      1942:                /*
                   1943:                 * Decide whether the signal should be returned.
                   1944:                 */
1.243     ad       1945:                switch ((long)SIGACTION(p, signo).sa_handler) {
1.33      cgd      1946:                case (long)SIG_DFL:
1.29      cgd      1947:                        /*
                   1948:                         * Don't take default actions on system processes.
                   1949:                         */
                   1950:                        if (p->p_pid <= 1) {
                   1951: #ifdef DIAGNOSTIC
                   1952:                                /*
                   1953:                                 * Are you sure you want to ignore SIGSEGV
                   1954:                                 * in init? XXX
                   1955:                                 */
1.243     ad       1956:                                printf_nolog("Process (pid %d) got sig %d\n",
                   1957:                                    p->p_pid, signo);
1.29      cgd      1958: #endif
1.243     ad       1959:                                continue;
1.29      cgd      1960:                        }
1.243     ad       1961:
1.29      cgd      1962:                        /*
1.243     ad       1963:                         * If there is a pending stop signal to process with
                   1964:                         * default action, stop here, then clear the signal.
                   1965:                         * However, if process is member of an orphaned
1.29      cgd      1966:                         * process group, ignore tty stop signals.
                   1967:                         */
                   1968:                        if (prop & SA_STOP) {
1.276     ad       1969:                                /*
                   1970:                                 * XXX Don't hold proc_lock for p_lflag,
                   1971:                                 * but it's not a big deal.
                   1972:                                 */
1.346     kamil    1973:                                if ((ISSET(p->p_slflag, PSL_TRACED) &&
                   1974:                                     !(ISSET(p->p_lflag, PL_PPWAIT) &&
                   1975:                                     (p->p_pptr == p->p_opptr))) ||
1.301     rmind    1976:                                    ((p->p_lflag & PL_ORPHANPG) != 0 &&
1.243     ad       1977:                                    prop & SA_TTYSTOP)) {
1.301     rmind    1978:                                        /* Ignore the signal. */
1.243     ad       1979:                                        continue;
                   1980:                                }
                   1981:                                /* Take the signal. */
                   1982:                                (void)sigget(sp, NULL, signo, NULL);
1.322     christos 1983:                                p->p_xsig = signo;
1.327     christos 1984:                                p->p_sflag &= ~PS_CONTINUED;
1.243     ad       1985:                                signo = 0;
1.369     kamil    1986:                                sigswitch(PS_NOCLDSTOP, p->p_xsig, false);
1.357     kamil    1987:                                mutex_enter(p->p_lock);
1.29      cgd      1988:                        } else if (prop & SA_IGNORE) {
                   1989:                                /*
                   1990:                                 * Except for SIGCONT, shouldn't get here.
                   1991:                                 * Default action is to ignore; drop it.
                   1992:                                 */
1.243     ad       1993:                                continue;
                   1994:                        }
                   1995:                        break;
1.29      cgd      1996:
1.33      cgd      1997:                case (long)SIG_IGN:
1.243     ad       1998: #ifdef DEBUG_ISSIGNAL
1.29      cgd      1999:                        /*
                   2000:                         * Masking above should prevent us ever trying
                   2001:                         * to take action on an ignored signal other
                   2002:                         * than SIGCONT, unless process is traced.
                   2003:                         */
                   2004:                        if ((prop & SA_CONT) == 0 &&
1.243     ad       2005:                            (p->p_slflag & PSL_TRACED) == 0)
                   2006:                                printf_nolog("issignal\n");
1.128     jdolecek 2007: #endif
1.243     ad       2008:                        continue;
1.29      cgd      2009:
                   2010:                default:
                   2011:                        /*
1.243     ad       2012:                         * This signal has an action, let postsig() process
                   2013:                         * it.
1.29      cgd      2014:                         */
1.243     ad       2015:                        break;
1.29      cgd      2016:                }
1.243     ad       2017:
                   2018:                break;
1.29      cgd      2019:        }
1.42      mycroft  2020:
1.243     ad       2021:        l->l_sigpendset = sp;
                   2022:        return signo;
1.29      cgd      2023: }
                   2024:
                   2025: /*
1.243     ad       2026:  * Take the action for the specified signal
                   2027:  * from the current set of pending signals.
1.29      cgd      2028:  */
1.179     christos 2029: void
1.243     ad       2030: postsig(int signo)
1.29      cgd      2031: {
1.243     ad       2032:        struct lwp      *l;
                   2033:        struct proc     *p;
                   2034:        struct sigacts  *ps;
                   2035:        sig_t           action;
                   2036:        sigset_t        *returnmask;
                   2037:        ksiginfo_t      ksi;
                   2038:
                   2039:        l = curlwp;
                   2040:        p = l->l_proc;
                   2041:        ps = p->p_sigacts;
                   2042:
1.277     ad       2043:        KASSERT(mutex_owned(p->p_lock));
1.243     ad       2044:        KASSERT(signo > 0);
                   2045:
                   2046:        /*
                   2047:         * Set the new mask value and also defer further occurrences of this
                   2048:         * signal.
                   2049:         *
1.268     yamt     2050:         * Special case: user has done a sigsuspend.  Here the current mask is
1.293     ad       2051:         * not of interest, but rather the mask from before the sigsuspend is
1.243     ad       2052:         * what we want restored after the signal processing is completed.
                   2053:         */
                   2054:        if (l->l_sigrestore) {
                   2055:                returnmask = &l->l_sigoldmask;
                   2056:                l->l_sigrestore = 0;
                   2057:        } else
                   2058:                returnmask = &l->l_sigmask;
1.29      cgd      2059:
1.243     ad       2060:        /*
                   2061:         * Commit to taking the signal before releasing the mutex.
                   2062:         */
                   2063:        action = SIGACTION_PS(ps, signo).sa_handler;
1.275     ad       2064:        l->l_ru.ru_nsignals++;
1.314     christos 2065:        if (l->l_sigpendset == NULL) {
1.313     christos 2066:                /* From the debugger */
1.332     kamil    2067:                if (p->p_sigctx.ps_faked &&
                   2068:                    signo == p->p_sigctx.ps_info._signo) {
                   2069:                        KSI_INIT(&ksi);
                   2070:                        ksi.ksi_info = p->p_sigctx.ps_info;
                   2071:                        ksi.ksi_lid = p->p_sigctx.ps_lwp;
                   2072:                        p->p_sigctx.ps_faked = false;
                   2073:                } else {
                   2074:                        if (!siggetinfo(&l->l_sigpend, &ksi, signo))
                   2075:                                (void)siggetinfo(&p->p_sigpend, &ksi, signo);
                   2076:                }
1.314     christos 2077:        } else
                   2078:                sigget(l->l_sigpendset, &ksi, signo, NULL);
1.104     thorpej  2079:
1.255     ad       2080:        if (ktrpoint(KTR_PSIG)) {
1.277     ad       2081:                mutex_exit(p->p_lock);
1.330     martin   2082:                if (p->p_emul->e_ktrpsig)
                   2083:                        p->p_emul->e_ktrpsig(signo, action,
                   2084:                            returnmask, &ksi);
                   2085:                else
                   2086:                        ktrpsig(signo, action, returnmask, &ksi);
1.277     ad       2087:                mutex_enter(p->p_lock);
1.243     ad       2088:        }
1.130     thorpej  2089:
1.320     christos 2090:        SDT_PROBE(proc, kernel, , signal__handle, signo, &ksi, action, 0, 0);
                   2091:
1.243     ad       2092:        if (action == SIG_DFL) {
1.175     cl       2093:                /*
1.243     ad       2094:                 * Default action, where the default is to kill
                   2095:                 * the process.  (Other cases were ignored above.)
1.175     cl       2096:                 */
1.243     ad       2097:                sigexit(l, signo);
                   2098:                return;
1.175     cl       2099:        }
                   2100:
1.202     perry    2101:        /*
1.243     ad       2102:         * If we get here, the signal must be caught.
1.130     thorpej  2103:         */
                   2104: #ifdef DIAGNOSTIC
1.243     ad       2105:        if (action == SIG_IGN || sigismember(&l->l_sigmask, signo))
                   2106:                panic("postsig action");
1.130     thorpej  2107: #endif
1.144     fvdl     2108:
1.243     ad       2109:        kpsendsig(l, &ksi, returnmask);
1.29      cgd      2110: }
                   2111:
1.133     nathanw  2112: /*
1.290     ad       2113:  * sendsig:
                   2114:  *
                   2115:  *     Default signal delivery method for NetBSD.
                   2116:  */
                   2117: void
                   2118: sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
                   2119: {
                   2120:        struct sigacts *sa;
                   2121:        int sig;
                   2122:
                   2123:        sig = ksi->ksi_signo;
                   2124:        sa = curproc->p_sigacts;
                   2125:
                   2126:        switch (sa->sa_sigdesc[sig].sd_vers)  {
                   2127:        case 0:
                   2128:        case 1:
                   2129:                /* Compat for 1.6 and earlier. */
                   2130:                if (sendsig_sigcontext_vec == NULL) {
                   2131:                        break;
                   2132:                }
                   2133:                (*sendsig_sigcontext_vec)(ksi, mask);
                   2134:                return;
                   2135:        case 2:
1.291     ad       2136:        case 3:
1.290     ad       2137:                sendsig_siginfo(ksi, mask);
                   2138:                return;
                   2139:        default:
                   2140:                break;
                   2141:        }
                   2142:
                   2143:        printf("sendsig: bad version %d\n", sa->sa_sigdesc[sig].sd_vers);
                   2144:        sigexit(curlwp, SIGILL);
                   2145: }
                   2146:
                   2147: /*
1.243     ad       2148:  * sendsig_reset:
1.133     nathanw  2149:  *
1.243     ad       2150:  *     Reset the signal action.  Called from emulation specific sendsig()
                   2151:  *     before unlocking to deliver the signal.
1.29      cgd      2152:  */
                   2153: void
1.243     ad       2154: sendsig_reset(struct lwp *l, int signo)
1.29      cgd      2155: {
1.243     ad       2156:        struct proc *p = l->l_proc;
                   2157:        struct sigacts *ps = p->p_sigacts;
1.29      cgd      2158:
1.277     ad       2159:        KASSERT(mutex_owned(p->p_lock));
1.106     thorpej  2160:
1.243     ad       2161:        p->p_sigctx.ps_lwp = 0;
1.332     kamil    2162:        memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
1.243     ad       2163:
1.259     ad       2164:        mutex_enter(&ps->sa_mutex);
1.317     rmind    2165:        sigplusset(&SIGACTION_PS(ps, signo).sa_mask, &l->l_sigmask);
1.243     ad       2166:        if (SIGACTION_PS(ps, signo).sa_flags & SA_RESETHAND) {
                   2167:                sigdelset(&p->p_sigctx.ps_sigcatch, signo);
                   2168:                if (signo != SIGCONT && sigprop[signo] & SA_IGNORE)
                   2169:                        sigaddset(&p->p_sigctx.ps_sigignore, signo);
                   2170:                SIGACTION_PS(ps, signo).sa_handler = SIG_DFL;
1.29      cgd      2171:        }
1.259     ad       2172:        mutex_exit(&ps->sa_mutex);
1.29      cgd      2173: }
                   2174:
                   2175: /*
                   2176:  * Kill the current process for stated reason.
                   2177:  */
1.52      christos 2178: void
1.122     manu     2179: killproc(struct proc *p, const char *why)
1.29      cgd      2180: {
1.276     ad       2181:
                   2182:        KASSERT(mutex_owned(proc_lock));
                   2183:
1.29      cgd      2184:        log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1.243     ad       2185:        uprintf_locked("sorry, pid %d was killed: %s\n", p->p_pid, why);
1.29      cgd      2186:        psignal(p, SIGKILL);
                   2187: }
                   2188:
                   2189: /*
                   2190:  * Force the current process to exit with the specified signal, dumping core
1.243     ad       2191:  * if appropriate.  We bypass the normal tests for masked and caught
                   2192:  * signals, allowing unrecoverable failures to terminate the process without
                   2193:  * changing signal state.  Mark the accounting record with the signal
                   2194:  * termination.  If dumping core, save the signal number for the debugger.
                   2195:  * Calls exit and does not return.
1.29      cgd      2196:  */
1.243     ad       2197: void
                   2198: sigexit(struct lwp *l, int signo)
                   2199: {
1.323     christos 2200:        int exitsig, error, docore;
1.243     ad       2201:        struct proc *p;
                   2202:        struct lwp *t;
1.96      fair     2203:
1.243     ad       2204:        p = l->l_proc;
1.96      fair     2205:
1.277     ad       2206:        KASSERT(mutex_owned(p->p_lock));
1.243     ad       2207:        KERNEL_UNLOCK_ALL(l, NULL);
1.96      fair     2208:
1.243     ad       2209:        /*
                   2210:         * Don't permit coredump() multiple times in the same process.
                   2211:         * Call back into sigexit, where we will be suspended until
                   2212:         * the deed is done.  Note that this is a recursive call, but
1.246     pavel    2213:         * LW_WCORE will prevent us from coming back this way.
1.243     ad       2214:         */
                   2215:        if ((p->p_sflag & PS_WCORE) != 0) {
                   2216:                lwp_lock(l);
1.246     pavel    2217:                l->l_flag |= (LW_WCORE | LW_WEXIT | LW_WSUSPEND);
1.243     ad       2218:                lwp_unlock(l);
1.277     ad       2219:                mutex_exit(p->p_lock);
1.243     ad       2220:                lwp_userret(l);
1.281     ad       2221:                panic("sigexit 1");
                   2222:                /* NOTREACHED */
                   2223:        }
                   2224:
                   2225:        /* If process is already on the way out, then bail now. */
                   2226:        if ((p->p_sflag & PS_WEXIT) != 0) {
                   2227:                mutex_exit(p->p_lock);
                   2228:                lwp_exit(l);
                   2229:                panic("sigexit 2");
1.243     ad       2230:                /* NOTREACHED */
                   2231:        }
1.130     thorpej  2232:
                   2233:        /*
1.243     ad       2234:         * Prepare all other LWPs for exit.  If dumping core, suspend them
                   2235:         * so that their registers are available long enough to be dumped.
                   2236:         */
                   2237:        if ((docore = (sigprop[signo] & SA_CORE)) != 0) {
                   2238:                p->p_sflag |= PS_WCORE;
                   2239:                for (;;) {
                   2240:                        LIST_FOREACH(t, &p->p_lwps, l_sibling) {
                   2241:                                lwp_lock(t);
                   2242:                                if (t == l) {
1.366     kamil    2243:                                        t->l_flag &=
                   2244:                                            ~(LW_WSUSPEND | LW_DBGSUSPEND);
1.243     ad       2245:                                        lwp_unlock(t);
                   2246:                                        continue;
                   2247:                                }
1.246     pavel    2248:                                t->l_flag |= (LW_WCORE | LW_WEXIT);
1.243     ad       2249:                                lwp_suspend(l, t);
                   2250:                        }
1.130     thorpej  2251:
1.243     ad       2252:                        if (p->p_nrlwps == 1)
                   2253:                                break;
1.130     thorpej  2254:
1.243     ad       2255:                        /*
                   2256:                         * Kick any LWPs sitting in lwp_wait1(), and wait
                   2257:                         * for everyone else to stop before proceeding.
                   2258:                         */
                   2259:                        p->p_nlwpwait++;
                   2260:                        cv_broadcast(&p->p_lwpcv);
1.277     ad       2261:                        cv_wait(&p->p_lwpcv, p->p_lock);
1.243     ad       2262:                        p->p_nlwpwait--;
                   2263:                }
                   2264:        }
1.130     thorpej  2265:
1.243     ad       2266:        exitsig = signo;
                   2267:        p->p_acflag |= AXSIG;
1.332     kamil    2268:        memset(&p->p_sigctx.ps_info, 0, sizeof(p->p_sigctx.ps_info));
                   2269:        p->p_sigctx.ps_info._signo = signo;
                   2270:        p->p_sigctx.ps_info._code = SI_NOINFO;
1.130     thorpej  2271:
1.243     ad       2272:        if (docore) {
1.281     ad       2273:                mutex_exit(p->p_lock);
1.323     christos 2274:                error = (*coredump_vec)(l, NULL);
1.102     sommerfe 2275:
                   2276:                if (kern_logsigexit) {
1.224     ad       2277:                        int uid = l->l_cred ?
                   2278:                            (int)kauth_cred_geteuid(l->l_cred) : -1;
1.102     sommerfe 2279:
1.202     perry    2280:                        if (error)
1.102     sommerfe 2281:                                log(LOG_INFO, lognocoredump, p->p_pid,
1.243     ad       2282:                                    p->p_comm, uid, signo, error);
1.102     sommerfe 2283:                        else
                   2284:                                log(LOG_INFO, logcoredump, p->p_pid,
1.243     ad       2285:                                    p->p_comm, uid, signo);
1.96      fair     2286:                }
                   2287:
1.240     elad     2288: #ifdef PAX_SEGVGUARD
1.249     thorpej  2289:                pax_segvguard(l, p->p_textvp, p->p_comm, true);
1.240     elad     2290: #endif /* PAX_SEGVGUARD */
1.281     ad       2291:                /* Acquire the sched state mutex.  exit1() will release it. */
                   2292:                mutex_enter(p->p_lock);
1.323     christos 2293:                if (error == 0)
                   2294:                        p->p_sflag |= PS_COREDUMP;
1.29      cgd      2295:        }
1.96      fair     2296:
1.243     ad       2297:        /* No longer dumping core. */
                   2298:        p->p_sflag &= ~PS_WCORE;
                   2299:
1.323     christos 2300:        exit1(l, 0, exitsig);
1.29      cgd      2301:        /* NOTREACHED */
                   2302: }
                   2303:
                   2304: /*
1.243     ad       2305:  * Put process 'p' into the stopped state and optionally, notify the parent.
1.29      cgd      2306:  */
1.243     ad       2307: void
1.334     christos 2308: proc_stop(struct proc *p, int signo)
1.29      cgd      2309: {
1.243     ad       2310:        struct lwp *l;
1.112     lukem    2311:
1.277     ad       2312:        KASSERT(mutex_owned(p->p_lock));
1.29      cgd      2313:
1.59      cgd      2314:        /*
1.243     ad       2315:         * First off, set the stopping indicator and bring all sleeping
                   2316:         * LWPs to a halt so they are included in p->p_nrlwps.  We musn't
                   2317:         * unlock between here and the p->p_nrlwps check below.
1.59      cgd      2318:         */
1.362     kamil    2319:        p->p_sflag |= PS_STOPPING;
1.260     ad       2320:        membar_producer();
1.59      cgd      2321:
1.272     yamt     2322:        proc_stop_lwps(p);
1.59      cgd      2323:
                   2324:        /*
1.243     ad       2325:         * If there are no LWPs available to take the signal, then we
                   2326:         * signal the parent process immediately.  Otherwise, the last
                   2327:         * LWP to stop will take care of it.
1.59      cgd      2328:         */
                   2329:
1.334     christos 2330:        if (p->p_nrlwps == 0) {
1.340     kamil    2331:                proc_stop_done(p, PS_NOCLDSTOP);
1.243     ad       2332:        } else {
                   2333:                /*
                   2334:                 * Have the remaining LWPs come to a halt, and trigger
                   2335:                 * proc_stop_callout() to ensure that they do.
                   2336:                 */
1.317     rmind    2337:                LIST_FOREACH(l, &p->p_lwps, l_sibling) {
                   2338:                        sigpost(l, SIG_DFL, SA_STOP, signo);
                   2339:                }
1.243     ad       2340:                callout_schedule(&proc_stop_ch, 1);
1.169     hannken  2341:        }
1.29      cgd      2342: }
                   2343:
                   2344: /*
1.243     ad       2345:  * When stopping a process, we do not immediatly set sleeping LWPs stopped,
                   2346:  * but wait for them to come to a halt at the kernel-user boundary.  This is
                   2347:  * to allow LWPs to release any locks that they may hold before stopping.
                   2348:  *
                   2349:  * Non-interruptable sleeps can be long, and there is the potential for an
                   2350:  * LWP to begin sleeping interruptably soon after the process has been set
                   2351:  * stopping (PS_STOPPING).  These LWPs will not notice that the process is
                   2352:  * stopping, and so complete halt of the process and the return of status
                   2353:  * information to the parent could be delayed indefinitely.
                   2354:  *
                   2355:  * To handle this race, proc_stop_callout() runs once per tick while there
1.256     ad       2356:  * are stopping processes in the system.  It sets LWPs that are sleeping
1.243     ad       2357:  * interruptably into the LSSTOP state.
                   2358:  *
                   2359:  * Note that we are not concerned about keeping all LWPs stopped while the
                   2360:  * process is stopped: stopped LWPs can awaken briefly to handle signals.
                   2361:  * What we do need to ensure is that all LWPs in a stopping process have
                   2362:  * stopped at least once, so that notification can be sent to the parent
                   2363:  * process.
1.29      cgd      2364:  */
1.243     ad       2365: static void
                   2366: proc_stop_callout(void *cookie)
1.29      cgd      2367: {
1.248     thorpej  2368:        bool more, restart;
1.243     ad       2369:        struct proc *p;
1.29      cgd      2370:
1.243     ad       2371:        (void)cookie;
1.94      bouyer   2372:
1.243     ad       2373:        do {
1.249     thorpej  2374:                restart = false;
                   2375:                more = false;
1.130     thorpej  2376:
1.276     ad       2377:                mutex_enter(proc_lock);
1.243     ad       2378:                PROCLIST_FOREACH(p, &allproc) {
1.277     ad       2379:                        mutex_enter(p->p_lock);
1.130     thorpej  2380:
1.243     ad       2381:                        if ((p->p_sflag & PS_STOPPING) == 0) {
1.277     ad       2382:                                mutex_exit(p->p_lock);
1.243     ad       2383:                                continue;
                   2384:                        }
1.130     thorpej  2385:
1.243     ad       2386:                        /* Stop any LWPs sleeping interruptably. */
1.272     yamt     2387:                        proc_stop_lwps(p);
1.243     ad       2388:                        if (p->p_nrlwps == 0) {
                   2389:                                /*
                   2390:                                 * We brought the process to a halt.
                   2391:                                 * Mark it as stopped and notify the
                   2392:                                 * parent.
1.362     kamil    2393:                                 *
                   2394:                                 * Note that proc_stop_done() will
                   2395:                                 * drop p->p_lock briefly.
                   2396:                                 * Arrange to restart and check
                   2397:                                 * all processes again.
1.243     ad       2398:                                 */
1.362     kamil    2399:                                restart = true;
1.340     kamil    2400:                                proc_stop_done(p, PS_NOCLDSTOP);
1.243     ad       2401:                        } else
1.249     thorpej  2402:                                more = true;
1.130     thorpej  2403:
1.277     ad       2404:                        mutex_exit(p->p_lock);
1.243     ad       2405:                        if (restart)
                   2406:                                break;
                   2407:                }
1.276     ad       2408:                mutex_exit(proc_lock);
1.243     ad       2409:        } while (restart);
1.185     matt     2410:
1.130     thorpej  2411:        /*
1.243     ad       2412:         * If we noted processes that are stopping but still have
                   2413:         * running LWPs, then arrange to check again in 1 tick.
1.130     thorpej  2414:         */
1.243     ad       2415:        if (more)
                   2416:                callout_schedule(&proc_stop_ch, 1);
1.108     jdolecek 2417: }
1.130     thorpej  2418:
1.135     jdolecek 2419: /*
1.243     ad       2420:  * Given a process in state SSTOP, set the state back to SACTIVE and
                   2421:  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1.135     jdolecek 2422:  */
1.243     ad       2423: void
                   2424: proc_unstop(struct proc *p)
1.135     jdolecek 2425: {
1.243     ad       2426:        struct lwp *l;
                   2427:        int sig;
1.208     cube     2428:
1.276     ad       2429:        KASSERT(mutex_owned(proc_lock));
1.277     ad       2430:        KASSERT(mutex_owned(p->p_lock));
1.135     jdolecek 2431:
1.243     ad       2432:        p->p_stat = SACTIVE;
                   2433:        p->p_sflag &= ~PS_STOPPING;
1.322     christos 2434:        sig = p->p_xsig;
1.219     mrg      2435:
1.243     ad       2436:        if (!p->p_waited)
                   2437:                p->p_pptr->p_nstopchild--;
1.173     jdolecek 2438:
1.243     ad       2439:        LIST_FOREACH(l, &p->p_lwps, l_sibling) {
                   2440:                lwp_lock(l);
1.366     kamil    2441:                if (l->l_stat != LSSTOP || (l->l_flag & LW_DBGSUSPEND) != 0) {
1.243     ad       2442:                        lwp_unlock(l);
                   2443:                        continue;
1.171     jdolecek 2444:                }
1.243     ad       2445:                if (l->l_wchan == NULL) {
                   2446:                        setrunnable(l);
                   2447:                        continue;
1.241     enami    2448:                }
1.246     pavel    2449:                if (sig && (l->l_flag & LW_SINTR) != 0) {
1.301     rmind    2450:                        setrunnable(l);
                   2451:                        sig = 0;
1.250     ad       2452:                } else {
                   2453:                        l->l_stat = LSSLEEP;
                   2454:                        p->p_nrlwps++;
1.243     ad       2455:                        lwp_unlock(l);
1.250     ad       2456:                }
1.135     jdolecek 2457:        }
1.29      cgd      2458: }
1.126     jdolecek 2459:
1.334     christos 2460: void
1.358     kamil    2461: proc_stoptrace(int trapno, int sysnum, const register_t args[],
                   2462:                const register_t *ret, int error)
1.334     christos 2463: {
                   2464:        struct lwp *l = curlwp;
1.341     kamil    2465:        struct proc *p = l->l_proc;
                   2466:        struct sigacts *ps;
                   2467:        sigset_t *mask;
                   2468:        sig_t action;
                   2469:        ksiginfo_t ksi;
1.358     kamil    2470:        size_t i, sy_narg;
1.341     kamil    2471:        const int signo = SIGTRAP;
                   2472:
                   2473:        KASSERT((trapno == TRAP_SCE) || (trapno == TRAP_SCX));
1.358     kamil    2474:        KASSERT(p->p_pptr != initproc);
                   2475:        KASSERT(ISSET(p->p_slflag, PSL_TRACED));
                   2476:        KASSERT(ISSET(p->p_slflag, PSL_SYSCALL));
                   2477:
                   2478:        sy_narg = p->p_emul->e_sysent[sysnum].sy_narg;
1.341     kamil    2479:
                   2480:        KSI_INIT_TRAP(&ksi);
                   2481:        ksi.ksi_lid = l->l_lid;
1.358     kamil    2482:        ksi.ksi_signo = signo;
                   2483:        ksi.ksi_code = trapno;
                   2484:
                   2485:        ksi.ksi_sysnum = sysnum;
                   2486:        if (trapno == TRAP_SCE) {
                   2487:                ksi.ksi_retval[0] = 0;
                   2488:                ksi.ksi_retval[1] = 0;
                   2489:                ksi.ksi_error = 0;
                   2490:        } else {
                   2491:                ksi.ksi_retval[0] = ret[0];
                   2492:                ksi.ksi_retval[1] = ret[1];
                   2493:                ksi.ksi_error = error;
                   2494:        }
                   2495:
                   2496:        memset(ksi.ksi_args, 0, sizeof(ksi.ksi_args));
                   2497:
                   2498:        for (i = 0; i < sy_narg; i++)
                   2499:                ksi.ksi_args[i] = args[i];
1.334     christos 2500:
                   2501:        mutex_enter(p->p_lock);
                   2502:
1.367     kamil    2503: repeat:
1.357     kamil    2504:        /*
1.359     kamil    2505:         * If we are exiting, demise now.
                   2506:         *
                   2507:         * This avoids notifying tracer and deadlocking.
                   2508:         */
                   2509:        if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
                   2510:                mutex_exit(p->p_lock);
                   2511:                lwp_exit(l);
                   2512:                panic("proc_stoptrace");
                   2513:                /* NOTREACHED */
                   2514:        }
                   2515:
                   2516:        /*
1.357     kamil    2517:         * If there's a pending SIGKILL process it immediately.
                   2518:         */
                   2519:        if (p->p_xsig == SIGKILL ||
                   2520:            sigismember(&p->p_sigpend.sp_set, SIGKILL)) {
                   2521:                mutex_exit(p->p_lock);
                   2522:                return;
                   2523:        }
                   2524:
1.367     kamil    2525:        /*
                   2526:         * If we are no longer traced, abandon this event signal.
                   2527:         *
                   2528:         * This avoids killing a process after detaching the debugger.
                   2529:         */
                   2530:        if (__predict_false(!ISSET(p->p_slflag, PSL_TRACED))) {
                   2531:                mutex_exit(p->p_lock);
                   2532:                return;
                   2533:        }
                   2534:
                   2535:        /*
                   2536:         * The process is already stopping.
                   2537:         */
                   2538:        if ((p->p_sflag & PS_STOPPING) != 0) {
1.370     kamil    2539:                sigswitch_unlock_and_switch_away(l);
1.367     kamil    2540:                mutex_enter(p->p_lock);
1.370     kamil    2541:                goto repeat;
1.367     kamil    2542:        }
                   2543:
1.341     kamil    2544:        /* Needed for ktrace */
                   2545:        ps = p->p_sigacts;
                   2546:        action = SIGACTION_PS(ps, signo).sa_handler;
                   2547:        mask = &l->l_sigmask;
                   2548:
                   2549:        p->p_xsig = signo;
                   2550:        p->p_sigctx.ps_lwp = ksi.ksi_lid;
                   2551:        p->p_sigctx.ps_info = ksi.ksi_info;
1.369     kamil    2552:        sigswitch(0, signo, false);
1.341     kamil    2553:
                   2554:        if (ktrpoint(KTR_PSIG)) {
                   2555:                if (p->p_emul->e_ktrpsig)
                   2556:                        p->p_emul->e_ktrpsig(signo, action, mask, &ksi);
                   2557:                else
                   2558:                        ktrpsig(signo, action, mask, &ksi);
                   2559:        }
1.334     christos 2560: }
                   2561:
1.126     jdolecek 2562: static int
                   2563: filt_sigattach(struct knote *kn)
                   2564: {
                   2565:        struct proc *p = curproc;
                   2566:
1.274     ad       2567:        kn->kn_obj = p;
1.301     rmind    2568:        kn->kn_flags |= EV_CLEAR;       /* automatically set */
1.126     jdolecek 2569:
1.277     ad       2570:        mutex_enter(p->p_lock);
1.126     jdolecek 2571:        SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
1.277     ad       2572:        mutex_exit(p->p_lock);
1.126     jdolecek 2573:
1.301     rmind    2574:        return 0;
1.126     jdolecek 2575: }
                   2576:
                   2577: static void
                   2578: filt_sigdetach(struct knote *kn)
                   2579: {
1.274     ad       2580:        struct proc *p = kn->kn_obj;
1.126     jdolecek 2581:
1.277     ad       2582:        mutex_enter(p->p_lock);
1.126     jdolecek 2583:        SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
1.277     ad       2584:        mutex_exit(p->p_lock);
1.126     jdolecek 2585: }
                   2586:
                   2587: /*
1.301     rmind    2588:  * Signal knotes are shared with proc knotes, so we apply a mask to
1.126     jdolecek 2589:  * the hint in order to differentiate them from process hints.  This
                   2590:  * could be avoided by using a signal-specific knote list, but probably
                   2591:  * isn't worth the trouble.
                   2592:  */
                   2593: static int
                   2594: filt_signal(struct knote *kn, long hint)
                   2595: {
                   2596:
                   2597:        if (hint & NOTE_SIGNAL) {
                   2598:                hint &= ~NOTE_SIGNAL;
                   2599:
                   2600:                if (kn->kn_id == hint)
                   2601:                        kn->kn_data++;
                   2602:        }
                   2603:        return (kn->kn_data != 0);
                   2604: }
                   2605:
                   2606: const struct filterops sig_filtops = {
1.338     maya     2607:                .f_isfd = 0,
                   2608:                .f_attach = filt_sigattach,
                   2609:                .f_detach = filt_sigdetach,
                   2610:                .f_event = filt_signal,
1.126     jdolecek 2611: };

CVSweb <webmaster@jp.NetBSD.org>