[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.349.2.1

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

CVSweb <webmaster@jp.NetBSD.org>