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