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>
48 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/limits.h>
53 #include <sys/loginclass.h>
54 #include <sys/malloc.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
60 #include <sys/ptrace.h>
61 #include <sys/refcount.h>
62 #include <sys/resourcevar.h>
63 #include <sys/rwlock.h>
65 #include <sys/sysent.h>
66 #include <sys/sched.h>
68 #include <sys/stack.h>
70 #include <sys/dtrace_bsd.h>
71 #include <sys/sysctl.h>
72 #include <sys/filedesc.h>
74 #include <sys/signalvar.h>
78 #include <sys/vnode.h>
81 #include <sys/ktrace.h>
89 #include <vm/vm_param.h>
90 #include <vm/vm_extern.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
97 #include <fs/devfs/devfs.h>
99 #ifdef COMPAT_FREEBSD32
100 #include <compat/freebsd32/freebsd32.h>
101 #include <compat/freebsd32/freebsd32_util.h>
104 SDT_PROVIDER_DEFINE(proc);
106 MALLOC_DEFINE(M_SESSION, "session", "session header");
107 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
108 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
110 static void doenterpgrp(struct proc *, struct pgrp *);
111 static void orphanpg(struct pgrp *pg);
112 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
113 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
114 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
116 static void pgdelete(struct pgrp *);
117 static int pgrp_init(void *mem, int size, int flags);
118 static int proc_ctor(void *mem, int size, void *arg, int flags);
119 static void proc_dtor(void *mem, int size, void *arg);
120 static int proc_init(void *mem, int size, int flags);
121 static void proc_fini(void *mem, int size);
122 static void pargs_free(struct pargs *pa);
125 * Other process lists
127 struct pidhashhead *pidhashtbl = NULL;
128 struct sx *pidhashtbl_lock;
131 struct pgrphashhead *pgrphashtbl;
133 struct proclist allproc = LIST_HEAD_INITIALIZER(allproc);
134 struct sx __exclusive_cache_line allproc_lock;
135 struct sx __exclusive_cache_line proctree_lock;
136 struct mtx __exclusive_cache_line ppeers_lock;
137 struct mtx __exclusive_cache_line procid_lock;
138 uma_zone_t proc_zone;
139 uma_zone_t pgrp_zone;
142 * The offset of various fields in struct proc and struct thread.
143 * These are used by kernel debuggers to enumerate kernel threads and
146 const int proc_off_p_pid = offsetof(struct proc, p_pid);
147 const int proc_off_p_comm = offsetof(struct proc, p_comm);
148 const int proc_off_p_list = offsetof(struct proc, p_list);
149 const int proc_off_p_hash = offsetof(struct proc, p_hash);
150 const int proc_off_p_threads = offsetof(struct proc, p_threads);
151 const int thread_off_td_tid = offsetof(struct thread, td_tid);
152 const int thread_off_td_name = offsetof(struct thread, td_name);
153 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
154 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
155 const int thread_off_td_plist = offsetof(struct thread, td_plist);
157 EVENTHANDLER_LIST_DEFINE(process_ctor);
158 EVENTHANDLER_LIST_DEFINE(process_dtor);
159 EVENTHANDLER_LIST_DEFINE(process_init);
160 EVENTHANDLER_LIST_DEFINE(process_fini);
161 EVENTHANDLER_LIST_DEFINE(process_exit);
162 EVENTHANDLER_LIST_DEFINE(process_fork);
163 EVENTHANDLER_LIST_DEFINE(process_exec);
165 int kstack_pages = KSTACK_PAGES;
166 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
167 "Kernel stack size in pages");
168 static int vmmap_skip_res_cnt = 0;
169 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
170 &vmmap_skip_res_cnt, 0,
171 "Skip calculation of the pages resident count in kern.proc.vmmap");
173 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
174 #ifdef COMPAT_FREEBSD32
175 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
179 * Initialize global process hashing structures.
186 sx_init(&allproc_lock, "allproc");
187 sx_init(&proctree_lock, "proctree");
188 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
189 mtx_init(&procid_lock, "procid", NULL, MTX_DEF);
190 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
191 pidhashlock = (pidhash + 1) / 64;
194 pidhashtbl_lock = malloc(sizeof(*pidhashtbl_lock) * (pidhashlock + 1),
195 M_PROC, M_WAITOK | M_ZERO);
196 for (i = 0; i < pidhashlock + 1; i++)
197 sx_init_flags(&pidhashtbl_lock[i], "pidhash", SX_DUPOK);
198 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
199 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
200 proc_ctor, proc_dtor, proc_init, proc_fini,
201 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
202 pgrp_zone = uma_zcreate("PGRP", sizeof(struct pgrp), NULL, NULL,
203 pgrp_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
208 * Prepare a proc for use.
211 proc_ctor(void *mem, int size, void *arg, int flags)
216 p = (struct proc *)mem;
218 kdtrace_proc_ctor(p);
220 EVENTHANDLER_DIRECT_INVOKE(process_ctor, p);
221 td = FIRST_THREAD_IN_PROC(p);
223 /* Make sure all thread constructors are executed */
224 EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td);
230 * Reclaim a proc after use.
233 proc_dtor(void *mem, int size, void *arg)
238 /* INVARIANTS checks go here */
239 p = (struct proc *)mem;
240 td = FIRST_THREAD_IN_PROC(p);
243 KASSERT((p->p_numthreads == 1),
244 ("bad number of threads in exiting process"));
245 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
247 /* Free all OSD associated to this thread. */
249 td_softdep_cleanup(td);
250 MPASS(td->td_su == NULL);
252 /* Make sure all thread destructors are executed */
253 EVENTHANDLER_DIRECT_INVOKE(thread_dtor, td);
255 EVENTHANDLER_DIRECT_INVOKE(process_dtor, p);
257 kdtrace_proc_dtor(p);
259 if (p->p_ksi != NULL)
260 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
264 * Initialize type-stable parts of a proc (when newly created).
267 proc_init(void *mem, int size, int flags)
271 p = (struct proc *)mem;
272 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
273 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
274 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
275 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
276 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
277 cv_init(&p->p_pwait, "ppwait");
278 TAILQ_INIT(&p->p_threads); /* all threads in proc */
279 EVENTHANDLER_DIRECT_INVOKE(process_init, p);
280 p->p_stats = pstats_alloc();
286 * UMA should ensure that this function is never called.
287 * Freeing a proc structure would violate type stability.
290 proc_fini(void *mem, int size)
295 p = (struct proc *)mem;
296 EVENTHANDLER_DIRECT_INVOKE(process_fini, p);
297 pstats_free(p->p_stats);
298 thread_free(FIRST_THREAD_IN_PROC(p));
299 mtx_destroy(&p->p_mtx);
300 if (p->p_ksi != NULL)
301 ksiginfo_free(p->p_ksi);
303 panic("proc reclaimed");
308 pgrp_init(void *mem, int size, int flags)
313 mtx_init(&pg->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
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)
578 sx_assert(&proctree_lock, SX_XLOCKED);
580 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
581 KASSERT(p->p_pid == pgid,
582 ("enterpgrp: new pgrp and pid != pgid"));
583 KASSERT(pgfind(pgid) == NULL,
584 ("enterpgrp: pgrp with pgid exists"));
585 KASSERT(!SESS_LEADER(p),
586 ("enterpgrp: session leader attempted setpgrp"));
592 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
594 p->p_flag &= ~P_CONTROLT;
598 sess->s_sid = p->p_pid;
599 proc_id_set(PROC_ID_SESSION, p->p_pid);
600 refcount_init(&sess->s_count, 1);
601 sess->s_ttyvp = NULL;
602 sess->s_ttydp = NULL;
604 bcopy(p->p_session->s_login, sess->s_login,
605 sizeof(sess->s_login));
606 pgrp->pg_session = sess;
607 KASSERT(p == curproc,
608 ("enterpgrp: mksession and p != curproc"));
610 pgrp->pg_session = p->p_session;
611 sess_hold(pgrp->pg_session);
615 proc_id_set(PROC_ID_GROUP, p->p_pid);
616 LIST_INIT(&pgrp->pg_members);
620 * As we have an exclusive lock of proctree_lock,
621 * this should not deadlock.
623 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
624 SLIST_INIT(&pgrp->pg_sigiolst);
627 doenterpgrp(p, pgrp);
633 * Move p to an existing process group
636 enterthispgrp(struct proc *p, struct pgrp *pgrp)
639 sx_assert(&proctree_lock, SX_XLOCKED);
640 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
641 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
642 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
643 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
644 KASSERT(pgrp->pg_session == p->p_session,
645 ("%s: pgrp's session %p, p->p_session %p proc %p\n",
646 __func__, pgrp->pg_session, p->p_session, p));
647 KASSERT(pgrp != p->p_pgrp,
648 ("%s: p %p belongs to pgrp %p", __func__, p, pgrp));
650 doenterpgrp(p, pgrp);
656 * If true, any child of q which belongs to group pgrp, qualifies the
657 * process group pgrp as not orphaned.
660 isjobproc(struct proc *q, struct pgrp *pgrp)
662 sx_assert(&proctree_lock, SX_LOCKED);
664 return (q->p_pgrp != pgrp &&
665 q->p_pgrp->pg_session == pgrp->pg_session);
669 jobc_reaper(struct proc *p)
673 sx_assert(&proctree_lock, SA_LOCKED);
677 if (pp->p_reaper == pp ||
678 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
684 jobc_parent(struct proc *p, struct proc *p_exiting)
688 sx_assert(&proctree_lock, SA_LOCKED);
690 pp = proc_realparent(p);
691 if (pp->p_pptr == NULL || pp == p_exiting ||
692 (pp->p_treeflag & P_TREE_GRPEXITED) == 0)
694 return (jobc_reaper(pp));
698 pgrp_calc_jobc(struct pgrp *pgrp)
704 if (!mtx_owned(&pgrp->pg_mtx))
705 sx_assert(&proctree_lock, SA_LOCKED);
709 LIST_FOREACH(q, &pgrp->pg_members, p_pglist) {
710 if ((q->p_treeflag & P_TREE_GRPEXITED) != 0 ||
713 if (isjobproc(jobc_parent(q, NULL), pgrp))
720 * Move p to a process group
723 doenterpgrp(struct proc *p, struct pgrp *pgrp)
725 struct pgrp *savepgrp;
728 sx_assert(&proctree_lock, SX_XLOCKED);
729 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
730 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
731 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
732 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
734 savepgrp = p->p_pgrp;
735 pp = jobc_parent(p, NULL);
739 if (isjobproc(pp, savepgrp) && pgrp_calc_jobc(savepgrp) == 1)
742 LIST_REMOVE(p, p_pglist);
745 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
746 if (isjobproc(pp, pgrp))
747 pgrp->pg_flags &= ~PGRP_ORPHANED;
748 PGRP_UNLOCK(savepgrp);
750 if (LIST_EMPTY(&savepgrp->pg_members))
755 * remove process from process group
758 leavepgrp(struct proc *p)
760 struct pgrp *savepgrp;
762 sx_assert(&proctree_lock, SX_XLOCKED);
763 savepgrp = p->p_pgrp;
766 LIST_REMOVE(p, p_pglist);
769 PGRP_UNLOCK(savepgrp);
770 if (LIST_EMPTY(&savepgrp->pg_members))
776 * delete a process group
779 pgdelete(struct pgrp *pgrp)
781 struct session *savesess;
784 sx_assert(&proctree_lock, SX_XLOCKED);
785 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
786 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
789 * Reset any sigio structures pointing to us as a result of
790 * F_SETOWN with our pgid. The proctree lock ensures that
791 * new sigio structures will not be added after this point.
793 funsetownlst(&pgrp->pg_sigiolst);
796 tp = pgrp->pg_session->s_ttyp;
797 LIST_REMOVE(pgrp, pg_hash);
798 savesess = pgrp->pg_session;
801 /* Remove the reference to the pgrp before deallocating it. */
804 tty_rel_pgrp(tp, pgrp);
807 proc_id_clear(PROC_ID_GROUP, pgrp->pg_id);
808 uma_zfree(pgrp_zone, pgrp);
809 sess_release(savesess);
814 fixjobc_kill(struct proc *p)
819 sx_assert(&proctree_lock, SX_LOCKED);
820 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
822 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
823 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
826 * p no longer affects process group orphanage for children.
827 * It is marked by the flag because p is only physically
828 * removed from its process group on wait(2).
830 MPASS((p->p_treeflag & P_TREE_GRPEXITED) == 0);
831 p->p_treeflag |= P_TREE_GRPEXITED;
834 * Check if exiting p orphans its own group.
837 if (isjobproc(jobc_parent(p, NULL), pgrp)) {
839 if (pgrp_calc_jobc(pgrp) == 0)
845 * Check this process' children to see whether they qualify
846 * their process groups after reparenting to reaper.
848 LIST_FOREACH(q, &p->p_children, p_sibling) {
851 if (pgrp_calc_jobc(pgrp) == 0) {
853 * We want to handle exactly the children that
854 * has p as realparent. Then, when calculating
855 * jobc_parent for children, we should ignore
856 * P_TREE_GRPEXITED flag already set on p.
858 if (jobc_parent(q, p) == p && isjobproc(p, pgrp))
861 pgrp->pg_flags &= ~PGRP_ORPHANED;
864 LIST_FOREACH(q, &p->p_orphans, p_orphan) {
867 if (pgrp_calc_jobc(pgrp) == 0) {
868 if (isjobproc(p, pgrp))
871 pgrp->pg_flags &= ~PGRP_ORPHANED;
885 MPASS(p->p_flag & P_WEXIT);
886 sx_assert(&proctree_lock, SX_LOCKED);
888 if (SESS_LEADER(p)) {
892 * s_ttyp is not zero'd; we use this to indicate that
893 * the session once had a controlling terminal. (for
894 * logging and informational purposes)
905 * Signal foreground pgrp and revoke access to
906 * controlling terminal if it has not been revoked
909 * Because the TTY may have been revoked in the mean
910 * time and could already have a new session associated
911 * with it, make sure we don't send a SIGHUP to a
912 * foreground process group that does not belong to this
918 if (tp->t_session == sp)
919 tty_signal_pgrp(tp, SIGHUP);
924 sx_xunlock(&proctree_lock);
925 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
926 VOP_REVOKE(ttyvp, REVOKEALL);
929 devfs_ctty_unref(ttyvp);
930 sx_xlock(&proctree_lock);
937 * A process group has become orphaned, mark it as such for signal
938 * delivery code. If there are any stopped processes in the group,
939 * hang-up all process in that group.
942 orphanpg(struct pgrp *pg)
946 PGRP_LOCK_ASSERT(pg, MA_OWNED);
948 pg->pg_flags |= PGRP_ORPHANED;
950 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
952 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
954 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
956 kern_psignal(p, SIGHUP);
957 kern_psignal(p, SIGCONT);
967 sess_hold(struct session *s)
970 refcount_acquire(&s->s_count);
974 sess_release(struct session *s)
977 if (refcount_release(&s->s_count)) {
978 if (s->s_ttyp != NULL) {
980 tty_rel_sess(s->s_ttyp, s);
982 proc_id_clear(PROC_ID_SESSION, s->s_sid);
983 mtx_destroy(&s->s_mtx);
991 db_print_pgrp_one(struct pgrp *pgrp, struct proc *p)
994 " pid %d at %p pr %d pgrp %p e %d jc %d\n",
995 p->p_pid, p, p->p_pptr == NULL ? -1 : p->p_pptr->p_pid,
996 p->p_pgrp, (p->p_treeflag & P_TREE_GRPEXITED) != 0,
997 p->p_pptr == NULL ? 0 : isjobproc(p->p_pptr, pgrp));
1000 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
1006 for (i = 0; i <= pgrphash; i++) {
1007 if (!LIST_EMPTY(&pgrphashtbl[i])) {
1008 db_printf("indx %d\n", i);
1009 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
1011 " pgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
1012 pgrp, (int)pgrp->pg_id, pgrp->pg_session,
1013 pgrp->pg_session->s_count,
1014 LIST_FIRST(&pgrp->pg_members));
1015 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
1016 db_print_pgrp_one(pgrp, p);
1024 * Calculate the kinfo_proc members which contain process-wide
1026 * Must be called with the target process locked.
1029 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
1033 PROC_LOCK_ASSERT(p, MA_OWNED);
1037 FOREACH_THREAD_IN_PROC(p, td) {
1039 kp->ki_pctcpu += sched_pctcpu(td);
1040 kp->ki_estcpu += sched_estcpu(td);
1046 * Fill in any information that is common to all threads in the process.
1047 * Must be called with the target process locked.
1050 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
1055 struct timeval boottime;
1057 PROC_LOCK_ASSERT(p, MA_OWNED);
1059 kp->ki_structsize = sizeof(*kp);
1061 kp->ki_addr =/* p->p_addr; */0; /* XXX */
1062 kp->ki_args = p->p_args;
1063 kp->ki_textvp = p->p_textvp;
1065 kp->ki_tracep = ktr_get_tracevp(p, false);
1066 kp->ki_traceflag = p->p_traceflag;
1068 kp->ki_fd = p->p_fd;
1069 kp->ki_pd = p->p_pd;
1070 kp->ki_vmspace = p->p_vmspace;
1071 kp->ki_flag = p->p_flag;
1072 kp->ki_flag2 = p->p_flag2;
1075 kp->ki_uid = cred->cr_uid;
1076 kp->ki_ruid = cred->cr_ruid;
1077 kp->ki_svuid = cred->cr_svuid;
1078 kp->ki_cr_flags = 0;
1079 if (cred->cr_flags & CRED_FLAG_CAPMODE)
1080 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
1081 /* XXX bde doesn't like KI_NGROUPS */
1082 if (cred->cr_ngroups > KI_NGROUPS) {
1083 kp->ki_ngroups = KI_NGROUPS;
1084 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
1086 kp->ki_ngroups = cred->cr_ngroups;
1087 bcopy(cred->cr_groups, kp->ki_groups,
1088 kp->ki_ngroups * sizeof(gid_t));
1089 kp->ki_rgid = cred->cr_rgid;
1090 kp->ki_svgid = cred->cr_svgid;
1091 /* If jailed(cred), emulate the old P_JAILED flag. */
1093 kp->ki_flag |= P_JAILED;
1094 /* If inside the jail, use 0 as a jail ID. */
1095 if (cred->cr_prison != curthread->td_ucred->cr_prison)
1096 kp->ki_jid = cred->cr_prison->pr_id;
1098 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
1099 sizeof(kp->ki_loginclass));
1103 mtx_lock(&ps->ps_mtx);
1104 kp->ki_sigignore = ps->ps_sigignore;
1105 kp->ki_sigcatch = ps->ps_sigcatch;
1106 mtx_unlock(&ps->ps_mtx);
1108 if (p->p_state != PRS_NEW &&
1109 p->p_state != PRS_ZOMBIE &&
1110 p->p_vmspace != NULL) {
1111 struct vmspace *vm = p->p_vmspace;
1113 kp->ki_size = vm->vm_map.size;
1114 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
1115 FOREACH_THREAD_IN_PROC(p, td0) {
1116 if (!TD_IS_SWAPPED(td0))
1117 kp->ki_rssize += td0->td_kstack_pages;
1119 kp->ki_swrss = vm->vm_swrss;
1120 kp->ki_tsize = vm->vm_tsize;
1121 kp->ki_dsize = vm->vm_dsize;
1122 kp->ki_ssize = vm->vm_ssize;
1123 } else if (p->p_state == PRS_ZOMBIE)
1124 kp->ki_stat = SZOMB;
1125 if (kp->ki_flag & P_INMEM)
1126 kp->ki_sflag = PS_INMEM;
1129 /* Calculate legacy swtime as seconds since 'swtick'. */
1130 kp->ki_swtime = (ticks - p->p_swtick) / hz;
1131 kp->ki_pid = p->p_pid;
1132 kp->ki_nice = p->p_nice;
1133 kp->ki_fibnum = p->p_fibnum;
1134 kp->ki_start = p->p_stats->p_start;
1135 getboottime(&boottime);
1136 timevaladd(&kp->ki_start, &boottime);
1138 rufetch(p, &kp->ki_rusage);
1139 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
1140 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
1142 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
1143 /* Some callers want child times in a single value. */
1144 kp->ki_childtime = kp->ki_childstime;
1145 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
1147 FOREACH_THREAD_IN_PROC(p, td0)
1148 kp->ki_cow += td0->td_cow;
1150 if (p->p_comm[0] != '\0')
1151 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1152 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1153 p->p_sysent->sv_name[0] != '\0')
1154 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1155 kp->ki_siglist = p->p_siglist;
1156 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1157 kp->ki_acflag = p->p_acflag;
1158 kp->ki_lock = p->p_lock;
1160 kp->ki_ppid = p->p_oppid;
1161 if (p->p_flag & P_TRACED)
1162 kp->ki_tracer = p->p_pptr->p_pid;
1167 * Fill job-related process information.
1170 fill_kinfo_proc_pgrp(struct proc *p, struct kinfo_proc *kp)
1176 sx_assert(&proctree_lock, SA_LOCKED);
1177 PROC_LOCK_ASSERT(p, MA_OWNED);
1183 kp->ki_pgid = pgrp->pg_id;
1184 kp->ki_jobc = pgrp_calc_jobc(pgrp);
1186 sp = pgrp->pg_session;
1190 kp->ki_sid = sp->s_sid;
1192 strlcpy(kp->ki_login, sp->s_login, sizeof(kp->ki_login));
1194 kp->ki_kiflag |= KI_CTTY;
1196 kp->ki_kiflag |= KI_SLEADER;
1201 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
1202 kp->ki_tdev = tty_udev(tp);
1203 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1204 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1206 kp->ki_tsid = tp->t_session->s_sid;
1208 kp->ki_tdev = NODEV;
1209 kp->ki_tdev_freebsd11 = kp->ki_tdev; /* truncate */
1214 * Fill in information that is thread specific. Must be called with
1215 * target process locked. If 'preferthread' is set, overwrite certain
1216 * process-related fields that are maintained for both threads and
1220 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1226 PROC_LOCK_ASSERT(p, MA_OWNED);
1231 if (td->td_wmesg != NULL)
1232 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1234 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1235 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1236 sizeof(kp->ki_tdname)) {
1237 strlcpy(kp->ki_moretdname,
1238 td->td_name + sizeof(kp->ki_tdname) - 1,
1239 sizeof(kp->ki_moretdname));
1241 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1243 if (TD_ON_LOCK(td)) {
1244 kp->ki_kiflag |= KI_LOCKBLOCK;
1245 strlcpy(kp->ki_lockname, td->td_lockname,
1246 sizeof(kp->ki_lockname));
1248 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1249 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1252 if (p->p_state == PRS_NORMAL) { /* approximate. */
1253 if (TD_ON_RUNQ(td) ||
1255 TD_IS_RUNNING(td)) {
1257 } else if (P_SHOULDSTOP(p)) {
1258 kp->ki_stat = SSTOP;
1259 } else if (TD_IS_SLEEPING(td)) {
1260 kp->ki_stat = SSLEEP;
1261 } else if (TD_ON_LOCK(td)) {
1262 kp->ki_stat = SLOCK;
1264 kp->ki_stat = SWAIT;
1266 } else if (p->p_state == PRS_ZOMBIE) {
1267 kp->ki_stat = SZOMB;
1272 /* Things in the thread */
1273 kp->ki_wchan = td->td_wchan;
1274 kp->ki_pri.pri_level = td->td_priority;
1275 kp->ki_pri.pri_native = td->td_base_pri;
1278 * Note: legacy fields; clamp at the old NOCPU value and/or
1279 * the maximum u_char CPU value.
1281 if (td->td_lastcpu == NOCPU)
1282 kp->ki_lastcpu_old = NOCPU_OLD;
1283 else if (td->td_lastcpu > MAXCPU_OLD)
1284 kp->ki_lastcpu_old = MAXCPU_OLD;
1286 kp->ki_lastcpu_old = td->td_lastcpu;
1288 if (td->td_oncpu == NOCPU)
1289 kp->ki_oncpu_old = NOCPU_OLD;
1290 else if (td->td_oncpu > MAXCPU_OLD)
1291 kp->ki_oncpu_old = MAXCPU_OLD;
1293 kp->ki_oncpu_old = td->td_oncpu;
1295 kp->ki_lastcpu = td->td_lastcpu;
1296 kp->ki_oncpu = td->td_oncpu;
1297 kp->ki_tdflags = td->td_flags;
1298 kp->ki_tid = td->td_tid;
1299 kp->ki_numthreads = p->p_numthreads;
1300 kp->ki_pcb = td->td_pcb;
1301 kp->ki_kstack = (void *)td->td_kstack;
1302 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1303 kp->ki_pri.pri_class = td->td_pri_class;
1304 kp->ki_pri.pri_user = td->td_user_pri;
1307 rufetchtd(td, &kp->ki_rusage);
1308 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1309 kp->ki_pctcpu = sched_pctcpu(td);
1310 kp->ki_estcpu = sched_estcpu(td);
1311 kp->ki_cow = td->td_cow;
1314 /* We can't get this anymore but ps etc never used it anyway. */
1318 kp->ki_siglist = td->td_siglist;
1319 kp->ki_sigmask = td->td_sigmask;
1326 * Fill in a kinfo_proc structure for the specified process.
1327 * Must be called with the target process locked.
1330 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1332 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1334 bzero(kp, sizeof(*kp));
1336 fill_kinfo_proc_pgrp(p,kp);
1337 fill_kinfo_proc_only(p, kp);
1338 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1339 fill_kinfo_aggregate(p, kp);
1346 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1350 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1353 pstats_fork(struct pstats *src, struct pstats *dst)
1356 bzero(&dst->pstat_startzero,
1357 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1358 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1359 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1363 pstats_free(struct pstats *ps)
1366 free(ps, M_SUBPROC);
1369 #ifdef COMPAT_FREEBSD32
1372 * This function is typically used to copy out the kernel address, so
1373 * it can be replaced by assignment of zero.
1375 static inline uint32_t
1376 ptr32_trim(const void *ptr)
1380 uptr = (uintptr_t)ptr;
1381 return ((uptr > UINT_MAX) ? 0 : uptr);
1384 #define PTRTRIM_CP(src,dst,fld) \
1385 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1388 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1392 bzero(ki32, sizeof(struct kinfo_proc32));
1393 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1394 CP(*ki, *ki32, ki_layout);
1395 PTRTRIM_CP(*ki, *ki32, ki_args);
1396 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1397 PTRTRIM_CP(*ki, *ki32, ki_addr);
1398 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1399 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1400 PTRTRIM_CP(*ki, *ki32, ki_fd);
1401 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1402 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1403 CP(*ki, *ki32, ki_pid);
1404 CP(*ki, *ki32, ki_ppid);
1405 CP(*ki, *ki32, ki_pgid);
1406 CP(*ki, *ki32, ki_tpgid);
1407 CP(*ki, *ki32, ki_sid);
1408 CP(*ki, *ki32, ki_tsid);
1409 CP(*ki, *ki32, ki_jobc);
1410 CP(*ki, *ki32, ki_tdev);
1411 CP(*ki, *ki32, ki_tdev_freebsd11);
1412 CP(*ki, *ki32, ki_siglist);
1413 CP(*ki, *ki32, ki_sigmask);
1414 CP(*ki, *ki32, ki_sigignore);
1415 CP(*ki, *ki32, ki_sigcatch);
1416 CP(*ki, *ki32, ki_uid);
1417 CP(*ki, *ki32, ki_ruid);
1418 CP(*ki, *ki32, ki_svuid);
1419 CP(*ki, *ki32, ki_rgid);
1420 CP(*ki, *ki32, ki_svgid);
1421 CP(*ki, *ki32, ki_ngroups);
1422 for (i = 0; i < KI_NGROUPS; i++)
1423 CP(*ki, *ki32, ki_groups[i]);
1424 CP(*ki, *ki32, ki_size);
1425 CP(*ki, *ki32, ki_rssize);
1426 CP(*ki, *ki32, ki_swrss);
1427 CP(*ki, *ki32, ki_tsize);
1428 CP(*ki, *ki32, ki_dsize);
1429 CP(*ki, *ki32, ki_ssize);
1430 CP(*ki, *ki32, ki_xstat);
1431 CP(*ki, *ki32, ki_acflag);
1432 CP(*ki, *ki32, ki_pctcpu);
1433 CP(*ki, *ki32, ki_estcpu);
1434 CP(*ki, *ki32, ki_slptime);
1435 CP(*ki, *ki32, ki_swtime);
1436 CP(*ki, *ki32, ki_cow);
1437 CP(*ki, *ki32, ki_runtime);
1438 TV_CP(*ki, *ki32, ki_start);
1439 TV_CP(*ki, *ki32, ki_childtime);
1440 CP(*ki, *ki32, ki_flag);
1441 CP(*ki, *ki32, ki_kiflag);
1442 CP(*ki, *ki32, ki_traceflag);
1443 CP(*ki, *ki32, ki_stat);
1444 CP(*ki, *ki32, ki_nice);
1445 CP(*ki, *ki32, ki_lock);
1446 CP(*ki, *ki32, ki_rqindex);
1447 CP(*ki, *ki32, ki_oncpu);
1448 CP(*ki, *ki32, ki_lastcpu);
1450 /* XXX TODO: wrap cpu value as appropriate */
1451 CP(*ki, *ki32, ki_oncpu_old);
1452 CP(*ki, *ki32, ki_lastcpu_old);
1454 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1455 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1456 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1457 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1458 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1459 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1460 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1461 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1462 CP(*ki, *ki32, ki_tracer);
1463 CP(*ki, *ki32, ki_flag2);
1464 CP(*ki, *ki32, ki_fibnum);
1465 CP(*ki, *ki32, ki_cr_flags);
1466 CP(*ki, *ki32, ki_jid);
1467 CP(*ki, *ki32, ki_numthreads);
1468 CP(*ki, *ki32, ki_tid);
1469 CP(*ki, *ki32, ki_pri);
1470 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1471 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1472 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1473 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1474 PTRTRIM_CP(*ki, *ki32, ki_udata);
1475 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
1476 CP(*ki, *ki32, ki_sflag);
1477 CP(*ki, *ki32, ki_tdflags);
1482 kern_proc_out_size(struct proc *p, int flags)
1486 PROC_LOCK_ASSERT(p, MA_OWNED);
1488 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1489 #ifdef COMPAT_FREEBSD32
1490 if ((flags & KERN_PROC_MASK32) != 0) {
1491 size += sizeof(struct kinfo_proc32);
1494 size += sizeof(struct kinfo_proc);
1496 #ifdef COMPAT_FREEBSD32
1497 if ((flags & KERN_PROC_MASK32) != 0)
1498 size += sizeof(struct kinfo_proc32) * p->p_numthreads;
1501 size += sizeof(struct kinfo_proc) * p->p_numthreads;
1508 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1511 struct kinfo_proc ki;
1512 #ifdef COMPAT_FREEBSD32
1513 struct kinfo_proc32 ki32;
1517 PROC_LOCK_ASSERT(p, MA_OWNED);
1518 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1521 fill_kinfo_proc(p, &ki);
1522 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1523 #ifdef COMPAT_FREEBSD32
1524 if ((flags & KERN_PROC_MASK32) != 0) {
1525 freebsd32_kinfo_proc_out(&ki, &ki32);
1526 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1530 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1533 FOREACH_THREAD_IN_PROC(p, td) {
1534 fill_kinfo_thread(td, &ki, 1);
1535 #ifdef COMPAT_FREEBSD32
1536 if ((flags & KERN_PROC_MASK32) != 0) {
1537 freebsd32_kinfo_proc_out(&ki, &ki32);
1538 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1542 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1553 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags)
1556 struct kinfo_proc ki;
1559 if (req->oldptr == NULL)
1560 return (SYSCTL_OUT(req, 0, kern_proc_out_size(p, flags)));
1562 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1563 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1564 error = kern_proc_out(p, &sb, flags);
1565 error2 = sbuf_finish(&sb);
1569 else if (error2 != 0)
1575 proc_iterate(int (*cb)(struct proc *, void *), void *cbarg)
1580 for (i = 0; i < pidhashlock + 1; i++) {
1581 sx_slock(&proctree_lock);
1582 sx_slock(&pidhashtbl_lock[i]);
1583 for (j = i; j <= pidhash; j += pidhashlock + 1) {
1584 LIST_FOREACH(p, &pidhashtbl[j], p_hash) {
1585 if (p->p_state == PRS_NEW)
1587 error = cb(p, cbarg);
1588 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1590 sx_sunlock(&pidhashtbl_lock[i]);
1591 sx_sunlock(&proctree_lock);
1596 sx_sunlock(&pidhashtbl_lock[i]);
1597 sx_sunlock(&proctree_lock);
1602 struct kern_proc_out_args {
1603 struct sysctl_req *req;
1610 sysctl_kern_proc_iterate(struct proc *p, void *origarg)
1612 struct kern_proc_out_args *arg = origarg;
1613 int *name = arg->name;
1614 int oid_number = arg->oid_number;
1615 int flags = arg->flags;
1616 struct sysctl_req *req = arg->req;
1621 KASSERT(p->p_ucred != NULL,
1622 ("process credential is NULL for non-NEW proc"));
1624 * Show a user only appropriate processes.
1626 if (p_cansee(curthread, p))
1629 * TODO - make more efficient (see notes below).
1632 switch (oid_number) {
1634 if (p->p_ucred->cr_gid != (gid_t)name[0])
1638 case KERN_PROC_PGRP:
1639 /* could do this by traversing pgrp */
1640 if (p->p_pgrp == NULL ||
1641 p->p_pgrp->pg_id != (pid_t)name[0])
1645 case KERN_PROC_RGID:
1646 if (p->p_ucred->cr_rgid != (gid_t)name[0])
1650 case KERN_PROC_SESSION:
1651 if (p->p_session == NULL ||
1652 p->p_session->s_sid != (pid_t)name[0])
1657 if ((p->p_flag & P_CONTROLT) == 0 ||
1658 p->p_session == NULL)
1660 /* XXX proctree_lock */
1661 SESS_LOCK(p->p_session);
1662 if (p->p_session->s_ttyp == NULL ||
1663 tty_udev(p->p_session->s_ttyp) !=
1665 SESS_UNLOCK(p->p_session);
1668 SESS_UNLOCK(p->p_session);
1672 if (p->p_ucred->cr_uid != (uid_t)name[0])
1676 case KERN_PROC_RUID:
1677 if (p->p_ucred->cr_ruid != (uid_t)name[0])
1681 case KERN_PROC_PROC:
1687 error = sysctl_out_proc(p, req, flags);
1688 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1696 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1698 struct kern_proc_out_args iterarg;
1699 int *name = (int *)arg1;
1700 u_int namelen = arg2;
1702 int flags, oid_number;
1705 oid_number = oidp->oid_number;
1706 if (oid_number != KERN_PROC_ALL &&
1707 (oid_number & KERN_PROC_INC_THREAD) == 0)
1708 flags = KERN_PROC_NOTHREADS;
1711 oid_number &= ~KERN_PROC_INC_THREAD;
1713 #ifdef COMPAT_FREEBSD32
1714 if (req->flags & SCTL_MASK32)
1715 flags |= KERN_PROC_MASK32;
1717 if (oid_number == KERN_PROC_PID) {
1720 error = sysctl_wire_old_buffer(req, 0);
1723 sx_slock(&proctree_lock);
1724 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1726 error = sysctl_out_proc(p, req, flags);
1727 sx_sunlock(&proctree_lock);
1731 switch (oid_number) {
1736 case KERN_PROC_PROC:
1737 if (namelen != 0 && namelen != 1)
1746 if (req->oldptr == NULL) {
1747 /* overestimate by 5 procs */
1748 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1752 error = sysctl_wire_old_buffer(req, 0);
1756 iterarg.flags = flags;
1757 iterarg.oid_number = oid_number;
1759 iterarg.name = name;
1760 error = proc_iterate(sysctl_kern_proc_iterate, &iterarg);
1765 pargs_alloc(int len)
1769 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1771 refcount_init(&pa->ar_ref, 1);
1772 pa->ar_length = len;
1777 pargs_free(struct pargs *pa)
1784 pargs_hold(struct pargs *pa)
1789 refcount_acquire(&pa->ar_ref);
1793 pargs_drop(struct pargs *pa)
1798 if (refcount_release(&pa->ar_ref))
1803 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1809 * This may return a short read if the string is shorter than the chunk
1810 * and is aligned at the end of the page, and the following page is not
1813 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1819 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1821 enum proc_vector_type {
1827 #ifdef COMPAT_FREEBSD32
1829 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1830 size_t *vsizep, enum proc_vector_type type)
1832 struct freebsd32_ps_strings pss;
1834 vm_offset_t vptr, ptr;
1835 uint32_t *proc_vector32;
1841 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1846 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1847 vsize = pss.ps_nargvstr;
1848 if (vsize > ARG_MAX)
1850 size = vsize * sizeof(int32_t);
1853 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1854 vsize = pss.ps_nenvstr;
1855 if (vsize > ARG_MAX)
1857 size = vsize * sizeof(int32_t);
1860 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1861 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1864 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1865 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1868 if (aux.a_type == AT_NULL)
1872 if (aux.a_type != AT_NULL)
1875 size = vsize * sizeof(aux);
1878 KASSERT(0, ("Wrong proc vector type: %d", type));
1881 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1882 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1886 if (type == PROC_AUX) {
1887 *proc_vectorp = (char **)proc_vector32;
1891 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1892 for (i = 0; i < (int)vsize; i++)
1893 proc_vector[i] = PTRIN(proc_vector32[i]);
1894 *proc_vectorp = proc_vector;
1897 free(proc_vector32, M_TEMP);
1903 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1904 size_t *vsizep, enum proc_vector_type type)
1906 struct ps_strings pss;
1908 vm_offset_t vptr, ptr;
1913 #ifdef COMPAT_FREEBSD32
1914 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1915 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1917 if (proc_readmem(td, p, PROC_PS_STRINGS(p), &pss, sizeof(pss)) !=
1922 vptr = (vm_offset_t)pss.ps_argvstr;
1923 vsize = pss.ps_nargvstr;
1924 if (vsize > ARG_MAX)
1926 size = vsize * sizeof(char *);
1929 vptr = (vm_offset_t)pss.ps_envstr;
1930 vsize = pss.ps_nenvstr;
1931 if (vsize > ARG_MAX)
1933 size = vsize * sizeof(char *);
1937 * The aux array is just above env array on the stack. Check
1938 * that the address is naturally aligned.
1940 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1942 #if __ELF_WORD_SIZE == 64
1943 if (vptr % sizeof(uint64_t) != 0)
1945 if (vptr % sizeof(uint32_t) != 0)
1949 * We count the array size reading the aux vectors from the
1950 * stack until AT_NULL vector is returned. So (to keep the code
1951 * simple) we read the process stack twice: the first time here
1952 * to find the size and the second time when copying the vectors
1953 * to the allocated proc_vector.
1955 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1956 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1959 if (aux.a_type == AT_NULL)
1964 * If the PROC_AUXV_MAX entries are iterated over, and we have
1965 * not reached AT_NULL, it is most likely we are reading wrong
1966 * data: either the process doesn't have auxv array or data has
1967 * been modified. Return the error in this case.
1969 if (aux.a_type != AT_NULL)
1972 size = vsize * sizeof(aux);
1975 KASSERT(0, ("Wrong proc vector type: %d", type));
1976 return (EINVAL); /* In case we are built without INVARIANTS. */
1978 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1979 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1980 free(proc_vector, M_TEMP);
1983 *proc_vectorp = proc_vector;
1989 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1992 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1993 enum proc_vector_type type)
1995 size_t done, len, nchr, vsize;
1997 char **proc_vector, *sptr;
1998 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
2000 PROC_ASSERT_HELD(p);
2003 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
2005 nchr = 2 * (PATH_MAX + ARG_MAX);
2007 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
2010 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
2012 * The program may have scribbled into its argv array, e.g. to
2013 * remove some arguments. If that has happened, break out
2014 * before trying to read from NULL.
2016 if (proc_vector[i] == NULL)
2018 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
2019 error = proc_read_string(td, p, sptr, pss_string,
2020 sizeof(pss_string));
2023 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
2024 if (done + len >= nchr)
2025 len = nchr - done - 1;
2026 sbuf_bcat(sb, pss_string, len);
2027 if (len != GET_PS_STRINGS_CHUNK_SZ)
2029 done += GET_PS_STRINGS_CHUNK_SZ;
2031 sbuf_bcat(sb, "", 1);
2035 free(proc_vector, M_TEMP);
2040 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
2043 return (get_ps_strings(curthread, p, sb, PROC_ARG));
2047 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
2050 return (get_ps_strings(curthread, p, sb, PROC_ENV));
2054 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
2060 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
2062 #ifdef COMPAT_FREEBSD32
2063 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
2064 size = vsize * sizeof(Elf32_Auxinfo);
2067 size = vsize * sizeof(Elf_Auxinfo);
2068 if (sbuf_bcat(sb, auxv, size) != 0)
2076 * This sysctl allows a process to retrieve the argument list or process
2077 * title for another process without groping around in the address space
2078 * of the other process. It also allow a process to set its own "process
2079 * title to a string of its own choice.
2082 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
2084 int *name = (int *)arg1;
2085 u_int namelen = arg2;
2086 struct pargs *newpa, *pa;
2089 int flags, error = 0, error2;
2096 pid = (pid_t)name[0];
2102 * If the query is for this process and it is single-threaded, there
2103 * is nobody to modify pargs, thus we can just read.
2105 if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL &&
2106 (pa = p->p_args) != NULL)
2107 return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));
2109 flags = PGET_CANSEE;
2110 if (req->newptr != NULL)
2111 flags |= PGET_ISCURRENT;
2112 error = pget(pid, flags, &p);
2120 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
2122 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
2125 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2126 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2127 error = proc_getargv(curthread, p, &sb);
2128 error2 = sbuf_finish(&sb);
2131 if (error == 0 && error2 != 0)
2136 if (error != 0 || req->newptr == NULL)
2139 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
2142 if (req->newlen == 0) {
2144 * Clear the argument pointer, so that we'll fetch arguments
2145 * with proc_getargv() until further notice.
2149 newpa = pargs_alloc(req->newlen);
2150 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
2165 * This sysctl allows a process to retrieve environment of another process.
2168 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
2170 int *name = (int *)arg1;
2171 u_int namelen = arg2;
2179 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2182 if ((p->p_flag & P_SYSTEM) != 0) {
2187 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2188 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2189 error = proc_getenvv(curthread, p, &sb);
2190 error2 = sbuf_finish(&sb);
2193 return (error != 0 ? error : error2);
2197 * This sysctl allows a process to retrieve ELF auxiliary vector of
2201 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
2203 int *name = (int *)arg1;
2204 u_int namelen = arg2;
2212 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2215 if ((p->p_flag & P_SYSTEM) != 0) {
2219 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
2220 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2221 error = proc_getauxv(curthread, p, &sb);
2222 error2 = sbuf_finish(&sb);
2225 return (error != 0 ? error : error2);
2229 * Look up the canonical executable path running in the specified process.
2230 * It tries to return the same hardlink name as was used for execve(2).
2231 * This allows the programs that modify their behavior based on their progname,
2232 * to operate correctly.
2234 * Result is returned in retbuf, it must not be freed, similar to vn_fullpath()
2235 * calling conventions.
2236 * binname is a pointer to temporary string buffer of length MAXPATHLEN,
2237 * allocated and freed by caller.
2238 * freebuf should be freed by caller, from the M_TEMP malloc type.
2241 proc_get_binpath(struct proc *p, char *binname, char **retbuf,
2244 struct nameidata nd;
2245 struct vnode *vp, *dvp;
2246 size_t freepath_size;
2250 PROC_LOCK_ASSERT(p, MA_OWNED);
2263 if (p->p_binname != NULL)
2264 strlcpy(binname, p->p_binname, MAXPATHLEN);
2269 if (dvp != NULL && binname[0] != '\0') {
2270 freepath_size = MAXPATHLEN;
2271 if (vn_fullpath_hardlink(vp, dvp, binname, strlen(binname),
2272 retbuf, freebuf, &freepath_size) == 0) {
2274 * Recheck the looked up path. The binary
2275 * might have been renamed or replaced, in
2276 * which case we should not report old name.
2278 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, *retbuf,
2283 do_fullpath = false;
2285 NDFREE(&nd, NDF_ONLY_PNBUF);
2290 free(*freebuf, M_TEMP);
2292 error = vn_fullpath(vp, retbuf, freebuf);
2301 * This sysctl allows a process to retrieve the path of the executable for
2302 * itself or another process.
2305 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2307 pid_t *pidp = (pid_t *)arg1;
2308 unsigned int arglen = arg2;
2310 char *retbuf, *freebuf, *binname;
2315 binname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
2317 if (*pidp == -1) { /* -1 means this process */
2319 p = req->td->td_proc;
2322 error = pget(*pidp, PGET_CANSEE, &p);
2326 error = proc_get_binpath(p, binname, &retbuf, &freebuf);
2327 free(binname, M_TEMP);
2330 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2331 free(freebuf, M_TEMP);
2336 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2349 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2352 sv_name = p->p_sysent->sv_name;
2354 return (sysctl_handle_string(oidp, sv_name, 0, req));
2357 #ifdef KINFO_OVMENTRY_SIZE
2358 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2361 #ifdef COMPAT_FREEBSD7
2363 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2365 vm_map_entry_t entry, tmp_entry;
2366 unsigned int last_timestamp, namelen;
2367 char *fullpath, *freepath;
2368 struct kinfo_ovmentry *kve;
2382 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2385 vm = vmspace_acquire_ref(p);
2390 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2393 vm_map_lock_read(map);
2394 VM_MAP_ENTRY_FOREACH(entry, map) {
2395 vm_object_t obj, tobj, lobj;
2398 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2401 bzero(kve, sizeof(*kve));
2402 kve->kve_structsize = sizeof(*kve);
2404 kve->kve_private_resident = 0;
2405 obj = entry->object.vm_object;
2407 VM_OBJECT_RLOCK(obj);
2408 if (obj->shadow_count == 1)
2409 kve->kve_private_resident =
2410 obj->resident_page_count;
2412 kve->kve_resident = 0;
2413 addr = entry->start;
2414 while (addr < entry->end) {
2415 if (pmap_extract(map->pmap, addr))
2416 kve->kve_resident++;
2420 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2422 VM_OBJECT_RLOCK(tobj);
2423 kve->kve_offset += tobj->backing_object_offset;
2426 VM_OBJECT_RUNLOCK(lobj);
2430 kve->kve_start = (void*)entry->start;
2431 kve->kve_end = (void*)entry->end;
2432 kve->kve_offset += (off_t)entry->offset;
2434 if (entry->protection & VM_PROT_READ)
2435 kve->kve_protection |= KVME_PROT_READ;
2436 if (entry->protection & VM_PROT_WRITE)
2437 kve->kve_protection |= KVME_PROT_WRITE;
2438 if (entry->protection & VM_PROT_EXECUTE)
2439 kve->kve_protection |= KVME_PROT_EXEC;
2441 if (entry->eflags & MAP_ENTRY_COW)
2442 kve->kve_flags |= KVME_FLAG_COW;
2443 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2444 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2445 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2446 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2448 last_timestamp = map->timestamp;
2449 vm_map_unlock_read(map);
2451 kve->kve_fileid = 0;
2456 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2457 if (kve->kve_type == KVME_TYPE_MGTDEVICE)
2458 kve->kve_type = KVME_TYPE_UNKNOWN;
2462 VM_OBJECT_RUNLOCK(lobj);
2464 kve->kve_ref_count = obj->ref_count;
2465 kve->kve_shadow_count = obj->shadow_count;
2466 VM_OBJECT_RUNLOCK(obj);
2468 vn_fullpath(vp, &fullpath, &freepath);
2469 cred = curthread->td_ucred;
2470 vn_lock(vp, LK_SHARED | LK_RETRY);
2471 if (VOP_GETATTR(vp, &va, cred) == 0) {
2472 kve->kve_fileid = va.va_fileid;
2474 kve->kve_fsid = va.va_fsid;
2479 kve->kve_type = KVME_TYPE_NONE;
2480 kve->kve_ref_count = 0;
2481 kve->kve_shadow_count = 0;
2484 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2485 if (freepath != NULL)
2486 free(freepath, M_TEMP);
2488 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2489 vm_map_lock_read(map);
2492 if (last_timestamp != map->timestamp) {
2493 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2497 vm_map_unlock_read(map);
2503 #endif /* COMPAT_FREEBSD7 */
2505 #ifdef KINFO_VMENTRY_SIZE
2506 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2510 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2511 int *resident_count, bool *super)
2513 vm_object_t obj, tobj;
2517 vm_pindex_t pi, pi_adv, pindex;
2520 *resident_count = 0;
2521 if (vmmap_skip_res_cnt)
2525 obj = entry->object.vm_object;
2526 addr = entry->start;
2528 pi = OFF_TO_IDX(entry->offset);
2529 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2530 if (m_adv != NULL) {
2533 pi_adv = atop(entry->end - addr);
2535 for (tobj = obj;; tobj = tobj->backing_object) {
2536 m = vm_page_find_least(tobj, pindex);
2538 if (m->pindex == pindex)
2540 if (pi_adv > m->pindex - pindex) {
2541 pi_adv = m->pindex - pindex;
2545 if (tobj->backing_object == NULL)
2547 pindex += OFF_TO_IDX(tobj->
2548 backing_object_offset);
2552 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2553 (addr & (pagesizes[1] - 1)) == 0 &&
2554 (pmap_mincore(map->pmap, addr, &pa) & MINCORE_SUPER) != 0) {
2556 pi_adv = atop(pagesizes[1]);
2559 * We do not test the found page on validity.
2560 * Either the page is busy and being paged in,
2561 * or it was invalidated. The first case
2562 * should be counted as resident, the second
2563 * is not so clear; we do account both.
2567 *resident_count += pi_adv;
2573 * Must be called with the process locked and will return unlocked.
2576 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2578 vm_map_entry_t entry, tmp_entry;
2581 vm_object_t lobj, nobj, obj, tobj;
2582 char *fullpath, *freepath;
2583 struct kinfo_vmentry *kve;
2588 unsigned int last_timestamp;
2592 PROC_LOCK_ASSERT(p, MA_OWNED);
2596 vm = vmspace_acquire_ref(p);
2601 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2605 vm_map_lock_read(map);
2606 VM_MAP_ENTRY_FOREACH(entry, map) {
2607 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2611 bzero(kve, sizeof(*kve));
2612 obj = entry->object.vm_object;
2614 if ((obj->flags & OBJ_ANON) != 0)
2615 kve->kve_obj = (uintptr_t)obj;
2617 for (tobj = obj; tobj != NULL;
2618 tobj = tobj->backing_object) {
2619 VM_OBJECT_RLOCK(tobj);
2620 kve->kve_offset += tobj->backing_object_offset;
2623 if (obj->backing_object == NULL)
2624 kve->kve_private_resident =
2625 obj->resident_page_count;
2626 kern_proc_vmmap_resident(map, entry,
2627 &kve->kve_resident, &super);
2629 kve->kve_flags |= KVME_FLAG_SUPER;
2630 for (tobj = obj; tobj != NULL; tobj = nobj) {
2631 nobj = tobj->backing_object;
2632 if (tobj != obj && tobj != lobj)
2633 VM_OBJECT_RUNLOCK(tobj);
2639 kve->kve_start = entry->start;
2640 kve->kve_end = entry->end;
2641 kve->kve_offset += entry->offset;
2643 if (entry->protection & VM_PROT_READ)
2644 kve->kve_protection |= KVME_PROT_READ;
2645 if (entry->protection & VM_PROT_WRITE)
2646 kve->kve_protection |= KVME_PROT_WRITE;
2647 if (entry->protection & VM_PROT_EXECUTE)
2648 kve->kve_protection |= KVME_PROT_EXEC;
2650 if (entry->eflags & MAP_ENTRY_COW)
2651 kve->kve_flags |= KVME_FLAG_COW;
2652 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2653 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2654 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2655 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2656 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2657 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2658 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2659 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2660 if (entry->eflags & MAP_ENTRY_USER_WIRED)
2661 kve->kve_flags |= KVME_FLAG_USER_WIRED;
2663 guard = (entry->eflags & MAP_ENTRY_GUARD) != 0;
2665 last_timestamp = map->timestamp;
2666 vm_map_unlock_read(map);
2671 kve->kve_type = vm_object_kvme_type(lobj, &vp);
2675 VM_OBJECT_RUNLOCK(lobj);
2677 kve->kve_ref_count = obj->ref_count;
2678 kve->kve_shadow_count = obj->shadow_count;
2679 VM_OBJECT_RUNLOCK(obj);
2681 vn_fullpath(vp, &fullpath, &freepath);
2682 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2683 cred = curthread->td_ucred;
2684 vn_lock(vp, LK_SHARED | LK_RETRY);
2685 if (VOP_GETATTR(vp, &va, cred) == 0) {
2686 kve->kve_vn_fileid = va.va_fileid;
2687 kve->kve_vn_fsid = va.va_fsid;
2688 kve->kve_vn_fsid_freebsd11 =
2689 kve->kve_vn_fsid; /* truncate */
2691 MAKEIMODE(va.va_type, va.va_mode);
2692 kve->kve_vn_size = va.va_size;
2693 kve->kve_vn_rdev = va.va_rdev;
2694 kve->kve_vn_rdev_freebsd11 =
2695 kve->kve_vn_rdev; /* truncate */
2696 kve->kve_status = KF_ATTR_VALID;
2701 kve->kve_type = guard ? KVME_TYPE_GUARD :
2703 kve->kve_ref_count = 0;
2704 kve->kve_shadow_count = 0;
2707 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2708 if (freepath != NULL)
2709 free(freepath, M_TEMP);
2711 /* Pack record size down */
2712 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2713 kve->kve_structsize =
2714 offsetof(struct kinfo_vmentry, kve_path) +
2715 strlen(kve->kve_path) + 1;
2717 kve->kve_structsize = sizeof(*kve);
2718 kve->kve_structsize = roundup(kve->kve_structsize,
2721 /* Halt filling and truncate rather than exceeding maxlen */
2722 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2724 vm_map_lock_read(map);
2726 } else if (maxlen != -1)
2727 maxlen -= kve->kve_structsize;
2729 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2731 vm_map_lock_read(map);
2734 if (last_timestamp != map->timestamp) {
2735 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2739 vm_map_unlock_read(map);
2747 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2752 int error, error2, *name;
2759 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2760 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2761 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2766 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2767 error2 = sbuf_finish(&sb);
2769 return (error != 0 ? error : error2);
2772 #if defined(STACK) || defined(DDB)
2774 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2776 struct kinfo_kstack *kkstp;
2777 int error, i, *name, numthreads;
2778 lwpid_t *lwpidarray;
2790 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2794 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2795 st = stack_create(M_WAITOK);
2800 if (lwpidarray != NULL) {
2801 free(lwpidarray, M_TEMP);
2804 numthreads = p->p_numthreads;
2806 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2809 } while (numthreads < p->p_numthreads);
2812 * XXXRW: During the below loop, execve(2) and countless other sorts
2813 * of changes could have taken place. Should we check to see if the
2814 * vmspace has been replaced, or the like, in order to prevent
2815 * giving a snapshot that spans, say, execve(2), with some threads
2816 * before and some after? Among other things, the credentials could
2817 * have changed, in which case the right to extract debug info might
2818 * no longer be assured.
2821 FOREACH_THREAD_IN_PROC(p, td) {
2822 KASSERT(i < numthreads,
2823 ("sysctl_kern_proc_kstack: numthreads"));
2824 lwpidarray[i] = td->td_tid;
2829 for (i = 0; i < numthreads; i++) {
2830 td = tdfind(lwpidarray[i], p->p_pid);
2834 bzero(kkstp, sizeof(*kkstp));
2835 (void)sbuf_new(&sb, kkstp->kkst_trace,
2836 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2838 kkstp->kkst_tid = td->td_tid;
2839 if (TD_IS_SWAPPED(td))
2840 kkstp->kkst_state = KKST_STATE_SWAPPED;
2841 else if (stack_save_td(st, td) == 0)
2842 kkstp->kkst_state = KKST_STATE_STACKOK;
2844 kkstp->kkst_state = KKST_STATE_RUNNING;
2847 stack_sbuf_print(&sb, st);
2850 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2855 if (lwpidarray != NULL)
2856 free(lwpidarray, M_TEMP);
2858 free(kkstp, M_TEMP);
2864 * This sysctl allows a process to retrieve the full list of groups from
2865 * itself or another process.
2868 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2870 pid_t *pidp = (pid_t *)arg1;
2871 unsigned int arglen = arg2;
2878 if (*pidp == -1) { /* -1 means this process */
2879 p = req->td->td_proc;
2882 error = pget(*pidp, PGET_CANSEE, &p);
2887 cred = crhold(p->p_ucred);
2890 error = SYSCTL_OUT(req, cred->cr_groups,
2891 cred->cr_ngroups * sizeof(gid_t));
2897 * This sysctl allows a process to retrieve or/and set the resource limit for
2901 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2903 int *name = (int *)arg1;
2904 u_int namelen = arg2;
2913 which = (u_int)name[1];
2914 if (which >= RLIM_NLIMITS)
2917 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2920 flags = PGET_HOLD | PGET_NOTWEXIT;
2921 if (req->newptr != NULL)
2922 flags |= PGET_CANDEBUG;
2924 flags |= PGET_CANSEE;
2925 error = pget((pid_t)name[0], flags, &p);
2932 if (req->oldptr != NULL) {
2934 lim_rlimit_proc(p, which, &rlim);
2937 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2944 if (req->newptr != NULL) {
2945 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2947 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2956 * This sysctl allows a process to retrieve ps_strings structure location of
2960 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2962 int *name = (int *)arg1;
2963 u_int namelen = arg2;
2965 vm_offset_t ps_strings;
2967 #ifdef COMPAT_FREEBSD32
2968 uint32_t ps_strings32;
2974 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2977 #ifdef COMPAT_FREEBSD32
2978 if ((req->flags & SCTL_MASK32) != 0) {
2980 * We return 0 if the 32 bit emulation request is for a 64 bit
2983 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2984 PTROUT(PROC_PS_STRINGS(p)) : 0;
2986 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2990 ps_strings = PROC_PS_STRINGS(p);
2992 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2997 * This sysctl allows a process to retrieve umask of another process.
3000 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
3002 int *name = (int *)arg1;
3003 u_int namelen = arg2;
3012 pid = (pid_t)name[0];
3014 if (pid == p->p_pid || pid == 0) {
3015 cmask = p->p_pd->pd_cmask;
3019 error = pget(pid, PGET_WANTREAD, &p);
3023 cmask = p->p_pd->pd_cmask;
3026 error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
3031 * This sysctl allows a process to set and retrieve binary osreldate of
3035 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
3037 int *name = (int *)arg1;
3038 u_int namelen = arg2;
3040 int flags, error, osrel;
3045 if (req->newptr != NULL && req->newlen != sizeof(osrel))
3048 flags = PGET_HOLD | PGET_NOTWEXIT;
3049 if (req->newptr != NULL)
3050 flags |= PGET_CANDEBUG;
3052 flags |= PGET_CANSEE;
3053 error = pget((pid_t)name[0], flags, &p);
3057 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
3061 if (req->newptr != NULL) {
3062 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
3077 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
3079 int *name = (int *)arg1;
3080 u_int namelen = arg2;
3082 struct kinfo_sigtramp kst;
3083 const struct sysentvec *sv;
3085 #ifdef COMPAT_FREEBSD32
3086 struct kinfo_sigtramp32 kst32;
3092 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3096 #ifdef COMPAT_FREEBSD32
3097 if ((req->flags & SCTL_MASK32) != 0) {
3098 bzero(&kst32, sizeof(kst32));
3099 if (SV_PROC_FLAG(p, SV_ILP32)) {
3100 if (sv->sv_sigcode_base != 0) {
3101 kst32.ksigtramp_start = sv->sv_sigcode_base;
3102 kst32.ksigtramp_end = sv->sv_sigcode_base +
3103 ((sv->sv_flags & SV_DSO_SIG) == 0 ?
3105 (uintptr_t)sv->sv_szsigcode);
3107 kst32.ksigtramp_start = PROC_PS_STRINGS(p) -
3109 kst32.ksigtramp_end = PROC_PS_STRINGS(p);
3113 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
3117 bzero(&kst, sizeof(kst));
3118 if (sv->sv_sigcode_base != 0) {
3119 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
3120 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
3121 ((sv->sv_flags & SV_DSO_SIG) == 0 ? *sv->sv_szsigcode :
3122 (uintptr_t)sv->sv_szsigcode);
3124 kst.ksigtramp_start = (char *)PROC_PS_STRINGS(p) -
3126 kst.ksigtramp_end = (char *)PROC_PS_STRINGS(p);
3129 error = SYSCTL_OUT(req, &kst, sizeof(kst));
3134 sysctl_kern_proc_sigfastblk(SYSCTL_HANDLER_ARGS)
3136 int *name = (int *)arg1;
3137 u_int namelen = arg2;
3142 #ifdef COMPAT_FREEBSD32
3147 if (namelen != 1 || req->newptr != NULL)
3150 pid = (pid_t)name[0];
3151 error = pget(pid, PGET_HOLD | PGET_NOTWEXIT | PGET_CANDEBUG, &p);
3156 #ifdef COMPAT_FREEBSD32
3157 if (SV_CURPROC_FLAG(SV_ILP32)) {
3158 if (!SV_PROC_FLAG(p, SV_ILP32)) {
3164 if (pid <= PID_MAX) {
3165 td1 = FIRST_THREAD_IN_PROC(p);
3167 FOREACH_THREAD_IN_PROC(p, td1) {
3168 if (td1->td_tid == pid)
3177 * The access to the private thread flags. It is fine as far
3178 * as no out-of-thin-air values are read from td_pflags, and
3179 * usermode read of the td_sigblock_ptr is racy inherently,
3180 * since target process might have already changed it
3183 if ((td1->td_pflags & TDP_SIGFASTBLOCK) != 0)
3184 addr = (uintptr_t)td1->td_sigblock_ptr;
3194 #ifdef COMPAT_FREEBSD32
3195 if (SV_CURPROC_FLAG(SV_ILP32)) {
3197 error = SYSCTL_OUT(req, &addr32, sizeof(addr32));
3200 error = SYSCTL_OUT(req, &addr, sizeof(addr));
3204 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
3207 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
3208 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
3209 "Return entire process table");
3211 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3212 sysctl_kern_proc, "Process table");
3214 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
3215 sysctl_kern_proc, "Process table");
3217 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3218 sysctl_kern_proc, "Process table");
3220 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
3221 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3223 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
3224 sysctl_kern_proc, "Process table");
3226 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3227 sysctl_kern_proc, "Process table");
3229 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3230 sysctl_kern_proc, "Process table");
3232 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
3233 sysctl_kern_proc, "Process table");
3235 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
3236 sysctl_kern_proc, "Return process table, no threads");
3238 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
3239 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
3240 sysctl_kern_proc_args, "Process argument list");
3242 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
3243 sysctl_kern_proc_env, "Process environment");
3245 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
3246 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
3248 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
3249 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
3251 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
3252 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
3253 "Process syscall vector name (ABI type)");
3255 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
3256 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3258 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
3259 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3261 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
3262 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3264 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
3265 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3267 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
3268 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3270 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
3271 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3273 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
3274 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3276 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
3277 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
3279 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
3280 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
3281 "Return process table, including threads");
3283 #ifdef COMPAT_FREEBSD7
3284 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
3285 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
3288 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
3289 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
3291 #if defined(STACK) || defined(DDB)
3292 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
3293 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
3296 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
3297 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
3299 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
3300 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
3301 "Process resource limits");
3303 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
3304 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
3305 "Process ps_strings location");
3307 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
3308 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
3310 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
3311 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
3312 "Process binary osreldate");
3314 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
3315 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
3316 "Process signal trampoline location");
3318 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGFASTBLK, sigfastblk, CTLFLAG_RD |
3319 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_sigfastblk,
3320 "Thread sigfastblock address");
3322 static struct sx stop_all_proc_blocker;
3323 SX_SYSINIT(stop_all_proc_blocker, &stop_all_proc_blocker, "sapblk");
3326 stop_all_proc_block(void)
3328 return (sx_xlock_sig(&stop_all_proc_blocker) == 0);
3332 stop_all_proc_unblock(void)
3334 sx_xunlock(&stop_all_proc_blocker);
3340 * stop_all_proc() purpose is to stop all process which have usermode,
3341 * except current process for obvious reasons. This makes it somewhat
3342 * unreliable when invoked from multithreaded process. The service
3343 * must not be user-callable anyway.
3348 struct proc *cp, *p;
3350 bool restart, seen_stopped, seen_exiting, stopped_some;
3352 if (!stop_all_proc_block())
3357 sx_xlock(&allproc_lock);
3359 seen_exiting = seen_stopped = stopped_some = restart = false;
3360 LIST_REMOVE(cp, p_list);
3361 LIST_INSERT_HEAD(&allproc, cp, p_list);
3363 p = LIST_NEXT(cp, p_list);
3366 LIST_REMOVE(cp, p_list);
3367 LIST_INSERT_AFTER(p, cp, p_list);
3369 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3373 if ((p->p_flag2 & P2_WEXIT) != 0) {
3374 seen_exiting = true;
3378 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3380 * Stopped processes are tolerated when there
3381 * are no other processes which might continue
3382 * them. P_STOPPED_SINGLE but not
3383 * P_TOTAL_STOP process still has at least one
3386 seen_stopped = true;
3390 sx_xunlock(&allproc_lock);
3392 r = thread_single(p, SINGLE_ALLPROC);
3396 stopped_some = true;
3399 sx_xlock(&allproc_lock);
3401 /* Catch forked children we did not see in iteration. */
3402 if (gen != allproc_gen)
3404 sx_xunlock(&allproc_lock);
3405 if (restart || stopped_some || seen_exiting || seen_stopped) {
3406 kern_yield(PRI_USER);
3412 resume_all_proc(void)
3414 struct proc *cp, *p;
3417 sx_xlock(&allproc_lock);
3419 LIST_REMOVE(cp, p_list);
3420 LIST_INSERT_HEAD(&allproc, cp, p_list);
3422 p = LIST_NEXT(cp, p_list);
3425 LIST_REMOVE(cp, p_list);
3426 LIST_INSERT_AFTER(p, cp, p_list);
3428 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3429 sx_xunlock(&allproc_lock);
3431 thread_single_end(p, SINGLE_ALLPROC);
3434 sx_xlock(&allproc_lock);
3439 /* Did the loop above missed any stopped process ? */
3440 FOREACH_PROC_IN_SYSTEM(p) {
3441 /* No need for proc lock. */
3442 if ((p->p_flag & P_TOTAL_STOP) != 0)
3445 sx_xunlock(&allproc_lock);
3447 stop_all_proc_unblock();
3450 /* #define TOTAL_STOP_DEBUG 1 */
3451 #ifdef TOTAL_STOP_DEBUG
3452 volatile static int ap_resume;
3453 #include <sys/mount.h>
3456 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3462 error = sysctl_handle_int(oidp, &val, 0, req);
3463 if (error != 0 || req->newptr == NULL)
3468 while (ap_resume == 0)
3476 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3477 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3478 sysctl_debug_stop_all_proc, "I",