xref: /freebsd/lib/libkvm/kvm_proc.c (revision e77f9fed15f3bd8e320e1d4544b7600bf7413832)
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  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
34f86e3350SBruce Evans #if 0
3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3658f0484fSRodney W. Grimes static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
38f86e3350SBruce Evans #endif
39f86e3350SBruce Evans 
40f86e3350SBruce Evans #include <sys/cdefs.h>
41f86e3350SBruce Evans __FBSDID("$FreeBSD$");
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes /*
4458f0484fSRodney W. Grimes  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
4558f0484fSRodney W. Grimes  * users of this code, so we've factored it out into a separate module.
4658f0484fSRodney W. Grimes  * Thus, we keep this grunge out of the other kvm applications (i.e.,
4758f0484fSRodney W. Grimes  * most other applications are interested only in open/close/read/nlist).
4858f0484fSRodney W. Grimes  */
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes #include <sys/param.h>
51b20ea179SAlfred Perlstein #define	_WANT_UCRED	/* make ucred.h give us 'struct ucred' */
52aa22cbfeSAlfred Perlstein #include <sys/ucred.h>
53f8197bf0SPawel Jakub Dawidek #include <sys/queue.h>
54f8197bf0SPawel Jakub Dawidek #include <sys/_lock.h>
55f8197bf0SPawel Jakub Dawidek #include <sys/_mutex.h>
56f8197bf0SPawel Jakub Dawidek #include <sys/_task.h>
57413628a7SBjoern A. Zeeb #include <sys/cpuset.h>
5858f0484fSRodney W. Grimes #include <sys/user.h>
5958f0484fSRodney W. Grimes #include <sys/proc.h>
60413628a7SBjoern A. Zeeb #define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
61413628a7SBjoern A. Zeeb #include <sys/jail.h>
6258f0484fSRodney W. Grimes #include <sys/exec.h>
6358f0484fSRodney W. Grimes #include <sys/stat.h>
64276de18cSGarance A Drosehn #include <sys/sysent.h>
6558f0484fSRodney W. Grimes #include <sys/ioctl.h>
6658f0484fSRodney W. Grimes #include <sys/tty.h>
67338c7541SDavid Greenman #include <sys/file.h>
682bdd5609SPeter Wemm #include <sys/conf.h>
6951295a4dSJordan K. Hubbard #include <stdio.h>
7051295a4dSJordan K. Hubbard #include <stdlib.h>
7158f0484fSRodney W. Grimes #include <unistd.h>
7258f0484fSRodney W. Grimes #include <nlist.h>
7358f0484fSRodney W. Grimes #include <kvm.h>
7458f0484fSRodney W. Grimes 
7558f0484fSRodney W. Grimes #include <sys/sysctl.h>
7658f0484fSRodney W. Grimes 
7758f0484fSRodney W. Grimes #include <limits.h>
7877721f53SPeter Wemm #include <memory.h>
7958f0484fSRodney W. Grimes #include <paths.h>
8058f0484fSRodney W. Grimes 
8158f0484fSRodney W. Grimes #include "kvm_private.h"
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes #define KREAD(kd, addr, obj) \
8458f0484fSRodney W. Grimes 	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
8558f0484fSRodney W. Grimes 
8684a0b303SJeff Roberson static int ticks;
8784a0b303SJeff Roberson static int hz;
88de788839SUlrich Spörlein static uint64_t cpu_tick_frequency;
89de788839SUlrich Spörlein 
90de788839SUlrich Spörlein /*
91de788839SUlrich Spörlein  * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is
92de788839SUlrich Spörlein  * read/initialized before this function is ever called.
93de788839SUlrich Spörlein  */
94de788839SUlrich Spörlein static uint64_t
95de788839SUlrich Spörlein cputick2usec(uint64_t tick)
96de788839SUlrich Spörlein {
97de788839SUlrich Spörlein 
98de788839SUlrich Spörlein 	if (cpu_tick_frequency == 0)
99de788839SUlrich Spörlein 		return (0);
100de788839SUlrich Spörlein 	if (tick > 18446744073709551)		/* floor(2^64 / 1000) */
101de788839SUlrich Spörlein 		return (tick / (cpu_tick_frequency / 1000000));
102de788839SUlrich Spörlein 	else if (tick > 18446744073709)	/* floor(2^64 / 1000000) */
103de788839SUlrich Spörlein 		return ((tick * 1000) / (cpu_tick_frequency / 1000));
104de788839SUlrich Spörlein 	else
105de788839SUlrich Spörlein 		return ((tick * 1000000) / cpu_tick_frequency);
106de788839SUlrich Spörlein }
10784a0b303SJeff Roberson 
10858f0484fSRodney W. Grimes /*
10958f0484fSRodney W. Grimes  * Read proc's from memory file into buffer bp, which has space to hold
11058f0484fSRodney W. Grimes  * at most maxcnt procs.
11158f0484fSRodney W. Grimes  */
11258f0484fSRodney W. Grimes static int
113c10970ddSUlrich Spörlein kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
114c10970ddSUlrich Spörlein     struct kinfo_proc *bp, int maxcnt)
11558f0484fSRodney W. Grimes {
116be04b6d1SDavid E. O'Brien 	int cnt = 0;
1171f7d2501SKirk McKusick 	struct kinfo_proc kinfo_proc, *kp;
11858f0484fSRodney W. Grimes 	struct pgrp pgrp;
11958f0484fSRodney W. Grimes 	struct session sess;
1202bdd5609SPeter Wemm 	struct cdev t_cdev;
12158f0484fSRodney W. Grimes 	struct tty tty;
1221f7d2501SKirk McKusick 	struct vmspace vmspace;
123840558b9SJohn Baldwin 	struct sigacts sigacts;
124c10970ddSUlrich Spörlein #if 0
1251f7d2501SKirk McKusick 	struct pstats pstats;
126c10970ddSUlrich Spörlein #endif
1271f7d2501SKirk McKusick 	struct ucred ucred;
128f8197bf0SPawel Jakub Dawidek 	struct prison pr;
12971fad9fdSJulian Elischer 	struct thread mtd;
13058f0484fSRodney W. Grimes 	struct proc proc;
131a58930d8STor Egge 	struct proc pproc;
132276de18cSGarance A Drosehn 	struct sysentvec sysent;
133276de18cSGarance A Drosehn 	char svname[KI_EMULNAMELEN];
13458f0484fSRodney W. Grimes 
1351f7d2501SKirk McKusick 	kp = &kinfo_proc;
1361f7d2501SKirk McKusick 	kp->ki_structsize = sizeof(kinfo_proc);
1377ab24ea3SJulian Elischer 	/*
1387ab24ea3SJulian Elischer 	 * Loop on the processes. this is completely broken because we need to be
1397ab24ea3SJulian Elischer 	 * able to loop on the threads and merge the ones that are the same process some how.
1407ab24ea3SJulian Elischer 	 */
14142680b3aSBen Smithurst 	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
1426f8c6a69SPeter Wemm 		memset(kp, 0, sizeof *kp);
14358f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)p, &proc)) {
144c10970ddSUlrich Spörlein 			_kvm_err(kd, kd->program, "can't read proc at %p", p);
14558f0484fSRodney W. Grimes 			return (-1);
14658f0484fSRodney W. Grimes 		}
1479fb9ea1cSAndriy Gapon 		if (proc.p_state == PRS_NEW)
1489fb9ea1cSAndriy Gapon 			continue;
14958551c03SJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
1503daf63fcSJulian Elischer 			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
15171fad9fdSJulian Elischer 			    &mtd)) {
15258551c03SJulian Elischer 				_kvm_err(kd, kd->program,
153c10970ddSUlrich Spörlein 				    "can't read thread at %p",
1543daf63fcSJulian Elischer 				    TAILQ_FIRST(&proc.p_threads));
1553daf63fcSJulian Elischer 				return (-1);
1563daf63fcSJulian Elischer 			}
15758551c03SJulian Elischer 		}
158b1fc0ec1SRobert Watson 		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
159b1fc0ec1SRobert Watson 			kp->ki_ruid = ucred.cr_ruid;
160b1fc0ec1SRobert Watson 			kp->ki_svuid = ucred.cr_svuid;
161b1fc0ec1SRobert Watson 			kp->ki_rgid = ucred.cr_rgid;
162b1fc0ec1SRobert Watson 			kp->ki_svgid = ucred.cr_svgid;
1631b5768beSBrooks Davis 			kp->ki_cr_flags = ucred.cr_flags;
1641b5768beSBrooks Davis 			if (ucred.cr_ngroups > KI_NGROUPS) {
1651b5768beSBrooks Davis 				kp->ki_ngroups = KI_NGROUPS;
1661b5768beSBrooks Davis 				kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
16768a5cc39SBrooks Davis 			} else
1681f7d2501SKirk McKusick 				kp->ki_ngroups = ucred.cr_ngroups;
169d534b0c2SBrooks Davis 			kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
1701b5768beSBrooks Davis 			    kp->ki_ngroups * sizeof(gid_t));
1711f7d2501SKirk McKusick 			kp->ki_uid = ucred.cr_uid;
172f8197bf0SPawel Jakub Dawidek 			if (ucred.cr_prison != NULL) {
173f8197bf0SPawel Jakub Dawidek 				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
174f8197bf0SPawel Jakub Dawidek 					_kvm_err(kd, kd->program,
175c10970ddSUlrich Spörlein 					    "can't read prison at %p",
176f8197bf0SPawel Jakub Dawidek 					    ucred.cr_prison);
177f8197bf0SPawel Jakub Dawidek 					return (-1);
178f8197bf0SPawel Jakub Dawidek 				}
179f8197bf0SPawel Jakub Dawidek 				kp->ki_jid = pr.pr_id;
180f8197bf0SPawel Jakub Dawidek 			}
1811f7d2501SKirk McKusick 		}
18258f0484fSRodney W. Grimes 
183694127f8SDaniel Eischen 		switch(what & ~KERN_PROC_INC_THREAD) {
18458f0484fSRodney W. Grimes 
185276de18cSGarance A Drosehn 		case KERN_PROC_GID:
186276de18cSGarance A Drosehn 			if (kp->ki_groups[0] != (gid_t)arg)
187276de18cSGarance A Drosehn 				continue;
188276de18cSGarance A Drosehn 			break;
189276de18cSGarance A Drosehn 
19058f0484fSRodney W. Grimes 		case KERN_PROC_PID:
19158f0484fSRodney W. Grimes 			if (proc.p_pid != (pid_t)arg)
19258f0484fSRodney W. Grimes 				continue;
19358f0484fSRodney W. Grimes 			break;
19458f0484fSRodney W. Grimes 
195276de18cSGarance A Drosehn 		case KERN_PROC_RGID:
196276de18cSGarance A Drosehn 			if (kp->ki_rgid != (gid_t)arg)
197276de18cSGarance A Drosehn 				continue;
198276de18cSGarance A Drosehn 			break;
199276de18cSGarance A Drosehn 
20058f0484fSRodney W. Grimes 		case KERN_PROC_UID:
2011f7d2501SKirk McKusick 			if (kp->ki_uid != (uid_t)arg)
20258f0484fSRodney W. Grimes 				continue;
20358f0484fSRodney W. Grimes 			break;
20458f0484fSRodney W. Grimes 
20558f0484fSRodney W. Grimes 		case KERN_PROC_RUID:
2061f7d2501SKirk McKusick 			if (kp->ki_ruid != (uid_t)arg)
20758f0484fSRodney W. Grimes 				continue;
20858f0484fSRodney W. Grimes 			break;
20958f0484fSRodney W. Grimes 		}
21058f0484fSRodney W. Grimes 		/*
21158f0484fSRodney W. Grimes 		 * We're going to add another proc to the set.  If this
21258f0484fSRodney W. Grimes 		 * will overflow the buffer, assume the reason is because
21358f0484fSRodney W. Grimes 		 * nprocs (or the proc list) is corrupt and declare an error.
21458f0484fSRodney W. Grimes 		 */
21558f0484fSRodney W. Grimes 		if (cnt >= maxcnt) {
21658f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "nprocs corrupt");
21758f0484fSRodney W. Grimes 			return (-1);
21858f0484fSRodney W. Grimes 		}
21958f0484fSRodney W. Grimes 		/*
2201f7d2501SKirk McKusick 		 * gather kinfo_proc
22158f0484fSRodney W. Grimes 		 */
2221f7d2501SKirk McKusick 		kp->ki_paddr = p;
2237a62aa8aSDavid Schultz 		kp->ki_addr = 0;	/* XXX uarea */
224b40ce416SJulian Elischer 		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
2251f7d2501SKirk McKusick 		kp->ki_args = proc.p_args;
2268b7a975eSJohn Baldwin 		kp->ki_tracep = proc.p_tracevp;
2271f7d2501SKirk McKusick 		kp->ki_textvp = proc.p_textvp;
2281f7d2501SKirk McKusick 		kp->ki_fd = proc.p_fd;
2291f7d2501SKirk McKusick 		kp->ki_vmspace = proc.p_vmspace;
230840558b9SJohn Baldwin 		if (proc.p_sigacts != NULL) {
231840558b9SJohn Baldwin 			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
2321f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
233c10970ddSUlrich Spörlein 				    "can't read sigacts at %p", proc.p_sigacts);
2341f7d2501SKirk McKusick 				return (-1);
2351f7d2501SKirk McKusick 			}
236840558b9SJohn Baldwin 			kp->ki_sigignore = sigacts.ps_sigignore;
237840558b9SJohn Baldwin 			kp->ki_sigcatch = sigacts.ps_sigcatch;
2381f7d2501SKirk McKusick 		}
2398ef6b142SJeff Roberson #if 0
240b61ce5b0SJeff Roberson 		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
2411f7d2501SKirk McKusick 			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
2421f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
2431f7d2501SKirk McKusick 				    "can't read stats at %x", proc.p_stats);
2441f7d2501SKirk McKusick 				return (-1);
2451f7d2501SKirk McKusick 			}
2461f7d2501SKirk McKusick 			kp->ki_start = pstats.p_start;
24709ff687cSJohn Baldwin 
24809ff687cSJohn Baldwin 			/*
24909ff687cSJohn Baldwin 			 * XXX: The times here are probably zero and need
25009ff687cSJohn Baldwin 			 * to be calculated from the raw data in p_rux and
25109ff687cSJohn Baldwin 			 * p_crux.
25209ff687cSJohn Baldwin 			 */
2531f7d2501SKirk McKusick 			kp->ki_rusage = pstats.p_ru;
254276de18cSGarance A Drosehn 			kp->ki_childstime = pstats.p_cru.ru_stime;
255276de18cSGarance A Drosehn 			kp->ki_childutime = pstats.p_cru.ru_utime;
256276de18cSGarance A Drosehn 			/* Some callers want child-times in a single value */
257276de18cSGarance A Drosehn 			timeradd(&kp->ki_childstime, &kp->ki_childutime,
258276de18cSGarance A Drosehn 			    &kp->ki_childtime);
2591f7d2501SKirk McKusick 		}
2608ef6b142SJeff Roberson #endif
261a58930d8STor Egge 		if (proc.p_oppid)
2621f7d2501SKirk McKusick 			kp->ki_ppid = proc.p_oppid;
263a58930d8STor Egge 		else if (proc.p_pptr) {
264a58930d8STor Egge 			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
2651f7d2501SKirk McKusick 				_kvm_err(kd, kd->program,
266c10970ddSUlrich Spörlein 				    "can't read pproc at %p", proc.p_pptr);
267a58930d8STor Egge 				return (-1);
268a58930d8STor Egge 			}
2691f7d2501SKirk McKusick 			kp->ki_ppid = pproc.p_pid;
270a58930d8STor Egge 		} else
2711f7d2501SKirk McKusick 			kp->ki_ppid = 0;
2726f8c6a69SPeter Wemm 		if (proc.p_pgrp == NULL)
2736f8c6a69SPeter Wemm 			goto nopgrp;
2746f8c6a69SPeter Wemm 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
275c10970ddSUlrich Spörlein 			_kvm_err(kd, kd->program, "can't read pgrp at %p",
2766f8c6a69SPeter Wemm 				 proc.p_pgrp);
2776f8c6a69SPeter Wemm 			return (-1);
2786f8c6a69SPeter Wemm 		}
2791f7d2501SKirk McKusick 		kp->ki_pgid = pgrp.pg_id;
2801f7d2501SKirk McKusick 		kp->ki_jobc = pgrp.pg_jobc;
28158f0484fSRodney W. Grimes 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
282c10970ddSUlrich Spörlein 			_kvm_err(kd, kd->program, "can't read session at %p",
28358f0484fSRodney W. Grimes 				pgrp.pg_session);
28458f0484fSRodney W. Grimes 			return (-1);
28558f0484fSRodney W. Grimes 		}
2861f7d2501SKirk McKusick 		kp->ki_sid = sess.s_sid;
2871f7d2501SKirk McKusick 		(void)memcpy(kp->ki_login, sess.s_login,
2881f7d2501SKirk McKusick 						sizeof(kp->ki_login));
2891f7d2501SKirk McKusick 		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
2901f7d2501SKirk McKusick 		if (sess.s_leader == p)
2911f7d2501SKirk McKusick 			kp->ki_kiflag |= KI_SLEADER;
29258f0484fSRodney W. Grimes 		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
29358f0484fSRodney W. Grimes 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
29458f0484fSRodney W. Grimes 				_kvm_err(kd, kd->program,
295c10970ddSUlrich Spörlein 					 "can't read tty at %p", sess.s_ttyp);
29658f0484fSRodney W. Grimes 				return (-1);
29758f0484fSRodney W. Grimes 			}
2982bdd5609SPeter Wemm 			if (tty.t_dev != NULL) {
2992bdd5609SPeter Wemm 				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
3002bdd5609SPeter Wemm 					_kvm_err(kd, kd->program,
301c10970ddSUlrich Spörlein 						 "can't read cdev at %p",
3022bdd5609SPeter Wemm 						tty.t_dev);
3032bdd5609SPeter Wemm 					return (-1);
3042bdd5609SPeter Wemm 				}
3059c4fb661SPoul-Henning Kamp #if 0
3062bdd5609SPeter Wemm 				kp->ki_tdev = t_cdev.si_udev;
3079c4fb661SPoul-Henning Kamp #else
3082cfe3fdaSPeter Wemm 				kp->ki_tdev = NODEV;
3099c4fb661SPoul-Henning Kamp #endif
3102bdd5609SPeter Wemm 			}
31158f0484fSRodney W. Grimes 			if (tty.t_pgrp != NULL) {
31258f0484fSRodney W. Grimes 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
31358f0484fSRodney W. Grimes 					_kvm_err(kd, kd->program,
314c10970ddSUlrich Spörlein 						 "can't read tpgrp at %p",
31558f0484fSRodney W. Grimes 						tty.t_pgrp);
31658f0484fSRodney W. Grimes 					return (-1);
31758f0484fSRodney W. Grimes 				}
3181f7d2501SKirk McKusick 				kp->ki_tpgid = pgrp.pg_id;
31958f0484fSRodney W. Grimes 			} else
3201f7d2501SKirk McKusick 				kp->ki_tpgid = -1;
3211f7d2501SKirk McKusick 			if (tty.t_session != NULL) {
3221f7d2501SKirk McKusick 				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
3231f7d2501SKirk McKusick 					_kvm_err(kd, kd->program,
324c10970ddSUlrich Spörlein 					    "can't read session at %p",
3251f7d2501SKirk McKusick 					    tty.t_session);
3261f7d2501SKirk McKusick 					return (-1);
3271f7d2501SKirk McKusick 				}
3281f7d2501SKirk McKusick 				kp->ki_tsid = sess.s_sid;
3291f7d2501SKirk McKusick 			}
3306f8c6a69SPeter Wemm 		} else {
3316f8c6a69SPeter Wemm nopgrp:
3321f7d2501SKirk McKusick 			kp->ki_tdev = NODEV;
3336f8c6a69SPeter Wemm 		}
33471fad9fdSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
33571fad9fdSJulian Elischer 			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
3361f7d2501SKirk McKusick 			    kp->ki_wmesg, WMESGLEN);
33758f0484fSRodney W. Grimes 
33858f0484fSRodney W. Grimes 		(void)kvm_read(kd, (u_long)proc.p_vmspace,
3391f7d2501SKirk McKusick 		    (char *)&vmspace, sizeof(vmspace));
3401f7d2501SKirk McKusick 		kp->ki_size = vmspace.vm_map.size;
3415f494640SSean Bruno 		/*
3425f494640SSean Bruno 		 * Approximate the kernel's method of calculating
3435f494640SSean Bruno 		 * this field.
3445f494640SSean Bruno 		 */
3455f494640SSean Bruno #define		pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
3465f494640SSean Bruno 		kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
3471f7d2501SKirk McKusick 		kp->ki_swrss = vmspace.vm_swrss;
3481f7d2501SKirk McKusick 		kp->ki_tsize = vmspace.vm_tsize;
3491f7d2501SKirk McKusick 		kp->ki_dsize = vmspace.vm_dsize;
3501f7d2501SKirk McKusick 		kp->ki_ssize = vmspace.vm_ssize;
35158f0484fSRodney W. Grimes 
352694127f8SDaniel Eischen 		switch (what & ~KERN_PROC_INC_THREAD) {
35358f0484fSRodney W. Grimes 
35458f0484fSRodney W. Grimes 		case KERN_PROC_PGRP:
3551f7d2501SKirk McKusick 			if (kp->ki_pgid != (pid_t)arg)
35658f0484fSRodney W. Grimes 				continue;
35758f0484fSRodney W. Grimes 			break;
35858f0484fSRodney W. Grimes 
359276de18cSGarance A Drosehn 		case KERN_PROC_SESSION:
360276de18cSGarance A Drosehn 			if (kp->ki_sid != (pid_t)arg)
361276de18cSGarance A Drosehn 				continue;
362276de18cSGarance A Drosehn 			break;
363276de18cSGarance A Drosehn 
36458f0484fSRodney W. Grimes 		case KERN_PROC_TTY:
36558f0484fSRodney W. Grimes 			if ((proc.p_flag & P_CONTROLT) == 0 ||
3661f7d2501SKirk McKusick 			     kp->ki_tdev != (dev_t)arg)
36758f0484fSRodney W. Grimes 				continue;
36858f0484fSRodney W. Grimes 			break;
36958f0484fSRodney W. Grimes 		}
370b7e7c21aSGarance A Drosehn 		if (proc.p_comm[0] != 0)
371b7e7c21aSGarance A Drosehn 			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
372276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
373276de18cSGarance A Drosehn 		    sizeof(sysent));
374276de18cSGarance A Drosehn 		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
375276de18cSGarance A Drosehn 		    sizeof(svname));
376276de18cSGarance A Drosehn 		if (svname[0] != 0)
377276de18cSGarance A Drosehn 			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
378bff4151cSJulian Elischer 		if ((proc.p_state != PRS_ZOMBIE) &&
37971fad9fdSJulian Elischer 		    (mtd.td_blocked != 0)) {
3800d632649SJohn Baldwin 			kp->ki_kiflag |= KI_LOCKBLOCK;
3810d632649SJohn Baldwin 			if (mtd.td_lockname)
382bff4151cSJulian Elischer 				(void)kvm_read(kd,
3830d632649SJohn Baldwin 				    (u_long)mtd.td_lockname,
3840d632649SJohn Baldwin 				    kp->ki_lockname, LOCKNAMELEN);
3850d632649SJohn Baldwin 			kp->ki_lockname[LOCKNAMELEN] = 0;
3861f7d2501SKirk McKusick 		}
387de788839SUlrich Spörlein 		kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
3881f7d2501SKirk McKusick 		kp->ki_pid = proc.p_pid;
389e8a58a83SJuli Mallett 		kp->ki_siglist = proc.p_siglist;
39042d3ad71SJeff Roberson 		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
39131a9779eSJeff Roberson 		kp->ki_sigmask = mtd.td_sigmask;
3921f7d2501SKirk McKusick 		kp->ki_xstat = proc.p_xstat;
3931f7d2501SKirk McKusick 		kp->ki_acflag = proc.p_acflag;
3944f0db5e0SJulian Elischer 		kp->ki_lock = proc.p_lock;
395bff4151cSJulian Elischer 		if (proc.p_state != PRS_ZOMBIE) {
39684a0b303SJeff Roberson 			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
3976143c383SJulian Elischer 			kp->ki_flag = proc.p_flag;
398b61ce5b0SJeff Roberson 			kp->ki_sflag = 0;
39930105366SJulian Elischer 			kp->ki_nice = proc.p_nice;
4001f7d2501SKirk McKusick 			kp->ki_traceflag = proc.p_traceflag;
4016143c383SJulian Elischer 			if (proc.p_state == PRS_NORMAL) {
40271fad9fdSJulian Elischer 				if (TD_ON_RUNQ(&mtd) ||
40371fad9fdSJulian Elischer 				    TD_CAN_RUN(&mtd) ||
40471fad9fdSJulian Elischer 				    TD_IS_RUNNING(&mtd)) {
405e602ba25SJulian Elischer 					kp->ki_stat = SRUN;
4064f0db5e0SJulian Elischer 				} else if (mtd.td_state ==
4074f0db5e0SJulian Elischer 				    TDS_INHIBITED) {
40871fad9fdSJulian Elischer 					if (P_SHOULDSTOP(&proc)) {
409e602ba25SJulian Elischer 						kp->ki_stat = SSTOP;
4104f0db5e0SJulian Elischer 					} else if (
4114f0db5e0SJulian Elischer 					    TD_IS_SLEEPING(&mtd)) {
41271fad9fdSJulian Elischer 						kp->ki_stat = SSLEEP;
4130d632649SJohn Baldwin 					} else if (TD_ON_LOCK(&mtd)) {
4140d632649SJohn Baldwin 						kp->ki_stat = SLOCK;
415e602ba25SJulian Elischer 					} else {
416e602ba25SJulian Elischer 						kp->ki_stat = SWAIT;
417e602ba25SJulian Elischer 					}
41871fad9fdSJulian Elischer 				}
419e602ba25SJulian Elischer 			} else {
420e602ba25SJulian Elischer 				kp->ki_stat = SIDL;
421e602ba25SJulian Elischer 			}
4224f0db5e0SJulian Elischer 			/* Stuff from the thread */
42371fad9fdSJulian Elischer 			kp->ki_pri.pri_level = mtd.td_priority;
42471fad9fdSJulian Elischer 			kp->ki_pri.pri_native = mtd.td_base_pri;
42571fad9fdSJulian Elischer 			kp->ki_lastcpu = mtd.td_lastcpu;
4264f0db5e0SJulian Elischer 			kp->ki_wchan = mtd.td_wchan;
4277ab24ea3SJulian Elischer 			if (mtd.td_name[0] != 0)
428925af544SBjoern A. Zeeb 				strlcpy(kp->ki_tdname, mtd.td_name, MAXCOMLEN);
429a0ddbf49SJulian Elischer 			kp->ki_oncpu = mtd.td_oncpu;
4307ab24ea3SJulian Elischer 			if (mtd.td_name[0] != '\0')
431925af544SBjoern A. Zeeb 				strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname));
432ed062c8dSJulian Elischer 			kp->ki_pctcpu = 0;
433ed062c8dSJulian Elischer 			kp->ki_rqindex = 0;
434*e77f9fedSAdrian Chadd 
435*e77f9fedSAdrian Chadd 			/*
436*e77f9fedSAdrian Chadd 			 * Note: legacy fields; wraps at NO_CPU_OLD or the
437*e77f9fedSAdrian Chadd 			 * old max CPU value as appropriate
438*e77f9fedSAdrian Chadd 			 */
439*e77f9fedSAdrian Chadd 			if (mtd.td_lastcpu == NOCPU)
440*e77f9fedSAdrian Chadd 				kp->ki_lastcpu_old = NOCPU_OLD;
441*e77f9fedSAdrian Chadd 			else if (mtd.td_lastcpu > MAXCPU_OLD)
442*e77f9fedSAdrian Chadd 				kp->ki_lastcpu_old = MAXCPU_OLD;
443*e77f9fedSAdrian Chadd 			else
444*e77f9fedSAdrian Chadd 				kp->ki_lastcpu_old = mtd.td_lastcpu;
445*e77f9fedSAdrian Chadd 
446*e77f9fedSAdrian Chadd 			if (mtd.td_oncpu == NOCPU)
447*e77f9fedSAdrian Chadd 				kp->ki_oncpu_old = NOCPU_OLD;
448*e77f9fedSAdrian Chadd 			else if (mtd.td_oncpu > MAXCPU_OLD)
449*e77f9fedSAdrian Chadd 				kp->ki_oncpu_old = MAXCPU_OLD;
450*e77f9fedSAdrian Chadd 			else
451*e77f9fedSAdrian Chadd 				kp->ki_oncpu_old = mtd.td_oncpu;
4524f0db5e0SJulian Elischer 		} else {
4536143c383SJulian Elischer 			kp->ki_stat = SZOMB;
4546143c383SJulian Elischer 		}
4551f7d2501SKirk McKusick 		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
45658f0484fSRodney W. Grimes 		++bp;
45758f0484fSRodney W. Grimes 		++cnt;
45858f0484fSRodney W. Grimes 	}
45958f0484fSRodney W. Grimes 	return (cnt);
46058f0484fSRodney W. Grimes }
46158f0484fSRodney W. Grimes 
46258f0484fSRodney W. Grimes /*
46358f0484fSRodney W. Grimes  * Build proc info array by reading in proc list from a crash dump.
46458f0484fSRodney W. Grimes  * Return number of procs read.  maxcnt is the max we will read.
46558f0484fSRodney W. Grimes  */
46658f0484fSRodney W. Grimes static int
467c10970ddSUlrich Spörlein kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
468c10970ddSUlrich Spörlein     u_long a_zombproc, int maxcnt)
46958f0484fSRodney W. Grimes {
470be04b6d1SDavid E. O'Brien 	struct kinfo_proc *bp = kd->procbase;
471be04b6d1SDavid E. O'Brien 	int acnt, zcnt;
47258f0484fSRodney W. Grimes 	struct proc *p;
47358f0484fSRodney W. Grimes 
47458f0484fSRodney W. Grimes 	if (KREAD(kd, a_allproc, &p)) {
47558f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read allproc");
47658f0484fSRodney W. Grimes 		return (-1);
47758f0484fSRodney W. Grimes 	}
47858f0484fSRodney W. Grimes 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
47958f0484fSRodney W. Grimes 	if (acnt < 0)
48058f0484fSRodney W. Grimes 		return (acnt);
48158f0484fSRodney W. Grimes 
48258f0484fSRodney W. Grimes 	if (KREAD(kd, a_zombproc, &p)) {
48358f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "cannot read zombproc");
48458f0484fSRodney W. Grimes 		return (-1);
48558f0484fSRodney W. Grimes 	}
48658f0484fSRodney W. Grimes 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
48758f0484fSRodney W. Grimes 	if (zcnt < 0)
48858f0484fSRodney W. Grimes 		zcnt = 0;
48958f0484fSRodney W. Grimes 
49058f0484fSRodney W. Grimes 	return (acnt + zcnt);
49158f0484fSRodney W. Grimes }
49258f0484fSRodney W. Grimes 
49358f0484fSRodney W. Grimes struct kinfo_proc *
494c10970ddSUlrich Spörlein kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
49558f0484fSRodney W. Grimes {
496c2ac238cSDoug Rabson 	int mib[4], st, nprocs;
497767993ecSMikolaj Golub 	size_t size, osize;
498694127f8SDaniel Eischen 	int temp_op;
49958f0484fSRodney W. Grimes 
50058f0484fSRodney W. Grimes 	if (kd->procbase != 0) {
50158f0484fSRodney W. Grimes 		free((void *)kd->procbase);
50258f0484fSRodney W. Grimes 		/*
50358f0484fSRodney W. Grimes 		 * Clear this pointer in case this call fails.  Otherwise,
50458f0484fSRodney W. Grimes 		 * kvm_close() will free it again.
50558f0484fSRodney W. Grimes 		 */
50658f0484fSRodney W. Grimes 		kd->procbase = 0;
50758f0484fSRodney W. Grimes 	}
50858f0484fSRodney W. Grimes 	if (ISALIVE(kd)) {
50958f0484fSRodney W. Grimes 		size = 0;
51058f0484fSRodney W. Grimes 		mib[0] = CTL_KERN;
51158f0484fSRodney W. Grimes 		mib[1] = KERN_PROC;
51258f0484fSRodney W. Grimes 		mib[2] = op;
51358f0484fSRodney W. Grimes 		mib[3] = arg;
514694127f8SDaniel Eischen 		temp_op = op & ~KERN_PROC_INC_THREAD;
515694127f8SDaniel Eischen 		st = sysctl(mib,
516694127f8SDaniel Eischen 		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
517f2dd06abSTim J. Robbins 		    3 : 4, NULL, &size, NULL, 0);
51858f0484fSRodney W. Grimes 		if (st == -1) {
51958f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
52058f0484fSRodney W. Grimes 			return (0);
52158f0484fSRodney W. Grimes 		}
52221687047SDima Dorfman 		/*
52321687047SDima Dorfman 		 * We can't continue with a size of 0 because we pass
52421687047SDima Dorfman 		 * it to realloc() (via _kvm_realloc()), and passing 0
52521687047SDima Dorfman 		 * to realloc() results in undefined behavior.
52621687047SDima Dorfman 		 */
52721687047SDima Dorfman 		if (size == 0) {
52821687047SDima Dorfman 			/*
52921687047SDima Dorfman 			 * XXX: We should probably return an invalid,
53021687047SDima Dorfman 			 * but non-NULL, pointer here so any client
53121687047SDima Dorfman 			 * program trying to dereference it will
53221687047SDima Dorfman 			 * crash.  However, _kvm_freeprocs() calls
53321687047SDima Dorfman 			 * free() on kd->procbase if it isn't NULL,
53421687047SDima Dorfman 			 * and free()'ing a junk pointer isn't good.
53521687047SDima Dorfman 			 * Then again, _kvm_freeprocs() isn't used
53621687047SDima Dorfman 			 * anywhere . . .
53721687047SDima Dorfman 			 */
53821687047SDima Dorfman 			kd->procbase = _kvm_malloc(kd, 1);
53921687047SDima Dorfman 			goto liveout;
54021687047SDima Dorfman 		}
541b2d3d0f0SDag-Erling Smørgrav 		do {
542b2d3d0f0SDag-Erling Smørgrav 			size += size / 10;
543b2d3d0f0SDag-Erling Smørgrav 			kd->procbase = (struct kinfo_proc *)
544b2d3d0f0SDag-Erling Smørgrav 			    _kvm_realloc(kd, kd->procbase, size);
54558f0484fSRodney W. Grimes 			if (kd->procbase == 0)
54658f0484fSRodney W. Grimes 				return (0);
547767993ecSMikolaj Golub 			osize = size;
548694127f8SDaniel Eischen 			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
549694127f8SDaniel Eischen 			    temp_op == KERN_PROC_PROC ? 3 : 4,
550b2d3d0f0SDag-Erling Smørgrav 			    kd->procbase, &size, NULL, 0);
551767993ecSMikolaj Golub 		} while (st == -1 && errno == ENOMEM && size == osize);
55258f0484fSRodney W. Grimes 		if (st == -1) {
55358f0484fSRodney W. Grimes 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
55458f0484fSRodney W. Grimes 			return (0);
55558f0484fSRodney W. Grimes 		}
55621687047SDima Dorfman 		/*
55721687047SDima Dorfman 		 * We have to check the size again because sysctl()
55821687047SDima Dorfman 		 * may "round up" oldlenp if oldp is NULL; hence it
55921687047SDima Dorfman 		 * might've told us that there was data to get when
56021687047SDima Dorfman 		 * there really isn't any.
56121687047SDima Dorfman 		 */
5620627f53bSDavid Malone 		if (size > 0 &&
5630627f53bSDavid Malone 		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
56458f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
565c10970ddSUlrich Spörlein 			    "kinfo_proc size mismatch (expected %zu, got %d)",
5661f7d2501SKirk McKusick 			    sizeof(struct kinfo_proc),
5671f7d2501SKirk McKusick 			    kd->procbase->ki_structsize);
56858f0484fSRodney W. Grimes 			return (0);
56958f0484fSRodney W. Grimes 		}
57021687047SDima Dorfman liveout:
571d339edcfSDavid Malone 		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
57258f0484fSRodney W. Grimes 	} else {
573de788839SUlrich Spörlein 		struct nlist nl[7], *p;
57458f0484fSRodney W. Grimes 
57558f0484fSRodney W. Grimes 		nl[0].n_name = "_nprocs";
57658f0484fSRodney W. Grimes 		nl[1].n_name = "_allproc";
57758f0484fSRodney W. Grimes 		nl[2].n_name = "_zombproc";
57884a0b303SJeff Roberson 		nl[3].n_name = "_ticks";
57984a0b303SJeff Roberson 		nl[4].n_name = "_hz";
580de788839SUlrich Spörlein 		nl[5].n_name = "_cpu_tick_frequency";
581de788839SUlrich Spörlein 		nl[6].n_name = 0;
58258f0484fSRodney W. Grimes 
58358f0484fSRodney W. Grimes 		if (kvm_nlist(kd, nl) != 0) {
58458f0484fSRodney W. Grimes 			for (p = nl; p->n_type != 0; ++p)
58558f0484fSRodney W. Grimes 				;
58658f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program,
58758f0484fSRodney W. Grimes 				 "%s: no such symbol", p->n_name);
58858f0484fSRodney W. Grimes 			return (0);
58958f0484fSRodney W. Grimes 		}
59058f0484fSRodney W. Grimes 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
59158f0484fSRodney W. Grimes 			_kvm_err(kd, kd->program, "can't read nprocs");
59258f0484fSRodney W. Grimes 			return (0);
59358f0484fSRodney W. Grimes 		}
59484a0b303SJeff Roberson 		if (KREAD(kd, nl[3].n_value, &ticks)) {
59584a0b303SJeff Roberson 			_kvm_err(kd, kd->program, "can't read ticks");
59684a0b303SJeff Roberson 			return (0);
59784a0b303SJeff Roberson 		}
59884a0b303SJeff Roberson 		if (KREAD(kd, nl[4].n_value, &hz)) {
59984a0b303SJeff Roberson 			_kvm_err(kd, kd->program, "can't read hz");
60084a0b303SJeff Roberson 			return (0);
60184a0b303SJeff Roberson 		}
602de788839SUlrich Spörlein 		if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) {
603de788839SUlrich Spörlein 			_kvm_err(kd, kd->program,
604de788839SUlrich Spörlein 			    "can't read cpu_tick_frequency");
605de788839SUlrich Spörlein 			return (0);
606de788839SUlrich Spörlein 		}
60758f0484fSRodney W. Grimes 		size = nprocs * sizeof(struct kinfo_proc);
60858f0484fSRodney W. Grimes 		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
60958f0484fSRodney W. Grimes 		if (kd->procbase == 0)
61058f0484fSRodney W. Grimes 			return (0);
61158f0484fSRodney W. Grimes 
61258f0484fSRodney W. Grimes 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
61358f0484fSRodney W. Grimes 				      nl[2].n_value, nprocs);
614ca895af3SAndriy Gapon 		if (nprocs <= 0) {
615ca895af3SAndriy Gapon 			_kvm_freeprocs(kd);
616ca895af3SAndriy Gapon 			nprocs = 0;
617ca895af3SAndriy Gapon 		}
61858f0484fSRodney W. Grimes #ifdef notdef
619ca895af3SAndriy Gapon 		else {
62058f0484fSRodney W. Grimes 			size = nprocs * sizeof(struct kinfo_proc);
621ca895af3SAndriy Gapon 			kd->procbase = realloc(kd->procbase, size);
622ca895af3SAndriy Gapon 		}
62358f0484fSRodney W. Grimes #endif
62458f0484fSRodney W. Grimes 	}
62558f0484fSRodney W. Grimes 	*cnt = nprocs;
62658f0484fSRodney W. Grimes 	return (kd->procbase);
62758f0484fSRodney W. Grimes }
62858f0484fSRodney W. Grimes 
62958f0484fSRodney W. Grimes void
630c10970ddSUlrich Spörlein _kvm_freeprocs(kvm_t *kd)
63158f0484fSRodney W. Grimes {
63258f0484fSRodney W. Grimes 	if (kd->procbase) {
63358f0484fSRodney W. Grimes 		free(kd->procbase);
63458f0484fSRodney W. Grimes 		kd->procbase = 0;
63558f0484fSRodney W. Grimes 	}
63658f0484fSRodney W. Grimes }
63758f0484fSRodney W. Grimes 
63858f0484fSRodney W. Grimes void *
639c10970ddSUlrich Spörlein _kvm_realloc(kvm_t *kd, void *p, size_t n)
64058f0484fSRodney W. Grimes {
64158f0484fSRodney W. Grimes 	void *np = (void *)realloc(p, n);
64258f0484fSRodney W. Grimes 
643e8420087SWarner Losh 	if (np == 0) {
644e8420087SWarner Losh 		free(p);
64558f0484fSRodney W. Grimes 		_kvm_err(kd, kd->program, "out of memory");
646e8420087SWarner Losh 	}
64758f0484fSRodney W. Grimes 	return (np);
64858f0484fSRodney W. Grimes }
64958f0484fSRodney W. Grimes 
65058f0484fSRodney W. Grimes /*
651ee5169dcSMikolaj Golub  * Get the command args or environment.
65258f0484fSRodney W. Grimes  */
65358f0484fSRodney W. Grimes static char **
654ee5169dcSMikolaj Golub kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
65558f0484fSRodney W. Grimes {
656b9df5231SPoul-Henning Kamp 	int oid[4];
657b7875890SDavid E. O'Brien 	int i;
658b7875890SDavid E. O'Brien 	size_t bufsz;
659ee5169dcSMikolaj Golub 	static int buflen;
660b9df5231SPoul-Henning Kamp 	static char *buf, *p;
661b9df5231SPoul-Henning Kamp 	static char **bufp;
662b9df5231SPoul-Henning Kamp 	static int argc;
663b9df5231SPoul-Henning Kamp 
664c4a7cdb3SPeter Wemm 	if (!ISALIVE(kd)) {
665c4a7cdb3SPeter Wemm 		_kvm_err(kd, kd->program,
666c4a7cdb3SPeter Wemm 		    "cannot read user space from dead kernel");
667c4a7cdb3SPeter Wemm 		return (0);
668c4a7cdb3SPeter Wemm 	}
669c4a7cdb3SPeter Wemm 
670ee5169dcSMikolaj Golub 	if (nchr == 0 || nchr > ARG_MAX)
671ee5169dcSMikolaj Golub 		nchr = ARG_MAX;
672ee5169dcSMikolaj Golub 	if (buflen == 0) {
673ee5169dcSMikolaj Golub 		buf = malloc(nchr);
674ee5169dcSMikolaj Golub 		if (buf == NULL) {
675ee5169dcSMikolaj Golub 			_kvm_err(kd, kd->program, "cannot allocate memory");
676ee5169dcSMikolaj Golub 			return (0);
677ee5169dcSMikolaj Golub 		}
678ee5169dcSMikolaj Golub 		buflen = nchr;
679b9df5231SPoul-Henning Kamp 		argc = 32;
680b9df5231SPoul-Henning Kamp 		bufp = malloc(sizeof(char *) * argc);
681ee5169dcSMikolaj Golub 	} else if (nchr > buflen) {
682ee5169dcSMikolaj Golub 		p = realloc(buf, nchr);
683ee5169dcSMikolaj Golub 		if (p != NULL) {
684ee5169dcSMikolaj Golub 			buf = p;
685ee5169dcSMikolaj Golub 			buflen = nchr;
686b9df5231SPoul-Henning Kamp 		}
687b9df5231SPoul-Henning Kamp 	}
688b9df5231SPoul-Henning Kamp 	oid[0] = CTL_KERN;
689b9df5231SPoul-Henning Kamp 	oid[1] = KERN_PROC;
690ee5169dcSMikolaj Golub 	oid[2] = env ? KERN_PROC_ENV : KERN_PROC_ARGS;
6911f7d2501SKirk McKusick 	oid[3] = kp->ki_pid;
692b7875890SDavid E. O'Brien 	bufsz = buflen;
693952a0c3eSMikolaj Golub 	if (sysctl(oid, 4, buf, &bufsz, 0, 0) == -1) {
694952a0c3eSMikolaj Golub 		/*
695952a0c3eSMikolaj Golub 		 * If the supplied buf is too short to hold the requested
696952a0c3eSMikolaj Golub 		 * value the sysctl returns with ENOMEM. The buf is filled
697952a0c3eSMikolaj Golub 		 * with the truncated value and the returned bufsz is equal
698952a0c3eSMikolaj Golub 		 * to the requested len.
699952a0c3eSMikolaj Golub 		 */
700952a0c3eSMikolaj Golub 		if (errno != ENOMEM || bufsz != (size_t)buflen)
701952a0c3eSMikolaj Golub 			return (0);
702952a0c3eSMikolaj Golub 		buf[bufsz - 1] = '\0';
703952a0c3eSMikolaj Golub 		errno = 0;
704952a0c3eSMikolaj Golub 	} else if (bufsz == 0) {
705952a0c3eSMikolaj Golub 		return (0);
706952a0c3eSMikolaj Golub 	}
707b9df5231SPoul-Henning Kamp 	i = 0;
708b9df5231SPoul-Henning Kamp 	p = buf;
709b9df5231SPoul-Henning Kamp 	do {
710b9df5231SPoul-Henning Kamp 		bufp[i++] = p;
711b9df5231SPoul-Henning Kamp 		p += strlen(p) + 1;
712b9df5231SPoul-Henning Kamp 		if (i >= argc) {
713b9df5231SPoul-Henning Kamp 			argc += argc;
714b9df5231SPoul-Henning Kamp 			bufp = realloc(bufp,
715b9df5231SPoul-Henning Kamp 			    sizeof(char *) * argc);
716b9df5231SPoul-Henning Kamp 		}
717b7875890SDavid E. O'Brien 	} while (p < buf + bufsz);
718b9df5231SPoul-Henning Kamp 	bufp[i++] = 0;
719b9df5231SPoul-Henning Kamp 	return (bufp);
720b9df5231SPoul-Henning Kamp }
721ee5169dcSMikolaj Golub 
722ee5169dcSMikolaj Golub char **
723ee5169dcSMikolaj Golub kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
724ee5169dcSMikolaj Golub {
725ee5169dcSMikolaj Golub 	return (kvm_argv(kd, kp, 0, nchr));
72658f0484fSRodney W. Grimes }
72758f0484fSRodney W. Grimes 
72858f0484fSRodney W. Grimes char **
729c10970ddSUlrich Spörlein kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
73058f0484fSRodney W. Grimes {
731ee5169dcSMikolaj Golub 	return (kvm_argv(kd, kp, 1, nchr));
73258f0484fSRodney W. Grimes }
733