xref: /freebsd/sys/kern/kern_proc.c (revision 4d34e019c4df776fe9c114e3945a413e30ed48a5)
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>
636ce13747SMikolaj Golub #include <sys/stat.h>
64fb919e4dSMark Murray #include <sys/sysctl.h>
6562d6ce3aSDon Lewis #include <sys/filedesc.h>
66df8bae1dSRodney W. Grimes #include <sys/tty.h>
67bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h>
685d217f17SJohn Birrell #include <sys/sdt.h>
691005a129SJohn Baldwin #include <sys/sx.h>
70fb919e4dSMark Murray #include <sys/user.h>
71fb919e4dSMark Murray #include <sys/jail.h>
72fe769cddSDavid Schultz #include <sys/vnode.h>
734a62a3e5SRandall Stewart #include <sys/eventhandler.h>
74fb919e4dSMark Murray 
75cc43c38cSRobert Watson #ifdef DDB
76cc43c38cSRobert Watson #include <ddb/ddb.h>
77cc43c38cSRobert Watson #endif
78cc43c38cSRobert Watson 
79efeaf95aSDavid Greenman #include <vm/vm.h>
80a136efe9SPeter Wemm #include <vm/vm_extern.h>
81efeaf95aSDavid Greenman #include <vm/pmap.h>
82efeaf95aSDavid Greenman #include <vm/vm_map.h>
83cc43c38cSRobert Watson #include <vm/vm_object.h>
845384d089SMikolaj Golub #include <vm/vm_page.h>
85c897b813SJeff Roberson #include <vm/uma.h>
86df8bae1dSRodney W. Grimes 
8705e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32
8805e06d11SKonstantin Belousov #include <compat/freebsd32/freebsd32.h>
8905e06d11SKonstantin Belousov #include <compat/freebsd32/freebsd32_util.h>
9005e06d11SKonstantin Belousov #endif
9105e06d11SKonstantin Belousov 
925d217f17SJohn Birrell SDT_PROVIDER_DEFINE(proc);
9379856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry);
945d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
955d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
965d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
975d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
9879856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, ctor, return, return);
995d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
1005d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
1015d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
1025d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
10379856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry);
1045d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
1055d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
1065d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
1075d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
10879856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, dtor, return, return);
1095d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
1105d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
1115d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
11279856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, init, entry, entry);
1135d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
1145d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
1155d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
11679856499SRui Paulo SDT_PROBE_DEFINE(proc, kernel, init, return, return);
1175d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
1185d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
1195d217f17SJohn Birrell SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
1205d217f17SJohn Birrell 
121f591779bSSeigo Tanimura MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
122a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SESSION, "session", "session header");
123876a94eeSBruce Evans static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
124a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
12555166637SPoul-Henning Kamp 
1264d77a549SAlfred Perlstein static void doenterpgrp(struct proc *, struct pgrp *);
1274d77a549SAlfred Perlstein static void orphanpg(struct pgrp *pg);
128f8d90480SAttilio Rao static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
1295032ff81SDon Lewis static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
130d92909c1SRobert Watson static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
131d92909c1SRobert Watson     int preferthread);
13202e878d9SJohn Baldwin static void pgadjustjobc(struct pgrp *pgrp, int entering);
13302e878d9SJohn Baldwin static void pgdelete(struct pgrp *);
134b23f72e9SBrian Feldman static int proc_ctor(void *mem, int size, void *arg, int flags);
135a136efe9SPeter Wemm static void proc_dtor(void *mem, int size, void *arg);
136b23f72e9SBrian Feldman static int proc_init(void *mem, int size, int flags);
137a136efe9SPeter Wemm static void proc_fini(void *mem, int size);
13858e8af1bSKonstantin Belousov static void pargs_free(struct pargs *pa);
139a136efe9SPeter Wemm 
140df8bae1dSRodney W. Grimes /*
141b75356e1SJeffrey Hsu  * Other process lists
142b75356e1SJeffrey Hsu  */
143b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl;
144b75356e1SJeffrey Hsu u_long pidhash;
145b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl;
146b75356e1SJeffrey Hsu u_long pgrphash;
147b75356e1SJeffrey Hsu struct proclist allproc;
148b75356e1SJeffrey Hsu struct proclist zombproc;
1491005a129SJohn Baldwin struct sx allproc_lock;
1501005a129SJohn Baldwin struct sx proctree_lock;
151c6544064SJohn Baldwin struct mtx ppeers_lock;
152c897b813SJeff Roberson uma_zone_t proc_zone;
153b75356e1SJeffrey Hsu 
154b9f009b0SPeter Wemm int kstack_pages = KSTACK_PAGES;
155b389be97SRebecca Cran SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
156b389be97SRebecca Cran     "Kernel stack size in pages");
157b9f009b0SPeter Wemm 
158a30d7c60SJake Burkholder CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
159ed780687SKonstantin Belousov #ifdef COMPAT_FREEBSD32
160ed780687SKonstantin Belousov CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
161ed780687SKonstantin Belousov #endif
162a30d7c60SJake Burkholder 
163b75356e1SJeffrey Hsu /*
164b75356e1SJeffrey Hsu  * Initialize global process hashing structures.
165df8bae1dSRodney W. Grimes  */
16626f9a767SRodney W. Grimes void
167b75356e1SJeffrey Hsu procinit()
168df8bae1dSRodney W. Grimes {
169df8bae1dSRodney W. Grimes 
1701005a129SJohn Baldwin 	sx_init(&allproc_lock, "allproc");
1711005a129SJohn Baldwin 	sx_init(&proctree_lock, "proctree");
172c6544064SJohn Baldwin 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
173b75356e1SJeffrey Hsu 	LIST_INIT(&allproc);
174b75356e1SJeffrey Hsu 	LIST_INIT(&zombproc);
175b75356e1SJeffrey Hsu 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
176b75356e1SJeffrey Hsu 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
177de028f5aSJeff Roberson 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
178a136efe9SPeter Wemm 	    proc_ctor, proc_dtor, proc_init, proc_fini,
179a136efe9SPeter Wemm 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
180f535380cSDon Lewis 	uihashinit();
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes /*
184a136efe9SPeter Wemm  * Prepare a proc for use.
185a136efe9SPeter Wemm  */
186b23f72e9SBrian Feldman static int
187b23f72e9SBrian Feldman proc_ctor(void *mem, int size, void *arg, int flags)
188a136efe9SPeter Wemm {
189a136efe9SPeter Wemm 	struct proc *p;
190a136efe9SPeter Wemm 
191a136efe9SPeter Wemm 	p = (struct proc *)mem;
1925d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
1934a62a3e5SRandall Stewart 	EVENTHANDLER_INVOKE(process_ctor, p);
1945d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
195b23f72e9SBrian Feldman 	return (0);
196a136efe9SPeter Wemm }
197a136efe9SPeter Wemm 
198a136efe9SPeter Wemm /*
199a136efe9SPeter Wemm  * Reclaim a proc after use.
200a136efe9SPeter Wemm  */
201a136efe9SPeter Wemm static void
202a136efe9SPeter Wemm proc_dtor(void *mem, int size, void *arg)
203a136efe9SPeter Wemm {
204a136efe9SPeter Wemm 	struct proc *p;
2051faf202eSJulian Elischer 	struct thread *td;
206a136efe9SPeter Wemm 
2071faf202eSJulian Elischer 	/* INVARIANTS checks go here */
208a136efe9SPeter Wemm 	p = (struct proc *)mem;
209ed062c8dSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);
2105d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
21189b57fcfSKonstantin Belousov 	if (td != NULL) {
212ed062c8dSJulian Elischer #ifdef INVARIANTS
2131faf202eSJulian Elischer 		KASSERT((p->p_numthreads == 1),
2141faf202eSJulian Elischer 		    ("bad number of threads in exiting process"));
2152c255e9dSRobert Watson 		KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
216ed062c8dSJulian Elischer #endif
2171ba4a712SPawel Jakub Dawidek 		/* Free all OSD associated to this thread. */
2181ba4a712SPawel Jakub Dawidek 		osd_thread_exit(td);
21989b57fcfSKonstantin Belousov 	}
2204a62a3e5SRandall Stewart 	EVENTHANDLER_INVOKE(process_dtor, p);
221ebceaf6dSDavid Xu 	if (p->p_ksi != NULL)
222ebceaf6dSDavid Xu 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
2235d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
224a136efe9SPeter Wemm }
225a136efe9SPeter Wemm 
226a136efe9SPeter Wemm /*
227a136efe9SPeter Wemm  * Initialize type-stable parts of a proc (when newly created).
228a136efe9SPeter Wemm  */
229b23f72e9SBrian Feldman static int
230b23f72e9SBrian Feldman proc_init(void *mem, int size, int flags)
231a136efe9SPeter Wemm {
232a136efe9SPeter Wemm 	struct proc *p;
233a136efe9SPeter Wemm 
234a136efe9SPeter Wemm 	p = (struct proc *)mem;
2355d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
236de028f5aSJeff Roberson 	p->p_sched = (struct p_sched *)&p[1];
2377d447c95SJohn Baldwin 	bzero(&p->p_mtx, sizeof(struct mtx));
2387d447c95SJohn Baldwin 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
239982d11f8SJeff Roberson 	mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
240aeb32571SKonstantin Belousov 	cv_init(&p->p_pwait, "ppwait");
2416fa39a73SKonstantin Belousov 	cv_init(&p->p_dbgwait, "dbgwait");
24289b57fcfSKonstantin Belousov 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
2434a62a3e5SRandall Stewart 	EVENTHANDLER_INVOKE(process_init, p);
2448b059651SDavid Schultz 	p->p_stats = pstats_alloc();
2455d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
246b23f72e9SBrian Feldman 	return (0);
247a136efe9SPeter Wemm }
248a136efe9SPeter Wemm 
249a136efe9SPeter Wemm /*
2508daa8c60SDavid Schultz  * UMA should ensure that this function is never called.
2518daa8c60SDavid Schultz  * Freeing a proc structure would violate type stability.
252a136efe9SPeter Wemm  */
253a136efe9SPeter Wemm static void
254a136efe9SPeter Wemm proc_fini(void *mem, int size)
255a136efe9SPeter Wemm {
256f55ab994SJohn Baldwin #ifdef notnow
257f55ab994SJohn Baldwin 	struct proc *p;
258a136efe9SPeter Wemm 
259f55ab994SJohn Baldwin 	p = (struct proc *)mem;
2604a62a3e5SRandall Stewart 	EVENTHANDLER_INVOKE(process_fini, p);
261f55ab994SJohn Baldwin 	pstats_free(p->p_stats);
262f55ab994SJohn Baldwin 	thread_free(FIRST_THREAD_IN_PROC(p));
263f55ab994SJohn Baldwin 	mtx_destroy(&p->p_mtx);
264ebceaf6dSDavid Xu 	if (p->p_ksi != NULL)
265ebceaf6dSDavid Xu 		ksiginfo_free(p->p_ksi);
266f55ab994SJohn Baldwin #else
2678daa8c60SDavid Schultz 	panic("proc reclaimed");
268f55ab994SJohn Baldwin #endif
269a136efe9SPeter Wemm }
270a136efe9SPeter Wemm 
271a136efe9SPeter Wemm /*
272df8bae1dSRodney W. Grimes  * Is p an inferior of the current process?
273df8bae1dSRodney W. Grimes  */
2741a432a2fSDima Ruban int
275df8bae1dSRodney W. Grimes inferior(p)
276df8bae1dSRodney W. Grimes 	register struct proc *p;
277df8bae1dSRodney W. Grimes {
278df8bae1dSRodney W. Grimes 
2795b29d6e9SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
2805b29d6e9SJohn Baldwin 	for (; p != curproc; p = p->p_pptr)
28100f13cb3SJohn Baldwin 		if (p->p_pid == 0)
28200f13cb3SJohn Baldwin 			return (0);
28300f13cb3SJohn Baldwin 	return (1);
284df8bae1dSRodney W. Grimes }
285df8bae1dSRodney W. Grimes 
286df8bae1dSRodney W. Grimes /*
2876cbea71cSRobert Watson  * Locate a process by number; return only "live" processes -- i.e., neither
2886cbea71cSRobert Watson  * zombies nor newly born but incompletely initialized processes.  By not
2896cbea71cSRobert Watson  * returning processes in the PRS_NEW state, we allow callers to avoid
2906cbea71cSRobert Watson  * testing for that condition to avoid dereferencing p_ucred, et al.
291df8bae1dSRodney W. Grimes  */
292df8bae1dSRodney W. Grimes struct proc *
293df8bae1dSRodney W. Grimes pfind(pid)
294df8bae1dSRodney W. Grimes 	register pid_t pid;
295df8bae1dSRodney W. Grimes {
296df8bae1dSRodney W. Grimes 	register struct proc *p;
297df8bae1dSRodney W. Grimes 
2981005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2991b727751SPoul-Henning Kamp 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
30033a9ed9dSJohn Baldwin 		if (p->p_pid == pid) {
30133a9ed9dSJohn Baldwin 			PROC_LOCK(p);
3028e6fa660SJohn Baldwin 			if (p->p_state == PRS_NEW) {
3038e6fa660SJohn Baldwin 				PROC_UNLOCK(p);
3048e6fa660SJohn Baldwin 				p = NULL;
3058e6fa660SJohn Baldwin 			}
306553629ebSJake Burkholder 			break;
30733a9ed9dSJohn Baldwin 		}
30898ab1489SJeffrey Hsu 	sx_sunlock(&allproc_lock);
309df8bae1dSRodney W. Grimes 	return (p);
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312b3bfb267SKonstantin Belousov static struct proc *
313b3bfb267SKonstantin Belousov pfind_tid(pid_t tid)
314b3bfb267SKonstantin Belousov {
315b3bfb267SKonstantin Belousov 	struct proc *p;
316b3bfb267SKonstantin Belousov 	struct thread *td;
317b3bfb267SKonstantin Belousov 
318b3bfb267SKonstantin Belousov 	sx_slock(&allproc_lock);
319b3bfb267SKonstantin Belousov 	FOREACH_PROC_IN_SYSTEM(p) {
320b3bfb267SKonstantin Belousov 		PROC_LOCK(p);
321b3bfb267SKonstantin Belousov 		if (p->p_state == PRS_NEW) {
322b3bfb267SKonstantin Belousov 			PROC_UNLOCK(p);
323b3bfb267SKonstantin Belousov 			continue;
324b3bfb267SKonstantin Belousov 		}
325b3bfb267SKonstantin Belousov 		FOREACH_THREAD_IN_PROC(p, td) {
326b3bfb267SKonstantin Belousov 			if (td->td_tid == tid)
327b3bfb267SKonstantin Belousov 				goto found;
328b3bfb267SKonstantin Belousov 		}
329b3bfb267SKonstantin Belousov 		PROC_UNLOCK(p);
330b3bfb267SKonstantin Belousov 	}
331b3bfb267SKonstantin Belousov found:
332b3bfb267SKonstantin Belousov 	sx_sunlock(&allproc_lock);
333b3bfb267SKonstantin Belousov 	return (p);
334b3bfb267SKonstantin Belousov }
335b3bfb267SKonstantin Belousov 
336df8bae1dSRodney W. Grimes /*
337f591779bSSeigo Tanimura  * Locate a process group by number.
338f089b570SJohn Baldwin  * The caller must hold proctree_lock.
339df8bae1dSRodney W. Grimes  */
340df8bae1dSRodney W. Grimes struct pgrp *
341df8bae1dSRodney W. Grimes pgfind(pgid)
342df8bae1dSRodney W. Grimes 	register pid_t pgid;
343df8bae1dSRodney W. Grimes {
344df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
345df8bae1dSRodney W. Grimes 
346f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
347f591779bSSeigo Tanimura 
348f591779bSSeigo Tanimura 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
349f591779bSSeigo Tanimura 		if (pgrp->pg_id == pgid) {
350f591779bSSeigo Tanimura 			PGRP_LOCK(pgrp);
351df8bae1dSRodney W. Grimes 			return (pgrp);
352f591779bSSeigo Tanimura 		}
353f591779bSSeigo Tanimura 	}
354df8bae1dSRodney W. Grimes 	return (NULL);
355df8bae1dSRodney W. Grimes }
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes /*
358fa3935bcSMikolaj Golub  * Locate process and do additional manipulations, depending on flags.
359fa3935bcSMikolaj Golub  */
360fa3935bcSMikolaj Golub int
361fa3935bcSMikolaj Golub pget(pid_t pid, int flags, struct proc **pp)
362fa3935bcSMikolaj Golub {
363fa3935bcSMikolaj Golub 	struct proc *p;
364fa3935bcSMikolaj Golub 	int error;
365fa3935bcSMikolaj Golub 
366b3bfb267SKonstantin Belousov 	if (pid <= PID_MAX)
367fa3935bcSMikolaj Golub 		p = pfind(pid);
368b3bfb267SKonstantin Belousov 	else if ((flags & PGET_NOTID) == 0)
369b3bfb267SKonstantin Belousov 		p = pfind_tid(pid);
370b3bfb267SKonstantin Belousov 	else
371b3bfb267SKonstantin Belousov 		p = NULL;
372fa3935bcSMikolaj Golub 	if (p == NULL)
373fa3935bcSMikolaj Golub 		return (ESRCH);
374fa3935bcSMikolaj Golub 	if ((flags & PGET_CANSEE) != 0) {
375fa3935bcSMikolaj Golub 		error = p_cansee(curthread, p);
376fa3935bcSMikolaj Golub 		if (error != 0)
377fa3935bcSMikolaj Golub 			goto errout;
378fa3935bcSMikolaj Golub 	}
379fa3935bcSMikolaj Golub 	if ((flags & PGET_CANDEBUG) != 0) {
380fa3935bcSMikolaj Golub 		error = p_candebug(curthread, p);
381fa3935bcSMikolaj Golub 		if (error != 0)
382fa3935bcSMikolaj Golub 			goto errout;
383fa3935bcSMikolaj Golub 	}
384fa3935bcSMikolaj Golub 	if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
385fa3935bcSMikolaj Golub 		error = EPERM;
386fa3935bcSMikolaj Golub 		goto errout;
387fa3935bcSMikolaj Golub 	}
388fa3935bcSMikolaj Golub 	if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
389fa3935bcSMikolaj Golub 		error = ESRCH;
390fa3935bcSMikolaj Golub 		goto errout;
391fa3935bcSMikolaj Golub 	}
392fa3935bcSMikolaj Golub 	if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
393fa3935bcSMikolaj Golub 		/*
394fa3935bcSMikolaj Golub 		 * XXXRW: Not clear ESRCH is the right error during proc
395fa3935bcSMikolaj Golub 		 * execve().
396fa3935bcSMikolaj Golub 		 */
397fa3935bcSMikolaj Golub 		error = ESRCH;
398fa3935bcSMikolaj Golub 		goto errout;
399fa3935bcSMikolaj Golub 	}
400fa3935bcSMikolaj Golub 	if ((flags & PGET_HOLD) != 0) {
401fa3935bcSMikolaj Golub 		_PHOLD(p);
402fa3935bcSMikolaj Golub 		PROC_UNLOCK(p);
403fa3935bcSMikolaj Golub 	}
404fa3935bcSMikolaj Golub 	*pp = p;
405fa3935bcSMikolaj Golub 	return (0);
406fa3935bcSMikolaj Golub errout:
407fa3935bcSMikolaj Golub 	PROC_UNLOCK(p);
408fa3935bcSMikolaj Golub 	return (error);
409fa3935bcSMikolaj Golub }
410fa3935bcSMikolaj Golub 
411fa3935bcSMikolaj Golub /*
412f591779bSSeigo Tanimura  * Create a new process group.
413f591779bSSeigo Tanimura  * pgid must be equal to the pid of p.
414f591779bSSeigo Tanimura  * Begin a new session if required.
415df8bae1dSRodney W. Grimes  */
41626f9a767SRodney W. Grimes int
417f591779bSSeigo Tanimura enterpgrp(p, pgid, pgrp, sess)
418df8bae1dSRodney W. Grimes 	register struct proc *p;
419df8bae1dSRodney W. Grimes 	pid_t pgid;
420f591779bSSeigo Tanimura 	struct pgrp *pgrp;
421f591779bSSeigo Tanimura 	struct session *sess;
422df8bae1dSRodney W. Grimes {
423f591779bSSeigo Tanimura 	struct pgrp *pgrp2;
424df8bae1dSRodney W. Grimes 
425f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
426f591779bSSeigo Tanimura 
427f591779bSSeigo Tanimura 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
428f591779bSSeigo Tanimura 	KASSERT(p->p_pid == pgid,
429f591779bSSeigo Tanimura 	    ("enterpgrp: new pgrp and pid != pgid"));
430f591779bSSeigo Tanimura 
431f591779bSSeigo Tanimura 	pgrp2 = pgfind(pgid);
432f591779bSSeigo Tanimura 
433f591779bSSeigo Tanimura 	KASSERT(pgrp2 == NULL,
434f591779bSSeigo Tanimura 	    ("enterpgrp: pgrp with pgid exists"));
4355526d2d9SEivind Eklund 	KASSERT(!SESS_LEADER(p),
4365526d2d9SEivind Eklund 	    ("enterpgrp: session leader attempted setpgrp"));
437219cbf59SEivind Eklund 
4386008862bSJohn Baldwin 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
439df8bae1dSRodney W. Grimes 
440f591779bSSeigo Tanimura 	if (sess != NULL) {
441df8bae1dSRodney W. Grimes 		/*
442df8bae1dSRodney W. Grimes 		 * new session
443df8bae1dSRodney W. Grimes 		 */
4446008862bSJohn Baldwin 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
445f591779bSSeigo Tanimura 		PROC_LOCK(p);
446f591779bSSeigo Tanimura 		p->p_flag &= ~P_CONTROLT;
447f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
448f591779bSSeigo Tanimura 		PGRP_LOCK(pgrp);
449df8bae1dSRodney W. Grimes 		sess->s_leader = p;
450643a8daaSDon Lewis 		sess->s_sid = p->p_pid;
451bc093719SEd Schouten 		refcount_init(&sess->s_count, 1);
452df8bae1dSRodney W. Grimes 		sess->s_ttyvp = NULL;
4538dc9b4cfSEd Schouten 		sess->s_ttydp = NULL;
454df8bae1dSRodney W. Grimes 		sess->s_ttyp = NULL;
455df8bae1dSRodney W. Grimes 		bcopy(p->p_session->s_login, sess->s_login,
456df8bae1dSRodney W. Grimes 			    sizeof(sess->s_login));
457df8bae1dSRodney W. Grimes 		pgrp->pg_session = sess;
4585526d2d9SEivind Eklund 		KASSERT(p == curproc,
4595526d2d9SEivind Eklund 		    ("enterpgrp: mksession and p != curproc"));
460df8bae1dSRodney W. Grimes 	} else {
461df8bae1dSRodney W. Grimes 		pgrp->pg_session = p->p_session;
462bc093719SEd Schouten 		sess_hold(pgrp->pg_session);
463f591779bSSeigo Tanimura 		PGRP_LOCK(pgrp);
464df8bae1dSRodney W. Grimes 	}
465df8bae1dSRodney W. Grimes 	pgrp->pg_id = pgid;
466b75356e1SJeffrey Hsu 	LIST_INIT(&pgrp->pg_members);
467f591779bSSeigo Tanimura 
468f591779bSSeigo Tanimura 	/*
469f089b570SJohn Baldwin 	 * As we have an exclusive lock of proctree_lock,
470f591779bSSeigo Tanimura 	 * this should not deadlock.
471f591779bSSeigo Tanimura 	 */
472b75356e1SJeffrey Hsu 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
473df8bae1dSRodney W. Grimes 	pgrp->pg_jobc = 0;
474831d27a9SDon Lewis 	SLIST_INIT(&pgrp->pg_sigiolst);
475f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
476f591779bSSeigo Tanimura 
477f591779bSSeigo Tanimura 	doenterpgrp(p, pgrp);
478f591779bSSeigo Tanimura 
479df8bae1dSRodney W. Grimes 	return (0);
480f591779bSSeigo Tanimura }
481f591779bSSeigo Tanimura 
482f591779bSSeigo Tanimura /*
483f591779bSSeigo Tanimura  * Move p to an existing process group
484f591779bSSeigo Tanimura  */
485f591779bSSeigo Tanimura int
486f591779bSSeigo Tanimura enterthispgrp(p, pgrp)
487f591779bSSeigo Tanimura 	register struct proc *p;
488f591779bSSeigo Tanimura 	struct pgrp *pgrp;
489f591779bSSeigo Tanimura {
490f089b570SJohn Baldwin 
491f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
492f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
493f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
494f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
495f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
496f591779bSSeigo Tanimura 	KASSERT(pgrp->pg_session == p->p_session,
497f591779bSSeigo Tanimura 		("%s: pgrp's session %p, p->p_session %p.\n",
498f591779bSSeigo Tanimura 		__func__,
499f591779bSSeigo Tanimura 		pgrp->pg_session,
500f591779bSSeigo Tanimura 		p->p_session));
501f591779bSSeigo Tanimura 	KASSERT(pgrp != p->p_pgrp,
502f591779bSSeigo Tanimura 		("%s: p belongs to pgrp.", __func__));
503f591779bSSeigo Tanimura 
504f591779bSSeigo Tanimura 	doenterpgrp(p, pgrp);
505f591779bSSeigo Tanimura 
506f591779bSSeigo Tanimura 	return (0);
507f591779bSSeigo Tanimura }
508f591779bSSeigo Tanimura 
509f591779bSSeigo Tanimura /*
510f591779bSSeigo Tanimura  * Move p to a process group
511f591779bSSeigo Tanimura  */
512f591779bSSeigo Tanimura static void
513f591779bSSeigo Tanimura doenterpgrp(p, pgrp)
514f591779bSSeigo Tanimura 	struct proc *p;
515f591779bSSeigo Tanimura 	struct pgrp *pgrp;
516f591779bSSeigo Tanimura {
517f591779bSSeigo Tanimura 	struct pgrp *savepgrp;
518f591779bSSeigo Tanimura 
519f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
520f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
521f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
522f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
523f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
524f591779bSSeigo Tanimura 
525f591779bSSeigo Tanimura 	savepgrp = p->p_pgrp;
526df8bae1dSRodney W. Grimes 
527df8bae1dSRodney W. Grimes 	/*
528df8bae1dSRodney W. Grimes 	 * Adjust eligibility of affected pgrps to participate in job control.
529df8bae1dSRodney W. Grimes 	 * Increment eligibility counts before decrementing, otherwise we
530df8bae1dSRodney W. Grimes 	 * could reach 0 spuriously during the first call.
531df8bae1dSRodney W. Grimes 	 */
532df8bae1dSRodney W. Grimes 	fixjobc(p, pgrp, 1);
533df8bae1dSRodney W. Grimes 	fixjobc(p, p->p_pgrp, 0);
534df8bae1dSRodney W. Grimes 
535f591779bSSeigo Tanimura 	PGRP_LOCK(pgrp);
536f591779bSSeigo Tanimura 	PGRP_LOCK(savepgrp);
53715e9ec51SJohn Baldwin 	PROC_LOCK(p);
538b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
539df8bae1dSRodney W. Grimes 	p->p_pgrp = pgrp;
54015e9ec51SJohn Baldwin 	PROC_UNLOCK(p);
541f591779bSSeigo Tanimura 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
542f591779bSSeigo Tanimura 	PGRP_UNLOCK(savepgrp);
543f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
544f591779bSSeigo Tanimura 	if (LIST_EMPTY(&savepgrp->pg_members))
545f591779bSSeigo Tanimura 		pgdelete(savepgrp);
546df8bae1dSRodney W. Grimes }
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes /*
549df8bae1dSRodney W. Grimes  * remove process from process group
550df8bae1dSRodney W. Grimes  */
55126f9a767SRodney W. Grimes int
552df8bae1dSRodney W. Grimes leavepgrp(p)
553df8bae1dSRodney W. Grimes 	register struct proc *p;
554df8bae1dSRodney W. Grimes {
555f591779bSSeigo Tanimura 	struct pgrp *savepgrp;
556df8bae1dSRodney W. Grimes 
557f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
558f591779bSSeigo Tanimura 	savepgrp = p->p_pgrp;
559f591779bSSeigo Tanimura 	PGRP_LOCK(savepgrp);
56015e9ec51SJohn Baldwin 	PROC_LOCK(p);
561b75356e1SJeffrey Hsu 	LIST_REMOVE(p, p_pglist);
56215e9ec51SJohn Baldwin 	p->p_pgrp = NULL;
56315e9ec51SJohn Baldwin 	PROC_UNLOCK(p);
564f591779bSSeigo Tanimura 	PGRP_UNLOCK(savepgrp);
565f591779bSSeigo Tanimura 	if (LIST_EMPTY(&savepgrp->pg_members))
566f591779bSSeigo Tanimura 		pgdelete(savepgrp);
567df8bae1dSRodney W. Grimes 	return (0);
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes /*
571df8bae1dSRodney W. Grimes  * delete a process group
572df8bae1dSRodney W. Grimes  */
57387b6de2bSPoul-Henning Kamp static void
574df8bae1dSRodney W. Grimes pgdelete(pgrp)
575df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
576df8bae1dSRodney W. Grimes {
577f591779bSSeigo Tanimura 	struct session *savesess;
578bc093719SEd Schouten 	struct tty *tp;
579f591779bSSeigo Tanimura 
580f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_XLOCKED);
581f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
582f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
583f591779bSSeigo Tanimura 
584831d27a9SDon Lewis 	/*
585831d27a9SDon Lewis 	 * Reset any sigio structures pointing to us as a result of
586831d27a9SDon Lewis 	 * F_SETOWN with our pgid.
587831d27a9SDon Lewis 	 */
588831d27a9SDon Lewis 	funsetownlst(&pgrp->pg_sigiolst);
589831d27a9SDon Lewis 
590e649887bSAlfred Perlstein 	PGRP_LOCK(pgrp);
591bc093719SEd Schouten 	tp = pgrp->pg_session->s_ttyp;
592b75356e1SJeffrey Hsu 	LIST_REMOVE(pgrp, pg_hash);
593f591779bSSeigo Tanimura 	savesess = pgrp->pg_session;
594f591779bSSeigo Tanimura 	PGRP_UNLOCK(pgrp);
595bc093719SEd Schouten 
596bc093719SEd Schouten 	/* Remove the reference to the pgrp before deallocating it. */
597bc093719SEd Schouten 	if (tp != NULL) {
598bc093719SEd Schouten 		tty_lock(tp);
599bc093719SEd Schouten 		tty_rel_pgrp(tp, pgrp);
600bc093719SEd Schouten 	}
601bc093719SEd Schouten 
6026041fa0aSSeigo Tanimura 	mtx_destroy(&pgrp->pg_mtx);
6031ede983cSDag-Erling Smørgrav 	free(pgrp, M_PGRP);
604bc093719SEd Schouten 	sess_release(savesess);
605df8bae1dSRodney W. Grimes }
606df8bae1dSRodney W. Grimes 
60702e878d9SJohn Baldwin static void
60802e878d9SJohn Baldwin pgadjustjobc(pgrp, entering)
60902e878d9SJohn Baldwin 	struct pgrp *pgrp;
61002e878d9SJohn Baldwin 	int entering;
61102e878d9SJohn Baldwin {
61202e878d9SJohn Baldwin 
61302e878d9SJohn Baldwin 	PGRP_LOCK(pgrp);
61402e878d9SJohn Baldwin 	if (entering)
61502e878d9SJohn Baldwin 		pgrp->pg_jobc++;
61602e878d9SJohn Baldwin 	else {
61702e878d9SJohn Baldwin 		--pgrp->pg_jobc;
61802e878d9SJohn Baldwin 		if (pgrp->pg_jobc == 0)
61902e878d9SJohn Baldwin 			orphanpg(pgrp);
62002e878d9SJohn Baldwin 	}
62102e878d9SJohn Baldwin 	PGRP_UNLOCK(pgrp);
62202e878d9SJohn Baldwin }
62302e878d9SJohn Baldwin 
624df8bae1dSRodney W. Grimes /*
625df8bae1dSRodney W. Grimes  * Adjust pgrp jobc counters when specified process changes process group.
626df8bae1dSRodney W. Grimes  * We count the number of processes in each process group that "qualify"
627df8bae1dSRodney W. Grimes  * the group for terminal job control (those with a parent in a different
628df8bae1dSRodney W. Grimes  * process group of the same session).  If that count reaches zero, the
629df8bae1dSRodney W. Grimes  * process group becomes orphaned.  Check both the specified process'
630df8bae1dSRodney W. Grimes  * process group and that of its children.
631df8bae1dSRodney W. Grimes  * entering == 0 => p is leaving specified group.
632df8bae1dSRodney W. Grimes  * entering == 1 => p is entering specified group.
633df8bae1dSRodney W. Grimes  */
63426f9a767SRodney W. Grimes void
635df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering)
636df8bae1dSRodney W. Grimes 	register struct proc *p;
637df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
638df8bae1dSRodney W. Grimes 	int entering;
639df8bae1dSRodney W. Grimes {
640df8bae1dSRodney W. Grimes 	register struct pgrp *hispgrp;
641f591779bSSeigo Tanimura 	register struct session *mysession;
642f591779bSSeigo Tanimura 
643f089b570SJohn Baldwin 	sx_assert(&proctree_lock, SX_LOCKED);
644f591779bSSeigo Tanimura 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
645f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
646f591779bSSeigo Tanimura 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes 	/*
649df8bae1dSRodney W. Grimes 	 * Check p's parent to see whether p qualifies its own process
650df8bae1dSRodney W. Grimes 	 * group; if so, adjust count for p's process group.
651df8bae1dSRodney W. Grimes 	 */
652f591779bSSeigo Tanimura 	mysession = pgrp->pg_session;
653df8bae1dSRodney W. Grimes 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
65402e878d9SJohn Baldwin 	    hispgrp->pg_session == mysession)
65502e878d9SJohn Baldwin 		pgadjustjobc(pgrp, entering);
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	/*
658df8bae1dSRodney W. Grimes 	 * Check this process' children to see whether they qualify
659df8bae1dSRodney W. Grimes 	 * their process groups; if so, adjust counts for children's
660df8bae1dSRodney W. Grimes 	 * process groups.
661df8bae1dSRodney W. Grimes 	 */
662f591779bSSeigo Tanimura 	LIST_FOREACH(p, &p->p_children, p_sibling) {
66302e878d9SJohn Baldwin 		hispgrp = p->p_pgrp;
66402e878d9SJohn Baldwin 		if (hispgrp == pgrp ||
66502e878d9SJohn Baldwin 		    hispgrp->pg_session != mysession)
66602e878d9SJohn Baldwin 			continue;
66702e878d9SJohn Baldwin 		PROC_LOCK(p);
66802e878d9SJohn Baldwin 		if (p->p_state == PRS_ZOMBIE) {
66902e878d9SJohn Baldwin 			PROC_UNLOCK(p);
67002e878d9SJohn Baldwin 			continue;
671df8bae1dSRodney W. Grimes 		}
67202e878d9SJohn Baldwin 		PROC_UNLOCK(p);
67302e878d9SJohn Baldwin 		pgadjustjobc(hispgrp, entering);
674f591779bSSeigo Tanimura 	}
675dfd5dee1SPeter Wemm }
676df8bae1dSRodney W. Grimes 
677df8bae1dSRodney W. Grimes /*
678df8bae1dSRodney W. Grimes  * A process group has become orphaned;
679df8bae1dSRodney W. Grimes  * if there are any stopped processes in the group,
680df8bae1dSRodney W. Grimes  * hang-up all process in that group.
681df8bae1dSRodney W. Grimes  */
682df8bae1dSRodney W. Grimes static void
683df8bae1dSRodney W. Grimes orphanpg(pg)
684df8bae1dSRodney W. Grimes 	struct pgrp *pg;
685df8bae1dSRodney W. Grimes {
686df8bae1dSRodney W. Grimes 	register struct proc *p;
687df8bae1dSRodney W. Grimes 
688f591779bSSeigo Tanimura 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
689f591779bSSeigo Tanimura 
6901b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
69102e878d9SJohn Baldwin 		PROC_LOCK(p);
692e602ba25SJulian Elischer 		if (P_SHOULDSTOP(p)) {
69302e878d9SJohn Baldwin 			PROC_UNLOCK(p);
6941b727751SPoul-Henning Kamp 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
69515e9ec51SJohn Baldwin 				PROC_LOCK(p);
6968451d0ddSKip Macy 				kern_psignal(p, SIGHUP);
6978451d0ddSKip Macy 				kern_psignal(p, SIGCONT);
69815e9ec51SJohn Baldwin 				PROC_UNLOCK(p);
699df8bae1dSRodney W. Grimes 			}
700df8bae1dSRodney W. Grimes 			return;
701df8bae1dSRodney W. Grimes 		}
70202e878d9SJohn Baldwin 		PROC_UNLOCK(p);
703df8bae1dSRodney W. Grimes 	}
704df8bae1dSRodney W. Grimes }
705df8bae1dSRodney W. Grimes 
706572b4402SPoul-Henning Kamp void
707bc093719SEd Schouten sess_hold(struct session *s)
708572b4402SPoul-Henning Kamp {
709572b4402SPoul-Henning Kamp 
710bc093719SEd Schouten 	refcount_acquire(&s->s_count);
711bc093719SEd Schouten }
712bc093719SEd Schouten 
713bc093719SEd Schouten void
714bc093719SEd Schouten sess_release(struct session *s)
715bc093719SEd Schouten {
716bc093719SEd Schouten 
717bc093719SEd Schouten 	if (refcount_release(&s->s_count)) {
718bc093719SEd Schouten 		if (s->s_ttyp != NULL) {
719bc093719SEd Schouten 			tty_lock(s->s_ttyp);
720bc093719SEd Schouten 			tty_rel_sess(s->s_ttyp, s);
721bc093719SEd Schouten 		}
722572b4402SPoul-Henning Kamp 		mtx_destroy(&s->s_mtx);
7231ede983cSDag-Erling Smørgrav 		free(s, M_SESSION);
724572b4402SPoul-Henning Kamp 	}
725572b4402SPoul-Henning Kamp }
726572b4402SPoul-Henning Kamp 
727831031ceSBruce Evans #include "opt_ddb.h"
728831031ceSBruce Evans #ifdef DDB
729831031ceSBruce Evans #include <ddb/ddb.h>
730831031ceSBruce Evans 
731831031ceSBruce Evans DB_SHOW_COMMAND(pgrpdump, pgrpdump)
732df8bae1dSRodney W. Grimes {
733df8bae1dSRodney W. Grimes 	register struct pgrp *pgrp;
734df8bae1dSRodney W. Grimes 	register struct proc *p;
735876a94eeSBruce Evans 	register int i;
736df8bae1dSRodney W. Grimes 
737b75356e1SJeffrey Hsu 	for (i = 0; i <= pgrphash; i++) {
7381b727751SPoul-Henning Kamp 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
739df8bae1dSRodney W. Grimes 			printf("\tindx %d\n", i);
7401b727751SPoul-Henning Kamp 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
741ac1e407bSBruce Evans 				printf(
742ac1e407bSBruce Evans 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
743ac1e407bSBruce Evans 				    (void *)pgrp, (long)pgrp->pg_id,
744ac1e407bSBruce Evans 				    (void *)pgrp->pg_session,
745b75356e1SJeffrey Hsu 				    pgrp->pg_session->s_count,
7461b727751SPoul-Henning Kamp 				    (void *)LIST_FIRST(&pgrp->pg_members));
7471b727751SPoul-Henning Kamp 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
748ac1e407bSBruce Evans 					printf("\t\tpid %ld addr %p pgrp %p\n",
749ac1e407bSBruce Evans 					    (long)p->p_pid, (void *)p,
750ac1e407bSBruce Evans 					    (void *)p->p_pgrp);
751df8bae1dSRodney W. Grimes 				}
752df8bae1dSRodney W. Grimes 			}
753df8bae1dSRodney W. Grimes 		}
754df8bae1dSRodney W. Grimes 	}
755df8bae1dSRodney W. Grimes }
756831031ceSBruce Evans #endif /* DDB */
757972f9b20SPoul-Henning Kamp 
758972f9b20SPoul-Henning Kamp /*
759f8d90480SAttilio Rao  * Calculate the kinfo_proc members which contain process-wide
760f8d90480SAttilio Rao  * informations.
761f8d90480SAttilio Rao  * Must be called with the target process locked.
762f8d90480SAttilio Rao  */
763f8d90480SAttilio Rao static void
764f8d90480SAttilio Rao fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
765f8d90480SAttilio Rao {
766f8d90480SAttilio Rao 	struct thread *td;
767f8d90480SAttilio Rao 
768f8d90480SAttilio Rao 	PROC_LOCK_ASSERT(p, MA_OWNED);
769f8d90480SAttilio Rao 
770f8d90480SAttilio Rao 	kp->ki_estcpu = 0;
771f8d90480SAttilio Rao 	kp->ki_pctcpu = 0;
772f8d90480SAttilio Rao 	FOREACH_THREAD_IN_PROC(p, td) {
773f8d90480SAttilio Rao 		thread_lock(td);
774f8d90480SAttilio Rao 		kp->ki_pctcpu += sched_pctcpu(td);
775f8d90480SAttilio Rao 		kp->ki_estcpu += td->td_estcpu;
776f8d90480SAttilio Rao 		thread_unlock(td);
777f8d90480SAttilio Rao 	}
778f8d90480SAttilio Rao }
779f8d90480SAttilio Rao 
780f8d90480SAttilio Rao /*
7815032ff81SDon Lewis  * Clear kinfo_proc and fill in any information that is common
7825032ff81SDon Lewis  * to all threads in the process.
78365c9b430SJohn Baldwin  * Must be called with the target process locked.
784972f9b20SPoul-Henning Kamp  */
7855032ff81SDon Lewis static void
7865032ff81SDon Lewis fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
787972f9b20SPoul-Henning Kamp {
7884093529dSJeff Roberson 	struct thread *td0;
7891f7d2501SKirk McKusick 	struct tty *tp;
7901f7d2501SKirk McKusick 	struct session *sp;
791d079d0a0SPawel Jakub Dawidek 	struct ucred *cred;
79290af4afaSJohn Baldwin 	struct sigacts *ps;
793972f9b20SPoul-Henning Kamp 
794374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
7951f7d2501SKirk McKusick 	bzero(kp, sizeof(*kp));
796972f9b20SPoul-Henning Kamp 
7971f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(*kp);
7981f7d2501SKirk McKusick 	kp->ki_paddr = p;
7996617724cSJeff Roberson 	kp->ki_addr =/* p->p_addr; */0; /* XXX */
8001f7d2501SKirk McKusick 	kp->ki_args = p->p_args;
8011f7d2501SKirk McKusick 	kp->ki_textvp = p->p_textvp;
8026c84de02SJohn Baldwin #ifdef KTRACE
803a5881ea5SJohn Baldwin 	kp->ki_tracep = p->p_tracevp;
8046c84de02SJohn Baldwin 	kp->ki_traceflag = p->p_traceflag;
8056c84de02SJohn Baldwin #endif
8061f7d2501SKirk McKusick 	kp->ki_fd = p->p_fd;
8071f7d2501SKirk McKusick 	kp->ki_vmspace = p->p_vmspace;
808cefcecbeSPawel Jakub Dawidek 	kp->ki_flag = p->p_flag;
809d079d0a0SPawel Jakub Dawidek 	cred = p->p_ucred;
810d079d0a0SPawel Jakub Dawidek 	if (cred) {
811d079d0a0SPawel Jakub Dawidek 		kp->ki_uid = cred->cr_uid;
812d079d0a0SPawel Jakub Dawidek 		kp->ki_ruid = cred->cr_ruid;
813d079d0a0SPawel Jakub Dawidek 		kp->ki_svuid = cred->cr_svuid;
81496fcc75fSRobert Watson 		kp->ki_cr_flags = 0;
81596fcc75fSRobert Watson 		if (cred->cr_flags & CRED_FLAG_CAPMODE)
81696fcc75fSRobert Watson 			kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
8171b5768beSBrooks Davis 		/* XXX bde doesn't like KI_NGROUPS */
8181b5768beSBrooks Davis 		if (cred->cr_ngroups > KI_NGROUPS) {
8191b5768beSBrooks Davis 			kp->ki_ngroups = KI_NGROUPS;
8201b5768beSBrooks Davis 			kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
8211b5768beSBrooks Davis 		} else
822838d9858SBrooks Davis 			kp->ki_ngroups = cred->cr_ngroups;
8231b5768beSBrooks Davis 		bcopy(cred->cr_groups, kp->ki_groups,
8241b5768beSBrooks Davis 		    kp->ki_ngroups * sizeof(gid_t));
825d079d0a0SPawel Jakub Dawidek 		kp->ki_rgid = cred->cr_rgid;
826d079d0a0SPawel Jakub Dawidek 		kp->ki_svgid = cred->cr_svgid;
827cefcecbeSPawel Jakub Dawidek 		/* If jailed(cred), emulate the old P_JAILED flag. */
828c78941e6SPawel Jakub Dawidek 		if (jailed(cred)) {
829cefcecbeSPawel Jakub Dawidek 			kp->ki_flag |= P_JAILED;
8300304c731SJamie Gritton 			/* If inside the jail, use 0 as a jail ID. */
8310304c731SJamie Gritton 			if (cred->cr_prison != curthread->td_ucred->cr_prison)
832c78941e6SPawel Jakub Dawidek 				kp->ki_jid = cred->cr_prison->pr_id;
833c78941e6SPawel Jakub Dawidek 		}
8347123f4cdSEdward Tomasz Napierala 		strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
8357123f4cdSEdward Tomasz Napierala 		    sizeof(kp->ki_loginclass));
836972f9b20SPoul-Henning Kamp 	}
83790af4afaSJohn Baldwin 	ps = p->p_sigacts;
838d079d0a0SPawel Jakub Dawidek 	if (ps) {
83990af4afaSJohn Baldwin 		mtx_lock(&ps->ps_mtx);
84090af4afaSJohn Baldwin 		kp->ki_sigignore = ps->ps_sigignore;
84190af4afaSJohn Baldwin 		kp->ki_sigcatch = ps->ps_sigcatch;
84290af4afaSJohn Baldwin 		mtx_unlock(&ps->ps_mtx);
843d8c85307SJulian Elischer 	}
844e602ba25SJulian Elischer 	if (p->p_state != PRS_NEW &&
845e602ba25SJulian Elischer 	    p->p_state != PRS_ZOMBIE &&
846e602ba25SJulian Elischer 	    p->p_vmspace != NULL) {
8471f7d2501SKirk McKusick 		struct vmspace *vm = p->p_vmspace;
848cd73303cSDavid Greenman 
8491f7d2501SKirk McKusick 		kp->ki_size = vm->vm_map.size;
8501f7d2501SKirk McKusick 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
851ceff7f2aSTim J. Robbins 		FOREACH_THREAD_IN_PROC(p, td0) {
852ceff7f2aSTim J. Robbins 			if (!TD_IS_SWAPPED(td0))
853ceff7f2aSTim J. Robbins 				kp->ki_rssize += td0->td_kstack_pages;
854ceff7f2aSTim J. Robbins 		}
8551f7d2501SKirk McKusick 		kp->ki_swrss = vm->vm_swrss;
8561f7d2501SKirk McKusick 		kp->ki_tsize = vm->vm_tsize;
8571f7d2501SKirk McKusick 		kp->ki_dsize = vm->vm_dsize;
8581f7d2501SKirk McKusick 		kp->ki_ssize = vm->vm_ssize;
8595032ff81SDon Lewis 	} else if (p->p_state == PRS_ZOMBIE)
8605032ff81SDon Lewis 		kp->ki_stat = SZOMB;
861b61ce5b0SJeff Roberson 	if (kp->ki_flag & P_INMEM)
862b61ce5b0SJeff Roberson 		kp->ki_sflag = PS_INMEM;
863b61ce5b0SJeff Roberson 	else
864b61ce5b0SJeff Roberson 		kp->ki_sflag = 0;
86554b0e65fSJeff Roberson 	/* Calculate legacy swtime as seconds since 'swtick'. */
86654b0e65fSJeff Roberson 	kp->ki_swtime = (ticks - p->p_swtick) / hz;
867cebabef0SPawel Jakub Dawidek 	kp->ki_pid = p->p_pid;
868cebabef0SPawel Jakub Dawidek 	kp->ki_nice = p->p_nice;
86978c85e8dSJohn Baldwin 	kp->ki_start = p->p_stats->p_start;
87078c85e8dSJohn Baldwin 	timevaladd(&kp->ki_start, &boottime);
871a1fe14bcSAttilio Rao 	PROC_SLOCK(p);
8728e6fa660SJohn Baldwin 	rufetch(p, &kp->ki_rusage);
8738e6fa660SJohn Baldwin 	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
87478c85e8dSJohn Baldwin 	calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
875a1fe14bcSAttilio Rao 	PROC_SUNLOCK(p);
87678c85e8dSJohn Baldwin 	calccru(p, &kp->ki_childutime, &kp->ki_childstime);
877a37e14e1SEdward Tomasz Napierala 	/* Some callers want child times in a single value. */
87878c85e8dSJohn Baldwin 	kp->ki_childtime = kp->ki_childstime;
87978c85e8dSJohn Baldwin 	timevaladd(&kp->ki_childtime, &kp->ki_childutime);
880a37e14e1SEdward Tomasz Napierala 
881*4d34e019SKonstantin Belousov 	FOREACH_THREAD_IN_PROC(p, td0)
882*4d34e019SKonstantin Belousov 		kp->ki_cow += td0->td_cow;
883*4d34e019SKonstantin Belousov 
884f591779bSSeigo Tanimura 	tp = NULL;
8851f7d2501SKirk McKusick 	if (p->p_pgrp) {
8861f7d2501SKirk McKusick 		kp->ki_pgid = p->p_pgrp->pg_id;
8871f7d2501SKirk McKusick 		kp->ki_jobc = p->p_pgrp->pg_jobc;
8881f7d2501SKirk McKusick 		sp = p->p_pgrp->pg_session;
8891f7d2501SKirk McKusick 
8901f7d2501SKirk McKusick 		if (sp != NULL) {
8911f7d2501SKirk McKusick 			kp->ki_sid = sp->s_sid;
892f591779bSSeigo Tanimura 			SESS_LOCK(sp);
893e80fb434SRobert Drehmel 			strlcpy(kp->ki_login, sp->s_login,
894e80fb434SRobert Drehmel 			    sizeof(kp->ki_login));
8951f7d2501SKirk McKusick 			if (sp->s_ttyvp)
896b8e6bf1eSJohn Baldwin 				kp->ki_kiflag |= KI_CTTY;
8971f7d2501SKirk McKusick 			if (SESS_LEADER(p))
8981f7d2501SKirk McKusick 				kp->ki_kiflag |= KI_SLEADER;
899bc093719SEd Schouten 			/* XXX proctree_lock */
900f591779bSSeigo Tanimura 			tp = sp->s_ttyp;
901f591779bSSeigo Tanimura 			SESS_UNLOCK(sp);
902cd73303cSDavid Greenman 		}
903cd73303cSDavid Greenman 	}
904f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
905bc093719SEd Schouten 		kp->ki_tdev = tty_udev(tp);
9061f7d2501SKirk McKusick 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
9071f7d2501SKirk McKusick 		if (tp->t_session)
9081f7d2501SKirk McKusick 			kp->ki_tsid = tp->t_session->s_sid;
909972f9b20SPoul-Henning Kamp 	} else
910f3732fd1SPoul-Henning Kamp 		kp->ki_tdev = NODEV;
91113b762a3SEd Maste 	if (p->p_comm[0] != '\0')
912e80fb434SRobert Drehmel 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
913078842c5SGarance A Drosehn 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
914078842c5SGarance A Drosehn 	    p->p_sysent->sv_name[0] != '\0')
915078842c5SGarance A Drosehn 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
9161d9c5696SJuli Mallett 	kp->ki_siglist = p->p_siglist;
9171f7d2501SKirk McKusick 	kp->ki_xstat = p->p_xstat;
9181f7d2501SKirk McKusick 	kp->ki_acflag = p->p_acflag;
9191f7d2501SKirk McKusick 	kp->ki_lock = p->p_lock;
92042a4ed99SJohn Baldwin 	if (p->p_pptr)
92142a4ed99SJohn Baldwin 		kp->ki_ppid = p->p_pptr->p_pid;
922972f9b20SPoul-Henning Kamp }
923972f9b20SPoul-Henning Kamp 
9245032ff81SDon Lewis /*
92500305546SKonstantin Belousov  * Fill in information that is thread specific.  Must be called with
92600305546SKonstantin Belousov  * target process locked.  If 'preferthread' is set, overwrite certain
92700305546SKonstantin Belousov  * process-related fields that are maintained for both threads and
92800305546SKonstantin Belousov  * processes.
9295032ff81SDon Lewis  */
9305032ff81SDon Lewis static void
931d92909c1SRobert Watson fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
9325032ff81SDon Lewis {
9335032ff81SDon Lewis 	struct proc *p;
9345032ff81SDon Lewis 
9355032ff81SDon Lewis 	p = td->td_proc;
9366239ef1dSEd Maste 	kp->ki_tdaddr = td;
937374ae2a3SJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
9385032ff81SDon Lewis 
9392417d97eSJohn Baldwin 	if (preferthread)
9402417d97eSJohn Baldwin 		PROC_SLOCK(p);
941982d11f8SJeff Roberson 	thread_lock(td);
9425032ff81SDon Lewis 	if (td->td_wmesg != NULL)
9435032ff81SDon Lewis 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
9445032ff81SDon Lewis 	else
9455032ff81SDon Lewis 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
946925af544SBjoern A. Zeeb 	strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
9475032ff81SDon Lewis 	if (TD_ON_LOCK(td)) {
9485032ff81SDon Lewis 		kp->ki_kiflag |= KI_LOCKBLOCK;
9495032ff81SDon Lewis 		strlcpy(kp->ki_lockname, td->td_lockname,
9505032ff81SDon Lewis 		    sizeof(kp->ki_lockname));
9515032ff81SDon Lewis 	} else {
9525032ff81SDon Lewis 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
9535032ff81SDon Lewis 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
9545032ff81SDon Lewis 	}
9555032ff81SDon Lewis 
9566617724cSJeff Roberson 	if (p->p_state == PRS_NORMAL) { /* approximate. */
9575032ff81SDon Lewis 		if (TD_ON_RUNQ(td) ||
9585032ff81SDon Lewis 		    TD_CAN_RUN(td) ||
9595032ff81SDon Lewis 		    TD_IS_RUNNING(td)) {
9605032ff81SDon Lewis 			kp->ki_stat = SRUN;
9615032ff81SDon Lewis 		} else if (P_SHOULDSTOP(p)) {
9625032ff81SDon Lewis 			kp->ki_stat = SSTOP;
9635032ff81SDon Lewis 		} else if (TD_IS_SLEEPING(td)) {
9645032ff81SDon Lewis 			kp->ki_stat = SSLEEP;
9655032ff81SDon Lewis 		} else if (TD_ON_LOCK(td)) {
9665032ff81SDon Lewis 			kp->ki_stat = SLOCK;
9675032ff81SDon Lewis 		} else {
9685032ff81SDon Lewis 			kp->ki_stat = SWAIT;
9695032ff81SDon Lewis 		}
9703357835aSDavid Xu 	} else if (p->p_state == PRS_ZOMBIE) {
9713357835aSDavid Xu 		kp->ki_stat = SZOMB;
9725032ff81SDon Lewis 	} else {
9735032ff81SDon Lewis 		kp->ki_stat = SIDL;
9745032ff81SDon Lewis 	}
9755032ff81SDon Lewis 
9765032ff81SDon Lewis 	/* Things in the thread */
9775032ff81SDon Lewis 	kp->ki_wchan = td->td_wchan;
9785032ff81SDon Lewis 	kp->ki_pri.pri_level = td->td_priority;
9795032ff81SDon Lewis 	kp->ki_pri.pri_native = td->td_base_pri;
9805032ff81SDon Lewis 	kp->ki_lastcpu = td->td_lastcpu;
9815032ff81SDon Lewis 	kp->ki_oncpu = td->td_oncpu;
9825032ff81SDon Lewis 	kp->ki_tdflags = td->td_flags;
9835032ff81SDon Lewis 	kp->ki_tid = td->td_tid;
9845032ff81SDon Lewis 	kp->ki_numthreads = p->p_numthreads;
9855032ff81SDon Lewis 	kp->ki_pcb = td->td_pcb;
9865032ff81SDon Lewis 	kp->ki_kstack = (void *)td->td_kstack;
98754b0e65fSJeff Roberson 	kp->ki_slptime = (ticks - td->td_slptick) / hz;
9888460a577SJohn Birrell 	kp->ki_pri.pri_class = td->td_pri_class;
9898460a577SJohn Birrell 	kp->ki_pri.pri_user = td->td_user_pri;
9905032ff81SDon Lewis 
991f8d90480SAttilio Rao 	if (preferthread) {
9922417d97eSJohn Baldwin 		rufetchtd(td, &kp->ki_rusage);
993213c077fSKonstantin Belousov 		kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
994f8d90480SAttilio Rao 		kp->ki_pctcpu = sched_pctcpu(td);
995f8d90480SAttilio Rao 		kp->ki_estcpu = td->td_estcpu;
996*4d34e019SKonstantin Belousov 		kp->ki_cow = td->td_cow;
997f8d90480SAttilio Rao 	}
998d92909c1SRobert Watson 
9995032ff81SDon Lewis 	/* We can't get this anymore but ps etc never used it anyway. */
10005032ff81SDon Lewis 	kp->ki_rqindex = 0;
10015032ff81SDon Lewis 
100200305546SKonstantin Belousov 	if (preferthread)
100300305546SKonstantin Belousov 		kp->ki_siglist = td->td_siglist;
10045032ff81SDon Lewis 	kp->ki_sigmask = td->td_sigmask;
1005982d11f8SJeff Roberson 	thread_unlock(td);
10062417d97eSJohn Baldwin 	if (preferthread)
10072417d97eSJohn Baldwin 		PROC_SUNLOCK(p);
10085032ff81SDon Lewis }
10095032ff81SDon Lewis 
10105032ff81SDon Lewis /*
10115032ff81SDon Lewis  * Fill in a kinfo_proc structure for the specified process.
10125032ff81SDon Lewis  * Must be called with the target process locked.
10135032ff81SDon Lewis  */
10145032ff81SDon Lewis void
10155032ff81SDon Lewis fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
10165032ff81SDon Lewis {
10175032ff81SDon Lewis 
1018f8d90480SAttilio Rao 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1019f8d90480SAttilio Rao 
10205032ff81SDon Lewis 	fill_kinfo_proc_only(p, kp);
1021d92909c1SRobert Watson 	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1022f8d90480SAttilio Rao 	fill_kinfo_aggregate(p, kp);
10235032ff81SDon Lewis }
10245032ff81SDon Lewis 
10258b059651SDavid Schultz struct pstats *
10268b059651SDavid Schultz pstats_alloc(void)
10278b059651SDavid Schultz {
10288b059651SDavid Schultz 
10298b059651SDavid Schultz 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
10308b059651SDavid Schultz }
10318b059651SDavid Schultz 
10328b059651SDavid Schultz /*
10338b059651SDavid Schultz  * Copy parts of p_stats; zero the rest of p_stats (statistics).
10348b059651SDavid Schultz  */
10358b059651SDavid Schultz void
10368b059651SDavid Schultz pstats_fork(struct pstats *src, struct pstats *dst)
10378b059651SDavid Schultz {
10388b059651SDavid Schultz 
10398b059651SDavid Schultz 	bzero(&dst->pstat_startzero,
10406db36923SDavid Schultz 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
10418b059651SDavid Schultz 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
10426db36923SDavid Schultz 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
10438b059651SDavid Schultz }
10448b059651SDavid Schultz 
10458b059651SDavid Schultz void
10468b059651SDavid Schultz pstats_free(struct pstats *ps)
10478b059651SDavid Schultz {
10488b059651SDavid Schultz 
10498b059651SDavid Schultz 	free(ps, M_SUBPROC);
10508b059651SDavid Schultz }
10518b059651SDavid Schultz 
10528b059651SDavid Schultz /*
105342a4ed99SJohn Baldwin  * Locate a zombie process by number
105442a4ed99SJohn Baldwin  */
105542a4ed99SJohn Baldwin struct proc *
10563ce93e4eSPoul-Henning Kamp zpfind(pid_t pid)
10573ce93e4eSPoul-Henning Kamp {
10583ce93e4eSPoul-Henning Kamp 	struct proc *p;
10593ce93e4eSPoul-Henning Kamp 
10601005a129SJohn Baldwin 	sx_slock(&allproc_lock);
10611b727751SPoul-Henning Kamp 	LIST_FOREACH(p, &zombproc, p_list)
106233a9ed9dSJohn Baldwin 		if (p->p_pid == pid) {
106333a9ed9dSJohn Baldwin 			PROC_LOCK(p);
1064c0c25570SJake Burkholder 			break;
106533a9ed9dSJohn Baldwin 		}
10661005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
10673ce93e4eSPoul-Henning Kamp 	return (p);
10683ce93e4eSPoul-Henning Kamp }
10693ce93e4eSPoul-Henning Kamp 
107030c6f34eSScott Long #define KERN_PROC_ZOMBMASK	0x3
107130c6f34eSScott Long #define KERN_PROC_NOTHREADS	0x4
10723ce93e4eSPoul-Henning Kamp 
107305e06d11SKonstantin Belousov #ifdef COMPAT_FREEBSD32
107405e06d11SKonstantin Belousov 
107505e06d11SKonstantin Belousov /*
107605e06d11SKonstantin Belousov  * This function is typically used to copy out the kernel address, so
107705e06d11SKonstantin Belousov  * it can be replaced by assignment of zero.
107805e06d11SKonstantin Belousov  */
107905e06d11SKonstantin Belousov static inline uint32_t
108005e06d11SKonstantin Belousov ptr32_trim(void *ptr)
108105e06d11SKonstantin Belousov {
108205e06d11SKonstantin Belousov 	uintptr_t uptr;
108305e06d11SKonstantin Belousov 
108405e06d11SKonstantin Belousov 	uptr = (uintptr_t)ptr;
108505e06d11SKonstantin Belousov 	return ((uptr > UINT_MAX) ? 0 : uptr);
108605e06d11SKonstantin Belousov }
108705e06d11SKonstantin Belousov 
108805e06d11SKonstantin Belousov #define PTRTRIM_CP(src,dst,fld) \
108905e06d11SKonstantin Belousov 	do { (dst).fld = ptr32_trim((src).fld); } while (0)
109005e06d11SKonstantin Belousov 
109105e06d11SKonstantin Belousov static void
109205e06d11SKonstantin Belousov freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
109305e06d11SKonstantin Belousov {
109405e06d11SKonstantin Belousov 	int i;
109505e06d11SKonstantin Belousov 
109605e06d11SKonstantin Belousov 	bzero(ki32, sizeof(struct kinfo_proc32));
109705e06d11SKonstantin Belousov 	ki32->ki_structsize = sizeof(struct kinfo_proc32);
109805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_layout);
109905e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_args);
110005e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_paddr);
110105e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_addr);
110205e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_tracep);
110305e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_textvp);
110405e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_fd);
110505e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_vmspace);
110605e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_wchan);
110705e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_pid);
110805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_ppid);
110905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_pgid);
111005e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_tpgid);
111105e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_sid);
111205e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_tsid);
111305e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_jobc);
111405e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_tdev);
111505e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_siglist);
111605e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_sigmask);
111705e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_sigignore);
111805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_sigcatch);
111905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_uid);
112005e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_ruid);
112105e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_svuid);
112205e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_rgid);
112305e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_svgid);
112405e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_ngroups);
112505e06d11SKonstantin Belousov 	for (i = 0; i < KI_NGROUPS; i++)
112605e06d11SKonstantin Belousov 		CP(*ki, *ki32, ki_groups[i]);
112705e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_size);
112805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_rssize);
112905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_swrss);
113005e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_tsize);
113105e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_dsize);
113205e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_ssize);
113305e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_xstat);
113405e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_acflag);
113505e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_pctcpu);
113605e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_estcpu);
113705e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_slptime);
113805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_swtime);
113905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_runtime);
114005e06d11SKonstantin Belousov 	TV_CP(*ki, *ki32, ki_start);
114105e06d11SKonstantin Belousov 	TV_CP(*ki, *ki32, ki_childtime);
114205e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_flag);
114305e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_kiflag);
114405e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_traceflag);
114505e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_stat);
114605e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_nice);
114705e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_lock);
114805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_rqindex);
114905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_oncpu);
115005e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_lastcpu);
1151925af544SBjoern A. Zeeb 	bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
115205e06d11SKonstantin Belousov 	bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
115305e06d11SKonstantin Belousov 	bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
115405e06d11SKonstantin Belousov 	bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
115505e06d11SKonstantin Belousov 	bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
115605e06d11SKonstantin Belousov 	bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
11577123f4cdSEdward Tomasz Napierala 	bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
115805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_cr_flags);
115905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_jid);
116005e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_numthreads);
116105e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_tid);
116205e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_pri);
116305e06d11SKonstantin Belousov 	freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
116405e06d11SKonstantin Belousov 	freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
116505e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_pcb);
116605e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_kstack);
116705e06d11SKonstantin Belousov 	PTRTRIM_CP(*ki, *ki32, ki_udata);
116805e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_sflag);
116905e06d11SKonstantin Belousov 	CP(*ki, *ki32, ki_tdflags);
117005e06d11SKonstantin Belousov }
117105e06d11SKonstantin Belousov 
117205e06d11SKonstantin Belousov static int
117305e06d11SKonstantin Belousov sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
117405e06d11SKonstantin Belousov {
117505e06d11SKonstantin Belousov 	struct kinfo_proc32 ki32;
117605e06d11SKonstantin Belousov 	int error;
117705e06d11SKonstantin Belousov 
117805e06d11SKonstantin Belousov 	if (req->flags & SCTL_MASK32) {
117905e06d11SKonstantin Belousov 		freebsd32_kinfo_proc_out(ki, &ki32);
1180e68d26fdSKonstantin Belousov 		error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
118105e06d11SKonstantin Belousov 	} else
1182e68d26fdSKonstantin Belousov 		error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
118305e06d11SKonstantin Belousov 	return (error);
118405e06d11SKonstantin Belousov }
118505e06d11SKonstantin Belousov #else
118605e06d11SKonstantin Belousov static int
118705e06d11SKonstantin Belousov sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
118805e06d11SKonstantin Belousov {
118905e06d11SKonstantin Belousov 
1190e68d26fdSKonstantin Belousov 	return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
119105e06d11SKonstantin Belousov }
119205e06d11SKonstantin Belousov #endif
119305e06d11SKonstantin Belousov 
119465c9b430SJohn Baldwin /*
119565c9b430SJohn Baldwin  * Must be called with the process locked and will return with it unlocked.
119665c9b430SJohn Baldwin  */
11973ce93e4eSPoul-Henning Kamp static int
119830c6f34eSScott Long sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
11993ce93e4eSPoul-Henning Kamp {
120030c6f34eSScott Long 	struct thread *td;
12011f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc;
120230c6f34eSScott Long 	int error = 0;
120333a9ed9dSJohn Baldwin 	struct proc *np;
12043ce93e4eSPoul-Henning Kamp 	pid_t pid = p->p_pid;
12053ce93e4eSPoul-Henning Kamp 
120665c9b430SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
1207f8d90480SAttilio Rao 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
120830c6f34eSScott Long 
1209f8d90480SAttilio Rao 	fill_kinfo_proc(p, &kinfo_proc);
1210f8d90480SAttilio Rao 	if (flags & KERN_PROC_NOTHREADS)
121105e06d11SKonstantin Belousov 		error = sysctl_out_proc_copyout(&kinfo_proc, req);
1212f8d90480SAttilio Rao 	else {
121330c6f34eSScott Long 		FOREACH_THREAD_IN_PROC(p, td) {
1214d92909c1SRobert Watson 			fill_kinfo_thread(td, &kinfo_proc, 1);
121505e06d11SKonstantin Belousov 			error = sysctl_out_proc_copyout(&kinfo_proc, req);
121630c6f34eSScott Long 			if (error)
121730c6f34eSScott Long 				break;
121830c6f34eSScott Long 		}
121930c6f34eSScott Long 	}
122030c6f34eSScott Long 	PROC_UNLOCK(p);
12213ce93e4eSPoul-Henning Kamp 	if (error)
12223ce93e4eSPoul-Henning Kamp 		return (error);
122330c6f34eSScott Long 	if (flags & KERN_PROC_ZOMBMASK)
122433a9ed9dSJohn Baldwin 		np = zpfind(pid);
122533a9ed9dSJohn Baldwin 	else {
122633a9ed9dSJohn Baldwin 		if (pid == 0)
122733a9ed9dSJohn Baldwin 			return (0);
122833a9ed9dSJohn Baldwin 		np = pfind(pid);
122933a9ed9dSJohn Baldwin 	}
123033a9ed9dSJohn Baldwin 	if (np == NULL)
1231f308bdddSKevin Lo 		return (ESRCH);
123233a9ed9dSJohn Baldwin 	if (np != p) {
123333a9ed9dSJohn Baldwin 		PROC_UNLOCK(np);
1234f308bdddSKevin Lo 		return (ESRCH);
123533a9ed9dSJohn Baldwin 	}
123633a9ed9dSJohn Baldwin 	PROC_UNLOCK(np);
12373ce93e4eSPoul-Henning Kamp 	return (0);
12383ce93e4eSPoul-Henning Kamp }
12393ce93e4eSPoul-Henning Kamp 
1240972f9b20SPoul-Henning Kamp static int
124182d9ae4eSPoul-Henning Kamp sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1242972f9b20SPoul-Henning Kamp {
1243972f9b20SPoul-Henning Kamp 	int *name = (int *)arg1;
1244972f9b20SPoul-Henning Kamp 	u_int namelen = arg2;
1245972f9b20SPoul-Henning Kamp 	struct proc *p;
12462648efa6SDaniel Eischen 	int flags, doingzomb, oid_number;
1247972f9b20SPoul-Henning Kamp 	int error = 0;
1248972f9b20SPoul-Henning Kamp 
12492648efa6SDaniel Eischen 	oid_number = oidp->oid_number;
12502648efa6SDaniel Eischen 	if (oid_number != KERN_PROC_ALL &&
12512648efa6SDaniel Eischen 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
12522648efa6SDaniel Eischen 		flags = KERN_PROC_NOTHREADS;
12532648efa6SDaniel Eischen 	else {
12542648efa6SDaniel Eischen 		flags = 0;
12552648efa6SDaniel Eischen 		oid_number &= ~KERN_PROC_INC_THREAD;
12562648efa6SDaniel Eischen 	}
12572648efa6SDaniel Eischen 	if (oid_number == KERN_PROC_PID) {
12583ce93e4eSPoul-Henning Kamp 		if (namelen != 1)
1259972f9b20SPoul-Henning Kamp 			return (EINVAL);
12605032ff81SDon Lewis 		error = sysctl_wire_old_buffer(req, 0);
12615032ff81SDon Lewis 		if (error)
12625032ff81SDon Lewis 			return (error);
1263fa3935bcSMikolaj Golub 		error = pget((pid_t)name[0], PGET_CANSEE, &p);
1264fa3935bcSMikolaj Golub 		if (error != 0)
1265e76bad96SRobert Drehmel 			return (error);
12662648efa6SDaniel Eischen 		error = sysctl_out_proc(p, req, flags);
12673ce93e4eSPoul-Henning Kamp 		return (error);
12683ce93e4eSPoul-Henning Kamp 	}
12693ddaef40STim J. Robbins 
12702648efa6SDaniel Eischen 	switch (oid_number) {
12713ddaef40STim J. Robbins 	case KERN_PROC_ALL:
12723ddaef40STim J. Robbins 		if (namelen != 0)
12733ce93e4eSPoul-Henning Kamp 			return (EINVAL);
12743ddaef40STim J. Robbins 		break;
127525e247afSPeter Wemm 	case KERN_PROC_PROC:
127625e247afSPeter Wemm 		if (namelen != 0 && namelen != 1)
127725e247afSPeter Wemm 			return (EINVAL);
127825e247afSPeter Wemm 		break;
12793ddaef40STim J. Robbins 	default:
12803ddaef40STim J. Robbins 		if (namelen != 1)
12813ddaef40STim J. Robbins 			return (EINVAL);
12823ddaef40STim J. Robbins 		break;
12833ddaef40STim J. Robbins 	}
12843ce93e4eSPoul-Henning Kamp 
1285972f9b20SPoul-Henning Kamp 	if (!req->oldptr) {
12863ce93e4eSPoul-Henning Kamp 		/* overestimate by 5 procs */
1287972f9b20SPoul-Henning Kamp 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1288972f9b20SPoul-Henning Kamp 		if (error)
1289972f9b20SPoul-Henning Kamp 			return (error);
1290972f9b20SPoul-Henning Kamp 	}
129147934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 0);
129247934cefSDon Lewis 	if (error != 0)
129347934cefSDon Lewis 		return (error);
12941005a129SJohn Baldwin 	sx_slock(&allproc_lock);
12953ce93e4eSPoul-Henning Kamp 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
12963ce93e4eSPoul-Henning Kamp 		if (!doingzomb)
12971b727751SPoul-Henning Kamp 			p = LIST_FIRST(&allproc);
12983ce93e4eSPoul-Henning Kamp 		else
12991b727751SPoul-Henning Kamp 			p = LIST_FIRST(&zombproc);
13001b727751SPoul-Henning Kamp 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
130102e878d9SJohn Baldwin 			/*
130202e878d9SJohn Baldwin 			 * Skip embryonic processes.
130302e878d9SJohn Baldwin 			 */
13048e6fa660SJohn Baldwin 			PROC_LOCK(p);
130502e878d9SJohn Baldwin 			if (p->p_state == PRS_NEW) {
13068e6fa660SJohn Baldwin 				PROC_UNLOCK(p);
130702e878d9SJohn Baldwin 				continue;
130802e878d9SJohn Baldwin 			}
1309b241b0a2SJuli Mallett 			KASSERT(p->p_ucred != NULL,
1310b241b0a2SJuli Mallett 			    ("process credential is NULL for non-NEW proc"));
1311972f9b20SPoul-Henning Kamp 			/*
1312387d2c03SRobert Watson 			 * Show a user only appropriate processes.
131303f808c5SPaul Saab 			 */
1314f44d9e24SJohn Baldwin 			if (p_cansee(curthread, p)) {
131565c9b430SJohn Baldwin 				PROC_UNLOCK(p);
131603f808c5SPaul Saab 				continue;
131765c9b430SJohn Baldwin 			}
131803f808c5SPaul Saab 			/*
1319972f9b20SPoul-Henning Kamp 			 * TODO - make more efficient (see notes below).
1320972f9b20SPoul-Henning Kamp 			 * do by session.
1321972f9b20SPoul-Henning Kamp 			 */
13222648efa6SDaniel Eischen 			switch (oid_number) {
1323972f9b20SPoul-Henning Kamp 
1324078842c5SGarance A Drosehn 			case KERN_PROC_GID:
1325b241b0a2SJuli Mallett 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1326078842c5SGarance A Drosehn 					PROC_UNLOCK(p);
1327078842c5SGarance A Drosehn 					continue;
1328078842c5SGarance A Drosehn 				}
1329078842c5SGarance A Drosehn 				break;
1330078842c5SGarance A Drosehn 
1331972f9b20SPoul-Henning Kamp 			case KERN_PROC_PGRP:
1332972f9b20SPoul-Henning Kamp 				/* could do this by traversing pgrp */
13333ce93e4eSPoul-Henning Kamp 				if (p->p_pgrp == NULL ||
1334f591779bSSeigo Tanimura 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1335f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1336972f9b20SPoul-Henning Kamp 					continue;
1337f591779bSSeigo Tanimura 				}
1338972f9b20SPoul-Henning Kamp 				break;
1339972f9b20SPoul-Henning Kamp 
1340b8fdc89dSGarance A Drosehn 			case KERN_PROC_RGID:
1341b241b0a2SJuli Mallett 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1342b8fdc89dSGarance A Drosehn 					PROC_UNLOCK(p);
1343b8fdc89dSGarance A Drosehn 					continue;
1344b8fdc89dSGarance A Drosehn 				}
1345b8fdc89dSGarance A Drosehn 				break;
1346b8fdc89dSGarance A Drosehn 
1347b8fdc89dSGarance A Drosehn 			case KERN_PROC_SESSION:
1348b8fdc89dSGarance A Drosehn 				if (p->p_session == NULL ||
1349b8fdc89dSGarance A Drosehn 				    p->p_session->s_sid != (pid_t)name[0]) {
1350b8fdc89dSGarance A Drosehn 					PROC_UNLOCK(p);
1351b8fdc89dSGarance A Drosehn 					continue;
1352b8fdc89dSGarance A Drosehn 				}
1353b8fdc89dSGarance A Drosehn 				break;
1354b8fdc89dSGarance A Drosehn 
1355972f9b20SPoul-Henning Kamp 			case KERN_PROC_TTY:
1356972f9b20SPoul-Henning Kamp 				if ((p->p_flag & P_CONTROLT) == 0 ||
1357f591779bSSeigo Tanimura 				    p->p_session == NULL) {
1358f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1359972f9b20SPoul-Henning Kamp 					continue;
1360f591779bSSeigo Tanimura 				}
1361bc093719SEd Schouten 				/* XXX proctree_lock */
1362f591779bSSeigo Tanimura 				SESS_LOCK(p->p_session);
1363f591779bSSeigo Tanimura 				if (p->p_session->s_ttyp == NULL ||
1364bc093719SEd Schouten 				    tty_udev(p->p_session->s_ttyp) !=
1365f3732fd1SPoul-Henning Kamp 				    (dev_t)name[0]) {
1366f591779bSSeigo Tanimura 					SESS_UNLOCK(p->p_session);
1367f591779bSSeigo Tanimura 					PROC_UNLOCK(p);
1368f591779bSSeigo Tanimura 					continue;
1369f591779bSSeigo Tanimura 				}
1370f591779bSSeigo Tanimura 				SESS_UNLOCK(p->p_session);
1371972f9b20SPoul-Henning Kamp 				break;
1372972f9b20SPoul-Henning Kamp 
1373972f9b20SPoul-Henning Kamp 			case KERN_PROC_UID:
1374b241b0a2SJuli Mallett 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
137565c9b430SJohn Baldwin 					PROC_UNLOCK(p);
1376972f9b20SPoul-Henning Kamp 					continue;
137765c9b430SJohn Baldwin 				}
1378972f9b20SPoul-Henning Kamp 				break;
1379972f9b20SPoul-Henning Kamp 
1380972f9b20SPoul-Henning Kamp 			case KERN_PROC_RUID:
1381b241b0a2SJuli Mallett 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
138265c9b430SJohn Baldwin 					PROC_UNLOCK(p);
1383972f9b20SPoul-Henning Kamp 					continue;
138465c9b430SJohn Baldwin 				}
1385972f9b20SPoul-Henning Kamp 				break;
138630c6f34eSScott Long 
138730c6f34eSScott Long 			case KERN_PROC_PROC:
138830c6f34eSScott Long 				break;
138930c6f34eSScott Long 
139030c6f34eSScott Long 			default:
139130c6f34eSScott Long 				break;
139230c6f34eSScott Long 
1393972f9b20SPoul-Henning Kamp 			}
1394972f9b20SPoul-Henning Kamp 
139530c6f34eSScott Long 			error = sysctl_out_proc(p, req, flags | doingzomb);
1396553629ebSJake Burkholder 			if (error) {
13971005a129SJohn Baldwin 				sx_sunlock(&allproc_lock);
1398972f9b20SPoul-Henning Kamp 				return (error);
1399972f9b20SPoul-Henning Kamp 			}
1400972f9b20SPoul-Henning Kamp 		}
1401553629ebSJake Burkholder 	}
14021005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
1403972f9b20SPoul-Henning Kamp 	return (0);
1404972f9b20SPoul-Henning Kamp }
1405972f9b20SPoul-Henning Kamp 
1406c1508b28SAlfred Perlstein struct pargs *
1407c1508b28SAlfred Perlstein pargs_alloc(int len)
1408c1508b28SAlfred Perlstein {
1409c1508b28SAlfred Perlstein 	struct pargs *pa;
1410c1508b28SAlfred Perlstein 
14111ede983cSDag-Erling Smørgrav 	pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1412a163d034SWarner Losh 		M_WAITOK);
141355b4a5aeSJohn Baldwin 	refcount_init(&pa->ar_ref, 1);
1414c1508b28SAlfred Perlstein 	pa->ar_length = len;
1415c1508b28SAlfred Perlstein 	return (pa);
1416c1508b28SAlfred Perlstein }
1417c1508b28SAlfred Perlstein 
141858e8af1bSKonstantin Belousov static void
1419c1508b28SAlfred Perlstein pargs_free(struct pargs *pa)
1420c1508b28SAlfred Perlstein {
1421c1508b28SAlfred Perlstein 
14221ede983cSDag-Erling Smørgrav 	free(pa, M_PARGS);
1423c1508b28SAlfred Perlstein }
1424c1508b28SAlfred Perlstein 
1425c1508b28SAlfred Perlstein void
1426c1508b28SAlfred Perlstein pargs_hold(struct pargs *pa)
1427c1508b28SAlfred Perlstein {
1428c1508b28SAlfred Perlstein 
1429c1508b28SAlfred Perlstein 	if (pa == NULL)
1430c1508b28SAlfred Perlstein 		return;
143155b4a5aeSJohn Baldwin 	refcount_acquire(&pa->ar_ref);
1432c1508b28SAlfred Perlstein }
1433c1508b28SAlfred Perlstein 
1434c1508b28SAlfred Perlstein void
1435c1508b28SAlfred Perlstein pargs_drop(struct pargs *pa)
1436c1508b28SAlfred Perlstein {
1437c1508b28SAlfred Perlstein 
1438c1508b28SAlfred Perlstein 	if (pa == NULL)
1439c1508b28SAlfred Perlstein 		return;
144055b4a5aeSJohn Baldwin 	if (refcount_release(&pa->ar_ref))
1441c1508b28SAlfred Perlstein 		pargs_free(pa);
1442c1508b28SAlfred Perlstein }
1443c1508b28SAlfred Perlstein 
1444c5cfcb1cSMikolaj Golub static int
1445c5cfcb1cSMikolaj Golub proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1446c5cfcb1cSMikolaj Golub     size_t len)
1447c5cfcb1cSMikolaj Golub {
1448c5cfcb1cSMikolaj Golub 	struct iovec iov;
1449c5cfcb1cSMikolaj Golub 	struct uio uio;
1450c5cfcb1cSMikolaj Golub 
1451c5cfcb1cSMikolaj Golub 	iov.iov_base = (caddr_t)buf;
1452c5cfcb1cSMikolaj Golub 	iov.iov_len = len;
1453c5cfcb1cSMikolaj Golub 	uio.uio_iov = &iov;
1454c5cfcb1cSMikolaj Golub 	uio.uio_iovcnt = 1;
1455c5cfcb1cSMikolaj Golub 	uio.uio_offset = offset;
1456c5cfcb1cSMikolaj Golub 	uio.uio_resid = (ssize_t)len;
1457c5cfcb1cSMikolaj Golub 	uio.uio_segflg = UIO_SYSSPACE;
1458c5cfcb1cSMikolaj Golub 	uio.uio_rw = UIO_READ;
1459c5cfcb1cSMikolaj Golub 	uio.uio_td = td;
1460c5cfcb1cSMikolaj Golub 
1461c5cfcb1cSMikolaj Golub 	return (proc_rwmem(p, &uio));
1462c5cfcb1cSMikolaj Golub }
1463c5cfcb1cSMikolaj Golub 
1464c5cfcb1cSMikolaj Golub static int
1465c5cfcb1cSMikolaj Golub proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1466c5cfcb1cSMikolaj Golub     size_t len)
1467c5cfcb1cSMikolaj Golub {
1468c5cfcb1cSMikolaj Golub 	size_t i;
1469c5cfcb1cSMikolaj Golub 	int error;
1470c5cfcb1cSMikolaj Golub 
1471c5cfcb1cSMikolaj Golub 	error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1472c5cfcb1cSMikolaj Golub 	/*
1473c5cfcb1cSMikolaj Golub 	 * Reading the chunk may validly return EFAULT if the string is shorter
1474c5cfcb1cSMikolaj Golub 	 * than the chunk and is aligned at the end of the page, assuming the
1475c5cfcb1cSMikolaj Golub 	 * next page is not mapped.  So if EFAULT is returned do a fallback to
1476c5cfcb1cSMikolaj Golub 	 * one byte read loop.
1477c5cfcb1cSMikolaj Golub 	 */
1478c5cfcb1cSMikolaj Golub 	if (error == EFAULT) {
1479c5cfcb1cSMikolaj Golub 		for (i = 0; i < len; i++, buf++, sptr++) {
1480c5cfcb1cSMikolaj Golub 			error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1481c5cfcb1cSMikolaj Golub 			if (error != 0)
1482c5cfcb1cSMikolaj Golub 				return (error);
1483c5cfcb1cSMikolaj Golub 			if (*buf == '\0')
1484c5cfcb1cSMikolaj Golub 				break;
1485c5cfcb1cSMikolaj Golub 		}
1486c5cfcb1cSMikolaj Golub 		error = 0;
1487c5cfcb1cSMikolaj Golub 	}
1488c5cfcb1cSMikolaj Golub 	return (error);
1489c5cfcb1cSMikolaj Golub }
1490c5cfcb1cSMikolaj Golub 
1491c5cfcb1cSMikolaj Golub #define PROC_AUXV_MAX	256	/* Safety limit on auxv size. */
1492c5cfcb1cSMikolaj Golub 
1493c5cfcb1cSMikolaj Golub enum proc_vector_type {
1494c5cfcb1cSMikolaj Golub 	PROC_ARG,
1495c5cfcb1cSMikolaj Golub 	PROC_ENV,
1496c5cfcb1cSMikolaj Golub 	PROC_AUX,
1497c5cfcb1cSMikolaj Golub };
1498c5cfcb1cSMikolaj Golub 
1499c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32
1500c5cfcb1cSMikolaj Golub static int
1501c5cfcb1cSMikolaj Golub get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1502c5cfcb1cSMikolaj Golub     size_t *vsizep, enum proc_vector_type type)
1503c5cfcb1cSMikolaj Golub {
1504c5cfcb1cSMikolaj Golub 	struct freebsd32_ps_strings pss;
1505c5cfcb1cSMikolaj Golub 	Elf32_Auxinfo aux;
1506c5cfcb1cSMikolaj Golub 	vm_offset_t vptr, ptr;
1507c5cfcb1cSMikolaj Golub 	uint32_t *proc_vector32;
1508c5cfcb1cSMikolaj Golub 	char **proc_vector;
1509c5cfcb1cSMikolaj Golub 	size_t vsize, size;
1510c5cfcb1cSMikolaj Golub 	int i, error;
1511c5cfcb1cSMikolaj Golub 
1512c5cfcb1cSMikolaj Golub 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1513c5cfcb1cSMikolaj Golub 	    &pss, sizeof(pss));
1514c5cfcb1cSMikolaj Golub 	if (error != 0)
1515c5cfcb1cSMikolaj Golub 		return (error);
1516c5cfcb1cSMikolaj Golub 	switch (type) {
1517c5cfcb1cSMikolaj Golub 	case PROC_ARG:
1518c5cfcb1cSMikolaj Golub 		vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1519c5cfcb1cSMikolaj Golub 		vsize = pss.ps_nargvstr;
1520c5cfcb1cSMikolaj Golub 		if (vsize > ARG_MAX)
1521c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1522c5cfcb1cSMikolaj Golub 		size = vsize * sizeof(int32_t);
1523c5cfcb1cSMikolaj Golub 		break;
1524c5cfcb1cSMikolaj Golub 	case PROC_ENV:
1525c5cfcb1cSMikolaj Golub 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1526c5cfcb1cSMikolaj Golub 		vsize = pss.ps_nenvstr;
1527c5cfcb1cSMikolaj Golub 		if (vsize > ARG_MAX)
1528c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1529c5cfcb1cSMikolaj Golub 		size = vsize * sizeof(int32_t);
1530c5cfcb1cSMikolaj Golub 		break;
1531c5cfcb1cSMikolaj Golub 	case PROC_AUX:
1532c5cfcb1cSMikolaj Golub 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1533c5cfcb1cSMikolaj Golub 		    (pss.ps_nenvstr + 1) * sizeof(int32_t);
1534c5cfcb1cSMikolaj Golub 		if (vptr % 4 != 0)
1535c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1536c5cfcb1cSMikolaj Golub 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1537c5cfcb1cSMikolaj Golub 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1538c5cfcb1cSMikolaj Golub 			if (error != 0)
1539c5cfcb1cSMikolaj Golub 				return (error);
1540c5cfcb1cSMikolaj Golub 			if (aux.a_type == AT_NULL)
1541c5cfcb1cSMikolaj Golub 				break;
1542c5cfcb1cSMikolaj Golub 			ptr += sizeof(aux);
1543c5cfcb1cSMikolaj Golub 		}
1544c5cfcb1cSMikolaj Golub 		if (aux.a_type != AT_NULL)
1545c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1546c5cfcb1cSMikolaj Golub 		vsize = i + 1;
1547c5cfcb1cSMikolaj Golub 		size = vsize * sizeof(aux);
1548c5cfcb1cSMikolaj Golub 		break;
1549c5cfcb1cSMikolaj Golub 	default:
1550c5cfcb1cSMikolaj Golub 		KASSERT(0, ("Wrong proc vector type: %d", type));
15517ad9baaeSMikolaj Golub 		return (EINVAL);
1552c5cfcb1cSMikolaj Golub 	}
1553c5cfcb1cSMikolaj Golub 	proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1554c5cfcb1cSMikolaj Golub 	error = proc_read_mem(td, p, vptr, proc_vector32, size);
1555c5cfcb1cSMikolaj Golub 	if (error != 0)
1556c5cfcb1cSMikolaj Golub 		goto done;
1557c5cfcb1cSMikolaj Golub 	if (type == PROC_AUX) {
1558c5cfcb1cSMikolaj Golub 		*proc_vectorp = (char **)proc_vector32;
1559c5cfcb1cSMikolaj Golub 		*vsizep = vsize;
1560c5cfcb1cSMikolaj Golub 		return (0);
1561c5cfcb1cSMikolaj Golub 	}
1562c5cfcb1cSMikolaj Golub 	proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1563c5cfcb1cSMikolaj Golub 	for (i = 0; i < (int)vsize; i++)
1564c5cfcb1cSMikolaj Golub 		proc_vector[i] = PTRIN(proc_vector32[i]);
1565c5cfcb1cSMikolaj Golub 	*proc_vectorp = proc_vector;
1566c5cfcb1cSMikolaj Golub 	*vsizep = vsize;
1567c5cfcb1cSMikolaj Golub done:
1568c5cfcb1cSMikolaj Golub 	free(proc_vector32, M_TEMP);
1569c5cfcb1cSMikolaj Golub 	return (error);
1570c5cfcb1cSMikolaj Golub }
1571c5cfcb1cSMikolaj Golub #endif
1572c5cfcb1cSMikolaj Golub 
1573c5cfcb1cSMikolaj Golub static int
1574c5cfcb1cSMikolaj Golub get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1575c5cfcb1cSMikolaj Golub     size_t *vsizep, enum proc_vector_type type)
1576c5cfcb1cSMikolaj Golub {
1577c5cfcb1cSMikolaj Golub 	struct ps_strings pss;
1578c5cfcb1cSMikolaj Golub 	Elf_Auxinfo aux;
1579c5cfcb1cSMikolaj Golub 	vm_offset_t vptr, ptr;
1580c5cfcb1cSMikolaj Golub 	char **proc_vector;
1581c5cfcb1cSMikolaj Golub 	size_t vsize, size;
1582c5cfcb1cSMikolaj Golub 	int error, i;
1583c5cfcb1cSMikolaj Golub 
1584c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32
1585c5cfcb1cSMikolaj Golub 	if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1586c5cfcb1cSMikolaj Golub 		return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1587c5cfcb1cSMikolaj Golub #endif
1588c5cfcb1cSMikolaj Golub 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1589c5cfcb1cSMikolaj Golub 	    &pss, sizeof(pss));
1590c5cfcb1cSMikolaj Golub 	if (error != 0)
1591c5cfcb1cSMikolaj Golub 		return (error);
1592c5cfcb1cSMikolaj Golub 	switch (type) {
1593c5cfcb1cSMikolaj Golub 	case PROC_ARG:
1594c5cfcb1cSMikolaj Golub 		vptr = (vm_offset_t)pss.ps_argvstr;
1595c5cfcb1cSMikolaj Golub 		vsize = pss.ps_nargvstr;
1596c5cfcb1cSMikolaj Golub 		if (vsize > ARG_MAX)
1597c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1598c5cfcb1cSMikolaj Golub 		size = vsize * sizeof(char *);
1599c5cfcb1cSMikolaj Golub 		break;
1600c5cfcb1cSMikolaj Golub 	case PROC_ENV:
1601c5cfcb1cSMikolaj Golub 		vptr = (vm_offset_t)pss.ps_envstr;
1602c5cfcb1cSMikolaj Golub 		vsize = pss.ps_nenvstr;
1603c5cfcb1cSMikolaj Golub 		if (vsize > ARG_MAX)
1604c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1605c5cfcb1cSMikolaj Golub 		size = vsize * sizeof(char *);
1606c5cfcb1cSMikolaj Golub 		break;
1607c5cfcb1cSMikolaj Golub 	case PROC_AUX:
1608c5cfcb1cSMikolaj Golub 		/*
1609c5cfcb1cSMikolaj Golub 		 * The aux array is just above env array on the stack. Check
1610c5cfcb1cSMikolaj Golub 		 * that the address is naturally aligned.
1611c5cfcb1cSMikolaj Golub 		 */
1612c5cfcb1cSMikolaj Golub 		vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1613c5cfcb1cSMikolaj Golub 		    * sizeof(char *);
1614c5cfcb1cSMikolaj Golub #if __ELF_WORD_SIZE == 64
1615c5cfcb1cSMikolaj Golub 		if (vptr % sizeof(uint64_t) != 0)
1616c5cfcb1cSMikolaj Golub #else
1617c5cfcb1cSMikolaj Golub 		if (vptr % sizeof(uint32_t) != 0)
1618c5cfcb1cSMikolaj Golub #endif
1619c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1620c5cfcb1cSMikolaj Golub 		/*
1621c5cfcb1cSMikolaj Golub 		 * We count the array size reading the aux vectors from the
1622c5cfcb1cSMikolaj Golub 		 * stack until AT_NULL vector is returned.  So (to keep the code
1623c5cfcb1cSMikolaj Golub 		 * simple) we read the process stack twice: the first time here
1624c5cfcb1cSMikolaj Golub 		 * to find the size and the second time when copying the vectors
1625c5cfcb1cSMikolaj Golub 		 * to the allocated proc_vector.
1626c5cfcb1cSMikolaj Golub 		 */
1627c5cfcb1cSMikolaj Golub 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1628c5cfcb1cSMikolaj Golub 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1629c5cfcb1cSMikolaj Golub 			if (error != 0)
1630c5cfcb1cSMikolaj Golub 				return (error);
1631c5cfcb1cSMikolaj Golub 			if (aux.a_type == AT_NULL)
1632c5cfcb1cSMikolaj Golub 				break;
1633c5cfcb1cSMikolaj Golub 			ptr += sizeof(aux);
1634c5cfcb1cSMikolaj Golub 		}
1635c5cfcb1cSMikolaj Golub 		/*
1636c5cfcb1cSMikolaj Golub 		 * If the PROC_AUXV_MAX entries are iterated over, and we have
1637c5cfcb1cSMikolaj Golub 		 * not reached AT_NULL, it is most likely we are reading wrong
1638c5cfcb1cSMikolaj Golub 		 * data: either the process doesn't have auxv array or data has
1639c5cfcb1cSMikolaj Golub 		 * been modified. Return the error in this case.
1640c5cfcb1cSMikolaj Golub 		 */
1641c5cfcb1cSMikolaj Golub 		if (aux.a_type != AT_NULL)
1642c5cfcb1cSMikolaj Golub 			return (ENOEXEC);
1643c5cfcb1cSMikolaj Golub 		vsize = i + 1;
1644c5cfcb1cSMikolaj Golub 		size = vsize * sizeof(aux);
1645c5cfcb1cSMikolaj Golub 		break;
1646c5cfcb1cSMikolaj Golub 	default:
1647c5cfcb1cSMikolaj Golub 		KASSERT(0, ("Wrong proc vector type: %d", type));
16487ad9baaeSMikolaj Golub 		return (EINVAL); /* In case we are built without INVARIANTS. */
1649c5cfcb1cSMikolaj Golub 	}
1650c5cfcb1cSMikolaj Golub 	proc_vector = malloc(size, M_TEMP, M_WAITOK);
1651c5cfcb1cSMikolaj Golub 	if (proc_vector == NULL)
1652c5cfcb1cSMikolaj Golub 		return (ENOMEM);
1653c5cfcb1cSMikolaj Golub 	error = proc_read_mem(td, p, vptr, proc_vector, size);
1654c5cfcb1cSMikolaj Golub 	if (error != 0) {
1655c5cfcb1cSMikolaj Golub 		free(proc_vector, M_TEMP);
1656c5cfcb1cSMikolaj Golub 		return (error);
1657c5cfcb1cSMikolaj Golub 	}
1658c5cfcb1cSMikolaj Golub 	*proc_vectorp = proc_vector;
1659c5cfcb1cSMikolaj Golub 	*vsizep = vsize;
1660c5cfcb1cSMikolaj Golub 
1661c5cfcb1cSMikolaj Golub 	return (0);
1662c5cfcb1cSMikolaj Golub }
1663c5cfcb1cSMikolaj Golub 
1664c5cfcb1cSMikolaj Golub #define GET_PS_STRINGS_CHUNK_SZ	256	/* Chunk size (bytes) for ps_strings operations. */
1665c5cfcb1cSMikolaj Golub 
1666c5cfcb1cSMikolaj Golub static int
1667c5cfcb1cSMikolaj Golub get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1668fe7f89b7SMikolaj Golub     enum proc_vector_type type)
1669c5cfcb1cSMikolaj Golub {
1670fe7f89b7SMikolaj Golub 	size_t done, len, nchr, vsize;
1671c5cfcb1cSMikolaj Golub 	int error, i;
1672c5cfcb1cSMikolaj Golub 	char **proc_vector, *sptr;
1673c5cfcb1cSMikolaj Golub 	char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1674c5cfcb1cSMikolaj Golub 
1675c5cfcb1cSMikolaj Golub 	PROC_ASSERT_HELD(p);
1676c5cfcb1cSMikolaj Golub 
1677c5cfcb1cSMikolaj Golub 	/*
1678c5cfcb1cSMikolaj Golub 	 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1679c5cfcb1cSMikolaj Golub 	 */
1680c5cfcb1cSMikolaj Golub 	nchr = 2 * (PATH_MAX + ARG_MAX);
1681c5cfcb1cSMikolaj Golub 
1682c5cfcb1cSMikolaj Golub 	error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1683c5cfcb1cSMikolaj Golub 	if (error != 0)
1684c5cfcb1cSMikolaj Golub 		return (error);
1685c5cfcb1cSMikolaj Golub 	for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1686c5cfcb1cSMikolaj Golub 		/*
1687c5cfcb1cSMikolaj Golub 		 * The program may have scribbled into its argv array, e.g. to
1688c5cfcb1cSMikolaj Golub 		 * remove some arguments.  If that has happened, break out
1689c5cfcb1cSMikolaj Golub 		 * before trying to read from NULL.
1690c5cfcb1cSMikolaj Golub 		 */
1691c5cfcb1cSMikolaj Golub 		if (proc_vector[i] == NULL)
1692c5cfcb1cSMikolaj Golub 			break;
1693c5cfcb1cSMikolaj Golub 		for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1694c5cfcb1cSMikolaj Golub 			error = proc_read_string(td, p, sptr, pss_string,
1695c5cfcb1cSMikolaj Golub 			    sizeof(pss_string));
1696c5cfcb1cSMikolaj Golub 			if (error != 0)
1697c5cfcb1cSMikolaj Golub 				goto done;
1698c5cfcb1cSMikolaj Golub 			len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1699c5cfcb1cSMikolaj Golub 			if (done + len >= nchr)
1700c5cfcb1cSMikolaj Golub 				len = nchr - done - 1;
1701c5cfcb1cSMikolaj Golub 			sbuf_bcat(sb, pss_string, len);
1702c5cfcb1cSMikolaj Golub 			if (len != GET_PS_STRINGS_CHUNK_SZ)
1703c5cfcb1cSMikolaj Golub 				break;
1704c5cfcb1cSMikolaj Golub 			done += GET_PS_STRINGS_CHUNK_SZ;
1705c5cfcb1cSMikolaj Golub 		}
1706c5cfcb1cSMikolaj Golub 		sbuf_bcat(sb, "", 1);
1707c5cfcb1cSMikolaj Golub 		done += len + 1;
1708c5cfcb1cSMikolaj Golub 	}
1709c5cfcb1cSMikolaj Golub done:
1710c5cfcb1cSMikolaj Golub 	free(proc_vector, M_TEMP);
1711c5cfcb1cSMikolaj Golub 	return (error);
1712c5cfcb1cSMikolaj Golub }
1713c5cfcb1cSMikolaj Golub 
1714c5cfcb1cSMikolaj Golub int
1715fe7f89b7SMikolaj Golub proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1716c5cfcb1cSMikolaj Golub {
1717c5cfcb1cSMikolaj Golub 
1718fe7f89b7SMikolaj Golub 	return (get_ps_strings(curthread, p, sb, PROC_ARG));
1719c5cfcb1cSMikolaj Golub }
1720c5cfcb1cSMikolaj Golub 
1721c5cfcb1cSMikolaj Golub int
1722fe7f89b7SMikolaj Golub proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1723c5cfcb1cSMikolaj Golub {
1724c5cfcb1cSMikolaj Golub 
1725fe7f89b7SMikolaj Golub 	return (get_ps_strings(curthread, p, sb, PROC_ENV));
1726c5cfcb1cSMikolaj Golub }
1727c5cfcb1cSMikolaj Golub 
1728b9df5231SPoul-Henning Kamp /*
1729b9df5231SPoul-Henning Kamp  * This sysctl allows a process to retrieve the argument list or process
1730b9df5231SPoul-Henning Kamp  * title for another process without groping around in the address space
1731b9df5231SPoul-Henning Kamp  * of the other process.  It also allow a process to set its own "process
1732b9df5231SPoul-Henning Kamp  * title to a string of its own choice.
1733b9df5231SPoul-Henning Kamp  */
1734b9df5231SPoul-Henning Kamp static int
173582d9ae4eSPoul-Henning Kamp sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1736b9df5231SPoul-Henning Kamp {
1737b9df5231SPoul-Henning Kamp 	int *name = (int *)arg1;
1738b9df5231SPoul-Henning Kamp 	u_int namelen = arg2;
17398510f2a8SJohn Baldwin 	struct pargs *newpa, *pa;
1740b9df5231SPoul-Henning Kamp 	struct proc *p;
1741c5cfcb1cSMikolaj Golub 	struct sbuf sb;
1742fa3935bcSMikolaj Golub 	int flags, error = 0, error2;
1743b9df5231SPoul-Henning Kamp 
1744b9df5231SPoul-Henning Kamp 	if (namelen != 1)
1745b9df5231SPoul-Henning Kamp 		return (EINVAL);
1746b9df5231SPoul-Henning Kamp 
1747fa3935bcSMikolaj Golub 	flags = PGET_CANSEE;
1748fa3935bcSMikolaj Golub 	if (req->newptr != NULL)
1749fa3935bcSMikolaj Golub 		flags |= PGET_ISCURRENT;
1750fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], flags, &p);
1751fa3935bcSMikolaj Golub 	if (error)
1752e76bad96SRobert Drehmel 		return (error);
1753b9df5231SPoul-Henning Kamp 
17547b11fea6SAlfred Perlstein 	pa = p->p_args;
1755c5cfcb1cSMikolaj Golub 	if (pa != NULL) {
17567b11fea6SAlfred Perlstein 		pargs_hold(pa);
17577b11fea6SAlfred Perlstein 		PROC_UNLOCK(p);
17587b11fea6SAlfred Perlstein 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
17597b11fea6SAlfred Perlstein 		pargs_drop(pa);
1760c5cfcb1cSMikolaj Golub 	} else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1761c5cfcb1cSMikolaj Golub 		_PHOLD(p);
1762c5cfcb1cSMikolaj Golub 		PROC_UNLOCK(p);
1763c5cfcb1cSMikolaj Golub 		sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1764fe7f89b7SMikolaj Golub 		error = proc_getargv(curthread, p, &sb);
1765c5cfcb1cSMikolaj Golub 		error2 = sbuf_finish(&sb);
1766c5cfcb1cSMikolaj Golub 		PRELE(p);
1767c5cfcb1cSMikolaj Golub 		sbuf_delete(&sb);
1768c5cfcb1cSMikolaj Golub 		if (error == 0 && error2 != 0)
1769c5cfcb1cSMikolaj Golub 			error = error2;
1770c5cfcb1cSMikolaj Golub 	} else {
1771c5cfcb1cSMikolaj Golub 		PROC_UNLOCK(p);
1772c5cfcb1cSMikolaj Golub 	}
17738510f2a8SJohn Baldwin 	if (error != 0 || req->newptr == NULL)
1774b9df5231SPoul-Henning Kamp 		return (error);
1775b9df5231SPoul-Henning Kamp 
1776b9df5231SPoul-Henning Kamp 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
17778510f2a8SJohn Baldwin 		return (ENOMEM);
17784bc6471bSJohn Baldwin 	newpa = pargs_alloc(req->newlen);
17794bc6471bSJohn Baldwin 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
17808510f2a8SJohn Baldwin 	if (error != 0) {
17814bc6471bSJohn Baldwin 		pargs_free(newpa);
1782b9df5231SPoul-Henning Kamp 		return (error);
1783b9df5231SPoul-Henning Kamp 	}
17844bc6471bSJohn Baldwin 	PROC_LOCK(p);
17854bc6471bSJohn Baldwin 	pa = p->p_args;
17864bc6471bSJohn Baldwin 	p->p_args = newpa;
17874bc6471bSJohn Baldwin 	PROC_UNLOCK(p);
17884bc6471bSJohn Baldwin 	pargs_drop(pa);
17894bc6471bSJohn Baldwin 	return (0);
17904bc6471bSJohn Baldwin }
17914bc6471bSJohn Baldwin 
1792fe769cddSDavid Schultz /*
1793c5cfcb1cSMikolaj Golub  * This sysctl allows a process to retrieve environment of another process.
1794c5cfcb1cSMikolaj Golub  */
1795c5cfcb1cSMikolaj Golub static int
1796c5cfcb1cSMikolaj Golub sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1797c5cfcb1cSMikolaj Golub {
1798c5cfcb1cSMikolaj Golub 	int *name = (int *)arg1;
1799c5cfcb1cSMikolaj Golub 	u_int namelen = arg2;
1800c5cfcb1cSMikolaj Golub 	struct proc *p;
1801c5cfcb1cSMikolaj Golub 	struct sbuf sb;
1802c5cfcb1cSMikolaj Golub 	int error, error2;
1803c5cfcb1cSMikolaj Golub 
1804c5cfcb1cSMikolaj Golub 	if (namelen != 1)
1805c5cfcb1cSMikolaj Golub 		return (EINVAL);
1806c5cfcb1cSMikolaj Golub 
1807fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1808fa3935bcSMikolaj Golub 	if (error != 0)
1809c5cfcb1cSMikolaj Golub 		return (error);
1810c5cfcb1cSMikolaj Golub 	if ((p->p_flag & P_SYSTEM) != 0) {
1811fa3935bcSMikolaj Golub 		PRELE(p);
1812c5cfcb1cSMikolaj Golub 		return (0);
1813c5cfcb1cSMikolaj Golub 	}
1814fa3935bcSMikolaj Golub 
1815c5cfcb1cSMikolaj Golub 	sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1816fe7f89b7SMikolaj Golub 	error = proc_getenvv(curthread, p, &sb);
1817c5cfcb1cSMikolaj Golub 	error2 = sbuf_finish(&sb);
1818c5cfcb1cSMikolaj Golub 	PRELE(p);
1819c5cfcb1cSMikolaj Golub 	sbuf_delete(&sb);
1820c5cfcb1cSMikolaj Golub 	return (error != 0 ? error : error2);
1821c5cfcb1cSMikolaj Golub }
1822c5cfcb1cSMikolaj Golub 
1823c5cfcb1cSMikolaj Golub /*
1824c5cfcb1cSMikolaj Golub  * This sysctl allows a process to retrieve ELF auxiliary vector of
1825c5cfcb1cSMikolaj Golub  * another process.
1826c5cfcb1cSMikolaj Golub  */
1827c5cfcb1cSMikolaj Golub static int
1828c5cfcb1cSMikolaj Golub sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1829c5cfcb1cSMikolaj Golub {
1830c5cfcb1cSMikolaj Golub 	int *name = (int *)arg1;
1831c5cfcb1cSMikolaj Golub 	u_int namelen = arg2;
1832c5cfcb1cSMikolaj Golub 	struct proc *p;
18334fd6053bSMikolaj Golub 	size_t vsize, size;
1834c5cfcb1cSMikolaj Golub 	char **auxv;
1835c5cfcb1cSMikolaj Golub 	int error;
1836c5cfcb1cSMikolaj Golub 
1837c5cfcb1cSMikolaj Golub 	if (namelen != 1)
1838c5cfcb1cSMikolaj Golub 		return (EINVAL);
1839c5cfcb1cSMikolaj Golub 
1840fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1841fa3935bcSMikolaj Golub 	if (error != 0)
1842c5cfcb1cSMikolaj Golub 		return (error);
1843c5cfcb1cSMikolaj Golub 	if ((p->p_flag & P_SYSTEM) != 0) {
1844fa3935bcSMikolaj Golub 		PRELE(p);
1845c5cfcb1cSMikolaj Golub 		return (0);
1846c5cfcb1cSMikolaj Golub 	}
1847c5cfcb1cSMikolaj Golub 	error = get_proc_vector(curthread, p, &auxv, &vsize, PROC_AUX);
1848c5cfcb1cSMikolaj Golub 	if (error == 0) {
1849c5cfcb1cSMikolaj Golub #ifdef COMPAT_FREEBSD32
1850c5cfcb1cSMikolaj Golub 		if (SV_PROC_FLAG(p, SV_ILP32) != 0)
18514fd6053bSMikolaj Golub 			size = vsize * sizeof(Elf32_Auxinfo);
1852c5cfcb1cSMikolaj Golub 		else
1853c5cfcb1cSMikolaj Golub #endif
18544fd6053bSMikolaj Golub 		size = vsize * sizeof(Elf_Auxinfo);
18554fd6053bSMikolaj Golub 		PRELE(p);
18564fd6053bSMikolaj Golub 		error = SYSCTL_OUT(req, auxv, size);
1857c5cfcb1cSMikolaj Golub 		free(auxv, M_TEMP);
18584fd6053bSMikolaj Golub 	} else {
18594fd6053bSMikolaj Golub 		PRELE(p);
1860c5cfcb1cSMikolaj Golub 	}
1861c5cfcb1cSMikolaj Golub 	return (error);
1862c5cfcb1cSMikolaj Golub }
1863c5cfcb1cSMikolaj Golub 
1864c5cfcb1cSMikolaj Golub /*
1865fe769cddSDavid Schultz  * This sysctl allows a process to retrieve the path of the executable for
1866fe769cddSDavid Schultz  * itself or another process.
1867fe769cddSDavid Schultz  */
1868fe769cddSDavid Schultz static int
1869fe769cddSDavid Schultz sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1870fe769cddSDavid Schultz {
1871fe769cddSDavid Schultz 	pid_t *pidp = (pid_t *)arg1;
1872fe769cddSDavid Schultz 	unsigned int arglen = arg2;
1873fe769cddSDavid Schultz 	struct proc *p;
1874fe769cddSDavid Schultz 	struct vnode *vp;
1875fe769cddSDavid Schultz 	char *retbuf, *freebuf;
187624f87fdbSJohn Baldwin 	int error, vfslocked;
1877fe769cddSDavid Schultz 
1878fe769cddSDavid Schultz 	if (arglen != 1)
1879fe769cddSDavid Schultz 		return (EINVAL);
1880fe769cddSDavid Schultz 	if (*pidp == -1) {	/* -1 means this process */
1881fe769cddSDavid Schultz 		p = req->td->td_proc;
1882fe769cddSDavid Schultz 	} else {
1883fa3935bcSMikolaj Golub 		error = pget(*pidp, PGET_CANSEE, &p);
1884fa3935bcSMikolaj Golub 		if (error != 0)
1885fe769cddSDavid Schultz 			return (error);
1886fe769cddSDavid Schultz 	}
1887fe769cddSDavid Schultz 
1888fe769cddSDavid Schultz 	vp = p->p_textvp;
1889965b55e2SRobert Watson 	if (vp == NULL) {
1890965b55e2SRobert Watson 		if (*pidp != -1)
1891965b55e2SRobert Watson 			PROC_UNLOCK(p);
1892965b55e2SRobert Watson 		return (0);
1893965b55e2SRobert Watson 	}
1894fe769cddSDavid Schultz 	vref(vp);
1895fe769cddSDavid Schultz 	if (*pidp != -1)
1896fe769cddSDavid Schultz 		PROC_UNLOCK(p);
1897fe769cddSDavid Schultz 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
189824f87fdbSJohn Baldwin 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1899fe769cddSDavid Schultz 	vrele(vp);
190024f87fdbSJohn Baldwin 	VFS_UNLOCK_GIANT(vfslocked);
1901fe769cddSDavid Schultz 	if (error)
1902fe769cddSDavid Schultz 		return (error);
1903fe769cddSDavid Schultz 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1904fe769cddSDavid Schultz 	free(freebuf, M_TEMP);
1905fe769cddSDavid Schultz 	return (error);
1906fe769cddSDavid Schultz }
1907fe769cddSDavid Schultz 
1908baf731e6SRobert Drehmel static int
1909baf731e6SRobert Drehmel sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1910baf731e6SRobert Drehmel {
1911baf731e6SRobert Drehmel 	struct proc *p;
1912baf731e6SRobert Drehmel 	char *sv_name;
1913baf731e6SRobert Drehmel 	int *name;
1914baf731e6SRobert Drehmel 	int namelen;
1915e76bad96SRobert Drehmel 	int error;
1916baf731e6SRobert Drehmel 
1917baf731e6SRobert Drehmel 	namelen = arg2;
1918baf731e6SRobert Drehmel 	if (namelen != 1)
1919baf731e6SRobert Drehmel 		return (EINVAL);
1920baf731e6SRobert Drehmel 
1921baf731e6SRobert Drehmel 	name = (int *)arg1;
1922fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_CANSEE, &p);
1923fa3935bcSMikolaj Golub 	if (error != 0)
1924e76bad96SRobert Drehmel 		return (error);
1925baf731e6SRobert Drehmel 	sv_name = p->p_sysent->sv_name;
1926baf731e6SRobert Drehmel 	PROC_UNLOCK(p);
1927baf731e6SRobert Drehmel 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1928baf731e6SRobert Drehmel }
1929baf731e6SRobert Drehmel 
193043151ee6SPeter Wemm #ifdef KINFO_OVMENTRY_SIZE
193143151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
193243151ee6SPeter Wemm #endif
193343151ee6SPeter Wemm 
193443151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7
1935cc43c38cSRobert Watson static int
193643151ee6SPeter Wemm sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1937cc43c38cSRobert Watson {
1938cc43c38cSRobert Watson 	vm_map_entry_t entry, tmp_entry;
1939cc43c38cSRobert Watson 	unsigned int last_timestamp;
1940cc43c38cSRobert Watson 	char *fullpath, *freepath;
194143151ee6SPeter Wemm 	struct kinfo_ovmentry *kve;
19427a9c4d24SPeter Wemm 	struct vattr va;
19437a9c4d24SPeter Wemm 	struct ucred *cred;
1944cc43c38cSRobert Watson 	int error, *name;
1945cc43c38cSRobert Watson 	struct vnode *vp;
1946cc43c38cSRobert Watson 	struct proc *p;
1947cc43c38cSRobert Watson 	vm_map_t map;
1948c7462f43SKonstantin Belousov 	struct vmspace *vm;
1949baf731e6SRobert Drehmel 
1950cc43c38cSRobert Watson 	name = (int *)arg1;
1951fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1952fa3935bcSMikolaj Golub 	if (error != 0)
1953cc43c38cSRobert Watson 		return (error);
1954c7462f43SKonstantin Belousov 	vm = vmspace_acquire_ref(p);
1955c7462f43SKonstantin Belousov 	if (vm == NULL) {
1956c7462f43SKonstantin Belousov 		PRELE(p);
1957c7462f43SKonstantin Belousov 		return (ESRCH);
1958c7462f43SKonstantin Belousov 	}
1959cc43c38cSRobert Watson 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1960cc43c38cSRobert Watson 
1961ca4aa8c3SSergey Kandaurov 	map = &vm->vm_map;
1962cc43c38cSRobert Watson 	vm_map_lock_read(map);
1963cc43c38cSRobert Watson 	for (entry = map->header.next; entry != &map->header;
1964cc43c38cSRobert Watson 	    entry = entry->next) {
1965cc43c38cSRobert Watson 		vm_object_t obj, tobj, lobj;
1966cc43c38cSRobert Watson 		vm_offset_t addr;
1967cc43c38cSRobert Watson 		int vfslocked;
1968cc43c38cSRobert Watson 
1969cc43c38cSRobert Watson 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1970cc43c38cSRobert Watson 			continue;
1971cc43c38cSRobert Watson 
1972cc43c38cSRobert Watson 		bzero(kve, sizeof(*kve));
1973cc43c38cSRobert Watson 		kve->kve_structsize = sizeof(*kve);
1974cc43c38cSRobert Watson 
1975cc43c38cSRobert Watson 		kve->kve_private_resident = 0;
1976cc43c38cSRobert Watson 		obj = entry->object.vm_object;
1977cc43c38cSRobert Watson 		if (obj != NULL) {
1978cc43c38cSRobert Watson 			VM_OBJECT_LOCK(obj);
1979cc43c38cSRobert Watson 			if (obj->shadow_count == 1)
1980cc43c38cSRobert Watson 				kve->kve_private_resident =
1981cc43c38cSRobert Watson 				    obj->resident_page_count;
1982cc43c38cSRobert Watson 		}
1983cc43c38cSRobert Watson 		kve->kve_resident = 0;
1984cc43c38cSRobert Watson 		addr = entry->start;
1985cc43c38cSRobert Watson 		while (addr < entry->end) {
1986cc43c38cSRobert Watson 			if (pmap_extract(map->pmap, addr))
1987cc43c38cSRobert Watson 				kve->kve_resident++;
1988cc43c38cSRobert Watson 			addr += PAGE_SIZE;
1989cc43c38cSRobert Watson 		}
1990cc43c38cSRobert Watson 
1991cc43c38cSRobert Watson 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1992cc43c38cSRobert Watson 			if (tobj != obj)
1993cc43c38cSRobert Watson 				VM_OBJECT_LOCK(tobj);
1994cc43c38cSRobert Watson 			if (lobj != obj)
1995cc43c38cSRobert Watson 				VM_OBJECT_UNLOCK(lobj);
1996cc43c38cSRobert Watson 			lobj = tobj;
1997cc43c38cSRobert Watson 		}
1998cc43c38cSRobert Watson 
1999118d0afaSKonstantin Belousov 		kve->kve_start = (void*)entry->start;
2000118d0afaSKonstantin Belousov 		kve->kve_end = (void*)entry->end;
2001118d0afaSKonstantin Belousov 		kve->kve_offset = (off_t)entry->offset;
2002118d0afaSKonstantin Belousov 
2003118d0afaSKonstantin Belousov 		if (entry->protection & VM_PROT_READ)
2004118d0afaSKonstantin Belousov 			kve->kve_protection |= KVME_PROT_READ;
2005118d0afaSKonstantin Belousov 		if (entry->protection & VM_PROT_WRITE)
2006118d0afaSKonstantin Belousov 			kve->kve_protection |= KVME_PROT_WRITE;
2007118d0afaSKonstantin Belousov 		if (entry->protection & VM_PROT_EXECUTE)
2008118d0afaSKonstantin Belousov 			kve->kve_protection |= KVME_PROT_EXEC;
2009118d0afaSKonstantin Belousov 
2010118d0afaSKonstantin Belousov 		if (entry->eflags & MAP_ENTRY_COW)
2011118d0afaSKonstantin Belousov 			kve->kve_flags |= KVME_FLAG_COW;
2012118d0afaSKonstantin Belousov 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2013118d0afaSKonstantin Belousov 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2014937912eaSAttilio Rao 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2015937912eaSAttilio Rao 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2016118d0afaSKonstantin Belousov 
2017118d0afaSKonstantin Belousov 		last_timestamp = map->timestamp;
2018118d0afaSKonstantin Belousov 		vm_map_unlock_read(map);
2019118d0afaSKonstantin Belousov 
20207a9c4d24SPeter Wemm 		kve->kve_fileid = 0;
20217a9c4d24SPeter Wemm 		kve->kve_fsid = 0;
2022cc43c38cSRobert Watson 		freepath = NULL;
2023cc43c38cSRobert Watson 		fullpath = "";
2024cc43c38cSRobert Watson 		if (lobj) {
2025cc43c38cSRobert Watson 			vp = NULL;
2026cc43c38cSRobert Watson 			switch (lobj->type) {
2027cc43c38cSRobert Watson 			case OBJT_DEFAULT:
2028cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_DEFAULT;
2029cc43c38cSRobert Watson 				break;
2030cc43c38cSRobert Watson 			case OBJT_VNODE:
2031cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_VNODE;
2032cc43c38cSRobert Watson 				vp = lobj->handle;
2033cc43c38cSRobert Watson 				vref(vp);
2034cc43c38cSRobert Watson 				break;
2035cc43c38cSRobert Watson 			case OBJT_SWAP:
2036cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_SWAP;
2037cc43c38cSRobert Watson 				break;
2038cc43c38cSRobert Watson 			case OBJT_DEVICE:
2039cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_DEVICE;
2040cc43c38cSRobert Watson 				break;
2041cc43c38cSRobert Watson 			case OBJT_PHYS:
2042cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_PHYS;
2043cc43c38cSRobert Watson 				break;
2044cc43c38cSRobert Watson 			case OBJT_DEAD:
2045cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_DEAD;
2046cc43c38cSRobert Watson 				break;
204701381811SJohn Baldwin 			case OBJT_SG:
204801381811SJohn Baldwin 				kve->kve_type = KVME_TYPE_SG;
204901381811SJohn Baldwin 				break;
2050cc43c38cSRobert Watson 			default:
2051cc43c38cSRobert Watson 				kve->kve_type = KVME_TYPE_UNKNOWN;
2052cc43c38cSRobert Watson 				break;
2053cc43c38cSRobert Watson 			}
2054cc43c38cSRobert Watson 			if (lobj != obj)
2055cc43c38cSRobert Watson 				VM_OBJECT_UNLOCK(lobj);
2056cc43c38cSRobert Watson 
2057cc43c38cSRobert Watson 			kve->kve_ref_count = obj->ref_count;
2058cc43c38cSRobert Watson 			kve->kve_shadow_count = obj->shadow_count;
2059cc43c38cSRobert Watson 			VM_OBJECT_UNLOCK(obj);
2060cc43c38cSRobert Watson 			if (vp != NULL) {
2061cc43c38cSRobert Watson 				vn_fullpath(curthread, vp, &fullpath,
2062cc43c38cSRobert Watson 				    &freepath);
20637a9c4d24SPeter Wemm 				cred = curthread->td_ucred;
20642ff47c5fSJohn Baldwin 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
20652ff47c5fSJohn Baldwin 				vn_lock(vp, LK_SHARED | LK_RETRY);
20667a9c4d24SPeter Wemm 				if (VOP_GETATTR(vp, &va, cred) == 0) {
20677a9c4d24SPeter Wemm 					kve->kve_fileid = va.va_fileid;
20687a9c4d24SPeter Wemm 					kve->kve_fsid = va.va_fsid;
20697a9c4d24SPeter Wemm 				}
2070cc43c38cSRobert Watson 				vput(vp);
2071cc43c38cSRobert Watson 				VFS_UNLOCK_GIANT(vfslocked);
2072cc43c38cSRobert Watson 			}
2073cc43c38cSRobert Watson 		} else {
2074cc43c38cSRobert Watson 			kve->kve_type = KVME_TYPE_NONE;
2075cc43c38cSRobert Watson 			kve->kve_ref_count = 0;
2076cc43c38cSRobert Watson 			kve->kve_shadow_count = 0;
2077cc43c38cSRobert Watson 		}
2078cc43c38cSRobert Watson 
2079cc43c38cSRobert Watson 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2080cc43c38cSRobert Watson 		if (freepath != NULL)
2081cc43c38cSRobert Watson 			free(freepath, M_TEMP);
2082cc43c38cSRobert Watson 
2083cc43c38cSRobert Watson 		error = SYSCTL_OUT(req, kve, sizeof(*kve));
2084cc43c38cSRobert Watson 		vm_map_lock_read(map);
2085cc43c38cSRobert Watson 		if (error)
2086cc43c38cSRobert Watson 			break;
208722a448c4SKonstantin Belousov 		if (last_timestamp != map->timestamp) {
2088cc43c38cSRobert Watson 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2089cc43c38cSRobert Watson 			entry = tmp_entry;
2090cc43c38cSRobert Watson 		}
2091cc43c38cSRobert Watson 	}
2092cc43c38cSRobert Watson 	vm_map_unlock_read(map);
2093c7462f43SKonstantin Belousov 	vmspace_free(vm);
2094cc43c38cSRobert Watson 	PRELE(p);
2095cc43c38cSRobert Watson 	free(kve, M_TEMP);
2096cc43c38cSRobert Watson 	return (error);
2097cc43c38cSRobert Watson }
209843151ee6SPeter Wemm #endif	/* COMPAT_FREEBSD7 */
209943151ee6SPeter Wemm 
210043151ee6SPeter Wemm #ifdef KINFO_VMENTRY_SIZE
210143151ee6SPeter Wemm CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
210243151ee6SPeter Wemm #endif
210343151ee6SPeter Wemm 
210443151ee6SPeter Wemm static int
210543151ee6SPeter Wemm sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
210643151ee6SPeter Wemm {
210743151ee6SPeter Wemm 	vm_map_entry_t entry, tmp_entry;
210843151ee6SPeter Wemm 	unsigned int last_timestamp;
210943151ee6SPeter Wemm 	char *fullpath, *freepath;
211043151ee6SPeter Wemm 	struct kinfo_vmentry *kve;
211143151ee6SPeter Wemm 	struct vattr va;
211243151ee6SPeter Wemm 	struct ucred *cred;
211343151ee6SPeter Wemm 	int error, *name;
211443151ee6SPeter Wemm 	struct vnode *vp;
211543151ee6SPeter Wemm 	struct proc *p;
2116c7462f43SKonstantin Belousov 	struct vmspace *vm;
211743151ee6SPeter Wemm 	vm_map_t map;
211843151ee6SPeter Wemm 
211943151ee6SPeter Wemm 	name = (int *)arg1;
2120fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2121fa3935bcSMikolaj Golub 	if (error != 0)
212243151ee6SPeter Wemm 		return (error);
2123c7462f43SKonstantin Belousov 	vm = vmspace_acquire_ref(p);
2124c7462f43SKonstantin Belousov 	if (vm == NULL) {
2125c7462f43SKonstantin Belousov 		PRELE(p);
2126c7462f43SKonstantin Belousov 		return (ESRCH);
2127c7462f43SKonstantin Belousov 	}
212843151ee6SPeter Wemm 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
212943151ee6SPeter Wemm 
2130ca4aa8c3SSergey Kandaurov 	map = &vm->vm_map;
213143151ee6SPeter Wemm 	vm_map_lock_read(map);
213243151ee6SPeter Wemm 	for (entry = map->header.next; entry != &map->header;
213343151ee6SPeter Wemm 	    entry = entry->next) {
213443151ee6SPeter Wemm 		vm_object_t obj, tobj, lobj;
213543151ee6SPeter Wemm 		vm_offset_t addr;
21365384d089SMikolaj Golub 		vm_paddr_t locked_pa;
21375384d089SMikolaj Golub 		int vfslocked, mincoreinfo;
213843151ee6SPeter Wemm 
213943151ee6SPeter Wemm 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
214043151ee6SPeter Wemm 			continue;
214143151ee6SPeter Wemm 
214243151ee6SPeter Wemm 		bzero(kve, sizeof(*kve));
214343151ee6SPeter Wemm 
214443151ee6SPeter Wemm 		kve->kve_private_resident = 0;
214543151ee6SPeter Wemm 		obj = entry->object.vm_object;
214643151ee6SPeter Wemm 		if (obj != NULL) {
214743151ee6SPeter Wemm 			VM_OBJECT_LOCK(obj);
214843151ee6SPeter Wemm 			if (obj->shadow_count == 1)
214943151ee6SPeter Wemm 				kve->kve_private_resident =
215043151ee6SPeter Wemm 				    obj->resident_page_count;
215143151ee6SPeter Wemm 		}
215243151ee6SPeter Wemm 		kve->kve_resident = 0;
215343151ee6SPeter Wemm 		addr = entry->start;
215443151ee6SPeter Wemm 		while (addr < entry->end) {
21555384d089SMikolaj Golub 			locked_pa = 0;
21565384d089SMikolaj Golub 			mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa);
21575384d089SMikolaj Golub 			if (locked_pa != 0)
21585384d089SMikolaj Golub 				vm_page_unlock(PHYS_TO_VM_PAGE(locked_pa));
21595384d089SMikolaj Golub 			if (mincoreinfo & MINCORE_INCORE)
216043151ee6SPeter Wemm 				kve->kve_resident++;
21615384d089SMikolaj Golub 			if (mincoreinfo & MINCORE_SUPER)
21625384d089SMikolaj Golub 				kve->kve_flags |= KVME_FLAG_SUPER;
216343151ee6SPeter Wemm 			addr += PAGE_SIZE;
216443151ee6SPeter Wemm 		}
216543151ee6SPeter Wemm 
216643151ee6SPeter Wemm 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
216743151ee6SPeter Wemm 			if (tobj != obj)
216843151ee6SPeter Wemm 				VM_OBJECT_LOCK(tobj);
216943151ee6SPeter Wemm 			if (lobj != obj)
217043151ee6SPeter Wemm 				VM_OBJECT_UNLOCK(lobj);
217143151ee6SPeter Wemm 			lobj = tobj;
217243151ee6SPeter Wemm 		}
217343151ee6SPeter Wemm 
2174118d0afaSKonstantin Belousov 		kve->kve_start = entry->start;
2175118d0afaSKonstantin Belousov 		kve->kve_end = entry->end;
2176118d0afaSKonstantin Belousov 		kve->kve_offset = entry->offset;
2177118d0afaSKonstantin Belousov 
2178118d0afaSKonstantin Belousov 		if (entry->protection & VM_PROT_READ)
2179118d0afaSKonstantin Belousov 			kve->kve_protection |= KVME_PROT_READ;
2180118d0afaSKonstantin Belousov 		if (entry->protection & VM_PROT_WRITE)
2181118d0afaSKonstantin Belousov 			kve->kve_protection |= KVME_PROT_WRITE;
2182118d0afaSKonstantin Belousov 		if (entry->protection & VM_PROT_EXECUTE)
2183118d0afaSKonstantin Belousov 			kve->kve_protection |= KVME_PROT_EXEC;
2184118d0afaSKonstantin Belousov 
2185118d0afaSKonstantin Belousov 		if (entry->eflags & MAP_ENTRY_COW)
2186118d0afaSKonstantin Belousov 			kve->kve_flags |= KVME_FLAG_COW;
2187118d0afaSKonstantin Belousov 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2188118d0afaSKonstantin Belousov 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2189937912eaSAttilio Rao 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2190937912eaSAttilio Rao 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2191118d0afaSKonstantin Belousov 
2192118d0afaSKonstantin Belousov 		last_timestamp = map->timestamp;
2193118d0afaSKonstantin Belousov 		vm_map_unlock_read(map);
2194118d0afaSKonstantin Belousov 
219543151ee6SPeter Wemm 		freepath = NULL;
219643151ee6SPeter Wemm 		fullpath = "";
219743151ee6SPeter Wemm 		if (lobj) {
219843151ee6SPeter Wemm 			vp = NULL;
219943151ee6SPeter Wemm 			switch (lobj->type) {
220043151ee6SPeter Wemm 			case OBJT_DEFAULT:
220143151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_DEFAULT;
220243151ee6SPeter Wemm 				break;
220343151ee6SPeter Wemm 			case OBJT_VNODE:
220443151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_VNODE;
220543151ee6SPeter Wemm 				vp = lobj->handle;
220643151ee6SPeter Wemm 				vref(vp);
220743151ee6SPeter Wemm 				break;
220843151ee6SPeter Wemm 			case OBJT_SWAP:
220943151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_SWAP;
221043151ee6SPeter Wemm 				break;
221143151ee6SPeter Wemm 			case OBJT_DEVICE:
221243151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_DEVICE;
221343151ee6SPeter Wemm 				break;
221443151ee6SPeter Wemm 			case OBJT_PHYS:
221543151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_PHYS;
221643151ee6SPeter Wemm 				break;
221743151ee6SPeter Wemm 			case OBJT_DEAD:
221843151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_DEAD;
221943151ee6SPeter Wemm 				break;
222001381811SJohn Baldwin 			case OBJT_SG:
222101381811SJohn Baldwin 				kve->kve_type = KVME_TYPE_SG;
222201381811SJohn Baldwin 				break;
222343151ee6SPeter Wemm 			default:
222443151ee6SPeter Wemm 				kve->kve_type = KVME_TYPE_UNKNOWN;
222543151ee6SPeter Wemm 				break;
222643151ee6SPeter Wemm 			}
222743151ee6SPeter Wemm 			if (lobj != obj)
222843151ee6SPeter Wemm 				VM_OBJECT_UNLOCK(lobj);
222943151ee6SPeter Wemm 
223043151ee6SPeter Wemm 			kve->kve_ref_count = obj->ref_count;
223143151ee6SPeter Wemm 			kve->kve_shadow_count = obj->shadow_count;
223243151ee6SPeter Wemm 			VM_OBJECT_UNLOCK(obj);
223343151ee6SPeter Wemm 			if (vp != NULL) {
223443151ee6SPeter Wemm 				vn_fullpath(curthread, vp, &fullpath,
223543151ee6SPeter Wemm 				    &freepath);
22360daf62d9SStanislav Sedov 				kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
223743151ee6SPeter Wemm 				cred = curthread->td_ucred;
223843151ee6SPeter Wemm 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
223943151ee6SPeter Wemm 				vn_lock(vp, LK_SHARED | LK_RETRY);
224043151ee6SPeter Wemm 				if (VOP_GETATTR(vp, &va, cred) == 0) {
22410daf62d9SStanislav Sedov 					kve->kve_vn_fileid = va.va_fileid;
22420daf62d9SStanislav Sedov 					kve->kve_vn_fsid = va.va_fsid;
22430daf62d9SStanislav Sedov 					kve->kve_vn_mode =
22440daf62d9SStanislav Sedov 					    MAKEIMODE(va.va_type, va.va_mode);
22450daf62d9SStanislav Sedov 					kve->kve_vn_size = va.va_size;
22460daf62d9SStanislav Sedov 					kve->kve_vn_rdev = va.va_rdev;
22470daf62d9SStanislav Sedov 					kve->kve_status = KF_ATTR_VALID;
224843151ee6SPeter Wemm 				}
224943151ee6SPeter Wemm 				vput(vp);
225043151ee6SPeter Wemm 				VFS_UNLOCK_GIANT(vfslocked);
225143151ee6SPeter Wemm 			}
225243151ee6SPeter Wemm 		} else {
225343151ee6SPeter Wemm 			kve->kve_type = KVME_TYPE_NONE;
225443151ee6SPeter Wemm 			kve->kve_ref_count = 0;
225543151ee6SPeter Wemm 			kve->kve_shadow_count = 0;
225643151ee6SPeter Wemm 		}
225743151ee6SPeter Wemm 
225843151ee6SPeter Wemm 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
225943151ee6SPeter Wemm 		if (freepath != NULL)
226043151ee6SPeter Wemm 			free(freepath, M_TEMP);
226143151ee6SPeter Wemm 
226243151ee6SPeter Wemm 		/* Pack record size down */
226343151ee6SPeter Wemm 		kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
226443151ee6SPeter Wemm 		    strlen(kve->kve_path) + 1;
226543151ee6SPeter Wemm 		kve->kve_structsize = roundup(kve->kve_structsize,
226643151ee6SPeter Wemm 		    sizeof(uint64_t));
226743151ee6SPeter Wemm 		error = SYSCTL_OUT(req, kve, kve->kve_structsize);
226843151ee6SPeter Wemm 		vm_map_lock_read(map);
226943151ee6SPeter Wemm 		if (error)
227043151ee6SPeter Wemm 			break;
227122a448c4SKonstantin Belousov 		if (last_timestamp != map->timestamp) {
227243151ee6SPeter Wemm 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
227343151ee6SPeter Wemm 			entry = tmp_entry;
227443151ee6SPeter Wemm 		}
227543151ee6SPeter Wemm 	}
227643151ee6SPeter Wemm 	vm_map_unlock_read(map);
2277c7462f43SKonstantin Belousov 	vmspace_free(vm);
227843151ee6SPeter Wemm 	PRELE(p);
227943151ee6SPeter Wemm 	free(kve, M_TEMP);
228043151ee6SPeter Wemm 	return (error);
228143151ee6SPeter Wemm }
2282cc43c38cSRobert Watson 
22831cc8c45cSRobert Watson #if defined(STACK) || defined(DDB)
22841cc8c45cSRobert Watson static int
22851cc8c45cSRobert Watson sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
22861cc8c45cSRobert Watson {
22871cc8c45cSRobert Watson 	struct kinfo_kstack *kkstp;
22881cc8c45cSRobert Watson 	int error, i, *name, numthreads;
22891cc8c45cSRobert Watson 	lwpid_t *lwpidarray;
22901cc8c45cSRobert Watson 	struct thread *td;
22911cc8c45cSRobert Watson 	struct stack *st;
22921cc8c45cSRobert Watson 	struct sbuf sb;
22931cc8c45cSRobert Watson 	struct proc *p;
22941cc8c45cSRobert Watson 
22951cc8c45cSRobert Watson 	name = (int *)arg1;
2296fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2297fa3935bcSMikolaj Golub 	if (error != 0)
22981cc8c45cSRobert Watson 		return (error);
22991cc8c45cSRobert Watson 
23001cc8c45cSRobert Watson 	kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
23011cc8c45cSRobert Watson 	st = stack_create();
23021cc8c45cSRobert Watson 
23031cc8c45cSRobert Watson 	lwpidarray = NULL;
23041cc8c45cSRobert Watson 	numthreads = 0;
2305374ae2a3SJeff Roberson 	PROC_LOCK(p);
23061cc8c45cSRobert Watson repeat:
23071cc8c45cSRobert Watson 	if (numthreads < p->p_numthreads) {
23081cc8c45cSRobert Watson 		if (lwpidarray != NULL) {
23091cc8c45cSRobert Watson 			free(lwpidarray, M_TEMP);
23101cc8c45cSRobert Watson 			lwpidarray = NULL;
23111cc8c45cSRobert Watson 		}
23121cc8c45cSRobert Watson 		numthreads = p->p_numthreads;
2313374ae2a3SJeff Roberson 		PROC_UNLOCK(p);
23141cc8c45cSRobert Watson 		lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
23151cc8c45cSRobert Watson 		    M_WAITOK | M_ZERO);
2316374ae2a3SJeff Roberson 		PROC_LOCK(p);
23171cc8c45cSRobert Watson 		goto repeat;
23181cc8c45cSRobert Watson 	}
23191cc8c45cSRobert Watson 	i = 0;
23201cc8c45cSRobert Watson 
23211cc8c45cSRobert Watson 	/*
23221cc8c45cSRobert Watson 	 * XXXRW: During the below loop, execve(2) and countless other sorts
23231cc8c45cSRobert Watson 	 * of changes could have taken place.  Should we check to see if the
23241cc8c45cSRobert Watson 	 * vmspace has been replaced, or the like, in order to prevent
23251cc8c45cSRobert Watson 	 * giving a snapshot that spans, say, execve(2), with some threads
23261cc8c45cSRobert Watson 	 * before and some after?  Among other things, the credentials could
23271cc8c45cSRobert Watson 	 * have changed, in which case the right to extract debug info might
23281cc8c45cSRobert Watson 	 * no longer be assured.
23291cc8c45cSRobert Watson 	 */
23301cc8c45cSRobert Watson 	FOREACH_THREAD_IN_PROC(p, td) {
23311cc8c45cSRobert Watson 		KASSERT(i < numthreads,
23321cc8c45cSRobert Watson 		    ("sysctl_kern_proc_kstack: numthreads"));
23331cc8c45cSRobert Watson 		lwpidarray[i] = td->td_tid;
23341cc8c45cSRobert Watson 		i++;
23351cc8c45cSRobert Watson 	}
23361cc8c45cSRobert Watson 	numthreads = i;
23371cc8c45cSRobert Watson 	for (i = 0; i < numthreads; i++) {
23381cc8c45cSRobert Watson 		td = thread_find(p, lwpidarray[i]);
23391cc8c45cSRobert Watson 		if (td == NULL) {
23401cc8c45cSRobert Watson 			continue;
23411cc8c45cSRobert Watson 		}
23421cc8c45cSRobert Watson 		bzero(kkstp, sizeof(*kkstp));
23431cc8c45cSRobert Watson 		(void)sbuf_new(&sb, kkstp->kkst_trace,
23441cc8c45cSRobert Watson 		    sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
23451cc8c45cSRobert Watson 		thread_lock(td);
23461cc8c45cSRobert Watson 		kkstp->kkst_tid = td->td_tid;
23471cc8c45cSRobert Watson 		if (TD_IS_SWAPPED(td))
23481cc8c45cSRobert Watson 			kkstp->kkst_state = KKST_STATE_SWAPPED;
23491cc8c45cSRobert Watson 		else if (TD_IS_RUNNING(td))
23501cc8c45cSRobert Watson 			kkstp->kkst_state = KKST_STATE_RUNNING;
23511cc8c45cSRobert Watson 		else {
23521cc8c45cSRobert Watson 			kkstp->kkst_state = KKST_STATE_STACKOK;
23531cc8c45cSRobert Watson 			stack_save_td(st, td);
23541cc8c45cSRobert Watson 		}
23551cc8c45cSRobert Watson 		thread_unlock(td);
23561cc8c45cSRobert Watson 		PROC_UNLOCK(p);
23571cc8c45cSRobert Watson 		stack_sbuf_print(&sb, st);
23581cc8c45cSRobert Watson 		sbuf_finish(&sb);
23591cc8c45cSRobert Watson 		sbuf_delete(&sb);
23601cc8c45cSRobert Watson 		error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
23611cc8c45cSRobert Watson 		PROC_LOCK(p);
23621cc8c45cSRobert Watson 		if (error)
23631cc8c45cSRobert Watson 			break;
23641cc8c45cSRobert Watson 	}
23651cc8c45cSRobert Watson 	_PRELE(p);
23661cc8c45cSRobert Watson 	PROC_UNLOCK(p);
23671cc8c45cSRobert Watson 	if (lwpidarray != NULL)
23681cc8c45cSRobert Watson 		free(lwpidarray, M_TEMP);
23691cc8c45cSRobert Watson 	stack_destroy(st);
23701cc8c45cSRobert Watson 	free(kkstp, M_TEMP);
23711cc8c45cSRobert Watson 	return (error);
23721cc8c45cSRobert Watson }
23731cc8c45cSRobert Watson #endif
23741cc8c45cSRobert Watson 
2375254d03c5SBrooks Davis /*
2376254d03c5SBrooks Davis  * This sysctl allows a process to retrieve the full list of groups from
2377254d03c5SBrooks Davis  * itself or another process.
2378254d03c5SBrooks Davis  */
2379254d03c5SBrooks Davis static int
2380254d03c5SBrooks Davis sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2381254d03c5SBrooks Davis {
2382254d03c5SBrooks Davis 	pid_t *pidp = (pid_t *)arg1;
2383254d03c5SBrooks Davis 	unsigned int arglen = arg2;
2384254d03c5SBrooks Davis 	struct proc *p;
2385254d03c5SBrooks Davis 	struct ucred *cred;
2386254d03c5SBrooks Davis 	int error;
2387254d03c5SBrooks Davis 
2388254d03c5SBrooks Davis 	if (arglen != 1)
2389254d03c5SBrooks Davis 		return (EINVAL);
2390254d03c5SBrooks Davis 	if (*pidp == -1) {	/* -1 means this process */
2391254d03c5SBrooks Davis 		p = req->td->td_proc;
2392254d03c5SBrooks Davis 	} else {
2393fa3935bcSMikolaj Golub 		error = pget(*pidp, PGET_CANSEE, &p);
2394fa3935bcSMikolaj Golub 		if (error != 0)
2395254d03c5SBrooks Davis 			return (error);
2396254d03c5SBrooks Davis 	}
2397254d03c5SBrooks Davis 
2398254d03c5SBrooks Davis 	cred = crhold(p->p_ucred);
2399254d03c5SBrooks Davis 	if (*pidp != -1)
2400254d03c5SBrooks Davis 		PROC_UNLOCK(p);
2401254d03c5SBrooks Davis 
2402254d03c5SBrooks Davis 	error = SYSCTL_OUT(req, cred->cr_groups,
2403254d03c5SBrooks Davis 	    cred->cr_ngroups * sizeof(gid_t));
2404254d03c5SBrooks Davis 	crfree(cred);
2405254d03c5SBrooks Davis 	return (error);
2406254d03c5SBrooks Davis }
2407254d03c5SBrooks Davis 
24089e7d0583SMikolaj Golub /*
24098854fe39SMikolaj Golub  * This sysctl allows a process to retrieve or/and set the resource limit for
24109e7d0583SMikolaj Golub  * another process.
24119e7d0583SMikolaj Golub  */
24129e7d0583SMikolaj Golub static int
24139e7d0583SMikolaj Golub sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
24149e7d0583SMikolaj Golub {
24159e7d0583SMikolaj Golub 	int *name = (int *)arg1;
24169e7d0583SMikolaj Golub 	u_int namelen = arg2;
24178854fe39SMikolaj Golub 	struct rlimit rlim;
24189e7d0583SMikolaj Golub 	struct proc *p;
24198854fe39SMikolaj Golub 	u_int which;
24208854fe39SMikolaj Golub 	int flags, error;
24219e7d0583SMikolaj Golub 
24228854fe39SMikolaj Golub 	if (namelen != 2)
24239e7d0583SMikolaj Golub 		return (EINVAL);
24249e7d0583SMikolaj Golub 
24258854fe39SMikolaj Golub 	which = (u_int)name[1];
24268854fe39SMikolaj Golub 	if (which >= RLIM_NLIMITS)
24278854fe39SMikolaj Golub 		return (EINVAL);
24288854fe39SMikolaj Golub 
24298854fe39SMikolaj Golub 	if (req->newptr != NULL && req->newlen != sizeof(rlim))
24308854fe39SMikolaj Golub 		return (EINVAL);
24318854fe39SMikolaj Golub 
24328854fe39SMikolaj Golub 	flags = PGET_HOLD | PGET_NOTWEXIT;
24338854fe39SMikolaj Golub 	if (req->newptr != NULL)
24348854fe39SMikolaj Golub 		flags |= PGET_CANDEBUG;
24358854fe39SMikolaj Golub 	else
24368854fe39SMikolaj Golub 		flags |= PGET_CANSEE;
24378854fe39SMikolaj Golub 	error = pget((pid_t)name[0], flags, &p);
2438fa3935bcSMikolaj Golub 	if (error != 0)
24399e7d0583SMikolaj Golub 		return (error);
24408854fe39SMikolaj Golub 
24419e7d0583SMikolaj Golub 	/*
24428854fe39SMikolaj Golub 	 * Retrieve limit.
24439e7d0583SMikolaj Golub 	 */
24448854fe39SMikolaj Golub 	if (req->oldptr != NULL) {
24458854fe39SMikolaj Golub 		PROC_LOCK(p);
24468854fe39SMikolaj Golub 		lim_rlimit(p, which, &rlim);
24479e7d0583SMikolaj Golub 		PROC_UNLOCK(p);
24488854fe39SMikolaj Golub 	}
24498854fe39SMikolaj Golub 	error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
24508854fe39SMikolaj Golub 	if (error != 0)
24518854fe39SMikolaj Golub 		goto errout;
24528854fe39SMikolaj Golub 
24538854fe39SMikolaj Golub 	/*
24548854fe39SMikolaj Golub 	 * Set limit.
24558854fe39SMikolaj Golub 	 */
24568854fe39SMikolaj Golub 	if (req->newptr != NULL) {
24578854fe39SMikolaj Golub 		error = SYSCTL_IN(req, &rlim, sizeof(rlim));
24588854fe39SMikolaj Golub 		if (error == 0)
24598854fe39SMikolaj Golub 			error = kern_proc_setrlimit(curthread, p, which, &rlim);
24609e7d0583SMikolaj Golub 	}
24619e7d0583SMikolaj Golub 
24628854fe39SMikolaj Golub errout:
24638854fe39SMikolaj Golub 	PRELE(p);
24649e7d0583SMikolaj Golub 	return (error);
24659e7d0583SMikolaj Golub }
24669e7d0583SMikolaj Golub 
24679732458fSMikolaj Golub /*
24689732458fSMikolaj Golub  * This sysctl allows a process to retrieve ps_strings structure location of
24699732458fSMikolaj Golub  * another process.
24709732458fSMikolaj Golub  */
24719732458fSMikolaj Golub static int
24729732458fSMikolaj Golub sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
24739732458fSMikolaj Golub {
24749732458fSMikolaj Golub 	int *name = (int *)arg1;
24759732458fSMikolaj Golub 	u_int namelen = arg2;
24769732458fSMikolaj Golub 	struct proc *p;
24779732458fSMikolaj Golub 	vm_offset_t ps_strings;
24789732458fSMikolaj Golub 	int error;
24799732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32
24809732458fSMikolaj Golub 	uint32_t ps_strings32;
24819732458fSMikolaj Golub #endif
24829732458fSMikolaj Golub 
24839732458fSMikolaj Golub 	if (namelen != 1)
24849732458fSMikolaj Golub 		return (EINVAL);
24859732458fSMikolaj Golub 
2486fa3935bcSMikolaj Golub 	error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2487fa3935bcSMikolaj Golub 	if (error != 0)
24889732458fSMikolaj Golub 		return (error);
24899732458fSMikolaj Golub #ifdef COMPAT_FREEBSD32
24909732458fSMikolaj Golub 	if ((req->flags & SCTL_MASK32) != 0) {
24919732458fSMikolaj Golub 		/*
24929732458fSMikolaj Golub 		 * We return 0 if the 32 bit emulation request is for a 64 bit
24939732458fSMikolaj Golub 		 * process.
24949732458fSMikolaj Golub 		 */
24959732458fSMikolaj Golub 		ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
24969732458fSMikolaj Golub 		    PTROUT(p->p_sysent->sv_psstrings) : 0;
24979732458fSMikolaj Golub 		PROC_UNLOCK(p);
24989732458fSMikolaj Golub 		error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
24999732458fSMikolaj Golub 		return (error);
25009732458fSMikolaj Golub 	}
25019732458fSMikolaj Golub #endif
25029732458fSMikolaj Golub 	ps_strings = p->p_sysent->sv_psstrings;
25039732458fSMikolaj Golub 	PROC_UNLOCK(p);
25049732458fSMikolaj Golub 	error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
25059732458fSMikolaj Golub 	return (error);
25069732458fSMikolaj Golub }
25079732458fSMikolaj Golub 
25086ce13747SMikolaj Golub /*
2509e0fcf639SMikolaj Golub  * This sysctl allows a process to retrieve umask of another process.
25106ce13747SMikolaj Golub  */
25116ce13747SMikolaj Golub static int
25126ce13747SMikolaj Golub sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
25136ce13747SMikolaj Golub {
25146ce13747SMikolaj Golub 	int *name = (int *)arg1;
25156ce13747SMikolaj Golub 	u_int namelen = arg2;
25166ce13747SMikolaj Golub 	struct proc *p;
25176ce13747SMikolaj Golub 	int error;
25186ce13747SMikolaj Golub 	u_short fd_cmask;
25196ce13747SMikolaj Golub 
25206ce13747SMikolaj Golub 	if (namelen != 1)
25216ce13747SMikolaj Golub 		return (EINVAL);
25226ce13747SMikolaj Golub 
25236ce13747SMikolaj Golub 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
25246ce13747SMikolaj Golub 	if (error != 0)
25256ce13747SMikolaj Golub 		return (error);
25266ce13747SMikolaj Golub 
25276ce13747SMikolaj Golub 	FILEDESC_SLOCK(p->p_fd);
25286ce13747SMikolaj Golub 	fd_cmask = p->p_fd->fd_cmask;
25296ce13747SMikolaj Golub 	FILEDESC_SUNLOCK(p->p_fd);
25306ce13747SMikolaj Golub 	PRELE(p);
2531e0fcf639SMikolaj Golub 	error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
25326ce13747SMikolaj Golub 	return (error);
25336ce13747SMikolaj Golub }
25346ce13747SMikolaj Golub 
2535903712c9SMikolaj Golub /*
2536903712c9SMikolaj Golub  * This sysctl allows a process to set and retrieve binary osreldate of
2537903712c9SMikolaj Golub  * another process.
2538903712c9SMikolaj Golub  */
2539903712c9SMikolaj Golub static int
2540903712c9SMikolaj Golub sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2541903712c9SMikolaj Golub {
2542903712c9SMikolaj Golub 	int *name = (int *)arg1;
2543903712c9SMikolaj Golub 	u_int namelen = arg2;
2544903712c9SMikolaj Golub 	struct proc *p;
2545903712c9SMikolaj Golub 	int flags, error, osrel;
2546903712c9SMikolaj Golub 
2547903712c9SMikolaj Golub 	if (namelen != 1)
2548903712c9SMikolaj Golub 		return (EINVAL);
2549903712c9SMikolaj Golub 
2550903712c9SMikolaj Golub 	if (req->newptr != NULL && req->newlen != sizeof(osrel))
2551903712c9SMikolaj Golub 		return (EINVAL);
2552903712c9SMikolaj Golub 
2553903712c9SMikolaj Golub 	flags = PGET_HOLD | PGET_NOTWEXIT;
2554903712c9SMikolaj Golub 	if (req->newptr != NULL)
2555903712c9SMikolaj Golub 		flags |= PGET_CANDEBUG;
2556903712c9SMikolaj Golub 	else
2557903712c9SMikolaj Golub 		flags |= PGET_CANSEE;
2558903712c9SMikolaj Golub 	error = pget((pid_t)name[0], flags, &p);
2559903712c9SMikolaj Golub 	if (error != 0)
2560903712c9SMikolaj Golub 		return (error);
2561903712c9SMikolaj Golub 
2562903712c9SMikolaj Golub 	error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2563903712c9SMikolaj Golub 	if (error != 0)
2564903712c9SMikolaj Golub 		goto errout;
2565903712c9SMikolaj Golub 
2566903712c9SMikolaj Golub 	if (req->newptr != NULL) {
2567903712c9SMikolaj Golub 		error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2568903712c9SMikolaj Golub 		if (error != 0)
2569903712c9SMikolaj Golub 			goto errout;
2570903712c9SMikolaj Golub 		if (osrel < 0) {
2571903712c9SMikolaj Golub 			error = EINVAL;
2572903712c9SMikolaj Golub 			goto errout;
2573903712c9SMikolaj Golub 		}
2574903712c9SMikolaj Golub 		p->p_osrel = osrel;
2575903712c9SMikolaj Golub 	}
2576903712c9SMikolaj Golub errout:
2577903712c9SMikolaj Golub 	PRELE(p);
2578903712c9SMikolaj Golub 	return (error);
2579903712c9SMikolaj Golub }
2580903712c9SMikolaj Golub 
2581cc43c38cSRobert Watson SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
25823ce93e4eSPoul-Henning Kamp 
258324f87fdbSJohn Baldwin SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
258424f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
258524f87fdbSJohn Baldwin 	"Return entire process table");
25863ce93e4eSPoul-Henning Kamp 
258724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2588078842c5SGarance A Drosehn 	sysctl_kern_proc, "Process table");
2589078842c5SGarance A Drosehn 
259024f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
25913ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
25923ce93e4eSPoul-Henning Kamp 
259324f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2594b8fdc89dSGarance A Drosehn 	sysctl_kern_proc, "Process table");
2595b8fdc89dSGarance A Drosehn 
259624f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
259724f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
259824f87fdbSJohn Baldwin 
259924f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2600b8fdc89dSGarance A Drosehn 	sysctl_kern_proc, "Process table");
2601b8fdc89dSGarance A Drosehn 
260224f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
26033ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
26043ce93e4eSPoul-Henning Kamp 
260524f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
26063ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
26073ce93e4eSPoul-Henning Kamp 
260824f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
26093ce93e4eSPoul-Henning Kamp 	sysctl_kern_proc, "Process table");
26103ce93e4eSPoul-Henning Kamp 
261124f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
261230c6f34eSScott Long 	sysctl_kern_proc, "Return process table, no threads");
261330c6f34eSScott Long 
26140c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
261524f87fdbSJohn Baldwin 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
26169b6d9dbaSPoul-Henning Kamp 	sysctl_kern_proc_args, "Process argument list");
2617baf731e6SRobert Drehmel 
261845efc9b4SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2619c5cfcb1cSMikolaj Golub 	sysctl_kern_proc_env, "Process environment");
2620c5cfcb1cSMikolaj Golub 
262145efc9b4SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
262245efc9b4SMikolaj Golub 	CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2623c5cfcb1cSMikolaj Golub 
262424f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
262524f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2626fe769cddSDavid Schultz 
262724f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
262824f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
262924f87fdbSJohn Baldwin 	"Process syscall vector name (ABI type)");
26302648efa6SDaniel Eischen 
26310c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
263224f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2633078842c5SGarance A Drosehn 
26340c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
263524f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
26362648efa6SDaniel Eischen 
26370c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
263824f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2639b8fdc89dSGarance A Drosehn 
26400c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
264124f87fdbSJohn Baldwin 	sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
26420c898376SPoul-Henning Kamp 
26430c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
264424f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2645b8fdc89dSGarance A Drosehn 
26460c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
264724f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
26482648efa6SDaniel Eischen 
26490c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
265024f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
26512648efa6SDaniel Eischen 
26520c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
265324f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
26542648efa6SDaniel Eischen 
26550c898376SPoul-Henning Kamp static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
265624f87fdbSJohn Baldwin 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
265724f87fdbSJohn Baldwin 	"Return process table, no threads");
2658cc43c38cSRobert Watson 
265943151ee6SPeter Wemm #ifdef COMPAT_FREEBSD7
266024f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
266124f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
266243151ee6SPeter Wemm #endif
266343151ee6SPeter Wemm 
266424f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
266524f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
26661cc8c45cSRobert Watson 
26671cc8c45cSRobert Watson #if defined(STACK) || defined(DDB)
266824f87fdbSJohn Baldwin static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
266924f87fdbSJohn Baldwin 	CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
26701cc8c45cSRobert Watson #endif
2671254d03c5SBrooks Davis 
2672254d03c5SBrooks Davis static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2673254d03c5SBrooks Davis 	CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
26749e7d0583SMikolaj Golub 
26758854fe39SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
26768854fe39SMikolaj Golub 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
26778854fe39SMikolaj Golub 	"Process resource limits");
26789732458fSMikolaj Golub 
267945efc9b4SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
268045efc9b4SMikolaj Golub 	CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
268145efc9b4SMikolaj Golub 	"Process ps_strings location");
26826ce13747SMikolaj Golub 
2683e0fcf639SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2684e0fcf639SMikolaj Golub 	CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2685903712c9SMikolaj Golub 
2686903712c9SMikolaj Golub static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2687903712c9SMikolaj Golub 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2688903712c9SMikolaj Golub 	"Process binary osreldate");
2689