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