1 /* $OpenBSD: kern_exit.c,v 1.93 2010/06/29 00:28:14 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 #include <sys/mutex.h> 63 #ifdef SYSVSEM 64 #include <sys/sem.h> 65 #endif 66 67 #include "systrace.h" 68 #include <dev/systrace.h> 69 70 #include <sys/mount.h> 71 #include <sys/syscallargs.h> 72 73 #include <machine/cpu.h> 74 75 #include <uvm/uvm_extern.h> 76 77 /* 78 * exit -- 79 * Death of process. 80 */ 81 int 82 sys_exit(struct proc *p, void *v, register_t *retval) 83 { 84 struct sys_exit_args /* { 85 syscallarg(int) rval; 86 } */ *uap = v; 87 88 exit1(p, W_EXITCODE(SCARG(uap, rval), 0), EXIT_NORMAL); 89 /* NOTREACHED */ 90 return (0); 91 } 92 93 int 94 sys_threxit(struct proc *p, void *v, register_t *retval) 95 { 96 struct sys_threxit_args /* { 97 syscallarg(pid_t *) notdead; 98 } */ *uap = v; 99 100 if (!rthreads_enabled) 101 return (EINVAL); 102 103 if (SCARG(uap, notdead) != NULL) { 104 pid_t zero = 0; 105 if (copyout(&zero, SCARG(uap, notdead), sizeof(zero))) { 106 psignal(p, SIGSEGV); 107 } 108 } 109 exit1(p, 0, EXIT_THREAD); 110 111 return (0); 112 } 113 114 /* 115 * Exit: deallocate address space and other resources, change proc state 116 * to zombie, and unlink proc from allproc and parent's lists. Save exit 117 * status and rusage for wait(). Check for child processes and orphan them. 118 */ 119 void 120 exit1(struct proc *p, int rv, int flags) 121 { 122 struct proc *q, *nq; 123 124 if (p->p_pid == 1) 125 panic("init died (signal %d, exit %d)", 126 WTERMSIG(rv), WEXITSTATUS(rv)); 127 128 /* unlink ourselves from the active threads */ 129 TAILQ_REMOVE(&p->p_p->ps_threads, p, p_thr_link); 130 if (TAILQ_EMPTY(&p->p_p->ps_threads)) 131 wakeup(&p->p_p->ps_threads); 132 /* 133 * if one thread calls exit, we take down everybody. 134 * we have to be careful not to get recursively caught. 135 * this is kinda sick. 136 */ 137 if (flags == EXIT_NORMAL && (p->p_flag & P_THREAD) && 138 (p->p_p->ps_mainproc->p_flag & P_WEXIT) == 0) { 139 /* 140 * we are one of the threads. we SIGKILL the parent, 141 * it will wake us up again, then we proceed. 142 */ 143 atomic_setbits_int(&p->p_p->ps_mainproc->p_flag, P_IGNEXITRV); 144 p->p_p->ps_mainproc->p_xstat = rv; 145 ptsignal(p->p_p->ps_mainproc, SIGKILL, SPROPAGATED); 146 tsleep(p->p_p, PUSER, "thrdying", 0); 147 } else if ((p->p_flag & P_THREAD) == 0) { 148 atomic_setbits_int(&p->p_flag, P_WEXIT); 149 if (flags == EXIT_NORMAL) { 150 q = TAILQ_FIRST(&p->p_p->ps_threads); 151 for (; q != NULL; q = nq) { 152 nq = TAILQ_NEXT(q, p_thr_link); 153 atomic_setbits_int(&q->p_flag, P_IGNEXITRV); 154 q->p_xstat = rv; 155 ptsignal(q, SIGKILL, SPROPAGATED); 156 } 157 } 158 wakeup(p->p_p); 159 while (!TAILQ_EMPTY(&p->p_p->ps_threads)) 160 tsleep(&p->p_p->ps_threads, PUSER, "thrdeath", 0); 161 } 162 163 if (p->p_flag & P_PROFIL) 164 stopprofclock(p); 165 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 166 /* 167 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 168 * wake up the parent early to avoid deadlock. 169 */ 170 atomic_setbits_int(&p->p_flag, P_WEXIT); 171 if (p->p_flag & P_PPWAIT) { 172 atomic_clearbits_int(&p->p_flag, P_PPWAIT); 173 wakeup(p->p_pptr); 174 } 175 p->p_sigignore = ~0; 176 p->p_siglist = 0; 177 timeout_del(&p->p_realit_to); 178 timeout_del(&p->p_stats->p_virt_to); 179 timeout_del(&p->p_stats->p_prof_to); 180 181 /* 182 * Close open files and release open-file table. 183 */ 184 fdfree(p); 185 186 #ifdef SYSVSEM 187 if ((p->p_flag & P_THREAD) == 0) 188 semexit(p->p_p); 189 #endif 190 if (SESS_LEADER(p)) { 191 struct session *sp = p->p_session; 192 193 if (sp->s_ttyvp) { 194 /* 195 * Controlling process. 196 * Signal foreground pgrp, 197 * drain controlling terminal 198 * and revoke access to controlling terminal. 199 */ 200 if (sp->s_ttyp->t_session == sp) { 201 if (sp->s_ttyp->t_pgrp) 202 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 203 (void) ttywait(sp->s_ttyp); 204 /* 205 * The tty could have been revoked 206 * if we blocked. 207 */ 208 if (sp->s_ttyvp) 209 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 210 } 211 if (sp->s_ttyvp) 212 vrele(sp->s_ttyvp); 213 sp->s_ttyvp = NULL; 214 /* 215 * s_ttyp is not zero'd; we use this to indicate 216 * that the session once had a controlling terminal. 217 * (for logging and informational purposes) 218 */ 219 } 220 sp->s_leader = NULL; 221 } 222 fixjobc(p, p->p_pgrp, 0); 223 #ifdef ACCOUNTING 224 (void)acct_process(p); 225 #endif 226 #ifdef KTRACE 227 /* 228 * release trace file 229 */ 230 p->p_traceflag = 0; /* don't trace the vrele() */ 231 if (p->p_tracep) 232 ktrsettracevnode(p, NULL); 233 #endif 234 #if NSYSTRACE > 0 235 if (ISSET(p->p_flag, P_SYSTRACE)) 236 systrace_exit(p); 237 #endif 238 /* 239 * Remove proc from pidhash chain so looking it up won't 240 * work. Move it from allproc to zombproc, but do not yet 241 * wake up the reaper. We will put the proc on the 242 * deadproc list later (using the p_hash member), and 243 * wake up the reaper when we do. 244 */ 245 rw_enter_write(&allproclk); 246 /* 247 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 248 */ 249 p->p_stat = SDEAD; 250 251 LIST_REMOVE(p, p_hash); 252 LIST_REMOVE(p, p_list); 253 LIST_INSERT_HEAD(&zombproc, p, p_list); 254 rw_exit_write(&allproclk); 255 256 /* 257 * Give orphaned children to init(8). 258 */ 259 q = LIST_FIRST(&p->p_children); 260 if (q) /* only need this if any child is S_ZOMB */ 261 wakeup(initproc); 262 for (; q != 0; q = nq) { 263 nq = LIST_NEXT(q, p_sibling); 264 proc_reparent(q, initproc); 265 /* 266 * Traced processes are killed 267 * since their existence means someone is screwing up. 268 */ 269 if (q->p_flag & P_TRACED) { 270 atomic_clearbits_int(&q->p_flag, P_TRACED); 271 psignal(q, SIGKILL); 272 } 273 } 274 275 276 /* 277 * Save exit status and final rusage info, adding in child rusage 278 * info and self times. 279 */ 280 if (!(p->p_flag & P_IGNEXITRV)) 281 p->p_xstat = rv; 282 *p->p_ru = p->p_stats->p_ru; 283 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 284 ruadd(p->p_ru, &p->p_stats->p_cru); 285 286 /* 287 * clear %cpu usage during swap 288 */ 289 p->p_pctcpu = 0; 290 291 /* 292 * notify interested parties of our demise. 293 */ 294 if (p == p->p_p->ps_mainproc) 295 KNOTE(&p->p_p->ps_klist, NOTE_EXIT); 296 297 /* 298 * Notify parent that we're gone. If we have P_NOZOMBIE or parent has 299 * the P_NOCLDWAIT flag set, notify process 1 instead (and hope it 300 * will handle this situation). 301 */ 302 if ((p->p_flag & P_NOZOMBIE) || (p->p_pptr->p_flag & P_NOCLDWAIT)) { 303 struct proc *pp = p->p_pptr; 304 proc_reparent(p, initproc); 305 /* 306 * If this was the last child of our parent, notify 307 * parent, so in case he was wait(2)ing, he will 308 * continue. 309 */ 310 if (LIST_EMPTY(&pp->p_children)) 311 wakeup(pp); 312 } 313 314 /* 315 * Release the process's signal state. 316 */ 317 sigactsfree(p); 318 319 /* 320 * Other substructures are freed from reaper and wait(). 321 */ 322 323 /* 324 * If emulation has process exit hook, call it now. 325 */ 326 if (p->p_emul->e_proc_exit) 327 (*p->p_emul->e_proc_exit)(p); 328 329 /* 330 * Finally, call machine-dependent code to switch to a new 331 * context (possibly the idle context). Once we are no longer 332 * using the dead process's vmspace and stack, exit2() will be 333 * called to schedule those resources to be released by the 334 * reaper thread. 335 * 336 * Note that cpu_exit() will end with a call equivalent to 337 * cpu_switch(), finishing our execution (pun intended). 338 */ 339 uvmexp.swtch++; 340 cpu_exit(p); 341 panic("cpu_exit returned"); 342 } 343 344 /* 345 * Locking of this proclist is special; it's accessed in a 346 * critical section of process exit, and thus locking it can't 347 * modify interrupt state. We use a simple spin lock for this 348 * proclist. Processes on this proclist are also on zombproc; 349 * we use the p_hash member to linkup to deadproc. 350 */ 351 struct mutex deadproc_mutex = MUTEX_INITIALIZER(IPL_NONE); 352 struct proclist deadproc = LIST_HEAD_INITIALIZER(deadproc); 353 354 /* 355 * We are called from cpu_exit() once it is safe to schedule the 356 * dead process's resources to be freed. 357 * 358 * NOTE: One must be careful with locking in this routine. It's 359 * called from a critical section in machine-dependent code, so 360 * we should refrain from changing any interrupt state. 361 * 362 * We lock the deadproc list, place the proc on that list (using 363 * the p_hash member), and wake up the reaper. 364 */ 365 void 366 exit2(struct proc *p) 367 { 368 mtx_enter(&deadproc_mutex); 369 LIST_INSERT_HEAD(&deadproc, p, p_hash); 370 mtx_leave(&deadproc_mutex); 371 372 wakeup(&deadproc); 373 } 374 375 /* 376 * Process reaper. This is run by a kernel thread to free the resources 377 * of a dead process. Once the resources are free, the process becomes 378 * a zombie, and the parent is allowed to read the undead's status. 379 */ 380 void 381 reaper(void) 382 { 383 struct proc *p; 384 385 KERNEL_PROC_UNLOCK(curproc); 386 387 SCHED_ASSERT_UNLOCKED(); 388 389 for (;;) { 390 mtx_enter(&deadproc_mutex); 391 while ((p = LIST_FIRST(&deadproc)) == NULL) 392 msleep(&deadproc, &deadproc_mutex, PVM, "reaper", 0); 393 394 /* Remove us from the deadproc list. */ 395 LIST_REMOVE(p, p_hash); 396 mtx_leave(&deadproc_mutex); 397 398 KERNEL_PROC_LOCK(curproc); 399 400 /* 401 * Free the VM resources we're still holding on to. 402 * We must do this from a valid thread because doing 403 * so may block. 404 */ 405 uvm_exit(p); 406 407 /* Process is now a true zombie. */ 408 if ((p->p_flag & P_NOZOMBIE) == 0) { 409 p->p_stat = SZOMB; 410 411 if (P_EXITSIG(p) != 0) 412 psignal(p->p_pptr, P_EXITSIG(p)); 413 /* Wake up the parent so it can get exit status. */ 414 wakeup(p->p_pptr); 415 } else { 416 /* Noone will wait for us. Just zap the process now */ 417 proc_zap(p); 418 } 419 420 KERNEL_PROC_UNLOCK(curproc); 421 } 422 } 423 424 pid_t 425 sys_wait4(struct proc *q, void *v, register_t *retval) 426 { 427 struct sys_wait4_args /* { 428 syscallarg(pid_t) pid; 429 syscallarg(int *) status; 430 syscallarg(int) options; 431 syscallarg(struct rusage *) rusage; 432 } */ *uap = v; 433 int nfound; 434 struct proc *p; 435 int status, error; 436 437 if (SCARG(uap, pid) == 0) 438 SCARG(uap, pid) = -q->p_pgid; 439 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG|WCONTINUED)) 440 return (EINVAL); 441 442 loop: 443 nfound = 0; 444 LIST_FOREACH(p, &q->p_children, p_sibling) { 445 if ((p->p_flag & P_NOZOMBIE) || 446 (SCARG(uap, pid) != WAIT_ANY && 447 p->p_pid != SCARG(uap, pid) && 448 p->p_pgid != -SCARG(uap, pid))) 449 continue; 450 451 /* 452 * Wait for processes with p_exitsig != SIGCHLD processes only 453 * if WALTSIG is set; wait for processes with pexitsig == 454 * SIGCHLD only if WALTSIG is clear. 455 */ 456 if ((SCARG(uap, options) & WALTSIG) ? 457 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD)) 458 continue; 459 460 nfound++; 461 if (p->p_stat == SZOMB) { 462 retval[0] = p->p_pid; 463 464 if (SCARG(uap, status)) { 465 status = p->p_xstat; /* convert to int */ 466 error = copyout(&status, 467 SCARG(uap, status), sizeof(status)); 468 if (error) 469 return (error); 470 } 471 if (SCARG(uap, rusage) && 472 (error = copyout(p->p_ru, 473 SCARG(uap, rusage), sizeof(struct rusage)))) 474 return (error); 475 proc_finish_wait(q, p); 476 return (0); 477 } 478 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 479 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 480 atomic_setbits_int(&p->p_flag, P_WAITED); 481 retval[0] = p->p_pid; 482 483 if (SCARG(uap, status)) { 484 status = W_STOPCODE(p->p_xstat); 485 error = copyout(&status, SCARG(uap, status), 486 sizeof(status)); 487 } else 488 error = 0; 489 return (error); 490 } 491 if ((SCARG(uap, options) & WCONTINUED) && (p->p_flag & P_CONTINUED)) { 492 atomic_clearbits_int(&p->p_flag, P_CONTINUED); 493 retval[0] = p->p_pid; 494 495 if (SCARG(uap, status)) { 496 status = _WCONTINUED; 497 error = copyout(&status, SCARG(uap, status), 498 sizeof(status)); 499 } else 500 error = 0; 501 return (error); 502 } 503 } 504 if (nfound == 0) 505 return (ECHILD); 506 if (SCARG(uap, options) & WNOHANG) { 507 retval[0] = 0; 508 return (0); 509 } 510 if ((error = tsleep(q, PWAIT | PCATCH, "wait", 0)) != 0) 511 return (error); 512 goto loop; 513 } 514 515 void 516 proc_finish_wait(struct proc *waiter, struct proc *p) 517 { 518 struct proc *t; 519 520 /* 521 * If we got the child via a ptrace 'attach', 522 * we need to give it back to the old parent. 523 */ 524 if (p->p_oppid && (t = pfind(p->p_oppid))) { 525 atomic_clearbits_int(&p->p_flag, P_TRACED); 526 p->p_oppid = 0; 527 proc_reparent(p, t); 528 if (p->p_exitsig != 0) 529 psignal(t, p->p_exitsig); 530 wakeup(t); 531 } else { 532 scheduler_wait_hook(waiter, p); 533 p->p_xstat = 0; 534 ruadd(&waiter->p_stats->p_cru, p->p_ru); 535 proc_zap(p); 536 } 537 } 538 539 /* 540 * make process 'parent' the new parent of process 'child'. 541 */ 542 void 543 proc_reparent(struct proc *child, struct proc *parent) 544 { 545 546 if (child->p_pptr == parent) 547 return; 548 549 if (parent == initproc) 550 child->p_exitsig = SIGCHLD; 551 552 LIST_REMOVE(child, p_sibling); 553 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 554 child->p_pptr = parent; 555 } 556 557 void 558 proc_zap(struct proc *p) 559 { 560 pool_put(&rusage_pool, p->p_ru); 561 if (p->p_ptstat) 562 free(p->p_ptstat, M_SUBPROC); 563 564 /* 565 * Finally finished with old proc entry. 566 * Unlink it from its process group and free it. 567 */ 568 leavepgrp(p); 569 rw_enter_write(&allproclk); 570 LIST_REMOVE(p, p_list); /* off zombproc */ 571 rw_exit_write(&allproclk); 572 LIST_REMOVE(p, p_sibling); 573 574 /* 575 * Decrement the count of procs running with this uid. 576 */ 577 (void)chgproccnt(p->p_cred->p_ruid, -1); 578 579 /* 580 * Release reference to text vnode 581 */ 582 if (p->p_textvp) 583 vrele(p->p_textvp); 584 585 /* 586 * Remove us from our process list, possibly killing the process 587 * in the process (pun intended). 588 */ 589 if (--p->p_p->ps_refcnt == 0) { 590 KASSERT(TAILQ_EMPTY(&p->p_p->ps_threads)); 591 limfree(p->p_p->ps_limit); 592 if (--p->p_p->ps_cred->p_refcnt == 0) { 593 crfree(p->p_p->ps_cred->pc_ucred); 594 pool_put(&pcred_pool, p->p_p->ps_cred); 595 } 596 pool_put(&process_pool, p->p_p); 597 } 598 599 pool_put(&proc_pool, p); 600 nprocs--; 601 } 602 603