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