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