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