2001-11-05 17:59:21 +03:00
|
|
|
/* $NetBSD: kvm_file.c,v 1.18 2001/11/05 15:01:49 lukem Exp $ */
|
1996-03-19 01:33:07 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1989, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-08-15 06:18:02 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1996-03-19 01:33:07 +03:00
|
|
|
#if 0
|
1994-05-09 07:15:36 +04:00
|
|
|
static char sccsid[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93";
|
1996-03-19 01:33:07 +03:00
|
|
|
#else
|
2001-11-05 17:59:21 +03:00
|
|
|
__RCSID("$NetBSD: kvm_file.c,v 1.18 2001/11/05 15:01:49 lukem Exp $");
|
1996-03-19 01:33:07 +03:00
|
|
|
#endif
|
1994-05-09 07:15:36 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
/*
|
1999-07-02 19:28:49 +04:00
|
|
|
* File list interface for kvm. pstat, fstat and netstat are
|
1994-05-09 07:15:36 +04:00
|
|
|
* users of this code, so we've factored it out into a separate module.
|
|
|
|
* Thus, we keep this grunge out of the other kvm applications (i.e.,
|
|
|
|
* most other applications are interested only in open/close/read/nlist).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/exec.h>
|
1995-03-28 21:18:59 +04:00
|
|
|
#define _KERNEL
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <sys/file.h>
|
1995-03-28 21:18:59 +04:00
|
|
|
#undef _KERNEL
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
|
2000-06-29 10:34:22 +04:00
|
|
|
#include <uvm/uvm_extern.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <ndbm.h>
|
|
|
|
#include <paths.h>
|
1998-09-29 06:19:02 +04:00
|
|
|
#include <string.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
#include "kvm_private.h"
|
|
|
|
|
|
|
|
#define KREAD(kd, addr, obj) \
|
1998-09-27 22:15:58 +04:00
|
|
|
(kvm_read(kd, (u_long) addr, obj, sizeof(*obj)) != sizeof(*obj))
|
1994-05-09 07:15:36 +04:00
|
|
|
|
1997-08-15 21:52:45 +04:00
|
|
|
static int
|
|
|
|
kvm_deadfiles __P((kvm_t *, int, int, long, int));
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Get file structures.
|
|
|
|
*/
|
1998-09-27 22:15:58 +04:00
|
|
|
/*ARGSUSED*/
|
1997-06-20 08:41:33 +04:00
|
|
|
static int
|
2001-11-05 17:59:21 +03:00
|
|
|
kvm_deadfiles(kd, op, arg, ofhead, numfiles)
|
1994-05-09 07:15:36 +04:00
|
|
|
kvm_t *kd;
|
2001-11-05 17:59:21 +03:00
|
|
|
int op, arg, numfiles;
|
1998-09-27 22:15:58 +04:00
|
|
|
long ofhead;
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1997-08-15 06:18:02 +04:00
|
|
|
int buflen = kd->arglen, n = 0;
|
|
|
|
struct file *fp;
|
1998-09-27 22:15:58 +04:00
|
|
|
struct filelist fhead;
|
1998-02-03 22:12:13 +03:00
|
|
|
char *where = kd->argspc;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* first copyout filehead
|
|
|
|
*/
|
1999-08-19 09:42:56 +04:00
|
|
|
if (buflen < sizeof(fhead) ||
|
|
|
|
KREAD(kd, ofhead, &fhead)) {
|
|
|
|
_kvm_err(kd, kd->program, "can't read filehead");
|
|
|
|
return (0);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
1999-08-19 09:42:56 +04:00
|
|
|
buflen -= sizeof(fhead);
|
|
|
|
where += sizeof(fhead);
|
|
|
|
(void)memcpy(kd->argspc, &fhead, sizeof(fhead));
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* followed by an array of file structures
|
|
|
|
*/
|
1998-09-27 22:15:58 +04:00
|
|
|
for (fp = fhead.lh_first; fp != 0; fp = fp->f_list.le_next) {
|
|
|
|
if (buflen > sizeof(struct file)) {
|
|
|
|
if (KREAD(kd, (long)fp, ((struct file *)(void *)where))) {
|
1994-05-09 07:15:36 +04:00
|
|
|
_kvm_err(kd, kd->program, "can't read kfp");
|
|
|
|
return (0);
|
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
buflen -= sizeof(struct file);
|
|
|
|
fp = (struct file *)(void *)where;
|
1999-08-19 09:42:56 +04:00
|
|
|
where += sizeof(struct file);
|
1994-05-09 07:15:36 +04:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
2001-11-05 17:59:21 +03:00
|
|
|
if (n != numfiles) {
|
1997-08-15 06:18:02 +04:00
|
|
|
_kvm_err(kd, kd->program, "inconsistent nfiles");
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
|
|
|
}
|
2001-11-05 17:59:21 +03:00
|
|
|
return (numfiles);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
kvm_getfiles(kd, op, arg, cnt)
|
|
|
|
kvm_t *kd;
|
|
|
|
int op, arg;
|
|
|
|
int *cnt;
|
|
|
|
{
|
1994-10-19 06:03:39 +03:00
|
|
|
size_t size;
|
1998-09-27 22:15:58 +04:00
|
|
|
int mib[2], st;
|
|
|
|
int numfiles;
|
1994-08-30 21:39:11 +04:00
|
|
|
struct file *fp, *fplim;
|
1998-09-27 22:15:58 +04:00
|
|
|
struct filelist fhead;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
if (ISSYSCTL(kd)) {
|
1994-05-09 07:15:36 +04:00
|
|
|
size = 0;
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_FILE;
|
|
|
|
st = sysctl(mib, 2, NULL, &size, NULL, 0);
|
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getprocs");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (kd->argspc == 0)
|
|
|
|
kd->argspc = (char *)_kvm_malloc(kd, size);
|
|
|
|
else if (kd->arglen < size)
|
|
|
|
kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
|
|
|
|
if (kd->argspc == 0)
|
|
|
|
return (0);
|
|
|
|
kd->arglen = size;
|
|
|
|
st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
|
1998-09-27 22:15:58 +04:00
|
|
|
if (st == -1 || size < sizeof(fhead)) {
|
1994-05-09 07:15:36 +04:00
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getfiles");
|
|
|
|
return (0);
|
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
(void)memcpy(&fhead, kd->argspc, sizeof(fhead));
|
|
|
|
fp = (struct file *)(void *)(kd->argspc + sizeof(fhead));
|
|
|
|
fplim = (struct file *)(void *)(kd->argspc + size);
|
|
|
|
for (numfiles = 0; fhead.lh_first && (fp < fplim);
|
|
|
|
numfiles++, fp++)
|
|
|
|
fhead.lh_first = fp->f_list.le_next;
|
1994-05-09 07:15:36 +04:00
|
|
|
} else {
|
|
|
|
struct nlist nl[3], *p;
|
|
|
|
|
1999-08-19 09:42:56 +04:00
|
|
|
nl[0].n_name = "_nfiles";
|
|
|
|
nl[1].n_name = "_filehead";
|
1994-05-09 07:15:36 +04:00
|
|
|
nl[2].n_name = 0;
|
|
|
|
|
|
|
|
if (kvm_nlist(kd, nl) != 0) {
|
|
|
|
for (p = nl; p->n_type != 0; ++p)
|
|
|
|
;
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"%s: no such symbol", p->n_name);
|
|
|
|
return (0);
|
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
if (KREAD(kd, nl[0].n_value, &numfiles)) {
|
|
|
|
_kvm_err(kd, kd->program, "can't read numfiles");
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
size = sizeof(fhead) + (numfiles + 10) * sizeof(struct file);
|
1994-05-09 07:15:36 +04:00
|
|
|
if (kd->argspc == 0)
|
|
|
|
kd->argspc = (char *)_kvm_malloc(kd, size);
|
|
|
|
else if (kd->arglen < size)
|
|
|
|
kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
|
|
|
|
if (kd->argspc == 0)
|
|
|
|
return (0);
|
|
|
|
kd->arglen = size;
|
1998-09-27 22:15:58 +04:00
|
|
|
numfiles = kvm_deadfiles(kd, op, arg, (long)nl[1].n_value,
|
|
|
|
numfiles);
|
|
|
|
if (numfiles == 0)
|
1994-05-09 07:15:36 +04:00
|
|
|
return (0);
|
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
*cnt = numfiles;
|
1994-05-09 07:15:36 +04:00
|
|
|
return (kd->argspc);
|
|
|
|
}
|