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 * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29b75356e1SJeffrey Hsu * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 356c84de02SJohn Baldwin #include "opt_ktrace.h" 36b9f009b0SPeter Wemm #include "opt_kstack_pages.h" 376c84de02SJohn Baldwin 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40df8bae1dSRodney W. Grimes #include <sys/kernel.h> 41fb919e4dSMark Murray #include <sys/lock.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43fb919e4dSMark Murray #include <sys/mutex.h> 44b9df5231SPoul-Henning Kamp #include <sys/proc.h> 4555b4a5aeSJohn Baldwin #include <sys/refcount.h> 46baf731e6SRobert Drehmel #include <sys/sysent.h> 47de028f5aSJeff Roberson #include <sys/sched.h> 48165d2b99SJulian Elischer #include <sys/smp.h> 49fb919e4dSMark Murray #include <sys/sysctl.h> 5062d6ce3aSDon Lewis #include <sys/filedesc.h> 51df8bae1dSRodney W. Grimes #include <sys/tty.h> 52bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h> 531005a129SJohn Baldwin #include <sys/sx.h> 54fb919e4dSMark Murray #include <sys/user.h> 55fb919e4dSMark Murray #include <sys/jail.h> 56fe769cddSDavid Schultz #include <sys/vnode.h> 576c84de02SJohn Baldwin #ifdef KTRACE 586c84de02SJohn Baldwin #include <sys/uio.h> 596c84de02SJohn Baldwin #include <sys/ktrace.h> 606c84de02SJohn Baldwin #endif 61fb919e4dSMark Murray 62efeaf95aSDavid Greenman #include <vm/vm.h> 63a136efe9SPeter Wemm #include <vm/vm_extern.h> 64efeaf95aSDavid Greenman #include <vm/pmap.h> 65efeaf95aSDavid Greenman #include <vm/vm_map.h> 66c897b813SJeff Roberson #include <vm/uma.h> 67df8bae1dSRodney W. Grimes 68f591779bSSeigo Tanimura MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 69a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header"); 70876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 71a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 7255166637SPoul-Henning Kamp 734d77a549SAlfred Perlstein static void doenterpgrp(struct proc *, struct pgrp *); 744d77a549SAlfred Perlstein static void orphanpg(struct pgrp *pg); 755032ff81SDon Lewis static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 765032ff81SDon Lewis static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp); 7702e878d9SJohn Baldwin static void pgadjustjobc(struct pgrp *pgrp, int entering); 7802e878d9SJohn Baldwin static void pgdelete(struct pgrp *); 79b23f72e9SBrian Feldman static int proc_ctor(void *mem, int size, void *arg, int flags); 80a136efe9SPeter Wemm static void proc_dtor(void *mem, int size, void *arg); 81b23f72e9SBrian Feldman static int proc_init(void *mem, int size, int flags); 82a136efe9SPeter Wemm static void proc_fini(void *mem, int size); 83a136efe9SPeter Wemm 84df8bae1dSRodney W. Grimes /* 85b75356e1SJeffrey Hsu * Other process lists 86b75356e1SJeffrey Hsu */ 87b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl; 88b75356e1SJeffrey Hsu u_long pidhash; 89b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl; 90b75356e1SJeffrey Hsu u_long pgrphash; 91b75356e1SJeffrey Hsu struct proclist allproc; 92b75356e1SJeffrey Hsu struct proclist zombproc; 931005a129SJohn Baldwin struct sx allproc_lock; 941005a129SJohn Baldwin struct sx proctree_lock; 95c6544064SJohn Baldwin struct mtx ppeers_lock; 96c897b813SJeff Roberson uma_zone_t proc_zone; 97c897b813SJeff Roberson uma_zone_t ithread_zone; 98b75356e1SJeffrey Hsu 99b9f009b0SPeter Wemm int kstack_pages = KSTACK_PAGES; 100b9f009b0SPeter Wemm SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, ""); 101b9f009b0SPeter Wemm 102a30d7c60SJake Burkholder CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 103a30d7c60SJake Burkholder 104b75356e1SJeffrey Hsu /* 105b75356e1SJeffrey Hsu * Initialize global process hashing structures. 106df8bae1dSRodney W. Grimes */ 10726f9a767SRodney W. Grimes void 108b75356e1SJeffrey Hsu procinit() 109df8bae1dSRodney W. Grimes { 110df8bae1dSRodney W. Grimes 1111005a129SJohn Baldwin sx_init(&allproc_lock, "allproc"); 1121005a129SJohn Baldwin sx_init(&proctree_lock, "proctree"); 113c6544064SJohn Baldwin mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 114b75356e1SJeffrey Hsu LIST_INIT(&allproc); 115b75356e1SJeffrey Hsu LIST_INIT(&zombproc); 116b75356e1SJeffrey Hsu pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 117b75356e1SJeffrey Hsu pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 118de028f5aSJeff Roberson proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 119a136efe9SPeter Wemm proc_ctor, proc_dtor, proc_init, proc_fini, 120a136efe9SPeter Wemm UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 121f535380cSDon Lewis uihashinit(); 122df8bae1dSRodney W. Grimes } 123df8bae1dSRodney W. Grimes 124df8bae1dSRodney W. Grimes /* 125a136efe9SPeter Wemm * Prepare a proc for use. 126a136efe9SPeter Wemm */ 127b23f72e9SBrian Feldman static int 128b23f72e9SBrian Feldman proc_ctor(void *mem, int size, void *arg, int flags) 129a136efe9SPeter Wemm { 130a136efe9SPeter Wemm struct proc *p; 131a136efe9SPeter Wemm 132a136efe9SPeter Wemm p = (struct proc *)mem; 133b23f72e9SBrian Feldman return (0); 134a136efe9SPeter Wemm } 135a136efe9SPeter Wemm 136a136efe9SPeter Wemm /* 137a136efe9SPeter Wemm * Reclaim a proc after use. 138a136efe9SPeter Wemm */ 139a136efe9SPeter Wemm static void 140a136efe9SPeter Wemm proc_dtor(void *mem, int size, void *arg) 141a136efe9SPeter Wemm { 142a136efe9SPeter Wemm struct proc *p; 1431faf202eSJulian Elischer struct thread *td; 144a136efe9SPeter Wemm 1451faf202eSJulian Elischer /* INVARIANTS checks go here */ 146a136efe9SPeter Wemm p = (struct proc *)mem; 147ed062c8dSJulian Elischer td = FIRST_THREAD_IN_PROC(p); 148ed062c8dSJulian Elischer #ifdef INVARIANTS 1491faf202eSJulian Elischer KASSERT((p->p_numthreads == 1), 1501faf202eSJulian Elischer ("bad number of threads in exiting process")); 1511faf202eSJulian Elischer KASSERT((td != NULL), ("proc_dtor: bad thread pointer")); 1522c255e9dSRobert Watson KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 153ed062c8dSJulian Elischer #endif 154316ec49aSScott Long 155316ec49aSScott Long /* Dispose of an alternate kstack, if it exists. 156316ec49aSScott Long * XXX What if there are more than one thread in the proc? 157316ec49aSScott Long * The first thread in the proc is special and not 158316ec49aSScott Long * freed, so you gotta do this here. 159316ec49aSScott Long */ 160316ec49aSScott Long if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0)) 16189f4fca2SAlan Cox vm_thread_dispose_altkstack(td); 162ebceaf6dSDavid Xu if (p->p_ksi != NULL) 163ebceaf6dSDavid Xu KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 164a136efe9SPeter Wemm } 165a136efe9SPeter Wemm 166a136efe9SPeter Wemm /* 167a136efe9SPeter Wemm * Initialize type-stable parts of a proc (when newly created). 168a136efe9SPeter Wemm */ 169b23f72e9SBrian Feldman static int 170b23f72e9SBrian Feldman proc_init(void *mem, int size, int flags) 171a136efe9SPeter Wemm { 172a136efe9SPeter Wemm struct proc *p; 1731faf202eSJulian Elischer struct thread *td; 174a136efe9SPeter Wemm 175a136efe9SPeter Wemm p = (struct proc *)mem; 176de028f5aSJeff Roberson p->p_sched = (struct p_sched *)&p[1]; 1771faf202eSJulian Elischer td = thread_alloc(); 1787d447c95SJohn Baldwin bzero(&p->p_mtx, sizeof(struct mtx)); 1797d447c95SJohn Baldwin mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 1808b059651SDavid Schultz p->p_stats = pstats_alloc(); 1818460a577SJohn Birrell proc_linkup(p, td); 182ad1e7d28SJulian Elischer sched_newproc(p, td); 183b23f72e9SBrian Feldman return (0); 184a136efe9SPeter Wemm } 185a136efe9SPeter Wemm 186a136efe9SPeter Wemm /* 1878daa8c60SDavid Schultz * UMA should ensure that this function is never called. 1888daa8c60SDavid Schultz * Freeing a proc structure would violate type stability. 189a136efe9SPeter Wemm */ 190a136efe9SPeter Wemm static void 191a136efe9SPeter Wemm proc_fini(void *mem, int size) 192a136efe9SPeter Wemm { 193f55ab994SJohn Baldwin #ifdef notnow 194f55ab994SJohn Baldwin struct proc *p; 195a136efe9SPeter Wemm 196f55ab994SJohn Baldwin p = (struct proc *)mem; 197f55ab994SJohn Baldwin pstats_free(p->p_stats); 198f55ab994SJohn Baldwin thread_free(FIRST_THREAD_IN_PROC(p)); 199f55ab994SJohn Baldwin mtx_destroy(&p->p_mtx); 200ebceaf6dSDavid Xu if (p->p_ksi != NULL) 201ebceaf6dSDavid Xu ksiginfo_free(p->p_ksi); 202f55ab994SJohn Baldwin #else 2038daa8c60SDavid Schultz panic("proc reclaimed"); 204f55ab994SJohn Baldwin #endif 205a136efe9SPeter Wemm } 206a136efe9SPeter Wemm 207a136efe9SPeter Wemm /* 208df8bae1dSRodney W. Grimes * Is p an inferior of the current process? 209df8bae1dSRodney W. Grimes */ 2101a432a2fSDima Ruban int 211df8bae1dSRodney W. Grimes inferior(p) 212df8bae1dSRodney W. Grimes register struct proc *p; 213df8bae1dSRodney W. Grimes { 214df8bae1dSRodney W. Grimes 2155b29d6e9SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 2165b29d6e9SJohn Baldwin for (; p != curproc; p = p->p_pptr) 21700f13cb3SJohn Baldwin if (p->p_pid == 0) 21800f13cb3SJohn Baldwin return (0); 21900f13cb3SJohn Baldwin return (1); 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes 222df8bae1dSRodney W. Grimes /* 2236cbea71cSRobert Watson * Locate a process by number; return only "live" processes -- i.e., neither 2246cbea71cSRobert Watson * zombies nor newly born but incompletely initialized processes. By not 2256cbea71cSRobert Watson * returning processes in the PRS_NEW state, we allow callers to avoid 2266cbea71cSRobert Watson * testing for that condition to avoid dereferencing p_ucred, et al. 227df8bae1dSRodney W. Grimes */ 228df8bae1dSRodney W. Grimes struct proc * 229df8bae1dSRodney W. Grimes pfind(pid) 230df8bae1dSRodney W. Grimes register pid_t pid; 231df8bae1dSRodney W. Grimes { 232df8bae1dSRodney W. Grimes register struct proc *p; 233df8bae1dSRodney W. Grimes 2341005a129SJohn Baldwin sx_slock(&allproc_lock); 2351b727751SPoul-Henning Kamp LIST_FOREACH(p, PIDHASH(pid), p_hash) 23633a9ed9dSJohn Baldwin if (p->p_pid == pid) { 2376cbea71cSRobert Watson if (p->p_state == PRS_NEW) { 2386cbea71cSRobert Watson p = NULL; 2396cbea71cSRobert Watson break; 2406cbea71cSRobert Watson } 24133a9ed9dSJohn Baldwin PROC_LOCK(p); 242553629ebSJake Burkholder break; 24333a9ed9dSJohn Baldwin } 24498ab1489SJeffrey Hsu sx_sunlock(&allproc_lock); 245df8bae1dSRodney W. Grimes return (p); 246df8bae1dSRodney W. Grimes } 247df8bae1dSRodney W. Grimes 248df8bae1dSRodney W. Grimes /* 249f591779bSSeigo Tanimura * Locate a process group by number. 250f089b570SJohn Baldwin * The caller must hold proctree_lock. 251df8bae1dSRodney W. Grimes */ 252df8bae1dSRodney W. Grimes struct pgrp * 253df8bae1dSRodney W. Grimes pgfind(pgid) 254df8bae1dSRodney W. Grimes register pid_t pgid; 255df8bae1dSRodney W. Grimes { 256df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 257df8bae1dSRodney W. Grimes 258f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 259f591779bSSeigo Tanimura 260f591779bSSeigo Tanimura LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 261f591779bSSeigo Tanimura if (pgrp->pg_id == pgid) { 262f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 263df8bae1dSRodney W. Grimes return (pgrp); 264f591779bSSeigo Tanimura } 265f591779bSSeigo Tanimura } 266df8bae1dSRodney W. Grimes return (NULL); 267df8bae1dSRodney W. Grimes } 268df8bae1dSRodney W. Grimes 269df8bae1dSRodney W. Grimes /* 270f591779bSSeigo Tanimura * Create a new process group. 271f591779bSSeigo Tanimura * pgid must be equal to the pid of p. 272f591779bSSeigo Tanimura * Begin a new session if required. 273df8bae1dSRodney W. Grimes */ 27426f9a767SRodney W. Grimes int 275f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess) 276df8bae1dSRodney W. Grimes register struct proc *p; 277df8bae1dSRodney W. Grimes pid_t pgid; 278f591779bSSeigo Tanimura struct pgrp *pgrp; 279f591779bSSeigo Tanimura struct session *sess; 280df8bae1dSRodney W. Grimes { 281f591779bSSeigo Tanimura struct pgrp *pgrp2; 282df8bae1dSRodney W. Grimes 283f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 284f591779bSSeigo Tanimura 285f591779bSSeigo Tanimura KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 286f591779bSSeigo Tanimura KASSERT(p->p_pid == pgid, 287f591779bSSeigo Tanimura ("enterpgrp: new pgrp and pid != pgid")); 288f591779bSSeigo Tanimura 289f591779bSSeigo Tanimura pgrp2 = pgfind(pgid); 290f591779bSSeigo Tanimura 291f591779bSSeigo Tanimura KASSERT(pgrp2 == NULL, 292f591779bSSeigo Tanimura ("enterpgrp: pgrp with pgid exists")); 2935526d2d9SEivind Eklund KASSERT(!SESS_LEADER(p), 2945526d2d9SEivind Eklund ("enterpgrp: session leader attempted setpgrp")); 295219cbf59SEivind Eklund 2966008862bSJohn Baldwin mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 297df8bae1dSRodney W. Grimes 298f591779bSSeigo Tanimura if (sess != NULL) { 299df8bae1dSRodney W. Grimes /* 300df8bae1dSRodney W. Grimes * new session 301df8bae1dSRodney W. Grimes */ 3026008862bSJohn Baldwin mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 3038be56372SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 304f591779bSSeigo Tanimura PROC_LOCK(p); 305f591779bSSeigo Tanimura p->p_flag &= ~P_CONTROLT; 306f591779bSSeigo Tanimura PROC_UNLOCK(p); 307f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 308df8bae1dSRodney W. Grimes sess->s_leader = p; 309643a8daaSDon Lewis sess->s_sid = p->p_pid; 310df8bae1dSRodney W. Grimes sess->s_count = 1; 311df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 312df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 313df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 314df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 315df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 3165526d2d9SEivind Eklund KASSERT(p == curproc, 3175526d2d9SEivind Eklund ("enterpgrp: mksession and p != curproc")); 318df8bae1dSRodney W. Grimes } else { 31945e68191SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 320df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 321f591779bSSeigo Tanimura SESS_LOCK(pgrp->pg_session); 322df8bae1dSRodney W. Grimes pgrp->pg_session->s_count++; 323f591779bSSeigo Tanimura SESS_UNLOCK(pgrp->pg_session); 324f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 325df8bae1dSRodney W. Grimes } 326df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 327b75356e1SJeffrey Hsu LIST_INIT(&pgrp->pg_members); 328f591779bSSeigo Tanimura 329f591779bSSeigo Tanimura /* 330f089b570SJohn Baldwin * As we have an exclusive lock of proctree_lock, 331f591779bSSeigo Tanimura * this should not deadlock. 332f591779bSSeigo Tanimura */ 333b75356e1SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 334df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 335831d27a9SDon Lewis SLIST_INIT(&pgrp->pg_sigiolst); 336f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 33745e68191SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 338f591779bSSeigo Tanimura 339f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 340f591779bSSeigo Tanimura 341df8bae1dSRodney W. Grimes return (0); 342f591779bSSeigo Tanimura } 343f591779bSSeigo Tanimura 344f591779bSSeigo Tanimura /* 345f591779bSSeigo Tanimura * Move p to an existing process group 346f591779bSSeigo Tanimura */ 347f591779bSSeigo Tanimura int 348f591779bSSeigo Tanimura enterthispgrp(p, pgrp) 349f591779bSSeigo Tanimura register struct proc *p; 350f591779bSSeigo Tanimura struct pgrp *pgrp; 351f591779bSSeigo Tanimura { 352f089b570SJohn Baldwin 353f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 354f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 355f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 356f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 357f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 358f591779bSSeigo Tanimura KASSERT(pgrp->pg_session == p->p_session, 359f591779bSSeigo Tanimura ("%s: pgrp's session %p, p->p_session %p.\n", 360f591779bSSeigo Tanimura __func__, 361f591779bSSeigo Tanimura pgrp->pg_session, 362f591779bSSeigo Tanimura p->p_session)); 363f591779bSSeigo Tanimura KASSERT(pgrp != p->p_pgrp, 364f591779bSSeigo Tanimura ("%s: p belongs to pgrp.", __func__)); 365f591779bSSeigo Tanimura 366f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 367f591779bSSeigo Tanimura 368f591779bSSeigo Tanimura return (0); 369f591779bSSeigo Tanimura } 370f591779bSSeigo Tanimura 371f591779bSSeigo Tanimura /* 372f591779bSSeigo Tanimura * Move p to a process group 373f591779bSSeigo Tanimura */ 374f591779bSSeigo Tanimura static void 375f591779bSSeigo Tanimura doenterpgrp(p, pgrp) 376f591779bSSeigo Tanimura struct proc *p; 377f591779bSSeigo Tanimura struct pgrp *pgrp; 378f591779bSSeigo Tanimura { 379f591779bSSeigo Tanimura struct pgrp *savepgrp; 380f591779bSSeigo Tanimura 381f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 382f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 383f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 384f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 385f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 386f591779bSSeigo Tanimura 387f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes /* 390df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 391df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 392df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 393df8bae1dSRodney W. Grimes */ 394df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 395df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 396df8bae1dSRodney W. Grimes 397d7b167b5SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 398f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 399f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 40015e9ec51SJohn Baldwin PROC_LOCK(p); 401b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 402df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 40315e9ec51SJohn Baldwin PROC_UNLOCK(p); 404f591779bSSeigo Tanimura LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 405f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 406f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 407d7b167b5SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 408f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 409f591779bSSeigo Tanimura pgdelete(savepgrp); 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes 412df8bae1dSRodney W. Grimes /* 413df8bae1dSRodney W. Grimes * remove process from process group 414df8bae1dSRodney W. Grimes */ 41526f9a767SRodney W. Grimes int 416df8bae1dSRodney W. Grimes leavepgrp(p) 417df8bae1dSRodney W. Grimes register struct proc *p; 418df8bae1dSRodney W. Grimes { 419f591779bSSeigo Tanimura struct pgrp *savepgrp; 420df8bae1dSRodney W. Grimes 421f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 422f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 423d7b167b5SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 424f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 42515e9ec51SJohn Baldwin PROC_LOCK(p); 426b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 42715e9ec51SJohn Baldwin p->p_pgrp = NULL; 42815e9ec51SJohn Baldwin PROC_UNLOCK(p); 429f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 430d7b167b5SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 431f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 432f591779bSSeigo Tanimura pgdelete(savepgrp); 433df8bae1dSRodney W. Grimes return (0); 434df8bae1dSRodney W. Grimes } 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes /* 437df8bae1dSRodney W. Grimes * delete a process group 438df8bae1dSRodney W. Grimes */ 43987b6de2bSPoul-Henning Kamp static void 440df8bae1dSRodney W. Grimes pgdelete(pgrp) 441df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 442df8bae1dSRodney W. Grimes { 443f591779bSSeigo Tanimura struct session *savesess; 444f591779bSSeigo Tanimura 445f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 446f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 447f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 448f591779bSSeigo Tanimura 449831d27a9SDon Lewis /* 450831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 451831d27a9SDon Lewis * F_SETOWN with our pgid. 452831d27a9SDon Lewis */ 453831d27a9SDon Lewis funsetownlst(&pgrp->pg_sigiolst); 454831d27a9SDon Lewis 455d7b167b5SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 456e649887bSAlfred Perlstein PGRP_LOCK(pgrp); 457df8bae1dSRodney W. Grimes if (pgrp->pg_session->s_ttyp != NULL && 458df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 459df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp = NULL; 460b75356e1SJeffrey Hsu LIST_REMOVE(pgrp, pg_hash); 461f591779bSSeigo Tanimura savesess = pgrp->pg_session; 462572b4402SPoul-Henning Kamp SESSRELE(savesess); 463f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 4646041fa0aSSeigo Tanimura mtx_destroy(&pgrp->pg_mtx); 465df8bae1dSRodney W. Grimes FREE(pgrp, M_PGRP); 466d7b167b5SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes 46902e878d9SJohn Baldwin static void 47002e878d9SJohn Baldwin pgadjustjobc(pgrp, entering) 47102e878d9SJohn Baldwin struct pgrp *pgrp; 47202e878d9SJohn Baldwin int entering; 47302e878d9SJohn Baldwin { 47402e878d9SJohn Baldwin 47502e878d9SJohn Baldwin PGRP_LOCK(pgrp); 47602e878d9SJohn Baldwin if (entering) 47702e878d9SJohn Baldwin pgrp->pg_jobc++; 47802e878d9SJohn Baldwin else { 47902e878d9SJohn Baldwin --pgrp->pg_jobc; 48002e878d9SJohn Baldwin if (pgrp->pg_jobc == 0) 48102e878d9SJohn Baldwin orphanpg(pgrp); 48202e878d9SJohn Baldwin } 48302e878d9SJohn Baldwin PGRP_UNLOCK(pgrp); 48402e878d9SJohn Baldwin } 48502e878d9SJohn Baldwin 486df8bae1dSRodney W. Grimes /* 487df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 488df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 489df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 490df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 491df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 492df8bae1dSRodney W. Grimes * process group and that of its children. 493df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 494df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 495df8bae1dSRodney W. Grimes */ 49626f9a767SRodney W. Grimes void 497df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 498df8bae1dSRodney W. Grimes register struct proc *p; 499df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 500df8bae1dSRodney W. Grimes int entering; 501df8bae1dSRodney W. Grimes { 502df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 503f591779bSSeigo Tanimura register struct session *mysession; 504f591779bSSeigo Tanimura 505f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 506f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 507f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 508f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 509df8bae1dSRodney W. Grimes 510df8bae1dSRodney W. Grimes /* 511df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 512df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 513df8bae1dSRodney W. Grimes */ 514f591779bSSeigo Tanimura mysession = pgrp->pg_session; 515df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 51602e878d9SJohn Baldwin hispgrp->pg_session == mysession) 51702e878d9SJohn Baldwin pgadjustjobc(pgrp, entering); 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes /* 520df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 521df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 522df8bae1dSRodney W. Grimes * process groups. 523df8bae1dSRodney W. Grimes */ 524f591779bSSeigo Tanimura LIST_FOREACH(p, &p->p_children, p_sibling) { 52502e878d9SJohn Baldwin hispgrp = p->p_pgrp; 52602e878d9SJohn Baldwin if (hispgrp == pgrp || 52702e878d9SJohn Baldwin hispgrp->pg_session != mysession) 52802e878d9SJohn Baldwin continue; 52902e878d9SJohn Baldwin PROC_LOCK(p); 53002e878d9SJohn Baldwin if (p->p_state == PRS_ZOMBIE) { 53102e878d9SJohn Baldwin PROC_UNLOCK(p); 53202e878d9SJohn Baldwin continue; 533df8bae1dSRodney W. Grimes } 53402e878d9SJohn Baldwin PROC_UNLOCK(p); 53502e878d9SJohn Baldwin pgadjustjobc(hispgrp, entering); 536f591779bSSeigo Tanimura } 537dfd5dee1SPeter Wemm } 538df8bae1dSRodney W. Grimes 539df8bae1dSRodney W. Grimes /* 540df8bae1dSRodney W. Grimes * A process group has become orphaned; 541df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 542df8bae1dSRodney W. Grimes * hang-up all process in that group. 543df8bae1dSRodney W. Grimes */ 544df8bae1dSRodney W. Grimes static void 545df8bae1dSRodney W. Grimes orphanpg(pg) 546df8bae1dSRodney W. Grimes struct pgrp *pg; 547df8bae1dSRodney W. Grimes { 548df8bae1dSRodney W. Grimes register struct proc *p; 549df8bae1dSRodney W. Grimes 550f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pg, MA_OWNED); 551f591779bSSeigo Tanimura 5521b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 55302e878d9SJohn Baldwin PROC_LOCK(p); 554e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 55502e878d9SJohn Baldwin PROC_UNLOCK(p); 5561b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 55715e9ec51SJohn Baldwin PROC_LOCK(p); 558df8bae1dSRodney W. Grimes psignal(p, SIGHUP); 559df8bae1dSRodney W. Grimes psignal(p, SIGCONT); 56015e9ec51SJohn Baldwin PROC_UNLOCK(p); 561df8bae1dSRodney W. Grimes } 562df8bae1dSRodney W. Grimes return; 563df8bae1dSRodney W. Grimes } 56402e878d9SJohn Baldwin PROC_UNLOCK(p); 565df8bae1dSRodney W. Grimes } 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes 568572b4402SPoul-Henning Kamp void 569572b4402SPoul-Henning Kamp sessrele(struct session *s) 570572b4402SPoul-Henning Kamp { 571572b4402SPoul-Henning Kamp int i; 572572b4402SPoul-Henning Kamp 573572b4402SPoul-Henning Kamp SESS_LOCK(s); 574572b4402SPoul-Henning Kamp i = --s->s_count; 575572b4402SPoul-Henning Kamp SESS_UNLOCK(s); 576572b4402SPoul-Henning Kamp if (i == 0) { 577572b4402SPoul-Henning Kamp if (s->s_ttyp != NULL) 578572b4402SPoul-Henning Kamp ttyrel(s->s_ttyp); 579572b4402SPoul-Henning Kamp mtx_destroy(&s->s_mtx); 580572b4402SPoul-Henning Kamp FREE(s, M_SESSION); 581572b4402SPoul-Henning Kamp } 582572b4402SPoul-Henning Kamp } 583572b4402SPoul-Henning Kamp 584831031ceSBruce Evans #include "opt_ddb.h" 585831031ceSBruce Evans #ifdef DDB 586831031ceSBruce Evans #include <ddb/ddb.h> 587831031ceSBruce Evans 588831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump) 589df8bae1dSRodney W. Grimes { 590df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 591df8bae1dSRodney W. Grimes register struct proc *p; 592876a94eeSBruce Evans register int i; 593df8bae1dSRodney W. Grimes 594b75356e1SJeffrey Hsu for (i = 0; i <= pgrphash; i++) { 5951b727751SPoul-Henning Kamp if (!LIST_EMPTY(&pgrphashtbl[i])) { 596df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 5971b727751SPoul-Henning Kamp LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 598ac1e407bSBruce Evans printf( 599ac1e407bSBruce Evans "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 600ac1e407bSBruce Evans (void *)pgrp, (long)pgrp->pg_id, 601ac1e407bSBruce Evans (void *)pgrp->pg_session, 602b75356e1SJeffrey Hsu pgrp->pg_session->s_count, 6031b727751SPoul-Henning Kamp (void *)LIST_FIRST(&pgrp->pg_members)); 6041b727751SPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 605ac1e407bSBruce Evans printf("\t\tpid %ld addr %p pgrp %p\n", 606ac1e407bSBruce Evans (long)p->p_pid, (void *)p, 607ac1e407bSBruce Evans (void *)p->p_pgrp); 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes } 613831031ceSBruce Evans #endif /* DDB */ 614972f9b20SPoul-Henning Kamp 615972f9b20SPoul-Henning Kamp /* 6165032ff81SDon Lewis * Clear kinfo_proc and fill in any information that is common 6175032ff81SDon Lewis * to all threads in the process. 61865c9b430SJohn Baldwin * Must be called with the target process locked. 619972f9b20SPoul-Henning Kamp */ 6205032ff81SDon Lewis static void 6215032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 622972f9b20SPoul-Henning Kamp { 6234093529dSJeff Roberson struct thread *td0; 6241f7d2501SKirk McKusick struct tty *tp; 6251f7d2501SKirk McKusick struct session *sp; 626d079d0a0SPawel Jakub Dawidek struct ucred *cred; 62790af4afaSJohn Baldwin struct sigacts *ps; 628972f9b20SPoul-Henning Kamp 6291f7d2501SKirk McKusick bzero(kp, sizeof(*kp)); 630972f9b20SPoul-Henning Kamp 6311f7d2501SKirk McKusick kp->ki_structsize = sizeof(*kp); 6321f7d2501SKirk McKusick kp->ki_paddr = p; 63365c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 634b40ce416SJulian Elischer kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 6351f7d2501SKirk McKusick kp->ki_args = p->p_args; 6361f7d2501SKirk McKusick kp->ki_textvp = p->p_textvp; 6376c84de02SJohn Baldwin #ifdef KTRACE 638a5881ea5SJohn Baldwin kp->ki_tracep = p->p_tracevp; 6396c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 6406c84de02SJohn Baldwin kp->ki_traceflag = p->p_traceflag; 6416c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 6426c84de02SJohn Baldwin #endif 6431f7d2501SKirk McKusick kp->ki_fd = p->p_fd; 6441f7d2501SKirk McKusick kp->ki_vmspace = p->p_vmspace; 645cefcecbeSPawel Jakub Dawidek kp->ki_flag = p->p_flag; 646d079d0a0SPawel Jakub Dawidek cred = p->p_ucred; 647d079d0a0SPawel Jakub Dawidek if (cred) { 648d079d0a0SPawel Jakub Dawidek kp->ki_uid = cred->cr_uid; 649d079d0a0SPawel Jakub Dawidek kp->ki_ruid = cred->cr_ruid; 650d079d0a0SPawel Jakub Dawidek kp->ki_svuid = cred->cr_svuid; 6510ecd57adSPeter Wemm /* XXX bde doesn't like KI_NGROUPS */ 652d079d0a0SPawel Jakub Dawidek kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS); 653d079d0a0SPawel Jakub Dawidek bcopy(cred->cr_groups, kp->ki_groups, 6540ecd57adSPeter Wemm kp->ki_ngroups * sizeof(gid_t)); 655d079d0a0SPawel Jakub Dawidek kp->ki_rgid = cred->cr_rgid; 656d079d0a0SPawel Jakub Dawidek kp->ki_svgid = cred->cr_svgid; 657cefcecbeSPawel Jakub Dawidek /* If jailed(cred), emulate the old P_JAILED flag. */ 658c78941e6SPawel Jakub Dawidek if (jailed(cred)) { 659cefcecbeSPawel Jakub Dawidek kp->ki_flag |= P_JAILED; 660c78941e6SPawel Jakub Dawidek /* If inside a jail, use 0 as a jail ID. */ 661c78941e6SPawel Jakub Dawidek if (!jailed(curthread->td_ucred)) 662c78941e6SPawel Jakub Dawidek kp->ki_jid = cred->cr_prison->pr_id; 663c78941e6SPawel Jakub Dawidek } 664972f9b20SPoul-Henning Kamp } 66590af4afaSJohn Baldwin ps = p->p_sigacts; 666d079d0a0SPawel Jakub Dawidek if (ps) { 66790af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 66890af4afaSJohn Baldwin kp->ki_sigignore = ps->ps_sigignore; 66990af4afaSJohn Baldwin kp->ki_sigcatch = ps->ps_sigcatch; 67090af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 671d8c85307SJulian Elischer } 6729ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 673e602ba25SJulian Elischer if (p->p_state != PRS_NEW && 674e602ba25SJulian Elischer p->p_state != PRS_ZOMBIE && 675e602ba25SJulian Elischer p->p_vmspace != NULL) { 6761f7d2501SKirk McKusick struct vmspace *vm = p->p_vmspace; 677cd73303cSDavid Greenman 6781f7d2501SKirk McKusick kp->ki_size = vm->vm_map.size; 6791f7d2501SKirk McKusick kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 680ceff7f2aSTim J. Robbins FOREACH_THREAD_IN_PROC(p, td0) { 681ceff7f2aSTim J. Robbins if (!TD_IS_SWAPPED(td0)) 682ceff7f2aSTim J. Robbins kp->ki_rssize += td0->td_kstack_pages; 683ceff7f2aSTim J. Robbins if (td0->td_altkstack_obj != NULL) 684913fc94dSTim J. Robbins kp->ki_rssize += td0->td_altkstack_pages; 685ceff7f2aSTim J. Robbins } 6861f7d2501SKirk McKusick kp->ki_swrss = vm->vm_swrss; 6871f7d2501SKirk McKusick kp->ki_tsize = vm->vm_tsize; 6881f7d2501SKirk McKusick kp->ki_dsize = vm->vm_dsize; 6891f7d2501SKirk McKusick kp->ki_ssize = vm->vm_ssize; 6905032ff81SDon Lewis } else if (p->p_state == PRS_ZOMBIE) 6915032ff81SDon Lewis kp->ki_stat = SZOMB; 692cebabef0SPawel Jakub Dawidek kp->ki_sflag = p->p_sflag; 693cebabef0SPawel Jakub Dawidek kp->ki_swtime = p->p_swtime; 694cebabef0SPawel Jakub Dawidek kp->ki_pid = p->p_pid; 695cebabef0SPawel Jakub Dawidek kp->ki_nice = p->p_nice; 696e8444a7eSPoul-Henning Kamp kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 6979ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 69878c85e8dSJohn Baldwin if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) { 69978c85e8dSJohn Baldwin kp->ki_start = p->p_stats->p_start; 70078c85e8dSJohn Baldwin timevaladd(&kp->ki_start, &boottime); 70178c85e8dSJohn Baldwin kp->ki_rusage = p->p_stats->p_ru; 70278c85e8dSJohn Baldwin calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 70378c85e8dSJohn Baldwin calccru(p, &kp->ki_childutime, &kp->ki_childstime); 70478c85e8dSJohn Baldwin 70578c85e8dSJohn Baldwin /* Some callers want child-times in a single value */ 70678c85e8dSJohn Baldwin kp->ki_childtime = kp->ki_childstime; 70778c85e8dSJohn Baldwin timevaladd(&kp->ki_childtime, &kp->ki_childutime); 70878c85e8dSJohn Baldwin } 709f591779bSSeigo Tanimura tp = NULL; 7101f7d2501SKirk McKusick if (p->p_pgrp) { 7111f7d2501SKirk McKusick kp->ki_pgid = p->p_pgrp->pg_id; 7121f7d2501SKirk McKusick kp->ki_jobc = p->p_pgrp->pg_jobc; 7131f7d2501SKirk McKusick sp = p->p_pgrp->pg_session; 7141f7d2501SKirk McKusick 7151f7d2501SKirk McKusick if (sp != NULL) { 7161f7d2501SKirk McKusick kp->ki_sid = sp->s_sid; 717f591779bSSeigo Tanimura SESS_LOCK(sp); 718e80fb434SRobert Drehmel strlcpy(kp->ki_login, sp->s_login, 719e80fb434SRobert Drehmel sizeof(kp->ki_login)); 7201f7d2501SKirk McKusick if (sp->s_ttyvp) 721b8e6bf1eSJohn Baldwin kp->ki_kiflag |= KI_CTTY; 7221f7d2501SKirk McKusick if (SESS_LEADER(p)) 7231f7d2501SKirk McKusick kp->ki_kiflag |= KI_SLEADER; 724f591779bSSeigo Tanimura tp = sp->s_ttyp; 725f591779bSSeigo Tanimura SESS_UNLOCK(sp); 726cd73303cSDavid Greenman } 727cd73303cSDavid Greenman } 728f591779bSSeigo Tanimura if ((p->p_flag & P_CONTROLT) && tp != NULL) { 7291f7d2501SKirk McKusick kp->ki_tdev = dev2udev(tp->t_dev); 7301f7d2501SKirk McKusick kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 7311f7d2501SKirk McKusick if (tp->t_session) 7321f7d2501SKirk McKusick kp->ki_tsid = tp->t_session->s_sid; 733972f9b20SPoul-Henning Kamp } else 734f3732fd1SPoul-Henning Kamp kp->ki_tdev = NODEV; 73513b762a3SEd Maste if (p->p_comm[0] != '\0') 736e80fb434SRobert Drehmel strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 737078842c5SGarance A Drosehn if (p->p_sysent && p->p_sysent->sv_name != NULL && 738078842c5SGarance A Drosehn p->p_sysent->sv_name[0] != '\0') 739078842c5SGarance A Drosehn strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 7401d9c5696SJuli Mallett kp->ki_siglist = p->p_siglist; 7411f7d2501SKirk McKusick kp->ki_xstat = p->p_xstat; 7421f7d2501SKirk McKusick kp->ki_acflag = p->p_acflag; 7431f7d2501SKirk McKusick kp->ki_lock = p->p_lock; 74442a4ed99SJohn Baldwin if (p->p_pptr) 74542a4ed99SJohn Baldwin kp->ki_ppid = p->p_pptr->p_pid; 746972f9b20SPoul-Henning Kamp } 747972f9b20SPoul-Henning Kamp 7485032ff81SDon Lewis /* 7495032ff81SDon Lewis * Fill in information that is thread specific. 7505032ff81SDon Lewis * Must be called with sched_lock locked. 7515032ff81SDon Lewis */ 7525032ff81SDon Lewis static void 7535032ff81SDon Lewis fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp) 7545032ff81SDon Lewis { 7555032ff81SDon Lewis struct proc *p; 7565032ff81SDon Lewis 7575032ff81SDon Lewis p = td->td_proc; 7585032ff81SDon Lewis 7595032ff81SDon Lewis if (td->td_wmesg != NULL) 7605032ff81SDon Lewis strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 7615032ff81SDon Lewis else 7625032ff81SDon Lewis bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 76311f4763dSJulian Elischer if (td->td_name[0] != '\0') 76411f4763dSJulian Elischer strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 7655032ff81SDon Lewis if (TD_ON_LOCK(td)) { 7665032ff81SDon Lewis kp->ki_kiflag |= KI_LOCKBLOCK; 7675032ff81SDon Lewis strlcpy(kp->ki_lockname, td->td_lockname, 7685032ff81SDon Lewis sizeof(kp->ki_lockname)); 7695032ff81SDon Lewis } else { 7705032ff81SDon Lewis kp->ki_kiflag &= ~KI_LOCKBLOCK; 7715032ff81SDon Lewis bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 7725032ff81SDon Lewis } 7735032ff81SDon Lewis 7745032ff81SDon Lewis if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 7755032ff81SDon Lewis if (TD_ON_RUNQ(td) || 7765032ff81SDon Lewis TD_CAN_RUN(td) || 7775032ff81SDon Lewis TD_IS_RUNNING(td)) { 7785032ff81SDon Lewis kp->ki_stat = SRUN; 7795032ff81SDon Lewis } else if (P_SHOULDSTOP(p)) { 7805032ff81SDon Lewis kp->ki_stat = SSTOP; 7815032ff81SDon Lewis } else if (TD_IS_SLEEPING(td)) { 7825032ff81SDon Lewis kp->ki_stat = SSLEEP; 7835032ff81SDon Lewis } else if (TD_ON_LOCK(td)) { 7845032ff81SDon Lewis kp->ki_stat = SLOCK; 7855032ff81SDon Lewis } else { 7865032ff81SDon Lewis kp->ki_stat = SWAIT; 7875032ff81SDon Lewis } 7883357835aSDavid Xu } else if (p->p_state == PRS_ZOMBIE) { 7893357835aSDavid Xu kp->ki_stat = SZOMB; 7905032ff81SDon Lewis } else { 7915032ff81SDon Lewis kp->ki_stat = SIDL; 7925032ff81SDon Lewis } 7935032ff81SDon Lewis 7945032ff81SDon Lewis /* Things in the thread */ 7955032ff81SDon Lewis kp->ki_wchan = td->td_wchan; 7965032ff81SDon Lewis kp->ki_pri.pri_level = td->td_priority; 7975032ff81SDon Lewis kp->ki_pri.pri_native = td->td_base_pri; 7985032ff81SDon Lewis kp->ki_lastcpu = td->td_lastcpu; 7995032ff81SDon Lewis kp->ki_oncpu = td->td_oncpu; 8005032ff81SDon Lewis kp->ki_tdflags = td->td_flags; 8015032ff81SDon Lewis kp->ki_tid = td->td_tid; 8025032ff81SDon Lewis kp->ki_numthreads = p->p_numthreads; 8035032ff81SDon Lewis kp->ki_pcb = td->td_pcb; 8045032ff81SDon Lewis kp->ki_kstack = (void *)td->td_kstack; 8055032ff81SDon Lewis kp->ki_pctcpu = sched_pctcpu(td); 8068460a577SJohn Birrell kp->ki_estcpu = td->td_estcpu; 8078460a577SJohn Birrell kp->ki_slptime = td->td_slptime; 8088460a577SJohn Birrell kp->ki_pri.pri_class = td->td_pri_class; 8098460a577SJohn Birrell kp->ki_pri.pri_user = td->td_user_pri; 8105032ff81SDon Lewis 8115032ff81SDon Lewis /* We can't get this anymore but ps etc never used it anyway. */ 8125032ff81SDon Lewis kp->ki_rqindex = 0; 8135032ff81SDon Lewis 8145032ff81SDon Lewis SIGSETOR(kp->ki_siglist, td->td_siglist); 8155032ff81SDon Lewis kp->ki_sigmask = td->td_sigmask; 8165032ff81SDon Lewis } 8175032ff81SDon Lewis 8185032ff81SDon Lewis /* 8195032ff81SDon Lewis * Fill in a kinfo_proc structure for the specified process. 8205032ff81SDon Lewis * Must be called with the target process locked. 8215032ff81SDon Lewis */ 8225032ff81SDon Lewis void 8235032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 8245032ff81SDon Lewis { 8255032ff81SDon Lewis 8265032ff81SDon Lewis fill_kinfo_proc_only(p, kp); 8275032ff81SDon Lewis mtx_lock_spin(&sched_lock); 8285032ff81SDon Lewis if (FIRST_THREAD_IN_PROC(p) != NULL) 8295032ff81SDon Lewis fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp); 8305032ff81SDon Lewis mtx_unlock_spin(&sched_lock); 8315032ff81SDon Lewis } 8325032ff81SDon Lewis 8338b059651SDavid Schultz struct pstats * 8348b059651SDavid Schultz pstats_alloc(void) 8358b059651SDavid Schultz { 8368b059651SDavid Schultz 8378b059651SDavid Schultz return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 8388b059651SDavid Schultz } 8398b059651SDavid Schultz 8408b059651SDavid Schultz /* 8418b059651SDavid Schultz * Copy parts of p_stats; zero the rest of p_stats (statistics). 8428b059651SDavid Schultz */ 8438b059651SDavid Schultz void 8448b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst) 8458b059651SDavid Schultz { 8468b059651SDavid Schultz 8478b059651SDavid Schultz bzero(&dst->pstat_startzero, 8486db36923SDavid Schultz __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 8498b059651SDavid Schultz bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 8506db36923SDavid Schultz __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 8518b059651SDavid Schultz } 8528b059651SDavid Schultz 8538b059651SDavid Schultz void 8548b059651SDavid Schultz pstats_free(struct pstats *ps) 8558b059651SDavid Schultz { 8568b059651SDavid Schultz 8578b059651SDavid Schultz free(ps, M_SUBPROC); 8588b059651SDavid Schultz } 8598b059651SDavid Schultz 8608b059651SDavid Schultz /* 86142a4ed99SJohn Baldwin * Locate a zombie process by number 86242a4ed99SJohn Baldwin */ 86342a4ed99SJohn Baldwin struct proc * 8643ce93e4eSPoul-Henning Kamp zpfind(pid_t pid) 8653ce93e4eSPoul-Henning Kamp { 8663ce93e4eSPoul-Henning Kamp struct proc *p; 8673ce93e4eSPoul-Henning Kamp 8681005a129SJohn Baldwin sx_slock(&allproc_lock); 8691b727751SPoul-Henning Kamp LIST_FOREACH(p, &zombproc, p_list) 87033a9ed9dSJohn Baldwin if (p->p_pid == pid) { 87133a9ed9dSJohn Baldwin PROC_LOCK(p); 872c0c25570SJake Burkholder break; 87333a9ed9dSJohn Baldwin } 8741005a129SJohn Baldwin sx_sunlock(&allproc_lock); 8753ce93e4eSPoul-Henning Kamp return (p); 8763ce93e4eSPoul-Henning Kamp } 8773ce93e4eSPoul-Henning Kamp 87830c6f34eSScott Long #define KERN_PROC_ZOMBMASK 0x3 87930c6f34eSScott Long #define KERN_PROC_NOTHREADS 0x4 8803ce93e4eSPoul-Henning Kamp 88165c9b430SJohn Baldwin /* 88265c9b430SJohn Baldwin * Must be called with the process locked and will return with it unlocked. 88365c9b430SJohn Baldwin */ 8843ce93e4eSPoul-Henning Kamp static int 88530c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 8863ce93e4eSPoul-Henning Kamp { 88730c6f34eSScott Long struct thread *td; 8881f7d2501SKirk McKusick struct kinfo_proc kinfo_proc; 88930c6f34eSScott Long int error = 0; 89033a9ed9dSJohn Baldwin struct proc *np; 8913ce93e4eSPoul-Henning Kamp pid_t pid = p->p_pid; 8923ce93e4eSPoul-Henning Kamp 89365c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 89430c6f34eSScott Long 8955032ff81SDon Lewis fill_kinfo_proc_only(p, &kinfo_proc); 89630c6f34eSScott Long if (flags & KERN_PROC_NOTHREADS) { 8975032ff81SDon Lewis mtx_lock_spin(&sched_lock); 8985032ff81SDon Lewis if (FIRST_THREAD_IN_PROC(p) != NULL) 8995032ff81SDon Lewis fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc); 9005032ff81SDon Lewis mtx_unlock_spin(&sched_lock); 90130c6f34eSScott Long error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 90230c6f34eSScott Long sizeof(kinfo_proc)); 90330c6f34eSScott Long } else { 9045032ff81SDon Lewis mtx_lock_spin(&sched_lock); 9055032ff81SDon Lewis if (FIRST_THREAD_IN_PROC(p) != NULL) 90630c6f34eSScott Long FOREACH_THREAD_IN_PROC(p, td) { 90730c6f34eSScott Long fill_kinfo_thread(td, &kinfo_proc); 90830c6f34eSScott Long error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 90930c6f34eSScott Long sizeof(kinfo_proc)); 91030c6f34eSScott Long if (error) 91130c6f34eSScott Long break; 91230c6f34eSScott Long } 9135032ff81SDon Lewis else 9145032ff81SDon Lewis error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 9155032ff81SDon Lewis sizeof(kinfo_proc)); 9165032ff81SDon Lewis mtx_unlock_spin(&sched_lock); 91730c6f34eSScott Long } 91830c6f34eSScott Long PROC_UNLOCK(p); 9193ce93e4eSPoul-Henning Kamp if (error) 9203ce93e4eSPoul-Henning Kamp return (error); 92130c6f34eSScott Long if (flags & KERN_PROC_ZOMBMASK) 92233a9ed9dSJohn Baldwin np = zpfind(pid); 92333a9ed9dSJohn Baldwin else { 92433a9ed9dSJohn Baldwin if (pid == 0) 92533a9ed9dSJohn Baldwin return (0); 92633a9ed9dSJohn Baldwin np = pfind(pid); 92733a9ed9dSJohn Baldwin } 92833a9ed9dSJohn Baldwin if (np == NULL) 9293ce93e4eSPoul-Henning Kamp return EAGAIN; 93033a9ed9dSJohn Baldwin if (np != p) { 93133a9ed9dSJohn Baldwin PROC_UNLOCK(np); 9323ce93e4eSPoul-Henning Kamp return EAGAIN; 93333a9ed9dSJohn Baldwin } 93433a9ed9dSJohn Baldwin PROC_UNLOCK(np); 9353ce93e4eSPoul-Henning Kamp return (0); 9363ce93e4eSPoul-Henning Kamp } 9373ce93e4eSPoul-Henning Kamp 938972f9b20SPoul-Henning Kamp static int 93982d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 940972f9b20SPoul-Henning Kamp { 941972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 942972f9b20SPoul-Henning Kamp u_int namelen = arg2; 943972f9b20SPoul-Henning Kamp struct proc *p; 9442648efa6SDaniel Eischen int flags, doingzomb, oid_number; 945972f9b20SPoul-Henning Kamp int error = 0; 946972f9b20SPoul-Henning Kamp 9472648efa6SDaniel Eischen oid_number = oidp->oid_number; 9482648efa6SDaniel Eischen if (oid_number != KERN_PROC_ALL && 9492648efa6SDaniel Eischen (oid_number & KERN_PROC_INC_THREAD) == 0) 9502648efa6SDaniel Eischen flags = KERN_PROC_NOTHREADS; 9512648efa6SDaniel Eischen else { 9522648efa6SDaniel Eischen flags = 0; 9532648efa6SDaniel Eischen oid_number &= ~KERN_PROC_INC_THREAD; 9542648efa6SDaniel Eischen } 9552648efa6SDaniel Eischen if (oid_number == KERN_PROC_PID) { 9563ce93e4eSPoul-Henning Kamp if (namelen != 1) 957972f9b20SPoul-Henning Kamp return (EINVAL); 9585032ff81SDon Lewis error = sysctl_wire_old_buffer(req, 0); 9595032ff81SDon Lewis if (error) 9605032ff81SDon Lewis return (error); 9613ce93e4eSPoul-Henning Kamp p = pfind((pid_t)name[0]); 9623ce93e4eSPoul-Henning Kamp if (!p) 963e76bad96SRobert Drehmel return (ESRCH); 964e76bad96SRobert Drehmel if ((error = p_cansee(curthread, p))) { 96533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 966e76bad96SRobert Drehmel return (error); 96733a9ed9dSJohn Baldwin } 9682648efa6SDaniel Eischen error = sysctl_out_proc(p, req, flags); 9693ce93e4eSPoul-Henning Kamp return (error); 9703ce93e4eSPoul-Henning Kamp } 9713ddaef40STim J. Robbins 9722648efa6SDaniel Eischen switch (oid_number) { 9733ddaef40STim J. Robbins case KERN_PROC_ALL: 9743ddaef40STim J. Robbins if (namelen != 0) 9753ce93e4eSPoul-Henning Kamp return (EINVAL); 9763ddaef40STim J. Robbins break; 97725e247afSPeter Wemm case KERN_PROC_PROC: 97825e247afSPeter Wemm if (namelen != 0 && namelen != 1) 97925e247afSPeter Wemm return (EINVAL); 98025e247afSPeter Wemm break; 9813ddaef40STim J. Robbins default: 9823ddaef40STim J. Robbins if (namelen != 1) 9833ddaef40STim J. Robbins return (EINVAL); 9843ddaef40STim J. Robbins break; 9853ddaef40STim J. Robbins } 9863ce93e4eSPoul-Henning Kamp 987972f9b20SPoul-Henning Kamp if (!req->oldptr) { 9883ce93e4eSPoul-Henning Kamp /* overestimate by 5 procs */ 989972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 990972f9b20SPoul-Henning Kamp if (error) 991972f9b20SPoul-Henning Kamp return (error); 992972f9b20SPoul-Henning Kamp } 99347934cefSDon Lewis error = sysctl_wire_old_buffer(req, 0); 99447934cefSDon Lewis if (error != 0) 99547934cefSDon Lewis return (error); 9961005a129SJohn Baldwin sx_slock(&allproc_lock); 9973ce93e4eSPoul-Henning Kamp for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 9983ce93e4eSPoul-Henning Kamp if (!doingzomb) 9991b727751SPoul-Henning Kamp p = LIST_FIRST(&allproc); 10003ce93e4eSPoul-Henning Kamp else 10011b727751SPoul-Henning Kamp p = LIST_FIRST(&zombproc); 10021b727751SPoul-Henning Kamp for (; p != 0; p = LIST_NEXT(p, p_list)) { 100302e878d9SJohn Baldwin /* 100402e878d9SJohn Baldwin * Skip embryonic processes. 100502e878d9SJohn Baldwin */ 100602e878d9SJohn Baldwin mtx_lock_spin(&sched_lock); 100702e878d9SJohn Baldwin if (p->p_state == PRS_NEW) { 100802e878d9SJohn Baldwin mtx_unlock_spin(&sched_lock); 100902e878d9SJohn Baldwin continue; 101002e878d9SJohn Baldwin } 101102e878d9SJohn Baldwin mtx_unlock_spin(&sched_lock); 101265c9b430SJohn Baldwin PROC_LOCK(p); 1013b241b0a2SJuli Mallett KASSERT(p->p_ucred != NULL, 1014b241b0a2SJuli Mallett ("process credential is NULL for non-NEW proc")); 1015972f9b20SPoul-Henning Kamp /* 1016387d2c03SRobert Watson * Show a user only appropriate processes. 101703f808c5SPaul Saab */ 1018f44d9e24SJohn Baldwin if (p_cansee(curthread, p)) { 101965c9b430SJohn Baldwin PROC_UNLOCK(p); 102003f808c5SPaul Saab continue; 102165c9b430SJohn Baldwin } 102203f808c5SPaul Saab /* 1023972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 1024972f9b20SPoul-Henning Kamp * do by session. 1025972f9b20SPoul-Henning Kamp */ 10262648efa6SDaniel Eischen switch (oid_number) { 1027972f9b20SPoul-Henning Kamp 1028078842c5SGarance A Drosehn case KERN_PROC_GID: 1029b241b0a2SJuli Mallett if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1030078842c5SGarance A Drosehn PROC_UNLOCK(p); 1031078842c5SGarance A Drosehn continue; 1032078842c5SGarance A Drosehn } 1033078842c5SGarance A Drosehn break; 1034078842c5SGarance A Drosehn 1035972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 1036972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 10373ce93e4eSPoul-Henning Kamp if (p->p_pgrp == NULL || 1038f591779bSSeigo Tanimura p->p_pgrp->pg_id != (pid_t)name[0]) { 1039f591779bSSeigo Tanimura PROC_UNLOCK(p); 1040972f9b20SPoul-Henning Kamp continue; 1041f591779bSSeigo Tanimura } 1042972f9b20SPoul-Henning Kamp break; 1043972f9b20SPoul-Henning Kamp 1044b8fdc89dSGarance A Drosehn case KERN_PROC_RGID: 1045b241b0a2SJuli Mallett if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1046b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1047b8fdc89dSGarance A Drosehn continue; 1048b8fdc89dSGarance A Drosehn } 1049b8fdc89dSGarance A Drosehn break; 1050b8fdc89dSGarance A Drosehn 1051b8fdc89dSGarance A Drosehn case KERN_PROC_SESSION: 1052b8fdc89dSGarance A Drosehn if (p->p_session == NULL || 1053b8fdc89dSGarance A Drosehn p->p_session->s_sid != (pid_t)name[0]) { 1054b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1055b8fdc89dSGarance A Drosehn continue; 1056b8fdc89dSGarance A Drosehn } 1057b8fdc89dSGarance A Drosehn break; 1058b8fdc89dSGarance A Drosehn 1059972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 1060972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 1061f591779bSSeigo Tanimura p->p_session == NULL) { 1062f591779bSSeigo Tanimura PROC_UNLOCK(p); 1063972f9b20SPoul-Henning Kamp continue; 1064f591779bSSeigo Tanimura } 1065f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 1066f591779bSSeigo Tanimura if (p->p_session->s_ttyp == NULL || 1067f591779bSSeigo Tanimura dev2udev(p->p_session->s_ttyp->t_dev) != 1068f3732fd1SPoul-Henning Kamp (dev_t)name[0]) { 1069f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1070f591779bSSeigo Tanimura PROC_UNLOCK(p); 1071f591779bSSeigo Tanimura continue; 1072f591779bSSeigo Tanimura } 1073f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1074972f9b20SPoul-Henning Kamp break; 1075972f9b20SPoul-Henning Kamp 1076972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 1077b241b0a2SJuli Mallett if (p->p_ucred->cr_uid != (uid_t)name[0]) { 107865c9b430SJohn Baldwin PROC_UNLOCK(p); 1079972f9b20SPoul-Henning Kamp continue; 108065c9b430SJohn Baldwin } 1081972f9b20SPoul-Henning Kamp break; 1082972f9b20SPoul-Henning Kamp 1083972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 1084b241b0a2SJuli Mallett if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 108565c9b430SJohn Baldwin PROC_UNLOCK(p); 1086972f9b20SPoul-Henning Kamp continue; 108765c9b430SJohn Baldwin } 1088972f9b20SPoul-Henning Kamp break; 108930c6f34eSScott Long 109030c6f34eSScott Long case KERN_PROC_PROC: 109130c6f34eSScott Long break; 109230c6f34eSScott Long 109330c6f34eSScott Long default: 109430c6f34eSScott Long break; 109530c6f34eSScott Long 1096972f9b20SPoul-Henning Kamp } 1097972f9b20SPoul-Henning Kamp 109830c6f34eSScott Long error = sysctl_out_proc(p, req, flags | doingzomb); 1099553629ebSJake Burkholder if (error) { 11001005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1101972f9b20SPoul-Henning Kamp return (error); 1102972f9b20SPoul-Henning Kamp } 1103972f9b20SPoul-Henning Kamp } 1104553629ebSJake Burkholder } 11051005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1106972f9b20SPoul-Henning Kamp return (0); 1107972f9b20SPoul-Henning Kamp } 1108972f9b20SPoul-Henning Kamp 1109c1508b28SAlfred Perlstein struct pargs * 1110c1508b28SAlfred Perlstein pargs_alloc(int len) 1111c1508b28SAlfred Perlstein { 1112c1508b28SAlfred Perlstein struct pargs *pa; 1113c1508b28SAlfred Perlstein 1114c1508b28SAlfred Perlstein MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1115a163d034SWarner Losh M_WAITOK); 111655b4a5aeSJohn Baldwin refcount_init(&pa->ar_ref, 1); 1117c1508b28SAlfred Perlstein pa->ar_length = len; 1118c1508b28SAlfred Perlstein return (pa); 1119c1508b28SAlfred Perlstein } 1120c1508b28SAlfred Perlstein 1121c1508b28SAlfred Perlstein void 1122c1508b28SAlfred Perlstein pargs_free(struct pargs *pa) 1123c1508b28SAlfred Perlstein { 1124c1508b28SAlfred Perlstein 1125c1508b28SAlfred Perlstein FREE(pa, M_PARGS); 1126c1508b28SAlfred Perlstein } 1127c1508b28SAlfred Perlstein 1128c1508b28SAlfred Perlstein void 1129c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa) 1130c1508b28SAlfred Perlstein { 1131c1508b28SAlfred Perlstein 1132c1508b28SAlfred Perlstein if (pa == NULL) 1133c1508b28SAlfred Perlstein return; 113455b4a5aeSJohn Baldwin refcount_acquire(&pa->ar_ref); 1135c1508b28SAlfred Perlstein } 1136c1508b28SAlfred Perlstein 1137c1508b28SAlfred Perlstein void 1138c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa) 1139c1508b28SAlfred Perlstein { 1140c1508b28SAlfred Perlstein 1141c1508b28SAlfred Perlstein if (pa == NULL) 1142c1508b28SAlfred Perlstein return; 114355b4a5aeSJohn Baldwin if (refcount_release(&pa->ar_ref)) 1144c1508b28SAlfred Perlstein pargs_free(pa); 1145c1508b28SAlfred Perlstein } 1146c1508b28SAlfred Perlstein 1147b9df5231SPoul-Henning Kamp /* 1148b9df5231SPoul-Henning Kamp * This sysctl allows a process to retrieve the argument list or process 1149b9df5231SPoul-Henning Kamp * title for another process without groping around in the address space 1150b9df5231SPoul-Henning Kamp * of the other process. It also allow a process to set its own "process 1151b9df5231SPoul-Henning Kamp * title to a string of its own choice. 1152b9df5231SPoul-Henning Kamp */ 1153b9df5231SPoul-Henning Kamp static int 115482d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1155b9df5231SPoul-Henning Kamp { 1156b9df5231SPoul-Henning Kamp int *name = (int*) arg1; 1157b9df5231SPoul-Henning Kamp u_int namelen = arg2; 11588510f2a8SJohn Baldwin struct pargs *newpa, *pa; 1159b9df5231SPoul-Henning Kamp struct proc *p; 1160b9df5231SPoul-Henning Kamp int error = 0; 1161b9df5231SPoul-Henning Kamp 1162b9df5231SPoul-Henning Kamp if (namelen != 1) 1163b9df5231SPoul-Henning Kamp return (EINVAL); 1164b9df5231SPoul-Henning Kamp 1165b9df5231SPoul-Henning Kamp p = pfind((pid_t)name[0]); 1166b9df5231SPoul-Henning Kamp if (!p) 1167e76bad96SRobert Drehmel return (ESRCH); 1168b9df5231SPoul-Henning Kamp 11699cdb6216SPawel Jakub Dawidek if ((error = p_cansee(curthread, p)) != 0) { 117033a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1171e76bad96SRobert Drehmel return (error); 117233a9ed9dSJohn Baldwin } 11734bc6471bSJohn Baldwin 11744bc6471bSJohn Baldwin if (req->newptr && curproc != p) { 117533a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1176b9df5231SPoul-Henning Kamp return (EPERM); 11774bc6471bSJohn Baldwin } 1178b9df5231SPoul-Henning Kamp 11797b11fea6SAlfred Perlstein pa = p->p_args; 11807b11fea6SAlfred Perlstein pargs_hold(pa); 11817b11fea6SAlfred Perlstein PROC_UNLOCK(p); 11828510f2a8SJohn Baldwin if (req->oldptr != NULL && pa != NULL) 11837b11fea6SAlfred Perlstein error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 11847b11fea6SAlfred Perlstein pargs_drop(pa); 11858510f2a8SJohn Baldwin if (error != 0 || req->newptr == NULL) 1186b9df5231SPoul-Henning Kamp return (error); 1187b9df5231SPoul-Henning Kamp 1188b9df5231SPoul-Henning Kamp if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 11898510f2a8SJohn Baldwin return (ENOMEM); 11904bc6471bSJohn Baldwin newpa = pargs_alloc(req->newlen); 11914bc6471bSJohn Baldwin error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 11928510f2a8SJohn Baldwin if (error != 0) { 11934bc6471bSJohn Baldwin pargs_free(newpa); 1194b9df5231SPoul-Henning Kamp return (error); 1195b9df5231SPoul-Henning Kamp } 11964bc6471bSJohn Baldwin PROC_LOCK(p); 11974bc6471bSJohn Baldwin pa = p->p_args; 11984bc6471bSJohn Baldwin p->p_args = newpa; 11994bc6471bSJohn Baldwin PROC_UNLOCK(p); 12004bc6471bSJohn Baldwin pargs_drop(pa); 12014bc6471bSJohn Baldwin return (0); 12024bc6471bSJohn Baldwin } 12034bc6471bSJohn Baldwin 1204fe769cddSDavid Schultz /* 1205fe769cddSDavid Schultz * This sysctl allows a process to retrieve the path of the executable for 1206fe769cddSDavid Schultz * itself or another process. 1207fe769cddSDavid Schultz */ 1208fe769cddSDavid Schultz static int 1209fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1210fe769cddSDavid Schultz { 1211fe769cddSDavid Schultz pid_t *pidp = (pid_t *)arg1; 1212fe769cddSDavid Schultz unsigned int arglen = arg2; 1213fe769cddSDavid Schultz struct proc *p; 1214fe769cddSDavid Schultz struct vnode *vp; 1215fe769cddSDavid Schultz char *retbuf, *freebuf; 1216fe769cddSDavid Schultz int error; 1217fe769cddSDavid Schultz 1218fe769cddSDavid Schultz if (arglen != 1) 1219fe769cddSDavid Schultz return (EINVAL); 1220fe769cddSDavid Schultz if (*pidp == -1) { /* -1 means this process */ 1221fe769cddSDavid Schultz p = req->td->td_proc; 1222fe769cddSDavid Schultz } else { 1223fe769cddSDavid Schultz p = pfind(*pidp); 1224fe769cddSDavid Schultz if (p == NULL) 1225fe769cddSDavid Schultz return (ESRCH); 1226fe769cddSDavid Schultz if ((error = p_cansee(curthread, p)) != 0) { 1227fe769cddSDavid Schultz PROC_UNLOCK(p); 1228fe769cddSDavid Schultz return (error); 1229fe769cddSDavid Schultz } 1230fe769cddSDavid Schultz } 1231fe769cddSDavid Schultz 1232fe769cddSDavid Schultz vp = p->p_textvp; 1233fe769cddSDavid Schultz vref(vp); 1234fe769cddSDavid Schultz if (*pidp != -1) 1235fe769cddSDavid Schultz PROC_UNLOCK(p); 1236fe769cddSDavid Schultz error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1237fe769cddSDavid Schultz vrele(vp); 1238fe769cddSDavid Schultz if (error) 1239fe769cddSDavid Schultz return (error); 1240fe769cddSDavid Schultz error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1241fe769cddSDavid Schultz free(freebuf, M_TEMP); 1242fe769cddSDavid Schultz return (error); 1243fe769cddSDavid Schultz } 1244fe769cddSDavid Schultz 1245baf731e6SRobert Drehmel static int 1246baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1247baf731e6SRobert Drehmel { 1248baf731e6SRobert Drehmel struct proc *p; 1249baf731e6SRobert Drehmel char *sv_name; 1250baf731e6SRobert Drehmel int *name; 1251baf731e6SRobert Drehmel int namelen; 1252e76bad96SRobert Drehmel int error; 1253baf731e6SRobert Drehmel 1254baf731e6SRobert Drehmel namelen = arg2; 1255baf731e6SRobert Drehmel if (namelen != 1) 1256baf731e6SRobert Drehmel return (EINVAL); 1257baf731e6SRobert Drehmel 1258baf731e6SRobert Drehmel name = (int *)arg1; 1259baf731e6SRobert Drehmel if ((p = pfind((pid_t)name[0])) == NULL) 1260e76bad96SRobert Drehmel return (ESRCH); 1261e76bad96SRobert Drehmel if ((error = p_cansee(curthread, p))) { 1262baf731e6SRobert Drehmel PROC_UNLOCK(p); 1263e76bad96SRobert Drehmel return (error); 1264baf731e6SRobert Drehmel } 1265baf731e6SRobert Drehmel sv_name = p->p_sysent->sv_name; 1266baf731e6SRobert Drehmel PROC_UNLOCK(p); 1267baf731e6SRobert Drehmel return (sysctl_handle_string(oidp, sv_name, 0, req)); 1268baf731e6SRobert Drehmel } 1269baf731e6SRobert Drehmel 1270baf731e6SRobert Drehmel 12710c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 12723ce93e4eSPoul-Henning Kamp 12733ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 12743d177f46SBill Fumerola 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 12753ce93e4eSPoul-Henning Kamp 12760c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD, 1277078842c5SGarance A Drosehn sysctl_kern_proc, "Process table"); 1278078842c5SGarance A Drosehn 12790c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 12803ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12813ce93e4eSPoul-Henning Kamp 12820c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD, 1283b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 1284b8fdc89dSGarance A Drosehn 12850c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD, 1286b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 1287b8fdc89dSGarance A Drosehn 12880c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 12893ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12903ce93e4eSPoul-Henning Kamp 12910c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 12923ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12933ce93e4eSPoul-Henning Kamp 12940c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 12953ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12963ce93e4eSPoul-Henning Kamp 12970c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1298972f9b20SPoul-Henning Kamp sysctl_kern_proc, "Process table"); 1299b9df5231SPoul-Henning Kamp 13000c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD, 130130c6f34eSScott Long sysctl_kern_proc, "Return process table, no threads"); 130230c6f34eSScott Long 13030c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 13040c898376SPoul-Henning Kamp CTLFLAG_RW | CTLFLAG_ANYBODY, 13059b6d9dbaSPoul-Henning Kamp sysctl_kern_proc_args, "Process argument list"); 1306baf731e6SRobert Drehmel 1307fe769cddSDavid Schultz static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD, 1308fe769cddSDavid Schultz sysctl_kern_proc_pathname, "Process executable path"); 1309fe769cddSDavid Schultz 13100c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD, 1311baf731e6SRobert Drehmel sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); 13122648efa6SDaniel Eischen 13130c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 1314078842c5SGarance A Drosehn CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1315078842c5SGarance A Drosehn 13160c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 13172648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13182648efa6SDaniel Eischen 13190c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 1320b8fdc89dSGarance A Drosehn CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1321b8fdc89dSGarance A Drosehn 13220c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 13230c898376SPoul-Henning Kamp sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13240c898376SPoul-Henning Kamp 13250c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 1326b8fdc89dSGarance A Drosehn CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1327b8fdc89dSGarance A Drosehn 13280c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 13292648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13302648efa6SDaniel Eischen 13310c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 13322648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13332648efa6SDaniel Eischen 13340c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 13352648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13362648efa6SDaniel Eischen 13370c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 13382648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads"); 1339