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_hash = offsetof(struct proc, p_hash);
146 const int proc_off_p_threads = offsetof(struct proc, p_threads);
147 const int thread_off_td_tid = offsetof(struct thread, td_tid);
148 const int thread_off_td_name = offsetof(struct thread, td_name);
149 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
150 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
151 const int thread_off_td_plist = offsetof(struct thread, td_plist);
153 EVENTHANDLER_LIST_DEFINE(process_ctor);
154 EVENTHANDLER_LIST_DEFINE(process_dtor);
155 EVENTHANDLER_LIST_DEFINE(process_init);
156 EVENTHANDLER_LIST_DEFINE(process_fini);
157 EVENTHANDLER_LIST_DEFINE(process_exit);
158 EVENTHANDLER_LIST_DEFINE(process_fork);
159 EVENTHANDLER_LIST_DEFINE(process_exec);
161 int kstack_pages = KSTACK_PAGES;
162 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
163 "Kernel stack size in pages");
164 static int vmmap_skip_res_cnt = 0;
165 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
166 &vmmap_skip_res_cnt, 0,
167 "Skip calculation of the pages resident count in kern.proc.vmmap");
169 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
170 #ifdef COMPAT_FREEBSD32
171 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
175 * Initialize global process hashing structures.
182 sx_init(&allproc_lock, "allproc");
183 sx_init(&proctree_lock, "proctree");
184 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
185 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
187 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
188 pidhashlock = (pidhash + 1) / 64;
191 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
192 M_PROC, M_WAITOK | M_ZERO);
193 for (i = 0; i < pidhashlock + 1; i++)
194 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
195 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
196 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
197 proc_ctor, proc_dtor, proc_init, proc_fini,
198 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
203 * Prepare a proc for use.
206 proc_ctor(void *mem, int size, void *arg, int flags)
211 p = (struct proc *)mem;
213 kdtrace_proc_ctor(p);
215 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
216 td = FIRST_THREAD_IN_PROC(p);
218 /* Make sure all thread constructors are executed */
219 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
225 * Reclaim a proc after use.
228 proc_dtor(void *mem, int size, void *arg)
233 /* INVARIANTS checks go here */
234 p = (struct proc *)mem;
235 td = FIRST_THREAD_IN_PROC(p);
238 KASSERT((p->p_numthreads == 1),
239 ("bad number of threads in exiting process"));
240 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
242 /* Free all OSD associated to this thread. */
244 td_softdep_cleanup(td);
245 MPASS(td->td_su == NULL);
247 /* Make sure all thread destructors are executed */
248 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
250 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
252 kdtrace_proc_dtor(p);
254 if (p->p_ksi != NULL)
255 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
259 * Initialize type-stable parts of a proc (when newly created).
262 proc_init(void *mem, int size, int flags)
266 p = (struct proc *)mem;
267 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
268 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
269 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
270 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
271 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
272 cv_init(&p->p_pwait, "ppwait");
273 TAILQ_INIT(&p->p_threads); /* all threads in proc */
274 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
275 p->p_stats = pstats_alloc();
281 * UMA should ensure that this function is never called.
282 * Freeing a proc structure would violate type stability.
285 proc_fini(void *mem, int size)
290 p = (struct proc *)mem;
291 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
292 pstats_free(p->p_stats);
293 thread_free(FIRST_THREAD_IN_PROC(p));
294 mtx_destroy(&p->p_mtx);
295 if (p->p_ksi != NULL)
296 ksiginfo_free(p->p_ksi);
298 panic("proc reclaimed");
303 * PID space management.
305 * These bitmaps are used by fork_findpid.
307 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
308 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
309 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
310 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
312 static bitstr_t *proc_id_array[] = {
320 proc_id_set(int type, pid_t id)
323 KASSERT(type >= 0 && type < nitems(proc_id_array),
324 ("invalid type %d\n", type));
325 mtx_lock(&procid_lock);
326 KASSERT(bit_test(proc_id_array[type], id) == 0,
327 ("bit %d already set in %d\n", id, type));
328 bit_set(proc_id_array[type], id);
329 mtx_unlock(&procid_lock);
333 proc_id_set_cond(int type, pid_t id)
336 KASSERT(type >= 0 && type < nitems(proc_id_array),
337 ("invalid type %d\n", type));
338 if (bit_test(proc_id_array[type], id))
340 mtx_lock(&procid_lock);
341 bit_set(proc_id_array[type], id);
342 mtx_unlock(&procid_lock);
346 proc_id_clear(int type, pid_t id)
349 KASSERT(type >= 0 && type < nitems(proc_id_array),
350 ("invalid type %d\n", type));
351 mtx_lock(&procid_lock);
352 KASSERT(bit_test(proc_id_array[type], id) != 0,
353 ("bit %d not set in %d\n", id, type));
354 bit_clear(proc_id_array[type], id);
355 mtx_unlock(&procid_lock);
359 * Is p an inferior of the current process?
362 inferior(struct proc *p)
365 sx_assert(&proctree_lock, SX_LOCKED);
366 PROC_LOCK_ASSERT(p, MA_OWNED);
367 for (; p != curproc; p = proc_realparent(p)) {
375 * Shared lock all the pid hash lists.
378 pidhash_slockall(void)
382 for (i = 0; i < pidhashlock + 1; i++)
383 sx_slock(&pidhashtbl_lock[i]);
387 * Shared unlock all the pid hash lists.
390 pidhash_sunlockall(void)
394 for (i = 0; i < pidhashlock + 1; i++)
395 sx_sunlock(&pidhashtbl_lock[i]);
399 * Similar to pfind_any(), this function finds zombies.
402 pfind_any_locked(pid_t pid)
406 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
407 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
408 if (p->p_pid == pid) {
410 if (p->p_state == PRS_NEW) {
421 * Locate a process by number.
423 * By not returning processes in the PRS_NEW state, we allow callers to avoid
424 * testing for that condition to avoid dereferencing p_ucred, et al.
426 static __always_inline struct proc *
427 _pfind(pid_t pid, bool zombie)
432 if (p->p_pid == pid) {
436 sx_slock(PIDHASHLOCK(pid));
437 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
438 if (p->p_pid == pid) {
440 if (p->p_state == PRS_NEW ||
441 (!zombie && p->p_state == PRS_ZOMBIE)) {
448 sx_sunlock(PIDHASHLOCK(pid));
456 return (_pfind(pid, false));
460 * Same as pfind but allow zombies.
466 return (_pfind(pid, true));
470 * Locate a process group by number.
471 * The caller must hold proctree_lock.
478 sx_assert(&proctree_lock, SX_LOCKED);
480 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
481 if (pgrp->pg_id == pgid) {
490 * Locate process and do additional manipulations, depending on flags.
493 pget(pid_t pid, int flags, struct proc **pp)
500 if (p->p_pid == pid) {
504 if (pid <= PID_MAX) {
505 if ((flags & PGET_NOTWEXIT) == 0)
509 } else if ((flags & PGET_NOTID) == 0) {
510 td1 = tdfind(pid, -1);
516 if ((flags & PGET_CANSEE) != 0) {
517 error = p_cansee(curthread, p);
522 if ((flags & PGET_CANDEBUG) != 0) {
523 error = p_candebug(curthread, p);
527 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
531 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
535 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
537 * XXXRW: Not clear ESRCH is the right error during proc
543 if ((flags & PGET_HOLD) != 0) {
555 * Create a new process group.
556 * pgid must be equal to the pid of p.
557 * Begin a new session if required.
560 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
563 sx_assert(&proctree_lock, SX_XLOCKED);
565 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
566 KASSERT(p->p_pid == pgid,
567 ("enterpgrp: new pgrp and pid != pgid"));
568 KASSERT(pgfind(pgid) == NULL,
569 ("enterpgrp: pgrp with pgid exists"));
570 KASSERT(!SESS_LEADER(p),
571 ("enterpgrp: session leader attempted setpgrp"));
573 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
579 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
581 p->p_flag &= ~P_CONTROLT;
585 sess->s_sid = p->p_pid;
586 proc_id_set(PROC_ID_SESSION, p->p_pid);
587 refcount_init(&sess->s_count, 1);
588 sess->s_ttyvp = NULL;
589 sess->s_ttydp = NULL;
591 bcopy(p->p_session->s_login, sess->s_login,
592 sizeof(sess->s_login));
593 pgrp->pg_session = sess;
594 KASSERT(p == curproc,
595 ("enterpgrp: mksession and p != curproc"));
597 pgrp->pg_session = p->p_session;
598 sess_hold(pgrp->pg_session);
602 proc_id_set(PROC_ID_GROUP, p->p_pid);
603 LIST_INIT(&pgrp->pg_members);
606 * As we have an exclusive lock of proctree_lock,
607 * this should not deadlock.
609 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
611 SLIST_INIT(&pgrp->pg_sigiolst);
614 doenterpgrp(p, pgrp);
620 * Move p to an existing process group
623 enterthispgrp(struct proc *p, struct pgrp *pgrp)
626 sx_assert(&proctree_lock, SX_XLOCKED);
627 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
628 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
629 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
630 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
631 KASSERT(pgrp->pg_session == p->p_session,
632 ("%s: pgrp's session %p, p->p_session %p.\n",
636 KASSERT(pgrp != p->p_pgrp,
637 ("%s: p belongs to pgrp.", __func__));
639 doenterpgrp(p, pgrp);
645 * If true, any child of q which belongs to group pgrp, qualifies the
646 * process group pgrp as not orphaned.
649 isjobproc(struct proc *q, struct pgrp *pgrp)
651 sx_assert(&proctree_lock, SX_LOCKED);
652 return (q->p_pgrp != pgrp &&
653 q->p_pgrp->pg_session == pgrp->pg_session);
657 jobc_reaper(struct proc *p)
661 sx_assert(&proctree_lock, SX_LOCKED);
665 if (pp->p_reaper == pp ||
666 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
672 jobc_parent(struct proc *p)
676 sx_assert(&proctree_lock, SX_LOCKED);
678 pp = proc_realparent(p);
679 if (pp->p_pptr == NULL ||
680 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
682 return (jobc_reaper(pp));
687 check_pgrp_jobc(struct pgrp *pgrp)
692 sx_assert(&proctree_lock, SX_LOCKED);
693 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
697 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
698 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
701 if (isjobproc(jobc_parent(q), pgrp))
704 KASSERT(pgrp->pg_jobc == cnt, ("pgrp %d %p pg_jobc %d cnt %d",
705 pgrp->pg_id, pgrp, pgrp->pg_jobc, cnt));
711 * Move p to a process group
714 doenterpgrp(struct proc *p, struct pgrp *pgrp)
716 struct pgrp *savepgrp;
718 sx_assert(&proctree_lock, SX_XLOCKED);
719 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
720 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
721 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
722 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
724 savepgrp = p->p_pgrp;
727 check_pgrp_jobc(pgrp);
728 check_pgrp_jobc(savepgrp);
732 * Adjust eligibility of affected pgrps to participate in job control.
734 fixjobc_enterpgrp(p, pgrp);
739 LIST_REMOVE(p, p_pglist);
742 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
743 PGRP_UNLOCK(savepgrp);
745 if (LIST_EMPTY(&savepgrp->pg_members))
750 * remove process from process group
753 leavepgrp(struct proc *p)
755 struct pgrp *savepgrp;
757 sx_assert(&proctree_lock, SX_XLOCKED);
758 savepgrp = p->p_pgrp;
761 LIST_REMOVE(p, p_pglist);
764 PGRP_UNLOCK(savepgrp);
765 if (LIST_EMPTY(&savepgrp->pg_members))
771 * delete a process group
774 pgdelete(struct pgrp *pgrp)
776 struct session *savesess;
779 sx_assert(&proctree_lock, SX_XLOCKED);
780 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
781 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
784 * Reset any sigio structures pointing to us as a result of
785 * F_SETOWN with our pgid. The proctree lock ensures that
786 * new sigio structures will not be added after this point.
788 funsetownlst(&pgrp->pg_sigiolst);
791 tp = pgrp->pg_session->s_ttyp;
792 LIST_REMOVE(pgrp, pg_hash);
793 savesess = pgrp->pg_session;
796 /* Remove the reference to the pgrp before deallocating it. */
799 tty_rel_pgrp(tp, pgrp);
802 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
803 mtx_destroy(&pgrp->pg_mtx);
805 sess_release(savesess);
809 pgadjustjobc(struct pgrp *pgrp, bool entering)
814 MPASS(pgrp->pg_jobc >= 0);
817 MPASS(pgrp->pg_jobc > 0);
819 if (pgrp->pg_jobc == 0)
826 fixjobc_enterpgrp_q(struct pgrp *pgrp, struct proc *p, struct proc *q, bool adj)
828 struct pgrp *childpgrp;
831 sx_assert(&proctree_lock, SX_LOCKED);
833 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
835 childpgrp = q->p_pgrp;
836 future_jobc = childpgrp != pgrp &&
837 childpgrp->pg_session == pgrp->pg_session;
839 if ((adj && !isjobproc(p, childpgrp) && future_jobc) ||
840 (!adj && isjobproc(p, childpgrp) && !future_jobc))
841 pgadjustjobc(childpgrp, adj);
845 * Adjust pgrp jobc counters when specified process changes process group.
846 * We count the number of processes in each process group that "qualify"
847 * the group for terminal job control (those with a parent in a different
848 * process group of the same session). If that count reaches zero, the
849 * process group becomes orphaned. Check both the specified process'
850 * process group and that of its children.
851 * We increment eligibility counts before decrementing, otherwise we
852 * could reach 0 spuriously during the decrement.
855 fixjobc_enterpgrp(struct proc *p, struct pgrp *pgrp)
859 sx_assert(&proctree_lock, SX_LOCKED);
860 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
861 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
862 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
864 if (p->p_pgrp == pgrp)
867 if (isjobproc(jobc_parent(p), pgrp))
868 pgadjustjobc(pgrp, true);
869 LIST_FOREACH(q, &p->p_children, p_sibling) {
870 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
872 fixjobc_enterpgrp_q(pgrp, p, q, true);
874 LIST_FOREACH(q, &p->p_orphans, p_orphan)
875 fixjobc_enterpgrp_q(pgrp, p, q, true);
877 if (isjobproc(jobc_parent(p), p->p_pgrp))
878 pgadjustjobc(p->p_pgrp, false);
879 LIST_FOREACH(q, &p->p_children, p_sibling) {
880 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
882 fixjobc_enterpgrp_q(pgrp, p, q, false);
884 LIST_FOREACH(q, &p->p_orphans, p_orphan)
885 fixjobc_enterpgrp_q(pgrp, p, q, false);
889 fixjobc_kill_q(struct proc *p, struct proc *q, bool adj)
891 struct pgrp *childpgrp;
893 sx_assert(&proctree_lock, SX_LOCKED);
895 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0)
897 childpgrp = q->p_pgrp;
899 if ((adj && isjobproc(jobc_reaper(q), childpgrp) &&
900 !isjobproc(p, childpgrp)) || (!adj && !isjobproc(jobc_reaper(q),
901 childpgrp) && isjobproc(p, childpgrp)))
902 pgadjustjobc(childpgrp, adj);
906 fixjobc_kill(struct proc *p)
911 sx_assert(&proctree_lock, SX_LOCKED);
912 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
914 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
915 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
917 check_pgrp_jobc(pgrp);
921 * p no longer affects process group orphanage for children.
922 * It is marked by the flag because p is only physically
923 * removed from its process group on wait(2).
925 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
926 p->p_treeflag |= P_TREE_GRPEXITED;
929 * Check p's parent to see whether p qualifies its own process
930 * group; if so, adjust count for p's process group.
932 if (isjobproc(jobc_parent(p), pgrp))
933 pgadjustjobc(pgrp, false);
936 * Check this process' children to see whether they qualify
937 * their process groups after reparenting to reaper. If so,
938 * adjust counts for children's process groups.
940 LIST_FOREACH(q, &p->p_children, p_sibling) {
941 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
943 fixjobc_kill_q(p, q, true);
945 LIST_FOREACH(q, &p->p_orphans, p_orphan)
946 fixjobc_kill_q(p, q, true);
947 LIST_FOREACH(q, &p->p_children, p_sibling) {
948 if ((q->p_treeflag & P_TREE_ORPHANED) != 0)
950 fixjobc_kill_q(p, q, false);
952 LIST_FOREACH(q, &p->p_orphans, p_orphan)
953 fixjobc_kill_q(p, q, false);
956 check_pgrp_jobc(pgrp);
969 MPASS(p->p_flag & P_WEXIT);
970 sx_assert(&proctree_lock, SX_LOCKED);
972 if (SESS_LEADER(p)) {
976 * s_ttyp is not zero'd; we use this to indicate that
977 * the session once had a controlling terminal. (for
978 * logging and informational purposes)
989 * Signal foreground pgrp and revoke access to
990 * controlling terminal if it has not been revoked
993 * Because the TTY may have been revoked in the mean
994 * time and could already have a new session associated
995 * with it, make sure we don't send a SIGHUP to a
996 * foreground process group that does not belong to this
1002 if (tp->t_session == sp)
1003 tty_signal_pgrp(tp, SIGHUP);
1007 if (ttyvp != NULL) {
1008 sx_xunlock(&proctree_lock);
1009 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
1010 VOP_REVOKE(ttyvp, REVOKEALL);
1013 devfs_ctty_unref(ttyvp);
1014 sx_xlock(&proctree_lock);
1021 * A process group has become orphaned;
1022 * if there are any stopped processes in the group,
1023 * hang-up all process in that group.
1026 orphanpg(struct pgrp *pg)
1030 PGRP_LOCK_ASSERT(pg, MA_OWNED);
1032 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1034 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
1036 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
1038 kern_psignal(p, SIGHUP);
1039 kern_psignal(p, SIGCONT);
1049 sess_hold(struct session *s)
1052 refcount_acquire(&s->s_count);
1056 sess_release(struct session *s)
1059 if (refcount_release(&s->s_count)) {
1060 if (s->s_ttyp != NULL) {
1061 tty_lock(s->s_ttyp);
1062 tty_rel_sess(s->s_ttyp, s);
1064 proc_id_clear(PROC_ID_SESSION, s->s_sid);
1065 mtx_destroy(&s->s_mtx);
1073 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
1076 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
1077 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
1078 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
1079 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
1082 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1088 for (i = 0; i <= pgrphash; i++) {
1089 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1090 db_printf("indx %d\n", i);
1091 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1093 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1094 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1095 pgrp->pg_session->s_count,
1096 LIST_FIRST(&pgrp->pg_members));
1097 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1098 db_print_pgrp_one(pgrp, p);
1106 * Calculate the kinfo_proc members which contain process-wide
1108 * Must be called with the target process locked.
1111 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1115 PROC_LOCK_ASSERT(p, MA_OWNED);
1119 FOREACH_THREAD_IN_PROC(p, td) {
1121 kp->ki_pctcpu += sched_pctcpu(td);
1122 kp->ki_estcpu += sched_estcpu(td);
1128 * Clear kinfo_proc and fill in any information that is common
1129 * to all threads in the process.
1130 * Must be called with the target process locked.
1133 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1140 struct timeval boottime;
1142 PROC_LOCK_ASSERT(p, MA_OWNED);
1143 bzero(kp, sizeof(*kp));
1145 kp->ki_structsize = sizeof(*kp);
1147 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1148 kp->ki_args = p->p_args;
1149 kp->ki_textvp = p->p_textvp;
1151 kp->ki_tracep = p->p_tracevp;
1152 kp->ki_traceflag = p->p_traceflag;
1154 kp->ki_fd = p->p_fd;
1155 kp->ki_pd = p->p_pd;
1156 kp->ki_vmspace = p->p_vmspace;
1157 kp->ki_flag = p->p_flag;
1158 kp->ki_flag2 = p->p_flag2;
1161 kp->ki_uid = cred->cr_uid;
1162 kp->ki_ruid = cred->cr_ruid;
1163 kp->ki_svuid = cred->cr_svuid;
1164 kp->ki_cr_flags = 0;
1165 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1166 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1167 /* XXX bde doesn't like KI_NGROUPS */
1168 if (cred->cr_ngroups > KI_NGROUPS) {
1169 kp->ki_ngroups = KI_NGROUPS;
1170 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1172 kp->ki_ngroups = cred->cr_ngroups;
1173 bcopy(cred->cr_groups, kp->ki_groups,
1174 kp->ki_ngroups * sizeof(gid_t));
1175 kp->ki_rgid = cred->cr_rgid;
1176 kp->ki_svgid = cred->cr_svgid;
1177 /* If jailed(cred), emulate the old P_JAILED flag. */
1179 kp->ki_flag |= P_JAILED;
1180 /* If inside the jail, use 0 as a jail ID. */
1181 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1182 kp->ki_jid = cred->cr_prison->pr_id;
1184 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1185 sizeof(kp->ki_loginclass));
1189 mtx_lock(&ps->ps_mtx);
1190 kp->ki_sigignore = ps->ps_sigignore;
1191 kp->ki_sigcatch = ps->ps_sigcatch;
1192 mtx_unlock(&ps->ps_mtx);
1194 if (p->p_state != PRS_NEW &&
1195 p->p_state != PRS_ZOMBIE &&
1196 p->p_vmspace != NULL) {
1197 struct vmspace *vm = p->p_vmspace;
1199 kp->ki_size = vm->vm_map.size;
1200 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1201 FOREACH_THREAD_IN_PROC(p, td0) {
1202 if (!TD_IS_SWAPPED(td0))
1203 kp->ki_rssize += td0->td_kstack_pages;
1205 kp->ki_swrss = vm->vm_swrss;
1206 kp->ki_tsize = vm->vm_tsize;
1207 kp->ki_dsize = vm->vm_dsize;
1208 kp->ki_ssize = vm->vm_ssize;
1209 } else if (p->p_state == PRS_ZOMBIE)
1210 kp->ki_stat = SZOMB;
1211 if (kp->ki_flag & P_INMEM)
1212 kp->ki_sflag = PS_INMEM;
1215 /* Calculate legacy swtime as seconds since 'swtick'. */
1216 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1217 kp->ki_pid = p->p_pid;
1218 kp->ki_nice = p->p_nice;
1219 kp->ki_fibnum = p->p_fibnum;
1220 kp->ki_start = p->p_stats->p_start;
1221 getboottime(&boottime);
1222 timevaladd(&kp->ki_start, &boottime);
1224 rufetch(p, &kp->ki_rusage);
1225 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1226 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1228 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1229 /* Some callers want child times in a single value. */
1230 kp->ki_childtime = kp->ki_childstime;
1231 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1233 FOREACH_THREAD_IN_PROC(p, td0)
1234 kp->ki_cow += td0->td_cow;
1238 kp->ki_pgid = p->p_pgrp->pg_id;
1239 kp->ki_jobc = p->p_pgrp->pg_jobc;
1240 sp = p->p_pgrp->pg_session;
1243 kp->ki_sid = sp->s_sid;
1245 strlcpy(kp->ki_login, sp->s_login,
1246 sizeof(kp->ki_login));
1248 kp->ki_kiflag |= KI_CTTY;
1250 kp->ki_kiflag |= KI_SLEADER;
1251 /* XXX proctree_lock */
1256 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1257 kp->ki_tdev = tty_udev(tp);
1258 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1259 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1261 kp->ki_tsid = tp->t_session->s_sid;
1263 kp->ki_tdev = NODEV;
1264 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1266 if (p->p_comm[0] != '\0')
1267 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1268 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1269 p->p_sysent->sv_name[0] != '\0')
1270 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1271 kp->ki_siglist = p->p_siglist;
1272 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1273 kp->ki_acflag = p->p_acflag;
1274 kp->ki_lock = p->p_lock;
1276 kp->ki_ppid = p->p_oppid;
1277 if (p->p_flag & P_TRACED)
1278 kp->ki_tracer = p->p_pptr->p_pid;
1283 * Fill in information that is thread specific. Must be called with
1284 * target process locked. If 'preferthread' is set, overwrite certain
1285 * process-related fields that are maintained for both threads and
1289 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1295 PROC_LOCK_ASSERT(p, MA_OWNED);
1300 if (td->td_wmesg != NULL)
1301 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1303 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1304 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1305 sizeof(kp->ki_tdname)) {
1306 strlcpy(kp->ki_moretdname,
1307 td->td_name + sizeof(kp->ki_tdname) - 1,
1308 sizeof(kp->ki_moretdname));
1310 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1312 if (TD_ON_LOCK(td)) {
1313 kp->ki_kiflag |= KI_LOCKBLOCK;
1314 strlcpy(kp->ki_lockname, td->td_lockname,
1315 sizeof(kp->ki_lockname));
1317 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1318 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1321 if (p->p_state == PRS_NORMAL) { /* approximate. */
1322 if (TD_ON_RUNQ(td) ||
1324 TD_IS_RUNNING(td)) {
1326 } else if (P_SHOULDSTOP(p)) {
1327 kp->ki_stat = SSTOP;
1328 } else if (TD_IS_SLEEPING(td)) {
1329 kp->ki_stat = SSLEEP;
1330 } else if (TD_ON_LOCK(td)) {
1331 kp->ki_stat = SLOCK;
1333 kp->ki_stat = SWAIT;
1335 } else if (p->p_state == PRS_ZOMBIE) {
1336 kp->ki_stat = SZOMB;
1341 /* Things in the thread */
1342 kp->ki_wchan = td->td_wchan;
1343 kp->ki_pri.pri_level = td->td_priority;
1344 kp->ki_pri.pri_native = td->td_base_pri;
1347 * Note: legacy fields; clamp at the old NOCPU value and/or
1348 * the maximum u_char CPU value.
1350 if (td->td_lastcpu == NOCPU)
1351 kp->ki_lastcpu_old = NOCPU_OLD;
1352 else if (td->td_lastcpu > MAXCPU_OLD)
1353 kp->ki_lastcpu_old = MAXCPU_OLD;
1355 kp->ki_lastcpu_old = td->td_lastcpu;
1357 if (td->td_oncpu == NOCPU)
1358 kp->ki_oncpu_old = NOCPU_OLD;
1359 else if (td->td_oncpu > MAXCPU_OLD)
1360 kp->ki_oncpu_old = MAXCPU_OLD;
1362 kp->ki_oncpu_old = td->td_oncpu;
1364 kp->ki_lastcpu = td->td_lastcpu;
1365 kp->ki_oncpu = td->td_oncpu;
1366 kp->ki_tdflags = td->td_flags;
1367 kp->ki_tid = td->td_tid;
1368 kp->ki_numthreads = p->p_numthreads;
1369 kp->ki_pcb = td->td_pcb;
1370 kp->ki_kstack = (void *)td->td_kstack;
1371 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1372 kp->ki_pri.pri_class = td->td_pri_class;
1373 kp->ki_pri.pri_user = td->td_user_pri;
1376 rufetchtd(td, &kp->ki_rusage);
1377 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1378 kp->ki_pctcpu = sched_pctcpu(td);
1379 kp->ki_estcpu = sched_estcpu(td);
1380 kp->ki_cow = td->td_cow;
1383 /* We can't get this anymore but ps etc never used it anyway. */
1387 kp->ki_siglist = td->td_siglist;
1388 kp->ki_sigmask = td->td_sigmask;
1395 * Fill in a kinfo_proc structure for the specified process.
1396 * Must be called with the target process locked.
1399 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1402 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1404 fill_kinfo_proc_only(p, kp);
1405 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1406 fill_kinfo_aggregate(p, kp);
1413 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1417 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1420 pstats_fork(struct pstats *src, struct pstats *dst)
1423 bzero(&dst->pstat_startzero,
1424 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1425 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1426 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1430 pstats_free(struct pstats *ps)
1433 free(ps, M_SUBPROC);
1436 #ifdef COMPAT_FREEBSD32
1439 * This function is typically used to copy out the kernel address, so
1440 * it can be replaced by assignment of zero.
1442 static inline uint32_t
1443 ptr32_trim(const void *ptr)
1447 uptr = (uintptr_t)ptr;
1448 return ((uptr > UINT_MAX) ? 0 : uptr);
1451 #define PTRTRIM_CP(src,dst,fld) \
1452 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1455 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1459 bzero(ki32, sizeof(struct kinfo_proc32));
1460 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1461 CP(*ki, *ki32, ki_layout);
1462 PTRTRIM_CP(*ki, *ki32, ki_args);
1463 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1464 PTRTRIM_CP(*ki, *ki32, ki_addr);
1465 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1466 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1467 PTRTRIM_CP(*ki, *ki32, ki_fd);
1468 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1469 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1470 CP(*ki, *ki32, ki_pid);
1471 CP(*ki, *ki32, ki_ppid);
1472 CP(*ki, *ki32, ki_pgid);
1473 CP(*ki, *ki32, ki_tpgid);
1474 CP(*ki, *ki32, ki_sid);
1475 CP(*ki, *ki32, ki_tsid);
1476 CP(*ki, *ki32, ki_jobc);
1477 CP(*ki, *ki32, ki_tdev);
1478 CP(*ki, *ki32, ki_tdev_freebsd11);
1479 CP(*ki, *ki32, ki_siglist);
1480 CP(*ki, *ki32, ki_sigmask);
1481 CP(*ki, *ki32, ki_sigignore);
1482 CP(*ki, *ki32, ki_sigcatch);
1483 CP(*ki, *ki32, ki_uid);
1484 CP(*ki, *ki32, ki_ruid);
1485 CP(*ki, *ki32, ki_svuid);
1486 CP(*ki, *ki32, ki_rgid);
1487 CP(*ki, *ki32, ki_svgid);
1488 CP(*ki, *ki32, ki_ngroups);
1489 for (i = 0; i < KI_NGROUPS; i++)
1490 CP(*ki, *ki32, ki_groups[i]);
1491 CP(*ki, *ki32, ki_size);
1492 CP(*ki, *ki32, ki_rssize);
1493 CP(*ki, *ki32, ki_swrss);
1494 CP(*ki, *ki32, ki_tsize);
1495 CP(*ki, *ki32, ki_dsize);
1496 CP(*ki, *ki32, ki_ssize);
1497 CP(*ki, *ki32, ki_xstat);
1498 CP(*ki, *ki32, ki_acflag);
1499 CP(*ki, *ki32, ki_pctcpu);
1500 CP(*ki, *ki32, ki_estcpu);
1501 CP(*ki, *ki32, ki_slptime);
1502 CP(*ki, *ki32, ki_swtime);
1503 CP(*ki, *ki32, ki_cow);
1504 CP(*ki, *ki32, ki_runtime);
1505 TV_CP(*ki, *ki32, ki_start);
1506 TV_CP(*ki, *ki32, ki_childtime);
1507 CP(*ki, *ki32, ki_flag);
1508 CP(*ki, *ki32, ki_kiflag);
1509 CP(*ki, *ki32, ki_traceflag);
1510 CP(*ki, *ki32, ki_stat);
1511 CP(*ki, *ki32, ki_nice);
1512 CP(*ki, *ki32, ki_lock);
1513 CP(*ki, *ki32, ki_rqindex);
1514 CP(*ki, *ki32, ki_oncpu);
1515 CP(*ki, *ki32, ki_lastcpu);
1517 /* XXX TODO: wrap cpu value as appropriate */
1518 CP(*ki, *ki32, ki_oncpu_old);
1519 CP(*ki, *ki32, ki_lastcpu_old);
1521 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1522 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1523 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1524 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1525 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1526 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1527 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1528 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1529 CP(*ki, *ki32, ki_tracer);
1530 CP(*ki, *ki32, ki_flag2);
1531 CP(*ki, *ki32, ki_fibnum);
1532 CP(*ki, *ki32, ki_cr_flags);
1533 CP(*ki, *ki32, ki_jid);
1534 CP(*ki, *ki32, ki_numthreads);
1535 CP(*ki, *ki32, ki_tid);
1536 CP(*ki, *ki32, ki_pri);
1537 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1538 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1539 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1540 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1541 PTRTRIM_CP(*ki, *ki32, ki_udata);
1542 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1543 CP(*ki, *ki32, ki_sflag);
1544 CP(*ki, *ki32, ki_tdflags);
1549 kern_proc_out_size(struct proc *p, int flags)
1553 PROC_LOCK_ASSERT(p, MA_OWNED);
1555 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1556 #ifdef COMPAT_FREEBSD32
1557 if ((flags & KERN_PROC_MASK32) != 0) {
1558 size += sizeof(struct kinfo_proc32);
1561 size += sizeof(struct kinfo_proc);
1563 #ifdef COMPAT_FREEBSD32
1564 if ((flags & KERN_PROC_MASK32) != 0)
1565 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1568 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1575 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1578 struct kinfo_proc ki;
1579 #ifdef COMPAT_FREEBSD32
1580 struct kinfo_proc32 ki32;
1584 PROC_LOCK_ASSERT(p, MA_OWNED);
1585 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1588 fill_kinfo_proc(p, &ki);
1589 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1590 #ifdef COMPAT_FREEBSD32
1591 if ((flags & KERN_PROC_MASK32) != 0) {
1592 freebsd32_kinfo_proc_out(&ki, &ki32);
1593 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1597 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1600 FOREACH_THREAD_IN_PROC(p, td) {
1601 fill_kinfo_thread(td, &ki, 1);
1602 #ifdef COMPAT_FREEBSD32
1603 if ((flags & KERN_PROC_MASK32) != 0) {
1604 freebsd32_kinfo_proc_out(&ki, &ki32);
1605 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1609 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1620 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1623 struct kinfo_proc ki;
1626 if (req->oldptr == NULL)
1627 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1629 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1630 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1631 error = kern_proc_out(p, &sb, flags);
1632 error2 = sbuf_finish(&sb);
1636 else if (error2 != 0)
1642 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1647 for (i = 0; i < pidhashlock + 1; i++) {
1648 sx_slock(&pidhashtbl_lock[i]);
1649 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1650 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1651 if (p->p_state == PRS_NEW)
1653 error = cb(p, cbarg);
1654 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1656 sx_sunlock(&pidhashtbl_lock[i]);
1661 sx_sunlock(&pidhashtbl_lock[i]);
1666 struct kern_proc_out_args {
1667 struct sysctl_req *req;
1674 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1676 struct kern_proc_out_args *arg = origarg;
1677 int *name = arg->name;
1678 int oid_number = arg->oid_number;
1679 int flags = arg->flags;
1680 struct sysctl_req *req = arg->req;
1685 KASSERT(p->p_ucred != NULL,
1686 ("process credential is NULL for non-NEW proc"));
1688 * Show a user only appropriate processes.
1690 if (p_cansee(curthread, p))
1693 * TODO - make more efficient (see notes below).
1696 switch (oid_number) {
1698 if (p->p_ucred->cr_gid != (gid_t)name[0])
1702 case KERN_PROC_PGRP:
1703 /* could do this by traversing pgrp */
1704 if (p->p_pgrp == NULL ||
1705 p->p_pgrp->pg_id != (pid_t)name[0])
1709 case KERN_PROC_RGID:
1710 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1714 case KERN_PROC_SESSION:
1715 if (p->p_session == NULL ||
1716 p->p_session->s_sid != (pid_t)name[0])
1721 if ((p->p_flag & P_CONTROLT) == 0 ||
1722 p->p_session == NULL)
1724 /* XXX proctree_lock */
1725 SESS_LOCK(p->p_session);
1726 if (p->p_session->s_ttyp == NULL ||
1727 tty_udev(p->p_session->s_ttyp) !=
1729 SESS_UNLOCK(p->p_session);
1732 SESS_UNLOCK(p->p_session);
1736 if (p->p_ucred->cr_uid != (uid_t)name[0])
1740 case KERN_PROC_RUID:
1741 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1745 case KERN_PROC_PROC:
1751 error = sysctl_out_proc(p, req, flags);
1752 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1760 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1762 struct kern_proc_out_args iterarg;
1763 int *name = (int *)arg1;
1764 u_int namelen = arg2;
1766 int flags, oid_number;
1769 oid_number = oidp->oid_number;
1770 if (oid_number != KERN_PROC_ALL &&
1771 (oid_number & KERN_PROC_INC_THREAD) == 0)
1772 flags = KERN_PROC_NOTHREADS;
1775 oid_number &= ~KERN_PROC_INC_THREAD;
1777 #ifdef COMPAT_FREEBSD32
1778 if (req->flags & SCTL_MASK32)
1779 flags |= KERN_PROC_MASK32;
1781 if (oid_number == KERN_PROC_PID) {
1784 error = sysctl_wire_old_buffer(req, 0);
1787 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1789 error = sysctl_out_proc(p, req, flags);
1793 switch (oid_number) {
1798 case KERN_PROC_PROC:
1799 if (namelen != 0 && namelen != 1)
1808 if (req->oldptr == NULL) {
1809 /* overestimate by 5 procs */
1810 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1814 error = sysctl_wire_old_buffer(req, 0);
1818 iterarg.flags = flags;
1819 iterarg.oid_number = oid_number;
1821 iterarg.name = name;
1822 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1827 pargs_alloc(int len)
1831 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1833 refcount_init(&pa->ar_ref, 1);
1834 pa->ar_length = len;
1839 pargs_free(struct pargs *pa)
1846 pargs_hold(struct pargs *pa)
1851 refcount_acquire(&pa->ar_ref);
1855 pargs_drop(struct pargs *pa)
1860 if (refcount_release(&pa->ar_ref))
1865 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1871 * This may return a short read if the string is shorter than the chunk
1872 * and is aligned at the end of the page, and the following page is not
1875 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1881 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1883 enum proc_vector_type {
1889 #ifdef COMPAT_FREEBSD32
1891 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1892 size_t *vsizep, enum proc_vector_type type)
1894 struct freebsd32_ps_strings pss;
1896 vm_offset_t vptr, ptr;
1897 uint32_t *proc_vector32;
1903 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1904 sizeof(pss)) != sizeof(pss))
1908 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1909 vsize = pss.ps_nargvstr;
1910 if (vsize > ARG_MAX)
1912 size = vsize * sizeof(int32_t);
1915 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1916 vsize = pss.ps_nenvstr;
1917 if (vsize > ARG_MAX)
1919 size = vsize * sizeof(int32_t);
1922 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1923 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1926 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1927 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1930 if (aux.a_type == AT_NULL)
1934 if (aux.a_type != AT_NULL)
1937 size = vsize * sizeof(aux);
1940 KASSERT(0, ("Wrong proc vector type: %d", type));
1943 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1944 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1948 if (type == PROC_AUX) {
1949 *proc_vectorp = (char **)proc_vector32;
1953 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1954 for (i = 0; i < (int)vsize; i++)
1955 proc_vector[i] = PTRIN(proc_vector32[i]);
1956 *proc_vectorp = proc_vector;
1959 free(proc_vector32, M_TEMP);
1965 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1966 size_t *vsizep, enum proc_vector_type type)
1968 struct ps_strings pss;
1970 vm_offset_t vptr, ptr;
1975 #ifdef COMPAT_FREEBSD32
1976 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1977 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1979 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1980 sizeof(pss)) != sizeof(pss))
1984 vptr = (vm_offset_t)pss.ps_argvstr;
1985 vsize = pss.ps_nargvstr;
1986 if (vsize > ARG_MAX)
1988 size = vsize * sizeof(char *);
1991 vptr = (vm_offset_t)pss.ps_envstr;
1992 vsize = pss.ps_nenvstr;
1993 if (vsize > ARG_MAX)
1995 size = vsize * sizeof(char *);
1999 * The aux array is just above env array on the stack. Check
2000 * that the address is naturally aligned.
2002 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
2004 #if __ELF_WORD_SIZE == 64
2005 if (vptr % sizeof(uint64_t) != 0)
2007 if (vptr % sizeof(uint32_t) != 0)
2011 * We count the array size reading the aux vectors from the
2012 * stack until AT_NULL vector is returned. So (to keep the code
2013 * simple) we read the process stack twice: the first time here
2014 * to find the size and the second time when copying the vectors
2015 * to the allocated proc_vector.
2017 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
2018 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
2021 if (aux.a_type == AT_NULL)
2026 * If the PROC_AUXV_MAX entries are iterated over, and we have
2027 * not reached AT_NULL, it is most likely we are reading wrong
2028 * data: either the process doesn't have auxv array or data has
2029 * been modified. Return the error in this case.
2031 if (aux.a_type != AT_NULL)
2034 size = vsize * sizeof(aux);
2037 KASSERT(0, ("Wrong proc vector type: %d", type));
2038 return (EINVAL); /* In case we are built without INVARIANTS. */
2040 proc_vector = malloc(size, M_TEMP, M_WAITOK);
2041 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
2042 free(proc_vector, M_TEMP);
2045 *proc_vectorp = proc_vector;
2051 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
2054 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
2055 enum proc_vector_type type)
2057 size_t done, len, nchr, vsize;
2059 char **proc_vector, *sptr;
2060 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2062 PROC_ASSERT_HELD(p);
2065 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2067 nchr = 2 * (PATH_MAX + ARG_MAX);
2069 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2072 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2074 * The program may have scribbled into its argv array, e.g. to
2075 * remove some arguments. If that has happened, break out
2076 * before trying to read from NULL.
2078 if (proc_vector[i] == NULL)
2080 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2081 error = proc_read_string(td, p, sptr, pss_string,
2082 sizeof(pss_string));
2085 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2086 if (done + len >= nchr)
2087 len = nchr - done - 1;
2088 sbuf_bcat(sb, pss_string, len);
2089 if (len != GET_PS_STRINGS_CHUNK_SZ)
2091 done += GET_PS_STRINGS_CHUNK_SZ;
2093 sbuf_bcat(sb, "", 1);
2097 free(proc_vector, M_TEMP);
2102 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2105 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2109 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2112 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2116 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2122 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2124 #ifdef COMPAT_FREEBSD32
2125 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2126 size = vsize * sizeof(Elf32_Auxinfo);
2129 size = vsize * sizeof(Elf_Auxinfo);
2130 if (sbuf_bcat(sb, auxv, size) != 0)
2138 * This sysctl allows a process to retrieve the argument list or process
2139 * title for another process without groping around in the address space
2140 * of the other process. It also allow a process to set its own "process
2141 * title to a string of its own choice.
2144 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2146 int *name = (int *)arg1;
2147 u_int namelen = arg2;
2148 struct pargs *newpa, *pa;
2151 int flags, error = 0, error2;
2157 pid = (pid_t)name[0];
2159 * If the query is for this process and it is single-threaded, there
2160 * is nobody to modify pargs, thus we can just read.
2163 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2164 (pa = p->p_args) != NULL)
2165 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2167 flags = PGET_CANSEE;
2168 if (req->newptr != NULL)
2169 flags |= PGET_ISCURRENT;
2170 error = pget(pid, flags, &p);
2178 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2180 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2183 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2184 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2185 error = proc_getargv(curthread, p, &sb);
2186 error2 = sbuf_finish(&sb);
2189 if (error == 0 && error2 != 0)
2194 if (error != 0 || req->newptr == NULL)
2197 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2200 if (req->newlen == 0) {
2202 * Clear the argument pointer, so that we'll fetch arguments
2203 * with proc_getargv() until further notice.
2207 newpa = pargs_alloc(req->newlen);
2208 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2223 * This sysctl allows a process to retrieve environment of another process.
2226 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2228 int *name = (int *)arg1;
2229 u_int namelen = arg2;
2237 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2240 if ((p->p_flag & P_SYSTEM) != 0) {
2245 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2246 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2247 error = proc_getenvv(curthread, p, &sb);
2248 error2 = sbuf_finish(&sb);
2251 return (error != 0 ? error : error2);
2255 * This sysctl allows a process to retrieve ELF auxiliary vector of
2259 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2261 int *name = (int *)arg1;
2262 u_int namelen = arg2;
2270 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2273 if ((p->p_flag & P_SYSTEM) != 0) {
2277 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2278 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2279 error = proc_getauxv(curthread, p, &sb);
2280 error2 = sbuf_finish(&sb);
2283 return (error != 0 ? error : error2);
2287 * This sysctl allows a process to retrieve the path of the executable for
2288 * itself or another process.
2291 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2293 pid_t *pidp = (pid_t *)arg1;
2294 unsigned int arglen = arg2;
2297 char *retbuf, *freebuf;
2302 if (*pidp == -1) { /* -1 means this process */
2303 p = req->td->td_proc;
2305 error = pget(*pidp, PGET_CANSEE, &p);
2319 error = vn_fullpath(vp, &retbuf, &freebuf);
2323 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2324 free(freebuf, M_TEMP);
2329 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2342 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2345 sv_name = p->p_sysent->sv_name;
2347 return (sysctl_handle_string(oidp, sv_name, 0, req));
2350 #ifdef KINFO_OVMENTRY_SIZE
2351 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2354 #ifdef COMPAT_FREEBSD7
2356 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2358 vm_map_entry_t entry, tmp_entry;
2359 unsigned int last_timestamp;
2360 char *fullpath, *freepath;
2361 struct kinfo_ovmentry *kve;
2371 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2374 vm = vmspace_acquire_ref(p);
2379 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2382 vm_map_lock_read(map);
2383 VM_MAP_ENTRY_FOREACH(entry, map) {
2384 vm_object_t obj, tobj, lobj;
2387 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2390 bzero(kve, sizeof(*kve));
2391 kve->kve_structsize = sizeof(*kve);
2393 kve->kve_private_resident = 0;
2394 obj = entry->object.vm_object;
2396 VM_OBJECT_RLOCK(obj);
2397 if (obj->shadow_count == 1)
2398 kve->kve_private_resident =
2399 obj->resident_page_count;
2401 kve->kve_resident = 0;
2402 addr = entry->start;
2403 while (addr < entry->end) {
2404 if (pmap_extract(map->pmap, addr))
2405 kve->kve_resident++;
2409 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2411 VM_OBJECT_RLOCK(tobj);
2412 kve->kve_offset += tobj->backing_object_offset;
2415 VM_OBJECT_RUNLOCK(lobj);
2419 kve->kve_start = (void*)entry->start;
2420 kve->kve_end = (void*)entry->end;
2421 kve->kve_offset += (off_t)entry->offset;
2423 if (entry->protection & VM_PROT_READ)
2424 kve->kve_protection |= KVME_PROT_READ;
2425 if (entry->protection & VM_PROT_WRITE)
2426 kve->kve_protection |= KVME_PROT_WRITE;
2427 if (entry->protection & VM_PROT_EXECUTE)
2428 kve->kve_protection |= KVME_PROT_EXEC;
2430 if (entry->eflags & MAP_ENTRY_COW)
2431 kve->kve_flags |= KVME_FLAG_COW;
2432 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2433 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2434 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2435 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2437 last_timestamp = map->timestamp;
2438 vm_map_unlock_read(map);
2440 kve->kve_fileid = 0;
2445 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2446 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2447 kve->kve_type = KVME_TYPE_UNKNOWN;
2451 VM_OBJECT_RUNLOCK(lobj);
2453 kve->kve_ref_count = obj->ref_count;
2454 kve->kve_shadow_count = obj->shadow_count;
2455 VM_OBJECT_RUNLOCK(obj);
2457 vn_fullpath(vp, &fullpath, &freepath);
2458 cred = curthread->td_ucred;
2459 vn_lock(vp, LK_SHARED | LK_RETRY);
2460 if (VOP_GETATTR(vp, &va, cred) == 0) {
2461 kve->kve_fileid = va.va_fileid;
2463 kve->kve_fsid = va.va_fsid;
2468 kve->kve_type = KVME_TYPE_NONE;
2469 kve->kve_ref_count = 0;
2470 kve->kve_shadow_count = 0;
2473 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2474 if (freepath != NULL)
2475 free(freepath, M_TEMP);
2477 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2478 vm_map_lock_read(map);
2481 if (last_timestamp != map->timestamp) {
2482 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2486 vm_map_unlock_read(map);
2492 #endif /* COMPAT_FREEBSD7 */
2494 #ifdef KINFO_VMENTRY_SIZE
2495 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2499 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2500 int *resident_count, bool *super)
2502 vm_object_t obj, tobj;
2506 vm_pindex_t pi, pi_adv, pindex;
2509 *resident_count = 0;
2510 if (vmmap_skip_res_cnt)
2514 obj = entry->object.vm_object;
2515 addr = entry->start;
2517 pi = OFF_TO_IDX(entry->offset);
2518 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2519 if (m_adv != NULL) {
2522 pi_adv = atop(entry->end - addr);
2524 for (tobj = obj;; tobj = tobj->backing_object) {
2525 m = vm_page_find_least(tobj, pindex);
2527 if (m->pindex == pindex)
2529 if (pi_adv > m->pindex - pindex) {
2530 pi_adv = m->pindex - pindex;
2534 if (tobj->backing_object == NULL)
2536 pindex += OFF_TO_IDX(tobj->
2537 backing_object_offset);
2541 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2542 (addr & (pagesizes[1] - 1)) == 0 &&
2543 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2545 pi_adv = atop(pagesizes[1]);
2548 * We do not test the found page on validity.
2549 * Either the page is busy and being paged in,
2550 * or it was invalidated. The first case
2551 * should be counted as resident, the second
2552 * is not so clear; we do account both.
2556 *resident_count += pi_adv;
2562 * Must be called with the process locked and will return unlocked.
2565 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2567 vm_map_entry_t entry, tmp_entry;
2570 vm_object_t obj, tobj, lobj;
2571 char *fullpath, *freepath;
2572 struct kinfo_vmentry *kve;
2577 unsigned int last_timestamp;
2581 PROC_LOCK_ASSERT(p, MA_OWNED);
2585 vm = vmspace_acquire_ref(p);
2590 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2594 vm_map_lock_read(map);
2595 VM_MAP_ENTRY_FOREACH(entry, map) {
2596 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2600 bzero(kve, sizeof(*kve));
2601 obj = entry->object.vm_object;
2603 for (tobj = obj; tobj != NULL;
2604 tobj = tobj->backing_object) {
2605 VM_OBJECT_RLOCK(tobj);
2606 kve->kve_offset += tobj->backing_object_offset;
2609 if (obj->backing_object == NULL)
2610 kve->kve_private_resident =
2611 obj->resident_page_count;
2612 kern_proc_vmmap_resident(map, entry,
2613 &kve->kve_resident, &super);
2615 kve->kve_flags |= KVME_FLAG_SUPER;
2616 for (tobj = obj; tobj != NULL;
2617 tobj = tobj->backing_object) {
2618 if (tobj != obj && tobj != lobj)
2619 VM_OBJECT_RUNLOCK(tobj);
2625 kve->kve_start = entry->start;
2626 kve->kve_end = entry->end;
2627 kve->kve_offset += entry->offset;
2629 if (entry->protection & VM_PROT_READ)
2630 kve->kve_protection |= KVME_PROT_READ;
2631 if (entry->protection & VM_PROT_WRITE)
2632 kve->kve_protection |= KVME_PROT_WRITE;
2633 if (entry->protection & VM_PROT_EXECUTE)
2634 kve->kve_protection |= KVME_PROT_EXEC;
2636 if (entry->eflags & MAP_ENTRY_COW)
2637 kve->kve_flags |= KVME_FLAG_COW;
2638 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2639 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2640 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2641 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2642 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2643 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2644 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2645 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2646 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2647 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2649 last_timestamp = map->timestamp;
2650 vm_map_unlock_read(map);
2655 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2659 VM_OBJECT_RUNLOCK(lobj);
2661 kve->kve_ref_count = obj->ref_count;
2662 kve->kve_shadow_count = obj->shadow_count;
2663 VM_OBJECT_RUNLOCK(obj);
2665 vn_fullpath(vp, &fullpath, &freepath);
2666 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2667 cred = curthread->td_ucred;
2668 vn_lock(vp, LK_SHARED | LK_RETRY);
2669 if (VOP_GETATTR(vp, &va, cred) == 0) {
2670 kve->kve_vn_fileid = va.va_fileid;
2671 kve->kve_vn_fsid = va.va_fsid;
2672 kve->kve_vn_fsid_freebsd11 =
2673 kve->kve_vn_fsid; /* truncate */
2675 MAKEIMODE(va.va_type, va.va_mode);
2676 kve->kve_vn_size = va.va_size;
2677 kve->kve_vn_rdev = va.va_rdev;
2678 kve->kve_vn_rdev_freebsd11 =
2679 kve->kve_vn_rdev; /* truncate */
2680 kve->kve_status = KF_ATTR_VALID;
2685 kve->kve_type = KVME_TYPE_NONE;
2686 kve->kve_ref_count = 0;
2687 kve->kve_shadow_count = 0;
2690 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2691 if (freepath != NULL)
2692 free(freepath, M_TEMP);
2694 /* Pack record size down */
2695 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2696 kve->kve_structsize =
2697 offsetof(struct kinfo_vmentry, kve_path) +
2698 strlen(kve->kve_path) + 1;
2700 kve->kve_structsize = sizeof(*kve);
2701 kve->kve_structsize = roundup(kve->kve_structsize,
2704 /* Halt filling and truncate rather than exceeding maxlen */
2705 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2707 vm_map_lock_read(map);
2709 } else if (maxlen != -1)
2710 maxlen -= kve->kve_structsize;
2712 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2714 vm_map_lock_read(map);
2717 if (last_timestamp != map->timestamp) {
2718 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2722 vm_map_unlock_read(map);
2730 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2734 int error, error2, *name;
2737 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2738 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2739 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2744 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2745 error2 = sbuf_finish(&sb);
2747 return (error != 0 ? error : error2);
2750 #if defined(STACK) || defined(DDB)
2752 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2754 struct kinfo_kstack *kkstp;
2755 int error, i, *name, numthreads;
2756 lwpid_t *lwpidarray;
2763 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2767 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2768 st = stack_create(M_WAITOK);
2773 if (lwpidarray != NULL) {
2774 free(lwpidarray, M_TEMP);
2777 numthreads = p->p_numthreads;
2779 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2782 } while (numthreads < p->p_numthreads);
2785 * XXXRW: During the below loop, execve(2) and countless other sorts
2786 * of changes could have taken place. Should we check to see if the
2787 * vmspace has been replaced, or the like, in order to prevent
2788 * giving a snapshot that spans, say, execve(2), with some threads
2789 * before and some after? Among other things, the credentials could
2790 * have changed, in which case the right to extract debug info might
2791 * no longer be assured.
2794 FOREACH_THREAD_IN_PROC(p, td) {
2795 KASSERT(i < numthreads,
2796 ("sysctl_kern_proc_kstack: numthreads"));
2797 lwpidarray[i] = td->td_tid;
2802 for (i = 0; i < numthreads; i++) {
2803 td = tdfind(lwpidarray[i], p->p_pid);
2807 bzero(kkstp, sizeof(*kkstp));
2808 (void)sbuf_new(&sb, kkstp->kkst_trace,
2809 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2811 kkstp->kkst_tid = td->td_tid;
2812 if (TD_IS_SWAPPED(td))
2813 kkstp->kkst_state = KKST_STATE_SWAPPED;
2814 else if (stack_save_td(st, td) == 0)
2815 kkstp->kkst_state = KKST_STATE_STACKOK;
2817 kkstp->kkst_state = KKST_STATE_RUNNING;
2820 stack_sbuf_print(&sb, st);
2823 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2828 if (lwpidarray != NULL)
2829 free(lwpidarray, M_TEMP);
2831 free(kkstp, M_TEMP);
2837 * This sysctl allows a process to retrieve the full list of groups from
2838 * itself or another process.
2841 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2843 pid_t *pidp = (pid_t *)arg1;
2844 unsigned int arglen = arg2;
2851 if (*pidp == -1) { /* -1 means this process */
2852 p = req->td->td_proc;
2855 error = pget(*pidp, PGET_CANSEE, &p);
2860 cred = crhold(p->p_ucred);
2863 error = SYSCTL_OUT(req, cred->cr_groups,
2864 cred->cr_ngroups * sizeof(gid_t));
2870 * This sysctl allows a process to retrieve or/and set the resource limit for
2874 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2876 int *name = (int *)arg1;
2877 u_int namelen = arg2;
2886 which = (u_int)name[1];
2887 if (which >= RLIM_NLIMITS)
2890 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2893 flags = PGET_HOLD | PGET_NOTWEXIT;
2894 if (req->newptr != NULL)
2895 flags |= PGET_CANDEBUG;
2897 flags |= PGET_CANSEE;
2898 error = pget((pid_t)name[0], flags, &p);
2905 if (req->oldptr != NULL) {
2907 lim_rlimit_proc(p, which, &rlim);
2910 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2917 if (req->newptr != NULL) {
2918 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2920 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2929 * This sysctl allows a process to retrieve ps_strings structure location of
2933 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2935 int *name = (int *)arg1;
2936 u_int namelen = arg2;
2938 vm_offset_t ps_strings;
2940 #ifdef COMPAT_FREEBSD32
2941 uint32_t ps_strings32;
2947 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2950 #ifdef COMPAT_FREEBSD32
2951 if ((req->flags & SCTL_MASK32) != 0) {
2953 * We return 0 if the 32 bit emulation request is for a 64 bit
2956 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2957 PTROUT(p->p_sysent->sv_psstrings) : 0;
2959 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2963 ps_strings = p->p_sysent->sv_psstrings;
2965 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2970 * This sysctl allows a process to retrieve umask of another process.
2973 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2975 int *name = (int *)arg1;
2976 u_int namelen = arg2;
2985 pid = (pid_t)name[0];
2987 if (pid == p->p_pid || pid == 0) {
2988 cmask = p->p_pd->pd_cmask;
2992 error = pget(pid, PGET_WANTREAD, &p);
2996 cmask = p->p_pd->pd_cmask;
2999 error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
3004 * This sysctl allows a process to set and retrieve binary osreldate of
3008 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3010 int *name = (int *)arg1;
3011 u_int namelen = arg2;
3013 int flags, error, osrel;
3018 if (req->newptr != NULL && req->newlen != sizeof(osrel))
3021 flags = PGET_HOLD | PGET_NOTWEXIT;
3022 if (req->newptr != NULL)
3023 flags |= PGET_CANDEBUG;
3025 flags |= PGET_CANSEE;
3026 error = pget((pid_t)name[0], flags, &p);
3030 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3034 if (req->newptr != NULL) {
3035 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3050 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
3052 int *name = (int *)arg1;
3053 u_int namelen = arg2;
3055 struct kinfo_sigtramp kst;
3056 const struct sysentvec *sv;
3058 #ifdef COMPAT_FREEBSD32
3059 struct kinfo_sigtramp32 kst32;
3065 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3069 #ifdef COMPAT_FREEBSD32
3070 if ((req->flags & SCTL_MASK32) != 0) {
3071 bzero(&kst32, sizeof(kst32));
3072 if (SV_PROC_FLAG(p, SV_ILP32)) {
3073 if (sv->sv_sigcode_base != 0) {
3074 kst32.ksigtramp_start = sv->sv_sigcode_base;
3075 kst32.ksigtramp_end = sv->sv_sigcode_base +
3078 kst32.ksigtramp_start = sv->sv_psstrings -
3080 kst32.ksigtramp_end = sv->sv_psstrings;
3084 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3088 bzero(&kst, sizeof(kst));
3089 if (sv->sv_sigcode_base != 0) {
3090 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3091 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3094 kst.ksigtramp_start = (char *)sv->sv_psstrings -
3096 kst.ksigtramp_end = (char *)sv->sv_psstrings;
3099 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3104 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3106 int *name = (int *)arg1;
3107 u_int namelen = arg2;
3112 #ifdef COMPAT_FREEBSD32
3117 if (namelen != 1 || req->newptr != NULL)
3120 pid = (pid_t)name[0];
3121 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3126 #ifdef COMPAT_FREEBSD32
3127 if (SV_CURPROC_FLAG(SV_ILP32)) {
3128 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3134 if (pid <= PID_MAX) {
3135 td1 = FIRST_THREAD_IN_PROC(p);
3137 FOREACH_THREAD_IN_PROC(p, td1) {
3138 if (td1->td_tid == pid)
3147 * The access to the private thread flags. It is fine as far
3148 * as no out-of-thin-air values are read from td_pflags, and
3149 * usermode read of the td_sigblock_ptr is racy inherently,
3150 * since target process might have already changed it
3153 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3154 addr = (uintptr_t)td1->td_sigblock_ptr;
3164 #ifdef COMPAT_FREEBSD32
3165 if (SV_CURPROC_FLAG(SV_ILP32)) {
3167 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3170 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3174 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3177 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3178 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3179 "Return entire process table");
3181 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3182 sysctl_kern_proc, "Process table");
3184 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3185 sysctl_kern_proc, "Process table");
3187 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3188 sysctl_kern_proc, "Process table");
3190 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3191 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3193 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3194 sysctl_kern_proc, "Process table");
3196 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3197 sysctl_kern_proc, "Process table");
3199 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3200 sysctl_kern_proc, "Process table");
3202 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3203 sysctl_kern_proc, "Process table");
3205 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3206 sysctl_kern_proc, "Return process table, no threads");
3208 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3209 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3210 sysctl_kern_proc_args, "Process argument list");
3212 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3213 sysctl_kern_proc_env, "Process environment");
3215 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3216 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3218 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3219 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3221 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3222 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3223 "Process syscall vector name (ABI type)");
3225 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3226 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3228 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3229 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3231 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3232 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3234 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3235 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3237 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3238 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3240 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3241 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3243 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3244 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3246 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3247 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3249 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3250 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3251 "Return process table, no threads");
3253 #ifdef COMPAT_FREEBSD7
3254 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3255 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3258 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3259 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3261 #if defined(STACK) || defined(DDB)
3262 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3263 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3266 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3267 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3269 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3270 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3271 "Process resource limits");
3273 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3274 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3275 "Process ps_strings location");
3277 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3278 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3280 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3281 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3282 "Process binary osreldate");
3284 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3285 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3286 "Process signal trampoline location");
3288 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3289 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3290 "Thread sigfastblock address");
3295 * stop_all_proc() purpose is to stop all process which have usermode,
3296 * except current process for obvious reasons. This makes it somewhat
3297 * unreliable when invoked from multithreaded process. The service
3298 * must not be user-callable anyway.
3303 struct proc *cp, *p;
3305 bool restart, seen_stopped, seen_exiting, stopped_some;
3309 sx_xlock(&allproc_lock);
3311 seen_exiting = seen_stopped = stopped_some = restart = false;
3312 LIST_REMOVE(cp, p_list);
3313 LIST_INSERT_HEAD(&allproc, cp, p_list);
3315 p = LIST_NEXT(cp, p_list);
3318 LIST_REMOVE(cp, p_list);
3319 LIST_INSERT_AFTER(p, cp, p_list);
3321 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3325 if ((p->p_flag & P_WEXIT) != 0) {
3326 seen_exiting = true;
3330 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3332 * Stopped processes are tolerated when there
3333 * are no other processes which might continue
3334 * them. P_STOPPED_SINGLE but not
3335 * P_TOTAL_STOP process still has at least one
3338 seen_stopped = true;
3342 sx_xunlock(&allproc_lock);
3344 r = thread_single(p, SINGLE_ALLPROC);
3348 stopped_some = true;
3351 sx_xlock(&allproc_lock);
3353 /* Catch forked children we did not see in iteration. */
3354 if (gen != allproc_gen)
3356 sx_xunlock(&allproc_lock);
3357 if (restart || stopped_some || seen_exiting || seen_stopped) {
3358 kern_yield(PRI_USER);
3364 resume_all_proc(void)
3366 struct proc *cp, *p;
3369 sx_xlock(&allproc_lock);
3371 LIST_REMOVE(cp, p_list);
3372 LIST_INSERT_HEAD(&allproc, cp, p_list);
3374 p = LIST_NEXT(cp, p_list);
3377 LIST_REMOVE(cp, p_list);
3378 LIST_INSERT_AFTER(p, cp, p_list);
3380 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3381 sx_xunlock(&allproc_lock);
3383 thread_single_end(p, SINGLE_ALLPROC);
3386 sx_xlock(&allproc_lock);
3391 /* Did the loop above missed any stopped process ? */
3392 FOREACH_PROC_IN_SYSTEM(p) {
3393 /* No need for proc lock. */
3394 if ((p->p_flag & P_TOTAL_STOP) != 0)
3397 sx_xunlock(&allproc_lock);
3400 /* #define TOTAL_STOP_DEBUG 1 */
3401 #ifdef TOTAL_STOP_DEBUG
3402 volatile static int ap_resume;
3403 #include <sys/mount.h>
3406 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3412 error = sysctl_handle_int(oidp, &val, 0, req);
3413 if (error != 0 || req->newptr == NULL)
3418 while (ap_resume == 0)
3426 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3427 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3428 sysctl_debug_stop_all_proc, "I",