19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_exit.c 8.7 (Berkeley) 2/12/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 405591b823SEivind Eklund #include "opt_compat.h" 415d217f17SJohn Birrell #include "opt_kdtrace.h" 42db6a20e2SGarrett Wollman #include "opt_ktrace.h" 43cfb5f768SJonathan Anderson #include "opt_procdesc.h" 44db6a20e2SGarrett Wollman 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/systm.h> 475fdb8324SBruce Evans #include <sys/sysproto.h> 48cfb5f768SJonathan Anderson #include <sys/capability.h> 4975b8b3b2SJohn Baldwin #include <sys/eventhandler.h> 501c5bb3eaSPeter Wemm #include <sys/kernel.h> 51a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 52f34fa851SJohn Baldwin #include <sys/lock.h> 5335e0e5b3SJohn Baldwin #include <sys/mutex.h> 54df8bae1dSRodney W. Grimes #include <sys/proc.h> 55cfb5f768SJonathan Anderson #include <sys/procdesc.h> 562a024a2bSSean Eric Fagan #include <sys/pioctl.h> 57413628a7SBjoern A. Zeeb #include <sys/jail.h> 58df8bae1dSRodney W. Grimes #include <sys/tty.h> 59df8bae1dSRodney W. Grimes #include <sys/wait.h> 60eb30c1c0SPeter Wemm #include <sys/vmmeter.h> 617a6b989bSJohn Baldwin #include <sys/vnode.h> 62097055e2SEdward Tomasz Napierala #include <sys/racct.h> 63df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 6425f6e35aSPoul-Henning Kamp #include <sys/sbuf.h> 65797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 66b43179fbSJeff Roberson #include <sys/sched.h> 671005a129SJohn Baldwin #include <sys/sx.h> 68c8837938SJohn Baldwin #include <sys/syscallsubr.h> 6925f6e35aSPoul-Henning Kamp #include <sys/syslog.h> 70df8bae1dSRodney W. Grimes #include <sys/ptrace.h> 717c409b8aSJeffrey Hsu #include <sys/acct.h> /* for acct_process() function prototype */ 72797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 735d217f17SJohn Birrell #include <sys/sdt.h> 74780dc5a8SPeter Wemm #include <sys/shm.h> 75780dc5a8SPeter Wemm #include <sys/sem.h> 766c84de02SJohn Baldwin #ifdef KTRACE 776c84de02SJohn Baldwin #include <sys/ktrace.h> 786c84de02SJohn Baldwin #endif 79780dc5a8SPeter Wemm 80fcf7f27aSRobert Watson #include <security/audit/audit.h> 81aed55708SRobert Watson #include <security/mac/mac_framework.h> 82fcf7f27aSRobert Watson 83df8bae1dSRodney W. Grimes #include <vm/vm.h> 84eb30c1c0SPeter Wemm #include <vm/vm_extern.h> 857a6b989bSJohn Baldwin #include <vm/vm_param.h> 86efeaf95aSDavid Greenman #include <vm/pmap.h> 87efeaf95aSDavid Greenman #include <vm/vm_map.h> 882d21129dSAlan Cox #include <vm/vm_page.h> 89c897b813SJeff Roberson #include <vm/uma.h> 90df8bae1dSRodney W. Grimes 915d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 925d217f17SJohn Birrell #include <sys/dtrace_bsd.h> 935d217f17SJohn Birrell dtrace_execexit_func_t dtrace_fasttrap_exit; 945d217f17SJohn Birrell #endif 955d217f17SJohn Birrell 965d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc); 977b77e1feSMark Johnston SDT_PROBE_DEFINE1(proc, kernel, , exit, exit, "int"); 985d217f17SJohn Birrell 99c0bc2867SGleb Smirnoff /* Hook for NFS teardown procedure. */ 100c0bc2867SGleb Smirnoff void (*nlminfo_release_p)(struct proc *p); 101c0bc2867SGleb Smirnoff 1022e39e24fSKonstantin Belousov static void 1032e39e24fSKonstantin Belousov clear_orphan(struct proc *p) 1042e39e24fSKonstantin Belousov { 1052e39e24fSKonstantin Belousov 1062e39e24fSKonstantin Belousov PROC_LOCK_ASSERT(p, MA_OWNED); 1072e39e24fSKonstantin Belousov 1082e39e24fSKonstantin Belousov if (p->p_flag & P_ORPHAN) { 1092e39e24fSKonstantin Belousov LIST_REMOVE(p, p_orphan); 1102e39e24fSKonstantin Belousov p->p_flag &= ~P_ORPHAN; 1112e39e24fSKonstantin Belousov } 1122e39e24fSKonstantin Belousov } 1132e39e24fSKonstantin Belousov 114df8bae1dSRodney W. Grimes /* 115873fbcd7SRobert Watson * exit -- death of process. 116df8bae1dSRodney W. Grimes */ 117fc0b1dbfSBruce Evans void 1188451d0ddSKip Macy sys_sys_exit(struct thread *td, struct sys_exit_args *uap) 119df8bae1dSRodney W. Grimes { 120b40ce416SJulian Elischer 121b40ce416SJulian Elischer exit1(td, W_EXITCODE(uap->rval, 0)); 122df8bae1dSRodney W. Grimes /* NOTREACHED */ 123df8bae1dSRodney W. Grimes } 124df8bae1dSRodney W. Grimes 125df8bae1dSRodney W. Grimes /* 126873fbcd7SRobert Watson * Exit: deallocate address space and other resources, change proc state to 127873fbcd7SRobert Watson * zombie, and unlink proc from allproc and parent's lists. Save exit status 128873fbcd7SRobert Watson * and rusage for wait(). Check for child processes and orphan them. 129df8bae1dSRodney W. Grimes */ 130fc0b1dbfSBruce Evans void 131830c3153SDag-Erling Smørgrav exit1(struct thread *td, int rv) 132df8bae1dSRodney W. Grimes { 133276c5169SJohn Baldwin struct proc *p, *nq, *q; 13479deba82SMatthew Dillon struct vnode *vtmp; 135bc093719SEd Schouten struct vnode *ttyvp = NULL; 13691d5354aSJohn Baldwin struct plimit *plim; 137df8bae1dSRodney W. Grimes 138e17660e7SKris Kennaway mtx_assert(&Giant, MA_NOTOWNED); 1390cddd8f0SMatthew Dillon 140276c5169SJohn Baldwin p = td->td_proc; 1415486ffc8SMarius Strobl /* 1425486ffc8SMarius Strobl * XXX in case we're rebooting we just let init die in order to 1435486ffc8SMarius Strobl * work around an unsolved stack overflow seen very late during 1445486ffc8SMarius Strobl * shutdown on sparc64 when the gmirror worker process exists. 1455486ffc8SMarius Strobl */ 1465486ffc8SMarius Strobl if (p == initproc && rebooting == 0) { 1475f7bd355SPoul-Henning Kamp printf("init died (signal %d, exit %d)\n", 148df8bae1dSRodney W. Grimes WTERMSIG(rv), WEXITSTATUS(rv)); 1495f7bd355SPoul-Henning Kamp panic("Going nowhere without my init!"); 1505f7bd355SPoul-Henning Kamp } 1512c1011f7SJohn Dyson 1527a6b989bSJohn Baldwin /* 1532c10d16aSJeff Roberson * MUST abort all other threads before proceeding past here. 1547a6b989bSJohn Baldwin */ 155e602ba25SJulian Elischer PROC_LOCK(p); 1567ab24ea3SJulian Elischer while (p->p_flag & P_HADTHREADS) { 157e602ba25SJulian Elischer /* 158773e3b7dSJohn Baldwin * First check if some other thread got here before us. 159773e3b7dSJohn Baldwin * If so, act appropriately: exit or suspend. 160e602ba25SJulian Elischer */ 161e602ba25SJulian Elischer thread_suspend_check(0); 162e602ba25SJulian Elischer 163e602ba25SJulian Elischer /* 164e602ba25SJulian Elischer * Kill off the other threads. This requires 1656111dcd2SJohn Baldwin * some co-operation from other parts of the kernel 1666111dcd2SJohn Baldwin * so it may not be instantaneous. With this state set 1676111dcd2SJohn Baldwin * any thread entering the kernel from userspace will 168e602ba25SJulian Elischer * thread_exit() in trap(). Any thread attempting to 1696111dcd2SJohn Baldwin * sleep will return immediately with EINTR or EWOULDBLOCK 1706111dcd2SJohn Baldwin * which will hopefully force them to back out to userland 1716111dcd2SJohn Baldwin * freeing resources as they go. Any thread attempting 172b3248998SJulian Elischer * to return to userland will thread_exit() from userret(). 1736111dcd2SJohn Baldwin * thread_exit() will unsuspend us when the last of the 1746111dcd2SJohn Baldwin * other threads exits. 175b370279eSDavid Xu * If there is already a thread singler after resumption, 1766111dcd2SJohn Baldwin * calling thread_single will fail; in that case, we just 177b370279eSDavid Xu * re-check all suspension request, the thread should 178b370279eSDavid Xu * either be suspended there or exit. 179e602ba25SJulian Elischer */ 1807ab24ea3SJulian Elischer if (!thread_single(SINGLE_EXIT)) 1817ab24ea3SJulian Elischer break; 1826111dcd2SJohn Baldwin 183e602ba25SJulian Elischer /* 184e602ba25SJulian Elischer * All other activity in this process is now stopped. 185ed062c8dSJulian Elischer * Threading support has been turned off. 186e602ba25SJulian Elischer */ 187e602ba25SJulian Elischer } 1881c4bcd05SJeff Roberson KASSERT(p->p_numthreads == 1, 1891c4bcd05SJeff Roberson ("exit1: proc %p exiting with %d threads", p, p->p_numthreads)); 19058c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1); 19106ad42b2SJohn Baldwin /* 19206ad42b2SJohn Baldwin * Wakeup anyone in procfs' PIOCWAIT. They should have a hold 19306ad42b2SJohn Baldwin * on our vmspace, so we should block below until they have 19406ad42b2SJohn Baldwin * released their reference to us. Note that if they have 19506ad42b2SJohn Baldwin * requested S_EXIT stops we will block here until they ack 19606ad42b2SJohn Baldwin * via PIOCCONT. 19706ad42b2SJohn Baldwin */ 19806ad42b2SJohn Baldwin _STOPEVENT(p, S_EXIT, rv); 19906ad42b2SJohn Baldwin 20006ad42b2SJohn Baldwin /* 2010f14f15bSJohn Baldwin * Ignore any pending request to stop due to a stop signal. 2020f14f15bSJohn Baldwin * Once P_WEXIT is set, future requests will be ignored as 2030f14f15bSJohn Baldwin * well. 2040f14f15bSJohn Baldwin */ 2050f14f15bSJohn Baldwin p->p_flag &= ~P_STOPPED_SIG; 2060f14f15bSJohn Baldwin KASSERT(!P_SHOULDSTOP(p), ("exiting process is stopped")); 2070f14f15bSJohn Baldwin 2080f14f15bSJohn Baldwin /* 20906ad42b2SJohn Baldwin * Note that we are exiting and do another wakeup of anyone in 21006ad42b2SJohn Baldwin * PIOCWAIT in case they aren't listening for S_EXIT stops or 21106ad42b2SJohn Baldwin * decided to wait again after we told them we are exiting. 21206ad42b2SJohn Baldwin */ 213e602ba25SJulian Elischer p->p_flag |= P_WEXIT; 21406ad42b2SJohn Baldwin wakeup(&p->p_stype); 21506ad42b2SJohn Baldwin 21606ad42b2SJohn Baldwin /* 21706ad42b2SJohn Baldwin * Wait for any processes that have a hold on our vmspace to 21806ad42b2SJohn Baldwin * release their reference. 21906ad42b2SJohn Baldwin */ 22006ad42b2SJohn Baldwin while (p->p_lock > 0) 22106ad42b2SJohn Baldwin msleep(&p->p_lock, &p->p_mtx, PWAIT, "exithold", 0); 222ebceaf6dSDavid Xu 223bb63fddeSAlexander Leidinger p->p_xstat = rv; /* Let event handler change exit status */ 224e602ba25SJulian Elischer PROC_UNLOCK(p); 2251c4bcd05SJeff Roberson /* Drain the limit callout while we don't have the proc locked */ 2261c4bcd05SJeff Roberson callout_drain(&p->p_limco); 227b40ce416SJulian Elischer 22800c28d96SRobert Watson #ifdef AUDIT 22900c28d96SRobert Watson /* 23000c28d96SRobert Watson * The Sun BSM exit token contains two components: an exit status as 23100c28d96SRobert Watson * passed to exit(), and a return value to indicate what sort of exit 23200c28d96SRobert Watson * it was. The exit status is WEXITSTATUS(rv), but it's not clear 23300c28d96SRobert Watson * what the return value is. 23400c28d96SRobert Watson */ 23514961ba7SRobert Watson AUDIT_ARG_EXIT(WEXITSTATUS(rv), 0); 23600c28d96SRobert Watson AUDIT_SYSCALL_EXIT(0, td); 23700c28d96SRobert Watson #endif 23800c28d96SRobert Watson 2397a6b989bSJohn Baldwin /* Are we a task leader? */ 2402c1011f7SJohn Dyson if (p == p->p_leader) { 241c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 2422c1011f7SJohn Dyson q = p->p_peers; 243776e0b36SJohn Baldwin while (q != NULL) { 244776e0b36SJohn Baldwin PROC_LOCK(q); 2458451d0ddSKip Macy kern_psignal(q, SIGKILL); 246776e0b36SJohn Baldwin PROC_UNLOCK(q); 2472c1011f7SJohn Dyson q = q->p_peers; 2482c1011f7SJohn Dyson } 249c6544064SJohn Baldwin while (p->p_peers != NULL) 250c6544064SJohn Baldwin msleep(p, &ppeers_lock, PWAIT, "exit1", 0); 251c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 2522c1011f7SJohn Dyson } 2532c1011f7SJohn Dyson 254fed06968SJulian Elischer /* 255e9189611SPeter Wemm * Check if any loadable modules need anything done at process exit. 2566111dcd2SJohn Baldwin * E.g. SYSV IPC stuff 257fed06968SJulian Elischer * XXX what if one of these generates an error? 258fed06968SJulian Elischer */ 25975b8b3b2SJohn Baldwin EVENTHANDLER_INVOKE(process_exit, p); 260a914fb6bSJohn Baldwin 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * If parent is waiting for us to exit or exec, 263df8bae1dSRodney W. Grimes * P_PPWAIT is set; we will wakeup the parent below. 264df8bae1dSRodney W. Grimes */ 265a914fb6bSJohn Baldwin PROC_LOCK(p); 266bb63fddeSAlexander Leidinger rv = p->p_xstat; /* Event handler could change exit status */ 267a282253aSJulian Elischer stopprofclock(p); 268888d4d4fSKonstantin Belousov p->p_flag &= ~(P_TRACED | P_PPWAIT | P_PPTRACE); 2695499ea01SJohn Baldwin 2705499ea01SJohn Baldwin /* 2715499ea01SJohn Baldwin * Stop the real interval timer. If the handler is currently 2725499ea01SJohn Baldwin * executing, prevent it from rearming itself and let it finish. 2735499ea01SJohn Baldwin */ 2745499ea01SJohn Baldwin if (timevalisset(&p->p_realtimer.it_value) && 2755499ea01SJohn Baldwin callout_stop(&p->p_itcallout) == 0) { 2765499ea01SJohn Baldwin timevalclear(&p->p_realtimer.it_interval); 2775499ea01SJohn Baldwin msleep(&p->p_itcallout, &p->p_mtx, PWAIT, "ritwait", 0); 2785499ea01SJohn Baldwin KASSERT(!timevalisset(&p->p_realtimer.it_value), 2795499ea01SJohn Baldwin ("realtime timer is still armed")); 2805499ea01SJohn Baldwin } 28196d7f8efSTim J. Robbins PROC_UNLOCK(p); 282df8bae1dSRodney W. Grimes 283df8bae1dSRodney W. Grimes /* 284831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 285831d27a9SDon Lewis * F_SETOWN with our pid. 286831d27a9SDon Lewis */ 287831d27a9SDon Lewis funsetownlst(&p->p_sigiolst); 288831d27a9SDon Lewis 289831d27a9SDon Lewis /* 290c0bc2867SGleb Smirnoff * If this process has an nlminfo data area (for lockd), release it 291c0bc2867SGleb Smirnoff */ 292c0bc2867SGleb Smirnoff if (nlminfo_release_p != NULL && p->p_nlminfo != NULL) 293c0bc2867SGleb Smirnoff (*nlminfo_release_p)(p); 294c0bc2867SGleb Smirnoff 295c0bc2867SGleb Smirnoff /* 296df8bae1dSRodney W. Grimes * Close open files and release open-file table. 297df8bae1dSRodney W. Grimes * This may block! 298df8bae1dSRodney W. Grimes */ 2992609222aSPawel Jakub Dawidek fdescfree(td); 300df8bae1dSRodney W. Grimes 301a914fb6bSJohn Baldwin /* 302a2587073SPoul-Henning Kamp * If this thread tickled GEOM, we need to wait for the giggling to 303a2587073SPoul-Henning Kamp * stop before we return to userland 304a2587073SPoul-Henning Kamp */ 305a2587073SPoul-Henning Kamp if (td->td_pflags & TDP_GEOM) 306a2587073SPoul-Henning Kamp g_waitidle(); 307a2587073SPoul-Henning Kamp 308a2587073SPoul-Henning Kamp /* 309a914fb6bSJohn Baldwin * Remove ourself from our leader's peer list and wake our leader. 310a914fb6bSJohn Baldwin */ 311c6544064SJohn Baldwin mtx_lock(&ppeers_lock); 31279fc0bf4SMike Smith if (p->p_leader->p_peers) { 31379fc0bf4SMike Smith q = p->p_leader; 31479fc0bf4SMike Smith while (q->p_peers != p) 31579fc0bf4SMike Smith q = q->p_peers; 31679fc0bf4SMike Smith q->p_peers = p->p_peers; 3177f05b035SAlfred Perlstein wakeup(p->p_leader); 31879fc0bf4SMike Smith } 319c6544064SJohn Baldwin mtx_unlock(&ppeers_lock); 32079fc0bf4SMike Smith 32157051fdcSTor Egge vmspace_exit(td); 322df8bae1dSRodney W. Grimes 323ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 324df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 325eaaaf190SEd Schouten struct session *sp = p->p_session; 326eaaaf190SEd Schouten struct tty *tp; 327df8bae1dSRodney W. Grimes 328eaaaf190SEd Schouten /* 329eaaaf190SEd Schouten * s_ttyp is not zero'd; we use this to indicate that 330eaaaf190SEd Schouten * the session once had a controlling terminal. (for 331eaaaf190SEd Schouten * logging and informational purposes) 332eaaaf190SEd Schouten */ 333bc093719SEd Schouten SESS_LOCK(sp); 334bc093719SEd Schouten ttyvp = sp->s_ttyvp; 335eaaaf190SEd Schouten tp = sp->s_ttyp; 336bc093719SEd Schouten sp->s_ttyvp = NULL; 3378dc9b4cfSEd Schouten sp->s_ttydp = NULL; 338eaaaf190SEd Schouten sp->s_leader = NULL; 339bc093719SEd Schouten SESS_UNLOCK(sp); 340bc093719SEd Schouten 341df8bae1dSRodney W. Grimes /* 342bc093719SEd Schouten * Signal foreground pgrp and revoke access to 343eaaaf190SEd Schouten * controlling terminal if it has not been revoked 344eaaaf190SEd Schouten * already. 345bc093719SEd Schouten * 346eaaaf190SEd Schouten * Because the TTY may have been revoked in the mean 347eaaaf190SEd Schouten * time and could already have a new session associated 348eaaaf190SEd Schouten * with it, make sure we don't send a SIGHUP to a 349eaaaf190SEd Schouten * foreground process group that does not belong to this 350eaaaf190SEd Schouten * session. 351df8bae1dSRodney W. Grimes */ 352bc093719SEd Schouten 353eaaaf190SEd Schouten if (tp != NULL) { 354bc093719SEd Schouten tty_lock(tp); 355eaaaf190SEd Schouten if (tp->t_session == sp) 356bc093719SEd Schouten tty_signal_pgrp(tp, SIGHUP); 357bc093719SEd Schouten tty_unlock(tp); 358eaaaf190SEd Schouten } 359bc093719SEd Schouten 3607c6d401cSKonstantin Belousov if (ttyvp != NULL) { 361ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 3626728dc16SKonstantin Belousov if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) { 363f591779bSSeigo Tanimura VOP_REVOKE(ttyvp, REVOKEALL); 364bc093719SEd Schouten VOP_UNLOCK(ttyvp, 0); 3656728dc16SKonstantin Belousov } 366ea97757aSJohn Baldwin sx_xlock(&proctree_lock); 367f591779bSSeigo Tanimura } 368f591779bSSeigo Tanimura } 369df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 370ea97757aSJohn Baldwin sx_xunlock(&proctree_lock); 371b40ce416SJulian Elischer (void)acct_process(td); 372bc093719SEd Schouten 373bc093719SEd Schouten /* Release the TTY now we've unlocked everything. */ 374bc093719SEd Schouten if (ttyvp != NULL) 375bc093719SEd Schouten vrele(ttyvp); 376df8bae1dSRodney W. Grimes #ifdef KTRACE 3772c255e9dSRobert Watson ktrprocexit(td); 378df8bae1dSRodney W. Grimes #endif 379df8bae1dSRodney W. Grimes /* 380ee42d0a9SDavid Malone * Release reference to text vnode 381ee42d0a9SDavid Malone */ 382ee42d0a9SDavid Malone if ((vtmp = p->p_textvp) != NULL) { 383ee42d0a9SDavid Malone p->p_textvp = NULL; 384ee42d0a9SDavid Malone vrele(vtmp); 385ee42d0a9SDavid Malone } 386ee42d0a9SDavid Malone 387ee42d0a9SDavid Malone /* 388d7aadbf9SJohn Baldwin * Release our limits structure. 389d7aadbf9SJohn Baldwin */ 39091d5354aSJohn Baldwin PROC_LOCK(p); 39191d5354aSJohn Baldwin plim = p->p_limit; 392d7aadbf9SJohn Baldwin p->p_limit = NULL; 39391d5354aSJohn Baldwin PROC_UNLOCK(p); 39491d5354aSJohn Baldwin lim_free(plim); 395d7aadbf9SJohn Baldwin 396cf7d9a8cSDavid Xu tidhash_remove(td); 397cf7d9a8cSDavid Xu 398d7aadbf9SJohn Baldwin /* 399df8bae1dSRodney W. Grimes * Remove proc from allproc queue and pidhash chain. 400df8bae1dSRodney W. Grimes * Place onto zombproc. Unlink from parent's child list. 401df8bae1dSRodney W. Grimes */ 4021005a129SJohn Baldwin sx_xlock(&allproc_lock); 403b75356e1SJeffrey Hsu LIST_REMOVE(p, p_list); 404b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&zombproc, p, p_list); 405b75356e1SJeffrey Hsu LIST_REMOVE(p, p_hash); 4061005a129SJohn Baldwin sx_xunlock(&allproc_lock); 407df8bae1dSRodney W. Grimes 40837f84a60SJohn Baldwin /* 4091bba2a94SJohn Baldwin * Call machine-dependent code to release any 4101bba2a94SJohn Baldwin * machine-dependent resources other than the address space. 4111bba2a94SJohn Baldwin * The address space is released by "vmspace_exitfree(p)" in 4121bba2a94SJohn Baldwin * vm_waitproc(). 4131bba2a94SJohn Baldwin */ 4141bba2a94SJohn Baldwin cpu_exit(td); 4151bba2a94SJohn Baldwin 4161bba2a94SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "process (pid %d) exiting", p->p_pid); 4171bba2a94SJohn Baldwin 4181bba2a94SJohn Baldwin /* 41937f84a60SJohn Baldwin * Reparent all of our children to init. 42037f84a60SJohn Baldwin */ 4211005a129SJohn Baldwin sx_xlock(&proctree_lock); 4222e3c8fcbSPoul-Henning Kamp q = LIST_FIRST(&p->p_children); 423a914fb6bSJohn Baldwin if (q != NULL) /* only need this if any child is S_ZOMB */ 4247f05b035SAlfred Perlstein wakeup(initproc); 425a914fb6bSJohn Baldwin for (; q != NULL; q = nq) { 4262e3c8fcbSPoul-Henning Kamp nq = LIST_NEXT(q, p_sibling); 427a914fb6bSJohn Baldwin PROC_LOCK(q); 428c65437a3SJohn Baldwin proc_reparent(q, initproc); 4294ac9ae70SJulian Elischer q->p_sigparent = SIGCHLD; 430df8bae1dSRodney W. Grimes /* 431df8bae1dSRodney W. Grimes * Traced processes are killed 432df8bae1dSRodney W. Grimes * since their existence means someone is screwing up. 433df8bae1dSRodney W. Grimes */ 434df8bae1dSRodney W. Grimes if (q->p_flag & P_TRACED) { 435904c5ec4SDavid Xu struct thread *temp; 436904c5ec4SDavid Xu 4375085ecb7SKonstantin Belousov /* 4385085ecb7SKonstantin Belousov * Since q was found on our children list, the 4395085ecb7SKonstantin Belousov * proc_reparent() call moved q to the orphan 4405085ecb7SKonstantin Belousov * list due to present P_TRACED flag. Clear 4415085ecb7SKonstantin Belousov * orphan link for q now while q is locked. 4425085ecb7SKonstantin Belousov */ 4435085ecb7SKonstantin Belousov clear_orphan(q); 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; 4478451d0ddSKip Macy kern_psignal(q, SIGKILL); 448c65437a3SJohn Baldwin } 449a914fb6bSJohn Baldwin PROC_UNLOCK(q); 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes 4525085ecb7SKonstantin Belousov /* 4535085ecb7SKonstantin Belousov * Also get rid of our orphans. 4545085ecb7SKonstantin Belousov */ 4555085ecb7SKonstantin Belousov while ((q = LIST_FIRST(&p->p_orphans)) != NULL) { 4565085ecb7SKonstantin Belousov PROC_LOCK(q); 4575085ecb7SKonstantin Belousov clear_orphan(q); 4585085ecb7SKonstantin Belousov PROC_UNLOCK(q); 4595085ecb7SKonstantin Belousov } 4605085ecb7SKonstantin Belousov 4611c4bcd05SJeff Roberson /* Save exit status. */ 462d7aadbf9SJohn Baldwin PROC_LOCK(p); 463cbf4e354SDavid Xu p->p_xthread = td; 4645d217f17SJohn Birrell 4650304c731SJamie Gritton /* Tell the prison that we are gone. */ 466413628a7SBjoern A. Zeeb prison_proc_free(p->p_ucred->cr_prison); 467413628a7SBjoern A. Zeeb 4685d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4695d217f17SJohn Birrell /* 4705d217f17SJohn Birrell * Tell the DTrace fasttrap provider about the exit if it 4715d217f17SJohn Birrell * has declared an interest. 4725d217f17SJohn Birrell */ 4735d217f17SJohn Birrell if (dtrace_fasttrap_exit) 4745d217f17SJohn Birrell dtrace_fasttrap_exit(p); 4755d217f17SJohn Birrell #endif 4765d217f17SJohn Birrell 4771c4bcd05SJeff Roberson /* 4787a6b989bSJohn Baldwin * Notify interested parties of our demise. 479cb679c38SJonathan Lemon */ 480ad3b9257SJohn-Mark Gurney KNOTE_LOCKED(&p->p_klist, NOTE_EXIT); 4817eaec467SJohn Baldwin 4825d217f17SJohn Birrell #ifdef KDTRACE_HOOKS 4835d217f17SJohn Birrell int reason = CLD_EXITED; 4845d217f17SJohn Birrell if (WCOREDUMP(rv)) 4855d217f17SJohn Birrell reason = CLD_DUMPED; 4865d217f17SJohn Birrell else if (WIFSIGNALED(rv)) 4875d217f17SJohn Birrell reason = CLD_KILLED; 4885d217f17SJohn Birrell SDT_PROBE(proc, kernel, , exit, reason, 0, 0, 0, 0); 4895d217f17SJohn Birrell #endif 4905d217f17SJohn Birrell 4911a29c806SOlivier Houchard /* 4921a29c806SOlivier Houchard * Just delete all entries in the p_klist. At this point we won't 4931a29c806SOlivier Houchard * report any more events, and there are nasty race conditions that 4941a29c806SOlivier Houchard * can beat us if we don't. 4951a29c806SOlivier Houchard */ 496ad3b9257SJohn-Mark Gurney knlist_clear(&p->p_klist, 1); 497cb679c38SJonathan Lemon 498cb679c38SJonathan Lemon /* 499cfb5f768SJonathan Anderson * If this is a process with a descriptor, we may not need to deliver 500cfb5f768SJonathan Anderson * a signal to the parent. proctree_lock is held over 501cfb5f768SJonathan Anderson * procdesc_exit() to serialize concurrent calls to close() and 502cfb5f768SJonathan Anderson * exit(). 503cfb5f768SJonathan Anderson */ 504cfb5f768SJonathan Anderson #ifdef PROCDESC 505cfb5f768SJonathan Anderson if (p->p_procdesc == NULL || procdesc_exit(p)) { 506cfb5f768SJonathan Anderson #endif 507cfb5f768SJonathan Anderson /* 508cfb5f768SJonathan Anderson * Notify parent that we're gone. If parent has the 509cfb5f768SJonathan Anderson * PS_NOCLDWAIT flag set, or if the handler is set to SIG_IGN, 510cfb5f768SJonathan Anderson * notify process 1 instead (and hope it will handle this 511cfb5f768SJonathan Anderson * situation). 512df8bae1dSRodney W. Grimes */ 513d7aadbf9SJohn Baldwin PROC_LOCK(p->p_pptr); 51490af4afaSJohn Baldwin mtx_lock(&p->p_pptr->p_sigacts->ps_mtx); 515cfb5f768SJonathan Anderson if (p->p_pptr->p_sigacts->ps_flag & 516cfb5f768SJonathan Anderson (PS_NOCLDWAIT | PS_CLDSIGIGN)) { 517276c5169SJohn Baldwin struct proc *pp; 518276c5169SJohn Baldwin 51990af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 520276c5169SJohn Baldwin pp = p->p_pptr; 521f591779bSSeigo Tanimura PROC_UNLOCK(pp); 522245f17d4SJoerg Wunsch proc_reparent(p, initproc); 52355b5f2a2SDon Lewis p->p_sigparent = SIGCHLD; 524f591779bSSeigo Tanimura PROC_LOCK(p->p_pptr); 525007abb3dSKonstantin Belousov 526245f17d4SJoerg Wunsch /* 527007abb3dSKonstantin Belousov * Notify parent, so in case he was wait(2)ing or 5286fae832aSKonstantin Belousov * executing waitpid(2) with our pid, he will 529245f17d4SJoerg Wunsch * continue. 530245f17d4SJoerg Wunsch */ 5317f05b035SAlfred Perlstein wakeup(pp); 53290af4afaSJohn Baldwin } else 53390af4afaSJohn Baldwin mtx_unlock(&p->p_pptr->p_sigacts->ps_mtx); 534245f17d4SJoerg Wunsch 5356567eef7SDon Lewis if (p->p_pptr == initproc) 5368451d0ddSKip Macy kern_psignal(p->p_pptr, SIGCHLD); 537ebceaf6dSDavid Xu else if (p->p_sigparent != 0) { 538ebceaf6dSDavid Xu if (p->p_sigparent == SIGCHLD) 539ebceaf6dSDavid Xu childproc_exited(p); 540ebceaf6dSDavid Xu else /* LINUX thread */ 5418451d0ddSKip Macy kern_psignal(p->p_pptr, p->p_sigparent); 542ebceaf6dSDavid Xu } 543cfb5f768SJonathan Anderson #ifdef PROCDESC 544cfb5f768SJonathan Anderson } else 545cfb5f768SJonathan Anderson PROC_LOCK(p->p_pptr); 546cfb5f768SJonathan Anderson #endif 547d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 548696058c3SJulian Elischer 549eb30c1c0SPeter Wemm /* 550f71e748dSDavid Xu * The state PRS_ZOMBIE prevents other proesses from sending 551f71e748dSDavid Xu * signal to the process, to avoid memory leak, we free memory 552f71e748dSDavid Xu * for signal queue at the time when the state is set. 553f71e748dSDavid Xu */ 554f71e748dSDavid Xu sigqueue_flush(&p->p_sigqueue); 555f71e748dSDavid Xu sigqueue_flush(&td->td_sigqueue); 556f71e748dSDavid Xu 557f71e748dSDavid Xu /* 558462f31bfSJohn Baldwin * We have to wait until after acquiring all locks before 559a9a64385SJohn Baldwin * changing p_state. We need to avoid all possible context 560a9a64385SJohn Baldwin * switches (including ones from blocking on a mutex) while 561ddf9c4f7SJohn Baldwin * marked as a zombie. We also have to set the zombie state 562ddf9c4f7SJohn Baldwin * before we release the parent process' proc lock to avoid 563ddf9c4f7SJohn Baldwin * a lost wakeup. So, we first call wakeup, then we grab the 564ddf9c4f7SJohn Baldwin * sched lock, update the state, and release the parent process' 565ddf9c4f7SJohn Baldwin * proc lock. 566eb30c1c0SPeter Wemm */ 567ddf9c4f7SJohn Baldwin wakeup(p->p_pptr); 568aeb32571SKonstantin Belousov cv_broadcast(&p->p_pwait); 569982d11f8SJeff Roberson sched_exit(p->p_pptr, td); 570982d11f8SJeff Roberson PROC_SLOCK(p); 571e602ba25SJulian Elischer p->p_state = PRS_ZOMBIE; 572d7aadbf9SJohn Baldwin PROC_UNLOCK(p->p_pptr); 573871684b8SBruce Evans 574f6f230feSJeff Roberson /* 5757eaec467SJohn Baldwin * Hopefully no one will try to deliver a signal to the process this 576ad3b9257SJohn-Mark Gurney * late in the game. 577ad3b9257SJohn-Mark Gurney */ 578ad3b9257SJohn-Mark Gurney knlist_destroy(&p->p_klist); 579ad3b9257SJohn-Mark Gurney 580ad3b9257SJohn-Mark Gurney /* 581a140976eSAttilio Rao * Save our children's rusage information in our exit rusage. 582a140976eSAttilio Rao */ 583a140976eSAttilio Rao ruadd(&p->p_ru, &p->p_rux, &p->p_stats->p_cru, &p->p_crux); 584a140976eSAttilio Rao 585a140976eSAttilio Rao /* 586696058c3SJulian Elischer * Make sure the scheduler takes this thread out of its tables etc. 587e602ba25SJulian Elischer * This will also release this thread's reference to the ucred. 588696058c3SJulian Elischer * Other thread parts to release include pcb bits and such. 589e602ba25SJulian Elischer */ 590e602ba25SJulian Elischer thread_exit(); 591df8bae1dSRodney W. Grimes } 592df8bae1dSRodney W. Grimes 59325f6e35aSPoul-Henning Kamp 59425f6e35aSPoul-Henning Kamp #ifndef _SYS_SYSPROTO_H_ 59525f6e35aSPoul-Henning Kamp struct abort2_args { 59625f6e35aSPoul-Henning Kamp char *why; 59725f6e35aSPoul-Henning Kamp int nargs; 59825f6e35aSPoul-Henning Kamp void **args; 59925f6e35aSPoul-Henning Kamp }; 60025f6e35aSPoul-Henning Kamp #endif 60125f6e35aSPoul-Henning Kamp 60225f6e35aSPoul-Henning Kamp int 6038451d0ddSKip Macy sys_abort2(struct thread *td, struct abort2_args *uap) 60425f6e35aSPoul-Henning Kamp { 60525f6e35aSPoul-Henning Kamp struct proc *p = td->td_proc; 60625f6e35aSPoul-Henning Kamp struct sbuf *sb; 60725f6e35aSPoul-Henning Kamp void *uargs[16]; 60825f6e35aSPoul-Henning Kamp int error, i, sig; 60925f6e35aSPoul-Henning Kamp 61025f6e35aSPoul-Henning Kamp /* 61125f6e35aSPoul-Henning Kamp * Do it right now so we can log either proper call of abort2(), or 61225f6e35aSPoul-Henning Kamp * note, that invalid argument was passed. 512 is big enough to 61325f6e35aSPoul-Henning Kamp * handle 16 arguments' descriptions with additional comments. 61425f6e35aSPoul-Henning Kamp */ 61525f6e35aSPoul-Henning Kamp sb = sbuf_new(NULL, NULL, 512, SBUF_FIXEDLEN); 61625f6e35aSPoul-Henning Kamp sbuf_clear(sb); 61725f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s(pid %d uid %d) aborted: ", 61825f6e35aSPoul-Henning Kamp p->p_comm, p->p_pid, td->td_ucred->cr_uid); 61925f6e35aSPoul-Henning Kamp /* 62025f6e35aSPoul-Henning Kamp * Since we can't return from abort2(), send SIGKILL in cases, where 62125f6e35aSPoul-Henning Kamp * abort2() was called improperly 62225f6e35aSPoul-Henning Kamp */ 62325f6e35aSPoul-Henning Kamp sig = SIGKILL; 62425f6e35aSPoul-Henning Kamp /* Prevent from DoSes from user-space. */ 62525f6e35aSPoul-Henning Kamp if (uap->nargs < 0 || uap->nargs > 16) 62625f6e35aSPoul-Henning Kamp goto out; 6274218a731SPoul-Henning Kamp if (uap->nargs > 0) { 62825f6e35aSPoul-Henning Kamp if (uap->args == NULL) 62925f6e35aSPoul-Henning Kamp goto out; 63025f6e35aSPoul-Henning Kamp error = copyin(uap->args, uargs, uap->nargs * sizeof(void *)); 63125f6e35aSPoul-Henning Kamp if (error != 0) 63225f6e35aSPoul-Henning Kamp goto out; 6334218a731SPoul-Henning Kamp } 63425f6e35aSPoul-Henning Kamp /* 63525f6e35aSPoul-Henning Kamp * Limit size of 'reason' string to 128. Will fit even when 63625f6e35aSPoul-Henning Kamp * maximal number of arguments was chosen to be logged. 63725f6e35aSPoul-Henning Kamp */ 63825f6e35aSPoul-Henning Kamp if (uap->why != NULL) { 63925f6e35aSPoul-Henning Kamp error = sbuf_copyin(sb, uap->why, 128); 64025f6e35aSPoul-Henning Kamp if (error < 0) 64125f6e35aSPoul-Henning Kamp goto out; 64225f6e35aSPoul-Henning Kamp } else { 64325f6e35aSPoul-Henning Kamp sbuf_printf(sb, "(null)"); 64425f6e35aSPoul-Henning Kamp } 6454218a731SPoul-Henning Kamp if (uap->nargs > 0) { 64625f6e35aSPoul-Henning Kamp sbuf_printf(sb, "("); 64725f6e35aSPoul-Henning Kamp for (i = 0;i < uap->nargs; i++) 64825f6e35aSPoul-Henning Kamp sbuf_printf(sb, "%s%p", i == 0 ? "" : ", ", uargs[i]); 64925f6e35aSPoul-Henning Kamp sbuf_printf(sb, ")"); 65025f6e35aSPoul-Henning Kamp } 65125f6e35aSPoul-Henning Kamp /* 65225f6e35aSPoul-Henning Kamp * Final stage: arguments were proper, string has been 65325f6e35aSPoul-Henning Kamp * successfully copied from userspace, and copying pointers 65425f6e35aSPoul-Henning Kamp * from user-space succeed. 65525f6e35aSPoul-Henning Kamp */ 65625f6e35aSPoul-Henning Kamp sig = SIGABRT; 65725f6e35aSPoul-Henning Kamp out: 65825f6e35aSPoul-Henning Kamp if (sig == SIGKILL) { 65925f6e35aSPoul-Henning Kamp sbuf_trim(sb); 66025f6e35aSPoul-Henning Kamp sbuf_printf(sb, " (Reason text inaccessible)"); 66125f6e35aSPoul-Henning Kamp } 66225f6e35aSPoul-Henning Kamp sbuf_cat(sb, "\n"); 66325f6e35aSPoul-Henning Kamp sbuf_finish(sb); 66425f6e35aSPoul-Henning Kamp log(LOG_INFO, "%s", sbuf_data(sb)); 66525f6e35aSPoul-Henning Kamp sbuf_delete(sb); 66625f6e35aSPoul-Henning Kamp exit1(td, W_EXITCODE(0, sig)); 66725f6e35aSPoul-Henning Kamp return (0); 66825f6e35aSPoul-Henning Kamp } 66925f6e35aSPoul-Henning Kamp 67025f6e35aSPoul-Henning Kamp 671b2f9e8b1SBruce Evans #ifdef COMPAT_43 672234216efSMatthew Dillon /* 673a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 674234216efSMatthew Dillon */ 67526f9a767SRodney W. Grimes int 676830c3153SDag-Erling Smørgrav owait(struct thread *td, struct owait_args *uap __unused) 677df8bae1dSRodney W. Grimes { 678b7e23e82SJohn Baldwin int error, status; 679df8bae1dSRodney W. Grimes 680b7e23e82SJohn Baldwin error = kern_wait(td, WAIT_ANY, &status, 0, NULL); 681b7e23e82SJohn Baldwin if (error == 0) 682b7e23e82SJohn Baldwin td->td_retval[1] = status; 683b7e23e82SJohn Baldwin return (error); 684df8bae1dSRodney W. Grimes } 685b2f9e8b1SBruce Evans #endif /* COMPAT_43 */ 686df8bae1dSRodney W. Grimes 687234216efSMatthew Dillon /* 688a9a64385SJohn Baldwin * The dirty work is handled by kern_wait(). 689234216efSMatthew Dillon */ 69026f9a767SRodney W. Grimes int 691f13b5a0fSKonstantin Belousov sys_wait4(struct thread *td, struct wait4_args *uap) 692df8bae1dSRodney W. Grimes { 69378c85e8dSJohn Baldwin struct rusage ru, *rup; 694b7e23e82SJohn Baldwin int error, status; 695b40ce416SJulian Elischer 69678c85e8dSJohn Baldwin if (uap->rusage != NULL) 69778c85e8dSJohn Baldwin rup = &ru; 69878c85e8dSJohn Baldwin else 69978c85e8dSJohn Baldwin rup = NULL; 70078c85e8dSJohn Baldwin error = kern_wait(td, uap->pid, &status, uap->options, rup); 701b7e23e82SJohn Baldwin if (uap->status != NULL && error == 0) 702b7e23e82SJohn Baldwin error = copyout(&status, uap->status, sizeof(status)); 703b7e23e82SJohn Baldwin if (uap->rusage != NULL && error == 0) 704b7e23e82SJohn Baldwin error = copyout(&ru, uap->rusage, sizeof(struct rusage)); 705b7e23e82SJohn Baldwin return (error); 706df8bae1dSRodney W. Grimes } 707df8bae1dSRodney W. Grimes 708f13b5a0fSKonstantin Belousov int 709f13b5a0fSKonstantin Belousov sys_wait6(struct thread *td, struct wait6_args *uap) 710f13b5a0fSKonstantin Belousov { 711f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 712f13b5a0fSKonstantin Belousov siginfo_t si, *sip; 713f13b5a0fSKonstantin Belousov idtype_t idtype; 714f13b5a0fSKonstantin Belousov id_t id; 715a2a85596SKonstantin Belousov int error, status; 716f13b5a0fSKonstantin Belousov 717f13b5a0fSKonstantin Belousov idtype = uap->idtype; 718f13b5a0fSKonstantin Belousov id = uap->id; 719f13b5a0fSKonstantin Belousov 720f13b5a0fSKonstantin Belousov if (uap->wrusage != NULL) 721f13b5a0fSKonstantin Belousov wrup = &wru; 722f13b5a0fSKonstantin Belousov else 723f13b5a0fSKonstantin Belousov wrup = NULL; 724f13b5a0fSKonstantin Belousov 725f13b5a0fSKonstantin Belousov if (uap->info != NULL) { 726f13b5a0fSKonstantin Belousov sip = &si; 727f13b5a0fSKonstantin Belousov bzero(sip, sizeof(*sip)); 728f13b5a0fSKonstantin Belousov } else 729f13b5a0fSKonstantin Belousov sip = NULL; 730f13b5a0fSKonstantin Belousov 731f13b5a0fSKonstantin Belousov /* 732f13b5a0fSKonstantin Belousov * We expect all callers of wait6() to know about WEXITED and 733f13b5a0fSKonstantin Belousov * WTRAPPED. 734f13b5a0fSKonstantin Belousov */ 735f13b5a0fSKonstantin Belousov error = kern_wait6(td, idtype, id, &status, uap->options, wrup, sip); 736f13b5a0fSKonstantin Belousov 737f13b5a0fSKonstantin Belousov if (uap->status != NULL && error == 0) 738f13b5a0fSKonstantin Belousov error = copyout(&status, uap->status, sizeof(status)); 739f13b5a0fSKonstantin Belousov if (uap->wrusage != NULL && error == 0) 740f13b5a0fSKonstantin Belousov error = copyout(&wru, uap->wrusage, sizeof(wru)); 741f13b5a0fSKonstantin Belousov if (uap->info != NULL && error == 0) 742f13b5a0fSKonstantin Belousov error = copyout(&si, uap->info, sizeof(si)); 743f13b5a0fSKonstantin Belousov return (error); 744f13b5a0fSKonstantin Belousov } 745f13b5a0fSKonstantin Belousov 746324fb6beSRobert Watson /* 747324fb6beSRobert Watson * Reap the remains of a zombie process and optionally return status and 748324fb6beSRobert Watson * rusage. Asserts and will release both the proctree_lock and the process 749324fb6beSRobert Watson * lock as part of its work. 750324fb6beSRobert Watson */ 751cfb5f768SJonathan Anderson void 752f13b5a0fSKonstantin Belousov proc_reap(struct thread *td, struct proc *p, int *status, int options) 753324fb6beSRobert Watson { 754324fb6beSRobert Watson struct proc *q, *t; 755324fb6beSRobert Watson 756324fb6beSRobert Watson sx_assert(&proctree_lock, SA_XLOCKED); 757324fb6beSRobert Watson PROC_LOCK_ASSERT(p, MA_OWNED); 758324fb6beSRobert Watson PROC_SLOCK_ASSERT(p, MA_OWNED); 759324fb6beSRobert Watson KASSERT(p->p_state == PRS_ZOMBIE, ("proc_reap: !PRS_ZOMBIE")); 760324fb6beSRobert Watson 761324fb6beSRobert Watson q = td->td_proc; 762f13b5a0fSKonstantin Belousov 763324fb6beSRobert Watson PROC_SUNLOCK(p); 764324fb6beSRobert Watson td->td_retval[0] = p->p_pid; 765324fb6beSRobert Watson if (status) 766324fb6beSRobert Watson *status = p->p_xstat; /* convert to int */ 767324fb6beSRobert Watson if (options & WNOWAIT) { 768324fb6beSRobert Watson /* 769324fb6beSRobert Watson * Only poll, returning the status. Caller does not wish to 770324fb6beSRobert Watson * release the proc struct just yet. 771324fb6beSRobert Watson */ 772324fb6beSRobert Watson PROC_UNLOCK(p); 773324fb6beSRobert Watson sx_xunlock(&proctree_lock); 774324fb6beSRobert Watson return; 775324fb6beSRobert Watson } 776324fb6beSRobert Watson 777324fb6beSRobert Watson PROC_LOCK(q); 778324fb6beSRobert Watson sigqueue_take(p->p_ksi); 779324fb6beSRobert Watson PROC_UNLOCK(q); 780324fb6beSRobert Watson PROC_UNLOCK(p); 781324fb6beSRobert Watson 782324fb6beSRobert Watson /* 783324fb6beSRobert Watson * If we got the child via a ptrace 'attach', we need to give it back 784324fb6beSRobert Watson * to the old parent. 785324fb6beSRobert Watson */ 786324fb6beSRobert Watson if (p->p_oppid && (t = pfind(p->p_oppid)) != NULL) { 787324fb6beSRobert Watson PROC_LOCK(p); 788324fb6beSRobert Watson proc_reparent(p, t); 789f528c3fdSDavid E. O'Brien p->p_oppid = 0; 790324fb6beSRobert Watson PROC_UNLOCK(p); 791ad6eec7bSJohn Baldwin pksignal(t, SIGCHLD, p->p_ksi); 792324fb6beSRobert Watson wakeup(t); 793324fb6beSRobert Watson cv_broadcast(&p->p_pwait); 794324fb6beSRobert Watson PROC_UNLOCK(t); 795324fb6beSRobert Watson sx_xunlock(&proctree_lock); 796324fb6beSRobert Watson return; 797324fb6beSRobert Watson } 798324fb6beSRobert Watson 799324fb6beSRobert Watson /* 800324fb6beSRobert Watson * Remove other references to this process to ensure we have an 801324fb6beSRobert Watson * exclusive reference. 802324fb6beSRobert Watson */ 803324fb6beSRobert Watson sx_xlock(&allproc_lock); 804324fb6beSRobert Watson LIST_REMOVE(p, p_list); /* off zombproc */ 805324fb6beSRobert Watson sx_xunlock(&allproc_lock); 806324fb6beSRobert Watson LIST_REMOVE(p, p_sibling); 8077335ed90SKonstantin Belousov PROC_LOCK(p); 8082e39e24fSKonstantin Belousov clear_orphan(p); 8097335ed90SKonstantin Belousov PROC_UNLOCK(p); 810324fb6beSRobert Watson leavepgrp(p); 811cfb5f768SJonathan Anderson #ifdef PROCDESC 812cfb5f768SJonathan Anderson if (p->p_procdesc != NULL) 813cfb5f768SJonathan Anderson procdesc_reap(p); 814cfb5f768SJonathan Anderson #endif 815324fb6beSRobert Watson sx_xunlock(&proctree_lock); 816324fb6beSRobert Watson 817324fb6beSRobert Watson /* 818324fb6beSRobert Watson * As a side effect of this lock, we know that all other writes to 819324fb6beSRobert Watson * this proc are visible now, so no more locking is needed for p. 820324fb6beSRobert Watson */ 821324fb6beSRobert Watson PROC_LOCK(p); 822324fb6beSRobert Watson p->p_xstat = 0; /* XXX: why? */ 823324fb6beSRobert Watson PROC_UNLOCK(p); 824324fb6beSRobert Watson PROC_LOCK(q); 825324fb6beSRobert Watson ruadd(&q->p_stats->p_cru, &q->p_crux, &p->p_ru, &p->p_rux); 826324fb6beSRobert Watson PROC_UNLOCK(q); 827324fb6beSRobert Watson 828324fb6beSRobert Watson /* 829324fb6beSRobert Watson * Decrement the count of procs running with this uid. 830324fb6beSRobert Watson */ 831324fb6beSRobert Watson (void)chgproccnt(p->p_ucred->cr_ruidinfo, -1, 0); 832324fb6beSRobert Watson 833324fb6beSRobert Watson /* 834097055e2SEdward Tomasz Napierala * Destroy resource accounting information associated with the process. 835097055e2SEdward Tomasz Napierala */ 836afcc55f3SEdward Tomasz Napierala #ifdef RACCT 837b38520f0SEdward Tomasz Napierala PROC_LOCK(p); 838b38520f0SEdward Tomasz Napierala racct_sub(p, RACCT_NPROC, 1); 839b38520f0SEdward Tomasz Napierala PROC_UNLOCK(p); 840afcc55f3SEdward Tomasz Napierala #endif 841b38520f0SEdward Tomasz Napierala racct_proc_exit(p); 842097055e2SEdward Tomasz Napierala 843097055e2SEdward Tomasz Napierala /* 844324fb6beSRobert Watson * Free credentials, arguments, and sigacts. 845324fb6beSRobert Watson */ 846324fb6beSRobert Watson crfree(p->p_ucred); 847324fb6beSRobert Watson p->p_ucred = NULL; 848324fb6beSRobert Watson pargs_drop(p->p_args); 849324fb6beSRobert Watson p->p_args = NULL; 850324fb6beSRobert Watson sigacts_free(p->p_sigacts); 851324fb6beSRobert Watson p->p_sigacts = NULL; 852324fb6beSRobert Watson 853324fb6beSRobert Watson /* 854324fb6beSRobert Watson * Do any thread-system specific cleanups. 855324fb6beSRobert Watson */ 856324fb6beSRobert Watson thread_wait(p); 857324fb6beSRobert Watson 858324fb6beSRobert Watson /* 859324fb6beSRobert Watson * Give vm and machine-dependent layer a chance to free anything that 860324fb6beSRobert Watson * cpu_exit couldn't release while still running in process context. 861324fb6beSRobert Watson */ 862324fb6beSRobert Watson vm_waitproc(p); 863324fb6beSRobert Watson #ifdef MAC 864324fb6beSRobert Watson mac_proc_destroy(p); 865324fb6beSRobert Watson #endif 866324fb6beSRobert Watson KASSERT(FIRST_THREAD_IN_PROC(p), 867324fb6beSRobert Watson ("proc_reap: no residual thread!")); 868324fb6beSRobert Watson uma_zfree(proc_zone, p); 869324fb6beSRobert Watson sx_xlock(&allproc_lock); 870324fb6beSRobert Watson nprocs--; 871324fb6beSRobert Watson sx_xunlock(&allproc_lock); 872324fb6beSRobert Watson } 873324fb6beSRobert Watson 874dcd43281SKonstantin Belousov static int 875f13b5a0fSKonstantin Belousov proc_to_reap(struct thread *td, struct proc *p, idtype_t idtype, id_t id, 876f13b5a0fSKonstantin Belousov int *status, int options, struct __wrusage *wrusage, siginfo_t *siginfo) 877dcd43281SKonstantin Belousov { 878dcd43281SKonstantin Belousov struct proc *q; 879f13b5a0fSKonstantin Belousov struct rusage *rup; 880dcd43281SKonstantin Belousov 881db62ced2SJaakko Heinonen sx_assert(&proctree_lock, SA_XLOCKED); 882db62ced2SJaakko Heinonen 883dcd43281SKonstantin Belousov q = td->td_proc; 884dcd43281SKonstantin Belousov PROC_LOCK(p); 885f13b5a0fSKonstantin Belousov 886f13b5a0fSKonstantin Belousov switch (idtype) { 887f13b5a0fSKonstantin Belousov case P_ALL: 888f13b5a0fSKonstantin Belousov break; 889f13b5a0fSKonstantin Belousov case P_PID: 890f13b5a0fSKonstantin Belousov if (p->p_pid != (pid_t)id) { 891dcd43281SKonstantin Belousov PROC_UNLOCK(p); 892dcd43281SKonstantin Belousov return (0); 893dcd43281SKonstantin Belousov } 894f13b5a0fSKonstantin Belousov break; 895f13b5a0fSKonstantin Belousov case P_PGID: 896f13b5a0fSKonstantin Belousov if (p->p_pgid != (pid_t)id) { 897f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 898f13b5a0fSKonstantin Belousov return (0); 899f13b5a0fSKonstantin Belousov } 900f13b5a0fSKonstantin Belousov break; 901f13b5a0fSKonstantin Belousov case P_SID: 902f13b5a0fSKonstantin Belousov if (p->p_session->s_sid != (pid_t)id) { 903f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 904f13b5a0fSKonstantin Belousov return (0); 905f13b5a0fSKonstantin Belousov } 906f13b5a0fSKonstantin Belousov break; 907f13b5a0fSKonstantin Belousov case P_UID: 908f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_uid != (uid_t)id) { 909f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 910f13b5a0fSKonstantin Belousov return (0); 911f13b5a0fSKonstantin Belousov } 912f13b5a0fSKonstantin Belousov break; 913f13b5a0fSKonstantin Belousov case P_GID: 914f13b5a0fSKonstantin Belousov if (p->p_ucred->cr_gid != (gid_t)id) { 915f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 916f13b5a0fSKonstantin Belousov return (0); 917f13b5a0fSKonstantin Belousov } 918f13b5a0fSKonstantin Belousov break; 919f13b5a0fSKonstantin Belousov case P_JAILID: 920b1051d92SMateusz Guzik if (p->p_ucred->cr_prison->pr_id != (int)id) { 921f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 922f13b5a0fSKonstantin Belousov return (0); 923f13b5a0fSKonstantin Belousov } 924f13b5a0fSKonstantin Belousov break; 925f13b5a0fSKonstantin Belousov /* 926f13b5a0fSKonstantin Belousov * It seems that the thread structures get zeroed out 927f13b5a0fSKonstantin Belousov * at process exit. This makes it impossible to 928f13b5a0fSKonstantin Belousov * support P_SETID, P_CID or P_CPUID. 929f13b5a0fSKonstantin Belousov */ 930f13b5a0fSKonstantin Belousov default: 931f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 932f13b5a0fSKonstantin Belousov return (0); 933f13b5a0fSKonstantin Belousov } 934f13b5a0fSKonstantin Belousov 935dcd43281SKonstantin Belousov if (p_canwait(td, p)) { 936dcd43281SKonstantin Belousov PROC_UNLOCK(p); 937dcd43281SKonstantin Belousov return (0); 938dcd43281SKonstantin Belousov } 939dcd43281SKonstantin Belousov 940f13b5a0fSKonstantin Belousov if (((options & WEXITED) == 0) && (p->p_state == PRS_ZOMBIE)) { 941f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 942f13b5a0fSKonstantin Belousov return (0); 943f13b5a0fSKonstantin Belousov } 944f13b5a0fSKonstantin Belousov 945dcd43281SKonstantin Belousov /* 946dcd43281SKonstantin Belousov * This special case handles a kthread spawned by linux_clone 947dcd43281SKonstantin Belousov * (see linux_misc.c). The linux_wait4 and linux_waitpid 948dcd43281SKonstantin Belousov * functions need to be able to distinguish between waiting 949dcd43281SKonstantin Belousov * on a process and waiting on a thread. It is a thread if 950dcd43281SKonstantin Belousov * p_sigparent is not SIGCHLD, and the WLINUXCLONE option 951dcd43281SKonstantin Belousov * signifies we want to wait for threads and not processes. 952dcd43281SKonstantin Belousov */ 953dcd43281SKonstantin Belousov if ((p->p_sigparent != SIGCHLD) ^ 954dcd43281SKonstantin Belousov ((options & WLINUXCLONE) != 0)) { 955dcd43281SKonstantin Belousov PROC_UNLOCK(p); 956dcd43281SKonstantin Belousov return (0); 957dcd43281SKonstantin Belousov } 958dcd43281SKonstantin Belousov 959dcd43281SKonstantin Belousov PROC_SLOCK(p); 960f13b5a0fSKonstantin Belousov 961f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 962f13b5a0fSKonstantin Belousov bzero(siginfo, sizeof(*siginfo)); 963f13b5a0fSKonstantin Belousov siginfo->si_errno = 0; 964f13b5a0fSKonstantin Belousov 965f13b5a0fSKonstantin Belousov /* 966f13b5a0fSKonstantin Belousov * SUSv4 requires that the si_signo value is always 967f13b5a0fSKonstantin Belousov * SIGCHLD. Obey it despite the rfork(2) interface 968f13b5a0fSKonstantin Belousov * allows to request other signal for child exit 969f13b5a0fSKonstantin Belousov * notification. 970f13b5a0fSKonstantin Belousov */ 971f13b5a0fSKonstantin Belousov siginfo->si_signo = SIGCHLD; 972f13b5a0fSKonstantin Belousov 973f13b5a0fSKonstantin Belousov /* 974f13b5a0fSKonstantin Belousov * This is still a rough estimate. We will fix the 975f13b5a0fSKonstantin Belousov * cases TRAPPED, STOPPED, and CONTINUED later. 976f13b5a0fSKonstantin Belousov */ 977*b20a9aa9SJilles Tjoelker if (WCOREDUMP(p->p_xstat)) { 978f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_DUMPED; 979*b20a9aa9SJilles Tjoelker siginfo->si_status = WTERMSIG(p->p_xstat); 980*b20a9aa9SJilles Tjoelker } else if (WIFSIGNALED(p->p_xstat)) { 981f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_KILLED; 982*b20a9aa9SJilles Tjoelker siginfo->si_status = WTERMSIG(p->p_xstat); 983*b20a9aa9SJilles Tjoelker } else { 984f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_EXITED; 985*b20a9aa9SJilles Tjoelker siginfo->si_status = WEXITSTATUS(p->p_xstat); 986*b20a9aa9SJilles Tjoelker } 987f13b5a0fSKonstantin Belousov 988f13b5a0fSKonstantin Belousov siginfo->si_pid = p->p_pid; 989f13b5a0fSKonstantin Belousov siginfo->si_uid = p->p_ucred->cr_uid; 990f13b5a0fSKonstantin Belousov 991f13b5a0fSKonstantin Belousov /* 992f13b5a0fSKonstantin Belousov * The si_addr field would be useful additional 993f13b5a0fSKonstantin Belousov * detail, but apparently the PC value may be lost 994f13b5a0fSKonstantin Belousov * when we reach this point. bzero() above sets 995f13b5a0fSKonstantin Belousov * siginfo->si_addr to NULL. 996f13b5a0fSKonstantin Belousov */ 997f13b5a0fSKonstantin Belousov } 998f13b5a0fSKonstantin Belousov 999f13b5a0fSKonstantin Belousov /* 1000f13b5a0fSKonstantin Belousov * There should be no reason to limit resources usage info to 1001f13b5a0fSKonstantin Belousov * exited processes only. A snapshot about any resources used 1002f13b5a0fSKonstantin Belousov * by a stopped process may be exactly what is needed. 1003f13b5a0fSKonstantin Belousov */ 1004f13b5a0fSKonstantin Belousov if (wrusage != NULL) { 1005f13b5a0fSKonstantin Belousov rup = &wrusage->wru_self; 1006f13b5a0fSKonstantin Belousov *rup = p->p_ru; 1007f13b5a0fSKonstantin Belousov calcru(p, &rup->ru_utime, &rup->ru_stime); 1008f13b5a0fSKonstantin Belousov 1009f13b5a0fSKonstantin Belousov rup = &wrusage->wru_children; 1010f13b5a0fSKonstantin Belousov *rup = p->p_stats->p_cru; 1011f13b5a0fSKonstantin Belousov calccru(p, &rup->ru_utime, &rup->ru_stime); 1012f13b5a0fSKonstantin Belousov } 1013f13b5a0fSKonstantin Belousov 1014dcd43281SKonstantin Belousov if (p->p_state == PRS_ZOMBIE) { 1015f13b5a0fSKonstantin Belousov proc_reap(td, p, status, options); 1016dcd43281SKonstantin Belousov return (-1); 1017dcd43281SKonstantin Belousov } 1018dcd43281SKonstantin Belousov PROC_SUNLOCK(p); 1019dcd43281SKonstantin Belousov PROC_UNLOCK(p); 1020dcd43281SKonstantin Belousov return (1); 1021dcd43281SKonstantin Belousov } 1022dcd43281SKonstantin Belousov 1023b7e23e82SJohn Baldwin int 10247eaec467SJohn Baldwin kern_wait(struct thread *td, pid_t pid, int *status, int options, 10257eaec467SJohn Baldwin struct rusage *rusage) 1026df8bae1dSRodney W. Grimes { 1027f13b5a0fSKonstantin Belousov struct __wrusage wru, *wrup; 1028f13b5a0fSKonstantin Belousov idtype_t idtype; 1029f13b5a0fSKonstantin Belousov id_t id; 1030f13b5a0fSKonstantin Belousov int ret; 1031f13b5a0fSKonstantin Belousov 1032538375d4SKonstantin Belousov /* 1033538375d4SKonstantin Belousov * Translate the special pid values into the (idtype, pid) 1034538375d4SKonstantin Belousov * pair for kern_wait6. The WAIT_MYPGRP case is handled by 1035538375d4SKonstantin Belousov * kern_wait6() on its own. 1036538375d4SKonstantin Belousov */ 1037f13b5a0fSKonstantin Belousov if (pid == WAIT_ANY) { 1038f13b5a0fSKonstantin Belousov idtype = P_ALL; 1039f13b5a0fSKonstantin Belousov id = 0; 1040ea293f3fSKonstantin Belousov } else if (pid < 0) { 1041f13b5a0fSKonstantin Belousov idtype = P_PGID; 1042f13b5a0fSKonstantin Belousov id = (id_t)-pid; 1043ea293f3fSKonstantin Belousov } else { 1044f13b5a0fSKonstantin Belousov idtype = P_PID; 1045f13b5a0fSKonstantin Belousov id = (id_t)pid; 1046f13b5a0fSKonstantin Belousov } 1047538375d4SKonstantin Belousov 1048f13b5a0fSKonstantin Belousov if (rusage != NULL) 1049f13b5a0fSKonstantin Belousov wrup = &wru; 1050f13b5a0fSKonstantin Belousov else 1051f13b5a0fSKonstantin Belousov wrup = NULL; 1052538375d4SKonstantin Belousov 1053f13b5a0fSKonstantin Belousov /* 1054f13b5a0fSKonstantin Belousov * For backward compatibility we implicitly add flags WEXITED 1055f13b5a0fSKonstantin Belousov * and WTRAPPED here. 1056f13b5a0fSKonstantin Belousov */ 1057f13b5a0fSKonstantin Belousov options |= WEXITED | WTRAPPED; 1058f13b5a0fSKonstantin Belousov ret = kern_wait6(td, idtype, id, status, options, wrup, NULL); 1059f13b5a0fSKonstantin Belousov if (rusage != NULL) 1060f13b5a0fSKonstantin Belousov *rusage = wru.wru_self; 1061f13b5a0fSKonstantin Belousov return (ret); 1062f13b5a0fSKonstantin Belousov } 1063f13b5a0fSKonstantin Belousov 1064f13b5a0fSKonstantin Belousov int 1065f13b5a0fSKonstantin Belousov kern_wait6(struct thread *td, idtype_t idtype, id_t id, int *status, 1066f13b5a0fSKonstantin Belousov int options, struct __wrusage *wrusage, siginfo_t *siginfo) 1067f13b5a0fSKonstantin Belousov { 1068324fb6beSRobert Watson struct proc *p, *q; 1069dcd43281SKonstantin Belousov int error, nfound, ret; 1070df8bae1dSRodney W. Grimes 1071f13b5a0fSKonstantin Belousov AUDIT_ARG_VALUE((int)idtype); /* XXX - This is likely wrong! */ 1072f13b5a0fSKonstantin Belousov AUDIT_ARG_PID((pid_t)id); /* XXX - This may be wrong! */ 10732ef24ddeSRobert Watson AUDIT_ARG_VALUE(options); 1074de3007e8SWayne Salamon 1075b40ce416SJulian Elischer q = td->td_proc; 1076f13b5a0fSKonstantin Belousov 1077a2a85596SKonstantin Belousov if ((pid_t)id == WAIT_MYPGRP && (idtype == P_PID || idtype == P_PGID)) { 1078a545089eSKonstantin Belousov PROC_LOCK(q); 1079f13b5a0fSKonstantin Belousov id = (id_t)q->p_pgid; 1080a545089eSKonstantin Belousov PROC_UNLOCK(q); 1081f13b5a0fSKonstantin Belousov idtype = P_PGID; 1082f591779bSSeigo Tanimura } 1083f13b5a0fSKonstantin Belousov 1084f528c3fdSDavid E. O'Brien /* If we don't know the option, just return. */ 1085f13b5a0fSKonstantin Belousov if ((options & ~(WUNTRACED | WNOHANG | WCONTINUED | WNOWAIT | 1086f13b5a0fSKonstantin Belousov WEXITED | WTRAPPED | WLINUXCLONE)) != 0) 10876dc958b9SJohn Baldwin return (EINVAL); 1088f13b5a0fSKonstantin Belousov if ((options & (WEXITED | WUNTRACED | WCONTINUED | WTRAPPED)) == 0) { 1089f13b5a0fSKonstantin Belousov /* 1090f13b5a0fSKonstantin Belousov * We will be unable to find any matching processes, 1091f13b5a0fSKonstantin Belousov * because there are no known events to look for. 1092f13b5a0fSKonstantin Belousov * Prefer to return error instead of blocking 1093f13b5a0fSKonstantin Belousov * indefinitely. 1094f13b5a0fSKonstantin Belousov */ 1095f13b5a0fSKonstantin Belousov return (EINVAL); 1096f13b5a0fSKonstantin Belousov } 1097f13b5a0fSKonstantin Belousov 1098df8bae1dSRodney W. Grimes loop: 1099902c0d82SDavid Xu if (q->p_flag & P_STATCHILD) { 1100902c0d82SDavid Xu PROC_LOCK(q); 1101902c0d82SDavid Xu q->p_flag &= ~P_STATCHILD; 1102902c0d82SDavid Xu PROC_UNLOCK(q); 1103902c0d82SDavid Xu } 1104df8bae1dSRodney W. Grimes nfound = 0; 1105d7aadbf9SJohn Baldwin sx_xlock(&proctree_lock); 11062e3c8fcbSPoul-Henning Kamp LIST_FOREACH(p, &q->p_children, p_sibling) { 1107f13b5a0fSKonstantin Belousov ret = proc_to_reap(td, p, idtype, id, status, options, 1108f13b5a0fSKonstantin Belousov wrusage, siginfo); 1109dcd43281SKonstantin Belousov if (ret == 0) 1110df8bae1dSRodney W. Grimes continue; 1111dcd43281SKonstantin Belousov else if (ret == 1) 1112df8bae1dSRodney W. Grimes nfound++; 1113dcd43281SKonstantin Belousov else 1114d7aadbf9SJohn Baldwin return (0); 1115dcd43281SKonstantin Belousov 1116dcd43281SKonstantin Belousov PROC_LOCK(p); 1117dcd43281SKonstantin Belousov PROC_SLOCK(p); 1118f13b5a0fSKonstantin Belousov 1119f13b5a0fSKonstantin Belousov if ((options & WTRAPPED) != 0 && 1120f13b5a0fSKonstantin Belousov (p->p_flag & P_TRACED) != 0 && 1121f13b5a0fSKonstantin Belousov (p->p_flag & (P_STOPPED_TRACE | P_STOPPED_SIG)) != 0 && 11225a2f73e6SDavid Xu (p->p_suspcount == p->p_numthreads) && 1123f13b5a0fSKonstantin Belousov ((p->p_flag & P_WAITED) == 0)) { 1124982d11f8SJeff Roberson PROC_SUNLOCK(p); 1125f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) 1126df8bae1dSRodney W. Grimes p->p_flag |= P_WAITED; 1127d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1128b40ce416SJulian Elischer td->td_retval[0] = p->p_pid; 1129ebceaf6dSDavid Xu 1130f13b5a0fSKonstantin Belousov if (status != NULL) 1131f13b5a0fSKonstantin Belousov *status = W_STOPCODE(p->p_xstat); 1132f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1133f13b5a0fSKonstantin Belousov siginfo->si_status = p->p_xstat; 1134f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_TRAPPED; 1135f13b5a0fSKonstantin Belousov } 1136f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) { 1137ebceaf6dSDavid Xu PROC_LOCK(q); 1138ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 1139ebceaf6dSDavid Xu PROC_UNLOCK(q); 1140f13b5a0fSKonstantin Belousov } 1141ebceaf6dSDavid Xu 1142f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1143f13b5a0fSKonstantin Belousov return (0); 1144f13b5a0fSKonstantin Belousov } 1145f13b5a0fSKonstantin Belousov if ((options & WUNTRACED) != 0 && 1146f13b5a0fSKonstantin Belousov (p->p_flag & P_STOPPED_SIG) != 0 && 1147f13b5a0fSKonstantin Belousov (p->p_suspcount == p->p_numthreads) && 1148f13b5a0fSKonstantin Belousov ((p->p_flag & P_WAITED) == 0)) { 1149f13b5a0fSKonstantin Belousov PROC_SUNLOCK(p); 1150f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) 1151f13b5a0fSKonstantin Belousov p->p_flag |= P_WAITED; 1152f13b5a0fSKonstantin Belousov sx_xunlock(&proctree_lock); 1153f13b5a0fSKonstantin Belousov td->td_retval[0] = p->p_pid; 1154f13b5a0fSKonstantin Belousov 1155f13b5a0fSKonstantin Belousov if (status != NULL) 1156f13b5a0fSKonstantin Belousov *status = W_STOPCODE(p->p_xstat); 1157f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1158f13b5a0fSKonstantin Belousov siginfo->si_status = p->p_xstat; 1159f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_STOPPED; 1160f13b5a0fSKonstantin Belousov } 1161f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) { 1162f13b5a0fSKonstantin Belousov PROC_LOCK(q); 1163f13b5a0fSKonstantin Belousov sigqueue_take(p->p_ksi); 1164f13b5a0fSKonstantin Belousov PROC_UNLOCK(q); 1165f13b5a0fSKonstantin Belousov } 1166f13b5a0fSKonstantin Belousov 1167f13b5a0fSKonstantin Belousov PROC_UNLOCK(p); 1168b7e23e82SJohn Baldwin return (0); 1169df8bae1dSRodney W. Grimes } 1170982d11f8SJeff Roberson PROC_SUNLOCK(p); 1171f13b5a0fSKonstantin Belousov if ((options & WCONTINUED) != 0 && 1172f13b5a0fSKonstantin Belousov (p->p_flag & P_CONTINUED) != 0) { 11736ee093fbSMike Barcroft sx_xunlock(&proctree_lock); 11746ee093fbSMike Barcroft td->td_retval[0] = p->p_pid; 1175f13b5a0fSKonstantin Belousov if ((options & WNOWAIT) == 0) { 11766ee093fbSMike Barcroft p->p_flag &= ~P_CONTINUED; 1177ebceaf6dSDavid Xu PROC_LOCK(q); 1178ebceaf6dSDavid Xu sigqueue_take(p->p_ksi); 1179ebceaf6dSDavid Xu PROC_UNLOCK(q); 1180f13b5a0fSKonstantin Belousov } 1181e94cc4acSDavid Xu PROC_UNLOCK(p); 1182ebceaf6dSDavid Xu 1183f13b5a0fSKonstantin Belousov if (status != NULL) 1184b7e23e82SJohn Baldwin *status = SIGCONT; 1185f13b5a0fSKonstantin Belousov if (siginfo != NULL) { 1186f13b5a0fSKonstantin Belousov siginfo->si_status = SIGCONT; 1187f13b5a0fSKonstantin Belousov siginfo->si_code = CLD_CONTINUED; 1188f13b5a0fSKonstantin Belousov } 1189dcdc6c36SKonstantin Belousov return (0); 11906ee093fbSMike Barcroft } 1191d7aadbf9SJohn Baldwin PROC_UNLOCK(p); 1192d7aadbf9SJohn Baldwin } 1193dcd43281SKonstantin Belousov 1194dcd43281SKonstantin Belousov /* 1195dcd43281SKonstantin Belousov * Look in the orphans list too, to allow the parent to 1196dcd43281SKonstantin Belousov * collect it's child exit status even if child is being 1197dcd43281SKonstantin Belousov * debugged. 1198dcd43281SKonstantin Belousov * 1199dcd43281SKonstantin Belousov * Debugger detaches from the parent upon successful 1200dcd43281SKonstantin Belousov * switch-over from parent to child. At this point due to 1201dcd43281SKonstantin Belousov * re-parenting the parent loses the child to debugger and a 1202dcd43281SKonstantin Belousov * wait4(2) call would report that it has no children to wait 1203dcd43281SKonstantin Belousov * for. By maintaining a list of orphans we allow the parent 1204dcd43281SKonstantin Belousov * to successfully wait until the child becomes a zombie. 1205dcd43281SKonstantin Belousov */ 1206dcd43281SKonstantin Belousov LIST_FOREACH(p, &q->p_orphans, p_orphan) { 1207f13b5a0fSKonstantin Belousov ret = proc_to_reap(td, p, idtype, id, status, options, 1208f13b5a0fSKonstantin Belousov wrusage, siginfo); 1209dcd43281SKonstantin Belousov if (ret == 0) 1210dcd43281SKonstantin Belousov continue; 1211dcd43281SKonstantin Belousov else if (ret == 1) 1212dcd43281SKonstantin Belousov nfound++; 1213dcd43281SKonstantin Belousov else 1214dcd43281SKonstantin Belousov return (0); 1215dcd43281SKonstantin Belousov } 1216d7aadbf9SJohn Baldwin if (nfound == 0) { 1217d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1218d7aadbf9SJohn Baldwin return (ECHILD); 1219d7aadbf9SJohn Baldwin } 1220b7e23e82SJohn Baldwin if (options & WNOHANG) { 1221d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 1222d7aadbf9SJohn Baldwin td->td_retval[0] = 0; 1223d7aadbf9SJohn Baldwin return (0); 1224d7aadbf9SJohn Baldwin } 1225d7aadbf9SJohn Baldwin PROC_LOCK(q); 1226d7aadbf9SJohn Baldwin sx_xunlock(&proctree_lock); 122795992d56SDavid Xu if (q->p_flag & P_STATCHILD) { 122895992d56SDavid Xu q->p_flag &= ~P_STATCHILD; 122995992d56SDavid Xu error = 0; 123095992d56SDavid Xu } else 12317f05b035SAlfred Perlstein error = msleep(q, &q->p_mtx, PWAIT | PCATCH, "wait", 0); 1232d7aadbf9SJohn Baldwin PROC_UNLOCK(q); 12334ae89b95SJohn Baldwin if (error) 1234d7aadbf9SJohn Baldwin return (error); 1235d7aadbf9SJohn Baldwin goto loop; 1236d7aadbf9SJohn Baldwin } 1237df8bae1dSRodney W. Grimes 1238df8bae1dSRodney W. Grimes /* 123998f03f90SJake Burkholder * Make process 'parent' the new parent of process 'child'. 124098f03f90SJake Burkholder * Must be called with an exclusive hold of proctree lock. 1241df8bae1dSRodney W. Grimes */ 1242df8bae1dSRodney W. Grimes void 1243830c3153SDag-Erling Smørgrav proc_reparent(struct proc *child, struct proc *parent) 1244df8bae1dSRodney W. Grimes { 1245df8bae1dSRodney W. Grimes 12464e5e677bSJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 1247c65437a3SJohn Baldwin PROC_LOCK_ASSERT(child, MA_OWNED); 1248df8bae1dSRodney W. Grimes if (child->p_pptr == parent) 1249df8bae1dSRodney W. Grimes return; 1250df8bae1dSRodney W. Grimes 1251ff766807SDavid Xu PROC_LOCK(child->p_pptr); 1252ff766807SDavid Xu sigqueue_take(child->p_ksi); 1253ff766807SDavid Xu PROC_UNLOCK(child->p_pptr); 1254b75356e1SJeffrey Hsu LIST_REMOVE(child, p_sibling); 1255b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&parent->p_children, child, p_sibling); 1256dcd43281SKonstantin Belousov 12572e39e24fSKonstantin Belousov clear_orphan(child); 1258dcd43281SKonstantin Belousov if (child->p_flag & P_TRACED) { 1259dcd43281SKonstantin Belousov LIST_INSERT_HEAD(&child->p_pptr->p_orphans, child, p_orphan); 1260dcd43281SKonstantin Belousov child->p_flag |= P_ORPHAN; 1261dcd43281SKonstantin Belousov } 1262dcd43281SKonstantin Belousov 1263df8bae1dSRodney W. Grimes child->p_pptr = parent; 1264df8bae1dSRodney W. Grimes } 1265