2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_ktrace.h"
39 #include "opt_kstack_pages.h"
40 #include "opt_stack.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bitstring.h>
46 #include <sys/eventhandler.h>
49 #include <sys/kernel.h>
50 #include <sys/limits.h>
52 #include <sys/loginclass.h>
53 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
58 #include <sys/ptrace.h>
59 #include <sys/refcount.h>
60 #include <sys/resourcevar.h>
61 #include <sys/rwlock.h>
63 #include <sys/sysent.h>
64 #include <sys/sched.h>
66 #include <sys/stack.h>
68 #include <sys/dtrace_bsd.h>
69 #include <sys/sysctl.h>
70 #include <sys/filedesc.h>
72 #include <sys/signalvar.h>
76 #include <sys/vnode.h>
84 #include <vm/vm_param.h>
85 #include <vm/vm_extern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
92 #include <fs/devfs/devfs.h>
94 #ifdef COMPAT_FREEBSD32
95 #include <compat/freebsd32/freebsd32.h>
96 #include <compat/freebsd32/freebsd32_util.h>
99 SDT_PROVIDER_DEFINE(proc);
101 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
102 MALLOC_DEFINE(M_SESSION, "session", "session header");
103 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
104 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
106 static void fixjobc_enterpgrp(struct proc *p, struct pgrp *pgrp);
107 static void doenterpgrp(struct proc *, struct pgrp *);
108 static void orphanpg(struct pgrp *pg);
109 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
110 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
111 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
113 static void pgadjustjobc(struct pgrp *pgrp, bool entering);
114 static void pgdelete(struct pgrp *);
115 static int proc_ctor(void *mem, int size, void *arg, int flags);
116 static void proc_dtor(void *mem, int size, void *arg);
117 static int proc_init(void *mem, int size, int flags);
118 static void proc_fini(void *mem, int size);
119 static void pargs_free(struct pargs *pa);
122 * Other process lists
124 struct pidhashhead *pidhashtbl;
125 struct sx *pidhashtbl_lock;
128 struct pgrphashhead *pgrphashtbl;
130 struct proclist allproc;
131 struct sx __exclusive_cache_line allproc_lock;
132 struct sx __exclusive_cache_line proctree_lock;
133 struct mtx __exclusive_cache_line ppeers_lock;
134 struct mtx __exclusive_cache_line procid_lock;
135 uma_zone_t proc_zone;
138 * The offset of various fields in struct proc and struct thread.
139 * These are used by kernel debuggers to enumerate kernel threads and
142 const int proc_off_p_pid = offsetof(struct proc, p_pid);
143 const int proc_off_p_comm = offsetof(struct proc, p_comm);
144 const int proc_off_p_list = offsetof(struct proc, p_list);
145 const int proc_off_p_threads = offsetof(struct proc, p_threads);
146 const int thread_off_td_tid = offsetof(struct thread, td_tid);
147 const int thread_off_td_name = offsetof(struct thread, td_name);
148 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
149 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
150 const int thread_off_td_plist = offsetof(struct thread, td_plist);
152 EVENTHANDLER_LIST_DEFINE(process_ctor);
153 EVENTHANDLER_LIST_DEFINE(process_dtor);
154 EVENTHANDLER_LIST_DEFINE(process_init);
155 EVENTHANDLER_LIST_DEFINE(process_fini);
156 EVENTHANDLER_LIST_DEFINE(process_exit);
157 EVENTHANDLER_LIST_DEFINE(process_fork);
158 EVENTHANDLER_LIST_DEFINE(process_exec);
160 int kstack_pages = KSTACK_PAGES;
161 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
162 "Kernel stack size in pages");
163 static int vmmap_skip_res_cnt = 0;
164 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
165 &vmmap_skip_res_cnt, 0,
166 "Skip calculation of the pages resident count in kern.proc.vmmap");
168 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
169 #ifdef COMPAT_FREEBSD32
170 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
174 * Initialize global process hashing structures.
181 sx_init(&allproc_lock, "allproc");
182 sx_init(&proctree_lock, "proctree");
183 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
184 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
186 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
187 pidhashlock = (pidhash + 1) / 64;
190 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
191 M_PROC, M_WAITOK | M_ZERO);
192 for (i = 0; i < pidhashlock + 1; i++)
193 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
194 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
195 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
196 proc_ctor, proc_dtor, proc_init, proc_fini,
197 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
202 * Prepare a proc for use.
205 proc_ctor(void *mem, int size, void *arg, int flags)
210 p = (struct proc *)mem;
212 kdtrace_proc_ctor(p);
214 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
215 td = FIRST_THREAD_IN_PROC(p);
217 /* Make sure all thread constructors are executed */
218 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
224 * Reclaim a proc after use.
227 proc_dtor(void *mem, int size, void *arg)
232 /* INVARIANTS checks go here */
233 p = (struct proc *)mem;
234 td = FIRST_THREAD_IN_PROC(p);
237 KASSERT((p->p_numthreads == 1),
238 ("bad number of threads in exiting process"));
239 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
241 /* Free all OSD associated to this thread. */
243 td_softdep_cleanup(td);
244 MPASS(td->td_su == NULL);
246 /* Make sure all thread destructors are executed */
247 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
249 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
251 kdtrace_proc_dtor(p);
253 if (p->p_ksi != NULL)
254 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
258 * Initialize type-stable parts of a proc (when newly created).
261 proc_init(void *mem, int size, int flags)
265 p = (struct proc *)mem;
266 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
267 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
268 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
269 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
270 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
271 cv_init(&p->p_pwait, "ppwait");
272 TAILQ_INIT(&p->p_threads); /* all threads in proc */
273 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
274 p->p_stats = pstats_alloc();
280 * UMA should ensure that this function is never called.
281 * Freeing a proc structure would violate type stability.
284 proc_fini(void *mem, int size)
289 p = (struct proc *)mem;
290 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
291 pstats_free(p->p_stats);
292 thread_free(FIRST_THREAD_IN_PROC(p));
293 mtx_destroy(&p->p_mtx);
294 if (p->p_ksi != NULL)
295 ksiginfo_free(p->p_ksi);
297 panic("proc reclaimed");
302 * PID space management.
304 * These bitmaps are used by fork_findpid.
306 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
307 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
308 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
309 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
311 static bitstr_t *proc_id_array[] = {
319 proc_id_set(int type, pid_t id)
322 KASSERT(type >= 0 && type < nitems(proc_id_array),
323 ("invalid type %d\n", type));
324 mtx_lock(&procid_lock);
325 KASSERT(bit_test(proc_id_array[type], id) == 0,
326 ("bit %d already set in %d\n", id, type));
327 bit_set(proc_id_array[type], id);
328 mtx_unlock(&procid_lock);
332 proc_id_set_cond(int type, pid_t id)
335 KASSERT(type >= 0 && type < nitems(proc_id_array),
336 ("invalid type %d\n", type));
337 if (bit_test(proc_id_array[type], id))
339 mtx_lock(&procid_lock);
340 bit_set(proc_id_array[type], id);
341 mtx_unlock(&procid_lock);
345 proc_id_clear(int type, pid_t id)
348 KASSERT(type >= 0 && type < nitems(proc_id_array),
349 ("invalid type %d\n", type));
350 mtx_lock(&procid_lock);
351 KASSERT(bit_test(proc_id_array[type], id) != 0,
352 ("bit %d not set in %d\n", id, type));
353 bit_clear(proc_id_array[type], id);
354 mtx_unlock(&procid_lock);
358 * Is p an inferior of the current process?
361 inferior(struct proc *p)
364 sx_assert(&proctree_lock, SX_LOCKED);
365 PROC_LOCK_ASSERT(p, MA_OWNED);
366 for (; p != curproc; p = proc_realparent(p)) {
374 * Shared lock all the pid hash lists.
377 pidhash_slockall(void)
381 for (i = 0; i < pidhashlock + 1; i++)
382 sx_slock(&pidhashtbl_lock[i]);
386 * Shared unlock all the pid hash lists.
389 pidhash_sunlockall(void)
393 for (i = 0; i < pidhashlock + 1; i++)
394 sx_sunlock(&pidhashtbl_lock[i]);
398 * Similar to pfind_any(), this function finds zombies.
401 pfind_any_locked(pid_t pid)
405 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
406 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
407 if (p->p_pid == pid) {
409 if (p->p_state == PRS_NEW) {
420 * Locate a process by number.
422 * By not returning processes in the PRS_NEW state, we allow callers to avoid
423 * testing for that condition to avoid dereferencing p_ucred, et al.
425 static __always_inline struct proc *
426 _pfind(pid_t pid, bool zombie)
431 if (p->p_pid == pid) {
435 sx_slock(PIDHASHLOCK(pid));
436 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
437 if (p->p_pid == pid) {
439 if (p->p_state == PRS_NEW ||
440 (!zombie && p->p_state == PRS_ZOMBIE)) {
447 sx_sunlock(PIDHASHLOCK(pid));
455 return (_pfind(pid, false));
459 * Same as pfind but allow zombies.
465 return (_pfind(pid, true));
469 * Locate a process group by number.
470 * The caller must hold proctree_lock.
477 sx_assert(&proctree_lock, SX_LOCKED);
479 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
480 if (pgrp->pg_id == pgid) {
489 * Locate process and do additional manipulations, depending on flags.
492 pget(pid_t pid, int flags, struct proc **pp)
499 if (p->p_pid == pid) {
503 if (pid <= PID_MAX) {
504 if ((flags & PGET_NOTWEXIT) == 0)
508 } else if ((flags & PGET_NOTID) == 0) {
509 td1 = tdfind(pid, -1);
515 if ((flags & PGET_CANSEE) != 0) {
516 error = p_cansee(curthread, p);
521 if ((flags & PGET_CANDEBUG) != 0) {
522 error = p_candebug(curthread, p);
526 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
530 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
534 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
536 * XXXRW: Not clear ESRCH is the right error during proc
542 if ((flags & PGET_HOLD) != 0) {
554 * Create a new process group.
555 * pgid must be equal to the pid of p.
556 * Begin a new session if required.
559 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
562 sx_assert(&proctree_lock, SX_XLOCKED);
564 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
565 KASSERT(p->p_pid == pgid,
566 ("enterpgrp: new pgrp and pid != pgid"));
567 KASSERT(pgfind(pgid) == NULL,
568 ("enterpgrp: pgrp with pgid exists"));
569 KASSERT(!SESS_LEADER(p),
570 ("enterpgrp: session leader attempted setpgrp"));
572 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
578 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
580 p->p_flag &= ~P_CONTROLT;
584 sess->s_sid = p->p_pid;
585 proc_id_set(PROC_ID_SESSION, p->p_pid);
586 refcount_init(&sess->s_count, 1);
587 sess->s_ttyvp = NULL;
588 sess->s_ttydp = NULL;
590 bcopy(p->p_session->s_login, sess->s_login,
591 sizeof(sess->s_login));
592 pgrp->pg_session = sess;
593 KASSERT(p == curproc,
594 ("enterpgrp: mksession and p != curproc"));
596 pgrp->pg_session = p->p_session;
597 sess_hold(pgrp->pg_session);
601 proc_id_set(PROC_ID_GROUP, p->p_pid);
602 LIST_INIT(&pgrp->pg_members);
605 * As we have an exclusive lock of proctree_lock,
606 * this should not deadlock.
608 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
610 SLIST_INIT(&pgrp->pg_sigiolst);
613 doenterpgrp(p, pgrp);
619 * Move p to an existing process group
622 enterthispgrp(struct proc *p, struct pgrp *pgrp)
625 sx_assert(&proctree_lock, SX_XLOCKED);
626 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
627 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
628 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
629 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
630 KASSERT(pgrp->pg_session == p->p_session,
631 ("%s: pgrp's session %p, p->p_session %p.\n",
635 KASSERT(pgrp != p->p_pgrp,
636 ("%s: p belongs to pgrp.", __func__));
638 doenterpgrp(p, pgrp);
644 * If true, any child of q which belongs to group pgrp, qualifies the
645 * process group pgrp as not orphaned.
648 isjobproc(struct proc *q, struct pgrp *pgrp)
650 sx_assert(&proctree_lock, SX_LOCKED);
651 return (q->p_pgrp != pgrp &&
652 q->p_pgrp->pg_session == pgrp->pg_session);
656 jobc_reaper(struct proc *p)
660 sx_assert(&proctree_lock, SX_LOCKED);
664 if (pp->p_reaper == pp ||
665 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
671 jobc_parent(struct proc *p)
675 sx_assert(&proctree_lock, SX_LOCKED);
677 pp = proc_realparent(p);
678 if (pp->p_pptr == NULL ||
679 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
681 return (jobc_reaper(pp));
686 check_pgrp_jobc(struct pgrp *pgrp)
691 sx_assert(&proctree_lock, SX_LOCKED);
692 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
696 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
697 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
700 if (isjobproc(jobc_parent(q), pgrp))
703 KASSERT(pgrp->pg_jobc == cnt, ("pgrp %d %p pg_jobc %d cnt %d",
704 pgrp->pg_id, pgrp, pgrp->pg_jobc, cnt));
710 * Move p to a process group
713 doenterpgrp(struct proc *p, struct pgrp *pgrp)
715 struct pgrp *savepgrp;
717 sx_assert(&proctree_lock, SX_XLOCKED);
718 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
719 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
720 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
721 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
723 savepgrp = p->p_pgrp;
726 check_pgrp_jobc(pgrp);
727 check_pgrp_jobc(savepgrp);
731 * Adjust eligibility of affected pgrps to participate in job control.
733 fixjobc_enterpgrp(p, pgrp);
738 LIST_REMOVE(p, p_pglist);
741 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
742 PGRP_UNLOCK(savepgrp);
744 if (LIST_EMPTY(&savepgrp->pg_members))
749 * remove process from process group
752 leavepgrp(struct proc *p)
754 struct pgrp *savepgrp;
756 sx_assert(&proctree_lock, SX_XLOCKED);
757 savepgrp = p->p_pgrp;
760 LIST_REMOVE(p, p_pglist);
763 PGRP_UNLOCK(savepgrp);
764 if (LIST_EMPTY(&savepgrp->pg_members))
770 * delete a process group
773 pgdelete(struct pgrp *pgrp)
775 struct session *savesess;
778 sx_assert(&proctree_lock, SX_XLOCKED);
779 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
780 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
783 * Reset any sigio structures pointing to us as a result of
784 * F_SETOWN with our pgid. The proctree lock ensures that
785 * new sigio structures will not be added after this point.
787 funsetownlst(&pgrp->pg_sigiolst);
790 tp = pgrp->pg_session->s_ttyp;
791 LIST_REMOVE(pgrp, pg_hash);
792 savesess = pgrp->pg_session;
795 /* Remove the reference to the pgrp before deallocating it. */
798 tty_rel_pgrp(tp, pgrp);
801 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
802 mtx_destroy(&pgrp->pg_mtx);
804 sess_release(savesess);
808 pgadjustjobc(struct pgrp *pgrp, bool entering)
813 MPASS(pgrp->pg_jobc >= 0);
816 MPASS(pgrp->pg_jobc > 0);
818 if (pgrp->pg_jobc == 0)
825 fixjobc_enterpgrp_q(struct pgrp *pgrp, struct proc *p, struct proc *q, bool adj)
827 struct pgrp *childpgrp;
830 sx_assert(&proctree_lock, SX_LOCKED);
832 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
834 childpgrp = q->p_pgrp;
835 future_jobc = childpgrp != pgrp &&
836 childpgrp->pg_session == pgrp->pg_session;
838 if ((adj && !isjobproc(p, childpgrp) && future_jobc) ||
839 (!adj && isjobproc(p, childpgrp) && !future_jobc))
840 pgadjustjobc(childpgrp, adj);
844 * Adjust pgrp jobc counters when specified process changes process group.
845 * We count the number of processes in each process group that "qualify"
846 * the group for terminal job control (those with a parent in a different
847 * process group of the same session). If that count reaches zero, the
848 * process group becomes orphaned. Check both the specified process'
849 * process group and that of its children.
850 * We increment eligibility counts before decrementing, otherwise we
851 * could reach 0 spuriously during the decrement.
854 fixjobc_enterpgrp(struct proc *p, struct pgrp *pgrp)
858 sx_assert(&proctree_lock, SX_LOCKED);
859 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
860 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
861 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
863 if (p->p_pgrp == pgrp)
866 if (isjobproc(jobc_parent(p), pgrp))
867 pgadjustjobc(pgrp, true);
868 LIST_FOREACH(q, &p->p_children, p_sibling) {
869 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
871 fixjobc_enterpgrp_q(pgrp, p, q, true);
873 LIST_FOREACH(q, &p->p_orphans, p_orphan)
874 fixjobc_enterpgrp_q(pgrp, p, q, true);
876 if (isjobproc(jobc_parent(p), p->p_pgrp))
877 pgadjustjobc(p->p_pgrp, false);
878 LIST_FOREACH(q, &p->p_children, p_sibling) {
879 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
881 fixjobc_enterpgrp_q(pgrp, p, q, false);
883 LIST_FOREACH(q, &p->p_orphans, p_orphan)
884 fixjobc_enterpgrp_q(pgrp, p, q, false);
888 fixjobc_kill_q(struct proc *p, struct proc *q, bool adj)
890 struct pgrp *childpgrp;
892 sx_assert(&proctree_lock, SX_LOCKED);
894 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
896 childpgrp = q->p_pgrp;
898 if ((adj && isjobproc(jobc_reaper(q), childpgrp) &&
899 !isjobproc(p, childpgrp)) || (!adj && !isjobproc(jobc_reaper(q),
900 childpgrp) && isjobproc(p, childpgrp)))
901 pgadjustjobc(childpgrp, adj);
905 fixjobc_kill(struct proc *p)
910 sx_assert(&proctree_lock, SX_LOCKED);
911 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
913 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
914 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
916 check_pgrp_jobc(pgrp);
920 * p no longer affects process group orphanage for children.
921 * It is marked by the flag because p is only physically
922 * removed from its process group on wait(2).
924 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
925 p->p_treeflag |= P_TREE_GRPEXITED;
928 * Check p's parent to see whether p qualifies its own process
929 * group; if so, adjust count for p's process group.
931 if (isjobproc(jobc_parent(p), pgrp))
932 pgadjustjobc(pgrp, false);
935 * Check this process' children to see whether they qualify
936 * their process groups after reparenting to reaper. If so,
937 * adjust counts for children's process groups.
939 LIST_FOREACH(q, &p->p_children, p_sibling) {
940 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
942 fixjobc_kill_q(p, q, true);
944 LIST_FOREACH(q, &p->p_orphans, p_orphan)
945 fixjobc_kill_q(p, q, true);
946 LIST_FOREACH(q, &p->p_children, p_sibling) {
947 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
949 fixjobc_kill_q(p, q, false);
951 LIST_FOREACH(q, &p->p_orphans, p_orphan)
952 fixjobc_kill_q(p, q, false);
955 check_pgrp_jobc(pgrp);
968 MPASS(p->p_flag & P_WEXIT);
969 sx_assert(&proctree_lock, SX_LOCKED);
971 if (SESS_LEADER(p)) {
975 * s_ttyp is not zero'd; we use this to indicate that
976 * the session once had a controlling terminal. (for
977 * logging and informational purposes)
988 * Signal foreground pgrp and revoke access to
989 * controlling terminal if it has not been revoked
992 * Because the TTY may have been revoked in the mean
993 * time and could already have a new session associated
994 * with it, make sure we don't send a SIGHUP to a
995 * foreground process group that does not belong to this
1001 if (tp->t_session == sp)
1002 tty_signal_pgrp(tp, SIGHUP);
1006 if (ttyvp != NULL) {
1007 sx_xunlock(&proctree_lock);
1008 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
1009 VOP_REVOKE(ttyvp, REVOKEALL);
1012 devfs_ctty_unref(ttyvp);
1013 sx_xlock(&proctree_lock);
1020 * A process group has become orphaned;
1021 * if there are any stopped processes in the group,
1022 * hang-up all process in that group.
1025 orphanpg(struct pgrp *pg)
1029 PGRP_LOCK_ASSERT(pg, MA_OWNED);
1031 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1033 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
1035 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1037 kern_psignal(p, SIGHUP);
1038 kern_psignal(p, SIGCONT);
1048 sess_hold(struct session *s)
1051 refcount_acquire(&s->s_count);
1055 sess_release(struct session *s)
1058 if (refcount_release(&s->s_count)) {
1059 if (s->s_ttyp != NULL) {
1060 tty_lock(s->s_ttyp);
1061 tty_rel_sess(s->s_ttyp, s);
1063 proc_id_clear(PROC_ID_SESSION, s->s_sid);
1064 mtx_destroy(&s->s_mtx);
1072 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
1075 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
1076 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
1077 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
1078 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
1081 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1087 for (i = 0; i <= pgrphash; i++) {
1088 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1089 db_printf("indx %d\n", i);
1090 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1092 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1093 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1094 pgrp->pg_session->s_count,
1095 LIST_FIRST(&pgrp->pg_members));
1096 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1097 db_print_pgrp_one(pgrp, p);
1105 * Calculate the kinfo_proc members which contain process-wide
1107 * Must be called with the target process locked.
1110 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1114 PROC_LOCK_ASSERT(p, MA_OWNED);
1118 FOREACH_THREAD_IN_PROC(p, td) {
1120 kp->ki_pctcpu += sched_pctcpu(td);
1121 kp->ki_estcpu += sched_estcpu(td);
1127 * Clear kinfo_proc and fill in any information that is common
1128 * to all threads in the process.
1129 * Must be called with the target process locked.
1132 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1139 struct timeval boottime;
1141 PROC_LOCK_ASSERT(p, MA_OWNED);
1142 bzero(kp, sizeof(*kp));
1144 kp->ki_structsize = sizeof(*kp);
1146 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1147 kp->ki_args = p->p_args;
1148 kp->ki_textvp = p->p_textvp;
1150 kp->ki_tracep = p->p_tracevp;
1151 kp->ki_traceflag = p->p_traceflag;
1153 kp->ki_fd = p->p_fd;
1154 kp->ki_pd = p->p_pd;
1155 kp->ki_vmspace = p->p_vmspace;
1156 kp->ki_flag = p->p_flag;
1157 kp->ki_flag2 = p->p_flag2;
1160 kp->ki_uid = cred->cr_uid;
1161 kp->ki_ruid = cred->cr_ruid;
1162 kp->ki_svuid = cred->cr_svuid;
1163 kp->ki_cr_flags = 0;
1164 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1165 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1166 /* XXX bde doesn't like KI_NGROUPS */
1167 if (cred->cr_ngroups > KI_NGROUPS) {
1168 kp->ki_ngroups = KI_NGROUPS;
1169 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1171 kp->ki_ngroups = cred->cr_ngroups;
1172 bcopy(cred->cr_groups, kp->ki_groups,
1173 kp->ki_ngroups * sizeof(gid_t));
1174 kp->ki_rgid = cred->cr_rgid;
1175 kp->ki_svgid = cred->cr_svgid;
1176 /* If jailed(cred), emulate the old P_JAILED flag. */
1178 kp->ki_flag |= P_JAILED;
1179 /* If inside the jail, use 0 as a jail ID. */
1180 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1181 kp->ki_jid = cred->cr_prison->pr_id;
1183 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1184 sizeof(kp->ki_loginclass));
1188 mtx_lock(&ps->ps_mtx);
1189 kp->ki_sigignore = ps->ps_sigignore;
1190 kp->ki_sigcatch = ps->ps_sigcatch;
1191 mtx_unlock(&ps->ps_mtx);
1193 if (p->p_state != PRS_NEW &&
1194 p->p_state != PRS_ZOMBIE &&
1195 p->p_vmspace != NULL) {
1196 struct vmspace *vm = p->p_vmspace;
1198 kp->ki_size = vm->vm_map.size;
1199 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1200 FOREACH_THREAD_IN_PROC(p, td0) {
1201 if (!TD_IS_SWAPPED(td0))
1202 kp->ki_rssize += td0->td_kstack_pages;
1204 kp->ki_swrss = vm->vm_swrss;
1205 kp->ki_tsize = vm->vm_tsize;
1206 kp->ki_dsize = vm->vm_dsize;
1207 kp->ki_ssize = vm->vm_ssize;
1208 } else if (p->p_state == PRS_ZOMBIE)
1209 kp->ki_stat = SZOMB;
1210 if (kp->ki_flag & P_INMEM)
1211 kp->ki_sflag = PS_INMEM;
1214 /* Calculate legacy swtime as seconds since 'swtick'. */
1215 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1216 kp->ki_pid = p->p_pid;
1217 kp->ki_nice = p->p_nice;
1218 kp->ki_fibnum = p->p_fibnum;
1219 kp->ki_start = p->p_stats->p_start;
1220 getboottime(&boottime);
1221 timevaladd(&kp->ki_start, &boottime);
1223 rufetch(p, &kp->ki_rusage);
1224 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1225 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1227 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1228 /* Some callers want child times in a single value. */
1229 kp->ki_childtime = kp->ki_childstime;
1230 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1232 FOREACH_THREAD_IN_PROC(p, td0)
1233 kp->ki_cow += td0->td_cow;
1237 kp->ki_pgid = p->p_pgrp->pg_id;
1238 kp->ki_jobc = p->p_pgrp->pg_jobc;
1239 sp = p->p_pgrp->pg_session;
1242 kp->ki_sid = sp->s_sid;
1244 strlcpy(kp->ki_login, sp->s_login,
1245 sizeof(kp->ki_login));
1247 kp->ki_kiflag |= KI_CTTY;
1249 kp->ki_kiflag |= KI_SLEADER;
1250 /* XXX proctree_lock */
1255 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1256 kp->ki_tdev = tty_udev(tp);
1257 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1258 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1260 kp->ki_tsid = tp->t_session->s_sid;
1262 kp->ki_tdev = NODEV;
1263 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1265 if (p->p_comm[0] != '\0')
1266 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1267 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1268 p->p_sysent->sv_name[0] != '\0')
1269 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1270 kp->ki_siglist = p->p_siglist;
1271 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1272 kp->ki_acflag = p->p_acflag;
1273 kp->ki_lock = p->p_lock;
1275 kp->ki_ppid = p->p_oppid;
1276 if (p->p_flag & P_TRACED)
1277 kp->ki_tracer = p->p_pptr->p_pid;
1282 * Fill in information that is thread specific. Must be called with
1283 * target process locked. If 'preferthread' is set, overwrite certain
1284 * process-related fields that are maintained for both threads and
1288 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1294 PROC_LOCK_ASSERT(p, MA_OWNED);
1299 if (td->td_wmesg != NULL)
1300 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1302 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1303 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1304 sizeof(kp->ki_tdname)) {
1305 strlcpy(kp->ki_moretdname,
1306 td->td_name + sizeof(kp->ki_tdname) - 1,
1307 sizeof(kp->ki_moretdname));
1309 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1311 if (TD_ON_LOCK(td)) {
1312 kp->ki_kiflag |= KI_LOCKBLOCK;
1313 strlcpy(kp->ki_lockname, td->td_lockname,
1314 sizeof(kp->ki_lockname));
1316 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1317 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1320 if (p->p_state == PRS_NORMAL) { /* approximate. */
1321 if (TD_ON_RUNQ(td) ||
1323 TD_IS_RUNNING(td)) {
1325 } else if (P_SHOULDSTOP(p)) {
1326 kp->ki_stat = SSTOP;
1327 } else if (TD_IS_SLEEPING(td)) {
1328 kp->ki_stat = SSLEEP;
1329 } else if (TD_ON_LOCK(td)) {
1330 kp->ki_stat = SLOCK;
1332 kp->ki_stat = SWAIT;
1334 } else if (p->p_state == PRS_ZOMBIE) {
1335 kp->ki_stat = SZOMB;
1340 /* Things in the thread */
1341 kp->ki_wchan = td->td_wchan;
1342 kp->ki_pri.pri_level = td->td_priority;
1343 kp->ki_pri.pri_native = td->td_base_pri;
1346 * Note: legacy fields; clamp at the old NOCPU value and/or
1347 * the maximum u_char CPU value.
1349 if (td->td_lastcpu == NOCPU)
1350 kp->ki_lastcpu_old = NOCPU_OLD;
1351 else if (td->td_lastcpu > MAXCPU_OLD)
1352 kp->ki_lastcpu_old = MAXCPU_OLD;
1354 kp->ki_lastcpu_old = td->td_lastcpu;
1356 if (td->td_oncpu == NOCPU)
1357 kp->ki_oncpu_old = NOCPU_OLD;
1358 else if (td->td_oncpu > MAXCPU_OLD)
1359 kp->ki_oncpu_old = MAXCPU_OLD;
1361 kp->ki_oncpu_old = td->td_oncpu;
1363 kp->ki_lastcpu = td->td_lastcpu;
1364 kp->ki_oncpu = td->td_oncpu;
1365 kp->ki_tdflags = td->td_flags;
1366 kp->ki_tid = td->td_tid;
1367 kp->ki_numthreads = p->p_numthreads;
1368 kp->ki_pcb = td->td_pcb;
1369 kp->ki_kstack = (void *)td->td_kstack;
1370 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1371 kp->ki_pri.pri_class = td->td_pri_class;
1372 kp->ki_pri.pri_user = td->td_user_pri;
1375 rufetchtd(td, &kp->ki_rusage);
1376 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1377 kp->ki_pctcpu = sched_pctcpu(td);
1378 kp->ki_estcpu = sched_estcpu(td);
1379 kp->ki_cow = td->td_cow;
1382 /* We can't get this anymore but ps etc never used it anyway. */
1386 kp->ki_siglist = td->td_siglist;
1387 kp->ki_sigmask = td->td_sigmask;
1394 * Fill in a kinfo_proc structure for the specified process.
1395 * Must be called with the target process locked.
1398 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1401 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1403 fill_kinfo_proc_only(p, kp);
1404 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1405 fill_kinfo_aggregate(p, kp);
1412 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1416 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1419 pstats_fork(struct pstats *src, struct pstats *dst)
1422 bzero(&dst->pstat_startzero,
1423 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1424 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1425 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1429 pstats_free(struct pstats *ps)
1432 free(ps, M_SUBPROC);
1435 #ifdef COMPAT_FREEBSD32
1438 * This function is typically used to copy out the kernel address, so
1439 * it can be replaced by assignment of zero.
1441 static inline uint32_t
1442 ptr32_trim(const void *ptr)
1446 uptr = (uintptr_t)ptr;
1447 return ((uptr > UINT_MAX) ? 0 : uptr);
1450 #define PTRTRIM_CP(src,dst,fld) \
1451 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1454 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1458 bzero(ki32, sizeof(struct kinfo_proc32));
1459 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1460 CP(*ki, *ki32, ki_layout);
1461 PTRTRIM_CP(*ki, *ki32, ki_args);
1462 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1463 PTRTRIM_CP(*ki, *ki32, ki_addr);
1464 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1465 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1466 PTRTRIM_CP(*ki, *ki32, ki_fd);
1467 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1468 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1469 CP(*ki, *ki32, ki_pid);
1470 CP(*ki, *ki32, ki_ppid);
1471 CP(*ki, *ki32, ki_pgid);
1472 CP(*ki, *ki32, ki_tpgid);
1473 CP(*ki, *ki32, ki_sid);
1474 CP(*ki, *ki32, ki_tsid);
1475 CP(*ki, *ki32, ki_jobc);
1476 CP(*ki, *ki32, ki_tdev);
1477 CP(*ki, *ki32, ki_tdev_freebsd11);
1478 CP(*ki, *ki32, ki_siglist);
1479 CP(*ki, *ki32, ki_sigmask);
1480 CP(*ki, *ki32, ki_sigignore);
1481 CP(*ki, *ki32, ki_sigcatch);
1482 CP(*ki, *ki32, ki_uid);
1483 CP(*ki, *ki32, ki_ruid);
1484 CP(*ki, *ki32, ki_svuid);
1485 CP(*ki, *ki32, ki_rgid);
1486 CP(*ki, *ki32, ki_svgid);
1487 CP(*ki, *ki32, ki_ngroups);
1488 for (i = 0; i < KI_NGROUPS; i++)
1489 CP(*ki, *ki32, ki_groups[i]);
1490 CP(*ki, *ki32, ki_size);
1491 CP(*ki, *ki32, ki_rssize);
1492 CP(*ki, *ki32, ki_swrss);
1493 CP(*ki, *ki32, ki_tsize);
1494 CP(*ki, *ki32, ki_dsize);
1495 CP(*ki, *ki32, ki_ssize);
1496 CP(*ki, *ki32, ki_xstat);
1497 CP(*ki, *ki32, ki_acflag);
1498 CP(*ki, *ki32, ki_pctcpu);
1499 CP(*ki, *ki32, ki_estcpu);
1500 CP(*ki, *ki32, ki_slptime);
1501 CP(*ki, *ki32, ki_swtime);
1502 CP(*ki, *ki32, ki_cow);
1503 CP(*ki, *ki32, ki_runtime);
1504 TV_CP(*ki, *ki32, ki_start);
1505 TV_CP(*ki, *ki32, ki_childtime);
1506 CP(*ki, *ki32, ki_flag);
1507 CP(*ki, *ki32, ki_kiflag);
1508 CP(*ki, *ki32, ki_traceflag);
1509 CP(*ki, *ki32, ki_stat);
1510 CP(*ki, *ki32, ki_nice);
1511 CP(*ki, *ki32, ki_lock);
1512 CP(*ki, *ki32, ki_rqindex);
1513 CP(*ki, *ki32, ki_oncpu);
1514 CP(*ki, *ki32, ki_lastcpu);
1516 /* XXX TODO: wrap cpu value as appropriate */
1517 CP(*ki, *ki32, ki_oncpu_old);
1518 CP(*ki, *ki32, ki_lastcpu_old);
1520 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1521 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1522 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1523 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1524 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1525 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1526 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1527 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1528 CP(*ki, *ki32, ki_tracer);
1529 CP(*ki, *ki32, ki_flag2);
1530 CP(*ki, *ki32, ki_fibnum);
1531 CP(*ki, *ki32, ki_cr_flags);
1532 CP(*ki, *ki32, ki_jid);
1533 CP(*ki, *ki32, ki_numthreads);
1534 CP(*ki, *ki32, ki_tid);
1535 CP(*ki, *ki32, ki_pri);
1536 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1537 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1538 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1539 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1540 PTRTRIM_CP(*ki, *ki32, ki_udata);
1541 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1542 CP(*ki, *ki32, ki_sflag);
1543 CP(*ki, *ki32, ki_tdflags);
1548 kern_proc_out_size(struct proc *p, int flags)
1552 PROC_LOCK_ASSERT(p, MA_OWNED);
1554 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1555 #ifdef COMPAT_FREEBSD32
1556 if ((flags & KERN_PROC_MASK32) != 0) {
1557 size += sizeof(struct kinfo_proc32);
1560 size += sizeof(struct kinfo_proc);
1562 #ifdef COMPAT_FREEBSD32
1563 if ((flags & KERN_PROC_MASK32) != 0)
1564 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1567 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1574 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1577 struct kinfo_proc ki;
1578 #ifdef COMPAT_FREEBSD32
1579 struct kinfo_proc32 ki32;
1583 PROC_LOCK_ASSERT(p, MA_OWNED);
1584 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1587 fill_kinfo_proc(p, &ki);
1588 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1589 #ifdef COMPAT_FREEBSD32
1590 if ((flags & KERN_PROC_MASK32) != 0) {
1591 freebsd32_kinfo_proc_out(&ki, &ki32);
1592 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1596 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1599 FOREACH_THREAD_IN_PROC(p, td) {
1600 fill_kinfo_thread(td, &ki, 1);
1601 #ifdef COMPAT_FREEBSD32
1602 if ((flags & KERN_PROC_MASK32) != 0) {
1603 freebsd32_kinfo_proc_out(&ki, &ki32);
1604 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1608 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1619 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1622 struct kinfo_proc ki;
1625 if (req->oldptr == NULL)
1626 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1628 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1629 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1630 error = kern_proc_out(p, &sb, flags);
1631 error2 = sbuf_finish(&sb);
1635 else if (error2 != 0)
1641 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1646 for (i = 0; i < pidhashlock + 1; i++) {
1647 sx_slock(&pidhashtbl_lock[i]);
1648 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1649 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1650 if (p->p_state == PRS_NEW)
1652 error = cb(p, cbarg);
1653 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1655 sx_sunlock(&pidhashtbl_lock[i]);
1660 sx_sunlock(&pidhashtbl_lock[i]);
1665 struct kern_proc_out_args {
1666 struct sysctl_req *req;
1673 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1675 struct kern_proc_out_args *arg = origarg;
1676 int *name = arg->name;
1677 int oid_number = arg->oid_number;
1678 int flags = arg->flags;
1679 struct sysctl_req *req = arg->req;
1684 KASSERT(p->p_ucred != NULL,
1685 ("process credential is NULL for non-NEW proc"));
1687 * Show a user only appropriate processes.
1689 if (p_cansee(curthread, p))
1692 * TODO - make more efficient (see notes below).
1695 switch (oid_number) {
1697 if (p->p_ucred->cr_gid != (gid_t)name[0])
1701 case KERN_PROC_PGRP:
1702 /* could do this by traversing pgrp */
1703 if (p->p_pgrp == NULL ||
1704 p->p_pgrp->pg_id != (pid_t)name[0])
1708 case KERN_PROC_RGID:
1709 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1713 case KERN_PROC_SESSION:
1714 if (p->p_session == NULL ||
1715 p->p_session->s_sid != (pid_t)name[0])
1720 if ((p->p_flag & P_CONTROLT) == 0 ||
1721 p->p_session == NULL)
1723 /* XXX proctree_lock */
1724 SESS_LOCK(p->p_session);
1725 if (p->p_session->s_ttyp == NULL ||
1726 tty_udev(p->p_session->s_ttyp) !=
1728 SESS_UNLOCK(p->p_session);
1731 SESS_UNLOCK(p->p_session);
1735 if (p->p_ucred->cr_uid != (uid_t)name[0])
1739 case KERN_PROC_RUID:
1740 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1744 case KERN_PROC_PROC:
1750 error = sysctl_out_proc(p, req, flags);
1751 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1759 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1761 struct kern_proc_out_args iterarg;
1762 int *name = (int *)arg1;
1763 u_int namelen = arg2;
1765 int flags, oid_number;
1768 oid_number = oidp->oid_number;
1769 if (oid_number != KERN_PROC_ALL &&
1770 (oid_number & KERN_PROC_INC_THREAD) == 0)
1771 flags = KERN_PROC_NOTHREADS;
1774 oid_number &= ~KERN_PROC_INC_THREAD;
1776 #ifdef COMPAT_FREEBSD32
1777 if (req->flags & SCTL_MASK32)
1778 flags |= KERN_PROC_MASK32;
1780 if (oid_number == KERN_PROC_PID) {
1783 error = sysctl_wire_old_buffer(req, 0);
1786 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1788 error = sysctl_out_proc(p, req, flags);
1792 switch (oid_number) {
1797 case KERN_PROC_PROC:
1798 if (namelen != 0 && namelen != 1)
1807 if (req->oldptr == NULL) {
1808 /* overestimate by 5 procs */
1809 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1813 error = sysctl_wire_old_buffer(req, 0);
1817 iterarg.flags = flags;
1818 iterarg.oid_number = oid_number;
1820 iterarg.name = name;
1821 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1826 pargs_alloc(int len)
1830 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1832 refcount_init(&pa->ar_ref, 1);
1833 pa->ar_length = len;
1838 pargs_free(struct pargs *pa)
1845 pargs_hold(struct pargs *pa)
1850 refcount_acquire(&pa->ar_ref);
1854 pargs_drop(struct pargs *pa)
1859 if (refcount_release(&pa->ar_ref))
1864 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1870 * This may return a short read if the string is shorter than the chunk
1871 * and is aligned at the end of the page, and the following page is not
1874 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1880 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1882 enum proc_vector_type {
1888 #ifdef COMPAT_FREEBSD32
1890 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1891 size_t *vsizep, enum proc_vector_type type)
1893 struct freebsd32_ps_strings pss;
1895 vm_offset_t vptr, ptr;
1896 uint32_t *proc_vector32;
1902 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1903 sizeof(pss)) != sizeof(pss))
1907 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1908 vsize = pss.ps_nargvstr;
1909 if (vsize > ARG_MAX)
1911 size = vsize * sizeof(int32_t);
1914 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1915 vsize = pss.ps_nenvstr;
1916 if (vsize > ARG_MAX)
1918 size = vsize * sizeof(int32_t);
1921 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1922 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1925 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1926 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1929 if (aux.a_type == AT_NULL)
1933 if (aux.a_type != AT_NULL)
1936 size = vsize * sizeof(aux);
1939 KASSERT(0, ("Wrong proc vector type: %d", type));
1942 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1943 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1947 if (type == PROC_AUX) {
1948 *proc_vectorp = (char **)proc_vector32;
1952 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1953 for (i = 0; i < (int)vsize; i++)
1954 proc_vector[i] = PTRIN(proc_vector32[i]);
1955 *proc_vectorp = proc_vector;
1958 free(proc_vector32, M_TEMP);
1964 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1965 size_t *vsizep, enum proc_vector_type type)
1967 struct ps_strings pss;
1969 vm_offset_t vptr, ptr;
1974 #ifdef COMPAT_FREEBSD32
1975 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1976 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1978 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1979 sizeof(pss)) != sizeof(pss))
1983 vptr = (vm_offset_t)pss.ps_argvstr;
1984 vsize = pss.ps_nargvstr;
1985 if (vsize > ARG_MAX)
1987 size = vsize * sizeof(char *);
1990 vptr = (vm_offset_t)pss.ps_envstr;
1991 vsize = pss.ps_nenvstr;
1992 if (vsize > ARG_MAX)
1994 size = vsize * sizeof(char *);
1998 * The aux array is just above env array on the stack. Check
1999 * that the address is naturally aligned.
2001 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
2003 #if __ELF_WORD_SIZE == 64
2004 if (vptr % sizeof(uint64_t) != 0)
2006 if (vptr % sizeof(uint32_t) != 0)
2010 * We count the array size reading the aux vectors from the
2011 * stack until AT_NULL vector is returned. So (to keep the code
2012 * simple) we read the process stack twice: the first time here
2013 * to find the size and the second time when copying the vectors
2014 * to the allocated proc_vector.
2016 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
2017 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
2020 if (aux.a_type == AT_NULL)
2025 * If the PROC_AUXV_MAX entries are iterated over, and we have
2026 * not reached AT_NULL, it is most likely we are reading wrong
2027 * data: either the process doesn't have auxv array or data has
2028 * been modified. Return the error in this case.
2030 if (aux.a_type != AT_NULL)
2033 size = vsize * sizeof(aux);
2036 KASSERT(0, ("Wrong proc vector type: %d", type));
2037 return (EINVAL); /* In case we are built without INVARIANTS. */
2039 proc_vector = malloc(size, M_TEMP, M_WAITOK);
2040 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
2041 free(proc_vector, M_TEMP);
2044 *proc_vectorp = proc_vector;
2050 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
2053 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
2054 enum proc_vector_type type)
2056 size_t done, len, nchr, vsize;
2058 char **proc_vector, *sptr;
2059 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2061 PROC_ASSERT_HELD(p);
2064 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2066 nchr = 2 * (PATH_MAX + ARG_MAX);
2068 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2071 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2073 * The program may have scribbled into its argv array, e.g. to
2074 * remove some arguments. If that has happened, break out
2075 * before trying to read from NULL.
2077 if (proc_vector[i] == NULL)
2079 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2080 error = proc_read_string(td, p, sptr, pss_string,
2081 sizeof(pss_string));
2084 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2085 if (done + len >= nchr)
2086 len = nchr - done - 1;
2087 sbuf_bcat(sb, pss_string, len);
2088 if (len != GET_PS_STRINGS_CHUNK_SZ)
2090 done += GET_PS_STRINGS_CHUNK_SZ;
2092 sbuf_bcat(sb, "", 1);
2096 free(proc_vector, M_TEMP);
2101 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2104 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2108 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2111 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2115 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2121 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2123 #ifdef COMPAT_FREEBSD32
2124 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2125 size = vsize * sizeof(Elf32_Auxinfo);
2128 size = vsize * sizeof(Elf_Auxinfo);
2129 if (sbuf_bcat(sb, auxv, size) != 0)
2137 * This sysctl allows a process to retrieve the argument list or process
2138 * title for another process without groping around in the address space
2139 * of the other process. It also allow a process to set its own "process
2140 * title to a string of its own choice.
2143 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2145 int *name = (int *)arg1;
2146 u_int namelen = arg2;
2147 struct pargs *newpa, *pa;
2150 int flags, error = 0, error2;
2156 pid = (pid_t)name[0];
2158 * If the query is for this process and it is single-threaded, there
2159 * is nobody to modify pargs, thus we can just read.
2162 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2163 (pa = p->p_args) != NULL)
2164 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2166 flags = PGET_CANSEE;
2167 if (req->newptr != NULL)
2168 flags |= PGET_ISCURRENT;
2169 error = pget(pid, flags, &p);
2177 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2179 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2182 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2183 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2184 error = proc_getargv(curthread, p, &sb);
2185 error2 = sbuf_finish(&sb);
2188 if (error == 0 && error2 != 0)
2193 if (error != 0 || req->newptr == NULL)
2196 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2199 if (req->newlen == 0) {
2201 * Clear the argument pointer, so that we'll fetch arguments
2202 * with proc_getargv() until further notice.
2206 newpa = pargs_alloc(req->newlen);
2207 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2222 * This sysctl allows a process to retrieve environment of another process.
2225 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2227 int *name = (int *)arg1;
2228 u_int namelen = arg2;
2236 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2239 if ((p->p_flag & P_SYSTEM) != 0) {
2244 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2245 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2246 error = proc_getenvv(curthread, p, &sb);
2247 error2 = sbuf_finish(&sb);
2250 return (error != 0 ? error : error2);
2254 * This sysctl allows a process to retrieve ELF auxiliary vector of
2258 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2260 int *name = (int *)arg1;
2261 u_int namelen = arg2;
2269 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2272 if ((p->p_flag & P_SYSTEM) != 0) {
2276 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2277 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2278 error = proc_getauxv(curthread, p, &sb);
2279 error2 = sbuf_finish(&sb);
2282 return (error != 0 ? error : error2);
2286 * This sysctl allows a process to retrieve the path of the executable for
2287 * itself or another process.
2290 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2292 pid_t *pidp = (pid_t *)arg1;
2293 unsigned int arglen = arg2;
2296 char *retbuf, *freebuf;
2301 if (*pidp == -1) { /* -1 means this process */
2302 p = req->td->td_proc;
2304 error = pget(*pidp, PGET_CANSEE, &p);
2318 error = vn_fullpath(vp, &retbuf, &freebuf);
2322 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2323 free(freebuf, M_TEMP);
2328 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2341 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2344 sv_name = p->p_sysent->sv_name;
2346 return (sysctl_handle_string(oidp, sv_name, 0, req));
2349 #ifdef KINFO_OVMENTRY_SIZE
2350 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2353 #ifdef COMPAT_FREEBSD7
2355 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2357 vm_map_entry_t entry, tmp_entry;
2358 unsigned int last_timestamp;
2359 char *fullpath, *freepath;
2360 struct kinfo_ovmentry *kve;
2370 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2373 vm = vmspace_acquire_ref(p);
2378 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2381 vm_map_lock_read(map);
2382 VM_MAP_ENTRY_FOREACH(entry, map) {
2383 vm_object_t obj, tobj, lobj;
2386 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2389 bzero(kve, sizeof(*kve));
2390 kve->kve_structsize = sizeof(*kve);
2392 kve->kve_private_resident = 0;
2393 obj = entry->object.vm_object;
2395 VM_OBJECT_RLOCK(obj);
2396 if (obj->shadow_count == 1)
2397 kve->kve_private_resident =
2398 obj->resident_page_count;
2400 kve->kve_resident = 0;
2401 addr = entry->start;
2402 while (addr < entry->end) {
2403 if (pmap_extract(map->pmap, addr))
2404 kve->kve_resident++;
2408 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2410 VM_OBJECT_RLOCK(tobj);
2411 kve->kve_offset += tobj->backing_object_offset;
2414 VM_OBJECT_RUNLOCK(lobj);
2418 kve->kve_start = (void*)entry->start;
2419 kve->kve_end = (void*)entry->end;
2420 kve->kve_offset += (off_t)entry->offset;
2422 if (entry->protection & VM_PROT_READ)
2423 kve->kve_protection |= KVME_PROT_READ;
2424 if (entry->protection & VM_PROT_WRITE)
2425 kve->kve_protection |= KVME_PROT_WRITE;
2426 if (entry->protection & VM_PROT_EXECUTE)
2427 kve->kve_protection |= KVME_PROT_EXEC;
2429 if (entry->eflags & MAP_ENTRY_COW)
2430 kve->kve_flags |= KVME_FLAG_COW;
2431 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2432 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2433 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2434 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2436 last_timestamp = map->timestamp;
2437 vm_map_unlock_read(map);
2439 kve->kve_fileid = 0;
2444 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2445 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2446 kve->kve_type = KVME_TYPE_UNKNOWN;
2450 VM_OBJECT_RUNLOCK(lobj);
2452 kve->kve_ref_count = obj->ref_count;
2453 kve->kve_shadow_count = obj->shadow_count;
2454 VM_OBJECT_RUNLOCK(obj);
2456 vn_fullpath(vp, &fullpath, &freepath);
2457 cred = curthread->td_ucred;
2458 vn_lock(vp, LK_SHARED | LK_RETRY);
2459 if (VOP_GETATTR(vp, &va, cred) == 0) {
2460 kve->kve_fileid = va.va_fileid;
2462 kve->kve_fsid = va.va_fsid;
2467 kve->kve_type = KVME_TYPE_NONE;
2468 kve->kve_ref_count = 0;
2469 kve->kve_shadow_count = 0;
2472 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2473 if (freepath != NULL)
2474 free(freepath, M_TEMP);
2476 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2477 vm_map_lock_read(map);
2480 if (last_timestamp != map->timestamp) {
2481 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2485 vm_map_unlock_read(map);
2491 #endif /* COMPAT_FREEBSD7 */
2493 #ifdef KINFO_VMENTRY_SIZE
2494 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2498 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2499 int *resident_count, bool *super)
2501 vm_object_t obj, tobj;
2505 vm_pindex_t pi, pi_adv, pindex;
2508 *resident_count = 0;
2509 if (vmmap_skip_res_cnt)
2513 obj = entry->object.vm_object;
2514 addr = entry->start;
2516 pi = OFF_TO_IDX(entry->offset);
2517 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2518 if (m_adv != NULL) {
2521 pi_adv = atop(entry->end - addr);
2523 for (tobj = obj;; tobj = tobj->backing_object) {
2524 m = vm_page_find_least(tobj, pindex);
2526 if (m->pindex == pindex)
2528 if (pi_adv > m->pindex - pindex) {
2529 pi_adv = m->pindex - pindex;
2533 if (tobj->backing_object == NULL)
2535 pindex += OFF_TO_IDX(tobj->
2536 backing_object_offset);
2540 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2541 (addr & (pagesizes[1] - 1)) == 0 &&
2542 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2544 pi_adv = atop(pagesizes[1]);
2547 * We do not test the found page on validity.
2548 * Either the page is busy and being paged in,
2549 * or it was invalidated. The first case
2550 * should be counted as resident, the second
2551 * is not so clear; we do account both.
2555 *resident_count += pi_adv;
2561 * Must be called with the process locked and will return unlocked.
2564 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2566 vm_map_entry_t entry, tmp_entry;
2569 vm_object_t obj, tobj, lobj;
2570 char *fullpath, *freepath;
2571 struct kinfo_vmentry *kve;
2576 unsigned int last_timestamp;
2580 PROC_LOCK_ASSERT(p, MA_OWNED);
2584 vm = vmspace_acquire_ref(p);
2589 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2593 vm_map_lock_read(map);
2594 VM_MAP_ENTRY_FOREACH(entry, map) {
2595 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2599 bzero(kve, sizeof(*kve));
2600 obj = entry->object.vm_object;
2602 for (tobj = obj; tobj != NULL;
2603 tobj = tobj->backing_object) {
2604 VM_OBJECT_RLOCK(tobj);
2605 kve->kve_offset += tobj->backing_object_offset;
2608 if (obj->backing_object == NULL)
2609 kve->kve_private_resident =
2610 obj->resident_page_count;
2611 kern_proc_vmmap_resident(map, entry,
2612 &kve->kve_resident, &super);
2614 kve->kve_flags |= KVME_FLAG_SUPER;
2615 for (tobj = obj; tobj != NULL;
2616 tobj = tobj->backing_object) {
2617 if (tobj != obj && tobj != lobj)
2618 VM_OBJECT_RUNLOCK(tobj);
2624 kve->kve_start = entry->start;
2625 kve->kve_end = entry->end;
2626 kve->kve_offset += entry->offset;
2628 if (entry->protection & VM_PROT_READ)
2629 kve->kve_protection |= KVME_PROT_READ;
2630 if (entry->protection & VM_PROT_WRITE)
2631 kve->kve_protection |= KVME_PROT_WRITE;
2632 if (entry->protection & VM_PROT_EXECUTE)
2633 kve->kve_protection |= KVME_PROT_EXEC;
2635 if (entry->eflags & MAP_ENTRY_COW)
2636 kve->kve_flags |= KVME_FLAG_COW;
2637 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2638 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2639 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2640 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2641 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2642 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2643 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2644 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2645 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2646 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2648 last_timestamp = map->timestamp;
2649 vm_map_unlock_read(map);
2654 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2658 VM_OBJECT_RUNLOCK(lobj);
2660 kve->kve_ref_count = obj->ref_count;
2661 kve->kve_shadow_count = obj->shadow_count;
2662 VM_OBJECT_RUNLOCK(obj);
2664 vn_fullpath(vp, &fullpath, &freepath);
2665 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2666 cred = curthread->td_ucred;
2667 vn_lock(vp, LK_SHARED | LK_RETRY);
2668 if (VOP_GETATTR(vp, &va, cred) == 0) {
2669 kve->kve_vn_fileid = va.va_fileid;
2670 kve->kve_vn_fsid = va.va_fsid;
2671 kve->kve_vn_fsid_freebsd11 =
2672 kve->kve_vn_fsid; /* truncate */
2674 MAKEIMODE(va.va_type, va.va_mode);
2675 kve->kve_vn_size = va.va_size;
2676 kve->kve_vn_rdev = va.va_rdev;
2677 kve->kve_vn_rdev_freebsd11 =
2678 kve->kve_vn_rdev; /* truncate */
2679 kve->kve_status = KF_ATTR_VALID;
2684 kve->kve_type = KVME_TYPE_NONE;
2685 kve->kve_ref_count = 0;
2686 kve->kve_shadow_count = 0;
2689 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2690 if (freepath != NULL)
2691 free(freepath, M_TEMP);
2693 /* Pack record size down */
2694 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2695 kve->kve_structsize =
2696 offsetof(struct kinfo_vmentry, kve_path) +
2697 strlen(kve->kve_path) + 1;
2699 kve->kve_structsize = sizeof(*kve);
2700 kve->kve_structsize = roundup(kve->kve_structsize,
2703 /* Halt filling and truncate rather than exceeding maxlen */
2704 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2706 vm_map_lock_read(map);
2708 } else if (maxlen != -1)
2709 maxlen -= kve->kve_structsize;
2711 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2713 vm_map_lock_read(map);
2716 if (last_timestamp != map->timestamp) {
2717 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2721 vm_map_unlock_read(map);
2729 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2733 int error, error2, *name;
2736 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2737 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2738 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2743 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2744 error2 = sbuf_finish(&sb);
2746 return (error != 0 ? error : error2);
2749 #if defined(STACK) || defined(DDB)
2751 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2753 struct kinfo_kstack *kkstp;
2754 int error, i, *name, numthreads;
2755 lwpid_t *lwpidarray;
2762 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2766 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2767 st = stack_create(M_WAITOK);
2772 if (lwpidarray != NULL) {
2773 free(lwpidarray, M_TEMP);
2776 numthreads = p->p_numthreads;
2778 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2781 } while (numthreads < p->p_numthreads);
2784 * XXXRW: During the below loop, execve(2) and countless other sorts
2785 * of changes could have taken place. Should we check to see if the
2786 * vmspace has been replaced, or the like, in order to prevent
2787 * giving a snapshot that spans, say, execve(2), with some threads
2788 * before and some after? Among other things, the credentials could
2789 * have changed, in which case the right to extract debug info might
2790 * no longer be assured.
2793 FOREACH_THREAD_IN_PROC(p, td) {
2794 KASSERT(i < numthreads,
2795 ("sysctl_kern_proc_kstack: numthreads"));
2796 lwpidarray[i] = td->td_tid;
2801 for (i = 0; i < numthreads; i++) {
2802 td = tdfind(lwpidarray[i], p->p_pid);
2806 bzero(kkstp, sizeof(*kkstp));
2807 (void)sbuf_new(&sb, kkstp->kkst_trace,
2808 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2810 kkstp->kkst_tid = td->td_tid;
2811 if (TD_IS_SWAPPED(td))
2812 kkstp->kkst_state = KKST_STATE_SWAPPED;
2813 else if (stack_save_td(st, td) == 0)
2814 kkstp->kkst_state = KKST_STATE_STACKOK;
2816 kkstp->kkst_state = KKST_STATE_RUNNING;
2819 stack_sbuf_print(&sb, st);
2822 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2827 if (lwpidarray != NULL)
2828 free(lwpidarray, M_TEMP);
2830 free(kkstp, M_TEMP);
2836 * This sysctl allows a process to retrieve the full list of groups from
2837 * itself or another process.
2840 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2842 pid_t *pidp = (pid_t *)arg1;
2843 unsigned int arglen = arg2;
2850 if (*pidp == -1) { /* -1 means this process */
2851 p = req->td->td_proc;
2854 error = pget(*pidp, PGET_CANSEE, &p);
2859 cred = crhold(p->p_ucred);
2862 error = SYSCTL_OUT(req, cred->cr_groups,
2863 cred->cr_ngroups * sizeof(gid_t));
2869 * This sysctl allows a process to retrieve or/and set the resource limit for
2873 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2875 int *name = (int *)arg1;
2876 u_int namelen = arg2;
2885 which = (u_int)name[1];
2886 if (which >= RLIM_NLIMITS)
2889 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2892 flags = PGET_HOLD | PGET_NOTWEXIT;
2893 if (req->newptr != NULL)
2894 flags |= PGET_CANDEBUG;
2896 flags |= PGET_CANSEE;
2897 error = pget((pid_t)name[0], flags, &p);
2904 if (req->oldptr != NULL) {
2906 lim_rlimit_proc(p, which, &rlim);
2909 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2916 if (req->newptr != NULL) {
2917 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2919 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2928 * This sysctl allows a process to retrieve ps_strings structure location of
2932 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2934 int *name = (int *)arg1;
2935 u_int namelen = arg2;
2937 vm_offset_t ps_strings;
2939 #ifdef COMPAT_FREEBSD32
2940 uint32_t ps_strings32;
2946 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2949 #ifdef COMPAT_FREEBSD32
2950 if ((req->flags & SCTL_MASK32) != 0) {
2952 * We return 0 if the 32 bit emulation request is for a 64 bit
2955 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2956 PTROUT(p->p_sysent->sv_psstrings) : 0;
2958 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2962 ps_strings = p->p_sysent->sv_psstrings;
2964 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2969 * This sysctl allows a process to retrieve umask of another process.
2972 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2974 int *name = (int *)arg1;
2975 u_int namelen = arg2;
2984 pid = (pid_t)name[0];
2986 if (pid == p->p_pid || pid == 0) {
2987 cmask = p->p_pd->pd_cmask;
2991 error = pget(pid, PGET_WANTREAD, &p);
2995 cmask = p->p_pd->pd_cmask;
2998 error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
3003 * This sysctl allows a process to set and retrieve binary osreldate of
3007 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3009 int *name = (int *)arg1;
3010 u_int namelen = arg2;
3012 int flags, error, osrel;
3017 if (req->newptr != NULL && req->newlen != sizeof(osrel))
3020 flags = PGET_HOLD | PGET_NOTWEXIT;
3021 if (req->newptr != NULL)
3022 flags |= PGET_CANDEBUG;
3024 flags |= PGET_CANSEE;
3025 error = pget((pid_t)name[0], flags, &p);
3029 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3033 if (req->newptr != NULL) {
3034 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3049 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
3051 int *name = (int *)arg1;
3052 u_int namelen = arg2;
3054 struct kinfo_sigtramp kst;
3055 const struct sysentvec *sv;
3057 #ifdef COMPAT_FREEBSD32
3058 struct kinfo_sigtramp32 kst32;
3064 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3068 #ifdef COMPAT_FREEBSD32
3069 if ((req->flags & SCTL_MASK32) != 0) {
3070 bzero(&kst32, sizeof(kst32));
3071 if (SV_PROC_FLAG(p, SV_ILP32)) {
3072 if (sv->sv_sigcode_base != 0) {
3073 kst32.ksigtramp_start = sv->sv_sigcode_base;
3074 kst32.ksigtramp_end = sv->sv_sigcode_base +
3077 kst32.ksigtramp_start = sv->sv_psstrings -
3079 kst32.ksigtramp_end = sv->sv_psstrings;
3083 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3087 bzero(&kst, sizeof(kst));
3088 if (sv->sv_sigcode_base != 0) {
3089 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3090 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3093 kst.ksigtramp_start = (char *)sv->sv_psstrings -
3095 kst.ksigtramp_end = (char *)sv->sv_psstrings;
3098 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3103 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3105 int *name = (int *)arg1;
3106 u_int namelen = arg2;
3111 #ifdef COMPAT_FREEBSD32
3116 if (namelen != 1 || req->newptr != NULL)
3119 pid = (pid_t)name[0];
3120 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3125 #ifdef COMPAT_FREEBSD32
3126 if (SV_CURPROC_FLAG(SV_ILP32)) {
3127 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3133 if (pid <= PID_MAX) {
3134 td1 = FIRST_THREAD_IN_PROC(p);
3136 FOREACH_THREAD_IN_PROC(p, td1) {
3137 if (td1->td_tid == pid)
3146 * The access to the private thread flags. It is fine as far
3147 * as no out-of-thin-air values are read from td_pflags, and
3148 * usermode read of the td_sigblock_ptr is racy inherently,
3149 * since target process might have already changed it
3152 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3153 addr = (uintptr_t)td1->td_sigblock_ptr;
3163 #ifdef COMPAT_FREEBSD32
3164 if (SV_CURPROC_FLAG(SV_ILP32)) {
3166 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3169 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3173 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3176 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3177 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3178 "Return entire process table");
3180 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3181 sysctl_kern_proc, "Process table");
3183 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3184 sysctl_kern_proc, "Process table");
3186 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3187 sysctl_kern_proc, "Process table");
3189 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3190 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3192 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3193 sysctl_kern_proc, "Process table");
3195 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3196 sysctl_kern_proc, "Process table");
3198 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3199 sysctl_kern_proc, "Process table");
3201 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3202 sysctl_kern_proc, "Process table");
3204 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3205 sysctl_kern_proc, "Return process table, no threads");
3207 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3208 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3209 sysctl_kern_proc_args, "Process argument list");
3211 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3212 sysctl_kern_proc_env, "Process environment");
3214 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3215 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3217 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3218 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3220 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3221 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3222 "Process syscall vector name (ABI type)");
3224 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3225 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3227 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3228 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3230 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3231 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3233 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3234 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3236 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3237 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3239 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3240 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3242 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3243 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3245 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3246 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3248 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3249 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3250 "Return process table, no threads");
3252 #ifdef COMPAT_FREEBSD7
3253 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3254 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3257 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3258 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3260 #if defined(STACK) || defined(DDB)
3261 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3262 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3265 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3266 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3268 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3269 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3270 "Process resource limits");
3272 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3273 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3274 "Process ps_strings location");
3276 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3277 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3279 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3280 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3281 "Process binary osreldate");
3283 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3284 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3285 "Process signal trampoline location");
3287 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3288 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3289 "Thread sigfastblock address");
3294 * stop_all_proc() purpose is to stop all process which have usermode,
3295 * except current process for obvious reasons. This makes it somewhat
3296 * unreliable when invoked from multithreaded process. The service
3297 * must not be user-callable anyway.
3302 struct proc *cp, *p;
3304 bool restart, seen_stopped, seen_exiting, stopped_some;
3308 sx_xlock(&allproc_lock);
3310 seen_exiting = seen_stopped = stopped_some = restart = false;
3311 LIST_REMOVE(cp, p_list);
3312 LIST_INSERT_HEAD(&allproc, cp, p_list);
3314 p = LIST_NEXT(cp, p_list);
3317 LIST_REMOVE(cp, p_list);
3318 LIST_INSERT_AFTER(p, cp, p_list);
3320 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3324 if ((p->p_flag & P_WEXIT) != 0) {
3325 seen_exiting = true;
3329 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3331 * Stopped processes are tolerated when there
3332 * are no other processes which might continue
3333 * them. P_STOPPED_SINGLE but not
3334 * P_TOTAL_STOP process still has at least one
3337 seen_stopped = true;
3341 sx_xunlock(&allproc_lock);
3343 r = thread_single(p, SINGLE_ALLPROC);
3347 stopped_some = true;
3350 sx_xlock(&allproc_lock);
3352 /* Catch forked children we did not see in iteration. */
3353 if (gen != allproc_gen)
3355 sx_xunlock(&allproc_lock);
3356 if (restart || stopped_some || seen_exiting || seen_stopped) {
3357 kern_yield(PRI_USER);
3363 resume_all_proc(void)
3365 struct proc *cp, *p;
3368 sx_xlock(&allproc_lock);
3370 LIST_REMOVE(cp, p_list);
3371 LIST_INSERT_HEAD(&allproc, cp, p_list);
3373 p = LIST_NEXT(cp, p_list);
3376 LIST_REMOVE(cp, p_list);
3377 LIST_INSERT_AFTER(p, cp, p_list);
3379 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3380 sx_xunlock(&allproc_lock);
3382 thread_single_end(p, SINGLE_ALLPROC);
3385 sx_xlock(&allproc_lock);
3390 /* Did the loop above missed any stopped process ? */
3391 FOREACH_PROC_IN_SYSTEM(p) {
3392 /* No need for proc lock. */
3393 if ((p->p_flag & P_TOTAL_STOP) != 0)
3396 sx_xunlock(&allproc_lock);
3399 /* #define TOTAL_STOP_DEBUG 1 */
3400 #ifdef TOTAL_STOP_DEBUG
3401 volatile static int ap_resume;
3402 #include <sys/mount.h>
3405 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3411 error = sysctl_handle_int(oidp, &val, 0, req);
3412 if (error != 0 || req->newptr == NULL)
3417 while (ap_resume == 0)
3425 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3426 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3427 sysctl_debug_stop_all_proc, "I",