Annotation of 43BSDReno/sys/kern/kern_sig.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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