Home | History | Annotate | Line # | Download | only in libkvm
kvm_file.c revision 1.4
      1 /*-
      2  * Copyright (c) 1989, 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     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 defined(LIBC_SCCS) && !defined(lint)
     35 static char sccsid[] = "@(#)kvm_file.c	8.1 (Berkeley) 6/4/93";
     36 #endif /* LIBC_SCCS and not lint */
     37 
     38 /*
     39  * File list interface for kvm.  pstat, fstat and netstat are
     40  * users of this code, so we've factored it out into a separate module.
     41  * Thus, we keep this grunge out of the other kvm applications (i.e.,
     42  * most other applications are interested only in open/close/read/nlist).
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/user.h>
     47 #include <sys/proc.h>
     48 #include <sys/exec.h>
     49 #define _KERNEL
     50 #include <sys/file.h>
     51 #undef _KERNEL
     52 #include <sys/stat.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/tty.h>
     55 #include <nlist.h>
     56 #include <kvm.h>
     57 
     58 #include <vm/vm.h>
     59 #include <vm/vm_param.h>
     60 #include <vm/swap_pager.h>
     61 
     62 #include <sys/sysctl.h>
     63 
     64 #include <limits.h>
     65 #include <ndbm.h>
     66 #include <paths.h>
     67 
     68 #include "kvm_private.h"
     69 
     70 #define KREAD(kd, addr, obj) \
     71 	(kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
     72 
     73 /*
     74  * Get file structures.
     75  */
     76 static
     77 kvm_deadfiles(kd, op, arg, filehead_o, nfiles)
     78 	kvm_t *kd;
     79 	int op, arg, nfiles;
     80 	long filehead_o;
     81 {
     82 	int buflen = kd->arglen, needed = buflen, error, n = 0;
     83 	struct file *fp, file;
     84 	struct filelist filehead;
     85 	register char *where = kd->argspc;
     86 	char *start = where;
     87 
     88 	/*
     89 	 * first copyout filehead
     90 	 */
     91 	if (buflen > sizeof (filehead)) {
     92 		if (KREAD(kd, filehead_o, &filehead)) {
     93 			_kvm_err(kd, kd->program, "can't read filehead");
     94 			return (0);
     95 		}
     96 		buflen -= sizeof (filehead);
     97 		where += sizeof (filehead);
     98 		*(struct filelist *)kd->argspc = filehead;
     99 	}
    100 	/*
    101 	 * followed by an array of file structures
    102 	 */
    103 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    104 		if (buflen > sizeof (struct file)) {
    105 			if (KREAD(kd, (long)fp, ((struct file *)where))) {
    106 				_kvm_err(kd, kd->program, "can't read kfp");
    107 				return (0);
    108 			}
    109 			buflen -= sizeof (struct file);
    110 			fp = (struct file *)where;
    111 			where += sizeof (struct file);
    112 			n++;
    113 		}
    114 	}
    115 	if (n != nfiles) {
    116 		_kvm_err(kd, kd->program, "inconsistant nfiles");
    117 		return (0);
    118 	}
    119 	return (nfiles);
    120 }
    121 
    122 char *
    123 kvm_getfiles(kd, op, arg, cnt)
    124 	kvm_t *kd;
    125 	int op, arg;
    126 	int *cnt;
    127 {
    128 	size_t size;
    129 	int mib[2], st, nfiles;
    130 	struct file *fp, *fplim;
    131 	struct filelist filehead;
    132 
    133 	if (ISALIVE(kd)) {
    134 		size = 0;
    135 		mib[0] = CTL_KERN;
    136 		mib[1] = KERN_FILE;
    137 		st = sysctl(mib, 2, NULL, &size, NULL, 0);
    138 		if (st == -1) {
    139 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
    140 			return (0);
    141 		}
    142 		if (kd->argspc == 0)
    143 			kd->argspc = (char *)_kvm_malloc(kd, size);
    144 		else if (kd->arglen < size)
    145 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
    146 		if (kd->argspc == 0)
    147 			return (0);
    148 		kd->arglen = size;
    149 		st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
    150 		if (st == -1 || size < sizeof(filehead)) {
    151 			_kvm_syserr(kd, kd->program, "kvm_getfiles");
    152 			return (0);
    153 		}
    154 		filehead = *(struct filelist *)kd->argspc;
    155 		fp = (struct file *)(kd->argspc + sizeof (filehead));
    156 		fplim = (struct file *)(kd->argspc + size);
    157 		for (nfiles = 0; filehead.lh_first && (fp < fplim);
    158 		    nfiles++, fp++)
    159 			filehead.lh_first = fp->f_list.le_next;
    160 	} else {
    161 		struct nlist nl[3], *p;
    162 
    163 		nl[0].n_name = "_filehead";
    164 		nl[1].n_name = "_nfiles";
    165 		nl[2].n_name = 0;
    166 
    167 		if (kvm_nlist(kd, nl) != 0) {
    168 			for (p = nl; p->n_type != 0; ++p)
    169 				;
    170 			_kvm_err(kd, kd->program,
    171 				 "%s: no such symbol", p->n_name);
    172 			return (0);
    173 		}
    174 		if (KREAD(kd, nl[0].n_value, &nfiles)) {
    175 			_kvm_err(kd, kd->program, "can't read nfiles");
    176 			return (0);
    177 		}
    178 		size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    179 		if (kd->argspc == 0)
    180 			kd->argspc = (char *)_kvm_malloc(kd, size);
    181 		else if (kd->arglen < size)
    182 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
    183 		if (kd->argspc == 0)
    184 			return (0);
    185 		kd->arglen = size;
    186 		nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
    187 		if (nfiles == 0)
    188 			return (0);
    189 	}
    190 	*cnt = nfiles;
    191 	return (kd->argspc);
    192 }
    193