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 #ifdef COMPAT_FREEBSD32
92 #include <compat/freebsd32/freebsd32.h>
93 #include <compat/freebsd32/freebsd32_util.h>
96 SDT_PROVIDER_DEFINE(proc);
98 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
99 MALLOC_DEFINE(M_SESSION, "session", "session header");
100 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
101 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
103 static void doenterpgrp(struct proc *, struct pgrp *);
104 static void orphanpg(struct pgrp *pg);
105 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
106 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
107 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
109 static void pgadjustjobc(struct pgrp *pgrp, int entering);
110 static void pgdelete(struct pgrp *);
111 static int proc_ctor(void *mem, int size, void *arg, int flags);
112 static void proc_dtor(void *mem, int size, void *arg);
113 static int proc_init(void *mem, int size, int flags);
114 static void proc_fini(void *mem, int size);
115 static void pargs_free(struct pargs *pa);
118 * Other process lists
120 struct pidhashhead *pidhashtbl;
121 struct sx *pidhashtbl_lock;
124 struct pgrphashhead *pgrphashtbl;
126 struct proclist allproc;
127 struct sx __exclusive_cache_line allproc_lock;
128 struct sx __exclusive_cache_line proctree_lock;
129 struct mtx __exclusive_cache_line ppeers_lock;
130 struct mtx __exclusive_cache_line procid_lock;
131 uma_zone_t proc_zone;
134 * The offset of various fields in struct proc and struct thread.
135 * These are used by kernel debuggers to enumerate kernel threads and
138 const int proc_off_p_pid = offsetof(struct proc, p_pid);
139 const int proc_off_p_comm = offsetof(struct proc, p_comm);
140 const int proc_off_p_list = offsetof(struct proc, p_list);
141 const int proc_off_p_threads = offsetof(struct proc, p_threads);
142 const int thread_off_td_tid = offsetof(struct thread, td_tid);
143 const int thread_off_td_name = offsetof(struct thread, td_name);
144 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
145 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
146 const int thread_off_td_plist = offsetof(struct thread, td_plist);
148 EVENTHANDLER_LIST_DEFINE(process_ctor);
149 EVENTHANDLER_LIST_DEFINE(process_dtor);
150 EVENTHANDLER_LIST_DEFINE(process_init);
151 EVENTHANDLER_LIST_DEFINE(process_fini);
152 EVENTHANDLER_LIST_DEFINE(process_exit);
153 EVENTHANDLER_LIST_DEFINE(process_fork);
154 EVENTHANDLER_LIST_DEFINE(process_exec);
156 int kstack_pages = KSTACK_PAGES;
157 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
158 "Kernel stack size in pages");
159 static int vmmap_skip_res_cnt = 0;
160 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
161 &vmmap_skip_res_cnt, 0,
162 "Skip calculation of the pages resident count in kern.proc.vmmap");
164 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
165 #ifdef COMPAT_FREEBSD32
166 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
170 * Initialize global process hashing structures.
177 sx_init(&allproc_lock, "allproc");
178 sx_init(&proctree_lock, "proctree");
179 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
180 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
182 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
183 pidhashlock = (pidhash + 1) / 64;
186 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
187 M_PROC, M_WAITOK | M_ZERO);
188 for (i = 0; i < pidhashlock + 1; i++)
189 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
190 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
191 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
192 proc_ctor, proc_dtor, proc_init, proc_fini,
193 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
198 * Prepare a proc for use.
201 proc_ctor(void *mem, int size, void *arg, int flags)
206 p = (struct proc *)mem;
207 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
208 td = FIRST_THREAD_IN_PROC(p);
210 /* Make sure all thread constructors are executed */
211 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
217 * Reclaim a proc after use.
220 proc_dtor(void *mem, int size, void *arg)
225 /* INVARIANTS checks go here */
226 p = (struct proc *)mem;
227 td = FIRST_THREAD_IN_PROC(p);
230 KASSERT((p->p_numthreads == 1),
231 ("bad number of threads in exiting process"));
232 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
234 /* Free all OSD associated to this thread. */
236 td_softdep_cleanup(td);
237 MPASS(td->td_su == NULL);
239 /* Make sure all thread destructors are executed */
240 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
242 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
243 if (p->p_ksi != NULL)
244 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
248 * Initialize type-stable parts of a proc (when newly created).
251 proc_init(void *mem, int size, int flags)
255 p = (struct proc *)mem;
256 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
257 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
258 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
259 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
260 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
261 cv_init(&p->p_pwait, "ppwait");
262 TAILQ_INIT(&p->p_threads); /* all threads in proc */
263 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
264 p->p_stats = pstats_alloc();
270 * UMA should ensure that this function is never called.
271 * Freeing a proc structure would violate type stability.
274 proc_fini(void *mem, int size)
279 p = (struct proc *)mem;
280 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
281 pstats_free(p->p_stats);
282 thread_free(FIRST_THREAD_IN_PROC(p));
283 mtx_destroy(&p->p_mtx);
284 if (p->p_ksi != NULL)
285 ksiginfo_free(p->p_ksi);
287 panic("proc reclaimed");
292 * PID space management.
294 * These bitmaps are used by fork_findpid.
296 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
297 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
298 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
299 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
301 static bitstr_t *proc_id_array[] = {
309 proc_id_set(int type, pid_t id)
312 KASSERT(type >= 0 && type < nitems(proc_id_array),
313 ("invalid type %d\n", type));
314 mtx_lock(&procid_lock);
315 KASSERT(bit_test(proc_id_array[type], id) == 0,
316 ("bit %d already set in %d\n", id, type));
317 bit_set(proc_id_array[type], id);
318 mtx_unlock(&procid_lock);
322 proc_id_set_cond(int type, pid_t id)
325 KASSERT(type >= 0 && type < nitems(proc_id_array),
326 ("invalid type %d\n", type));
327 if (bit_test(proc_id_array[type], id))
329 mtx_lock(&procid_lock);
330 bit_set(proc_id_array[type], id);
331 mtx_unlock(&procid_lock);
335 proc_id_clear(int type, pid_t id)
338 KASSERT(type >= 0 && type < nitems(proc_id_array),
339 ("invalid type %d\n", type));
340 mtx_lock(&procid_lock);
341 KASSERT(bit_test(proc_id_array[type], id) != 0,
342 ("bit %d not set in %d\n", id, type));
343 bit_clear(proc_id_array[type], id);
344 mtx_unlock(&procid_lock);
348 * Is p an inferior of the current process?
351 inferior(struct proc *p)
354 sx_assert(&proctree_lock, SX_LOCKED);
355 PROC_LOCK_ASSERT(p, MA_OWNED);
356 for (; p != curproc; p = proc_realparent(p)) {
364 * Shared lock all the pid hash lists.
367 pidhash_slockall(void)
371 for (i = 0; i < pidhashlock + 1; i++)
372 sx_slock(&pidhashtbl_lock[i]);
376 * Shared unlock all the pid hash lists.
379 pidhash_sunlockall(void)
383 for (i = 0; i < pidhashlock + 1; i++)
384 sx_sunlock(&pidhashtbl_lock[i]);
388 * Similar to pfind_any(), this function finds zombies.
391 pfind_any_locked(pid_t pid)
395 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
396 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
397 if (p->p_pid == pid) {
399 if (p->p_state == PRS_NEW) {
410 * Locate a process by number.
412 * By not returning processes in the PRS_NEW state, we allow callers to avoid
413 * testing for that condition to avoid dereferencing p_ucred, et al.
415 static __always_inline struct proc *
416 _pfind(pid_t pid, bool zombie)
421 if (p->p_pid == pid) {
425 sx_slock(PIDHASHLOCK(pid));
426 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
427 if (p->p_pid == pid) {
429 if (p->p_state == PRS_NEW ||
430 (!zombie && p->p_state == PRS_ZOMBIE)) {
437 sx_sunlock(PIDHASHLOCK(pid));
445 return (_pfind(pid, false));
449 * Same as pfind but allow zombies.
455 return (_pfind(pid, true));
459 * Locate a process group by number.
460 * The caller must hold proctree_lock.
467 sx_assert(&proctree_lock, SX_LOCKED);
469 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
470 if (pgrp->pg_id == pgid) {
479 * Locate process and do additional manipulations, depending on flags.
482 pget(pid_t pid, int flags, struct proc **pp)
489 if (p->p_pid == pid) {
493 if (pid <= PID_MAX) {
494 if ((flags & PGET_NOTWEXIT) == 0)
498 } else if ((flags & PGET_NOTID) == 0) {
499 td1 = tdfind(pid, -1);
505 if ((flags & PGET_CANSEE) != 0) {
506 error = p_cansee(curthread, p);
511 if ((flags & PGET_CANDEBUG) != 0) {
512 error = p_candebug(curthread, p);
516 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
520 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
524 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
526 * XXXRW: Not clear ESRCH is the right error during proc
532 if ((flags & PGET_HOLD) != 0) {
544 * Create a new process group.
545 * pgid must be equal to the pid of p.
546 * Begin a new session if required.
549 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
552 sx_assert(&proctree_lock, SX_XLOCKED);
554 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
555 KASSERT(p->p_pid == pgid,
556 ("enterpgrp: new pgrp and pid != pgid"));
557 KASSERT(pgfind(pgid) == NULL,
558 ("enterpgrp: pgrp with pgid exists"));
559 KASSERT(!SESS_LEADER(p),
560 ("enterpgrp: session leader attempted setpgrp"));
562 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
568 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
570 p->p_flag &= ~P_CONTROLT;
574 sess->s_sid = p->p_pid;
575 proc_id_set(PROC_ID_SESSION, p->p_pid);
576 refcount_init(&sess->s_count, 1);
577 sess->s_ttyvp = NULL;
578 sess->s_ttydp = NULL;
580 bcopy(p->p_session->s_login, sess->s_login,
581 sizeof(sess->s_login));
582 pgrp->pg_session = sess;
583 KASSERT(p == curproc,
584 ("enterpgrp: mksession and p != curproc"));
586 pgrp->pg_session = p->p_session;
587 sess_hold(pgrp->pg_session);
591 proc_id_set(PROC_ID_GROUP, p->p_pid);
592 LIST_INIT(&pgrp->pg_members);
595 * As we have an exclusive lock of proctree_lock,
596 * this should not deadlock.
598 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
600 SLIST_INIT(&pgrp->pg_sigiolst);
603 doenterpgrp(p, pgrp);
609 * Move p to an existing process group
612 enterthispgrp(struct proc *p, struct pgrp *pgrp)
615 sx_assert(&proctree_lock, SX_XLOCKED);
616 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
617 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
618 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
619 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
620 KASSERT(pgrp->pg_session == p->p_session,
621 ("%s: pgrp's session %p, p->p_session %p.\n",
625 KASSERT(pgrp != p->p_pgrp,
626 ("%s: p belongs to pgrp.", __func__));
628 doenterpgrp(p, pgrp);
634 * Move p to a process group
637 doenterpgrp(struct proc *p, struct pgrp *pgrp)
639 struct pgrp *savepgrp;
641 sx_assert(&proctree_lock, SX_XLOCKED);
642 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
643 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
644 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
645 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
647 savepgrp = p->p_pgrp;
650 * Adjust eligibility of affected pgrps to participate in job control.
651 * Increment eligibility counts before decrementing, otherwise we
652 * could reach 0 spuriously during the first call.
655 fixjobc(p, p->p_pgrp, 0);
660 LIST_REMOVE(p, p_pglist);
663 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
664 PGRP_UNLOCK(savepgrp);
666 if (LIST_EMPTY(&savepgrp->pg_members))
671 * remove process from process group
674 leavepgrp(struct proc *p)
676 struct pgrp *savepgrp;
678 sx_assert(&proctree_lock, SX_XLOCKED);
679 savepgrp = p->p_pgrp;
682 LIST_REMOVE(p, p_pglist);
685 PGRP_UNLOCK(savepgrp);
686 if (LIST_EMPTY(&savepgrp->pg_members))
692 * delete a process group
695 pgdelete(struct pgrp *pgrp)
697 struct session *savesess;
700 sx_assert(&proctree_lock, SX_XLOCKED);
701 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
702 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
705 * Reset any sigio structures pointing to us as a result of
706 * F_SETOWN with our pgid.
708 funsetownlst(&pgrp->pg_sigiolst);
711 tp = pgrp->pg_session->s_ttyp;
712 LIST_REMOVE(pgrp, pg_hash);
713 savesess = pgrp->pg_session;
716 /* Remove the reference to the pgrp before deallocating it. */
719 tty_rel_pgrp(tp, pgrp);
722 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
723 mtx_destroy(&pgrp->pg_mtx);
725 sess_release(savesess);
729 pgadjustjobc(struct pgrp *pgrp, int entering)
735 MPASS(pgrp->pg_jobc >= 0);
740 MPASS(pgrp->pg_jobc > 0);
743 if (pgrp->pg_jobc == 0)
750 * Adjust pgrp jobc counters when specified process changes process group.
751 * We count the number of processes in each process group that "qualify"
752 * the group for terminal job control (those with a parent in a different
753 * process group of the same session). If that count reaches zero, the
754 * process group becomes orphaned. Check both the specified process'
755 * process group and that of its children.
756 * entering == 0 => p is leaving specified group.
757 * entering == 1 => p is entering specified group.
760 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
762 struct pgrp *hispgrp;
763 struct session *mysession;
766 sx_assert(&proctree_lock, SX_LOCKED);
767 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
768 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
769 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
772 * Check p's parent to see whether p qualifies its own process
773 * group; if so, adjust count for p's process group.
775 mysession = pgrp->pg_session;
776 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
777 hispgrp->pg_session == mysession)
778 pgadjustjobc(pgrp, entering);
781 * Check this process' children to see whether they qualify
782 * their process groups; if so, adjust counts for children's
785 LIST_FOREACH(q, &p->p_children, p_sibling) {
787 if (hispgrp == pgrp ||
788 hispgrp->pg_session != mysession)
790 if (q->p_state == PRS_ZOMBIE)
792 pgadjustjobc(hispgrp, entering);
805 MPASS(p->p_flag & P_WEXIT);
807 * Do a quick check to see if there is anything to do with the
808 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
811 if (!SESS_LEADER(p) &&
812 (p->p_pgrp == p->p_pptr->p_pgrp) &&
813 LIST_EMPTY(&p->p_children)) {
819 sx_xlock(&proctree_lock);
820 if (SESS_LEADER(p)) {
824 * s_ttyp is not zero'd; we use this to indicate that
825 * the session once had a controlling terminal. (for
826 * logging and informational purposes)
837 * Signal foreground pgrp and revoke access to
838 * controlling terminal if it has not been revoked
841 * Because the TTY may have been revoked in the mean
842 * time and could already have a new session associated
843 * with it, make sure we don't send a SIGHUP to a
844 * foreground process group that does not belong to this
850 if (tp->t_session == sp)
851 tty_signal_pgrp(tp, SIGHUP);
856 sx_xunlock(&proctree_lock);
857 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
858 VOP_REVOKE(ttyvp, REVOKEALL);
862 sx_xlock(&proctree_lock);
865 fixjobc(p, p->p_pgrp, 0);
866 sx_xunlock(&proctree_lock);
870 * A process group has become orphaned;
871 * if there are any stopped processes in the group,
872 * hang-up all process in that group.
875 orphanpg(struct pgrp *pg)
879 PGRP_LOCK_ASSERT(pg, MA_OWNED);
881 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
883 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
885 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
887 kern_psignal(p, SIGHUP);
888 kern_psignal(p, SIGCONT);
898 sess_hold(struct session *s)
901 refcount_acquire(&s->s_count);
905 sess_release(struct session *s)
908 if (refcount_release(&s->s_count)) {
909 if (s->s_ttyp != NULL) {
911 tty_rel_sess(s->s_ttyp, s);
913 proc_id_clear(PROC_ID_SESSION, s->s_sid);
914 mtx_destroy(&s->s_mtx);
921 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
927 for (i = 0; i <= pgrphash; i++) {
928 if (!LIST_EMPTY(&pgrphashtbl[i])) {
929 printf("\tindx %d\n", i);
930 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
932 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
933 (void *)pgrp, (long)pgrp->pg_id,
934 (void *)pgrp->pg_session,
935 pgrp->pg_session->s_count,
936 (void *)LIST_FIRST(&pgrp->pg_members));
937 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
938 printf("\t\tpid %ld addr %p pgrp %p\n",
939 (long)p->p_pid, (void *)p,
949 * Calculate the kinfo_proc members which contain process-wide
951 * Must be called with the target process locked.
954 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
958 PROC_LOCK_ASSERT(p, MA_OWNED);
962 FOREACH_THREAD_IN_PROC(p, td) {
964 kp->ki_pctcpu += sched_pctcpu(td);
965 kp->ki_estcpu += sched_estcpu(td);
971 * Clear kinfo_proc and fill in any information that is common
972 * to all threads in the process.
973 * Must be called with the target process locked.
976 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
983 struct timeval boottime;
985 PROC_LOCK_ASSERT(p, MA_OWNED);
986 bzero(kp, sizeof(*kp));
988 kp->ki_structsize = sizeof(*kp);
990 kp->ki_addr =/* p->p_addr; */0; /* XXX */
991 kp->ki_args = p->p_args;
992 kp->ki_textvp = p->p_textvp;
994 kp->ki_tracep = p->p_tracevp;
995 kp->ki_traceflag = p->p_traceflag;
998 kp->ki_vmspace = p->p_vmspace;
999 kp->ki_flag = p->p_flag;
1000 kp->ki_flag2 = p->p_flag2;
1003 kp->ki_uid = cred->cr_uid;
1004 kp->ki_ruid = cred->cr_ruid;
1005 kp->ki_svuid = cred->cr_svuid;
1006 kp->ki_cr_flags = 0;
1007 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1008 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1009 /* XXX bde doesn't like KI_NGROUPS */
1010 if (cred->cr_ngroups > KI_NGROUPS) {
1011 kp->ki_ngroups = KI_NGROUPS;
1012 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1014 kp->ki_ngroups = cred->cr_ngroups;
1015 bcopy(cred->cr_groups, kp->ki_groups,
1016 kp->ki_ngroups * sizeof(gid_t));
1017 kp->ki_rgid = cred->cr_rgid;
1018 kp->ki_svgid = cred->cr_svgid;
1019 /* If jailed(cred), emulate the old P_JAILED flag. */
1021 kp->ki_flag |= P_JAILED;
1022 /* If inside the jail, use 0 as a jail ID. */
1023 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1024 kp->ki_jid = cred->cr_prison->pr_id;
1026 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1027 sizeof(kp->ki_loginclass));
1031 mtx_lock(&ps->ps_mtx);
1032 kp->ki_sigignore = ps->ps_sigignore;
1033 kp->ki_sigcatch = ps->ps_sigcatch;
1034 mtx_unlock(&ps->ps_mtx);
1036 if (p->p_state != PRS_NEW &&
1037 p->p_state != PRS_ZOMBIE &&
1038 p->p_vmspace != NULL) {
1039 struct vmspace *vm = p->p_vmspace;
1041 kp->ki_size = vm->vm_map.size;
1042 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1043 FOREACH_THREAD_IN_PROC(p, td0) {
1044 if (!TD_IS_SWAPPED(td0))
1045 kp->ki_rssize += td0->td_kstack_pages;
1047 kp->ki_swrss = vm->vm_swrss;
1048 kp->ki_tsize = vm->vm_tsize;
1049 kp->ki_dsize = vm->vm_dsize;
1050 kp->ki_ssize = vm->vm_ssize;
1051 } else if (p->p_state == PRS_ZOMBIE)
1052 kp->ki_stat = SZOMB;
1053 if (kp->ki_flag & P_INMEM)
1054 kp->ki_sflag = PS_INMEM;
1057 /* Calculate legacy swtime as seconds since 'swtick'. */
1058 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1059 kp->ki_pid = p->p_pid;
1060 kp->ki_nice = p->p_nice;
1061 kp->ki_fibnum = p->p_fibnum;
1062 kp->ki_start = p->p_stats->p_start;
1063 getboottime(&boottime);
1064 timevaladd(&kp->ki_start, &boottime);
1066 rufetch(p, &kp->ki_rusage);
1067 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1068 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1070 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1071 /* Some callers want child times in a single value. */
1072 kp->ki_childtime = kp->ki_childstime;
1073 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1075 FOREACH_THREAD_IN_PROC(p, td0)
1076 kp->ki_cow += td0->td_cow;
1080 kp->ki_pgid = p->p_pgrp->pg_id;
1081 kp->ki_jobc = p->p_pgrp->pg_jobc;
1082 sp = p->p_pgrp->pg_session;
1085 kp->ki_sid = sp->s_sid;
1087 strlcpy(kp->ki_login, sp->s_login,
1088 sizeof(kp->ki_login));
1090 kp->ki_kiflag |= KI_CTTY;
1092 kp->ki_kiflag |= KI_SLEADER;
1093 /* XXX proctree_lock */
1098 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1099 kp->ki_tdev = tty_udev(tp);
1100 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1101 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1103 kp->ki_tsid = tp->t_session->s_sid;
1105 kp->ki_tdev = NODEV;
1106 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1108 if (p->p_comm[0] != '\0')
1109 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1110 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1111 p->p_sysent->sv_name[0] != '\0')
1112 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1113 kp->ki_siglist = p->p_siglist;
1114 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1115 kp->ki_acflag = p->p_acflag;
1116 kp->ki_lock = p->p_lock;
1118 kp->ki_ppid = p->p_oppid;
1119 if (p->p_flag & P_TRACED)
1120 kp->ki_tracer = p->p_pptr->p_pid;
1125 * Fill in information that is thread specific. Must be called with
1126 * target process locked. If 'preferthread' is set, overwrite certain
1127 * process-related fields that are maintained for both threads and
1131 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1137 PROC_LOCK_ASSERT(p, MA_OWNED);
1142 if (td->td_wmesg != NULL)
1143 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1145 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1146 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1147 sizeof(kp->ki_tdname)) {
1148 strlcpy(kp->ki_moretdname,
1149 td->td_name + sizeof(kp->ki_tdname) - 1,
1150 sizeof(kp->ki_moretdname));
1152 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1154 if (TD_ON_LOCK(td)) {
1155 kp->ki_kiflag |= KI_LOCKBLOCK;
1156 strlcpy(kp->ki_lockname, td->td_lockname,
1157 sizeof(kp->ki_lockname));
1159 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1160 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1163 if (p->p_state == PRS_NORMAL) { /* approximate. */
1164 if (TD_ON_RUNQ(td) ||
1166 TD_IS_RUNNING(td)) {
1168 } else if (P_SHOULDSTOP(p)) {
1169 kp->ki_stat = SSTOP;
1170 } else if (TD_IS_SLEEPING(td)) {
1171 kp->ki_stat = SSLEEP;
1172 } else if (TD_ON_LOCK(td)) {
1173 kp->ki_stat = SLOCK;
1175 kp->ki_stat = SWAIT;
1177 } else if (p->p_state == PRS_ZOMBIE) {
1178 kp->ki_stat = SZOMB;
1183 /* Things in the thread */
1184 kp->ki_wchan = td->td_wchan;
1185 kp->ki_pri.pri_level = td->td_priority;
1186 kp->ki_pri.pri_native = td->td_base_pri;
1189 * Note: legacy fields; clamp at the old NOCPU value and/or
1190 * the maximum u_char CPU value.
1192 if (td->td_lastcpu == NOCPU)
1193 kp->ki_lastcpu_old = NOCPU_OLD;
1194 else if (td->td_lastcpu > MAXCPU_OLD)
1195 kp->ki_lastcpu_old = MAXCPU_OLD;
1197 kp->ki_lastcpu_old = td->td_lastcpu;
1199 if (td->td_oncpu == NOCPU)
1200 kp->ki_oncpu_old = NOCPU_OLD;
1201 else if (td->td_oncpu > MAXCPU_OLD)
1202 kp->ki_oncpu_old = MAXCPU_OLD;
1204 kp->ki_oncpu_old = td->td_oncpu;
1206 kp->ki_lastcpu = td->td_lastcpu;
1207 kp->ki_oncpu = td->td_oncpu;
1208 kp->ki_tdflags = td->td_flags;
1209 kp->ki_tid = td->td_tid;
1210 kp->ki_numthreads = p->p_numthreads;
1211 kp->ki_pcb = td->td_pcb;
1212 kp->ki_kstack = (void *)td->td_kstack;
1213 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1214 kp->ki_pri.pri_class = td->td_pri_class;
1215 kp->ki_pri.pri_user = td->td_user_pri;
1218 rufetchtd(td, &kp->ki_rusage);
1219 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1220 kp->ki_pctcpu = sched_pctcpu(td);
1221 kp->ki_estcpu = sched_estcpu(td);
1222 kp->ki_cow = td->td_cow;
1225 /* We can't get this anymore but ps etc never used it anyway. */
1229 kp->ki_siglist = td->td_siglist;
1230 kp->ki_sigmask = td->td_sigmask;
1237 * Fill in a kinfo_proc structure for the specified process.
1238 * Must be called with the target process locked.
1241 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1244 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1246 fill_kinfo_proc_only(p, kp);
1247 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1248 fill_kinfo_aggregate(p, kp);
1255 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1259 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1262 pstats_fork(struct pstats *src, struct pstats *dst)
1265 bzero(&dst->pstat_startzero,
1266 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1267 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1268 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1272 pstats_free(struct pstats *ps)
1275 free(ps, M_SUBPROC);
1278 #ifdef COMPAT_FREEBSD32
1281 * This function is typically used to copy out the kernel address, so
1282 * it can be replaced by assignment of zero.
1284 static inline uint32_t
1285 ptr32_trim(const void *ptr)
1289 uptr = (uintptr_t)ptr;
1290 return ((uptr > UINT_MAX) ? 0 : uptr);
1293 #define PTRTRIM_CP(src,dst,fld) \
1294 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1297 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1301 bzero(ki32, sizeof(struct kinfo_proc32));
1302 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1303 CP(*ki, *ki32, ki_layout);
1304 PTRTRIM_CP(*ki, *ki32, ki_args);
1305 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1306 PTRTRIM_CP(*ki, *ki32, ki_addr);
1307 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1308 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1309 PTRTRIM_CP(*ki, *ki32, ki_fd);
1310 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1311 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1312 CP(*ki, *ki32, ki_pid);
1313 CP(*ki, *ki32, ki_ppid);
1314 CP(*ki, *ki32, ki_pgid);
1315 CP(*ki, *ki32, ki_tpgid);
1316 CP(*ki, *ki32, ki_sid);
1317 CP(*ki, *ki32, ki_tsid);
1318 CP(*ki, *ki32, ki_jobc);
1319 CP(*ki, *ki32, ki_tdev);
1320 CP(*ki, *ki32, ki_tdev_freebsd11);
1321 CP(*ki, *ki32, ki_siglist);
1322 CP(*ki, *ki32, ki_sigmask);
1323 CP(*ki, *ki32, ki_sigignore);
1324 CP(*ki, *ki32, ki_sigcatch);
1325 CP(*ki, *ki32, ki_uid);
1326 CP(*ki, *ki32, ki_ruid);
1327 CP(*ki, *ki32, ki_svuid);
1328 CP(*ki, *ki32, ki_rgid);
1329 CP(*ki, *ki32, ki_svgid);
1330 CP(*ki, *ki32, ki_ngroups);
1331 for (i = 0; i < KI_NGROUPS; i++)
1332 CP(*ki, *ki32, ki_groups[i]);
1333 CP(*ki, *ki32, ki_size);
1334 CP(*ki, *ki32, ki_rssize);
1335 CP(*ki, *ki32, ki_swrss);
1336 CP(*ki, *ki32, ki_tsize);
1337 CP(*ki, *ki32, ki_dsize);
1338 CP(*ki, *ki32, ki_ssize);
1339 CP(*ki, *ki32, ki_xstat);
1340 CP(*ki, *ki32, ki_acflag);
1341 CP(*ki, *ki32, ki_pctcpu);
1342 CP(*ki, *ki32, ki_estcpu);
1343 CP(*ki, *ki32, ki_slptime);
1344 CP(*ki, *ki32, ki_swtime);
1345 CP(*ki, *ki32, ki_cow);
1346 CP(*ki, *ki32, ki_runtime);
1347 TV_CP(*ki, *ki32, ki_start);
1348 TV_CP(*ki, *ki32, ki_childtime);
1349 CP(*ki, *ki32, ki_flag);
1350 CP(*ki, *ki32, ki_kiflag);
1351 CP(*ki, *ki32, ki_traceflag);
1352 CP(*ki, *ki32, ki_stat);
1353 CP(*ki, *ki32, ki_nice);
1354 CP(*ki, *ki32, ki_lock);
1355 CP(*ki, *ki32, ki_rqindex);
1356 CP(*ki, *ki32, ki_oncpu);
1357 CP(*ki, *ki32, ki_lastcpu);
1359 /* XXX TODO: wrap cpu value as appropriate */
1360 CP(*ki, *ki32, ki_oncpu_old);
1361 CP(*ki, *ki32, ki_lastcpu_old);
1363 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1364 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1365 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1366 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1367 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1368 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1369 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1370 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1371 CP(*ki, *ki32, ki_tracer);
1372 CP(*ki, *ki32, ki_flag2);
1373 CP(*ki, *ki32, ki_fibnum);
1374 CP(*ki, *ki32, ki_cr_flags);
1375 CP(*ki, *ki32, ki_jid);
1376 CP(*ki, *ki32, ki_numthreads);
1377 CP(*ki, *ki32, ki_tid);
1378 CP(*ki, *ki32, ki_pri);
1379 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1380 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1381 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1382 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1383 PTRTRIM_CP(*ki, *ki32, ki_udata);
1384 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1385 CP(*ki, *ki32, ki_sflag);
1386 CP(*ki, *ki32, ki_tdflags);
1391 kern_proc_out_size(struct proc *p, int flags)
1395 PROC_LOCK_ASSERT(p, MA_OWNED);
1397 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1398 #ifdef COMPAT_FREEBSD32
1399 if ((flags & KERN_PROC_MASK32) != 0) {
1400 size += sizeof(struct kinfo_proc32);
1403 size += sizeof(struct kinfo_proc);
1405 #ifdef COMPAT_FREEBSD32
1406 if ((flags & KERN_PROC_MASK32) != 0)
1407 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1410 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1417 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1420 struct kinfo_proc ki;
1421 #ifdef COMPAT_FREEBSD32
1422 struct kinfo_proc32 ki32;
1426 PROC_LOCK_ASSERT(p, MA_OWNED);
1427 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1430 fill_kinfo_proc(p, &ki);
1431 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1432 #ifdef COMPAT_FREEBSD32
1433 if ((flags & KERN_PROC_MASK32) != 0) {
1434 freebsd32_kinfo_proc_out(&ki, &ki32);
1435 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1439 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1442 FOREACH_THREAD_IN_PROC(p, td) {
1443 fill_kinfo_thread(td, &ki, 1);
1444 #ifdef COMPAT_FREEBSD32
1445 if ((flags & KERN_PROC_MASK32) != 0) {
1446 freebsd32_kinfo_proc_out(&ki, &ki32);
1447 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1451 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1462 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1465 struct kinfo_proc ki;
1468 if (req->oldptr == NULL)
1469 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1471 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1472 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1473 error = kern_proc_out(p, &sb, flags);
1474 error2 = sbuf_finish(&sb);
1478 else if (error2 != 0)
1484 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1489 for (i = 0; i < pidhashlock + 1; i++) {
1490 sx_slock(&pidhashtbl_lock[i]);
1491 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1492 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1493 if (p->p_state == PRS_NEW)
1495 error = cb(p, cbarg);
1496 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1498 sx_sunlock(&pidhashtbl_lock[i]);
1503 sx_sunlock(&pidhashtbl_lock[i]);
1508 struct kern_proc_out_args {
1509 struct sysctl_req *req;
1516 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1518 struct kern_proc_out_args *arg = origarg;
1519 int *name = arg->name;
1520 int oid_number = arg->oid_number;
1521 int flags = arg->flags;
1522 struct sysctl_req *req = arg->req;
1527 KASSERT(p->p_ucred != NULL,
1528 ("process credential is NULL for non-NEW proc"));
1530 * Show a user only appropriate processes.
1532 if (p_cansee(curthread, p))
1535 * TODO - make more efficient (see notes below).
1538 switch (oid_number) {
1541 if (p->p_ucred->cr_gid != (gid_t)name[0])
1545 case KERN_PROC_PGRP:
1546 /* could do this by traversing pgrp */
1547 if (p->p_pgrp == NULL ||
1548 p->p_pgrp->pg_id != (pid_t)name[0])
1552 case KERN_PROC_RGID:
1553 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1557 case KERN_PROC_SESSION:
1558 if (p->p_session == NULL ||
1559 p->p_session->s_sid != (pid_t)name[0])
1564 if ((p->p_flag & P_CONTROLT) == 0 ||
1565 p->p_session == NULL)
1567 /* XXX proctree_lock */
1568 SESS_LOCK(p->p_session);
1569 if (p->p_session->s_ttyp == NULL ||
1570 tty_udev(p->p_session->s_ttyp) !=
1572 SESS_UNLOCK(p->p_session);
1575 SESS_UNLOCK(p->p_session);
1579 if (p->p_ucred->cr_uid != (uid_t)name[0])
1583 case KERN_PROC_RUID:
1584 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1588 case KERN_PROC_PROC:
1595 error = sysctl_out_proc(p, req, flags);
1596 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1604 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1606 struct kern_proc_out_args iterarg;
1607 int *name = (int *)arg1;
1608 u_int namelen = arg2;
1610 int flags, oid_number;
1613 oid_number = oidp->oid_number;
1614 if (oid_number != KERN_PROC_ALL &&
1615 (oid_number & KERN_PROC_INC_THREAD) == 0)
1616 flags = KERN_PROC_NOTHREADS;
1619 oid_number &= ~KERN_PROC_INC_THREAD;
1621 #ifdef COMPAT_FREEBSD32
1622 if (req->flags & SCTL_MASK32)
1623 flags |= KERN_PROC_MASK32;
1625 if (oid_number == KERN_PROC_PID) {
1628 error = sysctl_wire_old_buffer(req, 0);
1631 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1633 error = sysctl_out_proc(p, req, flags);
1637 switch (oid_number) {
1642 case KERN_PROC_PROC:
1643 if (namelen != 0 && namelen != 1)
1652 if (req->oldptr == NULL) {
1653 /* overestimate by 5 procs */
1654 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1658 error = sysctl_wire_old_buffer(req, 0);
1662 iterarg.flags = flags;
1663 iterarg.oid_number = oid_number;
1665 iterarg.name = name;
1666 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1671 pargs_alloc(int len)
1675 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1677 refcount_init(&pa->ar_ref, 1);
1678 pa->ar_length = len;
1683 pargs_free(struct pargs *pa)
1690 pargs_hold(struct pargs *pa)
1695 refcount_acquire(&pa->ar_ref);
1699 pargs_drop(struct pargs *pa)
1704 if (refcount_release(&pa->ar_ref))
1709 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1715 * This may return a short read if the string is shorter than the chunk
1716 * and is aligned at the end of the page, and the following page is not
1719 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1725 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1727 enum proc_vector_type {
1733 #ifdef COMPAT_FREEBSD32
1735 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1736 size_t *vsizep, enum proc_vector_type type)
1738 struct freebsd32_ps_strings pss;
1740 vm_offset_t vptr, ptr;
1741 uint32_t *proc_vector32;
1747 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1748 sizeof(pss)) != sizeof(pss))
1752 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1753 vsize = pss.ps_nargvstr;
1754 if (vsize > ARG_MAX)
1756 size = vsize * sizeof(int32_t);
1759 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1760 vsize = pss.ps_nenvstr;
1761 if (vsize > ARG_MAX)
1763 size = vsize * sizeof(int32_t);
1766 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1767 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1770 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1771 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1774 if (aux.a_type == AT_NULL)
1778 if (aux.a_type != AT_NULL)
1781 size = vsize * sizeof(aux);
1784 KASSERT(0, ("Wrong proc vector type: %d", type));
1787 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1788 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1792 if (type == PROC_AUX) {
1793 *proc_vectorp = (char **)proc_vector32;
1797 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1798 for (i = 0; i < (int)vsize; i++)
1799 proc_vector[i] = PTRIN(proc_vector32[i]);
1800 *proc_vectorp = proc_vector;
1803 free(proc_vector32, M_TEMP);
1809 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1810 size_t *vsizep, enum proc_vector_type type)
1812 struct ps_strings pss;
1814 vm_offset_t vptr, ptr;
1819 #ifdef COMPAT_FREEBSD32
1820 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1821 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1823 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1824 sizeof(pss)) != sizeof(pss))
1828 vptr = (vm_offset_t)pss.ps_argvstr;
1829 vsize = pss.ps_nargvstr;
1830 if (vsize > ARG_MAX)
1832 size = vsize * sizeof(char *);
1835 vptr = (vm_offset_t)pss.ps_envstr;
1836 vsize = pss.ps_nenvstr;
1837 if (vsize > ARG_MAX)
1839 size = vsize * sizeof(char *);
1843 * The aux array is just above env array on the stack. Check
1844 * that the address is naturally aligned.
1846 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1848 #if __ELF_WORD_SIZE == 64
1849 if (vptr % sizeof(uint64_t) != 0)
1851 if (vptr % sizeof(uint32_t) != 0)
1855 * We count the array size reading the aux vectors from the
1856 * stack until AT_NULL vector is returned. So (to keep the code
1857 * simple) we read the process stack twice: the first time here
1858 * to find the size and the second time when copying the vectors
1859 * to the allocated proc_vector.
1861 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1862 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1865 if (aux.a_type == AT_NULL)
1870 * If the PROC_AUXV_MAX entries are iterated over, and we have
1871 * not reached AT_NULL, it is most likely we are reading wrong
1872 * data: either the process doesn't have auxv array or data has
1873 * been modified. Return the error in this case.
1875 if (aux.a_type != AT_NULL)
1878 size = vsize * sizeof(aux);
1881 KASSERT(0, ("Wrong proc vector type: %d", type));
1882 return (EINVAL); /* In case we are built without INVARIANTS. */
1884 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1885 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1886 free(proc_vector, M_TEMP);
1889 *proc_vectorp = proc_vector;
1895 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1898 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1899 enum proc_vector_type type)
1901 size_t done, len, nchr, vsize;
1903 char **proc_vector, *sptr;
1904 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1906 PROC_ASSERT_HELD(p);
1909 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1911 nchr = 2 * (PATH_MAX + ARG_MAX);
1913 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1916 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1918 * The program may have scribbled into its argv array, e.g. to
1919 * remove some arguments. If that has happened, break out
1920 * before trying to read from NULL.
1922 if (proc_vector[i] == NULL)
1924 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1925 error = proc_read_string(td, p, sptr, pss_string,
1926 sizeof(pss_string));
1929 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1930 if (done + len >= nchr)
1931 len = nchr - done - 1;
1932 sbuf_bcat(sb, pss_string, len);
1933 if (len != GET_PS_STRINGS_CHUNK_SZ)
1935 done += GET_PS_STRINGS_CHUNK_SZ;
1937 sbuf_bcat(sb, "", 1);
1941 free(proc_vector, M_TEMP);
1946 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1949 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1953 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1956 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1960 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1966 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1968 #ifdef COMPAT_FREEBSD32
1969 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1970 size = vsize * sizeof(Elf32_Auxinfo);
1973 size = vsize * sizeof(Elf_Auxinfo);
1974 if (sbuf_bcat(sb, auxv, size) != 0)
1982 * This sysctl allows a process to retrieve the argument list or process
1983 * title for another process without groping around in the address space
1984 * of the other process. It also allow a process to set its own "process
1985 * title to a string of its own choice.
1988 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1990 int *name = (int *)arg1;
1991 u_int namelen = arg2;
1992 struct pargs *newpa, *pa;
1995 int flags, error = 0, error2;
2001 pid = (pid_t)name[0];
2003 * If the query is for this process and it is single-threaded, there
2004 * is nobody to modify pargs, thus we can just read.
2007 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2008 (pa = p->p_args) != NULL)
2009 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2011 flags = PGET_CANSEE;
2012 if (req->newptr != NULL)
2013 flags |= PGET_ISCURRENT;
2014 error = pget(pid, flags, &p);
2022 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2024 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2027 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2028 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2029 error = proc_getargv(curthread, p, &sb);
2030 error2 = sbuf_finish(&sb);
2033 if (error == 0 && error2 != 0)
2038 if (error != 0 || req->newptr == NULL)
2041 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2044 if (req->newlen == 0) {
2046 * Clear the argument pointer, so that we'll fetch arguments
2047 * with proc_getargv() until further notice.
2051 newpa = pargs_alloc(req->newlen);
2052 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2067 * This sysctl allows a process to retrieve environment of another process.
2070 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2072 int *name = (int *)arg1;
2073 u_int namelen = arg2;
2081 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2084 if ((p->p_flag & P_SYSTEM) != 0) {
2089 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2090 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2091 error = proc_getenvv(curthread, p, &sb);
2092 error2 = sbuf_finish(&sb);
2095 return (error != 0 ? error : error2);
2099 * This sysctl allows a process to retrieve ELF auxiliary vector of
2103 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2105 int *name = (int *)arg1;
2106 u_int namelen = arg2;
2114 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2117 if ((p->p_flag & P_SYSTEM) != 0) {
2121 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2122 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2123 error = proc_getauxv(curthread, p, &sb);
2124 error2 = sbuf_finish(&sb);
2127 return (error != 0 ? error : error2);
2131 * This sysctl allows a process to retrieve the path of the executable for
2132 * itself or another process.
2135 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2137 pid_t *pidp = (pid_t *)arg1;
2138 unsigned int arglen = arg2;
2141 char *retbuf, *freebuf;
2146 if (*pidp == -1) { /* -1 means this process */
2147 p = req->td->td_proc;
2149 error = pget(*pidp, PGET_CANSEE, &p);
2163 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2167 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2168 free(freebuf, M_TEMP);
2173 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2186 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2189 sv_name = p->p_sysent->sv_name;
2191 return (sysctl_handle_string(oidp, sv_name, 0, req));
2194 #ifdef KINFO_OVMENTRY_SIZE
2195 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2198 #ifdef COMPAT_FREEBSD7
2200 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2202 vm_map_entry_t entry, tmp_entry;
2203 unsigned int last_timestamp;
2204 char *fullpath, *freepath;
2205 struct kinfo_ovmentry *kve;
2215 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2218 vm = vmspace_acquire_ref(p);
2223 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2226 vm_map_lock_read(map);
2227 VM_MAP_ENTRY_FOREACH(entry, map) {
2228 vm_object_t obj, tobj, lobj;
2231 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2234 bzero(kve, sizeof(*kve));
2235 kve->kve_structsize = sizeof(*kve);
2237 kve->kve_private_resident = 0;
2238 obj = entry->object.vm_object;
2240 VM_OBJECT_RLOCK(obj);
2241 if (obj->shadow_count == 1)
2242 kve->kve_private_resident =
2243 obj->resident_page_count;
2245 kve->kve_resident = 0;
2246 addr = entry->start;
2247 while (addr < entry->end) {
2248 if (pmap_extract(map->pmap, addr))
2249 kve->kve_resident++;
2253 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2255 VM_OBJECT_RLOCK(tobj);
2256 kve->kve_offset += tobj->backing_object_offset;
2259 VM_OBJECT_RUNLOCK(lobj);
2263 kve->kve_start = (void*)entry->start;
2264 kve->kve_end = (void*)entry->end;
2265 kve->kve_offset += (off_t)entry->offset;
2267 if (entry->protection & VM_PROT_READ)
2268 kve->kve_protection |= KVME_PROT_READ;
2269 if (entry->protection & VM_PROT_WRITE)
2270 kve->kve_protection |= KVME_PROT_WRITE;
2271 if (entry->protection & VM_PROT_EXECUTE)
2272 kve->kve_protection |= KVME_PROT_EXEC;
2274 if (entry->eflags & MAP_ENTRY_COW)
2275 kve->kve_flags |= KVME_FLAG_COW;
2276 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2277 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2278 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2279 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2281 last_timestamp = map->timestamp;
2282 vm_map_unlock_read(map);
2284 kve->kve_fileid = 0;
2289 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2290 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2291 kve->kve_type = KVME_TYPE_UNKNOWN;
2295 VM_OBJECT_RUNLOCK(lobj);
2297 kve->kve_ref_count = obj->ref_count;
2298 kve->kve_shadow_count = obj->shadow_count;
2299 VM_OBJECT_RUNLOCK(obj);
2301 vn_fullpath(curthread, vp, &fullpath,
2303 cred = curthread->td_ucred;
2304 vn_lock(vp, LK_SHARED | LK_RETRY);
2305 if (VOP_GETATTR(vp, &va, cred) == 0) {
2306 kve->kve_fileid = va.va_fileid;
2308 kve->kve_fsid = va.va_fsid;
2313 kve->kve_type = KVME_TYPE_NONE;
2314 kve->kve_ref_count = 0;
2315 kve->kve_shadow_count = 0;
2318 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2319 if (freepath != NULL)
2320 free(freepath, M_TEMP);
2322 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2323 vm_map_lock_read(map);
2326 if (last_timestamp != map->timestamp) {
2327 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2331 vm_map_unlock_read(map);
2337 #endif /* COMPAT_FREEBSD7 */
2339 #ifdef KINFO_VMENTRY_SIZE
2340 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2344 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2345 int *resident_count, bool *super)
2347 vm_object_t obj, tobj;
2351 vm_pindex_t pi, pi_adv, pindex;
2354 *resident_count = 0;
2355 if (vmmap_skip_res_cnt)
2359 obj = entry->object.vm_object;
2360 addr = entry->start;
2362 pi = OFF_TO_IDX(entry->offset);
2363 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2364 if (m_adv != NULL) {
2367 pi_adv = atop(entry->end - addr);
2369 for (tobj = obj;; tobj = tobj->backing_object) {
2370 m = vm_page_find_least(tobj, pindex);
2372 if (m->pindex == pindex)
2374 if (pi_adv > m->pindex - pindex) {
2375 pi_adv = m->pindex - pindex;
2379 if (tobj->backing_object == NULL)
2381 pindex += OFF_TO_IDX(tobj->
2382 backing_object_offset);
2386 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2387 (addr & (pagesizes[1] - 1)) == 0 &&
2388 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2390 pi_adv = atop(pagesizes[1]);
2393 * We do not test the found page on validity.
2394 * Either the page is busy and being paged in,
2395 * or it was invalidated. The first case
2396 * should be counted as resident, the second
2397 * is not so clear; we do account both.
2401 *resident_count += pi_adv;
2407 * Must be called with the process locked and will return unlocked.
2410 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2412 vm_map_entry_t entry, tmp_entry;
2415 vm_object_t obj, tobj, lobj;
2416 char *fullpath, *freepath;
2417 struct kinfo_vmentry *kve;
2422 unsigned int last_timestamp;
2426 PROC_LOCK_ASSERT(p, MA_OWNED);
2430 vm = vmspace_acquire_ref(p);
2435 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2439 vm_map_lock_read(map);
2440 VM_MAP_ENTRY_FOREACH(entry, map) {
2441 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2445 bzero(kve, sizeof(*kve));
2446 obj = entry->object.vm_object;
2448 for (tobj = obj; tobj != NULL;
2449 tobj = tobj->backing_object) {
2450 VM_OBJECT_RLOCK(tobj);
2451 kve->kve_offset += tobj->backing_object_offset;
2454 if (obj->backing_object == NULL)
2455 kve->kve_private_resident =
2456 obj->resident_page_count;
2457 kern_proc_vmmap_resident(map, entry,
2458 &kve->kve_resident, &super);
2460 kve->kve_flags |= KVME_FLAG_SUPER;
2461 for (tobj = obj; tobj != NULL;
2462 tobj = tobj->backing_object) {
2463 if (tobj != obj && tobj != lobj)
2464 VM_OBJECT_RUNLOCK(tobj);
2470 kve->kve_start = entry->start;
2471 kve->kve_end = entry->end;
2472 kve->kve_offset += entry->offset;
2474 if (entry->protection & VM_PROT_READ)
2475 kve->kve_protection |= KVME_PROT_READ;
2476 if (entry->protection & VM_PROT_WRITE)
2477 kve->kve_protection |= KVME_PROT_WRITE;
2478 if (entry->protection & VM_PROT_EXECUTE)
2479 kve->kve_protection |= KVME_PROT_EXEC;
2481 if (entry->eflags & MAP_ENTRY_COW)
2482 kve->kve_flags |= KVME_FLAG_COW;
2483 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2484 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2485 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2486 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2487 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2488 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2489 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2490 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2491 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2492 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2494 last_timestamp = map->timestamp;
2495 vm_map_unlock_read(map);
2500 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2504 VM_OBJECT_RUNLOCK(lobj);
2506 kve->kve_ref_count = obj->ref_count;
2507 kve->kve_shadow_count = obj->shadow_count;
2508 VM_OBJECT_RUNLOCK(obj);
2510 vn_fullpath(curthread, vp, &fullpath,
2512 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2513 cred = curthread->td_ucred;
2514 vn_lock(vp, LK_SHARED | LK_RETRY);
2515 if (VOP_GETATTR(vp, &va, cred) == 0) {
2516 kve->kve_vn_fileid = va.va_fileid;
2517 kve->kve_vn_fsid = va.va_fsid;
2518 kve->kve_vn_fsid_freebsd11 =
2519 kve->kve_vn_fsid; /* truncate */
2521 MAKEIMODE(va.va_type, va.va_mode);
2522 kve->kve_vn_size = va.va_size;
2523 kve->kve_vn_rdev = va.va_rdev;
2524 kve->kve_vn_rdev_freebsd11 =
2525 kve->kve_vn_rdev; /* truncate */
2526 kve->kve_status = KF_ATTR_VALID;
2531 kve->kve_type = KVME_TYPE_NONE;
2532 kve->kve_ref_count = 0;
2533 kve->kve_shadow_count = 0;
2536 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2537 if (freepath != NULL)
2538 free(freepath, M_TEMP);
2540 /* Pack record size down */
2541 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2542 kve->kve_structsize =
2543 offsetof(struct kinfo_vmentry, kve_path) +
2544 strlen(kve->kve_path) + 1;
2546 kve->kve_structsize = sizeof(*kve);
2547 kve->kve_structsize = roundup(kve->kve_structsize,
2550 /* Halt filling and truncate rather than exceeding maxlen */
2551 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2553 vm_map_lock_read(map);
2555 } else if (maxlen != -1)
2556 maxlen -= kve->kve_structsize;
2558 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2560 vm_map_lock_read(map);
2563 if (last_timestamp != map->timestamp) {
2564 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2568 vm_map_unlock_read(map);
2576 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2580 int error, error2, *name;
2583 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2584 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2585 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2590 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2591 error2 = sbuf_finish(&sb);
2593 return (error != 0 ? error : error2);
2596 #if defined(STACK) || defined(DDB)
2598 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2600 struct kinfo_kstack *kkstp;
2601 int error, i, *name, numthreads;
2602 lwpid_t *lwpidarray;
2609 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2613 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2614 st = stack_create(M_WAITOK);
2619 if (lwpidarray != NULL) {
2620 free(lwpidarray, M_TEMP);
2623 numthreads = p->p_numthreads;
2625 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2628 } while (numthreads < p->p_numthreads);
2631 * XXXRW: During the below loop, execve(2) and countless other sorts
2632 * of changes could have taken place. Should we check to see if the
2633 * vmspace has been replaced, or the like, in order to prevent
2634 * giving a snapshot that spans, say, execve(2), with some threads
2635 * before and some after? Among other things, the credentials could
2636 * have changed, in which case the right to extract debug info might
2637 * no longer be assured.
2640 FOREACH_THREAD_IN_PROC(p, td) {
2641 KASSERT(i < numthreads,
2642 ("sysctl_kern_proc_kstack: numthreads"));
2643 lwpidarray[i] = td->td_tid;
2647 for (i = 0; i < numthreads; i++) {
2648 td = thread_find(p, lwpidarray[i]);
2652 bzero(kkstp, sizeof(*kkstp));
2653 (void)sbuf_new(&sb, kkstp->kkst_trace,
2654 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2656 kkstp->kkst_tid = td->td_tid;
2657 if (TD_IS_SWAPPED(td))
2658 kkstp->kkst_state = KKST_STATE_SWAPPED;
2659 else if (stack_save_td(st, td) == 0)
2660 kkstp->kkst_state = KKST_STATE_STACKOK;
2662 kkstp->kkst_state = KKST_STATE_RUNNING;
2665 stack_sbuf_print(&sb, st);
2668 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2675 if (lwpidarray != NULL)
2676 free(lwpidarray, M_TEMP);
2678 free(kkstp, M_TEMP);
2684 * This sysctl allows a process to retrieve the full list of groups from
2685 * itself or another process.
2688 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2690 pid_t *pidp = (pid_t *)arg1;
2691 unsigned int arglen = arg2;
2698 if (*pidp == -1) { /* -1 means this process */
2699 p = req->td->td_proc;
2702 error = pget(*pidp, PGET_CANSEE, &p);
2707 cred = crhold(p->p_ucred);
2710 error = SYSCTL_OUT(req, cred->cr_groups,
2711 cred->cr_ngroups * sizeof(gid_t));
2717 * This sysctl allows a process to retrieve or/and set the resource limit for
2721 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2723 int *name = (int *)arg1;
2724 u_int namelen = arg2;
2733 which = (u_int)name[1];
2734 if (which >= RLIM_NLIMITS)
2737 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2740 flags = PGET_HOLD | PGET_NOTWEXIT;
2741 if (req->newptr != NULL)
2742 flags |= PGET_CANDEBUG;
2744 flags |= PGET_CANSEE;
2745 error = pget((pid_t)name[0], flags, &p);
2752 if (req->oldptr != NULL) {
2754 lim_rlimit_proc(p, which, &rlim);
2757 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2764 if (req->newptr != NULL) {
2765 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2767 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2776 * This sysctl allows a process to retrieve ps_strings structure location of
2780 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2782 int *name = (int *)arg1;
2783 u_int namelen = arg2;
2785 vm_offset_t ps_strings;
2787 #ifdef COMPAT_FREEBSD32
2788 uint32_t ps_strings32;
2794 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2797 #ifdef COMPAT_FREEBSD32
2798 if ((req->flags & SCTL_MASK32) != 0) {
2800 * We return 0 if the 32 bit emulation request is for a 64 bit
2803 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2804 PTROUT(p->p_sysent->sv_psstrings) : 0;
2806 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2810 ps_strings = p->p_sysent->sv_psstrings;
2812 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2817 * This sysctl allows a process to retrieve umask of another process.
2820 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2822 int *name = (int *)arg1;
2823 u_int namelen = arg2;
2832 pid = (pid_t)name[0];
2834 if (pid == p->p_pid || pid == 0) {
2835 fd_cmask = p->p_fd->fd_cmask;
2839 error = pget(pid, PGET_WANTREAD, &p);
2843 fd_cmask = p->p_fd->fd_cmask;
2846 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2851 * This sysctl allows a process to set and retrieve binary osreldate of
2855 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2857 int *name = (int *)arg1;
2858 u_int namelen = arg2;
2860 int flags, error, osrel;
2865 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2868 flags = PGET_HOLD | PGET_NOTWEXIT;
2869 if (req->newptr != NULL)
2870 flags |= PGET_CANDEBUG;
2872 flags |= PGET_CANSEE;
2873 error = pget((pid_t)name[0], flags, &p);
2877 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2881 if (req->newptr != NULL) {
2882 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2897 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2899 int *name = (int *)arg1;
2900 u_int namelen = arg2;
2902 struct kinfo_sigtramp kst;
2903 const struct sysentvec *sv;
2905 #ifdef COMPAT_FREEBSD32
2906 struct kinfo_sigtramp32 kst32;
2912 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2916 #ifdef COMPAT_FREEBSD32
2917 if ((req->flags & SCTL_MASK32) != 0) {
2918 bzero(&kst32, sizeof(kst32));
2919 if (SV_PROC_FLAG(p, SV_ILP32)) {
2920 if (sv->sv_sigcode_base != 0) {
2921 kst32.ksigtramp_start = sv->sv_sigcode_base;
2922 kst32.ksigtramp_end = sv->sv_sigcode_base +
2925 kst32.ksigtramp_start = sv->sv_psstrings -
2927 kst32.ksigtramp_end = sv->sv_psstrings;
2931 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2935 bzero(&kst, sizeof(kst));
2936 if (sv->sv_sigcode_base != 0) {
2937 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2938 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2941 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2943 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2946 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2951 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
2953 int *name = (int *)arg1;
2954 u_int namelen = arg2;
2959 #ifdef COMPAT_FREEBSD32
2964 if (namelen != 1 || req->newptr != NULL)
2967 pid = (pid_t)name[0];
2968 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
2973 #ifdef COMPAT_FREEBSD32
2974 if (SV_CURPROC_FLAG(SV_ILP32)) {
2975 if (!SV_PROC_FLAG(p, SV_ILP32)) {
2981 if (pid <= PID_MAX) {
2982 td1 = FIRST_THREAD_IN_PROC(p);
2984 FOREACH_THREAD_IN_PROC(p, td1) {
2985 if (td1->td_tid == pid)
2994 * The access to the private thread flags. It is fine as far
2995 * as no out-of-thin-air values are read from td_pflags, and
2996 * usermode read of the td_sigblock_ptr is racy inherently,
2997 * since target process might have already changed it
3000 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3001 addr = (uintptr_t)td1->td_sigblock_ptr;
3011 #ifdef COMPAT_FREEBSD32
3012 if (SV_CURPROC_FLAG(SV_ILP32)) {
3014 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3017 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3021 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3024 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3025 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3026 "Return entire process table");
3028 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3029 sysctl_kern_proc, "Process table");
3031 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3032 sysctl_kern_proc, "Process table");
3034 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3035 sysctl_kern_proc, "Process table");
3037 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3038 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3040 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3041 sysctl_kern_proc, "Process table");
3043 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3044 sysctl_kern_proc, "Process table");
3046 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3047 sysctl_kern_proc, "Process table");
3049 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3050 sysctl_kern_proc, "Process table");
3052 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3053 sysctl_kern_proc, "Return process table, no threads");
3055 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3056 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3057 sysctl_kern_proc_args, "Process argument list");
3059 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3060 sysctl_kern_proc_env, "Process environment");
3062 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3063 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3065 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3066 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3068 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3069 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3070 "Process syscall vector name (ABI type)");
3072 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3073 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3075 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3076 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3078 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3079 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3081 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3082 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3084 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3085 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3087 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3088 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3090 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3091 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3093 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3094 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3096 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3097 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3098 "Return process table, no threads");
3100 #ifdef COMPAT_FREEBSD7
3101 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3102 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3105 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3106 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3108 #if defined(STACK) || defined(DDB)
3109 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3110 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3113 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3114 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3116 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3117 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3118 "Process resource limits");
3120 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3121 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3122 "Process ps_strings location");
3124 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3125 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3127 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3128 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3129 "Process binary osreldate");
3131 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3132 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3133 "Process signal trampoline location");
3135 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3136 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3137 "Thread sigfastblock address");
3142 * stop_all_proc() purpose is to stop all process which have usermode,
3143 * except current process for obvious reasons. This makes it somewhat
3144 * unreliable when invoked from multithreaded process. The service
3145 * must not be user-callable anyway.
3150 struct proc *cp, *p;
3152 bool restart, seen_stopped, seen_exiting, stopped_some;
3156 sx_xlock(&allproc_lock);
3158 seen_exiting = seen_stopped = stopped_some = restart = false;
3159 LIST_REMOVE(cp, p_list);
3160 LIST_INSERT_HEAD(&allproc, cp, p_list);
3162 p = LIST_NEXT(cp, p_list);
3165 LIST_REMOVE(cp, p_list);
3166 LIST_INSERT_AFTER(p, cp, p_list);
3168 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3172 if ((p->p_flag & P_WEXIT) != 0) {
3173 seen_exiting = true;
3177 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3179 * Stopped processes are tolerated when there
3180 * are no other processes which might continue
3181 * them. P_STOPPED_SINGLE but not
3182 * P_TOTAL_STOP process still has at least one
3185 seen_stopped = true;
3189 sx_xunlock(&allproc_lock);
3191 r = thread_single(p, SINGLE_ALLPROC);
3195 stopped_some = true;
3198 sx_xlock(&allproc_lock);
3200 /* Catch forked children we did not see in iteration. */
3201 if (gen != allproc_gen)
3203 sx_xunlock(&allproc_lock);
3204 if (restart || stopped_some || seen_exiting || seen_stopped) {
3205 kern_yield(PRI_USER);
3211 resume_all_proc(void)
3213 struct proc *cp, *p;
3216 sx_xlock(&allproc_lock);
3218 LIST_REMOVE(cp, p_list);
3219 LIST_INSERT_HEAD(&allproc, cp, p_list);
3221 p = LIST_NEXT(cp, p_list);
3224 LIST_REMOVE(cp, p_list);
3225 LIST_INSERT_AFTER(p, cp, p_list);
3227 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3228 sx_xunlock(&allproc_lock);
3230 thread_single_end(p, SINGLE_ALLPROC);
3233 sx_xlock(&allproc_lock);
3238 /* Did the loop above missed any stopped process ? */
3239 FOREACH_PROC_IN_SYSTEM(p) {
3240 /* No need for proc lock. */
3241 if ((p->p_flag & P_TOTAL_STOP) != 0)
3244 sx_xunlock(&allproc_lock);
3247 /* #define TOTAL_STOP_DEBUG 1 */
3248 #ifdef TOTAL_STOP_DEBUG
3249 volatile static int ap_resume;
3250 #include <sys/mount.h>
3253 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3259 error = sysctl_handle_int(oidp, &val, 0, req);
3260 if (error != 0 || req->newptr == NULL)
3265 while (ap_resume == 0)
3273 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3274 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3275 sysctl_debug_stop_all_proc, "I",