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