1*12790Ssam /* kern_exit.c 4.1 83/05/27 */ 2*12790Ssam 3*12790Ssam #include "../machine/reg.h" 4*12790Ssam #include "../machine/psl.h" 5*12790Ssam 6*12790Ssam #include "../h/param.h" 7*12790Ssam #include "../h/systm.h" 8*12790Ssam #include "../h/map.h" 9*12790Ssam #include "../h/dir.h" 10*12790Ssam #include "../h/user.h" 11*12790Ssam #include "../h/kernel.h" 12*12790Ssam #include "../h/proc.h" 13*12790Ssam #include "../h/buf.h" 14*12790Ssam #include "../h/wait.h" 15*12790Ssam #include "../h/vm.h" 16*12790Ssam #include "../h/file.h" 17*12790Ssam #include "../h/mbuf.h" 18*12790Ssam #include "../h/inode.h" 19*12790Ssam 20*12790Ssam /* 21*12790Ssam * Exit system call: pass back caller's arg 22*12790Ssam */ 23*12790Ssam rexit() 24*12790Ssam { 25*12790Ssam register struct a { 26*12790Ssam int rval; 27*12790Ssam } *uap; 28*12790Ssam 29*12790Ssam uap = (struct a *)u.u_ap; 30*12790Ssam exit((uap->rval & 0377) << 8); 31*12790Ssam } 32*12790Ssam 33*12790Ssam /* 34*12790Ssam * Release resources. 35*12790Ssam * Save u. area for parent to look at. 36*12790Ssam * Enter zombie state. 37*12790Ssam * Wake up parent and init processes, 38*12790Ssam * and dispose of children. 39*12790Ssam */ 40*12790Ssam exit(rv) 41*12790Ssam int rv; 42*12790Ssam { 43*12790Ssam register int i; 44*12790Ssam register struct proc *p, *q; 45*12790Ssam register int x; 46*12790Ssam struct mbuf *m = m_getclr(M_WAIT, MT_ZOMBIE); 47*12790Ssam 48*12790Ssam #ifdef PGINPROF 49*12790Ssam vmsizmon(); 50*12790Ssam #endif 51*12790Ssam p = u.u_procp; 52*12790Ssam p->p_flag &= ~(STRC|SULOCK); 53*12790Ssam p->p_flag |= SWEXIT; 54*12790Ssam (void) spl6(); 55*12790Ssam /* we know SIG_IGN is 1 */ 56*12790Ssam p->p_siga0 = ~0; 57*12790Ssam p->p_siga1 = 0; 58*12790Ssam (void) spl0(); 59*12790Ssam p->p_cpticks = 0; 60*12790Ssam p->p_pctcpu = 0; 61*12790Ssam for (i=0; i<NSIG; i++) 62*12790Ssam u.u_signal[i] = SIG_IGN; 63*12790Ssam untimeout(realitexpire, (caddr_t)p); 64*12790Ssam /* 65*12790Ssam * Release virtual memory. If we resulted from 66*12790Ssam * a vfork(), instead give the resources back to 67*12790Ssam * the parent. 68*12790Ssam */ 69*12790Ssam if ((p->p_flag & SVFORK) == 0) 70*12790Ssam vrelvm(); 71*12790Ssam else { 72*12790Ssam p->p_flag &= ~SVFORK; 73*12790Ssam wakeup((caddr_t)p); 74*12790Ssam while ((p->p_flag & SVFDONE) == 0) 75*12790Ssam sleep((caddr_t)p, PZERO - 1); 76*12790Ssam p->p_flag &= ~SVFDONE; 77*12790Ssam } 78*12790Ssam for (i = 0; i < NOFILE; i++) { 79*12790Ssam struct file *f; 80*12790Ssam int po; 81*12790Ssam 82*12790Ssam f = u.u_ofile[i]; 83*12790Ssam u.u_ofile[i] = NULL; 84*12790Ssam po = u.u_pofile[i]; 85*12790Ssam u.u_pofile[i] = 0; 86*12790Ssam closef(f, po); 87*12790Ssam } 88*12790Ssam ilock(u.u_cdir); 89*12790Ssam iput(u.u_cdir); 90*12790Ssam if (u.u_rdir) { 91*12790Ssam ilock(u.u_rdir); 92*12790Ssam iput(u.u_rdir); 93*12790Ssam } 94*12790Ssam u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 95*12790Ssam acct(); 96*12790Ssam #ifdef QUOTA 97*12790Ssam qclean(); 98*12790Ssam #endif 99*12790Ssam #ifdef sun 100*12790Ssam ctxfree(u.u_procp); 101*12790Ssam #endif 102*12790Ssam vrelpt(u.u_procp); 103*12790Ssam vrelu(u.u_procp, 0); 104*12790Ssam (void) spl5(); /* hack for mem alloc race XXX */ 105*12790Ssam multprog--; 106*12790Ssam p->p_stat = SZOMB; 107*12790Ssam noproc = 1; 108*12790Ssam i = PIDHASH(p->p_pid); 109*12790Ssam x = p - proc; 110*12790Ssam if (pidhash[i] == x) 111*12790Ssam pidhash[i] = p->p_idhash; 112*12790Ssam else { 113*12790Ssam for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 114*12790Ssam if (proc[i].p_idhash == x) { 115*12790Ssam proc[i].p_idhash = p->p_idhash; 116*12790Ssam goto done; 117*12790Ssam } 118*12790Ssam panic("exit"); 119*12790Ssam } 120*12790Ssam if (p->p_pid == 1) 121*12790Ssam panic("init died"); 122*12790Ssam done: 123*12790Ssam p->p_xstat = rv; 124*12790Ssam if (m == 0) 125*12790Ssam panic("exit: m_getclr"); 126*12790Ssam p->p_ru = mtod(m, struct rusage *); 127*12790Ssam *p->p_ru = u.u_ru; 128*12790Ssam ruadd(p->p_ru, &u.u_cru); 129*12790Ssam for (q = proc; q < procNPROC; q++) 130*12790Ssam if (q->p_pptr == p) { 131*12790Ssam if (q->p_osptr) 132*12790Ssam q->p_osptr->p_ysptr = q->p_ysptr; 133*12790Ssam if (q->p_ysptr) 134*12790Ssam q->p_ysptr->p_osptr = q->p_osptr; 135*12790Ssam if (proc[1].p_cptr) 136*12790Ssam proc[1].p_cptr->p_ysptr = q; 137*12790Ssam q->p_osptr = proc[1].p_cptr; 138*12790Ssam q->p_ysptr = NULL; 139*12790Ssam proc[1].p_cptr = q; 140*12790Ssam 141*12790Ssam q->p_pptr = &proc[1]; 142*12790Ssam q->p_ppid = 1; 143*12790Ssam wakeup((caddr_t)&proc[1]); 144*12790Ssam /* 145*12790Ssam * Traced processes are killed 146*12790Ssam * since their existence means someone is screwing up. 147*12790Ssam * Stopped processes are sent a hangup and a continue. 148*12790Ssam * This is designed to be ``safe'' for setuid 149*12790Ssam * processes since they must be willing to tolerate 150*12790Ssam * hangups anyways. 151*12790Ssam */ 152*12790Ssam if (q->p_flag&STRC) { 153*12790Ssam q->p_flag &= ~STRC; 154*12790Ssam psignal(q, SIGKILL); 155*12790Ssam } else if (q->p_stat == SSTOP) { 156*12790Ssam psignal(q, SIGHUP); 157*12790Ssam psignal(q, SIGCONT); 158*12790Ssam } 159*12790Ssam /* 160*12790Ssam * Protect this process from future 161*12790Ssam * tty signals, clear TSTP/TTIN/TTOU if pending. 162*12790Ssam */ 163*12790Ssam (void) spgrp(q, -1); 164*12790Ssam } 165*12790Ssam psignal(p->p_pptr, SIGCHLD); 166*12790Ssam wakeup((caddr_t)p->p_pptr); 167*12790Ssam swtch(); 168*12790Ssam } 169*12790Ssam 170*12790Ssam wait() 171*12790Ssam { 172*12790Ssam struct rusage ru, *rup; 173*12790Ssam 174*12790Ssam if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 175*12790Ssam u.u_error = wait1(0, (struct rusage *)0); 176*12790Ssam return; 177*12790Ssam } 178*12790Ssam rup = (struct rusage *)u.u_ar0[R1]; 179*12790Ssam u.u_error = wait1(u.u_ar0[R0], &ru); 180*12790Ssam if (u.u_error) 181*12790Ssam return; 182*12790Ssam (void) copyout((caddr_t)&ru, (caddr_t)rup, sizeof (struct rusage)); 183*12790Ssam } 184*12790Ssam 185*12790Ssam #ifndef NOCOMPAT 186*12790Ssam #include "../h/vtimes.h" 187*12790Ssam 188*12790Ssam owait() 189*12790Ssam { 190*12790Ssam struct rusage ru; 191*12790Ssam struct vtimes *vtp, avt; 192*12790Ssam 193*12790Ssam if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 194*12790Ssam u.u_error = wait1(0, (struct rusage *)0); 195*12790Ssam return; 196*12790Ssam } 197*12790Ssam vtp = (struct vtimes *)u.u_ar0[R1]; 198*12790Ssam u.u_error = wait1(u.u_ar0[R0], &ru); 199*12790Ssam if (u.u_error) 200*12790Ssam return; 201*12790Ssam getvtimes(&ru, &avt); 202*12790Ssam (void) copyout((caddr_t)&avt, (caddr_t)vtp, sizeof (struct vtimes)); 203*12790Ssam } 204*12790Ssam #endif 205*12790Ssam 206*12790Ssam /* 207*12790Ssam * Wait system call. 208*12790Ssam * Search for a terminated (zombie) child, 209*12790Ssam * finally lay it to rest, and collect its status. 210*12790Ssam * Look also for stopped (traced) children, 211*12790Ssam * and pass back status from them. 212*12790Ssam */ 213*12790Ssam wait1(options, ru) 214*12790Ssam register int options; 215*12790Ssam struct rusage *ru; 216*12790Ssam { 217*12790Ssam register f; 218*12790Ssam register struct proc *p, *q; 219*12790Ssam 220*12790Ssam f = 0; 221*12790Ssam loop: 222*12790Ssam for (p = proc; p < procNPROC; p++) 223*12790Ssam if (p->p_pptr == u.u_procp) { 224*12790Ssam f++; 225*12790Ssam if (p->p_stat == SZOMB) { 226*12790Ssam u.u_r.r_val1 = p->p_pid; 227*12790Ssam u.u_r.r_val2 = p->p_xstat; 228*12790Ssam p->p_xstat = 0; 229*12790Ssam if (ru) 230*12790Ssam *ru = *p->p_ru; 231*12790Ssam ruadd(&u.u_cru, p->p_ru); 232*12790Ssam (void) m_free(dtom(p->p_ru)); 233*12790Ssam p->p_ru = 0; 234*12790Ssam p->p_stat = NULL; 235*12790Ssam p->p_pid = 0; 236*12790Ssam p->p_ppid = 0; 237*12790Ssam if (q = p->p_ysptr) 238*12790Ssam q->p_osptr = p->p_osptr; 239*12790Ssam if (q = p->p_osptr) 240*12790Ssam q->p_ysptr = p->p_ysptr; 241*12790Ssam if ((q = p->p_pptr)->p_cptr == p) 242*12790Ssam q->p_cptr = p->p_osptr; 243*12790Ssam p->p_pptr = 0; 244*12790Ssam p->p_ysptr = 0; 245*12790Ssam p->p_osptr = 0; 246*12790Ssam p->p_cptr = 0; 247*12790Ssam p->p_sig = 0; 248*12790Ssam p->p_siga0 = 0; 249*12790Ssam p->p_siga1 = 0; 250*12790Ssam p->p_pgrp = 0; 251*12790Ssam p->p_flag = 0; 252*12790Ssam p->p_wchan = 0; 253*12790Ssam p->p_cursig = 0; 254*12790Ssam return (0); 255*12790Ssam } 256*12790Ssam if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 257*12790Ssam (p->p_flag&STRC || options&WUNTRACED)) { 258*12790Ssam p->p_flag |= SWTED; 259*12790Ssam u.u_r.r_val1 = p->p_pid; 260*12790Ssam u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 261*12790Ssam return (0); 262*12790Ssam } 263*12790Ssam } 264*12790Ssam if (f == 0) { 265*12790Ssam return (ECHILD); 266*12790Ssam } 267*12790Ssam if (options&WNOHANG) { 268*12790Ssam u.u_r.r_val1 = 0; 269*12790Ssam return (0); 270*12790Ssam } 271*12790Ssam if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) { 272*12790Ssam u.u_eosys = RESTARTSYS; 273*12790Ssam return (0); 274*12790Ssam } 275*12790Ssam sleep((caddr_t)u.u_procp, PWAIT); 276*12790Ssam goto loop; 277*12790Ssam } 278