2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include "opt_kdtrace.h"
38 #include "opt_ktrace.h"
39 #include "opt_kstack_pages.h"
40 #include "opt_stack.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mutex.h>
52 #include <sys/refcount.h>
54 #include <sys/sysent.h>
55 #include <sys/sched.h>
57 #include <sys/stack.h>
58 #include <sys/sysctl.h>
59 #include <sys/filedesc.h>
61 #include <sys/signalvar.h>
66 #include <sys/vnode.h>
67 #include <sys/eventhandler.h>
74 #include <vm/vm_extern.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
81 #ifdef COMPAT_FREEBSD32
82 #include <compat/freebsd32/freebsd32.h>
83 #include <compat/freebsd32/freebsd32_util.h>
86 SDT_PROVIDER_DEFINE(proc);
87 SDT_PROBE_DEFINE(proc, kernel, ctor, entry, entry);
88 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
89 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
92 SDT_PROBE_DEFINE(proc, kernel, ctor, return, return);
93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
94 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
95 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
96 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
97 SDT_PROBE_DEFINE(proc, kernel, dtor, entry, entry);
98 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
99 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
102 SDT_PROBE_DEFINE(proc, kernel, dtor, return, return);
103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
104 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
105 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
106 SDT_PROBE_DEFINE(proc, kernel, init, entry, entry);
107 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
108 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
109 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
110 SDT_PROBE_DEFINE(proc, kernel, init, return, return);
111 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
112 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
113 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
115 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
116 MALLOC_DEFINE(M_SESSION, "session", "session header");
117 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
118 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
120 static void doenterpgrp(struct proc *, struct pgrp *);
121 static void orphanpg(struct pgrp *pg);
122 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
123 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
124 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
126 static void pgadjustjobc(struct pgrp *pgrp, int entering);
127 static void pgdelete(struct pgrp *);
128 static int proc_ctor(void *mem, int size, void *arg, int flags);
129 static void proc_dtor(void *mem, int size, void *arg);
130 static int proc_init(void *mem, int size, int flags);
131 static void proc_fini(void *mem, int size);
132 static void pargs_free(struct pargs *pa);
135 * Other process lists
137 struct pidhashhead *pidhashtbl;
139 struct pgrphashhead *pgrphashtbl;
141 struct proclist allproc;
142 struct proclist zombproc;
143 struct sx allproc_lock;
144 struct sx proctree_lock;
145 struct mtx ppeers_lock;
146 uma_zone_t proc_zone;
148 int kstack_pages = KSTACK_PAGES;
149 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
150 "Kernel stack size in pages");
152 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
153 #ifdef COMPAT_FREEBSD32
154 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
158 * Initialize global process hashing structures.
164 sx_init(&allproc_lock, "allproc");
165 sx_init(&proctree_lock, "proctree");
166 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
168 LIST_INIT(&zombproc);
169 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
170 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
171 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
172 proc_ctor, proc_dtor, proc_init, proc_fini,
173 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
178 * Prepare a proc for use.
181 proc_ctor(void *mem, int size, void *arg, int flags)
185 p = (struct proc *)mem;
186 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
187 EVENTHANDLER_INVOKE(process_ctor, p);
188 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
193 * Reclaim a proc after use.
196 proc_dtor(void *mem, int size, void *arg)
201 /* INVARIANTS checks go here */
202 p = (struct proc *)mem;
203 td = FIRST_THREAD_IN_PROC(p);
204 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
207 KASSERT((p->p_numthreads == 1),
208 ("bad number of threads in exiting process"));
209 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
211 /* Free all OSD associated to this thread. */
214 EVENTHANDLER_INVOKE(process_dtor, p);
215 if (p->p_ksi != NULL)
216 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
217 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
221 * Initialize type-stable parts of a proc (when newly created).
224 proc_init(void *mem, int size, int flags)
228 p = (struct proc *)mem;
229 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
230 p->p_sched = (struct p_sched *)&p[1];
231 bzero(&p->p_mtx, sizeof(struct mtx));
232 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
233 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
234 cv_init(&p->p_pwait, "ppwait");
235 cv_init(&p->p_dbgwait, "dbgwait");
236 TAILQ_INIT(&p->p_threads); /* all threads in proc */
237 EVENTHANDLER_INVOKE(process_init, p);
238 p->p_stats = pstats_alloc();
239 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
244 * UMA should ensure that this function is never called.
245 * Freeing a proc structure would violate type stability.
248 proc_fini(void *mem, int size)
253 p = (struct proc *)mem;
254 EVENTHANDLER_INVOKE(process_fini, p);
255 pstats_free(p->p_stats);
256 thread_free(FIRST_THREAD_IN_PROC(p));
257 mtx_destroy(&p->p_mtx);
258 if (p->p_ksi != NULL)
259 ksiginfo_free(p->p_ksi);
261 panic("proc reclaimed");
266 * Is p an inferior of the current process?
270 register struct proc *p;
273 sx_assert(&proctree_lock, SX_LOCKED);
274 for (; p != curproc; p = p->p_pptr)
281 * Locate a process by number; return only "live" processes -- i.e., neither
282 * zombies nor newly born but incompletely initialized processes. By not
283 * returning processes in the PRS_NEW state, we allow callers to avoid
284 * testing for that condition to avoid dereferencing p_ucred, et al.
290 register struct proc *p;
292 sx_slock(&allproc_lock);
293 LIST_FOREACH(p, PIDHASH(pid), p_hash)
294 if (p->p_pid == pid) {
296 if (p->p_state == PRS_NEW) {
302 sx_sunlock(&allproc_lock);
312 sx_slock(&allproc_lock);
313 FOREACH_PROC_IN_SYSTEM(p) {
315 if (p->p_state == PRS_NEW) {
319 FOREACH_THREAD_IN_PROC(p, td) {
320 if (td->td_tid == tid)
326 sx_sunlock(&allproc_lock);
331 * Locate a process group by number.
332 * The caller must hold proctree_lock.
338 register struct pgrp *pgrp;
340 sx_assert(&proctree_lock, SX_LOCKED);
342 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
343 if (pgrp->pg_id == pgid) {
352 * Locate process and do additional manipulations, depending on flags.
355 pget(pid_t pid, int flags, struct proc **pp)
362 else if ((flags & PGET_NOTID) == 0)
368 if ((flags & PGET_CANSEE) != 0) {
369 error = p_cansee(curthread, p);
373 if ((flags & PGET_CANDEBUG) != 0) {
374 error = p_candebug(curthread, p);
378 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
382 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
386 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
388 * XXXRW: Not clear ESRCH is the right error during proc
394 if ((flags & PGET_HOLD) != 0) {
406 * Create a new process group.
407 * pgid must be equal to the pid of p.
408 * Begin a new session if required.
411 enterpgrp(p, pgid, pgrp, sess)
412 register struct proc *p;
415 struct session *sess;
418 sx_assert(&proctree_lock, SX_XLOCKED);
420 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
421 KASSERT(p->p_pid == pgid,
422 ("enterpgrp: new pgrp and pid != pgid"));
423 KASSERT(pgfind(pgid) == NULL,
424 ("enterpgrp: pgrp with pgid exists"));
425 KASSERT(!SESS_LEADER(p),
426 ("enterpgrp: session leader attempted setpgrp"));
428 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
434 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
436 p->p_flag &= ~P_CONTROLT;
440 sess->s_sid = p->p_pid;
441 refcount_init(&sess->s_count, 1);
442 sess->s_ttyvp = NULL;
444 bcopy(p->p_session->s_login, sess->s_login,
445 sizeof(sess->s_login));
446 pgrp->pg_session = sess;
447 KASSERT(p == curproc,
448 ("enterpgrp: mksession and p != curproc"));
450 pgrp->pg_session = p->p_session;
451 sess_hold(pgrp->pg_session);
455 LIST_INIT(&pgrp->pg_members);
458 * As we have an exclusive lock of proctree_lock,
459 * this should not deadlock.
461 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
463 SLIST_INIT(&pgrp->pg_sigiolst);
466 doenterpgrp(p, pgrp);
472 * Move p to an existing process group
475 enterthispgrp(p, pgrp)
476 register struct proc *p;
480 sx_assert(&proctree_lock, SX_XLOCKED);
481 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
482 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
483 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
484 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
485 KASSERT(pgrp->pg_session == p->p_session,
486 ("%s: pgrp's session %p, p->p_session %p.\n",
490 KASSERT(pgrp != p->p_pgrp,
491 ("%s: p belongs to pgrp.", __func__));
493 doenterpgrp(p, pgrp);
499 * Move p to a process group
506 struct pgrp *savepgrp;
508 sx_assert(&proctree_lock, SX_XLOCKED);
509 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
510 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
511 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
512 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
514 savepgrp = p->p_pgrp;
517 * Adjust eligibility of affected pgrps to participate in job control.
518 * Increment eligibility counts before decrementing, otherwise we
519 * could reach 0 spuriously during the first call.
522 fixjobc(p, p->p_pgrp, 0);
527 LIST_REMOVE(p, p_pglist);
530 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
531 PGRP_UNLOCK(savepgrp);
533 if (LIST_EMPTY(&savepgrp->pg_members))
538 * remove process from process group
542 register struct proc *p;
544 struct pgrp *savepgrp;
546 sx_assert(&proctree_lock, SX_XLOCKED);
547 savepgrp = p->p_pgrp;
550 LIST_REMOVE(p, p_pglist);
553 PGRP_UNLOCK(savepgrp);
554 if (LIST_EMPTY(&savepgrp->pg_members))
560 * delete a process group
564 register struct pgrp *pgrp;
566 struct session *savesess;
569 sx_assert(&proctree_lock, SX_XLOCKED);
570 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
571 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
574 * Reset any sigio structures pointing to us as a result of
575 * F_SETOWN with our pgid.
577 funsetownlst(&pgrp->pg_sigiolst);
580 tp = pgrp->pg_session->s_ttyp;
581 LIST_REMOVE(pgrp, pg_hash);
582 savesess = pgrp->pg_session;
585 /* Remove the reference to the pgrp before deallocating it. */
588 tty_rel_pgrp(tp, pgrp);
591 mtx_destroy(&pgrp->pg_mtx);
593 sess_release(savesess);
597 pgadjustjobc(pgrp, entering)
607 if (pgrp->pg_jobc == 0)
614 * Adjust pgrp jobc counters when specified process changes process group.
615 * We count the number of processes in each process group that "qualify"
616 * the group for terminal job control (those with a parent in a different
617 * process group of the same session). If that count reaches zero, the
618 * process group becomes orphaned. Check both the specified process'
619 * process group and that of its children.
620 * entering == 0 => p is leaving specified group.
621 * entering == 1 => p is entering specified group.
624 fixjobc(p, pgrp, entering)
625 register struct proc *p;
626 register struct pgrp *pgrp;
629 register struct pgrp *hispgrp;
630 register struct session *mysession;
632 sx_assert(&proctree_lock, SX_LOCKED);
633 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
634 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
635 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
638 * Check p's parent to see whether p qualifies its own process
639 * group; if so, adjust count for p's process group.
641 mysession = pgrp->pg_session;
642 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
643 hispgrp->pg_session == mysession)
644 pgadjustjobc(pgrp, entering);
647 * Check this process' children to see whether they qualify
648 * their process groups; if so, adjust counts for children's
651 LIST_FOREACH(p, &p->p_children, p_sibling) {
653 if (hispgrp == pgrp ||
654 hispgrp->pg_session != mysession)
657 if (p->p_state == PRS_ZOMBIE) {
662 pgadjustjobc(hispgrp, entering);
667 * A process group has become orphaned;
668 * if there are any stopped processes in the group,
669 * hang-up all process in that group.
675 register struct proc *p;
677 PGRP_LOCK_ASSERT(pg, MA_OWNED);
679 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
681 if (P_SHOULDSTOP(p)) {
683 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
696 sess_hold(struct session *s)
699 refcount_acquire(&s->s_count);
703 sess_release(struct session *s)
706 if (refcount_release(&s->s_count)) {
707 if (s->s_ttyp != NULL) {
709 tty_rel_sess(s->s_ttyp, s);
711 mtx_destroy(&s->s_mtx);
720 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
722 register struct pgrp *pgrp;
723 register struct proc *p;
726 for (i = 0; i <= pgrphash; i++) {
727 if (!LIST_EMPTY(&pgrphashtbl[i])) {
728 printf("\tindx %d\n", i);
729 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
731 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
732 (void *)pgrp, (long)pgrp->pg_id,
733 (void *)pgrp->pg_session,
734 pgrp->pg_session->s_count,
735 (void *)LIST_FIRST(&pgrp->pg_members));
736 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
737 printf("\t\tpid %ld addr %p pgrp %p\n",
738 (long)p->p_pid, (void *)p,
748 * Calculate the kinfo_proc members which contain process-wide
750 * Must be called with the target process locked.
753 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
757 PROC_LOCK_ASSERT(p, MA_OWNED);
761 FOREACH_THREAD_IN_PROC(p, td) {
763 kp->ki_pctcpu += sched_pctcpu(td);
764 kp->ki_estcpu += td->td_estcpu;
770 * Clear kinfo_proc and fill in any information that is common
771 * to all threads in the process.
772 * Must be called with the target process locked.
775 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
783 PROC_LOCK_ASSERT(p, MA_OWNED);
784 bzero(kp, sizeof(*kp));
786 kp->ki_structsize = sizeof(*kp);
788 kp->ki_addr =/* p->p_addr; */0; /* XXX */
789 kp->ki_args = p->p_args;
790 kp->ki_textvp = p->p_textvp;
792 kp->ki_tracep = p->p_tracevp;
793 kp->ki_traceflag = p->p_traceflag;
796 kp->ki_vmspace = p->p_vmspace;
797 kp->ki_flag = p->p_flag;
800 kp->ki_uid = cred->cr_uid;
801 kp->ki_ruid = cred->cr_ruid;
802 kp->ki_svuid = cred->cr_svuid;
803 kp->ki_cr_flags = cred->cr_flags;
804 /* XXX bde doesn't like KI_NGROUPS */
805 if (cred->cr_ngroups > KI_NGROUPS) {
806 kp->ki_ngroups = KI_NGROUPS;
807 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
809 kp->ki_ngroups = cred->cr_ngroups;
810 bcopy(cred->cr_groups, kp->ki_groups,
811 kp->ki_ngroups * sizeof(gid_t));
812 kp->ki_rgid = cred->cr_rgid;
813 kp->ki_svgid = cred->cr_svgid;
814 /* If jailed(cred), emulate the old P_JAILED flag. */
816 kp->ki_flag |= P_JAILED;
817 /* If inside the jail, use 0 as a jail ID. */
818 if (cred->cr_prison != curthread->td_ucred->cr_prison)
819 kp->ki_jid = cred->cr_prison->pr_id;
824 mtx_lock(&ps->ps_mtx);
825 kp->ki_sigignore = ps->ps_sigignore;
826 kp->ki_sigcatch = ps->ps_sigcatch;
827 mtx_unlock(&ps->ps_mtx);
829 if (p->p_state != PRS_NEW &&
830 p->p_state != PRS_ZOMBIE &&
831 p->p_vmspace != NULL) {
832 struct vmspace *vm = p->p_vmspace;
834 kp->ki_size = vm->vm_map.size;
835 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
836 FOREACH_THREAD_IN_PROC(p, td0) {
837 if (!TD_IS_SWAPPED(td0))
838 kp->ki_rssize += td0->td_kstack_pages;
840 kp->ki_swrss = vm->vm_swrss;
841 kp->ki_tsize = vm->vm_tsize;
842 kp->ki_dsize = vm->vm_dsize;
843 kp->ki_ssize = vm->vm_ssize;
844 } else if (p->p_state == PRS_ZOMBIE)
846 if (kp->ki_flag & P_INMEM)
847 kp->ki_sflag = PS_INMEM;
850 /* Calculate legacy swtime as seconds since 'swtick'. */
851 kp->ki_swtime = (ticks - p->p_swtick) / hz;
852 kp->ki_pid = p->p_pid;
853 kp->ki_nice = p->p_nice;
855 rufetch(p, &kp->ki_rusage);
856 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
858 if ((p->p_flag & P_INMEM) && p->p_stats != NULL) {
859 kp->ki_start = p->p_stats->p_start;
860 timevaladd(&kp->ki_start, &boottime);
862 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
864 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
866 /* Some callers want child-times in a single value */
867 kp->ki_childtime = kp->ki_childstime;
868 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
871 FOREACH_THREAD_IN_PROC(p, td0)
872 kp->ki_cow += td0->td_cow;
876 kp->ki_pgid = p->p_pgrp->pg_id;
877 kp->ki_jobc = p->p_pgrp->pg_jobc;
878 sp = p->p_pgrp->pg_session;
881 kp->ki_sid = sp->s_sid;
883 strlcpy(kp->ki_login, sp->s_login,
884 sizeof(kp->ki_login));
886 kp->ki_kiflag |= KI_CTTY;
888 kp->ki_kiflag |= KI_SLEADER;
889 /* XXX proctree_lock */
894 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
895 kp->ki_tdev = tty_udev(tp);
896 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
898 kp->ki_tsid = tp->t_session->s_sid;
901 if (p->p_comm[0] != '\0')
902 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
903 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
904 p->p_sysent->sv_name[0] != '\0')
905 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
906 kp->ki_siglist = p->p_siglist;
907 kp->ki_xstat = p->p_xstat;
908 kp->ki_acflag = p->p_acflag;
909 kp->ki_lock = p->p_lock;
911 kp->ki_ppid = p->p_pptr->p_pid;
915 * Fill in information that is thread specific. Must be called with
916 * target process locked. If 'preferthread' is set, overwrite certain
917 * process-related fields that are maintained for both threads and
921 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
927 PROC_LOCK_ASSERT(p, MA_OWNED);
930 if (td->td_wmesg != NULL)
931 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
933 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
934 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
935 if (TD_ON_LOCK(td)) {
936 kp->ki_kiflag |= KI_LOCKBLOCK;
937 strlcpy(kp->ki_lockname, td->td_lockname,
938 sizeof(kp->ki_lockname));
940 kp->ki_kiflag &= ~KI_LOCKBLOCK;
941 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
944 if (p->p_state == PRS_NORMAL) { /* approximate. */
945 if (TD_ON_RUNQ(td) ||
949 } else if (P_SHOULDSTOP(p)) {
951 } else if (TD_IS_SLEEPING(td)) {
952 kp->ki_stat = SSLEEP;
953 } else if (TD_ON_LOCK(td)) {
958 } else if (p->p_state == PRS_ZOMBIE) {
964 /* Things in the thread */
965 kp->ki_wchan = td->td_wchan;
966 kp->ki_pri.pri_level = td->td_priority;
967 kp->ki_pri.pri_native = td->td_base_pri;
968 kp->ki_lastcpu = td->td_lastcpu;
969 kp->ki_oncpu = td->td_oncpu;
970 kp->ki_tdflags = td->td_flags;
971 kp->ki_tid = td->td_tid;
972 kp->ki_numthreads = p->p_numthreads;
973 kp->ki_pcb = td->td_pcb;
974 kp->ki_kstack = (void *)td->td_kstack;
975 kp->ki_slptime = (ticks - td->td_slptick) / hz;
976 kp->ki_pri.pri_class = td->td_pri_class;
977 kp->ki_pri.pri_user = td->td_user_pri;
980 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
981 kp->ki_pctcpu = sched_pctcpu(td);
982 kp->ki_estcpu = td->td_estcpu;
983 kp->ki_cow = td->td_cow;
986 /* We can't get this anymore but ps etc never used it anyway. */
990 kp->ki_siglist = td->td_siglist;
991 kp->ki_sigmask = td->td_sigmask;
996 * Fill in a kinfo_proc structure for the specified process.
997 * Must be called with the target process locked.
1000 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1003 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1005 fill_kinfo_proc_only(p, kp);
1006 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1007 fill_kinfo_aggregate(p, kp);
1014 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1018 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1021 pstats_fork(struct pstats *src, struct pstats *dst)
1024 bzero(&dst->pstat_startzero,
1025 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1026 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1027 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1031 pstats_free(struct pstats *ps)
1034 free(ps, M_SUBPROC);
1038 * Locate a zombie process by number
1045 sx_slock(&allproc_lock);
1046 LIST_FOREACH(p, &zombproc, p_list)
1047 if (p->p_pid == pid) {
1051 sx_sunlock(&allproc_lock);
1055 #define KERN_PROC_ZOMBMASK 0x3
1056 #define KERN_PROC_NOTHREADS 0x4
1058 #ifdef COMPAT_FREEBSD32
1061 * This function is typically used to copy out the kernel address, so
1062 * it can be replaced by assignment of zero.
1064 static inline uint32_t
1065 ptr32_trim(void *ptr)
1069 uptr = (uintptr_t)ptr;
1070 return ((uptr > UINT_MAX) ? 0 : uptr);
1073 #define PTRTRIM_CP(src,dst,fld) \
1074 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1077 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1081 bzero(ki32, sizeof(struct kinfo_proc32));
1082 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1083 CP(*ki, *ki32, ki_layout);
1084 PTRTRIM_CP(*ki, *ki32, ki_args);
1085 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1086 PTRTRIM_CP(*ki, *ki32, ki_addr);
1087 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1088 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1089 PTRTRIM_CP(*ki, *ki32, ki_fd);
1090 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1091 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1092 CP(*ki, *ki32, ki_pid);
1093 CP(*ki, *ki32, ki_ppid);
1094 CP(*ki, *ki32, ki_pgid);
1095 CP(*ki, *ki32, ki_tpgid);
1096 CP(*ki, *ki32, ki_sid);
1097 CP(*ki, *ki32, ki_tsid);
1098 CP(*ki, *ki32, ki_jobc);
1099 CP(*ki, *ki32, ki_tdev);
1100 CP(*ki, *ki32, ki_siglist);
1101 CP(*ki, *ki32, ki_sigmask);
1102 CP(*ki, *ki32, ki_sigignore);
1103 CP(*ki, *ki32, ki_sigcatch);
1104 CP(*ki, *ki32, ki_uid);
1105 CP(*ki, *ki32, ki_ruid);
1106 CP(*ki, *ki32, ki_svuid);
1107 CP(*ki, *ki32, ki_rgid);
1108 CP(*ki, *ki32, ki_svgid);
1109 CP(*ki, *ki32, ki_ngroups);
1110 for (i = 0; i < KI_NGROUPS; i++)
1111 CP(*ki, *ki32, ki_groups[i]);
1112 CP(*ki, *ki32, ki_size);
1113 CP(*ki, *ki32, ki_rssize);
1114 CP(*ki, *ki32, ki_swrss);
1115 CP(*ki, *ki32, ki_tsize);
1116 CP(*ki, *ki32, ki_dsize);
1117 CP(*ki, *ki32, ki_ssize);
1118 CP(*ki, *ki32, ki_xstat);
1119 CP(*ki, *ki32, ki_acflag);
1120 CP(*ki, *ki32, ki_pctcpu);
1121 CP(*ki, *ki32, ki_estcpu);
1122 CP(*ki, *ki32, ki_slptime);
1123 CP(*ki, *ki32, ki_swtime);
1124 CP(*ki, *ki32, ki_cow);
1125 CP(*ki, *ki32, ki_runtime);
1126 TV_CP(*ki, *ki32, ki_start);
1127 TV_CP(*ki, *ki32, ki_childtime);
1128 CP(*ki, *ki32, ki_flag);
1129 CP(*ki, *ki32, ki_kiflag);
1130 CP(*ki, *ki32, ki_traceflag);
1131 CP(*ki, *ki32, ki_stat);
1132 CP(*ki, *ki32, ki_nice);
1133 CP(*ki, *ki32, ki_lock);
1134 CP(*ki, *ki32, ki_rqindex);
1135 CP(*ki, *ki32, ki_oncpu);
1136 CP(*ki, *ki32, ki_lastcpu);
1137 bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1);
1138 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1139 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1140 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1141 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1142 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1143 CP(*ki, *ki32, ki_cr_flags);
1144 CP(*ki, *ki32, ki_jid);
1145 CP(*ki, *ki32, ki_numthreads);
1146 CP(*ki, *ki32, ki_tid);
1147 CP(*ki, *ki32, ki_pri);
1148 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1149 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1150 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1151 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1152 PTRTRIM_CP(*ki, *ki32, ki_udata);
1153 CP(*ki, *ki32, ki_sflag);
1154 CP(*ki, *ki32, ki_tdflags);
1158 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1160 struct kinfo_proc32 ki32;
1163 if (req->flags & SCTL_MASK32) {
1164 freebsd32_kinfo_proc_out(ki, &ki32);
1165 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
1167 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
1172 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1175 return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
1180 * Must be called with the process locked and will return with it unlocked.
1183 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1186 struct kinfo_proc kinfo_proc;
1189 pid_t pid = p->p_pid;
1191 PROC_LOCK_ASSERT(p, MA_OWNED);
1192 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1194 fill_kinfo_proc(p, &kinfo_proc);
1195 if (flags & KERN_PROC_NOTHREADS)
1196 error = sysctl_out_proc_copyout(&kinfo_proc, req);
1198 FOREACH_THREAD_IN_PROC(p, td) {
1199 fill_kinfo_thread(td, &kinfo_proc, 1);
1200 error = sysctl_out_proc_copyout(&kinfo_proc, req);
1208 if (flags & KERN_PROC_ZOMBMASK)
1226 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1228 int *name = (int*) arg1;
1229 u_int namelen = arg2;
1231 int flags, doingzomb, oid_number;
1234 oid_number = oidp->oid_number;
1235 if (oid_number != KERN_PROC_ALL &&
1236 (oid_number & KERN_PROC_INC_THREAD) == 0)
1237 flags = KERN_PROC_NOTHREADS;
1240 oid_number &= ~KERN_PROC_INC_THREAD;
1242 if (oid_number == KERN_PROC_PID) {
1245 error = sysctl_wire_old_buffer(req, 0);
1248 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1251 error = sysctl_out_proc(p, req, flags);
1255 switch (oid_number) {
1260 case KERN_PROC_PROC:
1261 if (namelen != 0 && namelen != 1)
1271 /* overestimate by 5 procs */
1272 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1276 error = sysctl_wire_old_buffer(req, 0);
1279 sx_slock(&allproc_lock);
1280 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1282 p = LIST_FIRST(&allproc);
1284 p = LIST_FIRST(&zombproc);
1285 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1287 * Skip embryonic processes.
1290 if (p->p_state == PRS_NEW) {
1294 KASSERT(p->p_ucred != NULL,
1295 ("process credential is NULL for non-NEW proc"));
1297 * Show a user only appropriate processes.
1299 if (p_cansee(curthread, p)) {
1304 * TODO - make more efficient (see notes below).
1307 switch (oid_number) {
1310 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1316 case KERN_PROC_PGRP:
1317 /* could do this by traversing pgrp */
1318 if (p->p_pgrp == NULL ||
1319 p->p_pgrp->pg_id != (pid_t)name[0]) {
1325 case KERN_PROC_RGID:
1326 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1332 case KERN_PROC_SESSION:
1333 if (p->p_session == NULL ||
1334 p->p_session->s_sid != (pid_t)name[0]) {
1341 if ((p->p_flag & P_CONTROLT) == 0 ||
1342 p->p_session == NULL) {
1346 /* XXX proctree_lock */
1347 SESS_LOCK(p->p_session);
1348 if (p->p_session->s_ttyp == NULL ||
1349 tty_udev(p->p_session->s_ttyp) !=
1351 SESS_UNLOCK(p->p_session);
1355 SESS_UNLOCK(p->p_session);
1359 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1365 case KERN_PROC_RUID:
1366 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1372 case KERN_PROC_PROC:
1380 error = sysctl_out_proc(p, req, flags | doingzomb);
1382 sx_sunlock(&allproc_lock);
1387 sx_sunlock(&allproc_lock);
1392 pargs_alloc(int len)
1396 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1398 refcount_init(&pa->ar_ref, 1);
1399 pa->ar_length = len;
1404 pargs_free(struct pargs *pa)
1411 pargs_hold(struct pargs *pa)
1416 refcount_acquire(&pa->ar_ref);
1420 pargs_drop(struct pargs *pa)
1425 if (refcount_release(&pa->ar_ref))
1430 * This sysctl allows a process to retrieve the argument list or process
1431 * title for another process without groping around in the address space
1432 * of the other process. It also allow a process to set its own "process
1433 * title to a string of its own choice.
1436 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1438 int *name = (int*) arg1;
1439 u_int namelen = arg2;
1440 struct pargs *newpa, *pa;
1442 int flags, error = 0;
1447 flags = PGET_CANSEE;
1448 if (req->newptr != NULL)
1449 flags |= PGET_ISCURRENT;
1450 error = pget((pid_t)name[0], flags, &p);
1458 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1460 if (error != 0 || req->newptr == NULL)
1463 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1465 newpa = pargs_alloc(req->newlen);
1466 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1480 * This sysctl allows a process to retrieve the path of the executable for
1481 * itself or another process.
1484 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1486 pid_t *pidp = (pid_t *)arg1;
1487 unsigned int arglen = arg2;
1490 char *retbuf, *freebuf;
1491 int error, vfslocked;
1495 if (*pidp == -1) { /* -1 means this process */
1496 p = req->td->td_proc;
1498 error = pget(*pidp, PGET_CANSEE, &p);
1512 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1513 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1515 VFS_UNLOCK_GIANT(vfslocked);
1518 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1519 free(freebuf, M_TEMP);
1524 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1537 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1540 sv_name = p->p_sysent->sv_name;
1542 return (sysctl_handle_string(oidp, sv_name, 0, req));
1545 #ifdef KINFO_OVMENTRY_SIZE
1546 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1549 #ifdef COMPAT_FREEBSD7
1551 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1553 vm_map_entry_t entry, tmp_entry;
1554 unsigned int last_timestamp;
1555 char *fullpath, *freepath;
1556 struct kinfo_ovmentry *kve;
1566 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1569 vm = vmspace_acquire_ref(p);
1574 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1576 map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */
1577 vm_map_lock_read(map);
1578 for (entry = map->header.next; entry != &map->header;
1579 entry = entry->next) {
1580 vm_object_t obj, tobj, lobj;
1584 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1587 bzero(kve, sizeof(*kve));
1588 kve->kve_structsize = sizeof(*kve);
1590 kve->kve_private_resident = 0;
1591 obj = entry->object.vm_object;
1593 VM_OBJECT_LOCK(obj);
1594 if (obj->shadow_count == 1)
1595 kve->kve_private_resident =
1596 obj->resident_page_count;
1598 kve->kve_resident = 0;
1599 addr = entry->start;
1600 while (addr < entry->end) {
1601 if (pmap_extract(map->pmap, addr))
1602 kve->kve_resident++;
1606 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1608 VM_OBJECT_LOCK(tobj);
1610 VM_OBJECT_UNLOCK(lobj);
1614 kve->kve_start = (void*)entry->start;
1615 kve->kve_end = (void*)entry->end;
1616 kve->kve_offset = (off_t)entry->offset;
1618 if (entry->protection & VM_PROT_READ)
1619 kve->kve_protection |= KVME_PROT_READ;
1620 if (entry->protection & VM_PROT_WRITE)
1621 kve->kve_protection |= KVME_PROT_WRITE;
1622 if (entry->protection & VM_PROT_EXECUTE)
1623 kve->kve_protection |= KVME_PROT_EXEC;
1625 if (entry->eflags & MAP_ENTRY_COW)
1626 kve->kve_flags |= KVME_FLAG_COW;
1627 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1628 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1629 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1630 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1632 last_timestamp = map->timestamp;
1633 vm_map_unlock_read(map);
1635 kve->kve_fileid = 0;
1641 switch (lobj->type) {
1643 kve->kve_type = KVME_TYPE_DEFAULT;
1646 kve->kve_type = KVME_TYPE_VNODE;
1651 kve->kve_type = KVME_TYPE_SWAP;
1654 kve->kve_type = KVME_TYPE_DEVICE;
1657 kve->kve_type = KVME_TYPE_PHYS;
1660 kve->kve_type = KVME_TYPE_DEAD;
1663 kve->kve_type = KVME_TYPE_SG;
1666 kve->kve_type = KVME_TYPE_UNKNOWN;
1670 VM_OBJECT_UNLOCK(lobj);
1672 kve->kve_ref_count = obj->ref_count;
1673 kve->kve_shadow_count = obj->shadow_count;
1674 VM_OBJECT_UNLOCK(obj);
1676 vn_fullpath(curthread, vp, &fullpath,
1678 cred = curthread->td_ucred;
1679 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1680 vn_lock(vp, LK_SHARED | LK_RETRY);
1681 if (VOP_GETATTR(vp, &va, cred) == 0) {
1682 kve->kve_fileid = va.va_fileid;
1683 kve->kve_fsid = va.va_fsid;
1686 VFS_UNLOCK_GIANT(vfslocked);
1689 kve->kve_type = KVME_TYPE_NONE;
1690 kve->kve_ref_count = 0;
1691 kve->kve_shadow_count = 0;
1694 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1695 if (freepath != NULL)
1696 free(freepath, M_TEMP);
1698 error = SYSCTL_OUT(req, kve, sizeof(*kve));
1699 vm_map_lock_read(map);
1702 if (last_timestamp != map->timestamp) {
1703 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1707 vm_map_unlock_read(map);
1713 #endif /* COMPAT_FREEBSD7 */
1715 #ifdef KINFO_VMENTRY_SIZE
1716 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1720 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
1722 vm_map_entry_t entry, tmp_entry;
1723 unsigned int last_timestamp;
1724 char *fullpath, *freepath;
1725 struct kinfo_vmentry *kve;
1735 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1738 vm = vmspace_acquire_ref(p);
1743 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1745 map = &vm->vm_map; /* XXXRW: More locking required? */
1746 vm_map_lock_read(map);
1747 for (entry = map->header.next; entry != &map->header;
1748 entry = entry->next) {
1749 vm_object_t obj, tobj, lobj;
1751 int vfslocked, mincoreinfo;
1753 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1756 bzero(kve, sizeof(*kve));
1758 kve->kve_private_resident = 0;
1759 obj = entry->object.vm_object;
1761 VM_OBJECT_LOCK(obj);
1762 if (obj->shadow_count == 1)
1763 kve->kve_private_resident =
1764 obj->resident_page_count;
1766 kve->kve_resident = 0;
1767 addr = entry->start;
1768 while (addr < entry->end) {
1769 mincoreinfo = pmap_mincore(map->pmap, addr);
1770 if (mincoreinfo & MINCORE_INCORE)
1771 kve->kve_resident++;
1772 if (mincoreinfo & MINCORE_SUPER)
1773 kve->kve_flags |= KVME_FLAG_SUPER;
1777 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1779 VM_OBJECT_LOCK(tobj);
1781 VM_OBJECT_UNLOCK(lobj);
1785 kve->kve_start = entry->start;
1786 kve->kve_end = entry->end;
1787 kve->kve_offset = entry->offset;
1789 if (entry->protection & VM_PROT_READ)
1790 kve->kve_protection |= KVME_PROT_READ;
1791 if (entry->protection & VM_PROT_WRITE)
1792 kve->kve_protection |= KVME_PROT_WRITE;
1793 if (entry->protection & VM_PROT_EXECUTE)
1794 kve->kve_protection |= KVME_PROT_EXEC;
1796 if (entry->eflags & MAP_ENTRY_COW)
1797 kve->kve_flags |= KVME_FLAG_COW;
1798 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1799 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1800 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
1801 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
1803 last_timestamp = map->timestamp;
1804 vm_map_unlock_read(map);
1806 kve->kve_fileid = 0;
1812 switch (lobj->type) {
1814 kve->kve_type = KVME_TYPE_DEFAULT;
1817 kve->kve_type = KVME_TYPE_VNODE;
1822 kve->kve_type = KVME_TYPE_SWAP;
1825 kve->kve_type = KVME_TYPE_DEVICE;
1828 kve->kve_type = KVME_TYPE_PHYS;
1831 kve->kve_type = KVME_TYPE_DEAD;
1834 kve->kve_type = KVME_TYPE_SG;
1837 kve->kve_type = KVME_TYPE_UNKNOWN;
1841 VM_OBJECT_UNLOCK(lobj);
1843 kve->kve_ref_count = obj->ref_count;
1844 kve->kve_shadow_count = obj->shadow_count;
1845 VM_OBJECT_UNLOCK(obj);
1847 vn_fullpath(curthread, vp, &fullpath,
1849 cred = curthread->td_ucred;
1850 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1851 vn_lock(vp, LK_SHARED | LK_RETRY);
1852 if (VOP_GETATTR(vp, &va, cred) == 0) {
1853 kve->kve_fileid = va.va_fileid;
1854 kve->kve_fsid = va.va_fsid;
1857 VFS_UNLOCK_GIANT(vfslocked);
1860 kve->kve_type = KVME_TYPE_NONE;
1861 kve->kve_ref_count = 0;
1862 kve->kve_shadow_count = 0;
1865 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1866 if (freepath != NULL)
1867 free(freepath, M_TEMP);
1869 /* Pack record size down */
1870 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
1871 strlen(kve->kve_path) + 1;
1872 kve->kve_structsize = roundup(kve->kve_structsize,
1874 error = SYSCTL_OUT(req, kve, kve->kve_structsize);
1875 vm_map_lock_read(map);
1878 if (last_timestamp != map->timestamp) {
1879 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1883 vm_map_unlock_read(map);
1890 #if defined(STACK) || defined(DDB)
1892 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
1894 struct kinfo_kstack *kkstp;
1895 int error, i, *name, numthreads;
1896 lwpid_t *lwpidarray;
1903 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
1907 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
1908 st = stack_create();
1914 if (numthreads < p->p_numthreads) {
1915 if (lwpidarray != NULL) {
1916 free(lwpidarray, M_TEMP);
1919 numthreads = p->p_numthreads;
1921 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
1929 * XXXRW: During the below loop, execve(2) and countless other sorts
1930 * of changes could have taken place. Should we check to see if the
1931 * vmspace has been replaced, or the like, in order to prevent
1932 * giving a snapshot that spans, say, execve(2), with some threads
1933 * before and some after? Among other things, the credentials could
1934 * have changed, in which case the right to extract debug info might
1935 * no longer be assured.
1937 FOREACH_THREAD_IN_PROC(p, td) {
1938 KASSERT(i < numthreads,
1939 ("sysctl_kern_proc_kstack: numthreads"));
1940 lwpidarray[i] = td->td_tid;
1944 for (i = 0; i < numthreads; i++) {
1945 td = thread_find(p, lwpidarray[i]);
1949 bzero(kkstp, sizeof(*kkstp));
1950 (void)sbuf_new(&sb, kkstp->kkst_trace,
1951 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
1953 kkstp->kkst_tid = td->td_tid;
1954 if (TD_IS_SWAPPED(td))
1955 kkstp->kkst_state = KKST_STATE_SWAPPED;
1956 else if (TD_IS_RUNNING(td))
1957 kkstp->kkst_state = KKST_STATE_RUNNING;
1959 kkstp->kkst_state = KKST_STATE_STACKOK;
1960 stack_save_td(st, td);
1964 stack_sbuf_print(&sb, st);
1967 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
1974 if (lwpidarray != NULL)
1975 free(lwpidarray, M_TEMP);
1977 free(kkstp, M_TEMP);
1983 * This sysctl allows a process to retrieve the full list of groups from
1984 * itself or another process.
1987 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
1989 pid_t *pidp = (pid_t *)arg1;
1990 unsigned int arglen = arg2;
1997 if (*pidp == -1) { /* -1 means this process */
1998 p = req->td->td_proc;
2000 error = pget(*pidp, PGET_CANSEE, &p);
2005 cred = crhold(p->p_ucred);
2009 error = SYSCTL_OUT(req, cred->cr_groups,
2010 cred->cr_ngroups * sizeof(gid_t));
2016 * This sysctl allows a process to set and retrieve binary osreldate of
2020 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2022 int *name = (int *)arg1;
2023 u_int namelen = arg2;
2025 int flags, error, osrel;
2030 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2033 flags = PGET_HOLD | PGET_NOTWEXIT;
2034 if (req->newptr != NULL)
2035 flags |= PGET_CANDEBUG;
2037 flags |= PGET_CANSEE;
2038 error = pget((pid_t)name[0], flags, &p);
2042 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2046 if (req->newptr != NULL) {
2047 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2061 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2063 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2064 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2065 "Return entire process table");
2067 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2068 sysctl_kern_proc, "Process table");
2070 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2071 sysctl_kern_proc, "Process table");
2073 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2074 sysctl_kern_proc, "Process table");
2076 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2077 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2079 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2080 sysctl_kern_proc, "Process table");
2082 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2083 sysctl_kern_proc, "Process table");
2085 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2086 sysctl_kern_proc, "Process table");
2088 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2089 sysctl_kern_proc, "Process table");
2091 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2092 sysctl_kern_proc, "Return process table, no threads");
2094 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2095 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2096 sysctl_kern_proc_args, "Process argument list");
2098 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2099 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2101 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2102 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2103 "Process syscall vector name (ABI type)");
2105 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2106 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2108 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2109 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2111 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2112 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2114 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2115 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2117 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2118 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2120 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2121 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2123 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2124 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2126 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2127 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2129 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2130 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2131 "Return process table, no threads");
2133 #ifdef COMPAT_FREEBSD7
2134 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2135 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2138 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2139 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2141 #if defined(STACK) || defined(DDB)
2142 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2143 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2146 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2147 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2149 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2150 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2151 "Process binary osreldate");