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