2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include "opt_ktrace.h"
38 #include "opt_kstack_pages.h"
39 #include "opt_stack.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/eventhandler.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
50 #include <sys/loginclass.h>
51 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
56 #include <sys/ptrace.h>
57 #include <sys/refcount.h>
58 #include <sys/resourcevar.h>
59 #include <sys/rwlock.h>
61 #include <sys/sysent.h>
62 #include <sys/sched.h>
64 #include <sys/stack.h>
66 #include <sys/sysctl.h>
67 #include <sys/filedesc.h>
69 #include <sys/signalvar.h>
73 #include <sys/vnode.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_page.h>
89 #ifdef COMPAT_FREEBSD32
90 #include <compat/freebsd32/freebsd32.h>
91 #include <compat/freebsd32/freebsd32_util.h>
94 SDT_PROVIDER_DEFINE(proc);
95 SDT_PROBE_DEFINE4(proc, kernel, ctor, entry, "struct proc *", "int",
97 SDT_PROBE_DEFINE4(proc, kernel, ctor, return, "struct proc *", "int",
99 SDT_PROBE_DEFINE4(proc, kernel, dtor, entry, "struct proc *", "int",
100 "void *", "struct thread *");
101 SDT_PROBE_DEFINE3(proc, kernel, dtor, return, "struct proc *", "int",
103 SDT_PROBE_DEFINE3(proc, kernel, init, entry, "struct proc *", "int",
105 SDT_PROBE_DEFINE3(proc, kernel, init, return, "struct proc *", "int",
108 MALLOC_DEFINE(M_PGRP, "pgrp", "process group header");
109 MALLOC_DEFINE(M_SESSION, "session", "session header");
110 static MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
111 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
113 static void doenterpgrp(struct proc *, struct pgrp *);
114 static void orphanpg(struct pgrp *pg);
115 static void fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp);
116 static void fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp);
117 static void fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp,
119 static void pgadjustjobc(struct pgrp *pgrp, int entering);
120 static void pgdelete(struct pgrp *);
121 static int proc_ctor(void *mem, int size, void *arg, int flags);
122 static void proc_dtor(void *mem, int size, void *arg);
123 static int proc_init(void *mem, int size, int flags);
124 static void proc_fini(void *mem, int size);
125 static void pargs_free(struct pargs *pa);
126 static struct proc *zpfind_locked(pid_t pid);
129 * Other process lists
131 struct pidhashhead *pidhashtbl;
133 struct pgrphashhead *pgrphashtbl;
135 struct proclist allproc;
136 struct proclist zombproc;
137 struct sx allproc_lock;
138 struct sx proctree_lock;
139 struct mtx ppeers_lock;
140 uma_zone_t proc_zone;
142 int kstack_pages = KSTACK_PAGES;
143 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
144 "Kernel stack size in pages");
145 static int vmmap_skip_res_cnt = 0;
146 SYSCTL_INT(_kern, OID_AUTO, proc_vmmap_skip_resident_count, CTLFLAG_RW,
147 &vmmap_skip_res_cnt, 0,
148 "Skip calculation of the pages resident count in kern.proc.vmmap");
150 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
151 #ifdef COMPAT_FREEBSD32
152 CTASSERT(sizeof(struct kinfo_proc32) == KINFO_PROC32_SIZE);
156 * Initialize global process hashing structures.
162 sx_init(&allproc_lock, "allproc");
163 sx_init(&proctree_lock, "proctree");
164 mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF);
166 LIST_INIT(&zombproc);
167 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
168 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
169 proc_zone = uma_zcreate("PROC", sched_sizeof_proc(),
170 proc_ctor, proc_dtor, proc_init, proc_fini,
171 UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
176 * Prepare a proc for use.
179 proc_ctor(void *mem, int size, void *arg, int flags)
183 p = (struct proc *)mem;
184 SDT_PROBE(proc, kernel, ctor , entry, p, size, arg, flags, 0);
185 EVENTHANDLER_INVOKE(process_ctor, p);
186 SDT_PROBE(proc, kernel, ctor , return, p, size, arg, flags, 0);
191 * Reclaim a proc after use.
194 proc_dtor(void *mem, int size, void *arg)
199 /* INVARIANTS checks go here */
200 p = (struct proc *)mem;
201 td = FIRST_THREAD_IN_PROC(p);
202 SDT_PROBE(proc, kernel, dtor, entry, p, size, arg, td, 0);
205 KASSERT((p->p_numthreads == 1),
206 ("bad number of threads in exiting process"));
207 KASSERT(STAILQ_EMPTY(&p->p_ktr), ("proc_dtor: non-empty p_ktr"));
209 /* Free all OSD associated to this thread. */
212 EVENTHANDLER_INVOKE(process_dtor, p);
213 if (p->p_ksi != NULL)
214 KASSERT(! KSI_ONQ(p->p_ksi), ("SIGCHLD queue"));
215 SDT_PROBE(proc, kernel, dtor, return, p, size, arg, 0, 0);
219 * Initialize type-stable parts of a proc (when newly created).
222 proc_init(void *mem, int size, int flags)
226 p = (struct proc *)mem;
227 SDT_PROBE(proc, kernel, init, entry, p, size, flags, 0, 0);
228 p->p_sched = (struct p_sched *)&p[1];
229 mtx_init(&p->p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK | MTX_NEW);
230 mtx_init(&p->p_slock, "process slock", NULL, MTX_SPIN | MTX_NEW);
231 mtx_init(&p->p_statmtx, "pstatl", NULL, MTX_SPIN | MTX_NEW);
232 mtx_init(&p->p_itimmtx, "pitiml", NULL, MTX_SPIN | MTX_NEW);
233 mtx_init(&p->p_profmtx, "pprofl", NULL, MTX_SPIN | MTX_NEW);
234 cv_init(&p->p_pwait, "ppwait");
235 cv_init(&p->p_dbgwait, "dbgwait");
236 TAILQ_INIT(&p->p_threads); /* all threads in proc */
237 EVENTHANDLER_INVOKE(process_init, p);
238 p->p_stats = pstats_alloc();
239 SDT_PROBE(proc, kernel, init, return, p, size, flags, 0, 0);
244 * UMA should ensure that this function is never called.
245 * Freeing a proc structure would violate type stability.
248 proc_fini(void *mem, int size)
253 p = (struct proc *)mem;
254 EVENTHANDLER_INVOKE(process_fini, p);
255 pstats_free(p->p_stats);
256 thread_free(FIRST_THREAD_IN_PROC(p));
257 mtx_destroy(&p->p_mtx);
258 if (p->p_ksi != NULL)
259 ksiginfo_free(p->p_ksi);
261 panic("proc reclaimed");
266 * Is p an inferior of the current process?
269 inferior(struct proc *p)
272 sx_assert(&proctree_lock, SX_LOCKED);
273 PROC_LOCK_ASSERT(p, MA_OWNED);
274 for (; p != curproc; p = proc_realparent(p)) {
282 pfind_locked(pid_t pid)
286 sx_assert(&allproc_lock, SX_LOCKED);
287 LIST_FOREACH(p, PIDHASH(pid), p_hash) {
288 if (p->p_pid == pid) {
290 if (p->p_state == PRS_NEW) {
301 * Locate a process by number; return only "live" processes -- i.e., neither
302 * zombies nor newly born but incompletely initialized processes. By not
303 * returning processes in the PRS_NEW state, we allow callers to avoid
304 * testing for that condition to avoid dereferencing p_ucred, et al.
311 sx_slock(&allproc_lock);
312 p = pfind_locked(pid);
313 sx_sunlock(&allproc_lock);
318 pfind_tid_locked(pid_t tid)
323 sx_assert(&allproc_lock, SX_LOCKED);
324 FOREACH_PROC_IN_SYSTEM(p) {
326 if (p->p_state == PRS_NEW) {
330 FOREACH_THREAD_IN_PROC(p, td) {
331 if (td->td_tid == tid)
341 * Locate a process group by number.
342 * The caller must hold proctree_lock.
348 register struct pgrp *pgrp;
350 sx_assert(&proctree_lock, SX_LOCKED);
352 LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash) {
353 if (pgrp->pg_id == pgid) {
362 * Locate process and do additional manipulations, depending on flags.
365 pget(pid_t pid, int flags, struct proc **pp)
370 sx_slock(&allproc_lock);
371 if (pid <= PID_MAX) {
372 p = pfind_locked(pid);
373 if (p == NULL && (flags & PGET_NOTWEXIT) == 0)
374 p = zpfind_locked(pid);
375 } else if ((flags & PGET_NOTID) == 0) {
376 p = pfind_tid_locked(pid);
380 sx_sunlock(&allproc_lock);
383 if ((flags & PGET_CANSEE) != 0) {
384 error = p_cansee(curthread, p);
388 if ((flags & PGET_CANDEBUG) != 0) {
389 error = p_candebug(curthread, p);
393 if ((flags & PGET_ISCURRENT) != 0 && curproc != p) {
397 if ((flags & PGET_NOTWEXIT) != 0 && (p->p_flag & P_WEXIT) != 0) {
401 if ((flags & PGET_NOTINEXEC) != 0 && (p->p_flag & P_INEXEC) != 0) {
403 * XXXRW: Not clear ESRCH is the right error during proc
409 if ((flags & PGET_HOLD) != 0) {
421 * Create a new process group.
422 * pgid must be equal to the pid of p.
423 * Begin a new session if required.
426 enterpgrp(p, pgid, pgrp, sess)
427 register struct proc *p;
430 struct session *sess;
433 sx_assert(&proctree_lock, SX_XLOCKED);
435 KASSERT(pgrp != NULL, ("enterpgrp: pgrp == NULL"));
436 KASSERT(p->p_pid == pgid,
437 ("enterpgrp: new pgrp and pid != pgid"));
438 KASSERT(pgfind(pgid) == NULL,
439 ("enterpgrp: pgrp with pgid exists"));
440 KASSERT(!SESS_LEADER(p),
441 ("enterpgrp: session leader attempted setpgrp"));
443 mtx_init(&pgrp->pg_mtx, "process group", NULL, MTX_DEF | MTX_DUPOK);
449 mtx_init(&sess->s_mtx, "session", NULL, MTX_DEF);
451 p->p_flag &= ~P_CONTROLT;
455 sess->s_sid = p->p_pid;
456 refcount_init(&sess->s_count, 1);
457 sess->s_ttyvp = NULL;
458 sess->s_ttydp = NULL;
460 bcopy(p->p_session->s_login, sess->s_login,
461 sizeof(sess->s_login));
462 pgrp->pg_session = sess;
463 KASSERT(p == curproc,
464 ("enterpgrp: mksession and p != curproc"));
466 pgrp->pg_session = p->p_session;
467 sess_hold(pgrp->pg_session);
471 LIST_INIT(&pgrp->pg_members);
474 * As we have an exclusive lock of proctree_lock,
475 * this should not deadlock.
477 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
479 SLIST_INIT(&pgrp->pg_sigiolst);
482 doenterpgrp(p, pgrp);
488 * Move p to an existing process group
491 enterthispgrp(p, pgrp)
492 register struct proc *p;
496 sx_assert(&proctree_lock, SX_XLOCKED);
497 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
498 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
499 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
500 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
501 KASSERT(pgrp->pg_session == p->p_session,
502 ("%s: pgrp's session %p, p->p_session %p.\n",
506 KASSERT(pgrp != p->p_pgrp,
507 ("%s: p belongs to pgrp.", __func__));
509 doenterpgrp(p, pgrp);
515 * Move p to a process group
522 struct pgrp *savepgrp;
524 sx_assert(&proctree_lock, SX_XLOCKED);
525 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
526 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
527 PGRP_LOCK_ASSERT(p->p_pgrp, MA_NOTOWNED);
528 SESS_LOCK_ASSERT(p->p_session, MA_NOTOWNED);
530 savepgrp = p->p_pgrp;
533 * Adjust eligibility of affected pgrps to participate in job control.
534 * Increment eligibility counts before decrementing, otherwise we
535 * could reach 0 spuriously during the first call.
538 fixjobc(p, p->p_pgrp, 0);
543 LIST_REMOVE(p, p_pglist);
546 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
547 PGRP_UNLOCK(savepgrp);
549 if (LIST_EMPTY(&savepgrp->pg_members))
554 * remove process from process group
558 register struct proc *p;
560 struct pgrp *savepgrp;
562 sx_assert(&proctree_lock, SX_XLOCKED);
563 savepgrp = p->p_pgrp;
566 LIST_REMOVE(p, p_pglist);
569 PGRP_UNLOCK(savepgrp);
570 if (LIST_EMPTY(&savepgrp->pg_members))
576 * delete a process group
580 register struct pgrp *pgrp;
582 struct session *savesess;
585 sx_assert(&proctree_lock, SX_XLOCKED);
586 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
587 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
590 * Reset any sigio structures pointing to us as a result of
591 * F_SETOWN with our pgid.
593 funsetownlst(&pgrp->pg_sigiolst);
596 tp = pgrp->pg_session->s_ttyp;
597 LIST_REMOVE(pgrp, pg_hash);
598 savesess = pgrp->pg_session;
601 /* Remove the reference to the pgrp before deallocating it. */
604 tty_rel_pgrp(tp, pgrp);
607 mtx_destroy(&pgrp->pg_mtx);
609 sess_release(savesess);
613 pgadjustjobc(pgrp, entering)
623 if (pgrp->pg_jobc == 0)
630 * Adjust pgrp jobc counters when specified process changes process group.
631 * We count the number of processes in each process group that "qualify"
632 * the group for terminal job control (those with a parent in a different
633 * process group of the same session). If that count reaches zero, the
634 * process group becomes orphaned. Check both the specified process'
635 * process group and that of its children.
636 * entering == 0 => p is leaving specified group.
637 * entering == 1 => p is entering specified group.
640 fixjobc(p, pgrp, entering)
641 register struct proc *p;
642 register struct pgrp *pgrp;
645 register struct pgrp *hispgrp;
646 register struct session *mysession;
648 sx_assert(&proctree_lock, SX_LOCKED);
649 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
650 PGRP_LOCK_ASSERT(pgrp, MA_NOTOWNED);
651 SESS_LOCK_ASSERT(pgrp->pg_session, MA_NOTOWNED);
654 * Check p's parent to see whether p qualifies its own process
655 * group; if so, adjust count for p's process group.
657 mysession = pgrp->pg_session;
658 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
659 hispgrp->pg_session == mysession)
660 pgadjustjobc(pgrp, entering);
663 * Check this process' children to see whether they qualify
664 * their process groups; if so, adjust counts for children's
667 LIST_FOREACH(p, &p->p_children, p_sibling) {
669 if (hispgrp == pgrp ||
670 hispgrp->pg_session != mysession)
673 if (p->p_state == PRS_ZOMBIE) {
678 pgadjustjobc(hispgrp, entering);
683 * A process group has become orphaned;
684 * if there are any stopped processes in the group,
685 * hang-up all process in that group.
691 register struct proc *p;
693 PGRP_LOCK_ASSERT(pg, MA_OWNED);
695 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
697 if (P_SHOULDSTOP(p) == P_STOPPED_SIG) {
699 LIST_FOREACH(p, &pg->pg_members, p_pglist) {
701 kern_psignal(p, SIGHUP);
702 kern_psignal(p, SIGCONT);
712 sess_hold(struct session *s)
715 refcount_acquire(&s->s_count);
719 sess_release(struct session *s)
722 if (refcount_release(&s->s_count)) {
723 if (s->s_ttyp != NULL) {
725 tty_rel_sess(s->s_ttyp, s);
727 mtx_destroy(&s->s_mtx);
734 DB_SHOW_COMMAND(pgrpdump, pgrpdump)
736 register struct pgrp *pgrp;
737 register struct proc *p;
740 for (i = 0; i <= pgrphash; i++) {
741 if (!LIST_EMPTY(&pgrphashtbl[i])) {
742 printf("\tindx %d\n", i);
743 LIST_FOREACH(pgrp, &pgrphashtbl[i], pg_hash) {
745 "\tpgrp %p, pgid %ld, sess %p, sesscnt %d, mem %p\n",
746 (void *)pgrp, (long)pgrp->pg_id,
747 (void *)pgrp->pg_session,
748 pgrp->pg_session->s_count,
749 (void *)LIST_FIRST(&pgrp->pg_members));
750 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
751 printf("\t\tpid %ld addr %p pgrp %p\n",
752 (long)p->p_pid, (void *)p,
762 * Calculate the kinfo_proc members which contain process-wide
764 * Must be called with the target process locked.
767 fill_kinfo_aggregate(struct proc *p, struct kinfo_proc *kp)
771 PROC_LOCK_ASSERT(p, MA_OWNED);
775 FOREACH_THREAD_IN_PROC(p, td) {
777 kp->ki_pctcpu += sched_pctcpu(td);
778 kp->ki_estcpu += td->td_estcpu;
784 * Clear kinfo_proc and fill in any information that is common
785 * to all threads in the process.
786 * Must be called with the target process locked.
789 fill_kinfo_proc_only(struct proc *p, struct kinfo_proc *kp)
797 /* For proc_realparent. */
798 sx_assert(&proctree_lock, SX_LOCKED);
799 PROC_LOCK_ASSERT(p, MA_OWNED);
800 bzero(kp, sizeof(*kp));
802 kp->ki_structsize = sizeof(*kp);
804 kp->ki_addr =/* p->p_addr; */0; /* XXX */
805 kp->ki_args = p->p_args;
806 kp->ki_textvp = p->p_textvp;
808 kp->ki_tracep = p->p_tracevp;
809 kp->ki_traceflag = p->p_traceflag;
812 kp->ki_vmspace = p->p_vmspace;
813 kp->ki_flag = p->p_flag;
814 kp->ki_flag2 = p->p_flag2;
817 kp->ki_uid = cred->cr_uid;
818 kp->ki_ruid = cred->cr_ruid;
819 kp->ki_svuid = cred->cr_svuid;
821 if (cred->cr_flags & CRED_FLAG_CAPMODE)
822 kp->ki_cr_flags |= KI_CRF_CAPABILITY_MODE;
823 /* XXX bde doesn't like KI_NGROUPS */
824 if (cred->cr_ngroups > KI_NGROUPS) {
825 kp->ki_ngroups = KI_NGROUPS;
826 kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
828 kp->ki_ngroups = cred->cr_ngroups;
829 bcopy(cred->cr_groups, kp->ki_groups,
830 kp->ki_ngroups * sizeof(gid_t));
831 kp->ki_rgid = cred->cr_rgid;
832 kp->ki_svgid = cred->cr_svgid;
833 /* If jailed(cred), emulate the old P_JAILED flag. */
835 kp->ki_flag |= P_JAILED;
836 /* If inside the jail, use 0 as a jail ID. */
837 if (cred->cr_prison != curthread->td_ucred->cr_prison)
838 kp->ki_jid = cred->cr_prison->pr_id;
840 strlcpy(kp->ki_loginclass, cred->cr_loginclass->lc_name,
841 sizeof(kp->ki_loginclass));
845 mtx_lock(&ps->ps_mtx);
846 kp->ki_sigignore = ps->ps_sigignore;
847 kp->ki_sigcatch = ps->ps_sigcatch;
848 mtx_unlock(&ps->ps_mtx);
850 if (p->p_state != PRS_NEW &&
851 p->p_state != PRS_ZOMBIE &&
852 p->p_vmspace != NULL) {
853 struct vmspace *vm = p->p_vmspace;
855 kp->ki_size = vm->vm_map.size;
856 kp->ki_rssize = vmspace_resident_count(vm); /*XXX*/
857 FOREACH_THREAD_IN_PROC(p, td0) {
858 if (!TD_IS_SWAPPED(td0))
859 kp->ki_rssize += td0->td_kstack_pages;
861 kp->ki_swrss = vm->vm_swrss;
862 kp->ki_tsize = vm->vm_tsize;
863 kp->ki_dsize = vm->vm_dsize;
864 kp->ki_ssize = vm->vm_ssize;
865 } else if (p->p_state == PRS_ZOMBIE)
867 if (kp->ki_flag & P_INMEM)
868 kp->ki_sflag = PS_INMEM;
871 /* Calculate legacy swtime as seconds since 'swtick'. */
872 kp->ki_swtime = (ticks - p->p_swtick) / hz;
873 kp->ki_pid = p->p_pid;
874 kp->ki_nice = p->p_nice;
875 kp->ki_fibnum = p->p_fibnum;
876 kp->ki_start = p->p_stats->p_start;
877 timevaladd(&kp->ki_start, &boottime);
879 rufetch(p, &kp->ki_rusage);
880 kp->ki_runtime = cputick2usec(p->p_rux.rux_runtime);
881 calcru(p, &kp->ki_rusage.ru_utime, &kp->ki_rusage.ru_stime);
883 calccru(p, &kp->ki_childutime, &kp->ki_childstime);
884 /* Some callers want child times in a single value. */
885 kp->ki_childtime = kp->ki_childstime;
886 timevaladd(&kp->ki_childtime, &kp->ki_childutime);
888 FOREACH_THREAD_IN_PROC(p, td0)
889 kp->ki_cow += td0->td_cow;
893 kp->ki_pgid = p->p_pgrp->pg_id;
894 kp->ki_jobc = p->p_pgrp->pg_jobc;
895 sp = p->p_pgrp->pg_session;
898 kp->ki_sid = sp->s_sid;
900 strlcpy(kp->ki_login, sp->s_login,
901 sizeof(kp->ki_login));
903 kp->ki_kiflag |= KI_CTTY;
905 kp->ki_kiflag |= KI_SLEADER;
906 /* XXX proctree_lock */
911 if ((p->p_flag & P_CONTROLT) && tp != NULL) {
912 kp->ki_tdev = tty_udev(tp);
913 kp->ki_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
915 kp->ki_tsid = tp->t_session->s_sid;
918 if (p->p_comm[0] != '\0')
919 strlcpy(kp->ki_comm, p->p_comm, sizeof(kp->ki_comm));
920 if (p->p_sysent && p->p_sysent->sv_name != NULL &&
921 p->p_sysent->sv_name[0] != '\0')
922 strlcpy(kp->ki_emul, p->p_sysent->sv_name, sizeof(kp->ki_emul));
923 kp->ki_siglist = p->p_siglist;
924 kp->ki_xstat = KW_EXITCODE(p->p_xexit, p->p_xsig);
925 kp->ki_acflag = p->p_acflag;
926 kp->ki_lock = p->p_lock;
928 kp->ki_ppid = proc_realparent(p)->p_pid;
929 if (p->p_flag & P_TRACED)
930 kp->ki_tracer = p->p_pptr->p_pid;
935 * Fill in information that is thread specific. Must be called with
936 * target process locked. If 'preferthread' is set, overwrite certain
937 * process-related fields that are maintained for both threads and
941 fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread)
947 PROC_LOCK_ASSERT(p, MA_OWNED);
952 if (td->td_wmesg != NULL)
953 strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg));
955 bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg));
956 strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname));
957 if (TD_ON_LOCK(td)) {
958 kp->ki_kiflag |= KI_LOCKBLOCK;
959 strlcpy(kp->ki_lockname, td->td_lockname,
960 sizeof(kp->ki_lockname));
962 kp->ki_kiflag &= ~KI_LOCKBLOCK;
963 bzero(kp->ki_lockname, sizeof(kp->ki_lockname));
966 if (p->p_state == PRS_NORMAL) { /* approximate. */
967 if (TD_ON_RUNQ(td) ||
971 } else if (P_SHOULDSTOP(p)) {
973 } else if (TD_IS_SLEEPING(td)) {
974 kp->ki_stat = SSLEEP;
975 } else if (TD_ON_LOCK(td)) {
980 } else if (p->p_state == PRS_ZOMBIE) {
986 /* Things in the thread */
987 kp->ki_wchan = td->td_wchan;
988 kp->ki_pri.pri_level = td->td_priority;
989 kp->ki_pri.pri_native = td->td_base_pri;
992 * Note: legacy fields; clamp at the old NOCPU value and/or
993 * the maximum u_char CPU value.
995 if (td->td_lastcpu == NOCPU)
996 kp->ki_lastcpu_old = NOCPU_OLD;
997 else if (td->td_lastcpu > MAXCPU_OLD)
998 kp->ki_lastcpu_old = MAXCPU_OLD;
1000 kp->ki_lastcpu_old = td->td_lastcpu;
1002 if (td->td_oncpu == NOCPU)
1003 kp->ki_oncpu_old = NOCPU_OLD;
1004 else if (td->td_oncpu > MAXCPU_OLD)
1005 kp->ki_oncpu_old = MAXCPU_OLD;
1007 kp->ki_oncpu_old = td->td_oncpu;
1009 kp->ki_lastcpu = td->td_lastcpu;
1010 kp->ki_oncpu = td->td_oncpu;
1011 kp->ki_tdflags = td->td_flags;
1012 kp->ki_tid = td->td_tid;
1013 kp->ki_numthreads = p->p_numthreads;
1014 kp->ki_pcb = td->td_pcb;
1015 kp->ki_kstack = (void *)td->td_kstack;
1016 kp->ki_slptime = (ticks - td->td_slptick) / hz;
1017 kp->ki_pri.pri_class = td->td_pri_class;
1018 kp->ki_pri.pri_user = td->td_user_pri;
1021 rufetchtd(td, &kp->ki_rusage);
1022 kp->ki_runtime = cputick2usec(td->td_rux.rux_runtime);
1023 kp->ki_pctcpu = sched_pctcpu(td);
1024 kp->ki_estcpu = td->td_estcpu;
1025 kp->ki_cow = td->td_cow;
1028 /* We can't get this anymore but ps etc never used it anyway. */
1032 kp->ki_siglist = td->td_siglist;
1033 kp->ki_sigmask = td->td_sigmask;
1040 * Fill in a kinfo_proc structure for the specified process.
1041 * Must be called with the target process locked.
1044 fill_kinfo_proc(struct proc *p, struct kinfo_proc *kp)
1047 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1049 fill_kinfo_proc_only(p, kp);
1050 fill_kinfo_thread(FIRST_THREAD_IN_PROC(p), kp, 0);
1051 fill_kinfo_aggregate(p, kp);
1058 return (malloc(sizeof(struct pstats), M_SUBPROC, M_ZERO|M_WAITOK));
1062 * Copy parts of p_stats; zero the rest of p_stats (statistics).
1065 pstats_fork(struct pstats *src, struct pstats *dst)
1068 bzero(&dst->pstat_startzero,
1069 __rangeof(struct pstats, pstat_startzero, pstat_endzero));
1070 bcopy(&src->pstat_startcopy, &dst->pstat_startcopy,
1071 __rangeof(struct pstats, pstat_startcopy, pstat_endcopy));
1075 pstats_free(struct pstats *ps)
1078 free(ps, M_SUBPROC);
1081 static struct proc *
1082 zpfind_locked(pid_t pid)
1086 sx_assert(&allproc_lock, SX_LOCKED);
1087 LIST_FOREACH(p, &zombproc, p_list) {
1088 if (p->p_pid == pid) {
1097 * Locate a zombie process by number
1104 sx_slock(&allproc_lock);
1105 p = zpfind_locked(pid);
1106 sx_sunlock(&allproc_lock);
1110 #ifdef COMPAT_FREEBSD32
1113 * This function is typically used to copy out the kernel address, so
1114 * it can be replaced by assignment of zero.
1116 static inline uint32_t
1117 ptr32_trim(void *ptr)
1121 uptr = (uintptr_t)ptr;
1122 return ((uptr > UINT_MAX) ? 0 : uptr);
1125 #define PTRTRIM_CP(src,dst,fld) \
1126 do { (dst).fld = ptr32_trim((src).fld); } while (0)
1129 freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32)
1133 bzero(ki32, sizeof(struct kinfo_proc32));
1134 ki32->ki_structsize = sizeof(struct kinfo_proc32);
1135 CP(*ki, *ki32, ki_layout);
1136 PTRTRIM_CP(*ki, *ki32, ki_args);
1137 PTRTRIM_CP(*ki, *ki32, ki_paddr);
1138 PTRTRIM_CP(*ki, *ki32, ki_addr);
1139 PTRTRIM_CP(*ki, *ki32, ki_tracep);
1140 PTRTRIM_CP(*ki, *ki32, ki_textvp);
1141 PTRTRIM_CP(*ki, *ki32, ki_fd);
1142 PTRTRIM_CP(*ki, *ki32, ki_vmspace);
1143 PTRTRIM_CP(*ki, *ki32, ki_wchan);
1144 CP(*ki, *ki32, ki_pid);
1145 CP(*ki, *ki32, ki_ppid);
1146 CP(*ki, *ki32, ki_pgid);
1147 CP(*ki, *ki32, ki_tpgid);
1148 CP(*ki, *ki32, ki_sid);
1149 CP(*ki, *ki32, ki_tsid);
1150 CP(*ki, *ki32, ki_jobc);
1151 CP(*ki, *ki32, ki_tdev);
1152 CP(*ki, *ki32, ki_siglist);
1153 CP(*ki, *ki32, ki_sigmask);
1154 CP(*ki, *ki32, ki_sigignore);
1155 CP(*ki, *ki32, ki_sigcatch);
1156 CP(*ki, *ki32, ki_uid);
1157 CP(*ki, *ki32, ki_ruid);
1158 CP(*ki, *ki32, ki_svuid);
1159 CP(*ki, *ki32, ki_rgid);
1160 CP(*ki, *ki32, ki_svgid);
1161 CP(*ki, *ki32, ki_ngroups);
1162 for (i = 0; i < KI_NGROUPS; i++)
1163 CP(*ki, *ki32, ki_groups[i]);
1164 CP(*ki, *ki32, ki_size);
1165 CP(*ki, *ki32, ki_rssize);
1166 CP(*ki, *ki32, ki_swrss);
1167 CP(*ki, *ki32, ki_tsize);
1168 CP(*ki, *ki32, ki_dsize);
1169 CP(*ki, *ki32, ki_ssize);
1170 CP(*ki, *ki32, ki_xstat);
1171 CP(*ki, *ki32, ki_acflag);
1172 CP(*ki, *ki32, ki_pctcpu);
1173 CP(*ki, *ki32, ki_estcpu);
1174 CP(*ki, *ki32, ki_slptime);
1175 CP(*ki, *ki32, ki_swtime);
1176 CP(*ki, *ki32, ki_cow);
1177 CP(*ki, *ki32, ki_runtime);
1178 TV_CP(*ki, *ki32, ki_start);
1179 TV_CP(*ki, *ki32, ki_childtime);
1180 CP(*ki, *ki32, ki_flag);
1181 CP(*ki, *ki32, ki_kiflag);
1182 CP(*ki, *ki32, ki_traceflag);
1183 CP(*ki, *ki32, ki_stat);
1184 CP(*ki, *ki32, ki_nice);
1185 CP(*ki, *ki32, ki_lock);
1186 CP(*ki, *ki32, ki_rqindex);
1187 CP(*ki, *ki32, ki_oncpu);
1188 CP(*ki, *ki32, ki_lastcpu);
1190 /* XXX TODO: wrap cpu value as appropriate */
1191 CP(*ki, *ki32, ki_oncpu_old);
1192 CP(*ki, *ki32, ki_lastcpu_old);
1194 bcopy(ki->ki_tdname, ki32->ki_tdname, TDNAMLEN + 1);
1195 bcopy(ki->ki_wmesg, ki32->ki_wmesg, WMESGLEN + 1);
1196 bcopy(ki->ki_login, ki32->ki_login, LOGNAMELEN + 1);
1197 bcopy(ki->ki_lockname, ki32->ki_lockname, LOCKNAMELEN + 1);
1198 bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1);
1199 bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1);
1200 bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1);
1201 CP(*ki, *ki32, ki_tracer);
1202 CP(*ki, *ki32, ki_flag2);
1203 CP(*ki, *ki32, ki_fibnum);
1204 CP(*ki, *ki32, ki_cr_flags);
1205 CP(*ki, *ki32, ki_jid);
1206 CP(*ki, *ki32, ki_numthreads);
1207 CP(*ki, *ki32, ki_tid);
1208 CP(*ki, *ki32, ki_pri);
1209 freebsd32_rusage_out(&ki->ki_rusage, &ki32->ki_rusage);
1210 freebsd32_rusage_out(&ki->ki_rusage_ch, &ki32->ki_rusage_ch);
1211 PTRTRIM_CP(*ki, *ki32, ki_pcb);
1212 PTRTRIM_CP(*ki, *ki32, ki_kstack);
1213 PTRTRIM_CP(*ki, *ki32, ki_udata);
1214 CP(*ki, *ki32, ki_sflag);
1215 CP(*ki, *ki32, ki_tdflags);
1220 kern_proc_out(struct proc *p, struct sbuf *sb, int flags)
1223 struct kinfo_proc ki;
1224 #ifdef COMPAT_FREEBSD32
1225 struct kinfo_proc32 ki32;
1229 PROC_LOCK_ASSERT(p, MA_OWNED);
1230 MPASS(FIRST_THREAD_IN_PROC(p) != NULL);
1233 fill_kinfo_proc(p, &ki);
1234 if ((flags & KERN_PROC_NOTHREADS) != 0) {
1235 #ifdef COMPAT_FREEBSD32
1236 if ((flags & KERN_PROC_MASK32) != 0) {
1237 freebsd32_kinfo_proc_out(&ki, &ki32);
1238 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1242 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1245 FOREACH_THREAD_IN_PROC(p, td) {
1246 fill_kinfo_thread(td, &ki, 1);
1247 #ifdef COMPAT_FREEBSD32
1248 if ((flags & KERN_PROC_MASK32) != 0) {
1249 freebsd32_kinfo_proc_out(&ki, &ki32);
1250 if (sbuf_bcat(sb, &ki32, sizeof(ki32)) != 0)
1254 if (sbuf_bcat(sb, &ki, sizeof(ki)) != 0)
1265 sysctl_out_proc(struct proc *p, struct sysctl_req *req, int flags,
1269 struct kinfo_proc ki;
1275 sbuf_new_for_sysctl(&sb, (char *)&ki, sizeof(ki), req);
1276 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1277 error = kern_proc_out(p, &sb, flags);
1278 error2 = sbuf_finish(&sb);
1282 else if (error2 != 0)
1302 sysctl_kern_proc(SYSCTL_HANDLER_ARGS)
1304 int *name = (int *)arg1;
1305 u_int namelen = arg2;
1307 int flags, doingzomb, oid_number;
1310 oid_number = oidp->oid_number;
1311 if (oid_number != KERN_PROC_ALL &&
1312 (oid_number & KERN_PROC_INC_THREAD) == 0)
1313 flags = KERN_PROC_NOTHREADS;
1316 oid_number &= ~KERN_PROC_INC_THREAD;
1318 #ifdef COMPAT_FREEBSD32
1319 if (req->flags & SCTL_MASK32)
1320 flags |= KERN_PROC_MASK32;
1322 if (oid_number == KERN_PROC_PID) {
1325 error = sysctl_wire_old_buffer(req, 0);
1328 sx_slock(&proctree_lock);
1329 error = pget((pid_t)name[0], PGET_CANSEE, &p);
1331 error = sysctl_out_proc(p, req, flags, 0);
1332 sx_sunlock(&proctree_lock);
1336 switch (oid_number) {
1341 case KERN_PROC_PROC:
1342 if (namelen != 0 && namelen != 1)
1352 /* overestimate by 5 procs */
1353 error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5);
1357 error = sysctl_wire_old_buffer(req, 0);
1360 sx_slock(&proctree_lock);
1361 sx_slock(&allproc_lock);
1362 for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) {
1364 p = LIST_FIRST(&allproc);
1366 p = LIST_FIRST(&zombproc);
1367 for (; p != 0; p = LIST_NEXT(p, p_list)) {
1369 * Skip embryonic processes.
1372 if (p->p_state == PRS_NEW) {
1376 KASSERT(p->p_ucred != NULL,
1377 ("process credential is NULL for non-NEW proc"));
1379 * Show a user only appropriate processes.
1381 if (p_cansee(curthread, p)) {
1386 * TODO - make more efficient (see notes below).
1389 switch (oid_number) {
1392 if (p->p_ucred->cr_gid != (gid_t)name[0]) {
1398 case KERN_PROC_PGRP:
1399 /* could do this by traversing pgrp */
1400 if (p->p_pgrp == NULL ||
1401 p->p_pgrp->pg_id != (pid_t)name[0]) {
1407 case KERN_PROC_RGID:
1408 if (p->p_ucred->cr_rgid != (gid_t)name[0]) {
1414 case KERN_PROC_SESSION:
1415 if (p->p_session == NULL ||
1416 p->p_session->s_sid != (pid_t)name[0]) {
1423 if ((p->p_flag & P_CONTROLT) == 0 ||
1424 p->p_session == NULL) {
1428 /* XXX proctree_lock */
1429 SESS_LOCK(p->p_session);
1430 if (p->p_session->s_ttyp == NULL ||
1431 tty_udev(p->p_session->s_ttyp) !=
1433 SESS_UNLOCK(p->p_session);
1437 SESS_UNLOCK(p->p_session);
1441 if (p->p_ucred->cr_uid != (uid_t)name[0]) {
1447 case KERN_PROC_RUID:
1448 if (p->p_ucred->cr_ruid != (uid_t)name[0]) {
1454 case KERN_PROC_PROC:
1462 error = sysctl_out_proc(p, req, flags, doingzomb);
1464 sx_sunlock(&allproc_lock);
1465 sx_sunlock(&proctree_lock);
1470 sx_sunlock(&allproc_lock);
1471 sx_sunlock(&proctree_lock);
1476 pargs_alloc(int len)
1480 pa = malloc(sizeof(struct pargs) + len, M_PARGS,
1482 refcount_init(&pa->ar_ref, 1);
1483 pa->ar_length = len;
1488 pargs_free(struct pargs *pa)
1495 pargs_hold(struct pargs *pa)
1500 refcount_acquire(&pa->ar_ref);
1504 pargs_drop(struct pargs *pa)
1509 if (refcount_release(&pa->ar_ref))
1514 proc_read_mem(struct thread *td, struct proc *p, vm_offset_t offset, void* buf,
1520 iov.iov_base = (caddr_t)buf;
1524 uio.uio_offset = offset;
1525 uio.uio_resid = (ssize_t)len;
1526 uio.uio_segflg = UIO_SYSSPACE;
1527 uio.uio_rw = UIO_READ;
1530 return (proc_rwmem(p, &uio));
1534 proc_read_string(struct thread *td, struct proc *p, const char *sptr, char *buf,
1540 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, len);
1542 * Reading the chunk may validly return EFAULT if the string is shorter
1543 * than the chunk and is aligned at the end of the page, assuming the
1544 * next page is not mapped. So if EFAULT is returned do a fallback to
1545 * one byte read loop.
1547 if (error == EFAULT) {
1548 for (i = 0; i < len; i++, buf++, sptr++) {
1549 error = proc_read_mem(td, p, (vm_offset_t)sptr, buf, 1);
1560 #define PROC_AUXV_MAX 256 /* Safety limit on auxv size. */
1562 enum proc_vector_type {
1568 #ifdef COMPAT_FREEBSD32
1570 get_proc_vector32(struct thread *td, struct proc *p, char ***proc_vectorp,
1571 size_t *vsizep, enum proc_vector_type type)
1573 struct freebsd32_ps_strings pss;
1575 vm_offset_t vptr, ptr;
1576 uint32_t *proc_vector32;
1581 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1587 vptr = (vm_offset_t)PTRIN(pss.ps_argvstr);
1588 vsize = pss.ps_nargvstr;
1589 if (vsize > ARG_MAX)
1591 size = vsize * sizeof(int32_t);
1594 vptr = (vm_offset_t)PTRIN(pss.ps_envstr);
1595 vsize = pss.ps_nenvstr;
1596 if (vsize > ARG_MAX)
1598 size = vsize * sizeof(int32_t);
1601 vptr = (vm_offset_t)PTRIN(pss.ps_envstr) +
1602 (pss.ps_nenvstr + 1) * sizeof(int32_t);
1605 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1606 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1609 if (aux.a_type == AT_NULL)
1613 if (aux.a_type != AT_NULL)
1616 size = vsize * sizeof(aux);
1619 KASSERT(0, ("Wrong proc vector type: %d", type));
1622 proc_vector32 = malloc(size, M_TEMP, M_WAITOK);
1623 error = proc_read_mem(td, p, vptr, proc_vector32, size);
1626 if (type == PROC_AUX) {
1627 *proc_vectorp = (char **)proc_vector32;
1631 proc_vector = malloc(vsize * sizeof(char *), M_TEMP, M_WAITOK);
1632 for (i = 0; i < (int)vsize; i++)
1633 proc_vector[i] = PTRIN(proc_vector32[i]);
1634 *proc_vectorp = proc_vector;
1637 free(proc_vector32, M_TEMP);
1643 get_proc_vector(struct thread *td, struct proc *p, char ***proc_vectorp,
1644 size_t *vsizep, enum proc_vector_type type)
1646 struct ps_strings pss;
1648 vm_offset_t vptr, ptr;
1653 #ifdef COMPAT_FREEBSD32
1654 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1655 return (get_proc_vector32(td, p, proc_vectorp, vsizep, type));
1657 error = proc_read_mem(td, p, (vm_offset_t)(p->p_sysent->sv_psstrings),
1663 vptr = (vm_offset_t)pss.ps_argvstr;
1664 vsize = pss.ps_nargvstr;
1665 if (vsize > ARG_MAX)
1667 size = vsize * sizeof(char *);
1670 vptr = (vm_offset_t)pss.ps_envstr;
1671 vsize = pss.ps_nenvstr;
1672 if (vsize > ARG_MAX)
1674 size = vsize * sizeof(char *);
1678 * The aux array is just above env array on the stack. Check
1679 * that the address is naturally aligned.
1681 vptr = (vm_offset_t)pss.ps_envstr + (pss.ps_nenvstr + 1)
1683 #if __ELF_WORD_SIZE == 64
1684 if (vptr % sizeof(uint64_t) != 0)
1686 if (vptr % sizeof(uint32_t) != 0)
1690 * We count the array size reading the aux vectors from the
1691 * stack until AT_NULL vector is returned. So (to keep the code
1692 * simple) we read the process stack twice: the first time here
1693 * to find the size and the second time when copying the vectors
1694 * to the allocated proc_vector.
1696 for (ptr = vptr, i = 0; i < PROC_AUXV_MAX; i++) {
1697 error = proc_read_mem(td, p, ptr, &aux, sizeof(aux));
1700 if (aux.a_type == AT_NULL)
1705 * If the PROC_AUXV_MAX entries are iterated over, and we have
1706 * not reached AT_NULL, it is most likely we are reading wrong
1707 * data: either the process doesn't have auxv array or data has
1708 * been modified. Return the error in this case.
1710 if (aux.a_type != AT_NULL)
1713 size = vsize * sizeof(aux);
1716 KASSERT(0, ("Wrong proc vector type: %d", type));
1717 return (EINVAL); /* In case we are built without INVARIANTS. */
1719 proc_vector = malloc(size, M_TEMP, M_WAITOK);
1720 if (proc_vector == NULL)
1722 error = proc_read_mem(td, p, vptr, proc_vector, size);
1724 free(proc_vector, M_TEMP);
1727 *proc_vectorp = proc_vector;
1733 #define GET_PS_STRINGS_CHUNK_SZ 256 /* Chunk size (bytes) for ps_strings operations. */
1736 get_ps_strings(struct thread *td, struct proc *p, struct sbuf *sb,
1737 enum proc_vector_type type)
1739 size_t done, len, nchr, vsize;
1741 char **proc_vector, *sptr;
1742 char pss_string[GET_PS_STRINGS_CHUNK_SZ];
1744 PROC_ASSERT_HELD(p);
1747 * We are not going to read more than 2 * (PATH_MAX + ARG_MAX) bytes.
1749 nchr = 2 * (PATH_MAX + ARG_MAX);
1751 error = get_proc_vector(td, p, &proc_vector, &vsize, type);
1754 for (done = 0, i = 0; i < (int)vsize && done < nchr; i++) {
1756 * The program may have scribbled into its argv array, e.g. to
1757 * remove some arguments. If that has happened, break out
1758 * before trying to read from NULL.
1760 if (proc_vector[i] == NULL)
1762 for (sptr = proc_vector[i]; ; sptr += GET_PS_STRINGS_CHUNK_SZ) {
1763 error = proc_read_string(td, p, sptr, pss_string,
1764 sizeof(pss_string));
1767 len = strnlen(pss_string, GET_PS_STRINGS_CHUNK_SZ);
1768 if (done + len >= nchr)
1769 len = nchr - done - 1;
1770 sbuf_bcat(sb, pss_string, len);
1771 if (len != GET_PS_STRINGS_CHUNK_SZ)
1773 done += GET_PS_STRINGS_CHUNK_SZ;
1775 sbuf_bcat(sb, "", 1);
1779 free(proc_vector, M_TEMP);
1784 proc_getargv(struct thread *td, struct proc *p, struct sbuf *sb)
1787 return (get_ps_strings(curthread, p, sb, PROC_ARG));
1791 proc_getenvv(struct thread *td, struct proc *p, struct sbuf *sb)
1794 return (get_ps_strings(curthread, p, sb, PROC_ENV));
1798 proc_getauxv(struct thread *td, struct proc *p, struct sbuf *sb)
1804 error = get_proc_vector(td, p, &auxv, &vsize, PROC_AUX);
1806 #ifdef COMPAT_FREEBSD32
1807 if (SV_PROC_FLAG(p, SV_ILP32) != 0)
1808 size = vsize * sizeof(Elf32_Auxinfo);
1811 size = vsize * sizeof(Elf_Auxinfo);
1812 if (sbuf_bcat(sb, auxv, size) != 0)
1820 * This sysctl allows a process to retrieve the argument list or process
1821 * title for another process without groping around in the address space
1822 * of the other process. It also allow a process to set its own "process
1823 * title to a string of its own choice.
1826 sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS)
1828 int *name = (int *)arg1;
1829 u_int namelen = arg2;
1830 struct pargs *newpa, *pa;
1833 int flags, error = 0, error2;
1838 flags = PGET_CANSEE;
1839 if (req->newptr != NULL)
1840 flags |= PGET_ISCURRENT;
1841 error = pget((pid_t)name[0], flags, &p);
1849 error = SYSCTL_OUT(req, pa->ar_args, pa->ar_length);
1851 } else if ((p->p_flag & (P_WEXIT | P_SYSTEM)) == 0) {
1854 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1855 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1856 error = proc_getargv(curthread, p, &sb);
1857 error2 = sbuf_finish(&sb);
1860 if (error == 0 && error2 != 0)
1865 if (error != 0 || req->newptr == NULL)
1868 if (req->newlen + sizeof(struct pargs) > ps_arg_cache_limit)
1870 newpa = pargs_alloc(req->newlen);
1871 error = SYSCTL_IN(req, newpa->ar_args, req->newlen);
1885 * This sysctl allows a process to retrieve environment of another process.
1888 sysctl_kern_proc_env(SYSCTL_HANDLER_ARGS)
1890 int *name = (int *)arg1;
1891 u_int namelen = arg2;
1899 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1902 if ((p->p_flag & P_SYSTEM) != 0) {
1907 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1908 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1909 error = proc_getenvv(curthread, p, &sb);
1910 error2 = sbuf_finish(&sb);
1913 return (error != 0 ? error : error2);
1917 * This sysctl allows a process to retrieve ELF auxiliary vector of
1921 sysctl_kern_proc_auxv(SYSCTL_HANDLER_ARGS)
1923 int *name = (int *)arg1;
1924 u_int namelen = arg2;
1932 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
1935 if ((p->p_flag & P_SYSTEM) != 0) {
1939 sbuf_new_for_sysctl(&sb, NULL, GET_PS_STRINGS_CHUNK_SZ, req);
1940 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
1941 error = proc_getauxv(curthread, p, &sb);
1942 error2 = sbuf_finish(&sb);
1945 return (error != 0 ? error : error2);
1949 * This sysctl allows a process to retrieve the path of the executable for
1950 * itself or another process.
1953 sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS)
1955 pid_t *pidp = (pid_t *)arg1;
1956 unsigned int arglen = arg2;
1959 char *retbuf, *freebuf;
1964 if (*pidp == -1) { /* -1 means this process */
1965 p = req->td->td_proc;
1967 error = pget(*pidp, PGET_CANSEE, &p);
1981 error = vn_fullpath(req->td, vp, &retbuf, &freebuf);
1985 error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1);
1986 free(freebuf, M_TEMP);
1991 sysctl_kern_proc_sv_name(SYSCTL_HANDLER_ARGS)
2004 error = pget((pid_t)name[0], PGET_CANSEE, &p);
2007 sv_name = p->p_sysent->sv_name;
2009 return (sysctl_handle_string(oidp, sv_name, 0, req));
2012 #ifdef KINFO_OVMENTRY_SIZE
2013 CTASSERT(sizeof(struct kinfo_ovmentry) == KINFO_OVMENTRY_SIZE);
2016 #ifdef COMPAT_FREEBSD7
2018 sysctl_kern_proc_ovmmap(SYSCTL_HANDLER_ARGS)
2020 vm_map_entry_t entry, tmp_entry;
2021 unsigned int last_timestamp;
2022 char *fullpath, *freepath;
2023 struct kinfo_ovmentry *kve;
2033 error = pget((pid_t)name[0], PGET_WANTREAD, &p);
2036 vm = vmspace_acquire_ref(p);
2041 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2044 vm_map_lock_read(map);
2045 for (entry = map->header.next; entry != &map->header;
2046 entry = entry->next) {
2047 vm_object_t obj, tobj, lobj;
2050 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2053 bzero(kve, sizeof(*kve));
2054 kve->kve_structsize = sizeof(*kve);
2056 kve->kve_private_resident = 0;
2057 obj = entry->object.vm_object;
2059 VM_OBJECT_RLOCK(obj);
2060 if (obj->shadow_count == 1)
2061 kve->kve_private_resident =
2062 obj->resident_page_count;
2064 kve->kve_resident = 0;
2065 addr = entry->start;
2066 while (addr < entry->end) {
2067 if (pmap_extract(map->pmap, addr))
2068 kve->kve_resident++;
2072 for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
2074 VM_OBJECT_RLOCK(tobj);
2076 VM_OBJECT_RUNLOCK(lobj);
2080 kve->kve_start = (void*)entry->start;
2081 kve->kve_end = (void*)entry->end;
2082 kve->kve_offset = (off_t)entry->offset;
2084 if (entry->protection & VM_PROT_READ)
2085 kve->kve_protection |= KVME_PROT_READ;
2086 if (entry->protection & VM_PROT_WRITE)
2087 kve->kve_protection |= KVME_PROT_WRITE;
2088 if (entry->protection & VM_PROT_EXECUTE)
2089 kve->kve_protection |= KVME_PROT_EXEC;
2091 if (entry->eflags & MAP_ENTRY_COW)
2092 kve->kve_flags |= KVME_FLAG_COW;
2093 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2094 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2095 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2096 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2098 last_timestamp = map->timestamp;
2099 vm_map_unlock_read(map);
2101 kve->kve_fileid = 0;
2107 switch (lobj->type) {
2109 kve->kve_type = KVME_TYPE_DEFAULT;
2112 kve->kve_type = KVME_TYPE_VNODE;
2117 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2118 kve->kve_type = KVME_TYPE_VNODE;
2119 if ((lobj->flags & OBJ_TMPFS) != 0) {
2120 vp = lobj->un_pager.swp.swp_tmpfs;
2124 kve->kve_type = KVME_TYPE_SWAP;
2128 kve->kve_type = KVME_TYPE_DEVICE;
2131 kve->kve_type = KVME_TYPE_PHYS;
2134 kve->kve_type = KVME_TYPE_DEAD;
2137 kve->kve_type = KVME_TYPE_SG;
2140 kve->kve_type = KVME_TYPE_UNKNOWN;
2144 VM_OBJECT_RUNLOCK(lobj);
2146 kve->kve_ref_count = obj->ref_count;
2147 kve->kve_shadow_count = obj->shadow_count;
2148 VM_OBJECT_RUNLOCK(obj);
2150 vn_fullpath(curthread, vp, &fullpath,
2152 cred = curthread->td_ucred;
2153 vn_lock(vp, LK_SHARED | LK_RETRY);
2154 if (VOP_GETATTR(vp, &va, cred) == 0) {
2155 kve->kve_fileid = va.va_fileid;
2156 kve->kve_fsid = va.va_fsid;
2161 kve->kve_type = KVME_TYPE_NONE;
2162 kve->kve_ref_count = 0;
2163 kve->kve_shadow_count = 0;
2166 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2167 if (freepath != NULL)
2168 free(freepath, M_TEMP);
2170 error = SYSCTL_OUT(req, kve, sizeof(*kve));
2171 vm_map_lock_read(map);
2174 if (last_timestamp != map->timestamp) {
2175 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2179 vm_map_unlock_read(map);
2185 #endif /* COMPAT_FREEBSD7 */
2187 #ifdef KINFO_VMENTRY_SIZE
2188 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2192 kern_proc_vmmap_resident(vm_map_t map, vm_map_entry_t entry,
2193 struct kinfo_vmentry *kve)
2195 vm_object_t obj, tobj;
2198 vm_paddr_t locked_pa;
2199 vm_pindex_t pi, pi_adv, pindex;
2202 obj = entry->object.vm_object;
2203 addr = entry->start;
2205 pi = OFF_TO_IDX(entry->offset);
2206 for (; addr < entry->end; addr += IDX_TO_OFF(pi_adv), pi += pi_adv) {
2207 if (m_adv != NULL) {
2210 pi_adv = OFF_TO_IDX(entry->end - addr);
2212 for (tobj = obj;; tobj = tobj->backing_object) {
2213 m = vm_page_find_least(tobj, pindex);
2215 if (m->pindex == pindex)
2217 if (pi_adv > m->pindex - pindex) {
2218 pi_adv = m->pindex - pindex;
2222 if (tobj->backing_object == NULL)
2224 pindex += OFF_TO_IDX(tobj->
2225 backing_object_offset);
2229 if (m->psind != 0 && addr + pagesizes[1] <= entry->end &&
2230 (addr & (pagesizes[1] - 1)) == 0 &&
2231 (pmap_mincore(map->pmap, addr, &locked_pa) &
2232 MINCORE_SUPER) != 0) {
2233 kve->kve_flags |= KVME_FLAG_SUPER;
2234 pi_adv = OFF_TO_IDX(pagesizes[1]);
2237 * We do not test the found page on validity.
2238 * Either the page is busy and being paged in,
2239 * or it was invalidated. The first case
2240 * should be counted as resident, the second
2241 * is not so clear; we do account both.
2245 kve->kve_resident += pi_adv;
2248 PA_UNLOCK_COND(locked_pa);
2252 * Must be called with the process locked and will return unlocked.
2255 kern_proc_vmmap_out(struct proc *p, struct sbuf *sb)
2257 vm_map_entry_t entry, tmp_entry;
2260 vm_object_t obj, tobj, lobj;
2261 char *fullpath, *freepath;
2262 struct kinfo_vmentry *kve;
2267 unsigned int last_timestamp;
2270 PROC_LOCK_ASSERT(p, MA_OWNED);
2274 vm = vmspace_acquire_ref(p);
2279 kve = malloc(sizeof(*kve), M_TEMP, M_WAITOK);
2283 vm_map_lock_read(map);
2284 for (entry = map->header.next; entry != &map->header;
2285 entry = entry->next) {
2286 if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2290 bzero(kve, sizeof(*kve));
2291 obj = entry->object.vm_object;
2293 for (tobj = obj; tobj != NULL;
2294 tobj = tobj->backing_object) {
2295 VM_OBJECT_RLOCK(tobj);
2298 if (obj->backing_object == NULL)
2299 kve->kve_private_resident =
2300 obj->resident_page_count;
2301 if (!vmmap_skip_res_cnt)
2302 kern_proc_vmmap_resident(map, entry, kve);
2303 for (tobj = obj; tobj != NULL;
2304 tobj = tobj->backing_object) {
2305 if (tobj != obj && tobj != lobj)
2306 VM_OBJECT_RUNLOCK(tobj);
2312 kve->kve_start = entry->start;
2313 kve->kve_end = entry->end;
2314 kve->kve_offset = entry->offset;
2316 if (entry->protection & VM_PROT_READ)
2317 kve->kve_protection |= KVME_PROT_READ;
2318 if (entry->protection & VM_PROT_WRITE)
2319 kve->kve_protection |= KVME_PROT_WRITE;
2320 if (entry->protection & VM_PROT_EXECUTE)
2321 kve->kve_protection |= KVME_PROT_EXEC;
2323 if (entry->eflags & MAP_ENTRY_COW)
2324 kve->kve_flags |= KVME_FLAG_COW;
2325 if (entry->eflags & MAP_ENTRY_NEEDS_COPY)
2326 kve->kve_flags |= KVME_FLAG_NEEDS_COPY;
2327 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
2328 kve->kve_flags |= KVME_FLAG_NOCOREDUMP;
2329 if (entry->eflags & MAP_ENTRY_GROWS_UP)
2330 kve->kve_flags |= KVME_FLAG_GROWS_UP;
2331 if (entry->eflags & MAP_ENTRY_GROWS_DOWN)
2332 kve->kve_flags |= KVME_FLAG_GROWS_DOWN;
2334 last_timestamp = map->timestamp;
2335 vm_map_unlock_read(map);
2341 switch (lobj->type) {
2343 kve->kve_type = KVME_TYPE_DEFAULT;
2346 kve->kve_type = KVME_TYPE_VNODE;
2351 if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
2352 kve->kve_type = KVME_TYPE_VNODE;
2353 if ((lobj->flags & OBJ_TMPFS) != 0) {
2354 vp = lobj->un_pager.swp.swp_tmpfs;
2358 kve->kve_type = KVME_TYPE_SWAP;
2362 kve->kve_type = KVME_TYPE_DEVICE;
2365 kve->kve_type = KVME_TYPE_PHYS;
2368 kve->kve_type = KVME_TYPE_DEAD;
2371 kve->kve_type = KVME_TYPE_SG;
2373 case OBJT_MGTDEVICE:
2374 kve->kve_type = KVME_TYPE_MGTDEVICE;
2377 kve->kve_type = KVME_TYPE_UNKNOWN;
2381 VM_OBJECT_RUNLOCK(lobj);
2383 kve->kve_ref_count = obj->ref_count;
2384 kve->kve_shadow_count = obj->shadow_count;
2385 VM_OBJECT_RUNLOCK(obj);
2387 vn_fullpath(curthread, vp, &fullpath,
2389 kve->kve_vn_type = vntype_to_kinfo(vp->v_type);
2390 cred = curthread->td_ucred;
2391 vn_lock(vp, LK_SHARED | LK_RETRY);
2392 if (VOP_GETATTR(vp, &va, cred) == 0) {
2393 kve->kve_vn_fileid = va.va_fileid;
2394 kve->kve_vn_fsid = va.va_fsid;
2396 MAKEIMODE(va.va_type, va.va_mode);
2397 kve->kve_vn_size = va.va_size;
2398 kve->kve_vn_rdev = va.va_rdev;
2399 kve->kve_status = KF_ATTR_VALID;
2404 kve->kve_type = KVME_TYPE_NONE;
2405 kve->kve_ref_count = 0;
2406 kve->kve_shadow_count = 0;
2409 strlcpy(kve->kve_path, fullpath, sizeof(kve->kve_path));
2410 if (freepath != NULL)
2411 free(freepath, M_TEMP);
2413 /* Pack record size down */
2414 kve->kve_structsize = offsetof(struct kinfo_vmentry, kve_path) +
2415 strlen(kve->kve_path) + 1;
2416 kve->kve_structsize = roundup(kve->kve_structsize,
2418 if (sbuf_bcat(sb, kve, kve->kve_structsize) != 0)
2420 vm_map_lock_read(map);
2423 if (last_timestamp != map->timestamp) {
2424 vm_map_lookup_entry(map, addr - 1, &tmp_entry);
2428 vm_map_unlock_read(map);
2436 sysctl_kern_proc_vmmap(SYSCTL_HANDLER_ARGS)
2440 int error, error2, *name;
2443 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_vmentry), req);
2444 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
2445 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p);
2450 error = kern_proc_vmmap_out(p, &sb);
2451 error2 = sbuf_finish(&sb);
2453 return (error != 0 ? error : error2);
2456 #if defined(STACK) || defined(DDB)
2458 sysctl_kern_proc_kstack(SYSCTL_HANDLER_ARGS)
2460 struct kinfo_kstack *kkstp;
2461 int error, i, *name, numthreads;
2462 lwpid_t *lwpidarray;
2469 error = pget((pid_t)name[0], PGET_NOTINEXEC | PGET_WANTREAD, &p);
2473 kkstp = malloc(sizeof(*kkstp), M_TEMP, M_WAITOK);
2474 st = stack_create();
2480 if (numthreads < p->p_numthreads) {
2481 if (lwpidarray != NULL) {
2482 free(lwpidarray, M_TEMP);
2485 numthreads = p->p_numthreads;
2487 lwpidarray = malloc(sizeof(*lwpidarray) * numthreads, M_TEMP,
2495 * XXXRW: During the below loop, execve(2) and countless other sorts
2496 * of changes could have taken place. Should we check to see if the
2497 * vmspace has been replaced, or the like, in order to prevent
2498 * giving a snapshot that spans, say, execve(2), with some threads
2499 * before and some after? Among other things, the credentials could
2500 * have changed, in which case the right to extract debug info might
2501 * no longer be assured.
2503 FOREACH_THREAD_IN_PROC(p, td) {
2504 KASSERT(i < numthreads,
2505 ("sysctl_kern_proc_kstack: numthreads"));
2506 lwpidarray[i] = td->td_tid;
2510 for (i = 0; i < numthreads; i++) {
2511 td = thread_find(p, lwpidarray[i]);
2515 bzero(kkstp, sizeof(*kkstp));
2516 (void)sbuf_new(&sb, kkstp->kkst_trace,
2517 sizeof(kkstp->kkst_trace), SBUF_FIXEDLEN);
2519 kkstp->kkst_tid = td->td_tid;
2520 if (TD_IS_SWAPPED(td)) {
2521 kkstp->kkst_state = KKST_STATE_SWAPPED;
2522 } else if (TD_IS_RUNNING(td)) {
2523 if (stack_save_td_running(st, td) == 0)
2524 kkstp->kkst_state = KKST_STATE_STACKOK;
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;
2570 error = pget(*pidp, PGET_CANSEE, &p);
2575 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_proc(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");
2929 struct proc *cp, *p;
2931 bool restart, seen_stopped, seen_exiting, stopped_some;
2935 * stop_all_proc() assumes that all process which have
2936 * usermode must be stopped, except current process, for
2937 * obvious reasons. Since other threads in the process
2938 * establishing global stop could unstop something, disable
2939 * calls from multithreaded processes as precaution. The
2940 * service must not be user-callable anyway.
2942 KASSERT((cp->p_flag & P_HADTHREADS) == 0 ||
2943 (cp->p_flag & P_KTHREAD) != 0, ("mt stop_all_proc"));
2946 sx_xlock(&allproc_lock);
2948 seen_exiting = seen_stopped = stopped_some = restart = false;
2949 LIST_REMOVE(cp, p_list);
2950 LIST_INSERT_HEAD(&allproc, cp, p_list);
2952 p = LIST_NEXT(cp, p_list);
2955 LIST_REMOVE(cp, p_list);
2956 LIST_INSERT_AFTER(p, cp, p_list);
2958 if ((p->p_flag & (P_KTHREAD | P_SYSTEM |
2959 P_TOTAL_STOP)) != 0) {
2963 if ((p->p_flag & P_WEXIT) != 0) {
2964 seen_exiting = true;
2968 if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE) {
2970 * Stopped processes are tolerated when there
2971 * are no other processes which might continue
2972 * them. P_STOPPED_SINGLE but not
2973 * P_TOTAL_STOP process still has at least one
2976 seen_stopped = true;
2981 sx_xunlock(&allproc_lock);
2982 r = thread_single(p, SINGLE_ALLPROC);
2986 stopped_some = true;
2989 sx_xlock(&allproc_lock);
2991 /* Catch forked children we did not see in iteration. */
2992 if (gen != allproc_gen)
2994 sx_xunlock(&allproc_lock);
2995 if (restart || stopped_some || seen_exiting || seen_stopped) {
2996 kern_yield(PRI_USER);
3002 resume_all_proc(void)
3004 struct proc *cp, *p;
3007 sx_xlock(&allproc_lock);
3008 LIST_REMOVE(cp, p_list);
3009 LIST_INSERT_HEAD(&allproc, cp, p_list);
3011 p = LIST_NEXT(cp, p_list);
3014 LIST_REMOVE(cp, p_list);
3015 LIST_INSERT_AFTER(p, cp, p_list);
3017 if ((p->p_flag & P_TOTAL_STOP) != 0) {
3018 sx_xunlock(&allproc_lock);
3020 thread_single_end(p, SINGLE_ALLPROC);
3023 sx_xlock(&allproc_lock);
3028 sx_xunlock(&allproc_lock);
3031 #define TOTAL_STOP_DEBUG 1
3032 #ifdef TOTAL_STOP_DEBUG
3033 volatile static int ap_resume;
3034 #include <sys/mount.h>
3037 sysctl_debug_stop_all_proc(SYSCTL_HANDLER_ARGS)
3043 error = sysctl_handle_int(oidp, &val, 0, req);
3044 if (error != 0 || req->newptr == NULL)
3049 while (ap_resume == 0)
3057 SYSCTL_PROC(_debug, OID_AUTO, stop_all_proc, CTLTYPE_INT | CTLFLAG_RW |
3058 CTLFLAG_MPSAFE, __DEVOLATILE(int *, &ap_resume), 0,
3059 sysctl_debug_stop_all_proc, "I",