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>
47 #include <vm/vm_map.h>
49 #include <machine/smp.h>
51 #include <sys/refcount.h>
52 #include <sys/spinlock2.h>
53 #include <sys/mplock2.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)
67 LIST_HEAD(pidhashhead, proc);
69 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
70 MALLOC_DEFINE(M_SESSION, "session", "session header");
71 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
72 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
73 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
75 int ps_showallprocs = 1;
76 static int ps_showallthreads = 1;
77 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
79 "Unprivileged processes can see processes with different UID/GID");
80 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
81 &ps_showallthreads, 0,
82 "Unprivileged processes can see kernel threads");
84 static void orphanpg(struct pgrp *pg);
85 static void proc_makepid(struct proc *p, int random_offset);
90 static struct lwkt_token proc_tokens[ALLPROC_HSIZE];
91 static struct proclist allprocs[ALLPROC_HSIZE]; /* locked by proc_tokens */
92 static struct pgrplist allpgrps[ALLPROC_HSIZE]; /* locked by proc_tokens */
93 static struct sesslist allsessn[ALLPROC_HSIZE]; /* locked by proc_tokens */
96 * Random component to nextpid generation. We mix in a random factor to make
97 * it a little harder to predict. We sanity check the modulus value to avoid
98 * doing it in critical paths. Don't let it be too small or we pointlessly
99 * waste randomness entropy, and don't let it be impossibly large. Using a
100 * modulus that is too big causes a LOT more process table scans and slows
101 * down fork processing as the pidchecked caching is defeated.
103 static int randompid = 0;
109 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
114 error = sysctl_handle_int(oidp, &pid, 0, req);
115 if (error || !req->newptr)
117 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
119 else if (pid < 2) /* NOP */
121 else if (pid < 100) /* Make it reasonable */
127 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
128 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
131 * Initialize global process hashing structures.
133 * These functions are ONLY called from the low level boot code and do
134 * not lock their operations.
141 for (i = 0; i < ALLPROC_HSIZE; ++i) {
142 LIST_INIT(&allprocs[i]);
143 LIST_INIT(&allsessn[i]);
144 LIST_INIT(&allpgrps[i]);
145 lwkt_token_init(&proc_tokens[i], "allproc");
152 procinsertinit(struct proc *p)
154 LIST_INSERT_HEAD(&allprocs[ALLPROC_HASH(p->p_pid)], p, p_list);
158 pgrpinsertinit(struct pgrp *pg)
160 LIST_INSERT_HEAD(&allpgrps[ALLPROC_HASH(pg->pg_id)], pg, pg_list);
164 sessinsertinit(struct session *sess)
166 LIST_INSERT_HEAD(&allsessn[ALLPROC_HASH(sess->s_sid)], sess, s_list);
170 * Process hold/release support functions. Called via the PHOLD(),
171 * PRELE(), and PSTALL() macros.
173 * p->p_lock is a simple hold count with a waiting interlock. No wakeup()
174 * is issued unless someone is actually waiting for the process.
176 * Most holds are short-term, allowing a process scan or other similar
177 * operation to access a proc structure without it getting ripped out from
178 * under us. procfs and process-list sysctl ops also use the hold function
179 * interlocked with various p_flags to keep the vmspace intact when reading
180 * or writing a user process's address space.
182 * There are two situations where a hold count can be longer. Exiting lwps
183 * hold the process until the lwp is reaped, and the parent will hold the
184 * child during vfork()/exec() sequences while the child is marked P_PPWAIT.
186 * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at
187 * various critical points in the fork/exec and exit paths before proceeding.
189 #define PLOCK_ZOMB 0x20000000
190 #define PLOCK_WAITING 0x40000000
191 #define PLOCK_MASK 0x1FFFFFFF
194 pstall(struct proc *p, const char *wmesg, int count)
202 if ((o & PLOCK_MASK) <= count)
204 n = o | PLOCK_WAITING;
205 tsleep_interlock(&p->p_lock, 0);
208 * If someone is trying to single-step the process during
209 * an exec or an exit they can deadlock us because procfs
210 * sleeps with the process held.
213 if (p->p_flags & P_INEXEC) {
215 } else if (p->p_flags & P_POSTEXIT) {
216 spin_lock(&p->p_spin);
219 spin_unlock(&p->p_spin);
224 if (atomic_cmpset_int(&p->p_lock, o, n)) {
225 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
231 phold(struct proc *p)
233 atomic_add_int(&p->p_lock, 1);
237 * WARNING! On last release (p) can become instantly invalid due to
241 prele(struct proc *p)
249 if (atomic_cmpset_int(&p->p_lock, 1, 0))
257 KKASSERT((o & PLOCK_MASK) > 0);
259 n = (o - 1) & ~PLOCK_WAITING;
260 if (atomic_cmpset_int(&p->p_lock, o, n)) {
261 if (o & PLOCK_WAITING)
269 * Hold and flag serialized for zombie reaping purposes.
271 * This function will fail if it has to block, returning non-zero with
272 * neither the flag set or the hold count bumped. Note that we must block
273 * without holding a ref, meaning that the caller must ensure that (p)
274 * remains valid through some other interlock (typically on its parent
275 * process's p_token).
277 * Zero is returned on success. The hold count will be incremented and
278 * the serialization flag acquired. Note that serialization is only against
279 * other pholdzomb() calls, not against phold() calls.
282 pholdzomb(struct proc *p)
290 if (atomic_cmpset_int(&p->p_lock, 0, PLOCK_ZOMB | 1))
299 if ((o & PLOCK_ZOMB) == 0) {
300 n = (o + 1) | PLOCK_ZOMB;
301 if (atomic_cmpset_int(&p->p_lock, o, n))
304 KKASSERT((o & PLOCK_MASK) > 0);
305 n = o | PLOCK_WAITING;
306 tsleep_interlock(&p->p_lock, 0);
307 if (atomic_cmpset_int(&p->p_lock, o, n)) {
308 tsleep(&p->p_lock, PINTERLOCKED, "phldz", 0);
309 /* (p) can be ripped out at this point */
317 * Release PLOCK_ZOMB and the hold count, waking up any waiters.
319 * WARNING! On last release (p) can become instantly invalid due to
323 prelezomb(struct proc *p)
331 if (atomic_cmpset_int(&p->p_lock, PLOCK_ZOMB | 1, 0))
337 KKASSERT(p->p_lock & PLOCK_ZOMB);
340 KKASSERT((o & PLOCK_MASK) > 0);
342 n = (o - 1) & ~(PLOCK_ZOMB | PLOCK_WAITING);
343 if (atomic_cmpset_int(&p->p_lock, o, n)) {
344 if (o & PLOCK_WAITING)
352 * Is p an inferior of the current process?
357 inferior(struct proc *p)
362 lwkt_gettoken_shared(&p->p_token);
363 while (p != curproc) {
365 lwkt_reltoken(&p->p_token);
370 lwkt_reltoken(&p->p_token);
372 lwkt_gettoken_shared(&p2->p_token);
375 lwkt_reltoken(&p->p_token);
382 * Locate a process by number. The returned process will be referenced and
383 * must be released with PRELE().
390 struct proc *p = curproc;
394 * Shortcut the current process
396 if (p && p->p_pid == pid) {
402 * Otherwise find it in the hash table.
404 n = ALLPROC_HASH(pid);
406 lwkt_gettoken_shared(&proc_tokens[n]);
407 LIST_FOREACH(p, &allprocs[n], p_list) {
408 if (p->p_stat == SZOMB)
410 if (p->p_pid == pid) {
412 lwkt_reltoken(&proc_tokens[n]);
416 lwkt_reltoken(&proc_tokens[n]);
422 * Locate a process by number. The returned process is NOT referenced.
423 * The result will not be stable and is typically only used to validate
424 * against a process that the caller has in-hand.
431 struct proc *p = curproc;
435 * Shortcut the current process
437 if (p && p->p_pid == pid)
441 * Otherwise find it in the hash table.
443 n = ALLPROC_HASH(pid);
445 lwkt_gettoken_shared(&proc_tokens[n]);
446 LIST_FOREACH(p, &allprocs[n], p_list) {
447 if (p->p_stat == SZOMB)
449 if (p->p_pid == pid) {
450 lwkt_reltoken(&proc_tokens[n]);
454 lwkt_reltoken(&proc_tokens[n]);
460 * Locate a process on the zombie list. Return a process or NULL.
461 * The returned process will be referenced and the caller must release
464 * No other requirements.
469 struct proc *p = curproc;
473 * Shortcut the current process
475 if (p && p->p_pid == pid) {
481 * Otherwise find it in the hash table.
483 n = ALLPROC_HASH(pid);
485 lwkt_gettoken_shared(&proc_tokens[n]);
486 LIST_FOREACH(p, &allprocs[n], p_list) {
487 if (p->p_stat != SZOMB)
489 if (p->p_pid == pid) {
491 lwkt_reltoken(&proc_tokens[n]);
495 lwkt_reltoken(&proc_tokens[n]);
502 pgref(struct pgrp *pgrp)
504 refcount_acquire(&pgrp->pg_refs);
508 pgrel(struct pgrp *pgrp)
513 n = PGRP_HASH(pgrp->pg_id);
515 count = pgrp->pg_refs;
519 lwkt_gettoken(&proc_tokens[n]);
520 if (atomic_cmpset_int(&pgrp->pg_refs, 1, 0))
522 lwkt_reltoken(&proc_tokens[n]);
525 if (atomic_cmpset_int(&pgrp->pg_refs, count, count - 1))
532 * Successful 1->0 transition, pghash_spin is held.
534 LIST_REMOVE(pgrp, pg_list);
537 * Reset any sigio structures pointing to us as a result of
538 * F_SETOWN with our pgid.
540 funsetownlst(&pgrp->pg_sigiolst);
542 if (pgrp->pg_session->s_ttyp != NULL &&
543 pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
544 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
546 lwkt_reltoken(&proc_tokens[n]);
548 sess_rele(pgrp->pg_session);
553 * Locate a process group by number. The returned process group will be
554 * referenced w/pgref() and must be released with pgrel() (or assigned
555 * somewhere if you wish to keep the reference).
566 lwkt_gettoken_shared(&proc_tokens[n]);
568 LIST_FOREACH(pgrp, &allpgrps[n], pg_list) {
569 if (pgrp->pg_id == pgid) {
570 refcount_acquire(&pgrp->pg_refs);
571 lwkt_reltoken(&proc_tokens[n]);
575 lwkt_reltoken(&proc_tokens[n]);
580 * Move p to a new or existing process group (and session)
585 enterpgrp(struct proc *p, pid_t pgid, int mksess)
593 KASSERT(pgrp == NULL || !mksess,
594 ("enterpgrp: setsid into non-empty pgrp"));
595 KASSERT(!SESS_LEADER(p),
596 ("enterpgrp: session leader attempted setpgrp"));
599 pid_t savepid = p->p_pid;
606 KASSERT(p->p_pid == pgid,
607 ("enterpgrp: new pgrp and pid != pgid"));
608 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK | M_ZERO);
610 LIST_INIT(&pgrp->pg_members);
612 SLIST_INIT(&pgrp->pg_sigiolst);
613 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
614 refcount_init(&pgrp->pg_refs, 1);
615 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
619 if ((np = pfindn(savepid)) == NULL || np != p) {
620 lwkt_reltoken(&proc_tokens[n]);
626 lwkt_gettoken(&proc_tokens[n]);
628 struct session *sess;
633 sess = kmalloc(sizeof(struct session), M_SESSION,
635 lwkt_gettoken(&p->p_token);
637 sess->s_sid = p->p_pid;
639 sess->s_ttyvp = NULL;
641 bcopy(p->p_session->s_login, sess->s_login,
642 sizeof(sess->s_login));
643 pgrp->pg_session = sess;
644 KASSERT(p == curproc,
645 ("enterpgrp: mksession and p != curproc"));
646 p->p_flags &= ~P_CONTROLT;
647 LIST_INSERT_HEAD(&allsessn[n], sess, s_list);
648 lwkt_reltoken(&p->p_token);
650 lwkt_gettoken(&p->p_token);
651 pgrp->pg_session = p->p_session;
652 sess_hold(pgrp->pg_session);
653 lwkt_reltoken(&p->p_token);
655 LIST_INSERT_HEAD(&allpgrps[n], pgrp, pg_list);
657 lwkt_reltoken(&proc_tokens[n]);
658 } else if (pgrp == p->p_pgrp) {
661 } /* else pgfind() referenced the pgrp */
663 lwkt_gettoken(&pgrp->pg_token);
664 lwkt_gettoken(&p->p_token);
667 * Replace p->p_pgrp, handling any races that occur.
669 while ((opgrp = p->p_pgrp) != NULL) {
671 lwkt_gettoken(&opgrp->pg_token);
672 if (opgrp != p->p_pgrp) {
673 lwkt_reltoken(&opgrp->pg_token);
677 LIST_REMOVE(p, p_pglist);
681 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
684 * Adjust eligibility of affected pgrps to participate in job control.
685 * Increment eligibility counts before decrementing, otherwise we
686 * could reach 0 spuriously during the first call.
690 fixjobc(p, opgrp, 0);
691 lwkt_reltoken(&opgrp->pg_token);
692 pgrel(opgrp); /* manual pgref */
693 pgrel(opgrp); /* p->p_pgrp ref */
695 lwkt_reltoken(&p->p_token);
696 lwkt_reltoken(&pgrp->pg_token);
704 * Remove process from process group
709 leavepgrp(struct proc *p)
711 struct pgrp *pg = p->p_pgrp;
713 lwkt_gettoken(&p->p_token);
714 while ((pg = p->p_pgrp) != NULL) {
716 lwkt_gettoken(&pg->pg_token);
717 if (p->p_pgrp != pg) {
718 lwkt_reltoken(&pg->pg_token);
723 LIST_REMOVE(p, p_pglist);
724 lwkt_reltoken(&pg->pg_token);
725 pgrel(pg); /* manual pgref */
726 pgrel(pg); /* p->p_pgrp ref */
729 lwkt_reltoken(&p->p_token);
735 * Adjust the ref count on a session structure. When the ref count falls to
736 * zero the tty is disassociated from the session and the session structure
737 * is freed. Note that tty assocation is not itself ref-counted.
742 sess_hold(struct session *sp)
744 atomic_add_int(&sp->s_count, 1);
751 sess_rele(struct session *sess)
757 n = SESS_HASH(sess->s_sid);
759 count = sess->s_count;
763 lwkt_gettoken(&tty_token);
764 lwkt_gettoken(&proc_tokens[n]);
765 if (atomic_cmpset_int(&sess->s_count, 1, 0))
767 lwkt_reltoken(&proc_tokens[n]);
768 lwkt_reltoken(&tty_token);
771 if (atomic_cmpset_int(&sess->s_count, count, count - 1))
778 * Successful 1->0 transition and tty_token is held.
780 LIST_REMOVE(sess, s_list);
782 if (sess->s_ttyp && sess->s_ttyp->t_session) {
783 #ifdef TTY_DO_FULL_CLOSE
784 /* FULL CLOSE, see ttyclearsession() */
785 KKASSERT(sess->s_ttyp->t_session == sess);
786 sess->s_ttyp->t_session = NULL;
788 /* HALF CLOSE, see ttyclearsession() */
789 if (sess->s_ttyp->t_session == sess)
790 sess->s_ttyp->t_session = NULL;
793 if ((tp = sess->s_ttyp) != NULL) {
797 lwkt_reltoken(&proc_tokens[n]);
798 lwkt_reltoken(&tty_token);
800 kfree(sess, M_SESSION);
804 * Adjust pgrp jobc counters when specified process changes process group.
805 * We count the number of processes in each process group that "qualify"
806 * the group for terminal job control (those with a parent in a different
807 * process group of the same session). If that count reaches zero, the
808 * process group becomes orphaned. Check both the specified process'
809 * process group and that of its children.
810 * entering == 0 => p is leaving specified group.
811 * entering == 1 => p is entering specified group.
816 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
818 struct pgrp *hispgrp;
819 struct session *mysession;
823 * Check p's parent to see whether p qualifies its own process
824 * group; if so, adjust count for p's process group.
826 lwkt_gettoken(&p->p_token); /* p_children scan */
827 lwkt_gettoken(&pgrp->pg_token);
829 mysession = pgrp->pg_session;
830 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
831 hispgrp->pg_session == mysession) {
834 else if (--pgrp->pg_jobc == 0)
839 * Check this process' children to see whether they qualify
840 * their process groups; if so, adjust counts for children's
843 LIST_FOREACH(np, &p->p_children, p_sibling) {
845 lwkt_gettoken(&np->p_token);
846 if ((hispgrp = np->p_pgrp) != pgrp &&
847 hispgrp->pg_session == mysession &&
848 np->p_stat != SZOMB) {
850 lwkt_gettoken(&hispgrp->pg_token);
853 else if (--hispgrp->pg_jobc == 0)
855 lwkt_reltoken(&hispgrp->pg_token);
858 lwkt_reltoken(&np->p_token);
861 KKASSERT(pgrp->pg_refs > 0);
862 lwkt_reltoken(&pgrp->pg_token);
863 lwkt_reltoken(&p->p_token);
867 * A process group has become orphaned;
868 * if there are any stopped processes in the group,
869 * hang-up all process in that group.
871 * The caller must hold pg_token.
874 orphanpg(struct pgrp *pg)
878 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
879 if (p->p_stat == SSTOP) {
880 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
890 * Add a new process to the allproc list and the PID hash. This
891 * also assigns a pid to the new process.
896 proc_add_allproc(struct proc *p)
900 if ((random_offset = randompid) != 0) {
901 read_random(&random_offset, sizeof(random_offset));
902 random_offset = (random_offset & 0x7FFFFFFF) % randompid;
904 proc_makepid(p, random_offset);
908 * Calculate a new process pid. This function is integrated into
909 * proc_add_allproc() to guarentee that the new pid is not reused before
910 * the new process can be added to the allproc list.
912 * p_pid is assigned and the process is added to the allproc hash table
916 proc_makepid(struct proc *p, int random_offset)
918 static pid_t nextpid; /* heuristic, allowed to race */
921 struct session *sess;
926 * Calculate a hash index and find an unused process id within
927 * the table, looping if we cannot find one.
930 atomic_add_int(&nextpid, random_offset);
932 base = atomic_fetchadd_int(&nextpid, 1) + 1;
933 if (base >= PID_MAX) {
934 base = base % PID_MAX;
938 n = ALLPROC_HASH(base);
939 lwkt_gettoken(&proc_tokens[n]);
941 LIST_FOREACH(ps, &allprocs[n], p_list) {
942 if (ps->p_pid == base) {
943 base += ALLPROC_HSIZE;
944 if (base >= PID_MAX) {
945 lwkt_reltoken(&proc_tokens[n]);
950 LIST_FOREACH(pg, &allpgrps[n], pg_list) {
951 if (pg->pg_id == base) {
952 base += ALLPROC_HSIZE;
953 if (base >= PID_MAX) {
954 lwkt_reltoken(&proc_tokens[n]);
959 LIST_FOREACH(sess, &allsessn[n], s_list) {
960 if (sess->s_sid == base) {
961 base += ALLPROC_HSIZE;
962 if (base >= PID_MAX) {
963 lwkt_reltoken(&proc_tokens[n]);
970 * Assign the pid and insert the process.
973 LIST_INSERT_HEAD(&allprocs[n], p, p_list);
974 lwkt_reltoken(&proc_tokens[n]);
978 * Called from exit1 to place the process into a zombie state.
979 * The process is removed from the pid hash and p_stat is set
980 * to SZOMB. Normal pfind[n]() calls will not find it any more.
982 * Caller must hold p->p_token. We are required to wait until p_lock
983 * becomes zero before we can manipulate the list, allowing allproc
984 * scans to guarantee consistency during a list scan.
987 proc_move_allproc_zombie(struct proc *p)
991 n = ALLPROC_HASH(p->p_pid);
992 PSTALL(p, "reap1", 0);
993 lwkt_gettoken(&proc_tokens[n]);
995 PSTALL(p, "reap1a", 0);
998 lwkt_reltoken(&proc_tokens[n]);
1003 * This routine is called from kern_wait() and will remove the process
1004 * from the zombie list and the sibling list. This routine will block
1005 * if someone has a lock on the proces (p_lock).
1007 * Caller must hold p->p_token. We are required to wait until p_lock
1008 * becomes zero before we can manipulate the list, allowing allproc
1009 * scans to guarantee consistency during a list scan.
1012 proc_remove_zombie(struct proc *p)
1016 n = ALLPROC_HASH(p->p_pid);
1018 PSTALL(p, "reap2", 0);
1019 lwkt_gettoken(&proc_tokens[n]);
1020 PSTALL(p, "reap2a", 0);
1021 LIST_REMOVE(p, p_list); /* from remove master list */
1022 LIST_REMOVE(p, p_sibling); /* and from sibling list */
1024 lwkt_reltoken(&proc_tokens[n]);
1028 * Handle various requirements prior to returning to usermode. Called from
1029 * platform trap and system call code.
1032 lwpuserret(struct lwp *lp)
1034 struct proc *p = lp->lwp_proc;
1036 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1037 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1040 if (lp->lwp_mpflags & LWP_MP_WEXIT) {
1041 lwkt_gettoken(&p->p_token);
1043 lwkt_reltoken(&p->p_token); /* NOT REACHED */
1048 * Kernel threads run from user processes can also accumulate deferred
1049 * actions which need to be acted upon. Callers include:
1051 * nfsd - Can allocate lots of vnodes
1054 lwpkthreaddeferred(void)
1056 struct lwp *lp = curthread->td_lwp;
1059 if (lp->lwp_mpflags & LWP_MP_VNLRU) {
1060 atomic_clear_int(&lp->lwp_mpflags, LWP_MP_VNLRU);
1067 * Scan all processes on the allproc list. The process is automatically
1068 * held for the callback. A return value of -1 terminates the loop.
1069 * Zombie procs are skipped.
1071 * The callback is made with the process held and proc_token held.
1073 * We limit the scan to the number of processes as-of the start of
1074 * the scan so as not to get caught up in an endless loop if new processes
1075 * are created more quickly than we can scan the old ones. Add a little
1076 * slop to try to catch edge cases since nprocs can race.
1081 allproc_scan(int (*callback)(struct proc *, void *), void *data)
1083 int limit = nprocs + ncpus;
1089 * proc_tokens[n] protects the allproc list and PHOLD() prevents the
1090 * process from being removed from the allproc list or the zombproc
1093 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1094 if (LIST_FIRST(&allprocs[n]) == NULL)
1096 lwkt_gettoken(&proc_tokens[n]);
1097 LIST_FOREACH(p, &allprocs[n], p_list) {
1098 if (p->p_stat == SZOMB)
1101 r = callback(p, data);
1108 lwkt_reltoken(&proc_tokens[n]);
1111 * Check if asked to stop early
1119 * Scan all lwps of processes on the allproc list. The lwp is automatically
1120 * held for the callback. A return value of -1 terminates the loop.
1122 * The callback is made with the proces and lwp both held, and proc_token held.
1127 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
1134 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1135 if (LIST_FIRST(&allprocs[n]) == NULL)
1137 lwkt_gettoken(&proc_tokens[n]);
1138 LIST_FOREACH(p, &allprocs[n], p_list) {
1139 if (p->p_stat == SZOMB)
1142 lwkt_gettoken(&p->p_token);
1143 FOREACH_LWP_IN_PROC(lp, p) {
1145 r = callback(lp, data);
1148 lwkt_reltoken(&p->p_token);
1153 lwkt_reltoken(&proc_tokens[n]);
1156 * Asked to exit early
1164 * Scan all processes on the zombproc list. The process is automatically
1165 * held for the callback. A return value of -1 terminates the loop.
1168 * The callback is made with the proces held and proc_token held.
1171 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
1178 * proc_tokens[n] protects the allproc list and PHOLD() prevents the
1179 * process from being removed from the allproc list or the zombproc
1182 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1183 if (LIST_FIRST(&allprocs[n]) == NULL)
1185 lwkt_gettoken(&proc_tokens[n]);
1186 LIST_FOREACH(p, &allprocs[n], p_list) {
1187 if (p->p_stat != SZOMB)
1190 r = callback(p, data);
1195 lwkt_reltoken(&proc_tokens[n]);
1198 * Check if asked to stop early
1205 #include "opt_ddb.h"
1207 #include <ddb/ddb.h>
1212 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1218 for (i = 0; i < ALLPROC_HSIZE; ++i) {
1219 if (LIST_EMPTY(&allpgrps[i]))
1221 kprintf("\tindx %d\n", i);
1222 LIST_FOREACH(pgrp, &allpgrps[i], pg_list) {
1223 kprintf("\tpgrp %p, pgid %ld, sess %p, "
1224 "sesscnt %d, mem %p\n",
1225 (void *)pgrp, (long)pgrp->pg_id,
1226 (void *)pgrp->pg_session,
1227 pgrp->pg_session->s_count,
1228 (void *)LIST_FIRST(&pgrp->pg_members));
1229 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1230 kprintf("\t\tpid %ld addr %p pgrp %p\n",
1231 (long)p->p_pid, (void *)p,
1240 * The caller must hold proc_token.
1243 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1245 struct kinfo_proc ki;
1247 int skp = 0, had_output = 0;
1250 bzero(&ki, sizeof(ki));
1251 lwkt_gettoken_shared(&p->p_token);
1252 fill_kinfo_proc(p, &ki);
1253 if ((flags & KERN_PROC_FLAG_LWP) == 0)
1256 FOREACH_LWP_IN_PROC(lp, p) {
1258 fill_kinfo_lwp(lp, &ki.kp_lwp);
1260 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1267 lwkt_reltoken(&p->p_token);
1268 /* We need to output at least the proc, even if there is no lwp. */
1269 if (had_output == 0) {
1270 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1276 * The caller must hold proc_token.
1279 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req)
1281 struct kinfo_proc ki;
1284 fill_kinfo_proc_kthread(td, &ki);
1285 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1295 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1297 int *name = (int *)arg1;
1298 int oid = oidp->oid_number;
1299 u_int namelen = arg2;
1302 struct thread *marker;
1307 struct ucred *cr1 = curproc->p_ucred;
1309 flags = oid & KERN_PROC_FLAGMASK;
1310 oid &= ~KERN_PROC_FLAGMASK;
1312 if ((oid == KERN_PROC_ALL && namelen != 0) ||
1313 (oid != KERN_PROC_ALL && namelen != 1)) {
1318 * proc_token protects the allproc list and PHOLD() prevents the
1319 * process from being removed from the allproc list or the zombproc
1322 if (oid == KERN_PROC_PID) {
1323 p = pfind((pid_t)name[0]);
1325 if (PRISON_CHECK(cr1, p->p_ucred))
1326 error = sysctl_out_proc(p, req, flags);
1334 /* overestimate by 5 procs */
1335 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1340 for (n = 0; n < ALLPROC_HSIZE; ++n) {
1341 if (LIST_EMPTY(&allprocs[n]))
1343 lwkt_gettoken_shared(&proc_tokens[n]);
1344 LIST_FOREACH(p, &allprocs[n], p_list) {
1346 * Show a user only their processes.
1348 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
1351 * Skip embryonic processes.
1353 if (p->p_stat == SIDL)
1356 * TODO - make more efficient (see notes below).
1360 case KERN_PROC_PGRP:
1361 /* could do this by traversing pgrp */
1362 if (p->p_pgrp == NULL ||
1363 p->p_pgrp->pg_id != (pid_t)name[0])
1368 if ((p->p_flags & P_CONTROLT) == 0 ||
1369 p->p_session == NULL ||
1370 p->p_session->s_ttyp == NULL ||
1371 dev2udev(p->p_session->s_ttyp->t_dev) !=
1377 if (p->p_ucred == NULL ||
1378 p->p_ucred->cr_uid != (uid_t)name[0])
1382 case KERN_PROC_RUID:
1383 if (p->p_ucred == NULL ||
1384 p->p_ucred->cr_ruid != (uid_t)name[0])
1389 if (!PRISON_CHECK(cr1, p->p_ucred))
1392 error = sysctl_out_proc(p, req, flags);
1395 lwkt_reltoken(&proc_tokens[n]);
1399 lwkt_reltoken(&proc_tokens[n]);
1403 * Iterate over all active cpus and scan their thread list. Start
1404 * with the next logical cpu and end with our original cpu. We
1405 * migrate our own thread to each target cpu in order to safely scan
1406 * its thread list. In the last loop we migrate back to our original
1409 origcpu = mycpu->gd_cpuid;
1410 if (!ps_showallthreads || jailed(cr1))
1413 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1414 marker->td_flags = TDF_MARKER;
1417 for (n = 1; n <= ncpus; ++n) {
1421 nid = (origcpu + n) % ncpus;
1422 if ((smp_active_mask & CPUMASK(nid)) == 0)
1424 rgd = globaldata_find(nid);
1425 lwkt_setcpu_self(rgd);
1428 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1430 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1431 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1432 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1433 if (td->td_flags & TDF_MARKER)
1442 case KERN_PROC_PGRP:
1445 case KERN_PROC_RUID:
1448 error = sysctl_out_proc_kthread(td, req);
1456 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1464 * Userland scheduler expects us to return on the same cpu we
1467 if (mycpu->gd_cpuid != origcpu)
1468 lwkt_setcpu_self(globaldata_find(origcpu));
1470 kfree(marker, M_TEMP);
1477 * This sysctl allows a process to retrieve the argument list or process
1478 * title for another process without groping around in the address space
1479 * of the other process. It also allow a process to set its own "process
1480 * title to a string of its own choice.
1485 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1487 int *name = (int*) arg1;
1488 u_int namelen = arg2;
1493 struct ucred *cr1 = curproc->p_ucred;
1498 p = pfind((pid_t)name[0]);
1501 lwkt_gettoken(&p->p_token);
1503 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1506 if (req->newptr && curproc != p) {
1510 if (req->oldptr && (pa = p->p_args) != NULL) {
1511 refcount_acquire(&pa->ar_ref);
1512 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1513 if (refcount_release(&pa->ar_ref))
1516 if (req->newptr == NULL)
1519 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1523 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1524 refcount_init(&pa->ar_ref, 1);
1525 pa->ar_length = req->newlen;
1526 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1534 * Replace p_args with the new pa. p_args may have previously
1541 KKASSERT(opa->ar_ref > 0);
1542 if (refcount_release(&opa->ar_ref)) {
1543 kfree(opa, M_PARGS);
1549 lwkt_reltoken(&p->p_token);
1556 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1558 int *name = (int*) arg1;
1559 u_int namelen = arg2;
1562 char *fullpath, *freepath;
1563 struct ucred *cr1 = curproc->p_ucred;
1568 p = pfind((pid_t)name[0]);
1571 lwkt_gettoken_shared(&p->p_token);
1574 * If we are not allowed to see other args, we certainly shouldn't
1575 * get the cwd either. Also check the usual trespassing.
1577 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1580 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1581 struct nchandle nch;
1583 cache_copy(&p->p_fd->fd_ncdir, &nch);
1584 error = cache_fullpath(p, &nch, NULL,
1585 &fullpath, &freepath, 0);
1589 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1590 kfree(freepath, M_TEMP);
1595 lwkt_reltoken(&p->p_token);
1601 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1603 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1604 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1606 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1607 sysctl_kern_proc, "Process table");
1609 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1610 sysctl_kern_proc, "Process table");
1612 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1613 sysctl_kern_proc, "Process table");
1615 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1616 sysctl_kern_proc, "Process table");
1618 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1619 sysctl_kern_proc, "Process table");
1621 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1622 sysctl_kern_proc, "Process table");
1624 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1625 sysctl_kern_proc, "Process table");
1627 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1628 sysctl_kern_proc, "Process table");
1630 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1631 sysctl_kern_proc, "Process table");
1633 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1634 sysctl_kern_proc, "Process table");
1636 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1637 sysctl_kern_proc, "Process table");
1639 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1640 sysctl_kern_proc_args, "Process argument list");
1642 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1643 sysctl_kern_proc_cwd, "Process argument list");