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); 180982d11f8SJeff Roberson mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 1818b059651SDavid Schultz p->p_stats = pstats_alloc(); 1828460a577SJohn Birrell proc_linkup(p, td); 183ad1e7d28SJulian Elischer sched_newproc(p, td); 184b23f72e9SBrian Feldman return (0); 185a136efe9SPeter Wemm } 186a136efe9SPeter Wemm 187a136efe9SPeter Wemm /* 1888daa8c60SDavid Schultz * UMA should ensure that this function is never called. 1898daa8c60SDavid Schultz * Freeing a proc structure would violate type stability. 190a136efe9SPeter Wemm */ 191a136efe9SPeter Wemm static void 192a136efe9SPeter Wemm proc_fini(void *mem, int size) 193a136efe9SPeter Wemm { 194f55ab994SJohn Baldwin #ifdef notnow 195f55ab994SJohn Baldwin struct proc *p; 196a136efe9SPeter Wemm 197f55ab994SJohn Baldwin p = (struct proc *)mem; 198f55ab994SJohn Baldwin pstats_free(p->p_stats); 199f55ab994SJohn Baldwin thread_free(FIRST_THREAD_IN_PROC(p)); 200f55ab994SJohn Baldwin mtx_destroy(&p->p_mtx); 201ebceaf6dSDavid Xu if (p->p_ksi != NULL) 202ebceaf6dSDavid Xu ksiginfo_free(p->p_ksi); 203f55ab994SJohn Baldwin #else 2048daa8c60SDavid Schultz panic("proc reclaimed"); 205f55ab994SJohn Baldwin #endif 206a136efe9SPeter Wemm } 207a136efe9SPeter Wemm 208a136efe9SPeter Wemm /* 209df8bae1dSRodney W. Grimes * Is p an inferior of the current process? 210df8bae1dSRodney W. Grimes */ 2111a432a2fSDima Ruban int 212df8bae1dSRodney W. Grimes inferior(p) 213df8bae1dSRodney W. Grimes register struct proc *p; 214df8bae1dSRodney W. Grimes { 215df8bae1dSRodney W. Grimes 2165b29d6e9SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 2175b29d6e9SJohn Baldwin for (; p != curproc; p = p->p_pptr) 21800f13cb3SJohn Baldwin if (p->p_pid == 0) 21900f13cb3SJohn Baldwin return (0); 22000f13cb3SJohn Baldwin return (1); 221df8bae1dSRodney W. Grimes } 222df8bae1dSRodney W. Grimes 223df8bae1dSRodney W. Grimes /* 2246cbea71cSRobert Watson * Locate a process by number; return only "live" processes -- i.e., neither 2256cbea71cSRobert Watson * zombies nor newly born but incompletely initialized processes. By not 2266cbea71cSRobert Watson * returning processes in the PRS_NEW state, we allow callers to avoid 2276cbea71cSRobert Watson * testing for that condition to avoid dereferencing p_ucred, et al. 228df8bae1dSRodney W. Grimes */ 229df8bae1dSRodney W. Grimes struct proc * 230df8bae1dSRodney W. Grimes pfind(pid) 231df8bae1dSRodney W. Grimes register pid_t pid; 232df8bae1dSRodney W. Grimes { 233df8bae1dSRodney W. Grimes register struct proc *p; 234df8bae1dSRodney W. Grimes 2351005a129SJohn Baldwin sx_slock(&allproc_lock); 2361b727751SPoul-Henning Kamp LIST_FOREACH(p, PIDHASH(pid), p_hash) 23733a9ed9dSJohn Baldwin if (p->p_pid == pid) { 2386cbea71cSRobert Watson if (p->p_state == PRS_NEW) { 2396cbea71cSRobert Watson p = NULL; 2406cbea71cSRobert Watson break; 2416cbea71cSRobert Watson } 24233a9ed9dSJohn Baldwin PROC_LOCK(p); 243553629ebSJake Burkholder break; 24433a9ed9dSJohn Baldwin } 24598ab1489SJeffrey Hsu sx_sunlock(&allproc_lock); 246df8bae1dSRodney W. Grimes return (p); 247df8bae1dSRodney W. Grimes } 248df8bae1dSRodney W. Grimes 249df8bae1dSRodney W. Grimes /* 250f591779bSSeigo Tanimura * Locate a process group by number. 251f089b570SJohn Baldwin * The caller must hold proctree_lock. 252df8bae1dSRodney W. Grimes */ 253df8bae1dSRodney W. Grimes struct pgrp * 254df8bae1dSRodney W. Grimes pgfind(pgid) 255df8bae1dSRodney W. Grimes register pid_t pgid; 256df8bae1dSRodney W. Grimes { 257df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 258df8bae1dSRodney W. Grimes 259f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 260f591779bSSeigo Tanimura 261f591779bSSeigo Tanimura LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 262f591779bSSeigo Tanimura if (pgrp->pg_id == pgid) { 263f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 264df8bae1dSRodney W. Grimes return (pgrp); 265f591779bSSeigo Tanimura } 266f591779bSSeigo Tanimura } 267df8bae1dSRodney W. Grimes return (NULL); 268df8bae1dSRodney W. Grimes } 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes /* 271f591779bSSeigo Tanimura * Create a new process group. 272f591779bSSeigo Tanimura * pgid must be equal to the pid of p. 273f591779bSSeigo Tanimura * Begin a new session if required. 274df8bae1dSRodney W. Grimes */ 27526f9a767SRodney W. Grimes int 276f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess) 277df8bae1dSRodney W. Grimes register struct proc *p; 278df8bae1dSRodney W. Grimes pid_t pgid; 279f591779bSSeigo Tanimura struct pgrp *pgrp; 280f591779bSSeigo Tanimura struct session *sess; 281df8bae1dSRodney W. Grimes { 282f591779bSSeigo Tanimura struct pgrp *pgrp2; 283df8bae1dSRodney W. Grimes 284f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 285f591779bSSeigo Tanimura 286f591779bSSeigo Tanimura KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 287f591779bSSeigo Tanimura KASSERT(p->p_pid == pgid, 288f591779bSSeigo Tanimura ("enterpgrp: new pgrp and pid != pgid")); 289f591779bSSeigo Tanimura 290f591779bSSeigo Tanimura pgrp2 = pgfind(pgid); 291f591779bSSeigo Tanimura 292f591779bSSeigo Tanimura KASSERT(pgrp2 == NULL, 293f591779bSSeigo Tanimura ("enterpgrp: pgrp with pgid exists")); 2945526d2d9SEivind Eklund KASSERT(!SESS_LEADER(p), 2955526d2d9SEivind Eklund ("enterpgrp: session leader attempted setpgrp")); 296219cbf59SEivind Eklund 2976008862bSJohn Baldwin mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 298df8bae1dSRodney W. Grimes 299f591779bSSeigo Tanimura if (sess != NULL) { 300df8bae1dSRodney W. Grimes /* 301df8bae1dSRodney W. Grimes * new session 302df8bae1dSRodney W. Grimes */ 3036008862bSJohn Baldwin mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 3048be56372SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 305f591779bSSeigo Tanimura PROC_LOCK(p); 306f591779bSSeigo Tanimura p->p_flag &= ~P_CONTROLT; 307f591779bSSeigo Tanimura PROC_UNLOCK(p); 308f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 309df8bae1dSRodney W. Grimes sess->s_leader = p; 310643a8daaSDon Lewis sess->s_sid = p->p_pid; 311df8bae1dSRodney W. Grimes sess->s_count = 1; 312df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 313df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 314df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 315df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 316df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 3175526d2d9SEivind Eklund KASSERT(p == curproc, 3185526d2d9SEivind Eklund ("enterpgrp: mksession and p != curproc")); 319df8bae1dSRodney W. Grimes } else { 32045e68191SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 321df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 322f591779bSSeigo Tanimura SESS_LOCK(pgrp->pg_session); 323df8bae1dSRodney W. Grimes pgrp->pg_session->s_count++; 324f591779bSSeigo Tanimura SESS_UNLOCK(pgrp->pg_session); 325f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 328b75356e1SJeffrey Hsu LIST_INIT(&pgrp->pg_members); 329f591779bSSeigo Tanimura 330f591779bSSeigo Tanimura /* 331f089b570SJohn Baldwin * As we have an exclusive lock of proctree_lock, 332f591779bSSeigo Tanimura * this should not deadlock. 333f591779bSSeigo Tanimura */ 334b75356e1SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 335df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 336831d27a9SDon Lewis SLIST_INIT(&pgrp->pg_sigiolst); 337f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 33845e68191SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 339f591779bSSeigo Tanimura 340f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 341f591779bSSeigo Tanimura 342df8bae1dSRodney W. Grimes return (0); 343f591779bSSeigo Tanimura } 344f591779bSSeigo Tanimura 345f591779bSSeigo Tanimura /* 346f591779bSSeigo Tanimura * Move p to an existing process group 347f591779bSSeigo Tanimura */ 348f591779bSSeigo Tanimura int 349f591779bSSeigo Tanimura enterthispgrp(p, pgrp) 350f591779bSSeigo Tanimura register struct proc *p; 351f591779bSSeigo Tanimura struct pgrp *pgrp; 352f591779bSSeigo Tanimura { 353f089b570SJohn Baldwin 354f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 355f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 356f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 357f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 358f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 359f591779bSSeigo Tanimura KASSERT(pgrp->pg_session == p->p_session, 360f591779bSSeigo Tanimura ("%s: pgrp's session %p, p->p_session %p.\n", 361f591779bSSeigo Tanimura __func__, 362f591779bSSeigo Tanimura pgrp->pg_session, 363f591779bSSeigo Tanimura p->p_session)); 364f591779bSSeigo Tanimura KASSERT(pgrp != p->p_pgrp, 365f591779bSSeigo Tanimura ("%s: p belongs to pgrp.", __func__)); 366f591779bSSeigo Tanimura 367f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 368f591779bSSeigo Tanimura 369f591779bSSeigo Tanimura return (0); 370f591779bSSeigo Tanimura } 371f591779bSSeigo Tanimura 372f591779bSSeigo Tanimura /* 373f591779bSSeigo Tanimura * Move p to a process group 374f591779bSSeigo Tanimura */ 375f591779bSSeigo Tanimura static void 376f591779bSSeigo Tanimura doenterpgrp(p, pgrp) 377f591779bSSeigo Tanimura struct proc *p; 378f591779bSSeigo Tanimura struct pgrp *pgrp; 379f591779bSSeigo Tanimura { 380f591779bSSeigo Tanimura struct pgrp *savepgrp; 381f591779bSSeigo Tanimura 382f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 383f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 384f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 385f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 386f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 387f591779bSSeigo Tanimura 388f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 389df8bae1dSRodney W. Grimes 390df8bae1dSRodney W. Grimes /* 391df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 392df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 393df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 394df8bae1dSRodney W. Grimes */ 395df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 396df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 397df8bae1dSRodney W. Grimes 398d7b167b5SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 399f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 400f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 40115e9ec51SJohn Baldwin PROC_LOCK(p); 402b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 403df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 40415e9ec51SJohn Baldwin PROC_UNLOCK(p); 405f591779bSSeigo Tanimura LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 406f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 407f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 408d7b167b5SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 409f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 410f591779bSSeigo Tanimura pgdelete(savepgrp); 411df8bae1dSRodney W. Grimes } 412df8bae1dSRodney W. Grimes 413df8bae1dSRodney W. Grimes /* 414df8bae1dSRodney W. Grimes * remove process from process group 415df8bae1dSRodney W. Grimes */ 41626f9a767SRodney W. Grimes int 417df8bae1dSRodney W. Grimes leavepgrp(p) 418df8bae1dSRodney W. Grimes register struct proc *p; 419df8bae1dSRodney W. Grimes { 420f591779bSSeigo Tanimura struct pgrp *savepgrp; 421df8bae1dSRodney W. Grimes 422f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 423f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 424d7b167b5SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 425f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 42615e9ec51SJohn Baldwin PROC_LOCK(p); 427b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 42815e9ec51SJohn Baldwin p->p_pgrp = NULL; 42915e9ec51SJohn Baldwin PROC_UNLOCK(p); 430f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 431d7b167b5SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 432f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 433f591779bSSeigo Tanimura pgdelete(savepgrp); 434df8bae1dSRodney W. Grimes return (0); 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes 437df8bae1dSRodney W. Grimes /* 438df8bae1dSRodney W. Grimes * delete a process group 439df8bae1dSRodney W. Grimes */ 44087b6de2bSPoul-Henning Kamp static void 441df8bae1dSRodney W. Grimes pgdelete(pgrp) 442df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 443df8bae1dSRodney W. Grimes { 444f591779bSSeigo Tanimura struct session *savesess; 445f591779bSSeigo Tanimura 446f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 447f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 448f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 449f591779bSSeigo Tanimura 450831d27a9SDon Lewis /* 451831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 452831d27a9SDon Lewis * F_SETOWN with our pgid. 453831d27a9SDon Lewis */ 454831d27a9SDon Lewis funsetownlst(&pgrp->pg_sigiolst); 455831d27a9SDon Lewis 456d7b167b5SMartin Blapp mtx_lock(&Giant); /* XXX TTY */ 457e649887bSAlfred Perlstein PGRP_LOCK(pgrp); 458df8bae1dSRodney W. Grimes if (pgrp->pg_session->s_ttyp != NULL && 459df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 460df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp = NULL; 461b75356e1SJeffrey Hsu LIST_REMOVE(pgrp, pg_hash); 462f591779bSSeigo Tanimura savesess = pgrp->pg_session; 463572b4402SPoul-Henning Kamp SESSRELE(savesess); 464f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 4656041fa0aSSeigo Tanimura mtx_destroy(&pgrp->pg_mtx); 466df8bae1dSRodney W. Grimes FREE(pgrp, M_PGRP); 467d7b167b5SMartin Blapp mtx_unlock(&Giant); /* XXX TTY */ 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes 47002e878d9SJohn Baldwin static void 47102e878d9SJohn Baldwin pgadjustjobc(pgrp, entering) 47202e878d9SJohn Baldwin struct pgrp *pgrp; 47302e878d9SJohn Baldwin int entering; 47402e878d9SJohn Baldwin { 47502e878d9SJohn Baldwin 47602e878d9SJohn Baldwin PGRP_LOCK(pgrp); 47702e878d9SJohn Baldwin if (entering) 47802e878d9SJohn Baldwin pgrp->pg_jobc++; 47902e878d9SJohn Baldwin else { 48002e878d9SJohn Baldwin --pgrp->pg_jobc; 48102e878d9SJohn Baldwin if (pgrp->pg_jobc == 0) 48202e878d9SJohn Baldwin orphanpg(pgrp); 48302e878d9SJohn Baldwin } 48402e878d9SJohn Baldwin PGRP_UNLOCK(pgrp); 48502e878d9SJohn Baldwin } 48602e878d9SJohn Baldwin 487df8bae1dSRodney W. Grimes /* 488df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 489df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 490df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 491df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 492df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 493df8bae1dSRodney W. Grimes * process group and that of its children. 494df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 495df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 496df8bae1dSRodney W. Grimes */ 49726f9a767SRodney W. Grimes void 498df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 499df8bae1dSRodney W. Grimes register struct proc *p; 500df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 501df8bae1dSRodney W. Grimes int entering; 502df8bae1dSRodney W. Grimes { 503df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 504f591779bSSeigo Tanimura register struct session *mysession; 505f591779bSSeigo Tanimura 506f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 507f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 508f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 509f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes /* 512df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 513df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 514df8bae1dSRodney W. Grimes */ 515f591779bSSeigo Tanimura mysession = pgrp->pg_session; 516df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 51702e878d9SJohn Baldwin hispgrp->pg_session == mysession) 51802e878d9SJohn Baldwin pgadjustjobc(pgrp, entering); 519df8bae1dSRodney W. Grimes 520df8bae1dSRodney W. Grimes /* 521df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 522df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 523df8bae1dSRodney W. Grimes * process groups. 524df8bae1dSRodney W. Grimes */ 525f591779bSSeigo Tanimura LIST_FOREACH(p, &p->p_children, p_sibling) { 52602e878d9SJohn Baldwin hispgrp = p->p_pgrp; 52702e878d9SJohn Baldwin if (hispgrp == pgrp || 52802e878d9SJohn Baldwin hispgrp->pg_session != mysession) 52902e878d9SJohn Baldwin continue; 53002e878d9SJohn Baldwin PROC_LOCK(p); 53102e878d9SJohn Baldwin if (p->p_state == PRS_ZOMBIE) { 53202e878d9SJohn Baldwin PROC_UNLOCK(p); 53302e878d9SJohn Baldwin continue; 534df8bae1dSRodney W. Grimes } 53502e878d9SJohn Baldwin PROC_UNLOCK(p); 53602e878d9SJohn Baldwin pgadjustjobc(hispgrp, entering); 537f591779bSSeigo Tanimura } 538dfd5dee1SPeter Wemm } 539df8bae1dSRodney W. Grimes 540df8bae1dSRodney W. Grimes /* 541df8bae1dSRodney W. Grimes * A process group has become orphaned; 542df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 543df8bae1dSRodney W. Grimes * hang-up all process in that group. 544df8bae1dSRodney W. Grimes */ 545df8bae1dSRodney W. Grimes static void 546df8bae1dSRodney W. Grimes orphanpg(pg) 547df8bae1dSRodney W. Grimes struct pgrp *pg; 548df8bae1dSRodney W. Grimes { 549df8bae1dSRodney W. Grimes register struct proc *p; 550df8bae1dSRodney W. Grimes 551f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pg, MA_OWNED); 552f591779bSSeigo Tanimura 5531b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 55402e878d9SJohn Baldwin PROC_LOCK(p); 555e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 55602e878d9SJohn Baldwin PROC_UNLOCK(p); 5571b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 55815e9ec51SJohn Baldwin PROC_LOCK(p); 559df8bae1dSRodney W. Grimes psignal(p, SIGHUP); 560df8bae1dSRodney W. Grimes psignal(p, SIGCONT); 56115e9ec51SJohn Baldwin PROC_UNLOCK(p); 562df8bae1dSRodney W. Grimes } 563df8bae1dSRodney W. Grimes return; 564df8bae1dSRodney W. Grimes } 56502e878d9SJohn Baldwin PROC_UNLOCK(p); 566df8bae1dSRodney W. Grimes } 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes 569572b4402SPoul-Henning Kamp void 570572b4402SPoul-Henning Kamp sessrele(struct session *s) 571572b4402SPoul-Henning Kamp { 572572b4402SPoul-Henning Kamp int i; 573572b4402SPoul-Henning Kamp 574572b4402SPoul-Henning Kamp SESS_LOCK(s); 575572b4402SPoul-Henning Kamp i = --s->s_count; 576572b4402SPoul-Henning Kamp SESS_UNLOCK(s); 577572b4402SPoul-Henning Kamp if (i == 0) { 578572b4402SPoul-Henning Kamp if (s->s_ttyp != NULL) 579572b4402SPoul-Henning Kamp ttyrel(s->s_ttyp); 580572b4402SPoul-Henning Kamp mtx_destroy(&s->s_mtx); 581572b4402SPoul-Henning Kamp FREE(s, M_SESSION); 582572b4402SPoul-Henning Kamp } 583572b4402SPoul-Henning Kamp } 584572b4402SPoul-Henning Kamp 585831031ceSBruce Evans #include "opt_ddb.h" 586831031ceSBruce Evans #ifdef DDB 587831031ceSBruce Evans #include <ddb/ddb.h> 588831031ceSBruce Evans 589831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump) 590df8bae1dSRodney W. Grimes { 591df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 592df8bae1dSRodney W. Grimes register struct proc *p; 593876a94eeSBruce Evans register int i; 594df8bae1dSRodney W. Grimes 595b75356e1SJeffrey Hsu for (i = 0; i <= pgrphash; i++) { 5961b727751SPoul-Henning Kamp if (!LIST_EMPTY(&pgrphashtbl[i])) { 597df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 5981b727751SPoul-Henning Kamp LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 599ac1e407bSBruce Evans printf( 600ac1e407bSBruce Evans "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 601ac1e407bSBruce Evans (void *)pgrp, (long)pgrp->pg_id, 602ac1e407bSBruce Evans (void *)pgrp->pg_session, 603b75356e1SJeffrey Hsu pgrp->pg_session->s_count, 6041b727751SPoul-Henning Kamp (void *)LIST_FIRST(&pgrp->pg_members)); 6051b727751SPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 606ac1e407bSBruce Evans printf("\t\tpid %ld addr %p pgrp %p\n", 607ac1e407bSBruce Evans (long)p->p_pid, (void *)p, 608ac1e407bSBruce Evans (void *)p->p_pgrp); 609df8bae1dSRodney W. Grimes } 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes } 614831031ceSBruce Evans #endif /* DDB */ 615972f9b20SPoul-Henning Kamp 616972f9b20SPoul-Henning Kamp /* 6175032ff81SDon Lewis * Clear kinfo_proc and fill in any information that is common 6185032ff81SDon Lewis * to all threads in the process. 61965c9b430SJohn Baldwin * Must be called with the target process locked. 620972f9b20SPoul-Henning Kamp */ 6215032ff81SDon Lewis static void 6225032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 623972f9b20SPoul-Henning Kamp { 6244093529dSJeff Roberson struct thread *td0; 6251f7d2501SKirk McKusick struct tty *tp; 6261f7d2501SKirk McKusick struct session *sp; 627d079d0a0SPawel Jakub Dawidek struct ucred *cred; 62890af4afaSJohn Baldwin struct sigacts *ps; 629972f9b20SPoul-Henning Kamp 6301f7d2501SKirk McKusick bzero(kp, sizeof(*kp)); 631972f9b20SPoul-Henning Kamp 6321f7d2501SKirk McKusick kp->ki_structsize = sizeof(*kp); 6331f7d2501SKirk McKusick kp->ki_paddr = p; 63465c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 635b40ce416SJulian Elischer kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */ 6361f7d2501SKirk McKusick kp->ki_args = p->p_args; 6371f7d2501SKirk McKusick kp->ki_textvp = p->p_textvp; 6386c84de02SJohn Baldwin #ifdef KTRACE 639a5881ea5SJohn Baldwin kp->ki_tracep = p->p_tracevp; 6406c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 6416c84de02SJohn Baldwin kp->ki_traceflag = p->p_traceflag; 6426c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 6436c84de02SJohn Baldwin #endif 6441f7d2501SKirk McKusick kp->ki_fd = p->p_fd; 6451f7d2501SKirk McKusick kp->ki_vmspace = p->p_vmspace; 646cefcecbeSPawel Jakub Dawidek kp->ki_flag = p->p_flag; 647d079d0a0SPawel Jakub Dawidek cred = p->p_ucred; 648d079d0a0SPawel Jakub Dawidek if (cred) { 649d079d0a0SPawel Jakub Dawidek kp->ki_uid = cred->cr_uid; 650d079d0a0SPawel Jakub Dawidek kp->ki_ruid = cred->cr_ruid; 651d079d0a0SPawel Jakub Dawidek kp->ki_svuid = cred->cr_svuid; 6520ecd57adSPeter Wemm /* XXX bde doesn't like KI_NGROUPS */ 653d079d0a0SPawel Jakub Dawidek kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS); 654d079d0a0SPawel Jakub Dawidek bcopy(cred->cr_groups, kp->ki_groups, 6550ecd57adSPeter Wemm kp->ki_ngroups * sizeof(gid_t)); 656d079d0a0SPawel Jakub Dawidek kp->ki_rgid = cred->cr_rgid; 657d079d0a0SPawel Jakub Dawidek kp->ki_svgid = cred->cr_svgid; 658cefcecbeSPawel Jakub Dawidek /* If jailed(cred), emulate the old P_JAILED flag. */ 659c78941e6SPawel Jakub Dawidek if (jailed(cred)) { 660cefcecbeSPawel Jakub Dawidek kp->ki_flag |= P_JAILED; 661c78941e6SPawel Jakub Dawidek /* If inside a jail, use 0 as a jail ID. */ 662c78941e6SPawel Jakub Dawidek if (!jailed(curthread->td_ucred)) 663c78941e6SPawel Jakub Dawidek kp->ki_jid = cred->cr_prison->pr_id; 664c78941e6SPawel Jakub Dawidek } 665972f9b20SPoul-Henning Kamp } 66690af4afaSJohn Baldwin ps = p->p_sigacts; 667d079d0a0SPawel Jakub Dawidek if (ps) { 66890af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 66990af4afaSJohn Baldwin kp->ki_sigignore = ps->ps_sigignore; 67090af4afaSJohn Baldwin kp->ki_sigcatch = ps->ps_sigcatch; 67190af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 672d8c85307SJulian Elischer } 673982d11f8SJeff Roberson PROC_SLOCK(p); 674e602ba25SJulian Elischer if (p->p_state != PRS_NEW && 675e602ba25SJulian Elischer p->p_state != PRS_ZOMBIE && 676e602ba25SJulian Elischer p->p_vmspace != NULL) { 6771f7d2501SKirk McKusick struct vmspace *vm = p->p_vmspace; 678cd73303cSDavid Greenman 6791f7d2501SKirk McKusick kp->ki_size = vm->vm_map.size; 6801f7d2501SKirk McKusick kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 681ceff7f2aSTim J. Robbins FOREACH_THREAD_IN_PROC(p, td0) { 682ceff7f2aSTim J. Robbins if (!TD_IS_SWAPPED(td0)) 683ceff7f2aSTim J. Robbins kp->ki_rssize += td0->td_kstack_pages; 684ceff7f2aSTim J. Robbins if (td0->td_altkstack_obj != NULL) 685913fc94dSTim J. Robbins kp->ki_rssize += td0->td_altkstack_pages; 686ceff7f2aSTim J. Robbins } 6871f7d2501SKirk McKusick kp->ki_swrss = vm->vm_swrss; 6881f7d2501SKirk McKusick kp->ki_tsize = vm->vm_tsize; 6891f7d2501SKirk McKusick kp->ki_dsize = vm->vm_dsize; 6901f7d2501SKirk McKusick kp->ki_ssize = vm->vm_ssize; 6915032ff81SDon Lewis } else if (p->p_state == PRS_ZOMBIE) 6925032ff81SDon Lewis kp->ki_stat = SZOMB; 693cebabef0SPawel Jakub Dawidek kp->ki_sflag = p->p_sflag; 694cebabef0SPawel Jakub Dawidek kp->ki_swtime = p->p_swtime; 695cebabef0SPawel Jakub Dawidek kp->ki_pid = p->p_pid; 696cebabef0SPawel Jakub Dawidek kp->ki_nice = p->p_nice; 6971c4bcd05SJeff Roberson rufetch(p, &kp->ki_rusage); 698e8444a7eSPoul-Henning Kamp kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 699982d11f8SJeff Roberson PROC_SUNLOCK(p); 70078c85e8dSJohn Baldwin if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) { 70178c85e8dSJohn Baldwin kp->ki_start = p->p_stats->p_start; 70278c85e8dSJohn Baldwin timevaladd(&kp->ki_start, &boottime); 70378c85e8dSJohn Baldwin calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 70478c85e8dSJohn Baldwin calccru(p, &kp->ki_childutime, &kp->ki_childstime); 70578c85e8dSJohn Baldwin 70678c85e8dSJohn Baldwin /* Some callers want child-times in a single value */ 70778c85e8dSJohn Baldwin kp->ki_childtime = kp->ki_childstime; 70878c85e8dSJohn Baldwin timevaladd(&kp->ki_childtime, &kp->ki_childutime); 70978c85e8dSJohn Baldwin } 710f591779bSSeigo Tanimura tp = NULL; 7111f7d2501SKirk McKusick if (p->p_pgrp) { 7121f7d2501SKirk McKusick kp->ki_pgid = p->p_pgrp->pg_id; 7131f7d2501SKirk McKusick kp->ki_jobc = p->p_pgrp->pg_jobc; 7141f7d2501SKirk McKusick sp = p->p_pgrp->pg_session; 7151f7d2501SKirk McKusick 7161f7d2501SKirk McKusick if (sp != NULL) { 7171f7d2501SKirk McKusick kp->ki_sid = sp->s_sid; 718f591779bSSeigo Tanimura SESS_LOCK(sp); 719e80fb434SRobert Drehmel strlcpy(kp->ki_login, sp->s_login, 720e80fb434SRobert Drehmel sizeof(kp->ki_login)); 7211f7d2501SKirk McKusick if (sp->s_ttyvp) 722b8e6bf1eSJohn Baldwin kp->ki_kiflag |= KI_CTTY; 7231f7d2501SKirk McKusick if (SESS_LEADER(p)) 7241f7d2501SKirk McKusick kp->ki_kiflag |= KI_SLEADER; 725f591779bSSeigo Tanimura tp = sp->s_ttyp; 726f591779bSSeigo Tanimura SESS_UNLOCK(sp); 727cd73303cSDavid Greenman } 728cd73303cSDavid Greenman } 729f591779bSSeigo Tanimura if ((p->p_flag & P_CONTROLT) && tp != NULL) { 7301f7d2501SKirk McKusick kp->ki_tdev = dev2udev(tp->t_dev); 7311f7d2501SKirk McKusick kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 7321f7d2501SKirk McKusick if (tp->t_session) 7331f7d2501SKirk McKusick kp->ki_tsid = tp->t_session->s_sid; 734972f9b20SPoul-Henning Kamp } else 735f3732fd1SPoul-Henning Kamp kp->ki_tdev = NODEV; 73613b762a3SEd Maste if (p->p_comm[0] != '\0') 737e80fb434SRobert Drehmel strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 738078842c5SGarance A Drosehn if (p->p_sysent && p->p_sysent->sv_name != NULL && 739078842c5SGarance A Drosehn p->p_sysent->sv_name[0] != '\0') 740078842c5SGarance A Drosehn strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 7411d9c5696SJuli Mallett kp->ki_siglist = p->p_siglist; 7421f7d2501SKirk McKusick kp->ki_xstat = p->p_xstat; 7431f7d2501SKirk McKusick kp->ki_acflag = p->p_acflag; 7441f7d2501SKirk McKusick kp->ki_lock = p->p_lock; 74542a4ed99SJohn Baldwin if (p->p_pptr) 74642a4ed99SJohn Baldwin kp->ki_ppid = p->p_pptr->p_pid; 747972f9b20SPoul-Henning Kamp } 748972f9b20SPoul-Henning Kamp 7495032ff81SDon Lewis /* 7505032ff81SDon Lewis * Fill in information that is thread specific. 751982d11f8SJeff Roberson * Must be called with p_slock locked. 7525032ff81SDon Lewis */ 7535032ff81SDon Lewis static void 7545032ff81SDon Lewis fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp) 7555032ff81SDon Lewis { 7565032ff81SDon Lewis struct proc *p; 7575032ff81SDon Lewis 7585032ff81SDon Lewis p = td->td_proc; 759982d11f8SJeff Roberson PROC_SLOCK_ASSERT(p, MA_OWNED); 7605032ff81SDon Lewis 761982d11f8SJeff Roberson thread_lock(td); 7625032ff81SDon Lewis if (td->td_wmesg != NULL) 7635032ff81SDon Lewis strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 7645032ff81SDon Lewis else 7655032ff81SDon Lewis bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 76611f4763dSJulian Elischer if (td->td_name[0] != '\0') 76711f4763dSJulian Elischer strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm)); 7685032ff81SDon Lewis if (TD_ON_LOCK(td)) { 7695032ff81SDon Lewis kp->ki_kiflag |= KI_LOCKBLOCK; 7705032ff81SDon Lewis strlcpy(kp->ki_lockname, td->td_lockname, 7715032ff81SDon Lewis sizeof(kp->ki_lockname)); 7725032ff81SDon Lewis } else { 7735032ff81SDon Lewis kp->ki_kiflag &= ~KI_LOCKBLOCK; 7745032ff81SDon Lewis bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 7755032ff81SDon Lewis } 7765032ff81SDon Lewis 7775032ff81SDon Lewis if (p->p_state == PRS_NORMAL) { /* XXXKSE very approximate */ 7785032ff81SDon Lewis if (TD_ON_RUNQ(td) || 7795032ff81SDon Lewis TD_CAN_RUN(td) || 7805032ff81SDon Lewis TD_IS_RUNNING(td)) { 7815032ff81SDon Lewis kp->ki_stat = SRUN; 7825032ff81SDon Lewis } else if (P_SHOULDSTOP(p)) { 7835032ff81SDon Lewis kp->ki_stat = SSTOP; 7845032ff81SDon Lewis } else if (TD_IS_SLEEPING(td)) { 7855032ff81SDon Lewis kp->ki_stat = SSLEEP; 7865032ff81SDon Lewis } else if (TD_ON_LOCK(td)) { 7875032ff81SDon Lewis kp->ki_stat = SLOCK; 7885032ff81SDon Lewis } else { 7895032ff81SDon Lewis kp->ki_stat = SWAIT; 7905032ff81SDon Lewis } 7913357835aSDavid Xu } else if (p->p_state == PRS_ZOMBIE) { 7923357835aSDavid Xu kp->ki_stat = SZOMB; 7935032ff81SDon Lewis } else { 7945032ff81SDon Lewis kp->ki_stat = SIDL; 7955032ff81SDon Lewis } 7965032ff81SDon Lewis 7975032ff81SDon Lewis /* Things in the thread */ 7985032ff81SDon Lewis kp->ki_wchan = td->td_wchan; 7995032ff81SDon Lewis kp->ki_pri.pri_level = td->td_priority; 8005032ff81SDon Lewis kp->ki_pri.pri_native = td->td_base_pri; 8015032ff81SDon Lewis kp->ki_lastcpu = td->td_lastcpu; 8025032ff81SDon Lewis kp->ki_oncpu = td->td_oncpu; 8035032ff81SDon Lewis kp->ki_tdflags = td->td_flags; 8045032ff81SDon Lewis kp->ki_tid = td->td_tid; 8055032ff81SDon Lewis kp->ki_numthreads = p->p_numthreads; 8065032ff81SDon Lewis kp->ki_pcb = td->td_pcb; 8075032ff81SDon Lewis kp->ki_kstack = (void *)td->td_kstack; 8085032ff81SDon Lewis kp->ki_pctcpu = sched_pctcpu(td); 8098460a577SJohn Birrell kp->ki_estcpu = td->td_estcpu; 8108460a577SJohn Birrell kp->ki_slptime = td->td_slptime; 8118460a577SJohn Birrell kp->ki_pri.pri_class = td->td_pri_class; 8128460a577SJohn Birrell kp->ki_pri.pri_user = td->td_user_pri; 8135032ff81SDon Lewis 8145032ff81SDon Lewis /* We can't get this anymore but ps etc never used it anyway. */ 8155032ff81SDon Lewis kp->ki_rqindex = 0; 8165032ff81SDon Lewis 8175032ff81SDon Lewis SIGSETOR(kp->ki_siglist, td->td_siglist); 8185032ff81SDon Lewis kp->ki_sigmask = td->td_sigmask; 819982d11f8SJeff Roberson thread_unlock(td); 8205032ff81SDon Lewis } 8215032ff81SDon Lewis 8225032ff81SDon Lewis /* 8235032ff81SDon Lewis * Fill in a kinfo_proc structure for the specified process. 8245032ff81SDon Lewis * Must be called with the target process locked. 8255032ff81SDon Lewis */ 8265032ff81SDon Lewis void 8275032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 8285032ff81SDon Lewis { 8295032ff81SDon Lewis 8305032ff81SDon Lewis fill_kinfo_proc_only(p, kp); 831982d11f8SJeff Roberson PROC_SLOCK(p); 8325032ff81SDon Lewis if (FIRST_THREAD_IN_PROC(p) != NULL) 8335032ff81SDon Lewis fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp); 834982d11f8SJeff Roberson PROC_SUNLOCK(p); 8355032ff81SDon Lewis } 8365032ff81SDon Lewis 8378b059651SDavid Schultz struct pstats * 8388b059651SDavid Schultz pstats_alloc(void) 8398b059651SDavid Schultz { 8408b059651SDavid Schultz 8418b059651SDavid Schultz return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 8428b059651SDavid Schultz } 8438b059651SDavid Schultz 8448b059651SDavid Schultz /* 8458b059651SDavid Schultz * Copy parts of p_stats; zero the rest of p_stats (statistics). 8468b059651SDavid Schultz */ 8478b059651SDavid Schultz void 8488b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst) 8498b059651SDavid Schultz { 8508b059651SDavid Schultz 8518b059651SDavid Schultz bzero(&dst->pstat_startzero, 8526db36923SDavid Schultz __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 8538b059651SDavid Schultz bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 8546db36923SDavid Schultz __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 8558b059651SDavid Schultz } 8568b059651SDavid Schultz 8578b059651SDavid Schultz void 8588b059651SDavid Schultz pstats_free(struct pstats *ps) 8598b059651SDavid Schultz { 8608b059651SDavid Schultz 8618b059651SDavid Schultz free(ps, M_SUBPROC); 8628b059651SDavid Schultz } 8638b059651SDavid Schultz 8648b059651SDavid Schultz /* 86542a4ed99SJohn Baldwin * Locate a zombie process by number 86642a4ed99SJohn Baldwin */ 86742a4ed99SJohn Baldwin struct proc * 8683ce93e4eSPoul-Henning Kamp zpfind(pid_t pid) 8693ce93e4eSPoul-Henning Kamp { 8703ce93e4eSPoul-Henning Kamp struct proc *p; 8713ce93e4eSPoul-Henning Kamp 8721005a129SJohn Baldwin sx_slock(&allproc_lock); 8731b727751SPoul-Henning Kamp LIST_FOREACH(p, &zombproc, p_list) 87433a9ed9dSJohn Baldwin if (p->p_pid == pid) { 87533a9ed9dSJohn Baldwin PROC_LOCK(p); 876c0c25570SJake Burkholder break; 87733a9ed9dSJohn Baldwin } 8781005a129SJohn Baldwin sx_sunlock(&allproc_lock); 8793ce93e4eSPoul-Henning Kamp return (p); 8803ce93e4eSPoul-Henning Kamp } 8813ce93e4eSPoul-Henning Kamp 88230c6f34eSScott Long #define KERN_PROC_ZOMBMASK 0x3 88330c6f34eSScott Long #define KERN_PROC_NOTHREADS 0x4 8843ce93e4eSPoul-Henning Kamp 88565c9b430SJohn Baldwin /* 88665c9b430SJohn Baldwin * Must be called with the process locked and will return with it unlocked. 88765c9b430SJohn Baldwin */ 8883ce93e4eSPoul-Henning Kamp static int 88930c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 8903ce93e4eSPoul-Henning Kamp { 89130c6f34eSScott Long struct thread *td; 8921f7d2501SKirk McKusick struct kinfo_proc kinfo_proc; 89330c6f34eSScott Long int error = 0; 89433a9ed9dSJohn Baldwin struct proc *np; 8953ce93e4eSPoul-Henning Kamp pid_t pid = p->p_pid; 8963ce93e4eSPoul-Henning Kamp 89765c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 89830c6f34eSScott Long 8995032ff81SDon Lewis fill_kinfo_proc_only(p, &kinfo_proc); 90030c6f34eSScott Long if (flags & KERN_PROC_NOTHREADS) { 901982d11f8SJeff Roberson PROC_SLOCK(p); 9025032ff81SDon Lewis if (FIRST_THREAD_IN_PROC(p) != NULL) 9035032ff81SDon Lewis fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc); 904982d11f8SJeff Roberson PROC_SUNLOCK(p); 90530c6f34eSScott Long error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 90630c6f34eSScott Long sizeof(kinfo_proc)); 90730c6f34eSScott Long } else { 908982d11f8SJeff Roberson PROC_SLOCK(p); 9095032ff81SDon Lewis if (FIRST_THREAD_IN_PROC(p) != NULL) 91030c6f34eSScott Long FOREACH_THREAD_IN_PROC(p, td) { 91130c6f34eSScott Long fill_kinfo_thread(td, &kinfo_proc); 91230c6f34eSScott Long error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 91330c6f34eSScott Long sizeof(kinfo_proc)); 91430c6f34eSScott Long if (error) 91530c6f34eSScott Long break; 91630c6f34eSScott Long } 9175032ff81SDon Lewis else 9185032ff81SDon Lewis error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, 9195032ff81SDon Lewis sizeof(kinfo_proc)); 920982d11f8SJeff Roberson PROC_SUNLOCK(p); 92130c6f34eSScott Long } 92230c6f34eSScott Long PROC_UNLOCK(p); 9233ce93e4eSPoul-Henning Kamp if (error) 9243ce93e4eSPoul-Henning Kamp return (error); 92530c6f34eSScott Long if (flags & KERN_PROC_ZOMBMASK) 92633a9ed9dSJohn Baldwin np = zpfind(pid); 92733a9ed9dSJohn Baldwin else { 92833a9ed9dSJohn Baldwin if (pid == 0) 92933a9ed9dSJohn Baldwin return (0); 93033a9ed9dSJohn Baldwin np = pfind(pid); 93133a9ed9dSJohn Baldwin } 93233a9ed9dSJohn Baldwin if (np == NULL) 9333ce93e4eSPoul-Henning Kamp return EAGAIN; 93433a9ed9dSJohn Baldwin if (np != p) { 93533a9ed9dSJohn Baldwin PROC_UNLOCK(np); 9363ce93e4eSPoul-Henning Kamp return EAGAIN; 93733a9ed9dSJohn Baldwin } 93833a9ed9dSJohn Baldwin PROC_UNLOCK(np); 9393ce93e4eSPoul-Henning Kamp return (0); 9403ce93e4eSPoul-Henning Kamp } 9413ce93e4eSPoul-Henning Kamp 942972f9b20SPoul-Henning Kamp static int 94382d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 944972f9b20SPoul-Henning Kamp { 945972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 946972f9b20SPoul-Henning Kamp u_int namelen = arg2; 947972f9b20SPoul-Henning Kamp struct proc *p; 9482648efa6SDaniel Eischen int flags, doingzomb, oid_number; 949972f9b20SPoul-Henning Kamp int error = 0; 950972f9b20SPoul-Henning Kamp 9512648efa6SDaniel Eischen oid_number = oidp->oid_number; 9522648efa6SDaniel Eischen if (oid_number != KERN_PROC_ALL && 9532648efa6SDaniel Eischen (oid_number & KERN_PROC_INC_THREAD) == 0) 9542648efa6SDaniel Eischen flags = KERN_PROC_NOTHREADS; 9552648efa6SDaniel Eischen else { 9562648efa6SDaniel Eischen flags = 0; 9572648efa6SDaniel Eischen oid_number &= ~KERN_PROC_INC_THREAD; 9582648efa6SDaniel Eischen } 9592648efa6SDaniel Eischen if (oid_number == KERN_PROC_PID) { 9603ce93e4eSPoul-Henning Kamp if (namelen != 1) 961972f9b20SPoul-Henning Kamp return (EINVAL); 9625032ff81SDon Lewis error = sysctl_wire_old_buffer(req, 0); 9635032ff81SDon Lewis if (error) 9645032ff81SDon Lewis return (error); 9653ce93e4eSPoul-Henning Kamp p = pfind((pid_t)name[0]); 9663ce93e4eSPoul-Henning Kamp if (!p) 967e76bad96SRobert Drehmel return (ESRCH); 968e76bad96SRobert Drehmel if ((error = p_cansee(curthread, p))) { 96933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 970e76bad96SRobert Drehmel return (error); 97133a9ed9dSJohn Baldwin } 9722648efa6SDaniel Eischen error = sysctl_out_proc(p, req, flags); 9733ce93e4eSPoul-Henning Kamp return (error); 9743ce93e4eSPoul-Henning Kamp } 9753ddaef40STim J. Robbins 9762648efa6SDaniel Eischen switch (oid_number) { 9773ddaef40STim J. Robbins case KERN_PROC_ALL: 9783ddaef40STim J. Robbins if (namelen != 0) 9793ce93e4eSPoul-Henning Kamp return (EINVAL); 9803ddaef40STim J. Robbins break; 98125e247afSPeter Wemm case KERN_PROC_PROC: 98225e247afSPeter Wemm if (namelen != 0 && namelen != 1) 98325e247afSPeter Wemm return (EINVAL); 98425e247afSPeter Wemm break; 9853ddaef40STim J. Robbins default: 9863ddaef40STim J. Robbins if (namelen != 1) 9873ddaef40STim J. Robbins return (EINVAL); 9883ddaef40STim J. Robbins break; 9893ddaef40STim J. Robbins } 9903ce93e4eSPoul-Henning Kamp 991972f9b20SPoul-Henning Kamp if (!req->oldptr) { 9923ce93e4eSPoul-Henning Kamp /* overestimate by 5 procs */ 993972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 994972f9b20SPoul-Henning Kamp if (error) 995972f9b20SPoul-Henning Kamp return (error); 996972f9b20SPoul-Henning Kamp } 99747934cefSDon Lewis error = sysctl_wire_old_buffer(req, 0); 99847934cefSDon Lewis if (error != 0) 99947934cefSDon Lewis return (error); 10001005a129SJohn Baldwin sx_slock(&allproc_lock); 10013ce93e4eSPoul-Henning Kamp for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 10023ce93e4eSPoul-Henning Kamp if (!doingzomb) 10031b727751SPoul-Henning Kamp p = LIST_FIRST(&allproc); 10043ce93e4eSPoul-Henning Kamp else 10051b727751SPoul-Henning Kamp p = LIST_FIRST(&zombproc); 10061b727751SPoul-Henning Kamp for (; p != 0; p = LIST_NEXT(p, p_list)) { 100702e878d9SJohn Baldwin /* 100802e878d9SJohn Baldwin * Skip embryonic processes. 100902e878d9SJohn Baldwin */ 1010982d11f8SJeff Roberson PROC_SLOCK(p); 101102e878d9SJohn Baldwin if (p->p_state == PRS_NEW) { 1012982d11f8SJeff Roberson PROC_SUNLOCK(p); 101302e878d9SJohn Baldwin continue; 101402e878d9SJohn Baldwin } 1015982d11f8SJeff Roberson PROC_SUNLOCK(p); 101665c9b430SJohn Baldwin PROC_LOCK(p); 1017b241b0a2SJuli Mallett KASSERT(p->p_ucred != NULL, 1018b241b0a2SJuli Mallett ("process credential is NULL for non-NEW proc")); 1019972f9b20SPoul-Henning Kamp /* 1020387d2c03SRobert Watson * Show a user only appropriate processes. 102103f808c5SPaul Saab */ 1022f44d9e24SJohn Baldwin if (p_cansee(curthread, p)) { 102365c9b430SJohn Baldwin PROC_UNLOCK(p); 102403f808c5SPaul Saab continue; 102565c9b430SJohn Baldwin } 102603f808c5SPaul Saab /* 1027972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 1028972f9b20SPoul-Henning Kamp * do by session. 1029972f9b20SPoul-Henning Kamp */ 10302648efa6SDaniel Eischen switch (oid_number) { 1031972f9b20SPoul-Henning Kamp 1032078842c5SGarance A Drosehn case KERN_PROC_GID: 1033b241b0a2SJuli Mallett if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1034078842c5SGarance A Drosehn PROC_UNLOCK(p); 1035078842c5SGarance A Drosehn continue; 1036078842c5SGarance A Drosehn } 1037078842c5SGarance A Drosehn break; 1038078842c5SGarance A Drosehn 1039972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 1040972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 10413ce93e4eSPoul-Henning Kamp if (p->p_pgrp == NULL || 1042f591779bSSeigo Tanimura p->p_pgrp->pg_id != (pid_t)name[0]) { 1043f591779bSSeigo Tanimura PROC_UNLOCK(p); 1044972f9b20SPoul-Henning Kamp continue; 1045f591779bSSeigo Tanimura } 1046972f9b20SPoul-Henning Kamp break; 1047972f9b20SPoul-Henning Kamp 1048b8fdc89dSGarance A Drosehn case KERN_PROC_RGID: 1049b241b0a2SJuli Mallett if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1050b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1051b8fdc89dSGarance A Drosehn continue; 1052b8fdc89dSGarance A Drosehn } 1053b8fdc89dSGarance A Drosehn break; 1054b8fdc89dSGarance A Drosehn 1055b8fdc89dSGarance A Drosehn case KERN_PROC_SESSION: 1056b8fdc89dSGarance A Drosehn if (p->p_session == NULL || 1057b8fdc89dSGarance A Drosehn p->p_session->s_sid != (pid_t)name[0]) { 1058b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1059b8fdc89dSGarance A Drosehn continue; 1060b8fdc89dSGarance A Drosehn } 1061b8fdc89dSGarance A Drosehn break; 1062b8fdc89dSGarance A Drosehn 1063972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 1064972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 1065f591779bSSeigo Tanimura p->p_session == NULL) { 1066f591779bSSeigo Tanimura PROC_UNLOCK(p); 1067972f9b20SPoul-Henning Kamp continue; 1068f591779bSSeigo Tanimura } 1069f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 1070f591779bSSeigo Tanimura if (p->p_session->s_ttyp == NULL || 1071f591779bSSeigo Tanimura dev2udev(p->p_session->s_ttyp->t_dev) != 1072f3732fd1SPoul-Henning Kamp (dev_t)name[0]) { 1073f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1074f591779bSSeigo Tanimura PROC_UNLOCK(p); 1075f591779bSSeigo Tanimura continue; 1076f591779bSSeigo Tanimura } 1077f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1078972f9b20SPoul-Henning Kamp break; 1079972f9b20SPoul-Henning Kamp 1080972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 1081b241b0a2SJuli Mallett if (p->p_ucred->cr_uid != (uid_t)name[0]) { 108265c9b430SJohn Baldwin PROC_UNLOCK(p); 1083972f9b20SPoul-Henning Kamp continue; 108465c9b430SJohn Baldwin } 1085972f9b20SPoul-Henning Kamp break; 1086972f9b20SPoul-Henning Kamp 1087972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 1088b241b0a2SJuli Mallett if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 108965c9b430SJohn Baldwin PROC_UNLOCK(p); 1090972f9b20SPoul-Henning Kamp continue; 109165c9b430SJohn Baldwin } 1092972f9b20SPoul-Henning Kamp break; 109330c6f34eSScott Long 109430c6f34eSScott Long case KERN_PROC_PROC: 109530c6f34eSScott Long break; 109630c6f34eSScott Long 109730c6f34eSScott Long default: 109830c6f34eSScott Long break; 109930c6f34eSScott Long 1100972f9b20SPoul-Henning Kamp } 1101972f9b20SPoul-Henning Kamp 110230c6f34eSScott Long error = sysctl_out_proc(p, req, flags | doingzomb); 1103553629ebSJake Burkholder if (error) { 11041005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1105972f9b20SPoul-Henning Kamp return (error); 1106972f9b20SPoul-Henning Kamp } 1107972f9b20SPoul-Henning Kamp } 1108553629ebSJake Burkholder } 11091005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1110972f9b20SPoul-Henning Kamp return (0); 1111972f9b20SPoul-Henning Kamp } 1112972f9b20SPoul-Henning Kamp 1113c1508b28SAlfred Perlstein struct pargs * 1114c1508b28SAlfred Perlstein pargs_alloc(int len) 1115c1508b28SAlfred Perlstein { 1116c1508b28SAlfred Perlstein struct pargs *pa; 1117c1508b28SAlfred Perlstein 1118c1508b28SAlfred Perlstein MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS, 1119a163d034SWarner Losh M_WAITOK); 112055b4a5aeSJohn Baldwin refcount_init(&pa->ar_ref, 1); 1121c1508b28SAlfred Perlstein pa->ar_length = len; 1122c1508b28SAlfred Perlstein return (pa); 1123c1508b28SAlfred Perlstein } 1124c1508b28SAlfred Perlstein 1125c1508b28SAlfred Perlstein void 1126c1508b28SAlfred Perlstein pargs_free(struct pargs *pa) 1127c1508b28SAlfred Perlstein { 1128c1508b28SAlfred Perlstein 1129c1508b28SAlfred Perlstein FREE(pa, M_PARGS); 1130c1508b28SAlfred Perlstein } 1131c1508b28SAlfred Perlstein 1132c1508b28SAlfred Perlstein void 1133c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa) 1134c1508b28SAlfred Perlstein { 1135c1508b28SAlfred Perlstein 1136c1508b28SAlfred Perlstein if (pa == NULL) 1137c1508b28SAlfred Perlstein return; 113855b4a5aeSJohn Baldwin refcount_acquire(&pa->ar_ref); 1139c1508b28SAlfred Perlstein } 1140c1508b28SAlfred Perlstein 1141c1508b28SAlfred Perlstein void 1142c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa) 1143c1508b28SAlfred Perlstein { 1144c1508b28SAlfred Perlstein 1145c1508b28SAlfred Perlstein if (pa == NULL) 1146c1508b28SAlfred Perlstein return; 114755b4a5aeSJohn Baldwin if (refcount_release(&pa->ar_ref)) 1148c1508b28SAlfred Perlstein pargs_free(pa); 1149c1508b28SAlfred Perlstein } 1150c1508b28SAlfred Perlstein 1151b9df5231SPoul-Henning Kamp /* 1152b9df5231SPoul-Henning Kamp * This sysctl allows a process to retrieve the argument list or process 1153b9df5231SPoul-Henning Kamp * title for another process without groping around in the address space 1154b9df5231SPoul-Henning Kamp * of the other process. It also allow a process to set its own "process 1155b9df5231SPoul-Henning Kamp * title to a string of its own choice. 1156b9df5231SPoul-Henning Kamp */ 1157b9df5231SPoul-Henning Kamp static int 115882d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1159b9df5231SPoul-Henning Kamp { 1160b9df5231SPoul-Henning Kamp int *name = (int*) arg1; 1161b9df5231SPoul-Henning Kamp u_int namelen = arg2; 11628510f2a8SJohn Baldwin struct pargs *newpa, *pa; 1163b9df5231SPoul-Henning Kamp struct proc *p; 1164b9df5231SPoul-Henning Kamp int error = 0; 1165b9df5231SPoul-Henning Kamp 1166b9df5231SPoul-Henning Kamp if (namelen != 1) 1167b9df5231SPoul-Henning Kamp return (EINVAL); 1168b9df5231SPoul-Henning Kamp 1169b9df5231SPoul-Henning Kamp p = pfind((pid_t)name[0]); 1170b9df5231SPoul-Henning Kamp if (!p) 1171e76bad96SRobert Drehmel return (ESRCH); 1172b9df5231SPoul-Henning Kamp 11739cdb6216SPawel Jakub Dawidek if ((error = p_cansee(curthread, p)) != 0) { 117433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1175e76bad96SRobert Drehmel return (error); 117633a9ed9dSJohn Baldwin } 11774bc6471bSJohn Baldwin 11784bc6471bSJohn Baldwin if (req->newptr && curproc != p) { 117933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1180b9df5231SPoul-Henning Kamp return (EPERM); 11814bc6471bSJohn Baldwin } 1182b9df5231SPoul-Henning Kamp 11837b11fea6SAlfred Perlstein pa = p->p_args; 11847b11fea6SAlfred Perlstein pargs_hold(pa); 11857b11fea6SAlfred Perlstein PROC_UNLOCK(p); 11868510f2a8SJohn Baldwin if (req->oldptr != NULL && pa != NULL) 11877b11fea6SAlfred Perlstein error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 11887b11fea6SAlfred Perlstein pargs_drop(pa); 11898510f2a8SJohn Baldwin if (error != 0 || req->newptr == NULL) 1190b9df5231SPoul-Henning Kamp return (error); 1191b9df5231SPoul-Henning Kamp 1192b9df5231SPoul-Henning Kamp if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 11938510f2a8SJohn Baldwin return (ENOMEM); 11944bc6471bSJohn Baldwin newpa = pargs_alloc(req->newlen); 11954bc6471bSJohn Baldwin error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 11968510f2a8SJohn Baldwin if (error != 0) { 11974bc6471bSJohn Baldwin pargs_free(newpa); 1198b9df5231SPoul-Henning Kamp return (error); 1199b9df5231SPoul-Henning Kamp } 12004bc6471bSJohn Baldwin PROC_LOCK(p); 12014bc6471bSJohn Baldwin pa = p->p_args; 12024bc6471bSJohn Baldwin p->p_args = newpa; 12034bc6471bSJohn Baldwin PROC_UNLOCK(p); 12044bc6471bSJohn Baldwin pargs_drop(pa); 12054bc6471bSJohn Baldwin return (0); 12064bc6471bSJohn Baldwin } 12074bc6471bSJohn Baldwin 1208fe769cddSDavid Schultz /* 1209fe769cddSDavid Schultz * This sysctl allows a process to retrieve the path of the executable for 1210fe769cddSDavid Schultz * itself or another process. 1211fe769cddSDavid Schultz */ 1212fe769cddSDavid Schultz static int 1213fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1214fe769cddSDavid Schultz { 1215fe769cddSDavid Schultz pid_t *pidp = (pid_t *)arg1; 1216fe769cddSDavid Schultz unsigned int arglen = arg2; 1217fe769cddSDavid Schultz struct proc *p; 1218fe769cddSDavid Schultz struct vnode *vp; 1219fe769cddSDavid Schultz char *retbuf, *freebuf; 1220fe769cddSDavid Schultz int error; 1221fe769cddSDavid Schultz 1222fe769cddSDavid Schultz if (arglen != 1) 1223fe769cddSDavid Schultz return (EINVAL); 1224fe769cddSDavid Schultz if (*pidp == -1) { /* -1 means this process */ 1225fe769cddSDavid Schultz p = req->td->td_proc; 1226fe769cddSDavid Schultz } else { 1227fe769cddSDavid Schultz p = pfind(*pidp); 1228fe769cddSDavid Schultz if (p == NULL) 1229fe769cddSDavid Schultz return (ESRCH); 1230fe769cddSDavid Schultz if ((error = p_cansee(curthread, p)) != 0) { 1231fe769cddSDavid Schultz PROC_UNLOCK(p); 1232fe769cddSDavid Schultz return (error); 1233fe769cddSDavid Schultz } 1234fe769cddSDavid Schultz } 1235fe769cddSDavid Schultz 1236fe769cddSDavid Schultz vp = p->p_textvp; 1237fe769cddSDavid Schultz vref(vp); 1238fe769cddSDavid Schultz if (*pidp != -1) 1239fe769cddSDavid Schultz PROC_UNLOCK(p); 1240fe769cddSDavid Schultz error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 1241fe769cddSDavid Schultz vrele(vp); 1242fe769cddSDavid Schultz if (error) 1243fe769cddSDavid Schultz return (error); 1244fe769cddSDavid Schultz error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1245fe769cddSDavid Schultz free(freebuf, M_TEMP); 1246fe769cddSDavid Schultz return (error); 1247fe769cddSDavid Schultz } 1248fe769cddSDavid Schultz 1249baf731e6SRobert Drehmel static int 1250baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1251baf731e6SRobert Drehmel { 1252baf731e6SRobert Drehmel struct proc *p; 1253baf731e6SRobert Drehmel char *sv_name; 1254baf731e6SRobert Drehmel int *name; 1255baf731e6SRobert Drehmel int namelen; 1256e76bad96SRobert Drehmel int error; 1257baf731e6SRobert Drehmel 1258baf731e6SRobert Drehmel namelen = arg2; 1259baf731e6SRobert Drehmel if (namelen != 1) 1260baf731e6SRobert Drehmel return (EINVAL); 1261baf731e6SRobert Drehmel 1262baf731e6SRobert Drehmel name = (int *)arg1; 1263baf731e6SRobert Drehmel if ((p = pfind((pid_t)name[0])) == NULL) 1264e76bad96SRobert Drehmel return (ESRCH); 1265e76bad96SRobert Drehmel if ((error = p_cansee(curthread, p))) { 1266baf731e6SRobert Drehmel PROC_UNLOCK(p); 1267e76bad96SRobert Drehmel return (error); 1268baf731e6SRobert Drehmel } 1269baf731e6SRobert Drehmel sv_name = p->p_sysent->sv_name; 1270baf731e6SRobert Drehmel PROC_UNLOCK(p); 1271baf731e6SRobert Drehmel return (sysctl_handle_string(oidp, sv_name, 0, req)); 1272baf731e6SRobert Drehmel } 1273baf731e6SRobert Drehmel 1274baf731e6SRobert Drehmel 12750c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 12763ce93e4eSPoul-Henning Kamp 12773ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 12783d177f46SBill Fumerola 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table"); 12793ce93e4eSPoul-Henning Kamp 12800c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD, 1281078842c5SGarance A Drosehn sysctl_kern_proc, "Process table"); 1282078842c5SGarance A Drosehn 12830c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 12843ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12853ce93e4eSPoul-Henning Kamp 12860c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD, 1287b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 1288b8fdc89dSGarance A Drosehn 12890c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD, 1290b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 1291b8fdc89dSGarance A Drosehn 12920c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 12933ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12943ce93e4eSPoul-Henning Kamp 12950c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 12963ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 12973ce93e4eSPoul-Henning Kamp 12980c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 12993ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 13003ce93e4eSPoul-Henning Kamp 13010c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 1302972f9b20SPoul-Henning Kamp sysctl_kern_proc, "Process table"); 1303b9df5231SPoul-Henning Kamp 13040c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD, 130530c6f34eSScott Long sysctl_kern_proc, "Return process table, no threads"); 130630c6f34eSScott Long 13070c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 13080c898376SPoul-Henning Kamp CTLFLAG_RW | CTLFLAG_ANYBODY, 13099b6d9dbaSPoul-Henning Kamp sysctl_kern_proc_args, "Process argument list"); 1310baf731e6SRobert Drehmel 1311fe769cddSDavid Schultz static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD, 1312fe769cddSDavid Schultz sysctl_kern_proc_pathname, "Process executable path"); 1313fe769cddSDavid Schultz 13140c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD, 1315baf731e6SRobert Drehmel sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)"); 13162648efa6SDaniel Eischen 13170c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 1318078842c5SGarance A Drosehn CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1319078842c5SGarance A Drosehn 13200c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 13212648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13222648efa6SDaniel Eischen 13230c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 1324b8fdc89dSGarance A Drosehn CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1325b8fdc89dSGarance A Drosehn 13260c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 13270c898376SPoul-Henning Kamp sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13280c898376SPoul-Henning Kamp 13290c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 1330b8fdc89dSGarance A Drosehn CTLFLAG_RD, sysctl_kern_proc, "Process table"); 1331b8fdc89dSGarance A Drosehn 13320c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 13332648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13342648efa6SDaniel Eischen 13350c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 13362648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13372648efa6SDaniel Eischen 13380c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 13392648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Process table"); 13402648efa6SDaniel Eischen 13410c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 13422648efa6SDaniel Eischen CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads"); 1343