2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include "opt_ktrace.h"
38 #include "opt_kstack_pages.h"
39 #include "opt_stack.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/eventhandler.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
50 #include <sys/loginclass.h>
51 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
56 #include <sys/ptrace.h>
57 #include <sys/refcount.h>
58 #include <sys/resourcevar.h>
59 #include <sys/rwlock.h>
61 #include <sys/sysent.h>
62 #include <sys/sched.h>
64 #include <sys/stack.h>
66 #include <sys/sysctl.h>
67 #include <sys/filedesc.h>
69 #include <sys/signalvar.h>
73 #include <sys/vnode.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #ifdef COMPAT_FREEBSD32
90 #include <compat/freebsd32/freebsd32.h>
91 #include <compat/freebsd32/freebsd32_util.h>
94 SDT_PROVIDER_DEFINE(proc);
95 SDT_PROBE_DEFINE4(proc, , ctor, entry, "struct proc *", "int", "void *",
97 SDT_PROBE_DEFINE4(proc, , ctor, return, "struct proc *", "int", "void *",
99 SDT_PROBE_DEFINE4(proc, , dtor, entry, "struct proc *", "int", "void *",
101 SDT_PROBE_DEFINE3(proc, , dtor, return, "struct proc *", "int", "void *");
102 SDT_PROBE_DEFINE3(proc, , init, entry, "struct proc *", "int", "int");
103 SDT_PROBE_DEFINE3(proc, , init, return, "struct proc *", "int", "int");
105 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
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 pgadjustjobc(struct pgrp *pgrp, int entering);
117 static void pgdelete(struct pgrp *);
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);
123 static struct proc *zpfind_locked(pid_t pid);
126 * Other process lists
128 struct pidhashhead *pidhashtbl;
130 struct pgrphashhead *pgrphashtbl;
132 struct proclist allproc;
133 struct proclist zombproc;
134 struct sx allproc_lock;
135 struct sx proctree_lock;
136 struct mtx ppeers_lock;
137 uma_zone_t proc_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_threads = offsetof(struct proc, p_threads);
148 const int thread_off_td_tid = offsetof(struct thread, td_tid);
149 const int thread_off_td_name = offsetof(struct thread, td_name);
150 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
151 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
152 const int thread_off_td_plist = offsetof(struct thread, td_plist);
154 int kstack_pages = KSTACK_PAGES;
155 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
156 "Kernel stack size in pages");
157 static int vmmap_skip_res_cnt = 0;
158 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
159 &vmmap_skip_res_cnt, 0,
160 "Skip calculation of the pages resident count in kern.proc.vmmap");
162 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
163 #ifdef COMPAT_FREEBSD32
164 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
168 * Initialize global process hashing structures.
174 sx_init(&allproc_lock, "allproc");
175 sx_init(&proctree_lock, "proctree");
176 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
178 LIST_INIT(&zombproc);
179 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
180 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
181 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
182 proc_ctor, proc_dtor, proc_init, proc_fini,
183 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
188 * Prepare a proc for use.
191 proc_ctor(void *mem, int size, void *arg, int flags)
195 p = (struct proc *)mem;
196 SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
197 EVENTHANDLER_INVOKE(process_ctor, p);
198 SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
203 * Reclaim a proc after use.
206 proc_dtor(void *mem, int size, void *arg)
211 /* INVARIANTS checks go here */
212 p = (struct proc *)mem;
213 td = FIRST_THREAD_IN_PROC(p);
214 SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
217 KASSERT((p->p_numthreads == 1),
218 ("bad number of threads in exiting process"));
219 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
221 /* Free all OSD associated to this thread. */
224 EVENTHANDLER_INVOKE(process_dtor, p);
225 if (p->p_ksi != NULL)
226 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
227 SDT_PROBE3(proc, , dtor, return, p, size, arg);
231 * Initialize type-stable parts of a proc (when newly created).
234 proc_init(void *mem, int size, int flags)
238 p = (struct proc *)mem;
239 SDT_PROBE3(proc, , init, entry, p, size, flags);
240 p->p_sched = (struct p_sched *)&p[1];
241 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
242 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
243 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
244 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
245 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
246 cv_init(&p->p_pwait, "ppwait");
247 cv_init(&p->p_dbgwait, "dbgwait");
248 TAILQ_INIT(&p->p_threads); /* all threads in proc */
249 EVENTHANDLER_INVOKE(process_init, p);
250 p->p_stats = pstats_alloc();
252 SDT_PROBE3(proc, , init, return, p, size, flags);
257 * UMA should ensure that this function is never called.
258 * Freeing a proc structure would violate type stability.
261 proc_fini(void *mem, int size)
266 p = (struct proc *)mem;
267 EVENTHANDLER_INVOKE(process_fini, p);
268 pstats_free(p->p_stats);
269 thread_free(FIRST_THREAD_IN_PROC(p));
270 mtx_destroy(&p->p_mtx);
271 if (p->p_ksi != NULL)
272 ksiginfo_free(p->p_ksi);
274 panic("proc reclaimed");
279 * Is p an inferior of the current process?
282 inferior(struct proc *p)
285 sx_assert(&proctree_lock, SX_LOCKED);
286 PROC_LOCK_ASSERT(p, MA_OWNED);
287 for (; p != curproc; p = proc_realparent(p)) {
295 pfind_locked(pid_t pid)
299 sx_assert(&allproc_lock, SX_LOCKED);
300 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
301 if (p->p_pid == pid) {
303 if (p->p_state == PRS_NEW) {
314 * Locate a process by number; return only "live" processes -- i.e., neither
315 * zombies nor newly born but incompletely initialized processes. By not
316 * returning processes in the PRS_NEW state, we allow callers to avoid
317 * testing for that condition to avoid dereferencing p_ucred, et al.
324 sx_slock(&allproc_lock);
325 p = pfind_locked(pid);
326 sx_sunlock(&allproc_lock);
331 pfind_tid_locked(pid_t tid)
336 sx_assert(&allproc_lock, SX_LOCKED);
337 FOREACH_PROC_IN_SYSTEM(p) {
339 if (p->p_state == PRS_NEW) {
343 FOREACH_THREAD_IN_PROC(p, td) {
344 if (td->td_tid == tid)
354 * Locate a process group by number.
355 * The caller must hold proctree_lock.
361 register struct pgrp *pgrp;
363 sx_assert(&proctree_lock, SX_LOCKED);
365 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
366 if (pgrp->pg_id == pgid) {
375 * Locate process and do additional manipulations, depending on flags.
378 pget(pid_t pid, int flags, struct proc **pp)
383 sx_slock(&allproc_lock);
384 if (pid <= PID_MAX) {
385 p = pfind_locked(pid);
386 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
387 p = zpfind_locked(pid);
388 } else if ((flags & PGET_NOTID) == 0) {
389 p = pfind_tid_locked(pid);
393 sx_sunlock(&allproc_lock);
396 if ((flags & PGET_CANSEE) != 0) {
397 error = p_cansee(curthread, p);
401 if ((flags & PGET_CANDEBUG) != 0) {
402 error = p_candebug(curthread, p);
406 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
410 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
414 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
416 * XXXRW: Not clear ESRCH is the right error during proc
422 if ((flags & PGET_HOLD) != 0) {
434 * Create a new process group.
435 * pgid must be equal to the pid of p.
436 * Begin a new session if required.
439 enterpgrp(p, pgid, pgrp, sess)
440 register struct proc *p;
443 struct session *sess;
446 sx_assert(&proctree_lock, SX_XLOCKED);
448 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
449 KASSERT(p->p_pid == pgid,
450 ("enterpgrp: new pgrp and pid != pgid"));
451 KASSERT(pgfind(pgid) == NULL,
452 ("enterpgrp: pgrp with pgid exists"));
453 KASSERT(!SESS_LEADER(p),
454 ("enterpgrp: session leader attempted setpgrp"));
456 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
462 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
464 p->p_flag &= ~P_CONTROLT;
468 sess->s_sid = p->p_pid;
469 refcount_init(&sess->s_count, 1);
470 sess->s_ttyvp = NULL;
471 sess->s_ttydp = NULL;
473 bcopy(p->p_session->s_login, sess->s_login,
474 sizeof(sess->s_login));
475 pgrp->pg_session = sess;
476 KASSERT(p == curproc,
477 ("enterpgrp: mksession and p != curproc"));
479 pgrp->pg_session = p->p_session;
480 sess_hold(pgrp->pg_session);
484 LIST_INIT(&pgrp->pg_members);
487 * As we have an exclusive lock of proctree_lock,
488 * this should not deadlock.
490 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
492 SLIST_INIT(&pgrp->pg_sigiolst);
495 doenterpgrp(p, pgrp);
501 * Move p to an existing process group
504 enterthispgrp(p, pgrp)
505 register struct proc *p;
509 sx_assert(&proctree_lock, SX_XLOCKED);
510 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
511 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
512 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
513 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
514 KASSERT(pgrp->pg_session == p->p_session,
515 ("%s: pgrp's session %p, p->p_session %p.\n",
519 KASSERT(pgrp != p->p_pgrp,
520 ("%s: p belongs to pgrp.", __func__));
522 doenterpgrp(p, pgrp);
528 * Move p to a process group
535 struct pgrp *savepgrp;
537 sx_assert(&proctree_lock, SX_XLOCKED);
538 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
539 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
540 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
541 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
543 savepgrp = p->p_pgrp;
546 * Adjust eligibility of affected pgrps to participate in job control.
547 * Increment eligibility counts before decrementing, otherwise we
548 * could reach 0 spuriously during the first call.
551 fixjobc(p, p->p_pgrp, 0);
556 LIST_REMOVE(p, p_pglist);
559 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
560 PGRP_UNLOCK(savepgrp);
562 if (LIST_EMPTY(&savepgrp->pg_members))
567 * remove process from process group
571 register struct proc *p;
573 struct pgrp *savepgrp;
575 sx_assert(&proctree_lock, SX_XLOCKED);
576 savepgrp = p->p_pgrp;
579 LIST_REMOVE(p, p_pglist);
582 PGRP_UNLOCK(savepgrp);
583 if (LIST_EMPTY(&savepgrp->pg_members))
589 * delete a process group
593 register struct pgrp *pgrp;
595 struct session *savesess;
598 sx_assert(&proctree_lock, SX_XLOCKED);
599 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
600 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
603 * Reset any sigio structures pointing to us as a result of
604 * F_SETOWN with our pgid.
606 funsetownlst(&pgrp->pg_sigiolst);
609 tp = pgrp->pg_session->s_ttyp;
610 LIST_REMOVE(pgrp, pg_hash);
611 savesess = pgrp->pg_session;
614 /* Remove the reference to the pgrp before deallocating it. */
617 tty_rel_pgrp(tp, pgrp);
620 mtx_destroy(&pgrp->pg_mtx);
622 sess_release(savesess);
626 pgadjustjobc(pgrp, entering)
636 if (pgrp->pg_jobc == 0)
643 * Adjust pgrp jobc counters when specified process changes process group.
644 * We count the number of processes in each process group that "qualify"
645 * the group for terminal job control (those with a parent in a different
646 * process group of the same session). If that count reaches zero, the
647 * process group becomes orphaned. Check both the specified process'
648 * process group and that of its children.
649 * entering == 0 => p is leaving specified group.
650 * entering == 1 => p is entering specified group.
653 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
655 struct pgrp *hispgrp;
656 struct session *mysession;
659 sx_assert(&proctree_lock, SX_LOCKED);
660 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
661 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
662 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
665 * Check p's parent to see whether p qualifies its own process
666 * group; if so, adjust count for p's process group.
668 mysession = pgrp->pg_session;
669 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
670 hispgrp->pg_session == mysession)
671 pgadjustjobc(pgrp, entering);
674 * Check this process' children to see whether they qualify
675 * their process groups; if so, adjust counts for children's
678 LIST_FOREACH(q, &p->p_children, p_sibling) {
680 if (hispgrp == pgrp ||
681 hispgrp->pg_session != mysession)
683 if (q->p_state == PRS_ZOMBIE)
685 pgadjustjobc(hispgrp, entering);
698 MPASS(p->p_flag & P_WEXIT);
700 * Do a quick check to see if there is anything to do with the
701 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
704 if (!SESS_LEADER(p) &&
705 (p->p_pgrp == p->p_pptr->p_pgrp) &&
706 LIST_EMPTY(&p->p_children)) {
712 sx_xlock(&proctree_lock);
713 if (SESS_LEADER(p)) {
717 * s_ttyp is not zero'd; we use this to indicate that
718 * the session once had a controlling terminal. (for
719 * logging and informational purposes)
730 * Signal foreground pgrp and revoke access to
731 * controlling terminal if it has not been revoked
734 * Because the TTY may have been revoked in the mean
735 * time and could already have a new session associated
736 * with it, make sure we don't send a SIGHUP to a
737 * foreground process group that does not belong to this
743 if (tp->t_session == sp)
744 tty_signal_pgrp(tp, SIGHUP);
749 sx_xunlock(&proctree_lock);
750 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
751 VOP_REVOKE(ttyvp, REVOKEALL);
752 VOP_UNLOCK(ttyvp, 0);
755 sx_xlock(&proctree_lock);
758 fixjobc(p, p->p_pgrp, 0);
759 sx_xunlock(&proctree_lock);
763 * A process group has become orphaned;
764 * if there are any stopped processes in the group,
765 * hang-up all process in that group.
771 register struct proc *p;
773 PGRP_LOCK_ASSERT(pg, MA_OWNED);
775 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
777 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
779 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
781 kern_psignal(p, SIGHUP);
782 kern_psignal(p, SIGCONT);
792 sess_hold(struct session *s)
795 refcount_acquire(&s->s_count);
799 sess_release(struct session *s)
802 if (refcount_release(&s->s_count)) {
803 if (s->s_ttyp != NULL) {
805 tty_rel_sess(s->s_ttyp, s);
807 mtx_destroy(&s->s_mtx);
814 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
816 register struct pgrp *pgrp;
817 register struct proc *p;
820 for (i = 0; i <= pgrphash; i++) {
821 if (!LIST_EMPTY(&pgrphashtbl[i])) {
822 printf("\tindx %d\n", i);
823 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
825 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
826 (void *)pgrp, (long)pgrp->pg_id,
827 (void *)pgrp->pg_session,
828 pgrp->pg_session->s_count,
829 (void *)LIST_FIRST(&pgrp->pg_members));
830 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
831 printf("\t\tpid %ld addr %p pgrp %p\n",
832 (long)p->p_pid, (void *)p,
842 * Calculate the kinfo_proc members which contain process-wide
844 * Must be called with the target process locked.
847 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
851 PROC_LOCK_ASSERT(p, MA_OWNED);
855 FOREACH_THREAD_IN_PROC(p, td) {
857 kp->ki_pctcpu += sched_pctcpu(td);
858 kp->ki_estcpu += td->td_estcpu;
864 * Clear kinfo_proc and fill in any information that is common
865 * to all threads in the process.
866 * Must be called with the target process locked.
869 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
877 /* For proc_realparent. */
878 sx_assert(&proctree_lock, SX_LOCKED);
879 PROC_LOCK_ASSERT(p, MA_OWNED);
880 bzero(kp, sizeof(*kp));
882 kp->ki_structsize = sizeof(*kp);
884 kp->ki_addr =/* p->p_addr; */0; /* XXX */
885 kp->ki_args = p->p_args;
886 kp->ki_textvp = p->p_textvp;
888 kp->ki_tracep = p->p_tracevp;
889 kp->ki_traceflag = p->p_traceflag;
892 kp->ki_vmspace = p->p_vmspace;
893 kp->ki_flag = p->p_flag;
894 kp->ki_flag2 = p->p_flag2;
897 kp->ki_uid = cred->cr_uid;
898 kp->ki_ruid = cred->cr_ruid;
899 kp->ki_svuid = cred->cr_svuid;
901 if (cred->cr_flags & CRED_FLAG_CAPMODE)
902 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
903 /* XXX bde doesn't like KI_NGROUPS */
904 if (cred->cr_ngroups > KI_NGROUPS) {
905 kp->ki_ngroups = KI_NGROUPS;
906 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
908 kp->ki_ngroups = cred->cr_ngroups;
909 bcopy(cred->cr_groups, kp->ki_groups,
910 kp->ki_ngroups * sizeof(gid_t));
911 kp->ki_rgid = cred->cr_rgid;
912 kp->ki_svgid = cred->cr_svgid;
913 /* If jailed(cred), emulate the old P_JAILED flag. */
915 kp->ki_flag |= P_JAILED;
916 /* If inside the jail, use 0 as a jail ID. */
917 if (cred->cr_prison != curthread->td_ucred->cr_prison)
918 kp->ki_jid = cred->cr_prison->pr_id;
920 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
921 sizeof(kp->ki_loginclass));
925 mtx_lock(&ps->ps_mtx);
926 kp->ki_sigignore = ps->ps_sigignore;
927 kp->ki_sigcatch = ps->ps_sigcatch;
928 mtx_unlock(&ps->ps_mtx);
930 if (p->p_state != PRS_NEW &&
931 p->p_state != PRS_ZOMBIE &&
932 p->p_vmspace != NULL) {
933 struct vmspace *vm = p->p_vmspace;
935 kp->ki_size = vm->vm_map.size;
936 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
937 FOREACH_THREAD_IN_PROC(p, td0) {
938 if (!TD_IS_SWAPPED(td0))
939 kp->ki_rssize += td0->td_kstack_pages;
941 kp->ki_swrss = vm->vm_swrss;
942 kp->ki_tsize = vm->vm_tsize;
943 kp->ki_dsize = vm->vm_dsize;
944 kp->ki_ssize = vm->vm_ssize;
945 } else if (p->p_state == PRS_ZOMBIE)
947 if (kp->ki_flag & P_INMEM)
948 kp->ki_sflag = PS_INMEM;
951 /* Calculate legacy swtime as seconds since 'swtick'. */
952 kp->ki_swtime = (ticks - p->p_swtick) / hz;
953 kp->ki_pid = p->p_pid;
954 kp->ki_nice = p->p_nice;
955 kp->ki_fibnum = p->p_fibnum;
956 kp->ki_start = p->p_stats->p_start;
957 timevaladd(&kp->ki_start, &boottime);
959 rufetch(p, &kp->ki_rusage);
960 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
961 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
963 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
964 /* Some callers want child times in a single value. */
965 kp->ki_childtime = kp->ki_childstime;
966 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
968 FOREACH_THREAD_IN_PROC(p, td0)
969 kp->ki_cow += td0->td_cow;
973 kp->ki_pgid = p->p_pgrp->pg_id;
974 kp->ki_jobc = p->p_pgrp->pg_jobc;
975 sp = p->p_pgrp->pg_session;
978 kp->ki_sid = sp->s_sid;
980 strlcpy(kp->ki_login, sp->s_login,
981 sizeof(kp->ki_login));
983 kp->ki_kiflag |= KI_CTTY;
985 kp->ki_kiflag |= KI_SLEADER;
986 /* XXX proctree_lock */
991 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
992 kp->ki_tdev = tty_udev(tp);
993 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
995 kp->ki_tsid = tp->t_session->s_sid;
998 if (p->p_comm[0] != '\0')
999 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1000 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1001 p->p_sysent->sv_name[0] != '\0')
1002 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1003 kp->ki_siglist = p->p_siglist;
1004 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1005 kp->ki_acflag = p->p_acflag;
1006 kp->ki_lock = p->p_lock;
1008 kp->ki_ppid = proc_realparent(p)->p_pid;
1009 if (p->p_flag & P_TRACED)
1010 kp->ki_tracer = p->p_pptr->p_pid;
1015 * Fill in information that is thread specific. Must be called with
1016 * target process locked. If 'preferthread' is set, overwrite certain
1017 * process-related fields that are maintained for both threads and
1021 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1027 PROC_LOCK_ASSERT(p, MA_OWNED);
1032 if (td->td_wmesg != NULL)
1033 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1035 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1036 strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
1037 if (TD_ON_LOCK(td)) {
1038 kp->ki_kiflag |= KI_LOCKBLOCK;
1039 strlcpy(kp->ki_lockname, td->td_lockname,
1040 sizeof(kp->ki_lockname));
1042 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1043 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1046 if (p->p_state == PRS_NORMAL) { /* approximate. */
1047 if (TD_ON_RUNQ(td) ||
1049 TD_IS_RUNNING(td)) {
1051 } else if (P_SHOULDSTOP(p)) {
1052 kp->ki_stat = SSTOP;
1053 } else if (TD_IS_SLEEPING(td)) {
1054 kp->ki_stat = SSLEEP;
1055 } else if (TD_ON_LOCK(td)) {
1056 kp->ki_stat = SLOCK;
1058 kp->ki_stat = SWAIT;
1060 } else if (p->p_state == PRS_ZOMBIE) {
1061 kp->ki_stat = SZOMB;
1066 /* Things in the thread */
1067 kp->ki_wchan = td->td_wchan;
1068 kp->ki_pri.pri_level = td->td_priority;
1069 kp->ki_pri.pri_native = td->td_base_pri;
1072 * Note: legacy fields; clamp at the old NOCPU value and/or
1073 * the maximum u_char CPU value.
1075 if (td->td_lastcpu == NOCPU)
1076 kp->ki_lastcpu_old = NOCPU_OLD;
1077 else if (td->td_lastcpu > MAXCPU_OLD)
1078 kp->ki_lastcpu_old = MAXCPU_OLD;
1080 kp->ki_lastcpu_old = td->td_lastcpu;
1082 if (td->td_oncpu == NOCPU)
1083 kp->ki_oncpu_old = NOCPU_OLD;
1084 else if (td->td_oncpu > MAXCPU_OLD)
1085 kp->ki_oncpu_old = MAXCPU_OLD;
1087 kp->ki_oncpu_old = td->td_oncpu;
1089 kp->ki_lastcpu = td->td_lastcpu;
1090 kp->ki_oncpu = td->td_oncpu;
1091 kp->ki_tdflags = td->td_flags;
1092 kp->ki_tid = td->td_tid;
1093 kp->ki_numthreads = p->p_numthreads;
1094 kp->ki_pcb = td->td_pcb;
1095 kp->ki_kstack = (void *)td->td_kstack;
1096 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1097 kp->ki_pri.pri_class = td->td_pri_class;
1098 kp->ki_pri.pri_user = td->td_user_pri;
1101 rufetchtd(td, &kp->ki_rusage);
1102 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1103 kp->ki_pctcpu = sched_pctcpu(td);
1104 kp->ki_estcpu = td->td_estcpu;
1105 kp->ki_cow = td->td_cow;
1108 /* We can't get this anymore but ps etc never used it anyway. */
1112 kp->ki_siglist = td->td_siglist;
1113 kp->ki_sigmask = td->td_sigmask;
1120 * Fill in a kinfo_proc structure for the specified process.
1121 * Must be called with the target process locked.
1124 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1127 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1129 fill_kinfo_proc_only(p, kp);
1130 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1131 fill_kinfo_aggregate(p, kp);
1138 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1142 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1145 pstats_fork(struct pstats *src, struct pstats *dst)
1148 bzero(&dst->pstat_startzero,
1149 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1150 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1151 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1155 pstats_free(struct pstats *ps)
1158 free(ps, M_SUBPROC);
1161 static struct proc *
1162 zpfind_locked(pid_t pid)
1166 sx_assert(&allproc_lock, SX_LOCKED);
1167 LIST_FOREACH(p, &zombproc, p_list) {
1168 if (p->p_pid == pid) {
1177 * Locate a zombie process by number
1184 sx_slock(&allproc_lock);
1185 p = zpfind_locked(pid);
1186 sx_sunlock(&allproc_lock);
1190 #ifdef COMPAT_FREEBSD32
1193 * This function is typically used to copy out the kernel address, so
1194 * it can be replaced by assignment of zero.
1196 static inline uint32_t
1197 ptr32_trim(void *ptr)
1201 uptr = (uintptr_t)ptr;
1202 return ((uptr > UINT_MAX) ? 0 : uptr);
1205 #define PTRTRIM_CP(src,dst,fld) \
1206 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1209 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1213 bzero(ki32, sizeof(struct kinfo_proc32));
1214 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1215 CP(*ki, *ki32, ki_layout);
1216 PTRTRIM_CP(*ki, *ki32, ki_args);
1217 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1218 PTRTRIM_CP(*ki, *ki32, ki_addr);
1219 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1220 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1221 PTRTRIM_CP(*ki, *ki32, ki_fd);
1222 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1223 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1224 CP(*ki, *ki32, ki_pid);
1225 CP(*ki, *ki32, ki_ppid);
1226 CP(*ki, *ki32, ki_pgid);
1227 CP(*ki, *ki32, ki_tpgid);
1228 CP(*ki, *ki32, ki_sid);
1229 CP(*ki, *ki32, ki_tsid);
1230 CP(*ki, *ki32, ki_jobc);
1231 CP(*ki, *ki32, ki_tdev);
1232 CP(*ki, *ki32, ki_siglist);
1233 CP(*ki, *ki32, ki_sigmask);
1234 CP(*ki, *ki32, ki_sigignore);
1235 CP(*ki, *ki32, ki_sigcatch);
1236 CP(*ki, *ki32, ki_uid);
1237 CP(*ki, *ki32, ki_ruid);
1238 CP(*ki, *ki32, ki_svuid);
1239 CP(*ki, *ki32, ki_rgid);
1240 CP(*ki, *ki32, ki_svgid);
1241 CP(*ki, *ki32, ki_ngroups);
1242 for (i = 0; i < KI_NGROUPS; i++)
1243 CP(*ki, *ki32, ki_groups[i]);
1244 CP(*ki, *ki32, ki_size);
1245 CP(*ki, *ki32, ki_rssize);
1246 CP(*ki, *ki32, ki_swrss);
1247 CP(*ki, *ki32, ki_tsize);
1248 CP(*ki, *ki32, ki_dsize);
1249 CP(*ki, *ki32, ki_ssize);
1250 CP(*ki, *ki32, ki_xstat);
1251 CP(*ki, *ki32, ki_acflag);
1252 CP(*ki, *ki32, ki_pctcpu);
1253 CP(*ki, *ki32, ki_estcpu);
1254 CP(*ki, *ki32, ki_slptime);
1255 CP(*ki, *ki32, ki_swtime);
1256 CP(*ki, *ki32, ki_cow);
1257 CP(*ki, *ki32, ki_runtime);
1258 TV_CP(*ki, *ki32, ki_start);
1259 TV_CP(*ki, *ki32, ki_childtime);
1260 CP(*ki, *ki32, ki_flag);
1261 CP(*ki, *ki32, ki_kiflag);
1262 CP(*ki, *ki32, ki_traceflag);
1263 CP(*ki, *ki32, ki_stat);
1264 CP(*ki, *ki32, ki_nice);
1265 CP(*ki, *ki32, ki_lock);
1266 CP(*ki, *ki32, ki_rqindex);
1267 CP(*ki, *ki32, ki_oncpu);
1268 CP(*ki, *ki32, ki_lastcpu);
1270 /* XXX TODO: wrap cpu value as appropriate */
1271 CP(*ki, *ki32, ki_oncpu_old);
1272 CP(*ki, *ki32, ki_lastcpu_old);
1274 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1275 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1276 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1277 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1278 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1279 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1280 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1281 CP(*ki, *ki32, ki_tracer);
1282 CP(*ki, *ki32, ki_flag2);
1283 CP(*ki, *ki32, ki_fibnum);
1284 CP(*ki, *ki32, ki_cr_flags);
1285 CP(*ki, *ki32, ki_jid);
1286 CP(*ki, *ki32, ki_numthreads);
1287 CP(*ki, *ki32, ki_tid);
1288 CP(*ki, *ki32, ki_pri);
1289 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1290 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1291 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1292 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1293 PTRTRIM_CP(*ki, *ki32, ki_udata);
1294 CP(*ki, *ki32, ki_sflag);
1295 CP(*ki, *ki32, ki_tdflags);
1300 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1303 struct kinfo_proc ki;
1304 #ifdef COMPAT_FREEBSD32
1305 struct kinfo_proc32 ki32;
1309 PROC_LOCK_ASSERT(p, MA_OWNED);
1310 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1313 fill_kinfo_proc(p, &ki);
1314 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1315 #ifdef COMPAT_FREEBSD32
1316 if ((flags & KERN_PROC_MASK32) != 0) {
1317 freebsd32_kinfo_proc_out(&ki, &ki32);
1318 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1322 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1325 FOREACH_THREAD_IN_PROC(p, td) {
1326 fill_kinfo_thread(td, &ki, 1);
1327 #ifdef COMPAT_FREEBSD32
1328 if ((flags & KERN_PROC_MASK32) != 0) {
1329 freebsd32_kinfo_proc_out(&ki, &ki32);
1330 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1334 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1345 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1349 struct kinfo_proc ki;
1355 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1356 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1357 error = kern_proc_out(p, &sb, flags);
1358 error2 = sbuf_finish(&sb);
1362 else if (error2 != 0)
1382 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1384 int *name = (int *)arg1;
1385 u_int namelen = arg2;
1387 int flags, doingzomb, oid_number;
1390 oid_number = oidp->oid_number;
1391 if (oid_number != KERN_PROC_ALL &&
1392 (oid_number & KERN_PROC_INC_THREAD) == 0)
1393 flags = KERN_PROC_NOTHREADS;
1396 oid_number &= ~KERN_PROC_INC_THREAD;
1398 #ifdef COMPAT_FREEBSD32
1399 if (req->flags & SCTL_MASK32)
1400 flags |= KERN_PROC_MASK32;
1402 if (oid_number == KERN_PROC_PID) {
1405 error = sysctl_wire_old_buffer(req, 0);
1408 sx_slock(&proctree_lock);
1409 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1411 error = sysctl_out_proc(p, req, flags, 0);
1412 sx_sunlock(&proctree_lock);
1416 switch (oid_number) {
1421 case KERN_PROC_PROC:
1422 if (namelen != 0 && namelen != 1)
1432 /* overestimate by 5 procs */
1433 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1437 error = sysctl_wire_old_buffer(req, 0);
1440 sx_slock(&proctree_lock);
1441 sx_slock(&allproc_lock);
1442 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1444 p = LIST_FIRST(&allproc);
1446 p = LIST_FIRST(&zombproc);
1447 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1449 * Skip embryonic processes.
1452 if (p->p_state == PRS_NEW) {
1456 KASSERT(p->p_ucred != NULL,
1457 ("process credential is NULL for non-NEW proc"));
1459 * Show a user only appropriate processes.
1461 if (p_cansee(curthread, p)) {
1466 * TODO - make more efficient (see notes below).
1469 switch (oid_number) {
1472 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1478 case KERN_PROC_PGRP:
1479 /* could do this by traversing pgrp */
1480 if (p->p_pgrp == NULL ||
1481 p->p_pgrp->pg_id != (pid_t)name[0]) {
1487 case KERN_PROC_RGID:
1488 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1494 case KERN_PROC_SESSION:
1495 if (p->p_session == NULL ||
1496 p->p_session->s_sid != (pid_t)name[0]) {
1503 if ((p->p_flag & P_CONTROLT) == 0 ||
1504 p->p_session == NULL) {
1508 /* XXX proctree_lock */
1509 SESS_LOCK(p->p_session);
1510 if (p->p_session->s_ttyp == NULL ||
1511 tty_udev(p->p_session->s_ttyp) !=
1513 SESS_UNLOCK(p->p_session);
1517 SESS_UNLOCK(p->p_session);
1521 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1527 case KERN_PROC_RUID:
1528 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1534 case KERN_PROC_PROC:
1542 error = sysctl_out_proc(p, req, flags, doingzomb);
1544 sx_sunlock(&allproc_lock);
1545 sx_sunlock(&proctree_lock);
1550 sx_sunlock(&allproc_lock);
1551 sx_sunlock(&proctree_lock);
1556 pargs_alloc(int len)
1560 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1562 refcount_init(&pa->ar_ref, 1);
1563 pa->ar_length = len;
1568 pargs_free(struct pargs *pa)
1575 pargs_hold(struct pargs *pa)
1580 refcount_acquire(&pa->ar_ref);
1584 pargs_drop(struct pargs *pa)
1589 if (refcount_release(&pa->ar_ref))
1594 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1600 * This may return a short read if the string is shorter than the chunk
1601 * and is aligned at the end of the page, and the following page is not
1604 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1610 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1612 enum proc_vector_type {
1618 #ifdef COMPAT_FREEBSD32
1620 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1621 size_t *vsizep, enum proc_vector_type type)
1623 struct freebsd32_ps_strings pss;
1625 vm_offset_t vptr, ptr;
1626 uint32_t *proc_vector32;
1632 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1633 sizeof(pss)) != sizeof(pss))
1637 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1638 vsize = pss.ps_nargvstr;
1639 if (vsize > ARG_MAX)
1641 size = vsize * sizeof(int32_t);
1644 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1645 vsize = pss.ps_nenvstr;
1646 if (vsize > ARG_MAX)
1648 size = vsize * sizeof(int32_t);
1651 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1652 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1655 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1656 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1659 if (aux.a_type == AT_NULL)
1663 if (aux.a_type != AT_NULL)
1666 size = vsize * sizeof(aux);
1669 KASSERT(0, ("Wrong proc vector type: %d", type));
1672 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1673 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1677 if (type == PROC_AUX) {
1678 *proc_vectorp = (char **)proc_vector32;
1682 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1683 for (i = 0; i < (int)vsize; i++)
1684 proc_vector[i] = PTRIN(proc_vector32[i]);
1685 *proc_vectorp = proc_vector;
1688 free(proc_vector32, M_TEMP);
1694 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1695 size_t *vsizep, enum proc_vector_type type)
1697 struct ps_strings pss;
1699 vm_offset_t vptr, ptr;
1704 #ifdef COMPAT_FREEBSD32
1705 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1706 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1708 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1709 sizeof(pss)) != sizeof(pss))
1713 vptr = (vm_offset_t)pss.ps_argvstr;
1714 vsize = pss.ps_nargvstr;
1715 if (vsize > ARG_MAX)
1717 size = vsize * sizeof(char *);
1720 vptr = (vm_offset_t)pss.ps_envstr;
1721 vsize = pss.ps_nenvstr;
1722 if (vsize > ARG_MAX)
1724 size = vsize * sizeof(char *);
1728 * The aux array is just above env array on the stack. Check
1729 * that the address is naturally aligned.
1731 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1733 #if __ELF_WORD_SIZE == 64
1734 if (vptr % sizeof(uint64_t) != 0)
1736 if (vptr % sizeof(uint32_t) != 0)
1740 * We count the array size reading the aux vectors from the
1741 * stack until AT_NULL vector is returned. So (to keep the code
1742 * simple) we read the process stack twice: the first time here
1743 * to find the size and the second time when copying the vectors
1744 * to the allocated proc_vector.
1746 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1747 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1750 if (aux.a_type == AT_NULL)
1755 * If the PROC_AUXV_MAX entries are iterated over, and we have
1756 * not reached AT_NULL, it is most likely we are reading wrong
1757 * data: either the process doesn't have auxv array or data has
1758 * been modified. Return the error in this case.
1760 if (aux.a_type != AT_NULL)
1763 size = vsize * sizeof(aux);
1766 KASSERT(0, ("Wrong proc vector type: %d", type));
1767 return (EINVAL); /* In case we are built without INVARIANTS. */
1769 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1770 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1771 free(proc_vector, M_TEMP);
1774 *proc_vectorp = proc_vector;
1780 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1783 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1784 enum proc_vector_type type)
1786 size_t done, len, nchr, vsize;
1788 char **proc_vector, *sptr;
1789 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1791 PROC_ASSERT_HELD(p);
1794 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1796 nchr = 2 * (PATH_MAX + ARG_MAX);
1798 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1801 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1803 * The program may have scribbled into its argv array, e.g. to
1804 * remove some arguments. If that has happened, break out
1805 * before trying to read from NULL.
1807 if (proc_vector[i] == NULL)
1809 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1810 error = proc_read_string(td, p, sptr, pss_string,
1811 sizeof(pss_string));
1814 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1815 if (done + len >= nchr)
1816 len = nchr - done - 1;
1817 sbuf_bcat(sb, pss_string, len);
1818 if (len != GET_PS_STRINGS_CHUNK_SZ)
1820 done += GET_PS_STRINGS_CHUNK_SZ;
1822 sbuf_bcat(sb, "", 1);
1826 free(proc_vector, M_TEMP);
1831 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1834 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1838 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1841 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1845 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1851 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1853 #ifdef COMPAT_FREEBSD32
1854 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1855 size = vsize * sizeof(Elf32_Auxinfo);
1858 size = vsize * sizeof(Elf_Auxinfo);
1859 if (sbuf_bcat(sb, auxv, size) != 0)
1867 * This sysctl allows a process to retrieve the argument list or process
1868 * title for another process without groping around in the address space
1869 * of the other process. It also allow a process to set its own "process
1870 * title to a string of its own choice.
1873 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1875 int *name = (int *)arg1;
1876 u_int namelen = arg2;
1877 struct pargs *newpa, *pa;
1880 int flags, error = 0, error2;
1885 flags = PGET_CANSEE;
1886 if (req->newptr != NULL)
1887 flags |= PGET_ISCURRENT;
1888 error = pget((pid_t)name[0], flags, &p);
1896 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1898 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1901 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1902 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1903 error = proc_getargv(curthread, p, &sb);
1904 error2 = sbuf_finish(&sb);
1907 if (error == 0 && error2 != 0)
1912 if (error != 0 || req->newptr == NULL)
1915 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1917 newpa = pargs_alloc(req->newlen);
1918 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1932 * This sysctl allows a process to retrieve environment of another process.
1935 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1937 int *name = (int *)arg1;
1938 u_int namelen = arg2;
1946 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1949 if ((p->p_flag & P_SYSTEM) != 0) {
1954 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1955 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1956 error = proc_getenvv(curthread, p, &sb);
1957 error2 = sbuf_finish(&sb);
1960 return (error != 0 ? error : error2);
1964 * This sysctl allows a process to retrieve ELF auxiliary vector of
1968 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1970 int *name = (int *)arg1;
1971 u_int namelen = arg2;
1979 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1982 if ((p->p_flag & P_SYSTEM) != 0) {
1986 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1987 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1988 error = proc_getauxv(curthread, p, &sb);
1989 error2 = sbuf_finish(&sb);
1992 return (error != 0 ? error : error2);
1996 * This sysctl allows a process to retrieve the path of the executable for
1997 * itself or another process.
2000 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2002 pid_t *pidp = (pid_t *)arg1;
2003 unsigned int arglen = arg2;
2006 char *retbuf, *freebuf;
2011 if (*pidp == -1) { /* -1 means this process */
2012 p = req->td->td_proc;
2014 error = pget(*pidp, PGET_CANSEE, &p);
2028 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2032 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2033 free(freebuf, M_TEMP);
2038 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2051 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2054 sv_name = p->p_sysent->sv_name;
2056 return (sysctl_handle_string(oidp, sv_name, 0, req));
2059 #ifdef KINFO_OVMENTRY_SIZE
2060 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2063 #ifdef COMPAT_FREEBSD7
2065 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2067 vm_map_entry_t entry, tmp_entry;
2068 unsigned int last_timestamp;
2069 char *fullpath, *freepath;
2070 struct kinfo_ovmentry *kve;
2080 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2083 vm = vmspace_acquire_ref(p);
2088 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2091 vm_map_lock_read(map);
2092 for (entry = map->header.next; entry != &map->header;
2093 entry = entry->next) {
2094 vm_object_t obj, tobj, lobj;
2097 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2100 bzero(kve, sizeof(*kve));
2101 kve->kve_structsize = sizeof(*kve);
2103 kve->kve_private_resident = 0;
2104 obj = entry->object.vm_object;
2106 VM_OBJECT_RLOCK(obj);
2107 if (obj->shadow_count == 1)
2108 kve->kve_private_resident =
2109 obj->resident_page_count;
2111 kve->kve_resident = 0;
2112 addr = entry->start;
2113 while (addr < entry->end) {
2114 if (pmap_extract(map->pmap, addr))
2115 kve->kve_resident++;
2119 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2121 VM_OBJECT_RLOCK(tobj);
2123 VM_OBJECT_RUNLOCK(lobj);
2127 kve->kve_start = (void*)entry->start;
2128 kve->kve_end = (void*)entry->end;
2129 kve->kve_offset = (off_t)entry->offset;
2131 if (entry->protection & VM_PROT_READ)
2132 kve->kve_protection |= KVME_PROT_READ;
2133 if (entry->protection & VM_PROT_WRITE)
2134 kve->kve_protection |= KVME_PROT_WRITE;
2135 if (entry->protection & VM_PROT_EXECUTE)
2136 kve->kve_protection |= KVME_PROT_EXEC;
2138 if (entry->eflags & MAP_ENTRY_COW)
2139 kve->kve_flags |= KVME_FLAG_COW;
2140 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2141 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2142 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2143 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2145 last_timestamp = map->timestamp;
2146 vm_map_unlock_read(map);
2148 kve->kve_fileid = 0;
2154 switch (lobj->type) {
2156 kve->kve_type = KVME_TYPE_DEFAULT;
2159 kve->kve_type = KVME_TYPE_VNODE;
2164 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2165 kve->kve_type = KVME_TYPE_VNODE;
2166 if ((lobj->flags & OBJ_TMPFS) != 0) {
2167 vp = lobj->un_pager.swp.swp_tmpfs;
2171 kve->kve_type = KVME_TYPE_SWAP;
2175 kve->kve_type = KVME_TYPE_DEVICE;
2178 kve->kve_type = KVME_TYPE_PHYS;
2181 kve->kve_type = KVME_TYPE_DEAD;
2184 kve->kve_type = KVME_TYPE_SG;
2187 kve->kve_type = KVME_TYPE_UNKNOWN;
2191 VM_OBJECT_RUNLOCK(lobj);
2193 kve->kve_ref_count = obj->ref_count;
2194 kve->kve_shadow_count = obj->shadow_count;
2195 VM_OBJECT_RUNLOCK(obj);
2197 vn_fullpath(curthread, vp, &fullpath,
2199 cred = curthread->td_ucred;
2200 vn_lock(vp, LK_SHARED | LK_RETRY);
2201 if (VOP_GETATTR(vp, &va, cred) == 0) {
2202 kve->kve_fileid = va.va_fileid;
2203 kve->kve_fsid = va.va_fsid;
2208 kve->kve_type = KVME_TYPE_NONE;
2209 kve->kve_ref_count = 0;
2210 kve->kve_shadow_count = 0;
2213 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2214 if (freepath != NULL)
2215 free(freepath, M_TEMP);
2217 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2218 vm_map_lock_read(map);
2221 if (last_timestamp != map->timestamp) {
2222 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2226 vm_map_unlock_read(map);
2232 #endif /* COMPAT_FREEBSD7 */
2234 #ifdef KINFO_VMENTRY_SIZE
2235 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2239 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2240 struct kinfo_vmentry *kve)
2242 vm_object_t obj, tobj;
2245 vm_paddr_t locked_pa;
2246 vm_pindex_t pi, pi_adv, pindex;
2249 obj = entry->object.vm_object;
2250 addr = entry->start;
2252 pi = OFF_TO_IDX(entry->offset);
2253 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2254 if (m_adv != NULL) {
2257 pi_adv = OFF_TO_IDX(entry->end - addr);
2259 for (tobj = obj;; tobj = tobj->backing_object) {
2260 m = vm_page_find_least(tobj, pindex);
2262 if (m->pindex == pindex)
2264 if (pi_adv > m->pindex - pindex) {
2265 pi_adv = m->pindex - pindex;
2269 if (tobj->backing_object == NULL)
2271 pindex += OFF_TO_IDX(tobj->
2272 backing_object_offset);
2276 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2277 (addr & (pagesizes[1] - 1)) == 0 &&
2278 (pmap_mincore(map->pmap, addr, &locked_pa) &
2279 MINCORE_SUPER) != 0) {
2280 kve->kve_flags |= KVME_FLAG_SUPER;
2281 pi_adv = OFF_TO_IDX(pagesizes[1]);
2284 * We do not test the found page on validity.
2285 * Either the page is busy and being paged in,
2286 * or it was invalidated. The first case
2287 * should be counted as resident, the second
2288 * is not so clear; we do account both.
2292 kve->kve_resident += pi_adv;
2295 PA_UNLOCK_COND(locked_pa);
2299 * Must be called with the process locked and will return unlocked.
2302 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2304 vm_map_entry_t entry, tmp_entry;
2307 vm_object_t obj, tobj, lobj;
2308 char *fullpath, *freepath;
2309 struct kinfo_vmentry *kve;
2314 unsigned int last_timestamp;
2317 PROC_LOCK_ASSERT(p, MA_OWNED);
2321 vm = vmspace_acquire_ref(p);
2326 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2330 vm_map_lock_read(map);
2331 for (entry = map->header.next; entry != &map->header;
2332 entry = entry->next) {
2333 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2337 bzero(kve, sizeof(*kve));
2338 obj = entry->object.vm_object;
2340 for (tobj = obj; tobj != NULL;
2341 tobj = tobj->backing_object) {
2342 VM_OBJECT_RLOCK(tobj);
2345 if (obj->backing_object == NULL)
2346 kve->kve_private_resident =
2347 obj->resident_page_count;
2348 if (!vmmap_skip_res_cnt)
2349 kern_proc_vmmap_resident(map, entry, kve);
2350 for (tobj = obj; tobj != NULL;
2351 tobj = tobj->backing_object) {
2352 if (tobj != obj && tobj != lobj)
2353 VM_OBJECT_RUNLOCK(tobj);
2359 kve->kve_start = entry->start;
2360 kve->kve_end = entry->end;
2361 kve->kve_offset = entry->offset;
2363 if (entry->protection & VM_PROT_READ)
2364 kve->kve_protection |= KVME_PROT_READ;
2365 if (entry->protection & VM_PROT_WRITE)
2366 kve->kve_protection |= KVME_PROT_WRITE;
2367 if (entry->protection & VM_PROT_EXECUTE)
2368 kve->kve_protection |= KVME_PROT_EXEC;
2370 if (entry->eflags & MAP_ENTRY_COW)
2371 kve->kve_flags |= KVME_FLAG_COW;
2372 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2373 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2374 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2375 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2376 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2377 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2378 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2379 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2381 last_timestamp = map->timestamp;
2382 vm_map_unlock_read(map);
2388 switch (lobj->type) {
2390 kve->kve_type = KVME_TYPE_DEFAULT;
2393 kve->kve_type = KVME_TYPE_VNODE;
2398 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2399 kve->kve_type = KVME_TYPE_VNODE;
2400 if ((lobj->flags & OBJ_TMPFS) != 0) {
2401 vp = lobj->un_pager.swp.swp_tmpfs;
2405 kve->kve_type = KVME_TYPE_SWAP;
2409 kve->kve_type = KVME_TYPE_DEVICE;
2412 kve->kve_type = KVME_TYPE_PHYS;
2415 kve->kve_type = KVME_TYPE_DEAD;
2418 kve->kve_type = KVME_TYPE_SG;
2420 case OBJT_MGTDEVICE:
2421 kve->kve_type = KVME_TYPE_MGTDEVICE;
2424 kve->kve_type = KVME_TYPE_UNKNOWN;
2428 VM_OBJECT_RUNLOCK(lobj);
2430 kve->kve_ref_count = obj->ref_count;
2431 kve->kve_shadow_count = obj->shadow_count;
2432 VM_OBJECT_RUNLOCK(obj);
2434 vn_fullpath(curthread, vp, &fullpath,
2436 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2437 cred = curthread->td_ucred;
2438 vn_lock(vp, LK_SHARED | LK_RETRY);
2439 if (VOP_GETATTR(vp, &va, cred) == 0) {
2440 kve->kve_vn_fileid = va.va_fileid;
2441 kve->kve_vn_fsid = va.va_fsid;
2443 MAKEIMODE(va.va_type, va.va_mode);
2444 kve->kve_vn_size = va.va_size;
2445 kve->kve_vn_rdev = va.va_rdev;
2446 kve->kve_status = KF_ATTR_VALID;
2451 kve->kve_type = KVME_TYPE_NONE;
2452 kve->kve_ref_count = 0;
2453 kve->kve_shadow_count = 0;
2456 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2457 if (freepath != NULL)
2458 free(freepath, M_TEMP);
2460 /* Pack record size down */
2461 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2462 kve->kve_structsize =
2463 offsetof(struct kinfo_vmentry, kve_path) +
2464 strlen(kve->kve_path) + 1;
2466 kve->kve_structsize = sizeof(*kve);
2467 kve->kve_structsize = roundup(kve->kve_structsize,
2470 /* Halt filling and truncate rather than exceeding maxlen */
2471 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2473 vm_map_lock_read(map);
2475 } else if (maxlen != -1)
2476 maxlen -= kve->kve_structsize;
2478 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2480 vm_map_lock_read(map);
2483 if (last_timestamp != map->timestamp) {
2484 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2488 vm_map_unlock_read(map);
2496 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2500 int error, error2, *name;
2503 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2504 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2505 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2510 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2511 error2 = sbuf_finish(&sb);
2513 return (error != 0 ? error : error2);
2516 #if defined(STACK) || defined(DDB)
2518 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2520 struct kinfo_kstack *kkstp;
2521 int error, i, *name, numthreads;
2522 lwpid_t *lwpidarray;
2529 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2533 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2534 st = stack_create();
2540 if (numthreads < p->p_numthreads) {
2541 if (lwpidarray != NULL) {
2542 free(lwpidarray, M_TEMP);
2545 numthreads = p->p_numthreads;
2547 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2555 * XXXRW: During the below loop, execve(2) and countless other sorts
2556 * of changes could have taken place. Should we check to see if the
2557 * vmspace has been replaced, or the like, in order to prevent
2558 * giving a snapshot that spans, say, execve(2), with some threads
2559 * before and some after? Among other things, the credentials could
2560 * have changed, in which case the right to extract debug info might
2561 * no longer be assured.
2563 FOREACH_THREAD_IN_PROC(p, td) {
2564 KASSERT(i < numthreads,
2565 ("sysctl_kern_proc_kstack: numthreads"));
2566 lwpidarray[i] = td->td_tid;
2570 for (i = 0; i < numthreads; i++) {
2571 td = thread_find(p, lwpidarray[i]);
2575 bzero(kkstp, sizeof(*kkstp));
2576 (void)sbuf_new(&sb, kkstp->kkst_trace,
2577 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2579 kkstp->kkst_tid = td->td_tid;
2580 if (TD_IS_SWAPPED(td)) {
2581 kkstp->kkst_state = KKST_STATE_SWAPPED;
2582 } else if (TD_IS_RUNNING(td)) {
2583 if (stack_save_td_running(st, td) == 0)
2584 kkstp->kkst_state = KKST_STATE_STACKOK;
2586 kkstp->kkst_state = KKST_STATE_RUNNING;
2588 kkstp->kkst_state = KKST_STATE_STACKOK;
2589 stack_save_td(st, td);
2593 stack_sbuf_print(&sb, st);
2596 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2603 if (lwpidarray != NULL)
2604 free(lwpidarray, M_TEMP);
2606 free(kkstp, M_TEMP);
2612 * This sysctl allows a process to retrieve the full list of groups from
2613 * itself or another process.
2616 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2618 pid_t *pidp = (pid_t *)arg1;
2619 unsigned int arglen = arg2;
2626 if (*pidp == -1) { /* -1 means this process */
2627 p = req->td->td_proc;
2630 error = pget(*pidp, PGET_CANSEE, &p);
2635 cred = crhold(p->p_ucred);
2638 error = SYSCTL_OUT(req, cred->cr_groups,
2639 cred->cr_ngroups * sizeof(gid_t));
2645 * This sysctl allows a process to retrieve or/and set the resource limit for
2649 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2651 int *name = (int *)arg1;
2652 u_int namelen = arg2;
2661 which = (u_int)name[1];
2662 if (which >= RLIM_NLIMITS)
2665 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2668 flags = PGET_HOLD | PGET_NOTWEXIT;
2669 if (req->newptr != NULL)
2670 flags |= PGET_CANDEBUG;
2672 flags |= PGET_CANSEE;
2673 error = pget((pid_t)name[0], flags, &p);
2680 if (req->oldptr != NULL) {
2682 lim_rlimit_proc(p, which, &rlim);
2685 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2692 if (req->newptr != NULL) {
2693 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2695 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2704 * This sysctl allows a process to retrieve ps_strings structure location of
2708 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2710 int *name = (int *)arg1;
2711 u_int namelen = arg2;
2713 vm_offset_t ps_strings;
2715 #ifdef COMPAT_FREEBSD32
2716 uint32_t ps_strings32;
2722 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2725 #ifdef COMPAT_FREEBSD32
2726 if ((req->flags & SCTL_MASK32) != 0) {
2728 * We return 0 if the 32 bit emulation request is for a 64 bit
2731 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2732 PTROUT(p->p_sysent->sv_psstrings) : 0;
2734 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2738 ps_strings = p->p_sysent->sv_psstrings;
2740 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2745 * This sysctl allows a process to retrieve umask of another process.
2748 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2750 int *name = (int *)arg1;
2751 u_int namelen = arg2;
2759 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2763 FILEDESC_SLOCK(p->p_fd);
2764 fd_cmask = p->p_fd->fd_cmask;
2765 FILEDESC_SUNLOCK(p->p_fd);
2767 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2772 * This sysctl allows a process to set and retrieve binary osreldate of
2776 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2778 int *name = (int *)arg1;
2779 u_int namelen = arg2;
2781 int flags, error, osrel;
2786 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2789 flags = PGET_HOLD | PGET_NOTWEXIT;
2790 if (req->newptr != NULL)
2791 flags |= PGET_CANDEBUG;
2793 flags |= PGET_CANSEE;
2794 error = pget((pid_t)name[0], flags, &p);
2798 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2802 if (req->newptr != NULL) {
2803 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2818 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2820 int *name = (int *)arg1;
2821 u_int namelen = arg2;
2823 struct kinfo_sigtramp kst;
2824 const struct sysentvec *sv;
2826 #ifdef COMPAT_FREEBSD32
2827 struct kinfo_sigtramp32 kst32;
2833 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2837 #ifdef COMPAT_FREEBSD32
2838 if ((req->flags & SCTL_MASK32) != 0) {
2839 bzero(&kst32, sizeof(kst32));
2840 if (SV_PROC_FLAG(p, SV_ILP32)) {
2841 if (sv->sv_sigcode_base != 0) {
2842 kst32.ksigtramp_start = sv->sv_sigcode_base;
2843 kst32.ksigtramp_end = sv->sv_sigcode_base +
2846 kst32.ksigtramp_start = sv->sv_psstrings -
2848 kst32.ksigtramp_end = sv->sv_psstrings;
2852 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2856 bzero(&kst, sizeof(kst));
2857 if (sv->sv_sigcode_base != 0) {
2858 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2859 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2862 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2864 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2867 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2871 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2873 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2874 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2875 "Return entire process table");
2877 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2878 sysctl_kern_proc, "Process table");
2880 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2881 sysctl_kern_proc, "Process table");
2883 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2884 sysctl_kern_proc, "Process table");
2886 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2887 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2889 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2890 sysctl_kern_proc, "Process table");
2892 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2893 sysctl_kern_proc, "Process table");
2895 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2896 sysctl_kern_proc, "Process table");
2898 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2899 sysctl_kern_proc, "Process table");
2901 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2902 sysctl_kern_proc, "Return process table, no threads");
2904 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2905 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2906 sysctl_kern_proc_args, "Process argument list");
2908 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2909 sysctl_kern_proc_env, "Process environment");
2911 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2912 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2914 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2915 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2917 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2918 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2919 "Process syscall vector name (ABI type)");
2921 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2922 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2924 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2925 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2927 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2928 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2930 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2931 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2933 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2934 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2936 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2937 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2939 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2940 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2942 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2943 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2945 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2946 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2947 "Return process table, no threads");
2949 #ifdef COMPAT_FREEBSD7
2950 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2951 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2954 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2955 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2957 #if defined(STACK) || defined(DDB)
2958 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2959 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2962 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2963 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2965 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2966 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2967 "Process resource limits");
2969 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2970 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2971 "Process ps_strings location");
2973 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2974 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2976 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2977 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2978 "Process binary osreldate");
2980 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2981 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2982 "Process signal trampoline location");
2989 struct proc *cp, *p;
2991 bool restart, seen_stopped, seen_exiting, stopped_some;
2995 * stop_all_proc() assumes that all process which have
2996 * usermode must be stopped, except current process, for
2997 * obvious reasons. Since other threads in the process
2998 * establishing global stop could unstop something, disable
2999 * calls from multithreaded processes as precaution. The
3000 * service must not be user-callable anyway.
3002 KASSERT((cp->p_flag & P_HADTHREADS) == 0 ||
3003 (cp->p_flag & P_KTHREAD) != 0, ("mt stop_all_proc"));
3006 sx_xlock(&allproc_lock);
3008 seen_exiting = seen_stopped = stopped_some = restart = false;
3009 LIST_REMOVE(cp, p_list);
3010 LIST_INSERT_HEAD(&allproc, cp, p_list);
3012 p = LIST_NEXT(cp, p_list);
3015 LIST_REMOVE(cp, p_list);
3016 LIST_INSERT_AFTER(p, cp, p_list);
3018 if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
3019 P_TOTAL_STOP)) != 0) {
3023 if ((p->p_flag & P_WEXIT) != 0) {
3024 seen_exiting = true;
3028 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3030 * Stopped processes are tolerated when there
3031 * are no other processes which might continue
3032 * them. P_STOPPED_SINGLE but not
3033 * P_TOTAL_STOP process still has at least one
3036 seen_stopped = true;
3041 sx_xunlock(&allproc_lock);
3042 r = thread_single(p, SINGLE_ALLPROC);
3046 stopped_some = true;
3049 sx_xlock(&allproc_lock);
3051 /* Catch forked children we did not see in iteration. */
3052 if (gen != allproc_gen)
3054 sx_xunlock(&allproc_lock);
3055 if (restart || stopped_some || seen_exiting || seen_stopped) {
3056 kern_yield(PRI_USER);
3062 resume_all_proc(void)
3064 struct proc *cp, *p;
3067 sx_xlock(&allproc_lock);
3068 LIST_REMOVE(cp, p_list);
3069 LIST_INSERT_HEAD(&allproc, cp, p_list);
3071 p = LIST_NEXT(cp, p_list);
3074 LIST_REMOVE(cp, p_list);
3075 LIST_INSERT_AFTER(p, cp, p_list);
3077 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3078 sx_xunlock(&allproc_lock);
3080 thread_single_end(p, SINGLE_ALLPROC);
3083 sx_xlock(&allproc_lock);
3088 sx_xunlock(&allproc_lock);
3091 #define TOTAL_STOP_DEBUG 1
3092 #ifdef TOTAL_STOP_DEBUG
3093 volatile static int ap_resume;
3094 #include <sys/mount.h>
3097 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3103 error = sysctl_handle_int(oidp, &val, 0, req);
3104 if (error != 0 || req->newptr == NULL)
3109 while (ap_resume == 0)
3117 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3118 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3119 sysctl_debug_stop_all_proc, "I",