19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 405591b823SEivind Eklund #include "opt_compat.h" 41db6a20e2SGarrett Wollman #include "opt_ktrace.h" 422555374cSRobert Watson #include "opt_mac.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> 54df8bae1dSRodney W. Grimes #include <sys/tty.h> 55df8bae1dSRodney W. Grimes #include <sys/wait.h> 56eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 577a6b989bSJohn Baldwin #include <sys/vnode.h> 58df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 5925f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 60797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 61b43179fbSJeff Roberson #include <sys/sched.h> 621005a129SJohn Baldwin #include <sys/sx.h> 63c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6425f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 65df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 667c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 67797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 68780dc5a8SPeter Wemm #include <sys/shm.h> 69780dc5a8SPeter Wemm #include <sys/sem.h> 706c84de02SJohn Baldwin #ifdef KTRACE 716c84de02SJohn Baldwin #include <sys/ktrace.h> 726c84de02SJohn Baldwin #endif 73780dc5a8SPeter Wemm 74fcf7f27aSRobert Watson #include <security/audit/audit.h> 75aed55708SRobert Watson #include <security/mac/mac_framework.h> 76fcf7f27aSRobert Watson 77df8bae1dSRodney W. Grimes #include <vm/vm.h> 78eb30c1c0SPeter Wemm #include <vm/vm_extern.h> 797a6b989bSJohn Baldwin #include <vm/vm_param.h> 80efeaf95aSDavid Greenman #include <vm/pmap.h> 81efeaf95aSDavid Greenman #include <vm/vm_map.h> 822d21129dSAlan Cox #include <vm/vm_page.h> 83c897b813SJeff Roberson #include <vm/uma.h> 84df8bae1dSRodney W. Grimes 85ba198b1cSMark Newton /* Required to be non-static for SysVR4 emulator */ 8669a6f20bSMark Newton MALLOC_DEFINE(M_ZOMBIE, "zombie", "zombie proc status"); 8755166637SPoul-Henning Kamp 88c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 89c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 90c0bc2867SGleb Smirnoff 91df8bae1dSRodney W. Grimes /* 92873fbcd7SRobert Watson * exit -- death of process. 93df8bae1dSRodney W. Grimes */ 94fc0b1dbfSBruce Evans void 95830c3153SDag-Erling Smørgrav sys_exit(struct thread *td, struct sys_exit_args *uap) 96df8bae1dSRodney W. Grimes { 97b40ce416SJulian Elischer 98b40ce416SJulian Elischer exit1(td, W_EXITCODE(uap->rval, 0)); 99df8bae1dSRodney W. Grimes /* NOTREACHED */ 100df8bae1dSRodney W. Grimes } 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes /* 103873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 104873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 105873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 106df8bae1dSRodney W. Grimes */ 107fc0b1dbfSBruce Evans void 108830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv) 109df8bae1dSRodney W. Grimes { 110276c5169SJohn Baldwin struct proc *p, *nq, *q; 111276c5169SJohn Baldwin struct tty *tp; 112276c5169SJohn Baldwin struct vnode *ttyvp; 11379deba82SMatthew Dillon struct vnode *vtmp; 114d7aadbf9SJohn Baldwin #ifdef KTRACE 115d7aadbf9SJohn Baldwin struct vnode *tracevp; 116a5881ea5SJohn Baldwin struct ucred *tracecred; 117d7aadbf9SJohn Baldwin #endif 11891d5354aSJohn Baldwin struct plimit *plim; 11957051fdcSTor Egge int locked; 120df8bae1dSRodney W. Grimes 1214ae89b95SJohn Baldwin /* 1224ae89b95SJohn Baldwin * Drop Giant if caller has it. Eventually we should warn about 1234ae89b95SJohn Baldwin * being called with Giant held. 1244ae89b95SJohn Baldwin */ 1254ae89b95SJohn Baldwin while (mtx_owned(&Giant)) 1264ae89b95SJohn Baldwin mtx_unlock(&Giant); 1270cddd8f0SMatthew Dillon 128276c5169SJohn Baldwin p = td->td_proc; 129e746d950SJohn Baldwin if (p == initproc) { 1305f7bd355SPoul-Henning Kamp printf("init died (signal %d, exit %d)\n", 131df8bae1dSRodney W. Grimes WTERMSIG(rv), WEXITSTATUS(rv)); 1325f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 1335f7bd355SPoul-Henning Kamp } 1342c1011f7SJohn Dyson 1357a6b989bSJohn Baldwin /* 1362c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 1377a6b989bSJohn Baldwin */ 138e602ba25SJulian Elischer PROC_LOCK(p); 1397ab24ea3SJulian Elischer while (p->p_flag & P_HADTHREADS) { 140e602ba25SJulian Elischer /* 141e602ba25SJulian Elischer * First check if some other thread got here before us.. 142e602ba25SJulian Elischer * if so, act apropriatly, (exit or suspend); 143e602ba25SJulian Elischer */ 144e602ba25SJulian Elischer thread_suspend_check(0); 145e602ba25SJulian Elischer 146e602ba25SJulian Elischer /* 147e602ba25SJulian Elischer * Kill off the other threads. This requires 1486111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 1496111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 1506111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 151e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 1526111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 1536111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 1546111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 155b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 1566111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 1576111dcd2SJohn Baldwin * other threads exits. 158b370279eSDavid Xu * If there is already a thread singler after resumption, 1596111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 160b370279eSDavid Xu * re-check all suspension request, the thread should 161b370279eSDavid Xu * either be suspended there or exit. 162e602ba25SJulian Elischer */ 1637ab24ea3SJulian Elischer if (! thread_single(SINGLE_EXIT)) 1647ab24ea3SJulian Elischer break; 1656111dcd2SJohn Baldwin 166e602ba25SJulian Elischer /* 167e602ba25SJulian Elischer * All other activity in this process is now stopped. 168ed062c8dSJulian Elischer * Threading support has been turned off. 169e602ba25SJulian Elischer */ 170e602ba25SJulian Elischer } 1711c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 1721c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 17306ad42b2SJohn Baldwin /* 17406ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 17506ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 17606ad42b2SJohn Baldwin * released their reference to us. Note that if they have 17706ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 17806ad42b2SJohn Baldwin * via PIOCCONT. 17906ad42b2SJohn Baldwin */ 18006ad42b2SJohn Baldwin _STOPEVENT(p, S_EXIT, rv); 18106ad42b2SJohn Baldwin 18206ad42b2SJohn Baldwin /* 18306ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 18406ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 18506ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 18606ad42b2SJohn Baldwin */ 187e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 18806ad42b2SJohn Baldwin wakeup(&p->p_stype); 18906ad42b2SJohn Baldwin 19006ad42b2SJohn Baldwin /* 19106ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 19206ad42b2SJohn Baldwin * release their reference. 19306ad42b2SJohn Baldwin */ 19406ad42b2SJohn Baldwin while (p->p_lock > 0) 19506ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 196ebceaf6dSDavid Xu 197e602ba25SJulian Elischer PROC_UNLOCK(p); 1981c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 1991c4bcd05SJeff Roberson callout_drain(&p->p_limco); 200b40ce416SJulian Elischer 20100c28d96SRobert Watson #ifdef AUDIT 20200c28d96SRobert Watson /* 20300c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 20400c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 20500c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 20600c28d96SRobert Watson * what the return value is. 20700c28d96SRobert Watson */ 20800c28d96SRobert Watson AUDIT_ARG(exit, WEXITSTATUS(rv), 0); 20900c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 21000c28d96SRobert Watson #endif 21100c28d96SRobert Watson 2127a6b989bSJohn Baldwin /* Are we a task leader? */ 2132c1011f7SJohn Dyson if (p == p->p_leader) { 214c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 2152c1011f7SJohn Dyson q = p->p_peers; 216776e0b36SJohn Baldwin while (q != NULL) { 217776e0b36SJohn Baldwin PROC_LOCK(q); 218776e0b36SJohn Baldwin psignal(q, SIGKILL); 219776e0b36SJohn Baldwin PROC_UNLOCK(q); 2202c1011f7SJohn Dyson q = q->p_peers; 2212c1011f7SJohn Dyson } 222c6544064SJohn Baldwin while (p->p_peers != NULL) 223c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 224c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 2252c1011f7SJohn Dyson } 2262c1011f7SJohn Dyson 227fed06968SJulian Elischer /* 228e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 2296111dcd2SJohn Baldwin * E.g. SYSV IPC stuff 230fed06968SJulian Elischer * XXX what if one of these generates an error? 231fed06968SJulian Elischer */ 23275b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exit, p); 233a914fb6bSJohn Baldwin 234df8bae1dSRodney W. Grimes /* 235df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 236df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 237df8bae1dSRodney W. Grimes */ 238a914fb6bSJohn Baldwin PROC_LOCK(p); 239a282253aSJulian Elischer stopprofclock(p); 240df8bae1dSRodney W. Grimes p->p_flag &= ~(P_TRACED | P_PPWAIT); 2415499ea01SJohn Baldwin 2425499ea01SJohn Baldwin /* 2435499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 2445499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 2455499ea01SJohn Baldwin */ 2465499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 2475499ea01SJohn Baldwin callout_stop(&p->p_itcallout) == 0) { 2485499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 2495499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 2505499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 2515499ea01SJohn Baldwin ("realtime timer is still armed")); 2525499ea01SJohn Baldwin } 25396d7f8efSTim J. Robbins PROC_UNLOCK(p); 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 256831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 257831d27a9SDon Lewis * F_SETOWN with our pid. 258831d27a9SDon Lewis */ 259831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 260831d27a9SDon Lewis 261831d27a9SDon Lewis /* 262c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 263c0bc2867SGleb Smirnoff */ 264c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 265c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 266c0bc2867SGleb Smirnoff 267c0bc2867SGleb Smirnoff /* 268df8bae1dSRodney W. Grimes * Close open files and release open-file table. 269df8bae1dSRodney W. Grimes * This may block! 270df8bae1dSRodney W. Grimes */ 271edf6699aSAlfred Perlstein fdfree(td); 272df8bae1dSRodney W. Grimes 273a914fb6bSJohn Baldwin /* 274a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 275a2587073SPoul-Henning Kamp * stop before we return to userland 276a2587073SPoul-Henning Kamp */ 277a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 278a2587073SPoul-Henning Kamp g_waitidle(); 279a2587073SPoul-Henning Kamp 280a2587073SPoul-Henning Kamp /* 281a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 282a914fb6bSJohn Baldwin */ 283c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 28479fc0bf4SMike Smith if (p->p_leader->p_peers) { 28579fc0bf4SMike Smith q = p->p_leader; 28679fc0bf4SMike Smith while (q->p_peers != p) 28779fc0bf4SMike Smith q = q->p_peers; 28879fc0bf4SMike Smith q->p_peers = p->p_peers; 2897f05b035SAlfred Perlstein wakeup(p->p_leader); 29079fc0bf4SMike Smith } 291c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 29279fc0bf4SMike Smith 29357051fdcSTor Egge vmspace_exit(td); 294df8bae1dSRodney W. Grimes 2957ca6b782SChristian S.J. Peron mtx_lock(&Giant); /* XXX TTY */ 296ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 297df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 298830c3153SDag-Erling Smørgrav struct session *sp; 299df8bae1dSRodney W. Grimes 300f591779bSSeigo Tanimura sp = p->p_session; 301df8bae1dSRodney W. Grimes if (sp->s_ttyvp) { 302df8bae1dSRodney W. Grimes /* 303df8bae1dSRodney W. Grimes * Controlling process. 304df8bae1dSRodney W. Grimes * Signal foreground pgrp, 305df8bae1dSRodney W. Grimes * drain controlling terminal 306df8bae1dSRodney W. Grimes * and revoke access to controlling terminal. 307df8bae1dSRodney W. Grimes */ 308dd45d8adSJulian Elischer if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 309f591779bSSeigo Tanimura tp = sp->s_ttyp; 310f591779bSSeigo Tanimura if (sp->s_ttyp->t_pgrp) { 311f591779bSSeigo Tanimura PGRP_LOCK(sp->s_ttyp->t_pgrp); 312df8bae1dSRodney W. Grimes pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 313f591779bSSeigo Tanimura PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 314f591779bSSeigo Tanimura } 315f591779bSSeigo Tanimura /* XXX tp should be locked. */ 316ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 317f591779bSSeigo Tanimura (void) ttywait(tp); 318ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 319df8bae1dSRodney W. Grimes /* 320df8bae1dSRodney W. Grimes * The tty could have been revoked 321df8bae1dSRodney W. Grimes * if we blocked. 322df8bae1dSRodney W. Grimes */ 323f591779bSSeigo Tanimura if (sp->s_ttyvp) { 324f591779bSSeigo Tanimura ttyvp = sp->s_ttyvp; 325f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 326df8bae1dSRodney W. Grimes sp->s_ttyvp = NULL; 327f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 328ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 32922db15c0SAttilio Rao VOP_LOCK(ttyvp, LK_EXCLUSIVE); 330f591779bSSeigo Tanimura VOP_REVOKE(ttyvp, REVOKEALL); 3312b3183a8SJeff Roberson vput(ttyvp); 332ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 333f591779bSSeigo Tanimura } 334f591779bSSeigo Tanimura } 335f591779bSSeigo Tanimura if (sp->s_ttyvp) { 336f591779bSSeigo Tanimura ttyvp = sp->s_ttyvp; 337f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 338f591779bSSeigo Tanimura sp->s_ttyvp = NULL; 339f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 340f591779bSSeigo Tanimura vrele(ttyvp); 341f591779bSSeigo Tanimura } 342df8bae1dSRodney W. Grimes /* 343df8bae1dSRodney W. Grimes * s_ttyp is not zero'd; we use this to indicate 344df8bae1dSRodney W. Grimes * that the session once had a controlling terminal. 345df8bae1dSRodney W. Grimes * (for logging and informational purposes) 346df8bae1dSRodney W. Grimes */ 347df8bae1dSRodney W. Grimes } 348f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 349df8bae1dSRodney W. Grimes sp->s_leader = NULL; 350f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 351f591779bSSeigo Tanimura } 352df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 353ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 354b40ce416SJulian Elischer (void)acct_process(td); 3557ca6b782SChristian S.J. Peron mtx_unlock(&Giant); 356df8bae1dSRodney W. Grimes #ifdef KTRACE 357df8bae1dSRodney W. Grimes /* 35834a9edafSJohn Baldwin * Disable tracing, then drain any pending records and release 35934a9edafSJohn Baldwin * the trace file. 360df8bae1dSRodney W. Grimes */ 36134a9edafSJohn Baldwin if (p->p_traceflag != 0) { 36234a9edafSJohn Baldwin PROC_LOCK(p); 36334a9edafSJohn Baldwin mtx_lock(&ktrace_mtx); 36434a9edafSJohn Baldwin p->p_traceflag = 0; 36534a9edafSJohn Baldwin mtx_unlock(&ktrace_mtx); 36634a9edafSJohn Baldwin PROC_UNLOCK(p); 3672c255e9dSRobert Watson ktrprocexit(td); 368d7aadbf9SJohn Baldwin PROC_LOCK(p); 3696c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 370a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 371a5881ea5SJohn Baldwin p->p_tracevp = NULL; 372a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 373a5881ea5SJohn Baldwin p->p_tracecred = NULL; 3746c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 375d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 37615088317SBrian Feldman if (tracevp != NULL) { 37757606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(tracevp->v_mount); 378d7aadbf9SJohn Baldwin vrele(tracevp); 37957606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 38015088317SBrian Feldman } 381a5881ea5SJohn Baldwin if (tracecred != NULL) 382a5881ea5SJohn Baldwin crfree(tracecred); 38334a9edafSJohn Baldwin } 384df8bae1dSRodney W. Grimes #endif 385df8bae1dSRodney W. Grimes /* 386ee42d0a9SDavid Malone * Release reference to text vnode 387ee42d0a9SDavid Malone */ 388ee42d0a9SDavid Malone if ((vtmp = p->p_textvp) != NULL) { 389ee42d0a9SDavid Malone p->p_textvp = NULL; 39057606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(vtmp->v_mount); 391ee42d0a9SDavid Malone vrele(vtmp); 39257606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 393ee42d0a9SDavid Malone } 394ee42d0a9SDavid Malone 395ee42d0a9SDavid Malone /* 396d7aadbf9SJohn Baldwin * Release our limits structure. 397d7aadbf9SJohn Baldwin */ 39891d5354aSJohn Baldwin PROC_LOCK(p); 39991d5354aSJohn Baldwin plim = p->p_limit; 400d7aadbf9SJohn Baldwin p->p_limit = NULL; 40191d5354aSJohn Baldwin PROC_UNLOCK(p); 40291d5354aSJohn Baldwin lim_free(plim); 403d7aadbf9SJohn Baldwin 404d7aadbf9SJohn Baldwin /* 405df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 406df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 407df8bae1dSRodney W. Grimes */ 4081005a129SJohn Baldwin sx_xlock(&allproc_lock); 409b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 410b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 411b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4121005a129SJohn Baldwin sx_xunlock(&allproc_lock); 413df8bae1dSRodney W. Grimes 41437f84a60SJohn Baldwin /* 4151bba2a94SJohn Baldwin * Call machine-dependent code to release any 4161bba2a94SJohn Baldwin * machine-dependent resources other than the address space. 4171bba2a94SJohn Baldwin * The address space is released by "vmspace_exitfree(p)" in 4181bba2a94SJohn Baldwin * vm_waitproc(). 4191bba2a94SJohn Baldwin */ 4201bba2a94SJohn Baldwin cpu_exit(td); 4211bba2a94SJohn Baldwin 4221bba2a94SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 4231bba2a94SJohn Baldwin 4241bba2a94SJohn Baldwin /* 42537f84a60SJohn Baldwin * Reparent all of our children to init. 42637f84a60SJohn Baldwin */ 4271005a129SJohn Baldwin sx_xlock(&proctree_lock); 4282e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 429a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 4307f05b035SAlfred Perlstein wakeup(initproc); 431a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4322e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 433a914fb6bSJohn Baldwin PROC_LOCK(q); 434c65437a3SJohn Baldwin proc_reparent(q, initproc); 4354ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 436df8bae1dSRodney W. Grimes /* 437df8bae1dSRodney W. Grimes * Traced processes are killed 438df8bae1dSRodney W. Grimes * since their existence means someone is screwing up. 439df8bae1dSRodney W. Grimes */ 440df8bae1dSRodney W. Grimes if (q->p_flag & P_TRACED) { 441c2836532SDavid Xu q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 442df8bae1dSRodney W. Grimes psignal(q, SIGKILL); 443c65437a3SJohn Baldwin } 444a914fb6bSJohn Baldwin PROC_UNLOCK(q); 445df8bae1dSRodney W. Grimes } 446df8bae1dSRodney W. Grimes 4471c4bcd05SJeff Roberson /* Save exit status. */ 448d7aadbf9SJohn Baldwin PROC_LOCK(p); 449df8bae1dSRodney W. Grimes p->p_xstat = rv; 450cbf4e354SDavid Xu p->p_xthread = td; 4511c4bcd05SJeff Roberson /* 4527a6b989bSJohn Baldwin * Notify interested parties of our demise. 453cb679c38SJonathan Lemon */ 454ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 4557eaec467SJohn Baldwin 4561a29c806SOlivier Houchard /* 4571a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 4581a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 4591a29c806SOlivier Houchard * can beat us if we don't. 4601a29c806SOlivier Houchard */ 461ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 462cb679c38SJonathan Lemon 463cb679c38SJonathan Lemon /* 464645682fdSLuoqi Chen * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 465ba1551caSIan Dowse * flag set, or if the handler is set to SIG_IGN, notify process 466ba1551caSIan Dowse * 1 instead (and hope it will handle this situation). 467df8bae1dSRodney W. Grimes */ 468d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 46990af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 47090af4afaSJohn Baldwin if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 471276c5169SJohn Baldwin struct proc *pp; 472276c5169SJohn Baldwin 47390af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 474276c5169SJohn Baldwin pp = p->p_pptr; 475f591779bSSeigo Tanimura PROC_UNLOCK(pp); 476245f17d4SJoerg Wunsch proc_reparent(p, initproc); 47755b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 478f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 479245f17d4SJoerg Wunsch /* 480245f17d4SJoerg Wunsch * If this was the last child of our parent, notify 481245f17d4SJoerg Wunsch * parent, so in case he was wait(2)ing, he will 482245f17d4SJoerg Wunsch * continue. 483245f17d4SJoerg Wunsch */ 484245f17d4SJoerg Wunsch if (LIST_EMPTY(&pp->p_children)) 4857f05b035SAlfred Perlstein wakeup(pp); 48690af4afaSJohn Baldwin } else 48790af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 488245f17d4SJoerg Wunsch 4896567eef7SDon Lewis if (p->p_pptr == initproc) 4906626c604SJulian Elischer psignal(p->p_pptr, SIGCHLD); 491ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 492ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 493ebceaf6dSDavid Xu childproc_exited(p); 494ebceaf6dSDavid Xu else /* LINUX thread */ 4956567eef7SDon Lewis psignal(p->p_pptr, p->p_sigparent); 496ebceaf6dSDavid Xu } 497d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 498696058c3SJulian Elischer 499eb30c1c0SPeter Wemm /* 500f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 501f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 502f71e748dSDavid Xu * for signal queue at the time when the state is set. 503f71e748dSDavid Xu */ 504f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 505f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 506f71e748dSDavid Xu 507f71e748dSDavid Xu /* 508462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 509a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 510a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 511ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 512ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 513ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 514ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 515ddf9c4f7SJohn Baldwin * proc lock. 516eb30c1c0SPeter Wemm */ 517ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 518982d11f8SJeff Roberson PROC_SLOCK(p->p_pptr); 519982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 520982d11f8SJeff Roberson PROC_SUNLOCK(p->p_pptr); 521982d11f8SJeff Roberson PROC_SLOCK(p); 522e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 523d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 524871684b8SBruce Evans 525f6f230feSJeff Roberson /* 5267eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 527ad3b9257SJohn-Mark Gurney * late in the game. 528ad3b9257SJohn-Mark Gurney */ 529ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 530ad3b9257SJohn-Mark Gurney 531ad3b9257SJohn-Mark Gurney /* 532a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 533a140976eSAttilio Rao */ 534a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 535a140976eSAttilio Rao 536a140976eSAttilio Rao /* 537696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 538e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 539696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 540e602ba25SJulian Elischer */ 541e602ba25SJulian Elischer thread_exit(); 542df8bae1dSRodney W. Grimes } 543df8bae1dSRodney W. Grimes 54425f6e35aSPoul-Henning Kamp 54525f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 54625f6e35aSPoul-Henning Kamp struct abort2_args { 54725f6e35aSPoul-Henning Kamp char *why; 54825f6e35aSPoul-Henning Kamp int nargs; 54925f6e35aSPoul-Henning Kamp void **args; 55025f6e35aSPoul-Henning Kamp }; 55125f6e35aSPoul-Henning Kamp #endif 55225f6e35aSPoul-Henning Kamp 55325f6e35aSPoul-Henning Kamp int 55425f6e35aSPoul-Henning Kamp abort2(struct thread *td, struct abort2_args *uap) 55525f6e35aSPoul-Henning Kamp { 55625f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 55725f6e35aSPoul-Henning Kamp struct sbuf *sb; 55825f6e35aSPoul-Henning Kamp void *uargs[16]; 55925f6e35aSPoul-Henning Kamp int error, i, sig; 56025f6e35aSPoul-Henning Kamp 56125f6e35aSPoul-Henning Kamp error = 0; /* satisfy compiler */ 56225f6e35aSPoul-Henning Kamp 56325f6e35aSPoul-Henning Kamp /* 56425f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 56525f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 56625f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 56725f6e35aSPoul-Henning Kamp */ 56825f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 56925f6e35aSPoul-Henning Kamp sbuf_clear(sb); 57025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 57125f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 57225f6e35aSPoul-Henning Kamp /* 57325f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 57425f6e35aSPoul-Henning Kamp * abort2() was called improperly 57525f6e35aSPoul-Henning Kamp */ 57625f6e35aSPoul-Henning Kamp sig = SIGKILL; 57725f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 57825f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 57925f6e35aSPoul-Henning Kamp goto out; 58025f6e35aSPoul-Henning Kamp if (uap->args == NULL) 58125f6e35aSPoul-Henning Kamp goto out; 58225f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 58325f6e35aSPoul-Henning Kamp if (error != 0) 58425f6e35aSPoul-Henning Kamp goto out; 58525f6e35aSPoul-Henning Kamp /* 58625f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 58725f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 58825f6e35aSPoul-Henning Kamp */ 58925f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 59025f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 59125f6e35aSPoul-Henning Kamp if (error < 0) 59225f6e35aSPoul-Henning Kamp goto out; 59325f6e35aSPoul-Henning Kamp } else { 59425f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 59525f6e35aSPoul-Henning Kamp } 59625f6e35aSPoul-Henning Kamp if (uap->nargs) { 59725f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 59825f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 59925f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 60025f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 60125f6e35aSPoul-Henning Kamp } 60225f6e35aSPoul-Henning Kamp /* 60325f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 60425f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 60525f6e35aSPoul-Henning Kamp * from user-space succeed. 60625f6e35aSPoul-Henning Kamp */ 60725f6e35aSPoul-Henning Kamp sig = SIGABRT; 60825f6e35aSPoul-Henning Kamp out: 60925f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 61025f6e35aSPoul-Henning Kamp sbuf_trim(sb); 61125f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 61225f6e35aSPoul-Henning Kamp } 61325f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 61425f6e35aSPoul-Henning Kamp sbuf_finish(sb); 61525f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 61625f6e35aSPoul-Henning Kamp sbuf_delete(sb); 61725f6e35aSPoul-Henning Kamp exit1(td, W_EXITCODE(0, sig)); 61825f6e35aSPoul-Henning Kamp return (0); 61925f6e35aSPoul-Henning Kamp } 62025f6e35aSPoul-Henning Kamp 62125f6e35aSPoul-Henning Kamp 622b2f9e8b1SBruce Evans #ifdef COMPAT_43 623234216efSMatthew Dillon /* 624a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 625234216efSMatthew Dillon */ 62626f9a767SRodney W. Grimes int 627830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 628df8bae1dSRodney W. Grimes { 629b7e23e82SJohn Baldwin int error, status; 630df8bae1dSRodney W. Grimes 631b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 632b7e23e82SJohn Baldwin if (error == 0) 633b7e23e82SJohn Baldwin td->td_retval[1] = status; 634b7e23e82SJohn Baldwin return (error); 635df8bae1dSRodney W. Grimes } 636b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 637df8bae1dSRodney W. Grimes 638234216efSMatthew Dillon /* 639a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 640234216efSMatthew Dillon */ 64126f9a767SRodney W. Grimes int 642830c3153SDag-Erling Smørgrav wait4(struct thread *td, struct wait_args *uap) 643df8bae1dSRodney W. Grimes { 64478c85e8dSJohn Baldwin struct rusage ru, *rup; 645b7e23e82SJohn Baldwin int error, status; 646b40ce416SJulian Elischer 64778c85e8dSJohn Baldwin if (uap->rusage != NULL) 64878c85e8dSJohn Baldwin rup = &ru; 64978c85e8dSJohn Baldwin else 65078c85e8dSJohn Baldwin rup = NULL; 65178c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 652b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 653b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 654b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 655b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 656b7e23e82SJohn Baldwin return (error); 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes 659b7e23e82SJohn Baldwin int 6607eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 6617eaec467SJohn Baldwin struct rusage *rusage) 662df8bae1dSRodney W. Grimes { 66348bfcdddSJulian Elischer struct proc *p, *q, *t; 664a9a64385SJohn Baldwin int error, nfound; 665df8bae1dSRodney W. Grimes 666de3007e8SWayne Salamon AUDIT_ARG(pid, pid); 667de3007e8SWayne Salamon 668b40ce416SJulian Elischer q = td->td_proc; 669b7e23e82SJohn Baldwin if (pid == 0) { 670f591779bSSeigo Tanimura PROC_LOCK(q); 671b7e23e82SJohn Baldwin pid = -q->p_pgid; 672f591779bSSeigo Tanimura PROC_UNLOCK(q); 673f591779bSSeigo Tanimura } 674b7e23e82SJohn Baldwin if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 6756dc958b9SJohn Baldwin return (EINVAL); 676df8bae1dSRodney W. Grimes loop: 677902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 678902c0d82SDavid Xu PROC_LOCK(q); 679902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 680902c0d82SDavid Xu PROC_UNLOCK(q); 681902c0d82SDavid Xu } 682df8bae1dSRodney W. Grimes nfound = 0; 683d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 6842e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 685f591779bSSeigo Tanimura PROC_LOCK(p); 686b7e23e82SJohn Baldwin if (pid != WAIT_ANY && 687b7e23e82SJohn Baldwin p->p_pid != pid && p->p_pgid != -pid) { 688f591779bSSeigo Tanimura PROC_UNLOCK(p); 689df8bae1dSRodney W. Grimes continue; 690f591779bSSeigo Tanimura } 691babe9a2bSRobert Watson if (p_canwait(td, p)) { 692babe9a2bSRobert Watson PROC_UNLOCK(p); 693babe9a2bSRobert Watson continue; 694babe9a2bSRobert Watson } 6954ac9ae70SJulian Elischer 6961156bc4dSJake Burkholder /* 6971156bc4dSJake Burkholder * This special case handles a kthread spawned by linux_clone 6981156bc4dSJake Burkholder * (see linux_misc.c). The linux_wait4 and linux_waitpid 6991156bc4dSJake Burkholder * functions need to be able to distinguish between waiting 7001156bc4dSJake Burkholder * on a process and waiting on a thread. It is a thread if 7011156bc4dSJake Burkholder * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 7021156bc4dSJake Burkholder * signifies we want to wait for threads and not processes. 7034ac9ae70SJulian Elischer */ 7041156bc4dSJake Burkholder if ((p->p_sigparent != SIGCHLD) ^ 705b7e23e82SJohn Baldwin ((options & WLINUXCLONE) != 0)) { 706a914fb6bSJohn Baldwin PROC_UNLOCK(p); 7074ac9ae70SJulian Elischer continue; 708a914fb6bSJohn Baldwin } 7094ac9ae70SJulian Elischer 710df8bae1dSRodney W. Grimes nfound++; 711982d11f8SJeff Roberson PROC_SLOCK(p); 712a140976eSAttilio Rao if (p->p_state == PRS_ZOMBIE) { 71378c85e8dSJohn Baldwin if (rusage) { 714a140976eSAttilio Rao *rusage = p->p_ru; 71578c85e8dSJohn Baldwin calcru(p, &rusage->ru_utime, &rusage->ru_stime); 71678c85e8dSJohn Baldwin } 717a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 718a1fe14bcSAttilio Rao td->td_retval[0] = p->p_pid; 719a1fe14bcSAttilio Rao if (status) 720a1fe14bcSAttilio Rao *status = p->p_xstat; /* convert to int */ 721ebceaf6dSDavid Xu PROC_LOCK(q); 722ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 723ebceaf6dSDavid Xu PROC_UNLOCK(q); 724ebceaf6dSDavid Xu 725df8bae1dSRodney W. Grimes /* 726df8bae1dSRodney W. Grimes * If we got the child via a ptrace 'attach', 727df8bae1dSRodney W. Grimes * we need to give it back to the old parent. 728df8bae1dSRodney W. Grimes */ 729b7e23e82SJohn Baldwin PROC_UNLOCK(p); 730d7aadbf9SJohn Baldwin if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 731c65437a3SJohn Baldwin PROC_LOCK(p); 732df8bae1dSRodney W. Grimes p->p_oppid = 0; 733df8bae1dSRodney W. Grimes proc_reparent(p, t); 734c65437a3SJohn Baldwin PROC_UNLOCK(p); 735ebceaf6dSDavid Xu tdsignal(t, NULL, SIGCHLD, p->p_ksi); 7367f05b035SAlfred Perlstein wakeup(t); 737c65437a3SJohn Baldwin PROC_UNLOCK(t); 7381005a129SJohn Baldwin sx_xunlock(&proctree_lock); 739d7aadbf9SJohn Baldwin return (0); 740df8bae1dSRodney W. Grimes } 741ebdc3f1dSSeigo Tanimura 7423890793eSTim J. Robbins /* 7433890793eSTim J. Robbins * Remove other references to this process to ensure 7443890793eSTim J. Robbins * we have an exclusive reference. 7453890793eSTim J. Robbins */ 7466ec62361STim J. Robbins sx_xlock(&allproc_lock); 7476ec62361STim J. Robbins LIST_REMOVE(p, p_list); /* off zombproc */ 7486ec62361STim J. Robbins sx_xunlock(&allproc_lock); 7496ec62361STim J. Robbins LIST_REMOVE(p, p_sibling); 7503890793eSTim J. Robbins leavepgrp(p); 751ebdc3f1dSSeigo Tanimura sx_xunlock(&proctree_lock); 752ebdc3f1dSSeigo Tanimura 753ebdc3f1dSSeigo Tanimura /* 754d7aadbf9SJohn Baldwin * As a side effect of this lock, we know that 755d7aadbf9SJohn Baldwin * all other writes to this proc are visible now, so 756d7aadbf9SJohn Baldwin * no more locking is needed for p. 757d7aadbf9SJohn Baldwin */ 758d7aadbf9SJohn Baldwin PROC_LOCK(p); 759d7aadbf9SJohn Baldwin p->p_xstat = 0; /* XXX: why? */ 760d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 761d7aadbf9SJohn Baldwin PROC_LOCK(q); 762a140976eSAttilio Rao ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, 76378c85e8dSJohn Baldwin &p->p_rux); 764d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 765d7aadbf9SJohn Baldwin 766d7aadbf9SJohn Baldwin /* 767d7aadbf9SJohn Baldwin * Decrement the count of procs running with this uid. 768d7aadbf9SJohn Baldwin */ 769d7aadbf9SJohn Baldwin (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 770d7aadbf9SJohn Baldwin 771d7aadbf9SJohn Baldwin /* 772a9a64385SJohn Baldwin * Free credentials, arguments, and sigacts. 773df8bae1dSRodney W. Grimes */ 774da654d90SPoul-Henning Kamp crfree(p->p_ucred); 77590af4afaSJohn Baldwin p->p_ucred = NULL; 776d7aadbf9SJohn Baldwin pargs_drop(p->p_args); 777d7aadbf9SJohn Baldwin p->p_args = NULL; 77890af4afaSJohn Baldwin sigacts_free(p->p_sigacts); 77990af4afaSJohn Baldwin p->p_sigacts = NULL; 78088c5ea45SJulian Elischer 781df8bae1dSRodney W. Grimes /* 782a9a64385SJohn Baldwin * Do any thread-system specific cleanups. 783e602ba25SJulian Elischer */ 784696058c3SJulian Elischer thread_wait(p); 785e602ba25SJulian Elischer 786e602ba25SJulian Elischer /* 787eb30c1c0SPeter Wemm * Give vm and machine-dependent layer a chance 788df8bae1dSRodney W. Grimes * to free anything that cpu_exit couldn't 789df8bae1dSRodney W. Grimes * release while still running in process context. 790df8bae1dSRodney W. Grimes */ 791eb30c1c0SPeter Wemm vm_waitproc(p); 7922555374cSRobert Watson #ifdef MAC 79330d239bcSRobert Watson mac_proc_destroy(p); 7942555374cSRobert Watson #endif 7951faf202eSJulian Elischer KASSERT(FIRST_THREAD_IN_PROC(p), 796b7e23e82SJohn Baldwin ("kern_wait: no residual thread!")); 797c897b813SJeff Roberson uma_zfree(proc_zone, p); 798d7aadbf9SJohn Baldwin sx_xlock(&allproc_lock); 799df8bae1dSRodney W. Grimes nprocs--; 800d7aadbf9SJohn Baldwin sx_xunlock(&allproc_lock); 801d7aadbf9SJohn Baldwin return (0); 802df8bae1dSRodney W. Grimes } 8035a2f73e6SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && 8045a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 805a9a64385SJohn Baldwin (p->p_flag & P_WAITED) == 0 && 806b7e23e82SJohn Baldwin (p->p_flag & P_TRACED || options & WUNTRACED)) { 807982d11f8SJeff Roberson PROC_SUNLOCK(p); 808df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 809d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 810b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 811b7e23e82SJohn Baldwin if (status) 812b7e23e82SJohn Baldwin *status = W_STOPCODE(p->p_xstat); 813ebceaf6dSDavid Xu 814ebceaf6dSDavid Xu PROC_LOCK(q); 815ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 816ebceaf6dSDavid Xu PROC_UNLOCK(q); 817e94cc4acSDavid Xu PROC_UNLOCK(p); 818ebceaf6dSDavid Xu 819b7e23e82SJohn Baldwin return (0); 820df8bae1dSRodney W. Grimes } 821982d11f8SJeff Roberson PROC_SUNLOCK(p); 822b7e23e82SJohn Baldwin if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 8236ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 8246ee093fbSMike Barcroft td->td_retval[0] = p->p_pid; 8256ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 8266ee093fbSMike Barcroft 827ebceaf6dSDavid Xu PROC_LOCK(q); 828ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 829ebceaf6dSDavid Xu PROC_UNLOCK(q); 830e94cc4acSDavid Xu PROC_UNLOCK(p); 831ebceaf6dSDavid Xu 832b7e23e82SJohn Baldwin if (status) 833b7e23e82SJohn Baldwin *status = SIGCONT; 834b7e23e82SJohn Baldwin return (0); 8356ee093fbSMike Barcroft } 836d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 837d7aadbf9SJohn Baldwin } 838d7aadbf9SJohn Baldwin if (nfound == 0) { 839d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 840d7aadbf9SJohn Baldwin return (ECHILD); 841d7aadbf9SJohn Baldwin } 842b7e23e82SJohn Baldwin if (options & WNOHANG) { 843d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 844d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 845d7aadbf9SJohn Baldwin return (0); 846d7aadbf9SJohn Baldwin } 847d7aadbf9SJohn Baldwin PROC_LOCK(q); 848d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 84995992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 85095992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 85195992d56SDavid Xu error = 0; 85295992d56SDavid Xu } else 8537f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 854d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 8554ae89b95SJohn Baldwin if (error) 856d7aadbf9SJohn Baldwin return (error); 857d7aadbf9SJohn Baldwin goto loop; 858d7aadbf9SJohn Baldwin } 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes /* 86198f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 86298f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 863df8bae1dSRodney W. Grimes */ 864df8bae1dSRodney W. Grimes void 865830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 866df8bae1dSRodney W. Grimes { 867df8bae1dSRodney W. Grimes 8684e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 869c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 870df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 871df8bae1dSRodney W. Grimes return; 872df8bae1dSRodney W. Grimes 873ff766807SDavid Xu PROC_LOCK(child->p_pptr); 874ff766807SDavid Xu sigqueue_take(child->p_ksi); 875ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 876b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 877b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 878df8bae1dSRodney W. Grimes child->p_pptr = parent; 879df8bae1dSRodney W. Grimes } 880