xref: /freebsd/sys/kern/kern_proc.c (revision b5a8f767a62e0253ce02878cd6d69ea7f9574d1a)
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/sysproto.h>
45 #include <sys/sysctl.h>
46 #include <sys/filedesc.h>
47 #include <sys/tty.h>
48 #include <sys/signalvar.h>
49 #include <sys/sx.h>
50 #include <sys/user.h>
51 #include <sys/jail.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_zone.h>
57 
58 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
59 MALLOC_DEFINE(M_SESSION, "session", "session header");
60 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
61 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
62 
63 static struct proc *dopfind	__P((register pid_t));
64 
65 static void doenterpgrp	__P((struct proc *, struct pgrp *));
66 
67 static void pgdelete	__P((struct pgrp *));
68 
69 static void orphanpg	__P((struct pgrp *pg));
70 
71 /*
72  * Other process lists
73  */
74 struct pidhashhead *pidhashtbl;
75 u_long pidhash;
76 struct pgrphashhead *pgrphashtbl;
77 u_long pgrphash;
78 struct proclist allproc;
79 struct proclist zombproc;
80 struct sx allproc_lock;
81 struct sx proctree_lock;
82 struct sx pgrpsess_lock;
83 vm_zone_t proc_zone;
84 vm_zone_t ithread_zone;
85 
86 /*
87  * Initialize global process hashing structures.
88  */
89 void
90 procinit()
91 {
92 	int i, j;
93 
94 	sx_init(&allproc_lock, "allproc");
95 	sx_init(&proctree_lock, "proctree");
96 	sx_init(&pgrpsess_lock, "pgrpsess");
97 	LIST_INIT(&allproc);
98 	LIST_INIT(&zombproc);
99 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
100 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
101 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
102 	uihashinit();
103 	/*
104 	 * This should really be a compile time warning, but I do
105 	 * not know of any way to do that...
106 	 */
107 	if (sizeof(struct kinfo_proc) != KINFO_PROC_SIZE) {
108 		printf("This message will repeat for the next 20 seconds\n");
109 		for (i = 0; i < 20; i++) {
110 			printf("WARNING: size of kinfo_proc (%ld) should be %d!!!\n",
111 			    (long)sizeof(struct kinfo_proc), KINFO_PROC_SIZE);
112 			printf("The kinfo_proc structure was changed ");
113 			printf("incorrectly in <sys/user.h>\n");
114 			for (j = 0; j < 0x7ffffff; j++);
115 		}
116 
117 	}
118 }
119 
120 /*
121  * Note that we do not link to the proc's ucred here
122  * The thread is linked as if running but no KSE assigned
123  */
124 static  void
125 thread_link(struct thread *td, struct ksegrp *kg)
126 {
127 	struct proc *p = kg->kg_proc;
128 
129 	td->td_proc     = p;
130 	td->td_ksegrp   = kg;
131 	td->td_last_kse = &p->p_kse;
132 
133 	TAILQ_INSERT_HEAD(&p->p_threads, td, td_plist);
134 	TAILQ_INSERT_HEAD(&kg->kg_threads, td, td_kglist);
135 	td->td_critnest = 0;
136 	td->td_savecrit = 0;
137 	td->td_kse      = NULL;
138 }
139 
140 /*
141  * KSE is linked onto the idle queue.
142  */
143 static void
144 kse_link(struct kse *ke, struct ksegrp *kg)
145 {
146 	struct proc *p = kg->kg_proc;
147 
148 	TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist);
149 	kg->kg_kses++;
150 	TAILQ_INSERT_HEAD(&kg->kg_iq, ke, ke_kgrlist);
151 	ke->ke_proc	= p;
152 	ke->ke_ksegrp	= kg;
153 	ke->ke_thread	= NULL;
154 	ke->ke_oncpu = NOCPU;
155 }
156 
157 static void
158 ksegrp_link(struct ksegrp *kg, struct proc *p)
159 {
160 
161 	TAILQ_INIT(&kg->kg_threads);
162 	TAILQ_INIT(&kg->kg_runq);	/* links with td_runq */
163 	TAILQ_INIT(&kg->kg_slpq);	/* links with td_runq */
164 	TAILQ_INIT(&kg->kg_kseq);	/* all kses in ksegrp */
165 	TAILQ_INIT(&kg->kg_iq);		/* all kses in ksegrp */
166 	kg->kg_proc	= p;
167 /* the following counters are in the -zero- section and may not need clearing */
168 	kg->kg_runnable = 0;
169 	kg->kg_kses = 0;
170 	kg->kg_runq_kses = 0; /* XXXKSE change name */
171 /* link it in now that it's consitant */
172 	TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp);
173 }
174 
175 /*
176  * for a newly created process,
177  * link up a the structure and its initial threads etc.
178  */
179 void
180 proc_linkup(struct proc *p, struct ksegrp *kg,
181 			struct kse *ke, struct thread *td)
182 {
183 
184 	TAILQ_INIT(&p->p_ksegrps);	     /* all ksegrps in proc */
185 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
186 
187 	ksegrp_link(kg, p);
188 	kse_link(ke, kg);
189 	thread_link(td, kg);
190 	/* link them together for 1:1 */
191 	td->td_kse = ke;
192 	ke->ke_thread = td;
193 }
194 
195 /* temporary version is ultra simple while we are in 1:1 mode */
196 struct thread *
197 thread_get(struct proc *p)
198 {
199 	struct thread *td = &p->p_xxthread;
200 
201 	return (td);
202 }
203 
204 
205 /*********************
206 * STUB KSE syscalls
207 *********************/
208 
209 /* struct thread_wakeup_args { struct thread_mailbox *tmbx; }; */
210 int
211 thread_wakeup(struct thread *td, struct  thread_wakeup_args *uap)
212 {
213 
214 	return(ENOSYS);
215 }
216 
217 int
218 kse_exit(struct thread *td, struct kse_exit_args *uap)
219 {
220 
221 	return(ENOSYS);
222 }
223 
224 int
225 kse_yield(struct thread *td, struct kse_yield_args *uap)
226 {
227 
228 	return(ENOSYS);
229 }
230 
231 int kse_wakeup(struct thread *td, struct kse_wakeup_args *uap)
232 {
233 
234 	return(ENOSYS);
235 }
236 
237 
238 int
239 kse_new(struct thread *td, struct kse_new_args *uap)
240 /* struct kse_new_args {
241 	struct kse_mailbox *mbx;
242 	int	new_grp_flag;
243 }; */
244 {
245 
246 	return (ENOSYS);
247 }
248 
249 /*
250  * Is p an inferior of the current process?
251  */
252 int
253 inferior(p)
254 	register struct proc *p;
255 {
256 
257 	sx_assert(&proctree_lock, SX_LOCKED);
258 	for (; p != curproc; p = p->p_pptr)
259 		if (p->p_pid == 0)
260 			return (0);
261 	return (1);
262 }
263 
264 /*
265  * Locate a process by number
266  */
267 struct proc *
268 pfind(pid)
269 	register pid_t pid;
270 {
271 	register struct proc *p;
272 
273 	sx_slock(&allproc_lock);
274 	p = dopfind(pid);
275 	sx_sunlock(&allproc_lock);
276 	return (p);
277 }
278 
279 static struct proc *
280 dopfind(pid)
281 	register pid_t pid;
282 {
283 	register struct proc *p;
284 
285 	sx_assert(&allproc_lock, SX_LOCKED);
286 
287 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
288 		if (p->p_pid == pid) {
289 			PROC_LOCK(p);
290 			break;
291 		}
292 	return (p);
293 }
294 
295 /*
296  * Locate a process group by number.
297  * The caller must hold pgrpsess_lock.
298  */
299 struct pgrp *
300 pgfind(pgid)
301 	register pid_t pgid;
302 {
303 	register struct pgrp *pgrp;
304 
305 	PGRPSESS_LOCK_ASSERT(SX_LOCKED);
306 
307 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
308 		if (pgrp->pg_id == pgid) {
309 			PGRP_LOCK(pgrp);
310 			return (pgrp);
311 		}
312 	}
313 	return (NULL);
314 }
315 
316 /*
317  * Create a new process group.
318  * pgid must be equal to the pid of p.
319  * Begin a new session if required.
320  */
321 int
322 enterpgrp(p, pgid, pgrp, sess)
323 	register struct proc *p;
324 	pid_t pgid;
325 	struct pgrp *pgrp;
326 	struct session *sess;
327 {
328 	struct pgrp *pgrp2;
329 
330 	PGRPSESS_LOCK_ASSERT(SX_XLOCKED);
331 
332 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
333 	KASSERT(p->p_pid == pgid,
334 	    ("enterpgrp: new pgrp and pid != pgid"));
335 
336 	pgrp2 = pgfind(pgid);
337 
338 	KASSERT(pgrp2 == NULL,
339 	    ("enterpgrp: pgrp with pgid exists"));
340 	KASSERT(!SESS_LEADER(p),
341 	    ("enterpgrp: session leader attempted setpgrp"));
342 
343 	mtx_init(&pgrp->pg_mtx, "process group", MTX_DEF);
344 
345 	if (sess != NULL) {
346 		/*
347 		 * new session
348 		 */
349 		mtx_init(&sess->s_mtx, "session", MTX_DEF);
350 		PROC_LOCK(p);
351 		p->p_flag &= ~P_CONTROLT;
352 		PROC_UNLOCK(p);
353 		PGRP_LOCK(pgrp);
354 		sess->s_leader = p;
355 		sess->s_sid = p->p_pid;
356 		sess->s_count = 1;
357 		sess->s_ttyvp = NULL;
358 		sess->s_ttyp = NULL;
359 		bcopy(p->p_session->s_login, sess->s_login,
360 			    sizeof(sess->s_login));
361 		pgrp->pg_session = sess;
362 		KASSERT(p == curproc,
363 		    ("enterpgrp: mksession and p != curproc"));
364 	} else {
365 		pgrp->pg_session = p->p_session;
366 		SESS_LOCK(pgrp->pg_session);
367 		pgrp->pg_session->s_count++;
368 		SESS_UNLOCK(pgrp->pg_session);
369 		PGRP_LOCK(pgrp);
370 	}
371 	pgrp->pg_id = pgid;
372 	LIST_INIT(&pgrp->pg_members);
373 
374 	/*
375 	 * As we have an exclusive lock of pgrpsess_lock,
376 	 * this should not deadlock.
377 	 */
378 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
379 	pgrp->pg_jobc = 0;
380 	SLIST_INIT(&pgrp->pg_sigiolst);
381 	PGRP_UNLOCK(pgrp);
382 
383 	doenterpgrp(p, pgrp);
384 
385 	return (0);
386 }
387 
388 /*
389  * Move p to an existing process group
390  */
391 int
392 enterthispgrp(p, pgrp)
393 	register struct proc *p;
394 	struct pgrp *pgrp;
395 {
396 	PGRPSESS_LOCK_ASSERT(SX_XLOCKED);
397 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
398 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
399 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
400 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
401 	KASSERT(pgrp->pg_session == p->p_session,
402 		("%s: pgrp's session %p, p->p_session %p.\n",
403 		__func__,
404 		pgrp->pg_session,
405 		p->p_session));
406 	KASSERT(pgrp != p->p_pgrp,
407 		("%s: p belongs to pgrp.", __func__));
408 
409 	doenterpgrp(p, pgrp);
410 
411 	return (0);
412 }
413 
414 /*
415  * Move p to a process group
416  */
417 static void
418 doenterpgrp(p, pgrp)
419 	struct proc *p;
420 	struct pgrp *pgrp;
421 {
422 	struct pgrp *savepgrp;
423 
424 	PGRPSESS_LOCK_ASSERT(SX_XLOCKED);
425 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
426 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
427 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
428 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
429 
430 	savepgrp = p->p_pgrp;
431 
432 	/*
433 	 * Adjust eligibility of affected pgrps to participate in job control.
434 	 * Increment eligibility counts before decrementing, otherwise we
435 	 * could reach 0 spuriously during the first call.
436 	 */
437 	fixjobc(p, pgrp, 1);
438 	fixjobc(p, p->p_pgrp, 0);
439 
440 	PGRP_LOCK(pgrp);
441 	PGRP_LOCK(savepgrp);
442 	PROC_LOCK(p);
443 	LIST_REMOVE(p, p_pglist);
444 	p->p_pgrp = pgrp;
445 	PROC_UNLOCK(p);
446 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
447 	PGRP_UNLOCK(savepgrp);
448 	PGRP_UNLOCK(pgrp);
449 	if (LIST_EMPTY(&savepgrp->pg_members))
450 		pgdelete(savepgrp);
451 }
452 
453 /*
454  * remove process from process group
455  */
456 int
457 leavepgrp(p)
458 	register struct proc *p;
459 {
460 	struct pgrp *savepgrp;
461 
462 	PGRPSESS_XLOCK();
463 	savepgrp = p->p_pgrp;
464 	PGRP_LOCK(savepgrp);
465 	PROC_LOCK(p);
466 	LIST_REMOVE(p, p_pglist);
467 	p->p_pgrp = NULL;
468 	PROC_UNLOCK(p);
469 	PGRP_UNLOCK(savepgrp);
470 	if (LIST_EMPTY(&savepgrp->pg_members))
471 		pgdelete(savepgrp);
472 	PGRPSESS_XUNLOCK();
473 	return (0);
474 }
475 
476 /*
477  * delete a process group
478  */
479 static void
480 pgdelete(pgrp)
481 	register struct pgrp *pgrp;
482 {
483 	struct session *savesess;
484 
485 	PGRPSESS_LOCK_ASSERT(SX_XLOCKED);
486 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
487 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
488 
489 	PGRP_LOCK(pgrp);
490 
491 	/*
492 	 * Reset any sigio structures pointing to us as a result of
493 	 * F_SETOWN with our pgid.
494 	 */
495 	funsetownlst(&pgrp->pg_sigiolst);
496 
497 	if (pgrp->pg_session->s_ttyp != NULL &&
498 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
499 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
500 	LIST_REMOVE(pgrp, pg_hash);
501 	savesess = pgrp->pg_session;
502 	SESS_LOCK(savesess);
503 	savesess->s_count--;
504 	SESS_UNLOCK(savesess);
505 	PGRP_UNLOCK(pgrp);
506 	if (savesess->s_count == 0) {
507 		mtx_destroy(&savesess->s_mtx);
508 		FREE(pgrp->pg_session, M_SESSION);
509 	}
510 	mtx_destroy(&pgrp->pg_mtx);
511 	FREE(pgrp, M_PGRP);
512 }
513 
514 /*
515  * Adjust pgrp jobc counters when specified process changes process group.
516  * We count the number of processes in each process group that "qualify"
517  * the group for terminal job control (those with a parent in a different
518  * process group of the same session).  If that count reaches zero, the
519  * process group becomes orphaned.  Check both the specified process'
520  * process group and that of its children.
521  * entering == 0 => p is leaving specified group.
522  * entering == 1 => p is entering specified group.
523  */
524 void
525 fixjobc(p, pgrp, entering)
526 	register struct proc *p;
527 	register struct pgrp *pgrp;
528 	int entering;
529 {
530 	register struct pgrp *hispgrp;
531 	register struct session *mysession;
532 
533 	PGRPSESS_LOCK_ASSERT(SX_LOCKED);
534 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
535 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
536 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
537 
538 	/*
539 	 * Check p's parent to see whether p qualifies its own process
540 	 * group; if so, adjust count for p's process group.
541 	 */
542 	mysession = pgrp->pg_session;
543 	sx_slock(&proctree_lock);
544 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
545 	    hispgrp->pg_session == mysession) {
546 		PGRP_LOCK(pgrp);
547 		if (entering)
548 			pgrp->pg_jobc++;
549 		else {
550 			--pgrp->pg_jobc;
551 			if (pgrp->pg_jobc == 0)
552 				orphanpg(pgrp);
553 		}
554 		PGRP_UNLOCK(pgrp);
555 	}
556 
557 	/*
558 	 * Check this process' children to see whether they qualify
559 	 * their process groups; if so, adjust counts for children's
560 	 * process groups.
561 	 */
562 	LIST_FOREACH(p, &p->p_children, p_sibling) {
563 		if ((hispgrp = p->p_pgrp) != pgrp &&
564 		    hispgrp->pg_session == mysession &&
565 		    p->p_stat != SZOMB) {
566 			PGRP_LOCK(hispgrp);
567 			if (entering)
568 				hispgrp->pg_jobc++;
569 			else {
570 				--hispgrp->pg_jobc;
571 				if (hispgrp->pg_jobc == 0)
572 					orphanpg(hispgrp);
573 			}
574 			PGRP_UNLOCK(hispgrp);
575 		}
576 	}
577 	sx_sunlock(&proctree_lock);
578 }
579 
580 /*
581  * A process group has become orphaned;
582  * if there are any stopped processes in the group,
583  * hang-up all process in that group.
584  */
585 static void
586 orphanpg(pg)
587 	struct pgrp *pg;
588 {
589 	register struct proc *p;
590 
591 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
592 
593 	mtx_lock_spin(&sched_lock);
594 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
595 		if (p->p_stat == SSTOP) {
596 			mtx_unlock_spin(&sched_lock);
597 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
598 				PROC_LOCK(p);
599 				psignal(p, SIGHUP);
600 				psignal(p, SIGCONT);
601 				PROC_UNLOCK(p);
602 			}
603 			return;
604 		}
605 	}
606 	mtx_unlock_spin(&sched_lock);
607 }
608 
609 #include "opt_ddb.h"
610 #ifdef DDB
611 #include <ddb/ddb.h>
612 
613 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
614 {
615 	register struct pgrp *pgrp;
616 	register struct proc *p;
617 	register int i;
618 
619 	for (i = 0; i <= pgrphash; i++) {
620 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
621 			printf("\tindx %d\n", i);
622 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
623 				printf(
624 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
625 				    (void *)pgrp, (long)pgrp->pg_id,
626 				    (void *)pgrp->pg_session,
627 				    pgrp->pg_session->s_count,
628 				    (void *)LIST_FIRST(&pgrp->pg_members));
629 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
630 					printf("\t\tpid %ld addr %p pgrp %p\n",
631 					    (long)p->p_pid, (void *)p,
632 					    (void *)p->p_pgrp);
633 				}
634 			}
635 		}
636 	}
637 }
638 #endif /* DDB */
639 
640 /*
641  * Fill in an kinfo_proc structure for the specified process.
642  */
643 void
644 fill_kinfo_proc(p, kp)
645 	struct proc *p;
646 	struct kinfo_proc *kp;
647 {
648 	struct thread *td;
649 	struct tty *tp;
650 	struct session *sp;
651 	struct timeval tv;
652 
653 	bzero(kp, sizeof(*kp));
654 
655 	kp->ki_structsize = sizeof(*kp);
656 	kp->ki_paddr = p;
657 	PROC_LOCK(p);
658 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
659 	kp->ki_args = p->p_args;
660 	kp->ki_tracep = p->p_tracep;
661 	kp->ki_textvp = p->p_textvp;
662 	kp->ki_fd = p->p_fd;
663 	kp->ki_vmspace = p->p_vmspace;
664 	if (p->p_ucred) {
665 		kp->ki_uid = p->p_ucred->cr_uid;
666 		kp->ki_ruid = p->p_ucred->cr_ruid;
667 		kp->ki_svuid = p->p_ucred->cr_svuid;
668 		/* XXX bde doesn't like KI_NGROUPS */
669 		kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS);
670 		bcopy(p->p_ucred->cr_groups, kp->ki_groups,
671 		    kp->ki_ngroups * sizeof(gid_t));
672 		kp->ki_rgid = p->p_ucred->cr_rgid;
673 		kp->ki_svgid = p->p_ucred->cr_svgid;
674 	}
675 	if (p->p_procsig) {
676 		kp->ki_sigignore = p->p_procsig->ps_sigignore;
677 		kp->ki_sigcatch = p->p_procsig->ps_sigcatch;
678 	}
679 	mtx_lock_spin(&sched_lock);
680 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
681 		struct vmspace *vm = p->p_vmspace;
682 
683 		kp->ki_size = vm->vm_map.size;
684 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
685 		if (p->p_sflag & PS_INMEM)
686 			kp->ki_rssize += UAREA_PAGES;
687 		FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */
688 			kp->ki_rssize += KSTACK_PAGES;
689 		kp->ki_swrss = vm->vm_swrss;
690 		kp->ki_tsize = vm->vm_tsize;
691 		kp->ki_dsize = vm->vm_dsize;
692 		kp->ki_ssize = vm->vm_ssize;
693 	}
694 	if ((p->p_sflag & PS_INMEM) && p->p_stats) {
695 		kp->ki_start = p->p_stats->p_start;
696 		kp->ki_rusage = p->p_stats->p_ru;
697 		kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec +
698 		    p->p_stats->p_cru.ru_stime.tv_sec;
699 		kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec +
700 		    p->p_stats->p_cru.ru_stime.tv_usec;
701 	}
702 	td = FIRST_THREAD_IN_PROC(p);
703 	if (td->td_wmesg != NULL)
704 		strncpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg) - 1);
705 	if (p->p_stat == SMTX) {
706 		kp->ki_kiflag |= KI_MTXBLOCK;
707 		strncpy(kp->ki_mtxname, td->td_mtxname,
708 		    sizeof(kp->ki_mtxname) - 1);
709 	}
710 	kp->ki_stat = p->p_stat;
711 	kp->ki_sflag = p->p_sflag;
712 	kp->ki_swtime = p->p_swtime;
713 	kp->ki_traceflag = p->p_traceflag;
714 	kp->ki_pid = p->p_pid;
715 	/* vvv XXXKSE */
716 	bintime2timeval(&p->p_runtime, &tv);
717 	kp->ki_runtime = tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
718 	kp->ki_pctcpu = p->p_kse.ke_pctcpu;
719 	kp->ki_estcpu = td->td_ksegrp->kg_estcpu;
720 	kp->ki_slptime = td->td_ksegrp->kg_slptime;
721 	kp->ki_wchan = td->td_wchan;
722 	kp->ki_pri.pri_level = td->td_priority;
723 	kp->ki_pri.pri_user = td->td_ksegrp->kg_user_pri;
724 	kp->ki_pri.pri_class = td->td_ksegrp->kg_pri_class;
725 	kp->ki_pri.pri_native = td->td_base_pri;
726 	kp->ki_nice = td->td_ksegrp->kg_nice;
727 	kp->ki_rqindex = p->p_kse.ke_rqindex;
728 	kp->ki_oncpu = p->p_kse.ke_oncpu;
729 	kp->ki_lastcpu = td->td_lastcpu;
730 	kp->ki_tdflags = td->td_flags;
731 	kp->ki_pcb = td->td_pcb;
732 	kp->ki_kstack = (void *)td->td_kstack;
733 	/* ^^^ XXXKSE */
734 	mtx_unlock_spin(&sched_lock);
735 	sp = NULL;
736 	tp = NULL;
737 	if (p->p_pgrp) {
738 		kp->ki_pgid = p->p_pgrp->pg_id;
739 		kp->ki_jobc = p->p_pgrp->pg_jobc;
740 		sp = p->p_pgrp->pg_session;
741 
742 		if (sp != NULL) {
743 			kp->ki_sid = sp->s_sid;
744 			SESS_LOCK(sp);
745 			strncpy(kp->ki_login, sp->s_login,
746 			    sizeof(kp->ki_login) - 1);
747 			if (sp->s_ttyvp)
748 				kp->ki_kiflag |= KI_CTTY;
749 			if (SESS_LEADER(p))
750 				kp->ki_kiflag |= KI_SLEADER;
751 			tp = sp->s_ttyp;
752 			SESS_UNLOCK(sp);
753 		}
754 	}
755 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
756 		kp->ki_tdev = dev2udev(tp->t_dev);
757 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
758 		if (tp->t_session)
759 			kp->ki_tsid = tp->t_session->s_sid;
760 	} else
761 		kp->ki_tdev = NOUDEV;
762 	if (p->p_comm[0] != '\0') {
763 		strncpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm) - 1);
764 		strncpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm) - 1);
765 	}
766 	kp->ki_siglist = p->p_siglist;
767 	kp->ki_sigmask = p->p_sigmask;
768 	kp->ki_xstat = p->p_xstat;
769 	kp->ki_acflag = p->p_acflag;
770 	kp->ki_flag = p->p_flag;
771 	/* If jailed(p->p_ucred), emulate the old P_JAILED flag. */
772 	if (jailed(p->p_ucred))
773 		kp->ki_flag |= P_JAILED;
774 	kp->ki_lock = p->p_lock;
775 	if (p->p_pptr)
776 		kp->ki_ppid = p->p_pptr->p_pid;
777 	PROC_UNLOCK(p);
778 }
779 
780 /*
781  * Locate a zombie process by number
782  */
783 struct proc *
784 zpfind(pid_t pid)
785 {
786 	struct proc *p;
787 
788 	sx_slock(&allproc_lock);
789 	LIST_FOREACH(p, &zombproc, p_list)
790 		if (p->p_pid == pid) {
791 			PROC_LOCK(p);
792 			break;
793 		}
794 	sx_sunlock(&allproc_lock);
795 	return (p);
796 }
797 
798 
799 static int
800 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
801 {
802 	struct kinfo_proc kinfo_proc;
803 	int error;
804 	struct proc *np;
805 	pid_t pid = p->p_pid;
806 
807 	fill_kinfo_proc(p, &kinfo_proc);
808 	error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc));
809 	if (error)
810 		return (error);
811 	if (doingzomb)
812 		np = zpfind(pid);
813 	else {
814 		if (pid == 0)
815 			return (0);
816 		np = pfind(pid);
817 	}
818 	if (np == NULL)
819 		return EAGAIN;
820 	if (np != p) {
821 		PROC_UNLOCK(np);
822 		return EAGAIN;
823 	}
824 	PROC_UNLOCK(np);
825 	return (0);
826 }
827 
828 static int
829 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
830 {
831 	int *name = (int*) arg1;
832 	u_int namelen = arg2;
833 	struct proc *p;
834 	int doingzomb;
835 	int error = 0;
836 
837 	if (oidp->oid_number == KERN_PROC_PID) {
838 		if (namelen != 1)
839 			return (EINVAL);
840 		p = pfind((pid_t)name[0]);
841 		if (!p)
842 			return (0);
843 		if (p_cansee(curproc, p)) {
844 			PROC_UNLOCK(p);
845 			return (0);
846 		}
847 		PROC_UNLOCK(p);
848 		error = sysctl_out_proc(p, req, 0);
849 		return (error);
850 	}
851 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
852 		;
853 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
854 		;
855 	else
856 		return (EINVAL);
857 
858 	if (!req->oldptr) {
859 		/* overestimate by 5 procs */
860 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
861 		if (error)
862 			return (error);
863 	}
864 	sx_slock(&allproc_lock);
865 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
866 		if (!doingzomb)
867 			p = LIST_FIRST(&allproc);
868 		else
869 			p = LIST_FIRST(&zombproc);
870 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
871 			/*
872 			 * Show a user only appropriate processes.
873 			 */
874 			if (p_cansee(curproc, p))
875 				continue;
876 			/*
877 			 * Skip embryonic processes.
878 			 */
879 			if (p->p_stat == SIDL)
880 				continue;
881 			/*
882 			 * TODO - make more efficient (see notes below).
883 			 * do by session.
884 			 */
885 			switch (oidp->oid_number) {
886 
887 			case KERN_PROC_PGRP:
888 				/* could do this by traversing pgrp */
889 				PROC_LOCK(p);
890 				if (p->p_pgrp == NULL ||
891 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
892 					PROC_UNLOCK(p);
893 					continue;
894 				}
895 				PROC_UNLOCK(p);
896 				break;
897 
898 			case KERN_PROC_TTY:
899 				PROC_LOCK(p);
900 				if ((p->p_flag & P_CONTROLT) == 0 ||
901 				    p->p_session == NULL) {
902 					PROC_UNLOCK(p);
903 					continue;
904 				}
905 				SESS_LOCK(p->p_session);
906 				if (p->p_session->s_ttyp == NULL ||
907 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
908 				    (udev_t)name[0]) {
909 					SESS_UNLOCK(p->p_session);
910 					PROC_UNLOCK(p);
911 					continue;
912 				}
913 				SESS_UNLOCK(p->p_session);
914 				PROC_UNLOCK(p);
915 				break;
916 
917 			case KERN_PROC_UID:
918 				if (p->p_ucred == NULL ||
919 				    p->p_ucred->cr_uid != (uid_t)name[0])
920 					continue;
921 				break;
922 
923 			case KERN_PROC_RUID:
924 				if (p->p_ucred == NULL ||
925 				    p->p_ucred->cr_ruid != (uid_t)name[0])
926 					continue;
927 				break;
928 			}
929 
930 			if (p_cansee(curproc, p))
931 				continue;
932 
933 			error = sysctl_out_proc(p, req, doingzomb);
934 			if (error) {
935 				sx_sunlock(&allproc_lock);
936 				return (error);
937 			}
938 		}
939 	}
940 	sx_sunlock(&allproc_lock);
941 	return (0);
942 }
943 
944 /*
945  * This sysctl allows a process to retrieve the argument list or process
946  * title for another process without groping around in the address space
947  * of the other process.  It also allow a process to set its own "process
948  * title to a string of its own choice.
949  */
950 static int
951 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
952 {
953 	int *name = (int*) arg1;
954 	u_int namelen = arg2;
955 	struct proc *p;
956 	struct pargs *pa;
957 	int error = 0;
958 
959 	if (namelen != 1)
960 		return (EINVAL);
961 
962 	p = pfind((pid_t)name[0]);
963 	if (!p)
964 		return (0);
965 
966 	if ((!ps_argsopen) && p_cansee(curproc, p)) {
967 		PROC_UNLOCK(p);
968 		return (0);
969 	}
970 	PROC_UNLOCK(p);
971 
972 	if (req->newptr && curproc != p)
973 		return (EPERM);
974 
975 	if (req->oldptr && p->p_args != NULL)
976 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
977 	if (req->newptr == NULL)
978 		return (error);
979 
980 	PROC_LOCK(p);
981 	pa = p->p_args;
982 	p->p_args = NULL;
983 	PROC_UNLOCK(p);
984 	if (pa != NULL && --pa->ar_ref == 0)
985 		FREE(pa, M_PARGS);
986 
987 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
988 		return (error);
989 
990 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
991 	    M_PARGS, M_WAITOK);
992 	pa->ar_ref = 1;
993 	pa->ar_length = req->newlen;
994 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
995 	if (!error) {
996 		PROC_LOCK(p);
997 		p->p_args = pa;
998 		PROC_UNLOCK(p);
999 	} else
1000 		FREE(pa, M_PARGS);
1001 	return (error);
1002 }
1003 
1004 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1005 
1006 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1007 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1008 
1009 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1010 	sysctl_kern_proc, "Process table");
1011 
1012 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1013 	sysctl_kern_proc, "Process table");
1014 
1015 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1016 	sysctl_kern_proc, "Process table");
1017 
1018 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1019 	sysctl_kern_proc, "Process table");
1020 
1021 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1022 	sysctl_kern_proc, "Process table");
1023 
1024 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1025 	sysctl_kern_proc_args, "Process argument list");
1026