1 /* kern_exit.c 4.2 83/05/31 */ 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 (void) spl6(); 55 /* we know SIG_IGN is 1 */ 56 p->p_siga0 = ~0; 57 p->p_siga1 = 0; 58 (void) spl0(); 59 p->p_cpticks = 0; 60 p->p_pctcpu = 0; 61 for (i=0; i<NSIG; i++) 62 u.u_signal[i] = SIG_IGN; 63 untimeout(realitexpire, (caddr_t)p); 64 /* 65 * Release virtual memory. If we resulted from 66 * a vfork(), instead give the resources back to 67 * the parent. 68 */ 69 if ((p->p_flag & SVFORK) == 0) 70 vrelvm(); 71 else { 72 p->p_flag &= ~SVFORK; 73 wakeup((caddr_t)p); 74 while ((p->p_flag & SVFDONE) == 0) 75 sleep((caddr_t)p, PZERO - 1); 76 p->p_flag &= ~SVFDONE; 77 } 78 for (i = 0; i < NOFILE; i++) { 79 struct file *f; 80 int po; 81 82 f = u.u_ofile[i]; 83 u.u_ofile[i] = NULL; 84 po = u.u_pofile[i]; 85 u.u_pofile[i] = 0; 86 closef(f, po); 87 } 88 ilock(u.u_cdir); 89 iput(u.u_cdir); 90 if (u.u_rdir) { 91 ilock(u.u_rdir); 92 iput(u.u_rdir); 93 } 94 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 95 acct(); 96 #ifdef QUOTA 97 qclean(); 98 #endif 99 #ifdef sun 100 ctxfree(u.u_procp); 101 #endif 102 vrelpt(u.u_procp); 103 vrelu(u.u_procp, 0); 104 (void) spl5(); /* hack for mem alloc race XXX */ 105 multprog--; 106 p->p_stat = SZOMB; 107 noproc = 1; 108 i = PIDHASH(p->p_pid); 109 x = p - proc; 110 if (pidhash[i] == x) 111 pidhash[i] = p->p_idhash; 112 else { 113 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 114 if (proc[i].p_idhash == x) { 115 proc[i].p_idhash = p->p_idhash; 116 goto done; 117 } 118 panic("exit"); 119 } 120 if (p->p_pid == 1) 121 panic("init died"); 122 done: 123 p->p_xstat = rv; 124 if (m == 0) 125 panic("exit: m_getclr"); 126 p->p_ru = mtod(m, struct rusage *); 127 *p->p_ru = u.u_ru; 128 ruadd(p->p_ru, &u.u_cru); 129 for (q = proc; q < procNPROC; q++) 130 if (q->p_pptr == p) { 131 if (q->p_osptr) 132 q->p_osptr->p_ysptr = q->p_ysptr; 133 if (q->p_ysptr) 134 q->p_ysptr->p_osptr = q->p_osptr; 135 if (proc[1].p_cptr) 136 proc[1].p_cptr->p_ysptr = q; 137 q->p_osptr = proc[1].p_cptr; 138 q->p_ysptr = NULL; 139 proc[1].p_cptr = q; 140 141 q->p_pptr = &proc[1]; 142 q->p_ppid = 1; 143 wakeup((caddr_t)&proc[1]); 144 /* 145 * Traced processes are killed 146 * since their existence means someone is screwing up. 147 * Stopped processes are sent a hangup and a continue. 148 * This is designed to be ``safe'' for setuid 149 * processes since they must be willing to tolerate 150 * hangups anyways. 151 */ 152 if (q->p_flag&STRC) { 153 q->p_flag &= ~STRC; 154 psignal(q, SIGKILL); 155 } else if (q->p_stat == SSTOP) { 156 psignal(q, SIGHUP); 157 psignal(q, SIGCONT); 158 } 159 /* 160 * Protect this process from future 161 * tty signals, clear TSTP/TTIN/TTOU if pending. 162 */ 163 (void) spgrp(q, -1); 164 } 165 psignal(p->p_pptr, SIGCHLD); 166 wakeup((caddr_t)p->p_pptr); 167 swtch(); 168 } 169 170 wait() 171 { 172 struct rusage ru, *rup; 173 174 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 175 u.u_error = wait1(0, (struct rusage *)0); 176 return; 177 } 178 rup = (struct rusage *)u.u_ar0[R1]; 179 u.u_error = wait1(u.u_ar0[R0], &ru); 180 if (u.u_error) 181 return; 182 (void) copyout((caddr_t)&ru, (caddr_t)rup, sizeof (struct rusage)); 183 } 184 185 /* 186 * Wait system call. 187 * Search for a terminated (zombie) child, 188 * finally lay it to rest, and collect its status. 189 * Look also for stopped (traced) children, 190 * and pass back status from them. 191 */ 192 wait1(options, ru) 193 register int options; 194 struct rusage *ru; 195 { 196 register f; 197 register struct proc *p, *q; 198 199 f = 0; 200 loop: 201 for (p = proc; p < procNPROC; p++) 202 if (p->p_pptr == u.u_procp) { 203 f++; 204 if (p->p_stat == SZOMB) { 205 u.u_r.r_val1 = p->p_pid; 206 u.u_r.r_val2 = p->p_xstat; 207 p->p_xstat = 0; 208 if (ru) 209 *ru = *p->p_ru; 210 ruadd(&u.u_cru, p->p_ru); 211 (void) m_free(dtom(p->p_ru)); 212 p->p_ru = 0; 213 p->p_stat = NULL; 214 p->p_pid = 0; 215 p->p_ppid = 0; 216 if (q = p->p_ysptr) 217 q->p_osptr = p->p_osptr; 218 if (q = p->p_osptr) 219 q->p_ysptr = p->p_ysptr; 220 if ((q = p->p_pptr)->p_cptr == p) 221 q->p_cptr = p->p_osptr; 222 p->p_pptr = 0; 223 p->p_ysptr = 0; 224 p->p_osptr = 0; 225 p->p_cptr = 0; 226 p->p_sig = 0; 227 p->p_siga0 = 0; 228 p->p_siga1 = 0; 229 p->p_pgrp = 0; 230 p->p_flag = 0; 231 p->p_wchan = 0; 232 p->p_cursig = 0; 233 return (0); 234 } 235 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 236 (p->p_flag&STRC || options&WUNTRACED)) { 237 p->p_flag |= SWTED; 238 u.u_r.r_val1 = p->p_pid; 239 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 240 return (0); 241 } 242 } 243 if (f == 0) { 244 return (ECHILD); 245 } 246 if (options&WNOHANG) { 247 u.u_r.r_val1 = 0; 248 return (0); 249 } 250 if ((u.u_procp->p_flag&SNUSIG) && setjmp(&u.u_qsave)) { 251 u.u_eosys = RESTARTSYS; 252 return (0); 253 } 254 sleep((caddr_t)u.u_procp, PWAIT); 255 goto loop; 256 } 257