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" 415d217f17SJohn Birrell #include "opt_kdtrace.h" 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43cfb5f768SJonathan Anderson #include "opt_procdesc.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 475fdb8324SBruce Evans #include <sys/sysproto.h> 48cfb5f768SJonathan Anderson #include <sys/capability.h> 4975b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 501c5bb3eaSPeter Wemm #include <sys/kernel.h> 51a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 52f34fa851SJohn Baldwin #include <sys/lock.h> 5335e0e5b3SJohn Baldwin #include <sys/mutex.h> 54df8bae1dSRodney W. Grimes #include <sys/proc.h> 55cfb5f768SJonathan Anderson #include <sys/procdesc.h> 562a024a2bSSean Eric Fagan #include <sys/pioctl.h> 57413628a7SBjoern A. Zeeb #include <sys/jail.h> 58df8bae1dSRodney W. Grimes #include <sys/tty.h> 59df8bae1dSRodney W. Grimes #include <sys/wait.h> 60eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 617a6b989bSJohn Baldwin #include <sys/vnode.h> 62097055e2SEdward Tomasz Napierala #include <sys/racct.h> 63df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 6425f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 65797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 66b43179fbSJeff Roberson #include <sys/sched.h> 671005a129SJohn Baldwin #include <sys/sx.h> 68c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6925f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 70df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 717c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 72797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 735d217f17SJohn Birrell #include <sys/sdt.h> 74780dc5a8SPeter Wemm #include <sys/shm.h> 75780dc5a8SPeter Wemm #include <sys/sem.h> 766c84de02SJohn Baldwin #ifdef KTRACE 776c84de02SJohn Baldwin #include <sys/ktrace.h> 786c84de02SJohn Baldwin #endif 79780dc5a8SPeter Wemm 80fcf7f27aSRobert Watson #include <security/audit/audit.h> 81aed55708SRobert Watson #include <security/mac/mac_framework.h> 82fcf7f27aSRobert Watson 83df8bae1dSRodney W. Grimes #include <vm/vm.h> 84eb30c1c0SPeter Wemm #include <vm/vm_extern.h> 857a6b989bSJohn Baldwin #include <vm/vm_param.h> 86efeaf95aSDavid Greenman #include <vm/pmap.h> 87efeaf95aSDavid Greenman #include <vm/vm_map.h> 882d21129dSAlan Cox #include <vm/vm_page.h> 89c897b813SJeff Roberson #include <vm/uma.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); 9779856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, , exit, exit); 985d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , exit, 0, "int"); 995d217f17SJohn Birrell 100c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 101c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 102c0bc2867SGleb Smirnoff 1032e39e24fSKonstantin Belousov static void 1042e39e24fSKonstantin Belousov clear_orphan(struct proc *p) 1052e39e24fSKonstantin Belousov { 1062e39e24fSKonstantin Belousov 1072e39e24fSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 1082e39e24fSKonstantin Belousov 1092e39e24fSKonstantin Belousov if (p->p_flag & P_ORPHAN) { 1102e39e24fSKonstantin Belousov LIST_REMOVE(p, p_orphan); 1112e39e24fSKonstantin Belousov p->p_flag &= ~P_ORPHAN; 1122e39e24fSKonstantin Belousov } 1132e39e24fSKonstantin Belousov } 1142e39e24fSKonstantin Belousov 115df8bae1dSRodney W. Grimes /* 116873fbcd7SRobert Watson * exit -- death of process. 117df8bae1dSRodney W. Grimes */ 118fc0b1dbfSBruce Evans void 1198451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 120df8bae1dSRodney W. Grimes { 121b40ce416SJulian Elischer 122b40ce416SJulian Elischer exit1(td, W_EXITCODE(uap->rval, 0)); 123df8bae1dSRodney W. Grimes /* NOTREACHED */ 124df8bae1dSRodney W. Grimes } 125df8bae1dSRodney W. Grimes 126df8bae1dSRodney W. Grimes /* 127873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 128873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 129873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 130df8bae1dSRodney W. Grimes */ 131fc0b1dbfSBruce Evans void 132830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv) 133df8bae1dSRodney W. Grimes { 134276c5169SJohn Baldwin struct proc *p, *nq, *q; 13579deba82SMatthew Dillon struct vnode *vtmp; 136bc093719SEd Schouten struct vnode *ttyvp = NULL; 13791d5354aSJohn Baldwin struct plimit *plim; 13857051fdcSTor Egge int locked; 139df8bae1dSRodney W. Grimes 140e17660e7SKris Kennaway mtx_assert(&Giant, MA_NOTOWNED); 1410cddd8f0SMatthew Dillon 142276c5169SJohn Baldwin p = td->td_proc; 1435486ffc8SMarius Strobl /* 1445486ffc8SMarius Strobl * XXX in case we're rebooting we just let init die in order to 1455486ffc8SMarius Strobl * work around an unsolved stack overflow seen very late during 1465486ffc8SMarius Strobl * shutdown on sparc64 when the gmirror worker process exists. 1475486ffc8SMarius Strobl */ 1485486ffc8SMarius Strobl if (p == initproc && rebooting == 0) { 1495f7bd355SPoul-Henning Kamp printf("init died (signal %d, exit %d)\n", 150df8bae1dSRodney W. Grimes WTERMSIG(rv), WEXITSTATUS(rv)); 1515f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 1525f7bd355SPoul-Henning Kamp } 1532c1011f7SJohn Dyson 1547a6b989bSJohn Baldwin /* 1552c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 1567a6b989bSJohn Baldwin */ 157e602ba25SJulian Elischer PROC_LOCK(p); 1587ab24ea3SJulian Elischer while (p->p_flag & P_HADTHREADS) { 159e602ba25SJulian Elischer /* 160773e3b7dSJohn Baldwin * First check if some other thread got here before us. 161773e3b7dSJohn Baldwin * If so, act appropriately: exit or suspend. 162e602ba25SJulian Elischer */ 163e602ba25SJulian Elischer thread_suspend_check(0); 164e602ba25SJulian Elischer 165e602ba25SJulian Elischer /* 166e602ba25SJulian Elischer * Kill off the other threads. This requires 1676111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 1686111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 1696111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 170e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 1716111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 1726111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 1736111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 174b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 1756111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 1766111dcd2SJohn Baldwin * other threads exits. 177b370279eSDavid Xu * If there is already a thread singler after resumption, 1786111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 179b370279eSDavid Xu * re-check all suspension request, the thread should 180b370279eSDavid Xu * either be suspended there or exit. 181e602ba25SJulian Elischer */ 1827ab24ea3SJulian Elischer if (!thread_single(SINGLE_EXIT)) 1837ab24ea3SJulian Elischer break; 1846111dcd2SJohn Baldwin 185e602ba25SJulian Elischer /* 186e602ba25SJulian Elischer * All other activity in this process is now stopped. 187ed062c8dSJulian Elischer * Threading support has been turned off. 188e602ba25SJulian Elischer */ 189e602ba25SJulian Elischer } 1901c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 1911c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 19258c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 19306ad42b2SJohn Baldwin /* 19406ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 19506ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 19606ad42b2SJohn Baldwin * released their reference to us. Note that if they have 19706ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 19806ad42b2SJohn Baldwin * via PIOCCONT. 19906ad42b2SJohn Baldwin */ 20006ad42b2SJohn Baldwin _STOPEVENT(p, S_EXIT, rv); 20106ad42b2SJohn Baldwin 20206ad42b2SJohn Baldwin /* 203*0f14f15bSJohn Baldwin * Ignore any pending request to stop due to a stop signal. 204*0f14f15bSJohn Baldwin * Once P_WEXIT is set, future requests will be ignored as 205*0f14f15bSJohn Baldwin * well. 206*0f14f15bSJohn Baldwin */ 207*0f14f15bSJohn Baldwin p->p_flag &= ~P_STOPPED_SIG; 208*0f14f15bSJohn Baldwin KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 209*0f14f15bSJohn Baldwin 210*0f14f15bSJohn Baldwin /* 21106ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 21206ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 21306ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 21406ad42b2SJohn Baldwin */ 215e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 21606ad42b2SJohn Baldwin wakeup(&p->p_stype); 21706ad42b2SJohn Baldwin 21806ad42b2SJohn Baldwin /* 21906ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 22006ad42b2SJohn Baldwin * release their reference. 22106ad42b2SJohn Baldwin */ 22206ad42b2SJohn Baldwin while (p->p_lock > 0) 22306ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 224ebceaf6dSDavid Xu 225bb63fddeSAlexander Leidinger p->p_xstat = rv; /* Let event handler change exit status */ 226e602ba25SJulian Elischer PROC_UNLOCK(p); 2271c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 2281c4bcd05SJeff Roberson callout_drain(&p->p_limco); 229b40ce416SJulian Elischer 23000c28d96SRobert Watson #ifdef AUDIT 23100c28d96SRobert Watson /* 23200c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 23300c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 23400c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 23500c28d96SRobert Watson * what the return value is. 23600c28d96SRobert Watson */ 23714961ba7SRobert Watson AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0); 23800c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 23900c28d96SRobert Watson #endif 24000c28d96SRobert Watson 2417a6b989bSJohn Baldwin /* Are we a task leader? */ 2422c1011f7SJohn Dyson if (p == p->p_leader) { 243c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 2442c1011f7SJohn Dyson q = p->p_peers; 245776e0b36SJohn Baldwin while (q != NULL) { 246776e0b36SJohn Baldwin PROC_LOCK(q); 2478451d0ddSKip Macy kern_psignal(q, SIGKILL); 248776e0b36SJohn Baldwin PROC_UNLOCK(q); 2492c1011f7SJohn Dyson q = q->p_peers; 2502c1011f7SJohn Dyson } 251c6544064SJohn Baldwin while (p->p_peers != NULL) 252c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 253c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 2542c1011f7SJohn Dyson } 2552c1011f7SJohn Dyson 256fed06968SJulian Elischer /* 257e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 2586111dcd2SJohn Baldwin * E.g. SYSV IPC stuff 259fed06968SJulian Elischer * XXX what if one of these generates an error? 260fed06968SJulian Elischer */ 26175b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exit, p); 262a914fb6bSJohn Baldwin 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 265df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 266df8bae1dSRodney W. Grimes */ 267a914fb6bSJohn Baldwin PROC_LOCK(p); 268bb63fddeSAlexander Leidinger rv = p->p_xstat; /* Event handler could change exit status */ 269a282253aSJulian Elischer stopprofclock(p); 270df8bae1dSRodney W. Grimes p->p_flag &= ~(P_TRACED | P_PPWAIT); 2715499ea01SJohn Baldwin 2725499ea01SJohn Baldwin /* 2735499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 2745499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 2755499ea01SJohn Baldwin */ 2765499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 2775499ea01SJohn Baldwin callout_stop(&p->p_itcallout) == 0) { 2785499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 2795499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 2805499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 2815499ea01SJohn Baldwin ("realtime timer is still armed")); 2825499ea01SJohn Baldwin } 28396d7f8efSTim J. Robbins PROC_UNLOCK(p); 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes /* 286831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 287831d27a9SDon Lewis * F_SETOWN with our pid. 288831d27a9SDon Lewis */ 289831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 290831d27a9SDon Lewis 291831d27a9SDon Lewis /* 292c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 293c0bc2867SGleb Smirnoff */ 294c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 295c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 296c0bc2867SGleb Smirnoff 297c0bc2867SGleb Smirnoff /* 298df8bae1dSRodney W. Grimes * Close open files and release open-file table. 299df8bae1dSRodney W. Grimes * This may block! 300df8bae1dSRodney W. Grimes */ 301edf6699aSAlfred Perlstein fdfree(td); 302df8bae1dSRodney W. Grimes 303a914fb6bSJohn Baldwin /* 304a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 305a2587073SPoul-Henning Kamp * stop before we return to userland 306a2587073SPoul-Henning Kamp */ 307a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 308a2587073SPoul-Henning Kamp g_waitidle(); 309a2587073SPoul-Henning Kamp 310a2587073SPoul-Henning Kamp /* 311a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 312a914fb6bSJohn Baldwin */ 313c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 31479fc0bf4SMike Smith if (p->p_leader->p_peers) { 31579fc0bf4SMike Smith q = p->p_leader; 31679fc0bf4SMike Smith while (q->p_peers != p) 31779fc0bf4SMike Smith q = q->p_peers; 31879fc0bf4SMike Smith q->p_peers = p->p_peers; 3197f05b035SAlfred Perlstein wakeup(p->p_leader); 32079fc0bf4SMike Smith } 321c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 32279fc0bf4SMike Smith 32357051fdcSTor Egge vmspace_exit(td); 324df8bae1dSRodney W. Grimes 325ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 326df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 327eaaaf190SEd Schouten struct session *sp = p->p_session; 328eaaaf190SEd Schouten struct tty *tp; 329df8bae1dSRodney W. Grimes 330eaaaf190SEd Schouten /* 331eaaaf190SEd Schouten * s_ttyp is not zero'd; we use this to indicate that 332eaaaf190SEd Schouten * the session once had a controlling terminal. (for 333eaaaf190SEd Schouten * logging and informational purposes) 334eaaaf190SEd Schouten */ 335bc093719SEd Schouten SESS_LOCK(sp); 336bc093719SEd Schouten ttyvp = sp->s_ttyvp; 337eaaaf190SEd Schouten tp = sp->s_ttyp; 338bc093719SEd Schouten sp->s_ttyvp = NULL; 3398dc9b4cfSEd Schouten sp->s_ttydp = NULL; 340eaaaf190SEd Schouten sp->s_leader = NULL; 341bc093719SEd Schouten SESS_UNLOCK(sp); 342bc093719SEd Schouten 343df8bae1dSRodney W. Grimes /* 344bc093719SEd Schouten * Signal foreground pgrp and revoke access to 345eaaaf190SEd Schouten * controlling terminal if it has not been revoked 346eaaaf190SEd Schouten * already. 347bc093719SEd Schouten * 348eaaaf190SEd Schouten * Because the TTY may have been revoked in the mean 349eaaaf190SEd Schouten * time and could already have a new session associated 350eaaaf190SEd Schouten * with it, make sure we don't send a SIGHUP to a 351eaaaf190SEd Schouten * foreground process group that does not belong to this 352eaaaf190SEd Schouten * session. 353df8bae1dSRodney W. Grimes */ 354bc093719SEd Schouten 355eaaaf190SEd Schouten if (tp != NULL) { 356bc093719SEd Schouten tty_lock(tp); 357eaaaf190SEd Schouten if (tp->t_session == sp) 358bc093719SEd Schouten tty_signal_pgrp(tp, SIGHUP); 359bc093719SEd Schouten tty_unlock(tp); 360eaaaf190SEd Schouten } 361bc093719SEd Schouten 3627c6d401cSKonstantin Belousov if (ttyvp != NULL) { 363ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 3646728dc16SKonstantin Belousov if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) { 365f591779bSSeigo Tanimura VOP_REVOKE(ttyvp, REVOKEALL); 366bc093719SEd Schouten VOP_UNLOCK(ttyvp, 0); 3676728dc16SKonstantin Belousov } 368ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 369f591779bSSeigo Tanimura } 370f591779bSSeigo Tanimura } 371df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 372ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 373b40ce416SJulian Elischer (void)acct_process(td); 374bc093719SEd Schouten 375bc093719SEd Schouten /* Release the TTY now we've unlocked everything. */ 376bc093719SEd Schouten if (ttyvp != NULL) 377bc093719SEd Schouten vrele(ttyvp); 378df8bae1dSRodney W. Grimes #ifdef KTRACE 3792c255e9dSRobert Watson ktrprocexit(td); 380df8bae1dSRodney W. Grimes #endif 381df8bae1dSRodney W. Grimes /* 382ee42d0a9SDavid Malone * Release reference to text vnode 383ee42d0a9SDavid Malone */ 384ee42d0a9SDavid Malone if ((vtmp = p->p_textvp) != NULL) { 385ee42d0a9SDavid Malone p->p_textvp = NULL; 38657606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(vtmp->v_mount); 387ee42d0a9SDavid Malone vrele(vtmp); 38857606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 389ee42d0a9SDavid Malone } 390ee42d0a9SDavid Malone 391ee42d0a9SDavid Malone /* 392d7aadbf9SJohn Baldwin * Release our limits structure. 393d7aadbf9SJohn Baldwin */ 39491d5354aSJohn Baldwin PROC_LOCK(p); 39591d5354aSJohn Baldwin plim = p->p_limit; 396d7aadbf9SJohn Baldwin p->p_limit = NULL; 39791d5354aSJohn Baldwin PROC_UNLOCK(p); 39891d5354aSJohn Baldwin lim_free(plim); 399d7aadbf9SJohn Baldwin 400cf7d9a8cSDavid Xu tidhash_remove(td); 401cf7d9a8cSDavid Xu 402d7aadbf9SJohn Baldwin /* 403df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 404df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 405df8bae1dSRodney W. Grimes */ 4061005a129SJohn Baldwin sx_xlock(&allproc_lock); 407b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 408b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 409b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4101005a129SJohn Baldwin sx_xunlock(&allproc_lock); 411df8bae1dSRodney W. Grimes 41237f84a60SJohn Baldwin /* 4131bba2a94SJohn Baldwin * Call machine-dependent code to release any 4141bba2a94SJohn Baldwin * machine-dependent resources other than the address space. 4151bba2a94SJohn Baldwin * The address space is released by "vmspace_exitfree(p)" in 4161bba2a94SJohn Baldwin * vm_waitproc(). 4171bba2a94SJohn Baldwin */ 4181bba2a94SJohn Baldwin cpu_exit(td); 4191bba2a94SJohn Baldwin 4201bba2a94SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 4211bba2a94SJohn Baldwin 4221bba2a94SJohn Baldwin /* 42337f84a60SJohn Baldwin * Reparent all of our children to init. 42437f84a60SJohn Baldwin */ 4251005a129SJohn Baldwin sx_xlock(&proctree_lock); 4262e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 427a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 4287f05b035SAlfred Perlstein wakeup(initproc); 429a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4302e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 431a914fb6bSJohn Baldwin PROC_LOCK(q); 432c65437a3SJohn Baldwin proc_reparent(q, initproc); 4334ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 434df8bae1dSRodney W. Grimes /* 435df8bae1dSRodney W. Grimes * Traced processes are killed 436df8bae1dSRodney W. Grimes * since their existence means someone is screwing up. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes if (q->p_flag & P_TRACED) { 439904c5ec4SDavid Xu struct thread *temp; 440904c5ec4SDavid Xu 4415085ecb7SKonstantin Belousov /* 4425085ecb7SKonstantin Belousov * Since q was found on our children list, the 4435085ecb7SKonstantin Belousov * proc_reparent() call moved q to the orphan 4445085ecb7SKonstantin Belousov * list due to present P_TRACED flag. Clear 4455085ecb7SKonstantin Belousov * orphan link for q now while q is locked. 4465085ecb7SKonstantin Belousov */ 4475085ecb7SKonstantin Belousov clear_orphan(q); 448c2836532SDavid Xu q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 449904c5ec4SDavid Xu FOREACH_THREAD_IN_PROC(q, temp) 450904c5ec4SDavid Xu temp->td_dbgflags &= ~TDB_SUSPEND; 4518451d0ddSKip Macy kern_psignal(q, SIGKILL); 452c65437a3SJohn Baldwin } 453a914fb6bSJohn Baldwin PROC_UNLOCK(q); 454df8bae1dSRodney W. Grimes } 455df8bae1dSRodney W. Grimes 4565085ecb7SKonstantin Belousov /* 4575085ecb7SKonstantin Belousov * Also get rid of our orphans. 4585085ecb7SKonstantin Belousov */ 4595085ecb7SKonstantin Belousov while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 4605085ecb7SKonstantin Belousov PROC_LOCK(q); 4615085ecb7SKonstantin Belousov clear_orphan(q); 4625085ecb7SKonstantin Belousov PROC_UNLOCK(q); 4635085ecb7SKonstantin Belousov } 4645085ecb7SKonstantin Belousov 4651c4bcd05SJeff Roberson /* Save exit status. */ 466d7aadbf9SJohn Baldwin PROC_LOCK(p); 467cbf4e354SDavid Xu p->p_xthread = td; 4685d217f17SJohn Birrell 4690304c731SJamie Gritton /* Tell the prison that we are gone. */ 470413628a7SBjoern A. Zeeb prison_proc_free(p->p_ucred->cr_prison); 471413628a7SBjoern A. Zeeb 4725d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4735d217f17SJohn Birrell /* 4745d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 4755d217f17SJohn Birrell * has declared an interest. 4765d217f17SJohn Birrell */ 4775d217f17SJohn Birrell if (dtrace_fasttrap_exit) 4785d217f17SJohn Birrell dtrace_fasttrap_exit(p); 4795d217f17SJohn Birrell #endif 4805d217f17SJohn Birrell 4811c4bcd05SJeff Roberson /* 4827a6b989bSJohn Baldwin * Notify interested parties of our demise. 483cb679c38SJonathan Lemon */ 484ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 4857eaec467SJohn Baldwin 4865d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4875d217f17SJohn Birrell int reason = CLD_EXITED; 4885d217f17SJohn Birrell if (WCOREDUMP(rv)) 4895d217f17SJohn Birrell reason = CLD_DUMPED; 4905d217f17SJohn Birrell else if (WIFSIGNALED(rv)) 4915d217f17SJohn Birrell reason = CLD_KILLED; 4925d217f17SJohn Birrell SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 4935d217f17SJohn Birrell #endif 4945d217f17SJohn Birrell 4951a29c806SOlivier Houchard /* 4961a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 4971a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 4981a29c806SOlivier Houchard * can beat us if we don't. 4991a29c806SOlivier Houchard */ 500ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 501cb679c38SJonathan Lemon 502cb679c38SJonathan Lemon /* 503cfb5f768SJonathan Anderson * If this is a process with a descriptor, we may not need to deliver 504cfb5f768SJonathan Anderson * a signal to the parent. proctree_lock is held over 505cfb5f768SJonathan Anderson * procdesc_exit() to serialize concurrent calls to close() and 506cfb5f768SJonathan Anderson * exit(). 507cfb5f768SJonathan Anderson */ 508cfb5f768SJonathan Anderson #ifdef PROCDESC 509cfb5f768SJonathan Anderson if (p->p_procdesc == NULL || procdesc_exit(p)) { 510cfb5f768SJonathan Anderson #endif 511cfb5f768SJonathan Anderson /* 512cfb5f768SJonathan Anderson * Notify parent that we're gone. If parent has the 513cfb5f768SJonathan Anderson * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 514cfb5f768SJonathan Anderson * notify process 1 instead (and hope it will handle this 515cfb5f768SJonathan Anderson * situation). 516df8bae1dSRodney W. Grimes */ 517d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 51890af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 519cfb5f768SJonathan Anderson if (p->p_pptr->p_sigacts->ps_flag & 520cfb5f768SJonathan Anderson (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 521276c5169SJohn Baldwin struct proc *pp; 522276c5169SJohn Baldwin 52390af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 524276c5169SJohn Baldwin pp = p->p_pptr; 525f591779bSSeigo Tanimura PROC_UNLOCK(pp); 526245f17d4SJoerg Wunsch proc_reparent(p, initproc); 52755b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 528f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 529007abb3dSKonstantin Belousov 530245f17d4SJoerg Wunsch /* 531007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 5326fae832aSKonstantin Belousov * executing waitpid(2) with our pid, he will 533245f17d4SJoerg Wunsch * continue. 534245f17d4SJoerg Wunsch */ 5357f05b035SAlfred Perlstein wakeup(pp); 53690af4afaSJohn Baldwin } else 53790af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 538245f17d4SJoerg Wunsch 5396567eef7SDon Lewis if (p->p_pptr == initproc) 5408451d0ddSKip Macy kern_psignal(p->p_pptr, SIGCHLD); 541ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 542ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 543ebceaf6dSDavid Xu childproc_exited(p); 544ebceaf6dSDavid Xu else /* LINUX thread */ 5458451d0ddSKip Macy kern_psignal(p->p_pptr, p->p_sigparent); 546ebceaf6dSDavid Xu } 547cfb5f768SJonathan Anderson #ifdef PROCDESC 548cfb5f768SJonathan Anderson } else 549cfb5f768SJonathan Anderson PROC_LOCK(p->p_pptr); 550cfb5f768SJonathan Anderson #endif 551d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 552696058c3SJulian Elischer 553eb30c1c0SPeter Wemm /* 554f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 555f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 556f71e748dSDavid Xu * for signal queue at the time when the state is set. 557f71e748dSDavid Xu */ 558f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 559f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 560f71e748dSDavid Xu 561f71e748dSDavid Xu /* 562462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 563a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 564a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 565ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 566ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 567ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 568ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 569ddf9c4f7SJohn Baldwin * proc lock. 570eb30c1c0SPeter Wemm */ 571ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 572aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 573982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 574982d11f8SJeff Roberson PROC_SLOCK(p); 575e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 576d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 577871684b8SBruce Evans 578f6f230feSJeff Roberson /* 5797eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 580ad3b9257SJohn-Mark Gurney * late in the game. 581ad3b9257SJohn-Mark Gurney */ 582ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 583ad3b9257SJohn-Mark Gurney 584ad3b9257SJohn-Mark Gurney /* 585a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 586a140976eSAttilio Rao */ 587a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 588a140976eSAttilio Rao 589a140976eSAttilio Rao /* 590696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 591e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 592696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 593e602ba25SJulian Elischer */ 594e602ba25SJulian Elischer thread_exit(); 595df8bae1dSRodney W. Grimes } 596df8bae1dSRodney W. Grimes 59725f6e35aSPoul-Henning Kamp 59825f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 59925f6e35aSPoul-Henning Kamp struct abort2_args { 60025f6e35aSPoul-Henning Kamp char *why; 60125f6e35aSPoul-Henning Kamp int nargs; 60225f6e35aSPoul-Henning Kamp void **args; 60325f6e35aSPoul-Henning Kamp }; 60425f6e35aSPoul-Henning Kamp #endif 60525f6e35aSPoul-Henning Kamp 60625f6e35aSPoul-Henning Kamp int 6078451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap) 60825f6e35aSPoul-Henning Kamp { 60925f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 61025f6e35aSPoul-Henning Kamp struct sbuf *sb; 61125f6e35aSPoul-Henning Kamp void *uargs[16]; 61225f6e35aSPoul-Henning Kamp int error, i, sig; 61325f6e35aSPoul-Henning Kamp 61425f6e35aSPoul-Henning Kamp /* 61525f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 61625f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 61725f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 61825f6e35aSPoul-Henning Kamp */ 61925f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 62025f6e35aSPoul-Henning Kamp sbuf_clear(sb); 62125f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 62225f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 62325f6e35aSPoul-Henning Kamp /* 62425f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 62525f6e35aSPoul-Henning Kamp * abort2() was called improperly 62625f6e35aSPoul-Henning Kamp */ 62725f6e35aSPoul-Henning Kamp sig = SIGKILL; 62825f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 62925f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 63025f6e35aSPoul-Henning Kamp goto out; 6314218a731SPoul-Henning Kamp if (uap->nargs > 0) { 63225f6e35aSPoul-Henning Kamp if (uap->args == NULL) 63325f6e35aSPoul-Henning Kamp goto out; 63425f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 63525f6e35aSPoul-Henning Kamp if (error != 0) 63625f6e35aSPoul-Henning Kamp goto out; 6374218a731SPoul-Henning Kamp } 63825f6e35aSPoul-Henning Kamp /* 63925f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 64025f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 64125f6e35aSPoul-Henning Kamp */ 64225f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 64325f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 64425f6e35aSPoul-Henning Kamp if (error < 0) 64525f6e35aSPoul-Henning Kamp goto out; 64625f6e35aSPoul-Henning Kamp } else { 64725f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 64825f6e35aSPoul-Henning Kamp } 6494218a731SPoul-Henning Kamp if (uap->nargs > 0) { 65025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 65125f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 65225f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 65325f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 65425f6e35aSPoul-Henning Kamp } 65525f6e35aSPoul-Henning Kamp /* 65625f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 65725f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 65825f6e35aSPoul-Henning Kamp * from user-space succeed. 65925f6e35aSPoul-Henning Kamp */ 66025f6e35aSPoul-Henning Kamp sig = SIGABRT; 66125f6e35aSPoul-Henning Kamp out: 66225f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 66325f6e35aSPoul-Henning Kamp sbuf_trim(sb); 66425f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 66525f6e35aSPoul-Henning Kamp } 66625f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 66725f6e35aSPoul-Henning Kamp sbuf_finish(sb); 66825f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 66925f6e35aSPoul-Henning Kamp sbuf_delete(sb); 67025f6e35aSPoul-Henning Kamp exit1(td, W_EXITCODE(0, sig)); 67125f6e35aSPoul-Henning Kamp return (0); 67225f6e35aSPoul-Henning Kamp } 67325f6e35aSPoul-Henning Kamp 67425f6e35aSPoul-Henning Kamp 675b2f9e8b1SBruce Evans #ifdef COMPAT_43 676234216efSMatthew Dillon /* 677a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 678234216efSMatthew Dillon */ 67926f9a767SRodney W. Grimes int 680830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 681df8bae1dSRodney W. Grimes { 682b7e23e82SJohn Baldwin int error, status; 683df8bae1dSRodney W. Grimes 684b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 685b7e23e82SJohn Baldwin if (error == 0) 686b7e23e82SJohn Baldwin td->td_retval[1] = status; 687b7e23e82SJohn Baldwin return (error); 688df8bae1dSRodney W. Grimes } 689b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 690df8bae1dSRodney W. Grimes 691234216efSMatthew Dillon /* 692a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 693234216efSMatthew Dillon */ 69426f9a767SRodney W. Grimes int 6958451d0ddSKip Macy sys_wait4(struct thread *td, struct wait_args *uap) 696df8bae1dSRodney W. Grimes { 69778c85e8dSJohn Baldwin struct rusage ru, *rup; 698b7e23e82SJohn Baldwin int error, status; 699b40ce416SJulian Elischer 70078c85e8dSJohn Baldwin if (uap->rusage != NULL) 70178c85e8dSJohn Baldwin rup = &ru; 70278c85e8dSJohn Baldwin else 70378c85e8dSJohn Baldwin rup = NULL; 70478c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 705b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 706b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 707b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 708b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 709b7e23e82SJohn Baldwin return (error); 710df8bae1dSRodney W. Grimes } 711df8bae1dSRodney W. Grimes 712324fb6beSRobert Watson /* 713324fb6beSRobert Watson * Reap the remains of a zombie process and optionally return status and 714324fb6beSRobert Watson * rusage. Asserts and will release both the proctree_lock and the process 715324fb6beSRobert Watson * lock as part of its work. 716324fb6beSRobert Watson */ 717cfb5f768SJonathan Anderson void 718324fb6beSRobert Watson proc_reap(struct thread *td, struct proc *p, int *status, int options, 719324fb6beSRobert Watson struct rusage *rusage) 720324fb6beSRobert Watson { 721324fb6beSRobert Watson struct proc *q, *t; 722324fb6beSRobert Watson 723324fb6beSRobert Watson sx_assert(&proctree_lock, SA_XLOCKED); 724324fb6beSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 725324fb6beSRobert Watson PROC_SLOCK_ASSERT(p, MA_OWNED); 726324fb6beSRobert Watson KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 727324fb6beSRobert Watson 728324fb6beSRobert Watson q = td->td_proc; 729324fb6beSRobert Watson if (rusage) { 730324fb6beSRobert Watson *rusage = p->p_ru; 731324fb6beSRobert Watson calcru(p, &rusage->ru_utime, &rusage->ru_stime); 732324fb6beSRobert Watson } 733324fb6beSRobert Watson PROC_SUNLOCK(p); 734324fb6beSRobert Watson td->td_retval[0] = p->p_pid; 735324fb6beSRobert Watson if (status) 736324fb6beSRobert Watson *status = p->p_xstat; /* convert to int */ 737324fb6beSRobert Watson if (options & WNOWAIT) { 738324fb6beSRobert Watson /* 739324fb6beSRobert Watson * Only poll, returning the status. Caller does not wish to 740324fb6beSRobert Watson * release the proc struct just yet. 741324fb6beSRobert Watson */ 742324fb6beSRobert Watson PROC_UNLOCK(p); 743324fb6beSRobert Watson sx_xunlock(&proctree_lock); 744324fb6beSRobert Watson return; 745324fb6beSRobert Watson } 746324fb6beSRobert Watson 747324fb6beSRobert Watson PROC_LOCK(q); 748324fb6beSRobert Watson sigqueue_take(p->p_ksi); 749324fb6beSRobert Watson PROC_UNLOCK(q); 750324fb6beSRobert Watson PROC_UNLOCK(p); 751324fb6beSRobert Watson 752324fb6beSRobert Watson /* 753324fb6beSRobert Watson * If we got the child via a ptrace 'attach', we need to give it back 754324fb6beSRobert Watson * to the old parent. 755324fb6beSRobert Watson */ 756324fb6beSRobert Watson if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 757324fb6beSRobert Watson PROC_LOCK(p); 758324fb6beSRobert Watson proc_reparent(p, t); 759f528c3fdSDavid E. O'Brien p->p_oppid = 0; 760324fb6beSRobert Watson PROC_UNLOCK(p); 761ad6eec7bSJohn Baldwin pksignal(t, SIGCHLD, p->p_ksi); 762324fb6beSRobert Watson wakeup(t); 763324fb6beSRobert Watson cv_broadcast(&p->p_pwait); 764324fb6beSRobert Watson PROC_UNLOCK(t); 765324fb6beSRobert Watson sx_xunlock(&proctree_lock); 766324fb6beSRobert Watson return; 767324fb6beSRobert Watson } 768324fb6beSRobert Watson 769324fb6beSRobert Watson /* 770324fb6beSRobert Watson * Remove other references to this process to ensure we have an 771324fb6beSRobert Watson * exclusive reference. 772324fb6beSRobert Watson */ 773324fb6beSRobert Watson sx_xlock(&allproc_lock); 774324fb6beSRobert Watson LIST_REMOVE(p, p_list); /* off zombproc */ 775324fb6beSRobert Watson sx_xunlock(&allproc_lock); 776324fb6beSRobert Watson LIST_REMOVE(p, p_sibling); 7777335ed90SKonstantin Belousov PROC_LOCK(p); 7782e39e24fSKonstantin Belousov clear_orphan(p); 7797335ed90SKonstantin Belousov PROC_UNLOCK(p); 780324fb6beSRobert Watson leavepgrp(p); 781cfb5f768SJonathan Anderson #ifdef PROCDESC 782cfb5f768SJonathan Anderson if (p->p_procdesc != NULL) 783cfb5f768SJonathan Anderson procdesc_reap(p); 784cfb5f768SJonathan Anderson #endif 785324fb6beSRobert Watson sx_xunlock(&proctree_lock); 786324fb6beSRobert Watson 787324fb6beSRobert Watson /* 788324fb6beSRobert Watson * As a side effect of this lock, we know that all other writes to 789324fb6beSRobert Watson * this proc are visible now, so no more locking is needed for p. 790324fb6beSRobert Watson */ 791324fb6beSRobert Watson PROC_LOCK(p); 792324fb6beSRobert Watson p->p_xstat = 0; /* XXX: why? */ 793324fb6beSRobert Watson PROC_UNLOCK(p); 794324fb6beSRobert Watson PROC_LOCK(q); 795324fb6beSRobert Watson ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 796324fb6beSRobert Watson PROC_UNLOCK(q); 797324fb6beSRobert Watson 798324fb6beSRobert Watson /* 799324fb6beSRobert Watson * Decrement the count of procs running with this uid. 800324fb6beSRobert Watson */ 801324fb6beSRobert Watson (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 802324fb6beSRobert Watson 803324fb6beSRobert Watson /* 804097055e2SEdward Tomasz Napierala * Destroy resource accounting information associated with the process. 805097055e2SEdward Tomasz Napierala */ 806afcc55f3SEdward Tomasz Napierala #ifdef RACCT 807b38520f0SEdward Tomasz Napierala PROC_LOCK(p); 808b38520f0SEdward Tomasz Napierala racct_sub(p, RACCT_NPROC, 1); 809b38520f0SEdward Tomasz Napierala PROC_UNLOCK(p); 810afcc55f3SEdward Tomasz Napierala #endif 811b38520f0SEdward Tomasz Napierala racct_proc_exit(p); 812097055e2SEdward Tomasz Napierala 813097055e2SEdward Tomasz Napierala /* 814324fb6beSRobert Watson * Free credentials, arguments, and sigacts. 815324fb6beSRobert Watson */ 816324fb6beSRobert Watson crfree(p->p_ucred); 817324fb6beSRobert Watson p->p_ucred = NULL; 818324fb6beSRobert Watson pargs_drop(p->p_args); 819324fb6beSRobert Watson p->p_args = NULL; 820324fb6beSRobert Watson sigacts_free(p->p_sigacts); 821324fb6beSRobert Watson p->p_sigacts = NULL; 822324fb6beSRobert Watson 823324fb6beSRobert Watson /* 824324fb6beSRobert Watson * Do any thread-system specific cleanups. 825324fb6beSRobert Watson */ 826324fb6beSRobert Watson thread_wait(p); 827324fb6beSRobert Watson 828324fb6beSRobert Watson /* 829324fb6beSRobert Watson * Give vm and machine-dependent layer a chance to free anything that 830324fb6beSRobert Watson * cpu_exit couldn't release while still running in process context. 831324fb6beSRobert Watson */ 832324fb6beSRobert Watson vm_waitproc(p); 833324fb6beSRobert Watson #ifdef MAC 834324fb6beSRobert Watson mac_proc_destroy(p); 835324fb6beSRobert Watson #endif 836324fb6beSRobert Watson KASSERT(FIRST_THREAD_IN_PROC(p), 837324fb6beSRobert Watson ("proc_reap: no residual thread!")); 838324fb6beSRobert Watson uma_zfree(proc_zone, p); 839324fb6beSRobert Watson sx_xlock(&allproc_lock); 840324fb6beSRobert Watson nprocs--; 841324fb6beSRobert Watson sx_xunlock(&allproc_lock); 842324fb6beSRobert Watson } 843324fb6beSRobert Watson 844dcd43281SKonstantin Belousov static int 845dcd43281SKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, pid_t pid, int *status, 846dcd43281SKonstantin Belousov int options, struct rusage *rusage) 847dcd43281SKonstantin Belousov { 848dcd43281SKonstantin Belousov struct proc *q; 849dcd43281SKonstantin Belousov 850db62ced2SJaakko Heinonen sx_assert(&proctree_lock, SA_XLOCKED); 851db62ced2SJaakko Heinonen 852dcd43281SKonstantin Belousov q = td->td_proc; 853dcd43281SKonstantin Belousov PROC_LOCK(p); 854dcd43281SKonstantin Belousov if (pid != WAIT_ANY && p->p_pid != pid && p->p_pgid != -pid) { 855dcd43281SKonstantin Belousov PROC_UNLOCK(p); 856dcd43281SKonstantin Belousov return (0); 857dcd43281SKonstantin Belousov } 858dcd43281SKonstantin Belousov if (p_canwait(td, p)) { 859dcd43281SKonstantin Belousov PROC_UNLOCK(p); 860dcd43281SKonstantin Belousov return (0); 861dcd43281SKonstantin Belousov } 862dcd43281SKonstantin Belousov 863dcd43281SKonstantin Belousov /* 864dcd43281SKonstantin Belousov * This special case handles a kthread spawned by linux_clone 865dcd43281SKonstantin Belousov * (see linux_misc.c). The linux_wait4 and linux_waitpid 866dcd43281SKonstantin Belousov * functions need to be able to distinguish between waiting 867dcd43281SKonstantin Belousov * on a process and waiting on a thread. It is a thread if 868dcd43281SKonstantin Belousov * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 869dcd43281SKonstantin Belousov * signifies we want to wait for threads and not processes. 870dcd43281SKonstantin Belousov */ 871dcd43281SKonstantin Belousov if ((p->p_sigparent != SIGCHLD) ^ 872dcd43281SKonstantin Belousov ((options & WLINUXCLONE) != 0)) { 873dcd43281SKonstantin Belousov PROC_UNLOCK(p); 874dcd43281SKonstantin Belousov return (0); 875dcd43281SKonstantin Belousov } 876dcd43281SKonstantin Belousov 877dcd43281SKonstantin Belousov PROC_SLOCK(p); 878dcd43281SKonstantin Belousov if (p->p_state == PRS_ZOMBIE) { 879dcd43281SKonstantin Belousov proc_reap(td, p, status, options, rusage); 880dcd43281SKonstantin Belousov return (-1); 881dcd43281SKonstantin Belousov } 882dcd43281SKonstantin Belousov PROC_SUNLOCK(p); 883dcd43281SKonstantin Belousov PROC_UNLOCK(p); 884dcd43281SKonstantin Belousov return (1); 885dcd43281SKonstantin Belousov } 886dcd43281SKonstantin Belousov 887b7e23e82SJohn Baldwin int 8887eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 8897eaec467SJohn Baldwin struct rusage *rusage) 890df8bae1dSRodney W. Grimes { 891324fb6beSRobert Watson struct proc *p, *q; 892dcd43281SKonstantin Belousov int error, nfound, ret; 893df8bae1dSRodney W. Grimes 89414961ba7SRobert Watson AUDIT_ARG_PID(pid); 8952ef24ddeSRobert Watson AUDIT_ARG_VALUE(options); 896de3007e8SWayne Salamon 897b40ce416SJulian Elischer q = td->td_proc; 898b7e23e82SJohn Baldwin if (pid == 0) { 899f591779bSSeigo Tanimura PROC_LOCK(q); 900b7e23e82SJohn Baldwin pid = -q->p_pgid; 901f591779bSSeigo Tanimura PROC_UNLOCK(q); 902f591779bSSeigo Tanimura } 903f528c3fdSDavid E. O'Brien /* If we don't know the option, just return. */ 904cbc15844SKonstantin Belousov if (options & ~(WUNTRACED|WNOHANG|WCONTINUED|WNOWAIT|WLINUXCLONE)) 9056dc958b9SJohn Baldwin return (EINVAL); 906df8bae1dSRodney W. Grimes loop: 907902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 908902c0d82SDavid Xu PROC_LOCK(q); 909902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 910902c0d82SDavid Xu PROC_UNLOCK(q); 911902c0d82SDavid Xu } 912df8bae1dSRodney W. Grimes nfound = 0; 913d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 9142e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 915dcd43281SKonstantin Belousov ret = proc_to_reap(td, p, pid, status, options, rusage); 916dcd43281SKonstantin Belousov if (ret == 0) 917df8bae1dSRodney W. Grimes continue; 918dcd43281SKonstantin Belousov else if (ret == 1) 919df8bae1dSRodney W. Grimes nfound++; 920dcd43281SKonstantin Belousov else 921d7aadbf9SJohn Baldwin return (0); 922dcd43281SKonstantin Belousov 923dcd43281SKonstantin Belousov PROC_LOCK(p); 924dcd43281SKonstantin Belousov PROC_SLOCK(p); 9255a2f73e6SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && 9265a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 927a9a64385SJohn Baldwin (p->p_flag & P_WAITED) == 0 && 928b7e23e82SJohn Baldwin (p->p_flag & P_TRACED || options & WUNTRACED)) { 929982d11f8SJeff Roberson PROC_SUNLOCK(p); 930df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 931d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 932b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 933b7e23e82SJohn Baldwin if (status) 934b7e23e82SJohn Baldwin *status = W_STOPCODE(p->p_xstat); 935ebceaf6dSDavid Xu 936ebceaf6dSDavid Xu PROC_LOCK(q); 937ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 938ebceaf6dSDavid Xu PROC_UNLOCK(q); 939e94cc4acSDavid Xu PROC_UNLOCK(p); 940ebceaf6dSDavid Xu 941b7e23e82SJohn Baldwin return (0); 942df8bae1dSRodney W. Grimes } 943982d11f8SJeff Roberson PROC_SUNLOCK(p); 944b7e23e82SJohn Baldwin if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 9456ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 9466ee093fbSMike Barcroft td->td_retval[0] = p->p_pid; 9476ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 9486ee093fbSMike Barcroft 949ebceaf6dSDavid Xu PROC_LOCK(q); 950ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 951ebceaf6dSDavid Xu PROC_UNLOCK(q); 952e94cc4acSDavid Xu PROC_UNLOCK(p); 953ebceaf6dSDavid Xu 954b7e23e82SJohn Baldwin if (status) 955b7e23e82SJohn Baldwin *status = SIGCONT; 956dcdc6c36SKonstantin Belousov return (0); 9576ee093fbSMike Barcroft } 958d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 959d7aadbf9SJohn Baldwin } 960dcd43281SKonstantin Belousov 961dcd43281SKonstantin Belousov /* 962dcd43281SKonstantin Belousov * Look in the orphans list too, to allow the parent to 963dcd43281SKonstantin Belousov * collect it's child exit status even if child is being 964dcd43281SKonstantin Belousov * debugged. 965dcd43281SKonstantin Belousov * 966dcd43281SKonstantin Belousov * Debugger detaches from the parent upon successful 967dcd43281SKonstantin Belousov * switch-over from parent to child. At this point due to 968dcd43281SKonstantin Belousov * re-parenting the parent loses the child to debugger and a 969dcd43281SKonstantin Belousov * wait4(2) call would report that it has no children to wait 970dcd43281SKonstantin Belousov * for. By maintaining a list of orphans we allow the parent 971dcd43281SKonstantin Belousov * to successfully wait until the child becomes a zombie. 972dcd43281SKonstantin Belousov */ 973dcd43281SKonstantin Belousov LIST_FOREACH(p, &q->p_orphans, p_orphan) { 974dcd43281SKonstantin Belousov ret = proc_to_reap(td, p, pid, status, options, rusage); 975dcd43281SKonstantin Belousov if (ret == 0) 976dcd43281SKonstantin Belousov continue; 977dcd43281SKonstantin Belousov else if (ret == 1) 978dcd43281SKonstantin Belousov nfound++; 979dcd43281SKonstantin Belousov else 980dcd43281SKonstantin Belousov return (0); 981dcd43281SKonstantin Belousov } 982d7aadbf9SJohn Baldwin if (nfound == 0) { 983d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 984d7aadbf9SJohn Baldwin return (ECHILD); 985d7aadbf9SJohn Baldwin } 986b7e23e82SJohn Baldwin if (options & WNOHANG) { 987d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 988d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 989d7aadbf9SJohn Baldwin return (0); 990d7aadbf9SJohn Baldwin } 991d7aadbf9SJohn Baldwin PROC_LOCK(q); 992d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 99395992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 99495992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 99595992d56SDavid Xu error = 0; 99695992d56SDavid Xu } else 9977f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 998d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 9994ae89b95SJohn Baldwin if (error) 1000d7aadbf9SJohn Baldwin return (error); 1001d7aadbf9SJohn Baldwin goto loop; 1002d7aadbf9SJohn Baldwin } 1003df8bae1dSRodney W. Grimes 1004df8bae1dSRodney W. Grimes /* 100598f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 100698f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 1007df8bae1dSRodney W. Grimes */ 1008df8bae1dSRodney W. Grimes void 1009830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 1010df8bae1dSRodney W. Grimes { 1011df8bae1dSRodney W. Grimes 10124e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 1013c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 1014df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 1015df8bae1dSRodney W. Grimes return; 1016df8bae1dSRodney W. Grimes 1017ff766807SDavid Xu PROC_LOCK(child->p_pptr); 1018ff766807SDavid Xu sigqueue_take(child->p_ksi); 1019ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 1020b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 1021b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1022dcd43281SKonstantin Belousov 10232e39e24fSKonstantin Belousov clear_orphan(child); 1024dcd43281SKonstantin Belousov if (child->p_flag & P_TRACED) { 1025dcd43281SKonstantin Belousov LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, p_orphan); 1026dcd43281SKonstantin Belousov child->p_flag |= P_ORPHAN; 1027dcd43281SKonstantin Belousov } 1028dcd43281SKonstantin Belousov 1029df8bae1dSRodney W. Grimes child->p_pptr = parent; 1030df8bae1dSRodney W. Grimes } 1031