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_WAITING 0x40000000
163 #define PLOCK_MASK 0x3FFFFFFF
166 pstall(struct proc *p, const char *wmesg, int count)
174 if ((o & PLOCK_MASK) <= count)
176 n = o | PLOCK_WAITING;
177 tsleep_interlock(&p->p_lock, 0);
178 if (atomic_cmpset_int(&p->p_lock, o, n)) {
179 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
185 phold(struct proc *p)
194 if (atomic_cmpset_int(&p->p_lock, o, n))
200 prele(struct proc *p)
208 if (atomic_cmpset_int(&p->p_lock, 1, 0))
216 KKASSERT((o & PLOCK_MASK) > 0);
218 n = (o - 1) & ~PLOCK_WAITING;
219 if (atomic_cmpset_int(&p->p_lock, o, n)) {
220 if (o & PLOCK_WAITING)
228 * Is p an inferior of the current process?
231 * The caller must hold proc_token if the caller wishes a stable result.
234 inferior(struct proc *p)
236 lwkt_gettoken(&proc_token);
237 while (p != curproc) {
239 lwkt_reltoken(&proc_token);
244 lwkt_reltoken(&proc_token);
249 * Locate a process by number. The returned process will be referenced and
250 * must be released with PRELE().
259 lwkt_gettoken(&proc_token);
260 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
261 if (p->p_pid == pid) {
263 lwkt_reltoken(&proc_token);
267 lwkt_reltoken(&proc_token);
272 * Locate a process by number. The returned process is NOT referenced.
273 * The caller should hold proc_token if the caller wishes a stable result.
282 lwkt_gettoken(&proc_token);
283 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
284 if (p->p_pid == pid) {
285 lwkt_reltoken(&proc_token);
289 lwkt_reltoken(&proc_token);
294 pgref(struct pgrp *pgrp)
296 refcount_acquire(&pgrp->pg_refs);
300 pgrel(struct pgrp *pgrp)
302 if (refcount_release(&pgrp->pg_refs))
307 * Locate a process group by number. The returned process group will be
308 * referenced w/pgref() and must be released with pgrel() (or assigned
309 * somewhere if you wish to keep the reference).
318 lwkt_gettoken(&proc_token);
319 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
320 if (pgrp->pg_id == pgid) {
321 refcount_acquire(&pgrp->pg_refs);
322 lwkt_reltoken(&proc_token);
326 lwkt_reltoken(&proc_token);
331 * Move p to a new or existing process group (and session)
336 enterpgrp(struct proc *p, pid_t pgid, int mksess)
344 KASSERT(pgrp == NULL || !mksess,
345 ("enterpgrp: setsid into non-empty pgrp"));
346 KASSERT(!SESS_LEADER(p),
347 ("enterpgrp: session leader attempted setpgrp"));
350 pid_t savepid = p->p_pid;
355 KASSERT(p->p_pid == pgid,
356 ("enterpgrp: new pgrp and pid != pgid"));
357 if ((np = pfindn(savepid)) == NULL || np != p) {
361 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK);
363 struct session *sess;
368 sess = kmalloc(sizeof(struct session), M_SESSION,
371 sess->s_sid = p->p_pid;
373 sess->s_ttyvp = NULL;
375 bcopy(p->p_session->s_login, sess->s_login,
376 sizeof(sess->s_login));
377 pgrp->pg_session = sess;
378 KASSERT(p == curproc,
379 ("enterpgrp: mksession and p != curproc"));
380 lwkt_gettoken(&p->p_token);
381 p->p_flags &= ~P_CONTROLT;
382 lwkt_reltoken(&p->p_token);
384 pgrp->pg_session = p->p_session;
385 sess_hold(pgrp->pg_session);
388 LIST_INIT(&pgrp->pg_members);
389 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
391 SLIST_INIT(&pgrp->pg_sigiolst);
392 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
393 refcount_init(&pgrp->pg_refs, 1);
394 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
395 } else if (pgrp == p->p_pgrp) {
398 } /* else pgfind() referenced the pgrp */
401 * Adjust eligibility of affected pgrps to participate in job control.
402 * Increment eligibility counts before decrementing, otherwise we
403 * could reach 0 spuriously during the first call.
405 lwkt_gettoken(&pgrp->pg_token);
406 lwkt_gettoken(&p->p_token);
408 fixjobc(p, p->p_pgrp, 0);
409 while ((opgrp = p->p_pgrp) != NULL) {
411 lwkt_gettoken(&opgrp->pg_token);
412 LIST_REMOVE(p, p_pglist);
414 lwkt_reltoken(&opgrp->pg_token);
418 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
419 lwkt_reltoken(&p->p_token);
420 lwkt_reltoken(&pgrp->pg_token);
428 * Remove process from process group
433 leavepgrp(struct proc *p)
435 struct pgrp *pg = p->p_pgrp;
437 lwkt_gettoken(&p->p_token);
441 lwkt_gettoken(&pg->pg_token);
442 if (p->p_pgrp == pg) {
444 LIST_REMOVE(p, p_pglist);
447 lwkt_reltoken(&pg->pg_token);
448 lwkt_reltoken(&p->p_token); /* avoid chaining on rel */
451 lwkt_reltoken(&p->p_token);
457 * Delete a process group. Must be called only after the last ref has been
461 pgdelete(struct pgrp *pgrp)
464 * Reset any sigio structures pointing to us as a result of
465 * F_SETOWN with our pgid.
467 funsetownlst(&pgrp->pg_sigiolst);
469 if (pgrp->pg_session->s_ttyp != NULL &&
470 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
471 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
472 LIST_REMOVE(pgrp, pg_hash);
473 sess_rele(pgrp->pg_session);
478 * Adjust the ref count on a session structure. When the ref count falls to
479 * zero the tty is disassociated from the session and the session structure
480 * is freed. Note that tty assocation is not itself ref-counted.
485 sess_hold(struct session *sp)
487 lwkt_gettoken(&tty_token);
489 lwkt_reltoken(&tty_token);
496 sess_rele(struct session *sp)
500 KKASSERT(sp->s_count > 0);
501 lwkt_gettoken(&tty_token);
502 if (--sp->s_count == 0) {
503 if (sp->s_ttyp && sp->s_ttyp->t_session) {
504 #ifdef TTY_DO_FULL_CLOSE
505 /* FULL CLOSE, see ttyclearsession() */
506 KKASSERT(sp->s_ttyp->t_session == sp);
507 sp->s_ttyp->t_session = NULL;
509 /* HALF CLOSE, see ttyclearsession() */
510 if (sp->s_ttyp->t_session == sp)
511 sp->s_ttyp->t_session = NULL;
514 if ((tp = sp->s_ttyp) != NULL) {
518 kfree(sp, M_SESSION);
520 lwkt_reltoken(&tty_token);
524 * Adjust pgrp jobc counters when specified process changes process group.
525 * We count the number of processes in each process group that "qualify"
526 * the group for terminal job control (those with a parent in a different
527 * process group of the same session). If that count reaches zero, the
528 * process group becomes orphaned. Check both the specified process'
529 * process group and that of its children.
530 * entering == 0 => p is leaving specified group.
531 * entering == 1 => p is entering specified group.
536 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
538 struct pgrp *hispgrp;
539 struct session *mysession;
543 * Check p's parent to see whether p qualifies its own process
544 * group; if so, adjust count for p's process group.
546 lwkt_gettoken(&p->p_token); /* p_children scan */
547 lwkt_gettoken(&pgrp->pg_token);
549 mysession = pgrp->pg_session;
550 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
551 hispgrp->pg_session == mysession) {
554 else if (--pgrp->pg_jobc == 0)
559 * Check this process' children to see whether they qualify
560 * their process groups; if so, adjust counts for children's
563 LIST_FOREACH(np, &p->p_children, p_sibling) {
565 lwkt_gettoken(&np->p_token);
566 if ((hispgrp = np->p_pgrp) != pgrp &&
567 hispgrp->pg_session == mysession &&
568 np->p_stat != SZOMB) {
570 lwkt_gettoken(&hispgrp->pg_token);
573 else if (--hispgrp->pg_jobc == 0)
575 lwkt_reltoken(&hispgrp->pg_token);
578 lwkt_reltoken(&np->p_token);
581 KKASSERT(pgrp->pg_refs > 0);
582 lwkt_reltoken(&pgrp->pg_token);
583 lwkt_reltoken(&p->p_token);
587 * A process group has become orphaned;
588 * if there are any stopped processes in the group,
589 * hang-up all process in that group.
591 * The caller must hold pg_token.
594 orphanpg(struct pgrp *pg)
598 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
599 if (p->p_stat == SSTOP) {
600 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
610 * Add a new process to the allproc list and the PID hash. This
611 * also assigns a pid to the new process.
616 proc_add_allproc(struct proc *p)
620 if ((random_offset = randompid) != 0) {
622 random_offset = karc4random() % random_offset;
626 lwkt_gettoken(&proc_token);
627 p->p_pid = proc_getnewpid_locked(random_offset);
628 LIST_INSERT_HEAD(&allproc, p, p_list);
629 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
630 lwkt_reltoken(&proc_token);
634 * Calculate a new process pid. This function is integrated into
635 * proc_add_allproc() to guarentee that the new pid is not reused before
636 * the new process can be added to the allproc list.
638 * The caller must hold proc_token.
642 proc_getnewpid_locked(int random_offset)
644 static pid_t nextpid;
645 static pid_t pidchecked;
649 * Find an unused process ID. We remember a range of unused IDs
650 * ready to use (from nextpid+1 through pidchecked-1).
652 nextpid = nextpid + 1 + random_offset;
655 * If the process ID prototype has wrapped around,
656 * restart somewhat above 0, as the low-numbered procs
657 * tend to include daemons that don't exit.
659 if (nextpid >= PID_MAX) {
660 nextpid = nextpid % PID_MAX;
665 if (nextpid >= pidchecked) {
668 pidchecked = PID_MAX;
671 * Scan the active and zombie procs to check whether this pid
672 * is in use. Remember the lowest pid that's greater
673 * than nextpid, so we can avoid checking for a while.
675 * NOTE: Processes in the midst of being forked may not
676 * yet have p_pgrp and p_pgrp->pg_session set up
677 * yet, so we have to check for NULL.
679 * Processes being torn down should be interlocked
680 * with proc_token prior to the clearing of their
683 p = LIST_FIRST(&allproc);
685 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
686 while (p->p_pid == nextpid ||
687 (p->p_pgrp && p->p_pgrp->pg_id == nextpid) ||
688 (p->p_pgrp && p->p_session &&
689 p->p_session->s_sid == nextpid)) {
691 if (nextpid >= pidchecked)
694 if (p->p_pid > nextpid && pidchecked > p->p_pid)
695 pidchecked = p->p_pid;
697 p->p_pgrp->pg_id > nextpid &&
698 pidchecked > p->p_pgrp->pg_id) {
699 pidchecked = p->p_pgrp->pg_id;
701 if (p->p_pgrp && p->p_session &&
702 p->p_session->s_sid > nextpid &&
703 pidchecked > p->p_session->s_sid) {
704 pidchecked = p->p_session->s_sid;
709 p = LIST_FIRST(&zombproc);
717 * Called from exit1 to remove a process from the allproc
718 * list and move it to the zombie list.
720 * Caller must hold p->p_token. We are required to wait until p_lock
721 * becomes zero before we can manipulate the list, allowing allproc
722 * scans to guarantee consistency during a list scan.
725 proc_move_allproc_zombie(struct proc *p)
727 lwkt_gettoken(&proc_token);
728 PSTALL(p, "reap1", 0);
729 LIST_REMOVE(p, p_list);
730 LIST_INSERT_HEAD(&zombproc, p, p_list);
731 LIST_REMOVE(p, p_hash);
733 lwkt_reltoken(&proc_token);
738 * This routine is called from kern_wait() and will remove the process
739 * from the zombie list and the sibling list. This routine will block
740 * if someone has a lock on the proces (p_lock).
742 * Caller must hold p->p_token. We are required to wait until p_lock
743 * becomes zero before we can manipulate the list, allowing allproc
744 * scans to guarantee consistency during a list scan.
747 proc_remove_zombie(struct proc *p)
749 lwkt_gettoken(&proc_token);
750 PSTALL(p, "reap2", 0);
751 LIST_REMOVE(p, p_list); /* off zombproc */
752 LIST_REMOVE(p, p_sibling);
753 lwkt_reltoken(&proc_token);
757 * Scan all processes on the allproc list. The process is automatically
758 * held for the callback. A return value of -1 terminates the loop.
760 * The callback is made with the process held and proc_token held.
762 * We limit the scan to the number of processes as-of the start of
763 * the scan so as not to get caught up in an endless loop if new processes
764 * are created more quickly than we can scan the old ones. Add a little
765 * slop to try to catch edge cases since nprocs can race.
770 allproc_scan(int (*callback)(struct proc *, void *), void *data)
774 int limit = nprocs + ncpus;
777 * proc_token protects the allproc list and PHOLD() prevents the
778 * process from being removed from the allproc list or the zombproc
781 lwkt_gettoken(&proc_token);
782 LIST_FOREACH(p, &allproc, p_list) {
784 r = callback(p, data);
791 lwkt_reltoken(&proc_token);
795 * Scan all lwps of processes on the allproc list. The lwp is automatically
796 * held for the callback. A return value of -1 terminates the loop.
798 * The callback is made with the proces and lwp both held, and proc_token held.
803 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
810 * proc_token protects the allproc list and PHOLD() prevents the
811 * process from being removed from the allproc list or the zombproc
814 lwkt_gettoken(&proc_token);
815 LIST_FOREACH(p, &allproc, p_list) {
817 FOREACH_LWP_IN_PROC(lp, p) {
819 r = callback(lp, data);
826 lwkt_reltoken(&proc_token);
830 * Scan all processes on the zombproc list. The process is automatically
831 * held for the callback. A return value of -1 terminates the loop.
834 * The callback is made with the proces held and proc_token held.
837 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
842 lwkt_gettoken(&proc_token);
843 LIST_FOREACH(p, &zombproc, p_list) {
845 r = callback(p, data);
850 lwkt_reltoken(&proc_token);
860 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
866 for (i = 0; i <= pgrphash; i++) {
867 if (!LIST_EMPTY(&pgrphashtbl[i])) {
868 kprintf("\tindx %d\n", i);
869 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
871 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
872 (void *)pgrp, (long)pgrp->pg_id,
873 (void *)pgrp->pg_session,
874 pgrp->pg_session->s_count,
875 (void *)LIST_FIRST(&pgrp->pg_members));
876 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
877 kprintf("\t\tpid %ld addr %p pgrp %p\n",
878 (long)p->p_pid, (void *)p,
888 * Locate a process on the zombie list. Return a process or NULL.
889 * The returned process will be referenced and the caller must release
892 * No other requirements.
899 lwkt_gettoken(&proc_token);
900 LIST_FOREACH(p, &zombproc, p_list) {
901 if (p->p_pid == pid) {
903 lwkt_reltoken(&proc_token);
907 lwkt_reltoken(&proc_token);
912 * The caller must hold proc_token.
915 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
917 struct kinfo_proc ki;
919 int skp = 0, had_output = 0;
922 bzero(&ki, sizeof(ki));
923 lwkt_gettoken(&p->p_token);
924 fill_kinfo_proc(p, &ki);
925 if ((flags & KERN_PROC_FLAG_LWP) == 0)
928 FOREACH_LWP_IN_PROC(lp, p) {
930 fill_kinfo_lwp(lp, &ki.kp_lwp);
932 error = SYSCTL_OUT(req, &ki, sizeof(ki));
939 lwkt_reltoken(&p->p_token);
940 /* We need to output at least the proc, even if there is no lwp. */
941 if (had_output == 0) {
942 error = SYSCTL_OUT(req, &ki, sizeof(ki));
948 * The caller must hold proc_token.
951 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
953 struct kinfo_proc ki;
956 fill_kinfo_proc_kthread(td, &ki);
957 error = SYSCTL_OUT(req, &ki, sizeof(ki));
967 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
969 int *name = (int*) arg1;
970 int oid = oidp->oid_number;
971 u_int namelen = arg2;
973 struct proclist *plist;
975 struct thread *marker;
976 int doingzomb, flags = 0;
980 struct ucred *cr1 = curproc->p_ucred;
982 flags = oid & KERN_PROC_FLAGMASK;
983 oid &= ~KERN_PROC_FLAGMASK;
985 if ((oid == KERN_PROC_ALL && namelen != 0) ||
986 (oid != KERN_PROC_ALL && namelen != 1)) {
991 * proc_token protects the allproc list and PHOLD() prevents the
992 * process from being removed from the allproc list or the zombproc
995 lwkt_gettoken(&proc_token);
996 if (oid == KERN_PROC_PID) {
997 p = pfindn((pid_t)name[0]);
1000 if (!PRISON_CHECK(cr1, p->p_ucred))
1003 error = sysctl_out_proc(p, req, flags);
1009 /* overestimate by 5 procs */
1010 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1014 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
1019 LIST_FOREACH(p, plist, p_list) {
1021 * Show a user only their processes.
1023 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
1026 * Skip embryonic processes.
1028 if (p->p_stat == SIDL)
1031 * TODO - make more efficient (see notes below).
1035 case KERN_PROC_PGRP:
1036 /* could do this by traversing pgrp */
1037 if (p->p_pgrp == NULL ||
1038 p->p_pgrp->pg_id != (pid_t)name[0])
1043 if ((p->p_flags & P_CONTROLT) == 0 ||
1044 p->p_session == NULL ||
1045 p->p_session->s_ttyp == NULL ||
1046 dev2udev(p->p_session->s_ttyp->t_dev) !=
1052 if (p->p_ucred == NULL ||
1053 p->p_ucred->cr_uid != (uid_t)name[0])
1057 case KERN_PROC_RUID:
1058 if (p->p_ucred == NULL ||
1059 p->p_ucred->cr_ruid != (uid_t)name[0])
1064 if (!PRISON_CHECK(cr1, p->p_ucred))
1067 error = sysctl_out_proc(p, req, flags);
1075 * Iterate over all active cpus and scan their thread list. Start
1076 * with the next logical cpu and end with our original cpu. We
1077 * migrate our own thread to each target cpu in order to safely scan
1078 * its thread list. In the last loop we migrate back to our original
1081 origcpu = mycpu->gd_cpuid;
1082 if (!ps_showallthreads || jailed(cr1))
1085 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1086 marker->td_flags = TDF_MARKER;
1089 for (n = 1; n <= ncpus; ++n) {
1093 nid = (origcpu + n) % ncpus;
1094 if ((smp_active_mask & CPUMASK(nid)) == 0)
1096 rgd = globaldata_find(nid);
1097 lwkt_setcpu_self(rgd);
1100 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1102 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1103 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1104 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1105 if (td->td_flags & TDF_MARKER)
1114 case KERN_PROC_PGRP:
1117 case KERN_PROC_RUID:
1120 error = sysctl_out_proc_kthread(td, req,
1129 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1135 kfree(marker, M_TEMP);
1138 lwkt_reltoken(&proc_token);
1143 * This sysctl allows a process to retrieve the argument list or process
1144 * title for another process without groping around in the address space
1145 * of the other process. It also allow a process to set its own "process
1146 * title to a string of its own choice.
1151 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1153 int *name = (int*) arg1;
1154 u_int namelen = arg2;
1159 struct ucred *cr1 = curproc->p_ucred;
1164 p = pfind((pid_t)name[0]);
1167 lwkt_gettoken(&p->p_token);
1169 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1172 if (req->newptr && curproc != p) {
1176 if (req->oldptr && (pa = p->p_args) != NULL) {
1177 refcount_acquire(&pa->ar_ref);
1178 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1179 if (refcount_release(&pa->ar_ref))
1182 if (req->newptr == NULL)
1185 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1189 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1190 refcount_init(&pa->ar_ref, 1);
1191 pa->ar_length = req->newlen;
1192 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1200 * Replace p_args with the new pa. p_args may have previously
1207 KKASSERT(opa->ar_ref > 0);
1208 if (refcount_release(&opa->ar_ref)) {
1209 kfree(opa, M_PARGS);
1215 lwkt_reltoken(&p->p_token);
1222 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1224 int *name = (int*) arg1;
1225 u_int namelen = arg2;
1228 char *fullpath, *freepath;
1229 struct ucred *cr1 = curproc->p_ucred;
1234 p = pfind((pid_t)name[0]);
1237 lwkt_gettoken(&p->p_token);
1240 * If we are not allowed to see other args, we certainly shouldn't
1241 * get the cwd either. Also check the usual trespassing.
1243 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1246 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1247 struct nchandle nch;
1249 cache_copy(&p->p_fd->fd_ncdir, &nch);
1250 error = cache_fullpath(p, &nch, &fullpath, &freepath, 0);
1254 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1255 kfree(freepath, M_TEMP);
1260 lwkt_reltoken(&p->p_token);
1266 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1268 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1269 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1271 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1272 sysctl_kern_proc, "Process table");
1274 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1275 sysctl_kern_proc, "Process table");
1277 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1278 sysctl_kern_proc, "Process table");
1280 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1281 sysctl_kern_proc, "Process table");
1283 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1284 sysctl_kern_proc, "Process table");
1286 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1287 sysctl_kern_proc, "Process table");
1289 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1290 sysctl_kern_proc, "Process table");
1292 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1293 sysctl_kern_proc, "Process table");
1295 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1296 sysctl_kern_proc, "Process table");
1298 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1299 sysctl_kern_proc, "Process table");
1301 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1302 sysctl_kern_proc, "Process table");
1304 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1305 sysctl_kern_proc_args, "Process argument list");
1307 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1308 sysctl_kern_proc_cwd, "Process argument list");