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

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

CVSweb <webmaster@jp.NetBSD.org>