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" 432555374cSRobert Watson #include "opt_mac.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 475fdb8324SBruce Evans #include <sys/sysproto.h> 4875b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 491c5bb3eaSPeter Wemm #include <sys/kernel.h> 50a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 51f34fa851SJohn Baldwin #include <sys/lock.h> 5235e0e5b3SJohn Baldwin #include <sys/mutex.h> 53df8bae1dSRodney W. Grimes #include <sys/proc.h> 542a024a2bSSean Eric Fagan #include <sys/pioctl.h> 55413628a7SBjoern A. Zeeb #include <sys/jail.h> 56df8bae1dSRodney W. Grimes #include <sys/tty.h> 57df8bae1dSRodney W. Grimes #include <sys/wait.h> 58eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 597a6b989bSJohn Baldwin #include <sys/vnode.h> 60df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 6125f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 62797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 63b43179fbSJeff Roberson #include <sys/sched.h> 641005a129SJohn Baldwin #include <sys/sx.h> 65c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6625f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 67df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 687c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 69797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 705d217f17SJohn Birrell #include <sys/sdt.h> 71780dc5a8SPeter Wemm #include <sys/shm.h> 72780dc5a8SPeter Wemm #include <sys/sem.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 457413628a7SBjoern A. Zeeb /* In case we are jailed tell the prison that we are gone. */ 458413628a7SBjoern A. Zeeb if (jailed(p->p_ucred)) 459413628a7SBjoern A. Zeeb prison_proc_free(p->p_ucred->cr_prison); 460413628a7SBjoern A. Zeeb 4615d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4625d217f17SJohn Birrell /* 4635d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 4645d217f17SJohn Birrell * has declared an interest. 4655d217f17SJohn Birrell */ 4665d217f17SJohn Birrell if (dtrace_fasttrap_exit) 4675d217f17SJohn Birrell dtrace_fasttrap_exit(p); 4685d217f17SJohn Birrell #endif 4695d217f17SJohn Birrell 4701c4bcd05SJeff Roberson /* 4717a6b989bSJohn Baldwin * Notify interested parties of our demise. 472cb679c38SJonathan Lemon */ 473ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 4747eaec467SJohn Baldwin 4755d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4765d217f17SJohn Birrell int reason = CLD_EXITED; 4775d217f17SJohn Birrell if (WCOREDUMP(rv)) 4785d217f17SJohn Birrell reason = CLD_DUMPED; 4795d217f17SJohn Birrell else if (WIFSIGNALED(rv)) 4805d217f17SJohn Birrell reason = CLD_KILLED; 4815d217f17SJohn Birrell SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 4825d217f17SJohn Birrell #endif 4835d217f17SJohn Birrell 4841a29c806SOlivier Houchard /* 4851a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 4861a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 4871a29c806SOlivier Houchard * can beat us if we don't. 4881a29c806SOlivier Houchard */ 489ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 490cb679c38SJonathan Lemon 491cb679c38SJonathan Lemon /* 492645682fdSLuoqi Chen * Notify parent that we're gone. If parent has the PS_NOCLDWAIT 493ba1551caSIan Dowse * flag set, or if the handler is set to SIG_IGN, notify process 494ba1551caSIan Dowse * 1 instead (and hope it will handle this situation). 495df8bae1dSRodney W. Grimes */ 496d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 49790af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 49890af4afaSJohn Baldwin if (p->p_pptr->p_sigacts->ps_flag & (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 499276c5169SJohn Baldwin struct proc *pp; 500276c5169SJohn Baldwin 50190af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 502276c5169SJohn Baldwin pp = p->p_pptr; 503f591779bSSeigo Tanimura PROC_UNLOCK(pp); 504245f17d4SJoerg Wunsch proc_reparent(p, initproc); 50555b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 506f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 507007abb3dSKonstantin Belousov 508245f17d4SJoerg Wunsch /* 509007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 510007abb3dSKonstantin Belousov * executiing waitpid(2) with our pid, he will 511245f17d4SJoerg Wunsch * continue. 512245f17d4SJoerg Wunsch */ 5137f05b035SAlfred Perlstein wakeup(pp); 51490af4afaSJohn Baldwin } else 51590af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 516245f17d4SJoerg Wunsch 5176567eef7SDon Lewis if (p->p_pptr == initproc) 5186626c604SJulian Elischer psignal(p->p_pptr, SIGCHLD); 519ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 520ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 521ebceaf6dSDavid Xu childproc_exited(p); 522ebceaf6dSDavid Xu else /* LINUX thread */ 5236567eef7SDon Lewis psignal(p->p_pptr, p->p_sigparent); 524ebceaf6dSDavid Xu } 525d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 526696058c3SJulian Elischer 527eb30c1c0SPeter Wemm /* 528f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 529f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 530f71e748dSDavid Xu * for signal queue at the time when the state is set. 531f71e748dSDavid Xu */ 532f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 533f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 534f71e748dSDavid Xu 535f71e748dSDavid Xu /* 536462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 537a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 538a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 539ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 540ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 541ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 542ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 543ddf9c4f7SJohn Baldwin * proc lock. 544eb30c1c0SPeter Wemm */ 545ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 546aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 547982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 548982d11f8SJeff Roberson PROC_SLOCK(p); 549e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 550d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 551871684b8SBruce Evans 552f6f230feSJeff Roberson /* 5537eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 554ad3b9257SJohn-Mark Gurney * late in the game. 555ad3b9257SJohn-Mark Gurney */ 556ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 557ad3b9257SJohn-Mark Gurney 558ad3b9257SJohn-Mark Gurney /* 559a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 560a140976eSAttilio Rao */ 561a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 562a140976eSAttilio Rao 563a140976eSAttilio Rao /* 564696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 565e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 566696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 567e602ba25SJulian Elischer */ 568e602ba25SJulian Elischer thread_exit(); 569df8bae1dSRodney W. Grimes } 570df8bae1dSRodney W. Grimes 57125f6e35aSPoul-Henning Kamp 57225f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 57325f6e35aSPoul-Henning Kamp struct abort2_args { 57425f6e35aSPoul-Henning Kamp char *why; 57525f6e35aSPoul-Henning Kamp int nargs; 57625f6e35aSPoul-Henning Kamp void **args; 57725f6e35aSPoul-Henning Kamp }; 57825f6e35aSPoul-Henning Kamp #endif 57925f6e35aSPoul-Henning Kamp 58025f6e35aSPoul-Henning Kamp int 58125f6e35aSPoul-Henning Kamp abort2(struct thread *td, struct abort2_args *uap) 58225f6e35aSPoul-Henning Kamp { 58325f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 58425f6e35aSPoul-Henning Kamp struct sbuf *sb; 58525f6e35aSPoul-Henning Kamp void *uargs[16]; 58625f6e35aSPoul-Henning Kamp int error, i, sig; 58725f6e35aSPoul-Henning Kamp 58825f6e35aSPoul-Henning Kamp /* 58925f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 59025f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 59125f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 59225f6e35aSPoul-Henning Kamp */ 59325f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 59425f6e35aSPoul-Henning Kamp sbuf_clear(sb); 59525f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 59625f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 59725f6e35aSPoul-Henning Kamp /* 59825f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 59925f6e35aSPoul-Henning Kamp * abort2() was called improperly 60025f6e35aSPoul-Henning Kamp */ 60125f6e35aSPoul-Henning Kamp sig = SIGKILL; 60225f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 60325f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 60425f6e35aSPoul-Henning Kamp goto out; 6054218a731SPoul-Henning Kamp if (uap->nargs > 0) { 60625f6e35aSPoul-Henning Kamp if (uap->args == NULL) 60725f6e35aSPoul-Henning Kamp goto out; 60825f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 60925f6e35aSPoul-Henning Kamp if (error != 0) 61025f6e35aSPoul-Henning Kamp goto out; 6114218a731SPoul-Henning Kamp } 61225f6e35aSPoul-Henning Kamp /* 61325f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 61425f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 61525f6e35aSPoul-Henning Kamp */ 61625f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 61725f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 61825f6e35aSPoul-Henning Kamp if (error < 0) 61925f6e35aSPoul-Henning Kamp goto out; 62025f6e35aSPoul-Henning Kamp } else { 62125f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 62225f6e35aSPoul-Henning Kamp } 6234218a731SPoul-Henning Kamp if (uap->nargs > 0) { 62425f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 62525f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 62625f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 62725f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 62825f6e35aSPoul-Henning Kamp } 62925f6e35aSPoul-Henning Kamp /* 63025f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 63125f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 63225f6e35aSPoul-Henning Kamp * from user-space succeed. 63325f6e35aSPoul-Henning Kamp */ 63425f6e35aSPoul-Henning Kamp sig = SIGABRT; 63525f6e35aSPoul-Henning Kamp out: 63625f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 63725f6e35aSPoul-Henning Kamp sbuf_trim(sb); 63825f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 63925f6e35aSPoul-Henning Kamp } 64025f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 64125f6e35aSPoul-Henning Kamp sbuf_finish(sb); 64225f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 64325f6e35aSPoul-Henning Kamp sbuf_delete(sb); 64425f6e35aSPoul-Henning Kamp exit1(td, W_EXITCODE(0, sig)); 64525f6e35aSPoul-Henning Kamp return (0); 64625f6e35aSPoul-Henning Kamp } 64725f6e35aSPoul-Henning Kamp 64825f6e35aSPoul-Henning Kamp 649b2f9e8b1SBruce Evans #ifdef COMPAT_43 650234216efSMatthew Dillon /* 651a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 652234216efSMatthew Dillon */ 65326f9a767SRodney W. Grimes int 654830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 655df8bae1dSRodney W. Grimes { 656b7e23e82SJohn Baldwin int error, status; 657df8bae1dSRodney W. Grimes 658b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 659b7e23e82SJohn Baldwin if (error == 0) 660b7e23e82SJohn Baldwin td->td_retval[1] = status; 661b7e23e82SJohn Baldwin return (error); 662df8bae1dSRodney W. Grimes } 663b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 664df8bae1dSRodney W. Grimes 665234216efSMatthew Dillon /* 666a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 667234216efSMatthew Dillon */ 66826f9a767SRodney W. Grimes int 669830c3153SDag-Erling Smørgrav wait4(struct thread *td, struct wait_args *uap) 670df8bae1dSRodney W. Grimes { 67178c85e8dSJohn Baldwin struct rusage ru, *rup; 672b7e23e82SJohn Baldwin int error, status; 673b40ce416SJulian Elischer 67478c85e8dSJohn Baldwin if (uap->rusage != NULL) 67578c85e8dSJohn Baldwin rup = &ru; 67678c85e8dSJohn Baldwin else 67778c85e8dSJohn Baldwin rup = NULL; 67878c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 679b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 680b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 681b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 682b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 683b7e23e82SJohn Baldwin return (error); 684df8bae1dSRodney W. Grimes } 685df8bae1dSRodney W. Grimes 686b7e23e82SJohn Baldwin int 6877eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 6887eaec467SJohn Baldwin struct rusage *rusage) 689df8bae1dSRodney W. Grimes { 69048bfcdddSJulian Elischer struct proc *p, *q, *t; 691a9a64385SJohn Baldwin int error, nfound; 692df8bae1dSRodney W. Grimes 693de3007e8SWayne Salamon AUDIT_ARG(pid, pid); 694de3007e8SWayne Salamon 695b40ce416SJulian Elischer q = td->td_proc; 696b7e23e82SJohn Baldwin if (pid == 0) { 697f591779bSSeigo Tanimura PROC_LOCK(q); 698b7e23e82SJohn Baldwin pid = -q->p_pgid; 699f591779bSSeigo Tanimura PROC_UNLOCK(q); 700f591779bSSeigo Tanimura } 701cbc15844SKonstantin Belousov if (options &~ (WUNTRACED|WNOHANG|WCONTINUED|WNOWAIT|WLINUXCLONE)) 7026dc958b9SJohn Baldwin return (EINVAL); 703df8bae1dSRodney W. Grimes loop: 704902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 705902c0d82SDavid Xu PROC_LOCK(q); 706902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 707902c0d82SDavid Xu PROC_UNLOCK(q); 708902c0d82SDavid Xu } 709df8bae1dSRodney W. Grimes nfound = 0; 710d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 7112e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 712f591779bSSeigo Tanimura PROC_LOCK(p); 713b7e23e82SJohn Baldwin if (pid != WAIT_ANY && 714b7e23e82SJohn Baldwin p->p_pid != pid && p->p_pgid != -pid) { 715f591779bSSeigo Tanimura PROC_UNLOCK(p); 716df8bae1dSRodney W. Grimes continue; 717f591779bSSeigo Tanimura } 718babe9a2bSRobert Watson if (p_canwait(td, p)) { 719babe9a2bSRobert Watson PROC_UNLOCK(p); 720babe9a2bSRobert Watson continue; 721babe9a2bSRobert Watson } 7224ac9ae70SJulian Elischer 7231156bc4dSJake Burkholder /* 7241156bc4dSJake Burkholder * This special case handles a kthread spawned by linux_clone 7251156bc4dSJake Burkholder * (see linux_misc.c). The linux_wait4 and linux_waitpid 7261156bc4dSJake Burkholder * functions need to be able to distinguish between waiting 7271156bc4dSJake Burkholder * on a process and waiting on a thread. It is a thread if 7281156bc4dSJake Burkholder * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 7291156bc4dSJake Burkholder * signifies we want to wait for threads and not processes. 7304ac9ae70SJulian Elischer */ 7311156bc4dSJake Burkholder if ((p->p_sigparent != SIGCHLD) ^ 732b7e23e82SJohn Baldwin ((options & WLINUXCLONE) != 0)) { 733a914fb6bSJohn Baldwin PROC_UNLOCK(p); 7344ac9ae70SJulian Elischer continue; 735a914fb6bSJohn Baldwin } 7364ac9ae70SJulian Elischer 737df8bae1dSRodney W. Grimes nfound++; 738982d11f8SJeff Roberson PROC_SLOCK(p); 739a140976eSAttilio Rao if (p->p_state == PRS_ZOMBIE) { 74078c85e8dSJohn Baldwin if (rusage) { 741a140976eSAttilio Rao *rusage = p->p_ru; 74278c85e8dSJohn Baldwin calcru(p, &rusage->ru_utime, &rusage->ru_stime); 74378c85e8dSJohn Baldwin } 744a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 745a1fe14bcSAttilio Rao td->td_retval[0] = p->p_pid; 746a1fe14bcSAttilio Rao if (status) 747a1fe14bcSAttilio Rao *status = p->p_xstat; /* convert to int */ 748cbc15844SKonstantin Belousov if (options & WNOWAIT) { 749cbc15844SKonstantin Belousov 750cbc15844SKonstantin Belousov /* 751cbc15844SKonstantin Belousov * Only poll, returning the status. 752cbc15844SKonstantin Belousov * Caller does not wish to release the proc 753cbc15844SKonstantin Belousov * struct just yet. 754cbc15844SKonstantin Belousov */ 7553eb8b8bbSDavid Xu PROC_UNLOCK(p); 756cbc15844SKonstantin Belousov sx_xunlock(&proctree_lock); 757cbc15844SKonstantin Belousov return (0); 758cbc15844SKonstantin Belousov } 759cbc15844SKonstantin Belousov 7603eb8b8bbSDavid Xu PROC_LOCK(q); 7613eb8b8bbSDavid Xu sigqueue_take(p->p_ksi); 7623eb8b8bbSDavid Xu PROC_UNLOCK(q); 7633eb8b8bbSDavid Xu PROC_UNLOCK(p); 7643eb8b8bbSDavid Xu 765df8bae1dSRodney W. Grimes /* 766df8bae1dSRodney W. Grimes * If we got the child via a ptrace 'attach', 767df8bae1dSRodney W. Grimes * we need to give it back to the old parent. 768df8bae1dSRodney W. Grimes */ 769d7aadbf9SJohn Baldwin if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 770c65437a3SJohn Baldwin PROC_LOCK(p); 771df8bae1dSRodney W. Grimes p->p_oppid = 0; 772df8bae1dSRodney W. Grimes proc_reparent(p, t); 773c65437a3SJohn Baldwin PROC_UNLOCK(p); 774ebceaf6dSDavid Xu tdsignal(t, NULL, SIGCHLD, p->p_ksi); 7757f05b035SAlfred Perlstein wakeup(t); 776aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 777c65437a3SJohn Baldwin PROC_UNLOCK(t); 7781005a129SJohn Baldwin sx_xunlock(&proctree_lock); 779d7aadbf9SJohn Baldwin return (0); 780df8bae1dSRodney W. Grimes } 781ebdc3f1dSSeigo Tanimura 7823890793eSTim J. Robbins /* 7833890793eSTim J. Robbins * Remove other references to this process to ensure 7843890793eSTim J. Robbins * we have an exclusive reference. 7853890793eSTim J. Robbins */ 7866ec62361STim J. Robbins sx_xlock(&allproc_lock); 7876ec62361STim J. Robbins LIST_REMOVE(p, p_list); /* off zombproc */ 7886ec62361STim J. Robbins sx_xunlock(&allproc_lock); 7896ec62361STim J. Robbins LIST_REMOVE(p, p_sibling); 7903890793eSTim J. Robbins leavepgrp(p); 791ebdc3f1dSSeigo Tanimura sx_xunlock(&proctree_lock); 792ebdc3f1dSSeigo Tanimura 793ebdc3f1dSSeigo Tanimura /* 794d7aadbf9SJohn Baldwin * As a side effect of this lock, we know that 795d7aadbf9SJohn Baldwin * all other writes to this proc are visible now, so 796d7aadbf9SJohn Baldwin * no more locking is needed for p. 797d7aadbf9SJohn Baldwin */ 798d7aadbf9SJohn Baldwin PROC_LOCK(p); 799d7aadbf9SJohn Baldwin p->p_xstat = 0; /* XXX: why? */ 800d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 801d7aadbf9SJohn Baldwin PROC_LOCK(q); 802a140976eSAttilio Rao ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, 80378c85e8dSJohn Baldwin &p->p_rux); 804d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 805d7aadbf9SJohn Baldwin 806d7aadbf9SJohn Baldwin /* 807d7aadbf9SJohn Baldwin * Decrement the count of procs running with this uid. 808d7aadbf9SJohn Baldwin */ 809d7aadbf9SJohn Baldwin (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 810d7aadbf9SJohn Baldwin 811d7aadbf9SJohn Baldwin /* 812a9a64385SJohn Baldwin * Free credentials, arguments, and sigacts. 813df8bae1dSRodney W. Grimes */ 814da654d90SPoul-Henning Kamp crfree(p->p_ucred); 81590af4afaSJohn Baldwin p->p_ucred = NULL; 816d7aadbf9SJohn Baldwin pargs_drop(p->p_args); 817d7aadbf9SJohn Baldwin p->p_args = NULL; 81890af4afaSJohn Baldwin sigacts_free(p->p_sigacts); 81990af4afaSJohn Baldwin p->p_sigacts = NULL; 82088c5ea45SJulian Elischer 821df8bae1dSRodney W. Grimes /* 822a9a64385SJohn Baldwin * Do any thread-system specific cleanups. 823e602ba25SJulian Elischer */ 824696058c3SJulian Elischer thread_wait(p); 825e602ba25SJulian Elischer 826e602ba25SJulian Elischer /* 827eb30c1c0SPeter Wemm * Give vm and machine-dependent layer a chance 828df8bae1dSRodney W. Grimes * to free anything that cpu_exit couldn't 829df8bae1dSRodney W. Grimes * release while still running in process context. 830df8bae1dSRodney W. Grimes */ 831eb30c1c0SPeter Wemm vm_waitproc(p); 8322555374cSRobert Watson #ifdef MAC 83330d239bcSRobert Watson mac_proc_destroy(p); 8342555374cSRobert Watson #endif 8351faf202eSJulian Elischer KASSERT(FIRST_THREAD_IN_PROC(p), 836b7e23e82SJohn Baldwin ("kern_wait: no residual thread!")); 837c897b813SJeff Roberson uma_zfree(proc_zone, p); 838d7aadbf9SJohn Baldwin sx_xlock(&allproc_lock); 839df8bae1dSRodney W. Grimes nprocs--; 840d7aadbf9SJohn Baldwin sx_xunlock(&allproc_lock); 841d7aadbf9SJohn Baldwin return (0); 842df8bae1dSRodney W. Grimes } 8435a2f73e6SDavid Xu if ((p->p_flag & P_STOPPED_SIG) && 8445a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 845a9a64385SJohn Baldwin (p->p_flag & P_WAITED) == 0 && 846b7e23e82SJohn Baldwin (p->p_flag & P_TRACED || options & WUNTRACED)) { 847982d11f8SJeff Roberson PROC_SUNLOCK(p); 848df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 849d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 850b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 851b7e23e82SJohn Baldwin if (status) 852b7e23e82SJohn Baldwin *status = W_STOPCODE(p->p_xstat); 853ebceaf6dSDavid Xu 854ebceaf6dSDavid Xu PROC_LOCK(q); 855ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 856ebceaf6dSDavid Xu PROC_UNLOCK(q); 857e94cc4acSDavid Xu PROC_UNLOCK(p); 858ebceaf6dSDavid Xu 859b7e23e82SJohn Baldwin return (0); 860df8bae1dSRodney W. Grimes } 861982d11f8SJeff Roberson PROC_SUNLOCK(p); 862b7e23e82SJohn Baldwin if (options & WCONTINUED && (p->p_flag & P_CONTINUED)) { 8636ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 8646ee093fbSMike Barcroft td->td_retval[0] = p->p_pid; 8656ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 8666ee093fbSMike Barcroft 867ebceaf6dSDavid Xu PROC_LOCK(q); 868ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 869ebceaf6dSDavid Xu PROC_UNLOCK(q); 870e94cc4acSDavid Xu PROC_UNLOCK(p); 871ebceaf6dSDavid Xu 872b7e23e82SJohn Baldwin if (status) 873b7e23e82SJohn Baldwin *status = SIGCONT; 874b7e23e82SJohn Baldwin return (0); 8756ee093fbSMike Barcroft } 876d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 877d7aadbf9SJohn Baldwin } 878d7aadbf9SJohn Baldwin if (nfound == 0) { 879d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 880d7aadbf9SJohn Baldwin return (ECHILD); 881d7aadbf9SJohn Baldwin } 882b7e23e82SJohn Baldwin if (options & WNOHANG) { 883d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 884d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 885d7aadbf9SJohn Baldwin return (0); 886d7aadbf9SJohn Baldwin } 887d7aadbf9SJohn Baldwin PROC_LOCK(q); 888d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 88995992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 89095992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 89195992d56SDavid Xu error = 0; 89295992d56SDavid Xu } else 8937f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 894d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 8954ae89b95SJohn Baldwin if (error) 896d7aadbf9SJohn Baldwin return (error); 897d7aadbf9SJohn Baldwin goto loop; 898d7aadbf9SJohn Baldwin } 899df8bae1dSRodney W. Grimes 900df8bae1dSRodney W. Grimes /* 90198f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 90298f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 903df8bae1dSRodney W. Grimes */ 904df8bae1dSRodney W. Grimes void 905830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 906df8bae1dSRodney W. Grimes { 907df8bae1dSRodney W. Grimes 9084e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 909c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 910df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 911df8bae1dSRodney W. Grimes return; 912df8bae1dSRodney W. Grimes 913ff766807SDavid Xu PROC_LOCK(child->p_pptr); 914ff766807SDavid Xu sigqueue_take(child->p_ksi); 915ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 916b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 917b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 918df8bae1dSRodney W. Grimes child->p_pptr = parent; 919df8bae1dSRodney W. Grimes } 920