xref: /dragonfly/sys/kern/kern_proc.c (revision 618537cf)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/vnode.h>
37 #include <sys/jail.h>
38 #include <sys/filedesc.h>
39 #include <sys/tty.h>
40 #include <sys/dsched.h>
41 #include <sys/signalvar.h>
42 #include <sys/spinlock.h>
43 #include <sys/random.h>
44 #include <sys/vnode.h>
45 #include <sys/exec.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 <machine/smp.h>
52 
53 #include <sys/refcount.h>
54 #include <sys/spinlock2.h>
55 
56 /*
57  * Hash table size must be a power of two and is not currently dynamically
58  * sized.  There is a trade-off between the linear scans which must iterate
59  * all HSIZE elements and the number of elements which might accumulate
60  * within each hash chain.
61  */
62 #define ALLPROC_HSIZE	256
63 #define ALLPROC_HMASK	(ALLPROC_HSIZE - 1)
64 #define ALLPROC_HASH(pid)	(pid & ALLPROC_HMASK)
65 #define PGRP_HASH(pid)	(pid & ALLPROC_HMASK)
66 #define SESS_HASH(pid)	(pid & ALLPROC_HMASK)
67 
68 /*
69  * pid_doms[] management, used to control how quickly a PID can be recycled.
70  * Must be a multiple of ALLPROC_HSIZE for the proc_makepid() inner loops.
71  *
72  * WARNING! PIDDOM_DELAY should not be defined > 20 or so unless you change
73  *	    the array from int8_t's to int16_t's.
74  */
75 #define PIDDOM_COUNT	10	/* 10 pids per domain - reduce array size */
76 #define PIDDOM_DELAY	10	/* min 10 seconds after exit before reuse */
77 #define PIDDOM_SCALE	10	/* (10,000*SCALE)/sec performance guarantee */
78 #define PIDSEL_DOMAINS	(PID_MAX * PIDDOM_SCALE / PIDDOM_COUNT /	\
79 			 ALLPROC_HSIZE * ALLPROC_HSIZE)
80 
81 /* Used by libkvm */
82 int allproc_hsize = ALLPROC_HSIZE;
83 
84 LIST_HEAD(pidhashhead, proc);
85 
86 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
87 MALLOC_DEFINE(M_SESSION, "session", "session header");
88 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
89 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
90 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
91 
92 int ps_showallprocs = 1;
93 static int ps_showallthreads = 1;
94 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
95     &ps_showallprocs, 0,
96     "Unprivileged processes can see processes with different UID/GID");
97 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
98     &ps_showallthreads, 0,
99     "Unprivileged processes can see kernel threads");
100 static u_int pid_domain_skips;
101 SYSCTL_UINT(_kern, OID_AUTO, pid_domain_skips, CTLFLAG_RW,
102     &pid_domain_skips, 0,
103     "Number of pid_doms[] skipped");
104 static u_int pid_inner_skips;
105 SYSCTL_UINT(_kern, OID_AUTO, pid_inner_skips, CTLFLAG_RW,
106     &pid_inner_skips, 0,
107     "Number of pid_doms[] skipped");
108 
109 static void orphanpg(struct pgrp *pg);
110 static void proc_makepid(struct proc *p, int random_offset);
111 
112 /*
113  * Process related lists (for proc_token, allproc, allpgrp, and allsess)
114  */
115 typedef struct procglob procglob_t;
116 
117 static procglob_t	procglob[ALLPROC_HSIZE];
118 
119 /*
120  * We try our best to avoid recycling a PID too quickly.  We do this by
121  * storing (uint8_t)time_second in the related pid domain on-reap and then
122  * using that to skip-over the domain on-allocate.
123  *
124  * This array has to be fairly large to support a high fork/exec rate.
125  * A ~100,000 entry array will support a 10-second reuse latency at
126  * 10,000 execs/second, worst case.  Best-case multiply by PIDDOM_COUNT
127  * (approximately 100,000 execs/second).
128  *
129  * Currently we allocate around a megabyte, making the worst-case fork
130  * rate around 100,000/second.
131  */
132 static uint8_t *pid_doms;
133 
134 /*
135  * Random component to nextpid generation.  We mix in a random factor to make
136  * it a little harder to predict.  We sanity check the modulus value to avoid
137  * doing it in critical paths.  Don't let it be too small or we pointlessly
138  * waste randomness entropy, and don't let it be impossibly large.  Using a
139  * modulus that is too big causes a LOT more process table scans and slows
140  * down fork processing as the pidchecked caching is defeated.
141  */
142 static int randompid = 0;
143 
144 static __inline
145 struct ucred *
146 pcredcache(struct ucred *cr, struct proc *p)
147 {
148 	if (cr != p->p_ucred) {
149 		if (cr)
150 			crfree(cr);
151 		spin_lock(&p->p_spin);
152 		if ((cr = p->p_ucred) != NULL)
153 			crhold(cr);
154 		spin_unlock(&p->p_spin);
155 	}
156 	return cr;
157 }
158 
159 /*
160  * No requirements.
161  */
162 static int
163 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
164 {
165 	int error, pid;
166 
167 	pid = randompid;
168 	error = sysctl_handle_int(oidp, &pid, 0, req);
169 	if (error || !req->newptr)
170 		return (error);
171 	if (pid < 0 || pid > PID_MAX - 100)     /* out of range */
172 		pid = PID_MAX - 100;
173 	else if (pid < 2)                       /* NOP */
174 		pid = 0;
175 	else if (pid < 100)                     /* Make it reasonable */
176 		pid = 100;
177 	randompid = pid;
178 	return (error);
179 }
180 
181 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
182 	    0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
183 
184 /*
185  * Initialize global process hashing structures.
186  *
187  * These functions are ONLY called from the low level boot code and do
188  * not lock their operations.
189  */
190 void
191 procinit(void)
192 {
193 	u_long i;
194 
195 	/*
196 	 * Allocate dynamically.  This array can be large (~1MB) so don't
197 	 * waste boot loader space.
198 	 */
199 	pid_doms = kmalloc(sizeof(pid_doms[0]) * PIDSEL_DOMAINS,
200 			   M_PROC, M_WAITOK | M_ZERO);
201 
202 	/*
203 	 * Avoid unnecessary stalls due to pid_doms[] values all being
204 	 * the same.  Make sure that the allocation of pid 1 and pid 2
205 	 * succeeds.
206 	 */
207 	for (i = 0; i < PIDSEL_DOMAINS; ++i)
208 		pid_doms[i] = (int8_t)i - (int8_t)(PIDDOM_DELAY + 1);
209 
210 	/*
211 	 * Other misc init.
212 	 */
213 	for (i = 0; i < ALLPROC_HSIZE; ++i) {
214 		procglob_t *prg = &procglob[i];
215 		LIST_INIT(&prg->allproc);
216 		LIST_INIT(&prg->allsess);
217 		LIST_INIT(&prg->allpgrp);
218 		lwkt_token_init(&prg->proc_token, "allproc");
219 	}
220 	uihashinit();
221 }
222 
223 void
224 procinsertinit(struct proc *p)
225 {
226 	LIST_INSERT_HEAD(&procglob[ALLPROC_HASH(p->p_pid)].allproc,
227 			 p, p_list);
228 }
229 
230 void
231 pgrpinsertinit(struct pgrp *pg)
232 {
233 	LIST_INSERT_HEAD(&procglob[ALLPROC_HASH(pg->pg_id)].allpgrp,
234 			 pg, pg_list);
235 }
236 
237 void
238 sessinsertinit(struct session *sess)
239 {
240 	LIST_INSERT_HEAD(&procglob[ALLPROC_HASH(sess->s_sid)].allsess,
241 			 sess, s_list);
242 }
243 
244 /*
245  * Process hold/release support functions.  Called via the PHOLD(),
246  * PRELE(), and PSTALL() macros.
247  *
248  * p->p_lock is a simple hold count with a waiting interlock.  No wakeup()
249  * is issued unless someone is actually waiting for the process.
250  *
251  * Most holds are short-term, allowing a process scan or other similar
252  * operation to access a proc structure without it getting ripped out from
253  * under us.  procfs and process-list sysctl ops also use the hold function
254  * interlocked with various p_flags to keep the vmspace intact when reading
255  * or writing a user process's address space.
256  *
257  * There are two situations where a hold count can be longer.  Exiting lwps
258  * hold the process until the lwp is reaped, and the parent will hold the
259  * child during vfork()/exec() sequences while the child is marked P_PPWAIT.
260  *
261  * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at
262  * various critical points in the fork/exec and exit paths before proceeding.
263  */
264 #define PLOCK_ZOMB	0x20000000
265 #define PLOCK_WAITING	0x40000000
266 #define PLOCK_MASK	0x1FFFFFFF
267 
268 void
269 pstall(struct proc *p, const char *wmesg, int count)
270 {
271 	int o;
272 	int n;
273 
274 	for (;;) {
275 		o = p->p_lock;
276 		cpu_ccfence();
277 		if ((o & PLOCK_MASK) <= count)
278 			break;
279 		n = o | PLOCK_WAITING;
280 		tsleep_interlock(&p->p_lock, 0);
281 
282 		/*
283 		 * If someone is trying to single-step the process during
284 		 * an exec or an exit they can deadlock us because procfs
285 		 * sleeps with the process held.
286 		 */
287 		if (p->p_stops) {
288 			if (p->p_flags & P_INEXEC) {
289 				wakeup(&p->p_stype);
290 			} else if (p->p_flags & P_POSTEXIT) {
291 				spin_lock(&p->p_spin);
292 				p->p_stops = 0;
293 				p->p_step = 0;
294 				spin_unlock(&p->p_spin);
295 				wakeup(&p->p_stype);
296 			}
297 		}
298 
299 		if (atomic_cmpset_int(&p->p_lock, o, n)) {
300 			tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
301 		}
302 	}
303 }
304 
305 void
306 phold(struct proc *p)
307 {
308 	atomic_add_int(&p->p_lock, 1);
309 }
310 
311 /*
312  * WARNING!  On last release (p) can become instantly invalid due to
313  *	     MP races.
314  */
315 void
316 prele(struct proc *p)
317 {
318 	int o;
319 	int n;
320 
321 	/*
322 	 * Fast path
323 	 */
324 	if (atomic_cmpset_int(&p->p_lock, 1, 0))
325 		return;
326 
327 	/*
328 	 * Slow path
329 	 */
330 	for (;;) {
331 		o = p->p_lock;
332 		KKASSERT((o & PLOCK_MASK) > 0);
333 		cpu_ccfence();
334 		n = (o - 1) & ~PLOCK_WAITING;
335 		if (atomic_cmpset_int(&p->p_lock, o, n)) {
336 			if (o & PLOCK_WAITING)
337 				wakeup(&p->p_lock);
338 			break;
339 		}
340 	}
341 }
342 
343 /*
344  * Hold and flag serialized for zombie reaping purposes.
345  *
346  * This function will fail if it has to block, returning non-zero with
347  * neither the flag set or the hold count bumped.  Note that we must block
348  * without holding a ref, meaning that the caller must ensure that (p)
349  * remains valid through some other interlock (typically on its parent
350  * process's p_token).
351  *
352  * Zero is returned on success.  The hold count will be incremented and
353  * the serialization flag acquired.  Note that serialization is only against
354  * other pholdzomb() calls, not against phold() calls.
355  */
356 int
357 pholdzomb(struct proc *p)
358 {
359 	int o;
360 	int n;
361 
362 	/*
363 	 * Fast path
364 	 */
365 	if (atomic_cmpset_int(&p->p_lock, 0, PLOCK_ZOMB | 1))
366 		return(0);
367 
368 	/*
369 	 * Slow path
370 	 */
371 	for (;;) {
372 		o = p->p_lock;
373 		cpu_ccfence();
374 		if ((o & PLOCK_ZOMB) == 0) {
375 			n = (o + 1) | PLOCK_ZOMB;
376 			if (atomic_cmpset_int(&p->p_lock, o, n))
377 				return(0);
378 		} else {
379 			KKASSERT((o & PLOCK_MASK) > 0);
380 			n = o | PLOCK_WAITING;
381 			tsleep_interlock(&p->p_lock, 0);
382 			if (atomic_cmpset_int(&p->p_lock, o, n)) {
383 				tsleep(&p->p_lock, PINTERLOCKED, "phldz", 0);
384 				/* (p) can be ripped out at this point */
385 				return(1);
386 			}
387 		}
388 	}
389 }
390 
391 /*
392  * Release PLOCK_ZOMB and the hold count, waking up any waiters.
393  *
394  * WARNING!  On last release (p) can become instantly invalid due to
395  *	     MP races.
396  */
397 void
398 prelezomb(struct proc *p)
399 {
400 	int o;
401 	int n;
402 
403 	/*
404 	 * Fast path
405 	 */
406 	if (atomic_cmpset_int(&p->p_lock, PLOCK_ZOMB | 1, 0))
407 		return;
408 
409 	/*
410 	 * Slow path
411 	 */
412 	KKASSERT(p->p_lock & PLOCK_ZOMB);
413 	for (;;) {
414 		o = p->p_lock;
415 		KKASSERT((o & PLOCK_MASK) > 0);
416 		cpu_ccfence();
417 		n = (o - 1) & ~(PLOCK_ZOMB | PLOCK_WAITING);
418 		if (atomic_cmpset_int(&p->p_lock, o, n)) {
419 			if (o & PLOCK_WAITING)
420 				wakeup(&p->p_lock);
421 			break;
422 		}
423 	}
424 }
425 
426 /*
427  * Is p an inferior of the current process?
428  *
429  * No requirements.
430  */
431 int
432 inferior(struct proc *p)
433 {
434 	struct proc *p2;
435 
436 	PHOLD(p);
437 	lwkt_gettoken_shared(&p->p_token);
438 	while (p != curproc) {
439 		if (p->p_pid == 0) {
440 			lwkt_reltoken(&p->p_token);
441 			return (0);
442 		}
443 		p2 = p->p_pptr;
444 		PHOLD(p2);
445 		lwkt_reltoken(&p->p_token);
446 		PRELE(p);
447 		lwkt_gettoken_shared(&p2->p_token);
448 		p = p2;
449 	}
450 	lwkt_reltoken(&p->p_token);
451 	PRELE(p);
452 
453 	return (1);
454 }
455 
456 /*
457  * Locate a process by number.  The returned process will be referenced and
458  * must be released with PRELE().
459  *
460  * No requirements.
461  */
462 struct proc *
463 pfind(pid_t pid)
464 {
465 	struct proc *p = curproc;
466 	procglob_t *prg;
467 	int n;
468 
469 	/*
470 	 * Shortcut the current process
471 	 */
472 	if (p && p->p_pid == pid) {
473 		PHOLD(p);
474 		return (p);
475 	}
476 
477 	/*
478 	 * Otherwise find it in the hash table.
479 	 */
480 	n = ALLPROC_HASH(pid);
481 	prg = &procglob[n];
482 
483 	lwkt_gettoken_shared(&prg->proc_token);
484 	LIST_FOREACH(p, &prg->allproc, p_list) {
485 		if (p->p_stat == SZOMB)
486 			continue;
487 		if (p->p_pid == pid) {
488 			PHOLD(p);
489 			lwkt_reltoken(&prg->proc_token);
490 			return (p);
491 		}
492 	}
493 	lwkt_reltoken(&prg->proc_token);
494 
495 	return (NULL);
496 }
497 
498 /*
499  * Locate a process by number.  The returned process is NOT referenced.
500  * The result will not be stable and is typically only used to validate
501  * against a process that the caller has in-hand.
502  *
503  * No requirements.
504  */
505 struct proc *
506 pfindn(pid_t pid)
507 {
508 	struct proc *p = curproc;
509 	procglob_t *prg;
510 	int n;
511 
512 	/*
513 	 * Shortcut the current process
514 	 */
515 	if (p && p->p_pid == pid)
516 		return (p);
517 
518 	/*
519 	 * Otherwise find it in the hash table.
520 	 */
521 	n = ALLPROC_HASH(pid);
522 	prg = &procglob[n];
523 
524 	lwkt_gettoken_shared(&prg->proc_token);
525 	LIST_FOREACH(p, &prg->allproc, p_list) {
526 		if (p->p_stat == SZOMB)
527 			continue;
528 		if (p->p_pid == pid) {
529 			lwkt_reltoken(&prg->proc_token);
530 			return (p);
531 		}
532 	}
533 	lwkt_reltoken(&prg->proc_token);
534 
535 	return (NULL);
536 }
537 
538 /*
539  * Locate a process on the zombie list.  Return a process or NULL.
540  * The returned process will be referenced and the caller must release
541  * it with PRELE().
542  *
543  * No other requirements.
544  */
545 struct proc *
546 zpfind(pid_t pid)
547 {
548 	struct proc *p = curproc;
549 	procglob_t *prg;
550 	int n;
551 
552 	/*
553 	 * Shortcut the current process
554 	 */
555 	if (p && p->p_pid == pid) {
556 		PHOLD(p);
557 		return (p);
558 	}
559 
560 	/*
561 	 * Otherwise find it in the hash table.
562 	 */
563 	n = ALLPROC_HASH(pid);
564 	prg = &procglob[n];
565 
566 	lwkt_gettoken_shared(&prg->proc_token);
567 	LIST_FOREACH(p, &prg->allproc, p_list) {
568 		if (p->p_stat != SZOMB)
569 			continue;
570 		if (p->p_pid == pid) {
571 			PHOLD(p);
572 			lwkt_reltoken(&prg->proc_token);
573 			return (p);
574 		}
575 	}
576 	lwkt_reltoken(&prg->proc_token);
577 
578 	return (NULL);
579 }
580 
581 
582 void
583 pgref(struct pgrp *pgrp)
584 {
585 	refcount_acquire(&pgrp->pg_refs);
586 }
587 
588 void
589 pgrel(struct pgrp *pgrp)
590 {
591 	procglob_t *prg;
592 	int count;
593 	int n;
594 
595 	n = PGRP_HASH(pgrp->pg_id);
596 	prg = &procglob[n];
597 
598 	for (;;) {
599 		count = pgrp->pg_refs;
600 		cpu_ccfence();
601 		KKASSERT(count > 0);
602 		if (count == 1) {
603 			lwkt_gettoken(&prg->proc_token);
604 			if (atomic_cmpset_int(&pgrp->pg_refs, 1, 0))
605 				break;
606 			lwkt_reltoken(&prg->proc_token);
607 			/* retry */
608 		} else {
609 			if (atomic_cmpset_int(&pgrp->pg_refs, count, count - 1))
610 				return;
611 			/* retry */
612 		}
613 	}
614 
615 	/*
616 	 * Successful 1->0 transition, pghash_spin is held.
617 	 */
618 	LIST_REMOVE(pgrp, pg_list);
619 	if (pid_doms[pgrp->pg_id % PIDSEL_DOMAINS] != (uint8_t)time_second)
620 		pid_doms[pgrp->pg_id % PIDSEL_DOMAINS] = (uint8_t)time_second;
621 
622 	/*
623 	 * Reset any sigio structures pointing to us as a result of
624 	 * F_SETOWN with our pgid.
625 	 */
626 	funsetownlst(&pgrp->pg_sigiolst);
627 
628 	if (pgrp->pg_session->s_ttyp != NULL &&
629 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
630 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
631 	}
632 	lwkt_reltoken(&prg->proc_token);
633 
634 	sess_rele(pgrp->pg_session);
635 	kfree(pgrp, M_PGRP);
636 }
637 
638 /*
639  * Locate a process group by number.  The returned process group will be
640  * referenced w/pgref() and must be released with pgrel() (or assigned
641  * somewhere if you wish to keep the reference).
642  *
643  * No requirements.
644  */
645 struct pgrp *
646 pgfind(pid_t pgid)
647 {
648 	struct pgrp *pgrp;
649 	procglob_t *prg;
650 	int n;
651 
652 	n = PGRP_HASH(pgid);
653 	prg = &procglob[n];
654 	lwkt_gettoken_shared(&prg->proc_token);
655 
656 	LIST_FOREACH(pgrp, &prg->allpgrp, pg_list) {
657 		if (pgrp->pg_id == pgid) {
658 			refcount_acquire(&pgrp->pg_refs);
659 			lwkt_reltoken(&prg->proc_token);
660 			return (pgrp);
661 		}
662 	}
663 	lwkt_reltoken(&prg->proc_token);
664 	return (NULL);
665 }
666 
667 /*
668  * Move p to a new or existing process group (and session)
669  *
670  * No requirements.
671  */
672 int
673 enterpgrp(struct proc *p, pid_t pgid, int mksess)
674 {
675 	struct pgrp *pgrp;
676 	struct pgrp *opgrp;
677 	int error;
678 
679 	pgrp = pgfind(pgid);
680 
681 	KASSERT(pgrp == NULL || !mksess,
682 		("enterpgrp: setsid into non-empty pgrp"));
683 	KASSERT(!SESS_LEADER(p),
684 		("enterpgrp: session leader attempted setpgrp"));
685 
686 	if (pgrp == NULL) {
687 		pid_t savepid = p->p_pid;
688 		struct proc *np;
689 		procglob_t *prg;
690 		int n;
691 
692 		/*
693 		 * new process group
694 		 */
695 		KASSERT(p->p_pid == pgid,
696 			("enterpgrp: new pgrp and pid != pgid"));
697 		pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
698 		pgrp->pg_id = pgid;
699 		LIST_INIT(&pgrp->pg_members);
700 		pgrp->pg_jobc = 0;
701 		SLIST_INIT(&pgrp->pg_sigiolst);
702 		lwkt_token_init(&pgrp->pg_token, "pgrp_token");
703 		refcount_init(&pgrp->pg_refs, 1);
704 		lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
705 
706 		n = PGRP_HASH(pgid);
707 		prg = &procglob[n];
708 
709 		if ((np = pfindn(savepid)) == NULL || np != p) {
710 			lwkt_reltoken(&prg->proc_token);
711 			error = ESRCH;
712 			kfree(pgrp, M_PGRP);
713 			goto fatal;
714 		}
715 
716 		lwkt_gettoken(&prg->proc_token);
717 		if (mksess) {
718 			struct session *sess;
719 
720 			/*
721 			 * new session
722 			 */
723 			sess = kmalloc(sizeof(struct session), M_SESSION,
724 				       M_WAITOK | M_ZERO);
725 			lwkt_gettoken(&p->p_token);
726 			sess->s_leader = p;
727 			sess->s_sid = p->p_pid;
728 			sess->s_count = 1;
729 			sess->s_ttyvp = NULL;
730 			sess->s_ttyp = NULL;
731 			bcopy(p->p_session->s_login, sess->s_login,
732 			      sizeof(sess->s_login));
733 			pgrp->pg_session = sess;
734 			KASSERT(p == curproc,
735 				("enterpgrp: mksession and p != curproc"));
736 			p->p_flags &= ~P_CONTROLT;
737 			LIST_INSERT_HEAD(&prg->allsess, sess, s_list);
738 			lwkt_reltoken(&p->p_token);
739 		} else {
740 			lwkt_gettoken(&p->p_token);
741 			pgrp->pg_session = p->p_session;
742 			sess_hold(pgrp->pg_session);
743 			lwkt_reltoken(&p->p_token);
744 		}
745 		LIST_INSERT_HEAD(&prg->allpgrp, pgrp, pg_list);
746 
747 		lwkt_reltoken(&prg->proc_token);
748 	} else if (pgrp == p->p_pgrp) {
749 		pgrel(pgrp);
750 		goto done;
751 	} /* else pgfind() referenced the pgrp */
752 
753 	lwkt_gettoken(&pgrp->pg_token);
754 	lwkt_gettoken(&p->p_token);
755 
756 	/*
757 	 * Replace p->p_pgrp, handling any races that occur.
758 	 */
759 	while ((opgrp = p->p_pgrp) != NULL) {
760 		pgref(opgrp);
761 		lwkt_gettoken(&opgrp->pg_token);
762 		if (opgrp != p->p_pgrp) {
763 			lwkt_reltoken(&opgrp->pg_token);
764 			pgrel(opgrp);
765 			continue;
766 		}
767 		LIST_REMOVE(p, p_pglist);
768 		break;
769 	}
770 	p->p_pgrp = pgrp;
771 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
772 
773 	/*
774 	 * Adjust eligibility of affected pgrps to participate in job control.
775 	 * Increment eligibility counts before decrementing, otherwise we
776 	 * could reach 0 spuriously during the first call.
777 	 */
778 	fixjobc(p, pgrp, 1);
779 	if (opgrp) {
780 		fixjobc(p, opgrp, 0);
781 		lwkt_reltoken(&opgrp->pg_token);
782 		pgrel(opgrp);	/* manual pgref */
783 		pgrel(opgrp);	/* p->p_pgrp ref */
784 	}
785 	lwkt_reltoken(&p->p_token);
786 	lwkt_reltoken(&pgrp->pg_token);
787 done:
788 	error = 0;
789 fatal:
790 	return (error);
791 }
792 
793 /*
794  * Remove process from process group
795  *
796  * No requirements.
797  */
798 int
799 leavepgrp(struct proc *p)
800 {
801 	struct pgrp *pg = p->p_pgrp;
802 
803 	lwkt_gettoken(&p->p_token);
804 	while ((pg = p->p_pgrp) != NULL) {
805 		pgref(pg);
806 		lwkt_gettoken(&pg->pg_token);
807 		if (p->p_pgrp != pg) {
808 			lwkt_reltoken(&pg->pg_token);
809 			pgrel(pg);
810 			continue;
811 		}
812 		p->p_pgrp = NULL;
813 		LIST_REMOVE(p, p_pglist);
814 		lwkt_reltoken(&pg->pg_token);
815 		pgrel(pg);	/* manual pgref */
816 		pgrel(pg);	/* p->p_pgrp ref */
817 		break;
818 	}
819 	lwkt_reltoken(&p->p_token);
820 
821 	return (0);
822 }
823 
824 /*
825  * Adjust the ref count on a session structure.  When the ref count falls to
826  * zero the tty is disassociated from the session and the session structure
827  * is freed.  Note that tty assocation is not itself ref-counted.
828  *
829  * No requirements.
830  */
831 void
832 sess_hold(struct session *sp)
833 {
834 	atomic_add_int(&sp->s_count, 1);
835 }
836 
837 /*
838  * No requirements.
839  */
840 void
841 sess_rele(struct session *sess)
842 {
843 	procglob_t *prg;
844 	struct tty *tp;
845 	int count;
846 	int n;
847 
848 	n = SESS_HASH(sess->s_sid);
849 	prg = &procglob[n];
850 
851 	for (;;) {
852 		count = sess->s_count;
853 		cpu_ccfence();
854 		KKASSERT(count > 0);
855 		if (count == 1) {
856 			lwkt_gettoken(&tty_token);
857 			lwkt_gettoken(&prg->proc_token);
858 			if (atomic_cmpset_int(&sess->s_count, 1, 0))
859 				break;
860 			lwkt_reltoken(&prg->proc_token);
861 			lwkt_reltoken(&tty_token);
862 			/* retry */
863 		} else {
864 			if (atomic_cmpset_int(&sess->s_count, count, count - 1))
865 				return;
866 			/* retry */
867 		}
868 	}
869 
870 	/*
871 	 * Successful 1->0 transition and tty_token is held.
872 	 */
873 	LIST_REMOVE(sess, s_list);
874 	if (pid_doms[sess->s_sid % PIDSEL_DOMAINS] != (uint8_t)time_second)
875 		pid_doms[sess->s_sid % PIDSEL_DOMAINS] = (uint8_t)time_second;
876 
877 	if (sess->s_ttyp && sess->s_ttyp->t_session) {
878 #ifdef TTY_DO_FULL_CLOSE
879 		/* FULL CLOSE, see ttyclearsession() */
880 		KKASSERT(sess->s_ttyp->t_session == sess);
881 		sess->s_ttyp->t_session = NULL;
882 #else
883 		/* HALF CLOSE, see ttyclearsession() */
884 		if (sess->s_ttyp->t_session == sess)
885 			sess->s_ttyp->t_session = NULL;
886 #endif
887 	}
888 	if ((tp = sess->s_ttyp) != NULL) {
889 		sess->s_ttyp = NULL;
890 		ttyunhold(tp);
891 	}
892 	lwkt_reltoken(&prg->proc_token);
893 	lwkt_reltoken(&tty_token);
894 
895 	kfree(sess, M_SESSION);
896 }
897 
898 /*
899  * Adjust pgrp jobc counters when specified process changes process group.
900  * We count the number of processes in each process group that "qualify"
901  * the group for terminal job control (those with a parent in a different
902  * process group of the same session).  If that count reaches zero, the
903  * process group becomes orphaned.  Check both the specified process'
904  * process group and that of its children.
905  * entering == 0 => p is leaving specified group.
906  * entering == 1 => p is entering specified group.
907  *
908  * No requirements.
909  */
910 void
911 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
912 {
913 	struct pgrp *hispgrp;
914 	struct session *mysession;
915 	struct proc *np;
916 
917 	/*
918 	 * Check p's parent to see whether p qualifies its own process
919 	 * group; if so, adjust count for p's process group.
920 	 */
921 	lwkt_gettoken(&p->p_token);	/* p_children scan */
922 	lwkt_gettoken(&pgrp->pg_token);
923 
924 	mysession = pgrp->pg_session;
925 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
926 	    hispgrp->pg_session == mysession) {
927 		if (entering)
928 			pgrp->pg_jobc++;
929 		else if (--pgrp->pg_jobc == 0)
930 			orphanpg(pgrp);
931 	}
932 
933 	/*
934 	 * Check this process' children to see whether they qualify
935 	 * their process groups; if so, adjust counts for children's
936 	 * process groups.
937 	 */
938 	LIST_FOREACH(np, &p->p_children, p_sibling) {
939 		PHOLD(np);
940 		lwkt_gettoken(&np->p_token);
941 		if ((hispgrp = np->p_pgrp) != pgrp &&
942 		    hispgrp->pg_session == mysession &&
943 		    np->p_stat != SZOMB) {
944 			pgref(hispgrp);
945 			lwkt_gettoken(&hispgrp->pg_token);
946 			if (entering)
947 				hispgrp->pg_jobc++;
948 			else if (--hispgrp->pg_jobc == 0)
949 				orphanpg(hispgrp);
950 			lwkt_reltoken(&hispgrp->pg_token);
951 			pgrel(hispgrp);
952 		}
953 		lwkt_reltoken(&np->p_token);
954 		PRELE(np);
955 	}
956 	KKASSERT(pgrp->pg_refs > 0);
957 	lwkt_reltoken(&pgrp->pg_token);
958 	lwkt_reltoken(&p->p_token);
959 }
960 
961 /*
962  * A process group has become orphaned;
963  * if there are any stopped processes in the group,
964  * hang-up all process in that group.
965  *
966  * The caller must hold pg_token.
967  */
968 static void
969 orphanpg(struct pgrp *pg)
970 {
971 	struct proc *p;
972 
973 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
974 		if (p->p_stat == SSTOP) {
975 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
976 				ksignal(p, SIGHUP);
977 				ksignal(p, SIGCONT);
978 			}
979 			return;
980 		}
981 	}
982 }
983 
984 /*
985  * Add a new process to the allproc list and the PID hash.  This
986  * also assigns a pid to the new process.
987  *
988  * No requirements.
989  */
990 void
991 proc_add_allproc(struct proc *p)
992 {
993 	int random_offset;
994 
995 	if ((random_offset = randompid) != 0) {
996 		read_random(&random_offset, sizeof(random_offset));
997 		random_offset = (random_offset & 0x7FFFFFFF) % randompid;
998 	}
999 	proc_makepid(p, random_offset);
1000 }
1001 
1002 /*
1003  * Calculate a new process pid.  This function is integrated into
1004  * proc_add_allproc() to guarentee that the new pid is not reused before
1005  * the new process can be added to the allproc list.
1006  *
1007  * p_pid is assigned and the process is added to the allproc hash table
1008  *
1009  * WARNING! We need to allocate PIDs sequentially during early boot.
1010  *	    In particular, init needs to have a pid of 1.
1011  */
1012 static
1013 void
1014 proc_makepid(struct proc *p, int random_offset)
1015 {
1016 	static pid_t nextpid = 1;	/* heuristic, allowed to race */
1017 	procglob_t *prg;
1018 	struct pgrp *pg;
1019 	struct proc *ps;
1020 	struct session *sess;
1021 	pid_t base;
1022 	int8_t delta8;
1023 	int retries;
1024 	int n;
1025 
1026 	/*
1027 	 * Select the next pid base candidate.
1028 	 *
1029 	 * Check cyclement, do not allow a pid < 100.
1030 	 */
1031 	retries = 0;
1032 retry:
1033 	base = atomic_fetchadd_int(&nextpid, 1) + random_offset;
1034 	if (base <= 0 || base >= PID_MAX) {
1035 		base = base % PID_MAX;
1036 		if (base < 0)
1037 			base = 100;
1038 		if (base < 100)
1039 			base += 100;
1040 		nextpid = base;		/* reset (SMP race ok) */
1041 	}
1042 
1043 	/*
1044 	 * Do not allow a base pid to be selected from a domain that has
1045 	 * recently seen a pid/pgid/sessid reap.  Sleep a little if we looped
1046 	 * through all available domains.
1047 	 *
1048 	 * WARNING: We want the early pids to be allocated linearly,
1049 	 *	    particularly pid 1 and pid 2.
1050 	 */
1051 	if (++retries >= PIDSEL_DOMAINS)
1052 		tsleep(&nextpid, 0, "makepid", 1);
1053 	if (base >= 100) {
1054 		delta8 = (int8_t)time_second -
1055 			 (int8_t)pid_doms[base % PIDSEL_DOMAINS];
1056 		if (delta8 >= 0 && delta8 <= PIDDOM_DELAY) {
1057 			++pid_domain_skips;
1058 			goto retry;
1059 		}
1060 	}
1061 
1062 	/*
1063 	 * Calculate a hash index and find an unused process id within
1064 	 * the table, looping if we cannot find one.
1065 	 *
1066 	 * The inner loop increments by ALLPROC_HSIZE which keeps the
1067 	 * PID at the same pid_doms[] index as well as the same hash index.
1068 	 */
1069 	n = ALLPROC_HASH(base);
1070 	prg = &procglob[n];
1071 	lwkt_gettoken(&prg->proc_token);
1072 
1073 restart1:
1074 	LIST_FOREACH(ps, &prg->allproc, p_list) {
1075 		if (ps->p_pid == base) {
1076 			base += ALLPROC_HSIZE;
1077 			if (base >= PID_MAX) {
1078 				lwkt_reltoken(&prg->proc_token);
1079 				goto retry;
1080 			}
1081 			++pid_inner_skips;
1082 			goto restart1;
1083 		}
1084 	}
1085 	LIST_FOREACH(pg, &prg->allpgrp, pg_list) {
1086 		if (pg->pg_id == base) {
1087 			base += ALLPROC_HSIZE;
1088 			if (base >= PID_MAX) {
1089 				lwkt_reltoken(&prg->proc_token);
1090 				goto retry;
1091 			}
1092 			++pid_inner_skips;
1093 			goto restart1;
1094 		}
1095 	}
1096 	LIST_FOREACH(sess, &prg->allsess, s_list) {
1097 		if (sess->s_sid == base) {
1098 			base += ALLPROC_HSIZE;
1099 			if (base >= PID_MAX) {
1100 				lwkt_reltoken(&prg->proc_token);
1101 				goto retry;
1102 			}
1103 			++pid_inner_skips;
1104 			goto restart1;
1105 		}
1106 	}
1107 
1108 	/*
1109 	 * Assign the pid and insert the process.
1110 	 */
1111 	p->p_pid = base;
1112 	LIST_INSERT_HEAD(&prg->allproc, p, p_list);
1113 	lwkt_reltoken(&prg->proc_token);
1114 }
1115 
1116 /*
1117  * Called from exit1 to place the process into a zombie state.
1118  * The process is removed from the pid hash and p_stat is set
1119  * to SZOMB.  Normal pfind[n]() calls will not find it any more.
1120  *
1121  * Caller must hold p->p_token.  We are required to wait until p_lock
1122  * becomes zero before we can manipulate the list, allowing allproc
1123  * scans to guarantee consistency during a list scan.
1124  */
1125 void
1126 proc_move_allproc_zombie(struct proc *p)
1127 {
1128 	procglob_t *prg;
1129 	int n;
1130 
1131 	n = ALLPROC_HASH(p->p_pid);
1132 	prg = &procglob[n];
1133 	PSTALL(p, "reap1", 0);
1134 	lwkt_gettoken(&prg->proc_token);
1135 
1136 	PSTALL(p, "reap1a", 0);
1137 	p->p_stat = SZOMB;
1138 
1139 	lwkt_reltoken(&prg->proc_token);
1140 	dsched_exit_proc(p);
1141 }
1142 
1143 /*
1144  * This routine is called from kern_wait() and will remove the process
1145  * from the zombie list and the sibling list.  This routine will block
1146  * if someone has a lock on the proces (p_lock).
1147  *
1148  * Caller must hold p->p_token.  We are required to wait until p_lock
1149  * becomes zero before we can manipulate the list, allowing allproc
1150  * scans to guarantee consistency during a list scan.
1151  */
1152 void
1153 proc_remove_zombie(struct proc *p)
1154 {
1155 	procglob_t *prg;
1156 	int n;
1157 
1158 	n = ALLPROC_HASH(p->p_pid);
1159 	prg = &procglob[n];
1160 
1161 	PSTALL(p, "reap2", 0);
1162 	lwkt_gettoken(&prg->proc_token);
1163 	PSTALL(p, "reap2a", 0);
1164 	LIST_REMOVE(p, p_list);		/* from remove master list */
1165 	LIST_REMOVE(p, p_sibling);	/* and from sibling list */
1166 	p->p_pptr = NULL;
1167 	if (pid_doms[p->p_pid % PIDSEL_DOMAINS] != (uint8_t)time_second)
1168 		pid_doms[p->p_pid % PIDSEL_DOMAINS] = (uint8_t)time_second;
1169 	lwkt_reltoken(&prg->proc_token);
1170 }
1171 
1172 /*
1173  * Handle various requirements prior to returning to usermode.  Called from
1174  * platform trap and system call code.
1175  */
1176 void
1177 lwpuserret(struct lwp *lp)
1178 {
1179 	struct proc *p = lp->lwp_proc;
1180 
1181 	if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1182 		atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1183 		allocvnode_gc();
1184 	}
1185 	if (lp->lwp_mpflags & LWP_MP_WEXIT) {
1186 		lwkt_gettoken(&p->p_token);
1187 		lwp_exit(0, NULL);
1188 		lwkt_reltoken(&p->p_token);     /* NOT REACHED */
1189 	}
1190 }
1191 
1192 /*
1193  * Kernel threads run from user processes can also accumulate deferred
1194  * actions which need to be acted upon.  Callers include:
1195  *
1196  * nfsd		- Can allocate lots of vnodes
1197  */
1198 void
1199 lwpkthreaddeferred(void)
1200 {
1201 	struct lwp *lp = curthread->td_lwp;
1202 
1203 	if (lp) {
1204 		if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1205 			atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1206 			allocvnode_gc();
1207 		}
1208 	}
1209 }
1210 
1211 void
1212 proc_usermap(struct proc *p, int invfork)
1213 {
1214 	struct sys_upmap *upmap;
1215 
1216 	lwkt_gettoken(&p->p_token);
1217 	upmap = kmalloc(roundup2(sizeof(*upmap), PAGE_SIZE), M_PROC,
1218 			M_WAITOK | M_ZERO);
1219 	if (p->p_upmap == NULL) {
1220 		upmap->header[0].type = UKPTYPE_VERSION;
1221 		upmap->header[0].offset = offsetof(struct sys_upmap, version);
1222 		upmap->header[1].type = UPTYPE_RUNTICKS;
1223 		upmap->header[1].offset = offsetof(struct sys_upmap, runticks);
1224 		upmap->header[2].type = UPTYPE_FORKID;
1225 		upmap->header[2].offset = offsetof(struct sys_upmap, forkid);
1226 		upmap->header[3].type = UPTYPE_PID;
1227 		upmap->header[3].offset = offsetof(struct sys_upmap, pid);
1228 		upmap->header[4].type = UPTYPE_PROC_TITLE;
1229 		upmap->header[4].offset = offsetof(struct sys_upmap,proc_title);
1230 		upmap->header[5].type = UPTYPE_INVFORK;
1231 		upmap->header[5].offset = offsetof(struct sys_upmap, invfork);
1232 
1233 		upmap->version = UPMAP_VERSION;
1234 		upmap->pid = p->p_pid;
1235 		upmap->forkid = p->p_forkid;
1236 		upmap->invfork = invfork;
1237 		p->p_upmap = upmap;
1238 	} else {
1239 		kfree(upmap, M_PROC);
1240 	}
1241 	lwkt_reltoken(&p->p_token);
1242 }
1243 
1244 void
1245 proc_userunmap(struct proc *p)
1246 {
1247 	struct sys_upmap *upmap;
1248 
1249 	lwkt_gettoken(&p->p_token);
1250 	if ((upmap = p->p_upmap) != NULL) {
1251 		p->p_upmap = NULL;
1252 		kfree(upmap, M_PROC);
1253 	}
1254 	lwkt_reltoken(&p->p_token);
1255 }
1256 
1257 /*
1258  * Scan all processes on the allproc list.  The process is automatically
1259  * held for the callback.  A return value of -1 terminates the loop.
1260  * Zombie procs are skipped.
1261  *
1262  * The callback is made with the process held and proc_token held.
1263  *
1264  * We limit the scan to the number of processes as-of the start of
1265  * the scan so as not to get caught up in an endless loop if new processes
1266  * are created more quickly than we can scan the old ones.  Add a little
1267  * slop to try to catch edge cases since nprocs can race.
1268  *
1269  * No requirements.
1270  */
1271 void
1272 allproc_scan(int (*callback)(struct proc *, void *), void *data, int segmented)
1273 {
1274 	int limit = nprocs + ncpus;
1275 	struct proc *p;
1276 	int ns;
1277 	int ne;
1278 	int r;
1279 	int n;
1280 
1281 	if (segmented) {
1282 		int id = mycpu->gd_cpuid;
1283 		ns = id * ALLPROC_HSIZE / ncpus;
1284 		ne = (id + 1) * ALLPROC_HSIZE / ncpus;
1285 	} else {
1286 		ns = 0;
1287 		ne = ALLPROC_HSIZE;
1288 	}
1289 
1290 	/*
1291 	 * prg->proc_token protects the allproc list and PHOLD() prevents the
1292 	 * process from being removed from the allproc list or the zombproc
1293 	 * list.
1294 	 */
1295 	for (n = ns; n < ne; ++n) {
1296 		procglob_t *prg = &procglob[n];
1297 		if (LIST_FIRST(&prg->allproc) == NULL)
1298 			continue;
1299 		lwkt_gettoken(&prg->proc_token);
1300 		LIST_FOREACH(p, &prg->allproc, p_list) {
1301 			if (p->p_stat == SZOMB)
1302 				continue;
1303 			PHOLD(p);
1304 			r = callback(p, data);
1305 			PRELE(p);
1306 			if (r < 0)
1307 				break;
1308 			if (--limit < 0)
1309 				break;
1310 		}
1311 		lwkt_reltoken(&prg->proc_token);
1312 
1313 		/*
1314 		 * Check if asked to stop early
1315 		 */
1316 		if (p)
1317 			break;
1318 	}
1319 }
1320 
1321 /*
1322  * Scan all lwps of processes on the allproc list.  The lwp is automatically
1323  * held for the callback.  A return value of -1 terminates the loop.
1324  *
1325  * The callback is made with the proces and lwp both held, and proc_token held.
1326  *
1327  * No requirements.
1328  */
1329 void
1330 alllwp_scan(int (*callback)(struct lwp *, void *), void *data, int segmented)
1331 {
1332 	struct proc *p;
1333 	struct lwp *lp;
1334 	int ns;
1335 	int ne;
1336 	int r = 0;
1337 	int n;
1338 
1339 	if (segmented) {
1340 		int id = mycpu->gd_cpuid;
1341 		ns = id * ALLPROC_HSIZE / ncpus;
1342 		ne = (id + 1) * ALLPROC_HSIZE / ncpus;
1343 	} else {
1344 		ns = 0;
1345 		ne = ALLPROC_HSIZE;
1346 	}
1347 
1348 	for (n = ns; n < ne; ++n) {
1349 		procglob_t *prg = &procglob[n];
1350 
1351 		if (LIST_FIRST(&prg->allproc) == NULL)
1352 			continue;
1353 		lwkt_gettoken(&prg->proc_token);
1354 		LIST_FOREACH(p, &prg->allproc, p_list) {
1355 			if (p->p_stat == SZOMB)
1356 				continue;
1357 			PHOLD(p);
1358 			lwkt_gettoken(&p->p_token);
1359 			FOREACH_LWP_IN_PROC(lp, p) {
1360 				LWPHOLD(lp);
1361 				r = callback(lp, data);
1362 				LWPRELE(lp);
1363 			}
1364 			lwkt_reltoken(&p->p_token);
1365 			PRELE(p);
1366 			if (r < 0)
1367 				break;
1368 		}
1369 		lwkt_reltoken(&prg->proc_token);
1370 
1371 		/*
1372 		 * Asked to exit early
1373 		 */
1374 		if (p)
1375 			break;
1376 	}
1377 }
1378 
1379 /*
1380  * Scan all processes on the zombproc list.  The process is automatically
1381  * held for the callback.  A return value of -1 terminates the loop.
1382  *
1383  * No requirements.
1384  * The callback is made with the proces held and proc_token held.
1385  */
1386 void
1387 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
1388 {
1389 	struct proc *p;
1390 	int r;
1391 	int n;
1392 
1393 	/*
1394 	 * prg->proc_token protects the allproc list and PHOLD() prevents the
1395 	 * process from being removed from the allproc list or the zombproc
1396 	 * list.
1397 	 */
1398 	for (n = 0; n < ALLPROC_HSIZE; ++n) {
1399 		procglob_t *prg = &procglob[n];
1400 
1401 		if (LIST_FIRST(&prg->allproc) == NULL)
1402 			continue;
1403 		lwkt_gettoken(&prg->proc_token);
1404 		LIST_FOREACH(p, &prg->allproc, p_list) {
1405 			if (p->p_stat != SZOMB)
1406 				continue;
1407 			PHOLD(p);
1408 			r = callback(p, data);
1409 			PRELE(p);
1410 			if (r < 0)
1411 				break;
1412 		}
1413 		lwkt_reltoken(&prg->proc_token);
1414 
1415 		/*
1416 		 * Check if asked to stop early
1417 		 */
1418 		if (p)
1419 			break;
1420 	}
1421 }
1422 
1423 #include "opt_ddb.h"
1424 #ifdef DDB
1425 #include <ddb/ddb.h>
1426 
1427 /*
1428  * Debugging only
1429  */
1430 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1431 {
1432 	struct pgrp *pgrp;
1433 	struct proc *p;
1434 	procglob_t *prg;
1435 	int i;
1436 
1437 	for (i = 0; i < ALLPROC_HSIZE; ++i) {
1438 		prg = &procglob[i];
1439 
1440 		if (LIST_EMPTY(&prg->allpgrp))
1441 			continue;
1442 		kprintf("\tindx %d\n", i);
1443 		LIST_FOREACH(pgrp, &prg->allpgrp, pg_list) {
1444 			kprintf("\tpgrp %p, pgid %ld, sess %p, "
1445 				"sesscnt %d, mem %p\n",
1446 				(void *)pgrp, (long)pgrp->pg_id,
1447 				(void *)pgrp->pg_session,
1448 				pgrp->pg_session->s_count,
1449 				(void *)LIST_FIRST(&pgrp->pg_members));
1450 			LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1451 				kprintf("\t\tpid %ld addr %p pgrp %p\n",
1452 					(long)p->p_pid, (void *)p,
1453 					(void *)p->p_pgrp);
1454 			}
1455 		}
1456 	}
1457 }
1458 #endif /* DDB */
1459 
1460 /*
1461  * The caller must hold proc_token.
1462  */
1463 static int
1464 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1465 {
1466 	struct kinfo_proc ki;
1467 	struct lwp *lp;
1468 	int skp = 0, had_output = 0;
1469 	int error;
1470 
1471 	bzero(&ki, sizeof(ki));
1472 	lwkt_gettoken_shared(&p->p_token);
1473 	fill_kinfo_proc(p, &ki);
1474 	if ((flags & KERN_PROC_FLAG_LWP) == 0)
1475 		skp = 1;
1476 	error = 0;
1477 	FOREACH_LWP_IN_PROC(lp, p) {
1478 		LWPHOLD(lp);
1479 		fill_kinfo_lwp(lp, &ki.kp_lwp);
1480 		had_output = 1;
1481 		error = SYSCTL_OUT(req, &ki, sizeof(ki));
1482 		LWPRELE(lp);
1483 		if (error)
1484 			break;
1485 		if (skp)
1486 			break;
1487 	}
1488 	lwkt_reltoken(&p->p_token);
1489 	/* We need to output at least the proc, even if there is no lwp. */
1490 	if (had_output == 0) {
1491 		error = SYSCTL_OUT(req, &ki, sizeof(ki));
1492 	}
1493 	return (error);
1494 }
1495 
1496 /*
1497  * The caller must hold proc_token.
1498  */
1499 static int
1500 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req)
1501 {
1502 	struct kinfo_proc ki;
1503 	int error;
1504 
1505 	fill_kinfo_proc_kthread(td, &ki);
1506 	error = SYSCTL_OUT(req, &ki, sizeof(ki));
1507 	if (error)
1508 		return error;
1509 	return(0);
1510 }
1511 
1512 /*
1513  * No requirements.
1514  */
1515 static int
1516 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1517 {
1518 	int *name = (int *)arg1;
1519 	int oid = oidp->oid_number;
1520 	u_int namelen = arg2;
1521 	struct proc *p;
1522 	struct thread *td;
1523 	struct thread *marker;
1524 	int flags = 0;
1525 	int error = 0;
1526 	int n;
1527 	int origcpu;
1528 	struct ucred *cr1 = curproc->p_ucred;
1529 	struct ucred *crcache = NULL;
1530 
1531 	flags = oid & KERN_PROC_FLAGMASK;
1532 	oid &= ~KERN_PROC_FLAGMASK;
1533 
1534 	if ((oid == KERN_PROC_ALL && namelen != 0) ||
1535 	    (oid != KERN_PROC_ALL && namelen != 1)) {
1536 		return (EINVAL);
1537 	}
1538 
1539 	/*
1540 	 * proc_token protects the allproc list and PHOLD() prevents the
1541 	 * process from being removed from the allproc list or the zombproc
1542 	 * list.
1543 	 */
1544 	if (oid == KERN_PROC_PID) {
1545 		p = pfind((pid_t)name[0]);
1546 		if (p) {
1547 			crcache = pcredcache(crcache, p);
1548 			if (PRISON_CHECK(cr1, crcache))
1549 				error = sysctl_out_proc(p, req, flags);
1550 			PRELE(p);
1551 		}
1552 		goto post_threads;
1553 	}
1554 	p = NULL;
1555 
1556 	if (!req->oldptr) {
1557 		/* overestimate by 5 procs */
1558 		error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1559 		if (error)
1560 			goto post_threads;
1561 	}
1562 
1563 	for (n = 0; n < ALLPROC_HSIZE; ++n) {
1564 		procglob_t *prg = &procglob[n];
1565 
1566 		if (LIST_EMPTY(&prg->allproc))
1567 			continue;
1568 		lwkt_gettoken_shared(&prg->proc_token);
1569 		LIST_FOREACH(p, &prg->allproc, p_list) {
1570 			/*
1571 			 * Show a user only their processes.
1572 			 */
1573 			if (ps_showallprocs == 0) {
1574 				crcache = pcredcache(crcache, p);
1575 				if (crcache == NULL ||
1576 				    p_trespass(cr1, crcache)) {
1577 					continue;
1578 				}
1579 			}
1580 
1581 			/*
1582 			 * Skip embryonic processes.
1583 			 */
1584 			if (p->p_stat == SIDL)
1585 				continue;
1586 			/*
1587 			 * TODO - make more efficient (see notes below).
1588 			 * do by session.
1589 			 */
1590 			switch (oid) {
1591 			case KERN_PROC_PGRP:
1592 				/* could do this by traversing pgrp */
1593 				if (p->p_pgrp == NULL ||
1594 				    p->p_pgrp->pg_id != (pid_t)name[0])
1595 					continue;
1596 				break;
1597 
1598 			case KERN_PROC_TTY:
1599 				if ((p->p_flags & P_CONTROLT) == 0 ||
1600 				    p->p_session == NULL ||
1601 				    p->p_session->s_ttyp == NULL ||
1602 				    dev2udev(p->p_session->s_ttyp->t_dev) !=
1603 					(udev_t)name[0])
1604 					continue;
1605 				break;
1606 
1607 			case KERN_PROC_UID:
1608 				crcache = pcredcache(crcache, p);
1609 				if (crcache == NULL ||
1610 				    crcache->cr_uid != (uid_t)name[0]) {
1611 					continue;
1612 				}
1613 				break;
1614 
1615 			case KERN_PROC_RUID:
1616 				crcache = pcredcache(crcache, p);
1617 				if (crcache == NULL ||
1618 				    crcache->cr_ruid != (uid_t)name[0]) {
1619 					continue;
1620 				}
1621 				break;
1622 			}
1623 
1624 			crcache = pcredcache(crcache, p);
1625 			if (!PRISON_CHECK(cr1, crcache))
1626 				continue;
1627 			PHOLD(p);
1628 			error = sysctl_out_proc(p, req, flags);
1629 			PRELE(p);
1630 			if (error) {
1631 				lwkt_reltoken(&prg->proc_token);
1632 				goto post_threads;
1633 			}
1634 		}
1635 		lwkt_reltoken(&prg->proc_token);
1636 	}
1637 
1638 	/*
1639 	 * Iterate over all active cpus and scan their thread list.  Start
1640 	 * with the next logical cpu and end with our original cpu.  We
1641 	 * migrate our own thread to each target cpu in order to safely scan
1642 	 * its thread list.  In the last loop we migrate back to our original
1643 	 * cpu.
1644 	 */
1645 	origcpu = mycpu->gd_cpuid;
1646 	if (!ps_showallthreads || jailed(cr1))
1647 		goto post_threads;
1648 
1649 	marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1650 	marker->td_flags = TDF_MARKER;
1651 	error = 0;
1652 
1653 	for (n = 1; n <= ncpus; ++n) {
1654 		globaldata_t rgd;
1655 		int nid;
1656 
1657 		nid = (origcpu + n) % ncpus;
1658 		if (CPUMASK_TESTBIT(smp_active_mask, nid) == 0)
1659 			continue;
1660 		rgd = globaldata_find(nid);
1661 		lwkt_setcpu_self(rgd);
1662 
1663 		crit_enter();
1664 		TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1665 
1666 		while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1667 			TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1668 			TAILQ_INSERT_BEFORE(td, marker, td_allq);
1669 			if (td->td_flags & TDF_MARKER)
1670 				continue;
1671 			if (td->td_proc)
1672 				continue;
1673 
1674 			lwkt_hold(td);
1675 			crit_exit();
1676 
1677 			switch (oid) {
1678 			case KERN_PROC_PGRP:
1679 			case KERN_PROC_TTY:
1680 			case KERN_PROC_UID:
1681 			case KERN_PROC_RUID:
1682 				break;
1683 			default:
1684 				error = sysctl_out_proc_kthread(td, req);
1685 				break;
1686 			}
1687 			lwkt_rele(td);
1688 			crit_enter();
1689 			if (error)
1690 				break;
1691 		}
1692 		TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1693 		crit_exit();
1694 
1695 		if (error)
1696 			break;
1697 	}
1698 
1699 	/*
1700 	 * Userland scheduler expects us to return on the same cpu we
1701 	 * started on.
1702 	 */
1703 	if (mycpu->gd_cpuid != origcpu)
1704 		lwkt_setcpu_self(globaldata_find(origcpu));
1705 
1706 	kfree(marker, M_TEMP);
1707 
1708 post_threads:
1709 	if (crcache)
1710 		crfree(crcache);
1711 	return (error);
1712 }
1713 
1714 /*
1715  * This sysctl allows a process to retrieve the argument list or process
1716  * title for another process without groping around in the address space
1717  * of the other process.  It also allow a process to set its own "process
1718  * title to a string of its own choice.
1719  *
1720  * No requirements.
1721  */
1722 static int
1723 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1724 {
1725 	int *name = (int*) arg1;
1726 	u_int namelen = arg2;
1727 	struct proc *p;
1728 	struct pargs *opa;
1729 	struct pargs *pa;
1730 	int error = 0;
1731 	struct ucred *cr1 = curproc->p_ucred;
1732 
1733 	if (namelen != 1)
1734 		return (EINVAL);
1735 
1736 	p = pfind((pid_t)name[0]);
1737 	if (p == NULL)
1738 		goto done;
1739 	lwkt_gettoken(&p->p_token);
1740 
1741 	if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1742 		goto done;
1743 
1744 	if (req->newptr && curproc != p) {
1745 		error = EPERM;
1746 		goto done;
1747 	}
1748 	if (req->oldptr) {
1749 		if (p->p_upmap != NULL && p->p_upmap->proc_title[0]) {
1750 			/*
1751 			 * Args set via writable user process mmap.
1752 			 * We must calculate the string length manually
1753 			 * because the user data can change at any time.
1754 			 */
1755 			size_t n;
1756 			char *base;
1757 
1758 			base = p->p_upmap->proc_title;
1759 			for (n = 0; n < UPMAP_MAXPROCTITLE - 1; ++n) {
1760 				if (base[n] == 0)
1761 					break;
1762 			}
1763 			error = SYSCTL_OUT(req, base, n);
1764 			if (error == 0)
1765 				error = SYSCTL_OUT(req, "", 1);
1766 		} else if ((pa = p->p_args) != NULL) {
1767 			/*
1768 			 * Args set by setproctitle() sysctl.
1769 			 */
1770 			refcount_acquire(&pa->ar_ref);
1771 			error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1772 			if (refcount_release(&pa->ar_ref))
1773 				kfree(pa, M_PARGS);
1774 		}
1775 	}
1776 	if (req->newptr == NULL)
1777 		goto done;
1778 
1779 	if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1780 		goto done;
1781 	}
1782 
1783 	pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1784 	refcount_init(&pa->ar_ref, 1);
1785 	pa->ar_length = req->newlen;
1786 	error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1787 	if (error) {
1788 		kfree(pa, M_PARGS);
1789 		goto done;
1790 	}
1791 
1792 
1793 	/*
1794 	 * Replace p_args with the new pa.  p_args may have previously
1795 	 * been NULL.
1796 	 */
1797 	opa = p->p_args;
1798 	p->p_args = pa;
1799 
1800 	if (opa) {
1801 		KKASSERT(opa->ar_ref > 0);
1802 		if (refcount_release(&opa->ar_ref)) {
1803 			kfree(opa, M_PARGS);
1804 			/* opa = NULL; */
1805 		}
1806 	}
1807 done:
1808 	if (p) {
1809 		lwkt_reltoken(&p->p_token);
1810 		PRELE(p);
1811 	}
1812 	return (error);
1813 }
1814 
1815 static int
1816 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1817 {
1818 	int *name = (int*) arg1;
1819 	u_int namelen = arg2;
1820 	struct proc *p;
1821 	int error = 0;
1822 	char *fullpath, *freepath;
1823 	struct ucred *cr1 = curproc->p_ucred;
1824 
1825 	if (namelen != 1)
1826 		return (EINVAL);
1827 
1828 	p = pfind((pid_t)name[0]);
1829 	if (p == NULL)
1830 		goto done;
1831 	lwkt_gettoken_shared(&p->p_token);
1832 
1833 	/*
1834 	 * If we are not allowed to see other args, we certainly shouldn't
1835 	 * get the cwd either. Also check the usual trespassing.
1836 	 */
1837 	if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1838 		goto done;
1839 
1840 	if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1841 		struct nchandle nch;
1842 
1843 		cache_copy(&p->p_fd->fd_ncdir, &nch);
1844 		error = cache_fullpath(p, &nch, NULL,
1845 				       &fullpath, &freepath, 0);
1846 		cache_drop(&nch);
1847 		if (error)
1848 			goto done;
1849 		error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1850 		kfree(freepath, M_TEMP);
1851 	}
1852 
1853 done:
1854 	if (p) {
1855 		lwkt_reltoken(&p->p_token);
1856 		PRELE(p);
1857 	}
1858 	return (error);
1859 }
1860 
1861 /*
1862  * This sysctl allows a process to retrieve the path of the executable for
1863  * itself or another process.
1864  */
1865 static int
1866 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1867 {
1868 	pid_t *pidp = (pid_t *)arg1;
1869 	unsigned int arglen = arg2;
1870 	struct proc *p;
1871 	char *retbuf, *freebuf;
1872 	int error = 0;
1873 	struct nchandle nch;
1874 
1875 	if (arglen != 1)
1876 		return (EINVAL);
1877 	if (*pidp == -1) {	/* -1 means this process */
1878 		p = curproc;
1879 	} else {
1880 		p = pfind(*pidp);
1881 		if (p == NULL)
1882 			return (ESRCH);
1883 	}
1884 
1885 	cache_copy(&p->p_textnch, &nch);
1886 	error = cache_fullpath(p, &nch, NULL, &retbuf, &freebuf, 0);
1887 	cache_drop(&nch);
1888 	if (error)
1889 		goto done;
1890 	error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1891 	kfree(freebuf, M_TEMP);
1892 done:
1893 	if (*pidp != -1)
1894 		PRELE(p);
1895 
1896 	return (error);
1897 }
1898 
1899 static int
1900 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
1901 {
1902         /*int *name = (int *)arg1;*/
1903         u_int namelen = arg2;
1904         struct kinfo_sigtramp kst;
1905         const struct sysentvec *sv;
1906         int error;
1907 
1908         if (namelen > 1)
1909                 return (EINVAL);
1910         /* ignore pid if passed in (freebsd compatibility) */
1911 
1912         sv = curproc->p_sysent;
1913         bzero(&kst, sizeof(kst));
1914         if (sv->sv_szsigcode) {
1915 		intptr_t sigbase;
1916 
1917 		sigbase = trunc_page64((intptr_t)PS_STRINGS -
1918 				       *sv->sv_szsigcode);
1919 		sigbase -= SZSIGCODE_EXTRA_BYTES;
1920 
1921                 kst.ksigtramp_start = (void *)sigbase;
1922                 kst.ksigtramp_end = (void *)(sigbase + *sv->sv_szsigcode);
1923         }
1924         error = SYSCTL_OUT(req, &kst, sizeof(kst));
1925 
1926         return (error);
1927 }
1928 
1929 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
1930 
1931 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all,
1932 	CTLFLAG_RD | CTLTYPE_STRUCT | CTLFLAG_NOLOCK,
1933 	0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1934 
1935 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp,
1936 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1937 	sysctl_kern_proc, "Process table");
1938 
1939 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty,
1940 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1941 	sysctl_kern_proc, "Process table");
1942 
1943 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid,
1944 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1945 	sysctl_kern_proc, "Process table");
1946 
1947 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid,
1948 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1949 	sysctl_kern_proc, "Process table");
1950 
1951 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid,
1952 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1953 	sysctl_kern_proc, "Process table");
1954 
1955 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp,
1956 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1957 	sysctl_kern_proc, "Process table");
1958 
1959 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp,
1960 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1961 	sysctl_kern_proc, "Process table");
1962 
1963 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp,
1964 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1965 	sysctl_kern_proc, "Process table");
1966 
1967 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp,
1968 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1969 	sysctl_kern_proc, "Process table");
1970 
1971 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp,
1972 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1973 	sysctl_kern_proc, "Process table");
1974 
1975 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp,
1976 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1977 	sysctl_kern_proc, "Process table");
1978 
1979 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
1980 	CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
1981 	sysctl_kern_proc_args, "Process argument list");
1982 
1983 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd,
1984 	CTLFLAG_RD | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
1985 	sysctl_kern_proc_cwd, "Process argument list");
1986 
1987 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname,
1988 	CTLFLAG_RD | CTLFLAG_NOLOCK,
1989 	sysctl_kern_proc_pathname, "Process executable path");
1990 
1991 SYSCTL_PROC(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp,
1992 	CTLFLAG_RD | CTLTYPE_STRUCT | CTLFLAG_NOLOCK,
1993         0, 0, sysctl_kern_proc_sigtramp, "S,sigtramp",
1994         "Return sigtramp address range");
1995