2 * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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/bitstring.h>
46 #include <sys/eventhandler.h>
49 #include <sys/kernel.h>
50 #include <sys/limits.h>
52 #include <sys/loginclass.h>
53 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
58 #include <sys/ptrace.h>
59 #include <sys/refcount.h>
60 #include <sys/resourcevar.h>
61 #include <sys/rwlock.h>
63 #include <sys/sysent.h>
64 #include <sys/sched.h>
66 #include <sys/stack.h>
68 #include <sys/sysctl.h>
69 #include <sys/filedesc.h>
71 #include <sys/signalvar.h>
75 #include <sys/vnode.h>
83 #include <vm/vm_param.h>
84 #include <vm/vm_extern.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_page.h>
91 #include <fs/devfs/devfs.h>
93 #ifdef COMPAT_FREEBSD32
94 #include <compat/freebsd32/freebsd32.h>
95 #include <compat/freebsd32/freebsd32_util.h>
98 SDT_PROVIDER_DEFINE(proc);
100 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
101 MALLOC_DEFINE(M_SESSION, "session", "session header");
102 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
103 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
105 static void doenterpgrp(struct proc *, struct pgrp *);
106 static void orphanpg(struct pgrp *pg);
107 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
108 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
109 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
111 static void pgadjustjobc(struct pgrp *pgrp, int entering);
112 static void pgdelete(struct pgrp *);
113 static int proc_ctor(void *mem, int size, void *arg, int flags);
114 static void proc_dtor(void *mem, int size, void *arg);
115 static int proc_init(void *mem, int size, int flags);
116 static void proc_fini(void *mem, int size);
117 static void pargs_free(struct pargs *pa);
120 * Other process lists
122 struct pidhashhead *pidhashtbl;
123 struct sx *pidhashtbl_lock;
126 struct pgrphashhead *pgrphashtbl;
128 struct proclist allproc;
129 struct sx __exclusive_cache_line allproc_lock;
130 struct sx __exclusive_cache_line proctree_lock;
131 struct mtx __exclusive_cache_line ppeers_lock;
132 struct mtx __exclusive_cache_line procid_lock;
133 uma_zone_t proc_zone;
136 * The offset of various fields in struct proc and struct thread.
137 * These are used by kernel debuggers to enumerate kernel threads and
140 const int proc_off_p_pid = offsetof(struct proc, p_pid);
141 const int proc_off_p_comm = offsetof(struct proc, p_comm);
142 const int proc_off_p_list = offsetof(struct proc, p_list);
143 const int proc_off_p_threads = offsetof(struct proc, p_threads);
144 const int thread_off_td_tid = offsetof(struct thread, td_tid);
145 const int thread_off_td_name = offsetof(struct thread, td_name);
146 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
147 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
148 const int thread_off_td_plist = offsetof(struct thread, td_plist);
150 EVENTHANDLER_LIST_DEFINE(process_ctor);
151 EVENTHANDLER_LIST_DEFINE(process_dtor);
152 EVENTHANDLER_LIST_DEFINE(process_init);
153 EVENTHANDLER_LIST_DEFINE(process_fini);
154 EVENTHANDLER_LIST_DEFINE(process_exit);
155 EVENTHANDLER_LIST_DEFINE(process_fork);
156 EVENTHANDLER_LIST_DEFINE(process_exec);
158 int kstack_pages = KSTACK_PAGES;
159 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
160 "Kernel stack size in pages");
161 static int vmmap_skip_res_cnt = 0;
162 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
163 &vmmap_skip_res_cnt, 0,
164 "Skip calculation of the pages resident count in kern.proc.vmmap");
166 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
167 #ifdef COMPAT_FREEBSD32
168 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
172 * Initialize global process hashing structures.
179 sx_init(&allproc_lock, "allproc");
180 sx_init(&proctree_lock, "proctree");
181 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
182 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
184 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
185 pidhashlock = (pidhash + 1) / 64;
188 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
189 M_PROC, M_WAITOK | M_ZERO);
190 for (i = 0; i < pidhashlock + 1; i++)
191 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
192 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
193 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
194 proc_ctor, proc_dtor, proc_init, proc_fini,
195 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
200 * Prepare a proc for use.
203 proc_ctor(void *mem, int size, void *arg, int flags)
208 p = (struct proc *)mem;
209 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
210 td = FIRST_THREAD_IN_PROC(p);
212 /* Make sure all thread constructors are executed */
213 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
219 * Reclaim a proc after use.
222 proc_dtor(void *mem, int size, void *arg)
227 /* INVARIANTS checks go here */
228 p = (struct proc *)mem;
229 td = FIRST_THREAD_IN_PROC(p);
232 KASSERT((p->p_numthreads == 1),
233 ("bad number of threads in exiting process"));
234 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
236 /* Free all OSD associated to this thread. */
238 td_softdep_cleanup(td);
239 MPASS(td->td_su == NULL);
241 /* Make sure all thread destructors are executed */
242 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
244 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
245 if (p->p_ksi != NULL)
246 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
250 * Initialize type-stable parts of a proc (when newly created).
253 proc_init(void *mem, int size, int flags)
257 p = (struct proc *)mem;
258 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
259 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
260 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
261 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
262 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
263 cv_init(&p->p_pwait, "ppwait");
264 TAILQ_INIT(&p->p_threads); /* all threads in proc */
265 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
266 p->p_stats = pstats_alloc();
272 * UMA should ensure that this function is never called.
273 * Freeing a proc structure would violate type stability.
276 proc_fini(void *mem, int size)
281 p = (struct proc *)mem;
282 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
283 pstats_free(p->p_stats);
284 thread_free(FIRST_THREAD_IN_PROC(p));
285 mtx_destroy(&p->p_mtx);
286 if (p->p_ksi != NULL)
287 ksiginfo_free(p->p_ksi);
289 panic("proc reclaimed");
294 * PID space management.
296 * These bitmaps are used by fork_findpid.
298 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
299 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
300 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
301 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
303 static bitstr_t *proc_id_array[] = {
311 proc_id_set(int type, pid_t id)
314 KASSERT(type >= 0 && type < nitems(proc_id_array),
315 ("invalid type %d\n", type));
316 mtx_lock(&procid_lock);
317 KASSERT(bit_test(proc_id_array[type], id) == 0,
318 ("bit %d already set in %d\n", id, type));
319 bit_set(proc_id_array[type], id);
320 mtx_unlock(&procid_lock);
324 proc_id_set_cond(int type, pid_t id)
327 KASSERT(type >= 0 && type < nitems(proc_id_array),
328 ("invalid type %d\n", type));
329 if (bit_test(proc_id_array[type], id))
331 mtx_lock(&procid_lock);
332 bit_set(proc_id_array[type], id);
333 mtx_unlock(&procid_lock);
337 proc_id_clear(int type, pid_t id)
340 KASSERT(type >= 0 && type < nitems(proc_id_array),
341 ("invalid type %d\n", type));
342 mtx_lock(&procid_lock);
343 KASSERT(bit_test(proc_id_array[type], id) != 0,
344 ("bit %d not set in %d\n", id, type));
345 bit_clear(proc_id_array[type], id);
346 mtx_unlock(&procid_lock);
350 * Is p an inferior of the current process?
353 inferior(struct proc *p)
356 sx_assert(&proctree_lock, SX_LOCKED);
357 PROC_LOCK_ASSERT(p, MA_OWNED);
358 for (; p != curproc; p = proc_realparent(p)) {
366 * Shared lock all the pid hash lists.
369 pidhash_slockall(void)
373 for (i = 0; i < pidhashlock + 1; i++)
374 sx_slock(&pidhashtbl_lock[i]);
378 * Shared unlock all the pid hash lists.
381 pidhash_sunlockall(void)
385 for (i = 0; i < pidhashlock + 1; i++)
386 sx_sunlock(&pidhashtbl_lock[i]);
390 * Similar to pfind_any(), this function finds zombies.
393 pfind_any_locked(pid_t pid)
397 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
398 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
399 if (p->p_pid == pid) {
401 if (p->p_state == PRS_NEW) {
412 * Locate a process by number.
414 * By not returning processes in the PRS_NEW state, we allow callers to avoid
415 * testing for that condition to avoid dereferencing p_ucred, et al.
417 static __always_inline struct proc *
418 _pfind(pid_t pid, bool zombie)
423 if (p->p_pid == pid) {
427 sx_slock(PIDHASHLOCK(pid));
428 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
429 if (p->p_pid == pid) {
431 if (p->p_state == PRS_NEW ||
432 (!zombie && p->p_state == PRS_ZOMBIE)) {
439 sx_sunlock(PIDHASHLOCK(pid));
447 return (_pfind(pid, false));
451 * Same as pfind but allow zombies.
457 return (_pfind(pid, true));
461 * Locate a process group by number.
462 * The caller must hold proctree_lock.
469 sx_assert(&proctree_lock, SX_LOCKED);
471 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
472 if (pgrp->pg_id == pgid) {
481 * Locate process and do additional manipulations, depending on flags.
484 pget(pid_t pid, int flags, struct proc **pp)
491 if (p->p_pid == pid) {
495 if (pid <= PID_MAX) {
496 if ((flags & PGET_NOTWEXIT) == 0)
500 } else if ((flags & PGET_NOTID) == 0) {
501 td1 = tdfind(pid, -1);
507 if ((flags & PGET_CANSEE) != 0) {
508 error = p_cansee(curthread, p);
513 if ((flags & PGET_CANDEBUG) != 0) {
514 error = p_candebug(curthread, p);
518 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
522 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
526 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
528 * XXXRW: Not clear ESRCH is the right error during proc
534 if ((flags & PGET_HOLD) != 0) {
546 * Create a new process group.
547 * pgid must be equal to the pid of p.
548 * Begin a new session if required.
551 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
554 sx_assert(&proctree_lock, SX_XLOCKED);
556 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
557 KASSERT(p->p_pid == pgid,
558 ("enterpgrp: new pgrp and pid != pgid"));
559 KASSERT(pgfind(pgid) == NULL,
560 ("enterpgrp: pgrp with pgid exists"));
561 KASSERT(!SESS_LEADER(p),
562 ("enterpgrp: session leader attempted setpgrp"));
564 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
570 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
572 p->p_flag &= ~P_CONTROLT;
576 sess->s_sid = p->p_pid;
577 proc_id_set(PROC_ID_SESSION, p->p_pid);
578 refcount_init(&sess->s_count, 1);
579 sess->s_ttyvp = NULL;
580 sess->s_ttydp = NULL;
582 bcopy(p->p_session->s_login, sess->s_login,
583 sizeof(sess->s_login));
584 pgrp->pg_session = sess;
585 KASSERT(p == curproc,
586 ("enterpgrp: mksession and p != curproc"));
588 pgrp->pg_session = p->p_session;
589 sess_hold(pgrp->pg_session);
593 proc_id_set(PROC_ID_GROUP, p->p_pid);
594 LIST_INIT(&pgrp->pg_members);
597 * As we have an exclusive lock of proctree_lock,
598 * this should not deadlock.
600 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
602 SLIST_INIT(&pgrp->pg_sigiolst);
605 doenterpgrp(p, pgrp);
611 * Move p to an existing process group
614 enterthispgrp(struct proc *p, struct pgrp *pgrp)
617 sx_assert(&proctree_lock, SX_XLOCKED);
618 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
619 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
620 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
621 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
622 KASSERT(pgrp->pg_session == p->p_session,
623 ("%s: pgrp's session %p, p->p_session %p.\n",
627 KASSERT(pgrp != p->p_pgrp,
628 ("%s: p belongs to pgrp.", __func__));
630 doenterpgrp(p, pgrp);
636 * Move p to a process group
639 doenterpgrp(struct proc *p, struct pgrp *pgrp)
641 struct pgrp *savepgrp;
643 sx_assert(&proctree_lock, SX_XLOCKED);
644 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
645 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
646 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
647 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
649 savepgrp = p->p_pgrp;
652 * Adjust eligibility of affected pgrps to participate in job control.
653 * Increment eligibility counts before decrementing, otherwise we
654 * could reach 0 spuriously during the first call.
657 fixjobc(p, p->p_pgrp, 0);
662 LIST_REMOVE(p, p_pglist);
665 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
666 PGRP_UNLOCK(savepgrp);
668 if (LIST_EMPTY(&savepgrp->pg_members))
673 * remove process from process group
676 leavepgrp(struct proc *p)
678 struct pgrp *savepgrp;
680 sx_assert(&proctree_lock, SX_XLOCKED);
681 savepgrp = p->p_pgrp;
684 LIST_REMOVE(p, p_pglist);
687 PGRP_UNLOCK(savepgrp);
688 if (LIST_EMPTY(&savepgrp->pg_members))
694 * delete a process group
697 pgdelete(struct pgrp *pgrp)
699 struct session *savesess;
702 sx_assert(&proctree_lock, SX_XLOCKED);
703 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
704 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
707 * Reset any sigio structures pointing to us as a result of
708 * F_SETOWN with our pgid.
710 funsetownlst(&pgrp->pg_sigiolst);
713 tp = pgrp->pg_session->s_ttyp;
714 LIST_REMOVE(pgrp, pg_hash);
715 savesess = pgrp->pg_session;
718 /* Remove the reference to the pgrp before deallocating it. */
721 tty_rel_pgrp(tp, pgrp);
724 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
725 mtx_destroy(&pgrp->pg_mtx);
727 sess_release(savesess);
731 pgadjustjobc(struct pgrp *pgrp, int entering)
737 MPASS(pgrp->pg_jobc >= 0);
742 MPASS(pgrp->pg_jobc > 0);
745 if (pgrp->pg_jobc == 0)
752 * Adjust pgrp jobc counters when specified process changes process group.
753 * We count the number of processes in each process group that "qualify"
754 * the group for terminal job control (those with a parent in a different
755 * process group of the same session). If that count reaches zero, the
756 * process group becomes orphaned. Check both the specified process'
757 * process group and that of its children.
758 * entering == 0 => p is leaving specified group.
759 * entering == 1 => p is entering specified group.
762 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
764 struct pgrp *hispgrp;
765 struct session *mysession;
768 sx_assert(&proctree_lock, SX_LOCKED);
769 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
770 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
771 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
774 * Check p's parent to see whether p qualifies its own process
775 * group; if so, adjust count for p's process group.
777 mysession = pgrp->pg_session;
778 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
779 hispgrp->pg_session == mysession)
780 pgadjustjobc(pgrp, entering);
783 * Check this process' children to see whether they qualify
784 * their process groups; if so, adjust counts for children's
787 LIST_FOREACH(q, &p->p_children, p_sibling) {
789 if (hispgrp == pgrp ||
790 hispgrp->pg_session != mysession)
792 if (q->p_state == PRS_ZOMBIE)
794 pgadjustjobc(hispgrp, entering);
807 MPASS(p->p_flag & P_WEXIT);
809 * Do a quick check to see if there is anything to do with the
810 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
813 if (!SESS_LEADER(p) &&
814 (p->p_pgrp == p->p_pptr->p_pgrp) &&
815 LIST_EMPTY(&p->p_children)) {
821 sx_xlock(&proctree_lock);
822 if (SESS_LEADER(p)) {
826 * s_ttyp is not zero'd; we use this to indicate that
827 * the session once had a controlling terminal. (for
828 * logging and informational purposes)
839 * Signal foreground pgrp and revoke access to
840 * controlling terminal if it has not been revoked
843 * Because the TTY may have been revoked in the mean
844 * time and could already have a new session associated
845 * with it, make sure we don't send a SIGHUP to a
846 * foreground process group that does not belong to this
852 if (tp->t_session == sp)
853 tty_signal_pgrp(tp, SIGHUP);
858 sx_xunlock(&proctree_lock);
859 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
860 VOP_REVOKE(ttyvp, REVOKEALL);
863 devfs_ctty_unref(ttyvp);
864 sx_xlock(&proctree_lock);
867 fixjobc(p, p->p_pgrp, 0);
868 sx_xunlock(&proctree_lock);
872 * A process group has become orphaned;
873 * if there are any stopped processes in the group,
874 * hang-up all process in that group.
877 orphanpg(struct pgrp *pg)
881 PGRP_LOCK_ASSERT(pg, MA_OWNED);
883 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
885 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
887 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
889 kern_psignal(p, SIGHUP);
890 kern_psignal(p, SIGCONT);
900 sess_hold(struct session *s)
903 refcount_acquire(&s->s_count);
907 sess_release(struct session *s)
910 if (refcount_release(&s->s_count)) {
911 if (s->s_ttyp != NULL) {
913 tty_rel_sess(s->s_ttyp, s);
915 proc_id_clear(PROC_ID_SESSION, s->s_sid);
916 mtx_destroy(&s->s_mtx);
923 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
929 for (i = 0; i <= pgrphash; i++) {
930 if (!LIST_EMPTY(&pgrphashtbl[i])) {
931 printf("\tindx %d\n", i);
932 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
934 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
935 (void *)pgrp, (long)pgrp->pg_id,
936 (void *)pgrp->pg_session,
937 pgrp->pg_session->s_count,
938 (void *)LIST_FIRST(&pgrp->pg_members));
939 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
940 printf("\t\tpid %ld addr %p pgrp %p\n",
941 (long)p->p_pid, (void *)p,
951 * Calculate the kinfo_proc members which contain process-wide
953 * Must be called with the target process locked.
956 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
960 PROC_LOCK_ASSERT(p, MA_OWNED);
964 FOREACH_THREAD_IN_PROC(p, td) {
966 kp->ki_pctcpu += sched_pctcpu(td);
967 kp->ki_estcpu += sched_estcpu(td);
973 * Clear kinfo_proc and fill in any information that is common
974 * to all threads in the process.
975 * Must be called with the target process locked.
978 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
985 struct timeval boottime;
987 PROC_LOCK_ASSERT(p, MA_OWNED);
988 bzero(kp, sizeof(*kp));
990 kp->ki_structsize = sizeof(*kp);
992 kp->ki_addr =/* p->p_addr; */0; /* XXX */
993 kp->ki_args = p->p_args;
994 kp->ki_textvp = p->p_textvp;
996 kp->ki_tracep = p->p_tracevp;
997 kp->ki_traceflag = p->p_traceflag;
1000 kp->ki_vmspace = p->p_vmspace;
1001 kp->ki_flag = p->p_flag;
1002 kp->ki_flag2 = p->p_flag2;
1005 kp->ki_uid = cred->cr_uid;
1006 kp->ki_ruid = cred->cr_ruid;
1007 kp->ki_svuid = cred->cr_svuid;
1008 kp->ki_cr_flags = 0;
1009 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1010 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1011 /* XXX bde doesn't like KI_NGROUPS */
1012 if (cred->cr_ngroups > KI_NGROUPS) {
1013 kp->ki_ngroups = KI_NGROUPS;
1014 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1016 kp->ki_ngroups = cred->cr_ngroups;
1017 bcopy(cred->cr_groups, kp->ki_groups,
1018 kp->ki_ngroups * sizeof(gid_t));
1019 kp->ki_rgid = cred->cr_rgid;
1020 kp->ki_svgid = cred->cr_svgid;
1021 /* If jailed(cred), emulate the old P_JAILED flag. */
1023 kp->ki_flag |= P_JAILED;
1024 /* If inside the jail, use 0 as a jail ID. */
1025 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1026 kp->ki_jid = cred->cr_prison->pr_id;
1028 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1029 sizeof(kp->ki_loginclass));
1033 mtx_lock(&ps->ps_mtx);
1034 kp->ki_sigignore = ps->ps_sigignore;
1035 kp->ki_sigcatch = ps->ps_sigcatch;
1036 mtx_unlock(&ps->ps_mtx);
1038 if (p->p_state != PRS_NEW &&
1039 p->p_state != PRS_ZOMBIE &&
1040 p->p_vmspace != NULL) {
1041 struct vmspace *vm = p->p_vmspace;
1043 kp->ki_size = vm->vm_map.size;
1044 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1045 FOREACH_THREAD_IN_PROC(p, td0) {
1046 if (!TD_IS_SWAPPED(td0))
1047 kp->ki_rssize += td0->td_kstack_pages;
1049 kp->ki_swrss = vm->vm_swrss;
1050 kp->ki_tsize = vm->vm_tsize;
1051 kp->ki_dsize = vm->vm_dsize;
1052 kp->ki_ssize = vm->vm_ssize;
1053 } else if (p->p_state == PRS_ZOMBIE)
1054 kp->ki_stat = SZOMB;
1055 if (kp->ki_flag & P_INMEM)
1056 kp->ki_sflag = PS_INMEM;
1059 /* Calculate legacy swtime as seconds since 'swtick'. */
1060 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1061 kp->ki_pid = p->p_pid;
1062 kp->ki_nice = p->p_nice;
1063 kp->ki_fibnum = p->p_fibnum;
1064 kp->ki_start = p->p_stats->p_start;
1065 getboottime(&boottime);
1066 timevaladd(&kp->ki_start, &boottime);
1068 rufetch(p, &kp->ki_rusage);
1069 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1070 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1072 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1073 /* Some callers want child times in a single value. */
1074 kp->ki_childtime = kp->ki_childstime;
1075 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1077 FOREACH_THREAD_IN_PROC(p, td0)
1078 kp->ki_cow += td0->td_cow;
1082 kp->ki_pgid = p->p_pgrp->pg_id;
1083 kp->ki_jobc = p->p_pgrp->pg_jobc;
1084 sp = p->p_pgrp->pg_session;
1087 kp->ki_sid = sp->s_sid;
1089 strlcpy(kp->ki_login, sp->s_login,
1090 sizeof(kp->ki_login));
1092 kp->ki_kiflag |= KI_CTTY;
1094 kp->ki_kiflag |= KI_SLEADER;
1095 /* XXX proctree_lock */
1100 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1101 kp->ki_tdev = tty_udev(tp);
1102 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1103 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1105 kp->ki_tsid = tp->t_session->s_sid;
1107 kp->ki_tdev = NODEV;
1108 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1110 if (p->p_comm[0] != '\0')
1111 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1112 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1113 p->p_sysent->sv_name[0] != '\0')
1114 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1115 kp->ki_siglist = p->p_siglist;
1116 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1117 kp->ki_acflag = p->p_acflag;
1118 kp->ki_lock = p->p_lock;
1120 kp->ki_ppid = p->p_oppid;
1121 if (p->p_flag & P_TRACED)
1122 kp->ki_tracer = p->p_pptr->p_pid;
1127 * Fill in information that is thread specific. Must be called with
1128 * target process locked. If 'preferthread' is set, overwrite certain
1129 * process-related fields that are maintained for both threads and
1133 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1139 PROC_LOCK_ASSERT(p, MA_OWNED);
1144 if (td->td_wmesg != NULL)
1145 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1147 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1148 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1149 sizeof(kp->ki_tdname)) {
1150 strlcpy(kp->ki_moretdname,
1151 td->td_name + sizeof(kp->ki_tdname) - 1,
1152 sizeof(kp->ki_moretdname));
1154 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1156 if (TD_ON_LOCK(td)) {
1157 kp->ki_kiflag |= KI_LOCKBLOCK;
1158 strlcpy(kp->ki_lockname, td->td_lockname,
1159 sizeof(kp->ki_lockname));
1161 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1162 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1165 if (p->p_state == PRS_NORMAL) { /* approximate. */
1166 if (TD_ON_RUNQ(td) ||
1168 TD_IS_RUNNING(td)) {
1170 } else if (P_SHOULDSTOP(p)) {
1171 kp->ki_stat = SSTOP;
1172 } else if (TD_IS_SLEEPING(td)) {
1173 kp->ki_stat = SSLEEP;
1174 } else if (TD_ON_LOCK(td)) {
1175 kp->ki_stat = SLOCK;
1177 kp->ki_stat = SWAIT;
1179 } else if (p->p_state == PRS_ZOMBIE) {
1180 kp->ki_stat = SZOMB;
1185 /* Things in the thread */
1186 kp->ki_wchan = td->td_wchan;
1187 kp->ki_pri.pri_level = td->td_priority;
1188 kp->ki_pri.pri_native = td->td_base_pri;
1191 * Note: legacy fields; clamp at the old NOCPU value and/or
1192 * the maximum u_char CPU value.
1194 if (td->td_lastcpu == NOCPU)
1195 kp->ki_lastcpu_old = NOCPU_OLD;
1196 else if (td->td_lastcpu > MAXCPU_OLD)
1197 kp->ki_lastcpu_old = MAXCPU_OLD;
1199 kp->ki_lastcpu_old = td->td_lastcpu;
1201 if (td->td_oncpu == NOCPU)
1202 kp->ki_oncpu_old = NOCPU_OLD;
1203 else if (td->td_oncpu > MAXCPU_OLD)
1204 kp->ki_oncpu_old = MAXCPU_OLD;
1206 kp->ki_oncpu_old = td->td_oncpu;
1208 kp->ki_lastcpu = td->td_lastcpu;
1209 kp->ki_oncpu = td->td_oncpu;
1210 kp->ki_tdflags = td->td_flags;
1211 kp->ki_tid = td->td_tid;
1212 kp->ki_numthreads = p->p_numthreads;
1213 kp->ki_pcb = td->td_pcb;
1214 kp->ki_kstack = (void *)td->td_kstack;
1215 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1216 kp->ki_pri.pri_class = td->td_pri_class;
1217 kp->ki_pri.pri_user = td->td_user_pri;
1220 rufetchtd(td, &kp->ki_rusage);
1221 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1222 kp->ki_pctcpu = sched_pctcpu(td);
1223 kp->ki_estcpu = sched_estcpu(td);
1224 kp->ki_cow = td->td_cow;
1227 /* We can't get this anymore but ps etc never used it anyway. */
1231 kp->ki_siglist = td->td_siglist;
1232 kp->ki_sigmask = td->td_sigmask;
1239 * Fill in a kinfo_proc structure for the specified process.
1240 * Must be called with the target process locked.
1243 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1246 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1248 fill_kinfo_proc_only(p, kp);
1249 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1250 fill_kinfo_aggregate(p, kp);
1257 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1261 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1264 pstats_fork(struct pstats *src, struct pstats *dst)
1267 bzero(&dst->pstat_startzero,
1268 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1269 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1270 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1274 pstats_free(struct pstats *ps)
1277 free(ps, M_SUBPROC);
1280 #ifdef COMPAT_FREEBSD32
1283 * This function is typically used to copy out the kernel address, so
1284 * it can be replaced by assignment of zero.
1286 static inline uint32_t
1287 ptr32_trim(const void *ptr)
1291 uptr = (uintptr_t)ptr;
1292 return ((uptr > UINT_MAX) ? 0 : uptr);
1295 #define PTRTRIM_CP(src,dst,fld) \
1296 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1299 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1303 bzero(ki32, sizeof(struct kinfo_proc32));
1304 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1305 CP(*ki, *ki32, ki_layout);
1306 PTRTRIM_CP(*ki, *ki32, ki_args);
1307 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1308 PTRTRIM_CP(*ki, *ki32, ki_addr);
1309 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1310 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1311 PTRTRIM_CP(*ki, *ki32, ki_fd);
1312 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1313 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1314 CP(*ki, *ki32, ki_pid);
1315 CP(*ki, *ki32, ki_ppid);
1316 CP(*ki, *ki32, ki_pgid);
1317 CP(*ki, *ki32, ki_tpgid);
1318 CP(*ki, *ki32, ki_sid);
1319 CP(*ki, *ki32, ki_tsid);
1320 CP(*ki, *ki32, ki_jobc);
1321 CP(*ki, *ki32, ki_tdev);
1322 CP(*ki, *ki32, ki_tdev_freebsd11);
1323 CP(*ki, *ki32, ki_siglist);
1324 CP(*ki, *ki32, ki_sigmask);
1325 CP(*ki, *ki32, ki_sigignore);
1326 CP(*ki, *ki32, ki_sigcatch);
1327 CP(*ki, *ki32, ki_uid);
1328 CP(*ki, *ki32, ki_ruid);
1329 CP(*ki, *ki32, ki_svuid);
1330 CP(*ki, *ki32, ki_rgid);
1331 CP(*ki, *ki32, ki_svgid);
1332 CP(*ki, *ki32, ki_ngroups);
1333 for (i = 0; i < KI_NGROUPS; i++)
1334 CP(*ki, *ki32, ki_groups[i]);
1335 CP(*ki, *ki32, ki_size);
1336 CP(*ki, *ki32, ki_rssize);
1337 CP(*ki, *ki32, ki_swrss);
1338 CP(*ki, *ki32, ki_tsize);
1339 CP(*ki, *ki32, ki_dsize);
1340 CP(*ki, *ki32, ki_ssize);
1341 CP(*ki, *ki32, ki_xstat);
1342 CP(*ki, *ki32, ki_acflag);
1343 CP(*ki, *ki32, ki_pctcpu);
1344 CP(*ki, *ki32, ki_estcpu);
1345 CP(*ki, *ki32, ki_slptime);
1346 CP(*ki, *ki32, ki_swtime);
1347 CP(*ki, *ki32, ki_cow);
1348 CP(*ki, *ki32, ki_runtime);
1349 TV_CP(*ki, *ki32, ki_start);
1350 TV_CP(*ki, *ki32, ki_childtime);
1351 CP(*ki, *ki32, ki_flag);
1352 CP(*ki, *ki32, ki_kiflag);
1353 CP(*ki, *ki32, ki_traceflag);
1354 CP(*ki, *ki32, ki_stat);
1355 CP(*ki, *ki32, ki_nice);
1356 CP(*ki, *ki32, ki_lock);
1357 CP(*ki, *ki32, ki_rqindex);
1358 CP(*ki, *ki32, ki_oncpu);
1359 CP(*ki, *ki32, ki_lastcpu);
1361 /* XXX TODO: wrap cpu value as appropriate */
1362 CP(*ki, *ki32, ki_oncpu_old);
1363 CP(*ki, *ki32, ki_lastcpu_old);
1365 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1366 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1367 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1368 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1369 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1370 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1371 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1372 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1373 CP(*ki, *ki32, ki_tracer);
1374 CP(*ki, *ki32, ki_flag2);
1375 CP(*ki, *ki32, ki_fibnum);
1376 CP(*ki, *ki32, ki_cr_flags);
1377 CP(*ki, *ki32, ki_jid);
1378 CP(*ki, *ki32, ki_numthreads);
1379 CP(*ki, *ki32, ki_tid);
1380 CP(*ki, *ki32, ki_pri);
1381 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1382 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1383 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1384 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1385 PTRTRIM_CP(*ki, *ki32, ki_udata);
1386 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1387 CP(*ki, *ki32, ki_sflag);
1388 CP(*ki, *ki32, ki_tdflags);
1393 kern_proc_out_size(struct proc *p, int flags)
1397 PROC_LOCK_ASSERT(p, MA_OWNED);
1399 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1400 #ifdef COMPAT_FREEBSD32
1401 if ((flags & KERN_PROC_MASK32) != 0) {
1402 size += sizeof(struct kinfo_proc32);
1405 size += sizeof(struct kinfo_proc);
1407 #ifdef COMPAT_FREEBSD32
1408 if ((flags & KERN_PROC_MASK32) != 0)
1409 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1412 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1419 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1422 struct kinfo_proc ki;
1423 #ifdef COMPAT_FREEBSD32
1424 struct kinfo_proc32 ki32;
1428 PROC_LOCK_ASSERT(p, MA_OWNED);
1429 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1432 fill_kinfo_proc(p, &ki);
1433 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1434 #ifdef COMPAT_FREEBSD32
1435 if ((flags & KERN_PROC_MASK32) != 0) {
1436 freebsd32_kinfo_proc_out(&ki, &ki32);
1437 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1441 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1444 FOREACH_THREAD_IN_PROC(p, td) {
1445 fill_kinfo_thread(td, &ki, 1);
1446 #ifdef COMPAT_FREEBSD32
1447 if ((flags & KERN_PROC_MASK32) != 0) {
1448 freebsd32_kinfo_proc_out(&ki, &ki32);
1449 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1453 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1464 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1467 struct kinfo_proc ki;
1470 if (req->oldptr == NULL)
1471 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1473 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1474 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1475 error = kern_proc_out(p, &sb, flags);
1476 error2 = sbuf_finish(&sb);
1480 else if (error2 != 0)
1486 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1491 for (i = 0; i < pidhashlock + 1; i++) {
1492 sx_slock(&pidhashtbl_lock[i]);
1493 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1494 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1495 if (p->p_state == PRS_NEW)
1497 error = cb(p, cbarg);
1498 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1500 sx_sunlock(&pidhashtbl_lock[i]);
1505 sx_sunlock(&pidhashtbl_lock[i]);
1510 struct kern_proc_out_args {
1511 struct sysctl_req *req;
1518 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1520 struct kern_proc_out_args *arg = origarg;
1521 int *name = arg->name;
1522 int oid_number = arg->oid_number;
1523 int flags = arg->flags;
1524 struct sysctl_req *req = arg->req;
1529 KASSERT(p->p_ucred != NULL,
1530 ("process credential is NULL for non-NEW proc"));
1532 * Show a user only appropriate processes.
1534 if (p_cansee(curthread, p))
1537 * TODO - make more efficient (see notes below).
1540 switch (oid_number) {
1543 if (p->p_ucred->cr_gid != (gid_t)name[0])
1547 case KERN_PROC_PGRP:
1548 /* could do this by traversing pgrp */
1549 if (p->p_pgrp == NULL ||
1550 p->p_pgrp->pg_id != (pid_t)name[0])
1554 case KERN_PROC_RGID:
1555 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1559 case KERN_PROC_SESSION:
1560 if (p->p_session == NULL ||
1561 p->p_session->s_sid != (pid_t)name[0])
1566 if ((p->p_flag & P_CONTROLT) == 0 ||
1567 p->p_session == NULL)
1569 /* XXX proctree_lock */
1570 SESS_LOCK(p->p_session);
1571 if (p->p_session->s_ttyp == NULL ||
1572 tty_udev(p->p_session->s_ttyp) !=
1574 SESS_UNLOCK(p->p_session);
1577 SESS_UNLOCK(p->p_session);
1581 if (p->p_ucred->cr_uid != (uid_t)name[0])
1585 case KERN_PROC_RUID:
1586 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1590 case KERN_PROC_PROC:
1597 error = sysctl_out_proc(p, req, flags);
1598 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1606 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1608 struct kern_proc_out_args iterarg;
1609 int *name = (int *)arg1;
1610 u_int namelen = arg2;
1612 int flags, oid_number;
1615 oid_number = oidp->oid_number;
1616 if (oid_number != KERN_PROC_ALL &&
1617 (oid_number & KERN_PROC_INC_THREAD) == 0)
1618 flags = KERN_PROC_NOTHREADS;
1621 oid_number &= ~KERN_PROC_INC_THREAD;
1623 #ifdef COMPAT_FREEBSD32
1624 if (req->flags & SCTL_MASK32)
1625 flags |= KERN_PROC_MASK32;
1627 if (oid_number == KERN_PROC_PID) {
1630 error = sysctl_wire_old_buffer(req, 0);
1633 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1635 error = sysctl_out_proc(p, req, flags);
1639 switch (oid_number) {
1644 case KERN_PROC_PROC:
1645 if (namelen != 0 && namelen != 1)
1654 if (req->oldptr == NULL) {
1655 /* overestimate by 5 procs */
1656 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1660 error = sysctl_wire_old_buffer(req, 0);
1664 iterarg.flags = flags;
1665 iterarg.oid_number = oid_number;
1667 iterarg.name = name;
1668 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1673 pargs_alloc(int len)
1677 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1679 refcount_init(&pa->ar_ref, 1);
1680 pa->ar_length = len;
1685 pargs_free(struct pargs *pa)
1692 pargs_hold(struct pargs *pa)
1697 refcount_acquire(&pa->ar_ref);
1701 pargs_drop(struct pargs *pa)
1706 if (refcount_release(&pa->ar_ref))
1711 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1717 * This may return a short read if the string is shorter than the chunk
1718 * and is aligned at the end of the page, and the following page is not
1721 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1727 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1729 enum proc_vector_type {
1735 #ifdef COMPAT_FREEBSD32
1737 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1738 size_t *vsizep, enum proc_vector_type type)
1740 struct freebsd32_ps_strings pss;
1742 vm_offset_t vptr, ptr;
1743 uint32_t *proc_vector32;
1749 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1750 sizeof(pss)) != sizeof(pss))
1754 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1755 vsize = pss.ps_nargvstr;
1756 if (vsize > ARG_MAX)
1758 size = vsize * sizeof(int32_t);
1761 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1762 vsize = pss.ps_nenvstr;
1763 if (vsize > ARG_MAX)
1765 size = vsize * sizeof(int32_t);
1768 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1769 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1772 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1773 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1776 if (aux.a_type == AT_NULL)
1780 if (aux.a_type != AT_NULL)
1783 size = vsize * sizeof(aux);
1786 KASSERT(0, ("Wrong proc vector type: %d", type));
1789 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1790 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1794 if (type == PROC_AUX) {
1795 *proc_vectorp = (char **)proc_vector32;
1799 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1800 for (i = 0; i < (int)vsize; i++)
1801 proc_vector[i] = PTRIN(proc_vector32[i]);
1802 *proc_vectorp = proc_vector;
1805 free(proc_vector32, M_TEMP);
1811 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1812 size_t *vsizep, enum proc_vector_type type)
1814 struct ps_strings pss;
1816 vm_offset_t vptr, ptr;
1821 #ifdef COMPAT_FREEBSD32
1822 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1823 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1825 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1826 sizeof(pss)) != sizeof(pss))
1830 vptr = (vm_offset_t)pss.ps_argvstr;
1831 vsize = pss.ps_nargvstr;
1832 if (vsize > ARG_MAX)
1834 size = vsize * sizeof(char *);
1837 vptr = (vm_offset_t)pss.ps_envstr;
1838 vsize = pss.ps_nenvstr;
1839 if (vsize > ARG_MAX)
1841 size = vsize * sizeof(char *);
1845 * The aux array is just above env array on the stack. Check
1846 * that the address is naturally aligned.
1848 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1850 #if __ELF_WORD_SIZE == 64
1851 if (vptr % sizeof(uint64_t) != 0)
1853 if (vptr % sizeof(uint32_t) != 0)
1857 * We count the array size reading the aux vectors from the
1858 * stack until AT_NULL vector is returned. So (to keep the code
1859 * simple) we read the process stack twice: the first time here
1860 * to find the size and the second time when copying the vectors
1861 * to the allocated proc_vector.
1863 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1864 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1867 if (aux.a_type == AT_NULL)
1872 * If the PROC_AUXV_MAX entries are iterated over, and we have
1873 * not reached AT_NULL, it is most likely we are reading wrong
1874 * data: either the process doesn't have auxv array or data has
1875 * been modified. Return the error in this case.
1877 if (aux.a_type != AT_NULL)
1880 size = vsize * sizeof(aux);
1883 KASSERT(0, ("Wrong proc vector type: %d", type));
1884 return (EINVAL); /* In case we are built without INVARIANTS. */
1886 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1887 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1888 free(proc_vector, M_TEMP);
1891 *proc_vectorp = proc_vector;
1897 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1900 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1901 enum proc_vector_type type)
1903 size_t done, len, nchr, vsize;
1905 char **proc_vector, *sptr;
1906 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1908 PROC_ASSERT_HELD(p);
1911 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1913 nchr = 2 * (PATH_MAX + ARG_MAX);
1915 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1918 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1920 * The program may have scribbled into its argv array, e.g. to
1921 * remove some arguments. If that has happened, break out
1922 * before trying to read from NULL.
1924 if (proc_vector[i] == NULL)
1926 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1927 error = proc_read_string(td, p, sptr, pss_string,
1928 sizeof(pss_string));
1931 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1932 if (done + len >= nchr)
1933 len = nchr - done - 1;
1934 sbuf_bcat(sb, pss_string, len);
1935 if (len != GET_PS_STRINGS_CHUNK_SZ)
1937 done += GET_PS_STRINGS_CHUNK_SZ;
1939 sbuf_bcat(sb, "", 1);
1943 free(proc_vector, M_TEMP);
1948 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1951 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1955 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1958 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1962 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1968 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1970 #ifdef COMPAT_FREEBSD32
1971 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1972 size = vsize * sizeof(Elf32_Auxinfo);
1975 size = vsize * sizeof(Elf_Auxinfo);
1976 if (sbuf_bcat(sb, auxv, size) != 0)
1984 * This sysctl allows a process to retrieve the argument list or process
1985 * title for another process without groping around in the address space
1986 * of the other process. It also allow a process to set its own "process
1987 * title to a string of its own choice.
1990 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1992 int *name = (int *)arg1;
1993 u_int namelen = arg2;
1994 struct pargs *newpa, *pa;
1997 int flags, error = 0, error2;
2003 pid = (pid_t)name[0];
2005 * If the query is for this process and it is single-threaded, there
2006 * is nobody to modify pargs, thus we can just read.
2009 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2010 (pa = p->p_args) != NULL)
2011 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2013 flags = PGET_CANSEE;
2014 if (req->newptr != NULL)
2015 flags |= PGET_ISCURRENT;
2016 error = pget(pid, flags, &p);
2024 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2026 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2029 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2030 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2031 error = proc_getargv(curthread, p, &sb);
2032 error2 = sbuf_finish(&sb);
2035 if (error == 0 && error2 != 0)
2040 if (error != 0 || req->newptr == NULL)
2043 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2046 if (req->newlen == 0) {
2048 * Clear the argument pointer, so that we'll fetch arguments
2049 * with proc_getargv() until further notice.
2053 newpa = pargs_alloc(req->newlen);
2054 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2069 * This sysctl allows a process to retrieve environment of another process.
2072 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2074 int *name = (int *)arg1;
2075 u_int namelen = arg2;
2083 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2086 if ((p->p_flag & P_SYSTEM) != 0) {
2091 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2092 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2093 error = proc_getenvv(curthread, p, &sb);
2094 error2 = sbuf_finish(&sb);
2097 return (error != 0 ? error : error2);
2101 * This sysctl allows a process to retrieve ELF auxiliary vector of
2105 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2107 int *name = (int *)arg1;
2108 u_int namelen = arg2;
2116 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2119 if ((p->p_flag & P_SYSTEM) != 0) {
2123 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2124 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2125 error = proc_getauxv(curthread, p, &sb);
2126 error2 = sbuf_finish(&sb);
2129 return (error != 0 ? error : error2);
2133 * This sysctl allows a process to retrieve the path of the executable for
2134 * itself or another process.
2137 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2139 pid_t *pidp = (pid_t *)arg1;
2140 unsigned int arglen = arg2;
2143 char *retbuf, *freebuf;
2148 if (*pidp == -1) { /* -1 means this process */
2149 p = req->td->td_proc;
2151 error = pget(*pidp, PGET_CANSEE, &p);
2165 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2169 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2170 free(freebuf, M_TEMP);
2175 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2188 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2191 sv_name = p->p_sysent->sv_name;
2193 return (sysctl_handle_string(oidp, sv_name, 0, req));
2196 #ifdef KINFO_OVMENTRY_SIZE
2197 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2200 #ifdef COMPAT_FREEBSD7
2202 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2204 vm_map_entry_t entry, tmp_entry;
2205 unsigned int last_timestamp;
2206 char *fullpath, *freepath;
2207 struct kinfo_ovmentry *kve;
2217 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2220 vm = vmspace_acquire_ref(p);
2225 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2228 vm_map_lock_read(map);
2229 VM_MAP_ENTRY_FOREACH(entry, map) {
2230 vm_object_t obj, tobj, lobj;
2233 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2236 bzero(kve, sizeof(*kve));
2237 kve->kve_structsize = sizeof(*kve);
2239 kve->kve_private_resident = 0;
2240 obj = entry->object.vm_object;
2242 VM_OBJECT_RLOCK(obj);
2243 if (obj->shadow_count == 1)
2244 kve->kve_private_resident =
2245 obj->resident_page_count;
2247 kve->kve_resident = 0;
2248 addr = entry->start;
2249 while (addr < entry->end) {
2250 if (pmap_extract(map->pmap, addr))
2251 kve->kve_resident++;
2255 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2257 VM_OBJECT_RLOCK(tobj);
2258 kve->kve_offset += tobj->backing_object_offset;
2261 VM_OBJECT_RUNLOCK(lobj);
2265 kve->kve_start = (void*)entry->start;
2266 kve->kve_end = (void*)entry->end;
2267 kve->kve_offset += (off_t)entry->offset;
2269 if (entry->protection & VM_PROT_READ)
2270 kve->kve_protection |= KVME_PROT_READ;
2271 if (entry->protection & VM_PROT_WRITE)
2272 kve->kve_protection |= KVME_PROT_WRITE;
2273 if (entry->protection & VM_PROT_EXECUTE)
2274 kve->kve_protection |= KVME_PROT_EXEC;
2276 if (entry->eflags & MAP_ENTRY_COW)
2277 kve->kve_flags |= KVME_FLAG_COW;
2278 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2279 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2280 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2281 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2283 last_timestamp = map->timestamp;
2284 vm_map_unlock_read(map);
2286 kve->kve_fileid = 0;
2291 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2292 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2293 kve->kve_type = KVME_TYPE_UNKNOWN;
2297 VM_OBJECT_RUNLOCK(lobj);
2299 kve->kve_ref_count = obj->ref_count;
2300 kve->kve_shadow_count = obj->shadow_count;
2301 VM_OBJECT_RUNLOCK(obj);
2303 vn_fullpath(curthread, vp, &fullpath,
2305 cred = curthread->td_ucred;
2306 vn_lock(vp, LK_SHARED | LK_RETRY);
2307 if (VOP_GETATTR(vp, &va, cred) == 0) {
2308 kve->kve_fileid = va.va_fileid;
2310 kve->kve_fsid = va.va_fsid;
2315 kve->kve_type = KVME_TYPE_NONE;
2316 kve->kve_ref_count = 0;
2317 kve->kve_shadow_count = 0;
2320 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2321 if (freepath != NULL)
2322 free(freepath, M_TEMP);
2324 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2325 vm_map_lock_read(map);
2328 if (last_timestamp != map->timestamp) {
2329 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2333 vm_map_unlock_read(map);
2339 #endif /* COMPAT_FREEBSD7 */
2341 #ifdef KINFO_VMENTRY_SIZE
2342 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2346 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2347 int *resident_count, bool *super)
2349 vm_object_t obj, tobj;
2353 vm_pindex_t pi, pi_adv, pindex;
2356 *resident_count = 0;
2357 if (vmmap_skip_res_cnt)
2361 obj = entry->object.vm_object;
2362 addr = entry->start;
2364 pi = OFF_TO_IDX(entry->offset);
2365 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2366 if (m_adv != NULL) {
2369 pi_adv = atop(entry->end - addr);
2371 for (tobj = obj;; tobj = tobj->backing_object) {
2372 m = vm_page_find_least(tobj, pindex);
2374 if (m->pindex == pindex)
2376 if (pi_adv > m->pindex - pindex) {
2377 pi_adv = m->pindex - pindex;
2381 if (tobj->backing_object == NULL)
2383 pindex += OFF_TO_IDX(tobj->
2384 backing_object_offset);
2388 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2389 (addr & (pagesizes[1] - 1)) == 0 &&
2390 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2392 pi_adv = atop(pagesizes[1]);
2395 * We do not test the found page on validity.
2396 * Either the page is busy and being paged in,
2397 * or it was invalidated. The first case
2398 * should be counted as resident, the second
2399 * is not so clear; we do account both.
2403 *resident_count += pi_adv;
2409 * Must be called with the process locked and will return unlocked.
2412 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2414 vm_map_entry_t entry, tmp_entry;
2417 vm_object_t obj, tobj, lobj;
2418 char *fullpath, *freepath;
2419 struct kinfo_vmentry *kve;
2424 unsigned int last_timestamp;
2428 PROC_LOCK_ASSERT(p, MA_OWNED);
2432 vm = vmspace_acquire_ref(p);
2437 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2441 vm_map_lock_read(map);
2442 VM_MAP_ENTRY_FOREACH(entry, map) {
2443 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2447 bzero(kve, sizeof(*kve));
2448 obj = entry->object.vm_object;
2450 for (tobj = obj; tobj != NULL;
2451 tobj = tobj->backing_object) {
2452 VM_OBJECT_RLOCK(tobj);
2453 kve->kve_offset += tobj->backing_object_offset;
2456 if (obj->backing_object == NULL)
2457 kve->kve_private_resident =
2458 obj->resident_page_count;
2459 kern_proc_vmmap_resident(map, entry,
2460 &kve->kve_resident, &super);
2462 kve->kve_flags |= KVME_FLAG_SUPER;
2463 for (tobj = obj; tobj != NULL;
2464 tobj = tobj->backing_object) {
2465 if (tobj != obj && tobj != lobj)
2466 VM_OBJECT_RUNLOCK(tobj);
2472 kve->kve_start = entry->start;
2473 kve->kve_end = entry->end;
2474 kve->kve_offset += entry->offset;
2476 if (entry->protection & VM_PROT_READ)
2477 kve->kve_protection |= KVME_PROT_READ;
2478 if (entry->protection & VM_PROT_WRITE)
2479 kve->kve_protection |= KVME_PROT_WRITE;
2480 if (entry->protection & VM_PROT_EXECUTE)
2481 kve->kve_protection |= KVME_PROT_EXEC;
2483 if (entry->eflags & MAP_ENTRY_COW)
2484 kve->kve_flags |= KVME_FLAG_COW;
2485 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2486 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2487 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2488 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2489 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2490 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2491 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2492 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2493 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2494 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2496 last_timestamp = map->timestamp;
2497 vm_map_unlock_read(map);
2502 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2506 VM_OBJECT_RUNLOCK(lobj);
2508 kve->kve_ref_count = obj->ref_count;
2509 kve->kve_shadow_count = obj->shadow_count;
2510 VM_OBJECT_RUNLOCK(obj);
2512 vn_fullpath(curthread, vp, &fullpath,
2514 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2515 cred = curthread->td_ucred;
2516 vn_lock(vp, LK_SHARED | LK_RETRY);
2517 if (VOP_GETATTR(vp, &va, cred) == 0) {
2518 kve->kve_vn_fileid = va.va_fileid;
2519 kve->kve_vn_fsid = va.va_fsid;
2520 kve->kve_vn_fsid_freebsd11 =
2521 kve->kve_vn_fsid; /* truncate */
2523 MAKEIMODE(va.va_type, va.va_mode);
2524 kve->kve_vn_size = va.va_size;
2525 kve->kve_vn_rdev = va.va_rdev;
2526 kve->kve_vn_rdev_freebsd11 =
2527 kve->kve_vn_rdev; /* truncate */
2528 kve->kve_status = KF_ATTR_VALID;
2533 kve->kve_type = KVME_TYPE_NONE;
2534 kve->kve_ref_count = 0;
2535 kve->kve_shadow_count = 0;
2538 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2539 if (freepath != NULL)
2540 free(freepath, M_TEMP);
2542 /* Pack record size down */
2543 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2544 kve->kve_structsize =
2545 offsetof(struct kinfo_vmentry, kve_path) +
2546 strlen(kve->kve_path) + 1;
2548 kve->kve_structsize = sizeof(*kve);
2549 kve->kve_structsize = roundup(kve->kve_structsize,
2552 /* Halt filling and truncate rather than exceeding maxlen */
2553 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2555 vm_map_lock_read(map);
2557 } else if (maxlen != -1)
2558 maxlen -= kve->kve_structsize;
2560 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2562 vm_map_lock_read(map);
2565 if (last_timestamp != map->timestamp) {
2566 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2570 vm_map_unlock_read(map);
2578 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2582 int error, error2, *name;
2585 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2586 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2587 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2592 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2593 error2 = sbuf_finish(&sb);
2595 return (error != 0 ? error : error2);
2598 #if defined(STACK) || defined(DDB)
2600 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2602 struct kinfo_kstack *kkstp;
2603 int error, i, *name, numthreads;
2604 lwpid_t *lwpidarray;
2611 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2615 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2616 st = stack_create(M_WAITOK);
2621 if (lwpidarray != NULL) {
2622 free(lwpidarray, M_TEMP);
2625 numthreads = p->p_numthreads;
2627 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2630 } while (numthreads < p->p_numthreads);
2633 * XXXRW: During the below loop, execve(2) and countless other sorts
2634 * of changes could have taken place. Should we check to see if the
2635 * vmspace has been replaced, or the like, in order to prevent
2636 * giving a snapshot that spans, say, execve(2), with some threads
2637 * before and some after? Among other things, the credentials could
2638 * have changed, in which case the right to extract debug info might
2639 * no longer be assured.
2642 FOREACH_THREAD_IN_PROC(p, td) {
2643 KASSERT(i < numthreads,
2644 ("sysctl_kern_proc_kstack: numthreads"));
2645 lwpidarray[i] = td->td_tid;
2649 for (i = 0; i < numthreads; i++) {
2650 td = thread_find(p, lwpidarray[i]);
2654 bzero(kkstp, sizeof(*kkstp));
2655 (void)sbuf_new(&sb, kkstp->kkst_trace,
2656 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2658 kkstp->kkst_tid = td->td_tid;
2659 if (TD_IS_SWAPPED(td))
2660 kkstp->kkst_state = KKST_STATE_SWAPPED;
2661 else if (stack_save_td(st, td) == 0)
2662 kkstp->kkst_state = KKST_STATE_STACKOK;
2664 kkstp->kkst_state = KKST_STATE_RUNNING;
2667 stack_sbuf_print(&sb, st);
2670 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2677 if (lwpidarray != NULL)
2678 free(lwpidarray, M_TEMP);
2680 free(kkstp, M_TEMP);
2686 * This sysctl allows a process to retrieve the full list of groups from
2687 * itself or another process.
2690 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2692 pid_t *pidp = (pid_t *)arg1;
2693 unsigned int arglen = arg2;
2700 if (*pidp == -1) { /* -1 means this process */
2701 p = req->td->td_proc;
2704 error = pget(*pidp, PGET_CANSEE, &p);
2709 cred = crhold(p->p_ucred);
2712 error = SYSCTL_OUT(req, cred->cr_groups,
2713 cred->cr_ngroups * sizeof(gid_t));
2719 * This sysctl allows a process to retrieve or/and set the resource limit for
2723 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2725 int *name = (int *)arg1;
2726 u_int namelen = arg2;
2735 which = (u_int)name[1];
2736 if (which >= RLIM_NLIMITS)
2739 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2742 flags = PGET_HOLD | PGET_NOTWEXIT;
2743 if (req->newptr != NULL)
2744 flags |= PGET_CANDEBUG;
2746 flags |= PGET_CANSEE;
2747 error = pget((pid_t)name[0], flags, &p);
2754 if (req->oldptr != NULL) {
2756 lim_rlimit_proc(p, which, &rlim);
2759 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2766 if (req->newptr != NULL) {
2767 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2769 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2778 * This sysctl allows a process to retrieve ps_strings structure location of
2782 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2784 int *name = (int *)arg1;
2785 u_int namelen = arg2;
2787 vm_offset_t ps_strings;
2789 #ifdef COMPAT_FREEBSD32
2790 uint32_t ps_strings32;
2796 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2799 #ifdef COMPAT_FREEBSD32
2800 if ((req->flags & SCTL_MASK32) != 0) {
2802 * We return 0 if the 32 bit emulation request is for a 64 bit
2805 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2806 PTROUT(p->p_sysent->sv_psstrings) : 0;
2808 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2812 ps_strings = p->p_sysent->sv_psstrings;
2814 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2819 * This sysctl allows a process to retrieve umask of another process.
2822 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2824 int *name = (int *)arg1;
2825 u_int namelen = arg2;
2834 pid = (pid_t)name[0];
2836 if (pid == p->p_pid || pid == 0) {
2837 fd_cmask = p->p_fd->fd_cmask;
2841 error = pget(pid, PGET_WANTREAD, &p);
2845 fd_cmask = p->p_fd->fd_cmask;
2848 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2853 * This sysctl allows a process to set and retrieve binary osreldate of
2857 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2859 int *name = (int *)arg1;
2860 u_int namelen = arg2;
2862 int flags, error, osrel;
2867 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2870 flags = PGET_HOLD | PGET_NOTWEXIT;
2871 if (req->newptr != NULL)
2872 flags |= PGET_CANDEBUG;
2874 flags |= PGET_CANSEE;
2875 error = pget((pid_t)name[0], flags, &p);
2879 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2883 if (req->newptr != NULL) {
2884 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2899 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2901 int *name = (int *)arg1;
2902 u_int namelen = arg2;
2904 struct kinfo_sigtramp kst;
2905 const struct sysentvec *sv;
2907 #ifdef COMPAT_FREEBSD32
2908 struct kinfo_sigtramp32 kst32;
2914 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2918 #ifdef COMPAT_FREEBSD32
2919 if ((req->flags & SCTL_MASK32) != 0) {
2920 bzero(&kst32, sizeof(kst32));
2921 if (SV_PROC_FLAG(p, SV_ILP32)) {
2922 if (sv->sv_sigcode_base != 0) {
2923 kst32.ksigtramp_start = sv->sv_sigcode_base;
2924 kst32.ksigtramp_end = sv->sv_sigcode_base +
2927 kst32.ksigtramp_start = sv->sv_psstrings -
2929 kst32.ksigtramp_end = sv->sv_psstrings;
2933 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2937 bzero(&kst, sizeof(kst));
2938 if (sv->sv_sigcode_base != 0) {
2939 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2940 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2943 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2945 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2948 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2953 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
2955 int *name = (int *)arg1;
2956 u_int namelen = arg2;
2961 #ifdef COMPAT_FREEBSD32
2966 if (namelen != 1 || req->newptr != NULL)
2969 pid = (pid_t)name[0];
2970 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
2975 #ifdef COMPAT_FREEBSD32
2976 if (SV_CURPROC_FLAG(SV_ILP32)) {
2977 if (!SV_PROC_FLAG(p, SV_ILP32)) {
2983 if (pid <= PID_MAX) {
2984 td1 = FIRST_THREAD_IN_PROC(p);
2986 FOREACH_THREAD_IN_PROC(p, td1) {
2987 if (td1->td_tid == pid)
2996 * The access to the private thread flags. It is fine as far
2997 * as no out-of-thin-air values are read from td_pflags, and
2998 * usermode read of the td_sigblock_ptr is racy inherently,
2999 * since target process might have already changed it
3002 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3003 addr = (uintptr_t)td1->td_sigblock_ptr;
3013 #ifdef COMPAT_FREEBSD32
3014 if (SV_CURPROC_FLAG(SV_ILP32)) {
3016 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3019 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3023 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3026 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3027 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3028 "Return entire process table");
3030 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3031 sysctl_kern_proc, "Process table");
3033 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3034 sysctl_kern_proc, "Process table");
3036 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3037 sysctl_kern_proc, "Process table");
3039 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3040 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3042 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3043 sysctl_kern_proc, "Process table");
3045 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3046 sysctl_kern_proc, "Process table");
3048 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3049 sysctl_kern_proc, "Process table");
3051 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3052 sysctl_kern_proc, "Process table");
3054 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3055 sysctl_kern_proc, "Return process table, no threads");
3057 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3058 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3059 sysctl_kern_proc_args, "Process argument list");
3061 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3062 sysctl_kern_proc_env, "Process environment");
3064 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3065 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3067 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3068 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3070 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3071 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3072 "Process syscall vector name (ABI type)");
3074 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3075 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3077 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3078 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3080 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3081 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3083 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3084 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3086 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3087 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3089 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3090 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3092 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3093 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3095 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3096 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3098 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3099 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3100 "Return process table, no threads");
3102 #ifdef COMPAT_FREEBSD7
3103 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3104 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3107 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3108 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3110 #if defined(STACK) || defined(DDB)
3111 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3112 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3115 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3116 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3118 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3119 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3120 "Process resource limits");
3122 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3123 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3124 "Process ps_strings location");
3126 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3127 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3129 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3130 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3131 "Process binary osreldate");
3133 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3134 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3135 "Process signal trampoline location");
3137 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3138 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3139 "Thread sigfastblock address");
3144 * stop_all_proc() purpose is to stop all process which have usermode,
3145 * except current process for obvious reasons. This makes it somewhat
3146 * unreliable when invoked from multithreaded process. The service
3147 * must not be user-callable anyway.
3152 struct proc *cp, *p;
3154 bool restart, seen_stopped, seen_exiting, stopped_some;
3158 sx_xlock(&allproc_lock);
3160 seen_exiting = seen_stopped = stopped_some = restart = false;
3161 LIST_REMOVE(cp, p_list);
3162 LIST_INSERT_HEAD(&allproc, cp, p_list);
3164 p = LIST_NEXT(cp, p_list);
3167 LIST_REMOVE(cp, p_list);
3168 LIST_INSERT_AFTER(p, cp, p_list);
3170 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3174 if ((p->p_flag & P_WEXIT) != 0) {
3175 seen_exiting = true;
3179 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3181 * Stopped processes are tolerated when there
3182 * are no other processes which might continue
3183 * them. P_STOPPED_SINGLE but not
3184 * P_TOTAL_STOP process still has at least one
3187 seen_stopped = true;
3191 sx_xunlock(&allproc_lock);
3193 r = thread_single(p, SINGLE_ALLPROC);
3197 stopped_some = true;
3200 sx_xlock(&allproc_lock);
3202 /* Catch forked children we did not see in iteration. */
3203 if (gen != allproc_gen)
3205 sx_xunlock(&allproc_lock);
3206 if (restart || stopped_some || seen_exiting || seen_stopped) {
3207 kern_yield(PRI_USER);
3213 resume_all_proc(void)
3215 struct proc *cp, *p;
3218 sx_xlock(&allproc_lock);
3220 LIST_REMOVE(cp, p_list);
3221 LIST_INSERT_HEAD(&allproc, cp, p_list);
3223 p = LIST_NEXT(cp, p_list);
3226 LIST_REMOVE(cp, p_list);
3227 LIST_INSERT_AFTER(p, cp, p_list);
3229 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3230 sx_xunlock(&allproc_lock);
3232 thread_single_end(p, SINGLE_ALLPROC);
3235 sx_xlock(&allproc_lock);
3240 /* Did the loop above missed any stopped process ? */
3241 FOREACH_PROC_IN_SYSTEM(p) {
3242 /* No need for proc lock. */
3243 if ((p->p_flag & P_TOTAL_STOP) != 0)
3246 sx_xunlock(&allproc_lock);
3249 /* #define TOTAL_STOP_DEBUG 1 */
3250 #ifdef TOTAL_STOP_DEBUG
3251 volatile static int ap_resume;
3252 #include <sys/mount.h>
3255 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3261 error = sysctl_handle_int(oidp, &val, 0, req);
3262 if (error != 0 || req->newptr == NULL)
3267 while (ap_resume == 0)
3275 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3276 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3277 sysctl_debug_stop_all_proc, "I",