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