1 /* 2 * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)kern_exit.c 7.46 (Berkeley) 07/08/92 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "map.h" 13 #include "ioctl.h" 14 #include "proc.h" 15 #include "tty.h" 16 #include "time.h" 17 #include "resource.h" 18 #include "kernel.h" 19 #include "buf.h" 20 #include "wait.h" 21 #include "file.h" 22 #include "vnode.h" 23 #include "syslog.h" 24 #include "malloc.h" 25 #include "resourcevar.h" 26 #include "ptrace.h" 27 28 #include "machine/cpu.h" 29 #ifdef COMPAT_43 30 #include "machine/reg.h" 31 #include "machine/psl.h" 32 #endif 33 34 #include "vm/vm.h" 35 #include "vm/vm_kern.h" 36 37 /* 38 * Exit system call: pass back caller's arg 39 */ 40 /* ARGSUSED */ 41 rexit(p, uap, retval) 42 struct proc *p; 43 struct args { 44 int rval; 45 } *uap; 46 int *retval; 47 { 48 49 exit(p, W_EXITCODE(uap->rval, 0)); 50 /* NOTREACHED */ 51 } 52 53 /* 54 * Exit: deallocate address space and other resources, 55 * change proc state to zombie, and unlink proc from allproc 56 * and parent's lists. Save exit status and rusage for wait(). 57 * Check for child processes and orphan them. 58 */ 59 exit(p, rv) 60 register struct proc *p; 61 int rv; 62 { 63 register struct proc *q, *nq; 64 register struct proc **pp; 65 register struct vmspace *vm; 66 struct timeval tv; 67 int s; 68 69 if (p->p_pid == 1) 70 panic("init died (signal %d, exit %d)", 71 WTERMSIG(rv), WEXITSTATUS(rv)); 72 #ifdef PGINPROF 73 vmsizmon(); 74 #endif 75 if (p->p_flag & SPROFIL) 76 stopprofclock(p); 77 MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 78 M_ZOMBIE, M_WAITOK); 79 /* 80 * If parent is waiting for us to exit or exec, 81 * SPPWAIT is set; we will wakeup the parent below. 82 */ 83 p->p_flag &= ~(STRC|SPPWAIT); 84 p->p_flag |= SWEXIT; 85 p->p_sigignore = ~0; 86 p->p_sig = 0; 87 untimeout(realitexpire, (caddr_t)p); 88 89 /* 90 * Close open files and release open-file table. 91 * This may block! 92 */ 93 fdfree(p); 94 95 /* The next two chunks should probably be moved to vmspace_exit. */ 96 vm = p->p_vmspace; 97 #ifdef SYSVSHM 98 if (vm->vm_shm) 99 shmexit(p); 100 #endif 101 /* 102 * Release user portion of address space. 103 * This releases references to vnodes, 104 * which could cause I/O if the file has been unlinked. 105 * Need to do this early enough that we can still sleep. 106 * Can't free the entire vmspace as the kernel stack 107 * may be mapped within that space also. 108 */ 109 if (vm->vm_refcnt == 1) 110 (void) vm_map_remove(&vm->vm_map, VM_MIN_ADDRESS, 111 VM_MAXUSER_ADDRESS); 112 113 if (SESS_LEADER(p)) { 114 register struct session *sp = p->p_session; 115 116 if (sp->s_ttyvp) { 117 /* 118 * Controlling process. 119 * Signal foreground pgrp, 120 * drain controlling terminal 121 * and revoke access to controlling terminal. 122 */ 123 if (sp->s_ttyp->t_session == sp) { 124 if (sp->s_ttyp->t_pgrp) 125 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 126 (void) ttywait(sp->s_ttyp); 127 vgoneall(sp->s_ttyvp); 128 } 129 vrele(sp->s_ttyvp); 130 sp->s_ttyvp = NULL; 131 /* 132 * s_ttyp is not zero'd; we use this to indicate 133 * that the session once had a controlling terminal. 134 * (for logging and informational purposes) 135 */ 136 } 137 sp->s_leader = NULL; 138 } 139 fixjobc(p, p->p_pgrp, 0); 140 p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY; 141 (void) acct(p); 142 #ifdef KTRACE 143 /* 144 * release trace file 145 */ 146 p->p_traceflag = 0; /* don't trace the vrele() */ 147 if (p->p_tracep) 148 vrele(p->p_tracep); 149 #endif 150 /* 151 * Remove proc from allproc queue and pidhash chain. 152 * Place onto zombproc. Unlink from parent's child list. 153 */ 154 if (*p->p_prev = p->p_nxt) 155 p->p_nxt->p_prev = p->p_prev; 156 if (p->p_nxt = zombproc) 157 p->p_nxt->p_prev = &p->p_nxt; 158 p->p_prev = &zombproc; 159 zombproc = p; 160 p->p_stat = SZOMB; 161 162 for (pp = &pidhash[PIDHASH(p->p_pid)]; *pp; pp = &(*pp)->p_hash) 163 if (*pp == p) { 164 *pp = p->p_hash; 165 goto done; 166 } 167 panic("exit"); 168 done: 169 170 if (p->p_cptr) /* only need this if any child is S_ZOMB */ 171 wakeup((caddr_t) initproc); 172 for (q = p->p_cptr; q != NULL; q = nq) { 173 nq = q->p_osptr; 174 if (nq != NULL) 175 nq->p_ysptr = NULL; 176 if (initproc->p_cptr) 177 initproc->p_cptr->p_ysptr = q; 178 q->p_osptr = initproc->p_cptr; 179 q->p_ysptr = NULL; 180 initproc->p_cptr = q; 181 182 q->p_pptr = initproc; 183 /* 184 * Traced processes are killed 185 * since their existence means someone is screwing up. 186 */ 187 if (q->p_flag&STRC) { 188 q->p_flag &= ~STRC; 189 psignal(q, SIGKILL); 190 } 191 } 192 p->p_cptr = NULL; 193 194 /* 195 * Save exit status and final rusage info, adding in child rusage 196 * info and self times. Add its most recent runtime here; we are 197 * not going to reach the usual code in swtch(). 198 */ 199 p->p_xstat = rv; 200 *p->p_ru = p->p_stats->p_ru; 201 microtime(&tv); 202 timevalsub(&tv, &runtime); 203 timevaladd(&p->p_rtime, &tv); 204 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 205 ruadd(p->p_ru, &p->p_stats->p_cru); 206 207 /* 208 * Notify parent that we're gone. 209 */ 210 psignal(p->p_pptr, SIGCHLD); 211 wakeup((caddr_t)p->p_pptr); 212 #if defined(tahoe) 213 /* move this to cpu_exit */ 214 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 215 #endif 216 /* 217 * Clear curproc after we've done all operations 218 * that could block, and before tearing down the rest 219 * of the process state that might be used from clock, etc. 220 * Also, can't clear curproc while we're still runnable, 221 * as we're not on a run queue (we are current, just not 222 * a proper proc any longer!). 223 * 224 * Other substructures are freed from wait(). 225 */ 226 curproc = NULL; 227 if (--p->p_limit->p_refcnt == 0) 228 FREE(p->p_limit, M_SUBPROC); 229 230 /* 231 * Finally, call machine-dependent code to release the remaining 232 * resources including address space, the kernel stack and pcb. 233 * The address space is released by "vmspace_free(p->p_vmspace)"; 234 * This is machine-dependent, as we may have to change stacks 235 * or ensure that the current one isn't reallocated before we 236 * finish. cpu_exit will end with a call to cpu_swtch(), finishing 237 * our execution (pun intended). 238 */ 239 cpu_exit(p); 240 /* NOTREACHED */ 241 } 242 243 #ifdef COMPAT_43 244 owait(p, uap, retval) 245 struct proc *p; 246 register struct args { 247 int pid; 248 int *status; 249 int options; 250 struct rusage *rusage; 251 int compat; 252 } *uap; 253 int *retval; 254 { 255 256 #ifdef PSL_ALLCC 257 if ((p->p_md.md_regs[PS] & PSL_ALLCC) != PSL_ALLCC) { 258 uap->options = 0; 259 uap->rusage = 0; 260 } else { 261 uap->options = p->p_md.md_regs[R0]; 262 uap->rusage = (struct rusage *)p->p_md.md_regs[R1]; 263 } 264 #else 265 uap->options = 0; 266 uap->rusage = 0; 267 #endif 268 uap->pid = WAIT_ANY; 269 uap->status = 0; 270 uap->compat = 1; 271 return (wait1(p, uap, retval)); 272 } 273 274 wait4(p, uap, retval) 275 struct proc *p; 276 struct args { 277 int pid; 278 int *status; 279 int options; 280 struct rusage *rusage; 281 int compat; 282 } *uap; 283 int *retval; 284 { 285 286 uap->compat = 0; 287 return (wait1(p, uap, retval)); 288 } 289 #else 290 #define wait1 wait4 291 #endif 292 293 /* 294 * Wait: check child processes to see if any have exited, 295 * stopped under trace, or (optionally) stopped by a signal. 296 * Pass back status and deallocate exited child's proc structure. 297 */ 298 wait1(q, uap, retval) 299 register struct proc *q; 300 register struct args { 301 int pid; 302 int *status; 303 int options; 304 struct rusage *rusage; 305 #ifdef COMPAT_43 306 int compat; 307 #endif 308 } *uap; 309 int retval[]; 310 { 311 register int nfound; 312 register struct proc *p, *t; 313 int status, error; 314 315 if (uap->pid == 0) 316 uap->pid = -q->p_pgid; 317 #ifdef notyet 318 if (uap->options &~ (WUNTRACED|WNOHANG)) 319 return (EINVAL); 320 #endif 321 loop: 322 nfound = 0; 323 for (p = q->p_cptr; p; p = p->p_osptr) { 324 if (uap->pid != WAIT_ANY && 325 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 326 continue; 327 nfound++; 328 if (p->p_stat == SZOMB) { 329 retval[0] = p->p_pid; 330 #ifdef COMPAT_43 331 if (uap->compat) 332 retval[1] = p->p_xstat; 333 else 334 #endif 335 if (uap->status) { 336 status = p->p_xstat; /* convert to int */ 337 if (error = copyout((caddr_t)&status, 338 (caddr_t)uap->status, sizeof(status))) 339 return (error); 340 } 341 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 342 (caddr_t)uap->rusage, sizeof (struct rusage)))) 343 return (error); 344 /* 345 * If we got the child via a ptrace 'attach', 346 * we need to give it back to the old parent. 347 */ 348 if (p->p_oppid && (t = pfind(p->p_oppid))) { 349 p->p_oppid = 0; 350 proc_reparent(p, t); 351 psignal(t, SIGCHLD); 352 wakeup((caddr_t)t); 353 return (0); 354 } 355 p->p_xstat = 0; 356 ruadd(&q->p_stats->p_cru, p->p_ru); 357 FREE(p->p_ru, M_ZOMBIE); 358 if (--p->p_cred->p_refcnt == 0) { 359 crfree(p->p_cred->pc_ucred); 360 FREE(p->p_cred, M_SUBPROC); 361 } 362 363 /* 364 * Finally finished with old proc entry. 365 * Unlink it from its process group and free it. 366 */ 367 leavepgrp(p); 368 if (*p->p_prev = p->p_nxt) /* off zombproc */ 369 p->p_nxt->p_prev = p->p_prev; 370 if (q = p->p_ysptr) 371 q->p_osptr = p->p_osptr; 372 if (q = p->p_osptr) 373 q->p_ysptr = p->p_ysptr; 374 if ((q = p->p_pptr)->p_cptr == p) 375 q->p_cptr = p->p_osptr; 376 377 /* 378 * Give machine-dependent layer a chance 379 * to free anything that cpu_exit couldn't 380 * release while still running in process context. 381 */ 382 cpu_wait(p); 383 FREE(p, M_PROC); 384 nprocs--; 385 return (0); 386 } 387 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 388 (p->p_flag & STRC || uap->options & WUNTRACED)) { 389 p->p_flag |= SWTED; 390 retval[0] = p->p_pid; 391 #ifdef COMPAT_43 392 if (uap->compat) { 393 retval[1] = W_STOPCODE(p->p_xstat); 394 error = 0; 395 } else 396 #endif 397 if (uap->status) { 398 status = W_STOPCODE(p->p_xstat); 399 error = copyout((caddr_t)&status, 400 (caddr_t)uap->status, sizeof(status)); 401 } else 402 error = 0; 403 return (error); 404 } 405 } 406 if (nfound == 0) 407 return (ECHILD); 408 if (uap->options & WNOHANG) { 409 retval[0] = 0; 410 return (0); 411 } 412 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) 413 return (error); 414 goto loop; 415 } 416 417 /* 418 * make process 'parent' the new parent of process 'child'. 419 */ 420 void 421 proc_reparent(child, parent) 422 register struct proc *child; 423 register struct proc *parent; 424 { 425 register struct proc *o; 426 register struct proc *y; 427 428 if (child->p_pptr == parent) 429 return; 430 431 /* fix up the child linkage for the old parent */ 432 o = child->p_osptr; 433 y = child->p_ysptr; 434 if (y) 435 y->p_osptr = o; 436 if (o) 437 o->p_ysptr = y; 438 if (child->p_pptr->p_cptr == child) 439 child->p_pptr->p_cptr = o; 440 441 /* fix up child linkage for new parent */ 442 o = parent->p_cptr; 443 if (o) 444 o->p_ysptr = child; 445 child->p_osptr = o; 446 child->p_ysptr = NULL; 447 parent->p_cptr = child; 448 child->p_pptr = parent; 449 } 450