xref: /freebsd/sys/kern/kern_proc.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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_ktrace.h"
36 #include "opt_kstack_pages.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/refcount.h>
46 #include <sys/sysent.h>
47 #include <sys/sched.h>
48 #include <sys/smp.h>
49 #include <sys/sysctl.h>
50 #include <sys/filedesc.h>
51 #include <sys/tty.h>
52 #include <sys/signalvar.h>
53 #include <sys/sx.h>
54 #include <sys/user.h>
55 #include <sys/jail.h>
56 #include <sys/vnode.h>
57 #ifdef KTRACE
58 #include <sys/uio.h>
59 #include <sys/ktrace.h>
60 #endif
61 
62 #include <vm/vm.h>
63 #include <vm/vm_extern.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66 #include <vm/uma.h>
67 
68 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
69 MALLOC_DEFINE(M_SESSION, "session", "session header");
70 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
71 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
72 
73 static void doenterpgrp(struct proc *, struct pgrp *);
74 static void orphanpg(struct pgrp *pg);
75 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
76 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp);
77 static void pgadjustjobc(struct pgrp *pgrp, int entering);
78 static void pgdelete(struct pgrp *);
79 static int proc_ctor(void *mem, int size, void *arg, int flags);
80 static void proc_dtor(void *mem, int size, void *arg);
81 static int proc_init(void *mem, int size, int flags);
82 static void proc_fini(void *mem, int size);
83 
84 /*
85  * Other process lists
86  */
87 struct pidhashhead *pidhashtbl;
88 u_long pidhash;
89 struct pgrphashhead *pgrphashtbl;
90 u_long pgrphash;
91 struct proclist allproc;
92 struct proclist zombproc;
93 struct sx allproc_lock;
94 struct sx proctree_lock;
95 struct mtx ppeers_lock;
96 uma_zone_t proc_zone;
97 uma_zone_t ithread_zone;
98 
99 int kstack_pages = KSTACK_PAGES;
100 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
101 
102 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
103 
104 /*
105  * Initialize global process hashing structures.
106  */
107 void
108 procinit()
109 {
110 
111 	sx_init(&allproc_lock, "allproc");
112 	sx_init(&proctree_lock, "proctree");
113 	mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
114 	LIST_INIT(&allproc);
115 	LIST_INIT(&zombproc);
116 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
117 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
118 	proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
119 	    proc_ctor, proc_dtor, proc_init, proc_fini,
120 	    UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
121 	uihashinit();
122 }
123 
124 /*
125  * Prepare a proc for use.
126  */
127 static int
128 proc_ctor(void *mem, int size, void *arg, int flags)
129 {
130 	struct proc *p;
131 
132 	p = (struct proc *)mem;
133 	return (0);
134 }
135 
136 /*
137  * Reclaim a proc after use.
138  */
139 static void
140 proc_dtor(void *mem, int size, void *arg)
141 {
142 	struct proc *p;
143 	struct thread *td;
144 #if defined(INVARIANTS) && defined(KSE)
145 	struct ksegrp *kg;
146 #endif
147 
148 	/* INVARIANTS checks go here */
149 	p = (struct proc *)mem;
150         td = FIRST_THREAD_IN_PROC(p);
151 #ifdef INVARIANTS
152 	KASSERT((p->p_numthreads == 1),
153 	    ("bad number of threads in exiting process"));
154 #ifdef KSE
155 	KASSERT((p->p_numksegrps == 1), ("free proc with > 1 ksegrp"));
156 #endif
157 	KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
158 #ifdef KSE
159         kg = FIRST_KSEGRP_IN_PROC(p);
160 	KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
161 #endif
162 	KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
163 #endif
164 
165 	/* Dispose of an alternate kstack, if it exists.
166 	 * XXX What if there are more than one thread in the proc?
167 	 *     The first thread in the proc is special and not
168 	 *     freed, so you gotta do this here.
169 	 */
170 	if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
171 		vm_thread_dispose_altkstack(td);
172 	if (p->p_ksi != NULL)
173 		KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
174 }
175 
176 /*
177  * Initialize type-stable parts of a proc (when newly created).
178  */
179 static int
180 proc_init(void *mem, int size, int flags)
181 {
182 	struct proc *p;
183 	struct thread *td;
184 #ifdef KSE
185 	struct ksegrp *kg;
186 #endif
187 
188 	p = (struct proc *)mem;
189 	p->p_sched = (struct p_sched *)&p[1];
190 	td = thread_alloc();
191 #ifdef KSE
192 	kg = ksegrp_alloc();
193 #endif
194 	bzero(&p->p_mtx, sizeof(struct mtx));
195 	mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
196 	p->p_stats = pstats_alloc();
197 #ifdef KSE
198 	proc_linkup(p, kg, td);
199 	sched_newproc(p, kg, td);
200 #else
201 	proc_linkup(p, td);
202 #endif
203 	return (0);
204 }
205 
206 /*
207  * UMA should ensure that this function is never called.
208  * Freeing a proc structure would violate type stability.
209  */
210 static void
211 proc_fini(void *mem, int size)
212 {
213 #ifdef notnow
214 	struct proc *p;
215 
216 	p = (struct proc *)mem;
217 	pstats_free(p->p_stats);
218 #ifdef KSE
219 	ksegrp_free(FIRST_KSEGRP_IN_PROC(p));
220 #endif
221 	thread_free(FIRST_THREAD_IN_PROC(p));
222 	mtx_destroy(&p->p_mtx);
223 	if (p->p_ksi != NULL)
224 		ksiginfo_free(p->p_ksi);
225 #else
226 	panic("proc reclaimed");
227 #endif
228 }
229 
230 /*
231  * Is p an inferior of the current process?
232  */
233 int
234 inferior(p)
235 	register struct proc *p;
236 {
237 
238 	sx_assert(&proctree_lock, SX_LOCKED);
239 	for (; p != curproc; p = p->p_pptr)
240 		if (p->p_pid == 0)
241 			return (0);
242 	return (1);
243 }
244 
245 /*
246  * Locate a process by number; return only "live" processes -- i.e., neither
247  * zombies nor newly born but incompletely initialized processes.  By not
248  * returning processes in the PRS_NEW state, we allow callers to avoid
249  * testing for that condition to avoid dereferencing p_ucred, et al.
250  */
251 struct proc *
252 pfind(pid)
253 	register pid_t pid;
254 {
255 	register struct proc *p;
256 
257 	sx_slock(&allproc_lock);
258 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
259 		if (p->p_pid == pid) {
260 			if (p->p_state == PRS_NEW) {
261 				p = NULL;
262 				break;
263 			}
264 			PROC_LOCK(p);
265 			break;
266 		}
267 	sx_sunlock(&allproc_lock);
268 	return (p);
269 }
270 
271 /*
272  * Locate a process group by number.
273  * The caller must hold proctree_lock.
274  */
275 struct pgrp *
276 pgfind(pgid)
277 	register pid_t pgid;
278 {
279 	register struct pgrp *pgrp;
280 
281 	sx_assert(&proctree_lock, SX_LOCKED);
282 
283 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
284 		if (pgrp->pg_id == pgid) {
285 			PGRP_LOCK(pgrp);
286 			return (pgrp);
287 		}
288 	}
289 	return (NULL);
290 }
291 
292 /*
293  * Create a new process group.
294  * pgid must be equal to the pid of p.
295  * Begin a new session if required.
296  */
297 int
298 enterpgrp(p, pgid, pgrp, sess)
299 	register struct proc *p;
300 	pid_t pgid;
301 	struct pgrp *pgrp;
302 	struct session *sess;
303 {
304 	struct pgrp *pgrp2;
305 
306 	sx_assert(&proctree_lock, SX_XLOCKED);
307 
308 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
309 	KASSERT(p->p_pid == pgid,
310 	    ("enterpgrp: new pgrp and pid != pgid"));
311 
312 	pgrp2 = pgfind(pgid);
313 
314 	KASSERT(pgrp2 == NULL,
315 	    ("enterpgrp: pgrp with pgid exists"));
316 	KASSERT(!SESS_LEADER(p),
317 	    ("enterpgrp: session leader attempted setpgrp"));
318 
319 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
320 
321 	if (sess != NULL) {
322 		/*
323 		 * new session
324 		 */
325 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
326 		mtx_lock(&Giant);       /* XXX TTY */
327 		PROC_LOCK(p);
328 		p->p_flag &= ~P_CONTROLT;
329 		PROC_UNLOCK(p);
330 		PGRP_LOCK(pgrp);
331 		sess->s_leader = p;
332 		sess->s_sid = p->p_pid;
333 		sess->s_count = 1;
334 		sess->s_ttyvp = NULL;
335 		sess->s_ttyp = NULL;
336 		bcopy(p->p_session->s_login, sess->s_login,
337 			    sizeof(sess->s_login));
338 		pgrp->pg_session = sess;
339 		KASSERT(p == curproc,
340 		    ("enterpgrp: mksession and p != curproc"));
341 	} else {
342 		mtx_lock(&Giant);       /* XXX TTY */
343 		pgrp->pg_session = p->p_session;
344 		SESS_LOCK(pgrp->pg_session);
345 		pgrp->pg_session->s_count++;
346 		SESS_UNLOCK(pgrp->pg_session);
347 		PGRP_LOCK(pgrp);
348 	}
349 	pgrp->pg_id = pgid;
350 	LIST_INIT(&pgrp->pg_members);
351 
352 	/*
353 	 * As we have an exclusive lock of proctree_lock,
354 	 * this should not deadlock.
355 	 */
356 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
357 	pgrp->pg_jobc = 0;
358 	SLIST_INIT(&pgrp->pg_sigiolst);
359 	PGRP_UNLOCK(pgrp);
360 	mtx_unlock(&Giant);       /* XXX TTY */
361 
362 	doenterpgrp(p, pgrp);
363 
364 	return (0);
365 }
366 
367 /*
368  * Move p to an existing process group
369  */
370 int
371 enterthispgrp(p, pgrp)
372 	register struct proc *p;
373 	struct pgrp *pgrp;
374 {
375 
376 	sx_assert(&proctree_lock, SX_XLOCKED);
377 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
378 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
379 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
380 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
381 	KASSERT(pgrp->pg_session == p->p_session,
382 		("%s: pgrp's session %p, p->p_session %p.\n",
383 		__func__,
384 		pgrp->pg_session,
385 		p->p_session));
386 	KASSERT(pgrp != p->p_pgrp,
387 		("%s: p belongs to pgrp.", __func__));
388 
389 	doenterpgrp(p, pgrp);
390 
391 	return (0);
392 }
393 
394 /*
395  * Move p to a process group
396  */
397 static void
398 doenterpgrp(p, pgrp)
399 	struct proc *p;
400 	struct pgrp *pgrp;
401 {
402 	struct pgrp *savepgrp;
403 
404 	sx_assert(&proctree_lock, SX_XLOCKED);
405 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
406 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
407 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
408 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
409 
410 	savepgrp = p->p_pgrp;
411 
412 	/*
413 	 * Adjust eligibility of affected pgrps to participate in job control.
414 	 * Increment eligibility counts before decrementing, otherwise we
415 	 * could reach 0 spuriously during the first call.
416 	 */
417 	fixjobc(p, pgrp, 1);
418 	fixjobc(p, p->p_pgrp, 0);
419 
420 	mtx_lock(&Giant);       /* XXX TTY */
421 	PGRP_LOCK(pgrp);
422 	PGRP_LOCK(savepgrp);
423 	PROC_LOCK(p);
424 	LIST_REMOVE(p, p_pglist);
425 	p->p_pgrp = pgrp;
426 	PROC_UNLOCK(p);
427 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
428 	PGRP_UNLOCK(savepgrp);
429 	PGRP_UNLOCK(pgrp);
430 	mtx_unlock(&Giant);     /* XXX TTY */
431 	if (LIST_EMPTY(&savepgrp->pg_members))
432 		pgdelete(savepgrp);
433 }
434 
435 /*
436  * remove process from process group
437  */
438 int
439 leavepgrp(p)
440 	register struct proc *p;
441 {
442 	struct pgrp *savepgrp;
443 
444 	sx_assert(&proctree_lock, SX_XLOCKED);
445 	savepgrp = p->p_pgrp;
446 	mtx_lock(&Giant);	/* XXX TTY */
447 	PGRP_LOCK(savepgrp);
448 	PROC_LOCK(p);
449 	LIST_REMOVE(p, p_pglist);
450 	p->p_pgrp = NULL;
451 	PROC_UNLOCK(p);
452 	PGRP_UNLOCK(savepgrp);
453 	mtx_unlock(&Giant);	/* XXX TTY */
454 	if (LIST_EMPTY(&savepgrp->pg_members))
455 		pgdelete(savepgrp);
456 	return (0);
457 }
458 
459 /*
460  * delete a process group
461  */
462 static void
463 pgdelete(pgrp)
464 	register struct pgrp *pgrp;
465 {
466 	struct session *savesess;
467 
468 	sx_assert(&proctree_lock, SX_XLOCKED);
469 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
470 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
471 
472 	/*
473 	 * Reset any sigio structures pointing to us as a result of
474 	 * F_SETOWN with our pgid.
475 	 */
476 	funsetownlst(&pgrp->pg_sigiolst);
477 
478 	mtx_lock(&Giant);       /* XXX TTY */
479 	PGRP_LOCK(pgrp);
480 	if (pgrp->pg_session->s_ttyp != NULL &&
481 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
482 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
483 	LIST_REMOVE(pgrp, pg_hash);
484 	savesess = pgrp->pg_session;
485 	SESSRELE(savesess);
486 	PGRP_UNLOCK(pgrp);
487 	mtx_destroy(&pgrp->pg_mtx);
488 	FREE(pgrp, M_PGRP);
489 	mtx_unlock(&Giant);     /* XXX TTY */
490 }
491 
492 static void
493 pgadjustjobc(pgrp, entering)
494 	struct pgrp *pgrp;
495 	int entering;
496 {
497 
498 	PGRP_LOCK(pgrp);
499 	if (entering)
500 		pgrp->pg_jobc++;
501 	else {
502 		--pgrp->pg_jobc;
503 		if (pgrp->pg_jobc == 0)
504 			orphanpg(pgrp);
505 	}
506 	PGRP_UNLOCK(pgrp);
507 }
508 
509 /*
510  * Adjust pgrp jobc counters when specified process changes process group.
511  * We count the number of processes in each process group that "qualify"
512  * the group for terminal job control (those with a parent in a different
513  * process group of the same session).  If that count reaches zero, the
514  * process group becomes orphaned.  Check both the specified process'
515  * process group and that of its children.
516  * entering == 0 => p is leaving specified group.
517  * entering == 1 => p is entering specified group.
518  */
519 void
520 fixjobc(p, pgrp, entering)
521 	register struct proc *p;
522 	register struct pgrp *pgrp;
523 	int entering;
524 {
525 	register struct pgrp *hispgrp;
526 	register struct session *mysession;
527 
528 	sx_assert(&proctree_lock, SX_LOCKED);
529 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
530 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
531 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
532 
533 	/*
534 	 * Check p's parent to see whether p qualifies its own process
535 	 * group; if so, adjust count for p's process group.
536 	 */
537 	mysession = pgrp->pg_session;
538 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
539 	    hispgrp->pg_session == mysession)
540 		pgadjustjobc(pgrp, entering);
541 
542 	/*
543 	 * Check this process' children to see whether they qualify
544 	 * their process groups; if so, adjust counts for children's
545 	 * process groups.
546 	 */
547 	LIST_FOREACH(p, &p->p_children, p_sibling) {
548 		hispgrp = p->p_pgrp;
549 		if (hispgrp == pgrp ||
550 		    hispgrp->pg_session != mysession)
551 			continue;
552 		PROC_LOCK(p);
553 		if (p->p_state == PRS_ZOMBIE) {
554 			PROC_UNLOCK(p);
555 			continue;
556 		}
557 		PROC_UNLOCK(p);
558 		pgadjustjobc(hispgrp, entering);
559 	}
560 }
561 
562 /*
563  * A process group has become orphaned;
564  * if there are any stopped processes in the group,
565  * hang-up all process in that group.
566  */
567 static void
568 orphanpg(pg)
569 	struct pgrp *pg;
570 {
571 	register struct proc *p;
572 
573 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
574 
575 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
576 		PROC_LOCK(p);
577 		if (P_SHOULDSTOP(p)) {
578 			PROC_UNLOCK(p);
579 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
580 				PROC_LOCK(p);
581 				psignal(p, SIGHUP);
582 				psignal(p, SIGCONT);
583 				PROC_UNLOCK(p);
584 			}
585 			return;
586 		}
587 		PROC_UNLOCK(p);
588 	}
589 }
590 
591 void
592 sessrele(struct session *s)
593 {
594 	int i;
595 
596 	SESS_LOCK(s);
597 	i = --s->s_count;
598 	SESS_UNLOCK(s);
599 	if (i == 0) {
600 		if (s->s_ttyp != NULL)
601 			ttyrel(s->s_ttyp);
602 		mtx_destroy(&s->s_mtx);
603 		FREE(s, M_SESSION);
604 	}
605 }
606 
607 #include "opt_ddb.h"
608 #ifdef DDB
609 #include <ddb/ddb.h>
610 
611 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
612 {
613 	register struct pgrp *pgrp;
614 	register struct proc *p;
615 	register int i;
616 
617 	for (i = 0; i <= pgrphash; i++) {
618 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
619 			printf("\tindx %d\n", i);
620 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
621 				printf(
622 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
623 				    (void *)pgrp, (long)pgrp->pg_id,
624 				    (void *)pgrp->pg_session,
625 				    pgrp->pg_session->s_count,
626 				    (void *)LIST_FIRST(&pgrp->pg_members));
627 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
628 					printf("\t\tpid %ld addr %p pgrp %p\n",
629 					    (long)p->p_pid, (void *)p,
630 					    (void *)p->p_pgrp);
631 				}
632 			}
633 		}
634 	}
635 }
636 #endif /* DDB */
637 
638 /*
639  * Clear kinfo_proc and fill in any information that is common
640  * to all threads in the process.
641  * Must be called with the target process locked.
642  */
643 static void
644 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
645 {
646 	struct thread *td0;
647 	struct tty *tp;
648 	struct session *sp;
649 	struct ucred *cred;
650 	struct sigacts *ps;
651 
652 	bzero(kp, sizeof(*kp));
653 
654 	kp->ki_structsize = sizeof(*kp);
655 	kp->ki_paddr = p;
656 	PROC_LOCK_ASSERT(p, MA_OWNED);
657 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
658 	kp->ki_args = p->p_args;
659 	kp->ki_textvp = p->p_textvp;
660 #ifdef KTRACE
661 	kp->ki_tracep = p->p_tracevp;
662 	mtx_lock(&ktrace_mtx);
663 	kp->ki_traceflag = p->p_traceflag;
664 	mtx_unlock(&ktrace_mtx);
665 #endif
666 	kp->ki_fd = p->p_fd;
667 	kp->ki_vmspace = p->p_vmspace;
668 	kp->ki_flag = p->p_flag;
669 	cred = p->p_ucred;
670 	if (cred) {
671 		kp->ki_uid = cred->cr_uid;
672 		kp->ki_ruid = cred->cr_ruid;
673 		kp->ki_svuid = cred->cr_svuid;
674 		/* XXX bde doesn't like KI_NGROUPS */
675 		kp->ki_ngroups = min(cred->cr_ngroups, KI_NGROUPS);
676 		bcopy(cred->cr_groups, kp->ki_groups,
677 		    kp->ki_ngroups * sizeof(gid_t));
678 		kp->ki_rgid = cred->cr_rgid;
679 		kp->ki_svgid = cred->cr_svgid;
680 		/* If jailed(cred), emulate the old P_JAILED flag. */
681 		if (jailed(cred)) {
682 			kp->ki_flag |= P_JAILED;
683 			/* If inside a jail, use 0 as a jail ID. */
684 			if (!jailed(curthread->td_ucred))
685 				kp->ki_jid = cred->cr_prison->pr_id;
686 		}
687 	}
688 	ps = p->p_sigacts;
689 	if (ps) {
690 		mtx_lock(&ps->ps_mtx);
691 		kp->ki_sigignore = ps->ps_sigignore;
692 		kp->ki_sigcatch = ps->ps_sigcatch;
693 		mtx_unlock(&ps->ps_mtx);
694 	}
695 	mtx_lock_spin(&sched_lock);
696 	if (p->p_state != PRS_NEW &&
697 	    p->p_state != PRS_ZOMBIE &&
698 	    p->p_vmspace != NULL) {
699 		struct vmspace *vm = p->p_vmspace;
700 
701 		kp->ki_size = vm->vm_map.size;
702 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
703 		FOREACH_THREAD_IN_PROC(p, td0) {
704 			if (!TD_IS_SWAPPED(td0))
705 				kp->ki_rssize += td0->td_kstack_pages;
706 			if (td0->td_altkstack_obj != NULL)
707 				kp->ki_rssize += td0->td_altkstack_pages;
708 		}
709 		kp->ki_swrss = vm->vm_swrss;
710 		kp->ki_tsize = vm->vm_tsize;
711 		kp->ki_dsize = vm->vm_dsize;
712 		kp->ki_ssize = vm->vm_ssize;
713 	} else if (p->p_state == PRS_ZOMBIE)
714 		kp->ki_stat = SZOMB;
715 	kp->ki_sflag = p->p_sflag;
716 	kp->ki_swtime = p->p_swtime;
717 	kp->ki_pid = p->p_pid;
718 	kp->ki_nice = p->p_nice;
719 	kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
720 	mtx_unlock_spin(&sched_lock);
721 	if ((p->p_sflag & PS_INMEM) && p->p_stats != NULL) {
722 		kp->ki_start = p->p_stats->p_start;
723 		timevaladd(&kp->ki_start, &boottime);
724 		kp->ki_rusage = p->p_stats->p_ru;
725 		calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
726 		calccru(p, &kp->ki_childutime, &kp->ki_childstime);
727 
728 		/* Some callers want child-times in a single value */
729 		kp->ki_childtime = kp->ki_childstime;
730 		timevaladd(&kp->ki_childtime, &kp->ki_childutime);
731 	}
732 	tp = NULL;
733 	if (p->p_pgrp) {
734 		kp->ki_pgid = p->p_pgrp->pg_id;
735 		kp->ki_jobc = p->p_pgrp->pg_jobc;
736 		sp = p->p_pgrp->pg_session;
737 
738 		if (sp != NULL) {
739 			kp->ki_sid = sp->s_sid;
740 			SESS_LOCK(sp);
741 			strlcpy(kp->ki_login, sp->s_login,
742 			    sizeof(kp->ki_login));
743 			if (sp->s_ttyvp)
744 				kp->ki_kiflag |= KI_CTTY;
745 			if (SESS_LEADER(p))
746 				kp->ki_kiflag |= KI_SLEADER;
747 			tp = sp->s_ttyp;
748 			SESS_UNLOCK(sp);
749 		}
750 	}
751 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
752 		kp->ki_tdev = dev2udev(tp->t_dev);
753 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
754 		if (tp->t_session)
755 			kp->ki_tsid = tp->t_session->s_sid;
756 	} else
757 		kp->ki_tdev = NODEV;
758 	if (p->p_comm[0] != '\0') {
759 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
760 		/*
761 		 * Temporarily give the thread a default name of the process
762 		 * as it's erroneously used in the snmp code.
763 		 * Remove this when that is fixed. (soon I'm told)
764 		 */
765 		strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
766 	}
767 	if (p->p_sysent && p->p_sysent->sv_name != NULL &&
768 	    p->p_sysent->sv_name[0] != '\0')
769 		strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
770 	kp->ki_siglist = p->p_siglist;
771 	kp->ki_xstat = p->p_xstat;
772 	kp->ki_acflag = p->p_acflag;
773 	kp->ki_lock = p->p_lock;
774 	if (p->p_pptr)
775 		kp->ki_ppid = p->p_pptr->p_pid;
776 }
777 
778 /*
779  * Fill in information that is thread specific.
780  * Must be called with sched_lock locked.
781  */
782 static void
783 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp)
784 {
785 #ifdef KSE
786 	struct ksegrp *kg;
787 #endif
788 	struct proc *p;
789 
790 	p = td->td_proc;
791 
792 	if (td->td_wmesg != NULL)
793 		strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
794 	else
795 		bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
796 	if (td->td_name[0] != '\0')
797 		strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
798 	if (TD_ON_LOCK(td)) {
799 		kp->ki_kiflag |= KI_LOCKBLOCK;
800 		strlcpy(kp->ki_lockname, td->td_lockname,
801 		    sizeof(kp->ki_lockname));
802 	} else {
803 		kp->ki_kiflag &= ~KI_LOCKBLOCK;
804 		bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
805 	}
806 
807 	if (p->p_state == PRS_NORMAL) { /*  XXXKSE very approximate */
808 		if (TD_ON_RUNQ(td) ||
809 		    TD_CAN_RUN(td) ||
810 		    TD_IS_RUNNING(td)) {
811 			kp->ki_stat = SRUN;
812 		} else if (P_SHOULDSTOP(p)) {
813 			kp->ki_stat = SSTOP;
814 		} else if (TD_IS_SLEEPING(td)) {
815 			kp->ki_stat = SSLEEP;
816 		} else if (TD_ON_LOCK(td)) {
817 			kp->ki_stat = SLOCK;
818 		} else {
819 			kp->ki_stat = SWAIT;
820 		}
821 	} else if (p->p_state == PRS_ZOMBIE) {
822 		kp->ki_stat = SZOMB;
823 	} else {
824 		kp->ki_stat = SIDL;
825 	}
826 
827 #ifdef KSE
828 	kg = td->td_ksegrp;
829 
830 	/* things in the KSE GROUP */
831 	kp->ki_estcpu = kg->kg_estcpu;
832 	kp->ki_slptime = kg->kg_slptime;
833 	kp->ki_pri.pri_user = kg->kg_user_pri;
834 	kp->ki_pri.pri_class = kg->kg_pri_class;
835 #endif
836 	/* Things in the thread */
837 	kp->ki_wchan = td->td_wchan;
838 	kp->ki_pri.pri_level = td->td_priority;
839 	kp->ki_pri.pri_native = td->td_base_pri;
840 	kp->ki_lastcpu = td->td_lastcpu;
841 	kp->ki_oncpu = td->td_oncpu;
842 	kp->ki_tdflags = td->td_flags;
843 	kp->ki_tid = td->td_tid;
844 	kp->ki_numthreads = p->p_numthreads;
845 	kp->ki_pcb = td->td_pcb;
846 	kp->ki_kstack = (void *)td->td_kstack;
847 	kp->ki_pctcpu = sched_pctcpu(td);
848 #ifndef KSE
849 	kp->ki_estcpu = td->td_estcpu;
850 	kp->ki_slptime = td->td_slptime;
851 	kp->ki_pri.pri_class = td->td_pri_class;
852 	kp->ki_pri.pri_user = td->td_user_pri;
853 #endif
854 
855 	/* We can't get this anymore but ps etc never used it anyway. */
856 	kp->ki_rqindex = 0;
857 
858 	SIGSETOR(kp->ki_siglist, td->td_siglist);
859 	kp->ki_sigmask = td->td_sigmask;
860 }
861 
862 /*
863  * Fill in a kinfo_proc structure for the specified process.
864  * Must be called with the target process locked.
865  */
866 void
867 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
868 {
869 
870 	fill_kinfo_proc_only(p, kp);
871 	mtx_lock_spin(&sched_lock);
872 	if (FIRST_THREAD_IN_PROC(p) != NULL)
873 		fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp);
874 	mtx_unlock_spin(&sched_lock);
875 }
876 
877 struct pstats *
878 pstats_alloc(void)
879 {
880 
881 	return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
882 }
883 
884 /*
885  * Copy parts of p_stats; zero the rest of p_stats (statistics).
886  */
887 void
888 pstats_fork(struct pstats *src, struct pstats *dst)
889 {
890 
891 	bzero(&dst->pstat_startzero,
892 	    __rangeof(struct pstats, pstat_startzero, pstat_endzero));
893 	bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
894 	    __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
895 }
896 
897 void
898 pstats_free(struct pstats *ps)
899 {
900 
901 	free(ps, M_SUBPROC);
902 }
903 
904 /*
905  * Locate a zombie process by number
906  */
907 struct proc *
908 zpfind(pid_t pid)
909 {
910 	struct proc *p;
911 
912 	sx_slock(&allproc_lock);
913 	LIST_FOREACH(p, &zombproc, p_list)
914 		if (p->p_pid == pid) {
915 			PROC_LOCK(p);
916 			break;
917 		}
918 	sx_sunlock(&allproc_lock);
919 	return (p);
920 }
921 
922 #define KERN_PROC_ZOMBMASK	0x3
923 #define KERN_PROC_NOTHREADS	0x4
924 
925 /*
926  * Must be called with the process locked and will return with it unlocked.
927  */
928 static int
929 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
930 {
931 	struct thread *td;
932 	struct kinfo_proc kinfo_proc;
933 	int error = 0;
934 	struct proc *np;
935 	pid_t pid = p->p_pid;
936 
937 	PROC_LOCK_ASSERT(p, MA_OWNED);
938 
939 	fill_kinfo_proc_only(p, &kinfo_proc);
940 	if (flags & KERN_PROC_NOTHREADS) {
941 		mtx_lock_spin(&sched_lock);
942 		if (FIRST_THREAD_IN_PROC(p) != NULL)
943 			fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), &kinfo_proc);
944 		mtx_unlock_spin(&sched_lock);
945 		error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
946 				   sizeof(kinfo_proc));
947 	} else {
948 		mtx_lock_spin(&sched_lock);
949 		if (FIRST_THREAD_IN_PROC(p) != NULL)
950 			FOREACH_THREAD_IN_PROC(p, td) {
951 				fill_kinfo_thread(td, &kinfo_proc);
952 				error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
953 						   sizeof(kinfo_proc));
954 				if (error)
955 					break;
956 			}
957 		else
958 			error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc,
959 					   sizeof(kinfo_proc));
960 		mtx_unlock_spin(&sched_lock);
961 	}
962 	PROC_UNLOCK(p);
963 	if (error)
964 		return (error);
965 	if (flags & KERN_PROC_ZOMBMASK)
966 		np = zpfind(pid);
967 	else {
968 		if (pid == 0)
969 			return (0);
970 		np = pfind(pid);
971 	}
972 	if (np == NULL)
973 		return EAGAIN;
974 	if (np != p) {
975 		PROC_UNLOCK(np);
976 		return EAGAIN;
977 	}
978 	PROC_UNLOCK(np);
979 	return (0);
980 }
981 
982 static int
983 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
984 {
985 	int *name = (int*) arg1;
986 	u_int namelen = arg2;
987 	struct proc *p;
988 	int flags, doingzomb, oid_number;
989 	int error = 0;
990 
991 	oid_number = oidp->oid_number;
992 	if (oid_number != KERN_PROC_ALL &&
993 	    (oid_number & KERN_PROC_INC_THREAD) == 0)
994 		flags = KERN_PROC_NOTHREADS;
995 	else {
996 		flags = 0;
997 		oid_number &= ~KERN_PROC_INC_THREAD;
998 	}
999 	if (oid_number == KERN_PROC_PID) {
1000 		if (namelen != 1)
1001 			return (EINVAL);
1002 		error = sysctl_wire_old_buffer(req, 0);
1003 		if (error)
1004 			return (error);
1005 		p = pfind((pid_t)name[0]);
1006 		if (!p)
1007 			return (ESRCH);
1008 		if ((error = p_cansee(curthread, p))) {
1009 			PROC_UNLOCK(p);
1010 			return (error);
1011 		}
1012 		error = sysctl_out_proc(p, req, flags);
1013 		return (error);
1014 	}
1015 
1016 	switch (oid_number) {
1017 	case KERN_PROC_ALL:
1018 		if (namelen != 0)
1019 			return (EINVAL);
1020 		break;
1021 	case KERN_PROC_PROC:
1022 		if (namelen != 0 && namelen != 1)
1023 			return (EINVAL);
1024 		break;
1025 	default:
1026 		if (namelen != 1)
1027 			return (EINVAL);
1028 		break;
1029 	}
1030 
1031 	if (!req->oldptr) {
1032 		/* overestimate by 5 procs */
1033 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1034 		if (error)
1035 			return (error);
1036 	}
1037 	error = sysctl_wire_old_buffer(req, 0);
1038 	if (error != 0)
1039 		return (error);
1040 	sx_slock(&allproc_lock);
1041 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1042 		if (!doingzomb)
1043 			p = LIST_FIRST(&allproc);
1044 		else
1045 			p = LIST_FIRST(&zombproc);
1046 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1047 			/*
1048 			 * Skip embryonic processes.
1049 			 */
1050 			mtx_lock_spin(&sched_lock);
1051 			if (p->p_state == PRS_NEW) {
1052 				mtx_unlock_spin(&sched_lock);
1053 				continue;
1054 			}
1055 			mtx_unlock_spin(&sched_lock);
1056 			PROC_LOCK(p);
1057 			KASSERT(p->p_ucred != NULL,
1058 			    ("process credential is NULL for non-NEW proc"));
1059 			/*
1060 			 * Show a user only appropriate processes.
1061 			 */
1062 			if (p_cansee(curthread, p)) {
1063 				PROC_UNLOCK(p);
1064 				continue;
1065 			}
1066 			/*
1067 			 * TODO - make more efficient (see notes below).
1068 			 * do by session.
1069 			 */
1070 			switch (oid_number) {
1071 
1072 			case KERN_PROC_GID:
1073 				if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1074 					PROC_UNLOCK(p);
1075 					continue;
1076 				}
1077 				break;
1078 
1079 			case KERN_PROC_PGRP:
1080 				/* could do this by traversing pgrp */
1081 				if (p->p_pgrp == NULL ||
1082 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1083 					PROC_UNLOCK(p);
1084 					continue;
1085 				}
1086 				break;
1087 
1088 			case KERN_PROC_RGID:
1089 				if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1090 					PROC_UNLOCK(p);
1091 					continue;
1092 				}
1093 				break;
1094 
1095 			case KERN_PROC_SESSION:
1096 				if (p->p_session == NULL ||
1097 				    p->p_session->s_sid != (pid_t)name[0]) {
1098 					PROC_UNLOCK(p);
1099 					continue;
1100 				}
1101 				break;
1102 
1103 			case KERN_PROC_TTY:
1104 				if ((p->p_flag & P_CONTROLT) == 0 ||
1105 				    p->p_session == NULL) {
1106 					PROC_UNLOCK(p);
1107 					continue;
1108 				}
1109 				SESS_LOCK(p->p_session);
1110 				if (p->p_session->s_ttyp == NULL ||
1111 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
1112 				    (dev_t)name[0]) {
1113 					SESS_UNLOCK(p->p_session);
1114 					PROC_UNLOCK(p);
1115 					continue;
1116 				}
1117 				SESS_UNLOCK(p->p_session);
1118 				break;
1119 
1120 			case KERN_PROC_UID:
1121 				if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1122 					PROC_UNLOCK(p);
1123 					continue;
1124 				}
1125 				break;
1126 
1127 			case KERN_PROC_RUID:
1128 				if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1129 					PROC_UNLOCK(p);
1130 					continue;
1131 				}
1132 				break;
1133 
1134 			case KERN_PROC_PROC:
1135 				break;
1136 
1137 			default:
1138 				break;
1139 
1140 			}
1141 
1142 			error = sysctl_out_proc(p, req, flags | doingzomb);
1143 			if (error) {
1144 				sx_sunlock(&allproc_lock);
1145 				return (error);
1146 			}
1147 		}
1148 	}
1149 	sx_sunlock(&allproc_lock);
1150 	return (0);
1151 }
1152 
1153 struct pargs *
1154 pargs_alloc(int len)
1155 {
1156 	struct pargs *pa;
1157 
1158 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
1159 		M_WAITOK);
1160 	refcount_init(&pa->ar_ref, 1);
1161 	pa->ar_length = len;
1162 	return (pa);
1163 }
1164 
1165 void
1166 pargs_free(struct pargs *pa)
1167 {
1168 
1169 	FREE(pa, M_PARGS);
1170 }
1171 
1172 void
1173 pargs_hold(struct pargs *pa)
1174 {
1175 
1176 	if (pa == NULL)
1177 		return;
1178 	refcount_acquire(&pa->ar_ref);
1179 }
1180 
1181 void
1182 pargs_drop(struct pargs *pa)
1183 {
1184 
1185 	if (pa == NULL)
1186 		return;
1187 	if (refcount_release(&pa->ar_ref))
1188 		pargs_free(pa);
1189 }
1190 
1191 /*
1192  * This sysctl allows a process to retrieve the argument list or process
1193  * title for another process without groping around in the address space
1194  * of the other process.  It also allow a process to set its own "process
1195  * title to a string of its own choice.
1196  */
1197 static int
1198 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1199 {
1200 	int *name = (int*) arg1;
1201 	u_int namelen = arg2;
1202 	struct pargs *newpa, *pa;
1203 	struct proc *p;
1204 	int error = 0;
1205 
1206 	if (namelen != 1)
1207 		return (EINVAL);
1208 
1209 	p = pfind((pid_t)name[0]);
1210 	if (!p)
1211 		return (ESRCH);
1212 
1213 	if ((error = p_cansee(curthread, p)) != 0) {
1214 		PROC_UNLOCK(p);
1215 		return (error);
1216 	}
1217 
1218 	if (req->newptr && curproc != p) {
1219 		PROC_UNLOCK(p);
1220 		return (EPERM);
1221 	}
1222 
1223 	pa = p->p_args;
1224 	pargs_hold(pa);
1225 	PROC_UNLOCK(p);
1226 	if (req->oldptr != NULL && pa != NULL)
1227 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1228 	pargs_drop(pa);
1229 	if (error != 0 || req->newptr == NULL)
1230 		return (error);
1231 
1232 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1233 		return (ENOMEM);
1234 	newpa = pargs_alloc(req->newlen);
1235 	error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1236 	if (error != 0) {
1237 		pargs_free(newpa);
1238 		return (error);
1239 	}
1240 	PROC_LOCK(p);
1241 	pa = p->p_args;
1242 	p->p_args = newpa;
1243 	PROC_UNLOCK(p);
1244 	pargs_drop(pa);
1245 	return (0);
1246 }
1247 
1248 /*
1249  * This sysctl allows a process to retrieve the path of the executable for
1250  * itself or another process.
1251  */
1252 static int
1253 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1254 {
1255 	pid_t *pidp = (pid_t *)arg1;
1256 	unsigned int arglen = arg2;
1257 	struct proc *p;
1258 	struct vnode *vp;
1259 	char *retbuf, *freebuf;
1260 	int error;
1261 
1262 	if (arglen != 1)
1263 		return (EINVAL);
1264 	if (*pidp == -1) {	/* -1 means this process */
1265 		p = req->td->td_proc;
1266 	} else {
1267 		p = pfind(*pidp);
1268 		if (p == NULL)
1269 			return (ESRCH);
1270 		if ((error = p_cansee(curthread, p)) != 0) {
1271 			PROC_UNLOCK(p);
1272 			return (error);
1273 		}
1274 	}
1275 
1276 	vp = p->p_textvp;
1277 	vref(vp);
1278 	if (*pidp != -1)
1279 		PROC_UNLOCK(p);
1280 	error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1281 	vrele(vp);
1282 	if (error)
1283 		return (error);
1284 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1285 	free(freebuf, M_TEMP);
1286 	return (error);
1287 }
1288 
1289 static int
1290 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1291 {
1292 	struct proc *p;
1293 	char *sv_name;
1294 	int *name;
1295 	int namelen;
1296 	int error;
1297 
1298 	namelen = arg2;
1299 	if (namelen != 1)
1300 		return (EINVAL);
1301 
1302 	name = (int *)arg1;
1303 	if ((p = pfind((pid_t)name[0])) == NULL)
1304 		return (ESRCH);
1305 	if ((error = p_cansee(curthread, p))) {
1306 		PROC_UNLOCK(p);
1307 		return (error);
1308 	}
1309 	sv_name = p->p_sysent->sv_name;
1310 	PROC_UNLOCK(p);
1311 	return (sysctl_handle_string(oidp, sv_name, 0, req));
1312 }
1313 
1314 
1315 static SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1316 
1317 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1318 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1319 
1320 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD,
1321 	sysctl_kern_proc, "Process table");
1322 
1323 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1324 	sysctl_kern_proc, "Process table");
1325 
1326 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD,
1327 	sysctl_kern_proc, "Process table");
1328 
1329 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD,
1330 	sysctl_kern_proc, "Process table");
1331 
1332 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1333 	sysctl_kern_proc, "Process table");
1334 
1335 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1336 	sysctl_kern_proc, "Process table");
1337 
1338 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1339 	sysctl_kern_proc, "Process table");
1340 
1341 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1342 	sysctl_kern_proc, "Process table");
1343 
1344 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD,
1345 	sysctl_kern_proc, "Return process table, no threads");
1346 
1347 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
1348 	CTLFLAG_RW | CTLFLAG_ANYBODY,
1349 	sysctl_kern_proc_args, "Process argument list");
1350 
1351 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
1352 	sysctl_kern_proc_pathname, "Process executable path");
1353 
1354 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD,
1355 	sysctl_kern_proc_sv_name, "Process syscall vector name (ABI type)");
1356 
1357 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
1358 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1359 
1360 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
1361 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1362 
1363 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
1364 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1365 
1366 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
1367 	sid_td, CTLFLAG_RD, sysctl_kern_proc, "Process table");
1368 
1369 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
1370 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1371 
1372 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
1373 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1374 
1375 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
1376 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1377 
1378 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
1379 	CTLFLAG_RD, sysctl_kern_proc, "Process table");
1380 
1381 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
1382 	CTLFLAG_RD, sysctl_kern_proc, "Return process table, no threads");
1383