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 * 3. 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)
196 p = (struct proc *)mem;
197 SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
198 EVENTHANDLER_INVOKE(process_ctor, p);
199 SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
200 td = FIRST_THREAD_IN_PROC(p);
202 /* Make sure all thread constructors are executed */
203 EVENTHANDLER_INVOKE(thread_ctor, td);
209 * Reclaim a proc after use.
212 proc_dtor(void *mem, int size, void *arg)
217 /* INVARIANTS checks go here */
218 p = (struct proc *)mem;
219 td = FIRST_THREAD_IN_PROC(p);
220 SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
223 KASSERT((p->p_numthreads == 1),
224 ("bad number of threads in exiting process"));
225 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
227 /* Free all OSD associated to this thread. */
230 /* Make sure all thread destructors are executed */
231 EVENTHANDLER_INVOKE(thread_dtor, td);
233 EVENTHANDLER_INVOKE(process_dtor, p);
234 if (p->p_ksi != NULL)
235 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
236 SDT_PROBE3(proc, , dtor, return, p, size, arg);
240 * Initialize type-stable parts of a proc (when newly created).
243 proc_init(void *mem, int size, int flags)
247 p = (struct proc *)mem;
248 SDT_PROBE3(proc, , init, entry, p, size, flags);
249 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
250 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
251 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
252 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
253 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
254 cv_init(&p->p_pwait, "ppwait");
255 cv_init(&p->p_dbgwait, "dbgwait");
256 TAILQ_INIT(&p->p_threads); /* all threads in proc */
257 EVENTHANDLER_INVOKE(process_init, p);
258 p->p_stats = pstats_alloc();
260 SDT_PROBE3(proc, , init, return, p, size, flags);
265 * UMA should ensure that this function is never called.
266 * Freeing a proc structure would violate type stability.
269 proc_fini(void *mem, int size)
274 p = (struct proc *)mem;
275 EVENTHANDLER_INVOKE(process_fini, p);
276 pstats_free(p->p_stats);
277 thread_free(FIRST_THREAD_IN_PROC(p));
278 mtx_destroy(&p->p_mtx);
279 if (p->p_ksi != NULL)
280 ksiginfo_free(p->p_ksi);
282 panic("proc reclaimed");
287 * Is p an inferior of the current process?
290 inferior(struct proc *p)
293 sx_assert(&proctree_lock, SX_LOCKED);
294 PROC_LOCK_ASSERT(p, MA_OWNED);
295 for (; p != curproc; p = proc_realparent(p)) {
303 pfind_locked(pid_t pid)
307 sx_assert(&allproc_lock, SX_LOCKED);
308 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
309 if (p->p_pid == pid) {
311 if (p->p_state == PRS_NEW) {
322 * Locate a process by number; return only "live" processes -- i.e., neither
323 * zombies nor newly born but incompletely initialized processes. By not
324 * returning processes in the PRS_NEW state, we allow callers to avoid
325 * testing for that condition to avoid dereferencing p_ucred, et al.
332 sx_slock(&allproc_lock);
333 p = pfind_locked(pid);
334 sx_sunlock(&allproc_lock);
339 pfind_tid_locked(pid_t tid)
344 sx_assert(&allproc_lock, SX_LOCKED);
345 FOREACH_PROC_IN_SYSTEM(p) {
347 if (p->p_state == PRS_NEW) {
351 FOREACH_THREAD_IN_PROC(p, td) {
352 if (td->td_tid == tid)
362 * Locate a process group by number.
363 * The caller must hold proctree_lock.
370 sx_assert(&proctree_lock, SX_LOCKED);
372 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
373 if (pgrp->pg_id == pgid) {
382 * Locate process and do additional manipulations, depending on flags.
385 pget(pid_t pid, int flags, struct proc **pp)
390 sx_slock(&allproc_lock);
391 if (pid <= PID_MAX) {
392 p = pfind_locked(pid);
393 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
394 p = zpfind_locked(pid);
395 } else if ((flags & PGET_NOTID) == 0) {
396 p = pfind_tid_locked(pid);
400 sx_sunlock(&allproc_lock);
403 if ((flags & PGET_CANSEE) != 0) {
404 error = p_cansee(curthread, p);
408 if ((flags & PGET_CANDEBUG) != 0) {
409 error = p_candebug(curthread, p);
413 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
417 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
421 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
423 * XXXRW: Not clear ESRCH is the right error during proc
429 if ((flags & PGET_HOLD) != 0) {
441 * Create a new process group.
442 * pgid must be equal to the pid of p.
443 * Begin a new session if required.
446 enterpgrp(struct proc *p, pid_t pgid, struct pgrp *pgrp, struct session *sess)
449 sx_assert(&proctree_lock, SX_XLOCKED);
451 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
452 KASSERT(p->p_pid == pgid,
453 ("enterpgrp: new pgrp and pid != pgid"));
454 KASSERT(pgfind(pgid) == NULL,
455 ("enterpgrp: pgrp with pgid exists"));
456 KASSERT(!SESS_LEADER(p),
457 ("enterpgrp: session leader attempted setpgrp"));
459 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
465 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
467 p->p_flag &= ~P_CONTROLT;
471 sess->s_sid = p->p_pid;
472 refcount_init(&sess->s_count, 1);
473 sess->s_ttyvp = NULL;
474 sess->s_ttydp = NULL;
476 bcopy(p->p_session->s_login, sess->s_login,
477 sizeof(sess->s_login));
478 pgrp->pg_session = sess;
479 KASSERT(p == curproc,
480 ("enterpgrp: mksession and p != curproc"));
482 pgrp->pg_session = p->p_session;
483 sess_hold(pgrp->pg_session);
487 LIST_INIT(&pgrp->pg_members);
490 * As we have an exclusive lock of proctree_lock,
491 * this should not deadlock.
493 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
495 SLIST_INIT(&pgrp->pg_sigiolst);
498 doenterpgrp(p, pgrp);
504 * Move p to an existing process group
507 enterthispgrp(struct proc *p, struct pgrp *pgrp)
510 sx_assert(&proctree_lock, SX_XLOCKED);
511 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
512 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
513 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
514 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
515 KASSERT(pgrp->pg_session == p->p_session,
516 ("%s: pgrp's session %p, p->p_session %p.\n",
520 KASSERT(pgrp != p->p_pgrp,
521 ("%s: p belongs to pgrp.", __func__));
523 doenterpgrp(p, pgrp);
529 * Move p to a process group
532 doenterpgrp(struct proc *p, struct pgrp *pgrp)
534 struct pgrp *savepgrp;
536 sx_assert(&proctree_lock, SX_XLOCKED);
537 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
538 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
539 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
540 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
542 savepgrp = p->p_pgrp;
545 * Adjust eligibility of affected pgrps to participate in job control.
546 * Increment eligibility counts before decrementing, otherwise we
547 * could reach 0 spuriously during the first call.
550 fixjobc(p, p->p_pgrp, 0);
555 LIST_REMOVE(p, p_pglist);
558 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
559 PGRP_UNLOCK(savepgrp);
561 if (LIST_EMPTY(&savepgrp->pg_members))
566 * remove process from process group
569 leavepgrp(struct proc *p)
571 struct pgrp *savepgrp;
573 sx_assert(&proctree_lock, SX_XLOCKED);
574 savepgrp = p->p_pgrp;
577 LIST_REMOVE(p, p_pglist);
580 PGRP_UNLOCK(savepgrp);
581 if (LIST_EMPTY(&savepgrp->pg_members))
587 * delete a process group
590 pgdelete(struct pgrp *pgrp)
592 struct session *savesess;
595 sx_assert(&proctree_lock, SX_XLOCKED);
596 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
597 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
600 * Reset any sigio structures pointing to us as a result of
601 * F_SETOWN with our pgid.
603 funsetownlst(&pgrp->pg_sigiolst);
606 tp = pgrp->pg_session->s_ttyp;
607 LIST_REMOVE(pgrp, pg_hash);
608 savesess = pgrp->pg_session;
611 /* Remove the reference to the pgrp before deallocating it. */
614 tty_rel_pgrp(tp, pgrp);
617 mtx_destroy(&pgrp->pg_mtx);
619 sess_release(savesess);
623 pgadjustjobc(struct pgrp *pgrp, int entering)
631 if (pgrp->pg_jobc == 0)
638 * Adjust pgrp jobc counters when specified process changes process group.
639 * We count the number of processes in each process group that "qualify"
640 * the group for terminal job control (those with a parent in a different
641 * process group of the same session). If that count reaches zero, the
642 * process group becomes orphaned. Check both the specified process'
643 * process group and that of its children.
644 * entering == 0 => p is leaving specified group.
645 * entering == 1 => p is entering specified group.
648 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
650 struct pgrp *hispgrp;
651 struct session *mysession;
654 sx_assert(&proctree_lock, SX_LOCKED);
655 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
656 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
657 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
660 * Check p's parent to see whether p qualifies its own process
661 * group; if so, adjust count for p's process group.
663 mysession = pgrp->pg_session;
664 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
665 hispgrp->pg_session == mysession)
666 pgadjustjobc(pgrp, entering);
669 * Check this process' children to see whether they qualify
670 * their process groups; if so, adjust counts for children's
673 LIST_FOREACH(q, &p->p_children, p_sibling) {
675 if (hispgrp == pgrp ||
676 hispgrp->pg_session != mysession)
678 if (q->p_state == PRS_ZOMBIE)
680 pgadjustjobc(hispgrp, entering);
693 MPASS(p->p_flag & P_WEXIT);
695 * Do a quick check to see if there is anything to do with the
696 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
699 if (!SESS_LEADER(p) &&
700 (p->p_pgrp == p->p_pptr->p_pgrp) &&
701 LIST_EMPTY(&p->p_children)) {
707 sx_xlock(&proctree_lock);
708 if (SESS_LEADER(p)) {
712 * s_ttyp is not zero'd; we use this to indicate that
713 * the session once had a controlling terminal. (for
714 * logging and informational purposes)
725 * Signal foreground pgrp and revoke access to
726 * controlling terminal if it has not been revoked
729 * Because the TTY may have been revoked in the mean
730 * time and could already have a new session associated
731 * with it, make sure we don't send a SIGHUP to a
732 * foreground process group that does not belong to this
738 if (tp->t_session == sp)
739 tty_signal_pgrp(tp, SIGHUP);
744 sx_xunlock(&proctree_lock);
745 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
746 VOP_REVOKE(ttyvp, REVOKEALL);
747 VOP_UNLOCK(ttyvp, 0);
750 sx_xlock(&proctree_lock);
753 fixjobc(p, p->p_pgrp, 0);
754 sx_xunlock(&proctree_lock);
758 * A process group has become orphaned;
759 * if there are any stopped processes in the group,
760 * hang-up all process in that group.
763 orphanpg(struct pgrp *pg)
767 PGRP_LOCK_ASSERT(pg, MA_OWNED);
769 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
771 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
773 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
775 kern_psignal(p, SIGHUP);
776 kern_psignal(p, SIGCONT);
786 sess_hold(struct session *s)
789 refcount_acquire(&s->s_count);
793 sess_release(struct session *s)
796 if (refcount_release(&s->s_count)) {
797 if (s->s_ttyp != NULL) {
799 tty_rel_sess(s->s_ttyp, s);
801 mtx_destroy(&s->s_mtx);
808 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
814 for (i = 0; i <= pgrphash; i++) {
815 if (!LIST_EMPTY(&pgrphashtbl[i])) {
816 printf("\tindx %d\n", i);
817 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
819 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
820 (void *)pgrp, (long)pgrp->pg_id,
821 (void *)pgrp->pg_session,
822 pgrp->pg_session->s_count,
823 (void *)LIST_FIRST(&pgrp->pg_members));
824 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
825 printf("\t\tpid %ld addr %p pgrp %p\n",
826 (long)p->p_pid, (void *)p,
836 * Calculate the kinfo_proc members which contain process-wide
838 * Must be called with the target process locked.
841 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
845 PROC_LOCK_ASSERT(p, MA_OWNED);
849 FOREACH_THREAD_IN_PROC(p, td) {
851 kp->ki_pctcpu += sched_pctcpu(td);
852 kp->ki_estcpu += sched_estcpu(td);
858 * Clear kinfo_proc and fill in any information that is common
859 * to all threads in the process.
860 * Must be called with the target process locked.
863 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
870 struct timeval boottime;
872 /* For proc_realparent. */
873 sx_assert(&proctree_lock, SX_LOCKED);
874 PROC_LOCK_ASSERT(p, MA_OWNED);
875 bzero(kp, sizeof(*kp));
877 kp->ki_structsize = sizeof(*kp);
879 kp->ki_addr =/* p->p_addr; */0; /* XXX */
880 kp->ki_args = p->p_args;
881 kp->ki_textvp = p->p_textvp;
883 kp->ki_tracep = p->p_tracevp;
884 kp->ki_traceflag = p->p_traceflag;
887 kp->ki_vmspace = p->p_vmspace;
888 kp->ki_flag = p->p_flag;
889 kp->ki_flag2 = p->p_flag2;
892 kp->ki_uid = cred->cr_uid;
893 kp->ki_ruid = cred->cr_ruid;
894 kp->ki_svuid = cred->cr_svuid;
896 if (cred->cr_flags & CRED_FLAG_CAPMODE)
897 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
898 /* XXX bde doesn't like KI_NGROUPS */
899 if (cred->cr_ngroups > KI_NGROUPS) {
900 kp->ki_ngroups = KI_NGROUPS;
901 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
903 kp->ki_ngroups = cred->cr_ngroups;
904 bcopy(cred->cr_groups, kp->ki_groups,
905 kp->ki_ngroups * sizeof(gid_t));
906 kp->ki_rgid = cred->cr_rgid;
907 kp->ki_svgid = cred->cr_svgid;
908 /* If jailed(cred), emulate the old P_JAILED flag. */
910 kp->ki_flag |= P_JAILED;
911 /* If inside the jail, use 0 as a jail ID. */
912 if (cred->cr_prison != curthread->td_ucred->cr_prison)
913 kp->ki_jid = cred->cr_prison->pr_id;
915 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
916 sizeof(kp->ki_loginclass));
920 mtx_lock(&ps->ps_mtx);
921 kp->ki_sigignore = ps->ps_sigignore;
922 kp->ki_sigcatch = ps->ps_sigcatch;
923 mtx_unlock(&ps->ps_mtx);
925 if (p->p_state != PRS_NEW &&
926 p->p_state != PRS_ZOMBIE &&
927 p->p_vmspace != NULL) {
928 struct vmspace *vm = p->p_vmspace;
930 kp->ki_size = vm->vm_map.size;
931 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
932 FOREACH_THREAD_IN_PROC(p, td0) {
933 if (!TD_IS_SWAPPED(td0))
934 kp->ki_rssize += td0->td_kstack_pages;
936 kp->ki_swrss = vm->vm_swrss;
937 kp->ki_tsize = vm->vm_tsize;
938 kp->ki_dsize = vm->vm_dsize;
939 kp->ki_ssize = vm->vm_ssize;
940 } else if (p->p_state == PRS_ZOMBIE)
942 if (kp->ki_flag & P_INMEM)
943 kp->ki_sflag = PS_INMEM;
946 /* Calculate legacy swtime as seconds since 'swtick'. */
947 kp->ki_swtime = (ticks - p->p_swtick) / hz;
948 kp->ki_pid = p->p_pid;
949 kp->ki_nice = p->p_nice;
950 kp->ki_fibnum = p->p_fibnum;
951 kp->ki_start = p->p_stats->p_start;
952 getboottime(&boottime);
953 timevaladd(&kp->ki_start, &boottime);
955 rufetch(p, &kp->ki_rusage);
956 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
957 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
959 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
960 /* Some callers want child times in a single value. */
961 kp->ki_childtime = kp->ki_childstime;
962 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
964 FOREACH_THREAD_IN_PROC(p, td0)
965 kp->ki_cow += td0->td_cow;
969 kp->ki_pgid = p->p_pgrp->pg_id;
970 kp->ki_jobc = p->p_pgrp->pg_jobc;
971 sp = p->p_pgrp->pg_session;
974 kp->ki_sid = sp->s_sid;
976 strlcpy(kp->ki_login, sp->s_login,
977 sizeof(kp->ki_login));
979 kp->ki_kiflag |= KI_CTTY;
981 kp->ki_kiflag |= KI_SLEADER;
982 /* XXX proctree_lock */
987 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
988 kp->ki_tdev = tty_udev(tp);
989 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
991 kp->ki_tsid = tp->t_session->s_sid;
994 if (p->p_comm[0] != '\0')
995 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
996 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
997 p->p_sysent->sv_name[0] != '\0')
998 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
999 kp->ki_siglist = p->p_siglist;
1000 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1001 kp->ki_acflag = p->p_acflag;
1002 kp->ki_lock = p->p_lock;
1004 kp->ki_ppid = proc_realparent(p)->p_pid;
1005 if (p->p_flag & P_TRACED)
1006 kp->ki_tracer = p->p_pptr->p_pid;
1011 * Fill in information that is thread specific. Must be called with
1012 * target process locked. If 'preferthread' is set, overwrite certain
1013 * process-related fields that are maintained for both threads and
1017 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1023 PROC_LOCK_ASSERT(p, MA_OWNED);
1028 if (td->td_wmesg != NULL)
1029 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1031 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1032 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
1033 sizeof(kp->ki_tdname)) {
1034 strlcpy(kp->ki_moretdname,
1035 td->td_name + sizeof(kp->ki_tdname) - 1,
1036 sizeof(kp->ki_moretdname));
1038 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
1040 if (TD_ON_LOCK(td)) {
1041 kp->ki_kiflag |= KI_LOCKBLOCK;
1042 strlcpy(kp->ki_lockname, td->td_lockname,
1043 sizeof(kp->ki_lockname));
1045 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1046 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1049 if (p->p_state == PRS_NORMAL) { /* approximate. */
1050 if (TD_ON_RUNQ(td) ||
1052 TD_IS_RUNNING(td)) {
1054 } else if (P_SHOULDSTOP(p)) {
1055 kp->ki_stat = SSTOP;
1056 } else if (TD_IS_SLEEPING(td)) {
1057 kp->ki_stat = SSLEEP;
1058 } else if (TD_ON_LOCK(td)) {
1059 kp->ki_stat = SLOCK;
1061 kp->ki_stat = SWAIT;
1063 } else if (p->p_state == PRS_ZOMBIE) {
1064 kp->ki_stat = SZOMB;
1069 /* Things in the thread */
1070 kp->ki_wchan = td->td_wchan;
1071 kp->ki_pri.pri_level = td->td_priority;
1072 kp->ki_pri.pri_native = td->td_base_pri;
1075 * Note: legacy fields; clamp at the old NOCPU value and/or
1076 * the maximum u_char CPU value.
1078 if (td->td_lastcpu == NOCPU)
1079 kp->ki_lastcpu_old = NOCPU_OLD;
1080 else if (td->td_lastcpu > MAXCPU_OLD)
1081 kp->ki_lastcpu_old = MAXCPU_OLD;
1083 kp->ki_lastcpu_old = td->td_lastcpu;
1085 if (td->td_oncpu == NOCPU)
1086 kp->ki_oncpu_old = NOCPU_OLD;
1087 else if (td->td_oncpu > MAXCPU_OLD)
1088 kp->ki_oncpu_old = MAXCPU_OLD;
1090 kp->ki_oncpu_old = td->td_oncpu;
1092 kp->ki_lastcpu = td->td_lastcpu;
1093 kp->ki_oncpu = td->td_oncpu;
1094 kp->ki_tdflags = td->td_flags;
1095 kp->ki_tid = td->td_tid;
1096 kp->ki_numthreads = p->p_numthreads;
1097 kp->ki_pcb = td->td_pcb;
1098 kp->ki_kstack = (void *)td->td_kstack;
1099 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1100 kp->ki_pri.pri_class = td->td_pri_class;
1101 kp->ki_pri.pri_user = td->td_user_pri;
1104 rufetchtd(td, &kp->ki_rusage);
1105 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1106 kp->ki_pctcpu = sched_pctcpu(td);
1107 kp->ki_estcpu = sched_estcpu(td);
1108 kp->ki_cow = td->td_cow;
1111 /* We can't get this anymore but ps etc never used it anyway. */
1115 kp->ki_siglist = td->td_siglist;
1116 kp->ki_sigmask = td->td_sigmask;
1123 * Fill in a kinfo_proc structure for the specified process.
1124 * Must be called with the target process locked.
1127 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1130 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1132 fill_kinfo_proc_only(p, kp);
1133 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1134 fill_kinfo_aggregate(p, kp);
1141 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1145 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1148 pstats_fork(struct pstats *src, struct pstats *dst)
1151 bzero(&dst->pstat_startzero,
1152 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1153 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1154 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1158 pstats_free(struct pstats *ps)
1161 free(ps, M_SUBPROC);
1164 static struct proc *
1165 zpfind_locked(pid_t pid)
1169 sx_assert(&allproc_lock, SX_LOCKED);
1170 LIST_FOREACH(p, &zombproc, p_list) {
1171 if (p->p_pid == pid) {
1180 * Locate a zombie process by number
1187 sx_slock(&allproc_lock);
1188 p = zpfind_locked(pid);
1189 sx_sunlock(&allproc_lock);
1193 #ifdef COMPAT_FREEBSD32
1196 * This function is typically used to copy out the kernel address, so
1197 * it can be replaced by assignment of zero.
1199 static inline uint32_t
1200 ptr32_trim(void *ptr)
1204 uptr = (uintptr_t)ptr;
1205 return ((uptr > UINT_MAX) ? 0 : uptr);
1208 #define PTRTRIM_CP(src,dst,fld) \
1209 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1212 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1216 bzero(ki32, sizeof(struct kinfo_proc32));
1217 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1218 CP(*ki, *ki32, ki_layout);
1219 PTRTRIM_CP(*ki, *ki32, ki_args);
1220 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1221 PTRTRIM_CP(*ki, *ki32, ki_addr);
1222 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1223 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1224 PTRTRIM_CP(*ki, *ki32, ki_fd);
1225 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1226 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1227 CP(*ki, *ki32, ki_pid);
1228 CP(*ki, *ki32, ki_ppid);
1229 CP(*ki, *ki32, ki_pgid);
1230 CP(*ki, *ki32, ki_tpgid);
1231 CP(*ki, *ki32, ki_sid);
1232 CP(*ki, *ki32, ki_tsid);
1233 CP(*ki, *ki32, ki_jobc);
1234 CP(*ki, *ki32, ki_tdev);
1235 CP(*ki, *ki32, ki_siglist);
1236 CP(*ki, *ki32, ki_sigmask);
1237 CP(*ki, *ki32, ki_sigignore);
1238 CP(*ki, *ki32, ki_sigcatch);
1239 CP(*ki, *ki32, ki_uid);
1240 CP(*ki, *ki32, ki_ruid);
1241 CP(*ki, *ki32, ki_svuid);
1242 CP(*ki, *ki32, ki_rgid);
1243 CP(*ki, *ki32, ki_svgid);
1244 CP(*ki, *ki32, ki_ngroups);
1245 for (i = 0; i < KI_NGROUPS; i++)
1246 CP(*ki, *ki32, ki_groups[i]);
1247 CP(*ki, *ki32, ki_size);
1248 CP(*ki, *ki32, ki_rssize);
1249 CP(*ki, *ki32, ki_swrss);
1250 CP(*ki, *ki32, ki_tsize);
1251 CP(*ki, *ki32, ki_dsize);
1252 CP(*ki, *ki32, ki_ssize);
1253 CP(*ki, *ki32, ki_xstat);
1254 CP(*ki, *ki32, ki_acflag);
1255 CP(*ki, *ki32, ki_pctcpu);
1256 CP(*ki, *ki32, ki_estcpu);
1257 CP(*ki, *ki32, ki_slptime);
1258 CP(*ki, *ki32, ki_swtime);
1259 CP(*ki, *ki32, ki_cow);
1260 CP(*ki, *ki32, ki_runtime);
1261 TV_CP(*ki, *ki32, ki_start);
1262 TV_CP(*ki, *ki32, ki_childtime);
1263 CP(*ki, *ki32, ki_flag);
1264 CP(*ki, *ki32, ki_kiflag);
1265 CP(*ki, *ki32, ki_traceflag);
1266 CP(*ki, *ki32, ki_stat);
1267 CP(*ki, *ki32, ki_nice);
1268 CP(*ki, *ki32, ki_lock);
1269 CP(*ki, *ki32, ki_rqindex);
1270 CP(*ki, *ki32, ki_oncpu);
1271 CP(*ki, *ki32, ki_lastcpu);
1273 /* XXX TODO: wrap cpu value as appropriate */
1274 CP(*ki, *ki32, ki_oncpu_old);
1275 CP(*ki, *ki32, ki_lastcpu_old);
1277 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1278 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1279 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1280 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1281 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1282 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1283 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1284 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1285 CP(*ki, *ki32, ki_tracer);
1286 CP(*ki, *ki32, ki_flag2);
1287 CP(*ki, *ki32, ki_fibnum);
1288 CP(*ki, *ki32, ki_cr_flags);
1289 CP(*ki, *ki32, ki_jid);
1290 CP(*ki, *ki32, ki_numthreads);
1291 CP(*ki, *ki32, ki_tid);
1292 CP(*ki, *ki32, ki_pri);
1293 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1294 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1295 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1296 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1297 PTRTRIM_CP(*ki, *ki32, ki_udata);
1298 CP(*ki, *ki32, ki_sflag);
1299 CP(*ki, *ki32, ki_tdflags);
1304 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1307 struct kinfo_proc ki;
1308 #ifdef COMPAT_FREEBSD32
1309 struct kinfo_proc32 ki32;
1313 PROC_LOCK_ASSERT(p, MA_OWNED);
1314 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1317 fill_kinfo_proc(p, &ki);
1318 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1319 #ifdef COMPAT_FREEBSD32
1320 if ((flags & KERN_PROC_MASK32) != 0) {
1321 freebsd32_kinfo_proc_out(&ki, &ki32);
1322 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1326 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1329 FOREACH_THREAD_IN_PROC(p, td) {
1330 fill_kinfo_thread(td, &ki, 1);
1331 #ifdef COMPAT_FREEBSD32
1332 if ((flags & KERN_PROC_MASK32) != 0) {
1333 freebsd32_kinfo_proc_out(&ki, &ki32);
1334 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1338 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1349 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1353 struct kinfo_proc ki;
1359 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1360 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1361 error = kern_proc_out(p, &sb, flags);
1362 error2 = sbuf_finish(&sb);
1366 else if (error2 != 0)
1386 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1388 int *name = (int *)arg1;
1389 u_int namelen = arg2;
1391 int flags, doingzomb, oid_number;
1394 oid_number = oidp->oid_number;
1395 if (oid_number != KERN_PROC_ALL &&
1396 (oid_number & KERN_PROC_INC_THREAD) == 0)
1397 flags = KERN_PROC_NOTHREADS;
1400 oid_number &= ~KERN_PROC_INC_THREAD;
1402 #ifdef COMPAT_FREEBSD32
1403 if (req->flags & SCTL_MASK32)
1404 flags |= KERN_PROC_MASK32;
1406 if (oid_number == KERN_PROC_PID) {
1409 error = sysctl_wire_old_buffer(req, 0);
1412 sx_slock(&proctree_lock);
1413 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1415 error = sysctl_out_proc(p, req, flags, 0);
1416 sx_sunlock(&proctree_lock);
1420 switch (oid_number) {
1425 case KERN_PROC_PROC:
1426 if (namelen != 0 && namelen != 1)
1436 /* overestimate by 5 procs */
1437 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1441 error = sysctl_wire_old_buffer(req, 0);
1444 sx_slock(&proctree_lock);
1445 sx_slock(&allproc_lock);
1446 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1448 p = LIST_FIRST(&allproc);
1450 p = LIST_FIRST(&zombproc);
1451 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
1453 * Skip embryonic processes.
1456 if (p->p_state == PRS_NEW) {
1460 KASSERT(p->p_ucred != NULL,
1461 ("process credential is NULL for non-NEW proc"));
1463 * Show a user only appropriate processes.
1465 if (p_cansee(curthread, p)) {
1470 * TODO - make more efficient (see notes below).
1473 switch (oid_number) {
1476 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1482 case KERN_PROC_PGRP:
1483 /* could do this by traversing pgrp */
1484 if (p->p_pgrp == NULL ||
1485 p->p_pgrp->pg_id != (pid_t)name[0]) {
1491 case KERN_PROC_RGID:
1492 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1498 case KERN_PROC_SESSION:
1499 if (p->p_session == NULL ||
1500 p->p_session->s_sid != (pid_t)name[0]) {
1507 if ((p->p_flag & P_CONTROLT) == 0 ||
1508 p->p_session == NULL) {
1512 /* XXX proctree_lock */
1513 SESS_LOCK(p->p_session);
1514 if (p->p_session->s_ttyp == NULL ||
1515 tty_udev(p->p_session->s_ttyp) !=
1517 SESS_UNLOCK(p->p_session);
1521 SESS_UNLOCK(p->p_session);
1525 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1531 case KERN_PROC_RUID:
1532 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1538 case KERN_PROC_PROC:
1546 error = sysctl_out_proc(p, req, flags, doingzomb);
1548 sx_sunlock(&allproc_lock);
1549 sx_sunlock(&proctree_lock);
1554 sx_sunlock(&allproc_lock);
1555 sx_sunlock(&proctree_lock);
1560 pargs_alloc(int len)
1564 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1566 refcount_init(&pa->ar_ref, 1);
1567 pa->ar_length = len;
1572 pargs_free(struct pargs *pa)
1579 pargs_hold(struct pargs *pa)
1584 refcount_acquire(&pa->ar_ref);
1588 pargs_drop(struct pargs *pa)
1593 if (refcount_release(&pa->ar_ref))
1598 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1604 * This may return a short read if the string is shorter than the chunk
1605 * and is aligned at the end of the page, and the following page is not
1608 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1614 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1616 enum proc_vector_type {
1622 #ifdef COMPAT_FREEBSD32
1624 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1625 size_t *vsizep, enum proc_vector_type type)
1627 struct freebsd32_ps_strings pss;
1629 vm_offset_t vptr, ptr;
1630 uint32_t *proc_vector32;
1636 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1637 sizeof(pss)) != sizeof(pss))
1641 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1642 vsize = pss.ps_nargvstr;
1643 if (vsize > ARG_MAX)
1645 size = vsize * sizeof(int32_t);
1648 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1649 vsize = pss.ps_nenvstr;
1650 if (vsize > ARG_MAX)
1652 size = vsize * sizeof(int32_t);
1655 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1656 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1659 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1660 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1663 if (aux.a_type == AT_NULL)
1667 if (aux.a_type != AT_NULL)
1670 size = vsize * sizeof(aux);
1673 KASSERT(0, ("Wrong proc vector type: %d", type));
1676 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1677 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1681 if (type == PROC_AUX) {
1682 *proc_vectorp = (char **)proc_vector32;
1686 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1687 for (i = 0; i < (int)vsize; i++)
1688 proc_vector[i] = PTRIN(proc_vector32[i]);
1689 *proc_vectorp = proc_vector;
1692 free(proc_vector32, M_TEMP);
1698 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1699 size_t *vsizep, enum proc_vector_type type)
1701 struct ps_strings pss;
1703 vm_offset_t vptr, ptr;
1708 #ifdef COMPAT_FREEBSD32
1709 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1710 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1712 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1713 sizeof(pss)) != sizeof(pss))
1717 vptr = (vm_offset_t)pss.ps_argvstr;
1718 vsize = pss.ps_nargvstr;
1719 if (vsize > ARG_MAX)
1721 size = vsize * sizeof(char *);
1724 vptr = (vm_offset_t)pss.ps_envstr;
1725 vsize = pss.ps_nenvstr;
1726 if (vsize > ARG_MAX)
1728 size = vsize * sizeof(char *);
1732 * The aux array is just above env array on the stack. Check
1733 * that the address is naturally aligned.
1735 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1737 #if __ELF_WORD_SIZE == 64
1738 if (vptr % sizeof(uint64_t) != 0)
1740 if (vptr % sizeof(uint32_t) != 0)
1744 * We count the array size reading the aux vectors from the
1745 * stack until AT_NULL vector is returned. So (to keep the code
1746 * simple) we read the process stack twice: the first time here
1747 * to find the size and the second time when copying the vectors
1748 * to the allocated proc_vector.
1750 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1751 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1754 if (aux.a_type == AT_NULL)
1759 * If the PROC_AUXV_MAX entries are iterated over, and we have
1760 * not reached AT_NULL, it is most likely we are reading wrong
1761 * data: either the process doesn't have auxv array or data has
1762 * been modified. Return the error in this case.
1764 if (aux.a_type != AT_NULL)
1767 size = vsize * sizeof(aux);
1770 KASSERT(0, ("Wrong proc vector type: %d", type));
1771 return (EINVAL); /* In case we are built without INVARIANTS. */
1773 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1774 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1775 free(proc_vector, M_TEMP);
1778 *proc_vectorp = proc_vector;
1784 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1787 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1788 enum proc_vector_type type)
1790 size_t done, len, nchr, vsize;
1792 char **proc_vector, *sptr;
1793 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1795 PROC_ASSERT_HELD(p);
1798 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1800 nchr = 2 * (PATH_MAX + ARG_MAX);
1802 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1805 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1807 * The program may have scribbled into its argv array, e.g. to
1808 * remove some arguments. If that has happened, break out
1809 * before trying to read from NULL.
1811 if (proc_vector[i] == NULL)
1813 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1814 error = proc_read_string(td, p, sptr, pss_string,
1815 sizeof(pss_string));
1818 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1819 if (done + len >= nchr)
1820 len = nchr - done - 1;
1821 sbuf_bcat(sb, pss_string, len);
1822 if (len != GET_PS_STRINGS_CHUNK_SZ)
1824 done += GET_PS_STRINGS_CHUNK_SZ;
1826 sbuf_bcat(sb, "", 1);
1830 free(proc_vector, M_TEMP);
1835 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1838 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1842 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1845 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1849 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1855 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1857 #ifdef COMPAT_FREEBSD32
1858 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1859 size = vsize * sizeof(Elf32_Auxinfo);
1862 size = vsize * sizeof(Elf_Auxinfo);
1863 if (sbuf_bcat(sb, auxv, size) != 0)
1871 * This sysctl allows a process to retrieve the argument list or process
1872 * title for another process without groping around in the address space
1873 * of the other process. It also allow a process to set its own "process
1874 * title to a string of its own choice.
1877 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1879 int *name = (int *)arg1;
1880 u_int namelen = arg2;
1881 struct pargs *newpa, *pa;
1884 int flags, error = 0, error2;
1889 flags = PGET_CANSEE;
1890 if (req->newptr != NULL)
1891 flags |= PGET_ISCURRENT;
1892 error = pget((pid_t)name[0], flags, &p);
1900 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1902 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1905 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1906 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1907 error = proc_getargv(curthread, p, &sb);
1908 error2 = sbuf_finish(&sb);
1911 if (error == 0 && error2 != 0)
1916 if (error != 0 || req->newptr == NULL)
1919 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1921 newpa = pargs_alloc(req->newlen);
1922 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1936 * This sysctl allows a process to retrieve environment of another process.
1939 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1941 int *name = (int *)arg1;
1942 u_int namelen = arg2;
1950 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1953 if ((p->p_flag & P_SYSTEM) != 0) {
1958 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1959 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1960 error = proc_getenvv(curthread, p, &sb);
1961 error2 = sbuf_finish(&sb);
1964 return (error != 0 ? error : error2);
1968 * This sysctl allows a process to retrieve ELF auxiliary vector of
1972 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1974 int *name = (int *)arg1;
1975 u_int namelen = arg2;
1983 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1986 if ((p->p_flag & P_SYSTEM) != 0) {
1990 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1991 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1992 error = proc_getauxv(curthread, p, &sb);
1993 error2 = sbuf_finish(&sb);
1996 return (error != 0 ? error : error2);
2000 * This sysctl allows a process to retrieve the path of the executable for
2001 * itself or another process.
2004 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2006 pid_t *pidp = (pid_t *)arg1;
2007 unsigned int arglen = arg2;
2010 char *retbuf, *freebuf;
2015 if (*pidp == -1) { /* -1 means this process */
2016 p = req->td->td_proc;
2018 error = pget(*pidp, PGET_CANSEE, &p);
2032 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2036 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2037 free(freebuf, M_TEMP);
2042 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2055 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2058 sv_name = p->p_sysent->sv_name;
2060 return (sysctl_handle_string(oidp, sv_name, 0, req));
2063 #ifdef KINFO_OVMENTRY_SIZE
2064 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2067 #ifdef COMPAT_FREEBSD7
2069 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2071 vm_map_entry_t entry, tmp_entry;
2072 unsigned int last_timestamp;
2073 char *fullpath, *freepath;
2074 struct kinfo_ovmentry *kve;
2084 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2087 vm = vmspace_acquire_ref(p);
2092 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2095 vm_map_lock_read(map);
2096 for (entry = map->header.next; entry != &map->header;
2097 entry = entry->next) {
2098 vm_object_t obj, tobj, lobj;
2101 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2104 bzero(kve, sizeof(*kve));
2105 kve->kve_structsize = sizeof(*kve);
2107 kve->kve_private_resident = 0;
2108 obj = entry->object.vm_object;
2110 VM_OBJECT_RLOCK(obj);
2111 if (obj->shadow_count == 1)
2112 kve->kve_private_resident =
2113 obj->resident_page_count;
2115 kve->kve_resident = 0;
2116 addr = entry->start;
2117 while (addr < entry->end) {
2118 if (pmap_extract(map->pmap, addr))
2119 kve->kve_resident++;
2123 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2125 VM_OBJECT_RLOCK(tobj);
2127 VM_OBJECT_RUNLOCK(lobj);
2131 kve->kve_start = (void*)entry->start;
2132 kve->kve_end = (void*)entry->end;
2133 kve->kve_offset = (off_t)entry->offset;
2135 if (entry->protection & VM_PROT_READ)
2136 kve->kve_protection |= KVME_PROT_READ;
2137 if (entry->protection & VM_PROT_WRITE)
2138 kve->kve_protection |= KVME_PROT_WRITE;
2139 if (entry->protection & VM_PROT_EXECUTE)
2140 kve->kve_protection |= KVME_PROT_EXEC;
2142 if (entry->eflags & MAP_ENTRY_COW)
2143 kve->kve_flags |= KVME_FLAG_COW;
2144 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2145 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2146 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2147 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2149 last_timestamp = map->timestamp;
2150 vm_map_unlock_read(map);
2152 kve->kve_fileid = 0;
2158 switch (lobj->type) {
2160 kve->kve_type = KVME_TYPE_DEFAULT;
2163 kve->kve_type = KVME_TYPE_VNODE;
2168 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2169 kve->kve_type = KVME_TYPE_VNODE;
2170 if ((lobj->flags & OBJ_TMPFS) != 0) {
2171 vp = lobj->un_pager.swp.swp_tmpfs;
2175 kve->kve_type = KVME_TYPE_SWAP;
2179 kve->kve_type = KVME_TYPE_DEVICE;
2182 kve->kve_type = KVME_TYPE_PHYS;
2185 kve->kve_type = KVME_TYPE_DEAD;
2188 kve->kve_type = KVME_TYPE_SG;
2191 kve->kve_type = KVME_TYPE_UNKNOWN;
2195 VM_OBJECT_RUNLOCK(lobj);
2197 kve->kve_ref_count = obj->ref_count;
2198 kve->kve_shadow_count = obj->shadow_count;
2199 VM_OBJECT_RUNLOCK(obj);
2201 vn_fullpath(curthread, vp, &fullpath,
2203 cred = curthread->td_ucred;
2204 vn_lock(vp, LK_SHARED | LK_RETRY);
2205 if (VOP_GETATTR(vp, &va, cred) == 0) {
2206 kve->kve_fileid = va.va_fileid;
2207 kve->kve_fsid = va.va_fsid;
2212 kve->kve_type = KVME_TYPE_NONE;
2213 kve->kve_ref_count = 0;
2214 kve->kve_shadow_count = 0;
2217 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2218 if (freepath != NULL)
2219 free(freepath, M_TEMP);
2221 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2222 vm_map_lock_read(map);
2225 if (last_timestamp != map->timestamp) {
2226 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2230 vm_map_unlock_read(map);
2236 #endif /* COMPAT_FREEBSD7 */
2238 #ifdef KINFO_VMENTRY_SIZE
2239 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2243 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2244 struct kinfo_vmentry *kve)
2246 vm_object_t obj, tobj;
2249 vm_paddr_t locked_pa;
2250 vm_pindex_t pi, pi_adv, pindex;
2253 obj = entry->object.vm_object;
2254 addr = entry->start;
2256 pi = OFF_TO_IDX(entry->offset);
2257 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2258 if (m_adv != NULL) {
2261 pi_adv = atop(entry->end - addr);
2263 for (tobj = obj;; tobj = tobj->backing_object) {
2264 m = vm_page_find_least(tobj, pindex);
2266 if (m->pindex == pindex)
2268 if (pi_adv > m->pindex - pindex) {
2269 pi_adv = m->pindex - pindex;
2273 if (tobj->backing_object == NULL)
2275 pindex += OFF_TO_IDX(tobj->
2276 backing_object_offset);
2280 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2281 (addr & (pagesizes[1] - 1)) == 0 &&
2282 (pmap_mincore(map->pmap, addr, &locked_pa) &
2283 MINCORE_SUPER) != 0) {
2284 kve->kve_flags |= KVME_FLAG_SUPER;
2285 pi_adv = atop(pagesizes[1]);
2288 * We do not test the found page on validity.
2289 * Either the page is busy and being paged in,
2290 * or it was invalidated. The first case
2291 * should be counted as resident, the second
2292 * is not so clear; we do account both.
2296 kve->kve_resident += pi_adv;
2299 PA_UNLOCK_COND(locked_pa);
2303 * Must be called with the process locked and will return unlocked.
2306 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2308 vm_map_entry_t entry, tmp_entry;
2311 vm_object_t obj, tobj, lobj;
2312 char *fullpath, *freepath;
2313 struct kinfo_vmentry *kve;
2318 unsigned int last_timestamp;
2321 PROC_LOCK_ASSERT(p, MA_OWNED);
2325 vm = vmspace_acquire_ref(p);
2330 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2334 vm_map_lock_read(map);
2335 for (entry = map->header.next; entry != &map->header;
2336 entry = entry->next) {
2337 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2341 bzero(kve, sizeof(*kve));
2342 obj = entry->object.vm_object;
2344 for (tobj = obj; tobj != NULL;
2345 tobj = tobj->backing_object) {
2346 VM_OBJECT_RLOCK(tobj);
2349 if (obj->backing_object == NULL)
2350 kve->kve_private_resident =
2351 obj->resident_page_count;
2352 if (!vmmap_skip_res_cnt)
2353 kern_proc_vmmap_resident(map, entry, kve);
2354 for (tobj = obj; tobj != NULL;
2355 tobj = tobj->backing_object) {
2356 if (tobj != obj && tobj != lobj)
2357 VM_OBJECT_RUNLOCK(tobj);
2363 kve->kve_start = entry->start;
2364 kve->kve_end = entry->end;
2365 kve->kve_offset = entry->offset;
2367 if (entry->protection & VM_PROT_READ)
2368 kve->kve_protection |= KVME_PROT_READ;
2369 if (entry->protection & VM_PROT_WRITE)
2370 kve->kve_protection |= KVME_PROT_WRITE;
2371 if (entry->protection & VM_PROT_EXECUTE)
2372 kve->kve_protection |= KVME_PROT_EXEC;
2374 if (entry->eflags & MAP_ENTRY_COW)
2375 kve->kve_flags |= KVME_FLAG_COW;
2376 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2377 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2378 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2379 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2380 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2381 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2382 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2383 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2385 last_timestamp = map->timestamp;
2386 vm_map_unlock_read(map);
2392 switch (lobj->type) {
2394 kve->kve_type = KVME_TYPE_DEFAULT;
2397 kve->kve_type = KVME_TYPE_VNODE;
2402 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2403 kve->kve_type = KVME_TYPE_VNODE;
2404 if ((lobj->flags & OBJ_TMPFS) != 0) {
2405 vp = lobj->un_pager.swp.swp_tmpfs;
2409 kve->kve_type = KVME_TYPE_SWAP;
2413 kve->kve_type = KVME_TYPE_DEVICE;
2416 kve->kve_type = KVME_TYPE_PHYS;
2419 kve->kve_type = KVME_TYPE_DEAD;
2422 kve->kve_type = KVME_TYPE_SG;
2424 case OBJT_MGTDEVICE:
2425 kve->kve_type = KVME_TYPE_MGTDEVICE;
2428 kve->kve_type = KVME_TYPE_UNKNOWN;
2432 VM_OBJECT_RUNLOCK(lobj);
2434 kve->kve_ref_count = obj->ref_count;
2435 kve->kve_shadow_count = obj->shadow_count;
2436 VM_OBJECT_RUNLOCK(obj);
2438 vn_fullpath(curthread, vp, &fullpath,
2440 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2441 cred = curthread->td_ucred;
2442 vn_lock(vp, LK_SHARED | LK_RETRY);
2443 if (VOP_GETATTR(vp, &va, cred) == 0) {
2444 kve->kve_vn_fileid = va.va_fileid;
2445 kve->kve_vn_fsid = va.va_fsid;
2447 MAKEIMODE(va.va_type, va.va_mode);
2448 kve->kve_vn_size = va.va_size;
2449 kve->kve_vn_rdev = va.va_rdev;
2450 kve->kve_status = KF_ATTR_VALID;
2455 kve->kve_type = KVME_TYPE_NONE;
2456 kve->kve_ref_count = 0;
2457 kve->kve_shadow_count = 0;
2460 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2461 if (freepath != NULL)
2462 free(freepath, M_TEMP);
2464 /* Pack record size down */
2465 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2466 kve->kve_structsize =
2467 offsetof(struct kinfo_vmentry, kve_path) +
2468 strlen(kve->kve_path) + 1;
2470 kve->kve_structsize = sizeof(*kve);
2471 kve->kve_structsize = roundup(kve->kve_structsize,
2474 /* Halt filling and truncate rather than exceeding maxlen */
2475 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2477 vm_map_lock_read(map);
2479 } else if (maxlen != -1)
2480 maxlen -= kve->kve_structsize;
2482 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2484 vm_map_lock_read(map);
2487 if (last_timestamp != map->timestamp) {
2488 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2492 vm_map_unlock_read(map);
2500 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2504 int error, error2, *name;
2507 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2508 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2509 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2514 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2515 error2 = sbuf_finish(&sb);
2517 return (error != 0 ? error : error2);
2520 #if defined(STACK) || defined(DDB)
2522 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2524 struct kinfo_kstack *kkstp;
2525 int error, i, *name, numthreads;
2526 lwpid_t *lwpidarray;
2533 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2537 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2538 st = stack_create();
2543 if (lwpidarray != NULL) {
2544 free(lwpidarray, M_TEMP);
2547 numthreads = p->p_numthreads;
2549 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2552 } while (numthreads < p->p_numthreads);
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.
2564 FOREACH_THREAD_IN_PROC(p, td) {
2565 KASSERT(i < numthreads,
2566 ("sysctl_kern_proc_kstack: numthreads"));
2567 lwpidarray[i] = td->td_tid;
2571 for (i = 0; i < numthreads; i++) {
2572 td = thread_find(p, lwpidarray[i]);
2576 bzero(kkstp, sizeof(*kkstp));
2577 (void)sbuf_new(&sb, kkstp->kkst_trace,
2578 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2580 kkstp->kkst_tid = td->td_tid;
2581 if (TD_IS_SWAPPED(td)) {
2582 kkstp->kkst_state = KKST_STATE_SWAPPED;
2583 } else if (TD_IS_RUNNING(td)) {
2584 if (stack_save_td_running(st, td) == 0)
2585 kkstp->kkst_state = KKST_STATE_STACKOK;
2587 kkstp->kkst_state = KKST_STATE_RUNNING;
2589 kkstp->kkst_state = KKST_STATE_STACKOK;
2590 stack_save_td(st, td);
2594 stack_sbuf_print(&sb, st);
2597 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2604 if (lwpidarray != NULL)
2605 free(lwpidarray, M_TEMP);
2607 free(kkstp, M_TEMP);
2613 * This sysctl allows a process to retrieve the full list of groups from
2614 * itself or another process.
2617 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2619 pid_t *pidp = (pid_t *)arg1;
2620 unsigned int arglen = arg2;
2627 if (*pidp == -1) { /* -1 means this process */
2628 p = req->td->td_proc;
2631 error = pget(*pidp, PGET_CANSEE, &p);
2636 cred = crhold(p->p_ucred);
2639 error = SYSCTL_OUT(req, cred->cr_groups,
2640 cred->cr_ngroups * sizeof(gid_t));
2646 * This sysctl allows a process to retrieve or/and set the resource limit for
2650 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2652 int *name = (int *)arg1;
2653 u_int namelen = arg2;
2662 which = (u_int)name[1];
2663 if (which >= RLIM_NLIMITS)
2666 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2669 flags = PGET_HOLD | PGET_NOTWEXIT;
2670 if (req->newptr != NULL)
2671 flags |= PGET_CANDEBUG;
2673 flags |= PGET_CANSEE;
2674 error = pget((pid_t)name[0], flags, &p);
2681 if (req->oldptr != NULL) {
2683 lim_rlimit_proc(p, which, &rlim);
2686 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2693 if (req->newptr != NULL) {
2694 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2696 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2705 * This sysctl allows a process to retrieve ps_strings structure location of
2709 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2711 int *name = (int *)arg1;
2712 u_int namelen = arg2;
2714 vm_offset_t ps_strings;
2716 #ifdef COMPAT_FREEBSD32
2717 uint32_t ps_strings32;
2723 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2726 #ifdef COMPAT_FREEBSD32
2727 if ((req->flags & SCTL_MASK32) != 0) {
2729 * We return 0 if the 32 bit emulation request is for a 64 bit
2732 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2733 PTROUT(p->p_sysent->sv_psstrings) : 0;
2735 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2739 ps_strings = p->p_sysent->sv_psstrings;
2741 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2746 * This sysctl allows a process to retrieve umask of another process.
2749 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2751 int *name = (int *)arg1;
2752 u_int namelen = arg2;
2760 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2764 FILEDESC_SLOCK(p->p_fd);
2765 fd_cmask = p->p_fd->fd_cmask;
2766 FILEDESC_SUNLOCK(p->p_fd);
2768 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2773 * This sysctl allows a process to set and retrieve binary osreldate of
2777 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2779 int *name = (int *)arg1;
2780 u_int namelen = arg2;
2782 int flags, error, osrel;
2787 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2790 flags = PGET_HOLD | PGET_NOTWEXIT;
2791 if (req->newptr != NULL)
2792 flags |= PGET_CANDEBUG;
2794 flags |= PGET_CANSEE;
2795 error = pget((pid_t)name[0], flags, &p);
2799 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2803 if (req->newptr != NULL) {
2804 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2819 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2821 int *name = (int *)arg1;
2822 u_int namelen = arg2;
2824 struct kinfo_sigtramp kst;
2825 const struct sysentvec *sv;
2827 #ifdef COMPAT_FREEBSD32
2828 struct kinfo_sigtramp32 kst32;
2834 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2838 #ifdef COMPAT_FREEBSD32
2839 if ((req->flags & SCTL_MASK32) != 0) {
2840 bzero(&kst32, sizeof(kst32));
2841 if (SV_PROC_FLAG(p, SV_ILP32)) {
2842 if (sv->sv_sigcode_base != 0) {
2843 kst32.ksigtramp_start = sv->sv_sigcode_base;
2844 kst32.ksigtramp_end = sv->sv_sigcode_base +
2847 kst32.ksigtramp_start = sv->sv_psstrings -
2849 kst32.ksigtramp_end = sv->sv_psstrings;
2853 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2857 bzero(&kst, sizeof(kst));
2858 if (sv->sv_sigcode_base != 0) {
2859 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2860 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2863 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2865 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2868 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2872 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2874 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2875 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2876 "Return entire process table");
2878 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2879 sysctl_kern_proc, "Process table");
2881 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2882 sysctl_kern_proc, "Process table");
2884 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2885 sysctl_kern_proc, "Process table");
2887 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2888 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2890 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2891 sysctl_kern_proc, "Process table");
2893 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2894 sysctl_kern_proc, "Process table");
2896 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2897 sysctl_kern_proc, "Process table");
2899 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2900 sysctl_kern_proc, "Process table");
2902 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2903 sysctl_kern_proc, "Return process table, no threads");
2905 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2906 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2907 sysctl_kern_proc_args, "Process argument list");
2909 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2910 sysctl_kern_proc_env, "Process environment");
2912 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2913 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2915 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2916 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2918 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2919 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2920 "Process syscall vector name (ABI type)");
2922 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2923 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2925 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2926 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2928 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2929 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2931 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2932 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2934 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2935 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2937 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2938 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2940 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2941 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2943 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2944 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2946 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2947 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2948 "Return process table, no threads");
2950 #ifdef COMPAT_FREEBSD7
2951 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2952 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2955 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2956 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2958 #if defined(STACK) || defined(DDB)
2959 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2960 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2963 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2964 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2966 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2967 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2968 "Process resource limits");
2970 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2971 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2972 "Process ps_strings location");
2974 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2975 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2977 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2978 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2979 "Process binary osreldate");
2981 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2982 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2983 "Process signal trampoline location");
2988 * stop_all_proc() purpose is to stop all process which have usermode,
2989 * except current process for obvious reasons. This makes it somewhat
2990 * unreliable when invoked from multithreaded process. The service
2991 * must not be user-callable anyway.
2996 struct proc *cp, *p;
2998 bool restart, seen_stopped, seen_exiting, stopped_some;
3002 sx_xlock(&allproc_lock);
3004 seen_exiting = seen_stopped = stopped_some = restart = false;
3005 LIST_REMOVE(cp, p_list);
3006 LIST_INSERT_HEAD(&allproc, cp, p_list);
3008 p = LIST_NEXT(cp, p_list);
3011 LIST_REMOVE(cp, p_list);
3012 LIST_INSERT_AFTER(p, cp, p_list);
3014 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3018 if ((p->p_flag & P_WEXIT) != 0) {
3019 seen_exiting = true;
3023 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3025 * Stopped processes are tolerated when there
3026 * are no other processes which might continue
3027 * them. P_STOPPED_SINGLE but not
3028 * P_TOTAL_STOP process still has at least one
3031 seen_stopped = true;
3036 sx_xunlock(&allproc_lock);
3037 r = thread_single(p, SINGLE_ALLPROC);
3041 stopped_some = true;
3044 sx_xlock(&allproc_lock);
3046 /* Catch forked children we did not see in iteration. */
3047 if (gen != allproc_gen)
3049 sx_xunlock(&allproc_lock);
3050 if (restart || stopped_some || seen_exiting || seen_stopped) {
3051 kern_yield(PRI_USER);
3057 resume_all_proc(void)
3059 struct proc *cp, *p;
3062 sx_xlock(&allproc_lock);
3063 LIST_REMOVE(cp, p_list);
3064 LIST_INSERT_HEAD(&allproc, cp, p_list);
3066 p = LIST_NEXT(cp, p_list);
3069 LIST_REMOVE(cp, p_list);
3070 LIST_INSERT_AFTER(p, cp, p_list);
3072 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3073 sx_xunlock(&allproc_lock);
3075 thread_single_end(p, SINGLE_ALLPROC);
3078 sx_xlock(&allproc_lock);
3083 sx_xunlock(&allproc_lock);
3086 /* #define TOTAL_STOP_DEBUG 1 */
3087 #ifdef TOTAL_STOP_DEBUG
3088 volatile static int ap_resume;
3089 #include <sys/mount.h>
3092 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3098 error = sysctl_handle_int(oidp, &val, 0, req);
3099 if (error != 0 || req->newptr == NULL)
3104 while (ap_resume == 0)
3112 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3113 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3114 sysctl_debug_stop_all_proc, "I",