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 they can
181 * prevent us from going into zombie-land.
184 spin_lock(&p->p_spin);
187 spin_unlock(&p->p_spin);
191 if (atomic_cmpset_int(&p->p_lock, o, n)) {
192 tsleep(&p->p_lock, PINTERLOCKED, wmesg, 0);
198 phold(struct proc *p)
207 if (atomic_cmpset_int(&p->p_lock, o, n))
213 prele(struct proc *p)
221 if (atomic_cmpset_int(&p->p_lock, 1, 0))
229 KKASSERT((o & PLOCK_MASK) > 0);
231 n = (o - 1) & ~PLOCK_WAITING;
232 if (atomic_cmpset_int(&p->p_lock, o, n)) {
233 if (o & PLOCK_WAITING)
241 * Is p an inferior of the current process?
244 * The caller must hold proc_token if the caller wishes a stable result.
247 inferior(struct proc *p)
249 lwkt_gettoken(&proc_token);
250 while (p != curproc) {
252 lwkt_reltoken(&proc_token);
257 lwkt_reltoken(&proc_token);
262 * Locate a process by number. The returned process will be referenced and
263 * must be released with PRELE().
272 lwkt_gettoken(&proc_token);
273 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
274 if (p->p_pid == pid) {
276 lwkt_reltoken(&proc_token);
280 lwkt_reltoken(&proc_token);
285 * Locate a process by number. The returned process is NOT referenced.
286 * The caller should hold proc_token if the caller wishes a stable result.
295 lwkt_gettoken(&proc_token);
296 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
297 if (p->p_pid == pid) {
298 lwkt_reltoken(&proc_token);
302 lwkt_reltoken(&proc_token);
307 pgref(struct pgrp *pgrp)
309 refcount_acquire(&pgrp->pg_refs);
313 pgrel(struct pgrp *pgrp)
315 if (refcount_release(&pgrp->pg_refs))
320 * Locate a process group by number. The returned process group will be
321 * referenced w/pgref() and must be released with pgrel() (or assigned
322 * somewhere if you wish to keep the reference).
331 lwkt_gettoken(&proc_token);
332 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
333 if (pgrp->pg_id == pgid) {
334 refcount_acquire(&pgrp->pg_refs);
335 lwkt_reltoken(&proc_token);
339 lwkt_reltoken(&proc_token);
344 * Move p to a new or existing process group (and session)
349 enterpgrp(struct proc *p, pid_t pgid, int mksess)
357 KASSERT(pgrp == NULL || !mksess,
358 ("enterpgrp: setsid into non-empty pgrp"));
359 KASSERT(!SESS_LEADER(p),
360 ("enterpgrp: session leader attempted setpgrp"));
363 pid_t savepid = p->p_pid;
368 KASSERT(p->p_pid == pgid,
369 ("enterpgrp: new pgrp and pid != pgid"));
370 if ((np = pfindn(savepid)) == NULL || np != p) {
374 pgrp = kmalloc(sizeof(struct pgrp), M_PGRP, M_WAITOK);
376 struct session *sess;
381 sess = kmalloc(sizeof(struct session), M_SESSION,
384 sess->s_sid = p->p_pid;
386 sess->s_ttyvp = NULL;
388 bcopy(p->p_session->s_login, sess->s_login,
389 sizeof(sess->s_login));
390 pgrp->pg_session = sess;
391 KASSERT(p == curproc,
392 ("enterpgrp: mksession and p != curproc"));
393 lwkt_gettoken(&p->p_token);
394 p->p_flags &= ~P_CONTROLT;
395 lwkt_reltoken(&p->p_token);
397 pgrp->pg_session = p->p_session;
398 sess_hold(pgrp->pg_session);
401 LIST_INIT(&pgrp->pg_members);
402 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
404 SLIST_INIT(&pgrp->pg_sigiolst);
405 lwkt_token_init(&pgrp->pg_token, "pgrp_token");
406 refcount_init(&pgrp->pg_refs, 1);
407 lockinit(&pgrp->pg_lock, "pgwt", 0, 0);
408 } else if (pgrp == p->p_pgrp) {
411 } /* else pgfind() referenced the pgrp */
414 * Adjust eligibility of affected pgrps to participate in job control.
415 * Increment eligibility counts before decrementing, otherwise we
416 * could reach 0 spuriously during the first call.
418 lwkt_gettoken(&pgrp->pg_token);
419 lwkt_gettoken(&p->p_token);
421 fixjobc(p, p->p_pgrp, 0);
422 while ((opgrp = p->p_pgrp) != NULL) {
424 lwkt_gettoken(&opgrp->pg_token);
425 LIST_REMOVE(p, p_pglist);
427 lwkt_reltoken(&opgrp->pg_token);
431 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
432 lwkt_reltoken(&p->p_token);
433 lwkt_reltoken(&pgrp->pg_token);
441 * Remove process from process group
446 leavepgrp(struct proc *p)
448 struct pgrp *pg = p->p_pgrp;
450 lwkt_gettoken(&p->p_token);
454 lwkt_gettoken(&pg->pg_token);
455 if (p->p_pgrp == pg) {
457 LIST_REMOVE(p, p_pglist);
460 lwkt_reltoken(&pg->pg_token);
461 lwkt_reltoken(&p->p_token); /* avoid chaining on rel */
464 lwkt_reltoken(&p->p_token);
470 * Delete a process group. Must be called only after the last ref has been
474 pgdelete(struct pgrp *pgrp)
477 * Reset any sigio structures pointing to us as a result of
478 * F_SETOWN with our pgid.
480 funsetownlst(&pgrp->pg_sigiolst);
482 if (pgrp->pg_session->s_ttyp != NULL &&
483 pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
484 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
485 LIST_REMOVE(pgrp, pg_hash);
486 sess_rele(pgrp->pg_session);
491 * Adjust the ref count on a session structure. When the ref count falls to
492 * zero the tty is disassociated from the session and the session structure
493 * is freed. Note that tty assocation is not itself ref-counted.
498 sess_hold(struct session *sp)
500 lwkt_gettoken(&tty_token);
502 lwkt_reltoken(&tty_token);
509 sess_rele(struct session *sp)
513 KKASSERT(sp->s_count > 0);
514 lwkt_gettoken(&tty_token);
515 if (--sp->s_count == 0) {
516 if (sp->s_ttyp && sp->s_ttyp->t_session) {
517 #ifdef TTY_DO_FULL_CLOSE
518 /* FULL CLOSE, see ttyclearsession() */
519 KKASSERT(sp->s_ttyp->t_session == sp);
520 sp->s_ttyp->t_session = NULL;
522 /* HALF CLOSE, see ttyclearsession() */
523 if (sp->s_ttyp->t_session == sp)
524 sp->s_ttyp->t_session = NULL;
527 if ((tp = sp->s_ttyp) != NULL) {
531 kfree(sp, M_SESSION);
533 lwkt_reltoken(&tty_token);
537 * Adjust pgrp jobc counters when specified process changes process group.
538 * We count the number of processes in each process group that "qualify"
539 * the group for terminal job control (those with a parent in a different
540 * process group of the same session). If that count reaches zero, the
541 * process group becomes orphaned. Check both the specified process'
542 * process group and that of its children.
543 * entering == 0 => p is leaving specified group.
544 * entering == 1 => p is entering specified group.
549 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
551 struct pgrp *hispgrp;
552 struct session *mysession;
556 * Check p's parent to see whether p qualifies its own process
557 * group; if so, adjust count for p's process group.
559 lwkt_gettoken(&p->p_token); /* p_children scan */
560 lwkt_gettoken(&pgrp->pg_token);
562 mysession = pgrp->pg_session;
563 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
564 hispgrp->pg_session == mysession) {
567 else if (--pgrp->pg_jobc == 0)
572 * Check this process' children to see whether they qualify
573 * their process groups; if so, adjust counts for children's
576 LIST_FOREACH(np, &p->p_children, p_sibling) {
578 lwkt_gettoken(&np->p_token);
579 if ((hispgrp = np->p_pgrp) != pgrp &&
580 hispgrp->pg_session == mysession &&
581 np->p_stat != SZOMB) {
583 lwkt_gettoken(&hispgrp->pg_token);
586 else if (--hispgrp->pg_jobc == 0)
588 lwkt_reltoken(&hispgrp->pg_token);
591 lwkt_reltoken(&np->p_token);
594 KKASSERT(pgrp->pg_refs > 0);
595 lwkt_reltoken(&pgrp->pg_token);
596 lwkt_reltoken(&p->p_token);
600 * A process group has become orphaned;
601 * if there are any stopped processes in the group,
602 * hang-up all process in that group.
604 * The caller must hold pg_token.
607 orphanpg(struct pgrp *pg)
611 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
612 if (p->p_stat == SSTOP) {
613 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
623 * Add a new process to the allproc list and the PID hash. This
624 * also assigns a pid to the new process.
629 proc_add_allproc(struct proc *p)
633 if ((random_offset = randompid) != 0) {
635 random_offset = karc4random() % random_offset;
639 lwkt_gettoken(&proc_token);
640 p->p_pid = proc_getnewpid_locked(random_offset);
641 LIST_INSERT_HEAD(&allproc, p, p_list);
642 LIST_INSERT_HEAD(PIDHASH(p->p_pid), p, p_hash);
643 lwkt_reltoken(&proc_token);
647 * Calculate a new process pid. This function is integrated into
648 * proc_add_allproc() to guarentee that the new pid is not reused before
649 * the new process can be added to the allproc list.
651 * The caller must hold proc_token.
655 proc_getnewpid_locked(int random_offset)
657 static pid_t nextpid;
658 static pid_t pidchecked;
662 * Find an unused process ID. We remember a range of unused IDs
663 * ready to use (from nextpid+1 through pidchecked-1).
665 nextpid = nextpid + 1 + random_offset;
668 * If the process ID prototype has wrapped around,
669 * restart somewhat above 0, as the low-numbered procs
670 * tend to include daemons that don't exit.
672 if (nextpid >= PID_MAX) {
673 nextpid = nextpid % PID_MAX;
678 if (nextpid >= pidchecked) {
681 pidchecked = PID_MAX;
684 * Scan the active and zombie procs to check whether this pid
685 * is in use. Remember the lowest pid that's greater
686 * than nextpid, so we can avoid checking for a while.
688 * NOTE: Processes in the midst of being forked may not
689 * yet have p_pgrp and p_pgrp->pg_session set up
690 * yet, so we have to check for NULL.
692 * Processes being torn down should be interlocked
693 * with proc_token prior to the clearing of their
696 p = LIST_FIRST(&allproc);
698 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
699 while (p->p_pid == nextpid ||
700 (p->p_pgrp && p->p_pgrp->pg_id == nextpid) ||
701 (p->p_pgrp && p->p_session &&
702 p->p_session->s_sid == nextpid)) {
704 if (nextpid >= pidchecked)
707 if (p->p_pid > nextpid && pidchecked > p->p_pid)
708 pidchecked = p->p_pid;
710 p->p_pgrp->pg_id > nextpid &&
711 pidchecked > p->p_pgrp->pg_id) {
712 pidchecked = p->p_pgrp->pg_id;
714 if (p->p_pgrp && p->p_session &&
715 p->p_session->s_sid > nextpid &&
716 pidchecked > p->p_session->s_sid) {
717 pidchecked = p->p_session->s_sid;
722 p = LIST_FIRST(&zombproc);
730 * Called from exit1 to remove a process from the allproc
731 * list and move it to the zombie list.
733 * Caller must hold p->p_token. We are required to wait until p_lock
734 * becomes zero before we can manipulate the list, allowing allproc
735 * scans to guarantee consistency during a list scan.
738 proc_move_allproc_zombie(struct proc *p)
740 lwkt_gettoken(&proc_token);
741 PSTALL(p, "reap1", 0);
742 LIST_REMOVE(p, p_list);
743 LIST_INSERT_HEAD(&zombproc, p, p_list);
744 LIST_REMOVE(p, p_hash);
746 lwkt_reltoken(&proc_token);
751 * This routine is called from kern_wait() and will remove the process
752 * from the zombie list and the sibling list. This routine will block
753 * if someone has a lock on the proces (p_lock).
755 * Caller must hold p->p_token. We are required to wait until p_lock
756 * becomes zero before we can manipulate the list, allowing allproc
757 * scans to guarantee consistency during a list scan.
760 proc_remove_zombie(struct proc *p)
762 lwkt_gettoken(&proc_token);
763 PSTALL(p, "reap2", 0);
764 LIST_REMOVE(p, p_list); /* off zombproc */
765 LIST_REMOVE(p, p_sibling);
766 lwkt_reltoken(&proc_token);
770 * Scan all processes on the allproc list. The process is automatically
771 * held for the callback. A return value of -1 terminates the loop.
773 * The callback is made with the process held and proc_token held.
775 * We limit the scan to the number of processes as-of the start of
776 * the scan so as not to get caught up in an endless loop if new processes
777 * are created more quickly than we can scan the old ones. Add a little
778 * slop to try to catch edge cases since nprocs can race.
783 allproc_scan(int (*callback)(struct proc *, void *), void *data)
787 int limit = nprocs + ncpus;
790 * proc_token protects the allproc list and PHOLD() prevents the
791 * process from being removed from the allproc list or the zombproc
794 lwkt_gettoken(&proc_token);
795 LIST_FOREACH(p, &allproc, p_list) {
797 r = callback(p, data);
804 lwkt_reltoken(&proc_token);
808 * Scan all lwps of processes on the allproc list. The lwp is automatically
809 * held for the callback. A return value of -1 terminates the loop.
811 * The callback is made with the proces and lwp both held, and proc_token held.
816 alllwp_scan(int (*callback)(struct lwp *, void *), void *data)
823 * proc_token protects the allproc list and PHOLD() prevents the
824 * process from being removed from the allproc list or the zombproc
827 lwkt_gettoken(&proc_token);
828 LIST_FOREACH(p, &allproc, p_list) {
830 FOREACH_LWP_IN_PROC(lp, p) {
832 r = callback(lp, data);
839 lwkt_reltoken(&proc_token);
843 * Scan all processes on the zombproc list. The process is automatically
844 * held for the callback. A return value of -1 terminates the loop.
847 * The callback is made with the proces held and proc_token held.
850 zombproc_scan(int (*callback)(struct proc *, void *), void *data)
855 lwkt_gettoken(&proc_token);
856 LIST_FOREACH(p, &zombproc, p_list) {
858 r = callback(p, data);
863 lwkt_reltoken(&proc_token);
873 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
879 for (i = 0; i <= pgrphash; i++) {
880 if (!LIST_EMPTY(&pgrphashtbl[i])) {
881 kprintf("\tindx %d\n", i);
882 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
884 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
885 (void *)pgrp, (long)pgrp->pg_id,
886 (void *)pgrp->pg_session,
887 pgrp->pg_session->s_count,
888 (void *)LIST_FIRST(&pgrp->pg_members));
889 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
890 kprintf("\t\tpid %ld addr %p pgrp %p\n",
891 (long)p->p_pid, (void *)p,
901 * Locate a process on the zombie list. Return a process or NULL.
902 * The returned process will be referenced and the caller must release
905 * No other requirements.
912 lwkt_gettoken(&proc_token);
913 LIST_FOREACH(p, &zombproc, p_list) {
914 if (p->p_pid == pid) {
916 lwkt_reltoken(&proc_token);
920 lwkt_reltoken(&proc_token);
925 * The caller must hold proc_token.
928 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
930 struct kinfo_proc ki;
932 int skp = 0, had_output = 0;
935 bzero(&ki, sizeof(ki));
936 lwkt_gettoken(&p->p_token);
937 fill_kinfo_proc(p, &ki);
938 if ((flags & KERN_PROC_FLAG_LWP) == 0)
941 FOREACH_LWP_IN_PROC(lp, p) {
943 fill_kinfo_lwp(lp, &ki.kp_lwp);
945 error = SYSCTL_OUT(req, &ki, sizeof(ki));
952 lwkt_reltoken(&p->p_token);
953 /* We need to output at least the proc, even if there is no lwp. */
954 if (had_output == 0) {
955 error = SYSCTL_OUT(req, &ki, sizeof(ki));
961 * The caller must hold proc_token.
964 sysctl_out_proc_kthread(struct thread *td, struct sysctl_req *req, int flags)
966 struct kinfo_proc ki;
969 fill_kinfo_proc_kthread(td, &ki);
970 error = SYSCTL_OUT(req, &ki, sizeof(ki));
980 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
982 int *name = (int*) arg1;
983 int oid = oidp->oid_number;
984 u_int namelen = arg2;
986 struct proclist *plist;
988 struct thread *marker;
989 int doingzomb, flags = 0;
993 struct ucred *cr1 = curproc->p_ucred;
995 flags = oid & KERN_PROC_FLAGMASK;
996 oid &= ~KERN_PROC_FLAGMASK;
998 if ((oid == KERN_PROC_ALL && namelen != 0) ||
999 (oid != KERN_PROC_ALL && namelen != 1)) {
1004 * proc_token protects the allproc list and PHOLD() prevents the
1005 * process from being removed from the allproc list or the zombproc
1008 lwkt_gettoken(&proc_token);
1009 if (oid == KERN_PROC_PID) {
1010 p = pfindn((pid_t)name[0]);
1013 if (!PRISON_CHECK(cr1, p->p_ucred))
1016 error = sysctl_out_proc(p, req, flags);
1022 /* overestimate by 5 procs */
1023 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1027 for (doingzomb = 0; doingzomb <= 1; doingzomb++) {
1032 LIST_FOREACH(p, plist, p_list) {
1034 * Show a user only their processes.
1036 if ((!ps_showallprocs) && p_trespass(cr1, p->p_ucred))
1039 * Skip embryonic processes.
1041 if (p->p_stat == SIDL)
1044 * TODO - make more efficient (see notes below).
1048 case KERN_PROC_PGRP:
1049 /* could do this by traversing pgrp */
1050 if (p->p_pgrp == NULL ||
1051 p->p_pgrp->pg_id != (pid_t)name[0])
1056 if ((p->p_flags & P_CONTROLT) == 0 ||
1057 p->p_session == NULL ||
1058 p->p_session->s_ttyp == NULL ||
1059 dev2udev(p->p_session->s_ttyp->t_dev) !=
1065 if (p->p_ucred == NULL ||
1066 p->p_ucred->cr_uid != (uid_t)name[0])
1070 case KERN_PROC_RUID:
1071 if (p->p_ucred == NULL ||
1072 p->p_ucred->cr_ruid != (uid_t)name[0])
1077 if (!PRISON_CHECK(cr1, p->p_ucred))
1080 error = sysctl_out_proc(p, req, flags);
1088 * Iterate over all active cpus and scan their thread list. Start
1089 * with the next logical cpu and end with our original cpu. We
1090 * migrate our own thread to each target cpu in order to safely scan
1091 * its thread list. In the last loop we migrate back to our original
1094 origcpu = mycpu->gd_cpuid;
1095 if (!ps_showallthreads || jailed(cr1))
1098 marker = kmalloc(sizeof(struct thread), M_TEMP, M_WAITOK|M_ZERO);
1099 marker->td_flags = TDF_MARKER;
1102 for (n = 1; n <= ncpus; ++n) {
1106 nid = (origcpu + n) % ncpus;
1107 if ((smp_active_mask & CPUMASK(nid)) == 0)
1109 rgd = globaldata_find(nid);
1110 lwkt_setcpu_self(rgd);
1113 TAILQ_INSERT_TAIL(&rgd->gd_tdallq, marker, td_allq);
1115 while ((td = TAILQ_PREV(marker, lwkt_queue, td_allq)) != NULL) {
1116 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1117 TAILQ_INSERT_BEFORE(td, marker, td_allq);
1118 if (td->td_flags & TDF_MARKER)
1127 case KERN_PROC_PGRP:
1130 case KERN_PROC_RUID:
1133 error = sysctl_out_proc_kthread(td, req,
1142 TAILQ_REMOVE(&rgd->gd_tdallq, marker, td_allq);
1148 kfree(marker, M_TEMP);
1151 lwkt_reltoken(&proc_token);
1156 * This sysctl allows a process to retrieve the argument list or process
1157 * title for another process without groping around in the address space
1158 * of the other process. It also allow a process to set its own "process
1159 * title to a string of its own choice.
1164 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1166 int *name = (int*) arg1;
1167 u_int namelen = arg2;
1172 struct ucred *cr1 = curproc->p_ucred;
1177 p = pfind((pid_t)name[0]);
1180 lwkt_gettoken(&p->p_token);
1182 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1185 if (req->newptr && curproc != p) {
1189 if (req->oldptr && (pa = p->p_args) != NULL) {
1190 refcount_acquire(&pa->ar_ref);
1191 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1192 if (refcount_release(&pa->ar_ref))
1195 if (req->newptr == NULL)
1198 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit) {
1202 pa = kmalloc(sizeof(struct pargs) + req->newlen, M_PARGS, M_WAITOK);
1203 refcount_init(&pa->ar_ref, 1);
1204 pa->ar_length = req->newlen;
1205 error = SYSCTL_IN(req, pa->ar_args, req->newlen);
1213 * Replace p_args with the new pa. p_args may have previously
1220 KKASSERT(opa->ar_ref > 0);
1221 if (refcount_release(&opa->ar_ref)) {
1222 kfree(opa, M_PARGS);
1228 lwkt_reltoken(&p->p_token);
1235 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS)
1237 int *name = (int*) arg1;
1238 u_int namelen = arg2;
1241 char *fullpath, *freepath;
1242 struct ucred *cr1 = curproc->p_ucred;
1247 p = pfind((pid_t)name[0]);
1250 lwkt_gettoken(&p->p_token);
1253 * If we are not allowed to see other args, we certainly shouldn't
1254 * get the cwd either. Also check the usual trespassing.
1256 if ((!ps_argsopen) && p_trespass(cr1, p->p_ucred))
1259 if (req->oldptr && p->p_fd != NULL && p->p_fd->fd_ncdir.ncp) {
1260 struct nchandle nch;
1262 cache_copy(&p->p_fd->fd_ncdir, &nch);
1263 error = cache_fullpath(p, &nch, &fullpath, &freepath, 0);
1267 error = SYSCTL_OUT(req, fullpath, strlen(fullpath) + 1);
1268 kfree(freepath, M_TEMP);
1273 lwkt_reltoken(&p->p_token);
1279 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1281 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT,
1282 0, 0, sysctl_kern_proc, "S,proc", "Return entire process table");
1284 SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD,
1285 sysctl_kern_proc, "Process table");
1287 SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD,
1288 sysctl_kern_proc, "Process table");
1290 SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD,
1291 sysctl_kern_proc, "Process table");
1293 SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD,
1294 sysctl_kern_proc, "Process table");
1296 SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD,
1297 sysctl_kern_proc, "Process table");
1299 SYSCTL_NODE(_kern_proc, (KERN_PROC_ALL | KERN_PROC_FLAG_LWP), all_lwp, CTLFLAG_RD,
1300 sysctl_kern_proc, "Process table");
1302 SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_FLAG_LWP), pgrp_lwp, CTLFLAG_RD,
1303 sysctl_kern_proc, "Process table");
1305 SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_FLAG_LWP), tty_lwp, CTLFLAG_RD,
1306 sysctl_kern_proc, "Process table");
1308 SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_FLAG_LWP), uid_lwp, CTLFLAG_RD,
1309 sysctl_kern_proc, "Process table");
1311 SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_FLAG_LWP), ruid_lwp, CTLFLAG_RD,
1312 sysctl_kern_proc, "Process table");
1314 SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_FLAG_LWP), pid_lwp, CTLFLAG_RD,
1315 sysctl_kern_proc, "Process table");
1317 SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args, CTLFLAG_RW | CTLFLAG_ANYBODY,
1318 sysctl_kern_proc_args, "Process argument list");
1320 SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD | CTLFLAG_ANYBODY,
1321 sysctl_kern_proc_cwd, "Process argument list");