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);
180 * If someone is trying to single-step the process during
181 * an exec or an exit they can deadlock us because procfs
182 * sleeps with the process held.
185 if (p->p_flags & P_INEXEC) {
187 } else if (p->p_flags & P_POSTEXIT) {
188 spin_lock(&p->p_spin);
191 spin_unlock(&p->p_spin);
196 if (atomic_cmpset_int(&p->p_lock, o, n)) {
197 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
203 phold(struct proc *p)
212 if (atomic_cmpset_int(&p->p_lock, o, n))
218 prele(struct proc *p)
226 if (atomic_cmpset_int(&p->p_lock, 1, 0))
234 KKASSERT((o & PLOCK_MASK) > 0);
236 n = (o - 1) & ~PLOCK_WAITING;
237 if (atomic_cmpset_int(&p->p_lock, o, n)) {
238 if (o & PLOCK_WAITING)
246 * Is p an inferior of the current process?
249 * The caller must hold proc_token if the caller wishes a stable result.
252 inferior(struct proc *p)
254 lwkt_gettoken(&proc_token);
255 while (p != curproc) {
257 lwkt_reltoken(&proc_token);
262 lwkt_reltoken(&proc_token);
267 * Locate a process by number. The returned process will be referenced and
268 * must be released with PRELE().
277 lwkt_gettoken(&proc_token);
278 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
279 if (p->p_pid == pid) {
281 lwkt_reltoken(&proc_token);
285 lwkt_reltoken(&proc_token);
290 * Locate a process by number. The returned process is NOT referenced.
291 * The caller should hold proc_token if the caller wishes a stable result.
300 lwkt_gettoken(&proc_token);
301 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
302 if (p->p_pid == pid) {
303 lwkt_reltoken(&proc_token);
307 lwkt_reltoken(&proc_token);
312 pgref(struct pgrp *pgrp)
314 refcount_acquire(&pgrp->pg_refs);
318 pgrel(struct pgrp *pgrp)
320 if (refcount_release(&pgrp->pg_refs))
325 * Locate a process group by number. The returned process group will be
326 * referenced w/pgref() and must be released with pgrel() (or assigned
327 * somewhere if you wish to keep the reference).
336 lwkt_gettoken(&proc_token);
337 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
338 if (pgrp->pg_id == pgid) {
339 refcount_acquire(&pgrp->pg_refs);
340 lwkt_reltoken(&proc_token);
344 lwkt_reltoken(&proc_token);
349 * Move p to a new or existing process group (and session)
354 enterpgrp(struct proc *p, pid_t pgid, int mksess)
362 KASSERT(pgrp == NULL || !mksess,
363 ("enterpgrp: setsid into non-empty pgrp"));
364 KASSERT(!SESS_LEADER(p),
365 ("enterpgrp: session leader attempted setpgrp"));
368 pid_t savepid = p->p_pid;
373 KASSERT(p->p_pid == pgid,
374 ("enterpgrp: new pgrp and pid != pgid"));
375 if ((np = pfindn(savepid)) == NULL || np != p) {
379 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK);
381 struct session *sess;
386 sess = kmalloc(sizeof(struct session), M_SESSION,
389 sess->s_sid = p->p_pid;
391 sess->s_ttyvp = NULL;
393 bcopy(p->p_session->s_login, sess->s_login,
394 sizeof(sess->s_login));
395 pgrp->pg_session = sess;
396 KASSERT(p == curproc,
397 ("enterpgrp: mksession and p != curproc"));
398 lwkt_gettoken(&p->p_token);
399 p->p_flags &= ~P_CONTROLT;
400 lwkt_reltoken(&p->p_token);
402 pgrp->pg_session = p->p_session;
403 sess_hold(pgrp->pg_session);
406 LIST_INIT(&pgrp->pg_members);
407 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
409 SLIST_INIT(&pgrp->pg_sigiolst);
410 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
411 refcount_init(&pgrp->pg_refs, 1);
412 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
413 } else if (pgrp == p->p_pgrp) {
416 } /* else pgfind() referenced the pgrp */
419 * Adjust eligibility of affected pgrps to participate in job control.
420 * Increment eligibility counts before decrementing, otherwise we
421 * could reach 0 spuriously during the first call.
423 lwkt_gettoken(&pgrp->pg_token);
424 lwkt_gettoken(&p->p_token);
426 fixjobc(p, p->p_pgrp, 0);
427 while ((opgrp = p->p_pgrp) != NULL) {
429 lwkt_gettoken(&opgrp->pg_token);
430 LIST_REMOVE(p, p_pglist);
432 lwkt_reltoken(&opgrp->pg_token);
436 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
437 lwkt_reltoken(&p->p_token);
438 lwkt_reltoken(&pgrp->pg_token);
446 * Remove process from process group
451 leavepgrp(struct proc *p)
453 struct pgrp *pg = p->p_pgrp;
455 lwkt_gettoken(&p->p_token);
459 lwkt_gettoken(&pg->pg_token);
460 if (p->p_pgrp == pg) {
462 LIST_REMOVE(p, p_pglist);
465 lwkt_reltoken(&pg->pg_token);
466 lwkt_reltoken(&p->p_token); /* avoid chaining on rel */
469 lwkt_reltoken(&p->p_token);
475 * Delete a process group. Must be called only after the last ref has been
479 pgdelete(struct pgrp *pgrp)
482 * Reset any sigio structures pointing to us as a result of
483 * F_SETOWN with our pgid.
485 funsetownlst(&pgrp->pg_sigiolst);
487 if (pgrp->pg_session->s_ttyp != NULL &&
488 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
489 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
490 LIST_REMOVE(pgrp, pg_hash);
491 sess_rele(pgrp->pg_session);
496 * Adjust the ref count on a session structure. When the ref count falls to
497 * zero the tty is disassociated from the session and the session structure
498 * is freed. Note that tty assocation is not itself ref-counted.
503 sess_hold(struct session *sp)
505 lwkt_gettoken(&tty_token);
507 lwkt_reltoken(&tty_token);
514 sess_rele(struct session *sp)
518 KKASSERT(sp->s_count > 0);
519 lwkt_gettoken(&tty_token);
520 if (--sp->s_count == 0) {
521 if (sp->s_ttyp && sp->s_ttyp->t_session) {
522 #ifdef TTY_DO_FULL_CLOSE
523 /* FULL CLOSE, see ttyclearsession() */
524 KKASSERT(sp->s_ttyp->t_session == sp);
525 sp->s_ttyp->t_session = NULL;
527 /* HALF CLOSE, see ttyclearsession() */
528 if (sp->s_ttyp->t_session == sp)
529 sp->s_ttyp->t_session = NULL;
532 if ((tp = sp->s_ttyp) != NULL) {
536 kfree(sp, M_SESSION);
538 lwkt_reltoken(&tty_token);
542 * Adjust pgrp jobc counters when specified process changes process group.
543 * We count the number of processes in each process group that "qualify"
544 * the group for terminal job control (those with a parent in a different
545 * process group of the same session). If that count reaches zero, the
546 * process group becomes orphaned. Check both the specified process'
547 * process group and that of its children.
548 * entering == 0 => p is leaving specified group.
549 * entering == 1 => p is entering specified group.
554 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
556 struct pgrp *hispgrp;
557 struct session *mysession;
561 * Check p's parent to see whether p qualifies its own process
562 * group; if so, adjust count for p's process group.
564 lwkt_gettoken(&p->p_token); /* p_children scan */
565 lwkt_gettoken(&pgrp->pg_token);
567 mysession = pgrp->pg_session;
568 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
569 hispgrp->pg_session == mysession) {
572 else if (--pgrp->pg_jobc == 0)
577 * Check this process' children to see whether they qualify
578 * their process groups; if so, adjust counts for children's
581 LIST_FOREACH(np, &p->p_children, p_sibling) {
583 lwkt_gettoken(&np->p_token);
584 if ((hispgrp = np->p_pgrp) != pgrp &&
585 hispgrp->pg_session == mysession &&
586 np->p_stat != SZOMB) {
588 lwkt_gettoken(&hispgrp->pg_token);
591 else if (--hispgrp->pg_jobc == 0)
593 lwkt_reltoken(&hispgrp->pg_token);
596 lwkt_reltoken(&np->p_token);
599 KKASSERT(pgrp->pg_refs > 0);
600 lwkt_reltoken(&pgrp->pg_token);
601 lwkt_reltoken(&p->p_token);
605 * A process group has become orphaned;
606 * if there are any stopped processes in the group,
607 * hang-up all process in that group.
609 * The caller must hold pg_token.
612 orphanpg(struct pgrp *pg)
616 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
617 if (p->p_stat == SSTOP) {
618 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
628 * Add a new process to the allproc list and the PID hash. This
629 * also assigns a pid to the new process.
634 proc_add_allproc(struct proc *p)
638 if ((random_offset = randompid) != 0) {
640 random_offset = karc4random() % random_offset;
644 lwkt_gettoken(&proc_token);
645 p->p_pid = proc_getnewpid_locked(random_offset);
646 LIST_INSERT_HEAD(&allproc, p, p_list);
647 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
648 lwkt_reltoken(&proc_token);
652 * Calculate a new process pid. This function is integrated into
653 * proc_add_allproc() to guarentee that the new pid is not reused before
654 * the new process can be added to the allproc list.
656 * The caller must hold proc_token.
660 proc_getnewpid_locked(int random_offset)
662 static pid_t nextpid;
663 static pid_t pidchecked;
667 * Find an unused process ID. We remember a range of unused IDs
668 * ready to use (from nextpid+1 through pidchecked-1).
670 nextpid = nextpid + 1 + random_offset;
673 * If the process ID prototype has wrapped around,
674 * restart somewhat above 0, as the low-numbered procs
675 * tend to include daemons that don't exit.
677 if (nextpid >= PID_MAX) {
678 nextpid = nextpid % PID_MAX;
683 if (nextpid >= pidchecked) {
686 pidchecked = PID_MAX;
689 * Scan the active and zombie procs to check whether this pid
690 * is in use. Remember the lowest pid that's greater
691 * than nextpid, so we can avoid checking for a while.
693 * NOTE: Processes in the midst of being forked may not
694 * yet have p_pgrp and p_pgrp->pg_session set up
695 * yet, so we have to check for NULL.
697 * Processes being torn down should be interlocked
698 * with proc_token prior to the clearing of their
701 p = LIST_FIRST(&allproc);
703 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
704 while (p->p_pid == nextpid ||
705 (p->p_pgrp && p->p_pgrp->pg_id == nextpid) ||
706 (p->p_pgrp && p->p_session &&
707 p->p_session->s_sid == nextpid)) {
709 if (nextpid >= pidchecked)
712 if (p->p_pid > nextpid && pidchecked > p->p_pid)
713 pidchecked = p->p_pid;
715 p->p_pgrp->pg_id > nextpid &&
716 pidchecked > p->p_pgrp->pg_id) {
717 pidchecked = p->p_pgrp->pg_id;
719 if (p->p_pgrp && p->p_session &&
720 p->p_session->s_sid > nextpid &&
721 pidchecked > p->p_session->s_sid) {
722 pidchecked = p->p_session->s_sid;
727 p = LIST_FIRST(&zombproc);
735 * Called from exit1 to remove a process from the allproc
736 * list and move it to the zombie list.
738 * Caller must hold p->p_token. We are required to wait until p_lock
739 * becomes zero before we can manipulate the list, allowing allproc
740 * scans to guarantee consistency during a list scan.
743 proc_move_allproc_zombie(struct proc *p)
745 lwkt_gettoken(&proc_token);
746 PSTALL(p, "reap1", 0);
747 LIST_REMOVE(p, p_list);
748 LIST_INSERT_HEAD(&zombproc, p, p_list);
749 LIST_REMOVE(p, p_hash);
751 lwkt_reltoken(&proc_token);
756 * This routine is called from kern_wait() and will remove the process
757 * from the zombie list and the sibling list. This routine will block
758 * if someone has a lock on the proces (p_lock).
760 * Caller must hold p->p_token. We are required to wait until p_lock
761 * becomes zero before we can manipulate the list, allowing allproc
762 * scans to guarantee consistency during a list scan.
765 proc_remove_zombie(struct proc *p)
767 lwkt_gettoken(&proc_token);
768 PSTALL(p, "reap2", 0);
769 LIST_REMOVE(p, p_list); /* off zombproc */
770 LIST_REMOVE(p, p_sibling);
771 lwkt_reltoken(&proc_token);
775 * Scan all processes on the allproc list. The process is automatically
776 * held for the callback. A return value of -1 terminates the loop.
778 * The callback is made with the process held and proc_token held.
780 * We limit the scan to the number of processes as-of the start of
781 * the scan so as not to get caught up in an endless loop if new processes
782 * are created more quickly than we can scan the old ones. Add a little
783 * slop to try to catch edge cases since nprocs can race.
788 allproc_scan(int (*callback)(struct proc *, void *), void *data)
792 int limit = nprocs + ncpus;
795 * proc_token protects the allproc list and PHOLD() prevents the
796 * process from being removed from the allproc list or the zombproc
799 lwkt_gettoken(&proc_token);
800 LIST_FOREACH(p, &allproc, p_list) {
802 r = callback(p, data);
809 lwkt_reltoken(&proc_token);
813 * Scan all lwps of processes on the allproc list. The lwp is automatically
814 * held for the callback. A return value of -1 terminates the loop.
816 * The callback is made with the proces and lwp both held, and proc_token held.
821 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
828 * proc_token protects the allproc list and PHOLD() prevents the
829 * process from being removed from the allproc list or the zombproc
832 lwkt_gettoken(&proc_token);
833 LIST_FOREACH(p, &allproc, p_list) {
835 FOREACH_LWP_IN_PROC(lp, p) {
837 r = callback(lp, data);
844 lwkt_reltoken(&proc_token);
848 * Scan all processes on the zombproc list. The process is automatically
849 * held for the callback. A return value of -1 terminates the loop.
852 * The callback is made with the proces held and proc_token held.
855 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
860 lwkt_gettoken(&proc_token);
861 LIST_FOREACH(p, &zombproc, p_list) {
863 r = callback(p, data);
868 lwkt_reltoken(&proc_token);
878 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
884 for (i = 0; i <= pgrphash; i++) {
885 if (!LIST_EMPTY(&pgrphashtbl[i])) {
886 kprintf("\tindx %d\n", i);
887 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
889 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
890 (void *)pgrp, (long)pgrp->pg_id,
891 (void *)pgrp->pg_session,
892 pgrp->pg_session->s_count,
893 (void *)LIST_FIRST(&pgrp->pg_members));
894 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
895 kprintf("\t\tpid %ld addr %p pgrp %p\n",
896 (long)p->p_pid, (void *)p,
906 * Locate a process on the zombie list. Return a process or NULL.
907 * The returned process will be referenced and the caller must release
910 * No other requirements.
917 lwkt_gettoken(&proc_token);
918 LIST_FOREACH(p, &zombproc, p_list) {
919 if (p->p_pid == pid) {
921 lwkt_reltoken(&proc_token);
925 lwkt_reltoken(&proc_token);
930 * The caller must hold proc_token.
933 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
935 struct kinfo_proc ki;
937 int skp = 0, had_output = 0;
940 bzero(&ki, sizeof(ki));
941 lwkt_gettoken(&p->p_token);
942 fill_kinfo_proc(p, &ki);
943 if ((flags & KERN_PROC_FLAG_LWP) == 0)
946 FOREACH_LWP_IN_PROC(lp, p) {
948 fill_kinfo_lwp(lp, &ki.kp_lwp);
950 error = SYSCTL_OUT(req, &ki, sizeof(ki));
957 lwkt_reltoken(&p->p_token);
958 /* We need to output at least the proc, even if there is no lwp. */
959 if (had_output == 0) {
960 error = SYSCTL_OUT(req, &ki, sizeof(ki));
966 * The caller must hold proc_token.
969 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
971 struct kinfo_proc ki;
974 fill_kinfo_proc_kthread(td, &ki);
975 error = SYSCTL_OUT(req, &ki, sizeof(ki));
985 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
987 int *name = (int*) arg1;
988 int oid = oidp->oid_number;
989 u_int namelen = arg2;
991 struct proclist *plist;
993 struct thread *marker;
994 int doingzomb, flags = 0;
998 struct ucred *cr1 = curproc->p_ucred;
1000 flags = oid & KERN_PROC_FLAGMASK;
1001 oid &= ~KERN_PROC_FLAGMASK;
1003 if ((oid == KERN_PROC_ALL && namelen != 0) ||
1004 (oid != KERN_PROC_ALL && namelen != 1)) {
1009 * proc_token protects the allproc list and PHOLD() prevents the
1010 * process from being removed from the allproc list or the zombproc
1013 lwkt_gettoken(&proc_token);
1014 if (oid == KERN_PROC_PID) {
1015 p = pfindn((pid_t)name[0]);
1018 if (!PRISON_CHECK(cr1, p->p_ucred))
1021 error = sysctl_out_proc(p, req, flags);
1027 /* overestimate by 5 procs */
1028 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1032 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
1037 LIST_FOREACH(p, plist, p_list) {
1039 * Show a user only their processes.
1041 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
1044 * Skip embryonic processes.
1046 if (p->p_stat == SIDL)
1049 * TODO - make more efficient (see notes below).
1053 case KERN_PROC_PGRP:
1054 /* could do this by traversing pgrp */
1055 if (p->p_pgrp == NULL ||
1056 p->p_pgrp->pg_id != (pid_t)name[0])
1061 if ((p->p_flags & P_CONTROLT) == 0 ||
1062 p->p_session == NULL ||
1063 p->p_session->s_ttyp == NULL ||
1064 dev2udev(p->p_session->s_ttyp->t_dev) !=
1070 if (p->p_ucred == NULL ||
1071 p->p_ucred->cr_uid != (uid_t)name[0])
1075 case KERN_PROC_RUID:
1076 if (p->p_ucred == NULL ||
1077 p->p_ucred->cr_ruid != (uid_t)name[0])
1082 if (!PRISON_CHECK(cr1, p->p_ucred))
1085 error = sysctl_out_proc(p, req, flags);
1093 * Iterate over all active cpus and scan their thread list. Start
1094 * with the next logical cpu and end with our original cpu. We
1095 * migrate our own thread to each target cpu in order to safely scan
1096 * its thread list. In the last loop we migrate back to our original
1099 origcpu = mycpu->gd_cpuid;
1100 if (!ps_showallthreads || jailed(cr1))
1103 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1104 marker->td_flags = TDF_MARKER;
1107 for (n = 1; n <= ncpus; ++n) {
1111 nid = (origcpu + n) % ncpus;
1112 if ((smp_active_mask & CPUMASK(nid)) == 0)
1114 rgd = globaldata_find(nid);
1115 lwkt_setcpu_self(rgd);
1118 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1120 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1121 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1122 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1123 if (td->td_flags & TDF_MARKER)
1132 case KERN_PROC_PGRP:
1135 case KERN_PROC_RUID:
1138 error = sysctl_out_proc_kthread(td, req,
1147 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1153 kfree(marker, M_TEMP);
1156 lwkt_reltoken(&proc_token);
1161 * This sysctl allows a process to retrieve the argument list or process
1162 * title for another process without groping around in the address space
1163 * of the other process. It also allow a process to set its own "process
1164 * title to a string of its own choice.
1169 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1171 int *name = (int*) arg1;
1172 u_int namelen = arg2;
1177 struct ucred *cr1 = curproc->p_ucred;
1182 p = pfind((pid_t)name[0]);
1185 lwkt_gettoken(&p->p_token);
1187 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1190 if (req->newptr && curproc != p) {
1194 if (req->oldptr && (pa = p->p_args) != NULL) {
1195 refcount_acquire(&pa->ar_ref);
1196 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1197 if (refcount_release(&pa->ar_ref))
1200 if (req->newptr == NULL)
1203 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1207 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1208 refcount_init(&pa->ar_ref, 1);
1209 pa->ar_length = req->newlen;
1210 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1218 * Replace p_args with the new pa. p_args may have previously
1225 KKASSERT(opa->ar_ref > 0);
1226 if (refcount_release(&opa->ar_ref)) {
1227 kfree(opa, M_PARGS);
1233 lwkt_reltoken(&p->p_token);
1240 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1242 int *name = (int*) arg1;
1243 u_int namelen = arg2;
1246 char *fullpath, *freepath;
1247 struct ucred *cr1 = curproc->p_ucred;
1252 p = pfind((pid_t)name[0]);
1255 lwkt_gettoken(&p->p_token);
1258 * If we are not allowed to see other args, we certainly shouldn't
1259 * get the cwd either. Also check the usual trespassing.
1261 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1264 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1265 struct nchandle nch;
1267 cache_copy(&p->p_fd->fd_ncdir, &nch);
1268 error = cache_fullpath(p, &nch, &fullpath, &freepath, 0);
1272 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1273 kfree(freepath, M_TEMP);
1278 lwkt_reltoken(&p->p_token);
1284 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1286 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1287 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1289 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1290 sysctl_kern_proc, "Process table");
1292 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1293 sysctl_kern_proc, "Process table");
1295 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1296 sysctl_kern_proc, "Process table");
1298 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1299 sysctl_kern_proc, "Process table");
1301 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1302 sysctl_kern_proc, "Process table");
1304 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1305 sysctl_kern_proc, "Process table");
1307 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1308 sysctl_kern_proc, "Process table");
1310 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1311 sysctl_kern_proc, "Process table");
1313 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1314 sysctl_kern_proc, "Process table");
1316 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1317 sysctl_kern_proc, "Process table");
1319 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1320 sysctl_kern_proc, "Process table");
1322 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1323 sysctl_kern_proc_args, "Process argument list");
1325 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1326 sysctl_kern_proc_cwd, "Process argument list");