2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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>
36 #include <sys/vnode.h>
38 #include <sys/filedesc.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>
48 #include <vm/vm_map.h>
50 #include <machine/smp.h>
52 #include <sys/refcount.h>
53 #include <sys/spinlock2.h>
56 * Hash table size must be a power of two and is not currently dynamically
57 * sized. There is a trade-off between the linear scans which must iterate
58 * all HSIZE elements and the number of elements which might accumulate
59 * within each hash chain.
61 #define ALLPROC_HSIZE 256
62 #define ALLPROC_HMASK (ALLPROC_HSIZE - 1)
63 #define ALLPROC_HASH(pid) (pid & ALLPROC_HMASK)
64 #define PGRP_HASH(pid) (pid & ALLPROC_HMASK)
65 #define SESS_HASH(pid) (pid & ALLPROC_HMASK)
68 * pid_doms[] management, used to control how quickly a PID can be recycled.
69 * Must be a multiple of ALLPROC_HSIZE for the proc_makepid() inner loops.
71 * WARNING! PIDDOM_DELAY should not be defined > 20 or so unless you change
72 * the array from int8_t's to int16_t's.
74 #define PIDDOM_COUNT 10 /* 10 pids per domain - reduce array size */
75 #define PIDDOM_DELAY 10 /* min 10 seconds after exit before reuse */
76 #define PIDDOM_SCALE 10 /* (10,000*SCALE)/sec performance guarantee */
77 #define PIDSEL_DOMAINS (PID_MAX * PIDDOM_SCALE / PIDDOM_COUNT / \
78 ALLPROC_HSIZE * ALLPROC_HSIZE)
81 int allproc_hsize = ALLPROC_HSIZE;
83 LIST_HEAD(pidhashhead, proc);
85 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
86 MALLOC_DEFINE(M_SESSION, "session", "session header");
87 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
88 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
89 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
91 int ps_showallprocs = 1;
92 static int ps_showallthreads = 1;
93 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
95 "Unprivileged processes can see processes with different UID/GID");
96 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
97 &ps_showallthreads, 0,
98 "Unprivileged processes can see kernel threads");
99 static u_int pid_domain_skips;
100 SYSCTL_UINT(_kern, OID_AUTO, pid_domain_skips, CTLFLAG_RW,
101 &pid_domain_skips, 0,
102 "Number of pid_doms[] skipped");
103 static u_int pid_inner_skips;
104 SYSCTL_UINT(_kern, OID_AUTO, pid_inner_skips, CTLFLAG_RW,
106 "Number of pid_doms[] skipped");
108 static void orphanpg(struct pgrp *pg);
109 static void proc_makepid(struct proc *p, int random_offset);
112 * Process related lists (for proc_token, allproc, allpgrp, and allsess)
114 typedef struct procglob procglob_t;
116 static procglob_t procglob[ALLPROC_HSIZE];
119 * We try our best to avoid recycling a PID too quickly. We do this by
120 * storing (uint8_t)time_second in the related pid domain on-reap and then
121 * using that to skip-over the domain on-allocate.
123 * This array has to be fairly large to support a high fork/exec rate.
124 * A ~100,000 entry array will support a 10-second reuse latency at
125 * 10,000 execs/second, worst case. Best-case multiply by PIDDOM_COUNT
126 * (approximately 100,000 execs/second).
128 * Currently we allocate around a megabyte, making the worst-case fork
129 * rate around 100,000/second.
131 static uint8_t *pid_doms;
134 * Random component to nextpid generation. We mix in a random factor to make
135 * it a little harder to predict. We sanity check the modulus value to avoid
136 * doing it in critical paths. Don't let it be too small or we pointlessly
137 * waste randomness entropy, and don't let it be impossibly large. Using a
138 * modulus that is too big causes a LOT more process table scans and slows
139 * down fork processing as the pidchecked caching is defeated.
141 static int randompid = 0;
147 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
152 error = sysctl_handle_int(oidp, &pid, 0, req);
153 if (error || !req->newptr)
155 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
157 else if (pid < 2) /* NOP */
159 else if (pid < 100) /* Make it reasonable */
165 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
166 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
169 * Initialize global process hashing structures.
171 * These functions are ONLY called from the low level boot code and do
172 * not lock their operations.
180 * Allocate dynamically. This array can be large (~1MB) so don't
181 * waste boot loader space.
183 pid_doms = kmalloc(sizeof(pid_doms[0]) * PIDSEL_DOMAINS,
184 M_PROC, M_WAITOK | M_ZERO);
187 * Avoid unnecessary stalls due to pid_doms[] values all being
188 * the same. Make sure that the allocation of pid 1 and pid 2
191 for (i = 0; i < PIDSEL_DOMAINS; ++i)
192 pid_doms[i] = (int8_t)i - (int8_t)(PIDDOM_DELAY + 1);
197 for (i = 0; i < ALLPROC_HSIZE; ++i) {
198 procglob_t *prg = &procglob[i];
199 LIST_INIT(&prg->allproc);
200 LIST_INIT(&prg->allsess);
201 LIST_INIT(&prg->allpgrp);
202 lwkt_token_init(&prg->proc_token, "allproc");
208 procinsertinit(struct proc *p)
210 LIST_INSERT_HEAD(&procglob[ALLPROC_HASH(p->p_pid)].allproc,
215 pgrpinsertinit(struct pgrp *pg)
217 LIST_INSERT_HEAD(&procglob[ALLPROC_HASH(pg->pg_id)].allpgrp,
222 sessinsertinit(struct session *sess)
224 LIST_INSERT_HEAD(&procglob[ALLPROC_HASH(sess->s_sid)].allsess,
229 * Process hold/release support functions. Called via the PHOLD(),
230 * PRELE(), and PSTALL() macros.
232 * p->p_lock is a simple hold count with a waiting interlock. No wakeup()
233 * is issued unless someone is actually waiting for the process.
235 * Most holds are short-term, allowing a process scan or other similar
236 * operation to access a proc structure without it getting ripped out from
237 * under us. procfs and process-list sysctl ops also use the hold function
238 * interlocked with various p_flags to keep the vmspace intact when reading
239 * or writing a user process's address space.
241 * There are two situations where a hold count can be longer. Exiting lwps
242 * hold the process until the lwp is reaped, and the parent will hold the
243 * child during vfork()/exec() sequences while the child is marked P_PPWAIT.
245 * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at
246 * various critical points in the fork/exec and exit paths before proceeding.
248 #define PLOCK_ZOMB 0x20000000
249 #define PLOCK_WAITING 0x40000000
250 #define PLOCK_MASK 0x1FFFFFFF
253 pstall(struct proc *p, const char *wmesg, int count)
261 if ((o & PLOCK_MASK) <= count)
263 n = o | PLOCK_WAITING;
264 tsleep_interlock(&p->p_lock, 0);
267 * If someone is trying to single-step the process during
268 * an exec or an exit they can deadlock us because procfs
269 * sleeps with the process held.
272 if (p->p_flags & P_INEXEC) {
274 } else if (p->p_flags & P_POSTEXIT) {
275 spin_lock(&p->p_spin);
278 spin_unlock(&p->p_spin);
283 if (atomic_cmpset_int(&p->p_lock, o, n)) {
284 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
290 phold(struct proc *p)
292 atomic_add_int(&p->p_lock, 1);
296 * WARNING! On last release (p) can become instantly invalid due to
300 prele(struct proc *p)
308 if (atomic_cmpset_int(&p->p_lock, 1, 0))
316 KKASSERT((o & PLOCK_MASK) > 0);
318 n = (o - 1) & ~PLOCK_WAITING;
319 if (atomic_cmpset_int(&p->p_lock, o, n)) {
320 if (o & PLOCK_WAITING)
328 * Hold and flag serialized for zombie reaping purposes.
330 * This function will fail if it has to block, returning non-zero with
331 * neither the flag set or the hold count bumped. Note that we must block
332 * without holding a ref, meaning that the caller must ensure that (p)
333 * remains valid through some other interlock (typically on its parent
334 * process's p_token).
336 * Zero is returned on success. The hold count will be incremented and
337 * the serialization flag acquired. Note that serialization is only against
338 * other pholdzomb() calls, not against phold() calls.
341 pholdzomb(struct proc *p)
349 if (atomic_cmpset_int(&p->p_lock, 0, PLOCK_ZOMB | 1))
358 if ((o & PLOCK_ZOMB) == 0) {
359 n = (o + 1) | PLOCK_ZOMB;
360 if (atomic_cmpset_int(&p->p_lock, o, n))
363 KKASSERT((o & PLOCK_MASK) > 0);
364 n = o | PLOCK_WAITING;
365 tsleep_interlock(&p->p_lock, 0);
366 if (atomic_cmpset_int(&p->p_lock, o, n)) {
367 tsleep(&p->p_lock, PINTERLOCKED, "phldz", 0);
368 /* (p) can be ripped out at this point */
376 * Release PLOCK_ZOMB and the hold count, waking up any waiters.
378 * WARNING! On last release (p) can become instantly invalid due to
382 prelezomb(struct proc *p)
390 if (atomic_cmpset_int(&p->p_lock, PLOCK_ZOMB | 1, 0))
396 KKASSERT(p->p_lock & PLOCK_ZOMB);
399 KKASSERT((o & PLOCK_MASK) > 0);
401 n = (o - 1) & ~(PLOCK_ZOMB | PLOCK_WAITING);
402 if (atomic_cmpset_int(&p->p_lock, o, n)) {
403 if (o & PLOCK_WAITING)
411 * Is p an inferior of the current process?
416 inferior(struct proc *p)
421 lwkt_gettoken_shared(&p->p_token);
422 while (p != curproc) {
424 lwkt_reltoken(&p->p_token);
429 lwkt_reltoken(&p->p_token);
431 lwkt_gettoken_shared(&p2->p_token);
434 lwkt_reltoken(&p->p_token);
441 * Locate a process by number. The returned process will be referenced and
442 * must be released with PRELE().
449 struct proc *p = curproc;
454 * Shortcut the current process
456 if (p && p->p_pid == pid) {
462 * Otherwise find it in the hash table.
464 n = ALLPROC_HASH(pid);
467 lwkt_gettoken_shared(&prg->proc_token);
468 LIST_FOREACH(p, &prg->allproc, p_list) {
469 if (p->p_stat == SZOMB)
471 if (p->p_pid == pid) {
473 lwkt_reltoken(&prg->proc_token);
477 lwkt_reltoken(&prg->proc_token);
483 * Locate a process by number. The returned process is NOT referenced.
484 * The result will not be stable and is typically only used to validate
485 * against a process that the caller has in-hand.
492 struct proc *p = curproc;
497 * Shortcut the current process
499 if (p && p->p_pid == pid)
503 * Otherwise find it in the hash table.
505 n = ALLPROC_HASH(pid);
508 lwkt_gettoken_shared(&prg->proc_token);
509 LIST_FOREACH(p, &prg->allproc, p_list) {
510 if (p->p_stat == SZOMB)
512 if (p->p_pid == pid) {
513 lwkt_reltoken(&prg->proc_token);
517 lwkt_reltoken(&prg->proc_token);
523 * Locate a process on the zombie list. Return a process or NULL.
524 * The returned process will be referenced and the caller must release
527 * No other requirements.
532 struct proc *p = curproc;
537 * Shortcut the current process
539 if (p && p->p_pid == pid) {
545 * Otherwise find it in the hash table.
547 n = ALLPROC_HASH(pid);
550 lwkt_gettoken_shared(&prg->proc_token);
551 LIST_FOREACH(p, &prg->allproc, p_list) {
552 if (p->p_stat != SZOMB)
554 if (p->p_pid == pid) {
556 lwkt_reltoken(&prg->proc_token);
560 lwkt_reltoken(&prg->proc_token);
567 pgref(struct pgrp *pgrp)
569 refcount_acquire(&pgrp->pg_refs);
573 pgrel(struct pgrp *pgrp)
579 n = PGRP_HASH(pgrp->pg_id);
583 count = pgrp->pg_refs;
587 lwkt_gettoken(&prg->proc_token);
588 if (atomic_cmpset_int(&pgrp->pg_refs, 1, 0))
590 lwkt_reltoken(&prg->proc_token);
593 if (atomic_cmpset_int(&pgrp->pg_refs, count, count - 1))
600 * Successful 1->0 transition, pghash_spin is held.
602 LIST_REMOVE(pgrp, pg_list);
603 if (pid_doms[pgrp->pg_id % PIDSEL_DOMAINS] != (uint8_t)time_second)
604 pid_doms[pgrp->pg_id % PIDSEL_DOMAINS] = (uint8_t)time_second;
607 * Reset any sigio structures pointing to us as a result of
608 * F_SETOWN with our pgid.
610 funsetownlst(&pgrp->pg_sigiolst);
612 if (pgrp->pg_session->s_ttyp != NULL &&
613 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
614 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
616 lwkt_reltoken(&prg->proc_token);
618 sess_rele(pgrp->pg_session);
623 * Locate a process group by number. The returned process group will be
624 * referenced w/pgref() and must be released with pgrel() (or assigned
625 * somewhere if you wish to keep the reference).
638 lwkt_gettoken_shared(&prg->proc_token);
640 LIST_FOREACH(pgrp, &prg->allpgrp, pg_list) {
641 if (pgrp->pg_id == pgid) {
642 refcount_acquire(&pgrp->pg_refs);
643 lwkt_reltoken(&prg->proc_token);
647 lwkt_reltoken(&prg->proc_token);
652 * Move p to a new or existing process group (and session)
657 enterpgrp(struct proc *p, pid_t pgid, int mksess)
665 KASSERT(pgrp == NULL || !mksess,
666 ("enterpgrp: setsid into non-empty pgrp"));
667 KASSERT(!SESS_LEADER(p),
668 ("enterpgrp: session leader attempted setpgrp"));
671 pid_t savepid = p->p_pid;
679 KASSERT(p->p_pid == pgid,
680 ("enterpgrp: new pgrp and pid != pgid"));
681 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
683 LIST_INIT(&pgrp->pg_members);
685 SLIST_INIT(&pgrp->pg_sigiolst);
686 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
687 refcount_init(&pgrp->pg_refs, 1);
688 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
693 if ((np = pfindn(savepid)) == NULL || np != p) {
694 lwkt_reltoken(&prg->proc_token);
700 lwkt_gettoken(&prg->proc_token);
702 struct session *sess;
707 sess = kmalloc(sizeof(struct session), M_SESSION,
709 lwkt_gettoken(&p->p_token);
711 sess->s_sid = p->p_pid;
713 sess->s_ttyvp = NULL;
715 bcopy(p->p_session->s_login, sess->s_login,
716 sizeof(sess->s_login));
717 pgrp->pg_session = sess;
718 KASSERT(p == curproc,
719 ("enterpgrp: mksession and p != curproc"));
720 p->p_flags &= ~P_CONTROLT;
721 LIST_INSERT_HEAD(&prg->allsess, sess, s_list);
722 lwkt_reltoken(&p->p_token);
724 lwkt_gettoken(&p->p_token);
725 pgrp->pg_session = p->p_session;
726 sess_hold(pgrp->pg_session);
727 lwkt_reltoken(&p->p_token);
729 LIST_INSERT_HEAD(&prg->allpgrp, pgrp, pg_list);
731 lwkt_reltoken(&prg->proc_token);
732 } else if (pgrp == p->p_pgrp) {
735 } /* else pgfind() referenced the pgrp */
737 lwkt_gettoken(&pgrp->pg_token);
738 lwkt_gettoken(&p->p_token);
741 * Replace p->p_pgrp, handling any races that occur.
743 while ((opgrp = p->p_pgrp) != NULL) {
745 lwkt_gettoken(&opgrp->pg_token);
746 if (opgrp != p->p_pgrp) {
747 lwkt_reltoken(&opgrp->pg_token);
751 LIST_REMOVE(p, p_pglist);
755 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
758 * Adjust eligibility of affected pgrps to participate in job control.
759 * Increment eligibility counts before decrementing, otherwise we
760 * could reach 0 spuriously during the first call.
764 fixjobc(p, opgrp, 0);
765 lwkt_reltoken(&opgrp->pg_token);
766 pgrel(opgrp); /* manual pgref */
767 pgrel(opgrp); /* p->p_pgrp ref */
769 lwkt_reltoken(&p->p_token);
770 lwkt_reltoken(&pgrp->pg_token);
778 * Remove process from process group
783 leavepgrp(struct proc *p)
785 struct pgrp *pg = p->p_pgrp;
787 lwkt_gettoken(&p->p_token);
788 while ((pg = p->p_pgrp) != NULL) {
790 lwkt_gettoken(&pg->pg_token);
791 if (p->p_pgrp != pg) {
792 lwkt_reltoken(&pg->pg_token);
797 LIST_REMOVE(p, p_pglist);
798 lwkt_reltoken(&pg->pg_token);
799 pgrel(pg); /* manual pgref */
800 pgrel(pg); /* p->p_pgrp ref */
803 lwkt_reltoken(&p->p_token);
809 * Adjust the ref count on a session structure. When the ref count falls to
810 * zero the tty is disassociated from the session and the session structure
811 * is freed. Note that tty assocation is not itself ref-counted.
816 sess_hold(struct session *sp)
818 atomic_add_int(&sp->s_count, 1);
825 sess_rele(struct session *sess)
832 n = SESS_HASH(sess->s_sid);
836 count = sess->s_count;
840 lwkt_gettoken(&tty_token);
841 lwkt_gettoken(&prg->proc_token);
842 if (atomic_cmpset_int(&sess->s_count, 1, 0))
844 lwkt_reltoken(&prg->proc_token);
845 lwkt_reltoken(&tty_token);
848 if (atomic_cmpset_int(&sess->s_count, count, count - 1))
855 * Successful 1->0 transition and tty_token is held.
857 LIST_REMOVE(sess, s_list);
858 if (pid_doms[sess->s_sid % PIDSEL_DOMAINS] != (uint8_t)time_second)
859 pid_doms[sess->s_sid % PIDSEL_DOMAINS] = (uint8_t)time_second;
861 if (sess->s_ttyp && sess->s_ttyp->t_session) {
862 #ifdef TTY_DO_FULL_CLOSE
863 /* FULL CLOSE, see ttyclearsession() */
864 KKASSERT(sess->s_ttyp->t_session == sess);
865 sess->s_ttyp->t_session = NULL;
867 /* HALF CLOSE, see ttyclearsession() */
868 if (sess->s_ttyp->t_session == sess)
869 sess->s_ttyp->t_session = NULL;
872 if ((tp = sess->s_ttyp) != NULL) {
876 lwkt_reltoken(&prg->proc_token);
877 lwkt_reltoken(&tty_token);
879 kfree(sess, M_SESSION);
883 * Adjust pgrp jobc counters when specified process changes process group.
884 * We count the number of processes in each process group that "qualify"
885 * the group for terminal job control (those with a parent in a different
886 * process group of the same session). If that count reaches zero, the
887 * process group becomes orphaned. Check both the specified process'
888 * process group and that of its children.
889 * entering == 0 => p is leaving specified group.
890 * entering == 1 => p is entering specified group.
895 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
897 struct pgrp *hispgrp;
898 struct session *mysession;
902 * Check p's parent to see whether p qualifies its own process
903 * group; if so, adjust count for p's process group.
905 lwkt_gettoken(&p->p_token); /* p_children scan */
906 lwkt_gettoken(&pgrp->pg_token);
908 mysession = pgrp->pg_session;
909 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
910 hispgrp->pg_session == mysession) {
913 else if (--pgrp->pg_jobc == 0)
918 * Check this process' children to see whether they qualify
919 * their process groups; if so, adjust counts for children's
922 LIST_FOREACH(np, &p->p_children, p_sibling) {
924 lwkt_gettoken(&np->p_token);
925 if ((hispgrp = np->p_pgrp) != pgrp &&
926 hispgrp->pg_session == mysession &&
927 np->p_stat != SZOMB) {
929 lwkt_gettoken(&hispgrp->pg_token);
932 else if (--hispgrp->pg_jobc == 0)
934 lwkt_reltoken(&hispgrp->pg_token);
937 lwkt_reltoken(&np->p_token);
940 KKASSERT(pgrp->pg_refs > 0);
941 lwkt_reltoken(&pgrp->pg_token);
942 lwkt_reltoken(&p->p_token);
946 * A process group has become orphaned;
947 * if there are any stopped processes in the group,
948 * hang-up all process in that group.
950 * The caller must hold pg_token.
953 orphanpg(struct pgrp *pg)
957 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
958 if (p->p_stat == SSTOP) {
959 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
969 * Add a new process to the allproc list and the PID hash. This
970 * also assigns a pid to the new process.
975 proc_add_allproc(struct proc *p)
979 if ((random_offset = randompid) != 0) {
980 read_random(&random_offset, sizeof(random_offset));
981 random_offset = (random_offset & 0x7FFFFFFF) % randompid;
983 proc_makepid(p, random_offset);
987 * Calculate a new process pid. This function is integrated into
988 * proc_add_allproc() to guarentee that the new pid is not reused before
989 * the new process can be added to the allproc list.
991 * p_pid is assigned and the process is added to the allproc hash table
993 * WARNING! We need to allocate PIDs sequentially during early boot.
994 * In particular, init needs to have a pid of 1.
998 proc_makepid(struct proc *p, int random_offset)
1000 static pid_t nextpid = 1; /* heuristic, allowed to race */
1004 struct session *sess;
1011 * Select the next pid base candidate.
1013 * Check cyclement, do not allow a pid < 100.
1017 base = atomic_fetchadd_int(&nextpid, 1) + random_offset;
1018 if (base <= 0 || base >= PID_MAX) {
1019 base = base % PID_MAX;
1024 nextpid = base; /* reset (SMP race ok) */
1028 * Do not allow a base pid to be selected from a domain that has
1029 * recently seen a pid/pgid/sessid reap. Sleep a little if we looped
1030 * through all available domains.
1032 * WARNING: We want the early pids to be allocated linearly,
1033 * particularly pid 1 and pid 2.
1035 if (++retries >= PIDSEL_DOMAINS)
1036 tsleep(&nextpid, 0, "makepid", 1);
1038 delta8 = (int8_t)time_second -
1039 (int8_t)pid_doms[base % PIDSEL_DOMAINS];
1040 if (delta8 >= 0 && delta8 <= PIDDOM_DELAY) {
1047 * Calculate a hash index and find an unused process id within
1048 * the table, looping if we cannot find one.
1050 * The inner loop increments by ALLPROC_HSIZE which keeps the
1051 * PID at the same pid_doms[] index as well as the same hash index.
1053 n = ALLPROC_HASH(base);
1055 lwkt_gettoken(&prg->proc_token);
1058 LIST_FOREACH(ps, &prg->allproc, p_list) {
1059 if (ps->p_pid == base) {
1060 base += ALLPROC_HSIZE;
1061 if (base >= PID_MAX) {
1062 lwkt_reltoken(&prg->proc_token);
1069 LIST_FOREACH(pg, &prg->allpgrp, pg_list) {
1070 if (pg->pg_id == base) {
1071 base += ALLPROC_HSIZE;
1072 if (base >= PID_MAX) {
1073 lwkt_reltoken(&prg->proc_token);
1080 LIST_FOREACH(sess, &prg->allsess, s_list) {
1081 if (sess->s_sid == base) {
1082 base += ALLPROC_HSIZE;
1083 if (base >= PID_MAX) {
1084 lwkt_reltoken(&prg->proc_token);
1093 * Assign the pid and insert the process.
1096 LIST_INSERT_HEAD(&prg->allproc, p, p_list);
1097 lwkt_reltoken(&prg->proc_token);
1101 * Called from exit1 to place the process into a zombie state.
1102 * The process is removed from the pid hash and p_stat is set
1103 * to SZOMB. Normal pfind[n]() calls will not find it any more.
1105 * Caller must hold p->p_token. We are required to wait until p_lock
1106 * becomes zero before we can manipulate the list, allowing allproc
1107 * scans to guarantee consistency during a list scan.
1110 proc_move_allproc_zombie(struct proc *p)
1115 n = ALLPROC_HASH(p->p_pid);
1117 PSTALL(p, "reap1", 0);
1118 lwkt_gettoken(&prg->proc_token);
1120 PSTALL(p, "reap1a", 0);
1123 lwkt_reltoken(&prg->proc_token);
1124 dsched_exit_proc(p);
1128 * This routine is called from kern_wait() and will remove the process
1129 * from the zombie list and the sibling list. This routine will block
1130 * if someone has a lock on the proces (p_lock).
1132 * Caller must hold p->p_token. We are required to wait until p_lock
1133 * becomes zero before we can manipulate the list, allowing allproc
1134 * scans to guarantee consistency during a list scan.
1137 proc_remove_zombie(struct proc *p)
1142 n = ALLPROC_HASH(p->p_pid);
1145 PSTALL(p, "reap2", 0);
1146 lwkt_gettoken(&prg->proc_token);
1147 PSTALL(p, "reap2a", 0);
1148 LIST_REMOVE(p, p_list); /* from remove master list */
1149 LIST_REMOVE(p, p_sibling); /* and from sibling list */
1151 if (pid_doms[p->p_pid % PIDSEL_DOMAINS] != (uint8_t)time_second)
1152 pid_doms[p->p_pid % PIDSEL_DOMAINS] = (uint8_t)time_second;
1153 lwkt_reltoken(&prg->proc_token);
1157 * Handle various requirements prior to returning to usermode. Called from
1158 * platform trap and system call code.
1161 lwpuserret(struct lwp *lp)
1163 struct proc *p = lp->lwp_proc;
1165 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1166 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1169 if (lp->lwp_mpflags & LWP_MP_WEXIT) {
1170 lwkt_gettoken(&p->p_token);
1172 lwkt_reltoken(&p->p_token); /* NOT REACHED */
1177 * Kernel threads run from user processes can also accumulate deferred
1178 * actions which need to be acted upon. Callers include:
1180 * nfsd - Can allocate lots of vnodes
1183 lwpkthreaddeferred(void)
1185 struct lwp *lp = curthread->td_lwp;
1188 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1189 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1196 proc_usermap(struct proc *p, int invfork)
1198 struct sys_upmap *upmap;
1200 lwkt_gettoken(&p->p_token);
1201 upmap = kmalloc(roundup2(sizeof(*upmap), PAGE_SIZE), M_PROC,
1203 if (p->p_upmap == NULL) {
1204 upmap->header[0].type = UKPTYPE_VERSION;
1205 upmap->header[0].offset = offsetof(struct sys_upmap, version);
1206 upmap->header[1].type = UPTYPE_RUNTICKS;
1207 upmap->header[1].offset = offsetof(struct sys_upmap, runticks);
1208 upmap->header[2].type = UPTYPE_FORKID;
1209 upmap->header[2].offset = offsetof(struct sys_upmap, forkid);
1210 upmap->header[3].type = UPTYPE_PID;
1211 upmap->header[3].offset = offsetof(struct sys_upmap, pid);
1212 upmap->header[4].type = UPTYPE_PROC_TITLE;
1213 upmap->header[4].offset = offsetof(struct sys_upmap,proc_title);
1214 upmap->header[5].type = UPTYPE_INVFORK;
1215 upmap->header[5].offset = offsetof(struct sys_upmap, invfork);
1217 upmap->version = UPMAP_VERSION;
1218 upmap->pid = p->p_pid;
1219 upmap->forkid = p->p_forkid;
1220 upmap->invfork = invfork;
1223 kfree(upmap, M_PROC);
1225 lwkt_reltoken(&p->p_token);
1229 proc_userunmap(struct proc *p)
1231 struct sys_upmap *upmap;
1233 lwkt_gettoken(&p->p_token);
1234 if ((upmap = p->p_upmap) != NULL) {
1236 kfree(upmap, M_PROC);
1238 lwkt_reltoken(&p->p_token);
1242 * Scan all processes on the allproc list. The process is automatically
1243 * held for the callback. A return value of -1 terminates the loop.
1244 * Zombie procs are skipped.
1246 * The callback is made with the process held and proc_token held.
1248 * We limit the scan to the number of processes as-of the start of
1249 * the scan so as not to get caught up in an endless loop if new processes
1250 * are created more quickly than we can scan the old ones. Add a little
1251 * slop to try to catch edge cases since nprocs can race.
1256 allproc_scan(int (*callback)(struct proc *, void *), void *data)
1258 int limit = nprocs + ncpus;
1264 * prg->proc_token protects the allproc list and PHOLD() prevents the
1265 * process from being removed from the allproc list or the zombproc
1268 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1269 procglob_t *prg = &procglob[n];
1270 if (LIST_FIRST(&prg->allproc) == NULL)
1272 lwkt_gettoken(&prg->proc_token);
1273 LIST_FOREACH(p, &prg->allproc, p_list) {
1274 if (p->p_stat == SZOMB)
1277 r = callback(p, data);
1284 lwkt_reltoken(&prg->proc_token);
1287 * Check if asked to stop early
1295 * Scan all lwps of processes on the allproc list. The lwp is automatically
1296 * held for the callback. A return value of -1 terminates the loop.
1298 * The callback is made with the proces and lwp both held, and proc_token held.
1303 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
1310 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1311 procglob_t *prg = &procglob[n];
1313 if (LIST_FIRST(&prg->allproc) == NULL)
1315 lwkt_gettoken(&prg->proc_token);
1316 LIST_FOREACH(p, &prg->allproc, p_list) {
1317 if (p->p_stat == SZOMB)
1320 lwkt_gettoken(&p->p_token);
1321 FOREACH_LWP_IN_PROC(lp, p) {
1323 r = callback(lp, data);
1326 lwkt_reltoken(&p->p_token);
1331 lwkt_reltoken(&prg->proc_token);
1334 * Asked to exit early
1342 * Scan all processes on the zombproc list. The process is automatically
1343 * held for the callback. A return value of -1 terminates the loop.
1346 * The callback is made with the proces held and proc_token held.
1349 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
1356 * prg->proc_token protects the allproc list and PHOLD() prevents the
1357 * process from being removed from the allproc list or the zombproc
1360 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1361 procglob_t *prg = &procglob[n];
1363 if (LIST_FIRST(&prg->allproc) == NULL)
1365 lwkt_gettoken(&prg->proc_token);
1366 LIST_FOREACH(p, &prg->allproc, p_list) {
1367 if (p->p_stat != SZOMB)
1370 r = callback(p, data);
1375 lwkt_reltoken(&prg->proc_token);
1378 * Check if asked to stop early
1385 #include "opt_ddb.h"
1387 #include <ddb/ddb.h>
1392 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1399 for (i = 0; i < ALLPROC_HSIZE; ++i) {
1402 if (LIST_EMPTY(&prg->allpgrp))
1404 kprintf("\tindx %d\n", i);
1405 LIST_FOREACH(pgrp, &prg->allpgrp, pg_list) {
1406 kprintf("\tpgrp %p, pgid %ld, sess %p, "
1407 "sesscnt %d, mem %p\n",
1408 (void *)pgrp, (long)pgrp->pg_id,
1409 (void *)pgrp->pg_session,
1410 pgrp->pg_session->s_count,
1411 (void *)LIST_FIRST(&pgrp->pg_members));
1412 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1413 kprintf("\t\tpid %ld addr %p pgrp %p\n",
1414 (long)p->p_pid, (void *)p,
1423 * The caller must hold proc_token.
1426 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1428 struct kinfo_proc ki;
1430 int skp = 0, had_output = 0;
1433 bzero(&ki, sizeof(ki));
1434 lwkt_gettoken_shared(&p->p_token);
1435 fill_kinfo_proc(p, &ki);
1436 if ((flags & KERN_PROC_FLAG_LWP) == 0)
1439 FOREACH_LWP_IN_PROC(lp, p) {
1441 fill_kinfo_lwp(lp, &ki.kp_lwp);
1443 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1450 lwkt_reltoken(&p->p_token);
1451 /* We need to output at least the proc, even if there is no lwp. */
1452 if (had_output == 0) {
1453 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1459 * The caller must hold proc_token.
1462 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req)
1464 struct kinfo_proc ki;
1467 fill_kinfo_proc_kthread(td, &ki);
1468 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1478 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1480 int *name = (int *)arg1;
1481 int oid = oidp->oid_number;
1482 u_int namelen = arg2;
1485 struct thread *marker;
1490 struct ucred *cr1 = curproc->p_ucred;
1492 flags = oid & KERN_PROC_FLAGMASK;
1493 oid &= ~KERN_PROC_FLAGMASK;
1495 if ((oid == KERN_PROC_ALL && namelen != 0) ||
1496 (oid != KERN_PROC_ALL && namelen != 1)) {
1501 * proc_token protects the allproc list and PHOLD() prevents the
1502 * process from being removed from the allproc list or the zombproc
1505 if (oid == KERN_PROC_PID) {
1506 p = pfind((pid_t)name[0]);
1508 if (PRISON_CHECK(cr1, p->p_ucred))
1509 error = sysctl_out_proc(p, req, flags);
1517 /* overestimate by 5 procs */
1518 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1523 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1524 procglob_t *prg = &procglob[n];
1526 if (LIST_EMPTY(&prg->allproc))
1528 lwkt_gettoken_shared(&prg->proc_token);
1529 LIST_FOREACH(p, &prg->allproc, p_list) {
1531 * Show a user only their processes.
1533 if ((!ps_showallprocs) &&
1534 (p->p_ucred == NULL || p_trespass(cr1, p->p_ucred))) {
1538 * Skip embryonic processes.
1540 if (p->p_stat == SIDL)
1543 * TODO - make more efficient (see notes below).
1547 case KERN_PROC_PGRP:
1548 /* could do this by traversing pgrp */
1549 if (p->p_pgrp == NULL ||
1550 p->p_pgrp->pg_id != (pid_t)name[0])
1555 if ((p->p_flags & P_CONTROLT) == 0 ||
1556 p->p_session == NULL ||
1557 p->p_session->s_ttyp == NULL ||
1558 dev2udev(p->p_session->s_ttyp->t_dev) !=
1564 if (p->p_ucred == NULL ||
1565 p->p_ucred->cr_uid != (uid_t)name[0])
1569 case KERN_PROC_RUID:
1570 if (p->p_ucred == NULL ||
1571 p->p_ucred->cr_ruid != (uid_t)name[0])
1576 if (!PRISON_CHECK(cr1, p->p_ucred))
1579 error = sysctl_out_proc(p, req, flags);
1582 lwkt_reltoken(&prg->proc_token);
1586 lwkt_reltoken(&prg->proc_token);
1590 * Iterate over all active cpus and scan their thread list. Start
1591 * with the next logical cpu and end with our original cpu. We
1592 * migrate our own thread to each target cpu in order to safely scan
1593 * its thread list. In the last loop we migrate back to our original
1596 origcpu = mycpu->gd_cpuid;
1597 if (!ps_showallthreads || jailed(cr1))
1600 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1601 marker->td_flags = TDF_MARKER;
1604 for (n = 1; n <= ncpus; ++n) {
1608 nid = (origcpu + n) % ncpus;
1609 if (CPUMASK_TESTBIT(smp_active_mask, nid) == 0)
1611 rgd = globaldata_find(nid);
1612 lwkt_setcpu_self(rgd);
1615 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1617 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1618 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1619 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1620 if (td->td_flags & TDF_MARKER)
1629 case KERN_PROC_PGRP:
1632 case KERN_PROC_RUID:
1635 error = sysctl_out_proc_kthread(td, req);
1643 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1651 * Userland scheduler expects us to return on the same cpu we
1654 if (mycpu->gd_cpuid != origcpu)
1655 lwkt_setcpu_self(globaldata_find(origcpu));
1657 kfree(marker, M_TEMP);
1664 * This sysctl allows a process to retrieve the argument list or process
1665 * title for another process without groping around in the address space
1666 * of the other process. It also allow a process to set its own "process
1667 * title to a string of its own choice.
1672 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1674 int *name = (int*) arg1;
1675 u_int namelen = arg2;
1680 struct ucred *cr1 = curproc->p_ucred;
1685 p = pfind((pid_t)name[0]);
1688 lwkt_gettoken(&p->p_token);
1690 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1693 if (req->newptr && curproc != p) {
1698 if (p->p_upmap != NULL && p->p_upmap->proc_title[0]) {
1700 * Args set via writable user process mmap.
1701 * We must calculate the string length manually
1702 * because the user data can change at any time.
1707 base = p->p_upmap->proc_title;
1708 for (n = 0; n < UPMAP_MAXPROCTITLE - 1; ++n) {
1712 error = SYSCTL_OUT(req, base, n);
1714 error = SYSCTL_OUT(req, "", 1);
1715 } else if ((pa = p->p_args) != NULL) {
1717 * Args set by setproctitle() sysctl.
1719 refcount_acquire(&pa->ar_ref);
1720 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1721 if (refcount_release(&pa->ar_ref))
1725 if (req->newptr == NULL)
1728 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1732 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1733 refcount_init(&pa->ar_ref, 1);
1734 pa->ar_length = req->newlen;
1735 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1743 * Replace p_args with the new pa. p_args may have previously
1750 KKASSERT(opa->ar_ref > 0);
1751 if (refcount_release(&opa->ar_ref)) {
1752 kfree(opa, M_PARGS);
1758 lwkt_reltoken(&p->p_token);
1765 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1767 int *name = (int*) arg1;
1768 u_int namelen = arg2;
1771 char *fullpath, *freepath;
1772 struct ucred *cr1 = curproc->p_ucred;
1777 p = pfind((pid_t)name[0]);
1780 lwkt_gettoken_shared(&p->p_token);
1783 * If we are not allowed to see other args, we certainly shouldn't
1784 * get the cwd either. Also check the usual trespassing.
1786 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1789 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1790 struct nchandle nch;
1792 cache_copy(&p->p_fd->fd_ncdir, &nch);
1793 error = cache_fullpath(p, &nch, NULL,
1794 &fullpath, &freepath, 0);
1798 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1799 kfree(freepath, M_TEMP);
1804 lwkt_reltoken(&p->p_token);
1811 * This sysctl allows a process to retrieve the path of the executable for
1812 * itself or another process.
1815 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1817 pid_t *pidp = (pid_t *)arg1;
1818 unsigned int arglen = arg2;
1820 char *retbuf, *freebuf;
1822 struct nchandle nch;
1826 if (*pidp == -1) { /* -1 means this process */
1834 cache_copy(&p->p_textnch, &nch);
1835 error = cache_fullpath(p, &nch, NULL, &retbuf, &freebuf, 0);
1839 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1840 kfree(freebuf, M_TEMP);
1848 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1850 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1851 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1853 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1854 sysctl_kern_proc, "Process table");
1856 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1857 sysctl_kern_proc, "Process table");
1859 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1860 sysctl_kern_proc, "Process table");
1862 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1863 sysctl_kern_proc, "Process table");
1865 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1866 sysctl_kern_proc, "Process table");
1868 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1869 sysctl_kern_proc, "Process table");
1871 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1872 sysctl_kern_proc, "Process table");
1874 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1875 sysctl_kern_proc, "Process table");
1877 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1878 sysctl_kern_proc, "Process table");
1880 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1881 sysctl_kern_proc, "Process table");
1883 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1884 sysctl_kern_proc, "Process table");
1886 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1887 sysctl_kern_proc_args, "Process argument list");
1889 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1890 sysctl_kern_proc_cwd, "Process argument list");
1892 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
1893 sysctl_kern_proc_pathname, "Process executable path");