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/dtrace_bsd.h>
69 #include <sys/sysctl.h>
70 #include <sys/filedesc.h>
72 #include <sys/signalvar.h>
76 #include <sys/vnode.h>
84 #include <vm/vm_param.h>
85 #include <vm/vm_extern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
92 #include <fs/devfs/devfs.h>
94 #ifdef COMPAT_FREEBSD32
95 #include <compat/freebsd32/freebsd32.h>
96 #include <compat/freebsd32/freebsd32_util.h>
99 SDT_PROVIDER_DEFINE(proc);
101 MALLOC_DEFINE(M_SESSION, "session", "session header");
102 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
103 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
105 static void doenterpgrp(struct proc *, struct pgrp *);
106 static void orphanpg(struct pgrp *pg);
107 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
108 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
109 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
111 static void pgdelete(struct pgrp *);
112 static int pgrp_init(void *mem, int size, int flags);
113 static int proc_ctor(void *mem, int size, void *arg, int flags);
114 static void proc_dtor(void *mem, int size, void *arg);
115 static int proc_init(void *mem, int size, int flags);
116 static void proc_fini(void *mem, int size);
117 static void pargs_free(struct pargs *pa);
120 * Other process lists
122 struct pidhashhead *pidhashtbl;
123 struct sx *pidhashtbl_lock;
126 struct pgrphashhead *pgrphashtbl;
128 struct proclist allproc;
129 struct sx __exclusive_cache_line allproc_lock;
130 struct sx __exclusive_cache_line proctree_lock;
131 struct mtx __exclusive_cache_line ppeers_lock;
132 struct mtx __exclusive_cache_line procid_lock;
133 uma_zone_t proc_zone;
134 uma_zone_t pgrp_zone;
137 * The offset of various fields in struct proc and struct thread.
138 * These are used by kernel debuggers to enumerate kernel threads and
141 const int proc_off_p_pid = offsetof(struct proc, p_pid);
142 const int proc_off_p_comm = offsetof(struct proc, p_comm);
143 const int proc_off_p_list = offsetof(struct proc, p_list);
144 const int proc_off_p_hash = offsetof(struct proc, p_hash);
145 const int proc_off_p_threads = offsetof(struct proc, p_threads);
146 const int thread_off_td_tid = offsetof(struct thread, td_tid);
147 const int thread_off_td_name = offsetof(struct thread, td_name);
148 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
149 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
150 const int thread_off_td_plist = offsetof(struct thread, td_plist);
152 EVENTHANDLER_LIST_DEFINE(process_ctor);
153 EVENTHANDLER_LIST_DEFINE(process_dtor);
154 EVENTHANDLER_LIST_DEFINE(process_init);
155 EVENTHANDLER_LIST_DEFINE(process_fini);
156 EVENTHANDLER_LIST_DEFINE(process_exit);
157 EVENTHANDLER_LIST_DEFINE(process_fork);
158 EVENTHANDLER_LIST_DEFINE(process_exec);
160 int kstack_pages = KSTACK_PAGES;
161 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
162 "Kernel stack size in pages");
163 static int vmmap_skip_res_cnt = 0;
164 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
165 &vmmap_skip_res_cnt, 0,
166 "Skip calculation of the pages resident count in kern.proc.vmmap");
168 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
169 #ifdef COMPAT_FREEBSD32
170 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
174 * Initialize global process hashing structures.
181 sx_init(&allproc_lock, "allproc");
182 sx_init(&proctree_lock, "proctree");
183 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
184 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
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);
198 pgrp_zone = uma_zcreate("PGRP", sizeof(struct pgrp), NULL, NULL,
199 pgrp_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
204 * Prepare a proc for use.
207 proc_ctor(void *mem, int size, void *arg, int flags)
212 p = (struct proc *)mem;
214 kdtrace_proc_ctor(p);
216 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
217 td = FIRST_THREAD_IN_PROC(p);
219 /* Make sure all thread constructors are executed */
220 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
226 * Reclaim a proc after use.
229 proc_dtor(void *mem, int size, void *arg)
234 /* INVARIANTS checks go here */
235 p = (struct proc *)mem;
236 td = FIRST_THREAD_IN_PROC(p);
239 KASSERT((p->p_numthreads == 1),
240 ("bad number of threads in exiting process"));
241 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
243 /* Free all OSD associated to this thread. */
245 td_softdep_cleanup(td);
246 MPASS(td->td_su == NULL);
248 /* Make sure all thread destructors are executed */
249 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
251 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
253 kdtrace_proc_dtor(p);
255 if (p->p_ksi != NULL)
256 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
260 * Initialize type-stable parts of a proc (when newly created).
263 proc_init(void *mem, int size, int flags)
267 p = (struct proc *)mem;
268 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
269 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
270 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
271 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
272 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
273 cv_init(&p->p_pwait, "ppwait");
274 TAILQ_INIT(&p->p_threads); /* all threads in proc */
275 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
276 p->p_stats = pstats_alloc();
282 * UMA should ensure that this function is never called.
283 * Freeing a proc structure would violate type stability.
286 proc_fini(void *mem, int size)
291 p = (struct proc *)mem;
292 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
293 pstats_free(p->p_stats);
294 thread_free(FIRST_THREAD_IN_PROC(p));
295 mtx_destroy(&p->p_mtx);
296 if (p->p_ksi != NULL)
297 ksiginfo_free(p->p_ksi);
299 panic("proc reclaimed");
304 pgrp_init(void *mem, int size, int flags)
309 mtx_init(&pg->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
314 * PID space management.
316 * These bitmaps are used by fork_findpid.
318 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
319 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
320 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
321 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
323 static bitstr_t *proc_id_array[] = {
331 proc_id_set(int type, pid_t id)
334 KASSERT(type >= 0 && type < nitems(proc_id_array),
335 ("invalid type %d\n", type));
336 mtx_lock(&procid_lock);
337 KASSERT(bit_test(proc_id_array[type], id) == 0,
338 ("bit %d already set in %d\n", id, type));
339 bit_set(proc_id_array[type], id);
340 mtx_unlock(&procid_lock);
344 proc_id_set_cond(int type, pid_t id)
347 KASSERT(type >= 0 && type < nitems(proc_id_array),
348 ("invalid type %d\n", type));
349 if (bit_test(proc_id_array[type], id))
351 mtx_lock(&procid_lock);
352 bit_set(proc_id_array[type], id);
353 mtx_unlock(&procid_lock);
357 proc_id_clear(int type, pid_t id)
360 KASSERT(type >= 0 && type < nitems(proc_id_array),
361 ("invalid type %d\n", type));
362 mtx_lock(&procid_lock);
363 KASSERT(bit_test(proc_id_array[type], id) != 0,
364 ("bit %d not set in %d\n", id, type));
365 bit_clear(proc_id_array[type], id);
366 mtx_unlock(&procid_lock);
370 * Is p an inferior of the current process?
373 inferior(struct proc *p)
376 sx_assert(&proctree_lock, SX_LOCKED);
377 PROC_LOCK_ASSERT(p, MA_OWNED);
378 for (; p != curproc; p = proc_realparent(p)) {
386 * Shared lock all the pid hash lists.
389 pidhash_slockall(void)
393 for (i = 0; i < pidhashlock + 1; i++)
394 sx_slock(&pidhashtbl_lock[i]);
398 * Shared unlock all the pid hash lists.
401 pidhash_sunlockall(void)
405 for (i = 0; i < pidhashlock + 1; i++)
406 sx_sunlock(&pidhashtbl_lock[i]);
410 * Similar to pfind_any(), this function finds zombies.
413 pfind_any_locked(pid_t pid)
417 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
418 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
419 if (p->p_pid == pid) {
421 if (p->p_state == PRS_NEW) {
432 * Locate a process by number.
434 * By not returning processes in the PRS_NEW state, we allow callers to avoid
435 * testing for that condition to avoid dereferencing p_ucred, et al.
437 static __always_inline struct proc *
438 _pfind(pid_t pid, bool zombie)
443 if (p->p_pid == pid) {
447 sx_slock(PIDHASHLOCK(pid));
448 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
449 if (p->p_pid == pid) {
451 if (p->p_state == PRS_NEW ||
452 (!zombie && p->p_state == PRS_ZOMBIE)) {
459 sx_sunlock(PIDHASHLOCK(pid));
467 return (_pfind(pid, false));
471 * Same as pfind but allow zombies.
477 return (_pfind(pid, true));
481 * Locate a process group by number.
482 * The caller must hold proctree_lock.
489 sx_assert(&proctree_lock, SX_LOCKED);
491 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
492 if (pgrp->pg_id == pgid) {
501 * Locate process and do additional manipulations, depending on flags.
504 pget(pid_t pid, int flags, struct proc **pp)
511 if (p->p_pid == pid) {
515 if (pid <= PID_MAX) {
516 if ((flags & PGET_NOTWEXIT) == 0)
520 } else if ((flags & PGET_NOTID) == 0) {
521 td1 = tdfind(pid, -1);
527 if ((flags & PGET_CANSEE) != 0) {
528 error = p_cansee(curthread, p);
533 if ((flags & PGET_CANDEBUG) != 0) {
534 error = p_candebug(curthread, p);
538 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
542 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
546 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
548 * XXXRW: Not clear ESRCH is the right error during proc
554 if ((flags & PGET_HOLD) != 0) {
566 * Create a new process group.
567 * pgid must be equal to the pid of p.
568 * Begin a new session if required.
571 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
574 sx_assert(&proctree_lock, SX_XLOCKED);
576 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
577 KASSERT(p->p_pid == pgid,
578 ("enterpgrp: new pgrp and pid != pgid"));
579 KASSERT(pgfind(pgid) == NULL,
580 ("enterpgrp: pgrp with pgid exists"));
581 KASSERT(!SESS_LEADER(p),
582 ("enterpgrp: session leader attempted setpgrp"));
588 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
590 p->p_flag &= ~P_CONTROLT;
594 sess->s_sid = p->p_pid;
595 proc_id_set(PROC_ID_SESSION, p->p_pid);
596 refcount_init(&sess->s_count, 1);
597 sess->s_ttyvp = NULL;
598 sess->s_ttydp = NULL;
600 bcopy(p->p_session->s_login, sess->s_login,
601 sizeof(sess->s_login));
602 pgrp->pg_session = sess;
603 KASSERT(p == curproc,
604 ("enterpgrp: mksession and p != curproc"));
606 pgrp->pg_session = p->p_session;
607 sess_hold(pgrp->pg_session);
611 proc_id_set(PROC_ID_GROUP, p->p_pid);
612 LIST_INIT(&pgrp->pg_members);
616 * As we have an exclusive lock of proctree_lock,
617 * this should not deadlock.
619 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
620 SLIST_INIT(&pgrp->pg_sigiolst);
623 doenterpgrp(p, pgrp);
629 * Move p to an existing process group
632 enterthispgrp(struct proc *p, struct pgrp *pgrp)
635 sx_assert(&proctree_lock, SX_XLOCKED);
636 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
637 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
638 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
639 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
640 KASSERT(pgrp->pg_session == p->p_session,
641 ("%s: pgrp's session %p, p->p_session %p proc %p\n",
642 __func__, pgrp->pg_session, p->p_session, p));
643 KASSERT(pgrp != p->p_pgrp,
644 ("%s: p %p belongs to pgrp %p", __func__, p, pgrp));
646 doenterpgrp(p, pgrp);
652 * If true, any child of q which belongs to group pgrp, qualifies the
653 * process group pgrp as not orphaned.
656 isjobproc(struct proc *q, struct pgrp *pgrp)
658 sx_assert(&proctree_lock, SX_LOCKED);
660 return (q->p_pgrp != pgrp &&
661 q->p_pgrp->pg_session == pgrp->pg_session);
665 jobc_reaper(struct proc *p)
669 sx_assert(&proctree_lock, SA_LOCKED);
673 if (pp->p_reaper == pp ||
674 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
680 jobc_parent(struct proc *p, struct proc *p_exiting)
684 sx_assert(&proctree_lock, SA_LOCKED);
686 pp = proc_realparent(p);
687 if (pp->p_pptr == NULL || pp == p_exiting ||
688 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
690 return (jobc_reaper(pp));
694 pgrp_calc_jobc(struct pgrp *pgrp)
700 if (!mtx_owned(&pgrp->pg_mtx))
701 sx_assert(&proctree_lock, SA_LOCKED);
705 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
706 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
709 if (isjobproc(jobc_parent(q, NULL), pgrp))
716 * Move p to a process group
719 doenterpgrp(struct proc *p, struct pgrp *pgrp)
721 struct pgrp *savepgrp;
724 sx_assert(&proctree_lock, SX_XLOCKED);
725 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
726 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
727 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
728 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
730 savepgrp = p->p_pgrp;
731 pp = jobc_parent(p, NULL);
735 if (isjobproc(pp, savepgrp) && pgrp_calc_jobc(savepgrp) == 1)
738 LIST_REMOVE(p, p_pglist);
741 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
742 if (isjobproc(pp, pgrp))
743 pgrp->pg_flags &= ~PGRP_ORPHANED;
744 PGRP_UNLOCK(savepgrp);
746 if (LIST_EMPTY(&savepgrp->pg_members))
751 * remove process from process group
754 leavepgrp(struct proc *p)
756 struct pgrp *savepgrp;
758 sx_assert(&proctree_lock, SX_XLOCKED);
759 savepgrp = p->p_pgrp;
762 LIST_REMOVE(p, p_pglist);
765 PGRP_UNLOCK(savepgrp);
766 if (LIST_EMPTY(&savepgrp->pg_members))
772 * delete a process group
775 pgdelete(struct pgrp *pgrp)
777 struct session *savesess;
780 sx_assert(&proctree_lock, SX_XLOCKED);
781 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
782 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
785 * Reset any sigio structures pointing to us as a result of
786 * F_SETOWN with our pgid. The proctree lock ensures that
787 * new sigio structures will not be added after this point.
789 funsetownlst(&pgrp->pg_sigiolst);
792 tp = pgrp->pg_session->s_ttyp;
793 LIST_REMOVE(pgrp, pg_hash);
794 savesess = pgrp->pg_session;
797 /* Remove the reference to the pgrp before deallocating it. */
800 tty_rel_pgrp(tp, pgrp);
803 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
804 uma_zfree(pgrp_zone, pgrp);
805 sess_release(savesess);
810 fixjobc_kill(struct proc *p)
815 sx_assert(&proctree_lock, SX_LOCKED);
816 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
818 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
819 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
822 * p no longer affects process group orphanage for children.
823 * It is marked by the flag because p is only physically
824 * removed from its process group on wait(2).
826 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
827 p->p_treeflag |= P_TREE_GRPEXITED;
830 * Check if exiting p orphans its own group.
833 if (isjobproc(jobc_parent(p, NULL), pgrp)) {
835 if (pgrp_calc_jobc(pgrp) == 0)
841 * Check this process' children to see whether they qualify
842 * their process groups after reparenting to reaper.
844 LIST_FOREACH(q, &p->p_children, p_sibling) {
847 if (pgrp_calc_jobc(pgrp) == 0) {
849 * We want to handle exactly the children that
850 * has p as realparent. Then, when calculating
851 * jobc_parent for children, we should ignore
852 * P_TREE_GRPEXITED flag already set on p.
854 if (jobc_parent(q, p) == p && isjobproc(p, pgrp))
857 pgrp->pg_flags &= ~PGRP_ORPHANED;
860 LIST_FOREACH(q, &p->p_orphans, p_orphan) {
863 if (pgrp_calc_jobc(pgrp) == 0) {
864 if (isjobproc(p, pgrp))
867 pgrp->pg_flags &= ~PGRP_ORPHANED;
881 MPASS(p->p_flag & P_WEXIT);
882 sx_assert(&proctree_lock, SX_LOCKED);
884 if (SESS_LEADER(p)) {
888 * s_ttyp is not zero'd; we use this to indicate that
889 * the session once had a controlling terminal. (for
890 * logging and informational purposes)
901 * Signal foreground pgrp and revoke access to
902 * controlling terminal if it has not been revoked
905 * Because the TTY may have been revoked in the mean
906 * time and could already have a new session associated
907 * with it, make sure we don't send a SIGHUP to a
908 * foreground process group that does not belong to this
914 if (tp->t_session == sp)
915 tty_signal_pgrp(tp, SIGHUP);
920 sx_xunlock(&proctree_lock);
921 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
922 VOP_REVOKE(ttyvp, REVOKEALL);
925 devfs_ctty_unref(ttyvp);
926 sx_xlock(&proctree_lock);
933 * A process group has become orphaned, mark it as such for signal
934 * delivery code. If there are any stopped processes in the group,
935 * hang-up all process in that group.
938 orphanpg(struct pgrp *pg)
942 PGRP_LOCK_ASSERT(pg, MA_OWNED);
944 pg->pg_flags |= PGRP_ORPHANED;
946 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
948 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
950 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
952 kern_psignal(p, SIGHUP);
953 kern_psignal(p, SIGCONT);
963 sess_hold(struct session *s)
966 refcount_acquire(&s->s_count);
970 sess_release(struct session *s)
973 if (refcount_release(&s->s_count)) {
974 if (s->s_ttyp != NULL) {
976 tty_rel_sess(s->s_ttyp, s);
978 proc_id_clear(PROC_ID_SESSION, s->s_sid);
979 mtx_destroy(&s->s_mtx);
987 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
990 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
991 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
992 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
993 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
996 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1002 for (i = 0; i <= pgrphash; i++) {
1003 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1004 db_printf("indx %d\n", i);
1005 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1007 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1008 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1009 pgrp->pg_session->s_count,
1010 LIST_FIRST(&pgrp->pg_members));
1011 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1012 db_print_pgrp_one(pgrp, p);
1020 * Calculate the kinfo_proc members which contain process-wide
1022 * Must be called with the target process locked.
1025 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1029 PROC_LOCK_ASSERT(p, MA_OWNED);
1033 FOREACH_THREAD_IN_PROC(p, td) {
1035 kp->ki_pctcpu += sched_pctcpu(td);
1036 kp->ki_estcpu += sched_estcpu(td);
1042 * Fill in any information that is common to all threads in the process.
1043 * Must be called with the target process locked.
1046 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1051 struct timeval boottime;
1053 PROC_LOCK_ASSERT(p, MA_OWNED);
1055 kp->ki_structsize = sizeof(*kp);
1057 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1058 kp->ki_args = p->p_args;
1059 kp->ki_textvp = p->p_textvp;
1061 kp->ki_tracep = p->p_tracevp;
1062 kp->ki_traceflag = p->p_traceflag;
1064 kp->ki_fd = p->p_fd;
1065 kp->ki_pd = p->p_pd;
1066 kp->ki_vmspace = p->p_vmspace;
1067 kp->ki_flag = p->p_flag;
1068 kp->ki_flag2 = p->p_flag2;
1071 kp->ki_uid = cred->cr_uid;
1072 kp->ki_ruid = cred->cr_ruid;
1073 kp->ki_svuid = cred->cr_svuid;
1074 kp->ki_cr_flags = 0;
1075 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1076 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1077 /* XXX bde doesn't like KI_NGROUPS */
1078 if (cred->cr_ngroups > KI_NGROUPS) {
1079 kp->ki_ngroups = KI_NGROUPS;
1080 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1082 kp->ki_ngroups = cred->cr_ngroups;
1083 bcopy(cred->cr_groups, kp->ki_groups,
1084 kp->ki_ngroups * sizeof(gid_t));
1085 kp->ki_rgid = cred->cr_rgid;
1086 kp->ki_svgid = cred->cr_svgid;
1087 /* If jailed(cred), emulate the old P_JAILED flag. */
1089 kp->ki_flag |= P_JAILED;
1090 /* If inside the jail, use 0 as a jail ID. */
1091 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1092 kp->ki_jid = cred->cr_prison->pr_id;
1094 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1095 sizeof(kp->ki_loginclass));
1099 mtx_lock(&ps->ps_mtx);
1100 kp->ki_sigignore = ps->ps_sigignore;
1101 kp->ki_sigcatch = ps->ps_sigcatch;
1102 mtx_unlock(&ps->ps_mtx);
1104 if (p->p_state != PRS_NEW &&
1105 p->p_state != PRS_ZOMBIE &&
1106 p->p_vmspace != NULL) {
1107 struct vmspace *vm = p->p_vmspace;
1109 kp->ki_size = vm->vm_map.size;
1110 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1111 FOREACH_THREAD_IN_PROC(p, td0) {
1112 if (!TD_IS_SWAPPED(td0))
1113 kp->ki_rssize += td0->td_kstack_pages;
1115 kp->ki_swrss = vm->vm_swrss;
1116 kp->ki_tsize = vm->vm_tsize;
1117 kp->ki_dsize = vm->vm_dsize;
1118 kp->ki_ssize = vm->vm_ssize;
1119 } else if (p->p_state == PRS_ZOMBIE)
1120 kp->ki_stat = SZOMB;
1121 if (kp->ki_flag & P_INMEM)
1122 kp->ki_sflag = PS_INMEM;
1125 /* Calculate legacy swtime as seconds since 'swtick'. */
1126 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1127 kp->ki_pid = p->p_pid;
1128 kp->ki_nice = p->p_nice;
1129 kp->ki_fibnum = p->p_fibnum;
1130 kp->ki_start = p->p_stats->p_start;
1131 getboottime(&boottime);
1132 timevaladd(&kp->ki_start, &boottime);
1134 rufetch(p, &kp->ki_rusage);
1135 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1136 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1138 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1139 /* Some callers want child times in a single value. */
1140 kp->ki_childtime = kp->ki_childstime;
1141 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1143 FOREACH_THREAD_IN_PROC(p, td0)
1144 kp->ki_cow += td0->td_cow;
1146 if (p->p_comm[0] != '\0')
1147 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1148 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1149 p->p_sysent->sv_name[0] != '\0')
1150 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1151 kp->ki_siglist = p->p_siglist;
1152 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1153 kp->ki_acflag = p->p_acflag;
1154 kp->ki_lock = p->p_lock;
1156 kp->ki_ppid = p->p_oppid;
1157 if (p->p_flag & P_TRACED)
1158 kp->ki_tracer = p->p_pptr->p_pid;
1163 * Fill job-related process information.
1166 fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp)
1172 sx_assert(&proctree_lock, SA_LOCKED);
1173 PROC_LOCK_ASSERT(p, MA_OWNED);
1179 kp->ki_pgid = pgrp->pg_id;
1180 kp->ki_jobc = pgrp_calc_jobc(pgrp);
1182 sp = pgrp->pg_session;
1186 kp->ki_sid = sp->s_sid;
1188 strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login));
1190 kp->ki_kiflag |= KI_CTTY;
1192 kp->ki_kiflag |= KI_SLEADER;
1197 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1198 kp->ki_tdev = tty_udev(tp);
1199 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1200 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1202 kp->ki_tsid = tp->t_session->s_sid;
1204 kp->ki_tdev = NODEV;
1205 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1210 * Fill in information that is thread specific. Must be called with
1211 * target process locked. If 'preferthread' is set, overwrite certain
1212 * process-related fields that are maintained for both threads and
1216 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1222 PROC_LOCK_ASSERT(p, MA_OWNED);
1227 if (td->td_wmesg != NULL)
1228 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1230 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1231 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1232 sizeof(kp->ki_tdname)) {
1233 strlcpy(kp->ki_moretdname,
1234 td->td_name + sizeof(kp->ki_tdname) - 1,
1235 sizeof(kp->ki_moretdname));
1237 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1239 if (TD_ON_LOCK(td)) {
1240 kp->ki_kiflag |= KI_LOCKBLOCK;
1241 strlcpy(kp->ki_lockname, td->td_lockname,
1242 sizeof(kp->ki_lockname));
1244 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1245 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1248 if (p->p_state == PRS_NORMAL) { /* approximate. */
1249 if (TD_ON_RUNQ(td) ||
1251 TD_IS_RUNNING(td)) {
1253 } else if (P_SHOULDSTOP(p)) {
1254 kp->ki_stat = SSTOP;
1255 } else if (TD_IS_SLEEPING(td)) {
1256 kp->ki_stat = SSLEEP;
1257 } else if (TD_ON_LOCK(td)) {
1258 kp->ki_stat = SLOCK;
1260 kp->ki_stat = SWAIT;
1262 } else if (p->p_state == PRS_ZOMBIE) {
1263 kp->ki_stat = SZOMB;
1268 /* Things in the thread */
1269 kp->ki_wchan = td->td_wchan;
1270 kp->ki_pri.pri_level = td->td_priority;
1271 kp->ki_pri.pri_native = td->td_base_pri;
1274 * Note: legacy fields; clamp at the old NOCPU value and/or
1275 * the maximum u_char CPU value.
1277 if (td->td_lastcpu == NOCPU)
1278 kp->ki_lastcpu_old = NOCPU_OLD;
1279 else if (td->td_lastcpu > MAXCPU_OLD)
1280 kp->ki_lastcpu_old = MAXCPU_OLD;
1282 kp->ki_lastcpu_old = td->td_lastcpu;
1284 if (td->td_oncpu == NOCPU)
1285 kp->ki_oncpu_old = NOCPU_OLD;
1286 else if (td->td_oncpu > MAXCPU_OLD)
1287 kp->ki_oncpu_old = MAXCPU_OLD;
1289 kp->ki_oncpu_old = td->td_oncpu;
1291 kp->ki_lastcpu = td->td_lastcpu;
1292 kp->ki_oncpu = td->td_oncpu;
1293 kp->ki_tdflags = td->td_flags;
1294 kp->ki_tid = td->td_tid;
1295 kp->ki_numthreads = p->p_numthreads;
1296 kp->ki_pcb = td->td_pcb;
1297 kp->ki_kstack = (void *)td->td_kstack;
1298 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1299 kp->ki_pri.pri_class = td->td_pri_class;
1300 kp->ki_pri.pri_user = td->td_user_pri;
1303 rufetchtd(td, &kp->ki_rusage);
1304 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1305 kp->ki_pctcpu = sched_pctcpu(td);
1306 kp->ki_estcpu = sched_estcpu(td);
1307 kp->ki_cow = td->td_cow;
1310 /* We can't get this anymore but ps etc never used it anyway. */
1314 kp->ki_siglist = td->td_siglist;
1315 kp->ki_sigmask = td->td_sigmask;
1322 * Fill in a kinfo_proc structure for the specified process.
1323 * Must be called with the target process locked.
1326 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1328 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1330 bzero(kp, sizeof(*kp));
1332 fill_kinfo_proc_pgrp(p,kp);
1333 fill_kinfo_proc_only(p, kp);
1334 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1335 fill_kinfo_aggregate(p, kp);
1342 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1346 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1349 pstats_fork(struct pstats *src, struct pstats *dst)
1352 bzero(&dst->pstat_startzero,
1353 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1354 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1355 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1359 pstats_free(struct pstats *ps)
1362 free(ps, M_SUBPROC);
1365 #ifdef COMPAT_FREEBSD32
1368 * This function is typically used to copy out the kernel address, so
1369 * it can be replaced by assignment of zero.
1371 static inline uint32_t
1372 ptr32_trim(const void *ptr)
1376 uptr = (uintptr_t)ptr;
1377 return ((uptr > UINT_MAX) ? 0 : uptr);
1380 #define PTRTRIM_CP(src,dst,fld) \
1381 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1384 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1388 bzero(ki32, sizeof(struct kinfo_proc32));
1389 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1390 CP(*ki, *ki32, ki_layout);
1391 PTRTRIM_CP(*ki, *ki32, ki_args);
1392 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1393 PTRTRIM_CP(*ki, *ki32, ki_addr);
1394 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1395 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1396 PTRTRIM_CP(*ki, *ki32, ki_fd);
1397 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1398 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1399 CP(*ki, *ki32, ki_pid);
1400 CP(*ki, *ki32, ki_ppid);
1401 CP(*ki, *ki32, ki_pgid);
1402 CP(*ki, *ki32, ki_tpgid);
1403 CP(*ki, *ki32, ki_sid);
1404 CP(*ki, *ki32, ki_tsid);
1405 CP(*ki, *ki32, ki_jobc);
1406 CP(*ki, *ki32, ki_tdev);
1407 CP(*ki, *ki32, ki_tdev_freebsd11);
1408 CP(*ki, *ki32, ki_siglist);
1409 CP(*ki, *ki32, ki_sigmask);
1410 CP(*ki, *ki32, ki_sigignore);
1411 CP(*ki, *ki32, ki_sigcatch);
1412 CP(*ki, *ki32, ki_uid);
1413 CP(*ki, *ki32, ki_ruid);
1414 CP(*ki, *ki32, ki_svuid);
1415 CP(*ki, *ki32, ki_rgid);
1416 CP(*ki, *ki32, ki_svgid);
1417 CP(*ki, *ki32, ki_ngroups);
1418 for (i = 0; i < KI_NGROUPS; i++)
1419 CP(*ki, *ki32, ki_groups[i]);
1420 CP(*ki, *ki32, ki_size);
1421 CP(*ki, *ki32, ki_rssize);
1422 CP(*ki, *ki32, ki_swrss);
1423 CP(*ki, *ki32, ki_tsize);
1424 CP(*ki, *ki32, ki_dsize);
1425 CP(*ki, *ki32, ki_ssize);
1426 CP(*ki, *ki32, ki_xstat);
1427 CP(*ki, *ki32, ki_acflag);
1428 CP(*ki, *ki32, ki_pctcpu);
1429 CP(*ki, *ki32, ki_estcpu);
1430 CP(*ki, *ki32, ki_slptime);
1431 CP(*ki, *ki32, ki_swtime);
1432 CP(*ki, *ki32, ki_cow);
1433 CP(*ki, *ki32, ki_runtime);
1434 TV_CP(*ki, *ki32, ki_start);
1435 TV_CP(*ki, *ki32, ki_childtime);
1436 CP(*ki, *ki32, ki_flag);
1437 CP(*ki, *ki32, ki_kiflag);
1438 CP(*ki, *ki32, ki_traceflag);
1439 CP(*ki, *ki32, ki_stat);
1440 CP(*ki, *ki32, ki_nice);
1441 CP(*ki, *ki32, ki_lock);
1442 CP(*ki, *ki32, ki_rqindex);
1443 CP(*ki, *ki32, ki_oncpu);
1444 CP(*ki, *ki32, ki_lastcpu);
1446 /* XXX TODO: wrap cpu value as appropriate */
1447 CP(*ki, *ki32, ki_oncpu_old);
1448 CP(*ki, *ki32, ki_lastcpu_old);
1450 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1451 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1452 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1453 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1454 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1455 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1456 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1457 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1458 CP(*ki, *ki32, ki_tracer);
1459 CP(*ki, *ki32, ki_flag2);
1460 CP(*ki, *ki32, ki_fibnum);
1461 CP(*ki, *ki32, ki_cr_flags);
1462 CP(*ki, *ki32, ki_jid);
1463 CP(*ki, *ki32, ki_numthreads);
1464 CP(*ki, *ki32, ki_tid);
1465 CP(*ki, *ki32, ki_pri);
1466 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1467 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1468 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1469 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1470 PTRTRIM_CP(*ki, *ki32, ki_udata);
1471 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1472 CP(*ki, *ki32, ki_sflag);
1473 CP(*ki, *ki32, ki_tdflags);
1478 kern_proc_out_size(struct proc *p, int flags)
1482 PROC_LOCK_ASSERT(p, MA_OWNED);
1484 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1485 #ifdef COMPAT_FREEBSD32
1486 if ((flags & KERN_PROC_MASK32) != 0) {
1487 size += sizeof(struct kinfo_proc32);
1490 size += sizeof(struct kinfo_proc);
1492 #ifdef COMPAT_FREEBSD32
1493 if ((flags & KERN_PROC_MASK32) != 0)
1494 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1497 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1504 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1507 struct kinfo_proc ki;
1508 #ifdef COMPAT_FREEBSD32
1509 struct kinfo_proc32 ki32;
1513 PROC_LOCK_ASSERT(p, MA_OWNED);
1514 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1517 fill_kinfo_proc(p, &ki);
1518 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1519 #ifdef COMPAT_FREEBSD32
1520 if ((flags & KERN_PROC_MASK32) != 0) {
1521 freebsd32_kinfo_proc_out(&ki, &ki32);
1522 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1526 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1529 FOREACH_THREAD_IN_PROC(p, td) {
1530 fill_kinfo_thread(td, &ki, 1);
1531 #ifdef COMPAT_FREEBSD32
1532 if ((flags & KERN_PROC_MASK32) != 0) {
1533 freebsd32_kinfo_proc_out(&ki, &ki32);
1534 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1538 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1549 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1552 struct kinfo_proc ki;
1555 if (req->oldptr == NULL)
1556 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1558 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1559 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1560 error = kern_proc_out(p, &sb, flags);
1561 error2 = sbuf_finish(&sb);
1565 else if (error2 != 0)
1571 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1576 for (i = 0; i < pidhashlock + 1; i++) {
1577 sx_slock(&proctree_lock);
1578 sx_slock(&pidhashtbl_lock[i]);
1579 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1580 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1581 if (p->p_state == PRS_NEW)
1583 error = cb(p, cbarg);
1584 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1586 sx_sunlock(&pidhashtbl_lock[i]);
1587 sx_sunlock(&proctree_lock);
1592 sx_sunlock(&pidhashtbl_lock[i]);
1593 sx_sunlock(&proctree_lock);
1598 struct kern_proc_out_args {
1599 struct sysctl_req *req;
1606 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1608 struct kern_proc_out_args *arg = origarg;
1609 int *name = arg->name;
1610 int oid_number = arg->oid_number;
1611 int flags = arg->flags;
1612 struct sysctl_req *req = arg->req;
1617 KASSERT(p->p_ucred != NULL,
1618 ("process credential is NULL for non-NEW proc"));
1620 * Show a user only appropriate processes.
1622 if (p_cansee(curthread, p))
1625 * TODO - make more efficient (see notes below).
1628 switch (oid_number) {
1630 if (p->p_ucred->cr_gid != (gid_t)name[0])
1634 case KERN_PROC_PGRP:
1635 /* could do this by traversing pgrp */
1636 if (p->p_pgrp == NULL ||
1637 p->p_pgrp->pg_id != (pid_t)name[0])
1641 case KERN_PROC_RGID:
1642 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1646 case KERN_PROC_SESSION:
1647 if (p->p_session == NULL ||
1648 p->p_session->s_sid != (pid_t)name[0])
1653 if ((p->p_flag & P_CONTROLT) == 0 ||
1654 p->p_session == NULL)
1656 /* XXX proctree_lock */
1657 SESS_LOCK(p->p_session);
1658 if (p->p_session->s_ttyp == NULL ||
1659 tty_udev(p->p_session->s_ttyp) !=
1661 SESS_UNLOCK(p->p_session);
1664 SESS_UNLOCK(p->p_session);
1668 if (p->p_ucred->cr_uid != (uid_t)name[0])
1672 case KERN_PROC_RUID:
1673 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1677 case KERN_PROC_PROC:
1683 error = sysctl_out_proc(p, req, flags);
1684 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1692 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1694 struct kern_proc_out_args iterarg;
1695 int *name = (int *)arg1;
1696 u_int namelen = arg2;
1698 int flags, oid_number;
1701 oid_number = oidp->oid_number;
1702 if (oid_number != KERN_PROC_ALL &&
1703 (oid_number & KERN_PROC_INC_THREAD) == 0)
1704 flags = KERN_PROC_NOTHREADS;
1707 oid_number &= ~KERN_PROC_INC_THREAD;
1709 #ifdef COMPAT_FREEBSD32
1710 if (req->flags & SCTL_MASK32)
1711 flags |= KERN_PROC_MASK32;
1713 if (oid_number == KERN_PROC_PID) {
1716 error = sysctl_wire_old_buffer(req, 0);
1719 sx_slock(&proctree_lock);
1720 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1722 error = sysctl_out_proc(p, req, flags);
1723 sx_sunlock(&proctree_lock);
1727 switch (oid_number) {
1732 case KERN_PROC_PROC:
1733 if (namelen != 0 && namelen != 1)
1742 if (req->oldptr == NULL) {
1743 /* overestimate by 5 procs */
1744 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1748 error = sysctl_wire_old_buffer(req, 0);
1752 iterarg.flags = flags;
1753 iterarg.oid_number = oid_number;
1755 iterarg.name = name;
1756 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1761 pargs_alloc(int len)
1765 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1767 refcount_init(&pa->ar_ref, 1);
1768 pa->ar_length = len;
1773 pargs_free(struct pargs *pa)
1780 pargs_hold(struct pargs *pa)
1785 refcount_acquire(&pa->ar_ref);
1789 pargs_drop(struct pargs *pa)
1794 if (refcount_release(&pa->ar_ref))
1799 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1805 * This may return a short read if the string is shorter than the chunk
1806 * and is aligned at the end of the page, and the following page is not
1809 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1815 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1817 enum proc_vector_type {
1823 #ifdef COMPAT_FREEBSD32
1825 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1826 size_t *vsizep, enum proc_vector_type type)
1828 struct freebsd32_ps_strings pss;
1830 vm_offset_t vptr, ptr;
1831 uint32_t *proc_vector32;
1837 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1838 sizeof(pss)) != sizeof(pss))
1842 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1843 vsize = pss.ps_nargvstr;
1844 if (vsize > ARG_MAX)
1846 size = vsize * sizeof(int32_t);
1849 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1850 vsize = pss.ps_nenvstr;
1851 if (vsize > ARG_MAX)
1853 size = vsize * sizeof(int32_t);
1856 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1857 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1860 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1861 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1864 if (aux.a_type == AT_NULL)
1868 if (aux.a_type != AT_NULL)
1871 size = vsize * sizeof(aux);
1874 KASSERT(0, ("Wrong proc vector type: %d", type));
1877 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1878 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1882 if (type == PROC_AUX) {
1883 *proc_vectorp = (char **)proc_vector32;
1887 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1888 for (i = 0; i < (int)vsize; i++)
1889 proc_vector[i] = PTRIN(proc_vector32[i]);
1890 *proc_vectorp = proc_vector;
1893 free(proc_vector32, M_TEMP);
1899 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1900 size_t *vsizep, enum proc_vector_type type)
1902 struct ps_strings pss;
1904 vm_offset_t vptr, ptr;
1909 #ifdef COMPAT_FREEBSD32
1910 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1911 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1913 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1914 sizeof(pss)) != sizeof(pss))
1918 vptr = (vm_offset_t)pss.ps_argvstr;
1919 vsize = pss.ps_nargvstr;
1920 if (vsize > ARG_MAX)
1922 size = vsize * sizeof(char *);
1925 vptr = (vm_offset_t)pss.ps_envstr;
1926 vsize = pss.ps_nenvstr;
1927 if (vsize > ARG_MAX)
1929 size = vsize * sizeof(char *);
1933 * The aux array is just above env array on the stack. Check
1934 * that the address is naturally aligned.
1936 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1938 #if __ELF_WORD_SIZE == 64
1939 if (vptr % sizeof(uint64_t) != 0)
1941 if (vptr % sizeof(uint32_t) != 0)
1945 * We count the array size reading the aux vectors from the
1946 * stack until AT_NULL vector is returned. So (to keep the code
1947 * simple) we read the process stack twice: the first time here
1948 * to find the size and the second time when copying the vectors
1949 * to the allocated proc_vector.
1951 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1952 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1955 if (aux.a_type == AT_NULL)
1960 * If the PROC_AUXV_MAX entries are iterated over, and we have
1961 * not reached AT_NULL, it is most likely we are reading wrong
1962 * data: either the process doesn't have auxv array or data has
1963 * been modified. Return the error in this case.
1965 if (aux.a_type != AT_NULL)
1968 size = vsize * sizeof(aux);
1971 KASSERT(0, ("Wrong proc vector type: %d", type));
1972 return (EINVAL); /* In case we are built without INVARIANTS. */
1974 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1975 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1976 free(proc_vector, M_TEMP);
1979 *proc_vectorp = proc_vector;
1985 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1988 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1989 enum proc_vector_type type)
1991 size_t done, len, nchr, vsize;
1993 char **proc_vector, *sptr;
1994 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1996 PROC_ASSERT_HELD(p);
1999 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2001 nchr = 2 * (PATH_MAX + ARG_MAX);
2003 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2006 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2008 * The program may have scribbled into its argv array, e.g. to
2009 * remove some arguments. If that has happened, break out
2010 * before trying to read from NULL.
2012 if (proc_vector[i] == NULL)
2014 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2015 error = proc_read_string(td, p, sptr, pss_string,
2016 sizeof(pss_string));
2019 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2020 if (done + len >= nchr)
2021 len = nchr - done - 1;
2022 sbuf_bcat(sb, pss_string, len);
2023 if (len != GET_PS_STRINGS_CHUNK_SZ)
2025 done += GET_PS_STRINGS_CHUNK_SZ;
2027 sbuf_bcat(sb, "", 1);
2031 free(proc_vector, M_TEMP);
2036 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2039 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2043 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2046 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2050 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2056 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2058 #ifdef COMPAT_FREEBSD32
2059 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2060 size = vsize * sizeof(Elf32_Auxinfo);
2063 size = vsize * sizeof(Elf_Auxinfo);
2064 if (sbuf_bcat(sb, auxv, size) != 0)
2072 * This sysctl allows a process to retrieve the argument list or process
2073 * title for another process without groping around in the address space
2074 * of the other process. It also allow a process to set its own "process
2075 * title to a string of its own choice.
2078 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2080 int *name = (int *)arg1;
2081 u_int namelen = arg2;
2082 struct pargs *newpa, *pa;
2085 int flags, error = 0, error2;
2092 pid = (pid_t)name[0];
2098 * If the query is for this process and it is single-threaded, there
2099 * is nobody to modify pargs, thus we can just read.
2101 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2102 (pa = p->p_args) != NULL)
2103 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2105 flags = PGET_CANSEE;
2106 if (req->newptr != NULL)
2107 flags |= PGET_ISCURRENT;
2108 error = pget(pid, flags, &p);
2116 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2118 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2121 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2122 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2123 error = proc_getargv(curthread, p, &sb);
2124 error2 = sbuf_finish(&sb);
2127 if (error == 0 && error2 != 0)
2132 if (error != 0 || req->newptr == NULL)
2135 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2138 if (req->newlen == 0) {
2140 * Clear the argument pointer, so that we'll fetch arguments
2141 * with proc_getargv() until further notice.
2145 newpa = pargs_alloc(req->newlen);
2146 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2161 * This sysctl allows a process to retrieve environment of another process.
2164 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2166 int *name = (int *)arg1;
2167 u_int namelen = arg2;
2175 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2178 if ((p->p_flag & P_SYSTEM) != 0) {
2183 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2184 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2185 error = proc_getenvv(curthread, p, &sb);
2186 error2 = sbuf_finish(&sb);
2189 return (error != 0 ? error : error2);
2193 * This sysctl allows a process to retrieve ELF auxiliary vector of
2197 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2199 int *name = (int *)arg1;
2200 u_int namelen = arg2;
2208 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2211 if ((p->p_flag & P_SYSTEM) != 0) {
2215 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2216 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2217 error = proc_getauxv(curthread, p, &sb);
2218 error2 = sbuf_finish(&sb);
2221 return (error != 0 ? error : error2);
2225 * This sysctl allows a process to retrieve the path of the executable for
2226 * itself or another process.
2229 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2231 pid_t *pidp = (pid_t *)arg1;
2232 unsigned int arglen = arg2;
2235 char *retbuf, *freebuf;
2240 if (*pidp == -1) { /* -1 means this process */
2241 p = req->td->td_proc;
2243 error = pget(*pidp, PGET_CANSEE, &p);
2257 error = vn_fullpath(vp, &retbuf, &freebuf);
2261 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2262 free(freebuf, M_TEMP);
2267 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2280 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2283 sv_name = p->p_sysent->sv_name;
2285 return (sysctl_handle_string(oidp, sv_name, 0, req));
2288 #ifdef KINFO_OVMENTRY_SIZE
2289 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2292 #ifdef COMPAT_FREEBSD7
2294 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2296 vm_map_entry_t entry, tmp_entry;
2297 unsigned int last_timestamp;
2298 char *fullpath, *freepath;
2299 struct kinfo_ovmentry *kve;
2309 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2312 vm = vmspace_acquire_ref(p);
2317 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2320 vm_map_lock_read(map);
2321 VM_MAP_ENTRY_FOREACH(entry, map) {
2322 vm_object_t obj, tobj, lobj;
2325 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2328 bzero(kve, sizeof(*kve));
2329 kve->kve_structsize = sizeof(*kve);
2331 kve->kve_private_resident = 0;
2332 obj = entry->object.vm_object;
2334 VM_OBJECT_RLOCK(obj);
2335 if (obj->shadow_count == 1)
2336 kve->kve_private_resident =
2337 obj->resident_page_count;
2339 kve->kve_resident = 0;
2340 addr = entry->start;
2341 while (addr < entry->end) {
2342 if (pmap_extract(map->pmap, addr))
2343 kve->kve_resident++;
2347 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2349 VM_OBJECT_RLOCK(tobj);
2350 kve->kve_offset += tobj->backing_object_offset;
2353 VM_OBJECT_RUNLOCK(lobj);
2357 kve->kve_start = (void*)entry->start;
2358 kve->kve_end = (void*)entry->end;
2359 kve->kve_offset += (off_t)entry->offset;
2361 if (entry->protection & VM_PROT_READ)
2362 kve->kve_protection |= KVME_PROT_READ;
2363 if (entry->protection & VM_PROT_WRITE)
2364 kve->kve_protection |= KVME_PROT_WRITE;
2365 if (entry->protection & VM_PROT_EXECUTE)
2366 kve->kve_protection |= KVME_PROT_EXEC;
2368 if (entry->eflags & MAP_ENTRY_COW)
2369 kve->kve_flags |= KVME_FLAG_COW;
2370 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2371 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2372 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2373 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2375 last_timestamp = map->timestamp;
2376 vm_map_unlock_read(map);
2378 kve->kve_fileid = 0;
2383 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2384 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2385 kve->kve_type = KVME_TYPE_UNKNOWN;
2389 VM_OBJECT_RUNLOCK(lobj);
2391 kve->kve_ref_count = obj->ref_count;
2392 kve->kve_shadow_count = obj->shadow_count;
2393 VM_OBJECT_RUNLOCK(obj);
2395 vn_fullpath(vp, &fullpath, &freepath);
2396 cred = curthread->td_ucred;
2397 vn_lock(vp, LK_SHARED | LK_RETRY);
2398 if (VOP_GETATTR(vp, &va, cred) == 0) {
2399 kve->kve_fileid = va.va_fileid;
2401 kve->kve_fsid = va.va_fsid;
2406 kve->kve_type = KVME_TYPE_NONE;
2407 kve->kve_ref_count = 0;
2408 kve->kve_shadow_count = 0;
2411 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2412 if (freepath != NULL)
2413 free(freepath, M_TEMP);
2415 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2416 vm_map_lock_read(map);
2419 if (last_timestamp != map->timestamp) {
2420 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2424 vm_map_unlock_read(map);
2430 #endif /* COMPAT_FREEBSD7 */
2432 #ifdef KINFO_VMENTRY_SIZE
2433 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2437 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2438 int *resident_count, bool *super)
2440 vm_object_t obj, tobj;
2444 vm_pindex_t pi, pi_adv, pindex;
2447 *resident_count = 0;
2448 if (vmmap_skip_res_cnt)
2452 obj = entry->object.vm_object;
2453 addr = entry->start;
2455 pi = OFF_TO_IDX(entry->offset);
2456 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2457 if (m_adv != NULL) {
2460 pi_adv = atop(entry->end - addr);
2462 for (tobj = obj;; tobj = tobj->backing_object) {
2463 m = vm_page_find_least(tobj, pindex);
2465 if (m->pindex == pindex)
2467 if (pi_adv > m->pindex - pindex) {
2468 pi_adv = m->pindex - pindex;
2472 if (tobj->backing_object == NULL)
2474 pindex += OFF_TO_IDX(tobj->
2475 backing_object_offset);
2479 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2480 (addr & (pagesizes[1] - 1)) == 0 &&
2481 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2483 pi_adv = atop(pagesizes[1]);
2486 * We do not test the found page on validity.
2487 * Either the page is busy and being paged in,
2488 * or it was invalidated. The first case
2489 * should be counted as resident, the second
2490 * is not so clear; we do account both.
2494 *resident_count += pi_adv;
2500 * Must be called with the process locked and will return unlocked.
2503 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2505 vm_map_entry_t entry, tmp_entry;
2508 vm_object_t lobj, nobj, obj, tobj;
2509 char *fullpath, *freepath;
2510 struct kinfo_vmentry *kve;
2515 unsigned int last_timestamp;
2519 PROC_LOCK_ASSERT(p, MA_OWNED);
2523 vm = vmspace_acquire_ref(p);
2528 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2532 vm_map_lock_read(map);
2533 VM_MAP_ENTRY_FOREACH(entry, map) {
2534 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2538 bzero(kve, sizeof(*kve));
2539 obj = entry->object.vm_object;
2541 for (tobj = obj; tobj != NULL;
2542 tobj = tobj->backing_object) {
2543 VM_OBJECT_RLOCK(tobj);
2544 kve->kve_offset += tobj->backing_object_offset;
2547 if (obj->backing_object == NULL)
2548 kve->kve_private_resident =
2549 obj->resident_page_count;
2550 kern_proc_vmmap_resident(map, entry,
2551 &kve->kve_resident, &super);
2553 kve->kve_flags |= KVME_FLAG_SUPER;
2554 for (tobj = obj; tobj != NULL; tobj = nobj) {
2555 nobj = tobj->backing_object;
2556 if (tobj != obj && tobj != lobj)
2557 VM_OBJECT_RUNLOCK(tobj);
2563 kve->kve_start = entry->start;
2564 kve->kve_end = entry->end;
2565 kve->kve_offset += entry->offset;
2567 if (entry->protection & VM_PROT_READ)
2568 kve->kve_protection |= KVME_PROT_READ;
2569 if (entry->protection & VM_PROT_WRITE)
2570 kve->kve_protection |= KVME_PROT_WRITE;
2571 if (entry->protection & VM_PROT_EXECUTE)
2572 kve->kve_protection |= KVME_PROT_EXEC;
2574 if (entry->eflags & MAP_ENTRY_COW)
2575 kve->kve_flags |= KVME_FLAG_COW;
2576 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2577 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2578 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2579 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2580 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2581 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2582 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2583 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2584 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2585 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2587 guard = (entry->eflags & MAP_ENTRY_GUARD) != 0;
2589 last_timestamp = map->timestamp;
2590 vm_map_unlock_read(map);
2595 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2599 VM_OBJECT_RUNLOCK(lobj);
2601 kve->kve_ref_count = obj->ref_count;
2602 kve->kve_shadow_count = obj->shadow_count;
2603 VM_OBJECT_RUNLOCK(obj);
2605 vn_fullpath(vp, &fullpath, &freepath);
2606 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2607 cred = curthread->td_ucred;
2608 vn_lock(vp, LK_SHARED | LK_RETRY);
2609 if (VOP_GETATTR(vp, &va, cred) == 0) {
2610 kve->kve_vn_fileid = va.va_fileid;
2611 kve->kve_vn_fsid = va.va_fsid;
2612 kve->kve_vn_fsid_freebsd11 =
2613 kve->kve_vn_fsid; /* truncate */
2615 MAKEIMODE(va.va_type, va.va_mode);
2616 kve->kve_vn_size = va.va_size;
2617 kve->kve_vn_rdev = va.va_rdev;
2618 kve->kve_vn_rdev_freebsd11 =
2619 kve->kve_vn_rdev; /* truncate */
2620 kve->kve_status = KF_ATTR_VALID;
2625 kve->kve_type = guard ? KVME_TYPE_GUARD :
2627 kve->kve_ref_count = 0;
2628 kve->kve_shadow_count = 0;
2631 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2632 if (freepath != NULL)
2633 free(freepath, M_TEMP);
2635 /* Pack record size down */
2636 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2637 kve->kve_structsize =
2638 offsetof(struct kinfo_vmentry, kve_path) +
2639 strlen(kve->kve_path) + 1;
2641 kve->kve_structsize = sizeof(*kve);
2642 kve->kve_structsize = roundup(kve->kve_structsize,
2645 /* Halt filling and truncate rather than exceeding maxlen */
2646 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2648 vm_map_lock_read(map);
2650 } else if (maxlen != -1)
2651 maxlen -= kve->kve_structsize;
2653 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2655 vm_map_lock_read(map);
2658 if (last_timestamp != map->timestamp) {
2659 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2663 vm_map_unlock_read(map);
2671 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2675 int error, error2, *name;
2678 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2679 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2680 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2685 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2686 error2 = sbuf_finish(&sb);
2688 return (error != 0 ? error : error2);
2691 #if defined(STACK) || defined(DDB)
2693 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2695 struct kinfo_kstack *kkstp;
2696 int error, i, *name, numthreads;
2697 lwpid_t *lwpidarray;
2704 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2708 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2709 st = stack_create(M_WAITOK);
2714 if (lwpidarray != NULL) {
2715 free(lwpidarray, M_TEMP);
2718 numthreads = p->p_numthreads;
2720 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2723 } while (numthreads < p->p_numthreads);
2726 * XXXRW: During the below loop, execve(2) and countless other sorts
2727 * of changes could have taken place. Should we check to see if the
2728 * vmspace has been replaced, or the like, in order to prevent
2729 * giving a snapshot that spans, say, execve(2), with some threads
2730 * before and some after? Among other things, the credentials could
2731 * have changed, in which case the right to extract debug info might
2732 * no longer be assured.
2735 FOREACH_THREAD_IN_PROC(p, td) {
2736 KASSERT(i < numthreads,
2737 ("sysctl_kern_proc_kstack: numthreads"));
2738 lwpidarray[i] = td->td_tid;
2743 for (i = 0; i < numthreads; i++) {
2744 td = tdfind(lwpidarray[i], p->p_pid);
2748 bzero(kkstp, sizeof(*kkstp));
2749 (void)sbuf_new(&sb, kkstp->kkst_trace,
2750 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2752 kkstp->kkst_tid = td->td_tid;
2753 if (TD_IS_SWAPPED(td))
2754 kkstp->kkst_state = KKST_STATE_SWAPPED;
2755 else if (stack_save_td(st, td) == 0)
2756 kkstp->kkst_state = KKST_STATE_STACKOK;
2758 kkstp->kkst_state = KKST_STATE_RUNNING;
2761 stack_sbuf_print(&sb, st);
2764 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2769 if (lwpidarray != NULL)
2770 free(lwpidarray, M_TEMP);
2772 free(kkstp, M_TEMP);
2778 * This sysctl allows a process to retrieve the full list of groups from
2779 * itself or another process.
2782 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2784 pid_t *pidp = (pid_t *)arg1;
2785 unsigned int arglen = arg2;
2792 if (*pidp == -1) { /* -1 means this process */
2793 p = req->td->td_proc;
2796 error = pget(*pidp, PGET_CANSEE, &p);
2801 cred = crhold(p->p_ucred);
2804 error = SYSCTL_OUT(req, cred->cr_groups,
2805 cred->cr_ngroups * sizeof(gid_t));
2811 * This sysctl allows a process to retrieve or/and set the resource limit for
2815 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2817 int *name = (int *)arg1;
2818 u_int namelen = arg2;
2827 which = (u_int)name[1];
2828 if (which >= RLIM_NLIMITS)
2831 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2834 flags = PGET_HOLD | PGET_NOTWEXIT;
2835 if (req->newptr != NULL)
2836 flags |= PGET_CANDEBUG;
2838 flags |= PGET_CANSEE;
2839 error = pget((pid_t)name[0], flags, &p);
2846 if (req->oldptr != NULL) {
2848 lim_rlimit_proc(p, which, &rlim);
2851 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2858 if (req->newptr != NULL) {
2859 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2861 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2870 * This sysctl allows a process to retrieve ps_strings structure location of
2874 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2876 int *name = (int *)arg1;
2877 u_int namelen = arg2;
2879 vm_offset_t ps_strings;
2881 #ifdef COMPAT_FREEBSD32
2882 uint32_t ps_strings32;
2888 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2891 #ifdef COMPAT_FREEBSD32
2892 if ((req->flags & SCTL_MASK32) != 0) {
2894 * We return 0 if the 32 bit emulation request is for a 64 bit
2897 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2898 PTROUT(p->p_sysent->sv_psstrings) : 0;
2900 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2904 ps_strings = p->p_sysent->sv_psstrings;
2906 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2911 * This sysctl allows a process to retrieve umask of another process.
2914 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2916 int *name = (int *)arg1;
2917 u_int namelen = arg2;
2926 pid = (pid_t)name[0];
2928 if (pid == p->p_pid || pid == 0) {
2929 cmask = p->p_pd->pd_cmask;
2933 error = pget(pid, PGET_WANTREAD, &p);
2937 cmask = p->p_pd->pd_cmask;
2940 error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
2945 * This sysctl allows a process to set and retrieve binary osreldate of
2949 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2951 int *name = (int *)arg1;
2952 u_int namelen = arg2;
2954 int flags, error, osrel;
2959 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2962 flags = PGET_HOLD | PGET_NOTWEXIT;
2963 if (req->newptr != NULL)
2964 flags |= PGET_CANDEBUG;
2966 flags |= PGET_CANSEE;
2967 error = pget((pid_t)name[0], flags, &p);
2971 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2975 if (req->newptr != NULL) {
2976 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2991 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2993 int *name = (int *)arg1;
2994 u_int namelen = arg2;
2996 struct kinfo_sigtramp kst;
2997 const struct sysentvec *sv;
2999 #ifdef COMPAT_FREEBSD32
3000 struct kinfo_sigtramp32 kst32;
3006 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3010 #ifdef COMPAT_FREEBSD32
3011 if ((req->flags & SCTL_MASK32) != 0) {
3012 bzero(&kst32, sizeof(kst32));
3013 if (SV_PROC_FLAG(p, SV_ILP32)) {
3014 if (sv->sv_sigcode_base != 0) {
3015 kst32.ksigtramp_start = sv->sv_sigcode_base;
3016 kst32.ksigtramp_end = sv->sv_sigcode_base +
3019 kst32.ksigtramp_start = sv->sv_psstrings -
3021 kst32.ksigtramp_end = sv->sv_psstrings;
3025 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3029 bzero(&kst, sizeof(kst));
3030 if (sv->sv_sigcode_base != 0) {
3031 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3032 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3035 kst.ksigtramp_start = (char *)sv->sv_psstrings -
3037 kst.ksigtramp_end = (char *)sv->sv_psstrings;
3040 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3045 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3047 int *name = (int *)arg1;
3048 u_int namelen = arg2;
3053 #ifdef COMPAT_FREEBSD32
3058 if (namelen != 1 || req->newptr != NULL)
3061 pid = (pid_t)name[0];
3062 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3067 #ifdef COMPAT_FREEBSD32
3068 if (SV_CURPROC_FLAG(SV_ILP32)) {
3069 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3075 if (pid <= PID_MAX) {
3076 td1 = FIRST_THREAD_IN_PROC(p);
3078 FOREACH_THREAD_IN_PROC(p, td1) {
3079 if (td1->td_tid == pid)
3088 * The access to the private thread flags. It is fine as far
3089 * as no out-of-thin-air values are read from td_pflags, and
3090 * usermode read of the td_sigblock_ptr is racy inherently,
3091 * since target process might have already changed it
3094 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3095 addr = (uintptr_t)td1->td_sigblock_ptr;
3105 #ifdef COMPAT_FREEBSD32
3106 if (SV_CURPROC_FLAG(SV_ILP32)) {
3108 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3111 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3115 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3118 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3119 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3120 "Return entire process table");
3122 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3123 sysctl_kern_proc, "Process table");
3125 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3126 sysctl_kern_proc, "Process table");
3128 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3129 sysctl_kern_proc, "Process table");
3131 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3132 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3134 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3135 sysctl_kern_proc, "Process table");
3137 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3138 sysctl_kern_proc, "Process table");
3140 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3141 sysctl_kern_proc, "Process table");
3143 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3144 sysctl_kern_proc, "Process table");
3146 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3147 sysctl_kern_proc, "Return process table, no threads");
3149 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3150 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3151 sysctl_kern_proc_args, "Process argument list");
3153 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3154 sysctl_kern_proc_env, "Process environment");
3156 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3157 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3159 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3160 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3162 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3163 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3164 "Process syscall vector name (ABI type)");
3166 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3167 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3169 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3170 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3172 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3173 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3175 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3176 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3178 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3179 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3181 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3182 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3184 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3185 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3187 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3188 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3190 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3191 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3192 "Return process table, including threads");
3194 #ifdef COMPAT_FREEBSD7
3195 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3196 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3199 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3200 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3202 #if defined(STACK) || defined(DDB)
3203 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3204 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3207 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3208 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3210 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3211 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3212 "Process resource limits");
3214 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3215 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3216 "Process ps_strings location");
3218 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3219 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3221 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3222 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3223 "Process binary osreldate");
3225 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3226 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3227 "Process signal trampoline location");
3229 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3230 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3231 "Thread sigfastblock address");
3236 * stop_all_proc() purpose is to stop all process which have usermode,
3237 * except current process for obvious reasons. This makes it somewhat
3238 * unreliable when invoked from multithreaded process. The service
3239 * must not be user-callable anyway.
3244 struct proc *cp, *p;
3246 bool restart, seen_stopped, seen_exiting, stopped_some;
3250 sx_xlock(&allproc_lock);
3252 seen_exiting = seen_stopped = stopped_some = restart = false;
3253 LIST_REMOVE(cp, p_list);
3254 LIST_INSERT_HEAD(&allproc, cp, p_list);
3256 p = LIST_NEXT(cp, p_list);
3259 LIST_REMOVE(cp, p_list);
3260 LIST_INSERT_AFTER(p, cp, p_list);
3262 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3266 if ((p->p_flag & P_WEXIT) != 0) {
3267 seen_exiting = true;
3271 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3273 * Stopped processes are tolerated when there
3274 * are no other processes which might continue
3275 * them. P_STOPPED_SINGLE but not
3276 * P_TOTAL_STOP process still has at least one
3279 seen_stopped = true;
3283 sx_xunlock(&allproc_lock);
3285 r = thread_single(p, SINGLE_ALLPROC);
3289 stopped_some = true;
3292 sx_xlock(&allproc_lock);
3294 /* Catch forked children we did not see in iteration. */
3295 if (gen != allproc_gen)
3297 sx_xunlock(&allproc_lock);
3298 if (restart || stopped_some || seen_exiting || seen_stopped) {
3299 kern_yield(PRI_USER);
3305 resume_all_proc(void)
3307 struct proc *cp, *p;
3310 sx_xlock(&allproc_lock);
3312 LIST_REMOVE(cp, p_list);
3313 LIST_INSERT_HEAD(&allproc, cp, p_list);
3315 p = LIST_NEXT(cp, p_list);
3318 LIST_REMOVE(cp, p_list);
3319 LIST_INSERT_AFTER(p, cp, p_list);
3321 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3322 sx_xunlock(&allproc_lock);
3324 thread_single_end(p, SINGLE_ALLPROC);
3327 sx_xlock(&allproc_lock);
3332 /* Did the loop above missed any stopped process ? */
3333 FOREACH_PROC_IN_SYSTEM(p) {
3334 /* No need for proc lock. */
3335 if ((p->p_flag & P_TOTAL_STOP) != 0)
3338 sx_xunlock(&allproc_lock);
3341 /* #define TOTAL_STOP_DEBUG 1 */
3342 #ifdef TOTAL_STOP_DEBUG
3343 volatile static int ap_resume;
3344 #include <sys/mount.h>
3347 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3353 error = sysctl_handle_int(oidp, &val, 0, req);
3354 if (error != 0 || req->newptr == NULL)
3359 while (ap_resume == 0)
3367 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3368 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3369 sysctl_debug_stop_all_proc, "I",