Home | History | Annotate | Line # | Download | only in libkvm
kvm_file.c revision 1.14
      1 /*	$NetBSD: kvm_file.c,v 1.14 1999/08/19 05:42:56 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)kvm_file.c	8.1 (Berkeley) 6/4/93";
     40 #else
     41 __RCSID("$NetBSD: kvm_file.c,v 1.14 1999/08/19 05:42:56 cgd Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 /*
     46  * File list interface for kvm.  pstat, fstat and netstat are
     47  * users of this code, so we've factored it out into a separate module.
     48  * Thus, we keep this grunge out of the other kvm applications (i.e.,
     49  * most other applications are interested only in open/close/read/nlist).
     50  */
     51 
     52 #include <sys/param.h>
     53 #include <sys/user.h>
     54 #include <sys/proc.h>
     55 #include <sys/exec.h>
     56 #define _KERNEL
     57 #include <sys/file.h>
     58 #undef _KERNEL
     59 #include <sys/stat.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/tty.h>
     62 #include <nlist.h>
     63 #include <kvm.h>
     64 
     65 #include <vm/vm.h>
     66 #include <vm/vm_param.h>
     67 
     68 #include <sys/sysctl.h>
     69 
     70 #include <limits.h>
     71 #include <ndbm.h>
     72 #include <paths.h>
     73 #include <string.h>
     74 
     75 #include "kvm_private.h"
     76 
     77 #define KREAD(kd, addr, obj) \
     78 	(kvm_read(kd, (u_long) addr, obj, sizeof(*obj)) != sizeof(*obj))
     79 
     80 static int
     81 kvm_deadfiles __P((kvm_t *, int, int, long, int));
     82 
     83 /*
     84  * Get file structures.
     85  */
     86 /*ARGSUSED*/
     87 static int
     88 kvm_deadfiles(kd, op, arg, ofhead, nfiles)
     89 	kvm_t *kd;
     90 	int op, arg, nfiles;
     91 	long ofhead;
     92 {
     93 	int buflen = kd->arglen, n = 0;
     94 	struct file *fp;
     95 	struct filelist fhead;
     96 	char *where = kd->argspc;
     97 
     98 	/*
     99 	 * first copyout filehead
    100 	 */
    101 	if (buflen < sizeof(fhead) ||
    102 	    KREAD(kd, ofhead, &fhead)) {
    103 		_kvm_err(kd, kd->program, "can't read filehead");
    104 		return (0);
    105 	}
    106 	buflen -= sizeof(fhead);
    107 	where += sizeof(fhead);
    108 	(void)memcpy(kd->argspc, &fhead, sizeof(fhead));
    109 
    110 	/*
    111 	 * followed by an array of file structures
    112 	 */
    113 	for (fp = fhead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    114 		if (buflen > sizeof(struct file)) {
    115 			if (KREAD(kd, (long)fp, ((struct file *)(void *)where))) {
    116 				_kvm_err(kd, kd->program, "can't read kfp");
    117 				return (0);
    118 			}
    119 			buflen -= sizeof(struct file);
    120 			fp = (struct file *)(void *)where;
    121 			where += sizeof(struct file);
    122 			n++;
    123 		}
    124 	}
    125 	if (n != nfiles) {
    126 		_kvm_err(kd, kd->program, "inconsistent nfiles");
    127 		return (0);
    128 	}
    129 	return (nfiles);
    130 }
    131 
    132 char *
    133 kvm_getfiles(kd, op, arg, cnt)
    134 	kvm_t *kd;
    135 	int op, arg;
    136 	int *cnt;
    137 {
    138 	size_t size;
    139 	int mib[2], st;
    140 	int numfiles;
    141 	struct file *fp, *fplim;
    142 	struct filelist fhead;
    143 
    144 	if (ISALIVE(kd)) {
    145 		size = 0;
    146 		mib[0] = CTL_KERN;
    147 		mib[1] = KERN_FILE;
    148 		st = sysctl(mib, 2, NULL, &size, NULL, 0);
    149 		if (st == -1) {
    150 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
    151 			return (0);
    152 		}
    153 		if (kd->argspc == 0)
    154 			kd->argspc = (char *)_kvm_malloc(kd, size);
    155 		else if (kd->arglen < size)
    156 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
    157 		if (kd->argspc == 0)
    158 			return (0);
    159 		kd->arglen = size;
    160 		st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
    161 		if (st == -1 || size < sizeof(fhead)) {
    162 			_kvm_syserr(kd, kd->program, "kvm_getfiles");
    163 			return (0);
    164 		}
    165 		(void)memcpy(&fhead, kd->argspc, sizeof(fhead));
    166 		fp = (struct file *)(void *)(kd->argspc + sizeof(fhead));
    167 		fplim = (struct file *)(void *)(kd->argspc + size);
    168 		for (numfiles = 0; fhead.lh_first && (fp < fplim);
    169 		    numfiles++, fp++)
    170 			fhead.lh_first = fp->f_list.le_next;
    171 	} else {
    172 		struct nlist nl[3], *p;
    173 
    174 		nl[0].n_name = "_nfiles";
    175 		nl[1].n_name = "_filehead";
    176 		nl[2].n_name = 0;
    177 
    178 		if (kvm_nlist(kd, nl) != 0) {
    179 			for (p = nl; p->n_type != 0; ++p)
    180 				;
    181 			_kvm_err(kd, kd->program,
    182 				 "%s: no such symbol", p->n_name);
    183 			return (0);
    184 		}
    185 		if (KREAD(kd, nl[0].n_value, &numfiles)) {
    186 			_kvm_err(kd, kd->program, "can't read numfiles");
    187 			return (0);
    188 		}
    189 		size = sizeof(fhead) + (numfiles + 10) * sizeof(struct file);
    190 		if (kd->argspc == 0)
    191 			kd->argspc = (char *)_kvm_malloc(kd, size);
    192 		else if (kd->arglen < size)
    193 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
    194 		if (kd->argspc == 0)
    195 			return (0);
    196 		kd->arglen = size;
    197 		numfiles = kvm_deadfiles(kd, op, arg, (long)nl[1].n_value,
    198 		    numfiles);
    199 		if (numfiles == 0)
    200 			return (0);
    201 	}
    202 	*cnt = numfiles;
    203 	return (kd->argspc);
    204 }
    205