123369Smckusick /* 2*37728Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3*37728Smckusick * All rights reserved. 423369Smckusick * 5*37728Smckusick * Redistribution and use in source and binary forms are permitted 6*37728Smckusick * provided that the above copyright notice and this paragraph are 7*37728Smckusick * duplicated in all such forms and that any documentation, 8*37728Smckusick * advertising materials, and other materials related to such 9*37728Smckusick * distribution and use acknowledge that the software was developed 10*37728Smckusick * by the University of California, Berkeley. The name of the 11*37728Smckusick * University may not be used to endorse or promote products derived 12*37728Smckusick * from this software without specific prior written permission. 13*37728Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*37728Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*37728Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16*37728Smckusick * 17*37728Smckusick * @(#)kern_exit.c 7.7 (Berkeley) 05/09/89 1823369Smckusick */ 1912790Ssam 2017090Sbloom #include "param.h" 2117090Sbloom #include "systm.h" 2217090Sbloom #include "map.h" 2317090Sbloom #include "user.h" 2417090Sbloom #include "kernel.h" 2517090Sbloom #include "proc.h" 2617090Sbloom #include "buf.h" 2717090Sbloom #include "wait.h" 2817090Sbloom #include "vm.h" 2917090Sbloom #include "file.h" 30*37728Smckusick #include "vnode.h" 3137328Skarels #include "tty.h" 3218638Ssam #include "syslog.h" 3332018Smckusick #include "malloc.h" 3412790Ssam 3537520Smckusick #include "machine/reg.h" 3637328Skarels #ifdef COMPAT_43 3737520Smckusick #include "machine/psl.h" 3837328Skarels #endif 3937328Skarels 4012790Ssam /* 4112790Ssam * Exit system call: pass back caller's arg 4212790Ssam */ 4312790Ssam rexit() 4412790Ssam { 4512790Ssam register struct a { 4612790Ssam int rval; 4712790Ssam } *uap; 4837328Skarels union wait status; 4912790Ssam 5012790Ssam uap = (struct a *)u.u_ap; 5137328Skarels status.w_status = 0; 5237328Skarels status.w_retcode = uap->rval; 5337328Skarels exit(status.w_status); 5412790Ssam } 5512790Ssam 5612790Ssam /* 5712790Ssam * Release resources. 5812790Ssam * Save u. area for parent to look at. 5912790Ssam * Enter zombie state. 6012790Ssam * Wake up parent and init processes, 6112790Ssam * and dispose of children. 6212790Ssam */ 6312790Ssam exit(rv) 6437328Skarels int rv; /* should be union wait (XXX) */ 6512790Ssam { 6612790Ssam register int i; 6716527Skarels register struct proc *p, *q, *nq; 6812790Ssam register int x; 6912790Ssam 7012790Ssam #ifdef PGINPROF 7112790Ssam vmsizmon(); 7212790Ssam #endif 7312790Ssam p = u.u_procp; 7432018Smckusick MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 7532018Smckusick M_ZOMBIE, M_WAITOK); 7612790Ssam p->p_flag &= ~(STRC|SULOCK); 7712790Ssam p->p_flag |= SWEXIT; 7812882Ssam p->p_sigignore = ~0; 7912790Ssam p->p_cpticks = 0; 8012790Ssam p->p_pctcpu = 0; 8112882Ssam for (i = 0; i < NSIG; i++) 8212790Ssam u.u_signal[i] = SIG_IGN; 8312790Ssam untimeout(realitexpire, (caddr_t)p); 8412790Ssam /* 8512790Ssam * Release virtual memory. If we resulted from 8612790Ssam * a vfork(), instead give the resources back to 8712790Ssam * the parent. 8812790Ssam */ 8912790Ssam if ((p->p_flag & SVFORK) == 0) 9012790Ssam vrelvm(); 9112790Ssam else { 9212790Ssam p->p_flag &= ~SVFORK; 9312790Ssam wakeup((caddr_t)p); 9412790Ssam while ((p->p_flag & SVFDONE) == 0) 9512790Ssam sleep((caddr_t)p, PZERO - 1); 9612790Ssam p->p_flag &= ~SVFDONE; 9712790Ssam } 9821105Skarels for (i = 0; i <= u.u_lastfile; i++) { 9912790Ssam struct file *f; 10012790Ssam 10112790Ssam f = u.u_ofile[i]; 10221105Skarels if (f) { 10321105Skarels u.u_ofile[i] = NULL; 10421105Skarels u.u_pofile[i] = 0; 10521105Skarels closef(f); 10621105Skarels } 10712790Ssam } 10837328Skarels if (SESS_LEADER(p)) { 10937328Skarels p->p_session->s_leader = 0; 11037328Skarels /* TODO: vhangup(); */ 11137328Skarels if (u.u_ttyp) { 11237328Skarels u.u_ttyp->t_session = 0; 11337328Skarels u.u_ttyp->t_pgid = 0; 11437328Skarels } 11537328Skarels } 116*37728Smckusick VOP_LOCK(u.u_cdir); 117*37728Smckusick vput(u.u_cdir); 11812790Ssam if (u.u_rdir) { 119*37728Smckusick VOP_LOCK(u.u_rdir); 120*37728Smckusick vput(u.u_rdir); 12112790Ssam } 12212790Ssam u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 12312790Ssam acct(); 12412790Ssam #ifdef QUOTA 12512790Ssam qclean(); 12612790Ssam #endif 127*37728Smckusick crfree(u.u_cred); 12837328Skarels #ifdef KTRACE 12937328Skarels /* 13037328Skarels * release trace file 13137328Skarels */ 13237328Skarels if (p->p_tracep) 133*37728Smckusick vrele(p->p_tracep); 13437328Skarels #endif 13526823Skarels /* 13637328Skarels /* 13726823Skarels * Freeing the user structure and kernel stack 13826823Skarels * for the current process: have to run a bit longer 13926823Skarels * using the pages which are about to be freed... 14026823Skarels * vrelu will block memory allocation by raising ipl. 14126823Skarels */ 14226823Skarels vrelu(u.u_procp, 0); 14312790Ssam vrelpt(u.u_procp); 14416527Skarels if (*p->p_prev = p->p_nxt) /* off allproc queue */ 14516527Skarels p->p_nxt->p_prev = p->p_prev; 14616527Skarels if (p->p_nxt = zombproc) /* onto zombproc */ 14716527Skarels p->p_nxt->p_prev = &p->p_nxt; 14816527Skarels p->p_prev = &zombproc; 14916527Skarels zombproc = p; 15012790Ssam multprog--; 15112790Ssam p->p_stat = SZOMB; 15212790Ssam noproc = 1; 15312790Ssam i = PIDHASH(p->p_pid); 15412790Ssam x = p - proc; 15512790Ssam if (pidhash[i] == x) 15612790Ssam pidhash[i] = p->p_idhash; 15712790Ssam else { 15812790Ssam for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 15912790Ssam if (proc[i].p_idhash == x) { 16012790Ssam proc[i].p_idhash = p->p_idhash; 16112790Ssam goto done; 16212790Ssam } 16312790Ssam panic("exit"); 16412790Ssam } 16521105Skarels if (p->p_pid == 1) { 16621105Skarels if (p->p_dsize == 0) { 16737328Skarels printf("Can't exec init (errno %d)\n", rv >> 8); 16821105Skarels for (;;) 16921105Skarels ; 17021105Skarels } else 17121105Skarels panic("init died"); 17221105Skarels } 17312790Ssam done: 17412790Ssam p->p_xstat = rv; 17521105Skarels *p->p_ru = u.u_ru; 17621105Skarels ruadd(p->p_ru, &u.u_cru); 17716527Skarels if (p->p_cptr) /* only need this if any child is S_ZOMB */ 17816527Skarels wakeup((caddr_t)&proc[1]); 17937328Skarels if (PGRP_JOBC(p)) 18037328Skarels p->p_pgrp->pg_jobc--; 18116527Skarels for (q = p->p_cptr; q != NULL; q = nq) { 18237328Skarels if (PGRP_JOBC(q)) 18337328Skarels q->p_pgrp->pg_jobc--; 18416527Skarels nq = q->p_osptr; 18516527Skarels if (nq != NULL) 18616527Skarels nq->p_ysptr = NULL; 18716527Skarels if (proc[1].p_cptr) 18816527Skarels proc[1].p_cptr->p_ysptr = q; 18916527Skarels q->p_osptr = proc[1].p_cptr; 19016527Skarels q->p_ysptr = NULL; 19116527Skarels proc[1].p_cptr = q; 19212790Ssam 19316527Skarels q->p_pptr = &proc[1]; 19416527Skarels q->p_ppid = 1; 19516527Skarels /* 19616527Skarels * Traced processes are killed 19716527Skarels * since their existence means someone is screwing up. 19816527Skarels * Stopped processes are sent a hangup and a continue. 19916527Skarels * This is designed to be ``safe'' for setuid 20016527Skarels * processes since they must be willing to tolerate 20116527Skarels * hangups anyways. 20216527Skarels */ 20316527Skarels if (q->p_flag&STRC) { 20416527Skarels q->p_flag &= ~STRC; 20516527Skarels psignal(q, SIGKILL); 20616527Skarels } else if (q->p_stat == SSTOP) { 20716527Skarels psignal(q, SIGHUP); 20816527Skarels psignal(q, SIGCONT); 20912790Ssam } 21016527Skarels /* 21116527Skarels * Protect this process from future 21216527Skarels * tty signals, clear TSTP/TTIN/TTOU if pending. 21316527Skarels */ 21425385Skarels (void) spgrp(q); 21516527Skarels } 21617540Skarels p->p_cptr = NULL; 21712790Ssam psignal(p->p_pptr, SIGCHLD); 21812790Ssam wakeup((caddr_t)p->p_pptr); 21929946Skarels #if defined(tahoe) 22029946Skarels dkeyrelease(p->p_dkey), p->p_dkey = 0; 22129946Skarels ckeyrelease(p->p_ckey), p->p_ckey = 0; 22229946Skarels u.u_pcb.pcb_savacc.faddr = (float *)NULL; 22329946Skarels #endif 22412790Ssam swtch(); 22512790Ssam } 22612790Ssam 22737328Skarels #ifdef COMPAT_43 22837328Skarels owait() 22912790Ssam { 23037328Skarels struct a { 23137328Skarels int pid; 23237328Skarels union wait *status; 23337328Skarels int options; 23437328Skarels struct rusage *rusage; 23537328Skarels } *uap = (struct a *)u.u_ap; 23612790Ssam 23712790Ssam if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 23837328Skarels uap->options = WSIGRESTART; 23937328Skarels uap->rusage = 0; 24037328Skarels } else { 24137328Skarels uap->options = u.u_ar0[R0] | WSIGRESTART; 24237328Skarels uap->rusage = (struct rusage *)u.u_ar0[R1]; 24312790Ssam } 24437328Skarels uap->pid = WAIT_ANY; 24537328Skarels uap->status = 0; 24637328Skarels wait1(1); 24712790Ssam } 24812790Ssam 24937328Skarels wait4() 25037328Skarels { 25137328Skarels wait1(0); 25237328Skarels } 25337328Skarels #endif 25437328Skarels 25512790Ssam /* 25612790Ssam * Wait system call. 25712790Ssam * Search for a terminated (zombie) child, 25812790Ssam * finally lay it to rest, and collect its status. 25912790Ssam * Look also for stopped (traced) children, 26012790Ssam * and pass back status from them. 26112790Ssam */ 26237328Skarels #ifdef COMPAT_43 26337328Skarels wait1(compat) 26437328Skarels int compat; 26537328Skarels #else 26637328Skarels wait4() 26737328Skarels #endif 26812790Ssam { 26937328Skarels register struct a { 27037328Skarels int pid; 27137328Skarels union wait *status; 27237328Skarels int options; 27337328Skarels struct rusage *rusage; 27437328Skarels } *uap = (struct a *)u.u_ap; 27512790Ssam register f; 27612790Ssam register struct proc *p, *q; 27737328Skarels union wait status; 27812790Ssam 27912790Ssam f = 0; 28037328Skarels q = u.u_procp; 28137328Skarels if (uap->pid == 0) 28237328Skarels uap->pid = -q->p_pgid; 28337328Skarels if (uap->options &~ (WUNTRACED|WNOHANG|WSIGRESTART)) { 28437328Skarels u.u_error = EINVAL; 28537328Skarels return; 28637328Skarels } 28712790Ssam loop: 28816527Skarels for (p = q->p_cptr; p; p = p->p_osptr) { 28937328Skarels if (uap->pid != WAIT_ANY && 29037328Skarels p->p_pid != uap->pid && p->p_pgid != -uap->pid) 29137328Skarels continue; 29212790Ssam f++; 29312790Ssam if (p->p_stat == SZOMB) { 29437328Skarels pgrm(p); /* off pgrp */ 29512790Ssam u.u_r.r_val1 = p->p_pid; 29637328Skarels #ifdef COMPAT_43 29737328Skarels if (compat) 29837328Skarels u.u_r.r_val2 = p->p_xstat; 29937328Skarels else 30037328Skarels #endif 30137328Skarels if (uap->status) { 30237328Skarels status.w_status = p->p_xstat; 30337328Skarels if (u.u_error = copyout((caddr_t)&status, 30437328Skarels (caddr_t)uap->status, sizeof(status))) 30537328Skarels return; 30637328Skarels } 30712790Ssam p->p_xstat = 0; 30837328Skarels if (uap->rusage) 30937328Skarels u.u_error = copyout((caddr_t)p->p_ru, 31037328Skarels (caddr_t)uap->rusage, 31137328Skarels sizeof (struct rusage)); 31237328Skarels ruadd(&u.u_cru, p->p_ru); 31337328Skarels FREE(p->p_ru, M_ZOMBIE); 31437328Skarels p->p_ru = 0; 31512790Ssam p->p_stat = NULL; 31612790Ssam p->p_pid = 0; 31712790Ssam p->p_ppid = 0; 31816527Skarels if (*p->p_prev = p->p_nxt) /* off zombproc */ 31916527Skarels p->p_nxt->p_prev = p->p_prev; 32016527Skarels p->p_nxt = freeproc; /* onto freeproc */ 32116527Skarels freeproc = p; 32212790Ssam if (q = p->p_ysptr) 32312790Ssam q->p_osptr = p->p_osptr; 32412790Ssam if (q = p->p_osptr) 32512790Ssam q->p_ysptr = p->p_ysptr; 32612790Ssam if ((q = p->p_pptr)->p_cptr == p) 32712790Ssam q->p_cptr = p->p_osptr; 32812790Ssam p->p_pptr = 0; 32912790Ssam p->p_ysptr = 0; 33012790Ssam p->p_osptr = 0; 33112790Ssam p->p_cptr = 0; 33212790Ssam p->p_sig = 0; 33312882Ssam p->p_sigcatch = 0; 33412882Ssam p->p_sigignore = 0; 33512882Ssam p->p_sigmask = 0; 33637328Skarels /*p->p_pgrp = 0;*/ 33712790Ssam p->p_flag = 0; 33812790Ssam p->p_wchan = 0; 33912790Ssam p->p_cursig = 0; 34037328Skarels return; 34112790Ssam } 34237328Skarels if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 34337328Skarels (p->p_flag & STRC || uap->options & WUNTRACED)) { 34412790Ssam p->p_flag |= SWTED; 34512790Ssam u.u_r.r_val1 = p->p_pid; 34637328Skarels #ifdef COMPAT_43 34737328Skarels if (compat) 34837328Skarels u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 34937328Skarels else 35037328Skarels #endif 35137328Skarels if (uap->status) { 35237328Skarels status.w_status = 0; 35337328Skarels status.w_stopval = WSTOPPED; 35437328Skarels status.w_stopsig = p->p_cursig; 35537328Skarels u.u_error = copyout((caddr_t)&status, 35637328Skarels (caddr_t)uap->status, sizeof(status)); 35737328Skarels } 35837328Skarels return; 35912790Ssam } 36012790Ssam } 36137328Skarels if (f == 0) { 36237328Skarels u.u_error = ECHILD; 36337328Skarels return; 36437328Skarels } 36537328Skarels if (uap->options & WNOHANG) { 36612790Ssam u.u_r.r_val1 = 0; 36737328Skarels return; 36812790Ssam } 36937328Skarels if (uap->options & WSIGRESTART && setjmp(&u.u_qsave)) { 37018310Smckusick p = u.u_procp; 37137328Skarels if ((u.u_sigintr & sigmask(p->p_cursig)) != 0) { 37237328Skarels u.u_error = EINTR; 37337328Skarels return; 37437328Skarels } 37512790Ssam u.u_eosys = RESTARTSYS; 37637328Skarels return; 37712790Ssam } 37812790Ssam sleep((caddr_t)u.u_procp, PWAIT); 37912790Ssam goto loop; 38012790Ssam } 381