1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_ddb.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/sysproto.h> 48 #include <sys/capsicum.h> 49 #include <sys/eventhandler.h> 50 #include <sys/kernel.h> 51 #include <sys/ktr.h> 52 #include <sys/malloc.h> 53 #include <sys/lock.h> 54 #include <sys/mutex.h> 55 #include <sys/proc.h> 56 #include <sys/procdesc.h> 57 #include <sys/jail.h> 58 #include <sys/tty.h> 59 #include <sys/wait.h> 60 #include <sys/vmmeter.h> 61 #include <sys/vnode.h> 62 #include <sys/racct.h> 63 #include <sys/resourcevar.h> 64 #include <sys/sbuf.h> 65 #include <sys/signalvar.h> 66 #include <sys/sched.h> 67 #include <sys/sx.h> 68 #include <sys/syscallsubr.h> 69 #include <sys/sysctl.h> 70 #include <sys/syslog.h> 71 #include <sys/ptrace.h> 72 #include <sys/acct.h> /* for acct_process() function prototype */ 73 #include <sys/filedesc.h> 74 #include <sys/sdt.h> 75 #include <sys/shm.h> 76 #include <sys/sem.h> 77 #include <sys/sysent.h> 78 #include <sys/timers.h> 79 #include <sys/umtx.h> 80 #ifdef KTRACE 81 #include <sys/ktrace.h> 82 #endif 83 84 #include <security/audit/audit.h> 85 #include <security/mac/mac_framework.h> 86 87 #include <vm/vm.h> 88 #include <vm/vm_extern.h> 89 #include <vm/vm_param.h> 90 #include <vm/pmap.h> 91 #include <vm/vm_map.h> 92 #include <vm/vm_page.h> 93 #include <vm/uma.h> 94 95 #ifdef KDTRACE_HOOKS 96 #include <sys/dtrace_bsd.h> 97 dtrace_execexit_func_t dtrace_fasttrap_exit; 98 #endif 99 100 SDT_PROVIDER_DECLARE(proc); 101 SDT_PROBE_DEFINE1(proc, , , exit, "int"); 102 103 static int kern_kill_on_dbg_exit = 1; 104 SYSCTL_INT(_kern, OID_AUTO, kill_on_debugger_exit, CTLFLAG_RWTUN, 105 &kern_kill_on_dbg_exit, 0, 106 "Kill ptraced processes when debugger exits"); 107 108 struct proc * 109 proc_realparent(struct proc *child) 110 { 111 struct proc *p, *parent; 112 113 sx_assert(&proctree_lock, SX_LOCKED); 114 if ((child->p_treeflag & P_TREE_ORPHANED) == 0) 115 return (child->p_pptr->p_pid == child->p_oppid ? 116 child->p_pptr : child->p_reaper); 117 for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) { 118 /* Cannot use LIST_PREV(), since the list head is not known. */ 119 p = __containerof(p->p_orphan.le_prev, struct proc, 120 p_orphan.le_next); 121 KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0, 122 ("missing P_ORPHAN %p", p)); 123 } 124 parent = __containerof(p->p_orphan.le_prev, struct proc, 125 p_orphans.lh_first); 126 return (parent); 127 } 128 129 void 130 reaper_abandon_children(struct proc *p, bool exiting) 131 { 132 struct proc *p1, *p2, *ptmp; 133 134 sx_assert(&proctree_lock, SX_LOCKED); 135 KASSERT(p != initproc, ("reaper_abandon_children for initproc")); 136 if ((p->p_treeflag & P_TREE_REAPER) == 0) 137 return; 138 p1 = p->p_reaper; 139 LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) { 140 LIST_REMOVE(p2, p_reapsibling); 141 p2->p_reaper = p1; 142 p2->p_reapsubtree = p->p_reapsubtree; 143 LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling); 144 if (exiting && p2->p_pptr == p) { 145 PROC_LOCK(p2); 146 proc_reparent(p2, p1, true); 147 PROC_UNLOCK(p2); 148 } 149 } 150 KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty")); 151 p->p_treeflag &= ~P_TREE_REAPER; 152 } 153 154 static void 155 reaper_clear(struct proc *p) 156 { 157 struct proc *p1; 158 bool clear; 159 160 sx_assert(&proctree_lock, SX_LOCKED); 161 LIST_REMOVE(p, p_reapsibling); 162 if (p->p_reapsubtree == 1) 163 return; 164 clear = true; 165 LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) { 166 if (p1->p_reapsubtree == p->p_reapsubtree) { 167 clear = false; 168 break; 169 } 170 } 171 if (clear) 172 proc_id_clear(PROC_ID_REAP, p->p_reapsubtree); 173 } 174 175 void 176 proc_clear_orphan(struct proc *p) 177 { 178 struct proc *p1; 179 180 sx_assert(&proctree_lock, SA_XLOCKED); 181 if ((p->p_treeflag & P_TREE_ORPHANED) == 0) 182 return; 183 if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) { 184 p1 = LIST_NEXT(p, p_orphan); 185 if (p1 != NULL) 186 p1->p_treeflag |= P_TREE_FIRST_ORPHAN; 187 p->p_treeflag &= ~P_TREE_FIRST_ORPHAN; 188 } 189 LIST_REMOVE(p, p_orphan); 190 p->p_treeflag &= ~P_TREE_ORPHANED; 191 } 192 193 /* 194 * exit -- death of process. 195 */ 196 void 197 sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 198 { 199 200 exit1(td, uap->rval, 0); 201 /* NOTREACHED */ 202 } 203 204 /* 205 * Exit: deallocate address space and other resources, change proc state to 206 * zombie, and unlink proc from allproc and parent's lists. Save exit status 207 * and rusage for wait(). Check for child processes and orphan them. 208 */ 209 void 210 exit1(struct thread *td, int rval, int signo) 211 { 212 struct proc *p, *nq, *q, *t; 213 struct thread *tdt; 214 ksiginfo_t *ksi, *ksi1; 215 int signal_parent; 216 217 mtx_assert(&Giant, MA_NOTOWNED); 218 KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo)); 219 220 p = td->td_proc; 221 /* 222 * XXX in case we're rebooting we just let init die in order to 223 * work around an unsolved stack overflow seen very late during 224 * shutdown on sparc64 when the gmirror worker process exists. 225 * XXX what to do now that sparc64 is gone... remove if? 226 */ 227 if (p == initproc && rebooting == 0) { 228 printf("init died (signal %d, exit %d)\n", signo, rval); 229 panic("Going nowhere without my init!"); 230 } 231 232 /* 233 * Deref SU mp, since the thread does not return to userspace. 234 */ 235 td_softdep_cleanup(td); 236 237 /* 238 * MUST abort all other threads before proceeding past here. 239 */ 240 PROC_LOCK(p); 241 /* 242 * First check if some other thread or external request got 243 * here before us. If so, act appropriately: exit or suspend. 244 * We must ensure that stop requests are handled before we set 245 * P_WEXIT. 246 */ 247 thread_suspend_check(0); 248 while (p->p_flag & P_HADTHREADS) { 249 /* 250 * Kill off the other threads. This requires 251 * some co-operation from other parts of the kernel 252 * so it may not be instantaneous. With this state set 253 * any thread entering the kernel from userspace will 254 * thread_exit() in trap(). Any thread attempting to 255 * sleep will return immediately with EINTR or EWOULDBLOCK 256 * which will hopefully force them to back out to userland 257 * freeing resources as they go. Any thread attempting 258 * to return to userland will thread_exit() from userret(). 259 * thread_exit() will unsuspend us when the last of the 260 * other threads exits. 261 * If there is already a thread singler after resumption, 262 * calling thread_single will fail; in that case, we just 263 * re-check all suspension request, the thread should 264 * either be suspended there or exit. 265 */ 266 if (!thread_single(p, SINGLE_EXIT)) 267 /* 268 * All other activity in this process is now 269 * stopped. Threading support has been turned 270 * off. 271 */ 272 break; 273 /* 274 * Recheck for new stop or suspend requests which 275 * might appear while process lock was dropped in 276 * thread_single(). 277 */ 278 thread_suspend_check(0); 279 } 280 KASSERT(p->p_numthreads == 1, 281 ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 282 racct_sub(p, RACCT_NTHR, 1); 283 284 /* Let event handler change exit status */ 285 p->p_xexit = rval; 286 p->p_xsig = signo; 287 288 /* 289 * Ignore any pending request to stop due to a stop signal. 290 * Once P_WEXIT is set, future requests will be ignored as 291 * well. 292 */ 293 p->p_flag &= ~P_STOPPED_SIG; 294 KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 295 296 /* Note that we are exiting. */ 297 p->p_flag |= P_WEXIT; 298 299 /* 300 * Wait for any processes that have a hold on our vmspace to 301 * release their reference. 302 */ 303 while (p->p_lock > 0) 304 msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 305 306 PROC_UNLOCK(p); 307 /* Drain the limit callout while we don't have the proc locked */ 308 callout_drain(&p->p_limco); 309 310 #ifdef AUDIT 311 /* 312 * The Sun BSM exit token contains two components: an exit status as 313 * passed to exit(), and a return value to indicate what sort of exit 314 * it was. The exit status is WEXITSTATUS(rv), but it's not clear 315 * what the return value is. 316 */ 317 AUDIT_ARG_EXIT(rval, 0); 318 AUDIT_SYSCALL_EXIT(0, td); 319 #endif 320 321 /* Are we a task leader with peers? */ 322 if (p->p_peers != NULL && p == p->p_leader) { 323 mtx_lock(&ppeers_lock); 324 q = p->p_peers; 325 while (q != NULL) { 326 PROC_LOCK(q); 327 kern_psignal(q, SIGKILL); 328 PROC_UNLOCK(q); 329 q = q->p_peers; 330 } 331 while (p->p_peers != NULL) 332 msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 333 mtx_unlock(&ppeers_lock); 334 } 335 336 itimers_exit(p); 337 338 if (p->p_sysent->sv_onexit != NULL) 339 p->p_sysent->sv_onexit(p); 340 341 /* 342 * Check if any loadable modules need anything done at process exit. 343 * E.g. SYSV IPC stuff. 344 * Event handler could change exit status. 345 * XXX what if one of these generates an error? 346 */ 347 EVENTHANDLER_DIRECT_INVOKE(process_exit, p); 348 349 /* 350 * If parent is waiting for us to exit or exec, 351 * P_PPWAIT is set; we will wakeup the parent below. 352 */ 353 PROC_LOCK(p); 354 stopprofclock(p); 355 p->p_ptevents = 0; 356 357 /* 358 * Stop the real interval timer. If the handler is currently 359 * executing, prevent it from rearming itself and let it finish. 360 */ 361 if (timevalisset(&p->p_realtimer.it_value) && 362 _callout_stop_safe(&p->p_itcallout, CS_EXECUTING, NULL) == 0) { 363 timevalclear(&p->p_realtimer.it_interval); 364 msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 365 KASSERT(!timevalisset(&p->p_realtimer.it_value), 366 ("realtime timer is still armed")); 367 } 368 369 PROC_UNLOCK(p); 370 371 umtx_thread_exit(td); 372 seltdfini(td); 373 374 /* 375 * Reset any sigio structures pointing to us as a result of 376 * F_SETOWN with our pid. The P_WEXIT flag interlocks with fsetown(). 377 */ 378 funsetownlst(&p->p_sigiolst); 379 380 /* 381 * Close open files and release open-file table. 382 * This may block! 383 */ 384 pdescfree(td); 385 fdescfree(td); 386 387 /* 388 * If this thread tickled GEOM, we need to wait for the giggling to 389 * stop before we return to userland 390 */ 391 if (td->td_pflags & TDP_GEOM) 392 g_waitidle(); 393 394 /* 395 * Remove ourself from our leader's peer list and wake our leader. 396 */ 397 if (p->p_leader->p_peers != NULL) { 398 mtx_lock(&ppeers_lock); 399 if (p->p_leader->p_peers != NULL) { 400 q = p->p_leader; 401 while (q->p_peers != p) 402 q = q->p_peers; 403 q->p_peers = p->p_peers; 404 wakeup(p->p_leader); 405 } 406 mtx_unlock(&ppeers_lock); 407 } 408 409 vmspace_exit(td); 410 (void)acct_process(td); 411 412 #ifdef KTRACE 413 ktrprocexit(td); 414 #endif 415 /* 416 * Release reference to text vnode 417 */ 418 if (p->p_textvp != NULL) { 419 vrele(p->p_textvp); 420 p->p_textvp = NULL; 421 } 422 423 /* 424 * Release our limits structure. 425 */ 426 lim_free(p->p_limit); 427 p->p_limit = NULL; 428 429 tidhash_remove(td); 430 431 /* 432 * Call machine-dependent code to release any 433 * machine-dependent resources other than the address space. 434 * The address space is released by "vmspace_exitfree(p)" in 435 * vm_waitproc(). 436 */ 437 cpu_exit(td); 438 439 WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 440 441 /* 442 * Remove from allproc. It still sits in the hash. 443 */ 444 sx_xlock(&allproc_lock); 445 LIST_REMOVE(p, p_list); 446 447 #ifdef DDB 448 /* 449 * Used by ddb's 'ps' command to find this process via the 450 * pidhash. 451 */ 452 p->p_list.le_prev = NULL; 453 #endif 454 sx_xunlock(&allproc_lock); 455 456 sx_xlock(&proctree_lock); 457 PROC_LOCK(p); 458 p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 459 PROC_UNLOCK(p); 460 461 /* 462 * killjobc() might drop and re-acquire proctree_lock to 463 * revoke control tty if exiting process was a session leader. 464 */ 465 killjobc(); 466 467 /* 468 * Reparent all children processes: 469 * - traced ones to the original parent (or init if we are that parent) 470 * - the rest to init 471 */ 472 q = LIST_FIRST(&p->p_children); 473 if (q != NULL) /* only need this if any child is S_ZOMB */ 474 wakeup(q->p_reaper); 475 for (; q != NULL; q = nq) { 476 nq = LIST_NEXT(q, p_sibling); 477 ksi = ksiginfo_alloc(TRUE); 478 PROC_LOCK(q); 479 q->p_sigparent = SIGCHLD; 480 481 if ((q->p_flag & P_TRACED) == 0) { 482 proc_reparent(q, q->p_reaper, true); 483 if (q->p_state == PRS_ZOMBIE) { 484 /* 485 * Inform reaper about the reparented 486 * zombie, since wait(2) has something 487 * new to report. Guarantee queueing 488 * of the SIGCHLD signal, similar to 489 * the _exit() behaviour, by providing 490 * our ksiginfo. Ksi is freed by the 491 * signal delivery. 492 */ 493 if (q->p_ksi == NULL) { 494 ksi1 = NULL; 495 } else { 496 ksiginfo_copy(q->p_ksi, ksi); 497 ksi->ksi_flags |= KSI_INS; 498 ksi1 = ksi; 499 ksi = NULL; 500 } 501 PROC_LOCK(q->p_reaper); 502 pksignal(q->p_reaper, SIGCHLD, ksi1); 503 PROC_UNLOCK(q->p_reaper); 504 } else if (q->p_pdeathsig > 0) { 505 /* 506 * The child asked to received a signal 507 * when we exit. 508 */ 509 kern_psignal(q, q->p_pdeathsig); 510 } 511 } else { 512 /* 513 * Traced processes are killed by default 514 * since their existence means someone is 515 * screwing up. 516 */ 517 t = proc_realparent(q); 518 if (t == p) { 519 proc_reparent(q, q->p_reaper, true); 520 } else { 521 PROC_LOCK(t); 522 proc_reparent(q, t, true); 523 PROC_UNLOCK(t); 524 } 525 /* 526 * Since q was found on our children list, the 527 * proc_reparent() call moved q to the orphan 528 * list due to present P_TRACED flag. Clear 529 * orphan link for q now while q is locked. 530 */ 531 proc_clear_orphan(q); 532 q->p_flag &= ~P_TRACED; 533 q->p_flag2 &= ~P2_PTRACE_FSTP; 534 q->p_ptevents = 0; 535 p->p_xthread = NULL; 536 FOREACH_THREAD_IN_PROC(q, tdt) { 537 tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG | 538 TDB_FSTP); 539 tdt->td_xsig = 0; 540 } 541 if (kern_kill_on_dbg_exit) { 542 q->p_flag &= ~P_STOPPED_TRACE; 543 kern_psignal(q, SIGKILL); 544 } else if ((q->p_flag & (P_STOPPED_TRACE | 545 P_STOPPED_SIG)) != 0) { 546 sigqueue_delete_proc(q, SIGTRAP); 547 ptrace_unsuspend(q); 548 } 549 } 550 PROC_UNLOCK(q); 551 if (ksi != NULL) 552 ksiginfo_free(ksi); 553 } 554 555 /* 556 * Also get rid of our orphans. 557 */ 558 while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 559 PROC_LOCK(q); 560 KASSERT(q->p_oppid == p->p_pid, 561 ("orphan %p of %p has unexpected oppid %d", q, p, 562 q->p_oppid)); 563 q->p_oppid = q->p_reaper->p_pid; 564 565 /* 566 * If we are the real parent of this process 567 * but it has been reparented to a debugger, then 568 * check if it asked for a signal when we exit. 569 */ 570 if (q->p_pdeathsig > 0) 571 kern_psignal(q, q->p_pdeathsig); 572 CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid, 573 q->p_pid); 574 proc_clear_orphan(q); 575 PROC_UNLOCK(q); 576 } 577 578 #ifdef KDTRACE_HOOKS 579 if (SDT_PROBES_ENABLED()) { 580 int reason = CLD_EXITED; 581 if (WCOREDUMP(signo)) 582 reason = CLD_DUMPED; 583 else if (WIFSIGNALED(signo)) 584 reason = CLD_KILLED; 585 SDT_PROBE1(proc, , , exit, reason); 586 } 587 #endif 588 589 /* Save exit status. */ 590 PROC_LOCK(p); 591 p->p_xthread = td; 592 593 if (p->p_sysent->sv_ontdexit != NULL) 594 p->p_sysent->sv_ontdexit(td); 595 596 #ifdef KDTRACE_HOOKS 597 /* 598 * Tell the DTrace fasttrap provider about the exit if it 599 * has declared an interest. 600 */ 601 if (dtrace_fasttrap_exit) 602 dtrace_fasttrap_exit(p); 603 #endif 604 605 /* 606 * Notify interested parties of our demise. 607 */ 608 KNOTE_LOCKED(p->p_klist, NOTE_EXIT); 609 610 /* 611 * If this is a process with a descriptor, we may not need to deliver 612 * a signal to the parent. proctree_lock is held over 613 * procdesc_exit() to serialize concurrent calls to close() and 614 * exit(). 615 */ 616 signal_parent = 0; 617 if (p->p_procdesc == NULL || procdesc_exit(p)) { 618 /* 619 * Notify parent that we're gone. If parent has the 620 * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 621 * notify process 1 instead (and hope it will handle this 622 * situation). 623 */ 624 PROC_LOCK(p->p_pptr); 625 mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 626 if (p->p_pptr->p_sigacts->ps_flag & 627 (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 628 struct proc *pp; 629 630 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 631 pp = p->p_pptr; 632 PROC_UNLOCK(pp); 633 proc_reparent(p, p->p_reaper, true); 634 p->p_sigparent = SIGCHLD; 635 PROC_LOCK(p->p_pptr); 636 637 /* 638 * Notify parent, so in case he was wait(2)ing or 639 * executing waitpid(2) with our pid, he will 640 * continue. 641 */ 642 wakeup(pp); 643 } else 644 mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 645 646 if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) { 647 signal_parent = 1; 648 } else if (p->p_sigparent != 0) { 649 if (p->p_sigparent == SIGCHLD) { 650 signal_parent = 1; 651 } else { /* LINUX thread */ 652 signal_parent = 2; 653 } 654 } 655 } else 656 PROC_LOCK(p->p_pptr); 657 sx_xunlock(&proctree_lock); 658 659 if (signal_parent == 1) { 660 childproc_exited(p); 661 } else if (signal_parent == 2) { 662 kern_psignal(p->p_pptr, p->p_sigparent); 663 } 664 665 /* Tell the prison that we are gone. */ 666 prison_proc_free(p->p_ucred->cr_prison); 667 668 /* 669 * The state PRS_ZOMBIE prevents other proesses from sending 670 * signal to the process, to avoid memory leak, we free memory 671 * for signal queue at the time when the state is set. 672 */ 673 sigqueue_flush(&p->p_sigqueue); 674 sigqueue_flush(&td->td_sigqueue); 675 676 /* 677 * We have to wait until after acquiring all locks before 678 * changing p_state. We need to avoid all possible context 679 * switches (including ones from blocking on a mutex) while 680 * marked as a zombie. We also have to set the zombie state 681 * before we release the parent process' proc lock to avoid 682 * a lost wakeup. So, we first call wakeup, then we grab the 683 * sched lock, update the state, and release the parent process' 684 * proc lock. 685 */ 686 wakeup(p->p_pptr); 687 cv_broadcast(&p->p_pwait); 688 sched_exit(p->p_pptr, td); 689 PROC_SLOCK(p); 690 p->p_state = PRS_ZOMBIE; 691 PROC_UNLOCK(p->p_pptr); 692 693 /* 694 * Save our children's rusage information in our exit rusage. 695 */ 696 PROC_STATLOCK(p); 697 ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 698 PROC_STATUNLOCK(p); 699 700 /* 701 * Make sure the scheduler takes this thread out of its tables etc. 702 * This will also release this thread's reference to the ucred. 703 * Other thread parts to release include pcb bits and such. 704 */ 705 thread_exit(); 706 } 707 708 #ifndef _SYS_SYSPROTO_H_ 709 struct abort2_args { 710 char *why; 711 int nargs; 712 void **args; 713 }; 714 #endif 715 716 int 717 sys_abort2(struct thread *td, struct abort2_args *uap) 718 { 719 struct proc *p = td->td_proc; 720 struct sbuf *sb; 721 void *uargs[16]; 722 int error, i, sig; 723 724 /* 725 * Do it right now so we can log either proper call of abort2(), or 726 * note, that invalid argument was passed. 512 is big enough to 727 * handle 16 arguments' descriptions with additional comments. 728 */ 729 sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 730 sbuf_clear(sb); 731 sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 732 p->p_comm, p->p_pid, td->td_ucred->cr_uid); 733 /* 734 * Since we can't return from abort2(), send SIGKILL in cases, where 735 * abort2() was called improperly 736 */ 737 sig = SIGKILL; 738 /* Prevent from DoSes from user-space. */ 739 if (uap->nargs < 0 || uap->nargs > 16) 740 goto out; 741 if (uap->nargs > 0) { 742 if (uap->args == NULL) 743 goto out; 744 error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 745 if (error != 0) 746 goto out; 747 } 748 /* 749 * Limit size of 'reason' string to 128. Will fit even when 750 * maximal number of arguments was chosen to be logged. 751 */ 752 if (uap->why != NULL) { 753 error = sbuf_copyin(sb, uap->why, 128); 754 if (error < 0) 755 goto out; 756 } else { 757 sbuf_printf(sb, "(null)"); 758 } 759 if (uap->nargs > 0) { 760 sbuf_printf(sb, "("); 761 for (i = 0;i < uap->nargs; i++) 762 sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 763 sbuf_printf(sb, ")"); 764 } 765 /* 766 * Final stage: arguments were proper, string has been 767 * successfully copied from userspace, and copying pointers 768 * from user-space succeed. 769 */ 770 sig = SIGABRT; 771 out: 772 if (sig == SIGKILL) { 773 sbuf_trim(sb); 774 sbuf_printf(sb, " (Reason text inaccessible)"); 775 } 776 sbuf_cat(sb, "\n"); 777 sbuf_finish(sb); 778 log(LOG_INFO, "%s", sbuf_data(sb)); 779 sbuf_delete(sb); 780 exit1(td, 0, sig); 781 return (0); 782 } 783 784 #ifdef COMPAT_43 785 /* 786 * The dirty work is handled by kern_wait(). 787 */ 788 int 789 owait(struct thread *td, struct owait_args *uap __unused) 790 { 791 int error, status; 792 793 error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 794 if (error == 0) 795 td->td_retval[1] = status; 796 return (error); 797 } 798 #endif /* COMPAT_43 */ 799 800 /* 801 * The dirty work is handled by kern_wait(). 802 */ 803 int 804 sys_wait4(struct thread *td, struct wait4_args *uap) 805 { 806 struct rusage ru, *rup; 807 int error, status; 808 809 if (uap->rusage != NULL) 810 rup = &ru; 811 else 812 rup = NULL; 813 error = kern_wait(td, uap->pid, &status, uap->options, rup); 814 if (uap->status != NULL && error == 0 && td->td_retval[0] != 0) 815 error = copyout(&status, uap->status, sizeof(status)); 816 if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0) 817 error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 818 return (error); 819 } 820 821 int 822 sys_wait6(struct thread *td, struct wait6_args *uap) 823 { 824 struct __wrusage wru, *wrup; 825 siginfo_t si, *sip; 826 idtype_t idtype; 827 id_t id; 828 int error, status; 829 830 idtype = uap->idtype; 831 id = uap->id; 832 833 if (uap->wrusage != NULL) 834 wrup = &wru; 835 else 836 wrup = NULL; 837 838 if (uap->info != NULL) { 839 sip = &si; 840 bzero(sip, sizeof(*sip)); 841 } else 842 sip = NULL; 843 844 /* 845 * We expect all callers of wait6() to know about WEXITED and 846 * WTRAPPED. 847 */ 848 error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip); 849 850 if (uap->status != NULL && error == 0 && td->td_retval[0] != 0) 851 error = copyout(&status, uap->status, sizeof(status)); 852 if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0) 853 error = copyout(&wru, uap->wrusage, sizeof(wru)); 854 if (uap->info != NULL && error == 0) 855 error = copyout(&si, uap->info, sizeof(si)); 856 return (error); 857 } 858 859 /* 860 * Reap the remains of a zombie process and optionally return status and 861 * rusage. Asserts and will release both the proctree_lock and the process 862 * lock as part of its work. 863 */ 864 void 865 proc_reap(struct thread *td, struct proc *p, int *status, int options) 866 { 867 struct proc *q, *t; 868 869 sx_assert(&proctree_lock, SA_XLOCKED); 870 PROC_LOCK_ASSERT(p, MA_OWNED); 871 KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 872 873 mtx_spin_wait_unlocked(&p->p_slock); 874 875 q = td->td_proc; 876 877 if (status) 878 *status = KW_EXITCODE(p->p_xexit, p->p_xsig); 879 if (options & WNOWAIT) { 880 /* 881 * Only poll, returning the status. Caller does not wish to 882 * release the proc struct just yet. 883 */ 884 PROC_UNLOCK(p); 885 sx_xunlock(&proctree_lock); 886 return; 887 } 888 889 PROC_LOCK(q); 890 sigqueue_take(p->p_ksi); 891 PROC_UNLOCK(q); 892 893 /* 894 * If we got the child via a ptrace 'attach', we need to give it back 895 * to the old parent. 896 */ 897 if (p->p_oppid != p->p_pptr->p_pid) { 898 PROC_UNLOCK(p); 899 t = proc_realparent(p); 900 PROC_LOCK(t); 901 PROC_LOCK(p); 902 CTR2(KTR_PTRACE, 903 "wait: traced child %d moved back to parent %d", p->p_pid, 904 t->p_pid); 905 proc_reparent(p, t, false); 906 PROC_UNLOCK(p); 907 pksignal(t, SIGCHLD, p->p_ksi); 908 wakeup(t); 909 cv_broadcast(&p->p_pwait); 910 PROC_UNLOCK(t); 911 sx_xunlock(&proctree_lock); 912 return; 913 } 914 PROC_UNLOCK(p); 915 916 /* 917 * Remove other references to this process to ensure we have an 918 * exclusive reference. 919 */ 920 sx_xlock(PIDHASHLOCK(p->p_pid)); 921 LIST_REMOVE(p, p_hash); 922 sx_xunlock(PIDHASHLOCK(p->p_pid)); 923 LIST_REMOVE(p, p_sibling); 924 reaper_abandon_children(p, true); 925 reaper_clear(p); 926 PROC_LOCK(p); 927 proc_clear_orphan(p); 928 PROC_UNLOCK(p); 929 leavepgrp(p); 930 if (p->p_procdesc != NULL) 931 procdesc_reap(p); 932 sx_xunlock(&proctree_lock); 933 934 proc_id_clear(PROC_ID_PID, p->p_pid); 935 936 PROC_LOCK(p); 937 knlist_detach(p->p_klist); 938 p->p_klist = NULL; 939 PROC_UNLOCK(p); 940 941 /* 942 * Removal from allproc list and process group list paired with 943 * PROC_LOCK which was executed during that time should guarantee 944 * nothing can reach this process anymore. As such further locking 945 * is unnecessary. 946 */ 947 p->p_xexit = p->p_xsig = 0; /* XXX: why? */ 948 949 PROC_LOCK(q); 950 ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 951 PROC_UNLOCK(q); 952 953 /* 954 * Decrement the count of procs running with this uid. 955 */ 956 (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 957 958 /* 959 * Destroy resource accounting information associated with the process. 960 */ 961 #ifdef RACCT 962 if (racct_enable) { 963 PROC_LOCK(p); 964 racct_sub(p, RACCT_NPROC, 1); 965 PROC_UNLOCK(p); 966 } 967 #endif 968 racct_proc_exit(p); 969 970 /* 971 * Free credentials, arguments, and sigacts. 972 */ 973 proc_unset_cred(p); 974 pargs_drop(p->p_args); 975 p->p_args = NULL; 976 sigacts_free(p->p_sigacts); 977 p->p_sigacts = NULL; 978 979 /* 980 * Do any thread-system specific cleanups. 981 */ 982 thread_wait(p); 983 984 /* 985 * Give vm and machine-dependent layer a chance to free anything that 986 * cpu_exit couldn't release while still running in process context. 987 */ 988 vm_waitproc(p); 989 #ifdef MAC 990 mac_proc_destroy(p); 991 #endif 992 993 KASSERT(FIRST_THREAD_IN_PROC(p), 994 ("proc_reap: no residual thread!")); 995 uma_zfree(proc_zone, p); 996 atomic_add_int(&nprocs, -1); 997 } 998 999 static int 1000 proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, 1001 int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo, 1002 int check_only) 1003 { 1004 struct rusage *rup; 1005 1006 sx_assert(&proctree_lock, SA_XLOCKED); 1007 1008 PROC_LOCK(p); 1009 1010 switch (idtype) { 1011 case P_ALL: 1012 if (p->p_procdesc == NULL || 1013 (p->p_pptr == td->td_proc && 1014 (p->p_flag & P_TRACED) != 0)) { 1015 break; 1016 } 1017 1018 PROC_UNLOCK(p); 1019 return (0); 1020 case P_PID: 1021 if (p->p_pid != (pid_t)id) { 1022 PROC_UNLOCK(p); 1023 return (0); 1024 } 1025 break; 1026 case P_PGID: 1027 if (p->p_pgid != (pid_t)id) { 1028 PROC_UNLOCK(p); 1029 return (0); 1030 } 1031 break; 1032 case P_SID: 1033 if (p->p_session->s_sid != (pid_t)id) { 1034 PROC_UNLOCK(p); 1035 return (0); 1036 } 1037 break; 1038 case P_UID: 1039 if (p->p_ucred->cr_uid != (uid_t)id) { 1040 PROC_UNLOCK(p); 1041 return (0); 1042 } 1043 break; 1044 case P_GID: 1045 if (p->p_ucred->cr_gid != (gid_t)id) { 1046 PROC_UNLOCK(p); 1047 return (0); 1048 } 1049 break; 1050 case P_JAILID: 1051 if (p->p_ucred->cr_prison->pr_id != (int)id) { 1052 PROC_UNLOCK(p); 1053 return (0); 1054 } 1055 break; 1056 /* 1057 * It seems that the thread structures get zeroed out 1058 * at process exit. This makes it impossible to 1059 * support P_SETID, P_CID or P_CPUID. 1060 */ 1061 default: 1062 PROC_UNLOCK(p); 1063 return (0); 1064 } 1065 1066 if (p_canwait(td, p)) { 1067 PROC_UNLOCK(p); 1068 return (0); 1069 } 1070 1071 if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) { 1072 PROC_UNLOCK(p); 1073 return (0); 1074 } 1075 1076 /* 1077 * This special case handles a kthread spawned by linux_clone 1078 * (see linux_misc.c). The linux_wait4 and linux_waitpid 1079 * functions need to be able to distinguish between waiting 1080 * on a process and waiting on a thread. It is a thread if 1081 * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 1082 * signifies we want to wait for threads and not processes. 1083 */ 1084 if ((p->p_sigparent != SIGCHLD) ^ 1085 ((options & WLINUXCLONE) != 0)) { 1086 PROC_UNLOCK(p); 1087 return (0); 1088 } 1089 1090 if (siginfo != NULL) { 1091 bzero(siginfo, sizeof(*siginfo)); 1092 siginfo->si_errno = 0; 1093 1094 /* 1095 * SUSv4 requires that the si_signo value is always 1096 * SIGCHLD. Obey it despite the rfork(2) interface 1097 * allows to request other signal for child exit 1098 * notification. 1099 */ 1100 siginfo->si_signo = SIGCHLD; 1101 1102 /* 1103 * This is still a rough estimate. We will fix the 1104 * cases TRAPPED, STOPPED, and CONTINUED later. 1105 */ 1106 if (WCOREDUMP(p->p_xsig)) { 1107 siginfo->si_code = CLD_DUMPED; 1108 siginfo->si_status = WTERMSIG(p->p_xsig); 1109 } else if (WIFSIGNALED(p->p_xsig)) { 1110 siginfo->si_code = CLD_KILLED; 1111 siginfo->si_status = WTERMSIG(p->p_xsig); 1112 } else { 1113 siginfo->si_code = CLD_EXITED; 1114 siginfo->si_status = p->p_xexit; 1115 } 1116 1117 siginfo->si_pid = p->p_pid; 1118 siginfo->si_uid = p->p_ucred->cr_uid; 1119 1120 /* 1121 * The si_addr field would be useful additional 1122 * detail, but apparently the PC value may be lost 1123 * when we reach this point. bzero() above sets 1124 * siginfo->si_addr to NULL. 1125 */ 1126 } 1127 1128 /* 1129 * There should be no reason to limit resources usage info to 1130 * exited processes only. A snapshot about any resources used 1131 * by a stopped process may be exactly what is needed. 1132 */ 1133 if (wrusage != NULL) { 1134 rup = &wrusage->wru_self; 1135 *rup = p->p_ru; 1136 PROC_STATLOCK(p); 1137 calcru(p, &rup->ru_utime, &rup->ru_stime); 1138 PROC_STATUNLOCK(p); 1139 1140 rup = &wrusage->wru_children; 1141 *rup = p->p_stats->p_cru; 1142 calccru(p, &rup->ru_utime, &rup->ru_stime); 1143 } 1144 1145 if (p->p_state == PRS_ZOMBIE && !check_only) { 1146 proc_reap(td, p, status, options); 1147 return (-1); 1148 } 1149 return (1); 1150 } 1151 1152 int 1153 kern_wait(struct thread *td, pid_t pid, int *status, int options, 1154 struct rusage *rusage) 1155 { 1156 struct __wrusage wru, *wrup; 1157 idtype_t idtype; 1158 id_t id; 1159 int ret; 1160 1161 /* 1162 * Translate the special pid values into the (idtype, pid) 1163 * pair for kern_wait6. The WAIT_MYPGRP case is handled by 1164 * kern_wait6() on its own. 1165 */ 1166 if (pid == WAIT_ANY) { 1167 idtype = P_ALL; 1168 id = 0; 1169 } else if (pid < 0) { 1170 idtype = P_PGID; 1171 id = (id_t)-pid; 1172 } else { 1173 idtype = P_PID; 1174 id = (id_t)pid; 1175 } 1176 1177 if (rusage != NULL) 1178 wrup = &wru; 1179 else 1180 wrup = NULL; 1181 1182 /* 1183 * For backward compatibility we implicitly add flags WEXITED 1184 * and WTRAPPED here. 1185 */ 1186 options |= WEXITED | WTRAPPED; 1187 ret = kern_wait6(td, idtype, id, status, options, wrup, NULL); 1188 if (rusage != NULL) 1189 *rusage = wru.wru_self; 1190 return (ret); 1191 } 1192 1193 static void 1194 report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo, 1195 int *status, int options, int si_code) 1196 { 1197 bool cont; 1198 1199 PROC_LOCK_ASSERT(p, MA_OWNED); 1200 sx_assert(&proctree_lock, SA_XLOCKED); 1201 MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED || 1202 si_code == CLD_CONTINUED); 1203 1204 cont = si_code == CLD_CONTINUED; 1205 if ((options & WNOWAIT) == 0) { 1206 if (cont) 1207 p->p_flag &= ~P_CONTINUED; 1208 else 1209 p->p_flag |= P_WAITED; 1210 PROC_LOCK(td->td_proc); 1211 sigqueue_take(p->p_ksi); 1212 PROC_UNLOCK(td->td_proc); 1213 } 1214 sx_xunlock(&proctree_lock); 1215 if (siginfo != NULL) { 1216 siginfo->si_code = si_code; 1217 siginfo->si_status = cont ? SIGCONT : p->p_xsig; 1218 } 1219 if (status != NULL) 1220 *status = cont ? SIGCONT : W_STOPCODE(p->p_xsig); 1221 PROC_UNLOCK(p); 1222 td->td_retval[0] = p->p_pid; 1223 } 1224 1225 int 1226 kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, 1227 int options, struct __wrusage *wrusage, siginfo_t *siginfo) 1228 { 1229 struct proc *p, *q; 1230 pid_t pid; 1231 int error, nfound, ret; 1232 bool report; 1233 1234 AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */ 1235 AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */ 1236 AUDIT_ARG_VALUE(options); 1237 1238 q = td->td_proc; 1239 1240 if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1241 PROC_LOCK(q); 1242 id = (id_t)q->p_pgid; 1243 PROC_UNLOCK(q); 1244 idtype = P_PGID; 1245 } 1246 1247 /* If we don't know the option, just return. */ 1248 if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT | 1249 WEXITED | WTRAPPED | WLINUXCLONE)) != 0) 1250 return (EINVAL); 1251 if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) { 1252 /* 1253 * We will be unable to find any matching processes, 1254 * because there are no known events to look for. 1255 * Prefer to return error instead of blocking 1256 * indefinitely. 1257 */ 1258 return (EINVAL); 1259 } 1260 1261 loop: 1262 if (q->p_flag & P_STATCHILD) { 1263 PROC_LOCK(q); 1264 q->p_flag &= ~P_STATCHILD; 1265 PROC_UNLOCK(q); 1266 } 1267 sx_xlock(&proctree_lock); 1268 loop_locked: 1269 nfound = 0; 1270 LIST_FOREACH(p, &q->p_children, p_sibling) { 1271 pid = p->p_pid; 1272 ret = proc_to_reap(td, p, idtype, id, status, options, 1273 wrusage, siginfo, 0); 1274 if (ret == 0) 1275 continue; 1276 else if (ret != 1) { 1277 td->td_retval[0] = pid; 1278 return (0); 1279 } 1280 1281 nfound++; 1282 PROC_LOCK_ASSERT(p, MA_OWNED); 1283 1284 if ((options & WTRAPPED) != 0 && 1285 (p->p_flag & P_TRACED) != 0) { 1286 PROC_SLOCK(p); 1287 report = 1288 ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) && 1289 p->p_suspcount == p->p_numthreads && 1290 (p->p_flag & P_WAITED) == 0); 1291 PROC_SUNLOCK(p); 1292 if (report) { 1293 CTR4(KTR_PTRACE, 1294 "wait: returning trapped pid %d status %#x " 1295 "(xstat %d) xthread %d", 1296 p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig, 1297 p->p_xthread != NULL ? 1298 p->p_xthread->td_tid : -1); 1299 report_alive_proc(td, p, siginfo, status, 1300 options, CLD_TRAPPED); 1301 return (0); 1302 } 1303 } 1304 if ((options & WUNTRACED) != 0 && 1305 (p->p_flag & P_STOPPED_SIG) != 0) { 1306 PROC_SLOCK(p); 1307 report = (p->p_suspcount == p->p_numthreads && 1308 ((p->p_flag & P_WAITED) == 0)); 1309 PROC_SUNLOCK(p); 1310 if (report) { 1311 report_alive_proc(td, p, siginfo, status, 1312 options, CLD_STOPPED); 1313 return (0); 1314 } 1315 } 1316 if ((options & WCONTINUED) != 0 && 1317 (p->p_flag & P_CONTINUED) != 0) { 1318 report_alive_proc(td, p, siginfo, status, options, 1319 CLD_CONTINUED); 1320 return (0); 1321 } 1322 PROC_UNLOCK(p); 1323 } 1324 1325 /* 1326 * Look in the orphans list too, to allow the parent to 1327 * collect it's child exit status even if child is being 1328 * debugged. 1329 * 1330 * Debugger detaches from the parent upon successful 1331 * switch-over from parent to child. At this point due to 1332 * re-parenting the parent loses the child to debugger and a 1333 * wait4(2) call would report that it has no children to wait 1334 * for. By maintaining a list of orphans we allow the parent 1335 * to successfully wait until the child becomes a zombie. 1336 */ 1337 if (nfound == 0) { 1338 LIST_FOREACH(p, &q->p_orphans, p_orphan) { 1339 ret = proc_to_reap(td, p, idtype, id, NULL, options, 1340 NULL, NULL, 1); 1341 if (ret != 0) { 1342 KASSERT(ret != -1, ("reaped an orphan (pid %d)", 1343 (int)td->td_retval[0])); 1344 PROC_UNLOCK(p); 1345 nfound++; 1346 break; 1347 } 1348 } 1349 } 1350 if (nfound == 0) { 1351 sx_xunlock(&proctree_lock); 1352 return (ECHILD); 1353 } 1354 if (options & WNOHANG) { 1355 sx_xunlock(&proctree_lock); 1356 td->td_retval[0] = 0; 1357 return (0); 1358 } 1359 PROC_LOCK(q); 1360 if (q->p_flag & P_STATCHILD) { 1361 q->p_flag &= ~P_STATCHILD; 1362 PROC_UNLOCK(q); 1363 goto loop_locked; 1364 } 1365 sx_xunlock(&proctree_lock); 1366 error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0); 1367 if (error) 1368 return (error); 1369 goto loop; 1370 } 1371 1372 void 1373 proc_add_orphan(struct proc *child, struct proc *parent) 1374 { 1375 1376 sx_assert(&proctree_lock, SX_XLOCKED); 1377 KASSERT((child->p_flag & P_TRACED) != 0, 1378 ("proc_add_orphan: not traced")); 1379 1380 if (LIST_EMPTY(&parent->p_orphans)) { 1381 child->p_treeflag |= P_TREE_FIRST_ORPHAN; 1382 LIST_INSERT_HEAD(&parent->p_orphans, child, p_orphan); 1383 } else { 1384 LIST_INSERT_AFTER(LIST_FIRST(&parent->p_orphans), 1385 child, p_orphan); 1386 } 1387 child->p_treeflag |= P_TREE_ORPHANED; 1388 } 1389 1390 /* 1391 * Make process 'parent' the new parent of process 'child'. 1392 * Must be called with an exclusive hold of proctree lock. 1393 */ 1394 void 1395 proc_reparent(struct proc *child, struct proc *parent, bool set_oppid) 1396 { 1397 1398 sx_assert(&proctree_lock, SX_XLOCKED); 1399 PROC_LOCK_ASSERT(child, MA_OWNED); 1400 if (child->p_pptr == parent) 1401 return; 1402 1403 PROC_LOCK(child->p_pptr); 1404 sigqueue_take(child->p_ksi); 1405 PROC_UNLOCK(child->p_pptr); 1406 LIST_REMOVE(child, p_sibling); 1407 LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1408 1409 proc_clear_orphan(child); 1410 if ((child->p_flag & P_TRACED) != 0) { 1411 proc_add_orphan(child, child->p_pptr); 1412 } 1413 1414 child->p_pptr = parent; 1415 if (set_oppid) 1416 child->p_oppid = parent->p_pid; 1417 } 1418