xref: /freebsd/sys/kern/kern_proc.c (revision 1b6c2589164a3a7b2f62d4c28c2ffa1be860959e)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/filedesc.h>
46 #include <sys/tty.h>
47 #include <sys/signalvar.h>
48 #include <sys/sx.h>
49 #include <sys/user.h>
50 #include <sys/jail.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_zone.h>
56 
57 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
58 MALLOC_DEFINE(M_SESSION, "session", "session header");
59 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
60 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
61 
62 static void pgdelete	__P((struct pgrp *));
63 
64 static void	orphanpg __P((struct pgrp *pg));
65 
66 /*
67  * Other process lists
68  */
69 struct pidhashhead *pidhashtbl;
70 u_long pidhash;
71 struct pgrphashhead *pgrphashtbl;
72 u_long pgrphash;
73 struct proclist allproc;
74 struct proclist zombproc;
75 struct sx allproc_lock;
76 struct sx proctree_lock;
77 vm_zone_t proc_zone;
78 vm_zone_t ithread_zone;
79 
80 /*
81  * Initialize global process hashing structures.
82  */
83 void
84 procinit()
85 {
86 	int i, j;
87 
88 	sx_init(&allproc_lock, "allproc");
89 	sx_init(&proctree_lock, "proctree");
90 	LIST_INIT(&allproc);
91 	LIST_INIT(&zombproc);
92 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
93 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
94 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
95 	uihashinit();
96 	/*
97 	 * This should really be a compile time warning, but I do
98 	 * not know of any way to do that...
99 	 */
100 	if (sizeof(struct kinfo_proc) != KINFO_PROC_SIZE) {
101 		printf("This message will repeat for the next 20 seconds\n");
102 		for (i = 0; i < 20; i++) {
103 			printf("WARNING: size of kinfo_proc (%ld) should be %d!!!\n",
104 			    (long)sizeof(struct kinfo_proc), KINFO_PROC_SIZE);
105 			printf("The kinfo_proc structure was changed ");
106 			printf("incorrectly in <sys/user.h>\n");
107 			for (j = 0; j < 0x7ffffff; j++);
108 		}
109 
110 	}
111 }
112 
113 /*
114  * link up a process structure and it's inbuilt threads etc.
115  */
116 void
117 proc_linkup(struct proc *p)
118 {
119 	struct thread *td;
120 
121 	td = &p->p_thread;
122 
123 	/**** lists headed in the proc structure ****/
124 	/* ALL KSEGRPs in this process */
125 	TAILQ_INIT(       &p->p_ksegrps);	     /* all ksegrps in proc */
126 	TAILQ_INSERT_HEAD(&p->p_ksegrps, &p->p_ksegrp, kg_ksegrp);
127 
128 	/* All threads in this process (an optimisation) */
129 	TAILQ_INIT(       &p->p_threads);	     /* all threads in proc */
130 	TAILQ_INSERT_HEAD(&p->p_threads, &p->p_thread, td_plist);
131 
132 	/**** Lists headed in the KSEGROUP structure ****/
133 	/* all thread in this ksegroup */
134 	TAILQ_INIT(       &p->p_ksegrp.kg_threads);
135 	TAILQ_INSERT_HEAD(&p->p_ksegrp.kg_threads, &p->p_thread, td_kglist);
136 
137 	/* All runnable threads not assigned to a particular KSE */
138 	/* XXXKSE THIS MAY GO AWAY.. KSEs are never unassigned */
139 	TAILQ_INIT(       &p->p_ksegrp.kg_runq); /* links with td_runq */
140 
141 	/* All threads presently not runnable (Thread starts this way) */
142 	TAILQ_INIT(       &p->p_ksegrp.kg_slpq); /* links with td_runq */
143 	TAILQ_INSERT_HEAD(&p->p_ksegrp.kg_slpq, &p->p_thread, td_runq);
144 	/*p->p_thread.td_flags &= ~TDF_ONRUNQ;*/
145 
146 	/* all KSEs in this ksegroup */
147 	TAILQ_INIT(       &p->p_ksegrp.kg_kseq);     /* all kses in ksegrp */
148 	TAILQ_INSERT_HEAD(&p->p_ksegrp.kg_kseq, &p->p_kse, ke_kglist);
149 
150 	/* KSE starts out idle *//* XXXKSE */
151 	TAILQ_INIT(       &p->p_ksegrp.kg_rq);     /* all kses in ksegrp */
152 	TAILQ_INIT(       &p->p_ksegrp.kg_iq);     /* all kses in ksegrp */
153 #if 0
154 	TAILQ_INSERT_HEAD(&p->p_ksegrp.kg_iq, &p->p_kse, ke_kgrlist);
155 #endif  /* is running, not idle */
156 	/*p->p_kse.ke_flags &= &KEF_ONRUNQ;*/
157 
158 	/**** Lists headed in the KSE structure ****/
159 	/* runnable threads assigned to this kse */
160 	TAILQ_INIT(       &p->p_kse.ke_runq);	    /* links with td_runq */
161 
162 	p->p_thread.td_proc	= p;
163 	p->p_kse.ke_proc	= p;
164 	p->p_ksegrp.kg_proc	= p;
165 
166 	p->p_thread.td_ksegrp	= &p->p_ksegrp;
167 	p->p_kse.ke_ksegrp	= &p->p_ksegrp;
168 
169 	p->p_thread.td_last_kse	= &p->p_kse;
170 	p->p_thread.td_kse	= &p->p_kse;
171 
172 	p->p_kse.ke_thread	= &p->p_thread;
173 
174 	p->p_ksegrp.kg_runnable = 1;
175 	p->p_ksegrp.kg_kses = 1;
176 	p->p_ksegrp.kg_runq_kses = 1; /* XXXKSE change name */
177 }
178 
179 /*
180  * Is p an inferior of the current process?
181  */
182 int
183 inferior(p)
184 	register struct proc *p;
185 {
186 	int rval;
187 
188 	PROC_LOCK_ASSERT(p, MA_OWNED);
189 	if (p == curproc)
190 		return (1);
191 	if (p->p_pid == 0)
192 		return (0);
193 	PROC_LOCK(p->p_pptr);
194 	rval = inferior(p->p_pptr);
195 	PROC_UNLOCK(p->p_pptr);
196 	return (rval);
197 }
198 
199 /*
200  * Locate a process by number
201  */
202 struct proc *
203 pfind(pid)
204 	register pid_t pid;
205 {
206 	register struct proc *p;
207 
208 	sx_slock(&allproc_lock);
209 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
210 		if (p->p_pid == pid) {
211 			PROC_LOCK(p);
212 			break;
213 		}
214 	sx_sunlock(&allproc_lock);
215 	return (p);
216 }
217 
218 /*
219  * Locate a process group by number
220  */
221 struct pgrp *
222 pgfind(pgid)
223 	register pid_t pgid;
224 {
225 	register struct pgrp *pgrp;
226 
227 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
228 		if (pgrp->pg_id == pgid)
229 			return (pgrp);
230 	return (NULL);
231 }
232 
233 /*
234  * Move p to a new or existing process group (and session)
235  */
236 int
237 enterpgrp(p, pgid, mksess)
238 	register struct proc *p;
239 	pid_t pgid;
240 	int mksess;
241 {
242 	register struct pgrp *pgrp = pgfind(pgid);
243 	struct pgrp *savegrp;
244 
245 	KASSERT(pgrp == NULL || !mksess,
246 	    ("enterpgrp: setsid into non-empty pgrp"));
247 	KASSERT(!SESS_LEADER(p),
248 	    ("enterpgrp: session leader attempted setpgrp"));
249 
250 	if (pgrp == NULL) {
251 		pid_t savepid = p->p_pid;
252 		struct proc *np;
253 		/*
254 		 * new process group
255 		 */
256 		KASSERT(p->p_pid == pgid,
257 		    ("enterpgrp: new pgrp and pid != pgid"));
258 		if ((np = pfind(savepid)) == NULL || np != p) {
259 			if (np != NULL)
260 				PROC_UNLOCK(np);
261 			return (ESRCH);
262 		}
263 		PROC_UNLOCK(np);
264 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
265 		    M_WAITOK);
266 		if (mksess) {
267 			register struct session *sess;
268 
269 			/*
270 			 * new session
271 			 */
272 			MALLOC(sess, struct session *, sizeof(struct session),
273 			    M_SESSION, M_WAITOK);
274 			sess->s_leader = p;
275 			sess->s_sid = p->p_pid;
276 			sess->s_count = 1;
277 			sess->s_ttyvp = NULL;
278 			sess->s_ttyp = NULL;
279 			bcopy(p->p_session->s_login, sess->s_login,
280 			    sizeof(sess->s_login));
281 			PROC_LOCK(p);
282 			p->p_flag &= ~P_CONTROLT;
283 			PROC_UNLOCK(p);
284 			pgrp->pg_session = sess;
285 			KASSERT(p == curproc,
286 			    ("enterpgrp: mksession and p != curproc"));
287 		} else {
288 			pgrp->pg_session = p->p_session;
289 			pgrp->pg_session->s_count++;
290 		}
291 		pgrp->pg_id = pgid;
292 		LIST_INIT(&pgrp->pg_members);
293 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
294 		pgrp->pg_jobc = 0;
295 		SLIST_INIT(&pgrp->pg_sigiolst);
296 	} else if (pgrp == p->p_pgrp)
297 		return (0);
298 
299 	/*
300 	 * Adjust eligibility of affected pgrps to participate in job control.
301 	 * Increment eligibility counts before decrementing, otherwise we
302 	 * could reach 0 spuriously during the first call.
303 	 */
304 	fixjobc(p, pgrp, 1);
305 	fixjobc(p, p->p_pgrp, 0);
306 
307 	PROC_LOCK(p);
308 	LIST_REMOVE(p, p_pglist);
309 	savegrp = p->p_pgrp;
310 	p->p_pgrp = pgrp;
311 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
312 	PROC_UNLOCK(p);
313 	if (LIST_EMPTY(&savegrp->pg_members))
314 		pgdelete(savegrp);
315 	return (0);
316 }
317 
318 /*
319  * remove process from process group
320  */
321 int
322 leavepgrp(p)
323 	register struct proc *p;
324 {
325 	struct pgrp *savegrp;
326 
327 	PROC_LOCK(p);
328 	LIST_REMOVE(p, p_pglist);
329 	savegrp = p->p_pgrp;
330 	p->p_pgrp = NULL;
331 	PROC_UNLOCK(p);
332 	if (LIST_EMPTY(&savegrp->pg_members))
333 		pgdelete(savegrp);
334 	return (0);
335 }
336 
337 /*
338  * delete a process group
339  */
340 static void
341 pgdelete(pgrp)
342 	register struct pgrp *pgrp;
343 {
344 
345 	/*
346 	 * Reset any sigio structures pointing to us as a result of
347 	 * F_SETOWN with our pgid.
348 	 */
349 	funsetownlst(&pgrp->pg_sigiolst);
350 
351 	if (pgrp->pg_session->s_ttyp != NULL &&
352 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
353 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
354 	LIST_REMOVE(pgrp, pg_hash);
355 	if (--pgrp->pg_session->s_count == 0)
356 		FREE(pgrp->pg_session, M_SESSION);
357 	FREE(pgrp, M_PGRP);
358 }
359 
360 /*
361  * Adjust pgrp jobc counters when specified process changes process group.
362  * We count the number of processes in each process group that "qualify"
363  * the group for terminal job control (those with a parent in a different
364  * process group of the same session).  If that count reaches zero, the
365  * process group becomes orphaned.  Check both the specified process'
366  * process group and that of its children.
367  * entering == 0 => p is leaving specified group.
368  * entering == 1 => p is entering specified group.
369  */
370 void
371 fixjobc(p, pgrp, entering)
372 	register struct proc *p;
373 	register struct pgrp *pgrp;
374 	int entering;
375 {
376 	register struct pgrp *hispgrp;
377 	register struct session *mysession = pgrp->pg_session;
378 
379 	/*
380 	 * Check p's parent to see whether p qualifies its own process
381 	 * group; if so, adjust count for p's process group.
382 	 */
383 	sx_slock(&proctree_lock);
384 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
385 	    hispgrp->pg_session == mysession) {
386 		if (entering)
387 			pgrp->pg_jobc++;
388 		else if (--pgrp->pg_jobc == 0)
389 			orphanpg(pgrp);
390 	}
391 
392 	/*
393 	 * Check this process' children to see whether they qualify
394 	 * their process groups; if so, adjust counts for children's
395 	 * process groups.
396 	 */
397 	LIST_FOREACH(p, &p->p_children, p_sibling)
398 		if ((hispgrp = p->p_pgrp) != pgrp &&
399 		    hispgrp->pg_session == mysession &&
400 		    p->p_stat != SZOMB) {
401 			if (entering)
402 				hispgrp->pg_jobc++;
403 			else if (--hispgrp->pg_jobc == 0)
404 				orphanpg(hispgrp);
405 		}
406 	sx_sunlock(&proctree_lock);
407 }
408 
409 /*
410  * A process group has become orphaned;
411  * if there are any stopped processes in the group,
412  * hang-up all process in that group.
413  */
414 static void
415 orphanpg(pg)
416 	struct pgrp *pg;
417 {
418 	register struct proc *p;
419 
420 	mtx_lock_spin(&sched_lock);
421 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
422 		if (p->p_stat == SSTOP) {
423 			mtx_unlock_spin(&sched_lock);
424 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
425 				PROC_LOCK(p);
426 				psignal(p, SIGHUP);
427 				psignal(p, SIGCONT);
428 				PROC_UNLOCK(p);
429 			}
430 			return;
431 		}
432 	}
433 	mtx_unlock_spin(&sched_lock);
434 }
435 
436 #include "opt_ddb.h"
437 #ifdef DDB
438 #include <ddb/ddb.h>
439 
440 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
441 {
442 	register struct pgrp *pgrp;
443 	register struct proc *p;
444 	register int i;
445 
446 	for (i = 0; i <= pgrphash; i++) {
447 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
448 			printf("\tindx %d\n", i);
449 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
450 				printf(
451 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
452 				    (void *)pgrp, (long)pgrp->pg_id,
453 				    (void *)pgrp->pg_session,
454 				    pgrp->pg_session->s_count,
455 				    (void *)LIST_FIRST(&pgrp->pg_members));
456 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
457 					printf("\t\tpid %ld addr %p pgrp %p\n",
458 					    (long)p->p_pid, (void *)p,
459 					    (void *)p->p_pgrp);
460 				}
461 			}
462 		}
463 	}
464 }
465 #endif /* DDB */
466 
467 /*
468  * Fill in an kinfo_proc structure for the specified process.
469  */
470 void
471 fill_kinfo_proc(p, kp)
472 	struct proc *p;
473 	struct kinfo_proc *kp;
474 {
475 	struct thread *td;
476 	struct tty *tp;
477 	struct session *sp;
478 
479 	bzero(kp, sizeof(*kp));
480 
481 	kp->ki_structsize = sizeof(*kp);
482 	kp->ki_paddr = p;
483 	PROC_LOCK(p);
484 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
485 	kp->ki_args = p->p_args;
486 	kp->ki_tracep = p->p_tracep;
487 	kp->ki_textvp = p->p_textvp;
488 	kp->ki_fd = p->p_fd;
489 	kp->ki_vmspace = p->p_vmspace;
490 	if (p->p_ucred) {
491 		kp->ki_uid = p->p_ucred->cr_uid;
492 		kp->ki_ruid = p->p_ucred->cr_ruid;
493 		kp->ki_svuid = p->p_ucred->cr_svuid;
494 		/* XXX bde doesn't like KI_NGROUPS */
495 		kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS);
496 		bcopy(p->p_ucred->cr_groups, kp->ki_groups,
497 		    kp->ki_ngroups * sizeof(gid_t));
498 		kp->ki_rgid = p->p_ucred->cr_rgid;
499 		kp->ki_svgid = p->p_ucred->cr_svgid;
500 	}
501 	if (p->p_procsig) {
502 		kp->ki_sigignore = p->p_procsig->ps_sigignore;
503 		kp->ki_sigcatch = p->p_procsig->ps_sigcatch;
504 	}
505 	mtx_lock_spin(&sched_lock);
506 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
507 		struct vmspace *vm = p->p_vmspace;
508 
509 		kp->ki_size = vm->vm_map.size;
510 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
511 		if (p->p_sflag & PS_INMEM)
512 			kp->ki_rssize += UAREA_PAGES;
513 		FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */
514 			kp->ki_rssize += KSTACK_PAGES;
515 		kp->ki_swrss = vm->vm_swrss;
516 		kp->ki_tsize = vm->vm_tsize;
517 		kp->ki_dsize = vm->vm_dsize;
518 		kp->ki_ssize = vm->vm_ssize;
519 	}
520 	if ((p->p_sflag & PS_INMEM) && p->p_stats) {
521 		kp->ki_start = p->p_stats->p_start;
522 		kp->ki_rusage = p->p_stats->p_ru;
523 		kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec +
524 		    p->p_stats->p_cru.ru_stime.tv_sec;
525 		kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec +
526 		    p->p_stats->p_cru.ru_stime.tv_usec;
527 	}
528 	if (p->p_thread.td_wmesg != NULL)
529 		strncpy(kp->ki_wmesg, p->p_thread.td_wmesg, sizeof(kp->ki_wmesg) - 1);
530 	if (p->p_stat == SMTX) {
531 		kp->ki_kiflag |= KI_MTXBLOCK;
532 		strncpy(kp->ki_mtxname, p->p_thread.td_mtxname,
533 		    sizeof(kp->ki_mtxname) - 1);
534 	}
535 	kp->ki_stat = p->p_stat;
536 	kp->ki_sflag = p->p_sflag;
537 	kp->ki_swtime = p->p_swtime;
538 	kp->ki_traceflag = p->p_traceflag;
539 	kp->ki_pid = p->p_pid;
540 	/* vvv XXXKSE */
541 	kp->ki_runtime = p->p_runtime;
542 	kp->ki_pctcpu = p->p_kse.ke_pctcpu;
543 	kp->ki_estcpu = p->p_ksegrp.kg_estcpu;
544 	kp->ki_slptime = p->p_ksegrp.kg_slptime;
545 	kp->ki_wchan = p->p_thread.td_wchan;
546 	kp->ki_pri = p->p_ksegrp.kg_pri;
547 	kp->ki_nice = p->p_ksegrp.kg_nice;
548 	kp->ki_rqindex = p->p_kse.ke_rqindex;
549 	kp->ki_oncpu = p->p_kse.ke_oncpu;
550 	kp->ki_lastcpu = p->p_thread.td_lastcpu;
551 	kp->ki_tdflags = p->p_thread.td_flags;
552 	kp->ki_pcb = p->p_thread.td_pcb;
553 	kp->ki_kstack = (void *)p->p_thread.td_kstack;
554 	/* ^^^ XXXKSE */
555 	mtx_unlock_spin(&sched_lock);
556 	sp = NULL;
557 	if (p->p_pgrp) {
558 		kp->ki_pgid = p->p_pgrp->pg_id;
559 		kp->ki_jobc = p->p_pgrp->pg_jobc;
560 		sp = p->p_pgrp->pg_session;
561 
562 		if (sp != NULL) {
563 			kp->ki_sid = sp->s_sid;
564 			strncpy(kp->ki_login, sp->s_login,
565 			    sizeof(kp->ki_login) - 1);
566 			if (sp->s_ttyvp)
567 				kp->ki_kiflag = KI_CTTY;
568 			if (SESS_LEADER(p))
569 				kp->ki_kiflag |= KI_SLEADER;
570 		}
571 	}
572 	if ((p->p_flag & P_CONTROLT) && sp && ((tp = sp->s_ttyp) != NULL)) {
573 		kp->ki_tdev = dev2udev(tp->t_dev);
574 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
575 		if (tp->t_session)
576 			kp->ki_tsid = tp->t_session->s_sid;
577 	} else
578 		kp->ki_tdev = NOUDEV;
579 	if (p->p_comm[0] != '\0') {
580 		strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1);
581 		strncpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm) - 1);
582 	}
583 	kp->ki_siglist = p->p_siglist;
584 	kp->ki_sigmask = p->p_sigmask;
585 	kp->ki_xstat = p->p_xstat;
586 	kp->ki_acflag = p->p_acflag;
587 	kp->ki_flag = p->p_flag;
588 	/* If jailed(p->p_ucred), emulate the old P_JAILED flag. */
589 	if (jailed(p->p_ucred))
590 		kp->ki_flag |= P_JAILED;
591 	kp->ki_lock = p->p_lock;
592 	if (p->p_pptr)
593 		kp->ki_ppid = p->p_pptr->p_pid;
594 	PROC_UNLOCK(p);
595 }
596 
597 /*
598  * Locate a zombie process by number
599  */
600 struct proc *
601 zpfind(pid_t pid)
602 {
603 	struct proc *p;
604 
605 	sx_slock(&allproc_lock);
606 	LIST_FOREACH(p, &zombproc, p_list)
607 		if (p->p_pid == pid) {
608 			PROC_LOCK(p);
609 			break;
610 		}
611 	sx_sunlock(&allproc_lock);
612 	return (p);
613 }
614 
615 
616 static int
617 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
618 {
619 	struct kinfo_proc kinfo_proc;
620 	int error;
621 	struct proc *np;
622 	pid_t pid = p->p_pid;
623 
624 	fill_kinfo_proc(p, &kinfo_proc);
625 	error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc));
626 	if (error)
627 		return (error);
628 	if (doingzomb)
629 		np = zpfind(pid);
630 	else {
631 		if (pid == 0)
632 			return (0);
633 		np = pfind(pid);
634 	}
635 	if (np == NULL)
636 		return EAGAIN;
637 	if (np != p) {
638 		PROC_UNLOCK(np);
639 		return EAGAIN;
640 	}
641 	PROC_UNLOCK(np);
642 	return (0);
643 }
644 
645 static int
646 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
647 {
648 	int *name = (int*) arg1;
649 	u_int namelen = arg2;
650 	struct proc *p;
651 	int doingzomb;
652 	int error = 0;
653 
654 	if (oidp->oid_number == KERN_PROC_PID) {
655 		if (namelen != 1)
656 			return (EINVAL);
657 		p = pfind((pid_t)name[0]);
658 		if (!p)
659 			return (0);
660 		if (p_cansee(curproc, p)) {
661 			PROC_UNLOCK(p);
662 			return (0);
663 		}
664 		PROC_UNLOCK(p);
665 		error = sysctl_out_proc(p, req, 0);
666 		return (error);
667 	}
668 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
669 		;
670 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
671 		;
672 	else
673 		return (EINVAL);
674 
675 	if (!req->oldptr) {
676 		/* overestimate by 5 procs */
677 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
678 		if (error)
679 			return (error);
680 	}
681 	sx_slock(&allproc_lock);
682 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
683 		if (!doingzomb)
684 			p = LIST_FIRST(&allproc);
685 		else
686 			p = LIST_FIRST(&zombproc);
687 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
688 			/*
689 			 * Show a user only appropriate processes.
690 			 */
691 			if (p_cansee(curproc, p))
692 				continue;
693 			/*
694 			 * Skip embryonic processes.
695 			 */
696 			if (p->p_stat == SIDL)
697 				continue;
698 			/*
699 			 * TODO - make more efficient (see notes below).
700 			 * do by session.
701 			 */
702 			switch (oidp->oid_number) {
703 
704 			case KERN_PROC_PGRP:
705 				/* could do this by traversing pgrp */
706 				if (p->p_pgrp == NULL ||
707 				    p->p_pgrp->pg_id != (pid_t)name[0])
708 					continue;
709 				break;
710 
711 			case KERN_PROC_TTY:
712 				if ((p->p_flag & P_CONTROLT) == 0 ||
713 				    p->p_session == NULL ||
714 				    p->p_session->s_ttyp == NULL ||
715 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
716 					(udev_t)name[0])
717 					continue;
718 				break;
719 
720 			case KERN_PROC_UID:
721 				if (p->p_ucred == NULL ||
722 				    p->p_ucred->cr_uid != (uid_t)name[0])
723 					continue;
724 				break;
725 
726 			case KERN_PROC_RUID:
727 				if (p->p_ucred == NULL ||
728 				    p->p_ucred->cr_ruid != (uid_t)name[0])
729 					continue;
730 				break;
731 			}
732 
733 			if (p_cansee(curproc, p))
734 				continue;
735 
736 			error = sysctl_out_proc(p, req, doingzomb);
737 			if (error) {
738 				sx_sunlock(&allproc_lock);
739 				return (error);
740 			}
741 		}
742 	}
743 	sx_sunlock(&allproc_lock);
744 	return (0);
745 }
746 
747 /*
748  * This sysctl allows a process to retrieve the argument list or process
749  * title for another process without groping around in the address space
750  * of the other process.  It also allow a process to set its own "process
751  * title to a string of its own choice.
752  */
753 static int
754 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
755 {
756 	int *name = (int*) arg1;
757 	u_int namelen = arg2;
758 	struct proc *p;
759 	struct pargs *pa;
760 	int error = 0;
761 
762 	if (namelen != 1)
763 		return (EINVAL);
764 
765 	p = pfind((pid_t)name[0]);
766 	if (!p)
767 		return (0);
768 
769 	if ((!ps_argsopen) && p_cansee(curproc, p)) {
770 		PROC_UNLOCK(p);
771 		return (0);
772 	}
773 	PROC_UNLOCK(p);
774 
775 	if (req->newptr && curproc != p)
776 		return (EPERM);
777 
778 	if (req->oldptr && p->p_args != NULL)
779 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
780 	if (req->newptr == NULL)
781 		return (error);
782 
783 	PROC_LOCK(p);
784 	pa = p->p_args;
785 	p->p_args = NULL;
786 	PROC_UNLOCK(p);
787 	if (pa != NULL && --pa->ar_ref == 0)
788 		FREE(pa, M_PARGS);
789 
790 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
791 		return (error);
792 
793 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
794 	    M_PARGS, M_WAITOK);
795 	pa->ar_ref = 1;
796 	pa->ar_length = req->newlen;
797 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
798 	if (!error) {
799 		PROC_LOCK(p);
800 		p->p_args = pa;
801 		PROC_UNLOCK(p);
802 	} else
803 		FREE(pa, M_PARGS);
804 	return (error);
805 }
806 
807 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
808 
809 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
810 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
811 
812 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
813 	sysctl_kern_proc, "Process table");
814 
815 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
816 	sysctl_kern_proc, "Process table");
817 
818 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
819 	sysctl_kern_proc, "Process table");
820 
821 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
822 	sysctl_kern_proc, "Process table");
823 
824 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
825 	sysctl_kern_proc, "Process table");
826 
827 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
828 	sysctl_kern_proc_args, "Process argument list");
829