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