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