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 #include <fs/devfs/devfs.h>
93 #ifdef COMPAT_FREEBSD32
94 #include <compat/freebsd32/freebsd32.h>
95 #include <compat/freebsd32/freebsd32_util.h>
98 SDT_PROVIDER_DEFINE(proc);
100 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
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 fixjobc_enterpgrp(struct proc *p, struct pgrp *pgrp);
106 static void doenterpgrp(struct proc *, struct pgrp *);
107 static void orphanpg(struct pgrp *pg);
108 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
109 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
110 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
112 static void pgadjustjobc(struct pgrp *pgrp, bool entering);
113 static void pgdelete(struct pgrp *);
114 static int proc_ctor(void *mem, int size, void *arg, int flags);
115 static void proc_dtor(void *mem, int size, void *arg);
116 static int proc_init(void *mem, int size, int flags);
117 static void proc_fini(void *mem, int size);
118 static void pargs_free(struct pargs *pa);
121 * Other process lists
123 struct pidhashhead *pidhashtbl;
124 struct sx *pidhashtbl_lock;
127 struct pgrphashhead *pgrphashtbl;
129 struct proclist allproc;
130 struct sx __exclusive_cache_line allproc_lock;
131 struct sx __exclusive_cache_line proctree_lock;
132 struct mtx __exclusive_cache_line ppeers_lock;
133 struct mtx __exclusive_cache_line procid_lock;
134 uma_zone_t proc_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_threads = offsetof(struct proc, p_threads);
145 const int thread_off_td_tid = offsetof(struct thread, td_tid);
146 const int thread_off_td_name = offsetof(struct thread, td_name);
147 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
148 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
149 const int thread_off_td_plist = offsetof(struct thread, td_plist);
151 EVENTHANDLER_LIST_DEFINE(process_ctor);
152 EVENTHANDLER_LIST_DEFINE(process_dtor);
153 EVENTHANDLER_LIST_DEFINE(process_init);
154 EVENTHANDLER_LIST_DEFINE(process_fini);
155 EVENTHANDLER_LIST_DEFINE(process_exit);
156 EVENTHANDLER_LIST_DEFINE(process_fork);
157 EVENTHANDLER_LIST_DEFINE(process_exec);
159 int kstack_pages = KSTACK_PAGES;
160 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
161 "Kernel stack size in pages");
162 static int vmmap_skip_res_cnt = 0;
163 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
164 &vmmap_skip_res_cnt, 0,
165 "Skip calculation of the pages resident count in kern.proc.vmmap");
167 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
168 #ifdef COMPAT_FREEBSD32
169 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
173 * Initialize global process hashing structures.
180 sx_init(&allproc_lock, "allproc");
181 sx_init(&proctree_lock, "proctree");
182 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
183 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
185 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
186 pidhashlock = (pidhash + 1) / 64;
189 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
190 M_PROC, M_WAITOK | M_ZERO);
191 for (i = 0; i < pidhashlock + 1; i++)
192 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
193 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
194 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
195 proc_ctor, proc_dtor, proc_init, proc_fini,
196 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
201 * Prepare a proc for use.
204 proc_ctor(void *mem, int size, void *arg, int flags)
209 p = (struct proc *)mem;
210 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
211 td = FIRST_THREAD_IN_PROC(p);
213 /* Make sure all thread constructors are executed */
214 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
220 * Reclaim a proc after use.
223 proc_dtor(void *mem, int size, void *arg)
228 /* INVARIANTS checks go here */
229 p = (struct proc *)mem;
230 td = FIRST_THREAD_IN_PROC(p);
233 KASSERT((p->p_numthreads == 1),
234 ("bad number of threads in exiting process"));
235 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
237 /* Free all OSD associated to this thread. */
239 td_softdep_cleanup(td);
240 MPASS(td->td_su == NULL);
242 /* Make sure all thread destructors are executed */
243 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
245 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
246 if (p->p_ksi != NULL)
247 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
251 * Initialize type-stable parts of a proc (when newly created).
254 proc_init(void *mem, int size, int flags)
258 p = (struct proc *)mem;
259 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
260 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
261 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
262 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
263 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
264 cv_init(&p->p_pwait, "ppwait");
265 TAILQ_INIT(&p->p_threads); /* all threads in proc */
266 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
267 p->p_stats = pstats_alloc();
273 * UMA should ensure that this function is never called.
274 * Freeing a proc structure would violate type stability.
277 proc_fini(void *mem, int size)
282 p = (struct proc *)mem;
283 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
284 pstats_free(p->p_stats);
285 thread_free(FIRST_THREAD_IN_PROC(p));
286 mtx_destroy(&p->p_mtx);
287 if (p->p_ksi != NULL)
288 ksiginfo_free(p->p_ksi);
290 panic("proc reclaimed");
295 * PID space management.
297 * These bitmaps are used by fork_findpid.
299 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
300 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
301 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
302 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
304 static bitstr_t *proc_id_array[] = {
312 proc_id_set(int type, pid_t id)
315 KASSERT(type >= 0 && type < nitems(proc_id_array),
316 ("invalid type %d\n", type));
317 mtx_lock(&procid_lock);
318 KASSERT(bit_test(proc_id_array[type], id) == 0,
319 ("bit %d already set in %d\n", id, type));
320 bit_set(proc_id_array[type], id);
321 mtx_unlock(&procid_lock);
325 proc_id_set_cond(int type, pid_t id)
328 KASSERT(type >= 0 && type < nitems(proc_id_array),
329 ("invalid type %d\n", type));
330 if (bit_test(proc_id_array[type], id))
332 mtx_lock(&procid_lock);
333 bit_set(proc_id_array[type], id);
334 mtx_unlock(&procid_lock);
338 proc_id_clear(int type, pid_t id)
341 KASSERT(type >= 0 && type < nitems(proc_id_array),
342 ("invalid type %d\n", type));
343 mtx_lock(&procid_lock);
344 KASSERT(bit_test(proc_id_array[type], id) != 0,
345 ("bit %d not set in %d\n", id, type));
346 bit_clear(proc_id_array[type], id);
347 mtx_unlock(&procid_lock);
351 * Is p an inferior of the current process?
354 inferior(struct proc *p)
357 sx_assert(&proctree_lock, SX_LOCKED);
358 PROC_LOCK_ASSERT(p, MA_OWNED);
359 for (; p != curproc; p = proc_realparent(p)) {
367 * Shared lock all the pid hash lists.
370 pidhash_slockall(void)
374 for (i = 0; i < pidhashlock + 1; i++)
375 sx_slock(&pidhashtbl_lock[i]);
379 * Shared unlock all the pid hash lists.
382 pidhash_sunlockall(void)
386 for (i = 0; i < pidhashlock + 1; i++)
387 sx_sunlock(&pidhashtbl_lock[i]);
391 * Similar to pfind_any(), this function finds zombies.
394 pfind_any_locked(pid_t pid)
398 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
399 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
400 if (p->p_pid == pid) {
402 if (p->p_state == PRS_NEW) {
413 * Locate a process by number.
415 * By not returning processes in the PRS_NEW state, we allow callers to avoid
416 * testing for that condition to avoid dereferencing p_ucred, et al.
418 static __always_inline struct proc *
419 _pfind(pid_t pid, bool zombie)
424 if (p->p_pid == pid) {
428 sx_slock(PIDHASHLOCK(pid));
429 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
430 if (p->p_pid == pid) {
432 if (p->p_state == PRS_NEW ||
433 (!zombie && p->p_state == PRS_ZOMBIE)) {
440 sx_sunlock(PIDHASHLOCK(pid));
448 return (_pfind(pid, false));
452 * Same as pfind but allow zombies.
458 return (_pfind(pid, true));
462 * Locate a process group by number.
463 * The caller must hold proctree_lock.
470 sx_assert(&proctree_lock, SX_LOCKED);
472 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
473 if (pgrp->pg_id == pgid) {
482 * Locate process and do additional manipulations, depending on flags.
485 pget(pid_t pid, int flags, struct proc **pp)
492 if (p->p_pid == pid) {
496 if (pid <= PID_MAX) {
497 if ((flags & PGET_NOTWEXIT) == 0)
501 } else if ((flags & PGET_NOTID) == 0) {
502 td1 = tdfind(pid, -1);
508 if ((flags & PGET_CANSEE) != 0) {
509 error = p_cansee(curthread, p);
514 if ((flags & PGET_CANDEBUG) != 0) {
515 error = p_candebug(curthread, p);
519 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
523 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
527 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
529 * XXXRW: Not clear ESRCH is the right error during proc
535 if ((flags & PGET_HOLD) != 0) {
547 * Create a new process group.
548 * pgid must be equal to the pid of p.
549 * Begin a new session if required.
552 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
555 sx_assert(&proctree_lock, SX_XLOCKED);
557 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
558 KASSERT(p->p_pid == pgid,
559 ("enterpgrp: new pgrp and pid != pgid"));
560 KASSERT(pgfind(pgid) == NULL,
561 ("enterpgrp: pgrp with pgid exists"));
562 KASSERT(!SESS_LEADER(p),
563 ("enterpgrp: session leader attempted setpgrp"));
565 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
571 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
573 p->p_flag &= ~P_CONTROLT;
577 sess->s_sid = p->p_pid;
578 proc_id_set(PROC_ID_SESSION, p->p_pid);
579 refcount_init(&sess->s_count, 1);
580 sess->s_ttyvp = NULL;
581 sess->s_ttydp = NULL;
583 bcopy(p->p_session->s_login, sess->s_login,
584 sizeof(sess->s_login));
585 pgrp->pg_session = sess;
586 KASSERT(p == curproc,
587 ("enterpgrp: mksession and p != curproc"));
589 pgrp->pg_session = p->p_session;
590 sess_hold(pgrp->pg_session);
594 proc_id_set(PROC_ID_GROUP, p->p_pid);
595 LIST_INIT(&pgrp->pg_members);
598 * As we have an exclusive lock of proctree_lock,
599 * this should not deadlock.
601 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
603 SLIST_INIT(&pgrp->pg_sigiolst);
606 doenterpgrp(p, pgrp);
612 * Move p to an existing process group
615 enterthispgrp(struct proc *p, struct pgrp *pgrp)
618 sx_assert(&proctree_lock, SX_XLOCKED);
619 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
620 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
621 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
622 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
623 KASSERT(pgrp->pg_session == p->p_session,
624 ("%s: pgrp's session %p, p->p_session %p.\n",
628 KASSERT(pgrp != p->p_pgrp,
629 ("%s: p belongs to pgrp.", __func__));
631 doenterpgrp(p, pgrp);
637 * If true, any child of q which belongs to group pgrp, qualifies the
638 * process group pgrp as not orphaned.
641 isjobproc(struct proc *q, struct pgrp *pgrp)
643 sx_assert(&proctree_lock, SX_LOCKED);
644 return (q->p_pgrp != pgrp &&
645 q->p_pgrp->pg_session == pgrp->pg_session);
650 check_pgrp_jobc(struct pgrp *pgrp)
655 sx_assert(&proctree_lock, SX_LOCKED);
656 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
660 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
661 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
664 if (isjobproc(q->p_pptr, pgrp))
667 KASSERT(pgrp->pg_jobc == cnt, ("pgrp %d %p pg_jobc %d cnt %d",
668 pgrp->pg_id, pgrp, pgrp->pg_jobc, cnt));
674 * Move p to a process group
677 doenterpgrp(struct proc *p, struct pgrp *pgrp)
679 struct pgrp *savepgrp;
681 sx_assert(&proctree_lock, SX_XLOCKED);
682 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
683 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
684 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
685 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
687 savepgrp = p->p_pgrp;
690 check_pgrp_jobc(pgrp);
691 check_pgrp_jobc(savepgrp);
695 * Adjust eligibility of affected pgrps to participate in job control.
697 fixjobc_enterpgrp(p, pgrp);
702 LIST_REMOVE(p, p_pglist);
705 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
706 PGRP_UNLOCK(savepgrp);
708 if (LIST_EMPTY(&savepgrp->pg_members))
713 * remove process from process group
716 leavepgrp(struct proc *p)
718 struct pgrp *savepgrp;
720 sx_assert(&proctree_lock, SX_XLOCKED);
721 savepgrp = p->p_pgrp;
724 LIST_REMOVE(p, p_pglist);
727 PGRP_UNLOCK(savepgrp);
728 if (LIST_EMPTY(&savepgrp->pg_members))
734 * delete a process group
737 pgdelete(struct pgrp *pgrp)
739 struct session *savesess;
742 sx_assert(&proctree_lock, SX_XLOCKED);
743 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
744 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
747 * Reset any sigio structures pointing to us as a result of
748 * F_SETOWN with our pgid.
750 funsetownlst(&pgrp->pg_sigiolst);
753 tp = pgrp->pg_session->s_ttyp;
754 LIST_REMOVE(pgrp, pg_hash);
755 savesess = pgrp->pg_session;
758 /* Remove the reference to the pgrp before deallocating it. */
761 tty_rel_pgrp(tp, pgrp);
764 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
765 mtx_destroy(&pgrp->pg_mtx);
767 sess_release(savesess);
771 pgadjustjobc(struct pgrp *pgrp, bool entering)
776 MPASS(pgrp->pg_jobc >= 0);
779 MPASS(pgrp->pg_jobc > 0);
781 if (pgrp->pg_jobc == 0)
788 * Adjust pgrp jobc counters when specified process changes process group.
789 * We count the number of processes in each process group that "qualify"
790 * the group for terminal job control (those with a parent in a different
791 * process group of the same session). If that count reaches zero, the
792 * process group becomes orphaned. Check both the specified process'
793 * process group and that of its children.
794 * We increment eligibility counts before decrementing, otherwise we
795 * could reach 0 spuriously during the decrement.
798 fixjobc_enterpgrp(struct proc *p, struct pgrp *pgrp)
801 struct pgrp *childpgrp;
804 sx_assert(&proctree_lock, SX_LOCKED);
805 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
806 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
807 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
809 if (p->p_pgrp == pgrp)
812 if (isjobproc(p->p_pptr, pgrp))
813 pgadjustjobc(pgrp, true);
814 LIST_FOREACH(q, &p->p_children, p_sibling) {
815 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
817 childpgrp = q->p_pgrp;
818 future_jobc = childpgrp != pgrp &&
819 childpgrp->pg_session == pgrp->pg_session;
820 if (!isjobproc(p, childpgrp) && future_jobc)
821 pgadjustjobc(childpgrp, true);
824 if (isjobproc(p->p_pptr, p->p_pgrp))
825 pgadjustjobc(p->p_pgrp, false);
826 LIST_FOREACH(q, &p->p_children, p_sibling) {
827 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
829 childpgrp = q->p_pgrp;
830 future_jobc = childpgrp != pgrp &&
831 childpgrp->pg_session == pgrp->pg_session;
832 if (isjobproc(p, childpgrp) && !future_jobc)
833 pgadjustjobc(childpgrp, false);
838 fixjobc_kill(struct proc *p)
841 struct pgrp *childpgrp, *pgrp;
843 sx_assert(&proctree_lock, SX_LOCKED);
844 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
846 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
847 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
850 * p no longer affects process group orphanage for children.
851 * It is marked by the flag because p is only physically
852 * removed from its process group on wait(2).
854 p->p_treeflag |= P_TREE_GRPEXITED;
857 * Check p's parent to see whether p qualifies its own process
858 * group; if so, adjust count for p's process group.
860 if (isjobproc(p->p_pptr, pgrp))
861 pgadjustjobc(pgrp, false);
864 * Check this process' children to see whether they qualify
865 * their process groups after reparenting to reaper. If so,
866 * adjust counts for children's process groups.
868 LIST_FOREACH(q, &p->p_children, p_sibling) {
869 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
871 childpgrp = q->p_pgrp;
872 if (isjobproc(q->p_reaper, childpgrp) &&
873 !isjobproc(p, childpgrp))
874 pgadjustjobc(childpgrp, true);
876 LIST_FOREACH(q, &p->p_children, p_sibling) {
877 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
879 childpgrp = q->p_pgrp;
880 if (!isjobproc(q->p_reaper, childpgrp) &&
881 isjobproc(p, childpgrp))
882 pgadjustjobc(childpgrp, false);
895 MPASS(p->p_flag & P_WEXIT);
896 sx_assert(&proctree_lock, SX_LOCKED);
898 if (SESS_LEADER(p)) {
902 * s_ttyp is not zero'd; we use this to indicate that
903 * the session once had a controlling terminal. (for
904 * logging and informational purposes)
915 * Signal foreground pgrp and revoke access to
916 * controlling terminal if it has not been revoked
919 * Because the TTY may have been revoked in the mean
920 * time and could already have a new session associated
921 * with it, make sure we don't send a SIGHUP to a
922 * foreground process group that does not belong to this
928 if (tp->t_session == sp)
929 tty_signal_pgrp(tp, SIGHUP);
934 sx_xunlock(&proctree_lock);
935 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
936 VOP_REVOKE(ttyvp, REVOKEALL);
939 devfs_ctty_unref(ttyvp);
940 sx_xlock(&proctree_lock);
947 * A process group has become orphaned;
948 * if there are any stopped processes in the group,
949 * hang-up all process in that group.
952 orphanpg(struct pgrp *pg)
956 PGRP_LOCK_ASSERT(pg, MA_OWNED);
958 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
960 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
962 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
964 kern_psignal(p, SIGHUP);
965 kern_psignal(p, SIGCONT);
975 sess_hold(struct session *s)
978 refcount_acquire(&s->s_count);
982 sess_release(struct session *s)
985 if (refcount_release(&s->s_count)) {
986 if (s->s_ttyp != NULL) {
988 tty_rel_sess(s->s_ttyp, s);
990 proc_id_clear(PROC_ID_SESSION, s->s_sid);
991 mtx_destroy(&s->s_mtx);
998 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1004 for (i = 0; i <= pgrphash; i++) {
1005 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1006 printf("\tindx %d\n", i);
1007 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1009 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
1010 (void *)pgrp, (long)pgrp->pg_id,
1011 (void *)pgrp->pg_session,
1012 pgrp->pg_session->s_count,
1013 (void *)LIST_FIRST(&pgrp->pg_members));
1014 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1015 printf("\t\tpid %ld addr %p pgrp %p\n",
1016 (long)p->p_pid, (void *)p,
1026 * Calculate the kinfo_proc members which contain process-wide
1028 * Must be called with the target process locked.
1031 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1035 PROC_LOCK_ASSERT(p, MA_OWNED);
1039 FOREACH_THREAD_IN_PROC(p, td) {
1041 kp->ki_pctcpu += sched_pctcpu(td);
1042 kp->ki_estcpu += sched_estcpu(td);
1048 * Clear kinfo_proc and fill in any information that is common
1049 * to all threads in the process.
1050 * Must be called with the target process locked.
1053 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1060 struct timeval boottime;
1062 PROC_LOCK_ASSERT(p, MA_OWNED);
1063 bzero(kp, sizeof(*kp));
1065 kp->ki_structsize = sizeof(*kp);
1067 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1068 kp->ki_args = p->p_args;
1069 kp->ki_textvp = p->p_textvp;
1071 kp->ki_tracep = p->p_tracevp;
1072 kp->ki_traceflag = p->p_traceflag;
1074 kp->ki_fd = p->p_fd;
1075 kp->ki_vmspace = p->p_vmspace;
1076 kp->ki_flag = p->p_flag;
1077 kp->ki_flag2 = p->p_flag2;
1080 kp->ki_uid = cred->cr_uid;
1081 kp->ki_ruid = cred->cr_ruid;
1082 kp->ki_svuid = cred->cr_svuid;
1083 kp->ki_cr_flags = 0;
1084 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1085 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1086 /* XXX bde doesn't like KI_NGROUPS */
1087 if (cred->cr_ngroups > KI_NGROUPS) {
1088 kp->ki_ngroups = KI_NGROUPS;
1089 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1091 kp->ki_ngroups = cred->cr_ngroups;
1092 bcopy(cred->cr_groups, kp->ki_groups,
1093 kp->ki_ngroups * sizeof(gid_t));
1094 kp->ki_rgid = cred->cr_rgid;
1095 kp->ki_svgid = cred->cr_svgid;
1096 /* If jailed(cred), emulate the old P_JAILED flag. */
1098 kp->ki_flag |= P_JAILED;
1099 /* If inside the jail, use 0 as a jail ID. */
1100 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1101 kp->ki_jid = cred->cr_prison->pr_id;
1103 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1104 sizeof(kp->ki_loginclass));
1108 mtx_lock(&ps->ps_mtx);
1109 kp->ki_sigignore = ps->ps_sigignore;
1110 kp->ki_sigcatch = ps->ps_sigcatch;
1111 mtx_unlock(&ps->ps_mtx);
1113 if (p->p_state != PRS_NEW &&
1114 p->p_state != PRS_ZOMBIE &&
1115 p->p_vmspace != NULL) {
1116 struct vmspace *vm = p->p_vmspace;
1118 kp->ki_size = vm->vm_map.size;
1119 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1120 FOREACH_THREAD_IN_PROC(p, td0) {
1121 if (!TD_IS_SWAPPED(td0))
1122 kp->ki_rssize += td0->td_kstack_pages;
1124 kp->ki_swrss = vm->vm_swrss;
1125 kp->ki_tsize = vm->vm_tsize;
1126 kp->ki_dsize = vm->vm_dsize;
1127 kp->ki_ssize = vm->vm_ssize;
1128 } else if (p->p_state == PRS_ZOMBIE)
1129 kp->ki_stat = SZOMB;
1130 if (kp->ki_flag & P_INMEM)
1131 kp->ki_sflag = PS_INMEM;
1134 /* Calculate legacy swtime as seconds since 'swtick'. */
1135 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1136 kp->ki_pid = p->p_pid;
1137 kp->ki_nice = p->p_nice;
1138 kp->ki_fibnum = p->p_fibnum;
1139 kp->ki_start = p->p_stats->p_start;
1140 getboottime(&boottime);
1141 timevaladd(&kp->ki_start, &boottime);
1143 rufetch(p, &kp->ki_rusage);
1144 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1145 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1147 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1148 /* Some callers want child times in a single value. */
1149 kp->ki_childtime = kp->ki_childstime;
1150 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1152 FOREACH_THREAD_IN_PROC(p, td0)
1153 kp->ki_cow += td0->td_cow;
1157 kp->ki_pgid = p->p_pgrp->pg_id;
1158 kp->ki_jobc = p->p_pgrp->pg_jobc;
1159 sp = p->p_pgrp->pg_session;
1162 kp->ki_sid = sp->s_sid;
1164 strlcpy(kp->ki_login, sp->s_login,
1165 sizeof(kp->ki_login));
1167 kp->ki_kiflag |= KI_CTTY;
1169 kp->ki_kiflag |= KI_SLEADER;
1170 /* XXX proctree_lock */
1175 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1176 kp->ki_tdev = tty_udev(tp);
1177 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1178 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1180 kp->ki_tsid = tp->t_session->s_sid;
1182 kp->ki_tdev = NODEV;
1183 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1185 if (p->p_comm[0] != '\0')
1186 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1187 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1188 p->p_sysent->sv_name[0] != '\0')
1189 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1190 kp->ki_siglist = p->p_siglist;
1191 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1192 kp->ki_acflag = p->p_acflag;
1193 kp->ki_lock = p->p_lock;
1195 kp->ki_ppid = p->p_oppid;
1196 if (p->p_flag & P_TRACED)
1197 kp->ki_tracer = p->p_pptr->p_pid;
1202 * Fill in information that is thread specific. Must be called with
1203 * target process locked. If 'preferthread' is set, overwrite certain
1204 * process-related fields that are maintained for both threads and
1208 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1214 PROC_LOCK_ASSERT(p, MA_OWNED);
1219 if (td->td_wmesg != NULL)
1220 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1222 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1223 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1224 sizeof(kp->ki_tdname)) {
1225 strlcpy(kp->ki_moretdname,
1226 td->td_name + sizeof(kp->ki_tdname) - 1,
1227 sizeof(kp->ki_moretdname));
1229 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1231 if (TD_ON_LOCK(td)) {
1232 kp->ki_kiflag |= KI_LOCKBLOCK;
1233 strlcpy(kp->ki_lockname, td->td_lockname,
1234 sizeof(kp->ki_lockname));
1236 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1237 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1240 if (p->p_state == PRS_NORMAL) { /* approximate. */
1241 if (TD_ON_RUNQ(td) ||
1243 TD_IS_RUNNING(td)) {
1245 } else if (P_SHOULDSTOP(p)) {
1246 kp->ki_stat = SSTOP;
1247 } else if (TD_IS_SLEEPING(td)) {
1248 kp->ki_stat = SSLEEP;
1249 } else if (TD_ON_LOCK(td)) {
1250 kp->ki_stat = SLOCK;
1252 kp->ki_stat = SWAIT;
1254 } else if (p->p_state == PRS_ZOMBIE) {
1255 kp->ki_stat = SZOMB;
1260 /* Things in the thread */
1261 kp->ki_wchan = td->td_wchan;
1262 kp->ki_pri.pri_level = td->td_priority;
1263 kp->ki_pri.pri_native = td->td_base_pri;
1266 * Note: legacy fields; clamp at the old NOCPU value and/or
1267 * the maximum u_char CPU value.
1269 if (td->td_lastcpu == NOCPU)
1270 kp->ki_lastcpu_old = NOCPU_OLD;
1271 else if (td->td_lastcpu > MAXCPU_OLD)
1272 kp->ki_lastcpu_old = MAXCPU_OLD;
1274 kp->ki_lastcpu_old = td->td_lastcpu;
1276 if (td->td_oncpu == NOCPU)
1277 kp->ki_oncpu_old = NOCPU_OLD;
1278 else if (td->td_oncpu > MAXCPU_OLD)
1279 kp->ki_oncpu_old = MAXCPU_OLD;
1281 kp->ki_oncpu_old = td->td_oncpu;
1283 kp->ki_lastcpu = td->td_lastcpu;
1284 kp->ki_oncpu = td->td_oncpu;
1285 kp->ki_tdflags = td->td_flags;
1286 kp->ki_tid = td->td_tid;
1287 kp->ki_numthreads = p->p_numthreads;
1288 kp->ki_pcb = td->td_pcb;
1289 kp->ki_kstack = (void *)td->td_kstack;
1290 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1291 kp->ki_pri.pri_class = td->td_pri_class;
1292 kp->ki_pri.pri_user = td->td_user_pri;
1295 rufetchtd(td, &kp->ki_rusage);
1296 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1297 kp->ki_pctcpu = sched_pctcpu(td);
1298 kp->ki_estcpu = sched_estcpu(td);
1299 kp->ki_cow = td->td_cow;
1302 /* We can't get this anymore but ps etc never used it anyway. */
1306 kp->ki_siglist = td->td_siglist;
1307 kp->ki_sigmask = td->td_sigmask;
1314 * Fill in a kinfo_proc structure for the specified process.
1315 * Must be called with the target process locked.
1318 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1321 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1323 fill_kinfo_proc_only(p, kp);
1324 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1325 fill_kinfo_aggregate(p, kp);
1332 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1336 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1339 pstats_fork(struct pstats *src, struct pstats *dst)
1342 bzero(&dst->pstat_startzero,
1343 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1344 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1345 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1349 pstats_free(struct pstats *ps)
1352 free(ps, M_SUBPROC);
1355 #ifdef COMPAT_FREEBSD32
1358 * This function is typically used to copy out the kernel address, so
1359 * it can be replaced by assignment of zero.
1361 static inline uint32_t
1362 ptr32_trim(const void *ptr)
1366 uptr = (uintptr_t)ptr;
1367 return ((uptr > UINT_MAX) ? 0 : uptr);
1370 #define PTRTRIM_CP(src,dst,fld) \
1371 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1374 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1378 bzero(ki32, sizeof(struct kinfo_proc32));
1379 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1380 CP(*ki, *ki32, ki_layout);
1381 PTRTRIM_CP(*ki, *ki32, ki_args);
1382 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1383 PTRTRIM_CP(*ki, *ki32, ki_addr);
1384 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1385 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1386 PTRTRIM_CP(*ki, *ki32, ki_fd);
1387 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1388 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1389 CP(*ki, *ki32, ki_pid);
1390 CP(*ki, *ki32, ki_ppid);
1391 CP(*ki, *ki32, ki_pgid);
1392 CP(*ki, *ki32, ki_tpgid);
1393 CP(*ki, *ki32, ki_sid);
1394 CP(*ki, *ki32, ki_tsid);
1395 CP(*ki, *ki32, ki_jobc);
1396 CP(*ki, *ki32, ki_tdev);
1397 CP(*ki, *ki32, ki_tdev_freebsd11);
1398 CP(*ki, *ki32, ki_siglist);
1399 CP(*ki, *ki32, ki_sigmask);
1400 CP(*ki, *ki32, ki_sigignore);
1401 CP(*ki, *ki32, ki_sigcatch);
1402 CP(*ki, *ki32, ki_uid);
1403 CP(*ki, *ki32, ki_ruid);
1404 CP(*ki, *ki32, ki_svuid);
1405 CP(*ki, *ki32, ki_rgid);
1406 CP(*ki, *ki32, ki_svgid);
1407 CP(*ki, *ki32, ki_ngroups);
1408 for (i = 0; i < KI_NGROUPS; i++)
1409 CP(*ki, *ki32, ki_groups[i]);
1410 CP(*ki, *ki32, ki_size);
1411 CP(*ki, *ki32, ki_rssize);
1412 CP(*ki, *ki32, ki_swrss);
1413 CP(*ki, *ki32, ki_tsize);
1414 CP(*ki, *ki32, ki_dsize);
1415 CP(*ki, *ki32, ki_ssize);
1416 CP(*ki, *ki32, ki_xstat);
1417 CP(*ki, *ki32, ki_acflag);
1418 CP(*ki, *ki32, ki_pctcpu);
1419 CP(*ki, *ki32, ki_estcpu);
1420 CP(*ki, *ki32, ki_slptime);
1421 CP(*ki, *ki32, ki_swtime);
1422 CP(*ki, *ki32, ki_cow);
1423 CP(*ki, *ki32, ki_runtime);
1424 TV_CP(*ki, *ki32, ki_start);
1425 TV_CP(*ki, *ki32, ki_childtime);
1426 CP(*ki, *ki32, ki_flag);
1427 CP(*ki, *ki32, ki_kiflag);
1428 CP(*ki, *ki32, ki_traceflag);
1429 CP(*ki, *ki32, ki_stat);
1430 CP(*ki, *ki32, ki_nice);
1431 CP(*ki, *ki32, ki_lock);
1432 CP(*ki, *ki32, ki_rqindex);
1433 CP(*ki, *ki32, ki_oncpu);
1434 CP(*ki, *ki32, ki_lastcpu);
1436 /* XXX TODO: wrap cpu value as appropriate */
1437 CP(*ki, *ki32, ki_oncpu_old);
1438 CP(*ki, *ki32, ki_lastcpu_old);
1440 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1441 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1442 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1443 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1444 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1445 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1446 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1447 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1448 CP(*ki, *ki32, ki_tracer);
1449 CP(*ki, *ki32, ki_flag2);
1450 CP(*ki, *ki32, ki_fibnum);
1451 CP(*ki, *ki32, ki_cr_flags);
1452 CP(*ki, *ki32, ki_jid);
1453 CP(*ki, *ki32, ki_numthreads);
1454 CP(*ki, *ki32, ki_tid);
1455 CP(*ki, *ki32, ki_pri);
1456 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1457 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1458 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1459 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1460 PTRTRIM_CP(*ki, *ki32, ki_udata);
1461 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1462 CP(*ki, *ki32, ki_sflag);
1463 CP(*ki, *ki32, ki_tdflags);
1468 kern_proc_out_size(struct proc *p, int flags)
1472 PROC_LOCK_ASSERT(p, MA_OWNED);
1474 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1475 #ifdef COMPAT_FREEBSD32
1476 if ((flags & KERN_PROC_MASK32) != 0) {
1477 size += sizeof(struct kinfo_proc32);
1480 size += sizeof(struct kinfo_proc);
1482 #ifdef COMPAT_FREEBSD32
1483 if ((flags & KERN_PROC_MASK32) != 0)
1484 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1487 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1494 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1497 struct kinfo_proc ki;
1498 #ifdef COMPAT_FREEBSD32
1499 struct kinfo_proc32 ki32;
1503 PROC_LOCK_ASSERT(p, MA_OWNED);
1504 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1507 fill_kinfo_proc(p, &ki);
1508 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1509 #ifdef COMPAT_FREEBSD32
1510 if ((flags & KERN_PROC_MASK32) != 0) {
1511 freebsd32_kinfo_proc_out(&ki, &ki32);
1512 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1516 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1519 FOREACH_THREAD_IN_PROC(p, td) {
1520 fill_kinfo_thread(td, &ki, 1);
1521 #ifdef COMPAT_FREEBSD32
1522 if ((flags & KERN_PROC_MASK32) != 0) {
1523 freebsd32_kinfo_proc_out(&ki, &ki32);
1524 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1528 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1539 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1542 struct kinfo_proc ki;
1545 if (req->oldptr == NULL)
1546 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1548 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1549 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1550 error = kern_proc_out(p, &sb, flags);
1551 error2 = sbuf_finish(&sb);
1555 else if (error2 != 0)
1561 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1566 for (i = 0; i < pidhashlock + 1; i++) {
1567 sx_slock(&pidhashtbl_lock[i]);
1568 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1569 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1570 if (p->p_state == PRS_NEW)
1572 error = cb(p, cbarg);
1573 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1575 sx_sunlock(&pidhashtbl_lock[i]);
1580 sx_sunlock(&pidhashtbl_lock[i]);
1585 struct kern_proc_out_args {
1586 struct sysctl_req *req;
1593 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1595 struct kern_proc_out_args *arg = origarg;
1596 int *name = arg->name;
1597 int oid_number = arg->oid_number;
1598 int flags = arg->flags;
1599 struct sysctl_req *req = arg->req;
1604 KASSERT(p->p_ucred != NULL,
1605 ("process credential is NULL for non-NEW proc"));
1607 * Show a user only appropriate processes.
1609 if (p_cansee(curthread, p))
1612 * TODO - make more efficient (see notes below).
1615 switch (oid_number) {
1617 if (p->p_ucred->cr_gid != (gid_t)name[0])
1621 case KERN_PROC_PGRP:
1622 /* could do this by traversing pgrp */
1623 if (p->p_pgrp == NULL ||
1624 p->p_pgrp->pg_id != (pid_t)name[0])
1628 case KERN_PROC_RGID:
1629 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1633 case KERN_PROC_SESSION:
1634 if (p->p_session == NULL ||
1635 p->p_session->s_sid != (pid_t)name[0])
1640 if ((p->p_flag & P_CONTROLT) == 0 ||
1641 p->p_session == NULL)
1643 /* XXX proctree_lock */
1644 SESS_LOCK(p->p_session);
1645 if (p->p_session->s_ttyp == NULL ||
1646 tty_udev(p->p_session->s_ttyp) !=
1648 SESS_UNLOCK(p->p_session);
1651 SESS_UNLOCK(p->p_session);
1655 if (p->p_ucred->cr_uid != (uid_t)name[0])
1659 case KERN_PROC_RUID:
1660 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1664 case KERN_PROC_PROC:
1670 error = sysctl_out_proc(p, req, flags);
1671 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1679 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1681 struct kern_proc_out_args iterarg;
1682 int *name = (int *)arg1;
1683 u_int namelen = arg2;
1685 int flags, oid_number;
1688 oid_number = oidp->oid_number;
1689 if (oid_number != KERN_PROC_ALL &&
1690 (oid_number & KERN_PROC_INC_THREAD) == 0)
1691 flags = KERN_PROC_NOTHREADS;
1694 oid_number &= ~KERN_PROC_INC_THREAD;
1696 #ifdef COMPAT_FREEBSD32
1697 if (req->flags & SCTL_MASK32)
1698 flags |= KERN_PROC_MASK32;
1700 if (oid_number == KERN_PROC_PID) {
1703 error = sysctl_wire_old_buffer(req, 0);
1706 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1708 error = sysctl_out_proc(p, req, flags);
1712 switch (oid_number) {
1717 case KERN_PROC_PROC:
1718 if (namelen != 0 && namelen != 1)
1727 if (req->oldptr == NULL) {
1728 /* overestimate by 5 procs */
1729 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1733 error = sysctl_wire_old_buffer(req, 0);
1737 iterarg.flags = flags;
1738 iterarg.oid_number = oid_number;
1740 iterarg.name = name;
1741 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1746 pargs_alloc(int len)
1750 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1752 refcount_init(&pa->ar_ref, 1);
1753 pa->ar_length = len;
1758 pargs_free(struct pargs *pa)
1765 pargs_hold(struct pargs *pa)
1770 refcount_acquire(&pa->ar_ref);
1774 pargs_drop(struct pargs *pa)
1779 if (refcount_release(&pa->ar_ref))
1784 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1790 * This may return a short read if the string is shorter than the chunk
1791 * and is aligned at the end of the page, and the following page is not
1794 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1800 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1802 enum proc_vector_type {
1808 #ifdef COMPAT_FREEBSD32
1810 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1811 size_t *vsizep, enum proc_vector_type type)
1813 struct freebsd32_ps_strings pss;
1815 vm_offset_t vptr, ptr;
1816 uint32_t *proc_vector32;
1822 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1823 sizeof(pss)) != sizeof(pss))
1827 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1828 vsize = pss.ps_nargvstr;
1829 if (vsize > ARG_MAX)
1831 size = vsize * sizeof(int32_t);
1834 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1835 vsize = pss.ps_nenvstr;
1836 if (vsize > ARG_MAX)
1838 size = vsize * sizeof(int32_t);
1841 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1842 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1845 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1846 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1849 if (aux.a_type == AT_NULL)
1853 if (aux.a_type != AT_NULL)
1856 size = vsize * sizeof(aux);
1859 KASSERT(0, ("Wrong proc vector type: %d", type));
1862 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1863 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1867 if (type == PROC_AUX) {
1868 *proc_vectorp = (char **)proc_vector32;
1872 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1873 for (i = 0; i < (int)vsize; i++)
1874 proc_vector[i] = PTRIN(proc_vector32[i]);
1875 *proc_vectorp = proc_vector;
1878 free(proc_vector32, M_TEMP);
1884 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1885 size_t *vsizep, enum proc_vector_type type)
1887 struct ps_strings pss;
1889 vm_offset_t vptr, ptr;
1894 #ifdef COMPAT_FREEBSD32
1895 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1896 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1898 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1899 sizeof(pss)) != sizeof(pss))
1903 vptr = (vm_offset_t)pss.ps_argvstr;
1904 vsize = pss.ps_nargvstr;
1905 if (vsize > ARG_MAX)
1907 size = vsize * sizeof(char *);
1910 vptr = (vm_offset_t)pss.ps_envstr;
1911 vsize = pss.ps_nenvstr;
1912 if (vsize > ARG_MAX)
1914 size = vsize * sizeof(char *);
1918 * The aux array is just above env array on the stack. Check
1919 * that the address is naturally aligned.
1921 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1923 #if __ELF_WORD_SIZE == 64
1924 if (vptr % sizeof(uint64_t) != 0)
1926 if (vptr % sizeof(uint32_t) != 0)
1930 * We count the array size reading the aux vectors from the
1931 * stack until AT_NULL vector is returned. So (to keep the code
1932 * simple) we read the process stack twice: the first time here
1933 * to find the size and the second time when copying the vectors
1934 * to the allocated proc_vector.
1936 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1937 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1940 if (aux.a_type == AT_NULL)
1945 * If the PROC_AUXV_MAX entries are iterated over, and we have
1946 * not reached AT_NULL, it is most likely we are reading wrong
1947 * data: either the process doesn't have auxv array or data has
1948 * been modified. Return the error in this case.
1950 if (aux.a_type != AT_NULL)
1953 size = vsize * sizeof(aux);
1956 KASSERT(0, ("Wrong proc vector type: %d", type));
1957 return (EINVAL); /* In case we are built without INVARIANTS. */
1959 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1960 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1961 free(proc_vector, M_TEMP);
1964 *proc_vectorp = proc_vector;
1970 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1973 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1974 enum proc_vector_type type)
1976 size_t done, len, nchr, vsize;
1978 char **proc_vector, *sptr;
1979 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1981 PROC_ASSERT_HELD(p);
1984 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1986 nchr = 2 * (PATH_MAX + ARG_MAX);
1988 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1991 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1993 * The program may have scribbled into its argv array, e.g. to
1994 * remove some arguments. If that has happened, break out
1995 * before trying to read from NULL.
1997 if (proc_vector[i] == NULL)
1999 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2000 error = proc_read_string(td, p, sptr, pss_string,
2001 sizeof(pss_string));
2004 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2005 if (done + len >= nchr)
2006 len = nchr - done - 1;
2007 sbuf_bcat(sb, pss_string, len);
2008 if (len != GET_PS_STRINGS_CHUNK_SZ)
2010 done += GET_PS_STRINGS_CHUNK_SZ;
2012 sbuf_bcat(sb, "", 1);
2016 free(proc_vector, M_TEMP);
2021 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2024 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2028 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2031 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2035 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2041 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2043 #ifdef COMPAT_FREEBSD32
2044 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2045 size = vsize * sizeof(Elf32_Auxinfo);
2048 size = vsize * sizeof(Elf_Auxinfo);
2049 if (sbuf_bcat(sb, auxv, size) != 0)
2057 * This sysctl allows a process to retrieve the argument list or process
2058 * title for another process without groping around in the address space
2059 * of the other process. It also allow a process to set its own "process
2060 * title to a string of its own choice.
2063 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2065 int *name = (int *)arg1;
2066 u_int namelen = arg2;
2067 struct pargs *newpa, *pa;
2070 int flags, error = 0, error2;
2076 pid = (pid_t)name[0];
2078 * If the query is for this process and it is single-threaded, there
2079 * is nobody to modify pargs, thus we can just read.
2082 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2083 (pa = p->p_args) != NULL)
2084 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2086 flags = PGET_CANSEE;
2087 if (req->newptr != NULL)
2088 flags |= PGET_ISCURRENT;
2089 error = pget(pid, flags, &p);
2097 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2099 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2102 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2103 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2104 error = proc_getargv(curthread, p, &sb);
2105 error2 = sbuf_finish(&sb);
2108 if (error == 0 && error2 != 0)
2113 if (error != 0 || req->newptr == NULL)
2116 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2119 if (req->newlen == 0) {
2121 * Clear the argument pointer, so that we'll fetch arguments
2122 * with proc_getargv() until further notice.
2126 newpa = pargs_alloc(req->newlen);
2127 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2142 * This sysctl allows a process to retrieve environment of another process.
2145 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2147 int *name = (int *)arg1;
2148 u_int namelen = arg2;
2156 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2159 if ((p->p_flag & P_SYSTEM) != 0) {
2164 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2165 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2166 error = proc_getenvv(curthread, p, &sb);
2167 error2 = sbuf_finish(&sb);
2170 return (error != 0 ? error : error2);
2174 * This sysctl allows a process to retrieve ELF auxiliary vector of
2178 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2180 int *name = (int *)arg1;
2181 u_int namelen = arg2;
2189 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2192 if ((p->p_flag & P_SYSTEM) != 0) {
2196 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2197 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2198 error = proc_getauxv(curthread, p, &sb);
2199 error2 = sbuf_finish(&sb);
2202 return (error != 0 ? error : error2);
2206 * This sysctl allows a process to retrieve the path of the executable for
2207 * itself or another process.
2210 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2212 pid_t *pidp = (pid_t *)arg1;
2213 unsigned int arglen = arg2;
2216 char *retbuf, *freebuf;
2221 if (*pidp == -1) { /* -1 means this process */
2222 p = req->td->td_proc;
2224 error = pget(*pidp, PGET_CANSEE, &p);
2238 error = vn_fullpath(vp, &retbuf, &freebuf);
2242 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2243 free(freebuf, M_TEMP);
2248 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2261 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2264 sv_name = p->p_sysent->sv_name;
2266 return (sysctl_handle_string(oidp, sv_name, 0, req));
2269 #ifdef KINFO_OVMENTRY_SIZE
2270 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2273 #ifdef COMPAT_FREEBSD7
2275 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2277 vm_map_entry_t entry, tmp_entry;
2278 unsigned int last_timestamp;
2279 char *fullpath, *freepath;
2280 struct kinfo_ovmentry *kve;
2290 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2293 vm = vmspace_acquire_ref(p);
2298 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2301 vm_map_lock_read(map);
2302 VM_MAP_ENTRY_FOREACH(entry, map) {
2303 vm_object_t obj, tobj, lobj;
2306 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2309 bzero(kve, sizeof(*kve));
2310 kve->kve_structsize = sizeof(*kve);
2312 kve->kve_private_resident = 0;
2313 obj = entry->object.vm_object;
2315 VM_OBJECT_RLOCK(obj);
2316 if (obj->shadow_count == 1)
2317 kve->kve_private_resident =
2318 obj->resident_page_count;
2320 kve->kve_resident = 0;
2321 addr = entry->start;
2322 while (addr < entry->end) {
2323 if (pmap_extract(map->pmap, addr))
2324 kve->kve_resident++;
2328 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2330 VM_OBJECT_RLOCK(tobj);
2331 kve->kve_offset += tobj->backing_object_offset;
2334 VM_OBJECT_RUNLOCK(lobj);
2338 kve->kve_start = (void*)entry->start;
2339 kve->kve_end = (void*)entry->end;
2340 kve->kve_offset += (off_t)entry->offset;
2342 if (entry->protection & VM_PROT_READ)
2343 kve->kve_protection |= KVME_PROT_READ;
2344 if (entry->protection & VM_PROT_WRITE)
2345 kve->kve_protection |= KVME_PROT_WRITE;
2346 if (entry->protection & VM_PROT_EXECUTE)
2347 kve->kve_protection |= KVME_PROT_EXEC;
2349 if (entry->eflags & MAP_ENTRY_COW)
2350 kve->kve_flags |= KVME_FLAG_COW;
2351 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2352 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2353 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2354 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2356 last_timestamp = map->timestamp;
2357 vm_map_unlock_read(map);
2359 kve->kve_fileid = 0;
2364 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2365 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2366 kve->kve_type = KVME_TYPE_UNKNOWN;
2370 VM_OBJECT_RUNLOCK(lobj);
2372 kve->kve_ref_count = obj->ref_count;
2373 kve->kve_shadow_count = obj->shadow_count;
2374 VM_OBJECT_RUNLOCK(obj);
2376 vn_fullpath(vp, &fullpath, &freepath);
2377 cred = curthread->td_ucred;
2378 vn_lock(vp, LK_SHARED | LK_RETRY);
2379 if (VOP_GETATTR(vp, &va, cred) == 0) {
2380 kve->kve_fileid = va.va_fileid;
2382 kve->kve_fsid = va.va_fsid;
2387 kve->kve_type = KVME_TYPE_NONE;
2388 kve->kve_ref_count = 0;
2389 kve->kve_shadow_count = 0;
2392 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2393 if (freepath != NULL)
2394 free(freepath, M_TEMP);
2396 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2397 vm_map_lock_read(map);
2400 if (last_timestamp != map->timestamp) {
2401 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2405 vm_map_unlock_read(map);
2411 #endif /* COMPAT_FREEBSD7 */
2413 #ifdef KINFO_VMENTRY_SIZE
2414 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2418 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2419 int *resident_count, bool *super)
2421 vm_object_t obj, tobj;
2425 vm_pindex_t pi, pi_adv, pindex;
2428 *resident_count = 0;
2429 if (vmmap_skip_res_cnt)
2433 obj = entry->object.vm_object;
2434 addr = entry->start;
2436 pi = OFF_TO_IDX(entry->offset);
2437 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2438 if (m_adv != NULL) {
2441 pi_adv = atop(entry->end - addr);
2443 for (tobj = obj;; tobj = tobj->backing_object) {
2444 m = vm_page_find_least(tobj, pindex);
2446 if (m->pindex == pindex)
2448 if (pi_adv > m->pindex - pindex) {
2449 pi_adv = m->pindex - pindex;
2453 if (tobj->backing_object == NULL)
2455 pindex += OFF_TO_IDX(tobj->
2456 backing_object_offset);
2460 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2461 (addr & (pagesizes[1] - 1)) == 0 &&
2462 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2464 pi_adv = atop(pagesizes[1]);
2467 * We do not test the found page on validity.
2468 * Either the page is busy and being paged in,
2469 * or it was invalidated. The first case
2470 * should be counted as resident, the second
2471 * is not so clear; we do account both.
2475 *resident_count += pi_adv;
2481 * Must be called with the process locked and will return unlocked.
2484 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2486 vm_map_entry_t entry, tmp_entry;
2489 vm_object_t obj, tobj, lobj;
2490 char *fullpath, *freepath;
2491 struct kinfo_vmentry *kve;
2496 unsigned int last_timestamp;
2500 PROC_LOCK_ASSERT(p, MA_OWNED);
2504 vm = vmspace_acquire_ref(p);
2509 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2513 vm_map_lock_read(map);
2514 VM_MAP_ENTRY_FOREACH(entry, map) {
2515 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2519 bzero(kve, sizeof(*kve));
2520 obj = entry->object.vm_object;
2522 for (tobj = obj; tobj != NULL;
2523 tobj = tobj->backing_object) {
2524 VM_OBJECT_RLOCK(tobj);
2525 kve->kve_offset += tobj->backing_object_offset;
2528 if (obj->backing_object == NULL)
2529 kve->kve_private_resident =
2530 obj->resident_page_count;
2531 kern_proc_vmmap_resident(map, entry,
2532 &kve->kve_resident, &super);
2534 kve->kve_flags |= KVME_FLAG_SUPER;
2535 for (tobj = obj; tobj != NULL;
2536 tobj = tobj->backing_object) {
2537 if (tobj != obj && tobj != lobj)
2538 VM_OBJECT_RUNLOCK(tobj);
2544 kve->kve_start = entry->start;
2545 kve->kve_end = entry->end;
2546 kve->kve_offset += entry->offset;
2548 if (entry->protection & VM_PROT_READ)
2549 kve->kve_protection |= KVME_PROT_READ;
2550 if (entry->protection & VM_PROT_WRITE)
2551 kve->kve_protection |= KVME_PROT_WRITE;
2552 if (entry->protection & VM_PROT_EXECUTE)
2553 kve->kve_protection |= KVME_PROT_EXEC;
2555 if (entry->eflags & MAP_ENTRY_COW)
2556 kve->kve_flags |= KVME_FLAG_COW;
2557 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2558 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2559 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2560 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2561 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2562 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2563 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2564 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2565 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2566 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2568 last_timestamp = map->timestamp;
2569 vm_map_unlock_read(map);
2574 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2578 VM_OBJECT_RUNLOCK(lobj);
2580 kve->kve_ref_count = obj->ref_count;
2581 kve->kve_shadow_count = obj->shadow_count;
2582 VM_OBJECT_RUNLOCK(obj);
2584 vn_fullpath(vp, &fullpath, &freepath);
2585 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2586 cred = curthread->td_ucred;
2587 vn_lock(vp, LK_SHARED | LK_RETRY);
2588 if (VOP_GETATTR(vp, &va, cred) == 0) {
2589 kve->kve_vn_fileid = va.va_fileid;
2590 kve->kve_vn_fsid = va.va_fsid;
2591 kve->kve_vn_fsid_freebsd11 =
2592 kve->kve_vn_fsid; /* truncate */
2594 MAKEIMODE(va.va_type, va.va_mode);
2595 kve->kve_vn_size = va.va_size;
2596 kve->kve_vn_rdev = va.va_rdev;
2597 kve->kve_vn_rdev_freebsd11 =
2598 kve->kve_vn_rdev; /* truncate */
2599 kve->kve_status = KF_ATTR_VALID;
2604 kve->kve_type = KVME_TYPE_NONE;
2605 kve->kve_ref_count = 0;
2606 kve->kve_shadow_count = 0;
2609 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2610 if (freepath != NULL)
2611 free(freepath, M_TEMP);
2613 /* Pack record size down */
2614 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2615 kve->kve_structsize =
2616 offsetof(struct kinfo_vmentry, kve_path) +
2617 strlen(kve->kve_path) + 1;
2619 kve->kve_structsize = sizeof(*kve);
2620 kve->kve_structsize = roundup(kve->kve_structsize,
2623 /* Halt filling and truncate rather than exceeding maxlen */
2624 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2626 vm_map_lock_read(map);
2628 } else if (maxlen != -1)
2629 maxlen -= kve->kve_structsize;
2631 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2633 vm_map_lock_read(map);
2636 if (last_timestamp != map->timestamp) {
2637 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2641 vm_map_unlock_read(map);
2649 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2653 int error, error2, *name;
2656 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2657 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2658 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2663 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2664 error2 = sbuf_finish(&sb);
2666 return (error != 0 ? error : error2);
2669 #if defined(STACK) || defined(DDB)
2671 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2673 struct kinfo_kstack *kkstp;
2674 int error, i, *name, numthreads;
2675 lwpid_t *lwpidarray;
2682 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2686 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2687 st = stack_create(M_WAITOK);
2692 if (lwpidarray != NULL) {
2693 free(lwpidarray, M_TEMP);
2696 numthreads = p->p_numthreads;
2698 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2701 } while (numthreads < p->p_numthreads);
2704 * XXXRW: During the below loop, execve(2) and countless other sorts
2705 * of changes could have taken place. Should we check to see if the
2706 * vmspace has been replaced, or the like, in order to prevent
2707 * giving a snapshot that spans, say, execve(2), with some threads
2708 * before and some after? Among other things, the credentials could
2709 * have changed, in which case the right to extract debug info might
2710 * no longer be assured.
2713 FOREACH_THREAD_IN_PROC(p, td) {
2714 KASSERT(i < numthreads,
2715 ("sysctl_kern_proc_kstack: numthreads"));
2716 lwpidarray[i] = td->td_tid;
2720 for (i = 0; i < numthreads; i++) {
2721 td = thread_find(p, lwpidarray[i]);
2725 bzero(kkstp, sizeof(*kkstp));
2726 (void)sbuf_new(&sb, kkstp->kkst_trace,
2727 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2729 kkstp->kkst_tid = td->td_tid;
2730 if (TD_IS_SWAPPED(td))
2731 kkstp->kkst_state = KKST_STATE_SWAPPED;
2732 else if (stack_save_td(st, td) == 0)
2733 kkstp->kkst_state = KKST_STATE_STACKOK;
2735 kkstp->kkst_state = KKST_STATE_RUNNING;
2738 stack_sbuf_print(&sb, st);
2741 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2748 if (lwpidarray != NULL)
2749 free(lwpidarray, M_TEMP);
2751 free(kkstp, M_TEMP);
2757 * This sysctl allows a process to retrieve the full list of groups from
2758 * itself or another process.
2761 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2763 pid_t *pidp = (pid_t *)arg1;
2764 unsigned int arglen = arg2;
2771 if (*pidp == -1) { /* -1 means this process */
2772 p = req->td->td_proc;
2775 error = pget(*pidp, PGET_CANSEE, &p);
2780 cred = crhold(p->p_ucred);
2783 error = SYSCTL_OUT(req, cred->cr_groups,
2784 cred->cr_ngroups * sizeof(gid_t));
2790 * This sysctl allows a process to retrieve or/and set the resource limit for
2794 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2796 int *name = (int *)arg1;
2797 u_int namelen = arg2;
2806 which = (u_int)name[1];
2807 if (which >= RLIM_NLIMITS)
2810 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2813 flags = PGET_HOLD | PGET_NOTWEXIT;
2814 if (req->newptr != NULL)
2815 flags |= PGET_CANDEBUG;
2817 flags |= PGET_CANSEE;
2818 error = pget((pid_t)name[0], flags, &p);
2825 if (req->oldptr != NULL) {
2827 lim_rlimit_proc(p, which, &rlim);
2830 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2837 if (req->newptr != NULL) {
2838 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2840 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2849 * This sysctl allows a process to retrieve ps_strings structure location of
2853 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2855 int *name = (int *)arg1;
2856 u_int namelen = arg2;
2858 vm_offset_t ps_strings;
2860 #ifdef COMPAT_FREEBSD32
2861 uint32_t ps_strings32;
2867 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2870 #ifdef COMPAT_FREEBSD32
2871 if ((req->flags & SCTL_MASK32) != 0) {
2873 * We return 0 if the 32 bit emulation request is for a 64 bit
2876 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2877 PTROUT(p->p_sysent->sv_psstrings) : 0;
2879 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2883 ps_strings = p->p_sysent->sv_psstrings;
2885 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2890 * This sysctl allows a process to retrieve umask of another process.
2893 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2895 int *name = (int *)arg1;
2896 u_int namelen = arg2;
2905 pid = (pid_t)name[0];
2907 if (pid == p->p_pid || pid == 0) {
2908 fd_cmask = p->p_fd->fd_cmask;
2912 error = pget(pid, PGET_WANTREAD, &p);
2916 fd_cmask = p->p_fd->fd_cmask;
2919 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2924 * This sysctl allows a process to set and retrieve binary osreldate of
2928 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2930 int *name = (int *)arg1;
2931 u_int namelen = arg2;
2933 int flags, error, osrel;
2938 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2941 flags = PGET_HOLD | PGET_NOTWEXIT;
2942 if (req->newptr != NULL)
2943 flags |= PGET_CANDEBUG;
2945 flags |= PGET_CANSEE;
2946 error = pget((pid_t)name[0], flags, &p);
2950 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2954 if (req->newptr != NULL) {
2955 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2970 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2972 int *name = (int *)arg1;
2973 u_int namelen = arg2;
2975 struct kinfo_sigtramp kst;
2976 const struct sysentvec *sv;
2978 #ifdef COMPAT_FREEBSD32
2979 struct kinfo_sigtramp32 kst32;
2985 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2989 #ifdef COMPAT_FREEBSD32
2990 if ((req->flags & SCTL_MASK32) != 0) {
2991 bzero(&kst32, sizeof(kst32));
2992 if (SV_PROC_FLAG(p, SV_ILP32)) {
2993 if (sv->sv_sigcode_base != 0) {
2994 kst32.ksigtramp_start = sv->sv_sigcode_base;
2995 kst32.ksigtramp_end = sv->sv_sigcode_base +
2998 kst32.ksigtramp_start = sv->sv_psstrings -
3000 kst32.ksigtramp_end = sv->sv_psstrings;
3004 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3008 bzero(&kst, sizeof(kst));
3009 if (sv->sv_sigcode_base != 0) {
3010 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3011 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3014 kst.ksigtramp_start = (char *)sv->sv_psstrings -
3016 kst.ksigtramp_end = (char *)sv->sv_psstrings;
3019 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3024 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3026 int *name = (int *)arg1;
3027 u_int namelen = arg2;
3032 #ifdef COMPAT_FREEBSD32
3037 if (namelen != 1 || req->newptr != NULL)
3040 pid = (pid_t)name[0];
3041 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3046 #ifdef COMPAT_FREEBSD32
3047 if (SV_CURPROC_FLAG(SV_ILP32)) {
3048 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3054 if (pid <= PID_MAX) {
3055 td1 = FIRST_THREAD_IN_PROC(p);
3057 FOREACH_THREAD_IN_PROC(p, td1) {
3058 if (td1->td_tid == pid)
3067 * The access to the private thread flags. It is fine as far
3068 * as no out-of-thin-air values are read from td_pflags, and
3069 * usermode read of the td_sigblock_ptr is racy inherently,
3070 * since target process might have already changed it
3073 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3074 addr = (uintptr_t)td1->td_sigblock_ptr;
3084 #ifdef COMPAT_FREEBSD32
3085 if (SV_CURPROC_FLAG(SV_ILP32)) {
3087 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3090 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3094 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3097 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3098 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3099 "Return entire process table");
3101 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3102 sysctl_kern_proc, "Process table");
3104 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3105 sysctl_kern_proc, "Process table");
3107 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3108 sysctl_kern_proc, "Process table");
3110 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3111 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3113 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3114 sysctl_kern_proc, "Process table");
3116 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3117 sysctl_kern_proc, "Process table");
3119 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3120 sysctl_kern_proc, "Process table");
3122 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3123 sysctl_kern_proc, "Process table");
3125 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3126 sysctl_kern_proc, "Return process table, no threads");
3128 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3129 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3130 sysctl_kern_proc_args, "Process argument list");
3132 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3133 sysctl_kern_proc_env, "Process environment");
3135 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3136 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3138 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3139 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3141 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3142 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3143 "Process syscall vector name (ABI type)");
3145 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3146 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3148 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3149 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3151 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3152 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3154 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3155 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3157 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3158 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3160 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3161 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3163 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3164 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3166 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3167 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3169 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3170 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3171 "Return process table, no threads");
3173 #ifdef COMPAT_FREEBSD7
3174 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3175 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3178 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3179 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3181 #if defined(STACK) || defined(DDB)
3182 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3183 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3186 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3187 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3189 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3190 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3191 "Process resource limits");
3193 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3194 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3195 "Process ps_strings location");
3197 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3198 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3200 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3201 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3202 "Process binary osreldate");
3204 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3205 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3206 "Process signal trampoline location");
3208 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3209 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3210 "Thread sigfastblock address");
3215 * stop_all_proc() purpose is to stop all process which have usermode,
3216 * except current process for obvious reasons. This makes it somewhat
3217 * unreliable when invoked from multithreaded process. The service
3218 * must not be user-callable anyway.
3223 struct proc *cp, *p;
3225 bool restart, seen_stopped, seen_exiting, stopped_some;
3229 sx_xlock(&allproc_lock);
3231 seen_exiting = seen_stopped = stopped_some = restart = false;
3232 LIST_REMOVE(cp, p_list);
3233 LIST_INSERT_HEAD(&allproc, cp, p_list);
3235 p = LIST_NEXT(cp, p_list);
3238 LIST_REMOVE(cp, p_list);
3239 LIST_INSERT_AFTER(p, cp, p_list);
3241 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3245 if ((p->p_flag & P_WEXIT) != 0) {
3246 seen_exiting = true;
3250 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3252 * Stopped processes are tolerated when there
3253 * are no other processes which might continue
3254 * them. P_STOPPED_SINGLE but not
3255 * P_TOTAL_STOP process still has at least one
3258 seen_stopped = true;
3262 sx_xunlock(&allproc_lock);
3264 r = thread_single(p, SINGLE_ALLPROC);
3268 stopped_some = true;
3271 sx_xlock(&allproc_lock);
3273 /* Catch forked children we did not see in iteration. */
3274 if (gen != allproc_gen)
3276 sx_xunlock(&allproc_lock);
3277 if (restart || stopped_some || seen_exiting || seen_stopped) {
3278 kern_yield(PRI_USER);
3284 resume_all_proc(void)
3286 struct proc *cp, *p;
3289 sx_xlock(&allproc_lock);
3291 LIST_REMOVE(cp, p_list);
3292 LIST_INSERT_HEAD(&allproc, cp, p_list);
3294 p = LIST_NEXT(cp, p_list);
3297 LIST_REMOVE(cp, p_list);
3298 LIST_INSERT_AFTER(p, cp, p_list);
3300 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3301 sx_xunlock(&allproc_lock);
3303 thread_single_end(p, SINGLE_ALLPROC);
3306 sx_xlock(&allproc_lock);
3311 /* Did the loop above missed any stopped process ? */
3312 FOREACH_PROC_IN_SYSTEM(p) {
3313 /* No need for proc lock. */
3314 if ((p->p_flag & P_TOTAL_STOP) != 0)
3317 sx_xunlock(&allproc_lock);
3320 /* #define TOTAL_STOP_DEBUG 1 */
3321 #ifdef TOTAL_STOP_DEBUG
3322 volatile static int ap_resume;
3323 #include <sys/mount.h>
3326 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3332 error = sysctl_handle_int(oidp, &val, 0, req);
3333 if (error != 0 || req->newptr == NULL)
3338 while (ap_resume == 0)
3346 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3347 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3348 sysctl_debug_stop_all_proc, "I",