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