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