kvm_file.c revision 1.25 1 /* $NetBSD: kvm_file.c,v 1.25 2008/02/27 15:46:23 ad 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.25 2008/02/27 15:46:23 ad 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 #define _KERNEL
49 #include <sys/types.h>
50 #undef _KERNEL
51 #include <sys/param.h>
52 #include <sys/user.h>
53 #include <sys/lwp.h>
54 #include <sys/proc.h>
55 #include <sys/exec.h>
56 #define _KERNEL
57 #include <sys/file.h>
58 #undef _KERNEL
59 #include <sys/stat.h>
60 #include <sys/ioctl.h>
61 #include <sys/tty.h>
62 #include <nlist.h>
63 #include <kvm.h>
64
65 #include <uvm/uvm_extern.h>
66
67 #include <sys/sysctl.h>
68
69 #include <limits.h>
70 #include <ndbm.h>
71 #include <paths.h>
72 #include <string.h>
73
74 #include "kvm_private.h"
75
76 #define KREAD(kd, addr, obj) \
77 (kvm_read(kd, (u_long) addr, obj, sizeof(*obj)) != sizeof(*obj))
78
79 static int
80 kvm_deadfiles __P((kvm_t *, int, int, long, int));
81
82 /*
83 * Get file structures.
84 */
85 /*ARGSUSED*/
86 static int
87 kvm_deadfiles(kd, op, arg, ofhead, numfiles)
88 kvm_t *kd;
89 int op, arg, numfiles;
90 long ofhead;
91 {
92 size_t buflen = kd->argspc_len, n = 0;
93 struct file *fp;
94 struct filelist fhead;
95 char *where = kd->argspc;
96
97 /*
98 * first copyout filehead
99 */
100 if (buflen < sizeof(fhead) ||
101 KREAD(kd, ofhead, &fhead)) {
102 _kvm_err(kd, kd->program, "can't read filehead");
103 return (0);
104 }
105 buflen -= sizeof(fhead);
106 where += sizeof(fhead);
107 (void)memcpy(kd->argspc, &fhead, sizeof(fhead));
108
109 /*
110 * followed by an array of file structures
111 */
112 for (fp = fhead.lh_first; fp != 0; fp = fp->f_list.le_next) {
113 if (buflen > sizeof(struct file)) {
114 if (KREAD(kd, (long)fp, ((struct file *)(void *)where))) {
115 _kvm_err(kd, kd->program, "can't read kfp");
116 return (0);
117 }
118 buflen -= sizeof(struct file);
119 fp = (struct file *)(void *)where;
120 where += sizeof(struct file);
121 n++;
122 }
123 }
124 if (n != numfiles) {
125 _kvm_err(kd, kd->program, "inconsistent nfiles");
126 return (0);
127 }
128 return (numfiles);
129 }
130
131 char *
132 kvm_getfiles(kd, op, arg, cnt)
133 kvm_t *kd;
134 int op, arg;
135 int *cnt;
136 {
137 size_t size;
138 int mib[2], st;
139 int numfiles;
140 struct file *fp, *fplim;
141 struct filelist fhead;
142
143 if (ISSYSCTL(kd)) {
144 size = 0;
145 mib[0] = CTL_KERN;
146 mib[1] = KERN_FILE;
147 st = sysctl(mib, 2, NULL, &size, NULL, 0);
148 if (st == -1) {
149 _kvm_syserr(kd, kd->program, "kvm_getprocs");
150 return (0);
151 }
152 KVM_ALLOC(kd, argspc, size);
153 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
154 if (st == -1 || size < sizeof(fhead)) {
155 _kvm_syserr(kd, kd->program, "kvm_getfiles");
156 return (0);
157 }
158 (void)memcpy(&fhead, kd->argspc, sizeof(fhead));
159 fp = (struct file *)(void *)(kd->argspc + sizeof(fhead));
160 fplim = (struct file *)(void *)(kd->argspc + size);
161 for (numfiles = 0; fhead.lh_first && (fp < fplim);
162 numfiles++, fp++)
163 fhead.lh_first = fp->f_list.le_next;
164 } else {
165 struct nlist nl[3], *p;
166
167 nl[0].n_name = "_nfiles";
168 nl[1].n_name = "_filehead";
169 nl[2].n_name = 0;
170
171 if (kvm_nlist(kd, nl) != 0) {
172 for (p = nl; p->n_type != 0; ++p)
173 ;
174 _kvm_err(kd, kd->program,
175 "%s: no such symbol", p->n_name);
176 return (0);
177 }
178 if (KREAD(kd, nl[0].n_value, &numfiles)) {
179 _kvm_err(kd, kd->program, "can't read numfiles");
180 return (0);
181 }
182 size = sizeof(fhead) + (numfiles + 10) * sizeof(struct file);
183 KVM_ALLOC(kd, argspc, size);
184 numfiles = kvm_deadfiles(kd, op, arg, (long)nl[1].n_value,
185 numfiles);
186 if (numfiles == 0)
187 return (0);
188 }
189 *cnt = numfiles;
190 return (kd->argspc);
191 }
192