1 /* kern_exit.c 6.7 85/04/14 */ 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 < NOFILE; i++) { 76 struct file *f; 77 78 f = u.u_ofile[i]; 79 u.u_ofile[i] = NULL; 80 u.u_pofile[i] = 0; 81 closef(f); 82 } 83 ilock(u.u_cdir); 84 iput(u.u_cdir); 85 if (u.u_rdir) { 86 ilock(u.u_rdir); 87 iput(u.u_rdir); 88 } 89 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 90 acct(); 91 #ifdef QUOTA 92 qclean(); 93 #endif 94 vrelpt(u.u_procp); 95 vrelu(u.u_procp, 0); 96 (void) spl5(); /* hack for mem alloc race XXX */ 97 if (*p->p_prev = p->p_nxt) /* off allproc queue */ 98 p->p_nxt->p_prev = p->p_prev; 99 if (p->p_nxt = zombproc) /* onto zombproc */ 100 p->p_nxt->p_prev = &p->p_nxt; 101 p->p_prev = &zombproc; 102 zombproc = p; 103 multprog--; 104 p->p_stat = SZOMB; 105 noproc = 1; 106 i = PIDHASH(p->p_pid); 107 x = p - proc; 108 if (pidhash[i] == x) 109 pidhash[i] = p->p_idhash; 110 else { 111 for (i = pidhash[i]; i != 0; i = proc[i].p_idhash) 112 if (proc[i].p_idhash == x) { 113 proc[i].p_idhash = p->p_idhash; 114 goto done; 115 } 116 panic("exit"); 117 } 118 if (p->p_pid == 1) 119 panic("init died"); 120 done: 121 p->p_xstat = rv; 122 if (m) { 123 p->p_ru = mtod(m, struct rusage *); 124 *p->p_ru = u.u_ru; 125 ruadd(p->p_ru, &u.u_cru); 126 } else 127 log(KERN_ALERT, "exit: pid %d: no mbuf", p->p_pid); 128 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 129 wakeup((caddr_t)&proc[1]); 130 for (q = p->p_cptr; q != NULL; q = nq) { 131 nq = q->p_osptr; 132 if (nq != NULL) 133 nq->p_ysptr = NULL; 134 if (proc[1].p_cptr) 135 proc[1].p_cptr->p_ysptr = q; 136 q->p_osptr = proc[1].p_cptr; 137 q->p_ysptr = NULL; 138 proc[1].p_cptr = q; 139 140 q->p_pptr = &proc[1]; 141 q->p_ppid = 1; 142 /* 143 * Traced processes are killed 144 * since their existence means someone is screwing up. 145 * Stopped processes are sent a hangup and a continue. 146 * This is designed to be ``safe'' for setuid 147 * processes since they must be willing to tolerate 148 * hangups anyways. 149 */ 150 if (q->p_flag&STRC) { 151 q->p_flag &= ~STRC; 152 psignal(q, SIGKILL); 153 } else if (q->p_stat == SSTOP) { 154 psignal(q, SIGHUP); 155 psignal(q, SIGCONT); 156 } 157 /* 158 * Protect this process from future 159 * tty signals, clear TSTP/TTIN/TTOU if pending. 160 */ 161 (void) spgrp(q, -1); 162 } 163 p->p_cptr = NULL; 164 psignal(p->p_pptr, SIGCHLD); 165 wakeup((caddr_t)p->p_pptr); 166 swtch(); 167 } 168 169 wait() 170 { 171 struct rusage ru, *rup; 172 173 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 174 u.u_error = wait1(0, (struct rusage *)0); 175 return; 176 } 177 rup = (struct rusage *)u.u_ar0[R1]; 178 u.u_error = wait1(u.u_ar0[R0], &ru); 179 if (u.u_error) 180 return; 181 if (rup != (struct rusage *)0) 182 u.u_error = copyout((caddr_t)&ru, (caddr_t)rup, 183 sizeof (struct rusage)); 184 } 185 186 /* 187 * Wait system call. 188 * Search for a terminated (zombie) child, 189 * finally lay it to rest, and collect its status. 190 * Look also for stopped (traced) children, 191 * and pass back status from them. 192 */ 193 wait1(options, ru) 194 register int options; 195 struct rusage *ru; 196 { 197 register f; 198 register struct proc *p, *q; 199 200 f = 0; 201 loop: 202 q = u.u_procp; 203 for (p = q->p_cptr; p; p = p->p_osptr) { 204 f++; 205 if (p->p_stat == SZOMB) { 206 u.u_r.r_val1 = p->p_pid; 207 u.u_r.r_val2 = p->p_xstat; 208 p->p_xstat = 0; 209 if (ru && p->p_ru) 210 *ru = *p->p_ru; 211 if (p->p_ru) { 212 ruadd(&u.u_cru, p->p_ru); 213 (void) m_free(dtom(p->p_ru)); 214 p->p_ru = 0; 215 } 216 p->p_stat = NULL; 217 p->p_pid = 0; 218 p->p_ppid = 0; 219 if (*p->p_prev = p->p_nxt) /* off zombproc */ 220 p->p_nxt->p_prev = p->p_prev; 221 p->p_nxt = freeproc; /* onto freeproc */ 222 freeproc = p; 223 if (q = p->p_ysptr) 224 q->p_osptr = p->p_osptr; 225 if (q = p->p_osptr) 226 q->p_ysptr = p->p_ysptr; 227 if ((q = p->p_pptr)->p_cptr == p) 228 q->p_cptr = p->p_osptr; 229 p->p_pptr = 0; 230 p->p_ysptr = 0; 231 p->p_osptr = 0; 232 p->p_cptr = 0; 233 p->p_sig = 0; 234 p->p_sigcatch = 0; 235 p->p_sigignore = 0; 236 p->p_sigmask = 0; 237 p->p_pgrp = 0; 238 p->p_flag = 0; 239 p->p_wchan = 0; 240 p->p_cursig = 0; 241 return (0); 242 } 243 if (p->p_stat == SSTOP && (p->p_flag&SWTED)==0 && 244 (p->p_flag&STRC || options&WUNTRACED)) { 245 p->p_flag |= SWTED; 246 u.u_r.r_val1 = p->p_pid; 247 u.u_r.r_val2 = (p->p_cursig<<8) | WSTOPPED; 248 return (0); 249 } 250 } 251 if (f == 0) 252 return (ECHILD); 253 if (options&WNOHANG) { 254 u.u_r.r_val1 = 0; 255 return (0); 256 } 257 if (setjmp(&u.u_qsave)) { 258 p = u.u_procp; 259 if ((u.u_sigintr & sigmask(p->p_cursig)) != 0 || 260 (p->p_flag & SOUSIG) != 0) 261 return(EINTR); 262 u.u_eosys = RESTARTSYS; 263 return (0); 264 } 265 sleep((caddr_t)u.u_procp, PWAIT); 266 goto loop; 267 } 268