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