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>
54 #include <sys/mplock2.h>
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.
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)
69 int allproc_hsize = ALLPROC_HSIZE;
71 LIST_HEAD(pidhashhead, proc);
73 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
74 MALLOC_DEFINE(M_SESSION, "session", "session header");
75 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
76 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
77 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
79 int ps_showallprocs = 1;
80 static int ps_showallthreads = 1;
81 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
83 "Unprivileged processes can see processes with different UID/GID");
84 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
85 &ps_showallthreads, 0,
86 "Unprivileged processes can see kernel threads");
88 static void orphanpg(struct pgrp *pg);
89 static void proc_makepid(struct proc *p, int random_offset);
94 static struct lwkt_token proc_tokens[ALLPROC_HSIZE];
95 static struct proclist allprocs[ALLPROC_HSIZE]; /* locked by proc_tokens */
96 static struct pgrplist allpgrps[ALLPROC_HSIZE]; /* locked by proc_tokens */
97 static struct sesslist allsessn[ALLPROC_HSIZE]; /* locked by proc_tokens */
100 * Random component to nextpid generation. We mix in a random factor to make
101 * it a little harder to predict. We sanity check the modulus value to avoid
102 * doing it in critical paths. Don't let it be too small or we pointlessly
103 * waste randomness entropy, and don't let it be impossibly large. Using a
104 * modulus that is too big causes a LOT more process table scans and slows
105 * down fork processing as the pidchecked caching is defeated.
107 static int randompid = 0;
113 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
118 error = sysctl_handle_int(oidp, &pid, 0, req);
119 if (error || !req->newptr)
121 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
123 else if (pid < 2) /* NOP */
125 else if (pid < 100) /* Make it reasonable */
131 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
132 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
135 * Initialize global process hashing structures.
137 * These functions are ONLY called from the low level boot code and do
138 * not lock their operations.
145 for (i = 0; i < ALLPROC_HSIZE; ++i) {
146 LIST_INIT(&allprocs[i]);
147 LIST_INIT(&allsessn[i]);
148 LIST_INIT(&allpgrps[i]);
149 lwkt_token_init(&proc_tokens[i], "allproc");
156 procinsertinit(struct proc *p)
158 LIST_INSERT_HEAD(&allprocs[ALLPROC_HASH(p->p_pid)], p, p_list);
162 pgrpinsertinit(struct pgrp *pg)
164 LIST_INSERT_HEAD(&allpgrps[ALLPROC_HASH(pg->pg_id)], pg, pg_list);
168 sessinsertinit(struct session *sess)
170 LIST_INSERT_HEAD(&allsessn[ALLPROC_HASH(sess->s_sid)], sess, s_list);
174 * Process hold/release support functions. Called via the PHOLD(),
175 * PRELE(), and PSTALL() macros.
177 * p->p_lock is a simple hold count with a waiting interlock. No wakeup()
178 * is issued unless someone is actually waiting for the process.
180 * Most holds are short-term, allowing a process scan or other similar
181 * operation to access a proc structure without it getting ripped out from
182 * under us. procfs and process-list sysctl ops also use the hold function
183 * interlocked with various p_flags to keep the vmspace intact when reading
184 * or writing a user process's address space.
186 * There are two situations where a hold count can be longer. Exiting lwps
187 * hold the process until the lwp is reaped, and the parent will hold the
188 * child during vfork()/exec() sequences while the child is marked P_PPWAIT.
190 * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at
191 * various critical points in the fork/exec and exit paths before proceeding.
193 #define PLOCK_ZOMB 0x20000000
194 #define PLOCK_WAITING 0x40000000
195 #define PLOCK_MASK 0x1FFFFFFF
198 pstall(struct proc *p, const char *wmesg, int count)
206 if ((o & PLOCK_MASK) <= count)
208 n = o | PLOCK_WAITING;
209 tsleep_interlock(&p->p_lock, 0);
212 * If someone is trying to single-step the process during
213 * an exec or an exit they can deadlock us because procfs
214 * sleeps with the process held.
217 if (p->p_flags & P_INEXEC) {
219 } else if (p->p_flags & P_POSTEXIT) {
220 spin_lock(&p->p_spin);
223 spin_unlock(&p->p_spin);
228 if (atomic_cmpset_int(&p->p_lock, o, n)) {
229 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
235 phold(struct proc *p)
237 atomic_add_int(&p->p_lock, 1);
241 * WARNING! On last release (p) can become instantly invalid due to
245 prele(struct proc *p)
253 if (atomic_cmpset_int(&p->p_lock, 1, 0))
261 KKASSERT((o & PLOCK_MASK) > 0);
263 n = (o - 1) & ~PLOCK_WAITING;
264 if (atomic_cmpset_int(&p->p_lock, o, n)) {
265 if (o & PLOCK_WAITING)
273 * Hold and flag serialized for zombie reaping purposes.
275 * This function will fail if it has to block, returning non-zero with
276 * neither the flag set or the hold count bumped. Note that we must block
277 * without holding a ref, meaning that the caller must ensure that (p)
278 * remains valid through some other interlock (typically on its parent
279 * process's p_token).
281 * Zero is returned on success. The hold count will be incremented and
282 * the serialization flag acquired. Note that serialization is only against
283 * other pholdzomb() calls, not against phold() calls.
286 pholdzomb(struct proc *p)
294 if (atomic_cmpset_int(&p->p_lock, 0, PLOCK_ZOMB | 1))
303 if ((o & PLOCK_ZOMB) == 0) {
304 n = (o + 1) | PLOCK_ZOMB;
305 if (atomic_cmpset_int(&p->p_lock, o, n))
308 KKASSERT((o & PLOCK_MASK) > 0);
309 n = o | PLOCK_WAITING;
310 tsleep_interlock(&p->p_lock, 0);
311 if (atomic_cmpset_int(&p->p_lock, o, n)) {
312 tsleep(&p->p_lock, PINTERLOCKED, "phldz", 0);
313 /* (p) can be ripped out at this point */
321 * Release PLOCK_ZOMB and the hold count, waking up any waiters.
323 * WARNING! On last release (p) can become instantly invalid due to
327 prelezomb(struct proc *p)
335 if (atomic_cmpset_int(&p->p_lock, PLOCK_ZOMB | 1, 0))
341 KKASSERT(p->p_lock & PLOCK_ZOMB);
344 KKASSERT((o & PLOCK_MASK) > 0);
346 n = (o - 1) & ~(PLOCK_ZOMB | PLOCK_WAITING);
347 if (atomic_cmpset_int(&p->p_lock, o, n)) {
348 if (o & PLOCK_WAITING)
356 * Is p an inferior of the current process?
361 inferior(struct proc *p)
366 lwkt_gettoken_shared(&p->p_token);
367 while (p != curproc) {
369 lwkt_reltoken(&p->p_token);
374 lwkt_reltoken(&p->p_token);
376 lwkt_gettoken_shared(&p2->p_token);
379 lwkt_reltoken(&p->p_token);
386 * Locate a process by number. The returned process will be referenced and
387 * must be released with PRELE().
394 struct proc *p = curproc;
398 * Shortcut the current process
400 if (p && p->p_pid == pid) {
406 * Otherwise find it in the hash table.
408 n = ALLPROC_HASH(pid);
410 lwkt_gettoken_shared(&proc_tokens[n]);
411 LIST_FOREACH(p, &allprocs[n], p_list) {
412 if (p->p_stat == SZOMB)
414 if (p->p_pid == pid) {
416 lwkt_reltoken(&proc_tokens[n]);
420 lwkt_reltoken(&proc_tokens[n]);
426 * Locate a process by number. The returned process is NOT referenced.
427 * The result will not be stable and is typically only used to validate
428 * against a process that the caller has in-hand.
435 struct proc *p = curproc;
439 * Shortcut the current process
441 if (p && p->p_pid == pid)
445 * Otherwise find it in the hash table.
447 n = ALLPROC_HASH(pid);
449 lwkt_gettoken_shared(&proc_tokens[n]);
450 LIST_FOREACH(p, &allprocs[n], p_list) {
451 if (p->p_stat == SZOMB)
453 if (p->p_pid == pid) {
454 lwkt_reltoken(&proc_tokens[n]);
458 lwkt_reltoken(&proc_tokens[n]);
464 * Locate a process on the zombie list. Return a process or NULL.
465 * The returned process will be referenced and the caller must release
468 * No other requirements.
473 struct proc *p = curproc;
477 * Shortcut the current process
479 if (p && p->p_pid == pid) {
485 * Otherwise find it in the hash table.
487 n = ALLPROC_HASH(pid);
489 lwkt_gettoken_shared(&proc_tokens[n]);
490 LIST_FOREACH(p, &allprocs[n], p_list) {
491 if (p->p_stat != SZOMB)
493 if (p->p_pid == pid) {
495 lwkt_reltoken(&proc_tokens[n]);
499 lwkt_reltoken(&proc_tokens[n]);
506 pgref(struct pgrp *pgrp)
508 refcount_acquire(&pgrp->pg_refs);
512 pgrel(struct pgrp *pgrp)
517 n = PGRP_HASH(pgrp->pg_id);
519 count = pgrp->pg_refs;
523 lwkt_gettoken(&proc_tokens[n]);
524 if (atomic_cmpset_int(&pgrp->pg_refs, 1, 0))
526 lwkt_reltoken(&proc_tokens[n]);
529 if (atomic_cmpset_int(&pgrp->pg_refs, count, count - 1))
536 * Successful 1->0 transition, pghash_spin is held.
538 LIST_REMOVE(pgrp, pg_list);
541 * Reset any sigio structures pointing to us as a result of
542 * F_SETOWN with our pgid.
544 funsetownlst(&pgrp->pg_sigiolst);
546 if (pgrp->pg_session->s_ttyp != NULL &&
547 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
548 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
550 lwkt_reltoken(&proc_tokens[n]);
552 sess_rele(pgrp->pg_session);
557 * Locate a process group by number. The returned process group will be
558 * referenced w/pgref() and must be released with pgrel() (or assigned
559 * somewhere if you wish to keep the reference).
570 lwkt_gettoken_shared(&proc_tokens[n]);
572 LIST_FOREACH(pgrp, &allpgrps[n], pg_list) {
573 if (pgrp->pg_id == pgid) {
574 refcount_acquire(&pgrp->pg_refs);
575 lwkt_reltoken(&proc_tokens[n]);
579 lwkt_reltoken(&proc_tokens[n]);
584 * Move p to a new or existing process group (and session)
589 enterpgrp(struct proc *p, pid_t pgid, int mksess)
597 KASSERT(pgrp == NULL || !mksess,
598 ("enterpgrp: setsid into non-empty pgrp"));
599 KASSERT(!SESS_LEADER(p),
600 ("enterpgrp: session leader attempted setpgrp"));
603 pid_t savepid = p->p_pid;
610 KASSERT(p->p_pid == pgid,
611 ("enterpgrp: new pgrp and pid != pgid"));
612 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
614 LIST_INIT(&pgrp->pg_members);
616 SLIST_INIT(&pgrp->pg_sigiolst);
617 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
618 refcount_init(&pgrp->pg_refs, 1);
619 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
623 if ((np = pfindn(savepid)) == NULL || np != p) {
624 lwkt_reltoken(&proc_tokens[n]);
630 lwkt_gettoken(&proc_tokens[n]);
632 struct session *sess;
637 sess = kmalloc(sizeof(struct session), M_SESSION,
639 lwkt_gettoken(&p->p_token);
641 sess->s_sid = p->p_pid;
643 sess->s_ttyvp = NULL;
645 bcopy(p->p_session->s_login, sess->s_login,
646 sizeof(sess->s_login));
647 pgrp->pg_session = sess;
648 KASSERT(p == curproc,
649 ("enterpgrp: mksession and p != curproc"));
650 p->p_flags &= ~P_CONTROLT;
651 LIST_INSERT_HEAD(&allsessn[n], sess, s_list);
652 lwkt_reltoken(&p->p_token);
654 lwkt_gettoken(&p->p_token);
655 pgrp->pg_session = p->p_session;
656 sess_hold(pgrp->pg_session);
657 lwkt_reltoken(&p->p_token);
659 LIST_INSERT_HEAD(&allpgrps[n], pgrp, pg_list);
661 lwkt_reltoken(&proc_tokens[n]);
662 } else if (pgrp == p->p_pgrp) {
665 } /* else pgfind() referenced the pgrp */
667 lwkt_gettoken(&pgrp->pg_token);
668 lwkt_gettoken(&p->p_token);
671 * Replace p->p_pgrp, handling any races that occur.
673 while ((opgrp = p->p_pgrp) != NULL) {
675 lwkt_gettoken(&opgrp->pg_token);
676 if (opgrp != p->p_pgrp) {
677 lwkt_reltoken(&opgrp->pg_token);
681 LIST_REMOVE(p, p_pglist);
685 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
688 * Adjust eligibility of affected pgrps to participate in job control.
689 * Increment eligibility counts before decrementing, otherwise we
690 * could reach 0 spuriously during the first call.
694 fixjobc(p, opgrp, 0);
695 lwkt_reltoken(&opgrp->pg_token);
696 pgrel(opgrp); /* manual pgref */
697 pgrel(opgrp); /* p->p_pgrp ref */
699 lwkt_reltoken(&p->p_token);
700 lwkt_reltoken(&pgrp->pg_token);
708 * Remove process from process group
713 leavepgrp(struct proc *p)
715 struct pgrp *pg = p->p_pgrp;
717 lwkt_gettoken(&p->p_token);
718 while ((pg = p->p_pgrp) != NULL) {
720 lwkt_gettoken(&pg->pg_token);
721 if (p->p_pgrp != pg) {
722 lwkt_reltoken(&pg->pg_token);
727 LIST_REMOVE(p, p_pglist);
728 lwkt_reltoken(&pg->pg_token);
729 pgrel(pg); /* manual pgref */
730 pgrel(pg); /* p->p_pgrp ref */
733 lwkt_reltoken(&p->p_token);
739 * Adjust the ref count on a session structure. When the ref count falls to
740 * zero the tty is disassociated from the session and the session structure
741 * is freed. Note that tty assocation is not itself ref-counted.
746 sess_hold(struct session *sp)
748 atomic_add_int(&sp->s_count, 1);
755 sess_rele(struct session *sess)
761 n = SESS_HASH(sess->s_sid);
763 count = sess->s_count;
767 lwkt_gettoken(&tty_token);
768 lwkt_gettoken(&proc_tokens[n]);
769 if (atomic_cmpset_int(&sess->s_count, 1, 0))
771 lwkt_reltoken(&proc_tokens[n]);
772 lwkt_reltoken(&tty_token);
775 if (atomic_cmpset_int(&sess->s_count, count, count - 1))
782 * Successful 1->0 transition and tty_token is held.
784 LIST_REMOVE(sess, s_list);
786 if (sess->s_ttyp && sess->s_ttyp->t_session) {
787 #ifdef TTY_DO_FULL_CLOSE
788 /* FULL CLOSE, see ttyclearsession() */
789 KKASSERT(sess->s_ttyp->t_session == sess);
790 sess->s_ttyp->t_session = NULL;
792 /* HALF CLOSE, see ttyclearsession() */
793 if (sess->s_ttyp->t_session == sess)
794 sess->s_ttyp->t_session = NULL;
797 if ((tp = sess->s_ttyp) != NULL) {
801 lwkt_reltoken(&proc_tokens[n]);
802 lwkt_reltoken(&tty_token);
804 kfree(sess, M_SESSION);
808 * Adjust pgrp jobc counters when specified process changes process group.
809 * We count the number of processes in each process group that "qualify"
810 * the group for terminal job control (those with a parent in a different
811 * process group of the same session). If that count reaches zero, the
812 * process group becomes orphaned. Check both the specified process'
813 * process group and that of its children.
814 * entering == 0 => p is leaving specified group.
815 * entering == 1 => p is entering specified group.
820 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
822 struct pgrp *hispgrp;
823 struct session *mysession;
827 * Check p's parent to see whether p qualifies its own process
828 * group; if so, adjust count for p's process group.
830 lwkt_gettoken(&p->p_token); /* p_children scan */
831 lwkt_gettoken(&pgrp->pg_token);
833 mysession = pgrp->pg_session;
834 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
835 hispgrp->pg_session == mysession) {
838 else if (--pgrp->pg_jobc == 0)
843 * Check this process' children to see whether they qualify
844 * their process groups; if so, adjust counts for children's
847 LIST_FOREACH(np, &p->p_children, p_sibling) {
849 lwkt_gettoken(&np->p_token);
850 if ((hispgrp = np->p_pgrp) != pgrp &&
851 hispgrp->pg_session == mysession &&
852 np->p_stat != SZOMB) {
854 lwkt_gettoken(&hispgrp->pg_token);
857 else if (--hispgrp->pg_jobc == 0)
859 lwkt_reltoken(&hispgrp->pg_token);
862 lwkt_reltoken(&np->p_token);
865 KKASSERT(pgrp->pg_refs > 0);
866 lwkt_reltoken(&pgrp->pg_token);
867 lwkt_reltoken(&p->p_token);
871 * A process group has become orphaned;
872 * if there are any stopped processes in the group,
873 * hang-up all process in that group.
875 * The caller must hold pg_token.
878 orphanpg(struct pgrp *pg)
882 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
883 if (p->p_stat == SSTOP) {
884 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
894 * Add a new process to the allproc list and the PID hash. This
895 * also assigns a pid to the new process.
900 proc_add_allproc(struct proc *p)
904 if ((random_offset = randompid) != 0) {
905 read_random(&random_offset, sizeof(random_offset));
906 random_offset = (random_offset & 0x7FFFFFFF) % randompid;
908 proc_makepid(p, random_offset);
912 * Calculate a new process pid. This function is integrated into
913 * proc_add_allproc() to guarentee that the new pid is not reused before
914 * the new process can be added to the allproc list.
916 * p_pid is assigned and the process is added to the allproc hash table
920 proc_makepid(struct proc *p, int random_offset)
922 static pid_t nextpid; /* heuristic, allowed to race */
925 struct session *sess;
930 * Calculate a hash index and find an unused process id within
931 * the table, looping if we cannot find one.
934 atomic_add_int(&nextpid, random_offset);
936 base = atomic_fetchadd_int(&nextpid, 1) + 1;
937 if (base >= PID_MAX) {
938 base = base % PID_MAX;
942 n = ALLPROC_HASH(base);
943 lwkt_gettoken(&proc_tokens[n]);
945 LIST_FOREACH(ps, &allprocs[n], p_list) {
946 if (ps->p_pid == base) {
947 base += ALLPROC_HSIZE;
948 if (base >= PID_MAX) {
949 lwkt_reltoken(&proc_tokens[n]);
954 LIST_FOREACH(pg, &allpgrps[n], pg_list) {
955 if (pg->pg_id == base) {
956 base += ALLPROC_HSIZE;
957 if (base >= PID_MAX) {
958 lwkt_reltoken(&proc_tokens[n]);
963 LIST_FOREACH(sess, &allsessn[n], s_list) {
964 if (sess->s_sid == base) {
965 base += ALLPROC_HSIZE;
966 if (base >= PID_MAX) {
967 lwkt_reltoken(&proc_tokens[n]);
974 * Assign the pid and insert the process.
977 LIST_INSERT_HEAD(&allprocs[n], p, p_list);
978 lwkt_reltoken(&proc_tokens[n]);
982 * Called from exit1 to place the process into a zombie state.
983 * The process is removed from the pid hash and p_stat is set
984 * to SZOMB. Normal pfind[n]() calls will not find it any more.
986 * Caller must hold p->p_token. We are required to wait until p_lock
987 * becomes zero before we can manipulate the list, allowing allproc
988 * scans to guarantee consistency during a list scan.
991 proc_move_allproc_zombie(struct proc *p)
995 n = ALLPROC_HASH(p->p_pid);
996 PSTALL(p, "reap1", 0);
997 lwkt_gettoken(&proc_tokens[n]);
999 PSTALL(p, "reap1a", 0);
1002 lwkt_reltoken(&proc_tokens[n]);
1003 dsched_exit_proc(p);
1007 * This routine is called from kern_wait() and will remove the process
1008 * from the zombie list and the sibling list. This routine will block
1009 * if someone has a lock on the proces (p_lock).
1011 * Caller must hold p->p_token. We are required to wait until p_lock
1012 * becomes zero before we can manipulate the list, allowing allproc
1013 * scans to guarantee consistency during a list scan.
1016 proc_remove_zombie(struct proc *p)
1020 n = ALLPROC_HASH(p->p_pid);
1022 PSTALL(p, "reap2", 0);
1023 lwkt_gettoken(&proc_tokens[n]);
1024 PSTALL(p, "reap2a", 0);
1025 LIST_REMOVE(p, p_list); /* from remove master list */
1026 LIST_REMOVE(p, p_sibling); /* and from sibling list */
1028 lwkt_reltoken(&proc_tokens[n]);
1032 * Handle various requirements prior to returning to usermode. Called from
1033 * platform trap and system call code.
1036 lwpuserret(struct lwp *lp)
1038 struct proc *p = lp->lwp_proc;
1040 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1041 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1044 if (lp->lwp_mpflags & LWP_MP_WEXIT) {
1045 lwkt_gettoken(&p->p_token);
1047 lwkt_reltoken(&p->p_token); /* NOT REACHED */
1052 * Kernel threads run from user processes can also accumulate deferred
1053 * actions which need to be acted upon. Callers include:
1055 * nfsd - Can allocate lots of vnodes
1058 lwpkthreaddeferred(void)
1060 struct lwp *lp = curthread->td_lwp;
1063 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1064 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1071 * Scan all processes on the allproc list. The process is automatically
1072 * held for the callback. A return value of -1 terminates the loop.
1073 * Zombie procs are skipped.
1075 * The callback is made with the process held and proc_token held.
1077 * We limit the scan to the number of processes as-of the start of
1078 * the scan so as not to get caught up in an endless loop if new processes
1079 * are created more quickly than we can scan the old ones. Add a little
1080 * slop to try to catch edge cases since nprocs can race.
1085 allproc_scan(int (*callback)(struct proc *, void *), void *data)
1087 int limit = nprocs + ncpus;
1093 * proc_tokens[n] protects the allproc list and PHOLD() prevents the
1094 * process from being removed from the allproc list or the zombproc
1097 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1098 if (LIST_FIRST(&allprocs[n]) == NULL)
1100 lwkt_gettoken(&proc_tokens[n]);
1101 LIST_FOREACH(p, &allprocs[n], p_list) {
1102 if (p->p_stat == SZOMB)
1105 r = callback(p, data);
1112 lwkt_reltoken(&proc_tokens[n]);
1115 * Check if asked to stop early
1123 * Scan all lwps of processes on the allproc list. The lwp is automatically
1124 * held for the callback. A return value of -1 terminates the loop.
1126 * The callback is made with the proces and lwp both held, and proc_token held.
1131 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
1138 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1139 if (LIST_FIRST(&allprocs[n]) == NULL)
1141 lwkt_gettoken(&proc_tokens[n]);
1142 LIST_FOREACH(p, &allprocs[n], p_list) {
1143 if (p->p_stat == SZOMB)
1146 lwkt_gettoken(&p->p_token);
1147 FOREACH_LWP_IN_PROC(lp, p) {
1149 r = callback(lp, data);
1152 lwkt_reltoken(&p->p_token);
1157 lwkt_reltoken(&proc_tokens[n]);
1160 * Asked to exit early
1168 * Scan all processes on the zombproc list. The process is automatically
1169 * held for the callback. A return value of -1 terminates the loop.
1172 * The callback is made with the proces held and proc_token held.
1175 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
1182 * proc_tokens[n] protects the allproc list and PHOLD() prevents the
1183 * process from being removed from the allproc list or the zombproc
1186 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1187 if (LIST_FIRST(&allprocs[n]) == NULL)
1189 lwkt_gettoken(&proc_tokens[n]);
1190 LIST_FOREACH(p, &allprocs[n], p_list) {
1191 if (p->p_stat != SZOMB)
1194 r = callback(p, data);
1199 lwkt_reltoken(&proc_tokens[n]);
1202 * Check if asked to stop early
1209 #include "opt_ddb.h"
1211 #include <ddb/ddb.h>
1216 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1222 for (i = 0; i < ALLPROC_HSIZE; ++i) {
1223 if (LIST_EMPTY(&allpgrps[i]))
1225 kprintf("\tindx %d\n", i);
1226 LIST_FOREACH(pgrp, &allpgrps[i], pg_list) {
1227 kprintf("\tpgrp %p, pgid %ld, sess %p, "
1228 "sesscnt %d, mem %p\n",
1229 (void *)pgrp, (long)pgrp->pg_id,
1230 (void *)pgrp->pg_session,
1231 pgrp->pg_session->s_count,
1232 (void *)LIST_FIRST(&pgrp->pg_members));
1233 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1234 kprintf("\t\tpid %ld addr %p pgrp %p\n",
1235 (long)p->p_pid, (void *)p,
1244 * The caller must hold proc_token.
1247 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1249 struct kinfo_proc ki;
1251 int skp = 0, had_output = 0;
1254 bzero(&ki, sizeof(ki));
1255 lwkt_gettoken_shared(&p->p_token);
1256 fill_kinfo_proc(p, &ki);
1257 if ((flags & KERN_PROC_FLAG_LWP) == 0)
1260 FOREACH_LWP_IN_PROC(lp, p) {
1262 fill_kinfo_lwp(lp, &ki.kp_lwp);
1264 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1271 lwkt_reltoken(&p->p_token);
1272 /* We need to output at least the proc, even if there is no lwp. */
1273 if (had_output == 0) {
1274 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1280 * The caller must hold proc_token.
1283 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req)
1285 struct kinfo_proc ki;
1288 fill_kinfo_proc_kthread(td, &ki);
1289 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1299 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1301 int *name = (int *)arg1;
1302 int oid = oidp->oid_number;
1303 u_int namelen = arg2;
1306 struct thread *marker;
1311 struct ucred *cr1 = curproc->p_ucred;
1313 flags = oid & KERN_PROC_FLAGMASK;
1314 oid &= ~KERN_PROC_FLAGMASK;
1316 if ((oid == KERN_PROC_ALL && namelen != 0) ||
1317 (oid != KERN_PROC_ALL && namelen != 1)) {
1322 * proc_token protects the allproc list and PHOLD() prevents the
1323 * process from being removed from the allproc list or the zombproc
1326 if (oid == KERN_PROC_PID) {
1327 p = pfind((pid_t)name[0]);
1329 if (PRISON_CHECK(cr1, p->p_ucred))
1330 error = sysctl_out_proc(p, req, flags);
1338 /* overestimate by 5 procs */
1339 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1344 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1345 if (LIST_EMPTY(&allprocs[n]))
1347 lwkt_gettoken_shared(&proc_tokens[n]);
1348 LIST_FOREACH(p, &allprocs[n], p_list) {
1350 * Show a user only their processes.
1352 if ((!ps_showallprocs) &&
1353 (p->p_ucred == NULL || p_trespass(cr1, p->p_ucred))) {
1357 * Skip embryonic processes.
1359 if (p->p_stat == SIDL)
1362 * TODO - make more efficient (see notes below).
1366 case KERN_PROC_PGRP:
1367 /* could do this by traversing pgrp */
1368 if (p->p_pgrp == NULL ||
1369 p->p_pgrp->pg_id != (pid_t)name[0])
1374 if ((p->p_flags & P_CONTROLT) == 0 ||
1375 p->p_session == NULL ||
1376 p->p_session->s_ttyp == NULL ||
1377 dev2udev(p->p_session->s_ttyp->t_dev) !=
1383 if (p->p_ucred == NULL ||
1384 p->p_ucred->cr_uid != (uid_t)name[0])
1388 case KERN_PROC_RUID:
1389 if (p->p_ucred == NULL ||
1390 p->p_ucred->cr_ruid != (uid_t)name[0])
1395 if (!PRISON_CHECK(cr1, p->p_ucred))
1398 error = sysctl_out_proc(p, req, flags);
1401 lwkt_reltoken(&proc_tokens[n]);
1405 lwkt_reltoken(&proc_tokens[n]);
1409 * Iterate over all active cpus and scan their thread list. Start
1410 * with the next logical cpu and end with our original cpu. We
1411 * migrate our own thread to each target cpu in order to safely scan
1412 * its thread list. In the last loop we migrate back to our original
1415 origcpu = mycpu->gd_cpuid;
1416 if (!ps_showallthreads || jailed(cr1))
1419 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1420 marker->td_flags = TDF_MARKER;
1423 for (n = 1; n <= ncpus; ++n) {
1427 nid = (origcpu + n) % ncpus;
1428 if ((smp_active_mask & CPUMASK(nid)) == 0)
1430 rgd = globaldata_find(nid);
1431 lwkt_setcpu_self(rgd);
1434 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1436 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1437 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1438 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1439 if (td->td_flags & TDF_MARKER)
1448 case KERN_PROC_PGRP:
1451 case KERN_PROC_RUID:
1454 error = sysctl_out_proc_kthread(td, req);
1462 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1470 * Userland scheduler expects us to return on the same cpu we
1473 if (mycpu->gd_cpuid != origcpu)
1474 lwkt_setcpu_self(globaldata_find(origcpu));
1476 kfree(marker, M_TEMP);
1483 * This sysctl allows a process to retrieve the argument list or process
1484 * title for another process without groping around in the address space
1485 * of the other process. It also allow a process to set its own "process
1486 * title to a string of its own choice.
1491 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1493 int *name = (int*) arg1;
1494 u_int namelen = arg2;
1499 struct ucred *cr1 = curproc->p_ucred;
1504 p = pfind((pid_t)name[0]);
1507 lwkt_gettoken(&p->p_token);
1509 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1512 if (req->newptr && curproc != p) {
1516 if (req->oldptr && (pa = p->p_args) != NULL) {
1517 refcount_acquire(&pa->ar_ref);
1518 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1519 if (refcount_release(&pa->ar_ref))
1522 if (req->newptr == NULL)
1525 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1529 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1530 refcount_init(&pa->ar_ref, 1);
1531 pa->ar_length = req->newlen;
1532 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1540 * Replace p_args with the new pa. p_args may have previously
1547 KKASSERT(opa->ar_ref > 0);
1548 if (refcount_release(&opa->ar_ref)) {
1549 kfree(opa, M_PARGS);
1555 lwkt_reltoken(&p->p_token);
1562 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1564 int *name = (int*) arg1;
1565 u_int namelen = arg2;
1568 char *fullpath, *freepath;
1569 struct ucred *cr1 = curproc->p_ucred;
1574 p = pfind((pid_t)name[0]);
1577 lwkt_gettoken_shared(&p->p_token);
1580 * If we are not allowed to see other args, we certainly shouldn't
1581 * get the cwd either. Also check the usual trespassing.
1583 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1586 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1587 struct nchandle nch;
1589 cache_copy(&p->p_fd->fd_ncdir, &nch);
1590 error = cache_fullpath(p, &nch, NULL,
1591 &fullpath, &freepath, 0);
1595 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1596 kfree(freepath, M_TEMP);
1601 lwkt_reltoken(&p->p_token);
1608 * This sysctl allows a process to retrieve the path of the executable for
1609 * itself or another process.
1612 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1614 pid_t *pidp = (pid_t *)arg1;
1615 unsigned int arglen = arg2;
1618 char *retbuf, *freebuf;
1623 if (*pidp == -1) { /* -1 means this process */
1636 error = vn_fullpath(p, vp, &retbuf, &freebuf, 0);
1640 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1641 kfree(freebuf, M_TEMP);
1649 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1651 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1652 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1654 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1655 sysctl_kern_proc, "Process table");
1657 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1658 sysctl_kern_proc, "Process table");
1660 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1661 sysctl_kern_proc, "Process table");
1663 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1664 sysctl_kern_proc, "Process table");
1666 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1667 sysctl_kern_proc, "Process table");
1669 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1670 sysctl_kern_proc, "Process table");
1672 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1673 sysctl_kern_proc, "Process table");
1675 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1676 sysctl_kern_proc, "Process table");
1678 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1679 sysctl_kern_proc, "Process table");
1681 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1682 sysctl_kern_proc, "Process table");
1684 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1685 sysctl_kern_proc, "Process table");
1687 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1688 sysctl_kern_proc_args, "Process argument list");
1690 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1691 sysctl_kern_proc_cwd, "Process argument list");
1693 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
1694 sysctl_kern_proc_pathname, "Process executable path");