1 /* $NetBSD: kern_exit.c,v 1.119 2003/08/07 16:31:44 agc Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Copyright (c) 1982, 1986, 1989, 1991, 1993 42 * The Regents of the University of California. All rights reserved. 43 * (c) UNIX System Laboratories, Inc. 44 * All or some portions of this file are derived from material licensed 45 * to the University of California by American Telephone and Telegraph 46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 47 * the permission of UNIX System Laboratories, Inc. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. Neither the name of the University nor the names of its contributors 58 * may be used to endorse or promote products derived from this software 59 * without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 * 73 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95 74 */ 75 76 #include <sys/cdefs.h> 77 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.119 2003/08/07 16:31:44 agc Exp $"); 78 79 #include "opt_ktrace.h" 80 #include "opt_perfctrs.h" 81 #include "opt_systrace.h" 82 #include "opt_sysv.h" 83 84 #include <sys/param.h> 85 #include <sys/systm.h> 86 #include <sys/ioctl.h> 87 #include <sys/proc.h> 88 #include <sys/tty.h> 89 #include <sys/time.h> 90 #include <sys/resource.h> 91 #include <sys/kernel.h> 92 #include <sys/ktrace.h> 93 #include <sys/proc.h> 94 #include <sys/buf.h> 95 #include <sys/wait.h> 96 #include <sys/file.h> 97 #include <sys/vnode.h> 98 #include <sys/syslog.h> 99 #include <sys/malloc.h> 100 #include <sys/pool.h> 101 #include <sys/resourcevar.h> 102 #if defined(PERFCTRS) 103 #include <sys/pmc.h> 104 #endif 105 #include <sys/ptrace.h> 106 #include <sys/acct.h> 107 #include <sys/filedesc.h> 108 #include <sys/ras.h> 109 #include <sys/signalvar.h> 110 #include <sys/sched.h> 111 #include <sys/sa.h> 112 #include <sys/savar.h> 113 #include <sys/mount.h> 114 #include <sys/syscallargs.h> 115 #include <sys/systrace.h> 116 117 #include <machine/cpu.h> 118 119 #include <uvm/uvm_extern.h> 120 121 #define DEBUG_EXIT 122 123 #ifdef DEBUG_EXIT 124 int debug_exit = 0; 125 #define DPRINTF(x) if (debug_exit) printf x 126 #else 127 #define DPRINTF(x) 128 #endif 129 130 static void lwp_exit_hook(struct lwp *, void *); 131 132 /* 133 * exit -- 134 * Death of process. 135 */ 136 int 137 sys_exit(struct lwp *l, void *v, register_t *retval) 138 { 139 struct sys_exit_args /* { 140 syscallarg(int) rval; 141 } */ *uap = v; 142 143 /* Don't call exit1() multiple times in the same process.*/ 144 if (l->l_proc->p_flag & P_WEXIT) 145 lwp_exit(l); 146 147 exit1(l, W_EXITCODE(SCARG(uap, rval), 0)); 148 /* NOTREACHED */ 149 return (0); 150 } 151 152 /* 153 * Exit: deallocate address space and other resources, change proc state 154 * to zombie, and unlink proc from allproc and parent's lists. Save exit 155 * status and rusage for wait(). Check for child processes and orphan them. 156 */ 157 void 158 exit1(struct lwp *l, int rv) 159 { 160 struct proc *p, *q, *nq; 161 int s, sa; 162 163 p = l->l_proc; 164 165 if (__predict_false(p == initproc)) 166 panic("init died (signal %d, exit %d)", 167 WTERMSIG(rv), WEXITSTATUS(rv)); 168 169 p->p_flag |= P_WEXIT; 170 171 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid)); 172 /* 173 * Disable scheduler activation upcalls. 174 * We're trying to get out of here. 175 */ 176 sa = 0; 177 if (p->p_sa != NULL) { 178 179 l->l_flag &= ~L_SA; 180 #if 0 181 p->p_flag &= ~P_SA; 182 #endif 183 sa = 1; 184 } 185 186 #ifdef PGINPROF 187 vmsizmon(); 188 #endif 189 if (p->p_flag & P_PROFIL) 190 stopprofclock(p); 191 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 192 /* 193 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 194 * wake up the parent early to avoid deadlock. 195 */ 196 if (p->p_flag & P_PPWAIT) { 197 p->p_flag &= ~P_PPWAIT; 198 wakeup((caddr_t)p->p_pptr); 199 } 200 sigfillset(&p->p_sigctx.ps_sigignore); 201 sigemptyset(&p->p_sigctx.ps_siglist); 202 p->p_sigctx.ps_sigcheck = 0; 203 timers_free(p, TIMERS_ALL); 204 205 if (sa || (p->p_nlwps > 1)) 206 exit_lwps(l); 207 208 #if defined(__HAVE_RAS) 209 ras_purgeall(p); 210 #endif 211 212 /* 213 * Close open files and release open-file table. 214 * This may block! 215 */ 216 fdfree(p); 217 cwdfree(p); 218 219 doexithooks(p); 220 221 if (SESS_LEADER(p)) { 222 struct session *sp = p->p_session; 223 224 if (sp->s_ttyvp) { 225 /* 226 * Controlling process. 227 * Signal foreground pgrp, 228 * drain controlling terminal 229 * and revoke access to controlling terminal. 230 */ 231 if (sp->s_ttyp->t_session == sp) { 232 if (sp->s_ttyp->t_pgrp) 233 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 234 (void) ttywait(sp->s_ttyp); 235 /* 236 * The tty could have been revoked 237 * if we blocked. 238 */ 239 if (sp->s_ttyvp) 240 VOP_REVOKE(sp->s_ttyvp, REVOKEALL); 241 } 242 if (sp->s_ttyvp) 243 vrele(sp->s_ttyvp); 244 sp->s_ttyvp = NULL; 245 /* 246 * s_ttyp is not zero'd; we use this to indicate 247 * that the session once had a controlling terminal. 248 * (for logging and informational purposes) 249 */ 250 } 251 sp->s_leader = NULL; 252 } 253 fixjobc(p, p->p_pgrp, 0); 254 (void)acct_process(p); 255 #ifdef KTRACE 256 /* 257 * release trace file 258 */ 259 ktrderef(p); 260 #endif 261 #ifdef SYSTRACE 262 systrace_sys_exit(p); 263 #endif 264 /* 265 * If emulation has process exit hook, call it now. 266 */ 267 if (p->p_emul->e_proc_exit) 268 (*p->p_emul->e_proc_exit)(p); 269 270 /* 271 * Give orphaned children to init(8). 272 */ 273 q = LIST_FIRST(&p->p_children); 274 if (q) /* only need this if any child is S_ZOMB */ 275 wakeup((caddr_t)initproc); 276 for (; q != 0; q = nq) { 277 nq = LIST_NEXT(q, p_sibling); 278 279 /* 280 * Traced processes are killed since their existence 281 * means someone is screwing up. Since we reset the 282 * trace flags, the logic in sys_wait4() would not be 283 * triggered to reparent the process to its 284 * original parent, so we must do this here. 285 */ 286 if (q->p_flag & P_TRACED) { 287 if (q->p_opptr != q->p_pptr) { 288 struct proc *t = q->p_opptr; 289 proc_reparent(q, t ? t : initproc); 290 q->p_opptr = NULL; 291 } else 292 proc_reparent(q, initproc); 293 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 294 psignal(q, SIGKILL); 295 } else { 296 proc_reparent(q, initproc); 297 } 298 } 299 300 /* 301 * Reset p_opptr pointer of all former children which got 302 * traced by another process and were reparented. We reset 303 * it to NULL here; the trace detach code then reparents 304 * the child to initproc. We only check allproc list, since 305 * eventual former children on zombproc list won't reference 306 * p_opptr anymore. 307 */ 308 if (p->p_flag & P_CHTRACED) { 309 struct proc *t; 310 311 proclist_lock_read(); 312 313 LIST_FOREACH(t, &allproc, p_list) { 314 if (t->p_opptr == p) 315 t->p_opptr = NULL; 316 } 317 318 proclist_unlock_read(); 319 } 320 321 /* 322 * Save exit status and final rusage info, adding in child rusage 323 * info and self times. 324 * In order to pick up the time for the current execution, we must 325 * do this before unlinking the lwp from l_list. 326 */ 327 p->p_xstat = rv; 328 *p->p_ru = p->p_stats->p_ru; 329 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 330 ruadd(p->p_ru, &p->p_stats->p_cru); 331 332 /* 333 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 334 */ 335 336 /* 337 * Move proc from allproc to zombproc, but do not yet 338 * wake up the reaper. We will put the proc on the 339 * deadproc list later (using the p_dead member), and 340 * wake up the reaper when we do. 341 * Changing the state to SDEAD stops it being found by pfind(). 342 */ 343 s = proclist_lock_write(); 344 p->p_stat = SDEAD; 345 p->p_nrlwps--; 346 l->l_stat = SDEAD; 347 LIST_REMOVE(p, p_list); 348 LIST_INSERT_HEAD(&zombproc, p, p_list); 349 LIST_REMOVE(l, l_list); 350 l->l_flag |= L_DETACHED; 351 proclist_unlock_write(s); 352 353 /* 354 * Notify interested parties of our demise. 355 */ 356 KNOTE(&p->p_klist, NOTE_EXIT); 357 358 #if PERFCTRS 359 /* 360 * Save final PMC information in parent process & clean up. 361 */ 362 if (PMC_ENABLED(p)) { 363 pmc_save_context(p); 364 pmc_accumulate(p->p_pptr, p); 365 pmc_process_exit(p); 366 } 367 #endif 368 369 /* 370 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 371 * flag set, notify init instead (and hope it will handle 372 * this situation). 373 */ 374 if (p->p_pptr->p_flag & P_NOCLDWAIT) { 375 struct proc *pp = p->p_pptr; 376 proc_reparent(p, initproc); 377 /* 378 * If this was the last child of our parent, notify 379 * parent, so in case he was wait(2)ing, he will 380 * continue. 381 */ 382 if (LIST_FIRST(&pp->p_children) == NULL) 383 wakeup((caddr_t)pp); 384 } 385 386 /* 387 * Release the process's signal state. 388 */ 389 sigactsfree(p); 390 391 /* 392 * Clear curlwp after we've done all operations 393 * that could block, and before tearing down the rest 394 * of the process state that might be used from clock, etc. 395 * Also, can't clear curlwp while we're still runnable, 396 * as we're not on a run queue (we are current, just not 397 * a proper proc any longer!). 398 * 399 * Other substructures are freed from wait(). 400 */ 401 curlwp = NULL; 402 limfree(p->p_limit); 403 pstatsfree(p->p_stats); 404 p->p_limit = NULL; 405 406 /* This process no longer needs to hold the kernel lock. */ 407 KERNEL_PROC_UNLOCK(l); 408 409 /* 410 * Finally, call machine-dependent code to switch to a new 411 * context (possibly the idle context). Once we are no longer 412 * using the dead process's vmspace and stack, exit2() will be 413 * called to schedule those resources to be released by the 414 * reaper thread. 415 * 416 * Note that cpu_exit() will end with a call equivalent to 417 * cpu_switch(), finishing our execution (pun intended). 418 */ 419 420 cpu_exit(l, 1); 421 } 422 423 void 424 exit_lwps(struct lwp *l) 425 { 426 struct proc *p; 427 struct lwp *l2; 428 int s, error; 429 lwpid_t waited; 430 431 p = l->l_proc; 432 433 /* XXX SMP 434 * This would be the right place to IPI any LWPs running on 435 * other processors so that they can notice the userret exit hook. 436 */ 437 p->p_userret = lwp_exit_hook; 438 p->p_userret_arg = NULL; 439 440 /* 441 * Make SA-cached LWPs normal process runnable LWPs so that 442 * they'll also self-destruct. 443 */ 444 if (p->p_sa && p->p_sa->sa_ncached > 0) { 445 DPRINTF(("exit_lwps: Making cached LWPs of %d runnable: ", 446 p->p_pid)); 447 SCHED_LOCK(s); 448 while ((l2 = sa_getcachelwp(p)) != 0) { 449 l2->l_priority = l2->l_usrpri; 450 setrunnable(l2); 451 DPRINTF(("%d ", l2->l_lid)); 452 } 453 DPRINTF(("\n")); 454 SCHED_UNLOCK(s); 455 } 456 457 /* 458 * Interrupt LWPs in interruptable sleep, unsuspend suspended 459 * LWPs, make detached LWPs undetached (so we can wait for 460 * them) and then wait for everyone else to finish. 461 */ 462 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 463 #if 0 464 l2->l_flag &= ~(L_DETACHED|L_SA); 465 #endif 466 l2->l_flag &= ~(L_DETACHED); 467 468 if(l2->l_flag & L_SA_WANTS_VP) 469 { 470 wakeup(l2); 471 } 472 473 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) || 474 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) { 475 SCHED_LOCK(s); 476 setrunnable(l2); 477 SCHED_UNLOCK(s); 478 DPRINTF(("exit_lwps: Made %d.%d runnable\n", 479 p->p_pid, l2->l_lid)); 480 } 481 } 482 483 484 while (p->p_nlwps > 1) { 485 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n", 486 p->p_nlwps, p->p_nrlwps, p->p_nzlwps)); 487 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL); 488 if (error) 489 panic("exit_lwps: lwp_wait1 failed with error %d\n", 490 error); 491 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited)); 492 } 493 494 p->p_userret = NULL; 495 } 496 497 /* Wrapper function for use in p_userret */ 498 static void 499 lwp_exit_hook(struct lwp *l, void *arg) 500 { 501 KERNEL_PROC_LOCK(l); 502 lwp_exit(l); 503 } 504 505 /* 506 * We are called from cpu_exit() once it is safe to schedule the 507 * dead process's resources to be freed (i.e., once we've switched to 508 * the idle PCB for the current CPU). 509 * 510 * NOTE: One must be careful with locking in this routine. It's 511 * called from a critical section in machine-dependent code, so 512 * we should refrain from changing any interrupt state. 513 * 514 * We lock the deadproc list (a spin lock), place the proc on that 515 * list (using the p_dead member), and wake up the reaper. 516 */ 517 void 518 exit2(struct lwp *l) 519 { 520 struct proc *p = l->l_proc; 521 522 simple_lock(&deadproc_slock); 523 SLIST_INSERT_HEAD(&deadprocs, p, p_dead); 524 simple_unlock(&deadproc_slock); 525 526 /* lwp_exit2() will wake up deadproc for us. */ 527 lwp_exit2(l); 528 } 529 530 /* 531 * Process reaper. This is run by a kernel thread to free the resources 532 * of a dead process. Once the resources are free, the process becomes 533 * a zombie, and the parent is allowed to read the undead's status. 534 */ 535 void 536 reaper(void *arg) 537 { 538 struct proc *p; 539 struct lwp *l; 540 541 KERNEL_PROC_UNLOCK(curlwp); 542 543 for (;;) { 544 simple_lock(&deadproc_slock); 545 p = SLIST_FIRST(&deadprocs); 546 l = LIST_FIRST(&deadlwp); 547 if (p == NULL && l == NULL) { 548 /* No work for us; go to sleep until someone exits. */ 549 (void) ltsleep(&deadprocs, PVM|PNORELOCK, 550 "reaper", 0, &deadproc_slock); 551 continue; 552 } 553 554 if (l != NULL ) { 555 p = l->l_proc; 556 557 /* Remove lwp from the deadlwp list. */ 558 LIST_REMOVE(l, l_list); 559 simple_unlock(&deadproc_slock); 560 KERNEL_PROC_LOCK(curlwp); 561 562 /* 563 * Give machine-dependent code a chance to free any 564 * resources it couldn't free while still running on 565 * that process's context. This must be done before 566 * uvm_lwp_exit(), in case these resources are in the 567 * PCB. 568 */ 569 cpu_wait(l); 570 571 /* 572 * Free the VM resources we're still holding on to. 573 */ 574 uvm_lwp_exit(l); 575 576 l->l_stat = LSZOMB; 577 if (l->l_flag & L_DETACHED) { 578 /* Nobody waits for detached LWPs. */ 579 LIST_REMOVE(l, l_sibling); 580 p->p_nlwps--; 581 pool_put(&lwp_pool, l); 582 } else { 583 p->p_nzlwps++; 584 wakeup((caddr_t)&p->p_nlwps); 585 } 586 /* XXXNJW where should this be with respect to 587 * the wakeup() above? */ 588 KERNEL_PROC_UNLOCK(curlwp); 589 } else { 590 /* Remove proc from the deadproc list. */ 591 SLIST_REMOVE_HEAD(&deadprocs, p_dead); 592 simple_unlock(&deadproc_slock); 593 KERNEL_PROC_LOCK(curlwp); 594 595 /* 596 * Free the VM resources we're still holding on to. 597 * We must do this from a valid thread because doing 598 * so may block. 599 */ 600 uvm_proc_exit(p); 601 602 /* Process is now a true zombie. */ 603 p->p_stat = SZOMB; 604 605 /* Wake up the parent so it can get exit status. */ 606 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 607 psignal(p->p_pptr, P_EXITSIG(p)); 608 KERNEL_PROC_UNLOCK(curlwp); 609 wakeup((caddr_t)p->p_pptr); 610 } 611 } 612 } 613 614 int 615 sys_wait4(struct lwp *l, void *v, register_t *retval) 616 { 617 struct sys_wait4_args /* { 618 syscallarg(int) pid; 619 syscallarg(int *) status; 620 syscallarg(int) options; 621 syscallarg(struct rusage *) rusage; 622 } */ *uap = v; 623 struct proc *child, *parent; 624 int status, error; 625 626 parent = l->l_proc; 627 628 if (SCARG(uap, pid) == 0) 629 SCARG(uap, pid) = -parent->p_pgid; 630 if (SCARG(uap, options) & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG)) 631 return (EINVAL); 632 633 error = find_stopped_child(parent, SCARG(uap,pid), SCARG(uap,options), 634 &child); 635 if (error != 0) 636 return error; 637 if (child == NULL) { 638 *retval = 0; 639 return 0; 640 } 641 642 retval[0] = child->p_pid; 643 644 if (child->p_stat == SZOMB) { 645 if (SCARG(uap, status)) { 646 status = child->p_xstat; /* convert to int */ 647 error = copyout((caddr_t)&status, 648 (caddr_t)SCARG(uap, status), 649 sizeof(status)); 650 if (error) 651 return (error); 652 } 653 if (SCARG(uap, rusage)) { 654 error = copyout((caddr_t)child->p_ru, 655 (caddr_t)SCARG(uap, rusage), 656 sizeof(struct rusage)); 657 if (error) 658 return (error); 659 } 660 661 proc_free(child); 662 return 0; 663 } 664 665 /* child state must be SSTOP */ 666 if (SCARG(uap, status)) { 667 status = W_STOPCODE(child->p_xstat); 668 return copyout((caddr_t)&status, 669 (caddr_t)SCARG(uap, status), 670 sizeof(status)); 671 } 672 return 0; 673 } 674 675 /* 676 * Scan list of child processes for a child process that has stopped or 677 * exited. Used by sys_wait4 and 'compat' equivalents. 678 */ 679 int 680 find_stopped_child(struct proc *parent, pid_t pid, int options, 681 struct proc **child_p) 682 { 683 struct proc *child; 684 int c_found, error; 685 686 for (;;) { 687 c_found = 0; 688 LIST_FOREACH(child, &parent->p_children, p_sibling) { 689 if (pid != WAIT_ANY && 690 child->p_pid != pid && 691 child->p_pgid != -pid) 692 continue; 693 /* 694 * Wait for processes with p_exitsig != SIGCHLD 695 * processes only if WALTSIG is set; wait for 696 * processes with p_exitsig == SIGCHLD only 697 * if WALTSIG is clear. 698 */ 699 if (((options & WALLSIG) == 0) && 700 (options & WALTSIG ? child->p_exitsig == SIGCHLD 701 : P_EXITSIG(child) != SIGCHLD)) 702 continue; 703 704 c_found = 1; 705 if (child->p_stat == SZOMB && 706 (options & WNOZOMBIE) == 0) { 707 *child_p = child; 708 return 0; 709 } 710 711 if (child->p_stat == SSTOP && 712 (child->p_flag & P_WAITED) == 0 && 713 (child->p_flag & P_TRACED || options & WUNTRACED)) { 714 if ((options & WNOWAIT) == 0) 715 child->p_flag |= P_WAITED; 716 *child_p = child; 717 return 0; 718 } 719 } 720 if (c_found == 0) 721 return ECHILD; 722 if (options & WNOHANG) { 723 *child_p = NULL; 724 return 0; 725 } 726 error = tsleep((caddr_t)parent, PWAIT | PCATCH, "wait", 0); 727 if (error != 0) 728 return error; 729 } 730 } 731 732 /* 733 * Free a process after parent has taken all the state info. 734 */ 735 void 736 proc_free(struct proc *p) 737 { 738 struct proc *parent = p->p_pptr; 739 int s; 740 741 /* 742 * If we got the child via ptrace(2) or procfs, and 743 * the parent is different (meaning the process was 744 * attached, rather than run as a child), then we need 745 * to give it back to the old parent, and send the 746 * parent the exit signal. The rest of the cleanup 747 * will be done when the old parent waits on the child. 748 */ 749 if ((p->p_flag & P_TRACED) && p->p_opptr != parent){ 750 parent = p->p_opptr; 751 if (parent == NULL) 752 parent = initproc; 753 proc_reparent(p, parent); 754 p->p_opptr = NULL; 755 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 756 if (p->p_exitsig != 0) 757 psignal(parent, P_EXITSIG(p)); 758 wakeup((caddr_t)parent); 759 return; 760 } 761 762 scheduler_wait_hook(parent, p); 763 p->p_xstat = 0; 764 765 ruadd(&parent->p_stats->p_cru, p->p_ru); 766 767 /* 768 * At this point we are going to start freeing the final resources. 769 * If anyone tries to access the proc structure after here they 770 * will get a shock - bits are missing. 771 * Attempt to make it hard! 772 */ 773 774 p->p_stat = SIDL; /* not even a zombie any more */ 775 776 pool_put(&rusage_pool, p->p_ru); 777 778 /* 779 * Finally finished with old proc entry. 780 * Unlink it from its process group and free it. 781 */ 782 leavepgrp(p); 783 784 s = proclist_lock_write(); 785 LIST_REMOVE(p, p_list); /* off zombproc */ 786 LIST_REMOVE(p, p_sibling); 787 proclist_unlock_write(s); 788 789 /* 790 * Decrement the count of procs running with this uid. 791 */ 792 (void)chgproccnt(p->p_cred->p_ruid, -1); 793 794 /* 795 * Free up credentials. 796 */ 797 if (--p->p_cred->p_refcnt == 0) { 798 crfree(p->p_cred->pc_ucred); 799 pool_put(&pcred_pool, p->p_cred); 800 } 801 802 /* 803 * Release reference to text vnode 804 */ 805 if (p->p_textvp) 806 vrele(p->p_textvp); 807 808 /* 809 * Release any SA state 810 */ 811 if (p->p_sa) { 812 free(p->p_sa->sa_stacks, M_SA); 813 pool_put(&sadata_pool, p->p_sa); 814 } 815 816 /* Free proc structure and let pid be reallocated */ 817 proc_free_mem(p); 818 } 819 820 /* 821 * make process 'parent' the new parent of process 'child'. 822 */ 823 void 824 proc_reparent(struct proc *child, struct proc *parent) 825 { 826 827 if (child->p_pptr == parent) 828 return; 829 830 if (parent == initproc) 831 child->p_exitsig = SIGCHLD; 832 833 LIST_REMOVE(child, p_sibling); 834 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 835 child->p_pptr = parent; 836 } 837