xref: /minix3/lib/libkvm/kvm_proc.c (revision 9da227510d14c35cccbd92c6a9e316b4079c4911)
1 /*	$NetBSD: kvm_proc.c,v 1.90 2014/02/19 20:21:22 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1989, 1992, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * This code is derived from software developed by the Computer Systems
37  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
38  * BG 91-66 and contributed to Berkeley.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  */
64 
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
67 #if 0
68 static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
69 #else
70 __RCSID("$NetBSD: kvm_proc.c,v 1.90 2014/02/19 20:21:22 dsl Exp $");
71 #endif
72 #endif /* LIBC_SCCS and not lint */
73 
74 /*
75  * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
76  * users of this code, so we've factored it out into a separate module.
77  * Thus, we keep this grunge out of the other kvm applications (i.e.,
78  * most other applications are interested only in open/close/read/nlist).
79  */
80 
81 #include <sys/param.h>
82 #include <sys/lwp.h>
83 #include <sys/proc.h>
84 #include <sys/exec.h>
85 #include <sys/stat.h>
86 #include <sys/ioctl.h>
87 #include <sys/tty.h>
88 #include <sys/resourcevar.h>
89 #include <sys/mutex.h>
90 #include <sys/specificdata.h>
91 #include <sys/types.h>
92 
93 #include <errno.h>
94 #include <stdlib.h>
95 #include <stddef.h>
96 #include <string.h>
97 #include <unistd.h>
98 #include <nlist.h>
99 #include <kvm.h>
100 
101 #include <uvm/uvm_extern.h>
102 #include <uvm/uvm_param.h>
103 #include <uvm/uvm_amap.h>
104 #include <uvm/uvm_page.h>
105 
106 #include <sys/sysctl.h>
107 
108 #include <limits.h>
109 #include <db.h>
110 #include <paths.h>
111 
112 #include "kvm_private.h"
113 
114 /*
115  * Common info from kinfo_proc and kinfo_proc2 used by helper routines.
116  */
117 struct miniproc {
118 	struct	vmspace *p_vmspace;
119 	char	p_stat;
120 	struct	proc *p_paddr;
121 	pid_t	p_pid;
122 };
123 
124 /*
125  * Convert from struct proc and kinfo_proc{,2} to miniproc.
126  */
127 #define PTOMINI(kp, p) \
128 	do { \
129 		(p)->p_stat = (kp)->p_stat; \
130 		(p)->p_pid = (kp)->p_pid; \
131 		(p)->p_paddr = NULL; \
132 		(p)->p_vmspace = (kp)->p_vmspace; \
133 	} while (/*CONSTCOND*/0);
134 
135 #define KPTOMINI(kp, p) \
136 	do { \
137 		(p)->p_stat = (kp)->kp_proc.p_stat; \
138 		(p)->p_pid = (kp)->kp_proc.p_pid; \
139 		(p)->p_paddr = (kp)->kp_eproc.e_paddr; \
140 		(p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
141 	} while (/*CONSTCOND*/0);
142 
143 #define KP2TOMINI(kp, p) \
144 	do { \
145 		(p)->p_stat = (kp)->p_stat; \
146 		(p)->p_pid = (kp)->p_pid; \
147 		(p)->p_paddr = (void *)(long)(kp)->p_paddr; \
148 		(p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
149 	} while (/*CONSTCOND*/0);
150 
151 /*
152  * NetBSD uses kauth(9) to manage credentials, which are stored in kauth_cred_t,
153  * a kernel-only opaque type. This is an embedded version which is *INTERNAL* to
154  * kvm(3) so dumps can be read properly.
155  *
156  * Whenever NetBSD starts exporting credentials to userland consistently (using
157  * 'struct uucred', or something) this will have to be updated again.
158  */
159 struct kvm_kauth_cred {
160 	u_int cr_refcnt;		/* reference count */
161 	uint8_t cr_pad[CACHE_LINE_SIZE - sizeof(u_int)];
162 	uid_t cr_uid;			/* user id */
163 	uid_t cr_euid;			/* effective user id */
164 	uid_t cr_svuid;			/* saved effective user id */
165 	gid_t cr_gid;			/* group id */
166 	gid_t cr_egid;			/* effective group id */
167 	gid_t cr_svgid;			/* saved effective group id */
168 	u_int cr_ngroups;		/* number of groups */
169 	gid_t cr_groups[NGROUPS];	/* group memberships */
170 	specificdata_reference cr_sd;	/* specific data */
171 };
172 
173 /* XXX: What uses these two functions? */
174 char		*_kvm_uread(kvm_t *, const struct proc *, u_long, u_long *);
175 ssize_t		kvm_uread(kvm_t *, const struct proc *, u_long, char *,
176 		    size_t);
177 
178 static char	*_kvm_ureadm(kvm_t *, const struct miniproc *, u_long,
179 		    u_long *);
180 static ssize_t	kvm_ureadm(kvm_t *, const struct miniproc *, u_long,
181 		    char *, size_t);
182 
183 static char	**kvm_argv(kvm_t *, const struct miniproc *, u_long, int, int);
184 static int	kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int);
185 static char	**kvm_doargv(kvm_t *, const struct miniproc *, int,
186 		    void (*)(struct ps_strings *, u_long *, int *));
187 static char	**kvm_doargv2(kvm_t *, pid_t, int, int);
188 static int	kvm_proclist(kvm_t *, int, int, struct proc *,
189 		    struct kinfo_proc *, int);
190 static int	proc_verify(kvm_t *, u_long, const struct miniproc *);
191 static void	ps_str_a(struct ps_strings *, u_long *, int *);
192 static void	ps_str_e(struct ps_strings *, u_long *, int *);
193 
194 
195 static char *
_kvm_ureadm(kvm_t * kd,const struct miniproc * p,u_long va,u_long * cnt)196 _kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long va, u_long *cnt)
197 {
198 	u_long addr, head;
199 	u_long offset;
200 	struct vm_map_entry vme;
201 	struct vm_amap amap;
202 	struct vm_anon *anonp, anon;
203 	struct vm_page pg;
204 	u_long slot;
205 
206 	if (kd->swapspc == NULL) {
207 		kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg);
208 		if (kd->swapspc == NULL)
209 			return (NULL);
210 	}
211 
212 	/*
213 	 * Look through the address map for the memory object
214 	 * that corresponds to the given virtual address.
215 	 * The header just has the entire valid range.
216 	 */
217 	head = (u_long)&p->p_vmspace->vm_map.header;
218 	addr = head;
219 	for (;;) {
220 		if (KREAD(kd, addr, &vme))
221 			return (NULL);
222 
223 		if (va >= vme.start && va < vme.end &&
224 		    vme.aref.ar_amap != NULL)
225 			break;
226 
227 		addr = (u_long)vme.next;
228 		if (addr == head)
229 			return (NULL);
230 	}
231 
232 	/*
233 	 * we found the map entry, now to find the object...
234 	 */
235 	if (vme.aref.ar_amap == NULL)
236 		return (NULL);
237 
238 	addr = (u_long)vme.aref.ar_amap;
239 	if (KREAD(kd, addr, &amap))
240 		return (NULL);
241 
242 	offset = va - vme.start;
243 	slot = offset / kd->nbpg + vme.aref.ar_pageoff;
244 	/* sanity-check slot number */
245 	if (slot > amap.am_nslot)
246 		return (NULL);
247 
248 	addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
249 	if (KREAD(kd, addr, &anonp))
250 		return (NULL);
251 
252 	addr = (u_long)anonp;
253 	if (KREAD(kd, addr, &anon))
254 		return (NULL);
255 
256 	addr = (u_long)anon.an_page;
257 	if (addr) {
258 		if (KREAD(kd, addr, &pg))
259 			return (NULL);
260 
261 		if (_kvm_pread(kd, kd->pmfd, kd->swapspc, (size_t)kd->nbpg,
262 		    (off_t)pg.phys_addr) != kd->nbpg)
263 			return (NULL);
264 	} else {
265 		if (kd->swfd < 0 ||
266 		    _kvm_pread(kd, kd->swfd, kd->swapspc, (size_t)kd->nbpg,
267 		    (off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
268 			return (NULL);
269 	}
270 
271 	/* Found the page. */
272 	offset %= kd->nbpg;
273 	*cnt = kd->nbpg - offset;
274 	return (&kd->swapspc[(size_t)offset]);
275 }
276 
277 char *
_kvm_uread(kvm_t * kd,const struct proc * p,u_long va,u_long * cnt)278 _kvm_uread(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt)
279 {
280 	struct miniproc mp;
281 
282 	PTOMINI(p, &mp);
283 	return (_kvm_ureadm(kd, &mp, va, cnt));
284 }
285 
286 /*
287  * Convert credentials located in kernel space address 'cred' and store
288  * them in the appropriate members of 'eproc'.
289  */
290 static int
_kvm_convertcred(kvm_t * kd,u_long cred,struct eproc * eproc)291 _kvm_convertcred(kvm_t *kd, u_long cred, struct eproc *eproc)
292 {
293 	struct kvm_kauth_cred kauthcred;
294 	struct ki_pcred *pc = &eproc->e_pcred;
295 	struct ki_ucred *uc = &eproc->e_ucred;
296 
297 	if (KREAD(kd, cred, &kauthcred) != 0)
298 		return (-1);
299 
300 	/* inlined version of kauth_cred_to_pcred, see kauth(9). */
301 	pc->p_ruid = kauthcred.cr_uid;
302 	pc->p_svuid = kauthcred.cr_svuid;
303 	pc->p_rgid = kauthcred.cr_gid;
304 	pc->p_svgid = kauthcred.cr_svgid;
305 	pc->p_refcnt = kauthcred.cr_refcnt;
306 	pc->p_pad = NULL;
307 
308 	/* inlined version of kauth_cred_to_ucred(), see kauth(9). */
309 	uc->cr_ref = kauthcred.cr_refcnt;
310 	uc->cr_uid = kauthcred.cr_euid;
311 	uc->cr_gid = kauthcred.cr_egid;
312 	uc->cr_ngroups = (uint32_t)MIN(kauthcred.cr_ngroups,
313 	    sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
314 	memcpy(uc->cr_groups, kauthcred.cr_groups,
315 	    uc->cr_ngroups * sizeof(uc->cr_groups[0]));
316 
317 	return (0);
318 }
319 
320 /*
321  * Read proc's from memory file into buffer bp, which has space to hold
322  * at most maxcnt procs.
323  */
324 static int
kvm_proclist(kvm_t * kd,int what,int arg,struct proc * p,struct kinfo_proc * bp,int maxcnt)325 kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
326 	     struct kinfo_proc *bp, int maxcnt)
327 {
328 	int cnt = 0;
329 	int nlwps;
330 	struct kinfo_lwp *kl;
331 	struct eproc eproc;
332 	struct pgrp pgrp;
333 	struct session sess;
334 	struct tty tty;
335 	struct proc proc;
336 
337 	for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
338 		if (KREAD(kd, (u_long)p, &proc)) {
339 			_kvm_err(kd, kd->program, "can't read proc at %p", p);
340 			return (-1);
341 		}
342 		if (_kvm_convertcred(kd, (u_long)proc.p_cred, &eproc) != 0) {
343 			_kvm_err(kd, kd->program,
344 			    "can't read proc credentials at %p", p);
345 			return (-1);
346 		}
347 
348 		switch (what) {
349 
350 		case KERN_PROC_PID:
351 			if (proc.p_pid != (pid_t)arg)
352 				continue;
353 			break;
354 
355 		case KERN_PROC_UID:
356 			if (eproc.e_ucred.cr_uid != (uid_t)arg)
357 				continue;
358 			break;
359 
360 		case KERN_PROC_RUID:
361 			if (eproc.e_pcred.p_ruid != (uid_t)arg)
362 				continue;
363 			break;
364 		}
365 		/*
366 		 * We're going to add another proc to the set.  If this
367 		 * will overflow the buffer, assume the reason is because
368 		 * nprocs (or the proc list) is corrupt and declare an error.
369 		 */
370 		if (cnt >= maxcnt) {
371 			_kvm_err(kd, kd->program, "nprocs corrupt");
372 			return (-1);
373 		}
374 		/*
375 		 * gather eproc
376 		 */
377 		eproc.e_paddr = p;
378 		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
379 			_kvm_err(kd, kd->program, "can't read pgrp at %p",
380 			    proc.p_pgrp);
381 			return (-1);
382 		}
383 		eproc.e_sess = pgrp.pg_session;
384 		eproc.e_pgid = pgrp.pg_id;
385 		eproc.e_jobc = pgrp.pg_jobc;
386 		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
387 			_kvm_err(kd, kd->program, "can't read session at %p",
388 			    pgrp.pg_session);
389 			return (-1);
390 		}
391 		if ((proc.p_lflag & PL_CONTROLT) && sess.s_ttyp != NULL) {
392 			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
393 				_kvm_err(kd, kd->program,
394 				    "can't read tty at %p", sess.s_ttyp);
395 				return (-1);
396 			}
397 			eproc.e_tdev = (uint32_t)tty.t_dev;
398 			eproc.e_tsess = tty.t_session;
399 			if (tty.t_pgrp != NULL) {
400 				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
401 					_kvm_err(kd, kd->program,
402 					    "can't read tpgrp at %p",
403 					    tty.t_pgrp);
404 					return (-1);
405 				}
406 				eproc.e_tpgid = pgrp.pg_id;
407 			} else
408 				eproc.e_tpgid = -1;
409 		} else
410 			eproc.e_tdev = (uint32_t)NODEV;
411 		eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
412 		eproc.e_sid = sess.s_sid;
413 		if (sess.s_leader == p)
414 			eproc.e_flag |= EPROC_SLEADER;
415 		/*
416 		 * Fill in the old-style proc.p_wmesg by copying the wmesg
417 		 * from the first available LWP.
418 		 */
419 		kl = kvm_getlwps(kd, proc.p_pid,
420 		    (u_long)PTRTOUINT64(eproc.e_paddr),
421 		    sizeof(struct kinfo_lwp), &nlwps);
422 		if (kl) {
423 			if (nlwps > 0) {
424 				strcpy(eproc.e_wmesg, kl[0].l_wmesg);
425 			}
426 		}
427 		(void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm,
428 		    sizeof(eproc.e_vm));
429 
430 		eproc.e_xsize = eproc.e_xrssize = 0;
431 		eproc.e_xccount = eproc.e_xswrss = 0;
432 
433 		switch (what) {
434 
435 		case KERN_PROC_PGRP:
436 			if (eproc.e_pgid != (pid_t)arg)
437 				continue;
438 			break;
439 
440 		case KERN_PROC_TTY:
441 			if ((proc.p_lflag & PL_CONTROLT) == 0 ||
442 			    eproc.e_tdev != (dev_t)arg)
443 				continue;
444 			break;
445 		}
446 		memcpy(&bp->kp_proc, &proc, sizeof(proc));
447 		memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
448 		++bp;
449 		++cnt;
450 	}
451 	return (cnt);
452 }
453 
454 /*
455  * Build proc info array by reading in proc list from a crash dump.
456  * Return number of procs read.  maxcnt is the max we will read.
457  */
458 static int
kvm_deadprocs(kvm_t * kd,int what,int arg,u_long a_allproc,u_long a_zombproc,int maxcnt)459 kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
460 	      u_long a_zombproc, int maxcnt)
461 {
462 	struct kinfo_proc *bp = kd->procbase;
463 	int acnt, zcnt;
464 	struct proc *p;
465 
466 	if (KREAD(kd, a_allproc, &p)) {
467 		_kvm_err(kd, kd->program, "cannot read allproc");
468 		return (-1);
469 	}
470 	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
471 	if (acnt < 0)
472 		return (acnt);
473 
474 	if (KREAD(kd, a_zombproc, &p)) {
475 		_kvm_err(kd, kd->program, "cannot read zombproc");
476 		return (-1);
477 	}
478 	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
479 	    maxcnt - acnt);
480 	if (zcnt < 0)
481 		zcnt = 0;
482 
483 	return (acnt + zcnt);
484 }
485 
486 struct kinfo_proc2 *
kvm_getproc2(kvm_t * kd,int op,int arg,size_t esize,int * cnt)487 kvm_getproc2(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
488 {
489 	size_t size;
490 	int mib[6], st, nprocs;
491 	struct pstats pstats;
492 
493 	if (ISSYSCTL(kd)) {
494 		size = 0;
495 		mib[0] = CTL_KERN;
496 		mib[1] = KERN_PROC2;
497 		mib[2] = op;
498 		mib[3] = arg;
499 		mib[4] = (int)esize;
500 again:
501 		mib[5] = 0;
502 		st = sysctl(mib, 6, NULL, &size, NULL, (size_t)0);
503 		if (st == -1) {
504 			_kvm_syserr(kd, kd->program, "kvm_getproc2");
505 			return (NULL);
506 		}
507 
508 		mib[5] = (int) (size / esize);
509 		KVM_ALLOC(kd, procbase2, size);
510 		st = sysctl(mib, 6, kd->procbase2, &size, NULL, (size_t)0);
511 		if (st == -1) {
512 			if (errno == ENOMEM) {
513 				goto again;
514 			}
515 			_kvm_syserr(kd, kd->program, "kvm_getproc2");
516 			return (NULL);
517 		}
518 		nprocs = (int) (size / esize);
519 	} else {
520 		char *kp2c;
521 		struct kinfo_proc *kp;
522 		struct kinfo_proc2 kp2, *kp2p;
523 		struct kinfo_lwp *kl;
524 		int i, nlwps;
525 
526 		kp = kvm_getprocs(kd, op, arg, &nprocs);
527 		if (kp == NULL)
528 			return (NULL);
529 
530 		size = nprocs * esize;
531 		KVM_ALLOC(kd, procbase2, size);
532 		kp2c = (char *)(void *)kd->procbase2;
533 		kp2p = &kp2;
534 		for (i = 0; i < nprocs; i++, kp++) {
535 			struct timeval tv;
536 
537 			kl = kvm_getlwps(kd, kp->kp_proc.p_pid,
538 			    (u_long)PTRTOUINT64(kp->kp_eproc.e_paddr),
539 			    sizeof(struct kinfo_lwp), &nlwps);
540 
541 			if (kl == NULL) {
542 				_kvm_syserr(kd, NULL,
543 					"kvm_getlwps() failed on process %u\n",
544 					kp->kp_proc.p_pid);
545 				if (nlwps == 0)
546 					return NULL;
547 				else
548 					continue;
549 			}
550 
551 			/* We use kl[0] as the "representative" LWP */
552 			memset(kp2p, 0, sizeof(kp2));
553 			kp2p->p_forw = kl[0].l_forw;
554 			kp2p->p_back = kl[0].l_back;
555 			kp2p->p_paddr = PTRTOUINT64(kp->kp_eproc.e_paddr);
556 			kp2p->p_addr = kl[0].l_addr;
557 			kp2p->p_fd = PTRTOUINT64(kp->kp_proc.p_fd);
558 			kp2p->p_cwdi = PTRTOUINT64(kp->kp_proc.p_cwdi);
559 			kp2p->p_stats = PTRTOUINT64(kp->kp_proc.p_stats);
560 			kp2p->p_limit = PTRTOUINT64(kp->kp_proc.p_limit);
561 			kp2p->p_vmspace = PTRTOUINT64(kp->kp_proc.p_vmspace);
562 			kp2p->p_sigacts = PTRTOUINT64(kp->kp_proc.p_sigacts);
563 			kp2p->p_sess = PTRTOUINT64(kp->kp_eproc.e_sess);
564 			kp2p->p_tsess = 0;
565 #if 1 /* XXX: dsl - p_ru was only ever non-zero for zombies */
566 			kp2p->p_ru = 0;
567 #else
568 			kp2p->p_ru = PTRTOUINT64(pstats.p_ru);
569 #endif
570 
571 			kp2p->p_eflag = 0;
572 			kp2p->p_exitsig = kp->kp_proc.p_exitsig;
573 			kp2p->p_flag = kp->kp_proc.p_flag;
574 
575 			kp2p->p_pid = kp->kp_proc.p_pid;
576 
577 			kp2p->p_ppid = kp->kp_eproc.e_ppid;
578 			kp2p->p_sid = kp->kp_eproc.e_sid;
579 			kp2p->p__pgid = kp->kp_eproc.e_pgid;
580 
581 			kp2p->p_tpgid = -1 /* XXX NO_PGID! */;
582 
583 			kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid;
584 			kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid;
585 			kp2p->p_svuid = kp->kp_eproc.e_pcred.p_svuid;
586 			kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid;
587 			kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid;
588 			kp2p->p_svgid = kp->kp_eproc.e_pcred.p_svgid;
589 
590 			/*CONSTCOND*/
591 			memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups,
592 			    MIN(sizeof(kp2p->p_groups),
593 			    sizeof(kp->kp_eproc.e_ucred.cr_groups)));
594 			kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups;
595 
596 			kp2p->p_jobc = kp->kp_eproc.e_jobc;
597 			kp2p->p_tdev = kp->kp_eproc.e_tdev;
598 			kp2p->p_tpgid = kp->kp_eproc.e_tpgid;
599 			kp2p->p_tsess = PTRTOUINT64(kp->kp_eproc.e_tsess);
600 
601 			kp2p->p_estcpu = 0;
602 			bintime2timeval(&kp->kp_proc.p_rtime, &tv);
603 			kp2p->p_rtime_sec = (uint32_t)tv.tv_sec;
604 			kp2p->p_rtime_usec = (uint32_t)tv.tv_usec;
605 			kp2p->p_cpticks = kl[0].l_cpticks;
606 			kp2p->p_pctcpu = kp->kp_proc.p_pctcpu;
607 			kp2p->p_swtime = kl[0].l_swtime;
608 			kp2p->p_slptime = kl[0].l_slptime;
609 #if 0 /* XXX thorpej */
610 			kp2p->p_schedflags = kp->kp_proc.p_schedflags;
611 #else
612 			kp2p->p_schedflags = 0;
613 #endif
614 
615 			kp2p->p_uticks = kp->kp_proc.p_uticks;
616 			kp2p->p_sticks = kp->kp_proc.p_sticks;
617 			kp2p->p_iticks = kp->kp_proc.p_iticks;
618 
619 			kp2p->p_tracep = PTRTOUINT64(kp->kp_proc.p_tracep);
620 			kp2p->p_traceflag = kp->kp_proc.p_traceflag;
621 
622 			kp2p->p_holdcnt = kl[0].l_holdcnt;
623 
624 			memcpy(&kp2p->p_siglist,
625 			    &kp->kp_proc.p_sigpend.sp_set,
626 			    sizeof(ki_sigset_t));
627 			memset(&kp2p->p_sigmask, 0,
628 			    sizeof(ki_sigset_t));
629 			memcpy(&kp2p->p_sigignore,
630 			    &kp->kp_proc.p_sigctx.ps_sigignore,
631 			    sizeof(ki_sigset_t));
632 			memcpy(&kp2p->p_sigcatch,
633 			    &kp->kp_proc.p_sigctx.ps_sigcatch,
634 			    sizeof(ki_sigset_t));
635 
636 			kp2p->p_stat = kl[0].l_stat;
637 			kp2p->p_priority = kl[0].l_priority;
638 			kp2p->p_usrpri = kl[0].l_priority;
639 			kp2p->p_nice = kp->kp_proc.p_nice;
640 
641 			kp2p->p_xstat = kp->kp_proc.p_xstat;
642 			kp2p->p_acflag = kp->kp_proc.p_acflag;
643 
644 			/*CONSTCOND*/
645 			strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
646 			    MIN(sizeof(kp2p->p_comm),
647 			    sizeof(kp->kp_proc.p_comm)));
648 
649 			strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg,
650 			    sizeof(kp2p->p_wmesg));
651 			kp2p->p_wchan = kl[0].l_wchan;
652 			strncpy(kp2p->p_login, kp->kp_eproc.e_login,
653 			    sizeof(kp2p->p_login));
654 
655 			kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
656 			kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
657 			kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
658 			kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
659 			kp2p->p_vm_vsize = kp->kp_eproc.e_vm.vm_map.size
660 			    / kd->nbpg;
661 			/* Adjust mapped size */
662 			kp2p->p_vm_msize =
663 			    (kp->kp_eproc.e_vm.vm_map.size / kd->nbpg) -
664 			    kp->kp_eproc.e_vm.vm_issize +
665 			    kp->kp_eproc.e_vm.vm_ssize;
666 
667 			kp2p->p_eflag = (int32_t)kp->kp_eproc.e_flag;
668 
669 			kp2p->p_realflag = kp->kp_proc.p_flag;
670 			kp2p->p_nlwps = kp->kp_proc.p_nlwps;
671 			kp2p->p_nrlwps = kp->kp_proc.p_nrlwps;
672 			kp2p->p_realstat = kp->kp_proc.p_stat;
673 
674 			if (P_ZOMBIE(&kp->kp_proc) ||
675 			    kp->kp_proc.p_stats == NULL ||
676 			    KREAD(kd, (u_long)kp->kp_proc.p_stats, &pstats)) {
677 				kp2p->p_uvalid = 0;
678 			} else {
679 				kp2p->p_uvalid = 1;
680 
681 				kp2p->p_ustart_sec = (u_int32_t)
682 				    pstats.p_start.tv_sec;
683 				kp2p->p_ustart_usec = (u_int32_t)
684 				    pstats.p_start.tv_usec;
685 
686 				kp2p->p_uutime_sec = (u_int32_t)
687 				    pstats.p_ru.ru_utime.tv_sec;
688 				kp2p->p_uutime_usec = (u_int32_t)
689 				    pstats.p_ru.ru_utime.tv_usec;
690 				kp2p->p_ustime_sec = (u_int32_t)
691 				    pstats.p_ru.ru_stime.tv_sec;
692 				kp2p->p_ustime_usec = (u_int32_t)
693 				    pstats.p_ru.ru_stime.tv_usec;
694 
695 				kp2p->p_uru_maxrss = pstats.p_ru.ru_maxrss;
696 				kp2p->p_uru_ixrss = pstats.p_ru.ru_ixrss;
697 				kp2p->p_uru_idrss = pstats.p_ru.ru_idrss;
698 				kp2p->p_uru_isrss = pstats.p_ru.ru_isrss;
699 				kp2p->p_uru_minflt = pstats.p_ru.ru_minflt;
700 				kp2p->p_uru_majflt = pstats.p_ru.ru_majflt;
701 				kp2p->p_uru_nswap = pstats.p_ru.ru_nswap;
702 				kp2p->p_uru_inblock = pstats.p_ru.ru_inblock;
703 				kp2p->p_uru_oublock = pstats.p_ru.ru_oublock;
704 				kp2p->p_uru_msgsnd = pstats.p_ru.ru_msgsnd;
705 				kp2p->p_uru_msgrcv = pstats.p_ru.ru_msgrcv;
706 				kp2p->p_uru_nsignals = pstats.p_ru.ru_nsignals;
707 				kp2p->p_uru_nvcsw = pstats.p_ru.ru_nvcsw;
708 				kp2p->p_uru_nivcsw = pstats.p_ru.ru_nivcsw;
709 
710 				kp2p->p_uctime_sec = (u_int32_t)
711 				    (pstats.p_cru.ru_utime.tv_sec +
712 				    pstats.p_cru.ru_stime.tv_sec);
713 				kp2p->p_uctime_usec = (u_int32_t)
714 				    (pstats.p_cru.ru_utime.tv_usec +
715 				    pstats.p_cru.ru_stime.tv_usec);
716 			}
717 
718 			memcpy(kp2c, &kp2, esize);
719 			kp2c += esize;
720 		}
721 	}
722 	*cnt = nprocs;
723 	return (kd->procbase2);
724 }
725 
726 struct kinfo_lwp *
kvm_getlwps(kvm_t * kd,int pid,u_long paddr,size_t esize,int * cnt)727 kvm_getlwps(kvm_t *kd, int pid, u_long paddr, size_t esize, int *cnt)
728 {
729 	size_t size;
730 	int mib[5], nlwps;
731 	ssize_t st;
732 	struct kinfo_lwp *kl;
733 
734 	if (ISSYSCTL(kd)) {
735 		size = 0;
736 		mib[0] = CTL_KERN;
737 		mib[1] = KERN_LWP;
738 		mib[2] = pid;
739 		mib[3] = (int)esize;
740 		mib[4] = 0;
741 again:
742 		st = sysctl(mib, 5, NULL, &size, NULL, (size_t)0);
743 		if (st == -1) {
744 			switch (errno) {
745 			case ESRCH: /* Treat this as a soft error; see kvm.c */
746 				_kvm_syserr(kd, NULL, "kvm_getlwps");
747 				return NULL;
748 			default:
749 				_kvm_syserr(kd, kd->program, "kvm_getlwps");
750 				return NULL;
751 			}
752 		}
753 		mib[4] = (int) (size / esize);
754 		KVM_ALLOC(kd, lwpbase, size);
755 		st = sysctl(mib, 5, kd->lwpbase, &size, NULL, (size_t)0);
756 		if (st == -1) {
757 			switch (errno) {
758 			case ESRCH: /* Treat this as a soft error; see kvm.c */
759 				_kvm_syserr(kd, NULL, "kvm_getlwps");
760 				return NULL;
761 			case ENOMEM:
762 				goto again;
763 			default:
764 				_kvm_syserr(kd, kd->program, "kvm_getlwps");
765 				return NULL;
766 			}
767 		}
768 		nlwps = (int) (size / esize);
769 	} else {
770 		/* grovel through the memory image */
771 		struct proc p;
772 		struct lwp l;
773 		u_long laddr;
774 		void *back;
775 		int i;
776 
777 		st = kvm_read(kd, paddr, &p, sizeof(p));
778 		if (st == -1) {
779 			_kvm_syserr(kd, kd->program, "kvm_getlwps");
780 			return (NULL);
781 		}
782 
783 		nlwps = p.p_nlwps;
784 		size = nlwps * sizeof(*kd->lwpbase);
785 		KVM_ALLOC(kd, lwpbase, size);
786 		laddr = (u_long)PTRTOUINT64(p.p_lwps.lh_first);
787 		for (i = 0; (i < nlwps) && (laddr != 0); i++) {
788 			st = kvm_read(kd, laddr, &l, sizeof(l));
789 			if (st == -1) {
790 				_kvm_syserr(kd, kd->program, "kvm_getlwps");
791 				return (NULL);
792 			}
793 			kl = &kd->lwpbase[i];
794 			kl->l_laddr = laddr;
795 			kl->l_forw = PTRTOUINT64(l.l_runq.tqe_next);
796 			laddr = (u_long)PTRTOUINT64(l.l_runq.tqe_prev);
797 			st = kvm_read(kd, laddr, &back, sizeof(back));
798 			if (st == -1) {
799 				_kvm_syserr(kd, kd->program, "kvm_getlwps");
800 				return (NULL);
801 			}
802 			kl->l_back = PTRTOUINT64(back);
803 			kl->l_addr = PTRTOUINT64(l.l_addr);
804 			kl->l_lid = l.l_lid;
805 			kl->l_flag = l.l_flag;
806 			kl->l_swtime = l.l_swtime;
807 			kl->l_slptime = l.l_slptime;
808 			kl->l_schedflags = 0; /* XXX */
809 			kl->l_holdcnt = 0;
810 			kl->l_priority = l.l_priority;
811 			kl->l_usrpri = l.l_priority;
812 			kl->l_stat = l.l_stat;
813 			kl->l_wchan = PTRTOUINT64(l.l_wchan);
814 			if (l.l_wmesg)
815 				(void)kvm_read(kd, (u_long)l.l_wmesg,
816 				    kl->l_wmesg, (size_t)WMESGLEN);
817 			kl->l_cpuid = KI_NOCPU;
818 			laddr = (u_long)PTRTOUINT64(l.l_sibling.le_next);
819 		}
820 	}
821 
822 	*cnt = nlwps;
823 	return (kd->lwpbase);
824 }
825 
826 struct kinfo_proc *
kvm_getprocs(kvm_t * kd,int op,int arg,int * cnt)827 kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
828 {
829 	size_t size;
830 	int mib[4], st, nprocs;
831 
832 	if (ISALIVE(kd)) {
833 		size = 0;
834 		mib[0] = CTL_KERN;
835 		mib[1] = KERN_PROC;
836 		mib[2] = op;
837 		mib[3] = arg;
838 		st = sysctl(mib, 4, NULL, &size, NULL, (size_t)0);
839 		if (st == -1) {
840 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
841 			return (NULL);
842 		}
843 		KVM_ALLOC(kd, procbase, size);
844 		st = sysctl(mib, 4, kd->procbase, &size, NULL, (size_t)0);
845 		if (st == -1) {
846 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
847 			return (NULL);
848 		}
849 		if (size % sizeof(struct kinfo_proc) != 0) {
850 			_kvm_err(kd, kd->program,
851 			    "proc size mismatch (%lu total, %lu chunks)",
852 			    (u_long)size, (u_long)sizeof(struct kinfo_proc));
853 			return (NULL);
854 		}
855 		nprocs = (int) (size / sizeof(struct kinfo_proc));
856 	} else {
857 		struct nlist nl[4], *p;
858 
859 		(void)memset(nl, 0, sizeof(nl));
860 		nl[0].n_name = "_nprocs";
861 		nl[1].n_name = "_allproc";
862 		nl[2].n_name = "_zombproc";
863 		nl[3].n_name = NULL;
864 
865 		if (kvm_nlist(kd, nl) != 0) {
866 			for (p = nl; p->n_type != 0; ++p)
867 				continue;
868 			_kvm_err(kd, kd->program,
869 			    "%s: no such symbol", p->n_name);
870 			return (NULL);
871 		}
872 		if (KREAD(kd, nl[0].n_value, &nprocs)) {
873 			_kvm_err(kd, kd->program, "can't read nprocs");
874 			return (NULL);
875 		}
876 		size = nprocs * sizeof(*kd->procbase);
877 		KVM_ALLOC(kd, procbase, size);
878 		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
879 		    nl[2].n_value, nprocs);
880 		if (nprocs < 0)
881 			return (NULL);
882 #ifdef notdef
883 		size = nprocs * sizeof(struct kinfo_proc);
884 		(void)realloc(kd->procbase, size);
885 #endif
886 	}
887 	*cnt = nprocs;
888 	return (kd->procbase);
889 }
890 
891 void *
_kvm_realloc(kvm_t * kd,void * p,size_t n)892 _kvm_realloc(kvm_t *kd, void *p, size_t n)
893 {
894 	void *np = realloc(p, n);
895 
896 	if (np == NULL)
897 		_kvm_err(kd, kd->program, "out of memory");
898 	return (np);
899 }
900 
901 /*
902  * Read in an argument vector from the user address space of process p.
903  * addr if the user-space base address of narg null-terminated contiguous
904  * strings.  This is used to read in both the command arguments and
905  * environment strings.  Read at most maxcnt characters of strings.
906  */
907 static char **
kvm_argv(kvm_t * kd,const struct miniproc * p,u_long addr,int narg,int maxcnt)908 kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg,
909 	 int maxcnt)
910 {
911 	char *np, *cp, *ep, *ap;
912 	u_long oaddr = (u_long)~0L;
913 	u_long len;
914 	size_t cc;
915 	char **argv;
916 
917 	/*
918 	 * Check that there aren't an unreasonable number of arguments,
919 	 * and that the address is in user space.
920 	 */
921 	if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
922 		return (NULL);
923 
924 	if (kd->argv == NULL) {
925 		/*
926 		 * Try to avoid reallocs.
927 		 */
928 		kd->argc = MAX(narg + 1, 32);
929 		kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
930 		if (kd->argv == NULL)
931 			return (NULL);
932 	} else if (narg + 1 > kd->argc) {
933 		kd->argc = MAX(2 * kd->argc, narg + 1);
934 		kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
935 		    sizeof(*kd->argv));
936 		if (kd->argv == NULL)
937 			return (NULL);
938 	}
939 	if (kd->argspc == NULL) {
940 		kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg);
941 		if (kd->argspc == NULL)
942 			return (NULL);
943 		kd->argspc_len = kd->nbpg;
944 	}
945 	if (kd->argbuf == NULL) {
946 		kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg);
947 		if (kd->argbuf == NULL)
948 			return (NULL);
949 	}
950 	cc = sizeof(char *) * narg;
951 	if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc)
952 		return (NULL);
953 	ap = np = kd->argspc;
954 	argv = kd->argv;
955 	len = 0;
956 	/*
957 	 * Loop over pages, filling in the argument vector.
958 	 */
959 	while (argv < kd->argv + narg && *argv != NULL) {
960 		addr = (u_long)*argv & ~(kd->nbpg - 1);
961 		if (addr != oaddr) {
962 			if (kvm_ureadm(kd, p, addr, kd->argbuf,
963 			    (size_t)kd->nbpg) != kd->nbpg)
964 				return (NULL);
965 			oaddr = addr;
966 		}
967 		addr = (u_long)*argv & (kd->nbpg - 1);
968 		cp = kd->argbuf + (size_t)addr;
969 		cc = kd->nbpg - (size_t)addr;
970 		if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
971 			cc = (size_t)(maxcnt - len);
972 		ep = memchr(cp, '\0', cc);
973 		if (ep != NULL)
974 			cc = ep - cp + 1;
975 		if (len + cc > kd->argspc_len) {
976 			ptrdiff_t off;
977 			char **pp;
978 			char *op = kd->argspc;
979 
980 			kd->argspc_len *= 2;
981 			kd->argspc = _kvm_realloc(kd, kd->argspc,
982 			    kd->argspc_len);
983 			if (kd->argspc == NULL)
984 				return (NULL);
985 			/*
986 			 * Adjust argv pointers in case realloc moved
987 			 * the string space.
988 			 */
989 			off = kd->argspc - op;
990 			for (pp = kd->argv; pp < argv; pp++)
991 				*pp += off;
992 			ap += off;
993 			np += off;
994 		}
995 		memcpy(np, cp, cc);
996 		np += cc;
997 		len += cc;
998 		if (ep != NULL) {
999 			*argv++ = ap;
1000 			ap = np;
1001 		} else
1002 			*argv += cc;
1003 		if (maxcnt > 0 && len >= maxcnt) {
1004 			/*
1005 			 * We're stopping prematurely.  Terminate the
1006 			 * current string.
1007 			 */
1008 			if (ep == NULL) {
1009 				*np = '\0';
1010 				*argv++ = ap;
1011 			}
1012 			break;
1013 		}
1014 	}
1015 	/* Make sure argv is terminated. */
1016 	*argv = NULL;
1017 	return (kd->argv);
1018 }
1019 
1020 static void
ps_str_a(struct ps_strings * p,u_long * addr,int * n)1021 ps_str_a(struct ps_strings *p, u_long *addr, int *n)
1022 {
1023 
1024 	*addr = (u_long)p->ps_argvstr;
1025 	*n = p->ps_nargvstr;
1026 }
1027 
1028 static void
ps_str_e(struct ps_strings * p,u_long * addr,int * n)1029 ps_str_e(struct ps_strings *p, u_long *addr, int *n)
1030 {
1031 
1032 	*addr = (u_long)p->ps_envstr;
1033 	*n = p->ps_nenvstr;
1034 }
1035 
1036 /*
1037  * Determine if the proc indicated by p is still active.
1038  * This test is not 100% foolproof in theory, but chances of
1039  * being wrong are very low.
1040  */
1041 static int
proc_verify(kvm_t * kd,u_long kernp,const struct miniproc * p)1042 proc_verify(kvm_t *kd, u_long kernp, const struct miniproc *p)
1043 {
1044 	struct proc kernproc;
1045 
1046 	/*
1047 	 * Just read in the whole proc.  It's not that big relative
1048 	 * to the cost of the read system call.
1049 	 */
1050 	if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
1051 	    sizeof(kernproc))
1052 		return (0);
1053 	return (p->p_pid == kernproc.p_pid &&
1054 	    (kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
1055 }
1056 
1057 static char **
kvm_doargv(kvm_t * kd,const struct miniproc * p,int nchr,void (* info)(struct ps_strings *,u_long *,int *))1058 kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr,
1059 	   void (*info)(struct ps_strings *, u_long *, int *))
1060 {
1061 	char **ap;
1062 	u_long addr;
1063 	int cnt;
1064 	struct ps_strings arginfo;
1065 
1066 	/*
1067 	 * Pointers are stored at the top of the user stack.
1068 	 */
1069 	if (p->p_stat == SZOMB)
1070 		return (NULL);
1071 	cnt = (int)kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo),
1072 	    (void *)&arginfo, sizeof(arginfo));
1073 	if (cnt != sizeof(arginfo))
1074 		return (NULL);
1075 
1076 	(*info)(&arginfo, &addr, &cnt);
1077 	if (cnt == 0)
1078 		return (NULL);
1079 	ap = kvm_argv(kd, p, addr, cnt, nchr);
1080 	/*
1081 	 * For live kernels, make sure this process didn't go away.
1082 	 */
1083 	if (ap != NULL && ISALIVE(kd) &&
1084 	    !proc_verify(kd, (u_long)p->p_paddr, p))
1085 		ap = NULL;
1086 	return (ap);
1087 }
1088 
1089 /*
1090  * Get the command args.  This code is now machine independent.
1091  */
1092 char **
kvm_getargv(kvm_t * kd,const struct kinfo_proc * kp,int nchr)1093 kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
1094 {
1095 	struct miniproc p;
1096 
1097 	KPTOMINI(kp, &p);
1098 	return (kvm_doargv(kd, &p, nchr, ps_str_a));
1099 }
1100 
1101 char **
kvm_getenvv(kvm_t * kd,const struct kinfo_proc * kp,int nchr)1102 kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
1103 {
1104 	struct miniproc p;
1105 
1106 	KPTOMINI(kp, &p);
1107 	return (kvm_doargv(kd, &p, nchr, ps_str_e));
1108 }
1109 
1110 static char **
kvm_doargv2(kvm_t * kd,pid_t pid,int type,int nchr)1111 kvm_doargv2(kvm_t *kd, pid_t pid, int type, int nchr)
1112 {
1113 	size_t bufs;
1114 	int narg, mib[4];
1115 	size_t newargspc_len;
1116 	char **ap, *bp, *endp;
1117 
1118 	/*
1119 	 * Check that there aren't an unreasonable number of arguments.
1120 	 */
1121 	if (nchr > ARG_MAX)
1122 		return (NULL);
1123 
1124 	if (nchr == 0)
1125 		nchr = ARG_MAX;
1126 
1127 	/* Get number of strings in argv */
1128 	mib[0] = CTL_KERN;
1129 	mib[1] = KERN_PROC_ARGS;
1130 	mib[2] = pid;
1131 	mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV;
1132 	bufs = sizeof(narg);
1133 	if (sysctl(mib, 4, &narg, &bufs, NULL, (size_t)0) == -1)
1134 		return (NULL);
1135 
1136 	if (kd->argv == NULL) {
1137 		/*
1138 		 * Try to avoid reallocs.
1139 		 */
1140 		kd->argc = MAX(narg + 1, 32);
1141 		kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
1142 		if (kd->argv == NULL)
1143 			return (NULL);
1144 	} else if (narg + 1 > kd->argc) {
1145 		kd->argc = MAX(2 * kd->argc, narg + 1);
1146 		kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
1147 		    sizeof(*kd->argv));
1148 		if (kd->argv == NULL)
1149 			return (NULL);
1150 	}
1151 
1152 	newargspc_len = MIN(nchr, ARG_MAX);
1153 	KVM_ALLOC(kd, argspc, newargspc_len);
1154 	memset(kd->argspc, 0, (size_t)kd->argspc_len);	/* XXX necessary? */
1155 
1156 	mib[0] = CTL_KERN;
1157 	mib[1] = KERN_PROC_ARGS;
1158 	mib[2] = pid;
1159 	mib[3] = type;
1160 	bufs = kd->argspc_len;
1161 	if (sysctl(mib, 4, kd->argspc, &bufs, NULL, (size_t)0) == -1)
1162 		return (NULL);
1163 
1164 	bp = kd->argspc;
1165 	bp[kd->argspc_len-1] = '\0';	/* make sure the string ends with nul */
1166 	ap = kd->argv;
1167 	endp = bp + MIN(nchr, bufs);
1168 
1169 	while (bp < endp) {
1170 		*ap++ = bp;
1171 		/*
1172 		 * XXX: don't need following anymore, or stick check
1173 		 * for max argc in above while loop?
1174 		 */
1175 		if (ap >= kd->argv + kd->argc) {
1176 			kd->argc *= 2;
1177 			kd->argv = _kvm_realloc(kd, kd->argv,
1178 			    kd->argc * sizeof(*kd->argv));
1179 			ap = kd->argv;
1180 		}
1181 		bp += strlen(bp) + 1;
1182 	}
1183 	*ap = NULL;
1184 
1185 	return (kd->argv);
1186 }
1187 
1188 char **
kvm_getargv2(kvm_t * kd,const struct kinfo_proc2 * kp,int nchr)1189 kvm_getargv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
1190 {
1191 
1192 	return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr));
1193 }
1194 
1195 char **
kvm_getenvv2(kvm_t * kd,const struct kinfo_proc2 * kp,int nchr)1196 kvm_getenvv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
1197 {
1198 
1199 	return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr));
1200 }
1201 
1202 /*
1203  * Read from user space.  The user context is given by p.
1204  */
1205 static ssize_t
kvm_ureadm(kvm_t * kd,const struct miniproc * p,u_long uva,char * buf,size_t len)1206 kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva,
1207 	   char *buf, size_t len)
1208 {
1209 	char *cp;
1210 
1211 	cp = buf;
1212 	while (len > 0) {
1213 		size_t cc;
1214 		char *dp;
1215 		u_long cnt;
1216 
1217 		dp = _kvm_ureadm(kd, p, uva, &cnt);
1218 		if (dp == NULL) {
1219 			_kvm_err(kd, 0, "invalid address (%lx)", uva);
1220 			return (0);
1221 		}
1222 		cc = (size_t)MIN(cnt, len);
1223 		memcpy(cp, dp, cc);
1224 		cp += cc;
1225 		uva += cc;
1226 		len -= cc;
1227 	}
1228 	return (ssize_t)(cp - buf);
1229 }
1230 
1231 ssize_t
kvm_uread(kvm_t * kd,const struct proc * p,u_long uva,char * buf,size_t len)1232 kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf, size_t len)
1233 {
1234 	struct miniproc mp;
1235 
1236 	PTOMINI(p, &mp);
1237 	return (kvm_ureadm(kd, &mp, uva, buf, len));
1238 }
1239