Annotation of Net2/kern/kern_sig.c, revision 1.1.1.4

1.1       root        1: /*
                      2:  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  *
1.1.1.4 ! root       33:  *     from: @(#)kern_sig.c    7.35 (Berkeley) 6/28/91
        !            34:  *     kern_sig.c,v 1.6.2.1 1993/08/20 08:37:03 cgd Exp
1.1       root       35:  */
                     36: 
                     37: #define        SIGPROP         /* include signal properties table */
                     38: #include "param.h"
                     39: #include "signalvar.h"
                     40: #include "resourcevar.h"
                     41: #include "namei.h"
                     42: #include "vnode.h"
                     43: #include "proc.h"
                     44: #include "systm.h"
                     45: #include "timeb.h"
                     46: #include "times.h"
                     47: #include "buf.h"
                     48: #include "acct.h"
                     49: #include "file.h"
                     50: #include "kernel.h"
                     51: #include "wait.h"
                     52: #include "ktrace.h"
                     53: 
                     54: #include "machine/cpu.h"
                     55: 
                     56: #include "vm/vm.h"
                     57: #include "kinfo_proc.h"
                     58: #include "user.h"              /* for coredump */
                     59: 
                     60: /*
                     61:  * Can process p, with pcred pc, send the signal signo to process q?
                     62:  */
                     63: #define CANSIGNAL(p, pc, q, signo) \
                     64:        ((pc)->pc_ucred->cr_uid == 0 || \
                     65:            (pc)->p_ruid == (q)->p_cred->p_ruid || \
                     66:            (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
                     67:            (pc)->p_ruid == (q)->p_ucred->cr_uid || \
                     68:            (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
                     69:            ((signo) == SIGCONT && (q)->p_session == (p)->p_session))
                     70: 
1.1.1.4 ! root       71: void stop __P((struct proc *p));
        !            72: 
        !            73: struct sigaction_args {
        !            74:        int     signo;
        !            75:        struct  sigaction *nsa;
        !            76:        struct  sigaction *osa;
        !            77: };
        !            78: 
1.1       root       79: /* ARGSUSED */
                     80: sigaction(p, uap, retval)
                     81:        struct proc *p;
1.1.1.4 ! root       82:        register struct sigaction_args *uap;
1.1       root       83:        int *retval;
                     84: {
                     85:        struct sigaction vec;
                     86:        register struct sigaction *sa;
                     87:        register struct sigacts *ps = p->p_sigacts;
                     88:        register int sig;
                     89:        int bit, error;
                     90: 
                     91:        sig = uap->signo;
                     92:        if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
                     93:                return (EINVAL);
                     94:        sa = &vec;
                     95:        if (uap->osa) {
                     96:                sa->sa_handler = ps->ps_sigact[sig];
                     97:                sa->sa_mask = ps->ps_catchmask[sig];
                     98:                bit = sigmask(sig);
                     99:                sa->sa_flags = 0;
                    100:                if ((ps->ps_sigonstack & bit) != 0)
                    101:                        sa->sa_flags |= SA_ONSTACK;
                    102:                if ((ps->ps_sigintr & bit) == 0)
                    103:                        sa->sa_flags |= SA_RESTART;
                    104:                if (p->p_flag & SNOCLDSTOP)
                    105:                        sa->sa_flags |= SA_NOCLDSTOP;
                    106:                if (error = copyout((caddr_t)sa, (caddr_t)uap->osa,
                    107:                    sizeof (vec)))
                    108:                        return (error);
                    109:        }
                    110:        if (uap->nsa) {
                    111:                if (error = copyin((caddr_t)uap->nsa, (caddr_t)sa,
                    112:                    sizeof (vec)))
                    113:                        return (error);
                    114:                setsigvec(p, sig, sa);
                    115:        }
                    116:        return (0);
                    117: }
                    118: 
                    119: setsigvec(p, sig, sa)
                    120:        register struct proc *p;
                    121:        int sig;
                    122:        register struct sigaction *sa;
                    123: {
                    124:        register struct sigacts *ps = p->p_sigacts;
                    125:        register int bit;
                    126: 
                    127:        bit = sigmask(sig);
                    128:        /*
                    129:         * Change setting atomically.
                    130:         */
                    131:        (void) splhigh();
                    132:        ps->ps_sigact[sig] = sa->sa_handler;
                    133:        ps->ps_catchmask[sig] = sa->sa_mask &~ sigcantmask;
                    134:        if ((sa->sa_flags & SA_RESTART) == 0)
                    135:                ps->ps_sigintr |= bit;
                    136:        else
                    137:                ps->ps_sigintr &= ~bit;
                    138:        if (sa->sa_flags & SA_ONSTACK)
                    139:                ps->ps_sigonstack |= bit;
                    140:        else
                    141:                ps->ps_sigonstack &= ~bit;
                    142:        if (sig == SIGCHLD) {
                    143:                if (sa->sa_flags & SA_NOCLDSTOP)
                    144:                        p->p_flag |= SNOCLDSTOP;
                    145:                else
                    146:                        p->p_flag &= ~SNOCLDSTOP;
                    147:        }
                    148:        /*
                    149:         * Set bit in p_sigignore for signals that are set to SIG_IGN,
                    150:         * and for signals set to SIG_DFL where the default is to ignore.
                    151:         * However, don't put SIGCONT in p_sigignore,
                    152:         * as we have to restart the process.
                    153:         */
                    154:        if (sa->sa_handler == SIG_IGN ||
                    155:            (sigprop[sig] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
                    156:                p->p_sig &= ~bit;               /* never to be seen again */
                    157:                if (sig != SIGCONT)
                    158:                        p->p_sigignore |= bit;  /* easier in psignal */
                    159:                p->p_sigcatch &= ~bit;
                    160:        } else {
                    161:                p->p_sigignore &= ~bit;
                    162:                if (sa->sa_handler == SIG_DFL)
                    163:                        p->p_sigcatch &= ~bit;
                    164:                else
                    165:                        p->p_sigcatch |= bit;
                    166:        }
                    167:        (void) spl0();
                    168: }
                    169: 
                    170: /*
                    171:  * Initialize signal state for process 0;
                    172:  * set to ignore signals that are ignored by default.
                    173:  */
                    174: void
                    175: siginit(p)
                    176:        struct proc *p;
                    177: {
                    178:        register int i;
                    179: 
                    180:        for (i = 0; i < NSIG; i++)
                    181:                if (sigprop[i] & SA_IGNORE && i != SIGCONT)
                    182:                        p->p_sigignore |= sigmask(i);
                    183: }
                    184: 
                    185: /*
                    186:  * Reset signals for an exec of the specified process.
                    187:  */
                    188: void
                    189: execsigs(p)
                    190:        register struct proc *p;
                    191: {
                    192:        register struct sigacts *ps = p->p_sigacts;
                    193:        register int nc, mask;
                    194: 
                    195:        /*
                    196:         * Reset caught signals.  Held signals remain held
                    197:         * through p_sigmask (unless they were caught,
                    198:         * and are now ignored by default).
                    199:         */
                    200:        while (p->p_sigcatch) {
                    201:                nc = ffs((long)p->p_sigcatch);
                    202:                mask = sigmask(nc);
                    203:                p->p_sigcatch &= ~mask;
                    204:                if (sigprop[nc] & SA_IGNORE) {
                    205:                        if (nc != SIGCONT)
                    206:                                p->p_sigignore |= mask;
                    207:                        p->p_sig &= ~mask;
                    208:                }
                    209:                ps->ps_sigact[nc] = SIG_DFL;
                    210:        }
                    211:        /*
                    212:         * Reset stack state to the user stack.
                    213:         * Clear set of signals caught on the signal stack.
                    214:         */
                    215:        ps->ps_onstack = 0;
                    216:        ps->ps_sigsp = 0;
                    217:        ps->ps_sigonstack = 0;
                    218: }
                    219: 
                    220: /*
                    221:  * Manipulate signal mask.
                    222:  * Note that we receive new mask, not pointer,
                    223:  * and return old mask as return value;
                    224:  * the library stub does the rest.
                    225:  */
1.1.1.4 ! root      226: struct sigprocmask_args {
        !           227:        int     how;
        !           228:        sigset_t mask;
        !           229: };
        !           230: 
1.1       root      231: sigprocmask(p, uap, retval)
                    232:        register struct proc *p;
1.1.1.4 ! root      233:        struct sigprocmask_args *uap;
1.1       root      234:        int *retval;
                    235: {
                    236:        int error = 0;
                    237: 
                    238:        *retval = p->p_sigmask;
                    239:        (void) splhigh();
                    240: 
                    241:        switch (uap->how) {
                    242:        case SIG_BLOCK:
                    243:                p->p_sigmask |= uap->mask &~ sigcantmask;
                    244:                break;
                    245: 
                    246:        case SIG_UNBLOCK:
                    247:                p->p_sigmask &= ~uap->mask;
                    248:                break;
                    249: 
                    250:        case SIG_SETMASK:
                    251:                p->p_sigmask = uap->mask &~ sigcantmask;
                    252:                break;
                    253:        
                    254:        default:
                    255:                error = EINVAL;
                    256:                break;
                    257:        }
                    258:        (void) spl0();
                    259:        return (error);
                    260: }
                    261: 
                    262: /* ARGSUSED */
                    263: sigpending(p, uap, retval)
                    264:        struct proc *p;
                    265:        void *uap;
                    266:        int *retval;
                    267: {
                    268: 
                    269:        *retval = p->p_sig;
                    270:        return (0);
                    271: }
                    272: 
                    273: #ifdef COMPAT_43
                    274: /*
                    275:  * Generalized interface signal handler, 4.3-compatible.
                    276:  */
1.1.1.4 ! root      277: struct osigvec_args {
        !           278:        int     signo;
        !           279:        struct  sigvec *nsv;
        !           280:        struct  sigvec *osv;
        !           281: };
        !           282: 
1.1       root      283: /* ARGSUSED */
                    284: osigvec(p, uap, retval)
                    285:        struct proc *p;
1.1.1.4 ! root      286:        register struct osigvec_args *uap;
1.1       root      287:        int *retval;
                    288: {
                    289:        struct sigvec vec;
                    290:        register struct sigacts *ps = p->p_sigacts;
                    291:        register struct sigvec *sv;
                    292:        register int sig;
                    293:        int bit, error;
                    294: 
                    295:        sig = uap->signo;
                    296:        if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
                    297:                return (EINVAL);
                    298:        sv = &vec;
                    299:        if (uap->osv) {
                    300:                *(sig_t *)&sv->sv_handler = ps->ps_sigact[sig];
                    301:                sv->sv_mask = ps->ps_catchmask[sig];
                    302:                bit = sigmask(sig);
                    303:                sv->sv_flags = 0;
                    304:                if ((ps->ps_sigonstack & bit) != 0)
                    305:                        sv->sv_flags |= SV_ONSTACK;
                    306:                if ((ps->ps_sigintr & bit) != 0)
                    307:                        sv->sv_flags |= SV_INTERRUPT;
                    308:                if (p->p_flag & SNOCLDSTOP)
                    309:                        sv->sv_flags |= SA_NOCLDSTOP;
                    310:                if (error = copyout((caddr_t)sv, (caddr_t)uap->osv,
                    311:                    sizeof (vec)))
                    312:                        return (error);
                    313:        }
                    314:        if (uap->nsv) {
                    315:                if (error = copyin((caddr_t)uap->nsv, (caddr_t)sv,
                    316:                    sizeof (vec)))
                    317:                        return (error);
                    318:                sv->sv_flags ^= SA_RESTART;     /* opposite of SV_INTERRUPT */
                    319:                setsigvec(p, sig, (struct sigaction *)sv);
                    320:        }
                    321:        return (0);
                    322: }
                    323: 
1.1.1.4 ! root      324: struct osigblock_args {
        !           325:        int     mask;
        !           326: };
        !           327: 
1.1       root      328: osigblock(p, uap, retval)
                    329:        register struct proc *p;
1.1.1.4 ! root      330:        struct osigblock_args *uap;
1.1       root      331:        int *retval;
                    332: {
                    333: 
                    334:        (void) splhigh();
                    335:        *retval = p->p_sigmask;
                    336:        p->p_sigmask |= uap->mask &~ sigcantmask;
                    337:        (void) spl0();
                    338:        return (0);
                    339: }
                    340: 
1.1.1.4 ! root      341: struct osigsetmask_args {
        !           342:        int     mask;
        !           343: };
        !           344: 
        !           345: int
1.1       root      346: osigsetmask(p, uap, retval)
                    347:        struct proc *p;
1.1.1.4 ! root      348:        struct osigsetmask_args *uap;
1.1       root      349:        int *retval;
                    350: {
                    351: 
                    352:        (void) splhigh();
                    353:        *retval = p->p_sigmask;
                    354:        p->p_sigmask = uap->mask &~ sigcantmask;
                    355:        (void) spl0();
                    356:        return (0);
                    357: }
                    358: #endif
                    359: 
                    360: /*
                    361:  * Suspend process until signal, providing mask to be set
                    362:  * in the meantime.  Note nonstandard calling convention:
                    363:  * libc stub passes mask, not pointer, to save a copyin.
                    364:  */
1.1.1.4 ! root      365: struct sigsuspend_args {
        !           366:        sigset_t mask;
        !           367: };
        !           368: 
1.1       root      369: /* ARGSUSED */
1.1.1.4 ! root      370: int
1.1       root      371: sigsuspend(p, uap, retval)
                    372:        register struct proc *p;
1.1.1.4 ! root      373:        struct sigsuspend_args *uap;
1.1       root      374:        int *retval;
                    375: {
                    376:        register struct sigacts *ps = p->p_sigacts;
                    377: 
                    378:        /*
                    379:         * When returning from sigpause, we want
                    380:         * the old mask to be restored after the
                    381:         * signal handler has finished.  Thus, we
                    382:         * save it here and mark the proc structure
                    383:         * to indicate this (should be in sigacts).
                    384:         */
                    385:        ps->ps_oldmask = p->p_sigmask;
                    386:        ps->ps_flags |= SA_OLDMASK;
                    387:        p->p_sigmask = uap->mask &~ sigcantmask;
                    388:        (void) tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0);
                    389:        /* always return EINTR rather than ERESTART... */
                    390:        return (EINTR);
                    391: }
                    392: 
1.1.1.4 ! root      393: struct sigstack_args {
        !           394:        struct  sigstack *nss;
        !           395:        struct  sigstack *oss;
        !           396: };
        !           397: 
1.1       root      398: /* ARGSUSED */
1.1.1.4 ! root      399: int
1.1       root      400: sigstack(p, uap, retval)
                    401:        struct proc *p;
1.1.1.4 ! root      402:        register struct sigstack_args *uap;
1.1       root      403:        int *retval;
                    404: {
                    405:        struct sigstack ss;
                    406:        int error = 0;
                    407: 
                    408:        if (uap->oss && (error = copyout((caddr_t)&p->p_sigacts->ps_sigstack,
                    409:            (caddr_t)uap->oss, sizeof (struct sigstack))))
                    410:                return (error);
                    411:        if (uap->nss && (error = copyin((caddr_t)uap->nss, (caddr_t)&ss,
                    412:            sizeof (ss))) == 0)
                    413:                p->p_sigacts->ps_sigstack = ss;
                    414:        return (error);
                    415: }
                    416: 
1.1.1.4 ! root      417: struct kill_args {
        !           418:        int     pid;
        !           419:        int     signo;
        !           420: };
        !           421: 
1.1       root      422: /* ARGSUSED */
1.1.1.4 ! root      423: int
1.1       root      424: kill(cp, uap, retval)
                    425:        register struct proc *cp;
1.1.1.4 ! root      426:        register struct kill_args *uap;
1.1       root      427:        int *retval;
                    428: {
                    429:        register struct proc *p;
                    430:        register struct pcred *pc = cp->p_cred;
                    431: 
                    432:        if ((unsigned) uap->signo >= NSIG)
                    433:                return (EINVAL);
                    434:        if (uap->pid > 0) {
                    435:                /* kill single process */
                    436:                p = pfind(uap->pid);
                    437:                if (p == 0)
                    438:                        return (ESRCH);
                    439:                if (!CANSIGNAL(cp, pc, p, uap->signo))
                    440:                        return (EPERM);
                    441:                if (uap->signo)
                    442:                        psignal(p, uap->signo);
                    443:                return (0);
                    444:        }
                    445:        switch (uap->pid) {
                    446:        case -1:                /* broadcast signal */
                    447:                return (killpg1(cp, uap->signo, 0, 1));
                    448:        case 0:                 /* signal own process group */
                    449:                return (killpg1(cp, uap->signo, 0, 0));
                    450:        default:                /* negative explicit process group */
                    451:                return (killpg1(cp, uap->signo, -uap->pid, 0));
                    452:        }
                    453:        /* NOTREACHED */
                    454: }
                    455: 
                    456: #ifdef COMPAT_43
1.1.1.4 ! root      457: struct okillpg_args {
        !           458:        int     pgid;
        !           459:        int     signo;
        !           460: };
        !           461: 
1.1       root      462: /* ARGSUSED */
                    463: okillpg(p, uap, retval)
                    464:        struct proc *p;
1.1.1.4 ! root      465:        register struct okillpg_args *uap;
1.1       root      466:        int *retval;
                    467: {
                    468: 
                    469:        if ((unsigned) uap->signo >= NSIG)
                    470:                return (EINVAL);
                    471:        return (killpg1(p, uap->signo, uap->pgid, 0));
                    472: }
                    473: #endif
                    474: 
                    475: /*
                    476:  * Common code for kill process group/broadcast kill.
                    477:  * cp is calling process.
                    478:  */
                    479: killpg1(cp, signo, pgid, all)
                    480:        register struct proc *cp;
                    481:        int signo, pgid, all;
                    482: {
                    483:        register struct proc *p;
                    484:        register struct pcred *pc = cp->p_cred;
                    485:        struct pgrp *pgrp;
                    486:        int nfound = 0;
                    487:        
                    488:        if (all)        
                    489:                /* 
                    490:                 * broadcast 
                    491:                 */
                    492:                for (p = allproc; p != NULL; p = p->p_nxt) {
                    493:                        if (p->p_pid <= 1 || p->p_flag&SSYS || 
                    494:                            p == cp || !CANSIGNAL(cp, pc, p, signo))
                    495:                                continue;
                    496:                        nfound++;
                    497:                        if (signo)
                    498:                                psignal(p, signo);
                    499:                }
                    500:        else {
                    501:                if (pgid == 0)          
                    502:                        /* 
                    503:                         * zero pgid means send to my process group.
                    504:                         */
                    505:                        pgrp = cp->p_pgrp;
                    506:                else {
                    507:                        pgrp = pgfind(pgid);
                    508:                        if (pgrp == NULL)
                    509:                                return (ESRCH);
                    510:                }
                    511:                for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
                    512:                        if (p->p_pid <= 1 || p->p_flag&SSYS ||
                    513:                            p->p_stat == SZOMB || !CANSIGNAL(cp, pc, p, signo))
                    514:                                continue;
                    515:                        nfound++;
                    516:                        if (signo)
                    517:                                psignal(p, signo);
                    518:                }
                    519:        }
                    520:        return (nfound ? 0 : ESRCH);
                    521: }
                    522: 
                    523: /*
                    524:  * Send the specified signal to
                    525:  * all processes with 'pgid' as
                    526:  * process group.
                    527:  */
                    528: void
                    529: gsignal(pgid, sig)
                    530:        int pgid, sig;
                    531: {
                    532:        struct pgrp *pgrp;
                    533: 
                    534:        if (pgid && (pgrp = pgfind(pgid)))
                    535:                pgsignal(pgrp, sig, 0);
                    536: }
                    537: 
                    538: /*
                    539:  * Send sig to every member of a process group.
                    540:  * If checktty is 1, limit to members which have a controlling
                    541:  * terminal.
                    542:  */
                    543: void
                    544: pgsignal(pgrp, sig, checkctty)
                    545:        struct pgrp *pgrp;
                    546:        int sig, checkctty;
                    547: {
                    548:        register struct proc *p;
                    549: 
                    550:        if (pgrp)
                    551:                for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
                    552:                        if (checkctty == 0 || p->p_flag&SCTTY)
                    553:                                psignal(p, sig);
                    554: }
                    555: 
                    556: /*
                    557:  * Send a signal caused by a trap to the current process.
                    558:  * If it will be caught immediately, deliver it with correct code.
                    559:  * Otherwise, post it normally.
                    560:  */
                    561: void
                    562: trapsignal(p, sig, code)
                    563:        struct proc *p;
                    564:        register int sig;
                    565:        unsigned code;
                    566: {
                    567:        register struct sigacts *ps = p->p_sigacts;
                    568:        int mask;
                    569: 
1.1.1.4 ! root      570:        if (p != curproc)
        !           571:                panic("trapsignal: p != curproc; EBOZO");
        !           572: 
1.1       root      573:        mask = sigmask(sig);
1.1.1.4 ! root      574:        if ((p->p_flag & STRC) == 0 && (p->p_sigcatch & mask) != 0 &&
        !           575:            (p->p_sigmask & mask) == 0) {
1.1       root      576:                p->p_stats->p_ru.ru_nsignals++;
                    577: #ifdef KTRACE
                    578:                if (KTRPOINT(p, KTR_PSIG))
                    579:                        ktrpsig(p->p_tracep, sig, ps->ps_sigact[sig], 
                    580:                                p->p_sigmask, code);
                    581: #endif
                    582:                sendsig(ps->ps_sigact[sig], sig, p->p_sigmask, code);
                    583:                p->p_sigmask |= ps->ps_catchmask[sig] | mask;
                    584:        } else {
                    585:                ps->ps_code = code;     /* XXX for core dump/debugger */
                    586:                psignal(p, sig);
                    587:        }
                    588: }
                    589: 
                    590: /*
                    591:  * Send the specified signal to the specified process.
                    592:  * If the signal has an action, the action is usually performed
                    593:  * by the target process rather than the caller; we simply add
                    594:  * the signal to the set of pending signals for the process.
                    595:  * Exceptions:
                    596:  *   o When a stop signal is sent to a sleeping process that takes the default
                    597:  *     action, the process is stopped without awakening it.
                    598:  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
                    599:  *     regardless of the signal action (eg, blocked or ignored).
                    600:  * Other ignored signals are discarded immediately.
                    601:  */
                    602: void
                    603: psignal(p, sig)
                    604:        register struct proc *p;
                    605:        register int sig;
                    606: {
                    607:        register int s, prop;
                    608:        register sig_t action;
                    609:        int mask;
                    610: 
                    611:        if ((unsigned)sig >= NSIG || sig == 0)
                    612:                panic("psignal sig");
                    613:        mask = sigmask(sig);
                    614:        prop = sigprop[sig];
                    615: 
                    616:        /*
                    617:         * If proc is traced, always give parent a chance.
                    618:         */
                    619:        if (p->p_flag & STRC)
                    620:                action = SIG_DFL;
                    621:        else {
                    622:                /*
                    623:                 * If the signal is being ignored,
                    624:                 * then we forget about it immediately.
                    625:                 * (Note: we don't set SIGCONT in p_sigignore,
                    626:                 * and if it is set to SIG_IGN,
                    627:                 * action will be SIG_DFL here.)
                    628:                 */
                    629:                if (p->p_sigignore & mask)
                    630:                        return;
                    631:                if (p->p_sigmask & mask)
                    632:                        action = SIG_HOLD;
                    633:                else if (p->p_sigcatch & mask)
                    634:                        action = SIG_CATCH;
                    635:                else
                    636:                        action = SIG_DFL;
                    637:        }
                    638: 
                    639:        if (p->p_nice > NZERO && (sig == SIGKILL ||
                    640:            sig == SIGTERM && (p->p_flag&STRC || action != SIG_DFL)))
                    641:                p->p_nice = NZERO;
                    642: 
                    643:        if (prop & SA_CONT)
                    644:                p->p_sig &= ~stopsigmask;
                    645: 
                    646:        if (prop & SA_STOP) {
                    647:                /*
                    648:                 * If sending a tty stop signal to a member of an orphaned
                    649:                 * process group, discard the signal here if the action
                    650:                 * is default; don't stop the process below if sleeping,
                    651:                 * and don't clear any pending SIGCONT.
                    652:                 */
                    653:                if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0 &&
                    654:                    action == SIG_DFL)
                    655:                        return;
                    656:                p->p_sig &= ~contsigmask;
                    657:        }
                    658:        p->p_sig |= mask;
                    659: 
                    660:        /*
                    661:         * Defer further processing for signals which are held,
                    662:         * except that stopped processes must be continued by SIGCONT.
                    663:         */
                    664:        if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
                    665:                return;
                    666:        s = splhigh();
                    667:        switch (p->p_stat) {
                    668: 
                    669:        case SSLEEP:
                    670:                /*
                    671:                 * If process is sleeping uninterruptibly
                    672:                 * we can't interrupt the sleep... the signal will
                    673:                 * be noticed when the process returns through
                    674:                 * trap() or syscall().
                    675:                 */
                    676:                if ((p->p_flag & SSINTR) == 0)
                    677:                        goto out;
                    678:                /*
                    679:                 * Process is sleeping and traced... make it runnable
                    680:                 * so it can discover the signal in issig() and stop
                    681:                 * for the parent.
                    682:                 */
                    683:                if (p->p_flag&STRC)
                    684:                        goto run;
                    685:                /*
                    686:                 * When a sleeping process receives a stop
                    687:                 * signal, process immediately if possible.
                    688:                 * All other (caught or default) signals
                    689:                 * cause the process to run.
                    690:                 */
                    691:                if (prop & SA_STOP) {
                    692:                        if (action != SIG_DFL)
                    693:                                goto runfast;
                    694:                        /*
                    695:                         * If a child holding parent blocked,
                    696:                         * stopping could cause deadlock.
                    697:                         */
                    698:                        if (p->p_flag&SPPWAIT)
                    699:                                goto out;
                    700:                        p->p_sig &= ~mask;
                    701:                        p->p_xstat = sig;
                    702:                        if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
                    703:                                psignal(p->p_pptr, SIGCHLD);
                    704:                        stop(p);
                    705:                        goto out;
                    706:                } else
                    707:                        goto runfast;
                    708:                /*NOTREACHED*/
                    709: 
                    710:        case SSTOP:
                    711:                /*
                    712:                 * If traced process is already stopped,
                    713:                 * then no further action is necessary.
                    714:                 */
                    715:                if (p->p_flag&STRC)
                    716:                        goto out;
                    717: 
                    718:                /*
                    719:                 * Kill signal always sets processes running.
                    720:                 */
                    721:                if (sig == SIGKILL)
                    722:                        goto runfast;
                    723: 
                    724:                if (prop & SA_CONT) {
                    725:                        /*
                    726:                         * If SIGCONT is default (or ignored), we continue
                    727:                         * the process but don't leave the signal in p_sig,
                    728:                         * as it has no further action.  If SIGCONT is held,
                    729:                         * continue the process and leave the signal in p_sig.
                    730:                         * If the process catches SIGCONT, let it handle
                    731:                         * the signal itself.  If it isn't waiting on
                    732:                         * an event, then it goes back to run state.
                    733:                         * Otherwise, process goes back to sleep state.
                    734:                         */
                    735:                        if (action == SIG_DFL)
                    736:                                p->p_sig &= ~mask;
                    737:                        if (action == SIG_CATCH)
                    738:                                goto runfast;
                    739:                        if (p->p_wchan == 0)
                    740:                                goto run;
                    741:                        p->p_stat = SSLEEP;
                    742:                        goto out;
                    743:                }
                    744: 
                    745:                if (prop & SA_STOP) {
                    746:                        /*
                    747:                         * Already stopped, don't need to stop again.
                    748:                         * (If we did the shell could get confused.)
                    749:                         */
                    750:                        p->p_sig &= ~mask;              /* take it away */
                    751:                        goto out;
                    752:                }
                    753: 
                    754:                /*
                    755:                 * If process is sleeping interruptibly, then
                    756:                 * simulate a wakeup so that when it is continued,
                    757:                 * it will be made runnable and can look at the signal.
                    758:                 * But don't setrun the process, leave it stopped.
                    759:                 */
                    760:                if (p->p_wchan && p->p_flag & SSINTR)
                    761:                        unsleep(p);
                    762:                goto out;
                    763: 
                    764:        default:
                    765:                /*
                    766:                 * SRUN, SIDL, SZOMB do nothing with the signal,
                    767:                 * other than kicking ourselves if we are running.
                    768:                 * It will either never be noticed, or noticed very soon.
                    769:                 */
                    770:                if (p == curproc)
                    771:                        signotify(p);
                    772:                goto out;
                    773:        }
                    774:        /*NOTREACHED*/
                    775: 
                    776: runfast:
                    777:        /*
                    778:         * Raise priority to at least PUSER.
                    779:         */
                    780:        if (p->p_pri > PUSER)
                    781:                p->p_pri = PUSER;
                    782: run:
                    783:        setrun(p);
                    784: out:
                    785:        splx(s);
                    786: }
                    787: 
                    788: /*
                    789:  * If the current process has a signal to process (should be caught
                    790:  * or cause termination, should interrupt current syscall),
                    791:  * return the signal number.  Stop signals with default action
                    792:  * are processed immediately, then cleared; they aren't returned.
                    793:  * This is checked after each entry to the system for a syscall
                    794:  * or trap (though this can usually be done without actually calling
                    795:  * issig by checking the pending signal masks in the CURSIG macro.)
                    796:  * The normal call sequence is
                    797:  *
                    798:  *     while (sig = CURSIG(curproc))
                    799:  *             psig(sig);
                    800:  */
1.1.1.4 ! root      801: int
1.1       root      802: issig(p)
                    803:        register struct proc *p;
                    804: {
                    805:        register int sig, mask, prop;
                    806: 
                    807:        for (;;) {
                    808:                mask = p->p_sig &~ p->p_sigmask;
                    809:                if (p->p_flag&SPPWAIT)
                    810:                        mask &= ~stopsigmask;
                    811:                if (mask == 0)          /* no signal to send */
                    812:                        return (0);
                    813:                sig = ffs((long)mask);
                    814:                mask = sigmask(sig);
                    815:                prop = sigprop[sig];
                    816:                /*
                    817:                 * We should see pending but ignored signals
                    818:                 * only if STRC was on when they were posted.
                    819:                 */
                    820:                if (mask & p->p_sigignore && (p->p_flag&STRC) == 0) {
                    821:                        p->p_sig &= ~mask;
                    822:                        continue;
                    823:                }
                    824:                if (p->p_flag&STRC && (p->p_flag&SPPWAIT) == 0) {
                    825:                        /*
                    826:                         * If traced, always stop, and stay
                    827:                         * stopped until released by the parent.
                    828:                         */
                    829:                        p->p_xstat = sig;
                    830:                        psignal(p->p_pptr, SIGCHLD);
                    831:                        do {
                    832:                                stop(p);
                    833:                                swtch();
                    834:                        } while (!procxmt(p) && p->p_flag&STRC);
                    835: 
                    836:                        /*
                    837:                         * If the traced bit got turned off,
                    838:                         * go back up to the top to rescan signals.
                    839:                         * This ensures that p_sig* and ps_sigact
                    840:                         * are consistent.
                    841:                         */
                    842:                        if ((p->p_flag&STRC) == 0)
                    843:                                continue;
                    844: 
                    845:                        /*
                    846:                         * If parent wants us to take the signal,
                    847:                         * then it will leave it in p->p_xstat;
                    848:                         * otherwise we just look for signals again.
                    849:                         */
                    850:                        p->p_sig &= ~mask;      /* clear the old signal */
                    851:                        sig = p->p_xstat;
                    852:                        if (sig == 0)
                    853:                                continue;
                    854: 
                    855:                        /*
                    856:                         * Put the new signal into p_sig.
                    857:                         * If signal is being masked,
                    858:                         * look for other signals.
                    859:                         */
                    860:                        mask = sigmask(sig);
                    861:                        p->p_sig |= mask;
                    862:                        if (p->p_sigmask & mask)
                    863:                                continue;
                    864:                }
                    865: 
                    866:                /*
                    867:                 * Decide whether the signal should be returned.
                    868:                 * Return the signal's number, or fall through
                    869:                 * to clear it from the pending mask.
                    870:                 */
                    871:                switch ((int)p->p_sigacts->ps_sigact[sig]) {
                    872: 
                    873:                case SIG_DFL:
                    874:                        /*
                    875:                         * Don't take default actions on system processes.
                    876:                         */
                    877:                        if (p->p_pid <= 1)
                    878:                                break;          /* == ignore */
                    879:                        /*
                    880:                         * If there is a pending stop signal to process
                    881:                         * with default action, stop here,
                    882:                         * then clear the signal.  However,
                    883:                         * if process is member of an orphaned
                    884:                         * process group, ignore tty stop signals.
                    885:                         */
                    886:                        if (prop & SA_STOP) {
                    887:                                if (p->p_flag&STRC ||
                    888:                                    (p->p_pgrp->pg_jobc == 0 &&
                    889:                                    prop & SA_TTYSTOP))
                    890:                                        break;  /* == ignore */
                    891:                                p->p_xstat = sig;
                    892:                                stop(p);
                    893:                                if ((p->p_pptr->p_flag & SNOCLDSTOP) == 0)
                    894:                                        psignal(p->p_pptr, SIGCHLD);
                    895:                                swtch();
                    896:                                break;
                    897:                        } else if (prop & SA_IGNORE) {
                    898:                                /*
                    899:                                 * Except for SIGCONT, shouldn't get here.
                    900:                                 * Default action is to ignore; drop it.
                    901:                                 */
                    902:                                break;          /* == ignore */
                    903:                        } else
                    904:                                return (sig);
                    905:                        /*NOTREACHED*/
                    906: 
                    907:                case SIG_IGN:
                    908:                        /*
                    909:                         * Masking above should prevent us ever trying
                    910:                         * to take action on an ignored signal other
                    911:                         * than SIGCONT, unless process is traced.
                    912:                         */
                    913:                        if ((prop & SA_CONT) == 0 && (p->p_flag&STRC) == 0)
                    914:                                printf("issig\n");
                    915:                        break;          /* == ignore */
                    916: 
                    917:                default:
                    918:                        /*
                    919:                         * This signal has an action, let
                    920:                         * psig process it.
                    921:                         */
                    922:                        return (sig);
                    923:                }
                    924:                p->p_sig &= ~mask;              /* take the signal! */
                    925:        }
                    926:        /* NOTREACHED */
                    927: }
                    928: 
                    929: /*
                    930:  * Put the argument process into the stopped
                    931:  * state and notify the parent via wakeup.
                    932:  * Signals are handled elsewhere.
                    933:  * The process must not be on the run queue.
                    934:  */
1.1.1.4 ! root      935: void
1.1       root      936: stop(p)
                    937:        register struct proc *p;
                    938: {
                    939: 
                    940:        p->p_stat = SSTOP;
                    941:        p->p_flag &= ~SWTED;
                    942:        wakeup((caddr_t)p->p_pptr);
                    943: }
                    944: 
                    945: /*
                    946:  * Take the action for the specified signal
                    947:  * from the current set of pending signals.
                    948:  */
                    949: void
                    950: psig(sig)
                    951:        register int sig;
                    952: {
                    953:        register struct proc *p = curproc;
                    954:        register struct sigacts *ps = p->p_sigacts;
                    955:        register sig_t action;
                    956:        int mask, returnmask;
                    957: 
                    958: #ifdef DIAGNOSTIC
                    959:        if (sig == 0)
                    960:                panic("psig");
                    961: #endif
                    962:        mask = sigmask(sig);
                    963:        p->p_sig &= ~mask;
                    964:        action = ps->ps_sigact[sig];
                    965: #ifdef KTRACE
                    966:        if (KTRPOINT(p, KTR_PSIG))
                    967:                ktrpsig(p->p_tracep, sig, action, ps->ps_flags & SA_OLDMASK ?
                    968:                    ps->ps_oldmask : p->p_sigmask, 0);
                    969: #endif
                    970:        if (action == SIG_DFL) {
                    971:                /*
                    972:                 * Default action, where the default is to kill
                    973:                 * the process.  (Other cases were ignored above.)
                    974:                 */
                    975:                sigexit(p, sig);
                    976:                /* NOTREACHED */
                    977:        } else {
                    978:                /*
                    979:                 * If we get here, the signal must be caught.
                    980:                 */
                    981: #ifdef DIAGNOSTIC
                    982:                if (action == SIG_IGN || (p->p_sigmask & mask))
                    983:                        panic("psig action");
                    984: #endif
                    985:                /*
                    986:                 * Set the new mask value and also defer further
                    987:                 * occurences of this signal.
                    988:                 *
                    989:                 * Special case: user has done a sigpause.  Here the
                    990:                 * current mask is not of interest, but rather the
                    991:                 * mask from before the sigpause is what we want
                    992:                 * restored after the signal processing is completed.
                    993:                 */
                    994:                (void) splhigh();
                    995:                if (ps->ps_flags & SA_OLDMASK) {
                    996:                        returnmask = ps->ps_oldmask;
                    997:                        ps->ps_flags &= ~SA_OLDMASK;
                    998:                } else
                    999:                        returnmask = p->p_sigmask;
                   1000:                p->p_sigmask |= ps->ps_catchmask[sig] | mask;
                   1001:                (void) spl0();
                   1002:                p->p_stats->p_ru.ru_nsignals++;
                   1003:                sendsig(action, sig, returnmask, 0);
                   1004:        }
                   1005: }
                   1006: 
                   1007: /*
                   1008:  * Force the current process to exit with the specified
                   1009:  * signal, dumping core if appropriate.  We bypass the normal
                   1010:  * tests for masked and caught signals, allowing unrecoverable
                   1011:  * failures to terminate the process without changing signal state.
                   1012:  * Mark the accounting record with the signal termination.
                   1013:  * If dumping core, save the signal number for the debugger.
1.1.1.4 ! root     1014:  * Calls kexit and does not return.
1.1       root     1015:  */
1.1.1.4 ! root     1016: int
1.1       root     1017: sigexit(p, sig)
                   1018:        register struct proc *p;
                   1019:        int sig;
                   1020: {
                   1021: 
                   1022:        p->p_acflag |= AXSIG;
                   1023:        if (sigprop[sig] & SA_CORE) {
                   1024:                p->p_sigacts->ps_sig = sig;
                   1025:                if (coredump(p) == 0)
                   1026:                        sig |= WCOREFLAG;
                   1027:        }
1.1.1.4 ! root     1028:        kexit(p, W_EXITCODE(0, sig));
1.1       root     1029:        /* NOTREACHED */
                   1030: }
                   1031: 
                   1032: /*
                   1033:  * Create a core dump.
                   1034:  * The file name is "core.progname".
                   1035:  * Core dumps are not created if the process is setuid.
                   1036:  */
1.1.1.4 ! root     1037: int
1.1       root     1038: coredump(p)
                   1039:        register struct proc *p;
                   1040: {
                   1041:        register struct vnode *vp;
                   1042:        register struct pcred *pcred = p->p_cred;
                   1043:        register struct ucred *cred = pcred->pc_ucred;
                   1044:        register struct vmspace *vm = p->p_vmspace;
                   1045:        struct vattr vattr;
                   1046:        int error, error1;
                   1047:        struct nameidata nd;
                   1048:        char name[MAXCOMLEN+6]; /* core.progname */
                   1049: 
                   1050:        if (pcred->p_svuid != pcred->p_ruid ||
                   1051:            pcred->p_svgid != pcred->p_rgid)
                   1052:                return (EFAULT);
                   1053:        if (ctob(UPAGES + vm->vm_dsize + vm->vm_ssize) >=
                   1054:            p->p_rlimit[RLIMIT_CORE].rlim_cur)
                   1055:                return (EFAULT);
                   1056:        sprintf(name, "core.%s", p->p_comm);
                   1057:        nd.ni_dirp = name;
                   1058:        nd.ni_segflg = UIO_SYSSPACE;
                   1059:        if (error = vn_open(&nd, p, O_CREAT|FWRITE, 0644))
                   1060:                return (error);
                   1061:        vp = nd.ni_vp;
                   1062:        if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred, p) ||
                   1063:            vattr.va_nlink != 1) {
                   1064:                error = EFAULT;
                   1065:                goto out;
                   1066:        }
                   1067:        VATTR_NULL(&vattr);
                   1068:        vattr.va_size = 0;
                   1069:        VOP_SETATTR(vp, &vattr, cred, p);
                   1070:        p->p_acflag |= ACORE;
                   1071:        bcopy(p, &p->p_addr->u_kproc.kp_proc, sizeof(struct proc));
                   1072:        fill_eproc(p, &p->p_addr->u_kproc.kp_eproc);
                   1073: #ifdef HPUXCOMPAT
                   1074:        /*
                   1075:         * BLETCH!  If we loaded from an HPUX format binary file
                   1076:         * we have to dump an HPUX style user struct so that the
                   1077:         * HPUX debuggers can grok it.
                   1078:         */
                   1079:        if (p->p_addr->u_pcb.pcb_flags & PCB_HPUXBIN)
                   1080:                error = hpuxdumpu(vp, cred);
                   1081:        else
                   1082: #endif
                   1083:        error = vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
                   1084:            (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) NULL,
                   1085:            p);
                   1086:        if (error == 0)
                   1087:                error = vn_rdwr(UIO_WRITE, vp, vm->vm_daddr,
                   1088:                    (int)ctob(vm->vm_dsize), (off_t)ctob(UPAGES), UIO_USERSPACE,
                   1089:                    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
                   1090:        if (error == 0)
                   1091:                error = vn_rdwr(UIO_WRITE, vp,
1.1.1.4 ! root     1092:                    (caddr_t) trunc_page(USRSTACK - ctob(vm->vm_ssize)),
1.1       root     1093:                    round_page(ctob(vm->vm_ssize)),
                   1094:                    (off_t)ctob(UPAGES) + ctob(vm->vm_dsize), UIO_USERSPACE,
                   1095:                    IO_NODELOCKED|IO_UNIT, cred, (int *) NULL, p);
                   1096: out:
                   1097:        VOP_UNLOCK(vp);
                   1098:        error1 = vn_close(vp, FWRITE, cred, p);
                   1099:        if (error == 0)
                   1100:                error = error1;
                   1101:        return (error);
                   1102: }
                   1103: 
                   1104: /*
                   1105:  * Nonexistent system call-- signal process (may want to handle it).
                   1106:  * Flag error in case process won't see signal immediately (blocked or ignored).
                   1107:  */
                   1108: /* ARGSUSED */
1.1.1.4 ! root     1109: int
1.1       root     1110: nosys(p, args, retval)
                   1111:        struct proc *p;
                   1112:        void *args;
                   1113:        int *retval;
                   1114: {
                   1115: 
                   1116:        psignal(p, SIGSYS);
                   1117:        return (EINVAL);
                   1118: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.