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