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) {
1618 if (p->p_ucred->cr_gid != (gid_t)name[0])
1622 case KERN_PROC_PGRP:
1623 /* could do this by traversing pgrp */
1624 if (p->p_pgrp == NULL ||
1625 p->p_pgrp->pg_id != (pid_t)name[0])
1629 case KERN_PROC_RGID:
1630 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1634 case KERN_PROC_SESSION:
1635 if (p->p_session == NULL ||
1636 p->p_session->s_sid != (pid_t)name[0])
1641 if ((p->p_flag & P_CONTROLT) == 0 ||
1642 p->p_session == NULL)
1644 /* XXX proctree_lock */
1645 SESS_LOCK(p->p_session);
1646 if (p->p_session->s_ttyp == NULL ||
1647 tty_udev(p->p_session->s_ttyp) !=
1649 SESS_UNLOCK(p->p_session);
1652 SESS_UNLOCK(p->p_session);
1656 if (p->p_ucred->cr_uid != (uid_t)name[0])
1660 case KERN_PROC_RUID:
1661 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1665 case KERN_PROC_PROC:
1672 error = sysctl_out_proc(p, req, flags);
1673 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1681 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1683 struct kern_proc_out_args iterarg;
1684 int *name = (int *)arg1;
1685 u_int namelen = arg2;
1687 int flags, oid_number;
1690 oid_number = oidp->oid_number;
1691 if (oid_number != KERN_PROC_ALL &&
1692 (oid_number & KERN_PROC_INC_THREAD) == 0)
1693 flags = KERN_PROC_NOTHREADS;
1696 oid_number &= ~KERN_PROC_INC_THREAD;
1698 #ifdef COMPAT_FREEBSD32
1699 if (req->flags & SCTL_MASK32)
1700 flags |= KERN_PROC_MASK32;
1702 if (oid_number == KERN_PROC_PID) {
1705 error = sysctl_wire_old_buffer(req, 0);
1708 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1710 error = sysctl_out_proc(p, req, flags);
1714 switch (oid_number) {
1719 case KERN_PROC_PROC:
1720 if (namelen != 0 && namelen != 1)
1729 if (req->oldptr == NULL) {
1730 /* overestimate by 5 procs */
1731 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1735 error = sysctl_wire_old_buffer(req, 0);
1739 iterarg.flags = flags;
1740 iterarg.oid_number = oid_number;
1742 iterarg.name = name;
1743 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1748 pargs_alloc(int len)
1752 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1754 refcount_init(&pa->ar_ref, 1);
1755 pa->ar_length = len;
1760 pargs_free(struct pargs *pa)
1767 pargs_hold(struct pargs *pa)
1772 refcount_acquire(&pa->ar_ref);
1776 pargs_drop(struct pargs *pa)
1781 if (refcount_release(&pa->ar_ref))
1786 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1792 * This may return a short read if the string is shorter than the chunk
1793 * and is aligned at the end of the page, and the following page is not
1796 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1802 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1804 enum proc_vector_type {
1810 #ifdef COMPAT_FREEBSD32
1812 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1813 size_t *vsizep, enum proc_vector_type type)
1815 struct freebsd32_ps_strings pss;
1817 vm_offset_t vptr, ptr;
1818 uint32_t *proc_vector32;
1824 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1825 sizeof(pss)) != sizeof(pss))
1829 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1830 vsize = pss.ps_nargvstr;
1831 if (vsize > ARG_MAX)
1833 size = vsize * sizeof(int32_t);
1836 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1837 vsize = pss.ps_nenvstr;
1838 if (vsize > ARG_MAX)
1840 size = vsize * sizeof(int32_t);
1843 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1844 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1847 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1848 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1851 if (aux.a_type == AT_NULL)
1855 if (aux.a_type != AT_NULL)
1858 size = vsize * sizeof(aux);
1861 KASSERT(0, ("Wrong proc vector type: %d", type));
1864 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1865 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1869 if (type == PROC_AUX) {
1870 *proc_vectorp = (char **)proc_vector32;
1874 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1875 for (i = 0; i < (int)vsize; i++)
1876 proc_vector[i] = PTRIN(proc_vector32[i]);
1877 *proc_vectorp = proc_vector;
1880 free(proc_vector32, M_TEMP);
1886 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1887 size_t *vsizep, enum proc_vector_type type)
1889 struct ps_strings pss;
1891 vm_offset_t vptr, ptr;
1896 #ifdef COMPAT_FREEBSD32
1897 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1898 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1900 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1901 sizeof(pss)) != sizeof(pss))
1905 vptr = (vm_offset_t)pss.ps_argvstr;
1906 vsize = pss.ps_nargvstr;
1907 if (vsize > ARG_MAX)
1909 size = vsize * sizeof(char *);
1912 vptr = (vm_offset_t)pss.ps_envstr;
1913 vsize = pss.ps_nenvstr;
1914 if (vsize > ARG_MAX)
1916 size = vsize * sizeof(char *);
1920 * The aux array is just above env array on the stack. Check
1921 * that the address is naturally aligned.
1923 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1925 #if __ELF_WORD_SIZE == 64
1926 if (vptr % sizeof(uint64_t) != 0)
1928 if (vptr % sizeof(uint32_t) != 0)
1932 * We count the array size reading the aux vectors from the
1933 * stack until AT_NULL vector is returned. So (to keep the code
1934 * simple) we read the process stack twice: the first time here
1935 * to find the size and the second time when copying the vectors
1936 * to the allocated proc_vector.
1938 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1939 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1942 if (aux.a_type == AT_NULL)
1947 * If the PROC_AUXV_MAX entries are iterated over, and we have
1948 * not reached AT_NULL, it is most likely we are reading wrong
1949 * data: either the process doesn't have auxv array or data has
1950 * been modified. Return the error in this case.
1952 if (aux.a_type != AT_NULL)
1955 size = vsize * sizeof(aux);
1958 KASSERT(0, ("Wrong proc vector type: %d", type));
1959 return (EINVAL); /* In case we are built without INVARIANTS. */
1961 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1962 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1963 free(proc_vector, M_TEMP);
1966 *proc_vectorp = proc_vector;
1972 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1975 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1976 enum proc_vector_type type)
1978 size_t done, len, nchr, vsize;
1980 char **proc_vector, *sptr;
1981 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1983 PROC_ASSERT_HELD(p);
1986 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1988 nchr = 2 * (PATH_MAX + ARG_MAX);
1990 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1993 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1995 * The program may have scribbled into its argv array, e.g. to
1996 * remove some arguments. If that has happened, break out
1997 * before trying to read from NULL.
1999 if (proc_vector[i] == NULL)
2001 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2002 error = proc_read_string(td, p, sptr, pss_string,
2003 sizeof(pss_string));
2006 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2007 if (done + len >= nchr)
2008 len = nchr - done - 1;
2009 sbuf_bcat(sb, pss_string, len);
2010 if (len != GET_PS_STRINGS_CHUNK_SZ)
2012 done += GET_PS_STRINGS_CHUNK_SZ;
2014 sbuf_bcat(sb, "", 1);
2018 free(proc_vector, M_TEMP);
2023 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2026 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2030 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2033 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2037 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2043 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2045 #ifdef COMPAT_FREEBSD32
2046 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2047 size = vsize * sizeof(Elf32_Auxinfo);
2050 size = vsize * sizeof(Elf_Auxinfo);
2051 if (sbuf_bcat(sb, auxv, size) != 0)
2059 * This sysctl allows a process to retrieve the argument list or process
2060 * title for another process without groping around in the address space
2061 * of the other process. It also allow a process to set its own "process
2062 * title to a string of its own choice.
2065 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2067 int *name = (int *)arg1;
2068 u_int namelen = arg2;
2069 struct pargs *newpa, *pa;
2072 int flags, error = 0, error2;
2078 pid = (pid_t)name[0];
2080 * If the query is for this process and it is single-threaded, there
2081 * is nobody to modify pargs, thus we can just read.
2084 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2085 (pa = p->p_args) != NULL)
2086 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2088 flags = PGET_CANSEE;
2089 if (req->newptr != NULL)
2090 flags |= PGET_ISCURRENT;
2091 error = pget(pid, flags, &p);
2099 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2101 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2104 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2105 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2106 error = proc_getargv(curthread, p, &sb);
2107 error2 = sbuf_finish(&sb);
2110 if (error == 0 && error2 != 0)
2115 if (error != 0 || req->newptr == NULL)
2118 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2121 if (req->newlen == 0) {
2123 * Clear the argument pointer, so that we'll fetch arguments
2124 * with proc_getargv() until further notice.
2128 newpa = pargs_alloc(req->newlen);
2129 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2144 * This sysctl allows a process to retrieve environment of another process.
2147 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2149 int *name = (int *)arg1;
2150 u_int namelen = arg2;
2158 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2161 if ((p->p_flag & P_SYSTEM) != 0) {
2166 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2167 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2168 error = proc_getenvv(curthread, p, &sb);
2169 error2 = sbuf_finish(&sb);
2172 return (error != 0 ? error : error2);
2176 * This sysctl allows a process to retrieve ELF auxiliary vector of
2180 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2182 int *name = (int *)arg1;
2183 u_int namelen = arg2;
2191 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2194 if ((p->p_flag & P_SYSTEM) != 0) {
2198 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2199 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2200 error = proc_getauxv(curthread, p, &sb);
2201 error2 = sbuf_finish(&sb);
2204 return (error != 0 ? error : error2);
2208 * This sysctl allows a process to retrieve the path of the executable for
2209 * itself or another process.
2212 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2214 pid_t *pidp = (pid_t *)arg1;
2215 unsigned int arglen = arg2;
2218 char *retbuf, *freebuf;
2223 if (*pidp == -1) { /* -1 means this process */
2224 p = req->td->td_proc;
2226 error = pget(*pidp, PGET_CANSEE, &p);
2240 error = vn_fullpath(vp, &retbuf, &freebuf);
2244 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2245 free(freebuf, M_TEMP);
2250 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2263 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2266 sv_name = p->p_sysent->sv_name;
2268 return (sysctl_handle_string(oidp, sv_name, 0, req));
2271 #ifdef KINFO_OVMENTRY_SIZE
2272 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2275 #ifdef COMPAT_FREEBSD7
2277 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2279 vm_map_entry_t entry, tmp_entry;
2280 unsigned int last_timestamp;
2281 char *fullpath, *freepath;
2282 struct kinfo_ovmentry *kve;
2292 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2295 vm = vmspace_acquire_ref(p);
2300 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2303 vm_map_lock_read(map);
2304 VM_MAP_ENTRY_FOREACH(entry, map) {
2305 vm_object_t obj, tobj, lobj;
2308 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2311 bzero(kve, sizeof(*kve));
2312 kve->kve_structsize = sizeof(*kve);
2314 kve->kve_private_resident = 0;
2315 obj = entry->object.vm_object;
2317 VM_OBJECT_RLOCK(obj);
2318 if (obj->shadow_count == 1)
2319 kve->kve_private_resident =
2320 obj->resident_page_count;
2322 kve->kve_resident = 0;
2323 addr = entry->start;
2324 while (addr < entry->end) {
2325 if (pmap_extract(map->pmap, addr))
2326 kve->kve_resident++;
2330 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2332 VM_OBJECT_RLOCK(tobj);
2333 kve->kve_offset += tobj->backing_object_offset;
2336 VM_OBJECT_RUNLOCK(lobj);
2340 kve->kve_start = (void*)entry->start;
2341 kve->kve_end = (void*)entry->end;
2342 kve->kve_offset += (off_t)entry->offset;
2344 if (entry->protection & VM_PROT_READ)
2345 kve->kve_protection |= KVME_PROT_READ;
2346 if (entry->protection & VM_PROT_WRITE)
2347 kve->kve_protection |= KVME_PROT_WRITE;
2348 if (entry->protection & VM_PROT_EXECUTE)
2349 kve->kve_protection |= KVME_PROT_EXEC;
2351 if (entry->eflags & MAP_ENTRY_COW)
2352 kve->kve_flags |= KVME_FLAG_COW;
2353 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2354 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2355 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2356 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2358 last_timestamp = map->timestamp;
2359 vm_map_unlock_read(map);
2361 kve->kve_fileid = 0;
2366 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2367 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2368 kve->kve_type = KVME_TYPE_UNKNOWN;
2372 VM_OBJECT_RUNLOCK(lobj);
2374 kve->kve_ref_count = obj->ref_count;
2375 kve->kve_shadow_count = obj->shadow_count;
2376 VM_OBJECT_RUNLOCK(obj);
2378 vn_fullpath(vp, &fullpath, &freepath);
2379 cred = curthread->td_ucred;
2380 vn_lock(vp, LK_SHARED | LK_RETRY);
2381 if (VOP_GETATTR(vp, &va, cred) == 0) {
2382 kve->kve_fileid = va.va_fileid;
2384 kve->kve_fsid = va.va_fsid;
2389 kve->kve_type = KVME_TYPE_NONE;
2390 kve->kve_ref_count = 0;
2391 kve->kve_shadow_count = 0;
2394 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2395 if (freepath != NULL)
2396 free(freepath, M_TEMP);
2398 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2399 vm_map_lock_read(map);
2402 if (last_timestamp != map->timestamp) {
2403 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2407 vm_map_unlock_read(map);
2413 #endif /* COMPAT_FREEBSD7 */
2415 #ifdef KINFO_VMENTRY_SIZE
2416 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2420 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2421 int *resident_count, bool *super)
2423 vm_object_t obj, tobj;
2427 vm_pindex_t pi, pi_adv, pindex;
2430 *resident_count = 0;
2431 if (vmmap_skip_res_cnt)
2435 obj = entry->object.vm_object;
2436 addr = entry->start;
2438 pi = OFF_TO_IDX(entry->offset);
2439 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2440 if (m_adv != NULL) {
2443 pi_adv = atop(entry->end - addr);
2445 for (tobj = obj;; tobj = tobj->backing_object) {
2446 m = vm_page_find_least(tobj, pindex);
2448 if (m->pindex == pindex)
2450 if (pi_adv > m->pindex - pindex) {
2451 pi_adv = m->pindex - pindex;
2455 if (tobj->backing_object == NULL)
2457 pindex += OFF_TO_IDX(tobj->
2458 backing_object_offset);
2462 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2463 (addr & (pagesizes[1] - 1)) == 0 &&
2464 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2466 pi_adv = atop(pagesizes[1]);
2469 * We do not test the found page on validity.
2470 * Either the page is busy and being paged in,
2471 * or it was invalidated. The first case
2472 * should be counted as resident, the second
2473 * is not so clear; we do account both.
2477 *resident_count += pi_adv;
2483 * Must be called with the process locked and will return unlocked.
2486 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2488 vm_map_entry_t entry, tmp_entry;
2491 vm_object_t obj, tobj, lobj;
2492 char *fullpath, *freepath;
2493 struct kinfo_vmentry *kve;
2498 unsigned int last_timestamp;
2502 PROC_LOCK_ASSERT(p, MA_OWNED);
2506 vm = vmspace_acquire_ref(p);
2511 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2515 vm_map_lock_read(map);
2516 VM_MAP_ENTRY_FOREACH(entry, map) {
2517 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2521 bzero(kve, sizeof(*kve));
2522 obj = entry->object.vm_object;
2524 for (tobj = obj; tobj != NULL;
2525 tobj = tobj->backing_object) {
2526 VM_OBJECT_RLOCK(tobj);
2527 kve->kve_offset += tobj->backing_object_offset;
2530 if (obj->backing_object == NULL)
2531 kve->kve_private_resident =
2532 obj->resident_page_count;
2533 kern_proc_vmmap_resident(map, entry,
2534 &kve->kve_resident, &super);
2536 kve->kve_flags |= KVME_FLAG_SUPER;
2537 for (tobj = obj; tobj != NULL;
2538 tobj = tobj->backing_object) {
2539 if (tobj != obj && tobj != lobj)
2540 VM_OBJECT_RUNLOCK(tobj);
2546 kve->kve_start = entry->start;
2547 kve->kve_end = entry->end;
2548 kve->kve_offset += entry->offset;
2550 if (entry->protection & VM_PROT_READ)
2551 kve->kve_protection |= KVME_PROT_READ;
2552 if (entry->protection & VM_PROT_WRITE)
2553 kve->kve_protection |= KVME_PROT_WRITE;
2554 if (entry->protection & VM_PROT_EXECUTE)
2555 kve->kve_protection |= KVME_PROT_EXEC;
2557 if (entry->eflags & MAP_ENTRY_COW)
2558 kve->kve_flags |= KVME_FLAG_COW;
2559 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2560 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2561 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2562 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2563 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2564 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2565 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2566 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2567 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2568 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2570 last_timestamp = map->timestamp;
2571 vm_map_unlock_read(map);
2576 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2580 VM_OBJECT_RUNLOCK(lobj);
2582 kve->kve_ref_count = obj->ref_count;
2583 kve->kve_shadow_count = obj->shadow_count;
2584 VM_OBJECT_RUNLOCK(obj);
2586 vn_fullpath(vp, &fullpath, &freepath);
2587 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2588 cred = curthread->td_ucred;
2589 vn_lock(vp, LK_SHARED | LK_RETRY);
2590 if (VOP_GETATTR(vp, &va, cred) == 0) {
2591 kve->kve_vn_fileid = va.va_fileid;
2592 kve->kve_vn_fsid = va.va_fsid;
2593 kve->kve_vn_fsid_freebsd11 =
2594 kve->kve_vn_fsid; /* truncate */
2596 MAKEIMODE(va.va_type, va.va_mode);
2597 kve->kve_vn_size = va.va_size;
2598 kve->kve_vn_rdev = va.va_rdev;
2599 kve->kve_vn_rdev_freebsd11 =
2600 kve->kve_vn_rdev; /* truncate */
2601 kve->kve_status = KF_ATTR_VALID;
2606 kve->kve_type = KVME_TYPE_NONE;
2607 kve->kve_ref_count = 0;
2608 kve->kve_shadow_count = 0;
2611 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2612 if (freepath != NULL)
2613 free(freepath, M_TEMP);
2615 /* Pack record size down */
2616 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2617 kve->kve_structsize =
2618 offsetof(struct kinfo_vmentry, kve_path) +
2619 strlen(kve->kve_path) + 1;
2621 kve->kve_structsize = sizeof(*kve);
2622 kve->kve_structsize = roundup(kve->kve_structsize,
2625 /* Halt filling and truncate rather than exceeding maxlen */
2626 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2628 vm_map_lock_read(map);
2630 } else if (maxlen != -1)
2631 maxlen -= kve->kve_structsize;
2633 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2635 vm_map_lock_read(map);
2638 if (last_timestamp != map->timestamp) {
2639 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2643 vm_map_unlock_read(map);
2651 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2655 int error, error2, *name;
2658 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2659 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2660 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2665 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2666 error2 = sbuf_finish(&sb);
2668 return (error != 0 ? error : error2);
2671 #if defined(STACK) || defined(DDB)
2673 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2675 struct kinfo_kstack *kkstp;
2676 int error, i, *name, numthreads;
2677 lwpid_t *lwpidarray;
2684 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2688 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2689 st = stack_create(M_WAITOK);
2694 if (lwpidarray != NULL) {
2695 free(lwpidarray, M_TEMP);
2698 numthreads = p->p_numthreads;
2700 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2703 } while (numthreads < p->p_numthreads);
2706 * XXXRW: During the below loop, execve(2) and countless other sorts
2707 * of changes could have taken place. Should we check to see if the
2708 * vmspace has been replaced, or the like, in order to prevent
2709 * giving a snapshot that spans, say, execve(2), with some threads
2710 * before and some after? Among other things, the credentials could
2711 * have changed, in which case the right to extract debug info might
2712 * no longer be assured.
2715 FOREACH_THREAD_IN_PROC(p, td) {
2716 KASSERT(i < numthreads,
2717 ("sysctl_kern_proc_kstack: numthreads"));
2718 lwpidarray[i] = td->td_tid;
2722 for (i = 0; i < numthreads; i++) {
2723 td = thread_find(p, lwpidarray[i]);
2727 bzero(kkstp, sizeof(*kkstp));
2728 (void)sbuf_new(&sb, kkstp->kkst_trace,
2729 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2731 kkstp->kkst_tid = td->td_tid;
2732 if (TD_IS_SWAPPED(td))
2733 kkstp->kkst_state = KKST_STATE_SWAPPED;
2734 else if (stack_save_td(st, td) == 0)
2735 kkstp->kkst_state = KKST_STATE_STACKOK;
2737 kkstp->kkst_state = KKST_STATE_RUNNING;
2740 stack_sbuf_print(&sb, st);
2743 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2750 if (lwpidarray != NULL)
2751 free(lwpidarray, M_TEMP);
2753 free(kkstp, M_TEMP);
2759 * This sysctl allows a process to retrieve the full list of groups from
2760 * itself or another process.
2763 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2765 pid_t *pidp = (pid_t *)arg1;
2766 unsigned int arglen = arg2;
2773 if (*pidp == -1) { /* -1 means this process */
2774 p = req->td->td_proc;
2777 error = pget(*pidp, PGET_CANSEE, &p);
2782 cred = crhold(p->p_ucred);
2785 error = SYSCTL_OUT(req, cred->cr_groups,
2786 cred->cr_ngroups * sizeof(gid_t));
2792 * This sysctl allows a process to retrieve or/and set the resource limit for
2796 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2798 int *name = (int *)arg1;
2799 u_int namelen = arg2;
2808 which = (u_int)name[1];
2809 if (which >= RLIM_NLIMITS)
2812 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2815 flags = PGET_HOLD | PGET_NOTWEXIT;
2816 if (req->newptr != NULL)
2817 flags |= PGET_CANDEBUG;
2819 flags |= PGET_CANSEE;
2820 error = pget((pid_t)name[0], flags, &p);
2827 if (req->oldptr != NULL) {
2829 lim_rlimit_proc(p, which, &rlim);
2832 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2839 if (req->newptr != NULL) {
2840 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2842 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2851 * This sysctl allows a process to retrieve ps_strings structure location of
2855 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2857 int *name = (int *)arg1;
2858 u_int namelen = arg2;
2860 vm_offset_t ps_strings;
2862 #ifdef COMPAT_FREEBSD32
2863 uint32_t ps_strings32;
2869 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2872 #ifdef COMPAT_FREEBSD32
2873 if ((req->flags & SCTL_MASK32) != 0) {
2875 * We return 0 if the 32 bit emulation request is for a 64 bit
2878 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2879 PTROUT(p->p_sysent->sv_psstrings) : 0;
2881 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2885 ps_strings = p->p_sysent->sv_psstrings;
2887 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2892 * This sysctl allows a process to retrieve umask of another process.
2895 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2897 int *name = (int *)arg1;
2898 u_int namelen = arg2;
2907 pid = (pid_t)name[0];
2909 if (pid == p->p_pid || pid == 0) {
2910 fd_cmask = p->p_fd->fd_cmask;
2914 error = pget(pid, PGET_WANTREAD, &p);
2918 fd_cmask = p->p_fd->fd_cmask;
2921 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2926 * This sysctl allows a process to set and retrieve binary osreldate of
2930 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2932 int *name = (int *)arg1;
2933 u_int namelen = arg2;
2935 int flags, error, osrel;
2940 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2943 flags = PGET_HOLD | PGET_NOTWEXIT;
2944 if (req->newptr != NULL)
2945 flags |= PGET_CANDEBUG;
2947 flags |= PGET_CANSEE;
2948 error = pget((pid_t)name[0], flags, &p);
2952 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2956 if (req->newptr != NULL) {
2957 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2972 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2974 int *name = (int *)arg1;
2975 u_int namelen = arg2;
2977 struct kinfo_sigtramp kst;
2978 const struct sysentvec *sv;
2980 #ifdef COMPAT_FREEBSD32
2981 struct kinfo_sigtramp32 kst32;
2987 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2991 #ifdef COMPAT_FREEBSD32
2992 if ((req->flags & SCTL_MASK32) != 0) {
2993 bzero(&kst32, sizeof(kst32));
2994 if (SV_PROC_FLAG(p, SV_ILP32)) {
2995 if (sv->sv_sigcode_base != 0) {
2996 kst32.ksigtramp_start = sv->sv_sigcode_base;
2997 kst32.ksigtramp_end = sv->sv_sigcode_base +
3000 kst32.ksigtramp_start = sv->sv_psstrings -
3002 kst32.ksigtramp_end = sv->sv_psstrings;
3006 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3010 bzero(&kst, sizeof(kst));
3011 if (sv->sv_sigcode_base != 0) {
3012 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3013 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3016 kst.ksigtramp_start = (char *)sv->sv_psstrings -
3018 kst.ksigtramp_end = (char *)sv->sv_psstrings;
3021 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3026 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3028 int *name = (int *)arg1;
3029 u_int namelen = arg2;
3034 #ifdef COMPAT_FREEBSD32
3039 if (namelen != 1 || req->newptr != NULL)
3042 pid = (pid_t)name[0];
3043 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3048 #ifdef COMPAT_FREEBSD32
3049 if (SV_CURPROC_FLAG(SV_ILP32)) {
3050 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3056 if (pid <= PID_MAX) {
3057 td1 = FIRST_THREAD_IN_PROC(p);
3059 FOREACH_THREAD_IN_PROC(p, td1) {
3060 if (td1->td_tid == pid)
3069 * The access to the private thread flags. It is fine as far
3070 * as no out-of-thin-air values are read from td_pflags, and
3071 * usermode read of the td_sigblock_ptr is racy inherently,
3072 * since target process might have already changed it
3075 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3076 addr = (uintptr_t)td1->td_sigblock_ptr;
3086 #ifdef COMPAT_FREEBSD32
3087 if (SV_CURPROC_FLAG(SV_ILP32)) {
3089 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3092 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3096 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3099 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3100 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3101 "Return entire process table");
3103 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3104 sysctl_kern_proc, "Process table");
3106 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3107 sysctl_kern_proc, "Process table");
3109 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3110 sysctl_kern_proc, "Process table");
3112 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3113 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3115 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3116 sysctl_kern_proc, "Process table");
3118 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3119 sysctl_kern_proc, "Process table");
3121 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3122 sysctl_kern_proc, "Process table");
3124 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3125 sysctl_kern_proc, "Process table");
3127 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3128 sysctl_kern_proc, "Return process table, no threads");
3130 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3131 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3132 sysctl_kern_proc_args, "Process argument list");
3134 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3135 sysctl_kern_proc_env, "Process environment");
3137 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3138 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3140 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3141 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3143 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3144 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3145 "Process syscall vector name (ABI type)");
3147 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3148 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3150 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3151 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3153 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3154 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3156 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3157 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3159 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3160 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3162 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3163 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3165 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3166 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3168 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3169 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3171 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3172 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3173 "Return process table, no threads");
3175 #ifdef COMPAT_FREEBSD7
3176 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3177 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3180 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3181 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3183 #if defined(STACK) || defined(DDB)
3184 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3185 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3188 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3189 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3191 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3192 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3193 "Process resource limits");
3195 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3196 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3197 "Process ps_strings location");
3199 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3200 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3202 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3203 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3204 "Process binary osreldate");
3206 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3207 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3208 "Process signal trampoline location");
3210 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3211 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3212 "Thread sigfastblock address");
3217 * stop_all_proc() purpose is to stop all process which have usermode,
3218 * except current process for obvious reasons. This makes it somewhat
3219 * unreliable when invoked from multithreaded process. The service
3220 * must not be user-callable anyway.
3225 struct proc *cp, *p;
3227 bool restart, seen_stopped, seen_exiting, stopped_some;
3231 sx_xlock(&allproc_lock);
3233 seen_exiting = seen_stopped = stopped_some = restart = false;
3234 LIST_REMOVE(cp, p_list);
3235 LIST_INSERT_HEAD(&allproc, cp, p_list);
3237 p = LIST_NEXT(cp, p_list);
3240 LIST_REMOVE(cp, p_list);
3241 LIST_INSERT_AFTER(p, cp, p_list);
3243 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3247 if ((p->p_flag & P_WEXIT) != 0) {
3248 seen_exiting = true;
3252 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3254 * Stopped processes are tolerated when there
3255 * are no other processes which might continue
3256 * them. P_STOPPED_SINGLE but not
3257 * P_TOTAL_STOP process still has at least one
3260 seen_stopped = true;
3264 sx_xunlock(&allproc_lock);
3266 r = thread_single(p, SINGLE_ALLPROC);
3270 stopped_some = true;
3273 sx_xlock(&allproc_lock);
3275 /* Catch forked children we did not see in iteration. */
3276 if (gen != allproc_gen)
3278 sx_xunlock(&allproc_lock);
3279 if (restart || stopped_some || seen_exiting || seen_stopped) {
3280 kern_yield(PRI_USER);
3286 resume_all_proc(void)
3288 struct proc *cp, *p;
3291 sx_xlock(&allproc_lock);
3293 LIST_REMOVE(cp, p_list);
3294 LIST_INSERT_HEAD(&allproc, cp, p_list);
3296 p = LIST_NEXT(cp, p_list);
3299 LIST_REMOVE(cp, p_list);
3300 LIST_INSERT_AFTER(p, cp, p_list);
3302 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3303 sx_xunlock(&allproc_lock);
3305 thread_single_end(p, SINGLE_ALLPROC);
3308 sx_xlock(&allproc_lock);
3313 /* Did the loop above missed any stopped process ? */
3314 FOREACH_PROC_IN_SYSTEM(p) {
3315 /* No need for proc lock. */
3316 if ((p->p_flag & P_TOTAL_STOP) != 0)
3319 sx_xunlock(&allproc_lock);
3322 /* #define TOTAL_STOP_DEBUG 1 */
3323 #ifdef TOTAL_STOP_DEBUG
3324 volatile static int ap_resume;
3325 #include <sys/mount.h>
3328 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3334 error = sysctl_handle_int(oidp, &val, 0, req);
3335 if (error != 0 || req->newptr == NULL)
3340 while (ap_resume == 0)
3348 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3349 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3350 sysctl_debug_stop_all_proc, "I",