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