11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1989, 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software developed by the Computer Systems
61573Srgrimes * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
71573Srgrimes * BG 91-66 and contributed to Berkeley.
81573Srgrimes *
91573Srgrimes * Redistribution and use in source and binary forms, with or without
101573Srgrimes * modification, are permitted provided that the following conditions
111573Srgrimes * are met:
121573Srgrimes * 1. Redistributions of source code must retain the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
34103385Sbde#if 0
351573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
361573Srgrimesstatic char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
371573Srgrimes#endif /* LIBC_SCCS and not lint */
38103385Sbde#endif
391573Srgrimes
40103385Sbde#include <sys/cdefs.h>
41103385Sbde__FBSDID("$FreeBSD$");
42103385Sbde
431573Srgrimes/*
441573Srgrimes * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
451573Srgrimes * users of this code, so we've factored it out into a separate module.
461573Srgrimes * Thus, we keep this grunge out of the other kvm applications (i.e.,
471573Srgrimes * most other applications are interested only in open/close/read/nlist).
481573Srgrimes */
491573Srgrimes
501573Srgrimes#include <sys/param.h>
51143871Spjd#define	_WANT_UCRED	/* make ucred.h give us 'struct ucred' */
52101968Salfred#include <sys/ucred.h>
53143871Spjd#include <sys/queue.h>
54143871Spjd#include <sys/_lock.h>
55143871Spjd#include <sys/_mutex.h>
56143871Spjd#include <sys/_task.h>
57185435Sbz#include <sys/cpuset.h>
58185435Sbz#include <sys/user.h>
59185435Sbz#include <sys/proc.h>
60143871Spjd#define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
61143871Spjd#include <sys/jail.h>
621573Srgrimes#include <sys/exec.h>
631573Srgrimes#include <sys/stat.h>
64130728Sgad#include <sys/sysent.h>
651573Srgrimes#include <sys/ioctl.h>
661573Srgrimes#include <sys/tty.h>
672029Sdg#include <sys/file.h>
68136402Speter#include <sys/conf.h>
6917141Sjkh#include <stdio.h>
7017141Sjkh#include <stdlib.h>
711573Srgrimes#include <unistd.h>
721573Srgrimes#include <nlist.h>
731573Srgrimes#include <kvm.h>
741573Srgrimes
751573Srgrimes#include <sys/sysctl.h>
761573Srgrimes
771573Srgrimes#include <limits.h>
7812682Speter#include <memory.h>
791573Srgrimes#include <paths.h>
801573Srgrimes
811573Srgrimes#include "kvm_private.h"
821573Srgrimes
831573Srgrimes#define KREAD(kd, addr, obj) \
841573Srgrimes	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
851573Srgrimes
86172265Sjeffstatic int ticks;
87172265Sjeffstatic int hz;
88217745Suqsstatic uint64_t cpu_tick_frequency;
89172265Sjeff
901573Srgrimes/*
91217745Suqs * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is
92217745Suqs * read/initialized before this function is ever called.
93217745Suqs */
94217745Suqsstatic uint64_t
95217745Suqscputick2usec(uint64_t tick)
96217745Suqs{
97217745Suqs
98217745Suqs	if (cpu_tick_frequency == 0)
99217745Suqs		return (0);
100217745Suqs	if (tick > 18446744073709551)		/* floor(2^64 / 1000) */
101217745Suqs		return (tick / (cpu_tick_frequency / 1000000));
102217745Suqs	else if (tick > 18446744073709)	/* floor(2^64 / 1000000) */
103217745Suqs		return ((tick * 1000) / (cpu_tick_frequency / 1000));
104217745Suqs	else
105217745Suqs		return ((tick * 1000000) / cpu_tick_frequency);
106217745Suqs}
107217745Suqs
108217745Suqs/*
1091573Srgrimes * Read proc's from memory file into buffer bp, which has space to hold
1101573Srgrimes * at most maxcnt procs.
1111573Srgrimes */
1121573Srgrimesstatic int
113217744Suqskvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
114217744Suqs    struct kinfo_proc *bp, int maxcnt)
1151573Srgrimes{
11692913Sobrien	int cnt = 0;
11769896Smckusick	struct kinfo_proc kinfo_proc, *kp;
1181573Srgrimes	struct pgrp pgrp;
1191573Srgrimes	struct session sess;
120136402Speter	struct cdev t_cdev;
1211573Srgrimes	struct tty tty;
12269896Smckusick	struct vmspace vmspace;
123114996Sjhb	struct sigacts sigacts;
124217744Suqs#if 0
12569896Smckusick	struct pstats pstats;
126217744Suqs#endif
12769896Smckusick	struct ucred ucred;
128143871Spjd	struct prison pr;
129103216Sjulian	struct thread mtd;
1301573Srgrimes	struct proc proc;
13126947Stegge	struct proc pproc;
132130728Sgad	struct sysentvec sysent;
133130728Sgad	char svname[KI_EMULNAMELEN];
1341573Srgrimes
13569896Smckusick	kp = &kinfo_proc;
13669896Smckusick	kp->ki_structsize = sizeof(kinfo_proc);
137173004Sjulian	/*
138173004Sjulian	 * Loop on the processes. this is completely broken because we need to be
139173004Sjulian	 * able to loop on the threads and merge the ones that are the same process some how.
140173004Sjulian	 */
14170525Sben	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
14282266Speter		memset(kp, 0, sizeof *kp);
1431573Srgrimes		if (KREAD(kd, (u_long)p, &proc)) {
144217744Suqs			_kvm_err(kd, kd->program, "can't read proc at %p", p);
1451573Srgrimes			return (-1);
1461573Srgrimes		}
147241302Savg		if (proc.p_state == PRS_NEW)
148241302Savg			continue;
14999157Sjulian		if (proc.p_state != PRS_ZOMBIE) {
15099157Sjulian			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
151103216Sjulian			    &mtd)) {
15299157Sjulian				_kvm_err(kd, kd->program,
153217744Suqs				    "can't read thread at %p",
15499157Sjulian				    TAILQ_FIRST(&proc.p_threads));
15599157Sjulian				return (-1);
15699157Sjulian			}
15790360Sjulian		}
15877183Srwatson		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
15977183Srwatson			kp->ki_ruid = ucred.cr_ruid;
16077183Srwatson			kp->ki_svuid = ucred.cr_svuid;
16177183Srwatson			kp->ki_rgid = ucred.cr_rgid;
16277183Srwatson			kp->ki_svgid = ucred.cr_svgid;
163195843Sbrooks			kp->ki_cr_flags = ucred.cr_flags;
164195843Sbrooks			if (ucred.cr_ngroups > KI_NGROUPS) {
165195843Sbrooks				kp->ki_ngroups = KI_NGROUPS;
166195843Sbrooks				kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
167200366Sbrooks			} else
168195843Sbrooks				kp->ki_ngroups = ucred.cr_ngroups;
169196990Sbrooks			kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
170195843Sbrooks			    kp->ki_ngroups * sizeof(gid_t));
17169896Smckusick			kp->ki_uid = ucred.cr_uid;
172143871Spjd			if (ucred.cr_prison != NULL) {
173143871Spjd				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
174143871Spjd					_kvm_err(kd, kd->program,
175217744Suqs					    "can't read prison at %p",
176143871Spjd					    ucred.cr_prison);
177143871Spjd					return (-1);
178143871Spjd				}
179143871Spjd				kp->ki_jid = pr.pr_id;
180143871Spjd			}
18169896Smckusick		}
1821573Srgrimes
183126126Sdeischen		switch(what & ~KERN_PROC_INC_THREAD) {
1848870Srgrimes
185130728Sgad		case KERN_PROC_GID:
186130728Sgad			if (kp->ki_groups[0] != (gid_t)arg)
187130728Sgad				continue;
188130728Sgad			break;
189130728Sgad
1901573Srgrimes		case KERN_PROC_PID:
1911573Srgrimes			if (proc.p_pid != (pid_t)arg)
1921573Srgrimes				continue;
1931573Srgrimes			break;
1941573Srgrimes
195130728Sgad		case KERN_PROC_RGID:
196130728Sgad			if (kp->ki_rgid != (gid_t)arg)
197130728Sgad				continue;
198130728Sgad			break;
199130728Sgad
2001573Srgrimes		case KERN_PROC_UID:
20169896Smckusick			if (kp->ki_uid != (uid_t)arg)
2021573Srgrimes				continue;
2031573Srgrimes			break;
2041573Srgrimes
2051573Srgrimes		case KERN_PROC_RUID:
20669896Smckusick			if (kp->ki_ruid != (uid_t)arg)
2071573Srgrimes				continue;
2081573Srgrimes			break;
2091573Srgrimes		}
2101573Srgrimes		/*
2111573Srgrimes		 * We're going to add another proc to the set.  If this
2121573Srgrimes		 * will overflow the buffer, assume the reason is because
2131573Srgrimes		 * nprocs (or the proc list) is corrupt and declare an error.
2141573Srgrimes		 */
2151573Srgrimes		if (cnt >= maxcnt) {
2161573Srgrimes			_kvm_err(kd, kd->program, "nprocs corrupt");
2171573Srgrimes			return (-1);
2181573Srgrimes		}
2191573Srgrimes		/*
22069896Smckusick		 * gather kinfo_proc
2211573Srgrimes		 */
22269896Smckusick		kp->ki_paddr = p;
223137907Sdas		kp->ki_addr = 0;	/* XXX uarea */
22483366Sjulian		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
22569896Smckusick		kp->ki_args = proc.p_args;
226112204Sjhb		kp->ki_tracep = proc.p_tracevp;
22769896Smckusick		kp->ki_textvp = proc.p_textvp;
22869896Smckusick		kp->ki_fd = proc.p_fd;
22969896Smckusick		kp->ki_vmspace = proc.p_vmspace;
230114996Sjhb		if (proc.p_sigacts != NULL) {
231114996Sjhb			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
23269896Smckusick				_kvm_err(kd, kd->program,
233217744Suqs				    "can't read sigacts at %p", proc.p_sigacts);
23469896Smckusick				return (-1);
23569896Smckusick			}
236114996Sjhb			kp->ki_sigignore = sigacts.ps_sigignore;
237114996Sjhb			kp->ki_sigcatch = sigacts.ps_sigcatch;
23869896Smckusick		}
239170178Sjeff#if 0
240172207Sjeff		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
24169896Smckusick			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
24269896Smckusick				_kvm_err(kd, kd->program,
24369896Smckusick				    "can't read stats at %x", proc.p_stats);
24469896Smckusick				return (-1);
24569896Smckusick			}
24669896Smckusick			kp->ki_start = pstats.p_start;
247136195Sjhb
248136195Sjhb			/*
249136195Sjhb			 * XXX: The times here are probably zero and need
250136195Sjhb			 * to be calculated from the raw data in p_rux and
251136195Sjhb			 * p_crux.
252136195Sjhb			 */
25369896Smckusick			kp->ki_rusage = pstats.p_ru;
254130728Sgad			kp->ki_childstime = pstats.p_cru.ru_stime;
255130728Sgad			kp->ki_childutime = pstats.p_cru.ru_utime;
256130728Sgad			/* Some callers want child-times in a single value */
257130728Sgad			timeradd(&kp->ki_childstime, &kp->ki_childutime,
258130728Sgad			    &kp->ki_childtime);
25969896Smckusick		}
260170178Sjeff#endif
26126947Stegge		if (proc.p_oppid)
26269896Smckusick			kp->ki_ppid = proc.p_oppid;
26326947Stegge		else if (proc.p_pptr) {
26469896Smckusick			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
26569896Smckusick				_kvm_err(kd, kd->program,
266217744Suqs				    "can't read pproc at %p", proc.p_pptr);
26769896Smckusick				return (-1);
26869896Smckusick			}
26969896Smckusick			kp->ki_ppid = pproc.p_pid;
27026947Stegge		} else
27169896Smckusick			kp->ki_ppid = 0;
27282266Speter		if (proc.p_pgrp == NULL)
27382266Speter			goto nopgrp;
27482266Speter		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
275217744Suqs			_kvm_err(kd, kd->program, "can't read pgrp at %p",
27682266Speter				 proc.p_pgrp);
27782266Speter			return (-1);
27882266Speter		}
27969896Smckusick		kp->ki_pgid = pgrp.pg_id;
28069896Smckusick		kp->ki_jobc = pgrp.pg_jobc;
2811573Srgrimes		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
282217744Suqs			_kvm_err(kd, kd->program, "can't read session at %p",
2831573Srgrimes				pgrp.pg_session);
2841573Srgrimes			return (-1);
2851573Srgrimes		}
28669896Smckusick		kp->ki_sid = sess.s_sid;
28769896Smckusick		(void)memcpy(kp->ki_login, sess.s_login,
28869896Smckusick						sizeof(kp->ki_login));
28969896Smckusick		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
29069896Smckusick		if (sess.s_leader == p)
29169896Smckusick			kp->ki_kiflag |= KI_SLEADER;
2921573Srgrimes		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
2931573Srgrimes			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
2941573Srgrimes				_kvm_err(kd, kd->program,
295217744Suqs					 "can't read tty at %p", sess.s_ttyp);
2961573Srgrimes				return (-1);
2971573Srgrimes			}
298136402Speter			if (tty.t_dev != NULL) {
299136402Speter				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
300136402Speter					_kvm_err(kd, kd->program,
301217744Suqs						 "can't read cdev at %p",
302136402Speter						tty.t_dev);
303136402Speter					return (-1);
304136402Speter				}
305143648Sphk#if 0
306136402Speter				kp->ki_tdev = t_cdev.si_udev;
307143648Sphk#else
308147567Speter				kp->ki_tdev = NODEV;
309143648Sphk#endif
310136402Speter			}
3111573Srgrimes			if (tty.t_pgrp != NULL) {
3121573Srgrimes				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
3131573Srgrimes					_kvm_err(kd, kd->program,
314217744Suqs						 "can't read tpgrp at %p",
3151573Srgrimes						tty.t_pgrp);
3161573Srgrimes					return (-1);
3171573Srgrimes				}
31869896Smckusick				kp->ki_tpgid = pgrp.pg_id;
3191573Srgrimes			} else
32069896Smckusick				kp->ki_tpgid = -1;
32169896Smckusick			if (tty.t_session != NULL) {
32269896Smckusick				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
32369896Smckusick					_kvm_err(kd, kd->program,
324217744Suqs					    "can't read session at %p",
32569896Smckusick					    tty.t_session);
32669896Smckusick					return (-1);
32769896Smckusick				}
32869896Smckusick				kp->ki_tsid = sess.s_sid;
32969896Smckusick			}
33082266Speter		} else {
33182266Speternopgrp:
33269896Smckusick			kp->ki_tdev = NODEV;
33382266Speter		}
334103216Sjulian		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
335103216Sjulian			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
33669896Smckusick			    kp->ki_wmesg, WMESGLEN);
3371573Srgrimes
3381573Srgrimes		(void)kvm_read(kd, (u_long)proc.p_vmspace,
33969896Smckusick		    (char *)&vmspace, sizeof(vmspace));
34069896Smckusick		kp->ki_size = vmspace.vm_map.size;
341209276Ssbruno		/*
342209276Ssbruno		 * Approximate the kernel's method of calculating
343209276Ssbruno		 * this field.
344209276Ssbruno		 */
345209276Ssbruno#define		pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
346209276Ssbruno		kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
34769896Smckusick		kp->ki_swrss = vmspace.vm_swrss;
34869896Smckusick		kp->ki_tsize = vmspace.vm_tsize;
34969896Smckusick		kp->ki_dsize = vmspace.vm_dsize;
35069896Smckusick		kp->ki_ssize = vmspace.vm_ssize;
3511573Srgrimes
352126126Sdeischen		switch (what & ~KERN_PROC_INC_THREAD) {
3531573Srgrimes
3541573Srgrimes		case KERN_PROC_PGRP:
35569896Smckusick			if (kp->ki_pgid != (pid_t)arg)
3561573Srgrimes				continue;
3571573Srgrimes			break;
3581573Srgrimes
359130728Sgad		case KERN_PROC_SESSION:
360130728Sgad			if (kp->ki_sid != (pid_t)arg)
361130728Sgad				continue;
362130728Sgad			break;
363130728Sgad
3641573Srgrimes		case KERN_PROC_TTY:
3658870Srgrimes			if ((proc.p_flag & P_CONTROLT) == 0 ||
36669896Smckusick			     kp->ki_tdev != (dev_t)arg)
3671573Srgrimes				continue;
3681573Srgrimes			break;
3691573Srgrimes		}
370130992Sgad		if (proc.p_comm[0] != 0)
371130992Sgad			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
372130728Sgad		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
373130728Sgad		    sizeof(sysent));
374130728Sgad		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
375130728Sgad		    sizeof(svname));
376130728Sgad		if (svname[0] != 0)
377130728Sgad			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
37899142Sjulian		if ((proc.p_state != PRS_ZOMBIE) &&
379103216Sjulian		    (mtd.td_blocked != 0)) {
380104388Sjhb			kp->ki_kiflag |= KI_LOCKBLOCK;
381104388Sjhb			if (mtd.td_lockname)
38299142Sjulian				(void)kvm_read(kd,
383104388Sjhb				    (u_long)mtd.td_lockname,
384104388Sjhb				    kp->ki_lockname, LOCKNAMELEN);
385104388Sjhb			kp->ki_lockname[LOCKNAMELEN] = 0;
38669896Smckusick		}
387217745Suqs		kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
38869896Smckusick		kp->ki_pid = proc.p_pid;
389104307Sjmallett		kp->ki_siglist = proc.p_siglist;
390112923Sjeff		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
391112891Sjeff		kp->ki_sigmask = mtd.td_sigmask;
39269896Smckusick		kp->ki_xstat = proc.p_xstat;
39369896Smckusick		kp->ki_acflag = proc.p_acflag;
394103367Sjulian		kp->ki_lock = proc.p_lock;
39599142Sjulian		if (proc.p_state != PRS_ZOMBIE) {
396172265Sjeff			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
39799128Sjulian			kp->ki_flag = proc.p_flag;
398172207Sjeff			kp->ki_sflag = 0;
399130552Sjulian			kp->ki_nice = proc.p_nice;
40099128Sjulian			kp->ki_traceflag = proc.p_traceflag;
40199128Sjulian			if (proc.p_state == PRS_NORMAL) {
402103216Sjulian				if (TD_ON_RUNQ(&mtd) ||
403103216Sjulian				    TD_CAN_RUN(&mtd) ||
404103216Sjulian				    TD_IS_RUNNING(&mtd)) {
40599128Sjulian					kp->ki_stat = SRUN;
406103367Sjulian				} else if (mtd.td_state ==
407103367Sjulian				    TDS_INHIBITED) {
408103216Sjulian					if (P_SHOULDSTOP(&proc)) {
409103216Sjulian						kp->ki_stat = SSTOP;
410103367Sjulian					} else if (
411103367Sjulian					    TD_IS_SLEEPING(&mtd)) {
412103216Sjulian						kp->ki_stat = SSLEEP;
413104388Sjhb					} else if (TD_ON_LOCK(&mtd)) {
414104388Sjhb						kp->ki_stat = SLOCK;
415103216Sjulian					} else {
416103216Sjulian						kp->ki_stat = SWAIT;
417103216Sjulian					}
41899128Sjulian				}
41999072Sjulian			} else {
42099128Sjulian				kp->ki_stat = SIDL;
42199072Sjulian			}
422103367Sjulian			/* Stuff from the thread */
423103216Sjulian			kp->ki_pri.pri_level = mtd.td_priority;
424103216Sjulian			kp->ki_pri.pri_native = mtd.td_base_pri;
425103216Sjulian			kp->ki_lastcpu = mtd.td_lastcpu;
426103367Sjulian			kp->ki_wchan = mtd.td_wchan;
427173004Sjulian			if (mtd.td_name[0] != 0)
428224199Sbz				strlcpy(kp->ki_tdname, mtd.td_name, MAXCOMLEN);
429113340Sjulian			kp->ki_oncpu = mtd.td_oncpu;
430173004Sjulian			if (mtd.td_name[0] != '\0')
431224199Sbz				strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname));
432177089Sjeff			kp->ki_pctcpu = 0;
433177089Sjeff			kp->ki_rqindex = 0;
43499128Sjulian		} else {
43599072Sjulian			kp->ki_stat = SZOMB;
43699072Sjulian		}
43769896Smckusick		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
4381573Srgrimes		++bp;
4391573Srgrimes		++cnt;
4401573Srgrimes	}
4411573Srgrimes	return (cnt);
4421573Srgrimes}
4431573Srgrimes
4441573Srgrimes/*
4451573Srgrimes * Build proc info array by reading in proc list from a crash dump.
4461573Srgrimes * Return number of procs read.  maxcnt is the max we will read.
4471573Srgrimes */
4481573Srgrimesstatic int
449217744Suqskvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
450217744Suqs    u_long a_zombproc, int maxcnt)
4511573Srgrimes{
45292913Sobrien	struct kinfo_proc *bp = kd->procbase;
45392913Sobrien	int acnt, zcnt;
4541573Srgrimes	struct proc *p;
4551573Srgrimes
4561573Srgrimes	if (KREAD(kd, a_allproc, &p)) {
4571573Srgrimes		_kvm_err(kd, kd->program, "cannot read allproc");
4581573Srgrimes		return (-1);
4591573Srgrimes	}
4601573Srgrimes	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
4611573Srgrimes	if (acnt < 0)
4621573Srgrimes		return (acnt);
4631573Srgrimes
4641573Srgrimes	if (KREAD(kd, a_zombproc, &p)) {
4651573Srgrimes		_kvm_err(kd, kd->program, "cannot read zombproc");
4661573Srgrimes		return (-1);
4671573Srgrimes	}
4681573Srgrimes	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
4691573Srgrimes	if (zcnt < 0)
4701573Srgrimes		zcnt = 0;
4711573Srgrimes
4721573Srgrimes	return (acnt + zcnt);
4731573Srgrimes}
4741573Srgrimes
4751573Srgrimesstruct kinfo_proc *
476217744Suqskvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
4771573Srgrimes{
47838534Sdfr	int mib[4], st, nprocs;
479230873Strociny	size_t size, osize;
480126126Sdeischen	int temp_op;
4811573Srgrimes
4821573Srgrimes	if (kd->procbase != 0) {
4831573Srgrimes		free((void *)kd->procbase);
4848870Srgrimes		/*
4851573Srgrimes		 * Clear this pointer in case this call fails.  Otherwise,
4861573Srgrimes		 * kvm_close() will free it again.
4871573Srgrimes		 */
4881573Srgrimes		kd->procbase = 0;
4891573Srgrimes	}
4901573Srgrimes	if (ISALIVE(kd)) {
4911573Srgrimes		size = 0;
4921573Srgrimes		mib[0] = CTL_KERN;
4931573Srgrimes		mib[1] = KERN_PROC;
4941573Srgrimes		mib[2] = op;
4951573Srgrimes		mib[3] = arg;
496126126Sdeischen		temp_op = op & ~KERN_PROC_INC_THREAD;
497126126Sdeischen		st = sysctl(mib,
498126126Sdeischen		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
499120500Stjr		    3 : 4, NULL, &size, NULL, 0);
5001573Srgrimes		if (st == -1) {
5011573Srgrimes			_kvm_syserr(kd, kd->program, "kvm_getprocs");
5021573Srgrimes			return (0);
5031573Srgrimes		}
50494028Sdd		/*
50594028Sdd		 * We can't continue with a size of 0 because we pass
50694028Sdd		 * it to realloc() (via _kvm_realloc()), and passing 0
50794028Sdd		 * to realloc() results in undefined behavior.
50894028Sdd		 */
50994028Sdd		if (size == 0) {
51094028Sdd			/*
51194028Sdd			 * XXX: We should probably return an invalid,
51294028Sdd			 * but non-NULL, pointer here so any client
51394028Sdd			 * program trying to dereference it will
51494028Sdd			 * crash.  However, _kvm_freeprocs() calls
51594028Sdd			 * free() on kd->procbase if it isn't NULL,
51694028Sdd			 * and free()'ing a junk pointer isn't good.
51794028Sdd			 * Then again, _kvm_freeprocs() isn't used
51894028Sdd			 * anywhere . . .
51994028Sdd			 */
52094028Sdd			kd->procbase = _kvm_malloc(kd, 1);
52194028Sdd			goto liveout;
52294028Sdd		}
52340268Sdes		do {
52440268Sdes			size += size / 10;
52540268Sdes			kd->procbase = (struct kinfo_proc *)
52641880Sbde			    _kvm_realloc(kd, kd->procbase, size);
52740268Sdes			if (kd->procbase == 0)
52840268Sdes				return (0);
529230873Strociny			osize = size;
530126126Sdeischen			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
531126126Sdeischen			    temp_op == KERN_PROC_PROC ? 3 : 4,
53241880Sbde			    kd->procbase, &size, NULL, 0);
533230873Strociny		} while (st == -1 && errno == ENOMEM && size == osize);
5341573Srgrimes		if (st == -1) {
5351573Srgrimes			_kvm_syserr(kd, kd->program, "kvm_getprocs");
5361573Srgrimes			return (0);
5371573Srgrimes		}
53894028Sdd		/*
53994028Sdd		 * We have to check the size again because sysctl()
54094028Sdd		 * may "round up" oldlenp if oldp is NULL; hence it
54194028Sdd		 * might've told us that there was data to get when
54294028Sdd		 * there really isn't any.
54394028Sdd		 */
54476182Sdwmalone		if (size > 0 &&
54576182Sdwmalone		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
5461573Srgrimes			_kvm_err(kd, kd->program,
547217744Suqs			    "kinfo_proc size mismatch (expected %zu, got %d)",
54869896Smckusick			    sizeof(struct kinfo_proc),
54969896Smckusick			    kd->procbase->ki_structsize);
5501573Srgrimes			return (0);
5511573Srgrimes		}
55294028Sddliveout:
55376243Sdwmalone		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
5541573Srgrimes	} else {
555217745Suqs		struct nlist nl[7], *p;
5561573Srgrimes
5571573Srgrimes		nl[0].n_name = "_nprocs";
5581573Srgrimes		nl[1].n_name = "_allproc";
5591573Srgrimes		nl[2].n_name = "_zombproc";
560172265Sjeff		nl[3].n_name = "_ticks";
561172265Sjeff		nl[4].n_name = "_hz";
562217745Suqs		nl[5].n_name = "_cpu_tick_frequency";
563217745Suqs		nl[6].n_name = 0;
5641573Srgrimes
5651573Srgrimes		if (kvm_nlist(kd, nl) != 0) {
5661573Srgrimes			for (p = nl; p->n_type != 0; ++p)
5671573Srgrimes				;
5681573Srgrimes			_kvm_err(kd, kd->program,
5691573Srgrimes				 "%s: no such symbol", p->n_name);
5701573Srgrimes			return (0);
5711573Srgrimes		}
5721573Srgrimes		if (KREAD(kd, nl[0].n_value, &nprocs)) {
5731573Srgrimes			_kvm_err(kd, kd->program, "can't read nprocs");
5741573Srgrimes			return (0);
5751573Srgrimes		}
576172265Sjeff		if (KREAD(kd, nl[3].n_value, &ticks)) {
577172265Sjeff			_kvm_err(kd, kd->program, "can't read ticks");
578172265Sjeff			return (0);
579172265Sjeff		}
580172265Sjeff		if (KREAD(kd, nl[4].n_value, &hz)) {
581172265Sjeff			_kvm_err(kd, kd->program, "can't read hz");
582172265Sjeff			return (0);
583172265Sjeff		}
584217745Suqs		if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) {
585217745Suqs			_kvm_err(kd, kd->program,
586217745Suqs			    "can't read cpu_tick_frequency");
587217745Suqs			return (0);
588217745Suqs		}
5891573Srgrimes		size = nprocs * sizeof(struct kinfo_proc);
5901573Srgrimes		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
5911573Srgrimes		if (kd->procbase == 0)
5921573Srgrimes			return (0);
5931573Srgrimes
5941573Srgrimes		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
5951573Srgrimes				      nl[2].n_value, nprocs);
596241303Savg		if (nprocs <= 0) {
597241303Savg			_kvm_freeprocs(kd);
598241303Savg			nprocs = 0;
599241303Savg		}
6001573Srgrimes#ifdef notdef
601241303Savg		else {
602241303Savg			size = nprocs * sizeof(struct kinfo_proc);
603241303Savg			kd->procbase = realloc(kd->procbase, size);
604241303Savg		}
6051573Srgrimes#endif
6061573Srgrimes	}
6071573Srgrimes	*cnt = nprocs;
6081573Srgrimes	return (kd->procbase);
6091573Srgrimes}
6101573Srgrimes
6111573Srgrimesvoid
612217744Suqs_kvm_freeprocs(kvm_t *kd)
6131573Srgrimes{
6141573Srgrimes	if (kd->procbase) {
6151573Srgrimes		free(kd->procbase);
6161573Srgrimes		kd->procbase = 0;
6171573Srgrimes	}
6181573Srgrimes}
6191573Srgrimes
6201573Srgrimesvoid *
621217744Suqs_kvm_realloc(kvm_t *kd, void *p, size_t n)
6221573Srgrimes{
6231573Srgrimes	void *np = (void *)realloc(p, n);
6241573Srgrimes
62539327Simp	if (np == 0) {
62639327Simp		free(p);
6271573Srgrimes		_kvm_err(kd, kd->program, "out of memory");
62839327Simp	}
6291573Srgrimes	return (np);
6301573Srgrimes}
6311573Srgrimes
6321573Srgrimes/*
633227839Strociny * Get the command args or environment.
6341573Srgrimes */
6351573Srgrimesstatic char **
636227839Strocinykvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
6371573Srgrimes{
63853239Sphk	int oid[4];
63958642Sobrien	int i;
64058642Sobrien	size_t bufsz;
641227839Strociny	static int buflen;
64253239Sphk	static char *buf, *p;
64353239Sphk	static char **bufp;
64453239Sphk	static int argc;
64553239Sphk
64655127Speter	if (!ISALIVE(kd)) {
64755127Speter		_kvm_err(kd, kd->program,
64855127Speter		    "cannot read user space from dead kernel");
64955127Speter		return (0);
65055127Speter	}
65155127Speter
652227839Strociny	if (nchr == 0 || nchr > ARG_MAX)
653227839Strociny		nchr = ARG_MAX;
654227839Strociny	if (buflen == 0) {
655227839Strociny		buf = malloc(nchr);
656227839Strociny		if (buf == NULL) {
657227839Strociny			_kvm_err(kd, kd->program, "cannot allocate memory");
658227839Strociny			return (0);
65953239Sphk		}
660227839Strociny		buflen = nchr;
661227839Strociny		argc = 32;
662227839Strociny		bufp = malloc(sizeof(char *) * argc);
663227839Strociny	} else if (nchr > buflen) {
664227839Strociny		p = realloc(buf, nchr);
665227839Strociny		if (p != NULL) {
666227839Strociny			buf = p;
667227839Strociny			buflen = nchr;
668227839Strociny		}
66953239Sphk	}
670230146Strociny	oid[0] = CTL_KERN;
671230146Strociny	oid[1] = KERN_PROC;
672230146Strociny	oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
673230146Strociny	oid[3] = kp->ki_pid;
674230146Strociny	bufsz = buflen;
675230146Strociny	if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) {
676230146Strociny		/*
677230146Strociny		 * If the supplied buf is too short to hold the requested
678230146Strociny		 * value the sysctl returns with ENOMEM. The buf is filled
679230146Strociny		 * with the truncated value and the returned bufsz is equal
680230146Strociny		 * to the requested len.
681230146Strociny		 */
682230146Strociny		if (errno != ENOMEM || bufsz != (size_t)buflen)
683230146Strociny			return (0);
684230146Strociny		buf[bufsz - 1] = '\0';
685230146Strociny		errno = 0;
686230146Strociny	} else if (bufsz == 0) {
687230146Strociny		return (0);
688230146Strociny	}
689230146Strociny	i = 0;
690230146Strociny	p = buf;
691230146Strociny	do {
692230146Strociny		bufp[i++] = p;
693230146Strociny		p += strlen(p) + 1;
694230146Strociny		if (i >= argc) {
695230146Strociny			argc += argc;
696230146Strociny			bufp = realloc(bufp,
697230146Strociny			    sizeof(char *) * argc);
69853239Sphk		}
699230146Strociny	} while (p < buf + bufsz);
700230146Strociny	bufp[i++] = 0;
701230146Strociny	return (bufp);
7021573Srgrimes}
7031573Srgrimes
7041573Srgrimeschar **
705227839Strocinykvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
7061573Srgrimes{
707227839Strociny	return (kvm_argv(kd, kp, 0, nchr));
7081573Srgrimes}
7091573Srgrimes
710227839Strocinychar **
711227839Strocinykvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
7121573Srgrimes{
713227839Strociny	return (kvm_argv(kd, kp, 1, nchr));
7141573Srgrimes}
715