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_kdtrace.h"
38 #include "opt_ktrace.h"
39 #include "opt_kstack_pages.h"
40 #include "opt_stack.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
49 #include <sys/loginclass.h>
50 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
55 #include <sys/ptrace.h>
56 #include <sys/refcount.h>
57 #include <sys/resourcevar.h>
58 #include <sys/rwlock.h>
60 #include <sys/sysent.h>
61 #include <sys/sched.h>
63 #include <sys/stack.h>
65 #include <sys/sysctl.h>
66 #include <sys/filedesc.h>
68 #include <sys/signalvar.h>
73 #include <sys/vnode.h>
74 #include <sys/eventhandler.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #ifdef COMPAT_FREEBSD32
90 #include <compat/freebsd32/freebsd32.h>
91 #include <compat/freebsd32/freebsd32_util.h>
94 SDT_PROVIDER_DEFINE(proc);
95 SDT_PROBE_DEFINE4(proc, , ctor, entry, "struct proc *", "int", "void *",
97 SDT_PROBE_DEFINE4(proc, , ctor, return, "struct proc *", "int", "void *",
99 SDT_PROBE_DEFINE4(proc, , dtor, entry, "struct proc *", "int", "void *",
101 SDT_PROBE_DEFINE3(proc, , dtor, return, "struct proc *", "int", "void *");
102 SDT_PROBE_DEFINE3(proc, , init, entry, "struct proc *", "int", "int");
103 SDT_PROBE_DEFINE3(proc, , init, return, "struct proc *", "int", "int");
105 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
106 MALLOC_DEFINE(M_SESSION, "session", "session header");
107 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
108 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
110 static void doenterpgrp(struct proc *, struct pgrp *);
111 static void orphanpg(struct pgrp *pg);
112 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
113 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
114 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
116 static void pgadjustjobc(struct pgrp *pgrp, int entering);
117 static void pgdelete(struct pgrp *);
118 static int proc_ctor(void *mem, int size, void *arg, int flags);
119 static void proc_dtor(void *mem, int size, void *arg);
120 static int proc_init(void *mem, int size, int flags);
121 static void proc_fini(void *mem, int size);
122 static void pargs_free(struct pargs *pa);
123 static struct proc *zpfind_locked(pid_t pid);
126 * Other process lists
128 struct pidhashhead *pidhashtbl;
130 struct pgrphashhead *pgrphashtbl;
132 struct proclist allproc;
133 struct proclist zombproc;
134 struct sx allproc_lock;
135 struct sx proctree_lock;
136 struct mtx ppeers_lock;
137 uma_zone_t proc_zone;
140 * The offset of various fields in struct proc and struct thread.
141 * These are used by kernel debuggers to enumerate kernel threads and
144 const int proc_off_p_pid = offsetof(struct proc, p_pid);
145 const int proc_off_p_comm = offsetof(struct proc, p_comm);
146 const int proc_off_p_list = offsetof(struct proc, p_list);
147 const int proc_off_p_threads = offsetof(struct proc, p_threads);
148 const int thread_off_td_tid = offsetof(struct thread, td_tid);
149 const int thread_off_td_name = offsetof(struct thread, td_name);
150 const int thread_off_td_oncpu = offsetof(struct thread, td_oncpu);
151 const int thread_off_td_pcb = offsetof(struct thread, td_pcb);
152 const int thread_off_td_plist = offsetof(struct thread, td_plist);
154 int kstack_pages = KSTACK_PAGES;
155 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
156 "Kernel stack size in pages");
157 static int vmmap_skip_res_cnt = 0;
158 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
159 &vmmap_skip_res_cnt, 0,
160 "Skip calculation of the pages resident count in kern.proc.vmmap");
162 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
163 #ifdef COMPAT_FREEBSD32
164 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
168 * Initialize global process hashing structures.
174 sx_init(&allproc_lock, "allproc");
175 sx_init(&proctree_lock, "proctree");
176 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
178 LIST_INIT(&zombproc);
179 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
180 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
181 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
182 proc_ctor, proc_dtor, proc_init, proc_fini,
183 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
188 * Prepare a proc for use.
191 proc_ctor(void *mem, int size, void *arg, int flags)
196 p = (struct proc *)mem;
197 SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags);
198 EVENTHANDLER_INVOKE(process_ctor, p);
199 SDT_PROBE4(proc, , ctor , return, p, size, arg, flags);
200 td = FIRST_THREAD_IN_PROC(p);
202 /* Make sure all thread constructors are executed */
203 EVENTHANDLER_INVOKE(thread_ctor, td);
209 * Reclaim a proc after use.
212 proc_dtor(void *mem, int size, void *arg)
217 /* INVARIANTS checks go here */
218 p = (struct proc *)mem;
219 td = FIRST_THREAD_IN_PROC(p);
220 SDT_PROBE4(proc, , dtor, entry, p, size, arg, td);
223 KASSERT((p->p_numthreads == 1),
224 ("bad number of threads in exiting process"));
225 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
227 /* Free all OSD associated to this thread. */
230 /* Make sure all thread destructors are executed */
231 EVENTHANDLER_INVOKE(thread_dtor, td);
233 EVENTHANDLER_INVOKE(process_dtor, p);
234 if (p->p_ksi != NULL)
235 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
236 SDT_PROBE3(proc, , dtor, return, p, size, arg);
240 * Initialize type-stable parts of a proc (when newly created).
243 proc_init(void *mem, int size, int flags)
247 p = (struct proc *)mem;
248 SDT_PROBE3(proc, , init, entry, p, size, flags);
249 p->p_sched = (struct p_sched *)&p[1];
250 bzero(&p->p_mtx, sizeof(struct mtx));
251 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
252 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
253 cv_init(&p->p_pwait, "ppwait");
254 cv_init(&p->p_dbgwait, "dbgwait");
255 TAILQ_INIT(&p->p_threads); /* all threads in proc */
256 EVENTHANDLER_INVOKE(process_init, p);
257 p->p_stats = pstats_alloc();
259 SDT_PROBE3(proc, , init, return, p, size, flags);
264 * UMA should ensure that this function is never called.
265 * Freeing a proc structure would violate type stability.
268 proc_fini(void *mem, int size)
273 p = (struct proc *)mem;
274 EVENTHANDLER_INVOKE(process_fini, p);
275 pstats_free(p->p_stats);
276 thread_free(FIRST_THREAD_IN_PROC(p));
277 mtx_destroy(&p->p_mtx);
278 if (p->p_ksi != NULL)
279 ksiginfo_free(p->p_ksi);
281 panic("proc reclaimed");
286 * Is p an inferior of the current process?
289 inferior(struct proc *p)
292 sx_assert(&proctree_lock, SX_LOCKED);
293 PROC_LOCK_ASSERT(p, MA_OWNED);
294 for (; p != curproc; p = proc_realparent(p)) {
302 pfind_locked(pid_t pid)
306 sx_assert(&allproc_lock, SX_LOCKED);
307 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
308 if (p->p_pid == pid) {
310 if (p->p_state == PRS_NEW) {
321 * Locate a process by number; return only "live" processes -- i.e., neither
322 * zombies nor newly born but incompletely initialized processes. By not
323 * returning processes in the PRS_NEW state, we allow callers to avoid
324 * testing for that condition to avoid dereferencing p_ucred, et al.
331 sx_slock(&allproc_lock);
332 p = pfind_locked(pid);
333 sx_sunlock(&allproc_lock);
338 pfind_tid_locked(pid_t tid)
343 sx_assert(&allproc_lock, SX_LOCKED);
344 FOREACH_PROC_IN_SYSTEM(p) {
346 if (p->p_state == PRS_NEW) {
350 FOREACH_THREAD_IN_PROC(p, td) {
351 if (td->td_tid == tid)
361 * Locate a process group by number.
362 * The caller must hold proctree_lock.
368 register struct pgrp *pgrp;
370 sx_assert(&proctree_lock, SX_LOCKED);
372 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
373 if (pgrp->pg_id == pgid) {
382 * Locate process and do additional manipulations, depending on flags.
385 pget(pid_t pid, int flags, struct proc **pp)
390 sx_slock(&allproc_lock);
391 if (pid <= PID_MAX) {
392 p = pfind_locked(pid);
393 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
394 p = zpfind_locked(pid);
395 } else if ((flags & PGET_NOTID) == 0) {
396 p = pfind_tid_locked(pid);
400 sx_sunlock(&allproc_lock);
403 if ((flags & PGET_CANSEE) != 0) {
404 error = p_cansee(curthread, p);
408 if ((flags & PGET_CANDEBUG) != 0) {
409 error = p_candebug(curthread, p);
413 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
417 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
421 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
423 * XXXRW: Not clear ESRCH is the right error during proc
429 if ((flags & PGET_HOLD) != 0) {
441 * Create a new process group.
442 * pgid must be equal to the pid of p.
443 * Begin a new session if required.
446 enterpgrp(p, pgid, pgrp, sess)
447 register struct proc *p;
450 struct session *sess;
453 sx_assert(&proctree_lock, SX_XLOCKED);
455 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
456 KASSERT(p->p_pid == pgid,
457 ("enterpgrp: new pgrp and pid != pgid"));
458 KASSERT(pgfind(pgid) == NULL,
459 ("enterpgrp: pgrp with pgid exists"));
460 KASSERT(!SESS_LEADER(p),
461 ("enterpgrp: session leader attempted setpgrp"));
463 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
469 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
471 p->p_flag &= ~P_CONTROLT;
475 sess->s_sid = p->p_pid;
476 refcount_init(&sess->s_count, 1);
477 sess->s_ttyvp = NULL;
478 sess->s_ttydp = NULL;
480 bcopy(p->p_session->s_login, sess->s_login,
481 sizeof(sess->s_login));
482 pgrp->pg_session = sess;
483 KASSERT(p == curproc,
484 ("enterpgrp: mksession and p != curproc"));
486 pgrp->pg_session = p->p_session;
487 sess_hold(pgrp->pg_session);
491 LIST_INIT(&pgrp->pg_members);
494 * As we have an exclusive lock of proctree_lock,
495 * this should not deadlock.
497 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
499 SLIST_INIT(&pgrp->pg_sigiolst);
502 doenterpgrp(p, pgrp);
508 * Move p to an existing process group
511 enterthispgrp(p, pgrp)
512 register struct proc *p;
516 sx_assert(&proctree_lock, SX_XLOCKED);
517 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
518 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
519 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
520 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
521 KASSERT(pgrp->pg_session == p->p_session,
522 ("%s: pgrp's session %p, p->p_session %p.\n",
526 KASSERT(pgrp != p->p_pgrp,
527 ("%s: p belongs to pgrp.", __func__));
529 doenterpgrp(p, pgrp);
535 * Move p to a process group
542 struct pgrp *savepgrp;
544 sx_assert(&proctree_lock, SX_XLOCKED);
545 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
546 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
547 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
548 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
550 savepgrp = p->p_pgrp;
553 * Adjust eligibility of affected pgrps to participate in job control.
554 * Increment eligibility counts before decrementing, otherwise we
555 * could reach 0 spuriously during the first call.
558 fixjobc(p, p->p_pgrp, 0);
563 LIST_REMOVE(p, p_pglist);
566 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
567 PGRP_UNLOCK(savepgrp);
569 if (LIST_EMPTY(&savepgrp->pg_members))
574 * remove process from process group
578 register struct proc *p;
580 struct pgrp *savepgrp;
582 sx_assert(&proctree_lock, SX_XLOCKED);
583 savepgrp = p->p_pgrp;
586 LIST_REMOVE(p, p_pglist);
589 PGRP_UNLOCK(savepgrp);
590 if (LIST_EMPTY(&savepgrp->pg_members))
596 * delete a process group
600 register struct pgrp *pgrp;
602 struct session *savesess;
605 sx_assert(&proctree_lock, SX_XLOCKED);
606 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
607 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
610 * Reset any sigio structures pointing to us as a result of
611 * F_SETOWN with our pgid.
613 funsetownlst(&pgrp->pg_sigiolst);
616 tp = pgrp->pg_session->s_ttyp;
617 LIST_REMOVE(pgrp, pg_hash);
618 savesess = pgrp->pg_session;
621 /* Remove the reference to the pgrp before deallocating it. */
624 tty_rel_pgrp(tp, pgrp);
627 mtx_destroy(&pgrp->pg_mtx);
629 sess_release(savesess);
633 pgadjustjobc(pgrp, entering)
643 if (pgrp->pg_jobc == 0)
650 * Adjust pgrp jobc counters when specified process changes process group.
651 * We count the number of processes in each process group that "qualify"
652 * the group for terminal job control (those with a parent in a different
653 * process group of the same session). If that count reaches zero, the
654 * process group becomes orphaned. Check both the specified process'
655 * process group and that of its children.
656 * entering == 0 => p is leaving specified group.
657 * entering == 1 => p is entering specified group.
660 fixjobc(p, pgrp, entering)
661 register struct proc *p;
662 register struct pgrp *pgrp;
665 register struct pgrp *hispgrp;
666 register struct session *mysession;
668 sx_assert(&proctree_lock, SX_LOCKED);
669 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
670 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
671 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
674 * Check p's parent to see whether p qualifies its own process
675 * group; if so, adjust count for p's process group.
677 mysession = pgrp->pg_session;
678 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
679 hispgrp->pg_session == mysession)
680 pgadjustjobc(pgrp, entering);
683 * Check this process' children to see whether they qualify
684 * their process groups; if so, adjust counts for children's
687 LIST_FOREACH(p, &p->p_children, p_sibling) {
689 if (hispgrp == pgrp ||
690 hispgrp->pg_session != mysession)
693 if (p->p_state == PRS_ZOMBIE) {
698 pgadjustjobc(hispgrp, entering);
703 * A process group has become orphaned;
704 * if there are any stopped processes in the group,
705 * hang-up all process in that group.
711 register struct proc *p;
713 PGRP_LOCK_ASSERT(pg, MA_OWNED);
715 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
717 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
719 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
721 kern_psignal(p, SIGHUP);
722 kern_psignal(p, SIGCONT);
732 sess_hold(struct session *s)
735 refcount_acquire(&s->s_count);
739 sess_release(struct session *s)
742 if (refcount_release(&s->s_count)) {
743 if (s->s_ttyp != NULL) {
745 tty_rel_sess(s->s_ttyp, s);
747 mtx_destroy(&s->s_mtx);
754 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
756 register struct pgrp *pgrp;
757 register struct proc *p;
760 for (i = 0; i <= pgrphash; i++) {
761 if (!LIST_EMPTY(&pgrphashtbl[i])) {
762 printf("\tindx %d\n", i);
763 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
765 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
766 (void *)pgrp, (long)pgrp->pg_id,
767 (void *)pgrp->pg_session,
768 pgrp->pg_session->s_count,
769 (void *)LIST_FIRST(&pgrp->pg_members));
770 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
771 printf("\t\tpid %ld addr %p pgrp %p\n",
772 (long)p->p_pid, (void *)p,
782 * Calculate the kinfo_proc members which contain process-wide
784 * Must be called with the target process locked.
787 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
791 PROC_LOCK_ASSERT(p, MA_OWNED);
795 FOREACH_THREAD_IN_PROC(p, td) {
797 kp->ki_pctcpu += sched_pctcpu(td);
798 kp->ki_estcpu += td->td_estcpu;
804 * Clear kinfo_proc and fill in any information that is common
805 * to all threads in the process.
806 * Must be called with the target process locked.
809 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
817 PROC_LOCK_ASSERT(p, MA_OWNED);
818 bzero(kp, sizeof(*kp));
820 kp->ki_structsize = sizeof(*kp);
822 kp->ki_addr =/* p->p_addr; */0; /* XXX */
823 kp->ki_args = p->p_args;
824 kp->ki_textvp = p->p_textvp;
826 kp->ki_tracep = p->p_tracevp;
827 kp->ki_traceflag = p->p_traceflag;
830 kp->ki_vmspace = p->p_vmspace;
831 kp->ki_flag = p->p_flag;
832 kp->ki_flag2 = p->p_flag2;
835 kp->ki_uid = cred->cr_uid;
836 kp->ki_ruid = cred->cr_ruid;
837 kp->ki_svuid = cred->cr_svuid;
839 if (cred->cr_flags & CRED_FLAG_CAPMODE)
840 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
841 /* XXX bde doesn't like KI_NGROUPS */
842 if (cred->cr_ngroups > KI_NGROUPS) {
843 kp->ki_ngroups = KI_NGROUPS;
844 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
846 kp->ki_ngroups = cred->cr_ngroups;
847 bcopy(cred->cr_groups, kp->ki_groups,
848 kp->ki_ngroups * sizeof(gid_t));
849 kp->ki_rgid = cred->cr_rgid;
850 kp->ki_svgid = cred->cr_svgid;
851 /* If jailed(cred), emulate the old P_JAILED flag. */
853 kp->ki_flag |= P_JAILED;
854 /* If inside the jail, use 0 as a jail ID. */
855 if (cred->cr_prison != curthread->td_ucred->cr_prison)
856 kp->ki_jid = cred->cr_prison->pr_id;
858 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
859 sizeof(kp->ki_loginclass));
863 mtx_lock(&ps->ps_mtx);
864 kp->ki_sigignore = ps->ps_sigignore;
865 kp->ki_sigcatch = ps->ps_sigcatch;
866 mtx_unlock(&ps->ps_mtx);
868 if (p->p_state != PRS_NEW &&
869 p->p_state != PRS_ZOMBIE &&
870 p->p_vmspace != NULL) {
871 struct vmspace *vm = p->p_vmspace;
873 kp->ki_size = vm->vm_map.size;
874 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
875 FOREACH_THREAD_IN_PROC(p, td0) {
876 if (!TD_IS_SWAPPED(td0))
877 kp->ki_rssize += td0->td_kstack_pages;
879 kp->ki_swrss = vm->vm_swrss;
880 kp->ki_tsize = vm->vm_tsize;
881 kp->ki_dsize = vm->vm_dsize;
882 kp->ki_ssize = vm->vm_ssize;
883 } else if (p->p_state == PRS_ZOMBIE)
885 if (kp->ki_flag & P_INMEM)
886 kp->ki_sflag = PS_INMEM;
889 /* Calculate legacy swtime as seconds since 'swtick'. */
890 kp->ki_swtime = (ticks - p->p_swtick) / hz;
891 kp->ki_pid = p->p_pid;
892 kp->ki_nice = p->p_nice;
893 kp->ki_fibnum = p->p_fibnum;
894 kp->ki_start = p->p_stats->p_start;
895 timevaladd(&kp->ki_start, &boottime);
897 rufetch(p, &kp->ki_rusage);
898 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
899 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
901 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
902 /* Some callers want child times in a single value. */
903 kp->ki_childtime = kp->ki_childstime;
904 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
906 FOREACH_THREAD_IN_PROC(p, td0)
907 kp->ki_cow += td0->td_cow;
911 kp->ki_pgid = p->p_pgrp->pg_id;
912 kp->ki_jobc = p->p_pgrp->pg_jobc;
913 sp = p->p_pgrp->pg_session;
916 kp->ki_sid = sp->s_sid;
918 strlcpy(kp->ki_login, sp->s_login,
919 sizeof(kp->ki_login));
921 kp->ki_kiflag |= KI_CTTY;
923 kp->ki_kiflag |= KI_SLEADER;
924 /* XXX proctree_lock */
929 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
930 kp->ki_tdev = tty_udev(tp);
931 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
933 kp->ki_tsid = tp->t_session->s_sid;
936 if (p->p_comm[0] != '\0')
937 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
938 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
939 p->p_sysent->sv_name[0] != '\0')
940 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
941 kp->ki_siglist = p->p_siglist;
942 kp->ki_xstat = p->p_xstat;
943 kp->ki_acflag = p->p_acflag;
944 kp->ki_lock = p->p_lock;
946 kp->ki_ppid = p->p_pptr->p_pid;
950 * Fill in information that is thread specific. Must be called with
951 * target process locked. If 'preferthread' is set, overwrite certain
952 * process-related fields that are maintained for both threads and
956 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
962 PROC_LOCK_ASSERT(p, MA_OWNED);
967 if (td->td_wmesg != NULL)
968 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
970 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
971 if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >=
972 sizeof(kp->ki_tdname)) {
973 strlcpy(kp->ki_moretdname,
974 td->td_name + sizeof(kp->ki_tdname) - 1,
975 sizeof(kp->ki_moretdname));
977 bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname));
979 if (TD_ON_LOCK(td)) {
980 kp->ki_kiflag |= KI_LOCKBLOCK;
981 strlcpy(kp->ki_lockname, td->td_lockname,
982 sizeof(kp->ki_lockname));
984 kp->ki_kiflag &= ~KI_LOCKBLOCK;
985 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
988 if (p->p_state == PRS_NORMAL) { /* approximate. */
989 if (TD_ON_RUNQ(td) ||
993 } else if (P_SHOULDSTOP(p)) {
995 } else if (TD_IS_SLEEPING(td)) {
996 kp->ki_stat = SSLEEP;
997 } else if (TD_ON_LOCK(td)) {
1000 kp->ki_stat = SWAIT;
1002 } else if (p->p_state == PRS_ZOMBIE) {
1003 kp->ki_stat = SZOMB;
1008 /* Things in the thread */
1009 kp->ki_wchan = td->td_wchan;
1010 kp->ki_pri.pri_level = td->td_priority;
1011 kp->ki_pri.pri_native = td->td_base_pri;
1012 kp->ki_lastcpu = td->td_lastcpu;
1013 kp->ki_oncpu = td->td_oncpu;
1014 kp->ki_tdflags = td->td_flags;
1015 kp->ki_tid = td->td_tid;
1016 kp->ki_numthreads = p->p_numthreads;
1017 kp->ki_pcb = td->td_pcb;
1018 kp->ki_kstack = (void *)td->td_kstack;
1019 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1020 kp->ki_pri.pri_class = td->td_pri_class;
1021 kp->ki_pri.pri_user = td->td_user_pri;
1024 rufetchtd(td, &kp->ki_rusage);
1025 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1026 kp->ki_pctcpu = sched_pctcpu(td);
1027 kp->ki_estcpu = td->td_estcpu;
1028 kp->ki_cow = td->td_cow;
1031 /* We can't get this anymore but ps etc never used it anyway. */
1035 kp->ki_siglist = td->td_siglist;
1036 kp->ki_sigmask = td->td_sigmask;
1043 * Fill in a kinfo_proc structure for the specified process.
1044 * Must be called with the target process locked.
1047 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1050 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1052 fill_kinfo_proc_only(p, kp);
1053 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1054 fill_kinfo_aggregate(p, kp);
1061 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1065 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1068 pstats_fork(struct pstats *src, struct pstats *dst)
1071 bzero(&dst->pstat_startzero,
1072 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1073 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1074 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1078 pstats_free(struct pstats *ps)
1081 free(ps, M_SUBPROC);
1084 static struct proc *
1085 zpfind_locked(pid_t pid)
1089 sx_assert(&allproc_lock, SX_LOCKED);
1090 LIST_FOREACH(p, &zombproc, p_list) {
1091 if (p->p_pid == pid) {
1100 * Locate a zombie process by number
1107 sx_slock(&allproc_lock);
1108 p = zpfind_locked(pid);
1109 sx_sunlock(&allproc_lock);
1113 #ifdef COMPAT_FREEBSD32
1116 * This function is typically used to copy out the kernel address, so
1117 * it can be replaced by assignment of zero.
1119 static inline uint32_t
1120 ptr32_trim(void *ptr)
1124 uptr = (uintptr_t)ptr;
1125 return ((uptr > UINT_MAX) ? 0 : uptr);
1128 #define PTRTRIM_CP(src,dst,fld) \
1129 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1132 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1136 bzero(ki32, sizeof(struct kinfo_proc32));
1137 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1138 CP(*ki, *ki32, ki_layout);
1139 PTRTRIM_CP(*ki, *ki32, ki_args);
1140 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1141 PTRTRIM_CP(*ki, *ki32, ki_addr);
1142 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1143 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1144 PTRTRIM_CP(*ki, *ki32, ki_fd);
1145 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1146 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1147 CP(*ki, *ki32, ki_pid);
1148 CP(*ki, *ki32, ki_ppid);
1149 CP(*ki, *ki32, ki_pgid);
1150 CP(*ki, *ki32, ki_tpgid);
1151 CP(*ki, *ki32, ki_sid);
1152 CP(*ki, *ki32, ki_tsid);
1153 CP(*ki, *ki32, ki_jobc);
1154 CP(*ki, *ki32, ki_tdev);
1155 CP(*ki, *ki32, ki_siglist);
1156 CP(*ki, *ki32, ki_sigmask);
1157 CP(*ki, *ki32, ki_sigignore);
1158 CP(*ki, *ki32, ki_sigcatch);
1159 CP(*ki, *ki32, ki_uid);
1160 CP(*ki, *ki32, ki_ruid);
1161 CP(*ki, *ki32, ki_svuid);
1162 CP(*ki, *ki32, ki_rgid);
1163 CP(*ki, *ki32, ki_svgid);
1164 CP(*ki, *ki32, ki_ngroups);
1165 for (i = 0; i < KI_NGROUPS; i++)
1166 CP(*ki, *ki32, ki_groups[i]);
1167 CP(*ki, *ki32, ki_size);
1168 CP(*ki, *ki32, ki_rssize);
1169 CP(*ki, *ki32, ki_swrss);
1170 CP(*ki, *ki32, ki_tsize);
1171 CP(*ki, *ki32, ki_dsize);
1172 CP(*ki, *ki32, ki_ssize);
1173 CP(*ki, *ki32, ki_xstat);
1174 CP(*ki, *ki32, ki_acflag);
1175 CP(*ki, *ki32, ki_pctcpu);
1176 CP(*ki, *ki32, ki_estcpu);
1177 CP(*ki, *ki32, ki_slptime);
1178 CP(*ki, *ki32, ki_swtime);
1179 CP(*ki, *ki32, ki_cow);
1180 CP(*ki, *ki32, ki_runtime);
1181 TV_CP(*ki, *ki32, ki_start);
1182 TV_CP(*ki, *ki32, ki_childtime);
1183 CP(*ki, *ki32, ki_flag);
1184 CP(*ki, *ki32, ki_kiflag);
1185 CP(*ki, *ki32, ki_traceflag);
1186 CP(*ki, *ki32, ki_stat);
1187 CP(*ki, *ki32, ki_nice);
1188 CP(*ki, *ki32, ki_lock);
1189 CP(*ki, *ki32, ki_rqindex);
1190 CP(*ki, *ki32, ki_oncpu);
1191 CP(*ki, *ki32, ki_lastcpu);
1192 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1193 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1194 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1195 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1196 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1197 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1198 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1199 bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1);
1200 CP(*ki, *ki32, ki_flag2);
1201 CP(*ki, *ki32, ki_fibnum);
1202 CP(*ki, *ki32, ki_cr_flags);
1203 CP(*ki, *ki32, ki_jid);
1204 CP(*ki, *ki32, ki_numthreads);
1205 CP(*ki, *ki32, ki_tid);
1206 CP(*ki, *ki32, ki_pri);
1207 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1208 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1209 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1210 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1211 PTRTRIM_CP(*ki, *ki32, ki_udata);
1212 PTRTRIM_CP(*ki, *ki32, ki_tdaddr);
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 error = kern_proc_out(p, &sb, flags);
1276 error2 = sbuf_finish(&sb);
1280 else if (error2 != 0)
1300 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1302 int *name = (int *)arg1;
1303 u_int namelen = arg2;
1305 int flags, doingzomb, oid_number;
1308 oid_number = oidp->oid_number;
1309 if (oid_number != KERN_PROC_ALL &&
1310 (oid_number & KERN_PROC_INC_THREAD) == 0)
1311 flags = KERN_PROC_NOTHREADS;
1314 oid_number &= ~KERN_PROC_INC_THREAD;
1316 #ifdef COMPAT_FREEBSD32
1317 if (req->flags & SCTL_MASK32)
1318 flags |= KERN_PROC_MASK32;
1320 if (oid_number == KERN_PROC_PID) {
1323 error = sysctl_wire_old_buffer(req, 0);
1326 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1329 error = sysctl_out_proc(p, req, flags, 0);
1333 switch (oid_number) {
1338 case KERN_PROC_PROC:
1339 if (namelen != 0 && namelen != 1)
1349 /* overestimate by 5 procs */
1350 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1354 error = sysctl_wire_old_buffer(req, 0);
1357 sx_slock(&allproc_lock);
1358 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1360 p = LIST_FIRST(&allproc);
1362 p = LIST_FIRST(&zombproc);
1363 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1365 * Skip embryonic processes.
1368 if (p->p_state == PRS_NEW) {
1372 KASSERT(p->p_ucred != NULL,
1373 ("process credential is NULL for non-NEW proc"));
1375 * Show a user only appropriate processes.
1377 if (p_cansee(curthread, p)) {
1382 * TODO - make more efficient (see notes below).
1385 switch (oid_number) {
1388 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1394 case KERN_PROC_PGRP:
1395 /* could do this by traversing pgrp */
1396 if (p->p_pgrp == NULL ||
1397 p->p_pgrp->pg_id != (pid_t)name[0]) {
1403 case KERN_PROC_RGID:
1404 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1410 case KERN_PROC_SESSION:
1411 if (p->p_session == NULL ||
1412 p->p_session->s_sid != (pid_t)name[0]) {
1419 if ((p->p_flag & P_CONTROLT) == 0 ||
1420 p->p_session == NULL) {
1424 /* XXX proctree_lock */
1425 SESS_LOCK(p->p_session);
1426 if (p->p_session->s_ttyp == NULL ||
1427 tty_udev(p->p_session->s_ttyp) !=
1429 SESS_UNLOCK(p->p_session);
1433 SESS_UNLOCK(p->p_session);
1437 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1443 case KERN_PROC_RUID:
1444 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1450 case KERN_PROC_PROC:
1458 error = sysctl_out_proc(p, req, flags, doingzomb);
1460 sx_sunlock(&allproc_lock);
1465 sx_sunlock(&allproc_lock);
1470 pargs_alloc(int len)
1474 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1476 refcount_init(&pa->ar_ref, 1);
1477 pa->ar_length = len;
1482 pargs_free(struct pargs *pa)
1489 pargs_hold(struct pargs *pa)
1494 refcount_acquire(&pa->ar_ref);
1498 pargs_drop(struct pargs *pa)
1503 if (refcount_release(&pa->ar_ref))
1508 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1514 iov.iov_base = (caddr_t)buf;
1518 uio.uio_offset = offset;
1519 uio.uio_resid = (ssize_t)len;
1520 uio.uio_segflg = UIO_SYSSPACE;
1521 uio.uio_rw = UIO_READ;
1524 return (proc_rwmem(p, &uio));
1528 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1534 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1536 * Reading the chunk may validly return EFAULT if the string is shorter
1537 * than the chunk and is aligned at the end of the page, assuming the
1538 * next page is not mapped. So if EFAULT is returned do a fallback to
1539 * one byte read loop.
1541 if (error == EFAULT) {
1542 for (i = 0; i < len; i++, buf++, sptr++) {
1543 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1554 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1556 enum proc_vector_type {
1562 #ifdef COMPAT_FREEBSD32
1564 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1565 size_t *vsizep, enum proc_vector_type type)
1567 struct freebsd32_ps_strings pss;
1569 vm_offset_t vptr, ptr;
1570 uint32_t *proc_vector32;
1575 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1581 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1582 vsize = pss.ps_nargvstr;
1583 if (vsize > ARG_MAX)
1585 size = vsize * sizeof(int32_t);
1588 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1589 vsize = pss.ps_nenvstr;
1590 if (vsize > ARG_MAX)
1592 size = vsize * sizeof(int32_t);
1595 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1596 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1599 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1600 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1603 if (aux.a_type == AT_NULL)
1607 if (aux.a_type != AT_NULL)
1610 size = vsize * sizeof(aux);
1613 KASSERT(0, ("Wrong proc vector type: %d", type));
1616 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1617 error = proc_read_mem(td, p, vptr, proc_vector32, size);
1620 if (type == PROC_AUX) {
1621 *proc_vectorp = (char **)proc_vector32;
1625 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1626 for (i = 0; i < (int)vsize; i++)
1627 proc_vector[i] = PTRIN(proc_vector32[i]);
1628 *proc_vectorp = proc_vector;
1631 free(proc_vector32, M_TEMP);
1637 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1638 size_t *vsizep, enum proc_vector_type type)
1640 struct ps_strings pss;
1642 vm_offset_t vptr, ptr;
1647 #ifdef COMPAT_FREEBSD32
1648 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1649 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1651 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1657 vptr = (vm_offset_t)pss.ps_argvstr;
1658 vsize = pss.ps_nargvstr;
1659 if (vsize > ARG_MAX)
1661 size = vsize * sizeof(char *);
1664 vptr = (vm_offset_t)pss.ps_envstr;
1665 vsize = pss.ps_nenvstr;
1666 if (vsize > ARG_MAX)
1668 size = vsize * sizeof(char *);
1672 * The aux array is just above env array on the stack. Check
1673 * that the address is naturally aligned.
1675 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1677 #if __ELF_WORD_SIZE == 64
1678 if (vptr % sizeof(uint64_t) != 0)
1680 if (vptr % sizeof(uint32_t) != 0)
1684 * We count the array size reading the aux vectors from the
1685 * stack until AT_NULL vector is returned. So (to keep the code
1686 * simple) we read the process stack twice: the first time here
1687 * to find the size and the second time when copying the vectors
1688 * to the allocated proc_vector.
1690 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1691 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1694 if (aux.a_type == AT_NULL)
1699 * If the PROC_AUXV_MAX entries are iterated over, and we have
1700 * not reached AT_NULL, it is most likely we are reading wrong
1701 * data: either the process doesn't have auxv array or data has
1702 * been modified. Return the error in this case.
1704 if (aux.a_type != AT_NULL)
1707 size = vsize * sizeof(aux);
1710 KASSERT(0, ("Wrong proc vector type: %d", type));
1711 return (EINVAL); /* In case we are built without INVARIANTS. */
1713 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1714 if (proc_vector == NULL)
1716 error = proc_read_mem(td, p, vptr, proc_vector, size);
1718 free(proc_vector, M_TEMP);
1721 *proc_vectorp = proc_vector;
1727 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1730 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1731 enum proc_vector_type type)
1733 size_t done, len, nchr, vsize;
1735 char **proc_vector, *sptr;
1736 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1738 PROC_ASSERT_HELD(p);
1741 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1743 nchr = 2 * (PATH_MAX + ARG_MAX);
1745 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1748 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1750 * The program may have scribbled into its argv array, e.g. to
1751 * remove some arguments. If that has happened, break out
1752 * before trying to read from NULL.
1754 if (proc_vector[i] == NULL)
1756 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1757 error = proc_read_string(td, p, sptr, pss_string,
1758 sizeof(pss_string));
1761 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1762 if (done + len >= nchr)
1763 len = nchr - done - 1;
1764 sbuf_bcat(sb, pss_string, len);
1765 if (len != GET_PS_STRINGS_CHUNK_SZ)
1767 done += GET_PS_STRINGS_CHUNK_SZ;
1769 sbuf_bcat(sb, "", 1);
1773 free(proc_vector, M_TEMP);
1778 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1781 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1785 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1788 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1792 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1798 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1800 #ifdef COMPAT_FREEBSD32
1801 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1802 size = vsize * sizeof(Elf32_Auxinfo);
1805 size = vsize * sizeof(Elf_Auxinfo);
1806 if (sbuf_bcat(sb, auxv, size) != 0)
1814 * This sysctl allows a process to retrieve the argument list or process
1815 * title for another process without groping around in the address space
1816 * of the other process. It also allow a process to set its own "process
1817 * title to a string of its own choice.
1820 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1822 int *name = (int *)arg1;
1823 u_int namelen = arg2;
1824 struct pargs *newpa, *pa;
1827 int flags, error = 0, error2;
1832 flags = PGET_CANSEE;
1833 if (req->newptr != NULL)
1834 flags |= PGET_ISCURRENT;
1835 error = pget((pid_t)name[0], flags, &p);
1843 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1845 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1848 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1849 error = proc_getargv(curthread, p, &sb);
1850 error2 = sbuf_finish(&sb);
1853 if (error == 0 && error2 != 0)
1858 if (error != 0 || req->newptr == NULL)
1861 if (req->newlen > ps_arg_cache_limit - sizeof(struct pargs))
1863 newpa = pargs_alloc(req->newlen);
1864 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1878 * This sysctl allows a process to retrieve environment of another process.
1881 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1883 int *name = (int *)arg1;
1884 u_int namelen = arg2;
1892 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1895 if ((p->p_flag & P_SYSTEM) != 0) {
1900 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1901 error = proc_getenvv(curthread, p, &sb);
1902 error2 = sbuf_finish(&sb);
1905 return (error != 0 ? error : error2);
1909 * This sysctl allows a process to retrieve ELF auxiliary vector of
1913 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1915 int *name = (int *)arg1;
1916 u_int namelen = arg2;
1924 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1927 if ((p->p_flag & P_SYSTEM) != 0) {
1931 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1932 error = proc_getauxv(curthread, p, &sb);
1933 error2 = sbuf_finish(&sb);
1936 return (error != 0 ? error : error2);
1940 * This sysctl allows a process to retrieve the path of the executable for
1941 * itself or another process.
1944 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1946 pid_t *pidp = (pid_t *)arg1;
1947 unsigned int arglen = arg2;
1950 char *retbuf, *freebuf;
1955 if (*pidp == -1) { /* -1 means this process */
1956 p = req->td->td_proc;
1958 error = pget(*pidp, PGET_CANSEE, &p);
1972 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1976 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1977 free(freebuf, M_TEMP);
1982 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
1995 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1998 sv_name = p->p_sysent->sv_name;
2000 return (sysctl_handle_string(oidp, sv_name, 0, req));
2003 #ifdef KINFO_OVMENTRY_SIZE
2004 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2007 #ifdef COMPAT_FREEBSD7
2009 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2011 vm_map_entry_t entry, tmp_entry;
2012 unsigned int last_timestamp;
2013 char *fullpath, *freepath;
2014 struct kinfo_ovmentry *kve;
2024 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2027 vm = vmspace_acquire_ref(p);
2032 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2035 vm_map_lock_read(map);
2036 for (entry = map->header.next; entry != &map->header;
2037 entry = entry->next) {
2038 vm_object_t obj, tobj, lobj;
2041 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2044 bzero(kve, sizeof(*kve));
2045 kve->kve_structsize = sizeof(*kve);
2047 kve->kve_private_resident = 0;
2048 obj = entry->object.vm_object;
2050 VM_OBJECT_RLOCK(obj);
2051 if (obj->shadow_count == 1)
2052 kve->kve_private_resident =
2053 obj->resident_page_count;
2055 kve->kve_resident = 0;
2056 addr = entry->start;
2057 while (addr < entry->end) {
2058 if (pmap_extract(map->pmap, addr))
2059 kve->kve_resident++;
2063 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2065 VM_OBJECT_RLOCK(tobj);
2066 kve->kve_offset += tobj->backing_object_offset;
2069 VM_OBJECT_RUNLOCK(lobj);
2073 kve->kve_start = (void*)entry->start;
2074 kve->kve_end = (void*)entry->end;
2075 kve->kve_offset += (off_t)entry->offset;
2077 if (entry->protection & VM_PROT_READ)
2078 kve->kve_protection |= KVME_PROT_READ;
2079 if (entry->protection & VM_PROT_WRITE)
2080 kve->kve_protection |= KVME_PROT_WRITE;
2081 if (entry->protection & VM_PROT_EXECUTE)
2082 kve->kve_protection |= KVME_PROT_EXEC;
2084 if (entry->eflags & MAP_ENTRY_COW)
2085 kve->kve_flags |= KVME_FLAG_COW;
2086 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2087 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2088 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2089 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2091 last_timestamp = map->timestamp;
2092 vm_map_unlock_read(map);
2094 kve->kve_fileid = 0;
2100 switch (lobj->type) {
2102 kve->kve_type = KVME_TYPE_DEFAULT;
2105 kve->kve_type = KVME_TYPE_VNODE;
2110 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2111 kve->kve_type = KVME_TYPE_VNODE;
2112 if ((lobj->flags & OBJ_TMPFS) != 0) {
2113 vp = lobj->un_pager.swp.swp_tmpfs;
2117 kve->kve_type = KVME_TYPE_SWAP;
2121 kve->kve_type = KVME_TYPE_DEVICE;
2124 kve->kve_type = KVME_TYPE_PHYS;
2127 kve->kve_type = KVME_TYPE_DEAD;
2130 kve->kve_type = KVME_TYPE_SG;
2133 kve->kve_type = KVME_TYPE_UNKNOWN;
2137 VM_OBJECT_RUNLOCK(lobj);
2139 kve->kve_ref_count = obj->ref_count;
2140 kve->kve_shadow_count = obj->shadow_count;
2141 VM_OBJECT_RUNLOCK(obj);
2143 vn_fullpath(curthread, vp, &fullpath,
2145 cred = curthread->td_ucred;
2146 vn_lock(vp, LK_SHARED | LK_RETRY);
2147 if (VOP_GETATTR(vp, &va, cred) == 0) {
2148 kve->kve_fileid = va.va_fileid;
2149 kve->kve_fsid = va.va_fsid;
2154 kve->kve_type = KVME_TYPE_NONE;
2155 kve->kve_ref_count = 0;
2156 kve->kve_shadow_count = 0;
2159 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2160 if (freepath != NULL)
2161 free(freepath, M_TEMP);
2163 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2164 vm_map_lock_read(map);
2167 if (last_timestamp != map->timestamp) {
2168 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2172 vm_map_unlock_read(map);
2178 #endif /* COMPAT_FREEBSD7 */
2180 #ifdef KINFO_VMENTRY_SIZE
2181 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2185 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2186 struct kinfo_vmentry *kve)
2188 vm_object_t obj, tobj;
2191 vm_paddr_t locked_pa;
2192 vm_pindex_t pi, pi_adv, pindex;
2195 obj = entry->object.vm_object;
2196 addr = entry->start;
2198 pi = OFF_TO_IDX(entry->offset);
2199 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2200 if (m_adv != NULL) {
2203 pi_adv = OFF_TO_IDX(entry->end - addr);
2205 for (tobj = obj;; tobj = tobj->backing_object) {
2206 m = vm_page_find_least(tobj, pindex);
2208 if (m->pindex == pindex)
2210 if (pi_adv > m->pindex - pindex) {
2211 pi_adv = m->pindex - pindex;
2215 if (tobj->backing_object == NULL)
2217 pindex += OFF_TO_IDX(tobj->
2218 backing_object_offset);
2222 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2223 (addr & (pagesizes[1] - 1)) == 0 &&
2224 (pmap_mincore(map->pmap, addr, &locked_pa) &
2225 MINCORE_SUPER) != 0) {
2226 kve->kve_flags |= KVME_FLAG_SUPER;
2227 pi_adv = OFF_TO_IDX(pagesizes[1]);
2230 * We do not test the found page on validity.
2231 * Either the page is busy and being paged in,
2232 * or it was invalidated. The first case
2233 * should be counted as resident, the second
2234 * is not so clear; we do account both.
2238 kve->kve_resident += pi_adv;
2241 PA_UNLOCK_COND(locked_pa);
2245 * Must be called with the process locked and will return unlocked.
2248 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
2250 vm_map_entry_t entry, tmp_entry;
2253 vm_object_t obj, tobj, lobj;
2254 char *fullpath, *freepath;
2255 struct kinfo_vmentry *kve;
2260 unsigned int last_timestamp;
2263 PROC_LOCK_ASSERT(p, MA_OWNED);
2267 vm = vmspace_acquire_ref(p);
2272 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK | M_ZERO);
2276 vm_map_lock_read(map);
2277 for (entry = map->header.next; entry != &map->header;
2278 entry = entry->next) {
2279 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2283 bzero(kve, sizeof(*kve));
2284 obj = entry->object.vm_object;
2286 for (tobj = obj; tobj != NULL;
2287 tobj = tobj->backing_object) {
2288 VM_OBJECT_RLOCK(tobj);
2289 kve->kve_offset += tobj->backing_object_offset;
2292 if (obj->backing_object == NULL)
2293 kve->kve_private_resident =
2294 obj->resident_page_count;
2295 if (!vmmap_skip_res_cnt)
2296 kern_proc_vmmap_resident(map, entry, kve);
2297 for (tobj = obj; tobj != NULL;
2298 tobj = tobj->backing_object) {
2299 if (tobj != obj && tobj != lobj)
2300 VM_OBJECT_RUNLOCK(tobj);
2306 kve->kve_start = entry->start;
2307 kve->kve_end = entry->end;
2308 kve->kve_offset += entry->offset;
2310 if (entry->protection & VM_PROT_READ)
2311 kve->kve_protection |= KVME_PROT_READ;
2312 if (entry->protection & VM_PROT_WRITE)
2313 kve->kve_protection |= KVME_PROT_WRITE;
2314 if (entry->protection & VM_PROT_EXECUTE)
2315 kve->kve_protection |= KVME_PROT_EXEC;
2317 if (entry->eflags & MAP_ENTRY_COW)
2318 kve->kve_flags |= KVME_FLAG_COW;
2319 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2320 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2321 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2322 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2323 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2324 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2325 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2326 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2328 last_timestamp = map->timestamp;
2329 vm_map_unlock_read(map);
2335 switch (lobj->type) {
2337 kve->kve_type = KVME_TYPE_DEFAULT;
2340 kve->kve_type = KVME_TYPE_VNODE;
2345 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2346 kve->kve_type = KVME_TYPE_VNODE;
2347 if ((lobj->flags & OBJ_TMPFS) != 0) {
2348 vp = lobj->un_pager.swp.swp_tmpfs;
2352 kve->kve_type = KVME_TYPE_SWAP;
2356 kve->kve_type = KVME_TYPE_DEVICE;
2359 kve->kve_type = KVME_TYPE_PHYS;
2362 kve->kve_type = KVME_TYPE_DEAD;
2365 kve->kve_type = KVME_TYPE_SG;
2367 case OBJT_MGTDEVICE:
2368 kve->kve_type = KVME_TYPE_MGTDEVICE;
2371 kve->kve_type = KVME_TYPE_UNKNOWN;
2375 VM_OBJECT_RUNLOCK(lobj);
2377 kve->kve_ref_count = obj->ref_count;
2378 kve->kve_shadow_count = obj->shadow_count;
2379 VM_OBJECT_RUNLOCK(obj);
2381 vn_fullpath(curthread, vp, &fullpath,
2383 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2384 cred = curthread->td_ucred;
2385 vn_lock(vp, LK_SHARED | LK_RETRY);
2386 if (VOP_GETATTR(vp, &va, cred) == 0) {
2387 kve->kve_vn_fileid = va.va_fileid;
2388 kve->kve_vn_fsid = va.va_fsid;
2390 MAKEIMODE(va.va_type, va.va_mode);
2391 kve->kve_vn_size = va.va_size;
2392 kve->kve_vn_rdev = va.va_rdev;
2393 kve->kve_status = KF_ATTR_VALID;
2398 kve->kve_type = KVME_TYPE_NONE;
2399 kve->kve_ref_count = 0;
2400 kve->kve_shadow_count = 0;
2403 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2404 if (freepath != NULL)
2405 free(freepath, M_TEMP);
2407 /* Pack record size down */
2408 if ((flags & KERN_VMMAP_PACK_KINFO) != 0)
2409 kve->kve_structsize =
2410 offsetof(struct kinfo_vmentry, kve_path) +
2411 strlen(kve->kve_path) + 1;
2413 kve->kve_structsize = sizeof(*kve);
2414 kve->kve_structsize = roundup(kve->kve_structsize,
2417 /* Halt filling and truncate rather than exceeding maxlen */
2418 if (maxlen != -1 && maxlen < kve->kve_structsize) {
2420 vm_map_lock_read(map);
2422 } else if (maxlen != -1)
2423 maxlen -= kve->kve_structsize;
2425 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2427 vm_map_lock_read(map);
2430 if (last_timestamp != map->timestamp) {
2431 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2435 vm_map_unlock_read(map);
2443 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2447 int error, error2, *name;
2450 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2451 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2456 error = kern_proc_vmmap_out(p, &sb, -1, KERN_VMMAP_PACK_KINFO);
2457 error2 = sbuf_finish(&sb);
2459 return (error != 0 ? error : error2);
2462 #if defined(STACK) || defined(DDB)
2464 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2466 struct kinfo_kstack *kkstp;
2467 int error, i, *name, numthreads;
2468 lwpid_t *lwpidarray;
2475 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2479 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2480 st = stack_create();
2485 if (lwpidarray != NULL) {
2486 free(lwpidarray, M_TEMP);
2489 numthreads = p->p_numthreads;
2491 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2494 } while (numthreads < p->p_numthreads);
2497 * XXXRW: During the below loop, execve(2) and countless other sorts
2498 * of changes could have taken place. Should we check to see if the
2499 * vmspace has been replaced, or the like, in order to prevent
2500 * giving a snapshot that spans, say, execve(2), with some threads
2501 * before and some after? Among other things, the credentials could
2502 * have changed, in which case the right to extract debug info might
2503 * no longer be assured.
2506 FOREACH_THREAD_IN_PROC(p, td) {
2507 KASSERT(i < numthreads,
2508 ("sysctl_kern_proc_kstack: numthreads"));
2509 lwpidarray[i] = td->td_tid;
2513 for (i = 0; i < numthreads; i++) {
2514 td = thread_find(p, lwpidarray[i]);
2518 bzero(kkstp, sizeof(*kkstp));
2519 (void)sbuf_new(&sb, kkstp->kkst_trace,
2520 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2522 kkstp->kkst_tid = td->td_tid;
2523 if (TD_IS_SWAPPED(td))
2524 kkstp->kkst_state = KKST_STATE_SWAPPED;
2525 else if (TD_IS_RUNNING(td))
2526 kkstp->kkst_state = KKST_STATE_RUNNING;
2528 kkstp->kkst_state = KKST_STATE_STACKOK;
2529 stack_save_td(st, td);
2533 stack_sbuf_print(&sb, st);
2536 error = SYSCTL_OUT(req, kkstp, sizeof(*kkstp));
2543 if (lwpidarray != NULL)
2544 free(lwpidarray, M_TEMP);
2546 free(kkstp, M_TEMP);
2552 * This sysctl allows a process to retrieve the full list of groups from
2553 * itself or another process.
2556 sysctl_kern_proc_groups(SYSCTL_HANDLER_ARGS)
2558 pid_t *pidp = (pid_t *)arg1;
2559 unsigned int arglen = arg2;
2566 if (*pidp == -1) { /* -1 means this process */
2567 p = req->td->td_proc;
2569 error = pget(*pidp, PGET_CANSEE, &p);
2574 cred = crhold(p->p_ucred);
2578 error = SYSCTL_OUT(req, cred->cr_groups,
2579 cred->cr_ngroups * sizeof(gid_t));
2585 * This sysctl allows a process to retrieve or/and set the resource limit for
2589 sysctl_kern_proc_rlimit(SYSCTL_HANDLER_ARGS)
2591 int *name = (int *)arg1;
2592 u_int namelen = arg2;
2601 which = (u_int)name[1];
2602 if (which >= RLIM_NLIMITS)
2605 if (req->newptr != NULL && req->newlen != sizeof(rlim))
2608 flags = PGET_HOLD | PGET_NOTWEXIT;
2609 if (req->newptr != NULL)
2610 flags |= PGET_CANDEBUG;
2612 flags |= PGET_CANSEE;
2613 error = pget((pid_t)name[0], flags, &p);
2620 if (req->oldptr != NULL) {
2622 lim_rlimit(p, which, &rlim);
2625 error = SYSCTL_OUT(req, &rlim, sizeof(rlim));
2632 if (req->newptr != NULL) {
2633 error = SYSCTL_IN(req, &rlim, sizeof(rlim));
2635 error = kern_proc_setrlimit(curthread, p, which, &rlim);
2644 * This sysctl allows a process to retrieve ps_strings structure location of
2648 sysctl_kern_proc_ps_strings(SYSCTL_HANDLER_ARGS)
2650 int *name = (int *)arg1;
2651 u_int namelen = arg2;
2653 vm_offset_t ps_strings;
2655 #ifdef COMPAT_FREEBSD32
2656 uint32_t ps_strings32;
2662 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2665 #ifdef COMPAT_FREEBSD32
2666 if ((req->flags & SCTL_MASK32) != 0) {
2668 * We return 0 if the 32 bit emulation request is for a 64 bit
2671 ps_strings32 = SV_PROC_FLAG(p, SV_ILP32) != 0 ?
2672 PTROUT(p->p_sysent->sv_psstrings) : 0;
2674 error = SYSCTL_OUT(req, &ps_strings32, sizeof(ps_strings32));
2678 ps_strings = p->p_sysent->sv_psstrings;
2680 error = SYSCTL_OUT(req, &ps_strings, sizeof(ps_strings));
2685 * This sysctl allows a process to retrieve umask of another process.
2688 sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
2690 int *name = (int *)arg1;
2691 u_int namelen = arg2;
2699 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2703 FILEDESC_SLOCK(p->p_fd);
2704 fd_cmask = p->p_fd->fd_cmask;
2705 FILEDESC_SUNLOCK(p->p_fd);
2707 error = SYSCTL_OUT(req, &fd_cmask, sizeof(fd_cmask));
2712 * This sysctl allows a process to set and retrieve binary osreldate of
2716 sysctl_kern_proc_osrel(SYSCTL_HANDLER_ARGS)
2718 int *name = (int *)arg1;
2719 u_int namelen = arg2;
2721 int flags, error, osrel;
2726 if (req->newptr != NULL && req->newlen != sizeof(osrel))
2729 flags = PGET_HOLD | PGET_NOTWEXIT;
2730 if (req->newptr != NULL)
2731 flags |= PGET_CANDEBUG;
2733 flags |= PGET_CANSEE;
2734 error = pget((pid_t)name[0], flags, &p);
2738 error = SYSCTL_OUT(req, &p->p_osrel, sizeof(p->p_osrel));
2742 if (req->newptr != NULL) {
2743 error = SYSCTL_IN(req, &osrel, sizeof(osrel));
2758 sysctl_kern_proc_sigtramp(SYSCTL_HANDLER_ARGS)
2760 int *name = (int *)arg1;
2761 u_int namelen = arg2;
2763 struct kinfo_sigtramp kst;
2764 const struct sysentvec *sv;
2766 #ifdef COMPAT_FREEBSD32
2767 struct kinfo_sigtramp32 kst32;
2773 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2777 #ifdef COMPAT_FREEBSD32
2778 if ((req->flags & SCTL_MASK32) != 0) {
2779 bzero(&kst32, sizeof(kst32));
2780 if (SV_PROC_FLAG(p, SV_ILP32)) {
2781 if (sv->sv_sigcode_base != 0) {
2782 kst32.ksigtramp_start = sv->sv_sigcode_base;
2783 kst32.ksigtramp_end = sv->sv_sigcode_base +
2786 kst32.ksigtramp_start = sv->sv_psstrings -
2788 kst32.ksigtramp_end = sv->sv_psstrings;
2792 error = SYSCTL_OUT(req, &kst32, sizeof(kst32));
2796 bzero(&kst, sizeof(kst));
2797 if (sv->sv_sigcode_base != 0) {
2798 kst.ksigtramp_start = (char *)sv->sv_sigcode_base;
2799 kst.ksigtramp_end = (char *)sv->sv_sigcode_base +
2802 kst.ksigtramp_start = (char *)sv->sv_psstrings -
2804 kst.ksigtramp_end = (char *)sv->sv_psstrings;
2807 error = SYSCTL_OUT(req, &kst, sizeof(kst));
2811 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table");
2813 SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT|
2814 CTLFLAG_MPSAFE, 0, 0, sysctl_kern_proc, "S,proc",
2815 "Return entire process table");
2817 static SYSCTL_NODE(_kern_proc, KERN_PROC_GID, gid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2818 sysctl_kern_proc, "Process table");
2820 static SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD | CTLFLAG_MPSAFE,
2821 sysctl_kern_proc, "Process table");
2823 static SYSCTL_NODE(_kern_proc, KERN_PROC_RGID, rgid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2824 sysctl_kern_proc, "Process table");
2826 static SYSCTL_NODE(_kern_proc, KERN_PROC_SESSION, sid, CTLFLAG_RD |
2827 CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2829 static SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD | CTLFLAG_MPSAFE,
2830 sysctl_kern_proc, "Process table");
2832 static SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2833 sysctl_kern_proc, "Process table");
2835 static SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2836 sysctl_kern_proc, "Process table");
2838 static SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD | CTLFLAG_MPSAFE,
2839 sysctl_kern_proc, "Process table");
2841 static SYSCTL_NODE(_kern_proc, KERN_PROC_PROC, proc, CTLFLAG_RD | CTLFLAG_MPSAFE,
2842 sysctl_kern_proc, "Return process table, no threads");
2844 static SYSCTL_NODE(_kern_proc, KERN_PROC_ARGS, args,
2845 CTLFLAG_RW | CTLFLAG_CAPWR | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE,
2846 sysctl_kern_proc_args, "Process argument list");
2848 static SYSCTL_NODE(_kern_proc, KERN_PROC_ENV, env, CTLFLAG_RD | CTLFLAG_MPSAFE,
2849 sysctl_kern_proc_env, "Process environment");
2851 static SYSCTL_NODE(_kern_proc, KERN_PROC_AUXV, auxv, CTLFLAG_RD |
2852 CTLFLAG_MPSAFE, sysctl_kern_proc_auxv, "Process ELF auxiliary vector");
2854 static SYSCTL_NODE(_kern_proc, KERN_PROC_PATHNAME, pathname, CTLFLAG_RD |
2855 CTLFLAG_MPSAFE, sysctl_kern_proc_pathname, "Process executable path");
2857 static SYSCTL_NODE(_kern_proc, KERN_PROC_SV_NAME, sv_name, CTLFLAG_RD |
2858 CTLFLAG_MPSAFE, sysctl_kern_proc_sv_name,
2859 "Process syscall vector name (ABI type)");
2861 static SYSCTL_NODE(_kern_proc, (KERN_PROC_GID | KERN_PROC_INC_THREAD), gid_td,
2862 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2864 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PGRP | KERN_PROC_INC_THREAD), pgrp_td,
2865 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2867 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RGID | KERN_PROC_INC_THREAD), rgid_td,
2868 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2870 static SYSCTL_NODE(_kern_proc, (KERN_PROC_SESSION | KERN_PROC_INC_THREAD),
2871 sid_td, CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2873 static SYSCTL_NODE(_kern_proc, (KERN_PROC_TTY | KERN_PROC_INC_THREAD), tty_td,
2874 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2876 static SYSCTL_NODE(_kern_proc, (KERN_PROC_UID | KERN_PROC_INC_THREAD), uid_td,
2877 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2879 static SYSCTL_NODE(_kern_proc, (KERN_PROC_RUID | KERN_PROC_INC_THREAD), ruid_td,
2880 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2882 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PID | KERN_PROC_INC_THREAD), pid_td,
2883 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc, "Process table");
2885 static SYSCTL_NODE(_kern_proc, (KERN_PROC_PROC | KERN_PROC_INC_THREAD), proc_td,
2886 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_kern_proc,
2887 "Return process table, no threads");
2889 #ifdef COMPAT_FREEBSD7
2890 static SYSCTL_NODE(_kern_proc, KERN_PROC_OVMMAP, ovmmap, CTLFLAG_RD |
2891 CTLFLAG_MPSAFE, sysctl_kern_proc_ovmmap, "Old Process vm map entries");
2894 static SYSCTL_NODE(_kern_proc, KERN_PROC_VMMAP, vmmap, CTLFLAG_RD |
2895 CTLFLAG_MPSAFE, sysctl_kern_proc_vmmap, "Process vm map entries");
2897 #if defined(STACK) || defined(DDB)
2898 static SYSCTL_NODE(_kern_proc, KERN_PROC_KSTACK, kstack, CTLFLAG_RD |
2899 CTLFLAG_MPSAFE, sysctl_kern_proc_kstack, "Process kernel stacks");
2902 static SYSCTL_NODE(_kern_proc, KERN_PROC_GROUPS, groups, CTLFLAG_RD |
2903 CTLFLAG_MPSAFE, sysctl_kern_proc_groups, "Process groups");
2905 static SYSCTL_NODE(_kern_proc, KERN_PROC_RLIMIT, rlimit, CTLFLAG_RW |
2906 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_rlimit,
2907 "Process resource limits");
2909 static SYSCTL_NODE(_kern_proc, KERN_PROC_PS_STRINGS, ps_strings, CTLFLAG_RD |
2910 CTLFLAG_MPSAFE, sysctl_kern_proc_ps_strings,
2911 "Process ps_strings location");
2913 static SYSCTL_NODE(_kern_proc, KERN_PROC_UMASK, umask, CTLFLAG_RD |
2914 CTLFLAG_MPSAFE, sysctl_kern_proc_umask, "Process umask");
2916 static SYSCTL_NODE(_kern_proc, KERN_PROC_OSREL, osrel, CTLFLAG_RW |
2917 CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, sysctl_kern_proc_osrel,
2918 "Process binary osreldate");
2920 static SYSCTL_NODE(_kern_proc, KERN_PROC_SIGTRAMP, sigtramp, CTLFLAG_RD |
2921 CTLFLAG_MPSAFE, sysctl_kern_proc_sigtramp,
2922 "Process signal trampoline location");
2927 * stop_all_proc() purpose is to stop all process which have usermode,
2928 * except current process for obvious reasons. This makes it somewhat
2929 * unreliable when invoked from multithreaded process. The service
2930 * must not be user-callable anyway.
2935 struct proc *cp, *p;
2937 bool restart, seen_stopped, seen_exiting, stopped_some;
2941 sx_xlock(&allproc_lock);
2943 seen_exiting = seen_stopped = stopped_some = restart = false;
2944 LIST_REMOVE(cp, p_list);
2945 LIST_INSERT_HEAD(&allproc, cp, p_list);
2947 p = LIST_NEXT(cp, p_list);
2950 LIST_REMOVE(cp, p_list);
2951 LIST_INSERT_AFTER(p, cp, p_list);
2953 if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
2954 P_TOTAL_STOP)) != 0) {
2958 if ((p->p_flag & P_WEXIT) != 0) {
2959 seen_exiting = true;
2963 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
2965 * Stopped processes are tolerated when there
2966 * are no other processes which might continue
2967 * them. P_STOPPED_SINGLE but not
2968 * P_TOTAL_STOP process still has at least one
2971 seen_stopped = true;
2976 sx_xunlock(&allproc_lock);
2977 r = thread_single(p, SINGLE_ALLPROC);
2981 stopped_some = true;
2984 sx_xlock(&allproc_lock);
2986 /* Catch forked children we did not see in iteration. */
2987 if (gen != allproc_gen)
2989 sx_xunlock(&allproc_lock);
2990 if (restart || stopped_some || seen_exiting || seen_stopped) {
2991 kern_yield(PRI_USER);
2997 resume_all_proc(void)
2999 struct proc *cp, *p;
3002 sx_xlock(&allproc_lock);
3003 LIST_REMOVE(cp, p_list);
3004 LIST_INSERT_HEAD(&allproc, cp, p_list);
3006 p = LIST_NEXT(cp, p_list);
3009 LIST_REMOVE(cp, p_list);
3010 LIST_INSERT_AFTER(p, cp, p_list);
3012 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3013 sx_xunlock(&allproc_lock);
3015 thread_single_end(p, SINGLE_ALLPROC);
3018 sx_xlock(&allproc_lock);
3023 sx_xunlock(&allproc_lock);
3026 /* #define TOTAL_STOP_DEBUG 1 */
3027 #ifdef TOTAL_STOP_DEBUG
3028 volatile static int ap_resume;
3029 #include <sys/mount.h>
3032 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3038 error = sysctl_handle_int(oidp, &val, 0, req);
3039 if (error != 0 || req->newptr == NULL)
3044 while (ap_resume == 0)
3052 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3053 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3054 sysctl_debug_stop_all_proc, "I",