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 /* 92df8bae1dSRodney W. Grimes * exit -- 93df8bae1dSRodney W. Grimes * Death of process. 94234216efSMatthew Dillon * 95234216efSMatthew Dillon * MPSAFE 96df8bae1dSRodney W. Grimes */ 97fc0b1dbfSBruce Evans void 98830c3153SDag-Erling Smørgrav sys_exit(struct thread *td, struct sys_exit_args *uap) 99df8bae1dSRodney W. Grimes { 100b40ce416SJulian Elischer 101b40ce416SJulian Elischer exit1(td, W_EXITCODE(uap->rval, 0)); 102df8bae1dSRodney W. Grimes /* NOTREACHED */ 103df8bae1dSRodney W. Grimes } 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes /* 106df8bae1dSRodney W. Grimes * Exit: deallocate address space and other resources, change proc state 107df8bae1dSRodney W. Grimes * to zombie, and unlink proc from allproc and parent's lists. Save exit 108df8bae1dSRodney W. Grimes * status and rusage for wait(). Check for child processes and orphan them. 109df8bae1dSRodney W. Grimes */ 110fc0b1dbfSBruce Evans void 111830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv) 112df8bae1dSRodney W. Grimes { 113276c5169SJohn Baldwin struct proc *p, *nq, *q; 114276c5169SJohn Baldwin struct tty *tp; 115276c5169SJohn Baldwin struct vnode *ttyvp; 11679deba82SMatthew Dillon struct vnode *vtmp; 117d7aadbf9SJohn Baldwin #ifdef KTRACE 118d7aadbf9SJohn Baldwin struct vnode *tracevp; 119a5881ea5SJohn Baldwin struct ucred *tracecred; 120d7aadbf9SJohn Baldwin #endif 12191d5354aSJohn Baldwin struct plimit *plim; 12257051fdcSTor Egge int locked; 123df8bae1dSRodney W. Grimes 1244ae89b95SJohn Baldwin /* 1254ae89b95SJohn Baldwin * Drop Giant if caller has it. Eventually we should warn about 1264ae89b95SJohn Baldwin * being called with Giant held. 1274ae89b95SJohn Baldwin */ 1284ae89b95SJohn Baldwin while (mtx_owned(&Giant)) 1294ae89b95SJohn Baldwin mtx_unlock(&Giant); 1300cddd8f0SMatthew Dillon 131276c5169SJohn Baldwin p = td->td_proc; 132e746d950SJohn Baldwin if (p == initproc) { 1335f7bd355SPoul-Henning Kamp printf("init died (signal %d, exit %d)\n", 134df8bae1dSRodney W. Grimes WTERMSIG(rv), WEXITSTATUS(rv)); 1355f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 1365f7bd355SPoul-Henning Kamp } 1372c1011f7SJohn Dyson 1387a6b989bSJohn Baldwin /* 1392c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 1407a6b989bSJohn Baldwin */ 141e602ba25SJulian Elischer PROC_LOCK(p); 142ed062c8dSJulian Elischer if (p->p_flag & P_HADTHREADS) { 1430aabef65SDavid Xu retry: 144e602ba25SJulian Elischer /* 145e602ba25SJulian Elischer * First check if some other thread got here before us.. 146e602ba25SJulian Elischer * if so, act apropriatly, (exit or suspend); 147e602ba25SJulian Elischer */ 148e602ba25SJulian Elischer thread_suspend_check(0); 149e602ba25SJulian Elischer 150e602ba25SJulian Elischer /* 151e602ba25SJulian Elischer * Kill off the other threads. This requires 1526111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 1536111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 1546111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 155e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 1566111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 1576111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 1586111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 159b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 1606111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 1616111dcd2SJohn Baldwin * other threads exits. 162b370279eSDavid Xu * If there is already a thread singler after resumption, 1636111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 164b370279eSDavid Xu * re-check all suspension request, the thread should 165b370279eSDavid Xu * either be suspended there or exit. 166e602ba25SJulian Elischer */ 16737814395SPeter Wemm if (thread_single(SINGLE_EXIT)) 1680aabef65SDavid Xu goto retry; 1696111dcd2SJohn Baldwin 170e602ba25SJulian Elischer /* 171e602ba25SJulian Elischer * All other activity in this process is now stopped. 172ed062c8dSJulian Elischer * Threading support has been turned off. 173e602ba25SJulian Elischer */ 174e602ba25SJulian Elischer } 175e602ba25SJulian Elischer 17606ad42b2SJohn Baldwin /* 17706ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 17806ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 17906ad42b2SJohn Baldwin * released their reference to us. Note that if they have 18006ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 18106ad42b2SJohn Baldwin * via PIOCCONT. 18206ad42b2SJohn Baldwin */ 18306ad42b2SJohn Baldwin _STOPEVENT(p, S_EXIT, rv); 18406ad42b2SJohn Baldwin 18506ad42b2SJohn Baldwin /* 18606ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 18706ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 18806ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 18906ad42b2SJohn Baldwin */ 190e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 19106ad42b2SJohn Baldwin wakeup(&p->p_stype); 19206ad42b2SJohn Baldwin 19306ad42b2SJohn Baldwin /* 19406ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 19506ad42b2SJohn Baldwin * release their reference. 19606ad42b2SJohn Baldwin */ 19706ad42b2SJohn Baldwin while (p->p_lock > 0) 19806ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 199ebceaf6dSDavid Xu 200e602ba25SJulian Elischer PROC_UNLOCK(p); 201b40ce416SJulian Elischer 20200c28d96SRobert Watson #ifdef AUDIT 20300c28d96SRobert Watson /* 20400c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 20500c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 20600c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 20700c28d96SRobert Watson * what the return value is. 20800c28d96SRobert Watson */ 20900c28d96SRobert Watson AUDIT_ARG(exit, WEXITSTATUS(rv), 0); 21000c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 21100c28d96SRobert Watson #endif 21200c28d96SRobert Watson 2137a6b989bSJohn Baldwin /* Are we a task leader? */ 2142c1011f7SJohn Dyson if (p == p->p_leader) { 215c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 2162c1011f7SJohn Dyson q = p->p_peers; 217776e0b36SJohn Baldwin while (q != NULL) { 218776e0b36SJohn Baldwin PROC_LOCK(q); 219776e0b36SJohn Baldwin psignal(q, SIGKILL); 220776e0b36SJohn Baldwin PROC_UNLOCK(q); 2212c1011f7SJohn Dyson q = q->p_peers; 2222c1011f7SJohn Dyson } 223c6544064SJohn Baldwin while (p->p_peers != NULL) 224c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 225c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 2262c1011f7SJohn Dyson } 2272c1011f7SJohn Dyson 228fed06968SJulian Elischer /* 229e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 2306111dcd2SJohn Baldwin * E.g. SYSV IPC stuff 231fed06968SJulian Elischer * XXX what if one of these generates an error? 232fed06968SJulian Elischer */ 23375b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exit, p); 234a914fb6bSJohn Baldwin 235df8bae1dSRodney W. Grimes MALLOC(p->p_ru, struct rusage *, sizeof(struct rusage), 236a163d034SWarner Losh M_ZOMBIE, M_WAITOK); 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 239df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 240df8bae1dSRodney W. Grimes */ 241a914fb6bSJohn Baldwin PROC_LOCK(p); 242a282253aSJulian Elischer stopprofclock(p); 243df8bae1dSRodney W. Grimes p->p_flag &= ~(P_TRACED | P_PPWAIT); 2445499ea01SJohn Baldwin 2455499ea01SJohn Baldwin /* 2465499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 2475499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 2485499ea01SJohn Baldwin */ 2495499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 2505499ea01SJohn Baldwin callout_stop(&p->p_itcallout) == 0) { 2515499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 2525499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 2535499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 2545499ea01SJohn Baldwin ("realtime timer is still armed")); 2555499ea01SJohn Baldwin } 25696d7f8efSTim J. Robbins PROC_UNLOCK(p); 257df8bae1dSRodney W. Grimes 258df8bae1dSRodney W. Grimes /* 259831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 260831d27a9SDon Lewis * F_SETOWN with our pid. 261831d27a9SDon Lewis */ 262831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 263831d27a9SDon Lewis 264831d27a9SDon Lewis /* 265c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 266c0bc2867SGleb Smirnoff */ 267c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 268c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 269c0bc2867SGleb Smirnoff 270c0bc2867SGleb Smirnoff /* 271df8bae1dSRodney W. Grimes * Close open files and release open-file table. 272df8bae1dSRodney W. Grimes * This may block! 273df8bae1dSRodney W. Grimes */ 274edf6699aSAlfred Perlstein fdfree(td); 275df8bae1dSRodney W. Grimes 276a914fb6bSJohn Baldwin /* 277a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 278a2587073SPoul-Henning Kamp * stop before we return to userland 279a2587073SPoul-Henning Kamp */ 280a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 281a2587073SPoul-Henning Kamp g_waitidle(); 282a2587073SPoul-Henning Kamp 283a2587073SPoul-Henning Kamp /* 284a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 285a914fb6bSJohn Baldwin */ 286c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 28779fc0bf4SMike Smith if (p->p_leader->p_peers) { 28879fc0bf4SMike Smith q = p->p_leader; 28979fc0bf4SMike Smith while (q->p_peers != p) 29079fc0bf4SMike Smith q = q->p_peers; 29179fc0bf4SMike Smith q->p_peers = p->p_peers; 2927f05b035SAlfred Perlstein wakeup(p->p_leader); 29379fc0bf4SMike Smith } 294c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 29579fc0bf4SMike Smith 29657051fdcSTor Egge vmspace_exit(td); 297df8bae1dSRodney W. Grimes 2987ca6b782SChristian S.J. Peron mtx_lock(&Giant); /* XXX TTY */ 299ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 300df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 301830c3153SDag-Erling Smørgrav struct session *sp; 302df8bae1dSRodney W. Grimes 303f591779bSSeigo Tanimura sp = p->p_session; 304df8bae1dSRodney W. Grimes if (sp->s_ttyvp) { 305df8bae1dSRodney W. Grimes /* 306df8bae1dSRodney W. Grimes * Controlling process. 307df8bae1dSRodney W. Grimes * Signal foreground pgrp, 308df8bae1dSRodney W. Grimes * drain controlling terminal 309df8bae1dSRodney W. Grimes * and revoke access to controlling terminal. 310df8bae1dSRodney W. Grimes */ 311dd45d8adSJulian Elischer if (sp->s_ttyp && (sp->s_ttyp->t_session == sp)) { 312f591779bSSeigo Tanimura tp = sp->s_ttyp; 313f591779bSSeigo Tanimura if (sp->s_ttyp->t_pgrp) { 314f591779bSSeigo Tanimura PGRP_LOCK(sp->s_ttyp->t_pgrp); 315df8bae1dSRodney W. Grimes pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1); 316f591779bSSeigo Tanimura PGRP_UNLOCK(sp->s_ttyp->t_pgrp); 317f591779bSSeigo Tanimura } 318f591779bSSeigo Tanimura /* XXX tp should be locked. */ 319ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 320f591779bSSeigo Tanimura (void) ttywait(tp); 321ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 322df8bae1dSRodney W. Grimes /* 323df8bae1dSRodney W. Grimes * The tty could have been revoked 324df8bae1dSRodney W. Grimes * if we blocked. 325df8bae1dSRodney W. Grimes */ 326f591779bSSeigo Tanimura if (sp->s_ttyvp) { 327f591779bSSeigo Tanimura ttyvp = sp->s_ttyvp; 328f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 329df8bae1dSRodney W. Grimes sp->s_ttyvp = NULL; 330f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 331ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 3322b3183a8SJeff Roberson VOP_LOCK(ttyvp, LK_EXCLUSIVE, td); 333f591779bSSeigo Tanimura VOP_REVOKE(ttyvp, REVOKEALL); 3342b3183a8SJeff Roberson vput(ttyvp); 335ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 336f591779bSSeigo Tanimura } 337f591779bSSeigo Tanimura } 338f591779bSSeigo Tanimura if (sp->s_ttyvp) { 339f591779bSSeigo Tanimura ttyvp = sp->s_ttyvp; 340f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 341f591779bSSeigo Tanimura sp->s_ttyvp = NULL; 342f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 343f591779bSSeigo Tanimura vrele(ttyvp); 344f591779bSSeigo Tanimura } 345df8bae1dSRodney W. Grimes /* 346df8bae1dSRodney W. Grimes * s_ttyp is not zero'd; we use this to indicate 347df8bae1dSRodney W. Grimes * that the session once had a controlling terminal. 348df8bae1dSRodney W. Grimes * (for logging and informational purposes) 349df8bae1dSRodney W. Grimes */ 350df8bae1dSRodney W. Grimes } 351f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 352df8bae1dSRodney W. Grimes sp->s_leader = NULL; 353f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 354f591779bSSeigo Tanimura } 355df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 356ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 357b40ce416SJulian Elischer (void)acct_process(td); 3587ca6b782SChristian S.J. Peron mtx_unlock(&Giant); 359df8bae1dSRodney W. Grimes #ifdef KTRACE 360df8bae1dSRodney W. Grimes /* 3612c255e9dSRobert Watson * Drain any pending records on the thread and release the trace 3622c255e9dSRobert Watson * file. It might be better if drain-and-clear were atomic. 363df8bae1dSRodney W. Grimes */ 3642c255e9dSRobert Watson ktrprocexit(td); 365d7aadbf9SJohn Baldwin PROC_LOCK(p); 3666c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 367df8bae1dSRodney W. Grimes p->p_traceflag = 0; /* don't trace the vrele() */ 368a5881ea5SJohn Baldwin tracevp = p->p_tracevp; 369a5881ea5SJohn Baldwin p->p_tracevp = NULL; 370a5881ea5SJohn Baldwin tracecred = p->p_tracecred; 371a5881ea5SJohn Baldwin p->p_tracecred = NULL; 3726c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 373d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 37415088317SBrian Feldman if (tracevp != NULL) { 37557606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(tracevp->v_mount); 376d7aadbf9SJohn Baldwin vrele(tracevp); 37757606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 37815088317SBrian Feldman } 379a5881ea5SJohn Baldwin if (tracecred != NULL) 380a5881ea5SJohn Baldwin crfree(tracecred); 381df8bae1dSRodney W. Grimes #endif 382df8bae1dSRodney W. Grimes /* 383ee42d0a9SDavid Malone * Release reference to text vnode 384ee42d0a9SDavid Malone */ 385ee42d0a9SDavid Malone if ((vtmp = p->p_textvp) != NULL) { 386ee42d0a9SDavid Malone p->p_textvp = NULL; 38757606880SChristian S.J. Peron locked = VFS_LOCK_GIANT(vtmp->v_mount); 388ee42d0a9SDavid Malone vrele(vtmp); 38957606880SChristian S.J. Peron VFS_UNLOCK_GIANT(locked); 390ee42d0a9SDavid Malone } 391ee42d0a9SDavid Malone 392ee42d0a9SDavid Malone /* 393d7aadbf9SJohn Baldwin * Release our limits structure. 394d7aadbf9SJohn Baldwin */ 39591d5354aSJohn Baldwin PROC_LOCK(p); 39691d5354aSJohn Baldwin plim = p->p_limit; 397d7aadbf9SJohn Baldwin p->p_limit = NULL; 39891d5354aSJohn Baldwin PROC_UNLOCK(p); 39991d5354aSJohn Baldwin lim_free(plim); 400d7aadbf9SJohn Baldwin 401d7aadbf9SJohn Baldwin /* 402df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 403df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 404df8bae1dSRodney W. Grimes */ 4051005a129SJohn Baldwin sx_xlock(&allproc_lock); 406b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 407b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 408b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4091005a129SJohn Baldwin sx_xunlock(&allproc_lock); 410df8bae1dSRodney W. Grimes 41137f84a60SJohn Baldwin /* 41237f84a60SJohn Baldwin * Reparent all of our children to init. 41337f84a60SJohn Baldwin */ 4141005a129SJohn Baldwin sx_xlock(&proctree_lock); 4152e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 416a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 4177f05b035SAlfred Perlstein wakeup(initproc); 418a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4192e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 420a914fb6bSJohn Baldwin PROC_LOCK(q); 421c65437a3SJohn Baldwin proc_reparent(q, initproc); 4224ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 423df8bae1dSRodney W. Grimes /* 424df8bae1dSRodney W. Grimes * Traced processes are killed 425df8bae1dSRodney W. Grimes * since their existence means someone is screwing up. 426df8bae1dSRodney W. Grimes */ 427df8bae1dSRodney W. Grimes if (q->p_flag & P_TRACED) { 428c2836532SDavid Xu q->p_flag &= ~(P_TRACED | P_STOPPED_TRACE); 429df8bae1dSRodney W. Grimes psignal(q, SIGKILL); 430c65437a3SJohn Baldwin } 431a914fb6bSJohn Baldwin PROC_UNLOCK(q); 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes /* 43578c85e8dSJohn Baldwin * Save exit status and finalize rusage info except for times, 43603001f59SStephan Uphoff * adding in child rusage info later when our time is locked. 437df8bae1dSRodney W. Grimes */ 438d7aadbf9SJohn Baldwin PROC_LOCK(p); 439df8bae1dSRodney W. Grimes p->p_xstat = rv; 440cbf4e354SDavid Xu p->p_xthread = td; 44178c85e8dSJohn Baldwin p->p_stats->p_ru.ru_nvcsw++; 442df8bae1dSRodney W. Grimes *p->p_ru = p->p_stats->p_ru; 443df8bae1dSRodney W. Grimes 444df8bae1dSRodney W. Grimes /* 4457a6b989bSJohn Baldwin * Notify interested parties of our demise. 446cb679c38SJonathan Lemon */ 447ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 4487eaec467SJohn Baldwin 4491a29c806SOlivier Houchard /* 4501a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 4511a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 4521a29c806SOlivier Houchard * can beat us if we don't. 4531a29c806SOlivier Houchard */ 454ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 455cb679c38SJonathan Lemon 456cb679c38SJonathan Lemon /* 457645682fdSLuoqi Chen * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 458ba1551caSIan Dowse * flag set, or if the handler is set to SIG_IGN, notify process 459ba1551caSIan Dowse * 1 instead (and hope it will handle this situation). 460df8bae1dSRodney W. Grimes */ 461d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 46290af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 46390af4afaSJohn Baldwin if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 464276c5169SJohn Baldwin struct proc *pp; 465276c5169SJohn Baldwin 46690af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 467276c5169SJohn Baldwin pp = p->p_pptr; 468f591779bSSeigo Tanimura PROC_UNLOCK(pp); 469245f17d4SJoerg Wunsch proc_reparent(p, initproc); 47055b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 471f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 472245f17d4SJoerg Wunsch /* 473245f17d4SJoerg Wunsch * If this was the last child of our parent, notify 474245f17d4SJoerg Wunsch * parent, so in case he was wait(2)ing, he will 475245f17d4SJoerg Wunsch * continue. 476245f17d4SJoerg Wunsch */ 477245f17d4SJoerg Wunsch if (LIST_EMPTY(&pp->p_children)) 4787f05b035SAlfred Perlstein wakeup(pp); 47990af4afaSJohn Baldwin } else 48090af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 481245f17d4SJoerg Wunsch 4826567eef7SDon Lewis if (p->p_pptr == initproc) 4836626c604SJulian Elischer psignal(p->p_pptr, SIGCHLD); 484ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 485ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 486ebceaf6dSDavid Xu childproc_exited(p); 487ebceaf6dSDavid Xu else /* LINUX thread */ 4886567eef7SDon Lewis psignal(p->p_pptr, p->p_sigparent); 489ebceaf6dSDavid Xu } 490c65437a3SJohn Baldwin PROC_UNLOCK(p->p_pptr); 491c65437a3SJohn Baldwin PROC_UNLOCK(p); 4928e2e767bSJohn Baldwin 4938e2e767bSJohn Baldwin /* 494df8bae1dSRodney W. Grimes * Finally, call machine-dependent code to release the remaining 495696058c3SJulian Elischer * resources including address space. 496582ec34cSAlfred Perlstein * The address space is released by "vmspace_exitfree(p)" in 497582ec34cSAlfred Perlstein * vm_waitproc(). 498df8bae1dSRodney W. Grimes */ 499b40ce416SJulian Elischer cpu_exit(td); 500eb30c1c0SPeter Wemm 50183de502dSJohn Baldwin WITNESS_WARN(WARN_PANIC, &proctree_lock.sx_object, 50283de502dSJohn Baldwin "process (pid %d) exiting", p->p_pid); 50383de502dSJohn Baldwin 504eb30c1c0SPeter Wemm PROC_LOCK(p); 505d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 506d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 507696058c3SJulian Elischer 508eb30c1c0SPeter Wemm /* 509f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 510f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 511f71e748dSDavid Xu * for signal queue at the time when the state is set. 512f71e748dSDavid Xu */ 513f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 514f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 515f71e748dSDavid Xu 516f71e748dSDavid Xu /* 517462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 518a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 519a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 520ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 521ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 522ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 523ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 524ddf9c4f7SJohn Baldwin * proc lock. 525eb30c1c0SPeter Wemm */ 526ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 527e5bb601dSJohn Baldwin mtx_lock_spin(&sched_lock); 528e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 529d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 530871684b8SBruce Evans 53155d44f79SJulian Elischer sched_exit(p->p_pptr, td); 532f6f230feSJeff Roberson 533f6f230feSJeff Roberson /* 5347eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 535ad3b9257SJohn-Mark Gurney * late in the game. 536ad3b9257SJohn-Mark Gurney */ 537ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 538ad3b9257SJohn-Mark Gurney 539ad3b9257SJohn-Mark Gurney /* 540696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 541e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 542696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 543e602ba25SJulian Elischer */ 544e602ba25SJulian Elischer thread_exit(); 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes 54725f6e35aSPoul-Henning Kamp 54825f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 54925f6e35aSPoul-Henning Kamp struct abort2_args { 55025f6e35aSPoul-Henning Kamp char *why; 55125f6e35aSPoul-Henning Kamp int nargs; 55225f6e35aSPoul-Henning Kamp void **args; 55325f6e35aSPoul-Henning Kamp }; 55425f6e35aSPoul-Henning Kamp #endif 55525f6e35aSPoul-Henning Kamp 55625f6e35aSPoul-Henning Kamp /* 55725f6e35aSPoul-Henning Kamp * MPSAFE. 55825f6e35aSPoul-Henning Kamp */ 55925f6e35aSPoul-Henning Kamp int 56025f6e35aSPoul-Henning Kamp abort2(struct thread *td, struct abort2_args *uap) 56125f6e35aSPoul-Henning Kamp { 56225f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 56325f6e35aSPoul-Henning Kamp struct sbuf *sb; 56425f6e35aSPoul-Henning Kamp void *uargs[16]; 56525f6e35aSPoul-Henning Kamp int error, i, sig; 56625f6e35aSPoul-Henning Kamp 56725f6e35aSPoul-Henning Kamp error = 0; /* satisfy compiler */ 56825f6e35aSPoul-Henning Kamp 56925f6e35aSPoul-Henning Kamp /* 57025f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 57125f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 57225f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 57325f6e35aSPoul-Henning Kamp */ 57425f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 57525f6e35aSPoul-Henning Kamp sbuf_clear(sb); 57625f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 57725f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 57825f6e35aSPoul-Henning Kamp /* 57925f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 58025f6e35aSPoul-Henning Kamp * abort2() was called improperly 58125f6e35aSPoul-Henning Kamp */ 58225f6e35aSPoul-Henning Kamp sig = SIGKILL; 58325f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 58425f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 58525f6e35aSPoul-Henning Kamp goto out; 58625f6e35aSPoul-Henning Kamp if (uap->args == NULL) 58725f6e35aSPoul-Henning Kamp goto out; 58825f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 58925f6e35aSPoul-Henning Kamp if (error != 0) 59025f6e35aSPoul-Henning Kamp goto out; 59125f6e35aSPoul-Henning Kamp /* 59225f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 59325f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 59425f6e35aSPoul-Henning Kamp */ 59525f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 59625f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 59725f6e35aSPoul-Henning Kamp if (error < 0) 59825f6e35aSPoul-Henning Kamp goto out; 59925f6e35aSPoul-Henning Kamp } else { 60025f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 60125f6e35aSPoul-Henning Kamp } 60225f6e35aSPoul-Henning Kamp if (uap->nargs) { 60325f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 60425f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 60525f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 60625f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 60725f6e35aSPoul-Henning Kamp } 60825f6e35aSPoul-Henning Kamp /* 60925f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 61025f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 61125f6e35aSPoul-Henning Kamp * from user-space succeed. 61225f6e35aSPoul-Henning Kamp */ 61325f6e35aSPoul-Henning Kamp sig = SIGABRT; 61425f6e35aSPoul-Henning Kamp out: 61525f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 61625f6e35aSPoul-Henning Kamp sbuf_trim(sb); 61725f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 61825f6e35aSPoul-Henning Kamp } 61925f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 62025f6e35aSPoul-Henning Kamp sbuf_finish(sb); 62125f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 62225f6e35aSPoul-Henning Kamp sbuf_delete(sb); 62325f6e35aSPoul-Henning Kamp exit1(td, W_EXITCODE(0, sig)); 62425f6e35aSPoul-Henning Kamp return (0); 62525f6e35aSPoul-Henning Kamp } 62625f6e35aSPoul-Henning Kamp 62725f6e35aSPoul-Henning Kamp 628b2f9e8b1SBruce Evans #ifdef COMPAT_43 629234216efSMatthew Dillon /* 630a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 631a9a64385SJohn Baldwin * 632a9a64385SJohn Baldwin * MPSAFE. 633234216efSMatthew Dillon */ 63426f9a767SRodney W. Grimes int 635830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 636df8bae1dSRodney W. Grimes { 637b7e23e82SJohn Baldwin int error, status; 638df8bae1dSRodney W. Grimes 639b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 640b7e23e82SJohn Baldwin if (error == 0) 641b7e23e82SJohn Baldwin td->td_retval[1] = status; 642b7e23e82SJohn Baldwin return (error); 643df8bae1dSRodney W. Grimes } 644b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 645df8bae1dSRodney W. Grimes 646234216efSMatthew Dillon /* 647a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 648a9a64385SJohn Baldwin * 649a9a64385SJohn Baldwin * MPSAFE. 650234216efSMatthew Dillon */ 65126f9a767SRodney W. Grimes int 652830c3153SDag-Erling Smørgrav wait4(struct thread *td, struct wait_args *uap) 653df8bae1dSRodney W. Grimes { 65478c85e8dSJohn Baldwin struct rusage ru, *rup; 655b7e23e82SJohn Baldwin int error, status; 656b40ce416SJulian Elischer 65778c85e8dSJohn Baldwin if (uap->rusage != NULL) 65878c85e8dSJohn Baldwin rup = &ru; 65978c85e8dSJohn Baldwin else 66078c85e8dSJohn Baldwin rup = NULL; 66178c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 662b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 663b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 664b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 665b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 666b7e23e82SJohn Baldwin return (error); 667df8bae1dSRodney W. Grimes } 668df8bae1dSRodney W. Grimes 669b7e23e82SJohn Baldwin int 6707eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 6717eaec467SJohn Baldwin struct rusage *rusage) 672df8bae1dSRodney W. Grimes { 67348bfcdddSJulian Elischer struct proc *p, *q, *t; 674a9a64385SJohn Baldwin int error, nfound; 675df8bae1dSRodney W. Grimes 676de3007e8SWayne Salamon AUDIT_ARG(pid, pid); 677de3007e8SWayne Salamon 678b40ce416SJulian Elischer q = td->td_proc; 679b7e23e82SJohn Baldwin if (pid == 0) { 680f591779bSSeigo Tanimura PROC_LOCK(q); 681b7e23e82SJohn Baldwin pid = -q->p_pgid; 682f591779bSSeigo Tanimura PROC_UNLOCK(q); 683f591779bSSeigo Tanimura } 684b7e23e82SJohn Baldwin if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WLINUXCLONE)) 6856dc958b9SJohn Baldwin return (EINVAL); 686df8bae1dSRodney W. Grimes loop: 687902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 688902c0d82SDavid Xu PROC_LOCK(q); 689902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 690902c0d82SDavid Xu PROC_UNLOCK(q); 691902c0d82SDavid Xu } 692df8bae1dSRodney W. Grimes nfound = 0; 693d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 6942e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 695f591779bSSeigo Tanimura PROC_LOCK(p); 696b7e23e82SJohn Baldwin if (pid != WAIT_ANY && 697b7e23e82SJohn Baldwin p->p_pid != pid && p->p_pgid != -pid) { 698f591779bSSeigo Tanimura PROC_UNLOCK(p); 699df8bae1dSRodney W. Grimes continue; 700f591779bSSeigo Tanimura } 701babe9a2bSRobert Watson if (p_canwait(td, p)) { 702babe9a2bSRobert Watson PROC_UNLOCK(p); 703babe9a2bSRobert Watson continue; 704babe9a2bSRobert Watson } 7054ac9ae70SJulian Elischer 7061156bc4dSJake Burkholder /* 7071156bc4dSJake Burkholder * This special case handles a kthread spawned by linux_clone 7081156bc4dSJake Burkholder * (see linux_misc.c). The linux_wait4 and linux_waitpid 7091156bc4dSJake Burkholder * functions need to be able to distinguish between waiting 7101156bc4dSJake Burkholder * on a process and waiting on a thread. It is a thread if 7111156bc4dSJake Burkholder * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 7121156bc4dSJake Burkholder * signifies we want to wait for threads and not processes. 7134ac9ae70SJulian Elischer */ 7141156bc4dSJake Burkholder if ((p->p_sigparent != SIGCHLD) ^ 715b7e23e82SJohn Baldwin ((options & WLINUXCLONE) != 0)) { 716a914fb6bSJohn Baldwin PROC_UNLOCK(p); 7174ac9ae70SJulian Elischer continue; 718a914fb6bSJohn Baldwin } 7194ac9ae70SJulian Elischer 720df8bae1dSRodney W. Grimes nfound++; 721e602ba25SJulian Elischer if (p->p_state == PRS_ZOMBIE) { 722ddf9c4f7SJohn Baldwin 723ddf9c4f7SJohn Baldwin /* 724ddf9c4f7SJohn Baldwin * It is possible that the last thread of this 725ddf9c4f7SJohn Baldwin * process is still running on another CPU 726ddf9c4f7SJohn Baldwin * in thread_exit() after having dropped the process 727ddf9c4f7SJohn Baldwin * lock via PROC_UNLOCK() but before it has completed 728ddf9c4f7SJohn Baldwin * cpu_throw(). In that case, the other thread must 729ddf9c4f7SJohn Baldwin * still hold sched_lock, so simply by acquiring 730ddf9c4f7SJohn Baldwin * sched_lock once we will wait long enough for the 731ddf9c4f7SJohn Baldwin * thread to exit in that case. 732ddf9c4f7SJohn Baldwin */ 733ddf9c4f7SJohn Baldwin mtx_lock_spin(&sched_lock); 734ddf9c4f7SJohn Baldwin mtx_unlock_spin(&sched_lock); 735ddf9c4f7SJohn Baldwin 736b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 737b7e23e82SJohn Baldwin if (status) 738b7e23e82SJohn Baldwin *status = p->p_xstat; /* convert to int */ 73978c85e8dSJohn Baldwin if (rusage) { 740fd544ee8SRobert Watson *rusage = *p->p_ru; 74178c85e8dSJohn Baldwin calcru(p, &rusage->ru_utime, &rusage->ru_stime); 74278c85e8dSJohn Baldwin } 743b7e23e82SJohn Baldwin 744ebceaf6dSDavid Xu PROC_LOCK(q); 745ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 746ebceaf6dSDavid Xu PROC_UNLOCK(q); 747ebceaf6dSDavid Xu 748df8bae1dSRodney W. Grimes /* 749df8bae1dSRodney W. Grimes * If we got the child via a ptrace 'attach', 750df8bae1dSRodney W. Grimes * we need to give it back to the old parent. 751df8bae1dSRodney W. Grimes */ 752b7e23e82SJohn Baldwin PROC_UNLOCK(p); 753d7aadbf9SJohn Baldwin if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 754c65437a3SJohn Baldwin PROC_LOCK(p); 755df8bae1dSRodney W. Grimes p->p_oppid = 0; 756df8bae1dSRodney W. Grimes proc_reparent(p, t); 757c65437a3SJohn Baldwin PROC_UNLOCK(p); 758ebceaf6dSDavid Xu tdsignal(t, NULL, SIGCHLD, p->p_ksi); 7597f05b035SAlfred Perlstein wakeup(t); 760c65437a3SJohn Baldwin PROC_UNLOCK(t); 7611005a129SJohn Baldwin sx_xunlock(&proctree_lock); 762d7aadbf9SJohn Baldwin return (0); 763df8bae1dSRodney W. Grimes } 764ebdc3f1dSSeigo Tanimura 7653890793eSTim J. Robbins /* 7663890793eSTim J. Robbins * Remove other references to this process to ensure 7673890793eSTim J. Robbins * we have an exclusive reference. 7683890793eSTim J. Robbins */ 7696ec62361STim J. Robbins sx_xlock(&allproc_lock); 7706ec62361STim J. Robbins LIST_REMOVE(p, p_list); /* off zombproc */ 7716ec62361STim J. Robbins sx_xunlock(&allproc_lock); 7726ec62361STim J. Robbins LIST_REMOVE(p, p_sibling); 7733890793eSTim J. Robbins leavepgrp(p); 774ebdc3f1dSSeigo Tanimura sx_xunlock(&proctree_lock); 775ebdc3f1dSSeigo Tanimura 776ebdc3f1dSSeigo Tanimura /* 777d7aadbf9SJohn Baldwin * As a side effect of this lock, we know that 778d7aadbf9SJohn Baldwin * all other writes to this proc are visible now, so 779d7aadbf9SJohn Baldwin * no more locking is needed for p. 780d7aadbf9SJohn Baldwin */ 781d7aadbf9SJohn Baldwin PROC_LOCK(p); 782d7aadbf9SJohn Baldwin p->p_xstat = 0; /* XXX: why? */ 783d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 784d7aadbf9SJohn Baldwin PROC_LOCK(q); 78578c85e8dSJohn Baldwin ruadd(&q->p_stats->p_cru, &q->p_crux, p->p_ru, 78678c85e8dSJohn Baldwin &p->p_rux); 787d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 788d7aadbf9SJohn Baldwin FREE(p->p_ru, M_ZOMBIE); 789d7aadbf9SJohn Baldwin p->p_ru = NULL; 790d7aadbf9SJohn Baldwin 791d7aadbf9SJohn Baldwin /* 792d7aadbf9SJohn Baldwin * Decrement the count of procs running with this uid. 793d7aadbf9SJohn Baldwin */ 794d7aadbf9SJohn Baldwin (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 795d7aadbf9SJohn Baldwin 796d7aadbf9SJohn Baldwin /* 797a9a64385SJohn Baldwin * Free credentials, arguments, and sigacts. 798df8bae1dSRodney W. Grimes */ 799da654d90SPoul-Henning Kamp crfree(p->p_ucred); 80090af4afaSJohn Baldwin p->p_ucred = NULL; 801d7aadbf9SJohn Baldwin pargs_drop(p->p_args); 802d7aadbf9SJohn Baldwin p->p_args = NULL; 80390af4afaSJohn Baldwin sigacts_free(p->p_sigacts); 80490af4afaSJohn Baldwin p->p_sigacts = NULL; 80588c5ea45SJulian Elischer 806df8bae1dSRodney W. Grimes /* 807a9a64385SJohn Baldwin * Do any thread-system specific cleanups. 808e602ba25SJulian Elischer */ 809696058c3SJulian Elischer thread_wait(p); 810e602ba25SJulian Elischer 811e602ba25SJulian Elischer /* 812eb30c1c0SPeter Wemm * Give vm and machine-dependent layer a chance 813df8bae1dSRodney W. Grimes * to free anything that cpu_exit couldn't 814df8bae1dSRodney W. Grimes * release while still running in process context. 815df8bae1dSRodney W. Grimes */ 816eb30c1c0SPeter Wemm vm_waitproc(p); 8172555374cSRobert Watson #ifdef MAC 8182555374cSRobert Watson mac_destroy_proc(p); 8192555374cSRobert Watson #endif 820fcf7f27aSRobert Watson #ifdef AUDIT 821fcf7f27aSRobert Watson audit_proc_free(p); 822fcf7f27aSRobert Watson #endif 8231faf202eSJulian Elischer KASSERT(FIRST_THREAD_IN_PROC(p), 824b7e23e82SJohn Baldwin ("kern_wait: no residual thread!")); 825c897b813SJeff Roberson uma_zfree(proc_zone, p); 826d7aadbf9SJohn Baldwin sx_xlock(&allproc_lock); 827df8bae1dSRodney W. Grimes nprocs--; 828d7aadbf9SJohn Baldwin sx_xunlock(&allproc_lock); 829d7aadbf9SJohn Baldwin return (0); 830df8bae1dSRodney W. Grimes } 831112afcb2SJohn Baldwin mtx_lock_spin(&sched_lock); 8325a2f73e6SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && 8335a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 834a9a64385SJohn Baldwin (p->p_flag & P_WAITED) == 0 && 835b7e23e82SJohn Baldwin (p->p_flag & P_TRACED || options & WUNTRACED)) { 836112afcb2SJohn Baldwin mtx_unlock_spin(&sched_lock); 837df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 838d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 839b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 840b7e23e82SJohn Baldwin if (status) 841b7e23e82SJohn Baldwin *status = W_STOPCODE(p->p_xstat); 842ebceaf6dSDavid Xu 843ebceaf6dSDavid Xu PROC_LOCK(q); 844ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 845ebceaf6dSDavid Xu PROC_UNLOCK(q); 846e94cc4acSDavid Xu PROC_UNLOCK(p); 847ebceaf6dSDavid Xu 848b7e23e82SJohn Baldwin return (0); 849df8bae1dSRodney W. Grimes } 850112afcb2SJohn Baldwin mtx_unlock_spin(&sched_lock); 851b7e23e82SJohn Baldwin if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 8526ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 8536ee093fbSMike Barcroft td->td_retval[0] = p->p_pid; 8546ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 8556ee093fbSMike Barcroft 856ebceaf6dSDavid Xu PROC_LOCK(q); 857ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 858ebceaf6dSDavid Xu PROC_UNLOCK(q); 859e94cc4acSDavid Xu PROC_UNLOCK(p); 860ebceaf6dSDavid Xu 861b7e23e82SJohn Baldwin if (status) 862b7e23e82SJohn Baldwin *status = SIGCONT; 863b7e23e82SJohn Baldwin return (0); 8646ee093fbSMike Barcroft } 865d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 866d7aadbf9SJohn Baldwin } 867d7aadbf9SJohn Baldwin if (nfound == 0) { 868d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 869d7aadbf9SJohn Baldwin return (ECHILD); 870d7aadbf9SJohn Baldwin } 871b7e23e82SJohn Baldwin if (options & WNOHANG) { 872d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 873d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 874d7aadbf9SJohn Baldwin return (0); 875d7aadbf9SJohn Baldwin } 876d7aadbf9SJohn Baldwin PROC_LOCK(q); 877d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 87895992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 87995992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 88095992d56SDavid Xu error = 0; 88195992d56SDavid Xu } else 8827f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 883d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 8844ae89b95SJohn Baldwin if (error) 885d7aadbf9SJohn Baldwin return (error); 886d7aadbf9SJohn Baldwin goto loop; 887d7aadbf9SJohn Baldwin } 888df8bae1dSRodney W. Grimes 889df8bae1dSRodney W. Grimes /* 89098f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 89198f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 892df8bae1dSRodney W. Grimes */ 893df8bae1dSRodney W. Grimes void 894830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 895df8bae1dSRodney W. Grimes { 896df8bae1dSRodney W. Grimes 8974e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 898c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 899df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 900df8bae1dSRodney W. Grimes return; 901df8bae1dSRodney W. Grimes 902ff766807SDavid Xu PROC_LOCK(child->p_pptr); 903ff766807SDavid Xu sigqueue_take(child->p_ksi); 904ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 905b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 906b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 907df8bae1dSRodney W. Grimes child->p_pptr = parent; 908df8bae1dSRodney W. Grimes } 909