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" 43db6a20e2SGarrett Wollman 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 465fdb8324SBruce Evans #include <sys/sysproto.h> 4775b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 481c5bb3eaSPeter Wemm #include <sys/kernel.h> 49a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 50f34fa851SJohn Baldwin #include <sys/lock.h> 5135e0e5b3SJohn Baldwin #include <sys/mutex.h> 52df8bae1dSRodney W. Grimes #include <sys/proc.h> 532a024a2bSSean Eric Fagan #include <sys/pioctl.h> 54413628a7SBjoern A. Zeeb #include <sys/jail.h> 55df8bae1dSRodney W. Grimes #include <sys/tty.h> 56df8bae1dSRodney W. Grimes #include <sys/wait.h> 57eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 587a6b989bSJohn Baldwin #include <sys/vnode.h> 59df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 6025f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 61797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 62b43179fbSJeff Roberson #include <sys/sched.h> 631005a129SJohn Baldwin #include <sys/sx.h> 64c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6525f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 66df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 677c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 68797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 695d217f17SJohn Birrell #include <sys/sdt.h> 70780dc5a8SPeter Wemm #include <sys/shm.h> 71780dc5a8SPeter Wemm #include <sys/sem.h> 7229b02909SMarko Zec #include <sys/vimage.h> 736c84de02SJohn Baldwin #ifdef KTRACE 746c84de02SJohn Baldwin #include <sys/ktrace.h> 756c84de02SJohn Baldwin #endif 76780dc5a8SPeter Wemm 77fcf7f27aSRobert Watson #include <security/audit/audit.h> 78aed55708SRobert Watson #include <security/mac/mac_framework.h> 79fcf7f27aSRobert Watson 80df8bae1dSRodney W. Grimes #include <vm/vm.h> 81eb30c1c0SPeter Wemm #include <vm/vm_extern.h> 827a6b989bSJohn Baldwin #include <vm/vm_param.h> 83efeaf95aSDavid Greenman #include <vm/pmap.h> 84efeaf95aSDavid Greenman #include <vm/vm_map.h> 852d21129dSAlan Cox #include <vm/vm_page.h> 86c897b813SJeff Roberson #include <vm/uma.h> 87df8bae1dSRodney W. Grimes 885d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 895d217f17SJohn Birrell #include <sys/dtrace_bsd.h> 905d217f17SJohn Birrell dtrace_execexit_func_t dtrace_fasttrap_exit; 915d217f17SJohn Birrell #endif 925d217f17SJohn Birrell 935d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 945d217f17SJohn Birrell SDT_PROBE_DEFINE(proc, kernel, , exit); 955d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, , exit, 0, "int"); 965d217f17SJohn Birrell 97ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */ 9869a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 9955166637SPoul-Henning Kamp 100c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 101c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 102c0bc2867SGleb Smirnoff 103df8bae1dSRodney W. Grimes /* 104873fbcd7SRobert Watson * exit -- death of process. 105df8bae1dSRodney W. Grimes */ 106fc0b1dbfSBruce Evans void 107830c3153SDag-Erling Smørgrav sys_exit(struct thread *td, struct sys_exit_args *uap) 108df8bae1dSRodney W. Grimes { 109b40ce416SJulian Elischer 110b40ce416SJulian Elischer exit1(td, W_EXITCODE(uap->rval, 0)); 111df8bae1dSRodney W. Grimes /* NOTREACHED */ 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes /* 115873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 116873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 117873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 118df8bae1dSRodney W. Grimes */ 119fc0b1dbfSBruce Evans void 120830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv) 121df8bae1dSRodney W. Grimes { 122276c5169SJohn Baldwin struct proc *p, *nq, *q; 12379deba82SMatthew Dillon struct vnode *vtmp; 124bc093719SEd Schouten struct vnode *ttyvp = NULL; 125d7aadbf9SJohn Baldwin #ifdef KTRACE 126d7aadbf9SJohn Baldwin struct vnode *tracevp; 127a5881ea5SJohn Baldwin struct ucred *tracecred; 128d7aadbf9SJohn Baldwin #endif 12991d5354aSJohn Baldwin struct plimit *plim; 13057051fdcSTor Egge int locked; 131df8bae1dSRodney W. Grimes 132e17660e7SKris Kennaway mtx_assert(&Giant, MA_NOTOWNED); 1330cddd8f0SMatthew Dillon 134276c5169SJohn Baldwin p = td->td_proc; 135e746d950SJohn Baldwin if (p == initproc) { 1365f7bd355SPoul-Henning Kamp printf("init died (signal %d, exit %d)\n", 137df8bae1dSRodney W. Grimes WTERMSIG(rv), WEXITSTATUS(rv)); 1385f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 1395f7bd355SPoul-Henning Kamp } 1402c1011f7SJohn Dyson 1417a6b989bSJohn Baldwin /* 1422c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 1437a6b989bSJohn Baldwin */ 144e602ba25SJulian Elischer PROC_LOCK(p); 1457ab24ea3SJulian Elischer while (p->p_flag & P_HADTHREADS) { 146e602ba25SJulian Elischer /* 147e602ba25SJulian Elischer * First check if some other thread got here before us.. 148e602ba25SJulian Elischer * if so, act apropriatly, (exit or suspend); 149e602ba25SJulian Elischer */ 150e602ba25SJulian Elischer thread_suspend_check(0); 151e602ba25SJulian Elischer 152e602ba25SJulian Elischer /* 153e602ba25SJulian Elischer * Kill off the other threads. This requires 1546111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 1556111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 1566111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 157e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 1586111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 1596111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 1606111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 161b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 1626111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 1636111dcd2SJohn Baldwin * other threads exits. 164b370279eSDavid Xu * If there is already a thread singler after resumption, 1656111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 166b370279eSDavid Xu * re-check all suspension request, the thread should 167b370279eSDavid Xu * either be suspended there or exit. 168e602ba25SJulian Elischer */ 1697ab24ea3SJulian Elischer if (! thread_single(SINGLE_EXIT)) 1707ab24ea3SJulian Elischer break; 1716111dcd2SJohn Baldwin 172e602ba25SJulian Elischer /* 173e602ba25SJulian Elischer * All other activity in this process is now stopped. 174ed062c8dSJulian Elischer * Threading support has been turned off. 175e602ba25SJulian Elischer */ 176e602ba25SJulian Elischer } 1771c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 1781c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 17906ad42b2SJohn Baldwin /* 18006ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 18106ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 18206ad42b2SJohn Baldwin * released their reference to us. Note that if they have 18306ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 18406ad42b2SJohn Baldwin * via PIOCCONT. 18506ad42b2SJohn Baldwin */ 18606ad42b2SJohn Baldwin _STOPEVENT(p, S_EXIT, rv); 18706ad42b2SJohn Baldwin 18806ad42b2SJohn Baldwin /* 18906ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 19006ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 19106ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 19206ad42b2SJohn Baldwin */ 193e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 19406ad42b2SJohn Baldwin wakeup(&p->p_stype); 19506ad42b2SJohn Baldwin 19606ad42b2SJohn Baldwin /* 19706ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 19806ad42b2SJohn Baldwin * release their reference. 19906ad42b2SJohn Baldwin */ 20006ad42b2SJohn Baldwin while (p->p_lock > 0) 20106ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 202ebceaf6dSDavid Xu 203e602ba25SJulian Elischer PROC_UNLOCK(p); 2041c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 2051c4bcd05SJeff Roberson callout_drain(&p->p_limco); 206b40ce416SJulian Elischer 20700c28d96SRobert Watson #ifdef AUDIT 20800c28d96SRobert Watson /* 20900c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 21000c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 21100c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 21200c28d96SRobert Watson * what the return value is. 21300c28d96SRobert Watson */ 21400c28d96SRobert Watson AUDIT_ARG(exit, WEXITSTATUS(rv), 0); 21500c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 21600c28d96SRobert Watson #endif 21700c28d96SRobert Watson 2187a6b989bSJohn Baldwin /* Are we a task leader? */ 2192c1011f7SJohn Dyson if (p == p->p_leader) { 220c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 2212c1011f7SJohn Dyson q = p->p_peers; 222776e0b36SJohn Baldwin while (q != NULL) { 223776e0b36SJohn Baldwin PROC_LOCK(q); 224776e0b36SJohn Baldwin psignal(q, SIGKILL); 225776e0b36SJohn Baldwin PROC_UNLOCK(q); 2262c1011f7SJohn Dyson q = q->p_peers; 2272c1011f7SJohn Dyson } 228c6544064SJohn Baldwin while (p->p_peers != NULL) 229c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 230c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 2312c1011f7SJohn Dyson } 2322c1011f7SJohn Dyson 233fed06968SJulian Elischer /* 234e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 2356111dcd2SJohn Baldwin * E.g. SYSV IPC stuff 236fed06968SJulian Elischer * XXX what if one of these generates an error? 237fed06968SJulian Elischer */ 23875b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exit, p); 239a914fb6bSJohn Baldwin 240df8bae1dSRodney W. Grimes /* 241df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 242df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 243df8bae1dSRodney W. Grimes */ 244a914fb6bSJohn Baldwin PROC_LOCK(p); 245a282253aSJulian Elischer stopprofclock(p); 246df8bae1dSRodney W. Grimes p->p_flag &= ~(P_TRACED | P_PPWAIT); 2475499ea01SJohn Baldwin 2485499ea01SJohn Baldwin /* 2495499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 2505499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 2515499ea01SJohn Baldwin */ 2525499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 2535499ea01SJohn Baldwin callout_stop(&p->p_itcallout) == 0) { 2545499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 2555499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 2565499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 2575499ea01SJohn Baldwin ("realtime timer is still armed")); 2585499ea01SJohn Baldwin } 25996d7f8efSTim J. Robbins PROC_UNLOCK(p); 260df8bae1dSRodney W. Grimes 261df8bae1dSRodney W. Grimes /* 262831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 263831d27a9SDon Lewis * F_SETOWN with our pid. 264831d27a9SDon Lewis */ 265831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 266831d27a9SDon Lewis 267831d27a9SDon Lewis /* 268c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 269c0bc2867SGleb Smirnoff */ 270c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 271c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 272c0bc2867SGleb Smirnoff 273c0bc2867SGleb Smirnoff /* 274df8bae1dSRodney W. Grimes * Close open files and release open-file table. 275df8bae1dSRodney W. Grimes * This may block! 276df8bae1dSRodney W. Grimes */ 277edf6699aSAlfred Perlstein fdfree(td); 278df8bae1dSRodney W. Grimes 279a914fb6bSJohn Baldwin /* 280a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 281a2587073SPoul-Henning Kamp * stop before we return to userland 282a2587073SPoul-Henning Kamp */ 283a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 284a2587073SPoul-Henning Kamp g_waitidle(); 285a2587073SPoul-Henning Kamp 286a2587073SPoul-Henning Kamp /* 287a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 288a914fb6bSJohn Baldwin */ 289c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 29079fc0bf4SMike Smith if (p->p_leader->p_peers) { 29179fc0bf4SMike Smith q = p->p_leader; 29279fc0bf4SMike Smith while (q->p_peers != p) 29379fc0bf4SMike Smith q = q->p_peers; 29479fc0bf4SMike Smith q->p_peers = p->p_peers; 2957f05b035SAlfred Perlstein wakeup(p->p_leader); 29679fc0bf4SMike Smith } 297c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 29879fc0bf4SMike Smith 29957051fdcSTor Egge vmspace_exit(td); 300df8bae1dSRodney W. Grimes 301ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 302df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 303830c3153SDag-Erling Smørgrav struct session *sp; 304df8bae1dSRodney W. Grimes 305f591779bSSeigo Tanimura sp = p->p_session; 306bc093719SEd Schouten 307bc093719SEd Schouten SESS_LOCK(sp); 308bc093719SEd Schouten ttyvp = sp->s_ttyvp; 309bc093719SEd Schouten sp->s_ttyvp = NULL; 310bc093719SEd Schouten SESS_UNLOCK(sp); 311bc093719SEd Schouten 312bc093719SEd Schouten if (ttyvp != NULL) { 313df8bae1dSRodney W. Grimes /* 314df8bae1dSRodney W. Grimes * Controlling process. 315bc093719SEd Schouten * Signal foreground pgrp and revoke access to 316bc093719SEd Schouten * controlling terminal. 317bc093719SEd Schouten * 318bc093719SEd Schouten * There is no need to drain the terminal here, 319bc093719SEd Schouten * because this will be done on revocation. 320df8bae1dSRodney W. Grimes */ 321bc093719SEd Schouten if (sp->s_ttyp != NULL) { 322bc093719SEd Schouten struct tty *tp = sp->s_ttyp; 323bc093719SEd Schouten 324bc093719SEd Schouten tty_lock(tp); 325bc093719SEd Schouten tty_signal_pgrp(tp, SIGHUP); 326bc093719SEd Schouten tty_unlock(tp); 327bc093719SEd Schouten 328df8bae1dSRodney W. Grimes /* 329df8bae1dSRodney W. Grimes * The tty could have been revoked 330df8bae1dSRodney W. Grimes * if we blocked. 331df8bae1dSRodney W. Grimes */ 332bc093719SEd Schouten if (ttyvp->v_type != VBAD) { 333ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 33422db15c0SAttilio Rao VOP_LOCK(ttyvp, LK_EXCLUSIVE); 335f591779bSSeigo Tanimura VOP_REVOKE(ttyvp, REVOKEALL); 336bc093719SEd Schouten VOP_UNLOCK(ttyvp, 0); 337ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 338f591779bSSeigo Tanimura } 339f591779bSSeigo Tanimura } 340df8bae1dSRodney W. Grimes /* 341bc093719SEd Schouten * s_ttyp is not zero'd; we use this to indicate that 342bc093719SEd Schouten * the session once had a controlling terminal. 343df8bae1dSRodney W. Grimes * (for logging and informational purposes) 344df8bae1dSRodney W. Grimes */ 345df8bae1dSRodney W. Grimes } 346f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 347df8bae1dSRodney W. Grimes sp->s_leader = NULL; 348f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 349f591779bSSeigo Tanimura } 350df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 351ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 352b40ce416SJulian Elischer (void)acct_process(td); 353bc093719SEd Schouten 354bc093719SEd Schouten /* Release the TTY now we've unlocked everything. */ 355bc093719SEd Schouten if (ttyvp != NULL) 356bc093719SEd Schouten vrele(ttyvp); 357df8bae1dSRodney W. Grimes #ifdef KTRACE 358df8bae1dSRodney W. Grimes /* 35934a9edafSJohn Baldwin * Disable tracing, then drain any pending records and release 36034a9edafSJohn Baldwin * the trace file. 361df8bae1dSRodney W. Grimes */ 36234a9edafSJohn Baldwin if (p->p_traceflag != 0) { 36334a9edafSJohn Baldwin PROC_LOCK(p); 36434a9edafSJohn Baldwin mtx_lock(&ktrace_mtx); 36534a9edafSJohn Baldwin p->p_traceflag = 0; 36634a9edafSJohn Baldwin mtx_unlock(&ktrace_mtx); 36734a9edafSJohn Baldwin PROC_UNLOCK(p); 3682c255e9dSRobert Watson ktrprocexit(td); 369d7aadbf9SJohn Baldwin PROC_LOCK(p); 3706c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 371a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 372a5881ea5SJohn Baldwin p->p_tracevp = NULL; 373a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 374a5881ea5SJohn Baldwin p->p_tracecred = NULL; 3756c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 376d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 37715088317SBrian Feldman if (tracevp != NULL) { 37857606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(tracevp->v_mount); 379d7aadbf9SJohn Baldwin vrele(tracevp); 38057606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 38115088317SBrian Feldman } 382a5881ea5SJohn Baldwin if (tracecred != NULL) 383a5881ea5SJohn Baldwin crfree(tracecred); 38434a9edafSJohn Baldwin } 385df8bae1dSRodney W. Grimes #endif 386df8bae1dSRodney W. Grimes /* 387ee42d0a9SDavid Malone * Release reference to text vnode 388ee42d0a9SDavid Malone */ 389ee42d0a9SDavid Malone if ((vtmp = p->p_textvp) != NULL) { 390ee42d0a9SDavid Malone p->p_textvp = NULL; 39157606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(vtmp->v_mount); 392ee42d0a9SDavid Malone vrele(vtmp); 39357606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 394ee42d0a9SDavid Malone } 395ee42d0a9SDavid Malone 396ee42d0a9SDavid Malone /* 397d7aadbf9SJohn Baldwin * Release our limits structure. 398d7aadbf9SJohn Baldwin */ 39991d5354aSJohn Baldwin PROC_LOCK(p); 40091d5354aSJohn Baldwin plim = p->p_limit; 401d7aadbf9SJohn Baldwin p->p_limit = NULL; 40291d5354aSJohn Baldwin PROC_UNLOCK(p); 40391d5354aSJohn Baldwin lim_free(plim); 404d7aadbf9SJohn Baldwin 405d7aadbf9SJohn Baldwin /* 406df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 407df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 408df8bae1dSRodney W. Grimes */ 4091005a129SJohn Baldwin sx_xlock(&allproc_lock); 410b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 411b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 412b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4131005a129SJohn Baldwin sx_xunlock(&allproc_lock); 414df8bae1dSRodney W. Grimes 41537f84a60SJohn Baldwin /* 4161bba2a94SJohn Baldwin * Call machine-dependent code to release any 4171bba2a94SJohn Baldwin * machine-dependent resources other than the address space. 4181bba2a94SJohn Baldwin * The address space is released by "vmspace_exitfree(p)" in 4191bba2a94SJohn Baldwin * vm_waitproc(). 4201bba2a94SJohn Baldwin */ 4211bba2a94SJohn Baldwin cpu_exit(td); 4221bba2a94SJohn Baldwin 4231bba2a94SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 4241bba2a94SJohn Baldwin 4251bba2a94SJohn Baldwin /* 42637f84a60SJohn Baldwin * Reparent all of our children to init. 42737f84a60SJohn Baldwin */ 4281005a129SJohn Baldwin sx_xlock(&proctree_lock); 4292e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 430a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 4317f05b035SAlfred Perlstein wakeup(initproc); 432a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4332e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 434a914fb6bSJohn Baldwin PROC_LOCK(q); 435c65437a3SJohn Baldwin proc_reparent(q, initproc); 4364ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 437df8bae1dSRodney W. Grimes /* 438df8bae1dSRodney W. Grimes * Traced processes are killed 439df8bae1dSRodney W. Grimes * since their existence means someone is screwing up. 440df8bae1dSRodney W. Grimes */ 441df8bae1dSRodney W. Grimes if (q->p_flag & P_TRACED) { 442904c5ec4SDavid Xu struct thread *temp; 443904c5ec4SDavid Xu 444c2836532SDavid Xu q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 445904c5ec4SDavid Xu FOREACH_THREAD_IN_PROC(q, temp) 446904c5ec4SDavid Xu temp->td_dbgflags &= ~TDB_SUSPEND; 447df8bae1dSRodney W. Grimes psignal(q, SIGKILL); 448c65437a3SJohn Baldwin } 449a914fb6bSJohn Baldwin PROC_UNLOCK(q); 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes 4521c4bcd05SJeff Roberson /* Save exit status. */ 453d7aadbf9SJohn Baldwin PROC_LOCK(p); 454df8bae1dSRodney W. Grimes p->p_xstat = rv; 455cbf4e354SDavid Xu p->p_xthread = td; 4565d217f17SJohn Birrell 4570304c731SJamie Gritton /* Tell the prison that we are gone. */ 458413628a7SBjoern A. Zeeb prison_proc_free(p->p_ucred->cr_prison); 459413628a7SBjoern A. Zeeb 4605d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4615d217f17SJohn Birrell /* 4625d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 4635d217f17SJohn Birrell * has declared an interest. 4645d217f17SJohn Birrell */ 4655d217f17SJohn Birrell if (dtrace_fasttrap_exit) 4665d217f17SJohn Birrell dtrace_fasttrap_exit(p); 4675d217f17SJohn Birrell #endif 4685d217f17SJohn Birrell 4691c4bcd05SJeff Roberson /* 4707a6b989bSJohn Baldwin * Notify interested parties of our demise. 471cb679c38SJonathan Lemon */ 472ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 4737eaec467SJohn Baldwin 4745d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4755d217f17SJohn Birrell int reason = CLD_EXITED; 4765d217f17SJohn Birrell if (WCOREDUMP(rv)) 4775d217f17SJohn Birrell reason = CLD_DUMPED; 4785d217f17SJohn Birrell else if (WIFSIGNALED(rv)) 4795d217f17SJohn Birrell reason = CLD_KILLED; 4805d217f17SJohn Birrell SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 4815d217f17SJohn Birrell #endif 4825d217f17SJohn Birrell 4831a29c806SOlivier Houchard /* 4841a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 4851a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 4861a29c806SOlivier Houchard * can beat us if we don't. 4871a29c806SOlivier Houchard */ 488ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 489cb679c38SJonathan Lemon 490cb679c38SJonathan Lemon /* 491645682fdSLuoqi Chen * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 492ba1551caSIan Dowse * flag set, or if the handler is set to SIG_IGN, notify process 493ba1551caSIan Dowse * 1 instead (and hope it will handle this situation). 494df8bae1dSRodney W. Grimes */ 495d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 49690af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 49790af4afaSJohn Baldwin if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 498276c5169SJohn Baldwin struct proc *pp; 499276c5169SJohn Baldwin 50090af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 501276c5169SJohn Baldwin pp = p->p_pptr; 502f591779bSSeigo Tanimura PROC_UNLOCK(pp); 503245f17d4SJoerg Wunsch proc_reparent(p, initproc); 50455b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 505f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 506007abb3dSKonstantin Belousov 507245f17d4SJoerg Wunsch /* 508007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 5096fae832aSKonstantin Belousov * executing waitpid(2) with our pid, he will 510245f17d4SJoerg Wunsch * continue. 511245f17d4SJoerg Wunsch */ 5127f05b035SAlfred Perlstein wakeup(pp); 51390af4afaSJohn Baldwin } else 51490af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 515245f17d4SJoerg Wunsch 5166567eef7SDon Lewis if (p->p_pptr == initproc) 5176626c604SJulian Elischer psignal(p->p_pptr, SIGCHLD); 518ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 519ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 520ebceaf6dSDavid Xu childproc_exited(p); 521ebceaf6dSDavid Xu else /* LINUX thread */ 5226567eef7SDon Lewis psignal(p->p_pptr, p->p_sigparent); 523ebceaf6dSDavid Xu } 524d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 525696058c3SJulian Elischer 526eb30c1c0SPeter Wemm /* 527f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 528f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 529f71e748dSDavid Xu * for signal queue at the time when the state is set. 530f71e748dSDavid Xu */ 531f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 532f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 533f71e748dSDavid Xu 534f71e748dSDavid Xu /* 535462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 536a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 537a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 538ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 539ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 540ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 541ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 542ddf9c4f7SJohn Baldwin * proc lock. 543eb30c1c0SPeter Wemm */ 544ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 545aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 546982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 547982d11f8SJeff Roberson PROC_SLOCK(p); 548e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 549d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 550871684b8SBruce Evans 551f6f230feSJeff Roberson /* 5527eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 553ad3b9257SJohn-Mark Gurney * late in the game. 554ad3b9257SJohn-Mark Gurney */ 555ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 556ad3b9257SJohn-Mark Gurney 557ad3b9257SJohn-Mark Gurney /* 558a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 559a140976eSAttilio Rao */ 560a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 561a140976eSAttilio Rao 562a140976eSAttilio Rao /* 563696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 564e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 565696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 566e602ba25SJulian Elischer */ 567e602ba25SJulian Elischer thread_exit(); 568df8bae1dSRodney W. Grimes } 569df8bae1dSRodney W. Grimes 57025f6e35aSPoul-Henning Kamp 57125f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 57225f6e35aSPoul-Henning Kamp struct abort2_args { 57325f6e35aSPoul-Henning Kamp char *why; 57425f6e35aSPoul-Henning Kamp int nargs; 57525f6e35aSPoul-Henning Kamp void **args; 57625f6e35aSPoul-Henning Kamp }; 57725f6e35aSPoul-Henning Kamp #endif 57825f6e35aSPoul-Henning Kamp 57925f6e35aSPoul-Henning Kamp int 58025f6e35aSPoul-Henning Kamp abort2(struct thread *td, struct abort2_args *uap) 58125f6e35aSPoul-Henning Kamp { 58225f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 58325f6e35aSPoul-Henning Kamp struct sbuf *sb; 58425f6e35aSPoul-Henning Kamp void *uargs[16]; 58525f6e35aSPoul-Henning Kamp int error, i, sig; 58625f6e35aSPoul-Henning Kamp 58725f6e35aSPoul-Henning Kamp /* 58825f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 58925f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 59025f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 59125f6e35aSPoul-Henning Kamp */ 59225f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 59325f6e35aSPoul-Henning Kamp sbuf_clear(sb); 59425f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 59525f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 59625f6e35aSPoul-Henning Kamp /* 59725f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 59825f6e35aSPoul-Henning Kamp * abort2() was called improperly 59925f6e35aSPoul-Henning Kamp */ 60025f6e35aSPoul-Henning Kamp sig = SIGKILL; 60125f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 60225f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 60325f6e35aSPoul-Henning Kamp goto out; 6044218a731SPoul-Henning Kamp if (uap->nargs > 0) { 60525f6e35aSPoul-Henning Kamp if (uap->args == NULL) 60625f6e35aSPoul-Henning Kamp goto out; 60725f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 60825f6e35aSPoul-Henning Kamp if (error != 0) 60925f6e35aSPoul-Henning Kamp goto out; 6104218a731SPoul-Henning Kamp } 61125f6e35aSPoul-Henning Kamp /* 61225f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 61325f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 61425f6e35aSPoul-Henning Kamp */ 61525f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 61625f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 61725f6e35aSPoul-Henning Kamp if (error < 0) 61825f6e35aSPoul-Henning Kamp goto out; 61925f6e35aSPoul-Henning Kamp } else { 62025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 62125f6e35aSPoul-Henning Kamp } 6224218a731SPoul-Henning Kamp if (uap->nargs > 0) { 62325f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 62425f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 62525f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 62625f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 62725f6e35aSPoul-Henning Kamp } 62825f6e35aSPoul-Henning Kamp /* 62925f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 63025f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 63125f6e35aSPoul-Henning Kamp * from user-space succeed. 63225f6e35aSPoul-Henning Kamp */ 63325f6e35aSPoul-Henning Kamp sig = SIGABRT; 63425f6e35aSPoul-Henning Kamp out: 63525f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 63625f6e35aSPoul-Henning Kamp sbuf_trim(sb); 63725f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 63825f6e35aSPoul-Henning Kamp } 63925f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 64025f6e35aSPoul-Henning Kamp sbuf_finish(sb); 64125f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 64225f6e35aSPoul-Henning Kamp sbuf_delete(sb); 64325f6e35aSPoul-Henning Kamp exit1(td, W_EXITCODE(0, sig)); 64425f6e35aSPoul-Henning Kamp return (0); 64525f6e35aSPoul-Henning Kamp } 64625f6e35aSPoul-Henning Kamp 64725f6e35aSPoul-Henning Kamp 648b2f9e8b1SBruce Evans #ifdef COMPAT_43 649234216efSMatthew Dillon /* 650a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 651234216efSMatthew Dillon */ 65226f9a767SRodney W. Grimes int 653830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 654df8bae1dSRodney W. Grimes { 655b7e23e82SJohn Baldwin int error, status; 656df8bae1dSRodney W. Grimes 657b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 658b7e23e82SJohn Baldwin if (error == 0) 659b7e23e82SJohn Baldwin td->td_retval[1] = status; 660b7e23e82SJohn Baldwin return (error); 661df8bae1dSRodney W. Grimes } 662b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 663df8bae1dSRodney W. Grimes 664234216efSMatthew Dillon /* 665a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 666234216efSMatthew Dillon */ 66726f9a767SRodney W. Grimes int 668830c3153SDag-Erling Smørgrav wait4(struct thread *td, struct wait_args *uap) 669df8bae1dSRodney W. Grimes { 67078c85e8dSJohn Baldwin struct rusage ru, *rup; 671b7e23e82SJohn Baldwin int error, status; 672b40ce416SJulian Elischer 67378c85e8dSJohn Baldwin if (uap->rusage != NULL) 67478c85e8dSJohn Baldwin rup = &ru; 67578c85e8dSJohn Baldwin else 67678c85e8dSJohn Baldwin rup = NULL; 67778c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 678b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 679b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 680b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 681b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 682b7e23e82SJohn Baldwin return (error); 683df8bae1dSRodney W. Grimes } 684df8bae1dSRodney W. Grimes 685324fb6beSRobert Watson /* 686324fb6beSRobert Watson * Reap the remains of a zombie process and optionally return status and 687324fb6beSRobert Watson * rusage. Asserts and will release both the proctree_lock and the process 688324fb6beSRobert Watson * lock as part of its work. 689324fb6beSRobert Watson */ 690324fb6beSRobert Watson static void 691324fb6beSRobert Watson proc_reap(struct thread *td, struct proc *p, int *status, int options, 692324fb6beSRobert Watson struct rusage *rusage) 693324fb6beSRobert Watson { 694324fb6beSRobert Watson INIT_VPROCG(P_TO_VPROCG(p)); 695324fb6beSRobert Watson struct proc *q, *t; 696324fb6beSRobert Watson 697324fb6beSRobert Watson sx_assert(&proctree_lock, SA_XLOCKED); 698324fb6beSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 699324fb6beSRobert Watson PROC_SLOCK_ASSERT(p, MA_OWNED); 700324fb6beSRobert Watson KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 701324fb6beSRobert Watson 702324fb6beSRobert Watson q = td->td_proc; 703324fb6beSRobert Watson if (rusage) { 704324fb6beSRobert Watson *rusage = p->p_ru; 705324fb6beSRobert Watson calcru(p, &rusage->ru_utime, &rusage->ru_stime); 706324fb6beSRobert Watson } 707324fb6beSRobert Watson PROC_SUNLOCK(p); 708324fb6beSRobert Watson td->td_retval[0] = p->p_pid; 709324fb6beSRobert Watson if (status) 710324fb6beSRobert Watson *status = p->p_xstat; /* convert to int */ 711324fb6beSRobert Watson if (options & WNOWAIT) { 712324fb6beSRobert Watson /* 713324fb6beSRobert Watson * Only poll, returning the status. Caller does not wish to 714324fb6beSRobert Watson * release the proc struct just yet. 715324fb6beSRobert Watson */ 716324fb6beSRobert Watson PROC_UNLOCK(p); 717324fb6beSRobert Watson sx_xunlock(&proctree_lock); 718324fb6beSRobert Watson return; 719324fb6beSRobert Watson } 720324fb6beSRobert Watson 721324fb6beSRobert Watson PROC_LOCK(q); 722324fb6beSRobert Watson sigqueue_take(p->p_ksi); 723324fb6beSRobert Watson PROC_UNLOCK(q); 724324fb6beSRobert Watson PROC_UNLOCK(p); 725324fb6beSRobert Watson 726324fb6beSRobert Watson /* 727324fb6beSRobert Watson * If we got the child via a ptrace 'attach', we need to give it back 728324fb6beSRobert Watson * to the old parent. 729324fb6beSRobert Watson */ 730324fb6beSRobert Watson if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 731324fb6beSRobert Watson PROC_LOCK(p); 732324fb6beSRobert Watson p->p_oppid = 0; 733324fb6beSRobert Watson proc_reparent(p, t); 734324fb6beSRobert Watson PROC_UNLOCK(p); 735324fb6beSRobert Watson tdsignal(t, NULL, SIGCHLD, p->p_ksi); 736324fb6beSRobert Watson wakeup(t); 737324fb6beSRobert Watson cv_broadcast(&p->p_pwait); 738324fb6beSRobert Watson PROC_UNLOCK(t); 739324fb6beSRobert Watson sx_xunlock(&proctree_lock); 740324fb6beSRobert Watson return; 741324fb6beSRobert Watson } 742324fb6beSRobert Watson 743324fb6beSRobert Watson /* 744324fb6beSRobert Watson * Remove other references to this process to ensure we have an 745324fb6beSRobert Watson * exclusive reference. 746324fb6beSRobert Watson */ 747324fb6beSRobert Watson sx_xlock(&allproc_lock); 748324fb6beSRobert Watson LIST_REMOVE(p, p_list); /* off zombproc */ 749324fb6beSRobert Watson sx_xunlock(&allproc_lock); 750324fb6beSRobert Watson LIST_REMOVE(p, p_sibling); 751324fb6beSRobert Watson leavepgrp(p); 752324fb6beSRobert Watson sx_xunlock(&proctree_lock); 753324fb6beSRobert Watson 754324fb6beSRobert Watson /* 755324fb6beSRobert Watson * As a side effect of this lock, we know that all other writes to 756324fb6beSRobert Watson * this proc are visible now, so no more locking is needed for p. 757324fb6beSRobert Watson */ 758324fb6beSRobert Watson PROC_LOCK(p); 759324fb6beSRobert Watson p->p_xstat = 0; /* XXX: why? */ 760324fb6beSRobert Watson PROC_UNLOCK(p); 761324fb6beSRobert Watson PROC_LOCK(q); 762324fb6beSRobert Watson ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 763324fb6beSRobert Watson PROC_UNLOCK(q); 764324fb6beSRobert Watson 765324fb6beSRobert Watson /* 766324fb6beSRobert Watson * Decrement the count of procs running with this uid. 767324fb6beSRobert Watson */ 768324fb6beSRobert Watson (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 769324fb6beSRobert Watson 770324fb6beSRobert Watson /* 771324fb6beSRobert Watson * Free credentials, arguments, and sigacts. 772324fb6beSRobert Watson */ 773324fb6beSRobert Watson crfree(p->p_ucred); 774324fb6beSRobert Watson p->p_ucred = NULL; 775324fb6beSRobert Watson pargs_drop(p->p_args); 776324fb6beSRobert Watson p->p_args = NULL; 777324fb6beSRobert Watson sigacts_free(p->p_sigacts); 778324fb6beSRobert Watson p->p_sigacts = NULL; 779324fb6beSRobert Watson 780324fb6beSRobert Watson /* 781324fb6beSRobert Watson * Do any thread-system specific cleanups. 782324fb6beSRobert Watson */ 783324fb6beSRobert Watson thread_wait(p); 784324fb6beSRobert Watson 785324fb6beSRobert Watson /* 786324fb6beSRobert Watson * Give vm and machine-dependent layer a chance to free anything that 787324fb6beSRobert Watson * cpu_exit couldn't release while still running in process context. 788324fb6beSRobert Watson */ 789324fb6beSRobert Watson vm_waitproc(p); 790324fb6beSRobert Watson #ifdef MAC 791324fb6beSRobert Watson mac_proc_destroy(p); 792324fb6beSRobert Watson #endif 793324fb6beSRobert Watson KASSERT(FIRST_THREAD_IN_PROC(p), 794324fb6beSRobert Watson ("proc_reap: no residual thread!")); 795324fb6beSRobert Watson uma_zfree(proc_zone, p); 796324fb6beSRobert Watson sx_xlock(&allproc_lock); 797324fb6beSRobert Watson nprocs--; 798324fb6beSRobert Watson #ifdef VIMAGE 799324fb6beSRobert Watson vprocg->nprocs--; 800324fb6beSRobert Watson #endif 801324fb6beSRobert Watson sx_xunlock(&allproc_lock); 802324fb6beSRobert Watson } 803324fb6beSRobert Watson 804b7e23e82SJohn Baldwin int 8057eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 8067eaec467SJohn Baldwin struct rusage *rusage) 807df8bae1dSRodney W. Grimes { 808324fb6beSRobert Watson struct proc *p, *q; 809a9a64385SJohn Baldwin int error, nfound; 810df8bae1dSRodney W. Grimes 811de3007e8SWayne Salamon AUDIT_ARG(pid, pid); 812de3007e8SWayne Salamon 813b40ce416SJulian Elischer q = td->td_proc; 814b7e23e82SJohn Baldwin if (pid == 0) { 815f591779bSSeigo Tanimura PROC_LOCK(q); 816b7e23e82SJohn Baldwin pid = -q->p_pgid; 817f591779bSSeigo Tanimura PROC_UNLOCK(q); 818f591779bSSeigo Tanimura } 819cbc15844SKonstantin Belousov if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WNOWAIT|WLINUXCLONE)) 8206dc958b9SJohn Baldwin return (EINVAL); 821df8bae1dSRodney W. Grimes loop: 822902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 823902c0d82SDavid Xu PROC_LOCK(q); 824902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 825902c0d82SDavid Xu PROC_UNLOCK(q); 826902c0d82SDavid Xu } 827df8bae1dSRodney W. Grimes nfound = 0; 828d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 8292e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 830f591779bSSeigo Tanimura PROC_LOCK(p); 831b7e23e82SJohn Baldwin if (pid != WAIT_ANY && 832b7e23e82SJohn Baldwin p->p_pid != pid && p->p_pgid != -pid) { 833f591779bSSeigo Tanimura PROC_UNLOCK(p); 834df8bae1dSRodney W. Grimes continue; 835f591779bSSeigo Tanimura } 836babe9a2bSRobert Watson if (p_canwait(td, p)) { 837babe9a2bSRobert Watson PROC_UNLOCK(p); 838babe9a2bSRobert Watson continue; 839babe9a2bSRobert Watson } 8404ac9ae70SJulian Elischer 8411156bc4dSJake Burkholder /* 8421156bc4dSJake Burkholder * This special case handles a kthread spawned by linux_clone 8431156bc4dSJake Burkholder * (see linux_misc.c). The linux_wait4 and linux_waitpid 8441156bc4dSJake Burkholder * functions need to be able to distinguish between waiting 8451156bc4dSJake Burkholder * on a process and waiting on a thread. It is a thread if 8461156bc4dSJake Burkholder * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 8471156bc4dSJake Burkholder * signifies we want to wait for threads and not processes. 8484ac9ae70SJulian Elischer */ 8491156bc4dSJake Burkholder if ((p->p_sigparent != SIGCHLD) ^ 850b7e23e82SJohn Baldwin ((options & WLINUXCLONE) != 0)) { 851a914fb6bSJohn Baldwin PROC_UNLOCK(p); 8524ac9ae70SJulian Elischer continue; 853a914fb6bSJohn Baldwin } 8544ac9ae70SJulian Elischer 855df8bae1dSRodney W. Grimes nfound++; 856982d11f8SJeff Roberson PROC_SLOCK(p); 857a140976eSAttilio Rao if (p->p_state == PRS_ZOMBIE) { 858324fb6beSRobert Watson proc_reap(td, p, status, options, rusage); 859d7aadbf9SJohn Baldwin return (0); 860df8bae1dSRodney W. Grimes } 8615a2f73e6SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && 8625a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 863a9a64385SJohn Baldwin (p->p_flag & P_WAITED) == 0 && 864b7e23e82SJohn Baldwin (p->p_flag & P_TRACED || options & WUNTRACED)) { 865982d11f8SJeff Roberson PROC_SUNLOCK(p); 866df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 867d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 868b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 869b7e23e82SJohn Baldwin if (status) 870b7e23e82SJohn Baldwin *status = W_STOPCODE(p->p_xstat); 871ebceaf6dSDavid Xu 872ebceaf6dSDavid Xu PROC_LOCK(q); 873ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 874ebceaf6dSDavid Xu PROC_UNLOCK(q); 875e94cc4acSDavid Xu PROC_UNLOCK(p); 876ebceaf6dSDavid Xu 877b7e23e82SJohn Baldwin return (0); 878df8bae1dSRodney W. Grimes } 879982d11f8SJeff Roberson PROC_SUNLOCK(p); 880b7e23e82SJohn Baldwin if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 8816ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 8826ee093fbSMike Barcroft td->td_retval[0] = p->p_pid; 8836ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 8846ee093fbSMike Barcroft 885ebceaf6dSDavid Xu PROC_LOCK(q); 886ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 887ebceaf6dSDavid Xu PROC_UNLOCK(q); 888e94cc4acSDavid Xu PROC_UNLOCK(p); 889ebceaf6dSDavid Xu 890b7e23e82SJohn Baldwin if (status) 891b7e23e82SJohn Baldwin *status = SIGCONT; 892b7e23e82SJohn Baldwin return (0); 8936ee093fbSMike Barcroft } 894d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 895d7aadbf9SJohn Baldwin } 896d7aadbf9SJohn Baldwin if (nfound == 0) { 897d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 898d7aadbf9SJohn Baldwin return (ECHILD); 899d7aadbf9SJohn Baldwin } 900b7e23e82SJohn Baldwin if (options & WNOHANG) { 901d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 902d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 903d7aadbf9SJohn Baldwin return (0); 904d7aadbf9SJohn Baldwin } 905d7aadbf9SJohn Baldwin PROC_LOCK(q); 906d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 90795992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 90895992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 90995992d56SDavid Xu error = 0; 91095992d56SDavid Xu } else 9117f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 912d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 9134ae89b95SJohn Baldwin if (error) 914d7aadbf9SJohn Baldwin return (error); 915d7aadbf9SJohn Baldwin goto loop; 916d7aadbf9SJohn Baldwin } 917df8bae1dSRodney W. Grimes 918df8bae1dSRodney W. Grimes /* 91998f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 92098f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 921df8bae1dSRodney W. Grimes */ 922df8bae1dSRodney W. Grimes void 923830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 924df8bae1dSRodney W. Grimes { 925df8bae1dSRodney W. Grimes 9264e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 927c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 928df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 929df8bae1dSRodney W. Grimes return; 930df8bae1dSRodney W. Grimes 931ff766807SDavid Xu PROC_LOCK(child->p_pptr); 932ff766807SDavid Xu sigqueue_take(child->p_ksi); 933ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 934b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 935b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 936df8bae1dSRodney W. Grimes child->p_pptr = parent; 937df8bae1dSRodney W. Grimes } 938