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