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 proclist zombproc;
128 struct sx __exclusive_cache_line allproc_lock;
129 struct sx __exclusive_cache_line zombproc_lock;
130 struct sx __exclusive_cache_line proctree_lock;
131 struct mtx __exclusive_cache_line ppeers_lock;
132 struct mtx __exclusive_cache_line procid_lock;
133 uma_zone_t proc_zone;
136 * The offset of various fields in struct proc and struct thread.
137 * These are used by kernel debuggers to enumerate kernel threads and
140 const int proc_off_p_pid = offsetof(struct proc, p_pid);
141 const int proc_off_p_comm = offsetof(struct proc, p_comm);
142 const int proc_off_p_list = offsetof(struct proc, p_list);
143 const int proc_off_p_threads = offsetof(struct proc, p_threads);
144 const int thread_off_td_tid = offsetof(struct thread, td_tid);
145 const int thread_off_td_name = offsetof(struct thread, td_name);
146 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
147 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
148 const int thread_off_td_plist = offsetof(struct thread, td_plist);
150 EVENTHANDLER_LIST_DEFINE(process_ctor);
151 EVENTHANDLER_LIST_DEFINE(process_dtor);
152 EVENTHANDLER_LIST_DEFINE(process_init);
153 EVENTHANDLER_LIST_DEFINE(process_fini);
154 EVENTHANDLER_LIST_DEFINE(process_exit);
155 EVENTHANDLER_LIST_DEFINE(process_fork);
156 EVENTHANDLER_LIST_DEFINE(process_exec);
158 int kstack_pages = KSTACK_PAGES;
159 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
160 "Kernel stack size in pages");
161 static int vmmap_skip_res_cnt = 0;
162 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
163 &vmmap_skip_res_cnt, 0,
164 "Skip calculation of the pages resident count in kern.proc.vmmap");
166 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
167 #ifdef COMPAT_FREEBSD32
168 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
172 * Initialize global process hashing structures.
179 sx_init(&allproc_lock, "allproc");
180 sx_init(&zombproc_lock, "zombproc");
181 sx_init(&proctree_lock, "proctree");
182 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
183 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
185 LIST_INIT(&zombproc);
186 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
187 pidhashlock = (pidhash + 1) / 64;
190 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
191 M_PROC, M_WAITOK | M_ZERO);
192 for (i = 0; i < pidhashlock + 1; i++)
193 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
194 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
195 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
196 proc_ctor, proc_dtor, proc_init, proc_fini,
197 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
202 * Prepare a proc for use.
205 proc_ctor(void *mem, int size, void *arg, int flags)
210 p = (struct proc *)mem;
211 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
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);
234 KASSERT((p->p_numthreads == 1),
235 ("bad number of threads in exiting process"));
236 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
238 /* Free all OSD associated to this thread. */
240 td_softdep_cleanup(td);
241 MPASS(td->td_su == NULL);
243 /* Make sure all thread destructors are executed */
244 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
246 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
247 if (p->p_ksi != NULL)
248 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
252 * Initialize type-stable parts of a proc (when newly created).
255 proc_init(void *mem, int size, int flags)
259 p = (struct proc *)mem;
260 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
261 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
262 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
263 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
264 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
265 cv_init(&p->p_pwait, "ppwait");
266 TAILQ_INIT(&p->p_threads); /* all threads in proc */
267 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
268 p->p_stats = pstats_alloc();
274 * UMA should ensure that this function is never called.
275 * Freeing a proc structure would violate type stability.
278 proc_fini(void *mem, int size)
283 p = (struct proc *)mem;
284 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
285 pstats_free(p->p_stats);
286 thread_free(FIRST_THREAD_IN_PROC(p));
287 mtx_destroy(&p->p_mtx);
288 if (p->p_ksi != NULL)
289 ksiginfo_free(p->p_ksi);
291 panic("proc reclaimed");
296 * PID space management.
298 * These bitmaps are used by fork_findpid.
300 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
301 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
302 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
303 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
305 static bitstr_t *proc_id_array[] = {
313 proc_id_set(int type, pid_t id)
316 KASSERT(type >= 0 && type < nitems(proc_id_array),
317 ("invalid type %d\n", type));
318 mtx_lock(&procid_lock);
319 KASSERT(bit_test(proc_id_array[type], id) == 0,
320 ("bit %d already set in %d\n", id, type));
321 bit_set(proc_id_array[type], id);
322 mtx_unlock(&procid_lock);
326 proc_id_set_cond(int type, pid_t id)
329 KASSERT(type >= 0 && type < nitems(proc_id_array),
330 ("invalid type %d\n", type));
331 if (bit_test(proc_id_array[type], id))
333 mtx_lock(&procid_lock);
334 bit_set(proc_id_array[type], id);
335 mtx_unlock(&procid_lock);
339 proc_id_clear(int type, pid_t id)
342 KASSERT(type >= 0 && type < nitems(proc_id_array),
343 ("invalid type %d\n", type));
344 mtx_lock(&procid_lock);
345 KASSERT(bit_test(proc_id_array[type], id) != 0,
346 ("bit %d not set in %d\n", id, type));
347 bit_clear(proc_id_array[type], id);
348 mtx_unlock(&procid_lock);
352 * Is p an inferior of the current process?
355 inferior(struct proc *p)
358 sx_assert(&proctree_lock, SX_LOCKED);
359 PROC_LOCK_ASSERT(p, MA_OWNED);
360 for (; p != curproc; p = proc_realparent(p)) {
368 * Shared lock all the pid hash lists.
371 pidhash_slockall(void)
375 for (i = 0; i < pidhashlock + 1; i++)
376 sx_slock(&pidhashtbl_lock[i]);
380 * Shared unlock all the pid hash lists.
383 pidhash_sunlockall(void)
387 for (i = 0; i < pidhashlock + 1; i++)
388 sx_sunlock(&pidhashtbl_lock[i]);
392 * Similar to pfind_any(), this function finds zombies.
395 pfind_any_locked(pid_t pid)
399 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
400 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
401 if (p->p_pid == pid) {
403 if (p->p_state == PRS_NEW) {
414 * Locate a process by number.
416 * By not returning processes in the PRS_NEW state, we allow callers to avoid
417 * testing for that condition to avoid dereferencing p_ucred, et al.
419 static __always_inline struct proc *
420 _pfind(pid_t pid, bool zombie)
425 if (p->p_pid == pid) {
429 sx_slock(PIDHASHLOCK(pid));
430 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
431 if (p->p_pid == pid) {
433 if (p->p_state == PRS_NEW ||
434 (!zombie && p->p_state == PRS_ZOMBIE)) {
441 sx_sunlock(PIDHASHLOCK(pid));
449 return (_pfind(pid, false));
453 * Same as pfind but allow zombies.
459 return (_pfind(pid, true));
468 sx_slock(&allproc_lock);
469 FOREACH_PROC_IN_SYSTEM(p) {
471 if (p->p_state == PRS_NEW) {
475 FOREACH_THREAD_IN_PROC(p, td) {
476 if (td->td_tid == tid)
482 sx_sunlock(&allproc_lock);
487 * Locate a process group by number.
488 * The caller must hold proctree_lock.
495 sx_assert(&proctree_lock, SX_LOCKED);
497 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
498 if (pgrp->pg_id == pgid) {
507 * Locate process and do additional manipulations, depending on flags.
510 pget(pid_t pid, int flags, struct proc **pp)
516 if (p->p_pid == pid) {
520 if (pid <= PID_MAX) {
521 if ((flags & PGET_NOTWEXIT) == 0)
525 } else if ((flags & PGET_NOTID) == 0) {
530 if ((flags & PGET_CANSEE) != 0) {
531 error = p_cansee(curthread, p);
536 if ((flags & PGET_CANDEBUG) != 0) {
537 error = p_candebug(curthread, p);
541 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
545 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
549 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
551 * XXXRW: Not clear ESRCH is the right error during proc
557 if ((flags & PGET_HOLD) != 0) {
569 * Create a new process group.
570 * pgid must be equal to the pid of p.
571 * Begin a new session if required.
574 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
577 sx_assert(&proctree_lock, SX_XLOCKED);
579 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
580 KASSERT(p->p_pid == pgid,
581 ("enterpgrp: new pgrp and pid != pgid"));
582 KASSERT(pgfind(pgid) == NULL,
583 ("enterpgrp: pgrp with pgid exists"));
584 KASSERT(!SESS_LEADER(p),
585 ("enterpgrp: session leader attempted setpgrp"));
587 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
593 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
595 p->p_flag &= ~P_CONTROLT;
599 sess->s_sid = p->p_pid;
600 proc_id_set(PROC_ID_SESSION, p->p_pid);
601 refcount_init(&sess->s_count, 1);
602 sess->s_ttyvp = NULL;
603 sess->s_ttydp = NULL;
605 bcopy(p->p_session->s_login, sess->s_login,
606 sizeof(sess->s_login));
607 pgrp->pg_session = sess;
608 KASSERT(p == curproc,
609 ("enterpgrp: mksession and p != curproc"));
611 pgrp->pg_session = p->p_session;
612 sess_hold(pgrp->pg_session);
616 proc_id_set(PROC_ID_GROUP, p->p_pid);
617 LIST_INIT(&pgrp->pg_members);
620 * As we have an exclusive lock of proctree_lock,
621 * this should not deadlock.
623 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
625 SLIST_INIT(&pgrp->pg_sigiolst);
628 doenterpgrp(p, pgrp);
634 * Move p to an existing process group
637 enterthispgrp(struct proc *p, struct pgrp *pgrp)
640 sx_assert(&proctree_lock, SX_XLOCKED);
641 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
642 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
643 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
644 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
645 KASSERT(pgrp->pg_session == p->p_session,
646 ("%s: pgrp's session %p, p->p_session %p.\n",
650 KASSERT(pgrp != p->p_pgrp,
651 ("%s: p belongs to pgrp.", __func__));
653 doenterpgrp(p, pgrp);
659 * Move p to a process group
662 doenterpgrp(struct proc *p, struct pgrp *pgrp)
664 struct pgrp *savepgrp;
666 sx_assert(&proctree_lock, SX_XLOCKED);
667 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
668 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
669 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
670 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
672 savepgrp = p->p_pgrp;
675 * Adjust eligibility of affected pgrps to participate in job control.
676 * Increment eligibility counts before decrementing, otherwise we
677 * could reach 0 spuriously during the first call.
680 fixjobc(p, p->p_pgrp, 0);
685 LIST_REMOVE(p, p_pglist);
688 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
689 PGRP_UNLOCK(savepgrp);
691 if (LIST_EMPTY(&savepgrp->pg_members))
696 * remove process from process group
699 leavepgrp(struct proc *p)
701 struct pgrp *savepgrp;
703 sx_assert(&proctree_lock, SX_XLOCKED);
704 savepgrp = p->p_pgrp;
707 LIST_REMOVE(p, p_pglist);
710 PGRP_UNLOCK(savepgrp);
711 if (LIST_EMPTY(&savepgrp->pg_members))
717 * delete a process group
720 pgdelete(struct pgrp *pgrp)
722 struct session *savesess;
725 sx_assert(&proctree_lock, SX_XLOCKED);
726 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
727 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
730 * Reset any sigio structures pointing to us as a result of
731 * F_SETOWN with our pgid.
733 funsetownlst(&pgrp->pg_sigiolst);
736 tp = pgrp->pg_session->s_ttyp;
737 LIST_REMOVE(pgrp, pg_hash);
738 savesess = pgrp->pg_session;
741 /* Remove the reference to the pgrp before deallocating it. */
744 tty_rel_pgrp(tp, pgrp);
747 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
748 mtx_destroy(&pgrp->pg_mtx);
750 sess_release(savesess);
754 pgadjustjobc(struct pgrp *pgrp, int entering)
762 if (pgrp->pg_jobc == 0)
769 * Adjust pgrp jobc counters when specified process changes process group.
770 * We count the number of processes in each process group that "qualify"
771 * the group for terminal job control (those with a parent in a different
772 * process group of the same session). If that count reaches zero, the
773 * process group becomes orphaned. Check both the specified process'
774 * process group and that of its children.
775 * entering == 0 => p is leaving specified group.
776 * entering == 1 => p is entering specified group.
779 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
781 struct pgrp *hispgrp;
782 struct session *mysession;
785 sx_assert(&proctree_lock, SX_LOCKED);
786 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
787 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
788 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
791 * Check p's parent to see whether p qualifies its own process
792 * group; if so, adjust count for p's process group.
794 mysession = pgrp->pg_session;
795 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
796 hispgrp->pg_session == mysession)
797 pgadjustjobc(pgrp, entering);
800 * Check this process' children to see whether they qualify
801 * their process groups; if so, adjust counts for children's
804 LIST_FOREACH(q, &p->p_children, p_sibling) {
806 if (hispgrp == pgrp ||
807 hispgrp->pg_session != mysession)
809 if (q->p_state == PRS_ZOMBIE)
811 pgadjustjobc(hispgrp, entering);
824 MPASS(p->p_flag & P_WEXIT);
826 * Do a quick check to see if there is anything to do with the
827 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
830 if (!SESS_LEADER(p) &&
831 (p->p_pgrp == p->p_pptr->p_pgrp) &&
832 LIST_EMPTY(&p->p_children)) {
838 sx_xlock(&proctree_lock);
839 if (SESS_LEADER(p)) {
843 * s_ttyp is not zero'd; we use this to indicate that
844 * the session once had a controlling terminal. (for
845 * logging and informational purposes)
856 * Signal foreground pgrp and revoke access to
857 * controlling terminal if it has not been revoked
860 * Because the TTY may have been revoked in the mean
861 * time and could already have a new session associated
862 * with it, make sure we don't send a SIGHUP to a
863 * foreground process group that does not belong to this
869 if (tp->t_session == sp)
870 tty_signal_pgrp(tp, SIGHUP);
875 sx_xunlock(&proctree_lock);
876 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
877 VOP_REVOKE(ttyvp, REVOKEALL);
878 VOP_UNLOCK(ttyvp, 0);
881 sx_xlock(&proctree_lock);
884 fixjobc(p, p->p_pgrp, 0);
885 sx_xunlock(&proctree_lock);
889 * A process group has become orphaned;
890 * if there are any stopped processes in the group,
891 * hang-up all process in that group.
894 orphanpg(struct pgrp *pg)
898 PGRP_LOCK_ASSERT(pg, MA_OWNED);
900 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
902 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
904 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
906 kern_psignal(p, SIGHUP);
907 kern_psignal(p, SIGCONT);
917 sess_hold(struct session *s)
920 refcount_acquire(&s->s_count);
924 sess_release(struct session *s)
927 if (refcount_release(&s->s_count)) {
928 if (s->s_ttyp != NULL) {
930 tty_rel_sess(s->s_ttyp, s);
932 proc_id_clear(PROC_ID_SESSION, s->s_sid);
933 mtx_destroy(&s->s_mtx);
940 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
946 for (i = 0; i <= pgrphash; i++) {
947 if (!LIST_EMPTY(&pgrphashtbl[i])) {
948 printf("\tindx %d\n", i);
949 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
951 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
952 (void *)pgrp, (long)pgrp->pg_id,
953 (void *)pgrp->pg_session,
954 pgrp->pg_session->s_count,
955 (void *)LIST_FIRST(&pgrp->pg_members));
956 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
957 printf("\t\tpid %ld addr %p pgrp %p\n",
958 (long)p->p_pid, (void *)p,
968 * Calculate the kinfo_proc members which contain process-wide
970 * Must be called with the target process locked.
973 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
977 PROC_LOCK_ASSERT(p, MA_OWNED);
981 FOREACH_THREAD_IN_PROC(p, td) {
983 kp->ki_pctcpu += sched_pctcpu(td);
984 kp->ki_estcpu += sched_estcpu(td);
990 * Clear kinfo_proc and fill in any information that is common
991 * to all threads in the process.
992 * Must be called with the target process locked.
995 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1002 struct timeval boottime;
1004 PROC_LOCK_ASSERT(p, MA_OWNED);
1005 bzero(kp, sizeof(*kp));
1007 kp->ki_structsize = sizeof(*kp);
1009 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1010 kp->ki_args = p->p_args;
1011 kp->ki_textvp = p->p_textvp;
1013 kp->ki_tracep = p->p_tracevp;
1014 kp->ki_traceflag = p->p_traceflag;
1016 kp->ki_fd = p->p_fd;
1017 kp->ki_vmspace = p->p_vmspace;
1018 kp->ki_flag = p->p_flag;
1019 kp->ki_flag2 = p->p_flag2;
1022 kp->ki_uid = cred->cr_uid;
1023 kp->ki_ruid = cred->cr_ruid;
1024 kp->ki_svuid = cred->cr_svuid;
1025 kp->ki_cr_flags = 0;
1026 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1027 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1028 /* XXX bde doesn't like KI_NGROUPS */
1029 if (cred->cr_ngroups > KI_NGROUPS) {
1030 kp->ki_ngroups = KI_NGROUPS;
1031 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1033 kp->ki_ngroups = cred->cr_ngroups;
1034 bcopy(cred->cr_groups, kp->ki_groups,
1035 kp->ki_ngroups * sizeof(gid_t));
1036 kp->ki_rgid = cred->cr_rgid;
1037 kp->ki_svgid = cred->cr_svgid;
1038 /* If jailed(cred), emulate the old P_JAILED flag. */
1040 kp->ki_flag |= P_JAILED;
1041 /* If inside the jail, use 0 as a jail ID. */
1042 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1043 kp->ki_jid = cred->cr_prison->pr_id;
1045 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1046 sizeof(kp->ki_loginclass));
1050 mtx_lock(&ps->ps_mtx);
1051 kp->ki_sigignore = ps->ps_sigignore;
1052 kp->ki_sigcatch = ps->ps_sigcatch;
1053 mtx_unlock(&ps->ps_mtx);
1055 if (p->p_state != PRS_NEW &&
1056 p->p_state != PRS_ZOMBIE &&
1057 p->p_vmspace != NULL) {
1058 struct vmspace *vm = p->p_vmspace;
1060 kp->ki_size = vm->vm_map.size;
1061 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1062 FOREACH_THREAD_IN_PROC(p, td0) {
1063 if (!TD_IS_SWAPPED(td0))
1064 kp->ki_rssize += td0->td_kstack_pages;
1066 kp->ki_swrss = vm->vm_swrss;
1067 kp->ki_tsize = vm->vm_tsize;
1068 kp->ki_dsize = vm->vm_dsize;
1069 kp->ki_ssize = vm->vm_ssize;
1070 } else if (p->p_state == PRS_ZOMBIE)
1071 kp->ki_stat = SZOMB;
1072 if (kp->ki_flag & P_INMEM)
1073 kp->ki_sflag = PS_INMEM;
1076 /* Calculate legacy swtime as seconds since 'swtick'. */
1077 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1078 kp->ki_pid = p->p_pid;
1079 kp->ki_nice = p->p_nice;
1080 kp->ki_fibnum = p->p_fibnum;
1081 kp->ki_start = p->p_stats->p_start;
1082 getboottime(&boottime);
1083 timevaladd(&kp->ki_start, &boottime);
1085 rufetch(p, &kp->ki_rusage);
1086 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1087 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1089 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1090 /* Some callers want child times in a single value. */
1091 kp->ki_childtime = kp->ki_childstime;
1092 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1094 FOREACH_THREAD_IN_PROC(p, td0)
1095 kp->ki_cow += td0->td_cow;
1099 kp->ki_pgid = p->p_pgrp->pg_id;
1100 kp->ki_jobc = p->p_pgrp->pg_jobc;
1101 sp = p->p_pgrp->pg_session;
1104 kp->ki_sid = sp->s_sid;
1106 strlcpy(kp->ki_login, sp->s_login,
1107 sizeof(kp->ki_login));
1109 kp->ki_kiflag |= KI_CTTY;
1111 kp->ki_kiflag |= KI_SLEADER;
1112 /* XXX proctree_lock */
1117 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1118 kp->ki_tdev = tty_udev(tp);
1119 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1120 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1122 kp->ki_tsid = tp->t_session->s_sid;
1124 kp->ki_tdev = NODEV;
1125 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1127 if (p->p_comm[0] != '\0')
1128 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1129 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1130 p->p_sysent->sv_name[0] != '\0')
1131 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1132 kp->ki_siglist = p->p_siglist;
1133 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1134 kp->ki_acflag = p->p_acflag;
1135 kp->ki_lock = p->p_lock;
1137 kp->ki_ppid = p->p_oppid;
1138 if (p->p_flag & P_TRACED)
1139 kp->ki_tracer = p->p_pptr->p_pid;
1144 * Fill in information that is thread specific. Must be called with
1145 * target process locked. If 'preferthread' is set, overwrite certain
1146 * process-related fields that are maintained for both threads and
1150 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1156 PROC_LOCK_ASSERT(p, MA_OWNED);
1161 if (td->td_wmesg != NULL)
1162 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1164 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1165 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1166 sizeof(kp->ki_tdname)) {
1167 strlcpy(kp->ki_moretdname,
1168 td->td_name + sizeof(kp->ki_tdname) - 1,
1169 sizeof(kp->ki_moretdname));
1171 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1173 if (TD_ON_LOCK(td)) {
1174 kp->ki_kiflag |= KI_LOCKBLOCK;
1175 strlcpy(kp->ki_lockname, td->td_lockname,
1176 sizeof(kp->ki_lockname));
1178 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1179 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1182 if (p->p_state == PRS_NORMAL) { /* approximate. */
1183 if (TD_ON_RUNQ(td) ||
1185 TD_IS_RUNNING(td)) {
1187 } else if (P_SHOULDSTOP(p)) {
1188 kp->ki_stat = SSTOP;
1189 } else if (TD_IS_SLEEPING(td)) {
1190 kp->ki_stat = SSLEEP;
1191 } else if (TD_ON_LOCK(td)) {
1192 kp->ki_stat = SLOCK;
1194 kp->ki_stat = SWAIT;
1196 } else if (p->p_state == PRS_ZOMBIE) {
1197 kp->ki_stat = SZOMB;
1202 /* Things in the thread */
1203 kp->ki_wchan = td->td_wchan;
1204 kp->ki_pri.pri_level = td->td_priority;
1205 kp->ki_pri.pri_native = td->td_base_pri;
1208 * Note: legacy fields; clamp at the old NOCPU value and/or
1209 * the maximum u_char CPU value.
1211 if (td->td_lastcpu == NOCPU)
1212 kp->ki_lastcpu_old = NOCPU_OLD;
1213 else if (td->td_lastcpu > MAXCPU_OLD)
1214 kp->ki_lastcpu_old = MAXCPU_OLD;
1216 kp->ki_lastcpu_old = td->td_lastcpu;
1218 if (td->td_oncpu == NOCPU)
1219 kp->ki_oncpu_old = NOCPU_OLD;
1220 else if (td->td_oncpu > MAXCPU_OLD)
1221 kp->ki_oncpu_old = MAXCPU_OLD;
1223 kp->ki_oncpu_old = td->td_oncpu;
1225 kp->ki_lastcpu = td->td_lastcpu;
1226 kp->ki_oncpu = td->td_oncpu;
1227 kp->ki_tdflags = td->td_flags;
1228 kp->ki_tid = td->td_tid;
1229 kp->ki_numthreads = p->p_numthreads;
1230 kp->ki_pcb = td->td_pcb;
1231 kp->ki_kstack = (void *)td->td_kstack;
1232 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1233 kp->ki_pri.pri_class = td->td_pri_class;
1234 kp->ki_pri.pri_user = td->td_user_pri;
1237 rufetchtd(td, &kp->ki_rusage);
1238 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1239 kp->ki_pctcpu = sched_pctcpu(td);
1240 kp->ki_estcpu = sched_estcpu(td);
1241 kp->ki_cow = td->td_cow;
1244 /* We can't get this anymore but ps etc never used it anyway. */
1248 kp->ki_siglist = td->td_siglist;
1249 kp->ki_sigmask = td->td_sigmask;
1256 * Fill in a kinfo_proc structure for the specified process.
1257 * Must be called with the target process locked.
1260 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1263 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1265 fill_kinfo_proc_only(p, kp);
1266 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1267 fill_kinfo_aggregate(p, kp);
1274 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1278 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1281 pstats_fork(struct pstats *src, struct pstats *dst)
1284 bzero(&dst->pstat_startzero,
1285 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1286 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1287 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1291 pstats_free(struct pstats *ps)
1294 free(ps, M_SUBPROC);
1298 * Locate a zombie process by number
1305 sx_slock(&zombproc_lock);
1306 LIST_FOREACH(p, &zombproc, p_list) {
1307 if (p->p_pid == pid) {
1312 sx_sunlock(&zombproc_lock);
1316 #ifdef COMPAT_FREEBSD32
1319 * This function is typically used to copy out the kernel address, so
1320 * it can be replaced by assignment of zero.
1322 static inline uint32_t
1323 ptr32_trim(void *ptr)
1327 uptr = (uintptr_t)ptr;
1328 return ((uptr > UINT_MAX) ? 0 : uptr);
1331 #define PTRTRIM_CP(src,dst,fld) \
1332 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1335 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1339 bzero(ki32, sizeof(struct kinfo_proc32));
1340 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1341 CP(*ki, *ki32, ki_layout);
1342 PTRTRIM_CP(*ki, *ki32, ki_args);
1343 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1344 PTRTRIM_CP(*ki, *ki32, ki_addr);
1345 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1346 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1347 PTRTRIM_CP(*ki, *ki32, ki_fd);
1348 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1349 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1350 CP(*ki, *ki32, ki_pid);
1351 CP(*ki, *ki32, ki_ppid);
1352 CP(*ki, *ki32, ki_pgid);
1353 CP(*ki, *ki32, ki_tpgid);
1354 CP(*ki, *ki32, ki_sid);
1355 CP(*ki, *ki32, ki_tsid);
1356 CP(*ki, *ki32, ki_jobc);
1357 CP(*ki, *ki32, ki_tdev);
1358 CP(*ki, *ki32, ki_tdev_freebsd11);
1359 CP(*ki, *ki32, ki_siglist);
1360 CP(*ki, *ki32, ki_sigmask);
1361 CP(*ki, *ki32, ki_sigignore);
1362 CP(*ki, *ki32, ki_sigcatch);
1363 CP(*ki, *ki32, ki_uid);
1364 CP(*ki, *ki32, ki_ruid);
1365 CP(*ki, *ki32, ki_svuid);
1366 CP(*ki, *ki32, ki_rgid);
1367 CP(*ki, *ki32, ki_svgid);
1368 CP(*ki, *ki32, ki_ngroups);
1369 for (i = 0; i < KI_NGROUPS; i++)
1370 CP(*ki, *ki32, ki_groups[i]);
1371 CP(*ki, *ki32, ki_size);
1372 CP(*ki, *ki32, ki_rssize);
1373 CP(*ki, *ki32, ki_swrss);
1374 CP(*ki, *ki32, ki_tsize);
1375 CP(*ki, *ki32, ki_dsize);
1376 CP(*ki, *ki32, ki_ssize);
1377 CP(*ki, *ki32, ki_xstat);
1378 CP(*ki, *ki32, ki_acflag);
1379 CP(*ki, *ki32, ki_pctcpu);
1380 CP(*ki, *ki32, ki_estcpu);
1381 CP(*ki, *ki32, ki_slptime);
1382 CP(*ki, *ki32, ki_swtime);
1383 CP(*ki, *ki32, ki_cow);
1384 CP(*ki, *ki32, ki_runtime);
1385 TV_CP(*ki, *ki32, ki_start);
1386 TV_CP(*ki, *ki32, ki_childtime);
1387 CP(*ki, *ki32, ki_flag);
1388 CP(*ki, *ki32, ki_kiflag);
1389 CP(*ki, *ki32, ki_traceflag);
1390 CP(*ki, *ki32, ki_stat);
1391 CP(*ki, *ki32, ki_nice);
1392 CP(*ki, *ki32, ki_lock);
1393 CP(*ki, *ki32, ki_rqindex);
1394 CP(*ki, *ki32, ki_oncpu);
1395 CP(*ki, *ki32, ki_lastcpu);
1397 /* XXX TODO: wrap cpu value as appropriate */
1398 CP(*ki, *ki32, ki_oncpu_old);
1399 CP(*ki, *ki32, ki_lastcpu_old);
1401 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1402 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1403 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1404 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1405 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1406 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1407 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1408 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1409 CP(*ki, *ki32, ki_tracer);
1410 CP(*ki, *ki32, ki_flag2);
1411 CP(*ki, *ki32, ki_fibnum);
1412 CP(*ki, *ki32, ki_cr_flags);
1413 CP(*ki, *ki32, ki_jid);
1414 CP(*ki, *ki32, ki_numthreads);
1415 CP(*ki, *ki32, ki_tid);
1416 CP(*ki, *ki32, ki_pri);
1417 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1418 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1419 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1420 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1421 PTRTRIM_CP(*ki, *ki32, ki_udata);
1422 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1423 CP(*ki, *ki32, ki_sflag);
1424 CP(*ki, *ki32, ki_tdflags);
1429 kern_proc_out_size(struct proc *p, int flags)
1433 PROC_LOCK_ASSERT(p, MA_OWNED);
1435 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1436 #ifdef COMPAT_FREEBSD32
1437 if ((flags & KERN_PROC_MASK32) != 0) {
1438 size += sizeof(struct kinfo_proc32);
1441 size += sizeof(struct kinfo_proc);
1443 #ifdef COMPAT_FREEBSD32
1444 if ((flags & KERN_PROC_MASK32) != 0)
1445 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1448 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1455 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1458 struct kinfo_proc ki;
1459 #ifdef COMPAT_FREEBSD32
1460 struct kinfo_proc32 ki32;
1464 PROC_LOCK_ASSERT(p, MA_OWNED);
1465 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1468 fill_kinfo_proc(p, &ki);
1469 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1470 #ifdef COMPAT_FREEBSD32
1471 if ((flags & KERN_PROC_MASK32) != 0) {
1472 freebsd32_kinfo_proc_out(&ki, &ki32);
1473 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1477 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1480 FOREACH_THREAD_IN_PROC(p, td) {
1481 fill_kinfo_thread(td, &ki, 1);
1482 #ifdef COMPAT_FREEBSD32
1483 if ((flags & KERN_PROC_MASK32) != 0) {
1484 freebsd32_kinfo_proc_out(&ki, &ki32);
1485 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1489 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1500 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1503 struct kinfo_proc ki;
1506 if (req->oldptr == NULL)
1507 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1509 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1510 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1511 error = kern_proc_out(p, &sb, flags);
1512 error2 = sbuf_finish(&sb);
1516 else if (error2 != 0)
1522 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1527 for (i = 0; i < pidhashlock + 1; i++) {
1528 sx_slock(&pidhashtbl_lock[i]);
1529 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1530 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1531 if (p->p_state == PRS_NEW)
1533 error = cb(p, cbarg);
1534 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1536 sx_sunlock(&pidhashtbl_lock[i]);
1541 sx_sunlock(&pidhashtbl_lock[i]);
1546 struct kern_proc_out_args {
1547 struct sysctl_req *req;
1554 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1556 struct kern_proc_out_args *arg = origarg;
1557 int *name = arg->name;
1558 int oid_number = arg->oid_number;
1559 int flags = arg->flags;
1560 struct sysctl_req *req = arg->req;
1565 KASSERT(p->p_ucred != NULL,
1566 ("process credential is NULL for non-NEW proc"));
1568 * Show a user only appropriate processes.
1570 if (p_cansee(curthread, p))
1573 * TODO - make more efficient (see notes below).
1576 switch (oid_number) {
1579 if (p->p_ucred->cr_gid != (gid_t)name[0])
1583 case KERN_PROC_PGRP:
1584 /* could do this by traversing pgrp */
1585 if (p->p_pgrp == NULL ||
1586 p->p_pgrp->pg_id != (pid_t)name[0])
1590 case KERN_PROC_RGID:
1591 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1595 case KERN_PROC_SESSION:
1596 if (p->p_session == NULL ||
1597 p->p_session->s_sid != (pid_t)name[0])
1602 if ((p->p_flag & P_CONTROLT) == 0 ||
1603 p->p_session == NULL)
1605 /* XXX proctree_lock */
1606 SESS_LOCK(p->p_session);
1607 if (p->p_session->s_ttyp == NULL ||
1608 tty_udev(p->p_session->s_ttyp) !=
1610 SESS_UNLOCK(p->p_session);
1613 SESS_UNLOCK(p->p_session);
1617 if (p->p_ucred->cr_uid != (uid_t)name[0])
1621 case KERN_PROC_RUID:
1622 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1626 case KERN_PROC_PROC:
1633 error = sysctl_out_proc(p, req, flags);
1634 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1642 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1644 struct kern_proc_out_args iterarg;
1645 int *name = (int *)arg1;
1646 u_int namelen = arg2;
1648 int flags, oid_number;
1651 oid_number = oidp->oid_number;
1652 if (oid_number != KERN_PROC_ALL &&
1653 (oid_number & KERN_PROC_INC_THREAD) == 0)
1654 flags = KERN_PROC_NOTHREADS;
1657 oid_number &= ~KERN_PROC_INC_THREAD;
1659 #ifdef COMPAT_FREEBSD32
1660 if (req->flags & SCTL_MASK32)
1661 flags |= KERN_PROC_MASK32;
1663 if (oid_number == KERN_PROC_PID) {
1666 error = sysctl_wire_old_buffer(req, 0);
1669 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1671 error = sysctl_out_proc(p, req, flags);
1675 switch (oid_number) {
1680 case KERN_PROC_PROC:
1681 if (namelen != 0 && namelen != 1)
1690 if (req->oldptr == NULL) {
1691 /* overestimate by 5 procs */
1692 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1696 error = sysctl_wire_old_buffer(req, 0);
1700 iterarg.flags = flags;
1701 iterarg.oid_number = oid_number;
1703 iterarg.name = name;
1704 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1709 pargs_alloc(int len)
1713 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1715 refcount_init(&pa->ar_ref, 1);
1716 pa->ar_length = len;
1721 pargs_free(struct pargs *pa)
1728 pargs_hold(struct pargs *pa)
1733 refcount_acquire(&pa->ar_ref);
1737 pargs_drop(struct pargs *pa)
1742 if (refcount_release(&pa->ar_ref))
1747 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1753 * This may return a short read if the string is shorter than the chunk
1754 * and is aligned at the end of the page, and the following page is not
1757 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1763 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1765 enum proc_vector_type {
1771 #ifdef COMPAT_FREEBSD32
1773 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1774 size_t *vsizep, enum proc_vector_type type)
1776 struct freebsd32_ps_strings pss;
1778 vm_offset_t vptr, ptr;
1779 uint32_t *proc_vector32;
1785 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1786 sizeof(pss)) != sizeof(pss))
1790 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1791 vsize = pss.ps_nargvstr;
1792 if (vsize > ARG_MAX)
1794 size = vsize * sizeof(int32_t);
1797 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1798 vsize = pss.ps_nenvstr;
1799 if (vsize > ARG_MAX)
1801 size = vsize * sizeof(int32_t);
1804 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1805 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1808 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1809 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1812 if (aux.a_type == AT_NULL)
1816 if (aux.a_type != AT_NULL)
1819 size = vsize * sizeof(aux);
1822 KASSERT(0, ("Wrong proc vector type: %d", type));
1825 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1826 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1830 if (type == PROC_AUX) {
1831 *proc_vectorp = (char **)proc_vector32;
1835 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1836 for (i = 0; i < (int)vsize; i++)
1837 proc_vector[i] = PTRIN(proc_vector32[i]);
1838 *proc_vectorp = proc_vector;
1841 free(proc_vector32, M_TEMP);
1847 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1848 size_t *vsizep, enum proc_vector_type type)
1850 struct ps_strings pss;
1852 vm_offset_t vptr, ptr;
1857 #ifdef COMPAT_FREEBSD32
1858 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1859 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1861 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1862 sizeof(pss)) != sizeof(pss))
1866 vptr = (vm_offset_t)pss.ps_argvstr;
1867 vsize = pss.ps_nargvstr;
1868 if (vsize > ARG_MAX)
1870 size = vsize * sizeof(char *);
1873 vptr = (vm_offset_t)pss.ps_envstr;
1874 vsize = pss.ps_nenvstr;
1875 if (vsize > ARG_MAX)
1877 size = vsize * sizeof(char *);
1881 * The aux array is just above env array on the stack. Check
1882 * that the address is naturally aligned.
1884 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1886 #if __ELF_WORD_SIZE == 64
1887 if (vptr % sizeof(uint64_t) != 0)
1889 if (vptr % sizeof(uint32_t) != 0)
1893 * We count the array size reading the aux vectors from the
1894 * stack until AT_NULL vector is returned. So (to keep the code
1895 * simple) we read the process stack twice: the first time here
1896 * to find the size and the second time when copying the vectors
1897 * to the allocated proc_vector.
1899 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1900 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1903 if (aux.a_type == AT_NULL)
1908 * If the PROC_AUXV_MAX entries are iterated over, and we have
1909 * not reached AT_NULL, it is most likely we are reading wrong
1910 * data: either the process doesn't have auxv array or data has
1911 * been modified. Return the error in this case.
1913 if (aux.a_type != AT_NULL)
1916 size = vsize * sizeof(aux);
1919 KASSERT(0, ("Wrong proc vector type: %d", type));
1920 return (EINVAL); /* In case we are built without INVARIANTS. */
1922 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1923 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1924 free(proc_vector, M_TEMP);
1927 *proc_vectorp = proc_vector;
1933 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1936 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1937 enum proc_vector_type type)
1939 size_t done, len, nchr, vsize;
1941 char **proc_vector, *sptr;
1942 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1944 PROC_ASSERT_HELD(p);
1947 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1949 nchr = 2 * (PATH_MAX + ARG_MAX);
1951 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1954 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1956 * The program may have scribbled into its argv array, e.g. to
1957 * remove some arguments. If that has happened, break out
1958 * before trying to read from NULL.
1960 if (proc_vector[i] == NULL)
1962 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1963 error = proc_read_string(td, p, sptr, pss_string,
1964 sizeof(pss_string));
1967 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1968 if (done + len >= nchr)
1969 len = nchr - done - 1;
1970 sbuf_bcat(sb, pss_string, len);
1971 if (len != GET_PS_STRINGS_CHUNK_SZ)
1973 done += GET_PS_STRINGS_CHUNK_SZ;
1975 sbuf_bcat(sb, "", 1);
1979 free(proc_vector, M_TEMP);
1984 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1987 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1991 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1994 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1998 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2004 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2006 #ifdef COMPAT_FREEBSD32
2007 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2008 size = vsize * sizeof(Elf32_Auxinfo);
2011 size = vsize * sizeof(Elf_Auxinfo);
2012 if (sbuf_bcat(sb, auxv, size) != 0)
2020 * This sysctl allows a process to retrieve the argument list or process
2021 * title for another process without groping around in the address space
2022 * of the other process. It also allow a process to set its own "process
2023 * title to a string of its own choice.
2026 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2028 int *name = (int *)arg1;
2029 u_int namelen = arg2;
2030 struct pargs *newpa, *pa;
2033 int flags, error = 0, error2;
2039 pid = (pid_t)name[0];
2041 * If the query is for this process and it is single-threaded, there
2042 * is nobody to modify pargs, thus we can just read.
2045 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2046 (pa = p->p_args) != NULL)
2047 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2049 flags = PGET_CANSEE;
2050 if (req->newptr != NULL)
2051 flags |= PGET_ISCURRENT;
2052 error = pget(pid, flags, &p);
2060 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2062 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2065 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2066 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2067 error = proc_getargv(curthread, p, &sb);
2068 error2 = sbuf_finish(&sb);
2071 if (error == 0 && error2 != 0)
2076 if (error != 0 || req->newptr == NULL)
2079 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2082 if (req->newlen == 0) {
2084 * Clear the argument pointer, so that we'll fetch arguments
2085 * with proc_getargv() until further notice.
2089 newpa = pargs_alloc(req->newlen);
2090 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2105 * This sysctl allows a process to retrieve environment of another process.
2108 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2110 int *name = (int *)arg1;
2111 u_int namelen = arg2;
2119 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2122 if ((p->p_flag & P_SYSTEM) != 0) {
2127 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2128 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2129 error = proc_getenvv(curthread, p, &sb);
2130 error2 = sbuf_finish(&sb);
2133 return (error != 0 ? error : error2);
2137 * This sysctl allows a process to retrieve ELF auxiliary vector of
2141 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2143 int *name = (int *)arg1;
2144 u_int namelen = arg2;
2152 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2155 if ((p->p_flag & P_SYSTEM) != 0) {
2159 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2160 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2161 error = proc_getauxv(curthread, p, &sb);
2162 error2 = sbuf_finish(&sb);
2165 return (error != 0 ? error : error2);
2169 * This sysctl allows a process to retrieve the path of the executable for
2170 * itself or another process.
2173 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2175 pid_t *pidp = (pid_t *)arg1;
2176 unsigned int arglen = arg2;
2179 char *retbuf, *freebuf;
2184 if (*pidp == -1) { /* -1 means this process */
2185 p = req->td->td_proc;
2187 error = pget(*pidp, PGET_CANSEE, &p);
2201 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2205 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2206 free(freebuf, M_TEMP);
2211 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2224 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2227 sv_name = p->p_sysent->sv_name;
2229 return (sysctl_handle_string(oidp, sv_name, 0, req));
2232 #ifdef KINFO_OVMENTRY_SIZE
2233 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2236 #ifdef COMPAT_FREEBSD7
2238 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2240 vm_map_entry_t entry, tmp_entry;
2241 unsigned int last_timestamp;
2242 char *fullpath, *freepath;
2243 struct kinfo_ovmentry *kve;
2253 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2256 vm = vmspace_acquire_ref(p);
2261 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2264 vm_map_lock_read(map);
2265 for (entry = map->header.next; entry != &map->header;
2266 entry = entry->next) {
2267 vm_object_t obj, tobj, lobj;
2270 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2273 bzero(kve, sizeof(*kve));
2274 kve->kve_structsize = sizeof(*kve);
2276 kve->kve_private_resident = 0;
2277 obj = entry->object.vm_object;
2279 VM_OBJECT_RLOCK(obj);
2280 if (obj->shadow_count == 1)
2281 kve->kve_private_resident =
2282 obj->resident_page_count;
2284 kve->kve_resident = 0;
2285 addr = entry->start;
2286 while (addr < entry->end) {
2287 if (pmap_extract(map->pmap, addr))
2288 kve->kve_resident++;
2292 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2294 VM_OBJECT_RLOCK(tobj);
2295 kve->kve_offset += tobj->backing_object_offset;
2298 VM_OBJECT_RUNLOCK(lobj);
2302 kve->kve_start = (void*)entry->start;
2303 kve->kve_end = (void*)entry->end;
2304 kve->kve_offset += (off_t)entry->offset;
2306 if (entry->protection & VM_PROT_READ)
2307 kve->kve_protection |= KVME_PROT_READ;
2308 if (entry->protection & VM_PROT_WRITE)
2309 kve->kve_protection |= KVME_PROT_WRITE;
2310 if (entry->protection & VM_PROT_EXECUTE)
2311 kve->kve_protection |= KVME_PROT_EXEC;
2313 if (entry->eflags & MAP_ENTRY_COW)
2314 kve->kve_flags |= KVME_FLAG_COW;
2315 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2316 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2317 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2318 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2320 last_timestamp = map->timestamp;
2321 vm_map_unlock_read(map);
2323 kve->kve_fileid = 0;
2328 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2329 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2330 kve->kve_type = KVME_TYPE_UNKNOWN;
2334 VM_OBJECT_RUNLOCK(lobj);
2336 kve->kve_ref_count = obj->ref_count;
2337 kve->kve_shadow_count = obj->shadow_count;
2338 VM_OBJECT_RUNLOCK(obj);
2340 vn_fullpath(curthread, vp, &fullpath,
2342 cred = curthread->td_ucred;
2343 vn_lock(vp, LK_SHARED | LK_RETRY);
2344 if (VOP_GETATTR(vp, &va, cred) == 0) {
2345 kve->kve_fileid = va.va_fileid;
2347 kve->kve_fsid = va.va_fsid;
2352 kve->kve_type = KVME_TYPE_NONE;
2353 kve->kve_ref_count = 0;
2354 kve->kve_shadow_count = 0;
2357 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2358 if (freepath != NULL)
2359 free(freepath, M_TEMP);
2361 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2362 vm_map_lock_read(map);
2365 if (last_timestamp != map->timestamp) {
2366 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2370 vm_map_unlock_read(map);
2376 #endif /* COMPAT_FREEBSD7 */
2378 #ifdef KINFO_VMENTRY_SIZE
2379 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2383 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2384 int *resident_count, bool *super)
2386 vm_object_t obj, tobj;
2389 vm_paddr_t locked_pa;
2390 vm_pindex_t pi, pi_adv, pindex;
2393 *resident_count = 0;
2394 if (vmmap_skip_res_cnt)
2398 obj = entry->object.vm_object;
2399 addr = entry->start;
2401 pi = OFF_TO_IDX(entry->offset);
2402 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2403 if (m_adv != NULL) {
2406 pi_adv = atop(entry->end - addr);
2408 for (tobj = obj;; tobj = tobj->backing_object) {
2409 m = vm_page_find_least(tobj, pindex);
2411 if (m->pindex == pindex)
2413 if (pi_adv > m->pindex - pindex) {
2414 pi_adv = m->pindex - pindex;
2418 if (tobj->backing_object == NULL)
2420 pindex += OFF_TO_IDX(tobj->
2421 backing_object_offset);
2425 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2426 (addr & (pagesizes[1] - 1)) == 0 &&
2427 (pmap_mincore(map->pmap, addr, &locked_pa) &
2428 MINCORE_SUPER) != 0) {
2430 pi_adv = atop(pagesizes[1]);
2433 * We do not test the found page on validity.
2434 * Either the page is busy and being paged in,
2435 * or it was invalidated. The first case
2436 * should be counted as resident, the second
2437 * is not so clear; we do account both.
2441 *resident_count += pi_adv;
2444 PA_UNLOCK_COND(locked_pa);
2448 * Must be called with the process locked and will return unlocked.
2451 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2453 vm_map_entry_t entry, tmp_entry;
2456 vm_object_t obj, tobj, lobj;
2457 char *fullpath, *freepath;
2458 struct kinfo_vmentry *kve;
2463 unsigned int last_timestamp;
2467 PROC_LOCK_ASSERT(p, MA_OWNED);
2471 vm = vmspace_acquire_ref(p);
2476 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2480 vm_map_lock_read(map);
2481 for (entry = map->header.next; entry != &map->header;
2482 entry = entry->next) {
2483 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2487 bzero(kve, sizeof(*kve));
2488 obj = entry->object.vm_object;
2490 for (tobj = obj; tobj != NULL;
2491 tobj = tobj->backing_object) {
2492 VM_OBJECT_RLOCK(tobj);
2493 kve->kve_offset += tobj->backing_object_offset;
2496 if (obj->backing_object == NULL)
2497 kve->kve_private_resident =
2498 obj->resident_page_count;
2499 kern_proc_vmmap_resident(map, entry,
2500 &kve->kve_resident, &super);
2502 kve->kve_flags |= KVME_FLAG_SUPER;
2503 for (tobj = obj; tobj != NULL;
2504 tobj = tobj->backing_object) {
2505 if (tobj != obj && tobj != lobj)
2506 VM_OBJECT_RUNLOCK(tobj);
2512 kve->kve_start = entry->start;
2513 kve->kve_end = entry->end;
2514 kve->kve_offset += entry->offset;
2516 if (entry->protection & VM_PROT_READ)
2517 kve->kve_protection |= KVME_PROT_READ;
2518 if (entry->protection & VM_PROT_WRITE)
2519 kve->kve_protection |= KVME_PROT_WRITE;
2520 if (entry->protection & VM_PROT_EXECUTE)
2521 kve->kve_protection |= KVME_PROT_EXEC;
2523 if (entry->eflags & MAP_ENTRY_COW)
2524 kve->kve_flags |= KVME_FLAG_COW;
2525 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2526 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2527 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2528 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2529 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2530 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2531 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2532 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2533 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2534 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2536 last_timestamp = map->timestamp;
2537 vm_map_unlock_read(map);
2542 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2546 VM_OBJECT_RUNLOCK(lobj);
2548 kve->kve_ref_count = obj->ref_count;
2549 kve->kve_shadow_count = obj->shadow_count;
2550 VM_OBJECT_RUNLOCK(obj);
2552 vn_fullpath(curthread, vp, &fullpath,
2554 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2555 cred = curthread->td_ucred;
2556 vn_lock(vp, LK_SHARED | LK_RETRY);
2557 if (VOP_GETATTR(vp, &va, cred) == 0) {
2558 kve->kve_vn_fileid = va.va_fileid;
2559 kve->kve_vn_fsid = va.va_fsid;
2560 kve->kve_vn_fsid_freebsd11 =
2561 kve->kve_vn_fsid; /* truncate */
2563 MAKEIMODE(va.va_type, va.va_mode);
2564 kve->kve_vn_size = va.va_size;
2565 kve->kve_vn_rdev = va.va_rdev;
2566 kve->kve_vn_rdev_freebsd11 =
2567 kve->kve_vn_rdev; /* truncate */
2568 kve->kve_status = KF_ATTR_VALID;
2573 kve->kve_type = KVME_TYPE_NONE;
2574 kve->kve_ref_count = 0;
2575 kve->kve_shadow_count = 0;
2578 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2579 if (freepath != NULL)
2580 free(freepath, M_TEMP);
2582 /* Pack record size down */
2583 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2584 kve->kve_structsize =
2585 offsetof(struct kinfo_vmentry, kve_path) +
2586 strlen(kve->kve_path) + 1;
2588 kve->kve_structsize = sizeof(*kve);
2589 kve->kve_structsize = roundup(kve->kve_structsize,
2592 /* Halt filling and truncate rather than exceeding maxlen */
2593 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2595 vm_map_lock_read(map);
2597 } else if (maxlen != -1)
2598 maxlen -= kve->kve_structsize;
2600 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2602 vm_map_lock_read(map);
2605 if (last_timestamp != map->timestamp) {
2606 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2610 vm_map_unlock_read(map);
2618 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2622 int error, error2, *name;
2625 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2626 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2627 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2632 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2633 error2 = sbuf_finish(&sb);
2635 return (error != 0 ? error : error2);
2638 #if defined(STACK) || defined(DDB)
2640 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2642 struct kinfo_kstack *kkstp;
2643 int error, i, *name, numthreads;
2644 lwpid_t *lwpidarray;
2651 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2655 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2656 st = stack_create(M_WAITOK);
2661 if (lwpidarray != NULL) {
2662 free(lwpidarray, M_TEMP);
2665 numthreads = p->p_numthreads;
2667 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2670 } while (numthreads < p->p_numthreads);
2673 * XXXRW: During the below loop, execve(2) and countless other sorts
2674 * of changes could have taken place. Should we check to see if the
2675 * vmspace has been replaced, or the like, in order to prevent
2676 * giving a snapshot that spans, say, execve(2), with some threads
2677 * before and some after? Among other things, the credentials could
2678 * have changed, in which case the right to extract debug info might
2679 * no longer be assured.
2682 FOREACH_THREAD_IN_PROC(p, td) {
2683 KASSERT(i < numthreads,
2684 ("sysctl_kern_proc_kstack: numthreads"));
2685 lwpidarray[i] = td->td_tid;
2689 for (i = 0; i < numthreads; i++) {
2690 td = thread_find(p, lwpidarray[i]);
2694 bzero(kkstp, sizeof(*kkstp));
2695 (void)sbuf_new(&sb, kkstp->kkst_trace,
2696 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2698 kkstp->kkst_tid = td->td_tid;
2699 if (TD_IS_SWAPPED(td)) {
2700 kkstp->kkst_state = KKST_STATE_SWAPPED;
2701 } else if (TD_IS_RUNNING(td)) {
2702 if (stack_save_td_running(st, td) == 0)
2703 kkstp->kkst_state = KKST_STATE_STACKOK;
2705 kkstp->kkst_state = KKST_STATE_RUNNING;
2707 kkstp->kkst_state = KKST_STATE_STACKOK;
2708 stack_save_td(st, td);
2712 stack_sbuf_print(&sb, st);
2715 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2722 if (lwpidarray != NULL)
2723 free(lwpidarray, M_TEMP);
2725 free(kkstp, M_TEMP);
2731 * This sysctl allows a process to retrieve the full list of groups from
2732 * itself or another process.
2735 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2737 pid_t *pidp = (pid_t *)arg1;
2738 unsigned int arglen = arg2;
2745 if (*pidp == -1) { /* -1 means this process */
2746 p = req->td->td_proc;
2749 error = pget(*pidp, PGET_CANSEE, &p);
2754 cred = crhold(p->p_ucred);
2757 error = SYSCTL_OUT(req, cred->cr_groups,
2758 cred->cr_ngroups * sizeof(gid_t));
2764 * This sysctl allows a process to retrieve or/and set the resource limit for
2768 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2770 int *name = (int *)arg1;
2771 u_int namelen = arg2;
2780 which = (u_int)name[1];
2781 if (which >= RLIM_NLIMITS)
2784 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2787 flags = PGET_HOLD | PGET_NOTWEXIT;
2788 if (req->newptr != NULL)
2789 flags |= PGET_CANDEBUG;
2791 flags |= PGET_CANSEE;
2792 error = pget((pid_t)name[0], flags, &p);
2799 if (req->oldptr != NULL) {
2801 lim_rlimit_proc(p, which, &rlim);
2804 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2811 if (req->newptr != NULL) {
2812 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2814 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2823 * This sysctl allows a process to retrieve ps_strings structure location of
2827 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2829 int *name = (int *)arg1;
2830 u_int namelen = arg2;
2832 vm_offset_t ps_strings;
2834 #ifdef COMPAT_FREEBSD32
2835 uint32_t ps_strings32;
2841 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2844 #ifdef COMPAT_FREEBSD32
2845 if ((req->flags & SCTL_MASK32) != 0) {
2847 * We return 0 if the 32 bit emulation request is for a 64 bit
2850 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2851 PTROUT(p->p_sysent->sv_psstrings) : 0;
2853 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2857 ps_strings = p->p_sysent->sv_psstrings;
2859 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2864 * This sysctl allows a process to retrieve umask of another process.
2867 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2869 int *name = (int *)arg1;
2870 u_int namelen = arg2;
2879 pid = (pid_t)name[0];
2881 if (pid == p->p_pid || pid == 0) {
2882 fd_cmask = p->p_fd->fd_cmask;
2886 error = pget(pid, PGET_WANTREAD, &p);
2890 fd_cmask = p->p_fd->fd_cmask;
2893 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2898 * This sysctl allows a process to set and retrieve binary osreldate of
2902 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2904 int *name = (int *)arg1;
2905 u_int namelen = arg2;
2907 int flags, error, osrel;
2912 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2915 flags = PGET_HOLD | PGET_NOTWEXIT;
2916 if (req->newptr != NULL)
2917 flags |= PGET_CANDEBUG;
2919 flags |= PGET_CANSEE;
2920 error = pget((pid_t)name[0], flags, &p);
2924 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2928 if (req->newptr != NULL) {
2929 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2944 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2946 int *name = (int *)arg1;
2947 u_int namelen = arg2;
2949 struct kinfo_sigtramp kst;
2950 const struct sysentvec *sv;
2952 #ifdef COMPAT_FREEBSD32
2953 struct kinfo_sigtramp32 kst32;
2959 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2963 #ifdef COMPAT_FREEBSD32
2964 if ((req->flags & SCTL_MASK32) != 0) {
2965 bzero(&kst32, sizeof(kst32));
2966 if (SV_PROC_FLAG(p, SV_ILP32)) {
2967 if (sv->sv_sigcode_base != 0) {
2968 kst32.ksigtramp_start = sv->sv_sigcode_base;
2969 kst32.ksigtramp_end = sv->sv_sigcode_base +
2972 kst32.ksigtramp_start = sv->sv_psstrings -
2974 kst32.ksigtramp_end = sv->sv_psstrings;
2978 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2982 bzero(&kst, sizeof(kst));
2983 if (sv->sv_sigcode_base != 0) {
2984 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2985 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2988 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2990 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2993 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2997 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2999 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3000 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3001 "Return entire process table");
3003 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3004 sysctl_kern_proc, "Process table");
3006 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3007 sysctl_kern_proc, "Process table");
3009 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3010 sysctl_kern_proc, "Process table");
3012 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3013 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3015 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3016 sysctl_kern_proc, "Process table");
3018 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3019 sysctl_kern_proc, "Process table");
3021 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3022 sysctl_kern_proc, "Process table");
3024 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3025 sysctl_kern_proc, "Process table");
3027 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3028 sysctl_kern_proc, "Return process table, no threads");
3030 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3031 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3032 sysctl_kern_proc_args, "Process argument list");
3034 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3035 sysctl_kern_proc_env, "Process environment");
3037 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3038 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3040 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3041 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3043 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3044 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3045 "Process syscall vector name (ABI type)");
3047 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3048 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3050 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3051 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3053 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3054 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3056 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3057 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3059 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3060 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3062 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3063 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3065 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3066 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3068 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3069 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3071 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3072 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3073 "Return process table, no threads");
3075 #ifdef COMPAT_FREEBSD7
3076 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3077 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3080 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3081 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3083 #if defined(STACK) || defined(DDB)
3084 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3085 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3088 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3089 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3091 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3092 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3093 "Process resource limits");
3095 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3096 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3097 "Process ps_strings location");
3099 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3100 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3102 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3103 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3104 "Process binary osreldate");
3106 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3107 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3108 "Process signal trampoline location");
3113 * stop_all_proc() purpose is to stop all process which have usermode,
3114 * except current process for obvious reasons. This makes it somewhat
3115 * unreliable when invoked from multithreaded process. The service
3116 * must not be user-callable anyway.
3121 struct proc *cp, *p;
3123 bool restart, seen_stopped, seen_exiting, stopped_some;
3127 sx_xlock(&allproc_lock);
3129 seen_exiting = seen_stopped = stopped_some = restart = false;
3130 LIST_REMOVE(cp, p_list);
3131 LIST_INSERT_HEAD(&allproc, cp, p_list);
3133 p = LIST_NEXT(cp, p_list);
3136 LIST_REMOVE(cp, p_list);
3137 LIST_INSERT_AFTER(p, cp, p_list);
3139 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3143 if ((p->p_flag & P_WEXIT) != 0) {
3144 seen_exiting = true;
3148 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3150 * Stopped processes are tolerated when there
3151 * are no other processes which might continue
3152 * them. P_STOPPED_SINGLE but not
3153 * P_TOTAL_STOP process still has at least one
3156 seen_stopped = true;
3160 sx_xunlock(&allproc_lock);
3162 r = thread_single(p, SINGLE_ALLPROC);
3166 stopped_some = true;
3169 sx_xlock(&allproc_lock);
3171 /* Catch forked children we did not see in iteration. */
3172 if (gen != allproc_gen)
3174 sx_xunlock(&allproc_lock);
3175 if (restart || stopped_some || seen_exiting || seen_stopped) {
3176 kern_yield(PRI_USER);
3182 resume_all_proc(void)
3184 struct proc *cp, *p;
3187 sx_xlock(&allproc_lock);
3189 LIST_REMOVE(cp, p_list);
3190 LIST_INSERT_HEAD(&allproc, cp, p_list);
3192 p = LIST_NEXT(cp, p_list);
3195 LIST_REMOVE(cp, p_list);
3196 LIST_INSERT_AFTER(p, cp, p_list);
3198 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3199 sx_xunlock(&allproc_lock);
3201 thread_single_end(p, SINGLE_ALLPROC);
3204 sx_xlock(&allproc_lock);
3209 /* Did the loop above missed any stopped process ? */
3210 FOREACH_PROC_IN_SYSTEM(p) {
3211 /* No need for proc lock. */
3212 if ((p->p_flag & P_TOTAL_STOP) != 0)
3215 sx_xunlock(&allproc_lock);
3218 /* #define TOTAL_STOP_DEBUG 1 */
3219 #ifdef TOTAL_STOP_DEBUG
3220 volatile static int ap_resume;
3221 #include <sys/mount.h>
3224 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3230 error = sysctl_handle_int(oidp, &val, 0, req);
3231 if (error != 0 || req->newptr == NULL)
3236 while (ap_resume == 0)
3244 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3245 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3246 sysctl_debug_stop_all_proc, "I",