1 /* $NetBSD: kern_exit.c,v 1.216 2008/11/14 23:33:45 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2006, 2007, 2008 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, and by Andrew Doran. 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1989, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * (c) UNIX System Laboratories, Inc. 37 * All or some portions of this file are derived from material licensed 38 * to the University of California by American Telephone and Telegraph 39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 40 * the permission of UNIX System Laboratories, Inc. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)kern_exit.c 8.10 (Berkeley) 2/23/95 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.216 2008/11/14 23:33:45 ad Exp $"); 71 72 #include "opt_ktrace.h" 73 #include "opt_perfctrs.h" 74 #include "opt_sa.h" 75 #include "opt_sysv.h" 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/ioctl.h> 80 #include <sys/tty.h> 81 #include <sys/time.h> 82 #include <sys/resource.h> 83 #include <sys/kernel.h> 84 #include <sys/proc.h> 85 #include <sys/buf.h> 86 #include <sys/wait.h> 87 #include <sys/file.h> 88 #include <sys/vnode.h> 89 #include <sys/syslog.h> 90 #include <sys/malloc.h> 91 #include <sys/pool.h> 92 #include <sys/uidinfo.h> 93 #if defined(PERFCTRS) 94 #include <sys/pmc.h> 95 #endif 96 #include <sys/ptrace.h> 97 #include <sys/acct.h> 98 #include <sys/filedesc.h> 99 #include <sys/ras.h> 100 #include <sys/signalvar.h> 101 #include <sys/sched.h> 102 #include <sys/sa.h> 103 #include <sys/savar.h> 104 #include <sys/mount.h> 105 #include <sys/syscallargs.h> 106 #include <sys/kauth.h> 107 #include <sys/sleepq.h> 108 #include <sys/lockdebug.h> 109 #include <sys/ktrace.h> 110 #include <sys/cpu.h> 111 #include <sys/lwpctl.h> 112 #include <sys/atomic.h> 113 114 #include <uvm/uvm_extern.h> 115 116 #define DEBUG_EXIT 117 118 #ifdef DEBUG_EXIT 119 int debug_exit = 0; 120 #define DPRINTF(x) if (debug_exit) printf x 121 #else 122 #define DPRINTF(x) 123 #endif 124 125 static int find_stopped_child(struct proc *, pid_t, int, struct proc **, int *); 126 static void proc_free(struct proc *, struct rusage *); 127 128 /* 129 * Fill in the appropriate signal information, and signal the parent. 130 */ 131 static void 132 exit_psignal(struct proc *p, struct proc *pp, ksiginfo_t *ksi) 133 { 134 135 KSI_INIT(ksi); 136 if ((ksi->ksi_signo = P_EXITSIG(p)) == SIGCHLD) { 137 if (WIFSIGNALED(p->p_xstat)) { 138 if (WCOREDUMP(p->p_xstat)) 139 ksi->ksi_code = CLD_DUMPED; 140 else 141 ksi->ksi_code = CLD_KILLED; 142 } else { 143 ksi->ksi_code = CLD_EXITED; 144 } 145 } 146 /* 147 * We fill those in, even for non-SIGCHLD. 148 * It's safe to access p->p_cred unlocked here. 149 */ 150 ksi->ksi_pid = p->p_pid; 151 ksi->ksi_uid = kauth_cred_geteuid(p->p_cred); 152 ksi->ksi_status = p->p_xstat; 153 /* XXX: is this still valid? */ 154 ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 155 ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 156 } 157 158 /* 159 * exit -- 160 * Death of process. 161 */ 162 int 163 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval) 164 { 165 /* { 166 syscallarg(int) rval; 167 } */ 168 struct proc *p = l->l_proc; 169 170 /* Don't call exit1() multiple times in the same process. */ 171 mutex_enter(p->p_lock); 172 if (p->p_sflag & PS_WEXIT) { 173 mutex_exit(p->p_lock); 174 lwp_exit(l); 175 } 176 177 /* exit1() will release the mutex. */ 178 exit1(l, W_EXITCODE(SCARG(uap, rval), 0)); 179 /* NOTREACHED */ 180 return (0); 181 } 182 183 /* 184 * Exit: deallocate address space and other resources, change proc state 185 * to zombie, and unlink proc from allproc and parent's lists. Save exit 186 * status and rusage for wait(). Check for child processes and orphan them. 187 * 188 * Must be called with p->p_lock held. Does not return. 189 */ 190 void 191 exit1(struct lwp *l, int rv) 192 { 193 struct proc *p, *q, *nq; 194 struct pgrp *pgrp; 195 ksiginfo_t ksi; 196 ksiginfoq_t kq; 197 int wakeinit, sa; 198 199 p = l->l_proc; 200 201 KASSERT(mutex_owned(p->p_lock)); 202 203 if (__predict_false(p == initproc)) 204 panic("init died (signal %d, exit %d)", 205 WTERMSIG(rv), WEXITSTATUS(rv)); 206 207 /* 208 * Disable scheduler activation upcalls. We're trying to get out of 209 * here. 210 */ 211 sa = 0; 212 #ifdef KERN_SA 213 if ((p->p_sa != NULL)) { 214 l->l_pflag |= LP_SA_NOBLOCK; 215 sa = 1; 216 } 217 #endif 218 219 p->p_sflag |= PS_WEXIT; 220 221 /* 222 * Force all other LWPs to exit before we do. Only then can we 223 * begin to tear down the rest of the process state. 224 */ 225 if (sa || p->p_nlwps > 1) 226 exit_lwps(l); 227 228 ksiginfo_queue_init(&kq); 229 230 /* 231 * If we have been asked to stop on exit, do so now. 232 */ 233 if (__predict_false(p->p_sflag & PS_STOPEXIT)) { 234 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); 235 sigclearall(p, &contsigmask, &kq); 236 p->p_waited = 0; 237 membar_producer(); 238 p->p_stat = SSTOP; 239 lwp_lock(l); 240 p->p_nrlwps--; 241 l->l_stat = LSSTOP; 242 mutex_exit(p->p_lock); 243 mi_switch(l); 244 KERNEL_LOCK(l->l_biglocks, l); 245 mutex_enter(p->p_lock); 246 } 247 248 /* 249 * Bin any remaining signals and mark the process as dying so it will 250 * not be found for, e.g. signals. 251 */ 252 sigfillset(&p->p_sigctx.ps_sigignore); 253 sigclearall(p, NULL, &kq); 254 p->p_stat = SDYING; 255 mutex_exit(p->p_lock); 256 ksiginfo_queue_drain(&kq); 257 258 /* Destroy any lwpctl info. */ 259 if (p->p_lwpctl != NULL) 260 lwp_ctl_exit(); 261 262 /* 263 * Drain all remaining references that procfs, ptrace and others may 264 * have on the process. 265 */ 266 rw_enter(&p->p_reflock, RW_WRITER); 267 268 DPRINTF(("exit1: %d.%d exiting.\n", p->p_pid, l->l_lid)); 269 270 timers_free(p, TIMERS_ALL); 271 #if defined(__HAVE_RAS) 272 ras_purgeall(); 273 #endif 274 275 /* 276 * Close open files, release open-file table and free signal 277 * actions. This may block! 278 */ 279 fd_free(); 280 cwdfree(p->p_cwdi); 281 p->p_cwdi = NULL; 282 doexithooks(p); 283 sigactsfree(p->p_sigacts); 284 285 /* 286 * Write out accounting data. 287 */ 288 (void)acct_process(l); 289 290 #ifdef KTRACE 291 /* 292 * Release trace file. 293 */ 294 if (p->p_tracep != NULL) { 295 mutex_enter(&ktrace_lock); 296 ktrderef(p); 297 mutex_exit(&ktrace_lock); 298 } 299 #endif 300 301 /* 302 * If emulation has process exit hook, call it now. 303 * Set the exit status now so that the exit hook has 304 * an opportunity to tweak it (COMPAT_LINUX requires 305 * this for thread group emulation) 306 */ 307 p->p_xstat = rv; 308 if (p->p_emul->e_proc_exit) 309 (*p->p_emul->e_proc_exit)(p); 310 311 /* 312 * Free the VM resources we're still holding on to. 313 * We must do this from a valid thread because doing 314 * so may block. This frees vmspace, which we don't 315 * need anymore. The only remaining lwp is the one 316 * we run at this moment, nothing runs in userland 317 * anymore. 318 */ 319 uvm_proc_exit(p); 320 321 /* 322 * Stop profiling. 323 */ 324 if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) { 325 mutex_spin_enter(&p->p_stmutex); 326 stopprofclock(p); 327 mutex_spin_exit(&p->p_stmutex); 328 } 329 330 /* 331 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we 332 * wake up the parent early to avoid deadlock. We can do this once 333 * the VM resources are released. 334 */ 335 mutex_enter(proc_lock); 336 if (p->p_lflag & PL_PPWAIT) { 337 p->p_lflag &= ~PL_PPWAIT; 338 cv_broadcast(&p->p_pptr->p_waitcv); 339 } 340 341 if (SESS_LEADER(p)) { 342 struct vnode *vprele = NULL, *vprevoke = NULL; 343 struct session *sp = p->p_session; 344 struct tty *tp; 345 346 if (sp->s_ttyvp) { 347 /* 348 * Controlling process. 349 * Signal foreground pgrp, 350 * drain controlling terminal 351 * and revoke access to controlling terminal. 352 */ 353 tp = sp->s_ttyp; 354 mutex_spin_enter(&tty_lock); 355 if (tp->t_session == sp) { 356 /* we can't guarantee the revoke will do this */ 357 pgrp = tp->t_pgrp; 358 tp->t_pgrp = NULL; 359 tp->t_session = NULL; 360 mutex_spin_exit(&tty_lock); 361 if (pgrp != NULL) { 362 pgsignal(pgrp, SIGHUP, 1); 363 } 364 mutex_exit(proc_lock); 365 (void) ttywait(tp); 366 mutex_enter(proc_lock); 367 368 /* The tty could have been revoked. */ 369 vprevoke = sp->s_ttyvp; 370 } else 371 mutex_spin_exit(&tty_lock); 372 vprele = sp->s_ttyvp; 373 sp->s_ttyvp = NULL; 374 /* 375 * s_ttyp is not zero'd; we use this to indicate 376 * that the session once had a controlling terminal. 377 * (for logging and informational purposes) 378 */ 379 } 380 sp->s_leader = NULL; 381 382 if (vprevoke != NULL || vprele != NULL) { 383 if (vprevoke != NULL) { 384 SESSRELE(sp); 385 mutex_exit(proc_lock); 386 VOP_REVOKE(vprevoke, REVOKEALL); 387 } else 388 mutex_exit(proc_lock); 389 if (vprele != NULL) 390 vrele(vprele); 391 mutex_enter(proc_lock); 392 } 393 } 394 fixjobc(p, p->p_pgrp, 0); 395 396 /* 397 * Finalize the last LWP's specificdata, as well as the 398 * specificdata for the proc itself. 399 */ 400 lwp_finispecific(l); 401 proc_finispecific(p); 402 403 /* 404 * Notify interested parties of our demise. 405 */ 406 KNOTE(&p->p_klist, NOTE_EXIT); 407 408 #if PERFCTRS 409 /* 410 * Save final PMC information in parent process & clean up. 411 */ 412 if (PMC_ENABLED(p)) { 413 pmc_save_context(p); 414 pmc_accumulate(p->p_pptr, p); 415 pmc_process_exit(p); 416 } 417 #endif 418 419 /* 420 * Reset p_opptr pointer of all former children which got 421 * traced by another process and were reparented. We reset 422 * it to NULL here; the trace detach code then reparents 423 * the child to initproc. We only check allproc list, since 424 * eventual former children on zombproc list won't reference 425 * p_opptr anymore. 426 */ 427 if (__predict_false(p->p_slflag & PSL_CHTRACED)) { 428 PROCLIST_FOREACH(q, &allproc) { 429 if ((q->p_flag & PK_MARKER) != 0) 430 continue; 431 if (q->p_opptr == p) 432 q->p_opptr = NULL; 433 } 434 } 435 436 /* 437 * Give orphaned children to init(8). 438 */ 439 q = LIST_FIRST(&p->p_children); 440 wakeinit = (q != NULL); 441 for (; q != NULL; q = nq) { 442 nq = LIST_NEXT(q, p_sibling); 443 444 /* 445 * Traced processes are killed since their existence 446 * means someone is screwing up. Since we reset the 447 * trace flags, the logic in sys_wait4() would not be 448 * triggered to reparent the process to its 449 * original parent, so we must do this here. 450 */ 451 if (__predict_false(q->p_slflag & PSL_TRACED)) { 452 mutex_enter(p->p_lock); 453 q->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL); 454 mutex_exit(p->p_lock); 455 if (q->p_opptr != q->p_pptr) { 456 struct proc *t = q->p_opptr; 457 proc_reparent(q, t ? t : initproc); 458 q->p_opptr = NULL; 459 } else 460 proc_reparent(q, initproc); 461 killproc(q, "orphaned traced process"); 462 } else 463 proc_reparent(q, initproc); 464 } 465 466 /* 467 * Move proc from allproc to zombproc, it's now nearly ready to be 468 * collected by parent. 469 */ 470 LIST_REMOVE(l, l_list); 471 LIST_REMOVE(p, p_list); 472 LIST_INSERT_HEAD(&zombproc, p, p_list); 473 474 /* 475 * Mark the process as dead. We must do this before we signal 476 * the parent. 477 */ 478 p->p_stat = SDEAD; 479 480 /* Put in front of parent's sibling list for parent to collect it */ 481 q = p->p_pptr; 482 q->p_nstopchild++; 483 if (LIST_FIRST(&q->p_children) != p) { 484 /* Put child where it can be found quickly */ 485 LIST_REMOVE(p, p_sibling); 486 LIST_INSERT_HEAD(&q->p_children, p, p_sibling); 487 } 488 489 /* 490 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 491 * flag set, notify init instead (and hope it will handle 492 * this situation). 493 */ 494 if (q->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) { 495 proc_reparent(p, initproc); 496 wakeinit = 1; 497 498 /* 499 * If this was the last child of our parent, notify 500 * parent, so in case he was wait(2)ing, he will 501 * continue. 502 */ 503 if (LIST_FIRST(&q->p_children) == NULL) 504 cv_broadcast(&q->p_waitcv); 505 } 506 507 /* Reload parent pointer, since p may have been reparented above */ 508 q = p->p_pptr; 509 510 if (__predict_false((p->p_slflag & PSL_FSTRACE) == 0 && p->p_exitsig != 0)) { 511 exit_psignal(p, q, &ksi); 512 kpsignal(q, &ksi, NULL); 513 } 514 515 /* Calculate the final rusage info. */ 516 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, 517 NULL, NULL); 518 519 if (wakeinit) 520 cv_broadcast(&initproc->p_waitcv); 521 522 callout_destroy(&l->l_timeout_ch); 523 524 /* 525 * Remaining lwp resources will be freed in lwp_exit2() once we've 526 * switch to idle context; at that point, we will be marked as a 527 * full blown zombie. 528 */ 529 mutex_enter(p->p_lock); 530 lwp_drainrefs(l); 531 lwp_lock(l); 532 l->l_prflag &= ~LPR_DETACHED; 533 l->l_stat = LSZOMB; 534 lwp_unlock(l); 535 KASSERT(curlwp == l); 536 KASSERT(p->p_nrlwps == 1); 537 KASSERT(p->p_nlwps == 1); 538 p->p_stat = SZOMB; 539 p->p_nrlwps--; 540 p->p_nzlwps++; 541 p->p_ndlwps = 0; 542 mutex_exit(p->p_lock); 543 544 /* 545 * Signal the parent to collect us, and drop the proclist lock. 546 * Drop debugger/procfs lock; no new references can be gained. 547 */ 548 cv_broadcast(&p->p_pptr->p_waitcv); 549 mutex_exit(proc_lock); 550 rw_exit(&p->p_reflock); 551 552 /* Verify that we hold no locks other than the kernel lock. */ 553 LOCKDEBUG_BARRIER(&kernel_lock, 0); 554 555 /* 556 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 557 */ 558 559 /* 560 * Give machine-dependent code a chance to free any MD LWP 561 * resources. This must be done before uvm_lwp_exit(), in 562 * case these resources are in the PCB. 563 */ 564 #ifndef __NO_CPU_LWP_FREE 565 cpu_lwp_free(l, 1); 566 #endif 567 pmap_deactivate(l); 568 569 /* This process no longer needs to hold the kernel lock. */ 570 #ifdef notyet 571 /* XXXSMP hold in lwp_userret() */ 572 KERNEL_UNLOCK_LAST(l); 573 #else 574 KERNEL_UNLOCK_ALL(l, NULL); 575 #endif 576 577 lwp_exit_switchaway(l); 578 } 579 580 void 581 exit_lwps(struct lwp *l) 582 { 583 struct proc *p; 584 struct lwp *l2; 585 int error; 586 lwpid_t waited; 587 int nlocks; 588 589 KERNEL_UNLOCK_ALL(l, &nlocks); 590 591 p = l->l_proc; 592 KASSERT(mutex_owned(p->p_lock)); 593 594 #ifdef KERN_SA 595 if (p->p_sa != NULL) { 596 struct sadata_vp *vp; 597 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) { 598 /* 599 * Make SA-cached LWPs normal process interruptable 600 * so that the exit code can wake them. Locking 601 * savp_mutex locks all the lwps on this vp that 602 * we need to adjust. 603 */ 604 mutex_enter(&vp->savp_mutex); 605 DPRINTF(("exit_lwps: Making cached LWPs of %d on " 606 "VP %d interruptable: ", p->p_pid, vp->savp_id)); 607 TAILQ_FOREACH(l2, &vp->savp_lwpcache, l_sleepchain) { 608 l2->l_flag |= LW_SINTR; 609 DPRINTF(("%d ", l2->l_lid)); 610 } 611 DPRINTF(("\n")); 612 613 DPRINTF(("exit_lwps: Making unblocking LWPs of %d on " 614 "VP %d interruptable: ", p->p_pid, vp->savp_id)); 615 TAILQ_FOREACH(l2, &vp->savp_woken, l_sleepchain) { 616 vp->savp_woken_count--; 617 l2->l_flag |= LW_SINTR; 618 DPRINTF(("%d ", l2->l_lid)); 619 } 620 DPRINTF(("\n")); 621 mutex_exit(&vp->savp_mutex); 622 } 623 } 624 #endif 625 626 retry: 627 /* 628 * Interrupt LWPs in interruptable sleep, unsuspend suspended 629 * LWPs and then wait for everyone else to finish. 630 */ 631 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 632 if (l2 == l) 633 continue; 634 lwp_lock(l2); 635 l2->l_flag &= ~LW_SA; 636 l2->l_flag |= LW_WEXIT; 637 if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) || 638 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) { 639 /* setrunnable() will release the lock. */ 640 setrunnable(l2); 641 DPRINTF(("exit_lwps: Made %d.%d runnable\n", 642 p->p_pid, l2->l_lid)); 643 continue; 644 } 645 lwp_unlock(l2); 646 } 647 while (p->p_nlwps > 1) { 648 DPRINTF(("exit_lwps: waiting for %d LWPs (%d zombies)\n", 649 p->p_nlwps, p->p_nzlwps)); 650 error = lwp_wait1(l, 0, &waited, LWPWAIT_EXITCONTROL); 651 if (p->p_nlwps == 1) 652 break; 653 if (error == EDEADLK) { 654 /* 655 * LWPs can get suspended/slept behind us. 656 * (eg. sa_setwoken) 657 * kick them again and retry. 658 */ 659 goto retry; 660 } 661 if (error) 662 panic("exit_lwps: lwp_wait1 failed with error %d", 663 error); 664 DPRINTF(("exit_lwps: Got LWP %d from lwp_wait1()\n", waited)); 665 } 666 667 KERNEL_LOCK(nlocks, l); 668 KASSERT(p->p_nlwps == 1); 669 } 670 671 int 672 do_sys_wait(struct lwp *l, int *pid, int *status, int options, 673 struct rusage *ru, int *was_zombie) 674 { 675 struct proc *child; 676 int error; 677 678 mutex_enter(proc_lock); 679 error = find_stopped_child(l->l_proc, *pid, options, &child, status); 680 681 if (child == NULL) { 682 mutex_exit(proc_lock); 683 *pid = 0; 684 return error; 685 } 686 687 *pid = child->p_pid; 688 689 if (child->p_stat == SZOMB) { 690 /* proc_free() will release the proc_lock. */ 691 *was_zombie = 1; 692 if (options & WNOWAIT) 693 mutex_exit(proc_lock); 694 else { 695 proc_free(child, ru); 696 } 697 } else { 698 /* Child state must have been SSTOP. */ 699 *was_zombie = 0; 700 mutex_exit(proc_lock); 701 *status = W_STOPCODE(*status); 702 } 703 704 return 0; 705 } 706 707 int 708 sys_wait4(struct lwp *l, const struct sys_wait4_args *uap, register_t *retval) 709 { 710 /* { 711 syscallarg(int) pid; 712 syscallarg(int *) status; 713 syscallarg(int) options; 714 syscallarg(struct rusage *) rusage; 715 } */ 716 int status, error; 717 int was_zombie; 718 struct rusage ru; 719 int pid = SCARG(uap, pid); 720 721 error = do_sys_wait(l, &pid, &status, SCARG(uap, options), 722 SCARG(uap, rusage) != NULL ? &ru : NULL, &was_zombie); 723 724 retval[0] = pid; 725 if (pid == 0) 726 return error; 727 728 if (SCARG(uap, rusage)) 729 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 730 731 if (error == 0 && SCARG(uap, status)) 732 error = copyout(&status, SCARG(uap, status), sizeof(status)); 733 734 return error; 735 } 736 737 /* 738 * Scan list of child processes for a child process that has stopped or 739 * exited. Used by sys_wait4 and 'compat' equivalents. 740 * 741 * Must be called with the proc_lock held, and may release while waiting. 742 */ 743 static int 744 find_stopped_child(struct proc *parent, pid_t pid, int options, 745 struct proc **child_p, int *status_p) 746 { 747 struct proc *child, *dead; 748 int error; 749 750 KASSERT(mutex_owned(proc_lock)); 751 752 if (options & ~(WUNTRACED|WNOHANG|WALTSIG|WALLSIG) 753 && !(options & WOPTSCHECKED)) { 754 *child_p = NULL; 755 return EINVAL; 756 } 757 758 if (pid == 0 && !(options & WOPTSCHECKED)) 759 pid = -parent->p_pgid; 760 761 for (;;) { 762 error = ECHILD; 763 dead = NULL; 764 765 LIST_FOREACH(child, &parent->p_children, p_sibling) { 766 if (pid >= 0) { 767 if (child->p_pid != pid) { 768 child = p_find(pid, PFIND_ZOMBIE | 769 PFIND_LOCKED); 770 if (child == NULL || 771 child->p_pptr != parent) { 772 child = NULL; 773 break; 774 } 775 } 776 } else if (pid != WAIT_ANY && child->p_pgid != -pid) { 777 /* Child not in correct pgrp */ 778 continue; 779 } 780 781 /* 782 * Wait for processes with p_exitsig != SIGCHLD 783 * processes only if WALTSIG is set; wait for 784 * processes with p_exitsig == SIGCHLD only 785 * if WALTSIG is clear. 786 */ 787 if (((options & WALLSIG) == 0) && 788 (options & WALTSIG ? child->p_exitsig == SIGCHLD 789 : P_EXITSIG(child) != SIGCHLD)){ 790 if (child->p_pid == pid) { 791 child = NULL; 792 break; 793 } 794 continue; 795 } 796 797 error = 0; 798 if ((options & WNOZOMBIE) == 0) { 799 if (child->p_stat == SZOMB) 800 break; 801 if (child->p_stat == SDEAD) { 802 /* 803 * We may occasionally arrive here 804 * after receiving a signal, but 805 * immediatley before the child 806 * process is zombified. The wait 807 * will be short, so avoid returning 808 * to userspace. 809 */ 810 dead = child; 811 } 812 } 813 814 if (child->p_stat == SSTOP && 815 child->p_waited == 0 && 816 (child->p_slflag & PSL_TRACED || 817 options & WUNTRACED)) { 818 if ((options & WNOWAIT) == 0) { 819 child->p_waited = 1; 820 parent->p_nstopchild--; 821 } 822 break; 823 } 824 if (parent->p_nstopchild == 0 || child->p_pid == pid) { 825 child = NULL; 826 break; 827 } 828 } 829 830 if (child != NULL || error != 0 || 831 ((options & WNOHANG) != 0 && dead == NULL)) { 832 if (child != NULL) { 833 *status_p = child->p_xstat; 834 } 835 *child_p = child; 836 return error; 837 } 838 839 /* 840 * Wait for another child process to stop. 841 */ 842 error = cv_wait_sig(&parent->p_waitcv, proc_lock); 843 844 if (error != 0) { 845 *child_p = NULL; 846 return error; 847 } 848 } 849 } 850 851 /* 852 * Free a process after parent has taken all the state info. Must be called 853 * with the proclist lock held, and will release before returning. 854 * 855 * *ru is returned to the caller, and must be freed by the caller. 856 */ 857 static void 858 proc_free(struct proc *p, struct rusage *ru) 859 { 860 struct proc *parent; 861 struct lwp *l; 862 ksiginfo_t ksi; 863 kauth_cred_t cred1, cred2; 864 uid_t uid; 865 866 KASSERT(mutex_owned(proc_lock)); 867 KASSERT(p->p_nlwps == 1); 868 KASSERT(p->p_nzlwps == 1); 869 KASSERT(p->p_nrlwps == 0); 870 KASSERT(p->p_stat == SZOMB); 871 872 /* 873 * If we got the child via ptrace(2) or procfs, and 874 * the parent is different (meaning the process was 875 * attached, rather than run as a child), then we need 876 * to give it back to the old parent, and send the 877 * parent the exit signal. The rest of the cleanup 878 * will be done when the old parent waits on the child. 879 */ 880 if ((p->p_slflag & PSL_TRACED) != 0) { 881 parent = p->p_pptr; 882 if (p->p_opptr != parent){ 883 mutex_enter(p->p_lock); 884 p->p_slflag &= ~(PSL_TRACED|PSL_FSTRACE|PSL_SYSCALL); 885 mutex_exit(p->p_lock); 886 parent = p->p_opptr; 887 if (parent == NULL) 888 parent = initproc; 889 proc_reparent(p, parent); 890 p->p_opptr = NULL; 891 if (p->p_exitsig != 0) { 892 exit_psignal(p, parent, &ksi); 893 kpsignal(parent, &ksi, NULL); 894 } 895 cv_broadcast(&parent->p_waitcv); 896 mutex_exit(proc_lock); 897 return; 898 } 899 } 900 901 /* 902 * Finally finished with old proc entry. Unlink it from its process 903 * group. 904 */ 905 leavepgrp(p); 906 907 parent = p->p_pptr; 908 sched_proc_exit(parent, p); 909 910 /* 911 * Add child times of exiting process onto its own times. 912 * This cannot be done any earlier else it might get done twice. 913 */ 914 l = LIST_FIRST(&p->p_lwps); 915 p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw); 916 p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw; 917 ruadd(&p->p_stats->p_ru, &l->l_ru); 918 ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru); 919 ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru); 920 if (ru != NULL) 921 *ru = p->p_stats->p_ru; 922 p->p_xstat = 0; 923 924 /* Release any SA state. */ 925 #ifdef KERN_SA 926 if (p->p_sa) 927 sa_release(p); 928 #endif 929 930 /* 931 * At this point we are going to start freeing the final resources. 932 * If anyone tries to access the proc structure after here they will 933 * get a shock - bits are missing. Attempt to make it hard! We 934 * don't bother with any further locking past this point. 935 */ 936 p->p_stat = SIDL; /* not even a zombie any more */ 937 LIST_REMOVE(p, p_list); /* off zombproc */ 938 parent = p->p_pptr; 939 p->p_pptr->p_nstopchild--; 940 LIST_REMOVE(p, p_sibling); 941 942 /* 943 * Let pid be reallocated. 944 */ 945 proc_free_pid(p); 946 mutex_exit(proc_lock); 947 948 /* 949 * Delay release until after lwp_free. 950 */ 951 cred2 = l->l_cred; 952 953 /* 954 * Free the last LWP's resources. 955 * 956 * lwp_free ensures the LWP is no longer running on another CPU. 957 */ 958 lwp_free(l, false, true); 959 960 /* 961 * Now no one except us can reach the process p. 962 */ 963 964 /* 965 * Decrement the count of procs running with this uid. 966 */ 967 cred1 = p->p_cred; 968 uid = kauth_cred_getuid(cred1); 969 (void)chgproccnt(uid, -1); 970 971 /* 972 * Release substructures. 973 */ 974 975 limfree(p->p_limit); 976 pstatsfree(p->p_stats); 977 kauth_cred_free(cred1); 978 kauth_cred_free(cred2); 979 980 /* 981 * Release reference to text vnode 982 */ 983 if (p->p_textvp) 984 vrele(p->p_textvp); 985 986 mutex_destroy(&p->p_auxlock); 987 mutex_obj_free(p->p_lock); 988 mutex_destroy(&p->p_stmutex); 989 cv_destroy(&p->p_waitcv); 990 cv_destroy(&p->p_lwpcv); 991 rw_destroy(&p->p_reflock); 992 993 proc_free_mem(p); 994 } 995 996 /* 997 * make process 'parent' the new parent of process 'child'. 998 * 999 * Must be called with proc_lock held. 1000 */ 1001 void 1002 proc_reparent(struct proc *child, struct proc *parent) 1003 { 1004 1005 KASSERT(mutex_owned(proc_lock)); 1006 1007 if (child->p_pptr == parent) 1008 return; 1009 1010 if (child->p_stat == SZOMB || 1011 (child->p_stat == SSTOP && !child->p_waited)) { 1012 child->p_pptr->p_nstopchild--; 1013 parent->p_nstopchild++; 1014 } 1015 if (parent == initproc) 1016 child->p_exitsig = SIGCHLD; 1017 1018 LIST_REMOVE(child, p_sibling); 1019 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1020 child->p_pptr = parent; 1021 child->p_ppid = parent->p_pid; 1022 } 1023