1 /* $OpenBSD: kern_exit.c,v 1.49 2004/03/20 19:55:50 tedu Exp $ */ 2 /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/ioctl.h> 43 #include <sys/proc.h> 44 #include <sys/tty.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 #include <sys/kernel.h> 48 #include <sys/buf.h> 49 #include <sys/wait.h> 50 #include <sys/file.h> 51 #include <sys/vnode.h> 52 #include <sys/syslog.h> 53 #include <sys/malloc.h> 54 #include <sys/resourcevar.h> 55 #include <sys/ptrace.h> 56 #include <sys/acct.h> 57 #include <sys/filedesc.h> 58 #include <sys/signalvar.h> 59 #include <sys/sched.h> 60 #include <sys/ktrace.h> 61 #include <sys/pool.h> 62 #ifdef SYSVSHM 63 #include <sys/shm.h> 64 #endif 65 #ifdef SYSVSEM 66 #include <sys/sem.h> 67 #endif 68 69 #include "systrace.h" 70 #include <dev/systrace.h> 71 72 #include <sys/mount.h> 73 #include <sys/syscallargs.h> 74 75 #include <machine/cpu.h> 76 77 #include <uvm/uvm_extern.h> 78 79 /* 80 * exit -- 81 * Death of process. 82 */ 83 int 84 sys_exit(p, v, retval) 85 struct proc *p; 86 void *v; 87 register_t *retval; 88 { 89 struct sys_exit_args /* { 90 syscallarg(int) rval; 91 } */ *uap = v; 92 93 exit1(p, W_EXITCODE(SCARG(uap, rval), 0)); 94 /* NOTREACHED */ 95 return (0); 96 } 97 98 /* 99 * Exit: deallocate address space and other resources, change proc state 100 * to zombie, and unlink proc from allproc and parent's lists. Save exit 101 * status and rusage for wait(). Check for child processes and orphan them. 102 */ 103 void 104 exit1(p, rv) 105 struct proc *p; 106 int rv; 107 { 108 struct proc *q, *nq; 109 110 if (p->p_pid == 1) 111 panic("init died (signal %d, exit %d)", 112 WTERMSIG(rv), WEXITSTATUS(rv)); 113 114 if (p->p_flag & P_PROFIL) 115 stopprofclock(p); 116 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 117 /* 118 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 119 * wake up the parent early to avoid deadlock. 120 */ 121 p->p_flag |= P_WEXIT; 122 p->p_flag &= ~P_TRACED; 123 if (p->p_flag & P_PPWAIT) { 124 p->p_flag &= ~P_PPWAIT; 125 wakeup(p->p_pptr); 126 } 127 p->p_sigignore = ~0; 128 p->p_siglist = 0; 129 timeout_del(&p->p_realit_to); 130 131 /* 132 * Close open files and release open-file table. 133 * This may block! 134 */ 135 fdfree(p); 136 137 #ifdef SYSVSEM 138 semexit(p); 139 #endif 140 if (SESS_LEADER(p)) { 141 register struct session *sp = p->p_session; 142 143 if (sp->s_ttyvp) { 144 /* 145 * Controlling process. 146 * Signal foreground pgrp, 147 * drain controlling terminal 148 * and revoke access to controlling terminal. 149 */ 150 if (sp->s_ttyp->t_session == sp) { 151 if (sp->s_ttyp->t_pgrp) 152 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 153 (void) ttywait(sp->s_ttyp); 154 /* 155 * The tty could have been revoked 156 * if we blocked. 157 */ 158 if (sp->s_ttyvp) 159 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 160 } 161 if (sp->s_ttyvp) 162 vrele(sp->s_ttyvp); 163 sp->s_ttyvp = NULL; 164 /* 165 * s_ttyp is not zero'd; we use this to indicate 166 * that the session once had a controlling terminal. 167 * (for logging and informational purposes) 168 */ 169 } 170 sp->s_leader = NULL; 171 } 172 fixjobc(p, p->p_pgrp, 0); 173 (void)acct_process(p); 174 #ifdef KTRACE 175 /* 176 * release trace file 177 */ 178 p->p_traceflag = 0; /* don't trace the vrele() */ 179 if (p->p_tracep) 180 ktrsettracevnode(p, NULL); 181 #endif 182 #if NSYSTRACE > 0 183 if (ISSET(p->p_flag, P_SYSTRACE)) 184 systrace_exit(p); 185 #endif 186 /* 187 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 188 */ 189 p->p_stat = SDEAD; 190 191 /* 192 * Remove proc from pidhash chain so looking it up won't 193 * work. Move it from allproc to zombproc, but do not yet 194 * wake up the reaper. We will put the proc on the 195 * deadproc list later (using the p_hash member), and 196 * wake up the reaper when we do. 197 */ 198 LIST_REMOVE(p, p_hash); 199 LIST_REMOVE(p, p_list); 200 LIST_INSERT_HEAD(&zombproc, p, p_list); 201 202 /* 203 * Give orphaned children to init(8). 204 */ 205 q = p->p_children.lh_first; 206 if (q) /* only need this if any child is S_ZOMB */ 207 wakeup(initproc); 208 for (; q != 0; q = nq) { 209 nq = q->p_sibling.le_next; 210 proc_reparent(q, initproc); 211 /* 212 * Traced processes are killed 213 * since their existence means someone is screwing up. 214 */ 215 if (q->p_flag & P_TRACED) { 216 q->p_flag &= ~P_TRACED; 217 psignal(q, SIGKILL); 218 } 219 } 220 221 /* 222 * Save exit status and final rusage info, adding in child rusage 223 * info and self times. 224 */ 225 p->p_xstat = rv; 226 *p->p_ru = p->p_stats->p_ru; 227 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 228 ruadd(p->p_ru, &p->p_stats->p_cru); 229 230 /* 231 * clear %cpu usage during swap 232 */ 233 p->p_pctcpu = 0; 234 235 /* 236 * notify interested parties of our demise. 237 */ 238 KNOTE(&p->p_klist, NOTE_EXIT); 239 240 /* 241 * Notify parent that we're gone. If we have P_NOZOMBIE or parent has 242 * the P_NOCLDWAIT flag set, notify process 1 instead (and hope it 243 * will handle this situation). 244 */ 245 if ((p->p_flag & P_NOZOMBIE) || (p->p_pptr->p_flag & P_NOCLDWAIT)) { 246 struct proc *pp = p->p_pptr; 247 proc_reparent(p, initproc); 248 /* 249 * If this was the last child of our parent, notify 250 * parent, so in case he was wait(2)ing, he will 251 * continue. 252 */ 253 if (pp->p_children.lh_first == NULL) 254 wakeup(pp); 255 } 256 257 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 258 psignal(p->p_pptr, P_EXITSIG(p)); 259 wakeup(p->p_pptr); 260 261 /* 262 * Notify procfs debugger 263 */ 264 if (p->p_flag & P_FSTRACE) 265 wakeup(p); 266 267 /* 268 * Release the process's signal state. 269 */ 270 sigactsfree(p); 271 272 /* 273 * Clear curproc after we've done all operations 274 * that could block, and before tearing down the rest 275 * of the process state that might be used from clock, etc. 276 * Also, can't clear curproc while we're still runnable, 277 * as we're not on a run queue (we are current, just not 278 * a proper proc any longer!). 279 * 280 * Other substructures are freed from wait(). 281 */ 282 curproc = NULL; 283 limfree(p->p_limit); 284 p->p_limit = NULL; 285 286 /* 287 * If emulation has process exit hook, call it now. 288 */ 289 if (p->p_emul->e_proc_exit) 290 (*p->p_emul->e_proc_exit)(p); 291 292 /* 293 * Finally, call machine-dependent code to switch to a new 294 * context (possibly the idle context). Once we are no longer 295 * using the dead process's vmspace and stack, exit2() will be 296 * called to schedule those resources to be released by the 297 * reaper thread. 298 * 299 * Note that cpu_exit() will end with a call equivalent to 300 * cpu_switch(), finishing our execution (pun intended). 301 */ 302 cpu_exit(p); 303 } 304 305 /* 306 * We are called from cpu_exit() once it is safe to schedule the 307 * dead process's resources to be freed. 308 * 309 * NOTE: One must be careful with locking in this routine. It's 310 * called from a critical section in machine-dependent code, so 311 * we should refrain from changing any interrupt state. 312 * 313 * We lock the deadproc list (a spin lock), place the proc on that 314 * list (using the p_hash member), and wake up the reaper. 315 */ 316 void 317 exit2(p) 318 struct proc *p; 319 { 320 321 simple_lock(&deadproc_slock); 322 LIST_INSERT_HEAD(&deadproc, p, p_hash); 323 simple_unlock(&deadproc_slock); 324 325 wakeup(&deadproc); 326 } 327 328 /* 329 * Process reaper. This is run by a kernel thread to free the resources 330 * of a dead process. Once the resources are free, the process becomes 331 * a zombie, and the parent is allowed to read the undead's status. 332 */ 333 void 334 reaper(void) 335 { 336 struct proc *p; 337 338 for (;;) { 339 simple_lock(&deadproc_slock); 340 p = LIST_FIRST(&deadproc); 341 if (p == NULL) { 342 /* No work for us; go to sleep until someone exits. */ 343 simple_unlock(&deadproc_slock); 344 (void) tsleep(&deadproc, PVM, "reaper", 0); 345 continue; 346 } 347 348 /* Remove us from the deadproc list. */ 349 LIST_REMOVE(p, p_hash); 350 simple_unlock(&deadproc_slock); 351 352 /* 353 * Give machine-dependent code a chance to free any 354 * resources it couldn't free while still running on 355 * that process's context. This must be done before 356 * uvm_exit(), in case these resources are in the PCB. 357 */ 358 cpu_wait(p); 359 360 /* 361 * Free the VM resources we're still holding on to. 362 * We must do this from a valid thread because doing 363 * so may block. 364 */ 365 uvm_exit(p); 366 367 /* Process is now a true zombie. */ 368 if ((p->p_flag & P_NOZOMBIE) == 0) { 369 p->p_stat = SZOMB; 370 371 /* Wake up the parent so it can get exit status. */ 372 psignal(p->p_pptr, SIGCHLD); 373 wakeup(p->p_pptr); 374 } else { 375 /* Noone will wait for us. Just zap the process now */ 376 proc_zap(p); 377 } 378 } 379 } 380 381 pid_t 382 sys_wait4(q, v, retval) 383 register struct proc *q; 384 void *v; 385 register_t *retval; 386 { 387 register struct sys_wait4_args /* { 388 syscallarg(pid_t) pid; 389 syscallarg(int *) status; 390 syscallarg(int) options; 391 syscallarg(struct rusage *) rusage; 392 } */ *uap = v; 393 register int nfound; 394 register struct proc *p, *t; 395 int status, error; 396 397 if (SCARG(uap, pid) == 0) 398 SCARG(uap, pid) = -q->p_pgid; 399 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG|WCONTINUED)) 400 return (EINVAL); 401 402 loop: 403 nfound = 0; 404 for (p = q->p_children.lh_first; p != 0; p = p->p_sibling.le_next) { 405 if ((p->p_flag & P_NOZOMBIE) || 406 (SCARG(uap, pid) != WAIT_ANY && 407 p->p_pid != SCARG(uap, pid) && 408 p->p_pgid != -SCARG(uap, pid))) 409 continue; 410 411 /* 412 * Wait for processes with p_exitsig != SIGCHLD processes only 413 * if WALTSIG is set; wait for processes with pexitsig == 414 * SIGCHLD only if WALTSIG is clear. 415 */ 416 if ((SCARG(uap, options) & WALTSIG) ? 417 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD)) 418 continue; 419 420 nfound++; 421 if (p->p_stat == SZOMB) { 422 retval[0] = p->p_pid; 423 424 if (SCARG(uap, status)) { 425 status = p->p_xstat; /* convert to int */ 426 error = copyout(&status, 427 SCARG(uap, status), sizeof(status)); 428 if (error) 429 return (error); 430 } 431 if (SCARG(uap, rusage) && 432 (error = copyout(p->p_ru, 433 SCARG(uap, rusage), sizeof(struct rusage)))) 434 return (error); 435 436 /* 437 * If we got the child via a ptrace 'attach', 438 * we need to give it back to the old parent. 439 */ 440 if (p->p_oppid && (t = pfind(p->p_oppid))) { 441 p->p_oppid = 0; 442 proc_reparent(p, t); 443 if (p->p_exitsig != 0) 444 psignal(t, P_EXITSIG(p)); 445 wakeup(t); 446 return (0); 447 } 448 449 scheduler_wait_hook(q, p); 450 p->p_xstat = 0; 451 ruadd(&q->p_stats->p_cru, p->p_ru); 452 453 proc_zap(p); 454 455 return (0); 456 } 457 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 458 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 459 p->p_flag |= P_WAITED; 460 retval[0] = p->p_pid; 461 462 if (SCARG(uap, status)) { 463 status = W_STOPCODE(p->p_xstat); 464 error = copyout(&status, SCARG(uap, status), 465 sizeof(status)); 466 } else 467 error = 0; 468 return (error); 469 } 470 if ((SCARG(uap, options) & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 471 p->p_flag &= ~P_CONTINUED; 472 retval[0] = p->p_pid; 473 474 if (SCARG(uap, status)) { 475 status = _WCONTINUED; 476 error = copyout(&status, SCARG(uap, status), 477 sizeof(status)); 478 } else 479 error = 0; 480 return (error); 481 } 482 } 483 if (nfound == 0) 484 return (ECHILD); 485 if (SCARG(uap, options) & WNOHANG) { 486 retval[0] = 0; 487 return (0); 488 } 489 if ((error = tsleep(q, PWAIT | PCATCH, "wait", 0)) != 0) 490 return (error); 491 goto loop; 492 } 493 494 /* 495 * make process 'parent' the new parent of process 'child'. 496 */ 497 void 498 proc_reparent(child, parent) 499 register struct proc *child; 500 register struct proc *parent; 501 { 502 503 if (child->p_pptr == parent) 504 return; 505 506 if (parent == initproc) 507 child->p_exitsig = SIGCHLD; 508 509 LIST_REMOVE(child, p_sibling); 510 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 511 child->p_pptr = parent; 512 } 513 514 void 515 proc_zap(struct proc *p) 516 { 517 pool_put(&rusage_pool, p->p_ru); 518 519 /* 520 * Finally finished with old proc entry. 521 * Unlink it from its process group and free it. 522 */ 523 leavepgrp(p); 524 LIST_REMOVE(p, p_list); /* off zombproc */ 525 LIST_REMOVE(p, p_sibling); 526 527 /* 528 * Decrement the count of procs running with this uid. 529 */ 530 (void)chgproccnt(p->p_cred->p_ruid, -1); 531 532 /* 533 * Free up credentials. 534 */ 535 if (--p->p_cred->p_refcnt == 0) { 536 crfree(p->p_cred->pc_ucred); 537 pool_put(&pcred_pool, p->p_cred); 538 } 539 540 /* 541 * Release reference to text vnode 542 */ 543 if (p->p_textvp) 544 vrele(p->p_textvp); 545 546 pool_put(&proc_pool, p); 547 nprocs--; 548 } 549 550