xref: /freebsd/lib/libkvm/kvm_proc.c (revision f8197bf090767fb53abaf0ac777e9a6dc9ee112a)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1989, 1992, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software developed by the Computer Systems
658f0484fSRodney W. Grimes  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
758f0484fSRodney W. Grimes  * BG 91-66 and contributed to Berkeley.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  */
3758f0484fSRodney W. Grimes 
38f86e3350SBruce Evans #if 0
3958f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
4058f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
42f86e3350SBruce Evans #endif
43f86e3350SBruce Evans 
44f86e3350SBruce Evans #include <sys/cdefs.h>
45f86e3350SBruce Evans __FBSDID("$FreeBSD$");
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes /*
4858f0484fSRodney W. Grimes  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
4958f0484fSRodney W. Grimes  * users of this code, so we've factored it out into a separate module.
5058f0484fSRodney W. Grimes  * Thus, we keep this grunge out of the other kvm applications (i.e.,
5158f0484fSRodney W. Grimes  * most other applications are interested only in open/close/read/nlist).
5258f0484fSRodney W. Grimes  */
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes #include <sys/param.h>
55b20ea179SAlfred Perlstein #define	_WANT_UCRED	/* make ucred.h give us 'struct ucred' */
56aa22cbfeSAlfred Perlstein #include <sys/ucred.h>
57f8197bf0SPawel Jakub Dawidek #include <sys/queue.h>
58f8197bf0SPawel Jakub Dawidek #include <sys/_lock.h>
59f8197bf0SPawel Jakub Dawidek #include <sys/_mutex.h>
60f8197bf0SPawel Jakub Dawidek #include <sys/_task.h>
61f8197bf0SPawel Jakub Dawidek #define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
62f8197bf0SPawel Jakub Dawidek #include <sys/jail.h>
6358f0484fSRodney W. Grimes #include <sys/user.h>
6458f0484fSRodney W. Grimes #include <sys/proc.h>
6558f0484fSRodney W. Grimes #include <sys/exec.h>
6658f0484fSRodney W. Grimes #include <sys/stat.h>
67276de18cSGarance A Drosehn #include <sys/sysent.h>
6858f0484fSRodney W. Grimes #include <sys/ioctl.h>
6958f0484fSRodney W. Grimes #include <sys/tty.h>
70338c7541SDavid Greenman #include <sys/file.h>
712bdd5609SPeter Wemm #include <sys/conf.h>
7251295a4dSJordan K. Hubbard #include <stdio.h>
7351295a4dSJordan K. Hubbard #include <stdlib.h>
7458f0484fSRodney W. Grimes #include <unistd.h>
7558f0484fSRodney W. Grimes #include <nlist.h>
7658f0484fSRodney W. Grimes #include <kvm.h>
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes #include <vm/vm.h>
7958f0484fSRodney W. Grimes #include <vm/vm_param.h>
8058f0484fSRodney W. Grimes 
8158f0484fSRodney W. Grimes #include <sys/sysctl.h>
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes #include <limits.h>
8477721f53SPeter Wemm #include <memory.h>
8558f0484fSRodney W. Grimes #include <paths.h>
8658f0484fSRodney W. Grimes 
8758f0484fSRodney W. Grimes #include "kvm_private.h"
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \
9058f0484fSRodney W. Grimes 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
9158f0484fSRodney W. Grimes 
9258f0484fSRodney W. Grimes /*
9358f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
9458f0484fSRodney W. Grimes  * at most maxcnt procs.
9558f0484fSRodney W. Grimes  */
9658f0484fSRodney W. Grimes static int
9758f0484fSRodney W. Grimes kvm_proclist(kd, what, arg, p, bp, maxcnt)
9858f0484fSRodney W. Grimes 	kvm_t *kd;
9958f0484fSRodney W. Grimes 	int what, arg;
10058f0484fSRodney W. Grimes 	struct proc *p;
10158f0484fSRodney W. Grimes 	struct kinfo_proc *bp;
10258f0484fSRodney W. Grimes 	int maxcnt;
10358f0484fSRodney W. Grimes {
104be04b6d1SDavid E. O'Brien 	int cnt = 0;
1051f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc, *kp;
10658f0484fSRodney W. Grimes 	struct pgrp pgrp;
10758f0484fSRodney W. Grimes 	struct session sess;
1082bdd5609SPeter Wemm 	struct cdev t_cdev;
10958f0484fSRodney W. Grimes 	struct tty tty;
1101f7d2501SKirk McKusick 	struct vmspace vmspace;
111840558b9SJohn Baldwin 	struct sigacts sigacts;
1121f7d2501SKirk McKusick 	struct pstats pstats;
1131f7d2501SKirk McKusick 	struct ucred ucred;
114f8197bf0SPawel Jakub Dawidek 	struct prison pr;
11571fad9fdSJulian Elischer 	struct thread mtd;
116ed062c8dSJulian Elischer 	/*struct kse mke;*/
1174f0db5e0SJulian Elischer 	struct ksegrp mkg;
11858f0484fSRodney W. Grimes 	struct proc proc;
119a58930d8STor Egge 	struct proc pproc;
12049b33de8SBrian Feldman 	struct timeval tv;
121276de18cSGarance A Drosehn 	struct sysentvec sysent;
122276de18cSGarance A Drosehn 	char svname[KI_EMULNAMELEN];
12358f0484fSRodney W. Grimes 
1241f7d2501SKirk McKusick 	kp = &kinfo_proc;
1251f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(kinfo_proc);
12642680b3aSBen Smithurst 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
1276f8c6a69SPeter Wemm 		memset(kp, 0, sizeof *kp);
12858f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
12958f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read proc at %x", p);
13058f0484fSRodney W. Grimes 			return (-1);
13158f0484fSRodney W. Grimes 		}
13258551c03SJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
1333daf63fcSJulian Elischer 			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
13471fad9fdSJulian Elischer 			    &mtd)) {
13558551c03SJulian Elischer 				_kvm_err(kd, kd->program,
13658551c03SJulian Elischer 				    "can't read thread at %x",
1373daf63fcSJulian Elischer 				    TAILQ_FIRST(&proc.p_threads));
1383daf63fcSJulian Elischer 				return (-1);
1393daf63fcSJulian Elischer 			}
1401566329bSGarance A Drosehn 			if ((proc.p_flag & P_SA) == 0) {
1414f0db5e0SJulian Elischer 				if (KREAD(kd,
1424f0db5e0SJulian Elischer 				    (u_long)TAILQ_FIRST(&proc.p_ksegrps),
1434f0db5e0SJulian Elischer 				    &mkg)) {
1444f0db5e0SJulian Elischer 					_kvm_err(kd, kd->program,
1454f0db5e0SJulian Elischer 					    "can't read ksegrp at %x",
1464f0db5e0SJulian Elischer 					    TAILQ_FIRST(&proc.p_ksegrps));
1474f0db5e0SJulian Elischer 					return (-1);
1484f0db5e0SJulian Elischer 				}
149ed062c8dSJulian Elischer #if 0
1504f0db5e0SJulian Elischer 				if (KREAD(kd,
1514f0db5e0SJulian Elischer 				    (u_long)TAILQ_FIRST(&mkg.kg_kseq), &mke)) {
1524f0db5e0SJulian Elischer 					_kvm_err(kd, kd->program,
1534f0db5e0SJulian Elischer 					    "can't read kse at %x",
1544f0db5e0SJulian Elischer 					    TAILQ_FIRST(&mkg.kg_kseq));
1554f0db5e0SJulian Elischer 					return (-1);
1564f0db5e0SJulian Elischer 				}
157ed062c8dSJulian Elischer #endif
1584f0db5e0SJulian Elischer 			}
15958551c03SJulian Elischer 		}
160b1fc0ec1SRobert Watson 		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
161b1fc0ec1SRobert Watson 			kp->ki_ruid = ucred.cr_ruid;
162b1fc0ec1SRobert Watson 			kp->ki_svuid = ucred.cr_svuid;
163b1fc0ec1SRobert Watson 			kp->ki_rgid = ucred.cr_rgid;
164b1fc0ec1SRobert Watson 			kp->ki_svgid = ucred.cr_svgid;
1651f7d2501SKirk McKusick 			kp->ki_ngroups = ucred.cr_ngroups;
1661f7d2501SKirk McKusick 			bcopy(ucred.cr_groups, kp->ki_groups,
1671f7d2501SKirk McKusick 			    NGROUPS * sizeof(gid_t));
1681f7d2501SKirk McKusick 			kp->ki_uid = ucred.cr_uid;
169f8197bf0SPawel Jakub Dawidek 			if (ucred.cr_prison != NULL) {
170f8197bf0SPawel Jakub Dawidek 				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
171f8197bf0SPawel Jakub Dawidek 					_kvm_err(kd, kd->program,
172f8197bf0SPawel Jakub Dawidek 					    "can't read prison at %x",
173f8197bf0SPawel Jakub Dawidek 					    ucred.cr_prison);
174f8197bf0SPawel Jakub Dawidek 					return (-1);
175f8197bf0SPawel Jakub Dawidek 				}
176f8197bf0SPawel Jakub Dawidek 				kp->ki_jid = pr.pr_id;
177f8197bf0SPawel Jakub Dawidek 			}
1781f7d2501SKirk McKusick 		}
17958f0484fSRodney W. Grimes 
180694127f8SDaniel Eischen 		switch(what & ~KERN_PROC_INC_THREAD) {
18158f0484fSRodney W. Grimes 
182276de18cSGarance A Drosehn 		case KERN_PROC_GID:
183276de18cSGarance A Drosehn 			if (kp->ki_groups[0] != (gid_t)arg)
184276de18cSGarance A Drosehn 				continue;
185276de18cSGarance A Drosehn 			break;
186276de18cSGarance A Drosehn 
18758f0484fSRodney W. Grimes 		case KERN_PROC_PID:
18858f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
18958f0484fSRodney W. Grimes 				continue;
19058f0484fSRodney W. Grimes 			break;
19158f0484fSRodney W. Grimes 
192276de18cSGarance A Drosehn 		case KERN_PROC_RGID:
193276de18cSGarance A Drosehn 			if (kp->ki_rgid != (gid_t)arg)
194276de18cSGarance A Drosehn 				continue;
195276de18cSGarance A Drosehn 			break;
196276de18cSGarance A Drosehn 
19758f0484fSRodney W. Grimes 		case KERN_PROC_UID:
1981f7d2501SKirk McKusick 			if (kp->ki_uid != (uid_t)arg)
19958f0484fSRodney W. Grimes 				continue;
20058f0484fSRodney W. Grimes 			break;
20158f0484fSRodney W. Grimes 
20258f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
2031f7d2501SKirk McKusick 			if (kp->ki_ruid != (uid_t)arg)
20458f0484fSRodney W. Grimes 				continue;
20558f0484fSRodney W. Grimes 			break;
20658f0484fSRodney W. Grimes 		}
20758f0484fSRodney W. Grimes 		/*
20858f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
20958f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
21058f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
21158f0484fSRodney W. Grimes 		 */
21258f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
21358f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
21458f0484fSRodney W. Grimes 			return (-1);
21558f0484fSRodney W. Grimes 		}
21658f0484fSRodney W. Grimes 		/*
2171f7d2501SKirk McKusick 		 * gather kinfo_proc
21858f0484fSRodney W. Grimes 		 */
2191f7d2501SKirk McKusick 		kp->ki_paddr = p;
2207a62aa8aSDavid Schultz 		kp->ki_addr = 0;	/* XXX uarea */
221b40ce416SJulian Elischer 		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
2221f7d2501SKirk McKusick 		kp->ki_args = proc.p_args;
2238b7a975eSJohn Baldwin 		kp->ki_tracep = proc.p_tracevp;
2241f7d2501SKirk McKusick 		kp->ki_textvp = proc.p_textvp;
2251f7d2501SKirk McKusick 		kp->ki_fd = proc.p_fd;
2261f7d2501SKirk McKusick 		kp->ki_vmspace = proc.p_vmspace;
227840558b9SJohn Baldwin 		if (proc.p_sigacts != NULL) {
228840558b9SJohn Baldwin 			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
2291f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
230840558b9SJohn Baldwin 				    "can't read sigacts at %x", proc.p_sigacts);
2311f7d2501SKirk McKusick 				return (-1);
2321f7d2501SKirk McKusick 			}
233840558b9SJohn Baldwin 			kp->ki_sigignore = sigacts.ps_sigignore;
234840558b9SJohn Baldwin 			kp->ki_sigcatch = sigacts.ps_sigcatch;
2351f7d2501SKirk McKusick 		}
2366d3e7b9bSJohn Baldwin 		if ((proc.p_sflag & PS_INMEM) && proc.p_stats != NULL) {
2371f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
2381f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2391f7d2501SKirk McKusick 				    "can't read stats at %x", proc.p_stats);
2401f7d2501SKirk McKusick 				return (-1);
2411f7d2501SKirk McKusick 			}
2421f7d2501SKirk McKusick 			kp->ki_start = pstats.p_start;
24309ff687cSJohn Baldwin 
24409ff687cSJohn Baldwin 			/*
24509ff687cSJohn Baldwin 			 * XXX: The times here are probably zero and need
24609ff687cSJohn Baldwin 			 * to be calculated from the raw data in p_rux and
24709ff687cSJohn Baldwin 			 * p_crux.
24809ff687cSJohn Baldwin 			 */
2491f7d2501SKirk McKusick 			kp->ki_rusage = pstats.p_ru;
250276de18cSGarance A Drosehn 			kp->ki_childstime = pstats.p_cru.ru_stime;
251276de18cSGarance A Drosehn 			kp->ki_childutime = pstats.p_cru.ru_utime;
252276de18cSGarance A Drosehn 			/* Some callers want child-times in a single value */
253276de18cSGarance A Drosehn 			timeradd(&kp->ki_childstime, &kp->ki_childutime,
254276de18cSGarance A Drosehn 			    &kp->ki_childtime);
2551f7d2501SKirk McKusick 		}
256a58930d8STor Egge 		if (proc.p_oppid)
2571f7d2501SKirk McKusick 			kp->ki_ppid = proc.p_oppid;
258a58930d8STor Egge 		else if (proc.p_pptr) {
259a58930d8STor Egge 			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
2601f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2611f7d2501SKirk McKusick 				    "can't read pproc at %x", proc.p_pptr);
262a58930d8STor Egge 				return (-1);
263a58930d8STor Egge 			}
2641f7d2501SKirk McKusick 			kp->ki_ppid = pproc.p_pid;
265a58930d8STor Egge 		} else
2661f7d2501SKirk McKusick 			kp->ki_ppid = 0;
2676f8c6a69SPeter Wemm 		if (proc.p_pgrp == NULL)
2686f8c6a69SPeter Wemm 			goto nopgrp;
2696f8c6a69SPeter Wemm 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
2706f8c6a69SPeter Wemm 			_kvm_err(kd, kd->program, "can't read pgrp at %x",
2716f8c6a69SPeter Wemm 				 proc.p_pgrp);
2726f8c6a69SPeter Wemm 			return (-1);
2736f8c6a69SPeter Wemm 		}
2741f7d2501SKirk McKusick 		kp->ki_pgid = pgrp.pg_id;
2751f7d2501SKirk McKusick 		kp->ki_jobc = pgrp.pg_jobc;
27658f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
27758f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read session at %x",
27858f0484fSRodney W. Grimes 				pgrp.pg_session);
27958f0484fSRodney W. Grimes 			return (-1);
28058f0484fSRodney W. Grimes 		}
2811f7d2501SKirk McKusick 		kp->ki_sid = sess.s_sid;
2821f7d2501SKirk McKusick 		(void)memcpy(kp->ki_login, sess.s_login,
2831f7d2501SKirk McKusick 						sizeof(kp->ki_login));
2841f7d2501SKirk McKusick 		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
2851f7d2501SKirk McKusick 		if (sess.s_leader == p)
2861f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_SLEADER;
28758f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
28858f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
28958f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
29058f0484fSRodney W. Grimes 					 "can't read tty at %x", sess.s_ttyp);
29158f0484fSRodney W. Grimes 				return (-1);
29258f0484fSRodney W. Grimes 			}
2932bdd5609SPeter Wemm 			if (tty.t_dev != NULL) {
2942bdd5609SPeter Wemm 				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
2952bdd5609SPeter Wemm 					_kvm_err(kd, kd->program,
2962bdd5609SPeter Wemm 						 "can't read cdev at %x",
2972bdd5609SPeter Wemm 						tty.t_dev);
2982bdd5609SPeter Wemm 					return (-1);
2992bdd5609SPeter Wemm 				}
3009c4fb661SPoul-Henning Kamp #if 0
3012bdd5609SPeter Wemm 				kp->ki_tdev = t_cdev.si_udev;
3029c4fb661SPoul-Henning Kamp #else
3039c4fb661SPoul-Henning Kamp 				kp->ki_tdev = NULL;
3049c4fb661SPoul-Henning Kamp #endif
3052bdd5609SPeter Wemm 			}
30658f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
30758f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
30858f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
309d3cf4981SKris Kennaway 						 "can't read tpgrp at %x",
31058f0484fSRodney W. Grimes 						tty.t_pgrp);
31158f0484fSRodney W. Grimes 					return (-1);
31258f0484fSRodney W. Grimes 				}
3131f7d2501SKirk McKusick 				kp->ki_tpgid = pgrp.pg_id;
31458f0484fSRodney W. Grimes 			} else
3151f7d2501SKirk McKusick 				kp->ki_tpgid = -1;
3161f7d2501SKirk McKusick 			if (tty.t_session != NULL) {
3171f7d2501SKirk McKusick 				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
3181f7d2501SKirk McKusick 					_kvm_err(kd, kd->program,
3191f7d2501SKirk McKusick 					    "can't read session at %x",
3201f7d2501SKirk McKusick 					    tty.t_session);
3211f7d2501SKirk McKusick 					return (-1);
3221f7d2501SKirk McKusick 				}
3231f7d2501SKirk McKusick 				kp->ki_tsid = sess.s_sid;
3241f7d2501SKirk McKusick 			}
3256f8c6a69SPeter Wemm 		} else {
3266f8c6a69SPeter Wemm nopgrp:
3271f7d2501SKirk McKusick 			kp->ki_tdev = NODEV;
3286f8c6a69SPeter Wemm 		}
32971fad9fdSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
33071fad9fdSJulian Elischer 			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
3311f7d2501SKirk McKusick 			    kp->ki_wmesg, WMESGLEN);
33258f0484fSRodney W. Grimes 
33358f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
3341f7d2501SKirk McKusick 		    (char *)&vmspace, sizeof(vmspace));
3351f7d2501SKirk McKusick 		kp->ki_size = vmspace.vm_map.size;
3361f7d2501SKirk McKusick 		kp->ki_rssize = vmspace.vm_swrss; /* XXX */
3371f7d2501SKirk McKusick 		kp->ki_swrss = vmspace.vm_swrss;
3381f7d2501SKirk McKusick 		kp->ki_tsize = vmspace.vm_tsize;
3391f7d2501SKirk McKusick 		kp->ki_dsize = vmspace.vm_dsize;
3401f7d2501SKirk McKusick 		kp->ki_ssize = vmspace.vm_ssize;
34158f0484fSRodney W. Grimes 
342694127f8SDaniel Eischen 		switch (what & ~KERN_PROC_INC_THREAD) {
34358f0484fSRodney W. Grimes 
34458f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
3451f7d2501SKirk McKusick 			if (kp->ki_pgid != (pid_t)arg)
34658f0484fSRodney W. Grimes 				continue;
34758f0484fSRodney W. Grimes 			break;
34858f0484fSRodney W. Grimes 
349276de18cSGarance A Drosehn 		case KERN_PROC_SESSION:
350276de18cSGarance A Drosehn 			if (kp->ki_sid != (pid_t)arg)
351276de18cSGarance A Drosehn 				continue;
352276de18cSGarance A Drosehn 			break;
353276de18cSGarance A Drosehn 
35458f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
35558f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
3561f7d2501SKirk McKusick 			     kp->ki_tdev != (dev_t)arg)
35758f0484fSRodney W. Grimes 				continue;
35858f0484fSRodney W. Grimes 			break;
35958f0484fSRodney W. Grimes 		}
360b7e7c21aSGarance A Drosehn 		if (proc.p_comm[0] != 0)
361b7e7c21aSGarance A Drosehn 			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
362276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
363276de18cSGarance A Drosehn 		    sizeof(sysent));
364276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
365276de18cSGarance A Drosehn 		    sizeof(svname));
366276de18cSGarance A Drosehn 		if (svname[0] != 0)
367276de18cSGarance A Drosehn 			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
368bff4151cSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) &&
36971fad9fdSJulian Elischer 		    (mtd.td_blocked != 0)) {
3700d632649SJohn Baldwin 			kp->ki_kiflag |= KI_LOCKBLOCK;
3710d632649SJohn Baldwin 			if (mtd.td_lockname)
372bff4151cSJulian Elischer 				(void)kvm_read(kd,
3730d632649SJohn Baldwin 				    (u_long)mtd.td_lockname,
3740d632649SJohn Baldwin 				    kp->ki_lockname, LOCKNAMELEN);
3750d632649SJohn Baldwin 			kp->ki_lockname[LOCKNAMELEN] = 0;
3761f7d2501SKirk McKusick 		}
37709ff687cSJohn Baldwin 		bintime2timeval(&proc.p_rux.rux_runtime, &tv);
37849b33de8SBrian Feldman 		kp->ki_runtime = (u_int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
3791f7d2501SKirk McKusick 		kp->ki_pid = proc.p_pid;
380e8a58a83SJuli Mallett 		kp->ki_siglist = proc.p_siglist;
38142d3ad71SJeff Roberson 		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
38231a9779eSJeff Roberson 		kp->ki_sigmask = mtd.td_sigmask;
3831f7d2501SKirk McKusick 		kp->ki_xstat = proc.p_xstat;
3841f7d2501SKirk McKusick 		kp->ki_acflag = proc.p_acflag;
3854f0db5e0SJulian Elischer 		kp->ki_lock = proc.p_lock;
386bff4151cSJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
3871f7d2501SKirk McKusick 			kp->ki_swtime = proc.p_swtime;
3886143c383SJulian Elischer 			kp->ki_flag = proc.p_flag;
3896d3e7b9bSJohn Baldwin 			kp->ki_sflag = proc.p_sflag;
39030105366SJulian Elischer 			kp->ki_nice = proc.p_nice;
3911f7d2501SKirk McKusick 			kp->ki_traceflag = proc.p_traceflag;
3926143c383SJulian Elischer 			if (proc.p_state == PRS_NORMAL) {
39371fad9fdSJulian Elischer 				if (TD_ON_RUNQ(&mtd) ||
39471fad9fdSJulian Elischer 				    TD_CAN_RUN(&mtd) ||
39571fad9fdSJulian Elischer 				    TD_IS_RUNNING(&mtd)) {
396e602ba25SJulian Elischer 					kp->ki_stat = SRUN;
3974f0db5e0SJulian Elischer 				} else if (mtd.td_state ==
3984f0db5e0SJulian Elischer 				    TDS_INHIBITED) {
39971fad9fdSJulian Elischer 					if (P_SHOULDSTOP(&proc)) {
400e602ba25SJulian Elischer 						kp->ki_stat = SSTOP;
4014f0db5e0SJulian Elischer 					} else if (
4024f0db5e0SJulian Elischer 					    TD_IS_SLEEPING(&mtd)) {
40371fad9fdSJulian Elischer 						kp->ki_stat = SSLEEP;
4040d632649SJohn Baldwin 					} else if (TD_ON_LOCK(&mtd)) {
4050d632649SJohn Baldwin 						kp->ki_stat = SLOCK;
406e602ba25SJulian Elischer 					} else {
407e602ba25SJulian Elischer 						kp->ki_stat = SWAIT;
408e602ba25SJulian Elischer 					}
40971fad9fdSJulian Elischer 				}
410e602ba25SJulian Elischer 			} else {
411e602ba25SJulian Elischer 				kp->ki_stat = SIDL;
412e602ba25SJulian Elischer 			}
4134f0db5e0SJulian Elischer 			/* Stuff from the thread */
41471fad9fdSJulian Elischer 			kp->ki_pri.pri_level = mtd.td_priority;
41571fad9fdSJulian Elischer 			kp->ki_pri.pri_native = mtd.td_base_pri;
41671fad9fdSJulian Elischer 			kp->ki_lastcpu = mtd.td_lastcpu;
4174f0db5e0SJulian Elischer 			kp->ki_wchan = mtd.td_wchan;
418a0ddbf49SJulian Elischer 			kp->ki_oncpu = mtd.td_oncpu;
4194f0db5e0SJulian Elischer 
420d8406adaSDavid Xu 			if (!(proc.p_flag & P_SA)) {
4214f0db5e0SJulian Elischer 				/* stuff from the ksegrp */
4224f0db5e0SJulian Elischer 				kp->ki_slptime = mkg.kg_slptime;
4234f0db5e0SJulian Elischer 				kp->ki_pri.pri_class = mkg.kg_pri_class;
4244f0db5e0SJulian Elischer 				kp->ki_pri.pri_user = mkg.kg_user_pri;
4254f0db5e0SJulian Elischer 				kp->ki_estcpu = mkg.kg_estcpu;
4264f0db5e0SJulian Elischer 
427ed062c8dSJulian Elischer #if 0
4284f0db5e0SJulian Elischer 				/* Stuff from the kse */
4294f0db5e0SJulian Elischer 				kp->ki_pctcpu = mke.ke_pctcpu;
4304f0db5e0SJulian Elischer 				kp->ki_rqindex = mke.ke_rqindex;
431ed062c8dSJulian Elischer #else
432ed062c8dSJulian Elischer 				kp->ki_pctcpu = 0;
433ed062c8dSJulian Elischer 				kp->ki_rqindex = 0;
434ed062c8dSJulian Elischer #endif
4354f0db5e0SJulian Elischer 			} else {
4364f0db5e0SJulian Elischer 				kp->ki_tdflags = -1;
4374f0db5e0SJulian Elischer 				/* All the rest are 0 for now */
4384f0db5e0SJulian Elischer 			}
4396143c383SJulian Elischer 		} else {
4406143c383SJulian Elischer 			kp->ki_stat = SZOMB;
4416143c383SJulian Elischer 		}
4421f7d2501SKirk McKusick 		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
44358f0484fSRodney W. Grimes 		++bp;
44458f0484fSRodney W. Grimes 		++cnt;
44558f0484fSRodney W. Grimes 	}
44658f0484fSRodney W. Grimes 	return (cnt);
44758f0484fSRodney W. Grimes }
44858f0484fSRodney W. Grimes 
44958f0484fSRodney W. Grimes /*
45058f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
45158f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
45258f0484fSRodney W. Grimes  */
45358f0484fSRodney W. Grimes static int
45458f0484fSRodney W. Grimes kvm_deadprocs(kd, what, arg, a_allproc, a_zombproc, maxcnt)
45558f0484fSRodney W. Grimes 	kvm_t *kd;
45658f0484fSRodney W. Grimes 	int what, arg;
45758f0484fSRodney W. Grimes 	u_long a_allproc;
45858f0484fSRodney W. Grimes 	u_long a_zombproc;
45958f0484fSRodney W. Grimes 	int maxcnt;
46058f0484fSRodney W. Grimes {
461be04b6d1SDavid E. O'Brien 	struct kinfo_proc *bp = kd->procbase;
462be04b6d1SDavid E. O'Brien 	int acnt, zcnt;
46358f0484fSRodney W. Grimes 	struct proc *p;
46458f0484fSRodney W. Grimes 
46558f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
46658f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
46758f0484fSRodney W. Grimes 		return (-1);
46858f0484fSRodney W. Grimes 	}
46958f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
47058f0484fSRodney W. Grimes 	if (acnt < 0)
47158f0484fSRodney W. Grimes 		return (acnt);
47258f0484fSRodney W. Grimes 
47358f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
47458f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
47558f0484fSRodney W. Grimes 		return (-1);
47658f0484fSRodney W. Grimes 	}
47758f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
47858f0484fSRodney W. Grimes 	if (zcnt < 0)
47958f0484fSRodney W. Grimes 		zcnt = 0;
48058f0484fSRodney W. Grimes 
48158f0484fSRodney W. Grimes 	return (acnt + zcnt);
48258f0484fSRodney W. Grimes }
48358f0484fSRodney W. Grimes 
48458f0484fSRodney W. Grimes struct kinfo_proc *
48558f0484fSRodney W. Grimes kvm_getprocs(kd, op, arg, cnt)
48658f0484fSRodney W. Grimes 	kvm_t *kd;
48758f0484fSRodney W. Grimes 	int op, arg;
48858f0484fSRodney W. Grimes 	int *cnt;
48958f0484fSRodney W. Grimes {
490c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
491c2ac238cSDoug Rabson 	size_t size;
492694127f8SDaniel Eischen 	int temp_op;
49358f0484fSRodney W. Grimes 
49458f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
49558f0484fSRodney W. Grimes 		free((void *)kd->procbase);
49658f0484fSRodney W. Grimes 		/*
49758f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
49858f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
49958f0484fSRodney W. Grimes 		 */
50058f0484fSRodney W. Grimes 		kd->procbase = 0;
50158f0484fSRodney W. Grimes 	}
50258f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
50358f0484fSRodney W. Grimes 		size = 0;
50458f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
50558f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
50658f0484fSRodney W. Grimes 		mib[2] = op;
50758f0484fSRodney W. Grimes 		mib[3] = arg;
508694127f8SDaniel Eischen 		temp_op = op & ~KERN_PROC_INC_THREAD;
509694127f8SDaniel Eischen 		st = sysctl(mib,
510694127f8SDaniel Eischen 		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
511f2dd06abSTim J. Robbins 		    3 : 4, NULL, &size, NULL, 0);
51258f0484fSRodney W. Grimes 		if (st == -1) {
51358f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
51458f0484fSRodney W. Grimes 			return (0);
51558f0484fSRodney W. Grimes 		}
51621687047SDima Dorfman 		/*
51721687047SDima Dorfman 		 * We can't continue with a size of 0 because we pass
51821687047SDima Dorfman 		 * it to realloc() (via _kvm_realloc()), and passing 0
51921687047SDima Dorfman 		 * to realloc() results in undefined behavior.
52021687047SDima Dorfman 		 */
52121687047SDima Dorfman 		if (size == 0) {
52221687047SDima Dorfman 			/*
52321687047SDima Dorfman 			 * XXX: We should probably return an invalid,
52421687047SDima Dorfman 			 * but non-NULL, pointer here so any client
52521687047SDima Dorfman 			 * program trying to dereference it will
52621687047SDima Dorfman 			 * crash.  However, _kvm_freeprocs() calls
52721687047SDima Dorfman 			 * free() on kd->procbase if it isn't NULL,
52821687047SDima Dorfman 			 * and free()'ing a junk pointer isn't good.
52921687047SDima Dorfman 			 * Then again, _kvm_freeprocs() isn't used
53021687047SDima Dorfman 			 * anywhere . . .
53121687047SDima Dorfman 			 */
53221687047SDima Dorfman 			kd->procbase = _kvm_malloc(kd, 1);
53321687047SDima Dorfman 			goto liveout;
53421687047SDima Dorfman 		}
535b2d3d0f0SDag-Erling Smørgrav 		do {
536b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
537b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
538b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
53958f0484fSRodney W. Grimes 			if (kd->procbase == 0)
54058f0484fSRodney W. Grimes 				return (0);
541694127f8SDaniel Eischen 			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
542694127f8SDaniel Eischen 			    temp_op == KERN_PROC_PROC ? 3 : 4,
543b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
544b2d3d0f0SDag-Erling Smørgrav 		} while (st == -1 && errno == ENOMEM);
54558f0484fSRodney W. Grimes 		if (st == -1) {
54658f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
54758f0484fSRodney W. Grimes 			return (0);
54858f0484fSRodney W. Grimes 		}
54921687047SDima Dorfman 		/*
55021687047SDima Dorfman 		 * We have to check the size again because sysctl()
55121687047SDima Dorfman 		 * may "round up" oldlenp if oldp is NULL; hence it
55221687047SDima Dorfman 		 * might've told us that there was data to get when
55321687047SDima Dorfman 		 * there really isn't any.
55421687047SDima Dorfman 		 */
5550627f53bSDavid Malone 		if (size > 0 &&
5560627f53bSDavid Malone 		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
55758f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
5581f7d2501SKirk McKusick 			    "kinfo_proc size mismatch (expected %d, got %d)",
5591f7d2501SKirk McKusick 			    sizeof(struct kinfo_proc),
5601f7d2501SKirk McKusick 			    kd->procbase->ki_structsize);
56158f0484fSRodney W. Grimes 			return (0);
56258f0484fSRodney W. Grimes 		}
56321687047SDima Dorfman liveout:
564d339edcfSDavid Malone 		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
56558f0484fSRodney W. Grimes 	} else {
56658f0484fSRodney W. Grimes 		struct nlist nl[4], *p;
56758f0484fSRodney W. Grimes 
56858f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
56958f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
57058f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
57158f0484fSRodney W. Grimes 		nl[3].n_name = 0;
57258f0484fSRodney W. Grimes 
57358f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
57458f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
57558f0484fSRodney W. Grimes 				;
57658f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
57758f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
57858f0484fSRodney W. Grimes 			return (0);
57958f0484fSRodney W. Grimes 		}
58058f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
58158f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
58258f0484fSRodney W. Grimes 			return (0);
58358f0484fSRodney W. Grimes 		}
58458f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
58558f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
58658f0484fSRodney W. Grimes 		if (kd->procbase == 0)
58758f0484fSRodney W. Grimes 			return (0);
58858f0484fSRodney W. Grimes 
58958f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
59058f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
59158f0484fSRodney W. Grimes #ifdef notdef
59258f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
59358f0484fSRodney W. Grimes 		(void)realloc(kd->procbase, size);
59458f0484fSRodney W. Grimes #endif
59558f0484fSRodney W. Grimes 	}
59658f0484fSRodney W. Grimes 	*cnt = nprocs;
59758f0484fSRodney W. Grimes 	return (kd->procbase);
59858f0484fSRodney W. Grimes }
59958f0484fSRodney W. Grimes 
60058f0484fSRodney W. Grimes void
60158f0484fSRodney W. Grimes _kvm_freeprocs(kd)
60258f0484fSRodney W. Grimes 	kvm_t *kd;
60358f0484fSRodney W. Grimes {
60458f0484fSRodney W. Grimes 	if (kd->procbase) {
60558f0484fSRodney W. Grimes 		free(kd->procbase);
60658f0484fSRodney W. Grimes 		kd->procbase = 0;
60758f0484fSRodney W. Grimes 	}
60858f0484fSRodney W. Grimes }
60958f0484fSRodney W. Grimes 
61058f0484fSRodney W. Grimes void *
61158f0484fSRodney W. Grimes _kvm_realloc(kd, p, n)
61258f0484fSRodney W. Grimes 	kvm_t *kd;
61358f0484fSRodney W. Grimes 	void *p;
61458f0484fSRodney W. Grimes 	size_t n;
61558f0484fSRodney W. Grimes {
61658f0484fSRodney W. Grimes 	void *np = (void *)realloc(p, n);
61758f0484fSRodney W. Grimes 
618e8420087SWarner Losh 	if (np == 0) {
619e8420087SWarner Losh 		free(p);
62058f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
621e8420087SWarner Losh 	}
62258f0484fSRodney W. Grimes 	return (np);
62358f0484fSRodney W. Grimes }
62458f0484fSRodney W. Grimes 
62558f0484fSRodney W. Grimes #ifndef MAX
62658f0484fSRodney W. Grimes #define MAX(a, b) ((a) > (b) ? (a) : (b))
62758f0484fSRodney W. Grimes #endif
62858f0484fSRodney W. Grimes 
62958f0484fSRodney W. Grimes /*
6301f7d2501SKirk McKusick  * Read in an argument vector from the user address space of process kp.
63177721f53SPeter Wemm  * addr if the user-space base address of narg null-terminated contiguous
63258f0484fSRodney W. Grimes  * strings.  This is used to read in both the command arguments and
63358f0484fSRodney W. Grimes  * environment strings.  Read at most maxcnt characters of strings.
63458f0484fSRodney W. Grimes  */
63558f0484fSRodney W. Grimes static char **
6361f7d2501SKirk McKusick kvm_argv(kd, kp, addr, narg, maxcnt)
63758f0484fSRodney W. Grimes 	kvm_t *kd;
6381f7d2501SKirk McKusick 	struct kinfo_proc *kp;
639be04b6d1SDavid E. O'Brien 	u_long addr;
640be04b6d1SDavid E. O'Brien 	int narg;
641be04b6d1SDavid E. O'Brien 	int maxcnt;
64258f0484fSRodney W. Grimes {
643be04b6d1SDavid E. O'Brien 	char *np, *cp, *ep, *ap;
644be04b6d1SDavid E. O'Brien 	u_long oaddr = -1;
645be04b6d1SDavid E. O'Brien 	int len, cc;
646be04b6d1SDavid E. O'Brien 	char **argv;
64758f0484fSRodney W. Grimes 
64858f0484fSRodney W. Grimes 	/*
64958f0484fSRodney W. Grimes 	 * Check that there aren't an unreasonable number of agruments,
65058f0484fSRodney W. Grimes 	 * and that the address is in user space.
65158f0484fSRodney W. Grimes 	 */
6526b492ae4SPeter Wemm 	if (narg > 512 || addr < VM_MIN_ADDRESS || addr >= VM_MAXUSER_ADDRESS)
65358f0484fSRodney W. Grimes 		return (0);
65458f0484fSRodney W. Grimes 
6556b492ae4SPeter Wemm 	/*
6566b492ae4SPeter Wemm 	 * kd->argv : work space for fetching the strings from the target
6576b492ae4SPeter Wemm 	 *            process's space, and is converted for returning to caller
6586b492ae4SPeter Wemm 	 */
65958f0484fSRodney W. Grimes 	if (kd->argv == 0) {
66058f0484fSRodney W. Grimes 		/*
66158f0484fSRodney W. Grimes 		 * Try to avoid reallocs.
66258f0484fSRodney W. Grimes 		 */
66358f0484fSRodney W. Grimes 		kd->argc = MAX(narg + 1, 32);
66458f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
66558f0484fSRodney W. Grimes 						sizeof(*kd->argv));
66658f0484fSRodney W. Grimes 		if (kd->argv == 0)
66758f0484fSRodney W. Grimes 			return (0);
66858f0484fSRodney W. Grimes 	} else if (narg + 1 > kd->argc) {
66958f0484fSRodney W. Grimes 		kd->argc = MAX(2 * kd->argc, narg + 1);
67058f0484fSRodney W. Grimes 		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
67158f0484fSRodney W. Grimes 						sizeof(*kd->argv));
67258f0484fSRodney W. Grimes 		if (kd->argv == 0)
67358f0484fSRodney W. Grimes 			return (0);
67458f0484fSRodney W. Grimes 	}
6756b492ae4SPeter Wemm 	/*
6766b492ae4SPeter Wemm 	 * kd->argspc : returned to user, this is where the kd->argv
6776b492ae4SPeter Wemm 	 *              arrays are left pointing to the collected strings.
6786b492ae4SPeter Wemm 	 */
67958f0484fSRodney W. Grimes 	if (kd->argspc == 0) {
6802572133eSPoul-Henning Kamp 		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
68158f0484fSRodney W. Grimes 		if (kd->argspc == 0)
68258f0484fSRodney W. Grimes 			return (0);
6832572133eSPoul-Henning Kamp 		kd->arglen = PAGE_SIZE;
68458f0484fSRodney W. Grimes 	}
6856b492ae4SPeter Wemm 	/*
6866b492ae4SPeter Wemm 	 * kd->argbuf : used to pull in pages from the target process.
6876b492ae4SPeter Wemm 	 *              the strings are copied out of here.
6886b492ae4SPeter Wemm 	 */
68977721f53SPeter Wemm 	if (kd->argbuf == 0) {
6902572133eSPoul-Henning Kamp 		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
69177721f53SPeter Wemm 		if (kd->argbuf == 0)
69277721f53SPeter Wemm 			return (0);
69377721f53SPeter Wemm 	}
6946b492ae4SPeter Wemm 
6956b492ae4SPeter Wemm 	/* Pull in the target process'es argv vector */
69677721f53SPeter Wemm 	cc = sizeof(char *) * narg;
6971f7d2501SKirk McKusick 	if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc)
69877721f53SPeter Wemm 		return (0);
6996b492ae4SPeter Wemm 	/*
7006b492ae4SPeter Wemm 	 * ap : saved start address of string we're working on in kd->argspc
7016b492ae4SPeter Wemm 	 * np : pointer to next place to write in kd->argspc
7026b492ae4SPeter Wemm 	 * len: length of data in kd->argspc
7036b492ae4SPeter Wemm 	 * argv: pointer to the argv vector that we are hunting around the
7046b492ae4SPeter Wemm 	 *       target process space for, and converting to addresses in
7056b492ae4SPeter Wemm 	 *       our address space (kd->argspc).
7066b492ae4SPeter Wemm 	 */
70777721f53SPeter Wemm 	ap = np = kd->argspc;
70858f0484fSRodney W. Grimes 	argv = kd->argv;
70958f0484fSRodney W. Grimes 	len = 0;
71058f0484fSRodney W. Grimes 	/*
71158f0484fSRodney W. Grimes 	 * Loop over pages, filling in the argument vector.
7126b492ae4SPeter Wemm 	 * Note that the argv strings could be pointing *anywhere* in
7136b492ae4SPeter Wemm 	 * the user address space and are no longer contiguous.
7146b492ae4SPeter Wemm 	 * Note that *argv is modified when we are going to fetch a string
7156b492ae4SPeter Wemm 	 * that crosses a page boundary.  We copy the next part of the string
7166b492ae4SPeter Wemm 	 * into to "np" and eventually convert the pointer.
71758f0484fSRodney W. Grimes 	 */
71877721f53SPeter Wemm 	while (argv < kd->argv + narg && *argv != 0) {
7196b492ae4SPeter Wemm 
7206b492ae4SPeter Wemm 		/* get the address that the current argv string is on */
7212572133eSPoul-Henning Kamp 		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
7226b492ae4SPeter Wemm 
7236b492ae4SPeter Wemm 		/* is it the same page as the last one? */
72477721f53SPeter Wemm 		if (addr != oaddr) {
7251f7d2501SKirk McKusick 			if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) !=
7262572133eSPoul-Henning Kamp 			    PAGE_SIZE)
72777721f53SPeter Wemm 				return (0);
72877721f53SPeter Wemm 			oaddr = addr;
72977721f53SPeter Wemm 		}
7306b492ae4SPeter Wemm 
7316b492ae4SPeter Wemm 		/* offset within the page... kd->argbuf */
7322572133eSPoul-Henning Kamp 		addr = (u_long)*argv & (PAGE_SIZE - 1);
7336b492ae4SPeter Wemm 
7346b492ae4SPeter Wemm 		/* cp = start of string, cc = count of chars in this chunk */
73577721f53SPeter Wemm 		cp = kd->argbuf + addr;
7362572133eSPoul-Henning Kamp 		cc = PAGE_SIZE - addr;
7376b492ae4SPeter Wemm 
7386b492ae4SPeter Wemm 		/* dont get more than asked for by user process */
73958f0484fSRodney W. Grimes 		if (maxcnt > 0 && cc > maxcnt - len)
7406b492ae4SPeter Wemm 			cc = maxcnt - len;
7416b492ae4SPeter Wemm 
7426b492ae4SPeter Wemm 		/* pointer to end of string if we found it in this page */
74377721f53SPeter Wemm 		ep = memchr(cp, '\0', cc);
74477721f53SPeter Wemm 		if (ep != 0)
74577721f53SPeter Wemm 			cc = ep - cp + 1;
7466b492ae4SPeter Wemm 		/*
7476b492ae4SPeter Wemm 		 * at this point, cc is the count of the chars that we are
7486b492ae4SPeter Wemm 		 * going to retrieve this time. we may or may not have found
7496b492ae4SPeter Wemm 		 * the end of it.  (ep points to the null if the end is known)
7506b492ae4SPeter Wemm 		 */
7516b492ae4SPeter Wemm 
7526b492ae4SPeter Wemm 		/* will we exceed the malloc/realloced buffer? */
75358f0484fSRodney W. Grimes 		if (len + cc > kd->arglen) {
754be04b6d1SDavid E. O'Brien 			int off;
755be04b6d1SDavid E. O'Brien 			char **pp;
756be04b6d1SDavid E. O'Brien 			char *op = kd->argspc;
75758f0484fSRodney W. Grimes 
75858f0484fSRodney W. Grimes 			kd->arglen *= 2;
75958f0484fSRodney W. Grimes 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
76058f0484fSRodney W. Grimes 							  kd->arglen);
76158f0484fSRodney W. Grimes 			if (kd->argspc == 0)
76258f0484fSRodney W. Grimes 				return (0);
76358f0484fSRodney W. Grimes 			/*
76458f0484fSRodney W. Grimes 			 * Adjust argv pointers in case realloc moved
76558f0484fSRodney W. Grimes 			 * the string space.
76658f0484fSRodney W. Grimes 			 */
76758f0484fSRodney W. Grimes 			off = kd->argspc - op;
76877721f53SPeter Wemm 			for (pp = kd->argv; pp < argv; pp++)
76958f0484fSRodney W. Grimes 				*pp += off;
77077721f53SPeter Wemm 			ap += off;
77177721f53SPeter Wemm 			np += off;
77258f0484fSRodney W. Grimes 		}
7736b492ae4SPeter Wemm 		/* np = where to put the next part of the string in kd->argspc*/
7746b492ae4SPeter Wemm 		/* np is kinda redundant.. could use "kd->argspc + len" */
77577721f53SPeter Wemm 		memcpy(np, cp, cc);
7766b492ae4SPeter Wemm 		np += cc;	/* inc counters */
77758f0484fSRodney W. Grimes 		len += cc;
7786b492ae4SPeter Wemm 
7796b492ae4SPeter Wemm 		/*
7806b492ae4SPeter Wemm 		 * if end of string found, set the *argv pointer to the
7816b492ae4SPeter Wemm 		 * saved beginning of string, and advance. argv points to
7826b492ae4SPeter Wemm 		 * somewhere in kd->argv..  This is initially relative
7836b492ae4SPeter Wemm 		 * to the target process, but when we close it off, we set
7846b492ae4SPeter Wemm 		 * it to point in our address space.
7856b492ae4SPeter Wemm 		 */
78677721f53SPeter Wemm 		if (ep != 0) {
78777721f53SPeter Wemm 			*argv++ = ap;
78877721f53SPeter Wemm 			ap = np;
7896b492ae4SPeter Wemm 		} else {
7906b492ae4SPeter Wemm 			/* update the address relative to the target process */
79177721f53SPeter Wemm 			*argv += cc;
7926b492ae4SPeter Wemm 		}
7936b492ae4SPeter Wemm 
79458f0484fSRodney W. Grimes 		if (maxcnt > 0 && len >= maxcnt) {
79558f0484fSRodney W. Grimes 			/*
79658f0484fSRodney W. Grimes 			 * We're stopping prematurely.  Terminate the
79777721f53SPeter Wemm 			 * current string.
79858f0484fSRodney W. Grimes 			 */
79977721f53SPeter Wemm 			if (ep == 0) {
80077721f53SPeter Wemm 				*np = '\0';
80177721f53SPeter Wemm 				*argv++ = ap;
80277721f53SPeter Wemm 			}
80377721f53SPeter Wemm 			break;
80477721f53SPeter Wemm 		}
80577721f53SPeter Wemm 	}
80677721f53SPeter Wemm 	/* Make sure argv is terminated. */
80777721f53SPeter Wemm 	*argv = 0;
80858f0484fSRodney W. Grimes 	return (kd->argv);
80958f0484fSRodney W. Grimes }
81058f0484fSRodney W. Grimes 
81158f0484fSRodney W. Grimes static void
81258f0484fSRodney W. Grimes ps_str_a(p, addr, n)
81358f0484fSRodney W. Grimes 	struct ps_strings *p;
81458f0484fSRodney W. Grimes 	u_long *addr;
81558f0484fSRodney W. Grimes 	int *n;
81658f0484fSRodney W. Grimes {
81758f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_argvstr;
81858f0484fSRodney W. Grimes 	*n = p->ps_nargvstr;
81958f0484fSRodney W. Grimes }
82058f0484fSRodney W. Grimes 
82158f0484fSRodney W. Grimes static void
82258f0484fSRodney W. Grimes ps_str_e(p, addr, n)
82358f0484fSRodney W. Grimes 	struct ps_strings *p;
82458f0484fSRodney W. Grimes 	u_long *addr;
82558f0484fSRodney W. Grimes 	int *n;
82658f0484fSRodney W. Grimes {
82758f0484fSRodney W. Grimes 	*addr = (u_long)p->ps_envstr;
82858f0484fSRodney W. Grimes 	*n = p->ps_nenvstr;
82958f0484fSRodney W. Grimes }
83058f0484fSRodney W. Grimes 
83158f0484fSRodney W. Grimes /*
83258f0484fSRodney W. Grimes  * Determine if the proc indicated by p is still active.
83358f0484fSRodney W. Grimes  * This test is not 100% foolproof in theory, but chances of
83458f0484fSRodney W. Grimes  * being wrong are very low.
83558f0484fSRodney W. Grimes  */
83658f0484fSRodney W. Grimes static int
8371f7d2501SKirk McKusick proc_verify(curkp)
8381f7d2501SKirk McKusick 	struct kinfo_proc *curkp;
83958f0484fSRodney W. Grimes {
8401f7d2501SKirk McKusick 	struct kinfo_proc newkp;
84160160c5eSBruce Evans 	int mib[4];
842c2ac238cSDoug Rabson 	size_t len;
84358f0484fSRodney W. Grimes 
84435e6b695SPoul-Henning Kamp 	mib[0] = CTL_KERN;
84535e6b695SPoul-Henning Kamp 	mib[1] = KERN_PROC;
84635e6b695SPoul-Henning Kamp 	mib[2] = KERN_PROC_PID;
8471f7d2501SKirk McKusick 	mib[3] = curkp->ki_pid;
8481f7d2501SKirk McKusick 	len = sizeof(newkp);
8491f7d2501SKirk McKusick 	if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1)
85058f0484fSRodney W. Grimes 		return (0);
8511f7d2501SKirk McKusick 	return (curkp->ki_pid == newkp.ki_pid &&
8521f7d2501SKirk McKusick 	    (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB));
85358f0484fSRodney W. Grimes }
85458f0484fSRodney W. Grimes 
85558f0484fSRodney W. Grimes static char **
85658f0484fSRodney W. Grimes kvm_doargv(kd, kp, nchr, info)
85758f0484fSRodney W. Grimes 	kvm_t *kd;
8581f7d2501SKirk McKusick 	struct kinfo_proc *kp;
85958f0484fSRodney W. Grimes 	int nchr;
86077721f53SPeter Wemm 	void (*info)(struct ps_strings *, u_long *, int *);
86158f0484fSRodney W. Grimes {
8621f7d2501SKirk McKusick 	char **ap;
86358f0484fSRodney W. Grimes 	u_long addr;
86458f0484fSRodney W. Grimes 	int cnt;
86560160c5eSBruce Evans 	static struct ps_strings arginfo;
86660160c5eSBruce Evans 	static u_long ps_strings;
8677350dd84SPeter Wemm 	size_t len;
8687350dd84SPeter Wemm 
8695aaa432dSJens Schweikhardt 	if (ps_strings == 0) {
87060160c5eSBruce Evans 		len = sizeof(ps_strings);
87160160c5eSBruce Evans 		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
87260160c5eSBruce Evans 		    0) == -1)
8737350dd84SPeter Wemm 			ps_strings = PS_STRINGS;
87435e6b695SPoul-Henning Kamp 	}
87558f0484fSRodney W. Grimes 
87658f0484fSRodney W. Grimes 	/*
87758f0484fSRodney W. Grimes 	 * Pointers are stored at the top of the user stack.
87858f0484fSRodney W. Grimes 	 */
8791f7d2501SKirk McKusick 	if (kp->ki_stat == SZOMB ||
8801f7d2501SKirk McKusick 	    kvm_uread(kd, kp, ps_strings, (char *)&arginfo,
88177721f53SPeter Wemm 		      sizeof(arginfo)) != sizeof(arginfo))
88258f0484fSRodney W. Grimes 		return (0);
88358f0484fSRodney W. Grimes 
88458f0484fSRodney W. Grimes 	(*info)(&arginfo, &addr, &cnt);
88577721f53SPeter Wemm 	if (cnt == 0)
88677721f53SPeter Wemm 		return (0);
8871f7d2501SKirk McKusick 	ap = kvm_argv(kd, kp, addr, cnt, nchr);
88858f0484fSRodney W. Grimes 	/*
88958f0484fSRodney W. Grimes 	 * For live kernels, make sure this process didn't go away.
89058f0484fSRodney W. Grimes 	 */
8911f7d2501SKirk McKusick 	if (ap != 0 && ISALIVE(kd) && !proc_verify(kp))
89258f0484fSRodney W. Grimes 		ap = 0;
89358f0484fSRodney W. Grimes 	return (ap);
89458f0484fSRodney W. Grimes }
89558f0484fSRodney W. Grimes 
89658f0484fSRodney W. Grimes /*
89758f0484fSRodney W. Grimes  * Get the command args.  This code is now machine independent.
89858f0484fSRodney W. Grimes  */
89958f0484fSRodney W. Grimes char **
90058f0484fSRodney W. Grimes kvm_getargv(kd, kp, nchr)
90158f0484fSRodney W. Grimes 	kvm_t *kd;
90258f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
90358f0484fSRodney W. Grimes 	int nchr;
90458f0484fSRodney W. Grimes {
905b9df5231SPoul-Henning Kamp 	int oid[4];
906b7875890SDavid E. O'Brien 	int i;
907b7875890SDavid E. O'Brien 	size_t bufsz;
90815d2f5f0SPeter Wemm 	static unsigned long buflen;
909b9df5231SPoul-Henning Kamp 	static char *buf, *p;
910b9df5231SPoul-Henning Kamp 	static char **bufp;
911b9df5231SPoul-Henning Kamp 	static int argc;
912b9df5231SPoul-Henning Kamp 
913c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
914c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
915c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
916c4a7cdb3SPeter Wemm 		return (0);
917c4a7cdb3SPeter Wemm 	}
918c4a7cdb3SPeter Wemm 
919b9df5231SPoul-Henning Kamp 	if (!buflen) {
920b7875890SDavid E. O'Brien 		bufsz = sizeof(buflen);
921b9df5231SPoul-Henning Kamp 		i = sysctlbyname("kern.ps_arg_cache_limit",
922b7875890SDavid E. O'Brien 		    &buflen, &bufsz, NULL, 0);
923b9df5231SPoul-Henning Kamp 		if (i == -1) {
924b7875890SDavid E. O'Brien 			buflen = 0;
925b9df5231SPoul-Henning Kamp 		} else {
926b9df5231SPoul-Henning Kamp 			buf = malloc(buflen);
927b9df5231SPoul-Henning Kamp 			if (buf == NULL)
928b9df5231SPoul-Henning Kamp 				buflen = 0;
929b9df5231SPoul-Henning Kamp 			argc = 32;
930b9df5231SPoul-Henning Kamp 			bufp = malloc(sizeof(char *) * argc);
931b9df5231SPoul-Henning Kamp 		}
932b9df5231SPoul-Henning Kamp 	}
933b9df5231SPoul-Henning Kamp 	if (buf != NULL) {
934b9df5231SPoul-Henning Kamp 		oid[0] = CTL_KERN;
935b9df5231SPoul-Henning Kamp 		oid[1] = KERN_PROC;
936b9df5231SPoul-Henning Kamp 		oid[2] = KERN_PROC_ARGS;
9371f7d2501SKirk McKusick 		oid[3] = kp->ki_pid;
938b7875890SDavid E. O'Brien 		bufsz = buflen;
939b7875890SDavid E. O'Brien 		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
940b7875890SDavid E. O'Brien 		if (i == 0 && bufsz > 0) {
941b9df5231SPoul-Henning Kamp 			i = 0;
942b9df5231SPoul-Henning Kamp 			p = buf;
943b9df5231SPoul-Henning Kamp 			do {
944b9df5231SPoul-Henning Kamp 				bufp[i++] = p;
945b9df5231SPoul-Henning Kamp 				p += strlen(p) + 1;
946b9df5231SPoul-Henning Kamp 				if (i >= argc) {
947b9df5231SPoul-Henning Kamp 					argc += argc;
948b9df5231SPoul-Henning Kamp 					bufp = realloc(bufp,
949b9df5231SPoul-Henning Kamp 					    sizeof(char *) * argc);
950b9df5231SPoul-Henning Kamp 				}
951b7875890SDavid E. O'Brien 			} while (p < buf + bufsz);
952b9df5231SPoul-Henning Kamp 			bufp[i++] = 0;
953b9df5231SPoul-Henning Kamp 			return (bufp);
954b9df5231SPoul-Henning Kamp 		}
955b9df5231SPoul-Henning Kamp 	}
9561f7d2501SKirk McKusick 	if (kp->ki_flag & P_SYSTEM)
957b9df5231SPoul-Henning Kamp 		return (NULL);
95858f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_a));
95958f0484fSRodney W. Grimes }
96058f0484fSRodney W. Grimes 
96158f0484fSRodney W. Grimes char **
96258f0484fSRodney W. Grimes kvm_getenvv(kd, kp, nchr)
96358f0484fSRodney W. Grimes 	kvm_t *kd;
96458f0484fSRodney W. Grimes 	const struct kinfo_proc *kp;
96558f0484fSRodney W. Grimes 	int nchr;
96658f0484fSRodney W. Grimes {
96758f0484fSRodney W. Grimes 	return (kvm_doargv(kd, kp, nchr, ps_str_e));
96858f0484fSRodney W. Grimes }
96958f0484fSRodney W. Grimes 
97058f0484fSRodney W. Grimes /*
97158f0484fSRodney W. Grimes  * Read from user space.  The user context is given by p.
97258f0484fSRodney W. Grimes  */
97358f0484fSRodney W. Grimes ssize_t
9741f7d2501SKirk McKusick kvm_uread(kd, kp, uva, buf, len)
97558f0484fSRodney W. Grimes 	kvm_t *kd;
9761f7d2501SKirk McKusick 	struct kinfo_proc *kp;
977be04b6d1SDavid E. O'Brien 	u_long uva;
978be04b6d1SDavid E. O'Brien 	char *buf;
979be04b6d1SDavid E. O'Brien 	size_t len;
98058f0484fSRodney W. Grimes {
981be04b6d1SDavid E. O'Brien 	char *cp;
982338c7541SDavid Greenman 	char procfile[MAXPATHLEN];
983338c7541SDavid Greenman 	ssize_t amount;
984338c7541SDavid Greenman 	int fd;
98558f0484fSRodney W. Grimes 
9867350dd84SPeter Wemm 	if (!ISALIVE(kd)) {
987d7fb4b13SBruce Evans 		_kvm_err(kd, kd->program,
988d7fb4b13SBruce Evans 		    "cannot read user space from dead kernel");
9897350dd84SPeter Wemm 		return (0);
9907350dd84SPeter Wemm 	}
9917350dd84SPeter Wemm 
9921f7d2501SKirk McKusick 	sprintf(procfile, "/proc/%d/mem", kp->ki_pid);
993338c7541SDavid Greenman 	fd = open(procfile, O_RDONLY, 0);
994338c7541SDavid Greenman 	if (fd < 0) {
995338c7541SDavid Greenman 		_kvm_err(kd, kd->program, "cannot open %s", procfile);
99658f0484fSRodney W. Grimes 		return (0);
99758f0484fSRodney W. Grimes 	}
998338c7541SDavid Greenman 
999d7fb4b13SBruce Evans 	cp = buf;
1000338c7541SDavid Greenman 	while (len > 0) {
1001d7fb4b13SBruce Evans 		errno = 0;
1002ec4f2251SJoerg Wunsch 		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
1003d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "invalid address (%x) in %s",
1004d7fb4b13SBruce Evans 			    uva, procfile);
100558f0484fSRodney W. Grimes 			break;
100658f0484fSRodney W. Grimes 		}
1007567127faSDavid Greenman 		amount = read(fd, cp, len);
1008338c7541SDavid Greenman 		if (amount < 0) {
1009d7fb4b13SBruce Evans 			_kvm_syserr(kd, kd->program, "error reading %s",
1010d7fb4b13SBruce Evans 			    procfile);
1011d7fb4b13SBruce Evans 			break;
1012d7fb4b13SBruce Evans 		}
1013d7fb4b13SBruce Evans 		if (amount == 0) {
1014d7fb4b13SBruce Evans 			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
1015338c7541SDavid Greenman 			break;
1016338c7541SDavid Greenman 		}
1017338c7541SDavid Greenman 		cp += amount;
1018338c7541SDavid Greenman 		uva += amount;
1019338c7541SDavid Greenman 		len -= amount;
1020338c7541SDavid Greenman 	}
1021338c7541SDavid Greenman 
1022338c7541SDavid Greenman 	close(fd);
1023d7fb4b13SBruce Evans 	return ((ssize_t)(cp - buf));
102458f0484fSRodney W. Grimes }
1025