xref: /freebsd/sys/kern/kern_proc.c (revision 6de306ecee3831f48debaad1d0b22418faa48e10)
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/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/filedesc.h>
44 #include <sys/tty.h>
45 #include <sys/signalvar.h>
46 #include <vm/vm.h>
47 #include <sys/lock.h>
48 #include <vm/pmap.h>
49 #include <vm/vm_map.h>
50 #include <sys/user.h>
51 #include <vm/vm_zone.h>
52 #include <sys/jail.h>
53 
54 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
55 MALLOC_DEFINE(M_SESSION, "session", "session header");
56 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
57 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
58 
59 int ps_showallprocs = 1;
60 SYSCTL_INT(_kern, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
61     &ps_showallprocs, 0, "");
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 lock allproc_lock;
77 struct lock 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 
88 	lockinit(&allproc_lock, PZERO, "allproc", 0, 0);
89 	lockinit(&proctree_lock, PZERO, "proctree", 0, 0);
90 	LIST_INIT(&allproc);
91 	LIST_INIT(&zombproc);
92 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
93 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
94 	proc_zone = zinit("PROC", sizeof (struct proc), 0, 0, 5);
95 	uihashinit();
96 	/*
97 	 * This should really be a compile time warning, but I do
98 	 * not know of any way to do that...
99 	 */
100 	if (sizeof(struct kinfo_proc) != KINFO_PROC_SIZE)
101 		printf("WARNING: size of kinfo_proc (%ld) should be %d!!!\n",
102 			(long)sizeof(struct kinfo_proc), KINFO_PROC_SIZE);
103 }
104 
105 /*
106  * Is p an inferior of the current process?
107  */
108 int
109 inferior(p)
110 	register struct proc *p;
111 {
112 	int rval = 1;
113 
114 	PROCTREE_LOCK(PT_SHARED);
115 	for (; p != curproc; p = p->p_pptr)
116 		if (p->p_pid == 0) {
117 			rval = 0;
118 			break;
119 		}
120 	PROCTREE_LOCK(PT_RELEASE);
121 	return (rval);
122 }
123 
124 /*
125  * Locate a process by number
126  */
127 struct proc *
128 pfind(pid)
129 	register pid_t pid;
130 {
131 	register struct proc *p;
132 
133 	ALLPROC_LOCK(AP_SHARED);
134 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
135 		if (p->p_pid == pid)
136 			break;
137 	ALLPROC_LOCK(AP_RELEASE);
138 	return (p);
139 }
140 
141 /*
142  * Locate a process group by number
143  */
144 struct pgrp *
145 pgfind(pgid)
146 	register pid_t pgid;
147 {
148 	register struct pgrp *pgrp;
149 
150 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
151 		if (pgrp->pg_id == pgid)
152 			return (pgrp);
153 	return (NULL);
154 }
155 
156 /*
157  * Move p to a new or existing process group (and session)
158  */
159 int
160 enterpgrp(p, pgid, mksess)
161 	register struct proc *p;
162 	pid_t pgid;
163 	int mksess;
164 {
165 	register struct pgrp *pgrp = pgfind(pgid);
166 
167 	KASSERT(pgrp == NULL || !mksess,
168 	    ("enterpgrp: setsid into non-empty pgrp"));
169 	KASSERT(!SESS_LEADER(p),
170 	    ("enterpgrp: session leader attempted setpgrp"));
171 
172 	if (pgrp == NULL) {
173 		pid_t savepid = p->p_pid;
174 		struct proc *np;
175 		/*
176 		 * new process group
177 		 */
178 		KASSERT(p->p_pid == pgid,
179 		    ("enterpgrp: new pgrp and pid != pgid"));
180 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
181 		    M_WAITOK);
182 		if ((np = pfind(savepid)) == NULL || np != p)
183 			return (ESRCH);
184 		if (mksess) {
185 			register struct session *sess;
186 
187 			/*
188 			 * new session
189 			 */
190 			MALLOC(sess, struct session *, sizeof(struct session),
191 			    M_SESSION, M_WAITOK);
192 			sess->s_leader = p;
193 			sess->s_sid = p->p_pid;
194 			sess->s_count = 1;
195 			sess->s_ttyvp = NULL;
196 			sess->s_ttyp = NULL;
197 			bcopy(p->p_session->s_login, sess->s_login,
198 			    sizeof(sess->s_login));
199 			p->p_flag &= ~P_CONTROLT;
200 			pgrp->pg_session = sess;
201 			KASSERT(p == curproc,
202 			    ("enterpgrp: mksession and p != curproc"));
203 		} else {
204 			pgrp->pg_session = p->p_session;
205 			pgrp->pg_session->s_count++;
206 		}
207 		pgrp->pg_id = pgid;
208 		LIST_INIT(&pgrp->pg_members);
209 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
210 		pgrp->pg_jobc = 0;
211 		SLIST_INIT(&pgrp->pg_sigiolst);
212 	} else if (pgrp == p->p_pgrp)
213 		return (0);
214 
215 	/*
216 	 * Adjust eligibility of affected pgrps to participate in job control.
217 	 * Increment eligibility counts before decrementing, otherwise we
218 	 * could reach 0 spuriously during the first call.
219 	 */
220 	fixjobc(p, pgrp, 1);
221 	fixjobc(p, p->p_pgrp, 0);
222 
223 	LIST_REMOVE(p, p_pglist);
224 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
225 		pgdelete(p->p_pgrp);
226 	p->p_pgrp = pgrp;
227 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
228 	return (0);
229 }
230 
231 /*
232  * remove process from process group
233  */
234 int
235 leavepgrp(p)
236 	register struct proc *p;
237 {
238 
239 	LIST_REMOVE(p, p_pglist);
240 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
241 		pgdelete(p->p_pgrp);
242 	p->p_pgrp = 0;
243 	return (0);
244 }
245 
246 /*
247  * delete a process group
248  */
249 static void
250 pgdelete(pgrp)
251 	register struct pgrp *pgrp;
252 {
253 
254 	/*
255 	 * Reset any sigio structures pointing to us as a result of
256 	 * F_SETOWN with our pgid.
257 	 */
258 	funsetownlst(&pgrp->pg_sigiolst);
259 
260 	if (pgrp->pg_session->s_ttyp != NULL &&
261 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
262 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
263 	LIST_REMOVE(pgrp, pg_hash);
264 	if (--pgrp->pg_session->s_count == 0)
265 		FREE(pgrp->pg_session, M_SESSION);
266 	FREE(pgrp, M_PGRP);
267 }
268 
269 /*
270  * Adjust pgrp jobc counters when specified process changes process group.
271  * We count the number of processes in each process group that "qualify"
272  * the group for terminal job control (those with a parent in a different
273  * process group of the same session).  If that count reaches zero, the
274  * process group becomes orphaned.  Check both the specified process'
275  * process group and that of its children.
276  * entering == 0 => p is leaving specified group.
277  * entering == 1 => p is entering specified group.
278  */
279 void
280 fixjobc(p, pgrp, entering)
281 	register struct proc *p;
282 	register struct pgrp *pgrp;
283 	int entering;
284 {
285 	register struct pgrp *hispgrp;
286 	register struct session *mysession = pgrp->pg_session;
287 
288 	/*
289 	 * Check p's parent to see whether p qualifies its own process
290 	 * group; if so, adjust count for p's process group.
291 	 */
292 	PROCTREE_LOCK(PT_SHARED);
293 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
294 	    hispgrp->pg_session == mysession) {
295 		if (entering)
296 			pgrp->pg_jobc++;
297 		else if (--pgrp->pg_jobc == 0)
298 			orphanpg(pgrp);
299 	}
300 
301 	/*
302 	 * Check this process' children to see whether they qualify
303 	 * their process groups; if so, adjust counts for children's
304 	 * process groups.
305 	 */
306 	LIST_FOREACH(p, &p->p_children, p_sibling)
307 		if ((hispgrp = p->p_pgrp) != pgrp &&
308 		    hispgrp->pg_session == mysession &&
309 		    p->p_stat != SZOMB) {
310 			if (entering)
311 				hispgrp->pg_jobc++;
312 			else if (--hispgrp->pg_jobc == 0)
313 				orphanpg(hispgrp);
314 		}
315 	PROCTREE_LOCK(PT_RELEASE);
316 }
317 
318 /*
319  * A process group has become orphaned;
320  * if there are any stopped processes in the group,
321  * hang-up all process in that group.
322  */
323 static void
324 orphanpg(pg)
325 	struct pgrp *pg;
326 {
327 	register struct proc *p;
328 
329 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
330 		if (p->p_stat == SSTOP) {
331 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
332 				psignal(p, SIGHUP);
333 				psignal(p, SIGCONT);
334 			}
335 			return;
336 		}
337 	}
338 }
339 
340 #include "opt_ddb.h"
341 #ifdef DDB
342 #include <ddb/ddb.h>
343 
344 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
345 {
346 	register struct pgrp *pgrp;
347 	register struct proc *p;
348 	register int i;
349 
350 	for (i = 0; i <= pgrphash; i++) {
351 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
352 			printf("\tindx %d\n", i);
353 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
354 				printf(
355 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
356 				    (void *)pgrp, (long)pgrp->pg_id,
357 				    (void *)pgrp->pg_session,
358 				    pgrp->pg_session->s_count,
359 				    (void *)LIST_FIRST(&pgrp->pg_members));
360 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
361 					printf("\t\tpid %ld addr %p pgrp %p\n",
362 					    (long)p->p_pid, (void *)p,
363 					    (void *)p->p_pgrp);
364 				}
365 			}
366 		}
367 	}
368 }
369 #endif /* DDB */
370 
371 /*
372  * Fill in an kinfo_proc structure for the specified process.
373  */
374 void
375 fill_kinfo_proc(p, kp)
376 	struct proc *p;
377 	struct kinfo_proc *kp;
378 {
379 	struct tty *tp;
380 	struct session *sp;
381 
382 	bzero(kp, sizeof(*kp));
383 
384 	kp->ki_structsize = sizeof(*kp);
385 	kp->ki_paddr = p;
386 	PROC_LOCK(p);
387 	kp->ki_addr = p->p_addr;
388 	kp->ki_args = p->p_args;
389 	kp->ki_tracep = p->p_tracep;
390 	kp->ki_textvp = p->p_textvp;
391 	kp->ki_fd = p->p_fd;
392 	kp->ki_vmspace = p->p_vmspace;
393 	if (p->p_cred) {
394 		kp->ki_uid = p->p_cred->pc_ucred->cr_uid;
395 		kp->ki_ruid = p->p_cred->p_ruid;
396 		kp->ki_svuid = p->p_cred->p_svuid;
397 		kp->ki_ngroups = p->p_cred->pc_ucred->cr_ngroups;
398 		bcopy(p->p_cred->pc_ucred->cr_groups, kp->ki_groups,
399 		    NGROUPS * sizeof(gid_t));
400 		kp->ki_rgid = p->p_cred->p_rgid;
401 		kp->ki_svgid = p->p_cred->p_svgid;
402 	}
403 	if (p->p_procsig) {
404 		kp->ki_sigignore = p->p_procsig->ps_sigignore;
405 		kp->ki_sigcatch = p->p_procsig->ps_sigcatch;
406 	}
407 	mtx_lock_spin(&sched_lock);
408 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
409 		struct vmspace *vm = p->p_vmspace;
410 
411 		kp->ki_size = vm->vm_map.size;
412 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
413 		kp->ki_swrss = vm->vm_swrss;
414 		kp->ki_tsize = vm->vm_tsize;
415 		kp->ki_dsize = vm->vm_dsize;
416 		kp->ki_ssize = vm->vm_ssize;
417 	}
418 	if ((p->p_sflag & PS_INMEM) && p->p_stats) {
419 		kp->ki_start = p->p_stats->p_start;
420 		kp->ki_rusage = p->p_stats->p_ru;
421 		kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec +
422 		    p->p_stats->p_cru.ru_stime.tv_sec;
423 		kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec +
424 		    p->p_stats->p_cru.ru_stime.tv_usec;
425 	}
426 	if (p->p_wmesg) {
427 		strncpy(kp->ki_wmesg, p->p_wmesg, WMESGLEN);
428 		kp->ki_wmesg[WMESGLEN] = 0;
429 	}
430 	if (p->p_stat == SMTX) {
431 		kp->ki_kiflag |= KI_MTXBLOCK;
432 		strncpy(kp->ki_mtxname, p->p_mtxname, MTXNAMELEN);
433 		kp->ki_mtxname[MTXNAMELEN] = 0;
434 	}
435 	kp->ki_stat = p->p_stat;
436 	kp->ki_sflag = p->p_sflag;
437 	kp->ki_pctcpu = p->p_pctcpu;
438 	kp->ki_estcpu = p->p_estcpu;
439 	kp->ki_slptime = p->p_slptime;
440 	kp->ki_swtime = p->p_swtime;
441 	kp->ki_wchan = p->p_wchan;
442 	kp->ki_traceflag = p->p_traceflag;
443 	kp->ki_pri = p->p_pri;
444 	kp->ki_nice = p->p_nice;
445 	kp->ki_runtime = p->p_runtime;
446 	kp->ki_pid = p->p_pid;
447 	kp->ki_rqindex = p->p_rqindex;
448 	kp->ki_oncpu = p->p_oncpu;
449 	kp->ki_lastcpu = p->p_lastcpu;
450 	mtx_unlock_spin(&sched_lock);
451 	sp = NULL;
452 	if (p->p_pgrp) {
453 		kp->ki_pgid = p->p_pgrp->pg_id;
454 		kp->ki_jobc = p->p_pgrp->pg_jobc;
455 		sp = p->p_pgrp->pg_session;
456 
457 		if (sp != NULL) {
458 			kp->ki_sid = sp->s_sid;
459 			bcopy(sp->s_login, kp->ki_login, sizeof(kp->ki_login));
460 			if (sp->s_ttyvp)
461 				kp->ki_kiflag = KI_CTTY;
462 			if (SESS_LEADER(p))
463 				kp->ki_kiflag |= KI_SLEADER;
464 		}
465 	}
466 	if ((p->p_flag & P_CONTROLT) && sp && ((tp = sp->s_ttyp) != NULL)) {
467 		kp->ki_tdev = dev2udev(tp->t_dev);
468 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
469 		if (tp->t_session)
470 			kp->ki_tsid = tp->t_session->s_sid;
471 	} else
472 		kp->ki_tdev = NOUDEV;
473 	if (p->p_comm[0] != 0) {
474 		strncpy(kp->ki_comm, p->p_comm, MAXCOMLEN);
475 		kp->ki_comm[MAXCOMLEN] = 0;
476 	}
477 	kp->ki_siglist = p->p_siglist;
478 	kp->ki_sigmask = p->p_sigmask;
479 	kp->ki_xstat = p->p_xstat;
480 	kp->ki_acflag = p->p_acflag;
481 	kp->ki_flag = p->p_flag;
482 	/* If jailed(p->p_ucred), emulate the old P_JAILED flag. */
483 	if (jailed(p->p_ucred))
484 		kp->ki_flag |= P_JAILED;
485 	kp->ki_lock = p->p_lock;
486 	PROC_UNLOCK(p);
487 	PROCTREE_LOCK(PT_SHARED);
488 	if (p->p_pptr)
489 		kp->ki_ppid = p->p_pptr->p_pid;
490 	PROCTREE_LOCK(PT_RELEASE);
491 }
492 
493 /*
494  * Locate a zombie process by number
495  */
496 struct proc *
497 zpfind(pid_t pid)
498 {
499 	struct proc *p;
500 
501 	ALLPROC_LOCK(AP_SHARED);
502 	LIST_FOREACH(p, &zombproc, p_list)
503 		if (p->p_pid == pid)
504 			break;
505 	ALLPROC_LOCK(AP_RELEASE);
506 	return (p);
507 }
508 
509 
510 static int
511 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
512 {
513 	struct kinfo_proc kinfo_proc;
514 	int error;
515 	pid_t pid = p->p_pid;
516 
517 	fill_kinfo_proc(p, &kinfo_proc);
518 	error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc));
519 	if (error)
520 		return (error);
521 	if (!doingzomb && pid && (pfind(pid) != p))
522 		return EAGAIN;
523 	if (doingzomb && zpfind(pid) != p)
524 		return EAGAIN;
525 	return (0);
526 }
527 
528 static int
529 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
530 {
531 	int *name = (int*) arg1;
532 	u_int namelen = arg2;
533 	struct proc *p;
534 	int doingzomb;
535 	int error = 0;
536 
537 	if (oidp->oid_number == KERN_PROC_PID) {
538 		if (namelen != 1)
539 			return (EINVAL);
540 		p = pfind((pid_t)name[0]);
541 		if (!p)
542 			return (0);
543 		if (p_can(curproc, p, P_CAN_SEE, NULL))
544 			return (0);
545 		error = sysctl_out_proc(p, req, 0);
546 		return (error);
547 	}
548 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
549 		;
550 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
551 		;
552 	else
553 		return (EINVAL);
554 
555 	if (!req->oldptr) {
556 		/* overestimate by 5 procs */
557 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
558 		if (error)
559 			return (error);
560 	}
561 	ALLPROC_LOCK(AP_SHARED);
562 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
563 		if (!doingzomb)
564 			p = LIST_FIRST(&allproc);
565 		else
566 			p = LIST_FIRST(&zombproc);
567 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
568 			/*
569 			 * Show a user only appropriate processes.
570 			 */
571 			if (p_can(curproc, p, P_CAN_SEE, NULL))
572 				continue;
573 			/*
574 			 * Skip embryonic processes.
575 			 */
576 			if (p->p_stat == SIDL)
577 				continue;
578 			/*
579 			 * TODO - make more efficient (see notes below).
580 			 * do by session.
581 			 */
582 			switch (oidp->oid_number) {
583 
584 			case KERN_PROC_PGRP:
585 				/* could do this by traversing pgrp */
586 				if (p->p_pgrp == NULL ||
587 				    p->p_pgrp->pg_id != (pid_t)name[0])
588 					continue;
589 				break;
590 
591 			case KERN_PROC_TTY:
592 				if ((p->p_flag & P_CONTROLT) == 0 ||
593 				    p->p_session == NULL ||
594 				    p->p_session->s_ttyp == NULL ||
595 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
596 					(udev_t)name[0])
597 					continue;
598 				break;
599 
600 			case KERN_PROC_UID:
601 				if (p->p_ucred == NULL ||
602 				    p->p_ucred->cr_uid != (uid_t)name[0])
603 					continue;
604 				break;
605 
606 			case KERN_PROC_RUID:
607 				if (p->p_ucred == NULL ||
608 				    p->p_cred->p_ruid != (uid_t)name[0])
609 					continue;
610 				break;
611 			}
612 
613 			if (p_can(curproc, p, P_CAN_SEE, NULL))
614 				continue;
615 
616 			error = sysctl_out_proc(p, req, doingzomb);
617 			if (error) {
618 				ALLPROC_LOCK(AP_RELEASE);
619 				return (error);
620 			}
621 		}
622 	}
623 	ALLPROC_LOCK(AP_RELEASE);
624 	return (0);
625 }
626 
627 /*
628  * This sysctl allows a process to retrieve the argument list or process
629  * title for another process without groping around in the address space
630  * of the other process.  It also allow a process to set its own "process
631  * title to a string of its own choice.
632  */
633 static int
634 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
635 {
636 	int *name = (int*) arg1;
637 	u_int namelen = arg2;
638 	struct proc *p;
639 	struct pargs *pa;
640 	int error = 0;
641 
642 	if (namelen != 1)
643 		return (EINVAL);
644 
645 	p = pfind((pid_t)name[0]);
646 	if (!p)
647 		return (0);
648 
649 	if ((!ps_argsopen) && p_can(curproc, p, P_CAN_SEE, NULL))
650 		return (0);
651 
652 	if (req->newptr && curproc != p)
653 		return (EPERM);
654 
655 	if (req->oldptr && p->p_args != NULL)
656 		error = SYSCTL_OUT(req, p->p_args->ar_args, p->p_args->ar_length);
657 	if (req->newptr == NULL)
658 		return (error);
659 
660 	if (p->p_args && --p->p_args->ar_ref == 0)
661 		FREE(p->p_args, M_PARGS);
662 	p->p_args = NULL;
663 
664 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
665 		return (error);
666 
667 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + req->newlen,
668 	    M_PARGS, M_WAITOK);
669 	pa->ar_ref = 1;
670 	pa->ar_length = req->newlen;
671 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
672 	if (!error)
673 		p->p_args = pa;
674 	else
675 		FREE(pa, M_PARGS);
676 	return (error);
677 }
678 
679 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
680 
681 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
682 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
683 
684 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
685 	sysctl_kern_proc, "Process table");
686 
687 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
688 	sysctl_kern_proc, "Process table");
689 
690 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
691 	sysctl_kern_proc, "Process table");
692 
693 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
694 	sysctl_kern_proc, "Process table");
695 
696 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
697 	sysctl_kern_proc, "Process table");
698 
699 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
700 	sysctl_kern_proc_args, "Process argument list");
701