1 /* $NetBSD: kern_exit.c,v 1.108 2003/01/27 20:30:32 nathanw 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. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 * 77 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95 78 */ 79 80 #include <sys/cdefs.h> 81 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.108 2003/01/27 20:30:32 nathanw Exp $"); 82 83 #include "opt_ktrace.h" 84 #include "opt_perfctrs.h" 85 #include "opt_systrace.h" 86 #include "opt_sysv.h" 87 88 #include <sys/param.h> 89 #include <sys/systm.h> 90 #include <sys/ioctl.h> 91 #include <sys/proc.h> 92 #include <sys/tty.h> 93 #include <sys/time.h> 94 #include <sys/resource.h> 95 #include <sys/kernel.h> 96 #include <sys/ktrace.h> 97 #include <sys/proc.h> 98 #include <sys/buf.h> 99 #include <sys/wait.h> 100 #include <sys/file.h> 101 #include <sys/vnode.h> 102 #include <sys/syslog.h> 103 #include <sys/malloc.h> 104 #include <sys/pool.h> 105 #include <sys/resourcevar.h> 106 #if defined(PERFCTRS) 107 #include <sys/pmc.h> 108 #endif 109 #include <sys/ptrace.h> 110 #include <sys/acct.h> 111 #include <sys/filedesc.h> 112 #include <sys/ras.h> 113 #include <sys/signalvar.h> 114 #include <sys/sched.h> 115 #include <sys/sa.h> 116 #include <sys/savar.h> 117 #include <sys/mount.h> 118 #include <sys/syscallargs.h> 119 #include <sys/systrace.h> 120 121 #include <machine/cpu.h> 122 123 #include <uvm/uvm_extern.h> 124 125 #define DEBUG_EXIT 126 127 #ifdef DEBUG_EXIT 128 int debug_exit = 0; 129 #define DPRINTF(x) if (debug_exit) printf x 130 #else 131 #define DPRINTF(x) 132 #endif 133 134 static void lwp_exit_hook(struct lwp *, void *); 135 136 /* 137 * exit -- 138 * Death of process. 139 */ 140 int 141 sys_exit(struct lwp *l, void *v, register_t *retval) 142 { 143 struct sys_exit_args /* { 144 syscallarg(int) rval; 145 } */ *uap = v; 146 147 /* Don't call exit1() multiple times in the same process.*/ 148 if (l->l_proc->p_flag & P_WEXIT) 149 lwp_exit(l); 150 151 exit1(l, W_EXITCODE(SCARG(uap, rval), 0)); 152 /* NOTREACHED */ 153 return (0); 154 } 155 156 /* 157 * Exit: deallocate address space and other resources, change proc state 158 * to zombie, and unlink proc from allproc and parent's lists. Save exit 159 * status and rusage for wait(). Check for child processes and orphan them. 160 */ 161 void 162 exit1(struct lwp *l, int rv) 163 { 164 struct proc *p, *q, *nq; 165 int s, sa; 166 167 p = l->l_proc; 168 169 if (__predict_false(p == initproc)) 170 panic("init died (signal %d, exit %d)", 171 WTERMSIG(rv), WEXITSTATUS(rv)); 172 173 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid)); 174 /* 175 * Disable scheduler activation upcalls. 176 * We're trying to get out of here. 177 */ 178 sa = 0; 179 if (l->l_flag & L_SA) { 180 l->l_flag &= ~L_SA; 181 p->p_flag &= ~P_SA; 182 sa = 1; 183 } 184 185 #ifdef PGINPROF 186 vmsizmon(); 187 #endif 188 if (p->p_flag & P_PROFIL) 189 stopprofclock(p); 190 p->p_ru = pool_get(&rusage_pool, PR_WAITOK); 191 /* 192 * If parent is waiting for us to exit or exec, P_PPWAIT is set; we 193 * wake up the parent early to avoid deadlock. 194 */ 195 p->p_flag |= P_WEXIT; 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 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 272 */ 273 p->p_stat = SDEAD; 274 p->p_nrlwps--; 275 l->l_stat = SDEAD; 276 277 /* 278 * Remove proc from pidhash chain so looking it up won't 279 * work. Move it from allproc to zombproc, but do not yet 280 * wake up the reaper. We will put the proc on the 281 * deadproc list later (using the p_hash member), and 282 * wake up the reaper when we do. 283 */ 284 s = proclist_lock_write(); 285 LIST_REMOVE(p, p_hash); 286 LIST_REMOVE(p, p_list); 287 LIST_INSERT_HEAD(&zombproc, p, p_list); 288 LIST_REMOVE(l, l_list); 289 l->l_flag |= L_DETACHED; 290 proclist_unlock_write(s); 291 292 /* 293 * Give orphaned children to init(8). 294 */ 295 q = LIST_FIRST(&p->p_children); 296 if (q) /* only need this if any child is S_ZOMB */ 297 wakeup((caddr_t)initproc); 298 for (; q != 0; q = nq) { 299 nq = LIST_NEXT(q, p_sibling); 300 301 /* 302 * Traced processes are killed since their existence 303 * means someone is screwing up. Since we reset the 304 * trace flags, the logic in sys_wait4() would not be 305 * triggered to reparent the process to its 306 * original parent, so we must do this here. 307 */ 308 if (q->p_flag & P_TRACED) { 309 if (q->p_opptr != q->p_pptr) { 310 struct proc *t = q->p_opptr; 311 proc_reparent(q, t ? t : initproc); 312 q->p_opptr = NULL; 313 } else 314 proc_reparent(q, initproc); 315 q->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 316 psignal(q, SIGKILL); 317 } else { 318 proc_reparent(q, initproc); 319 } 320 } 321 322 /* 323 * Reset p_opptr pointer of all former children which got 324 * traced by another process and were reparented. We reset 325 * it to NULL here; the trace detach code then reparents 326 * the child to initproc. We only check allproc list, since 327 * eventual former children on zombproc list won't reference 328 * p_opptr anymore. 329 */ 330 if (p->p_flag & P_CHTRACED) { 331 struct proc *t; 332 333 proclist_lock_read(); 334 335 LIST_FOREACH(t, &allproc, p_list) { 336 if (t->p_opptr == p) 337 t->p_opptr = NULL; 338 } 339 340 proclist_unlock_read(); 341 } 342 343 /* 344 * Save exit status and final rusage info, adding in child rusage 345 * info and self times. 346 */ 347 p->p_xstat = rv; 348 *p->p_ru = p->p_stats->p_ru; 349 calcru(p, &p->p_ru->ru_utime, &p->p_ru->ru_stime, NULL); 350 ruadd(p->p_ru, &p->p_stats->p_cru); 351 352 /* 353 * Notify interested parties of our demise. 354 */ 355 KNOTE(&p->p_klist, NOTE_EXIT); 356 357 #if PERFCTRS 358 /* 359 * Save final PMC information in parent process & clean up. 360 */ 361 if (PMC_ENABLED(p)) { 362 pmc_save_context(p); 363 pmc_accumulate(p->p_pptr, p); 364 pmc_process_exit(p); 365 } 366 #endif 367 368 /* 369 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 370 * flag set, notify init instead (and hope it will handle 371 * this situation). 372 */ 373 if (p->p_pptr->p_flag & P_NOCLDWAIT) { 374 struct proc *pp = p->p_pptr; 375 proc_reparent(p, initproc); 376 /* 377 * If this was the last child of our parent, notify 378 * parent, so in case he was wait(2)ing, he will 379 * continue. 380 */ 381 if (LIST_FIRST(&pp->p_children) == NULL) 382 wakeup((caddr_t)pp); 383 } 384 385 /* 386 * Release the process's signal state. 387 */ 388 sigactsfree(p); 389 390 /* 391 * Clear curlwp after we've done all operations 392 * that could block, and before tearing down the rest 393 * of the process state that might be used from clock, etc. 394 * Also, can't clear curlwp while we're still runnable, 395 * as we're not on a run queue (we are current, just not 396 * a proper proc any longer!). 397 * 398 * Other substructures are freed from wait(). 399 */ 400 curlwp = NULL; 401 limfree(p->p_limit); 402 pstatsfree(p->p_stats); 403 p->p_limit = NULL; 404 405 /* This process no longer needs to hold the kernel lock. */ 406 KERNEL_PROC_UNLOCK(l); 407 408 /* 409 * Finally, call machine-dependent code to switch to a new 410 * context (possibly the idle context). Once we are no longer 411 * using the dead process's vmspace and stack, exit2() will be 412 * called to schedule those resources to be released by the 413 * reaper thread. 414 * 415 * Note that cpu_exit() will end with a call equivalent to 416 * cpu_switch(), finishing our execution (pun intended). 417 */ 418 419 cpu_exit(l, 1); 420 } 421 422 void 423 exit_lwps(struct lwp *l) 424 { 425 struct proc *p; 426 struct lwp *l2; 427 int s, error; 428 lwpid_t waited; 429 430 p = l->l_proc; 431 432 /* XXX SMP 433 * This would be the right place to IPI any LWPs running on 434 * other processors so that they can notice the userret exit hook. 435 */ 436 p->p_userret = lwp_exit_hook; 437 p->p_userret_arg = NULL; 438 439 /* 440 * Make SA-cached LWPs normal process runnable LWPs so that 441 * they'll also self-destruct. 442 */ 443 if (p->p_sa && p->p_sa->sa_ncached > 0) { 444 DPRINTF(("exit_lwps: Making cached LWPs of %d runnable: ", 445 p->p_pid)); 446 SCHED_LOCK(s); 447 while ((l2 = sa_getcachelwp(p)) != 0) { 448 l2->l_priority = l2->l_usrpri; 449 setrunnable(l2); 450 DPRINTF(("%d ", l2->l_lid)); 451 } 452 DPRINTF(("\n")); 453 SCHED_UNLOCK(s); 454 } 455 456 /* 457 * Interrupt LWPs in interruptable sleep, unsuspend suspended 458 * LWPs, make detached LWPs undeached (so we can wait for 459 * them) and then wait for everyone else to finish. 460 */ 461 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 462 l2->l_flag &= ~(L_DETACHED|L_SA); 463 if ((l2->l_stat == LSSLEEP && (l2->l_flag & L_SINTR)) || 464 l2->l_stat == LSSUSPENDED) { 465 SCHED_LOCK(s); 466 setrunnable(l2); 467 SCHED_UNLOCK(s); 468 DPRINTF(("exit_lwps: Made %d.%d runnable\n", 469 p->p_pid, l2->l_lid)); 470 } 471 } 472 473 474 while (p->p_nlwps > 1) { 475 DPRINTF(("exit_lwps: waiting for %d LWPs (%d runnable, %d zombies)\n", 476 p->p_nlwps, p->p_nrlwps, p->p_nzlwps)); 477 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL); 478 if (error) 479 panic("exit_lwps: lwp_wait1 failed with error %d\n", 480 error); 481 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited)); 482 } 483 484 p->p_userret = NULL; 485 } 486 487 /* Wrapper function for use in p_userret */ 488 static void 489 lwp_exit_hook(struct lwp *l, void *arg) 490 { 491 KERNEL_PROC_LOCK(l); 492 lwp_exit(l); 493 } 494 495 /* 496 * We are called from cpu_exit() once it is safe to schedule the 497 * dead process's resources to be freed (i.e., once we've switched to 498 * the idle PCB for the current CPU). 499 * 500 * NOTE: One must be careful with locking in this routine. It's 501 * called from a critical section in machine-dependent code, so 502 * we should refrain from changing any interrupt state. 503 * 504 * We lock the deadproc list (a spin lock), place the proc on that 505 * list (using the p_hash member), and wake up the reaper. 506 */ 507 void 508 exit2(struct lwp *l) 509 { 510 struct proc *p = l->l_proc; 511 512 simple_lock(&deadproc_slock); 513 LIST_INSERT_HEAD(&deadproc, p, p_hash); 514 simple_unlock(&deadproc_slock); 515 516 /* lwp_exit2() will wake up deadproc for us. */ 517 lwp_exit2(l); 518 } 519 520 /* 521 * Process reaper. This is run by a kernel thread to free the resources 522 * of a dead process. Once the resources are free, the process becomes 523 * a zombie, and the parent is allowed to read the undead's status. 524 */ 525 void 526 reaper(void *arg) 527 { 528 struct proc *p; 529 struct lwp *l; 530 531 KERNEL_PROC_UNLOCK(curlwp); 532 533 for (;;) { 534 simple_lock(&deadproc_slock); 535 p = LIST_FIRST(&deadproc); 536 l = LIST_FIRST(&deadlwp); 537 if (p == NULL && l == NULL) { 538 /* No work for us; go to sleep until someone exits. */ 539 (void) ltsleep(&deadproc, PVM|PNORELOCK, 540 "reaper", 0, &deadproc_slock); 541 continue; 542 } 543 544 if (l != NULL ) { 545 p = l->l_proc; 546 547 /* Remove us from the deadlwp list. */ 548 LIST_REMOVE(l, l_list); 549 simple_unlock(&deadproc_slock); 550 KERNEL_PROC_LOCK(curlwp); 551 552 /* 553 * Give machine-dependent code a chance to free any 554 * resources it couldn't free while still running on 555 * that process's context. This must be done before 556 * uvm_lwp_exit(), in case these resources are in the 557 * PCB. 558 */ 559 cpu_wait(l); 560 561 /* 562 * Free the VM resources we're still holding on to. 563 */ 564 uvm_lwp_exit(l); 565 566 l->l_stat = LSZOMB; 567 if (l->l_flag & L_DETACHED) { 568 /* Nobody waits for detached LWPs. */ 569 LIST_REMOVE(l, l_sibling); 570 p->p_nlwps--; 571 pool_put(&lwp_pool, l); 572 } else { 573 p->p_nzlwps++; 574 wakeup((caddr_t)&p->p_nlwps); 575 } 576 /* XXXNJW where should this be with respect to 577 * the wakeup() above? */ 578 KERNEL_PROC_UNLOCK(curlwp); 579 } else { 580 /* Remove us from the deadproc list. */ 581 LIST_REMOVE(p, p_hash); 582 simple_unlock(&deadproc_slock); 583 KERNEL_PROC_LOCK(curlwp); 584 585 /* 586 * Free the VM resources we're still holding on to. 587 * We must do this from a valid thread because doing 588 * so may block. 589 */ 590 uvm_proc_exit(p); 591 592 /* Process is now a true zombie. */ 593 p->p_stat = SZOMB; 594 595 /* Wake up the parent so it can get exit status. */ 596 if ((p->p_flag & P_FSTRACE) == 0 && p->p_exitsig != 0) 597 psignal(p->p_pptr, P_EXITSIG(p)); 598 KERNEL_PROC_UNLOCK(curlwp); 599 wakeup((caddr_t)p->p_pptr); 600 } 601 } 602 } 603 604 int 605 sys_wait4(struct lwp *l, void *v, register_t *retval) 606 { 607 struct sys_wait4_args /* { 608 syscallarg(int) pid; 609 syscallarg(int *) status; 610 syscallarg(int) options; 611 syscallarg(struct rusage *) rusage; 612 } */ *uap = v; 613 struct proc *p, *q, *t; 614 int nfound, status, error, s; 615 616 q = l->l_proc; 617 618 if (SCARG(uap, pid) == 0) 619 SCARG(uap, pid) = -q->p_pgid; 620 if (SCARG(uap, options) &~ (WUNTRACED|WNOHANG|WALTSIG)) 621 return (EINVAL); 622 623 loop: 624 nfound = 0; 625 LIST_FOREACH(p, &q->p_children, p_sibling) { 626 if (SCARG(uap, pid) != WAIT_ANY && 627 p->p_pid != SCARG(uap, pid) && 628 p->p_pgid != -SCARG(uap, pid)) 629 continue; 630 /* 631 * Wait for processes with p_exitsig != SIGCHLD processes only 632 * if WALTSIG is set; wait for processes with p_exitsig == 633 * SIGCHLD only if WALTSIG is clear. 634 */ 635 if (((SCARG(uap, options) & WALLSIG) == 0) && 636 ((SCARG(uap, options) & WALTSIG) ? 637 (p->p_exitsig == SIGCHLD) : (P_EXITSIG(p) != SIGCHLD))) 638 continue; 639 640 nfound++; 641 if (p->p_stat == SZOMB) { 642 retval[0] = p->p_pid; 643 644 if (SCARG(uap, status)) { 645 status = p->p_xstat; /* convert to int */ 646 error = copyout((caddr_t)&status, 647 (caddr_t)SCARG(uap, status), 648 sizeof(status)); 649 if (error) 650 return (error); 651 } 652 if (SCARG(uap, rusage) && 653 (error = copyout((caddr_t)p->p_ru, 654 (caddr_t)SCARG(uap, rusage), 655 sizeof(struct rusage)))) 656 return (error); 657 /* 658 * If we got the child via ptrace(2) or procfs, and 659 * the parent is different (meaning the process was 660 * attached, rather than run as a child), then we need 661 * to give it back to the old parent, and send the 662 * parent the exit signal. The rest of the cleanup 663 * will be done when the old parent waits on the child. 664 */ 665 if ((p->p_flag & P_TRACED) && p->p_opptr != p->p_pptr){ 666 t = p->p_opptr; 667 proc_reparent(p, t ? t : initproc); 668 p->p_opptr = NULL; 669 p->p_flag &= ~(P_TRACED|P_WAITED|P_FSTRACE); 670 if (p->p_exitsig != 0) 671 psignal(p->p_pptr, P_EXITSIG(p)); 672 wakeup((caddr_t)p->p_pptr); 673 return (0); 674 } 675 scheduler_wait_hook(q, p); 676 p->p_xstat = 0; 677 ruadd(&q->p_stats->p_cru, p->p_ru); 678 pool_put(&rusage_pool, p->p_ru); 679 680 /* 681 * Finally finished with old proc entry. 682 * Unlink it from its process group and free it. 683 */ 684 leavepgrp(p); 685 686 s = proclist_lock_write(); 687 LIST_REMOVE(p, p_list); /* off zombproc */ 688 proclist_unlock_write(s); 689 690 LIST_REMOVE(p, p_sibling); 691 692 /* 693 * Decrement the count of procs running with this uid. 694 */ 695 (void)chgproccnt(p->p_cred->p_ruid, -1); 696 697 /* 698 * Free up credentials. 699 */ 700 if (--p->p_cred->p_refcnt == 0) { 701 crfree(p->p_cred->pc_ucred); 702 pool_put(&pcred_pool, p->p_cred); 703 } 704 705 /* 706 * Release reference to text vnode 707 */ 708 if (p->p_textvp) 709 vrele(p->p_textvp); 710 711 /* 712 * Release any SA state 713 */ 714 if (p->p_sa) { 715 free(p->p_sa->sa_stacks, M_SA); 716 pool_put(&sadata_pool, p->p_sa); 717 } 718 719 pool_put(&proc_pool, p); 720 nprocs--; 721 return (0); 722 } 723 if (p->p_stat == SSTOP && (p->p_flag & P_WAITED) == 0 && 724 (p->p_flag & P_TRACED || SCARG(uap, options) & WUNTRACED)) { 725 p->p_flag |= P_WAITED; 726 retval[0] = p->p_pid; 727 728 if (SCARG(uap, status)) { 729 status = W_STOPCODE(p->p_xstat); 730 error = copyout((caddr_t)&status, 731 (caddr_t)SCARG(uap, status), 732 sizeof(status)); 733 } else 734 error = 0; 735 return (error); 736 } 737 } 738 if (nfound == 0) 739 return (ECHILD); 740 if (SCARG(uap, options) & WNOHANG) { 741 retval[0] = 0; 742 return (0); 743 } 744 if ((error = tsleep((caddr_t)q, PWAIT | PCATCH, "wait", 0)) != 0) 745 return (error); 746 goto loop; 747 } 748 749 /* 750 * make process 'parent' the new parent of process 'child'. 751 */ 752 void 753 proc_reparent(struct proc *child, struct proc *parent) 754 { 755 756 if (child->p_pptr == parent) 757 return; 758 759 if (parent == initproc) 760 child->p_exitsig = SIGCHLD; 761 762 LIST_REMOVE(child, p_sibling); 763 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 764 child->p_pptr = parent; 765 } 766