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

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

CVSweb <webmaster@jp.NetBSD.org>