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);
649 jobc_reaper(struct proc *p)
653 sx_assert(&proctree_lock, SX_LOCKED);
657 if (pp->p_reaper == pp ||
658 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
664 jobc_parent(struct proc *p)
668 sx_assert(&proctree_lock, SX_LOCKED);
670 pp = proc_realparent(p);
671 if (pp->p_pptr == NULL ||
672 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
674 return (jobc_reaper(pp));
679 check_pgrp_jobc(struct pgrp *pgrp)
684 sx_assert(&proctree_lock, SX_LOCKED);
685 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
689 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
690 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
693 if (isjobproc(jobc_parent(q), pgrp))
696 KASSERT(pgrp->pg_jobc == cnt, ("pgrp %d %p pg_jobc %d cnt %d",
697 pgrp->pg_id, pgrp, pgrp->pg_jobc, cnt));
703 * Move p to a process group
706 doenterpgrp(struct proc *p, struct pgrp *pgrp)
708 struct pgrp *savepgrp;
710 sx_assert(&proctree_lock, SX_XLOCKED);
711 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
712 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
713 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
714 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
716 savepgrp = p->p_pgrp;
719 check_pgrp_jobc(pgrp);
720 check_pgrp_jobc(savepgrp);
724 * Adjust eligibility of affected pgrps to participate in job control.
726 fixjobc_enterpgrp(p, pgrp);
731 LIST_REMOVE(p, p_pglist);
734 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
735 PGRP_UNLOCK(savepgrp);
737 if (LIST_EMPTY(&savepgrp->pg_members))
742 * remove process from process group
745 leavepgrp(struct proc *p)
747 struct pgrp *savepgrp;
749 sx_assert(&proctree_lock, SX_XLOCKED);
750 savepgrp = p->p_pgrp;
753 LIST_REMOVE(p, p_pglist);
756 PGRP_UNLOCK(savepgrp);
757 if (LIST_EMPTY(&savepgrp->pg_members))
763 * delete a process group
766 pgdelete(struct pgrp *pgrp)
768 struct session *savesess;
771 sx_assert(&proctree_lock, SX_XLOCKED);
772 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
773 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
776 * Reset any sigio structures pointing to us as a result of
777 * F_SETOWN with our pgid. The proctree lock ensures that
778 * new sigio structures will not be added after this point.
780 funsetownlst(&pgrp->pg_sigiolst);
783 tp = pgrp->pg_session->s_ttyp;
784 LIST_REMOVE(pgrp, pg_hash);
785 savesess = pgrp->pg_session;
788 /* Remove the reference to the pgrp before deallocating it. */
791 tty_rel_pgrp(tp, pgrp);
794 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
795 mtx_destroy(&pgrp->pg_mtx);
797 sess_release(savesess);
801 pgadjustjobc(struct pgrp *pgrp, bool entering)
806 MPASS(pgrp->pg_jobc >= 0);
809 MPASS(pgrp->pg_jobc > 0);
811 if (pgrp->pg_jobc == 0)
818 fixjobc_enterpgrp_q(struct pgrp *pgrp, struct proc *p, struct proc *q, bool adj)
820 struct pgrp *childpgrp;
823 sx_assert(&proctree_lock, SX_LOCKED);
825 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
827 childpgrp = q->p_pgrp;
828 future_jobc = childpgrp != pgrp &&
829 childpgrp->pg_session == pgrp->pg_session;
831 if ((adj && !isjobproc(p, childpgrp) && future_jobc) ||
832 (!adj && isjobproc(p, childpgrp) && !future_jobc))
833 pgadjustjobc(childpgrp, adj);
837 * Adjust pgrp jobc counters when specified process changes process group.
838 * We count the number of processes in each process group that "qualify"
839 * the group for terminal job control (those with a parent in a different
840 * process group of the same session). If that count reaches zero, the
841 * process group becomes orphaned. Check both the specified process'
842 * process group and that of its children.
843 * We increment eligibility counts before decrementing, otherwise we
844 * could reach 0 spuriously during the decrement.
847 fixjobc_enterpgrp(struct proc *p, struct pgrp *pgrp)
851 sx_assert(&proctree_lock, SX_LOCKED);
852 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
853 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
854 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
856 if (p->p_pgrp == pgrp)
859 if (isjobproc(jobc_parent(p), pgrp))
860 pgadjustjobc(pgrp, true);
861 LIST_FOREACH(q, &p->p_children, p_sibling) {
862 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
864 fixjobc_enterpgrp_q(pgrp, p, q, true);
866 LIST_FOREACH(q, &p->p_orphans, p_orphan)
867 fixjobc_enterpgrp_q(pgrp, p, q, true);
869 if (isjobproc(jobc_parent(p), p->p_pgrp))
870 pgadjustjobc(p->p_pgrp, false);
871 LIST_FOREACH(q, &p->p_children, p_sibling) {
872 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
874 fixjobc_enterpgrp_q(pgrp, p, q, false);
876 LIST_FOREACH(q, &p->p_orphans, p_orphan)
877 fixjobc_enterpgrp_q(pgrp, p, q, false);
881 fixjobc_kill_q(struct proc *p, struct proc *q, bool adj)
883 struct pgrp *childpgrp;
885 sx_assert(&proctree_lock, SX_LOCKED);
887 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
889 childpgrp = q->p_pgrp;
891 if ((adj && isjobproc(jobc_reaper(q), childpgrp) &&
892 !isjobproc(p, childpgrp)) || (!adj && !isjobproc(jobc_reaper(q),
893 childpgrp) && isjobproc(p, childpgrp)))
894 pgadjustjobc(childpgrp, adj);
898 fixjobc_kill(struct proc *p)
903 sx_assert(&proctree_lock, SX_LOCKED);
904 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
906 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
907 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
909 check_pgrp_jobc(pgrp);
913 * p no longer affects process group orphanage for children.
914 * It is marked by the flag because p is only physically
915 * removed from its process group on wait(2).
917 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
918 p->p_treeflag |= P_TREE_GRPEXITED;
921 * Check p's parent to see whether p qualifies its own process
922 * group; if so, adjust count for p's process group.
924 if (isjobproc(jobc_parent(p), pgrp))
925 pgadjustjobc(pgrp, false);
928 * Check this process' children to see whether they qualify
929 * their process groups after reparenting to reaper. If so,
930 * adjust counts for children's process groups.
932 LIST_FOREACH(q, &p->p_children, p_sibling) {
933 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
935 fixjobc_kill_q(p, q, true);
937 LIST_FOREACH(q, &p->p_orphans, p_orphan)
938 fixjobc_kill_q(p, q, true);
939 LIST_FOREACH(q, &p->p_children, p_sibling) {
940 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
942 fixjobc_kill_q(p, q, false);
944 LIST_FOREACH(q, &p->p_orphans, p_orphan)
945 fixjobc_kill_q(p, q, false);
948 check_pgrp_jobc(pgrp);
961 MPASS(p->p_flag & P_WEXIT);
962 sx_assert(&proctree_lock, SX_LOCKED);
964 if (SESS_LEADER(p)) {
968 * s_ttyp is not zero'd; we use this to indicate that
969 * the session once had a controlling terminal. (for
970 * logging and informational purposes)
981 * Signal foreground pgrp and revoke access to
982 * controlling terminal if it has not been revoked
985 * Because the TTY may have been revoked in the mean
986 * time and could already have a new session associated
987 * with it, make sure we don't send a SIGHUP to a
988 * foreground process group that does not belong to this
994 if (tp->t_session == sp)
995 tty_signal_pgrp(tp, SIGHUP);
1000 sx_xunlock(&proctree_lock);
1001 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
1002 VOP_REVOKE(ttyvp, REVOKEALL);
1005 devfs_ctty_unref(ttyvp);
1006 sx_xlock(&proctree_lock);
1013 * A process group has become orphaned;
1014 * if there are any stopped processes in the group,
1015 * hang-up all process in that group.
1018 orphanpg(struct pgrp *pg)
1022 PGRP_LOCK_ASSERT(pg, MA_OWNED);
1024 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1026 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
1028 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1030 kern_psignal(p, SIGHUP);
1031 kern_psignal(p, SIGCONT);
1041 sess_hold(struct session *s)
1044 refcount_acquire(&s->s_count);
1048 sess_release(struct session *s)
1051 if (refcount_release(&s->s_count)) {
1052 if (s->s_ttyp != NULL) {
1053 tty_lock(s->s_ttyp);
1054 tty_rel_sess(s->s_ttyp, s);
1056 proc_id_clear(PROC_ID_SESSION, s->s_sid);
1057 mtx_destroy(&s->s_mtx);
1065 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
1068 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
1069 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
1070 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
1071 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
1074 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1080 for (i = 0; i <= pgrphash; i++) {
1081 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1082 db_printf("indx %d\n", i);
1083 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1085 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1086 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1087 pgrp->pg_session->s_count,
1088 LIST_FIRST(&pgrp->pg_members));
1089 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1090 db_print_pgrp_one(pgrp, p);
1098 * Calculate the kinfo_proc members which contain process-wide
1100 * Must be called with the target process locked.
1103 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1107 PROC_LOCK_ASSERT(p, MA_OWNED);
1111 FOREACH_THREAD_IN_PROC(p, td) {
1113 kp->ki_pctcpu += sched_pctcpu(td);
1114 kp->ki_estcpu += sched_estcpu(td);
1120 * Clear kinfo_proc and fill in any information that is common
1121 * to all threads in the process.
1122 * Must be called with the target process locked.
1125 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1132 struct timeval boottime;
1134 PROC_LOCK_ASSERT(p, MA_OWNED);
1135 bzero(kp, sizeof(*kp));
1137 kp->ki_structsize = sizeof(*kp);
1139 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1140 kp->ki_args = p->p_args;
1141 kp->ki_textvp = p->p_textvp;
1143 kp->ki_tracep = p->p_tracevp;
1144 kp->ki_traceflag = p->p_traceflag;
1146 kp->ki_fd = p->p_fd;
1147 kp->ki_vmspace = p->p_vmspace;
1148 kp->ki_flag = p->p_flag;
1149 kp->ki_flag2 = p->p_flag2;
1152 kp->ki_uid = cred->cr_uid;
1153 kp->ki_ruid = cred->cr_ruid;
1154 kp->ki_svuid = cred->cr_svuid;
1155 kp->ki_cr_flags = 0;
1156 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1157 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1158 /* XXX bde doesn't like KI_NGROUPS */
1159 if (cred->cr_ngroups > KI_NGROUPS) {
1160 kp->ki_ngroups = KI_NGROUPS;
1161 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1163 kp->ki_ngroups = cred->cr_ngroups;
1164 bcopy(cred->cr_groups, kp->ki_groups,
1165 kp->ki_ngroups * sizeof(gid_t));
1166 kp->ki_rgid = cred->cr_rgid;
1167 kp->ki_svgid = cred->cr_svgid;
1168 /* If jailed(cred), emulate the old P_JAILED flag. */
1170 kp->ki_flag |= P_JAILED;
1171 /* If inside the jail, use 0 as a jail ID. */
1172 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1173 kp->ki_jid = cred->cr_prison->pr_id;
1175 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1176 sizeof(kp->ki_loginclass));
1180 mtx_lock(&ps->ps_mtx);
1181 kp->ki_sigignore = ps->ps_sigignore;
1182 kp->ki_sigcatch = ps->ps_sigcatch;
1183 mtx_unlock(&ps->ps_mtx);
1185 if (p->p_state != PRS_NEW &&
1186 p->p_state != PRS_ZOMBIE &&
1187 p->p_vmspace != NULL) {
1188 struct vmspace *vm = p->p_vmspace;
1190 kp->ki_size = vm->vm_map.size;
1191 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1192 FOREACH_THREAD_IN_PROC(p, td0) {
1193 if (!TD_IS_SWAPPED(td0))
1194 kp->ki_rssize += td0->td_kstack_pages;
1196 kp->ki_swrss = vm->vm_swrss;
1197 kp->ki_tsize = vm->vm_tsize;
1198 kp->ki_dsize = vm->vm_dsize;
1199 kp->ki_ssize = vm->vm_ssize;
1200 } else if (p->p_state == PRS_ZOMBIE)
1201 kp->ki_stat = SZOMB;
1202 if (kp->ki_flag & P_INMEM)
1203 kp->ki_sflag = PS_INMEM;
1206 /* Calculate legacy swtime as seconds since 'swtick'. */
1207 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1208 kp->ki_pid = p->p_pid;
1209 kp->ki_nice = p->p_nice;
1210 kp->ki_fibnum = p->p_fibnum;
1211 kp->ki_start = p->p_stats->p_start;
1212 getboottime(&boottime);
1213 timevaladd(&kp->ki_start, &boottime);
1215 rufetch(p, &kp->ki_rusage);
1216 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1217 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1219 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1220 /* Some callers want child times in a single value. */
1221 kp->ki_childtime = kp->ki_childstime;
1222 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1224 FOREACH_THREAD_IN_PROC(p, td0)
1225 kp->ki_cow += td0->td_cow;
1229 kp->ki_pgid = p->p_pgrp->pg_id;
1230 kp->ki_jobc = p->p_pgrp->pg_jobc;
1231 sp = p->p_pgrp->pg_session;
1234 kp->ki_sid = sp->s_sid;
1236 strlcpy(kp->ki_login, sp->s_login,
1237 sizeof(kp->ki_login));
1239 kp->ki_kiflag |= KI_CTTY;
1241 kp->ki_kiflag |= KI_SLEADER;
1242 /* XXX proctree_lock */
1247 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1248 kp->ki_tdev = tty_udev(tp);
1249 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1250 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1252 kp->ki_tsid = tp->t_session->s_sid;
1254 kp->ki_tdev = NODEV;
1255 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1257 if (p->p_comm[0] != '\0')
1258 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1259 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1260 p->p_sysent->sv_name[0] != '\0')
1261 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1262 kp->ki_siglist = p->p_siglist;
1263 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1264 kp->ki_acflag = p->p_acflag;
1265 kp->ki_lock = p->p_lock;
1267 kp->ki_ppid = p->p_oppid;
1268 if (p->p_flag & P_TRACED)
1269 kp->ki_tracer = p->p_pptr->p_pid;
1274 * Fill in information that is thread specific. Must be called with
1275 * target process locked. If 'preferthread' is set, overwrite certain
1276 * process-related fields that are maintained for both threads and
1280 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1286 PROC_LOCK_ASSERT(p, MA_OWNED);
1291 if (td->td_wmesg != NULL)
1292 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1294 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1295 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1296 sizeof(kp->ki_tdname)) {
1297 strlcpy(kp->ki_moretdname,
1298 td->td_name + sizeof(kp->ki_tdname) - 1,
1299 sizeof(kp->ki_moretdname));
1301 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1303 if (TD_ON_LOCK(td)) {
1304 kp->ki_kiflag |= KI_LOCKBLOCK;
1305 strlcpy(kp->ki_lockname, td->td_lockname,
1306 sizeof(kp->ki_lockname));
1308 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1309 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1312 if (p->p_state == PRS_NORMAL) { /* approximate. */
1313 if (TD_ON_RUNQ(td) ||
1315 TD_IS_RUNNING(td)) {
1317 } else if (P_SHOULDSTOP(p)) {
1318 kp->ki_stat = SSTOP;
1319 } else if (TD_IS_SLEEPING(td)) {
1320 kp->ki_stat = SSLEEP;
1321 } else if (TD_ON_LOCK(td)) {
1322 kp->ki_stat = SLOCK;
1324 kp->ki_stat = SWAIT;
1326 } else if (p->p_state == PRS_ZOMBIE) {
1327 kp->ki_stat = SZOMB;
1332 /* Things in the thread */
1333 kp->ki_wchan = td->td_wchan;
1334 kp->ki_pri.pri_level = td->td_priority;
1335 kp->ki_pri.pri_native = td->td_base_pri;
1338 * Note: legacy fields; clamp at the old NOCPU value and/or
1339 * the maximum u_char CPU value.
1341 if (td->td_lastcpu == NOCPU)
1342 kp->ki_lastcpu_old = NOCPU_OLD;
1343 else if (td->td_lastcpu > MAXCPU_OLD)
1344 kp->ki_lastcpu_old = MAXCPU_OLD;
1346 kp->ki_lastcpu_old = td->td_lastcpu;
1348 if (td->td_oncpu == NOCPU)
1349 kp->ki_oncpu_old = NOCPU_OLD;
1350 else if (td->td_oncpu > MAXCPU_OLD)
1351 kp->ki_oncpu_old = MAXCPU_OLD;
1353 kp->ki_oncpu_old = td->td_oncpu;
1355 kp->ki_lastcpu = td->td_lastcpu;
1356 kp->ki_oncpu = td->td_oncpu;
1357 kp->ki_tdflags = td->td_flags;
1358 kp->ki_tid = td->td_tid;
1359 kp->ki_numthreads = p->p_numthreads;
1360 kp->ki_pcb = td->td_pcb;
1361 kp->ki_kstack = (void *)td->td_kstack;
1362 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1363 kp->ki_pri.pri_class = td->td_pri_class;
1364 kp->ki_pri.pri_user = td->td_user_pri;
1367 rufetchtd(td, &kp->ki_rusage);
1368 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1369 kp->ki_pctcpu = sched_pctcpu(td);
1370 kp->ki_estcpu = sched_estcpu(td);
1371 kp->ki_cow = td->td_cow;
1374 /* We can't get this anymore but ps etc never used it anyway. */
1378 kp->ki_siglist = td->td_siglist;
1379 kp->ki_sigmask = td->td_sigmask;
1386 * Fill in a kinfo_proc structure for the specified process.
1387 * Must be called with the target process locked.
1390 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1393 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1395 fill_kinfo_proc_only(p, kp);
1396 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1397 fill_kinfo_aggregate(p, kp);
1404 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1408 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1411 pstats_fork(struct pstats *src, struct pstats *dst)
1414 bzero(&dst->pstat_startzero,
1415 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1416 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1417 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1421 pstats_free(struct pstats *ps)
1424 free(ps, M_SUBPROC);
1427 #ifdef COMPAT_FREEBSD32
1430 * This function is typically used to copy out the kernel address, so
1431 * it can be replaced by assignment of zero.
1433 static inline uint32_t
1434 ptr32_trim(const void *ptr)
1438 uptr = (uintptr_t)ptr;
1439 return ((uptr > UINT_MAX) ? 0 : uptr);
1442 #define PTRTRIM_CP(src,dst,fld) \
1443 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1446 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1450 bzero(ki32, sizeof(struct kinfo_proc32));
1451 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1452 CP(*ki, *ki32, ki_layout);
1453 PTRTRIM_CP(*ki, *ki32, ki_args);
1454 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1455 PTRTRIM_CP(*ki, *ki32, ki_addr);
1456 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1457 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1458 PTRTRIM_CP(*ki, *ki32, ki_fd);
1459 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1460 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1461 CP(*ki, *ki32, ki_pid);
1462 CP(*ki, *ki32, ki_ppid);
1463 CP(*ki, *ki32, ki_pgid);
1464 CP(*ki, *ki32, ki_tpgid);
1465 CP(*ki, *ki32, ki_sid);
1466 CP(*ki, *ki32, ki_tsid);
1467 CP(*ki, *ki32, ki_jobc);
1468 CP(*ki, *ki32, ki_tdev);
1469 CP(*ki, *ki32, ki_tdev_freebsd11);
1470 CP(*ki, *ki32, ki_siglist);
1471 CP(*ki, *ki32, ki_sigmask);
1472 CP(*ki, *ki32, ki_sigignore);
1473 CP(*ki, *ki32, ki_sigcatch);
1474 CP(*ki, *ki32, ki_uid);
1475 CP(*ki, *ki32, ki_ruid);
1476 CP(*ki, *ki32, ki_svuid);
1477 CP(*ki, *ki32, ki_rgid);
1478 CP(*ki, *ki32, ki_svgid);
1479 CP(*ki, *ki32, ki_ngroups);
1480 for (i = 0; i < KI_NGROUPS; i++)
1481 CP(*ki, *ki32, ki_groups[i]);
1482 CP(*ki, *ki32, ki_size);
1483 CP(*ki, *ki32, ki_rssize);
1484 CP(*ki, *ki32, ki_swrss);
1485 CP(*ki, *ki32, ki_tsize);
1486 CP(*ki, *ki32, ki_dsize);
1487 CP(*ki, *ki32, ki_ssize);
1488 CP(*ki, *ki32, ki_xstat);
1489 CP(*ki, *ki32, ki_acflag);
1490 CP(*ki, *ki32, ki_pctcpu);
1491 CP(*ki, *ki32, ki_estcpu);
1492 CP(*ki, *ki32, ki_slptime);
1493 CP(*ki, *ki32, ki_swtime);
1494 CP(*ki, *ki32, ki_cow);
1495 CP(*ki, *ki32, ki_runtime);
1496 TV_CP(*ki, *ki32, ki_start);
1497 TV_CP(*ki, *ki32, ki_childtime);
1498 CP(*ki, *ki32, ki_flag);
1499 CP(*ki, *ki32, ki_kiflag);
1500 CP(*ki, *ki32, ki_traceflag);
1501 CP(*ki, *ki32, ki_stat);
1502 CP(*ki, *ki32, ki_nice);
1503 CP(*ki, *ki32, ki_lock);
1504 CP(*ki, *ki32, ki_rqindex);
1505 CP(*ki, *ki32, ki_oncpu);
1506 CP(*ki, *ki32, ki_lastcpu);
1508 /* XXX TODO: wrap cpu value as appropriate */
1509 CP(*ki, *ki32, ki_oncpu_old);
1510 CP(*ki, *ki32, ki_lastcpu_old);
1512 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1513 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1514 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1515 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1516 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1517 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1518 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1519 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1520 CP(*ki, *ki32, ki_tracer);
1521 CP(*ki, *ki32, ki_flag2);
1522 CP(*ki, *ki32, ki_fibnum);
1523 CP(*ki, *ki32, ki_cr_flags);
1524 CP(*ki, *ki32, ki_jid);
1525 CP(*ki, *ki32, ki_numthreads);
1526 CP(*ki, *ki32, ki_tid);
1527 CP(*ki, *ki32, ki_pri);
1528 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1529 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1530 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1531 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1532 PTRTRIM_CP(*ki, *ki32, ki_udata);
1533 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1534 CP(*ki, *ki32, ki_sflag);
1535 CP(*ki, *ki32, ki_tdflags);
1540 kern_proc_out_size(struct proc *p, int flags)
1544 PROC_LOCK_ASSERT(p, MA_OWNED);
1546 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1547 #ifdef COMPAT_FREEBSD32
1548 if ((flags & KERN_PROC_MASK32) != 0) {
1549 size += sizeof(struct kinfo_proc32);
1552 size += sizeof(struct kinfo_proc);
1554 #ifdef COMPAT_FREEBSD32
1555 if ((flags & KERN_PROC_MASK32) != 0)
1556 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1559 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1566 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1569 struct kinfo_proc ki;
1570 #ifdef COMPAT_FREEBSD32
1571 struct kinfo_proc32 ki32;
1575 PROC_LOCK_ASSERT(p, MA_OWNED);
1576 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1579 fill_kinfo_proc(p, &ki);
1580 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1581 #ifdef COMPAT_FREEBSD32
1582 if ((flags & KERN_PROC_MASK32) != 0) {
1583 freebsd32_kinfo_proc_out(&ki, &ki32);
1584 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1588 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1591 FOREACH_THREAD_IN_PROC(p, td) {
1592 fill_kinfo_thread(td, &ki, 1);
1593 #ifdef COMPAT_FREEBSD32
1594 if ((flags & KERN_PROC_MASK32) != 0) {
1595 freebsd32_kinfo_proc_out(&ki, &ki32);
1596 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1600 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1611 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1614 struct kinfo_proc ki;
1617 if (req->oldptr == NULL)
1618 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1620 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1621 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1622 error = kern_proc_out(p, &sb, flags);
1623 error2 = sbuf_finish(&sb);
1627 else if (error2 != 0)
1633 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1638 for (i = 0; i < pidhashlock + 1; i++) {
1639 sx_slock(&pidhashtbl_lock[i]);
1640 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1641 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1642 if (p->p_state == PRS_NEW)
1644 error = cb(p, cbarg);
1645 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1647 sx_sunlock(&pidhashtbl_lock[i]);
1652 sx_sunlock(&pidhashtbl_lock[i]);
1657 struct kern_proc_out_args {
1658 struct sysctl_req *req;
1665 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1667 struct kern_proc_out_args *arg = origarg;
1668 int *name = arg->name;
1669 int oid_number = arg->oid_number;
1670 int flags = arg->flags;
1671 struct sysctl_req *req = arg->req;
1676 KASSERT(p->p_ucred != NULL,
1677 ("process credential is NULL for non-NEW proc"));
1679 * Show a user only appropriate processes.
1681 if (p_cansee(curthread, p))
1684 * TODO - make more efficient (see notes below).
1687 switch (oid_number) {
1689 if (p->p_ucred->cr_gid != (gid_t)name[0])
1693 case KERN_PROC_PGRP:
1694 /* could do this by traversing pgrp */
1695 if (p->p_pgrp == NULL ||
1696 p->p_pgrp->pg_id != (pid_t)name[0])
1700 case KERN_PROC_RGID:
1701 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1705 case KERN_PROC_SESSION:
1706 if (p->p_session == NULL ||
1707 p->p_session->s_sid != (pid_t)name[0])
1712 if ((p->p_flag & P_CONTROLT) == 0 ||
1713 p->p_session == NULL)
1715 /* XXX proctree_lock */
1716 SESS_LOCK(p->p_session);
1717 if (p->p_session->s_ttyp == NULL ||
1718 tty_udev(p->p_session->s_ttyp) !=
1720 SESS_UNLOCK(p->p_session);
1723 SESS_UNLOCK(p->p_session);
1727 if (p->p_ucred->cr_uid != (uid_t)name[0])
1731 case KERN_PROC_RUID:
1732 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1736 case KERN_PROC_PROC:
1742 error = sysctl_out_proc(p, req, flags);
1743 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1751 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1753 struct kern_proc_out_args iterarg;
1754 int *name = (int *)arg1;
1755 u_int namelen = arg2;
1757 int flags, oid_number;
1760 oid_number = oidp->oid_number;
1761 if (oid_number != KERN_PROC_ALL &&
1762 (oid_number & KERN_PROC_INC_THREAD) == 0)
1763 flags = KERN_PROC_NOTHREADS;
1766 oid_number &= ~KERN_PROC_INC_THREAD;
1768 #ifdef COMPAT_FREEBSD32
1769 if (req->flags & SCTL_MASK32)
1770 flags |= KERN_PROC_MASK32;
1772 if (oid_number == KERN_PROC_PID) {
1775 error = sysctl_wire_old_buffer(req, 0);
1778 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1780 error = sysctl_out_proc(p, req, flags);
1784 switch (oid_number) {
1789 case KERN_PROC_PROC:
1790 if (namelen != 0 && namelen != 1)
1799 if (req->oldptr == NULL) {
1800 /* overestimate by 5 procs */
1801 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1805 error = sysctl_wire_old_buffer(req, 0);
1809 iterarg.flags = flags;
1810 iterarg.oid_number = oid_number;
1812 iterarg.name = name;
1813 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1818 pargs_alloc(int len)
1822 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1824 refcount_init(&pa->ar_ref, 1);
1825 pa->ar_length = len;
1830 pargs_free(struct pargs *pa)
1837 pargs_hold(struct pargs *pa)
1842 refcount_acquire(&pa->ar_ref);
1846 pargs_drop(struct pargs *pa)
1851 if (refcount_release(&pa->ar_ref))
1856 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1862 * This may return a short read if the string is shorter than the chunk
1863 * and is aligned at the end of the page, and the following page is not
1866 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1872 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1874 enum proc_vector_type {
1880 #ifdef COMPAT_FREEBSD32
1882 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1883 size_t *vsizep, enum proc_vector_type type)
1885 struct freebsd32_ps_strings pss;
1887 vm_offset_t vptr, ptr;
1888 uint32_t *proc_vector32;
1894 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1895 sizeof(pss)) != sizeof(pss))
1899 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1900 vsize = pss.ps_nargvstr;
1901 if (vsize > ARG_MAX)
1903 size = vsize * sizeof(int32_t);
1906 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1907 vsize = pss.ps_nenvstr;
1908 if (vsize > ARG_MAX)
1910 size = vsize * sizeof(int32_t);
1913 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1914 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1917 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1918 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1921 if (aux.a_type == AT_NULL)
1925 if (aux.a_type != AT_NULL)
1928 size = vsize * sizeof(aux);
1931 KASSERT(0, ("Wrong proc vector type: %d", type));
1934 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1935 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1939 if (type == PROC_AUX) {
1940 *proc_vectorp = (char **)proc_vector32;
1944 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1945 for (i = 0; i < (int)vsize; i++)
1946 proc_vector[i] = PTRIN(proc_vector32[i]);
1947 *proc_vectorp = proc_vector;
1950 free(proc_vector32, M_TEMP);
1956 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1957 size_t *vsizep, enum proc_vector_type type)
1959 struct ps_strings pss;
1961 vm_offset_t vptr, ptr;
1966 #ifdef COMPAT_FREEBSD32
1967 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1968 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1970 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1971 sizeof(pss)) != sizeof(pss))
1975 vptr = (vm_offset_t)pss.ps_argvstr;
1976 vsize = pss.ps_nargvstr;
1977 if (vsize > ARG_MAX)
1979 size = vsize * sizeof(char *);
1982 vptr = (vm_offset_t)pss.ps_envstr;
1983 vsize = pss.ps_nenvstr;
1984 if (vsize > ARG_MAX)
1986 size = vsize * sizeof(char *);
1990 * The aux array is just above env array on the stack. Check
1991 * that the address is naturally aligned.
1993 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1995 #if __ELF_WORD_SIZE == 64
1996 if (vptr % sizeof(uint64_t) != 0)
1998 if (vptr % sizeof(uint32_t) != 0)
2002 * We count the array size reading the aux vectors from the
2003 * stack until AT_NULL vector is returned. So (to keep the code
2004 * simple) we read the process stack twice: the first time here
2005 * to find the size and the second time when copying the vectors
2006 * to the allocated proc_vector.
2008 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
2009 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
2012 if (aux.a_type == AT_NULL)
2017 * If the PROC_AUXV_MAX entries are iterated over, and we have
2018 * not reached AT_NULL, it is most likely we are reading wrong
2019 * data: either the process doesn't have auxv array or data has
2020 * been modified. Return the error in this case.
2022 if (aux.a_type != AT_NULL)
2025 size = vsize * sizeof(aux);
2028 KASSERT(0, ("Wrong proc vector type: %d", type));
2029 return (EINVAL); /* In case we are built without INVARIANTS. */
2031 proc_vector = malloc(size, M_TEMP, M_WAITOK);
2032 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
2033 free(proc_vector, M_TEMP);
2036 *proc_vectorp = proc_vector;
2042 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
2045 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
2046 enum proc_vector_type type)
2048 size_t done, len, nchr, vsize;
2050 char **proc_vector, *sptr;
2051 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2053 PROC_ASSERT_HELD(p);
2056 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2058 nchr = 2 * (PATH_MAX + ARG_MAX);
2060 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2063 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2065 * The program may have scribbled into its argv array, e.g. to
2066 * remove some arguments. If that has happened, break out
2067 * before trying to read from NULL.
2069 if (proc_vector[i] == NULL)
2071 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2072 error = proc_read_string(td, p, sptr, pss_string,
2073 sizeof(pss_string));
2076 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2077 if (done + len >= nchr)
2078 len = nchr - done - 1;
2079 sbuf_bcat(sb, pss_string, len);
2080 if (len != GET_PS_STRINGS_CHUNK_SZ)
2082 done += GET_PS_STRINGS_CHUNK_SZ;
2084 sbuf_bcat(sb, "", 1);
2088 free(proc_vector, M_TEMP);
2093 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2096 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2100 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2103 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2107 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2113 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2115 #ifdef COMPAT_FREEBSD32
2116 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2117 size = vsize * sizeof(Elf32_Auxinfo);
2120 size = vsize * sizeof(Elf_Auxinfo);
2121 if (sbuf_bcat(sb, auxv, size) != 0)
2129 * This sysctl allows a process to retrieve the argument list or process
2130 * title for another process without groping around in the address space
2131 * of the other process. It also allow a process to set its own "process
2132 * title to a string of its own choice.
2135 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2137 int *name = (int *)arg1;
2138 u_int namelen = arg2;
2139 struct pargs *newpa, *pa;
2142 int flags, error = 0, error2;
2148 pid = (pid_t)name[0];
2150 * If the query is for this process and it is single-threaded, there
2151 * is nobody to modify pargs, thus we can just read.
2154 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2155 (pa = p->p_args) != NULL)
2156 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2158 flags = PGET_CANSEE;
2159 if (req->newptr != NULL)
2160 flags |= PGET_ISCURRENT;
2161 error = pget(pid, flags, &p);
2169 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2171 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2174 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2175 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2176 error = proc_getargv(curthread, p, &sb);
2177 error2 = sbuf_finish(&sb);
2180 if (error == 0 && error2 != 0)
2185 if (error != 0 || req->newptr == NULL)
2188 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2191 if (req->newlen == 0) {
2193 * Clear the argument pointer, so that we'll fetch arguments
2194 * with proc_getargv() until further notice.
2198 newpa = pargs_alloc(req->newlen);
2199 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2214 * This sysctl allows a process to retrieve environment of another process.
2217 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2219 int *name = (int *)arg1;
2220 u_int namelen = arg2;
2228 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2231 if ((p->p_flag & P_SYSTEM) != 0) {
2236 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2237 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2238 error = proc_getenvv(curthread, p, &sb);
2239 error2 = sbuf_finish(&sb);
2242 return (error != 0 ? error : error2);
2246 * This sysctl allows a process to retrieve ELF auxiliary vector of
2250 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2252 int *name = (int *)arg1;
2253 u_int namelen = arg2;
2261 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2264 if ((p->p_flag & P_SYSTEM) != 0) {
2268 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2269 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2270 error = proc_getauxv(curthread, p, &sb);
2271 error2 = sbuf_finish(&sb);
2274 return (error != 0 ? error : error2);
2278 * This sysctl allows a process to retrieve the path of the executable for
2279 * itself or another process.
2282 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2284 pid_t *pidp = (pid_t *)arg1;
2285 unsigned int arglen = arg2;
2288 char *retbuf, *freebuf;
2293 if (*pidp == -1) { /* -1 means this process */
2294 p = req->td->td_proc;
2296 error = pget(*pidp, PGET_CANSEE, &p);
2310 error = vn_fullpath(vp, &retbuf, &freebuf);
2314 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2315 free(freebuf, M_TEMP);
2320 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2333 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2336 sv_name = p->p_sysent->sv_name;
2338 return (sysctl_handle_string(oidp, sv_name, 0, req));
2341 #ifdef KINFO_OVMENTRY_SIZE
2342 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2345 #ifdef COMPAT_FREEBSD7
2347 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2349 vm_map_entry_t entry, tmp_entry;
2350 unsigned int last_timestamp;
2351 char *fullpath, *freepath;
2352 struct kinfo_ovmentry *kve;
2362 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2365 vm = vmspace_acquire_ref(p);
2370 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2373 vm_map_lock_read(map);
2374 VM_MAP_ENTRY_FOREACH(entry, map) {
2375 vm_object_t obj, tobj, lobj;
2378 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2381 bzero(kve, sizeof(*kve));
2382 kve->kve_structsize = sizeof(*kve);
2384 kve->kve_private_resident = 0;
2385 obj = entry->object.vm_object;
2387 VM_OBJECT_RLOCK(obj);
2388 if (obj->shadow_count == 1)
2389 kve->kve_private_resident =
2390 obj->resident_page_count;
2392 kve->kve_resident = 0;
2393 addr = entry->start;
2394 while (addr < entry->end) {
2395 if (pmap_extract(map->pmap, addr))
2396 kve->kve_resident++;
2400 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2402 VM_OBJECT_RLOCK(tobj);
2403 kve->kve_offset += tobj->backing_object_offset;
2406 VM_OBJECT_RUNLOCK(lobj);
2410 kve->kve_start = (void*)entry->start;
2411 kve->kve_end = (void*)entry->end;
2412 kve->kve_offset += (off_t)entry->offset;
2414 if (entry->protection & VM_PROT_READ)
2415 kve->kve_protection |= KVME_PROT_READ;
2416 if (entry->protection & VM_PROT_WRITE)
2417 kve->kve_protection |= KVME_PROT_WRITE;
2418 if (entry->protection & VM_PROT_EXECUTE)
2419 kve->kve_protection |= KVME_PROT_EXEC;
2421 if (entry->eflags & MAP_ENTRY_COW)
2422 kve->kve_flags |= KVME_FLAG_COW;
2423 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2424 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2425 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2426 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2428 last_timestamp = map->timestamp;
2429 vm_map_unlock_read(map);
2431 kve->kve_fileid = 0;
2436 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2437 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2438 kve->kve_type = KVME_TYPE_UNKNOWN;
2442 VM_OBJECT_RUNLOCK(lobj);
2444 kve->kve_ref_count = obj->ref_count;
2445 kve->kve_shadow_count = obj->shadow_count;
2446 VM_OBJECT_RUNLOCK(obj);
2448 vn_fullpath(vp, &fullpath, &freepath);
2449 cred = curthread->td_ucred;
2450 vn_lock(vp, LK_SHARED | LK_RETRY);
2451 if (VOP_GETATTR(vp, &va, cred) == 0) {
2452 kve->kve_fileid = va.va_fileid;
2454 kve->kve_fsid = va.va_fsid;
2459 kve->kve_type = KVME_TYPE_NONE;
2460 kve->kve_ref_count = 0;
2461 kve->kve_shadow_count = 0;
2464 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2465 if (freepath != NULL)
2466 free(freepath, M_TEMP);
2468 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2469 vm_map_lock_read(map);
2472 if (last_timestamp != map->timestamp) {
2473 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2477 vm_map_unlock_read(map);
2483 #endif /* COMPAT_FREEBSD7 */
2485 #ifdef KINFO_VMENTRY_SIZE
2486 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2490 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2491 int *resident_count, bool *super)
2493 vm_object_t obj, tobj;
2497 vm_pindex_t pi, pi_adv, pindex;
2500 *resident_count = 0;
2501 if (vmmap_skip_res_cnt)
2505 obj = entry->object.vm_object;
2506 addr = entry->start;
2508 pi = OFF_TO_IDX(entry->offset);
2509 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2510 if (m_adv != NULL) {
2513 pi_adv = atop(entry->end - addr);
2515 for (tobj = obj;; tobj = tobj->backing_object) {
2516 m = vm_page_find_least(tobj, pindex);
2518 if (m->pindex == pindex)
2520 if (pi_adv > m->pindex - pindex) {
2521 pi_adv = m->pindex - pindex;
2525 if (tobj->backing_object == NULL)
2527 pindex += OFF_TO_IDX(tobj->
2528 backing_object_offset);
2532 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2533 (addr & (pagesizes[1] - 1)) == 0 &&
2534 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2536 pi_adv = atop(pagesizes[1]);
2539 * We do not test the found page on validity.
2540 * Either the page is busy and being paged in,
2541 * or it was invalidated. The first case
2542 * should be counted as resident, the second
2543 * is not so clear; we do account both.
2547 *resident_count += pi_adv;
2553 * Must be called with the process locked and will return unlocked.
2556 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2558 vm_map_entry_t entry, tmp_entry;
2561 vm_object_t obj, tobj, lobj;
2562 char *fullpath, *freepath;
2563 struct kinfo_vmentry *kve;
2568 unsigned int last_timestamp;
2572 PROC_LOCK_ASSERT(p, MA_OWNED);
2576 vm = vmspace_acquire_ref(p);
2581 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2585 vm_map_lock_read(map);
2586 VM_MAP_ENTRY_FOREACH(entry, map) {
2587 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2591 bzero(kve, sizeof(*kve));
2592 obj = entry->object.vm_object;
2594 for (tobj = obj; tobj != NULL;
2595 tobj = tobj->backing_object) {
2596 VM_OBJECT_RLOCK(tobj);
2597 kve->kve_offset += tobj->backing_object_offset;
2600 if (obj->backing_object == NULL)
2601 kve->kve_private_resident =
2602 obj->resident_page_count;
2603 kern_proc_vmmap_resident(map, entry,
2604 &kve->kve_resident, &super);
2606 kve->kve_flags |= KVME_FLAG_SUPER;
2607 for (tobj = obj; tobj != NULL;
2608 tobj = tobj->backing_object) {
2609 if (tobj != obj && tobj != lobj)
2610 VM_OBJECT_RUNLOCK(tobj);
2616 kve->kve_start = entry->start;
2617 kve->kve_end = entry->end;
2618 kve->kve_offset += entry->offset;
2620 if (entry->protection & VM_PROT_READ)
2621 kve->kve_protection |= KVME_PROT_READ;
2622 if (entry->protection & VM_PROT_WRITE)
2623 kve->kve_protection |= KVME_PROT_WRITE;
2624 if (entry->protection & VM_PROT_EXECUTE)
2625 kve->kve_protection |= KVME_PROT_EXEC;
2627 if (entry->eflags & MAP_ENTRY_COW)
2628 kve->kve_flags |= KVME_FLAG_COW;
2629 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2630 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2631 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2632 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2633 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2634 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2635 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2636 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2637 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2638 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2640 last_timestamp = map->timestamp;
2641 vm_map_unlock_read(map);
2646 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2650 VM_OBJECT_RUNLOCK(lobj);
2652 kve->kve_ref_count = obj->ref_count;
2653 kve->kve_shadow_count = obj->shadow_count;
2654 VM_OBJECT_RUNLOCK(obj);
2656 vn_fullpath(vp, &fullpath, &freepath);
2657 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2658 cred = curthread->td_ucred;
2659 vn_lock(vp, LK_SHARED | LK_RETRY);
2660 if (VOP_GETATTR(vp, &va, cred) == 0) {
2661 kve->kve_vn_fileid = va.va_fileid;
2662 kve->kve_vn_fsid = va.va_fsid;
2663 kve->kve_vn_fsid_freebsd11 =
2664 kve->kve_vn_fsid; /* truncate */
2666 MAKEIMODE(va.va_type, va.va_mode);
2667 kve->kve_vn_size = va.va_size;
2668 kve->kve_vn_rdev = va.va_rdev;
2669 kve->kve_vn_rdev_freebsd11 =
2670 kve->kve_vn_rdev; /* truncate */
2671 kve->kve_status = KF_ATTR_VALID;
2676 kve->kve_type = KVME_TYPE_NONE;
2677 kve->kve_ref_count = 0;
2678 kve->kve_shadow_count = 0;
2681 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2682 if (freepath != NULL)
2683 free(freepath, M_TEMP);
2685 /* Pack record size down */
2686 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2687 kve->kve_structsize =
2688 offsetof(struct kinfo_vmentry, kve_path) +
2689 strlen(kve->kve_path) + 1;
2691 kve->kve_structsize = sizeof(*kve);
2692 kve->kve_structsize = roundup(kve->kve_structsize,
2695 /* Halt filling and truncate rather than exceeding maxlen */
2696 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2698 vm_map_lock_read(map);
2700 } else if (maxlen != -1)
2701 maxlen -= kve->kve_structsize;
2703 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2705 vm_map_lock_read(map);
2708 if (last_timestamp != map->timestamp) {
2709 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2713 vm_map_unlock_read(map);
2721 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2725 int error, error2, *name;
2728 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2729 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2730 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2735 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2736 error2 = sbuf_finish(&sb);
2738 return (error != 0 ? error : error2);
2741 #if defined(STACK) || defined(DDB)
2743 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2745 struct kinfo_kstack *kkstp;
2746 int error, i, *name, numthreads;
2747 lwpid_t *lwpidarray;
2754 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2758 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2759 st = stack_create(M_WAITOK);
2764 if (lwpidarray != NULL) {
2765 free(lwpidarray, M_TEMP);
2768 numthreads = p->p_numthreads;
2770 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2773 } while (numthreads < p->p_numthreads);
2776 * XXXRW: During the below loop, execve(2) and countless other sorts
2777 * of changes could have taken place. Should we check to see if the
2778 * vmspace has been replaced, or the like, in order to prevent
2779 * giving a snapshot that spans, say, execve(2), with some threads
2780 * before and some after? Among other things, the credentials could
2781 * have changed, in which case the right to extract debug info might
2782 * no longer be assured.
2785 FOREACH_THREAD_IN_PROC(p, td) {
2786 KASSERT(i < numthreads,
2787 ("sysctl_kern_proc_kstack: numthreads"));
2788 lwpidarray[i] = td->td_tid;
2793 for (i = 0; i < numthreads; i++) {
2794 td = tdfind(lwpidarray[i], p->p_pid);
2798 bzero(kkstp, sizeof(*kkstp));
2799 (void)sbuf_new(&sb, kkstp->kkst_trace,
2800 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2802 kkstp->kkst_tid = td->td_tid;
2803 if (TD_IS_SWAPPED(td))
2804 kkstp->kkst_state = KKST_STATE_SWAPPED;
2805 else if (stack_save_td(st, td) == 0)
2806 kkstp->kkst_state = KKST_STATE_STACKOK;
2808 kkstp->kkst_state = KKST_STATE_RUNNING;
2811 stack_sbuf_print(&sb, st);
2814 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2819 if (lwpidarray != NULL)
2820 free(lwpidarray, M_TEMP);
2822 free(kkstp, M_TEMP);
2828 * This sysctl allows a process to retrieve the full list of groups from
2829 * itself or another process.
2832 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2834 pid_t *pidp = (pid_t *)arg1;
2835 unsigned int arglen = arg2;
2842 if (*pidp == -1) { /* -1 means this process */
2843 p = req->td->td_proc;
2846 error = pget(*pidp, PGET_CANSEE, &p);
2851 cred = crhold(p->p_ucred);
2854 error = SYSCTL_OUT(req, cred->cr_groups,
2855 cred->cr_ngroups * sizeof(gid_t));
2861 * This sysctl allows a process to retrieve or/and set the resource limit for
2865 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2867 int *name = (int *)arg1;
2868 u_int namelen = arg2;
2877 which = (u_int)name[1];
2878 if (which >= RLIM_NLIMITS)
2881 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2884 flags = PGET_HOLD | PGET_NOTWEXIT;
2885 if (req->newptr != NULL)
2886 flags |= PGET_CANDEBUG;
2888 flags |= PGET_CANSEE;
2889 error = pget((pid_t)name[0], flags, &p);
2896 if (req->oldptr != NULL) {
2898 lim_rlimit_proc(p, which, &rlim);
2901 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2908 if (req->newptr != NULL) {
2909 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2911 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2920 * This sysctl allows a process to retrieve ps_strings structure location of
2924 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2926 int *name = (int *)arg1;
2927 u_int namelen = arg2;
2929 vm_offset_t ps_strings;
2931 #ifdef COMPAT_FREEBSD32
2932 uint32_t ps_strings32;
2938 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2941 #ifdef COMPAT_FREEBSD32
2942 if ((req->flags & SCTL_MASK32) != 0) {
2944 * We return 0 if the 32 bit emulation request is for a 64 bit
2947 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2948 PTROUT(p->p_sysent->sv_psstrings) : 0;
2950 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2954 ps_strings = p->p_sysent->sv_psstrings;
2956 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2961 * This sysctl allows a process to retrieve umask of another process.
2964 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2966 int *name = (int *)arg1;
2967 u_int namelen = arg2;
2976 pid = (pid_t)name[0];
2978 if (pid == p->p_pid || pid == 0) {
2979 fd_cmask = p->p_fd->fd_cmask;
2983 error = pget(pid, PGET_WANTREAD, &p);
2987 fd_cmask = p->p_fd->fd_cmask;
2990 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2995 * This sysctl allows a process to set and retrieve binary osreldate of
2999 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3001 int *name = (int *)arg1;
3002 u_int namelen = arg2;
3004 int flags, error, osrel;
3009 if (req->newptr != NULL && req->newlen != sizeof(osrel))
3012 flags = PGET_HOLD | PGET_NOTWEXIT;
3013 if (req->newptr != NULL)
3014 flags |= PGET_CANDEBUG;
3016 flags |= PGET_CANSEE;
3017 error = pget((pid_t)name[0], flags, &p);
3021 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3025 if (req->newptr != NULL) {
3026 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3041 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
3043 int *name = (int *)arg1;
3044 u_int namelen = arg2;
3046 struct kinfo_sigtramp kst;
3047 const struct sysentvec *sv;
3049 #ifdef COMPAT_FREEBSD32
3050 struct kinfo_sigtramp32 kst32;
3056 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3060 #ifdef COMPAT_FREEBSD32
3061 if ((req->flags & SCTL_MASK32) != 0) {
3062 bzero(&kst32, sizeof(kst32));
3063 if (SV_PROC_FLAG(p, SV_ILP32)) {
3064 if (sv->sv_sigcode_base != 0) {
3065 kst32.ksigtramp_start = sv->sv_sigcode_base;
3066 kst32.ksigtramp_end = sv->sv_sigcode_base +
3069 kst32.ksigtramp_start = sv->sv_psstrings -
3071 kst32.ksigtramp_end = sv->sv_psstrings;
3075 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3079 bzero(&kst, sizeof(kst));
3080 if (sv->sv_sigcode_base != 0) {
3081 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3082 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3085 kst.ksigtramp_start = (char *)sv->sv_psstrings -
3087 kst.ksigtramp_end = (char *)sv->sv_psstrings;
3090 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3095 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3097 int *name = (int *)arg1;
3098 u_int namelen = arg2;
3103 #ifdef COMPAT_FREEBSD32
3108 if (namelen != 1 || req->newptr != NULL)
3111 pid = (pid_t)name[0];
3112 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3117 #ifdef COMPAT_FREEBSD32
3118 if (SV_CURPROC_FLAG(SV_ILP32)) {
3119 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3125 if (pid <= PID_MAX) {
3126 td1 = FIRST_THREAD_IN_PROC(p);
3128 FOREACH_THREAD_IN_PROC(p, td1) {
3129 if (td1->td_tid == pid)
3138 * The access to the private thread flags. It is fine as far
3139 * as no out-of-thin-air values are read from td_pflags, and
3140 * usermode read of the td_sigblock_ptr is racy inherently,
3141 * since target process might have already changed it
3144 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3145 addr = (uintptr_t)td1->td_sigblock_ptr;
3155 #ifdef COMPAT_FREEBSD32
3156 if (SV_CURPROC_FLAG(SV_ILP32)) {
3158 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3161 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3165 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3168 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3169 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3170 "Return entire process table");
3172 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3173 sysctl_kern_proc, "Process table");
3175 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3176 sysctl_kern_proc, "Process table");
3178 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3179 sysctl_kern_proc, "Process table");
3181 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3182 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3184 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3185 sysctl_kern_proc, "Process table");
3187 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3188 sysctl_kern_proc, "Process table");
3190 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3191 sysctl_kern_proc, "Process table");
3193 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3194 sysctl_kern_proc, "Process table");
3196 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3197 sysctl_kern_proc, "Return process table, no threads");
3199 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3200 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3201 sysctl_kern_proc_args, "Process argument list");
3203 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3204 sysctl_kern_proc_env, "Process environment");
3206 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3207 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3209 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3210 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3212 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3213 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3214 "Process syscall vector name (ABI type)");
3216 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3217 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3219 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3220 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3222 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3223 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3225 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3226 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3228 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3229 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3231 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3232 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3234 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3235 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3237 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3238 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3240 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3241 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3242 "Return process table, no threads");
3244 #ifdef COMPAT_FREEBSD7
3245 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3246 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3249 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3250 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3252 #if defined(STACK) || defined(DDB)
3253 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3254 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3257 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3258 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3260 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3261 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3262 "Process resource limits");
3264 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3265 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3266 "Process ps_strings location");
3268 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3269 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3271 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3272 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3273 "Process binary osreldate");
3275 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3276 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3277 "Process signal trampoline location");
3279 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3280 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3281 "Thread sigfastblock address");
3286 * stop_all_proc() purpose is to stop all process which have usermode,
3287 * except current process for obvious reasons. This makes it somewhat
3288 * unreliable when invoked from multithreaded process. The service
3289 * must not be user-callable anyway.
3294 struct proc *cp, *p;
3296 bool restart, seen_stopped, seen_exiting, stopped_some;
3300 sx_xlock(&allproc_lock);
3302 seen_exiting = seen_stopped = stopped_some = restart = false;
3303 LIST_REMOVE(cp, p_list);
3304 LIST_INSERT_HEAD(&allproc, cp, p_list);
3306 p = LIST_NEXT(cp, p_list);
3309 LIST_REMOVE(cp, p_list);
3310 LIST_INSERT_AFTER(p, cp, p_list);
3312 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3316 if ((p->p_flag & P_WEXIT) != 0) {
3317 seen_exiting = true;
3321 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3323 * Stopped processes are tolerated when there
3324 * are no other processes which might continue
3325 * them. P_STOPPED_SINGLE but not
3326 * P_TOTAL_STOP process still has at least one
3329 seen_stopped = true;
3333 sx_xunlock(&allproc_lock);
3335 r = thread_single(p, SINGLE_ALLPROC);
3339 stopped_some = true;
3342 sx_xlock(&allproc_lock);
3344 /* Catch forked children we did not see in iteration. */
3345 if (gen != allproc_gen)
3347 sx_xunlock(&allproc_lock);
3348 if (restart || stopped_some || seen_exiting || seen_stopped) {
3349 kern_yield(PRI_USER);
3355 resume_all_proc(void)
3357 struct proc *cp, *p;
3360 sx_xlock(&allproc_lock);
3362 LIST_REMOVE(cp, p_list);
3363 LIST_INSERT_HEAD(&allproc, cp, p_list);
3365 p = LIST_NEXT(cp, p_list);
3368 LIST_REMOVE(cp, p_list);
3369 LIST_INSERT_AFTER(p, cp, p_list);
3371 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3372 sx_xunlock(&allproc_lock);
3374 thread_single_end(p, SINGLE_ALLPROC);
3377 sx_xlock(&allproc_lock);
3382 /* Did the loop above missed any stopped process ? */
3383 FOREACH_PROC_IN_SYSTEM(p) {
3384 /* No need for proc lock. */
3385 if ((p->p_flag & P_TOTAL_STOP) != 0)
3388 sx_xunlock(&allproc_lock);
3391 /* #define TOTAL_STOP_DEBUG 1 */
3392 #ifdef TOTAL_STOP_DEBUG
3393 volatile static int ap_resume;
3394 #include <sys/mount.h>
3397 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3403 error = sysctl_handle_int(oidp, &val, 0, req);
3404 if (error != 0 || req->newptr == NULL)
3409 while (ap_resume == 0)
3417 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3418 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3419 sysctl_debug_stop_all_proc, "I",