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)
68 LIST_HEAD(pidhashhead, proc);
70 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
71 MALLOC_DEFINE(M_SESSION, "session", "session header");
72 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
73 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
74 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
76 int ps_showallprocs = 1;
77 static int ps_showallthreads = 1;
78 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
80 "Unprivileged processes can see processes with different UID/GID");
81 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
82 &ps_showallthreads, 0,
83 "Unprivileged processes can see kernel threads");
85 static void orphanpg(struct pgrp *pg);
86 static void proc_makepid(struct proc *p, int random_offset);
91 static struct lwkt_token proc_tokens[ALLPROC_HSIZE];
92 static struct proclist allprocs[ALLPROC_HSIZE]; /* locked by proc_tokens */
93 static struct pgrplist allpgrps[ALLPROC_HSIZE]; /* locked by proc_tokens */
94 static struct sesslist allsessn[ALLPROC_HSIZE]; /* locked by proc_tokens */
97 * Random component to nextpid generation. We mix in a random factor to make
98 * it a little harder to predict. We sanity check the modulus value to avoid
99 * doing it in critical paths. Don't let it be too small or we pointlessly
100 * waste randomness entropy, and don't let it be impossibly large. Using a
101 * modulus that is too big causes a LOT more process table scans and slows
102 * down fork processing as the pidchecked caching is defeated.
104 static int randompid = 0;
110 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
115 error = sysctl_handle_int(oidp, &pid, 0, req);
116 if (error || !req->newptr)
118 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
120 else if (pid < 2) /* NOP */
122 else if (pid < 100) /* Make it reasonable */
128 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
129 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
132 * Initialize global process hashing structures.
134 * These functions are ONLY called from the low level boot code and do
135 * not lock their operations.
142 for (i = 0; i < ALLPROC_HSIZE; ++i) {
143 LIST_INIT(&allprocs[i]);
144 LIST_INIT(&allsessn[i]);
145 LIST_INIT(&allpgrps[i]);
146 lwkt_token_init(&proc_tokens[i], "allproc");
153 procinsertinit(struct proc *p)
155 LIST_INSERT_HEAD(&allprocs[ALLPROC_HASH(p->p_pid)], p, p_list);
159 pgrpinsertinit(struct pgrp *pg)
161 LIST_INSERT_HEAD(&allpgrps[ALLPROC_HASH(pg->pg_id)], pg, pg_list);
165 sessinsertinit(struct session *sess)
167 LIST_INSERT_HEAD(&allsessn[ALLPROC_HASH(sess->s_sid)], sess, s_list);
171 * Process hold/release support functions. Called via the PHOLD(),
172 * PRELE(), and PSTALL() macros.
174 * p->p_lock is a simple hold count with a waiting interlock. No wakeup()
175 * is issued unless someone is actually waiting for the process.
177 * Most holds are short-term, allowing a process scan or other similar
178 * operation to access a proc structure without it getting ripped out from
179 * under us. procfs and process-list sysctl ops also use the hold function
180 * interlocked with various p_flags to keep the vmspace intact when reading
181 * or writing a user process's address space.
183 * There are two situations where a hold count can be longer. Exiting lwps
184 * hold the process until the lwp is reaped, and the parent will hold the
185 * child during vfork()/exec() sequences while the child is marked P_PPWAIT.
187 * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at
188 * various critical points in the fork/exec and exit paths before proceeding.
190 #define PLOCK_ZOMB 0x20000000
191 #define PLOCK_WAITING 0x40000000
192 #define PLOCK_MASK 0x1FFFFFFF
195 pstall(struct proc *p, const char *wmesg, int count)
203 if ((o & PLOCK_MASK) <= count)
205 n = o | PLOCK_WAITING;
206 tsleep_interlock(&p->p_lock, 0);
209 * If someone is trying to single-step the process during
210 * an exec or an exit they can deadlock us because procfs
211 * sleeps with the process held.
214 if (p->p_flags & P_INEXEC) {
216 } else if (p->p_flags & P_POSTEXIT) {
217 spin_lock(&p->p_spin);
220 spin_unlock(&p->p_spin);
225 if (atomic_cmpset_int(&p->p_lock, o, n)) {
226 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
232 phold(struct proc *p)
234 atomic_add_int(&p->p_lock, 1);
238 * WARNING! On last release (p) can become instantly invalid due to
242 prele(struct proc *p)
250 if (atomic_cmpset_int(&p->p_lock, 1, 0))
258 KKASSERT((o & PLOCK_MASK) > 0);
260 n = (o - 1) & ~PLOCK_WAITING;
261 if (atomic_cmpset_int(&p->p_lock, o, n)) {
262 if (o & PLOCK_WAITING)
270 * Hold and flag serialized for zombie reaping purposes.
272 * This function will fail if it has to block, returning non-zero with
273 * neither the flag set or the hold count bumped. Note that we must block
274 * without holding a ref, meaning that the caller must ensure that (p)
275 * remains valid through some other interlock (typically on its parent
276 * process's p_token).
278 * Zero is returned on success. The hold count will be incremented and
279 * the serialization flag acquired. Note that serialization is only against
280 * other pholdzomb() calls, not against phold() calls.
283 pholdzomb(struct proc *p)
291 if (atomic_cmpset_int(&p->p_lock, 0, PLOCK_ZOMB | 1))
300 if ((o & PLOCK_ZOMB) == 0) {
301 n = (o + 1) | PLOCK_ZOMB;
302 if (atomic_cmpset_int(&p->p_lock, o, n))
305 KKASSERT((o & PLOCK_MASK) > 0);
306 n = o | PLOCK_WAITING;
307 tsleep_interlock(&p->p_lock, 0);
308 if (atomic_cmpset_int(&p->p_lock, o, n)) {
309 tsleep(&p->p_lock, PINTERLOCKED, "phldz", 0);
310 /* (p) can be ripped out at this point */
318 * Release PLOCK_ZOMB and the hold count, waking up any waiters.
320 * WARNING! On last release (p) can become instantly invalid due to
324 prelezomb(struct proc *p)
332 if (atomic_cmpset_int(&p->p_lock, PLOCK_ZOMB | 1, 0))
338 KKASSERT(p->p_lock & PLOCK_ZOMB);
341 KKASSERT((o & PLOCK_MASK) > 0);
343 n = (o - 1) & ~(PLOCK_ZOMB | PLOCK_WAITING);
344 if (atomic_cmpset_int(&p->p_lock, o, n)) {
345 if (o & PLOCK_WAITING)
353 * Is p an inferior of the current process?
358 inferior(struct proc *p)
363 lwkt_gettoken_shared(&p->p_token);
364 while (p != curproc) {
366 lwkt_reltoken(&p->p_token);
371 lwkt_reltoken(&p->p_token);
373 lwkt_gettoken_shared(&p2->p_token);
376 lwkt_reltoken(&p->p_token);
383 * Locate a process by number. The returned process will be referenced and
384 * must be released with PRELE().
391 struct proc *p = curproc;
395 * Shortcut the current process
397 if (p && p->p_pid == pid) {
403 * Otherwise find it in the hash table.
405 n = ALLPROC_HASH(pid);
407 lwkt_gettoken_shared(&proc_tokens[n]);
408 LIST_FOREACH(p, &allprocs[n], p_list) {
409 if (p->p_stat == SZOMB)
411 if (p->p_pid == pid) {
413 lwkt_reltoken(&proc_tokens[n]);
417 lwkt_reltoken(&proc_tokens[n]);
423 * Locate a process by number. The returned process is NOT referenced.
424 * The result will not be stable and is typically only used to validate
425 * against a process that the caller has in-hand.
432 struct proc *p = curproc;
436 * Shortcut the current process
438 if (p && p->p_pid == pid)
442 * Otherwise find it in the hash table.
444 n = ALLPROC_HASH(pid);
446 lwkt_gettoken_shared(&proc_tokens[n]);
447 LIST_FOREACH(p, &allprocs[n], p_list) {
448 if (p->p_stat == SZOMB)
450 if (p->p_pid == pid) {
451 lwkt_reltoken(&proc_tokens[n]);
455 lwkt_reltoken(&proc_tokens[n]);
461 * Locate a process on the zombie list. Return a process or NULL.
462 * The returned process will be referenced and the caller must release
465 * No other requirements.
470 struct proc *p = curproc;
474 * Shortcut the current process
476 if (p && p->p_pid == pid) {
482 * Otherwise find it in the hash table.
484 n = ALLPROC_HASH(pid);
486 lwkt_gettoken_shared(&proc_tokens[n]);
487 LIST_FOREACH(p, &allprocs[n], p_list) {
488 if (p->p_stat != SZOMB)
490 if (p->p_pid == pid) {
492 lwkt_reltoken(&proc_tokens[n]);
496 lwkt_reltoken(&proc_tokens[n]);
503 pgref(struct pgrp *pgrp)
505 refcount_acquire(&pgrp->pg_refs);
509 pgrel(struct pgrp *pgrp)
514 n = PGRP_HASH(pgrp->pg_id);
516 count = pgrp->pg_refs;
520 lwkt_gettoken(&proc_tokens[n]);
521 if (atomic_cmpset_int(&pgrp->pg_refs, 1, 0))
523 lwkt_reltoken(&proc_tokens[n]);
526 if (atomic_cmpset_int(&pgrp->pg_refs, count, count - 1))
533 * Successful 1->0 transition, pghash_spin is held.
535 LIST_REMOVE(pgrp, pg_list);
538 * Reset any sigio structures pointing to us as a result of
539 * F_SETOWN with our pgid.
541 funsetownlst(&pgrp->pg_sigiolst);
543 if (pgrp->pg_session->s_ttyp != NULL &&
544 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
545 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
547 lwkt_reltoken(&proc_tokens[n]);
549 sess_rele(pgrp->pg_session);
554 * Locate a process group by number. The returned process group will be
555 * referenced w/pgref() and must be released with pgrel() (or assigned
556 * somewhere if you wish to keep the reference).
567 lwkt_gettoken_shared(&proc_tokens[n]);
569 LIST_FOREACH(pgrp, &allpgrps[n], pg_list) {
570 if (pgrp->pg_id == pgid) {
571 refcount_acquire(&pgrp->pg_refs);
572 lwkt_reltoken(&proc_tokens[n]);
576 lwkt_reltoken(&proc_tokens[n]);
581 * Move p to a new or existing process group (and session)
586 enterpgrp(struct proc *p, pid_t pgid, int mksess)
594 KASSERT(pgrp == NULL || !mksess,
595 ("enterpgrp: setsid into non-empty pgrp"));
596 KASSERT(!SESS_LEADER(p),
597 ("enterpgrp: session leader attempted setpgrp"));
600 pid_t savepid = p->p_pid;
607 KASSERT(p->p_pid == pgid,
608 ("enterpgrp: new pgrp and pid != pgid"));
609 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
611 LIST_INIT(&pgrp->pg_members);
613 SLIST_INIT(&pgrp->pg_sigiolst);
614 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
615 refcount_init(&pgrp->pg_refs, 1);
616 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
620 if ((np = pfindn(savepid)) == NULL || np != p) {
621 lwkt_reltoken(&proc_tokens[n]);
627 lwkt_gettoken(&proc_tokens[n]);
629 struct session *sess;
634 sess = kmalloc(sizeof(struct session), M_SESSION,
636 lwkt_gettoken(&p->p_token);
638 sess->s_sid = p->p_pid;
640 sess->s_ttyvp = NULL;
642 bcopy(p->p_session->s_login, sess->s_login,
643 sizeof(sess->s_login));
644 pgrp->pg_session = sess;
645 KASSERT(p == curproc,
646 ("enterpgrp: mksession and p != curproc"));
647 p->p_flags &= ~P_CONTROLT;
648 LIST_INSERT_HEAD(&allsessn[n], sess, s_list);
649 lwkt_reltoken(&p->p_token);
651 lwkt_gettoken(&p->p_token);
652 pgrp->pg_session = p->p_session;
653 sess_hold(pgrp->pg_session);
654 lwkt_reltoken(&p->p_token);
656 LIST_INSERT_HEAD(&allpgrps[n], pgrp, pg_list);
658 lwkt_reltoken(&proc_tokens[n]);
659 } else if (pgrp == p->p_pgrp) {
662 } /* else pgfind() referenced the pgrp */
664 lwkt_gettoken(&pgrp->pg_token);
665 lwkt_gettoken(&p->p_token);
668 * Replace p->p_pgrp, handling any races that occur.
670 while ((opgrp = p->p_pgrp) != NULL) {
672 lwkt_gettoken(&opgrp->pg_token);
673 if (opgrp != p->p_pgrp) {
674 lwkt_reltoken(&opgrp->pg_token);
678 LIST_REMOVE(p, p_pglist);
682 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
685 * Adjust eligibility of affected pgrps to participate in job control.
686 * Increment eligibility counts before decrementing, otherwise we
687 * could reach 0 spuriously during the first call.
691 fixjobc(p, opgrp, 0);
692 lwkt_reltoken(&opgrp->pg_token);
693 pgrel(opgrp); /* manual pgref */
694 pgrel(opgrp); /* p->p_pgrp ref */
696 lwkt_reltoken(&p->p_token);
697 lwkt_reltoken(&pgrp->pg_token);
705 * Remove process from process group
710 leavepgrp(struct proc *p)
712 struct pgrp *pg = p->p_pgrp;
714 lwkt_gettoken(&p->p_token);
715 while ((pg = p->p_pgrp) != NULL) {
717 lwkt_gettoken(&pg->pg_token);
718 if (p->p_pgrp != pg) {
719 lwkt_reltoken(&pg->pg_token);
724 LIST_REMOVE(p, p_pglist);
725 lwkt_reltoken(&pg->pg_token);
726 pgrel(pg); /* manual pgref */
727 pgrel(pg); /* p->p_pgrp ref */
730 lwkt_reltoken(&p->p_token);
736 * Adjust the ref count on a session structure. When the ref count falls to
737 * zero the tty is disassociated from the session and the session structure
738 * is freed. Note that tty assocation is not itself ref-counted.
743 sess_hold(struct session *sp)
745 atomic_add_int(&sp->s_count, 1);
752 sess_rele(struct session *sess)
758 n = SESS_HASH(sess->s_sid);
760 count = sess->s_count;
764 lwkt_gettoken(&tty_token);
765 lwkt_gettoken(&proc_tokens[n]);
766 if (atomic_cmpset_int(&sess->s_count, 1, 0))
768 lwkt_reltoken(&proc_tokens[n]);
769 lwkt_reltoken(&tty_token);
772 if (atomic_cmpset_int(&sess->s_count, count, count - 1))
779 * Successful 1->0 transition and tty_token is held.
781 LIST_REMOVE(sess, s_list);
783 if (sess->s_ttyp && sess->s_ttyp->t_session) {
784 #ifdef TTY_DO_FULL_CLOSE
785 /* FULL CLOSE, see ttyclearsession() */
786 KKASSERT(sess->s_ttyp->t_session == sess);
787 sess->s_ttyp->t_session = NULL;
789 /* HALF CLOSE, see ttyclearsession() */
790 if (sess->s_ttyp->t_session == sess)
791 sess->s_ttyp->t_session = NULL;
794 if ((tp = sess->s_ttyp) != NULL) {
798 lwkt_reltoken(&proc_tokens[n]);
799 lwkt_reltoken(&tty_token);
801 kfree(sess, M_SESSION);
805 * Adjust pgrp jobc counters when specified process changes process group.
806 * We count the number of processes in each process group that "qualify"
807 * the group for terminal job control (those with a parent in a different
808 * process group of the same session). If that count reaches zero, the
809 * process group becomes orphaned. Check both the specified process'
810 * process group and that of its children.
811 * entering == 0 => p is leaving specified group.
812 * entering == 1 => p is entering specified group.
817 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
819 struct pgrp *hispgrp;
820 struct session *mysession;
824 * Check p's parent to see whether p qualifies its own process
825 * group; if so, adjust count for p's process group.
827 lwkt_gettoken(&p->p_token); /* p_children scan */
828 lwkt_gettoken(&pgrp->pg_token);
830 mysession = pgrp->pg_session;
831 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
832 hispgrp->pg_session == mysession) {
835 else if (--pgrp->pg_jobc == 0)
840 * Check this process' children to see whether they qualify
841 * their process groups; if so, adjust counts for children's
844 LIST_FOREACH(np, &p->p_children, p_sibling) {
846 lwkt_gettoken(&np->p_token);
847 if ((hispgrp = np->p_pgrp) != pgrp &&
848 hispgrp->pg_session == mysession &&
849 np->p_stat != SZOMB) {
851 lwkt_gettoken(&hispgrp->pg_token);
854 else if (--hispgrp->pg_jobc == 0)
856 lwkt_reltoken(&hispgrp->pg_token);
859 lwkt_reltoken(&np->p_token);
862 KKASSERT(pgrp->pg_refs > 0);
863 lwkt_reltoken(&pgrp->pg_token);
864 lwkt_reltoken(&p->p_token);
868 * A process group has become orphaned;
869 * if there are any stopped processes in the group,
870 * hang-up all process in that group.
872 * The caller must hold pg_token.
875 orphanpg(struct pgrp *pg)
879 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
880 if (p->p_stat == SSTOP) {
881 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
891 * Add a new process to the allproc list and the PID hash. This
892 * also assigns a pid to the new process.
897 proc_add_allproc(struct proc *p)
901 if ((random_offset = randompid) != 0) {
902 read_random(&random_offset, sizeof(random_offset));
903 random_offset = (random_offset & 0x7FFFFFFF) % randompid;
905 proc_makepid(p, random_offset);
909 * Calculate a new process pid. This function is integrated into
910 * proc_add_allproc() to guarentee that the new pid is not reused before
911 * the new process can be added to the allproc list.
913 * p_pid is assigned and the process is added to the allproc hash table
917 proc_makepid(struct proc *p, int random_offset)
919 static pid_t nextpid; /* heuristic, allowed to race */
922 struct session *sess;
927 * Calculate a hash index and find an unused process id within
928 * the table, looping if we cannot find one.
931 atomic_add_int(&nextpid, random_offset);
933 base = atomic_fetchadd_int(&nextpid, 1) + 1;
934 if (base >= PID_MAX) {
935 base = base % PID_MAX;
939 n = ALLPROC_HASH(base);
940 lwkt_gettoken(&proc_tokens[n]);
942 LIST_FOREACH(ps, &allprocs[n], p_list) {
943 if (ps->p_pid == base) {
944 base += ALLPROC_HSIZE;
945 if (base >= PID_MAX) {
946 lwkt_reltoken(&proc_tokens[n]);
951 LIST_FOREACH(pg, &allpgrps[n], pg_list) {
952 if (pg->pg_id == base) {
953 base += ALLPROC_HSIZE;
954 if (base >= PID_MAX) {
955 lwkt_reltoken(&proc_tokens[n]);
960 LIST_FOREACH(sess, &allsessn[n], s_list) {
961 if (sess->s_sid == base) {
962 base += ALLPROC_HSIZE;
963 if (base >= PID_MAX) {
964 lwkt_reltoken(&proc_tokens[n]);
971 * Assign the pid and insert the process.
974 LIST_INSERT_HEAD(&allprocs[n], p, p_list);
975 lwkt_reltoken(&proc_tokens[n]);
979 * Called from exit1 to place the process into a zombie state.
980 * The process is removed from the pid hash and p_stat is set
981 * to SZOMB. Normal pfind[n]() calls will not find it any more.
983 * Caller must hold p->p_token. We are required to wait until p_lock
984 * becomes zero before we can manipulate the list, allowing allproc
985 * scans to guarantee consistency during a list scan.
988 proc_move_allproc_zombie(struct proc *p)
992 n = ALLPROC_HASH(p->p_pid);
993 PSTALL(p, "reap1", 0);
994 lwkt_gettoken(&proc_tokens[n]);
996 PSTALL(p, "reap1a", 0);
999 lwkt_reltoken(&proc_tokens[n]);
1000 dsched_exit_proc(p);
1004 * This routine is called from kern_wait() and will remove the process
1005 * from the zombie list and the sibling list. This routine will block
1006 * if someone has a lock on the proces (p_lock).
1008 * Caller must hold p->p_token. We are required to wait until p_lock
1009 * becomes zero before we can manipulate the list, allowing allproc
1010 * scans to guarantee consistency during a list scan.
1013 proc_remove_zombie(struct proc *p)
1017 n = ALLPROC_HASH(p->p_pid);
1019 PSTALL(p, "reap2", 0);
1020 lwkt_gettoken(&proc_tokens[n]);
1021 PSTALL(p, "reap2a", 0);
1022 LIST_REMOVE(p, p_list); /* from remove master list */
1023 LIST_REMOVE(p, p_sibling); /* and from sibling list */
1025 lwkt_reltoken(&proc_tokens[n]);
1029 * Handle various requirements prior to returning to usermode. Called from
1030 * platform trap and system call code.
1033 lwpuserret(struct lwp *lp)
1035 struct proc *p = lp->lwp_proc;
1037 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1038 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1041 if (lp->lwp_mpflags & LWP_MP_WEXIT) {
1042 lwkt_gettoken(&p->p_token);
1044 lwkt_reltoken(&p->p_token); /* NOT REACHED */
1049 * Kernel threads run from user processes can also accumulate deferred
1050 * actions which need to be acted upon. Callers include:
1052 * nfsd - Can allocate lots of vnodes
1055 lwpkthreaddeferred(void)
1057 struct lwp *lp = curthread->td_lwp;
1060 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1061 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1068 * Scan all processes on the allproc list. The process is automatically
1069 * held for the callback. A return value of -1 terminates the loop.
1070 * Zombie procs are skipped.
1072 * The callback is made with the process held and proc_token held.
1074 * We limit the scan to the number of processes as-of the start of
1075 * the scan so as not to get caught up in an endless loop if new processes
1076 * are created more quickly than we can scan the old ones. Add a little
1077 * slop to try to catch edge cases since nprocs can race.
1082 allproc_scan(int (*callback)(struct proc *, void *), void *data)
1084 int limit = nprocs + ncpus;
1090 * proc_tokens[n] protects the allproc list and PHOLD() prevents the
1091 * process from being removed from the allproc list or the zombproc
1094 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1095 if (LIST_FIRST(&allprocs[n]) == NULL)
1097 lwkt_gettoken(&proc_tokens[n]);
1098 LIST_FOREACH(p, &allprocs[n], p_list) {
1099 if (p->p_stat == SZOMB)
1102 r = callback(p, data);
1109 lwkt_reltoken(&proc_tokens[n]);
1112 * Check if asked to stop early
1120 * Scan all lwps of processes on the allproc list. The lwp is automatically
1121 * held for the callback. A return value of -1 terminates the loop.
1123 * The callback is made with the proces and lwp both held, and proc_token held.
1128 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
1135 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1136 if (LIST_FIRST(&allprocs[n]) == NULL)
1138 lwkt_gettoken(&proc_tokens[n]);
1139 LIST_FOREACH(p, &allprocs[n], p_list) {
1140 if (p->p_stat == SZOMB)
1143 lwkt_gettoken(&p->p_token);
1144 FOREACH_LWP_IN_PROC(lp, p) {
1146 r = callback(lp, data);
1149 lwkt_reltoken(&p->p_token);
1154 lwkt_reltoken(&proc_tokens[n]);
1157 * Asked to exit early
1165 * Scan all processes on the zombproc list. The process is automatically
1166 * held for the callback. A return value of -1 terminates the loop.
1169 * The callback is made with the proces held and proc_token held.
1172 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
1179 * proc_tokens[n] protects the allproc list and PHOLD() prevents the
1180 * process from being removed from the allproc list or the zombproc
1183 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1184 if (LIST_FIRST(&allprocs[n]) == NULL)
1186 lwkt_gettoken(&proc_tokens[n]);
1187 LIST_FOREACH(p, &allprocs[n], p_list) {
1188 if (p->p_stat != SZOMB)
1191 r = callback(p, data);
1196 lwkt_reltoken(&proc_tokens[n]);
1199 * Check if asked to stop early
1206 #include "opt_ddb.h"
1208 #include <ddb/ddb.h>
1213 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1219 for (i = 0; i < ALLPROC_HSIZE; ++i) {
1220 if (LIST_EMPTY(&allpgrps[i]))
1222 kprintf("\tindx %d\n", i);
1223 LIST_FOREACH(pgrp, &allpgrps[i], pg_list) {
1224 kprintf("\tpgrp %p, pgid %ld, sess %p, "
1225 "sesscnt %d, mem %p\n",
1226 (void *)pgrp, (long)pgrp->pg_id,
1227 (void *)pgrp->pg_session,
1228 pgrp->pg_session->s_count,
1229 (void *)LIST_FIRST(&pgrp->pg_members));
1230 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1231 kprintf("\t\tpid %ld addr %p pgrp %p\n",
1232 (long)p->p_pid, (void *)p,
1241 * The caller must hold proc_token.
1244 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1246 struct kinfo_proc ki;
1248 int skp = 0, had_output = 0;
1251 bzero(&ki, sizeof(ki));
1252 lwkt_gettoken_shared(&p->p_token);
1253 fill_kinfo_proc(p, &ki);
1254 if ((flags & KERN_PROC_FLAG_LWP) == 0)
1257 FOREACH_LWP_IN_PROC(lp, p) {
1259 fill_kinfo_lwp(lp, &ki.kp_lwp);
1261 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1268 lwkt_reltoken(&p->p_token);
1269 /* We need to output at least the proc, even if there is no lwp. */
1270 if (had_output == 0) {
1271 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1277 * The caller must hold proc_token.
1280 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req)
1282 struct kinfo_proc ki;
1285 fill_kinfo_proc_kthread(td, &ki);
1286 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1296 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1298 int *name = (int *)arg1;
1299 int oid = oidp->oid_number;
1300 u_int namelen = arg2;
1303 struct thread *marker;
1308 struct ucred *cr1 = curproc->p_ucred;
1310 flags = oid & KERN_PROC_FLAGMASK;
1311 oid &= ~KERN_PROC_FLAGMASK;
1313 if ((oid == KERN_PROC_ALL && namelen != 0) ||
1314 (oid != KERN_PROC_ALL && namelen != 1)) {
1319 * proc_token protects the allproc list and PHOLD() prevents the
1320 * process from being removed from the allproc list or the zombproc
1323 if (oid == KERN_PROC_PID) {
1324 p = pfind((pid_t)name[0]);
1326 if (PRISON_CHECK(cr1, p->p_ucred))
1327 error = sysctl_out_proc(p, req, flags);
1335 /* overestimate by 5 procs */
1336 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1341 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1342 if (LIST_EMPTY(&allprocs[n]))
1344 lwkt_gettoken_shared(&proc_tokens[n]);
1345 LIST_FOREACH(p, &allprocs[n], p_list) {
1347 * Show a user only their processes.
1349 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
1352 * Skip embryonic processes.
1354 if (p->p_stat == SIDL)
1357 * TODO - make more efficient (see notes below).
1361 case KERN_PROC_PGRP:
1362 /* could do this by traversing pgrp */
1363 if (p->p_pgrp == NULL ||
1364 p->p_pgrp->pg_id != (pid_t)name[0])
1369 if ((p->p_flags & P_CONTROLT) == 0 ||
1370 p->p_session == NULL ||
1371 p->p_session->s_ttyp == NULL ||
1372 dev2udev(p->p_session->s_ttyp->t_dev) !=
1378 if (p->p_ucred == NULL ||
1379 p->p_ucred->cr_uid != (uid_t)name[0])
1383 case KERN_PROC_RUID:
1384 if (p->p_ucred == NULL ||
1385 p->p_ucred->cr_ruid != (uid_t)name[0])
1390 if (!PRISON_CHECK(cr1, p->p_ucred))
1393 error = sysctl_out_proc(p, req, flags);
1396 lwkt_reltoken(&proc_tokens[n]);
1400 lwkt_reltoken(&proc_tokens[n]);
1404 * Iterate over all active cpus and scan their thread list. Start
1405 * with the next logical cpu and end with our original cpu. We
1406 * migrate our own thread to each target cpu in order to safely scan
1407 * its thread list. In the last loop we migrate back to our original
1410 origcpu = mycpu->gd_cpuid;
1411 if (!ps_showallthreads || jailed(cr1))
1414 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1415 marker->td_flags = TDF_MARKER;
1418 for (n = 1; n <= ncpus; ++n) {
1422 nid = (origcpu + n) % ncpus;
1423 if ((smp_active_mask & CPUMASK(nid)) == 0)
1425 rgd = globaldata_find(nid);
1426 lwkt_setcpu_self(rgd);
1429 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1431 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1432 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1433 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1434 if (td->td_flags & TDF_MARKER)
1443 case KERN_PROC_PGRP:
1446 case KERN_PROC_RUID:
1449 error = sysctl_out_proc_kthread(td, req);
1457 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1465 * Userland scheduler expects us to return on the same cpu we
1468 if (mycpu->gd_cpuid != origcpu)
1469 lwkt_setcpu_self(globaldata_find(origcpu));
1471 kfree(marker, M_TEMP);
1478 * This sysctl allows a process to retrieve the argument list or process
1479 * title for another process without groping around in the address space
1480 * of the other process. It also allow a process to set its own "process
1481 * title to a string of its own choice.
1486 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1488 int *name = (int*) arg1;
1489 u_int namelen = arg2;
1494 struct ucred *cr1 = curproc->p_ucred;
1499 p = pfind((pid_t)name[0]);
1502 lwkt_gettoken(&p->p_token);
1504 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1507 if (req->newptr && curproc != p) {
1511 if (req->oldptr && (pa = p->p_args) != NULL) {
1512 refcount_acquire(&pa->ar_ref);
1513 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1514 if (refcount_release(&pa->ar_ref))
1517 if (req->newptr == NULL)
1520 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1524 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1525 refcount_init(&pa->ar_ref, 1);
1526 pa->ar_length = req->newlen;
1527 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1535 * Replace p_args with the new pa. p_args may have previously
1542 KKASSERT(opa->ar_ref > 0);
1543 if (refcount_release(&opa->ar_ref)) {
1544 kfree(opa, M_PARGS);
1550 lwkt_reltoken(&p->p_token);
1557 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1559 int *name = (int*) arg1;
1560 u_int namelen = arg2;
1563 char *fullpath, *freepath;
1564 struct ucred *cr1 = curproc->p_ucred;
1569 p = pfind((pid_t)name[0]);
1572 lwkt_gettoken_shared(&p->p_token);
1575 * If we are not allowed to see other args, we certainly shouldn't
1576 * get the cwd either. Also check the usual trespassing.
1578 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1581 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1582 struct nchandle nch;
1584 cache_copy(&p->p_fd->fd_ncdir, &nch);
1585 error = cache_fullpath(p, &nch, NULL,
1586 &fullpath, &freepath, 0);
1590 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1591 kfree(freepath, M_TEMP);
1596 lwkt_reltoken(&p->p_token);
1603 * This sysctl allows a process to retrieve the path of the executable for
1604 * itself or another process.
1607 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1609 pid_t *pidp = (pid_t *)arg1;
1610 unsigned int arglen = arg2;
1613 char *retbuf, *freebuf;
1618 if (*pidp == -1) { /* -1 means this process */
1631 error = vn_fullpath(p, vp, &retbuf, &freebuf, 0);
1635 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1636 kfree(freebuf, M_TEMP);
1640 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1642 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1643 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1645 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1646 sysctl_kern_proc, "Process table");
1648 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1649 sysctl_kern_proc, "Process table");
1651 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1652 sysctl_kern_proc, "Process table");
1654 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1655 sysctl_kern_proc, "Process table");
1657 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1658 sysctl_kern_proc, "Process table");
1660 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1661 sysctl_kern_proc, "Process table");
1663 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1664 sysctl_kern_proc, "Process table");
1666 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1667 sysctl_kern_proc, "Process table");
1669 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1670 sysctl_kern_proc, "Process table");
1672 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1673 sysctl_kern_proc, "Process table");
1675 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1676 sysctl_kern_proc, "Process table");
1678 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1679 sysctl_kern_proc_args, "Process argument list");
1681 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1682 sysctl_kern_proc_cwd, "Process argument list");
1684 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD,
1685 sysctl_kern_proc_pathname, "Process executable path");