1 /* $NetBSD: kvm_file.c,v 1.5 1996/03/18 22:33:18 thorpej 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 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93"; 39 #else 40 static char *rcsid = "$NetBSD: kvm_file.c,v 1.5 1996/03/18 22:33:18 thorpej Exp $"; 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 /* 45 * File list interface for kvm. pstat, fstat and netstat are 46 * users of this code, so we've factored it out into a separate module. 47 * Thus, we keep this grunge out of the other kvm applications (i.e., 48 * most other applications are interested only in open/close/read/nlist). 49 */ 50 51 #include <sys/param.h> 52 #include <sys/user.h> 53 #include <sys/proc.h> 54 #include <sys/exec.h> 55 #define _KERNEL 56 #include <sys/file.h> 57 #undef _KERNEL 58 #include <sys/stat.h> 59 #include <sys/ioctl.h> 60 #include <sys/tty.h> 61 #include <nlist.h> 62 #include <kvm.h> 63 64 #include <vm/vm.h> 65 #include <vm/vm_param.h> 66 #include <vm/swap_pager.h> 67 68 #include <sys/sysctl.h> 69 70 #include <limits.h> 71 #include <ndbm.h> 72 #include <paths.h> 73 74 #include "kvm_private.h" 75 76 #define KREAD(kd, addr, obj) \ 77 (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj)) 78 79 /* 80 * Get file structures. 81 */ 82 static 83 kvm_deadfiles(kd, op, arg, filehead_o, nfiles) 84 kvm_t *kd; 85 int op, arg, nfiles; 86 long filehead_o; 87 { 88 int buflen = kd->arglen, needed = buflen, error, n = 0; 89 struct file *fp, file; 90 struct filelist filehead; 91 register char *where = kd->argspc; 92 char *start = where; 93 94 /* 95 * first copyout filehead 96 */ 97 if (buflen > sizeof (filehead)) { 98 if (KREAD(kd, filehead_o, &filehead)) { 99 _kvm_err(kd, kd->program, "can't read filehead"); 100 return (0); 101 } 102 buflen -= sizeof (filehead); 103 where += sizeof (filehead); 104 *(struct filelist *)kd->argspc = filehead; 105 } 106 /* 107 * followed by an array of file structures 108 */ 109 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) { 110 if (buflen > sizeof (struct file)) { 111 if (KREAD(kd, (long)fp, ((struct file *)where))) { 112 _kvm_err(kd, kd->program, "can't read kfp"); 113 return (0); 114 } 115 buflen -= sizeof (struct file); 116 fp = (struct file *)where; 117 where += sizeof (struct file); 118 n++; 119 } 120 } 121 if (n != nfiles) { 122 _kvm_err(kd, kd->program, "inconsistant nfiles"); 123 return (0); 124 } 125 return (nfiles); 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, nfiles; 136 struct file *fp, *fplim; 137 struct filelist filehead; 138 139 if (ISALIVE(kd)) { 140 size = 0; 141 mib[0] = CTL_KERN; 142 mib[1] = KERN_FILE; 143 st = sysctl(mib, 2, NULL, &size, NULL, 0); 144 if (st == -1) { 145 _kvm_syserr(kd, kd->program, "kvm_getprocs"); 146 return (0); 147 } 148 if (kd->argspc == 0) 149 kd->argspc = (char *)_kvm_malloc(kd, size); 150 else if (kd->arglen < size) 151 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size); 152 if (kd->argspc == 0) 153 return (0); 154 kd->arglen = size; 155 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0); 156 if (st == -1 || size < sizeof(filehead)) { 157 _kvm_syserr(kd, kd->program, "kvm_getfiles"); 158 return (0); 159 } 160 filehead = *(struct filelist *)kd->argspc; 161 fp = (struct file *)(kd->argspc + sizeof (filehead)); 162 fplim = (struct file *)(kd->argspc + size); 163 for (nfiles = 0; filehead.lh_first && (fp < fplim); 164 nfiles++, fp++) 165 filehead.lh_first = fp->f_list.le_next; 166 } else { 167 struct nlist nl[3], *p; 168 169 nl[0].n_name = "_filehead"; 170 nl[1].n_name = "_nfiles"; 171 nl[2].n_name = 0; 172 173 if (kvm_nlist(kd, nl) != 0) { 174 for (p = nl; p->n_type != 0; ++p) 175 ; 176 _kvm_err(kd, kd->program, 177 "%s: no such symbol", p->n_name); 178 return (0); 179 } 180 if (KREAD(kd, nl[0].n_value, &nfiles)) { 181 _kvm_err(kd, kd->program, "can't read nfiles"); 182 return (0); 183 } 184 size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file); 185 if (kd->argspc == 0) 186 kd->argspc = (char *)_kvm_malloc(kd, size); 187 else if (kd->arglen < size) 188 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size); 189 if (kd->argspc == 0) 190 return (0); 191 kd->arglen = size; 192 nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles); 193 if (nfiles == 0) 194 return (0); 195 } 196 *cnt = nfiles; 197 return (kd->argspc); 198 } 199