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 3543151ee6SPeter Wemm #include "opt_compat.h" 36cc43c38cSRobert Watson #include "opt_ddb.h" 375d217f17SJohn Birrell #include "opt_kdtrace.h" 386c84de02SJohn Baldwin #include "opt_ktrace.h" 39b9f009b0SPeter Wemm #include "opt_kstack_pages.h" 401cc8c45cSRobert Watson #include "opt_stack.h" 416c84de02SJohn Baldwin 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44c5cfcb1cSMikolaj Golub #include <sys/elf.h> 45c5cfcb1cSMikolaj Golub #include <sys/exec.h> 46df8bae1dSRodney W. Grimes #include <sys/kernel.h> 4705e06d11SKonstantin Belousov #include <sys/limits.h> 48fb919e4dSMark Murray #include <sys/lock.h> 497123f4cdSEdward Tomasz Napierala #include <sys/loginclass.h> 50df8bae1dSRodney W. Grimes #include <sys/malloc.h> 515384d089SMikolaj Golub #include <sys/mman.h> 52cc43c38cSRobert Watson #include <sys/mount.h> 53fb919e4dSMark Murray #include <sys/mutex.h> 54b9df5231SPoul-Henning Kamp #include <sys/proc.h> 55c5cfcb1cSMikolaj Golub #include <sys/ptrace.h> 5655b4a5aeSJohn Baldwin #include <sys/refcount.h> 579e7d0583SMikolaj Golub #include <sys/resourcevar.h> 58cc43c38cSRobert Watson #include <sys/sbuf.h> 59baf731e6SRobert Drehmel #include <sys/sysent.h> 60de028f5aSJeff Roberson #include <sys/sched.h> 61165d2b99SJulian Elischer #include <sys/smp.h> 621cc8c45cSRobert Watson #include <sys/stack.h> 63fb919e4dSMark Murray #include <sys/sysctl.h> 6462d6ce3aSDon Lewis #include <sys/filedesc.h> 65df8bae1dSRodney W. Grimes #include <sys/tty.h> 66bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h> 675d217f17SJohn Birrell #include <sys/sdt.h> 681005a129SJohn Baldwin #include <sys/sx.h> 69fb919e4dSMark Murray #include <sys/user.h> 70fb919e4dSMark Murray #include <sys/jail.h> 71fe769cddSDavid Schultz #include <sys/vnode.h> 724a62a3e5SRandall Stewart #include <sys/eventhandler.h> 73fb919e4dSMark Murray 74cc43c38cSRobert Watson #ifdef DDB 75cc43c38cSRobert Watson #include <ddb/ddb.h> 76cc43c38cSRobert Watson #endif 77cc43c38cSRobert Watson 78efeaf95aSDavid Greenman #include <vm/vm.h> 79a136efe9SPeter Wemm #include <vm/vm_extern.h> 80efeaf95aSDavid Greenman #include <vm/pmap.h> 81efeaf95aSDavid Greenman #include <vm/vm_map.h> 82cc43c38cSRobert Watson #include <vm/vm_object.h> 835384d089SMikolaj Golub #include <vm/vm_page.h> 84c897b813SJeff Roberson #include <vm/uma.h> 85df8bae1dSRodney W. Grimes 8605e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32 8705e06d11SKonstantin Belousov #include <compat/freebsd32/freebsd32.h> 8805e06d11SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h> 8905e06d11SKonstantin Belousov #endif 9005e06d11SKonstantin Belousov 915d217f17SJohn Birrell SDT_PROVIDER_DEFINE(proc); 9279856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry); 935d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *"); 945d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int"); 955d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *"); 965d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int"); 9779856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, ctor, return, return); 985d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *"); 995d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int"); 1005d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *"); 1015d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int"); 10279856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry); 1035d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *"); 1045d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int"); 1055d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *"); 1065d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *"); 10779856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, dtor, return, return); 1085d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *"); 1095d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int"); 1105d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *"); 11179856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, init, entry, entry); 1125d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *"); 1135d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int"); 1145d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int"); 11579856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, init, return, return); 1165d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *"); 1175d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int"); 1185d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int"); 1195d217f17SJohn Birrell 120f591779bSSeigo Tanimura MALLOC_DEFINE(M_PGRP, "pgrp", "process group header"); 121a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header"); 122876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures"); 123a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures"); 12455166637SPoul-Henning Kamp 1254d77a549SAlfred Perlstein static void doenterpgrp(struct proc *, struct pgrp *); 1264d77a549SAlfred Perlstein static void orphanpg(struct pgrp *pg); 127f8d90480SAttilio Rao static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp); 1285032ff81SDon Lewis static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp); 129d92909c1SRobert Watson static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, 130d92909c1SRobert Watson int preferthread); 13102e878d9SJohn Baldwin static void pgadjustjobc(struct pgrp *pgrp, int entering); 13202e878d9SJohn Baldwin static void pgdelete(struct pgrp *); 133b23f72e9SBrian Feldman static int proc_ctor(void *mem, int size, void *arg, int flags); 134a136efe9SPeter Wemm static void proc_dtor(void *mem, int size, void *arg); 135b23f72e9SBrian Feldman static int proc_init(void *mem, int size, int flags); 136a136efe9SPeter Wemm static void proc_fini(void *mem, int size); 13758e8af1bSKonstantin Belousov static void pargs_free(struct pargs *pa); 138a136efe9SPeter Wemm 139df8bae1dSRodney W. Grimes /* 140b75356e1SJeffrey Hsu * Other process lists 141b75356e1SJeffrey Hsu */ 142b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl; 143b75356e1SJeffrey Hsu u_long pidhash; 144b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl; 145b75356e1SJeffrey Hsu u_long pgrphash; 146b75356e1SJeffrey Hsu struct proclist allproc; 147b75356e1SJeffrey Hsu struct proclist zombproc; 1481005a129SJohn Baldwin struct sx allproc_lock; 1491005a129SJohn Baldwin struct sx proctree_lock; 150c6544064SJohn Baldwin struct mtx ppeers_lock; 151c897b813SJeff Roberson uma_zone_t proc_zone; 152b75356e1SJeffrey Hsu 153b9f009b0SPeter Wemm int kstack_pages = KSTACK_PAGES; 154b389be97SRebecca Cran SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, 155b389be97SRebecca Cran "Kernel stack size in pages"); 156b9f009b0SPeter Wemm 157a30d7c60SJake Burkholder CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE); 158ed780687SKonstantin Belousov #ifdef COMPAT_FREEBSD32 159ed780687SKonstantin Belousov CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE); 160ed780687SKonstantin Belousov #endif 161a30d7c60SJake Burkholder 162b75356e1SJeffrey Hsu /* 163b75356e1SJeffrey Hsu * Initialize global process hashing structures. 164df8bae1dSRodney W. Grimes */ 16526f9a767SRodney W. Grimes void 166b75356e1SJeffrey Hsu procinit() 167df8bae1dSRodney W. Grimes { 168df8bae1dSRodney W. Grimes 1691005a129SJohn Baldwin sx_init(&allproc_lock, "allproc"); 1701005a129SJohn Baldwin sx_init(&proctree_lock, "proctree"); 171c6544064SJohn Baldwin mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); 172b75356e1SJeffrey Hsu LIST_INIT(&allproc); 173b75356e1SJeffrey Hsu LIST_INIT(&zombproc); 174b75356e1SJeffrey Hsu pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 175b75356e1SJeffrey Hsu pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 176de028f5aSJeff Roberson proc_zone = uma_zcreate("PROC", sched_sizeof_proc(), 177a136efe9SPeter Wemm proc_ctor, proc_dtor, proc_init, proc_fini, 178a136efe9SPeter Wemm UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 179f535380cSDon Lewis uihashinit(); 180df8bae1dSRodney W. Grimes } 181df8bae1dSRodney W. Grimes 182df8bae1dSRodney W. Grimes /* 183a136efe9SPeter Wemm * Prepare a proc for use. 184a136efe9SPeter Wemm */ 185b23f72e9SBrian Feldman static int 186b23f72e9SBrian Feldman proc_ctor(void *mem, int size, void *arg, int flags) 187a136efe9SPeter Wemm { 188a136efe9SPeter Wemm struct proc *p; 189a136efe9SPeter Wemm 190a136efe9SPeter Wemm p = (struct proc *)mem; 1915d217f17SJohn Birrell SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0); 1924a62a3e5SRandall Stewart EVENTHANDLER_INVOKE(process_ctor, p); 1935d217f17SJohn Birrell SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0); 194b23f72e9SBrian Feldman return (0); 195a136efe9SPeter Wemm } 196a136efe9SPeter Wemm 197a136efe9SPeter Wemm /* 198a136efe9SPeter Wemm * Reclaim a proc after use. 199a136efe9SPeter Wemm */ 200a136efe9SPeter Wemm static void 201a136efe9SPeter Wemm proc_dtor(void *mem, int size, void *arg) 202a136efe9SPeter Wemm { 203a136efe9SPeter Wemm struct proc *p; 2041faf202eSJulian Elischer struct thread *td; 205a136efe9SPeter Wemm 2061faf202eSJulian Elischer /* INVARIANTS checks go here */ 207a136efe9SPeter Wemm p = (struct proc *)mem; 208ed062c8dSJulian Elischer td = FIRST_THREAD_IN_PROC(p); 2095d217f17SJohn Birrell SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0); 21089b57fcfSKonstantin Belousov if (td != NULL) { 211ed062c8dSJulian Elischer #ifdef INVARIANTS 2121faf202eSJulian Elischer KASSERT((p->p_numthreads == 1), 2131faf202eSJulian Elischer ("bad number of threads in exiting process")); 2142c255e9dSRobert Watson KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr")); 215ed062c8dSJulian Elischer #endif 2161ba4a712SPawel Jakub Dawidek /* Free all OSD associated to this thread. */ 2171ba4a712SPawel Jakub Dawidek osd_thread_exit(td); 21889b57fcfSKonstantin Belousov } 2194a62a3e5SRandall Stewart EVENTHANDLER_INVOKE(process_dtor, p); 220ebceaf6dSDavid Xu if (p->p_ksi != NULL) 221ebceaf6dSDavid Xu KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue")); 2225d217f17SJohn Birrell SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0); 223a136efe9SPeter Wemm } 224a136efe9SPeter Wemm 225a136efe9SPeter Wemm /* 226a136efe9SPeter Wemm * Initialize type-stable parts of a proc (when newly created). 227a136efe9SPeter Wemm */ 228b23f72e9SBrian Feldman static int 229b23f72e9SBrian Feldman proc_init(void *mem, int size, int flags) 230a136efe9SPeter Wemm { 231a136efe9SPeter Wemm struct proc *p; 232a136efe9SPeter Wemm 233a136efe9SPeter Wemm p = (struct proc *)mem; 2345d217f17SJohn Birrell SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0); 235de028f5aSJeff Roberson p->p_sched = (struct p_sched *)&p[1]; 2367d447c95SJohn Baldwin bzero(&p->p_mtx, sizeof(struct mtx)); 2377d447c95SJohn Baldwin mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 238982d11f8SJeff Roberson mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 239aeb32571SKonstantin Belousov cv_init(&p->p_pwait, "ppwait"); 2406fa39a73SKonstantin Belousov cv_init(&p->p_dbgwait, "dbgwait"); 24189b57fcfSKonstantin Belousov TAILQ_INIT(&p->p_threads); /* all threads in proc */ 2424a62a3e5SRandall Stewart EVENTHANDLER_INVOKE(process_init, p); 2438b059651SDavid Schultz p->p_stats = pstats_alloc(); 2445d217f17SJohn Birrell SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0); 245b23f72e9SBrian Feldman return (0); 246a136efe9SPeter Wemm } 247a136efe9SPeter Wemm 248a136efe9SPeter Wemm /* 2498daa8c60SDavid Schultz * UMA should ensure that this function is never called. 2508daa8c60SDavid Schultz * Freeing a proc structure would violate type stability. 251a136efe9SPeter Wemm */ 252a136efe9SPeter Wemm static void 253a136efe9SPeter Wemm proc_fini(void *mem, int size) 254a136efe9SPeter Wemm { 255f55ab994SJohn Baldwin #ifdef notnow 256f55ab994SJohn Baldwin struct proc *p; 257a136efe9SPeter Wemm 258f55ab994SJohn Baldwin p = (struct proc *)mem; 2594a62a3e5SRandall Stewart EVENTHANDLER_INVOKE(process_fini, p); 260f55ab994SJohn Baldwin pstats_free(p->p_stats); 261f55ab994SJohn Baldwin thread_free(FIRST_THREAD_IN_PROC(p)); 262f55ab994SJohn Baldwin mtx_destroy(&p->p_mtx); 263ebceaf6dSDavid Xu if (p->p_ksi != NULL) 264ebceaf6dSDavid Xu ksiginfo_free(p->p_ksi); 265f55ab994SJohn Baldwin #else 2668daa8c60SDavid Schultz panic("proc reclaimed"); 267f55ab994SJohn Baldwin #endif 268a136efe9SPeter Wemm } 269a136efe9SPeter Wemm 270a136efe9SPeter Wemm /* 271df8bae1dSRodney W. Grimes * Is p an inferior of the current process? 272df8bae1dSRodney W. Grimes */ 2731a432a2fSDima Ruban int 274df8bae1dSRodney W. Grimes inferior(p) 275df8bae1dSRodney W. Grimes register struct proc *p; 276df8bae1dSRodney W. Grimes { 277df8bae1dSRodney W. Grimes 2785b29d6e9SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 2795b29d6e9SJohn Baldwin for (; p != curproc; p = p->p_pptr) 28000f13cb3SJohn Baldwin if (p->p_pid == 0) 28100f13cb3SJohn Baldwin return (0); 28200f13cb3SJohn Baldwin return (1); 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes /* 2866cbea71cSRobert Watson * Locate a process by number; return only "live" processes -- i.e., neither 2876cbea71cSRobert Watson * zombies nor newly born but incompletely initialized processes. By not 2886cbea71cSRobert Watson * returning processes in the PRS_NEW state, we allow callers to avoid 2896cbea71cSRobert Watson * testing for that condition to avoid dereferencing p_ucred, et al. 290df8bae1dSRodney W. Grimes */ 291df8bae1dSRodney W. Grimes struct proc * 292df8bae1dSRodney W. Grimes pfind(pid) 293df8bae1dSRodney W. Grimes register pid_t pid; 294df8bae1dSRodney W. Grimes { 295df8bae1dSRodney W. Grimes register struct proc *p; 296df8bae1dSRodney W. Grimes 2971005a129SJohn Baldwin sx_slock(&allproc_lock); 2981b727751SPoul-Henning Kamp LIST_FOREACH(p, PIDHASH(pid), p_hash) 29933a9ed9dSJohn Baldwin if (p->p_pid == pid) { 30033a9ed9dSJohn Baldwin PROC_LOCK(p); 3018e6fa660SJohn Baldwin if (p->p_state == PRS_NEW) { 3028e6fa660SJohn Baldwin PROC_UNLOCK(p); 3038e6fa660SJohn Baldwin p = NULL; 3048e6fa660SJohn Baldwin } 305553629ebSJake Burkholder break; 30633a9ed9dSJohn Baldwin } 30798ab1489SJeffrey Hsu sx_sunlock(&allproc_lock); 308df8bae1dSRodney W. Grimes return (p); 309df8bae1dSRodney W. Grimes } 310df8bae1dSRodney W. Grimes 311df8bae1dSRodney W. Grimes /* 312f591779bSSeigo Tanimura * Locate a process group by number. 313f089b570SJohn Baldwin * The caller must hold proctree_lock. 314df8bae1dSRodney W. Grimes */ 315df8bae1dSRodney W. Grimes struct pgrp * 316df8bae1dSRodney W. Grimes pgfind(pgid) 317df8bae1dSRodney W. Grimes register pid_t pgid; 318df8bae1dSRodney W. Grimes { 319df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 320df8bae1dSRodney W. Grimes 321f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 322f591779bSSeigo Tanimura 323f591779bSSeigo Tanimura LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) { 324f591779bSSeigo Tanimura if (pgrp->pg_id == pgid) { 325f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 326df8bae1dSRodney W. Grimes return (pgrp); 327f591779bSSeigo Tanimura } 328f591779bSSeigo Tanimura } 329df8bae1dSRodney W. Grimes return (NULL); 330df8bae1dSRodney W. Grimes } 331df8bae1dSRodney W. Grimes 332df8bae1dSRodney W. Grimes /* 333*fa3935bcSMikolaj Golub * Locate process and do additional manipulations, depending on flags. 334*fa3935bcSMikolaj Golub */ 335*fa3935bcSMikolaj Golub int 336*fa3935bcSMikolaj Golub pget(pid_t pid, int flags, struct proc **pp) 337*fa3935bcSMikolaj Golub { 338*fa3935bcSMikolaj Golub struct proc *p; 339*fa3935bcSMikolaj Golub int error; 340*fa3935bcSMikolaj Golub 341*fa3935bcSMikolaj Golub p = pfind(pid); 342*fa3935bcSMikolaj Golub if (p == NULL) 343*fa3935bcSMikolaj Golub return (ESRCH); 344*fa3935bcSMikolaj Golub if ((flags & PGET_CANSEE) != 0) { 345*fa3935bcSMikolaj Golub error = p_cansee(curthread, p); 346*fa3935bcSMikolaj Golub if (error != 0) 347*fa3935bcSMikolaj Golub goto errout; 348*fa3935bcSMikolaj Golub } 349*fa3935bcSMikolaj Golub if ((flags & PGET_CANDEBUG) != 0) { 350*fa3935bcSMikolaj Golub error = p_candebug(curthread, p); 351*fa3935bcSMikolaj Golub if (error != 0) 352*fa3935bcSMikolaj Golub goto errout; 353*fa3935bcSMikolaj Golub } 354*fa3935bcSMikolaj Golub if ((flags & PGET_ISCURRENT) != 0 && curproc != p) { 355*fa3935bcSMikolaj Golub error = EPERM; 356*fa3935bcSMikolaj Golub goto errout; 357*fa3935bcSMikolaj Golub } 358*fa3935bcSMikolaj Golub if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) { 359*fa3935bcSMikolaj Golub error = ESRCH; 360*fa3935bcSMikolaj Golub goto errout; 361*fa3935bcSMikolaj Golub } 362*fa3935bcSMikolaj Golub if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) { 363*fa3935bcSMikolaj Golub /* 364*fa3935bcSMikolaj Golub * XXXRW: Not clear ESRCH is the right error during proc 365*fa3935bcSMikolaj Golub * execve(). 366*fa3935bcSMikolaj Golub */ 367*fa3935bcSMikolaj Golub error = ESRCH; 368*fa3935bcSMikolaj Golub goto errout; 369*fa3935bcSMikolaj Golub } 370*fa3935bcSMikolaj Golub if ((flags & PGET_HOLD) != 0) { 371*fa3935bcSMikolaj Golub _PHOLD(p); 372*fa3935bcSMikolaj Golub PROC_UNLOCK(p); 373*fa3935bcSMikolaj Golub } 374*fa3935bcSMikolaj Golub *pp = p; 375*fa3935bcSMikolaj Golub return (0); 376*fa3935bcSMikolaj Golub errout: 377*fa3935bcSMikolaj Golub PROC_UNLOCK(p); 378*fa3935bcSMikolaj Golub return (error); 379*fa3935bcSMikolaj Golub } 380*fa3935bcSMikolaj Golub 381*fa3935bcSMikolaj Golub /* 382f591779bSSeigo Tanimura * Create a new process group. 383f591779bSSeigo Tanimura * pgid must be equal to the pid of p. 384f591779bSSeigo Tanimura * Begin a new session if required. 385df8bae1dSRodney W. Grimes */ 38626f9a767SRodney W. Grimes int 387f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess) 388df8bae1dSRodney W. Grimes register struct proc *p; 389df8bae1dSRodney W. Grimes pid_t pgid; 390f591779bSSeigo Tanimura struct pgrp *pgrp; 391f591779bSSeigo Tanimura struct session *sess; 392df8bae1dSRodney W. Grimes { 393f591779bSSeigo Tanimura struct pgrp *pgrp2; 394df8bae1dSRodney W. Grimes 395f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 396f591779bSSeigo Tanimura 397f591779bSSeigo Tanimura KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 398f591779bSSeigo Tanimura KASSERT(p->p_pid == pgid, 399f591779bSSeigo Tanimura ("enterpgrp: new pgrp and pid != pgid")); 400f591779bSSeigo Tanimura 401f591779bSSeigo Tanimura pgrp2 = pgfind(pgid); 402f591779bSSeigo Tanimura 403f591779bSSeigo Tanimura KASSERT(pgrp2 == NULL, 404f591779bSSeigo Tanimura ("enterpgrp: pgrp with pgid exists")); 4055526d2d9SEivind Eklund KASSERT(!SESS_LEADER(p), 4065526d2d9SEivind Eklund ("enterpgrp: session leader attempted setpgrp")); 407219cbf59SEivind Eklund 4086008862bSJohn Baldwin mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 409df8bae1dSRodney W. Grimes 410f591779bSSeigo Tanimura if (sess != NULL) { 411df8bae1dSRodney W. Grimes /* 412df8bae1dSRodney W. Grimes * new session 413df8bae1dSRodney W. Grimes */ 4146008862bSJohn Baldwin mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 415f591779bSSeigo Tanimura PROC_LOCK(p); 416f591779bSSeigo Tanimura p->p_flag &= ~P_CONTROLT; 417f591779bSSeigo Tanimura PROC_UNLOCK(p); 418f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 419df8bae1dSRodney W. Grimes sess->s_leader = p; 420643a8daaSDon Lewis sess->s_sid = p->p_pid; 421bc093719SEd Schouten refcount_init(&sess->s_count, 1); 422df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 4238dc9b4cfSEd Schouten sess->s_ttydp = NULL; 424df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 425df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 426df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 427df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 4285526d2d9SEivind Eklund KASSERT(p == curproc, 4295526d2d9SEivind Eklund ("enterpgrp: mksession and p != curproc")); 430df8bae1dSRodney W. Grimes } else { 431df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 432bc093719SEd Schouten sess_hold(pgrp->pg_session); 433f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 434df8bae1dSRodney W. Grimes } 435df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 436b75356e1SJeffrey Hsu LIST_INIT(&pgrp->pg_members); 437f591779bSSeigo Tanimura 438f591779bSSeigo Tanimura /* 439f089b570SJohn Baldwin * As we have an exclusive lock of proctree_lock, 440f591779bSSeigo Tanimura * this should not deadlock. 441f591779bSSeigo Tanimura */ 442b75356e1SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 443df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 444831d27a9SDon Lewis SLIST_INIT(&pgrp->pg_sigiolst); 445f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 446f591779bSSeigo Tanimura 447f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 448f591779bSSeigo Tanimura 449df8bae1dSRodney W. Grimes return (0); 450f591779bSSeigo Tanimura } 451f591779bSSeigo Tanimura 452f591779bSSeigo Tanimura /* 453f591779bSSeigo Tanimura * Move p to an existing process group 454f591779bSSeigo Tanimura */ 455f591779bSSeigo Tanimura int 456f591779bSSeigo Tanimura enterthispgrp(p, pgrp) 457f591779bSSeigo Tanimura register struct proc *p; 458f591779bSSeigo Tanimura struct pgrp *pgrp; 459f591779bSSeigo Tanimura { 460f089b570SJohn Baldwin 461f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 462f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 463f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 464f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 465f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 466f591779bSSeigo Tanimura KASSERT(pgrp->pg_session == p->p_session, 467f591779bSSeigo Tanimura ("%s: pgrp's session %p, p->p_session %p.\n", 468f591779bSSeigo Tanimura __func__, 469f591779bSSeigo Tanimura pgrp->pg_session, 470f591779bSSeigo Tanimura p->p_session)); 471f591779bSSeigo Tanimura KASSERT(pgrp != p->p_pgrp, 472f591779bSSeigo Tanimura ("%s: p belongs to pgrp.", __func__)); 473f591779bSSeigo Tanimura 474f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 475f591779bSSeigo Tanimura 476f591779bSSeigo Tanimura return (0); 477f591779bSSeigo Tanimura } 478f591779bSSeigo Tanimura 479f591779bSSeigo Tanimura /* 480f591779bSSeigo Tanimura * Move p to a process group 481f591779bSSeigo Tanimura */ 482f591779bSSeigo Tanimura static void 483f591779bSSeigo Tanimura doenterpgrp(p, pgrp) 484f591779bSSeigo Tanimura struct proc *p; 485f591779bSSeigo Tanimura struct pgrp *pgrp; 486f591779bSSeigo Tanimura { 487f591779bSSeigo Tanimura struct pgrp *savepgrp; 488f591779bSSeigo Tanimura 489f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 490f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 491f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 492f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 493f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 494f591779bSSeigo Tanimura 495f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 496df8bae1dSRodney W. Grimes 497df8bae1dSRodney W. Grimes /* 498df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 499df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 500df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 501df8bae1dSRodney W. Grimes */ 502df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 503df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 504df8bae1dSRodney W. Grimes 505f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 506f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 50715e9ec51SJohn Baldwin PROC_LOCK(p); 508b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 509df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 51015e9ec51SJohn Baldwin PROC_UNLOCK(p); 511f591779bSSeigo Tanimura LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 512f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 513f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 514f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 515f591779bSSeigo Tanimura pgdelete(savepgrp); 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes 518df8bae1dSRodney W. Grimes /* 519df8bae1dSRodney W. Grimes * remove process from process group 520df8bae1dSRodney W. Grimes */ 52126f9a767SRodney W. Grimes int 522df8bae1dSRodney W. Grimes leavepgrp(p) 523df8bae1dSRodney W. Grimes register struct proc *p; 524df8bae1dSRodney W. Grimes { 525f591779bSSeigo Tanimura struct pgrp *savepgrp; 526df8bae1dSRodney W. Grimes 527f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 528f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 529f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 53015e9ec51SJohn Baldwin PROC_LOCK(p); 531b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 53215e9ec51SJohn Baldwin p->p_pgrp = NULL; 53315e9ec51SJohn Baldwin PROC_UNLOCK(p); 534f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 535f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 536f591779bSSeigo Tanimura pgdelete(savepgrp); 537df8bae1dSRodney W. Grimes return (0); 538df8bae1dSRodney W. Grimes } 539df8bae1dSRodney W. Grimes 540df8bae1dSRodney W. Grimes /* 541df8bae1dSRodney W. Grimes * delete a process group 542df8bae1dSRodney W. Grimes */ 54387b6de2bSPoul-Henning Kamp static void 544df8bae1dSRodney W. Grimes pgdelete(pgrp) 545df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 546df8bae1dSRodney W. Grimes { 547f591779bSSeigo Tanimura struct session *savesess; 548bc093719SEd Schouten struct tty *tp; 549f591779bSSeigo Tanimura 550f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 551f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 552f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 553f591779bSSeigo Tanimura 554831d27a9SDon Lewis /* 555831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 556831d27a9SDon Lewis * F_SETOWN with our pgid. 557831d27a9SDon Lewis */ 558831d27a9SDon Lewis funsetownlst(&pgrp->pg_sigiolst); 559831d27a9SDon Lewis 560e649887bSAlfred Perlstein PGRP_LOCK(pgrp); 561bc093719SEd Schouten tp = pgrp->pg_session->s_ttyp; 562b75356e1SJeffrey Hsu LIST_REMOVE(pgrp, pg_hash); 563f591779bSSeigo Tanimura savesess = pgrp->pg_session; 564f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 565bc093719SEd Schouten 566bc093719SEd Schouten /* Remove the reference to the pgrp before deallocating it. */ 567bc093719SEd Schouten if (tp != NULL) { 568bc093719SEd Schouten tty_lock(tp); 569bc093719SEd Schouten tty_rel_pgrp(tp, pgrp); 570bc093719SEd Schouten } 571bc093719SEd Schouten 5726041fa0aSSeigo Tanimura mtx_destroy(&pgrp->pg_mtx); 5731ede983cSDag-Erling Smørgrav free(pgrp, M_PGRP); 574bc093719SEd Schouten sess_release(savesess); 575df8bae1dSRodney W. Grimes } 576df8bae1dSRodney W. Grimes 57702e878d9SJohn Baldwin static void 57802e878d9SJohn Baldwin pgadjustjobc(pgrp, entering) 57902e878d9SJohn Baldwin struct pgrp *pgrp; 58002e878d9SJohn Baldwin int entering; 58102e878d9SJohn Baldwin { 58202e878d9SJohn Baldwin 58302e878d9SJohn Baldwin PGRP_LOCK(pgrp); 58402e878d9SJohn Baldwin if (entering) 58502e878d9SJohn Baldwin pgrp->pg_jobc++; 58602e878d9SJohn Baldwin else { 58702e878d9SJohn Baldwin --pgrp->pg_jobc; 58802e878d9SJohn Baldwin if (pgrp->pg_jobc == 0) 58902e878d9SJohn Baldwin orphanpg(pgrp); 59002e878d9SJohn Baldwin } 59102e878d9SJohn Baldwin PGRP_UNLOCK(pgrp); 59202e878d9SJohn Baldwin } 59302e878d9SJohn Baldwin 594df8bae1dSRodney W. Grimes /* 595df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 596df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 597df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 598df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 599df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 600df8bae1dSRodney W. Grimes * process group and that of its children. 601df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 602df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 603df8bae1dSRodney W. Grimes */ 60426f9a767SRodney W. Grimes void 605df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 606df8bae1dSRodney W. Grimes register struct proc *p; 607df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 608df8bae1dSRodney W. Grimes int entering; 609df8bae1dSRodney W. Grimes { 610df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 611f591779bSSeigo Tanimura register struct session *mysession; 612f591779bSSeigo Tanimura 613f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 614f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 615f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 616f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 617df8bae1dSRodney W. Grimes 618df8bae1dSRodney W. Grimes /* 619df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 620df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 621df8bae1dSRodney W. Grimes */ 622f591779bSSeigo Tanimura mysession = pgrp->pg_session; 623df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 62402e878d9SJohn Baldwin hispgrp->pg_session == mysession) 62502e878d9SJohn Baldwin pgadjustjobc(pgrp, entering); 626df8bae1dSRodney W. Grimes 627df8bae1dSRodney W. Grimes /* 628df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 629df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 630df8bae1dSRodney W. Grimes * process groups. 631df8bae1dSRodney W. Grimes */ 632f591779bSSeigo Tanimura LIST_FOREACH(p, &p->p_children, p_sibling) { 63302e878d9SJohn Baldwin hispgrp = p->p_pgrp; 63402e878d9SJohn Baldwin if (hispgrp == pgrp || 63502e878d9SJohn Baldwin hispgrp->pg_session != mysession) 63602e878d9SJohn Baldwin continue; 63702e878d9SJohn Baldwin PROC_LOCK(p); 63802e878d9SJohn Baldwin if (p->p_state == PRS_ZOMBIE) { 63902e878d9SJohn Baldwin PROC_UNLOCK(p); 64002e878d9SJohn Baldwin continue; 641df8bae1dSRodney W. Grimes } 64202e878d9SJohn Baldwin PROC_UNLOCK(p); 64302e878d9SJohn Baldwin pgadjustjobc(hispgrp, entering); 644f591779bSSeigo Tanimura } 645dfd5dee1SPeter Wemm } 646df8bae1dSRodney W. Grimes 647df8bae1dSRodney W. Grimes /* 648df8bae1dSRodney W. Grimes * A process group has become orphaned; 649df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 650df8bae1dSRodney W. Grimes * hang-up all process in that group. 651df8bae1dSRodney W. Grimes */ 652df8bae1dSRodney W. Grimes static void 653df8bae1dSRodney W. Grimes orphanpg(pg) 654df8bae1dSRodney W. Grimes struct pgrp *pg; 655df8bae1dSRodney W. Grimes { 656df8bae1dSRodney W. Grimes register struct proc *p; 657df8bae1dSRodney W. Grimes 658f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pg, MA_OWNED); 659f591779bSSeigo Tanimura 6601b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 66102e878d9SJohn Baldwin PROC_LOCK(p); 662e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 66302e878d9SJohn Baldwin PROC_UNLOCK(p); 6641b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 66515e9ec51SJohn Baldwin PROC_LOCK(p); 6668451d0ddSKip Macy kern_psignal(p, SIGHUP); 6678451d0ddSKip Macy kern_psignal(p, SIGCONT); 66815e9ec51SJohn Baldwin PROC_UNLOCK(p); 669df8bae1dSRodney W. Grimes } 670df8bae1dSRodney W. Grimes return; 671df8bae1dSRodney W. Grimes } 67202e878d9SJohn Baldwin PROC_UNLOCK(p); 673df8bae1dSRodney W. Grimes } 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes 676572b4402SPoul-Henning Kamp void 677bc093719SEd Schouten sess_hold(struct session *s) 678572b4402SPoul-Henning Kamp { 679572b4402SPoul-Henning Kamp 680bc093719SEd Schouten refcount_acquire(&s->s_count); 681bc093719SEd Schouten } 682bc093719SEd Schouten 683bc093719SEd Schouten void 684bc093719SEd Schouten sess_release(struct session *s) 685bc093719SEd Schouten { 686bc093719SEd Schouten 687bc093719SEd Schouten if (refcount_release(&s->s_count)) { 688bc093719SEd Schouten if (s->s_ttyp != NULL) { 689bc093719SEd Schouten tty_lock(s->s_ttyp); 690bc093719SEd Schouten tty_rel_sess(s->s_ttyp, s); 691bc093719SEd Schouten } 692572b4402SPoul-Henning Kamp mtx_destroy(&s->s_mtx); 6931ede983cSDag-Erling Smørgrav free(s, M_SESSION); 694572b4402SPoul-Henning Kamp } 695572b4402SPoul-Henning Kamp } 696572b4402SPoul-Henning Kamp 697831031ceSBruce Evans #include "opt_ddb.h" 698831031ceSBruce Evans #ifdef DDB 699831031ceSBruce Evans #include <ddb/ddb.h> 700831031ceSBruce Evans 701831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump) 702df8bae1dSRodney W. Grimes { 703df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 704df8bae1dSRodney W. Grimes register struct proc *p; 705876a94eeSBruce Evans register int i; 706df8bae1dSRodney W. Grimes 707b75356e1SJeffrey Hsu for (i = 0; i <= pgrphash; i++) { 7081b727751SPoul-Henning Kamp if (!LIST_EMPTY(&pgrphashtbl[i])) { 709df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 7101b727751SPoul-Henning Kamp LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 711ac1e407bSBruce Evans printf( 712ac1e407bSBruce Evans "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 713ac1e407bSBruce Evans (void *)pgrp, (long)pgrp->pg_id, 714ac1e407bSBruce Evans (void *)pgrp->pg_session, 715b75356e1SJeffrey Hsu pgrp->pg_session->s_count, 7161b727751SPoul-Henning Kamp (void *)LIST_FIRST(&pgrp->pg_members)); 7171b727751SPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 718ac1e407bSBruce Evans printf("\t\tpid %ld addr %p pgrp %p\n", 719ac1e407bSBruce Evans (long)p->p_pid, (void *)p, 720ac1e407bSBruce Evans (void *)p->p_pgrp); 721df8bae1dSRodney W. Grimes } 722df8bae1dSRodney W. Grimes } 723df8bae1dSRodney W. Grimes } 724df8bae1dSRodney W. Grimes } 725df8bae1dSRodney W. Grimes } 726831031ceSBruce Evans #endif /* DDB */ 727972f9b20SPoul-Henning Kamp 728972f9b20SPoul-Henning Kamp /* 729f8d90480SAttilio Rao * Calculate the kinfo_proc members which contain process-wide 730f8d90480SAttilio Rao * informations. 731f8d90480SAttilio Rao * Must be called with the target process locked. 732f8d90480SAttilio Rao */ 733f8d90480SAttilio Rao static void 734f8d90480SAttilio Rao fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) 735f8d90480SAttilio Rao { 736f8d90480SAttilio Rao struct thread *td; 737f8d90480SAttilio Rao 738f8d90480SAttilio Rao PROC_LOCK_ASSERT(p, MA_OWNED); 739f8d90480SAttilio Rao 740f8d90480SAttilio Rao kp->ki_estcpu = 0; 741f8d90480SAttilio Rao kp->ki_pctcpu = 0; 742f8d90480SAttilio Rao FOREACH_THREAD_IN_PROC(p, td) { 743f8d90480SAttilio Rao thread_lock(td); 744f8d90480SAttilio Rao kp->ki_pctcpu += sched_pctcpu(td); 745f8d90480SAttilio Rao kp->ki_estcpu += td->td_estcpu; 746f8d90480SAttilio Rao thread_unlock(td); 747f8d90480SAttilio Rao } 748f8d90480SAttilio Rao } 749f8d90480SAttilio Rao 750f8d90480SAttilio Rao /* 7515032ff81SDon Lewis * Clear kinfo_proc and fill in any information that is common 7525032ff81SDon Lewis * to all threads in the process. 75365c9b430SJohn Baldwin * Must be called with the target process locked. 754972f9b20SPoul-Henning Kamp */ 7555032ff81SDon Lewis static void 7565032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 757972f9b20SPoul-Henning Kamp { 7584093529dSJeff Roberson struct thread *td0; 7591f7d2501SKirk McKusick struct tty *tp; 7601f7d2501SKirk McKusick struct session *sp; 761d079d0a0SPawel Jakub Dawidek struct ucred *cred; 76290af4afaSJohn Baldwin struct sigacts *ps; 763972f9b20SPoul-Henning Kamp 764374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 7651f7d2501SKirk McKusick bzero(kp, sizeof(*kp)); 766972f9b20SPoul-Henning Kamp 7671f7d2501SKirk McKusick kp->ki_structsize = sizeof(*kp); 7681f7d2501SKirk McKusick kp->ki_paddr = p; 7696617724cSJeff Roberson kp->ki_addr =/* p->p_addr; */0; /* XXX */ 7701f7d2501SKirk McKusick kp->ki_args = p->p_args; 7711f7d2501SKirk McKusick kp->ki_textvp = p->p_textvp; 7726c84de02SJohn Baldwin #ifdef KTRACE 773a5881ea5SJohn Baldwin kp->ki_tracep = p->p_tracevp; 7746c84de02SJohn Baldwin kp->ki_traceflag = p->p_traceflag; 7756c84de02SJohn Baldwin #endif 7761f7d2501SKirk McKusick kp->ki_fd = p->p_fd; 7771f7d2501SKirk McKusick kp->ki_vmspace = p->p_vmspace; 778cefcecbeSPawel Jakub Dawidek kp->ki_flag = p->p_flag; 779d079d0a0SPawel Jakub Dawidek cred = p->p_ucred; 780d079d0a0SPawel Jakub Dawidek if (cred) { 781d079d0a0SPawel Jakub Dawidek kp->ki_uid = cred->cr_uid; 782d079d0a0SPawel Jakub Dawidek kp->ki_ruid = cred->cr_ruid; 783d079d0a0SPawel Jakub Dawidek kp->ki_svuid = cred->cr_svuid; 78496fcc75fSRobert Watson kp->ki_cr_flags = 0; 78596fcc75fSRobert Watson if (cred->cr_flags & CRED_FLAG_CAPMODE) 78696fcc75fSRobert Watson kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE; 7871b5768beSBrooks Davis /* XXX bde doesn't like KI_NGROUPS */ 7881b5768beSBrooks Davis if (cred->cr_ngroups > KI_NGROUPS) { 7891b5768beSBrooks Davis kp->ki_ngroups = KI_NGROUPS; 7901b5768beSBrooks Davis kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 7911b5768beSBrooks Davis } else 792838d9858SBrooks Davis kp->ki_ngroups = cred->cr_ngroups; 7931b5768beSBrooks Davis bcopy(cred->cr_groups, kp->ki_groups, 7941b5768beSBrooks Davis kp->ki_ngroups * sizeof(gid_t)); 795d079d0a0SPawel Jakub Dawidek kp->ki_rgid = cred->cr_rgid; 796d079d0a0SPawel Jakub Dawidek kp->ki_svgid = cred->cr_svgid; 797cefcecbeSPawel Jakub Dawidek /* If jailed(cred), emulate the old P_JAILED flag. */ 798c78941e6SPawel Jakub Dawidek if (jailed(cred)) { 799cefcecbeSPawel Jakub Dawidek kp->ki_flag |= P_JAILED; 8000304c731SJamie Gritton /* If inside the jail, use 0 as a jail ID. */ 8010304c731SJamie Gritton if (cred->cr_prison != curthread->td_ucred->cr_prison) 802c78941e6SPawel Jakub Dawidek kp->ki_jid = cred->cr_prison->pr_id; 803c78941e6SPawel Jakub Dawidek } 8047123f4cdSEdward Tomasz Napierala strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name, 8057123f4cdSEdward Tomasz Napierala sizeof(kp->ki_loginclass)); 806972f9b20SPoul-Henning Kamp } 80790af4afaSJohn Baldwin ps = p->p_sigacts; 808d079d0a0SPawel Jakub Dawidek if (ps) { 80990af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 81090af4afaSJohn Baldwin kp->ki_sigignore = ps->ps_sigignore; 81190af4afaSJohn Baldwin kp->ki_sigcatch = ps->ps_sigcatch; 81290af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 813d8c85307SJulian Elischer } 814e602ba25SJulian Elischer if (p->p_state != PRS_NEW && 815e602ba25SJulian Elischer p->p_state != PRS_ZOMBIE && 816e602ba25SJulian Elischer p->p_vmspace != NULL) { 8171f7d2501SKirk McKusick struct vmspace *vm = p->p_vmspace; 818cd73303cSDavid Greenman 8191f7d2501SKirk McKusick kp->ki_size = vm->vm_map.size; 8201f7d2501SKirk McKusick kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 821ceff7f2aSTim J. Robbins FOREACH_THREAD_IN_PROC(p, td0) { 822ceff7f2aSTim J. Robbins if (!TD_IS_SWAPPED(td0)) 823ceff7f2aSTim J. Robbins kp->ki_rssize += td0->td_kstack_pages; 824ceff7f2aSTim J. Robbins } 8251f7d2501SKirk McKusick kp->ki_swrss = vm->vm_swrss; 8261f7d2501SKirk McKusick kp->ki_tsize = vm->vm_tsize; 8271f7d2501SKirk McKusick kp->ki_dsize = vm->vm_dsize; 8281f7d2501SKirk McKusick kp->ki_ssize = vm->vm_ssize; 8295032ff81SDon Lewis } else if (p->p_state == PRS_ZOMBIE) 8305032ff81SDon Lewis kp->ki_stat = SZOMB; 831b61ce5b0SJeff Roberson if (kp->ki_flag & P_INMEM) 832b61ce5b0SJeff Roberson kp->ki_sflag = PS_INMEM; 833b61ce5b0SJeff Roberson else 834b61ce5b0SJeff Roberson kp->ki_sflag = 0; 83554b0e65fSJeff Roberson /* Calculate legacy swtime as seconds since 'swtick'. */ 83654b0e65fSJeff Roberson kp->ki_swtime = (ticks - p->p_swtick) / hz; 837cebabef0SPawel Jakub Dawidek kp->ki_pid = p->p_pid; 838cebabef0SPawel Jakub Dawidek kp->ki_nice = p->p_nice; 83978c85e8dSJohn Baldwin kp->ki_start = p->p_stats->p_start; 84078c85e8dSJohn Baldwin timevaladd(&kp->ki_start, &boottime); 841a1fe14bcSAttilio Rao PROC_SLOCK(p); 8428e6fa660SJohn Baldwin rufetch(p, &kp->ki_rusage); 8438e6fa660SJohn Baldwin kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 84478c85e8dSJohn Baldwin calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 845a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 84678c85e8dSJohn Baldwin calccru(p, &kp->ki_childutime, &kp->ki_childstime); 847a37e14e1SEdward Tomasz Napierala /* Some callers want child times in a single value. */ 84878c85e8dSJohn Baldwin kp->ki_childtime = kp->ki_childstime; 84978c85e8dSJohn Baldwin timevaladd(&kp->ki_childtime, &kp->ki_childutime); 850a37e14e1SEdward Tomasz Napierala 851f591779bSSeigo Tanimura tp = NULL; 8521f7d2501SKirk McKusick if (p->p_pgrp) { 8531f7d2501SKirk McKusick kp->ki_pgid = p->p_pgrp->pg_id; 8541f7d2501SKirk McKusick kp->ki_jobc = p->p_pgrp->pg_jobc; 8551f7d2501SKirk McKusick sp = p->p_pgrp->pg_session; 8561f7d2501SKirk McKusick 8571f7d2501SKirk McKusick if (sp != NULL) { 8581f7d2501SKirk McKusick kp->ki_sid = sp->s_sid; 859f591779bSSeigo Tanimura SESS_LOCK(sp); 860e80fb434SRobert Drehmel strlcpy(kp->ki_login, sp->s_login, 861e80fb434SRobert Drehmel sizeof(kp->ki_login)); 8621f7d2501SKirk McKusick if (sp->s_ttyvp) 863b8e6bf1eSJohn Baldwin kp->ki_kiflag |= KI_CTTY; 8641f7d2501SKirk McKusick if (SESS_LEADER(p)) 8651f7d2501SKirk McKusick kp->ki_kiflag |= KI_SLEADER; 866bc093719SEd Schouten /* XXX proctree_lock */ 867f591779bSSeigo Tanimura tp = sp->s_ttyp; 868f591779bSSeigo Tanimura SESS_UNLOCK(sp); 869cd73303cSDavid Greenman } 870cd73303cSDavid Greenman } 871f591779bSSeigo Tanimura if ((p->p_flag & P_CONTROLT) && tp != NULL) { 872bc093719SEd Schouten kp->ki_tdev = tty_udev(tp); 8731f7d2501SKirk McKusick kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 8741f7d2501SKirk McKusick if (tp->t_session) 8751f7d2501SKirk McKusick kp->ki_tsid = tp->t_session->s_sid; 876972f9b20SPoul-Henning Kamp } else 877f3732fd1SPoul-Henning Kamp kp->ki_tdev = NODEV; 87813b762a3SEd Maste if (p->p_comm[0] != '\0') 879e80fb434SRobert Drehmel strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 880078842c5SGarance A Drosehn if (p->p_sysent && p->p_sysent->sv_name != NULL && 881078842c5SGarance A Drosehn p->p_sysent->sv_name[0] != '\0') 882078842c5SGarance A Drosehn strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 8831d9c5696SJuli Mallett kp->ki_siglist = p->p_siglist; 8841f7d2501SKirk McKusick kp->ki_xstat = p->p_xstat; 8851f7d2501SKirk McKusick kp->ki_acflag = p->p_acflag; 8861f7d2501SKirk McKusick kp->ki_lock = p->p_lock; 88742a4ed99SJohn Baldwin if (p->p_pptr) 88842a4ed99SJohn Baldwin kp->ki_ppid = p->p_pptr->p_pid; 889972f9b20SPoul-Henning Kamp } 890972f9b20SPoul-Henning Kamp 8915032ff81SDon Lewis /* 89200305546SKonstantin Belousov * Fill in information that is thread specific. Must be called with 89300305546SKonstantin Belousov * target process locked. If 'preferthread' is set, overwrite certain 89400305546SKonstantin Belousov * process-related fields that are maintained for both threads and 89500305546SKonstantin Belousov * processes. 8965032ff81SDon Lewis */ 8975032ff81SDon Lewis static void 898d92909c1SRobert Watson fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 8995032ff81SDon Lewis { 9005032ff81SDon Lewis struct proc *p; 9015032ff81SDon Lewis 9025032ff81SDon Lewis p = td->td_proc; 9036239ef1dSEd Maste kp->ki_tdaddr = td; 904374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 9055032ff81SDon Lewis 9062417d97eSJohn Baldwin if (preferthread) 9072417d97eSJohn Baldwin PROC_SLOCK(p); 908982d11f8SJeff Roberson thread_lock(td); 9095032ff81SDon Lewis if (td->td_wmesg != NULL) 9105032ff81SDon Lewis strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 9115032ff81SDon Lewis else 9125032ff81SDon Lewis bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 913925af544SBjoern A. Zeeb strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)); 9145032ff81SDon Lewis if (TD_ON_LOCK(td)) { 9155032ff81SDon Lewis kp->ki_kiflag |= KI_LOCKBLOCK; 9165032ff81SDon Lewis strlcpy(kp->ki_lockname, td->td_lockname, 9175032ff81SDon Lewis sizeof(kp->ki_lockname)); 9185032ff81SDon Lewis } else { 9195032ff81SDon Lewis kp->ki_kiflag &= ~KI_LOCKBLOCK; 9205032ff81SDon Lewis bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 9215032ff81SDon Lewis } 9225032ff81SDon Lewis 9236617724cSJeff Roberson if (p->p_state == PRS_NORMAL) { /* approximate. */ 9245032ff81SDon Lewis if (TD_ON_RUNQ(td) || 9255032ff81SDon Lewis TD_CAN_RUN(td) || 9265032ff81SDon Lewis TD_IS_RUNNING(td)) { 9275032ff81SDon Lewis kp->ki_stat = SRUN; 9285032ff81SDon Lewis } else if (P_SHOULDSTOP(p)) { 9295032ff81SDon Lewis kp->ki_stat = SSTOP; 9305032ff81SDon Lewis } else if (TD_IS_SLEEPING(td)) { 9315032ff81SDon Lewis kp->ki_stat = SSLEEP; 9325032ff81SDon Lewis } else if (TD_ON_LOCK(td)) { 9335032ff81SDon Lewis kp->ki_stat = SLOCK; 9345032ff81SDon Lewis } else { 9355032ff81SDon Lewis kp->ki_stat = SWAIT; 9365032ff81SDon Lewis } 9373357835aSDavid Xu } else if (p->p_state == PRS_ZOMBIE) { 9383357835aSDavid Xu kp->ki_stat = SZOMB; 9395032ff81SDon Lewis } else { 9405032ff81SDon Lewis kp->ki_stat = SIDL; 9415032ff81SDon Lewis } 9425032ff81SDon Lewis 9435032ff81SDon Lewis /* Things in the thread */ 9445032ff81SDon Lewis kp->ki_wchan = td->td_wchan; 9455032ff81SDon Lewis kp->ki_pri.pri_level = td->td_priority; 9465032ff81SDon Lewis kp->ki_pri.pri_native = td->td_base_pri; 9475032ff81SDon Lewis kp->ki_lastcpu = td->td_lastcpu; 9485032ff81SDon Lewis kp->ki_oncpu = td->td_oncpu; 9495032ff81SDon Lewis kp->ki_tdflags = td->td_flags; 9505032ff81SDon Lewis kp->ki_tid = td->td_tid; 9515032ff81SDon Lewis kp->ki_numthreads = p->p_numthreads; 9525032ff81SDon Lewis kp->ki_pcb = td->td_pcb; 9535032ff81SDon Lewis kp->ki_kstack = (void *)td->td_kstack; 95454b0e65fSJeff Roberson kp->ki_slptime = (ticks - td->td_slptick) / hz; 9558460a577SJohn Birrell kp->ki_pri.pri_class = td->td_pri_class; 9568460a577SJohn Birrell kp->ki_pri.pri_user = td->td_user_pri; 9575032ff81SDon Lewis 958f8d90480SAttilio Rao if (preferthread) { 9592417d97eSJohn Baldwin rufetchtd(td, &kp->ki_rusage); 960213c077fSKonstantin Belousov kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 961f8d90480SAttilio Rao kp->ki_pctcpu = sched_pctcpu(td); 962f8d90480SAttilio Rao kp->ki_estcpu = td->td_estcpu; 963f8d90480SAttilio Rao } 964d92909c1SRobert Watson 9655032ff81SDon Lewis /* We can't get this anymore but ps etc never used it anyway. */ 9665032ff81SDon Lewis kp->ki_rqindex = 0; 9675032ff81SDon Lewis 96800305546SKonstantin Belousov if (preferthread) 96900305546SKonstantin Belousov kp->ki_siglist = td->td_siglist; 9705032ff81SDon Lewis kp->ki_sigmask = td->td_sigmask; 971982d11f8SJeff Roberson thread_unlock(td); 9722417d97eSJohn Baldwin if (preferthread) 9732417d97eSJohn Baldwin PROC_SUNLOCK(p); 9745032ff81SDon Lewis } 9755032ff81SDon Lewis 9765032ff81SDon Lewis /* 9775032ff81SDon Lewis * Fill in a kinfo_proc structure for the specified process. 9785032ff81SDon Lewis * Must be called with the target process locked. 9795032ff81SDon Lewis */ 9805032ff81SDon Lewis void 9815032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 9825032ff81SDon Lewis { 9835032ff81SDon Lewis 984f8d90480SAttilio Rao MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 985f8d90480SAttilio Rao 9865032ff81SDon Lewis fill_kinfo_proc_only(p, kp); 987d92909c1SRobert Watson fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 988f8d90480SAttilio Rao fill_kinfo_aggregate(p, kp); 9895032ff81SDon Lewis } 9905032ff81SDon Lewis 9918b059651SDavid Schultz struct pstats * 9928b059651SDavid Schultz pstats_alloc(void) 9938b059651SDavid Schultz { 9948b059651SDavid Schultz 9958b059651SDavid Schultz return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 9968b059651SDavid Schultz } 9978b059651SDavid Schultz 9988b059651SDavid Schultz /* 9998b059651SDavid Schultz * Copy parts of p_stats; zero the rest of p_stats (statistics). 10008b059651SDavid Schultz */ 10018b059651SDavid Schultz void 10028b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst) 10038b059651SDavid Schultz { 10048b059651SDavid Schultz 10058b059651SDavid Schultz bzero(&dst->pstat_startzero, 10066db36923SDavid Schultz __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 10078b059651SDavid Schultz bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 10086db36923SDavid Schultz __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 10098b059651SDavid Schultz } 10108b059651SDavid Schultz 10118b059651SDavid Schultz void 10128b059651SDavid Schultz pstats_free(struct pstats *ps) 10138b059651SDavid Schultz { 10148b059651SDavid Schultz 10158b059651SDavid Schultz free(ps, M_SUBPROC); 10168b059651SDavid Schultz } 10178b059651SDavid Schultz 10188b059651SDavid Schultz /* 101942a4ed99SJohn Baldwin * Locate a zombie process by number 102042a4ed99SJohn Baldwin */ 102142a4ed99SJohn Baldwin struct proc * 10223ce93e4eSPoul-Henning Kamp zpfind(pid_t pid) 10233ce93e4eSPoul-Henning Kamp { 10243ce93e4eSPoul-Henning Kamp struct proc *p; 10253ce93e4eSPoul-Henning Kamp 10261005a129SJohn Baldwin sx_slock(&allproc_lock); 10271b727751SPoul-Henning Kamp LIST_FOREACH(p, &zombproc, p_list) 102833a9ed9dSJohn Baldwin if (p->p_pid == pid) { 102933a9ed9dSJohn Baldwin PROC_LOCK(p); 1030c0c25570SJake Burkholder break; 103133a9ed9dSJohn Baldwin } 10321005a129SJohn Baldwin sx_sunlock(&allproc_lock); 10333ce93e4eSPoul-Henning Kamp return (p); 10343ce93e4eSPoul-Henning Kamp } 10353ce93e4eSPoul-Henning Kamp 103630c6f34eSScott Long #define KERN_PROC_ZOMBMASK 0x3 103730c6f34eSScott Long #define KERN_PROC_NOTHREADS 0x4 10383ce93e4eSPoul-Henning Kamp 103905e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32 104005e06d11SKonstantin Belousov 104105e06d11SKonstantin Belousov /* 104205e06d11SKonstantin Belousov * This function is typically used to copy out the kernel address, so 104305e06d11SKonstantin Belousov * it can be replaced by assignment of zero. 104405e06d11SKonstantin Belousov */ 104505e06d11SKonstantin Belousov static inline uint32_t 104605e06d11SKonstantin Belousov ptr32_trim(void *ptr) 104705e06d11SKonstantin Belousov { 104805e06d11SKonstantin Belousov uintptr_t uptr; 104905e06d11SKonstantin Belousov 105005e06d11SKonstantin Belousov uptr = (uintptr_t)ptr; 105105e06d11SKonstantin Belousov return ((uptr > UINT_MAX) ? 0 : uptr); 105205e06d11SKonstantin Belousov } 105305e06d11SKonstantin Belousov 105405e06d11SKonstantin Belousov #define PTRTRIM_CP(src,dst,fld) \ 105505e06d11SKonstantin Belousov do { (dst).fld = ptr32_trim((src).fld); } while (0) 105605e06d11SKonstantin Belousov 105705e06d11SKonstantin Belousov static void 105805e06d11SKonstantin Belousov freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 105905e06d11SKonstantin Belousov { 106005e06d11SKonstantin Belousov int i; 106105e06d11SKonstantin Belousov 106205e06d11SKonstantin Belousov bzero(ki32, sizeof(struct kinfo_proc32)); 106305e06d11SKonstantin Belousov ki32->ki_structsize = sizeof(struct kinfo_proc32); 106405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_layout); 106505e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_args); 106605e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_paddr); 106705e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_addr); 106805e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_tracep); 106905e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_textvp); 107005e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_fd); 107105e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_vmspace); 107205e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_wchan); 107305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pid); 107405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ppid); 107505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pgid); 107605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tpgid); 107705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sid); 107805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tsid); 107905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_jobc); 108005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tdev); 108105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_siglist); 108205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigmask); 108305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigignore); 108405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigcatch); 108505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_uid); 108605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ruid); 108705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_svuid); 108805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rgid); 108905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_svgid); 109005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ngroups); 109105e06d11SKonstantin Belousov for (i = 0; i < KI_NGROUPS; i++) 109205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_groups[i]); 109305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_size); 109405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rssize); 109505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_swrss); 109605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tsize); 109705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_dsize); 109805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ssize); 109905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_xstat); 110005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_acflag); 110105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pctcpu); 110205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_estcpu); 110305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_slptime); 110405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_swtime); 110505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_runtime); 110605e06d11SKonstantin Belousov TV_CP(*ki, *ki32, ki_start); 110705e06d11SKonstantin Belousov TV_CP(*ki, *ki32, ki_childtime); 110805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_flag); 110905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_kiflag); 111005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_traceflag); 111105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_stat); 111205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_nice); 111305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_lock); 111405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rqindex); 111505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_oncpu); 111605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_lastcpu); 1117925af544SBjoern A. Zeeb bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1); 111805e06d11SKonstantin Belousov bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 111905e06d11SKonstantin Belousov bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 112005e06d11SKonstantin Belousov bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 112105e06d11SKonstantin Belousov bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 112205e06d11SKonstantin Belousov bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 11237123f4cdSEdward Tomasz Napierala bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); 112405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_cr_flags); 112505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_jid); 112605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_numthreads); 112705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tid); 112805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pri); 112905e06d11SKonstantin Belousov freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 113005e06d11SKonstantin Belousov freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 113105e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_pcb); 113205e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_kstack); 113305e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_udata); 113405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sflag); 113505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tdflags); 113605e06d11SKonstantin Belousov } 113705e06d11SKonstantin Belousov 113805e06d11SKonstantin Belousov static int 113905e06d11SKonstantin Belousov sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 114005e06d11SKonstantin Belousov { 114105e06d11SKonstantin Belousov struct kinfo_proc32 ki32; 114205e06d11SKonstantin Belousov int error; 114305e06d11SKonstantin Belousov 114405e06d11SKonstantin Belousov if (req->flags & SCTL_MASK32) { 114505e06d11SKonstantin Belousov freebsd32_kinfo_proc_out(ki, &ki32); 1146e68d26fdSKonstantin Belousov error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32)); 114705e06d11SKonstantin Belousov } else 1148e68d26fdSKonstantin Belousov error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)); 114905e06d11SKonstantin Belousov return (error); 115005e06d11SKonstantin Belousov } 115105e06d11SKonstantin Belousov #else 115205e06d11SKonstantin Belousov static int 115305e06d11SKonstantin Belousov sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 115405e06d11SKonstantin Belousov { 115505e06d11SKonstantin Belousov 1156e68d26fdSKonstantin Belousov return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc))); 115705e06d11SKonstantin Belousov } 115805e06d11SKonstantin Belousov #endif 115905e06d11SKonstantin Belousov 116065c9b430SJohn Baldwin /* 116165c9b430SJohn Baldwin * Must be called with the process locked and will return with it unlocked. 116265c9b430SJohn Baldwin */ 11633ce93e4eSPoul-Henning Kamp static int 116430c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 11653ce93e4eSPoul-Henning Kamp { 116630c6f34eSScott Long struct thread *td; 11671f7d2501SKirk McKusick struct kinfo_proc kinfo_proc; 116830c6f34eSScott Long int error = 0; 116933a9ed9dSJohn Baldwin struct proc *np; 11703ce93e4eSPoul-Henning Kamp pid_t pid = p->p_pid; 11713ce93e4eSPoul-Henning Kamp 117265c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1173f8d90480SAttilio Rao MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 117430c6f34eSScott Long 1175f8d90480SAttilio Rao fill_kinfo_proc(p, &kinfo_proc); 1176f8d90480SAttilio Rao if (flags & KERN_PROC_NOTHREADS) 117705e06d11SKonstantin Belousov error = sysctl_out_proc_copyout(&kinfo_proc, req); 1178f8d90480SAttilio Rao else { 117930c6f34eSScott Long FOREACH_THREAD_IN_PROC(p, td) { 1180d92909c1SRobert Watson fill_kinfo_thread(td, &kinfo_proc, 1); 118105e06d11SKonstantin Belousov error = sysctl_out_proc_copyout(&kinfo_proc, req); 118230c6f34eSScott Long if (error) 118330c6f34eSScott Long break; 118430c6f34eSScott Long } 118530c6f34eSScott Long } 118630c6f34eSScott Long PROC_UNLOCK(p); 11873ce93e4eSPoul-Henning Kamp if (error) 11883ce93e4eSPoul-Henning Kamp return (error); 118930c6f34eSScott Long if (flags & KERN_PROC_ZOMBMASK) 119033a9ed9dSJohn Baldwin np = zpfind(pid); 119133a9ed9dSJohn Baldwin else { 119233a9ed9dSJohn Baldwin if (pid == 0) 119333a9ed9dSJohn Baldwin return (0); 119433a9ed9dSJohn Baldwin np = pfind(pid); 119533a9ed9dSJohn Baldwin } 119633a9ed9dSJohn Baldwin if (np == NULL) 1197f308bdddSKevin Lo return (ESRCH); 119833a9ed9dSJohn Baldwin if (np != p) { 119933a9ed9dSJohn Baldwin PROC_UNLOCK(np); 1200f308bdddSKevin Lo return (ESRCH); 120133a9ed9dSJohn Baldwin } 120233a9ed9dSJohn Baldwin PROC_UNLOCK(np); 12033ce93e4eSPoul-Henning Kamp return (0); 12043ce93e4eSPoul-Henning Kamp } 12053ce93e4eSPoul-Henning Kamp 1206972f9b20SPoul-Henning Kamp static int 120782d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1208972f9b20SPoul-Henning Kamp { 1209972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 1210972f9b20SPoul-Henning Kamp u_int namelen = arg2; 1211972f9b20SPoul-Henning Kamp struct proc *p; 12122648efa6SDaniel Eischen int flags, doingzomb, oid_number; 1213972f9b20SPoul-Henning Kamp int error = 0; 1214972f9b20SPoul-Henning Kamp 12152648efa6SDaniel Eischen oid_number = oidp->oid_number; 12162648efa6SDaniel Eischen if (oid_number != KERN_PROC_ALL && 12172648efa6SDaniel Eischen (oid_number & KERN_PROC_INC_THREAD) == 0) 12182648efa6SDaniel Eischen flags = KERN_PROC_NOTHREADS; 12192648efa6SDaniel Eischen else { 12202648efa6SDaniel Eischen flags = 0; 12212648efa6SDaniel Eischen oid_number &= ~KERN_PROC_INC_THREAD; 12222648efa6SDaniel Eischen } 12232648efa6SDaniel Eischen if (oid_number == KERN_PROC_PID) { 12243ce93e4eSPoul-Henning Kamp if (namelen != 1) 1225972f9b20SPoul-Henning Kamp return (EINVAL); 12265032ff81SDon Lewis error = sysctl_wire_old_buffer(req, 0); 12275032ff81SDon Lewis if (error) 12285032ff81SDon Lewis return (error); 1229*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANSEE, &p); 1230*fa3935bcSMikolaj Golub if (error != 0) 1231e76bad96SRobert Drehmel return (error); 12322648efa6SDaniel Eischen error = sysctl_out_proc(p, req, flags); 12333ce93e4eSPoul-Henning Kamp return (error); 12343ce93e4eSPoul-Henning Kamp } 12353ddaef40STim J. Robbins 12362648efa6SDaniel Eischen switch (oid_number) { 12373ddaef40STim J. Robbins case KERN_PROC_ALL: 12383ddaef40STim J. Robbins if (namelen != 0) 12393ce93e4eSPoul-Henning Kamp return (EINVAL); 12403ddaef40STim J. Robbins break; 124125e247afSPeter Wemm case KERN_PROC_PROC: 124225e247afSPeter Wemm if (namelen != 0 && namelen != 1) 124325e247afSPeter Wemm return (EINVAL); 124425e247afSPeter Wemm break; 12453ddaef40STim J. Robbins default: 12463ddaef40STim J. Robbins if (namelen != 1) 12473ddaef40STim J. Robbins return (EINVAL); 12483ddaef40STim J. Robbins break; 12493ddaef40STim J. Robbins } 12503ce93e4eSPoul-Henning Kamp 1251972f9b20SPoul-Henning Kamp if (!req->oldptr) { 12523ce93e4eSPoul-Henning Kamp /* overestimate by 5 procs */ 1253972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1254972f9b20SPoul-Henning Kamp if (error) 1255972f9b20SPoul-Henning Kamp return (error); 1256972f9b20SPoul-Henning Kamp } 125747934cefSDon Lewis error = sysctl_wire_old_buffer(req, 0); 125847934cefSDon Lewis if (error != 0) 125947934cefSDon Lewis return (error); 12601005a129SJohn Baldwin sx_slock(&allproc_lock); 12613ce93e4eSPoul-Henning Kamp for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 12623ce93e4eSPoul-Henning Kamp if (!doingzomb) 12631b727751SPoul-Henning Kamp p = LIST_FIRST(&allproc); 12643ce93e4eSPoul-Henning Kamp else 12651b727751SPoul-Henning Kamp p = LIST_FIRST(&zombproc); 12661b727751SPoul-Henning Kamp for (; p != 0; p = LIST_NEXT(p, p_list)) { 126702e878d9SJohn Baldwin /* 126802e878d9SJohn Baldwin * Skip embryonic processes. 126902e878d9SJohn Baldwin */ 12708e6fa660SJohn Baldwin PROC_LOCK(p); 127102e878d9SJohn Baldwin if (p->p_state == PRS_NEW) { 12728e6fa660SJohn Baldwin PROC_UNLOCK(p); 127302e878d9SJohn Baldwin continue; 127402e878d9SJohn Baldwin } 1275b241b0a2SJuli Mallett KASSERT(p->p_ucred != NULL, 1276b241b0a2SJuli Mallett ("process credential is NULL for non-NEW proc")); 1277972f9b20SPoul-Henning Kamp /* 1278387d2c03SRobert Watson * Show a user only appropriate processes. 127903f808c5SPaul Saab */ 1280f44d9e24SJohn Baldwin if (p_cansee(curthread, p)) { 128165c9b430SJohn Baldwin PROC_UNLOCK(p); 128203f808c5SPaul Saab continue; 128365c9b430SJohn Baldwin } 128403f808c5SPaul Saab /* 1285972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 1286972f9b20SPoul-Henning Kamp * do by session. 1287972f9b20SPoul-Henning Kamp */ 12882648efa6SDaniel Eischen switch (oid_number) { 1289972f9b20SPoul-Henning Kamp 1290078842c5SGarance A Drosehn case KERN_PROC_GID: 1291b241b0a2SJuli Mallett if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1292078842c5SGarance A Drosehn PROC_UNLOCK(p); 1293078842c5SGarance A Drosehn continue; 1294078842c5SGarance A Drosehn } 1295078842c5SGarance A Drosehn break; 1296078842c5SGarance A Drosehn 1297972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 1298972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 12993ce93e4eSPoul-Henning Kamp if (p->p_pgrp == NULL || 1300f591779bSSeigo Tanimura p->p_pgrp->pg_id != (pid_t)name[0]) { 1301f591779bSSeigo Tanimura PROC_UNLOCK(p); 1302972f9b20SPoul-Henning Kamp continue; 1303f591779bSSeigo Tanimura } 1304972f9b20SPoul-Henning Kamp break; 1305972f9b20SPoul-Henning Kamp 1306b8fdc89dSGarance A Drosehn case KERN_PROC_RGID: 1307b241b0a2SJuli Mallett if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1308b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1309b8fdc89dSGarance A Drosehn continue; 1310b8fdc89dSGarance A Drosehn } 1311b8fdc89dSGarance A Drosehn break; 1312b8fdc89dSGarance A Drosehn 1313b8fdc89dSGarance A Drosehn case KERN_PROC_SESSION: 1314b8fdc89dSGarance A Drosehn if (p->p_session == NULL || 1315b8fdc89dSGarance A Drosehn p->p_session->s_sid != (pid_t)name[0]) { 1316b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1317b8fdc89dSGarance A Drosehn continue; 1318b8fdc89dSGarance A Drosehn } 1319b8fdc89dSGarance A Drosehn break; 1320b8fdc89dSGarance A Drosehn 1321972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 1322972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 1323f591779bSSeigo Tanimura p->p_session == NULL) { 1324f591779bSSeigo Tanimura PROC_UNLOCK(p); 1325972f9b20SPoul-Henning Kamp continue; 1326f591779bSSeigo Tanimura } 1327bc093719SEd Schouten /* XXX proctree_lock */ 1328f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 1329f591779bSSeigo Tanimura if (p->p_session->s_ttyp == NULL || 1330bc093719SEd Schouten tty_udev(p->p_session->s_ttyp) != 1331f3732fd1SPoul-Henning Kamp (dev_t)name[0]) { 1332f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1333f591779bSSeigo Tanimura PROC_UNLOCK(p); 1334f591779bSSeigo Tanimura continue; 1335f591779bSSeigo Tanimura } 1336f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1337972f9b20SPoul-Henning Kamp break; 1338972f9b20SPoul-Henning Kamp 1339972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 1340b241b0a2SJuli Mallett if (p->p_ucred->cr_uid != (uid_t)name[0]) { 134165c9b430SJohn Baldwin PROC_UNLOCK(p); 1342972f9b20SPoul-Henning Kamp continue; 134365c9b430SJohn Baldwin } 1344972f9b20SPoul-Henning Kamp break; 1345972f9b20SPoul-Henning Kamp 1346972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 1347b241b0a2SJuli Mallett if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 134865c9b430SJohn Baldwin PROC_UNLOCK(p); 1349972f9b20SPoul-Henning Kamp continue; 135065c9b430SJohn Baldwin } 1351972f9b20SPoul-Henning Kamp break; 135230c6f34eSScott Long 135330c6f34eSScott Long case KERN_PROC_PROC: 135430c6f34eSScott Long break; 135530c6f34eSScott Long 135630c6f34eSScott Long default: 135730c6f34eSScott Long break; 135830c6f34eSScott Long 1359972f9b20SPoul-Henning Kamp } 1360972f9b20SPoul-Henning Kamp 136130c6f34eSScott Long error = sysctl_out_proc(p, req, flags | doingzomb); 1362553629ebSJake Burkholder if (error) { 13631005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1364972f9b20SPoul-Henning Kamp return (error); 1365972f9b20SPoul-Henning Kamp } 1366972f9b20SPoul-Henning Kamp } 1367553629ebSJake Burkholder } 13681005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1369972f9b20SPoul-Henning Kamp return (0); 1370972f9b20SPoul-Henning Kamp } 1371972f9b20SPoul-Henning Kamp 1372c1508b28SAlfred Perlstein struct pargs * 1373c1508b28SAlfred Perlstein pargs_alloc(int len) 1374c1508b28SAlfred Perlstein { 1375c1508b28SAlfred Perlstein struct pargs *pa; 1376c1508b28SAlfred Perlstein 13771ede983cSDag-Erling Smørgrav pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1378a163d034SWarner Losh M_WAITOK); 137955b4a5aeSJohn Baldwin refcount_init(&pa->ar_ref, 1); 1380c1508b28SAlfred Perlstein pa->ar_length = len; 1381c1508b28SAlfred Perlstein return (pa); 1382c1508b28SAlfred Perlstein } 1383c1508b28SAlfred Perlstein 138458e8af1bSKonstantin Belousov static void 1385c1508b28SAlfred Perlstein pargs_free(struct pargs *pa) 1386c1508b28SAlfred Perlstein { 1387c1508b28SAlfred Perlstein 13881ede983cSDag-Erling Smørgrav free(pa, M_PARGS); 1389c1508b28SAlfred Perlstein } 1390c1508b28SAlfred Perlstein 1391c1508b28SAlfred Perlstein void 1392c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa) 1393c1508b28SAlfred Perlstein { 1394c1508b28SAlfred Perlstein 1395c1508b28SAlfred Perlstein if (pa == NULL) 1396c1508b28SAlfred Perlstein return; 139755b4a5aeSJohn Baldwin refcount_acquire(&pa->ar_ref); 1398c1508b28SAlfred Perlstein } 1399c1508b28SAlfred Perlstein 1400c1508b28SAlfred Perlstein void 1401c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa) 1402c1508b28SAlfred Perlstein { 1403c1508b28SAlfred Perlstein 1404c1508b28SAlfred Perlstein if (pa == NULL) 1405c1508b28SAlfred Perlstein return; 140655b4a5aeSJohn Baldwin if (refcount_release(&pa->ar_ref)) 1407c1508b28SAlfred Perlstein pargs_free(pa); 1408c1508b28SAlfred Perlstein } 1409c1508b28SAlfred Perlstein 1410c5cfcb1cSMikolaj Golub static int 1411c5cfcb1cSMikolaj Golub proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf, 1412c5cfcb1cSMikolaj Golub size_t len) 1413c5cfcb1cSMikolaj Golub { 1414c5cfcb1cSMikolaj Golub struct iovec iov; 1415c5cfcb1cSMikolaj Golub struct uio uio; 1416c5cfcb1cSMikolaj Golub 1417c5cfcb1cSMikolaj Golub iov.iov_base = (caddr_t)buf; 1418c5cfcb1cSMikolaj Golub iov.iov_len = len; 1419c5cfcb1cSMikolaj Golub uio.uio_iov = &iov; 1420c5cfcb1cSMikolaj Golub uio.uio_iovcnt = 1; 1421c5cfcb1cSMikolaj Golub uio.uio_offset = offset; 1422c5cfcb1cSMikolaj Golub uio.uio_resid = (ssize_t)len; 1423c5cfcb1cSMikolaj Golub uio.uio_segflg = UIO_SYSSPACE; 1424c5cfcb1cSMikolaj Golub uio.uio_rw = UIO_READ; 1425c5cfcb1cSMikolaj Golub uio.uio_td = td; 1426c5cfcb1cSMikolaj Golub 1427c5cfcb1cSMikolaj Golub return (proc_rwmem(p, &uio)); 1428c5cfcb1cSMikolaj Golub } 1429c5cfcb1cSMikolaj Golub 1430c5cfcb1cSMikolaj Golub static int 1431c5cfcb1cSMikolaj Golub proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf, 1432c5cfcb1cSMikolaj Golub size_t len) 1433c5cfcb1cSMikolaj Golub { 1434c5cfcb1cSMikolaj Golub size_t i; 1435c5cfcb1cSMikolaj Golub int error; 1436c5cfcb1cSMikolaj Golub 1437c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len); 1438c5cfcb1cSMikolaj Golub /* 1439c5cfcb1cSMikolaj Golub * Reading the chunk may validly return EFAULT if the string is shorter 1440c5cfcb1cSMikolaj Golub * than the chunk and is aligned at the end of the page, assuming the 1441c5cfcb1cSMikolaj Golub * next page is not mapped. So if EFAULT is returned do a fallback to 1442c5cfcb1cSMikolaj Golub * one byte read loop. 1443c5cfcb1cSMikolaj Golub */ 1444c5cfcb1cSMikolaj Golub if (error == EFAULT) { 1445c5cfcb1cSMikolaj Golub for (i = 0; i < len; i++, buf++, sptr++) { 1446c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1); 1447c5cfcb1cSMikolaj Golub if (error != 0) 1448c5cfcb1cSMikolaj Golub return (error); 1449c5cfcb1cSMikolaj Golub if (*buf == '\0') 1450c5cfcb1cSMikolaj Golub break; 1451c5cfcb1cSMikolaj Golub } 1452c5cfcb1cSMikolaj Golub error = 0; 1453c5cfcb1cSMikolaj Golub } 1454c5cfcb1cSMikolaj Golub return (error); 1455c5cfcb1cSMikolaj Golub } 1456c5cfcb1cSMikolaj Golub 1457c5cfcb1cSMikolaj Golub #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */ 1458c5cfcb1cSMikolaj Golub 1459c5cfcb1cSMikolaj Golub enum proc_vector_type { 1460c5cfcb1cSMikolaj Golub PROC_ARG, 1461c5cfcb1cSMikolaj Golub PROC_ENV, 1462c5cfcb1cSMikolaj Golub PROC_AUX, 1463c5cfcb1cSMikolaj Golub }; 1464c5cfcb1cSMikolaj Golub 1465c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32 1466c5cfcb1cSMikolaj Golub static int 1467c5cfcb1cSMikolaj Golub get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp, 1468c5cfcb1cSMikolaj Golub size_t *vsizep, enum proc_vector_type type) 1469c5cfcb1cSMikolaj Golub { 1470c5cfcb1cSMikolaj Golub struct freebsd32_ps_strings pss; 1471c5cfcb1cSMikolaj Golub Elf32_Auxinfo aux; 1472c5cfcb1cSMikolaj Golub vm_offset_t vptr, ptr; 1473c5cfcb1cSMikolaj Golub uint32_t *proc_vector32; 1474c5cfcb1cSMikolaj Golub char **proc_vector; 1475c5cfcb1cSMikolaj Golub size_t vsize, size; 1476c5cfcb1cSMikolaj Golub int i, error; 1477c5cfcb1cSMikolaj Golub 1478c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1479c5cfcb1cSMikolaj Golub &pss, sizeof(pss)); 1480c5cfcb1cSMikolaj Golub if (error != 0) 1481c5cfcb1cSMikolaj Golub return (error); 1482c5cfcb1cSMikolaj Golub switch (type) { 1483c5cfcb1cSMikolaj Golub case PROC_ARG: 1484c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_argvstr); 1485c5cfcb1cSMikolaj Golub vsize = pss.ps_nargvstr; 1486c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1487c5cfcb1cSMikolaj Golub return (ENOEXEC); 1488c5cfcb1cSMikolaj Golub size = vsize * sizeof(int32_t); 1489c5cfcb1cSMikolaj Golub break; 1490c5cfcb1cSMikolaj Golub case PROC_ENV: 1491c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_envstr); 1492c5cfcb1cSMikolaj Golub vsize = pss.ps_nenvstr; 1493c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1494c5cfcb1cSMikolaj Golub return (ENOEXEC); 1495c5cfcb1cSMikolaj Golub size = vsize * sizeof(int32_t); 1496c5cfcb1cSMikolaj Golub break; 1497c5cfcb1cSMikolaj Golub case PROC_AUX: 1498c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_envstr) + 1499c5cfcb1cSMikolaj Golub (pss.ps_nenvstr + 1) * sizeof(int32_t); 1500c5cfcb1cSMikolaj Golub if (vptr % 4 != 0) 1501c5cfcb1cSMikolaj Golub return (ENOEXEC); 1502c5cfcb1cSMikolaj Golub for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1503c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1504c5cfcb1cSMikolaj Golub if (error != 0) 1505c5cfcb1cSMikolaj Golub return (error); 1506c5cfcb1cSMikolaj Golub if (aux.a_type == AT_NULL) 1507c5cfcb1cSMikolaj Golub break; 1508c5cfcb1cSMikolaj Golub ptr += sizeof(aux); 1509c5cfcb1cSMikolaj Golub } 1510c5cfcb1cSMikolaj Golub if (aux.a_type != AT_NULL) 1511c5cfcb1cSMikolaj Golub return (ENOEXEC); 1512c5cfcb1cSMikolaj Golub vsize = i + 1; 1513c5cfcb1cSMikolaj Golub size = vsize * sizeof(aux); 1514c5cfcb1cSMikolaj Golub break; 1515c5cfcb1cSMikolaj Golub default: 1516c5cfcb1cSMikolaj Golub KASSERT(0, ("Wrong proc vector type: %d", type)); 15177ad9baaeSMikolaj Golub return (EINVAL); 1518c5cfcb1cSMikolaj Golub } 1519c5cfcb1cSMikolaj Golub proc_vector32 = malloc(size, M_TEMP, M_WAITOK); 1520c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, vptr, proc_vector32, size); 1521c5cfcb1cSMikolaj Golub if (error != 0) 1522c5cfcb1cSMikolaj Golub goto done; 1523c5cfcb1cSMikolaj Golub if (type == PROC_AUX) { 1524c5cfcb1cSMikolaj Golub *proc_vectorp = (char **)proc_vector32; 1525c5cfcb1cSMikolaj Golub *vsizep = vsize; 1526c5cfcb1cSMikolaj Golub return (0); 1527c5cfcb1cSMikolaj Golub } 1528c5cfcb1cSMikolaj Golub proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK); 1529c5cfcb1cSMikolaj Golub for (i = 0; i < (int)vsize; i++) 1530c5cfcb1cSMikolaj Golub proc_vector[i] = PTRIN(proc_vector32[i]); 1531c5cfcb1cSMikolaj Golub *proc_vectorp = proc_vector; 1532c5cfcb1cSMikolaj Golub *vsizep = vsize; 1533c5cfcb1cSMikolaj Golub done: 1534c5cfcb1cSMikolaj Golub free(proc_vector32, M_TEMP); 1535c5cfcb1cSMikolaj Golub return (error); 1536c5cfcb1cSMikolaj Golub } 1537c5cfcb1cSMikolaj Golub #endif 1538c5cfcb1cSMikolaj Golub 1539c5cfcb1cSMikolaj Golub static int 1540c5cfcb1cSMikolaj Golub get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp, 1541c5cfcb1cSMikolaj Golub size_t *vsizep, enum proc_vector_type type) 1542c5cfcb1cSMikolaj Golub { 1543c5cfcb1cSMikolaj Golub struct ps_strings pss; 1544c5cfcb1cSMikolaj Golub Elf_Auxinfo aux; 1545c5cfcb1cSMikolaj Golub vm_offset_t vptr, ptr; 1546c5cfcb1cSMikolaj Golub char **proc_vector; 1547c5cfcb1cSMikolaj Golub size_t vsize, size; 1548c5cfcb1cSMikolaj Golub int error, i; 1549c5cfcb1cSMikolaj Golub 1550c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32 1551c5cfcb1cSMikolaj Golub if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1552c5cfcb1cSMikolaj Golub return (get_proc_vector32(td, p, proc_vectorp, vsizep, type)); 1553c5cfcb1cSMikolaj Golub #endif 1554c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1555c5cfcb1cSMikolaj Golub &pss, sizeof(pss)); 1556c5cfcb1cSMikolaj Golub if (error != 0) 1557c5cfcb1cSMikolaj Golub return (error); 1558c5cfcb1cSMikolaj Golub switch (type) { 1559c5cfcb1cSMikolaj Golub case PROC_ARG: 1560c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_argvstr; 1561c5cfcb1cSMikolaj Golub vsize = pss.ps_nargvstr; 1562c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1563c5cfcb1cSMikolaj Golub return (ENOEXEC); 1564c5cfcb1cSMikolaj Golub size = vsize * sizeof(char *); 1565c5cfcb1cSMikolaj Golub break; 1566c5cfcb1cSMikolaj Golub case PROC_ENV: 1567c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_envstr; 1568c5cfcb1cSMikolaj Golub vsize = pss.ps_nenvstr; 1569c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1570c5cfcb1cSMikolaj Golub return (ENOEXEC); 1571c5cfcb1cSMikolaj Golub size = vsize * sizeof(char *); 1572c5cfcb1cSMikolaj Golub break; 1573c5cfcb1cSMikolaj Golub case PROC_AUX: 1574c5cfcb1cSMikolaj Golub /* 1575c5cfcb1cSMikolaj Golub * The aux array is just above env array on the stack. Check 1576c5cfcb1cSMikolaj Golub * that the address is naturally aligned. 1577c5cfcb1cSMikolaj Golub */ 1578c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1) 1579c5cfcb1cSMikolaj Golub * sizeof(char *); 1580c5cfcb1cSMikolaj Golub #if __ELF_WORD_SIZE == 64 1581c5cfcb1cSMikolaj Golub if (vptr % sizeof(uint64_t) != 0) 1582c5cfcb1cSMikolaj Golub #else 1583c5cfcb1cSMikolaj Golub if (vptr % sizeof(uint32_t) != 0) 1584c5cfcb1cSMikolaj Golub #endif 1585c5cfcb1cSMikolaj Golub return (ENOEXEC); 1586c5cfcb1cSMikolaj Golub /* 1587c5cfcb1cSMikolaj Golub * We count the array size reading the aux vectors from the 1588c5cfcb1cSMikolaj Golub * stack until AT_NULL vector is returned. So (to keep the code 1589c5cfcb1cSMikolaj Golub * simple) we read the process stack twice: the first time here 1590c5cfcb1cSMikolaj Golub * to find the size and the second time when copying the vectors 1591c5cfcb1cSMikolaj Golub * to the allocated proc_vector. 1592c5cfcb1cSMikolaj Golub */ 1593c5cfcb1cSMikolaj Golub for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1594c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1595c5cfcb1cSMikolaj Golub if (error != 0) 1596c5cfcb1cSMikolaj Golub return (error); 1597c5cfcb1cSMikolaj Golub if (aux.a_type == AT_NULL) 1598c5cfcb1cSMikolaj Golub break; 1599c5cfcb1cSMikolaj Golub ptr += sizeof(aux); 1600c5cfcb1cSMikolaj Golub } 1601c5cfcb1cSMikolaj Golub /* 1602c5cfcb1cSMikolaj Golub * If the PROC_AUXV_MAX entries are iterated over, and we have 1603c5cfcb1cSMikolaj Golub * not reached AT_NULL, it is most likely we are reading wrong 1604c5cfcb1cSMikolaj Golub * data: either the process doesn't have auxv array or data has 1605c5cfcb1cSMikolaj Golub * been modified. Return the error in this case. 1606c5cfcb1cSMikolaj Golub */ 1607c5cfcb1cSMikolaj Golub if (aux.a_type != AT_NULL) 1608c5cfcb1cSMikolaj Golub return (ENOEXEC); 1609c5cfcb1cSMikolaj Golub vsize = i + 1; 1610c5cfcb1cSMikolaj Golub size = vsize * sizeof(aux); 1611c5cfcb1cSMikolaj Golub break; 1612c5cfcb1cSMikolaj Golub default: 1613c5cfcb1cSMikolaj Golub KASSERT(0, ("Wrong proc vector type: %d", type)); 16147ad9baaeSMikolaj Golub return (EINVAL); /* In case we are built without INVARIANTS. */ 1615c5cfcb1cSMikolaj Golub } 1616c5cfcb1cSMikolaj Golub proc_vector = malloc(size, M_TEMP, M_WAITOK); 1617c5cfcb1cSMikolaj Golub if (proc_vector == NULL) 1618c5cfcb1cSMikolaj Golub return (ENOMEM); 1619c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, vptr, proc_vector, size); 1620c5cfcb1cSMikolaj Golub if (error != 0) { 1621c5cfcb1cSMikolaj Golub free(proc_vector, M_TEMP); 1622c5cfcb1cSMikolaj Golub return (error); 1623c5cfcb1cSMikolaj Golub } 1624c5cfcb1cSMikolaj Golub *proc_vectorp = proc_vector; 1625c5cfcb1cSMikolaj Golub *vsizep = vsize; 1626c5cfcb1cSMikolaj Golub 1627c5cfcb1cSMikolaj Golub return (0); 1628c5cfcb1cSMikolaj Golub } 1629c5cfcb1cSMikolaj Golub 1630c5cfcb1cSMikolaj Golub #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */ 1631c5cfcb1cSMikolaj Golub 1632c5cfcb1cSMikolaj Golub static int 1633c5cfcb1cSMikolaj Golub get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb, 1634c5cfcb1cSMikolaj Golub enum proc_vector_type type, size_t nchr) 1635c5cfcb1cSMikolaj Golub { 1636c5cfcb1cSMikolaj Golub size_t done, len, vsize; 1637c5cfcb1cSMikolaj Golub int error, i; 1638c5cfcb1cSMikolaj Golub char **proc_vector, *sptr; 1639c5cfcb1cSMikolaj Golub char pss_string[GET_PS_STRINGS_CHUNK_SZ]; 1640c5cfcb1cSMikolaj Golub 1641c5cfcb1cSMikolaj Golub PROC_ASSERT_HELD(p); 1642c5cfcb1cSMikolaj Golub 1643c5cfcb1cSMikolaj Golub /* 1644c5cfcb1cSMikolaj Golub * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes. 1645c5cfcb1cSMikolaj Golub */ 1646c5cfcb1cSMikolaj Golub if (nchr > 2 * (PATH_MAX + ARG_MAX)) 1647c5cfcb1cSMikolaj Golub nchr = 2 * (PATH_MAX + ARG_MAX); 1648c5cfcb1cSMikolaj Golub 1649c5cfcb1cSMikolaj Golub error = get_proc_vector(td, p, &proc_vector, &vsize, type); 1650c5cfcb1cSMikolaj Golub if (error != 0) 1651c5cfcb1cSMikolaj Golub return (error); 1652c5cfcb1cSMikolaj Golub for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) { 1653c5cfcb1cSMikolaj Golub /* 1654c5cfcb1cSMikolaj Golub * The program may have scribbled into its argv array, e.g. to 1655c5cfcb1cSMikolaj Golub * remove some arguments. If that has happened, break out 1656c5cfcb1cSMikolaj Golub * before trying to read from NULL. 1657c5cfcb1cSMikolaj Golub */ 1658c5cfcb1cSMikolaj Golub if (proc_vector[i] == NULL) 1659c5cfcb1cSMikolaj Golub break; 1660c5cfcb1cSMikolaj Golub for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) { 1661c5cfcb1cSMikolaj Golub error = proc_read_string(td, p, sptr, pss_string, 1662c5cfcb1cSMikolaj Golub sizeof(pss_string)); 1663c5cfcb1cSMikolaj Golub if (error != 0) 1664c5cfcb1cSMikolaj Golub goto done; 1665c5cfcb1cSMikolaj Golub len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ); 1666c5cfcb1cSMikolaj Golub if (done + len >= nchr) 1667c5cfcb1cSMikolaj Golub len = nchr - done - 1; 1668c5cfcb1cSMikolaj Golub sbuf_bcat(sb, pss_string, len); 1669c5cfcb1cSMikolaj Golub if (len != GET_PS_STRINGS_CHUNK_SZ) 1670c5cfcb1cSMikolaj Golub break; 1671c5cfcb1cSMikolaj Golub done += GET_PS_STRINGS_CHUNK_SZ; 1672c5cfcb1cSMikolaj Golub } 1673c5cfcb1cSMikolaj Golub sbuf_bcat(sb, "", 1); 1674c5cfcb1cSMikolaj Golub done += len + 1; 1675c5cfcb1cSMikolaj Golub } 1676c5cfcb1cSMikolaj Golub done: 1677c5cfcb1cSMikolaj Golub free(proc_vector, M_TEMP); 1678c5cfcb1cSMikolaj Golub return (error); 1679c5cfcb1cSMikolaj Golub } 1680c5cfcb1cSMikolaj Golub 1681c5cfcb1cSMikolaj Golub int 1682c5cfcb1cSMikolaj Golub proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb, size_t nchr) 1683c5cfcb1cSMikolaj Golub { 1684c5cfcb1cSMikolaj Golub 1685c5cfcb1cSMikolaj Golub return (get_ps_strings(curthread, p, sb, PROC_ARG, nchr)); 1686c5cfcb1cSMikolaj Golub } 1687c5cfcb1cSMikolaj Golub 1688c5cfcb1cSMikolaj Golub int 1689c5cfcb1cSMikolaj Golub proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb, size_t nchr) 1690c5cfcb1cSMikolaj Golub { 1691c5cfcb1cSMikolaj Golub 1692c5cfcb1cSMikolaj Golub return (get_ps_strings(curthread, p, sb, PROC_ENV, nchr)); 1693c5cfcb1cSMikolaj Golub } 1694c5cfcb1cSMikolaj Golub 1695b9df5231SPoul-Henning Kamp /* 1696b9df5231SPoul-Henning Kamp * This sysctl allows a process to retrieve the argument list or process 1697b9df5231SPoul-Henning Kamp * title for another process without groping around in the address space 1698b9df5231SPoul-Henning Kamp * of the other process. It also allow a process to set its own "process 1699b9df5231SPoul-Henning Kamp * title to a string of its own choice. 1700b9df5231SPoul-Henning Kamp */ 1701b9df5231SPoul-Henning Kamp static int 170282d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1703b9df5231SPoul-Henning Kamp { 1704b9df5231SPoul-Henning Kamp int *name = (int*) arg1; 1705b9df5231SPoul-Henning Kamp u_int namelen = arg2; 17068510f2a8SJohn Baldwin struct pargs *newpa, *pa; 1707b9df5231SPoul-Henning Kamp struct proc *p; 1708c5cfcb1cSMikolaj Golub struct sbuf sb; 1709*fa3935bcSMikolaj Golub int flags, error = 0, error2; 1710b9df5231SPoul-Henning Kamp 1711b9df5231SPoul-Henning Kamp if (namelen != 1) 1712b9df5231SPoul-Henning Kamp return (EINVAL); 1713b9df5231SPoul-Henning Kamp 1714*fa3935bcSMikolaj Golub flags = PGET_CANSEE; 1715*fa3935bcSMikolaj Golub if (req->newptr != NULL) 1716*fa3935bcSMikolaj Golub flags |= PGET_ISCURRENT; 1717*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], flags, &p); 1718*fa3935bcSMikolaj Golub if (error) 1719e76bad96SRobert Drehmel return (error); 1720b9df5231SPoul-Henning Kamp 17217b11fea6SAlfred Perlstein pa = p->p_args; 1722c5cfcb1cSMikolaj Golub if (pa != NULL) { 17237b11fea6SAlfred Perlstein pargs_hold(pa); 17247b11fea6SAlfred Perlstein PROC_UNLOCK(p); 17257b11fea6SAlfred Perlstein error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 17267b11fea6SAlfred Perlstein pargs_drop(pa); 1727c5cfcb1cSMikolaj Golub } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) { 1728c5cfcb1cSMikolaj Golub _PHOLD(p); 1729c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1730c5cfcb1cSMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1731c5cfcb1cSMikolaj Golub error = proc_getargv(curthread, p, &sb, req->oldlen); 1732c5cfcb1cSMikolaj Golub error2 = sbuf_finish(&sb); 1733c5cfcb1cSMikolaj Golub PRELE(p); 1734c5cfcb1cSMikolaj Golub sbuf_delete(&sb); 1735c5cfcb1cSMikolaj Golub if (error == 0 && error2 != 0) 1736c5cfcb1cSMikolaj Golub error = error2; 1737c5cfcb1cSMikolaj Golub } else { 1738c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1739c5cfcb1cSMikolaj Golub } 17408510f2a8SJohn Baldwin if (error != 0 || req->newptr == NULL) 1741b9df5231SPoul-Henning Kamp return (error); 1742b9df5231SPoul-Henning Kamp 1743b9df5231SPoul-Henning Kamp if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 17448510f2a8SJohn Baldwin return (ENOMEM); 17454bc6471bSJohn Baldwin newpa = pargs_alloc(req->newlen); 17464bc6471bSJohn Baldwin error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 17478510f2a8SJohn Baldwin if (error != 0) { 17484bc6471bSJohn Baldwin pargs_free(newpa); 1749b9df5231SPoul-Henning Kamp return (error); 1750b9df5231SPoul-Henning Kamp } 17514bc6471bSJohn Baldwin PROC_LOCK(p); 17524bc6471bSJohn Baldwin pa = p->p_args; 17534bc6471bSJohn Baldwin p->p_args = newpa; 17544bc6471bSJohn Baldwin PROC_UNLOCK(p); 17554bc6471bSJohn Baldwin pargs_drop(pa); 17564bc6471bSJohn Baldwin return (0); 17574bc6471bSJohn Baldwin } 17584bc6471bSJohn Baldwin 1759fe769cddSDavid Schultz /* 1760c5cfcb1cSMikolaj Golub * This sysctl allows a process to retrieve environment of another process. 1761c5cfcb1cSMikolaj Golub */ 1762c5cfcb1cSMikolaj Golub static int 1763c5cfcb1cSMikolaj Golub sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS) 1764c5cfcb1cSMikolaj Golub { 1765c5cfcb1cSMikolaj Golub int *name = (int*) arg1; 1766c5cfcb1cSMikolaj Golub u_int namelen = arg2; 1767c5cfcb1cSMikolaj Golub struct proc *p; 1768c5cfcb1cSMikolaj Golub struct sbuf sb; 1769c5cfcb1cSMikolaj Golub int error, error2; 1770c5cfcb1cSMikolaj Golub 1771c5cfcb1cSMikolaj Golub if (namelen != 1) 1772c5cfcb1cSMikolaj Golub return (EINVAL); 1773c5cfcb1cSMikolaj Golub 1774*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1775*fa3935bcSMikolaj Golub if (error != 0) 1776c5cfcb1cSMikolaj Golub return (error); 1777c5cfcb1cSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) { 1778*fa3935bcSMikolaj Golub PRELE(p); 1779c5cfcb1cSMikolaj Golub return (0); 1780c5cfcb1cSMikolaj Golub } 1781*fa3935bcSMikolaj Golub 1782c5cfcb1cSMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1783c5cfcb1cSMikolaj Golub error = proc_getenvv(curthread, p, &sb, req->oldlen); 1784c5cfcb1cSMikolaj Golub error2 = sbuf_finish(&sb); 1785c5cfcb1cSMikolaj Golub PRELE(p); 1786c5cfcb1cSMikolaj Golub sbuf_delete(&sb); 1787c5cfcb1cSMikolaj Golub return (error != 0 ? error : error2); 1788c5cfcb1cSMikolaj Golub } 1789c5cfcb1cSMikolaj Golub 1790c5cfcb1cSMikolaj Golub /* 1791c5cfcb1cSMikolaj Golub * This sysctl allows a process to retrieve ELF auxiliary vector of 1792c5cfcb1cSMikolaj Golub * another process. 1793c5cfcb1cSMikolaj Golub */ 1794c5cfcb1cSMikolaj Golub static int 1795c5cfcb1cSMikolaj Golub sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS) 1796c5cfcb1cSMikolaj Golub { 1797c5cfcb1cSMikolaj Golub int *name = (int*) arg1; 1798c5cfcb1cSMikolaj Golub u_int namelen = arg2; 1799c5cfcb1cSMikolaj Golub struct proc *p; 18004fd6053bSMikolaj Golub size_t vsize, size; 1801c5cfcb1cSMikolaj Golub char **auxv; 1802c5cfcb1cSMikolaj Golub int error; 1803c5cfcb1cSMikolaj Golub 1804c5cfcb1cSMikolaj Golub if (namelen != 1) 1805c5cfcb1cSMikolaj Golub return (EINVAL); 1806c5cfcb1cSMikolaj Golub 1807*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1808*fa3935bcSMikolaj Golub if (error != 0) 1809c5cfcb1cSMikolaj Golub return (error); 1810c5cfcb1cSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) { 1811*fa3935bcSMikolaj Golub PRELE(p); 1812c5cfcb1cSMikolaj Golub return (0); 1813c5cfcb1cSMikolaj Golub } 1814c5cfcb1cSMikolaj Golub error = get_proc_vector(curthread, p, &auxv, &vsize, PROC_AUX); 1815c5cfcb1cSMikolaj Golub if (error == 0) { 1816c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32 1817c5cfcb1cSMikolaj Golub if (SV_PROC_FLAG(p, SV_ILP32) != 0) 18184fd6053bSMikolaj Golub size = vsize * sizeof(Elf32_Auxinfo); 1819c5cfcb1cSMikolaj Golub else 1820c5cfcb1cSMikolaj Golub #endif 18214fd6053bSMikolaj Golub size = vsize * sizeof(Elf_Auxinfo); 18224fd6053bSMikolaj Golub PRELE(p); 18234fd6053bSMikolaj Golub error = SYSCTL_OUT(req, auxv, size); 1824c5cfcb1cSMikolaj Golub free(auxv, M_TEMP); 18254fd6053bSMikolaj Golub } else { 18264fd6053bSMikolaj Golub PRELE(p); 1827c5cfcb1cSMikolaj Golub } 1828c5cfcb1cSMikolaj Golub return (error); 1829c5cfcb1cSMikolaj Golub } 1830c5cfcb1cSMikolaj Golub 1831c5cfcb1cSMikolaj Golub /* 1832fe769cddSDavid Schultz * This sysctl allows a process to retrieve the path of the executable for 1833fe769cddSDavid Schultz * itself or another process. 1834fe769cddSDavid Schultz */ 1835fe769cddSDavid Schultz static int 1836fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1837fe769cddSDavid Schultz { 1838fe769cddSDavid Schultz pid_t *pidp = (pid_t *)arg1; 1839fe769cddSDavid Schultz unsigned int arglen = arg2; 1840fe769cddSDavid Schultz struct proc *p; 1841fe769cddSDavid Schultz struct vnode *vp; 1842fe769cddSDavid Schultz char *retbuf, *freebuf; 184324f87fdbSJohn Baldwin int error, vfslocked; 1844fe769cddSDavid Schultz 1845fe769cddSDavid Schultz if (arglen != 1) 1846fe769cddSDavid Schultz return (EINVAL); 1847fe769cddSDavid Schultz if (*pidp == -1) { /* -1 means this process */ 1848fe769cddSDavid Schultz p = req->td->td_proc; 1849fe769cddSDavid Schultz } else { 1850*fa3935bcSMikolaj Golub error = pget(*pidp, PGET_CANSEE, &p); 1851*fa3935bcSMikolaj Golub if (error != 0) 1852fe769cddSDavid Schultz return (error); 1853fe769cddSDavid Schultz } 1854fe769cddSDavid Schultz 1855fe769cddSDavid Schultz vp = p->p_textvp; 1856965b55e2SRobert Watson if (vp == NULL) { 1857965b55e2SRobert Watson if (*pidp != -1) 1858965b55e2SRobert Watson PROC_UNLOCK(p); 1859965b55e2SRobert Watson return (0); 1860965b55e2SRobert Watson } 1861fe769cddSDavid Schultz vref(vp); 1862fe769cddSDavid Schultz if (*pidp != -1) 1863fe769cddSDavid Schultz PROC_UNLOCK(p); 1864fe769cddSDavid Schultz error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 186524f87fdbSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1866fe769cddSDavid Schultz vrele(vp); 186724f87fdbSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 1868fe769cddSDavid Schultz if (error) 1869fe769cddSDavid Schultz return (error); 1870fe769cddSDavid Schultz error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1871fe769cddSDavid Schultz free(freebuf, M_TEMP); 1872fe769cddSDavid Schultz return (error); 1873fe769cddSDavid Schultz } 1874fe769cddSDavid Schultz 1875baf731e6SRobert Drehmel static int 1876baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1877baf731e6SRobert Drehmel { 1878baf731e6SRobert Drehmel struct proc *p; 1879baf731e6SRobert Drehmel char *sv_name; 1880baf731e6SRobert Drehmel int *name; 1881baf731e6SRobert Drehmel int namelen; 1882e76bad96SRobert Drehmel int error; 1883baf731e6SRobert Drehmel 1884baf731e6SRobert Drehmel namelen = arg2; 1885baf731e6SRobert Drehmel if (namelen != 1) 1886baf731e6SRobert Drehmel return (EINVAL); 1887baf731e6SRobert Drehmel 1888baf731e6SRobert Drehmel name = (int *)arg1; 1889*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANSEE, &p); 1890*fa3935bcSMikolaj Golub if (error != 0) 1891e76bad96SRobert Drehmel return (error); 1892baf731e6SRobert Drehmel sv_name = p->p_sysent->sv_name; 1893baf731e6SRobert Drehmel PROC_UNLOCK(p); 1894baf731e6SRobert Drehmel return (sysctl_handle_string(oidp, sv_name, 0, req)); 1895baf731e6SRobert Drehmel } 1896baf731e6SRobert Drehmel 189743151ee6SPeter Wemm #ifdef KINFO_OVMENTRY_SIZE 189843151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 189943151ee6SPeter Wemm #endif 190043151ee6SPeter Wemm 190143151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7 1902cc43c38cSRobert Watson static int 190343151ee6SPeter Wemm sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 1904cc43c38cSRobert Watson { 1905cc43c38cSRobert Watson vm_map_entry_t entry, tmp_entry; 1906cc43c38cSRobert Watson unsigned int last_timestamp; 1907cc43c38cSRobert Watson char *fullpath, *freepath; 190843151ee6SPeter Wemm struct kinfo_ovmentry *kve; 19097a9c4d24SPeter Wemm struct vattr va; 19107a9c4d24SPeter Wemm struct ucred *cred; 1911cc43c38cSRobert Watson int error, *name; 1912cc43c38cSRobert Watson struct vnode *vp; 1913cc43c38cSRobert Watson struct proc *p; 1914cc43c38cSRobert Watson vm_map_t map; 1915c7462f43SKonstantin Belousov struct vmspace *vm; 1916baf731e6SRobert Drehmel 1917cc43c38cSRobert Watson name = (int *)arg1; 1918*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p); 1919*fa3935bcSMikolaj Golub if (error != 0) 1920cc43c38cSRobert Watson return (error); 1921c7462f43SKonstantin Belousov vm = vmspace_acquire_ref(p); 1922c7462f43SKonstantin Belousov if (vm == NULL) { 1923c7462f43SKonstantin Belousov PRELE(p); 1924c7462f43SKonstantin Belousov return (ESRCH); 1925c7462f43SKonstantin Belousov } 1926cc43c38cSRobert Watson kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1927cc43c38cSRobert Watson 1928ca4aa8c3SSergey Kandaurov map = &vm->vm_map; 1929cc43c38cSRobert Watson vm_map_lock_read(map); 1930cc43c38cSRobert Watson for (entry = map->header.next; entry != &map->header; 1931cc43c38cSRobert Watson entry = entry->next) { 1932cc43c38cSRobert Watson vm_object_t obj, tobj, lobj; 1933cc43c38cSRobert Watson vm_offset_t addr; 1934cc43c38cSRobert Watson int vfslocked; 1935cc43c38cSRobert Watson 1936cc43c38cSRobert Watson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1937cc43c38cSRobert Watson continue; 1938cc43c38cSRobert Watson 1939cc43c38cSRobert Watson bzero(kve, sizeof(*kve)); 1940cc43c38cSRobert Watson kve->kve_structsize = sizeof(*kve); 1941cc43c38cSRobert Watson 1942cc43c38cSRobert Watson kve->kve_private_resident = 0; 1943cc43c38cSRobert Watson obj = entry->object.vm_object; 1944cc43c38cSRobert Watson if (obj != NULL) { 1945cc43c38cSRobert Watson VM_OBJECT_LOCK(obj); 1946cc43c38cSRobert Watson if (obj->shadow_count == 1) 1947cc43c38cSRobert Watson kve->kve_private_resident = 1948cc43c38cSRobert Watson obj->resident_page_count; 1949cc43c38cSRobert Watson } 1950cc43c38cSRobert Watson kve->kve_resident = 0; 1951cc43c38cSRobert Watson addr = entry->start; 1952cc43c38cSRobert Watson while (addr < entry->end) { 1953cc43c38cSRobert Watson if (pmap_extract(map->pmap, addr)) 1954cc43c38cSRobert Watson kve->kve_resident++; 1955cc43c38cSRobert Watson addr += PAGE_SIZE; 1956cc43c38cSRobert Watson } 1957cc43c38cSRobert Watson 1958cc43c38cSRobert Watson for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1959cc43c38cSRobert Watson if (tobj != obj) 1960cc43c38cSRobert Watson VM_OBJECT_LOCK(tobj); 1961cc43c38cSRobert Watson if (lobj != obj) 1962cc43c38cSRobert Watson VM_OBJECT_UNLOCK(lobj); 1963cc43c38cSRobert Watson lobj = tobj; 1964cc43c38cSRobert Watson } 1965cc43c38cSRobert Watson 1966118d0afaSKonstantin Belousov kve->kve_start = (void*)entry->start; 1967118d0afaSKonstantin Belousov kve->kve_end = (void*)entry->end; 1968118d0afaSKonstantin Belousov kve->kve_offset = (off_t)entry->offset; 1969118d0afaSKonstantin Belousov 1970118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_READ) 1971118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_READ; 1972118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_WRITE) 1973118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_WRITE; 1974118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_EXECUTE) 1975118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_EXEC; 1976118d0afaSKonstantin Belousov 1977118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_COW) 1978118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_COW; 1979118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1980118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1981937912eaSAttilio Rao if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 1982937912eaSAttilio Rao kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 1983118d0afaSKonstantin Belousov 1984118d0afaSKonstantin Belousov last_timestamp = map->timestamp; 1985118d0afaSKonstantin Belousov vm_map_unlock_read(map); 1986118d0afaSKonstantin Belousov 19877a9c4d24SPeter Wemm kve->kve_fileid = 0; 19887a9c4d24SPeter Wemm kve->kve_fsid = 0; 1989cc43c38cSRobert Watson freepath = NULL; 1990cc43c38cSRobert Watson fullpath = ""; 1991cc43c38cSRobert Watson if (lobj) { 1992cc43c38cSRobert Watson vp = NULL; 1993cc43c38cSRobert Watson switch (lobj->type) { 1994cc43c38cSRobert Watson case OBJT_DEFAULT: 1995cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_DEFAULT; 1996cc43c38cSRobert Watson break; 1997cc43c38cSRobert Watson case OBJT_VNODE: 1998cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_VNODE; 1999cc43c38cSRobert Watson vp = lobj->handle; 2000cc43c38cSRobert Watson vref(vp); 2001cc43c38cSRobert Watson break; 2002cc43c38cSRobert Watson case OBJT_SWAP: 2003cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_SWAP; 2004cc43c38cSRobert Watson break; 2005cc43c38cSRobert Watson case OBJT_DEVICE: 2006cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_DEVICE; 2007cc43c38cSRobert Watson break; 2008cc43c38cSRobert Watson case OBJT_PHYS: 2009cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_PHYS; 2010cc43c38cSRobert Watson break; 2011cc43c38cSRobert Watson case OBJT_DEAD: 2012cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_DEAD; 2013cc43c38cSRobert Watson break; 201401381811SJohn Baldwin case OBJT_SG: 201501381811SJohn Baldwin kve->kve_type = KVME_TYPE_SG; 201601381811SJohn Baldwin break; 2017cc43c38cSRobert Watson default: 2018cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_UNKNOWN; 2019cc43c38cSRobert Watson break; 2020cc43c38cSRobert Watson } 2021cc43c38cSRobert Watson if (lobj != obj) 2022cc43c38cSRobert Watson VM_OBJECT_UNLOCK(lobj); 2023cc43c38cSRobert Watson 2024cc43c38cSRobert Watson kve->kve_ref_count = obj->ref_count; 2025cc43c38cSRobert Watson kve->kve_shadow_count = obj->shadow_count; 2026cc43c38cSRobert Watson VM_OBJECT_UNLOCK(obj); 2027cc43c38cSRobert Watson if (vp != NULL) { 2028cc43c38cSRobert Watson vn_fullpath(curthread, vp, &fullpath, 2029cc43c38cSRobert Watson &freepath); 20307a9c4d24SPeter Wemm cred = curthread->td_ucred; 20312ff47c5fSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 20322ff47c5fSJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 20337a9c4d24SPeter Wemm if (VOP_GETATTR(vp, &va, cred) == 0) { 20347a9c4d24SPeter Wemm kve->kve_fileid = va.va_fileid; 20357a9c4d24SPeter Wemm kve->kve_fsid = va.va_fsid; 20367a9c4d24SPeter Wemm } 2037cc43c38cSRobert Watson vput(vp); 2038cc43c38cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 2039cc43c38cSRobert Watson } 2040cc43c38cSRobert Watson } else { 2041cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_NONE; 2042cc43c38cSRobert Watson kve->kve_ref_count = 0; 2043cc43c38cSRobert Watson kve->kve_shadow_count = 0; 2044cc43c38cSRobert Watson } 2045cc43c38cSRobert Watson 2046cc43c38cSRobert Watson strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2047cc43c38cSRobert Watson if (freepath != NULL) 2048cc43c38cSRobert Watson free(freepath, M_TEMP); 2049cc43c38cSRobert Watson 2050cc43c38cSRobert Watson error = SYSCTL_OUT(req, kve, sizeof(*kve)); 2051cc43c38cSRobert Watson vm_map_lock_read(map); 2052cc43c38cSRobert Watson if (error) 2053cc43c38cSRobert Watson break; 205422a448c4SKonstantin Belousov if (last_timestamp != map->timestamp) { 2055cc43c38cSRobert Watson vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2056cc43c38cSRobert Watson entry = tmp_entry; 2057cc43c38cSRobert Watson } 2058cc43c38cSRobert Watson } 2059cc43c38cSRobert Watson vm_map_unlock_read(map); 2060c7462f43SKonstantin Belousov vmspace_free(vm); 2061cc43c38cSRobert Watson PRELE(p); 2062cc43c38cSRobert Watson free(kve, M_TEMP); 2063cc43c38cSRobert Watson return (error); 2064cc43c38cSRobert Watson } 206543151ee6SPeter Wemm #endif /* COMPAT_FREEBSD7 */ 206643151ee6SPeter Wemm 206743151ee6SPeter Wemm #ifdef KINFO_VMENTRY_SIZE 206843151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 206943151ee6SPeter Wemm #endif 207043151ee6SPeter Wemm 207143151ee6SPeter Wemm static int 207243151ee6SPeter Wemm sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 207343151ee6SPeter Wemm { 207443151ee6SPeter Wemm vm_map_entry_t entry, tmp_entry; 207543151ee6SPeter Wemm unsigned int last_timestamp; 207643151ee6SPeter Wemm char *fullpath, *freepath; 207743151ee6SPeter Wemm struct kinfo_vmentry *kve; 207843151ee6SPeter Wemm struct vattr va; 207943151ee6SPeter Wemm struct ucred *cred; 208043151ee6SPeter Wemm int error, *name; 208143151ee6SPeter Wemm struct vnode *vp; 208243151ee6SPeter Wemm struct proc *p; 2083c7462f43SKonstantin Belousov struct vmspace *vm; 208443151ee6SPeter Wemm vm_map_t map; 208543151ee6SPeter Wemm 208643151ee6SPeter Wemm name = (int *)arg1; 2087*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_WANTREAD, &p); 2088*fa3935bcSMikolaj Golub if (error != 0) 208943151ee6SPeter Wemm return (error); 2090c7462f43SKonstantin Belousov vm = vmspace_acquire_ref(p); 2091c7462f43SKonstantin Belousov if (vm == NULL) { 2092c7462f43SKonstantin Belousov PRELE(p); 2093c7462f43SKonstantin Belousov return (ESRCH); 2094c7462f43SKonstantin Belousov } 209543151ee6SPeter Wemm kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 209643151ee6SPeter Wemm 2097ca4aa8c3SSergey Kandaurov map = &vm->vm_map; 209843151ee6SPeter Wemm vm_map_lock_read(map); 209943151ee6SPeter Wemm for (entry = map->header.next; entry != &map->header; 210043151ee6SPeter Wemm entry = entry->next) { 210143151ee6SPeter Wemm vm_object_t obj, tobj, lobj; 210243151ee6SPeter Wemm vm_offset_t addr; 21035384d089SMikolaj Golub vm_paddr_t locked_pa; 21045384d089SMikolaj Golub int vfslocked, mincoreinfo; 210543151ee6SPeter Wemm 210643151ee6SPeter Wemm if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 210743151ee6SPeter Wemm continue; 210843151ee6SPeter Wemm 210943151ee6SPeter Wemm bzero(kve, sizeof(*kve)); 211043151ee6SPeter Wemm 211143151ee6SPeter Wemm kve->kve_private_resident = 0; 211243151ee6SPeter Wemm obj = entry->object.vm_object; 211343151ee6SPeter Wemm if (obj != NULL) { 211443151ee6SPeter Wemm VM_OBJECT_LOCK(obj); 211543151ee6SPeter Wemm if (obj->shadow_count == 1) 211643151ee6SPeter Wemm kve->kve_private_resident = 211743151ee6SPeter Wemm obj->resident_page_count; 211843151ee6SPeter Wemm } 211943151ee6SPeter Wemm kve->kve_resident = 0; 212043151ee6SPeter Wemm addr = entry->start; 212143151ee6SPeter Wemm while (addr < entry->end) { 21225384d089SMikolaj Golub locked_pa = 0; 21235384d089SMikolaj Golub mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa); 21245384d089SMikolaj Golub if (locked_pa != 0) 21255384d089SMikolaj Golub vm_page_unlock(PHYS_TO_VM_PAGE(locked_pa)); 21265384d089SMikolaj Golub if (mincoreinfo & MINCORE_INCORE) 212743151ee6SPeter Wemm kve->kve_resident++; 21285384d089SMikolaj Golub if (mincoreinfo & MINCORE_SUPER) 21295384d089SMikolaj Golub kve->kve_flags |= KVME_FLAG_SUPER; 213043151ee6SPeter Wemm addr += PAGE_SIZE; 213143151ee6SPeter Wemm } 213243151ee6SPeter Wemm 213343151ee6SPeter Wemm for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 213443151ee6SPeter Wemm if (tobj != obj) 213543151ee6SPeter Wemm VM_OBJECT_LOCK(tobj); 213643151ee6SPeter Wemm if (lobj != obj) 213743151ee6SPeter Wemm VM_OBJECT_UNLOCK(lobj); 213843151ee6SPeter Wemm lobj = tobj; 213943151ee6SPeter Wemm } 214043151ee6SPeter Wemm 2141118d0afaSKonstantin Belousov kve->kve_start = entry->start; 2142118d0afaSKonstantin Belousov kve->kve_end = entry->end; 2143118d0afaSKonstantin Belousov kve->kve_offset = entry->offset; 2144118d0afaSKonstantin Belousov 2145118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_READ) 2146118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_READ; 2147118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_WRITE) 2148118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_WRITE; 2149118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_EXECUTE) 2150118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_EXEC; 2151118d0afaSKonstantin Belousov 2152118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_COW) 2153118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_COW; 2154118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2155118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2156937912eaSAttilio Rao if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2157937912eaSAttilio Rao kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2158118d0afaSKonstantin Belousov 2159118d0afaSKonstantin Belousov last_timestamp = map->timestamp; 2160118d0afaSKonstantin Belousov vm_map_unlock_read(map); 2161118d0afaSKonstantin Belousov 216243151ee6SPeter Wemm freepath = NULL; 216343151ee6SPeter Wemm fullpath = ""; 216443151ee6SPeter Wemm if (lobj) { 216543151ee6SPeter Wemm vp = NULL; 216643151ee6SPeter Wemm switch (lobj->type) { 216743151ee6SPeter Wemm case OBJT_DEFAULT: 216843151ee6SPeter Wemm kve->kve_type = KVME_TYPE_DEFAULT; 216943151ee6SPeter Wemm break; 217043151ee6SPeter Wemm case OBJT_VNODE: 217143151ee6SPeter Wemm kve->kve_type = KVME_TYPE_VNODE; 217243151ee6SPeter Wemm vp = lobj->handle; 217343151ee6SPeter Wemm vref(vp); 217443151ee6SPeter Wemm break; 217543151ee6SPeter Wemm case OBJT_SWAP: 217643151ee6SPeter Wemm kve->kve_type = KVME_TYPE_SWAP; 217743151ee6SPeter Wemm break; 217843151ee6SPeter Wemm case OBJT_DEVICE: 217943151ee6SPeter Wemm kve->kve_type = KVME_TYPE_DEVICE; 218043151ee6SPeter Wemm break; 218143151ee6SPeter Wemm case OBJT_PHYS: 218243151ee6SPeter Wemm kve->kve_type = KVME_TYPE_PHYS; 218343151ee6SPeter Wemm break; 218443151ee6SPeter Wemm case OBJT_DEAD: 218543151ee6SPeter Wemm kve->kve_type = KVME_TYPE_DEAD; 218643151ee6SPeter Wemm break; 218701381811SJohn Baldwin case OBJT_SG: 218801381811SJohn Baldwin kve->kve_type = KVME_TYPE_SG; 218901381811SJohn Baldwin break; 219043151ee6SPeter Wemm default: 219143151ee6SPeter Wemm kve->kve_type = KVME_TYPE_UNKNOWN; 219243151ee6SPeter Wemm break; 219343151ee6SPeter Wemm } 219443151ee6SPeter Wemm if (lobj != obj) 219543151ee6SPeter Wemm VM_OBJECT_UNLOCK(lobj); 219643151ee6SPeter Wemm 219743151ee6SPeter Wemm kve->kve_ref_count = obj->ref_count; 219843151ee6SPeter Wemm kve->kve_shadow_count = obj->shadow_count; 219943151ee6SPeter Wemm VM_OBJECT_UNLOCK(obj); 220043151ee6SPeter Wemm if (vp != NULL) { 220143151ee6SPeter Wemm vn_fullpath(curthread, vp, &fullpath, 220243151ee6SPeter Wemm &freepath); 22030daf62d9SStanislav Sedov kve->kve_vn_type = vntype_to_kinfo(vp->v_type); 220443151ee6SPeter Wemm cred = curthread->td_ucred; 220543151ee6SPeter Wemm vfslocked = VFS_LOCK_GIANT(vp->v_mount); 220643151ee6SPeter Wemm vn_lock(vp, LK_SHARED | LK_RETRY); 220743151ee6SPeter Wemm if (VOP_GETATTR(vp, &va, cred) == 0) { 22080daf62d9SStanislav Sedov kve->kve_vn_fileid = va.va_fileid; 22090daf62d9SStanislav Sedov kve->kve_vn_fsid = va.va_fsid; 22100daf62d9SStanislav Sedov kve->kve_vn_mode = 22110daf62d9SStanislav Sedov MAKEIMODE(va.va_type, va.va_mode); 22120daf62d9SStanislav Sedov kve->kve_vn_size = va.va_size; 22130daf62d9SStanislav Sedov kve->kve_vn_rdev = va.va_rdev; 22140daf62d9SStanislav Sedov kve->kve_status = KF_ATTR_VALID; 221543151ee6SPeter Wemm } 221643151ee6SPeter Wemm vput(vp); 221743151ee6SPeter Wemm VFS_UNLOCK_GIANT(vfslocked); 221843151ee6SPeter Wemm } 221943151ee6SPeter Wemm } else { 222043151ee6SPeter Wemm kve->kve_type = KVME_TYPE_NONE; 222143151ee6SPeter Wemm kve->kve_ref_count = 0; 222243151ee6SPeter Wemm kve->kve_shadow_count = 0; 222343151ee6SPeter Wemm } 222443151ee6SPeter Wemm 222543151ee6SPeter Wemm strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 222643151ee6SPeter Wemm if (freepath != NULL) 222743151ee6SPeter Wemm free(freepath, M_TEMP); 222843151ee6SPeter Wemm 222943151ee6SPeter Wemm /* Pack record size down */ 223043151ee6SPeter Wemm kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 223143151ee6SPeter Wemm strlen(kve->kve_path) + 1; 223243151ee6SPeter Wemm kve->kve_structsize = roundup(kve->kve_structsize, 223343151ee6SPeter Wemm sizeof(uint64_t)); 223443151ee6SPeter Wemm error = SYSCTL_OUT(req, kve, kve->kve_structsize); 223543151ee6SPeter Wemm vm_map_lock_read(map); 223643151ee6SPeter Wemm if (error) 223743151ee6SPeter Wemm break; 223822a448c4SKonstantin Belousov if (last_timestamp != map->timestamp) { 223943151ee6SPeter Wemm vm_map_lookup_entry(map, addr - 1, &tmp_entry); 224043151ee6SPeter Wemm entry = tmp_entry; 224143151ee6SPeter Wemm } 224243151ee6SPeter Wemm } 224343151ee6SPeter Wemm vm_map_unlock_read(map); 2244c7462f43SKonstantin Belousov vmspace_free(vm); 224543151ee6SPeter Wemm PRELE(p); 224643151ee6SPeter Wemm free(kve, M_TEMP); 224743151ee6SPeter Wemm return (error); 224843151ee6SPeter Wemm } 2249cc43c38cSRobert Watson 22501cc8c45cSRobert Watson #if defined(STACK) || defined(DDB) 22511cc8c45cSRobert Watson static int 22521cc8c45cSRobert Watson sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 22531cc8c45cSRobert Watson { 22541cc8c45cSRobert Watson struct kinfo_kstack *kkstp; 22551cc8c45cSRobert Watson int error, i, *name, numthreads; 22561cc8c45cSRobert Watson lwpid_t *lwpidarray; 22571cc8c45cSRobert Watson struct thread *td; 22581cc8c45cSRobert Watson struct stack *st; 22591cc8c45cSRobert Watson struct sbuf sb; 22601cc8c45cSRobert Watson struct proc *p; 22611cc8c45cSRobert Watson 22621cc8c45cSRobert Watson name = (int *)arg1; 2263*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p); 2264*fa3935bcSMikolaj Golub if (error != 0) 22651cc8c45cSRobert Watson return (error); 22661cc8c45cSRobert Watson 22671cc8c45cSRobert Watson kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 22681cc8c45cSRobert Watson st = stack_create(); 22691cc8c45cSRobert Watson 22701cc8c45cSRobert Watson lwpidarray = NULL; 22711cc8c45cSRobert Watson numthreads = 0; 2272374ae2a3SJeff Roberson PROC_LOCK(p); 22731cc8c45cSRobert Watson repeat: 22741cc8c45cSRobert Watson if (numthreads < p->p_numthreads) { 22751cc8c45cSRobert Watson if (lwpidarray != NULL) { 22761cc8c45cSRobert Watson free(lwpidarray, M_TEMP); 22771cc8c45cSRobert Watson lwpidarray = NULL; 22781cc8c45cSRobert Watson } 22791cc8c45cSRobert Watson numthreads = p->p_numthreads; 2280374ae2a3SJeff Roberson PROC_UNLOCK(p); 22811cc8c45cSRobert Watson lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 22821cc8c45cSRobert Watson M_WAITOK | M_ZERO); 2283374ae2a3SJeff Roberson PROC_LOCK(p); 22841cc8c45cSRobert Watson goto repeat; 22851cc8c45cSRobert Watson } 22861cc8c45cSRobert Watson i = 0; 22871cc8c45cSRobert Watson 22881cc8c45cSRobert Watson /* 22891cc8c45cSRobert Watson * XXXRW: During the below loop, execve(2) and countless other sorts 22901cc8c45cSRobert Watson * of changes could have taken place. Should we check to see if the 22911cc8c45cSRobert Watson * vmspace has been replaced, or the like, in order to prevent 22921cc8c45cSRobert Watson * giving a snapshot that spans, say, execve(2), with some threads 22931cc8c45cSRobert Watson * before and some after? Among other things, the credentials could 22941cc8c45cSRobert Watson * have changed, in which case the right to extract debug info might 22951cc8c45cSRobert Watson * no longer be assured. 22961cc8c45cSRobert Watson */ 22971cc8c45cSRobert Watson FOREACH_THREAD_IN_PROC(p, td) { 22981cc8c45cSRobert Watson KASSERT(i < numthreads, 22991cc8c45cSRobert Watson ("sysctl_kern_proc_kstack: numthreads")); 23001cc8c45cSRobert Watson lwpidarray[i] = td->td_tid; 23011cc8c45cSRobert Watson i++; 23021cc8c45cSRobert Watson } 23031cc8c45cSRobert Watson numthreads = i; 23041cc8c45cSRobert Watson for (i = 0; i < numthreads; i++) { 23051cc8c45cSRobert Watson td = thread_find(p, lwpidarray[i]); 23061cc8c45cSRobert Watson if (td == NULL) { 23071cc8c45cSRobert Watson continue; 23081cc8c45cSRobert Watson } 23091cc8c45cSRobert Watson bzero(kkstp, sizeof(*kkstp)); 23101cc8c45cSRobert Watson (void)sbuf_new(&sb, kkstp->kkst_trace, 23111cc8c45cSRobert Watson sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 23121cc8c45cSRobert Watson thread_lock(td); 23131cc8c45cSRobert Watson kkstp->kkst_tid = td->td_tid; 23141cc8c45cSRobert Watson if (TD_IS_SWAPPED(td)) 23151cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_SWAPPED; 23161cc8c45cSRobert Watson else if (TD_IS_RUNNING(td)) 23171cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_RUNNING; 23181cc8c45cSRobert Watson else { 23191cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_STACKOK; 23201cc8c45cSRobert Watson stack_save_td(st, td); 23211cc8c45cSRobert Watson } 23221cc8c45cSRobert Watson thread_unlock(td); 23231cc8c45cSRobert Watson PROC_UNLOCK(p); 23241cc8c45cSRobert Watson stack_sbuf_print(&sb, st); 23251cc8c45cSRobert Watson sbuf_finish(&sb); 23261cc8c45cSRobert Watson sbuf_delete(&sb); 23271cc8c45cSRobert Watson error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 23281cc8c45cSRobert Watson PROC_LOCK(p); 23291cc8c45cSRobert Watson if (error) 23301cc8c45cSRobert Watson break; 23311cc8c45cSRobert Watson } 23321cc8c45cSRobert Watson _PRELE(p); 23331cc8c45cSRobert Watson PROC_UNLOCK(p); 23341cc8c45cSRobert Watson if (lwpidarray != NULL) 23351cc8c45cSRobert Watson free(lwpidarray, M_TEMP); 23361cc8c45cSRobert Watson stack_destroy(st); 23371cc8c45cSRobert Watson free(kkstp, M_TEMP); 23381cc8c45cSRobert Watson return (error); 23391cc8c45cSRobert Watson } 23401cc8c45cSRobert Watson #endif 23411cc8c45cSRobert Watson 2342254d03c5SBrooks Davis /* 2343254d03c5SBrooks Davis * This sysctl allows a process to retrieve the full list of groups from 2344254d03c5SBrooks Davis * itself or another process. 2345254d03c5SBrooks Davis */ 2346254d03c5SBrooks Davis static int 2347254d03c5SBrooks Davis sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 2348254d03c5SBrooks Davis { 2349254d03c5SBrooks Davis pid_t *pidp = (pid_t *)arg1; 2350254d03c5SBrooks Davis unsigned int arglen = arg2; 2351254d03c5SBrooks Davis struct proc *p; 2352254d03c5SBrooks Davis struct ucred *cred; 2353254d03c5SBrooks Davis int error; 2354254d03c5SBrooks Davis 2355254d03c5SBrooks Davis if (arglen != 1) 2356254d03c5SBrooks Davis return (EINVAL); 2357254d03c5SBrooks Davis if (*pidp == -1) { /* -1 means this process */ 2358254d03c5SBrooks Davis p = req->td->td_proc; 2359254d03c5SBrooks Davis } else { 2360*fa3935bcSMikolaj Golub error = pget(*pidp, PGET_CANSEE, &p); 2361*fa3935bcSMikolaj Golub if (error != 0) 2362254d03c5SBrooks Davis return (error); 2363254d03c5SBrooks Davis } 2364254d03c5SBrooks Davis 2365254d03c5SBrooks Davis cred = crhold(p->p_ucred); 2366254d03c5SBrooks Davis if (*pidp != -1) 2367254d03c5SBrooks Davis PROC_UNLOCK(p); 2368254d03c5SBrooks Davis 2369254d03c5SBrooks Davis error = SYSCTL_OUT(req, cred->cr_groups, 2370254d03c5SBrooks Davis cred->cr_ngroups * sizeof(gid_t)); 2371254d03c5SBrooks Davis crfree(cred); 2372254d03c5SBrooks Davis return (error); 2373254d03c5SBrooks Davis } 2374254d03c5SBrooks Davis 23759e7d0583SMikolaj Golub /* 23769e7d0583SMikolaj Golub * This sysctl allows a process to retrieve the resource limits for 23779e7d0583SMikolaj Golub * another process. 23789e7d0583SMikolaj Golub */ 23799e7d0583SMikolaj Golub static int 23809e7d0583SMikolaj Golub sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS) 23819e7d0583SMikolaj Golub { 23829e7d0583SMikolaj Golub int *name = (int*) arg1; 23839e7d0583SMikolaj Golub u_int namelen = arg2; 23849e7d0583SMikolaj Golub struct plimit *limp; 23859e7d0583SMikolaj Golub struct proc *p; 23869e7d0583SMikolaj Golub int error = 0; 23879e7d0583SMikolaj Golub 23889e7d0583SMikolaj Golub if (namelen != 1) 23899e7d0583SMikolaj Golub return (EINVAL); 23909e7d0583SMikolaj Golub 2391*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANSEE, &p); 2392*fa3935bcSMikolaj Golub if (error != 0) 23939e7d0583SMikolaj Golub return (error); 23949e7d0583SMikolaj Golub /* 23959e7d0583SMikolaj Golub * Check the request size. We alow sizes smaller rlimit array for 23969e7d0583SMikolaj Golub * backward binary compatibility: the number of resource limits may 23979e7d0583SMikolaj Golub * grow. 23989e7d0583SMikolaj Golub */ 23999e7d0583SMikolaj Golub if (sizeof(limp->pl_rlimit) < req->oldlen) { 24009e7d0583SMikolaj Golub PROC_UNLOCK(p); 24019e7d0583SMikolaj Golub return (EINVAL); 24029e7d0583SMikolaj Golub } 24039e7d0583SMikolaj Golub 24049e7d0583SMikolaj Golub limp = lim_hold(p->p_limit); 24059e7d0583SMikolaj Golub PROC_UNLOCK(p); 24069e7d0583SMikolaj Golub error = SYSCTL_OUT(req, limp->pl_rlimit, req->oldlen); 24079e7d0583SMikolaj Golub lim_free(limp); 24089e7d0583SMikolaj Golub return (error); 24099e7d0583SMikolaj Golub } 24109e7d0583SMikolaj Golub 24119732458fSMikolaj Golub /* 24129732458fSMikolaj Golub * This sysctl allows a process to retrieve ps_strings structure location of 24139732458fSMikolaj Golub * another process. 24149732458fSMikolaj Golub */ 24159732458fSMikolaj Golub static int 24169732458fSMikolaj Golub sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS) 24179732458fSMikolaj Golub { 24189732458fSMikolaj Golub int *name = (int*) arg1; 24199732458fSMikolaj Golub u_int namelen = arg2; 24209732458fSMikolaj Golub struct proc *p; 24219732458fSMikolaj Golub vm_offset_t ps_strings; 24229732458fSMikolaj Golub int error; 24239732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32 24249732458fSMikolaj Golub uint32_t ps_strings32; 24259732458fSMikolaj Golub #endif 24269732458fSMikolaj Golub 24279732458fSMikolaj Golub if (namelen != 1) 24289732458fSMikolaj Golub return (EINVAL); 24299732458fSMikolaj Golub 2430*fa3935bcSMikolaj Golub error = pget((pid_t)name[0], PGET_CANDEBUG, &p); 2431*fa3935bcSMikolaj Golub if (error != 0) 24329732458fSMikolaj Golub return (error); 24339732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32 24349732458fSMikolaj Golub if ((req->flags & SCTL_MASK32) != 0) { 24359732458fSMikolaj Golub /* 24369732458fSMikolaj Golub * We return 0 if the 32 bit emulation request is for a 64 bit 24379732458fSMikolaj Golub * process. 24389732458fSMikolaj Golub */ 24399732458fSMikolaj Golub ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ? 24409732458fSMikolaj Golub PTROUT(p->p_sysent->sv_psstrings) : 0; 24419732458fSMikolaj Golub PROC_UNLOCK(p); 24429732458fSMikolaj Golub error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32)); 24439732458fSMikolaj Golub return (error); 24449732458fSMikolaj Golub } 24459732458fSMikolaj Golub #endif 24469732458fSMikolaj Golub ps_strings = p->p_sysent->sv_psstrings; 24479732458fSMikolaj Golub PROC_UNLOCK(p); 24489732458fSMikolaj Golub error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings)); 24499732458fSMikolaj Golub return (error); 24509732458fSMikolaj Golub } 24519732458fSMikolaj Golub 2452cc43c38cSRobert Watson SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 24533ce93e4eSPoul-Henning Kamp 245424f87fdbSJohn Baldwin SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 245524f87fdbSJohn Baldwin CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 245624f87fdbSJohn Baldwin "Return entire process table"); 24573ce93e4eSPoul-Henning Kamp 245824f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2459078842c5SGarance A Drosehn sysctl_kern_proc, "Process table"); 2460078842c5SGarance A Drosehn 246124f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 24623ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 24633ce93e4eSPoul-Henning Kamp 246424f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2465b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 2466b8fdc89dSGarance A Drosehn 246724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 246824f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 246924f87fdbSJohn Baldwin 247024f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2471b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 2472b8fdc89dSGarance A Drosehn 247324f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 24743ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 24753ce93e4eSPoul-Henning Kamp 247624f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 24773ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 24783ce93e4eSPoul-Henning Kamp 247924f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 24803ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 24813ce93e4eSPoul-Henning Kamp 248224f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 248330c6f34eSScott Long sysctl_kern_proc, "Return process table, no threads"); 248430c6f34eSScott Long 24850c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 248624f87fdbSJohn Baldwin CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 24879b6d9dbaSPoul-Henning Kamp sysctl_kern_proc_args, "Process argument list"); 2488baf731e6SRobert Drehmel 2489c5cfcb1cSMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, 2490c5cfcb1cSMikolaj Golub CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2491c5cfcb1cSMikolaj Golub sysctl_kern_proc_env, "Process environment"); 2492c5cfcb1cSMikolaj Golub 2493c5cfcb1cSMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, 2494c5cfcb1cSMikolaj Golub CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2495c5cfcb1cSMikolaj Golub sysctl_kern_proc_auxv, "Process ELF auxiliary vector"); 2496c5cfcb1cSMikolaj Golub 249724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 249824f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2499fe769cddSDavid Schultz 250024f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 250124f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 250224f87fdbSJohn Baldwin "Process syscall vector name (ABI type)"); 25032648efa6SDaniel Eischen 25040c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 250524f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2506078842c5SGarance A Drosehn 25070c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 250824f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25092648efa6SDaniel Eischen 25100c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 251124f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2512b8fdc89dSGarance A Drosehn 25130c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 251424f87fdbSJohn Baldwin sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25150c898376SPoul-Henning Kamp 25160c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 251724f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2518b8fdc89dSGarance A Drosehn 25190c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 252024f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25212648efa6SDaniel Eischen 25220c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 252324f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25242648efa6SDaniel Eischen 25250c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 252624f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25272648efa6SDaniel Eischen 25280c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 252924f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 253024f87fdbSJohn Baldwin "Return process table, no threads"); 2531cc43c38cSRobert Watson 253243151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7 253324f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 253424f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 253543151ee6SPeter Wemm #endif 253643151ee6SPeter Wemm 253724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 253824f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 25391cc8c45cSRobert Watson 25401cc8c45cSRobert Watson #if defined(STACK) || defined(DDB) 254124f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 254224f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 25431cc8c45cSRobert Watson #endif 2544254d03c5SBrooks Davis 2545254d03c5SBrooks Davis static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2546254d03c5SBrooks Davis CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 25479e7d0583SMikolaj Golub 25489e7d0583SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RD | 25499e7d0583SMikolaj Golub CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, "Process resource limits"); 25509732458fSMikolaj Golub 25519732458fSMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, 25529732458fSMikolaj Golub CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 25539732458fSMikolaj Golub sysctl_kern_proc_ps_strings, "Process ps_strings location"); 2554