123369Smckusick /* 2*37328Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 323369Smckusick * All rights reserved. The Berkeley software License Agreement 423369Smckusick * specifies the terms and conditions for redistribution. 523369Smckusick * 6*37328Skarels * @(#)kern_exit.c 7.5 (Berkeley) 04/08/89 723369Smckusick */ 812790Ssam 917090Sbloom #include "param.h" 1017090Sbloom #include "systm.h" 1117090Sbloom #include "map.h" 1217090Sbloom #include "dir.h" 1317090Sbloom #include "user.h" 1417090Sbloom #include "kernel.h" 1517090Sbloom #include "proc.h" 1617090Sbloom #include "buf.h" 1717090Sbloom #include "wait.h" 1817090Sbloom #include "vm.h" 1917090Sbloom #include "file.h" 2017090Sbloom #include "inode.h" 21*37328Skarels #include "tty.h" 2218638Ssam #include "syslog.h" 2332018Smckusick #include "malloc.h" 2412790Ssam 25*37328Skarels #include "../machine/reg.h" 26*37328Skarels #ifdef COMPAT_43 27*37328Skarels #include "../machine/psl.h" 28*37328Skarels #endif 29*37328Skarels 3012790Ssam /* 3112790Ssam * Exit system call: pass back caller's arg 3212790Ssam */ 3312790Ssam rexit() 3412790Ssam { 3512790Ssam register struct a { 3612790Ssam int rval; 3712790Ssam } *uap; 38*37328Skarels union wait status; 3912790Ssam 4012790Ssam uap = (struct a *)u.u_ap; 41*37328Skarels status.w_status = 0; 42*37328Skarels status.w_retcode = uap->rval; 43*37328Skarels exit(status.w_status); 4412790Ssam } 4512790Ssam 4612790Ssam /* 4712790Ssam * Release resources. 4812790Ssam * Save u. area for parent to look at. 4912790Ssam * Enter zombie state. 5012790Ssam * Wake up parent and init processes, 5112790Ssam * and dispose of children. 5212790Ssam */ 5312790Ssam exit(rv) 54*37328Skarels int rv; /* should be union wait (XXX) */ 5512790Ssam { 5612790Ssam register int i; 5716527Skarels register struct proc *p, *q, *nq; 5812790Ssam register int x; 5912790Ssam 6012790Ssam #ifdef PGINPROF 6112790Ssam vmsizmon(); 6212790Ssam #endif 6312790Ssam p = u.u_procp; 6432018Smckusick MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 6532018Smckusick M_ZOMBIE, M_WAITOK); 6612790Ssam p->p_flag &= ~(STRC|SULOCK); 6712790Ssam p->p_flag |= SWEXIT; 6812882Ssam p->p_sigignore = ~0; 6912790Ssam p->p_cpticks = 0; 7012790Ssam p->p_pctcpu = 0; 7112882Ssam for (i = 0; i < NSIG; i++) 7212790Ssam u.u_signal[i] = SIG_IGN; 7312790Ssam untimeout(realitexpire, (caddr_t)p); 7412790Ssam /* 7512790Ssam * Release virtual memory. If we resulted from 7612790Ssam * a vfork(), instead give the resources back to 7712790Ssam * the parent. 7812790Ssam */ 7912790Ssam if ((p->p_flag & SVFORK) == 0) 8012790Ssam vrelvm(); 8112790Ssam else { 8212790Ssam p->p_flag &= ~SVFORK; 8312790Ssam wakeup((caddr_t)p); 8412790Ssam while ((p->p_flag & SVFDONE) == 0) 8512790Ssam sleep((caddr_t)p, PZERO - 1); 8612790Ssam p->p_flag &= ~SVFDONE; 8712790Ssam } 8821105Skarels for (i = 0; i <= u.u_lastfile; i++) { 8912790Ssam struct file *f; 9012790Ssam 9112790Ssam f = u.u_ofile[i]; 9221105Skarels if (f) { 9321105Skarels u.u_ofile[i] = NULL; 9421105Skarels u.u_pofile[i] = 0; 9521105Skarels closef(f); 9621105Skarels } 9712790Ssam } 98*37328Skarels if (SESS_LEADER(p)) { 99*37328Skarels p->p_session->s_leader = 0; 100*37328Skarels /* TODO: vhangup(); */ 101*37328Skarels if (u.u_ttyp) { 102*37328Skarels u.u_ttyp->t_session = 0; 103*37328Skarels u.u_ttyp->t_pgid = 0; 104*37328Skarels } 105*37328Skarels } 10612790Ssam ilock(u.u_cdir); 10712790Ssam iput(u.u_cdir); 10812790Ssam if (u.u_rdir) { 10912790Ssam ilock(u.u_rdir); 11012790Ssam iput(u.u_rdir); 11112790Ssam } 11212790Ssam u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 11312790Ssam acct(); 11412790Ssam #ifdef QUOTA 11512790Ssam qclean(); 11612790Ssam #endif 117*37328Skarels #ifdef KTRACE 118*37328Skarels /* 119*37328Skarels * release trace file 120*37328Skarels */ 121*37328Skarels if (p->p_tracep) 122*37328Skarels irele(p->p_tracep); 123*37328Skarels #endif 12426823Skarels /* 125*37328Skarels /* 12626823Skarels * Freeing the user structure and kernel stack 12726823Skarels * for the current process: have to run a bit longer 12826823Skarels * using the pages which are about to be freed... 12926823Skarels * vrelu will block memory allocation by raising ipl. 13026823Skarels */ 13126823Skarels vrelu(u.u_procp, 0); 13212790Ssam vrelpt(u.u_procp); 13316527Skarels if (*p->p_prev = p->p_nxt) /* off allproc queue */ 13416527Skarels p->p_nxt->p_prev = p->p_prev; 13516527Skarels if (p->p_nxt = zombproc) /* onto zombproc */ 13616527Skarels p->p_nxt->p_prev = &p->p_nxt; 13716527Skarels p->p_prev = &zombproc; 13816527Skarels zombproc = p; 13912790Ssam multprog--; 14012790Ssam p->p_stat = SZOMB; 14112790Ssam noproc = 1; 14212790Ssam i = PIDHASH(p->p_pid); 14312790Ssam x = p - proc; 14412790Ssam if (pidhash[i] == x) 14512790Ssam pidhash[i] = p->p_idhash; 14612790Ssam else { 14712790Ssam for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 14812790Ssam if (proc[i].p_idhash == x) { 14912790Ssam proc[i].p_idhash = p->p_idhash; 15012790Ssam goto done; 15112790Ssam } 15212790Ssam panic("exit"); 15312790Ssam } 15421105Skarels if (p->p_pid == 1) { 15521105Skarels if (p->p_dsize == 0) { 156*37328Skarels printf("Can't exec init (errno %d)\n", rv >> 8); 15721105Skarels for (;;) 15821105Skarels ; 15921105Skarels } else 16021105Skarels panic("init died"); 16121105Skarels } 16212790Ssam done: 16312790Ssam p->p_xstat = rv; 16421105Skarels *p->p_ru = u.u_ru; 16521105Skarels ruadd(p->p_ru, &u.u_cru); 16616527Skarels if (p->p_cptr) /* only need this if any child is S_ZOMB */ 16716527Skarels wakeup((caddr_t)&proc[1]); 168*37328Skarels if (PGRP_JOBC(p)) 169*37328Skarels p->p_pgrp->pg_jobc--; 17016527Skarels for (q = p->p_cptr; q != NULL; q = nq) { 171*37328Skarels if (PGRP_JOBC(q)) 172*37328Skarels q->p_pgrp->pg_jobc--; 17316527Skarels nq = q->p_osptr; 17416527Skarels if (nq != NULL) 17516527Skarels nq->p_ysptr = NULL; 17616527Skarels if (proc[1].p_cptr) 17716527Skarels proc[1].p_cptr->p_ysptr = q; 17816527Skarels q->p_osptr = proc[1].p_cptr; 17916527Skarels q->p_ysptr = NULL; 18016527Skarels proc[1].p_cptr = q; 18112790Ssam 18216527Skarels q->p_pptr = &proc[1]; 18316527Skarels q->p_ppid = 1; 18416527Skarels /* 18516527Skarels * Traced processes are killed 18616527Skarels * since their existence means someone is screwing up. 18716527Skarels * Stopped processes are sent a hangup and a continue. 18816527Skarels * This is designed to be ``safe'' for setuid 18916527Skarels * processes since they must be willing to tolerate 19016527Skarels * hangups anyways. 19116527Skarels */ 19216527Skarels if (q->p_flag&STRC) { 19316527Skarels q->p_flag &= ~STRC; 19416527Skarels psignal(q, SIGKILL); 19516527Skarels } else if (q->p_stat == SSTOP) { 19616527Skarels psignal(q, SIGHUP); 19716527Skarels psignal(q, SIGCONT); 19812790Ssam } 19916527Skarels /* 20016527Skarels * Protect this process from future 20116527Skarels * tty signals, clear TSTP/TTIN/TTOU if pending. 20216527Skarels */ 20325385Skarels (void) spgrp(q); 20416527Skarels } 20517540Skarels p->p_cptr = NULL; 20612790Ssam psignal(p->p_pptr, SIGCHLD); 20712790Ssam wakeup((caddr_t)p->p_pptr); 20829946Skarels #if defined(tahoe) 20929946Skarels dkeyrelease(p->p_dkey), p->p_dkey = 0; 21029946Skarels ckeyrelease(p->p_ckey), p->p_ckey = 0; 21129946Skarels u.u_pcb.pcb_savacc.faddr = (float *)NULL; 21229946Skarels #endif 21312790Ssam swtch(); 21412790Ssam } 21512790Ssam 216*37328Skarels #ifdef COMPAT_43 217*37328Skarels owait() 21812790Ssam { 219*37328Skarels struct a { 220*37328Skarels int pid; 221*37328Skarels union wait *status; 222*37328Skarels int options; 223*37328Skarels struct rusage *rusage; 224*37328Skarels } *uap = (struct a *)u.u_ap; 22512790Ssam 22612790Ssam if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 227*37328Skarels uap->options = WSIGRESTART; 228*37328Skarels uap->rusage = 0; 229*37328Skarels } else { 230*37328Skarels uap->options = u.u_ar0[R0] | WSIGRESTART; 231*37328Skarels uap->rusage = (struct rusage *)u.u_ar0[R1]; 23212790Ssam } 233*37328Skarels uap->pid = WAIT_ANY; 234*37328Skarels uap->status = 0; 235*37328Skarels wait1(1); 23612790Ssam } 23712790Ssam 238*37328Skarels wait4() 239*37328Skarels { 240*37328Skarels wait1(0); 241*37328Skarels } 242*37328Skarels #endif 243*37328Skarels 24412790Ssam /* 24512790Ssam * Wait system call. 24612790Ssam * Search for a terminated (zombie) child, 24712790Ssam * finally lay it to rest, and collect its status. 24812790Ssam * Look also for stopped (traced) children, 24912790Ssam * and pass back status from them. 25012790Ssam */ 251*37328Skarels #ifdef COMPAT_43 252*37328Skarels wait1(compat) 253*37328Skarels int compat; 254*37328Skarels #else 255*37328Skarels wait4() 256*37328Skarels #endif 25712790Ssam { 258*37328Skarels register struct a { 259*37328Skarels int pid; 260*37328Skarels union wait *status; 261*37328Skarels int options; 262*37328Skarels struct rusage *rusage; 263*37328Skarels } *uap = (struct a *)u.u_ap; 26412790Ssam register f; 26512790Ssam register struct proc *p, *q; 266*37328Skarels union wait status; 26712790Ssam 26812790Ssam f = 0; 269*37328Skarels q = u.u_procp; 270*37328Skarels if (uap->pid == 0) 271*37328Skarels uap->pid = -q->p_pgid; 272*37328Skarels if (uap->options &~ (WUNTRACED|WNOHANG|WSIGRESTART)) { 273*37328Skarels u.u_error = EINVAL; 274*37328Skarels return; 275*37328Skarels } 27612790Ssam loop: 27716527Skarels for (p = q->p_cptr; p; p = p->p_osptr) { 278*37328Skarels if (uap->pid != WAIT_ANY && 279*37328Skarels p->p_pid != uap->pid && p->p_pgid != -uap->pid) 280*37328Skarels continue; 28112790Ssam f++; 28212790Ssam if (p->p_stat == SZOMB) { 283*37328Skarels pgrm(p); /* off pgrp */ 28412790Ssam u.u_r.r_val1 = p->p_pid; 285*37328Skarels #ifdef COMPAT_43 286*37328Skarels if (compat) 287*37328Skarels u.u_r.r_val2 = p->p_xstat; 288*37328Skarels else 289*37328Skarels #endif 290*37328Skarels if (uap->status) { 291*37328Skarels status.w_status = p->p_xstat; 292*37328Skarels if (u.u_error = copyout((caddr_t)&status, 293*37328Skarels (caddr_t)uap->status, sizeof(status))) 294*37328Skarels return; 295*37328Skarels } 29612790Ssam p->p_xstat = 0; 297*37328Skarels if (uap->rusage) 298*37328Skarels u.u_error = copyout((caddr_t)p->p_ru, 299*37328Skarels (caddr_t)uap->rusage, 300*37328Skarels sizeof (struct rusage)); 301*37328Skarels ruadd(&u.u_cru, p->p_ru); 302*37328Skarels FREE(p->p_ru, M_ZOMBIE); 303*37328Skarels p->p_ru = 0; 30412790Ssam p->p_stat = NULL; 30512790Ssam p->p_pid = 0; 30612790Ssam p->p_ppid = 0; 30716527Skarels if (*p->p_prev = p->p_nxt) /* off zombproc */ 30816527Skarels p->p_nxt->p_prev = p->p_prev; 30916527Skarels p->p_nxt = freeproc; /* onto freeproc */ 31016527Skarels freeproc = p; 31112790Ssam if (q = p->p_ysptr) 31212790Ssam q->p_osptr = p->p_osptr; 31312790Ssam if (q = p->p_osptr) 31412790Ssam q->p_ysptr = p->p_ysptr; 31512790Ssam if ((q = p->p_pptr)->p_cptr == p) 31612790Ssam q->p_cptr = p->p_osptr; 31712790Ssam p->p_pptr = 0; 31812790Ssam p->p_ysptr = 0; 31912790Ssam p->p_osptr = 0; 32012790Ssam p->p_cptr = 0; 32112790Ssam p->p_sig = 0; 32212882Ssam p->p_sigcatch = 0; 32312882Ssam p->p_sigignore = 0; 32412882Ssam p->p_sigmask = 0; 325*37328Skarels /*p->p_pgrp = 0;*/ 32612790Ssam p->p_flag = 0; 32712790Ssam p->p_wchan = 0; 32812790Ssam p->p_cursig = 0; 329*37328Skarels return; 33012790Ssam } 331*37328Skarels if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 332*37328Skarels (p->p_flag & STRC || uap->options & WUNTRACED)) { 33312790Ssam p->p_flag |= SWTED; 33412790Ssam u.u_r.r_val1 = p->p_pid; 335*37328Skarels #ifdef COMPAT_43 336*37328Skarels if (compat) 337*37328Skarels u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 338*37328Skarels else 339*37328Skarels #endif 340*37328Skarels if (uap->status) { 341*37328Skarels status.w_status = 0; 342*37328Skarels status.w_stopval = WSTOPPED; 343*37328Skarels status.w_stopsig = p->p_cursig; 344*37328Skarels u.u_error = copyout((caddr_t)&status, 345*37328Skarels (caddr_t)uap->status, sizeof(status)); 346*37328Skarels } 347*37328Skarels return; 34812790Ssam } 34912790Ssam } 350*37328Skarels if (f == 0) { 351*37328Skarels u.u_error = ECHILD; 352*37328Skarels return; 353*37328Skarels } 354*37328Skarels if (uap->options & WNOHANG) { 35512790Ssam u.u_r.r_val1 = 0; 356*37328Skarels return; 35712790Ssam } 358*37328Skarels if (uap->options & WSIGRESTART && setjmp(&u.u_qsave)) { 35918310Smckusick p = u.u_procp; 360*37328Skarels if ((u.u_sigintr & sigmask(p->p_cursig)) != 0) { 361*37328Skarels u.u_error = EINTR; 362*37328Skarels return; 363*37328Skarels } 36412790Ssam u.u_eosys = RESTARTSYS; 365*37328Skarels return; 36612790Ssam } 36712790Ssam sleep((caddr_t)u.u_procp, PWAIT); 36812790Ssam goto loop; 36912790Ssam } 370