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