4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
36 * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.9 2003/05/08 07:47:16 kbyanc Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
46 #include <sys/filedesc.h>
48 #include <sys/dsched.h>
49 #include <sys/signalvar.h>
50 #include <sys/spinlock.h>
54 #include <vm/vm_map.h>
56 #include <machine/smp.h>
58 #include <sys/refcount.h>
59 #include <sys/spinlock2.h>
60 #include <sys/mplock2.h>
62 static MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
63 MALLOC_DEFINE(M_SESSION, "session", "session header");
64 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
65 MALLOC_DEFINE(M_LWP, "lwp", "lwp structures");
66 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
68 int ps_showallprocs = 1;
69 static int ps_showallthreads = 1;
70 SYSCTL_INT(_security, OID_AUTO, ps_showallprocs, CTLFLAG_RW,
72 "Unprivileged processes can see proccesses with different UID/GID");
73 SYSCTL_INT(_security, OID_AUTO, ps_showallthreads, CTLFLAG_RW,
74 &ps_showallthreads, 0,
75 "Unprivileged processes can see kernel threads");
77 static void pgdelete(struct pgrp *);
78 static void orphanpg(struct pgrp *pg);
79 static pid_t proc_getnewpid_locked(int random_offset);
84 struct pidhashhead *pidhashtbl;
86 struct pgrphashhead *pgrphashtbl;
88 struct proclist allproc;
89 struct proclist zombproc;
92 * Random component to nextpid generation. We mix in a random factor to make
93 * it a little harder to predict. We sanity check the modulus value to avoid
94 * doing it in critical paths. Don't let it be too small or we pointlessly
95 * waste randomness entropy, and don't let it be impossibly large. Using a
96 * modulus that is too big causes a LOT more process table scans and slows
97 * down fork processing as the pidchecked caching is defeated.
99 static int randompid = 0;
105 sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
110 error = sysctl_handle_int(oidp, &pid, 0, req);
111 if (error || !req->newptr)
113 if (pid < 0 || pid > PID_MAX - 100) /* out of range */
115 else if (pid < 2) /* NOP */
117 else if (pid < 100) /* Make it reasonable */
123 SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
124 0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
127 * Initialize global process hashing structures.
129 * Called from the low level boot code only.
135 LIST_INIT(&zombproc);
137 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
138 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
143 * Process hold/release support functions. These functions must be MPSAFE.
144 * Called via the PHOLD(), PRELE(), and PSTALL() macros.
146 * p->p_lock is a simple hold count with a waiting interlock. No wakeup()
147 * is issued unless someone is actually waiting for the process.
149 * Most holds are short-term, allowing a process scan or other similar
150 * operation to access a proc structure without it getting ripped out from
151 * under us. procfs and process-list sysctl ops also use the hold function
152 * interlocked with various p_flags to keep the vmspace intact when reading
153 * or writing a user process's address space.
155 * There are two situations where a hold count can be longer. Exiting lwps
156 * hold the process until the lwp is reaped, and the parent will hold the
157 * child during vfork()/exec() sequences while the child is marked P_PPWAIT.
159 * The kernel waits for the hold count to drop to 0 (or 1 in some cases) at
160 * various critical points in the fork/exec and exit paths before proceeding.
162 #define PLOCK_ZOMB 0x20000000
163 #define PLOCK_WAITING 0x40000000
164 #define PLOCK_MASK 0x1FFFFFFF
167 pstall(struct proc *p, const char *wmesg, int count)
175 if ((o & PLOCK_MASK) <= count)
177 n = o | PLOCK_WAITING;
178 tsleep_interlock(&p->p_lock, 0);
181 * If someone is trying to single-step the process during
182 * an exec or an exit they can deadlock us because procfs
183 * sleeps with the process held.
186 if (p->p_flags & P_INEXEC) {
188 } else if (p->p_flags & P_POSTEXIT) {
189 spin_lock(&p->p_spin);
192 spin_unlock(&p->p_spin);
197 if (atomic_cmpset_int(&p->p_lock, o, n)) {
198 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
204 phold(struct proc *p)
206 atomic_add_int(&p->p_lock, 1);
210 * WARNING! On last release (p) can become instantly invalid due to
214 prele(struct proc *p)
222 if (atomic_cmpset_int(&p->p_lock, 1, 0))
230 KKASSERT((o & PLOCK_MASK) > 0);
232 n = (o - 1) & ~PLOCK_WAITING;
233 if (atomic_cmpset_int(&p->p_lock, o, n)) {
234 if (o & PLOCK_WAITING)
242 * Hold and flag serialized for zombie reaping purposes.
244 * This function will fail if it has to block, returning non-zero with
245 * neither the flag set or the hold count bumped. Note that we must block
246 * without holding a ref, meaning that the caller must ensure that (p)
247 * remains valid through some other interlock (typically on its parent
248 * process's p_token).
250 * Zero is returned on success. The hold count will be incremented and
251 * the serialization flag acquired. Note that serialization is only against
252 * other pholdzomb() calls, not against phold() calls.
255 pholdzomb(struct proc *p)
263 if (atomic_cmpset_int(&p->p_lock, 0, PLOCK_ZOMB | 1))
272 if ((o & PLOCK_ZOMB) == 0) {
273 n = (o + 1) | PLOCK_ZOMB;
274 if (atomic_cmpset_int(&p->p_lock, o, n))
277 KKASSERT((o & PLOCK_MASK) > 0);
278 n = o | PLOCK_WAITING;
279 tsleep_interlock(&p->p_lock, 0);
280 if (atomic_cmpset_int(&p->p_lock, o, n)) {
281 tsleep(&p->p_lock, PINTERLOCKED, "phldz", 0);
282 /* (p) can be ripped out at this point */
290 * Release PLOCK_ZOMB and the hold count, waking up any waiters.
292 * WARNING! On last release (p) can become instantly invalid due to
296 prelezomb(struct proc *p)
304 if (atomic_cmpset_int(&p->p_lock, PLOCK_ZOMB | 1, 0))
310 KKASSERT(p->p_lock & PLOCK_ZOMB);
313 KKASSERT((o & PLOCK_MASK) > 0);
315 n = (o - 1) & ~(PLOCK_ZOMB | PLOCK_WAITING);
316 if (atomic_cmpset_int(&p->p_lock, o, n)) {
317 if (o & PLOCK_WAITING)
325 * Is p an inferior of the current process?
328 * The caller must hold proc_token if the caller wishes a stable result.
331 inferior(struct proc *p)
333 lwkt_gettoken(&proc_token);
334 while (p != curproc) {
336 lwkt_reltoken(&proc_token);
341 lwkt_reltoken(&proc_token);
346 * Locate a process by number. The returned process will be referenced and
347 * must be released with PRELE().
356 lwkt_gettoken(&proc_token);
357 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
358 if (p->p_pid == pid) {
360 lwkt_reltoken(&proc_token);
364 lwkt_reltoken(&proc_token);
369 * Locate a process by number. The returned process is NOT referenced.
370 * The caller should hold proc_token if the caller wishes a stable result.
379 lwkt_gettoken(&proc_token);
380 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
381 if (p->p_pid == pid) {
382 lwkt_reltoken(&proc_token);
386 lwkt_reltoken(&proc_token);
391 pgref(struct pgrp *pgrp)
393 refcount_acquire(&pgrp->pg_refs);
397 pgrel(struct pgrp *pgrp)
399 if (refcount_release(&pgrp->pg_refs))
404 * Locate a process group by number. The returned process group will be
405 * referenced w/pgref() and must be released with pgrel() (or assigned
406 * somewhere if you wish to keep the reference).
415 lwkt_gettoken(&proc_token);
416 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
417 if (pgrp->pg_id == pgid) {
418 refcount_acquire(&pgrp->pg_refs);
419 lwkt_reltoken(&proc_token);
423 lwkt_reltoken(&proc_token);
428 * Move p to a new or existing process group (and session)
433 enterpgrp(struct proc *p, pid_t pgid, int mksess)
441 KASSERT(pgrp == NULL || !mksess,
442 ("enterpgrp: setsid into non-empty pgrp"));
443 KASSERT(!SESS_LEADER(p),
444 ("enterpgrp: session leader attempted setpgrp"));
447 pid_t savepid = p->p_pid;
452 KASSERT(p->p_pid == pgid,
453 ("enterpgrp: new pgrp and pid != pgid"));
454 if ((np = pfindn(savepid)) == NULL || np != p) {
458 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK);
460 struct session *sess;
465 sess = kmalloc(sizeof(struct session), M_SESSION,
468 sess->s_sid = p->p_pid;
470 sess->s_ttyvp = NULL;
472 bcopy(p->p_session->s_login, sess->s_login,
473 sizeof(sess->s_login));
474 pgrp->pg_session = sess;
475 KASSERT(p == curproc,
476 ("enterpgrp: mksession and p != curproc"));
477 lwkt_gettoken(&p->p_token);
478 p->p_flags &= ~P_CONTROLT;
479 lwkt_reltoken(&p->p_token);
481 pgrp->pg_session = p->p_session;
482 sess_hold(pgrp->pg_session);
485 LIST_INIT(&pgrp->pg_members);
486 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
488 SLIST_INIT(&pgrp->pg_sigiolst);
489 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
490 refcount_init(&pgrp->pg_refs, 1);
491 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
492 } else if (pgrp == p->p_pgrp) {
495 } /* else pgfind() referenced the pgrp */
498 * Adjust eligibility of affected pgrps to participate in job control.
499 * Increment eligibility counts before decrementing, otherwise we
500 * could reach 0 spuriously during the first call.
502 lwkt_gettoken(&pgrp->pg_token);
503 lwkt_gettoken(&p->p_token);
505 fixjobc(p, p->p_pgrp, 0);
506 while ((opgrp = p->p_pgrp) != NULL) {
508 lwkt_gettoken(&opgrp->pg_token);
509 LIST_REMOVE(p, p_pglist);
511 lwkt_reltoken(&opgrp->pg_token);
515 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
516 lwkt_reltoken(&p->p_token);
517 lwkt_reltoken(&pgrp->pg_token);
525 * Remove process from process group
530 leavepgrp(struct proc *p)
532 struct pgrp *pg = p->p_pgrp;
534 lwkt_gettoken(&p->p_token);
538 lwkt_gettoken(&pg->pg_token);
539 if (p->p_pgrp == pg) {
541 LIST_REMOVE(p, p_pglist);
544 lwkt_reltoken(&pg->pg_token);
545 lwkt_reltoken(&p->p_token); /* avoid chaining on rel */
548 lwkt_reltoken(&p->p_token);
554 * Delete a process group. Must be called only after the last ref has been
558 pgdelete(struct pgrp *pgrp)
561 * Reset any sigio structures pointing to us as a result of
562 * F_SETOWN with our pgid.
564 funsetownlst(&pgrp->pg_sigiolst);
566 if (pgrp->pg_session->s_ttyp != NULL &&
567 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
568 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
569 LIST_REMOVE(pgrp, pg_hash);
570 sess_rele(pgrp->pg_session);
575 * Adjust the ref count on a session structure. When the ref count falls to
576 * zero the tty is disassociated from the session and the session structure
577 * is freed. Note that tty assocation is not itself ref-counted.
582 sess_hold(struct session *sp)
584 lwkt_gettoken(&tty_token);
586 lwkt_reltoken(&tty_token);
593 sess_rele(struct session *sp)
597 KKASSERT(sp->s_count > 0);
598 lwkt_gettoken(&tty_token);
599 if (--sp->s_count == 0) {
600 if (sp->s_ttyp && sp->s_ttyp->t_session) {
601 #ifdef TTY_DO_FULL_CLOSE
602 /* FULL CLOSE, see ttyclearsession() */
603 KKASSERT(sp->s_ttyp->t_session == sp);
604 sp->s_ttyp->t_session = NULL;
606 /* HALF CLOSE, see ttyclearsession() */
607 if (sp->s_ttyp->t_session == sp)
608 sp->s_ttyp->t_session = NULL;
611 if ((tp = sp->s_ttyp) != NULL) {
615 kfree(sp, M_SESSION);
617 lwkt_reltoken(&tty_token);
621 * Adjust pgrp jobc counters when specified process changes process group.
622 * We count the number of processes in each process group that "qualify"
623 * the group for terminal job control (those with a parent in a different
624 * process group of the same session). If that count reaches zero, the
625 * process group becomes orphaned. Check both the specified process'
626 * process group and that of its children.
627 * entering == 0 => p is leaving specified group.
628 * entering == 1 => p is entering specified group.
633 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
635 struct pgrp *hispgrp;
636 struct session *mysession;
640 * Check p's parent to see whether p qualifies its own process
641 * group; if so, adjust count for p's process group.
643 lwkt_gettoken(&p->p_token); /* p_children scan */
644 lwkt_gettoken(&pgrp->pg_token);
646 mysession = pgrp->pg_session;
647 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
648 hispgrp->pg_session == mysession) {
651 else if (--pgrp->pg_jobc == 0)
656 * Check this process' children to see whether they qualify
657 * their process groups; if so, adjust counts for children's
660 LIST_FOREACH(np, &p->p_children, p_sibling) {
662 lwkt_gettoken(&np->p_token);
663 if ((hispgrp = np->p_pgrp) != pgrp &&
664 hispgrp->pg_session == mysession &&
665 np->p_stat != SZOMB) {
667 lwkt_gettoken(&hispgrp->pg_token);
670 else if (--hispgrp->pg_jobc == 0)
672 lwkt_reltoken(&hispgrp->pg_token);
675 lwkt_reltoken(&np->p_token);
678 KKASSERT(pgrp->pg_refs > 0);
679 lwkt_reltoken(&pgrp->pg_token);
680 lwkt_reltoken(&p->p_token);
684 * A process group has become orphaned;
685 * if there are any stopped processes in the group,
686 * hang-up all process in that group.
688 * The caller must hold pg_token.
691 orphanpg(struct pgrp *pg)
695 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
696 if (p->p_stat == SSTOP) {
697 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
707 * Add a new process to the allproc list and the PID hash. This
708 * also assigns a pid to the new process.
713 proc_add_allproc(struct proc *p)
717 if ((random_offset = randompid) != 0) {
719 random_offset = karc4random() % random_offset;
723 lwkt_gettoken(&proc_token);
724 p->p_pid = proc_getnewpid_locked(random_offset);
725 LIST_INSERT_HEAD(&allproc, p, p_list);
726 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
727 lwkt_reltoken(&proc_token);
731 * Calculate a new process pid. This function is integrated into
732 * proc_add_allproc() to guarentee that the new pid is not reused before
733 * the new process can be added to the allproc list.
735 * The caller must hold proc_token.
739 proc_getnewpid_locked(int random_offset)
741 static pid_t nextpid;
742 static pid_t pidchecked;
746 * Find an unused process ID. We remember a range of unused IDs
747 * ready to use (from nextpid+1 through pidchecked-1).
749 nextpid = nextpid + 1 + random_offset;
752 * If the process ID prototype has wrapped around,
753 * restart somewhat above 0, as the low-numbered procs
754 * tend to include daemons that don't exit.
756 if (nextpid >= PID_MAX) {
757 nextpid = nextpid % PID_MAX;
762 if (nextpid >= pidchecked) {
765 pidchecked = PID_MAX;
768 * Scan the active and zombie procs to check whether this pid
769 * is in use. Remember the lowest pid that's greater
770 * than nextpid, so we can avoid checking for a while.
772 * NOTE: Processes in the midst of being forked may not
773 * yet have p_pgrp and p_pgrp->pg_session set up
774 * yet, so we have to check for NULL.
776 * Processes being torn down should be interlocked
777 * with proc_token prior to the clearing of their
780 p = LIST_FIRST(&allproc);
782 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
783 while (p->p_pid == nextpid ||
784 (p->p_pgrp && p->p_pgrp->pg_id == nextpid) ||
785 (p->p_pgrp && p->p_session &&
786 p->p_session->s_sid == nextpid)) {
788 if (nextpid >= pidchecked)
791 if (p->p_pid > nextpid && pidchecked > p->p_pid)
792 pidchecked = p->p_pid;
794 p->p_pgrp->pg_id > nextpid &&
795 pidchecked > p->p_pgrp->pg_id) {
796 pidchecked = p->p_pgrp->pg_id;
798 if (p->p_pgrp && p->p_session &&
799 p->p_session->s_sid > nextpid &&
800 pidchecked > p->p_session->s_sid) {
801 pidchecked = p->p_session->s_sid;
806 p = LIST_FIRST(&zombproc);
814 * Called from exit1 to remove a process from the allproc
815 * list and move it to the zombie list.
817 * Caller must hold p->p_token. We are required to wait until p_lock
818 * becomes zero before we can manipulate the list, allowing allproc
819 * scans to guarantee consistency during a list scan.
822 proc_move_allproc_zombie(struct proc *p)
824 lwkt_gettoken(&proc_token);
825 PSTALL(p, "reap1", 0);
826 LIST_REMOVE(p, p_list);
827 LIST_INSERT_HEAD(&zombproc, p, p_list);
828 LIST_REMOVE(p, p_hash);
830 lwkt_reltoken(&proc_token);
835 * This routine is called from kern_wait() and will remove the process
836 * from the zombie list and the sibling list. This routine will block
837 * if someone has a lock on the proces (p_lock).
839 * Caller must hold p->p_token. We are required to wait until p_lock
840 * becomes zero before we can manipulate the list, allowing allproc
841 * scans to guarantee consistency during a list scan.
844 proc_remove_zombie(struct proc *p)
846 lwkt_gettoken(&proc_token);
847 PSTALL(p, "reap2", 0);
848 LIST_REMOVE(p, p_list); /* off zombproc */
849 LIST_REMOVE(p, p_sibling);
851 lwkt_reltoken(&proc_token);
855 * Scan all processes on the allproc list. The process is automatically
856 * held for the callback. A return value of -1 terminates the loop.
858 * The callback is made with the process held and proc_token held.
860 * We limit the scan to the number of processes as-of the start of
861 * the scan so as not to get caught up in an endless loop if new processes
862 * are created more quickly than we can scan the old ones. Add a little
863 * slop to try to catch edge cases since nprocs can race.
868 allproc_scan(int (*callback)(struct proc *, void *), void *data)
872 int limit = nprocs + ncpus;
875 * proc_token protects the allproc list and PHOLD() prevents the
876 * process from being removed from the allproc list or the zombproc
879 lwkt_gettoken(&proc_token);
880 LIST_FOREACH(p, &allproc, p_list) {
882 r = callback(p, data);
889 lwkt_reltoken(&proc_token);
893 * Scan all lwps of processes on the allproc list. The lwp is automatically
894 * held for the callback. A return value of -1 terminates the loop.
896 * The callback is made with the proces and lwp both held, and proc_token held.
901 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
908 * proc_token protects the allproc list and PHOLD() prevents the
909 * process from being removed from the allproc list or the zombproc
912 lwkt_gettoken(&proc_token);
913 LIST_FOREACH(p, &allproc, p_list) {
915 FOREACH_LWP_IN_PROC(lp, p) {
917 r = callback(lp, data);
924 lwkt_reltoken(&proc_token);
928 * Scan all processes on the zombproc list. The process is automatically
929 * held for the callback. A return value of -1 terminates the loop.
932 * The callback is made with the proces held and proc_token held.
935 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
940 lwkt_gettoken(&proc_token);
941 LIST_FOREACH(p, &zombproc, p_list) {
943 r = callback(p, data);
948 lwkt_reltoken(&proc_token);
958 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
964 for (i = 0; i <= pgrphash; i++) {
965 if (!LIST_EMPTY(&pgrphashtbl[i])) {
966 kprintf("\tindx %d\n", i);
967 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
969 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
970 (void *)pgrp, (long)pgrp->pg_id,
971 (void *)pgrp->pg_session,
972 pgrp->pg_session->s_count,
973 (void *)LIST_FIRST(&pgrp->pg_members));
974 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
975 kprintf("\t\tpid %ld addr %p pgrp %p\n",
976 (long)p->p_pid, (void *)p,
986 * Locate a process on the zombie list. Return a process or NULL.
987 * The returned process will be referenced and the caller must release
990 * No other requirements.
997 lwkt_gettoken(&proc_token);
998 LIST_FOREACH(p, &zombproc, p_list) {
999 if (p->p_pid == pid) {
1001 lwkt_reltoken(&proc_token);
1005 lwkt_reltoken(&proc_token);
1010 * The caller must hold proc_token.
1013 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1015 struct kinfo_proc ki;
1017 int skp = 0, had_output = 0;
1020 bzero(&ki, sizeof(ki));
1021 lwkt_gettoken(&p->p_token);
1022 fill_kinfo_proc(p, &ki);
1023 if ((flags & KERN_PROC_FLAG_LWP) == 0)
1026 FOREACH_LWP_IN_PROC(lp, p) {
1028 fill_kinfo_lwp(lp, &ki.kp_lwp);
1030 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1037 lwkt_reltoken(&p->p_token);
1038 /* We need to output at least the proc, even if there is no lwp. */
1039 if (had_output == 0) {
1040 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1046 * The caller must hold proc_token.
1049 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
1051 struct kinfo_proc ki;
1054 fill_kinfo_proc_kthread(td, &ki);
1055 error = SYSCTL_OUT(req, &ki, sizeof(ki));
1065 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1067 int *name = (int*) arg1;
1068 int oid = oidp->oid_number;
1069 u_int namelen = arg2;
1071 struct proclist *plist;
1073 struct thread *marker;
1074 int doingzomb, flags = 0;
1078 struct ucred *cr1 = curproc->p_ucred;
1080 flags = oid & KERN_PROC_FLAGMASK;
1081 oid &= ~KERN_PROC_FLAGMASK;
1083 if ((oid == KERN_PROC_ALL && namelen != 0) ||
1084 (oid != KERN_PROC_ALL && namelen != 1)) {
1089 * proc_token protects the allproc list and PHOLD() prevents the
1090 * process from being removed from the allproc list or the zombproc
1093 lwkt_gettoken(&proc_token);
1094 if (oid == KERN_PROC_PID) {
1095 p = pfindn((pid_t)name[0]);
1098 if (!PRISON_CHECK(cr1, p->p_ucred))
1101 error = sysctl_out_proc(p, req, flags);
1107 /* overestimate by 5 procs */
1108 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1112 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
1117 LIST_FOREACH(p, plist, p_list) {
1119 * Show a user only their processes.
1121 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
1124 * Skip embryonic processes.
1126 if (p->p_stat == SIDL)
1129 * TODO - make more efficient (see notes below).
1133 case KERN_PROC_PGRP:
1134 /* could do this by traversing pgrp */
1135 if (p->p_pgrp == NULL ||
1136 p->p_pgrp->pg_id != (pid_t)name[0])
1141 if ((p->p_flags & P_CONTROLT) == 0 ||
1142 p->p_session == NULL ||
1143 p->p_session->s_ttyp == NULL ||
1144 dev2udev(p->p_session->s_ttyp->t_dev) !=
1150 if (p->p_ucred == NULL ||
1151 p->p_ucred->cr_uid != (uid_t)name[0])
1155 case KERN_PROC_RUID:
1156 if (p->p_ucred == NULL ||
1157 p->p_ucred->cr_ruid != (uid_t)name[0])
1162 if (!PRISON_CHECK(cr1, p->p_ucred))
1165 error = sysctl_out_proc(p, req, flags);
1173 * Iterate over all active cpus and scan their thread list. Start
1174 * with the next logical cpu and end with our original cpu. We
1175 * migrate our own thread to each target cpu in order to safely scan
1176 * its thread list. In the last loop we migrate back to our original
1179 origcpu = mycpu->gd_cpuid;
1180 if (!ps_showallthreads || jailed(cr1))
1183 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1184 marker->td_flags = TDF_MARKER;
1187 for (n = 1; n <= ncpus; ++n) {
1191 nid = (origcpu + n) % ncpus;
1192 if ((smp_active_mask & CPUMASK(nid)) == 0)
1194 rgd = globaldata_find(nid);
1195 lwkt_setcpu_self(rgd);
1198 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1200 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1201 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1202 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1203 if (td->td_flags & TDF_MARKER)
1212 case KERN_PROC_PGRP:
1215 case KERN_PROC_RUID:
1218 error = sysctl_out_proc_kthread(td, req,
1227 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1233 kfree(marker, M_TEMP);
1236 lwkt_reltoken(&proc_token);
1241 * This sysctl allows a process to retrieve the argument list or process
1242 * title for another process without groping around in the address space
1243 * of the other process. It also allow a process to set its own "process
1244 * title to a string of its own choice.
1249 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1251 int *name = (int*) arg1;
1252 u_int namelen = arg2;
1257 struct ucred *cr1 = curproc->p_ucred;
1262 p = pfind((pid_t)name[0]);
1265 lwkt_gettoken(&p->p_token);
1267 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1270 if (req->newptr && curproc != p) {
1274 if (req->oldptr && (pa = p->p_args) != NULL) {
1275 refcount_acquire(&pa->ar_ref);
1276 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1277 if (refcount_release(&pa->ar_ref))
1280 if (req->newptr == NULL)
1283 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1287 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1288 refcount_init(&pa->ar_ref, 1);
1289 pa->ar_length = req->newlen;
1290 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1298 * Replace p_args with the new pa. p_args may have previously
1305 KKASSERT(opa->ar_ref > 0);
1306 if (refcount_release(&opa->ar_ref)) {
1307 kfree(opa, M_PARGS);
1313 lwkt_reltoken(&p->p_token);
1320 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1322 int *name = (int*) arg1;
1323 u_int namelen = arg2;
1326 char *fullpath, *freepath;
1327 struct ucred *cr1 = curproc->p_ucred;
1332 p = pfind((pid_t)name[0]);
1335 lwkt_gettoken(&p->p_token);
1338 * If we are not allowed to see other args, we certainly shouldn't
1339 * get the cwd either. Also check the usual trespassing.
1341 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1344 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1345 struct nchandle nch;
1347 cache_copy(&p->p_fd->fd_ncdir, &nch);
1348 error = cache_fullpath(p, &nch, NULL,
1349 &fullpath, &freepath, 0);
1353 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1354 kfree(freepath, M_TEMP);
1359 lwkt_reltoken(&p->p_token);
1365 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1367 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1368 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1370 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1371 sysctl_kern_proc, "Process table");
1373 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1374 sysctl_kern_proc, "Process table");
1376 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1377 sysctl_kern_proc, "Process table");
1379 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1380 sysctl_kern_proc, "Process table");
1382 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1383 sysctl_kern_proc, "Process table");
1385 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1386 sysctl_kern_proc, "Process table");
1388 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1389 sysctl_kern_proc, "Process table");
1391 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1392 sysctl_kern_proc, "Process table");
1394 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1395 sysctl_kern_proc, "Process table");
1397 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1398 sysctl_kern_proc, "Process table");
1400 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1401 sysctl_kern_proc, "Process table");
1403 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1404 sysctl_kern_proc_args, "Process argument list");
1406 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1407 sysctl_kern_proc_cwd, "Process argument list");