kvm_file.c revision 83551
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1989, 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
3483551Sdillon#include <sys/cdefs.h>
3583551Sdillon__FBSDID("$FreeBSD: head/lib/libkvm/kvm_file.c 83551 2001-09-16 21:35:07Z dillon $");
3683551Sdillon
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3855127Speter#if 0
391573Srgrimesstatic char sccsid[] = "@(#)kvm_file.c	8.1 (Berkeley) 6/4/93";
4055127Speter#endif
411573Srgrimes#endif /* LIBC_SCCS and not lint */
421573Srgrimes
431573Srgrimes/*
448870Srgrimes * File list interface for kvm.  pstat, fstat and netstat are
451573Srgrimes * users of this code, so we've factored it out into a separate module.
461573Srgrimes * Thus, we keep this grunge out of the other kvm applications (i.e.,
471573Srgrimes * most other applications are interested only in open/close/read/nlist).
481573Srgrimes */
491573Srgrimes
501573Srgrimes#include <sys/param.h>
5176176Smarkm#include <sys/lock.h>
5276176Smarkm#include <sys/mutex.h>
531573Srgrimes#include <sys/user.h>
541573Srgrimes#include <sys/proc.h>
5555206Speter#define _KERNEL
561573Srgrimes#include <sys/file.h>
5755206Speter#undef _KERNEL
581573Srgrimes#include <sys/stat.h>
591573Srgrimes#include <sys/ioctl.h>
601573Srgrimes#include <nlist.h>
611573Srgrimes#include <kvm.h>
621573Srgrimes
631573Srgrimes#include <vm/vm.h>
641573Srgrimes#include <vm/vm_param.h>
651573Srgrimes#include <vm/swap_pager.h>
661573Srgrimes
671573Srgrimes#include <sys/sysctl.h>
681573Srgrimes
691573Srgrimes#include <limits.h>
701573Srgrimes#include <ndbm.h>
711573Srgrimes#include <paths.h>
721573Srgrimes
731573Srgrimes#include "kvm_private.h"
741573Srgrimes
751573Srgrimes#define KREAD(kd, addr, obj) \
761573Srgrimes	(kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
771573Srgrimes
781573Srgrimes/*
791573Srgrimes * Get file structures.
801573Srgrimes */
8117141Sjkhstatic int
821573Srgrimeskvm_deadfiles(kd, op, arg, filehead_o, nfiles)
831573Srgrimes	kvm_t *kd;
841573Srgrimes	int op, arg, nfiles;
851573Srgrimes	long filehead_o;
861573Srgrimes{
876683Sphk	int buflen = kd->arglen, n = 0;
8814523Shsu	struct file *fp;
891573Srgrimes	register char *where = kd->argspc;
9014523Shsu	struct filelist filehead;
911573Srgrimes
921573Srgrimes	/*
931573Srgrimes	 * first copyout filehead
941573Srgrimes	 */
951573Srgrimes	if (buflen > sizeof (filehead)) {
961573Srgrimes		if (KREAD(kd, filehead_o, &filehead)) {
971573Srgrimes			_kvm_err(kd, kd->program, "can't read filehead");
981573Srgrimes			return (0);
991573Srgrimes		}
1001573Srgrimes		buflen -= sizeof (filehead);
1011573Srgrimes		where += sizeof (filehead);
10214523Shsu		*(struct filelist *)kd->argspc = filehead;
1031573Srgrimes	}
1041573Srgrimes	/*
1051573Srgrimes	 * followed by an array of file structures
1061573Srgrimes	 */
10770525Sben	LIST_FOREACH(fp, &filehead, f_list) {
1081573Srgrimes		if (buflen > sizeof (struct file)) {
1091573Srgrimes			if (KREAD(kd, (long)fp, ((struct file *)where))) {
1101573Srgrimes				_kvm_err(kd, kd->program, "can't read kfp");
1111573Srgrimes				return (0);
1121573Srgrimes			}
1131573Srgrimes			buflen -= sizeof (struct file);
1141573Srgrimes			fp = (struct file *)where;
1151573Srgrimes			where += sizeof (struct file);
1161573Srgrimes			n++;
1171573Srgrimes		}
1181573Srgrimes	}
1191573Srgrimes	if (n != nfiles) {
1201573Srgrimes		_kvm_err(kd, kd->program, "inconsistant nfiles");
1211573Srgrimes		return (0);
1221573Srgrimes	}
1231573Srgrimes	return (nfiles);
1241573Srgrimes}
1251573Srgrimes
1261573Srgrimeschar *
1271573Srgrimeskvm_getfiles(kd, op, arg, cnt)
1281573Srgrimes	kvm_t *kd;
1291573Srgrimes	int op, arg;
1301573Srgrimes	int *cnt;
1311573Srgrimes{
13238534Sdfr	int mib[2], st, nfiles;
13338534Sdfr	size_t size;
13414523Shsu	struct file *fp, *fplim;
13514523Shsu	struct filelist filehead;
1361573Srgrimes
1371573Srgrimes	if (ISALIVE(kd)) {
1381573Srgrimes		size = 0;
1391573Srgrimes		mib[0] = CTL_KERN;
1401573Srgrimes		mib[1] = KERN_FILE;
1411573Srgrimes		st = sysctl(mib, 2, NULL, &size, NULL, 0);
1421573Srgrimes		if (st == -1) {
14357321Speter			_kvm_syserr(kd, kd->program, "kvm_getfiles");
1441573Srgrimes			return (0);
1451573Srgrimes		}
1461573Srgrimes		if (kd->argspc == 0)
1471573Srgrimes			kd->argspc = (char *)_kvm_malloc(kd, size);
1481573Srgrimes		else if (kd->arglen < size)
1491573Srgrimes			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
1501573Srgrimes		if (kd->argspc == 0)
1511573Srgrimes			return (0);
1521573Srgrimes		kd->arglen = size;
1531573Srgrimes		st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
1541573Srgrimes		if (st == -1 || size < sizeof(filehead)) {
1551573Srgrimes			_kvm_syserr(kd, kd->program, "kvm_getfiles");
1561573Srgrimes			return (0);
1571573Srgrimes		}
15814523Shsu		filehead = *(struct filelist *)kd->argspc;
1591573Srgrimes		fp = (struct file *)(kd->argspc + sizeof (filehead));
1601573Srgrimes		fplim = (struct file *)(kd->argspc + size);
16170525Sben		for (nfiles = 0; LIST_FIRST(&filehead) && (fp < fplim); nfiles++, fp++)
16270525Sben			LIST_FIRST(&filehead) = LIST_NEXT(fp, f_list);
1631573Srgrimes	} else {
1641573Srgrimes		struct nlist nl[3], *p;
1651573Srgrimes
1661573Srgrimes		nl[0].n_name = "_filehead";
1671573Srgrimes		nl[1].n_name = "_nfiles";
1681573Srgrimes		nl[2].n_name = 0;
1691573Srgrimes
1701573Srgrimes		if (kvm_nlist(kd, nl) != 0) {
1711573Srgrimes			for (p = nl; p->n_type != 0; ++p)
1721573Srgrimes				;
1731573Srgrimes			_kvm_err(kd, kd->program,
1741573Srgrimes				 "%s: no such symbol", p->n_name);
1751573Srgrimes			return (0);
1761573Srgrimes		}
1771573Srgrimes		if (KREAD(kd, nl[0].n_value, &nfiles)) {
1781573Srgrimes			_kvm_err(kd, kd->program, "can't read nfiles");
1791573Srgrimes			return (0);
1801573Srgrimes		}
1811573Srgrimes		size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
1821573Srgrimes		if (kd->argspc == 0)
1831573Srgrimes			kd->argspc = (char *)_kvm_malloc(kd, size);
1841573Srgrimes		else if (kd->arglen < size)
1851573Srgrimes			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
1861573Srgrimes		if (kd->argspc == 0)
1871573Srgrimes			return (0);
1881573Srgrimes		kd->arglen = size;
1891573Srgrimes		nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
1901573Srgrimes		if (nfiles == 0)
1911573Srgrimes			return (0);
1921573Srgrimes	}
1931573Srgrimes	*cnt = nfiles;
1941573Srgrimes	return (kd->argspc);
1951573Srgrimes}
196