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 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
241 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
242 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
243 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
244 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
245 cv_init(&p->p_pwait, "ppwait");
246 cv_init(&p->p_dbgwait, "dbgwait");
247 TAILQ_INIT(&p->p_threads); /* all threads in proc */
248 EVENTHANDLER_INVOKE(process_init, p);
249 p->p_stats = pstats_alloc();
251 SDT_PROBE3(proc, , init, return, p, size, flags);
256 * UMA should ensure that this function is never called.
257 * Freeing a proc structure would violate type stability.
260 proc_fini(void *mem, int size)
265 p = (struct proc *)mem;
266 EVENTHANDLER_INVOKE(process_fini, p);
267 pstats_free(p->p_stats);
268 thread_free(FIRST_THREAD_IN_PROC(p));
269 mtx_destroy(&p->p_mtx);
270 if (p->p_ksi != NULL)
271 ksiginfo_free(p->p_ksi);
273 panic("proc reclaimed");
278 * Is p an inferior of the current process?
281 inferior(struct proc *p)
284 sx_assert(&proctree_lock, SX_LOCKED);
285 PROC_LOCK_ASSERT(p, MA_OWNED);
286 for (; p != curproc; p = proc_realparent(p)) {
294 pfind_locked(pid_t pid)
298 sx_assert(&allproc_lock, SX_LOCKED);
299 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
300 if (p->p_pid == pid) {
302 if (p->p_state == PRS_NEW) {
313 * Locate a process by number; return only "live" processes -- i.e., neither
314 * zombies nor newly born but incompletely initialized processes. By not
315 * returning processes in the PRS_NEW state, we allow callers to avoid
316 * testing for that condition to avoid dereferencing p_ucred, et al.
323 sx_slock(&allproc_lock);
324 p = pfind_locked(pid);
325 sx_sunlock(&allproc_lock);
330 pfind_tid_locked(pid_t tid)
335 sx_assert(&allproc_lock, SX_LOCKED);
336 FOREACH_PROC_IN_SYSTEM(p) {
338 if (p->p_state == PRS_NEW) {
342 FOREACH_THREAD_IN_PROC(p, td) {
343 if (td->td_tid == tid)
353 * Locate a process group by number.
354 * The caller must hold proctree_lock.
360 register struct pgrp *pgrp;
362 sx_assert(&proctree_lock, SX_LOCKED);
364 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
365 if (pgrp->pg_id == pgid) {
374 * Locate process and do additional manipulations, depending on flags.
377 pget(pid_t pid, int flags, struct proc **pp)
382 sx_slock(&allproc_lock);
383 if (pid <= PID_MAX) {
384 p = pfind_locked(pid);
385 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
386 p = zpfind_locked(pid);
387 } else if ((flags & PGET_NOTID) == 0) {
388 p = pfind_tid_locked(pid);
392 sx_sunlock(&allproc_lock);
395 if ((flags & PGET_CANSEE) != 0) {
396 error = p_cansee(curthread, p);
400 if ((flags & PGET_CANDEBUG) != 0) {
401 error = p_candebug(curthread, p);
405 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
409 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
413 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
415 * XXXRW: Not clear ESRCH is the right error during proc
421 if ((flags & PGET_HOLD) != 0) {
433 * Create a new process group.
434 * pgid must be equal to the pid of p.
435 * Begin a new session if required.
438 enterpgrp(p, pgid, pgrp, sess)
439 register struct proc *p;
442 struct session *sess;
445 sx_assert(&proctree_lock, SX_XLOCKED);
447 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
448 KASSERT(p->p_pid == pgid,
449 ("enterpgrp: new pgrp and pid != pgid"));
450 KASSERT(pgfind(pgid) == NULL,
451 ("enterpgrp: pgrp with pgid exists"));
452 KASSERT(!SESS_LEADER(p),
453 ("enterpgrp: session leader attempted setpgrp"));
455 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
461 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
463 p->p_flag &= ~P_CONTROLT;
467 sess->s_sid = p->p_pid;
468 refcount_init(&sess->s_count, 1);
469 sess->s_ttyvp = NULL;
470 sess->s_ttydp = NULL;
472 bcopy(p->p_session->s_login, sess->s_login,
473 sizeof(sess->s_login));
474 pgrp->pg_session = sess;
475 KASSERT(p == curproc,
476 ("enterpgrp: mksession and p != curproc"));
478 pgrp->pg_session = p->p_session;
479 sess_hold(pgrp->pg_session);
483 LIST_INIT(&pgrp->pg_members);
486 * As we have an exclusive lock of proctree_lock,
487 * this should not deadlock.
489 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
491 SLIST_INIT(&pgrp->pg_sigiolst);
494 doenterpgrp(p, pgrp);
500 * Move p to an existing process group
503 enterthispgrp(p, pgrp)
504 register struct proc *p;
508 sx_assert(&proctree_lock, SX_XLOCKED);
509 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
510 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
511 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
512 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
513 KASSERT(pgrp->pg_session == p->p_session,
514 ("%s: pgrp's session %p, p->p_session %p.\n",
518 KASSERT(pgrp != p->p_pgrp,
519 ("%s: p belongs to pgrp.", __func__));
521 doenterpgrp(p, pgrp);
527 * Move p to a process group
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
570 register struct proc *p;
572 struct pgrp *savepgrp;
574 sx_assert(&proctree_lock, SX_XLOCKED);
575 savepgrp = p->p_pgrp;
578 LIST_REMOVE(p, p_pglist);
581 PGRP_UNLOCK(savepgrp);
582 if (LIST_EMPTY(&savepgrp->pg_members))
588 * delete a process group
592 register struct pgrp *pgrp;
594 struct session *savesess;
597 sx_assert(&proctree_lock, SX_XLOCKED);
598 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
599 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
602 * Reset any sigio structures pointing to us as a result of
603 * F_SETOWN with our pgid.
605 funsetownlst(&pgrp->pg_sigiolst);
608 tp = pgrp->pg_session->s_ttyp;
609 LIST_REMOVE(pgrp, pg_hash);
610 savesess = pgrp->pg_session;
613 /* Remove the reference to the pgrp before deallocating it. */
616 tty_rel_pgrp(tp, pgrp);
619 mtx_destroy(&pgrp->pg_mtx);
621 sess_release(savesess);
625 pgadjustjobc(pgrp, entering)
635 if (pgrp->pg_jobc == 0)
642 * Adjust pgrp jobc counters when specified process changes process group.
643 * We count the number of processes in each process group that "qualify"
644 * the group for terminal job control (those with a parent in a different
645 * process group of the same session). If that count reaches zero, the
646 * process group becomes orphaned. Check both the specified process'
647 * process group and that of its children.
648 * entering == 0 => p is leaving specified group.
649 * entering == 1 => p is entering specified group.
652 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
654 struct pgrp *hispgrp;
655 struct session *mysession;
658 sx_assert(&proctree_lock, SX_LOCKED);
659 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
660 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
661 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
664 * Check p's parent to see whether p qualifies its own process
665 * group; if so, adjust count for p's process group.
667 mysession = pgrp->pg_session;
668 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
669 hispgrp->pg_session == mysession)
670 pgadjustjobc(pgrp, entering);
673 * Check this process' children to see whether they qualify
674 * their process groups; if so, adjust counts for children's
677 LIST_FOREACH(q, &p->p_children, p_sibling) {
679 if (hispgrp == pgrp ||
680 hispgrp->pg_session != mysession)
682 if (q->p_state == PRS_ZOMBIE)
684 pgadjustjobc(hispgrp, entering);
697 MPASS(p->p_flag & P_WEXIT);
699 * Do a quick check to see if there is anything to do with the
700 * proctree_lock held. pgrp and LIST_EMPTY checks are for fixjobc().
703 if (!SESS_LEADER(p) &&
704 (p->p_pgrp == p->p_pptr->p_pgrp) &&
705 LIST_EMPTY(&p->p_children)) {
711 sx_xlock(&proctree_lock);
712 if (SESS_LEADER(p)) {
716 * s_ttyp is not zero'd; we use this to indicate that
717 * the session once had a controlling terminal. (for
718 * logging and informational purposes)
729 * Signal foreground pgrp and revoke access to
730 * controlling terminal if it has not been revoked
733 * Because the TTY may have been revoked in the mean
734 * time and could already have a new session associated
735 * with it, make sure we don't send a SIGHUP to a
736 * foreground process group that does not belong to this
742 if (tp->t_session == sp)
743 tty_signal_pgrp(tp, SIGHUP);
748 sx_xunlock(&proctree_lock);
749 if (vn_lock(ttyvp, LK_EXCLUSIVE) == 0) {
750 VOP_REVOKE(ttyvp, REVOKEALL);
751 VOP_UNLOCK(ttyvp, 0);
754 sx_xlock(&proctree_lock);
757 fixjobc(p, p->p_pgrp, 0);
758 sx_xunlock(&proctree_lock);
762 * A process group has become orphaned;
763 * if there are any stopped processes in the group,
764 * hang-up all process in that group.
770 register struct proc *p;
772 PGRP_LOCK_ASSERT(pg, MA_OWNED);
774 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
776 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
778 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
780 kern_psignal(p, SIGHUP);
781 kern_psignal(p, SIGCONT);
791 sess_hold(struct session *s)
794 refcount_acquire(&s->s_count);
798 sess_release(struct session *s)
801 if (refcount_release(&s->s_count)) {
802 if (s->s_ttyp != NULL) {
804 tty_rel_sess(s->s_ttyp, s);
806 mtx_destroy(&s->s_mtx);
813 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
815 register struct pgrp *pgrp;
816 register struct proc *p;
819 for (i = 0; i <= pgrphash; i++) {
820 if (!LIST_EMPTY(&pgrphashtbl[i])) {
821 printf("\tindx %d\n", i);
822 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
824 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
825 (void *)pgrp, (long)pgrp->pg_id,
826 (void *)pgrp->pg_session,
827 pgrp->pg_session->s_count,
828 (void *)LIST_FIRST(&pgrp->pg_members));
829 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
830 printf("\t\tpid %ld addr %p pgrp %p\n",
831 (long)p->p_pid, (void *)p,
841 * Calculate the kinfo_proc members which contain process-wide
843 * Must be called with the target process locked.
846 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
850 PROC_LOCK_ASSERT(p, MA_OWNED);
854 FOREACH_THREAD_IN_PROC(p, td) {
856 kp->ki_pctcpu += sched_pctcpu(td);
857 kp->ki_estcpu += sched_estcpu(td);
863 * Clear kinfo_proc and fill in any information that is common
864 * to all threads in the process.
865 * Must be called with the target process locked.
868 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
875 struct timeval boottime;
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 getboottime(&boottime);
958 timevaladd(&kp->ki_start, &boottime);
960 rufetch(p, &kp->ki_rusage);
961 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
962 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
964 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
965 /* Some callers want child times in a single value. */
966 kp->ki_childtime = kp->ki_childstime;
967 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
969 FOREACH_THREAD_IN_PROC(p, td0)
970 kp->ki_cow += td0->td_cow;
974 kp->ki_pgid = p->p_pgrp->pg_id;
975 kp->ki_jobc = p->p_pgrp->pg_jobc;
976 sp = p->p_pgrp->pg_session;
979 kp->ki_sid = sp->s_sid;
981 strlcpy(kp->ki_login, sp->s_login,
982 sizeof(kp->ki_login));
984 kp->ki_kiflag |= KI_CTTY;
986 kp->ki_kiflag |= KI_SLEADER;
987 /* XXX proctree_lock */
992 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
993 kp->ki_tdev = tty_udev(tp);
994 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
996 kp->ki_tsid = tp->t_session->s_sid;
999 if (p->p_comm[0] != '\0')
1000 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
1001 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
1002 p->p_sysent->sv_name[0] != '\0')
1003 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
1004 kp->ki_siglist = p->p_siglist;
1005 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
1006 kp->ki_acflag = p->p_acflag;
1007 kp->ki_lock = p->p_lock;
1009 kp->ki_ppid = proc_realparent(p)->p_pid;
1010 if (p->p_flag & P_TRACED)
1011 kp->ki_tracer = p->p_pptr->p_pid;
1016 * Fill in information that is thread specific. Must be called with
1017 * target process locked. If 'preferthread' is set, overwrite certain
1018 * process-related fields that are maintained for both threads and
1022 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
1028 PROC_LOCK_ASSERT(p, MA_OWNED);
1033 if (td->td_wmesg != NULL)
1034 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
1036 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
1037 strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
1038 if (TD_ON_LOCK(td)) {
1039 kp->ki_kiflag |= KI_LOCKBLOCK;
1040 strlcpy(kp->ki_lockname, td->td_lockname,
1041 sizeof(kp->ki_lockname));
1043 kp->ki_kiflag &= ~KI_LOCKBLOCK;
1044 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
1047 if (p->p_state == PRS_NORMAL) { /* approximate. */
1048 if (TD_ON_RUNQ(td) ||
1050 TD_IS_RUNNING(td)) {
1052 } else if (P_SHOULDSTOP(p)) {
1053 kp->ki_stat = SSTOP;
1054 } else if (TD_IS_SLEEPING(td)) {
1055 kp->ki_stat = SSLEEP;
1056 } else if (TD_ON_LOCK(td)) {
1057 kp->ki_stat = SLOCK;
1059 kp->ki_stat = SWAIT;
1061 } else if (p->p_state == PRS_ZOMBIE) {
1062 kp->ki_stat = SZOMB;
1067 /* Things in the thread */
1068 kp->ki_wchan = td->td_wchan;
1069 kp->ki_pri.pri_level = td->td_priority;
1070 kp->ki_pri.pri_native = td->td_base_pri;
1073 * Note: legacy fields; clamp at the old NOCPU value and/or
1074 * the maximum u_char CPU value.
1076 if (td->td_lastcpu == NOCPU)
1077 kp->ki_lastcpu_old = NOCPU_OLD;
1078 else if (td->td_lastcpu > MAXCPU_OLD)
1079 kp->ki_lastcpu_old = MAXCPU_OLD;
1081 kp->ki_lastcpu_old = td->td_lastcpu;
1083 if (td->td_oncpu == NOCPU)
1084 kp->ki_oncpu_old = NOCPU_OLD;
1085 else if (td->td_oncpu > MAXCPU_OLD)
1086 kp->ki_oncpu_old = MAXCPU_OLD;
1088 kp->ki_oncpu_old = td->td_oncpu;
1090 kp->ki_lastcpu = td->td_lastcpu;
1091 kp->ki_oncpu = td->td_oncpu;
1092 kp->ki_tdflags = td->td_flags;
1093 kp->ki_tid = td->td_tid;
1094 kp->ki_numthreads = p->p_numthreads;
1095 kp->ki_pcb = td->td_pcb;
1096 kp->ki_kstack = (void *)td->td_kstack;
1097 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1098 kp->ki_pri.pri_class = td->td_pri_class;
1099 kp->ki_pri.pri_user = td->td_user_pri;
1102 rufetchtd(td, &kp->ki_rusage);
1103 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1104 kp->ki_pctcpu = sched_pctcpu(td);
1105 kp->ki_estcpu = sched_estcpu(td);
1106 kp->ki_cow = td->td_cow;
1109 /* We can't get this anymore but ps etc never used it anyway. */
1113 kp->ki_siglist = td->td_siglist;
1114 kp->ki_sigmask = td->td_sigmask;
1121 * Fill in a kinfo_proc structure for the specified process.
1122 * Must be called with the target process locked.
1125 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1128 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1130 fill_kinfo_proc_only(p, kp);
1131 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1132 fill_kinfo_aggregate(p, kp);
1139 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1143 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1146 pstats_fork(struct pstats *src, struct pstats *dst)
1149 bzero(&dst->pstat_startzero,
1150 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1151 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1152 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1156 pstats_free(struct pstats *ps)
1159 free(ps, M_SUBPROC);
1162 static struct proc *
1163 zpfind_locked(pid_t pid)
1167 sx_assert(&allproc_lock, SX_LOCKED);
1168 LIST_FOREACH(p, &zombproc, p_list) {
1169 if (p->p_pid == pid) {
1178 * Locate a zombie process by number
1185 sx_slock(&allproc_lock);
1186 p = zpfind_locked(pid);
1187 sx_sunlock(&allproc_lock);
1191 #ifdef COMPAT_FREEBSD32
1194 * This function is typically used to copy out the kernel address, so
1195 * it can be replaced by assignment of zero.
1197 static inline uint32_t
1198 ptr32_trim(void *ptr)
1202 uptr = (uintptr_t)ptr;
1203 return ((uptr > UINT_MAX) ? 0 : uptr);
1206 #define PTRTRIM_CP(src,dst,fld) \
1207 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1210 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1214 bzero(ki32, sizeof(struct kinfo_proc32));
1215 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1216 CP(*ki, *ki32, ki_layout);
1217 PTRTRIM_CP(*ki, *ki32, ki_args);
1218 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1219 PTRTRIM_CP(*ki, *ki32, ki_addr);
1220 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1221 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1222 PTRTRIM_CP(*ki, *ki32, ki_fd);
1223 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1224 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1225 CP(*ki, *ki32, ki_pid);
1226 CP(*ki, *ki32, ki_ppid);
1227 CP(*ki, *ki32, ki_pgid);
1228 CP(*ki, *ki32, ki_tpgid);
1229 CP(*ki, *ki32, ki_sid);
1230 CP(*ki, *ki32, ki_tsid);
1231 CP(*ki, *ki32, ki_jobc);
1232 CP(*ki, *ki32, ki_tdev);
1233 CP(*ki, *ki32, ki_siglist);
1234 CP(*ki, *ki32, ki_sigmask);
1235 CP(*ki, *ki32, ki_sigignore);
1236 CP(*ki, *ki32, ki_sigcatch);
1237 CP(*ki, *ki32, ki_uid);
1238 CP(*ki, *ki32, ki_ruid);
1239 CP(*ki, *ki32, ki_svuid);
1240 CP(*ki, *ki32, ki_rgid);
1241 CP(*ki, *ki32, ki_svgid);
1242 CP(*ki, *ki32, ki_ngroups);
1243 for (i = 0; i < KI_NGROUPS; i++)
1244 CP(*ki, *ki32, ki_groups[i]);
1245 CP(*ki, *ki32, ki_size);
1246 CP(*ki, *ki32, ki_rssize);
1247 CP(*ki, *ki32, ki_swrss);
1248 CP(*ki, *ki32, ki_tsize);
1249 CP(*ki, *ki32, ki_dsize);
1250 CP(*ki, *ki32, ki_ssize);
1251 CP(*ki, *ki32, ki_xstat);
1252 CP(*ki, *ki32, ki_acflag);
1253 CP(*ki, *ki32, ki_pctcpu);
1254 CP(*ki, *ki32, ki_estcpu);
1255 CP(*ki, *ki32, ki_slptime);
1256 CP(*ki, *ki32, ki_swtime);
1257 CP(*ki, *ki32, ki_cow);
1258 CP(*ki, *ki32, ki_runtime);
1259 TV_CP(*ki, *ki32, ki_start);
1260 TV_CP(*ki, *ki32, ki_childtime);
1261 CP(*ki, *ki32, ki_flag);
1262 CP(*ki, *ki32, ki_kiflag);
1263 CP(*ki, *ki32, ki_traceflag);
1264 CP(*ki, *ki32, ki_stat);
1265 CP(*ki, *ki32, ki_nice);
1266 CP(*ki, *ki32, ki_lock);
1267 CP(*ki, *ki32, ki_rqindex);
1268 CP(*ki, *ki32, ki_oncpu);
1269 CP(*ki, *ki32, ki_lastcpu);
1271 /* XXX TODO: wrap cpu value as appropriate */
1272 CP(*ki, *ki32, ki_oncpu_old);
1273 CP(*ki, *ki32, ki_lastcpu_old);
1275 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1276 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1277 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1278 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1279 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1280 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1281 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1282 CP(*ki, *ki32, ki_tracer);
1283 CP(*ki, *ki32, ki_flag2);
1284 CP(*ki, *ki32, ki_fibnum);
1285 CP(*ki, *ki32, ki_cr_flags);
1286 CP(*ki, *ki32, ki_jid);
1287 CP(*ki, *ki32, ki_numthreads);
1288 CP(*ki, *ki32, ki_tid);
1289 CP(*ki, *ki32, ki_pri);
1290 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1291 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1292 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1293 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1294 PTRTRIM_CP(*ki, *ki32, ki_udata);
1295 CP(*ki, *ki32, ki_sflag);
1296 CP(*ki, *ki32, ki_tdflags);
1301 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1304 struct kinfo_proc ki;
1305 #ifdef COMPAT_FREEBSD32
1306 struct kinfo_proc32 ki32;
1310 PROC_LOCK_ASSERT(p, MA_OWNED);
1311 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1314 fill_kinfo_proc(p, &ki);
1315 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1316 #ifdef COMPAT_FREEBSD32
1317 if ((flags & KERN_PROC_MASK32) != 0) {
1318 freebsd32_kinfo_proc_out(&ki, &ki32);
1319 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1323 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1326 FOREACH_THREAD_IN_PROC(p, td) {
1327 fill_kinfo_thread(td, &ki, 1);
1328 #ifdef COMPAT_FREEBSD32
1329 if ((flags & KERN_PROC_MASK32) != 0) {
1330 freebsd32_kinfo_proc_out(&ki, &ki32);
1331 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1335 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1346 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1350 struct kinfo_proc ki;
1356 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1357 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1358 error = kern_proc_out(p, &sb, flags);
1359 error2 = sbuf_finish(&sb);
1363 else if (error2 != 0)
1383 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1385 int *name = (int *)arg1;
1386 u_int namelen = arg2;
1388 int flags, doingzomb, oid_number;
1391 oid_number = oidp->oid_number;
1392 if (oid_number != KERN_PROC_ALL &&
1393 (oid_number & KERN_PROC_INC_THREAD) == 0)
1394 flags = KERN_PROC_NOTHREADS;
1397 oid_number &= ~KERN_PROC_INC_THREAD;
1399 #ifdef COMPAT_FREEBSD32
1400 if (req->flags & SCTL_MASK32)
1401 flags |= KERN_PROC_MASK32;
1403 if (oid_number == KERN_PROC_PID) {
1406 error = sysctl_wire_old_buffer(req, 0);
1409 sx_slock(&proctree_lock);
1410 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1412 error = sysctl_out_proc(p, req, flags, 0);
1413 sx_sunlock(&proctree_lock);
1417 switch (oid_number) {
1422 case KERN_PROC_PROC:
1423 if (namelen != 0 && namelen != 1)
1433 /* overestimate by 5 procs */
1434 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1438 error = sysctl_wire_old_buffer(req, 0);
1441 sx_slock(&proctree_lock);
1442 sx_slock(&allproc_lock);
1443 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1445 p = LIST_FIRST(&allproc);
1447 p = LIST_FIRST(&zombproc);
1448 for (; p != NULL; p = LIST_NEXT(p, p_list)) {
1450 * Skip embryonic processes.
1453 if (p->p_state == PRS_NEW) {
1457 KASSERT(p->p_ucred != NULL,
1458 ("process credential is NULL for non-NEW proc"));
1460 * Show a user only appropriate processes.
1462 if (p_cansee(curthread, p)) {
1467 * TODO - make more efficient (see notes below).
1470 switch (oid_number) {
1473 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1479 case KERN_PROC_PGRP:
1480 /* could do this by traversing pgrp */
1481 if (p->p_pgrp == NULL ||
1482 p->p_pgrp->pg_id != (pid_t)name[0]) {
1488 case KERN_PROC_RGID:
1489 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1495 case KERN_PROC_SESSION:
1496 if (p->p_session == NULL ||
1497 p->p_session->s_sid != (pid_t)name[0]) {
1504 if ((p->p_flag & P_CONTROLT) == 0 ||
1505 p->p_session == NULL) {
1509 /* XXX proctree_lock */
1510 SESS_LOCK(p->p_session);
1511 if (p->p_session->s_ttyp == NULL ||
1512 tty_udev(p->p_session->s_ttyp) !=
1514 SESS_UNLOCK(p->p_session);
1518 SESS_UNLOCK(p->p_session);
1522 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1528 case KERN_PROC_RUID:
1529 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1535 case KERN_PROC_PROC:
1543 error = sysctl_out_proc(p, req, flags, doingzomb);
1545 sx_sunlock(&allproc_lock);
1546 sx_sunlock(&proctree_lock);
1551 sx_sunlock(&allproc_lock);
1552 sx_sunlock(&proctree_lock);
1557 pargs_alloc(int len)
1561 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1563 refcount_init(&pa->ar_ref, 1);
1564 pa->ar_length = len;
1569 pargs_free(struct pargs *pa)
1576 pargs_hold(struct pargs *pa)
1581 refcount_acquire(&pa->ar_ref);
1585 pargs_drop(struct pargs *pa)
1590 if (refcount_release(&pa->ar_ref))
1595 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1601 * This may return a short read if the string is shorter than the chunk
1602 * and is aligned at the end of the page, and the following page is not
1605 n = proc_readmem(td, p, (vm_offset_t)sptr, buf, len);
1611 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1613 enum proc_vector_type {
1619 #ifdef COMPAT_FREEBSD32
1621 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1622 size_t *vsizep, enum proc_vector_type type)
1624 struct freebsd32_ps_strings pss;
1626 vm_offset_t vptr, ptr;
1627 uint32_t *proc_vector32;
1633 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1634 sizeof(pss)) != sizeof(pss))
1638 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1639 vsize = pss.ps_nargvstr;
1640 if (vsize > ARG_MAX)
1642 size = vsize * sizeof(int32_t);
1645 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1646 vsize = pss.ps_nenvstr;
1647 if (vsize > ARG_MAX)
1649 size = vsize * sizeof(int32_t);
1652 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1653 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1656 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1657 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1660 if (aux.a_type == AT_NULL)
1664 if (aux.a_type != AT_NULL)
1667 size = vsize * sizeof(aux);
1670 KASSERT(0, ("Wrong proc vector type: %d", type));
1673 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1674 if (proc_readmem(td, p, vptr, proc_vector32, size) != size) {
1678 if (type == PROC_AUX) {
1679 *proc_vectorp = (char **)proc_vector32;
1683 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1684 for (i = 0; i < (int)vsize; i++)
1685 proc_vector[i] = PTRIN(proc_vector32[i]);
1686 *proc_vectorp = proc_vector;
1689 free(proc_vector32, M_TEMP);
1695 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1696 size_t *vsizep, enum proc_vector_type type)
1698 struct ps_strings pss;
1700 vm_offset_t vptr, ptr;
1705 #ifdef COMPAT_FREEBSD32
1706 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1707 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1709 if (proc_readmem(td, p, (vm_offset_t)p->p_sysent->sv_psstrings, &pss,
1710 sizeof(pss)) != sizeof(pss))
1714 vptr = (vm_offset_t)pss.ps_argvstr;
1715 vsize = pss.ps_nargvstr;
1716 if (vsize > ARG_MAX)
1718 size = vsize * sizeof(char *);
1721 vptr = (vm_offset_t)pss.ps_envstr;
1722 vsize = pss.ps_nenvstr;
1723 if (vsize > ARG_MAX)
1725 size = vsize * sizeof(char *);
1729 * The aux array is just above env array on the stack. Check
1730 * that the address is naturally aligned.
1732 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1734 #if __ELF_WORD_SIZE == 64
1735 if (vptr % sizeof(uint64_t) != 0)
1737 if (vptr % sizeof(uint32_t) != 0)
1741 * We count the array size reading the aux vectors from the
1742 * stack until AT_NULL vector is returned. So (to keep the code
1743 * simple) we read the process stack twice: the first time here
1744 * to find the size and the second time when copying the vectors
1745 * to the allocated proc_vector.
1747 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1748 if (proc_readmem(td, p, ptr, &aux, sizeof(aux)) !=
1751 if (aux.a_type == AT_NULL)
1756 * If the PROC_AUXV_MAX entries are iterated over, and we have
1757 * not reached AT_NULL, it is most likely we are reading wrong
1758 * data: either the process doesn't have auxv array or data has
1759 * been modified. Return the error in this case.
1761 if (aux.a_type != AT_NULL)
1764 size = vsize * sizeof(aux);
1767 KASSERT(0, ("Wrong proc vector type: %d", type));
1768 return (EINVAL); /* In case we are built without INVARIANTS. */
1770 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1771 if (proc_readmem(td, p, vptr, proc_vector, size) != size) {
1772 free(proc_vector, M_TEMP);
1775 *proc_vectorp = proc_vector;
1781 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1784 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1785 enum proc_vector_type type)
1787 size_t done, len, nchr, vsize;
1789 char **proc_vector, *sptr;
1790 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1792 PROC_ASSERT_HELD(p);
1795 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1797 nchr = 2 * (PATH_MAX + ARG_MAX);
1799 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1802 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1804 * The program may have scribbled into its argv array, e.g. to
1805 * remove some arguments. If that has happened, break out
1806 * before trying to read from NULL.
1808 if (proc_vector[i] == NULL)
1810 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1811 error = proc_read_string(td, p, sptr, pss_string,
1812 sizeof(pss_string));
1815 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1816 if (done + len >= nchr)
1817 len = nchr - done - 1;
1818 sbuf_bcat(sb, pss_string, len);
1819 if (len != GET_PS_STRINGS_CHUNK_SZ)
1821 done += GET_PS_STRINGS_CHUNK_SZ;
1823 sbuf_bcat(sb, "", 1);
1827 free(proc_vector, M_TEMP);
1832 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1835 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1839 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1842 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1846 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1852 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1854 #ifdef COMPAT_FREEBSD32
1855 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1856 size = vsize * sizeof(Elf32_Auxinfo);
1859 size = vsize * sizeof(Elf_Auxinfo);
1860 if (sbuf_bcat(sb, auxv, size) != 0)
1868 * This sysctl allows a process to retrieve the argument list or process
1869 * title for another process without groping around in the address space
1870 * of the other process. It also allow a process to set its own "process
1871 * title to a string of its own choice.
1874 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1876 int *name = (int *)arg1;
1877 u_int namelen = arg2;
1878 struct pargs *newpa, *pa;
1881 int flags, error = 0, error2;
1886 flags = PGET_CANSEE;
1887 if (req->newptr != NULL)
1888 flags |= PGET_ISCURRENT;
1889 error = pget((pid_t)name[0], flags, &p);
1897 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1899 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1902 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1903 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1904 error = proc_getargv(curthread, p, &sb);
1905 error2 = sbuf_finish(&sb);
1908 if (error == 0 && error2 != 0)
1913 if (error != 0 || req->newptr == NULL)
1916 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1918 newpa = pargs_alloc(req->newlen);
1919 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1933 * This sysctl allows a process to retrieve environment of another process.
1936 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1938 int *name = (int *)arg1;
1939 u_int namelen = arg2;
1947 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1950 if ((p->p_flag & P_SYSTEM) != 0) {
1955 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1956 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1957 error = proc_getenvv(curthread, p, &sb);
1958 error2 = sbuf_finish(&sb);
1961 return (error != 0 ? error : error2);
1965 * This sysctl allows a process to retrieve ELF auxiliary vector of
1969 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1971 int *name = (int *)arg1;
1972 u_int namelen = arg2;
1980 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1983 if ((p->p_flag & P_SYSTEM) != 0) {
1987 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1988 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1989 error = proc_getauxv(curthread, p, &sb);
1990 error2 = sbuf_finish(&sb);
1993 return (error != 0 ? error : error2);
1997 * This sysctl allows a process to retrieve the path of the executable for
1998 * itself or another process.
2001 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
2003 pid_t *pidp = (pid_t *)arg1;
2004 unsigned int arglen = arg2;
2007 char *retbuf, *freebuf;
2012 if (*pidp == -1) { /* -1 means this process */
2013 p = req->td->td_proc;
2015 error = pget(*pidp, PGET_CANSEE, &p);
2029 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
2033 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
2034 free(freebuf, M_TEMP);
2039 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2052 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2055 sv_name = p->p_sysent->sv_name;
2057 return (sysctl_handle_string(oidp, sv_name, 0, req));
2060 #ifdef KINFO_OVMENTRY_SIZE
2061 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2064 #ifdef COMPAT_FREEBSD7
2066 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2068 vm_map_entry_t entry, tmp_entry;
2069 unsigned int last_timestamp;
2070 char *fullpath, *freepath;
2071 struct kinfo_ovmentry *kve;
2081 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2084 vm = vmspace_acquire_ref(p);
2089 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2092 vm_map_lock_read(map);
2093 for (entry = map->header.next; entry != &map->header;
2094 entry = entry->next) {
2095 vm_object_t obj, tobj, lobj;
2098 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2101 bzero(kve, sizeof(*kve));
2102 kve->kve_structsize = sizeof(*kve);
2104 kve->kve_private_resident = 0;
2105 obj = entry->object.vm_object;
2107 VM_OBJECT_RLOCK(obj);
2108 if (obj->shadow_count == 1)
2109 kve->kve_private_resident =
2110 obj->resident_page_count;
2112 kve->kve_resident = 0;
2113 addr = entry->start;
2114 while (addr < entry->end) {
2115 if (pmap_extract(map->pmap, addr))
2116 kve->kve_resident++;
2120 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2122 VM_OBJECT_RLOCK(tobj);
2124 VM_OBJECT_RUNLOCK(lobj);
2128 kve->kve_start = (void*)entry->start;
2129 kve->kve_end = (void*)entry->end;
2130 kve->kve_offset = (off_t)entry->offset;
2132 if (entry->protection & VM_PROT_READ)
2133 kve->kve_protection |= KVME_PROT_READ;
2134 if (entry->protection & VM_PROT_WRITE)
2135 kve->kve_protection |= KVME_PROT_WRITE;
2136 if (entry->protection & VM_PROT_EXECUTE)
2137 kve->kve_protection |= KVME_PROT_EXEC;
2139 if (entry->eflags & MAP_ENTRY_COW)
2140 kve->kve_flags |= KVME_FLAG_COW;
2141 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2142 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2143 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2144 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2146 last_timestamp = map->timestamp;
2147 vm_map_unlock_read(map);
2149 kve->kve_fileid = 0;
2155 switch (lobj->type) {
2157 kve->kve_type = KVME_TYPE_DEFAULT;
2160 kve->kve_type = KVME_TYPE_VNODE;
2165 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2166 kve->kve_type = KVME_TYPE_VNODE;
2167 if ((lobj->flags & OBJ_TMPFS) != 0) {
2168 vp = lobj->un_pager.swp.swp_tmpfs;
2172 kve->kve_type = KVME_TYPE_SWAP;
2176 kve->kve_type = KVME_TYPE_DEVICE;
2179 kve->kve_type = KVME_TYPE_PHYS;
2182 kve->kve_type = KVME_TYPE_DEAD;
2185 kve->kve_type = KVME_TYPE_SG;
2188 kve->kve_type = KVME_TYPE_UNKNOWN;
2192 VM_OBJECT_RUNLOCK(lobj);
2194 kve->kve_ref_count = obj->ref_count;
2195 kve->kve_shadow_count = obj->shadow_count;
2196 VM_OBJECT_RUNLOCK(obj);
2198 vn_fullpath(curthread, vp, &fullpath,
2200 cred = curthread->td_ucred;
2201 vn_lock(vp, LK_SHARED | LK_RETRY);
2202 if (VOP_GETATTR(vp, &va, cred) == 0) {
2203 kve->kve_fileid = va.va_fileid;
2204 kve->kve_fsid = va.va_fsid;
2209 kve->kve_type = KVME_TYPE_NONE;
2210 kve->kve_ref_count = 0;
2211 kve->kve_shadow_count = 0;
2214 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2215 if (freepath != NULL)
2216 free(freepath, M_TEMP);
2218 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2219 vm_map_lock_read(map);
2222 if (last_timestamp != map->timestamp) {
2223 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2227 vm_map_unlock_read(map);
2233 #endif /* COMPAT_FREEBSD7 */
2235 #ifdef KINFO_VMENTRY_SIZE
2236 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2240 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2241 struct kinfo_vmentry *kve)
2243 vm_object_t obj, tobj;
2246 vm_paddr_t locked_pa;
2247 vm_pindex_t pi, pi_adv, pindex;
2250 obj = entry->object.vm_object;
2251 addr = entry->start;
2253 pi = OFF_TO_IDX(entry->offset);
2254 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2255 if (m_adv != NULL) {
2258 pi_adv = OFF_TO_IDX(entry->end - addr);
2260 for (tobj = obj;; tobj = tobj->backing_object) {
2261 m = vm_page_find_least(tobj, pindex);
2263 if (m->pindex == pindex)
2265 if (pi_adv > m->pindex - pindex) {
2266 pi_adv = m->pindex - pindex;
2270 if (tobj->backing_object == NULL)
2272 pindex += OFF_TO_IDX(tobj->
2273 backing_object_offset);
2277 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2278 (addr & (pagesizes[1] - 1)) == 0 &&
2279 (pmap_mincore(map->pmap, addr, &locked_pa) &
2280 MINCORE_SUPER) != 0) {
2281 kve->kve_flags |= KVME_FLAG_SUPER;
2282 pi_adv = OFF_TO_IDX(pagesizes[1]);
2285 * We do not test the found page on validity.
2286 * Either the page is busy and being paged in,
2287 * or it was invalidated. The first case
2288 * should be counted as resident, the second
2289 * is not so clear; we do account both.
2293 kve->kve_resident += pi_adv;
2296 PA_UNLOCK_COND(locked_pa);
2300 * Must be called with the process locked and will return unlocked.
2303 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2305 vm_map_entry_t entry, tmp_entry;
2308 vm_object_t obj, tobj, lobj;
2309 char *fullpath, *freepath;
2310 struct kinfo_vmentry *kve;
2315 unsigned int last_timestamp;
2318 PROC_LOCK_ASSERT(p, MA_OWNED);
2322 vm = vmspace_acquire_ref(p);
2327 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2331 vm_map_lock_read(map);
2332 for (entry = map->header.next; entry != &map->header;
2333 entry = entry->next) {
2334 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2338 bzero(kve, sizeof(*kve));
2339 obj = entry->object.vm_object;
2341 for (tobj = obj; tobj != NULL;
2342 tobj = tobj->backing_object) {
2343 VM_OBJECT_RLOCK(tobj);
2346 if (obj->backing_object == NULL)
2347 kve->kve_private_resident =
2348 obj->resident_page_count;
2349 if (!vmmap_skip_res_cnt)
2350 kern_proc_vmmap_resident(map, entry, kve);
2351 for (tobj = obj; tobj != NULL;
2352 tobj = tobj->backing_object) {
2353 if (tobj != obj && tobj != lobj)
2354 VM_OBJECT_RUNLOCK(tobj);
2360 kve->kve_start = entry->start;
2361 kve->kve_end = entry->end;
2362 kve->kve_offset = entry->offset;
2364 if (entry->protection & VM_PROT_READ)
2365 kve->kve_protection |= KVME_PROT_READ;
2366 if (entry->protection & VM_PROT_WRITE)
2367 kve->kve_protection |= KVME_PROT_WRITE;
2368 if (entry->protection & VM_PROT_EXECUTE)
2369 kve->kve_protection |= KVME_PROT_EXEC;
2371 if (entry->eflags & MAP_ENTRY_COW)
2372 kve->kve_flags |= KVME_FLAG_COW;
2373 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2374 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2375 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2376 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2377 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2378 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2379 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2380 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2382 last_timestamp = map->timestamp;
2383 vm_map_unlock_read(map);
2389 switch (lobj->type) {
2391 kve->kve_type = KVME_TYPE_DEFAULT;
2394 kve->kve_type = KVME_TYPE_VNODE;
2399 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2400 kve->kve_type = KVME_TYPE_VNODE;
2401 if ((lobj->flags & OBJ_TMPFS) != 0) {
2402 vp = lobj->un_pager.swp.swp_tmpfs;
2406 kve->kve_type = KVME_TYPE_SWAP;
2410 kve->kve_type = KVME_TYPE_DEVICE;
2413 kve->kve_type = KVME_TYPE_PHYS;
2416 kve->kve_type = KVME_TYPE_DEAD;
2419 kve->kve_type = KVME_TYPE_SG;
2421 case OBJT_MGTDEVICE:
2422 kve->kve_type = KVME_TYPE_MGTDEVICE;
2425 kve->kve_type = KVME_TYPE_UNKNOWN;
2429 VM_OBJECT_RUNLOCK(lobj);
2431 kve->kve_ref_count = obj->ref_count;
2432 kve->kve_shadow_count = obj->shadow_count;
2433 VM_OBJECT_RUNLOCK(obj);
2435 vn_fullpath(curthread, vp, &fullpath,
2437 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2438 cred = curthread->td_ucred;
2439 vn_lock(vp, LK_SHARED | LK_RETRY);
2440 if (VOP_GETATTR(vp, &va, cred) == 0) {
2441 kve->kve_vn_fileid = va.va_fileid;
2442 kve->kve_vn_fsid = va.va_fsid;
2444 MAKEIMODE(va.va_type, va.va_mode);
2445 kve->kve_vn_size = va.va_size;
2446 kve->kve_vn_rdev = va.va_rdev;
2447 kve->kve_status = KF_ATTR_VALID;
2452 kve->kve_type = KVME_TYPE_NONE;
2453 kve->kve_ref_count = 0;
2454 kve->kve_shadow_count = 0;
2457 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2458 if (freepath != NULL)
2459 free(freepath, M_TEMP);
2461 /* Pack record size down */
2462 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2463 kve->kve_structsize =
2464 offsetof(struct kinfo_vmentry, kve_path) +
2465 strlen(kve->kve_path) + 1;
2467 kve->kve_structsize = sizeof(*kve);
2468 kve->kve_structsize = roundup(kve->kve_structsize,
2471 /* Halt filling and truncate rather than exceeding maxlen */
2472 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2474 vm_map_lock_read(map);
2476 } else if (maxlen != -1)
2477 maxlen -= kve->kve_structsize;
2479 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2481 vm_map_lock_read(map);
2484 if (last_timestamp != map->timestamp) {
2485 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2489 vm_map_unlock_read(map);
2497 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2501 int error, error2, *name;
2504 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2505 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2506 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2511 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2512 error2 = sbuf_finish(&sb);
2514 return (error != 0 ? error : error2);
2517 #if defined(STACK) || defined(DDB)
2519 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2521 struct kinfo_kstack *kkstp;
2522 int error, i, *name, numthreads;
2523 lwpid_t *lwpidarray;
2530 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2534 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2535 st = stack_create();
2540 if (lwpidarray != NULL) {
2541 free(lwpidarray, M_TEMP);
2544 numthreads = p->p_numthreads;
2546 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2549 } while (numthreads < p->p_numthreads);
2552 * XXXRW: During the below loop, execve(2) and countless other sorts
2553 * of changes could have taken place. Should we check to see if the
2554 * vmspace has been replaced, or the like, in order to prevent
2555 * giving a snapshot that spans, say, execve(2), with some threads
2556 * before and some after? Among other things, the credentials could
2557 * have changed, in which case the right to extract debug info might
2558 * no longer be assured.
2561 FOREACH_THREAD_IN_PROC(p, td) {
2562 KASSERT(i < numthreads,
2563 ("sysctl_kern_proc_kstack: numthreads"));
2564 lwpidarray[i] = td->td_tid;
2568 for (i = 0; i < numthreads; i++) {
2569 td = thread_find(p, lwpidarray[i]);
2573 bzero(kkstp, sizeof(*kkstp));
2574 (void)sbuf_new(&sb, kkstp->kkst_trace,
2575 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2577 kkstp->kkst_tid = td->td_tid;
2578 if (TD_IS_SWAPPED(td)) {
2579 kkstp->kkst_state = KKST_STATE_SWAPPED;
2580 } else if (TD_IS_RUNNING(td)) {
2581 if (stack_save_td_running(st, td) == 0)
2582 kkstp->kkst_state = KKST_STATE_STACKOK;
2584 kkstp->kkst_state = KKST_STATE_RUNNING;
2586 kkstp->kkst_state = KKST_STATE_STACKOK;
2587 stack_save_td(st, td);
2591 stack_sbuf_print(&sb, st);
2594 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2601 if (lwpidarray != NULL)
2602 free(lwpidarray, M_TEMP);
2604 free(kkstp, M_TEMP);
2610 * This sysctl allows a process to retrieve the full list of groups from
2611 * itself or another process.
2614 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2616 pid_t *pidp = (pid_t *)arg1;
2617 unsigned int arglen = arg2;
2624 if (*pidp == -1) { /* -1 means this process */
2625 p = req->td->td_proc;
2628 error = pget(*pidp, PGET_CANSEE, &p);
2633 cred = crhold(p->p_ucred);
2636 error = SYSCTL_OUT(req, cred->cr_groups,
2637 cred->cr_ngroups * sizeof(gid_t));
2643 * This sysctl allows a process to retrieve or/and set the resource limit for
2647 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2649 int *name = (int *)arg1;
2650 u_int namelen = arg2;
2659 which = (u_int)name[1];
2660 if (which >= RLIM_NLIMITS)
2663 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2666 flags = PGET_HOLD | PGET_NOTWEXIT;
2667 if (req->newptr != NULL)
2668 flags |= PGET_CANDEBUG;
2670 flags |= PGET_CANSEE;
2671 error = pget((pid_t)name[0], flags, &p);
2678 if (req->oldptr != NULL) {
2680 lim_rlimit_proc(p, which, &rlim);
2683 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2690 if (req->newptr != NULL) {
2691 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2693 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2702 * This sysctl allows a process to retrieve ps_strings structure location of
2706 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2708 int *name = (int *)arg1;
2709 u_int namelen = arg2;
2711 vm_offset_t ps_strings;
2713 #ifdef COMPAT_FREEBSD32
2714 uint32_t ps_strings32;
2720 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2723 #ifdef COMPAT_FREEBSD32
2724 if ((req->flags & SCTL_MASK32) != 0) {
2726 * We return 0 if the 32 bit emulation request is for a 64 bit
2729 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2730 PTROUT(p->p_sysent->sv_psstrings) : 0;
2732 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2736 ps_strings = p->p_sysent->sv_psstrings;
2738 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2743 * This sysctl allows a process to retrieve umask of another process.
2746 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2748 int *name = (int *)arg1;
2749 u_int namelen = arg2;
2757 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2761 FILEDESC_SLOCK(p->p_fd);
2762 fd_cmask = p->p_fd->fd_cmask;
2763 FILEDESC_SUNLOCK(p->p_fd);
2765 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2770 * This sysctl allows a process to set and retrieve binary osreldate of
2774 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2776 int *name = (int *)arg1;
2777 u_int namelen = arg2;
2779 int flags, error, osrel;
2784 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2787 flags = PGET_HOLD | PGET_NOTWEXIT;
2788 if (req->newptr != NULL)
2789 flags |= PGET_CANDEBUG;
2791 flags |= PGET_CANSEE;
2792 error = pget((pid_t)name[0], flags, &p);
2796 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2800 if (req->newptr != NULL) {
2801 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2816 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2818 int *name = (int *)arg1;
2819 u_int namelen = arg2;
2821 struct kinfo_sigtramp kst;
2822 const struct sysentvec *sv;
2824 #ifdef COMPAT_FREEBSD32
2825 struct kinfo_sigtramp32 kst32;
2831 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2835 #ifdef COMPAT_FREEBSD32
2836 if ((req->flags & SCTL_MASK32) != 0) {
2837 bzero(&kst32, sizeof(kst32));
2838 if (SV_PROC_FLAG(p, SV_ILP32)) {
2839 if (sv->sv_sigcode_base != 0) {
2840 kst32.ksigtramp_start = sv->sv_sigcode_base;
2841 kst32.ksigtramp_end = sv->sv_sigcode_base +
2844 kst32.ksigtramp_start = sv->sv_psstrings -
2846 kst32.ksigtramp_end = sv->sv_psstrings;
2850 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2854 bzero(&kst, sizeof(kst));
2855 if (sv->sv_sigcode_base != 0) {
2856 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2857 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2860 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2862 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2865 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2869 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2871 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2872 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2873 "Return entire process table");
2875 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2876 sysctl_kern_proc, "Process table");
2878 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2879 sysctl_kern_proc, "Process table");
2881 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2882 sysctl_kern_proc, "Process table");
2884 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2885 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2887 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2888 sysctl_kern_proc, "Process table");
2890 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2891 sysctl_kern_proc, "Process table");
2893 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2894 sysctl_kern_proc, "Process table");
2896 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2897 sysctl_kern_proc, "Process table");
2899 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2900 sysctl_kern_proc, "Return process table, no threads");
2902 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2903 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2904 sysctl_kern_proc_args, "Process argument list");
2906 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2907 sysctl_kern_proc_env, "Process environment");
2909 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2910 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2912 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2913 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2915 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2916 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2917 "Process syscall vector name (ABI type)");
2919 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2920 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2922 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2923 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2925 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2926 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2928 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2929 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2931 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2932 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2934 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2935 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2937 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2938 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2940 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2941 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2943 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2944 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2945 "Return process table, no threads");
2947 #ifdef COMPAT_FREEBSD7
2948 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2949 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2952 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2953 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2955 #if defined(STACK) || defined(DDB)
2956 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2957 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2960 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2961 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2963 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2964 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2965 "Process resource limits");
2967 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2968 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2969 "Process ps_strings location");
2971 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2972 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2974 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2975 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2976 "Process binary osreldate");
2978 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2979 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2980 "Process signal trampoline location");
2985 * stop_all_proc() purpose is to stop all process which have usermode,
2986 * except current process for obvious reasons. This makes it somewhat
2987 * unreliable when invoked from multithreaded process. The service
2988 * must not be user-callable anyway.
2993 struct proc *cp, *p;
2995 bool restart, seen_stopped, seen_exiting, stopped_some;
2999 sx_xlock(&allproc_lock);
3001 seen_exiting = seen_stopped = stopped_some = restart = false;
3002 LIST_REMOVE(cp, p_list);
3003 LIST_INSERT_HEAD(&allproc, cp, p_list);
3005 p = LIST_NEXT(cp, p_list);
3008 LIST_REMOVE(cp, p_list);
3009 LIST_INSERT_AFTER(p, cp, p_list);
3011 if ((p->p_flag & (P_KPROC | P_SYSTEM | P_TOTAL_STOP)) != 0) {
3015 if ((p->p_flag & P_WEXIT) != 0) {
3016 seen_exiting = true;
3020 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
3022 * Stopped processes are tolerated when there
3023 * are no other processes which might continue
3024 * them. P_STOPPED_SINGLE but not
3025 * P_TOTAL_STOP process still has at least one
3028 seen_stopped = true;
3033 sx_xunlock(&allproc_lock);
3034 r = thread_single(p, SINGLE_ALLPROC);
3038 stopped_some = true;
3041 sx_xlock(&allproc_lock);
3043 /* Catch forked children we did not see in iteration. */
3044 if (gen != allproc_gen)
3046 sx_xunlock(&allproc_lock);
3047 if (restart || stopped_some || seen_exiting || seen_stopped) {
3048 kern_yield(PRI_USER);
3054 resume_all_proc(void)
3056 struct proc *cp, *p;
3059 sx_xlock(&allproc_lock);
3060 LIST_REMOVE(cp, p_list);
3061 LIST_INSERT_HEAD(&allproc, cp, p_list);
3063 p = LIST_NEXT(cp, p_list);
3066 LIST_REMOVE(cp, p_list);
3067 LIST_INSERT_AFTER(p, cp, p_list);
3069 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3070 sx_xunlock(&allproc_lock);
3072 thread_single_end(p, SINGLE_ALLPROC);
3075 sx_xlock(&allproc_lock);
3080 sx_xunlock(&allproc_lock);
3083 /* #define TOTAL_STOP_DEBUG 1 */
3084 #ifdef TOTAL_STOP_DEBUG
3085 volatile static int ap_resume;
3086 #include <sys/mount.h>
3089 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3095 error = sysctl_handle_int(oidp, &val, 0, req);
3096 if (error != 0 || req->newptr == NULL)
3101 while (ap_resume == 0)
3109 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3110 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3111 sysctl_debug_stop_all_proc, "I",