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

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

CVSweb <webmaster@jp.NetBSD.org>