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