19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro 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 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 465fdb8324SBruce Evans #include <sys/sysproto.h> 474a144410SRobert Watson #include <sys/capsicum.h> 4875b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 491c5bb3eaSPeter Wemm #include <sys/kernel.h> 50*daec9284SConrad Meyer #include <sys/ktr.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> 91df8bae1dSRodney W. Grimes 925d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 935d217f17SJohn Birrell #include <sys/dtrace_bsd.h> 945d217f17SJohn Birrell dtrace_execexit_func_t dtrace_fasttrap_exit; 955d217f17SJohn Birrell #endif 965d217f17SJohn Birrell 975d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 9836160958SMark Johnston SDT_PROBE_DEFINE1(proc, , , exit, "int"); 995d217f17SJohn Birrell 100c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 101c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 102c0bc2867SGleb Smirnoff 103d7359980SKonstantin Belousov struct proc * 104d7359980SKonstantin Belousov proc_realparent(struct proc *child) 105d7359980SKonstantin Belousov { 106d7359980SKonstantin Belousov struct proc *p, *parent; 107d7359980SKonstantin Belousov 108d7359980SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED); 1092c054ce9SMateusz Guzik if ((child->p_treeflag & P_TREE_ORPHANED) == 0) 1102c054ce9SMateusz Guzik return (child->p_pptr->p_pid == child->p_oppid ? 1112c054ce9SMateusz Guzik child->p_pptr : initproc); 112d7359980SKonstantin Belousov for (p = child; (p->p_treeflag & P_TREE_FIRST_ORPHAN) == 0;) { 113d7359980SKonstantin Belousov /* Cannot use LIST_PREV(), since the list head is not known. */ 114d7359980SKonstantin Belousov p = __containerof(p->p_orphan.le_prev, struct proc, 115d7359980SKonstantin Belousov p_orphan.le_next); 116d7359980SKonstantin Belousov KASSERT((p->p_treeflag & P_TREE_ORPHANED) != 0, 117d7359980SKonstantin Belousov ("missing P_ORPHAN %p", p)); 118d7359980SKonstantin Belousov } 119d7359980SKonstantin Belousov parent = __containerof(p->p_orphan.le_prev, struct proc, 120d7359980SKonstantin Belousov p_orphans.lh_first); 121d7359980SKonstantin Belousov return (parent); 122d7359980SKonstantin Belousov } 123d7359980SKonstantin Belousov 124237623b0SKonstantin Belousov void 125237623b0SKonstantin Belousov reaper_abandon_children(struct proc *p, bool exiting) 126237623b0SKonstantin Belousov { 127237623b0SKonstantin Belousov struct proc *p1, *p2, *ptmp; 128237623b0SKonstantin Belousov 129237623b0SKonstantin Belousov sx_assert(&proctree_lock, SX_LOCKED); 130237623b0SKonstantin Belousov KASSERT(p != initproc, ("reaper_abandon_children for initproc")); 131237623b0SKonstantin Belousov if ((p->p_treeflag & P_TREE_REAPER) == 0) 132237623b0SKonstantin Belousov return; 133237623b0SKonstantin Belousov p1 = p->p_reaper; 134237623b0SKonstantin Belousov LIST_FOREACH_SAFE(p2, &p->p_reaplist, p_reapsibling, ptmp) { 135237623b0SKonstantin Belousov LIST_REMOVE(p2, p_reapsibling); 136237623b0SKonstantin Belousov p2->p_reaper = p1; 137237623b0SKonstantin Belousov p2->p_reapsubtree = p->p_reapsubtree; 138237623b0SKonstantin Belousov LIST_INSERT_HEAD(&p1->p_reaplist, p2, p_reapsibling); 139237623b0SKonstantin Belousov if (exiting && p2->p_pptr == p) { 140237623b0SKonstantin Belousov PROC_LOCK(p2); 1412c054ce9SMateusz Guzik proc_reparent(p2, p1, true); 142237623b0SKonstantin Belousov PROC_UNLOCK(p2); 143237623b0SKonstantin Belousov } 144237623b0SKonstantin Belousov } 145237623b0SKonstantin Belousov KASSERT(LIST_EMPTY(&p->p_reaplist), ("p_reaplist not empty")); 146237623b0SKonstantin Belousov p->p_treeflag &= ~P_TREE_REAPER; 147237623b0SKonstantin Belousov } 148237623b0SKonstantin Belousov 1492e39e24fSKonstantin Belousov static void 15034ebdceaSMateusz Guzik reaper_clear(struct proc *p) 15134ebdceaSMateusz Guzik { 15234ebdceaSMateusz Guzik struct proc *p1; 15334ebdceaSMateusz Guzik bool clear; 15434ebdceaSMateusz Guzik 15534ebdceaSMateusz Guzik sx_assert(&proctree_lock, SX_LOCKED); 15634ebdceaSMateusz Guzik LIST_REMOVE(p, p_reapsibling); 15734ebdceaSMateusz Guzik if (p->p_reapsubtree == 1) 15834ebdceaSMateusz Guzik return; 15934ebdceaSMateusz Guzik clear = true; 16034ebdceaSMateusz Guzik LIST_FOREACH(p1, &p->p_reaper->p_reaplist, p_reapsibling) { 16134ebdceaSMateusz Guzik if (p1->p_reapsubtree == p->p_reapsubtree) { 16234ebdceaSMateusz Guzik clear = false; 16334ebdceaSMateusz Guzik break; 16434ebdceaSMateusz Guzik } 16534ebdceaSMateusz Guzik } 16634ebdceaSMateusz Guzik if (clear) 16734ebdceaSMateusz Guzik proc_id_clear(PROC_ID_REAP, p->p_reapsubtree); 16834ebdceaSMateusz Guzik } 16934ebdceaSMateusz Guzik 17034ebdceaSMateusz Guzik static void 1712e39e24fSKonstantin Belousov clear_orphan(struct proc *p) 1722e39e24fSKonstantin Belousov { 173d7359980SKonstantin Belousov struct proc *p1; 1742e39e24fSKonstantin Belousov 175d7359980SKonstantin Belousov sx_assert(&proctree_lock, SA_XLOCKED); 176d7359980SKonstantin Belousov if ((p->p_treeflag & P_TREE_ORPHANED) == 0) 177d7359980SKonstantin Belousov return; 178d7359980SKonstantin Belousov if ((p->p_treeflag & P_TREE_FIRST_ORPHAN) != 0) { 179d7359980SKonstantin Belousov p1 = LIST_NEXT(p, p_orphan); 180d7359980SKonstantin Belousov if (p1 != NULL) 181d7359980SKonstantin Belousov p1->p_treeflag |= P_TREE_FIRST_ORPHAN; 182d7359980SKonstantin Belousov p->p_treeflag &= ~P_TREE_FIRST_ORPHAN; 1832e39e24fSKonstantin Belousov } 184d7359980SKonstantin Belousov LIST_REMOVE(p, p_orphan); 185d7359980SKonstantin Belousov p->p_treeflag &= ~P_TREE_ORPHANED; 1862e39e24fSKonstantin Belousov } 1872e39e24fSKonstantin Belousov 188df8bae1dSRodney W. Grimes /* 189873fbcd7SRobert Watson * exit -- death of process. 190df8bae1dSRodney W. Grimes */ 191fc0b1dbfSBruce Evans void 1928451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 193df8bae1dSRodney W. Grimes { 194b40ce416SJulian Elischer 195b4490c6eSKonstantin Belousov exit1(td, uap->rval, 0); 196df8bae1dSRodney W. Grimes /* NOTREACHED */ 197df8bae1dSRodney W. Grimes } 198df8bae1dSRodney W. Grimes 199df8bae1dSRodney W. Grimes /* 200873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 201873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 202873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 203df8bae1dSRodney W. Grimes */ 204fc0b1dbfSBruce Evans void 205b4490c6eSKonstantin Belousov exit1(struct thread *td, int rval, int signo) 206df8bae1dSRodney W. Grimes { 207a661bebeSMateusz Guzik struct proc *p, *nq, *q, *t; 208a661bebeSMateusz Guzik struct thread *tdt; 2099a2dde80SKonstantin Belousov ksiginfo_t *ksi, *ksi1; 210015cd8dcSMateusz Guzik int signal_parent; 211df8bae1dSRodney W. Grimes 212e17660e7SKris Kennaway mtx_assert(&Giant, MA_NOTOWNED); 213b4490c6eSKonstantin Belousov KASSERT(rval == 0 || signo == 0, ("exit1 rv %d sig %d", rval, signo)); 2140cddd8f0SMatthew Dillon 215276c5169SJohn Baldwin p = td->td_proc; 2165486ffc8SMarius Strobl /* 2175486ffc8SMarius Strobl * XXX in case we're rebooting we just let init die in order to 2185486ffc8SMarius Strobl * work around an unsolved stack overflow seen very late during 2195486ffc8SMarius Strobl * shutdown on sparc64 when the gmirror worker process exists. 2205486ffc8SMarius Strobl */ 2215486ffc8SMarius Strobl if (p == initproc && rebooting == 0) { 222b4490c6eSKonstantin Belousov printf("init died (signal %d, exit %d)\n", signo, rval); 2235f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 2245f7bd355SPoul-Henning Kamp } 2252c1011f7SJohn Dyson 2267a6b989bSJohn Baldwin /* 2271bc93bb7SKonstantin Belousov * Deref SU mp, since the thread does not return to userspace. 2281bc93bb7SKonstantin Belousov */ 229aca4bb91SKonstantin Belousov td_softdep_cleanup(td); 2301bc93bb7SKonstantin Belousov 2311bc93bb7SKonstantin Belousov /* 2322c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 2337a6b989bSJohn Baldwin */ 234e602ba25SJulian Elischer PROC_LOCK(p); 235e602ba25SJulian Elischer /* 236aba1ca52SKonstantin Belousov * First check if some other thread or external request got 237aba1ca52SKonstantin Belousov * here before us. If so, act appropriately: exit or suspend. 238aba1ca52SKonstantin Belousov * We must ensure that stop requests are handled before we set 239aba1ca52SKonstantin Belousov * P_WEXIT. 240e602ba25SJulian Elischer */ 241e602ba25SJulian Elischer thread_suspend_check(0); 242aba1ca52SKonstantin Belousov while (p->p_flag & P_HADTHREADS) { 243e602ba25SJulian Elischer /* 244e602ba25SJulian Elischer * Kill off the other threads. This requires 2456111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 2466111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 2476111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 248e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 2496111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 2506111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 2516111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 252b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 2536111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 2546111dcd2SJohn Baldwin * other threads exits. 255b370279eSDavid Xu * If there is already a thread singler after resumption, 2566111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 257b370279eSDavid Xu * re-check all suspension request, the thread should 258b370279eSDavid Xu * either be suspended there or exit. 259e602ba25SJulian Elischer */ 2606ddcc233SKonstantin Belousov if (!thread_single(p, SINGLE_EXIT)) 261e602ba25SJulian Elischer /* 262aba1ca52SKonstantin Belousov * All other activity in this process is now 263aba1ca52SKonstantin Belousov * stopped. Threading support has been turned 264aba1ca52SKonstantin Belousov * off. 265e602ba25SJulian Elischer */ 266aba1ca52SKonstantin Belousov break; 267aba1ca52SKonstantin Belousov /* 268aba1ca52SKonstantin Belousov * Recheck for new stop or suspend requests which 269aba1ca52SKonstantin Belousov * might appear while process lock was dropped in 270aba1ca52SKonstantin Belousov * thread_single(). 271aba1ca52SKonstantin Belousov */ 272aba1ca52SKonstantin Belousov thread_suspend_check(0); 273e602ba25SJulian Elischer } 2741c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 2751c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 27658c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 277b4490c6eSKonstantin Belousov 278b4490c6eSKonstantin Belousov /* Let event handler change exit status */ 279b4490c6eSKonstantin Belousov p->p_xexit = rval; 280b4490c6eSKonstantin Belousov p->p_xsig = signo; 281b4490c6eSKonstantin Belousov 28206ad42b2SJohn Baldwin /* 28306ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 28406ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 28506ad42b2SJohn Baldwin * released their reference to us. Note that if they have 28606ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 28706ad42b2SJohn Baldwin * via PIOCCONT. 28806ad42b2SJohn Baldwin */ 289b4490c6eSKonstantin Belousov _STOPEVENT(p, S_EXIT, 0); 29006ad42b2SJohn Baldwin 29106ad42b2SJohn Baldwin /* 2920f14f15bSJohn Baldwin * Ignore any pending request to stop due to a stop signal. 2930f14f15bSJohn Baldwin * Once P_WEXIT is set, future requests will be ignored as 2940f14f15bSJohn Baldwin * well. 2950f14f15bSJohn Baldwin */ 2960f14f15bSJohn Baldwin p->p_flag &= ~P_STOPPED_SIG; 2970f14f15bSJohn Baldwin KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 2980f14f15bSJohn Baldwin 2990f14f15bSJohn Baldwin /* 30006ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 30106ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 30206ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 30306ad42b2SJohn Baldwin */ 304e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 30506ad42b2SJohn Baldwin wakeup(&p->p_stype); 30606ad42b2SJohn Baldwin 30706ad42b2SJohn Baldwin /* 30806ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 30906ad42b2SJohn Baldwin * release their reference. 31006ad42b2SJohn Baldwin */ 31106ad42b2SJohn Baldwin while (p->p_lock > 0) 31206ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 313ebceaf6dSDavid Xu 314e602ba25SJulian Elischer PROC_UNLOCK(p); 3151c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 3161c4bcd05SJeff Roberson callout_drain(&p->p_limco); 317b40ce416SJulian Elischer 31800c28d96SRobert Watson #ifdef AUDIT 31900c28d96SRobert Watson /* 32000c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 32100c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 32200c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 32300c28d96SRobert Watson * what the return value is. 32400c28d96SRobert Watson */ 325b4490c6eSKonstantin Belousov AUDIT_ARG_EXIT(rval, 0); 32600c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 32700c28d96SRobert Watson #endif 32800c28d96SRobert Watson 329bad2520aSMateusz Guzik /* Are we a task leader with peers? */ 330bad2520aSMateusz Guzik if (p->p_peers != NULL && p == p->p_leader) { 331c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 3322c1011f7SJohn Dyson q = p->p_peers; 333776e0b36SJohn Baldwin while (q != NULL) { 334776e0b36SJohn Baldwin PROC_LOCK(q); 3358451d0ddSKip Macy kern_psignal(q, SIGKILL); 336776e0b36SJohn Baldwin PROC_UNLOCK(q); 3372c1011f7SJohn Dyson q = q->p_peers; 3382c1011f7SJohn Dyson } 339c6544064SJohn Baldwin while (p->p_peers != NULL) 340c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 341c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 3422c1011f7SJohn Dyson } 3432c1011f7SJohn Dyson 344fed06968SJulian Elischer /* 345e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 346b4490c6eSKonstantin Belousov * E.g. SYSV IPC stuff. 347b4490c6eSKonstantin Belousov * Event handler could change exit status. 348fed06968SJulian Elischer * XXX what if one of these generates an error? 349fed06968SJulian Elischer */ 3502ca45184SMatt Joras EVENTHANDLER_DIRECT_INVOKE(process_exit, p); 351a914fb6bSJohn Baldwin 352e3d3e828SMateusz Guzik /* 353e3d3e828SMateusz Guzik * If parent is waiting for us to exit or exec, 354e3d3e828SMateusz Guzik * P_PPWAIT is set; we will wakeup the parent below. 355e3d3e828SMateusz Guzik */ 356a914fb6bSJohn Baldwin PROC_LOCK(p); 357a282253aSJulian Elischer stopprofclock(p); 358e3d3e828SMateusz Guzik p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 3598d570f64SJohn Baldwin p->p_ptevents = 0; 3605499ea01SJohn Baldwin 3615499ea01SJohn Baldwin /* 3625499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 3635499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 3645499ea01SJohn Baldwin */ 3655499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 36656d48d6dSKonstantin Belousov _callout_stop_safe(&p->p_itcallout, CS_EXECUTING, NULL) == 0) { 3675499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 3685499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 3695499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 3705499ea01SJohn Baldwin ("realtime timer is still armed")); 3715499ea01SJohn Baldwin } 3722a339d9eSKonstantin Belousov 37396d7f8efSTim J. Robbins PROC_UNLOCK(p); 374df8bae1dSRodney W. Grimes 3752a339d9eSKonstantin Belousov umtx_thread_exit(td); 3762a339d9eSKonstantin Belousov 377df8bae1dSRodney W. Grimes /* 378831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 379831d27a9SDon Lewis * F_SETOWN with our pid. 380831d27a9SDon Lewis */ 381831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 382831d27a9SDon Lewis 383831d27a9SDon Lewis /* 384c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 385c0bc2867SGleb Smirnoff */ 386c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 387c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 388c0bc2867SGleb Smirnoff 389c0bc2867SGleb Smirnoff /* 390df8bae1dSRodney W. Grimes * Close open files and release open-file table. 391df8bae1dSRodney W. Grimes * This may block! 392df8bae1dSRodney W. Grimes */ 3932609222aSPawel Jakub Dawidek fdescfree(td); 394df8bae1dSRodney W. Grimes 395a914fb6bSJohn Baldwin /* 396a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 397a2587073SPoul-Henning Kamp * stop before we return to userland 398a2587073SPoul-Henning Kamp */ 399a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 400a2587073SPoul-Henning Kamp g_waitidle(); 401a2587073SPoul-Henning Kamp 402a2587073SPoul-Henning Kamp /* 403a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 404a914fb6bSJohn Baldwin */ 405bad2520aSMateusz Guzik if (p->p_leader->p_peers != NULL) { 406c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 407bad2520aSMateusz Guzik if (p->p_leader->p_peers != NULL) { 40879fc0bf4SMike Smith q = p->p_leader; 40979fc0bf4SMike Smith while (q->p_peers != p) 41079fc0bf4SMike Smith q = q->p_peers; 41179fc0bf4SMike Smith q->p_peers = p->p_peers; 4127f05b035SAlfred Perlstein wakeup(p->p_leader); 41379fc0bf4SMike Smith } 414c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 415bad2520aSMateusz Guzik } 41679fc0bf4SMike Smith 41757051fdcSTor Egge vmspace_exit(td); 41817182b13SMateusz Guzik killjobc(); 419b40ce416SJulian Elischer (void)acct_process(td); 420bc093719SEd Schouten 421df8bae1dSRodney W. Grimes #ifdef KTRACE 4222c255e9dSRobert Watson ktrprocexit(td); 423df8bae1dSRodney W. Grimes #endif 424df8bae1dSRodney W. Grimes /* 425ee42d0a9SDavid Malone * Release reference to text vnode 426ee42d0a9SDavid Malone */ 42788f98985SMateusz Guzik if (p->p_textvp != NULL) { 42888f98985SMateusz Guzik vrele(p->p_textvp); 429ee42d0a9SDavid Malone p->p_textvp = NULL; 430ee42d0a9SDavid Malone } 431ee42d0a9SDavid Malone 432ee42d0a9SDavid Malone /* 433d7aadbf9SJohn Baldwin * Release our limits structure. 434d7aadbf9SJohn Baldwin */ 43588f98985SMateusz Guzik lim_free(p->p_limit); 436d7aadbf9SJohn Baldwin p->p_limit = NULL; 437d7aadbf9SJohn Baldwin 438cf7d9a8cSDavid Xu tidhash_remove(td); 439cf7d9a8cSDavid Xu 440d7aadbf9SJohn Baldwin /* 44181d68271SMateusz Guzik * Call machine-dependent code to release any 44281d68271SMateusz Guzik * machine-dependent resources other than the address space. 44381d68271SMateusz Guzik * The address space is released by "vmspace_exitfree(p)" in 44481d68271SMateusz Guzik * vm_waitproc(). 44581d68271SMateusz Guzik */ 44681d68271SMateusz Guzik cpu_exit(td); 44781d68271SMateusz Guzik 44881d68271SMateusz Guzik WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 44981d68271SMateusz Guzik 45081d68271SMateusz Guzik /* 451a627b462SMateusz Guzik * Move proc from allproc queue to zombproc. 452df8bae1dSRodney W. Grimes */ 4531005a129SJohn Baldwin sx_xlock(&allproc_lock); 4541e9a1bf5SMateusz Guzik sx_xlock(&zombproc_lock); 455b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 456b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 4571e9a1bf5SMateusz Guzik sx_xunlock(&zombproc_lock); 4581005a129SJohn Baldwin sx_xunlock(&allproc_lock); 459df8bae1dSRodney W. Grimes 460b1fbffe7SMateusz Guzik sx_xlock(&proctree_lock); 461b1fbffe7SMateusz Guzik 46237f84a60SJohn Baldwin /* 463a661bebeSMateusz Guzik * Reparent all children processes: 464a661bebeSMateusz Guzik * - traced ones to the original parent (or init if we are that parent) 465a661bebeSMateusz Guzik * - the rest to init 46637f84a60SJohn Baldwin */ 4672e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 468a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 469237623b0SKonstantin Belousov wakeup(q->p_reaper); 470a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4712e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 4729a2dde80SKonstantin Belousov ksi = ksiginfo_alloc(TRUE); 473a914fb6bSJohn Baldwin PROC_LOCK(q); 4744ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 475904c5ec4SDavid Xu 476a661bebeSMateusz Guzik if (!(q->p_flag & P_TRACED)) { 4772c054ce9SMateusz Guzik proc_reparent(q, q->p_reaper, true); 478545d3122SKonstantin Belousov if (q->p_state == PRS_ZOMBIE) { 4799a2dde80SKonstantin Belousov /* 4809a2dde80SKonstantin Belousov * Inform reaper about the reparented 4819a2dde80SKonstantin Belousov * zombie, since wait(2) has something 4829a2dde80SKonstantin Belousov * new to report. Guarantee queueing 4839a2dde80SKonstantin Belousov * of the SIGCHLD signal, similar to 4849a2dde80SKonstantin Belousov * the _exit() behaviour, by providing 4859a2dde80SKonstantin Belousov * our ksiginfo. Ksi is freed by the 4869a2dde80SKonstantin Belousov * signal delivery. 4879a2dde80SKonstantin Belousov */ 4889a2dde80SKonstantin Belousov if (q->p_ksi == NULL) { 4899a2dde80SKonstantin Belousov ksi1 = NULL; 4909a2dde80SKonstantin Belousov } else { 4919a2dde80SKonstantin Belousov ksiginfo_copy(q->p_ksi, ksi); 4929a2dde80SKonstantin Belousov ksi->ksi_flags |= KSI_INS; 4939a2dde80SKonstantin Belousov ksi1 = ksi; 4949a2dde80SKonstantin Belousov ksi = NULL; 4959a2dde80SKonstantin Belousov } 496545d3122SKonstantin Belousov PROC_LOCK(q->p_reaper); 4979a2dde80SKonstantin Belousov pksignal(q->p_reaper, SIGCHLD, ksi1); 498545d3122SKonstantin Belousov PROC_UNLOCK(q->p_reaper); 499b9408863SKonstantin Belousov } else if (q->p_pdeathsig > 0) { 500b9408863SKonstantin Belousov /* 501b9408863SKonstantin Belousov * The child asked to received a signal 502b9408863SKonstantin Belousov * when we exit. 503b9408863SKonstantin Belousov */ 504b9408863SKonstantin Belousov kern_psignal(q, q->p_pdeathsig); 505545d3122SKonstantin Belousov } 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) { 5132c054ce9SMateusz Guzik proc_reparent(q, q->p_reaper, true); 514a661bebeSMateusz Guzik } else { 515a661bebeSMateusz Guzik PROC_LOCK(t); 5162c054ce9SMateusz Guzik proc_reparent(q, t, true); 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); 527b7a25e63SKonstantin Belousov q->p_flag2 &= ~P2_PTRACE_FSTP; 5288d570f64SJohn Baldwin q->p_ptevents = 0; 529b7a25e63SKonstantin Belousov FOREACH_THREAD_IN_PROC(q, tdt) { 530b7a25e63SKonstantin Belousov tdt->td_dbgflags &= ~(TDB_SUSPEND | TDB_XSIG | 531b7a25e63SKonstantin Belousov TDB_FSTP); 532b7a25e63SKonstantin Belousov } 5338451d0ddSKip Macy kern_psignal(q, SIGKILL); 534c65437a3SJohn Baldwin } 535a914fb6bSJohn Baldwin PROC_UNLOCK(q); 5369a2dde80SKonstantin Belousov if (ksi != NULL) 5379a2dde80SKonstantin Belousov ksiginfo_free(ksi); 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes 5405085ecb7SKonstantin Belousov /* 5415085ecb7SKonstantin Belousov * Also get rid of our orphans. 5425085ecb7SKonstantin Belousov */ 5435085ecb7SKonstantin Belousov while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 5445085ecb7SKonstantin Belousov PROC_LOCK(q); 545128c9bc0SMark Johnston KASSERT(q->p_oppid == p->p_pid, 546128c9bc0SMark Johnston ("orphan %p of %p has unexpected oppid %d", q, p, 547128c9bc0SMark Johnston q->p_oppid)); 548128c9bc0SMark Johnston q->p_oppid = q->p_reaper->p_pid; 549128c9bc0SMark Johnston 550b9408863SKonstantin Belousov /* 551b9408863SKonstantin Belousov * If we are the real parent of this process 552b9408863SKonstantin Belousov * but it has been reparented to a debugger, then 553b9408863SKonstantin Belousov * check if it asked for a signal when we exit. 554b9408863SKonstantin Belousov */ 555b9408863SKonstantin Belousov if (q->p_pdeathsig > 0) 556b9408863SKonstantin Belousov kern_psignal(q, q->p_pdeathsig); 557515b7a0bSJohn Baldwin CTR2(KTR_PTRACE, "exit: pid %d, clearing orphan %d", p->p_pid, 558515b7a0bSJohn Baldwin q->p_pid); 5595085ecb7SKonstantin Belousov clear_orphan(q); 5605085ecb7SKonstantin Belousov PROC_UNLOCK(q); 5615085ecb7SKonstantin Belousov } 5625085ecb7SKonstantin Belousov 563eadb1dcbSMateusz Guzik #ifdef KDTRACE_HOOKS 564eadb1dcbSMateusz Guzik if (SDT_PROBES_ENABLED()) { 565eadb1dcbSMateusz Guzik int reason = CLD_EXITED; 566eadb1dcbSMateusz Guzik if (WCOREDUMP(signo)) 567eadb1dcbSMateusz Guzik reason = CLD_DUMPED; 568eadb1dcbSMateusz Guzik else if (WIFSIGNALED(signo)) 569eadb1dcbSMateusz Guzik reason = CLD_KILLED; 570eadb1dcbSMateusz Guzik SDT_PROBE1(proc, , , exit, reason); 571eadb1dcbSMateusz Guzik } 572eadb1dcbSMateusz Guzik #endif 573eadb1dcbSMateusz Guzik 5741c4bcd05SJeff Roberson /* Save exit status. */ 575d7aadbf9SJohn Baldwin PROC_LOCK(p); 576cbf4e354SDavid Xu p->p_xthread = td; 5775d217f17SJohn Birrell 5785d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 5795d217f17SJohn Birrell /* 5805d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 5815d217f17SJohn Birrell * has declared an interest. 5825d217f17SJohn Birrell */ 5835d217f17SJohn Birrell if (dtrace_fasttrap_exit) 5845d217f17SJohn Birrell dtrace_fasttrap_exit(p); 5855d217f17SJohn Birrell #endif 5865d217f17SJohn Birrell 5871c4bcd05SJeff Roberson /* 5887a6b989bSJohn Baldwin * Notify interested parties of our demise. 589cb679c38SJonathan Lemon */ 5909e590ff0SKonstantin Belousov KNOTE_LOCKED(p->p_klist, NOTE_EXIT); 5917eaec467SJohn Baldwin 5921a29c806SOlivier Houchard /* 593cfb5f768SJonathan Anderson * If this is a process with a descriptor, we may not need to deliver 594cfb5f768SJonathan Anderson * a signal to the parent. proctree_lock is held over 595cfb5f768SJonathan Anderson * procdesc_exit() to serialize concurrent calls to close() and 596cfb5f768SJonathan Anderson * exit(). 597cfb5f768SJonathan Anderson */ 598015cd8dcSMateusz Guzik signal_parent = 0; 599cfb5f768SJonathan Anderson if (p->p_procdesc == NULL || procdesc_exit(p)) { 600cfb5f768SJonathan Anderson /* 601cfb5f768SJonathan Anderson * Notify parent that we're gone. If parent has the 602cfb5f768SJonathan Anderson * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 603cfb5f768SJonathan Anderson * notify process 1 instead (and hope it will handle this 604cfb5f768SJonathan Anderson * situation). 605df8bae1dSRodney W. Grimes */ 606d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 60790af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 608cfb5f768SJonathan Anderson if (p->p_pptr->p_sigacts->ps_flag & 609cfb5f768SJonathan Anderson (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 610276c5169SJohn Baldwin struct proc *pp; 611276c5169SJohn Baldwin 61290af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 613276c5169SJohn Baldwin pp = p->p_pptr; 614f591779bSSeigo Tanimura PROC_UNLOCK(pp); 6152c054ce9SMateusz Guzik proc_reparent(p, p->p_reaper, true); 61655b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 617f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 618007abb3dSKonstantin Belousov 619245f17d4SJoerg Wunsch /* 620007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 6216fae832aSKonstantin Belousov * executing waitpid(2) with our pid, he will 622245f17d4SJoerg Wunsch * continue. 623245f17d4SJoerg Wunsch */ 6247f05b035SAlfred Perlstein wakeup(pp); 62590af4afaSJohn Baldwin } else 62690af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 627245f17d4SJoerg Wunsch 628015cd8dcSMateusz Guzik if (p->p_pptr == p->p_reaper || p->p_pptr == initproc) { 629015cd8dcSMateusz Guzik signal_parent = 1; 630015cd8dcSMateusz Guzik } else if (p->p_sigparent != 0) { 631015cd8dcSMateusz Guzik if (p->p_sigparent == SIGCHLD) { 632015cd8dcSMateusz Guzik signal_parent = 1; 633015cd8dcSMateusz Guzik } else { /* LINUX thread */ 634015cd8dcSMateusz Guzik signal_parent = 2; 635015cd8dcSMateusz Guzik } 636ebceaf6dSDavid Xu } 637cfb5f768SJonathan Anderson } else 638cfb5f768SJonathan Anderson PROC_LOCK(p->p_pptr); 639d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 640696058c3SJulian Elischer 641015cd8dcSMateusz Guzik if (signal_parent == 1) { 642015cd8dcSMateusz Guzik childproc_exited(p); 643015cd8dcSMateusz Guzik } else if (signal_parent == 2) { 644015cd8dcSMateusz Guzik kern_psignal(p->p_pptr, p->p_sigparent); 645015cd8dcSMateusz Guzik } 646015cd8dcSMateusz Guzik 6477e588b92SMateusz Guzik /* Tell the prison that we are gone. */ 6487e588b92SMateusz Guzik prison_proc_free(p->p_ucred->cr_prison); 6497e588b92SMateusz Guzik 650eb30c1c0SPeter Wemm /* 651f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 652f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 653f71e748dSDavid Xu * for signal queue at the time when the state is set. 654f71e748dSDavid Xu */ 655f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 656f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 657f71e748dSDavid Xu 658f71e748dSDavid Xu /* 659462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 660a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 661a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 662ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 663ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 664ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 665ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 666ddf9c4f7SJohn Baldwin * proc lock. 667eb30c1c0SPeter Wemm */ 668ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 669e3d3e828SMateusz Guzik cv_broadcast(&p->p_pwait); 670982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 671982d11f8SJeff Roberson PROC_SLOCK(p); 672e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 673d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 674871684b8SBruce Evans 675f6f230feSJeff Roberson /* 676a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 677a140976eSAttilio Rao */ 6785c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 679a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 6805c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 681a140976eSAttilio Rao 682a140976eSAttilio Rao /* 683696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 684e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 685696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 686e602ba25SJulian Elischer */ 687e602ba25SJulian Elischer thread_exit(); 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes 69025f6e35aSPoul-Henning Kamp 69125f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 69225f6e35aSPoul-Henning Kamp struct abort2_args { 69325f6e35aSPoul-Henning Kamp char *why; 69425f6e35aSPoul-Henning Kamp int nargs; 69525f6e35aSPoul-Henning Kamp void **args; 69625f6e35aSPoul-Henning Kamp }; 69725f6e35aSPoul-Henning Kamp #endif 69825f6e35aSPoul-Henning Kamp 69925f6e35aSPoul-Henning Kamp int 7008451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap) 70125f6e35aSPoul-Henning Kamp { 70225f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 70325f6e35aSPoul-Henning Kamp struct sbuf *sb; 70425f6e35aSPoul-Henning Kamp void *uargs[16]; 70525f6e35aSPoul-Henning Kamp int error, i, sig; 70625f6e35aSPoul-Henning Kamp 70725f6e35aSPoul-Henning Kamp /* 70825f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 70925f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 71025f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 71125f6e35aSPoul-Henning Kamp */ 71225f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 71325f6e35aSPoul-Henning Kamp sbuf_clear(sb); 71425f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 71525f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 71625f6e35aSPoul-Henning Kamp /* 71725f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 71825f6e35aSPoul-Henning Kamp * abort2() was called improperly 71925f6e35aSPoul-Henning Kamp */ 72025f6e35aSPoul-Henning Kamp sig = SIGKILL; 72125f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 72225f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 72325f6e35aSPoul-Henning Kamp goto out; 7244218a731SPoul-Henning Kamp if (uap->nargs > 0) { 72525f6e35aSPoul-Henning Kamp if (uap->args == NULL) 72625f6e35aSPoul-Henning Kamp goto out; 72725f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 72825f6e35aSPoul-Henning Kamp if (error != 0) 72925f6e35aSPoul-Henning Kamp goto out; 7304218a731SPoul-Henning Kamp } 73125f6e35aSPoul-Henning Kamp /* 73225f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 73325f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 73425f6e35aSPoul-Henning Kamp */ 73525f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 73625f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 73725f6e35aSPoul-Henning Kamp if (error < 0) 73825f6e35aSPoul-Henning Kamp goto out; 73925f6e35aSPoul-Henning Kamp } else { 74025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 74125f6e35aSPoul-Henning Kamp } 7424218a731SPoul-Henning Kamp if (uap->nargs > 0) { 74325f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 74425f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 74525f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 74625f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 74725f6e35aSPoul-Henning Kamp } 74825f6e35aSPoul-Henning Kamp /* 74925f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 75025f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 75125f6e35aSPoul-Henning Kamp * from user-space succeed. 75225f6e35aSPoul-Henning Kamp */ 75325f6e35aSPoul-Henning Kamp sig = SIGABRT; 75425f6e35aSPoul-Henning Kamp out: 75525f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 75625f6e35aSPoul-Henning Kamp sbuf_trim(sb); 75725f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 75825f6e35aSPoul-Henning Kamp } 75925f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 76025f6e35aSPoul-Henning Kamp sbuf_finish(sb); 76125f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 76225f6e35aSPoul-Henning Kamp sbuf_delete(sb); 763b4490c6eSKonstantin Belousov exit1(td, 0, sig); 76425f6e35aSPoul-Henning Kamp return (0); 76525f6e35aSPoul-Henning Kamp } 76625f6e35aSPoul-Henning Kamp 76725f6e35aSPoul-Henning Kamp 768b2f9e8b1SBruce Evans #ifdef COMPAT_43 769234216efSMatthew Dillon /* 770a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 771234216efSMatthew Dillon */ 77226f9a767SRodney W. Grimes int 773830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 774df8bae1dSRodney W. Grimes { 775b7e23e82SJohn Baldwin int error, status; 776df8bae1dSRodney W. Grimes 777b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 778b7e23e82SJohn Baldwin if (error == 0) 779b7e23e82SJohn Baldwin td->td_retval[1] = status; 780b7e23e82SJohn Baldwin return (error); 781df8bae1dSRodney W. Grimes } 782b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 783df8bae1dSRodney W. Grimes 784234216efSMatthew Dillon /* 785a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 786234216efSMatthew Dillon */ 78726f9a767SRodney W. Grimes int 788f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap) 789df8bae1dSRodney W. Grimes { 79078c85e8dSJohn Baldwin struct rusage ru, *rup; 791b7e23e82SJohn Baldwin int error, status; 792b40ce416SJulian Elischer 79378c85e8dSJohn Baldwin if (uap->rusage != NULL) 79478c85e8dSJohn Baldwin rup = &ru; 79578c85e8dSJohn Baldwin else 79678c85e8dSJohn Baldwin rup = NULL; 79778c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 798d30e66e5SJilles Tjoelker if (uap->status != NULL && error == 0 && td->td_retval[0] != 0) 799b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 800d30e66e5SJilles Tjoelker if (uap->rusage != NULL && error == 0 && td->td_retval[0] != 0) 801b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 802b7e23e82SJohn Baldwin return (error); 803df8bae1dSRodney W. Grimes } 804df8bae1dSRodney W. Grimes 805f13b5a0fSKonstantin Belousov int 806f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap) 807f13b5a0fSKonstantin Belousov { 808f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 809f13b5a0fSKonstantin Belousov siginfo_t si, *sip; 810f13b5a0fSKonstantin Belousov idtype_t idtype; 811f13b5a0fSKonstantin Belousov id_t id; 812a2a85596SKonstantin Belousov int error, status; 813f13b5a0fSKonstantin Belousov 814f13b5a0fSKonstantin Belousov idtype = uap->idtype; 815f13b5a0fSKonstantin Belousov id = uap->id; 816f13b5a0fSKonstantin Belousov 817f13b5a0fSKonstantin Belousov if (uap->wrusage != NULL) 818f13b5a0fSKonstantin Belousov wrup = &wru; 819f13b5a0fSKonstantin Belousov else 820f13b5a0fSKonstantin Belousov wrup = NULL; 821f13b5a0fSKonstantin Belousov 822f13b5a0fSKonstantin Belousov if (uap->info != NULL) { 823f13b5a0fSKonstantin Belousov sip = &si; 824f13b5a0fSKonstantin Belousov bzero(sip, sizeof(*sip)); 825f13b5a0fSKonstantin Belousov } else 826f13b5a0fSKonstantin Belousov sip = NULL; 827f13b5a0fSKonstantin Belousov 828f13b5a0fSKonstantin Belousov /* 829f13b5a0fSKonstantin Belousov * We expect all callers of wait6() to know about WEXITED and 830f13b5a0fSKonstantin Belousov * WTRAPPED. 831f13b5a0fSKonstantin Belousov */ 832f13b5a0fSKonstantin Belousov error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip); 833f13b5a0fSKonstantin Belousov 834d30e66e5SJilles Tjoelker if (uap->status != NULL && error == 0 && td->td_retval[0] != 0) 835f13b5a0fSKonstantin Belousov error = copyout(&status, uap->status, sizeof(status)); 836d30e66e5SJilles Tjoelker if (uap->wrusage != NULL && error == 0 && td->td_retval[0] != 0) 837f13b5a0fSKonstantin Belousov error = copyout(&wru, uap->wrusage, sizeof(wru)); 838f13b5a0fSKonstantin Belousov if (uap->info != NULL && error == 0) 839f13b5a0fSKonstantin Belousov error = copyout(&si, uap->info, sizeof(si)); 840f13b5a0fSKonstantin Belousov return (error); 841f13b5a0fSKonstantin Belousov } 842f13b5a0fSKonstantin Belousov 843324fb6beSRobert Watson /* 844324fb6beSRobert Watson * Reap the remains of a zombie process and optionally return status and 845324fb6beSRobert Watson * rusage. Asserts and will release both the proctree_lock and the process 846324fb6beSRobert Watson * lock as part of its work. 847324fb6beSRobert Watson */ 848cfb5f768SJonathan Anderson void 849f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options) 850324fb6beSRobert Watson { 851324fb6beSRobert Watson struct proc *q, *t; 852324fb6beSRobert Watson 853324fb6beSRobert Watson sx_assert(&proctree_lock, SA_XLOCKED); 854324fb6beSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 855324fb6beSRobert Watson KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 856324fb6beSRobert Watson 8572ca66c1eSMateusz Guzik mtx_spin_wait_unlocked(&p->p_slock); 8582ca66c1eSMateusz Guzik 859324fb6beSRobert Watson q = td->td_proc; 860f13b5a0fSKonstantin Belousov 861324fb6beSRobert Watson if (status) 862b4490c6eSKonstantin Belousov *status = KW_EXITCODE(p->p_xexit, p->p_xsig); 863324fb6beSRobert Watson if (options & WNOWAIT) { 864324fb6beSRobert Watson /* 865324fb6beSRobert Watson * Only poll, returning the status. Caller does not wish to 866324fb6beSRobert Watson * release the proc struct just yet. 867324fb6beSRobert Watson */ 868324fb6beSRobert Watson PROC_UNLOCK(p); 869324fb6beSRobert Watson sx_xunlock(&proctree_lock); 870324fb6beSRobert Watson return; 871324fb6beSRobert Watson } 872324fb6beSRobert Watson 873324fb6beSRobert Watson PROC_LOCK(q); 874324fb6beSRobert Watson sigqueue_take(p->p_ksi); 875324fb6beSRobert Watson PROC_UNLOCK(q); 876324fb6beSRobert Watson 877324fb6beSRobert Watson /* 878324fb6beSRobert Watson * If we got the child via a ptrace 'attach', we need to give it back 879324fb6beSRobert Watson * to the old parent. 880324fb6beSRobert Watson */ 8812c054ce9SMateusz Guzik if (p->p_oppid != p->p_pptr->p_pid) { 882c209e3e2SJohn Baldwin PROC_UNLOCK(p); 883d7359980SKonstantin Belousov t = proc_realparent(p); 884d7359980SKonstantin Belousov PROC_LOCK(t); 885324fb6beSRobert Watson PROC_LOCK(p); 886515b7a0bSJohn Baldwin CTR2(KTR_PTRACE, 887515b7a0bSJohn Baldwin "wait: traced child %d moved back to parent %d", p->p_pid, 888515b7a0bSJohn Baldwin t->p_pid); 8892c054ce9SMateusz Guzik proc_reparent(p, t, false); 890324fb6beSRobert Watson PROC_UNLOCK(p); 891ad6eec7bSJohn Baldwin pksignal(t, SIGCHLD, p->p_ksi); 892324fb6beSRobert Watson wakeup(t); 893324fb6beSRobert Watson cv_broadcast(&p->p_pwait); 894324fb6beSRobert Watson PROC_UNLOCK(t); 895324fb6beSRobert Watson sx_xunlock(&proctree_lock); 896324fb6beSRobert Watson return; 897324fb6beSRobert Watson } 898c209e3e2SJohn Baldwin PROC_UNLOCK(p); 899324fb6beSRobert Watson 900324fb6beSRobert Watson /* 901324fb6beSRobert Watson * Remove other references to this process to ensure we have an 902324fb6beSRobert Watson * exclusive reference. 903324fb6beSRobert Watson */ 9041e9a1bf5SMateusz Guzik sx_xlock(&zombproc_lock); 905324fb6beSRobert Watson LIST_REMOVE(p, p_list); /* off zombproc */ 9061e9a1bf5SMateusz Guzik sx_xunlock(&zombproc_lock); 9073d3e6793SMateusz Guzik sx_xlock(PIDHASHLOCK(p->p_pid)); 9083d3e6793SMateusz Guzik LIST_REMOVE(p, p_hash); 9093d3e6793SMateusz Guzik sx_xunlock(PIDHASHLOCK(p->p_pid)); 910324fb6beSRobert Watson LIST_REMOVE(p, p_sibling); 911237623b0SKonstantin Belousov reaper_abandon_children(p, true); 91234ebdceaSMateusz Guzik reaper_clear(p); 91334ebdceaSMateusz Guzik proc_id_clear(PROC_ID_PID, p->p_pid); 9147335ed90SKonstantin Belousov PROC_LOCK(p); 9152e39e24fSKonstantin Belousov clear_orphan(p); 9167335ed90SKonstantin Belousov PROC_UNLOCK(p); 917324fb6beSRobert Watson leavepgrp(p); 918cfb5f768SJonathan Anderson if (p->p_procdesc != NULL) 919cfb5f768SJonathan Anderson procdesc_reap(p); 920324fb6beSRobert Watson sx_xunlock(&proctree_lock); 921324fb6beSRobert Watson 9229e590ff0SKonstantin Belousov PROC_LOCK(p); 9239e590ff0SKonstantin Belousov knlist_detach(p->p_klist); 9249e590ff0SKonstantin Belousov p->p_klist = NULL; 9259e590ff0SKonstantin Belousov PROC_UNLOCK(p); 9269e590ff0SKonstantin Belousov 927324fb6beSRobert Watson /* 928fbe503d4SMateusz Guzik * Removal from allproc list and process group list paired with 929fbe503d4SMateusz Guzik * PROC_LOCK which was executed during that time should guarantee 930fbe503d4SMateusz Guzik * nothing can reach this process anymore. As such further locking 931fbe503d4SMateusz Guzik * is unnecessary. 932324fb6beSRobert Watson */ 933b4490c6eSKonstantin Belousov p->p_xexit = p->p_xsig = 0; /* XXX: why? */ 934fbe503d4SMateusz Guzik 935324fb6beSRobert Watson PROC_LOCK(q); 936324fb6beSRobert Watson ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 937324fb6beSRobert Watson PROC_UNLOCK(q); 938324fb6beSRobert Watson 939324fb6beSRobert Watson /* 940324fb6beSRobert Watson * Decrement the count of procs running with this uid. 941324fb6beSRobert Watson */ 942324fb6beSRobert Watson (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 943324fb6beSRobert Watson 944324fb6beSRobert Watson /* 945097055e2SEdward Tomasz Napierala * Destroy resource accounting information associated with the process. 946097055e2SEdward Tomasz Napierala */ 947afcc55f3SEdward Tomasz Napierala #ifdef RACCT 9484b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 949b38520f0SEdward Tomasz Napierala PROC_LOCK(p); 950b38520f0SEdward Tomasz Napierala racct_sub(p, RACCT_NPROC, 1); 951b38520f0SEdward Tomasz Napierala PROC_UNLOCK(p); 9524b5c9cf6SEdward Tomasz Napierala } 953afcc55f3SEdward Tomasz Napierala #endif 954b38520f0SEdward Tomasz Napierala racct_proc_exit(p); 955097055e2SEdward Tomasz Napierala 956097055e2SEdward Tomasz Napierala /* 957324fb6beSRobert Watson * Free credentials, arguments, and sigacts. 958324fb6beSRobert Watson */ 959324fb6beSRobert Watson crfree(p->p_ucred); 960daf63fd2SMateusz Guzik proc_set_cred(p, NULL); 961324fb6beSRobert Watson pargs_drop(p->p_args); 962324fb6beSRobert Watson p->p_args = NULL; 963324fb6beSRobert Watson sigacts_free(p->p_sigacts); 964324fb6beSRobert Watson p->p_sigacts = NULL; 965324fb6beSRobert Watson 966324fb6beSRobert Watson /* 967324fb6beSRobert Watson * Do any thread-system specific cleanups. 968324fb6beSRobert Watson */ 969324fb6beSRobert Watson thread_wait(p); 970324fb6beSRobert Watson 971324fb6beSRobert Watson /* 972324fb6beSRobert Watson * Give vm and machine-dependent layer a chance to free anything that 973324fb6beSRobert Watson * cpu_exit couldn't release while still running in process context. 974324fb6beSRobert Watson */ 975324fb6beSRobert Watson vm_waitproc(p); 976324fb6beSRobert Watson #ifdef MAC 977324fb6beSRobert Watson mac_proc_destroy(p); 978324fb6beSRobert Watson #endif 9796520495aSAdrian Chadd 980324fb6beSRobert Watson KASSERT(FIRST_THREAD_IN_PROC(p), 981324fb6beSRobert Watson ("proc_reap: no residual thread!")); 982324fb6beSRobert Watson uma_zfree(proc_zone, p); 9834b48959fSKonstantin Belousov atomic_add_int(&nprocs, -1); 984324fb6beSRobert Watson } 985324fb6beSRobert Watson 986dcd43281SKonstantin Belousov static int 987f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, 98857c74f5bSJohn Baldwin int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo, 98957c74f5bSJohn Baldwin int check_only) 990dcd43281SKonstantin Belousov { 991f13b5a0fSKonstantin Belousov struct rusage *rup; 992dcd43281SKonstantin Belousov 993db62ced2SJaakko Heinonen sx_assert(&proctree_lock, SA_XLOCKED); 994db62ced2SJaakko Heinonen 995dcd43281SKonstantin Belousov PROC_LOCK(p); 996f13b5a0fSKonstantin Belousov 997f13b5a0fSKonstantin Belousov switch (idtype) { 998f13b5a0fSKonstantin Belousov case P_ALL: 99953bf545dSMariusz Zaborski if (p->p_procdesc != NULL) { 100053bf545dSMariusz Zaborski PROC_UNLOCK(p); 100153bf545dSMariusz Zaborski return (0); 100253bf545dSMariusz Zaborski } 1003f13b5a0fSKonstantin Belousov break; 1004f13b5a0fSKonstantin Belousov case P_PID: 1005f13b5a0fSKonstantin Belousov if (p->p_pid != (pid_t)id) { 1006dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1007dcd43281SKonstantin Belousov return (0); 1008dcd43281SKonstantin Belousov } 1009f13b5a0fSKonstantin Belousov break; 1010f13b5a0fSKonstantin Belousov case P_PGID: 1011f13b5a0fSKonstantin Belousov if (p->p_pgid != (pid_t)id) { 1012f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1013f13b5a0fSKonstantin Belousov return (0); 1014f13b5a0fSKonstantin Belousov } 1015f13b5a0fSKonstantin Belousov break; 1016f13b5a0fSKonstantin Belousov case P_SID: 1017f13b5a0fSKonstantin Belousov if (p->p_session->s_sid != (pid_t)id) { 1018f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1019f13b5a0fSKonstantin Belousov return (0); 1020f13b5a0fSKonstantin Belousov } 1021f13b5a0fSKonstantin Belousov break; 1022f13b5a0fSKonstantin Belousov case P_UID: 1023f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_uid != (uid_t)id) { 1024f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1025f13b5a0fSKonstantin Belousov return (0); 1026f13b5a0fSKonstantin Belousov } 1027f13b5a0fSKonstantin Belousov break; 1028f13b5a0fSKonstantin Belousov case P_GID: 1029f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_gid != (gid_t)id) { 1030f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1031f13b5a0fSKonstantin Belousov return (0); 1032f13b5a0fSKonstantin Belousov } 1033f13b5a0fSKonstantin Belousov break; 1034f13b5a0fSKonstantin Belousov case P_JAILID: 1035b1051d92SMateusz Guzik if (p->p_ucred->cr_prison->pr_id != (int)id) { 1036f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1037f13b5a0fSKonstantin Belousov return (0); 1038f13b5a0fSKonstantin Belousov } 1039f13b5a0fSKonstantin Belousov break; 1040f13b5a0fSKonstantin Belousov /* 1041f13b5a0fSKonstantin Belousov * It seems that the thread structures get zeroed out 1042f13b5a0fSKonstantin Belousov * at process exit. This makes it impossible to 1043f13b5a0fSKonstantin Belousov * support P_SETID, P_CID or P_CPUID. 1044f13b5a0fSKonstantin Belousov */ 1045f13b5a0fSKonstantin Belousov default: 1046f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1047f13b5a0fSKonstantin Belousov return (0); 1048f13b5a0fSKonstantin Belousov } 1049f13b5a0fSKonstantin Belousov 1050dcd43281SKonstantin Belousov if (p_canwait(td, p)) { 1051dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1052dcd43281SKonstantin Belousov return (0); 1053dcd43281SKonstantin Belousov } 1054dcd43281SKonstantin Belousov 1055f13b5a0fSKonstantin Belousov if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) { 1056f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1057f13b5a0fSKonstantin Belousov return (0); 1058f13b5a0fSKonstantin Belousov } 1059f13b5a0fSKonstantin Belousov 1060dcd43281SKonstantin Belousov /* 1061dcd43281SKonstantin Belousov * This special case handles a kthread spawned by linux_clone 1062dcd43281SKonstantin Belousov * (see linux_misc.c). The linux_wait4 and linux_waitpid 1063dcd43281SKonstantin Belousov * functions need to be able to distinguish between waiting 1064dcd43281SKonstantin Belousov * on a process and waiting on a thread. It is a thread if 1065dcd43281SKonstantin Belousov * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 1066dcd43281SKonstantin Belousov * signifies we want to wait for threads and not processes. 1067dcd43281SKonstantin Belousov */ 1068dcd43281SKonstantin Belousov if ((p->p_sigparent != SIGCHLD) ^ 1069dcd43281SKonstantin Belousov ((options & WLINUXCLONE) != 0)) { 1070dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1071dcd43281SKonstantin Belousov return (0); 1072dcd43281SKonstantin Belousov } 1073dcd43281SKonstantin Belousov 1074f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1075f13b5a0fSKonstantin Belousov bzero(siginfo, sizeof(*siginfo)); 1076f13b5a0fSKonstantin Belousov siginfo->si_errno = 0; 1077f13b5a0fSKonstantin Belousov 1078f13b5a0fSKonstantin Belousov /* 1079f13b5a0fSKonstantin Belousov * SUSv4 requires that the si_signo value is always 1080f13b5a0fSKonstantin Belousov * SIGCHLD. Obey it despite the rfork(2) interface 1081f13b5a0fSKonstantin Belousov * allows to request other signal for child exit 1082f13b5a0fSKonstantin Belousov * notification. 1083f13b5a0fSKonstantin Belousov */ 1084f13b5a0fSKonstantin Belousov siginfo->si_signo = SIGCHLD; 1085f13b5a0fSKonstantin Belousov 1086f13b5a0fSKonstantin Belousov /* 1087f13b5a0fSKonstantin Belousov * This is still a rough estimate. We will fix the 1088f13b5a0fSKonstantin Belousov * cases TRAPPED, STOPPED, and CONTINUED later. 1089f13b5a0fSKonstantin Belousov */ 1090b4490c6eSKonstantin Belousov if (WCOREDUMP(p->p_xsig)) { 1091f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_DUMPED; 1092b4490c6eSKonstantin Belousov siginfo->si_status = WTERMSIG(p->p_xsig); 1093b4490c6eSKonstantin Belousov } else if (WIFSIGNALED(p->p_xsig)) { 1094f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_KILLED; 1095b4490c6eSKonstantin Belousov siginfo->si_status = WTERMSIG(p->p_xsig); 1096b20a9aa9SJilles Tjoelker } else { 1097f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_EXITED; 1098b4490c6eSKonstantin Belousov siginfo->si_status = p->p_xexit; 1099b20a9aa9SJilles Tjoelker } 1100f13b5a0fSKonstantin Belousov 1101f13b5a0fSKonstantin Belousov siginfo->si_pid = p->p_pid; 1102f13b5a0fSKonstantin Belousov siginfo->si_uid = p->p_ucred->cr_uid; 1103f13b5a0fSKonstantin Belousov 1104f13b5a0fSKonstantin Belousov /* 1105f13b5a0fSKonstantin Belousov * The si_addr field would be useful additional 1106f13b5a0fSKonstantin Belousov * detail, but apparently the PC value may be lost 1107f13b5a0fSKonstantin Belousov * when we reach this point. bzero() above sets 1108f13b5a0fSKonstantin Belousov * siginfo->si_addr to NULL. 1109f13b5a0fSKonstantin Belousov */ 1110f13b5a0fSKonstantin Belousov } 1111f13b5a0fSKonstantin Belousov 1112f13b5a0fSKonstantin Belousov /* 1113f13b5a0fSKonstantin Belousov * There should be no reason to limit resources usage info to 1114f13b5a0fSKonstantin Belousov * exited processes only. A snapshot about any resources used 1115f13b5a0fSKonstantin Belousov * by a stopped process may be exactly what is needed. 1116f13b5a0fSKonstantin Belousov */ 1117f13b5a0fSKonstantin Belousov if (wrusage != NULL) { 1118f13b5a0fSKonstantin Belousov rup = &wrusage->wru_self; 1119f13b5a0fSKonstantin Belousov *rup = p->p_ru; 11205c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 1121f13b5a0fSKonstantin Belousov calcru(p, &rup->ru_utime, &rup->ru_stime); 11225c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 1123f13b5a0fSKonstantin Belousov 1124f13b5a0fSKonstantin Belousov rup = &wrusage->wru_children; 1125f13b5a0fSKonstantin Belousov *rup = p->p_stats->p_cru; 1126f13b5a0fSKonstantin Belousov calccru(p, &rup->ru_utime, &rup->ru_stime); 1127f13b5a0fSKonstantin Belousov } 1128f13b5a0fSKonstantin Belousov 112957c74f5bSJohn Baldwin if (p->p_state == PRS_ZOMBIE && !check_only) { 1130f13b5a0fSKonstantin Belousov proc_reap(td, p, status, options); 1131dcd43281SKonstantin Belousov return (-1); 1132dcd43281SKonstantin Belousov } 1133dcd43281SKonstantin Belousov return (1); 1134dcd43281SKonstantin Belousov } 1135dcd43281SKonstantin Belousov 1136b7e23e82SJohn Baldwin int 11377eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 11387eaec467SJohn Baldwin struct rusage *rusage) 1139df8bae1dSRodney W. Grimes { 1140f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 1141f13b5a0fSKonstantin Belousov idtype_t idtype; 1142f13b5a0fSKonstantin Belousov id_t id; 1143f13b5a0fSKonstantin Belousov int ret; 1144f13b5a0fSKonstantin Belousov 1145538375d4SKonstantin Belousov /* 1146538375d4SKonstantin Belousov * Translate the special pid values into the (idtype, pid) 1147538375d4SKonstantin Belousov * pair for kern_wait6. The WAIT_MYPGRP case is handled by 1148538375d4SKonstantin Belousov * kern_wait6() on its own. 1149538375d4SKonstantin Belousov */ 1150f13b5a0fSKonstantin Belousov if (pid == WAIT_ANY) { 1151f13b5a0fSKonstantin Belousov idtype = P_ALL; 1152f13b5a0fSKonstantin Belousov id = 0; 1153ea293f3fSKonstantin Belousov } else if (pid < 0) { 1154f13b5a0fSKonstantin Belousov idtype = P_PGID; 1155f13b5a0fSKonstantin Belousov id = (id_t)-pid; 1156ea293f3fSKonstantin Belousov } else { 1157f13b5a0fSKonstantin Belousov idtype = P_PID; 1158f13b5a0fSKonstantin Belousov id = (id_t)pid; 1159f13b5a0fSKonstantin Belousov } 1160538375d4SKonstantin Belousov 1161f13b5a0fSKonstantin Belousov if (rusage != NULL) 1162f13b5a0fSKonstantin Belousov wrup = &wru; 1163f13b5a0fSKonstantin Belousov else 1164f13b5a0fSKonstantin Belousov wrup = NULL; 1165538375d4SKonstantin Belousov 1166f13b5a0fSKonstantin Belousov /* 1167f13b5a0fSKonstantin Belousov * For backward compatibility we implicitly add flags WEXITED 1168f13b5a0fSKonstantin Belousov * and WTRAPPED here. 1169f13b5a0fSKonstantin Belousov */ 1170f13b5a0fSKonstantin Belousov options |= WEXITED | WTRAPPED; 1171f13b5a0fSKonstantin Belousov ret = kern_wait6(td, idtype, id, status, options, wrup, NULL); 1172f13b5a0fSKonstantin Belousov if (rusage != NULL) 1173f13b5a0fSKonstantin Belousov *rusage = wru.wru_self; 1174f13b5a0fSKonstantin Belousov return (ret); 1175f13b5a0fSKonstantin Belousov } 1176f13b5a0fSKonstantin Belousov 1177435da985SKonstantin Belousov static void 1178435da985SKonstantin Belousov report_alive_proc(struct thread *td, struct proc *p, siginfo_t *siginfo, 1179435da985SKonstantin Belousov int *status, int options, int si_code) 1180435da985SKonstantin Belousov { 1181435da985SKonstantin Belousov bool cont; 1182435da985SKonstantin Belousov 1183435da985SKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 1184435da985SKonstantin Belousov sx_assert(&proctree_lock, SA_XLOCKED); 1185435da985SKonstantin Belousov MPASS(si_code == CLD_TRAPPED || si_code == CLD_STOPPED || 1186435da985SKonstantin Belousov si_code == CLD_CONTINUED); 1187435da985SKonstantin Belousov 1188435da985SKonstantin Belousov cont = si_code == CLD_CONTINUED; 1189435da985SKonstantin Belousov if ((options & WNOWAIT) == 0) { 1190435da985SKonstantin Belousov if (cont) 1191435da985SKonstantin Belousov p->p_flag &= ~P_CONTINUED; 1192435da985SKonstantin Belousov else 1193435da985SKonstantin Belousov p->p_flag |= P_WAITED; 1194435da985SKonstantin Belousov PROC_LOCK(td->td_proc); 1195435da985SKonstantin Belousov sigqueue_take(p->p_ksi); 1196435da985SKonstantin Belousov PROC_UNLOCK(td->td_proc); 1197435da985SKonstantin Belousov } 1198435da985SKonstantin Belousov sx_xunlock(&proctree_lock); 1199435da985SKonstantin Belousov if (siginfo != NULL) { 1200435da985SKonstantin Belousov siginfo->si_code = si_code; 1201435da985SKonstantin Belousov siginfo->si_status = cont ? SIGCONT : p->p_xsig; 1202435da985SKonstantin Belousov } 1203435da985SKonstantin Belousov if (status != NULL) 1204435da985SKonstantin Belousov *status = cont ? SIGCONT : W_STOPCODE(p->p_xsig); 1205435da985SKonstantin Belousov PROC_UNLOCK(p); 1206435da985SKonstantin Belousov td->td_retval[0] = p->p_pid; 1207435da985SKonstantin Belousov } 1208435da985SKonstantin Belousov 1209f13b5a0fSKonstantin Belousov int 1210f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, 1211f13b5a0fSKonstantin Belousov int options, struct __wrusage *wrusage, siginfo_t *siginfo) 1212f13b5a0fSKonstantin Belousov { 1213324fb6beSRobert Watson struct proc *p, *q; 12143a41ec6aSEd Schouten pid_t pid; 1215dcd43281SKonstantin Belousov int error, nfound, ret; 12167beb6082SMateusz Guzik bool report; 1217df8bae1dSRodney W. Grimes 1218f13b5a0fSKonstantin Belousov AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */ 1219f13b5a0fSKonstantin Belousov AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */ 12202ef24ddeSRobert Watson AUDIT_ARG_VALUE(options); 1221de3007e8SWayne Salamon 1222b40ce416SJulian Elischer q = td->td_proc; 1223f13b5a0fSKonstantin Belousov 1224a2a85596SKonstantin Belousov if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1225a545089eSKonstantin Belousov PROC_LOCK(q); 1226f13b5a0fSKonstantin Belousov id = (id_t)q->p_pgid; 1227a545089eSKonstantin Belousov PROC_UNLOCK(q); 1228f13b5a0fSKonstantin Belousov idtype = P_PGID; 1229f591779bSSeigo Tanimura } 1230f13b5a0fSKonstantin Belousov 1231f528c3fdSDavid E. O'Brien /* If we don't know the option, just return. */ 1232f13b5a0fSKonstantin Belousov if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT | 1233f13b5a0fSKonstantin Belousov WEXITED | WTRAPPED | WLINUXCLONE)) != 0) 12346dc958b9SJohn Baldwin return (EINVAL); 1235f13b5a0fSKonstantin Belousov if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) { 1236f13b5a0fSKonstantin Belousov /* 1237f13b5a0fSKonstantin Belousov * We will be unable to find any matching processes, 1238f13b5a0fSKonstantin Belousov * because there are no known events to look for. 1239f13b5a0fSKonstantin Belousov * Prefer to return error instead of blocking 1240f13b5a0fSKonstantin Belousov * indefinitely. 1241f13b5a0fSKonstantin Belousov */ 1242f13b5a0fSKonstantin Belousov return (EINVAL); 1243f13b5a0fSKonstantin Belousov } 1244f13b5a0fSKonstantin Belousov 1245df8bae1dSRodney W. Grimes loop: 1246902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 1247902c0d82SDavid Xu PROC_LOCK(q); 1248902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 1249902c0d82SDavid Xu PROC_UNLOCK(q); 1250902c0d82SDavid Xu } 1251d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 12526776bfebSMateusz Guzik loop_locked: 12536776bfebSMateusz Guzik nfound = 0; 12542e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 12553a41ec6aSEd Schouten pid = p->p_pid; 1256f13b5a0fSKonstantin Belousov ret = proc_to_reap(td, p, idtype, id, status, options, 125757c74f5bSJohn Baldwin wrusage, siginfo, 0); 1258dcd43281SKonstantin Belousov if (ret == 0) 1259df8bae1dSRodney W. Grimes continue; 12606776bfebSMateusz Guzik else if (ret != 1) { 12613a41ec6aSEd Schouten td->td_retval[0] = pid; 1262d7aadbf9SJohn Baldwin return (0); 12633a41ec6aSEd Schouten } 1264dcd43281SKonstantin Belousov 12656776bfebSMateusz Guzik nfound++; 1266746b6e81SMateusz Guzik PROC_LOCK_ASSERT(p, MA_OWNED); 1267435da985SKonstantin Belousov 1268f13b5a0fSKonstantin Belousov if ((options & WTRAPPED) != 0 && 12697beb6082SMateusz Guzik (p->p_flag & P_TRACED) != 0) { 12707beb6082SMateusz Guzik PROC_SLOCK(p); 12717beb6082SMateusz Guzik report = 12727beb6082SMateusz Guzik ((p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) && 1273435da985SKonstantin Belousov p->p_suspcount == p->p_numthreads && 12747beb6082SMateusz Guzik (p->p_flag & P_WAITED) == 0); 12758bf6ff22SMateusz Guzik PROC_SUNLOCK(p); 12767beb6082SMateusz Guzik if (report) { 1277515b7a0bSJohn Baldwin CTR4(KTR_PTRACE, 1278435da985SKonstantin Belousov "wait: returning trapped pid %d status %#x " 1279435da985SKonstantin Belousov "(xstat %d) xthread %d", 1280b4490c6eSKonstantin Belousov p->p_pid, W_STOPCODE(p->p_xsig), p->p_xsig, 1281435da985SKonstantin Belousov p->p_xthread != NULL ? 1282435da985SKonstantin Belousov p->p_xthread->td_tid : -1); 12837beb6082SMateusz Guzik report_alive_proc(td, p, siginfo, status, 12847beb6082SMateusz Guzik options, CLD_TRAPPED); 1285f13b5a0fSKonstantin Belousov return (0); 1286f13b5a0fSKonstantin Belousov } 12877beb6082SMateusz Guzik } 1288f13b5a0fSKonstantin Belousov if ((options & WUNTRACED) != 0 && 12897beb6082SMateusz Guzik (p->p_flag & P_STOPPED_SIG) != 0) { 12907beb6082SMateusz Guzik PROC_SLOCK(p); 12917beb6082SMateusz Guzik report = (p->p_suspcount == p->p_numthreads && 12927beb6082SMateusz Guzik ((p->p_flag & P_WAITED) == 0)); 12938bf6ff22SMateusz Guzik PROC_SUNLOCK(p); 12947beb6082SMateusz Guzik if (report) { 12957beb6082SMateusz Guzik report_alive_proc(td, p, siginfo, status, 12967beb6082SMateusz Guzik options, CLD_STOPPED); 1297b7e23e82SJohn Baldwin return (0); 1298df8bae1dSRodney W. Grimes } 12997beb6082SMateusz Guzik } 1300f13b5a0fSKonstantin Belousov if ((options & WCONTINUED) != 0 && 1301f13b5a0fSKonstantin Belousov (p->p_flag & P_CONTINUED) != 0) { 1302435da985SKonstantin Belousov report_alive_proc(td, p, siginfo, status, options, 1303435da985SKonstantin Belousov CLD_CONTINUED); 1304dcdc6c36SKonstantin Belousov return (0); 13056ee093fbSMike Barcroft } 1306d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 1307d7aadbf9SJohn Baldwin } 1308dcd43281SKonstantin Belousov 1309dcd43281SKonstantin Belousov /* 1310dcd43281SKonstantin Belousov * Look in the orphans list too, to allow the parent to 1311dcd43281SKonstantin Belousov * collect it's child exit status even if child is being 1312dcd43281SKonstantin Belousov * debugged. 1313dcd43281SKonstantin Belousov * 1314dcd43281SKonstantin Belousov * Debugger detaches from the parent upon successful 1315dcd43281SKonstantin Belousov * switch-over from parent to child. At this point due to 1316dcd43281SKonstantin Belousov * re-parenting the parent loses the child to debugger and a 1317dcd43281SKonstantin Belousov * wait4(2) call would report that it has no children to wait 1318dcd43281SKonstantin Belousov * for. By maintaining a list of orphans we allow the parent 1319dcd43281SKonstantin Belousov * to successfully wait until the child becomes a zombie. 1320dcd43281SKonstantin Belousov */ 132157c74f5bSJohn Baldwin if (nfound == 0) { 1322dcd43281SKonstantin Belousov LIST_FOREACH(p, &q->p_orphans, p_orphan) { 132357c74f5bSJohn Baldwin ret = proc_to_reap(td, p, idtype, id, NULL, options, 132457c74f5bSJohn Baldwin NULL, NULL, 1); 132557c74f5bSJohn Baldwin if (ret != 0) { 132657c74f5bSJohn Baldwin KASSERT(ret != -1, ("reaped an orphan (pid %d)", 132757c74f5bSJohn Baldwin (int)td->td_retval[0])); 1328746b6e81SMateusz Guzik PROC_UNLOCK(p); 1329dcd43281SKonstantin Belousov nfound++; 133057c74f5bSJohn Baldwin break; 133157c74f5bSJohn Baldwin } 133257c74f5bSJohn Baldwin } 1333dcd43281SKonstantin Belousov } 1334d7aadbf9SJohn Baldwin if (nfound == 0) { 1335d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1336d7aadbf9SJohn Baldwin return (ECHILD); 1337d7aadbf9SJohn Baldwin } 1338b7e23e82SJohn Baldwin if (options & WNOHANG) { 1339d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1340d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 1341d7aadbf9SJohn Baldwin return (0); 1342d7aadbf9SJohn Baldwin } 1343d7aadbf9SJohn Baldwin PROC_LOCK(q); 134495992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 134595992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 1346d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 13476776bfebSMateusz Guzik goto loop_locked; 13486776bfebSMateusz Guzik } 13496776bfebSMateusz Guzik sx_xunlock(&proctree_lock); 13506776bfebSMateusz Guzik error = msleep(q, &q->p_mtx, PWAIT | PCATCH | PDROP, "wait", 0); 13514ae89b95SJohn Baldwin if (error) 1352d7aadbf9SJohn Baldwin return (error); 1353d7aadbf9SJohn Baldwin goto loop; 1354d7aadbf9SJohn Baldwin } 1355df8bae1dSRodney W. Grimes 1356df8bae1dSRodney W. Grimes /* 135798f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 135898f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 1359df8bae1dSRodney W. Grimes */ 1360df8bae1dSRodney W. Grimes void 13612c054ce9SMateusz Guzik proc_reparent(struct proc *child, struct proc *parent, bool set_oppid) 1362df8bae1dSRodney W. Grimes { 1363df8bae1dSRodney W. Grimes 13644e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 1365c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 1366df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 1367df8bae1dSRodney W. Grimes return; 1368df8bae1dSRodney W. Grimes 1369ff766807SDavid Xu PROC_LOCK(child->p_pptr); 1370ff766807SDavid Xu sigqueue_take(child->p_ksi); 1371ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 1372b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 1373b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1374dcd43281SKonstantin Belousov 13752e39e24fSKonstantin Belousov clear_orphan(child); 1376dcd43281SKonstantin Belousov if (child->p_flag & P_TRACED) { 1377d7359980SKonstantin Belousov if (LIST_EMPTY(&child->p_pptr->p_orphans)) { 1378d7359980SKonstantin Belousov child->p_treeflag |= P_TREE_FIRST_ORPHAN; 1379d7359980SKonstantin Belousov LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, 1380d7359980SKonstantin Belousov p_orphan); 1381d7359980SKonstantin Belousov } else { 1382ba78d6b7SMark Johnston LIST_INSERT_AFTER(LIST_FIRST(&child->p_pptr->p_orphans), 1383ba78d6b7SMark Johnston child, p_orphan); 1384d7359980SKonstantin Belousov } 1385d7359980SKonstantin Belousov child->p_treeflag |= P_TREE_ORPHANED; 1386dcd43281SKonstantin Belousov } 1387dcd43281SKonstantin Belousov 1388df8bae1dSRodney W. Grimes child->p_pptr = parent; 13892c054ce9SMateusz Guzik if (set_oppid) 13902c054ce9SMateusz Guzik child->p_oppid = parent->p_pid; 1391df8bae1dSRodney W. Grimes } 1392