Annotation of Net2/kern/kern_exit.c, revision 1.1.1.2

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:  *
                     33:  *     @(#)kern_exit.c 7.35 (Berkeley) 6/27/91
                     34:  */
                     35: 
                     36: #include "param.h"
                     37: #include "systm.h"
                     38: #include "ioctl.h"
                     39: #include "tty.h"
                     40: #include "time.h"
                     41: #include "resource.h"
                     42: #include "kernel.h"
                     43: #include "proc.h"
                     44: #include "buf.h"
                     45: #include "wait.h"
                     46: #include "file.h"
                     47: #include "vnode.h"
                     48: #include "syslog.h"
                     49: #include "malloc.h"
                     50: #include "resourcevar.h"
                     51: 
                     52: #include "machine/cpu.h"
                     53: #ifdef COMPAT_43
                     54: #include "machine/reg.h"
                     55: #include "machine/psl.h"
                     56: #endif
                     57: 
                     58: #include "vm/vm.h"
                     59: #include "vm/vm_kern.h"
                     60: 
                     61: /*
                     62:  * Exit system call: pass back caller's arg
                     63:  */
                     64: /* ARGSUSED */
                     65: rexit(p, uap, retval)
                     66:        struct proc *p;
                     67:        struct args {
                     68:                int     rval;
                     69:        } *uap;
                     70:        int *retval;
                     71: {
                     72: 
                     73:        exit(p, W_EXITCODE(uap->rval, 0));
                     74:        /* NOTREACHED */
                     75: }
                     76: 
                     77: /*
                     78:  * Exit: deallocate address space and other resources,
                     79:  * change proc state to zombie, and unlink proc from allproc
                     80:  * and parent's lists.  Save exit status and rusage for wait().
                     81:  * Check for child processes and orphan them.
                     82:  */
                     83: exit(p, rv)
                     84:        register struct proc *p;
                     85:        int rv;
                     86: {
                     87:        register struct proc *q, *nq;
                     88:        register struct proc **pp;
                     89:        int s;
                     90: 
                     91: #ifdef PGINPROF
                     92:        vmsizmon();
                     93: #endif
                     94:        MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage),
                     95:                M_ZOMBIE, M_WAITOK);
                     96:        /*
                     97:         * If parent is waiting for us to exit or exec,
                     98:         * SPPWAIT is set; we will wakeup the parent below.
                     99:         */
                    100:        p->p_flag &= ~(STRC|SPPWAIT);
                    101:        p->p_flag |= SWEXIT;
                    102:        p->p_sigignore = ~0;
                    103:        p->p_sig = 0;
                    104:        untimeout(realitexpire, (caddr_t)p);
                    105: 
                    106:        /*
                    107:         * Close open files and release open-file table.
                    108:         * This may block!
                    109:         */
                    110:        fdfree(p);
                    111: 
                    112:        /* The next two chunks should probably be moved to vmspace_exit. */
                    113: #ifdef SYSVSHM
                    114:        if (p->p_vmspace->vm_shm)
                    115:                shmexit(p);
                    116: #endif
                    117:        /*
                    118:         * Release user portion of address space.
                    119:         * This releases references to vnodes,
                    120:         * which could cause I/O if the file has been unlinked.
                    121:         * Need to do this early enough that we can still sleep.
                    122:         * Can't free the entire vmspace as the kernel stack
                    123:         * may be mapped within that space also.
                    124:         */
                    125:        if (p->p_vmspace->vm_refcnt == 1)
                    126:                (void) vm_map_remove(&p->p_vmspace->vm_map, VM_MIN_ADDRESS,
                    127:                    VM_MAXUSER_ADDRESS);
                    128: 
                    129:        if (p->p_pid == 1)
                    130:                panic("init died");
                    131:        if (SESS_LEADER(p)) {
                    132:                register struct session *sp = p->p_session;
                    133: 
                    134:                if (sp->s_ttyvp) {
                    135:                        /*
                    136:                         * Controlling process.
                    137:                         * Signal foreground pgrp,
                    138:                         * drain controlling terminal
                    139:                         * and revoke access to controlling terminal.
                    140:                         */
                    141:                        if (sp->s_ttyp->t_session == sp) {
                    142:                                if (sp->s_ttyp->t_pgrp)
                    143:                                        pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
                    144:                                (void) ttywait(sp->s_ttyp);
                    145:                                vgoneall(sp->s_ttyvp);
                    146:                        }
                    147:                        vrele(sp->s_ttyvp);
                    148:                        sp->s_ttyvp = NULL;
                    149:                        /*
                    150:                         * s_ttyp is not zero'd; we use this to indicate
                    151:                         * that the session once had a controlling terminal.
                    152:                         * (for logging and informational purposes)
                    153:                         */
                    154:                }
                    155:                sp->s_leader = NULL;
                    156:        }
                    157:        fixjobc(p, p->p_pgrp, 0);
                    158:        p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
                    159: #ifdef KTRACE
                    160:        /* 
                    161:         * release trace file
                    162:         */
                    163:        if (p->p_tracep)
                    164:                vrele(p->p_tracep);
                    165: #endif
                    166: 
1.1.1.2 ! root      167:        /* current process does not exist, as far as other parts of the
        !           168:         * system (clock) is concerned, since parts of it might not be
        !           169:         * there anymore */
        !           170:        curproc = NULL;
        !           171: 
        !           172:        if (--p->p_limit->p_refcnt == 0) {
        !           173:                FREE(p->p_limit, M_SUBPROC);
        !           174:                p->p_limit = (struct plimit *) -1;
        !           175:        }
        !           176: 
1.1       root      177:        /*
                    178:         * Remove proc from allproc queue and pidhash chain.
                    179:         * Place onto zombproc.  Unlink from parent's child list.
                    180:         */
                    181:        if (*p->p_prev = p->p_nxt)
                    182:                p->p_nxt->p_prev = p->p_prev;
                    183:        if (p->p_nxt = zombproc)
                    184:                p->p_nxt->p_prev = &p->p_nxt;
                    185:        p->p_prev = &zombproc;
                    186:        zombproc = p;
                    187:        p->p_stat = SZOMB;
                    188:        for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash)
                    189:                if (*pp == p) {
                    190:                        *pp = p->p_hash;
                    191:                        goto done;
                    192:                }
                    193:        panic("exit");
                    194: done:
                    195: 
                    196:        if (p->p_cptr)          /* only need this if any child is S_ZOMB */
                    197:                wakeup((caddr_t) initproc);
                    198:        for (q = p->p_cptr; q != NULL; q = nq) {
                    199:                nq = q->p_osptr;
                    200:                if (nq != NULL)
                    201:                        nq->p_ysptr = NULL;
                    202:                if (initproc->p_cptr)
                    203:                        initproc->p_cptr->p_ysptr = q;
                    204:                q->p_osptr = initproc->p_cptr;
                    205:                q->p_ysptr = NULL;
                    206:                initproc->p_cptr = q;
                    207: 
                    208:                q->p_pptr = initproc;
                    209:                /*
                    210:                 * Traced processes are killed
                    211:                 * since their existence means someone is screwing up.
                    212:                 */
                    213:                if (q->p_flag&STRC) {
                    214:                        q->p_flag &= ~STRC;
                    215:                        psignal(q, SIGKILL);
                    216:                }
                    217:        }
                    218:        p->p_cptr = NULL;
                    219: 
                    220:        /*
                    221:         * Save exit status and final rusage info,
                    222:         * adding in child rusage info and self times.
                    223:         */
                    224:        p->p_xstat = rv;
                    225:        *p->p_ru = p->p_stats->p_ru;
                    226:        p->p_ru->ru_stime = p->p_stime;
                    227:        p->p_ru->ru_utime = p->p_utime;
                    228:        ruadd(p->p_ru, &p->p_stats->p_cru);
                    229: 
                    230:        /*
                    231:         * Notify parent that we're gone.
                    232:         */
                    233:        psignal(p->p_pptr, SIGCHLD);
                    234:        wakeup((caddr_t)p->p_pptr);
                    235: #if defined(tahoe)
                    236:        /* move this to cpu_exit */
                    237:        p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL;
                    238: #endif
                    239:        /*
                    240:         * Finally, call machine-dependent code to release the remaining
                    241:         * resources including address space, the kernel stack and pcb.
                    242:         * The address space is released by "vmspace_free(p->p_vmspace)";
                    243:         * This is machine-dependent, as we may have to change stacks
                    244:         * or ensure that the current one isn't reallocated before we
                    245:         * finish.  cpu_exit will end with a call to swtch(), finishing
                    246:         * our execution (pun intended).
                    247:         */
                    248:        cpu_exit(p);
                    249:        /* NOTREACHED */
                    250: }
                    251: 
                    252: #ifdef COMPAT_43
                    253: owait(p, uap, retval)
                    254:        struct proc *p;
                    255:        register struct args {
                    256:                int     pid;
                    257:                int     *status;
                    258:                int     options;
                    259:                struct  rusage *rusage;
                    260:                int     compat;
                    261:        } *uap;
                    262:        int *retval;
                    263: {
                    264: 
1.1.1.2 ! root      265:        uap->options = 0;
        !           266:        uap->rusage = 0;
1.1       root      267:        uap->pid = WAIT_ANY;
                    268:        uap->status = 0;
                    269:        uap->compat = 1;
                    270:        return (wait1(p, uap, retval));
                    271: }
                    272: 
                    273: wait4(p, uap, retval)
                    274:        struct proc *p;
                    275:        struct args {
                    276:                int     pid;
                    277:                int     *status;
                    278:                int     options;
                    279:                struct  rusage *rusage;
                    280:                int     compat;
                    281:        } *uap;
                    282:        int *retval;
                    283: {
                    284: 
                    285:        uap->compat = 0;
                    286:        return (wait1(p, uap, retval));
                    287: }
                    288: #else
                    289: #define        wait1   wait4
                    290: #endif
                    291: 
                    292: /*
                    293:  * Wait: check child processes to see if any have exited,
                    294:  * stopped under trace, or (optionally) stopped by a signal.
                    295:  * Pass back status and deallocate exited child's proc structure.
                    296:  */
                    297: wait1(q, uap, retval)
                    298:        register struct proc *q;
                    299:        register struct args {
                    300:                int     pid;
                    301:                int     *status;
                    302:                int     options;
                    303:                struct  rusage *rusage;
                    304: #ifdef COMPAT_43
                    305:                int compat;
                    306: #endif
                    307:        } *uap;
                    308:        int retval[];
                    309: {
                    310:        register int nfound;
                    311:        register struct proc *p;
                    312:        int status, error;
                    313: 
                    314:        if (uap->pid == 0)
                    315:                uap->pid = -q->p_pgid;
                    316: #ifdef notyet
                    317:        if (uap->options &~ (WUNTRACED|WNOHANG))
                    318:                return (EINVAL);
                    319: #endif
                    320: loop:
                    321:        nfound = 0;
                    322:        for (p = q->p_cptr; p; p = p->p_osptr) {
                    323:                if (uap->pid != WAIT_ANY &&
                    324:                    p->p_pid != uap->pid && p->p_pgid != -uap->pid)
                    325:                        continue;
                    326:                nfound++;
                    327:                if (p->p_stat == SZOMB) {
                    328:                        retval[0] = p->p_pid;
                    329: #ifdef COMPAT_43
                    330:                        if (uap->compat)
                    331:                                retval[1] = p->p_xstat;
                    332:                        else
                    333: #endif
                    334:                        if (uap->status) {
                    335:                                status = p->p_xstat;    /* convert to int */
                    336:                                if (error = copyout((caddr_t)&status,
                    337:                                    (caddr_t)uap->status, sizeof(status)))
                    338:                                        return (error);
                    339:                        }
                    340:                        if (uap->rusage && (error = copyout((caddr_t)p->p_ru,
                    341:                            (caddr_t)uap->rusage, sizeof (struct rusage))))
                    342:                                return (error);
                    343:                        p->p_xstat = 0;
                    344:                        ruadd(&q->p_stats->p_cru, p->p_ru);
                    345:                        FREE(p->p_ru, M_ZOMBIE);
                    346:                        if (--p->p_cred->p_refcnt == 0) {
                    347:                                crfree(p->p_cred->pc_ucred);
                    348:                                FREE(p->p_cred, M_SUBPROC);
1.1.1.2 ! root      349:                                p->p_cred = (struct pcred *) -1;
1.1       root      350:                        }
                    351: 
                    352:                        /*
                    353:                         * Finally finished with old proc entry.
                    354:                         * Unlink it from its process group and free it.
                    355:                         */
                    356:                        leavepgrp(p);
                    357:                        if (*p->p_prev = p->p_nxt)      /* off zombproc */
                    358:                                p->p_nxt->p_prev = p->p_prev;
                    359:                        if (q = p->p_ysptr)
                    360:                                q->p_osptr = p->p_osptr;
                    361:                        if (q = p->p_osptr)
                    362:                                q->p_ysptr = p->p_ysptr;
                    363:                        if ((q = p->p_pptr)->p_cptr == p)
                    364:                                q->p_cptr = p->p_osptr;
                    365: 
                    366:                        /*
                    367:                         * Give machine-dependent layer a chance
                    368:                         * to free anything that cpu_exit couldn't
                    369:                         * release while still running in process context.
                    370:                         */
                    371:                        cpu_wait(p);
                    372:                        FREE(p, M_PROC);
                    373:                        nprocs--;
                    374:                        return (0);
                    375:                }
                    376:                if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 &&
                    377:                    (p->p_flag & STRC || uap->options & WUNTRACED)) {
                    378:                        p->p_flag |= SWTED;
                    379:                        retval[0] = p->p_pid;
                    380: #ifdef COMPAT_43
                    381:                        if (uap->compat) {
                    382:                                retval[1] = W_STOPCODE(p->p_xstat);
                    383:                                error = 0;
                    384:                        } else
                    385: #endif
                    386:                        if (uap->status) {
                    387:                                status = W_STOPCODE(p->p_xstat);
                    388:                                error = copyout((caddr_t)&status,
                    389:                                    (caddr_t)uap->status, sizeof(status));
                    390:                        } else
                    391:                                error = 0;
                    392:                        return (error);
                    393:                }
                    394:        }
                    395:        if (nfound == 0)
                    396:                return (ECHILD);
                    397:        if (uap->options & WNOHANG) {
                    398:                retval[0] = 0;
                    399:                return (0);
                    400:        }
                    401:        if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0))
                    402:                return (error);
                    403:        goto loop;
                    404: }

unix.superglobalmegacorp.com

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