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

1.204   ! yamt        1: /*     $NetBSD$        */
1.29      cgd         2:
                      3: /*
                      4:  * Copyright (c) 1982, 1986, 1989, 1991, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  * (c) UNIX System Laboratories, Inc.
                      7:  * All or some portions of this file are derived from material licensed
                      8:  * to the University of California by American Telephone and Telegraph
                      9:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     10:  * the permission of UNIX System Laboratories, Inc.
                     11:  *
                     12:  * Redistribution and use in source and binary forms, with or without
                     13:  * modification, are permitted provided that the following conditions
                     14:  * are met:
                     15:  * 1. Redistributions of source code must retain the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer.
                     17:  * 2. Redistributions in binary form must reproduce the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer in the
                     19:  *    documentation and/or other materials provided with the distribution.
1.146     agc        20:  * 3. Neither the name of the University nor the names of its contributors
1.29      cgd        21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  *
1.71      fvdl       36:  *     @(#)kern_sig.c  8.14 (Berkeley) 5/14/95
1.29      cgd        37:  */
1.116     lukem      38:
                     39: #include <sys/cdefs.h>
1.204   ! yamt       40: __KERNEL_RCSID(0, "$NetBSD$");
1.70      mrg        41:
1.73      thorpej    42: #include "opt_ktrace.h"
1.74      thorpej    43: #include "opt_compat_sunos.h"
1.158     christos   44: #include "opt_compat_netbsd.h"
1.202     perry      45: #include "opt_compat_netbsd32.h"
1.29      cgd        46:
                     47: #define        SIGPROP         /* include signal properties table */
                     48: #include <sys/param.h>
                     49: #include <sys/signalvar.h>
                     50: #include <sys/resourcevar.h>
                     51: #include <sys/namei.h>
                     52: #include <sys/vnode.h>
                     53: #include <sys/proc.h>
                     54: #include <sys/systm.h>
                     55: #include <sys/timeb.h>
                     56: #include <sys/times.h>
                     57: #include <sys/buf.h>
                     58: #include <sys/acct.h>
                     59: #include <sys/file.h>
                     60: #include <sys/kernel.h>
                     61: #include <sys/wait.h>
                     62: #include <sys/ktrace.h>
                     63: #include <sys/syslog.h>
                     64: #include <sys/stat.h>
                     65: #include <sys/core.h>
1.59      cgd        66: #include <sys/filedesc.h>
1.89      thorpej    67: #include <sys/malloc.h>
                     68: #include <sys/pool.h>
1.130     thorpej    69: #include <sys/ucontext.h>
                     70: #include <sys/sa.h>
                     71: #include <sys/savar.h>
1.118     thorpej    72: #include <sys/exec.h>
1.29      cgd        73:
1.32      cgd        74: #include <sys/mount.h>
                     75: #include <sys/syscallargs.h>
                     76:
1.29      cgd        77: #include <machine/cpu.h>
                     78:
                     79: #include <sys/user.h>          /* for coredump */
1.52      christos   80:
1.196     skrll      81: #include <uvm/uvm.h>
1.69      mrg        82: #include <uvm/uvm_extern.h>
                     83:
1.151     christos   84: static void    child_psignal(struct proc *, int);
1.201     christos   85: static int     build_corename(struct proc *, char *, const char *, size_t);
1.152     christos   86: static void    ksiginfo_exithook(struct proc *, void *);
1.160     christos   87: static void    ksiginfo_put(struct proc *, const ksiginfo_t *);
1.152     christos   88: static ksiginfo_t *ksiginfo_get(struct proc *, int);
1.160     christos   89: static void    kpsignal2(struct proc *, const ksiginfo_t *, int);
1.152     christos   90:
1.198     jdolecek   91: sigset_t       contsigmask, stopsigmask, sigcantmask;
1.29      cgd        92:
1.196     skrll      93: struct pool    sigacts_pool;   /* memory pool for sigacts structures */
                     94:
                     95: /*
                     96:  * struct sigacts memory pool allocator.
                     97:  */
                     98:
                     99: static void *
                    100: sigacts_poolpage_alloc(struct pool *pp, int flags)
                    101: {
                    102:
1.204   ! yamt      103:        return (void *)uvm_km_alloc(kernel_map,
        !           104:            (PAGE_SIZE)*2, (PAGE_SIZE)*2,
        !           105:            ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
        !           106:            | UVM_KMF_WIRED);
1.196     skrll     107: }
                    108:
                    109: static void
                    110: sigacts_poolpage_free(struct pool *pp, void *v)
                    111: {
1.204   ! yamt      112:         uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2, UVM_KMF_WIRED);
1.196     skrll     113: }
                    114:
                    115: static struct pool_allocator sigactspool_allocator = {
1.197     he        116:         sigacts_poolpage_alloc, sigacts_poolpage_free,
1.196     skrll     117: };
                    118:
1.194     simonb    119: POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
                    120:     &pool_allocator_nointr);
                    121: POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL);
1.89      thorpej   122:
1.29      cgd       123: /*
                    124:  * Can process p, with pcred pc, send the signal signum to process q?
                    125:  */
1.112     lukem     126: #define        CANSIGNAL(p, pc, q, signum) \
1.29      cgd       127:        ((pc)->pc_ucred->cr_uid == 0 || \
                    128:            (pc)->p_ruid == (q)->p_cred->p_ruid || \
                    129:            (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
                    130:            (pc)->p_ruid == (q)->p_ucred->cr_uid || \
                    131:            (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
                    132:            ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
                    133:
1.89      thorpej   134: /*
1.155     christos  135:  * Remove and return the first ksiginfo element that matches our requested
                    136:  * signal, or return NULL if one not found.
1.152     christos  137:  */
                    138: static ksiginfo_t *
                    139: ksiginfo_get(struct proc *p, int signo)
                    140: {
1.155     christos  141:        ksiginfo_t *ksi;
1.168     pk        142:        int s;
1.152     christos  143:
1.168     pk        144:        s = splsoftclock();
1.155     christos  145:        simple_lock(&p->p_sigctx.ps_silock);
                    146:        CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
                    147:                if (ksi->ksi_signo == signo) {
                    148:                        CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
1.168     pk        149:                        goto out;
1.155     christos  150:                }
1.152     christos  151:        }
1.168     pk        152:        ksi = NULL;
                    153: out:
1.155     christos  154:        simple_unlock(&p->p_sigctx.ps_silock);
1.168     pk        155:        splx(s);
                    156:        return ksi;
1.152     christos  157: }
                    158:
1.155     christos  159: /*
                    160:  * Append a new ksiginfo element to the list of pending ksiginfo's, if
                    161:  * we need to (SA_SIGINFO was requested). We replace non RT signals if
                    162:  * they already existed in the queue and we add new entries for RT signals,
                    163:  * or for non RT signals with non-existing entries.
                    164:  */
1.152     christos  165: static void
1.160     christos  166: ksiginfo_put(struct proc *p, const ksiginfo_t *ksi)
1.152     christos  167: {
1.155     christos  168:        ksiginfo_t *kp;
                    169:        struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
1.167     pk        170:        int s;
1.152     christos  171:
1.155     christos  172:        if ((sa->sa_flags & SA_SIGINFO) == 0)
                    173:                return;
1.192     matt      174:        /*
                    175:         * If there's no info, don't save it.
                    176:         */
                    177:        if (KSI_EMPTY_P(ksi))
                    178:                return;
1.152     christos  179:
1.167     pk        180:        s = splsoftclock();
1.155     christos  181:        simple_lock(&p->p_sigctx.ps_silock);
1.152     christos  182: #ifdef notyet  /* XXX: QUEUING */
1.155     christos  183:        if (ksi->ksi_signo < SIGRTMIN)
1.152     christos  184: #endif
1.155     christos  185:        {
                    186:                CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
                    187:                        if (kp->ksi_signo == ksi->ksi_signo) {
1.174     jdolecek  188:                                KSI_COPY(ksi, kp);
1.167     pk        189:                                goto out;
1.155     christos  190:                        }
                    191:                }
1.153     christos  192:        }
1.155     christos  193:        kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
1.157     christos  194:        if (kp == NULL) {
                    195: #ifdef DIAGNOSTIC
                    196:                printf("Out of memory allocating siginfo for pid %d\n",
                    197:                    p->p_pid);
                    198: #endif
1.167     pk        199:                goto out;
1.157     christos  200:        }
1.155     christos  201:        *kp = *ksi;
                    202:        CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
1.167     pk        203: out:
1.155     christos  204:        simple_unlock(&p->p_sigctx.ps_silock);
1.167     pk        205:        splx(s);
1.152     christos  206: }
                    207:
                    208: /*
                    209:  * free all pending ksiginfo on exit
                    210:  */
                    211: static void
                    212: ksiginfo_exithook(struct proc *p, void *v)
                    213: {
1.168     pk        214:        int s;
1.152     christos  215:
1.168     pk        216:        s = splsoftclock();
1.155     christos  217:        simple_lock(&p->p_sigctx.ps_silock);
                    218:        while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
                    219:                ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
                    220:                CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
1.152     christos  221:                pool_put(&ksiginfo_pool, ksi);
                    222:        }
1.155     christos  223:        simple_unlock(&p->p_sigctx.ps_silock);
1.168     pk        224:        splx(s);
1.152     christos  225: }
                    226:
                    227: /*
1.89      thorpej   228:  * Initialize signal-related data structures.
                    229:  */
                    230: void
1.112     lukem     231: signal_init(void)
1.89      thorpej   232: {
1.194     simonb    233:
1.197     he        234:        sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
                    235:
1.196     skrll     236:        pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
                    237:            sizeof(struct sigacts) > PAGE_SIZE ?
                    238:            &sigactspool_allocator : &pool_allocator_nointr);
                    239:
1.152     christos  240:        exithook_establish(ksiginfo_exithook, NULL);
                    241:        exechook_establish(ksiginfo_exithook, NULL);
1.89      thorpej   242: }
                    243:
                    244: /*
1.109     jdolecek  245:  * Create an initial sigctx structure, using the same signal state
                    246:  * as p. If 'share' is set, share the sigctx_proc part, otherwise just
                    247:  * copy it from parent.
1.89      thorpej   248:  */
1.109     jdolecek  249: void
1.112     lukem     250: sigactsinit(struct proc *np, struct proc *pp, int share)
1.89      thorpej   251: {
                    252:        struct sigacts *ps;
                    253:
1.109     jdolecek  254:        if (share) {
                    255:                np->p_sigacts = pp->p_sigacts;
                    256:                pp->p_sigacts->sa_refcnt++;
                    257:        } else {
                    258:                ps = pool_get(&sigacts_pool, PR_WAITOK);
                    259:                if (pp)
                    260:                        memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
                    261:                else
                    262:                        memset(ps, '\0', sizeof(struct sigacts));
                    263:                ps->sa_refcnt = 1;
                    264:                np->p_sigacts = ps;
                    265:        }
1.89      thorpej   266: }
                    267:
                    268: /*
1.109     jdolecek  269:  * Make this process not share its sigctx, maintaining all
1.89      thorpej   270:  * signal state.
                    271:  */
                    272: void
1.112     lukem     273: sigactsunshare(struct proc *p)
1.89      thorpej   274: {
1.109     jdolecek  275:        struct sigacts *oldps;
1.89      thorpej   276:
1.109     jdolecek  277:        if (p->p_sigacts->sa_refcnt == 1)
1.89      thorpej   278:                return;
                    279:
1.109     jdolecek  280:        oldps = p->p_sigacts;
                    281:        sigactsinit(p, NULL, 0);
                    282:
                    283:        if (--oldps->sa_refcnt == 0)
                    284:                pool_put(&sigacts_pool, oldps);
1.89      thorpej   285: }
                    286:
                    287: /*
1.109     jdolecek  288:  * Release a sigctx structure.
1.89      thorpej   289:  */
                    290: void
1.195     pk        291: sigactsfree(struct sigacts *ps)
1.89      thorpej   292: {
                    293:
1.109     jdolecek  294:        if (--ps->sa_refcnt > 0)
1.89      thorpej   295:                return;
                    296:
                    297:        pool_put(&sigacts_pool, ps);
                    298: }
                    299:
1.79      mycroft   300: int
1.112     lukem     301: sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
1.162     matt      302:        struct sigaction *osa, const void *tramp, int vers)
1.79      mycroft   303: {
1.112     lukem     304:        struct sigacts  *ps;
                    305:        int             prop;
1.79      mycroft   306:
1.112     lukem     307:        ps = p->p_sigacts;
1.79      mycroft   308:        if (signum <= 0 || signum >= NSIG)
                    309:                return (EINVAL);
                    310:
1.121     thorpej   311:        /*
                    312:         * Trampoline ABI version 0 is reserved for the legacy
1.162     matt      313:         * kernel-provided on-stack trampoline.  Conversely, if we are
                    314:         * using a non-0 ABI version, we must have a trampoline.  Only
1.163     christos  315:         * validate the vers if a new sigaction was supplied. Emulations
                    316:         * use legacy kernel trampolines with version 0, alternatively
                    317:         * check for that too.
1.121     thorpej   318:         */
                    319:        if ((vers != 0 && tramp == NULL) ||
1.161     matt      320: #ifdef SIGTRAMP_VALID
1.202     perry     321:            (nsa != NULL &&
                    322:            ((vers == 0) ?
1.163     christos  323:                (p->p_emul->e_sigcode == NULL) :
                    324:                !SIGTRAMP_VALID(vers))) ||
1.161     matt      325: #endif
1.121     thorpej   326:            (vers == 0 && tramp != NULL))
                    327:                return (EINVAL);
                    328:
1.79      mycroft   329:        if (osa)
1.109     jdolecek  330:                *osa = SIGACTION_PS(ps, signum);
1.79      mycroft   331:
                    332:        if (nsa) {
                    333:                if (nsa->sa_flags & ~SA_ALLBITS)
                    334:                        return (EINVAL);
1.149     kleink    335:
1.79      mycroft   336:                prop = sigprop[signum];
                    337:                if (prop & SA_CANTMASK)
                    338:                        return (EINVAL);
                    339:
1.105     thorpej   340:                (void) splsched();      /* XXXSMP */
1.109     jdolecek  341:                SIGACTION_PS(ps, signum) = *nsa;
1.121     thorpej   342:                ps->sa_sigdesc[signum].sd_tramp = tramp;
                    343:                ps->sa_sigdesc[signum].sd_vers = vers;
1.109     jdolecek  344:                sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
1.79      mycroft   345:                if ((prop & SA_NORESET) != 0)
1.109     jdolecek  346:                        SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
1.79      mycroft   347:                if (signum == SIGCHLD) {
                    348:                        if (nsa->sa_flags & SA_NOCLDSTOP)
                    349:                                p->p_flag |= P_NOCLDSTOP;
                    350:                        else
                    351:                                p->p_flag &= ~P_NOCLDSTOP;
1.82      enami     352:                        if (nsa->sa_flags & SA_NOCLDWAIT) {
1.81      christos  353:                                /*
                    354:                                 * Paranoia: since SA_NOCLDWAIT is implemented
                    355:                                 * by reparenting the dying child to PID 1 (and
1.112     lukem     356:                                 * trust it to reap the zombie), PID 1 itself
                    357:                                 * is forbidden to set SA_NOCLDWAIT.
1.81      christos  358:                                 */
                    359:                                if (p->p_pid == 1)
                    360:                                        p->p_flag &= ~P_NOCLDWAIT;
                    361:                                else
                    362:                                        p->p_flag |= P_NOCLDWAIT;
                    363:                        } else
                    364:                                p->p_flag &= ~P_NOCLDWAIT;
1.203     christos  365:
                    366:                        if (nsa->sa_handler == SIG_IGN) {
                    367:                                /*
                    368:                                 * Paranoia: same as above.
                    369:                                 */
                    370:                                if (p->p_pid == 1)
                    371:                                        p->p_flag &= ~P_CLDSIGIGN;
                    372:                                else
                    373:                                        p->p_flag |= P_CLDSIGIGN;
                    374:                        } else
                    375:                                p->p_flag &= ~P_CLDSIGIGN;
                    376:
1.79      mycroft   377:                }
                    378:                if ((nsa->sa_flags & SA_NODEFER) == 0)
1.109     jdolecek  379:                        sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
1.79      mycroft   380:                else
1.109     jdolecek  381:                        sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
1.79      mycroft   382:                /*
1.112     lukem     383:                 * Set bit in p_sigctx.ps_sigignore for signals that are set to
                    384:                 * SIG_IGN, and for signals set to SIG_DFL where the default is
                    385:                 * to ignore. However, don't put SIGCONT in
                    386:                 * p_sigctx.ps_sigignore, as we have to restart the process.
                    387:                 */
1.79      mycroft   388:                if (nsa->sa_handler == SIG_IGN ||
                    389:                    (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
1.112     lukem     390:                                                /* never to be seen again */
                    391:                        sigdelset(&p->p_sigctx.ps_siglist, signum);
                    392:                        if (signum != SIGCONT) {
                    393:                                                /* easier in psignal */
                    394:                                sigaddset(&p->p_sigctx.ps_sigignore, signum);
                    395:                        }
1.109     jdolecek  396:                        sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.79      mycroft   397:                } else {
1.109     jdolecek  398:                        sigdelset(&p->p_sigctx.ps_sigignore, signum);
1.79      mycroft   399:                        if (nsa->sa_handler == SIG_DFL)
1.109     jdolecek  400:                                sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.79      mycroft   401:                        else
1.109     jdolecek  402:                                sigaddset(&p->p_sigctx.ps_sigcatch, signum);
1.79      mycroft   403:                }
                    404:                (void) spl0();
                    405:        }
                    406:
                    407:        return (0);
                    408: }
                    409:
1.158     christos  410: #ifdef COMPAT_16
1.29      cgd       411: /* ARGSUSED */
1.52      christos  412: int
1.158     christos  413: compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
1.48      thorpej   414: {
1.159     nathanw   415:        struct compat_16_sys___sigaction14_args /* {
1.112     lukem     416:                syscallarg(int)                         signum;
                    417:                syscallarg(const struct sigaction *)    nsa;
                    418:                syscallarg(struct sigaction *)          osa;
1.48      thorpej   419:        } */ *uap = v;
1.130     thorpej   420:        struct proc             *p;
1.112     lukem     421:        struct sigaction        nsa, osa;
                    422:        int                     error;
1.29      cgd       423:
1.79      mycroft   424:        if (SCARG(uap, nsa)) {
                    425:                error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
1.52      christos  426:                if (error)
1.29      cgd       427:                        return (error);
                    428:        }
1.130     thorpej   429:        p = l->l_proc;
1.79      mycroft   430:        error = sigaction1(p, SCARG(uap, signum),
1.121     thorpej   431:            SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
                    432:            NULL, 0);
                    433:        if (error)
                    434:                return (error);
                    435:        if (SCARG(uap, osa)) {
                    436:                error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
                    437:                if (error)
                    438:                        return (error);
                    439:        }
                    440:        return (0);
                    441: }
1.158     christos  442: #endif
1.121     thorpej   443:
                    444: /* ARGSUSED */
                    445: int
1.130     thorpej   446: sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
1.121     thorpej   447: {
                    448:        struct sys___sigaction_sigtramp_args /* {
                    449:                syscallarg(int)                         signum;
                    450:                syscallarg(const struct sigaction *)    nsa;
                    451:                syscallarg(struct sigaction *)          osa;
                    452:                syscallarg(void *)                      tramp;
                    453:                syscallarg(int)                         vers;
                    454:        } */ *uap = v;
1.130     thorpej   455:        struct proc *p = l->l_proc;
1.121     thorpej   456:        struct sigaction nsa, osa;
                    457:        int error;
                    458:
                    459:        if (SCARG(uap, nsa)) {
                    460:                error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
                    461:                if (error)
                    462:                        return (error);
                    463:        }
                    464:        error = sigaction1(p, SCARG(uap, signum),
                    465:            SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
                    466:            SCARG(uap, tramp), SCARG(uap, vers));
1.79      mycroft   467:        if (error)
                    468:                return (error);
                    469:        if (SCARG(uap, osa)) {
                    470:                error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
1.52      christos  471:                if (error)
1.29      cgd       472:                        return (error);
                    473:        }
                    474:        return (0);
                    475: }
                    476:
                    477: /*
                    478:  * Initialize signal state for process 0;
1.79      mycroft   479:  * set to ignore signals that are ignored by default and disable the signal
                    480:  * stack.
1.29      cgd       481:  */
                    482: void
1.112     lukem     483: siginit(struct proc *p)
1.29      cgd       484: {
1.112     lukem     485:        struct sigacts  *ps;
                    486:        int             signum, prop;
1.79      mycroft   487:
1.112     lukem     488:        ps = p->p_sigacts;
1.79      mycroft   489:        sigemptyset(&contsigmask);
                    490:        sigemptyset(&stopsigmask);
                    491:        sigemptyset(&sigcantmask);
1.85      mycroft   492:        for (signum = 1; signum < NSIG; signum++) {
1.79      mycroft   493:                prop = sigprop[signum];
                    494:                if (prop & SA_CONT)
                    495:                        sigaddset(&contsigmask, signum);
                    496:                if (prop & SA_STOP)
                    497:                        sigaddset(&stopsigmask, signum);
                    498:                if (prop & SA_CANTMASK)
                    499:                        sigaddset(&sigcantmask, signum);
                    500:                if (prop & SA_IGNORE && signum != SIGCONT)
1.109     jdolecek  501:                        sigaddset(&p->p_sigctx.ps_sigignore, signum);
                    502:                sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
                    503:                SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
1.79      mycroft   504:        }
1.109     jdolecek  505:        sigemptyset(&p->p_sigctx.ps_sigcatch);
1.171     jdolecek  506:        p->p_sigctx.ps_sigwaited = NULL;
1.79      mycroft   507:        p->p_flag &= ~P_NOCLDSTOP;
1.29      cgd       508:
1.79      mycroft   509:        /*
                    510:         * Reset stack state to the user stack.
                    511:         */
1.109     jdolecek  512:        p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
                    513:        p->p_sigctx.ps_sigstk.ss_size = 0;
                    514:        p->p_sigctx.ps_sigstk.ss_sp = 0;
1.89      thorpej   515:
                    516:        /* One reference. */
1.109     jdolecek  517:        ps->sa_refcnt = 1;
1.29      cgd       518: }
                    519:
                    520: /*
                    521:  * Reset signals for an exec of the specified process.
                    522:  */
                    523: void
1.112     lukem     524: execsigs(struct proc *p)
1.29      cgd       525: {
1.112     lukem     526:        struct sigacts  *ps;
                    527:        int             signum, prop;
1.29      cgd       528:
1.115     thorpej   529:        sigactsunshare(p);
                    530:
1.112     lukem     531:        ps = p->p_sigacts;
1.115     thorpej   532:
1.29      cgd       533:        /*
                    534:         * Reset caught signals.  Held signals remain held
1.109     jdolecek  535:         * through p_sigctx.ps_sigmask (unless they were caught,
1.29      cgd       536:         * and are now ignored by default).
                    537:         */
1.85      mycroft   538:        for (signum = 1; signum < NSIG; signum++) {
1.109     jdolecek  539:                if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
1.79      mycroft   540:                        prop = sigprop[signum];
                    541:                        if (prop & SA_IGNORE) {
                    542:                                if ((prop & SA_CONT) == 0)
1.112     lukem     543:                                        sigaddset(&p->p_sigctx.ps_sigignore,
                    544:                                            signum);
1.109     jdolecek  545:                                sigdelset(&p->p_sigctx.ps_siglist, signum);
1.79      mycroft   546:                        }
1.109     jdolecek  547:                        SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1.29      cgd       548:                }
1.109     jdolecek  549:                sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
                    550:                SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
1.29      cgd       551:        }
1.109     jdolecek  552:        sigemptyset(&p->p_sigctx.ps_sigcatch);
1.171     jdolecek  553:        p->p_sigctx.ps_sigwaited = NULL;
1.79      mycroft   554:        p->p_flag &= ~P_NOCLDSTOP;
                    555:
1.29      cgd       556:        /*
                    557:         * Reset stack state to the user stack.
                    558:         */
1.109     jdolecek  559:        p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
                    560:        p->p_sigctx.ps_sigstk.ss_size = 0;
                    561:        p->p_sigctx.ps_sigstk.ss_sp = 0;
1.29      cgd       562: }
                    563:
1.79      mycroft   564: int
1.112     lukem     565: sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
1.79      mycroft   566: {
                    567:
                    568:        if (oss)
1.109     jdolecek  569:                *oss = p->p_sigctx.ps_sigmask;
1.79      mycroft   570:
                    571:        if (nss) {
1.105     thorpej   572:                (void)splsched();       /* XXXSMP */
1.79      mycroft   573:                switch (how) {
                    574:                case SIG_BLOCK:
1.109     jdolecek  575:                        sigplusset(nss, &p->p_sigctx.ps_sigmask);
1.79      mycroft   576:                        break;
                    577:                case SIG_UNBLOCK:
1.109     jdolecek  578:                        sigminusset(nss, &p->p_sigctx.ps_sigmask);
1.110     thorpej   579:                        CHECKSIGS(p);
1.79      mycroft   580:                        break;
                    581:                case SIG_SETMASK:
1.109     jdolecek  582:                        p->p_sigctx.ps_sigmask = *nss;
1.110     thorpej   583:                        CHECKSIGS(p);
1.79      mycroft   584:                        break;
                    585:                default:
1.104     thorpej   586:                        (void)spl0();   /* XXXSMP */
1.79      mycroft   587:                        return (EINVAL);
                    588:                }
1.109     jdolecek  589:                sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
1.104     thorpej   590:                (void)spl0();           /* XXXSMP */
1.79      mycroft   591:        }
                    592:
                    593:        return (0);
                    594: }
1.202     perry     595:
1.29      cgd       596: /*
                    597:  * Manipulate signal mask.
                    598:  * Note that we receive new mask, not pointer,
                    599:  * and return old mask as return value;
                    600:  * the library stub does the rest.
                    601:  */
1.52      christos  602: int
1.130     thorpej   603: sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
1.48      thorpej   604: {
1.79      mycroft   605:        struct sys___sigprocmask14_args /* {
1.112     lukem     606:                syscallarg(int)                 how;
                    607:                syscallarg(const sigset_t *)    set;
                    608:                syscallarg(sigset_t *)          oset;
1.48      thorpej   609:        } */ *uap = v;
1.130     thorpej   610:        struct proc     *p;
1.112     lukem     611:        sigset_t        nss, oss;
                    612:        int             error;
1.29      cgd       613:
1.79      mycroft   614:        if (SCARG(uap, set)) {
                    615:                error = copyin(SCARG(uap, set), &nss, sizeof(nss));
                    616:                if (error)
                    617:                        return (error);
                    618:        }
1.130     thorpej   619:        p = l->l_proc;
1.79      mycroft   620:        error = sigprocmask1(p, SCARG(uap, how),
                    621:            SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
                    622:        if (error)
                    623:                return (error);
                    624:        if (SCARG(uap, oset)) {
                    625:                error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
                    626:                if (error)
                    627:                        return (error);
                    628:        }
                    629:        return (0);
                    630: }
                    631:
                    632: void
1.112     lukem     633: sigpending1(struct proc *p, sigset_t *ss)
1.79      mycroft   634: {
1.29      cgd       635:
1.109     jdolecek  636:        *ss = p->p_sigctx.ps_siglist;
                    637:        sigminusset(&p->p_sigctx.ps_sigmask, ss);
1.29      cgd       638: }
                    639:
                    640: /* ARGSUSED */
1.52      christos  641: int
1.130     thorpej   642: sys___sigpending14(struct lwp *l, void *v, register_t *retval)
1.29      cgd       643: {
1.98      augustss  644:        struct sys___sigpending14_args /* {
1.112     lukem     645:                syscallarg(sigset_t *)  set;
1.79      mycroft   646:        } */ *uap = v;
1.130     thorpej   647:        struct proc     *p;
                    648:        sigset_t        ss;
1.79      mycroft   649:
1.130     thorpej   650:        p = l->l_proc;
1.79      mycroft   651:        sigpending1(p, &ss);
                    652:        return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
                    653: }
                    654:
                    655: int
1.112     lukem     656: sigsuspend1(struct proc *p, const sigset_t *ss)
1.79      mycroft   657: {
1.112     lukem     658:        struct sigacts *ps;
1.29      cgd       659:
1.112     lukem     660:        ps = p->p_sigacts;
1.79      mycroft   661:        if (ss) {
                    662:                /*
                    663:                 * When returning from sigpause, we want
                    664:                 * the old mask to be restored after the
                    665:                 * signal handler has finished.  Thus, we
1.109     jdolecek  666:                 * save it here and mark the sigctx structure
1.79      mycroft   667:                 * to indicate this.
                    668:                 */
1.109     jdolecek  669:                p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
                    670:                p->p_sigctx.ps_flags |= SAS_OLDMASK;
1.105     thorpej   671:                (void) splsched();      /* XXXSMP */
1.109     jdolecek  672:                p->p_sigctx.ps_sigmask = *ss;
1.110     thorpej   673:                CHECKSIGS(p);
1.109     jdolecek  674:                sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
1.104     thorpej   675:                (void) spl0();          /* XXXSMP */
1.79      mycroft   676:        }
                    677:
                    678:        while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
1.145     nathanw   679:                /* void */;
1.202     perry     680:
1.79      mycroft   681:        /* always return EINTR rather than ERESTART... */
                    682:        return (EINTR);
1.29      cgd       683: }
                    684:
                    685: /*
                    686:  * Suspend process until signal, providing mask to be set
                    687:  * in the meantime.  Note nonstandard calling convention:
                    688:  * libc stub passes mask, not pointer, to save a copyin.
                    689:  */
                    690: /* ARGSUSED */
                    691: int
1.130     thorpej   692: sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
1.48      thorpej   693: {
1.79      mycroft   694:        struct sys___sigsuspend14_args /* {
1.112     lukem     695:                syscallarg(const sigset_t *)    set;
1.48      thorpej   696:        } */ *uap = v;
1.130     thorpej   697:        struct proc     *p;
1.112     lukem     698:        sigset_t        ss;
                    699:        int             error;
1.79      mycroft   700:
                    701:        if (SCARG(uap, set)) {
                    702:                error = copyin(SCARG(uap, set), &ss, sizeof(ss));
                    703:                if (error)
                    704:                        return (error);
                    705:        }
                    706:
1.130     thorpej   707:        p = l->l_proc;
1.79      mycroft   708:        return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
                    709: }
                    710:
                    711: int
1.112     lukem     712: sigaltstack1(struct proc *p, const struct sigaltstack *nss,
                    713:        struct sigaltstack *oss)
1.79      mycroft   714: {
1.112     lukem     715:
1.79      mycroft   716:        if (oss)
1.109     jdolecek  717:                *oss = p->p_sigctx.ps_sigstk;
1.79      mycroft   718:
                    719:        if (nss) {
                    720:                if (nss->ss_flags & ~SS_ALLBITS)
                    721:                        return (EINVAL);
                    722:
                    723:                if (nss->ss_flags & SS_DISABLE) {
1.109     jdolecek  724:                        if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
1.79      mycroft   725:                                return (EINVAL);
                    726:                } else {
                    727:                        if (nss->ss_size < MINSIGSTKSZ)
                    728:                                return (ENOMEM);
                    729:                }
1.109     jdolecek  730:                p->p_sigctx.ps_sigstk = *nss;
1.79      mycroft   731:        }
                    732:
                    733:        return (0);
1.29      cgd       734: }
                    735:
                    736: /* ARGSUSED */
1.52      christos  737: int
1.130     thorpej   738: sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
1.48      thorpej   739: {
1.98      augustss  740:        struct sys___sigaltstack14_args /* {
1.112     lukem     741:                syscallarg(const struct sigaltstack *)  nss;
                    742:                syscallarg(struct sigaltstack *)        oss;
1.48      thorpej   743:        } */ *uap = v;
1.130     thorpej   744:        struct proc             *p;
1.112     lukem     745:        struct sigaltstack      nss, oss;
                    746:        int                     error;
1.29      cgd       747:
1.79      mycroft   748:        if (SCARG(uap, nss)) {
                    749:                error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
                    750:                if (error)
                    751:                        return (error);
                    752:        }
1.130     thorpej   753:        p = l->l_proc;
1.79      mycroft   754:        error = sigaltstack1(p,
                    755:            SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
1.52      christos  756:        if (error)
1.29      cgd       757:                return (error);
1.79      mycroft   758:        if (SCARG(uap, oss)) {
                    759:                error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
                    760:                if (error)
                    761:                        return (error);
1.29      cgd       762:        }
                    763:        return (0);
                    764: }
                    765:
                    766: /* ARGSUSED */
                    767: int
1.130     thorpej   768: sys_kill(struct lwp *l, void *v, register_t *retval)
1.48      thorpej   769: {
1.98      augustss  770:        struct sys_kill_args /* {
1.112     lukem     771:                syscallarg(int) pid;
                    772:                syscallarg(int) signum;
1.48      thorpej   773:        } */ *uap = v;
1.130     thorpej   774:        struct proc     *cp, *p;
1.112     lukem     775:        struct pcred    *pc;
1.148     christos  776:        ksiginfo_t      ksi;
1.29      cgd       777:
1.130     thorpej   778:        cp = l->l_proc;
1.112     lukem     779:        pc = cp->p_cred;
1.32      cgd       780:        if ((u_int)SCARG(uap, signum) >= NSIG)
1.29      cgd       781:                return (EINVAL);
1.191     matt      782:        KSI_INIT(&ksi);
1.148     christos  783:        ksi.ksi_signo = SCARG(uap, signum);
                    784:        ksi.ksi_code = SI_USER;
                    785:        ksi.ksi_pid = cp->p_pid;
                    786:        ksi.ksi_uid = cp->p_ucred->cr_uid;
1.32      cgd       787:        if (SCARG(uap, pid) > 0) {
1.29      cgd       788:                /* kill single process */
1.32      cgd       789:                if ((p = pfind(SCARG(uap, pid))) == NULL)
1.29      cgd       790:                        return (ESRCH);
1.32      cgd       791:                if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
1.29      cgd       792:                        return (EPERM);
1.32      cgd       793:                if (SCARG(uap, signum))
1.160     christos  794:                        kpsignal2(p, &ksi, 1);
1.29      cgd       795:                return (0);
                    796:        }
1.32      cgd       797:        switch (SCARG(uap, pid)) {
1.29      cgd       798:        case -1:                /* broadcast signal */
1.148     christos  799:                return (killpg1(cp, &ksi, 0, 1));
1.29      cgd       800:        case 0:                 /* signal own process group */
1.148     christos  801:                return (killpg1(cp, &ksi, 0, 0));
1.29      cgd       802:        default:                /* negative explicit process group */
1.148     christos  803:                return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
1.29      cgd       804:        }
                    805:        /* NOTREACHED */
                    806: }
                    807:
                    808: /*
                    809:  * Common code for kill process group/broadcast kill.
                    810:  * cp is calling process.
                    811:  */
1.52      christos  812: int
1.148     christos  813: killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
1.29      cgd       814: {
1.112     lukem     815:        struct proc     *p;
                    816:        struct pcred    *pc;
                    817:        struct pgrp     *pgrp;
                    818:        int             nfound;
1.148     christos  819:        int             signum = ksi->ksi_signo;
1.202     perry     820:
1.112     lukem     821:        pc = cp->p_cred;
                    822:        nfound = 0;
1.91      thorpej   823:        if (all) {
1.202     perry     824:                /*
                    825:                 * broadcast
1.29      cgd       826:                 */
1.92      thorpej   827:                proclist_lock_read();
1.199     yamt      828:                PROCLIST_FOREACH(p, &allproc) {
1.202     perry     829:                        if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1.29      cgd       830:                            p == cp || !CANSIGNAL(cp, pc, p, signum))
                    831:                                continue;
                    832:                        nfound++;
                    833:                        if (signum)
1.160     christos  834:                                kpsignal2(p, ksi, 1);
1.29      cgd       835:                }
1.91      thorpej   836:                proclist_unlock_read();
                    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 {
                    844:                        pgrp = pgfind(pgid);
                    845:                        if (pgrp == NULL)
                    846:                                return (ESRCH);
                    847:                }
1.124     matt      848:                LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1.29      cgd       849:                        if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
                    850:                            !CANSIGNAL(cp, pc, p, signum))
                    851:                                continue;
                    852:                        nfound++;
1.90      thorpej   853:                        if (signum && P_ZOMBIE(p) == 0)
1.160     christos  854:                                kpsignal2(p, ksi, 1);
1.29      cgd       855:                }
                    856:        }
                    857:        return (nfound ? 0 : ESRCH);
                    858: }
                    859:
                    860: /*
                    861:  * Send a signal to a process group.
                    862:  */
                    863: void
1.112     lukem     864: gsignal(int pgid, int signum)
1.29      cgd       865: {
1.148     christos  866:        ksiginfo_t ksi;
1.192     matt      867:        KSI_INIT_EMPTY(&ksi);
1.148     christos  868:        ksi.ksi_signo = signum;
                    869:        kgsignal(pgid, &ksi, NULL);
                    870: }
                    871:
                    872: void
                    873: kgsignal(int pgid, ksiginfo_t *ksi, void *data)
                    874: {
1.29      cgd       875:        struct pgrp *pgrp;
                    876:
                    877:        if (pgid && (pgrp = pgfind(pgid)))
1.148     christos  878:                kpgsignal(pgrp, ksi, data, 0);
1.29      cgd       879: }
                    880:
                    881: /*
1.71      fvdl      882:  * Send a signal to a process group. If checktty is 1,
1.29      cgd       883:  * limit to members which have a controlling terminal.
                    884:  */
                    885: void
1.148     christos  886: pgsignal(struct pgrp *pgrp, int sig, int checkctty)
                    887: {
                    888:        ksiginfo_t ksi;
1.192     matt      889:        KSI_INIT_EMPTY(&ksi);
1.148     christos  890:        ksi.ksi_signo = sig;
                    891:        kpgsignal(pgrp, &ksi, NULL, checkctty);
                    892: }
                    893:
                    894: void
                    895: kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
1.29      cgd       896: {
1.98      augustss  897:        struct proc *p;
1.29      cgd       898:
                    899:        if (pgrp)
1.124     matt      900:                LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1.29      cgd       901:                        if (checkctty == 0 || p->p_flag & P_CONTROLT)
1.148     christos  902:                                kpsignal(p, ksi, data);
1.29      cgd       903: }
                    904:
                    905: /*
                    906:  * Send a signal caused by a trap to the current process.
                    907:  * If it will be caught immediately, deliver it with correct code.
                    908:  * Otherwise, post it normally.
                    909:  */
1.148     christos  910: void
1.160     christos  911: trapsignal(struct lwp *l, const ksiginfo_t *ksi)
1.148     christos  912: {
1.130     thorpej   913:        struct proc     *p;
                    914:        struct sigacts  *ps;
1.148     christos  915:        int signum = ksi->ksi_signo;
1.29      cgd       916:
1.166     thorpej   917:        KASSERT(KSI_TRAP_P(ksi));
                    918:
1.130     thorpej   919:        p = l->l_proc;
1.112     lukem     920:        ps = p->p_sigacts;
1.79      mycroft   921:        if ((p->p_flag & P_TRACED) == 0 &&
1.109     jdolecek  922:            sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
                    923:            !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
1.29      cgd       924:                p->p_stats->p_ru.ru_nsignals++;
                    925: #ifdef KTRACE
                    926:                if (KTRPOINT(p, KTR_PSIG))
1.148     christos  927:                        ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
1.157     christos  928:                            &p->p_sigctx.ps_sigmask, ksi);
1.29      cgd       929: #endif
1.148     christos  930:                kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
1.105     thorpej   931:                (void) splsched();      /* XXXSMP */
1.112     lukem     932:                sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
                    933:                    &p->p_sigctx.ps_sigmask);
1.109     jdolecek  934:                if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
                    935:                        sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.45      mycroft   936:                        if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1.109     jdolecek  937:                                sigaddset(&p->p_sigctx.ps_sigignore, signum);
                    938:                        SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1.45      mycroft   939:                }
1.104     thorpej   940:                (void) spl0();          /* XXXSMP */
1.29      cgd       941:        } else {
1.152     christos  942:                p->p_sigctx.ps_lwp = l->l_lid;
1.148     christos  943:                /* XXX for core dump/debugger */
1.152     christos  944:                p->p_sigctx.ps_signo = ksi->ksi_signo;
                    945:                p->p_sigctx.ps_code = ksi->ksi_trap;
1.160     christos  946:                kpsignal2(p, ksi, 1);
1.29      cgd       947:        }
                    948: }
                    949:
                    950: /*
1.151     christos  951:  * Fill in signal information and signal the parent for a child status change.
                    952:  */
                    953: static void
                    954: child_psignal(struct proc *p, int dolock)
                    955: {
                    956:        ksiginfo_t ksi;
                    957:
1.191     matt      958:        KSI_INIT(&ksi);
1.151     christos  959:        ksi.ksi_signo = SIGCHLD;
                    960:        ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
                    961:        ksi.ksi_pid = p->p_pid;
                    962:        ksi.ksi_uid = p->p_ucred->cr_uid;
                    963:        ksi.ksi_status = p->p_xstat;
                    964:        ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
                    965:        ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
1.160     christos  966:        kpsignal2(p->p_pptr, &ksi, dolock);
1.151     christos  967: }
                    968:
                    969: /*
1.29      cgd       970:  * Send the signal to the process.  If the signal has an action, the action
                    971:  * is usually performed by the target process rather than the caller; we add
                    972:  * the signal to the set of pending signals for the process.
                    973:  *
                    974:  * Exceptions:
                    975:  *   o When a stop signal is sent to a sleeping process that takes the
                    976:  *     default action, the process is stopped without awakening it.
                    977:  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
                    978:  *     regardless of the signal action (eg, blocked or ignored).
                    979:  *
                    980:  * Other ignored signals are discarded immediately.
1.104     thorpej   981:  *
                    982:  * XXXSMP: Invoked as psignal() or sched_psignal().
1.29      cgd       983:  */
                    984: void
1.148     christos  985: psignal1(struct proc *p, int signum, int dolock)
                    986: {
1.165     thorpej   987:        ksiginfo_t ksi;
                    988:
1.192     matt      989:        KSI_INIT_EMPTY(&ksi);
1.165     thorpej   990:        ksi.ksi_signo = signum;
                    991:        kpsignal2(p, &ksi, dolock);
1.148     christos  992: }
                    993:
                    994: void
1.160     christos  995: kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
                    996: {
1.165     thorpej   997:
1.164     christos  998:        if ((p->p_flag & P_WEXIT) == 0 && data) {
1.160     christos  999:                size_t fd;
                   1000:                struct filedesc *fdp = p->p_fd;
1.165     thorpej  1001:
1.160     christos 1002:                ksi->ksi_fd = -1;
                   1003:                for (fd = 0; fd < fdp->fd_nfiles; fd++) {
                   1004:                        struct file *fp = fdp->fd_ofiles[fd];
                   1005:                        /* XXX: lock? */
                   1006:                        if (fp && fp->f_data == data) {
                   1007:                                ksi->ksi_fd = fd;
                   1008:                                break;
                   1009:                        }
                   1010:                }
                   1011:        }
                   1012:        kpsignal2(p, ksi, dolock);
                   1013: }
                   1014:
1.202     perry    1015: static void
1.160     christos 1016: kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
1.29      cgd      1017: {
1.170     christos 1018:        struct lwp *l, *suspended = NULL;
1.187     cl       1019:        struct sadata_vp *vp;
1.130     thorpej  1020:        int     s = 0, prop, allsusp;
1.112     lukem    1021:        sig_t   action;
1.148     christos 1022:        int     signum = ksi->ksi_signo;
1.29      cgd      1023:
1.79      mycroft  1024: #ifdef DIAGNOSTIC
                   1025:        if (signum <= 0 || signum >= NSIG)
1.148     christos 1026:                panic("psignal signal number %d", signum);
                   1027:
1.104     thorpej  1028:        /* XXXSMP: works, but icky */
                   1029:        if (dolock)
                   1030:                SCHED_ASSERT_UNLOCKED();
                   1031:        else
                   1032:                SCHED_ASSERT_LOCKED();
1.79      mycroft  1033: #endif
1.148     christos 1034:
1.183     fvdl     1035:        /*
1.126     jdolecek 1036:         * Notify any interested parties in the signal.
                   1037:         */
                   1038:        KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
                   1039:
1.29      cgd      1040:        prop = sigprop[signum];
                   1041:
                   1042:        /*
                   1043:         * If proc is traced, always give parent a chance.
                   1044:         */
1.198     jdolecek 1045:        if (p->p_flag & P_TRACED) {
                   1046:                action = SIG_DFL;
                   1047:
                   1048:                /*
                   1049:                 * If the process is being traced and the signal is being
                   1050:                 * caught, make sure to save any ksiginfo.
                   1051:                 */
                   1052:                if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
                   1053:                        ksiginfo_put(p, ksi);
                   1054:        } else {
                   1055:                /*
                   1056:                 * If the signal was the result of a trap, reset it
                   1057:                 * to default action if it's currently masked, so that it would
                   1058:                 * coredump immediatelly instead of spinning repeatedly
                   1059:                 * taking the signal.
                   1060:                 */
                   1061:                if (KSI_TRAP_P(ksi)
                   1062:                    && sigismember(&p->p_sigctx.ps_sigmask, signum)
                   1063:                    && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
                   1064:                        sigdelset(&p->p_sigctx.ps_sigignore, signum);
                   1065:                        sigdelset(&p->p_sigctx.ps_sigcatch, signum);
                   1066:                        sigdelset(&p->p_sigctx.ps_sigmask, signum);
                   1067:                        SIGACTION(p, signum).sa_handler = SIG_DFL;
1.190     matt     1068:                }
1.198     jdolecek 1069:
                   1070:                /*
                   1071:                 * If the signal is being ignored,
                   1072:                 * then we forget about it immediately.
                   1073:                 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
                   1074:                 * and if it is set to SIG_IGN,
                   1075:                 * action will be SIG_DFL here.)
                   1076:                 */
                   1077:                if (sigismember(&p->p_sigctx.ps_sigignore, signum))
                   1078:                        return;
                   1079:                if (sigismember(&p->p_sigctx.ps_sigmask, signum))
                   1080:                        action = SIG_HOLD;
                   1081:                else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
                   1082:                        action = SIG_CATCH;
                   1083:                else {
                   1084:                        action = SIG_DFL;
                   1085:
1.44      mycroft  1086:                        if (prop & SA_KILL && p->p_nice > NZERO)
                   1087:                                p->p_nice = NZERO;
                   1088:
                   1089:                        /*
                   1090:                         * If sending a tty stop signal to a member of an
                   1091:                         * orphaned process group, discard the signal here if
                   1092:                         * the action is default; don't stop the process below
                   1093:                         * if sleeping, and don't clear any pending SIGCONT.
                   1094:                         */
                   1095:                        if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
                   1096:                                return;
                   1097:                }
1.29      cgd      1098:        }
                   1099:
                   1100:        if (prop & SA_CONT)
1.109     jdolecek 1101:                sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
1.29      cgd      1102:
1.44      mycroft  1103:        if (prop & SA_STOP)
1.109     jdolecek 1104:                sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
1.44      mycroft  1105:
1.29      cgd      1106:        /*
1.135     jdolecek 1107:         * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1.171     jdolecek 1108:         * please!), check if anything waits on it. If yes, save the
                   1109:         * info into provided ps_sigwaited, and wake-up the waiter.
1.135     jdolecek 1110:         * The signal won't be processed further here.
                   1111:         */
                   1112:        if ((prop & SA_CANTMASK) == 0
1.171     jdolecek 1113:            && p->p_sigctx.ps_sigwaited
                   1114:            && sigismember(p->p_sigctx.ps_sigwait, signum)
1.152     christos 1115:            && p->p_stat != SSTOP) {
1.171     jdolecek 1116:                p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
                   1117:                p->p_sigctx.ps_sigwaited = NULL;
1.135     jdolecek 1118:                if (dolock)
                   1119:                        wakeup_one(&p->p_sigctx.ps_sigwait);
                   1120:                else
                   1121:                        sched_wakeup(&p->p_sigctx.ps_sigwait);
                   1122:                return;
                   1123:        }
                   1124:
1.171     jdolecek 1125:        sigaddset(&p->p_sigctx.ps_siglist, signum);
                   1126:
                   1127:        /* CHECKSIGS() is "inlined" here. */
                   1128:        p->p_sigctx.ps_sigcheck = 1;
                   1129:
1.135     jdolecek 1130:        /*
1.29      cgd      1131:         * Defer further processing for signals which are held,
                   1132:         * except that stopped processes must be continued by SIGCONT.
                   1133:         */
1.152     christos 1134:        if (action == SIG_HOLD &&
                   1135:            ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
1.155     christos 1136:                ksiginfo_put(p, ksi);
1.29      cgd      1137:                return;
1.152     christos 1138:        }
1.104     thorpej  1139:        /* XXXSMP: works, but icky */
                   1140:        if (dolock)
                   1141:                SCHED_LOCK(s);
                   1142:
1.175     cl       1143:        if (p->p_flag & P_SA) {
                   1144:                allsusp = 0;
1.187     cl       1145:                l = NULL;
1.175     cl       1146:                if (p->p_stat == SACTIVE) {
1.187     cl       1147:                        SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
                   1148:                                l = vp->savp_lwp;
                   1149:                                KDASSERT(l != NULL);
                   1150:                                if (l->l_flag & L_SA_IDLE) {
                   1151:                                        /* wakeup idle LWP */
                   1152:                                        goto found;
                   1153:                                        /*NOTREACHED*/
                   1154:                                } else if (l->l_flag & L_SA_YIELD) {
                   1155:                                        /* idle LWP is already waking up */
                   1156:                                        goto out;
                   1157:                                        /*NOTREACHED*/
                   1158:                                }
                   1159:                        }
                   1160:                        SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
                   1161:                                l = vp->savp_lwp;
1.175     cl       1162:                                if (l->l_stat == LSRUN ||
                   1163:                                    l->l_stat == LSONPROC) {
                   1164:                                        signotify(p);
                   1165:                                        goto out;
                   1166:                                        /*NOTREACHED*/
                   1167:                                }
1.202     perry    1168:                                if (l->l_stat == LSSLEEP &&
1.175     cl       1169:                                    l->l_flag & L_SINTR) {
                   1170:                                        /* ok to signal vp lwp */
                   1171:                                } else
                   1172:                                        l = NULL;
                   1173:                        }
1.187     cl       1174:                        if (l == NULL)
                   1175:                                allsusp = 1;
1.175     cl       1176:                } else if (p->p_stat == SSTOP) {
1.187     cl       1177:                        SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
                   1178:                                l = vp->savp_lwp;
                   1179:                                if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
                   1180:                                        break;
1.175     cl       1181:                                l = NULL;
1.187     cl       1182:                        }
1.175     cl       1183:                }
                   1184:        } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
1.29      cgd      1185:                /*
1.202     perry    1186:                 * At least one LWP is running or on a run queue.
                   1187:                 * The signal will be noticed when one of them returns
1.130     thorpej  1188:                 * to userspace.
1.29      cgd      1189:                 */
1.130     thorpej  1190:                signotify(p);
1.202     perry    1191:                /*
1.130     thorpej  1192:                 * The signal will be noticed very soon.
1.29      cgd      1193:                 */
1.130     thorpej  1194:                goto out;
1.175     cl       1195:                /*NOTREACHED*/
1.130     thorpej  1196:        } else {
1.175     cl       1197:                /*
                   1198:                 * Find out if any of the sleeps are interruptable,
                   1199:                 * and if all the live LWPs remaining are suspended.
                   1200:                 */
                   1201:                allsusp = 1;
                   1202:                LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.202     perry    1203:                        if (l->l_stat == LSSLEEP &&
1.175     cl       1204:                            l->l_flag & L_SINTR)
                   1205:                                break;
                   1206:                        if (l->l_stat == LSSUSPENDED)
                   1207:                                suspended = l;
1.202     perry    1208:                        else if ((l->l_stat != LSZOMB) &&
1.175     cl       1209:                            (l->l_stat != LSDEAD))
1.144     fvdl     1210:                                allsusp = 0;
1.29      cgd      1211:                }
1.175     cl       1212:        }
                   1213:
1.187     cl       1214:  found:
1.186     christos 1215:        switch (p->p_stat) {
                   1216:        case SACTIVE:
1.130     thorpej  1217:
1.176     cl       1218:                if (l != NULL && (p->p_flag & P_TRACED))
                   1219:                        goto run;
1.202     perry    1220:
1.176     cl       1221:                /*
                   1222:                 * If SIGCONT is default (or ignored) and process is
                   1223:                 * asleep, we are finished; the process should not
                   1224:                 * be awakened.
                   1225:                 */
                   1226:                if ((prop & SA_CONT) && action == SIG_DFL) {
                   1227:                        sigdelset(&p->p_sigctx.ps_siglist, signum);
                   1228:                        goto done;
                   1229:                }
                   1230:
                   1231:                /*
                   1232:                 * When a sleeping process receives a stop
                   1233:                 * signal, process immediately if possible.
                   1234:                 */
                   1235:                if ((prop & SA_STOP) && action == SIG_DFL) {
1.29      cgd      1236:                        /*
1.176     cl       1237:                         * If a child holding parent blocked,
                   1238:                         * stopping could cause deadlock.
1.29      cgd      1239:                         */
1.176     cl       1240:                        if (p->p_flag & P_PPWAIT) {
                   1241:                                goto out;
1.130     thorpej  1242:                        }
1.176     cl       1243:                        sigdelset(&p->p_sigctx.ps_siglist, signum);
                   1244:                        p->p_xstat = signum;
                   1245:                        if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
1.104     thorpej  1246:                                /*
1.176     cl       1247:                                 * XXXSMP: recursive call; don't lock
                   1248:                                 * the second time around.
1.104     thorpej  1249:                                 */
1.176     cl       1250:                                child_psignal(p, 0);
1.104     thorpej  1251:                        }
1.180     manu     1252:                        proc_stop(p, 1);        /* XXXSMP: recurse? */
1.176     cl       1253:                        goto done;
                   1254:                }
1.29      cgd      1255:
1.176     cl       1256:                if (l == NULL) {
1.130     thorpej  1257:                        /*
1.176     cl       1258:                         * Special case: SIGKILL of a process
                   1259:                         * which is entirely composed of
                   1260:                         * suspended LWPs should succeed. We
                   1261:                         * make this happen by unsuspending one of
                   1262:                         * them.
1.130     thorpej  1263:                         */
1.187     cl       1264:                        if (allsusp && (signum == SIGKILL)) {
                   1265:                                if (p->p_flag & P_SA) {
                   1266:                                        /*
                   1267:                                         * get a suspended lwp from
                   1268:                                         * the cache to send KILL
                   1269:                                         * signal
                   1270:                                         * XXXcl add signal checks at resume points
                   1271:                                         */
                   1272:                                        suspended = sa_getcachelwp
                   1273:                                                (SLIST_FIRST(&p->p_sa->sa_vps));
                   1274:                                }
1.176     cl       1275:                                lwp_continue(suspended);
1.187     cl       1276:                        }
1.176     cl       1277:                        goto done;
                   1278:                }
                   1279:                /*
                   1280:                 * All other (caught or default) signals
                   1281:                 * cause the process to run.
                   1282:                 */
                   1283:                goto runfast;
                   1284:                /*NOTREACHED*/
1.186     christos 1285:        case SSTOP:
1.176     cl       1286:                /* Process is stopped */
                   1287:                /*
                   1288:                 * If traced process is already stopped,
                   1289:                 * then no further action is necessary.
                   1290:                 */
                   1291:                if (p->p_flag & P_TRACED)
                   1292:                        goto done;
1.29      cgd      1293:
1.176     cl       1294:                /*
                   1295:                 * Kill signal always sets processes running,
                   1296:                 * if possible.
                   1297:                 */
                   1298:                if (signum == SIGKILL) {
                   1299:                        l = proc_unstop(p);
                   1300:                        if (l)
                   1301:                                goto runfast;
                   1302:                        goto done;
                   1303:                }
1.202     perry    1304:
1.176     cl       1305:                if (prop & SA_CONT) {
1.29      cgd      1306:                        /*
1.176     cl       1307:                         * If SIGCONT is default (or ignored),
                   1308:                         * we continue the process but don't
                   1309:                         * leave the signal in ps_siglist, as
                   1310:                         * it has no further action.  If
                   1311:                         * SIGCONT is held, we continue the
                   1312:                         * process and leave the signal in
                   1313:                         * ps_siglist.  If the process catches
                   1314:                         * SIGCONT, let it handle the signal
                   1315:                         * itself.  If it isn't waiting on an
                   1316:                         * event, then it goes back to run
                   1317:                         * state.  Otherwise, process goes
1.202     perry    1318:                         * back to sleep state.
1.29      cgd      1319:                         */
1.176     cl       1320:                        if (action == SIG_DFL)
1.202     perry    1321:                                sigdelset(&p->p_sigctx.ps_siglist,
1.176     cl       1322:                                    signum);
                   1323:                        l = proc_unstop(p);
                   1324:                        if (l && (action == SIG_CATCH))
                   1325:                                goto runfast;
                   1326:                        goto out;
                   1327:                }
1.29      cgd      1328:
1.176     cl       1329:                if (prop & SA_STOP) {
1.29      cgd      1330:                        /*
1.176     cl       1331:                         * Already stopped, don't need to stop again.
                   1332:                         * (If we did the shell could get confused.)
1.29      cgd      1333:                         */
1.176     cl       1334:                        sigdelset(&p->p_sigctx.ps_siglist, signum);
                   1335:                        goto done;
1.29      cgd      1336:                }
1.176     cl       1337:
                   1338:                /*
                   1339:                 * If a lwp is sleeping interruptibly, then
                   1340:                 * wake it up; it will run until the kernel
                   1341:                 * boundary, where it will stop in issignal(),
                   1342:                 * since p->p_stat is still SSTOP. When the
                   1343:                 * process is continued, it will be made
                   1344:                 * runnable and can look at the signal.
                   1345:                 */
                   1346:                if (l)
                   1347:                        goto run;
                   1348:                goto out;
1.186     christos 1349:        case SIDL:
                   1350:                /* Process is being created by fork */
                   1351:                /* XXX: We are not ready to receive signals yet */
                   1352:                goto done;
                   1353:        default:
1.176     cl       1354:                /* Else what? */
                   1355:                panic("psignal: Invalid process state %d.", p->p_stat);
                   1356:        }
1.29      cgd      1357:        /*NOTREACHED*/
                   1358:
1.112     lukem    1359:  runfast:
1.152     christos 1360:        if (action == SIG_CATCH) {
1.155     christos 1361:                ksiginfo_put(p, ksi);
1.152     christos 1362:                action = SIG_HOLD;
                   1363:        }
1.29      cgd      1364:        /*
                   1365:         * Raise priority to at least PUSER.
                   1366:         */
1.130     thorpej  1367:        if (l->l_priority > PUSER)
                   1368:                l->l_priority = PUSER;
1.112     lukem    1369:  run:
1.152     christos 1370:        if (action == SIG_CATCH) {
1.155     christos 1371:                ksiginfo_put(p, ksi);
1.152     christos 1372:                action = SIG_HOLD;
                   1373:        }
1.202     perry    1374:
1.130     thorpej  1375:        setrunnable(l);         /* XXXSMP: recurse? */
1.112     lukem    1376:  out:
1.152     christos 1377:        if (action == SIG_CATCH)
1.155     christos 1378:                ksiginfo_put(p, ksi);
1.152     christos 1379:  done:
1.104     thorpej  1380:        /* XXXSMP: works, but icky */
                   1381:        if (dolock)
                   1382:                SCHED_UNLOCK(s);
1.29      cgd      1383: }
                   1384:
1.130     thorpej  1385: void
1.160     christos 1386: kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1.130     thorpej  1387: {
                   1388:        struct proc *p = l->l_proc;
                   1389:        struct lwp *le, *li;
1.150     cl       1390:        siginfo_t *si;
                   1391:        int f;
1.130     thorpej  1392:
                   1393:        if (p->p_flag & P_SA) {
1.144     fvdl     1394:
                   1395:                /* XXXUPSXXX What if not on sa_vp ? */
                   1396:
1.150     cl       1397:                f = l->l_flag & L_SA;
1.202     perry    1398:                l->l_flag &= ~L_SA;
1.130     thorpej  1399:                si = pool_get(&siginfo_pool, PR_WAITOK);
1.166     thorpej  1400:                si->_info = ksi->ksi_info;
1.130     thorpej  1401:                le = li = NULL;
1.166     thorpej  1402:                if (KSI_TRAP_P(ksi))
1.130     thorpej  1403:                        le = l;
                   1404:                else
                   1405:                        li = l;
1.202     perry    1406:                if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1.200     mycroft  1407:                    sizeof(*si), si) != 0) {
                   1408:                        pool_put(&siginfo_pool, si);
                   1409:                        if (KSI_TRAP_P(ksi))
                   1410:                                /* XXX What do we do here?? */;
                   1411:                }
1.150     cl       1412:                l->l_flag |= f;
1.130     thorpej  1413:                return;
                   1414:        }
                   1415:
1.148     christos 1416:        (*p->p_emul->e_sendsig)(ksi, mask);
1.130     thorpej  1417: }
                   1418:
1.112     lukem    1419: static __inline int firstsig(const sigset_t *);
1.79      mycroft  1420:
                   1421: static __inline int
1.112     lukem    1422: firstsig(const sigset_t *ss)
1.79      mycroft  1423: {
                   1424:        int sig;
                   1425:
                   1426:        sig = ffs(ss->__bits[0]);
                   1427:        if (sig != 0)
                   1428:                return (sig);
                   1429: #if NSIG > 33
                   1430:        sig = ffs(ss->__bits[1]);
                   1431:        if (sig != 0)
                   1432:                return (sig + 32);
                   1433: #endif
                   1434: #if NSIG > 65
                   1435:        sig = ffs(ss->__bits[2]);
                   1436:        if (sig != 0)
                   1437:                return (sig + 64);
                   1438: #endif
                   1439: #if NSIG > 97
                   1440:        sig = ffs(ss->__bits[3]);
                   1441:        if (sig != 0)
                   1442:                return (sig + 96);
                   1443: #endif
                   1444:        return (0);
                   1445: }
                   1446:
1.29      cgd      1447: /*
                   1448:  * If the current process has received a signal (should be caught or cause
                   1449:  * termination, should interrupt current syscall), return the signal number.
                   1450:  * Stop signals with default action are processed immediately, then cleared;
                   1451:  * they aren't returned.  This is checked after each entry to the system for
                   1452:  * a syscall or trap (though this can usually be done without calling issignal
                   1453:  * by checking the pending signal masks in the CURSIG macro.) The normal call
                   1454:  * sequence is
                   1455:  *
1.130     thorpej  1456:  *     while (signum = CURSIG(curlwp))
1.29      cgd      1457:  *             postsig(signum);
                   1458:  */
                   1459: int
1.130     thorpej  1460: issignal(struct lwp *l)
1.29      cgd      1461: {
1.130     thorpej  1462:        struct proc     *p = l->l_proc;
1.127     scw      1463:        int             s = 0, signum, prop;
1.130     thorpej  1464:        int             dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1.112     lukem    1465:        sigset_t        ss;
1.29      cgd      1466:
1.187     cl       1467:        /* Bail out if we do not own the virtual processor */
                   1468:        if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
                   1469:                return 0;
1.144     fvdl     1470:
1.130     thorpej  1471:        if (p->p_stat == SSTOP) {
                   1472:                /*
                   1473:                 * The process is stopped/stopping. Stop ourselves now that
                   1474:                 * we're on the kernel/userspace boundary.
                   1475:                 */
                   1476:                if (dolock)
                   1477:                        SCHED_LOCK(s);
                   1478:                l->l_stat = LSSTOP;
                   1479:                p->p_nrlwps--;
                   1480:                if (p->p_flag & P_TRACED)
                   1481:                        goto sigtraceswitch;
                   1482:                else
                   1483:                        goto sigswitch;
                   1484:        }
1.29      cgd      1485:        for (;;) {
1.79      mycroft  1486:                sigpending1(p, &ss);
1.29      cgd      1487:                if (p->p_flag & P_PPWAIT)
1.79      mycroft  1488:                        sigminusset(&stopsigmask, &ss);
                   1489:                signum = firstsig(&ss);
                   1490:                if (signum == 0) {                      /* no signal to send */
1.109     jdolecek 1491:                        p->p_sigctx.ps_sigcheck = 0;
1.119     christos 1492:                        if (locked && dolock)
                   1493:                                SCHED_LOCK(s);
1.29      cgd      1494:                        return (0);
1.79      mycroft  1495:                }
1.112     lukem    1496:                                                        /* take the signal! */
                   1497:                sigdelset(&p->p_sigctx.ps_siglist, signum);
1.42      mycroft  1498:
1.29      cgd      1499:                /*
                   1500:                 * We should see pending but ignored signals
                   1501:                 * only if P_TRACED was on when they were posted.
                   1502:                 */
1.109     jdolecek 1503:                if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1.79      mycroft  1504:                    (p->p_flag & P_TRACED) == 0)
1.29      cgd      1505:                        continue;
1.42      mycroft  1506:
1.29      cgd      1507:                if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
                   1508:                        /*
                   1509:                         * If traced, always stop, and stay
                   1510:                         * stopped until released by the debugger.
                   1511:                         */
                   1512:                        p->p_xstat = signum;
1.184     manu     1513:
                   1514:                        /* Emulation-specific handling of signal trace */
                   1515:                        if ((p->p_emul->e_tracesig != NULL) &&
                   1516:                            ((*p->p_emul->e_tracesig)(p, signum) != 0))
                   1517:                                goto childresumed;
                   1518:
1.66      mycroft  1519:                        if ((p->p_flag & P_FSTRACE) == 0)
1.151     christos 1520:                                child_psignal(p, dolock);
1.119     christos 1521:                        if (dolock)
                   1522:                                SCHED_LOCK(s);
1.180     manu     1523:                        proc_stop(p, 1);
1.130     thorpej  1524:                sigtraceswitch:
                   1525:                        mi_switch(l, NULL);
1.114     nathanw  1526:                        SCHED_ASSERT_UNLOCKED();
1.119     christos 1527:                        if (dolock)
                   1528:                                splx(s);
                   1529:                        else
                   1530:                                dolock = 1;
1.29      cgd      1531:
1.184     manu     1532:                childresumed:
1.29      cgd      1533:                        /*
1.42      mycroft  1534:                         * If we are no longer being traced, or the parent
                   1535:                         * didn't give us a signal, look for more signals.
1.29      cgd      1536:                         */
1.42      mycroft  1537:                        if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1.29      cgd      1538:                                continue;
                   1539:
                   1540:                        /*
1.42      mycroft  1541:                         * If the new signal is being masked, look for other
                   1542:                         * signals.
1.29      cgd      1543:                         */
1.42      mycroft  1544:                        signum = p->p_xstat;
1.130     thorpej  1545:                        p->p_xstat = 0;
1.112     lukem    1546:                        /*
                   1547:                         * `p->p_sigctx.ps_siglist |= mask' is done
                   1548:                         * in setrunnable().
                   1549:                         */
1.109     jdolecek 1550:                        if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1.29      cgd      1551:                                continue;
1.112     lukem    1552:                                                        /* take the signal! */
                   1553:                        sigdelset(&p->p_sigctx.ps_siglist, signum);
1.29      cgd      1554:                }
                   1555:
1.42      mycroft  1556:                prop = sigprop[signum];
                   1557:
1.29      cgd      1558:                /*
                   1559:                 * Decide whether the signal should be returned.
                   1560:                 * Return the signal's number, or fall through
                   1561:                 * to clear it from the pending mask.
                   1562:                 */
1.109     jdolecek 1563:                switch ((long)SIGACTION(p, signum).sa_handler) {
1.29      cgd      1564:
1.33      cgd      1565:                case (long)SIG_DFL:
1.29      cgd      1566:                        /*
                   1567:                         * Don't take default actions on system processes.
                   1568:                         */
                   1569:                        if (p->p_pid <= 1) {
                   1570: #ifdef DIAGNOSTIC
                   1571:                                /*
                   1572:                                 * Are you sure you want to ignore SIGSEGV
                   1573:                                 * in init? XXX
                   1574:                                 */
1.57      christos 1575:                                printf("Process (pid %d) got signal %d\n",
1.29      cgd      1576:                                    p->p_pid, signum);
                   1577: #endif
                   1578:                                break;          /* == ignore */
                   1579:                        }
                   1580:                        /*
                   1581:                         * If there is a pending stop signal to process
                   1582:                         * with default action, stop here,
                   1583:                         * then clear the signal.  However,
                   1584:                         * if process is member of an orphaned
                   1585:                         * process group, ignore tty stop signals.
                   1586:                         */
                   1587:                        if (prop & SA_STOP) {
                   1588:                                if (p->p_flag & P_TRACED ||
                   1589:                                    (p->p_pgrp->pg_jobc == 0 &&
                   1590:                                    prop & SA_TTYSTOP))
                   1591:                                        break;  /* == ignore */
                   1592:                                p->p_xstat = signum;
                   1593:                                if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1.151     christos 1594:                                        child_psignal(p, dolock);
1.119     christos 1595:                                if (dolock)
                   1596:                                        SCHED_LOCK(s);
1.180     manu     1597:                                proc_stop(p, 1);
1.130     thorpej  1598:                        sigswitch:
                   1599:                                mi_switch(l, NULL);
1.104     thorpej  1600:                                SCHED_ASSERT_UNLOCKED();
1.119     christos 1601:                                if (dolock)
                   1602:                                        splx(s);
                   1603:                                else
                   1604:                                        dolock = 1;
1.29      cgd      1605:                                break;
                   1606:                        } else if (prop & SA_IGNORE) {
                   1607:                                /*
                   1608:                                 * Except for SIGCONT, shouldn't get here.
                   1609:                                 * Default action is to ignore; drop it.
                   1610:                                 */
                   1611:                                break;          /* == ignore */
                   1612:                        } else
1.42      mycroft  1613:                                goto keep;
1.29      cgd      1614:                        /*NOTREACHED*/
                   1615:
1.33      cgd      1616:                case (long)SIG_IGN:
1.29      cgd      1617:                        /*
                   1618:                         * Masking above should prevent us ever trying
                   1619:                         * to take action on an ignored signal other
                   1620:                         * than SIGCONT, unless process is traced.
                   1621:                         */
1.128     jdolecek 1622: #ifdef DEBUG_ISSIGNAL
1.29      cgd      1623:                        if ((prop & SA_CONT) == 0 &&
                   1624:                            (p->p_flag & P_TRACED) == 0)
1.57      christos 1625:                                printf("issignal\n");
1.128     jdolecek 1626: #endif
1.29      cgd      1627:                        break;          /* == ignore */
                   1628:
                   1629:                default:
                   1630:                        /*
                   1631:                         * This signal has an action, let
                   1632:                         * postsig() process it.
                   1633:                         */
1.42      mycroft  1634:                        goto keep;
1.29      cgd      1635:                }
                   1636:        }
                   1637:        /* NOTREACHED */
1.42      mycroft  1638:
1.112     lukem    1639:  keep:
                   1640:                                                /* leave the signal for later */
                   1641:        sigaddset(&p->p_sigctx.ps_siglist, signum);
1.110     thorpej  1642:        CHECKSIGS(p);
1.119     christos 1643:        if (locked && dolock)
                   1644:                SCHED_LOCK(s);
1.42      mycroft  1645:        return (signum);
1.29      cgd      1646: }
                   1647:
                   1648: /*
                   1649:  * Put the argument process into the stopped state and notify the parent
                   1650:  * via wakeup.  Signals are handled elsewhere.  The process must not be
                   1651:  * on the run queue.
                   1652:  */
1.179     christos 1653: void
1.180     manu     1654: proc_stop(struct proc *p, int wakeup)
1.29      cgd      1655: {
1.130     thorpej  1656:        struct lwp *l;
1.178     dsl      1657:        struct proc *parent;
1.187     cl       1658:        struct sadata_vp *vp;
1.29      cgd      1659:
1.104     thorpej  1660:        SCHED_ASSERT_LOCKED();
                   1661:
1.130     thorpej  1662:        /* XXX lock process LWP state */
1.178     dsl      1663:        p->p_flag &= ~P_WAITED;
1.29      cgd      1664:        p->p_stat = SSTOP;
1.178     dsl      1665:        parent = p->p_pptr;
                   1666:        parent->p_nstopchild++;
1.130     thorpej  1667:
1.175     cl       1668:        if (p->p_flag & P_SA) {
                   1669:                /*
                   1670:                 * Only (try to) put the LWP on the VP in stopped
1.202     perry    1671:                 * state.
1.177     cl       1672:                 * All other LWPs will suspend in sa_setwoken()
                   1673:                 * because the VP-LWP in stopped state cannot be
                   1674:                 * repossessed.
1.175     cl       1675:                 */
1.187     cl       1676:                SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
                   1677:                        l = vp->savp_lwp;
                   1678:                        if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
                   1679:                                l->l_stat = LSSTOP;
                   1680:                                p->p_nrlwps--;
                   1681:                        } else if (l->l_stat == LSRUN) {
                   1682:                                /* Remove LWP from the run queue */
                   1683:                                remrunqueue(l);
                   1684:                                l->l_stat = LSSTOP;
                   1685:                                p->p_nrlwps--;
                   1686:                        } else if (l->l_stat == LSSLEEP &&
                   1687:                            l->l_flag & L_SA_IDLE) {
                   1688:                                l->l_flag &= ~L_SA_IDLE;
                   1689:                                l->l_stat = LSSTOP;
                   1690:                        }
1.175     cl       1691:                }
                   1692:                goto out;
                   1693:        }
                   1694:
1.202     perry    1695:        /*
                   1696:         * Put as many LWP's as possible in stopped state.
1.130     thorpej  1697:         * Sleeping ones will notice the stopped state as they try to
                   1698:         * return to userspace.
                   1699:         */
1.175     cl       1700:
1.132     jdolecek 1701:        LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.175     cl       1702:                if (l->l_stat == LSONPROC) {
1.130     thorpej  1703:                        /* XXX SMP this assumes that a LWP that is LSONPROC
1.202     perry    1704:                         * is curlwp and hence is about to be mi_switched
1.130     thorpej  1705:                         * away; the only callers of proc_stop() are:
                   1706:                         * - psignal
                   1707:                         * - issignal()
                   1708:                         * For the former, proc_stop() is only called when
                   1709:                         * no processes are running, so we don't worry.
                   1710:                         * For the latter, proc_stop() is called right
                   1711:                         * before mi_switch().
                   1712:                         */
                   1713:                        l->l_stat = LSSTOP;
                   1714:                        p->p_nrlwps--;
1.175     cl       1715:                } else if (l->l_stat == LSRUN) {
1.130     thorpej  1716:                        /* Remove LWP from the run queue */
                   1717:                        remrunqueue(l);
                   1718:                        l->l_stat = LSSTOP;
                   1719:                        p->p_nrlwps--;
                   1720:                } else if ((l->l_stat == LSSLEEP) ||
1.202     perry    1721:                    (l->l_stat == LSSUSPENDED) ||
1.130     thorpej  1722:                    (l->l_stat == LSZOMB) ||
                   1723:                    (l->l_stat == LSDEAD)) {
                   1724:                        /*
                   1725:                         * Don't do anything; let sleeping LWPs
                   1726:                         * discover the stopped state of the process
                   1727:                         * on their way out of the kernel; otherwise,
                   1728:                         * things like NFS threads that sleep with
                   1729:                         * locks will block the rest of the system
                   1730:                         * from getting any work done.
                   1731:                         *
                   1732:                         * Suspended/dead/zombie LWPs aren't going
                   1733:                         * anywhere, so we don't need to touch them.
                   1734:                         */
                   1735:                }
                   1736: #ifdef DIAGNOSTIC
                   1737:                else {
                   1738:                        panic("proc_stop: process %d lwp %d "
1.202     perry    1739:                              "in unstoppable state %d.\n",
1.130     thorpej  1740:                            p->p_pid, l->l_lid, l->l_stat);
                   1741:                }
                   1742: #endif
                   1743:        }
1.175     cl       1744:
                   1745:  out:
1.130     thorpej  1746:        /* XXX unlock process LWP state */
1.144     fvdl     1747:
1.180     manu     1748:        if (wakeup)
                   1749:                sched_wakeup((caddr_t)p->p_pptr);
1.29      cgd      1750: }
                   1751:
1.133     nathanw  1752: /*
1.202     perry    1753:  * Given a process in state SSTOP, set the state back to SACTIVE and
1.133     nathanw  1754:  * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
                   1755:  *
                   1756:  * If no LWPs ended up runnable (and therefore able to take a signal),
                   1757:  * return a LWP that is sleeping interruptably. The caller can wake
                   1758:  * that LWP up to take a signal.
                   1759:  */
1.130     thorpej  1760: struct lwp *
1.139     skrll    1761: proc_unstop(struct proc *p)
1.130     thorpej  1762: {
                   1763:        struct lwp *l, *lr = NULL;
1.187     cl       1764:        struct sadata_vp *vp;
1.133     nathanw  1765:        int cantake = 0;
1.130     thorpej  1766:
                   1767:        SCHED_ASSERT_LOCKED();
                   1768:
                   1769:        /*
1.140     nathanw  1770:         * Our caller wants to be informed if there are only sleeping
                   1771:         * and interruptable LWPs left after we have run so that it
                   1772:         * can invoke setrunnable() if required - return one of the
                   1773:         * interruptable LWPs if this is the case.
1.130     thorpej  1774:         */
                   1775:
1.178     dsl      1776:        if (!(p->p_flag & P_WAITED))
                   1777:                p->p_pptr->p_nstopchild--;
1.130     thorpej  1778:        p->p_stat = SACTIVE;
1.132     jdolecek 1779:        LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1.140     nathanw  1780:                if (l->l_stat == LSRUN) {
                   1781:                        lr = NULL;
1.133     nathanw  1782:                        cantake = 1;
1.140     nathanw  1783:                }
1.132     jdolecek 1784:                if (l->l_stat != LSSTOP)
                   1785:                        continue;
                   1786:
1.133     nathanw  1787:                if (l->l_wchan != NULL) {
                   1788:                        l->l_stat = LSSLEEP;
                   1789:                        if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1.132     jdolecek 1790:                                lr = l;
1.133     nathanw  1791:                                cantake = 1;
                   1792:                        }
                   1793:                } else {
1.140     nathanw  1794:                        setrunnable(l);
                   1795:                        lr = NULL;
1.133     nathanw  1796:                        cantake = 1;
                   1797:                }
1.132     jdolecek 1798:        }
1.175     cl       1799:        if (p->p_flag & P_SA) {
                   1800:                /* Only consider returning the LWP on the VP. */
1.187     cl       1801:                SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
                   1802:                        lr = vp->savp_lwp;
                   1803:                        if (lr->l_stat == LSSLEEP) {
                   1804:                                if (lr->l_flag & L_SA_YIELD) {
                   1805:                                        setrunnable(lr);
                   1806:                                        break;
                   1807:                                } else if (lr->l_flag & L_SINTR)
                   1808:                                        return lr;
                   1809:                        }
1.175     cl       1810:                }
                   1811:                return NULL;
                   1812:        }
1.130     thorpej  1813:        return lr;
                   1814: }
                   1815:
1.29      cgd      1816: /*
                   1817:  * Take the action for the specified signal
                   1818:  * from the current set of pending signals.
                   1819:  */
                   1820: void
1.112     lukem    1821: postsig(int signum)
1.29      cgd      1822: {
1.130     thorpej  1823:        struct lwp *l;
1.112     lukem    1824:        struct proc     *p;
                   1825:        struct sigacts  *ps;
                   1826:        sig_t           action;
                   1827:        sigset_t        *returnmask;
1.29      cgd      1828:
1.130     thorpej  1829:        l = curlwp;
                   1830:        p = l->l_proc;
1.112     lukem    1831:        ps = p->p_sigacts;
1.29      cgd      1832: #ifdef DIAGNOSTIC
                   1833:        if (signum == 0)
                   1834:                panic("postsig");
                   1835: #endif
1.106     thorpej  1836:
1.130     thorpej  1837:        KERNEL_PROC_LOCK(l);
1.106     thorpej  1838:
1.188     cl       1839: #ifdef MULTIPROCESSOR
                   1840:        /*
                   1841:         * On MP, issignal() can return the same signal to multiple
                   1842:         * LWPs.  The LWPs will block above waiting for the kernel
                   1843:         * lock and the first LWP which gets through will then remove
                   1844:         * the signal from ps_siglist.  All other LWPs exit here.
                   1845:         */
                   1846:        if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
                   1847:                KERNEL_PROC_UNLOCK(l);
                   1848:                return;
                   1849:        }
                   1850: #endif
1.109     jdolecek 1851:        sigdelset(&p->p_sigctx.ps_siglist, signum);
                   1852:        action = SIGACTION_PS(ps, signum).sa_handler;
1.157     christos 1853:        if (action == SIG_DFL) {
1.29      cgd      1854: #ifdef KTRACE
1.157     christos 1855:                if (KTRPOINT(p, KTR_PSIG))
                   1856:                        ktrpsig(p, signum, action,
                   1857:                            p->p_sigctx.ps_flags & SAS_OLDMASK ?
                   1858:                            &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
                   1859:                            NULL);
1.29      cgd      1860: #endif
                   1861:                /*
                   1862:                 * Default action, where the default is to kill
                   1863:                 * the process.  (Other cases were ignored above.)
                   1864:                 */
1.130     thorpej  1865:                sigexit(l, signum);
1.29      cgd      1866:                /* NOTREACHED */
                   1867:        } else {
1.152     christos 1868:                ksiginfo_t *ksi;
1.29      cgd      1869:                /*
                   1870:                 * If we get here, the signal must be caught.
                   1871:                 */
                   1872: #ifdef DIAGNOSTIC
1.112     lukem    1873:                if (action == SIG_IGN ||
                   1874:                    sigismember(&p->p_sigctx.ps_sigmask, signum))
1.29      cgd      1875:                        panic("postsig action");
                   1876: #endif
                   1877:                /*
                   1878:                 * Set the new mask value and also defer further
1.138     wiz      1879:                 * occurrences of this signal.
1.29      cgd      1880:                 *
                   1881:                 * Special case: user has done a sigpause.  Here the
                   1882:                 * current mask is not of interest, but rather the
                   1883:                 * mask from before the sigpause is what we want
                   1884:                 * restored after the signal processing is completed.
                   1885:                 */
1.109     jdolecek 1886:                if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
                   1887:                        returnmask = &p->p_sigctx.ps_oldmask;
                   1888:                        p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1.29      cgd      1889:                } else
1.109     jdolecek 1890:                        returnmask = &p->p_sigctx.ps_sigmask;
1.29      cgd      1891:                p->p_stats->p_ru.ru_nsignals++;
1.152     christos 1892:                ksi = ksiginfo_get(p, signum);
1.157     christos 1893: #ifdef KTRACE
                   1894:                if (KTRPOINT(p, KTR_PSIG))
                   1895:                        ktrpsig(p, signum, action,
                   1896:                            p->p_sigctx.ps_flags & SAS_OLDMASK ?
                   1897:                            &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
                   1898:                            ksi);
                   1899: #endif
1.152     christos 1900:                if (ksi == NULL) {
                   1901:                        ksiginfo_t ksi1;
                   1902:                        /*
                   1903:                         * we did not save any siginfo for this, either
                   1904:                         * because the signal was not caught, or because the
                   1905:                         * user did not request SA_SIGINFO
                   1906:                         */
1.192     matt     1907:                        KSI_INIT_EMPTY(&ksi1);
1.152     christos 1908:                        ksi1.ksi_signo = signum;
                   1909:                        kpsendsig(l, &ksi1, returnmask);
1.29      cgd      1910:                } else {
1.152     christos 1911:                        kpsendsig(l, ksi, returnmask);
                   1912:                        pool_put(&ksiginfo_pool, ksi);
                   1913:                }
                   1914:                p->p_sigctx.ps_lwp = 0;
                   1915:                p->p_sigctx.ps_code = 0;
                   1916:                p->p_sigctx.ps_signo = 0;
1.105     thorpej  1917:                (void) splsched();      /* XXXSMP */
1.112     lukem    1918:                sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
                   1919:                    &p->p_sigctx.ps_sigmask);
1.109     jdolecek 1920:                if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
                   1921:                        sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1.79      mycroft  1922:                        if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1.109     jdolecek 1923:                                sigaddset(&p->p_sigctx.ps_sigignore, signum);
                   1924:                        SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1.79      mycroft  1925:                }
1.104     thorpej  1926:                (void) spl0();          /* XXXSMP */
1.29      cgd      1927:        }
1.106     thorpej  1928:
1.130     thorpej  1929:        KERNEL_PROC_UNLOCK(l);
1.29      cgd      1930: }
                   1931:
                   1932: /*
                   1933:  * Kill the current process for stated reason.
                   1934:  */
1.52      christos 1935: void
1.122     manu     1936: killproc(struct proc *p, const char *why)
1.29      cgd      1937: {
                   1938:        log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
                   1939:        uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
                   1940:        psignal(p, SIGKILL);
                   1941: }
                   1942:
                   1943: /*
                   1944:  * Force the current process to exit with the specified signal, dumping core
                   1945:  * if appropriate.  We bypass the normal tests for masked and caught signals,
                   1946:  * allowing unrecoverable failures to terminate the process without changing
                   1947:  * signal state.  Mark the accounting record with the signal termination.
                   1948:  * If dumping core, save the signal number for the debugger.  Calls exit and
                   1949:  * does not return.
                   1950:  */
1.96      fair     1951:
1.97      fair     1952: #if defined(DEBUG)
1.96      fair     1953: int    kern_logsigexit = 1;    /* not static to make public for sysctl */
                   1954: #else
                   1955: int    kern_logsigexit = 0;    /* not static to make public for sysctl */
                   1956: #endif
                   1957:
1.102     sommerfe 1958: static const char logcoredump[] =
1.96      fair     1959:        "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1.102     sommerfe 1960: static const char lognocoredump[] =
1.96      fair     1961:        "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
                   1962:
1.130     thorpej  1963: /* Wrapper function for use in p_userret */
                   1964: static void
                   1965: lwp_coredump_hook(struct lwp *l, void *arg)
                   1966: {
                   1967:        int s;
                   1968:
                   1969:        /*
                   1970:         * Suspend ourselves, so that the kernel stack and therefore
                   1971:         * the userland registers saved in the trapframe are around
                   1972:         * for coredump() to write them out.
                   1973:         */
                   1974:        KERNEL_PROC_LOCK(l);
                   1975:        l->l_flag &= ~L_DETACHED;
                   1976:        SCHED_LOCK(s);
                   1977:        l->l_stat = LSSUSPENDED;
                   1978:        l->l_proc->p_nrlwps--;
                   1979:        /* XXX NJWLWP check if this makes sense here: */
1.202     perry    1980:        l->l_proc->p_stats->p_ru.ru_nvcsw++;
1.130     thorpej  1981:        mi_switch(l, NULL);
                   1982:        SCHED_ASSERT_UNLOCKED();
                   1983:        splx(s);
                   1984:
                   1985:        lwp_exit(l);
                   1986: }
                   1987:
1.52      christos 1988: void
1.130     thorpej  1989: sigexit(struct lwp *l, int signum)
1.29      cgd      1990: {
1.130     thorpej  1991:        struct proc     *p;
1.144     fvdl     1992: #if 0
1.136     nathanw  1993:        struct lwp      *l2;
1.144     fvdl     1994: #endif
1.130     thorpej  1995:        int             error, exitsig;
                   1996:
                   1997:        p = l->l_proc;
                   1998:
                   1999:        /*
1.202     perry    2000:         * Don't permit coredump() or exit1() multiple times
1.130     thorpej  2001:         * in the same process.
                   2002:         */
1.136     nathanw  2003:        if (p->p_flag & P_WEXIT) {
                   2004:                KERNEL_PROC_UNLOCK(l);
1.130     thorpej  2005:                (*p->p_userret)(l, p->p_userret_arg);
1.136     nathanw  2006:        }
1.130     thorpej  2007:        p->p_flag |= P_WEXIT;
                   2008:        /* We don't want to switch away from exiting. */
                   2009:        /* XXX multiprocessor: stop LWPs on other processors. */
1.144     fvdl     2010: #if 0
1.136     nathanw  2011:        if (p->p_flag & P_SA) {
                   2012:                LIST_FOREACH(l2, &p->p_lwps, l_sibling)
                   2013:                    l2->l_flag &= ~L_SA;
1.130     thorpej  2014:                p->p_flag &= ~P_SA;
                   2015:        }
1.144     fvdl     2016: #endif
1.130     thorpej  2017:
                   2018:        /* Make other LWPs stick around long enough to be dumped */
                   2019:        p->p_userret = lwp_coredump_hook;
                   2020:        p->p_userret_arg = NULL;
1.96      fair     2021:
1.112     lukem    2022:        exitsig = signum;
1.29      cgd      2023:        p->p_acflag |= AXSIG;
                   2024:        if (sigprop[signum] & SA_CORE) {
1.152     christos 2025:                p->p_sigctx.ps_signo = signum;
1.201     christos 2026:                if ((error = coredump(l, NULL)) == 0)
1.102     sommerfe 2027:                        exitsig |= WCOREFLAG;
                   2028:
                   2029:                if (kern_logsigexit) {
1.123     thorpej  2030:                        /* XXX What if we ever have really large UIDs? */
1.202     perry    2031:                        int uid = p->p_cred && p->p_ucred ?
1.123     thorpej  2032:                                (int) p->p_ucred->cr_uid : -1;
1.102     sommerfe 2033:
1.202     perry    2034:                        if (error)
1.102     sommerfe 2035:                                log(LOG_INFO, lognocoredump, p->p_pid,
                   2036:                                    p->p_comm, uid, signum, error);
                   2037:                        else
                   2038:                                log(LOG_INFO, logcoredump, p->p_pid,
                   2039:                                    p->p_comm, uid, signum);
1.96      fair     2040:                }
                   2041:
1.29      cgd      2042:        }
1.96      fair     2043:
1.130     thorpej  2044:        exit1(l, W_EXITCODE(0, exitsig));
1.29      cgd      2045:        /* NOTREACHED */
                   2046: }
                   2047:
                   2048: /*
1.202     perry    2049:  * Dump core, into a file named "progname.core" or "core" (depending on the
1.75      nathanw  2050:  * value of shortcorename), unless the process was setuid/setgid.
1.29      cgd      2051:  */
                   2052: int
1.201     christos 2053: coredump(struct lwp *l, const char *pattern)
1.29      cgd      2054: {
1.112     lukem    2055:        struct vnode            *vp;
1.130     thorpej  2056:        struct proc             *p;
1.112     lukem    2057:        struct vmspace          *vm;
                   2058:        struct ucred            *cred;
                   2059:        struct nameidata        nd;
                   2060:        struct vattr            vattr;
1.169     hannken  2061:        struct mount            *mp;
1.112     lukem    2062:        int                     error, error1;
                   2063:        char                    name[MAXPATHLEN];
                   2064:
1.130     thorpej  2065:        p = l->l_proc;
1.112     lukem    2066:        vm = p->p_vmspace;
                   2067:        cred = p->p_cred->pc_ucred;
1.29      cgd      2068:
1.59      cgd      2069:        /*
                   2070:         * Make sure the process has not set-id, to prevent data leaks.
                   2071:         */
1.58      mrg      2072:        if (p->p_flag & P_SUGID)
1.59      cgd      2073:                return (EPERM);
                   2074:
                   2075:        /*
                   2076:         * Refuse to core if the data + stack + user size is larger than
                   2077:         * the core dump limit.  XXX THIS IS WRONG, because of mapped
                   2078:         * data.
                   2079:         */
1.30      deraadt  2080:        if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1.29      cgd      2081:            p->p_rlimit[RLIMIT_CORE].rlim_cur)
1.59      cgd      2082:                return (EFBIG);         /* better error code? */
                   2083:
1.169     hannken  2084: restart:
1.59      cgd      2085:        /*
                   2086:         * The core dump will go in the current working directory.  Make
1.80      pk       2087:         * sure that the directory is still there and that the mount flags
                   2088:         * allow us to write core dumps there.
1.59      cgd      2089:         */
1.88      thorpej  2090:        vp = p->p_cwdi->cwdi_cdir;
1.80      pk       2091:        if (vp->v_mount == NULL ||
                   2092:            (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1.59      cgd      2093:                return (EPERM);
                   2094:
1.201     christos 2095:        if (pattern == NULL)
                   2096:                pattern = p->p_limit->pl_corename;
                   2097:        if ((error = build_corename(p, name, pattern, sizeof(name))) != 0)
1.94      bouyer   2098:                return error;
                   2099:
1.143     fvdl     2100:        NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1.129     christos 2101:        error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
1.52      christos 2102:        if (error)
1.29      cgd      2103:                return (error);
                   2104:        vp = nd.ni_vp;
                   2105:
1.169     hannken  2106:        if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
                   2107:                VOP_UNLOCK(vp, 0);
                   2108:                if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
                   2109:                        return (error);
                   2110:                if ((error = vn_start_write(NULL, &mp,
                   2111:                    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
                   2112:                        return (error);
                   2113:                goto restart;
                   2114:        }
                   2115:
1.29      cgd      2116:        /* Don't dump to non-regular files or files with links. */
                   2117:        if (vp->v_type != VREG ||
1.143     fvdl     2118:            VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1.59      cgd      2119:                error = EINVAL;
1.29      cgd      2120:                goto out;
                   2121:        }
                   2122:        VATTR_NULL(&vattr);
                   2123:        vattr.va_size = 0;
1.143     fvdl     2124:        VOP_LEASE(vp, p, cred, LEASE_WRITE);
                   2125:        VOP_SETATTR(vp, &vattr, cred, p);
1.29      cgd      2126:        p->p_acflag |= ACORE;
1.95      eeh      2127:
1.118     thorpej  2128:        /* Now dump the actual core file. */
1.130     thorpej  2129:        error = (*p->p_execsw->es_coredump)(l, vp, cred);
1.112     lukem    2130:  out:
1.71      fvdl     2131:        VOP_UNLOCK(vp, 0);
1.169     hannken  2132:        vn_finished_write(mp, 0);
1.143     fvdl     2133:        error1 = vn_close(vp, FWRITE, cred, p);
1.29      cgd      2134:        if (error == 0)
                   2135:                error = error1;
                   2136:        return (error);
                   2137: }
                   2138:
                   2139: /*
                   2140:  * Nonexistent system call-- signal process (may want to handle it).
                   2141:  * Flag error in case process won't see signal immediately (blocked or ignored).
                   2142:  */
                   2143: /* ARGSUSED */
                   2144: int
1.130     thorpej  2145: sys_nosys(struct lwp *l, void *v, register_t *retval)
1.29      cgd      2146: {
1.130     thorpej  2147:        struct proc     *p;
1.29      cgd      2148:
1.130     thorpej  2149:        p = l->l_proc;
1.29      cgd      2150:        psignal(p, SIGSYS);
1.36      cgd      2151:        return (ENOSYS);
1.94      bouyer   2152: }
                   2153:
                   2154: static int
1.201     christos 2155: build_corename(struct proc *p, char *dst, const char *src, size_t len)
1.94      bouyer   2156: {
1.112     lukem    2157:        const char      *s;
                   2158:        char            *d, *end;
                   2159:        int             i;
1.202     perry    2160:
1.201     christos 2161:        for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
1.94      bouyer   2162:                if (*s == '%') {
1.107     enami    2163:                        switch (*(s + 1)) {
1.94      bouyer   2164:                        case 'n':
1.107     enami    2165:                                i = snprintf(d, end - d, "%s", p->p_comm);
1.94      bouyer   2166:                                break;
                   2167:                        case 'p':
1.107     enami    2168:                                i = snprintf(d, end - d, "%d", p->p_pid);
1.94      bouyer   2169:                                break;
                   2170:                        case 'u':
1.134     dsl      2171:                                i = snprintf(d, end - d, "%.*s",
                   2172:                                    (int)sizeof p->p_pgrp->pg_session->s_login,
1.100     sommerfe 2173:                                    p->p_pgrp->pg_session->s_login);
1.94      bouyer   2174:                                break;
                   2175:                        case 't':
1.107     enami    2176:                                i = snprintf(d, end - d, "%ld",
1.100     sommerfe 2177:                                    p->p_stats->p_start.tv_sec);
1.94      bouyer   2178:                                break;
                   2179:                        default:
                   2180:                                goto copy;
                   2181:                        }
                   2182:                        d += i;
                   2183:                        s++;
                   2184:                } else {
1.112     lukem    2185:  copy:                 *d = *s;
1.94      bouyer   2186:                        d++;
                   2187:                }
1.107     enami    2188:                if (d >= end)
                   2189:                        return (ENAMETOOLONG);
1.94      bouyer   2190:        }
                   2191:        *d = '\0';
1.130     thorpej  2192:        return 0;
                   2193: }
                   2194:
                   2195: void
                   2196: getucontext(struct lwp *l, ucontext_t *ucp)
                   2197: {
                   2198:        struct proc     *p;
                   2199:
                   2200:        p = l->l_proc;
                   2201:
                   2202:        ucp->uc_flags = 0;
                   2203:        ucp->uc_link = l->l_ctxlink;
                   2204:
                   2205:        (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
                   2206:        ucp->uc_flags |= _UC_SIGMASK;
                   2207:
                   2208:        /*
                   2209:         * The (unsupplied) definition of the `current execution stack'
                   2210:         * in the System V Interface Definition appears to allow returning
                   2211:         * the main context stack.
                   2212:         */
                   2213:        if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
                   2214:                ucp->uc_stack.ss_sp = (void *)USRSTACK;
                   2215:                ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
                   2216:                ucp->uc_stack.ss_flags = 0;     /* XXX, def. is Very Fishy */
                   2217:        } else {
                   2218:                /* Simply copy alternate signal execution stack. */
                   2219:                ucp->uc_stack = p->p_sigctx.ps_sigstk;
                   2220:        }
                   2221:        ucp->uc_flags |= _UC_STACK;
                   2222:
                   2223:        cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
                   2224: }
                   2225:
                   2226: /* ARGSUSED */
                   2227: int
                   2228: sys_getcontext(struct lwp *l, void *v, register_t *retval)
                   2229: {
                   2230:        struct sys_getcontext_args /* {
                   2231:                syscallarg(struct __ucontext *) ucp;
                   2232:        } */ *uap = v;
                   2233:        ucontext_t uc;
                   2234:
                   2235:        getucontext(l, &uc);
                   2236:
                   2237:        return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
                   2238: }
                   2239:
                   2240: int
                   2241: setucontext(struct lwp *l, const ucontext_t *ucp)
                   2242: {
                   2243:        struct proc     *p;
                   2244:        int             error;
                   2245:
                   2246:        p = l->l_proc;
                   2247:        if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
                   2248:                return (error);
                   2249:        l->l_ctxlink = ucp->uc_link;
1.185     matt     2250:
                   2251:        if ((ucp->uc_flags & _UC_SIGMASK) != 0)
                   2252:                sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
                   2253:
1.130     thorpej  2254:        /*
1.185     matt     2255:         * If there was stack information, update whether or not we are
                   2256:         * still running on an alternate signal stack.
1.130     thorpej  2257:         */
1.185     matt     2258:        if ((ucp->uc_flags & _UC_STACK) != 0) {
                   2259:                if (ucp->uc_stack.ss_flags & SS_ONSTACK)
                   2260:                        p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
                   2261:                else
                   2262:                        p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
                   2263:        }
1.130     thorpej  2264:
                   2265:        return 0;
                   2266: }
                   2267:
                   2268: /* ARGSUSED */
                   2269: int
                   2270: sys_setcontext(struct lwp *l, void *v, register_t *retval)
                   2271: {
                   2272:        struct sys_setcontext_args /* {
                   2273:                syscallarg(const ucontext_t *) ucp;
                   2274:        } */ *uap = v;
                   2275:        ucontext_t uc;
                   2276:        int error;
                   2277:
                   2278:        if (SCARG(uap, ucp) == NULL)    /* i.e. end of uc_link chain */
                   2279:                exit1(l, W_EXITCODE(0, 0));
                   2280:        else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
                   2281:            (error = setucontext(l, &uc)) != 0)
                   2282:                return (error);
                   2283:
                   2284:        return (EJUSTRETURN);
1.108     jdolecek 2285: }
1.130     thorpej  2286:
1.135     jdolecek 2287: /*
                   2288:  * sigtimedwait(2) system call, used also for implementation
                   2289:  * of sigwaitinfo() and sigwait().
                   2290:  *
                   2291:  * This only handles single LWP in signal wait. libpthread provides
                   2292:  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
                   2293:  */
                   2294: int
                   2295: sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
                   2296: {
                   2297:        struct sys___sigtimedwait_args /* {
                   2298:                syscallarg(const sigset_t *) set;
                   2299:                syscallarg(siginfo_t *) info;
                   2300:                syscallarg(struct timespec *) timeout;
                   2301:        } */ *uap = v;
1.173     jdolecek 2302:        sigset_t *waitset, twaitset;
1.135     jdolecek 2303:        struct proc *p = l->l_proc;
                   2304:        int error, signum, s;
                   2305:        int timo = 0;
                   2306:        struct timeval tvstart;
                   2307:        struct timespec ts;
1.171     jdolecek 2308:        ksiginfo_t *ksi;
1.135     jdolecek 2309:
1.173     jdolecek 2310:        MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
                   2311:
                   2312:        if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t)))) {
                   2313:                FREE(waitset, M_TEMP);
1.135     jdolecek 2314:                return (error);
1.173     jdolecek 2315:        }
1.135     jdolecek 2316:
                   2317:        /*
                   2318:         * Silently ignore SA_CANTMASK signals. psignal1() would
                   2319:         * ignore SA_CANTMASK signals in waitset, we do this
                   2320:         * only for the below siglist check.
                   2321:         */
1.173     jdolecek 2322:        sigminusset(&sigcantmask, waitset);
1.135     jdolecek 2323:
                   2324:        /*
                   2325:         * First scan siglist and check if there is signal from
                   2326:         * our waitset already pending.
                   2327:         */
1.173     jdolecek 2328:        twaitset = *waitset;
1.135     jdolecek 2329:        __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
                   2330:        if ((signum = firstsig(&twaitset))) {
                   2331:                /* found pending signal */
                   2332:                sigdelset(&p->p_sigctx.ps_siglist, signum);
1.171     jdolecek 2333:                ksi = ksiginfo_get(p, signum);
                   2334:                if (!ksi) {
                   2335:                        /* No queued siginfo, manufacture one */
                   2336:                        ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
                   2337:                        KSI_INIT(ksi);
                   2338:                        ksi->ksi_info._signo = signum;
                   2339:                        ksi->ksi_info._code = SI_USER;
                   2340:                }
1.202     perry    2341:
1.135     jdolecek 2342:                goto sig;
                   2343:        }
                   2344:
                   2345:        /*
                   2346:         * Calculate timeout, if it was specified.
                   2347:         */
                   2348:        if (SCARG(uap, timeout)) {
                   2349:                uint64_t ms;
                   2350:
                   2351:                if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
                   2352:                        return (error);
                   2353:
                   2354:                ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
                   2355:                timo = mstohz(ms);
                   2356:                if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
                   2357:                        timo = 1;
                   2358:                if (timo <= 0)
                   2359:                        return (EAGAIN);
                   2360:
                   2361:                /*
                   2362:                 * Remember current mono_time, it would be used in
                   2363:                 * ECANCELED/ERESTART case.
                   2364:                 */
                   2365:                s = splclock();
                   2366:                tvstart = mono_time;
                   2367:                splx(s);
                   2368:        }
                   2369:
                   2370:        /*
1.173     jdolecek 2371:         * Setup ps_sigwait list. Pass pointer to malloced memory
                   2372:         * here; it's not possible to pass pointer to a structure
                   2373:         * on current process's stack, the current process might
                   2374:         * be swapped out at the time the signal would get delivered.
1.135     jdolecek 2375:         */
1.171     jdolecek 2376:        ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
                   2377:        p->p_sigctx.ps_sigwaited = ksi;
1.173     jdolecek 2378:        p->p_sigctx.ps_sigwait = waitset;
1.135     jdolecek 2379:
                   2380:        /*
                   2381:         * Wait for signal to arrive. We can either be woken up or
                   2382:         * time out.
                   2383:         */
                   2384:        error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
                   2385:
                   2386:        /*
1.171     jdolecek 2387:         * Need to find out if we woke as a result of lwp_wakeup()
                   2388:         * or a signal outside our wait set.
1.135     jdolecek 2389:         */
1.171     jdolecek 2390:        if (error == EINTR && p->p_sigctx.ps_sigwaited
                   2391:            && !firstsig(&p->p_sigctx.ps_siglist)) {
                   2392:                /* wakeup via _lwp_wakeup() */
                   2393:                error = ECANCELED;
                   2394:        } else if (!error && p->p_sigctx.ps_sigwaited) {
                   2395:                /* spurious wakeup - arrange for syscall restart */
                   2396:                error = ERESTART;
                   2397:                goto fail;
1.135     jdolecek 2398:        }
                   2399:
                   2400:        /*
1.171     jdolecek 2401:         * On error, clear sigwait indication. psignal1() clears it
1.135     jdolecek 2402:         * in !error case.
                   2403:         */
                   2404:        if (error) {
1.171     jdolecek 2405:                p->p_sigctx.ps_sigwaited = NULL;
1.135     jdolecek 2406:
                   2407:                /*
                   2408:                 * If the sleep was interrupted (either by signal or wakeup),
                   2409:                 * update the timeout and copyout new value back.
                   2410:                 * It would be used when the syscall would be restarted
                   2411:                 * or called again.
                   2412:                 */
                   2413:                if (timo && (error == ERESTART || error == ECANCELED)) {
                   2414:                        struct timeval tvnow, tvtimo;
                   2415:                        int err;
                   2416:
                   2417:                        s = splclock();
                   2418:                        tvnow = mono_time;
                   2419:                        splx(s);
                   2420:
                   2421:                        TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
                   2422:
                   2423:                        /* compute how much time has passed since start */
                   2424:                        timersub(&tvnow, &tvstart, &tvnow);
                   2425:                        /* substract passed time from timeout */
                   2426:                        timersub(&tvtimo, &tvnow, &tvtimo);
                   2427:
1.171     jdolecek 2428:                        if (tvtimo.tv_sec < 0) {
                   2429:                                error = EAGAIN;
                   2430:                                goto fail;
                   2431:                        }
                   2432:
1.135     jdolecek 2433:                        TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
                   2434:
                   2435:                        /* copy updated timeout to userland */
1.171     jdolecek 2436:                        if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) {
                   2437:                                error = err;
                   2438:                                goto fail;
                   2439:                        }
1.135     jdolecek 2440:                }
                   2441:
1.171     jdolecek 2442:                goto fail;
1.135     jdolecek 2443:        }
                   2444:
                   2445:        /*
                   2446:         * If a signal from the wait set arrived, copy it to userland.
1.171     jdolecek 2447:         * Copy only the used part of siginfo, the padding part is
                   2448:         * left unchanged (userland is not supposed to touch it anyway).
1.135     jdolecek 2449:         */
                   2450:  sig:
1.171     jdolecek 2451:        error = copyout(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
1.135     jdolecek 2452:
1.171     jdolecek 2453:  fail:
1.173     jdolecek 2454:        FREE(waitset, M_TEMP);
1.171     jdolecek 2455:        pool_put(&ksiginfo_pool, ksi);
                   2456:        p->p_sigctx.ps_sigwait = NULL;
1.135     jdolecek 2457:
1.171     jdolecek 2458:        return (error);
1.135     jdolecek 2459: }
1.108     jdolecek 2460:
                   2461: /*
                   2462:  * Returns true if signal is ignored or masked for passed process.
                   2463:  */
                   2464: int
1.112     lukem    2465: sigismasked(struct proc *p, int sig)
1.108     jdolecek 2466: {
1.112     lukem    2467:
1.117     enami    2468:        return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
                   2469:            sigismember(&p->p_sigctx.ps_sigmask, sig));
1.29      cgd      2470: }
1.126     jdolecek 2471:
                   2472: static int
                   2473: filt_sigattach(struct knote *kn)
                   2474: {
                   2475:        struct proc *p = curproc;
                   2476:
                   2477:        kn->kn_ptr.p_proc = p;
                   2478:        kn->kn_flags |= EV_CLEAR;               /* automatically set */
                   2479:
                   2480:        SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
                   2481:
                   2482:        return (0);
                   2483: }
                   2484:
                   2485: static void
                   2486: filt_sigdetach(struct knote *kn)
                   2487: {
                   2488:        struct proc *p = kn->kn_ptr.p_proc;
                   2489:
                   2490:        SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
                   2491: }
                   2492:
                   2493: /*
                   2494:  * signal knotes are shared with proc knotes, so we apply a mask to
                   2495:  * the hint in order to differentiate them from process hints.  This
                   2496:  * could be avoided by using a signal-specific knote list, but probably
                   2497:  * isn't worth the trouble.
                   2498:  */
                   2499: static int
                   2500: filt_signal(struct knote *kn, long hint)
                   2501: {
                   2502:
                   2503:        if (hint & NOTE_SIGNAL) {
                   2504:                hint &= ~NOTE_SIGNAL;
                   2505:
                   2506:                if (kn->kn_id == hint)
                   2507:                        kn->kn_data++;
                   2508:        }
                   2509:        return (kn->kn_data != 0);
                   2510: }
                   2511:
                   2512: const struct filterops sig_filtops = {
                   2513:        0, filt_sigattach, filt_sigdetach, filt_signal
                   2514: };

CVSweb <webmaster@jp.NetBSD.org>