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>
45 #include <sys/kernel.h>
46 #include <sys/limits.h>
48 #include <sys/loginclass.h>
49 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
54 #include <sys/ptrace.h>
55 #include <sys/refcount.h>
56 #include <sys/resourcevar.h>
57 #include <sys/rwlock.h>
59 #include <sys/sysent.h>
60 #include <sys/sched.h>
62 #include <sys/stack.h>
64 #include <sys/sysctl.h>
65 #include <sys/filedesc.h>
67 #include <sys/signalvar.h>
72 #include <sys/vnode.h>
73 #include <sys/eventhandler.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_extern.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
88 #ifdef COMPAT_FREEBSD32
89 #include <compat/freebsd32/freebsd32.h>
90 #include <compat/freebsd32/freebsd32_util.h>
93 SDT_PROVIDER_DEFINE(proc);
94 SDT_PROBE_DEFINE4(proc, kernel, ctor, entry, "struct proc *", "int",
96 SDT_PROBE_DEFINE4(proc, kernel, ctor, return, "struct proc *", "int",
98 SDT_PROBE_DEFINE4(proc, kernel, dtor, entry, "struct proc *", "int",
99 "void *", "struct thread *");
100 SDT_PROBE_DEFINE3(proc, kernel, dtor, return, "struct proc *", "int",
102 SDT_PROBE_DEFINE3(proc, kernel, init, entry, "struct proc *", "int",
104 SDT_PROBE_DEFINE3(proc, kernel, init, return, "struct proc *", "int",
107 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
108 MALLOC_DEFINE(M_SESSION, "session", "session header");
109 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
110 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
112 static void doenterpgrp(struct proc *, struct pgrp *);
113 static void orphanpg(struct pgrp *pg);
114 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
115 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
116 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
118 static void pgadjustjobc(struct pgrp *pgrp, int entering);
119 static void pgdelete(struct pgrp *);
120 static int proc_ctor(void *mem, int size, void *arg, int flags);
121 static void proc_dtor(void *mem, int size, void *arg);
122 static int proc_init(void *mem, int size, int flags);
123 static void proc_fini(void *mem, int size);
124 static void pargs_free(struct pargs *pa);
125 static struct proc *zpfind_locked(pid_t pid);
128 * Other process lists
130 struct pidhashhead *pidhashtbl;
132 struct pgrphashhead *pgrphashtbl;
134 struct proclist allproc;
135 struct proclist zombproc;
136 struct sx allproc_lock;
137 struct sx proctree_lock;
138 struct mtx ppeers_lock;
139 uma_zone_t proc_zone;
141 int kstack_pages = KSTACK_PAGES;
142 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
143 "Kernel stack size in pages");
144 static int vmmap_skip_res_cnt = 0;
145 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
146 &vmmap_skip_res_cnt, 0,
147 "Skip calculation of the pages resident count in kern.proc.vmmap");
149 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
150 #ifdef COMPAT_FREEBSD32
151 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
155 * Initialize global process hashing structures.
161 sx_init(&allproc_lock, "allproc");
162 sx_init(&proctree_lock, "proctree");
163 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
165 LIST_INIT(&zombproc);
166 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
167 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
168 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
169 proc_ctor, proc_dtor, proc_init, proc_fini,
170 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
175 * Prepare a proc for use.
178 proc_ctor(void *mem, int size, void *arg, int flags)
182 p = (struct proc *)mem;
183 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
184 EVENTHANDLER_INVOKE(process_ctor, p);
185 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
190 * Reclaim a proc after use.
193 proc_dtor(void *mem, int size, void *arg)
198 /* INVARIANTS checks go here */
199 p = (struct proc *)mem;
200 td = FIRST_THREAD_IN_PROC(p);
201 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
204 KASSERT((p->p_numthreads == 1),
205 ("bad number of threads in exiting process"));
206 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
208 /* Free all OSD associated to this thread. */
211 EVENTHANDLER_INVOKE(process_dtor, p);
212 if (p->p_ksi != NULL)
213 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
214 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
218 * Initialize type-stable parts of a proc (when newly created).
221 proc_init(void *mem, int size, int flags)
225 p = (struct proc *)mem;
226 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
227 p->p_sched = (struct p_sched *)&p[1];
228 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
229 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
230 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
231 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
232 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
233 cv_init(&p->p_pwait, "ppwait");
234 cv_init(&p->p_dbgwait, "dbgwait");
235 TAILQ_INIT(&p->p_threads); /* all threads in proc */
236 EVENTHANDLER_INVOKE(process_init, p);
237 p->p_stats = pstats_alloc();
238 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
243 * UMA should ensure that this function is never called.
244 * Freeing a proc structure would violate type stability.
247 proc_fini(void *mem, int size)
252 p = (struct proc *)mem;
253 EVENTHANDLER_INVOKE(process_fini, p);
254 pstats_free(p->p_stats);
255 thread_free(FIRST_THREAD_IN_PROC(p));
256 mtx_destroy(&p->p_mtx);
257 if (p->p_ksi != NULL)
258 ksiginfo_free(p->p_ksi);
260 panic("proc reclaimed");
265 * Is p an inferior of the current process?
268 inferior(struct proc *p)
271 sx_assert(&proctree_lock, SX_LOCKED);
272 PROC_LOCK_ASSERT(p, MA_OWNED);
273 for (; p != curproc; p = proc_realparent(p)) {
281 pfind_locked(pid_t pid)
285 sx_assert(&allproc_lock, SX_LOCKED);
286 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
287 if (p->p_pid == pid) {
289 if (p->p_state == PRS_NEW) {
300 * Locate a process by number; return only "live" processes -- i.e., neither
301 * zombies nor newly born but incompletely initialized processes. By not
302 * returning processes in the PRS_NEW state, we allow callers to avoid
303 * testing for that condition to avoid dereferencing p_ucred, et al.
310 sx_slock(&allproc_lock);
311 p = pfind_locked(pid);
312 sx_sunlock(&allproc_lock);
317 pfind_tid_locked(pid_t tid)
322 sx_assert(&allproc_lock, SX_LOCKED);
323 FOREACH_PROC_IN_SYSTEM(p) {
325 if (p->p_state == PRS_NEW) {
329 FOREACH_THREAD_IN_PROC(p, td) {
330 if (td->td_tid == tid)
340 * Locate a process group by number.
341 * The caller must hold proctree_lock.
347 register struct pgrp *pgrp;
349 sx_assert(&proctree_lock, SX_LOCKED);
351 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
352 if (pgrp->pg_id == pgid) {
361 * Locate process and do additional manipulations, depending on flags.
364 pget(pid_t pid, int flags, struct proc **pp)
369 sx_slock(&allproc_lock);
370 if (pid <= PID_MAX) {
371 p = pfind_locked(pid);
372 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
373 p = zpfind_locked(pid);
374 } else if ((flags & PGET_NOTID) == 0) {
375 p = pfind_tid_locked(pid);
379 sx_sunlock(&allproc_lock);
382 if ((flags & PGET_CANSEE) != 0) {
383 error = p_cansee(curthread, p);
387 if ((flags & PGET_CANDEBUG) != 0) {
388 error = p_candebug(curthread, p);
392 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
396 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
400 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
402 * XXXRW: Not clear ESRCH is the right error during proc
408 if ((flags & PGET_HOLD) != 0) {
420 * Create a new process group.
421 * pgid must be equal to the pid of p.
422 * Begin a new session if required.
425 enterpgrp(p, pgid, pgrp, sess)
426 register struct proc *p;
429 struct session *sess;
432 sx_assert(&proctree_lock, SX_XLOCKED);
434 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
435 KASSERT(p->p_pid == pgid,
436 ("enterpgrp: new pgrp and pid != pgid"));
437 KASSERT(pgfind(pgid) == NULL,
438 ("enterpgrp: pgrp with pgid exists"));
439 KASSERT(!SESS_LEADER(p),
440 ("enterpgrp: session leader attempted setpgrp"));
442 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
448 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
450 p->p_flag &= ~P_CONTROLT;
454 sess->s_sid = p->p_pid;
455 refcount_init(&sess->s_count, 1);
456 sess->s_ttyvp = NULL;
457 sess->s_ttydp = NULL;
459 bcopy(p->p_session->s_login, sess->s_login,
460 sizeof(sess->s_login));
461 pgrp->pg_session = sess;
462 KASSERT(p == curproc,
463 ("enterpgrp: mksession and p != curproc"));
465 pgrp->pg_session = p->p_session;
466 sess_hold(pgrp->pg_session);
470 LIST_INIT(&pgrp->pg_members);
473 * As we have an exclusive lock of proctree_lock,
474 * this should not deadlock.
476 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
478 SLIST_INIT(&pgrp->pg_sigiolst);
481 doenterpgrp(p, pgrp);
487 * Move p to an existing process group
490 enterthispgrp(p, pgrp)
491 register struct proc *p;
495 sx_assert(&proctree_lock, SX_XLOCKED);
496 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
497 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
498 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
499 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
500 KASSERT(pgrp->pg_session == p->p_session,
501 ("%s: pgrp's session %p, p->p_session %p.\n",
505 KASSERT(pgrp != p->p_pgrp,
506 ("%s: p belongs to pgrp.", __func__));
508 doenterpgrp(p, pgrp);
514 * Move p to a process group
521 struct pgrp *savepgrp;
523 sx_assert(&proctree_lock, SX_XLOCKED);
524 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
525 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
526 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
527 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
529 savepgrp = p->p_pgrp;
532 * Adjust eligibility of affected pgrps to participate in job control.
533 * Increment eligibility counts before decrementing, otherwise we
534 * could reach 0 spuriously during the first call.
537 fixjobc(p, p->p_pgrp, 0);
542 LIST_REMOVE(p, p_pglist);
545 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
546 PGRP_UNLOCK(savepgrp);
548 if (LIST_EMPTY(&savepgrp->pg_members))
553 * remove process from process group
557 register struct proc *p;
559 struct pgrp *savepgrp;
561 sx_assert(&proctree_lock, SX_XLOCKED);
562 savepgrp = p->p_pgrp;
565 LIST_REMOVE(p, p_pglist);
568 PGRP_UNLOCK(savepgrp);
569 if (LIST_EMPTY(&savepgrp->pg_members))
575 * delete a process group
579 register struct pgrp *pgrp;
581 struct session *savesess;
584 sx_assert(&proctree_lock, SX_XLOCKED);
585 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
586 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
589 * Reset any sigio structures pointing to us as a result of
590 * F_SETOWN with our pgid.
592 funsetownlst(&pgrp->pg_sigiolst);
595 tp = pgrp->pg_session->s_ttyp;
596 LIST_REMOVE(pgrp, pg_hash);
597 savesess = pgrp->pg_session;
600 /* Remove the reference to the pgrp before deallocating it. */
603 tty_rel_pgrp(tp, pgrp);
606 mtx_destroy(&pgrp->pg_mtx);
608 sess_release(savesess);
612 pgadjustjobc(pgrp, entering)
622 if (pgrp->pg_jobc == 0)
629 * Adjust pgrp jobc counters when specified process changes process group.
630 * We count the number of processes in each process group that "qualify"
631 * the group for terminal job control (those with a parent in a different
632 * process group of the same session). If that count reaches zero, the
633 * process group becomes orphaned. Check both the specified process'
634 * process group and that of its children.
635 * entering == 0 => p is leaving specified group.
636 * entering == 1 => p is entering specified group.
639 fixjobc(p, pgrp, entering)
640 register struct proc *p;
641 register struct pgrp *pgrp;
644 register struct pgrp *hispgrp;
645 register struct session *mysession;
647 sx_assert(&proctree_lock, SX_LOCKED);
648 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
649 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
650 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
653 * Check p's parent to see whether p qualifies its own process
654 * group; if so, adjust count for p's process group.
656 mysession = pgrp->pg_session;
657 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
658 hispgrp->pg_session == mysession)
659 pgadjustjobc(pgrp, entering);
662 * Check this process' children to see whether they qualify
663 * their process groups; if so, adjust counts for children's
666 LIST_FOREACH(p, &p->p_children, p_sibling) {
668 if (hispgrp == pgrp ||
669 hispgrp->pg_session != mysession)
672 if (p->p_state == PRS_ZOMBIE) {
677 pgadjustjobc(hispgrp, entering);
682 * A process group has become orphaned;
683 * if there are any stopped processes in the group,
684 * hang-up all process in that group.
690 register struct proc *p;
692 PGRP_LOCK_ASSERT(pg, MA_OWNED);
694 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
696 if (P_SHOULDSTOP(p)) {
698 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
700 kern_psignal(p, SIGHUP);
701 kern_psignal(p, SIGCONT);
711 sess_hold(struct session *s)
714 refcount_acquire(&s->s_count);
718 sess_release(struct session *s)
721 if (refcount_release(&s->s_count)) {
722 if (s->s_ttyp != NULL) {
724 tty_rel_sess(s->s_ttyp, s);
726 mtx_destroy(&s->s_mtx);
733 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
735 register struct pgrp *pgrp;
736 register struct proc *p;
739 for (i = 0; i <= pgrphash; i++) {
740 if (!LIST_EMPTY(&pgrphashtbl[i])) {
741 printf("\tindx %d\n", i);
742 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
744 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
745 (void *)pgrp, (long)pgrp->pg_id,
746 (void *)pgrp->pg_session,
747 pgrp->pg_session->s_count,
748 (void *)LIST_FIRST(&pgrp->pg_members));
749 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
750 printf("\t\tpid %ld addr %p pgrp %p\n",
751 (long)p->p_pid, (void *)p,
761 * Calculate the kinfo_proc members which contain process-wide
763 * Must be called with the target process locked.
766 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
770 PROC_LOCK_ASSERT(p, MA_OWNED);
774 FOREACH_THREAD_IN_PROC(p, td) {
776 kp->ki_pctcpu += sched_pctcpu(td);
777 kp->ki_estcpu += td->td_estcpu;
783 * Clear kinfo_proc and fill in any information that is common
784 * to all threads in the process.
785 * Must be called with the target process locked.
788 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
796 /* For proc_realparent. */
797 sx_assert(&proctree_lock, SX_LOCKED);
798 PROC_LOCK_ASSERT(p, MA_OWNED);
799 bzero(kp, sizeof(*kp));
801 kp->ki_structsize = sizeof(*kp);
803 kp->ki_addr =/* p->p_addr; */0; /* XXX */
804 kp->ki_args = p->p_args;
805 kp->ki_textvp = p->p_textvp;
807 kp->ki_tracep = p->p_tracevp;
808 kp->ki_traceflag = p->p_traceflag;
811 kp->ki_vmspace = p->p_vmspace;
812 kp->ki_flag = p->p_flag;
813 kp->ki_flag2 = p->p_flag2;
816 kp->ki_uid = cred->cr_uid;
817 kp->ki_ruid = cred->cr_ruid;
818 kp->ki_svuid = cred->cr_svuid;
820 if (cred->cr_flags & CRED_FLAG_CAPMODE)
821 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
822 /* XXX bde doesn't like KI_NGROUPS */
823 if (cred->cr_ngroups > KI_NGROUPS) {
824 kp->ki_ngroups = KI_NGROUPS;
825 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
827 kp->ki_ngroups = cred->cr_ngroups;
828 bcopy(cred->cr_groups, kp->ki_groups,
829 kp->ki_ngroups * sizeof(gid_t));
830 kp->ki_rgid = cred->cr_rgid;
831 kp->ki_svgid = cred->cr_svgid;
832 /* If jailed(cred), emulate the old P_JAILED flag. */
834 kp->ki_flag |= P_JAILED;
835 /* If inside the jail, use 0 as a jail ID. */
836 if (cred->cr_prison != curthread->td_ucred->cr_prison)
837 kp->ki_jid = cred->cr_prison->pr_id;
839 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
840 sizeof(kp->ki_loginclass));
844 mtx_lock(&ps->ps_mtx);
845 kp->ki_sigignore = ps->ps_sigignore;
846 kp->ki_sigcatch = ps->ps_sigcatch;
847 mtx_unlock(&ps->ps_mtx);
849 if (p->p_state != PRS_NEW &&
850 p->p_state != PRS_ZOMBIE &&
851 p->p_vmspace != NULL) {
852 struct vmspace *vm = p->p_vmspace;
854 kp->ki_size = vm->vm_map.size;
855 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
856 FOREACH_THREAD_IN_PROC(p, td0) {
857 if (!TD_IS_SWAPPED(td0))
858 kp->ki_rssize += td0->td_kstack_pages;
860 kp->ki_swrss = vm->vm_swrss;
861 kp->ki_tsize = vm->vm_tsize;
862 kp->ki_dsize = vm->vm_dsize;
863 kp->ki_ssize = vm->vm_ssize;
864 } else if (p->p_state == PRS_ZOMBIE)
866 if (kp->ki_flag & P_INMEM)
867 kp->ki_sflag = PS_INMEM;
870 /* Calculate legacy swtime as seconds since 'swtick'. */
871 kp->ki_swtime = (ticks - p->p_swtick) / hz;
872 kp->ki_pid = p->p_pid;
873 kp->ki_nice = p->p_nice;
874 kp->ki_fibnum = p->p_fibnum;
875 kp->ki_start = p->p_stats->p_start;
876 timevaladd(&kp->ki_start, &boottime);
878 rufetch(p, &kp->ki_rusage);
879 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
880 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
882 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
883 /* Some callers want child times in a single value. */
884 kp->ki_childtime = kp->ki_childstime;
885 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
887 FOREACH_THREAD_IN_PROC(p, td0)
888 kp->ki_cow += td0->td_cow;
892 kp->ki_pgid = p->p_pgrp->pg_id;
893 kp->ki_jobc = p->p_pgrp->pg_jobc;
894 sp = p->p_pgrp->pg_session;
897 kp->ki_sid = sp->s_sid;
899 strlcpy(kp->ki_login, sp->s_login,
900 sizeof(kp->ki_login));
902 kp->ki_kiflag |= KI_CTTY;
904 kp->ki_kiflag |= KI_SLEADER;
905 /* XXX proctree_lock */
910 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
911 kp->ki_tdev = tty_udev(tp);
912 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
914 kp->ki_tsid = tp->t_session->s_sid;
917 if (p->p_comm[0] != '\0')
918 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
919 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
920 p->p_sysent->sv_name[0] != '\0')
921 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
922 kp->ki_siglist = p->p_siglist;
923 kp->ki_xstat = p->p_xstat;
924 kp->ki_acflag = p->p_acflag;
925 kp->ki_lock = p->p_lock;
927 kp->ki_ppid = proc_realparent(p)->p_pid;
928 if (p->p_flag & P_TRACED)
929 kp->ki_tracer = p->p_pptr->p_pid;
934 * Fill in information that is thread specific. Must be called with
935 * target process locked. If 'preferthread' is set, overwrite certain
936 * process-related fields that are maintained for both threads and
940 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
946 PROC_LOCK_ASSERT(p, MA_OWNED);
951 if (td->td_wmesg != NULL)
952 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
954 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
955 strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
956 if (TD_ON_LOCK(td)) {
957 kp->ki_kiflag |= KI_LOCKBLOCK;
958 strlcpy(kp->ki_lockname, td->td_lockname,
959 sizeof(kp->ki_lockname));
961 kp->ki_kiflag &= ~KI_LOCKBLOCK;
962 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
965 if (p->p_state == PRS_NORMAL) { /* approximate. */
966 if (TD_ON_RUNQ(td) ||
970 } else if (P_SHOULDSTOP(p)) {
972 } else if (TD_IS_SLEEPING(td)) {
973 kp->ki_stat = SSLEEP;
974 } else if (TD_ON_LOCK(td)) {
979 } else if (p->p_state == PRS_ZOMBIE) {
985 /* Things in the thread */
986 kp->ki_wchan = td->td_wchan;
987 kp->ki_pri.pri_level = td->td_priority;
988 kp->ki_pri.pri_native = td->td_base_pri;
991 * Note: legacy fields; clamp at the old NOCPU value and/or
992 * the maximum u_char CPU value.
994 if (td->td_lastcpu == NOCPU)
995 kp->ki_lastcpu_old = NOCPU_OLD;
996 else if (td->td_lastcpu > MAXCPU_OLD)
997 kp->ki_lastcpu_old = MAXCPU_OLD;
999 kp->ki_lastcpu_old = td->td_lastcpu;
1001 if (td->td_oncpu == NOCPU)
1002 kp->ki_oncpu_old = NOCPU_OLD;
1003 else if (td->td_oncpu > MAXCPU_OLD)
1004 kp->ki_oncpu_old = MAXCPU_OLD;
1006 kp->ki_oncpu_old = td->td_oncpu;
1008 kp->ki_lastcpu = td->td_lastcpu;
1009 kp->ki_oncpu = td->td_oncpu;
1010 kp->ki_tdflags = td->td_flags;
1011 kp->ki_tid = td->td_tid;
1012 kp->ki_numthreads = p->p_numthreads;
1013 kp->ki_pcb = td->td_pcb;
1014 kp->ki_kstack = (void *)td->td_kstack;
1015 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1016 kp->ki_pri.pri_class = td->td_pri_class;
1017 kp->ki_pri.pri_user = td->td_user_pri;
1020 rufetchtd(td, &kp->ki_rusage);
1021 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1022 kp->ki_pctcpu = sched_pctcpu(td);
1023 kp->ki_estcpu = td->td_estcpu;
1024 kp->ki_cow = td->td_cow;
1027 /* We can't get this anymore but ps etc never used it anyway. */
1031 kp->ki_siglist = td->td_siglist;
1032 kp->ki_sigmask = td->td_sigmask;
1039 * Fill in a kinfo_proc structure for the specified process.
1040 * Must be called with the target process locked.
1043 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1046 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1048 fill_kinfo_proc_only(p, kp);
1049 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1050 fill_kinfo_aggregate(p, kp);
1057 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1061 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1064 pstats_fork(struct pstats *src, struct pstats *dst)
1067 bzero(&dst->pstat_startzero,
1068 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1069 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1070 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1074 pstats_free(struct pstats *ps)
1077 free(ps, M_SUBPROC);
1080 static struct proc *
1081 zpfind_locked(pid_t pid)
1085 sx_assert(&allproc_lock, SX_LOCKED);
1086 LIST_FOREACH(p, &zombproc, p_list) {
1087 if (p->p_pid == pid) {
1096 * Locate a zombie process by number
1103 sx_slock(&allproc_lock);
1104 p = zpfind_locked(pid);
1105 sx_sunlock(&allproc_lock);
1109 #ifdef COMPAT_FREEBSD32
1112 * This function is typically used to copy out the kernel address, so
1113 * it can be replaced by assignment of zero.
1115 static inline uint32_t
1116 ptr32_trim(void *ptr)
1120 uptr = (uintptr_t)ptr;
1121 return ((uptr > UINT_MAX) ? 0 : uptr);
1124 #define PTRTRIM_CP(src,dst,fld) \
1125 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1128 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1132 bzero(ki32, sizeof(struct kinfo_proc32));
1133 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1134 CP(*ki, *ki32, ki_layout);
1135 PTRTRIM_CP(*ki, *ki32, ki_args);
1136 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1137 PTRTRIM_CP(*ki, *ki32, ki_addr);
1138 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1139 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1140 PTRTRIM_CP(*ki, *ki32, ki_fd);
1141 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1142 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1143 CP(*ki, *ki32, ki_pid);
1144 CP(*ki, *ki32, ki_ppid);
1145 CP(*ki, *ki32, ki_pgid);
1146 CP(*ki, *ki32, ki_tpgid);
1147 CP(*ki, *ki32, ki_sid);
1148 CP(*ki, *ki32, ki_tsid);
1149 CP(*ki, *ki32, ki_jobc);
1150 CP(*ki, *ki32, ki_tdev);
1151 CP(*ki, *ki32, ki_siglist);
1152 CP(*ki, *ki32, ki_sigmask);
1153 CP(*ki, *ki32, ki_sigignore);
1154 CP(*ki, *ki32, ki_sigcatch);
1155 CP(*ki, *ki32, ki_uid);
1156 CP(*ki, *ki32, ki_ruid);
1157 CP(*ki, *ki32, ki_svuid);
1158 CP(*ki, *ki32, ki_rgid);
1159 CP(*ki, *ki32, ki_svgid);
1160 CP(*ki, *ki32, ki_ngroups);
1161 for (i = 0; i < KI_NGROUPS; i++)
1162 CP(*ki, *ki32, ki_groups[i]);
1163 CP(*ki, *ki32, ki_size);
1164 CP(*ki, *ki32, ki_rssize);
1165 CP(*ki, *ki32, ki_swrss);
1166 CP(*ki, *ki32, ki_tsize);
1167 CP(*ki, *ki32, ki_dsize);
1168 CP(*ki, *ki32, ki_ssize);
1169 CP(*ki, *ki32, ki_xstat);
1170 CP(*ki, *ki32, ki_acflag);
1171 CP(*ki, *ki32, ki_pctcpu);
1172 CP(*ki, *ki32, ki_estcpu);
1173 CP(*ki, *ki32, ki_slptime);
1174 CP(*ki, *ki32, ki_swtime);
1175 CP(*ki, *ki32, ki_cow);
1176 CP(*ki, *ki32, ki_runtime);
1177 TV_CP(*ki, *ki32, ki_start);
1178 TV_CP(*ki, *ki32, ki_childtime);
1179 CP(*ki, *ki32, ki_flag);
1180 CP(*ki, *ki32, ki_kiflag);
1181 CP(*ki, *ki32, ki_traceflag);
1182 CP(*ki, *ki32, ki_stat);
1183 CP(*ki, *ki32, ki_nice);
1184 CP(*ki, *ki32, ki_lock);
1185 CP(*ki, *ki32, ki_rqindex);
1186 CP(*ki, *ki32, ki_oncpu);
1187 CP(*ki, *ki32, ki_lastcpu);
1189 /* XXX TODO: wrap cpu value as appropriate */
1190 CP(*ki, *ki32, ki_oncpu_old);
1191 CP(*ki, *ki32, ki_lastcpu_old);
1193 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1194 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1195 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1196 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1197 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1198 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1199 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1200 CP(*ki, *ki32, ki_tracer);
1201 CP(*ki, *ki32, ki_flag2);
1202 CP(*ki, *ki32, ki_fibnum);
1203 CP(*ki, *ki32, ki_cr_flags);
1204 CP(*ki, *ki32, ki_jid);
1205 CP(*ki, *ki32, ki_numthreads);
1206 CP(*ki, *ki32, ki_tid);
1207 CP(*ki, *ki32, ki_pri);
1208 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1209 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1210 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1211 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1212 PTRTRIM_CP(*ki, *ki32, ki_udata);
1213 CP(*ki, *ki32, ki_sflag);
1214 CP(*ki, *ki32, ki_tdflags);
1219 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1222 struct kinfo_proc ki;
1223 #ifdef COMPAT_FREEBSD32
1224 struct kinfo_proc32 ki32;
1228 PROC_LOCK_ASSERT(p, MA_OWNED);
1229 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1232 fill_kinfo_proc(p, &ki);
1233 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1234 #ifdef COMPAT_FREEBSD32
1235 if ((flags & KERN_PROC_MASK32) != 0) {
1236 freebsd32_kinfo_proc_out(&ki, &ki32);
1237 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1241 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1244 FOREACH_THREAD_IN_PROC(p, td) {
1245 fill_kinfo_thread(td, &ki, 1);
1246 #ifdef COMPAT_FREEBSD32
1247 if ((flags & KERN_PROC_MASK32) != 0) {
1248 freebsd32_kinfo_proc_out(&ki, &ki32);
1249 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1253 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1264 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1268 struct kinfo_proc ki;
1274 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1275 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1276 error = kern_proc_out(p, &sb, flags);
1277 error2 = sbuf_finish(&sb);
1281 else if (error2 != 0)
1301 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1303 int *name = (int *)arg1;
1304 u_int namelen = arg2;
1306 int flags, doingzomb, oid_number;
1309 oid_number = oidp->oid_number;
1310 if (oid_number != KERN_PROC_ALL &&
1311 (oid_number & KERN_PROC_INC_THREAD) == 0)
1312 flags = KERN_PROC_NOTHREADS;
1315 oid_number &= ~KERN_PROC_INC_THREAD;
1317 #ifdef COMPAT_FREEBSD32
1318 if (req->flags & SCTL_MASK32)
1319 flags |= KERN_PROC_MASK32;
1321 if (oid_number == KERN_PROC_PID) {
1324 error = sysctl_wire_old_buffer(req, 0);
1327 sx_slock(&proctree_lock);
1328 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1330 error = sysctl_out_proc(p, req, flags, 0);
1331 sx_sunlock(&proctree_lock);
1335 switch (oid_number) {
1340 case KERN_PROC_PROC:
1341 if (namelen != 0 && namelen != 1)
1351 /* overestimate by 5 procs */
1352 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1356 error = sysctl_wire_old_buffer(req, 0);
1359 sx_slock(&proctree_lock);
1360 sx_slock(&allproc_lock);
1361 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1363 p = LIST_FIRST(&allproc);
1365 p = LIST_FIRST(&zombproc);
1366 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1368 * Skip embryonic processes.
1371 if (p->p_state == PRS_NEW) {
1375 KASSERT(p->p_ucred != NULL,
1376 ("process credential is NULL for non-NEW proc"));
1378 * Show a user only appropriate processes.
1380 if (p_cansee(curthread, p)) {
1385 * TODO - make more efficient (see notes below).
1388 switch (oid_number) {
1391 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1397 case KERN_PROC_PGRP:
1398 /* could do this by traversing pgrp */
1399 if (p->p_pgrp == NULL ||
1400 p->p_pgrp->pg_id != (pid_t)name[0]) {
1406 case KERN_PROC_RGID:
1407 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1413 case KERN_PROC_SESSION:
1414 if (p->p_session == NULL ||
1415 p->p_session->s_sid != (pid_t)name[0]) {
1422 if ((p->p_flag & P_CONTROLT) == 0 ||
1423 p->p_session == NULL) {
1427 /* XXX proctree_lock */
1428 SESS_LOCK(p->p_session);
1429 if (p->p_session->s_ttyp == NULL ||
1430 tty_udev(p->p_session->s_ttyp) !=
1432 SESS_UNLOCK(p->p_session);
1436 SESS_UNLOCK(p->p_session);
1440 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1446 case KERN_PROC_RUID:
1447 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1453 case KERN_PROC_PROC:
1461 error = sysctl_out_proc(p, req, flags, doingzomb);
1463 sx_sunlock(&allproc_lock);
1464 sx_sunlock(&proctree_lock);
1469 sx_sunlock(&allproc_lock);
1470 sx_sunlock(&proctree_lock);
1475 pargs_alloc(int len)
1479 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1481 refcount_init(&pa->ar_ref, 1);
1482 pa->ar_length = len;
1487 pargs_free(struct pargs *pa)
1494 pargs_hold(struct pargs *pa)
1499 refcount_acquire(&pa->ar_ref);
1503 pargs_drop(struct pargs *pa)
1508 if (refcount_release(&pa->ar_ref))
1513 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1519 iov.iov_base = (caddr_t)buf;
1523 uio.uio_offset = offset;
1524 uio.uio_resid = (ssize_t)len;
1525 uio.uio_segflg = UIO_SYSSPACE;
1526 uio.uio_rw = UIO_READ;
1529 return (proc_rwmem(p, &uio));
1533 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1539 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1541 * Reading the chunk may validly return EFAULT if the string is shorter
1542 * than the chunk and is aligned at the end of the page, assuming the
1543 * next page is not mapped. So if EFAULT is returned do a fallback to
1544 * one byte read loop.
1546 if (error == EFAULT) {
1547 for (i = 0; i < len; i++, buf++, sptr++) {
1548 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1559 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1561 enum proc_vector_type {
1567 #ifdef COMPAT_FREEBSD32
1569 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1570 size_t *vsizep, enum proc_vector_type type)
1572 struct freebsd32_ps_strings pss;
1574 vm_offset_t vptr, ptr;
1575 uint32_t *proc_vector32;
1580 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1586 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1587 vsize = pss.ps_nargvstr;
1588 if (vsize > ARG_MAX)
1590 size = vsize * sizeof(int32_t);
1593 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1594 vsize = pss.ps_nenvstr;
1595 if (vsize > ARG_MAX)
1597 size = vsize * sizeof(int32_t);
1600 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1601 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1604 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1605 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1608 if (aux.a_type == AT_NULL)
1612 if (aux.a_type != AT_NULL)
1615 size = vsize * sizeof(aux);
1618 KASSERT(0, ("Wrong proc vector type: %d", type));
1621 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1622 error = proc_read_mem(td, p, vptr, proc_vector32, size);
1625 if (type == PROC_AUX) {
1626 *proc_vectorp = (char **)proc_vector32;
1630 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1631 for (i = 0; i < (int)vsize; i++)
1632 proc_vector[i] = PTRIN(proc_vector32[i]);
1633 *proc_vectorp = proc_vector;
1636 free(proc_vector32, M_TEMP);
1642 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1643 size_t *vsizep, enum proc_vector_type type)
1645 struct ps_strings pss;
1647 vm_offset_t vptr, ptr;
1652 #ifdef COMPAT_FREEBSD32
1653 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1654 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1656 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1662 vptr = (vm_offset_t)pss.ps_argvstr;
1663 vsize = pss.ps_nargvstr;
1664 if (vsize > ARG_MAX)
1666 size = vsize * sizeof(char *);
1669 vptr = (vm_offset_t)pss.ps_envstr;
1670 vsize = pss.ps_nenvstr;
1671 if (vsize > ARG_MAX)
1673 size = vsize * sizeof(char *);
1677 * The aux array is just above env array on the stack. Check
1678 * that the address is naturally aligned.
1680 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1682 #if __ELF_WORD_SIZE == 64
1683 if (vptr % sizeof(uint64_t) != 0)
1685 if (vptr % sizeof(uint32_t) != 0)
1689 * We count the array size reading the aux vectors from the
1690 * stack until AT_NULL vector is returned. So (to keep the code
1691 * simple) we read the process stack twice: the first time here
1692 * to find the size and the second time when copying the vectors
1693 * to the allocated proc_vector.
1695 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1696 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1699 if (aux.a_type == AT_NULL)
1704 * If the PROC_AUXV_MAX entries are iterated over, and we have
1705 * not reached AT_NULL, it is most likely we are reading wrong
1706 * data: either the process doesn't have auxv array or data has
1707 * been modified. Return the error in this case.
1709 if (aux.a_type != AT_NULL)
1712 size = vsize * sizeof(aux);
1715 KASSERT(0, ("Wrong proc vector type: %d", type));
1716 return (EINVAL); /* In case we are built without INVARIANTS. */
1718 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1719 if (proc_vector == NULL)
1721 error = proc_read_mem(td, p, vptr, proc_vector, size);
1723 free(proc_vector, M_TEMP);
1726 *proc_vectorp = proc_vector;
1732 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1735 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1736 enum proc_vector_type type)
1738 size_t done, len, nchr, vsize;
1740 char **proc_vector, *sptr;
1741 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1743 PROC_ASSERT_HELD(p);
1746 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1748 nchr = 2 * (PATH_MAX + ARG_MAX);
1750 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1753 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1755 * The program may have scribbled into its argv array, e.g. to
1756 * remove some arguments. If that has happened, break out
1757 * before trying to read from NULL.
1759 if (proc_vector[i] == NULL)
1761 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1762 error = proc_read_string(td, p, sptr, pss_string,
1763 sizeof(pss_string));
1766 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1767 if (done + len >= nchr)
1768 len = nchr - done - 1;
1769 sbuf_bcat(sb, pss_string, len);
1770 if (len != GET_PS_STRINGS_CHUNK_SZ)
1772 done += GET_PS_STRINGS_CHUNK_SZ;
1774 sbuf_bcat(sb, "", 1);
1778 free(proc_vector, M_TEMP);
1783 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1786 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1790 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1793 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1797 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1803 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1805 #ifdef COMPAT_FREEBSD32
1806 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1807 size = vsize * sizeof(Elf32_Auxinfo);
1810 size = vsize * sizeof(Elf_Auxinfo);
1811 if (sbuf_bcat(sb, auxv, size) != 0)
1819 * This sysctl allows a process to retrieve the argument list or process
1820 * title for another process without groping around in the address space
1821 * of the other process. It also allow a process to set its own "process
1822 * title to a string of its own choice.
1825 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1827 int *name = (int *)arg1;
1828 u_int namelen = arg2;
1829 struct pargs *newpa, *pa;
1832 int flags, error = 0, error2;
1837 flags = PGET_CANSEE;
1838 if (req->newptr != NULL)
1839 flags |= PGET_ISCURRENT;
1840 error = pget((pid_t)name[0], flags, &p);
1848 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1850 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1853 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1854 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1855 error = proc_getargv(curthread, p, &sb);
1856 error2 = sbuf_finish(&sb);
1859 if (error == 0 && error2 != 0)
1864 if (error != 0 || req->newptr == NULL)
1867 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1869 newpa = pargs_alloc(req->newlen);
1870 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1884 * This sysctl allows a process to retrieve environment of another process.
1887 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1889 int *name = (int *)arg1;
1890 u_int namelen = arg2;
1898 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1901 if ((p->p_flag & P_SYSTEM) != 0) {
1906 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1907 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1908 error = proc_getenvv(curthread, p, &sb);
1909 error2 = sbuf_finish(&sb);
1912 return (error != 0 ? error : error2);
1916 * This sysctl allows a process to retrieve ELF auxiliary vector of
1920 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1922 int *name = (int *)arg1;
1923 u_int namelen = arg2;
1931 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1934 if ((p->p_flag & P_SYSTEM) != 0) {
1938 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1939 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1940 error = proc_getauxv(curthread, p, &sb);
1941 error2 = sbuf_finish(&sb);
1944 return (error != 0 ? error : error2);
1948 * This sysctl allows a process to retrieve the path of the executable for
1949 * itself or another process.
1952 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1954 pid_t *pidp = (pid_t *)arg1;
1955 unsigned int arglen = arg2;
1958 char *retbuf, *freebuf;
1963 if (*pidp == -1) { /* -1 means this process */
1964 p = req->td->td_proc;
1966 error = pget(*pidp, PGET_CANSEE, &p);
1980 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1984 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1985 free(freebuf, M_TEMP);
1990 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2003 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2006 sv_name = p->p_sysent->sv_name;
2008 return (sysctl_handle_string(oidp, sv_name, 0, req));
2011 #ifdef KINFO_OVMENTRY_SIZE
2012 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2015 #ifdef COMPAT_FREEBSD7
2017 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2019 vm_map_entry_t entry, tmp_entry;
2020 unsigned int last_timestamp;
2021 char *fullpath, *freepath;
2022 struct kinfo_ovmentry *kve;
2032 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2035 vm = vmspace_acquire_ref(p);
2040 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2043 vm_map_lock_read(map);
2044 for (entry = map->header.next; entry != &map->header;
2045 entry = entry->next) {
2046 vm_object_t obj, tobj, lobj;
2049 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2052 bzero(kve, sizeof(*kve));
2053 kve->kve_structsize = sizeof(*kve);
2055 kve->kve_private_resident = 0;
2056 obj = entry->object.vm_object;
2058 VM_OBJECT_RLOCK(obj);
2059 if (obj->shadow_count == 1)
2060 kve->kve_private_resident =
2061 obj->resident_page_count;
2063 kve->kve_resident = 0;
2064 addr = entry->start;
2065 while (addr < entry->end) {
2066 if (pmap_extract(map->pmap, addr))
2067 kve->kve_resident++;
2071 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2073 VM_OBJECT_RLOCK(tobj);
2075 VM_OBJECT_RUNLOCK(lobj);
2079 kve->kve_start = (void*)entry->start;
2080 kve->kve_end = (void*)entry->end;
2081 kve->kve_offset = (off_t)entry->offset;
2083 if (entry->protection & VM_PROT_READ)
2084 kve->kve_protection |= KVME_PROT_READ;
2085 if (entry->protection & VM_PROT_WRITE)
2086 kve->kve_protection |= KVME_PROT_WRITE;
2087 if (entry->protection & VM_PROT_EXECUTE)
2088 kve->kve_protection |= KVME_PROT_EXEC;
2090 if (entry->eflags & MAP_ENTRY_COW)
2091 kve->kve_flags |= KVME_FLAG_COW;
2092 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2093 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2094 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2095 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2097 last_timestamp = map->timestamp;
2098 vm_map_unlock_read(map);
2100 kve->kve_fileid = 0;
2106 switch (lobj->type) {
2108 kve->kve_type = KVME_TYPE_DEFAULT;
2111 kve->kve_type = KVME_TYPE_VNODE;
2116 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2117 kve->kve_type = KVME_TYPE_VNODE;
2118 if ((lobj->flags & OBJ_TMPFS) != 0) {
2119 vp = lobj->un_pager.swp.swp_tmpfs;
2123 kve->kve_type = KVME_TYPE_SWAP;
2127 kve->kve_type = KVME_TYPE_DEVICE;
2130 kve->kve_type = KVME_TYPE_PHYS;
2133 kve->kve_type = KVME_TYPE_DEAD;
2136 kve->kve_type = KVME_TYPE_SG;
2139 kve->kve_type = KVME_TYPE_UNKNOWN;
2143 VM_OBJECT_RUNLOCK(lobj);
2145 kve->kve_ref_count = obj->ref_count;
2146 kve->kve_shadow_count = obj->shadow_count;
2147 VM_OBJECT_RUNLOCK(obj);
2149 vn_fullpath(curthread, vp, &fullpath,
2151 cred = curthread->td_ucred;
2152 vn_lock(vp, LK_SHARED | LK_RETRY);
2153 if (VOP_GETATTR(vp, &va, cred) == 0) {
2154 kve->kve_fileid = va.va_fileid;
2155 kve->kve_fsid = va.va_fsid;
2160 kve->kve_type = KVME_TYPE_NONE;
2161 kve->kve_ref_count = 0;
2162 kve->kve_shadow_count = 0;
2165 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2166 if (freepath != NULL)
2167 free(freepath, M_TEMP);
2169 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2170 vm_map_lock_read(map);
2173 if (last_timestamp != map->timestamp) {
2174 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2178 vm_map_unlock_read(map);
2184 #endif /* COMPAT_FREEBSD7 */
2186 #ifdef KINFO_VMENTRY_SIZE
2187 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2191 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2192 struct kinfo_vmentry *kve)
2194 vm_object_t obj, tobj;
2197 vm_paddr_t locked_pa;
2198 vm_pindex_t pi, pi_adv, pindex;
2201 obj = entry->object.vm_object;
2202 addr = entry->start;
2204 pi = OFF_TO_IDX(entry->offset);
2205 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2206 if (m_adv != NULL) {
2209 pi_adv = OFF_TO_IDX(entry->end - addr);
2211 for (tobj = obj;; tobj = tobj->backing_object) {
2212 m = vm_page_find_least(tobj, pindex);
2214 if (m->pindex == pindex)
2216 if (pi_adv > m->pindex - pindex) {
2217 pi_adv = m->pindex - pindex;
2221 if (tobj->backing_object == NULL)
2223 pindex += OFF_TO_IDX(tobj->
2224 backing_object_offset);
2228 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2229 (addr & (pagesizes[1] - 1)) == 0 &&
2230 (pmap_mincore(map->pmap, addr, &locked_pa) &
2231 MINCORE_SUPER) != 0) {
2232 kve->kve_flags |= KVME_FLAG_SUPER;
2233 pi_adv = OFF_TO_IDX(pagesizes[1]);
2236 * We do not test the found page on validity.
2237 * Either the page is busy and being paged in,
2238 * or it was invalidated. The first case
2239 * should be counted as resident, the second
2240 * is not so clear; we do account both.
2244 kve->kve_resident += pi_adv;
2247 PA_UNLOCK_COND(locked_pa);
2251 * Must be called with the process locked and will return unlocked.
2254 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb)
2256 vm_map_entry_t entry, tmp_entry;
2259 vm_object_t obj, tobj, lobj;
2260 char *fullpath, *freepath;
2261 struct kinfo_vmentry *kve;
2266 unsigned int last_timestamp;
2269 PROC_LOCK_ASSERT(p, MA_OWNED);
2273 vm = vmspace_acquire_ref(p);
2278 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2282 vm_map_lock_read(map);
2283 for (entry = map->header.next; entry != &map->header;
2284 entry = entry->next) {
2285 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2289 bzero(kve, sizeof(*kve));
2290 obj = entry->object.vm_object;
2292 for (tobj = obj; tobj != NULL;
2293 tobj = tobj->backing_object) {
2294 VM_OBJECT_RLOCK(tobj);
2297 if (obj->backing_object == NULL)
2298 kve->kve_private_resident =
2299 obj->resident_page_count;
2300 if (!vmmap_skip_res_cnt)
2301 kern_proc_vmmap_resident(map, entry, kve);
2302 for (tobj = obj; tobj != NULL;
2303 tobj = tobj->backing_object) {
2304 if (tobj != obj && tobj != lobj)
2305 VM_OBJECT_RUNLOCK(tobj);
2311 kve->kve_start = entry->start;
2312 kve->kve_end = entry->end;
2313 kve->kve_offset = entry->offset;
2315 if (entry->protection & VM_PROT_READ)
2316 kve->kve_protection |= KVME_PROT_READ;
2317 if (entry->protection & VM_PROT_WRITE)
2318 kve->kve_protection |= KVME_PROT_WRITE;
2319 if (entry->protection & VM_PROT_EXECUTE)
2320 kve->kve_protection |= KVME_PROT_EXEC;
2322 if (entry->eflags & MAP_ENTRY_COW)
2323 kve->kve_flags |= KVME_FLAG_COW;
2324 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2325 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2326 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2327 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2328 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2329 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2330 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2331 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2333 last_timestamp = map->timestamp;
2334 vm_map_unlock_read(map);
2340 switch (lobj->type) {
2342 kve->kve_type = KVME_TYPE_DEFAULT;
2345 kve->kve_type = KVME_TYPE_VNODE;
2350 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2351 kve->kve_type = KVME_TYPE_VNODE;
2352 if ((lobj->flags & OBJ_TMPFS) != 0) {
2353 vp = lobj->un_pager.swp.swp_tmpfs;
2357 kve->kve_type = KVME_TYPE_SWAP;
2361 kve->kve_type = KVME_TYPE_DEVICE;
2364 kve->kve_type = KVME_TYPE_PHYS;
2367 kve->kve_type = KVME_TYPE_DEAD;
2370 kve->kve_type = KVME_TYPE_SG;
2372 case OBJT_MGTDEVICE:
2373 kve->kve_type = KVME_TYPE_MGTDEVICE;
2376 kve->kve_type = KVME_TYPE_UNKNOWN;
2380 VM_OBJECT_RUNLOCK(lobj);
2382 kve->kve_ref_count = obj->ref_count;
2383 kve->kve_shadow_count = obj->shadow_count;
2384 VM_OBJECT_RUNLOCK(obj);
2386 vn_fullpath(curthread, vp, &fullpath,
2388 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2389 cred = curthread->td_ucred;
2390 vn_lock(vp, LK_SHARED | LK_RETRY);
2391 if (VOP_GETATTR(vp, &va, cred) == 0) {
2392 kve->kve_vn_fileid = va.va_fileid;
2393 kve->kve_vn_fsid = va.va_fsid;
2395 MAKEIMODE(va.va_type, va.va_mode);
2396 kve->kve_vn_size = va.va_size;
2397 kve->kve_vn_rdev = va.va_rdev;
2398 kve->kve_status = KF_ATTR_VALID;
2403 kve->kve_type = KVME_TYPE_NONE;
2404 kve->kve_ref_count = 0;
2405 kve->kve_shadow_count = 0;
2408 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2409 if (freepath != NULL)
2410 free(freepath, M_TEMP);
2412 /* Pack record size down */
2413 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
2414 strlen(kve->kve_path) + 1;
2415 kve->kve_structsize = roundup(kve->kve_structsize,
2417 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2419 vm_map_lock_read(map);
2422 if (last_timestamp != map->timestamp) {
2423 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2427 vm_map_unlock_read(map);
2435 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2439 int error, error2, *name;
2442 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2443 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2444 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2449 error = kern_proc_vmmap_out(p, &sb);
2450 error2 = sbuf_finish(&sb);
2452 return (error != 0 ? error : error2);
2455 #if defined(STACK) || defined(DDB)
2457 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2459 struct kinfo_kstack *kkstp;
2460 int error, i, *name, numthreads;
2461 lwpid_t *lwpidarray;
2468 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2472 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2473 st = stack_create();
2479 if (numthreads < p->p_numthreads) {
2480 if (lwpidarray != NULL) {
2481 free(lwpidarray, M_TEMP);
2484 numthreads = p->p_numthreads;
2486 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2494 * XXXRW: During the below loop, execve(2) and countless other sorts
2495 * of changes could have taken place. Should we check to see if the
2496 * vmspace has been replaced, or the like, in order to prevent
2497 * giving a snapshot that spans, say, execve(2), with some threads
2498 * before and some after? Among other things, the credentials could
2499 * have changed, in which case the right to extract debug info might
2500 * no longer be assured.
2502 FOREACH_THREAD_IN_PROC(p, td) {
2503 KASSERT(i < numthreads,
2504 ("sysctl_kern_proc_kstack: numthreads"));
2505 lwpidarray[i] = td->td_tid;
2509 for (i = 0; i < numthreads; i++) {
2510 td = thread_find(p, lwpidarray[i]);
2514 bzero(kkstp, sizeof(*kkstp));
2515 (void)sbuf_new(&sb, kkstp->kkst_trace,
2516 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2518 kkstp->kkst_tid = td->td_tid;
2519 if (TD_IS_SWAPPED(td))
2520 kkstp->kkst_state = KKST_STATE_SWAPPED;
2521 else if (TD_IS_RUNNING(td))
2522 kkstp->kkst_state = KKST_STATE_RUNNING;
2524 kkstp->kkst_state = KKST_STATE_STACKOK;
2525 stack_save_td(st, td);
2529 stack_sbuf_print(&sb, st);
2532 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2539 if (lwpidarray != NULL)
2540 free(lwpidarray, M_TEMP);
2542 free(kkstp, M_TEMP);
2548 * This sysctl allows a process to retrieve the full list of groups from
2549 * itself or another process.
2552 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2554 pid_t *pidp = (pid_t *)arg1;
2555 unsigned int arglen = arg2;
2562 if (*pidp == -1) { /* -1 means this process */
2563 p = req->td->td_proc;
2566 error = pget(*pidp, PGET_CANSEE, &p);
2571 cred = crhold(p->p_ucred);
2574 error = SYSCTL_OUT(req, cred->cr_groups,
2575 cred->cr_ngroups * sizeof(gid_t));
2581 * This sysctl allows a process to retrieve or/and set the resource limit for
2585 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2587 int *name = (int *)arg1;
2588 u_int namelen = arg2;
2597 which = (u_int)name[1];
2598 if (which >= RLIM_NLIMITS)
2601 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2604 flags = PGET_HOLD | PGET_NOTWEXIT;
2605 if (req->newptr != NULL)
2606 flags |= PGET_CANDEBUG;
2608 flags |= PGET_CANSEE;
2609 error = pget((pid_t)name[0], flags, &p);
2616 if (req->oldptr != NULL) {
2618 lim_rlimit_proc(p, which, &rlim);
2621 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2628 if (req->newptr != NULL) {
2629 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2631 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2640 * This sysctl allows a process to retrieve ps_strings structure location of
2644 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2646 int *name = (int *)arg1;
2647 u_int namelen = arg2;
2649 vm_offset_t ps_strings;
2651 #ifdef COMPAT_FREEBSD32
2652 uint32_t ps_strings32;
2658 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2661 #ifdef COMPAT_FREEBSD32
2662 if ((req->flags & SCTL_MASK32) != 0) {
2664 * We return 0 if the 32 bit emulation request is for a 64 bit
2667 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2668 PTROUT(p->p_sysent->sv_psstrings) : 0;
2670 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2674 ps_strings = p->p_sysent->sv_psstrings;
2676 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2681 * This sysctl allows a process to retrieve umask of another process.
2684 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2686 int *name = (int *)arg1;
2687 u_int namelen = arg2;
2695 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2699 FILEDESC_SLOCK(p->p_fd);
2700 fd_cmask = p->p_fd->fd_cmask;
2701 FILEDESC_SUNLOCK(p->p_fd);
2703 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2708 * This sysctl allows a process to set and retrieve binary osreldate of
2712 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2714 int *name = (int *)arg1;
2715 u_int namelen = arg2;
2717 int flags, error, osrel;
2722 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2725 flags = PGET_HOLD | PGET_NOTWEXIT;
2726 if (req->newptr != NULL)
2727 flags |= PGET_CANDEBUG;
2729 flags |= PGET_CANSEE;
2730 error = pget((pid_t)name[0], flags, &p);
2734 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2738 if (req->newptr != NULL) {
2739 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2754 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2756 int *name = (int *)arg1;
2757 u_int namelen = arg2;
2759 struct kinfo_sigtramp kst;
2760 const struct sysentvec *sv;
2762 #ifdef COMPAT_FREEBSD32
2763 struct kinfo_sigtramp32 kst32;
2769 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2773 #ifdef COMPAT_FREEBSD32
2774 if ((req->flags & SCTL_MASK32) != 0) {
2775 bzero(&kst32, sizeof(kst32));
2776 if (SV_PROC_FLAG(p, SV_ILP32)) {
2777 if (sv->sv_sigcode_base != 0) {
2778 kst32.ksigtramp_start = sv->sv_sigcode_base;
2779 kst32.ksigtramp_end = sv->sv_sigcode_base +
2782 kst32.ksigtramp_start = sv->sv_psstrings -
2784 kst32.ksigtramp_end = sv->sv_psstrings;
2788 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2792 bzero(&kst, sizeof(kst));
2793 if (sv->sv_sigcode_base != 0) {
2794 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2795 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2798 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2800 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2803 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2807 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2809 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2810 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2811 "Return entire process table");
2813 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2814 sysctl_kern_proc, "Process table");
2816 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2817 sysctl_kern_proc, "Process table");
2819 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2820 sysctl_kern_proc, "Process table");
2822 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2823 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2825 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2826 sysctl_kern_proc, "Process table");
2828 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2829 sysctl_kern_proc, "Process table");
2831 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2832 sysctl_kern_proc, "Process table");
2834 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2835 sysctl_kern_proc, "Process table");
2837 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2838 sysctl_kern_proc, "Return process table, no threads");
2840 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2841 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2842 sysctl_kern_proc_args, "Process argument list");
2844 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2845 sysctl_kern_proc_env, "Process environment");
2847 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2848 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2850 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2851 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2853 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2854 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2855 "Process syscall vector name (ABI type)");
2857 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2858 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2860 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2861 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2863 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2864 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2866 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2867 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2869 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2870 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2872 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2873 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2875 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2876 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2878 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2879 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2881 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2882 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2883 "Return process table, no threads");
2885 #ifdef COMPAT_FREEBSD7
2886 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2887 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2890 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2891 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2893 #if defined(STACK) || defined(DDB)
2894 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2895 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2898 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2899 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2901 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2902 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2903 "Process resource limits");
2905 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2906 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2907 "Process ps_strings location");
2909 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2910 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2912 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2913 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2914 "Process binary osreldate");
2916 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2917 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2918 "Process signal trampoline location");
2925 struct proc *cp, *p;
2927 bool restart, seen_stopped, seen_exiting, stopped_some;
2931 * stop_all_proc() assumes that all process which have
2932 * usermode must be stopped, except current process, for
2933 * obvious reasons. Since other threads in the process
2934 * establishing global stop could unstop something, disable
2935 * calls from multithreaded processes as precaution. The
2936 * service must not be user-callable anyway.
2938 KASSERT((cp->p_flag & P_HADTHREADS) == 0 ||
2939 (cp->p_flag & P_KTHREAD) != 0, ("mt stop_all_proc"));
2942 sx_xlock(&allproc_lock);
2944 seen_exiting = seen_stopped = stopped_some = restart = false;
2945 LIST_REMOVE(cp, p_list);
2946 LIST_INSERT_HEAD(&allproc, cp, p_list);
2948 p = LIST_NEXT(cp, p_list);
2951 LIST_REMOVE(cp, p_list);
2952 LIST_INSERT_AFTER(p, cp, p_list);
2954 if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
2955 P_TOTAL_STOP)) != 0) {
2959 if ((p->p_flag & P_WEXIT) != 0) {
2960 seen_exiting = true;
2964 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
2966 * Stopped processes are tolerated when there
2967 * are no other processes which might continue
2968 * them. P_STOPPED_SINGLE but not
2969 * P_TOTAL_STOP process still has at least one
2972 seen_stopped = true;
2977 sx_xunlock(&allproc_lock);
2978 r = thread_single(p, SINGLE_ALLPROC);
2982 stopped_some = true;
2985 sx_xlock(&allproc_lock);
2987 /* Catch forked children we did not see in iteration. */
2988 if (gen != allproc_gen)
2990 sx_xunlock(&allproc_lock);
2991 if (restart || stopped_some || seen_exiting || seen_stopped) {
2992 kern_yield(PRI_USER);
2998 resume_all_proc(void)
3000 struct proc *cp, *p;
3003 sx_xlock(&allproc_lock);
3004 LIST_REMOVE(cp, p_list);
3005 LIST_INSERT_HEAD(&allproc, cp, p_list);
3007 p = LIST_NEXT(cp, p_list);
3010 LIST_REMOVE(cp, p_list);
3011 LIST_INSERT_AFTER(p, cp, p_list);
3013 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3014 sx_xunlock(&allproc_lock);
3016 thread_single_end(p, SINGLE_ALLPROC);
3019 sx_xlock(&allproc_lock);
3024 sx_xunlock(&allproc_lock);
3027 #define TOTAL_STOP_DEBUG 1
3028 #ifdef TOTAL_STOP_DEBUG
3029 volatile static int ap_resume;
3030 #include <sys/mount.h>
3033 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3039 error = sysctl_handle_int(oidp, &val, 0, req);
3040 if (error != 0 || req->newptr == NULL)
3045 while (ap_resume == 0)
3053 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3054 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3055 sysctl_debug_stop_all_proc, "I",