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>
36 #include "opt_ktrace.h"
37 #include "opt_kstack_pages.h"
38 #include "opt_stack.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bitstring.h>
44 #include <sys/eventhandler.h>
46 #include <sys/fcntl.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
51 #include <sys/loginclass.h>
52 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/namei.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>
79 #include <sys/ktrace.h>
87 #include <vm/vm_param.h>
88 #include <vm/vm_extern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_page.h>
95 #include <fs/devfs/devfs.h>
97 #ifdef COMPAT_FREEBSD32
98 #include <compat/freebsd32/freebsd32.h>
99 #include <compat/freebsd32/freebsd32_util.h>
102 SDT_PROVIDER_DEFINE(proc);
104 MALLOC_DEFINE(M_SESSION, "session", "session header");
105 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
106 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
108 static void doenterpgrp(struct proc *, struct pgrp *);
109 static void orphanpg(struct pgrp *pg);
110 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
111 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
112 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
114 static void pgdelete(struct pgrp *);
115 static int pgrp_init(void *mem, int size, int flags);
116 static int proc_ctor(void *mem, int size, void *arg, int flags);
117 static void proc_dtor(void *mem, int size, void *arg);
118 static int proc_init(void *mem, int size, int flags);
119 static void proc_fini(void *mem, int size);
120 static void pargs_free(struct pargs *pa);
123 * Other process lists
125 struct pidhashhead *pidhashtbl = NULL;
126 struct sx *pidhashtbl_lock;
129 struct pgrphashhead *pgrphashtbl;
131 struct proclist allproc = LIST_HEAD_INITIALIZER(allproc);
132 struct sx __exclusive_cache_line allproc_lock;
133 struct sx __exclusive_cache_line proctree_lock;
134 struct mtx __exclusive_cache_line ppeers_lock;
135 struct mtx __exclusive_cache_line procid_lock;
136 uma_zone_t proc_zone;
137 uma_zone_t pgrp_zone;
140 * The offset of various fields in struct proc and struct thread.
141 * These are used by kernel debuggers to enumerate kernel threads and
144 const int proc_off_p_pid = offsetof(struct proc, p_pid);
145 const int proc_off_p_comm = offsetof(struct proc, p_comm);
146 const int proc_off_p_list = offsetof(struct proc, p_list);
147 const int proc_off_p_hash = offsetof(struct proc, p_hash);
148 const int proc_off_p_threads = offsetof(struct proc, p_threads);
149 const int thread_off_td_tid = offsetof(struct thread, td_tid);
150 const int thread_off_td_name = offsetof(struct thread, td_name);
151 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
152 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
153 const int thread_off_td_plist = offsetof(struct thread, td_plist);
155 EVENTHANDLER_LIST_DEFINE(process_ctor);
156 EVENTHANDLER_LIST_DEFINE(process_dtor);
157 EVENTHANDLER_LIST_DEFINE(process_init);
158 EVENTHANDLER_LIST_DEFINE(process_fini);
159 EVENTHANDLER_LIST_DEFINE(process_exit);
160 EVENTHANDLER_LIST_DEFINE(process_fork);
161 EVENTHANDLER_LIST_DEFINE(process_exec);
163 int kstack_pages = KSTACK_PAGES;
164 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
165 "Kernel stack size in pages");
166 static int vmmap_skip_res_cnt = 0;
167 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
168 &vmmap_skip_res_cnt, 0,
169 "Skip calculation of the pages resident count in kern.proc.vmmap");
171 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
172 #ifdef COMPAT_FREEBSD32
173 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
177 * Initialize global process hashing structures.
184 sx_init(&allproc_lock, "allproc");
185 sx_init(&proctree_lock, "proctree");
186 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
187 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
188 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
189 pidhashlock = (pidhash + 1) / 64;
192 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
193 M_PROC, M_WAITOK | M_ZERO);
194 for (i = 0; i < pidhashlock + 1; i++)
195 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
196 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
197 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
198 proc_ctor, proc_dtor, proc_init, proc_fini,
199 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
200 pgrp_zone = uma_zcreate("PGRP", sizeof(struct pgrp), NULL, NULL,
201 pgrp_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
206 * Prepare a proc for use.
209 proc_ctor(void *mem, int size, void *arg, int flags)
214 p = (struct proc *)mem;
216 kdtrace_proc_ctor(p);
218 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
219 td = FIRST_THREAD_IN_PROC(p);
221 /* Make sure all thread constructors are executed */
222 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
228 * Reclaim a proc after use.
231 proc_dtor(void *mem, int size, void *arg)
236 /* INVARIANTS checks go here */
237 p = (struct proc *)mem;
238 td = FIRST_THREAD_IN_PROC(p);
241 KASSERT((p->p_numthreads == 1),
242 ("bad number of threads in exiting process"));
243 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
245 /* Free all OSD associated to this thread. */
247 td_softdep_cleanup(td);
248 MPASS(td->td_su == NULL);
250 /* Make sure all thread destructors are executed */
251 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
253 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
255 kdtrace_proc_dtor(p);
257 if (p->p_ksi != NULL)
258 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
262 * Initialize type-stable parts of a proc (when newly created).
265 proc_init(void *mem, int size, int flags)
269 p = (struct proc *)mem;
270 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
271 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
272 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
273 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
274 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
275 cv_init(&p->p_pwait, "ppwait");
276 TAILQ_INIT(&p->p_threads); /* all threads in proc */
277 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
278 p->p_stats = pstats_alloc();
280 TAILQ_INIT(&p->p_kqtim_stop);
285 * UMA should ensure that this function is never called.
286 * Freeing a proc structure would violate type stability.
289 proc_fini(void *mem, int size)
294 p = (struct proc *)mem;
295 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
296 pstats_free(p->p_stats);
297 thread_free(FIRST_THREAD_IN_PROC(p));
298 mtx_destroy(&p->p_mtx);
299 if (p->p_ksi != NULL)
300 ksiginfo_free(p->p_ksi);
302 panic("proc reclaimed");
307 pgrp_init(void *mem, int size, int flags)
312 mtx_init(&pg->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
313 sx_init(&pg->pg_killsx, "killpg racer");
318 * PID space management.
320 * These bitmaps are used by fork_findpid.
322 bitstr_t bit_decl(proc_id_pidmap, PID_MAX);
323 bitstr_t bit_decl(proc_id_grpidmap, PID_MAX);
324 bitstr_t bit_decl(proc_id_sessidmap, PID_MAX);
325 bitstr_t bit_decl(proc_id_reapmap, PID_MAX);
327 static bitstr_t *proc_id_array[] = {
335 proc_id_set(int type, pid_t id)
338 KASSERT(type >= 0 && type < nitems(proc_id_array),
339 ("invalid type %d\n", type));
340 mtx_lock(&procid_lock);
341 KASSERT(bit_test(proc_id_array[type], id) == 0,
342 ("bit %d already set in %d\n", id, type));
343 bit_set(proc_id_array[type], id);
344 mtx_unlock(&procid_lock);
348 proc_id_set_cond(int type, pid_t id)
351 KASSERT(type >= 0 && type < nitems(proc_id_array),
352 ("invalid type %d\n", type));
353 if (bit_test(proc_id_array[type], id))
355 mtx_lock(&procid_lock);
356 bit_set(proc_id_array[type], id);
357 mtx_unlock(&procid_lock);
361 proc_id_clear(int type, pid_t id)
364 KASSERT(type >= 0 && type < nitems(proc_id_array),
365 ("invalid type %d\n", type));
366 mtx_lock(&procid_lock);
367 KASSERT(bit_test(proc_id_array[type], id) != 0,
368 ("bit %d not set in %d\n", id, type));
369 bit_clear(proc_id_array[type], id);
370 mtx_unlock(&procid_lock);
374 * Is p an inferior of the current process?
377 inferior(struct proc *p)
380 sx_assert(&proctree_lock, SX_LOCKED);
381 PROC_LOCK_ASSERT(p, MA_OWNED);
382 for (; p != curproc; p = proc_realparent(p)) {
390 * Shared lock all the pid hash lists.
393 pidhash_slockall(void)
397 for (i = 0; i < pidhashlock + 1; i++)
398 sx_slock(&pidhashtbl_lock[i]);
402 * Shared unlock all the pid hash lists.
405 pidhash_sunlockall(void)
409 for (i = 0; i < pidhashlock + 1; i++)
410 sx_sunlock(&pidhashtbl_lock[i]);
414 * Similar to pfind_any(), this function finds zombies.
417 pfind_any_locked(pid_t pid)
421 sx_assert(PIDHASHLOCK(pid), SX_LOCKED);
422 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
423 if (p->p_pid == pid) {
425 if (p->p_state == PRS_NEW) {
436 * Locate a process by number.
438 * By not returning processes in the PRS_NEW state, we allow callers to avoid
439 * testing for that condition to avoid dereferencing p_ucred, et al.
441 static __always_inline struct proc *
442 _pfind(pid_t pid, bool zombie)
447 if (p->p_pid == pid) {
451 sx_slock(PIDHASHLOCK(pid));
452 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
453 if (p->p_pid == pid) {
455 if (p->p_state == PRS_NEW ||
456 (!zombie && p->p_state == PRS_ZOMBIE)) {
463 sx_sunlock(PIDHASHLOCK(pid));
471 return (_pfind(pid, false));
475 * Same as pfind but allow zombies.
481 return (_pfind(pid, true));
485 * Locate a process group by number.
486 * The caller must hold proctree_lock.
493 sx_assert(&proctree_lock, SX_LOCKED);
495 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
496 if (pgrp->pg_id == pgid) {
505 * Locate process and do additional manipulations, depending on flags.
508 pget(pid_t pid, int flags, struct proc **pp)
515 if (p->p_pid == pid) {
519 if (pid <= PID_MAX) {
520 if ((flags & PGET_NOTWEXIT) == 0)
524 } else if ((flags & PGET_NOTID) == 0) {
525 td1 = tdfind(pid, -1);
531 if ((flags & PGET_CANSEE) != 0) {
532 error = p_cansee(curthread, p);
537 if ((flags & PGET_CANDEBUG) != 0) {
538 error = p_candebug(curthread, p);
542 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
546 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
550 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
552 * XXXRW: Not clear ESRCH is the right error during proc
558 if ((flags & PGET_HOLD) != 0) {
570 * Create a new process group.
571 * pgid must be equal to the pid of p.
572 * Begin a new session if required.
575 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
577 struct pgrp *old_pgrp;
579 sx_assert(&proctree_lock, SX_XLOCKED);
581 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
582 KASSERT(p->p_pid == pgid,
583 ("enterpgrp: new pgrp and pid != pgid"));
584 KASSERT(pgfind(pgid) == NULL,
585 ("enterpgrp: pgrp with pgid exists"));
586 KASSERT(!SESS_LEADER(p),
587 ("enterpgrp: session leader attempted setpgrp"));
589 old_pgrp = p->p_pgrp;
590 if (!sx_try_xlock(&old_pgrp->pg_killsx))
592 MPASS(old_pgrp == p->p_pgrp);
598 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
600 p->p_flag &= ~P_CONTROLT;
604 sess->s_sid = p->p_pid;
605 proc_id_set(PROC_ID_SESSION, p->p_pid);
606 refcount_init(&sess->s_count, 1);
607 sess->s_ttyvp = NULL;
608 sess->s_ttydp = NULL;
610 bcopy(p->p_session->s_login, sess->s_login,
611 sizeof(sess->s_login));
612 pgrp->pg_session = sess;
613 KASSERT(p == curproc,
614 ("enterpgrp: mksession and p != curproc"));
616 pgrp->pg_session = p->p_session;
617 sess_hold(pgrp->pg_session);
621 proc_id_set(PROC_ID_GROUP, p->p_pid);
622 LIST_INIT(&pgrp->pg_members);
626 * As we have an exclusive lock of proctree_lock,
627 * this should not deadlock.
629 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
630 SLIST_INIT(&pgrp->pg_sigiolst);
633 doenterpgrp(p, pgrp);
635 sx_xunlock(&old_pgrp->pg_killsx);
640 * Move p to an existing process group
643 enterthispgrp(struct proc *p, struct pgrp *pgrp)
645 struct pgrp *old_pgrp;
647 sx_assert(&proctree_lock, SX_XLOCKED);
648 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
649 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
650 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
651 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
652 KASSERT(pgrp->pg_session == p->p_session,
653 ("%s: pgrp's session %p, p->p_session %p proc %p\n",
654 __func__, pgrp->pg_session, p->p_session, p));
655 KASSERT(pgrp != p->p_pgrp,
656 ("%s: p %p belongs to pgrp %p", __func__, p, pgrp));
658 old_pgrp = p->p_pgrp;
659 if (!sx_try_xlock(&old_pgrp->pg_killsx))
661 MPASS(old_pgrp == p->p_pgrp);
662 if (!sx_try_xlock(&pgrp->pg_killsx)) {
663 sx_xunlock(&old_pgrp->pg_killsx);
667 doenterpgrp(p, pgrp);
669 sx_xunlock(&pgrp->pg_killsx);
670 sx_xunlock(&old_pgrp->pg_killsx);
675 * If true, any child of q which belongs to group pgrp, qualifies the
676 * process group pgrp as not orphaned.
679 isjobproc(struct proc *q, struct pgrp *pgrp)
681 sx_assert(&proctree_lock, SX_LOCKED);
683 return (q->p_pgrp != pgrp &&
684 q->p_pgrp->pg_session == pgrp->pg_session);
688 jobc_reaper(struct proc *p)
692 sx_assert(&proctree_lock, SA_LOCKED);
696 if (pp->p_reaper == pp ||
697 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
703 jobc_parent(struct proc *p, struct proc *p_exiting)
707 sx_assert(&proctree_lock, SA_LOCKED);
709 pp = proc_realparent(p);
710 if (pp->p_pptr == NULL || pp == p_exiting ||
711 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
713 return (jobc_reaper(pp));
717 pgrp_calc_jobc(struct pgrp *pgrp)
723 if (!mtx_owned(&pgrp->pg_mtx))
724 sx_assert(&proctree_lock, SA_LOCKED);
728 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
729 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
732 if (isjobproc(jobc_parent(q, NULL), pgrp))
739 * Move p to a process group
742 doenterpgrp(struct proc *p, struct pgrp *pgrp)
744 struct pgrp *savepgrp;
747 sx_assert(&proctree_lock, SX_XLOCKED);
748 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
749 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
750 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
751 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
753 savepgrp = p->p_pgrp;
754 pp = jobc_parent(p, NULL);
758 if (isjobproc(pp, savepgrp) && pgrp_calc_jobc(savepgrp) == 1)
761 LIST_REMOVE(p, p_pglist);
764 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
765 if (isjobproc(pp, pgrp))
766 pgrp->pg_flags &= ~PGRP_ORPHANED;
767 PGRP_UNLOCK(savepgrp);
769 if (LIST_EMPTY(&savepgrp->pg_members))
774 * remove process from process group
777 leavepgrp(struct proc *p)
779 struct pgrp *savepgrp;
781 sx_assert(&proctree_lock, SX_XLOCKED);
782 savepgrp = p->p_pgrp;
785 LIST_REMOVE(p, p_pglist);
788 PGRP_UNLOCK(savepgrp);
789 if (LIST_EMPTY(&savepgrp->pg_members))
795 * delete a process group
798 pgdelete(struct pgrp *pgrp)
800 struct session *savesess;
803 sx_assert(&proctree_lock, SX_XLOCKED);
804 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
805 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
808 * Reset any sigio structures pointing to us as a result of
809 * F_SETOWN with our pgid. The proctree lock ensures that
810 * new sigio structures will not be added after this point.
812 funsetownlst(&pgrp->pg_sigiolst);
815 tp = pgrp->pg_session->s_ttyp;
816 LIST_REMOVE(pgrp, pg_hash);
817 savesess = pgrp->pg_session;
820 /* Remove the reference to the pgrp before deallocating it. */
823 tty_rel_pgrp(tp, pgrp);
826 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
827 uma_zfree(pgrp_zone, pgrp);
828 sess_release(savesess);
833 fixjobc_kill(struct proc *p)
838 sx_assert(&proctree_lock, SX_LOCKED);
839 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
841 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
842 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
845 * p no longer affects process group orphanage for children.
846 * It is marked by the flag because p is only physically
847 * removed from its process group on wait(2).
849 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
850 p->p_treeflag |= P_TREE_GRPEXITED;
853 * Check if exiting p orphans its own group.
856 if (isjobproc(jobc_parent(p, NULL), pgrp)) {
858 if (pgrp_calc_jobc(pgrp) == 0)
864 * Check this process' children to see whether they qualify
865 * their process groups after reparenting to reaper.
867 LIST_FOREACH(q, &p->p_children, p_sibling) {
870 if (pgrp_calc_jobc(pgrp) == 0) {
872 * We want to handle exactly the children that
873 * has p as realparent. Then, when calculating
874 * jobc_parent for children, we should ignore
875 * P_TREE_GRPEXITED flag already set on p.
877 if (jobc_parent(q, p) == p && isjobproc(p, pgrp))
880 pgrp->pg_flags &= ~PGRP_ORPHANED;
883 LIST_FOREACH(q, &p->p_orphans, p_orphan) {
886 if (pgrp_calc_jobc(pgrp) == 0) {
887 if (isjobproc(p, pgrp))
890 pgrp->pg_flags &= ~PGRP_ORPHANED;
904 MPASS(p->p_flag & P_WEXIT);
905 sx_assert(&proctree_lock, SX_LOCKED);
907 if (SESS_LEADER(p)) {
911 * s_ttyp is not zero'd; we use this to indicate that
912 * the session once had a controlling terminal. (for
913 * logging and informational purposes)
924 * Signal foreground pgrp and revoke access to
925 * controlling terminal if it has not been revoked
928 * Because the TTY may have been revoked in the mean
929 * time and could already have a new session associated
930 * with it, make sure we don't send a SIGHUP to a
931 * foreground process group that does not belong to this
937 if (tp->t_session == sp)
938 tty_signal_pgrp(tp, SIGHUP);
943 sx_xunlock(&proctree_lock);
944 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
945 VOP_REVOKE(ttyvp, REVOKEALL);
948 devfs_ctty_unref(ttyvp);
949 sx_xlock(&proctree_lock);
956 * A process group has become orphaned, mark it as such for signal
957 * delivery code. If there are any stopped processes in the group,
958 * hang-up all process in that group.
961 orphanpg(struct pgrp *pg)
965 PGRP_LOCK_ASSERT(pg, MA_OWNED);
967 pg->pg_flags |= PGRP_ORPHANED;
969 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
971 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
973 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
975 kern_psignal(p, SIGHUP);
976 kern_psignal(p, SIGCONT);
986 sess_hold(struct session *s)
989 refcount_acquire(&s->s_count);
993 sess_release(struct session *s)
996 if (refcount_release(&s->s_count)) {
997 if (s->s_ttyp != NULL) {
999 tty_rel_sess(s->s_ttyp, s);
1001 proc_id_clear(PROC_ID_SESSION, s->s_sid);
1002 mtx_destroy(&s->s_mtx);
1010 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
1013 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
1014 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
1015 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
1016 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
1019 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1025 for (i = 0; i <= pgrphash; i++) {
1026 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1027 db_printf("indx %d\n", i);
1028 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1030 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1031 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1032 pgrp->pg_session->s_count,
1033 LIST_FIRST(&pgrp->pg_members));
1034 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1035 db_print_pgrp_one(pgrp, p);
1043 * Calculate the kinfo_proc members which contain process-wide
1045 * Must be called with the target process locked.
1048 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1052 PROC_LOCK_ASSERT(p, MA_OWNED);
1056 FOREACH_THREAD_IN_PROC(p, td) {
1058 kp->ki_pctcpu += sched_pctcpu(td);
1059 kp->ki_estcpu += sched_estcpu(td);
1065 * Fill in any information that is common to all threads in the process.
1066 * Must be called with the target process locked.
1069 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1074 struct timeval boottime;
1076 PROC_LOCK_ASSERT(p, MA_OWNED);
1078 kp->ki_structsize = sizeof(*kp);
1080 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1081 kp->ki_args = p->p_args;
1082 kp->ki_textvp = p->p_textvp;
1084 kp->ki_tracep = ktr_get_tracevp(p, false);
1085 kp->ki_traceflag = p->p_traceflag;
1087 kp->ki_fd = p->p_fd;
1088 kp->ki_pd = p->p_pd;
1089 kp->ki_vmspace = p->p_vmspace;
1090 kp->ki_flag = p->p_flag;
1091 kp->ki_flag2 = p->p_flag2;
1094 kp->ki_uid = cred->cr_uid;
1095 kp->ki_ruid = cred->cr_ruid;
1096 kp->ki_svuid = cred->cr_svuid;
1097 kp->ki_cr_flags = 0;
1098 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1099 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1100 /* XXX bde doesn't like KI_NGROUPS */
1101 if (cred->cr_ngroups > KI_NGROUPS) {
1102 kp->ki_ngroups = KI_NGROUPS;
1103 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1105 kp->ki_ngroups = cred->cr_ngroups;
1106 bcopy(cred->cr_groups, kp->ki_groups,
1107 kp->ki_ngroups * sizeof(gid_t));
1108 kp->ki_rgid = cred->cr_rgid;
1109 kp->ki_svgid = cred->cr_svgid;
1110 /* If jailed(cred), emulate the old P_JAILED flag. */
1112 kp->ki_flag |= P_JAILED;
1113 /* If inside the jail, use 0 as a jail ID. */
1114 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1115 kp->ki_jid = cred->cr_prison->pr_id;
1117 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1118 sizeof(kp->ki_loginclass));
1122 mtx_lock(&ps->ps_mtx);
1123 kp->ki_sigignore = ps->ps_sigignore;
1124 kp->ki_sigcatch = ps->ps_sigcatch;
1125 mtx_unlock(&ps->ps_mtx);
1127 if (p->p_state != PRS_NEW &&
1128 p->p_state != PRS_ZOMBIE &&
1129 p->p_vmspace != NULL) {
1130 struct vmspace *vm = p->p_vmspace;
1132 kp->ki_size = vm->vm_map.size;
1133 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1134 FOREACH_THREAD_IN_PROC(p, td0) {
1135 if (!TD_IS_SWAPPED(td0))
1136 kp->ki_rssize += td0->td_kstack_pages;
1138 kp->ki_swrss = vm->vm_swrss;
1139 kp->ki_tsize = vm->vm_tsize;
1140 kp->ki_dsize = vm->vm_dsize;
1141 kp->ki_ssize = vm->vm_ssize;
1142 } else if (p->p_state == PRS_ZOMBIE)
1143 kp->ki_stat = SZOMB;
1144 if (kp->ki_flag & P_INMEM)
1145 kp->ki_sflag = PS_INMEM;
1148 /* Calculate legacy swtime as seconds since 'swtick'. */
1149 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1150 kp->ki_pid = p->p_pid;
1151 kp->ki_nice = p->p_nice;
1152 kp->ki_fibnum = p->p_fibnum;
1153 kp->ki_start = p->p_stats->p_start;
1154 getboottime(&boottime);
1155 timevaladd(&kp->ki_start, &boottime);
1157 rufetch(p, &kp->ki_rusage);
1158 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1159 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1161 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1162 /* Some callers want child times in a single value. */
1163 kp->ki_childtime = kp->ki_childstime;
1164 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1166 FOREACH_THREAD_IN_PROC(p, td0)
1167 kp->ki_cow += td0->td_cow;
1169 if (p->p_comm[0] != '\0')
1170 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1171 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1172 p->p_sysent->sv_name[0] != '\0')
1173 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1174 kp->ki_siglist = p->p_siglist;
1175 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1176 kp->ki_acflag = p->p_acflag;
1177 kp->ki_lock = p->p_lock;
1179 kp->ki_ppid = p->p_oppid;
1180 if (p->p_flag & P_TRACED)
1181 kp->ki_tracer = p->p_pptr->p_pid;
1186 * Fill job-related process information.
1189 fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp)
1195 sx_assert(&proctree_lock, SA_LOCKED);
1196 PROC_LOCK_ASSERT(p, MA_OWNED);
1202 kp->ki_pgid = pgrp->pg_id;
1203 kp->ki_jobc = pgrp_calc_jobc(pgrp);
1205 sp = pgrp->pg_session;
1209 kp->ki_sid = sp->s_sid;
1211 strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login));
1213 kp->ki_kiflag |= KI_CTTY;
1215 kp->ki_kiflag |= KI_SLEADER;
1220 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1221 kp->ki_tdev = tty_udev(tp);
1222 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1223 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1225 kp->ki_tsid = tp->t_session->s_sid;
1227 kp->ki_tdev = NODEV;
1228 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1233 * Fill in information that is thread specific. Must be called with
1234 * target process locked. If 'preferthread' is set, overwrite certain
1235 * process-related fields that are maintained for both threads and
1239 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1245 PROC_LOCK_ASSERT(p, MA_OWNED);
1250 if (td->td_wmesg != NULL)
1251 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1253 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1254 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1255 sizeof(kp->ki_tdname)) {
1256 strlcpy(kp->ki_moretdname,
1257 td->td_name + sizeof(kp->ki_tdname) - 1,
1258 sizeof(kp->ki_moretdname));
1260 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1262 if (TD_ON_LOCK(td)) {
1263 kp->ki_kiflag |= KI_LOCKBLOCK;
1264 strlcpy(kp->ki_lockname, td->td_lockname,
1265 sizeof(kp->ki_lockname));
1267 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1268 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1271 if (p->p_state == PRS_NORMAL) { /* approximate. */
1272 if (TD_ON_RUNQ(td) ||
1274 TD_IS_RUNNING(td)) {
1276 } else if (P_SHOULDSTOP(p)) {
1277 kp->ki_stat = SSTOP;
1278 } else if (TD_IS_SLEEPING(td)) {
1279 kp->ki_stat = SSLEEP;
1280 } else if (TD_ON_LOCK(td)) {
1281 kp->ki_stat = SLOCK;
1283 kp->ki_stat = SWAIT;
1285 } else if (p->p_state == PRS_ZOMBIE) {
1286 kp->ki_stat = SZOMB;
1291 /* Things in the thread */
1292 kp->ki_wchan = td->td_wchan;
1293 kp->ki_pri.pri_level = td->td_priority;
1294 kp->ki_pri.pri_native = td->td_base_pri;
1297 * Note: legacy fields; clamp at the old NOCPU value and/or
1298 * the maximum u_char CPU value.
1300 if (td->td_lastcpu == NOCPU)
1301 kp->ki_lastcpu_old = NOCPU_OLD;
1302 else if (td->td_lastcpu > MAXCPU_OLD)
1303 kp->ki_lastcpu_old = MAXCPU_OLD;
1305 kp->ki_lastcpu_old = td->td_lastcpu;
1307 if (td->td_oncpu == NOCPU)
1308 kp->ki_oncpu_old = NOCPU_OLD;
1309 else if (td->td_oncpu > MAXCPU_OLD)
1310 kp->ki_oncpu_old = MAXCPU_OLD;
1312 kp->ki_oncpu_old = td->td_oncpu;
1314 kp->ki_lastcpu = td->td_lastcpu;
1315 kp->ki_oncpu = td->td_oncpu;
1316 kp->ki_tdflags = td->td_flags;
1317 kp->ki_tid = td->td_tid;
1318 kp->ki_numthreads = p->p_numthreads;
1319 kp->ki_pcb = td->td_pcb;
1320 kp->ki_kstack = (void *)td->td_kstack;
1321 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1322 kp->ki_pri.pri_class = td->td_pri_class;
1323 kp->ki_pri.pri_user = td->td_user_pri;
1326 rufetchtd(td, &kp->ki_rusage);
1327 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1328 kp->ki_pctcpu = sched_pctcpu(td);
1329 kp->ki_estcpu = sched_estcpu(td);
1330 kp->ki_cow = td->td_cow;
1333 /* We can't get this anymore but ps etc never used it anyway. */
1337 kp->ki_siglist = td->td_siglist;
1338 kp->ki_sigmask = td->td_sigmask;
1345 * Fill in a kinfo_proc structure for the specified process.
1346 * Must be called with the target process locked.
1349 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1351 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1353 bzero(kp, sizeof(*kp));
1355 fill_kinfo_proc_pgrp(p,kp);
1356 fill_kinfo_proc_only(p, kp);
1357 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1358 fill_kinfo_aggregate(p, kp);
1365 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1369 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1372 pstats_fork(struct pstats *src, struct pstats *dst)
1375 bzero(&dst->pstat_startzero,
1376 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1377 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1378 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1382 pstats_free(struct pstats *ps)
1385 free(ps, M_SUBPROC);
1388 #ifdef COMPAT_FREEBSD32
1391 * This function is typically used to copy out the kernel address, so
1392 * it can be replaced by assignment of zero.
1394 static inline uint32_t
1395 ptr32_trim(const void *ptr)
1399 uptr = (uintptr_t)ptr;
1400 return ((uptr > UINT_MAX) ? 0 : uptr);
1403 #define PTRTRIM_CP(src,dst,fld) \
1404 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1407 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1411 bzero(ki32, sizeof(struct kinfo_proc32));
1412 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1413 CP(*ki, *ki32, ki_layout);
1414 PTRTRIM_CP(*ki, *ki32, ki_args);
1415 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1416 PTRTRIM_CP(*ki, *ki32, ki_addr);
1417 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1418 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1419 PTRTRIM_CP(*ki, *ki32, ki_fd);
1420 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1421 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1422 CP(*ki, *ki32, ki_pid);
1423 CP(*ki, *ki32, ki_ppid);
1424 CP(*ki, *ki32, ki_pgid);
1425 CP(*ki, *ki32, ki_tpgid);
1426 CP(*ki, *ki32, ki_sid);
1427 CP(*ki, *ki32, ki_tsid);
1428 CP(*ki, *ki32, ki_jobc);
1429 CP(*ki, *ki32, ki_tdev);
1430 CP(*ki, *ki32, ki_tdev_freebsd11);
1431 CP(*ki, *ki32, ki_siglist);
1432 CP(*ki, *ki32, ki_sigmask);
1433 CP(*ki, *ki32, ki_sigignore);
1434 CP(*ki, *ki32, ki_sigcatch);
1435 CP(*ki, *ki32, ki_uid);
1436 CP(*ki, *ki32, ki_ruid);
1437 CP(*ki, *ki32, ki_svuid);
1438 CP(*ki, *ki32, ki_rgid);
1439 CP(*ki, *ki32, ki_svgid);
1440 CP(*ki, *ki32, ki_ngroups);
1441 for (i = 0; i < KI_NGROUPS; i++)
1442 CP(*ki, *ki32, ki_groups[i]);
1443 CP(*ki, *ki32, ki_size);
1444 CP(*ki, *ki32, ki_rssize);
1445 CP(*ki, *ki32, ki_swrss);
1446 CP(*ki, *ki32, ki_tsize);
1447 CP(*ki, *ki32, ki_dsize);
1448 CP(*ki, *ki32, ki_ssize);
1449 CP(*ki, *ki32, ki_xstat);
1450 CP(*ki, *ki32, ki_acflag);
1451 CP(*ki, *ki32, ki_pctcpu);
1452 CP(*ki, *ki32, ki_estcpu);
1453 CP(*ki, *ki32, ki_slptime);
1454 CP(*ki, *ki32, ki_swtime);
1455 CP(*ki, *ki32, ki_cow);
1456 CP(*ki, *ki32, ki_runtime);
1457 TV_CP(*ki, *ki32, ki_start);
1458 TV_CP(*ki, *ki32, ki_childtime);
1459 CP(*ki, *ki32, ki_flag);
1460 CP(*ki, *ki32, ki_kiflag);
1461 CP(*ki, *ki32, ki_traceflag);
1462 CP(*ki, *ki32, ki_stat);
1463 CP(*ki, *ki32, ki_nice);
1464 CP(*ki, *ki32, ki_lock);
1465 CP(*ki, *ki32, ki_rqindex);
1466 CP(*ki, *ki32, ki_oncpu);
1467 CP(*ki, *ki32, ki_lastcpu);
1469 /* XXX TODO: wrap cpu value as appropriate */
1470 CP(*ki, *ki32, ki_oncpu_old);
1471 CP(*ki, *ki32, ki_lastcpu_old);
1473 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1474 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1475 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1476 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1477 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1478 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1479 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1480 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1481 CP(*ki, *ki32, ki_tracer);
1482 CP(*ki, *ki32, ki_flag2);
1483 CP(*ki, *ki32, ki_fibnum);
1484 CP(*ki, *ki32, ki_cr_flags);
1485 CP(*ki, *ki32, ki_jid);
1486 CP(*ki, *ki32, ki_numthreads);
1487 CP(*ki, *ki32, ki_tid);
1488 CP(*ki, *ki32, ki_pri);
1489 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1490 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1491 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1492 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1493 PTRTRIM_CP(*ki, *ki32, ki_udata);
1494 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1495 CP(*ki, *ki32, ki_sflag);
1496 CP(*ki, *ki32, ki_tdflags);
1501 kern_proc_out_size(struct proc *p, int flags)
1505 PROC_LOCK_ASSERT(p, MA_OWNED);
1507 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1508 #ifdef COMPAT_FREEBSD32
1509 if ((flags & KERN_PROC_MASK32) != 0) {
1510 size += sizeof(struct kinfo_proc32);
1513 size += sizeof(struct kinfo_proc);
1515 #ifdef COMPAT_FREEBSD32
1516 if ((flags & KERN_PROC_MASK32) != 0)
1517 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1520 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1527 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1530 struct kinfo_proc ki;
1531 #ifdef COMPAT_FREEBSD32
1532 struct kinfo_proc32 ki32;
1536 PROC_LOCK_ASSERT(p, MA_OWNED);
1537 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1540 fill_kinfo_proc(p, &ki);
1541 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1542 #ifdef COMPAT_FREEBSD32
1543 if ((flags & KERN_PROC_MASK32) != 0) {
1544 freebsd32_kinfo_proc_out(&ki, &ki32);
1545 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1549 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1552 FOREACH_THREAD_IN_PROC(p, td) {
1553 fill_kinfo_thread(td, &ki, 1);
1554 #ifdef COMPAT_FREEBSD32
1555 if ((flags & KERN_PROC_MASK32) != 0) {
1556 freebsd32_kinfo_proc_out(&ki, &ki32);
1557 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1561 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1572 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1575 struct kinfo_proc ki;
1578 if (req->oldptr == NULL)
1579 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1581 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1582 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1583 error = kern_proc_out(p, &sb, flags);
1584 error2 = sbuf_finish(&sb);
1588 else if (error2 != 0)
1594 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1599 for (i = 0; i < pidhashlock + 1; i++) {
1600 sx_slock(&proctree_lock);
1601 sx_slock(&pidhashtbl_lock[i]);
1602 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1603 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1604 if (p->p_state == PRS_NEW)
1606 error = cb(p, cbarg);
1607 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1609 sx_sunlock(&pidhashtbl_lock[i]);
1610 sx_sunlock(&proctree_lock);
1615 sx_sunlock(&pidhashtbl_lock[i]);
1616 sx_sunlock(&proctree_lock);
1621 struct kern_proc_out_args {
1622 struct sysctl_req *req;
1629 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1631 struct kern_proc_out_args *arg = origarg;
1632 int *name = arg->name;
1633 int oid_number = arg->oid_number;
1634 int flags = arg->flags;
1635 struct sysctl_req *req = arg->req;
1640 KASSERT(p->p_ucred != NULL,
1641 ("process credential is NULL for non-NEW proc"));
1643 * Show a user only appropriate processes.
1645 if (p_cansee(curthread, p))
1648 * TODO - make more efficient (see notes below).
1651 switch (oid_number) {
1653 if (p->p_ucred->cr_gid != (gid_t)name[0])
1657 case KERN_PROC_PGRP:
1658 /* could do this by traversing pgrp */
1659 if (p->p_pgrp == NULL ||
1660 p->p_pgrp->pg_id != (pid_t)name[0])
1664 case KERN_PROC_RGID:
1665 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1669 case KERN_PROC_SESSION:
1670 if (p->p_session == NULL ||
1671 p->p_session->s_sid != (pid_t)name[0])
1676 if ((p->p_flag & P_CONTROLT) == 0 ||
1677 p->p_session == NULL)
1679 /* XXX proctree_lock */
1680 SESS_LOCK(p->p_session);
1681 if (p->p_session->s_ttyp == NULL ||
1682 tty_udev(p->p_session->s_ttyp) !=
1684 SESS_UNLOCK(p->p_session);
1687 SESS_UNLOCK(p->p_session);
1691 if (p->p_ucred->cr_uid != (uid_t)name[0])
1695 case KERN_PROC_RUID:
1696 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1700 case KERN_PROC_PROC:
1706 error = sysctl_out_proc(p, req, flags);
1707 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1715 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1717 struct kern_proc_out_args iterarg;
1718 int *name = (int *)arg1;
1719 u_int namelen = arg2;
1721 int flags, oid_number;
1724 oid_number = oidp->oid_number;
1725 if (oid_number != KERN_PROC_ALL &&
1726 (oid_number & KERN_PROC_INC_THREAD) == 0)
1727 flags = KERN_PROC_NOTHREADS;
1730 oid_number &= ~KERN_PROC_INC_THREAD;
1732 #ifdef COMPAT_FREEBSD32
1733 if (req->flags & SCTL_MASK32)
1734 flags |= KERN_PROC_MASK32;
1736 if (oid_number == KERN_PROC_PID) {
1739 error = sysctl_wire_old_buffer(req, 0);
1742 sx_slock(&proctree_lock);
1743 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1745 error = sysctl_out_proc(p, req, flags);
1746 sx_sunlock(&proctree_lock);
1750 switch (oid_number) {
1755 case KERN_PROC_PROC:
1756 if (namelen != 0 && namelen != 1)
1765 if (req->oldptr == NULL) {
1766 /* overestimate by 5 procs */
1767 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1771 error = sysctl_wire_old_buffer(req, 0);
1775 iterarg.flags = flags;
1776 iterarg.oid_number = oid_number;
1778 iterarg.name = name;
1779 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1784 pargs_alloc(int len)
1788 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1790 refcount_init(&pa->ar_ref, 1);
1791 pa->ar_length = len;
1796 pargs_free(struct pargs *pa)
1803 pargs_hold(struct pargs *pa)
1808 refcount_acquire(&pa->ar_ref);
1812 pargs_drop(struct pargs *pa)
1817 if (refcount_release(&pa->ar_ref))
1822 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1828 * This may return a short read if the string is shorter than the chunk
1829 * and is aligned at the end of the page, and the following page is not
1832 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1838 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1840 enum proc_vector_type {
1846 #ifdef COMPAT_FREEBSD32
1848 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1849 size_t *vsizep, enum proc_vector_type type)
1851 struct freebsd32_ps_strings pss;
1853 vm_offset_t vptr, ptr;
1854 uint32_t *proc_vector32;
1860 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1865 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1866 vsize = pss.ps_nargvstr;
1867 if (vsize > ARG_MAX)
1869 size = vsize * sizeof(int32_t);
1872 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1873 vsize = pss.ps_nenvstr;
1874 if (vsize > ARG_MAX)
1876 size = vsize * sizeof(int32_t);
1879 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1880 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1883 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1884 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1887 if (aux.a_type == AT_NULL)
1891 if (aux.a_type != AT_NULL)
1894 size = vsize * sizeof(aux);
1897 KASSERT(0, ("Wrong proc vector type: %d", type));
1900 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1901 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1905 if (type == PROC_AUX) {
1906 *proc_vectorp = (char **)proc_vector32;
1910 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1911 for (i = 0; i < (int)vsize; i++)
1912 proc_vector[i] = PTRIN(proc_vector32[i]);
1913 *proc_vectorp = proc_vector;
1916 free(proc_vector32, M_TEMP);
1922 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1923 size_t *vsizep, enum proc_vector_type type)
1925 struct ps_strings pss;
1927 vm_offset_t vptr, ptr;
1932 #ifdef COMPAT_FREEBSD32
1933 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1934 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1936 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1941 vptr = (vm_offset_t)pss.ps_argvstr;
1942 vsize = pss.ps_nargvstr;
1943 if (vsize > ARG_MAX)
1945 size = vsize * sizeof(char *);
1948 vptr = (vm_offset_t)pss.ps_envstr;
1949 vsize = pss.ps_nenvstr;
1950 if (vsize > ARG_MAX)
1952 size = vsize * sizeof(char *);
1956 * The aux array is just above env array on the stack. Check
1957 * that the address is naturally aligned.
1959 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1961 #if __ELF_WORD_SIZE == 64
1962 if (vptr % sizeof(uint64_t) != 0)
1964 if (vptr % sizeof(uint32_t) != 0)
1968 * We count the array size reading the aux vectors from the
1969 * stack until AT_NULL vector is returned. So (to keep the code
1970 * simple) we read the process stack twice: the first time here
1971 * to find the size and the second time when copying the vectors
1972 * to the allocated proc_vector.
1974 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1975 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1978 if (aux.a_type == AT_NULL)
1983 * If the PROC_AUXV_MAX entries are iterated over, and we have
1984 * not reached AT_NULL, it is most likely we are reading wrong
1985 * data: either the process doesn't have auxv array or data has
1986 * been modified. Return the error in this case.
1988 if (aux.a_type != AT_NULL)
1991 size = vsize * sizeof(aux);
1994 KASSERT(0, ("Wrong proc vector type: %d", type));
1995 return (EINVAL); /* In case we are built without INVARIANTS. */
1997 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1998 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1999 free(proc_vector, M_TEMP);
2002 *proc_vectorp = proc_vector;
2008 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
2011 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
2012 enum proc_vector_type type)
2014 size_t done, len, nchr, vsize;
2016 char **proc_vector, *sptr;
2017 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2019 PROC_ASSERT_HELD(p);
2022 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2024 nchr = 2 * (PATH_MAX + ARG_MAX);
2026 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2029 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2031 * The program may have scribbled into its argv array, e.g. to
2032 * remove some arguments. If that has happened, break out
2033 * before trying to read from NULL.
2035 if (proc_vector[i] == NULL)
2037 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2038 error = proc_read_string(td, p, sptr, pss_string,
2039 sizeof(pss_string));
2042 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2043 if (done + len >= nchr)
2044 len = nchr - done - 1;
2045 sbuf_bcat(sb, pss_string, len);
2046 if (len != GET_PS_STRINGS_CHUNK_SZ)
2048 done += GET_PS_STRINGS_CHUNK_SZ;
2050 sbuf_bcat(sb, "", 1);
2054 free(proc_vector, M_TEMP);
2059 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2062 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2066 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2069 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2073 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2079 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2081 #ifdef COMPAT_FREEBSD32
2082 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2083 size = vsize * sizeof(Elf32_Auxinfo);
2086 size = vsize * sizeof(Elf_Auxinfo);
2087 if (sbuf_bcat(sb, auxv, size) != 0)
2095 * This sysctl allows a process to retrieve the argument list or process
2096 * title for another process without groping around in the address space
2097 * of the other process. It also allow a process to set its own "process
2098 * title to a string of its own choice.
2101 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2103 int *name = (int *)arg1;
2104 u_int namelen = arg2;
2105 struct pargs *newpa, *pa;
2108 int flags, error = 0, error2;
2115 pid = (pid_t)name[0];
2121 * If the query is for this process and it is single-threaded, there
2122 * is nobody to modify pargs, thus we can just read.
2124 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2125 (pa = p->p_args) != NULL)
2126 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2128 flags = PGET_CANSEE;
2129 if (req->newptr != NULL)
2130 flags |= PGET_ISCURRENT;
2131 error = pget(pid, flags, &p);
2139 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2141 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2144 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2145 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2146 error = proc_getargv(curthread, p, &sb);
2147 error2 = sbuf_finish(&sb);
2150 if (error == 0 && error2 != 0)
2155 if (error != 0 || req->newptr == NULL)
2158 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2161 if (req->newlen == 0) {
2163 * Clear the argument pointer, so that we'll fetch arguments
2164 * with proc_getargv() until further notice.
2168 newpa = pargs_alloc(req->newlen);
2169 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2184 * This sysctl allows a process to retrieve environment of another process.
2187 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2189 int *name = (int *)arg1;
2190 u_int namelen = arg2;
2198 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2201 if ((p->p_flag & P_SYSTEM) != 0) {
2206 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2207 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2208 error = proc_getenvv(curthread, p, &sb);
2209 error2 = sbuf_finish(&sb);
2212 return (error != 0 ? error : error2);
2216 * This sysctl allows a process to retrieve ELF auxiliary vector of
2220 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2222 int *name = (int *)arg1;
2223 u_int namelen = arg2;
2231 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2234 if ((p->p_flag & P_SYSTEM) != 0) {
2238 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2239 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2240 error = proc_getauxv(curthread, p, &sb);
2241 error2 = sbuf_finish(&sb);
2244 return (error != 0 ? error : error2);
2248 * Look up the canonical executable path running in the specified process.
2249 * It tries to return the same hardlink name as was used for execve(2).
2250 * This allows the programs that modify their behavior based on their progname,
2251 * to operate correctly.
2253 * Result is returned in retbuf, it must not be freed, similar to vn_fullpath()
2254 * calling conventions.
2255 * binname is a pointer to temporary string buffer of length MAXPATHLEN,
2256 * allocated and freed by caller.
2257 * freebuf should be freed by caller, from the M_TEMP malloc type.
2260 proc_get_binpath(struct proc *p, char *binname, char **retbuf,
2263 struct nameidata nd;
2264 struct vnode *vp, *dvp;
2265 size_t freepath_size;
2269 PROC_LOCK_ASSERT(p, MA_OWNED);
2282 if (p->p_binname != NULL)
2283 strlcpy(binname, p->p_binname, MAXPATHLEN);
2288 if (dvp != NULL && binname[0] != '\0') {
2289 freepath_size = MAXPATHLEN;
2290 if (vn_fullpath_hardlink(vp, dvp, binname, strlen(binname),
2291 retbuf, freebuf, &freepath_size) == 0) {
2293 * Recheck the looked up path. The binary
2294 * might have been renamed or replaced, in
2295 * which case we should not report old name.
2297 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, *retbuf,
2302 do_fullpath = false;
2304 NDFREE(&nd, NDF_ONLY_PNBUF);
2309 free(*freebuf, M_TEMP);
2311 error = vn_fullpath(vp, retbuf, freebuf);
2320 * This sysctl allows a process to retrieve the path of the executable for
2321 * itself or another process.
2324 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2326 pid_t *pidp = (pid_t *)arg1;
2327 unsigned int arglen = arg2;
2329 char *retbuf, *freebuf, *binname;
2334 binname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2336 if (*pidp == -1) { /* -1 means this process */
2338 p = req->td->td_proc;
2341 error = pget(*pidp, PGET_CANSEE, &p);
2345 error = proc_get_binpath(p, binname, &retbuf, &freebuf);
2346 free(binname, M_TEMP);
2349 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2350 free(freebuf, M_TEMP);
2355 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2368 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2371 sv_name = p->p_sysent->sv_name;
2373 return (sysctl_handle_string(oidp, sv_name, 0, req));
2376 #ifdef KINFO_OVMENTRY_SIZE
2377 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2380 #ifdef COMPAT_FREEBSD7
2382 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2384 vm_map_entry_t entry, tmp_entry;
2385 unsigned int last_timestamp, namelen;
2386 char *fullpath, *freepath;
2387 struct kinfo_ovmentry *kve;
2401 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2404 vm = vmspace_acquire_ref(p);
2409 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2412 vm_map_lock_read(map);
2413 VM_MAP_ENTRY_FOREACH(entry, map) {
2414 vm_object_t obj, tobj, lobj;
2417 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2420 bzero(kve, sizeof(*kve));
2421 kve->kve_structsize = sizeof(*kve);
2423 kve->kve_private_resident = 0;
2424 obj = entry->object.vm_object;
2426 VM_OBJECT_RLOCK(obj);
2427 if (obj->shadow_count == 1)
2428 kve->kve_private_resident =
2429 obj->resident_page_count;
2431 kve->kve_resident = 0;
2432 addr = entry->start;
2433 while (addr < entry->end) {
2434 if (pmap_extract(map->pmap, addr))
2435 kve->kve_resident++;
2439 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2441 VM_OBJECT_RLOCK(tobj);
2442 kve->kve_offset += tobj->backing_object_offset;
2445 VM_OBJECT_RUNLOCK(lobj);
2449 kve->kve_start = (void*)entry->start;
2450 kve->kve_end = (void*)entry->end;
2451 kve->kve_offset += (off_t)entry->offset;
2453 if (entry->protection & VM_PROT_READ)
2454 kve->kve_protection |= KVME_PROT_READ;
2455 if (entry->protection & VM_PROT_WRITE)
2456 kve->kve_protection |= KVME_PROT_WRITE;
2457 if (entry->protection & VM_PROT_EXECUTE)
2458 kve->kve_protection |= KVME_PROT_EXEC;
2460 if (entry->eflags & MAP_ENTRY_COW)
2461 kve->kve_flags |= KVME_FLAG_COW;
2462 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2463 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2464 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2465 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2467 last_timestamp = map->timestamp;
2468 vm_map_unlock_read(map);
2470 kve->kve_fileid = 0;
2475 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2476 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2477 kve->kve_type = KVME_TYPE_UNKNOWN;
2481 VM_OBJECT_RUNLOCK(lobj);
2483 kve->kve_ref_count = obj->ref_count;
2484 kve->kve_shadow_count = obj->shadow_count;
2485 VM_OBJECT_RUNLOCK(obj);
2487 vn_fullpath(vp, &fullpath, &freepath);
2488 cred = curthread->td_ucred;
2489 vn_lock(vp, LK_SHARED | LK_RETRY);
2490 if (VOP_GETATTR(vp, &va, cred) == 0) {
2491 kve->kve_fileid = va.va_fileid;
2493 kve->kve_fsid = va.va_fsid;
2498 kve->kve_type = KVME_TYPE_NONE;
2499 kve->kve_ref_count = 0;
2500 kve->kve_shadow_count = 0;
2503 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2504 if (freepath != NULL)
2505 free(freepath, M_TEMP);
2507 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2508 vm_map_lock_read(map);
2511 if (last_timestamp != map->timestamp) {
2512 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2516 vm_map_unlock_read(map);
2522 #endif /* COMPAT_FREEBSD7 */
2524 #ifdef KINFO_VMENTRY_SIZE
2525 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2529 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2530 int *resident_count, bool *super)
2532 vm_object_t obj, tobj;
2536 vm_pindex_t pi, pi_adv, pindex;
2539 *resident_count = 0;
2540 if (vmmap_skip_res_cnt)
2544 obj = entry->object.vm_object;
2545 addr = entry->start;
2547 pi = OFF_TO_IDX(entry->offset);
2548 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2549 if (m_adv != NULL) {
2552 pi_adv = atop(entry->end - addr);
2554 for (tobj = obj;; tobj = tobj->backing_object) {
2555 m = vm_page_find_least(tobj, pindex);
2557 if (m->pindex == pindex)
2559 if (pi_adv > m->pindex - pindex) {
2560 pi_adv = m->pindex - pindex;
2564 if (tobj->backing_object == NULL)
2566 pindex += OFF_TO_IDX(tobj->
2567 backing_object_offset);
2571 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2572 (addr & (pagesizes[1] - 1)) == 0 &&
2573 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2575 pi_adv = atop(pagesizes[1]);
2578 * We do not test the found page on validity.
2579 * Either the page is busy and being paged in,
2580 * or it was invalidated. The first case
2581 * should be counted as resident, the second
2582 * is not so clear; we do account both.
2586 *resident_count += pi_adv;
2592 * Must be called with the process locked and will return unlocked.
2595 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2597 vm_map_entry_t entry, tmp_entry;
2600 vm_object_t lobj, nobj, obj, tobj;
2601 char *fullpath, *freepath;
2602 struct kinfo_vmentry *kve;
2607 unsigned int last_timestamp;
2611 PROC_LOCK_ASSERT(p, MA_OWNED);
2615 vm = vmspace_acquire_ref(p);
2620 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2624 vm_map_lock_read(map);
2625 VM_MAP_ENTRY_FOREACH(entry, map) {
2626 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2630 bzero(kve, sizeof(*kve));
2631 obj = entry->object.vm_object;
2633 if ((obj->flags & OBJ_ANON) != 0)
2634 kve->kve_obj = (uintptr_t)obj;
2636 for (tobj = obj; tobj != NULL;
2637 tobj = tobj->backing_object) {
2638 VM_OBJECT_RLOCK(tobj);
2639 kve->kve_offset += tobj->backing_object_offset;
2642 if (obj->backing_object == NULL)
2643 kve->kve_private_resident =
2644 obj->resident_page_count;
2645 kern_proc_vmmap_resident(map, entry,
2646 &kve->kve_resident, &super);
2648 kve->kve_flags |= KVME_FLAG_SUPER;
2649 for (tobj = obj; tobj != NULL; tobj = nobj) {
2650 nobj = tobj->backing_object;
2651 if (tobj != obj && tobj != lobj)
2652 VM_OBJECT_RUNLOCK(tobj);
2658 kve->kve_start = entry->start;
2659 kve->kve_end = entry->end;
2660 kve->kve_offset += entry->offset;
2662 if (entry->protection & VM_PROT_READ)
2663 kve->kve_protection |= KVME_PROT_READ;
2664 if (entry->protection & VM_PROT_WRITE)
2665 kve->kve_protection |= KVME_PROT_WRITE;
2666 if (entry->protection & VM_PROT_EXECUTE)
2667 kve->kve_protection |= KVME_PROT_EXEC;
2669 if (entry->eflags & MAP_ENTRY_COW)
2670 kve->kve_flags |= KVME_FLAG_COW;
2671 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2672 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2673 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2674 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2675 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2676 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2677 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2678 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2679 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2680 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2682 guard = (entry->eflags & MAP_ENTRY_GUARD) != 0;
2684 last_timestamp = map->timestamp;
2685 vm_map_unlock_read(map);
2690 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2694 VM_OBJECT_RUNLOCK(lobj);
2696 kve->kve_ref_count = obj->ref_count;
2697 kve->kve_shadow_count = obj->shadow_count;
2698 VM_OBJECT_RUNLOCK(obj);
2700 vn_fullpath(vp, &fullpath, &freepath);
2701 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2702 cred = curthread->td_ucred;
2703 vn_lock(vp, LK_SHARED | LK_RETRY);
2704 if (VOP_GETATTR(vp, &va, cred) == 0) {
2705 kve->kve_vn_fileid = va.va_fileid;
2706 kve->kve_vn_fsid = va.va_fsid;
2707 kve->kve_vn_fsid_freebsd11 =
2708 kve->kve_vn_fsid; /* truncate */
2710 MAKEIMODE(va.va_type, va.va_mode);
2711 kve->kve_vn_size = va.va_size;
2712 kve->kve_vn_rdev = va.va_rdev;
2713 kve->kve_vn_rdev_freebsd11 =
2714 kve->kve_vn_rdev; /* truncate */
2715 kve->kve_status = KF_ATTR_VALID;
2720 kve->kve_type = guard ? KVME_TYPE_GUARD :
2722 kve->kve_ref_count = 0;
2723 kve->kve_shadow_count = 0;
2726 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2727 if (freepath != NULL)
2728 free(freepath, M_TEMP);
2730 /* Pack record size down */
2731 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2732 kve->kve_structsize =
2733 offsetof(struct kinfo_vmentry, kve_path) +
2734 strlen(kve->kve_path) + 1;
2736 kve->kve_structsize = sizeof(*kve);
2737 kve->kve_structsize = roundup(kve->kve_structsize,
2740 /* Halt filling and truncate rather than exceeding maxlen */
2741 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2743 vm_map_lock_read(map);
2745 } else if (maxlen != -1)
2746 maxlen -= kve->kve_structsize;
2748 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2750 vm_map_lock_read(map);
2753 if (last_timestamp != map->timestamp) {
2754 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2758 vm_map_unlock_read(map);
2766 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2771 int error, error2, *name;
2778 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2779 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2780 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2785 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2786 error2 = sbuf_finish(&sb);
2788 return (error != 0 ? error : error2);
2791 #if defined(STACK) || defined(DDB)
2793 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2795 struct kinfo_kstack *kkstp;
2796 int error, i, *name, numthreads;
2797 lwpid_t *lwpidarray;
2809 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2813 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2814 st = stack_create(M_WAITOK);
2819 if (lwpidarray != NULL) {
2820 free(lwpidarray, M_TEMP);
2823 numthreads = p->p_numthreads;
2825 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2828 } while (numthreads < p->p_numthreads);
2831 * XXXRW: During the below loop, execve(2) and countless other sorts
2832 * of changes could have taken place. Should we check to see if the
2833 * vmspace has been replaced, or the like, in order to prevent
2834 * giving a snapshot that spans, say, execve(2), with some threads
2835 * before and some after? Among other things, the credentials could
2836 * have changed, in which case the right to extract debug info might
2837 * no longer be assured.
2840 FOREACH_THREAD_IN_PROC(p, td) {
2841 KASSERT(i < numthreads,
2842 ("sysctl_kern_proc_kstack: numthreads"));
2843 lwpidarray[i] = td->td_tid;
2848 for (i = 0; i < numthreads; i++) {
2849 td = tdfind(lwpidarray[i], p->p_pid);
2853 bzero(kkstp, sizeof(*kkstp));
2854 (void)sbuf_new(&sb, kkstp->kkst_trace,
2855 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2857 kkstp->kkst_tid = td->td_tid;
2858 if (TD_IS_SWAPPED(td))
2859 kkstp->kkst_state = KKST_STATE_SWAPPED;
2860 else if (stack_save_td(st, td) == 0)
2861 kkstp->kkst_state = KKST_STATE_STACKOK;
2863 kkstp->kkst_state = KKST_STATE_RUNNING;
2866 stack_sbuf_print(&sb, st);
2869 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2874 if (lwpidarray != NULL)
2875 free(lwpidarray, M_TEMP);
2877 free(kkstp, M_TEMP);
2883 * This sysctl allows a process to retrieve the full list of groups from
2884 * itself or another process.
2887 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2889 pid_t *pidp = (pid_t *)arg1;
2890 unsigned int arglen = arg2;
2897 if (*pidp == -1) { /* -1 means this process */
2898 p = req->td->td_proc;
2901 error = pget(*pidp, PGET_CANSEE, &p);
2906 cred = crhold(p->p_ucred);
2909 error = SYSCTL_OUT(req, cred->cr_groups,
2910 cred->cr_ngroups * sizeof(gid_t));
2916 * This sysctl allows a process to retrieve or/and set the resource limit for
2920 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2922 int *name = (int *)arg1;
2923 u_int namelen = arg2;
2932 which = (u_int)name[1];
2933 if (which >= RLIM_NLIMITS)
2936 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2939 flags = PGET_HOLD | PGET_NOTWEXIT;
2940 if (req->newptr != NULL)
2941 flags |= PGET_CANDEBUG;
2943 flags |= PGET_CANSEE;
2944 error = pget((pid_t)name[0], flags, &p);
2951 if (req->oldptr != NULL) {
2953 lim_rlimit_proc(p, which, &rlim);
2956 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2963 if (req->newptr != NULL) {
2964 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2966 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2975 * This sysctl allows a process to retrieve ps_strings structure location of
2979 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2981 int *name = (int *)arg1;
2982 u_int namelen = arg2;
2984 vm_offset_t ps_strings;
2986 #ifdef COMPAT_FREEBSD32
2987 uint32_t ps_strings32;
2993 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2996 #ifdef COMPAT_FREEBSD32
2997 if ((req->flags & SCTL_MASK32) != 0) {
2999 * We return 0 if the 32 bit emulation request is for a 64 bit
3002 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
3003 PTROUT(PROC_PS_STRINGS(p)) : 0;
3005 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
3009 ps_strings = PROC_PS_STRINGS(p);
3011 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
3016 * This sysctl allows a process to retrieve umask of another process.
3019 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
3021 int *name = (int *)arg1;
3022 u_int namelen = arg2;
3031 pid = (pid_t)name[0];
3033 if (pid == p->p_pid || pid == 0) {
3034 cmask = p->p_pd->pd_cmask;
3038 error = pget(pid, PGET_WANTREAD, &p);
3042 cmask = p->p_pd->pd_cmask;
3045 error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
3050 * This sysctl allows a process to set and retrieve binary osreldate of
3054 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3056 int *name = (int *)arg1;
3057 u_int namelen = arg2;
3059 int flags, error, osrel;
3064 if (req->newptr != NULL && req->newlen != sizeof(osrel))
3067 flags = PGET_HOLD | PGET_NOTWEXIT;
3068 if (req->newptr != NULL)
3069 flags |= PGET_CANDEBUG;
3071 flags |= PGET_CANSEE;
3072 error = pget((pid_t)name[0], flags, &p);
3076 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3080 if (req->newptr != NULL) {
3081 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3096 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
3098 int *name = (int *)arg1;
3099 u_int namelen = arg2;
3101 struct kinfo_sigtramp kst;
3102 const struct sysentvec *sv;
3104 #ifdef COMPAT_FREEBSD32
3105 struct kinfo_sigtramp32 kst32;
3111 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3115 #ifdef COMPAT_FREEBSD32
3116 if ((req->flags & SCTL_MASK32) != 0) {
3117 bzero(&kst32, sizeof(kst32));
3118 if (SV_PROC_FLAG(p, SV_ILP32)) {
3119 if (sv->sv_sigcode_base != 0) {
3120 kst32.ksigtramp_start = sv->sv_sigcode_base;
3121 kst32.ksigtramp_end = sv->sv_sigcode_base +
3122 ((sv->sv_flags & SV_DSO_SIG) == 0 ?
3124 (uintptr_t)sv->sv_szsigcode);
3126 kst32.ksigtramp_start = PROC_PS_STRINGS(p) -
3128 kst32.ksigtramp_end = PROC_PS_STRINGS(p);
3132 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3136 bzero(&kst, sizeof(kst));
3137 if (sv->sv_sigcode_base != 0) {
3138 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3139 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3140 ((sv->sv_flags & SV_DSO_SIG) == 0 ? *sv->sv_szsigcode :
3141 (uintptr_t)sv->sv_szsigcode);
3143 kst.ksigtramp_start = (char *)PROC_PS_STRINGS(p) -
3145 kst.ksigtramp_end = (char *)PROC_PS_STRINGS(p);
3148 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3153 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3155 int *name = (int *)arg1;
3156 u_int namelen = arg2;
3161 #ifdef COMPAT_FREEBSD32
3166 if (namelen != 1 || req->newptr != NULL)
3169 pid = (pid_t)name[0];
3170 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3175 #ifdef COMPAT_FREEBSD32
3176 if (SV_CURPROC_FLAG(SV_ILP32)) {
3177 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3183 if (pid <= PID_MAX) {
3184 td1 = FIRST_THREAD_IN_PROC(p);
3186 FOREACH_THREAD_IN_PROC(p, td1) {
3187 if (td1->td_tid == pid)
3196 * The access to the private thread flags. It is fine as far
3197 * as no out-of-thin-air values are read from td_pflags, and
3198 * usermode read of the td_sigblock_ptr is racy inherently,
3199 * since target process might have already changed it
3202 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3203 addr = (uintptr_t)td1->td_sigblock_ptr;
3213 #ifdef COMPAT_FREEBSD32
3214 if (SV_CURPROC_FLAG(SV_ILP32)) {
3216 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3219 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3223 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3226 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3227 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3228 "Return entire process table");
3230 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3231 sysctl_kern_proc, "Process table");
3233 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3234 sysctl_kern_proc, "Process table");
3236 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3237 sysctl_kern_proc, "Process table");
3239 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3240 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3242 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3243 sysctl_kern_proc, "Process table");
3245 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3246 sysctl_kern_proc, "Process table");
3248 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3249 sysctl_kern_proc, "Process table");
3251 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3252 sysctl_kern_proc, "Process table");
3254 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3255 sysctl_kern_proc, "Return process table, no threads");
3257 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3258 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3259 sysctl_kern_proc_args, "Process argument list");
3261 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3262 sysctl_kern_proc_env, "Process environment");
3264 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3265 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3267 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3268 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3270 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3271 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3272 "Process syscall vector name (ABI type)");
3274 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3275 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3277 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3278 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3280 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3281 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3283 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3284 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3286 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3287 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3289 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3290 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3292 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3293 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3295 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3296 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3298 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3299 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3300 "Return process table, including threads");
3302 #ifdef COMPAT_FREEBSD7
3303 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3304 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3307 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3308 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3310 #if defined(STACK) || defined(DDB)
3311 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3312 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3315 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3316 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3318 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3319 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3320 "Process resource limits");
3322 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3323 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3324 "Process ps_strings location");
3326 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3327 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3329 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3330 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3331 "Process binary osreldate");
3333 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3334 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3335 "Process signal trampoline location");
3337 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3338 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3339 "Thread sigfastblock address");
3341 static struct sx stop_all_proc_blocker;
3342 SX_SYSINIT(stop_all_proc_blocker, &stop_all_proc_blocker, "sapblk");
3345 stop_all_proc_block(void)
3347 return (sx_xlock_sig(&stop_all_proc_blocker) == 0);
3351 stop_all_proc_unblock(void)
3353 sx_xunlock(&stop_all_proc_blocker);
3359 * stop_all_proc() purpose is to stop all process which have usermode,
3360 * except current process for obvious reasons. This makes it somewhat
3361 * unreliable when invoked from multithreaded process. The service
3362 * must not be user-callable anyway.
3367 struct proc *cp, *p;
3369 bool restart, seen_stopped, seen_exiting, stopped_some;
3371 if (!stop_all_proc_block())
3376 sx_xlock(&allproc_lock);
3378 seen_exiting = seen_stopped = stopped_some = restart = false;
3379 LIST_REMOVE(cp, p_list);
3380 LIST_INSERT_HEAD(&allproc, cp, p_list);
3382 p = LIST_NEXT(cp, p_list);
3385 LIST_REMOVE(cp, p_list);
3386 LIST_INSERT_AFTER(p, cp, p_list);
3388 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3392 if ((p->p_flag2 & P2_WEXIT) != 0) {
3393 seen_exiting = true;
3397 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3399 * Stopped processes are tolerated when there
3400 * are no other processes which might continue
3401 * them. P_STOPPED_SINGLE but not
3402 * P_TOTAL_STOP process still has at least one
3405 seen_stopped = true;
3409 sx_xunlock(&allproc_lock);
3411 r = thread_single(p, SINGLE_ALLPROC);
3415 stopped_some = true;
3418 sx_xlock(&allproc_lock);
3420 /* Catch forked children we did not see in iteration. */
3421 if (gen != allproc_gen)
3423 sx_xunlock(&allproc_lock);
3424 if (restart || stopped_some || seen_exiting || seen_stopped) {
3425 kern_yield(PRI_USER);
3431 resume_all_proc(void)
3433 struct proc *cp, *p;
3436 sx_xlock(&allproc_lock);
3438 LIST_REMOVE(cp, p_list);
3439 LIST_INSERT_HEAD(&allproc, cp, p_list);
3441 p = LIST_NEXT(cp, p_list);
3444 LIST_REMOVE(cp, p_list);
3445 LIST_INSERT_AFTER(p, cp, p_list);
3447 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3448 sx_xunlock(&allproc_lock);
3450 thread_single_end(p, SINGLE_ALLPROC);
3453 sx_xlock(&allproc_lock);
3458 /* Did the loop above missed any stopped process ? */
3459 FOREACH_PROC_IN_SYSTEM(p) {
3460 /* No need for proc lock. */
3461 if ((p->p_flag & P_TOTAL_STOP) != 0)
3464 sx_xunlock(&allproc_lock);
3466 stop_all_proc_unblock();
3469 /* #define TOTAL_STOP_DEBUG 1 */
3470 #ifdef TOTAL_STOP_DEBUG
3471 volatile static int ap_resume;
3472 #include <sys/mount.h>
3475 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3481 error = sysctl_handle_int(oidp, &val, 0, req);
3482 if (error != 0 || req->newptr == NULL)
3487 while (ap_resume == 0)
3495 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3496 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3497 sysctl_debug_stop_all_proc, "I",