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