kvm_proc.c revision 217745
1/*-
2 * Copyright (c) 1989, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software developed by the Computer Systems
6 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7 * BG 91-66 and contributed to Berkeley.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#if defined(LIBC_SCCS) && !defined(lint)
36static char sccsid[] = "@(#)kvm_proc.c	8.3 (Berkeley) 9/23/93";
37#endif /* LIBC_SCCS and not lint */
38#endif
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libkvm/kvm_proc.c 217745 2011-01-23 11:08:36Z uqs $");
42
43/*
44 * Proc traversal interface for kvm.  ps and w are (probably) the exclusive
45 * users of this code, so we've factored it out into a separate module.
46 * Thus, we keep this grunge out of the other kvm applications (i.e.,
47 * most other applications are interested only in open/close/read/nlist).
48 */
49
50#include <sys/param.h>
51#define	_WANT_UCRED	/* make ucred.h give us 'struct ucred' */
52#include <sys/ucred.h>
53#include <sys/queue.h>
54#include <sys/_lock.h>
55#include <sys/_mutex.h>
56#include <sys/_task.h>
57#include <sys/cpuset.h>
58#include <sys/user.h>
59#include <sys/proc.h>
60#define	_WANT_PRISON	/* make jail.h give us 'struct prison' */
61#include <sys/jail.h>
62#include <sys/exec.h>
63#include <sys/stat.h>
64#include <sys/sysent.h>
65#include <sys/ioctl.h>
66#include <sys/tty.h>
67#include <sys/file.h>
68#include <sys/conf.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <unistd.h>
72#include <nlist.h>
73#include <kvm.h>
74
75#include <vm/vm.h>
76#include <vm/vm_param.h>
77
78#include <sys/sysctl.h>
79
80#include <limits.h>
81#include <memory.h>
82#include <paths.h>
83
84#include "kvm_private.h"
85
86#define KREAD(kd, addr, obj) \
87	(kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
88
89static int ticks;
90static int hz;
91static uint64_t cpu_tick_frequency;
92
93/*
94 * From sys/kern/kern_tc.c. Depends on cpu_tick_frequency, which is
95 * read/initialized before this function is ever called.
96 */
97static uint64_t
98cputick2usec(uint64_t tick)
99{
100
101	if (cpu_tick_frequency == 0)
102		return (0);
103	if (tick > 18446744073709551)		/* floor(2^64 / 1000) */
104		return (tick / (cpu_tick_frequency / 1000000));
105	else if (tick > 18446744073709)	/* floor(2^64 / 1000000) */
106		return ((tick * 1000) / (cpu_tick_frequency / 1000));
107	else
108		return ((tick * 1000000) / cpu_tick_frequency);
109}
110
111/*
112 * Read proc's from memory file into buffer bp, which has space to hold
113 * at most maxcnt procs.
114 */
115static int
116kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
117    struct kinfo_proc *bp, int maxcnt)
118{
119	int cnt = 0;
120	struct kinfo_proc kinfo_proc, *kp;
121	struct pgrp pgrp;
122	struct session sess;
123	struct cdev t_cdev;
124	struct tty tty;
125	struct vmspace vmspace;
126	struct sigacts sigacts;
127#if 0
128	struct pstats pstats;
129#endif
130	struct ucred ucred;
131	struct prison pr;
132	struct thread mtd;
133	struct proc proc;
134	struct proc pproc;
135	struct sysentvec sysent;
136	char svname[KI_EMULNAMELEN];
137
138	kp = &kinfo_proc;
139	kp->ki_structsize = sizeof(kinfo_proc);
140	/*
141	 * Loop on the processes. this is completely broken because we need to be
142	 * able to loop on the threads and merge the ones that are the same process some how.
143	 */
144	for (; cnt < maxcnt && p != NULL; p = LIST_NEXT(&proc, p_list)) {
145		memset(kp, 0, sizeof *kp);
146		if (KREAD(kd, (u_long)p, &proc)) {
147			_kvm_err(kd, kd->program, "can't read proc at %p", p);
148			return (-1);
149		}
150		if (proc.p_state != PRS_ZOMBIE) {
151			if (KREAD(kd, (u_long)TAILQ_FIRST(&proc.p_threads),
152			    &mtd)) {
153				_kvm_err(kd, kd->program,
154				    "can't read thread at %p",
155				    TAILQ_FIRST(&proc.p_threads));
156				return (-1);
157			}
158		}
159		if (KREAD(kd, (u_long)proc.p_ucred, &ucred) == 0) {
160			kp->ki_ruid = ucred.cr_ruid;
161			kp->ki_svuid = ucred.cr_svuid;
162			kp->ki_rgid = ucred.cr_rgid;
163			kp->ki_svgid = ucred.cr_svgid;
164			kp->ki_cr_flags = ucred.cr_flags;
165			if (ucred.cr_ngroups > KI_NGROUPS) {
166				kp->ki_ngroups = KI_NGROUPS;
167				kp->ki_cr_flags |= KI_CRF_GRP_OVERFLOW;
168			} else
169				kp->ki_ngroups = ucred.cr_ngroups;
170			kvm_read(kd, (u_long)ucred.cr_groups, kp->ki_groups,
171			    kp->ki_ngroups * sizeof(gid_t));
172			kp->ki_uid = ucred.cr_uid;
173			if (ucred.cr_prison != NULL) {
174				if (KREAD(kd, (u_long)ucred.cr_prison, &pr)) {
175					_kvm_err(kd, kd->program,
176					    "can't read prison at %p",
177					    ucred.cr_prison);
178					return (-1);
179				}
180				kp->ki_jid = pr.pr_id;
181			}
182		}
183
184		switch(what & ~KERN_PROC_INC_THREAD) {
185
186		case KERN_PROC_GID:
187			if (kp->ki_groups[0] != (gid_t)arg)
188				continue;
189			break;
190
191		case KERN_PROC_PID:
192			if (proc.p_pid != (pid_t)arg)
193				continue;
194			break;
195
196		case KERN_PROC_RGID:
197			if (kp->ki_rgid != (gid_t)arg)
198				continue;
199			break;
200
201		case KERN_PROC_UID:
202			if (kp->ki_uid != (uid_t)arg)
203				continue;
204			break;
205
206		case KERN_PROC_RUID:
207			if (kp->ki_ruid != (uid_t)arg)
208				continue;
209			break;
210		}
211		/*
212		 * We're going to add another proc to the set.  If this
213		 * will overflow the buffer, assume the reason is because
214		 * nprocs (or the proc list) is corrupt and declare an error.
215		 */
216		if (cnt >= maxcnt) {
217			_kvm_err(kd, kd->program, "nprocs corrupt");
218			return (-1);
219		}
220		/*
221		 * gather kinfo_proc
222		 */
223		kp->ki_paddr = p;
224		kp->ki_addr = 0;	/* XXX uarea */
225		/* kp->ki_kstack = proc.p_thread.td_kstack; XXXKSE */
226		kp->ki_args = proc.p_args;
227		kp->ki_tracep = proc.p_tracevp;
228		kp->ki_textvp = proc.p_textvp;
229		kp->ki_fd = proc.p_fd;
230		kp->ki_vmspace = proc.p_vmspace;
231		if (proc.p_sigacts != NULL) {
232			if (KREAD(kd, (u_long)proc.p_sigacts, &sigacts)) {
233				_kvm_err(kd, kd->program,
234				    "can't read sigacts at %p", proc.p_sigacts);
235				return (-1);
236			}
237			kp->ki_sigignore = sigacts.ps_sigignore;
238			kp->ki_sigcatch = sigacts.ps_sigcatch;
239		}
240#if 0
241		if ((proc.p_flag & P_INMEM) && proc.p_stats != NULL) {
242			if (KREAD(kd, (u_long)proc.p_stats, &pstats)) {
243				_kvm_err(kd, kd->program,
244				    "can't read stats at %x", proc.p_stats);
245				return (-1);
246			}
247			kp->ki_start = pstats.p_start;
248
249			/*
250			 * XXX: The times here are probably zero and need
251			 * to be calculated from the raw data in p_rux and
252			 * p_crux.
253			 */
254			kp->ki_rusage = pstats.p_ru;
255			kp->ki_childstime = pstats.p_cru.ru_stime;
256			kp->ki_childutime = pstats.p_cru.ru_utime;
257			/* Some callers want child-times in a single value */
258			timeradd(&kp->ki_childstime, &kp->ki_childutime,
259			    &kp->ki_childtime);
260		}
261#endif
262		if (proc.p_oppid)
263			kp->ki_ppid = proc.p_oppid;
264		else if (proc.p_pptr) {
265			if (KREAD(kd, (u_long)proc.p_pptr, &pproc)) {
266				_kvm_err(kd, kd->program,
267				    "can't read pproc at %p", proc.p_pptr);
268				return (-1);
269			}
270			kp->ki_ppid = pproc.p_pid;
271		} else
272			kp->ki_ppid = 0;
273		if (proc.p_pgrp == NULL)
274			goto nopgrp;
275		if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
276			_kvm_err(kd, kd->program, "can't read pgrp at %p",
277				 proc.p_pgrp);
278			return (-1);
279		}
280		kp->ki_pgid = pgrp.pg_id;
281		kp->ki_jobc = pgrp.pg_jobc;
282		if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
283			_kvm_err(kd, kd->program, "can't read session at %p",
284				pgrp.pg_session);
285			return (-1);
286		}
287		kp->ki_sid = sess.s_sid;
288		(void)memcpy(kp->ki_login, sess.s_login,
289						sizeof(kp->ki_login));
290		kp->ki_kiflag = sess.s_ttyvp ? KI_CTTY : 0;
291		if (sess.s_leader == p)
292			kp->ki_kiflag |= KI_SLEADER;
293		if ((proc.p_flag & P_CONTROLT) && sess.s_ttyp != NULL) {
294			if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
295				_kvm_err(kd, kd->program,
296					 "can't read tty at %p", sess.s_ttyp);
297				return (-1);
298			}
299			if (tty.t_dev != NULL) {
300				if (KREAD(kd, (u_long)tty.t_dev, &t_cdev)) {
301					_kvm_err(kd, kd->program,
302						 "can't read cdev at %p",
303						tty.t_dev);
304					return (-1);
305				}
306#if 0
307				kp->ki_tdev = t_cdev.si_udev;
308#else
309				kp->ki_tdev = NODEV;
310#endif
311			}
312			if (tty.t_pgrp != NULL) {
313				if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
314					_kvm_err(kd, kd->program,
315						 "can't read tpgrp at %p",
316						tty.t_pgrp);
317					return (-1);
318				}
319				kp->ki_tpgid = pgrp.pg_id;
320			} else
321				kp->ki_tpgid = -1;
322			if (tty.t_session != NULL) {
323				if (KREAD(kd, (u_long)tty.t_session, &sess)) {
324					_kvm_err(kd, kd->program,
325					    "can't read session at %p",
326					    tty.t_session);
327					return (-1);
328				}
329				kp->ki_tsid = sess.s_sid;
330			}
331		} else {
332nopgrp:
333			kp->ki_tdev = NODEV;
334		}
335		if ((proc.p_state != PRS_ZOMBIE) && mtd.td_wmesg)
336			(void)kvm_read(kd, (u_long)mtd.td_wmesg,
337			    kp->ki_wmesg, WMESGLEN);
338
339		(void)kvm_read(kd, (u_long)proc.p_vmspace,
340		    (char *)&vmspace, sizeof(vmspace));
341		kp->ki_size = vmspace.vm_map.size;
342		/*
343		 * Approximate the kernel's method of calculating
344		 * this field.
345		 */
346#define		pmap_resident_count(pm) ((pm)->pm_stats.resident_count)
347		kp->ki_rssize = pmap_resident_count(&vmspace.vm_pmap);
348		kp->ki_swrss = vmspace.vm_swrss;
349		kp->ki_tsize = vmspace.vm_tsize;
350		kp->ki_dsize = vmspace.vm_dsize;
351		kp->ki_ssize = vmspace.vm_ssize;
352
353		switch (what & ~KERN_PROC_INC_THREAD) {
354
355		case KERN_PROC_PGRP:
356			if (kp->ki_pgid != (pid_t)arg)
357				continue;
358			break;
359
360		case KERN_PROC_SESSION:
361			if (kp->ki_sid != (pid_t)arg)
362				continue;
363			break;
364
365		case KERN_PROC_TTY:
366			if ((proc.p_flag & P_CONTROLT) == 0 ||
367			     kp->ki_tdev != (dev_t)arg)
368				continue;
369			break;
370		}
371		if (proc.p_comm[0] != 0)
372			strlcpy(kp->ki_comm, proc.p_comm, MAXCOMLEN);
373		(void)kvm_read(kd, (u_long)proc.p_sysent, (char *)&sysent,
374		    sizeof(sysent));
375		(void)kvm_read(kd, (u_long)sysent.sv_name, (char *)&svname,
376		    sizeof(svname));
377		if (svname[0] != 0)
378			strlcpy(kp->ki_emul, svname, KI_EMULNAMELEN);
379		if ((proc.p_state != PRS_ZOMBIE) &&
380		    (mtd.td_blocked != 0)) {
381			kp->ki_kiflag |= KI_LOCKBLOCK;
382			if (mtd.td_lockname)
383				(void)kvm_read(kd,
384				    (u_long)mtd.td_lockname,
385				    kp->ki_lockname, LOCKNAMELEN);
386			kp->ki_lockname[LOCKNAMELEN] = 0;
387		}
388		kp->ki_runtime = cputick2usec(proc.p_rux.rux_runtime);
389		kp->ki_pid = proc.p_pid;
390		kp->ki_siglist = proc.p_siglist;
391		SIGSETOR(kp->ki_siglist, mtd.td_siglist);
392		kp->ki_sigmask = mtd.td_sigmask;
393		kp->ki_xstat = proc.p_xstat;
394		kp->ki_acflag = proc.p_acflag;
395		kp->ki_lock = proc.p_lock;
396		if (proc.p_state != PRS_ZOMBIE) {
397			kp->ki_swtime = (ticks - proc.p_swtick) / hz;
398			kp->ki_flag = proc.p_flag;
399			kp->ki_sflag = 0;
400			kp->ki_nice = proc.p_nice;
401			kp->ki_traceflag = proc.p_traceflag;
402			if (proc.p_state == PRS_NORMAL) {
403				if (TD_ON_RUNQ(&mtd) ||
404				    TD_CAN_RUN(&mtd) ||
405				    TD_IS_RUNNING(&mtd)) {
406					kp->ki_stat = SRUN;
407				} else if (mtd.td_state ==
408				    TDS_INHIBITED) {
409					if (P_SHOULDSTOP(&proc)) {
410						kp->ki_stat = SSTOP;
411					} else if (
412					    TD_IS_SLEEPING(&mtd)) {
413						kp->ki_stat = SSLEEP;
414					} else if (TD_ON_LOCK(&mtd)) {
415						kp->ki_stat = SLOCK;
416					} else {
417						kp->ki_stat = SWAIT;
418					}
419				}
420			} else {
421				kp->ki_stat = SIDL;
422			}
423			/* Stuff from the thread */
424			kp->ki_pri.pri_level = mtd.td_priority;
425			kp->ki_pri.pri_native = mtd.td_base_pri;
426			kp->ki_lastcpu = mtd.td_lastcpu;
427			kp->ki_wchan = mtd.td_wchan;
428			if (mtd.td_name[0] != 0)
429				strlcpy(kp->ki_ocomm, mtd.td_name, MAXCOMLEN);
430			kp->ki_oncpu = mtd.td_oncpu;
431			if (mtd.td_name[0] != '\0')
432				strlcpy(kp->ki_ocomm, mtd.td_name, sizeof(kp->ki_ocomm));
433			kp->ki_pctcpu = 0;
434			kp->ki_rqindex = 0;
435		} else {
436			kp->ki_stat = SZOMB;
437		}
438		bcopy(&kinfo_proc, bp, sizeof(kinfo_proc));
439		++bp;
440		++cnt;
441	}
442	return (cnt);
443}
444
445/*
446 * Build proc info array by reading in proc list from a crash dump.
447 * Return number of procs read.  maxcnt is the max we will read.
448 */
449static int
450kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
451    u_long a_zombproc, int maxcnt)
452{
453	struct kinfo_proc *bp = kd->procbase;
454	int acnt, zcnt;
455	struct proc *p;
456
457	if (KREAD(kd, a_allproc, &p)) {
458		_kvm_err(kd, kd->program, "cannot read allproc");
459		return (-1);
460	}
461	acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
462	if (acnt < 0)
463		return (acnt);
464
465	if (KREAD(kd, a_zombproc, &p)) {
466		_kvm_err(kd, kd->program, "cannot read zombproc");
467		return (-1);
468	}
469	zcnt = kvm_proclist(kd, what, arg, p, bp + acnt, maxcnt - acnt);
470	if (zcnt < 0)
471		zcnt = 0;
472
473	return (acnt + zcnt);
474}
475
476struct kinfo_proc *
477kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
478{
479	int mib[4], st, nprocs;
480	size_t size;
481	int temp_op;
482
483	if (kd->procbase != 0) {
484		free((void *)kd->procbase);
485		/*
486		 * Clear this pointer in case this call fails.  Otherwise,
487		 * kvm_close() will free it again.
488		 */
489		kd->procbase = 0;
490	}
491	if (ISALIVE(kd)) {
492		size = 0;
493		mib[0] = CTL_KERN;
494		mib[1] = KERN_PROC;
495		mib[2] = op;
496		mib[3] = arg;
497		temp_op = op & ~KERN_PROC_INC_THREAD;
498		st = sysctl(mib,
499		    temp_op == KERN_PROC_ALL || temp_op == KERN_PROC_PROC ?
500		    3 : 4, NULL, &size, NULL, 0);
501		if (st == -1) {
502			_kvm_syserr(kd, kd->program, "kvm_getprocs");
503			return (0);
504		}
505		/*
506		 * We can't continue with a size of 0 because we pass
507		 * it to realloc() (via _kvm_realloc()), and passing 0
508		 * to realloc() results in undefined behavior.
509		 */
510		if (size == 0) {
511			/*
512			 * XXX: We should probably return an invalid,
513			 * but non-NULL, pointer here so any client
514			 * program trying to dereference it will
515			 * crash.  However, _kvm_freeprocs() calls
516			 * free() on kd->procbase if it isn't NULL,
517			 * and free()'ing a junk pointer isn't good.
518			 * Then again, _kvm_freeprocs() isn't used
519			 * anywhere . . .
520			 */
521			kd->procbase = _kvm_malloc(kd, 1);
522			goto liveout;
523		}
524		do {
525			size += size / 10;
526			kd->procbase = (struct kinfo_proc *)
527			    _kvm_realloc(kd, kd->procbase, size);
528			if (kd->procbase == 0)
529				return (0);
530			st = sysctl(mib, temp_op == KERN_PROC_ALL ||
531			    temp_op == KERN_PROC_PROC ? 3 : 4,
532			    kd->procbase, &size, NULL, 0);
533		} while (st == -1 && errno == ENOMEM);
534		if (st == -1) {
535			_kvm_syserr(kd, kd->program, "kvm_getprocs");
536			return (0);
537		}
538		/*
539		 * We have to check the size again because sysctl()
540		 * may "round up" oldlenp if oldp is NULL; hence it
541		 * might've told us that there was data to get when
542		 * there really isn't any.
543		 */
544		if (size > 0 &&
545		    kd->procbase->ki_structsize != sizeof(struct kinfo_proc)) {
546			_kvm_err(kd, kd->program,
547			    "kinfo_proc size mismatch (expected %zu, got %d)",
548			    sizeof(struct kinfo_proc),
549			    kd->procbase->ki_structsize);
550			return (0);
551		}
552liveout:
553		nprocs = size == 0 ? 0 : size / kd->procbase->ki_structsize;
554	} else {
555		struct nlist nl[7], *p;
556
557		nl[0].n_name = "_nprocs";
558		nl[1].n_name = "_allproc";
559		nl[2].n_name = "_zombproc";
560		nl[3].n_name = "_ticks";
561		nl[4].n_name = "_hz";
562		nl[5].n_name = "_cpu_tick_frequency";
563		nl[6].n_name = 0;
564
565		if (kvm_nlist(kd, nl) != 0) {
566			for (p = nl; p->n_type != 0; ++p)
567				;
568			_kvm_err(kd, kd->program,
569				 "%s: no such symbol", p->n_name);
570			return (0);
571		}
572		if (KREAD(kd, nl[0].n_value, &nprocs)) {
573			_kvm_err(kd, kd->program, "can't read nprocs");
574			return (0);
575		}
576		if (KREAD(kd, nl[3].n_value, &ticks)) {
577			_kvm_err(kd, kd->program, "can't read ticks");
578			return (0);
579		}
580		if (KREAD(kd, nl[4].n_value, &hz)) {
581			_kvm_err(kd, kd->program, "can't read hz");
582			return (0);
583		}
584		if (KREAD(kd, nl[5].n_value, &cpu_tick_frequency)) {
585			_kvm_err(kd, kd->program,
586			    "can't read cpu_tick_frequency");
587			return (0);
588		}
589		size = nprocs * sizeof(struct kinfo_proc);
590		kd->procbase = (struct kinfo_proc *)_kvm_malloc(kd, size);
591		if (kd->procbase == 0)
592			return (0);
593
594		nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
595				      nl[2].n_value, nprocs);
596#ifdef notdef
597		size = nprocs * sizeof(struct kinfo_proc);
598		(void)realloc(kd->procbase, size);
599#endif
600	}
601	*cnt = nprocs;
602	return (kd->procbase);
603}
604
605void
606_kvm_freeprocs(kvm_t *kd)
607{
608	if (kd->procbase) {
609		free(kd->procbase);
610		kd->procbase = 0;
611	}
612}
613
614void *
615_kvm_realloc(kvm_t *kd, void *p, size_t n)
616{
617	void *np = (void *)realloc(p, n);
618
619	if (np == 0) {
620		free(p);
621		_kvm_err(kd, kd->program, "out of memory");
622	}
623	return (np);
624}
625
626#ifndef MAX
627#define MAX(a, b) ((a) > (b) ? (a) : (b))
628#endif
629
630/*
631 * Read in an argument vector from the user address space of process kp.
632 * addr if the user-space base address of narg null-terminated contiguous
633 * strings.  This is used to read in both the command arguments and
634 * environment strings.  Read at most maxcnt characters of strings.
635 */
636static char **
637kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, u_long addr, int narg,
638    int maxcnt)
639{
640	char *np, *cp, *ep, *ap;
641	u_long oaddr = -1;
642	int len, cc;
643	char **argv;
644
645	/*
646	 * Check that there aren't an unreasonable number of arguments,
647	 * and that the address is in user space.  Special test for
648	 * VM_MIN_ADDRESS as it evaluates to zero, but is not a simple zero
649	 * constant for some archs.  We cannot use the pre-processor here and
650	 * for some archs the compiler would trigger a signedness warning.
651	 */
652	if (narg > 512 || addr + 1 < VM_MIN_ADDRESS + 1 || addr >= VM_MAXUSER_ADDRESS)
653		return (0);
654
655	/*
656	 * kd->argv : work space for fetching the strings from the target
657	 *            process's space, and is converted for returning to caller
658	 */
659	if (kd->argv == 0) {
660		/*
661		 * Try to avoid reallocs.
662		 */
663		kd->argc = MAX(narg + 1, 32);
664		kd->argv = (char **)_kvm_malloc(kd, kd->argc *
665						sizeof(*kd->argv));
666		if (kd->argv == 0)
667			return (0);
668	} else if (narg + 1 > kd->argc) {
669		kd->argc = MAX(2 * kd->argc, narg + 1);
670		kd->argv = (char **)_kvm_realloc(kd, kd->argv, kd->argc *
671						sizeof(*kd->argv));
672		if (kd->argv == 0)
673			return (0);
674	}
675	/*
676	 * kd->argspc : returned to user, this is where the kd->argv
677	 *              arrays are left pointing to the collected strings.
678	 */
679	if (kd->argspc == 0) {
680		kd->argspc = (char *)_kvm_malloc(kd, PAGE_SIZE);
681		if (kd->argspc == 0)
682			return (0);
683		kd->arglen = PAGE_SIZE;
684	}
685	/*
686	 * kd->argbuf : used to pull in pages from the target process.
687	 *              the strings are copied out of here.
688	 */
689	if (kd->argbuf == 0) {
690		kd->argbuf = (char *)_kvm_malloc(kd, PAGE_SIZE);
691		if (kd->argbuf == 0)
692			return (0);
693	}
694
695	/* Pull in the target process'es argv vector */
696	cc = sizeof(char *) * narg;
697	if (kvm_uread(kd, kp, addr, (char *)kd->argv, cc) != cc)
698		return (0);
699	/*
700	 * ap : saved start address of string we're working on in kd->argspc
701	 * np : pointer to next place to write in kd->argspc
702	 * len: length of data in kd->argspc
703	 * argv: pointer to the argv vector that we are hunting around the
704	 *       target process space for, and converting to addresses in
705	 *       our address space (kd->argspc).
706	 */
707	ap = np = kd->argspc;
708	argv = kd->argv;
709	len = 0;
710	/*
711	 * Loop over pages, filling in the argument vector.
712	 * Note that the argv strings could be pointing *anywhere* in
713	 * the user address space and are no longer contiguous.
714	 * Note that *argv is modified when we are going to fetch a string
715	 * that crosses a page boundary.  We copy the next part of the string
716	 * into to "np" and eventually convert the pointer.
717	 */
718	while (argv < kd->argv + narg && *argv != 0) {
719
720		/* get the address that the current argv string is on */
721		addr = (u_long)*argv & ~(PAGE_SIZE - 1);
722
723		/* is it the same page as the last one? */
724		if (addr != oaddr) {
725			if (kvm_uread(kd, kp, addr, kd->argbuf, PAGE_SIZE) !=
726			    PAGE_SIZE)
727				return (0);
728			oaddr = addr;
729		}
730
731		/* offset within the page... kd->argbuf */
732		addr = (u_long)*argv & (PAGE_SIZE - 1);
733
734		/* cp = start of string, cc = count of chars in this chunk */
735		cp = kd->argbuf + addr;
736		cc = PAGE_SIZE - addr;
737
738		/* dont get more than asked for by user process */
739		if (maxcnt > 0 && cc > maxcnt - len)
740			cc = maxcnt - len;
741
742		/* pointer to end of string if we found it in this page */
743		ep = memchr(cp, '\0', cc);
744		if (ep != 0)
745			cc = ep - cp + 1;
746		/*
747		 * at this point, cc is the count of the chars that we are
748		 * going to retrieve this time. we may or may not have found
749		 * the end of it.  (ep points to the null if the end is known)
750		 */
751
752		/* will we exceed the malloc/realloced buffer? */
753		if (len + cc > kd->arglen) {
754			int off;
755			char **pp;
756			char *op = kd->argspc;
757
758			kd->arglen *= 2;
759			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc,
760							  kd->arglen);
761			if (kd->argspc == 0)
762				return (0);
763			/*
764			 * Adjust argv pointers in case realloc moved
765			 * the string space.
766			 */
767			off = kd->argspc - op;
768			for (pp = kd->argv; pp < argv; pp++)
769				*pp += off;
770			ap += off;
771			np += off;
772		}
773		/* np = where to put the next part of the string in kd->argspc*/
774		/* np is kinda redundant.. could use "kd->argspc + len" */
775		memcpy(np, cp, cc);
776		np += cc;	/* inc counters */
777		len += cc;
778
779		/*
780		 * if end of string found, set the *argv pointer to the
781		 * saved beginning of string, and advance. argv points to
782		 * somewhere in kd->argv..  This is initially relative
783		 * to the target process, but when we close it off, we set
784		 * it to point in our address space.
785		 */
786		if (ep != 0) {
787			*argv++ = ap;
788			ap = np;
789		} else {
790			/* update the address relative to the target process */
791			*argv += cc;
792		}
793
794		if (maxcnt > 0 && len >= maxcnt) {
795			/*
796			 * We're stopping prematurely.  Terminate the
797			 * current string.
798			 */
799			if (ep == 0) {
800				*np = '\0';
801				*argv++ = ap;
802			}
803			break;
804		}
805	}
806	/* Make sure argv is terminated. */
807	*argv = 0;
808	return (kd->argv);
809}
810
811static void
812ps_str_a(struct ps_strings *p, u_long *addr, int *n)
813{
814	*addr = (u_long)p->ps_argvstr;
815	*n = p->ps_nargvstr;
816}
817
818static void
819ps_str_e (struct ps_strings *p, u_long *addr, int *n)
820{
821	*addr = (u_long)p->ps_envstr;
822	*n = p->ps_nenvstr;
823}
824
825/*
826 * Determine if the proc indicated by p is still active.
827 * This test is not 100% foolproof in theory, but chances of
828 * being wrong are very low.
829 */
830static int
831proc_verify(const struct kinfo_proc *curkp)
832{
833	struct kinfo_proc newkp;
834	int mib[4];
835	size_t len;
836
837	mib[0] = CTL_KERN;
838	mib[1] = KERN_PROC;
839	mib[2] = KERN_PROC_PID;
840	mib[3] = curkp->ki_pid;
841	len = sizeof(newkp);
842	if (sysctl(mib, 4, &newkp, &len, NULL, 0) == -1)
843		return (0);
844	return (curkp->ki_pid == newkp.ki_pid &&
845	    (newkp.ki_stat != SZOMB || curkp->ki_stat == SZOMB));
846}
847
848static char **
849kvm_doargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr,
850    void (*info)(struct ps_strings *, u_long *, int *))
851{
852	char **ap;
853	u_long addr;
854	int cnt;
855	static struct ps_strings arginfo;
856	static u_long ps_strings;
857	size_t len;
858
859	if (ps_strings == 0) {
860		len = sizeof(ps_strings);
861		if (sysctlbyname("kern.ps_strings", &ps_strings, &len, NULL,
862		    0) == -1)
863			ps_strings = PS_STRINGS;
864	}
865
866	/*
867	 * Pointers are stored at the top of the user stack.
868	 */
869	if (kp->ki_stat == SZOMB ||
870	    kvm_uread(kd, kp, ps_strings, (char *)&arginfo,
871		      sizeof(arginfo)) != sizeof(arginfo))
872		return (0);
873
874	(*info)(&arginfo, &addr, &cnt);
875	if (cnt == 0)
876		return (0);
877	ap = kvm_argv(kd, kp, addr, cnt, nchr);
878	/*
879	 * For live kernels, make sure this process didn't go away.
880	 */
881	if (ap != 0 && ISALIVE(kd) && !proc_verify(kp))
882		ap = 0;
883	return (ap);
884}
885
886/*
887 * Get the command args.  This code is now machine independent.
888 */
889char **
890kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
891{
892	int oid[4];
893	int i;
894	size_t bufsz;
895	static unsigned long buflen;
896	static char *buf, *p;
897	static char **bufp;
898	static int argc;
899
900	if (!ISALIVE(kd)) {
901		_kvm_err(kd, kd->program,
902		    "cannot read user space from dead kernel");
903		return (0);
904	}
905
906	if (!buflen) {
907		bufsz = sizeof(buflen);
908		i = sysctlbyname("kern.ps_arg_cache_limit",
909		    &buflen, &bufsz, NULL, 0);
910		if (i == -1) {
911			buflen = 0;
912		} else {
913			buf = malloc(buflen);
914			if (buf == NULL)
915				buflen = 0;
916			argc = 32;
917			bufp = malloc(sizeof(char *) * argc);
918		}
919	}
920	if (buf != NULL) {
921		oid[0] = CTL_KERN;
922		oid[1] = KERN_PROC;
923		oid[2] = KERN_PROC_ARGS;
924		oid[3] = kp->ki_pid;
925		bufsz = buflen;
926		i = sysctl(oid, 4, buf, &bufsz, 0, 0);
927		if (i == 0 && bufsz > 0) {
928			i = 0;
929			p = buf;
930			do {
931				bufp[i++] = p;
932				p += strlen(p) + 1;
933				if (i >= argc) {
934					argc += argc;
935					bufp = realloc(bufp,
936					    sizeof(char *) * argc);
937				}
938			} while (p < buf + bufsz);
939			bufp[i++] = 0;
940			return (bufp);
941		}
942	}
943	if (kp->ki_flag & P_SYSTEM)
944		return (NULL);
945	return (kvm_doargv(kd, kp, nchr, ps_str_a));
946}
947
948char **
949kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
950{
951	return (kvm_doargv(kd, kp, nchr, ps_str_e));
952}
953
954/*
955 * Read from user space.  The user context is given by p.
956 */
957ssize_t
958kvm_uread(kvm_t *kd, const struct kinfo_proc *kp, u_long uva, char *buf,
959	size_t len)
960{
961	char *cp;
962	char procfile[MAXPATHLEN];
963	ssize_t amount;
964	int fd;
965
966	if (!ISALIVE(kd)) {
967		_kvm_err(kd, kd->program,
968		    "cannot read user space from dead kernel");
969		return (0);
970	}
971
972	sprintf(procfile, "/proc/%d/mem", kp->ki_pid);
973	fd = open(procfile, O_RDONLY, 0);
974	if (fd < 0) {
975		_kvm_err(kd, kd->program, "cannot open %s", procfile);
976		return (0);
977	}
978
979	cp = buf;
980	while (len > 0) {
981		errno = 0;
982		if (lseek(fd, (off_t)uva, 0) == -1 && errno != 0) {
983			_kvm_err(kd, kd->program, "invalid address (%lx) in %s",
984			    uva, procfile);
985			break;
986		}
987		amount = read(fd, cp, len);
988		if (amount < 0) {
989			_kvm_syserr(kd, kd->program, "error reading %s",
990			    procfile);
991			break;
992		}
993		if (amount == 0) {
994			_kvm_err(kd, kd->program, "EOF reading %s", procfile);
995			break;
996		}
997		cp += amount;
998		uva += amount;
999		len -= amount;
1000	}
1001
1002	close(fd);
1003	return ((ssize_t)(cp - buf));
1004}
1005