1 /* $NetBSD: kern_exit.c,v 1.284 2020/02/22 21:07:46 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2006, 2007, 2008, 2020 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.284 2020/02/22 21:07:46 ad Exp $"); 71 72 #include "opt_ktrace.h" 73 #include "opt_dtrace.h" 74 #include "opt_sysv.h" 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/ioctl.h> 79 #include <sys/tty.h> 80 #include <sys/time.h> 81 #include <sys/resource.h> 82 #include <sys/kernel.h> 83 #include <sys/proc.h> 84 #include <sys/buf.h> 85 #include <sys/wait.h> 86 #include <sys/file.h> 87 #include <sys/fstrans.h> 88 #include <sys/vnode.h> 89 #include <sys/syslog.h> 90 #include <sys/pool.h> 91 #include <sys/uidinfo.h> 92 #include <sys/ptrace.h> 93 #include <sys/acct.h> 94 #include <sys/filedesc.h> 95 #include <sys/ras.h> 96 #include <sys/signalvar.h> 97 #include <sys/sched.h> 98 #include <sys/mount.h> 99 #include <sys/syscallargs.h> 100 #include <sys/kauth.h> 101 #include <sys/sleepq.h> 102 #include <sys/lock.h> 103 #include <sys/lockdebug.h> 104 #include <sys/ktrace.h> 105 #include <sys/cpu.h> 106 #include <sys/lwpctl.h> 107 #include <sys/atomic.h> 108 #include <sys/sdt.h> 109 #include <sys/psref.h> 110 111 #include <uvm/uvm_extern.h> 112 113 #ifdef DEBUG_EXIT 114 int debug_exit = 0; 115 #define DPRINTF(x) if (debug_exit) printf x 116 #else 117 #define DPRINTF(x) 118 #endif 119 120 static int find_stopped_child(struct proc *, idtype_t, id_t, int, 121 struct proc **, struct wrusage *, siginfo_t *); 122 static void proc_free(struct proc *, struct wrusage *); 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 (p->p_xsig) { 141 if (p->p_sflag & PS_COREDUMP) 142 ksi->ksi_code = CLD_DUMPED; 143 else 144 ksi->ksi_code = CLD_KILLED; 145 ksi->ksi_status = p->p_xsig; 146 } else { 147 ksi->ksi_code = CLD_EXITED; 148 ksi->ksi_status = p->p_xexit; 149 } 150 } else { 151 ksi->ksi_code = SI_USER; 152 ksi->ksi_status = p->p_xsig; 153 } 154 /* 155 * We fill those in, even for non-SIGCHLD. 156 * It's safe to access p->p_cred unlocked here. 157 */ 158 ksi->ksi_pid = p->p_pid; 159 ksi->ksi_uid = kauth_cred_geteuid(p->p_cred); 160 /* XXX: is this still valid? */ 161 ksi->ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec; 162 ksi->ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec; 163 } 164 165 /* 166 * exit -- 167 * Death of process. 168 */ 169 int 170 sys_exit(struct lwp *l, const struct sys_exit_args *uap, register_t *retval) 171 { 172 /* { 173 syscallarg(int) rval; 174 } */ 175 struct proc *p = l->l_proc; 176 177 /* Don't call exit1() multiple times in the same process. */ 178 mutex_enter(p->p_lock); 179 if (p->p_sflag & PS_WEXIT) { 180 mutex_exit(p->p_lock); 181 lwp_exit(l); 182 } 183 184 /* exit1() will release the mutex. */ 185 exit1(l, SCARG(uap, rval), 0); 186 /* NOTREACHED */ 187 return (0); 188 } 189 190 /* 191 * Exit: deallocate address space and other resources, change proc state 192 * to zombie, and unlink proc from allproc and parent's lists. Save exit 193 * status and rusage for wait(). Check for child processes and orphan them. 194 * 195 * Must be called with p->p_lock held. Does not return. 196 */ 197 void 198 exit1(struct lwp *l, int exitcode, int signo) 199 { 200 struct proc *p, *child, *next_child, *old_parent, *new_parent; 201 struct pgrp *pgrp; 202 ksiginfo_t ksi; 203 ksiginfoq_t kq; 204 int wakeinit; 205 struct lwp *l2 __diagused; 206 207 p = l->l_proc; 208 209 /* XXX Temporary. */ 210 kernel_lock_plug_leak(); 211 212 /* Verify that we hold no locks other than p->p_lock. */ 213 LOCKDEBUG_BARRIER(p->p_lock, 0); 214 KASSERTMSG(curcpu()->ci_biglock_count == 0, "kernel_lock leaked"); 215 KASSERT(mutex_owned(p->p_lock)); 216 KASSERT(p->p_vmspace != NULL); 217 218 if (__predict_false(p == initproc)) { 219 panic("init died (signal %d, exit %d)", signo, exitcode); 220 } 221 222 p->p_sflag |= PS_WEXIT; 223 224 /* 225 * Force all other LWPs to exit before we do. Only then can we 226 * begin to tear down the rest of the process state. 227 */ 228 if (p->p_nlwps > 1) { 229 exit_lwps(l); 230 } 231 232 ksiginfo_queue_init(&kq); 233 234 /* 235 * If we have been asked to stop on exit, do so now. 236 */ 237 if (__predict_false(p->p_sflag & PS_STOPEXIT)) { 238 KERNEL_UNLOCK_ALL(l, &l->l_biglocks); 239 sigclearall(p, &contsigmask, &kq); 240 241 if (!mutex_tryenter(proc_lock)) { 242 mutex_exit(p->p_lock); 243 mutex_enter(proc_lock); 244 mutex_enter(p->p_lock); 245 } 246 p->p_waited = 0; 247 p->p_pptr->p_nstopchild++; 248 p->p_stat = SSTOP; 249 mutex_exit(proc_lock); 250 lwp_lock(l); 251 p->p_nrlwps--; 252 l->l_stat = LSSTOP; 253 lwp_unlock(l); 254 mutex_exit(p->p_lock); 255 lwp_lock(l); 256 spc_lock(l->l_cpu); 257 mi_switch(l); 258 mutex_enter(p->p_lock); 259 } 260 261 /* 262 * Bin any remaining signals and mark the process as dying so it will 263 * not be found for, e.g. signals. 264 */ 265 sigfillset(&p->p_sigctx.ps_sigignore); 266 sigclearall(p, NULL, &kq); 267 p->p_stat = SDYING; 268 mutex_exit(p->p_lock); 269 ksiginfo_queue_drain(&kq); 270 271 /* Destroy any lwpctl info. */ 272 if (p->p_lwpctl != NULL) 273 lwp_ctl_exit(); 274 275 /* 276 * Drain all remaining references that procfs, ptrace and others may 277 * have on the process. 278 */ 279 rw_enter(&p->p_reflock, RW_WRITER); 280 281 DPRINTF(("%s: %d.%d exiting.\n", __func__, p->p_pid, l->l_lid)); 282 283 timers_free(p, TIMERS_ALL); 284 #if defined(__HAVE_RAS) 285 ras_purgeall(); 286 #endif 287 288 /* 289 * Close open files, release open-file table and free signal 290 * actions. This may block! 291 */ 292 fd_free(); 293 cwdfree(p->p_cwdi); 294 p->p_cwdi = NULL; 295 doexithooks(p); 296 sigactsfree(p->p_sigacts); 297 298 /* 299 * Write out accounting data. 300 */ 301 (void)acct_process(l); 302 303 #ifdef KTRACE 304 /* 305 * Release trace file. 306 */ 307 if (p->p_tracep != NULL) { 308 mutex_enter(&ktrace_lock); 309 ktrderef(p); 310 mutex_exit(&ktrace_lock); 311 } 312 #endif 313 314 p->p_xexit = exitcode; 315 p->p_xsig = signo; 316 317 /* 318 * If emulation has process exit hook, call it now. 319 * Set the exit status now so that the exit hook has 320 * an opportunity to tweak it (COMPAT_LINUX requires 321 * this for thread group emulation) 322 */ 323 if (p->p_emul->e_proc_exit) 324 (*p->p_emul->e_proc_exit)(p); 325 326 /* 327 * Free the VM resources we're still holding on to. 328 * We must do this from a valid thread because doing 329 * so may block. This frees vmspace, which we don't 330 * need anymore. The only remaining lwp is the one 331 * we run at this moment, nothing runs in userland 332 * anymore. 333 */ 334 ruspace(p); /* Update our vm resource use */ 335 uvm_proc_exit(p); 336 337 /* 338 * Stop profiling. 339 */ 340 if (__predict_false((p->p_stflag & PST_PROFIL) != 0)) { 341 mutex_spin_enter(&p->p_stmutex); 342 stopprofclock(p); 343 mutex_spin_exit(&p->p_stmutex); 344 } 345 346 /* 347 * If parent is waiting for us to exit or exec, PL_PPWAIT is set; we 348 * wake up the parent early to avoid deadlock. We can do this once 349 * the VM resources are released. 350 */ 351 mutex_enter(proc_lock); 352 if (p->p_lflag & PL_PPWAIT) { 353 lwp_t *lp; 354 355 l->l_lwpctl = NULL; /* was on loan from blocked parent */ 356 p->p_lflag &= ~PL_PPWAIT; 357 358 lp = p->p_vforklwp; 359 p->p_vforklwp = NULL; 360 lp->l_vforkwaiting = false; 361 cv_broadcast(&lp->l_waitcv); 362 } 363 364 if (SESS_LEADER(p)) { 365 struct vnode *vprele = NULL, *vprevoke = NULL; 366 struct session *sp = p->p_session; 367 struct tty *tp; 368 369 if (sp->s_ttyvp) { 370 /* 371 * Controlling process. 372 * Signal foreground pgrp, 373 * drain controlling terminal 374 * and revoke access to controlling terminal. 375 */ 376 tp = sp->s_ttyp; 377 mutex_spin_enter(&tty_lock); 378 if (tp->t_session == sp) { 379 /* we can't guarantee the revoke will do this */ 380 pgrp = tp->t_pgrp; 381 tp->t_pgrp = NULL; 382 tp->t_session = NULL; 383 mutex_spin_exit(&tty_lock); 384 if (pgrp != NULL) { 385 pgsignal(pgrp, SIGHUP, 1); 386 } 387 mutex_exit(proc_lock); 388 (void) ttywait(tp); 389 mutex_enter(proc_lock); 390 391 /* The tty could have been revoked. */ 392 vprevoke = sp->s_ttyvp; 393 } else 394 mutex_spin_exit(&tty_lock); 395 vprele = sp->s_ttyvp; 396 sp->s_ttyvp = NULL; 397 /* 398 * s_ttyp is not zero'd; we use this to indicate 399 * that the session once had a controlling terminal. 400 * (for logging and informational purposes) 401 */ 402 } 403 sp->s_leader = NULL; 404 405 if (vprevoke != NULL || vprele != NULL) { 406 if (vprevoke != NULL) { 407 /* Releases proc_lock. */ 408 proc_sessrele(sp); 409 VOP_REVOKE(vprevoke, REVOKEALL); 410 } else 411 mutex_exit(proc_lock); 412 if (vprele != NULL) 413 vrele(vprele); 414 mutex_enter(proc_lock); 415 } 416 } 417 fixjobc(p, p->p_pgrp, 0); 418 419 /* Release fstrans private data. */ 420 fstrans_lwp_dtor(l); 421 422 /* 423 * Finalize the last LWP's specificdata, as well as the 424 * specificdata for the proc itself. 425 */ 426 lwp_finispecific(l); 427 proc_finispecific(p); 428 429 /* 430 * Notify interested parties of our demise. 431 */ 432 KNOTE(&p->p_klist, NOTE_EXIT); 433 434 SDT_PROBE(proc, kernel, , exit, 435 ((p->p_sflag & PS_COREDUMP) ? CLD_DUMPED : 436 (p->p_xsig ? CLD_KILLED : CLD_EXITED)), 437 0,0,0,0); 438 439 /* 440 * Reset p_opptr pointer of all former children which got 441 * traced by another process and were reparented. We reset 442 * it to NULL here; the trace detach code then reparents 443 * the child to initproc. We only check allproc list, since 444 * eventual former children on zombproc list won't reference 445 * p_opptr anymore. 446 */ 447 if (__predict_false(p->p_slflag & PSL_CHTRACED)) { 448 struct proc *q; 449 PROCLIST_FOREACH(q, &allproc) { 450 if (q->p_opptr == p) 451 q->p_opptr = NULL; 452 } 453 PROCLIST_FOREACH(q, &zombproc) { 454 if (q->p_opptr == p) 455 q->p_opptr = NULL; 456 } 457 } 458 459 /* 460 * Give orphaned children to init(8). 461 */ 462 child = LIST_FIRST(&p->p_children); 463 wakeinit = (child != NULL); 464 for (; child != NULL; child = next_child) { 465 next_child = LIST_NEXT(child, p_sibling); 466 467 /* 468 * Traced processes are killed since their existence 469 * means someone is screwing up. Since we reset the 470 * trace flags, the logic in sys_wait4() would not be 471 * triggered to reparent the process to its 472 * original parent, so we must do this here. 473 */ 474 if (__predict_false(child->p_slflag & PSL_TRACED)) { 475 mutex_enter(p->p_lock); 476 child->p_slflag &= 477 ~(PSL_TRACED|PSL_SYSCALL); 478 mutex_exit(p->p_lock); 479 if (child->p_opptr != child->p_pptr) { 480 struct proc *t = child->p_opptr; 481 proc_reparent(child, t ? t : initproc); 482 child->p_opptr = NULL; 483 } else 484 proc_reparent(child, initproc); 485 killproc(child, "orphaned traced process"); 486 } else 487 proc_reparent(child, initproc); 488 } 489 490 /* 491 * Move proc from allproc to zombproc, it's now nearly ready to be 492 * collected by parent. 493 */ 494 LIST_REMOVE(l, l_list); 495 LIST_REMOVE(p, p_list); 496 LIST_INSERT_HEAD(&zombproc, p, p_list); 497 498 /* 499 * Mark the process as dead. We must do this before we signal 500 * the parent. 501 */ 502 p->p_stat = SDEAD; 503 504 /* Put in front of parent's sibling list for parent to collect it */ 505 old_parent = p->p_pptr; 506 old_parent->p_nstopchild++; 507 if (LIST_FIRST(&old_parent->p_children) != p) { 508 /* Put child where it can be found quickly */ 509 LIST_REMOVE(p, p_sibling); 510 LIST_INSERT_HEAD(&old_parent->p_children, p, p_sibling); 511 } 512 513 /* 514 * Notify parent that we're gone. If parent has the P_NOCLDWAIT 515 * flag set, notify init instead (and hope it will handle 516 * this situation). 517 */ 518 if (old_parent->p_flag & (PK_NOCLDWAIT|PK_CLDSIGIGN)) { 519 proc_reparent(p, initproc); 520 wakeinit = 1; 521 522 /* 523 * If this was the last child of our parent, notify 524 * parent, so in case he was wait(2)ing, he will 525 * continue. 526 */ 527 if (LIST_FIRST(&old_parent->p_children) == NULL) 528 cv_broadcast(&old_parent->p_waitcv); 529 } 530 531 /* Reload parent pointer, since p may have been reparented above */ 532 new_parent = p->p_pptr; 533 534 if (__predict_false(p->p_exitsig != 0)) { 535 exit_psignal(p, new_parent, &ksi); 536 kpsignal(new_parent, &ksi, NULL); 537 } 538 539 /* Calculate the final rusage info. */ 540 calcru(p, &p->p_stats->p_ru.ru_utime, &p->p_stats->p_ru.ru_stime, 541 NULL, NULL); 542 543 if (wakeinit) 544 cv_broadcast(&initproc->p_waitcv); 545 546 callout_destroy(&l->l_timeout_ch); 547 548 /* 549 * Release any PCU resources before becoming a zombie. 550 */ 551 pcu_discard_all(l); 552 553 mutex_enter(p->p_lock); 554 /* Don't bother with p_treelock as no other LWPs remain. */ 555 l2 = radix_tree_remove_node(&p->p_lwptree, (uint64_t)(l->l_lid - 1)); 556 KASSERT(l2 == l); 557 KASSERT(radix_tree_empty_tree_p(&p->p_lwptree)); 558 radix_tree_fini_tree(&p->p_lwptree); 559 /* Free the linux lwp id */ 560 if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid) 561 proc_free_pid(l->l_lid); 562 lwp_drainrefs(l); 563 lwp_lock(l); 564 l->l_prflag &= ~LPR_DETACHED; 565 l->l_stat = LSZOMB; 566 lwp_unlock(l); 567 KASSERT(curlwp == l); 568 KASSERT(p->p_nrlwps == 1); 569 KASSERT(p->p_nlwps == 1); 570 p->p_stat = SZOMB; 571 p->p_nrlwps--; 572 p->p_nzlwps++; 573 p->p_ndlwps = 0; 574 mutex_exit(p->p_lock); 575 576 /* 577 * Signal the parent to collect us, and drop the proclist lock. 578 * Drop debugger/procfs lock; no new references can be gained. 579 */ 580 cv_broadcast(&p->p_pptr->p_waitcv); 581 rw_exit(&p->p_reflock); 582 mutex_exit(proc_lock); 583 584 /* 585 * NOTE: WE ARE NO LONGER ALLOWED TO SLEEP! 586 */ 587 588 /* 589 * Give machine-dependent code a chance to free any MD LWP 590 * resources. This must be done before uvm_lwp_exit(), in 591 * case these resources are in the PCB. 592 */ 593 cpu_lwp_free(l, 1); 594 595 /* Switch away into oblivion. */ 596 lwp_lock(l); 597 spc_lock(l->l_cpu); 598 mi_switch(l); 599 panic("exit1"); 600 } 601 602 void 603 exit_lwps(struct lwp *l) 604 { 605 proc_t *p = l->l_proc; 606 lwp_t *l2; 607 608 retry: 609 KASSERT(mutex_owned(p->p_lock)); 610 611 /* 612 * Interrupt LWPs in interruptable sleep, unsuspend suspended 613 * LWPs and then wait for everyone else to finish. 614 */ 615 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 616 if (l2 == l) 617 continue; 618 lwp_lock(l2); 619 l2->l_flag |= LW_WEXIT; 620 if ((l2->l_stat == LSSLEEP && (l2->l_flag & LW_SINTR)) || 621 l2->l_stat == LSSUSPENDED || l2->l_stat == LSSTOP) { 622 l2->l_flag &= ~LW_DBGSUSPEND; 623 /* setrunnable() will release the lock. */ 624 setrunnable(l2); 625 continue; 626 } 627 lwp_need_userret(l2); 628 lwp_unlock(l2); 629 } 630 631 /* 632 * Wait for every LWP to exit. Note: LWPs can get suspended/slept 633 * behind us or there may even be new LWPs created. Therefore, a 634 * full retry is required on error. 635 */ 636 while (p->p_nlwps > 1) { 637 if (lwp_wait(l, 0, NULL, true)) { 638 goto retry; 639 } 640 } 641 642 KASSERT(p->p_nlwps == 1); 643 } 644 645 int 646 do_sys_waitid(idtype_t idtype, id_t id, int *pid, int *status, int options, 647 struct wrusage *wru, siginfo_t *si) 648 { 649 proc_t *child; 650 int error; 651 652 653 if (wru != NULL) 654 memset(wru, 0, sizeof(*wru)); 655 if (si != NULL) 656 memset(si, 0, sizeof(*si)); 657 658 mutex_enter(proc_lock); 659 error = find_stopped_child(curproc, idtype, id, options, &child, 660 wru, si); 661 if (child == NULL) { 662 mutex_exit(proc_lock); 663 *pid = 0; 664 *status = 0; 665 return error; 666 } 667 *pid = child->p_pid; 668 669 if (child->p_stat == SZOMB) { 670 /* Child is exiting */ 671 *status = P_WAITSTATUS(child); 672 /* proc_free() will release the proc_lock. */ 673 if (options & WNOWAIT) { 674 mutex_exit(proc_lock); 675 } else { 676 proc_free(child, wru); 677 } 678 } else { 679 /* Don't mark SIGCONT if we are being stopped */ 680 *status = (child->p_xsig == SIGCONT && child->p_stat != SSTOP) ? 681 W_CONTCODE() : W_STOPCODE(child->p_xsig); 682 mutex_exit(proc_lock); 683 } 684 return 0; 685 } 686 687 int 688 do_sys_wait(int *pid, int *status, int options, struct rusage *ru) 689 { 690 idtype_t idtype; 691 id_t id; 692 int ret; 693 struct wrusage wru; 694 695 /* 696 * Translate the special pid values into the (idtype, pid) 697 * pair for wait6. The WAIT_MYPGRP case is handled by 698 * find_stopped_child() on its own. 699 */ 700 if (*pid == WAIT_ANY) { 701 idtype = P_ALL; 702 id = 0; 703 } else if (*pid < 0) { 704 idtype = P_PGID; 705 id = (id_t)-*pid; 706 } else { 707 idtype = P_PID; 708 id = (id_t)*pid; 709 } 710 options |= WEXITED | WTRAPPED; 711 ret = do_sys_waitid(idtype, id, pid, status, options, ru ? &wru : NULL, 712 NULL); 713 if (ru) 714 *ru = wru.wru_self; 715 return ret; 716 } 717 718 int 719 sys___wait450(struct lwp *l, const struct sys___wait450_args *uap, 720 register_t *retval) 721 { 722 /* { 723 syscallarg(int) pid; 724 syscallarg(int *) status; 725 syscallarg(int) options; 726 syscallarg(struct rusage *) rusage; 727 } */ 728 int error, status, pid = SCARG(uap, pid); 729 struct rusage ru; 730 731 error = do_sys_wait(&pid, &status, SCARG(uap, options), 732 SCARG(uap, rusage) != NULL ? &ru : NULL); 733 734 retval[0] = pid; 735 if (pid == 0) { 736 return error; 737 } 738 if (SCARG(uap, status)) { 739 error = copyout(&status, SCARG(uap, status), sizeof(status)); 740 } 741 if (SCARG(uap, rusage) && error == 0) { 742 error = copyout(&ru, SCARG(uap, rusage), sizeof(ru)); 743 } 744 return error; 745 } 746 747 int 748 sys_wait6(struct lwp *l, const struct sys_wait6_args *uap, register_t *retval) 749 { 750 /* { 751 syscallarg(idtype_t) idtype; 752 syscallarg(id_t) id; 753 syscallarg(int *) status; 754 syscallarg(int) options; 755 syscallarg(struct wrusage *) wru; 756 syscallarg(siginfo_t *) si; 757 } */ 758 struct wrusage wru, *wrup; 759 siginfo_t si, *sip; 760 idtype_t idtype; 761 int pid; 762 id_t id; 763 int error, status; 764 765 idtype = SCARG(uap, idtype); 766 id = SCARG(uap, id); 767 768 if (SCARG(uap, wru) != NULL) 769 wrup = &wru; 770 else 771 wrup = NULL; 772 773 if (SCARG(uap, info) != NULL) 774 sip = &si; 775 else 776 sip = NULL; 777 778 /* 779 * We expect all callers of wait6() to know about WEXITED and 780 * WTRAPPED. 781 */ 782 error = do_sys_waitid(idtype, id, &pid, &status, SCARG(uap, options), 783 wrup, sip); 784 785 retval[0] = pid; /* tell userland who it was */ 786 787 #if 0 788 /* 789 * should we copyout if there was no process, hence no useful data? 790 * We don't for an old sytle wait4() (etc) but I believe 791 * FreeBSD does for wait6(), so a tossup... Go with FreeBSD for now. 792 */ 793 if (pid == 0) 794 return error; 795 #endif 796 797 if (SCARG(uap, status) != NULL && error == 0) 798 error = copyout(&status, SCARG(uap, status), sizeof(status)); 799 if (SCARG(uap, wru) != NULL && error == 0) 800 error = copyout(&wru, SCARG(uap, wru), sizeof(wru)); 801 if (SCARG(uap, info) != NULL && error == 0) 802 error = copyout(&si, SCARG(uap, info), sizeof(si)); 803 return error; 804 } 805 806 807 /* 808 * Find a process that matches the provided criteria, and fill siginfo 809 * and resources if found. 810 * Returns: 811 * -1: Not found, abort early 812 * 0: Not matched 813 * 1: Matched, there might be more matches 814 * 2: This is the only match 815 */ 816 static int 817 match_process(const struct proc *pp, struct proc **q, idtype_t idtype, id_t id, 818 int options, struct wrusage *wrusage, siginfo_t *siginfo) 819 { 820 struct rusage *rup; 821 struct proc *p = *q; 822 int rv = 1; 823 824 mutex_enter(p->p_lock); 825 switch (idtype) { 826 case P_ALL: 827 break; 828 case P_PID: 829 if (p->p_pid != (pid_t)id) { 830 mutex_exit(p->p_lock); 831 p = *q = proc_find_raw((pid_t)id); 832 if (p == NULL || p->p_stat == SIDL || p->p_pptr != pp) { 833 *q = NULL; 834 return -1; 835 } 836 mutex_enter(p->p_lock); 837 } 838 rv++; 839 break; 840 case P_PGID: 841 if (p->p_pgid != (pid_t)id) 842 goto out; 843 break; 844 case P_SID: 845 if (p->p_session->s_sid != (pid_t)id) 846 goto out; 847 break; 848 case P_UID: 849 if (kauth_cred_geteuid(p->p_cred) != (uid_t)id) 850 goto out; 851 break; 852 case P_GID: 853 if (kauth_cred_getegid(p->p_cred) != (gid_t)id) 854 goto out; 855 break; 856 case P_CID: 857 case P_PSETID: 858 case P_CPUID: 859 /* XXX: Implement me */ 860 default: 861 out: 862 mutex_exit(p->p_lock); 863 return 0; 864 } 865 866 if ((options & WEXITED) == 0 && p->p_stat == SZOMB) 867 goto out; 868 869 if (siginfo != NULL) { 870 siginfo->si_errno = 0; 871 872 /* 873 * SUSv4 requires that the si_signo value is always 874 * SIGCHLD. Obey it despite the rfork(2) interface 875 * allows to request other signal for child exit 876 * notification. 877 */ 878 siginfo->si_signo = SIGCHLD; 879 880 /* 881 * This is still a rough estimate. We will fix the 882 * cases TRAPPED, STOPPED, and CONTINUED later. 883 */ 884 if (p->p_sflag & PS_COREDUMP) { 885 siginfo->si_code = CLD_DUMPED; 886 siginfo->si_status = p->p_xsig; 887 } else if (p->p_xsig) { 888 siginfo->si_code = CLD_KILLED; 889 siginfo->si_status = p->p_xsig; 890 } else { 891 siginfo->si_code = CLD_EXITED; 892 siginfo->si_status = p->p_xexit; 893 } 894 895 siginfo->si_pid = p->p_pid; 896 siginfo->si_uid = kauth_cred_geteuid(p->p_cred); 897 siginfo->si_utime = p->p_stats->p_ru.ru_utime.tv_sec; 898 siginfo->si_stime = p->p_stats->p_ru.ru_stime.tv_sec; 899 } 900 901 /* 902 * There should be no reason to limit resources usage info to 903 * exited processes only. A snapshot about any resources used 904 * by a stopped process may be exactly what is needed. 905 */ 906 if (wrusage != NULL) { 907 rup = &wrusage->wru_self; 908 *rup = p->p_stats->p_ru; 909 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL); 910 911 rup = &wrusage->wru_children; 912 *rup = p->p_stats->p_cru; 913 calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL); 914 } 915 916 mutex_exit(p->p_lock); 917 return rv; 918 } 919 920 /* 921 * Determine if there are existing processes being debugged 922 * that used to be (and sometime later will be again) children 923 * of a specific parent (while matching wait criteria) 924 */ 925 static bool 926 debugged_child_exists(idtype_t idtype, id_t id, int options, siginfo_t *si, 927 const struct proc *parent) 928 { 929 struct proc *pp; 930 931 /* 932 * If we are searching for a specific pid, we can optimise a little 933 */ 934 if (idtype == P_PID) { 935 /* 936 * Check the specific process to see if its real parent is us 937 */ 938 pp = proc_find_raw((pid_t)id); 939 if (pp != NULL && pp->p_stat != SIDL && pp->p_opptr == parent) { 940 /* 941 * using P_ALL here avoids match_process() doing the 942 * same work that we just did, but incorrectly for 943 * this scenario. 944 */ 945 if (match_process(parent, &pp, P_ALL, id, options, 946 NULL, si)) 947 return true; 948 } 949 return false; 950 } 951 952 /* 953 * For the hard cases, just look everywhere to see if some 954 * stolen (reparented) process is really our lost child. 955 * Then check if that process could satisfy the wait conditions. 956 */ 957 958 /* 959 * XXX inefficient, but hopefully fairly rare. 960 * XXX should really use a list of reparented processes. 961 */ 962 PROCLIST_FOREACH(pp, &allproc) { 963 if (pp->p_stat == SIDL) /* XXX impossible ?? */ 964 continue; 965 if (pp->p_opptr == parent && 966 match_process(parent, &pp, idtype, id, options, NULL, si)) 967 return true; 968 } 969 PROCLIST_FOREACH(pp, &zombproc) { 970 if (pp->p_stat == SIDL) /* XXX impossible ?? */ 971 continue; 972 if (pp->p_opptr == parent && 973 match_process(parent, &pp, idtype, id, options, NULL, si)) 974 return true; 975 } 976 977 return false; 978 } 979 980 /* 981 * Scan list of child processes for a child process that has stopped or 982 * exited. Used by sys_wait4 and 'compat' equivalents. 983 * 984 * Must be called with the proc_lock held, and may release while waiting. 985 */ 986 static int 987 find_stopped_child(struct proc *parent, idtype_t idtype, id_t id, int options, 988 struct proc **child_p, struct wrusage *wru, siginfo_t *si) 989 { 990 struct proc *child, *dead; 991 int error; 992 993 KASSERT(mutex_owned(proc_lock)); 994 995 if (options & ~WALLOPTS) { 996 *child_p = NULL; 997 return EINVAL; 998 } 999 1000 if ((options & WSELECTOPTS) == 0) { 1001 /* 1002 * We will be unable to find any matching processes, 1003 * because there are no known events to look for. 1004 * Prefer to return error instead of blocking 1005 * indefinitely. 1006 */ 1007 *child_p = NULL; 1008 return EINVAL; 1009 } 1010 1011 if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1012 mutex_enter(parent->p_lock); 1013 id = (id_t)parent->p_pgid; 1014 mutex_exit(parent->p_lock); 1015 idtype = P_PGID; 1016 } 1017 1018 for (;;) { 1019 error = ECHILD; 1020 dead = NULL; 1021 1022 LIST_FOREACH(child, &parent->p_children, p_sibling) { 1023 int rv = match_process(parent, &child, idtype, id, 1024 options, wru, si); 1025 if (rv == -1) 1026 break; 1027 if (rv == 0) 1028 continue; 1029 1030 /* 1031 * Wait for processes with p_exitsig != SIGCHLD 1032 * processes only if WALTSIG is set; wait for 1033 * processes with p_exitsig == SIGCHLD only 1034 * if WALTSIG is clear. 1035 */ 1036 if (((options & WALLSIG) == 0) && 1037 (options & WALTSIG ? child->p_exitsig == SIGCHLD 1038 : P_EXITSIG(child) != SIGCHLD)){ 1039 if (rv == 2) { 1040 child = NULL; 1041 break; 1042 } 1043 continue; 1044 } 1045 1046 error = 0; 1047 if ((options & WNOZOMBIE) == 0) { 1048 if (child->p_stat == SZOMB) 1049 break; 1050 if (child->p_stat == SDEAD) { 1051 /* 1052 * We may occasionally arrive here 1053 * after receiving a signal, but 1054 * immediately before the child 1055 * process is zombified. The wait 1056 * will be short, so avoid returning 1057 * to userspace. 1058 */ 1059 dead = child; 1060 } 1061 } 1062 1063 if ((options & WCONTINUED) != 0 && 1064 child->p_xsig == SIGCONT && 1065 (child->p_sflag & PS_CONTINUED)) { 1066 if ((options & WNOWAIT) == 0) { 1067 child->p_sflag &= ~PS_CONTINUED; 1068 child->p_waited = 1; 1069 parent->p_nstopchild--; 1070 } 1071 if (si) { 1072 si->si_status = child->p_xsig; 1073 si->si_code = CLD_CONTINUED; 1074 } 1075 break; 1076 } 1077 1078 if ((options & (WTRAPPED|WSTOPPED)) != 0 && 1079 child->p_stat == SSTOP && 1080 child->p_waited == 0 && 1081 ((child->p_slflag & PSL_TRACED) || 1082 options & (WUNTRACED|WSTOPPED))) { 1083 if ((options & WNOWAIT) == 0) { 1084 child->p_waited = 1; 1085 parent->p_nstopchild--; 1086 } 1087 if (si) { 1088 si->si_status = child->p_xsig; 1089 si->si_code = 1090 (child->p_slflag & PSL_TRACED) ? 1091 CLD_TRAPPED : CLD_STOPPED; 1092 } 1093 break; 1094 } 1095 if (parent->p_nstopchild == 0 || rv == 2) { 1096 child = NULL; 1097 break; 1098 } 1099 } 1100 1101 /* 1102 * If we found nothing, but we are the bereaved parent 1103 * of a stolen child, look and see if that child (or 1104 * one of them) meets our search criteria. If so, then 1105 * we cannot succeed, but we can hang (wait...), 1106 * or if WNOHANG, return 0 instead of ECHILD 1107 */ 1108 if (child == NULL && error == ECHILD && 1109 (parent->p_slflag & PSL_CHTRACED) && 1110 debugged_child_exists(idtype, id, options, si, parent)) 1111 error = 0; 1112 1113 if (child != NULL || error != 0 || 1114 ((options & WNOHANG) != 0 && dead == NULL)) { 1115 *child_p = child; 1116 return error; 1117 } 1118 1119 /* 1120 * Wait for another child process to stop. 1121 */ 1122 error = cv_wait_sig(&parent->p_waitcv, proc_lock); 1123 1124 if (error != 0) { 1125 *child_p = NULL; 1126 return error; 1127 } 1128 } 1129 } 1130 1131 /* 1132 * Free a process after parent has taken all the state info. Must be called 1133 * with the proclist lock held, and will release before returning. 1134 * 1135 * *ru is returned to the caller, and must be freed by the caller. 1136 */ 1137 static void 1138 proc_free(struct proc *p, struct wrusage *wru) 1139 { 1140 struct proc *parent = p->p_pptr; 1141 struct lwp *l; 1142 ksiginfo_t ksi; 1143 kauth_cred_t cred1, cred2; 1144 uid_t uid; 1145 1146 KASSERT(mutex_owned(proc_lock)); 1147 KASSERT(p->p_nlwps == 1); 1148 KASSERT(p->p_nzlwps == 1); 1149 KASSERT(p->p_nrlwps == 0); 1150 KASSERT(p->p_stat == SZOMB); 1151 1152 /* 1153 * If we got the child via ptrace(2) or procfs, and 1154 * the parent is different (meaning the process was 1155 * attached, rather than run as a child), then we need 1156 * to give it back to the old parent, and send the 1157 * parent the exit signal. The rest of the cleanup 1158 * will be done when the old parent waits on the child. 1159 */ 1160 if ((p->p_slflag & PSL_TRACED) != 0 && p->p_opptr != parent) { 1161 mutex_enter(p->p_lock); 1162 p->p_slflag &= ~(PSL_TRACED|PSL_SYSCALL); 1163 mutex_exit(p->p_lock); 1164 parent = (p->p_opptr == NULL) ? initproc : p->p_opptr; 1165 proc_reparent(p, parent); 1166 p->p_opptr = NULL; 1167 if (p->p_exitsig != 0) { 1168 exit_psignal(p, parent, &ksi); 1169 kpsignal(parent, &ksi, NULL); 1170 } 1171 cv_broadcast(&parent->p_waitcv); 1172 mutex_exit(proc_lock); 1173 return; 1174 } 1175 1176 sched_proc_exit(parent, p); 1177 1178 /* 1179 * Add child times of exiting process onto its own times. 1180 * This cannot be done any earlier else it might get done twice. 1181 */ 1182 l = LIST_FIRST(&p->p_lwps); 1183 p->p_stats->p_ru.ru_nvcsw += (l->l_ncsw - l->l_nivcsw); 1184 p->p_stats->p_ru.ru_nivcsw += l->l_nivcsw; 1185 ruadd(&p->p_stats->p_ru, &l->l_ru); 1186 ruadd(&p->p_stats->p_ru, &p->p_stats->p_cru); 1187 ruadd(&parent->p_stats->p_cru, &p->p_stats->p_ru); 1188 if (wru != NULL) { 1189 wru->wru_self = p->p_stats->p_ru; 1190 wru->wru_children = p->p_stats->p_cru; 1191 } 1192 p->p_xsig = 0; 1193 p->p_xexit = 0; 1194 1195 /* 1196 * At this point we are going to start freeing the final resources. 1197 * If anyone tries to access the proc structure after here they will 1198 * get a shock - bits are missing. Attempt to make it hard! We 1199 * don't bother with any further locking past this point. 1200 */ 1201 p->p_stat = SIDL; /* not even a zombie any more */ 1202 LIST_REMOVE(p, p_list); /* off zombproc */ 1203 parent->p_nstopchild--; 1204 LIST_REMOVE(p, p_sibling); 1205 1206 /* 1207 * Let pid be reallocated. 1208 */ 1209 proc_free_pid(p->p_pid); 1210 1211 /* 1212 * Unlink process from its process group. 1213 * Releases the proc_lock. 1214 */ 1215 proc_leavepgrp(p); 1216 1217 /* 1218 * Delay release until after lwp_free. 1219 */ 1220 cred2 = l->l_cred; 1221 1222 /* 1223 * Free the last LWP's resources. 1224 * 1225 * lwp_free ensures the LWP is no longer running on another CPU. 1226 */ 1227 lwp_free(l, false, true); 1228 1229 /* 1230 * Now no one except us can reach the process p. 1231 */ 1232 1233 /* 1234 * Decrement the count of procs running with this uid. 1235 */ 1236 cred1 = p->p_cred; 1237 uid = kauth_cred_getuid(cred1); 1238 (void)chgproccnt(uid, -1); 1239 1240 /* 1241 * Release substructures. 1242 */ 1243 1244 lim_free(p->p_limit); 1245 pstatsfree(p->p_stats); 1246 kauth_cred_free(cred1); 1247 kauth_cred_free(cred2); 1248 1249 /* 1250 * Release reference to text vnode 1251 */ 1252 if (p->p_textvp) 1253 vrele(p->p_textvp); 1254 kmem_strfree(p->p_path); 1255 1256 mutex_destroy(&p->p_auxlock); 1257 mutex_obj_free(p->p_lock); 1258 mutex_destroy(&p->p_stmutex); 1259 cv_destroy(&p->p_waitcv); 1260 cv_destroy(&p->p_lwpcv); 1261 rw_destroy(&p->p_reflock); 1262 rw_destroy(&p->p_treelock); 1263 1264 proc_free_mem(p); 1265 } 1266 1267 /* 1268 * Change the parent of a process for tracing purposes. 1269 */ 1270 void 1271 proc_changeparent(struct proc *t, struct proc *p) 1272 { 1273 SET(t->p_slflag, PSL_TRACED); 1274 t->p_opptr = t->p_pptr; 1275 if (t->p_pptr == p) 1276 return; 1277 struct proc *parent = t->p_pptr; 1278 1279 if (parent->p_lock < t->p_lock) { 1280 if (!mutex_tryenter(parent->p_lock)) { 1281 mutex_exit(t->p_lock); 1282 mutex_enter(parent->p_lock); 1283 mutex_enter(t->p_lock); 1284 } 1285 } else if (parent->p_lock > t->p_lock) { 1286 mutex_enter(parent->p_lock); 1287 } 1288 parent->p_slflag |= PSL_CHTRACED; 1289 proc_reparent(t, p); 1290 if (parent->p_lock != t->p_lock) 1291 mutex_exit(parent->p_lock); 1292 } 1293 1294 /* 1295 * make process 'parent' the new parent of process 'child'. 1296 * 1297 * Must be called with proc_lock held. 1298 */ 1299 void 1300 proc_reparent(struct proc *child, struct proc *parent) 1301 { 1302 1303 KASSERT(mutex_owned(proc_lock)); 1304 1305 if (child->p_pptr == parent) 1306 return; 1307 1308 if (child->p_stat == SZOMB || child->p_stat == SDEAD || 1309 (child->p_stat == SSTOP && !child->p_waited)) { 1310 child->p_pptr->p_nstopchild--; 1311 parent->p_nstopchild++; 1312 } 1313 if (parent == initproc) { 1314 child->p_exitsig = SIGCHLD; 1315 child->p_ppid = parent->p_pid; 1316 } 1317 1318 LIST_REMOVE(child, p_sibling); 1319 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1320 child->p_pptr = parent; 1321 } 1322