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