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>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
51 #include <sys/refcount.h>
53 #include <sys/sysent.h>
54 #include <sys/sched.h>
56 #include <sys/stack.h>
57 #include <sys/sysctl.h>
58 #include <sys/filedesc.h>
60 #include <sys/signalvar.h>
65 #include <sys/vnode.h>
66 #include <sys/eventhandler.h>
69 #include <sys/ktrace.h>
77 #include <vm/vm_extern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_object.h>
83 #ifdef COMPAT_FREEBSD32
84 #include <compat/freebsd32/freebsd32.h>
85 #include <compat/freebsd32/freebsd32_util.h>
88 SDT_PROVIDER_DEFINE(proc);
89 SDT_PROBE_DEFINE(proc, kernel, ctor, entry);
90 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 0, "struct proc *");
91 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 1, "int");
92 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 2, "void *");
93 SDT_PROBE_ARGTYPE(proc, kernel, ctor, entry, 3, "int");
94 SDT_PROBE_DEFINE(proc, kernel, ctor, return);
95 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 0, "struct proc *");
96 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 1, "int");
97 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 2, "void *");
98 SDT_PROBE_ARGTYPE(proc, kernel, ctor, return, 3, "int");
99 SDT_PROBE_DEFINE(proc, kernel, dtor, entry);
100 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 0, "struct proc *");
101 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 1, "int");
102 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 2, "void *");
103 SDT_PROBE_ARGTYPE(proc, kernel, dtor, entry, 3, "struct thread *");
104 SDT_PROBE_DEFINE(proc, kernel, dtor, return);
105 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 0, "struct proc *");
106 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 1, "int");
107 SDT_PROBE_ARGTYPE(proc, kernel, dtor, return, 2, "void *");
108 SDT_PROBE_DEFINE(proc, kernel, init, entry);
109 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 0, "struct proc *");
110 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 1, "int");
111 SDT_PROBE_ARGTYPE(proc, kernel, init, entry, 2, "int");
112 SDT_PROBE_DEFINE(proc, kernel, init, return);
113 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 0, "struct proc *");
114 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 1, "int");
115 SDT_PROBE_ARGTYPE(proc, kernel, init, return, 2, "int");
117 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
118 MALLOC_DEFINE(M_SESSION, "session", "session header");
119 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
120 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
122 static void doenterpgrp(struct proc *, struct pgrp *);
123 static void orphanpg(struct pgrp *pg);
124 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
125 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
126 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
128 static void pgadjustjobc(struct pgrp *pgrp, int entering);
129 static void pgdelete(struct pgrp *);
130 static int proc_ctor(void *mem, int size, void *arg, int flags);
131 static void proc_dtor(void *mem, int size, void *arg);
132 static int proc_init(void *mem, int size, int flags);
133 static void proc_fini(void *mem, int size);
134 static void pargs_free(struct pargs *pa);
137 * Other process lists
139 struct pidhashhead *pidhashtbl;
141 struct pgrphashhead *pgrphashtbl;
143 struct proclist allproc;
144 struct proclist zombproc;
145 struct sx allproc_lock;
146 struct sx proctree_lock;
147 struct mtx ppeers_lock;
148 uma_zone_t proc_zone;
150 int kstack_pages = KSTACK_PAGES;
151 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0, "");
153 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
154 #ifdef COMPAT_FREEBSD32
155 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
159 * Initialize global process hashing structures.
165 sx_init(&allproc_lock, "allproc");
166 sx_init(&proctree_lock, "proctree");
167 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
169 LIST_INIT(&zombproc);
170 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
171 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
172 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
173 proc_ctor, proc_dtor, proc_init, proc_fini,
174 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
179 * Prepare a proc for use.
182 proc_ctor(void *mem, int size, void *arg, int flags)
186 p = (struct proc *)mem;
187 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
188 EVENTHANDLER_INVOKE(process_ctor, p);
189 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
194 * Reclaim a proc after use.
197 proc_dtor(void *mem, int size, void *arg)
202 /* INVARIANTS checks go here */
203 p = (struct proc *)mem;
204 td = FIRST_THREAD_IN_PROC(p);
205 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
208 KASSERT((p->p_numthreads == 1),
209 ("bad number of threads in exiting process"));
210 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
212 /* Free all OSD associated to this thread. */
215 EVENTHANDLER_INVOKE(process_dtor, p);
216 if (p->p_ksi != NULL)
217 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
218 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
222 * Initialize type-stable parts of a proc (when newly created).
225 proc_init(void *mem, int size, int flags)
229 p = (struct proc *)mem;
230 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
231 p->p_sched = (struct p_sched *)&p[1];
232 bzero(&p->p_mtx, sizeof(struct mtx));
233 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
234 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
235 cv_init(&p->p_pwait, "ppwait");
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) {
295 if (p->p_state == PRS_NEW) {
302 sx_sunlock(&allproc_lock);
307 * Locate a process group by number.
308 * The caller must hold proctree_lock.
314 register struct pgrp *pgrp;
316 sx_assert(&proctree_lock, SX_LOCKED);
318 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
319 if (pgrp->pg_id == pgid) {
328 * Create a new process group.
329 * pgid must be equal to the pid of p.
330 * Begin a new session if required.
333 enterpgrp(p, pgid, pgrp, sess)
334 register struct proc *p;
337 struct session *sess;
341 sx_assert(&proctree_lock, SX_XLOCKED);
343 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
344 KASSERT(p->p_pid == pgid,
345 ("enterpgrp: new pgrp and pid != pgid"));
347 pgrp2 = pgfind(pgid);
349 KASSERT(pgrp2 == NULL,
350 ("enterpgrp: pgrp with pgid exists"));
351 KASSERT(!SESS_LEADER(p),
352 ("enterpgrp: session leader attempted setpgrp"));
354 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
360 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
362 p->p_flag &= ~P_CONTROLT;
366 sess->s_sid = p->p_pid;
367 refcount_init(&sess->s_count, 1);
368 sess->s_ttyvp = NULL;
370 bcopy(p->p_session->s_login, sess->s_login,
371 sizeof(sess->s_login));
372 pgrp->pg_session = sess;
373 KASSERT(p == curproc,
374 ("enterpgrp: mksession and p != curproc"));
376 pgrp->pg_session = p->p_session;
377 sess_hold(pgrp->pg_session);
381 LIST_INIT(&pgrp->pg_members);
384 * As we have an exclusive lock of proctree_lock,
385 * this should not deadlock.
387 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
389 SLIST_INIT(&pgrp->pg_sigiolst);
392 doenterpgrp(p, pgrp);
398 * Move p to an existing process group
401 enterthispgrp(p, pgrp)
402 register struct proc *p;
406 sx_assert(&proctree_lock, SX_XLOCKED);
407 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
408 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
409 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
410 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
411 KASSERT(pgrp->pg_session == p->p_session,
412 ("%s: pgrp's session %p, p->p_session %p.\n",
416 KASSERT(pgrp != p->p_pgrp,
417 ("%s: p belongs to pgrp.", __func__));
419 doenterpgrp(p, pgrp);
425 * Move p to a process group
432 struct pgrp *savepgrp;
434 sx_assert(&proctree_lock, SX_XLOCKED);
435 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
436 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
437 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
438 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
440 savepgrp = p->p_pgrp;
443 * Adjust eligibility of affected pgrps to participate in job control.
444 * Increment eligibility counts before decrementing, otherwise we
445 * could reach 0 spuriously during the first call.
448 fixjobc(p, p->p_pgrp, 0);
453 LIST_REMOVE(p, p_pglist);
456 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
457 PGRP_UNLOCK(savepgrp);
459 if (LIST_EMPTY(&savepgrp->pg_members))
464 * remove process from process group
468 register struct proc *p;
470 struct pgrp *savepgrp;
472 sx_assert(&proctree_lock, SX_XLOCKED);
473 savepgrp = p->p_pgrp;
476 LIST_REMOVE(p, p_pglist);
479 PGRP_UNLOCK(savepgrp);
480 if (LIST_EMPTY(&savepgrp->pg_members))
486 * delete a process group
490 register struct pgrp *pgrp;
492 struct session *savesess;
495 sx_assert(&proctree_lock, SX_XLOCKED);
496 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
497 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
500 * Reset any sigio structures pointing to us as a result of
501 * F_SETOWN with our pgid.
503 funsetownlst(&pgrp->pg_sigiolst);
506 tp = pgrp->pg_session->s_ttyp;
507 LIST_REMOVE(pgrp, pg_hash);
508 savesess = pgrp->pg_session;
511 /* Remove the reference to the pgrp before deallocating it. */
514 tty_rel_pgrp(tp, pgrp);
517 mtx_destroy(&pgrp->pg_mtx);
519 sess_release(savesess);
523 pgadjustjobc(pgrp, entering)
533 if (pgrp->pg_jobc == 0)
540 * Adjust pgrp jobc counters when specified process changes process group.
541 * We count the number of processes in each process group that "qualify"
542 * the group for terminal job control (those with a parent in a different
543 * process group of the same session). If that count reaches zero, the
544 * process group becomes orphaned. Check both the specified process'
545 * process group and that of its children.
546 * entering == 0 => p is leaving specified group.
547 * entering == 1 => p is entering specified group.
550 fixjobc(p, pgrp, entering)
551 register struct proc *p;
552 register struct pgrp *pgrp;
555 register struct pgrp *hispgrp;
556 register struct session *mysession;
558 sx_assert(&proctree_lock, SX_LOCKED);
559 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
560 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
561 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
564 * Check p's parent to see whether p qualifies its own process
565 * group; if so, adjust count for p's process group.
567 mysession = pgrp->pg_session;
568 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
569 hispgrp->pg_session == mysession)
570 pgadjustjobc(pgrp, entering);
573 * Check this process' children to see whether they qualify
574 * their process groups; if so, adjust counts for children's
577 LIST_FOREACH(p, &p->p_children, p_sibling) {
579 if (hispgrp == pgrp ||
580 hispgrp->pg_session != mysession)
583 if (p->p_state == PRS_ZOMBIE) {
588 pgadjustjobc(hispgrp, entering);
593 * A process group has become orphaned;
594 * if there are any stopped processes in the group,
595 * hang-up all process in that group.
601 register struct proc *p;
603 PGRP_LOCK_ASSERT(pg, MA_OWNED);
605 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
607 if (P_SHOULDSTOP(p)) {
609 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
622 sess_hold(struct session *s)
625 refcount_acquire(&s->s_count);
629 sess_release(struct session *s)
632 if (refcount_release(&s->s_count)) {
633 if (s->s_ttyp != NULL) {
635 tty_rel_sess(s->s_ttyp, s);
637 mtx_destroy(&s->s_mtx);
646 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
648 register struct pgrp *pgrp;
649 register struct proc *p;
652 for (i = 0; i <= pgrphash; i++) {
653 if (!LIST_EMPTY(&pgrphashtbl[i])) {
654 printf("\tindx %d\n", i);
655 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
657 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
658 (void *)pgrp, (long)pgrp->pg_id,
659 (void *)pgrp->pg_session,
660 pgrp->pg_session->s_count,
661 (void *)LIST_FIRST(&pgrp->pg_members));
662 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
663 printf("\t\tpid %ld addr %p pgrp %p\n",
664 (long)p->p_pid, (void *)p,
674 * Calculate the kinfo_proc members which contain process-wide
676 * Must be called with the target process locked.
679 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
683 PROC_LOCK_ASSERT(p, MA_OWNED);
687 FOREACH_THREAD_IN_PROC(p, td) {
689 kp->ki_pctcpu += sched_pctcpu(td);
690 kp->ki_estcpu += td->td_estcpu;
696 * Clear kinfo_proc and fill in any information that is common
697 * to all threads in the process.
698 * Must be called with the target process locked.
701 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
709 PROC_LOCK_ASSERT(p, MA_OWNED);
710 bzero(kp, sizeof(*kp));
712 kp->ki_structsize = sizeof(*kp);
714 kp->ki_addr =/* p->p_addr; */0; /* XXX */
715 kp->ki_args = p->p_args;
716 kp->ki_textvp = p->p_textvp;
718 kp->ki_tracep = p->p_tracevp;
719 mtx_lock(&ktrace_mtx);
720 kp->ki_traceflag = p->p_traceflag;
721 mtx_unlock(&ktrace_mtx);
724 kp->ki_vmspace = p->p_vmspace;
725 kp->ki_flag = p->p_flag;
728 kp->ki_uid = cred->cr_uid;
729 kp->ki_ruid = cred->cr_ruid;
730 kp->ki_svuid = cred->cr_svuid;
731 kp->ki_cr_flags = cred->cr_flags;
732 /* XXX bde doesn't like KI_NGROUPS */
733 if (cred->cr_ngroups > KI_NGROUPS) {
734 kp->ki_ngroups = KI_NGROUPS;
735 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
737 kp->ki_ngroups = cred->cr_ngroups;
738 bcopy(cred->cr_groups, kp->ki_groups,
739 kp->ki_ngroups * sizeof(gid_t));
740 kp->ki_rgid = cred->cr_rgid;
741 kp->ki_svgid = cred->cr_svgid;
742 /* If jailed(cred), emulate the old P_JAILED flag. */
744 kp->ki_flag |= P_JAILED;
745 /* If inside the jail, use 0 as a jail ID. */
746 if (cred->cr_prison != curthread->td_ucred->cr_prison)
747 kp->ki_jid = cred->cr_prison->pr_id;
752 mtx_lock(&ps->ps_mtx);
753 kp->ki_sigignore = ps->ps_sigignore;
754 kp->ki_sigcatch = ps->ps_sigcatch;
755 mtx_unlock(&ps->ps_mtx);
758 if (p->p_state != PRS_NEW &&
759 p->p_state != PRS_ZOMBIE &&
760 p->p_vmspace != NULL) {
761 struct vmspace *vm = p->p_vmspace;
763 kp->ki_size = vm->vm_map.size;
764 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
765 FOREACH_THREAD_IN_PROC(p, td0) {
766 if (!TD_IS_SWAPPED(td0))
767 kp->ki_rssize += td0->td_kstack_pages;
769 kp->ki_swrss = vm->vm_swrss;
770 kp->ki_tsize = vm->vm_tsize;
771 kp->ki_dsize = vm->vm_dsize;
772 kp->ki_ssize = vm->vm_ssize;
773 } else if (p->p_state == PRS_ZOMBIE)
775 if (kp->ki_flag & P_INMEM)
776 kp->ki_sflag = PS_INMEM;
779 /* Calculate legacy swtime as seconds since 'swtick'. */
780 kp->ki_swtime = (ticks - p->p_swtick) / hz;
781 kp->ki_pid = p->p_pid;
782 kp->ki_nice = p->p_nice;
783 rufetch(p, &kp->ki_rusage);
784 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
786 if ((p->p_flag & P_INMEM) && p->p_stats != NULL) {
787 kp->ki_start = p->p_stats->p_start;
788 timevaladd(&kp->ki_start, &boottime);
790 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
792 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
794 /* Some callers want child-times in a single value */
795 kp->ki_childtime = kp->ki_childstime;
796 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
800 kp->ki_pgid = p->p_pgrp->pg_id;
801 kp->ki_jobc = p->p_pgrp->pg_jobc;
802 sp = p->p_pgrp->pg_session;
805 kp->ki_sid = sp->s_sid;
807 strlcpy(kp->ki_login, sp->s_login,
808 sizeof(kp->ki_login));
810 kp->ki_kiflag |= KI_CTTY;
812 kp->ki_kiflag |= KI_SLEADER;
813 /* XXX proctree_lock */
818 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
819 kp->ki_tdev = tty_udev(tp);
820 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
822 kp->ki_tsid = tp->t_session->s_sid;
825 if (p->p_comm[0] != '\0')
826 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
827 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
828 p->p_sysent->sv_name[0] != '\0')
829 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
830 kp->ki_siglist = p->p_siglist;
831 kp->ki_xstat = p->p_xstat;
832 kp->ki_acflag = p->p_acflag;
833 kp->ki_lock = p->p_lock;
835 kp->ki_ppid = p->p_pptr->p_pid;
839 * Fill in information that is thread specific. Must be called with
840 * target process locked. If 'preferthread' is set, overwrite certain
841 * process-related fields that are maintained for both threads and
845 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
850 PROC_LOCK_ASSERT(p, MA_OWNED);
853 if (td->td_wmesg != NULL)
854 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
856 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
857 strlcpy(kp->ki_ocomm, td->td_name, sizeof(kp->ki_ocomm));
858 if (TD_ON_LOCK(td)) {
859 kp->ki_kiflag |= KI_LOCKBLOCK;
860 strlcpy(kp->ki_lockname, td->td_lockname,
861 sizeof(kp->ki_lockname));
863 kp->ki_kiflag &= ~KI_LOCKBLOCK;
864 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
867 if (p->p_state == PRS_NORMAL) { /* approximate. */
868 if (TD_ON_RUNQ(td) ||
872 } else if (P_SHOULDSTOP(p)) {
874 } else if (TD_IS_SLEEPING(td)) {
875 kp->ki_stat = SSLEEP;
876 } else if (TD_ON_LOCK(td)) {
881 } else if (p->p_state == PRS_ZOMBIE) {
887 /* Things in the thread */
888 kp->ki_wchan = td->td_wchan;
889 kp->ki_pri.pri_level = td->td_priority;
890 kp->ki_pri.pri_native = td->td_base_pri;
891 kp->ki_lastcpu = td->td_lastcpu;
892 kp->ki_oncpu = td->td_oncpu;
893 kp->ki_tdflags = td->td_flags;
894 kp->ki_tid = td->td_tid;
895 kp->ki_numthreads = p->p_numthreads;
896 kp->ki_pcb = td->td_pcb;
897 kp->ki_kstack = (void *)td->td_kstack;
898 kp->ki_slptime = (ticks - td->td_slptick) / hz;
899 kp->ki_pri.pri_class = td->td_pri_class;
900 kp->ki_pri.pri_user = td->td_user_pri;
903 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
904 kp->ki_pctcpu = sched_pctcpu(td);
905 kp->ki_estcpu = td->td_estcpu;
908 /* We can't get this anymore but ps etc never used it anyway. */
912 kp->ki_siglist = td->td_siglist;
913 kp->ki_sigmask = td->td_sigmask;
918 * Fill in a kinfo_proc structure for the specified process.
919 * Must be called with the target process locked.
922 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
925 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
927 fill_kinfo_proc_only(p, kp);
928 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
929 fill_kinfo_aggregate(p, kp);
936 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
940 * Copy parts of p_stats; zero the rest of p_stats (statistics).
943 pstats_fork(struct pstats *src, struct pstats *dst)
946 bzero(&dst->pstat_startzero,
947 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
948 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
949 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
953 pstats_free(struct pstats *ps)
960 * Locate a zombie process by number
967 sx_slock(&allproc_lock);
968 LIST_FOREACH(p, &zombproc, p_list)
969 if (p->p_pid == pid) {
973 sx_sunlock(&allproc_lock);
977 #define KERN_PROC_ZOMBMASK 0x3
978 #define KERN_PROC_NOTHREADS 0x4
980 #ifdef COMPAT_FREEBSD32
983 * This function is typically used to copy out the kernel address, so
984 * it can be replaced by assignment of zero.
986 static inline uint32_t
987 ptr32_trim(void *ptr)
991 uptr = (uintptr_t)ptr;
992 return ((uptr > UINT_MAX) ? 0 : uptr);
995 #define PTRTRIM_CP(src,dst,fld) \
996 do { (dst).fld = ptr32_trim((src).fld); } while (0)
999 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1003 bzero(ki32, sizeof(struct kinfo_proc32));
1004 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1005 CP(*ki, *ki32, ki_layout);
1006 PTRTRIM_CP(*ki, *ki32, ki_args);
1007 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1008 PTRTRIM_CP(*ki, *ki32, ki_addr);
1009 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1010 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1011 PTRTRIM_CP(*ki, *ki32, ki_fd);
1012 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1013 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1014 CP(*ki, *ki32, ki_pid);
1015 CP(*ki, *ki32, ki_ppid);
1016 CP(*ki, *ki32, ki_pgid);
1017 CP(*ki, *ki32, ki_tpgid);
1018 CP(*ki, *ki32, ki_sid);
1019 CP(*ki, *ki32, ki_tsid);
1020 CP(*ki, *ki32, ki_jobc);
1021 CP(*ki, *ki32, ki_tdev);
1022 CP(*ki, *ki32, ki_siglist);
1023 CP(*ki, *ki32, ki_sigmask);
1024 CP(*ki, *ki32, ki_sigignore);
1025 CP(*ki, *ki32, ki_sigcatch);
1026 CP(*ki, *ki32, ki_uid);
1027 CP(*ki, *ki32, ki_ruid);
1028 CP(*ki, *ki32, ki_svuid);
1029 CP(*ki, *ki32, ki_rgid);
1030 CP(*ki, *ki32, ki_svgid);
1031 CP(*ki, *ki32, ki_ngroups);
1032 for (i = 0; i < KI_NGROUPS; i++)
1033 CP(*ki, *ki32, ki_groups[i]);
1034 CP(*ki, *ki32, ki_size);
1035 CP(*ki, *ki32, ki_rssize);
1036 CP(*ki, *ki32, ki_swrss);
1037 CP(*ki, *ki32, ki_tsize);
1038 CP(*ki, *ki32, ki_dsize);
1039 CP(*ki, *ki32, ki_ssize);
1040 CP(*ki, *ki32, ki_xstat);
1041 CP(*ki, *ki32, ki_acflag);
1042 CP(*ki, *ki32, ki_pctcpu);
1043 CP(*ki, *ki32, ki_estcpu);
1044 CP(*ki, *ki32, ki_slptime);
1045 CP(*ki, *ki32, ki_swtime);
1046 CP(*ki, *ki32, ki_runtime);
1047 TV_CP(*ki, *ki32, ki_start);
1048 TV_CP(*ki, *ki32, ki_childtime);
1049 CP(*ki, *ki32, ki_flag);
1050 CP(*ki, *ki32, ki_kiflag);
1051 CP(*ki, *ki32, ki_traceflag);
1052 CP(*ki, *ki32, ki_stat);
1053 CP(*ki, *ki32, ki_nice);
1054 CP(*ki, *ki32, ki_lock);
1055 CP(*ki, *ki32, ki_rqindex);
1056 CP(*ki, *ki32, ki_oncpu);
1057 CP(*ki, *ki32, ki_lastcpu);
1058 bcopy(ki->ki_ocomm, ki32->ki_ocomm, OCOMMLEN + 1);
1059 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1060 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1061 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1062 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1063 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1064 CP(*ki, *ki32, ki_cr_flags);
1065 CP(*ki, *ki32, ki_jid);
1066 CP(*ki, *ki32, ki_numthreads);
1067 CP(*ki, *ki32, ki_tid);
1068 CP(*ki, *ki32, ki_pri);
1069 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1070 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1071 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1072 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1073 PTRTRIM_CP(*ki, *ki32, ki_udata);
1074 CP(*ki, *ki32, ki_sflag);
1075 CP(*ki, *ki32, ki_tdflags);
1079 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1081 struct kinfo_proc32 ki32;
1084 if (req->flags & SCTL_MASK32) {
1085 freebsd32_kinfo_proc_out(ki, &ki32);
1086 error = SYSCTL_OUT(req, &ki32, sizeof(struct kinfo_proc32));
1088 error = SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc));
1093 sysctl_out_proc_copyout(struct kinfo_proc *ki, struct sysctl_req *req)
1096 return (SYSCTL_OUT(req, ki, sizeof(struct kinfo_proc)));
1101 * Must be called with the process locked and will return with it unlocked.
1104 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1107 struct kinfo_proc kinfo_proc;
1110 pid_t pid = p->p_pid;
1112 PROC_LOCK_ASSERT(p, MA_OWNED);
1113 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1115 fill_kinfo_proc(p, &kinfo_proc);
1116 if (flags & KERN_PROC_NOTHREADS)
1117 error = sysctl_out_proc_copyout(&kinfo_proc, req);
1119 FOREACH_THREAD_IN_PROC(p, td) {
1120 fill_kinfo_thread(td, &kinfo_proc, 1);
1121 error = sysctl_out_proc_copyout(&kinfo_proc, req);
1129 if (flags & KERN_PROC_ZOMBMASK)
1147 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1149 int *name = (int*) arg1;
1150 u_int namelen = arg2;
1152 int flags, doingzomb, oid_number;
1155 oid_number = oidp->oid_number;
1156 if (oid_number != KERN_PROC_ALL &&
1157 (oid_number & KERN_PROC_INC_THREAD) == 0)
1158 flags = KERN_PROC_NOTHREADS;
1161 oid_number &= ~KERN_PROC_INC_THREAD;
1163 if (oid_number == KERN_PROC_PID) {
1166 error = sysctl_wire_old_buffer(req, 0);
1169 p = pfind((pid_t)name[0]);
1172 if ((error = p_cansee(curthread, p))) {
1176 error = sysctl_out_proc(p, req, flags);
1180 switch (oid_number) {
1185 case KERN_PROC_PROC:
1186 if (namelen != 0 && namelen != 1)
1196 /* overestimate by 5 procs */
1197 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1201 error = sysctl_wire_old_buffer(req, 0);
1204 sx_slock(&allproc_lock);
1205 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1207 p = LIST_FIRST(&allproc);
1209 p = LIST_FIRST(&zombproc);
1210 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1212 * Skip embryonic processes.
1215 if (p->p_state == PRS_NEW) {
1221 KASSERT(p->p_ucred != NULL,
1222 ("process credential is NULL for non-NEW proc"));
1224 * Show a user only appropriate processes.
1226 if (p_cansee(curthread, p)) {
1231 * TODO - make more efficient (see notes below).
1234 switch (oid_number) {
1237 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1243 case KERN_PROC_PGRP:
1244 /* could do this by traversing pgrp */
1245 if (p->p_pgrp == NULL ||
1246 p->p_pgrp->pg_id != (pid_t)name[0]) {
1252 case KERN_PROC_RGID:
1253 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1259 case KERN_PROC_SESSION:
1260 if (p->p_session == NULL ||
1261 p->p_session->s_sid != (pid_t)name[0]) {
1268 if ((p->p_flag & P_CONTROLT) == 0 ||
1269 p->p_session == NULL) {
1273 /* XXX proctree_lock */
1274 SESS_LOCK(p->p_session);
1275 if (p->p_session->s_ttyp == NULL ||
1276 tty_udev(p->p_session->s_ttyp) !=
1278 SESS_UNLOCK(p->p_session);
1282 SESS_UNLOCK(p->p_session);
1286 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1292 case KERN_PROC_RUID:
1293 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1299 case KERN_PROC_PROC:
1307 error = sysctl_out_proc(p, req, flags | doingzomb);
1309 sx_sunlock(&allproc_lock);
1314 sx_sunlock(&allproc_lock);
1319 pargs_alloc(int len)
1323 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1325 refcount_init(&pa->ar_ref, 1);
1326 pa->ar_length = len;
1331 pargs_free(struct pargs *pa)
1338 pargs_hold(struct pargs *pa)
1343 refcount_acquire(&pa->ar_ref);
1347 pargs_drop(struct pargs *pa)
1352 if (refcount_release(&pa->ar_ref))
1357 * This sysctl allows a process to retrieve the argument list or process
1358 * title for another process without groping around in the address space
1359 * of the other process. It also allow a process to set its own "process
1360 * title to a string of its own choice.
1363 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1365 int *name = (int*) arg1;
1366 u_int namelen = arg2;
1367 struct pargs *newpa, *pa;
1374 p = pfind((pid_t)name[0]);
1378 if ((error = p_cansee(curthread, p)) != 0) {
1383 if (req->newptr && curproc != p) {
1391 if (req->oldptr != NULL && pa != NULL)
1392 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1394 if (error != 0 || req->newptr == NULL)
1397 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1399 newpa = pargs_alloc(req->newlen);
1400 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1414 * This sysctl allows a process to retrieve the path of the executable for
1415 * itself or another process.
1418 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1420 pid_t *pidp = (pid_t *)arg1;
1421 unsigned int arglen = arg2;
1424 char *retbuf, *freebuf;
1425 int error, vfslocked;
1429 if (*pidp == -1) { /* -1 means this process */
1430 p = req->td->td_proc;
1435 if ((error = p_cansee(curthread, p)) != 0) {
1450 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1451 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1453 VFS_UNLOCK_GIANT(vfslocked);
1456 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1457 free(freebuf, M_TEMP);
1462 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1475 if ((p = pfind((pid_t)name[0])) == NULL)
1477 if ((error = p_cansee(curthread, p))) {
1481 sv_name = p->p_sysent->sv_name;
1483 return (sysctl_handle_string(oidp, sv_name, 0, req));
1486 #ifdef KINFO_OVMENTRY_SIZE
1487 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
1490 #ifdef COMPAT_FREEBSD7
1492 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
1494 vm_map_entry_t entry, tmp_entry;
1495 unsigned int last_timestamp;
1496 char *fullpath, *freepath;
1497 struct kinfo_ovmentry *kve;
1507 if ((p = pfind((pid_t)name[0])) == NULL)
1509 if (p->p_flag & P_WEXIT) {
1513 if ((error = p_candebug(curthread, p))) {
1519 vm = vmspace_acquire_ref(p);
1524 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1526 map = &p->p_vmspace->vm_map; /* XXXRW: More locking required? */
1527 vm_map_lock_read(map);
1528 for (entry = map->header.next; entry != &map->header;
1529 entry = entry->next) {
1530 vm_object_t obj, tobj, lobj;
1534 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1537 bzero(kve, sizeof(*kve));
1538 kve->kve_structsize = sizeof(*kve);
1540 kve->kve_private_resident = 0;
1541 obj = entry->object.vm_object;
1543 VM_OBJECT_LOCK(obj);
1544 if (obj->shadow_count == 1)
1545 kve->kve_private_resident =
1546 obj->resident_page_count;
1548 kve->kve_resident = 0;
1549 addr = entry->start;
1550 while (addr < entry->end) {
1551 if (pmap_extract(map->pmap, addr))
1552 kve->kve_resident++;
1556 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1558 VM_OBJECT_LOCK(tobj);
1560 VM_OBJECT_UNLOCK(lobj);
1564 kve->kve_start = (void*)entry->start;
1565 kve->kve_end = (void*)entry->end;
1566 kve->kve_offset = (off_t)entry->offset;
1568 if (entry->protection & VM_PROT_READ)
1569 kve->kve_protection |= KVME_PROT_READ;
1570 if (entry->protection & VM_PROT_WRITE)
1571 kve->kve_protection |= KVME_PROT_WRITE;
1572 if (entry->protection & VM_PROT_EXECUTE)
1573 kve->kve_protection |= KVME_PROT_EXEC;
1575 if (entry->eflags & MAP_ENTRY_COW)
1576 kve->kve_flags |= KVME_FLAG_COW;
1577 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1578 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1580 last_timestamp = map->timestamp;
1581 vm_map_unlock_read(map);
1583 kve->kve_fileid = 0;
1589 switch (lobj->type) {
1591 kve->kve_type = KVME_TYPE_DEFAULT;
1594 kve->kve_type = KVME_TYPE_VNODE;
1599 kve->kve_type = KVME_TYPE_SWAP;
1602 kve->kve_type = KVME_TYPE_DEVICE;
1605 kve->kve_type = KVME_TYPE_PHYS;
1608 kve->kve_type = KVME_TYPE_DEAD;
1611 kve->kve_type = KVME_TYPE_SG;
1614 kve->kve_type = KVME_TYPE_UNKNOWN;
1618 VM_OBJECT_UNLOCK(lobj);
1620 kve->kve_ref_count = obj->ref_count;
1621 kve->kve_shadow_count = obj->shadow_count;
1622 VM_OBJECT_UNLOCK(obj);
1624 vn_fullpath(curthread, vp, &fullpath,
1626 cred = curthread->td_ucred;
1627 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1628 vn_lock(vp, LK_SHARED | LK_RETRY);
1629 if (VOP_GETATTR(vp, &va, cred) == 0) {
1630 kve->kve_fileid = va.va_fileid;
1631 kve->kve_fsid = va.va_fsid;
1634 VFS_UNLOCK_GIANT(vfslocked);
1637 kve->kve_type = KVME_TYPE_NONE;
1638 kve->kve_ref_count = 0;
1639 kve->kve_shadow_count = 0;
1642 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1643 if (freepath != NULL)
1644 free(freepath, M_TEMP);
1646 error = SYSCTL_OUT(req, kve, sizeof(*kve));
1647 vm_map_lock_read(map);
1650 if (last_timestamp != map->timestamp) {
1651 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1655 vm_map_unlock_read(map);
1661 #endif /* COMPAT_FREEBSD7 */
1663 #ifdef KINFO_VMENTRY_SIZE
1664 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1668 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
1670 vm_map_entry_t entry, tmp_entry;
1671 unsigned int last_timestamp;
1672 char *fullpath, *freepath;
1673 struct kinfo_vmentry *kve;
1683 if ((p = pfind((pid_t)name[0])) == NULL)
1685 if (p->p_flag & P_WEXIT) {
1689 if ((error = p_candebug(curthread, p))) {
1695 vm = vmspace_acquire_ref(p);
1700 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
1702 map = &vm->vm_map; /* XXXRW: More locking required? */
1703 vm_map_lock_read(map);
1704 for (entry = map->header.next; entry != &map->header;
1705 entry = entry->next) {
1706 vm_object_t obj, tobj, lobj;
1710 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
1713 bzero(kve, sizeof(*kve));
1715 kve->kve_private_resident = 0;
1716 obj = entry->object.vm_object;
1718 VM_OBJECT_LOCK(obj);
1719 if (obj->shadow_count == 1)
1720 kve->kve_private_resident =
1721 obj->resident_page_count;
1723 kve->kve_resident = 0;
1724 addr = entry->start;
1725 while (addr < entry->end) {
1726 if (pmap_extract(map->pmap, addr))
1727 kve->kve_resident++;
1731 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
1733 VM_OBJECT_LOCK(tobj);
1735 VM_OBJECT_UNLOCK(lobj);
1739 kve->kve_start = entry->start;
1740 kve->kve_end = entry->end;
1741 kve->kve_offset = entry->offset;
1743 if (entry->protection & VM_PROT_READ)
1744 kve->kve_protection |= KVME_PROT_READ;
1745 if (entry->protection & VM_PROT_WRITE)
1746 kve->kve_protection |= KVME_PROT_WRITE;
1747 if (entry->protection & VM_PROT_EXECUTE)
1748 kve->kve_protection |= KVME_PROT_EXEC;
1750 if (entry->eflags & MAP_ENTRY_COW)
1751 kve->kve_flags |= KVME_FLAG_COW;
1752 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
1753 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
1755 last_timestamp = map->timestamp;
1756 vm_map_unlock_read(map);
1758 kve->kve_fileid = 0;
1764 switch (lobj->type) {
1766 kve->kve_type = KVME_TYPE_DEFAULT;
1769 kve->kve_type = KVME_TYPE_VNODE;
1774 kve->kve_type = KVME_TYPE_SWAP;
1777 kve->kve_type = KVME_TYPE_DEVICE;
1780 kve->kve_type = KVME_TYPE_PHYS;
1783 kve->kve_type = KVME_TYPE_DEAD;
1786 kve->kve_type = KVME_TYPE_SG;
1789 kve->kve_type = KVME_TYPE_UNKNOWN;
1793 VM_OBJECT_UNLOCK(lobj);
1795 kve->kve_ref_count = obj->ref_count;
1796 kve->kve_shadow_count = obj->shadow_count;
1797 VM_OBJECT_UNLOCK(obj);
1799 vn_fullpath(curthread, vp, &fullpath,
1801 cred = curthread->td_ucred;
1802 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1803 vn_lock(vp, LK_SHARED | LK_RETRY);
1804 if (VOP_GETATTR(vp, &va, cred) == 0) {
1805 kve->kve_fileid = va.va_fileid;
1806 kve->kve_fsid = va.va_fsid;
1809 VFS_UNLOCK_GIANT(vfslocked);
1812 kve->kve_type = KVME_TYPE_NONE;
1813 kve->kve_ref_count = 0;
1814 kve->kve_shadow_count = 0;
1817 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
1818 if (freepath != NULL)
1819 free(freepath, M_TEMP);
1821 /* Pack record size down */
1822 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
1823 strlen(kve->kve_path) + 1;
1824 kve->kve_structsize = roundup(kve->kve_structsize,
1826 error = SYSCTL_OUT(req, kve, kve->kve_structsize);
1827 vm_map_lock_read(map);
1830 if (last_timestamp != map->timestamp) {
1831 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
1835 vm_map_unlock_read(map);
1842 #if defined(STACK) || defined(DDB)
1844 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
1846 struct kinfo_kstack *kkstp;
1847 int error, i, *name, numthreads;
1848 lwpid_t *lwpidarray;
1855 if ((p = pfind((pid_t)name[0])) == NULL)
1857 /* XXXRW: Not clear ESRCH is the right error during proc execve(). */
1858 if (p->p_flag & P_WEXIT || p->p_flag & P_INEXEC) {
1862 if ((error = p_candebug(curthread, p))) {
1869 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
1870 st = stack_create();
1876 if (numthreads < p->p_numthreads) {
1877 if (lwpidarray != NULL) {
1878 free(lwpidarray, M_TEMP);
1881 numthreads = p->p_numthreads;
1883 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
1891 * XXXRW: During the below loop, execve(2) and countless other sorts
1892 * of changes could have taken place. Should we check to see if the
1893 * vmspace has been replaced, or the like, in order to prevent
1894 * giving a snapshot that spans, say, execve(2), with some threads
1895 * before and some after? Among other things, the credentials could
1896 * have changed, in which case the right to extract debug info might
1897 * no longer be assured.
1899 FOREACH_THREAD_IN_PROC(p, td) {
1900 KASSERT(i < numthreads,
1901 ("sysctl_kern_proc_kstack: numthreads"));
1902 lwpidarray[i] = td->td_tid;
1906 for (i = 0; i < numthreads; i++) {
1907 td = thread_find(p, lwpidarray[i]);
1911 bzero(kkstp, sizeof(*kkstp));
1912 (void)sbuf_new(&sb, kkstp->kkst_trace,
1913 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
1915 kkstp->kkst_tid = td->td_tid;
1916 if (TD_IS_SWAPPED(td))
1917 kkstp->kkst_state = KKST_STATE_SWAPPED;
1918 else if (TD_IS_RUNNING(td))
1919 kkstp->kkst_state = KKST_STATE_RUNNING;
1921 kkstp->kkst_state = KKST_STATE_STACKOK;
1922 stack_save_td(st, td);
1926 stack_sbuf_print(&sb, st);
1929 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
1936 if (lwpidarray != NULL)
1937 free(lwpidarray, M_TEMP);
1939 free(kkstp, M_TEMP);
1945 * This sysctl allows a process to retrieve the full list of groups from
1946 * itself or another process.
1949 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
1951 pid_t *pidp = (pid_t *)arg1;
1952 unsigned int arglen = arg2;
1959 if (*pidp == -1) { /* -1 means this process */
1960 p = req->td->td_proc;
1965 if ((error = p_cansee(curthread, p)) != 0) {
1971 cred = crhold(p->p_ucred);
1975 error = SYSCTL_OUT(req, cred->cr_groups,
1976 cred->cr_ngroups * sizeof(gid_t));
1981 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
1983 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
1984 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
1985 "Return entire process table");
1987 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1988 sysctl_kern_proc, "Process table");
1990 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
1991 sysctl_kern_proc, "Process table");
1993 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
1994 sysctl_kern_proc, "Process table");
1996 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
1997 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
1999 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2000 sysctl_kern_proc, "Process table");
2002 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2003 sysctl_kern_proc, "Process table");
2005 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2006 sysctl_kern_proc, "Process table");
2008 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2009 sysctl_kern_proc, "Process table");
2011 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2012 sysctl_kern_proc, "Return process table, no threads");
2014 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2015 CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2016 sysctl_kern_proc_args, "Process argument list");
2018 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2019 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2021 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2022 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2023 "Process syscall vector name (ABI type)");
2025 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2026 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2028 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2029 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2031 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2032 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2034 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2035 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2037 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2038 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2040 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2041 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2043 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2044 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2046 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2047 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2049 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2050 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2051 "Return process table, no threads");
2053 #ifdef COMPAT_FREEBSD7
2054 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2055 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2058 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2059 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2061 #if defined(STACK) || defined(DDB)
2062 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2063 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2066 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2067 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");