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 /* 333f591779bSSeigo Tanimura * Create a new process group. 334f591779bSSeigo Tanimura * pgid must be equal to the pid of p. 335f591779bSSeigo Tanimura * Begin a new session if required. 336df8bae1dSRodney W. Grimes */ 33726f9a767SRodney W. Grimes int 338f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess) 339df8bae1dSRodney W. Grimes register struct proc *p; 340df8bae1dSRodney W. Grimes pid_t pgid; 341f591779bSSeigo Tanimura struct pgrp *pgrp; 342f591779bSSeigo Tanimura struct session *sess; 343df8bae1dSRodney W. Grimes { 344f591779bSSeigo Tanimura struct pgrp *pgrp2; 345df8bae1dSRodney W. Grimes 346f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 347f591779bSSeigo Tanimura 348f591779bSSeigo Tanimura KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL")); 349f591779bSSeigo Tanimura KASSERT(p->p_pid == pgid, 350f591779bSSeigo Tanimura ("enterpgrp: new pgrp and pid != pgid")); 351f591779bSSeigo Tanimura 352f591779bSSeigo Tanimura pgrp2 = pgfind(pgid); 353f591779bSSeigo Tanimura 354f591779bSSeigo Tanimura KASSERT(pgrp2 == NULL, 355f591779bSSeigo Tanimura ("enterpgrp: pgrp with pgid exists")); 3565526d2d9SEivind Eklund KASSERT(!SESS_LEADER(p), 3575526d2d9SEivind Eklund ("enterpgrp: session leader attempted setpgrp")); 358219cbf59SEivind Eklund 3596008862bSJohn Baldwin mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK); 360df8bae1dSRodney W. Grimes 361f591779bSSeigo Tanimura if (sess != NULL) { 362df8bae1dSRodney W. Grimes /* 363df8bae1dSRodney W. Grimes * new session 364df8bae1dSRodney W. Grimes */ 3656008862bSJohn Baldwin mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF); 366f591779bSSeigo Tanimura PROC_LOCK(p); 367f591779bSSeigo Tanimura p->p_flag &= ~P_CONTROLT; 368f591779bSSeigo Tanimura PROC_UNLOCK(p); 369f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 370df8bae1dSRodney W. Grimes sess->s_leader = p; 371643a8daaSDon Lewis sess->s_sid = p->p_pid; 372bc093719SEd Schouten refcount_init(&sess->s_count, 1); 373df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 3748dc9b4cfSEd Schouten sess->s_ttydp = NULL; 375df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 376df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 377df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 378df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 3795526d2d9SEivind Eklund KASSERT(p == curproc, 3805526d2d9SEivind Eklund ("enterpgrp: mksession and p != curproc")); 381df8bae1dSRodney W. Grimes } else { 382df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 383bc093719SEd Schouten sess_hold(pgrp->pg_session); 384f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 385df8bae1dSRodney W. Grimes } 386df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 387b75356e1SJeffrey Hsu LIST_INIT(&pgrp->pg_members); 388f591779bSSeigo Tanimura 389f591779bSSeigo Tanimura /* 390f089b570SJohn Baldwin * As we have an exclusive lock of proctree_lock, 391f591779bSSeigo Tanimura * this should not deadlock. 392f591779bSSeigo Tanimura */ 393b75356e1SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 394df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 395831d27a9SDon Lewis SLIST_INIT(&pgrp->pg_sigiolst); 396f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 397f591779bSSeigo Tanimura 398f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 399f591779bSSeigo Tanimura 400df8bae1dSRodney W. Grimes return (0); 401f591779bSSeigo Tanimura } 402f591779bSSeigo Tanimura 403f591779bSSeigo Tanimura /* 404f591779bSSeigo Tanimura * Move p to an existing process group 405f591779bSSeigo Tanimura */ 406f591779bSSeigo Tanimura int 407f591779bSSeigo Tanimura enterthispgrp(p, pgrp) 408f591779bSSeigo Tanimura register struct proc *p; 409f591779bSSeigo Tanimura struct pgrp *pgrp; 410f591779bSSeigo Tanimura { 411f089b570SJohn Baldwin 412f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 413f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 414f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 415f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 416f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 417f591779bSSeigo Tanimura KASSERT(pgrp->pg_session == p->p_session, 418f591779bSSeigo Tanimura ("%s: pgrp's session %p, p->p_session %p.\n", 419f591779bSSeigo Tanimura __func__, 420f591779bSSeigo Tanimura pgrp->pg_session, 421f591779bSSeigo Tanimura p->p_session)); 422f591779bSSeigo Tanimura KASSERT(pgrp != p->p_pgrp, 423f591779bSSeigo Tanimura ("%s: p belongs to pgrp.", __func__)); 424f591779bSSeigo Tanimura 425f591779bSSeigo Tanimura doenterpgrp(p, pgrp); 426f591779bSSeigo Tanimura 427f591779bSSeigo Tanimura return (0); 428f591779bSSeigo Tanimura } 429f591779bSSeigo Tanimura 430f591779bSSeigo Tanimura /* 431f591779bSSeigo Tanimura * Move p to a process group 432f591779bSSeigo Tanimura */ 433f591779bSSeigo Tanimura static void 434f591779bSSeigo Tanimura doenterpgrp(p, pgrp) 435f591779bSSeigo Tanimura struct proc *p; 436f591779bSSeigo Tanimura struct pgrp *pgrp; 437f591779bSSeigo Tanimura { 438f591779bSSeigo Tanimura struct pgrp *savepgrp; 439f591779bSSeigo Tanimura 440f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 441f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 442f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 443f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED); 444f591779bSSeigo Tanimura SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED); 445f591779bSSeigo Tanimura 446f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 447df8bae1dSRodney W. Grimes 448df8bae1dSRodney W. Grimes /* 449df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 450df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 451df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 452df8bae1dSRodney W. Grimes */ 453df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 454df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 455df8bae1dSRodney W. Grimes 456f591779bSSeigo Tanimura PGRP_LOCK(pgrp); 457f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 45815e9ec51SJohn Baldwin PROC_LOCK(p); 459b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 460df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 46115e9ec51SJohn Baldwin PROC_UNLOCK(p); 462f591779bSSeigo Tanimura LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 463f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 464f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 465f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 466f591779bSSeigo Tanimura pgdelete(savepgrp); 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes 469df8bae1dSRodney W. Grimes /* 470df8bae1dSRodney W. Grimes * remove process from process group 471df8bae1dSRodney W. Grimes */ 47226f9a767SRodney W. Grimes int 473df8bae1dSRodney W. Grimes leavepgrp(p) 474df8bae1dSRodney W. Grimes register struct proc *p; 475df8bae1dSRodney W. Grimes { 476f591779bSSeigo Tanimura struct pgrp *savepgrp; 477df8bae1dSRodney W. Grimes 478f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 479f591779bSSeigo Tanimura savepgrp = p->p_pgrp; 480f591779bSSeigo Tanimura PGRP_LOCK(savepgrp); 48115e9ec51SJohn Baldwin PROC_LOCK(p); 482b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 48315e9ec51SJohn Baldwin p->p_pgrp = NULL; 48415e9ec51SJohn Baldwin PROC_UNLOCK(p); 485f591779bSSeigo Tanimura PGRP_UNLOCK(savepgrp); 486f591779bSSeigo Tanimura if (LIST_EMPTY(&savepgrp->pg_members)) 487f591779bSSeigo Tanimura pgdelete(savepgrp); 488df8bae1dSRodney W. Grimes return (0); 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes 491df8bae1dSRodney W. Grimes /* 492df8bae1dSRodney W. Grimes * delete a process group 493df8bae1dSRodney W. Grimes */ 49487b6de2bSPoul-Henning Kamp static void 495df8bae1dSRodney W. Grimes pgdelete(pgrp) 496df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 497df8bae1dSRodney W. Grimes { 498f591779bSSeigo Tanimura struct session *savesess; 499bc093719SEd Schouten struct tty *tp; 500f591779bSSeigo Tanimura 501f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_XLOCKED); 502f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 503f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 504f591779bSSeigo Tanimura 505831d27a9SDon Lewis /* 506831d27a9SDon Lewis * Reset any sigio structures pointing to us as a result of 507831d27a9SDon Lewis * F_SETOWN with our pgid. 508831d27a9SDon Lewis */ 509831d27a9SDon Lewis funsetownlst(&pgrp->pg_sigiolst); 510831d27a9SDon Lewis 511e649887bSAlfred Perlstein PGRP_LOCK(pgrp); 512bc093719SEd Schouten tp = pgrp->pg_session->s_ttyp; 513b75356e1SJeffrey Hsu LIST_REMOVE(pgrp, pg_hash); 514f591779bSSeigo Tanimura savesess = pgrp->pg_session; 515f591779bSSeigo Tanimura PGRP_UNLOCK(pgrp); 516bc093719SEd Schouten 517bc093719SEd Schouten /* Remove the reference to the pgrp before deallocating it. */ 518bc093719SEd Schouten if (tp != NULL) { 519bc093719SEd Schouten tty_lock(tp); 520bc093719SEd Schouten tty_rel_pgrp(tp, pgrp); 521bc093719SEd Schouten } 522bc093719SEd Schouten 5236041fa0aSSeigo Tanimura mtx_destroy(&pgrp->pg_mtx); 5241ede983cSDag-Erling Smørgrav free(pgrp, M_PGRP); 525bc093719SEd Schouten sess_release(savesess); 526df8bae1dSRodney W. Grimes } 527df8bae1dSRodney W. Grimes 52802e878d9SJohn Baldwin static void 52902e878d9SJohn Baldwin pgadjustjobc(pgrp, entering) 53002e878d9SJohn Baldwin struct pgrp *pgrp; 53102e878d9SJohn Baldwin int entering; 53202e878d9SJohn Baldwin { 53302e878d9SJohn Baldwin 53402e878d9SJohn Baldwin PGRP_LOCK(pgrp); 53502e878d9SJohn Baldwin if (entering) 53602e878d9SJohn Baldwin pgrp->pg_jobc++; 53702e878d9SJohn Baldwin else { 53802e878d9SJohn Baldwin --pgrp->pg_jobc; 53902e878d9SJohn Baldwin if (pgrp->pg_jobc == 0) 54002e878d9SJohn Baldwin orphanpg(pgrp); 54102e878d9SJohn Baldwin } 54202e878d9SJohn Baldwin PGRP_UNLOCK(pgrp); 54302e878d9SJohn Baldwin } 54402e878d9SJohn Baldwin 545df8bae1dSRodney W. Grimes /* 546df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 547df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 548df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 549df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 550df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 551df8bae1dSRodney W. Grimes * process group and that of its children. 552df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 553df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 554df8bae1dSRodney W. Grimes */ 55526f9a767SRodney W. Grimes void 556df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 557df8bae1dSRodney W. Grimes register struct proc *p; 558df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 559df8bae1dSRodney W. Grimes int entering; 560df8bae1dSRodney W. Grimes { 561df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 562f591779bSSeigo Tanimura register struct session *mysession; 563f591779bSSeigo Tanimura 564f089b570SJohn Baldwin sx_assert(&proctree_lock, SX_LOCKED); 565f591779bSSeigo Tanimura PROC_LOCK_ASSERT(p, MA_NOTOWNED); 566f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED); 567f591779bSSeigo Tanimura SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED); 568df8bae1dSRodney W. Grimes 569df8bae1dSRodney W. Grimes /* 570df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 571df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 572df8bae1dSRodney W. Grimes */ 573f591779bSSeigo Tanimura mysession = pgrp->pg_session; 574df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 57502e878d9SJohn Baldwin hispgrp->pg_session == mysession) 57602e878d9SJohn Baldwin pgadjustjobc(pgrp, entering); 577df8bae1dSRodney W. Grimes 578df8bae1dSRodney W. Grimes /* 579df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 580df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 581df8bae1dSRodney W. Grimes * process groups. 582df8bae1dSRodney W. Grimes */ 583f591779bSSeigo Tanimura LIST_FOREACH(p, &p->p_children, p_sibling) { 58402e878d9SJohn Baldwin hispgrp = p->p_pgrp; 58502e878d9SJohn Baldwin if (hispgrp == pgrp || 58602e878d9SJohn Baldwin hispgrp->pg_session != mysession) 58702e878d9SJohn Baldwin continue; 58802e878d9SJohn Baldwin PROC_LOCK(p); 58902e878d9SJohn Baldwin if (p->p_state == PRS_ZOMBIE) { 59002e878d9SJohn Baldwin PROC_UNLOCK(p); 59102e878d9SJohn Baldwin continue; 592df8bae1dSRodney W. Grimes } 59302e878d9SJohn Baldwin PROC_UNLOCK(p); 59402e878d9SJohn Baldwin pgadjustjobc(hispgrp, entering); 595f591779bSSeigo Tanimura } 596dfd5dee1SPeter Wemm } 597df8bae1dSRodney W. Grimes 598df8bae1dSRodney W. Grimes /* 599df8bae1dSRodney W. Grimes * A process group has become orphaned; 600df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 601df8bae1dSRodney W. Grimes * hang-up all process in that group. 602df8bae1dSRodney W. Grimes */ 603df8bae1dSRodney W. Grimes static void 604df8bae1dSRodney W. Grimes orphanpg(pg) 605df8bae1dSRodney W. Grimes struct pgrp *pg; 606df8bae1dSRodney W. Grimes { 607df8bae1dSRodney W. Grimes register struct proc *p; 608df8bae1dSRodney W. Grimes 609f591779bSSeigo Tanimura PGRP_LOCK_ASSERT(pg, MA_OWNED); 610f591779bSSeigo Tanimura 6111b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 61202e878d9SJohn Baldwin PROC_LOCK(p); 613e602ba25SJulian Elischer if (P_SHOULDSTOP(p)) { 61402e878d9SJohn Baldwin PROC_UNLOCK(p); 6151b727751SPoul-Henning Kamp LIST_FOREACH(p, &pg->pg_members, p_pglist) { 61615e9ec51SJohn Baldwin PROC_LOCK(p); 6178451d0ddSKip Macy kern_psignal(p, SIGHUP); 6188451d0ddSKip Macy kern_psignal(p, SIGCONT); 61915e9ec51SJohn Baldwin PROC_UNLOCK(p); 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes return; 622df8bae1dSRodney W. Grimes } 62302e878d9SJohn Baldwin PROC_UNLOCK(p); 624df8bae1dSRodney W. Grimes } 625df8bae1dSRodney W. Grimes } 626df8bae1dSRodney W. Grimes 627572b4402SPoul-Henning Kamp void 628bc093719SEd Schouten sess_hold(struct session *s) 629572b4402SPoul-Henning Kamp { 630572b4402SPoul-Henning Kamp 631bc093719SEd Schouten refcount_acquire(&s->s_count); 632bc093719SEd Schouten } 633bc093719SEd Schouten 634bc093719SEd Schouten void 635bc093719SEd Schouten sess_release(struct session *s) 636bc093719SEd Schouten { 637bc093719SEd Schouten 638bc093719SEd Schouten if (refcount_release(&s->s_count)) { 639bc093719SEd Schouten if (s->s_ttyp != NULL) { 640bc093719SEd Schouten tty_lock(s->s_ttyp); 641bc093719SEd Schouten tty_rel_sess(s->s_ttyp, s); 642bc093719SEd Schouten } 643572b4402SPoul-Henning Kamp mtx_destroy(&s->s_mtx); 6441ede983cSDag-Erling Smørgrav free(s, M_SESSION); 645572b4402SPoul-Henning Kamp } 646572b4402SPoul-Henning Kamp } 647572b4402SPoul-Henning Kamp 648831031ceSBruce Evans #include "opt_ddb.h" 649831031ceSBruce Evans #ifdef DDB 650831031ceSBruce Evans #include <ddb/ddb.h> 651831031ceSBruce Evans 652831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump) 653df8bae1dSRodney W. Grimes { 654df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 655df8bae1dSRodney W. Grimes register struct proc *p; 656876a94eeSBruce Evans register int i; 657df8bae1dSRodney W. Grimes 658b75356e1SJeffrey Hsu for (i = 0; i <= pgrphash; i++) { 6591b727751SPoul-Henning Kamp if (!LIST_EMPTY(&pgrphashtbl[i])) { 660df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 6611b727751SPoul-Henning Kamp LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) { 662ac1e407bSBruce Evans printf( 663ac1e407bSBruce Evans "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n", 664ac1e407bSBruce Evans (void *)pgrp, (long)pgrp->pg_id, 665ac1e407bSBruce Evans (void *)pgrp->pg_session, 666b75356e1SJeffrey Hsu pgrp->pg_session->s_count, 6671b727751SPoul-Henning Kamp (void *)LIST_FIRST(&pgrp->pg_members)); 6681b727751SPoul-Henning Kamp LIST_FOREACH(p, &pgrp->pg_members, p_pglist) { 669ac1e407bSBruce Evans printf("\t\tpid %ld addr %p pgrp %p\n", 670ac1e407bSBruce Evans (long)p->p_pid, (void *)p, 671ac1e407bSBruce Evans (void *)p->p_pgrp); 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes } 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes } 676df8bae1dSRodney W. Grimes } 677831031ceSBruce Evans #endif /* DDB */ 678972f9b20SPoul-Henning Kamp 679972f9b20SPoul-Henning Kamp /* 680f8d90480SAttilio Rao * Calculate the kinfo_proc members which contain process-wide 681f8d90480SAttilio Rao * informations. 682f8d90480SAttilio Rao * Must be called with the target process locked. 683f8d90480SAttilio Rao */ 684f8d90480SAttilio Rao static void 685f8d90480SAttilio Rao fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp) 686f8d90480SAttilio Rao { 687f8d90480SAttilio Rao struct thread *td; 688f8d90480SAttilio Rao 689f8d90480SAttilio Rao PROC_LOCK_ASSERT(p, MA_OWNED); 690f8d90480SAttilio Rao 691f8d90480SAttilio Rao kp->ki_estcpu = 0; 692f8d90480SAttilio Rao kp->ki_pctcpu = 0; 693f8d90480SAttilio Rao FOREACH_THREAD_IN_PROC(p, td) { 694f8d90480SAttilio Rao thread_lock(td); 695f8d90480SAttilio Rao kp->ki_pctcpu += sched_pctcpu(td); 696f8d90480SAttilio Rao kp->ki_estcpu += td->td_estcpu; 697f8d90480SAttilio Rao thread_unlock(td); 698f8d90480SAttilio Rao } 699f8d90480SAttilio Rao } 700f8d90480SAttilio Rao 701f8d90480SAttilio Rao /* 7025032ff81SDon Lewis * Clear kinfo_proc and fill in any information that is common 7035032ff81SDon Lewis * to all threads in the process. 70465c9b430SJohn Baldwin * Must be called with the target process locked. 705972f9b20SPoul-Henning Kamp */ 7065032ff81SDon Lewis static void 7075032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp) 708972f9b20SPoul-Henning Kamp { 7094093529dSJeff Roberson struct thread *td0; 7101f7d2501SKirk McKusick struct tty *tp; 7111f7d2501SKirk McKusick struct session *sp; 712d079d0a0SPawel Jakub Dawidek struct ucred *cred; 71390af4afaSJohn Baldwin struct sigacts *ps; 714972f9b20SPoul-Henning Kamp 715374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 7161f7d2501SKirk McKusick bzero(kp, sizeof(*kp)); 717972f9b20SPoul-Henning Kamp 7181f7d2501SKirk McKusick kp->ki_structsize = sizeof(*kp); 7191f7d2501SKirk McKusick kp->ki_paddr = p; 7206617724cSJeff Roberson kp->ki_addr =/* p->p_addr; */0; /* XXX */ 7211f7d2501SKirk McKusick kp->ki_args = p->p_args; 7221f7d2501SKirk McKusick kp->ki_textvp = p->p_textvp; 7236c84de02SJohn Baldwin #ifdef KTRACE 724a5881ea5SJohn Baldwin kp->ki_tracep = p->p_tracevp; 7256c84de02SJohn Baldwin kp->ki_traceflag = p->p_traceflag; 7266c84de02SJohn Baldwin #endif 7271f7d2501SKirk McKusick kp->ki_fd = p->p_fd; 7281f7d2501SKirk McKusick kp->ki_vmspace = p->p_vmspace; 729cefcecbeSPawel Jakub Dawidek kp->ki_flag = p->p_flag; 730d079d0a0SPawel Jakub Dawidek cred = p->p_ucred; 731d079d0a0SPawel Jakub Dawidek if (cred) { 732d079d0a0SPawel Jakub Dawidek kp->ki_uid = cred->cr_uid; 733d079d0a0SPawel Jakub Dawidek kp->ki_ruid = cred->cr_ruid; 734d079d0a0SPawel Jakub Dawidek kp->ki_svuid = cred->cr_svuid; 73596fcc75fSRobert Watson kp->ki_cr_flags = 0; 73696fcc75fSRobert Watson if (cred->cr_flags & CRED_FLAG_CAPMODE) 73796fcc75fSRobert Watson kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE; 7381b5768beSBrooks Davis /* XXX bde doesn't like KI_NGROUPS */ 7391b5768beSBrooks Davis if (cred->cr_ngroups > KI_NGROUPS) { 7401b5768beSBrooks Davis kp->ki_ngroups = KI_NGROUPS; 7411b5768beSBrooks Davis kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW; 7421b5768beSBrooks Davis } else 743838d9858SBrooks Davis kp->ki_ngroups = cred->cr_ngroups; 7441b5768beSBrooks Davis bcopy(cred->cr_groups, kp->ki_groups, 7451b5768beSBrooks Davis kp->ki_ngroups * sizeof(gid_t)); 746d079d0a0SPawel Jakub Dawidek kp->ki_rgid = cred->cr_rgid; 747d079d0a0SPawel Jakub Dawidek kp->ki_svgid = cred->cr_svgid; 748cefcecbeSPawel Jakub Dawidek /* If jailed(cred), emulate the old P_JAILED flag. */ 749c78941e6SPawel Jakub Dawidek if (jailed(cred)) { 750cefcecbeSPawel Jakub Dawidek kp->ki_flag |= P_JAILED; 7510304c731SJamie Gritton /* If inside the jail, use 0 as a jail ID. */ 7520304c731SJamie Gritton if (cred->cr_prison != curthread->td_ucred->cr_prison) 753c78941e6SPawel Jakub Dawidek kp->ki_jid = cred->cr_prison->pr_id; 754c78941e6SPawel Jakub Dawidek } 7557123f4cdSEdward Tomasz Napierala strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name, 7567123f4cdSEdward Tomasz Napierala sizeof(kp->ki_loginclass)); 757972f9b20SPoul-Henning Kamp } 75890af4afaSJohn Baldwin ps = p->p_sigacts; 759d079d0a0SPawel Jakub Dawidek if (ps) { 76090af4afaSJohn Baldwin mtx_lock(&ps->ps_mtx); 76190af4afaSJohn Baldwin kp->ki_sigignore = ps->ps_sigignore; 76290af4afaSJohn Baldwin kp->ki_sigcatch = ps->ps_sigcatch; 76390af4afaSJohn Baldwin mtx_unlock(&ps->ps_mtx); 764d8c85307SJulian Elischer } 765e602ba25SJulian Elischer if (p->p_state != PRS_NEW && 766e602ba25SJulian Elischer p->p_state != PRS_ZOMBIE && 767e602ba25SJulian Elischer p->p_vmspace != NULL) { 7681f7d2501SKirk McKusick struct vmspace *vm = p->p_vmspace; 769cd73303cSDavid Greenman 7701f7d2501SKirk McKusick kp->ki_size = vm->vm_map.size; 7711f7d2501SKirk McKusick kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/ 772ceff7f2aSTim J. Robbins FOREACH_THREAD_IN_PROC(p, td0) { 773ceff7f2aSTim J. Robbins if (!TD_IS_SWAPPED(td0)) 774ceff7f2aSTim J. Robbins kp->ki_rssize += td0->td_kstack_pages; 775ceff7f2aSTim J. Robbins } 7761f7d2501SKirk McKusick kp->ki_swrss = vm->vm_swrss; 7771f7d2501SKirk McKusick kp->ki_tsize = vm->vm_tsize; 7781f7d2501SKirk McKusick kp->ki_dsize = vm->vm_dsize; 7791f7d2501SKirk McKusick kp->ki_ssize = vm->vm_ssize; 7805032ff81SDon Lewis } else if (p->p_state == PRS_ZOMBIE) 7815032ff81SDon Lewis kp->ki_stat = SZOMB; 782b61ce5b0SJeff Roberson if (kp->ki_flag & P_INMEM) 783b61ce5b0SJeff Roberson kp->ki_sflag = PS_INMEM; 784b61ce5b0SJeff Roberson else 785b61ce5b0SJeff Roberson kp->ki_sflag = 0; 78654b0e65fSJeff Roberson /* Calculate legacy swtime as seconds since 'swtick'. */ 78754b0e65fSJeff Roberson kp->ki_swtime = (ticks - p->p_swtick) / hz; 788cebabef0SPawel Jakub Dawidek kp->ki_pid = p->p_pid; 789cebabef0SPawel Jakub Dawidek kp->ki_nice = p->p_nice; 79078c85e8dSJohn Baldwin kp->ki_start = p->p_stats->p_start; 79178c85e8dSJohn Baldwin timevaladd(&kp->ki_start, &boottime); 792a1fe14bcSAttilio Rao PROC_SLOCK(p); 7938e6fa660SJohn Baldwin rufetch(p, &kp->ki_rusage); 7948e6fa660SJohn Baldwin kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime); 79578c85e8dSJohn Baldwin calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime); 796a1fe14bcSAttilio Rao PROC_SUNLOCK(p); 79778c85e8dSJohn Baldwin calccru(p, &kp->ki_childutime, &kp->ki_childstime); 798a37e14e1SEdward Tomasz Napierala /* Some callers want child times in a single value. */ 79978c85e8dSJohn Baldwin kp->ki_childtime = kp->ki_childstime; 80078c85e8dSJohn Baldwin timevaladd(&kp->ki_childtime, &kp->ki_childutime); 801a37e14e1SEdward Tomasz Napierala 802f591779bSSeigo Tanimura tp = NULL; 8031f7d2501SKirk McKusick if (p->p_pgrp) { 8041f7d2501SKirk McKusick kp->ki_pgid = p->p_pgrp->pg_id; 8051f7d2501SKirk McKusick kp->ki_jobc = p->p_pgrp->pg_jobc; 8061f7d2501SKirk McKusick sp = p->p_pgrp->pg_session; 8071f7d2501SKirk McKusick 8081f7d2501SKirk McKusick if (sp != NULL) { 8091f7d2501SKirk McKusick kp->ki_sid = sp->s_sid; 810f591779bSSeigo Tanimura SESS_LOCK(sp); 811e80fb434SRobert Drehmel strlcpy(kp->ki_login, sp->s_login, 812e80fb434SRobert Drehmel sizeof(kp->ki_login)); 8131f7d2501SKirk McKusick if (sp->s_ttyvp) 814b8e6bf1eSJohn Baldwin kp->ki_kiflag |= KI_CTTY; 8151f7d2501SKirk McKusick if (SESS_LEADER(p)) 8161f7d2501SKirk McKusick kp->ki_kiflag |= KI_SLEADER; 817bc093719SEd Schouten /* XXX proctree_lock */ 818f591779bSSeigo Tanimura tp = sp->s_ttyp; 819f591779bSSeigo Tanimura SESS_UNLOCK(sp); 820cd73303cSDavid Greenman } 821cd73303cSDavid Greenman } 822f591779bSSeigo Tanimura if ((p->p_flag & P_CONTROLT) && tp != NULL) { 823bc093719SEd Schouten kp->ki_tdev = tty_udev(tp); 8241f7d2501SKirk McKusick kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 8251f7d2501SKirk McKusick if (tp->t_session) 8261f7d2501SKirk McKusick kp->ki_tsid = tp->t_session->s_sid; 827972f9b20SPoul-Henning Kamp } else 828f3732fd1SPoul-Henning Kamp kp->ki_tdev = NODEV; 82913b762a3SEd Maste if (p->p_comm[0] != '\0') 830e80fb434SRobert Drehmel strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm)); 831078842c5SGarance A Drosehn if (p->p_sysent && p->p_sysent->sv_name != NULL && 832078842c5SGarance A Drosehn p->p_sysent->sv_name[0] != '\0') 833078842c5SGarance A Drosehn strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul)); 8341d9c5696SJuli Mallett kp->ki_siglist = p->p_siglist; 8351f7d2501SKirk McKusick kp->ki_xstat = p->p_xstat; 8361f7d2501SKirk McKusick kp->ki_acflag = p->p_acflag; 8371f7d2501SKirk McKusick kp->ki_lock = p->p_lock; 83842a4ed99SJohn Baldwin if (p->p_pptr) 83942a4ed99SJohn Baldwin kp->ki_ppid = p->p_pptr->p_pid; 840972f9b20SPoul-Henning Kamp } 841972f9b20SPoul-Henning Kamp 8425032ff81SDon Lewis /* 84300305546SKonstantin Belousov * Fill in information that is thread specific. Must be called with 84400305546SKonstantin Belousov * target process locked. If 'preferthread' is set, overwrite certain 84500305546SKonstantin Belousov * process-related fields that are maintained for both threads and 84600305546SKonstantin Belousov * processes. 8475032ff81SDon Lewis */ 8485032ff81SDon Lewis static void 849d92909c1SRobert Watson fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) 8505032ff81SDon Lewis { 8515032ff81SDon Lewis struct proc *p; 8525032ff81SDon Lewis 8535032ff81SDon Lewis p = td->td_proc; 8546239ef1dSEd Maste kp->ki_tdaddr = td; 855374ae2a3SJeff Roberson PROC_LOCK_ASSERT(p, MA_OWNED); 8565032ff81SDon Lewis 8572417d97eSJohn Baldwin if (preferthread) 8582417d97eSJohn Baldwin PROC_SLOCK(p); 859982d11f8SJeff Roberson thread_lock(td); 8605032ff81SDon Lewis if (td->td_wmesg != NULL) 8615032ff81SDon Lewis strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); 8625032ff81SDon Lewis else 8635032ff81SDon Lewis bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); 864925af544SBjoern A. Zeeb strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)); 8655032ff81SDon Lewis if (TD_ON_LOCK(td)) { 8665032ff81SDon Lewis kp->ki_kiflag |= KI_LOCKBLOCK; 8675032ff81SDon Lewis strlcpy(kp->ki_lockname, td->td_lockname, 8685032ff81SDon Lewis sizeof(kp->ki_lockname)); 8695032ff81SDon Lewis } else { 8705032ff81SDon Lewis kp->ki_kiflag &= ~KI_LOCKBLOCK; 8715032ff81SDon Lewis bzero(kp->ki_lockname, sizeof(kp->ki_lockname)); 8725032ff81SDon Lewis } 8735032ff81SDon Lewis 8746617724cSJeff Roberson if (p->p_state == PRS_NORMAL) { /* approximate. */ 8755032ff81SDon Lewis if (TD_ON_RUNQ(td) || 8765032ff81SDon Lewis TD_CAN_RUN(td) || 8775032ff81SDon Lewis TD_IS_RUNNING(td)) { 8785032ff81SDon Lewis kp->ki_stat = SRUN; 8795032ff81SDon Lewis } else if (P_SHOULDSTOP(p)) { 8805032ff81SDon Lewis kp->ki_stat = SSTOP; 8815032ff81SDon Lewis } else if (TD_IS_SLEEPING(td)) { 8825032ff81SDon Lewis kp->ki_stat = SSLEEP; 8835032ff81SDon Lewis } else if (TD_ON_LOCK(td)) { 8845032ff81SDon Lewis kp->ki_stat = SLOCK; 8855032ff81SDon Lewis } else { 8865032ff81SDon Lewis kp->ki_stat = SWAIT; 8875032ff81SDon Lewis } 8883357835aSDavid Xu } else if (p->p_state == PRS_ZOMBIE) { 8893357835aSDavid Xu kp->ki_stat = SZOMB; 8905032ff81SDon Lewis } else { 8915032ff81SDon Lewis kp->ki_stat = SIDL; 8925032ff81SDon Lewis } 8935032ff81SDon Lewis 8945032ff81SDon Lewis /* Things in the thread */ 8955032ff81SDon Lewis kp->ki_wchan = td->td_wchan; 8965032ff81SDon Lewis kp->ki_pri.pri_level = td->td_priority; 8975032ff81SDon Lewis kp->ki_pri.pri_native = td->td_base_pri; 8985032ff81SDon Lewis kp->ki_lastcpu = td->td_lastcpu; 8995032ff81SDon Lewis kp->ki_oncpu = td->td_oncpu; 9005032ff81SDon Lewis kp->ki_tdflags = td->td_flags; 9015032ff81SDon Lewis kp->ki_tid = td->td_tid; 9025032ff81SDon Lewis kp->ki_numthreads = p->p_numthreads; 9035032ff81SDon Lewis kp->ki_pcb = td->td_pcb; 9045032ff81SDon Lewis kp->ki_kstack = (void *)td->td_kstack; 90554b0e65fSJeff Roberson kp->ki_slptime = (ticks - td->td_slptick) / hz; 9068460a577SJohn Birrell kp->ki_pri.pri_class = td->td_pri_class; 9078460a577SJohn Birrell kp->ki_pri.pri_user = td->td_user_pri; 9085032ff81SDon Lewis 909f8d90480SAttilio Rao if (preferthread) { 9102417d97eSJohn Baldwin rufetchtd(td, &kp->ki_rusage); 911213c077fSKonstantin Belousov kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime); 912f8d90480SAttilio Rao kp->ki_pctcpu = sched_pctcpu(td); 913f8d90480SAttilio Rao kp->ki_estcpu = td->td_estcpu; 914f8d90480SAttilio Rao } 915d92909c1SRobert Watson 9165032ff81SDon Lewis /* We can't get this anymore but ps etc never used it anyway. */ 9175032ff81SDon Lewis kp->ki_rqindex = 0; 9185032ff81SDon Lewis 91900305546SKonstantin Belousov if (preferthread) 92000305546SKonstantin Belousov kp->ki_siglist = td->td_siglist; 9215032ff81SDon Lewis kp->ki_sigmask = td->td_sigmask; 922982d11f8SJeff Roberson thread_unlock(td); 9232417d97eSJohn Baldwin if (preferthread) 9242417d97eSJohn Baldwin PROC_SUNLOCK(p); 9255032ff81SDon Lewis } 9265032ff81SDon Lewis 9275032ff81SDon Lewis /* 9285032ff81SDon Lewis * Fill in a kinfo_proc structure for the specified process. 9295032ff81SDon Lewis * Must be called with the target process locked. 9305032ff81SDon Lewis */ 9315032ff81SDon Lewis void 9325032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp) 9335032ff81SDon Lewis { 9345032ff81SDon Lewis 935f8d90480SAttilio Rao MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 936f8d90480SAttilio Rao 9375032ff81SDon Lewis fill_kinfo_proc_only(p, kp); 938d92909c1SRobert Watson fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0); 939f8d90480SAttilio Rao fill_kinfo_aggregate(p, kp); 9405032ff81SDon Lewis } 9415032ff81SDon Lewis 9428b059651SDavid Schultz struct pstats * 9438b059651SDavid Schultz pstats_alloc(void) 9448b059651SDavid Schultz { 9458b059651SDavid Schultz 9468b059651SDavid Schultz return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK)); 9478b059651SDavid Schultz } 9488b059651SDavid Schultz 9498b059651SDavid Schultz /* 9508b059651SDavid Schultz * Copy parts of p_stats; zero the rest of p_stats (statistics). 9518b059651SDavid Schultz */ 9528b059651SDavid Schultz void 9538b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst) 9548b059651SDavid Schultz { 9558b059651SDavid Schultz 9568b059651SDavid Schultz bzero(&dst->pstat_startzero, 9576db36923SDavid Schultz __rangeof(struct pstats, pstat_startzero, pstat_endzero)); 9588b059651SDavid Schultz bcopy(&src->pstat_startcopy, &dst->pstat_startcopy, 9596db36923SDavid Schultz __rangeof(struct pstats, pstat_startcopy, pstat_endcopy)); 9608b059651SDavid Schultz } 9618b059651SDavid Schultz 9628b059651SDavid Schultz void 9638b059651SDavid Schultz pstats_free(struct pstats *ps) 9648b059651SDavid Schultz { 9658b059651SDavid Schultz 9668b059651SDavid Schultz free(ps, M_SUBPROC); 9678b059651SDavid Schultz } 9688b059651SDavid Schultz 9698b059651SDavid Schultz /* 97042a4ed99SJohn Baldwin * Locate a zombie process by number 97142a4ed99SJohn Baldwin */ 97242a4ed99SJohn Baldwin struct proc * 9733ce93e4eSPoul-Henning Kamp zpfind(pid_t pid) 9743ce93e4eSPoul-Henning Kamp { 9753ce93e4eSPoul-Henning Kamp struct proc *p; 9763ce93e4eSPoul-Henning Kamp 9771005a129SJohn Baldwin sx_slock(&allproc_lock); 9781b727751SPoul-Henning Kamp LIST_FOREACH(p, &zombproc, p_list) 97933a9ed9dSJohn Baldwin if (p->p_pid == pid) { 98033a9ed9dSJohn Baldwin PROC_LOCK(p); 981c0c25570SJake Burkholder break; 98233a9ed9dSJohn Baldwin } 9831005a129SJohn Baldwin sx_sunlock(&allproc_lock); 9843ce93e4eSPoul-Henning Kamp return (p); 9853ce93e4eSPoul-Henning Kamp } 9863ce93e4eSPoul-Henning Kamp 98730c6f34eSScott Long #define KERN_PROC_ZOMBMASK 0x3 98830c6f34eSScott Long #define KERN_PROC_NOTHREADS 0x4 9893ce93e4eSPoul-Henning Kamp 99005e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32 99105e06d11SKonstantin Belousov 99205e06d11SKonstantin Belousov /* 99305e06d11SKonstantin Belousov * This function is typically used to copy out the kernel address, so 99405e06d11SKonstantin Belousov * it can be replaced by assignment of zero. 99505e06d11SKonstantin Belousov */ 99605e06d11SKonstantin Belousov static inline uint32_t 99705e06d11SKonstantin Belousov ptr32_trim(void *ptr) 99805e06d11SKonstantin Belousov { 99905e06d11SKonstantin Belousov uintptr_t uptr; 100005e06d11SKonstantin Belousov 100105e06d11SKonstantin Belousov uptr = (uintptr_t)ptr; 100205e06d11SKonstantin Belousov return ((uptr > UINT_MAX) ? 0 : uptr); 100305e06d11SKonstantin Belousov } 100405e06d11SKonstantin Belousov 100505e06d11SKonstantin Belousov #define PTRTRIM_CP(src,dst,fld) \ 100605e06d11SKonstantin Belousov do { (dst).fld = ptr32_trim((src).fld); } while (0) 100705e06d11SKonstantin Belousov 100805e06d11SKonstantin Belousov static void 100905e06d11SKonstantin Belousov freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) 101005e06d11SKonstantin Belousov { 101105e06d11SKonstantin Belousov int i; 101205e06d11SKonstantin Belousov 101305e06d11SKonstantin Belousov bzero(ki32, sizeof(struct kinfo_proc32)); 101405e06d11SKonstantin Belousov ki32->ki_structsize = sizeof(struct kinfo_proc32); 101505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_layout); 101605e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_args); 101705e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_paddr); 101805e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_addr); 101905e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_tracep); 102005e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_textvp); 102105e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_fd); 102205e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_vmspace); 102305e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_wchan); 102405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pid); 102505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ppid); 102605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pgid); 102705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tpgid); 102805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sid); 102905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tsid); 103005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_jobc); 103105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tdev); 103205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_siglist); 103305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigmask); 103405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigignore); 103505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sigcatch); 103605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_uid); 103705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ruid); 103805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_svuid); 103905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rgid); 104005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_svgid); 104105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ngroups); 104205e06d11SKonstantin Belousov for (i = 0; i < KI_NGROUPS; i++) 104305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_groups[i]); 104405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_size); 104505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rssize); 104605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_swrss); 104705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tsize); 104805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_dsize); 104905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_ssize); 105005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_xstat); 105105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_acflag); 105205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pctcpu); 105305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_estcpu); 105405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_slptime); 105505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_swtime); 105605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_runtime); 105705e06d11SKonstantin Belousov TV_CP(*ki, *ki32, ki_start); 105805e06d11SKonstantin Belousov TV_CP(*ki, *ki32, ki_childtime); 105905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_flag); 106005e06d11SKonstantin Belousov CP(*ki, *ki32, ki_kiflag); 106105e06d11SKonstantin Belousov CP(*ki, *ki32, ki_traceflag); 106205e06d11SKonstantin Belousov CP(*ki, *ki32, ki_stat); 106305e06d11SKonstantin Belousov CP(*ki, *ki32, ki_nice); 106405e06d11SKonstantin Belousov CP(*ki, *ki32, ki_lock); 106505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_rqindex); 106605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_oncpu); 106705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_lastcpu); 1068925af544SBjoern A. Zeeb bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1); 106905e06d11SKonstantin Belousov bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1); 107005e06d11SKonstantin Belousov bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1); 107105e06d11SKonstantin Belousov bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1); 107205e06d11SKonstantin Belousov bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); 107305e06d11SKonstantin Belousov bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); 10747123f4cdSEdward Tomasz Napierala bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); 107505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_cr_flags); 107605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_jid); 107705e06d11SKonstantin Belousov CP(*ki, *ki32, ki_numthreads); 107805e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tid); 107905e06d11SKonstantin Belousov CP(*ki, *ki32, ki_pri); 108005e06d11SKonstantin Belousov freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage); 108105e06d11SKonstantin Belousov freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch); 108205e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_pcb); 108305e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_kstack); 108405e06d11SKonstantin Belousov PTRTRIM_CP(*ki, *ki32, ki_udata); 108505e06d11SKonstantin Belousov CP(*ki, *ki32, ki_sflag); 108605e06d11SKonstantin Belousov CP(*ki, *ki32, ki_tdflags); 108705e06d11SKonstantin Belousov } 108805e06d11SKonstantin Belousov 108905e06d11SKonstantin Belousov static int 109005e06d11SKonstantin Belousov sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 109105e06d11SKonstantin Belousov { 109205e06d11SKonstantin Belousov struct kinfo_proc32 ki32; 109305e06d11SKonstantin Belousov int error; 109405e06d11SKonstantin Belousov 109505e06d11SKonstantin Belousov if (req->flags & SCTL_MASK32) { 109605e06d11SKonstantin Belousov freebsd32_kinfo_proc_out(ki, &ki32); 1097e68d26fdSKonstantin Belousov error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32)); 109805e06d11SKonstantin Belousov } else 1099e68d26fdSKonstantin Belousov error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)); 110005e06d11SKonstantin Belousov return (error); 110105e06d11SKonstantin Belousov } 110205e06d11SKonstantin Belousov #else 110305e06d11SKonstantin Belousov static int 110405e06d11SKonstantin Belousov sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req) 110505e06d11SKonstantin Belousov { 110605e06d11SKonstantin Belousov 1107e68d26fdSKonstantin Belousov return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc))); 110805e06d11SKonstantin Belousov } 110905e06d11SKonstantin Belousov #endif 111005e06d11SKonstantin Belousov 111165c9b430SJohn Baldwin /* 111265c9b430SJohn Baldwin * Must be called with the process locked and will return with it unlocked. 111365c9b430SJohn Baldwin */ 11143ce93e4eSPoul-Henning Kamp static int 111530c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags) 11163ce93e4eSPoul-Henning Kamp { 111730c6f34eSScott Long struct thread *td; 11181f7d2501SKirk McKusick struct kinfo_proc kinfo_proc; 111930c6f34eSScott Long int error = 0; 112033a9ed9dSJohn Baldwin struct proc *np; 11213ce93e4eSPoul-Henning Kamp pid_t pid = p->p_pid; 11223ce93e4eSPoul-Henning Kamp 112365c9b430SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 1124f8d90480SAttilio Rao MPASS(FIRST_THREAD_IN_PROC(p) != NULL); 112530c6f34eSScott Long 1126f8d90480SAttilio Rao fill_kinfo_proc(p, &kinfo_proc); 1127f8d90480SAttilio Rao if (flags & KERN_PROC_NOTHREADS) 112805e06d11SKonstantin Belousov error = sysctl_out_proc_copyout(&kinfo_proc, req); 1129f8d90480SAttilio Rao else { 113030c6f34eSScott Long FOREACH_THREAD_IN_PROC(p, td) { 1131d92909c1SRobert Watson fill_kinfo_thread(td, &kinfo_proc, 1); 113205e06d11SKonstantin Belousov error = sysctl_out_proc_copyout(&kinfo_proc, req); 113330c6f34eSScott Long if (error) 113430c6f34eSScott Long break; 113530c6f34eSScott Long } 113630c6f34eSScott Long } 113730c6f34eSScott Long PROC_UNLOCK(p); 11383ce93e4eSPoul-Henning Kamp if (error) 11393ce93e4eSPoul-Henning Kamp return (error); 114030c6f34eSScott Long if (flags & KERN_PROC_ZOMBMASK) 114133a9ed9dSJohn Baldwin np = zpfind(pid); 114233a9ed9dSJohn Baldwin else { 114333a9ed9dSJohn Baldwin if (pid == 0) 114433a9ed9dSJohn Baldwin return (0); 114533a9ed9dSJohn Baldwin np = pfind(pid); 114633a9ed9dSJohn Baldwin } 114733a9ed9dSJohn Baldwin if (np == NULL) 1148f308bdddSKevin Lo return (ESRCH); 114933a9ed9dSJohn Baldwin if (np != p) { 115033a9ed9dSJohn Baldwin PROC_UNLOCK(np); 1151f308bdddSKevin Lo return (ESRCH); 115233a9ed9dSJohn Baldwin } 115333a9ed9dSJohn Baldwin PROC_UNLOCK(np); 11543ce93e4eSPoul-Henning Kamp return (0); 11553ce93e4eSPoul-Henning Kamp } 11563ce93e4eSPoul-Henning Kamp 1157972f9b20SPoul-Henning Kamp static int 115882d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS) 1159972f9b20SPoul-Henning Kamp { 1160972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 1161972f9b20SPoul-Henning Kamp u_int namelen = arg2; 1162972f9b20SPoul-Henning Kamp struct proc *p; 11632648efa6SDaniel Eischen int flags, doingzomb, oid_number; 1164972f9b20SPoul-Henning Kamp int error = 0; 1165972f9b20SPoul-Henning Kamp 11662648efa6SDaniel Eischen oid_number = oidp->oid_number; 11672648efa6SDaniel Eischen if (oid_number != KERN_PROC_ALL && 11682648efa6SDaniel Eischen (oid_number & KERN_PROC_INC_THREAD) == 0) 11692648efa6SDaniel Eischen flags = KERN_PROC_NOTHREADS; 11702648efa6SDaniel Eischen else { 11712648efa6SDaniel Eischen flags = 0; 11722648efa6SDaniel Eischen oid_number &= ~KERN_PROC_INC_THREAD; 11732648efa6SDaniel Eischen } 11742648efa6SDaniel Eischen if (oid_number == KERN_PROC_PID) { 11753ce93e4eSPoul-Henning Kamp if (namelen != 1) 1176972f9b20SPoul-Henning Kamp return (EINVAL); 11775032ff81SDon Lewis error = sysctl_wire_old_buffer(req, 0); 11785032ff81SDon Lewis if (error) 11795032ff81SDon Lewis return (error); 11803ce93e4eSPoul-Henning Kamp p = pfind((pid_t)name[0]); 11813ce93e4eSPoul-Henning Kamp if (!p) 1182e76bad96SRobert Drehmel return (ESRCH); 1183e76bad96SRobert Drehmel if ((error = p_cansee(curthread, p))) { 118433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1185e76bad96SRobert Drehmel return (error); 118633a9ed9dSJohn Baldwin } 11872648efa6SDaniel Eischen error = sysctl_out_proc(p, req, flags); 11883ce93e4eSPoul-Henning Kamp return (error); 11893ce93e4eSPoul-Henning Kamp } 11903ddaef40STim J. Robbins 11912648efa6SDaniel Eischen switch (oid_number) { 11923ddaef40STim J. Robbins case KERN_PROC_ALL: 11933ddaef40STim J. Robbins if (namelen != 0) 11943ce93e4eSPoul-Henning Kamp return (EINVAL); 11953ddaef40STim J. Robbins break; 119625e247afSPeter Wemm case KERN_PROC_PROC: 119725e247afSPeter Wemm if (namelen != 0 && namelen != 1) 119825e247afSPeter Wemm return (EINVAL); 119925e247afSPeter Wemm break; 12003ddaef40STim J. Robbins default: 12013ddaef40STim J. Robbins if (namelen != 1) 12023ddaef40STim J. Robbins return (EINVAL); 12033ddaef40STim J. Robbins break; 12043ddaef40STim J. Robbins } 12053ce93e4eSPoul-Henning Kamp 1206972f9b20SPoul-Henning Kamp if (!req->oldptr) { 12073ce93e4eSPoul-Henning Kamp /* overestimate by 5 procs */ 1208972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 1209972f9b20SPoul-Henning Kamp if (error) 1210972f9b20SPoul-Henning Kamp return (error); 1211972f9b20SPoul-Henning Kamp } 121247934cefSDon Lewis error = sysctl_wire_old_buffer(req, 0); 121347934cefSDon Lewis if (error != 0) 121447934cefSDon Lewis return (error); 12151005a129SJohn Baldwin sx_slock(&allproc_lock); 12163ce93e4eSPoul-Henning Kamp for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 12173ce93e4eSPoul-Henning Kamp if (!doingzomb) 12181b727751SPoul-Henning Kamp p = LIST_FIRST(&allproc); 12193ce93e4eSPoul-Henning Kamp else 12201b727751SPoul-Henning Kamp p = LIST_FIRST(&zombproc); 12211b727751SPoul-Henning Kamp for (; p != 0; p = LIST_NEXT(p, p_list)) { 122202e878d9SJohn Baldwin /* 122302e878d9SJohn Baldwin * Skip embryonic processes. 122402e878d9SJohn Baldwin */ 12258e6fa660SJohn Baldwin PROC_LOCK(p); 122602e878d9SJohn Baldwin if (p->p_state == PRS_NEW) { 12278e6fa660SJohn Baldwin PROC_UNLOCK(p); 122802e878d9SJohn Baldwin continue; 122902e878d9SJohn Baldwin } 1230b241b0a2SJuli Mallett KASSERT(p->p_ucred != NULL, 1231b241b0a2SJuli Mallett ("process credential is NULL for non-NEW proc")); 1232972f9b20SPoul-Henning Kamp /* 1233387d2c03SRobert Watson * Show a user only appropriate processes. 123403f808c5SPaul Saab */ 1235f44d9e24SJohn Baldwin if (p_cansee(curthread, p)) { 123665c9b430SJohn Baldwin PROC_UNLOCK(p); 123703f808c5SPaul Saab continue; 123865c9b430SJohn Baldwin } 123903f808c5SPaul Saab /* 1240972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 1241972f9b20SPoul-Henning Kamp * do by session. 1242972f9b20SPoul-Henning Kamp */ 12432648efa6SDaniel Eischen switch (oid_number) { 1244972f9b20SPoul-Henning Kamp 1245078842c5SGarance A Drosehn case KERN_PROC_GID: 1246b241b0a2SJuli Mallett if (p->p_ucred->cr_gid != (gid_t)name[0]) { 1247078842c5SGarance A Drosehn PROC_UNLOCK(p); 1248078842c5SGarance A Drosehn continue; 1249078842c5SGarance A Drosehn } 1250078842c5SGarance A Drosehn break; 1251078842c5SGarance A Drosehn 1252972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 1253972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 12543ce93e4eSPoul-Henning Kamp if (p->p_pgrp == NULL || 1255f591779bSSeigo Tanimura p->p_pgrp->pg_id != (pid_t)name[0]) { 1256f591779bSSeigo Tanimura PROC_UNLOCK(p); 1257972f9b20SPoul-Henning Kamp continue; 1258f591779bSSeigo Tanimura } 1259972f9b20SPoul-Henning Kamp break; 1260972f9b20SPoul-Henning Kamp 1261b8fdc89dSGarance A Drosehn case KERN_PROC_RGID: 1262b241b0a2SJuli Mallett if (p->p_ucred->cr_rgid != (gid_t)name[0]) { 1263b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1264b8fdc89dSGarance A Drosehn continue; 1265b8fdc89dSGarance A Drosehn } 1266b8fdc89dSGarance A Drosehn break; 1267b8fdc89dSGarance A Drosehn 1268b8fdc89dSGarance A Drosehn case KERN_PROC_SESSION: 1269b8fdc89dSGarance A Drosehn if (p->p_session == NULL || 1270b8fdc89dSGarance A Drosehn p->p_session->s_sid != (pid_t)name[0]) { 1271b8fdc89dSGarance A Drosehn PROC_UNLOCK(p); 1272b8fdc89dSGarance A Drosehn continue; 1273b8fdc89dSGarance A Drosehn } 1274b8fdc89dSGarance A Drosehn break; 1275b8fdc89dSGarance A Drosehn 1276972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 1277972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 1278f591779bSSeigo Tanimura p->p_session == NULL) { 1279f591779bSSeigo Tanimura PROC_UNLOCK(p); 1280972f9b20SPoul-Henning Kamp continue; 1281f591779bSSeigo Tanimura } 1282bc093719SEd Schouten /* XXX proctree_lock */ 1283f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 1284f591779bSSeigo Tanimura if (p->p_session->s_ttyp == NULL || 1285bc093719SEd Schouten tty_udev(p->p_session->s_ttyp) != 1286f3732fd1SPoul-Henning Kamp (dev_t)name[0]) { 1287f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1288f591779bSSeigo Tanimura PROC_UNLOCK(p); 1289f591779bSSeigo Tanimura continue; 1290f591779bSSeigo Tanimura } 1291f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 1292972f9b20SPoul-Henning Kamp break; 1293972f9b20SPoul-Henning Kamp 1294972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 1295b241b0a2SJuli Mallett if (p->p_ucred->cr_uid != (uid_t)name[0]) { 129665c9b430SJohn Baldwin PROC_UNLOCK(p); 1297972f9b20SPoul-Henning Kamp continue; 129865c9b430SJohn Baldwin } 1299972f9b20SPoul-Henning Kamp break; 1300972f9b20SPoul-Henning Kamp 1301972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 1302b241b0a2SJuli Mallett if (p->p_ucred->cr_ruid != (uid_t)name[0]) { 130365c9b430SJohn Baldwin PROC_UNLOCK(p); 1304972f9b20SPoul-Henning Kamp continue; 130565c9b430SJohn Baldwin } 1306972f9b20SPoul-Henning Kamp break; 130730c6f34eSScott Long 130830c6f34eSScott Long case KERN_PROC_PROC: 130930c6f34eSScott Long break; 131030c6f34eSScott Long 131130c6f34eSScott Long default: 131230c6f34eSScott Long break; 131330c6f34eSScott Long 1314972f9b20SPoul-Henning Kamp } 1315972f9b20SPoul-Henning Kamp 131630c6f34eSScott Long error = sysctl_out_proc(p, req, flags | doingzomb); 1317553629ebSJake Burkholder if (error) { 13181005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1319972f9b20SPoul-Henning Kamp return (error); 1320972f9b20SPoul-Henning Kamp } 1321972f9b20SPoul-Henning Kamp } 1322553629ebSJake Burkholder } 13231005a129SJohn Baldwin sx_sunlock(&allproc_lock); 1324972f9b20SPoul-Henning Kamp return (0); 1325972f9b20SPoul-Henning Kamp } 1326972f9b20SPoul-Henning Kamp 1327c1508b28SAlfred Perlstein struct pargs * 1328c1508b28SAlfred Perlstein pargs_alloc(int len) 1329c1508b28SAlfred Perlstein { 1330c1508b28SAlfred Perlstein struct pargs *pa; 1331c1508b28SAlfred Perlstein 13321ede983cSDag-Erling Smørgrav pa = malloc(sizeof(struct pargs) + len, M_PARGS, 1333a163d034SWarner Losh M_WAITOK); 133455b4a5aeSJohn Baldwin refcount_init(&pa->ar_ref, 1); 1335c1508b28SAlfred Perlstein pa->ar_length = len; 1336c1508b28SAlfred Perlstein return (pa); 1337c1508b28SAlfred Perlstein } 1338c1508b28SAlfred Perlstein 133958e8af1bSKonstantin Belousov static void 1340c1508b28SAlfred Perlstein pargs_free(struct pargs *pa) 1341c1508b28SAlfred Perlstein { 1342c1508b28SAlfred Perlstein 13431ede983cSDag-Erling Smørgrav free(pa, M_PARGS); 1344c1508b28SAlfred Perlstein } 1345c1508b28SAlfred Perlstein 1346c1508b28SAlfred Perlstein void 1347c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa) 1348c1508b28SAlfred Perlstein { 1349c1508b28SAlfred Perlstein 1350c1508b28SAlfred Perlstein if (pa == NULL) 1351c1508b28SAlfred Perlstein return; 135255b4a5aeSJohn Baldwin refcount_acquire(&pa->ar_ref); 1353c1508b28SAlfred Perlstein } 1354c1508b28SAlfred Perlstein 1355c1508b28SAlfred Perlstein void 1356c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa) 1357c1508b28SAlfred Perlstein { 1358c1508b28SAlfred Perlstein 1359c1508b28SAlfred Perlstein if (pa == NULL) 1360c1508b28SAlfred Perlstein return; 136155b4a5aeSJohn Baldwin if (refcount_release(&pa->ar_ref)) 1362c1508b28SAlfred Perlstein pargs_free(pa); 1363c1508b28SAlfred Perlstein } 1364c1508b28SAlfred Perlstein 1365c5cfcb1cSMikolaj Golub static int 1366c5cfcb1cSMikolaj Golub proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf, 1367c5cfcb1cSMikolaj Golub size_t len) 1368c5cfcb1cSMikolaj Golub { 1369c5cfcb1cSMikolaj Golub struct iovec iov; 1370c5cfcb1cSMikolaj Golub struct uio uio; 1371c5cfcb1cSMikolaj Golub 1372c5cfcb1cSMikolaj Golub iov.iov_base = (caddr_t)buf; 1373c5cfcb1cSMikolaj Golub iov.iov_len = len; 1374c5cfcb1cSMikolaj Golub uio.uio_iov = &iov; 1375c5cfcb1cSMikolaj Golub uio.uio_iovcnt = 1; 1376c5cfcb1cSMikolaj Golub uio.uio_offset = offset; 1377c5cfcb1cSMikolaj Golub uio.uio_resid = (ssize_t)len; 1378c5cfcb1cSMikolaj Golub uio.uio_segflg = UIO_SYSSPACE; 1379c5cfcb1cSMikolaj Golub uio.uio_rw = UIO_READ; 1380c5cfcb1cSMikolaj Golub uio.uio_td = td; 1381c5cfcb1cSMikolaj Golub 1382c5cfcb1cSMikolaj Golub return (proc_rwmem(p, &uio)); 1383c5cfcb1cSMikolaj Golub } 1384c5cfcb1cSMikolaj Golub 1385c5cfcb1cSMikolaj Golub static int 1386c5cfcb1cSMikolaj Golub proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf, 1387c5cfcb1cSMikolaj Golub size_t len) 1388c5cfcb1cSMikolaj Golub { 1389c5cfcb1cSMikolaj Golub size_t i; 1390c5cfcb1cSMikolaj Golub int error; 1391c5cfcb1cSMikolaj Golub 1392c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len); 1393c5cfcb1cSMikolaj Golub /* 1394c5cfcb1cSMikolaj Golub * Reading the chunk may validly return EFAULT if the string is shorter 1395c5cfcb1cSMikolaj Golub * than the chunk and is aligned at the end of the page, assuming the 1396c5cfcb1cSMikolaj Golub * next page is not mapped. So if EFAULT is returned do a fallback to 1397c5cfcb1cSMikolaj Golub * one byte read loop. 1398c5cfcb1cSMikolaj Golub */ 1399c5cfcb1cSMikolaj Golub if (error == EFAULT) { 1400c5cfcb1cSMikolaj Golub for (i = 0; i < len; i++, buf++, sptr++) { 1401c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1); 1402c5cfcb1cSMikolaj Golub if (error != 0) 1403c5cfcb1cSMikolaj Golub return (error); 1404c5cfcb1cSMikolaj Golub if (*buf == '\0') 1405c5cfcb1cSMikolaj Golub break; 1406c5cfcb1cSMikolaj Golub } 1407c5cfcb1cSMikolaj Golub error = 0; 1408c5cfcb1cSMikolaj Golub } 1409c5cfcb1cSMikolaj Golub return (error); 1410c5cfcb1cSMikolaj Golub } 1411c5cfcb1cSMikolaj Golub 1412c5cfcb1cSMikolaj Golub #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */ 1413c5cfcb1cSMikolaj Golub 1414c5cfcb1cSMikolaj Golub enum proc_vector_type { 1415c5cfcb1cSMikolaj Golub PROC_ARG, 1416c5cfcb1cSMikolaj Golub PROC_ENV, 1417c5cfcb1cSMikolaj Golub PROC_AUX, 1418c5cfcb1cSMikolaj Golub }; 1419c5cfcb1cSMikolaj Golub 1420c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32 1421c5cfcb1cSMikolaj Golub static int 1422c5cfcb1cSMikolaj Golub get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp, 1423c5cfcb1cSMikolaj Golub size_t *vsizep, enum proc_vector_type type) 1424c5cfcb1cSMikolaj Golub { 1425c5cfcb1cSMikolaj Golub struct freebsd32_ps_strings pss; 1426c5cfcb1cSMikolaj Golub Elf32_Auxinfo aux; 1427c5cfcb1cSMikolaj Golub vm_offset_t vptr, ptr; 1428c5cfcb1cSMikolaj Golub uint32_t *proc_vector32; 1429c5cfcb1cSMikolaj Golub char **proc_vector; 1430c5cfcb1cSMikolaj Golub size_t vsize, size; 1431c5cfcb1cSMikolaj Golub int i, error; 1432c5cfcb1cSMikolaj Golub 1433c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1434c5cfcb1cSMikolaj Golub &pss, sizeof(pss)); 1435c5cfcb1cSMikolaj Golub if (error != 0) 1436c5cfcb1cSMikolaj Golub return (error); 1437c5cfcb1cSMikolaj Golub switch (type) { 1438c5cfcb1cSMikolaj Golub case PROC_ARG: 1439c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_argvstr); 1440c5cfcb1cSMikolaj Golub vsize = pss.ps_nargvstr; 1441c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1442c5cfcb1cSMikolaj Golub return (ENOEXEC); 1443c5cfcb1cSMikolaj Golub size = vsize * sizeof(int32_t); 1444c5cfcb1cSMikolaj Golub break; 1445c5cfcb1cSMikolaj Golub case PROC_ENV: 1446c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_envstr); 1447c5cfcb1cSMikolaj Golub vsize = pss.ps_nenvstr; 1448c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1449c5cfcb1cSMikolaj Golub return (ENOEXEC); 1450c5cfcb1cSMikolaj Golub size = vsize * sizeof(int32_t); 1451c5cfcb1cSMikolaj Golub break; 1452c5cfcb1cSMikolaj Golub case PROC_AUX: 1453c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)PTRIN(pss.ps_envstr) + 1454c5cfcb1cSMikolaj Golub (pss.ps_nenvstr + 1) * sizeof(int32_t); 1455c5cfcb1cSMikolaj Golub if (vptr % 4 != 0) 1456c5cfcb1cSMikolaj Golub return (ENOEXEC); 1457c5cfcb1cSMikolaj Golub for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1458c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1459c5cfcb1cSMikolaj Golub if (error != 0) 1460c5cfcb1cSMikolaj Golub return (error); 1461c5cfcb1cSMikolaj Golub if (aux.a_type == AT_NULL) 1462c5cfcb1cSMikolaj Golub break; 1463c5cfcb1cSMikolaj Golub ptr += sizeof(aux); 1464c5cfcb1cSMikolaj Golub } 1465c5cfcb1cSMikolaj Golub if (aux.a_type != AT_NULL) 1466c5cfcb1cSMikolaj Golub return (ENOEXEC); 1467c5cfcb1cSMikolaj Golub vsize = i + 1; 1468c5cfcb1cSMikolaj Golub size = vsize * sizeof(aux); 1469c5cfcb1cSMikolaj Golub break; 1470c5cfcb1cSMikolaj Golub default: 1471c5cfcb1cSMikolaj Golub KASSERT(0, ("Wrong proc vector type: %d", type)); 14727ad9baaeSMikolaj Golub return (EINVAL); 1473c5cfcb1cSMikolaj Golub } 1474c5cfcb1cSMikolaj Golub proc_vector32 = malloc(size, M_TEMP, M_WAITOK); 1475c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, vptr, proc_vector32, size); 1476c5cfcb1cSMikolaj Golub if (error != 0) 1477c5cfcb1cSMikolaj Golub goto done; 1478c5cfcb1cSMikolaj Golub if (type == PROC_AUX) { 1479c5cfcb1cSMikolaj Golub *proc_vectorp = (char **)proc_vector32; 1480c5cfcb1cSMikolaj Golub *vsizep = vsize; 1481c5cfcb1cSMikolaj Golub return (0); 1482c5cfcb1cSMikolaj Golub } 1483c5cfcb1cSMikolaj Golub proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK); 1484c5cfcb1cSMikolaj Golub for (i = 0; i < (int)vsize; i++) 1485c5cfcb1cSMikolaj Golub proc_vector[i] = PTRIN(proc_vector32[i]); 1486c5cfcb1cSMikolaj Golub *proc_vectorp = proc_vector; 1487c5cfcb1cSMikolaj Golub *vsizep = vsize; 1488c5cfcb1cSMikolaj Golub done: 1489c5cfcb1cSMikolaj Golub free(proc_vector32, M_TEMP); 1490c5cfcb1cSMikolaj Golub return (error); 1491c5cfcb1cSMikolaj Golub } 1492c5cfcb1cSMikolaj Golub #endif 1493c5cfcb1cSMikolaj Golub 1494c5cfcb1cSMikolaj Golub static int 1495c5cfcb1cSMikolaj Golub get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp, 1496c5cfcb1cSMikolaj Golub size_t *vsizep, enum proc_vector_type type) 1497c5cfcb1cSMikolaj Golub { 1498c5cfcb1cSMikolaj Golub struct ps_strings pss; 1499c5cfcb1cSMikolaj Golub Elf_Auxinfo aux; 1500c5cfcb1cSMikolaj Golub vm_offset_t vptr, ptr; 1501c5cfcb1cSMikolaj Golub char **proc_vector; 1502c5cfcb1cSMikolaj Golub size_t vsize, size; 1503c5cfcb1cSMikolaj Golub int error, i; 1504c5cfcb1cSMikolaj Golub 1505c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32 1506c5cfcb1cSMikolaj Golub if (SV_PROC_FLAG(p, SV_ILP32) != 0) 1507c5cfcb1cSMikolaj Golub return (get_proc_vector32(td, p, proc_vectorp, vsizep, type)); 1508c5cfcb1cSMikolaj Golub #endif 1509c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings), 1510c5cfcb1cSMikolaj Golub &pss, sizeof(pss)); 1511c5cfcb1cSMikolaj Golub if (error != 0) 1512c5cfcb1cSMikolaj Golub return (error); 1513c5cfcb1cSMikolaj Golub switch (type) { 1514c5cfcb1cSMikolaj Golub case PROC_ARG: 1515c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_argvstr; 1516c5cfcb1cSMikolaj Golub vsize = pss.ps_nargvstr; 1517c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1518c5cfcb1cSMikolaj Golub return (ENOEXEC); 1519c5cfcb1cSMikolaj Golub size = vsize * sizeof(char *); 1520c5cfcb1cSMikolaj Golub break; 1521c5cfcb1cSMikolaj Golub case PROC_ENV: 1522c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_envstr; 1523c5cfcb1cSMikolaj Golub vsize = pss.ps_nenvstr; 1524c5cfcb1cSMikolaj Golub if (vsize > ARG_MAX) 1525c5cfcb1cSMikolaj Golub return (ENOEXEC); 1526c5cfcb1cSMikolaj Golub size = vsize * sizeof(char *); 1527c5cfcb1cSMikolaj Golub break; 1528c5cfcb1cSMikolaj Golub case PROC_AUX: 1529c5cfcb1cSMikolaj Golub /* 1530c5cfcb1cSMikolaj Golub * The aux array is just above env array on the stack. Check 1531c5cfcb1cSMikolaj Golub * that the address is naturally aligned. 1532c5cfcb1cSMikolaj Golub */ 1533c5cfcb1cSMikolaj Golub vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1) 1534c5cfcb1cSMikolaj Golub * sizeof(char *); 1535c5cfcb1cSMikolaj Golub #if __ELF_WORD_SIZE == 64 1536c5cfcb1cSMikolaj Golub if (vptr % sizeof(uint64_t) != 0) 1537c5cfcb1cSMikolaj Golub #else 1538c5cfcb1cSMikolaj Golub if (vptr % sizeof(uint32_t) != 0) 1539c5cfcb1cSMikolaj Golub #endif 1540c5cfcb1cSMikolaj Golub return (ENOEXEC); 1541c5cfcb1cSMikolaj Golub /* 1542c5cfcb1cSMikolaj Golub * We count the array size reading the aux vectors from the 1543c5cfcb1cSMikolaj Golub * stack until AT_NULL vector is returned. So (to keep the code 1544c5cfcb1cSMikolaj Golub * simple) we read the process stack twice: the first time here 1545c5cfcb1cSMikolaj Golub * to find the size and the second time when copying the vectors 1546c5cfcb1cSMikolaj Golub * to the allocated proc_vector. 1547c5cfcb1cSMikolaj Golub */ 1548c5cfcb1cSMikolaj Golub for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) { 1549c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, ptr, &aux, sizeof(aux)); 1550c5cfcb1cSMikolaj Golub if (error != 0) 1551c5cfcb1cSMikolaj Golub return (error); 1552c5cfcb1cSMikolaj Golub if (aux.a_type == AT_NULL) 1553c5cfcb1cSMikolaj Golub break; 1554c5cfcb1cSMikolaj Golub ptr += sizeof(aux); 1555c5cfcb1cSMikolaj Golub } 1556c5cfcb1cSMikolaj Golub /* 1557c5cfcb1cSMikolaj Golub * If the PROC_AUXV_MAX entries are iterated over, and we have 1558c5cfcb1cSMikolaj Golub * not reached AT_NULL, it is most likely we are reading wrong 1559c5cfcb1cSMikolaj Golub * data: either the process doesn't have auxv array or data has 1560c5cfcb1cSMikolaj Golub * been modified. Return the error in this case. 1561c5cfcb1cSMikolaj Golub */ 1562c5cfcb1cSMikolaj Golub if (aux.a_type != AT_NULL) 1563c5cfcb1cSMikolaj Golub return (ENOEXEC); 1564c5cfcb1cSMikolaj Golub vsize = i + 1; 1565c5cfcb1cSMikolaj Golub size = vsize * sizeof(aux); 1566c5cfcb1cSMikolaj Golub break; 1567c5cfcb1cSMikolaj Golub default: 1568c5cfcb1cSMikolaj Golub KASSERT(0, ("Wrong proc vector type: %d", type)); 15697ad9baaeSMikolaj Golub return (EINVAL); /* In case we are built without INVARIANTS. */ 1570c5cfcb1cSMikolaj Golub } 1571c5cfcb1cSMikolaj Golub proc_vector = malloc(size, M_TEMP, M_WAITOK); 1572c5cfcb1cSMikolaj Golub if (proc_vector == NULL) 1573c5cfcb1cSMikolaj Golub return (ENOMEM); 1574c5cfcb1cSMikolaj Golub error = proc_read_mem(td, p, vptr, proc_vector, size); 1575c5cfcb1cSMikolaj Golub if (error != 0) { 1576c5cfcb1cSMikolaj Golub free(proc_vector, M_TEMP); 1577c5cfcb1cSMikolaj Golub return (error); 1578c5cfcb1cSMikolaj Golub } 1579c5cfcb1cSMikolaj Golub *proc_vectorp = proc_vector; 1580c5cfcb1cSMikolaj Golub *vsizep = vsize; 1581c5cfcb1cSMikolaj Golub 1582c5cfcb1cSMikolaj Golub return (0); 1583c5cfcb1cSMikolaj Golub } 1584c5cfcb1cSMikolaj Golub 1585c5cfcb1cSMikolaj Golub #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */ 1586c5cfcb1cSMikolaj Golub 1587c5cfcb1cSMikolaj Golub static int 1588c5cfcb1cSMikolaj Golub get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb, 1589c5cfcb1cSMikolaj Golub enum proc_vector_type type, size_t nchr) 1590c5cfcb1cSMikolaj Golub { 1591c5cfcb1cSMikolaj Golub size_t done, len, vsize; 1592c5cfcb1cSMikolaj Golub int error, i; 1593c5cfcb1cSMikolaj Golub char **proc_vector, *sptr; 1594c5cfcb1cSMikolaj Golub char pss_string[GET_PS_STRINGS_CHUNK_SZ]; 1595c5cfcb1cSMikolaj Golub 1596c5cfcb1cSMikolaj Golub PROC_ASSERT_HELD(p); 1597c5cfcb1cSMikolaj Golub 1598c5cfcb1cSMikolaj Golub /* 1599c5cfcb1cSMikolaj Golub * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes. 1600c5cfcb1cSMikolaj Golub */ 1601c5cfcb1cSMikolaj Golub if (nchr > 2 * (PATH_MAX + ARG_MAX)) 1602c5cfcb1cSMikolaj Golub nchr = 2 * (PATH_MAX + ARG_MAX); 1603c5cfcb1cSMikolaj Golub 1604c5cfcb1cSMikolaj Golub error = get_proc_vector(td, p, &proc_vector, &vsize, type); 1605c5cfcb1cSMikolaj Golub if (error != 0) 1606c5cfcb1cSMikolaj Golub return (error); 1607c5cfcb1cSMikolaj Golub for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) { 1608c5cfcb1cSMikolaj Golub /* 1609c5cfcb1cSMikolaj Golub * The program may have scribbled into its argv array, e.g. to 1610c5cfcb1cSMikolaj Golub * remove some arguments. If that has happened, break out 1611c5cfcb1cSMikolaj Golub * before trying to read from NULL. 1612c5cfcb1cSMikolaj Golub */ 1613c5cfcb1cSMikolaj Golub if (proc_vector[i] == NULL) 1614c5cfcb1cSMikolaj Golub break; 1615c5cfcb1cSMikolaj Golub for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) { 1616c5cfcb1cSMikolaj Golub error = proc_read_string(td, p, sptr, pss_string, 1617c5cfcb1cSMikolaj Golub sizeof(pss_string)); 1618c5cfcb1cSMikolaj Golub if (error != 0) 1619c5cfcb1cSMikolaj Golub goto done; 1620c5cfcb1cSMikolaj Golub len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ); 1621c5cfcb1cSMikolaj Golub if (done + len >= nchr) 1622c5cfcb1cSMikolaj Golub len = nchr - done - 1; 1623c5cfcb1cSMikolaj Golub sbuf_bcat(sb, pss_string, len); 1624c5cfcb1cSMikolaj Golub if (len != GET_PS_STRINGS_CHUNK_SZ) 1625c5cfcb1cSMikolaj Golub break; 1626c5cfcb1cSMikolaj Golub done += GET_PS_STRINGS_CHUNK_SZ; 1627c5cfcb1cSMikolaj Golub } 1628c5cfcb1cSMikolaj Golub sbuf_bcat(sb, "", 1); 1629c5cfcb1cSMikolaj Golub done += len + 1; 1630c5cfcb1cSMikolaj Golub } 1631c5cfcb1cSMikolaj Golub done: 1632c5cfcb1cSMikolaj Golub free(proc_vector, M_TEMP); 1633c5cfcb1cSMikolaj Golub return (error); 1634c5cfcb1cSMikolaj Golub } 1635c5cfcb1cSMikolaj Golub 1636c5cfcb1cSMikolaj Golub int 1637c5cfcb1cSMikolaj Golub proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb, size_t nchr) 1638c5cfcb1cSMikolaj Golub { 1639c5cfcb1cSMikolaj Golub 1640c5cfcb1cSMikolaj Golub return (get_ps_strings(curthread, p, sb, PROC_ARG, nchr)); 1641c5cfcb1cSMikolaj Golub } 1642c5cfcb1cSMikolaj Golub 1643c5cfcb1cSMikolaj Golub int 1644c5cfcb1cSMikolaj Golub proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb, size_t nchr) 1645c5cfcb1cSMikolaj Golub { 1646c5cfcb1cSMikolaj Golub 1647c5cfcb1cSMikolaj Golub return (get_ps_strings(curthread, p, sb, PROC_ENV, nchr)); 1648c5cfcb1cSMikolaj Golub } 1649c5cfcb1cSMikolaj Golub 1650b9df5231SPoul-Henning Kamp /* 1651b9df5231SPoul-Henning Kamp * This sysctl allows a process to retrieve the argument list or process 1652b9df5231SPoul-Henning Kamp * title for another process without groping around in the address space 1653b9df5231SPoul-Henning Kamp * of the other process. It also allow a process to set its own "process 1654b9df5231SPoul-Henning Kamp * title to a string of its own choice. 1655b9df5231SPoul-Henning Kamp */ 1656b9df5231SPoul-Henning Kamp static int 165782d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) 1658b9df5231SPoul-Henning Kamp { 1659b9df5231SPoul-Henning Kamp int *name = (int*) arg1; 1660b9df5231SPoul-Henning Kamp u_int namelen = arg2; 16618510f2a8SJohn Baldwin struct pargs *newpa, *pa; 1662b9df5231SPoul-Henning Kamp struct proc *p; 1663c5cfcb1cSMikolaj Golub struct sbuf sb; 1664c5cfcb1cSMikolaj Golub int error = 0, error2; 1665b9df5231SPoul-Henning Kamp 1666b9df5231SPoul-Henning Kamp if (namelen != 1) 1667b9df5231SPoul-Henning Kamp return (EINVAL); 1668b9df5231SPoul-Henning Kamp 1669b9df5231SPoul-Henning Kamp p = pfind((pid_t)name[0]); 1670b9df5231SPoul-Henning Kamp if (!p) 1671e76bad96SRobert Drehmel return (ESRCH); 1672b9df5231SPoul-Henning Kamp 16739cdb6216SPawel Jakub Dawidek if ((error = p_cansee(curthread, p)) != 0) { 167433a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1675e76bad96SRobert Drehmel return (error); 167633a9ed9dSJohn Baldwin } 16774bc6471bSJohn Baldwin 16784bc6471bSJohn Baldwin if (req->newptr && curproc != p) { 167933a9ed9dSJohn Baldwin PROC_UNLOCK(p); 1680b9df5231SPoul-Henning Kamp return (EPERM); 16814bc6471bSJohn Baldwin } 1682b9df5231SPoul-Henning Kamp 16837b11fea6SAlfred Perlstein pa = p->p_args; 1684c5cfcb1cSMikolaj Golub if (pa != NULL) { 16857b11fea6SAlfred Perlstein pargs_hold(pa); 16867b11fea6SAlfred Perlstein PROC_UNLOCK(p); 16877b11fea6SAlfred Perlstein error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length); 16887b11fea6SAlfred Perlstein pargs_drop(pa); 1689c5cfcb1cSMikolaj Golub } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) { 1690c5cfcb1cSMikolaj Golub _PHOLD(p); 1691c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1692c5cfcb1cSMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1693c5cfcb1cSMikolaj Golub error = proc_getargv(curthread, p, &sb, req->oldlen); 1694c5cfcb1cSMikolaj Golub error2 = sbuf_finish(&sb); 1695c5cfcb1cSMikolaj Golub PRELE(p); 1696c5cfcb1cSMikolaj Golub sbuf_delete(&sb); 1697c5cfcb1cSMikolaj Golub if (error == 0 && error2 != 0) 1698c5cfcb1cSMikolaj Golub error = error2; 1699c5cfcb1cSMikolaj Golub } else { 1700c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1701c5cfcb1cSMikolaj Golub } 17028510f2a8SJohn Baldwin if (error != 0 || req->newptr == NULL) 1703b9df5231SPoul-Henning Kamp return (error); 1704b9df5231SPoul-Henning Kamp 1705b9df5231SPoul-Henning Kamp if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) 17068510f2a8SJohn Baldwin return (ENOMEM); 17074bc6471bSJohn Baldwin newpa = pargs_alloc(req->newlen); 17084bc6471bSJohn Baldwin error = SYSCTL_IN(req, newpa->ar_args, req->newlen); 17098510f2a8SJohn Baldwin if (error != 0) { 17104bc6471bSJohn Baldwin pargs_free(newpa); 1711b9df5231SPoul-Henning Kamp return (error); 1712b9df5231SPoul-Henning Kamp } 17134bc6471bSJohn Baldwin PROC_LOCK(p); 17144bc6471bSJohn Baldwin pa = p->p_args; 17154bc6471bSJohn Baldwin p->p_args = newpa; 17164bc6471bSJohn Baldwin PROC_UNLOCK(p); 17174bc6471bSJohn Baldwin pargs_drop(pa); 17184bc6471bSJohn Baldwin return (0); 17194bc6471bSJohn Baldwin } 17204bc6471bSJohn Baldwin 1721fe769cddSDavid Schultz /* 1722c5cfcb1cSMikolaj Golub * This sysctl allows a process to retrieve environment of another process. 1723c5cfcb1cSMikolaj Golub */ 1724c5cfcb1cSMikolaj Golub static int 1725c5cfcb1cSMikolaj Golub sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS) 1726c5cfcb1cSMikolaj Golub { 1727c5cfcb1cSMikolaj Golub int *name = (int*) arg1; 1728c5cfcb1cSMikolaj Golub u_int namelen = arg2; 1729c5cfcb1cSMikolaj Golub struct proc *p; 1730c5cfcb1cSMikolaj Golub struct sbuf sb; 1731c5cfcb1cSMikolaj Golub int error, error2; 1732c5cfcb1cSMikolaj Golub 1733c5cfcb1cSMikolaj Golub if (namelen != 1) 1734c5cfcb1cSMikolaj Golub return (EINVAL); 1735c5cfcb1cSMikolaj Golub 1736c5cfcb1cSMikolaj Golub p = pfind((pid_t)name[0]); 1737c5cfcb1cSMikolaj Golub if (p == NULL) 1738c5cfcb1cSMikolaj Golub return (ESRCH); 1739c5cfcb1cSMikolaj Golub if ((p->p_flag & P_WEXIT) != 0) { 1740c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1741c5cfcb1cSMikolaj Golub return (ESRCH); 1742c5cfcb1cSMikolaj Golub } 1743c5cfcb1cSMikolaj Golub if ((error = p_candebug(curthread, p)) != 0) { 1744c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1745c5cfcb1cSMikolaj Golub return (error); 1746c5cfcb1cSMikolaj Golub } 1747c5cfcb1cSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) { 1748c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1749c5cfcb1cSMikolaj Golub return (0); 1750c5cfcb1cSMikolaj Golub } 1751c5cfcb1cSMikolaj Golub _PHOLD(p); 1752c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1753c5cfcb1cSMikolaj Golub sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req); 1754c5cfcb1cSMikolaj Golub error = proc_getenvv(curthread, p, &sb, req->oldlen); 1755c5cfcb1cSMikolaj Golub error2 = sbuf_finish(&sb); 1756c5cfcb1cSMikolaj Golub PRELE(p); 1757c5cfcb1cSMikolaj Golub sbuf_delete(&sb); 1758c5cfcb1cSMikolaj Golub return (error != 0 ? error : error2); 1759c5cfcb1cSMikolaj Golub } 1760c5cfcb1cSMikolaj Golub 1761c5cfcb1cSMikolaj Golub /* 1762c5cfcb1cSMikolaj Golub * This sysctl allows a process to retrieve ELF auxiliary vector of 1763c5cfcb1cSMikolaj Golub * another process. 1764c5cfcb1cSMikolaj Golub */ 1765c5cfcb1cSMikolaj Golub static int 1766c5cfcb1cSMikolaj Golub sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS) 1767c5cfcb1cSMikolaj Golub { 1768c5cfcb1cSMikolaj Golub int *name = (int*) arg1; 1769c5cfcb1cSMikolaj Golub u_int namelen = arg2; 1770c5cfcb1cSMikolaj Golub struct proc *p; 17714fd6053bSMikolaj Golub size_t vsize, size; 1772c5cfcb1cSMikolaj Golub char **auxv; 1773c5cfcb1cSMikolaj Golub int error; 1774c5cfcb1cSMikolaj Golub 1775c5cfcb1cSMikolaj Golub if (namelen != 1) 1776c5cfcb1cSMikolaj Golub return (EINVAL); 1777c5cfcb1cSMikolaj Golub 1778c5cfcb1cSMikolaj Golub p = pfind((pid_t)name[0]); 1779c5cfcb1cSMikolaj Golub if (p == NULL) 1780c5cfcb1cSMikolaj Golub return (ESRCH); 1781c5cfcb1cSMikolaj Golub if (p->p_flag & P_WEXIT) { 1782c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1783c5cfcb1cSMikolaj Golub return (ESRCH); 1784c5cfcb1cSMikolaj Golub } 1785c5cfcb1cSMikolaj Golub if ((error = p_cansee(curthread, p)) != 0) { 1786c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1787c5cfcb1cSMikolaj Golub return (error); 1788c5cfcb1cSMikolaj Golub } 1789c5cfcb1cSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) { 1790c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1791c5cfcb1cSMikolaj Golub return (0); 1792c5cfcb1cSMikolaj Golub } 1793c5cfcb1cSMikolaj Golub _PHOLD(p); 1794c5cfcb1cSMikolaj Golub PROC_UNLOCK(p); 1795c5cfcb1cSMikolaj Golub error = get_proc_vector(curthread, p, &auxv, &vsize, PROC_AUX); 1796c5cfcb1cSMikolaj Golub if (error == 0) { 1797c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32 1798c5cfcb1cSMikolaj Golub if (SV_PROC_FLAG(p, SV_ILP32) != 0) 17994fd6053bSMikolaj Golub size = vsize * sizeof(Elf32_Auxinfo); 1800c5cfcb1cSMikolaj Golub else 1801c5cfcb1cSMikolaj Golub #endif 18024fd6053bSMikolaj Golub size = vsize * sizeof(Elf_Auxinfo); 18034fd6053bSMikolaj Golub PRELE(p); 18044fd6053bSMikolaj Golub error = SYSCTL_OUT(req, auxv, size); 1805c5cfcb1cSMikolaj Golub free(auxv, M_TEMP); 18064fd6053bSMikolaj Golub } else { 18074fd6053bSMikolaj Golub PRELE(p); 1808c5cfcb1cSMikolaj Golub } 1809c5cfcb1cSMikolaj Golub return (error); 1810c5cfcb1cSMikolaj Golub } 1811c5cfcb1cSMikolaj Golub 1812c5cfcb1cSMikolaj Golub /* 1813fe769cddSDavid Schultz * This sysctl allows a process to retrieve the path of the executable for 1814fe769cddSDavid Schultz * itself or another process. 1815fe769cddSDavid Schultz */ 1816fe769cddSDavid Schultz static int 1817fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) 1818fe769cddSDavid Schultz { 1819fe769cddSDavid Schultz pid_t *pidp = (pid_t *)arg1; 1820fe769cddSDavid Schultz unsigned int arglen = arg2; 1821fe769cddSDavid Schultz struct proc *p; 1822fe769cddSDavid Schultz struct vnode *vp; 1823fe769cddSDavid Schultz char *retbuf, *freebuf; 182424f87fdbSJohn Baldwin int error, vfslocked; 1825fe769cddSDavid Schultz 1826fe769cddSDavid Schultz if (arglen != 1) 1827fe769cddSDavid Schultz return (EINVAL); 1828fe769cddSDavid Schultz if (*pidp == -1) { /* -1 means this process */ 1829fe769cddSDavid Schultz p = req->td->td_proc; 1830fe769cddSDavid Schultz } else { 1831fe769cddSDavid Schultz p = pfind(*pidp); 1832fe769cddSDavid Schultz if (p == NULL) 1833fe769cddSDavid Schultz return (ESRCH); 1834fe769cddSDavid Schultz if ((error = p_cansee(curthread, p)) != 0) { 1835fe769cddSDavid Schultz PROC_UNLOCK(p); 1836fe769cddSDavid Schultz return (error); 1837fe769cddSDavid Schultz } 1838fe769cddSDavid Schultz } 1839fe769cddSDavid Schultz 1840fe769cddSDavid Schultz vp = p->p_textvp; 1841965b55e2SRobert Watson if (vp == NULL) { 1842965b55e2SRobert Watson if (*pidp != -1) 1843965b55e2SRobert Watson PROC_UNLOCK(p); 1844965b55e2SRobert Watson return (0); 1845965b55e2SRobert Watson } 1846fe769cddSDavid Schultz vref(vp); 1847fe769cddSDavid Schultz if (*pidp != -1) 1848fe769cddSDavid Schultz PROC_UNLOCK(p); 1849fe769cddSDavid Schultz error = vn_fullpath(req->td, vp, &retbuf, &freebuf); 185024f87fdbSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 1851fe769cddSDavid Schultz vrele(vp); 185224f87fdbSJohn Baldwin VFS_UNLOCK_GIANT(vfslocked); 1853fe769cddSDavid Schultz if (error) 1854fe769cddSDavid Schultz return (error); 1855fe769cddSDavid Schultz error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); 1856fe769cddSDavid Schultz free(freebuf, M_TEMP); 1857fe769cddSDavid Schultz return (error); 1858fe769cddSDavid Schultz } 1859fe769cddSDavid Schultz 1860baf731e6SRobert Drehmel static int 1861baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS) 1862baf731e6SRobert Drehmel { 1863baf731e6SRobert Drehmel struct proc *p; 1864baf731e6SRobert Drehmel char *sv_name; 1865baf731e6SRobert Drehmel int *name; 1866baf731e6SRobert Drehmel int namelen; 1867e76bad96SRobert Drehmel int error; 1868baf731e6SRobert Drehmel 1869baf731e6SRobert Drehmel namelen = arg2; 1870baf731e6SRobert Drehmel if (namelen != 1) 1871baf731e6SRobert Drehmel return (EINVAL); 1872baf731e6SRobert Drehmel 1873baf731e6SRobert Drehmel name = (int *)arg1; 1874baf731e6SRobert Drehmel if ((p = pfind((pid_t)name[0])) == NULL) 1875e76bad96SRobert Drehmel return (ESRCH); 1876e76bad96SRobert Drehmel if ((error = p_cansee(curthread, p))) { 1877baf731e6SRobert Drehmel PROC_UNLOCK(p); 1878e76bad96SRobert Drehmel return (error); 1879baf731e6SRobert Drehmel } 1880baf731e6SRobert Drehmel sv_name = p->p_sysent->sv_name; 1881baf731e6SRobert Drehmel PROC_UNLOCK(p); 1882baf731e6SRobert Drehmel return (sysctl_handle_string(oidp, sv_name, 0, req)); 1883baf731e6SRobert Drehmel } 1884baf731e6SRobert Drehmel 188543151ee6SPeter Wemm #ifdef KINFO_OVMENTRY_SIZE 188643151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE); 188743151ee6SPeter Wemm #endif 188843151ee6SPeter Wemm 188943151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7 1890cc43c38cSRobert Watson static int 189143151ee6SPeter Wemm sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS) 1892cc43c38cSRobert Watson { 1893cc43c38cSRobert Watson vm_map_entry_t entry, tmp_entry; 1894cc43c38cSRobert Watson unsigned int last_timestamp; 1895cc43c38cSRobert Watson char *fullpath, *freepath; 189643151ee6SPeter Wemm struct kinfo_ovmentry *kve; 18977a9c4d24SPeter Wemm struct vattr va; 18987a9c4d24SPeter Wemm struct ucred *cred; 1899cc43c38cSRobert Watson int error, *name; 1900cc43c38cSRobert Watson struct vnode *vp; 1901cc43c38cSRobert Watson struct proc *p; 1902cc43c38cSRobert Watson vm_map_t map; 1903c7462f43SKonstantin Belousov struct vmspace *vm; 1904baf731e6SRobert Drehmel 1905cc43c38cSRobert Watson name = (int *)arg1; 1906cc43c38cSRobert Watson if ((p = pfind((pid_t)name[0])) == NULL) 1907cc43c38cSRobert Watson return (ESRCH); 190863d79c4fSRobert Watson if (p->p_flag & P_WEXIT) { 190963d79c4fSRobert Watson PROC_UNLOCK(p); 191063d79c4fSRobert Watson return (ESRCH); 191163d79c4fSRobert Watson } 1912cc43c38cSRobert Watson if ((error = p_candebug(curthread, p))) { 1913cc43c38cSRobert Watson PROC_UNLOCK(p); 1914cc43c38cSRobert Watson return (error); 1915cc43c38cSRobert Watson } 1916cc43c38cSRobert Watson _PHOLD(p); 1917cc43c38cSRobert Watson PROC_UNLOCK(p); 1918c7462f43SKonstantin Belousov vm = vmspace_acquire_ref(p); 1919c7462f43SKonstantin Belousov if (vm == NULL) { 1920c7462f43SKonstantin Belousov PRELE(p); 1921c7462f43SKonstantin Belousov return (ESRCH); 1922c7462f43SKonstantin Belousov } 1923cc43c38cSRobert Watson kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 1924cc43c38cSRobert Watson 1925ca4aa8c3SSergey Kandaurov map = &vm->vm_map; 1926cc43c38cSRobert Watson vm_map_lock_read(map); 1927cc43c38cSRobert Watson for (entry = map->header.next; entry != &map->header; 1928cc43c38cSRobert Watson entry = entry->next) { 1929cc43c38cSRobert Watson vm_object_t obj, tobj, lobj; 1930cc43c38cSRobert Watson vm_offset_t addr; 1931cc43c38cSRobert Watson int vfslocked; 1932cc43c38cSRobert Watson 1933cc43c38cSRobert Watson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 1934cc43c38cSRobert Watson continue; 1935cc43c38cSRobert Watson 1936cc43c38cSRobert Watson bzero(kve, sizeof(*kve)); 1937cc43c38cSRobert Watson kve->kve_structsize = sizeof(*kve); 1938cc43c38cSRobert Watson 1939cc43c38cSRobert Watson kve->kve_private_resident = 0; 1940cc43c38cSRobert Watson obj = entry->object.vm_object; 1941cc43c38cSRobert Watson if (obj != NULL) { 1942cc43c38cSRobert Watson VM_OBJECT_LOCK(obj); 1943cc43c38cSRobert Watson if (obj->shadow_count == 1) 1944cc43c38cSRobert Watson kve->kve_private_resident = 1945cc43c38cSRobert Watson obj->resident_page_count; 1946cc43c38cSRobert Watson } 1947cc43c38cSRobert Watson kve->kve_resident = 0; 1948cc43c38cSRobert Watson addr = entry->start; 1949cc43c38cSRobert Watson while (addr < entry->end) { 1950cc43c38cSRobert Watson if (pmap_extract(map->pmap, addr)) 1951cc43c38cSRobert Watson kve->kve_resident++; 1952cc43c38cSRobert Watson addr += PAGE_SIZE; 1953cc43c38cSRobert Watson } 1954cc43c38cSRobert Watson 1955cc43c38cSRobert Watson for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 1956cc43c38cSRobert Watson if (tobj != obj) 1957cc43c38cSRobert Watson VM_OBJECT_LOCK(tobj); 1958cc43c38cSRobert Watson if (lobj != obj) 1959cc43c38cSRobert Watson VM_OBJECT_UNLOCK(lobj); 1960cc43c38cSRobert Watson lobj = tobj; 1961cc43c38cSRobert Watson } 1962cc43c38cSRobert Watson 1963118d0afaSKonstantin Belousov kve->kve_start = (void*)entry->start; 1964118d0afaSKonstantin Belousov kve->kve_end = (void*)entry->end; 1965118d0afaSKonstantin Belousov kve->kve_offset = (off_t)entry->offset; 1966118d0afaSKonstantin Belousov 1967118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_READ) 1968118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_READ; 1969118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_WRITE) 1970118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_WRITE; 1971118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_EXECUTE) 1972118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_EXEC; 1973118d0afaSKonstantin Belousov 1974118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_COW) 1975118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_COW; 1976118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 1977118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 1978937912eaSAttilio Rao if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 1979937912eaSAttilio Rao kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 1980118d0afaSKonstantin Belousov 1981118d0afaSKonstantin Belousov last_timestamp = map->timestamp; 1982118d0afaSKonstantin Belousov vm_map_unlock_read(map); 1983118d0afaSKonstantin Belousov 19847a9c4d24SPeter Wemm kve->kve_fileid = 0; 19857a9c4d24SPeter Wemm kve->kve_fsid = 0; 1986cc43c38cSRobert Watson freepath = NULL; 1987cc43c38cSRobert Watson fullpath = ""; 1988cc43c38cSRobert Watson if (lobj) { 1989cc43c38cSRobert Watson vp = NULL; 1990cc43c38cSRobert Watson switch (lobj->type) { 1991cc43c38cSRobert Watson case OBJT_DEFAULT: 1992cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_DEFAULT; 1993cc43c38cSRobert Watson break; 1994cc43c38cSRobert Watson case OBJT_VNODE: 1995cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_VNODE; 1996cc43c38cSRobert Watson vp = lobj->handle; 1997cc43c38cSRobert Watson vref(vp); 1998cc43c38cSRobert Watson break; 1999cc43c38cSRobert Watson case OBJT_SWAP: 2000cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_SWAP; 2001cc43c38cSRobert Watson break; 2002cc43c38cSRobert Watson case OBJT_DEVICE: 2003cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_DEVICE; 2004cc43c38cSRobert Watson break; 2005cc43c38cSRobert Watson case OBJT_PHYS: 2006cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_PHYS; 2007cc43c38cSRobert Watson break; 2008cc43c38cSRobert Watson case OBJT_DEAD: 2009cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_DEAD; 2010cc43c38cSRobert Watson break; 201101381811SJohn Baldwin case OBJT_SG: 201201381811SJohn Baldwin kve->kve_type = KVME_TYPE_SG; 201301381811SJohn Baldwin break; 2014cc43c38cSRobert Watson default: 2015cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_UNKNOWN; 2016cc43c38cSRobert Watson break; 2017cc43c38cSRobert Watson } 2018cc43c38cSRobert Watson if (lobj != obj) 2019cc43c38cSRobert Watson VM_OBJECT_UNLOCK(lobj); 2020cc43c38cSRobert Watson 2021cc43c38cSRobert Watson kve->kve_ref_count = obj->ref_count; 2022cc43c38cSRobert Watson kve->kve_shadow_count = obj->shadow_count; 2023cc43c38cSRobert Watson VM_OBJECT_UNLOCK(obj); 2024cc43c38cSRobert Watson if (vp != NULL) { 2025cc43c38cSRobert Watson vn_fullpath(curthread, vp, &fullpath, 2026cc43c38cSRobert Watson &freepath); 20277a9c4d24SPeter Wemm cred = curthread->td_ucred; 20282ff47c5fSJohn Baldwin vfslocked = VFS_LOCK_GIANT(vp->v_mount); 20292ff47c5fSJohn Baldwin vn_lock(vp, LK_SHARED | LK_RETRY); 20307a9c4d24SPeter Wemm if (VOP_GETATTR(vp, &va, cred) == 0) { 20317a9c4d24SPeter Wemm kve->kve_fileid = va.va_fileid; 20327a9c4d24SPeter Wemm kve->kve_fsid = va.va_fsid; 20337a9c4d24SPeter Wemm } 2034cc43c38cSRobert Watson vput(vp); 2035cc43c38cSRobert Watson VFS_UNLOCK_GIANT(vfslocked); 2036cc43c38cSRobert Watson } 2037cc43c38cSRobert Watson } else { 2038cc43c38cSRobert Watson kve->kve_type = KVME_TYPE_NONE; 2039cc43c38cSRobert Watson kve->kve_ref_count = 0; 2040cc43c38cSRobert Watson kve->kve_shadow_count = 0; 2041cc43c38cSRobert Watson } 2042cc43c38cSRobert Watson 2043cc43c38cSRobert Watson strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 2044cc43c38cSRobert Watson if (freepath != NULL) 2045cc43c38cSRobert Watson free(freepath, M_TEMP); 2046cc43c38cSRobert Watson 2047cc43c38cSRobert Watson error = SYSCTL_OUT(req, kve, sizeof(*kve)); 2048cc43c38cSRobert Watson vm_map_lock_read(map); 2049cc43c38cSRobert Watson if (error) 2050cc43c38cSRobert Watson break; 205122a448c4SKonstantin Belousov if (last_timestamp != map->timestamp) { 2052cc43c38cSRobert Watson vm_map_lookup_entry(map, addr - 1, &tmp_entry); 2053cc43c38cSRobert Watson entry = tmp_entry; 2054cc43c38cSRobert Watson } 2055cc43c38cSRobert Watson } 2056cc43c38cSRobert Watson vm_map_unlock_read(map); 2057c7462f43SKonstantin Belousov vmspace_free(vm); 2058cc43c38cSRobert Watson PRELE(p); 2059cc43c38cSRobert Watson free(kve, M_TEMP); 2060cc43c38cSRobert Watson return (error); 2061cc43c38cSRobert Watson } 206243151ee6SPeter Wemm #endif /* COMPAT_FREEBSD7 */ 206343151ee6SPeter Wemm 206443151ee6SPeter Wemm #ifdef KINFO_VMENTRY_SIZE 206543151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE); 206643151ee6SPeter Wemm #endif 206743151ee6SPeter Wemm 206843151ee6SPeter Wemm static int 206943151ee6SPeter Wemm sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS) 207043151ee6SPeter Wemm { 207143151ee6SPeter Wemm vm_map_entry_t entry, tmp_entry; 207243151ee6SPeter Wemm unsigned int last_timestamp; 207343151ee6SPeter Wemm char *fullpath, *freepath; 207443151ee6SPeter Wemm struct kinfo_vmentry *kve; 207543151ee6SPeter Wemm struct vattr va; 207643151ee6SPeter Wemm struct ucred *cred; 207743151ee6SPeter Wemm int error, *name; 207843151ee6SPeter Wemm struct vnode *vp; 207943151ee6SPeter Wemm struct proc *p; 2080c7462f43SKonstantin Belousov struct vmspace *vm; 208143151ee6SPeter Wemm vm_map_t map; 208243151ee6SPeter Wemm 208343151ee6SPeter Wemm name = (int *)arg1; 208443151ee6SPeter Wemm if ((p = pfind((pid_t)name[0])) == NULL) 208543151ee6SPeter Wemm return (ESRCH); 208643151ee6SPeter Wemm if (p->p_flag & P_WEXIT) { 208743151ee6SPeter Wemm PROC_UNLOCK(p); 208843151ee6SPeter Wemm return (ESRCH); 208943151ee6SPeter Wemm } 209043151ee6SPeter Wemm if ((error = p_candebug(curthread, p))) { 209143151ee6SPeter Wemm PROC_UNLOCK(p); 209243151ee6SPeter Wemm return (error); 209343151ee6SPeter Wemm } 209443151ee6SPeter Wemm _PHOLD(p); 209543151ee6SPeter Wemm PROC_UNLOCK(p); 2096c7462f43SKonstantin Belousov vm = vmspace_acquire_ref(p); 2097c7462f43SKonstantin Belousov if (vm == NULL) { 2098c7462f43SKonstantin Belousov PRELE(p); 2099c7462f43SKonstantin Belousov return (ESRCH); 2100c7462f43SKonstantin Belousov } 210143151ee6SPeter Wemm kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK); 210243151ee6SPeter Wemm 2103ca4aa8c3SSergey Kandaurov map = &vm->vm_map; 210443151ee6SPeter Wemm vm_map_lock_read(map); 210543151ee6SPeter Wemm for (entry = map->header.next; entry != &map->header; 210643151ee6SPeter Wemm entry = entry->next) { 210743151ee6SPeter Wemm vm_object_t obj, tobj, lobj; 210843151ee6SPeter Wemm vm_offset_t addr; 21095384d089SMikolaj Golub vm_paddr_t locked_pa; 21105384d089SMikolaj Golub int vfslocked, mincoreinfo; 211143151ee6SPeter Wemm 211243151ee6SPeter Wemm if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 211343151ee6SPeter Wemm continue; 211443151ee6SPeter Wemm 211543151ee6SPeter Wemm bzero(kve, sizeof(*kve)); 211643151ee6SPeter Wemm 211743151ee6SPeter Wemm kve->kve_private_resident = 0; 211843151ee6SPeter Wemm obj = entry->object.vm_object; 211943151ee6SPeter Wemm if (obj != NULL) { 212043151ee6SPeter Wemm VM_OBJECT_LOCK(obj); 212143151ee6SPeter Wemm if (obj->shadow_count == 1) 212243151ee6SPeter Wemm kve->kve_private_resident = 212343151ee6SPeter Wemm obj->resident_page_count; 212443151ee6SPeter Wemm } 212543151ee6SPeter Wemm kve->kve_resident = 0; 212643151ee6SPeter Wemm addr = entry->start; 212743151ee6SPeter Wemm while (addr < entry->end) { 21285384d089SMikolaj Golub locked_pa = 0; 21295384d089SMikolaj Golub mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa); 21305384d089SMikolaj Golub if (locked_pa != 0) 21315384d089SMikolaj Golub vm_page_unlock(PHYS_TO_VM_PAGE(locked_pa)); 21325384d089SMikolaj Golub if (mincoreinfo & MINCORE_INCORE) 213343151ee6SPeter Wemm kve->kve_resident++; 21345384d089SMikolaj Golub if (mincoreinfo & MINCORE_SUPER) 21355384d089SMikolaj Golub kve->kve_flags |= KVME_FLAG_SUPER; 213643151ee6SPeter Wemm addr += PAGE_SIZE; 213743151ee6SPeter Wemm } 213843151ee6SPeter Wemm 213943151ee6SPeter Wemm for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) { 214043151ee6SPeter Wemm if (tobj != obj) 214143151ee6SPeter Wemm VM_OBJECT_LOCK(tobj); 214243151ee6SPeter Wemm if (lobj != obj) 214343151ee6SPeter Wemm VM_OBJECT_UNLOCK(lobj); 214443151ee6SPeter Wemm lobj = tobj; 214543151ee6SPeter Wemm } 214643151ee6SPeter Wemm 2147118d0afaSKonstantin Belousov kve->kve_start = entry->start; 2148118d0afaSKonstantin Belousov kve->kve_end = entry->end; 2149118d0afaSKonstantin Belousov kve->kve_offset = entry->offset; 2150118d0afaSKonstantin Belousov 2151118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_READ) 2152118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_READ; 2153118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_WRITE) 2154118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_WRITE; 2155118d0afaSKonstantin Belousov if (entry->protection & VM_PROT_EXECUTE) 2156118d0afaSKonstantin Belousov kve->kve_protection |= KVME_PROT_EXEC; 2157118d0afaSKonstantin Belousov 2158118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_COW) 2159118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_COW; 2160118d0afaSKonstantin Belousov if (entry->eflags & MAP_ENTRY_NEEDS_COPY) 2161118d0afaSKonstantin Belousov kve->kve_flags |= KVME_FLAG_NEEDS_COPY; 2162937912eaSAttilio Rao if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 2163937912eaSAttilio Rao kve->kve_flags |= KVME_FLAG_NOCOREDUMP; 2164118d0afaSKonstantin Belousov 2165118d0afaSKonstantin Belousov last_timestamp = map->timestamp; 2166118d0afaSKonstantin Belousov vm_map_unlock_read(map); 2167118d0afaSKonstantin Belousov 216843151ee6SPeter Wemm freepath = NULL; 216943151ee6SPeter Wemm fullpath = ""; 217043151ee6SPeter Wemm if (lobj) { 217143151ee6SPeter Wemm vp = NULL; 217243151ee6SPeter Wemm switch (lobj->type) { 217343151ee6SPeter Wemm case OBJT_DEFAULT: 217443151ee6SPeter Wemm kve->kve_type = KVME_TYPE_DEFAULT; 217543151ee6SPeter Wemm break; 217643151ee6SPeter Wemm case OBJT_VNODE: 217743151ee6SPeter Wemm kve->kve_type = KVME_TYPE_VNODE; 217843151ee6SPeter Wemm vp = lobj->handle; 217943151ee6SPeter Wemm vref(vp); 218043151ee6SPeter Wemm break; 218143151ee6SPeter Wemm case OBJT_SWAP: 218243151ee6SPeter Wemm kve->kve_type = KVME_TYPE_SWAP; 218343151ee6SPeter Wemm break; 218443151ee6SPeter Wemm case OBJT_DEVICE: 218543151ee6SPeter Wemm kve->kve_type = KVME_TYPE_DEVICE; 218643151ee6SPeter Wemm break; 218743151ee6SPeter Wemm case OBJT_PHYS: 218843151ee6SPeter Wemm kve->kve_type = KVME_TYPE_PHYS; 218943151ee6SPeter Wemm break; 219043151ee6SPeter Wemm case OBJT_DEAD: 219143151ee6SPeter Wemm kve->kve_type = KVME_TYPE_DEAD; 219243151ee6SPeter Wemm break; 219301381811SJohn Baldwin case OBJT_SG: 219401381811SJohn Baldwin kve->kve_type = KVME_TYPE_SG; 219501381811SJohn Baldwin break; 219643151ee6SPeter Wemm default: 219743151ee6SPeter Wemm kve->kve_type = KVME_TYPE_UNKNOWN; 219843151ee6SPeter Wemm break; 219943151ee6SPeter Wemm } 220043151ee6SPeter Wemm if (lobj != obj) 220143151ee6SPeter Wemm VM_OBJECT_UNLOCK(lobj); 220243151ee6SPeter Wemm 220343151ee6SPeter Wemm kve->kve_ref_count = obj->ref_count; 220443151ee6SPeter Wemm kve->kve_shadow_count = obj->shadow_count; 220543151ee6SPeter Wemm VM_OBJECT_UNLOCK(obj); 220643151ee6SPeter Wemm if (vp != NULL) { 220743151ee6SPeter Wemm vn_fullpath(curthread, vp, &fullpath, 220843151ee6SPeter Wemm &freepath); 22090daf62d9SStanislav Sedov kve->kve_vn_type = vntype_to_kinfo(vp->v_type); 221043151ee6SPeter Wemm cred = curthread->td_ucred; 221143151ee6SPeter Wemm vfslocked = VFS_LOCK_GIANT(vp->v_mount); 221243151ee6SPeter Wemm vn_lock(vp, LK_SHARED | LK_RETRY); 221343151ee6SPeter Wemm if (VOP_GETATTR(vp, &va, cred) == 0) { 22140daf62d9SStanislav Sedov kve->kve_vn_fileid = va.va_fileid; 22150daf62d9SStanislav Sedov kve->kve_vn_fsid = va.va_fsid; 22160daf62d9SStanislav Sedov kve->kve_vn_mode = 22170daf62d9SStanislav Sedov MAKEIMODE(va.va_type, va.va_mode); 22180daf62d9SStanislav Sedov kve->kve_vn_size = va.va_size; 22190daf62d9SStanislav Sedov kve->kve_vn_rdev = va.va_rdev; 22200daf62d9SStanislav Sedov kve->kve_status = KF_ATTR_VALID; 222143151ee6SPeter Wemm } 222243151ee6SPeter Wemm vput(vp); 222343151ee6SPeter Wemm VFS_UNLOCK_GIANT(vfslocked); 222443151ee6SPeter Wemm } 222543151ee6SPeter Wemm } else { 222643151ee6SPeter Wemm kve->kve_type = KVME_TYPE_NONE; 222743151ee6SPeter Wemm kve->kve_ref_count = 0; 222843151ee6SPeter Wemm kve->kve_shadow_count = 0; 222943151ee6SPeter Wemm } 223043151ee6SPeter Wemm 223143151ee6SPeter Wemm strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path)); 223243151ee6SPeter Wemm if (freepath != NULL) 223343151ee6SPeter Wemm free(freepath, M_TEMP); 223443151ee6SPeter Wemm 223543151ee6SPeter Wemm /* Pack record size down */ 223643151ee6SPeter Wemm kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) + 223743151ee6SPeter Wemm strlen(kve->kve_path) + 1; 223843151ee6SPeter Wemm kve->kve_structsize = roundup(kve->kve_structsize, 223943151ee6SPeter Wemm sizeof(uint64_t)); 224043151ee6SPeter Wemm error = SYSCTL_OUT(req, kve, kve->kve_structsize); 224143151ee6SPeter Wemm vm_map_lock_read(map); 224243151ee6SPeter Wemm if (error) 224343151ee6SPeter Wemm break; 224422a448c4SKonstantin Belousov if (last_timestamp != map->timestamp) { 224543151ee6SPeter Wemm vm_map_lookup_entry(map, addr - 1, &tmp_entry); 224643151ee6SPeter Wemm entry = tmp_entry; 224743151ee6SPeter Wemm } 224843151ee6SPeter Wemm } 224943151ee6SPeter Wemm vm_map_unlock_read(map); 2250c7462f43SKonstantin Belousov vmspace_free(vm); 225143151ee6SPeter Wemm PRELE(p); 225243151ee6SPeter Wemm free(kve, M_TEMP); 225343151ee6SPeter Wemm return (error); 225443151ee6SPeter Wemm } 2255cc43c38cSRobert Watson 22561cc8c45cSRobert Watson #if defined(STACK) || defined(DDB) 22571cc8c45cSRobert Watson static int 22581cc8c45cSRobert Watson sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS) 22591cc8c45cSRobert Watson { 22601cc8c45cSRobert Watson struct kinfo_kstack *kkstp; 22611cc8c45cSRobert Watson int error, i, *name, numthreads; 22621cc8c45cSRobert Watson lwpid_t *lwpidarray; 22631cc8c45cSRobert Watson struct thread *td; 22641cc8c45cSRobert Watson struct stack *st; 22651cc8c45cSRobert Watson struct sbuf sb; 22661cc8c45cSRobert Watson struct proc *p; 22671cc8c45cSRobert Watson 22681cc8c45cSRobert Watson name = (int *)arg1; 22691cc8c45cSRobert Watson if ((p = pfind((pid_t)name[0])) == NULL) 22701cc8c45cSRobert Watson return (ESRCH); 22710417fe54SRobert Watson /* XXXRW: Not clear ESRCH is the right error during proc execve(). */ 22720417fe54SRobert Watson if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) { 227363d79c4fSRobert Watson PROC_UNLOCK(p); 227463d79c4fSRobert Watson return (ESRCH); 227563d79c4fSRobert Watson } 22761cc8c45cSRobert Watson if ((error = p_candebug(curthread, p))) { 22771cc8c45cSRobert Watson PROC_UNLOCK(p); 22781cc8c45cSRobert Watson return (error); 22791cc8c45cSRobert Watson } 22801cc8c45cSRobert Watson _PHOLD(p); 22811cc8c45cSRobert Watson PROC_UNLOCK(p); 22821cc8c45cSRobert Watson 22831cc8c45cSRobert Watson kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK); 22841cc8c45cSRobert Watson st = stack_create(); 22851cc8c45cSRobert Watson 22861cc8c45cSRobert Watson lwpidarray = NULL; 22871cc8c45cSRobert Watson numthreads = 0; 2288374ae2a3SJeff Roberson PROC_LOCK(p); 22891cc8c45cSRobert Watson repeat: 22901cc8c45cSRobert Watson if (numthreads < p->p_numthreads) { 22911cc8c45cSRobert Watson if (lwpidarray != NULL) { 22921cc8c45cSRobert Watson free(lwpidarray, M_TEMP); 22931cc8c45cSRobert Watson lwpidarray = NULL; 22941cc8c45cSRobert Watson } 22951cc8c45cSRobert Watson numthreads = p->p_numthreads; 2296374ae2a3SJeff Roberson PROC_UNLOCK(p); 22971cc8c45cSRobert Watson lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP, 22981cc8c45cSRobert Watson M_WAITOK | M_ZERO); 2299374ae2a3SJeff Roberson PROC_LOCK(p); 23001cc8c45cSRobert Watson goto repeat; 23011cc8c45cSRobert Watson } 23021cc8c45cSRobert Watson i = 0; 23031cc8c45cSRobert Watson 23041cc8c45cSRobert Watson /* 23051cc8c45cSRobert Watson * XXXRW: During the below loop, execve(2) and countless other sorts 23061cc8c45cSRobert Watson * of changes could have taken place. Should we check to see if the 23071cc8c45cSRobert Watson * vmspace has been replaced, or the like, in order to prevent 23081cc8c45cSRobert Watson * giving a snapshot that spans, say, execve(2), with some threads 23091cc8c45cSRobert Watson * before and some after? Among other things, the credentials could 23101cc8c45cSRobert Watson * have changed, in which case the right to extract debug info might 23111cc8c45cSRobert Watson * no longer be assured. 23121cc8c45cSRobert Watson */ 23131cc8c45cSRobert Watson FOREACH_THREAD_IN_PROC(p, td) { 23141cc8c45cSRobert Watson KASSERT(i < numthreads, 23151cc8c45cSRobert Watson ("sysctl_kern_proc_kstack: numthreads")); 23161cc8c45cSRobert Watson lwpidarray[i] = td->td_tid; 23171cc8c45cSRobert Watson i++; 23181cc8c45cSRobert Watson } 23191cc8c45cSRobert Watson numthreads = i; 23201cc8c45cSRobert Watson for (i = 0; i < numthreads; i++) { 23211cc8c45cSRobert Watson td = thread_find(p, lwpidarray[i]); 23221cc8c45cSRobert Watson if (td == NULL) { 23231cc8c45cSRobert Watson continue; 23241cc8c45cSRobert Watson } 23251cc8c45cSRobert Watson bzero(kkstp, sizeof(*kkstp)); 23261cc8c45cSRobert Watson (void)sbuf_new(&sb, kkstp->kkst_trace, 23271cc8c45cSRobert Watson sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN); 23281cc8c45cSRobert Watson thread_lock(td); 23291cc8c45cSRobert Watson kkstp->kkst_tid = td->td_tid; 23301cc8c45cSRobert Watson if (TD_IS_SWAPPED(td)) 23311cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_SWAPPED; 23321cc8c45cSRobert Watson else if (TD_IS_RUNNING(td)) 23331cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_RUNNING; 23341cc8c45cSRobert Watson else { 23351cc8c45cSRobert Watson kkstp->kkst_state = KKST_STATE_STACKOK; 23361cc8c45cSRobert Watson stack_save_td(st, td); 23371cc8c45cSRobert Watson } 23381cc8c45cSRobert Watson thread_unlock(td); 23391cc8c45cSRobert Watson PROC_UNLOCK(p); 23401cc8c45cSRobert Watson stack_sbuf_print(&sb, st); 23411cc8c45cSRobert Watson sbuf_finish(&sb); 23421cc8c45cSRobert Watson sbuf_delete(&sb); 23431cc8c45cSRobert Watson error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp)); 23441cc8c45cSRobert Watson PROC_LOCK(p); 23451cc8c45cSRobert Watson if (error) 23461cc8c45cSRobert Watson break; 23471cc8c45cSRobert Watson } 23481cc8c45cSRobert Watson _PRELE(p); 23491cc8c45cSRobert Watson PROC_UNLOCK(p); 23501cc8c45cSRobert Watson if (lwpidarray != NULL) 23511cc8c45cSRobert Watson free(lwpidarray, M_TEMP); 23521cc8c45cSRobert Watson stack_destroy(st); 23531cc8c45cSRobert Watson free(kkstp, M_TEMP); 23541cc8c45cSRobert Watson return (error); 23551cc8c45cSRobert Watson } 23561cc8c45cSRobert Watson #endif 23571cc8c45cSRobert Watson 2358254d03c5SBrooks Davis /* 2359254d03c5SBrooks Davis * This sysctl allows a process to retrieve the full list of groups from 2360254d03c5SBrooks Davis * itself or another process. 2361254d03c5SBrooks Davis */ 2362254d03c5SBrooks Davis static int 2363254d03c5SBrooks Davis sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS) 2364254d03c5SBrooks Davis { 2365254d03c5SBrooks Davis pid_t *pidp = (pid_t *)arg1; 2366254d03c5SBrooks Davis unsigned int arglen = arg2; 2367254d03c5SBrooks Davis struct proc *p; 2368254d03c5SBrooks Davis struct ucred *cred; 2369254d03c5SBrooks Davis int error; 2370254d03c5SBrooks Davis 2371254d03c5SBrooks Davis if (arglen != 1) 2372254d03c5SBrooks Davis return (EINVAL); 2373254d03c5SBrooks Davis if (*pidp == -1) { /* -1 means this process */ 2374254d03c5SBrooks Davis p = req->td->td_proc; 2375254d03c5SBrooks Davis } else { 2376254d03c5SBrooks Davis p = pfind(*pidp); 2377254d03c5SBrooks Davis if (p == NULL) 2378254d03c5SBrooks Davis return (ESRCH); 2379254d03c5SBrooks Davis if ((error = p_cansee(curthread, p)) != 0) { 2380254d03c5SBrooks Davis PROC_UNLOCK(p); 2381254d03c5SBrooks Davis return (error); 2382254d03c5SBrooks Davis } 2383254d03c5SBrooks Davis } 2384254d03c5SBrooks Davis 2385254d03c5SBrooks Davis cred = crhold(p->p_ucred); 2386254d03c5SBrooks Davis if (*pidp != -1) 2387254d03c5SBrooks Davis PROC_UNLOCK(p); 2388254d03c5SBrooks Davis 2389254d03c5SBrooks Davis error = SYSCTL_OUT(req, cred->cr_groups, 2390254d03c5SBrooks Davis cred->cr_ngroups * sizeof(gid_t)); 2391254d03c5SBrooks Davis crfree(cred); 2392254d03c5SBrooks Davis return (error); 2393254d03c5SBrooks Davis } 2394254d03c5SBrooks Davis 23959e7d0583SMikolaj Golub /* 23969e7d0583SMikolaj Golub * This sysctl allows a process to retrieve the resource limits for 23979e7d0583SMikolaj Golub * another process. 23989e7d0583SMikolaj Golub */ 23999e7d0583SMikolaj Golub static int 24009e7d0583SMikolaj Golub sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS) 24019e7d0583SMikolaj Golub { 24029e7d0583SMikolaj Golub int *name = (int*) arg1; 24039e7d0583SMikolaj Golub u_int namelen = arg2; 24049e7d0583SMikolaj Golub struct plimit *limp; 24059e7d0583SMikolaj Golub struct proc *p; 24069e7d0583SMikolaj Golub int error = 0; 24079e7d0583SMikolaj Golub 24089e7d0583SMikolaj Golub if (namelen != 1) 24099e7d0583SMikolaj Golub return (EINVAL); 24109e7d0583SMikolaj Golub 24119e7d0583SMikolaj Golub p = pfind((pid_t)name[0]); 24129e7d0583SMikolaj Golub if (p == NULL) 24139e7d0583SMikolaj Golub return (ESRCH); 24149e7d0583SMikolaj Golub 24159e7d0583SMikolaj Golub if ((error = p_cansee(curthread, p)) != 0) { 24169e7d0583SMikolaj Golub PROC_UNLOCK(p); 24179e7d0583SMikolaj Golub return (error); 24189e7d0583SMikolaj Golub } 24199e7d0583SMikolaj Golub 24209e7d0583SMikolaj Golub /* 24219e7d0583SMikolaj Golub * Check the request size. We alow sizes smaller rlimit array for 24229e7d0583SMikolaj Golub * backward binary compatibility: the number of resource limits may 24239e7d0583SMikolaj Golub * grow. 24249e7d0583SMikolaj Golub */ 24259e7d0583SMikolaj Golub if (sizeof(limp->pl_rlimit) < req->oldlen) { 24269e7d0583SMikolaj Golub PROC_UNLOCK(p); 24279e7d0583SMikolaj Golub return (EINVAL); 24289e7d0583SMikolaj Golub } 24299e7d0583SMikolaj Golub 24309e7d0583SMikolaj Golub limp = lim_hold(p->p_limit); 24319e7d0583SMikolaj Golub PROC_UNLOCK(p); 24329e7d0583SMikolaj Golub error = SYSCTL_OUT(req, limp->pl_rlimit, req->oldlen); 24339e7d0583SMikolaj Golub lim_free(limp); 24349e7d0583SMikolaj Golub return (error); 24359e7d0583SMikolaj Golub } 24369e7d0583SMikolaj Golub 2437*9732458fSMikolaj Golub /* 2438*9732458fSMikolaj Golub * This sysctl allows a process to retrieve ps_strings structure location of 2439*9732458fSMikolaj Golub * another process. 2440*9732458fSMikolaj Golub */ 2441*9732458fSMikolaj Golub static int 2442*9732458fSMikolaj Golub sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS) 2443*9732458fSMikolaj Golub { 2444*9732458fSMikolaj Golub int *name = (int*) arg1; 2445*9732458fSMikolaj Golub u_int namelen = arg2; 2446*9732458fSMikolaj Golub struct proc *p; 2447*9732458fSMikolaj Golub vm_offset_t ps_strings; 2448*9732458fSMikolaj Golub int error; 2449*9732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32 2450*9732458fSMikolaj Golub uint32_t ps_strings32; 2451*9732458fSMikolaj Golub #endif 2452*9732458fSMikolaj Golub 2453*9732458fSMikolaj Golub if (namelen != 1) 2454*9732458fSMikolaj Golub return (EINVAL); 2455*9732458fSMikolaj Golub 2456*9732458fSMikolaj Golub p = pfind((pid_t)name[0]); 2457*9732458fSMikolaj Golub if (p == NULL) 2458*9732458fSMikolaj Golub return (ESRCH); 2459*9732458fSMikolaj Golub if (p->p_flag & P_WEXIT) { 2460*9732458fSMikolaj Golub PROC_UNLOCK(p); 2461*9732458fSMikolaj Golub return (ESRCH); 2462*9732458fSMikolaj Golub } 2463*9732458fSMikolaj Golub if ((error = p_cansee(curthread, p)) != 0) { 2464*9732458fSMikolaj Golub PROC_UNLOCK(p); 2465*9732458fSMikolaj Golub return (error); 2466*9732458fSMikolaj Golub } 2467*9732458fSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) { 2468*9732458fSMikolaj Golub PROC_UNLOCK(p); 2469*9732458fSMikolaj Golub return (0); 2470*9732458fSMikolaj Golub } 2471*9732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32 2472*9732458fSMikolaj Golub if ((req->flags & SCTL_MASK32) != 0) { 2473*9732458fSMikolaj Golub /* 2474*9732458fSMikolaj Golub * We return 0 if the 32 bit emulation request is for a 64 bit 2475*9732458fSMikolaj Golub * process. 2476*9732458fSMikolaj Golub */ 2477*9732458fSMikolaj Golub ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ? 2478*9732458fSMikolaj Golub PTROUT(p->p_sysent->sv_psstrings) : 0; 2479*9732458fSMikolaj Golub PROC_UNLOCK(p); 2480*9732458fSMikolaj Golub error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32)); 2481*9732458fSMikolaj Golub return (error); 2482*9732458fSMikolaj Golub } 2483*9732458fSMikolaj Golub #endif 2484*9732458fSMikolaj Golub ps_strings = p->p_sysent->sv_psstrings; 2485*9732458fSMikolaj Golub PROC_UNLOCK(p); 2486*9732458fSMikolaj Golub error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings)); 2487*9732458fSMikolaj Golub return (error); 2488*9732458fSMikolaj Golub } 2489*9732458fSMikolaj Golub 2490cc43c38cSRobert Watson SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 24913ce93e4eSPoul-Henning Kamp 249224f87fdbSJohn Baldwin SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT| 249324f87fdbSJohn Baldwin CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc", 249424f87fdbSJohn Baldwin "Return entire process table"); 24953ce93e4eSPoul-Henning Kamp 249624f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2497078842c5SGarance A Drosehn sysctl_kern_proc, "Process table"); 2498078842c5SGarance A Drosehn 249924f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE, 25003ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 25013ce93e4eSPoul-Henning Kamp 250224f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE, 2503b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 2504b8fdc89dSGarance A Drosehn 250524f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD | 250624f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 250724f87fdbSJohn Baldwin 250824f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE, 2509b8fdc89dSGarance A Drosehn sysctl_kern_proc, "Process table"); 2510b8fdc89dSGarance A Drosehn 251124f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE, 25123ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 25133ce93e4eSPoul-Henning Kamp 251424f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE, 25153ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 25163ce93e4eSPoul-Henning Kamp 251724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE, 25183ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 25193ce93e4eSPoul-Henning Kamp 252024f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 252130c6f34eSScott Long sysctl_kern_proc, "Return process table, no threads"); 252230c6f34eSScott Long 25230c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, 252424f87fdbSJohn Baldwin CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 25259b6d9dbaSPoul-Henning Kamp sysctl_kern_proc_args, "Process argument list"); 2526baf731e6SRobert Drehmel 2527c5cfcb1cSMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, 2528c5cfcb1cSMikolaj Golub CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2529c5cfcb1cSMikolaj Golub sysctl_kern_proc_env, "Process environment"); 2530c5cfcb1cSMikolaj Golub 2531c5cfcb1cSMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, 2532c5cfcb1cSMikolaj Golub CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2533c5cfcb1cSMikolaj Golub sysctl_kern_proc_auxv, "Process ELF auxiliary vector"); 2534c5cfcb1cSMikolaj Golub 253524f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD | 253624f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path"); 2537fe769cddSDavid Schultz 253824f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD | 253924f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name, 254024f87fdbSJohn Baldwin "Process syscall vector name (ABI type)"); 25412648efa6SDaniel Eischen 25420c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td, 254324f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2544078842c5SGarance A Drosehn 25450c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td, 254624f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25472648efa6SDaniel Eischen 25480c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td, 254924f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2550b8fdc89dSGarance A Drosehn 25510c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD), 255224f87fdbSJohn Baldwin sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25530c898376SPoul-Henning Kamp 25540c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td, 255524f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 2556b8fdc89dSGarance A Drosehn 25570c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td, 255824f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25592648efa6SDaniel Eischen 25600c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td, 256124f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25622648efa6SDaniel Eischen 25630c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td, 256424f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table"); 25652648efa6SDaniel Eischen 25660c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td, 256724f87fdbSJohn Baldwin CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, 256824f87fdbSJohn Baldwin "Return process table, no threads"); 2569cc43c38cSRobert Watson 257043151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7 257124f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD | 257224f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries"); 257343151ee6SPeter Wemm #endif 257443151ee6SPeter Wemm 257524f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD | 257624f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries"); 25771cc8c45cSRobert Watson 25781cc8c45cSRobert Watson #if defined(STACK) || defined(DDB) 257924f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD | 258024f87fdbSJohn Baldwin CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks"); 25811cc8c45cSRobert Watson #endif 2582254d03c5SBrooks Davis 2583254d03c5SBrooks Davis static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD | 2584254d03c5SBrooks Davis CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups"); 25859e7d0583SMikolaj Golub 25869e7d0583SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RD | 25879e7d0583SMikolaj Golub CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit, "Process resource limits"); 2588*9732458fSMikolaj Golub 2589*9732458fSMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, 2590*9732458fSMikolaj Golub CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 2591*9732458fSMikolaj Golub sysctl_kern_proc_ps_strings, "Process ps_strings location"); 2592