19454b2d8SWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 7df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 8df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 9df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 2069a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 39677b542eSDavid E. O'Brien #include <sys/cdefs.h> 40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 41677b542eSDavid E. O'Brien 425591b823SEivind Eklund #include "opt_compat.h" 43db6a20e2SGarrett Wollman #include "opt_ktrace.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 475fdb8324SBruce Evans #include <sys/sysproto.h> 484a144410SRobert Watson #include <sys/capsicum.h> 4975b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 501c5bb3eaSPeter Wemm #include <sys/kernel.h> 51a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 52f34fa851SJohn Baldwin #include <sys/lock.h> 5335e0e5b3SJohn Baldwin #include <sys/mutex.h> 54df8bae1dSRodney W. Grimes #include <sys/proc.h> 55cfb5f768SJonathan Anderson #include <sys/procdesc.h> 562a024a2bSSean Eric Fagan #include <sys/pioctl.h> 57413628a7SBjoern A. Zeeb #include <sys/jail.h> 58df8bae1dSRodney W. Grimes #include <sys/tty.h> 59df8bae1dSRodney W. Grimes #include <sys/wait.h> 60eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 617a6b989bSJohn Baldwin #include <sys/vnode.h> 62097055e2SEdward Tomasz Napierala #include <sys/racct.h> 63df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 6425f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 65797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 66b43179fbSJeff Roberson #include <sys/sched.h> 671005a129SJohn Baldwin #include <sys/sx.h> 68c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6925f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 70df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 717c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 72797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 735d217f17SJohn Birrell #include <sys/sdt.h> 74780dc5a8SPeter Wemm #include <sys/shm.h> 75780dc5a8SPeter Wemm #include <sys/sem.h> 7613dad108SKonstantin Belousov #include <sys/umtx.h> 776c84de02SJohn Baldwin #ifdef KTRACE 786c84de02SJohn Baldwin #include <sys/ktrace.h> 796c84de02SJohn Baldwin #endif 80780dc5a8SPeter Wemm 81fcf7f27aSRobert Watson #include <security/audit/audit.h> 82aed55708SRobert Watson #include <security/mac/mac_framework.h> 83fcf7f27aSRobert Watson 84df8bae1dSRodney W. Grimes #include <vm/vm.h> 85eb30c1c0SPeter Wemm #include <vm/vm_extern.h> 867a6b989bSJohn Baldwin #include <vm/vm_param.h> 87efeaf95aSDavid Greenman #include <vm/pmap.h> 88efeaf95aSDavid Greenman #include <vm/vm_map.h> 892d21129dSAlan Cox #include <vm/vm_page.h> 90c897b813SJeff Roberson #include <vm/uma.h> 916520495aSAdrian Chadd #include <vm/vm_domain.h> 92df8bae1dSRodney W. Grimes 935d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 945d217f17SJohn Birrell #include <sys/dtrace_bsd.h> 955d217f17SJohn Birrell dtrace_execexit_func_t dtrace_fasttrap_exit; 965d217f17SJohn Birrell #endif 975d217f17SJohn Birrell 985d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 9936160958SMark Johnston SDT_PROBE_DEFINE1(proc, , , exit, "int"); 1005d217f17SJohn Birrell 101c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 102c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 103c0bc2867SGleb Smirnoff 1042ca45184SMatt Joras EVENTHANDLER_LIST_DECLARE(process_exit); 1052ca45184SMatt Joras 106d7359980SKonstantin Belousov struct proc * 107d7359980SKonstantin Belousov proc_realparent(struct proc *child) 108d7359980SKonstantin Belousov { 109d7359980SKonstantin Belousov struct proc *p, *parent; 110d7359980SKonstantin Belousov 111d7359980SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED); 112d7359980SKonstantin Belousov if ((child->p_treeflag & P_TREE_ORPHANED) == 0) { 1139152087eSMateusz Guzik if (child->p_oppid == 0 || 1149152087eSMateusz Guzik child->p_pptr->p_pid == child->p_oppid) 1159152087eSMateusz Guzik parent = child->p_pptr; 1169152087eSMateusz Guzik else 1179152087eSMateusz Guzik parent = initproc; 1189152087eSMateusz Guzik return (parent); 119d7359980SKonstantin Belousov } 120d7359980SKonstantin Belousov for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) { 121d7359980SKonstantin Belousov /* Cannot use LIST_PREV(), since the list head is not known. */ 122d7359980SKonstantin Belousov p = __containerof(p->p_orphan.le_prev, struct proc, 123d7359980SKonstantin Belousov p_orphan.le_next); 124d7359980SKonstantin Belousov KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0, 125d7359980SKonstantin Belousov ("missing P_ORPHAN %p", p)); 126d7359980SKonstantin Belousov } 127d7359980SKonstantin Belousov parent = __containerof(p->p_orphan.le_prev, struct proc, 128d7359980SKonstantin Belousov p_orphans.lh_first); 129d7359980SKonstantin Belousov return (parent); 130d7359980SKonstantin Belousov } 131d7359980SKonstantin Belousov 132237623b0SKonstantin Belousov void 133237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting) 134237623b0SKonstantin Belousov { 135237623b0SKonstantin Belousov struct proc *p1, *p2, *ptmp; 136237623b0SKonstantin Belousov 137237623b0SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED); 138237623b0SKonstantin Belousov KASSERT(p != initproc, ("reaper_abandon_children for initproc")); 139237623b0SKonstantin Belousov if ((p->p_treeflag & P_TREE_REAPER) == 0) 140237623b0SKonstantin Belousov return; 141237623b0SKonstantin Belousov p1 = p->p_reaper; 142237623b0SKonstantin Belousov LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) { 143237623b0SKonstantin Belousov LIST_REMOVE(p2, p_reapsibling); 144237623b0SKonstantin Belousov p2->p_reaper = p1; 145237623b0SKonstantin Belousov p2->p_reapsubtree = p->p_reapsubtree; 146237623b0SKonstantin Belousov LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling); 147237623b0SKonstantin Belousov if (exiting && p2->p_pptr == p) { 148237623b0SKonstantin Belousov PROC_LOCK(p2); 149237623b0SKonstantin Belousov proc_reparent(p2, p1); 150237623b0SKonstantin Belousov PROC_UNLOCK(p2); 151237623b0SKonstantin Belousov } 152237623b0SKonstantin Belousov } 153237623b0SKonstantin Belousov KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty")); 154237623b0SKonstantin Belousov p->p_treeflag &= ~P_TREE_REAPER; 155237623b0SKonstantin Belousov } 156237623b0SKonstantin Belousov 1572e39e24fSKonstantin Belousov static void 1582e39e24fSKonstantin Belousov clear_orphan(struct proc *p) 1592e39e24fSKonstantin Belousov { 160d7359980SKonstantin Belousov struct proc *p1; 1612e39e24fSKonstantin Belousov 162d7359980SKonstantin Belousov sx_assert(&proctree_lock, SA_XLOCKED); 163d7359980SKonstantin Belousov if ((p->p_treeflag & P_TREE_ORPHANED) == 0) 164d7359980SKonstantin Belousov return; 165d7359980SKonstantin Belousov if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) { 166d7359980SKonstantin Belousov p1 = LIST_NEXT(p, p_orphan); 167d7359980SKonstantin Belousov if (p1 != NULL) 168d7359980SKonstantin Belousov p1->p_treeflag |= P_TREE_FIRST_ORPHAN; 169d7359980SKonstantin Belousov p->p_treeflag &= ~P_TREE_FIRST_ORPHAN; 1702e39e24fSKonstantin Belousov } 171d7359980SKonstantin Belousov LIST_REMOVE(p, p_orphan); 172d7359980SKonstantin Belousov p->p_treeflag &= ~P_TREE_ORPHANED; 1732e39e24fSKonstantin Belousov } 1742e39e24fSKonstantin Belousov 175df8bae1dSRodney W. Grimes /* 176873fbcd7SRobert Watson * exit -- death of process. 177df8bae1dSRodney W. Grimes */ 178fc0b1dbfSBruce Evans void 1798451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 180df8bae1dSRodney W. Grimes { 181b40ce416SJulian Elischer 182b4490c6eSKonstantin Belousov exit1(td, uap->rval, 0); 183df8bae1dSRodney W. Grimes /* NOTREACHED */ 184df8bae1dSRodney W. Grimes } 185df8bae1dSRodney W. Grimes 186df8bae1dSRodney W. Grimes /* 187873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 188873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 189873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 190df8bae1dSRodney W. Grimes */ 191fc0b1dbfSBruce Evans void 192b4490c6eSKonstantin Belousov exit1(struct thread *td, int rval, int signo) 193df8bae1dSRodney W. Grimes { 194a661bebeSMateusz Guzik struct proc *p, *nq, *q, *t; 195a661bebeSMateusz Guzik struct thread *tdt; 1969a2dde80SKonstantin Belousov ksiginfo_t *ksi, *ksi1; 197df8bae1dSRodney W. Grimes 198e17660e7SKris Kennaway mtx_assert(&Giant, MA_NOTOWNED); 199b4490c6eSKonstantin Belousov KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo)); 2000cddd8f0SMatthew Dillon 201276c5169SJohn Baldwin p = td->td_proc; 2025486ffc8SMarius Strobl /* 2035486ffc8SMarius Strobl * XXX in case we're rebooting we just let init die in order to 2045486ffc8SMarius Strobl * work around an unsolved stack overflow seen very late during 2055486ffc8SMarius Strobl * shutdown on sparc64 when the gmirror worker process exists. 2065486ffc8SMarius Strobl */ 2075486ffc8SMarius Strobl if (p == initproc && rebooting == 0) { 208b4490c6eSKonstantin Belousov printf("init died (signal %d, exit %d)\n", signo, rval); 2095f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 2105f7bd355SPoul-Henning Kamp } 2112c1011f7SJohn Dyson 2127a6b989bSJohn Baldwin /* 2131bc93bb7SKonstantin Belousov * Deref SU mp, since the thread does not return to userspace. 2141bc93bb7SKonstantin Belousov */ 215aca4bb91SKonstantin Belousov td_softdep_cleanup(td); 2161bc93bb7SKonstantin Belousov 2171bc93bb7SKonstantin Belousov /* 2182c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 2197a6b989bSJohn Baldwin */ 220e602ba25SJulian Elischer PROC_LOCK(p); 221e602ba25SJulian Elischer /* 222aba1ca52SKonstantin Belousov * First check if some other thread or external request got 223aba1ca52SKonstantin Belousov * here before us. If so, act appropriately: exit or suspend. 224aba1ca52SKonstantin Belousov * We must ensure that stop requests are handled before we set 225aba1ca52SKonstantin Belousov * P_WEXIT. 226e602ba25SJulian Elischer */ 227e602ba25SJulian Elischer thread_suspend_check(0); 228aba1ca52SKonstantin Belousov while (p->p_flag & P_HADTHREADS) { 229e602ba25SJulian Elischer /* 230e602ba25SJulian Elischer * Kill off the other threads. This requires 2316111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 2326111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 2336111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 234e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 2356111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 2366111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 2376111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 238b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 2396111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 2406111dcd2SJohn Baldwin * other threads exits. 241b370279eSDavid Xu * If there is already a thread singler after resumption, 2426111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 243b370279eSDavid Xu * re-check all suspension request, the thread should 244b370279eSDavid Xu * either be suspended there or exit. 245e602ba25SJulian Elischer */ 2466ddcc233SKonstantin Belousov if (!thread_single(p, SINGLE_EXIT)) 247e602ba25SJulian Elischer /* 248aba1ca52SKonstantin Belousov * All other activity in this process is now 249aba1ca52SKonstantin Belousov * stopped. Threading support has been turned 250aba1ca52SKonstantin Belousov * off. 251e602ba25SJulian Elischer */ 252aba1ca52SKonstantin Belousov break; 253aba1ca52SKonstantin Belousov /* 254aba1ca52SKonstantin Belousov * Recheck for new stop or suspend requests which 255aba1ca52SKonstantin Belousov * might appear while process lock was dropped in 256aba1ca52SKonstantin Belousov * thread_single(). 257aba1ca52SKonstantin Belousov */ 258aba1ca52SKonstantin Belousov thread_suspend_check(0); 259e602ba25SJulian Elischer } 2601c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 2611c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 26258c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 263b4490c6eSKonstantin Belousov 264b4490c6eSKonstantin Belousov /* Let event handler change exit status */ 265b4490c6eSKonstantin Belousov p->p_xexit = rval; 266b4490c6eSKonstantin Belousov p->p_xsig = signo; 267b4490c6eSKonstantin Belousov 26806ad42b2SJohn Baldwin /* 26906ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 27006ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 27106ad42b2SJohn Baldwin * released their reference to us. Note that if they have 27206ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 27306ad42b2SJohn Baldwin * via PIOCCONT. 27406ad42b2SJohn Baldwin */ 275b4490c6eSKonstantin Belousov _STOPEVENT(p, S_EXIT, 0); 27606ad42b2SJohn Baldwin 27706ad42b2SJohn Baldwin /* 2780f14f15bSJohn Baldwin * Ignore any pending request to stop due to a stop signal. 2790f14f15bSJohn Baldwin * Once P_WEXIT is set, future requests will be ignored as 2800f14f15bSJohn Baldwin * well. 2810f14f15bSJohn Baldwin */ 2820f14f15bSJohn Baldwin p->p_flag &= ~P_STOPPED_SIG; 2830f14f15bSJohn Baldwin KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 2840f14f15bSJohn Baldwin 2850f14f15bSJohn Baldwin /* 28606ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 28706ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 28806ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 28906ad42b2SJohn Baldwin */ 290e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 29106ad42b2SJohn Baldwin wakeup(&p->p_stype); 29206ad42b2SJohn Baldwin 29306ad42b2SJohn Baldwin /* 29406ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 29506ad42b2SJohn Baldwin * release their reference. 29606ad42b2SJohn Baldwin */ 29706ad42b2SJohn Baldwin while (p->p_lock > 0) 29806ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 299ebceaf6dSDavid Xu 300e602ba25SJulian Elischer PROC_UNLOCK(p); 3011c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 3021c4bcd05SJeff Roberson callout_drain(&p->p_limco); 303b40ce416SJulian Elischer 30400c28d96SRobert Watson #ifdef AUDIT 30500c28d96SRobert Watson /* 30600c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 30700c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 30800c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 30900c28d96SRobert Watson * what the return value is. 31000c28d96SRobert Watson */ 311b4490c6eSKonstantin Belousov AUDIT_ARG_EXIT(rval, 0); 31200c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 31300c28d96SRobert Watson #endif 31400c28d96SRobert Watson 315bad2520aSMateusz Guzik /* Are we a task leader with peers? */ 316bad2520aSMateusz Guzik if (p->p_peers != NULL && p == p->p_leader) { 317c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 3182c1011f7SJohn Dyson q = p->p_peers; 319776e0b36SJohn Baldwin while (q != NULL) { 320776e0b36SJohn Baldwin PROC_LOCK(q); 3218451d0ddSKip Macy kern_psignal(q, SIGKILL); 322776e0b36SJohn Baldwin PROC_UNLOCK(q); 3232c1011f7SJohn Dyson q = q->p_peers; 3242c1011f7SJohn Dyson } 325c6544064SJohn Baldwin while (p->p_peers != NULL) 326c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 327c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 3282c1011f7SJohn Dyson } 3292c1011f7SJohn Dyson 330fed06968SJulian Elischer /* 331e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 332b4490c6eSKonstantin Belousov * E.g. SYSV IPC stuff. 333b4490c6eSKonstantin Belousov * Event handler could change exit status. 334fed06968SJulian Elischer * XXX what if one of these generates an error? 335fed06968SJulian Elischer */ 3362ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(process_exit, p); 337a914fb6bSJohn Baldwin 338df8bae1dSRodney W. Grimes /* 339df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 340df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 341df8bae1dSRodney W. Grimes */ 342a914fb6bSJohn Baldwin PROC_LOCK(p); 343a282253aSJulian Elischer stopprofclock(p); 344888d4d4fSKonstantin Belousov p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 3458d570f64SJohn Baldwin p->p_ptevents = 0; 3465499ea01SJohn Baldwin 3475499ea01SJohn Baldwin /* 3485499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 3495499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 3505499ea01SJohn Baldwin */ 3515499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 35256d48d6dSKonstantin Belousov _callout_stop_safe(&p->p_itcallout, CS_EXECUTING, NULL) == 0) { 3535499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 3545499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 3555499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 3565499ea01SJohn Baldwin ("realtime timer is still armed")); 3575499ea01SJohn Baldwin } 3582a339d9eSKonstantin Belousov 35996d7f8efSTim J. Robbins PROC_UNLOCK(p); 360df8bae1dSRodney W. Grimes 3612a339d9eSKonstantin Belousov umtx_thread_exit(td); 3622a339d9eSKonstantin Belousov 363df8bae1dSRodney W. Grimes /* 364831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 365831d27a9SDon Lewis * F_SETOWN with our pid. 366831d27a9SDon Lewis */ 367831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 368831d27a9SDon Lewis 369831d27a9SDon Lewis /* 370c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 371c0bc2867SGleb Smirnoff */ 372c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 373c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 374c0bc2867SGleb Smirnoff 375c0bc2867SGleb Smirnoff /* 376df8bae1dSRodney W. Grimes * Close open files and release open-file table. 377df8bae1dSRodney W. Grimes * This may block! 378df8bae1dSRodney W. Grimes */ 3792609222aSPawel Jakub Dawidek fdescfree(td); 380df8bae1dSRodney W. Grimes 381a914fb6bSJohn Baldwin /* 382a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 383a2587073SPoul-Henning Kamp * stop before we return to userland 384a2587073SPoul-Henning Kamp */ 385a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 386a2587073SPoul-Henning Kamp g_waitidle(); 387a2587073SPoul-Henning Kamp 388a2587073SPoul-Henning Kamp /* 389a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 390a914fb6bSJohn Baldwin */ 391bad2520aSMateusz Guzik if (p->p_leader->p_peers != NULL) { 392c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 393bad2520aSMateusz Guzik if (p->p_leader->p_peers != NULL) { 39479fc0bf4SMike Smith q = p->p_leader; 39579fc0bf4SMike Smith while (q->p_peers != p) 39679fc0bf4SMike Smith q = q->p_peers; 39779fc0bf4SMike Smith q->p_peers = p->p_peers; 3987f05b035SAlfred Perlstein wakeup(p->p_leader); 39979fc0bf4SMike Smith } 400c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 401bad2520aSMateusz Guzik } 40279fc0bf4SMike Smith 40357051fdcSTor Egge vmspace_exit(td); 40417182b13SMateusz Guzik killjobc(); 405b40ce416SJulian Elischer (void)acct_process(td); 406bc093719SEd Schouten 407df8bae1dSRodney W. Grimes #ifdef KTRACE 4082c255e9dSRobert Watson ktrprocexit(td); 409df8bae1dSRodney W. Grimes #endif 410df8bae1dSRodney W. Grimes /* 411ee42d0a9SDavid Malone * Release reference to text vnode 412ee42d0a9SDavid Malone */ 41388f98985SMateusz Guzik if (p->p_textvp != NULL) { 41488f98985SMateusz Guzik vrele(p->p_textvp); 415ee42d0a9SDavid Malone p->p_textvp = NULL; 416ee42d0a9SDavid Malone } 417ee42d0a9SDavid Malone 418ee42d0a9SDavid Malone /* 419d7aadbf9SJohn Baldwin * Release our limits structure. 420d7aadbf9SJohn Baldwin */ 42188f98985SMateusz Guzik lim_free(p->p_limit); 422d7aadbf9SJohn Baldwin p->p_limit = NULL; 423d7aadbf9SJohn Baldwin 424cf7d9a8cSDavid Xu tidhash_remove(td); 425cf7d9a8cSDavid Xu 426d7aadbf9SJohn Baldwin /* 427df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 428df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 429df8bae1dSRodney W. Grimes */ 4301005a129SJohn Baldwin sx_xlock(&allproc_lock); 431b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 432b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 433b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4341005a129SJohn Baldwin sx_xunlock(&allproc_lock); 435df8bae1dSRodney W. Grimes 43637f84a60SJohn Baldwin /* 4371bba2a94SJohn Baldwin * Call machine-dependent code to release any 4381bba2a94SJohn Baldwin * machine-dependent resources other than the address space. 4391bba2a94SJohn Baldwin * The address space is released by "vmspace_exitfree(p)" in 4401bba2a94SJohn Baldwin * vm_waitproc(). 4411bba2a94SJohn Baldwin */ 4421bba2a94SJohn Baldwin cpu_exit(td); 4431bba2a94SJohn Baldwin 4441bba2a94SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 4451bba2a94SJohn Baldwin 4461bba2a94SJohn Baldwin /* 447a661bebeSMateusz Guzik * Reparent all children processes: 448a661bebeSMateusz Guzik * - traced ones to the original parent (or init if we are that parent) 449a661bebeSMateusz Guzik * - the rest to init 45037f84a60SJohn Baldwin */ 4511005a129SJohn Baldwin sx_xlock(&proctree_lock); 4522e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 453a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 454237623b0SKonstantin Belousov wakeup(q->p_reaper); 455a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4562e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 4579a2dde80SKonstantin Belousov ksi = ksiginfo_alloc(TRUE); 458a914fb6bSJohn Baldwin PROC_LOCK(q); 4594ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 460904c5ec4SDavid Xu 461a661bebeSMateusz Guzik if (!(q->p_flag & P_TRACED)) { 462237623b0SKonstantin Belousov proc_reparent(q, q->p_reaper); 463545d3122SKonstantin Belousov if (q->p_state == PRS_ZOMBIE) { 4649a2dde80SKonstantin Belousov /* 4659a2dde80SKonstantin Belousov * Inform reaper about the reparented 4669a2dde80SKonstantin Belousov * zombie, since wait(2) has something 4679a2dde80SKonstantin Belousov * new to report. Guarantee queueing 4689a2dde80SKonstantin Belousov * of the SIGCHLD signal, similar to 4699a2dde80SKonstantin Belousov * the _exit() behaviour, by providing 4709a2dde80SKonstantin Belousov * our ksiginfo. Ksi is freed by the 4719a2dde80SKonstantin Belousov * signal delivery. 4729a2dde80SKonstantin Belousov */ 4739a2dde80SKonstantin Belousov if (q->p_ksi == NULL) { 4749a2dde80SKonstantin Belousov ksi1 = NULL; 4759a2dde80SKonstantin Belousov } else { 4769a2dde80SKonstantin Belousov ksiginfo_copy(q->p_ksi, ksi); 4779a2dde80SKonstantin Belousov ksi->ksi_flags |= KSI_INS; 4789a2dde80SKonstantin Belousov ksi1 = ksi; 4799a2dde80SKonstantin Belousov ksi = NULL; 4809a2dde80SKonstantin Belousov } 481545d3122SKonstantin Belousov PROC_LOCK(q->p_reaper); 4829a2dde80SKonstantin Belousov pksignal(q->p_reaper, SIGCHLD, ksi1); 483545d3122SKonstantin Belousov PROC_UNLOCK(q->p_reaper); 484545d3122SKonstantin Belousov } 485a661bebeSMateusz Guzik } else { 486a661bebeSMateusz Guzik /* 487a661bebeSMateusz Guzik * Traced processes are killed since their existence 488a661bebeSMateusz Guzik * means someone is screwing up. 489a661bebeSMateusz Guzik */ 490a661bebeSMateusz Guzik t = proc_realparent(q); 491a661bebeSMateusz Guzik if (t == p) { 492237623b0SKonstantin Belousov proc_reparent(q, q->p_reaper); 493a661bebeSMateusz Guzik } else { 494a661bebeSMateusz Guzik PROC_LOCK(t); 495a661bebeSMateusz Guzik proc_reparent(q, t); 496a661bebeSMateusz Guzik PROC_UNLOCK(t); 497a661bebeSMateusz Guzik } 4985085ecb7SKonstantin Belousov /* 4995085ecb7SKonstantin Belousov * Since q was found on our children list, the 5005085ecb7SKonstantin Belousov * proc_reparent() call moved q to the orphan 5015085ecb7SKonstantin Belousov * list due to present P_TRACED flag. Clear 5025085ecb7SKonstantin Belousov * orphan link for q now while q is locked. 5035085ecb7SKonstantin Belousov */ 5045085ecb7SKonstantin Belousov clear_orphan(q); 505c2836532SDavid Xu q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 506b7a25e63SKonstantin Belousov q->p_flag2 &= ~P2_PTRACE_FSTP; 5078d570f64SJohn Baldwin q->p_ptevents = 0; 508b7a25e63SKonstantin Belousov FOREACH_THREAD_IN_PROC(q, tdt) { 509b7a25e63SKonstantin Belousov tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG | 510b7a25e63SKonstantin Belousov TDB_FSTP); 511b7a25e63SKonstantin Belousov } 5128451d0ddSKip Macy kern_psignal(q, SIGKILL); 513c65437a3SJohn Baldwin } 514a914fb6bSJohn Baldwin PROC_UNLOCK(q); 5159a2dde80SKonstantin Belousov if (ksi != NULL) 5169a2dde80SKonstantin Belousov ksiginfo_free(ksi); 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes 5195085ecb7SKonstantin Belousov /* 5205085ecb7SKonstantin Belousov * Also get rid of our orphans. 5215085ecb7SKonstantin Belousov */ 5225085ecb7SKonstantin Belousov while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 5235085ecb7SKonstantin Belousov PROC_LOCK(q); 524515b7a0bSJohn Baldwin CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid, 525515b7a0bSJohn Baldwin q->p_pid); 5265085ecb7SKonstantin Belousov clear_orphan(q); 5275085ecb7SKonstantin Belousov PROC_UNLOCK(q); 5285085ecb7SKonstantin Belousov } 5295085ecb7SKonstantin Belousov 5301c4bcd05SJeff Roberson /* Save exit status. */ 531d7aadbf9SJohn Baldwin PROC_LOCK(p); 532cbf4e354SDavid Xu p->p_xthread = td; 5335d217f17SJohn Birrell 5340304c731SJamie Gritton /* Tell the prison that we are gone. */ 535413628a7SBjoern A. Zeeb prison_proc_free(p->p_ucred->cr_prison); 536413628a7SBjoern A. Zeeb 5375d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 5385d217f17SJohn Birrell /* 5395d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 5405d217f17SJohn Birrell * has declared an interest. 5415d217f17SJohn Birrell */ 5425d217f17SJohn Birrell if (dtrace_fasttrap_exit) 5435d217f17SJohn Birrell dtrace_fasttrap_exit(p); 5445d217f17SJohn Birrell #endif 5455d217f17SJohn Birrell 5461c4bcd05SJeff Roberson /* 5477a6b989bSJohn Baldwin * Notify interested parties of our demise. 548cb679c38SJonathan Lemon */ 5499e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_EXIT); 5507eaec467SJohn Baldwin 5515d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 5525d217f17SJohn Birrell int reason = CLD_EXITED; 553b4490c6eSKonstantin Belousov if (WCOREDUMP(signo)) 5545d217f17SJohn Birrell reason = CLD_DUMPED; 555b4490c6eSKonstantin Belousov else if (WIFSIGNALED(signo)) 5565d217f17SJohn Birrell reason = CLD_KILLED; 55736160958SMark Johnston SDT_PROBE1(proc, , , exit, reason); 5585d217f17SJohn Birrell #endif 5595d217f17SJohn Birrell 5601a29c806SOlivier Houchard /* 561cfb5f768SJonathan Anderson * If this is a process with a descriptor, we may not need to deliver 562cfb5f768SJonathan Anderson * a signal to the parent. proctree_lock is held over 563cfb5f768SJonathan Anderson * procdesc_exit() to serialize concurrent calls to close() and 564cfb5f768SJonathan Anderson * exit(). 565cfb5f768SJonathan Anderson */ 566cfb5f768SJonathan Anderson if (p->p_procdesc == NULL || procdesc_exit(p)) { 567cfb5f768SJonathan Anderson /* 568cfb5f768SJonathan Anderson * Notify parent that we're gone. If parent has the 569cfb5f768SJonathan Anderson * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 570cfb5f768SJonathan Anderson * notify process 1 instead (and hope it will handle this 571cfb5f768SJonathan Anderson * situation). 572df8bae1dSRodney W. Grimes */ 573d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 57490af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 575cfb5f768SJonathan Anderson if (p->p_pptr->p_sigacts->ps_flag & 576cfb5f768SJonathan Anderson (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 577276c5169SJohn Baldwin struct proc *pp; 578276c5169SJohn Baldwin 57990af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 580276c5169SJohn Baldwin pp = p->p_pptr; 581f591779bSSeigo Tanimura PROC_UNLOCK(pp); 582237623b0SKonstantin Belousov proc_reparent(p, p->p_reaper); 58355b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 584f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 585007abb3dSKonstantin Belousov 586245f17d4SJoerg Wunsch /* 587007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 5886fae832aSKonstantin Belousov * executing waitpid(2) with our pid, he will 589245f17d4SJoerg Wunsch * continue. 590245f17d4SJoerg Wunsch */ 5917f05b035SAlfred Perlstein wakeup(pp); 59290af4afaSJohn Baldwin } else 59390af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 594245f17d4SJoerg Wunsch 595237623b0SKonstantin Belousov if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) 596237623b0SKonstantin Belousov childproc_exited(p); 597ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 598ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 599ebceaf6dSDavid Xu childproc_exited(p); 600ebceaf6dSDavid Xu else /* LINUX thread */ 6018451d0ddSKip Macy kern_psignal(p->p_pptr, p->p_sigparent); 602ebceaf6dSDavid Xu } 603cfb5f768SJonathan Anderson } else 604cfb5f768SJonathan Anderson PROC_LOCK(p->p_pptr); 605d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 606696058c3SJulian Elischer 607eb30c1c0SPeter Wemm /* 608f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 609f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 610f71e748dSDavid Xu * for signal queue at the time when the state is set. 611f71e748dSDavid Xu */ 612f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 613f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 614f71e748dSDavid Xu 615f71e748dSDavid Xu /* 616462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 617a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 618a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 619ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 620ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 621ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 622ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 623ddf9c4f7SJohn Baldwin * proc lock. 624eb30c1c0SPeter Wemm */ 625ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 626aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 627982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 628982d11f8SJeff Roberson PROC_SLOCK(p); 629e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 630d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 631871684b8SBruce Evans 632f6f230feSJeff Roberson /* 633a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 634a140976eSAttilio Rao */ 6355c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 636a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 6375c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 638a140976eSAttilio Rao 639a140976eSAttilio Rao /* 640696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 641e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 642696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 643e602ba25SJulian Elischer */ 644e602ba25SJulian Elischer thread_exit(); 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes 64725f6e35aSPoul-Henning Kamp 64825f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 64925f6e35aSPoul-Henning Kamp struct abort2_args { 65025f6e35aSPoul-Henning Kamp char *why; 65125f6e35aSPoul-Henning Kamp int nargs; 65225f6e35aSPoul-Henning Kamp void **args; 65325f6e35aSPoul-Henning Kamp }; 65425f6e35aSPoul-Henning Kamp #endif 65525f6e35aSPoul-Henning Kamp 65625f6e35aSPoul-Henning Kamp int 6578451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap) 65825f6e35aSPoul-Henning Kamp { 65925f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 66025f6e35aSPoul-Henning Kamp struct sbuf *sb; 66125f6e35aSPoul-Henning Kamp void *uargs[16]; 66225f6e35aSPoul-Henning Kamp int error, i, sig; 66325f6e35aSPoul-Henning Kamp 66425f6e35aSPoul-Henning Kamp /* 66525f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 66625f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 66725f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 66825f6e35aSPoul-Henning Kamp */ 66925f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 67025f6e35aSPoul-Henning Kamp sbuf_clear(sb); 67125f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 67225f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 67325f6e35aSPoul-Henning Kamp /* 67425f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 67525f6e35aSPoul-Henning Kamp * abort2() was called improperly 67625f6e35aSPoul-Henning Kamp */ 67725f6e35aSPoul-Henning Kamp sig = SIGKILL; 67825f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 67925f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 68025f6e35aSPoul-Henning Kamp goto out; 6814218a731SPoul-Henning Kamp if (uap->nargs > 0) { 68225f6e35aSPoul-Henning Kamp if (uap->args == NULL) 68325f6e35aSPoul-Henning Kamp goto out; 68425f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 68525f6e35aSPoul-Henning Kamp if (error != 0) 68625f6e35aSPoul-Henning Kamp goto out; 6874218a731SPoul-Henning Kamp } 68825f6e35aSPoul-Henning Kamp /* 68925f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 69025f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 69125f6e35aSPoul-Henning Kamp */ 69225f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 69325f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 69425f6e35aSPoul-Henning Kamp if (error < 0) 69525f6e35aSPoul-Henning Kamp goto out; 69625f6e35aSPoul-Henning Kamp } else { 69725f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 69825f6e35aSPoul-Henning Kamp } 6994218a731SPoul-Henning Kamp if (uap->nargs > 0) { 70025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 70125f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 70225f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 70325f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 70425f6e35aSPoul-Henning Kamp } 70525f6e35aSPoul-Henning Kamp /* 70625f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 70725f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 70825f6e35aSPoul-Henning Kamp * from user-space succeed. 70925f6e35aSPoul-Henning Kamp */ 71025f6e35aSPoul-Henning Kamp sig = SIGABRT; 71125f6e35aSPoul-Henning Kamp out: 71225f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 71325f6e35aSPoul-Henning Kamp sbuf_trim(sb); 71425f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 71525f6e35aSPoul-Henning Kamp } 71625f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 71725f6e35aSPoul-Henning Kamp sbuf_finish(sb); 71825f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 71925f6e35aSPoul-Henning Kamp sbuf_delete(sb); 720b4490c6eSKonstantin Belousov exit1(td, 0, sig); 72125f6e35aSPoul-Henning Kamp return (0); 72225f6e35aSPoul-Henning Kamp } 72325f6e35aSPoul-Henning Kamp 72425f6e35aSPoul-Henning Kamp 725b2f9e8b1SBruce Evans #ifdef COMPAT_43 726234216efSMatthew Dillon /* 727a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 728234216efSMatthew Dillon */ 72926f9a767SRodney W. Grimes int 730830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 731df8bae1dSRodney W. Grimes { 732b7e23e82SJohn Baldwin int error, status; 733df8bae1dSRodney W. Grimes 734b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 735b7e23e82SJohn Baldwin if (error == 0) 736b7e23e82SJohn Baldwin td->td_retval[1] = status; 737b7e23e82SJohn Baldwin return (error); 738df8bae1dSRodney W. Grimes } 739b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 740df8bae1dSRodney W. Grimes 741234216efSMatthew Dillon /* 742a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 743234216efSMatthew Dillon */ 74426f9a767SRodney W. Grimes int 745f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap) 746df8bae1dSRodney W. Grimes { 74778c85e8dSJohn Baldwin struct rusage ru, *rup; 748b7e23e82SJohn Baldwin int error, status; 749b40ce416SJulian Elischer 75078c85e8dSJohn Baldwin if (uap->rusage != NULL) 75178c85e8dSJohn Baldwin rup = &ru; 75278c85e8dSJohn Baldwin else 75378c85e8dSJohn Baldwin rup = NULL; 75478c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 755d30e66e5SJilles Tjoelker if (uap->status != NULL && error == 0 && td->td_retval[0] != 0) 756b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 757d30e66e5SJilles Tjoelker if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0) 758b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 759b7e23e82SJohn Baldwin return (error); 760df8bae1dSRodney W. Grimes } 761df8bae1dSRodney W. Grimes 762f13b5a0fSKonstantin Belousov int 763f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap) 764f13b5a0fSKonstantin Belousov { 765f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 766f13b5a0fSKonstantin Belousov siginfo_t si, *sip; 767f13b5a0fSKonstantin Belousov idtype_t idtype; 768f13b5a0fSKonstantin Belousov id_t id; 769a2a85596SKonstantin Belousov int error, status; 770f13b5a0fSKonstantin Belousov 771f13b5a0fSKonstantin Belousov idtype = uap->idtype; 772f13b5a0fSKonstantin Belousov id = uap->id; 773f13b5a0fSKonstantin Belousov 774f13b5a0fSKonstantin Belousov if (uap->wrusage != NULL) 775f13b5a0fSKonstantin Belousov wrup = &wru; 776f13b5a0fSKonstantin Belousov else 777f13b5a0fSKonstantin Belousov wrup = NULL; 778f13b5a0fSKonstantin Belousov 779f13b5a0fSKonstantin Belousov if (uap->info != NULL) { 780f13b5a0fSKonstantin Belousov sip = &si; 781f13b5a0fSKonstantin Belousov bzero(sip, sizeof(*sip)); 782f13b5a0fSKonstantin Belousov } else 783f13b5a0fSKonstantin Belousov sip = NULL; 784f13b5a0fSKonstantin Belousov 785f13b5a0fSKonstantin Belousov /* 786f13b5a0fSKonstantin Belousov * We expect all callers of wait6() to know about WEXITED and 787f13b5a0fSKonstantin Belousov * WTRAPPED. 788f13b5a0fSKonstantin Belousov */ 789f13b5a0fSKonstantin Belousov error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip); 790f13b5a0fSKonstantin Belousov 791d30e66e5SJilles Tjoelker if (uap->status != NULL && error == 0 && td->td_retval[0] != 0) 792f13b5a0fSKonstantin Belousov error = copyout(&status, uap->status, sizeof(status)); 793d30e66e5SJilles Tjoelker if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0) 794f13b5a0fSKonstantin Belousov error = copyout(&wru, uap->wrusage, sizeof(wru)); 795f13b5a0fSKonstantin Belousov if (uap->info != NULL && error == 0) 796f13b5a0fSKonstantin Belousov error = copyout(&si, uap->info, sizeof(si)); 797f13b5a0fSKonstantin Belousov return (error); 798f13b5a0fSKonstantin Belousov } 799f13b5a0fSKonstantin Belousov 800324fb6beSRobert Watson /* 801324fb6beSRobert Watson * Reap the remains of a zombie process and optionally return status and 802324fb6beSRobert Watson * rusage. Asserts and will release both the proctree_lock and the process 803324fb6beSRobert Watson * lock as part of its work. 804324fb6beSRobert Watson */ 805cfb5f768SJonathan Anderson void 806f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options) 807324fb6beSRobert Watson { 808324fb6beSRobert Watson struct proc *q, *t; 809324fb6beSRobert Watson 810324fb6beSRobert Watson sx_assert(&proctree_lock, SA_XLOCKED); 811324fb6beSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 812324fb6beSRobert Watson PROC_SLOCK_ASSERT(p, MA_OWNED); 813324fb6beSRobert Watson KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 814324fb6beSRobert Watson 815324fb6beSRobert Watson q = td->td_proc; 816f13b5a0fSKonstantin Belousov 817324fb6beSRobert Watson PROC_SUNLOCK(p); 818324fb6beSRobert Watson if (status) 819b4490c6eSKonstantin Belousov *status = KW_EXITCODE(p->p_xexit, p->p_xsig); 820324fb6beSRobert Watson if (options & WNOWAIT) { 821324fb6beSRobert Watson /* 822324fb6beSRobert Watson * Only poll, returning the status. Caller does not wish to 823324fb6beSRobert Watson * release the proc struct just yet. 824324fb6beSRobert Watson */ 825324fb6beSRobert Watson PROC_UNLOCK(p); 826324fb6beSRobert Watson sx_xunlock(&proctree_lock); 827324fb6beSRobert Watson return; 828324fb6beSRobert Watson } 829324fb6beSRobert Watson 830324fb6beSRobert Watson PROC_LOCK(q); 831324fb6beSRobert Watson sigqueue_take(p->p_ksi); 832324fb6beSRobert Watson PROC_UNLOCK(q); 833324fb6beSRobert Watson 834324fb6beSRobert Watson /* 835324fb6beSRobert Watson * If we got the child via a ptrace 'attach', we need to give it back 836324fb6beSRobert Watson * to the old parent. 837324fb6beSRobert Watson */ 838c209e3e2SJohn Baldwin if (p->p_oppid != 0 && p->p_oppid != p->p_pptr->p_pid) { 839c209e3e2SJohn Baldwin PROC_UNLOCK(p); 840d7359980SKonstantin Belousov t = proc_realparent(p); 841d7359980SKonstantin Belousov PROC_LOCK(t); 842324fb6beSRobert Watson PROC_LOCK(p); 843515b7a0bSJohn Baldwin CTR2(KTR_PTRACE, 844515b7a0bSJohn Baldwin "wait: traced child %d moved back to parent %d", p->p_pid, 845515b7a0bSJohn Baldwin t->p_pid); 846324fb6beSRobert Watson proc_reparent(p, t); 847f528c3fdSDavid E. O'Brien p->p_oppid = 0; 848324fb6beSRobert Watson PROC_UNLOCK(p); 849ad6eec7bSJohn Baldwin pksignal(t, SIGCHLD, p->p_ksi); 850324fb6beSRobert Watson wakeup(t); 851324fb6beSRobert Watson cv_broadcast(&p->p_pwait); 852324fb6beSRobert Watson PROC_UNLOCK(t); 853324fb6beSRobert Watson sx_xunlock(&proctree_lock); 854324fb6beSRobert Watson return; 855324fb6beSRobert Watson } 856c209e3e2SJohn Baldwin p->p_oppid = 0; 857c209e3e2SJohn Baldwin PROC_UNLOCK(p); 858324fb6beSRobert Watson 859324fb6beSRobert Watson /* 860324fb6beSRobert Watson * Remove other references to this process to ensure we have an 861324fb6beSRobert Watson * exclusive reference. 862324fb6beSRobert Watson */ 863324fb6beSRobert Watson sx_xlock(&allproc_lock); 864324fb6beSRobert Watson LIST_REMOVE(p, p_list); /* off zombproc */ 865324fb6beSRobert Watson sx_xunlock(&allproc_lock); 866324fb6beSRobert Watson LIST_REMOVE(p, p_sibling); 867237623b0SKonstantin Belousov reaper_abandon_children(p, true); 868237623b0SKonstantin Belousov LIST_REMOVE(p, p_reapsibling); 8697335ed90SKonstantin Belousov PROC_LOCK(p); 8702e39e24fSKonstantin Belousov clear_orphan(p); 8717335ed90SKonstantin Belousov PROC_UNLOCK(p); 872324fb6beSRobert Watson leavepgrp(p); 873cfb5f768SJonathan Anderson if (p->p_procdesc != NULL) 874cfb5f768SJonathan Anderson procdesc_reap(p); 875324fb6beSRobert Watson sx_xunlock(&proctree_lock); 876324fb6beSRobert Watson 8779e590ff0SKonstantin Belousov PROC_LOCK(p); 8789e590ff0SKonstantin Belousov knlist_detach(p->p_klist); 8799e590ff0SKonstantin Belousov p->p_klist = NULL; 8809e590ff0SKonstantin Belousov PROC_UNLOCK(p); 8819e590ff0SKonstantin Belousov 882324fb6beSRobert Watson /* 883fbe503d4SMateusz Guzik * Removal from allproc list and process group list paired with 884fbe503d4SMateusz Guzik * PROC_LOCK which was executed during that time should guarantee 885fbe503d4SMateusz Guzik * nothing can reach this process anymore. As such further locking 886fbe503d4SMateusz Guzik * is unnecessary. 887324fb6beSRobert Watson */ 888b4490c6eSKonstantin Belousov p->p_xexit = p->p_xsig = 0; /* XXX: why? */ 889fbe503d4SMateusz Guzik 890324fb6beSRobert Watson PROC_LOCK(q); 891324fb6beSRobert Watson ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 892324fb6beSRobert Watson PROC_UNLOCK(q); 893324fb6beSRobert Watson 894324fb6beSRobert Watson /* 895324fb6beSRobert Watson * Decrement the count of procs running with this uid. 896324fb6beSRobert Watson */ 897324fb6beSRobert Watson (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 898324fb6beSRobert Watson 899324fb6beSRobert Watson /* 900097055e2SEdward Tomasz Napierala * Destroy resource accounting information associated with the process. 901097055e2SEdward Tomasz Napierala */ 902afcc55f3SEdward Tomasz Napierala #ifdef RACCT 9034b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 904b38520f0SEdward Tomasz Napierala PROC_LOCK(p); 905b38520f0SEdward Tomasz Napierala racct_sub(p, RACCT_NPROC, 1); 906b38520f0SEdward Tomasz Napierala PROC_UNLOCK(p); 9074b5c9cf6SEdward Tomasz Napierala } 908afcc55f3SEdward Tomasz Napierala #endif 909b38520f0SEdward Tomasz Napierala racct_proc_exit(p); 910097055e2SEdward Tomasz Napierala 911097055e2SEdward Tomasz Napierala /* 912324fb6beSRobert Watson * Free credentials, arguments, and sigacts. 913324fb6beSRobert Watson */ 914324fb6beSRobert Watson crfree(p->p_ucred); 915daf63fd2SMateusz Guzik proc_set_cred(p, NULL); 916324fb6beSRobert Watson pargs_drop(p->p_args); 917324fb6beSRobert Watson p->p_args = NULL; 918324fb6beSRobert Watson sigacts_free(p->p_sigacts); 919324fb6beSRobert Watson p->p_sigacts = NULL; 920324fb6beSRobert Watson 921324fb6beSRobert Watson /* 922324fb6beSRobert Watson * Do any thread-system specific cleanups. 923324fb6beSRobert Watson */ 924324fb6beSRobert Watson thread_wait(p); 925324fb6beSRobert Watson 926324fb6beSRobert Watson /* 927324fb6beSRobert Watson * Give vm and machine-dependent layer a chance to free anything that 928324fb6beSRobert Watson * cpu_exit couldn't release while still running in process context. 929324fb6beSRobert Watson */ 930324fb6beSRobert Watson vm_waitproc(p); 931324fb6beSRobert Watson #ifdef MAC 932324fb6beSRobert Watson mac_proc_destroy(p); 933324fb6beSRobert Watson #endif 9346520495aSAdrian Chadd /* 9356520495aSAdrian Chadd * Free any domain policy that's still hiding around. 9366520495aSAdrian Chadd */ 9376520495aSAdrian Chadd vm_domain_policy_cleanup(&p->p_vm_dom_policy); 9386520495aSAdrian Chadd 939324fb6beSRobert Watson KASSERT(FIRST_THREAD_IN_PROC(p), 940324fb6beSRobert Watson ("proc_reap: no residual thread!")); 941324fb6beSRobert Watson uma_zfree(proc_zone, p); 9424b48959fSKonstantin Belousov atomic_add_int(&nprocs, -1); 943324fb6beSRobert Watson } 944324fb6beSRobert Watson 945dcd43281SKonstantin Belousov static int 946f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, 94757c74f5bSJohn Baldwin int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo, 94857c74f5bSJohn Baldwin int check_only) 949dcd43281SKonstantin Belousov { 950f13b5a0fSKonstantin Belousov struct rusage *rup; 951dcd43281SKonstantin Belousov 952db62ced2SJaakko Heinonen sx_assert(&proctree_lock, SA_XLOCKED); 953db62ced2SJaakko Heinonen 954dcd43281SKonstantin Belousov PROC_LOCK(p); 955f13b5a0fSKonstantin Belousov 956f13b5a0fSKonstantin Belousov switch (idtype) { 957f13b5a0fSKonstantin Belousov case P_ALL: 95853bf545dSMariusz Zaborski if (p->p_procdesc != NULL) { 95953bf545dSMariusz Zaborski PROC_UNLOCK(p); 96053bf545dSMariusz Zaborski return (0); 96153bf545dSMariusz Zaborski } 962f13b5a0fSKonstantin Belousov break; 963f13b5a0fSKonstantin Belousov case P_PID: 964f13b5a0fSKonstantin Belousov if (p->p_pid != (pid_t)id) { 965dcd43281SKonstantin Belousov PROC_UNLOCK(p); 966dcd43281SKonstantin Belousov return (0); 967dcd43281SKonstantin Belousov } 968f13b5a0fSKonstantin Belousov break; 969f13b5a0fSKonstantin Belousov case P_PGID: 970f13b5a0fSKonstantin Belousov if (p->p_pgid != (pid_t)id) { 971f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 972f13b5a0fSKonstantin Belousov return (0); 973f13b5a0fSKonstantin Belousov } 974f13b5a0fSKonstantin Belousov break; 975f13b5a0fSKonstantin Belousov case P_SID: 976f13b5a0fSKonstantin Belousov if (p->p_session->s_sid != (pid_t)id) { 977f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 978f13b5a0fSKonstantin Belousov return (0); 979f13b5a0fSKonstantin Belousov } 980f13b5a0fSKonstantin Belousov break; 981f13b5a0fSKonstantin Belousov case P_UID: 982f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_uid != (uid_t)id) { 983f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 984f13b5a0fSKonstantin Belousov return (0); 985f13b5a0fSKonstantin Belousov } 986f13b5a0fSKonstantin Belousov break; 987f13b5a0fSKonstantin Belousov case P_GID: 988f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_gid != (gid_t)id) { 989f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 990f13b5a0fSKonstantin Belousov return (0); 991f13b5a0fSKonstantin Belousov } 992f13b5a0fSKonstantin Belousov break; 993f13b5a0fSKonstantin Belousov case P_JAILID: 994b1051d92SMateusz Guzik if (p->p_ucred->cr_prison->pr_id != (int)id) { 995f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 996f13b5a0fSKonstantin Belousov return (0); 997f13b5a0fSKonstantin Belousov } 998f13b5a0fSKonstantin Belousov break; 999f13b5a0fSKonstantin Belousov /* 1000f13b5a0fSKonstantin Belousov * It seems that the thread structures get zeroed out 1001f13b5a0fSKonstantin Belousov * at process exit. This makes it impossible to 1002f13b5a0fSKonstantin Belousov * support P_SETID, P_CID or P_CPUID. 1003f13b5a0fSKonstantin Belousov */ 1004f13b5a0fSKonstantin Belousov default: 1005f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1006f13b5a0fSKonstantin Belousov return (0); 1007f13b5a0fSKonstantin Belousov } 1008f13b5a0fSKonstantin Belousov 1009dcd43281SKonstantin Belousov if (p_canwait(td, p)) { 1010dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1011dcd43281SKonstantin Belousov return (0); 1012dcd43281SKonstantin Belousov } 1013dcd43281SKonstantin Belousov 1014f13b5a0fSKonstantin Belousov if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) { 1015f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1016f13b5a0fSKonstantin Belousov return (0); 1017f13b5a0fSKonstantin Belousov } 1018f13b5a0fSKonstantin Belousov 1019dcd43281SKonstantin Belousov /* 1020dcd43281SKonstantin Belousov * This special case handles a kthread spawned by linux_clone 1021dcd43281SKonstantin Belousov * (see linux_misc.c). The linux_wait4 and linux_waitpid 1022dcd43281SKonstantin Belousov * functions need to be able to distinguish between waiting 1023dcd43281SKonstantin Belousov * on a process and waiting on a thread. It is a thread if 1024dcd43281SKonstantin Belousov * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 1025dcd43281SKonstantin Belousov * signifies we want to wait for threads and not processes. 1026dcd43281SKonstantin Belousov */ 1027dcd43281SKonstantin Belousov if ((p->p_sigparent != SIGCHLD) ^ 1028dcd43281SKonstantin Belousov ((options & WLINUXCLONE) != 0)) { 1029dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1030dcd43281SKonstantin Belousov return (0); 1031dcd43281SKonstantin Belousov } 1032dcd43281SKonstantin Belousov 1033f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1034f13b5a0fSKonstantin Belousov bzero(siginfo, sizeof(*siginfo)); 1035f13b5a0fSKonstantin Belousov siginfo->si_errno = 0; 1036f13b5a0fSKonstantin Belousov 1037f13b5a0fSKonstantin Belousov /* 1038f13b5a0fSKonstantin Belousov * SUSv4 requires that the si_signo value is always 1039f13b5a0fSKonstantin Belousov * SIGCHLD. Obey it despite the rfork(2) interface 1040f13b5a0fSKonstantin Belousov * allows to request other signal for child exit 1041f13b5a0fSKonstantin Belousov * notification. 1042f13b5a0fSKonstantin Belousov */ 1043f13b5a0fSKonstantin Belousov siginfo->si_signo = SIGCHLD; 1044f13b5a0fSKonstantin Belousov 1045f13b5a0fSKonstantin Belousov /* 1046f13b5a0fSKonstantin Belousov * This is still a rough estimate. We will fix the 1047f13b5a0fSKonstantin Belousov * cases TRAPPED, STOPPED, and CONTINUED later. 1048f13b5a0fSKonstantin Belousov */ 1049b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 1050f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_DUMPED; 1051b4490c6eSKonstantin Belousov siginfo->si_status = WTERMSIG(p->p_xsig); 1052b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 1053f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_KILLED; 1054b4490c6eSKonstantin Belousov siginfo->si_status = WTERMSIG(p->p_xsig); 1055b20a9aa9SJilles Tjoelker } else { 1056f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_EXITED; 1057b4490c6eSKonstantin Belousov siginfo->si_status = p->p_xexit; 1058b20a9aa9SJilles Tjoelker } 1059f13b5a0fSKonstantin Belousov 1060f13b5a0fSKonstantin Belousov siginfo->si_pid = p->p_pid; 1061f13b5a0fSKonstantin Belousov siginfo->si_uid = p->p_ucred->cr_uid; 1062f13b5a0fSKonstantin Belousov 1063f13b5a0fSKonstantin Belousov /* 1064f13b5a0fSKonstantin Belousov * The si_addr field would be useful additional 1065f13b5a0fSKonstantin Belousov * detail, but apparently the PC value may be lost 1066f13b5a0fSKonstantin Belousov * when we reach this point. bzero() above sets 1067f13b5a0fSKonstantin Belousov * siginfo->si_addr to NULL. 1068f13b5a0fSKonstantin Belousov */ 1069f13b5a0fSKonstantin Belousov } 1070f13b5a0fSKonstantin Belousov 1071f13b5a0fSKonstantin Belousov /* 1072f13b5a0fSKonstantin Belousov * There should be no reason to limit resources usage info to 1073f13b5a0fSKonstantin Belousov * exited processes only. A snapshot about any resources used 1074f13b5a0fSKonstantin Belousov * by a stopped process may be exactly what is needed. 1075f13b5a0fSKonstantin Belousov */ 1076f13b5a0fSKonstantin Belousov if (wrusage != NULL) { 1077f13b5a0fSKonstantin Belousov rup = &wrusage->wru_self; 1078f13b5a0fSKonstantin Belousov *rup = p->p_ru; 10795c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 1080f13b5a0fSKonstantin Belousov calcru(p, &rup->ru_utime, &rup->ru_stime); 10815c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 1082f13b5a0fSKonstantin Belousov 1083f13b5a0fSKonstantin Belousov rup = &wrusage->wru_children; 1084f13b5a0fSKonstantin Belousov *rup = p->p_stats->p_cru; 1085f13b5a0fSKonstantin Belousov calccru(p, &rup->ru_utime, &rup->ru_stime); 1086f13b5a0fSKonstantin Belousov } 1087f13b5a0fSKonstantin Belousov 108857c74f5bSJohn Baldwin if (p->p_state == PRS_ZOMBIE && !check_only) { 10895c7bebf9SKonstantin Belousov PROC_SLOCK(p); 1090f13b5a0fSKonstantin Belousov proc_reap(td, p, status, options); 1091dcd43281SKonstantin Belousov return (-1); 1092dcd43281SKonstantin Belousov } 1093dcd43281SKonstantin Belousov return (1); 1094dcd43281SKonstantin Belousov } 1095dcd43281SKonstantin Belousov 1096b7e23e82SJohn Baldwin int 10977eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 10987eaec467SJohn Baldwin struct rusage *rusage) 1099df8bae1dSRodney W. Grimes { 1100f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 1101f13b5a0fSKonstantin Belousov idtype_t idtype; 1102f13b5a0fSKonstantin Belousov id_t id; 1103f13b5a0fSKonstantin Belousov int ret; 1104f13b5a0fSKonstantin Belousov 1105538375d4SKonstantin Belousov /* 1106538375d4SKonstantin Belousov * Translate the special pid values into the (idtype, pid) 1107538375d4SKonstantin Belousov * pair for kern_wait6. The WAIT_MYPGRP case is handled by 1108538375d4SKonstantin Belousov * kern_wait6() on its own. 1109538375d4SKonstantin Belousov */ 1110f13b5a0fSKonstantin Belousov if (pid == WAIT_ANY) { 1111f13b5a0fSKonstantin Belousov idtype = P_ALL; 1112f13b5a0fSKonstantin Belousov id = 0; 1113ea293f3fSKonstantin Belousov } else if (pid < 0) { 1114f13b5a0fSKonstantin Belousov idtype = P_PGID; 1115f13b5a0fSKonstantin Belousov id = (id_t)-pid; 1116ea293f3fSKonstantin Belousov } else { 1117f13b5a0fSKonstantin Belousov idtype = P_PID; 1118f13b5a0fSKonstantin Belousov id = (id_t)pid; 1119f13b5a0fSKonstantin Belousov } 1120538375d4SKonstantin Belousov 1121f13b5a0fSKonstantin Belousov if (rusage != NULL) 1122f13b5a0fSKonstantin Belousov wrup = &wru; 1123f13b5a0fSKonstantin Belousov else 1124f13b5a0fSKonstantin Belousov wrup = NULL; 1125538375d4SKonstantin Belousov 1126f13b5a0fSKonstantin Belousov /* 1127f13b5a0fSKonstantin Belousov * For backward compatibility we implicitly add flags WEXITED 1128f13b5a0fSKonstantin Belousov * and WTRAPPED here. 1129f13b5a0fSKonstantin Belousov */ 1130f13b5a0fSKonstantin Belousov options |= WEXITED | WTRAPPED; 1131f13b5a0fSKonstantin Belousov ret = kern_wait6(td, idtype, id, status, options, wrup, NULL); 1132f13b5a0fSKonstantin Belousov if (rusage != NULL) 1133f13b5a0fSKonstantin Belousov *rusage = wru.wru_self; 1134f13b5a0fSKonstantin Belousov return (ret); 1135f13b5a0fSKonstantin Belousov } 1136f13b5a0fSKonstantin Belousov 1137435da985SKonstantin Belousov static void 1138435da985SKonstantin Belousov report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo, 1139435da985SKonstantin Belousov int *status, int options, int si_code) 1140435da985SKonstantin Belousov { 1141435da985SKonstantin Belousov bool cont; 1142435da985SKonstantin Belousov 1143435da985SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 1144435da985SKonstantin Belousov sx_assert(&proctree_lock, SA_XLOCKED); 1145435da985SKonstantin Belousov MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED || 1146435da985SKonstantin Belousov si_code == CLD_CONTINUED); 1147435da985SKonstantin Belousov 1148435da985SKonstantin Belousov cont = si_code == CLD_CONTINUED; 1149435da985SKonstantin Belousov if ((options & WNOWAIT) == 0) { 1150435da985SKonstantin Belousov if (cont) 1151435da985SKonstantin Belousov p->p_flag &= ~P_CONTINUED; 1152435da985SKonstantin Belousov else 1153435da985SKonstantin Belousov p->p_flag |= P_WAITED; 1154435da985SKonstantin Belousov PROC_LOCK(td->td_proc); 1155435da985SKonstantin Belousov sigqueue_take(p->p_ksi); 1156435da985SKonstantin Belousov PROC_UNLOCK(td->td_proc); 1157435da985SKonstantin Belousov } 1158435da985SKonstantin Belousov sx_xunlock(&proctree_lock); 1159435da985SKonstantin Belousov if (siginfo != NULL) { 1160435da985SKonstantin Belousov siginfo->si_code = si_code; 1161435da985SKonstantin Belousov siginfo->si_status = cont ? SIGCONT : p->p_xsig; 1162435da985SKonstantin Belousov } 1163435da985SKonstantin Belousov if (status != NULL) 1164435da985SKonstantin Belousov *status = cont ? SIGCONT : W_STOPCODE(p->p_xsig); 1165435da985SKonstantin Belousov PROC_UNLOCK(p); 1166435da985SKonstantin Belousov td->td_retval[0] = p->p_pid; 1167435da985SKonstantin Belousov } 1168435da985SKonstantin Belousov 1169f13b5a0fSKonstantin Belousov int 1170f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, 1171f13b5a0fSKonstantin Belousov int options, struct __wrusage *wrusage, siginfo_t *siginfo) 1172f13b5a0fSKonstantin Belousov { 1173324fb6beSRobert Watson struct proc *p, *q; 11743a41ec6aSEd Schouten pid_t pid; 1175dcd43281SKonstantin Belousov int error, nfound, ret; 1176df8bae1dSRodney W. Grimes 1177f13b5a0fSKonstantin Belousov AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */ 1178f13b5a0fSKonstantin Belousov AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */ 11792ef24ddeSRobert Watson AUDIT_ARG_VALUE(options); 1180de3007e8SWayne Salamon 1181b40ce416SJulian Elischer q = td->td_proc; 1182f13b5a0fSKonstantin Belousov 1183a2a85596SKonstantin Belousov if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1184a545089eSKonstantin Belousov PROC_LOCK(q); 1185f13b5a0fSKonstantin Belousov id = (id_t)q->p_pgid; 1186a545089eSKonstantin Belousov PROC_UNLOCK(q); 1187f13b5a0fSKonstantin Belousov idtype = P_PGID; 1188f591779bSSeigo Tanimura } 1189f13b5a0fSKonstantin Belousov 1190f528c3fdSDavid E. O'Brien /* If we don't know the option, just return. */ 1191f13b5a0fSKonstantin Belousov if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT | 1192f13b5a0fSKonstantin Belousov WEXITED | WTRAPPED | WLINUXCLONE)) != 0) 11936dc958b9SJohn Baldwin return (EINVAL); 1194f13b5a0fSKonstantin Belousov if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) { 1195f13b5a0fSKonstantin Belousov /* 1196f13b5a0fSKonstantin Belousov * We will be unable to find any matching processes, 1197f13b5a0fSKonstantin Belousov * because there are no known events to look for. 1198f13b5a0fSKonstantin Belousov * Prefer to return error instead of blocking 1199f13b5a0fSKonstantin Belousov * indefinitely. 1200f13b5a0fSKonstantin Belousov */ 1201f13b5a0fSKonstantin Belousov return (EINVAL); 1202f13b5a0fSKonstantin Belousov } 1203f13b5a0fSKonstantin Belousov 1204df8bae1dSRodney W. Grimes loop: 1205902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 1206902c0d82SDavid Xu PROC_LOCK(q); 1207902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 1208902c0d82SDavid Xu PROC_UNLOCK(q); 1209902c0d82SDavid Xu } 1210df8bae1dSRodney W. Grimes nfound = 0; 1211d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 12122e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 12133a41ec6aSEd Schouten pid = p->p_pid; 1214f13b5a0fSKonstantin Belousov ret = proc_to_reap(td, p, idtype, id, status, options, 121557c74f5bSJohn Baldwin wrusage, siginfo, 0); 1216dcd43281SKonstantin Belousov if (ret == 0) 1217df8bae1dSRodney W. Grimes continue; 1218dcd43281SKonstantin Belousov else if (ret == 1) 1219df8bae1dSRodney W. Grimes nfound++; 12203a41ec6aSEd Schouten else { 12213a41ec6aSEd Schouten td->td_retval[0] = pid; 1222d7aadbf9SJohn Baldwin return (0); 12233a41ec6aSEd Schouten } 1224dcd43281SKonstantin Belousov 1225746b6e81SMateusz Guzik PROC_LOCK_ASSERT(p, MA_OWNED); 1226435da985SKonstantin Belousov 1227435da985SKonstantin Belousov if ((options & (WTRAPPED | WUNTRACED)) != 0) 1228dcd43281SKonstantin Belousov PROC_SLOCK(p); 1229f13b5a0fSKonstantin Belousov 1230f13b5a0fSKonstantin Belousov if ((options & WTRAPPED) != 0 && 1231f13b5a0fSKonstantin Belousov (p->p_flag & P_TRACED) != 0 && 1232f13b5a0fSKonstantin Belousov (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 && 1233435da985SKonstantin Belousov p->p_suspcount == p->p_numthreads && 1234435da985SKonstantin Belousov (p->p_flag & P_WAITED) == 0) { 1235982d11f8SJeff Roberson PROC_SUNLOCK(p); 1236515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, 1237435da985SKonstantin Belousov "wait: returning trapped pid %d status %#x " 1238435da985SKonstantin Belousov "(xstat %d) xthread %d", 1239b4490c6eSKonstantin Belousov p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig, 1240435da985SKonstantin Belousov p->p_xthread != NULL ? 1241435da985SKonstantin Belousov p->p_xthread->td_tid : -1); 1242435da985SKonstantin Belousov report_alive_proc(td, p, siginfo, status, options, 1243435da985SKonstantin Belousov CLD_TRAPPED); 1244f13b5a0fSKonstantin Belousov return (0); 1245f13b5a0fSKonstantin Belousov } 1246f13b5a0fSKonstantin Belousov if ((options & WUNTRACED) != 0 && 1247f13b5a0fSKonstantin Belousov (p->p_flag & P_STOPPED_SIG) != 0 && 1248435da985SKonstantin Belousov p->p_suspcount == p->p_numthreads && 1249435da985SKonstantin Belousov (p->p_flag & P_WAITED) == 0) { 1250f13b5a0fSKonstantin Belousov PROC_SUNLOCK(p); 1251435da985SKonstantin Belousov report_alive_proc(td, p, siginfo, status, options, 1252435da985SKonstantin Belousov CLD_STOPPED); 1253b7e23e82SJohn Baldwin return (0); 1254df8bae1dSRodney W. Grimes } 1255435da985SKonstantin Belousov if ((options & (WTRAPPED | WUNTRACED)) != 0) 1256982d11f8SJeff Roberson PROC_SUNLOCK(p); 1257f13b5a0fSKonstantin Belousov if ((options & WCONTINUED) != 0 && 1258f13b5a0fSKonstantin Belousov (p->p_flag & P_CONTINUED) != 0) { 1259435da985SKonstantin Belousov report_alive_proc(td, p, siginfo, status, options, 1260435da985SKonstantin Belousov CLD_CONTINUED); 1261dcdc6c36SKonstantin Belousov return (0); 12626ee093fbSMike Barcroft } 1263d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 1264d7aadbf9SJohn Baldwin } 1265dcd43281SKonstantin Belousov 1266dcd43281SKonstantin Belousov /* 1267dcd43281SKonstantin Belousov * Look in the orphans list too, to allow the parent to 1268dcd43281SKonstantin Belousov * collect it's child exit status even if child is being 1269dcd43281SKonstantin Belousov * debugged. 1270dcd43281SKonstantin Belousov * 1271dcd43281SKonstantin Belousov * Debugger detaches from the parent upon successful 1272dcd43281SKonstantin Belousov * switch-over from parent to child. At this point due to 1273dcd43281SKonstantin Belousov * re-parenting the parent loses the child to debugger and a 1274dcd43281SKonstantin Belousov * wait4(2) call would report that it has no children to wait 1275dcd43281SKonstantin Belousov * for. By maintaining a list of orphans we allow the parent 1276dcd43281SKonstantin Belousov * to successfully wait until the child becomes a zombie. 1277dcd43281SKonstantin Belousov */ 127857c74f5bSJohn Baldwin if (nfound == 0) { 1279dcd43281SKonstantin Belousov LIST_FOREACH(p, &q->p_orphans, p_orphan) { 128057c74f5bSJohn Baldwin ret = proc_to_reap(td, p, idtype, id, NULL, options, 128157c74f5bSJohn Baldwin NULL, NULL, 1); 128257c74f5bSJohn Baldwin if (ret != 0) { 128357c74f5bSJohn Baldwin KASSERT(ret != -1, ("reaped an orphan (pid %d)", 128457c74f5bSJohn Baldwin (int)td->td_retval[0])); 1285746b6e81SMateusz Guzik PROC_UNLOCK(p); 1286dcd43281SKonstantin Belousov nfound++; 128757c74f5bSJohn Baldwin break; 128857c74f5bSJohn Baldwin } 128957c74f5bSJohn Baldwin } 1290dcd43281SKonstantin Belousov } 1291d7aadbf9SJohn Baldwin if (nfound == 0) { 1292d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1293d7aadbf9SJohn Baldwin return (ECHILD); 1294d7aadbf9SJohn Baldwin } 1295b7e23e82SJohn Baldwin if (options & WNOHANG) { 1296d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1297d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 1298d7aadbf9SJohn Baldwin return (0); 1299d7aadbf9SJohn Baldwin } 1300d7aadbf9SJohn Baldwin PROC_LOCK(q); 1301d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 130295992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 130395992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 130495992d56SDavid Xu error = 0; 130595992d56SDavid Xu } else 13067f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 1307d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 13084ae89b95SJohn Baldwin if (error) 1309d7aadbf9SJohn Baldwin return (error); 1310d7aadbf9SJohn Baldwin goto loop; 1311d7aadbf9SJohn Baldwin } 1312df8bae1dSRodney W. Grimes 1313df8bae1dSRodney W. Grimes /* 131498f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 131598f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 1316df8bae1dSRodney W. Grimes */ 1317df8bae1dSRodney W. Grimes void 1318830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 1319df8bae1dSRodney W. Grimes { 1320df8bae1dSRodney W. Grimes 13214e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 1322c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 1323df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 1324df8bae1dSRodney W. Grimes return; 1325df8bae1dSRodney W. Grimes 1326ff766807SDavid Xu PROC_LOCK(child->p_pptr); 1327ff766807SDavid Xu sigqueue_take(child->p_ksi); 1328ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 1329b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 1330b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1331dcd43281SKonstantin Belousov 13322e39e24fSKonstantin Belousov clear_orphan(child); 1333dcd43281SKonstantin Belousov if (child->p_flag & P_TRACED) { 1334d7359980SKonstantin Belousov if (LIST_EMPTY(&child->p_pptr->p_orphans)) { 1335d7359980SKonstantin Belousov child->p_treeflag |= P_TREE_FIRST_ORPHAN; 1336d7359980SKonstantin Belousov LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, 1337d7359980SKonstantin Belousov p_orphan); 1338d7359980SKonstantin Belousov } else { 1339ba78d6b7SMark Johnston LIST_INSERT_AFTER(LIST_FIRST(&child->p_pptr->p_orphans), 1340ba78d6b7SMark Johnston child, p_orphan); 1341d7359980SKonstantin Belousov } 1342d7359980SKonstantin Belousov child->p_treeflag |= P_TREE_ORPHANED; 1343dcd43281SKonstantin Belousov } 1344dcd43281SKonstantin Belousov 1345df8bae1dSRodney W. Grimes child->p_pptr = parent; 1346df8bae1dSRodney W. Grimes } 1347