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