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