xref: /freebsd/sys/kern/kern_proc.c (revision f18d3c411697ff46d85e579a72be54ca0cc67dd0)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 #include "opt_ddb.h"
37 #include "opt_ktrace.h"
38 #include "opt_kstack_pages.h"
39 #include "opt_stack.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/elf.h>
44 #include <sys/exec.h>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
47 #include <sys/lock.h>
48 #include <sys/loginclass.h>
49 #include <sys/malloc.h>
50 #include <sys/mman.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/ptrace.h>
55 #include <sys/refcount.h>
56 #include <sys/resourcevar.h>
57 #include <sys/rwlock.h>
58 #include <sys/sbuf.h>
59 #include <sys/sysent.h>
60 #include <sys/sched.h>
61 #include <sys/smp.h>
62 #include <sys/stack.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/filedesc.h>
66 #include <sys/tty.h>
67 #include <sys/signalvar.h>
68 #include <sys/sdt.h>
69 #include <sys/sx.h>
70 #include <sys/user.h>
71 #include <sys/jail.h>
72 #include <sys/vnode.h>
73 #include <sys/eventhandler.h>
74 
75 #ifdef DDB
76 #include <ddb/ddb.h>
77 #endif
78 
79 #include <vm/vm.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_extern.h>
82 #include <vm/pmap.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/uma.h>
87 
88 #ifdef COMPAT_FREEBSD32
89 #include <compat/freebsd32/freebsd32.h>
90 #include <compat/freebsd32/freebsd32_util.h>
91 #endif
92 
93 SDT_PROVIDER_DEFINE(proc);
94 SDT_PROBE_DEFINE4(proc, kernel, ctor, entry, entry, "struct proc *", "int",
95     "void *", "int");
96 SDT_PROBE_DEFINE4(proc, kernel, ctor, return, return, "struct proc *", "int",
97     "void *", "int");
98 SDT_PROBE_DEFINE4(proc, kernel, dtor, entry, entry, "struct proc *", "int",
99     "void *", "struct thread *");
100 SDT_PROBE_DEFINE3(proc, kernel, dtor, return, return, "struct proc *", "int",
101     "void *");
102 SDT_PROBE_DEFINE3(proc, kernel, init, entry, entry, "struct proc *", "int",
103     "int");
104 SDT_PROBE_DEFINE3(proc, kernel, init, return, return, "struct proc *", "int",
105     "int");
106 
107 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
108 MALLOC_DEFINE(M_SESSION, "session", "session header");
109 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
110 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
111 
112 static void doenterpgrp(struct proc *, struct pgrp *);
113 static void orphanpg(struct pgrp *pg);
114 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
115 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
116 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
117     int preferthread);
118 static void pgadjustjobc(struct pgrp *pgrp, int entering);
119 static void pgdelete(struct pgrp *);
120 static int proc_ctor(void *mem, int size, void *arg, int flags);
121 static void proc_dtor(void *mem, int size, void *arg);
122 static int proc_init(void *mem, int size, int flags);
123 static void proc_fini(void *mem, int size);
124 static void pargs_free(struct pargs *pa);
125 static struct proc *zpfind_locked(pid_t pid);
126 
127 /*
128  * Other process lists
129  */
130 struct pidhashhead *pidhashtbl;
131 u_long pidhash;
132 struct pgrphashhead *pgrphashtbl;
133 u_long pgrphash;
134 struct proclist allproc;
135 struct proclist zombproc;
136 struct sx allproc_lock;
137 struct sx proctree_lock;
138 struct mtx ppeers_lock;
139 uma_zone_t proc_zone;
140 
141 int kstack_pages = KSTACK_PAGES;
142 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
143     "Kernel stack size in pages");
144 
145 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
146 #ifdef COMPAT_FREEBSD32
147 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
148 #endif
149 
150 /*
151  * Initialize global process hashing structures.
152  */
153 void
154 procinit()
155 {
156 
157 	sx_init(&allproc_lock, "allproc");
158 	sx_init(&proctree_lock, "proctree");
159 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
160 	LIST_INIT(&allproc);
161 	LIST_INIT(&zombproc);
162 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
163 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
164 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
165 	    proc_ctor, proc_dtor, proc_init, proc_fini,
166 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
167 	uihashinit();
168 }
169 
170 /*
171  * Prepare a proc for use.
172  */
173 static int
174 proc_ctor(void *mem, int size, void *arg, int flags)
175 {
176 	struct proc *p;
177 
178 	p = (struct proc *)mem;
179 	SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
180 	EVENTHANDLER_INVOKE(process_ctor, p);
181 	SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
182 	return (0);
183 }
184 
185 /*
186  * Reclaim a proc after use.
187  */
188 static void
189 proc_dtor(void *mem, int size, void *arg)
190 {
191 	struct proc *p;
192 	struct thread *td;
193 
194 	/* INVARIANTS checks go here */
195 	p = (struct proc *)mem;
196 	td = FIRST_THREAD_IN_PROC(p);
197 	SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
198 	if (td != NULL) {
199 #ifdef INVARIANTS
200 		KASSERT((p->p_numthreads == 1),
201 		    ("bad number of threads in exiting process"));
202 		KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
203 #endif
204 		/* Free all OSD associated to this thread. */
205 		osd_thread_exit(td);
206 	}
207 	EVENTHANDLER_INVOKE(process_dtor, p);
208 	if (p->p_ksi != NULL)
209 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
210 	SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
211 }
212 
213 /*
214  * Initialize type-stable parts of a proc (when newly created).
215  */
216 static int
217 proc_init(void *mem, int size, int flags)
218 {
219 	struct proc *p;
220 
221 	p = (struct proc *)mem;
222 	SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
223 	p->p_sched = (struct p_sched *)&p[1];
224 	bzero(&p->p_mtx, sizeof(struct mtx));
225 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
226 	mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
227 	cv_init(&p->p_pwait, "ppwait");
228 	cv_init(&p->p_dbgwait, "dbgwait");
229 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
230 	EVENTHANDLER_INVOKE(process_init, p);
231 	p->p_stats = pstats_alloc();
232 	SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
233 	return (0);
234 }
235 
236 /*
237  * UMA should ensure that this function is never called.
238  * Freeing a proc structure would violate type stability.
239  */
240 static void
241 proc_fini(void *mem, int size)
242 {
243 #ifdef notnow
244 	struct proc *p;
245 
246 	p = (struct proc *)mem;
247 	EVENTHANDLER_INVOKE(process_fini, p);
248 	pstats_free(p->p_stats);
249 	thread_free(FIRST_THREAD_IN_PROC(p));
250 	mtx_destroy(&p->p_mtx);
251 	if (p->p_ksi != NULL)
252 		ksiginfo_free(p->p_ksi);
253 #else
254 	panic("proc reclaimed");
255 #endif
256 }
257 
258 /*
259  * Is p an inferior of the current process?
260  */
261 int
262 inferior(p)
263 	register struct proc *p;
264 {
265 
266 	sx_assert(&proctree_lock, SX_LOCKED);
267 	for (; p != curproc; p = p->p_pptr)
268 		if (p->p_pid == 0)
269 			return (0);
270 	return (1);
271 }
272 
273 struct proc *
274 pfind_locked(pid_t pid)
275 {
276 	struct proc *p;
277 
278 	sx_assert(&allproc_lock, SX_LOCKED);
279 	LIST_FOREACH(p, PIDHASH(pid), p_hash) {
280 		if (p->p_pid == pid) {
281 			PROC_LOCK(p);
282 			if (p->p_state == PRS_NEW) {
283 				PROC_UNLOCK(p);
284 				p = NULL;
285 			}
286 			break;
287 		}
288 	}
289 	return (p);
290 }
291 
292 /*
293  * Locate a process by number; return only "live" processes -- i.e., neither
294  * zombies nor newly born but incompletely initialized processes.  By not
295  * returning processes in the PRS_NEW state, we allow callers to avoid
296  * testing for that condition to avoid dereferencing p_ucred, et al.
297  */
298 struct proc *
299 pfind(pid_t pid)
300 {
301 	struct proc *p;
302 
303 	sx_slock(&allproc_lock);
304 	p = pfind_locked(pid);
305 	sx_sunlock(&allproc_lock);
306 	return (p);
307 }
308 
309 static struct proc *
310 pfind_tid_locked(pid_t tid)
311 {
312 	struct proc *p;
313 	struct thread *td;
314 
315 	sx_assert(&allproc_lock, SX_LOCKED);
316 	FOREACH_PROC_IN_SYSTEM(p) {
317 		PROC_LOCK(p);
318 		if (p->p_state == PRS_NEW) {
319 			PROC_UNLOCK(p);
320 			continue;
321 		}
322 		FOREACH_THREAD_IN_PROC(p, td) {
323 			if (td->td_tid == tid)
324 				goto found;
325 		}
326 		PROC_UNLOCK(p);
327 	}
328 found:
329 	return (p);
330 }
331 
332 /*
333  * Locate a process group by number.
334  * The caller must hold proctree_lock.
335  */
336 struct pgrp *
337 pgfind(pgid)
338 	register pid_t pgid;
339 {
340 	register struct pgrp *pgrp;
341 
342 	sx_assert(&proctree_lock, SX_LOCKED);
343 
344 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
345 		if (pgrp->pg_id == pgid) {
346 			PGRP_LOCK(pgrp);
347 			return (pgrp);
348 		}
349 	}
350 	return (NULL);
351 }
352 
353 /*
354  * Locate process and do additional manipulations, depending on flags.
355  */
356 int
357 pget(pid_t pid, int flags, struct proc **pp)
358 {
359 	struct proc *p;
360 	int error;
361 
362 	sx_slock(&allproc_lock);
363 	if (pid <= PID_MAX) {
364 		p = pfind_locked(pid);
365 		if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
366 			p = zpfind_locked(pid);
367 	} else if ((flags & PGET_NOTID) == 0) {
368 		p = pfind_tid_locked(pid);
369 	} else {
370 		p = NULL;
371 	}
372 	sx_sunlock(&allproc_lock);
373 	if (p == NULL)
374 		return (ESRCH);
375 	if ((flags & PGET_CANSEE) != 0) {
376 		error = p_cansee(curthread, p);
377 		if (error != 0)
378 			goto errout;
379 	}
380 	if ((flags & PGET_CANDEBUG) != 0) {
381 		error = p_candebug(curthread, p);
382 		if (error != 0)
383 			goto errout;
384 	}
385 	if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
386 		error = EPERM;
387 		goto errout;
388 	}
389 	if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
390 		error = ESRCH;
391 		goto errout;
392 	}
393 	if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
394 		/*
395 		 * XXXRW: Not clear ESRCH is the right error during proc
396 		 * execve().
397 		 */
398 		error = ESRCH;
399 		goto errout;
400 	}
401 	if ((flags & PGET_HOLD) != 0) {
402 		_PHOLD(p);
403 		PROC_UNLOCK(p);
404 	}
405 	*pp = p;
406 	return (0);
407 errout:
408 	PROC_UNLOCK(p);
409 	return (error);
410 }
411 
412 /*
413  * Create a new process group.
414  * pgid must be equal to the pid of p.
415  * Begin a new session if required.
416  */
417 int
418 enterpgrp(p, pgid, pgrp, sess)
419 	register struct proc *p;
420 	pid_t pgid;
421 	struct pgrp *pgrp;
422 	struct session *sess;
423 {
424 
425 	sx_assert(&proctree_lock, SX_XLOCKED);
426 
427 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
428 	KASSERT(p->p_pid == pgid,
429 	    ("enterpgrp: new pgrp and pid != pgid"));
430 	KASSERT(pgfind(pgid) == NULL,
431 	    ("enterpgrp: pgrp with pgid exists"));
432 	KASSERT(!SESS_LEADER(p),
433 	    ("enterpgrp: session leader attempted setpgrp"));
434 
435 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
436 
437 	if (sess != NULL) {
438 		/*
439 		 * new session
440 		 */
441 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
442 		PROC_LOCK(p);
443 		p->p_flag &= ~P_CONTROLT;
444 		PROC_UNLOCK(p);
445 		PGRP_LOCK(pgrp);
446 		sess->s_leader = p;
447 		sess->s_sid = p->p_pid;
448 		refcount_init(&sess->s_count, 1);
449 		sess->s_ttyvp = NULL;
450 		sess->s_ttydp = NULL;
451 		sess->s_ttyp = NULL;
452 		bcopy(p->p_session->s_login, sess->s_login,
453 			    sizeof(sess->s_login));
454 		pgrp->pg_session = sess;
455 		KASSERT(p == curproc,
456 		    ("enterpgrp: mksession and p != curproc"));
457 	} else {
458 		pgrp->pg_session = p->p_session;
459 		sess_hold(pgrp->pg_session);
460 		PGRP_LOCK(pgrp);
461 	}
462 	pgrp->pg_id = pgid;
463 	LIST_INIT(&pgrp->pg_members);
464 
465 	/*
466 	 * As we have an exclusive lock of proctree_lock,
467 	 * this should not deadlock.
468 	 */
469 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
470 	pgrp->pg_jobc = 0;
471 	SLIST_INIT(&pgrp->pg_sigiolst);
472 	PGRP_UNLOCK(pgrp);
473 
474 	doenterpgrp(p, pgrp);
475 
476 	return (0);
477 }
478 
479 /*
480  * Move p to an existing process group
481  */
482 int
483 enterthispgrp(p, pgrp)
484 	register struct proc *p;
485 	struct pgrp *pgrp;
486 {
487 
488 	sx_assert(&proctree_lock, SX_XLOCKED);
489 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
490 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
491 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
492 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
493 	KASSERT(pgrp->pg_session == p->p_session,
494 		("%s: pgrp's session %p, p->p_session %p.\n",
495 		__func__,
496 		pgrp->pg_session,
497 		p->p_session));
498 	KASSERT(pgrp != p->p_pgrp,
499 		("%s: p belongs to pgrp.", __func__));
500 
501 	doenterpgrp(p, pgrp);
502 
503 	return (0);
504 }
505 
506 /*
507  * Move p to a process group
508  */
509 static void
510 doenterpgrp(p, pgrp)
511 	struct proc *p;
512 	struct pgrp *pgrp;
513 {
514 	struct pgrp *savepgrp;
515 
516 	sx_assert(&proctree_lock, SX_XLOCKED);
517 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
518 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
519 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
520 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
521 
522 	savepgrp = p->p_pgrp;
523 
524 	/*
525 	 * Adjust eligibility of affected pgrps to participate in job control.
526 	 * Increment eligibility counts before decrementing, otherwise we
527 	 * could reach 0 spuriously during the first call.
528 	 */
529 	fixjobc(p, pgrp, 1);
530 	fixjobc(p, p->p_pgrp, 0);
531 
532 	PGRP_LOCK(pgrp);
533 	PGRP_LOCK(savepgrp);
534 	PROC_LOCK(p);
535 	LIST_REMOVE(p, p_pglist);
536 	p->p_pgrp = pgrp;
537 	PROC_UNLOCK(p);
538 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
539 	PGRP_UNLOCK(savepgrp);
540 	PGRP_UNLOCK(pgrp);
541 	if (LIST_EMPTY(&savepgrp->pg_members))
542 		pgdelete(savepgrp);
543 }
544 
545 /*
546  * remove process from process group
547  */
548 int
549 leavepgrp(p)
550 	register struct proc *p;
551 {
552 	struct pgrp *savepgrp;
553 
554 	sx_assert(&proctree_lock, SX_XLOCKED);
555 	savepgrp = p->p_pgrp;
556 	PGRP_LOCK(savepgrp);
557 	PROC_LOCK(p);
558 	LIST_REMOVE(p, p_pglist);
559 	p->p_pgrp = NULL;
560 	PROC_UNLOCK(p);
561 	PGRP_UNLOCK(savepgrp);
562 	if (LIST_EMPTY(&savepgrp->pg_members))
563 		pgdelete(savepgrp);
564 	return (0);
565 }
566 
567 /*
568  * delete a process group
569  */
570 static void
571 pgdelete(pgrp)
572 	register struct pgrp *pgrp;
573 {
574 	struct session *savesess;
575 	struct tty *tp;
576 
577 	sx_assert(&proctree_lock, SX_XLOCKED);
578 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
579 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
580 
581 	/*
582 	 * Reset any sigio structures pointing to us as a result of
583 	 * F_SETOWN with our pgid.
584 	 */
585 	funsetownlst(&pgrp->pg_sigiolst);
586 
587 	PGRP_LOCK(pgrp);
588 	tp = pgrp->pg_session->s_ttyp;
589 	LIST_REMOVE(pgrp, pg_hash);
590 	savesess = pgrp->pg_session;
591 	PGRP_UNLOCK(pgrp);
592 
593 	/* Remove the reference to the pgrp before deallocating it. */
594 	if (tp != NULL) {
595 		tty_lock(tp);
596 		tty_rel_pgrp(tp, pgrp);
597 	}
598 
599 	mtx_destroy(&pgrp->pg_mtx);
600 	free(pgrp, M_PGRP);
601 	sess_release(savesess);
602 }
603 
604 static void
605 pgadjustjobc(pgrp, entering)
606 	struct pgrp *pgrp;
607 	int entering;
608 {
609 
610 	PGRP_LOCK(pgrp);
611 	if (entering)
612 		pgrp->pg_jobc++;
613 	else {
614 		--pgrp->pg_jobc;
615 		if (pgrp->pg_jobc == 0)
616 			orphanpg(pgrp);
617 	}
618 	PGRP_UNLOCK(pgrp);
619 }
620 
621 /*
622  * Adjust pgrp jobc counters when specified process changes process group.
623  * We count the number of processes in each process group that "qualify"
624  * the group for terminal job control (those with a parent in a different
625  * process group of the same session).  If that count reaches zero, the
626  * process group becomes orphaned.  Check both the specified process'
627  * process group and that of its children.
628  * entering == 0 => p is leaving specified group.
629  * entering == 1 => p is entering specified group.
630  */
631 void
632 fixjobc(p, pgrp, entering)
633 	register struct proc *p;
634 	register struct pgrp *pgrp;
635 	int entering;
636 {
637 	register struct pgrp *hispgrp;
638 	register struct session *mysession;
639 
640 	sx_assert(&proctree_lock, SX_LOCKED);
641 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
642 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
643 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
644 
645 	/*
646 	 * Check p's parent to see whether p qualifies its own process
647 	 * group; if so, adjust count for p's process group.
648 	 */
649 	mysession = pgrp->pg_session;
650 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
651 	    hispgrp->pg_session == mysession)
652 		pgadjustjobc(pgrp, entering);
653 
654 	/*
655 	 * Check this process' children to see whether they qualify
656 	 * their process groups; if so, adjust counts for children's
657 	 * process groups.
658 	 */
659 	LIST_FOREACH(p, &p->p_children, p_sibling) {
660 		hispgrp = p->p_pgrp;
661 		if (hispgrp == pgrp ||
662 		    hispgrp->pg_session != mysession)
663 			continue;
664 		PROC_LOCK(p);
665 		if (p->p_state == PRS_ZOMBIE) {
666 			PROC_UNLOCK(p);
667 			continue;
668 		}
669 		PROC_UNLOCK(p);
670 		pgadjustjobc(hispgrp, entering);
671 	}
672 }
673 
674 /*
675  * A process group has become orphaned;
676  * if there are any stopped processes in the group,
677  * hang-up all process in that group.
678  */
679 static void
680 orphanpg(pg)
681 	struct pgrp *pg;
682 {
683 	register struct proc *p;
684 
685 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
686 
687 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
688 		PROC_LOCK(p);
689 		if (P_SHOULDSTOP(p)) {
690 			PROC_UNLOCK(p);
691 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
692 				PROC_LOCK(p);
693 				kern_psignal(p, SIGHUP);
694 				kern_psignal(p, SIGCONT);
695 				PROC_UNLOCK(p);
696 			}
697 			return;
698 		}
699 		PROC_UNLOCK(p);
700 	}
701 }
702 
703 void
704 sess_hold(struct session *s)
705 {
706 
707 	refcount_acquire(&s->s_count);
708 }
709 
710 void
711 sess_release(struct session *s)
712 {
713 
714 	if (refcount_release(&s->s_count)) {
715 		if (s->s_ttyp != NULL) {
716 			tty_lock(s->s_ttyp);
717 			tty_rel_sess(s->s_ttyp, s);
718 		}
719 		mtx_destroy(&s->s_mtx);
720 		free(s, M_SESSION);
721 	}
722 }
723 
724 #ifdef DDB
725 
726 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
727 {
728 	register struct pgrp *pgrp;
729 	register struct proc *p;
730 	register int i;
731 
732 	for (i = 0; i <= pgrphash; i++) {
733 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
734 			printf("\tindx %d\n", i);
735 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
736 				printf(
737 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
738 				    (void *)pgrp, (long)pgrp->pg_id,
739 				    (void *)pgrp->pg_session,
740 				    pgrp->pg_session->s_count,
741 				    (void *)LIST_FIRST(&pgrp->pg_members));
742 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
743 					printf("\t\tpid %ld addr %p pgrp %p\n",
744 					    (long)p->p_pid, (void *)p,
745 					    (void *)p->p_pgrp);
746 				}
747 			}
748 		}
749 	}
750 }
751 #endif /* DDB */
752 
753 /*
754  * Calculate the kinfo_proc members which contain process-wide
755  * informations.
756  * Must be called with the target process locked.
757  */
758 static void
759 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
760 {
761 	struct thread *td;
762 
763 	PROC_LOCK_ASSERT(p, MA_OWNED);
764 
765 	kp->ki_estcpu = 0;
766 	kp->ki_pctcpu = 0;
767 	FOREACH_THREAD_IN_PROC(p, td) {
768 		thread_lock(td);
769 		kp->ki_pctcpu += sched_pctcpu(td);
770 		kp->ki_estcpu += td->td_estcpu;
771 		thread_unlock(td);
772 	}
773 }
774 
775 /*
776  * Clear kinfo_proc and fill in any information that is common
777  * to all threads in the process.
778  * Must be called with the target process locked.
779  */
780 static void
781 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
782 {
783 	struct thread *td0;
784 	struct tty *tp;
785 	struct session *sp;
786 	struct ucred *cred;
787 	struct sigacts *ps;
788 
789 	PROC_LOCK_ASSERT(p, MA_OWNED);
790 	bzero(kp, sizeof(*kp));
791 
792 	kp->ki_structsize = sizeof(*kp);
793 	kp->ki_paddr = p;
794 	kp->ki_addr =/* p->p_addr; */0; /* XXX */
795 	kp->ki_args = p->p_args;
796 	kp->ki_textvp = p->p_textvp;
797 #ifdef KTRACE
798 	kp->ki_tracep = p->p_tracevp;
799 	kp->ki_traceflag = p->p_traceflag;
800 #endif
801 	kp->ki_fd = p->p_fd;
802 	kp->ki_vmspace = p->p_vmspace;
803 	kp->ki_flag = p->p_flag;
804 	kp->ki_flag2 = p->p_flag2;
805 	cred = p->p_ucred;
806 	if (cred) {
807 		kp->ki_uid = cred->cr_uid;
808 		kp->ki_ruid = cred->cr_ruid;
809 		kp->ki_svuid = cred->cr_svuid;
810 		kp->ki_cr_flags = 0;
811 		if (cred->cr_flags & CRED_FLAG_CAPMODE)
812 			kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
813 		/* XXX bde doesn't like KI_NGROUPS */
814 		if (cred->cr_ngroups > KI_NGROUPS) {
815 			kp->ki_ngroups = KI_NGROUPS;
816 			kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
817 		} else
818 			kp->ki_ngroups = cred->cr_ngroups;
819 		bcopy(cred->cr_groups, kp->ki_groups,
820 		    kp->ki_ngroups * sizeof(gid_t));
821 		kp->ki_rgid = cred->cr_rgid;
822 		kp->ki_svgid = cred->cr_svgid;
823 		/* If jailed(cred), emulate the old P_JAILED flag. */
824 		if (jailed(cred)) {
825 			kp->ki_flag |= P_JAILED;
826 			/* If inside the jail, use 0 as a jail ID. */
827 			if (cred->cr_prison != curthread->td_ucred->cr_prison)
828 				kp->ki_jid = cred->cr_prison->pr_id;
829 		}
830 		strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
831 		    sizeof(kp->ki_loginclass));
832 	}
833 	ps = p->p_sigacts;
834 	if (ps) {
835 		mtx_lock(&ps->ps_mtx);
836 		kp->ki_sigignore = ps->ps_sigignore;
837 		kp->ki_sigcatch = ps->ps_sigcatch;
838 		mtx_unlock(&ps->ps_mtx);
839 	}
840 	if (p->p_state != PRS_NEW &&
841 	    p->p_state != PRS_ZOMBIE &&
842 	    p->p_vmspace != NULL) {
843 		struct vmspace *vm = p->p_vmspace;
844 
845 		kp->ki_size = vm->vm_map.size;
846 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
847 		FOREACH_THREAD_IN_PROC(p, td0) {
848 			if (!TD_IS_SWAPPED(td0))
849 				kp->ki_rssize += td0->td_kstack_pages;
850 		}
851 		kp->ki_swrss = vm->vm_swrss;
852 		kp->ki_tsize = vm->vm_tsize;
853 		kp->ki_dsize = vm->vm_dsize;
854 		kp->ki_ssize = vm->vm_ssize;
855 	} else if (p->p_state == PRS_ZOMBIE)
856 		kp->ki_stat = SZOMB;
857 	if (kp->ki_flag & P_INMEM)
858 		kp->ki_sflag = PS_INMEM;
859 	else
860 		kp->ki_sflag = 0;
861 	/* Calculate legacy swtime as seconds since 'swtick'. */
862 	kp->ki_swtime = (ticks - p->p_swtick) / hz;
863 	kp->ki_pid = p->p_pid;
864 	kp->ki_nice = p->p_nice;
865 	kp->ki_fibnum = p->p_fibnum;
866 	kp->ki_start = p->p_stats->p_start;
867 	timevaladd(&kp->ki_start, &boottime);
868 	PROC_SLOCK(p);
869 	rufetch(p, &kp->ki_rusage);
870 	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
871 	calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
872 	PROC_SUNLOCK(p);
873 	calccru(p, &kp->ki_childutime, &kp->ki_childstime);
874 	/* Some callers want child times in a single value. */
875 	kp->ki_childtime = kp->ki_childstime;
876 	timevaladd(&kp->ki_childtime, &kp->ki_childutime);
877 
878 	FOREACH_THREAD_IN_PROC(p, td0)
879 		kp->ki_cow += td0->td_cow;
880 
881 	tp = NULL;
882 	if (p->p_pgrp) {
883 		kp->ki_pgid = p->p_pgrp->pg_id;
884 		kp->ki_jobc = p->p_pgrp->pg_jobc;
885 		sp = p->p_pgrp->pg_session;
886 
887 		if (sp != NULL) {
888 			kp->ki_sid = sp->s_sid;
889 			SESS_LOCK(sp);
890 			strlcpy(kp->ki_login, sp->s_login,
891 			    sizeof(kp->ki_login));
892 			if (sp->s_ttyvp)
893 				kp->ki_kiflag |= KI_CTTY;
894 			if (SESS_LEADER(p))
895 				kp->ki_kiflag |= KI_SLEADER;
896 			/* XXX proctree_lock */
897 			tp = sp->s_ttyp;
898 			SESS_UNLOCK(sp);
899 		}
900 	}
901 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
902 		kp->ki_tdev = tty_udev(tp);
903 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
904 		if (tp->t_session)
905 			kp->ki_tsid = tp->t_session->s_sid;
906 	} else
907 		kp->ki_tdev = NODEV;
908 	if (p->p_comm[0] != '\0')
909 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
910 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
911 	    p->p_sysent->sv_name[0] != '\0')
912 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
913 	kp->ki_siglist = p->p_siglist;
914 	kp->ki_xstat = p->p_xstat;
915 	kp->ki_acflag = p->p_acflag;
916 	kp->ki_lock = p->p_lock;
917 	if (p->p_pptr)
918 		kp->ki_ppid = p->p_pptr->p_pid;
919 }
920 
921 /*
922  * Fill in information that is thread specific.  Must be called with
923  * target process locked.  If 'preferthread' is set, overwrite certain
924  * process-related fields that are maintained for both threads and
925  * processes.
926  */
927 static void
928 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
929 {
930 	struct proc *p;
931 
932 	p = td->td_proc;
933 	kp->ki_tdaddr = td;
934 	PROC_LOCK_ASSERT(p, MA_OWNED);
935 
936 	if (preferthread)
937 		PROC_SLOCK(p);
938 	thread_lock(td);
939 	if (td->td_wmesg != NULL)
940 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
941 	else
942 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
943 	strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
944 	if (TD_ON_LOCK(td)) {
945 		kp->ki_kiflag |= KI_LOCKBLOCK;
946 		strlcpy(kp->ki_lockname, td->td_lockname,
947 		    sizeof(kp->ki_lockname));
948 	} else {
949 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
950 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
951 	}
952 
953 	if (p->p_state == PRS_NORMAL) { /* approximate. */
954 		if (TD_ON_RUNQ(td) ||
955 		    TD_CAN_RUN(td) ||
956 		    TD_IS_RUNNING(td)) {
957 			kp->ki_stat = SRUN;
958 		} else if (P_SHOULDSTOP(p)) {
959 			kp->ki_stat = SSTOP;
960 		} else if (TD_IS_SLEEPING(td)) {
961 			kp->ki_stat = SSLEEP;
962 		} else if (TD_ON_LOCK(td)) {
963 			kp->ki_stat = SLOCK;
964 		} else {
965 			kp->ki_stat = SWAIT;
966 		}
967 	} else if (p->p_state == PRS_ZOMBIE) {
968 		kp->ki_stat = SZOMB;
969 	} else {
970 		kp->ki_stat = SIDL;
971 	}
972 
973 	/* Things in the thread */
974 	kp->ki_wchan = td->td_wchan;
975 	kp->ki_pri.pri_level = td->td_priority;
976 	kp->ki_pri.pri_native = td->td_base_pri;
977 	kp->ki_lastcpu = td->td_lastcpu;
978 	kp->ki_oncpu = td->td_oncpu;
979 	kp->ki_tdflags = td->td_flags;
980 	kp->ki_tid = td->td_tid;
981 	kp->ki_numthreads = p->p_numthreads;
982 	kp->ki_pcb = td->td_pcb;
983 	kp->ki_kstack = (void *)td->td_kstack;
984 	kp->ki_slptime = (ticks - td->td_slptick) / hz;
985 	kp->ki_pri.pri_class = td->td_pri_class;
986 	kp->ki_pri.pri_user = td->td_user_pri;
987 
988 	if (preferthread) {
989 		rufetchtd(td, &kp->ki_rusage);
990 		kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
991 		kp->ki_pctcpu = sched_pctcpu(td);
992 		kp->ki_estcpu = td->td_estcpu;
993 		kp->ki_cow = td->td_cow;
994 	}
995 
996 	/* We can't get this anymore but ps etc never used it anyway. */
997 	kp->ki_rqindex = 0;
998 
999 	if (preferthread)
1000 		kp->ki_siglist = td->td_siglist;
1001 	kp->ki_sigmask = td->td_sigmask;
1002 	thread_unlock(td);
1003 	if (preferthread)
1004 		PROC_SUNLOCK(p);
1005 }
1006 
1007 /*
1008  * Fill in a kinfo_proc structure for the specified process.
1009  * Must be called with the target process locked.
1010  */
1011 void
1012 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1013 {
1014 
1015 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1016 
1017 	fill_kinfo_proc_only(p, kp);
1018 	fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1019 	fill_kinfo_aggregate(p, kp);
1020 }
1021 
1022 struct pstats *
1023 pstats_alloc(void)
1024 {
1025 
1026 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1027 }
1028 
1029 /*
1030  * Copy parts of p_stats; zero the rest of p_stats (statistics).
1031  */
1032 void
1033 pstats_fork(struct pstats *src, struct pstats *dst)
1034 {
1035 
1036 	bzero(&dst->pstat_startzero,
1037 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1038 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1039 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1040 }
1041 
1042 void
1043 pstats_free(struct pstats *ps)
1044 {
1045 
1046 	free(ps, M_SUBPROC);
1047 }
1048 
1049 static struct proc *
1050 zpfind_locked(pid_t pid)
1051 {
1052 	struct proc *p;
1053 
1054 	sx_assert(&allproc_lock, SX_LOCKED);
1055 	LIST_FOREACH(p, &zombproc, p_list) {
1056 		if (p->p_pid == pid) {
1057 			PROC_LOCK(p);
1058 			break;
1059 		}
1060 	}
1061 	return (p);
1062 }
1063 
1064 /*
1065  * Locate a zombie process by number
1066  */
1067 struct proc *
1068 zpfind(pid_t pid)
1069 {
1070 	struct proc *p;
1071 
1072 	sx_slock(&allproc_lock);
1073 	p = zpfind_locked(pid);
1074 	sx_sunlock(&allproc_lock);
1075 	return (p);
1076 }
1077 
1078 #ifdef COMPAT_FREEBSD32
1079 
1080 /*
1081  * This function is typically used to copy out the kernel address, so
1082  * it can be replaced by assignment of zero.
1083  */
1084 static inline uint32_t
1085 ptr32_trim(void *ptr)
1086 {
1087 	uintptr_t uptr;
1088 
1089 	uptr = (uintptr_t)ptr;
1090 	return ((uptr > UINT_MAX) ? 0 : uptr);
1091 }
1092 
1093 #define PTRTRIM_CP(src,dst,fld) \
1094 	do { (dst).fld = ptr32_trim((src).fld); } while (0)
1095 
1096 static void
1097 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1098 {
1099 	int i;
1100 
1101 	bzero(ki32, sizeof(struct kinfo_proc32));
1102 	ki32->ki_structsize = sizeof(struct kinfo_proc32);
1103 	CP(*ki, *ki32, ki_layout);
1104 	PTRTRIM_CP(*ki, *ki32, ki_args);
1105 	PTRTRIM_CP(*ki, *ki32, ki_paddr);
1106 	PTRTRIM_CP(*ki, *ki32, ki_addr);
1107 	PTRTRIM_CP(*ki, *ki32, ki_tracep);
1108 	PTRTRIM_CP(*ki, *ki32, ki_textvp);
1109 	PTRTRIM_CP(*ki, *ki32, ki_fd);
1110 	PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1111 	PTRTRIM_CP(*ki, *ki32, ki_wchan);
1112 	CP(*ki, *ki32, ki_pid);
1113 	CP(*ki, *ki32, ki_ppid);
1114 	CP(*ki, *ki32, ki_pgid);
1115 	CP(*ki, *ki32, ki_tpgid);
1116 	CP(*ki, *ki32, ki_sid);
1117 	CP(*ki, *ki32, ki_tsid);
1118 	CP(*ki, *ki32, ki_jobc);
1119 	CP(*ki, *ki32, ki_tdev);
1120 	CP(*ki, *ki32, ki_siglist);
1121 	CP(*ki, *ki32, ki_sigmask);
1122 	CP(*ki, *ki32, ki_sigignore);
1123 	CP(*ki, *ki32, ki_sigcatch);
1124 	CP(*ki, *ki32, ki_uid);
1125 	CP(*ki, *ki32, ki_ruid);
1126 	CP(*ki, *ki32, ki_svuid);
1127 	CP(*ki, *ki32, ki_rgid);
1128 	CP(*ki, *ki32, ki_svgid);
1129 	CP(*ki, *ki32, ki_ngroups);
1130 	for (i = 0; i < KI_NGROUPS; i++)
1131 		CP(*ki, *ki32, ki_groups[i]);
1132 	CP(*ki, *ki32, ki_size);
1133 	CP(*ki, *ki32, ki_rssize);
1134 	CP(*ki, *ki32, ki_swrss);
1135 	CP(*ki, *ki32, ki_tsize);
1136 	CP(*ki, *ki32, ki_dsize);
1137 	CP(*ki, *ki32, ki_ssize);
1138 	CP(*ki, *ki32, ki_xstat);
1139 	CP(*ki, *ki32, ki_acflag);
1140 	CP(*ki, *ki32, ki_pctcpu);
1141 	CP(*ki, *ki32, ki_estcpu);
1142 	CP(*ki, *ki32, ki_slptime);
1143 	CP(*ki, *ki32, ki_swtime);
1144 	CP(*ki, *ki32, ki_cow);
1145 	CP(*ki, *ki32, ki_runtime);
1146 	TV_CP(*ki, *ki32, ki_start);
1147 	TV_CP(*ki, *ki32, ki_childtime);
1148 	CP(*ki, *ki32, ki_flag);
1149 	CP(*ki, *ki32, ki_kiflag);
1150 	CP(*ki, *ki32, ki_traceflag);
1151 	CP(*ki, *ki32, ki_stat);
1152 	CP(*ki, *ki32, ki_nice);
1153 	CP(*ki, *ki32, ki_lock);
1154 	CP(*ki, *ki32, ki_rqindex);
1155 	CP(*ki, *ki32, ki_oncpu);
1156 	CP(*ki, *ki32, ki_lastcpu);
1157 	bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1158 	bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1159 	bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1160 	bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1161 	bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1162 	bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1163 	bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1164 	CP(*ki, *ki32, ki_flag2);
1165 	CP(*ki, *ki32, ki_fibnum);
1166 	CP(*ki, *ki32, ki_cr_flags);
1167 	CP(*ki, *ki32, ki_jid);
1168 	CP(*ki, *ki32, ki_numthreads);
1169 	CP(*ki, *ki32, ki_tid);
1170 	CP(*ki, *ki32, ki_pri);
1171 	freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1172 	freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1173 	PTRTRIM_CP(*ki, *ki32, ki_pcb);
1174 	PTRTRIM_CP(*ki, *ki32, ki_kstack);
1175 	PTRTRIM_CP(*ki, *ki32, ki_udata);
1176 	CP(*ki, *ki32, ki_sflag);
1177 	CP(*ki, *ki32, ki_tdflags);
1178 }
1179 #endif
1180 
1181 int
1182 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1183 {
1184 	struct thread *td;
1185 	struct kinfo_proc ki;
1186 #ifdef COMPAT_FREEBSD32
1187 	struct kinfo_proc32 ki32;
1188 #endif
1189 	int error;
1190 
1191 	PROC_LOCK_ASSERT(p, MA_OWNED);
1192 	MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1193 
1194 	error = 0;
1195 	fill_kinfo_proc(p, &ki);
1196 	if ((flags & KERN_PROC_NOTHREADS) != 0) {
1197 #ifdef COMPAT_FREEBSD32
1198 		if ((flags & KERN_PROC_MASK32) != 0) {
1199 			freebsd32_kinfo_proc_out(&ki, &ki32);
1200 			error = sbuf_bcat(sb, &ki32, sizeof(ki32));
1201 		} else
1202 #endif
1203 			error = sbuf_bcat(sb, &ki, sizeof(ki));
1204 	} else {
1205 		FOREACH_THREAD_IN_PROC(p, td) {
1206 			fill_kinfo_thread(td, &ki, 1);
1207 #ifdef COMPAT_FREEBSD32
1208 			if ((flags & KERN_PROC_MASK32) != 0) {
1209 				freebsd32_kinfo_proc_out(&ki, &ki32);
1210 				error = sbuf_bcat(sb, &ki32, sizeof(ki32));
1211 			} else
1212 #endif
1213 				error = sbuf_bcat(sb, &ki, sizeof(ki));
1214 			if (error)
1215 				break;
1216 		}
1217 	}
1218 	PROC_UNLOCK(p);
1219 	return (error);
1220 }
1221 
1222 static int
1223 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1224     int doingzomb)
1225 {
1226 	struct sbuf sb;
1227 	struct kinfo_proc ki;
1228 	struct proc *np;
1229 	int error, error2;
1230 	pid_t pid;
1231 
1232 	pid = p->p_pid;
1233 	sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1234 	error = kern_proc_out(p, &sb, flags);
1235 	error2 = sbuf_finish(&sb);
1236 	sbuf_delete(&sb);
1237 	if (error != 0)
1238 		return (error);
1239 	else if (error2 != 0)
1240 		return (error2);
1241 	if (doingzomb)
1242 		np = zpfind(pid);
1243 	else {
1244 		if (pid == 0)
1245 			return (0);
1246 		np = pfind(pid);
1247 	}
1248 	if (np == NULL)
1249 		return (ESRCH);
1250 	if (np != p) {
1251 		PROC_UNLOCK(np);
1252 		return (ESRCH);
1253 	}
1254 	PROC_UNLOCK(np);
1255 	return (0);
1256 }
1257 
1258 static int
1259 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1260 {
1261 	int *name = (int *)arg1;
1262 	u_int namelen = arg2;
1263 	struct proc *p;
1264 	int flags, doingzomb, oid_number;
1265 	int error = 0;
1266 
1267 	oid_number = oidp->oid_number;
1268 	if (oid_number != KERN_PROC_ALL &&
1269 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
1270 		flags = KERN_PROC_NOTHREADS;
1271 	else {
1272 		flags = 0;
1273 		oid_number &= ~KERN_PROC_INC_THREAD;
1274 	}
1275 #ifdef COMPAT_FREEBSD32
1276 	if (req->flags & SCTL_MASK32)
1277 		flags |= KERN_PROC_MASK32;
1278 #endif
1279 	if (oid_number == KERN_PROC_PID) {
1280 		if (namelen != 1)
1281 			return (EINVAL);
1282 		error = sysctl_wire_old_buffer(req, 0);
1283 		if (error)
1284 			return (error);
1285 		error = pget((pid_t)name[0], PGET_CANSEE, &p);
1286 		if (error != 0)
1287 			return (error);
1288 		error = sysctl_out_proc(p, req, flags, 0);
1289 		return (error);
1290 	}
1291 
1292 	switch (oid_number) {
1293 	case KERN_PROC_ALL:
1294 		if (namelen != 0)
1295 			return (EINVAL);
1296 		break;
1297 	case KERN_PROC_PROC:
1298 		if (namelen != 0 && namelen != 1)
1299 			return (EINVAL);
1300 		break;
1301 	default:
1302 		if (namelen != 1)
1303 			return (EINVAL);
1304 		break;
1305 	}
1306 
1307 	if (!req->oldptr) {
1308 		/* overestimate by 5 procs */
1309 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1310 		if (error)
1311 			return (error);
1312 	}
1313 	error = sysctl_wire_old_buffer(req, 0);
1314 	if (error != 0)
1315 		return (error);
1316 	sx_slock(&allproc_lock);
1317 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1318 		if (!doingzomb)
1319 			p = LIST_FIRST(&allproc);
1320 		else
1321 			p = LIST_FIRST(&zombproc);
1322 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1323 			/*
1324 			 * Skip embryonic processes.
1325 			 */
1326 			PROC_LOCK(p);
1327 			if (p->p_state == PRS_NEW) {
1328 				PROC_UNLOCK(p);
1329 				continue;
1330 			}
1331 			KASSERT(p->p_ucred != NULL,
1332 			    ("process credential is NULL for non-NEW proc"));
1333 			/*
1334 			 * Show a user only appropriate processes.
1335 			 */
1336 			if (p_cansee(curthread, p)) {
1337 				PROC_UNLOCK(p);
1338 				continue;
1339 			}
1340 			/*
1341 			 * TODO - make more efficient (see notes below).
1342 			 * do by session.
1343 			 */
1344 			switch (oid_number) {
1345 
1346 			case KERN_PROC_GID:
1347 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1348 					PROC_UNLOCK(p);
1349 					continue;
1350 				}
1351 				break;
1352 
1353 			case KERN_PROC_PGRP:
1354 				/* could do this by traversing pgrp */
1355 				if (p->p_pgrp == NULL ||
1356 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1357 					PROC_UNLOCK(p);
1358 					continue;
1359 				}
1360 				break;
1361 
1362 			case KERN_PROC_RGID:
1363 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1364 					PROC_UNLOCK(p);
1365 					continue;
1366 				}
1367 				break;
1368 
1369 			case KERN_PROC_SESSION:
1370 				if (p->p_session == NULL ||
1371 				    p->p_session->s_sid != (pid_t)name[0]) {
1372 					PROC_UNLOCK(p);
1373 					continue;
1374 				}
1375 				break;
1376 
1377 			case KERN_PROC_TTY:
1378 				if ((p->p_flag & P_CONTROLT) == 0 ||
1379 				    p->p_session == NULL) {
1380 					PROC_UNLOCK(p);
1381 					continue;
1382 				}
1383 				/* XXX proctree_lock */
1384 				SESS_LOCK(p->p_session);
1385 				if (p->p_session->s_ttyp == NULL ||
1386 				    tty_udev(p->p_session->s_ttyp) !=
1387 				    (dev_t)name[0]) {
1388 					SESS_UNLOCK(p->p_session);
1389 					PROC_UNLOCK(p);
1390 					continue;
1391 				}
1392 				SESS_UNLOCK(p->p_session);
1393 				break;
1394 
1395 			case KERN_PROC_UID:
1396 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1397 					PROC_UNLOCK(p);
1398 					continue;
1399 				}
1400 				break;
1401 
1402 			case KERN_PROC_RUID:
1403 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1404 					PROC_UNLOCK(p);
1405 					continue;
1406 				}
1407 				break;
1408 
1409 			case KERN_PROC_PROC:
1410 				break;
1411 
1412 			default:
1413 				break;
1414 
1415 			}
1416 
1417 			error = sysctl_out_proc(p, req, flags, doingzomb);
1418 			if (error) {
1419 				sx_sunlock(&allproc_lock);
1420 				return (error);
1421 			}
1422 		}
1423 	}
1424 	sx_sunlock(&allproc_lock);
1425 	return (0);
1426 }
1427 
1428 struct pargs *
1429 pargs_alloc(int len)
1430 {
1431 	struct pargs *pa;
1432 
1433 	pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1434 		M_WAITOK);
1435 	refcount_init(&pa->ar_ref, 1);
1436 	pa->ar_length = len;
1437 	return (pa);
1438 }
1439 
1440 static void
1441 pargs_free(struct pargs *pa)
1442 {
1443 
1444 	free(pa, M_PARGS);
1445 }
1446 
1447 void
1448 pargs_hold(struct pargs *pa)
1449 {
1450 
1451 	if (pa == NULL)
1452 		return;
1453 	refcount_acquire(&pa->ar_ref);
1454 }
1455 
1456 void
1457 pargs_drop(struct pargs *pa)
1458 {
1459 
1460 	if (pa == NULL)
1461 		return;
1462 	if (refcount_release(&pa->ar_ref))
1463 		pargs_free(pa);
1464 }
1465 
1466 static int
1467 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1468     size_t len)
1469 {
1470 	struct iovec iov;
1471 	struct uio uio;
1472 
1473 	iov.iov_base = (caddr_t)buf;
1474 	iov.iov_len = len;
1475 	uio.uio_iov = &iov;
1476 	uio.uio_iovcnt = 1;
1477 	uio.uio_offset = offset;
1478 	uio.uio_resid = (ssize_t)len;
1479 	uio.uio_segflg = UIO_SYSSPACE;
1480 	uio.uio_rw = UIO_READ;
1481 	uio.uio_td = td;
1482 
1483 	return (proc_rwmem(p, &uio));
1484 }
1485 
1486 static int
1487 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1488     size_t len)
1489 {
1490 	size_t i;
1491 	int error;
1492 
1493 	error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1494 	/*
1495 	 * Reading the chunk may validly return EFAULT if the string is shorter
1496 	 * than the chunk and is aligned at the end of the page, assuming the
1497 	 * next page is not mapped.  So if EFAULT is returned do a fallback to
1498 	 * one byte read loop.
1499 	 */
1500 	if (error == EFAULT) {
1501 		for (i = 0; i < len; i++, buf++, sptr++) {
1502 			error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1503 			if (error != 0)
1504 				return (error);
1505 			if (*buf == '\0')
1506 				break;
1507 		}
1508 		error = 0;
1509 	}
1510 	return (error);
1511 }
1512 
1513 #define PROC_AUXV_MAX	256	/* Safety limit on auxv size. */
1514 
1515 enum proc_vector_type {
1516 	PROC_ARG,
1517 	PROC_ENV,
1518 	PROC_AUX,
1519 };
1520 
1521 #ifdef COMPAT_FREEBSD32
1522 static int
1523 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1524     size_t *vsizep, enum proc_vector_type type)
1525 {
1526 	struct freebsd32_ps_strings pss;
1527 	Elf32_Auxinfo aux;
1528 	vm_offset_t vptr, ptr;
1529 	uint32_t *proc_vector32;
1530 	char **proc_vector;
1531 	size_t vsize, size;
1532 	int i, error;
1533 
1534 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1535 	    &pss, sizeof(pss));
1536 	if (error != 0)
1537 		return (error);
1538 	switch (type) {
1539 	case PROC_ARG:
1540 		vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1541 		vsize = pss.ps_nargvstr;
1542 		if (vsize > ARG_MAX)
1543 			return (ENOEXEC);
1544 		size = vsize * sizeof(int32_t);
1545 		break;
1546 	case PROC_ENV:
1547 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1548 		vsize = pss.ps_nenvstr;
1549 		if (vsize > ARG_MAX)
1550 			return (ENOEXEC);
1551 		size = vsize * sizeof(int32_t);
1552 		break;
1553 	case PROC_AUX:
1554 		vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1555 		    (pss.ps_nenvstr + 1) * sizeof(int32_t);
1556 		if (vptr % 4 != 0)
1557 			return (ENOEXEC);
1558 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1559 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1560 			if (error != 0)
1561 				return (error);
1562 			if (aux.a_type == AT_NULL)
1563 				break;
1564 			ptr += sizeof(aux);
1565 		}
1566 		if (aux.a_type != AT_NULL)
1567 			return (ENOEXEC);
1568 		vsize = i + 1;
1569 		size = vsize * sizeof(aux);
1570 		break;
1571 	default:
1572 		KASSERT(0, ("Wrong proc vector type: %d", type));
1573 		return (EINVAL);
1574 	}
1575 	proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1576 	error = proc_read_mem(td, p, vptr, proc_vector32, size);
1577 	if (error != 0)
1578 		goto done;
1579 	if (type == PROC_AUX) {
1580 		*proc_vectorp = (char **)proc_vector32;
1581 		*vsizep = vsize;
1582 		return (0);
1583 	}
1584 	proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1585 	for (i = 0; i < (int)vsize; i++)
1586 		proc_vector[i] = PTRIN(proc_vector32[i]);
1587 	*proc_vectorp = proc_vector;
1588 	*vsizep = vsize;
1589 done:
1590 	free(proc_vector32, M_TEMP);
1591 	return (error);
1592 }
1593 #endif
1594 
1595 static int
1596 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1597     size_t *vsizep, enum proc_vector_type type)
1598 {
1599 	struct ps_strings pss;
1600 	Elf_Auxinfo aux;
1601 	vm_offset_t vptr, ptr;
1602 	char **proc_vector;
1603 	size_t vsize, size;
1604 	int error, i;
1605 
1606 #ifdef COMPAT_FREEBSD32
1607 	if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1608 		return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1609 #endif
1610 	error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1611 	    &pss, sizeof(pss));
1612 	if (error != 0)
1613 		return (error);
1614 	switch (type) {
1615 	case PROC_ARG:
1616 		vptr = (vm_offset_t)pss.ps_argvstr;
1617 		vsize = pss.ps_nargvstr;
1618 		if (vsize > ARG_MAX)
1619 			return (ENOEXEC);
1620 		size = vsize * sizeof(char *);
1621 		break;
1622 	case PROC_ENV:
1623 		vptr = (vm_offset_t)pss.ps_envstr;
1624 		vsize = pss.ps_nenvstr;
1625 		if (vsize > ARG_MAX)
1626 			return (ENOEXEC);
1627 		size = vsize * sizeof(char *);
1628 		break;
1629 	case PROC_AUX:
1630 		/*
1631 		 * The aux array is just above env array on the stack. Check
1632 		 * that the address is naturally aligned.
1633 		 */
1634 		vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1635 		    * sizeof(char *);
1636 #if __ELF_WORD_SIZE == 64
1637 		if (vptr % sizeof(uint64_t) != 0)
1638 #else
1639 		if (vptr % sizeof(uint32_t) != 0)
1640 #endif
1641 			return (ENOEXEC);
1642 		/*
1643 		 * We count the array size reading the aux vectors from the
1644 		 * stack until AT_NULL vector is returned.  So (to keep the code
1645 		 * simple) we read the process stack twice: the first time here
1646 		 * to find the size and the second time when copying the vectors
1647 		 * to the allocated proc_vector.
1648 		 */
1649 		for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1650 			error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1651 			if (error != 0)
1652 				return (error);
1653 			if (aux.a_type == AT_NULL)
1654 				break;
1655 			ptr += sizeof(aux);
1656 		}
1657 		/*
1658 		 * If the PROC_AUXV_MAX entries are iterated over, and we have
1659 		 * not reached AT_NULL, it is most likely we are reading wrong
1660 		 * data: either the process doesn't have auxv array or data has
1661 		 * been modified. Return the error in this case.
1662 		 */
1663 		if (aux.a_type != AT_NULL)
1664 			return (ENOEXEC);
1665 		vsize = i + 1;
1666 		size = vsize * sizeof(aux);
1667 		break;
1668 	default:
1669 		KASSERT(0, ("Wrong proc vector type: %d", type));
1670 		return (EINVAL); /* In case we are built without INVARIANTS. */
1671 	}
1672 	proc_vector = malloc(size, M_TEMP, M_WAITOK);
1673 	if (proc_vector == NULL)
1674 		return (ENOMEM);
1675 	error = proc_read_mem(td, p, vptr, proc_vector, size);
1676 	if (error != 0) {
1677 		free(proc_vector, M_TEMP);
1678 		return (error);
1679 	}
1680 	*proc_vectorp = proc_vector;
1681 	*vsizep = vsize;
1682 
1683 	return (0);
1684 }
1685 
1686 #define GET_PS_STRINGS_CHUNK_SZ	256	/* Chunk size (bytes) for ps_strings operations. */
1687 
1688 static int
1689 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1690     enum proc_vector_type type)
1691 {
1692 	size_t done, len, nchr, vsize;
1693 	int error, i;
1694 	char **proc_vector, *sptr;
1695 	char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1696 
1697 	PROC_ASSERT_HELD(p);
1698 
1699 	/*
1700 	 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1701 	 */
1702 	nchr = 2 * (PATH_MAX + ARG_MAX);
1703 
1704 	error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1705 	if (error != 0)
1706 		return (error);
1707 	for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1708 		/*
1709 		 * The program may have scribbled into its argv array, e.g. to
1710 		 * remove some arguments.  If that has happened, break out
1711 		 * before trying to read from NULL.
1712 		 */
1713 		if (proc_vector[i] == NULL)
1714 			break;
1715 		for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1716 			error = proc_read_string(td, p, sptr, pss_string,
1717 			    sizeof(pss_string));
1718 			if (error != 0)
1719 				goto done;
1720 			len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1721 			if (done + len >= nchr)
1722 				len = nchr - done - 1;
1723 			sbuf_bcat(sb, pss_string, len);
1724 			if (len != GET_PS_STRINGS_CHUNK_SZ)
1725 				break;
1726 			done += GET_PS_STRINGS_CHUNK_SZ;
1727 		}
1728 		sbuf_bcat(sb, "", 1);
1729 		done += len + 1;
1730 	}
1731 done:
1732 	free(proc_vector, M_TEMP);
1733 	return (error);
1734 }
1735 
1736 int
1737 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1738 {
1739 
1740 	return (get_ps_strings(curthread, p, sb, PROC_ARG));
1741 }
1742 
1743 int
1744 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1745 {
1746 
1747 	return (get_ps_strings(curthread, p, sb, PROC_ENV));
1748 }
1749 
1750 int
1751 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1752 {
1753 	size_t vsize, size;
1754 	char **auxv;
1755 	int error;
1756 
1757 	error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1758 	if (error == 0) {
1759 #ifdef COMPAT_FREEBSD32
1760 		if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1761 			size = vsize * sizeof(Elf32_Auxinfo);
1762 		else
1763 #endif
1764 			size = vsize * sizeof(Elf_Auxinfo);
1765 		error = sbuf_bcat(sb, auxv, size);
1766 		free(auxv, M_TEMP);
1767 	}
1768 	return (error);
1769 }
1770 
1771 /*
1772  * This sysctl allows a process to retrieve the argument list or process
1773  * title for another process without groping around in the address space
1774  * of the other process.  It also allow a process to set its own "process
1775  * title to a string of its own choice.
1776  */
1777 static int
1778 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1779 {
1780 	int *name = (int *)arg1;
1781 	u_int namelen = arg2;
1782 	struct pargs *newpa, *pa;
1783 	struct proc *p;
1784 	struct sbuf sb;
1785 	int flags, error = 0, error2;
1786 
1787 	if (namelen != 1)
1788 		return (EINVAL);
1789 
1790 	flags = PGET_CANSEE;
1791 	if (req->newptr != NULL)
1792 		flags |= PGET_ISCURRENT;
1793 	error = pget((pid_t)name[0], flags, &p);
1794 	if (error)
1795 		return (error);
1796 
1797 	pa = p->p_args;
1798 	if (pa != NULL) {
1799 		pargs_hold(pa);
1800 		PROC_UNLOCK(p);
1801 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1802 		pargs_drop(pa);
1803 	} else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1804 		_PHOLD(p);
1805 		PROC_UNLOCK(p);
1806 		sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1807 		error = proc_getargv(curthread, p, &sb);
1808 		error2 = sbuf_finish(&sb);
1809 		PRELE(p);
1810 		sbuf_delete(&sb);
1811 		if (error == 0 && error2 != 0)
1812 			error = error2;
1813 	} else {
1814 		PROC_UNLOCK(p);
1815 	}
1816 	if (error != 0 || req->newptr == NULL)
1817 		return (error);
1818 
1819 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1820 		return (ENOMEM);
1821 	newpa = pargs_alloc(req->newlen);
1822 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1823 	if (error != 0) {
1824 		pargs_free(newpa);
1825 		return (error);
1826 	}
1827 	PROC_LOCK(p);
1828 	pa = p->p_args;
1829 	p->p_args = newpa;
1830 	PROC_UNLOCK(p);
1831 	pargs_drop(pa);
1832 	return (0);
1833 }
1834 
1835 /*
1836  * This sysctl allows a process to retrieve environment of another process.
1837  */
1838 static int
1839 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1840 {
1841 	int *name = (int *)arg1;
1842 	u_int namelen = arg2;
1843 	struct proc *p;
1844 	struct sbuf sb;
1845 	int error, error2;
1846 
1847 	if (namelen != 1)
1848 		return (EINVAL);
1849 
1850 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1851 	if (error != 0)
1852 		return (error);
1853 	if ((p->p_flag & P_SYSTEM) != 0) {
1854 		PRELE(p);
1855 		return (0);
1856 	}
1857 
1858 	sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1859 	error = proc_getenvv(curthread, p, &sb);
1860 	error2 = sbuf_finish(&sb);
1861 	PRELE(p);
1862 	sbuf_delete(&sb);
1863 	return (error != 0 ? error : error2);
1864 }
1865 
1866 /*
1867  * This sysctl allows a process to retrieve ELF auxiliary vector of
1868  * another process.
1869  */
1870 static int
1871 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1872 {
1873 	int *name = (int *)arg1;
1874 	u_int namelen = arg2;
1875 	struct proc *p;
1876 	struct sbuf sb;
1877 	int error, error2;
1878 
1879 	if (namelen != 1)
1880 		return (EINVAL);
1881 
1882 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1883 	if (error != 0)
1884 		return (error);
1885 	if ((p->p_flag & P_SYSTEM) != 0) {
1886 		PRELE(p);
1887 		return (0);
1888 	}
1889 	sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1890 	error = proc_getauxv(curthread, p, &sb);
1891 	error2 = sbuf_finish(&sb);
1892 	PRELE(p);
1893 	sbuf_delete(&sb);
1894 	return (error != 0 ? error : error2);
1895 }
1896 
1897 /*
1898  * This sysctl allows a process to retrieve the path of the executable for
1899  * itself or another process.
1900  */
1901 static int
1902 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1903 {
1904 	pid_t *pidp = (pid_t *)arg1;
1905 	unsigned int arglen = arg2;
1906 	struct proc *p;
1907 	struct vnode *vp;
1908 	char *retbuf, *freebuf;
1909 	int error;
1910 
1911 	if (arglen != 1)
1912 		return (EINVAL);
1913 	if (*pidp == -1) {	/* -1 means this process */
1914 		p = req->td->td_proc;
1915 	} else {
1916 		error = pget(*pidp, PGET_CANSEE, &p);
1917 		if (error != 0)
1918 			return (error);
1919 	}
1920 
1921 	vp = p->p_textvp;
1922 	if (vp == NULL) {
1923 		if (*pidp != -1)
1924 			PROC_UNLOCK(p);
1925 		return (0);
1926 	}
1927 	vref(vp);
1928 	if (*pidp != -1)
1929 		PROC_UNLOCK(p);
1930 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1931 	vrele(vp);
1932 	if (error)
1933 		return (error);
1934 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1935 	free(freebuf, M_TEMP);
1936 	return (error);
1937 }
1938 
1939 static int
1940 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1941 {
1942 	struct proc *p;
1943 	char *sv_name;
1944 	int *name;
1945 	int namelen;
1946 	int error;
1947 
1948 	namelen = arg2;
1949 	if (namelen != 1)
1950 		return (EINVAL);
1951 
1952 	name = (int *)arg1;
1953 	error = pget((pid_t)name[0], PGET_CANSEE, &p);
1954 	if (error != 0)
1955 		return (error);
1956 	sv_name = p->p_sysent->sv_name;
1957 	PROC_UNLOCK(p);
1958 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1959 }
1960 
1961 #ifdef KINFO_OVMENTRY_SIZE
1962 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1963 #endif
1964 
1965 #ifdef COMPAT_FREEBSD7
1966 static int
1967 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1968 {
1969 	vm_map_entry_t entry, tmp_entry;
1970 	unsigned int last_timestamp;
1971 	char *fullpath, *freepath;
1972 	struct kinfo_ovmentry *kve;
1973 	struct vattr va;
1974 	struct ucred *cred;
1975 	int error, *name;
1976 	struct vnode *vp;
1977 	struct proc *p;
1978 	vm_map_t map;
1979 	struct vmspace *vm;
1980 
1981 	name = (int *)arg1;
1982 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1983 	if (error != 0)
1984 		return (error);
1985 	vm = vmspace_acquire_ref(p);
1986 	if (vm == NULL) {
1987 		PRELE(p);
1988 		return (ESRCH);
1989 	}
1990 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1991 
1992 	map = &vm->vm_map;
1993 	vm_map_lock_read(map);
1994 	for (entry = map->header.next; entry != &map->header;
1995 	    entry = entry->next) {
1996 		vm_object_t obj, tobj, lobj;
1997 		vm_offset_t addr;
1998 
1999 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2000 			continue;
2001 
2002 		bzero(kve, sizeof(*kve));
2003 		kve->kve_structsize = sizeof(*kve);
2004 
2005 		kve->kve_private_resident = 0;
2006 		obj = entry->object.vm_object;
2007 		if (obj != NULL) {
2008 			VM_OBJECT_RLOCK(obj);
2009 			if (obj->shadow_count == 1)
2010 				kve->kve_private_resident =
2011 				    obj->resident_page_count;
2012 		}
2013 		kve->kve_resident = 0;
2014 		addr = entry->start;
2015 		while (addr < entry->end) {
2016 			if (pmap_extract(map->pmap, addr))
2017 				kve->kve_resident++;
2018 			addr += PAGE_SIZE;
2019 		}
2020 
2021 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2022 			if (tobj != obj)
2023 				VM_OBJECT_RLOCK(tobj);
2024 			if (lobj != obj)
2025 				VM_OBJECT_RUNLOCK(lobj);
2026 			lobj = tobj;
2027 		}
2028 
2029 		kve->kve_start = (void*)entry->start;
2030 		kve->kve_end = (void*)entry->end;
2031 		kve->kve_offset = (off_t)entry->offset;
2032 
2033 		if (entry->protection & VM_PROT_READ)
2034 			kve->kve_protection |= KVME_PROT_READ;
2035 		if (entry->protection & VM_PROT_WRITE)
2036 			kve->kve_protection |= KVME_PROT_WRITE;
2037 		if (entry->protection & VM_PROT_EXECUTE)
2038 			kve->kve_protection |= KVME_PROT_EXEC;
2039 
2040 		if (entry->eflags & MAP_ENTRY_COW)
2041 			kve->kve_flags |= KVME_FLAG_COW;
2042 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2043 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2044 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2045 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2046 
2047 		last_timestamp = map->timestamp;
2048 		vm_map_unlock_read(map);
2049 
2050 		kve->kve_fileid = 0;
2051 		kve->kve_fsid = 0;
2052 		freepath = NULL;
2053 		fullpath = "";
2054 		if (lobj) {
2055 			vp = NULL;
2056 			switch (lobj->type) {
2057 			case OBJT_DEFAULT:
2058 				kve->kve_type = KVME_TYPE_DEFAULT;
2059 				break;
2060 			case OBJT_VNODE:
2061 				kve->kve_type = KVME_TYPE_VNODE;
2062 				vp = lobj->handle;
2063 				vref(vp);
2064 				break;
2065 			case OBJT_SWAP:
2066 				kve->kve_type = KVME_TYPE_SWAP;
2067 				break;
2068 			case OBJT_DEVICE:
2069 				kve->kve_type = KVME_TYPE_DEVICE;
2070 				break;
2071 			case OBJT_PHYS:
2072 				kve->kve_type = KVME_TYPE_PHYS;
2073 				break;
2074 			case OBJT_DEAD:
2075 				kve->kve_type = KVME_TYPE_DEAD;
2076 				break;
2077 			case OBJT_SG:
2078 				kve->kve_type = KVME_TYPE_SG;
2079 				break;
2080 			default:
2081 				kve->kve_type = KVME_TYPE_UNKNOWN;
2082 				break;
2083 			}
2084 			if (lobj != obj)
2085 				VM_OBJECT_RUNLOCK(lobj);
2086 
2087 			kve->kve_ref_count = obj->ref_count;
2088 			kve->kve_shadow_count = obj->shadow_count;
2089 			VM_OBJECT_RUNLOCK(obj);
2090 			if (vp != NULL) {
2091 				vn_fullpath(curthread, vp, &fullpath,
2092 				    &freepath);
2093 				cred = curthread->td_ucred;
2094 				vn_lock(vp, LK_SHARED | LK_RETRY);
2095 				if (VOP_GETATTR(vp, &va, cred) == 0) {
2096 					kve->kve_fileid = va.va_fileid;
2097 					kve->kve_fsid = va.va_fsid;
2098 				}
2099 				vput(vp);
2100 			}
2101 		} else {
2102 			kve->kve_type = KVME_TYPE_NONE;
2103 			kve->kve_ref_count = 0;
2104 			kve->kve_shadow_count = 0;
2105 		}
2106 
2107 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2108 		if (freepath != NULL)
2109 			free(freepath, M_TEMP);
2110 
2111 		error = SYSCTL_OUT(req, kve, sizeof(*kve));
2112 		vm_map_lock_read(map);
2113 		if (error)
2114 			break;
2115 		if (last_timestamp != map->timestamp) {
2116 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2117 			entry = tmp_entry;
2118 		}
2119 	}
2120 	vm_map_unlock_read(map);
2121 	vmspace_free(vm);
2122 	PRELE(p);
2123 	free(kve, M_TEMP);
2124 	return (error);
2125 }
2126 #endif	/* COMPAT_FREEBSD7 */
2127 
2128 #ifdef KINFO_VMENTRY_SIZE
2129 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2130 #endif
2131 
2132 /*
2133  * Must be called with the process locked and will return unlocked.
2134  */
2135 int
2136 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb)
2137 {
2138 	vm_map_entry_t entry, tmp_entry;
2139 	unsigned int last_timestamp;
2140 	char *fullpath, *freepath;
2141 	struct kinfo_vmentry *kve;
2142 	struct vattr va;
2143 	struct ucred *cred;
2144 	int error;
2145 	struct vnode *vp;
2146 	struct vmspace *vm;
2147 	vm_map_t map;
2148 
2149 	PROC_LOCK_ASSERT(p, MA_OWNED);
2150 
2151 	_PHOLD(p);
2152 	PROC_UNLOCK(p);
2153 	vm = vmspace_acquire_ref(p);
2154 	if (vm == NULL) {
2155 		PRELE(p);
2156 		return (ESRCH);
2157 	}
2158 	kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2159 
2160 	error = 0;
2161 	map = &vm->vm_map;
2162 	vm_map_lock_read(map);
2163 	for (entry = map->header.next; entry != &map->header;
2164 	    entry = entry->next) {
2165 		vm_object_t obj, tobj, lobj;
2166 		vm_offset_t addr;
2167 		vm_paddr_t locked_pa;
2168 		int mincoreinfo;
2169 
2170 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2171 			continue;
2172 
2173 		bzero(kve, sizeof(*kve));
2174 
2175 		kve->kve_private_resident = 0;
2176 		obj = entry->object.vm_object;
2177 		if (obj != NULL) {
2178 			VM_OBJECT_RLOCK(obj);
2179 			if (obj->shadow_count == 1)
2180 				kve->kve_private_resident =
2181 				    obj->resident_page_count;
2182 		}
2183 		kve->kve_resident = 0;
2184 		addr = entry->start;
2185 		while (addr < entry->end) {
2186 			locked_pa = 0;
2187 			mincoreinfo = pmap_mincore(map->pmap, addr, &locked_pa);
2188 			if (locked_pa != 0)
2189 				vm_page_unlock(PHYS_TO_VM_PAGE(locked_pa));
2190 			if (mincoreinfo & MINCORE_INCORE)
2191 				kve->kve_resident++;
2192 			if (mincoreinfo & MINCORE_SUPER)
2193 				kve->kve_flags |= KVME_FLAG_SUPER;
2194 			addr += PAGE_SIZE;
2195 		}
2196 
2197 		for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2198 			if (tobj != obj)
2199 				VM_OBJECT_RLOCK(tobj);
2200 			if (lobj != obj)
2201 				VM_OBJECT_RUNLOCK(lobj);
2202 			lobj = tobj;
2203 		}
2204 
2205 		kve->kve_start = entry->start;
2206 		kve->kve_end = entry->end;
2207 		kve->kve_offset = entry->offset;
2208 
2209 		if (entry->protection & VM_PROT_READ)
2210 			kve->kve_protection |= KVME_PROT_READ;
2211 		if (entry->protection & VM_PROT_WRITE)
2212 			kve->kve_protection |= KVME_PROT_WRITE;
2213 		if (entry->protection & VM_PROT_EXECUTE)
2214 			kve->kve_protection |= KVME_PROT_EXEC;
2215 
2216 		if (entry->eflags & MAP_ENTRY_COW)
2217 			kve->kve_flags |= KVME_FLAG_COW;
2218 		if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2219 			kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2220 		if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2221 			kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2222 		if (entry->eflags & MAP_ENTRY_GROWS_UP)
2223 			kve->kve_flags |= KVME_FLAG_GROWS_UP;
2224 		if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2225 			kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2226 
2227 		last_timestamp = map->timestamp;
2228 		vm_map_unlock_read(map);
2229 
2230 		freepath = NULL;
2231 		fullpath = "";
2232 		if (lobj) {
2233 			vp = NULL;
2234 			switch (lobj->type) {
2235 			case OBJT_DEFAULT:
2236 				kve->kve_type = KVME_TYPE_DEFAULT;
2237 				break;
2238 			case OBJT_VNODE:
2239 				kve->kve_type = KVME_TYPE_VNODE;
2240 				vp = lobj->handle;
2241 				vref(vp);
2242 				break;
2243 			case OBJT_SWAP:
2244 				kve->kve_type = KVME_TYPE_SWAP;
2245 				break;
2246 			case OBJT_DEVICE:
2247 				kve->kve_type = KVME_TYPE_DEVICE;
2248 				break;
2249 			case OBJT_PHYS:
2250 				kve->kve_type = KVME_TYPE_PHYS;
2251 				break;
2252 			case OBJT_DEAD:
2253 				kve->kve_type = KVME_TYPE_DEAD;
2254 				break;
2255 			case OBJT_SG:
2256 				kve->kve_type = KVME_TYPE_SG;
2257 				break;
2258 			default:
2259 				kve->kve_type = KVME_TYPE_UNKNOWN;
2260 				break;
2261 			}
2262 			if (lobj != obj)
2263 				VM_OBJECT_RUNLOCK(lobj);
2264 
2265 			kve->kve_ref_count = obj->ref_count;
2266 			kve->kve_shadow_count = obj->shadow_count;
2267 			VM_OBJECT_RUNLOCK(obj);
2268 			if (vp != NULL) {
2269 				vn_fullpath(curthread, vp, &fullpath,
2270 				    &freepath);
2271 				kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2272 				cred = curthread->td_ucred;
2273 				vn_lock(vp, LK_SHARED | LK_RETRY);
2274 				if (VOP_GETATTR(vp, &va, cred) == 0) {
2275 					kve->kve_vn_fileid = va.va_fileid;
2276 					kve->kve_vn_fsid = va.va_fsid;
2277 					kve->kve_vn_mode =
2278 					    MAKEIMODE(va.va_type, va.va_mode);
2279 					kve->kve_vn_size = va.va_size;
2280 					kve->kve_vn_rdev = va.va_rdev;
2281 					kve->kve_status = KF_ATTR_VALID;
2282 				}
2283 				vput(vp);
2284 			}
2285 		} else {
2286 			kve->kve_type = KVME_TYPE_NONE;
2287 			kve->kve_ref_count = 0;
2288 			kve->kve_shadow_count = 0;
2289 		}
2290 
2291 		strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2292 		if (freepath != NULL)
2293 			free(freepath, M_TEMP);
2294 
2295 		/* Pack record size down */
2296 		kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
2297 		    strlen(kve->kve_path) + 1;
2298 		kve->kve_structsize = roundup(kve->kve_structsize,
2299 		    sizeof(uint64_t));
2300 		error = sbuf_bcat(sb, kve, kve->kve_structsize);
2301 		vm_map_lock_read(map);
2302 		if (error)
2303 			break;
2304 		if (last_timestamp != map->timestamp) {
2305 			vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2306 			entry = tmp_entry;
2307 		}
2308 	}
2309 	vm_map_unlock_read(map);
2310 	vmspace_free(vm);
2311 	PRELE(p);
2312 	free(kve, M_TEMP);
2313 	return (error);
2314 }
2315 
2316 static int
2317 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2318 {
2319 	struct proc *p;
2320 	struct sbuf sb;
2321 	int error, error2, *name;
2322 
2323 	name = (int *)arg1;
2324 	sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2325 	error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2326 	if (error != 0) {
2327 		sbuf_delete(&sb);
2328 		return (error);
2329 	}
2330 	error = kern_proc_vmmap_out(p, &sb);
2331 	error2 = sbuf_finish(&sb);
2332 	sbuf_delete(&sb);
2333 	return (error != 0 ? error : error2);
2334 }
2335 
2336 #if defined(STACK) || defined(DDB)
2337 static int
2338 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2339 {
2340 	struct kinfo_kstack *kkstp;
2341 	int error, i, *name, numthreads;
2342 	lwpid_t *lwpidarray;
2343 	struct thread *td;
2344 	struct stack *st;
2345 	struct sbuf sb;
2346 	struct proc *p;
2347 
2348 	name = (int *)arg1;
2349 	error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2350 	if (error != 0)
2351 		return (error);
2352 
2353 	kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2354 	st = stack_create();
2355 
2356 	lwpidarray = NULL;
2357 	numthreads = 0;
2358 	PROC_LOCK(p);
2359 repeat:
2360 	if (numthreads < p->p_numthreads) {
2361 		if (lwpidarray != NULL) {
2362 			free(lwpidarray, M_TEMP);
2363 			lwpidarray = NULL;
2364 		}
2365 		numthreads = p->p_numthreads;
2366 		PROC_UNLOCK(p);
2367 		lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2368 		    M_WAITOK | M_ZERO);
2369 		PROC_LOCK(p);
2370 		goto repeat;
2371 	}
2372 	i = 0;
2373 
2374 	/*
2375 	 * XXXRW: During the below loop, execve(2) and countless other sorts
2376 	 * of changes could have taken place.  Should we check to see if the
2377 	 * vmspace has been replaced, or the like, in order to prevent
2378 	 * giving a snapshot that spans, say, execve(2), with some threads
2379 	 * before and some after?  Among other things, the credentials could
2380 	 * have changed, in which case the right to extract debug info might
2381 	 * no longer be assured.
2382 	 */
2383 	FOREACH_THREAD_IN_PROC(p, td) {
2384 		KASSERT(i < numthreads,
2385 		    ("sysctl_kern_proc_kstack: numthreads"));
2386 		lwpidarray[i] = td->td_tid;
2387 		i++;
2388 	}
2389 	numthreads = i;
2390 	for (i = 0; i < numthreads; i++) {
2391 		td = thread_find(p, lwpidarray[i]);
2392 		if (td == NULL) {
2393 			continue;
2394 		}
2395 		bzero(kkstp, sizeof(*kkstp));
2396 		(void)sbuf_new(&sb, kkstp->kkst_trace,
2397 		    sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2398 		thread_lock(td);
2399 		kkstp->kkst_tid = td->td_tid;
2400 		if (TD_IS_SWAPPED(td))
2401 			kkstp->kkst_state = KKST_STATE_SWAPPED;
2402 		else if (TD_IS_RUNNING(td))
2403 			kkstp->kkst_state = KKST_STATE_RUNNING;
2404 		else {
2405 			kkstp->kkst_state = KKST_STATE_STACKOK;
2406 			stack_save_td(st, td);
2407 		}
2408 		thread_unlock(td);
2409 		PROC_UNLOCK(p);
2410 		stack_sbuf_print(&sb, st);
2411 		sbuf_finish(&sb);
2412 		sbuf_delete(&sb);
2413 		error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2414 		PROC_LOCK(p);
2415 		if (error)
2416 			break;
2417 	}
2418 	_PRELE(p);
2419 	PROC_UNLOCK(p);
2420 	if (lwpidarray != NULL)
2421 		free(lwpidarray, M_TEMP);
2422 	stack_destroy(st);
2423 	free(kkstp, M_TEMP);
2424 	return (error);
2425 }
2426 #endif
2427 
2428 /*
2429  * This sysctl allows a process to retrieve the full list of groups from
2430  * itself or another process.
2431  */
2432 static int
2433 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2434 {
2435 	pid_t *pidp = (pid_t *)arg1;
2436 	unsigned int arglen = arg2;
2437 	struct proc *p;
2438 	struct ucred *cred;
2439 	int error;
2440 
2441 	if (arglen != 1)
2442 		return (EINVAL);
2443 	if (*pidp == -1) {	/* -1 means this process */
2444 		p = req->td->td_proc;
2445 	} else {
2446 		error = pget(*pidp, PGET_CANSEE, &p);
2447 		if (error != 0)
2448 			return (error);
2449 	}
2450 
2451 	cred = crhold(p->p_ucred);
2452 	if (*pidp != -1)
2453 		PROC_UNLOCK(p);
2454 
2455 	error = SYSCTL_OUT(req, cred->cr_groups,
2456 	    cred->cr_ngroups * sizeof(gid_t));
2457 	crfree(cred);
2458 	return (error);
2459 }
2460 
2461 /*
2462  * This sysctl allows a process to retrieve or/and set the resource limit for
2463  * another process.
2464  */
2465 static int
2466 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2467 {
2468 	int *name = (int *)arg1;
2469 	u_int namelen = arg2;
2470 	struct rlimit rlim;
2471 	struct proc *p;
2472 	u_int which;
2473 	int flags, error;
2474 
2475 	if (namelen != 2)
2476 		return (EINVAL);
2477 
2478 	which = (u_int)name[1];
2479 	if (which >= RLIM_NLIMITS)
2480 		return (EINVAL);
2481 
2482 	if (req->newptr != NULL && req->newlen != sizeof(rlim))
2483 		return (EINVAL);
2484 
2485 	flags = PGET_HOLD | PGET_NOTWEXIT;
2486 	if (req->newptr != NULL)
2487 		flags |= PGET_CANDEBUG;
2488 	else
2489 		flags |= PGET_CANSEE;
2490 	error = pget((pid_t)name[0], flags, &p);
2491 	if (error != 0)
2492 		return (error);
2493 
2494 	/*
2495 	 * Retrieve limit.
2496 	 */
2497 	if (req->oldptr != NULL) {
2498 		PROC_LOCK(p);
2499 		lim_rlimit(p, which, &rlim);
2500 		PROC_UNLOCK(p);
2501 	}
2502 	error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2503 	if (error != 0)
2504 		goto errout;
2505 
2506 	/*
2507 	 * Set limit.
2508 	 */
2509 	if (req->newptr != NULL) {
2510 		error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2511 		if (error == 0)
2512 			error = kern_proc_setrlimit(curthread, p, which, &rlim);
2513 	}
2514 
2515 errout:
2516 	PRELE(p);
2517 	return (error);
2518 }
2519 
2520 /*
2521  * This sysctl allows a process to retrieve ps_strings structure location of
2522  * another process.
2523  */
2524 static int
2525 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2526 {
2527 	int *name = (int *)arg1;
2528 	u_int namelen = arg2;
2529 	struct proc *p;
2530 	vm_offset_t ps_strings;
2531 	int error;
2532 #ifdef COMPAT_FREEBSD32
2533 	uint32_t ps_strings32;
2534 #endif
2535 
2536 	if (namelen != 1)
2537 		return (EINVAL);
2538 
2539 	error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2540 	if (error != 0)
2541 		return (error);
2542 #ifdef COMPAT_FREEBSD32
2543 	if ((req->flags & SCTL_MASK32) != 0) {
2544 		/*
2545 		 * We return 0 if the 32 bit emulation request is for a 64 bit
2546 		 * process.
2547 		 */
2548 		ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2549 		    PTROUT(p->p_sysent->sv_psstrings) : 0;
2550 		PROC_UNLOCK(p);
2551 		error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2552 		return (error);
2553 	}
2554 #endif
2555 	ps_strings = p->p_sysent->sv_psstrings;
2556 	PROC_UNLOCK(p);
2557 	error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2558 	return (error);
2559 }
2560 
2561 /*
2562  * This sysctl allows a process to retrieve umask of another process.
2563  */
2564 static int
2565 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2566 {
2567 	int *name = (int *)arg1;
2568 	u_int namelen = arg2;
2569 	struct proc *p;
2570 	int error;
2571 	u_short fd_cmask;
2572 
2573 	if (namelen != 1)
2574 		return (EINVAL);
2575 
2576 	error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2577 	if (error != 0)
2578 		return (error);
2579 
2580 	FILEDESC_SLOCK(p->p_fd);
2581 	fd_cmask = p->p_fd->fd_cmask;
2582 	FILEDESC_SUNLOCK(p->p_fd);
2583 	PRELE(p);
2584 	error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2585 	return (error);
2586 }
2587 
2588 /*
2589  * This sysctl allows a process to set and retrieve binary osreldate of
2590  * another process.
2591  */
2592 static int
2593 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2594 {
2595 	int *name = (int *)arg1;
2596 	u_int namelen = arg2;
2597 	struct proc *p;
2598 	int flags, error, osrel;
2599 
2600 	if (namelen != 1)
2601 		return (EINVAL);
2602 
2603 	if (req->newptr != NULL && req->newlen != sizeof(osrel))
2604 		return (EINVAL);
2605 
2606 	flags = PGET_HOLD | PGET_NOTWEXIT;
2607 	if (req->newptr != NULL)
2608 		flags |= PGET_CANDEBUG;
2609 	else
2610 		flags |= PGET_CANSEE;
2611 	error = pget((pid_t)name[0], flags, &p);
2612 	if (error != 0)
2613 		return (error);
2614 
2615 	error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2616 	if (error != 0)
2617 		goto errout;
2618 
2619 	if (req->newptr != NULL) {
2620 		error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2621 		if (error != 0)
2622 			goto errout;
2623 		if (osrel < 0) {
2624 			error = EINVAL;
2625 			goto errout;
2626 		}
2627 		p->p_osrel = osrel;
2628 	}
2629 errout:
2630 	PRELE(p);
2631 	return (error);
2632 }
2633 
2634 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
2635 
2636 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2637 	CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2638 	"Return entire process table");
2639 
2640 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2641 	sysctl_kern_proc, "Process table");
2642 
2643 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2644 	sysctl_kern_proc, "Process table");
2645 
2646 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2647 	sysctl_kern_proc, "Process table");
2648 
2649 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2650 	CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2651 
2652 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2653 	sysctl_kern_proc, "Process table");
2654 
2655 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2656 	sysctl_kern_proc, "Process table");
2657 
2658 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2659 	sysctl_kern_proc, "Process table");
2660 
2661 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2662 	sysctl_kern_proc, "Process table");
2663 
2664 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2665 	sysctl_kern_proc, "Return process table, no threads");
2666 
2667 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2668 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2669 	sysctl_kern_proc_args, "Process argument list");
2670 
2671 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2672 	sysctl_kern_proc_env, "Process environment");
2673 
2674 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2675 	CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2676 
2677 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2678 	CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2679 
2680 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2681 	CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2682 	"Process syscall vector name (ABI type)");
2683 
2684 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2685 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2686 
2687 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2688 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2689 
2690 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2691 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2692 
2693 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2694 	sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2695 
2696 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2697 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2698 
2699 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2700 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2701 
2702 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2703 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2704 
2705 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2706 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2707 
2708 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2709 	CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2710 	"Return process table, no threads");
2711 
2712 #ifdef COMPAT_FREEBSD7
2713 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2714 	CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2715 #endif
2716 
2717 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2718 	CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2719 
2720 #if defined(STACK) || defined(DDB)
2721 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2722 	CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2723 #endif
2724 
2725 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2726 	CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2727 
2728 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2729 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2730 	"Process resource limits");
2731 
2732 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2733 	CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2734 	"Process ps_strings location");
2735 
2736 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2737 	CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2738 
2739 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2740 	CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2741 	"Process binary osreldate");
2742