19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 405591b823SEivind Eklund #include "opt_compat.h" 41db6a20e2SGarrett Wollman #include "opt_ktrace.h" 42db6a20e2SGarrett Wollman 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 455fdb8324SBruce Evans #include <sys/sysproto.h> 464a144410SRobert Watson #include <sys/capsicum.h> 4775b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 481c5bb3eaSPeter Wemm #include <sys/kernel.h> 49a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 50f34fa851SJohn Baldwin #include <sys/lock.h> 5135e0e5b3SJohn Baldwin #include <sys/mutex.h> 52df8bae1dSRodney W. Grimes #include <sys/proc.h> 53cfb5f768SJonathan Anderson #include <sys/procdesc.h> 542a024a2bSSean Eric Fagan #include <sys/pioctl.h> 55413628a7SBjoern A. Zeeb #include <sys/jail.h> 56df8bae1dSRodney W. Grimes #include <sys/tty.h> 57df8bae1dSRodney W. Grimes #include <sys/wait.h> 58eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 597a6b989bSJohn Baldwin #include <sys/vnode.h> 60097055e2SEdward Tomasz Napierala #include <sys/racct.h> 61df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 6225f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 63797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 64b43179fbSJeff Roberson #include <sys/sched.h> 651005a129SJohn Baldwin #include <sys/sx.h> 66c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6725f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 68df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 697c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 70797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 715d217f17SJohn Birrell #include <sys/sdt.h> 72780dc5a8SPeter Wemm #include <sys/shm.h> 73780dc5a8SPeter Wemm #include <sys/sem.h> 7413dad108SKonstantin Belousov #include <sys/umtx.h> 756c84de02SJohn Baldwin #ifdef KTRACE 766c84de02SJohn Baldwin #include <sys/ktrace.h> 776c84de02SJohn Baldwin #endif 78780dc5a8SPeter Wemm 79fcf7f27aSRobert Watson #include <security/audit/audit.h> 80aed55708SRobert Watson #include <security/mac/mac_framework.h> 81fcf7f27aSRobert Watson 82df8bae1dSRodney W. Grimes #include <vm/vm.h> 83eb30c1c0SPeter Wemm #include <vm/vm_extern.h> 847a6b989bSJohn Baldwin #include <vm/vm_param.h> 85efeaf95aSDavid Greenman #include <vm/pmap.h> 86efeaf95aSDavid Greenman #include <vm/vm_map.h> 872d21129dSAlan Cox #include <vm/vm_page.h> 88c897b813SJeff Roberson #include <vm/uma.h> 896520495aSAdrian Chadd #include <vm/vm_domain.h> 90df8bae1dSRodney W. Grimes 915d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 925d217f17SJohn Birrell #include <sys/dtrace_bsd.h> 935d217f17SJohn Birrell dtrace_execexit_func_t dtrace_fasttrap_exit; 945d217f17SJohn Birrell #endif 955d217f17SJohn Birrell 965d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 97d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(proc, kernel, , exit, "int"); 985d217f17SJohn Birrell 99c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 100c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 101c0bc2867SGleb Smirnoff 102d7359980SKonstantin Belousov struct proc * 103d7359980SKonstantin Belousov proc_realparent(struct proc *child) 104d7359980SKonstantin Belousov { 105d7359980SKonstantin Belousov struct proc *p, *parent; 106d7359980SKonstantin Belousov 107d7359980SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED); 108d7359980SKonstantin Belousov if ((child->p_treeflag & P_TREE_ORPHANED) == 0) { 1099152087eSMateusz Guzik if (child->p_oppid == 0 || 1109152087eSMateusz Guzik child->p_pptr->p_pid == child->p_oppid) 1119152087eSMateusz Guzik parent = child->p_pptr; 1129152087eSMateusz Guzik else 1139152087eSMateusz Guzik parent = initproc; 1149152087eSMateusz Guzik return (parent); 115d7359980SKonstantin Belousov } 116d7359980SKonstantin Belousov for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) { 117d7359980SKonstantin Belousov /* Cannot use LIST_PREV(), since the list head is not known. */ 118d7359980SKonstantin Belousov p = __containerof(p->p_orphan.le_prev, struct proc, 119d7359980SKonstantin Belousov p_orphan.le_next); 120d7359980SKonstantin Belousov KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0, 121d7359980SKonstantin Belousov ("missing P_ORPHAN %p", p)); 122d7359980SKonstantin Belousov } 123d7359980SKonstantin Belousov parent = __containerof(p->p_orphan.le_prev, struct proc, 124d7359980SKonstantin Belousov p_orphans.lh_first); 125d7359980SKonstantin Belousov return (parent); 126d7359980SKonstantin Belousov } 127d7359980SKonstantin Belousov 128237623b0SKonstantin Belousov void 129237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting) 130237623b0SKonstantin Belousov { 131237623b0SKonstantin Belousov struct proc *p1, *p2, *ptmp; 132237623b0SKonstantin Belousov 133237623b0SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED); 134237623b0SKonstantin Belousov KASSERT(p != initproc, ("reaper_abandon_children for initproc")); 135237623b0SKonstantin Belousov if ((p->p_treeflag & P_TREE_REAPER) == 0) 136237623b0SKonstantin Belousov return; 137237623b0SKonstantin Belousov p1 = p->p_reaper; 138237623b0SKonstantin Belousov LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) { 139237623b0SKonstantin Belousov LIST_REMOVE(p2, p_reapsibling); 140237623b0SKonstantin Belousov p2->p_reaper = p1; 141237623b0SKonstantin Belousov p2->p_reapsubtree = p->p_reapsubtree; 142237623b0SKonstantin Belousov LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling); 143237623b0SKonstantin Belousov if (exiting && p2->p_pptr == p) { 144237623b0SKonstantin Belousov PROC_LOCK(p2); 145237623b0SKonstantin Belousov proc_reparent(p2, p1); 146237623b0SKonstantin Belousov PROC_UNLOCK(p2); 147237623b0SKonstantin Belousov } 148237623b0SKonstantin Belousov } 149237623b0SKonstantin Belousov KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty")); 150237623b0SKonstantin Belousov p->p_treeflag &= ~P_TREE_REAPER; 151237623b0SKonstantin Belousov } 152237623b0SKonstantin Belousov 1532e39e24fSKonstantin Belousov static void 1542e39e24fSKonstantin Belousov clear_orphan(struct proc *p) 1552e39e24fSKonstantin Belousov { 156d7359980SKonstantin Belousov struct proc *p1; 1572e39e24fSKonstantin Belousov 158d7359980SKonstantin Belousov sx_assert(&proctree_lock, SA_XLOCKED); 159d7359980SKonstantin Belousov if ((p->p_treeflag & P_TREE_ORPHANED) == 0) 160d7359980SKonstantin Belousov return; 161d7359980SKonstantin Belousov if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) { 162d7359980SKonstantin Belousov p1 = LIST_NEXT(p, p_orphan); 163d7359980SKonstantin Belousov if (p1 != NULL) 164d7359980SKonstantin Belousov p1->p_treeflag |= P_TREE_FIRST_ORPHAN; 165d7359980SKonstantin Belousov p->p_treeflag &= ~P_TREE_FIRST_ORPHAN; 1662e39e24fSKonstantin Belousov } 167d7359980SKonstantin Belousov LIST_REMOVE(p, p_orphan); 168d7359980SKonstantin Belousov p->p_treeflag &= ~P_TREE_ORPHANED; 1692e39e24fSKonstantin Belousov } 1702e39e24fSKonstantin Belousov 171df8bae1dSRodney W. Grimes /* 172873fbcd7SRobert Watson * exit -- death of process. 173df8bae1dSRodney W. Grimes */ 174fc0b1dbfSBruce Evans void 1758451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 176df8bae1dSRodney W. Grimes { 177b40ce416SJulian Elischer 178b4490c6eSKonstantin Belousov exit1(td, uap->rval, 0); 179df8bae1dSRodney W. Grimes /* NOTREACHED */ 180df8bae1dSRodney W. Grimes } 181df8bae1dSRodney W. Grimes 182df8bae1dSRodney W. Grimes /* 183873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 184873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 185873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 186df8bae1dSRodney W. Grimes */ 187fc0b1dbfSBruce Evans void 188b4490c6eSKonstantin Belousov exit1(struct thread *td, int rval, int signo) 189df8bae1dSRodney W. Grimes { 190a661bebeSMateusz Guzik struct proc *p, *nq, *q, *t; 191a661bebeSMateusz Guzik struct thread *tdt; 192bc093719SEd Schouten struct vnode *ttyvp = NULL; 193df8bae1dSRodney W. Grimes 194e17660e7SKris Kennaway mtx_assert(&Giant, MA_NOTOWNED); 195b4490c6eSKonstantin Belousov KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo)); 1960cddd8f0SMatthew Dillon 197276c5169SJohn Baldwin p = td->td_proc; 1985486ffc8SMarius Strobl /* 1995486ffc8SMarius Strobl * XXX in case we're rebooting we just let init die in order to 2005486ffc8SMarius Strobl * work around an unsolved stack overflow seen very late during 2015486ffc8SMarius Strobl * shutdown on sparc64 when the gmirror worker process exists. 2025486ffc8SMarius Strobl */ 2035486ffc8SMarius Strobl if (p == initproc && rebooting == 0) { 204b4490c6eSKonstantin Belousov printf("init died (signal %d, exit %d)\n", signo, rval); 2055f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 2065f7bd355SPoul-Henning Kamp } 2072c1011f7SJohn Dyson 2087a6b989bSJohn Baldwin /* 2091bc93bb7SKonstantin Belousov * Deref SU mp, since the thread does not return to userspace. 2101bc93bb7SKonstantin Belousov */ 2111bc93bb7SKonstantin Belousov if (softdep_ast_cleanup != NULL) 2121bc93bb7SKonstantin Belousov softdep_ast_cleanup(); 2131bc93bb7SKonstantin Belousov 2141bc93bb7SKonstantin Belousov /* 2152c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 2167a6b989bSJohn Baldwin */ 217e602ba25SJulian Elischer PROC_LOCK(p); 218e602ba25SJulian Elischer /* 219aba1ca52SKonstantin Belousov * First check if some other thread or external request got 220aba1ca52SKonstantin Belousov * here before us. If so, act appropriately: exit or suspend. 221aba1ca52SKonstantin Belousov * We must ensure that stop requests are handled before we set 222aba1ca52SKonstantin Belousov * P_WEXIT. 223e602ba25SJulian Elischer */ 224e602ba25SJulian Elischer thread_suspend_check(0); 225aba1ca52SKonstantin Belousov while (p->p_flag & P_HADTHREADS) { 226e602ba25SJulian Elischer /* 227e602ba25SJulian Elischer * Kill off the other threads. This requires 2286111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 2296111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 2306111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 231e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 2326111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 2336111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 2346111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 235b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 2366111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 2376111dcd2SJohn Baldwin * other threads exits. 238b370279eSDavid Xu * If there is already a thread singler after resumption, 2396111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 240b370279eSDavid Xu * re-check all suspension request, the thread should 241b370279eSDavid Xu * either be suspended there or exit. 242e602ba25SJulian Elischer */ 2436ddcc233SKonstantin Belousov if (!thread_single(p, SINGLE_EXIT)) 244e602ba25SJulian Elischer /* 245aba1ca52SKonstantin Belousov * All other activity in this process is now 246aba1ca52SKonstantin Belousov * stopped. Threading support has been turned 247aba1ca52SKonstantin Belousov * off. 248e602ba25SJulian Elischer */ 249aba1ca52SKonstantin Belousov break; 250aba1ca52SKonstantin Belousov /* 251aba1ca52SKonstantin Belousov * Recheck for new stop or suspend requests which 252aba1ca52SKonstantin Belousov * might appear while process lock was dropped in 253aba1ca52SKonstantin Belousov * thread_single(). 254aba1ca52SKonstantin Belousov */ 255aba1ca52SKonstantin Belousov thread_suspend_check(0); 256e602ba25SJulian Elischer } 2571c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 2581c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 25958c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 260b4490c6eSKonstantin Belousov 261b4490c6eSKonstantin Belousov /* Let event handler change exit status */ 262b4490c6eSKonstantin Belousov p->p_xexit = rval; 263b4490c6eSKonstantin Belousov p->p_xsig = signo; 264b4490c6eSKonstantin Belousov 26506ad42b2SJohn Baldwin /* 26606ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 26706ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 26806ad42b2SJohn Baldwin * released their reference to us. Note that if they have 26906ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 27006ad42b2SJohn Baldwin * via PIOCCONT. 27106ad42b2SJohn Baldwin */ 272b4490c6eSKonstantin Belousov _STOPEVENT(p, S_EXIT, 0); 27306ad42b2SJohn Baldwin 27406ad42b2SJohn Baldwin /* 2750f14f15bSJohn Baldwin * Ignore any pending request to stop due to a stop signal. 2760f14f15bSJohn Baldwin * Once P_WEXIT is set, future requests will be ignored as 2770f14f15bSJohn Baldwin * well. 2780f14f15bSJohn Baldwin */ 2790f14f15bSJohn Baldwin p->p_flag &= ~P_STOPPED_SIG; 2800f14f15bSJohn Baldwin KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 2810f14f15bSJohn Baldwin 2820f14f15bSJohn Baldwin /* 28306ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 28406ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 28506ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 28606ad42b2SJohn Baldwin */ 287e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 28806ad42b2SJohn Baldwin wakeup(&p->p_stype); 28906ad42b2SJohn Baldwin 29006ad42b2SJohn Baldwin /* 29106ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 29206ad42b2SJohn Baldwin * release their reference. 29306ad42b2SJohn Baldwin */ 29406ad42b2SJohn Baldwin while (p->p_lock > 0) 29506ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 296ebceaf6dSDavid Xu 297e602ba25SJulian Elischer PROC_UNLOCK(p); 2981c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 2991c4bcd05SJeff Roberson callout_drain(&p->p_limco); 300b40ce416SJulian Elischer 30100c28d96SRobert Watson #ifdef AUDIT 30200c28d96SRobert Watson /* 30300c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 30400c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 30500c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 30600c28d96SRobert Watson * what the return value is. 30700c28d96SRobert Watson */ 308b4490c6eSKonstantin Belousov AUDIT_ARG_EXIT(rval, 0); 30900c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 31000c28d96SRobert Watson #endif 31100c28d96SRobert Watson 312bad2520aSMateusz Guzik /* Are we a task leader with peers? */ 313bad2520aSMateusz Guzik if (p->p_peers != NULL && p == p->p_leader) { 314c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 3152c1011f7SJohn Dyson q = p->p_peers; 316776e0b36SJohn Baldwin while (q != NULL) { 317776e0b36SJohn Baldwin PROC_LOCK(q); 3188451d0ddSKip Macy kern_psignal(q, SIGKILL); 319776e0b36SJohn Baldwin PROC_UNLOCK(q); 3202c1011f7SJohn Dyson q = q->p_peers; 3212c1011f7SJohn Dyson } 322c6544064SJohn Baldwin while (p->p_peers != NULL) 323c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 324c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 3252c1011f7SJohn Dyson } 3262c1011f7SJohn Dyson 327fed06968SJulian Elischer /* 328e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 329b4490c6eSKonstantin Belousov * E.g. SYSV IPC stuff. 330b4490c6eSKonstantin Belousov * Event handler could change exit status. 331fed06968SJulian Elischer * XXX what if one of these generates an error? 332fed06968SJulian Elischer */ 33375b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exit, p); 334a914fb6bSJohn Baldwin 335df8bae1dSRodney W. Grimes /* 336df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 337df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 338df8bae1dSRodney W. Grimes */ 339a914fb6bSJohn Baldwin PROC_LOCK(p); 340a282253aSJulian Elischer stopprofclock(p); 341888d4d4fSKonstantin Belousov p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 3425499ea01SJohn Baldwin 3435499ea01SJohn Baldwin /* 3445499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 3455499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 3465499ea01SJohn Baldwin */ 3475499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 3485499ea01SJohn Baldwin callout_stop(&p->p_itcallout) == 0) { 3495499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 3505499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 3515499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 3525499ea01SJohn Baldwin ("realtime timer is still armed")); 3535499ea01SJohn Baldwin } 35496d7f8efSTim J. Robbins PROC_UNLOCK(p); 355df8bae1dSRodney W. Grimes 356df8bae1dSRodney W. Grimes /* 357831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 358831d27a9SDon Lewis * F_SETOWN with our pid. 359831d27a9SDon Lewis */ 360831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 361831d27a9SDon Lewis 362831d27a9SDon Lewis /* 363c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 364c0bc2867SGleb Smirnoff */ 365c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 366c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 367c0bc2867SGleb Smirnoff 368c0bc2867SGleb Smirnoff /* 369df8bae1dSRodney W. Grimes * Close open files and release open-file table. 370df8bae1dSRodney W. Grimes * This may block! 371df8bae1dSRodney W. Grimes */ 3722609222aSPawel Jakub Dawidek fdescfree(td); 373df8bae1dSRodney W. Grimes 374a914fb6bSJohn Baldwin /* 375a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 376a2587073SPoul-Henning Kamp * stop before we return to userland 377a2587073SPoul-Henning Kamp */ 378a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 379a2587073SPoul-Henning Kamp g_waitidle(); 380a2587073SPoul-Henning Kamp 381a2587073SPoul-Henning Kamp /* 382a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 383a914fb6bSJohn Baldwin */ 384bad2520aSMateusz Guzik if (p->p_leader->p_peers != NULL) { 385c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 386bad2520aSMateusz Guzik if (p->p_leader->p_peers != NULL) { 38779fc0bf4SMike Smith q = p->p_leader; 38879fc0bf4SMike Smith while (q->p_peers != p) 38979fc0bf4SMike Smith q = q->p_peers; 39079fc0bf4SMike Smith q->p_peers = p->p_peers; 3917f05b035SAlfred Perlstein wakeup(p->p_leader); 39279fc0bf4SMike Smith } 393c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 394bad2520aSMateusz Guzik } 39579fc0bf4SMike Smith 39657051fdcSTor Egge vmspace_exit(td); 397df8bae1dSRodney W. Grimes 398ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 399df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 400eaaaf190SEd Schouten struct session *sp = p->p_session; 401eaaaf190SEd Schouten struct tty *tp; 402df8bae1dSRodney W. Grimes 403eaaaf190SEd Schouten /* 404eaaaf190SEd Schouten * s_ttyp is not zero'd; we use this to indicate that 405eaaaf190SEd Schouten * the session once had a controlling terminal. (for 406eaaaf190SEd Schouten * logging and informational purposes) 407eaaaf190SEd Schouten */ 408bc093719SEd Schouten SESS_LOCK(sp); 409bc093719SEd Schouten ttyvp = sp->s_ttyvp; 410eaaaf190SEd Schouten tp = sp->s_ttyp; 411bc093719SEd Schouten sp->s_ttyvp = NULL; 4128dc9b4cfSEd Schouten sp->s_ttydp = NULL; 413eaaaf190SEd Schouten sp->s_leader = NULL; 414bc093719SEd Schouten SESS_UNLOCK(sp); 415bc093719SEd Schouten 416df8bae1dSRodney W. Grimes /* 417bc093719SEd Schouten * Signal foreground pgrp and revoke access to 418eaaaf190SEd Schouten * controlling terminal if it has not been revoked 419eaaaf190SEd Schouten * already. 420bc093719SEd Schouten * 421eaaaf190SEd Schouten * Because the TTY may have been revoked in the mean 422eaaaf190SEd Schouten * time and could already have a new session associated 423eaaaf190SEd Schouten * with it, make sure we don't send a SIGHUP to a 424eaaaf190SEd Schouten * foreground process group that does not belong to this 425eaaaf190SEd Schouten * session. 426df8bae1dSRodney W. Grimes */ 427bc093719SEd Schouten 428eaaaf190SEd Schouten if (tp != NULL) { 429bc093719SEd Schouten tty_lock(tp); 430eaaaf190SEd Schouten if (tp->t_session == sp) 431bc093719SEd Schouten tty_signal_pgrp(tp, SIGHUP); 432bc093719SEd Schouten tty_unlock(tp); 433eaaaf190SEd Schouten } 434bc093719SEd Schouten 4357c6d401cSKonstantin Belousov if (ttyvp != NULL) { 436ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 4376728dc16SKonstantin Belousov if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) { 438f591779bSSeigo Tanimura VOP_REVOKE(ttyvp, REVOKEALL); 439bc093719SEd Schouten VOP_UNLOCK(ttyvp, 0); 4406728dc16SKonstantin Belousov } 441ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 442f591779bSSeigo Tanimura } 443f591779bSSeigo Tanimura } 444df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 445ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 446b40ce416SJulian Elischer (void)acct_process(td); 447bc093719SEd Schouten 448bc093719SEd Schouten /* Release the TTY now we've unlocked everything. */ 449bc093719SEd Schouten if (ttyvp != NULL) 450bc093719SEd Schouten vrele(ttyvp); 451df8bae1dSRodney W. Grimes #ifdef KTRACE 4522c255e9dSRobert Watson ktrprocexit(td); 453df8bae1dSRodney W. Grimes #endif 454df8bae1dSRodney W. Grimes /* 455ee42d0a9SDavid Malone * Release reference to text vnode 456ee42d0a9SDavid Malone */ 45788f98985SMateusz Guzik if (p->p_textvp != NULL) { 45888f98985SMateusz Guzik vrele(p->p_textvp); 459ee42d0a9SDavid Malone p->p_textvp = NULL; 460ee42d0a9SDavid Malone } 461ee42d0a9SDavid Malone 462ee42d0a9SDavid Malone /* 463d7aadbf9SJohn Baldwin * Release our limits structure. 464d7aadbf9SJohn Baldwin */ 46588f98985SMateusz Guzik lim_free(p->p_limit); 466d7aadbf9SJohn Baldwin p->p_limit = NULL; 467d7aadbf9SJohn Baldwin 468cf7d9a8cSDavid Xu tidhash_remove(td); 469cf7d9a8cSDavid Xu 470d7aadbf9SJohn Baldwin /* 471df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 472df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 473df8bae1dSRodney W. Grimes */ 4741005a129SJohn Baldwin sx_xlock(&allproc_lock); 475b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 476b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 477b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4781005a129SJohn Baldwin sx_xunlock(&allproc_lock); 479df8bae1dSRodney W. Grimes 48037f84a60SJohn Baldwin /* 4811bba2a94SJohn Baldwin * Call machine-dependent code to release any 4821bba2a94SJohn Baldwin * machine-dependent resources other than the address space. 4831bba2a94SJohn Baldwin * The address space is released by "vmspace_exitfree(p)" in 4841bba2a94SJohn Baldwin * vm_waitproc(). 4851bba2a94SJohn Baldwin */ 4861bba2a94SJohn Baldwin cpu_exit(td); 4871bba2a94SJohn Baldwin 4881bba2a94SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 4891bba2a94SJohn Baldwin 4901bba2a94SJohn Baldwin /* 491a661bebeSMateusz Guzik * Reparent all children processes: 492a661bebeSMateusz Guzik * - traced ones to the original parent (or init if we are that parent) 493a661bebeSMateusz Guzik * - the rest to init 49437f84a60SJohn Baldwin */ 4951005a129SJohn Baldwin sx_xlock(&proctree_lock); 4962e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 497a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 498237623b0SKonstantin Belousov wakeup(q->p_reaper); 499a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 5002e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 501a914fb6bSJohn Baldwin PROC_LOCK(q); 5024ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 503904c5ec4SDavid Xu 504a661bebeSMateusz Guzik if (!(q->p_flag & P_TRACED)) { 505237623b0SKonstantin Belousov proc_reparent(q, q->p_reaper); 506a661bebeSMateusz Guzik } else { 507a661bebeSMateusz Guzik /* 508a661bebeSMateusz Guzik * Traced processes are killed since their existence 509a661bebeSMateusz Guzik * means someone is screwing up. 510a661bebeSMateusz Guzik */ 511a661bebeSMateusz Guzik t = proc_realparent(q); 512a661bebeSMateusz Guzik if (t == p) { 513237623b0SKonstantin Belousov proc_reparent(q, q->p_reaper); 514a661bebeSMateusz Guzik } else { 515a661bebeSMateusz Guzik PROC_LOCK(t); 516a661bebeSMateusz Guzik proc_reparent(q, t); 517a661bebeSMateusz Guzik PROC_UNLOCK(t); 518a661bebeSMateusz Guzik } 5195085ecb7SKonstantin Belousov /* 5205085ecb7SKonstantin Belousov * Since q was found on our children list, the 5215085ecb7SKonstantin Belousov * proc_reparent() call moved q to the orphan 5225085ecb7SKonstantin Belousov * list due to present P_TRACED flag. Clear 5235085ecb7SKonstantin Belousov * orphan link for q now while q is locked. 5245085ecb7SKonstantin Belousov */ 5255085ecb7SKonstantin Belousov clear_orphan(q); 526c2836532SDavid Xu q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 527a661bebeSMateusz Guzik FOREACH_THREAD_IN_PROC(q, tdt) 528a661bebeSMateusz Guzik tdt->td_dbgflags &= ~TDB_SUSPEND; 5298451d0ddSKip Macy kern_psignal(q, SIGKILL); 530c65437a3SJohn Baldwin } 531a914fb6bSJohn Baldwin PROC_UNLOCK(q); 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes 5345085ecb7SKonstantin Belousov /* 5355085ecb7SKonstantin Belousov * Also get rid of our orphans. 5365085ecb7SKonstantin Belousov */ 5375085ecb7SKonstantin Belousov while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 5385085ecb7SKonstantin Belousov PROC_LOCK(q); 539515b7a0bSJohn Baldwin CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid, 540515b7a0bSJohn Baldwin q->p_pid); 5415085ecb7SKonstantin Belousov clear_orphan(q); 5425085ecb7SKonstantin Belousov PROC_UNLOCK(q); 5435085ecb7SKonstantin Belousov } 5445085ecb7SKonstantin Belousov 5451c4bcd05SJeff Roberson /* Save exit status. */ 546d7aadbf9SJohn Baldwin PROC_LOCK(p); 547cbf4e354SDavid Xu p->p_xthread = td; 5485d217f17SJohn Birrell 5490304c731SJamie Gritton /* Tell the prison that we are gone. */ 550413628a7SBjoern A. Zeeb prison_proc_free(p->p_ucred->cr_prison); 551413628a7SBjoern A. Zeeb 5525d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 5535d217f17SJohn Birrell /* 5545d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 5555d217f17SJohn Birrell * has declared an interest. 5565d217f17SJohn Birrell */ 5575d217f17SJohn Birrell if (dtrace_fasttrap_exit) 5585d217f17SJohn Birrell dtrace_fasttrap_exit(p); 5595d217f17SJohn Birrell #endif 5605d217f17SJohn Birrell 5611c4bcd05SJeff Roberson /* 5627a6b989bSJohn Baldwin * Notify interested parties of our demise. 563cb679c38SJonathan Lemon */ 564ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 5657eaec467SJohn Baldwin 5665d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 5675d217f17SJohn Birrell int reason = CLD_EXITED; 568b4490c6eSKonstantin Belousov if (WCOREDUMP(signo)) 5695d217f17SJohn Birrell reason = CLD_DUMPED; 570b4490c6eSKonstantin Belousov else if (WIFSIGNALED(signo)) 5715d217f17SJohn Birrell reason = CLD_KILLED; 5725d217f17SJohn Birrell SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 5735d217f17SJohn Birrell #endif 5745d217f17SJohn Birrell 5751a29c806SOlivier Houchard /* 5761a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 5771a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 5781a29c806SOlivier Houchard * can beat us if we don't. 5791a29c806SOlivier Houchard */ 580ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 581cb679c38SJonathan Lemon 582cb679c38SJonathan Lemon /* 583cfb5f768SJonathan Anderson * If this is a process with a descriptor, we may not need to deliver 584cfb5f768SJonathan Anderson * a signal to the parent. proctree_lock is held over 585cfb5f768SJonathan Anderson * procdesc_exit() to serialize concurrent calls to close() and 586cfb5f768SJonathan Anderson * exit(). 587cfb5f768SJonathan Anderson */ 588cfb5f768SJonathan Anderson if (p->p_procdesc == NULL || procdesc_exit(p)) { 589cfb5f768SJonathan Anderson /* 590cfb5f768SJonathan Anderson * Notify parent that we're gone. If parent has the 591cfb5f768SJonathan Anderson * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 592cfb5f768SJonathan Anderson * notify process 1 instead (and hope it will handle this 593cfb5f768SJonathan Anderson * situation). 594df8bae1dSRodney W. Grimes */ 595d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 59690af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 597cfb5f768SJonathan Anderson if (p->p_pptr->p_sigacts->ps_flag & 598cfb5f768SJonathan Anderson (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 599276c5169SJohn Baldwin struct proc *pp; 600276c5169SJohn Baldwin 60190af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 602276c5169SJohn Baldwin pp = p->p_pptr; 603f591779bSSeigo Tanimura PROC_UNLOCK(pp); 604237623b0SKonstantin Belousov proc_reparent(p, p->p_reaper); 60555b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 606f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 607007abb3dSKonstantin Belousov 608245f17d4SJoerg Wunsch /* 609007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 6106fae832aSKonstantin Belousov * executing waitpid(2) with our pid, he will 611245f17d4SJoerg Wunsch * continue. 612245f17d4SJoerg Wunsch */ 6137f05b035SAlfred Perlstein wakeup(pp); 61490af4afaSJohn Baldwin } else 61590af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 616245f17d4SJoerg Wunsch 617237623b0SKonstantin Belousov if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) 618237623b0SKonstantin Belousov childproc_exited(p); 619ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 620ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 621ebceaf6dSDavid Xu childproc_exited(p); 622ebceaf6dSDavid Xu else /* LINUX thread */ 6238451d0ddSKip Macy kern_psignal(p->p_pptr, p->p_sigparent); 624ebceaf6dSDavid Xu } 625cfb5f768SJonathan Anderson } else 626cfb5f768SJonathan Anderson PROC_LOCK(p->p_pptr); 627d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 628696058c3SJulian Elischer 629eb30c1c0SPeter Wemm /* 630f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 631f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 632f71e748dSDavid Xu * for signal queue at the time when the state is set. 633f71e748dSDavid Xu */ 634f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 635f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 636f71e748dSDavid Xu 637f71e748dSDavid Xu /* 638462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 639a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 640a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 641ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 642ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 643ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 644ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 645ddf9c4f7SJohn Baldwin * proc lock. 646eb30c1c0SPeter Wemm */ 647ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 648aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 649982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 65013dad108SKonstantin Belousov umtx_thread_exit(td); 651982d11f8SJeff Roberson PROC_SLOCK(p); 652e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 653d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 654871684b8SBruce Evans 655f6f230feSJeff Roberson /* 6567eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 657ad3b9257SJohn-Mark Gurney * late in the game. 658ad3b9257SJohn-Mark Gurney */ 659ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 660ad3b9257SJohn-Mark Gurney 661ad3b9257SJohn-Mark Gurney /* 662a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 663a140976eSAttilio Rao */ 6645c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 665a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 6665c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 667a140976eSAttilio Rao 668a140976eSAttilio Rao /* 669696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 670e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 671696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 672e602ba25SJulian Elischer */ 673e602ba25SJulian Elischer thread_exit(); 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes 67625f6e35aSPoul-Henning Kamp 67725f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 67825f6e35aSPoul-Henning Kamp struct abort2_args { 67925f6e35aSPoul-Henning Kamp char *why; 68025f6e35aSPoul-Henning Kamp int nargs; 68125f6e35aSPoul-Henning Kamp void **args; 68225f6e35aSPoul-Henning Kamp }; 68325f6e35aSPoul-Henning Kamp #endif 68425f6e35aSPoul-Henning Kamp 68525f6e35aSPoul-Henning Kamp int 6868451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap) 68725f6e35aSPoul-Henning Kamp { 68825f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 68925f6e35aSPoul-Henning Kamp struct sbuf *sb; 69025f6e35aSPoul-Henning Kamp void *uargs[16]; 69125f6e35aSPoul-Henning Kamp int error, i, sig; 69225f6e35aSPoul-Henning Kamp 69325f6e35aSPoul-Henning Kamp /* 69425f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 69525f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 69625f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 69725f6e35aSPoul-Henning Kamp */ 69825f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 69925f6e35aSPoul-Henning Kamp sbuf_clear(sb); 70025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 70125f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 70225f6e35aSPoul-Henning Kamp /* 70325f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 70425f6e35aSPoul-Henning Kamp * abort2() was called improperly 70525f6e35aSPoul-Henning Kamp */ 70625f6e35aSPoul-Henning Kamp sig = SIGKILL; 70725f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 70825f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 70925f6e35aSPoul-Henning Kamp goto out; 7104218a731SPoul-Henning Kamp if (uap->nargs > 0) { 71125f6e35aSPoul-Henning Kamp if (uap->args == NULL) 71225f6e35aSPoul-Henning Kamp goto out; 71325f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 71425f6e35aSPoul-Henning Kamp if (error != 0) 71525f6e35aSPoul-Henning Kamp goto out; 7164218a731SPoul-Henning Kamp } 71725f6e35aSPoul-Henning Kamp /* 71825f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 71925f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 72025f6e35aSPoul-Henning Kamp */ 72125f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 72225f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 72325f6e35aSPoul-Henning Kamp if (error < 0) 72425f6e35aSPoul-Henning Kamp goto out; 72525f6e35aSPoul-Henning Kamp } else { 72625f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 72725f6e35aSPoul-Henning Kamp } 7284218a731SPoul-Henning Kamp if (uap->nargs > 0) { 72925f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 73025f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 73125f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 73225f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 73325f6e35aSPoul-Henning Kamp } 73425f6e35aSPoul-Henning Kamp /* 73525f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 73625f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 73725f6e35aSPoul-Henning Kamp * from user-space succeed. 73825f6e35aSPoul-Henning Kamp */ 73925f6e35aSPoul-Henning Kamp sig = SIGABRT; 74025f6e35aSPoul-Henning Kamp out: 74125f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 74225f6e35aSPoul-Henning Kamp sbuf_trim(sb); 74325f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 74425f6e35aSPoul-Henning Kamp } 74525f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 74625f6e35aSPoul-Henning Kamp sbuf_finish(sb); 74725f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 74825f6e35aSPoul-Henning Kamp sbuf_delete(sb); 749b4490c6eSKonstantin Belousov exit1(td, 0, sig); 75025f6e35aSPoul-Henning Kamp return (0); 75125f6e35aSPoul-Henning Kamp } 75225f6e35aSPoul-Henning Kamp 75325f6e35aSPoul-Henning Kamp 754b2f9e8b1SBruce Evans #ifdef COMPAT_43 755234216efSMatthew Dillon /* 756a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 757234216efSMatthew Dillon */ 75826f9a767SRodney W. Grimes int 759830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 760df8bae1dSRodney W. Grimes { 761b7e23e82SJohn Baldwin int error, status; 762df8bae1dSRodney W. Grimes 763b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 764b7e23e82SJohn Baldwin if (error == 0) 765b7e23e82SJohn Baldwin td->td_retval[1] = status; 766b7e23e82SJohn Baldwin return (error); 767df8bae1dSRodney W. Grimes } 768b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 769df8bae1dSRodney W. Grimes 770234216efSMatthew Dillon /* 771a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 772234216efSMatthew Dillon */ 77326f9a767SRodney W. Grimes int 774f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap) 775df8bae1dSRodney W. Grimes { 77678c85e8dSJohn Baldwin struct rusage ru, *rup; 777b7e23e82SJohn Baldwin int error, status; 778b40ce416SJulian Elischer 77978c85e8dSJohn Baldwin if (uap->rusage != NULL) 78078c85e8dSJohn Baldwin rup = &ru; 78178c85e8dSJohn Baldwin else 78278c85e8dSJohn Baldwin rup = NULL; 78378c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 784b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 785b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 786b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 787b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 788b7e23e82SJohn Baldwin return (error); 789df8bae1dSRodney W. Grimes } 790df8bae1dSRodney W. Grimes 791f13b5a0fSKonstantin Belousov int 792f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap) 793f13b5a0fSKonstantin Belousov { 794f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 795f13b5a0fSKonstantin Belousov siginfo_t si, *sip; 796f13b5a0fSKonstantin Belousov idtype_t idtype; 797f13b5a0fSKonstantin Belousov id_t id; 798a2a85596SKonstantin Belousov int error, status; 799f13b5a0fSKonstantin Belousov 800f13b5a0fSKonstantin Belousov idtype = uap->idtype; 801f13b5a0fSKonstantin Belousov id = uap->id; 802f13b5a0fSKonstantin Belousov 803f13b5a0fSKonstantin Belousov if (uap->wrusage != NULL) 804f13b5a0fSKonstantin Belousov wrup = &wru; 805f13b5a0fSKonstantin Belousov else 806f13b5a0fSKonstantin Belousov wrup = NULL; 807f13b5a0fSKonstantin Belousov 808f13b5a0fSKonstantin Belousov if (uap->info != NULL) { 809f13b5a0fSKonstantin Belousov sip = &si; 810f13b5a0fSKonstantin Belousov bzero(sip, sizeof(*sip)); 811f13b5a0fSKonstantin Belousov } else 812f13b5a0fSKonstantin Belousov sip = NULL; 813f13b5a0fSKonstantin Belousov 814f13b5a0fSKonstantin Belousov /* 815f13b5a0fSKonstantin Belousov * We expect all callers of wait6() to know about WEXITED and 816f13b5a0fSKonstantin Belousov * WTRAPPED. 817f13b5a0fSKonstantin Belousov */ 818f13b5a0fSKonstantin Belousov error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip); 819f13b5a0fSKonstantin Belousov 820f13b5a0fSKonstantin Belousov if (uap->status != NULL && error == 0) 821f13b5a0fSKonstantin Belousov error = copyout(&status, uap->status, sizeof(status)); 822f13b5a0fSKonstantin Belousov if (uap->wrusage != NULL && error == 0) 823f13b5a0fSKonstantin Belousov error = copyout(&wru, uap->wrusage, sizeof(wru)); 824f13b5a0fSKonstantin Belousov if (uap->info != NULL && error == 0) 825f13b5a0fSKonstantin Belousov error = copyout(&si, uap->info, sizeof(si)); 826f13b5a0fSKonstantin Belousov return (error); 827f13b5a0fSKonstantin Belousov } 828f13b5a0fSKonstantin Belousov 829324fb6beSRobert Watson /* 830324fb6beSRobert Watson * Reap the remains of a zombie process and optionally return status and 831324fb6beSRobert Watson * rusage. Asserts and will release both the proctree_lock and the process 832324fb6beSRobert Watson * lock as part of its work. 833324fb6beSRobert Watson */ 834cfb5f768SJonathan Anderson void 835f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options) 836324fb6beSRobert Watson { 837324fb6beSRobert Watson struct proc *q, *t; 838324fb6beSRobert Watson 839324fb6beSRobert Watson sx_assert(&proctree_lock, SA_XLOCKED); 840324fb6beSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 841324fb6beSRobert Watson PROC_SLOCK_ASSERT(p, MA_OWNED); 842324fb6beSRobert Watson KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 843324fb6beSRobert Watson 844324fb6beSRobert Watson q = td->td_proc; 845f13b5a0fSKonstantin Belousov 846324fb6beSRobert Watson PROC_SUNLOCK(p); 847324fb6beSRobert Watson if (status) 848b4490c6eSKonstantin Belousov *status = KW_EXITCODE(p->p_xexit, p->p_xsig); 849324fb6beSRobert Watson if (options & WNOWAIT) { 850324fb6beSRobert Watson /* 851324fb6beSRobert Watson * Only poll, returning the status. Caller does not wish to 852324fb6beSRobert Watson * release the proc struct just yet. 853324fb6beSRobert Watson */ 854324fb6beSRobert Watson PROC_UNLOCK(p); 855324fb6beSRobert Watson sx_xunlock(&proctree_lock); 856324fb6beSRobert Watson return; 857324fb6beSRobert Watson } 858324fb6beSRobert Watson 859324fb6beSRobert Watson PROC_LOCK(q); 860324fb6beSRobert Watson sigqueue_take(p->p_ksi); 861324fb6beSRobert Watson PROC_UNLOCK(q); 862324fb6beSRobert Watson 863324fb6beSRobert Watson /* 864324fb6beSRobert Watson * If we got the child via a ptrace 'attach', we need to give it back 865324fb6beSRobert Watson * to the old parent. 866324fb6beSRobert Watson */ 867c209e3e2SJohn Baldwin if (p->p_oppid != 0 && p->p_oppid != p->p_pptr->p_pid) { 868c209e3e2SJohn Baldwin PROC_UNLOCK(p); 869d7359980SKonstantin Belousov t = proc_realparent(p); 870d7359980SKonstantin Belousov PROC_LOCK(t); 871324fb6beSRobert Watson PROC_LOCK(p); 872515b7a0bSJohn Baldwin CTR2(KTR_PTRACE, 873515b7a0bSJohn Baldwin "wait: traced child %d moved back to parent %d", p->p_pid, 874515b7a0bSJohn Baldwin t->p_pid); 875324fb6beSRobert Watson proc_reparent(p, t); 876f528c3fdSDavid E. O'Brien p->p_oppid = 0; 877324fb6beSRobert Watson PROC_UNLOCK(p); 878ad6eec7bSJohn Baldwin pksignal(t, SIGCHLD, p->p_ksi); 879324fb6beSRobert Watson wakeup(t); 880324fb6beSRobert Watson cv_broadcast(&p->p_pwait); 881324fb6beSRobert Watson PROC_UNLOCK(t); 882324fb6beSRobert Watson sx_xunlock(&proctree_lock); 883324fb6beSRobert Watson return; 884324fb6beSRobert Watson } 885c209e3e2SJohn Baldwin p->p_oppid = 0; 886c209e3e2SJohn Baldwin PROC_UNLOCK(p); 887324fb6beSRobert Watson 888324fb6beSRobert Watson /* 889324fb6beSRobert Watson * Remove other references to this process to ensure we have an 890324fb6beSRobert Watson * exclusive reference. 891324fb6beSRobert Watson */ 892324fb6beSRobert Watson sx_xlock(&allproc_lock); 893324fb6beSRobert Watson LIST_REMOVE(p, p_list); /* off zombproc */ 894324fb6beSRobert Watson sx_xunlock(&allproc_lock); 895324fb6beSRobert Watson LIST_REMOVE(p, p_sibling); 896237623b0SKonstantin Belousov reaper_abandon_children(p, true); 897237623b0SKonstantin Belousov LIST_REMOVE(p, p_reapsibling); 8987335ed90SKonstantin Belousov PROC_LOCK(p); 8992e39e24fSKonstantin Belousov clear_orphan(p); 9007335ed90SKonstantin Belousov PROC_UNLOCK(p); 901324fb6beSRobert Watson leavepgrp(p); 902cfb5f768SJonathan Anderson if (p->p_procdesc != NULL) 903cfb5f768SJonathan Anderson procdesc_reap(p); 904324fb6beSRobert Watson sx_xunlock(&proctree_lock); 905324fb6beSRobert Watson 906324fb6beSRobert Watson /* 907fbe503d4SMateusz Guzik * Removal from allproc list and process group list paired with 908fbe503d4SMateusz Guzik * PROC_LOCK which was executed during that time should guarantee 909fbe503d4SMateusz Guzik * nothing can reach this process anymore. As such further locking 910fbe503d4SMateusz Guzik * is unnecessary. 911324fb6beSRobert Watson */ 912b4490c6eSKonstantin Belousov p->p_xexit = p->p_xsig = 0; /* XXX: why? */ 913fbe503d4SMateusz Guzik 914324fb6beSRobert Watson PROC_LOCK(q); 915324fb6beSRobert Watson ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 916324fb6beSRobert Watson PROC_UNLOCK(q); 917324fb6beSRobert Watson 918324fb6beSRobert Watson /* 919324fb6beSRobert Watson * Decrement the count of procs running with this uid. 920324fb6beSRobert Watson */ 921324fb6beSRobert Watson (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 922324fb6beSRobert Watson 923324fb6beSRobert Watson /* 924097055e2SEdward Tomasz Napierala * Destroy resource accounting information associated with the process. 925097055e2SEdward Tomasz Napierala */ 926afcc55f3SEdward Tomasz Napierala #ifdef RACCT 9274b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 928b38520f0SEdward Tomasz Napierala PROC_LOCK(p); 929b38520f0SEdward Tomasz Napierala racct_sub(p, RACCT_NPROC, 1); 930b38520f0SEdward Tomasz Napierala PROC_UNLOCK(p); 9314b5c9cf6SEdward Tomasz Napierala } 932afcc55f3SEdward Tomasz Napierala #endif 933b38520f0SEdward Tomasz Napierala racct_proc_exit(p); 934097055e2SEdward Tomasz Napierala 935097055e2SEdward Tomasz Napierala /* 936324fb6beSRobert Watson * Free credentials, arguments, and sigacts. 937324fb6beSRobert Watson */ 938324fb6beSRobert Watson crfree(p->p_ucred); 939daf63fd2SMateusz Guzik proc_set_cred(p, NULL); 940324fb6beSRobert Watson pargs_drop(p->p_args); 941324fb6beSRobert Watson p->p_args = NULL; 942324fb6beSRobert Watson sigacts_free(p->p_sigacts); 943324fb6beSRobert Watson p->p_sigacts = NULL; 944324fb6beSRobert Watson 945324fb6beSRobert Watson /* 946324fb6beSRobert Watson * Do any thread-system specific cleanups. 947324fb6beSRobert Watson */ 948324fb6beSRobert Watson thread_wait(p); 949324fb6beSRobert Watson 950324fb6beSRobert Watson /* 951324fb6beSRobert Watson * Give vm and machine-dependent layer a chance to free anything that 952324fb6beSRobert Watson * cpu_exit couldn't release while still running in process context. 953324fb6beSRobert Watson */ 954324fb6beSRobert Watson vm_waitproc(p); 955324fb6beSRobert Watson #ifdef MAC 956324fb6beSRobert Watson mac_proc_destroy(p); 957324fb6beSRobert Watson #endif 9586520495aSAdrian Chadd /* 9596520495aSAdrian Chadd * Free any domain policy that's still hiding around. 9606520495aSAdrian Chadd */ 9616520495aSAdrian Chadd vm_domain_policy_cleanup(&p->p_vm_dom_policy); 9626520495aSAdrian Chadd 963324fb6beSRobert Watson KASSERT(FIRST_THREAD_IN_PROC(p), 964324fb6beSRobert Watson ("proc_reap: no residual thread!")); 965324fb6beSRobert Watson uma_zfree(proc_zone, p); 966324fb6beSRobert Watson sx_xlock(&allproc_lock); 967324fb6beSRobert Watson nprocs--; 968324fb6beSRobert Watson sx_xunlock(&allproc_lock); 969324fb6beSRobert Watson } 970324fb6beSRobert Watson 971dcd43281SKonstantin Belousov static int 972f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, 97357c74f5bSJohn Baldwin int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo, 97457c74f5bSJohn Baldwin int check_only) 975dcd43281SKonstantin Belousov { 976f13b5a0fSKonstantin Belousov struct rusage *rup; 977dcd43281SKonstantin Belousov 978db62ced2SJaakko Heinonen sx_assert(&proctree_lock, SA_XLOCKED); 979db62ced2SJaakko Heinonen 980dcd43281SKonstantin Belousov PROC_LOCK(p); 981f13b5a0fSKonstantin Belousov 982f13b5a0fSKonstantin Belousov switch (idtype) { 983f13b5a0fSKonstantin Belousov case P_ALL: 984*53bf545dSMariusz Zaborski if (p->p_procdesc != NULL) { 985*53bf545dSMariusz Zaborski PROC_UNLOCK(p); 986*53bf545dSMariusz Zaborski return (0); 987*53bf545dSMariusz Zaborski } 988f13b5a0fSKonstantin Belousov break; 989f13b5a0fSKonstantin Belousov case P_PID: 990f13b5a0fSKonstantin Belousov if (p->p_pid != (pid_t)id) { 991dcd43281SKonstantin Belousov PROC_UNLOCK(p); 992dcd43281SKonstantin Belousov return (0); 993dcd43281SKonstantin Belousov } 994f13b5a0fSKonstantin Belousov break; 995f13b5a0fSKonstantin Belousov case P_PGID: 996f13b5a0fSKonstantin Belousov if (p->p_pgid != (pid_t)id) { 997f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 998f13b5a0fSKonstantin Belousov return (0); 999f13b5a0fSKonstantin Belousov } 1000f13b5a0fSKonstantin Belousov break; 1001f13b5a0fSKonstantin Belousov case P_SID: 1002f13b5a0fSKonstantin Belousov if (p->p_session->s_sid != (pid_t)id) { 1003f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1004f13b5a0fSKonstantin Belousov return (0); 1005f13b5a0fSKonstantin Belousov } 1006f13b5a0fSKonstantin Belousov break; 1007f13b5a0fSKonstantin Belousov case P_UID: 1008f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_uid != (uid_t)id) { 1009f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1010f13b5a0fSKonstantin Belousov return (0); 1011f13b5a0fSKonstantin Belousov } 1012f13b5a0fSKonstantin Belousov break; 1013f13b5a0fSKonstantin Belousov case P_GID: 1014f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_gid != (gid_t)id) { 1015f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1016f13b5a0fSKonstantin Belousov return (0); 1017f13b5a0fSKonstantin Belousov } 1018f13b5a0fSKonstantin Belousov break; 1019f13b5a0fSKonstantin Belousov case P_JAILID: 1020b1051d92SMateusz Guzik if (p->p_ucred->cr_prison->pr_id != (int)id) { 1021f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1022f13b5a0fSKonstantin Belousov return (0); 1023f13b5a0fSKonstantin Belousov } 1024f13b5a0fSKonstantin Belousov break; 1025f13b5a0fSKonstantin Belousov /* 1026f13b5a0fSKonstantin Belousov * It seems that the thread structures get zeroed out 1027f13b5a0fSKonstantin Belousov * at process exit. This makes it impossible to 1028f13b5a0fSKonstantin Belousov * support P_SETID, P_CID or P_CPUID. 1029f13b5a0fSKonstantin Belousov */ 1030f13b5a0fSKonstantin Belousov default: 1031f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1032f13b5a0fSKonstantin Belousov return (0); 1033f13b5a0fSKonstantin Belousov } 1034f13b5a0fSKonstantin Belousov 1035dcd43281SKonstantin Belousov if (p_canwait(td, p)) { 1036dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1037dcd43281SKonstantin Belousov return (0); 1038dcd43281SKonstantin Belousov } 1039dcd43281SKonstantin Belousov 1040f13b5a0fSKonstantin Belousov if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) { 1041f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1042f13b5a0fSKonstantin Belousov return (0); 1043f13b5a0fSKonstantin Belousov } 1044f13b5a0fSKonstantin Belousov 1045dcd43281SKonstantin Belousov /* 1046dcd43281SKonstantin Belousov * This special case handles a kthread spawned by linux_clone 1047dcd43281SKonstantin Belousov * (see linux_misc.c). The linux_wait4 and linux_waitpid 1048dcd43281SKonstantin Belousov * functions need to be able to distinguish between waiting 1049dcd43281SKonstantin Belousov * on a process and waiting on a thread. It is a thread if 1050dcd43281SKonstantin Belousov * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 1051dcd43281SKonstantin Belousov * signifies we want to wait for threads and not processes. 1052dcd43281SKonstantin Belousov */ 1053dcd43281SKonstantin Belousov if ((p->p_sigparent != SIGCHLD) ^ 1054dcd43281SKonstantin Belousov ((options & WLINUXCLONE) != 0)) { 1055dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1056dcd43281SKonstantin Belousov return (0); 1057dcd43281SKonstantin Belousov } 1058dcd43281SKonstantin Belousov 1059f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1060f13b5a0fSKonstantin Belousov bzero(siginfo, sizeof(*siginfo)); 1061f13b5a0fSKonstantin Belousov siginfo->si_errno = 0; 1062f13b5a0fSKonstantin Belousov 1063f13b5a0fSKonstantin Belousov /* 1064f13b5a0fSKonstantin Belousov * SUSv4 requires that the si_signo value is always 1065f13b5a0fSKonstantin Belousov * SIGCHLD. Obey it despite the rfork(2) interface 1066f13b5a0fSKonstantin Belousov * allows to request other signal for child exit 1067f13b5a0fSKonstantin Belousov * notification. 1068f13b5a0fSKonstantin Belousov */ 1069f13b5a0fSKonstantin Belousov siginfo->si_signo = SIGCHLD; 1070f13b5a0fSKonstantin Belousov 1071f13b5a0fSKonstantin Belousov /* 1072f13b5a0fSKonstantin Belousov * This is still a rough estimate. We will fix the 1073f13b5a0fSKonstantin Belousov * cases TRAPPED, STOPPED, and CONTINUED later. 1074f13b5a0fSKonstantin Belousov */ 1075b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 1076f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_DUMPED; 1077b4490c6eSKonstantin Belousov siginfo->si_status = WTERMSIG(p->p_xsig); 1078b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 1079f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_KILLED; 1080b4490c6eSKonstantin Belousov siginfo->si_status = WTERMSIG(p->p_xsig); 1081b20a9aa9SJilles Tjoelker } else { 1082f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_EXITED; 1083b4490c6eSKonstantin Belousov siginfo->si_status = p->p_xexit; 1084b20a9aa9SJilles Tjoelker } 1085f13b5a0fSKonstantin Belousov 1086f13b5a0fSKonstantin Belousov siginfo->si_pid = p->p_pid; 1087f13b5a0fSKonstantin Belousov siginfo->si_uid = p->p_ucred->cr_uid; 1088f13b5a0fSKonstantin Belousov 1089f13b5a0fSKonstantin Belousov /* 1090f13b5a0fSKonstantin Belousov * The si_addr field would be useful additional 1091f13b5a0fSKonstantin Belousov * detail, but apparently the PC value may be lost 1092f13b5a0fSKonstantin Belousov * when we reach this point. bzero() above sets 1093f13b5a0fSKonstantin Belousov * siginfo->si_addr to NULL. 1094f13b5a0fSKonstantin Belousov */ 1095f13b5a0fSKonstantin Belousov } 1096f13b5a0fSKonstantin Belousov 1097f13b5a0fSKonstantin Belousov /* 1098f13b5a0fSKonstantin Belousov * There should be no reason to limit resources usage info to 1099f13b5a0fSKonstantin Belousov * exited processes only. A snapshot about any resources used 1100f13b5a0fSKonstantin Belousov * by a stopped process may be exactly what is needed. 1101f13b5a0fSKonstantin Belousov */ 1102f13b5a0fSKonstantin Belousov if (wrusage != NULL) { 1103f13b5a0fSKonstantin Belousov rup = &wrusage->wru_self; 1104f13b5a0fSKonstantin Belousov *rup = p->p_ru; 11055c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 1106f13b5a0fSKonstantin Belousov calcru(p, &rup->ru_utime, &rup->ru_stime); 11075c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 1108f13b5a0fSKonstantin Belousov 1109f13b5a0fSKonstantin Belousov rup = &wrusage->wru_children; 1110f13b5a0fSKonstantin Belousov *rup = p->p_stats->p_cru; 1111f13b5a0fSKonstantin Belousov calccru(p, &rup->ru_utime, &rup->ru_stime); 1112f13b5a0fSKonstantin Belousov } 1113f13b5a0fSKonstantin Belousov 111457c74f5bSJohn Baldwin if (p->p_state == PRS_ZOMBIE && !check_only) { 11155c7bebf9SKonstantin Belousov PROC_SLOCK(p); 1116f13b5a0fSKonstantin Belousov proc_reap(td, p, status, options); 1117dcd43281SKonstantin Belousov return (-1); 1118dcd43281SKonstantin Belousov } 1119dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1120dcd43281SKonstantin Belousov return (1); 1121dcd43281SKonstantin Belousov } 1122dcd43281SKonstantin Belousov 1123b7e23e82SJohn Baldwin int 11247eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 11257eaec467SJohn Baldwin struct rusage *rusage) 1126df8bae1dSRodney W. Grimes { 1127f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 1128f13b5a0fSKonstantin Belousov idtype_t idtype; 1129f13b5a0fSKonstantin Belousov id_t id; 1130f13b5a0fSKonstantin Belousov int ret; 1131f13b5a0fSKonstantin Belousov 1132538375d4SKonstantin Belousov /* 1133538375d4SKonstantin Belousov * Translate the special pid values into the (idtype, pid) 1134538375d4SKonstantin Belousov * pair for kern_wait6. The WAIT_MYPGRP case is handled by 1135538375d4SKonstantin Belousov * kern_wait6() on its own. 1136538375d4SKonstantin Belousov */ 1137f13b5a0fSKonstantin Belousov if (pid == WAIT_ANY) { 1138f13b5a0fSKonstantin Belousov idtype = P_ALL; 1139f13b5a0fSKonstantin Belousov id = 0; 1140ea293f3fSKonstantin Belousov } else if (pid < 0) { 1141f13b5a0fSKonstantin Belousov idtype = P_PGID; 1142f13b5a0fSKonstantin Belousov id = (id_t)-pid; 1143ea293f3fSKonstantin Belousov } else { 1144f13b5a0fSKonstantin Belousov idtype = P_PID; 1145f13b5a0fSKonstantin Belousov id = (id_t)pid; 1146f13b5a0fSKonstantin Belousov } 1147538375d4SKonstantin Belousov 1148f13b5a0fSKonstantin Belousov if (rusage != NULL) 1149f13b5a0fSKonstantin Belousov wrup = &wru; 1150f13b5a0fSKonstantin Belousov else 1151f13b5a0fSKonstantin Belousov wrup = NULL; 1152538375d4SKonstantin Belousov 1153f13b5a0fSKonstantin Belousov /* 1154f13b5a0fSKonstantin Belousov * For backward compatibility we implicitly add flags WEXITED 1155f13b5a0fSKonstantin Belousov * and WTRAPPED here. 1156f13b5a0fSKonstantin Belousov */ 1157f13b5a0fSKonstantin Belousov options |= WEXITED | WTRAPPED; 1158f13b5a0fSKonstantin Belousov ret = kern_wait6(td, idtype, id, status, options, wrup, NULL); 1159f13b5a0fSKonstantin Belousov if (rusage != NULL) 1160f13b5a0fSKonstantin Belousov *rusage = wru.wru_self; 1161f13b5a0fSKonstantin Belousov return (ret); 1162f13b5a0fSKonstantin Belousov } 1163f13b5a0fSKonstantin Belousov 1164f13b5a0fSKonstantin Belousov int 1165f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, 1166f13b5a0fSKonstantin Belousov int options, struct __wrusage *wrusage, siginfo_t *siginfo) 1167f13b5a0fSKonstantin Belousov { 1168324fb6beSRobert Watson struct proc *p, *q; 11693a41ec6aSEd Schouten pid_t pid; 1170dcd43281SKonstantin Belousov int error, nfound, ret; 1171df8bae1dSRodney W. Grimes 1172f13b5a0fSKonstantin Belousov AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */ 1173f13b5a0fSKonstantin Belousov AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */ 11742ef24ddeSRobert Watson AUDIT_ARG_VALUE(options); 1175de3007e8SWayne Salamon 1176b40ce416SJulian Elischer q = td->td_proc; 1177f13b5a0fSKonstantin Belousov 1178a2a85596SKonstantin Belousov if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1179a545089eSKonstantin Belousov PROC_LOCK(q); 1180f13b5a0fSKonstantin Belousov id = (id_t)q->p_pgid; 1181a545089eSKonstantin Belousov PROC_UNLOCK(q); 1182f13b5a0fSKonstantin Belousov idtype = P_PGID; 1183f591779bSSeigo Tanimura } 1184f13b5a0fSKonstantin Belousov 1185f528c3fdSDavid E. O'Brien /* If we don't know the option, just return. */ 1186f13b5a0fSKonstantin Belousov if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT | 1187f13b5a0fSKonstantin Belousov WEXITED | WTRAPPED | WLINUXCLONE)) != 0) 11886dc958b9SJohn Baldwin return (EINVAL); 1189f13b5a0fSKonstantin Belousov if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) { 1190f13b5a0fSKonstantin Belousov /* 1191f13b5a0fSKonstantin Belousov * We will be unable to find any matching processes, 1192f13b5a0fSKonstantin Belousov * because there are no known events to look for. 1193f13b5a0fSKonstantin Belousov * Prefer to return error instead of blocking 1194f13b5a0fSKonstantin Belousov * indefinitely. 1195f13b5a0fSKonstantin Belousov */ 1196f13b5a0fSKonstantin Belousov return (EINVAL); 1197f13b5a0fSKonstantin Belousov } 1198f13b5a0fSKonstantin Belousov 1199df8bae1dSRodney W. Grimes loop: 1200902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 1201902c0d82SDavid Xu PROC_LOCK(q); 1202902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 1203902c0d82SDavid Xu PROC_UNLOCK(q); 1204902c0d82SDavid Xu } 1205df8bae1dSRodney W. Grimes nfound = 0; 1206d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 12072e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 12083a41ec6aSEd Schouten pid = p->p_pid; 1209f13b5a0fSKonstantin Belousov ret = proc_to_reap(td, p, idtype, id, status, options, 121057c74f5bSJohn Baldwin wrusage, siginfo, 0); 1211dcd43281SKonstantin Belousov if (ret == 0) 1212df8bae1dSRodney W. Grimes continue; 1213dcd43281SKonstantin Belousov else if (ret == 1) 1214df8bae1dSRodney W. Grimes nfound++; 12153a41ec6aSEd Schouten else { 12163a41ec6aSEd Schouten td->td_retval[0] = pid; 1217d7aadbf9SJohn Baldwin return (0); 12183a41ec6aSEd Schouten } 1219dcd43281SKonstantin Belousov 1220dcd43281SKonstantin Belousov PROC_LOCK(p); 1221dcd43281SKonstantin Belousov PROC_SLOCK(p); 1222f13b5a0fSKonstantin Belousov 1223f13b5a0fSKonstantin Belousov if ((options & WTRAPPED) != 0 && 1224f13b5a0fSKonstantin Belousov (p->p_flag & P_TRACED) != 0 && 1225f13b5a0fSKonstantin Belousov (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 && 12265a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 1227f13b5a0fSKonstantin Belousov ((p->p_flag & P_WAITED) == 0)) { 1228982d11f8SJeff Roberson PROC_SUNLOCK(p); 1229f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) 1230df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 1231d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1232ebceaf6dSDavid Xu 1233f13b5a0fSKonstantin Belousov if (status != NULL) 1234b4490c6eSKonstantin Belousov *status = W_STOPCODE(p->p_xsig); 1235f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1236b4490c6eSKonstantin Belousov siginfo->si_status = p->p_xsig; 1237f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_TRAPPED; 1238f13b5a0fSKonstantin Belousov } 1239f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) { 1240ebceaf6dSDavid Xu PROC_LOCK(q); 1241ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 1242ebceaf6dSDavid Xu PROC_UNLOCK(q); 1243f13b5a0fSKonstantin Belousov } 1244ebceaf6dSDavid Xu 1245515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, 1246515b7a0bSJohn Baldwin "wait: returning trapped pid %d status %#x (xstat %d) xthread %d", 1247b4490c6eSKonstantin Belousov p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig, 1248515b7a0bSJohn Baldwin p->p_xthread != NULL ? p->p_xthread->td_tid : -1); 1249f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 12503a41ec6aSEd Schouten td->td_retval[0] = pid; 1251f13b5a0fSKonstantin Belousov return (0); 1252f13b5a0fSKonstantin Belousov } 1253f13b5a0fSKonstantin Belousov if ((options & WUNTRACED) != 0 && 1254f13b5a0fSKonstantin Belousov (p->p_flag & P_STOPPED_SIG) != 0 && 1255f13b5a0fSKonstantin Belousov (p->p_suspcount == p->p_numthreads) && 1256f13b5a0fSKonstantin Belousov ((p->p_flag & P_WAITED) == 0)) { 1257f13b5a0fSKonstantin Belousov PROC_SUNLOCK(p); 1258f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) 1259f13b5a0fSKonstantin Belousov p->p_flag |= P_WAITED; 1260f13b5a0fSKonstantin Belousov sx_xunlock(&proctree_lock); 1261f13b5a0fSKonstantin Belousov 1262f13b5a0fSKonstantin Belousov if (status != NULL) 1263b4490c6eSKonstantin Belousov *status = W_STOPCODE(p->p_xsig); 1264f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1265b4490c6eSKonstantin Belousov siginfo->si_status = p->p_xsig; 1266f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_STOPPED; 1267f13b5a0fSKonstantin Belousov } 1268f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) { 1269f13b5a0fSKonstantin Belousov PROC_LOCK(q); 1270f13b5a0fSKonstantin Belousov sigqueue_take(p->p_ksi); 1271f13b5a0fSKonstantin Belousov PROC_UNLOCK(q); 1272f13b5a0fSKonstantin Belousov } 1273f13b5a0fSKonstantin Belousov 1274f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 12753a41ec6aSEd Schouten td->td_retval[0] = pid; 1276b7e23e82SJohn Baldwin return (0); 1277df8bae1dSRodney W. Grimes } 1278982d11f8SJeff Roberson PROC_SUNLOCK(p); 1279f13b5a0fSKonstantin Belousov if ((options & WCONTINUED) != 0 && 1280f13b5a0fSKonstantin Belousov (p->p_flag & P_CONTINUED) != 0) { 12816ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 1282f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) { 12836ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 1284ebceaf6dSDavid Xu PROC_LOCK(q); 1285ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 1286ebceaf6dSDavid Xu PROC_UNLOCK(q); 1287f13b5a0fSKonstantin Belousov } 1288e94cc4acSDavid Xu PROC_UNLOCK(p); 1289ebceaf6dSDavid Xu 1290f13b5a0fSKonstantin Belousov if (status != NULL) 1291b7e23e82SJohn Baldwin *status = SIGCONT; 1292f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1293f13b5a0fSKonstantin Belousov siginfo->si_status = SIGCONT; 1294f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_CONTINUED; 1295f13b5a0fSKonstantin Belousov } 12963a41ec6aSEd Schouten td->td_retval[0] = pid; 1297dcdc6c36SKonstantin Belousov return (0); 12986ee093fbSMike Barcroft } 1299d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 1300d7aadbf9SJohn Baldwin } 1301dcd43281SKonstantin Belousov 1302dcd43281SKonstantin Belousov /* 1303dcd43281SKonstantin Belousov * Look in the orphans list too, to allow the parent to 1304dcd43281SKonstantin Belousov * collect it's child exit status even if child is being 1305dcd43281SKonstantin Belousov * debugged. 1306dcd43281SKonstantin Belousov * 1307dcd43281SKonstantin Belousov * Debugger detaches from the parent upon successful 1308dcd43281SKonstantin Belousov * switch-over from parent to child. At this point due to 1309dcd43281SKonstantin Belousov * re-parenting the parent loses the child to debugger and a 1310dcd43281SKonstantin Belousov * wait4(2) call would report that it has no children to wait 1311dcd43281SKonstantin Belousov * for. By maintaining a list of orphans we allow the parent 1312dcd43281SKonstantin Belousov * to successfully wait until the child becomes a zombie. 1313dcd43281SKonstantin Belousov */ 131457c74f5bSJohn Baldwin if (nfound == 0) { 1315dcd43281SKonstantin Belousov LIST_FOREACH(p, &q->p_orphans, p_orphan) { 131657c74f5bSJohn Baldwin ret = proc_to_reap(td, p, idtype, id, NULL, options, 131757c74f5bSJohn Baldwin NULL, NULL, 1); 131857c74f5bSJohn Baldwin if (ret != 0) { 131957c74f5bSJohn Baldwin KASSERT(ret != -1, ("reaped an orphan (pid %d)", 132057c74f5bSJohn Baldwin (int)td->td_retval[0])); 1321dcd43281SKonstantin Belousov nfound++; 132257c74f5bSJohn Baldwin break; 132357c74f5bSJohn Baldwin } 132457c74f5bSJohn Baldwin } 1325dcd43281SKonstantin Belousov } 1326d7aadbf9SJohn Baldwin if (nfound == 0) { 1327d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1328d7aadbf9SJohn Baldwin return (ECHILD); 1329d7aadbf9SJohn Baldwin } 1330b7e23e82SJohn Baldwin if (options & WNOHANG) { 1331d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1332d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 1333d7aadbf9SJohn Baldwin return (0); 1334d7aadbf9SJohn Baldwin } 1335d7aadbf9SJohn Baldwin PROC_LOCK(q); 1336d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 133795992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 133895992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 133995992d56SDavid Xu error = 0; 134095992d56SDavid Xu } else 13417f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 1342d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 13434ae89b95SJohn Baldwin if (error) 1344d7aadbf9SJohn Baldwin return (error); 1345d7aadbf9SJohn Baldwin goto loop; 1346d7aadbf9SJohn Baldwin } 1347df8bae1dSRodney W. Grimes 1348df8bae1dSRodney W. Grimes /* 134998f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 135098f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 1351df8bae1dSRodney W. Grimes */ 1352df8bae1dSRodney W. Grimes void 1353830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 1354df8bae1dSRodney W. Grimes { 1355df8bae1dSRodney W. Grimes 13564e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 1357c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 1358df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 1359df8bae1dSRodney W. Grimes return; 1360df8bae1dSRodney W. Grimes 1361ff766807SDavid Xu PROC_LOCK(child->p_pptr); 1362ff766807SDavid Xu sigqueue_take(child->p_ksi); 1363ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 1364b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 1365b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1366dcd43281SKonstantin Belousov 13672e39e24fSKonstantin Belousov clear_orphan(child); 1368dcd43281SKonstantin Belousov if (child->p_flag & P_TRACED) { 1369d7359980SKonstantin Belousov if (LIST_EMPTY(&child->p_pptr->p_orphans)) { 1370d7359980SKonstantin Belousov child->p_treeflag |= P_TREE_FIRST_ORPHAN; 1371d7359980SKonstantin Belousov LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, 1372d7359980SKonstantin Belousov p_orphan); 1373d7359980SKonstantin Belousov } else { 1374ba78d6b7SMark Johnston LIST_INSERT_AFTER(LIST_FIRST(&child->p_pptr->p_orphans), 1375ba78d6b7SMark Johnston child, p_orphan); 1376d7359980SKonstantin Belousov } 1377d7359980SKonstantin Belousov child->p_treeflag |= P_TREE_ORPHANED; 1378dcd43281SKonstantin Belousov } 1379dcd43281SKonstantin Belousov 1380df8bae1dSRodney W. Grimes child->p_pptr = parent; 1381df8bae1dSRodney W. Grimes } 1382