xref: /freebsd/sys/kern/kern_proc.c (revision a220d00e74dd245b4fca59c5eca0c53963686325)
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 = 1;
187 
188 	sx_assert(&proctree_lock, SX_LOCKED);
189 	for (; p != curproc; p = p->p_pptr)
190 		if (p->p_pid == 0) {
191 			rval = 0;
192 			break;
193 		}
194 	return (rval);
195 }
196 
197 /*
198  * Locate a process by number
199  */
200 struct proc *
201 pfind(pid)
202 	register pid_t pid;
203 {
204 	register struct proc *p;
205 
206 	sx_slock(&allproc_lock);
207 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
208 		if (p->p_pid == pid) {
209 			PROC_LOCK(p);
210 			break;
211 		}
212 	sx_sunlock(&allproc_lock);
213 	return (p);
214 }
215 
216 /*
217  * Locate a process group by number
218  */
219 struct pgrp *
220 pgfind(pgid)
221 	register pid_t pgid;
222 {
223 	register struct pgrp *pgrp;
224 
225 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
226 		if (pgrp->pg_id == pgid)
227 			return (pgrp);
228 	return (NULL);
229 }
230 
231 /*
232  * Move p to a new or existing process group (and session)
233  */
234 int
235 enterpgrp(p, pgid, mksess)
236 	register struct proc *p;
237 	pid_t pgid;
238 	int mksess;
239 {
240 	register struct pgrp *pgrp = pgfind(pgid);
241 	struct pgrp *savegrp;
242 
243 	KASSERT(pgrp == NULL || !mksess,
244 	    ("enterpgrp: setsid into non-empty pgrp"));
245 	KASSERT(!SESS_LEADER(p),
246 	    ("enterpgrp: session leader attempted setpgrp"));
247 
248 	if (pgrp == NULL) {
249 		pid_t savepid = p->p_pid;
250 		struct proc *np;
251 		/*
252 		 * new process group
253 		 */
254 		KASSERT(p->p_pid == pgid,
255 		    ("enterpgrp: new pgrp and pid != pgid"));
256 		if ((np = pfind(savepid)) == NULL || np != p) {
257 			if (np != NULL)
258 				PROC_UNLOCK(np);
259 			return (ESRCH);
260 		}
261 		PROC_UNLOCK(np);
262 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
263 		    M_WAITOK);
264 		if (mksess) {
265 			register struct session *sess;
266 
267 			/*
268 			 * new session
269 			 */
270 			MALLOC(sess, struct session *, sizeof(struct session),
271 			    M_SESSION, M_WAITOK);
272 			sess->s_leader = p;
273 			sess->s_sid = p->p_pid;
274 			sess->s_count = 1;
275 			sess->s_ttyvp = NULL;
276 			sess->s_ttyp = NULL;
277 			bcopy(p->p_session->s_login, sess->s_login,
278 			    sizeof(sess->s_login));
279 			PROC_LOCK(p);
280 			p->p_flag &= ~P_CONTROLT;
281 			PROC_UNLOCK(p);
282 			pgrp->pg_session = sess;
283 			KASSERT(p == curproc,
284 			    ("enterpgrp: mksession and p != curproc"));
285 		} else {
286 			pgrp->pg_session = p->p_session;
287 			pgrp->pg_session->s_count++;
288 		}
289 		pgrp->pg_id = pgid;
290 		LIST_INIT(&pgrp->pg_members);
291 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
292 		pgrp->pg_jobc = 0;
293 		SLIST_INIT(&pgrp->pg_sigiolst);
294 	} else if (pgrp == p->p_pgrp)
295 		return (0);
296 
297 	/*
298 	 * Adjust eligibility of affected pgrps to participate in job control.
299 	 * Increment eligibility counts before decrementing, otherwise we
300 	 * could reach 0 spuriously during the first call.
301 	 */
302 	fixjobc(p, pgrp, 1);
303 	fixjobc(p, p->p_pgrp, 0);
304 
305 	PROC_LOCK(p);
306 	LIST_REMOVE(p, p_pglist);
307 	savegrp = p->p_pgrp;
308 	p->p_pgrp = pgrp;
309 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
310 	PROC_UNLOCK(p);
311 	if (LIST_EMPTY(&savegrp->pg_members))
312 		pgdelete(savegrp);
313 	return (0);
314 }
315 
316 /*
317  * remove process from process group
318  */
319 int
320 leavepgrp(p)
321 	register struct proc *p;
322 {
323 	struct pgrp *savegrp;
324 
325 	PROC_LOCK(p);
326 	LIST_REMOVE(p, p_pglist);
327 	savegrp = p->p_pgrp;
328 	p->p_pgrp = NULL;
329 	PROC_UNLOCK(p);
330 	if (LIST_EMPTY(&savegrp->pg_members))
331 		pgdelete(savegrp);
332 	return (0);
333 }
334 
335 /*
336  * delete a process group
337  */
338 static void
339 pgdelete(pgrp)
340 	register struct pgrp *pgrp;
341 {
342 
343 	/*
344 	 * Reset any sigio structures pointing to us as a result of
345 	 * F_SETOWN with our pgid.
346 	 */
347 	funsetownlst(&pgrp->pg_sigiolst);
348 
349 	if (pgrp->pg_session->s_ttyp != NULL &&
350 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
351 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
352 	LIST_REMOVE(pgrp, pg_hash);
353 	if (--pgrp->pg_session->s_count == 0)
354 		FREE(pgrp->pg_session, M_SESSION);
355 	FREE(pgrp, M_PGRP);
356 }
357 
358 /*
359  * Adjust pgrp jobc counters when specified process changes process group.
360  * We count the number of processes in each process group that "qualify"
361  * the group for terminal job control (those with a parent in a different
362  * process group of the same session).  If that count reaches zero, the
363  * process group becomes orphaned.  Check both the specified process'
364  * process group and that of its children.
365  * entering == 0 => p is leaving specified group.
366  * entering == 1 => p is entering specified group.
367  */
368 void
369 fixjobc(p, pgrp, entering)
370 	register struct proc *p;
371 	register struct pgrp *pgrp;
372 	int entering;
373 {
374 	register struct pgrp *hispgrp;
375 	register struct session *mysession = pgrp->pg_session;
376 
377 	/*
378 	 * Check p's parent to see whether p qualifies its own process
379 	 * group; if so, adjust count for p's process group.
380 	 */
381 	sx_slock(&proctree_lock);
382 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
383 	    hispgrp->pg_session == mysession) {
384 		if (entering)
385 			pgrp->pg_jobc++;
386 		else if (--pgrp->pg_jobc == 0)
387 			orphanpg(pgrp);
388 	}
389 
390 	/*
391 	 * Check this process' children to see whether they qualify
392 	 * their process groups; if so, adjust counts for children's
393 	 * process groups.
394 	 */
395 	LIST_FOREACH(p, &p->p_children, p_sibling)
396 		if ((hispgrp = p->p_pgrp) != pgrp &&
397 		    hispgrp->pg_session == mysession &&
398 		    p->p_stat != SZOMB) {
399 			if (entering)
400 				hispgrp->pg_jobc++;
401 			else if (--hispgrp->pg_jobc == 0)
402 				orphanpg(hispgrp);
403 		}
404 	sx_sunlock(&proctree_lock);
405 }
406 
407 /*
408  * A process group has become orphaned;
409  * if there are any stopped processes in the group,
410  * hang-up all process in that group.
411  */
412 static void
413 orphanpg(pg)
414 	struct pgrp *pg;
415 {
416 	register struct proc *p;
417 
418 	mtx_lock_spin(&sched_lock);
419 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
420 		if (p->p_stat == SSTOP) {
421 			mtx_unlock_spin(&sched_lock);
422 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
423 				PROC_LOCK(p);
424 				psignal(p, SIGHUP);
425 				psignal(p, SIGCONT);
426 				PROC_UNLOCK(p);
427 			}
428 			return;
429 		}
430 	}
431 	mtx_unlock_spin(&sched_lock);
432 }
433 
434 #include "opt_ddb.h"
435 #ifdef DDB
436 #include <ddb/ddb.h>
437 
438 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
439 {
440 	register struct pgrp *pgrp;
441 	register struct proc *p;
442 	register int i;
443 
444 	for (i = 0; i <= pgrphash; i++) {
445 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
446 			printf("\tindx %d\n", i);
447 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
448 				printf(
449 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
450 				    (void *)pgrp, (long)pgrp->pg_id,
451 				    (void *)pgrp->pg_session,
452 				    pgrp->pg_session->s_count,
453 				    (void *)LIST_FIRST(&pgrp->pg_members));
454 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
455 					printf("\t\tpid %ld addr %p pgrp %p\n",
456 					    (long)p->p_pid, (void *)p,
457 					    (void *)p->p_pgrp);
458 				}
459 			}
460 		}
461 	}
462 }
463 #endif /* DDB */
464 
465 /*
466  * Fill in an kinfo_proc structure for the specified process.
467  */
468 void
469 fill_kinfo_proc(p, kp)
470 	struct proc *p;
471 	struct kinfo_proc *kp;
472 {
473 	struct thread *td;
474 	struct tty *tp;
475 	struct session *sp;
476 
477 	bzero(kp, sizeof(*kp));
478 
479 	kp->ki_structsize = sizeof(*kp);
480 	kp->ki_paddr = p;
481 	PROC_LOCK(p);
482 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
483 	kp->ki_args = p->p_args;
484 	kp->ki_tracep = p->p_tracep;
485 	kp->ki_textvp = p->p_textvp;
486 	kp->ki_fd = p->p_fd;
487 	kp->ki_vmspace = p->p_vmspace;
488 	if (p->p_ucred) {
489 		kp->ki_uid = p->p_ucred->cr_uid;
490 		kp->ki_ruid = p->p_ucred->cr_ruid;
491 		kp->ki_svuid = p->p_ucred->cr_svuid;
492 		/* XXX bde doesn't like KI_NGROUPS */
493 		kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS);
494 		bcopy(p->p_ucred->cr_groups, kp->ki_groups,
495 		    kp->ki_ngroups * sizeof(gid_t));
496 		kp->ki_rgid = p->p_ucred->cr_rgid;
497 		kp->ki_svgid = p->p_ucred->cr_svgid;
498 	}
499 	if (p->p_procsig) {
500 		kp->ki_sigignore = p->p_procsig->ps_sigignore;
501 		kp->ki_sigcatch = p->p_procsig->ps_sigcatch;
502 	}
503 	mtx_lock_spin(&sched_lock);
504 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
505 		struct vmspace *vm = p->p_vmspace;
506 
507 		kp->ki_size = vm->vm_map.size;
508 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
509 		if (p->p_sflag & PS_INMEM)
510 			kp->ki_rssize += UAREA_PAGES;
511 		FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */
512 			kp->ki_rssize += KSTACK_PAGES;
513 		kp->ki_swrss = vm->vm_swrss;
514 		kp->ki_tsize = vm->vm_tsize;
515 		kp->ki_dsize = vm->vm_dsize;
516 		kp->ki_ssize = vm->vm_ssize;
517 	}
518 	if ((p->p_sflag & PS_INMEM) && p->p_stats) {
519 		kp->ki_start = p->p_stats->p_start;
520 		kp->ki_rusage = p->p_stats->p_ru;
521 		kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec +
522 		    p->p_stats->p_cru.ru_stime.tv_sec;
523 		kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec +
524 		    p->p_stats->p_cru.ru_stime.tv_usec;
525 	}
526 	if (p->p_thread.td_wmesg != NULL)
527 		strncpy(kp->ki_wmesg, p->p_thread.td_wmesg, sizeof(kp->ki_wmesg) - 1);
528 	if (p->p_stat == SMTX) {
529 		kp->ki_kiflag |= KI_MTXBLOCK;
530 		strncpy(kp->ki_mtxname, p->p_thread.td_mtxname,
531 		    sizeof(kp->ki_mtxname) - 1);
532 	}
533 	kp->ki_stat = p->p_stat;
534 	kp->ki_sflag = p->p_sflag;
535 	kp->ki_swtime = p->p_swtime;
536 	kp->ki_traceflag = p->p_traceflag;
537 	kp->ki_pid = p->p_pid;
538 	/* vvv XXXKSE */
539 	kp->ki_runtime = p->p_runtime;
540 	kp->ki_pctcpu = p->p_kse.ke_pctcpu;
541 	kp->ki_estcpu = p->p_ksegrp.kg_estcpu;
542 	kp->ki_slptime = p->p_ksegrp.kg_slptime;
543 	kp->ki_wchan = p->p_thread.td_wchan;
544 	kp->ki_pri = p->p_ksegrp.kg_pri;
545 	kp->ki_nice = p->p_ksegrp.kg_nice;
546 	kp->ki_rqindex = p->p_kse.ke_rqindex;
547 	kp->ki_oncpu = p->p_kse.ke_oncpu;
548 	kp->ki_lastcpu = p->p_thread.td_lastcpu;
549 	kp->ki_tdflags = p->p_thread.td_flags;
550 	kp->ki_pcb = p->p_thread.td_pcb;
551 	kp->ki_kstack = (void *)p->p_thread.td_kstack;
552 	/* ^^^ XXXKSE */
553 	mtx_unlock_spin(&sched_lock);
554 	sp = NULL;
555 	if (p->p_pgrp) {
556 		kp->ki_pgid = p->p_pgrp->pg_id;
557 		kp->ki_jobc = p->p_pgrp->pg_jobc;
558 		sp = p->p_pgrp->pg_session;
559 
560 		if (sp != NULL) {
561 			kp->ki_sid = sp->s_sid;
562 			strncpy(kp->ki_login, sp->s_login,
563 			    sizeof(kp->ki_login) - 1);
564 			if (sp->s_ttyvp)
565 				kp->ki_kiflag = KI_CTTY;
566 			if (SESS_LEADER(p))
567 				kp->ki_kiflag |= KI_SLEADER;
568 		}
569 	}
570 	if ((p->p_flag & P_CONTROLT) && sp && ((tp = sp->s_ttyp) != NULL)) {
571 		kp->ki_tdev = dev2udev(tp->t_dev);
572 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
573 		if (tp->t_session)
574 			kp->ki_tsid = tp->t_session->s_sid;
575 	} else
576 		kp->ki_tdev = NOUDEV;
577 	if (p->p_comm[0] != '\0') {
578 		strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1);
579 		strncpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm) - 1);
580 	}
581 	kp->ki_siglist = p->p_siglist;
582 	kp->ki_sigmask = p->p_sigmask;
583 	kp->ki_xstat = p->p_xstat;
584 	kp->ki_acflag = p->p_acflag;
585 	kp->ki_flag = p->p_flag;
586 	/* If jailed(p->p_ucred), emulate the old P_JAILED flag. */
587 	if (jailed(p->p_ucred))
588 		kp->ki_flag |= P_JAILED;
589 	kp->ki_lock = p->p_lock;
590 	if (p->p_pptr)
591 		kp->ki_ppid = p->p_pptr->p_pid;
592 	PROC_UNLOCK(p);
593 }
594 
595 /*
596  * Locate a zombie process by number
597  */
598 struct proc *
599 zpfind(pid_t pid)
600 {
601 	struct proc *p;
602 
603 	sx_slock(&allproc_lock);
604 	LIST_FOREACH(p, &zombproc, p_list)
605 		if (p->p_pid == pid) {
606 			PROC_LOCK(p);
607 			break;
608 		}
609 	sx_sunlock(&allproc_lock);
610 	return (p);
611 }
612 
613 
614 static int
615 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
616 {
617 	struct kinfo_proc kinfo_proc;
618 	int error;
619 	struct proc *np;
620 	pid_t pid = p->p_pid;
621 
622 	fill_kinfo_proc(p, &kinfo_proc);
623 	error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc));
624 	if (error)
625 		return (error);
626 	if (doingzomb)
627 		np = zpfind(pid);
628 	else {
629 		if (pid == 0)
630 			return (0);
631 		np = pfind(pid);
632 	}
633 	if (np == NULL)
634 		return EAGAIN;
635 	if (np != p) {
636 		PROC_UNLOCK(np);
637 		return EAGAIN;
638 	}
639 	PROC_UNLOCK(np);
640 	return (0);
641 }
642 
643 static int
644 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
645 {
646 	int *name = (int*) arg1;
647 	u_int namelen = arg2;
648 	struct proc *p;
649 	int doingzomb;
650 	int error = 0;
651 
652 	if (oidp->oid_number == KERN_PROC_PID) {
653 		if (namelen != 1)
654 			return (EINVAL);
655 		p = pfind((pid_t)name[0]);
656 		if (!p)
657 			return (0);
658 		if (p_cansee(curproc, p)) {
659 			PROC_UNLOCK(p);
660 			return (0);
661 		}
662 		PROC_UNLOCK(p);
663 		error = sysctl_out_proc(p, req, 0);
664 		return (error);
665 	}
666 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
667 		;
668 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
669 		;
670 	else
671 		return (EINVAL);
672 
673 	if (!req->oldptr) {
674 		/* overestimate by 5 procs */
675 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
676 		if (error)
677 			return (error);
678 	}
679 	sx_slock(&allproc_lock);
680 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
681 		if (!doingzomb)
682 			p = LIST_FIRST(&allproc);
683 		else
684 			p = LIST_FIRST(&zombproc);
685 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
686 			/*
687 			 * Show a user only appropriate processes.
688 			 */
689 			if (p_cansee(curproc, p))
690 				continue;
691 			/*
692 			 * Skip embryonic processes.
693 			 */
694 			if (p->p_stat == SIDL)
695 				continue;
696 			/*
697 			 * TODO - make more efficient (see notes below).
698 			 * do by session.
699 			 */
700 			switch (oidp->oid_number) {
701 
702 			case KERN_PROC_PGRP:
703 				/* could do this by traversing pgrp */
704 				if (p->p_pgrp == NULL ||
705 				    p->p_pgrp->pg_id != (pid_t)name[0])
706 					continue;
707 				break;
708 
709 			case KERN_PROC_TTY:
710 				if ((p->p_flag & P_CONTROLT) == 0 ||
711 				    p->p_session == NULL ||
712 				    p->p_session->s_ttyp == NULL ||
713 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
714 					(udev_t)name[0])
715 					continue;
716 				break;
717 
718 			case KERN_PROC_UID:
719 				if (p->p_ucred == NULL ||
720 				    p->p_ucred->cr_uid != (uid_t)name[0])
721 					continue;
722 				break;
723 
724 			case KERN_PROC_RUID:
725 				if (p->p_ucred == NULL ||
726 				    p->p_ucred->cr_ruid != (uid_t)name[0])
727 					continue;
728 				break;
729 			}
730 
731 			if (p_cansee(curproc, p))
732 				continue;
733 
734 			error = sysctl_out_proc(p, req, doingzomb);
735 			if (error) {
736 				sx_sunlock(&allproc_lock);
737 				return (error);
738 			}
739 		}
740 	}
741 	sx_sunlock(&allproc_lock);
742 	return (0);
743 }
744 
745 /*
746  * This sysctl allows a process to retrieve the argument list or process
747  * title for another process without groping around in the address space
748  * of the other process.  It also allow a process to set its own "process
749  * title to a string of its own choice.
750  */
751 static int
752 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
753 {
754 	int *name = (int*) arg1;
755 	u_int namelen = arg2;
756 	struct proc *p;
757 	struct pargs *pa;
758 	int error = 0;
759 
760 	if (namelen != 1)
761 		return (EINVAL);
762 
763 	p = pfind((pid_t)name[0]);
764 	if (!p)
765 		return (0);
766 
767 	if ((!ps_argsopen) && p_cansee(curproc, p)) {
768 		PROC_UNLOCK(p);
769 		return (0);
770 	}
771 	PROC_UNLOCK(p);
772 
773 	if (req->newptr && curproc != p)
774 		return (EPERM);
775 
776 	if (req->oldptr && p->p_args != NULL)
777 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
778 	if (req->newptr == NULL)
779 		return (error);
780 
781 	PROC_LOCK(p);
782 	pa = p->p_args;
783 	p->p_args = NULL;
784 	PROC_UNLOCK(p);
785 	if (pa != NULL && --pa->ar_ref == 0)
786 		FREE(pa, M_PARGS);
787 
788 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
789 		return (error);
790 
791 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
792 	    M_PARGS, M_WAITOK);
793 	pa->ar_ref = 1;
794 	pa->ar_length = req->newlen;
795 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
796 	if (!error) {
797 		PROC_LOCK(p);
798 		p->p_args = pa;
799 		PROC_UNLOCK(p);
800 	} else
801 		FREE(pa, M_PARGS);
802 	return (error);
803 }
804 
805 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
806 
807 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
808 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
809 
810 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
811 	sysctl_kern_proc, "Process table");
812 
813 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
814 	sysctl_kern_proc, "Process table");
815 
816 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
817 	sysctl_kern_proc, "Process table");
818 
819 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
820 	sysctl_kern_proc, "Process table");
821 
822 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
823 	sysctl_kern_proc, "Process table");
824 
825 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
826 	sysctl_kern_proc_args, "Process argument list");
827