xref: /freebsd/sys/kern/kern_proc.c (revision 51a9219f5780e61e1437d25220bf8750d9df7f8b)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
34  * $FreeBSD$
35  */
36 
37 #include "opt_ktrace.h"
38 #include "opt_kstack_pages.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/sysproto.h>
48 #include <sys/kse.h>
49 #include <sys/smp.h>
50 #include <sys/sysctl.h>
51 #include <sys/filedesc.h>
52 #include <sys/tty.h>
53 #include <sys/signalvar.h>
54 #include <sys/sx.h>
55 #include <sys/user.h>
56 #include <sys/jail.h>
57 #ifdef KTRACE
58 #include <sys/uio.h>
59 #include <sys/ktrace.h>
60 #endif
61 
62 #include <vm/vm.h>
63 #include <vm/vm_extern.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66 #include <vm/uma.h>
67 #include <machine/critical.h>
68 
69 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
70 MALLOC_DEFINE(M_SESSION, "session", "session header");
71 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
72 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
73 
74 static struct proc *dopfind(register pid_t);
75 
76 static void doenterpgrp(struct proc *, struct pgrp *);
77 
78 static void pgdelete(struct pgrp *);
79 
80 static void orphanpg(struct pgrp *pg);
81 
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", sizeof (struct 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 	KASSERT((size == sizeof(struct proc)),
142 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc)));
143 	p = (struct proc *)mem;
144 }
145 
146 /*
147  * Reclaim a proc after use.
148  */
149 static void
150 proc_dtor(void *mem, int size, void *arg)
151 {
152 	struct proc *p;
153 	struct thread *td;
154 	struct ksegrp *kg;
155 	struct kse *ke;
156 
157 	/* INVARIANTS checks go here */
158 	KASSERT((size == sizeof(struct proc)),
159 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc)));
160 	p = (struct proc *)mem;
161 	KASSERT((p->p_numthreads == 1),
162 	    ("bad number of threads in exiting process"));
163         td = FIRST_THREAD_IN_PROC(p);
164 	KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
165         kg = FIRST_KSEGRP_IN_PROC(p);
166 	KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
167         ke = FIRST_KSE_IN_KSEGRP(kg);
168 	KASSERT((ke != NULL), ("proc_dtor: bad ke pointer"));
169 
170 	/* Dispose of an alternate kstack, if it exists.
171 	 * XXX What if there are more than one thread in the proc?
172 	 *     The first thread in the proc is special and not
173 	 *     freed, so you gotta do this here.
174 	 */
175 	if (((p->p_flag & P_KTHREAD) != 0) && (td->td_altkstack != 0))
176 		pmap_dispose_altkstack(td);
177 
178 	/*
179 	 * We want to make sure we know the initial linkages.
180 	 * so for now tear them down and remake them.
181 	 * This is probably un-needed as we can probably rely
182 	 * on the state coming in here from wait4().
183 	 */
184 	proc_linkup(p, kg, ke, td);
185 }
186 
187 /*
188  * Initialize type-stable parts of a proc (when newly created).
189  */
190 static void
191 proc_init(void *mem, int size)
192 {
193 	struct proc *p;
194 	struct thread *td;
195 	struct ksegrp *kg;
196 	struct kse *ke;
197 
198 	KASSERT((size == sizeof(struct proc)),
199 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc)));
200 	p = (struct proc *)mem;
201 	vm_proc_new(p);
202 	td = thread_alloc();
203 	ke = kse_alloc();
204 	kg = ksegrp_alloc();
205 	proc_linkup(p, kg, ke, td);
206 }
207 
208 /*
209  * Tear down type-stable parts of a proc (just before being discarded)
210  */
211 static void
212 proc_fini(void *mem, int size)
213 {
214 	struct proc *p;
215 	struct thread *td;
216 	struct ksegrp *kg;
217 	struct kse *ke;
218 
219 	KASSERT((size == sizeof(struct proc)),
220 	    ("size mismatch: %d != %d\n", size, (int)sizeof(struct proc)));
221 	p = (struct proc *)mem;
222 	KASSERT((p->p_numthreads == 1),
223 	    ("bad number of threads in freeing process"));
224         td = FIRST_THREAD_IN_PROC(p);
225 	KASSERT((td != NULL), ("proc_dtor: bad thread pointer"));
226         kg = FIRST_KSEGRP_IN_PROC(p);
227 	KASSERT((kg != NULL), ("proc_dtor: bad kg pointer"));
228         ke = FIRST_KSE_IN_KSEGRP(kg);
229 	KASSERT((ke != NULL), ("proc_dtor: bad ke pointer"));
230 	vm_proc_dispose(p);
231 	thread_free(td);
232 	ksegrp_free(kg);
233 	kse_free(ke);
234 }
235 
236 /*
237  * KSE is linked onto the idle queue.
238  */
239 void
240 kse_link(struct kse *ke, struct ksegrp *kg)
241 {
242 	struct proc *p = kg->kg_proc;
243 
244 	TAILQ_INSERT_HEAD(&kg->kg_kseq, ke, ke_kglist);
245 	kg->kg_kses++;
246 	ke->ke_state = KES_UNQUEUED;
247 	ke->ke_proc	= p;
248 	ke->ke_ksegrp	= kg;
249 	ke->ke_thread	= NULL;
250 	ke->ke_oncpu = NOCPU;
251 }
252 
253 void
254 ksegrp_link(struct ksegrp *kg, struct proc *p)
255 {
256 
257 	TAILQ_INIT(&kg->kg_threads);
258 	TAILQ_INIT(&kg->kg_runq);	/* links with td_runq */
259 	TAILQ_INIT(&kg->kg_slpq);	/* links with td_runq */
260 	TAILQ_INIT(&kg->kg_kseq);	/* all kses in ksegrp */
261 	TAILQ_INIT(&kg->kg_iq);		/* idle kses in ksegrp */
262 	TAILQ_INIT(&kg->kg_lq);		/* loan kses in ksegrp */
263 	kg->kg_proc	= p;
264 /* the following counters are in the -zero- section and may not need clearing */
265 	kg->kg_numthreads = 0;
266 	kg->kg_runnable = 0;
267 	kg->kg_kses = 0;
268 	kg->kg_idle_kses = 0;
269 	kg->kg_loan_kses = 0;
270 	kg->kg_runq_kses = 0; /* XXXKSE change name */
271 /* link it in now that it's consistent */
272 	p->p_numksegrps++;
273 	TAILQ_INSERT_HEAD(&p->p_ksegrps, kg, kg_ksegrp);
274 }
275 
276 /*
277  * for a newly created process,
278  * link up a the structure and its initial threads etc.
279  */
280 void
281 proc_linkup(struct proc *p, struct ksegrp *kg,
282 			struct kse *ke, struct thread *td)
283 {
284 
285 	TAILQ_INIT(&p->p_ksegrps);	     /* all ksegrps in proc */
286 	TAILQ_INIT(&p->p_threads);	     /* all threads in proc */
287 	TAILQ_INIT(&p->p_suspended);	     /* Threads suspended */
288 	p->p_numksegrps = 0;
289 	p->p_numthreads = 0;
290 
291 	ksegrp_link(kg, p);
292 	kse_link(ke, kg);
293 	thread_link(td, kg);
294 }
295 
296 int
297 kse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap)
298 {
299 
300 	return(ENOSYS);
301 }
302 
303 int
304 kse_exit(struct thread *td, struct kse_exit_args *uap)
305 {
306 
307 	return(ENOSYS);
308 }
309 
310 int
311 kse_release(struct thread *td, struct kse_release_args *uap)
312 {
313 	struct proc *p;
314 
315 	p = td->td_proc;
316 	/* KSE-enabled processes only, please. */
317 	if (p->p_flag & P_KSES) {
318 		PROC_LOCK(p);
319 		mtx_lock_spin(&sched_lock);
320 		thread_exit();
321 		/* NOTREACHED */
322 	}
323 	return (EINVAL);
324 }
325 
326 /* struct kse_wakeup_args {
327 	struct kse_mailbox *mbx;
328 }; */
329 int
330 kse_wakeup(struct thread *td, struct kse_wakeup_args *uap)
331 {
332 	struct proc *p;
333 	struct kse *ke, *ke2;
334 	struct ksegrp *kg;
335 
336 	p = td->td_proc;
337 	/* KSE-enabled processes only, please. */
338 	if (!(p->p_flag & P_KSES))
339 		return EINVAL;
340 	if (td->td_standin == NULL)
341 		td->td_standin = thread_alloc();
342 	ke = NULL;
343 	mtx_lock_spin(&sched_lock);
344 	if (uap->mbx) {
345 		FOREACH_KSEGRP_IN_PROC(p, kg) {
346 			FOREACH_KSE_IN_GROUP(kg, ke2) {
347 				if (ke2->ke_mailbox != uap->mbx)
348 					continue;
349 				if (ke2->ke_state == KES_IDLE) {
350 					ke = ke2;
351 					goto found;
352 				} else {
353 					mtx_unlock_spin(&sched_lock);
354 					td->td_retval[0] = 0;
355 					td->td_retval[1] = 0;
356 					return 0;
357 				}
358 			}
359 		}
360 	} else {
361 		kg = td->td_ksegrp;
362 		ke = TAILQ_FIRST(&kg->kg_iq);
363 	}
364 	if (ke == NULL) {
365 		mtx_unlock_spin(&sched_lock);
366 		return ESRCH;
367 	}
368 found:
369 	thread_schedule_upcall(td, ke);
370 	mtx_unlock_spin(&sched_lock);
371 	td->td_retval[0] = 0;
372 	td->td_retval[1] = 0;
373 	return 0;
374 }
375 
376 /*
377  * No new KSEG: first call: use current KSE, don't schedule an upcall
378  * All other situations, do allocate a new KSE and schedule an upcall on it.
379  */
380 /* struct kse_create_args {
381 	struct kse_mailbox *mbx;
382 	int newgroup;
383 }; */
384 int
385 kse_create(struct thread *td, struct kse_create_args *uap)
386 {
387 	struct kse *newke;
388 	struct kse *ke;
389 	struct ksegrp *newkg;
390 	struct ksegrp *kg;
391 	struct proc *p;
392 	struct kse_mailbox mbx;
393 	int err;
394 
395 	p = td->td_proc;
396 	if ((err = copyin(uap->mbx, &mbx, sizeof(mbx))))
397 		return (err);
398 
399 	p->p_flag |= P_KSES; /* easier to just set it than to test and set */
400 	kg = td->td_ksegrp;
401 	if (uap->newgroup) {
402 		/*
403 		 * If we want a new KSEGRP it doesn't matter whether
404 		 * we have already fired up KSE mode before or not.
405 		 * We put the process in KSE mode and create a new KSEGRP
406 		 * and KSE. If our KSE has not got a mailbox yet then
407 		 * that doesn't matter, just leave it that way. It will
408 		 * ensure that this thread stay BOUND. It's possible
409 		 * that the call came form a threaded library and the main
410 		 * program knows nothing of threads.
411 		 */
412 		newkg = ksegrp_alloc();
413 		bzero(&newkg->kg_startzero, RANGEOF(struct ksegrp,
414 		      kg_startzero, kg_endzero));
415 		bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
416 		      RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy));
417 		newke = kse_alloc();
418 	} else {
419 		/*
420 		 * Otherwise, if we have already set this KSE
421 		 * to have a mailbox, we want to make another KSE here,
422 		 * but only if there are not already the limit, which
423 		 * is 1 per CPU max.
424 		 *
425 		 * If the current KSE doesn't have a mailbox we just use it
426 		 * and give it one.
427 		 *
428 		 * Because we don't like to access
429 		 * the KSE outside of schedlock if we are UNBOUND,
430 		 * (because it can change if we are preempted by an interrupt)
431 		 * we can deduce it as having a mailbox if we are UNBOUND,
432 		 * and only need to actually look at it if we are BOUND,
433 		 * which is safe.
434 		 */
435 		if ((td->td_flags & TDF_UNBOUND) || td->td_kse->ke_mailbox) {
436 #if 0  /* while debugging */
437 #ifdef SMP
438 			if (kg->kg_kses > mp_ncpus)
439 #endif
440 				return (EPROCLIM);
441 #endif
442 			newke = kse_alloc();
443 		} else {
444 			newke = NULL;
445 		}
446 		newkg = NULL;
447 	}
448 	if (newke) {
449 		bzero(&newke->ke_startzero, RANGEOF(struct kse,
450 		      ke_startzero, ke_endzero));
451 #if 0
452 		bcopy(&ke->ke_startcopy, &newke->ke_startcopy,
453 		      RANGEOF(struct kse, ke_startcopy, ke_endcopy));
454 #endif
455 		PROC_LOCK(p);
456 		if (SIGPENDING(p))
457 			newke->ke_flags |= KEF_ASTPENDING;
458 		PROC_UNLOCK(p);
459 		/* For the first call this may not have been set */
460 		if (td->td_standin == NULL) {
461 			td->td_standin = thread_alloc();
462 		}
463 		mtx_lock_spin(&sched_lock);
464 		if (newkg)
465 			ksegrp_link(newkg, p);
466 		else
467 			newkg = kg;
468 		kse_link(newke, newkg);
469 		newke->ke_mailbox = uap->mbx;
470 		newke->ke_upcall = mbx.km_func;
471 		bcopy(&mbx.km_stack, &newke->ke_stack, sizeof(stack_t));
472 		thread_schedule_upcall(td, newke);
473 		mtx_unlock_spin(&sched_lock);
474 	} else {
475 		/*
476 		 * If we didn't allocate a new KSE then the we are using
477 		 * the exisiting (BOUND) kse.
478 		 */
479 		ke = td->td_kse;
480 		ke->ke_mailbox = uap->mbx;
481 		ke->ke_upcall = mbx.km_func;
482 		bcopy(&mbx.km_stack, &ke->ke_stack, sizeof(stack_t));
483 	}
484 	/*
485 	 * Fill out the KSE-mode specific fields of the new kse.
486 	 */
487 
488 	td->td_retval[0] = 0;
489 	td->td_retval[1] = 0;
490 	return (0);
491 }
492 
493 /*
494  * Is p an inferior of the current process?
495  */
496 int
497 inferior(p)
498 	register struct proc *p;
499 {
500 
501 	sx_assert(&proctree_lock, SX_LOCKED);
502 	for (; p != curproc; p = p->p_pptr)
503 		if (p->p_pid == 0)
504 			return (0);
505 	return (1);
506 }
507 
508 /*
509  * Locate a process by number
510  */
511 struct proc *
512 pfind(pid)
513 	register pid_t pid;
514 {
515 	register struct proc *p;
516 
517 	sx_slock(&allproc_lock);
518 	p = dopfind(pid);
519 	sx_sunlock(&allproc_lock);
520 	return (p);
521 }
522 
523 static struct proc *
524 dopfind(pid)
525 	register pid_t pid;
526 {
527 	register struct proc *p;
528 
529 	sx_assert(&allproc_lock, SX_LOCKED);
530 
531 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
532 		if (p->p_pid == pid) {
533 			PROC_LOCK(p);
534 			break;
535 		}
536 	return (p);
537 }
538 
539 /*
540  * Locate a process group by number.
541  * The caller must hold proctree_lock.
542  */
543 struct pgrp *
544 pgfind(pgid)
545 	register pid_t pgid;
546 {
547 	register struct pgrp *pgrp;
548 
549 	sx_assert(&proctree_lock, SX_LOCKED);
550 
551 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
552 		if (pgrp->pg_id == pgid) {
553 			PGRP_LOCK(pgrp);
554 			return (pgrp);
555 		}
556 	}
557 	return (NULL);
558 }
559 
560 /*
561  * Create a new process group.
562  * pgid must be equal to the pid of p.
563  * Begin a new session if required.
564  */
565 int
566 enterpgrp(p, pgid, pgrp, sess)
567 	register struct proc *p;
568 	pid_t pgid;
569 	struct pgrp *pgrp;
570 	struct session *sess;
571 {
572 	struct pgrp *pgrp2;
573 
574 	sx_assert(&proctree_lock, SX_XLOCKED);
575 
576 	KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
577 	KASSERT(p->p_pid == pgid,
578 	    ("enterpgrp: new pgrp and pid != pgid"));
579 
580 	pgrp2 = pgfind(pgid);
581 
582 	KASSERT(pgrp2 == NULL,
583 	    ("enterpgrp: pgrp with pgid exists"));
584 	KASSERT(!SESS_LEADER(p),
585 	    ("enterpgrp: session leader attempted setpgrp"));
586 
587 	mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
588 
589 	if (sess != NULL) {
590 		/*
591 		 * new session
592 		 */
593 		mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
594 		PROC_LOCK(p);
595 		p->p_flag &= ~P_CONTROLT;
596 		PROC_UNLOCK(p);
597 		PGRP_LOCK(pgrp);
598 		sess->s_leader = p;
599 		sess->s_sid = p->p_pid;
600 		sess->s_count = 1;
601 		sess->s_ttyvp = NULL;
602 		sess->s_ttyp = NULL;
603 		bcopy(p->p_session->s_login, sess->s_login,
604 			    sizeof(sess->s_login));
605 		pgrp->pg_session = sess;
606 		KASSERT(p == curproc,
607 		    ("enterpgrp: mksession and p != curproc"));
608 	} else {
609 		pgrp->pg_session = p->p_session;
610 		SESS_LOCK(pgrp->pg_session);
611 		pgrp->pg_session->s_count++;
612 		SESS_UNLOCK(pgrp->pg_session);
613 		PGRP_LOCK(pgrp);
614 	}
615 	pgrp->pg_id = pgid;
616 	LIST_INIT(&pgrp->pg_members);
617 
618 	/*
619 	 * As we have an exclusive lock of proctree_lock,
620 	 * this should not deadlock.
621 	 */
622 	LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
623 	pgrp->pg_jobc = 0;
624 	SLIST_INIT(&pgrp->pg_sigiolst);
625 	PGRP_UNLOCK(pgrp);
626 
627 	doenterpgrp(p, pgrp);
628 
629 	return (0);
630 }
631 
632 /*
633  * Move p to an existing process group
634  */
635 int
636 enterthispgrp(p, pgrp)
637 	register struct proc *p;
638 	struct pgrp *pgrp;
639 {
640 
641 	sx_assert(&proctree_lock, SX_XLOCKED);
642 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
643 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
644 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
645 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
646 	KASSERT(pgrp->pg_session == p->p_session,
647 		("%s: pgrp's session %p, p->p_session %p.\n",
648 		__func__,
649 		pgrp->pg_session,
650 		p->p_session));
651 	KASSERT(pgrp != p->p_pgrp,
652 		("%s: p belongs to pgrp.", __func__));
653 
654 	doenterpgrp(p, pgrp);
655 
656 	return (0);
657 }
658 
659 /*
660  * Move p to a process group
661  */
662 static void
663 doenterpgrp(p, pgrp)
664 	struct proc *p;
665 	struct pgrp *pgrp;
666 {
667 	struct pgrp *savepgrp;
668 
669 	sx_assert(&proctree_lock, SX_XLOCKED);
670 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
671 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
672 	PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
673 	SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
674 
675 	savepgrp = p->p_pgrp;
676 
677 	/*
678 	 * Adjust eligibility of affected pgrps to participate in job control.
679 	 * Increment eligibility counts before decrementing, otherwise we
680 	 * could reach 0 spuriously during the first call.
681 	 */
682 	fixjobc(p, pgrp, 1);
683 	fixjobc(p, p->p_pgrp, 0);
684 
685 	PGRP_LOCK(pgrp);
686 	PGRP_LOCK(savepgrp);
687 	PROC_LOCK(p);
688 	LIST_REMOVE(p, p_pglist);
689 	p->p_pgrp = pgrp;
690 	PROC_UNLOCK(p);
691 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
692 	PGRP_UNLOCK(savepgrp);
693 	PGRP_UNLOCK(pgrp);
694 	if (LIST_EMPTY(&savepgrp->pg_members))
695 		pgdelete(savepgrp);
696 }
697 
698 /*
699  * remove process from process group
700  */
701 int
702 leavepgrp(p)
703 	register struct proc *p;
704 {
705 	struct pgrp *savepgrp;
706 
707 	sx_assert(&proctree_lock, SX_XLOCKED);
708 	savepgrp = p->p_pgrp;
709 	PGRP_LOCK(savepgrp);
710 	PROC_LOCK(p);
711 	LIST_REMOVE(p, p_pglist);
712 	p->p_pgrp = NULL;
713 	PROC_UNLOCK(p);
714 	PGRP_UNLOCK(savepgrp);
715 	if (LIST_EMPTY(&savepgrp->pg_members))
716 		pgdelete(savepgrp);
717 	return (0);
718 }
719 
720 /*
721  * delete a process group
722  */
723 static void
724 pgdelete(pgrp)
725 	register struct pgrp *pgrp;
726 {
727 	struct session *savesess;
728 
729 	sx_assert(&proctree_lock, SX_XLOCKED);
730 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
731 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
732 
733 	/*
734 	 * Reset any sigio structures pointing to us as a result of
735 	 * F_SETOWN with our pgid.
736 	 */
737 	funsetownlst(&pgrp->pg_sigiolst);
738 
739 	PGRP_LOCK(pgrp);
740 	if (pgrp->pg_session->s_ttyp != NULL &&
741 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
742 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
743 	LIST_REMOVE(pgrp, pg_hash);
744 	savesess = pgrp->pg_session;
745 	SESS_LOCK(savesess);
746 	savesess->s_count--;
747 	SESS_UNLOCK(savesess);
748 	PGRP_UNLOCK(pgrp);
749 	if (savesess->s_count == 0) {
750 		mtx_destroy(&savesess->s_mtx);
751 		FREE(pgrp->pg_session, M_SESSION);
752 	}
753 	mtx_destroy(&pgrp->pg_mtx);
754 	FREE(pgrp, M_PGRP);
755 }
756 
757 /*
758  * Adjust pgrp jobc counters when specified process changes process group.
759  * We count the number of processes in each process group that "qualify"
760  * the group for terminal job control (those with a parent in a different
761  * process group of the same session).  If that count reaches zero, the
762  * process group becomes orphaned.  Check both the specified process'
763  * process group and that of its children.
764  * entering == 0 => p is leaving specified group.
765  * entering == 1 => p is entering specified group.
766  */
767 void
768 fixjobc(p, pgrp, entering)
769 	register struct proc *p;
770 	register struct pgrp *pgrp;
771 	int entering;
772 {
773 	register struct pgrp *hispgrp;
774 	register struct session *mysession;
775 
776 	sx_assert(&proctree_lock, SX_LOCKED);
777 	PROC_LOCK_ASSERT(p, MA_NOTOWNED);
778 	PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
779 	SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
780 
781 	/*
782 	 * Check p's parent to see whether p qualifies its own process
783 	 * group; if so, adjust count for p's process group.
784 	 */
785 	mysession = pgrp->pg_session;
786 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
787 	    hispgrp->pg_session == mysession) {
788 		PGRP_LOCK(pgrp);
789 		if (entering)
790 			pgrp->pg_jobc++;
791 		else {
792 			--pgrp->pg_jobc;
793 			if (pgrp->pg_jobc == 0)
794 				orphanpg(pgrp);
795 		}
796 		PGRP_UNLOCK(pgrp);
797 	}
798 
799 	/*
800 	 * Check this process' children to see whether they qualify
801 	 * their process groups; if so, adjust counts for children's
802 	 * process groups.
803 	 */
804 	LIST_FOREACH(p, &p->p_children, p_sibling) {
805 		if ((hispgrp = p->p_pgrp) != pgrp &&
806 		    hispgrp->pg_session == mysession &&
807 		    p->p_state != PRS_ZOMBIE) {
808 			PGRP_LOCK(hispgrp);
809 			if (entering)
810 				hispgrp->pg_jobc++;
811 			else {
812 				--hispgrp->pg_jobc;
813 				if (hispgrp->pg_jobc == 0)
814 					orphanpg(hispgrp);
815 			}
816 			PGRP_UNLOCK(hispgrp);
817 		}
818 	}
819 }
820 
821 /*
822  * A process group has become orphaned;
823  * if there are any stopped processes in the group,
824  * hang-up all process in that group.
825  */
826 static void
827 orphanpg(pg)
828 	struct pgrp *pg;
829 {
830 	register struct proc *p;
831 
832 	PGRP_LOCK_ASSERT(pg, MA_OWNED);
833 
834 	mtx_lock_spin(&sched_lock);
835 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
836 		if (P_SHOULDSTOP(p)) {
837 			mtx_unlock_spin(&sched_lock);
838 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
839 				PROC_LOCK(p);
840 				psignal(p, SIGHUP);
841 				psignal(p, SIGCONT);
842 				PROC_UNLOCK(p);
843 			}
844 			return;
845 		}
846 	}
847 	mtx_unlock_spin(&sched_lock);
848 }
849 
850 #include "opt_ddb.h"
851 #ifdef DDB
852 #include <ddb/ddb.h>
853 
854 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
855 {
856 	register struct pgrp *pgrp;
857 	register struct proc *p;
858 	register int i;
859 
860 	for (i = 0; i <= pgrphash; i++) {
861 		if (!LIST_EMPTY(&pgrphashtbl[i])) {
862 			printf("\tindx %d\n", i);
863 			LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
864 				printf(
865 			"\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
866 				    (void *)pgrp, (long)pgrp->pg_id,
867 				    (void *)pgrp->pg_session,
868 				    pgrp->pg_session->s_count,
869 				    (void *)LIST_FIRST(&pgrp->pg_members));
870 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
871 					printf("\t\tpid %ld addr %p pgrp %p\n",
872 					    (long)p->p_pid, (void *)p,
873 					    (void *)p->p_pgrp);
874 				}
875 			}
876 		}
877 	}
878 }
879 #endif /* DDB */
880 
881 /*
882  * Fill in an kinfo_proc structure for the specified process.
883  * Must be called with the target process locked.
884  */
885 void
886 fill_kinfo_proc(p, kp)
887 	struct proc *p;
888 	struct kinfo_proc *kp;
889 {
890 	struct thread *td;
891 	struct kse *ke;
892 	struct ksegrp *kg;
893 	struct tty *tp;
894 	struct session *sp;
895 	struct timeval tv;
896 
897 	bzero(kp, sizeof(*kp));
898 
899 	kp->ki_structsize = sizeof(*kp);
900 	kp->ki_paddr = p;
901 	PROC_LOCK_ASSERT(p, MA_OWNED);
902 	kp->ki_addr =/* p->p_addr; */0; /* XXXKSE */
903 	kp->ki_args = p->p_args;
904 	kp->ki_textvp = p->p_textvp;
905 #ifdef KTRACE
906 	kp->ki_tracep = p->p_tracep;
907 	mtx_lock(&ktrace_mtx);
908 	kp->ki_traceflag = p->p_traceflag;
909 	mtx_unlock(&ktrace_mtx);
910 #endif
911 	kp->ki_fd = p->p_fd;
912 	kp->ki_vmspace = p->p_vmspace;
913 	if (p->p_ucred) {
914 		kp->ki_uid = p->p_ucred->cr_uid;
915 		kp->ki_ruid = p->p_ucred->cr_ruid;
916 		kp->ki_svuid = p->p_ucred->cr_svuid;
917 		/* XXX bde doesn't like KI_NGROUPS */
918 		kp->ki_ngroups = min(p->p_ucred->cr_ngroups, KI_NGROUPS);
919 		bcopy(p->p_ucred->cr_groups, kp->ki_groups,
920 		    kp->ki_ngroups * sizeof(gid_t));
921 		kp->ki_rgid = p->p_ucred->cr_rgid;
922 		kp->ki_svgid = p->p_ucred->cr_svgid;
923 	}
924 	if (p->p_procsig) {
925 		kp->ki_sigignore = p->p_procsig->ps_sigignore;
926 		kp->ki_sigcatch = p->p_procsig->ps_sigcatch;
927 	}
928 	mtx_lock_spin(&sched_lock);
929 	if (p->p_state != PRS_NEW &&
930 	    p->p_state != PRS_ZOMBIE &&
931 	    p->p_vmspace != NULL) {
932 		struct vmspace *vm = p->p_vmspace;
933 
934 		kp->ki_size = vm->vm_map.size;
935 		kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
936 		if (p->p_sflag & PS_INMEM)
937 			kp->ki_rssize += UAREA_PAGES;
938 		FOREACH_THREAD_IN_PROC(p, td) /* XXXKSE: thread swapout check */
939 			kp->ki_rssize += KSTACK_PAGES;
940 		kp->ki_swrss = vm->vm_swrss;
941 		kp->ki_tsize = vm->vm_tsize;
942 		kp->ki_dsize = vm->vm_dsize;
943 		kp->ki_ssize = vm->vm_ssize;
944 	}
945 	if ((p->p_sflag & PS_INMEM) && p->p_stats) {
946 		kp->ki_start = p->p_stats->p_start;
947 		kp->ki_rusage = p->p_stats->p_ru;
948 		kp->ki_childtime.tv_sec = p->p_stats->p_cru.ru_utime.tv_sec +
949 		    p->p_stats->p_cru.ru_stime.tv_sec;
950 		kp->ki_childtime.tv_usec = p->p_stats->p_cru.ru_utime.tv_usec +
951 		    p->p_stats->p_cru.ru_stime.tv_usec;
952 	}
953 	if (p->p_state != PRS_ZOMBIE) {
954 		td = FIRST_THREAD_IN_PROC(p);
955 		if (td == NULL) {
956 			/* XXXKSE: This should never happen. */
957 			printf("fill_kinfo_proc(): pid %d has no threads!\n",
958 			    p->p_pid);
959 			mtx_unlock_spin(&sched_lock);
960 			return;
961 		}
962 		if (!(p->p_flag & P_KSES)) {
963 			if (td->td_wmesg != NULL) {
964 				strlcpy(kp->ki_wmesg, td->td_wmesg,
965 				    sizeof(kp->ki_wmesg));
966 			}
967 			if (TD_ON_LOCK(td)) {
968 				kp->ki_kiflag |= KI_LOCKBLOCK;
969 				strlcpy(kp->ki_lockname, td->td_lockname,
970 				    sizeof(kp->ki_lockname));
971 			}
972 		}
973 
974 		if (p->p_state == PRS_NORMAL) { /*  XXXKSE very approximate */
975 			if (TD_ON_RUNQ(td) ||
976 			    TD_CAN_RUN(td) ||
977 			    TD_IS_RUNNING(td)) {
978 				kp->ki_stat = SRUN;
979 			} else if (P_SHOULDSTOP(p)) {
980 				kp->ki_stat = SSTOP;
981 			} else if (TD_IS_SLEEPING(td)) {
982 				kp->ki_stat = SSLEEP;
983 			} else if (TD_ON_LOCK(td)) {
984 				kp->ki_stat = SLOCK;
985 			} else {
986 				kp->ki_stat = SWAIT;
987 			}
988 		} else {
989 			kp->ki_stat = SIDL;
990 		}
991 
992 		kp->ki_sflag = p->p_sflag;
993 		kp->ki_swtime = p->p_swtime;
994 		kp->ki_pid = p->p_pid;
995 		/* vvv XXXKSE */
996 		if (!(p->p_flag & P_KSES)) {
997 			kg = td->td_ksegrp;
998 			ke = td->td_kse;
999 			KASSERT((ke != NULL), ("fill_kinfo_proc: Null KSE"));
1000 			bintime2timeval(&p->p_runtime, &tv);
1001 			kp->ki_runtime =
1002 			    tv.tv_sec * (u_int64_t)1000000 + tv.tv_usec;
1003 
1004 			/* things in the KSE GROUP */
1005 			kp->ki_estcpu = kg->kg_estcpu;
1006 			kp->ki_slptime = kg->kg_slptime;
1007 			kp->ki_pri.pri_user = kg->kg_user_pri;
1008 			kp->ki_pri.pri_class = kg->kg_pri_class;
1009 			kp->ki_nice = kg->kg_nice;
1010 
1011 			/* Things in the thread */
1012 			kp->ki_wchan = td->td_wchan;
1013 			kp->ki_pri.pri_level = td->td_priority;
1014 			kp->ki_pri.pri_native = td->td_base_pri;
1015 			kp->ki_lastcpu = td->td_lastcpu;
1016 			kp->ki_tdflags = td->td_flags;
1017 			kp->ki_pcb = td->td_pcb;
1018 			kp->ki_kstack = (void *)td->td_kstack;
1019 
1020 			/* Things in the kse */
1021 			kp->ki_rqindex = ke->ke_rqindex;
1022 			kp->ki_oncpu = ke->ke_oncpu;
1023 			kp->ki_pctcpu = ke->ke_pctcpu;
1024 		} else {
1025 			kp->ki_oncpu = -1;
1026 			kp->ki_lastcpu = -1;
1027 			kp->ki_tdflags = -1;
1028 			/* All the rest are 0 for now */
1029 		}
1030 		/* ^^^ XXXKSE */
1031 	} else {
1032 		kp->ki_stat = SZOMB;
1033 	}
1034 	mtx_unlock_spin(&sched_lock);
1035 	sp = NULL;
1036 	tp = NULL;
1037 	if (p->p_pgrp) {
1038 		kp->ki_pgid = p->p_pgrp->pg_id;
1039 		kp->ki_jobc = p->p_pgrp->pg_jobc;
1040 		sp = p->p_pgrp->pg_session;
1041 
1042 		if (sp != NULL) {
1043 			kp->ki_sid = sp->s_sid;
1044 			SESS_LOCK(sp);
1045 			strlcpy(kp->ki_login, sp->s_login,
1046 			    sizeof(kp->ki_login));
1047 			if (sp->s_ttyvp)
1048 				kp->ki_kiflag |= KI_CTTY;
1049 			if (SESS_LEADER(p))
1050 				kp->ki_kiflag |= KI_SLEADER;
1051 			tp = sp->s_ttyp;
1052 			SESS_UNLOCK(sp);
1053 		}
1054 	}
1055 	if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1056 		kp->ki_tdev = dev2udev(tp->t_dev);
1057 		kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1058 		if (tp->t_session)
1059 			kp->ki_tsid = tp->t_session->s_sid;
1060 	} else
1061 		kp->ki_tdev = NOUDEV;
1062 	if (p->p_comm[0] != '\0') {
1063 		strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1064 		strlcpy(kp->ki_ocomm, p->p_comm, sizeof(kp->ki_ocomm));
1065 	}
1066 	kp->ki_siglist = p->p_siglist;
1067 	kp->ki_sigmask = p->p_sigmask;
1068 	kp->ki_xstat = p->p_xstat;
1069 	kp->ki_acflag = p->p_acflag;
1070 	kp->ki_flag = p->p_flag;
1071 	/* If jailed(p->p_ucred), emulate the old P_JAILED flag. */
1072 	if (jailed(p->p_ucred))
1073 		kp->ki_flag |= P_JAILED;
1074 	kp->ki_lock = p->p_lock;
1075 	if (p->p_pptr)
1076 		kp->ki_ppid = p->p_pptr->p_pid;
1077 }
1078 
1079 /*
1080  * Locate a zombie process by number
1081  */
1082 struct proc *
1083 zpfind(pid_t pid)
1084 {
1085 	struct proc *p;
1086 
1087 	sx_slock(&allproc_lock);
1088 	LIST_FOREACH(p, &zombproc, p_list)
1089 		if (p->p_pid == pid) {
1090 			PROC_LOCK(p);
1091 			break;
1092 		}
1093 	sx_sunlock(&allproc_lock);
1094 	return (p);
1095 }
1096 
1097 
1098 /*
1099  * Must be called with the process locked and will return with it unlocked.
1100  */
1101 static int
1102 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb)
1103 {
1104 	struct kinfo_proc kinfo_proc;
1105 	int error;
1106 	struct proc *np;
1107 	pid_t pid = p->p_pid;
1108 
1109 	PROC_LOCK_ASSERT(p, MA_OWNED);
1110 	fill_kinfo_proc(p, &kinfo_proc);
1111 	PROC_UNLOCK(p);
1112 	error = SYSCTL_OUT(req, (caddr_t)&kinfo_proc, sizeof(kinfo_proc));
1113 	if (error)
1114 		return (error);
1115 	if (doingzomb)
1116 		np = zpfind(pid);
1117 	else {
1118 		if (pid == 0)
1119 			return (0);
1120 		np = pfind(pid);
1121 	}
1122 	if (np == NULL)
1123 		return EAGAIN;
1124 	if (np != p) {
1125 		PROC_UNLOCK(np);
1126 		return EAGAIN;
1127 	}
1128 	PROC_UNLOCK(np);
1129 	return (0);
1130 }
1131 
1132 static int
1133 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1134 {
1135 	int *name = (int*) arg1;
1136 	u_int namelen = arg2;
1137 	struct proc *p;
1138 	int doingzomb;
1139 	int error = 0;
1140 
1141 	if (oidp->oid_number == KERN_PROC_PID) {
1142 		if (namelen != 1)
1143 			return (EINVAL);
1144 		p = pfind((pid_t)name[0]);
1145 		if (!p)
1146 			return (0);
1147 		if (p_cansee(curthread, p)) {
1148 			PROC_UNLOCK(p);
1149 			return (0);
1150 		}
1151 		error = sysctl_out_proc(p, req, 0);
1152 		return (error);
1153 	}
1154 	if (oidp->oid_number == KERN_PROC_ALL && !namelen)
1155 		;
1156 	else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1)
1157 		;
1158 	else
1159 		return (EINVAL);
1160 
1161 	if (!req->oldptr) {
1162 		/* overestimate by 5 procs */
1163 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1164 		if (error)
1165 			return (error);
1166 	}
1167 	sysctl_wire_old_buffer(req, 0);
1168 	sx_slock(&allproc_lock);
1169 	for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1170 		if (!doingzomb)
1171 			p = LIST_FIRST(&allproc);
1172 		else
1173 			p = LIST_FIRST(&zombproc);
1174 		for (; p != 0; p = LIST_NEXT(p, p_list)) {
1175 			PROC_LOCK(p);
1176 			/*
1177 			 * Show a user only appropriate processes.
1178 			 */
1179 			if (p_cansee(curthread, p)) {
1180 				PROC_UNLOCK(p);
1181 				continue;
1182 			}
1183 			/*
1184 			 * Skip embryonic processes.
1185 			 */
1186 			if (p->p_state == PRS_NEW) {
1187 				PROC_UNLOCK(p);
1188 				continue;
1189 			}
1190 			/*
1191 			 * TODO - make more efficient (see notes below).
1192 			 * do by session.
1193 			 */
1194 			switch (oidp->oid_number) {
1195 
1196 			case KERN_PROC_PGRP:
1197 				/* could do this by traversing pgrp */
1198 				if (p->p_pgrp == NULL ||
1199 				    p->p_pgrp->pg_id != (pid_t)name[0]) {
1200 					PROC_UNLOCK(p);
1201 					continue;
1202 				}
1203 				break;
1204 
1205 			case KERN_PROC_TTY:
1206 				if ((p->p_flag & P_CONTROLT) == 0 ||
1207 				    p->p_session == NULL) {
1208 					PROC_UNLOCK(p);
1209 					continue;
1210 				}
1211 				SESS_LOCK(p->p_session);
1212 				if (p->p_session->s_ttyp == NULL ||
1213 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
1214 				    (udev_t)name[0]) {
1215 					SESS_UNLOCK(p->p_session);
1216 					PROC_UNLOCK(p);
1217 					continue;
1218 				}
1219 				SESS_UNLOCK(p->p_session);
1220 				break;
1221 
1222 			case KERN_PROC_UID:
1223 				if (p->p_ucred == NULL ||
1224 				    p->p_ucred->cr_uid != (uid_t)name[0]) {
1225 					PROC_UNLOCK(p);
1226 					continue;
1227 				}
1228 				break;
1229 
1230 			case KERN_PROC_RUID:
1231 				if (p->p_ucred == NULL ||
1232 				    p->p_ucred->cr_ruid != (uid_t)name[0]) {
1233 					PROC_UNLOCK(p);
1234 					continue;
1235 				}
1236 				break;
1237 			}
1238 
1239 			error = sysctl_out_proc(p, req, doingzomb);
1240 			if (error) {
1241 				sx_sunlock(&allproc_lock);
1242 				return (error);
1243 			}
1244 		}
1245 	}
1246 	sx_sunlock(&allproc_lock);
1247 	return (0);
1248 }
1249 
1250 struct pargs *
1251 pargs_alloc(int len)
1252 {
1253 	struct pargs *pa;
1254 
1255 	MALLOC(pa, struct pargs *, sizeof(struct pargs) + len, M_PARGS,
1256 		M_WAITOK);
1257 	pa->ar_ref = 1;
1258 	pa->ar_length = len;
1259 	return (pa);
1260 }
1261 
1262 void
1263 pargs_free(struct pargs *pa)
1264 {
1265 
1266 	FREE(pa, M_PARGS);
1267 }
1268 
1269 void
1270 pargs_hold(struct pargs *pa)
1271 {
1272 
1273 	if (pa == NULL)
1274 		return;
1275 	PARGS_LOCK(pa);
1276 	pa->ar_ref++;
1277 	PARGS_UNLOCK(pa);
1278 }
1279 
1280 void
1281 pargs_drop(struct pargs *pa)
1282 {
1283 
1284 	if (pa == NULL)
1285 		return;
1286 	PARGS_LOCK(pa);
1287 	if (--pa->ar_ref == 0) {
1288 		PARGS_UNLOCK(pa);
1289 		pargs_free(pa);
1290 	} else
1291 		PARGS_UNLOCK(pa);
1292 }
1293 
1294 /*
1295  * This sysctl allows a process to retrieve the argument list or process
1296  * title for another process without groping around in the address space
1297  * of the other process.  It also allow a process to set its own "process
1298  * title to a string of its own choice.
1299  */
1300 static int
1301 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1302 {
1303 	int *name = (int*) arg1;
1304 	u_int namelen = arg2;
1305 	struct proc *p;
1306 	struct pargs *pa;
1307 	int error = 0;
1308 
1309 	if (namelen != 1)
1310 		return (EINVAL);
1311 
1312 	p = pfind((pid_t)name[0]);
1313 	if (!p)
1314 		return (0);
1315 
1316 	if ((!ps_argsopen) && p_cansee(curthread, p)) {
1317 		PROC_UNLOCK(p);
1318 		return (0);
1319 	}
1320 	PROC_UNLOCK(p);
1321 
1322 	if (req->newptr && curproc != p)
1323 		return (EPERM);
1324 
1325 	PROC_LOCK(p);
1326 	pa = p->p_args;
1327 	pargs_hold(pa);
1328 	PROC_UNLOCK(p);
1329 	if (req->oldptr && pa != NULL) {
1330 		error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1331 	}
1332 	pargs_drop(pa);
1333 	if (req->newptr == NULL)
1334 		return (error);
1335 
1336 	PROC_LOCK(p);
1337 	pa = p->p_args;
1338 	p->p_args = NULL;
1339 	PROC_UNLOCK(p);
1340 	pargs_drop(pa);
1341 
1342 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1343 		return (error);
1344 
1345 	pa = pargs_alloc(req->newlen);
1346 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1347 	if (!error) {
1348 		PROC_LOCK(p);
1349 		p->p_args = pa;
1350 		PROC_UNLOCK(p);
1351 	} else
1352 		pargs_free(pa);
1353 	return (error);
1354 }
1355 
1356 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1357 
1358 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1359 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1360 
1361 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1362 	sysctl_kern_proc, "Process table");
1363 
1364 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1365 	sysctl_kern_proc, "Process table");
1366 
1367 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1368 	sysctl_kern_proc, "Process table");
1369 
1370 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1371 	sysctl_kern_proc, "Process table");
1372 
1373 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1374 	sysctl_kern_proc, "Process table");
1375 
1376 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1377 	sysctl_kern_proc_args, "Process argument list");
1378 
1379