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

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

CVSweb <webmaster@jp.NetBSD.org>