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