1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)kern_exit.c 7.21 (Berkeley) 06/05/90 18 */ 19 20 #include "param.h" 21 #include "systm.h" 22 #include "map.h" 23 #include "user.h" 24 #include "kernel.h" 25 #include "proc.h" 26 #include "buf.h" 27 #include "wait.h" 28 #include "vm.h" 29 #include "file.h" 30 #include "vnode.h" 31 #include "ioctl.h" 32 #include "tty.h" 33 #include "syslog.h" 34 #include "malloc.h" 35 36 #include "machine/reg.h" 37 #ifdef COMPAT_43 38 #include "machine/psl.h" 39 #endif 40 41 /* 42 * Exit system call: pass back caller's arg 43 */ 44 rexit() 45 { 46 struct a { 47 int rval; 48 } *uap; 49 50 uap = (struct a *)u.u_ap; 51 exit(W_EXITCODE(uap->rval, 0)); 52 } 53 54 /* 55 * Release resources. 56 * Save u. area for parent to look at. 57 * Enter zombie state. 58 * Wake up parent and init processes, 59 * and dispose of children. 60 */ 61 exit(rv) 62 int rv; 63 { 64 register int i; 65 register struct proc *p, *q, *nq; 66 register struct proc **pp; 67 68 #ifdef PGINPROF 69 vmsizmon(); 70 #endif 71 p = u.u_procp; 72 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 73 M_ZOMBIE, M_WAITOK); 74 p->p_flag &= ~(STRC|SULOCK); 75 p->p_flag |= SWEXIT; 76 p->p_sigignore = ~0; 77 p->p_sig = 0; 78 p->p_cpticks = 0; 79 p->p_pctcpu = 0; 80 for (i = 0; i < NSIG; i++) 81 u.u_signal[i] = SIG_IGN; 82 untimeout(realitexpire, (caddr_t)p); 83 /* 84 * Release virtual memory. If we resulted from 85 * a vfork(), instead give the resources back to 86 * the parent. 87 */ 88 if ((p->p_flag & SVFORK) == 0) { 89 #ifdef MAPMEM 90 if (u.u_mmap) 91 mmexit(); 92 #endif 93 vrelvm(); 94 } else { 95 p->p_flag &= ~SVFORK; 96 wakeup((caddr_t)p); 97 while ((p->p_flag & SVFDONE) == 0) 98 sleep((caddr_t)p, PZERO - 1); 99 p->p_flag &= ~SVFDONE; 100 } 101 for (i = 0; i <= u.u_lastfile; i++) { 102 struct file *f; 103 104 f = u.u_ofile[i]; 105 if (f) { 106 u.u_ofile[i] = NULL; 107 u.u_pofile[i] = 0; 108 (void) closef(f); 109 } 110 } 111 if (SESS_LEADER(p)) { 112 register struct session *sp = p->p_session; 113 114 if (sp->s_ttyvp) { 115 /* 116 * Controlling process. 117 * Signal foreground pgrp and revoke access 118 * to controlling terminal. 119 */ 120 if (sp->s_ttyp->t_pgrp) 121 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 122 vgoneall(sp->s_ttyvp); 123 vrele(sp->s_ttyvp); 124 sp->s_ttyvp = NULL; 125 /* 126 * s_ttyp is not zero'd; we use this to indicate 127 * that the session once had a controlling terminal. 128 * (for logging and informational purposes) 129 */ 130 } 131 sp->s_leader = 0; 132 } 133 VOP_LOCK(u.u_cdir); 134 vput(u.u_cdir); 135 if (u.u_rdir) { 136 VOP_LOCK(u.u_rdir); 137 vput(u.u_rdir); 138 } 139 u.u_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 140 acct(); 141 crfree(u.u_cred); 142 #ifdef KTRACE 143 /* 144 * release trace file 145 */ 146 if (p->p_tracep) 147 vrele(p->p_tracep); 148 #endif 149 /* 150 * Freeing the user structure and kernel stack 151 * for the current process: have to run a bit longer 152 * using the pages which are about to be freed... 153 * vrelu will block memory allocation by raising ipl. 154 */ 155 vrelu(u.u_procp, 0); 156 vrelpt(u.u_procp); 157 if (*p->p_prev = p->p_nxt) /* off allproc queue */ 158 p->p_nxt->p_prev = p->p_prev; 159 if (p->p_nxt = zombproc) /* onto zombproc */ 160 p->p_nxt->p_prev = &p->p_nxt; 161 p->p_prev = &zombproc; 162 zombproc = p; 163 multprog--; 164 p->p_stat = SZOMB; 165 noproc = 1; 166 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) 167 if (*pp == p) { 168 *pp = p->p_hash; 169 goto done; 170 } 171 panic("exit"); 172 done: 173 if (p->p_pid == 1) { 174 if (p->p_dsize == 0) { 175 printf("Can't exec init (errno %d)\n", WEXITSTATUS(rv)); 176 for (;;) 177 ; 178 } else 179 panic("init died"); 180 } 181 p->p_xstat = rv; 182 *p->p_ru = u.u_ru; 183 i = splclock(); 184 p->p_ru->ru_stime = p->p_stime; 185 p->p_ru->ru_utime = p->p_utime; 186 splx(i); 187 ruadd(p->p_ru, &u.u_cru); 188 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 189 wakeup((caddr_t)&proc[1]); 190 fixjobc(p, 0); 191 for (q = p->p_cptr; q != NULL; q = nq) { 192 nq = q->p_osptr; 193 if (nq != NULL) 194 nq->p_ysptr = NULL; 195 if (proc[1].p_cptr) 196 proc[1].p_cptr->p_ysptr = q; 197 q->p_osptr = proc[1].p_cptr; 198 q->p_ysptr = NULL; 199 proc[1].p_cptr = q; 200 201 q->p_pptr = &proc[1]; 202 q->p_ppid = 1; 203 /* 204 * Traced processes are killed 205 * since their existence means someone is screwing up. 206 */ 207 if (q->p_flag&STRC) { 208 q->p_flag &= ~STRC; 209 psignal(q, SIGKILL); 210 } 211 } 212 p->p_cptr = NULL; 213 psignal(p->p_pptr, SIGCHLD); 214 wakeup((caddr_t)p->p_pptr); 215 #if defined(tahoe) 216 dkeyrelease(p->p_dkey), p->p_dkey = 0; 217 ckeyrelease(p->p_ckey), p->p_ckey = 0; 218 u.u_pcb.pcb_savacc.faddr = (float *)NULL; 219 #endif 220 swtch(); 221 } 222 223 #ifdef COMPAT_43 224 owait() 225 { 226 register struct a { 227 int pid; 228 int *status; 229 int options; 230 struct rusage *rusage; 231 int compat; 232 } *uap = (struct a *)u.u_ap; 233 234 if ((u.u_ar0[PS] & PSL_ALLCC) != PSL_ALLCC) { 235 uap->options = 0; 236 uap->rusage = 0; 237 } else { 238 uap->options = u.u_ar0[R0]; 239 uap->rusage = (struct rusage *)u.u_ar0[R1]; 240 } 241 uap->pid = WAIT_ANY; 242 uap->status = 0; 243 uap->compat = 1; 244 u.u_error = wait1(); 245 } 246 247 wait4() 248 { 249 register struct a { 250 int pid; 251 int *status; 252 int options; 253 struct rusage *rusage; 254 int compat; 255 } *uap = (struct a *)u.u_ap; 256 257 uap->compat = 0; 258 u.u_error = wait1(); 259 } 260 #else 261 #define wait1 wait4 262 #endif 263 264 /* 265 * Wait system call. 266 * Search for a terminated (zombie) child, 267 * finally lay it to rest, and collect its status. 268 * Look also for stopped (traced) children, 269 * and pass back status from them. 270 */ 271 wait1() 272 { 273 register struct a { 274 int pid; 275 int *status; 276 int options; 277 struct rusage *rusage; 278 #ifdef COMPAT_43 279 int compat; 280 #endif 281 } *uap = (struct a *)u.u_ap; 282 register f; 283 register struct proc *p, *q; 284 int status, error; 285 286 q = u.u_procp; 287 if (uap->pid == 0) 288 uap->pid = -q->p_pgid; 289 #ifdef notyet 290 if (uap->options &~ (WUNTRACED|WNOHANG)) 291 return (EINVAL); 292 #endif 293 loop: 294 f = 0; 295 for (p = q->p_cptr; p; p = p->p_osptr) { 296 if (uap->pid != WAIT_ANY && 297 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 298 continue; 299 f++; 300 if (p->p_stat == SZOMB) { 301 u.u_r.r_val1 = p->p_pid; 302 #ifdef COMPAT_43 303 if (uap->compat) 304 u.u_r.r_val2 = p->p_xstat; 305 else 306 #endif 307 if (uap->status) { 308 status = p->p_xstat; /* convert to int */ 309 if (error = copyout((caddr_t)&status, 310 (caddr_t)uap->status, sizeof(status))) 311 return (error); 312 } 313 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 314 (caddr_t)uap->rusage, sizeof (struct rusage)))) 315 return (error); 316 pgrm(p); /* off pgrp */ 317 p->p_xstat = 0; 318 ruadd(&u.u_cru, p->p_ru); 319 FREE(p->p_ru, M_ZOMBIE); 320 p->p_ru = 0; 321 p->p_stat = NULL; 322 p->p_pid = 0; 323 p->p_ppid = 0; 324 if (*p->p_prev = p->p_nxt) /* off zombproc */ 325 p->p_nxt->p_prev = p->p_prev; 326 p->p_nxt = freeproc; /* onto freeproc */ 327 freeproc = p; 328 if (q = p->p_ysptr) 329 q->p_osptr = p->p_osptr; 330 if (q = p->p_osptr) 331 q->p_ysptr = p->p_ysptr; 332 if ((q = p->p_pptr)->p_cptr == p) 333 q->p_cptr = p->p_osptr; 334 p->p_pptr = 0; 335 p->p_ysptr = 0; 336 p->p_osptr = 0; 337 p->p_cptr = 0; 338 p->p_sig = 0; 339 p->p_sigcatch = 0; 340 p->p_sigignore = 0; 341 p->p_sigmask = 0; 342 /*p->p_pgrp = 0;*/ 343 p->p_flag = 0; 344 p->p_wchan = 0; 345 p->p_cursig = 0; 346 return (0); 347 } 348 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 349 (p->p_flag & STRC || uap->options & WUNTRACED)) { 350 p->p_flag |= SWTED; 351 u.u_r.r_val1 = p->p_pid; 352 #ifdef COMPAT_43 353 if (uap->compat) { 354 u.u_r.r_val2 = W_STOPCODE(p->p_cursig); 355 error = 0; 356 } else 357 #endif 358 if (uap->status) { 359 status = W_STOPCODE(p->p_cursig); 360 error = copyout((caddr_t)&status, 361 (caddr_t)uap->status, sizeof(status)); 362 } else 363 error = 0; 364 return (error); 365 } 366 } 367 if (f == 0) 368 return (ECHILD); 369 if (uap->options & WNOHANG) { 370 u.u_r.r_val1 = 0; 371 return (0); 372 } 373 if (error = tsleep((caddr_t)u.u_procp, PWAIT | PCATCH, "wait", 0)) 374 return (error); 375 goto loop; 376 } 377