xref: /dragonfly/sys/kern/kern_proc.c (revision 49781055)
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: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $
35  * $DragonFly: src/sys/kern/kern_proc.c,v 1.22 2005/12/01 18:30:08 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/jail.h>
45 #include <sys/filedesc.h>
46 #include <sys/tty.h>
47 #include <sys/signalvar.h>
48 #include <vm/vm.h>
49 #include <sys/lock.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <sys/user.h>
53 #include <vm/vm_zone.h>
54 #include <machine/smp.h>
55 
56 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
57 MALLOC_DEFINE(M_SESSION, "session", "session header");
58 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
59 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
60 
61 int ps_showallprocs = 1;
62 static int ps_showallthreads = 1;
63 SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
64     &ps_showallprocs, 0, "");
65 SYSCTL_INT(_kern, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
66     &ps_showallthreads, 0, "");
67 
68 static void pgdelete	(struct pgrp *);
69 
70 static void	orphanpg (struct pgrp *pg);
71 
72 /*
73  * Other process lists
74  */
75 struct pidhashhead *pidhashtbl;
76 u_long pidhash;
77 struct pgrphashhead *pgrphashtbl;
78 u_long pgrphash;
79 struct proclist allproc;
80 struct proclist zombproc;
81 vm_zone_t proc_zone;
82 vm_zone_t thread_zone;
83 
84 /*
85  * Initialize global process hashing structures.
86  */
87 void
88 procinit(void)
89 {
90 
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 	thread_zone = zinit("THREAD", sizeof (struct thread), 0, 0, 5);
97 	uihashinit();
98 }
99 
100 /*
101  * Is p an inferior of the current process?
102  */
103 int
104 inferior(struct proc *p)
105 {
106 
107 	for (; p != curproc; p = p->p_pptr)
108 		if (p->p_pid == 0)
109 			return (0);
110 	return (1);
111 }
112 
113 /*
114  * Locate a process by number
115  */
116 struct proc *
117 pfind(pid_t pid)
118 {
119 	struct proc *p;
120 
121 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
122 		if (p->p_pid == pid)
123 			return (p);
124 	return (NULL);
125 }
126 
127 /*
128  * Locate a process group by number
129  */
130 struct pgrp *
131 pgfind(pid_t pgid)
132 {
133 	struct pgrp *pgrp;
134 
135 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
136 		if (pgrp->pg_id == pgid)
137 			return (pgrp);
138 	return (NULL);
139 }
140 
141 /*
142  * Move p to a new or existing process group (and session)
143  */
144 int
145 enterpgrp(struct proc *p, pid_t pgid, int mksess)
146 {
147 	struct pgrp *pgrp = pgfind(pgid);
148 
149 	KASSERT(pgrp == NULL || !mksess,
150 	    ("enterpgrp: setsid into non-empty pgrp"));
151 	KASSERT(!SESS_LEADER(p),
152 	    ("enterpgrp: session leader attempted setpgrp"));
153 
154 	if (pgrp == NULL) {
155 		pid_t savepid = p->p_pid;
156 		struct proc *np;
157 		/*
158 		 * new process group
159 		 */
160 		KASSERT(p->p_pid == pgid,
161 		    ("enterpgrp: new pgrp and pid != pgid"));
162 		if ((np = pfind(savepid)) == NULL || np != p)
163 			return (ESRCH);
164 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
165 		    M_WAITOK);
166 		if (mksess) {
167 			struct session *sess;
168 
169 			/*
170 			 * new session
171 			 */
172 			MALLOC(sess, struct session *, sizeof(struct session),
173 			    M_SESSION, M_WAITOK);
174 			sess->s_leader = p;
175 			sess->s_sid = p->p_pid;
176 			sess->s_count = 1;
177 			sess->s_ttyvp = NULL;
178 			sess->s_ttyp = NULL;
179 			bcopy(p->p_session->s_login, sess->s_login,
180 			    sizeof(sess->s_login));
181 			p->p_flag &= ~P_CONTROLT;
182 			pgrp->pg_session = sess;
183 			KASSERT(p == curproc,
184 			    ("enterpgrp: mksession and p != curproc"));
185 		} else {
186 			pgrp->pg_session = p->p_session;
187 			sess_hold(pgrp->pg_session);
188 		}
189 		pgrp->pg_id = pgid;
190 		LIST_INIT(&pgrp->pg_members);
191 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
192 		pgrp->pg_jobc = 0;
193 		SLIST_INIT(&pgrp->pg_sigiolst);
194 	} else if (pgrp == p->p_pgrp)
195 		return (0);
196 
197 	/*
198 	 * Adjust eligibility of affected pgrps to participate in job control.
199 	 * Increment eligibility counts before decrementing, otherwise we
200 	 * could reach 0 spuriously during the first call.
201 	 */
202 	fixjobc(p, pgrp, 1);
203 	fixjobc(p, p->p_pgrp, 0);
204 
205 	LIST_REMOVE(p, p_pglist);
206 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
207 		pgdelete(p->p_pgrp);
208 	p->p_pgrp = pgrp;
209 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
210 	return (0);
211 }
212 
213 /*
214  * remove process from process group
215  */
216 int
217 leavepgrp(struct proc *p)
218 {
219 
220 	LIST_REMOVE(p, p_pglist);
221 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
222 		pgdelete(p->p_pgrp);
223 	p->p_pgrp = 0;
224 	return (0);
225 }
226 
227 /*
228  * delete a process group
229  */
230 static void
231 pgdelete(struct pgrp *pgrp)
232 {
233 
234 	/*
235 	 * Reset any sigio structures pointing to us as a result of
236 	 * F_SETOWN with our pgid.
237 	 */
238 	funsetownlst(&pgrp->pg_sigiolst);
239 
240 	if (pgrp->pg_session->s_ttyp != NULL &&
241 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
242 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
243 	LIST_REMOVE(pgrp, pg_hash);
244 	sess_rele(pgrp->pg_session);
245 	free(pgrp, M_PGRP);
246 }
247 
248 /*
249  * Adjust the ref count on a session structure.  When the ref count falls to
250  * zero the tty is disassociated from the session and the session structure
251  * is freed.  Note that tty assocation is not itself ref-counted.
252  */
253 void
254 sess_hold(struct session *sp)
255 {
256 	++sp->s_count;
257 }
258 
259 void
260 sess_rele(struct session *sp)
261 {
262 	KKASSERT(sp->s_count > 0);
263 	if (--sp->s_count == 0) {
264 		if (sp->s_ttyp && sp->s_ttyp->t_session) {
265 #ifdef TTY_DO_FULL_CLOSE
266 			/* FULL CLOSE, see ttyclearsession() */
267 			KKASSERT(sp->s_ttyp->t_session == sp);
268 			sp->s_ttyp->t_session = NULL;
269 #else
270 			/* HALF CLOSE, see ttyclearsession() */
271 			if (sp->s_ttyp->t_session == sp)
272 				sp->s_ttyp->t_session = NULL;
273 #endif
274 		}
275 		free(sp, M_SESSION);
276 	}
277 }
278 
279 /*
280  * Adjust pgrp jobc counters when specified process changes process group.
281  * We count the number of processes in each process group that "qualify"
282  * the group for terminal job control (those with a parent in a different
283  * process group of the same session).  If that count reaches zero, the
284  * process group becomes orphaned.  Check both the specified process'
285  * process group and that of its children.
286  * entering == 0 => p is leaving specified group.
287  * entering == 1 => p is entering specified group.
288  */
289 void
290 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
291 {
292 	struct pgrp *hispgrp;
293 	struct session *mysession = pgrp->pg_session;
294 
295 	/*
296 	 * Check p's parent to see whether p qualifies its own process
297 	 * group; if so, adjust count for p's process group.
298 	 */
299 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
300 	    hispgrp->pg_session == mysession) {
301 		if (entering)
302 			pgrp->pg_jobc++;
303 		else if (--pgrp->pg_jobc == 0)
304 			orphanpg(pgrp);
305 	}
306 
307 	/*
308 	 * Check this process' children to see whether they qualify
309 	 * their process groups; if so, adjust counts for children's
310 	 * process groups.
311 	 */
312 	LIST_FOREACH(p, &p->p_children, p_sibling)
313 		if ((hispgrp = p->p_pgrp) != pgrp &&
314 		    hispgrp->pg_session == mysession &&
315 		    (p->p_flag & P_ZOMBIE) == 0) {
316 			if (entering)
317 				hispgrp->pg_jobc++;
318 			else if (--hispgrp->pg_jobc == 0)
319 				orphanpg(hispgrp);
320 		}
321 }
322 
323 /*
324  * A process group has become orphaned;
325  * if there are any stopped processes in the group,
326  * hang-up all process in that group.
327  */
328 static void
329 orphanpg(struct pgrp *pg)
330 {
331 	struct proc *p;
332 
333 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
334 		if (p->p_flag & P_STOPPED) {
335 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
336 				psignal(p, SIGHUP);
337 				psignal(p, SIGCONT);
338 			}
339 			return;
340 		}
341 	}
342 }
343 
344 #include "opt_ddb.h"
345 #ifdef DDB
346 #include <ddb/ddb.h>
347 
348 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
349 {
350 	struct pgrp *pgrp;
351 	struct proc *p;
352 	int i;
353 
354 	for (i = 0; i <= pgrphash; i++) {
355 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
356 			printf("\tindx %d\n", i);
357 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
358 				printf(
359 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
360 				    (void *)pgrp, (long)pgrp->pg_id,
361 				    (void *)pgrp->pg_session,
362 				    pgrp->pg_session->s_count,
363 				    (void *)LIST_FIRST(&pgrp->pg_members));
364 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
365 					printf("\t\tpid %ld addr %p pgrp %p\n",
366 					    (long)p->p_pid, (void *)p,
367 					    (void *)p->p_pgrp);
368 				}
369 			}
370 		}
371 	}
372 }
373 #endif /* DDB */
374 
375 /*
376  * Fill in an eproc structure for the specified thread.
377  */
378 void
379 fill_eproc_td(thread_t td, struct eproc *ep, struct proc *xp)
380 {
381 	bzero(ep, sizeof(*ep));
382 
383 	ep->e_uticks = td->td_uticks;
384 	ep->e_sticks = td->td_sticks;
385 	ep->e_iticks = td->td_iticks;
386 	ep->e_tdev = NOUDEV;
387 	ep->e_cpuid = td->td_gd->gd_cpuid;
388 	if (td->td_wmesg) {
389 		strncpy(ep->e_wmesg, td->td_wmesg, WMESGLEN);
390 		ep->e_wmesg[WMESGLEN] = 0;
391 	}
392 
393 	/*
394 	 * Fake up portions of the proc structure copied out by the sysctl
395 	 * to return useful information.  Note that using td_pri directly
396 	 * is messy because it includes critial section data so we fake
397 	 * up an rtprio.prio for threads.
398 	 */
399 	if (xp) {
400 		*xp = *initproc;
401 		xp->p_rtprio.type = RTP_PRIO_THREAD;
402 		xp->p_rtprio.prio = td->td_pri & TDPRI_MASK;
403 		xp->p_pid = -1;
404 	}
405 }
406 
407 /*
408  * Fill in an eproc structure for the specified process.
409  */
410 void
411 fill_eproc(struct proc *p, struct eproc *ep)
412 {
413 	struct tty *tp;
414 
415 	fill_eproc_td(p->p_thread, ep, NULL);
416 
417 	ep->e_paddr = p;
418 	if (p->p_ucred) {
419 		ep->e_ucred = *p->p_ucred;
420 	}
421 	if (p->p_procsig) {
422 		ep->e_procsig = *p->p_procsig;
423 	}
424 	if (p->p_stat != SIDL && (p->p_flag & P_ZOMBIE) == 0 &&
425 	    p->p_vmspace != NULL) {
426 		struct vmspace *vm = p->p_vmspace;
427 		ep->e_vm = *vm;
428 		ep->e_vm.vm_rssize = vmspace_resident_count(vm); /*XXX*/
429 	}
430 	if ((p->p_flag & P_SWAPPEDOUT) == 0 && p->p_stats)
431 		ep->e_stats = *p->p_stats;
432 	if (p->p_pptr)
433 		ep->e_ppid = p->p_pptr->p_pid;
434 	if (p->p_pgrp) {
435 		ep->e_pgid = p->p_pgrp->pg_id;
436 		ep->e_jobc = p->p_pgrp->pg_jobc;
437 		ep->e_sess = p->p_pgrp->pg_session;
438 
439 		if (ep->e_sess) {
440 			bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login));
441 			if (ep->e_sess->s_ttyvp)
442 				ep->e_flag = EPROC_CTTY;
443 			if (p->p_session && SESS_LEADER(p))
444 				ep->e_flag |= EPROC_SLEADER;
445 		}
446 	}
447 	if ((p->p_flag & P_CONTROLT) &&
448 	    (ep->e_sess != NULL) &&
449 	    ((tp = ep->e_sess->s_ttyp) != NULL)) {
450 		ep->e_tdev = dev2udev(tp->t_dev);
451 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
452 		ep->e_tsess = tp->t_session;
453 	} else {
454 		ep->e_tdev = NOUDEV;
455 	}
456 }
457 
458 struct proc *
459 zpfind(pid_t pid)
460 {
461 	struct proc *p;
462 
463 	LIST_FOREACH(p, &zombproc, p_list)
464 		if (p->p_pid == pid)
465 			return (p);
466 	return (NULL);
467 }
468 
469 static int
470 sysctl_out_proc(struct proc *p, struct thread *td, struct sysctl_req *req, int doingzomb)
471 {
472 	struct eproc eproc;
473 	struct proc xproc;
474 	int error;
475 #if 0
476 	pid_t pid = p->p_pid;
477 #endif
478 
479 	if (p) {
480 		td = p->p_thread;
481 		fill_eproc(p, &eproc);
482 		xproc = *p;
483 
484 		/*
485 		 * p_stat fixup.  If we are in a thread sleep mark p_stat
486 		 * as sleeping if the thread is blocked.
487 		 */
488 		if (p->p_stat == SRUN && td && (td->td_flags & TDF_BLOCKED)) {
489 			xproc.p_stat = SSLEEP;
490 		}
491 		/*
492 		 * If the process is being stopped but is in a normal tsleep,
493 		 * mark it as being SSTOP.
494 		 */
495 		if (p->p_stat == SSLEEP && (p->p_flag & P_STOPPED))
496 			xproc.p_stat = SSTOP;
497 		if (p->p_flag & P_ZOMBIE)
498 			xproc.p_stat = SZOMB;
499 	} else if (td) {
500 		fill_eproc_td(td, &eproc, &xproc);
501 	}
502 	error = SYSCTL_OUT(req,(caddr_t)&xproc, sizeof(struct proc));
503 	if (error)
504 		return (error);
505 	error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc));
506 	if (error)
507 		return (error);
508 	error = SYSCTL_OUT(req,(caddr_t)td, sizeof(struct thread));
509 	if (error)
510 		return (error);
511 #if 0
512 	if (!doingzomb && pid && (pfind(pid) != p))
513 		return EAGAIN;
514 	if (doingzomb && zpfind(pid) != p)
515 		return EAGAIN;
516 #endif
517 	return (0);
518 }
519 
520 static int
521 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
522 {
523 	int *name = (int*) arg1;
524 	u_int namelen = arg2;
525 	struct proc *p;
526 	struct thread *td;
527 	int doingzomb;
528 	int error = 0;
529 	int n;
530 	int origcpu;
531 	struct ucred *cr1 = curproc->p_ucred;
532 
533 	if (oidp->oid_number == KERN_PROC_PID) {
534 		if (namelen != 1)
535 			return (EINVAL);
536 		p = pfind((pid_t)name[0]);
537 		if (!p)
538 			return (0);
539 		if (!PRISON_CHECK(cr1, p->p_ucred))
540 			return (0);
541 		error = sysctl_out_proc(p, NULL, req, 0);
542 		return (error);
543 	}
544 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
545 		;
546 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
547 		;
548 	else
549 		return (EINVAL);
550 
551 	if (!req->oldptr) {
552 		/* overestimate by 5 procs */
553 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
554 		if (error)
555 			return (error);
556 	}
557 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
558 		if (!doingzomb)
559 			p = LIST_FIRST(&allproc);
560 		else
561 			p = LIST_FIRST(&zombproc);
562 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
563 			/*
564 			 * Show a user only their processes.
565 			 */
566 			if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
567 				continue;
568 			/*
569 			 * Skip embryonic processes.
570 			 */
571 			if (p->p_stat == SIDL)
572 				continue;
573 			/*
574 			 * TODO - make more efficient (see notes below).
575 			 * do by session.
576 			 */
577 			switch (oidp->oid_number) {
578 			case KERN_PROC_PGRP:
579 				/* could do this by traversing pgrp */
580 				if (p->p_pgrp == NULL ||
581 				    p->p_pgrp->pg_id != (pid_t)name[0])
582 					continue;
583 				break;
584 
585 			case KERN_PROC_TTY:
586 				if ((p->p_flag & P_CONTROLT) == 0 ||
587 				    p->p_session == NULL ||
588 				    p->p_session->s_ttyp == NULL ||
589 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
590 					(udev_t)name[0])
591 					continue;
592 				break;
593 
594 			case KERN_PROC_UID:
595 				if (p->p_ucred == NULL ||
596 				    p->p_ucred->cr_uid != (uid_t)name[0])
597 					continue;
598 				break;
599 
600 			case KERN_PROC_RUID:
601 				if (p->p_ucred == NULL ||
602 				    p->p_ucred->cr_ruid != (uid_t)name[0])
603 					continue;
604 				break;
605 			}
606 
607 			if (!PRISON_CHECK(cr1, p->p_ucred))
608 				continue;
609 			PHOLD(p);
610 			error = sysctl_out_proc(p, NULL, req, doingzomb);
611 			PRELE(p);
612 			if (error)
613 				return (error);
614 		}
615 	}
616 
617 	/*
618 	 * Iterate over all active cpus and scan their thread list.  Start
619 	 * with the next logical cpu and end with our original cpu.  We
620 	 * migrate our own thread to each target cpu in order to safely scan
621 	 * its thread list.  In the last loop we migrate back to our original
622 	 * cpu.
623 	 */
624 	origcpu = mycpu->gd_cpuid;
625 	if (!ps_showallthreads || jailed(cr1))
626 		goto post_threads;
627 	for (n = 1; n <= ncpus; ++n) {
628 		globaldata_t rgd;
629 		int nid;
630 
631 		nid = (origcpu + n) % ncpus;
632 		if ((smp_active_mask & (1 << nid)) == 0)
633 			continue;
634 		rgd = globaldata_find(nid);
635 		lwkt_setcpu_self(rgd);
636 
637 		TAILQ_FOREACH(td, &mycpu->gd_tdallq, td_allq) {
638 			if (td->td_proc)
639 				continue;
640 			switch (oidp->oid_number) {
641 			case KERN_PROC_PGRP:
642 			case KERN_PROC_TTY:
643 			case KERN_PROC_UID:
644 			case KERN_PROC_RUID:
645 				continue;
646 			default:
647 				break;
648 			}
649 			lwkt_hold(td);
650 			error = sysctl_out_proc(NULL, td, req, doingzomb);
651 			lwkt_rele(td);
652 			if (error)
653 				return (error);
654 		}
655 	}
656 post_threads:
657 	return (0);
658 }
659 
660 /*
661  * This sysctl allows a process to retrieve the argument list or process
662  * title for another process without groping around in the address space
663  * of the other process.  It also allow a process to set its own "process
664  * title to a string of its own choice.
665  */
666 static int
667 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
668 {
669 	int *name = (int*) arg1;
670 	u_int namelen = arg2;
671 	struct proc *p;
672 	struct pargs *pa;
673 	int error = 0;
674 	struct ucred *cr1 = curproc->p_ucred;
675 
676 	if (namelen != 1)
677 		return (EINVAL);
678 
679 	p = pfind((pid_t)name[0]);
680 	if (!p)
681 		return (0);
682 
683 	if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
684 		return (0);
685 
686 	if (req->newptr && curproc != p)
687 		return (EPERM);
688 
689 	if (req->oldptr && p->p_args != NULL)
690 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
691 	if (req->newptr == NULL)
692 		return (error);
693 
694 	if (p->p_args && --p->p_args->ar_ref == 0)
695 		FREE(p->p_args, M_PARGS);
696 	p->p_args = NULL;
697 
698 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
699 		return (error);
700 
701 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
702 	    M_PARGS, M_WAITOK);
703 	pa->ar_ref = 1;
704 	pa->ar_length = req->newlen;
705 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
706 	if (!error)
707 		p->p_args = pa;
708 	else
709 		FREE(pa, M_PARGS);
710 	return (error);
711 }
712 
713 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
714 
715 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
716 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
717 
718 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
719 	sysctl_kern_proc, "Process table");
720 
721 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
722 	sysctl_kern_proc, "Process table");
723 
724 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
725 	sysctl_kern_proc, "Process table");
726 
727 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
728 	sysctl_kern_proc, "Process table");
729 
730 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
731 	sysctl_kern_proc, "Process table");
732 
733 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
734 	sysctl_kern_proc_args, "Process argument list");
735