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