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

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

CVSweb <webmaster@jp.NetBSD.org>