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.50 (Berkeley) 10/11/92 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/map.h> 13 #include <sys/ioctl.h> 14 #include <sys/proc.h> 15 #include <sys/tty.h> 16 #include <sys/time.h> 17 #include <sys/resource.h> 18 #include <sys/kernel.h> 19 #include <sys/buf.h> 20 #include <sys/wait.h> 21 #include <sys/file.h> 22 #include <sys/vnode.h> 23 #include <sys/syslog.h> 24 #include <sys/malloc.h> 25 #include <sys/resourcevar.h> 26 #include <sys/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 struct rexit_args { 41 int rval; 42 }; 43 /* ARGSUSED */ 44 rexit(p, uap, retval) 45 struct proc *p; 46 struct rexit_args *uap; 47 int *retval; 48 { 49 50 exit(p, W_EXITCODE(uap->rval, 0)); 51 /* NOTREACHED */ 52 } 53 54 /* 55 * Exit: deallocate address space and other resources, 56 * change proc state to zombie, and unlink proc from allproc 57 * and parent's lists. Save exit status and rusage for wait(). 58 * Check for child processes and orphan them. 59 */ 60 exit(p, rv) 61 register struct proc *p; 62 int rv; 63 { 64 register struct proc *q, *nq; 65 register struct proc **pp; 66 register struct vmspace *vm; 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. 197 */ 198 p->p_xstat = rv; 199 *p->p_ru = p->p_stats->p_ru; 200 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 201 ruadd(p->p_ru, &p->p_stats->p_cru); 202 203 /* 204 * Notify parent that we're gone. 205 */ 206 psignal(p->p_pptr, SIGCHLD); 207 wakeup((caddr_t)p->p_pptr); 208 #if defined(tahoe) 209 /* move this to cpu_exit */ 210 p->p_addr->u_pcb.pcb_savacc.faddr = (float *)NULL; 211 #endif 212 /* 213 * Clear curproc after we've done all operations 214 * that could block, and before tearing down the rest 215 * of the process state that might be used from clock, etc. 216 * Also, can't clear curproc while we're still runnable, 217 * as we're not on a run queue (we are current, just not 218 * a proper proc any longer!). 219 * 220 * Other substructures are freed from wait(). 221 */ 222 curproc = NULL; 223 if (--p->p_limit->p_refcnt == 0) 224 FREE(p->p_limit, M_SUBPROC); 225 226 /* 227 * Finally, call machine-dependent code to release the remaining 228 * resources including address space, the kernel stack and pcb. 229 * The address space is released by "vmspace_free(p->p_vmspace)"; 230 * This is machine-dependent, as we may have to change stacks 231 * or ensure that the current one isn't reallocated before we 232 * finish. cpu_exit will end with a call to cpu_swtch(), finishing 233 * our execution (pun intended). 234 */ 235 cpu_exit(p); 236 /* NOTREACHED */ 237 } 238 239 struct wait_args { 240 int pid; 241 int *status; 242 int options; 243 struct rusage *rusage; 244 #ifdef COMPAT_43 245 int compat; /* pseudo */ 246 #endif 247 }; 248 249 #ifdef COMPAT_43 250 owait(p, uap, retval) 251 struct proc *p; 252 register struct wait_args *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 = NULL; 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 = NULL; 267 #endif 268 uap->pid = WAIT_ANY; 269 uap->status = NULL; 270 uap->compat = 1; 271 return (wait1(p, uap, retval)); 272 } 273 274 wait4(p, uap, retval) 275 struct proc *p; 276 struct wait_args *uap; 277 int *retval; 278 { 279 280 uap->compat = 0; 281 return (wait1(p, uap, retval)); 282 } 283 #else 284 #define wait1 wait4 285 #endif 286 287 /* 288 * Wait: check child processes to see if any have exited, 289 * stopped under trace, or (optionally) stopped by a signal. 290 * Pass back status and deallocate exited child's proc structure. 291 */ 292 wait1(q, uap, retval) 293 register struct proc *q; 294 register struct wait_args *uap; 295 int retval[]; 296 { 297 register int nfound; 298 register struct proc *p, *t; 299 int status, error; 300 301 if (uap->pid == 0) 302 uap->pid = -q->p_pgid; 303 #ifdef notyet 304 if (uap->options &~ (WUNTRACED|WNOHANG)) 305 return (EINVAL); 306 #endif 307 loop: 308 nfound = 0; 309 for (p = q->p_cptr; p; p = p->p_osptr) { 310 if (uap->pid != WAIT_ANY && 311 p->p_pid != uap->pid && p->p_pgid != -uap->pid) 312 continue; 313 nfound++; 314 if (p->p_stat == SZOMB) { 315 retval[0] = p->p_pid; 316 #ifdef COMPAT_43 317 if (uap->compat) 318 retval[1] = p->p_xstat; 319 else 320 #endif 321 if (uap->status) { 322 status = p->p_xstat; /* convert to int */ 323 if (error = copyout((caddr_t)&status, 324 (caddr_t)uap->status, sizeof(status))) 325 return (error); 326 } 327 if (uap->rusage && (error = copyout((caddr_t)p->p_ru, 328 (caddr_t)uap->rusage, sizeof (struct rusage)))) 329 return (error); 330 /* 331 * If we got the child via a ptrace 'attach', 332 * we need to give it back to the old parent. 333 */ 334 if (p->p_oppid && (t = pfind(p->p_oppid))) { 335 p->p_oppid = 0; 336 proc_reparent(p, t); 337 psignal(t, SIGCHLD); 338 wakeup((caddr_t)t); 339 return (0); 340 } 341 p->p_xstat = 0; 342 ruadd(&q->p_stats->p_cru, p->p_ru); 343 FREE(p->p_ru, M_ZOMBIE); 344 345 /* 346 * Decrement the count of procs running with this uid. 347 */ 348 (void)chgproccnt(p->p_cred->p_ruid, -1); 349 350 /* 351 * Free up credentials. 352 */ 353 if (--p->p_cred->p_refcnt == 0) { 354 crfree(p->p_cred->pc_ucred); 355 FREE(p->p_cred, M_SUBPROC); 356 } 357 358 /* 359 * Finally finished with old proc entry. 360 * Unlink it from its process group and free it. 361 */ 362 leavepgrp(p); 363 if (*p->p_prev = p->p_nxt) /* off zombproc */ 364 p->p_nxt->p_prev = p->p_prev; 365 if (q = p->p_ysptr) 366 q->p_osptr = p->p_osptr; 367 if (q = p->p_osptr) 368 q->p_ysptr = p->p_ysptr; 369 if ((q = p->p_pptr)->p_cptr == p) 370 q->p_cptr = p->p_osptr; 371 372 /* 373 * Give machine-dependent layer a chance 374 * to free anything that cpu_exit couldn't 375 * release while still running in process context. 376 */ 377 cpu_wait(p); 378 FREE(p, M_PROC); 379 nprocs--; 380 return (0); 381 } 382 if (p->p_stat == SSTOP && (p->p_flag & SWTED) == 0 && 383 (p->p_flag & STRC || uap->options & WUNTRACED)) { 384 p->p_flag |= SWTED; 385 retval[0] = p->p_pid; 386 #ifdef COMPAT_43 387 if (uap->compat) { 388 retval[1] = W_STOPCODE(p->p_xstat); 389 error = 0; 390 } else 391 #endif 392 if (uap->status) { 393 status = W_STOPCODE(p->p_xstat); 394 error = copyout((caddr_t)&status, 395 (caddr_t)uap->status, sizeof(status)); 396 } else 397 error = 0; 398 return (error); 399 } 400 } 401 if (nfound == 0) 402 return (ECHILD); 403 if (uap->options & WNOHANG) { 404 retval[0] = 0; 405 return (0); 406 } 407 if (error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) 408 return (error); 409 goto loop; 410 } 411 412 /* 413 * make process 'parent' the new parent of process 'child'. 414 */ 415 void 416 proc_reparent(child, parent) 417 register struct proc *child; 418 register struct proc *parent; 419 { 420 register struct proc *o; 421 register struct proc *y; 422 423 if (child->p_pptr == parent) 424 return; 425 426 /* fix up the child linkage for the old parent */ 427 o = child->p_osptr; 428 y = child->p_ysptr; 429 if (y) 430 y->p_osptr = o; 431 if (o) 432 o->p_ysptr = y; 433 if (child->p_pptr->p_cptr == child) 434 child->p_pptr->p_cptr = o; 435 436 /* fix up child linkage for new parent */ 437 o = parent->p_cptr; 438 if (o) 439 o->p_ysptr = child; 440 child->p_osptr = o; 441 child->p_ysptr = NULL; 442 parent->p_cptr = child; 443 child->p_pptr = parent; 444 } 445