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

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

CVSweb <webmaster@jp.NetBSD.org>