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 EVENTHANDLER_LIST_DECLARE(thread_ctor);
159 EVENTHANDLER_LIST_DECLARE(thread_dtor);
161 int kstack_pages = KSTACK_PAGES;
162 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
163 "Kernel stack size in pages");
164 static int vmmap_skip_res_cnt = 0;
165 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
166 &vmmap_skip_res_cnt, 0,
167 "Skip calculation of the pages resident count in kern.proc.vmmap");
169 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
170 #ifdef COMPAT_FREEBSD32
171 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
175 * Initialize global process hashing structures.
182 sx_init(&allproc_lock, "allproc");
183 sx_init(&zombproc_lock, "zombproc");
184 sx_init(&proctree_lock, "proctree");
185 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
186 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
188 LIST_INIT(&zombproc);
189 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
190 pidhashlock = (pidhash + 1) / 64;
193 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
194 M_PROC, M_WAITOK | M_ZERO);
195 for (i = 0; i < pidhashlock + 1; i++)
196 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
197 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
198 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
199 proc_ctor, proc_dtor, proc_init, proc_fini,
200 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
205 * Prepare a proc for use.
208 proc_ctor(void *mem, int size, void *arg, int flags)
213 p = (struct proc *)mem;
214 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
215 td = FIRST_THREAD_IN_PROC(p);
217 /* Make sure all thread constructors are executed */
218 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
224 * Reclaim a proc after use.
227 proc_dtor(void *mem, int size, void *arg)
232 /* INVARIANTS checks go here */
233 p = (struct proc *)mem;
234 td = FIRST_THREAD_IN_PROC(p);
237 KASSERT((p->p_numthreads == 1),
238 ("bad number of threads in exiting process"));
239 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
241 /* Free all OSD associated to this thread. */
243 td_softdep_cleanup(td);
244 MPASS(td->td_su == NULL);
246 /* Make sure all thread destructors are executed */
247 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
249 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
250 if (p->p_ksi != NULL)
251 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
255 * Initialize type-stable parts of a proc (when newly created).
258 proc_init(void *mem, int size, int flags)
262 p = (struct proc *)mem;
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();
277 * UMA should ensure that this function is never called.
278 * Freeing a proc structure would violate type stability.
281 proc_fini(void *mem, int size)
286 p = (struct proc *)mem;
287 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
288 pstats_free(p->p_stats);
289 thread_free(FIRST_THREAD_IN_PROC(p));
290 mtx_destroy(&p->p_mtx);
291 if (p->p_ksi != NULL)
292 ksiginfo_free(p->p_ksi);
294 panic("proc reclaimed");
299 * PID space management.
301 * These bitmaps are used by fork_findpid.
303 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
304 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
305 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
306 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
308 static bitstr_t *proc_id_array[] = {
316 proc_id_set(int type, pid_t id)
319 KASSERT(type >= 0 && type < nitems(proc_id_array),
320 ("invalid type %d\n", type));
321 mtx_lock(&procid_lock);
322 KASSERT(bit_test(proc_id_array[type], id) == 0,
323 ("bit %d already set in %d\n", id, type));
324 bit_set(proc_id_array[type], id);
325 mtx_unlock(&procid_lock);
329 proc_id_set_cond(int type, pid_t id)
332 KASSERT(type >= 0 && type < nitems(proc_id_array),
333 ("invalid type %d\n", type));
334 if (bit_test(proc_id_array[type], id))
336 mtx_lock(&procid_lock);
337 bit_set(proc_id_array[type], id);
338 mtx_unlock(&procid_lock);
342 proc_id_clear(int type, pid_t id)
345 KASSERT(type >= 0 && type < nitems(proc_id_array),
346 ("invalid type %d\n", type));
347 mtx_lock(&procid_lock);
348 KASSERT(bit_test(proc_id_array[type], id) != 0,
349 ("bit %d not set in %d\n", id, type));
350 bit_clear(proc_id_array[type], id);
351 mtx_unlock(&procid_lock);
355 * Is p an inferior of the current process?
358 inferior(struct proc *p)
361 sx_assert(&proctree_lock, SX_LOCKED);
362 PROC_LOCK_ASSERT(p, MA_OWNED);
363 for (; p != curproc; p = proc_realparent(p)) {
371 * Shared lock all the pid hash lists.
374 pidhash_slockall(void)
378 for (i = 0; i < pidhashlock + 1; i++)
379 sx_slock(&pidhashtbl_lock[i]);
383 * Shared unlock all the pid hash lists.
386 pidhash_sunlockall(void)
390 for (i = 0; i < pidhashlock + 1; i++)
391 sx_sunlock(&pidhashtbl_lock[i]);
395 * Similar to pfind_any(), this function finds zombies.
398 pfind_any_locked(pid_t pid)
402 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
403 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
404 if (p->p_pid == pid) {
406 if (p->p_state == PRS_NEW) {
417 * Locate a process by number.
419 * By not returning processes in the PRS_NEW state, we allow callers to avoid
420 * testing for that condition to avoid dereferencing p_ucred, et al.
422 static __always_inline struct proc *
423 _pfind(pid_t pid, bool zombie)
428 if (p->p_pid == pid) {
432 sx_slock(PIDHASHLOCK(pid));
433 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
434 if (p->p_pid == pid) {
436 if (p->p_state == PRS_NEW ||
437 (!zombie && p->p_state == PRS_ZOMBIE)) {
444 sx_sunlock(PIDHASHLOCK(pid));
452 return (_pfind(pid, false));
456 * Same as pfind but allow zombies.
462 return (_pfind(pid, true));
471 sx_slock(&allproc_lock);
472 FOREACH_PROC_IN_SYSTEM(p) {
474 if (p->p_state == PRS_NEW) {
478 FOREACH_THREAD_IN_PROC(p, td) {
479 if (td->td_tid == tid)
485 sx_sunlock(&allproc_lock);
490 * Locate a process group by number.
491 * The caller must hold proctree_lock.
498 sx_assert(&proctree_lock, SX_LOCKED);
500 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
501 if (pgrp->pg_id == pgid) {
510 * Locate process and do additional manipulations, depending on flags.
513 pget(pid_t pid, int flags, struct proc **pp)
519 if (p->p_pid == pid) {
523 if (pid <= PID_MAX) {
524 if ((flags & PGET_NOTWEXIT) == 0)
528 } else if ((flags & PGET_NOTID) == 0) {
533 if ((flags & PGET_CANSEE) != 0) {
534 error = p_cansee(curthread, p);
539 if ((flags & PGET_CANDEBUG) != 0) {
540 error = p_candebug(curthread, p);
544 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
548 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
552 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
554 * XXXRW: Not clear ESRCH is the right error during proc
560 if ((flags & PGET_HOLD) != 0) {
572 * Create a new process group.
573 * pgid must be equal to the pid of p.
574 * Begin a new session if required.
577 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
580 sx_assert(&proctree_lock, SX_XLOCKED);
582 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
583 KASSERT(p->p_pid == pgid,
584 ("enterpgrp: new pgrp and pid != pgid"));
585 KASSERT(pgfind(pgid) == NULL,
586 ("enterpgrp: pgrp with pgid exists"));
587 KASSERT(!SESS_LEADER(p),
588 ("enterpgrp: session leader attempted setpgrp"));
590 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
596 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
598 p->p_flag &= ~P_CONTROLT;
602 sess->s_sid = p->p_pid;
603 proc_id_set(PROC_ID_SESSION, p->p_pid);
604 refcount_init(&sess->s_count, 1);
605 sess->s_ttyvp = NULL;
606 sess->s_ttydp = NULL;
608 bcopy(p->p_session->s_login, sess->s_login,
609 sizeof(sess->s_login));
610 pgrp->pg_session = sess;
611 KASSERT(p == curproc,
612 ("enterpgrp: mksession and p != curproc"));
614 pgrp->pg_session = p->p_session;
615 sess_hold(pgrp->pg_session);
619 proc_id_set(PROC_ID_GROUP, p->p_pid);
620 LIST_INIT(&pgrp->pg_members);
623 * As we have an exclusive lock of proctree_lock,
624 * this should not deadlock.
626 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
628 SLIST_INIT(&pgrp->pg_sigiolst);
631 doenterpgrp(p, pgrp);
637 * Move p to an existing process group
640 enterthispgrp(struct proc *p, struct pgrp *pgrp)
643 sx_assert(&proctree_lock, SX_XLOCKED);
644 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
645 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
646 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
647 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
648 KASSERT(pgrp->pg_session == p->p_session,
649 ("%s: pgrp's session %p, p->p_session %p.\n",
653 KASSERT(pgrp != p->p_pgrp,
654 ("%s: p belongs to pgrp.", __func__));
656 doenterpgrp(p, pgrp);
662 * Move p to a process group
665 doenterpgrp(struct proc *p, struct pgrp *pgrp)
667 struct pgrp *savepgrp;
669 sx_assert(&proctree_lock, SX_XLOCKED);
670 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
671 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
672 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
673 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
675 savepgrp = p->p_pgrp;
678 * Adjust eligibility of affected pgrps to participate in job control.
679 * Increment eligibility counts before decrementing, otherwise we
680 * could reach 0 spuriously during the first call.
683 fixjobc(p, p->p_pgrp, 0);
688 LIST_REMOVE(p, p_pglist);
691 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
692 PGRP_UNLOCK(savepgrp);
694 if (LIST_EMPTY(&savepgrp->pg_members))
699 * remove process from process group
702 leavepgrp(struct proc *p)
704 struct pgrp *savepgrp;
706 sx_assert(&proctree_lock, SX_XLOCKED);
707 savepgrp = p->p_pgrp;
710 LIST_REMOVE(p, p_pglist);
713 PGRP_UNLOCK(savepgrp);
714 if (LIST_EMPTY(&savepgrp->pg_members))
720 * delete a process group
723 pgdelete(struct pgrp *pgrp)
725 struct session *savesess;
728 sx_assert(&proctree_lock, SX_XLOCKED);
729 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
730 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
733 * Reset any sigio structures pointing to us as a result of
734 * F_SETOWN with our pgid.
736 funsetownlst(&pgrp->pg_sigiolst);
739 tp = pgrp->pg_session->s_ttyp;
740 LIST_REMOVE(pgrp, pg_hash);
741 savesess = pgrp->pg_session;
744 /* Remove the reference to the pgrp before deallocating it. */
747 tty_rel_pgrp(tp, pgrp);
750 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
751 mtx_destroy(&pgrp->pg_mtx);
753 sess_release(savesess);
757 pgadjustjobc(struct pgrp *pgrp, int entering)
765 if (pgrp->pg_jobc == 0)
772 * Adjust pgrp jobc counters when specified process changes process group.
773 * We count the number of processes in each process group that "qualify"
774 * the group for terminal job control (those with a parent in a different
775 * process group of the same session). If that count reaches zero, the
776 * process group becomes orphaned. Check both the specified process'
777 * process group and that of its children.
778 * entering == 0 => p is leaving specified group.
779 * entering == 1 => p is entering specified group.
782 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
784 struct pgrp *hispgrp;
785 struct session *mysession;
788 sx_assert(&proctree_lock, SX_LOCKED);
789 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
790 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
791 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
794 * Check p's parent to see whether p qualifies its own process
795 * group; if so, adjust count for p's process group.
797 mysession = pgrp->pg_session;
798 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
799 hispgrp->pg_session == mysession)
800 pgadjustjobc(pgrp, entering);
803 * Check this process' children to see whether they qualify
804 * their process groups; if so, adjust counts for children's
807 LIST_FOREACH(q, &p->p_children, p_sibling) {
809 if (hispgrp == pgrp ||
810 hispgrp->pg_session != mysession)
812 if (q->p_state == PRS_ZOMBIE)
814 pgadjustjobc(hispgrp, entering);
827 MPASS(p->p_flag & P_WEXIT);
829 * Do a quick check to see if there is anything to do with the
830 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
833 if (!SESS_LEADER(p) &&
834 (p->p_pgrp == p->p_pptr->p_pgrp) &&
835 LIST_EMPTY(&p->p_children)) {
841 sx_xlock(&proctree_lock);
842 if (SESS_LEADER(p)) {
846 * s_ttyp is not zero'd; we use this to indicate that
847 * the session once had a controlling terminal. (for
848 * logging and informational purposes)
859 * Signal foreground pgrp and revoke access to
860 * controlling terminal if it has not been revoked
863 * Because the TTY may have been revoked in the mean
864 * time and could already have a new session associated
865 * with it, make sure we don't send a SIGHUP to a
866 * foreground process group that does not belong to this
872 if (tp->t_session == sp)
873 tty_signal_pgrp(tp, SIGHUP);
878 sx_xunlock(&proctree_lock);
879 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
880 VOP_REVOKE(ttyvp, REVOKEALL);
881 VOP_UNLOCK(ttyvp, 0);
884 sx_xlock(&proctree_lock);
887 fixjobc(p, p->p_pgrp, 0);
888 sx_xunlock(&proctree_lock);
892 * A process group has become orphaned;
893 * if there are any stopped processes in the group,
894 * hang-up all process in that group.
897 orphanpg(struct pgrp *pg)
901 PGRP_LOCK_ASSERT(pg, MA_OWNED);
903 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
905 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
907 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
909 kern_psignal(p, SIGHUP);
910 kern_psignal(p, SIGCONT);
920 sess_hold(struct session *s)
923 refcount_acquire(&s->s_count);
927 sess_release(struct session *s)
930 if (refcount_release(&s->s_count)) {
931 if (s->s_ttyp != NULL) {
933 tty_rel_sess(s->s_ttyp, s);
935 proc_id_clear(PROC_ID_SESSION, s->s_sid);
936 mtx_destroy(&s->s_mtx);
943 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
949 for (i = 0; i <= pgrphash; i++) {
950 if (!LIST_EMPTY(&pgrphashtbl[i])) {
951 printf("\tindx %d\n", i);
952 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
954 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
955 (void *)pgrp, (long)pgrp->pg_id,
956 (void *)pgrp->pg_session,
957 pgrp->pg_session->s_count,
958 (void *)LIST_FIRST(&pgrp->pg_members));
959 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
960 printf("\t\tpid %ld addr %p pgrp %p\n",
961 (long)p->p_pid, (void *)p,
971 * Calculate the kinfo_proc members which contain process-wide
973 * Must be called with the target process locked.
976 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
980 PROC_LOCK_ASSERT(p, MA_OWNED);
984 FOREACH_THREAD_IN_PROC(p, td) {
986 kp->ki_pctcpu += sched_pctcpu(td);
987 kp->ki_estcpu += sched_estcpu(td);
993 * Clear kinfo_proc and fill in any information that is common
994 * to all threads in the process.
995 * Must be called with the target process locked.
998 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1005 struct timeval boottime;
1007 PROC_LOCK_ASSERT(p, MA_OWNED);
1008 bzero(kp, sizeof(*kp));
1010 kp->ki_structsize = sizeof(*kp);
1012 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1013 kp->ki_args = p->p_args;
1014 kp->ki_textvp = p->p_textvp;
1016 kp->ki_tracep = p->p_tracevp;
1017 kp->ki_traceflag = p->p_traceflag;
1019 kp->ki_fd = p->p_fd;
1020 kp->ki_vmspace = p->p_vmspace;
1021 kp->ki_flag = p->p_flag;
1022 kp->ki_flag2 = p->p_flag2;
1025 kp->ki_uid = cred->cr_uid;
1026 kp->ki_ruid = cred->cr_ruid;
1027 kp->ki_svuid = cred->cr_svuid;
1028 kp->ki_cr_flags = 0;
1029 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1030 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1031 /* XXX bde doesn't like KI_NGROUPS */
1032 if (cred->cr_ngroups > KI_NGROUPS) {
1033 kp->ki_ngroups = KI_NGROUPS;
1034 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1036 kp->ki_ngroups = cred->cr_ngroups;
1037 bcopy(cred->cr_groups, kp->ki_groups,
1038 kp->ki_ngroups * sizeof(gid_t));
1039 kp->ki_rgid = cred->cr_rgid;
1040 kp->ki_svgid = cred->cr_svgid;
1041 /* If jailed(cred), emulate the old P_JAILED flag. */
1043 kp->ki_flag |= P_JAILED;
1044 /* If inside the jail, use 0 as a jail ID. */
1045 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1046 kp->ki_jid = cred->cr_prison->pr_id;
1048 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1049 sizeof(kp->ki_loginclass));
1053 mtx_lock(&ps->ps_mtx);
1054 kp->ki_sigignore = ps->ps_sigignore;
1055 kp->ki_sigcatch = ps->ps_sigcatch;
1056 mtx_unlock(&ps->ps_mtx);
1058 if (p->p_state != PRS_NEW &&
1059 p->p_state != PRS_ZOMBIE &&
1060 p->p_vmspace != NULL) {
1061 struct vmspace *vm = p->p_vmspace;
1063 kp->ki_size = vm->vm_map.size;
1064 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1065 FOREACH_THREAD_IN_PROC(p, td0) {
1066 if (!TD_IS_SWAPPED(td0))
1067 kp->ki_rssize += td0->td_kstack_pages;
1069 kp->ki_swrss = vm->vm_swrss;
1070 kp->ki_tsize = vm->vm_tsize;
1071 kp->ki_dsize = vm->vm_dsize;
1072 kp->ki_ssize = vm->vm_ssize;
1073 } else if (p->p_state == PRS_ZOMBIE)
1074 kp->ki_stat = SZOMB;
1075 if (kp->ki_flag & P_INMEM)
1076 kp->ki_sflag = PS_INMEM;
1079 /* Calculate legacy swtime as seconds since 'swtick'. */
1080 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1081 kp->ki_pid = p->p_pid;
1082 kp->ki_nice = p->p_nice;
1083 kp->ki_fibnum = p->p_fibnum;
1084 kp->ki_start = p->p_stats->p_start;
1085 getboottime(&boottime);
1086 timevaladd(&kp->ki_start, &boottime);
1088 rufetch(p, &kp->ki_rusage);
1089 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1090 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1092 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1093 /* Some callers want child times in a single value. */
1094 kp->ki_childtime = kp->ki_childstime;
1095 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1097 FOREACH_THREAD_IN_PROC(p, td0)
1098 kp->ki_cow += td0->td_cow;
1102 kp->ki_pgid = p->p_pgrp->pg_id;
1103 kp->ki_jobc = p->p_pgrp->pg_jobc;
1104 sp = p->p_pgrp->pg_session;
1107 kp->ki_sid = sp->s_sid;
1109 strlcpy(kp->ki_login, sp->s_login,
1110 sizeof(kp->ki_login));
1112 kp->ki_kiflag |= KI_CTTY;
1114 kp->ki_kiflag |= KI_SLEADER;
1115 /* XXX proctree_lock */
1120 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1121 kp->ki_tdev = tty_udev(tp);
1122 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1123 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1125 kp->ki_tsid = tp->t_session->s_sid;
1127 kp->ki_tdev = NODEV;
1128 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1130 if (p->p_comm[0] != '\0')
1131 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1132 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1133 p->p_sysent->sv_name[0] != '\0')
1134 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1135 kp->ki_siglist = p->p_siglist;
1136 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1137 kp->ki_acflag = p->p_acflag;
1138 kp->ki_lock = p->p_lock;
1140 kp->ki_ppid = p->p_oppid;
1141 if (p->p_flag & P_TRACED)
1142 kp->ki_tracer = p->p_pptr->p_pid;
1147 * Fill in information that is thread specific. Must be called with
1148 * target process locked. If 'preferthread' is set, overwrite certain
1149 * process-related fields that are maintained for both threads and
1153 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1159 PROC_LOCK_ASSERT(p, MA_OWNED);
1164 if (td->td_wmesg != NULL)
1165 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1167 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1168 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1169 sizeof(kp->ki_tdname)) {
1170 strlcpy(kp->ki_moretdname,
1171 td->td_name + sizeof(kp->ki_tdname) - 1,
1172 sizeof(kp->ki_moretdname));
1174 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1176 if (TD_ON_LOCK(td)) {
1177 kp->ki_kiflag |= KI_LOCKBLOCK;
1178 strlcpy(kp->ki_lockname, td->td_lockname,
1179 sizeof(kp->ki_lockname));
1181 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1182 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1185 if (p->p_state == PRS_NORMAL) { /* approximate. */
1186 if (TD_ON_RUNQ(td) ||
1188 TD_IS_RUNNING(td)) {
1190 } else if (P_SHOULDSTOP(p)) {
1191 kp->ki_stat = SSTOP;
1192 } else if (TD_IS_SLEEPING(td)) {
1193 kp->ki_stat = SSLEEP;
1194 } else if (TD_ON_LOCK(td)) {
1195 kp->ki_stat = SLOCK;
1197 kp->ki_stat = SWAIT;
1199 } else if (p->p_state == PRS_ZOMBIE) {
1200 kp->ki_stat = SZOMB;
1205 /* Things in the thread */
1206 kp->ki_wchan = td->td_wchan;
1207 kp->ki_pri.pri_level = td->td_priority;
1208 kp->ki_pri.pri_native = td->td_base_pri;
1211 * Note: legacy fields; clamp at the old NOCPU value and/or
1212 * the maximum u_char CPU value.
1214 if (td->td_lastcpu == NOCPU)
1215 kp->ki_lastcpu_old = NOCPU_OLD;
1216 else if (td->td_lastcpu > MAXCPU_OLD)
1217 kp->ki_lastcpu_old = MAXCPU_OLD;
1219 kp->ki_lastcpu_old = td->td_lastcpu;
1221 if (td->td_oncpu == NOCPU)
1222 kp->ki_oncpu_old = NOCPU_OLD;
1223 else if (td->td_oncpu > MAXCPU_OLD)
1224 kp->ki_oncpu_old = MAXCPU_OLD;
1226 kp->ki_oncpu_old = td->td_oncpu;
1228 kp->ki_lastcpu = td->td_lastcpu;
1229 kp->ki_oncpu = td->td_oncpu;
1230 kp->ki_tdflags = td->td_flags;
1231 kp->ki_tid = td->td_tid;
1232 kp->ki_numthreads = p->p_numthreads;
1233 kp->ki_pcb = td->td_pcb;
1234 kp->ki_kstack = (void *)td->td_kstack;
1235 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1236 kp->ki_pri.pri_class = td->td_pri_class;
1237 kp->ki_pri.pri_user = td->td_user_pri;
1240 rufetchtd(td, &kp->ki_rusage);
1241 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1242 kp->ki_pctcpu = sched_pctcpu(td);
1243 kp->ki_estcpu = sched_estcpu(td);
1244 kp->ki_cow = td->td_cow;
1247 /* We can't get this anymore but ps etc never used it anyway. */
1251 kp->ki_siglist = td->td_siglist;
1252 kp->ki_sigmask = td->td_sigmask;
1259 * Fill in a kinfo_proc structure for the specified process.
1260 * Must be called with the target process locked.
1263 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1266 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1268 fill_kinfo_proc_only(p, kp);
1269 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1270 fill_kinfo_aggregate(p, kp);
1277 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1281 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1284 pstats_fork(struct pstats *src, struct pstats *dst)
1287 bzero(&dst->pstat_startzero,
1288 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1289 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1290 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1294 pstats_free(struct pstats *ps)
1297 free(ps, M_SUBPROC);
1301 * Locate a zombie process by number
1308 sx_slock(&zombproc_lock);
1309 LIST_FOREACH(p, &zombproc, p_list) {
1310 if (p->p_pid == pid) {
1315 sx_sunlock(&zombproc_lock);
1319 #ifdef COMPAT_FREEBSD32
1322 * This function is typically used to copy out the kernel address, so
1323 * it can be replaced by assignment of zero.
1325 static inline uint32_t
1326 ptr32_trim(void *ptr)
1330 uptr = (uintptr_t)ptr;
1331 return ((uptr > UINT_MAX) ? 0 : uptr);
1334 #define PTRTRIM_CP(src,dst,fld) \
1335 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1338 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1342 bzero(ki32, sizeof(struct kinfo_proc32));
1343 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1344 CP(*ki, *ki32, ki_layout);
1345 PTRTRIM_CP(*ki, *ki32, ki_args);
1346 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1347 PTRTRIM_CP(*ki, *ki32, ki_addr);
1348 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1349 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1350 PTRTRIM_CP(*ki, *ki32, ki_fd);
1351 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1352 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1353 CP(*ki, *ki32, ki_pid);
1354 CP(*ki, *ki32, ki_ppid);
1355 CP(*ki, *ki32, ki_pgid);
1356 CP(*ki, *ki32, ki_tpgid);
1357 CP(*ki, *ki32, ki_sid);
1358 CP(*ki, *ki32, ki_tsid);
1359 CP(*ki, *ki32, ki_jobc);
1360 CP(*ki, *ki32, ki_tdev);
1361 CP(*ki, *ki32, ki_tdev_freebsd11);
1362 CP(*ki, *ki32, ki_siglist);
1363 CP(*ki, *ki32, ki_sigmask);
1364 CP(*ki, *ki32, ki_sigignore);
1365 CP(*ki, *ki32, ki_sigcatch);
1366 CP(*ki, *ki32, ki_uid);
1367 CP(*ki, *ki32, ki_ruid);
1368 CP(*ki, *ki32, ki_svuid);
1369 CP(*ki, *ki32, ki_rgid);
1370 CP(*ki, *ki32, ki_svgid);
1371 CP(*ki, *ki32, ki_ngroups);
1372 for (i = 0; i < KI_NGROUPS; i++)
1373 CP(*ki, *ki32, ki_groups[i]);
1374 CP(*ki, *ki32, ki_size);
1375 CP(*ki, *ki32, ki_rssize);
1376 CP(*ki, *ki32, ki_swrss);
1377 CP(*ki, *ki32, ki_tsize);
1378 CP(*ki, *ki32, ki_dsize);
1379 CP(*ki, *ki32, ki_ssize);
1380 CP(*ki, *ki32, ki_xstat);
1381 CP(*ki, *ki32, ki_acflag);
1382 CP(*ki, *ki32, ki_pctcpu);
1383 CP(*ki, *ki32, ki_estcpu);
1384 CP(*ki, *ki32, ki_slptime);
1385 CP(*ki, *ki32, ki_swtime);
1386 CP(*ki, *ki32, ki_cow);
1387 CP(*ki, *ki32, ki_runtime);
1388 TV_CP(*ki, *ki32, ki_start);
1389 TV_CP(*ki, *ki32, ki_childtime);
1390 CP(*ki, *ki32, ki_flag);
1391 CP(*ki, *ki32, ki_kiflag);
1392 CP(*ki, *ki32, ki_traceflag);
1393 CP(*ki, *ki32, ki_stat);
1394 CP(*ki, *ki32, ki_nice);
1395 CP(*ki, *ki32, ki_lock);
1396 CP(*ki, *ki32, ki_rqindex);
1397 CP(*ki, *ki32, ki_oncpu);
1398 CP(*ki, *ki32, ki_lastcpu);
1400 /* XXX TODO: wrap cpu value as appropriate */
1401 CP(*ki, *ki32, ki_oncpu_old);
1402 CP(*ki, *ki32, ki_lastcpu_old);
1404 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1405 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1406 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1407 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1408 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1409 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1410 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1411 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1412 CP(*ki, *ki32, ki_tracer);
1413 CP(*ki, *ki32, ki_flag2);
1414 CP(*ki, *ki32, ki_fibnum);
1415 CP(*ki, *ki32, ki_cr_flags);
1416 CP(*ki, *ki32, ki_jid);
1417 CP(*ki, *ki32, ki_numthreads);
1418 CP(*ki, *ki32, ki_tid);
1419 CP(*ki, *ki32, ki_pri);
1420 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1421 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1422 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1423 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1424 PTRTRIM_CP(*ki, *ki32, ki_udata);
1425 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1426 CP(*ki, *ki32, ki_sflag);
1427 CP(*ki, *ki32, ki_tdflags);
1432 kern_proc_out_size(struct proc *p, int flags)
1436 PROC_LOCK_ASSERT(p, MA_OWNED);
1438 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1439 #ifdef COMPAT_FREEBSD32
1440 if ((flags & KERN_PROC_MASK32) != 0) {
1441 size += sizeof(struct kinfo_proc32);
1444 size += sizeof(struct kinfo_proc);
1446 #ifdef COMPAT_FREEBSD32
1447 if ((flags & KERN_PROC_MASK32) != 0)
1448 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1451 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1458 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1461 struct kinfo_proc ki;
1462 #ifdef COMPAT_FREEBSD32
1463 struct kinfo_proc32 ki32;
1467 PROC_LOCK_ASSERT(p, MA_OWNED);
1468 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1471 fill_kinfo_proc(p, &ki);
1472 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1473 #ifdef COMPAT_FREEBSD32
1474 if ((flags & KERN_PROC_MASK32) != 0) {
1475 freebsd32_kinfo_proc_out(&ki, &ki32);
1476 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1480 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1483 FOREACH_THREAD_IN_PROC(p, td) {
1484 fill_kinfo_thread(td, &ki, 1);
1485 #ifdef COMPAT_FREEBSD32
1486 if ((flags & KERN_PROC_MASK32) != 0) {
1487 freebsd32_kinfo_proc_out(&ki, &ki32);
1488 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1492 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1503 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1506 struct kinfo_proc ki;
1509 if (req->oldptr == NULL)
1510 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1512 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1513 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1514 error = kern_proc_out(p, &sb, flags);
1515 error2 = sbuf_finish(&sb);
1519 else if (error2 != 0)
1525 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1530 for (i = 0; i < pidhashlock + 1; i++) {
1531 sx_slock(&pidhashtbl_lock[i]);
1532 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1533 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1534 if (p->p_state == PRS_NEW)
1536 error = cb(p, cbarg);
1537 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1539 sx_sunlock(&pidhashtbl_lock[i]);
1544 sx_sunlock(&pidhashtbl_lock[i]);
1549 struct kern_proc_out_args {
1550 struct sysctl_req *req;
1557 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1559 struct kern_proc_out_args *arg = origarg;
1560 int *name = arg->name;
1561 int oid_number = arg->oid_number;
1562 int flags = arg->flags;
1563 struct sysctl_req *req = arg->req;
1568 KASSERT(p->p_ucred != NULL,
1569 ("process credential is NULL for non-NEW proc"));
1571 * Show a user only appropriate processes.
1573 if (p_cansee(curthread, p))
1576 * TODO - make more efficient (see notes below).
1579 switch (oid_number) {
1582 if (p->p_ucred->cr_gid != (gid_t)name[0])
1586 case KERN_PROC_PGRP:
1587 /* could do this by traversing pgrp */
1588 if (p->p_pgrp == NULL ||
1589 p->p_pgrp->pg_id != (pid_t)name[0])
1593 case KERN_PROC_RGID:
1594 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1598 case KERN_PROC_SESSION:
1599 if (p->p_session == NULL ||
1600 p->p_session->s_sid != (pid_t)name[0])
1605 if ((p->p_flag & P_CONTROLT) == 0 ||
1606 p->p_session == NULL)
1608 /* XXX proctree_lock */
1609 SESS_LOCK(p->p_session);
1610 if (p->p_session->s_ttyp == NULL ||
1611 tty_udev(p->p_session->s_ttyp) !=
1613 SESS_UNLOCK(p->p_session);
1616 SESS_UNLOCK(p->p_session);
1620 if (p->p_ucred->cr_uid != (uid_t)name[0])
1624 case KERN_PROC_RUID:
1625 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1629 case KERN_PROC_PROC:
1636 error = sysctl_out_proc(p, req, flags);
1637 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1645 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1647 struct kern_proc_out_args iterarg;
1648 int *name = (int *)arg1;
1649 u_int namelen = arg2;
1651 int flags, oid_number;
1654 oid_number = oidp->oid_number;
1655 if (oid_number != KERN_PROC_ALL &&
1656 (oid_number & KERN_PROC_INC_THREAD) == 0)
1657 flags = KERN_PROC_NOTHREADS;
1660 oid_number &= ~KERN_PROC_INC_THREAD;
1662 #ifdef COMPAT_FREEBSD32
1663 if (req->flags & SCTL_MASK32)
1664 flags |= KERN_PROC_MASK32;
1666 if (oid_number == KERN_PROC_PID) {
1669 error = sysctl_wire_old_buffer(req, 0);
1672 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1674 error = sysctl_out_proc(p, req, flags);
1678 switch (oid_number) {
1683 case KERN_PROC_PROC:
1684 if (namelen != 0 && namelen != 1)
1693 if (req->oldptr == NULL) {
1694 /* overestimate by 5 procs */
1695 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1699 error = sysctl_wire_old_buffer(req, 0);
1703 iterarg.flags = flags;
1704 iterarg.oid_number = oid_number;
1706 iterarg.name = name;
1707 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1712 pargs_alloc(int len)
1716 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1718 refcount_init(&pa->ar_ref, 1);
1719 pa->ar_length = len;
1724 pargs_free(struct pargs *pa)
1731 pargs_hold(struct pargs *pa)
1736 refcount_acquire(&pa->ar_ref);
1740 pargs_drop(struct pargs *pa)
1745 if (refcount_release(&pa->ar_ref))
1750 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1756 * This may return a short read if the string is shorter than the chunk
1757 * and is aligned at the end of the page, and the following page is not
1760 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1766 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1768 enum proc_vector_type {
1774 #ifdef COMPAT_FREEBSD32
1776 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1777 size_t *vsizep, enum proc_vector_type type)
1779 struct freebsd32_ps_strings pss;
1781 vm_offset_t vptr, ptr;
1782 uint32_t *proc_vector32;
1788 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1789 sizeof(pss)) != sizeof(pss))
1793 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1794 vsize = pss.ps_nargvstr;
1795 if (vsize > ARG_MAX)
1797 size = vsize * sizeof(int32_t);
1800 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1801 vsize = pss.ps_nenvstr;
1802 if (vsize > ARG_MAX)
1804 size = vsize * sizeof(int32_t);
1807 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1808 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1811 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1812 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1815 if (aux.a_type == AT_NULL)
1819 if (aux.a_type != AT_NULL)
1822 size = vsize * sizeof(aux);
1825 KASSERT(0, ("Wrong proc vector type: %d", type));
1828 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1829 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1833 if (type == PROC_AUX) {
1834 *proc_vectorp = (char **)proc_vector32;
1838 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1839 for (i = 0; i < (int)vsize; i++)
1840 proc_vector[i] = PTRIN(proc_vector32[i]);
1841 *proc_vectorp = proc_vector;
1844 free(proc_vector32, M_TEMP);
1850 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1851 size_t *vsizep, enum proc_vector_type type)
1853 struct ps_strings pss;
1855 vm_offset_t vptr, ptr;
1860 #ifdef COMPAT_FREEBSD32
1861 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1862 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1864 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1865 sizeof(pss)) != sizeof(pss))
1869 vptr = (vm_offset_t)pss.ps_argvstr;
1870 vsize = pss.ps_nargvstr;
1871 if (vsize > ARG_MAX)
1873 size = vsize * sizeof(char *);
1876 vptr = (vm_offset_t)pss.ps_envstr;
1877 vsize = pss.ps_nenvstr;
1878 if (vsize > ARG_MAX)
1880 size = vsize * sizeof(char *);
1884 * The aux array is just above env array on the stack. Check
1885 * that the address is naturally aligned.
1887 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1889 #if __ELF_WORD_SIZE == 64
1890 if (vptr % sizeof(uint64_t) != 0)
1892 if (vptr % sizeof(uint32_t) != 0)
1896 * We count the array size reading the aux vectors from the
1897 * stack until AT_NULL vector is returned. So (to keep the code
1898 * simple) we read the process stack twice: the first time here
1899 * to find the size and the second time when copying the vectors
1900 * to the allocated proc_vector.
1902 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1903 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1906 if (aux.a_type == AT_NULL)
1911 * If the PROC_AUXV_MAX entries are iterated over, and we have
1912 * not reached AT_NULL, it is most likely we are reading wrong
1913 * data: either the process doesn't have auxv array or data has
1914 * been modified. Return the error in this case.
1916 if (aux.a_type != AT_NULL)
1919 size = vsize * sizeof(aux);
1922 KASSERT(0, ("Wrong proc vector type: %d", type));
1923 return (EINVAL); /* In case we are built without INVARIANTS. */
1925 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1926 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1927 free(proc_vector, M_TEMP);
1930 *proc_vectorp = proc_vector;
1936 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1939 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1940 enum proc_vector_type type)
1942 size_t done, len, nchr, vsize;
1944 char **proc_vector, *sptr;
1945 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1947 PROC_ASSERT_HELD(p);
1950 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1952 nchr = 2 * (PATH_MAX + ARG_MAX);
1954 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1957 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1959 * The program may have scribbled into its argv array, e.g. to
1960 * remove some arguments. If that has happened, break out
1961 * before trying to read from NULL.
1963 if (proc_vector[i] == NULL)
1965 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1966 error = proc_read_string(td, p, sptr, pss_string,
1967 sizeof(pss_string));
1970 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1971 if (done + len >= nchr)
1972 len = nchr - done - 1;
1973 sbuf_bcat(sb, pss_string, len);
1974 if (len != GET_PS_STRINGS_CHUNK_SZ)
1976 done += GET_PS_STRINGS_CHUNK_SZ;
1978 sbuf_bcat(sb, "", 1);
1982 free(proc_vector, M_TEMP);
1987 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1990 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1994 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1997 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2001 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2007 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2009 #ifdef COMPAT_FREEBSD32
2010 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2011 size = vsize * sizeof(Elf32_Auxinfo);
2014 size = vsize * sizeof(Elf_Auxinfo);
2015 if (sbuf_bcat(sb, auxv, size) != 0)
2023 * This sysctl allows a process to retrieve the argument list or process
2024 * title for another process without groping around in the address space
2025 * of the other process. It also allow a process to set its own "process
2026 * title to a string of its own choice.
2029 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2031 int *name = (int *)arg1;
2032 u_int namelen = arg2;
2033 struct pargs *newpa, *pa;
2036 int flags, error = 0, error2;
2042 pid = (pid_t)name[0];
2044 * If the query is for this process and it is single-threaded, there
2045 * is nobody to modify pargs, thus we can just read.
2048 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2049 (pa = p->p_args) != NULL)
2050 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2052 flags = PGET_CANSEE;
2053 if (req->newptr != NULL)
2054 flags |= PGET_ISCURRENT;
2055 error = pget(pid, flags, &p);
2063 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2065 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2068 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2069 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2070 error = proc_getargv(curthread, p, &sb);
2071 error2 = sbuf_finish(&sb);
2074 if (error == 0 && error2 != 0)
2079 if (error != 0 || req->newptr == NULL)
2082 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2085 if (req->newlen == 0) {
2087 * Clear the argument pointer, so that we'll fetch arguments
2088 * with proc_getargv() until further notice.
2092 newpa = pargs_alloc(req->newlen);
2093 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2108 * This sysctl allows a process to retrieve environment of another process.
2111 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2113 int *name = (int *)arg1;
2114 u_int namelen = arg2;
2122 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2125 if ((p->p_flag & P_SYSTEM) != 0) {
2130 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2131 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2132 error = proc_getenvv(curthread, p, &sb);
2133 error2 = sbuf_finish(&sb);
2136 return (error != 0 ? error : error2);
2140 * This sysctl allows a process to retrieve ELF auxiliary vector of
2144 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2146 int *name = (int *)arg1;
2147 u_int namelen = arg2;
2155 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2158 if ((p->p_flag & P_SYSTEM) != 0) {
2162 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2163 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2164 error = proc_getauxv(curthread, p, &sb);
2165 error2 = sbuf_finish(&sb);
2168 return (error != 0 ? error : error2);
2172 * This sysctl allows a process to retrieve the path of the executable for
2173 * itself or another process.
2176 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2178 pid_t *pidp = (pid_t *)arg1;
2179 unsigned int arglen = arg2;
2182 char *retbuf, *freebuf;
2187 if (*pidp == -1) { /* -1 means this process */
2188 p = req->td->td_proc;
2190 error = pget(*pidp, PGET_CANSEE, &p);
2204 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2208 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2209 free(freebuf, M_TEMP);
2214 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2227 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2230 sv_name = p->p_sysent->sv_name;
2232 return (sysctl_handle_string(oidp, sv_name, 0, req));
2235 #ifdef KINFO_OVMENTRY_SIZE
2236 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2239 #ifdef COMPAT_FREEBSD7
2241 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2243 vm_map_entry_t entry, tmp_entry;
2244 unsigned int last_timestamp;
2245 char *fullpath, *freepath;
2246 struct kinfo_ovmentry *kve;
2256 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2259 vm = vmspace_acquire_ref(p);
2264 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2267 vm_map_lock_read(map);
2268 for (entry = map->header.next; entry != &map->header;
2269 entry = entry->next) {
2270 vm_object_t obj, tobj, lobj;
2273 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2276 bzero(kve, sizeof(*kve));
2277 kve->kve_structsize = sizeof(*kve);
2279 kve->kve_private_resident = 0;
2280 obj = entry->object.vm_object;
2282 VM_OBJECT_RLOCK(obj);
2283 if (obj->shadow_count == 1)
2284 kve->kve_private_resident =
2285 obj->resident_page_count;
2287 kve->kve_resident = 0;
2288 addr = entry->start;
2289 while (addr < entry->end) {
2290 if (pmap_extract(map->pmap, addr))
2291 kve->kve_resident++;
2295 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2297 VM_OBJECT_RLOCK(tobj);
2298 kve->kve_offset += tobj->backing_object_offset;
2301 VM_OBJECT_RUNLOCK(lobj);
2305 kve->kve_start = (void*)entry->start;
2306 kve->kve_end = (void*)entry->end;
2307 kve->kve_offset += (off_t)entry->offset;
2309 if (entry->protection & VM_PROT_READ)
2310 kve->kve_protection |= KVME_PROT_READ;
2311 if (entry->protection & VM_PROT_WRITE)
2312 kve->kve_protection |= KVME_PROT_WRITE;
2313 if (entry->protection & VM_PROT_EXECUTE)
2314 kve->kve_protection |= KVME_PROT_EXEC;
2316 if (entry->eflags & MAP_ENTRY_COW)
2317 kve->kve_flags |= KVME_FLAG_COW;
2318 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2319 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2320 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2321 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2323 last_timestamp = map->timestamp;
2324 vm_map_unlock_read(map);
2326 kve->kve_fileid = 0;
2331 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2332 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2333 kve->kve_type = KVME_TYPE_UNKNOWN;
2337 VM_OBJECT_RUNLOCK(lobj);
2339 kve->kve_ref_count = obj->ref_count;
2340 kve->kve_shadow_count = obj->shadow_count;
2341 VM_OBJECT_RUNLOCK(obj);
2343 vn_fullpath(curthread, vp, &fullpath,
2345 cred = curthread->td_ucred;
2346 vn_lock(vp, LK_SHARED | LK_RETRY);
2347 if (VOP_GETATTR(vp, &va, cred) == 0) {
2348 kve->kve_fileid = va.va_fileid;
2350 kve->kve_fsid = va.va_fsid;
2355 kve->kve_type = KVME_TYPE_NONE;
2356 kve->kve_ref_count = 0;
2357 kve->kve_shadow_count = 0;
2360 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2361 if (freepath != NULL)
2362 free(freepath, M_TEMP);
2364 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2365 vm_map_lock_read(map);
2368 if (last_timestamp != map->timestamp) {
2369 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2373 vm_map_unlock_read(map);
2379 #endif /* COMPAT_FREEBSD7 */
2381 #ifdef KINFO_VMENTRY_SIZE
2382 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2386 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2387 int *resident_count, bool *super)
2389 vm_object_t obj, tobj;
2392 vm_paddr_t locked_pa;
2393 vm_pindex_t pi, pi_adv, pindex;
2396 *resident_count = 0;
2397 if (vmmap_skip_res_cnt)
2401 obj = entry->object.vm_object;
2402 addr = entry->start;
2404 pi = OFF_TO_IDX(entry->offset);
2405 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2406 if (m_adv != NULL) {
2409 pi_adv = atop(entry->end - addr);
2411 for (tobj = obj;; tobj = tobj->backing_object) {
2412 m = vm_page_find_least(tobj, pindex);
2414 if (m->pindex == pindex)
2416 if (pi_adv > m->pindex - pindex) {
2417 pi_adv = m->pindex - pindex;
2421 if (tobj->backing_object == NULL)
2423 pindex += OFF_TO_IDX(tobj->
2424 backing_object_offset);
2428 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2429 (addr & (pagesizes[1] - 1)) == 0 &&
2430 (pmap_mincore(map->pmap, addr, &locked_pa) &
2431 MINCORE_SUPER) != 0) {
2433 pi_adv = atop(pagesizes[1]);
2436 * We do not test the found page on validity.
2437 * Either the page is busy and being paged in,
2438 * or it was invalidated. The first case
2439 * should be counted as resident, the second
2440 * is not so clear; we do account both.
2444 *resident_count += pi_adv;
2447 PA_UNLOCK_COND(locked_pa);
2451 * Must be called with the process locked and will return unlocked.
2454 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2456 vm_map_entry_t entry, tmp_entry;
2459 vm_object_t obj, tobj, lobj;
2460 char *fullpath, *freepath;
2461 struct kinfo_vmentry *kve;
2466 unsigned int last_timestamp;
2470 PROC_LOCK_ASSERT(p, MA_OWNED);
2474 vm = vmspace_acquire_ref(p);
2479 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2483 vm_map_lock_read(map);
2484 for (entry = map->header.next; entry != &map->header;
2485 entry = entry->next) {
2486 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2490 bzero(kve, sizeof(*kve));
2491 obj = entry->object.vm_object;
2493 for (tobj = obj; tobj != NULL;
2494 tobj = tobj->backing_object) {
2495 VM_OBJECT_RLOCK(tobj);
2496 kve->kve_offset += tobj->backing_object_offset;
2499 if (obj->backing_object == NULL)
2500 kve->kve_private_resident =
2501 obj->resident_page_count;
2502 kern_proc_vmmap_resident(map, entry,
2503 &kve->kve_resident, &super);
2505 kve->kve_flags |= KVME_FLAG_SUPER;
2506 for (tobj = obj; tobj != NULL;
2507 tobj = tobj->backing_object) {
2508 if (tobj != obj && tobj != lobj)
2509 VM_OBJECT_RUNLOCK(tobj);
2515 kve->kve_start = entry->start;
2516 kve->kve_end = entry->end;
2517 kve->kve_offset += entry->offset;
2519 if (entry->protection & VM_PROT_READ)
2520 kve->kve_protection |= KVME_PROT_READ;
2521 if (entry->protection & VM_PROT_WRITE)
2522 kve->kve_protection |= KVME_PROT_WRITE;
2523 if (entry->protection & VM_PROT_EXECUTE)
2524 kve->kve_protection |= KVME_PROT_EXEC;
2526 if (entry->eflags & MAP_ENTRY_COW)
2527 kve->kve_flags |= KVME_FLAG_COW;
2528 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2529 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2530 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2531 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2532 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2533 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2534 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2535 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2536 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2537 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2539 last_timestamp = map->timestamp;
2540 vm_map_unlock_read(map);
2545 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2549 VM_OBJECT_RUNLOCK(lobj);
2551 kve->kve_ref_count = obj->ref_count;
2552 kve->kve_shadow_count = obj->shadow_count;
2553 VM_OBJECT_RUNLOCK(obj);
2555 vn_fullpath(curthread, vp, &fullpath,
2557 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2558 cred = curthread->td_ucred;
2559 vn_lock(vp, LK_SHARED | LK_RETRY);
2560 if (VOP_GETATTR(vp, &va, cred) == 0) {
2561 kve->kve_vn_fileid = va.va_fileid;
2562 kve->kve_vn_fsid = va.va_fsid;
2563 kve->kve_vn_fsid_freebsd11 =
2564 kve->kve_vn_fsid; /* truncate */
2566 MAKEIMODE(va.va_type, va.va_mode);
2567 kve->kve_vn_size = va.va_size;
2568 kve->kve_vn_rdev = va.va_rdev;
2569 kve->kve_vn_rdev_freebsd11 =
2570 kve->kve_vn_rdev; /* truncate */
2571 kve->kve_status = KF_ATTR_VALID;
2576 kve->kve_type = KVME_TYPE_NONE;
2577 kve->kve_ref_count = 0;
2578 kve->kve_shadow_count = 0;
2581 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2582 if (freepath != NULL)
2583 free(freepath, M_TEMP);
2585 /* Pack record size down */
2586 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2587 kve->kve_structsize =
2588 offsetof(struct kinfo_vmentry, kve_path) +
2589 strlen(kve->kve_path) + 1;
2591 kve->kve_structsize = sizeof(*kve);
2592 kve->kve_structsize = roundup(kve->kve_structsize,
2595 /* Halt filling and truncate rather than exceeding maxlen */
2596 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2598 vm_map_lock_read(map);
2600 } else if (maxlen != -1)
2601 maxlen -= kve->kve_structsize;
2603 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2605 vm_map_lock_read(map);
2608 if (last_timestamp != map->timestamp) {
2609 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2613 vm_map_unlock_read(map);
2621 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2625 int error, error2, *name;
2628 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2629 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2630 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2635 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2636 error2 = sbuf_finish(&sb);
2638 return (error != 0 ? error : error2);
2641 #if defined(STACK) || defined(DDB)
2643 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2645 struct kinfo_kstack *kkstp;
2646 int error, i, *name, numthreads;
2647 lwpid_t *lwpidarray;
2654 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2658 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2659 st = stack_create(M_WAITOK);
2664 if (lwpidarray != NULL) {
2665 free(lwpidarray, M_TEMP);
2668 numthreads = p->p_numthreads;
2670 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2673 } while (numthreads < p->p_numthreads);
2676 * XXXRW: During the below loop, execve(2) and countless other sorts
2677 * of changes could have taken place. Should we check to see if the
2678 * vmspace has been replaced, or the like, in order to prevent
2679 * giving a snapshot that spans, say, execve(2), with some threads
2680 * before and some after? Among other things, the credentials could
2681 * have changed, in which case the right to extract debug info might
2682 * no longer be assured.
2685 FOREACH_THREAD_IN_PROC(p, td) {
2686 KASSERT(i < numthreads,
2687 ("sysctl_kern_proc_kstack: numthreads"));
2688 lwpidarray[i] = td->td_tid;
2692 for (i = 0; i < numthreads; i++) {
2693 td = thread_find(p, lwpidarray[i]);
2697 bzero(kkstp, sizeof(*kkstp));
2698 (void)sbuf_new(&sb, kkstp->kkst_trace,
2699 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2701 kkstp->kkst_tid = td->td_tid;
2702 if (TD_IS_SWAPPED(td)) {
2703 kkstp->kkst_state = KKST_STATE_SWAPPED;
2704 } else if (TD_IS_RUNNING(td)) {
2705 if (stack_save_td_running(st, td) == 0)
2706 kkstp->kkst_state = KKST_STATE_STACKOK;
2708 kkstp->kkst_state = KKST_STATE_RUNNING;
2710 kkstp->kkst_state = KKST_STATE_STACKOK;
2711 stack_save_td(st, td);
2715 stack_sbuf_print(&sb, st);
2718 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2725 if (lwpidarray != NULL)
2726 free(lwpidarray, M_TEMP);
2728 free(kkstp, M_TEMP);
2734 * This sysctl allows a process to retrieve the full list of groups from
2735 * itself or another process.
2738 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2740 pid_t *pidp = (pid_t *)arg1;
2741 unsigned int arglen = arg2;
2748 if (*pidp == -1) { /* -1 means this process */
2749 p = req->td->td_proc;
2752 error = pget(*pidp, PGET_CANSEE, &p);
2757 cred = crhold(p->p_ucred);
2760 error = SYSCTL_OUT(req, cred->cr_groups,
2761 cred->cr_ngroups * sizeof(gid_t));
2767 * This sysctl allows a process to retrieve or/and set the resource limit for
2771 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2773 int *name = (int *)arg1;
2774 u_int namelen = arg2;
2783 which = (u_int)name[1];
2784 if (which >= RLIM_NLIMITS)
2787 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2790 flags = PGET_HOLD | PGET_NOTWEXIT;
2791 if (req->newptr != NULL)
2792 flags |= PGET_CANDEBUG;
2794 flags |= PGET_CANSEE;
2795 error = pget((pid_t)name[0], flags, &p);
2802 if (req->oldptr != NULL) {
2804 lim_rlimit_proc(p, which, &rlim);
2807 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2814 if (req->newptr != NULL) {
2815 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2817 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2826 * This sysctl allows a process to retrieve ps_strings structure location of
2830 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2832 int *name = (int *)arg1;
2833 u_int namelen = arg2;
2835 vm_offset_t ps_strings;
2837 #ifdef COMPAT_FREEBSD32
2838 uint32_t ps_strings32;
2844 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2847 #ifdef COMPAT_FREEBSD32
2848 if ((req->flags & SCTL_MASK32) != 0) {
2850 * We return 0 if the 32 bit emulation request is for a 64 bit
2853 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2854 PTROUT(p->p_sysent->sv_psstrings) : 0;
2856 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2860 ps_strings = p->p_sysent->sv_psstrings;
2862 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2867 * This sysctl allows a process to retrieve umask of another process.
2870 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2872 int *name = (int *)arg1;
2873 u_int namelen = arg2;
2882 pid = (pid_t)name[0];
2884 if (pid == p->p_pid || pid == 0) {
2885 fd_cmask = p->p_fd->fd_cmask;
2889 error = pget(pid, PGET_WANTREAD, &p);
2893 fd_cmask = p->p_fd->fd_cmask;
2896 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2901 * This sysctl allows a process to set and retrieve binary osreldate of
2905 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2907 int *name = (int *)arg1;
2908 u_int namelen = arg2;
2910 int flags, error, osrel;
2915 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2918 flags = PGET_HOLD | PGET_NOTWEXIT;
2919 if (req->newptr != NULL)
2920 flags |= PGET_CANDEBUG;
2922 flags |= PGET_CANSEE;
2923 error = pget((pid_t)name[0], flags, &p);
2927 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2931 if (req->newptr != NULL) {
2932 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2947 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2949 int *name = (int *)arg1;
2950 u_int namelen = arg2;
2952 struct kinfo_sigtramp kst;
2953 const struct sysentvec *sv;
2955 #ifdef COMPAT_FREEBSD32
2956 struct kinfo_sigtramp32 kst32;
2962 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2966 #ifdef COMPAT_FREEBSD32
2967 if ((req->flags & SCTL_MASK32) != 0) {
2968 bzero(&kst32, sizeof(kst32));
2969 if (SV_PROC_FLAG(p, SV_ILP32)) {
2970 if (sv->sv_sigcode_base != 0) {
2971 kst32.ksigtramp_start = sv->sv_sigcode_base;
2972 kst32.ksigtramp_end = sv->sv_sigcode_base +
2975 kst32.ksigtramp_start = sv->sv_psstrings -
2977 kst32.ksigtramp_end = sv->sv_psstrings;
2981 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2985 bzero(&kst, sizeof(kst));
2986 if (sv->sv_sigcode_base != 0) {
2987 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2988 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2991 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2993 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2996 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3000 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
3002 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3003 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3004 "Return entire process table");
3006 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3007 sysctl_kern_proc, "Process table");
3009 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3010 sysctl_kern_proc, "Process table");
3012 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3013 sysctl_kern_proc, "Process table");
3015 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3016 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3018 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3019 sysctl_kern_proc, "Process table");
3021 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3022 sysctl_kern_proc, "Process table");
3024 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3025 sysctl_kern_proc, "Process table");
3027 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3028 sysctl_kern_proc, "Process table");
3030 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3031 sysctl_kern_proc, "Return process table, no threads");
3033 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3034 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3035 sysctl_kern_proc_args, "Process argument list");
3037 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3038 sysctl_kern_proc_env, "Process environment");
3040 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3041 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3043 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3044 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3046 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3047 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3048 "Process syscall vector name (ABI type)");
3050 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3051 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3053 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3054 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3056 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3057 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3059 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3060 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3062 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3063 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3065 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3066 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3068 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3069 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3071 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3072 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3074 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3075 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3076 "Return process table, no threads");
3078 #ifdef COMPAT_FREEBSD7
3079 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3080 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3083 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3084 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3086 #if defined(STACK) || defined(DDB)
3087 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3088 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3091 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3092 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3094 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3095 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3096 "Process resource limits");
3098 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3099 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3100 "Process ps_strings location");
3102 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3103 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3105 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3106 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3107 "Process binary osreldate");
3109 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3110 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3111 "Process signal trampoline location");
3116 * stop_all_proc() purpose is to stop all process which have usermode,
3117 * except current process for obvious reasons. This makes it somewhat
3118 * unreliable when invoked from multithreaded process. The service
3119 * must not be user-callable anyway.
3124 struct proc *cp, *p;
3126 bool restart, seen_stopped, seen_exiting, stopped_some;
3130 sx_xlock(&allproc_lock);
3132 seen_exiting = seen_stopped = stopped_some = restart = false;
3133 LIST_REMOVE(cp, p_list);
3134 LIST_INSERT_HEAD(&allproc, cp, p_list);
3136 p = LIST_NEXT(cp, p_list);
3139 LIST_REMOVE(cp, p_list);
3140 LIST_INSERT_AFTER(p, cp, p_list);
3142 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3146 if ((p->p_flag & P_WEXIT) != 0) {
3147 seen_exiting = true;
3151 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3153 * Stopped processes are tolerated when there
3154 * are no other processes which might continue
3155 * them. P_STOPPED_SINGLE but not
3156 * P_TOTAL_STOP process still has at least one
3159 seen_stopped = true;
3163 sx_xunlock(&allproc_lock);
3165 r = thread_single(p, SINGLE_ALLPROC);
3169 stopped_some = true;
3172 sx_xlock(&allproc_lock);
3174 /* Catch forked children we did not see in iteration. */
3175 if (gen != allproc_gen)
3177 sx_xunlock(&allproc_lock);
3178 if (restart || stopped_some || seen_exiting || seen_stopped) {
3179 kern_yield(PRI_USER);
3185 resume_all_proc(void)
3187 struct proc *cp, *p;
3190 sx_xlock(&allproc_lock);
3192 LIST_REMOVE(cp, p_list);
3193 LIST_INSERT_HEAD(&allproc, cp, p_list);
3195 p = LIST_NEXT(cp, p_list);
3198 LIST_REMOVE(cp, p_list);
3199 LIST_INSERT_AFTER(p, cp, p_list);
3201 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3202 sx_xunlock(&allproc_lock);
3204 thread_single_end(p, SINGLE_ALLPROC);
3207 sx_xlock(&allproc_lock);
3212 /* Did the loop above missed any stopped process ? */
3213 FOREACH_PROC_IN_SYSTEM(p) {
3214 /* No need for proc lock. */
3215 if ((p->p_flag & P_TOTAL_STOP) != 0)
3218 sx_xunlock(&allproc_lock);
3221 /* #define TOTAL_STOP_DEBUG 1 */
3222 #ifdef TOTAL_STOP_DEBUG
3223 volatile static int ap_resume;
3224 #include <sys/mount.h>
3227 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3233 error = sysctl_handle_int(oidp, &val, 0, req);
3234 if (error != 0 || req->newptr == NULL)
3239 while (ap_resume == 0)
3247 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3248 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3249 sysctl_debug_stop_all_proc, "I",