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>
45 #include <sys/eventhandler.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
51 #include <sys/loginclass.h>
52 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
57 #include <sys/ptrace.h>
58 #include <sys/refcount.h>
59 #include <sys/resourcevar.h>
60 #include <sys/rwlock.h>
62 #include <sys/sysent.h>
63 #include <sys/sched.h>
65 #include <sys/stack.h>
67 #include <sys/sysctl.h>
68 #include <sys/filedesc.h>
70 #include <sys/signalvar.h>
74 #include <sys/vnode.h>
82 #include <vm/vm_param.h>
83 #include <vm/vm_extern.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
90 #ifdef COMPAT_FREEBSD32
91 #include <compat/freebsd32/freebsd32.h>
92 #include <compat/freebsd32/freebsd32_util.h>
95 SDT_PROVIDER_DEFINE(proc);
96 SDT_PROBE_DEFINE4(proc, , ctor, entry, "struct proc *", "int", "void *",
98 SDT_PROBE_DEFINE4(proc, , ctor, return, "struct proc *", "int", "void *",
100 SDT_PROBE_DEFINE4(proc, , dtor, entry, "struct proc *", "int", "void *",
102 SDT_PROBE_DEFINE3(proc, , dtor, return, "struct proc *", "int", "void *");
103 SDT_PROBE_DEFINE3(proc, , init, entry, "struct proc *", "int", "int");
104 SDT_PROBE_DEFINE3(proc, , init, return, "struct proc *", "int", "int");
106 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
107 MALLOC_DEFINE(M_SESSION, "session", "session header");
108 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
109 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
111 static void doenterpgrp(struct proc *, struct pgrp *);
112 static void orphanpg(struct pgrp *pg);
113 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
114 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
115 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
117 static void pgadjustjobc(struct pgrp *pgrp, int entering);
118 static void pgdelete(struct pgrp *);
119 static int proc_ctor(void *mem, int size, void *arg, int flags);
120 static void proc_dtor(void *mem, int size, void *arg);
121 static int proc_init(void *mem, int size, int flags);
122 static void proc_fini(void *mem, int size);
123 static void pargs_free(struct pargs *pa);
124 static struct proc *zpfind_locked(pid_t pid);
127 * Other process lists
129 struct pidhashhead *pidhashtbl;
131 struct pgrphashhead *pgrphashtbl;
133 struct proclist allproc;
134 struct proclist zombproc;
135 struct sx __exclusive_cache_line allproc_lock;
136 struct sx __exclusive_cache_line proctree_lock;
137 struct mtx __exclusive_cache_line ppeers_lock;
138 uma_zone_t proc_zone;
141 * The offset of various fields in struct proc and struct thread.
142 * These are used by kernel debuggers to enumerate kernel threads and
145 const int proc_off_p_pid = offsetof(struct proc, p_pid);
146 const int proc_off_p_comm = offsetof(struct proc, p_comm);
147 const int proc_off_p_list = offsetof(struct proc, p_list);
148 const int proc_off_p_threads = offsetof(struct proc, p_threads);
149 const int thread_off_td_tid = offsetof(struct thread, td_tid);
150 const int thread_off_td_name = offsetof(struct thread, td_name);
151 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
152 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
153 const int thread_off_td_plist = offsetof(struct thread, td_plist);
155 EVENTHANDLER_LIST_DEFINE(process_ctor);
156 EVENTHANDLER_LIST_DEFINE(process_dtor);
157 EVENTHANDLER_LIST_DEFINE(process_init);
158 EVENTHANDLER_LIST_DEFINE(process_fini);
159 EVENTHANDLER_LIST_DEFINE(process_exit);
160 EVENTHANDLER_LIST_DEFINE(process_fork);
161 EVENTHANDLER_LIST_DEFINE(process_exec);
163 EVENTHANDLER_LIST_DECLARE(thread_ctor);
164 EVENTHANDLER_LIST_DECLARE(thread_dtor);
166 int kstack_pages = KSTACK_PAGES;
167 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
168 "Kernel stack size in pages");
169 static int vmmap_skip_res_cnt = 0;
170 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
171 &vmmap_skip_res_cnt, 0,
172 "Skip calculation of the pages resident count in kern.proc.vmmap");
174 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
175 #ifdef COMPAT_FREEBSD32
176 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
180 * Initialize global process hashing structures.
186 sx_init(&allproc_lock, "allproc");
187 sx_init(&proctree_lock, "proctree");
188 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
190 LIST_INIT(&zombproc);
191 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
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 SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
210 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
211 SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
212 td = FIRST_THREAD_IN_PROC(p);
214 /* Make sure all thread constructors are executed */
215 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
221 * Reclaim a proc after use.
224 proc_dtor(void *mem, int size, void *arg)
229 /* INVARIANTS checks go here */
230 p = (struct proc *)mem;
231 td = FIRST_THREAD_IN_PROC(p);
232 SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
235 KASSERT((p->p_numthreads == 1),
236 ("bad number of threads in exiting process"));
237 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
239 /* Free all OSD associated to this thread. */
241 td_softdep_cleanup(td);
242 MPASS(td->td_su == NULL);
244 /* Make sure all thread destructors are executed */
245 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
247 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
248 if (p->p_ksi != NULL)
249 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
250 SDT_PROBE3(proc, , dtor, return, p, size, arg);
254 * Initialize type-stable parts of a proc (when newly created).
257 proc_init(void *mem, int size, int flags)
261 p = (struct proc *)mem;
262 SDT_PROBE3(proc, , init, entry, p, size, flags);
263 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
264 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
265 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
266 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
267 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
268 cv_init(&p->p_pwait, "ppwait");
269 TAILQ_INIT(&p->p_threads); /* all threads in proc */
270 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
271 p->p_stats = pstats_alloc();
273 SDT_PROBE3(proc, , init, return, p, size, flags);
278 * UMA should ensure that this function is never called.
279 * Freeing a proc structure would violate type stability.
282 proc_fini(void *mem, int size)
287 p = (struct proc *)mem;
288 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
289 pstats_free(p->p_stats);
290 thread_free(FIRST_THREAD_IN_PROC(p));
291 mtx_destroy(&p->p_mtx);
292 if (p->p_ksi != NULL)
293 ksiginfo_free(p->p_ksi);
295 panic("proc reclaimed");
300 * Is p an inferior of the current process?
303 inferior(struct proc *p)
306 sx_assert(&proctree_lock, SX_LOCKED);
307 PROC_LOCK_ASSERT(p, MA_OWNED);
308 for (; p != curproc; p = proc_realparent(p)) {
316 pfind_locked(pid_t pid)
320 sx_assert(&allproc_lock, SX_LOCKED);
321 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
322 if (p->p_pid == pid) {
324 if (p->p_state == PRS_NEW) {
335 * Locate a process by number; return only "live" processes -- i.e., neither
336 * zombies nor newly born but incompletely initialized processes. By not
337 * returning processes in the PRS_NEW state, we allow callers to avoid
338 * testing for that condition to avoid dereferencing p_ucred, et al.
346 if (p->p_pid == pid) {
350 sx_slock(&allproc_lock);
351 p = pfind_locked(pid);
352 sx_sunlock(&allproc_lock);
357 * Same as pfind but allow zombies.
364 sx_slock(&allproc_lock);
365 p = pfind_locked(pid);
367 p = zpfind_locked(pid);
368 sx_sunlock(&allproc_lock);
374 pfind_tid_locked(pid_t tid)
379 sx_assert(&allproc_lock, SX_LOCKED);
380 FOREACH_PROC_IN_SYSTEM(p) {
382 if (p->p_state == PRS_NEW) {
386 FOREACH_THREAD_IN_PROC(p, td) {
387 if (td->td_tid == tid)
397 * Locate a process group by number.
398 * The caller must hold proctree_lock.
405 sx_assert(&proctree_lock, SX_LOCKED);
407 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
408 if (pgrp->pg_id == pgid) {
417 * Locate process and do additional manipulations, depending on flags.
420 pget(pid_t pid, int flags, struct proc **pp)
426 if (p->p_pid == pid) {
429 sx_slock(&allproc_lock);
430 if (pid <= PID_MAX) {
431 p = pfind_locked(pid);
432 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
433 p = zpfind_locked(pid);
434 } else if ((flags & PGET_NOTID) == 0) {
435 p = pfind_tid_locked(pid);
439 sx_sunlock(&allproc_lock);
442 if ((flags & PGET_CANSEE) != 0) {
443 error = p_cansee(curthread, p);
448 if ((flags & PGET_CANDEBUG) != 0) {
449 error = p_candebug(curthread, p);
453 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
457 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
461 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
463 * XXXRW: Not clear ESRCH is the right error during proc
469 if ((flags & PGET_HOLD) != 0) {
481 * Create a new process group.
482 * pgid must be equal to the pid of p.
483 * Begin a new session if required.
486 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
489 sx_assert(&proctree_lock, SX_XLOCKED);
491 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
492 KASSERT(p->p_pid == pgid,
493 ("enterpgrp: new pgrp and pid != pgid"));
494 KASSERT(pgfind(pgid) == NULL,
495 ("enterpgrp: pgrp with pgid exists"));
496 KASSERT(!SESS_LEADER(p),
497 ("enterpgrp: session leader attempted setpgrp"));
499 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
505 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
507 p->p_flag &= ~P_CONTROLT;
511 sess->s_sid = p->p_pid;
512 refcount_init(&sess->s_count, 1);
513 sess->s_ttyvp = NULL;
514 sess->s_ttydp = NULL;
516 bcopy(p->p_session->s_login, sess->s_login,
517 sizeof(sess->s_login));
518 pgrp->pg_session = sess;
519 KASSERT(p == curproc,
520 ("enterpgrp: mksession and p != curproc"));
522 pgrp->pg_session = p->p_session;
523 sess_hold(pgrp->pg_session);
527 LIST_INIT(&pgrp->pg_members);
530 * As we have an exclusive lock of proctree_lock,
531 * this should not deadlock.
533 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
535 SLIST_INIT(&pgrp->pg_sigiolst);
538 doenterpgrp(p, pgrp);
544 * Move p to an existing process group
547 enterthispgrp(struct proc *p, struct pgrp *pgrp)
550 sx_assert(&proctree_lock, SX_XLOCKED);
551 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
552 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
553 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
554 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
555 KASSERT(pgrp->pg_session == p->p_session,
556 ("%s: pgrp's session %p, p->p_session %p.\n",
560 KASSERT(pgrp != p->p_pgrp,
561 ("%s: p belongs to pgrp.", __func__));
563 doenterpgrp(p, pgrp);
569 * Move p to a process group
572 doenterpgrp(struct proc *p, struct pgrp *pgrp)
574 struct pgrp *savepgrp;
576 sx_assert(&proctree_lock, SX_XLOCKED);
577 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
578 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
579 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
580 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
582 savepgrp = p->p_pgrp;
585 * Adjust eligibility of affected pgrps to participate in job control.
586 * Increment eligibility counts before decrementing, otherwise we
587 * could reach 0 spuriously during the first call.
590 fixjobc(p, p->p_pgrp, 0);
595 LIST_REMOVE(p, p_pglist);
598 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
599 PGRP_UNLOCK(savepgrp);
601 if (LIST_EMPTY(&savepgrp->pg_members))
606 * remove process from process group
609 leavepgrp(struct proc *p)
611 struct pgrp *savepgrp;
613 sx_assert(&proctree_lock, SX_XLOCKED);
614 savepgrp = p->p_pgrp;
617 LIST_REMOVE(p, p_pglist);
620 PGRP_UNLOCK(savepgrp);
621 if (LIST_EMPTY(&savepgrp->pg_members))
627 * delete a process group
630 pgdelete(struct pgrp *pgrp)
632 struct session *savesess;
635 sx_assert(&proctree_lock, SX_XLOCKED);
636 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
637 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
640 * Reset any sigio structures pointing to us as a result of
641 * F_SETOWN with our pgid.
643 funsetownlst(&pgrp->pg_sigiolst);
646 tp = pgrp->pg_session->s_ttyp;
647 LIST_REMOVE(pgrp, pg_hash);
648 savesess = pgrp->pg_session;
651 /* Remove the reference to the pgrp before deallocating it. */
654 tty_rel_pgrp(tp, pgrp);
657 mtx_destroy(&pgrp->pg_mtx);
659 sess_release(savesess);
663 pgadjustjobc(struct pgrp *pgrp, int entering)
671 if (pgrp->pg_jobc == 0)
678 * Adjust pgrp jobc counters when specified process changes process group.
679 * We count the number of processes in each process group that "qualify"
680 * the group for terminal job control (those with a parent in a different
681 * process group of the same session). If that count reaches zero, the
682 * process group becomes orphaned. Check both the specified process'
683 * process group and that of its children.
684 * entering == 0 => p is leaving specified group.
685 * entering == 1 => p is entering specified group.
688 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
690 struct pgrp *hispgrp;
691 struct session *mysession;
694 sx_assert(&proctree_lock, SX_LOCKED);
695 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
696 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
697 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
700 * Check p's parent to see whether p qualifies its own process
701 * group; if so, adjust count for p's process group.
703 mysession = pgrp->pg_session;
704 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
705 hispgrp->pg_session == mysession)
706 pgadjustjobc(pgrp, entering);
709 * Check this process' children to see whether they qualify
710 * their process groups; if so, adjust counts for children's
713 LIST_FOREACH(q, &p->p_children, p_sibling) {
715 if (hispgrp == pgrp ||
716 hispgrp->pg_session != mysession)
718 if (q->p_state == PRS_ZOMBIE)
720 pgadjustjobc(hispgrp, entering);
733 MPASS(p->p_flag & P_WEXIT);
735 * Do a quick check to see if there is anything to do with the
736 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
739 if (!SESS_LEADER(p) &&
740 (p->p_pgrp == p->p_pptr->p_pgrp) &&
741 LIST_EMPTY(&p->p_children)) {
747 sx_xlock(&proctree_lock);
748 if (SESS_LEADER(p)) {
752 * s_ttyp is not zero'd; we use this to indicate that
753 * the session once had a controlling terminal. (for
754 * logging and informational purposes)
765 * Signal foreground pgrp and revoke access to
766 * controlling terminal if it has not been revoked
769 * Because the TTY may have been revoked in the mean
770 * time and could already have a new session associated
771 * with it, make sure we don't send a SIGHUP to a
772 * foreground process group that does not belong to this
778 if (tp->t_session == sp)
779 tty_signal_pgrp(tp, SIGHUP);
784 sx_xunlock(&proctree_lock);
785 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
786 VOP_REVOKE(ttyvp, REVOKEALL);
787 VOP_UNLOCK(ttyvp, 0);
790 sx_xlock(&proctree_lock);
793 fixjobc(p, p->p_pgrp, 0);
794 sx_xunlock(&proctree_lock);
798 * A process group has become orphaned;
799 * if there are any stopped processes in the group,
800 * hang-up all process in that group.
803 orphanpg(struct pgrp *pg)
807 PGRP_LOCK_ASSERT(pg, MA_OWNED);
809 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
811 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
813 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
815 kern_psignal(p, SIGHUP);
816 kern_psignal(p, SIGCONT);
826 sess_hold(struct session *s)
829 refcount_acquire(&s->s_count);
833 sess_release(struct session *s)
836 if (refcount_release(&s->s_count)) {
837 if (s->s_ttyp != NULL) {
839 tty_rel_sess(s->s_ttyp, s);
841 mtx_destroy(&s->s_mtx);
848 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
854 for (i = 0; i <= pgrphash; i++) {
855 if (!LIST_EMPTY(&pgrphashtbl[i])) {
856 printf("\tindx %d\n", i);
857 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
859 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
860 (void *)pgrp, (long)pgrp->pg_id,
861 (void *)pgrp->pg_session,
862 pgrp->pg_session->s_count,
863 (void *)LIST_FIRST(&pgrp->pg_members));
864 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
865 printf("\t\tpid %ld addr %p pgrp %p\n",
866 (long)p->p_pid, (void *)p,
876 * Calculate the kinfo_proc members which contain process-wide
878 * Must be called with the target process locked.
881 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
885 PROC_LOCK_ASSERT(p, MA_OWNED);
889 FOREACH_THREAD_IN_PROC(p, td) {
891 kp->ki_pctcpu += sched_pctcpu(td);
892 kp->ki_estcpu += sched_estcpu(td);
898 * Clear kinfo_proc and fill in any information that is common
899 * to all threads in the process.
900 * Must be called with the target process locked.
903 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
910 struct timeval boottime;
912 PROC_LOCK_ASSERT(p, MA_OWNED);
913 bzero(kp, sizeof(*kp));
915 kp->ki_structsize = sizeof(*kp);
917 kp->ki_addr =/* p->p_addr; */0; /* XXX */
918 kp->ki_args = p->p_args;
919 kp->ki_textvp = p->p_textvp;
921 kp->ki_tracep = p->p_tracevp;
922 kp->ki_traceflag = p->p_traceflag;
925 kp->ki_vmspace = p->p_vmspace;
926 kp->ki_flag = p->p_flag;
927 kp->ki_flag2 = p->p_flag2;
930 kp->ki_uid = cred->cr_uid;
931 kp->ki_ruid = cred->cr_ruid;
932 kp->ki_svuid = cred->cr_svuid;
934 if (cred->cr_flags & CRED_FLAG_CAPMODE)
935 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
936 /* XXX bde doesn't like KI_NGROUPS */
937 if (cred->cr_ngroups > KI_NGROUPS) {
938 kp->ki_ngroups = KI_NGROUPS;
939 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
941 kp->ki_ngroups = cred->cr_ngroups;
942 bcopy(cred->cr_groups, kp->ki_groups,
943 kp->ki_ngroups * sizeof(gid_t));
944 kp->ki_rgid = cred->cr_rgid;
945 kp->ki_svgid = cred->cr_svgid;
946 /* If jailed(cred), emulate the old P_JAILED flag. */
948 kp->ki_flag |= P_JAILED;
949 /* If inside the jail, use 0 as a jail ID. */
950 if (cred->cr_prison != curthread->td_ucred->cr_prison)
951 kp->ki_jid = cred->cr_prison->pr_id;
953 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
954 sizeof(kp->ki_loginclass));
958 mtx_lock(&ps->ps_mtx);
959 kp->ki_sigignore = ps->ps_sigignore;
960 kp->ki_sigcatch = ps->ps_sigcatch;
961 mtx_unlock(&ps->ps_mtx);
963 if (p->p_state != PRS_NEW &&
964 p->p_state != PRS_ZOMBIE &&
965 p->p_vmspace != NULL) {
966 struct vmspace *vm = p->p_vmspace;
968 kp->ki_size = vm->vm_map.size;
969 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
970 FOREACH_THREAD_IN_PROC(p, td0) {
971 if (!TD_IS_SWAPPED(td0))
972 kp->ki_rssize += td0->td_kstack_pages;
974 kp->ki_swrss = vm->vm_swrss;
975 kp->ki_tsize = vm->vm_tsize;
976 kp->ki_dsize = vm->vm_dsize;
977 kp->ki_ssize = vm->vm_ssize;
978 } else if (p->p_state == PRS_ZOMBIE)
980 if (kp->ki_flag & P_INMEM)
981 kp->ki_sflag = PS_INMEM;
984 /* Calculate legacy swtime as seconds since 'swtick'. */
985 kp->ki_swtime = (ticks - p->p_swtick) / hz;
986 kp->ki_pid = p->p_pid;
987 kp->ki_nice = p->p_nice;
988 kp->ki_fibnum = p->p_fibnum;
989 kp->ki_start = p->p_stats->p_start;
990 getboottime(&boottime);
991 timevaladd(&kp->ki_start, &boottime);
993 rufetch(p, &kp->ki_rusage);
994 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
995 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
997 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
998 /* Some callers want child times in a single value. */
999 kp->ki_childtime = kp->ki_childstime;
1000 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1002 FOREACH_THREAD_IN_PROC(p, td0)
1003 kp->ki_cow += td0->td_cow;
1007 kp->ki_pgid = p->p_pgrp->pg_id;
1008 kp->ki_jobc = p->p_pgrp->pg_jobc;
1009 sp = p->p_pgrp->pg_session;
1012 kp->ki_sid = sp->s_sid;
1014 strlcpy(kp->ki_login, sp->s_login,
1015 sizeof(kp->ki_login));
1017 kp->ki_kiflag |= KI_CTTY;
1019 kp->ki_kiflag |= KI_SLEADER;
1020 /* XXX proctree_lock */
1025 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1026 kp->ki_tdev = tty_udev(tp);
1027 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1028 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1030 kp->ki_tsid = tp->t_session->s_sid;
1032 kp->ki_tdev = NODEV;
1033 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1035 if (p->p_comm[0] != '\0')
1036 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1037 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1038 p->p_sysent->sv_name[0] != '\0')
1039 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1040 kp->ki_siglist = p->p_siglist;
1041 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1042 kp->ki_acflag = p->p_acflag;
1043 kp->ki_lock = p->p_lock;
1045 kp->ki_ppid = p->p_oppid;
1046 if (p->p_flag & P_TRACED)
1047 kp->ki_tracer = p->p_pptr->p_pid;
1052 * Fill in information that is thread specific. Must be called with
1053 * target process locked. If 'preferthread' is set, overwrite certain
1054 * process-related fields that are maintained for both threads and
1058 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1064 PROC_LOCK_ASSERT(p, MA_OWNED);
1069 if (td->td_wmesg != NULL)
1070 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1072 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1073 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1074 sizeof(kp->ki_tdname)) {
1075 strlcpy(kp->ki_moretdname,
1076 td->td_name + sizeof(kp->ki_tdname) - 1,
1077 sizeof(kp->ki_moretdname));
1079 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1081 if (TD_ON_LOCK(td)) {
1082 kp->ki_kiflag |= KI_LOCKBLOCK;
1083 strlcpy(kp->ki_lockname, td->td_lockname,
1084 sizeof(kp->ki_lockname));
1086 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1087 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1090 if (p->p_state == PRS_NORMAL) { /* approximate. */
1091 if (TD_ON_RUNQ(td) ||
1093 TD_IS_RUNNING(td)) {
1095 } else if (P_SHOULDSTOP(p)) {
1096 kp->ki_stat = SSTOP;
1097 } else if (TD_IS_SLEEPING(td)) {
1098 kp->ki_stat = SSLEEP;
1099 } else if (TD_ON_LOCK(td)) {
1100 kp->ki_stat = SLOCK;
1102 kp->ki_stat = SWAIT;
1104 } else if (p->p_state == PRS_ZOMBIE) {
1105 kp->ki_stat = SZOMB;
1110 /* Things in the thread */
1111 kp->ki_wchan = td->td_wchan;
1112 kp->ki_pri.pri_level = td->td_priority;
1113 kp->ki_pri.pri_native = td->td_base_pri;
1116 * Note: legacy fields; clamp at the old NOCPU value and/or
1117 * the maximum u_char CPU value.
1119 if (td->td_lastcpu == NOCPU)
1120 kp->ki_lastcpu_old = NOCPU_OLD;
1121 else if (td->td_lastcpu > MAXCPU_OLD)
1122 kp->ki_lastcpu_old = MAXCPU_OLD;
1124 kp->ki_lastcpu_old = td->td_lastcpu;
1126 if (td->td_oncpu == NOCPU)
1127 kp->ki_oncpu_old = NOCPU_OLD;
1128 else if (td->td_oncpu > MAXCPU_OLD)
1129 kp->ki_oncpu_old = MAXCPU_OLD;
1131 kp->ki_oncpu_old = td->td_oncpu;
1133 kp->ki_lastcpu = td->td_lastcpu;
1134 kp->ki_oncpu = td->td_oncpu;
1135 kp->ki_tdflags = td->td_flags;
1136 kp->ki_tid = td->td_tid;
1137 kp->ki_numthreads = p->p_numthreads;
1138 kp->ki_pcb = td->td_pcb;
1139 kp->ki_kstack = (void *)td->td_kstack;
1140 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1141 kp->ki_pri.pri_class = td->td_pri_class;
1142 kp->ki_pri.pri_user = td->td_user_pri;
1145 rufetchtd(td, &kp->ki_rusage);
1146 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1147 kp->ki_pctcpu = sched_pctcpu(td);
1148 kp->ki_estcpu = sched_estcpu(td);
1149 kp->ki_cow = td->td_cow;
1152 /* We can't get this anymore but ps etc never used it anyway. */
1156 kp->ki_siglist = td->td_siglist;
1157 kp->ki_sigmask = td->td_sigmask;
1164 * Fill in a kinfo_proc structure for the specified process.
1165 * Must be called with the target process locked.
1168 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1171 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1173 fill_kinfo_proc_only(p, kp);
1174 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1175 fill_kinfo_aggregate(p, kp);
1182 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1186 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1189 pstats_fork(struct pstats *src, struct pstats *dst)
1192 bzero(&dst->pstat_startzero,
1193 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1194 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1195 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1199 pstats_free(struct pstats *ps)
1202 free(ps, M_SUBPROC);
1205 static struct proc *
1206 zpfind_locked(pid_t pid)
1210 sx_assert(&allproc_lock, SX_LOCKED);
1211 LIST_FOREACH(p, &zombproc, p_list) {
1212 if (p->p_pid == pid) {
1221 * Locate a zombie process by number
1228 sx_slock(&allproc_lock);
1229 p = zpfind_locked(pid);
1230 sx_sunlock(&allproc_lock);
1234 #ifdef COMPAT_FREEBSD32
1237 * This function is typically used to copy out the kernel address, so
1238 * it can be replaced by assignment of zero.
1240 static inline uint32_t
1241 ptr32_trim(void *ptr)
1245 uptr = (uintptr_t)ptr;
1246 return ((uptr > UINT_MAX) ? 0 : uptr);
1249 #define PTRTRIM_CP(src,dst,fld) \
1250 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1253 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1257 bzero(ki32, sizeof(struct kinfo_proc32));
1258 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1259 CP(*ki, *ki32, ki_layout);
1260 PTRTRIM_CP(*ki, *ki32, ki_args);
1261 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1262 PTRTRIM_CP(*ki, *ki32, ki_addr);
1263 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1264 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1265 PTRTRIM_CP(*ki, *ki32, ki_fd);
1266 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1267 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1268 CP(*ki, *ki32, ki_pid);
1269 CP(*ki, *ki32, ki_ppid);
1270 CP(*ki, *ki32, ki_pgid);
1271 CP(*ki, *ki32, ki_tpgid);
1272 CP(*ki, *ki32, ki_sid);
1273 CP(*ki, *ki32, ki_tsid);
1274 CP(*ki, *ki32, ki_jobc);
1275 CP(*ki, *ki32, ki_tdev);
1276 CP(*ki, *ki32, ki_tdev_freebsd11);
1277 CP(*ki, *ki32, ki_siglist);
1278 CP(*ki, *ki32, ki_sigmask);
1279 CP(*ki, *ki32, ki_sigignore);
1280 CP(*ki, *ki32, ki_sigcatch);
1281 CP(*ki, *ki32, ki_uid);
1282 CP(*ki, *ki32, ki_ruid);
1283 CP(*ki, *ki32, ki_svuid);
1284 CP(*ki, *ki32, ki_rgid);
1285 CP(*ki, *ki32, ki_svgid);
1286 CP(*ki, *ki32, ki_ngroups);
1287 for (i = 0; i < KI_NGROUPS; i++)
1288 CP(*ki, *ki32, ki_groups[i]);
1289 CP(*ki, *ki32, ki_size);
1290 CP(*ki, *ki32, ki_rssize);
1291 CP(*ki, *ki32, ki_swrss);
1292 CP(*ki, *ki32, ki_tsize);
1293 CP(*ki, *ki32, ki_dsize);
1294 CP(*ki, *ki32, ki_ssize);
1295 CP(*ki, *ki32, ki_xstat);
1296 CP(*ki, *ki32, ki_acflag);
1297 CP(*ki, *ki32, ki_pctcpu);
1298 CP(*ki, *ki32, ki_estcpu);
1299 CP(*ki, *ki32, ki_slptime);
1300 CP(*ki, *ki32, ki_swtime);
1301 CP(*ki, *ki32, ki_cow);
1302 CP(*ki, *ki32, ki_runtime);
1303 TV_CP(*ki, *ki32, ki_start);
1304 TV_CP(*ki, *ki32, ki_childtime);
1305 CP(*ki, *ki32, ki_flag);
1306 CP(*ki, *ki32, ki_kiflag);
1307 CP(*ki, *ki32, ki_traceflag);
1308 CP(*ki, *ki32, ki_stat);
1309 CP(*ki, *ki32, ki_nice);
1310 CP(*ki, *ki32, ki_lock);
1311 CP(*ki, *ki32, ki_rqindex);
1312 CP(*ki, *ki32, ki_oncpu);
1313 CP(*ki, *ki32, ki_lastcpu);
1315 /* XXX TODO: wrap cpu value as appropriate */
1316 CP(*ki, *ki32, ki_oncpu_old);
1317 CP(*ki, *ki32, ki_lastcpu_old);
1319 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1320 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1321 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1322 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1323 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1324 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1325 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1326 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1327 CP(*ki, *ki32, ki_tracer);
1328 CP(*ki, *ki32, ki_flag2);
1329 CP(*ki, *ki32, ki_fibnum);
1330 CP(*ki, *ki32, ki_cr_flags);
1331 CP(*ki, *ki32, ki_jid);
1332 CP(*ki, *ki32, ki_numthreads);
1333 CP(*ki, *ki32, ki_tid);
1334 CP(*ki, *ki32, ki_pri);
1335 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1336 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1337 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1338 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1339 PTRTRIM_CP(*ki, *ki32, ki_udata);
1340 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1341 CP(*ki, *ki32, ki_sflag);
1342 CP(*ki, *ki32, ki_tdflags);
1347 kern_proc_out_size(struct proc *p, int flags)
1351 PROC_LOCK_ASSERT(p, MA_OWNED);
1353 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1354 #ifdef COMPAT_FREEBSD32
1355 if ((flags & KERN_PROC_MASK32) != 0) {
1356 size += sizeof(struct kinfo_proc32);
1359 size += sizeof(struct kinfo_proc);
1361 #ifdef COMPAT_FREEBSD32
1362 if ((flags & KERN_PROC_MASK32) != 0)
1363 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1366 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1373 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1376 struct kinfo_proc ki;
1377 #ifdef COMPAT_FREEBSD32
1378 struct kinfo_proc32 ki32;
1382 PROC_LOCK_ASSERT(p, MA_OWNED);
1383 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1386 fill_kinfo_proc(p, &ki);
1387 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1388 #ifdef COMPAT_FREEBSD32
1389 if ((flags & KERN_PROC_MASK32) != 0) {
1390 freebsd32_kinfo_proc_out(&ki, &ki32);
1391 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1395 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1398 FOREACH_THREAD_IN_PROC(p, td) {
1399 fill_kinfo_thread(td, &ki, 1);
1400 #ifdef COMPAT_FREEBSD32
1401 if ((flags & KERN_PROC_MASK32) != 0) {
1402 freebsd32_kinfo_proc_out(&ki, &ki32);
1403 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1407 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1418 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1421 struct kinfo_proc ki;
1424 if (req->oldptr == NULL)
1425 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1427 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1428 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1429 error = kern_proc_out(p, &sb, flags);
1430 error2 = sbuf_finish(&sb);
1434 else if (error2 != 0)
1440 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1442 int *name = (int *)arg1;
1443 u_int namelen = arg2;
1445 int flags, doingzomb, oid_number;
1448 oid_number = oidp->oid_number;
1449 if (oid_number != KERN_PROC_ALL &&
1450 (oid_number & KERN_PROC_INC_THREAD) == 0)
1451 flags = KERN_PROC_NOTHREADS;
1454 oid_number &= ~KERN_PROC_INC_THREAD;
1456 #ifdef COMPAT_FREEBSD32
1457 if (req->flags & SCTL_MASK32)
1458 flags |= KERN_PROC_MASK32;
1460 if (oid_number == KERN_PROC_PID) {
1463 error = sysctl_wire_old_buffer(req, 0);
1466 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1468 error = sysctl_out_proc(p, req, flags);
1472 switch (oid_number) {
1477 case KERN_PROC_PROC:
1478 if (namelen != 0 && namelen != 1)
1487 if (req->oldptr == NULL) {
1488 /* overestimate by 5 procs */
1489 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1493 error = sysctl_wire_old_buffer(req, 0);
1497 sx_slock(&allproc_lock);
1498 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1500 p = LIST_FIRST(&allproc);
1502 p = LIST_FIRST(&zombproc);
1503 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
1505 * Skip embryonic processes.
1507 if (p->p_state == PRS_NEW)
1510 KASSERT(p->p_ucred != NULL,
1511 ("process credential is NULL for non-NEW proc"));
1513 * Show a user only appropriate processes.
1515 if (p_cansee(curthread, p)) {
1520 * TODO - make more efficient (see notes below).
1523 switch (oid_number) {
1526 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1532 case KERN_PROC_PGRP:
1533 /* could do this by traversing pgrp */
1534 if (p->p_pgrp == NULL ||
1535 p->p_pgrp->pg_id != (pid_t)name[0]) {
1541 case KERN_PROC_RGID:
1542 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1548 case KERN_PROC_SESSION:
1549 if (p->p_session == NULL ||
1550 p->p_session->s_sid != (pid_t)name[0]) {
1557 if ((p->p_flag & P_CONTROLT) == 0 ||
1558 p->p_session == NULL) {
1562 /* XXX proctree_lock */
1563 SESS_LOCK(p->p_session);
1564 if (p->p_session->s_ttyp == NULL ||
1565 tty_udev(p->p_session->s_ttyp) !=
1567 SESS_UNLOCK(p->p_session);
1571 SESS_UNLOCK(p->p_session);
1575 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1581 case KERN_PROC_RUID:
1582 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1588 case KERN_PROC_PROC:
1596 error = sysctl_out_proc(p, req, flags);
1602 sx_sunlock(&allproc_lock);
1607 pargs_alloc(int len)
1611 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1613 refcount_init(&pa->ar_ref, 1);
1614 pa->ar_length = len;
1619 pargs_free(struct pargs *pa)
1626 pargs_hold(struct pargs *pa)
1631 refcount_acquire(&pa->ar_ref);
1635 pargs_drop(struct pargs *pa)
1640 if (refcount_release(&pa->ar_ref))
1645 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1651 * This may return a short read if the string is shorter than the chunk
1652 * and is aligned at the end of the page, and the following page is not
1655 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1661 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1663 enum proc_vector_type {
1669 #ifdef COMPAT_FREEBSD32
1671 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1672 size_t *vsizep, enum proc_vector_type type)
1674 struct freebsd32_ps_strings pss;
1676 vm_offset_t vptr, ptr;
1677 uint32_t *proc_vector32;
1683 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1684 sizeof(pss)) != sizeof(pss))
1688 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1689 vsize = pss.ps_nargvstr;
1690 if (vsize > ARG_MAX)
1692 size = vsize * sizeof(int32_t);
1695 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1696 vsize = pss.ps_nenvstr;
1697 if (vsize > ARG_MAX)
1699 size = vsize * sizeof(int32_t);
1702 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1703 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1706 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1707 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1710 if (aux.a_type == AT_NULL)
1714 if (aux.a_type != AT_NULL)
1717 size = vsize * sizeof(aux);
1720 KASSERT(0, ("Wrong proc vector type: %d", type));
1723 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1724 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1728 if (type == PROC_AUX) {
1729 *proc_vectorp = (char **)proc_vector32;
1733 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1734 for (i = 0; i < (int)vsize; i++)
1735 proc_vector[i] = PTRIN(proc_vector32[i]);
1736 *proc_vectorp = proc_vector;
1739 free(proc_vector32, M_TEMP);
1745 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1746 size_t *vsizep, enum proc_vector_type type)
1748 struct ps_strings pss;
1750 vm_offset_t vptr, ptr;
1755 #ifdef COMPAT_FREEBSD32
1756 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1757 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1759 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1760 sizeof(pss)) != sizeof(pss))
1764 vptr = (vm_offset_t)pss.ps_argvstr;
1765 vsize = pss.ps_nargvstr;
1766 if (vsize > ARG_MAX)
1768 size = vsize * sizeof(char *);
1771 vptr = (vm_offset_t)pss.ps_envstr;
1772 vsize = pss.ps_nenvstr;
1773 if (vsize > ARG_MAX)
1775 size = vsize * sizeof(char *);
1779 * The aux array is just above env array on the stack. Check
1780 * that the address is naturally aligned.
1782 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1784 #if __ELF_WORD_SIZE == 64
1785 if (vptr % sizeof(uint64_t) != 0)
1787 if (vptr % sizeof(uint32_t) != 0)
1791 * We count the array size reading the aux vectors from the
1792 * stack until AT_NULL vector is returned. So (to keep the code
1793 * simple) we read the process stack twice: the first time here
1794 * to find the size and the second time when copying the vectors
1795 * to the allocated proc_vector.
1797 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1798 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1801 if (aux.a_type == AT_NULL)
1806 * If the PROC_AUXV_MAX entries are iterated over, and we have
1807 * not reached AT_NULL, it is most likely we are reading wrong
1808 * data: either the process doesn't have auxv array or data has
1809 * been modified. Return the error in this case.
1811 if (aux.a_type != AT_NULL)
1814 size = vsize * sizeof(aux);
1817 KASSERT(0, ("Wrong proc vector type: %d", type));
1818 return (EINVAL); /* In case we are built without INVARIANTS. */
1820 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1821 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1822 free(proc_vector, M_TEMP);
1825 *proc_vectorp = proc_vector;
1831 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1834 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1835 enum proc_vector_type type)
1837 size_t done, len, nchr, vsize;
1839 char **proc_vector, *sptr;
1840 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1842 PROC_ASSERT_HELD(p);
1845 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1847 nchr = 2 * (PATH_MAX + ARG_MAX);
1849 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1852 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1854 * The program may have scribbled into its argv array, e.g. to
1855 * remove some arguments. If that has happened, break out
1856 * before trying to read from NULL.
1858 if (proc_vector[i] == NULL)
1860 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1861 error = proc_read_string(td, p, sptr, pss_string,
1862 sizeof(pss_string));
1865 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1866 if (done + len >= nchr)
1867 len = nchr - done - 1;
1868 sbuf_bcat(sb, pss_string, len);
1869 if (len != GET_PS_STRINGS_CHUNK_SZ)
1871 done += GET_PS_STRINGS_CHUNK_SZ;
1873 sbuf_bcat(sb, "", 1);
1877 free(proc_vector, M_TEMP);
1882 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1885 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1889 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1892 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1896 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1902 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1904 #ifdef COMPAT_FREEBSD32
1905 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1906 size = vsize * sizeof(Elf32_Auxinfo);
1909 size = vsize * sizeof(Elf_Auxinfo);
1910 if (sbuf_bcat(sb, auxv, size) != 0)
1918 * This sysctl allows a process to retrieve the argument list or process
1919 * title for another process without groping around in the address space
1920 * of the other process. It also allow a process to set its own "process
1921 * title to a string of its own choice.
1924 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1926 int *name = (int *)arg1;
1927 u_int namelen = arg2;
1928 struct pargs *newpa, *pa;
1931 int flags, error = 0, error2;
1937 pid = (pid_t)name[0];
1939 * If the query is for this process and it is single-threaded, there
1940 * is nobody to modify pargs, thus we can just read.
1943 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
1944 (pa = p->p_args) != NULL)
1945 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
1947 flags = PGET_CANSEE;
1948 if (req->newptr != NULL)
1949 flags |= PGET_ISCURRENT;
1950 error = pget(pid, flags, &p);
1958 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1960 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1963 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1964 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1965 error = proc_getargv(curthread, p, &sb);
1966 error2 = sbuf_finish(&sb);
1969 if (error == 0 && error2 != 0)
1974 if (error != 0 || req->newptr == NULL)
1977 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
1980 if (req->newlen == 0) {
1982 * Clear the argument pointer, so that we'll fetch arguments
1983 * with proc_getargv() until further notice.
1987 newpa = pargs_alloc(req->newlen);
1988 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2003 * This sysctl allows a process to retrieve environment of another process.
2006 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2008 int *name = (int *)arg1;
2009 u_int namelen = arg2;
2017 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2020 if ((p->p_flag & P_SYSTEM) != 0) {
2025 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2026 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2027 error = proc_getenvv(curthread, p, &sb);
2028 error2 = sbuf_finish(&sb);
2031 return (error != 0 ? error : error2);
2035 * This sysctl allows a process to retrieve ELF auxiliary vector of
2039 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2041 int *name = (int *)arg1;
2042 u_int namelen = arg2;
2050 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2053 if ((p->p_flag & P_SYSTEM) != 0) {
2057 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2058 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2059 error = proc_getauxv(curthread, p, &sb);
2060 error2 = sbuf_finish(&sb);
2063 return (error != 0 ? error : error2);
2067 * This sysctl allows a process to retrieve the path of the executable for
2068 * itself or another process.
2071 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2073 pid_t *pidp = (pid_t *)arg1;
2074 unsigned int arglen = arg2;
2077 char *retbuf, *freebuf;
2082 if (*pidp == -1) { /* -1 means this process */
2083 p = req->td->td_proc;
2085 error = pget(*pidp, PGET_CANSEE, &p);
2099 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2103 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2104 free(freebuf, M_TEMP);
2109 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2122 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2125 sv_name = p->p_sysent->sv_name;
2127 return (sysctl_handle_string(oidp, sv_name, 0, req));
2130 #ifdef KINFO_OVMENTRY_SIZE
2131 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2134 #ifdef COMPAT_FREEBSD7
2136 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2138 vm_map_entry_t entry, tmp_entry;
2139 unsigned int last_timestamp;
2140 char *fullpath, *freepath;
2141 struct kinfo_ovmentry *kve;
2151 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2154 vm = vmspace_acquire_ref(p);
2159 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2162 vm_map_lock_read(map);
2163 for (entry = map->header.next; entry != &map->header;
2164 entry = entry->next) {
2165 vm_object_t obj, tobj, lobj;
2168 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2171 bzero(kve, sizeof(*kve));
2172 kve->kve_structsize = sizeof(*kve);
2174 kve->kve_private_resident = 0;
2175 obj = entry->object.vm_object;
2177 VM_OBJECT_RLOCK(obj);
2178 if (obj->shadow_count == 1)
2179 kve->kve_private_resident =
2180 obj->resident_page_count;
2182 kve->kve_resident = 0;
2183 addr = entry->start;
2184 while (addr < entry->end) {
2185 if (pmap_extract(map->pmap, addr))
2186 kve->kve_resident++;
2190 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2192 VM_OBJECT_RLOCK(tobj);
2193 kve->kve_offset += tobj->backing_object_offset;
2196 VM_OBJECT_RUNLOCK(lobj);
2200 kve->kve_start = (void*)entry->start;
2201 kve->kve_end = (void*)entry->end;
2202 kve->kve_offset += (off_t)entry->offset;
2204 if (entry->protection & VM_PROT_READ)
2205 kve->kve_protection |= KVME_PROT_READ;
2206 if (entry->protection & VM_PROT_WRITE)
2207 kve->kve_protection |= KVME_PROT_WRITE;
2208 if (entry->protection & VM_PROT_EXECUTE)
2209 kve->kve_protection |= KVME_PROT_EXEC;
2211 if (entry->eflags & MAP_ENTRY_COW)
2212 kve->kve_flags |= KVME_FLAG_COW;
2213 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2214 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2215 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2216 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2218 last_timestamp = map->timestamp;
2219 vm_map_unlock_read(map);
2221 kve->kve_fileid = 0;
2227 switch (lobj->type) {
2229 kve->kve_type = KVME_TYPE_DEFAULT;
2232 kve->kve_type = KVME_TYPE_VNODE;
2237 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2238 kve->kve_type = KVME_TYPE_VNODE;
2239 if ((lobj->flags & OBJ_TMPFS) != 0) {
2240 vp = lobj->un_pager.swp.swp_tmpfs;
2244 kve->kve_type = KVME_TYPE_SWAP;
2248 kve->kve_type = KVME_TYPE_DEVICE;
2251 kve->kve_type = KVME_TYPE_PHYS;
2254 kve->kve_type = KVME_TYPE_DEAD;
2257 kve->kve_type = KVME_TYPE_SG;
2260 kve->kve_type = KVME_TYPE_UNKNOWN;
2264 VM_OBJECT_RUNLOCK(lobj);
2266 kve->kve_ref_count = obj->ref_count;
2267 kve->kve_shadow_count = obj->shadow_count;
2268 VM_OBJECT_RUNLOCK(obj);
2270 vn_fullpath(curthread, vp, &fullpath,
2272 cred = curthread->td_ucred;
2273 vn_lock(vp, LK_SHARED | LK_RETRY);
2274 if (VOP_GETATTR(vp, &va, cred) == 0) {
2275 kve->kve_fileid = va.va_fileid;
2277 kve->kve_fsid = va.va_fsid;
2282 kve->kve_type = KVME_TYPE_NONE;
2283 kve->kve_ref_count = 0;
2284 kve->kve_shadow_count = 0;
2287 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2288 if (freepath != NULL)
2289 free(freepath, M_TEMP);
2291 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2292 vm_map_lock_read(map);
2295 if (last_timestamp != map->timestamp) {
2296 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2300 vm_map_unlock_read(map);
2306 #endif /* COMPAT_FREEBSD7 */
2308 #ifdef KINFO_VMENTRY_SIZE
2309 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2313 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2314 int *resident_count, bool *super)
2316 vm_object_t obj, tobj;
2319 vm_paddr_t locked_pa;
2320 vm_pindex_t pi, pi_adv, pindex;
2323 *resident_count = 0;
2324 if (vmmap_skip_res_cnt)
2328 obj = entry->object.vm_object;
2329 addr = entry->start;
2331 pi = OFF_TO_IDX(entry->offset);
2332 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2333 if (m_adv != NULL) {
2336 pi_adv = atop(entry->end - addr);
2338 for (tobj = obj;; tobj = tobj->backing_object) {
2339 m = vm_page_find_least(tobj, pindex);
2341 if (m->pindex == pindex)
2343 if (pi_adv > m->pindex - pindex) {
2344 pi_adv = m->pindex - pindex;
2348 if (tobj->backing_object == NULL)
2350 pindex += OFF_TO_IDX(tobj->
2351 backing_object_offset);
2355 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2356 (addr & (pagesizes[1] - 1)) == 0 &&
2357 (pmap_mincore(map->pmap, addr, &locked_pa) &
2358 MINCORE_SUPER) != 0) {
2360 pi_adv = atop(pagesizes[1]);
2363 * We do not test the found page on validity.
2364 * Either the page is busy and being paged in,
2365 * or it was invalidated. The first case
2366 * should be counted as resident, the second
2367 * is not so clear; we do account both.
2371 *resident_count += pi_adv;
2374 PA_UNLOCK_COND(locked_pa);
2378 * Must be called with the process locked and will return unlocked.
2381 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2383 vm_map_entry_t entry, tmp_entry;
2386 vm_object_t obj, tobj, lobj;
2387 char *fullpath, *freepath;
2388 struct kinfo_vmentry *kve;
2393 unsigned int last_timestamp;
2397 PROC_LOCK_ASSERT(p, MA_OWNED);
2401 vm = vmspace_acquire_ref(p);
2406 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2410 vm_map_lock_read(map);
2411 for (entry = map->header.next; entry != &map->header;
2412 entry = entry->next) {
2413 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2417 bzero(kve, sizeof(*kve));
2418 obj = entry->object.vm_object;
2420 for (tobj = obj; tobj != NULL;
2421 tobj = tobj->backing_object) {
2422 VM_OBJECT_RLOCK(tobj);
2423 kve->kve_offset += tobj->backing_object_offset;
2426 if (obj->backing_object == NULL)
2427 kve->kve_private_resident =
2428 obj->resident_page_count;
2429 kern_proc_vmmap_resident(map, entry,
2430 &kve->kve_resident, &super);
2432 kve->kve_flags |= KVME_FLAG_SUPER;
2433 for (tobj = obj; tobj != NULL;
2434 tobj = tobj->backing_object) {
2435 if (tobj != obj && tobj != lobj)
2436 VM_OBJECT_RUNLOCK(tobj);
2442 kve->kve_start = entry->start;
2443 kve->kve_end = entry->end;
2444 kve->kve_offset += entry->offset;
2446 if (entry->protection & VM_PROT_READ)
2447 kve->kve_protection |= KVME_PROT_READ;
2448 if (entry->protection & VM_PROT_WRITE)
2449 kve->kve_protection |= KVME_PROT_WRITE;
2450 if (entry->protection & VM_PROT_EXECUTE)
2451 kve->kve_protection |= KVME_PROT_EXEC;
2453 if (entry->eflags & MAP_ENTRY_COW)
2454 kve->kve_flags |= KVME_FLAG_COW;
2455 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2456 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2457 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2458 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2459 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2460 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2461 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2462 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2463 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2464 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2466 last_timestamp = map->timestamp;
2467 vm_map_unlock_read(map);
2473 switch (lobj->type) {
2475 kve->kve_type = KVME_TYPE_DEFAULT;
2478 kve->kve_type = KVME_TYPE_VNODE;
2483 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2484 kve->kve_type = KVME_TYPE_VNODE;
2485 if ((lobj->flags & OBJ_TMPFS) != 0) {
2486 vp = lobj->un_pager.swp.swp_tmpfs;
2490 kve->kve_type = KVME_TYPE_SWAP;
2494 kve->kve_type = KVME_TYPE_DEVICE;
2497 kve->kve_type = KVME_TYPE_PHYS;
2500 kve->kve_type = KVME_TYPE_DEAD;
2503 kve->kve_type = KVME_TYPE_SG;
2505 case OBJT_MGTDEVICE:
2506 kve->kve_type = KVME_TYPE_MGTDEVICE;
2509 kve->kve_type = KVME_TYPE_UNKNOWN;
2513 VM_OBJECT_RUNLOCK(lobj);
2515 kve->kve_ref_count = obj->ref_count;
2516 kve->kve_shadow_count = obj->shadow_count;
2517 VM_OBJECT_RUNLOCK(obj);
2519 vn_fullpath(curthread, vp, &fullpath,
2521 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2522 cred = curthread->td_ucred;
2523 vn_lock(vp, LK_SHARED | LK_RETRY);
2524 if (VOP_GETATTR(vp, &va, cred) == 0) {
2525 kve->kve_vn_fileid = va.va_fileid;
2526 kve->kve_vn_fsid = va.va_fsid;
2527 kve->kve_vn_fsid_freebsd11 =
2528 kve->kve_vn_fsid; /* truncate */
2530 MAKEIMODE(va.va_type, va.va_mode);
2531 kve->kve_vn_size = va.va_size;
2532 kve->kve_vn_rdev = va.va_rdev;
2533 kve->kve_vn_rdev_freebsd11 =
2534 kve->kve_vn_rdev; /* truncate */
2535 kve->kve_status = KF_ATTR_VALID;
2540 kve->kve_type = KVME_TYPE_NONE;
2541 kve->kve_ref_count = 0;
2542 kve->kve_shadow_count = 0;
2545 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2546 if (freepath != NULL)
2547 free(freepath, M_TEMP);
2549 /* Pack record size down */
2550 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2551 kve->kve_structsize =
2552 offsetof(struct kinfo_vmentry, kve_path) +
2553 strlen(kve->kve_path) + 1;
2555 kve->kve_structsize = sizeof(*kve);
2556 kve->kve_structsize = roundup(kve->kve_structsize,
2559 /* Halt filling and truncate rather than exceeding maxlen */
2560 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2562 vm_map_lock_read(map);
2564 } else if (maxlen != -1)
2565 maxlen -= kve->kve_structsize;
2567 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2569 vm_map_lock_read(map);
2572 if (last_timestamp != map->timestamp) {
2573 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2577 vm_map_unlock_read(map);
2585 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2589 int error, error2, *name;
2592 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2593 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2594 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2599 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2600 error2 = sbuf_finish(&sb);
2602 return (error != 0 ? error : error2);
2605 #if defined(STACK) || defined(DDB)
2607 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2609 struct kinfo_kstack *kkstp;
2610 int error, i, *name, numthreads;
2611 lwpid_t *lwpidarray;
2618 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2622 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2623 st = stack_create(M_WAITOK);
2628 if (lwpidarray != NULL) {
2629 free(lwpidarray, M_TEMP);
2632 numthreads = p->p_numthreads;
2634 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2637 } while (numthreads < p->p_numthreads);
2640 * XXXRW: During the below loop, execve(2) and countless other sorts
2641 * of changes could have taken place. Should we check to see if the
2642 * vmspace has been replaced, or the like, in order to prevent
2643 * giving a snapshot that spans, say, execve(2), with some threads
2644 * before and some after? Among other things, the credentials could
2645 * have changed, in which case the right to extract debug info might
2646 * no longer be assured.
2649 FOREACH_THREAD_IN_PROC(p, td) {
2650 KASSERT(i < numthreads,
2651 ("sysctl_kern_proc_kstack: numthreads"));
2652 lwpidarray[i] = td->td_tid;
2656 for (i = 0; i < numthreads; i++) {
2657 td = thread_find(p, lwpidarray[i]);
2661 bzero(kkstp, sizeof(*kkstp));
2662 (void)sbuf_new(&sb, kkstp->kkst_trace,
2663 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2665 kkstp->kkst_tid = td->td_tid;
2666 if (TD_IS_SWAPPED(td)) {
2667 kkstp->kkst_state = KKST_STATE_SWAPPED;
2668 } else if (TD_IS_RUNNING(td)) {
2669 if (stack_save_td_running(st, td) == 0)
2670 kkstp->kkst_state = KKST_STATE_STACKOK;
2672 kkstp->kkst_state = KKST_STATE_RUNNING;
2674 kkstp->kkst_state = KKST_STATE_STACKOK;
2675 stack_save_td(st, td);
2679 stack_sbuf_print(&sb, st);
2682 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2689 if (lwpidarray != NULL)
2690 free(lwpidarray, M_TEMP);
2692 free(kkstp, M_TEMP);
2698 * This sysctl allows a process to retrieve the full list of groups from
2699 * itself or another process.
2702 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2704 pid_t *pidp = (pid_t *)arg1;
2705 unsigned int arglen = arg2;
2712 if (*pidp == -1) { /* -1 means this process */
2713 p = req->td->td_proc;
2716 error = pget(*pidp, PGET_CANSEE, &p);
2721 cred = crhold(p->p_ucred);
2724 error = SYSCTL_OUT(req, cred->cr_groups,
2725 cred->cr_ngroups * sizeof(gid_t));
2731 * This sysctl allows a process to retrieve or/and set the resource limit for
2735 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2737 int *name = (int *)arg1;
2738 u_int namelen = arg2;
2747 which = (u_int)name[1];
2748 if (which >= RLIM_NLIMITS)
2751 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2754 flags = PGET_HOLD | PGET_NOTWEXIT;
2755 if (req->newptr != NULL)
2756 flags |= PGET_CANDEBUG;
2758 flags |= PGET_CANSEE;
2759 error = pget((pid_t)name[0], flags, &p);
2766 if (req->oldptr != NULL) {
2768 lim_rlimit_proc(p, which, &rlim);
2771 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2778 if (req->newptr != NULL) {
2779 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2781 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2790 * This sysctl allows a process to retrieve ps_strings structure location of
2794 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2796 int *name = (int *)arg1;
2797 u_int namelen = arg2;
2799 vm_offset_t ps_strings;
2801 #ifdef COMPAT_FREEBSD32
2802 uint32_t ps_strings32;
2808 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2811 #ifdef COMPAT_FREEBSD32
2812 if ((req->flags & SCTL_MASK32) != 0) {
2814 * We return 0 if the 32 bit emulation request is for a 64 bit
2817 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2818 PTROUT(p->p_sysent->sv_psstrings) : 0;
2820 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2824 ps_strings = p->p_sysent->sv_psstrings;
2826 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2831 * This sysctl allows a process to retrieve umask of another process.
2834 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2836 int *name = (int *)arg1;
2837 u_int namelen = arg2;
2846 pid = (pid_t)name[0];
2848 if (pid == p->p_pid || pid == 0) {
2849 fd_cmask = p->p_fd->fd_cmask;
2853 error = pget(pid, PGET_WANTREAD, &p);
2857 fd_cmask = p->p_fd->fd_cmask;
2860 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2865 * This sysctl allows a process to set and retrieve binary osreldate of
2869 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2871 int *name = (int *)arg1;
2872 u_int namelen = arg2;
2874 int flags, error, osrel;
2879 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2882 flags = PGET_HOLD | PGET_NOTWEXIT;
2883 if (req->newptr != NULL)
2884 flags |= PGET_CANDEBUG;
2886 flags |= PGET_CANSEE;
2887 error = pget((pid_t)name[0], flags, &p);
2891 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2895 if (req->newptr != NULL) {
2896 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2911 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2913 int *name = (int *)arg1;
2914 u_int namelen = arg2;
2916 struct kinfo_sigtramp kst;
2917 const struct sysentvec *sv;
2919 #ifdef COMPAT_FREEBSD32
2920 struct kinfo_sigtramp32 kst32;
2926 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2930 #ifdef COMPAT_FREEBSD32
2931 if ((req->flags & SCTL_MASK32) != 0) {
2932 bzero(&kst32, sizeof(kst32));
2933 if (SV_PROC_FLAG(p, SV_ILP32)) {
2934 if (sv->sv_sigcode_base != 0) {
2935 kst32.ksigtramp_start = sv->sv_sigcode_base;
2936 kst32.ksigtramp_end = sv->sv_sigcode_base +
2939 kst32.ksigtramp_start = sv->sv_psstrings -
2941 kst32.ksigtramp_end = sv->sv_psstrings;
2945 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2949 bzero(&kst, sizeof(kst));
2950 if (sv->sv_sigcode_base != 0) {
2951 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2952 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2955 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2957 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2960 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2964 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2966 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2967 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2968 "Return entire process table");
2970 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2971 sysctl_kern_proc, "Process table");
2973 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2974 sysctl_kern_proc, "Process table");
2976 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2977 sysctl_kern_proc, "Process table");
2979 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2980 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2982 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2983 sysctl_kern_proc, "Process table");
2985 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2986 sysctl_kern_proc, "Process table");
2988 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2989 sysctl_kern_proc, "Process table");
2991 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2992 sysctl_kern_proc, "Process table");
2994 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2995 sysctl_kern_proc, "Return process table, no threads");
2997 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2998 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2999 sysctl_kern_proc_args, "Process argument list");
3001 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3002 sysctl_kern_proc_env, "Process environment");
3004 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3005 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3007 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3008 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3010 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3011 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3012 "Process syscall vector name (ABI type)");
3014 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3015 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3017 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3018 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3020 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3021 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3023 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3024 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3026 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3027 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3029 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3030 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3032 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3033 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3035 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3036 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3038 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3039 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3040 "Return process table, no threads");
3042 #ifdef COMPAT_FREEBSD7
3043 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3044 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3047 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3048 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3050 #if defined(STACK) || defined(DDB)
3051 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3052 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3055 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3056 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3058 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3059 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3060 "Process resource limits");
3062 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3063 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3064 "Process ps_strings location");
3066 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3067 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3069 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3070 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3071 "Process binary osreldate");
3073 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3074 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3075 "Process signal trampoline location");
3080 * stop_all_proc() purpose is to stop all process which have usermode,
3081 * except current process for obvious reasons. This makes it somewhat
3082 * unreliable when invoked from multithreaded process. The service
3083 * must not be user-callable anyway.
3088 struct proc *cp, *p;
3090 bool restart, seen_stopped, seen_exiting, stopped_some;
3094 sx_xlock(&allproc_lock);
3096 seen_exiting = seen_stopped = stopped_some = restart = false;
3097 LIST_REMOVE(cp, p_list);
3098 LIST_INSERT_HEAD(&allproc, cp, p_list);
3100 p = LIST_NEXT(cp, p_list);
3103 LIST_REMOVE(cp, p_list);
3104 LIST_INSERT_AFTER(p, cp, p_list);
3106 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3110 if ((p->p_flag & P_WEXIT) != 0) {
3111 seen_exiting = true;
3115 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3117 * Stopped processes are tolerated when there
3118 * are no other processes which might continue
3119 * them. P_STOPPED_SINGLE but not
3120 * P_TOTAL_STOP process still has at least one
3123 seen_stopped = true;
3127 sx_xunlock(&allproc_lock);
3129 r = thread_single(p, SINGLE_ALLPROC);
3133 stopped_some = true;
3136 sx_xlock(&allproc_lock);
3138 /* Catch forked children we did not see in iteration. */
3139 if (gen != allproc_gen)
3141 sx_xunlock(&allproc_lock);
3142 if (restart || stopped_some || seen_exiting || seen_stopped) {
3143 kern_yield(PRI_USER);
3149 resume_all_proc(void)
3151 struct proc *cp, *p;
3154 sx_xlock(&allproc_lock);
3156 LIST_REMOVE(cp, p_list);
3157 LIST_INSERT_HEAD(&allproc, cp, p_list);
3159 p = LIST_NEXT(cp, p_list);
3162 LIST_REMOVE(cp, p_list);
3163 LIST_INSERT_AFTER(p, cp, p_list);
3165 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3166 sx_xunlock(&allproc_lock);
3168 thread_single_end(p, SINGLE_ALLPROC);
3171 sx_xlock(&allproc_lock);
3176 /* Did the loop above missed any stopped process ? */
3177 FOREACH_PROC_IN_SYSTEM(p) {
3178 /* No need for proc lock. */
3179 if ((p->p_flag & P_TOTAL_STOP) != 0)
3182 sx_xunlock(&allproc_lock);
3185 /* #define TOTAL_STOP_DEBUG 1 */
3186 #ifdef TOTAL_STOP_DEBUG
3187 volatile static int ap_resume;
3188 #include <sys/mount.h>
3191 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3197 error = sysctl_handle_int(oidp, &val, 0, req);
3198 if (error != 0 || req->newptr == NULL)
3203 while (ap_resume == 0)
3211 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3212 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3213 sysctl_debug_stop_all_proc, "I",