2 * Copyright (c) 1989, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93";
37 #endif /* LIBC_SCCS and not lint */
40 * File list interface for kvm. pstat, fstat and netstat are
41 * users of this code, so we've factored it out into a separate module.
42 * Thus, we keep this grunge out of the other kvm applications (i.e.,
43 * most other applications are interested only in open/close/read/nlist).
46 #include <sys/param.h>
53 #include <sys/ioctl.h>
58 #include <vm/vm_param.h>
60 #include <sys/sysctl.h>
66 #include "kvm_private.h"
68 #define KREAD(kd, addr, obj) \
69 (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
71 #define KREADN(kd, addr, obj, cnt) \
72 (kvm_read(kd, addr, obj, (cnt)) != (cnt))
75 * Get file structures.
78 kvm_deadfiles(kd, op, arg, allproc_o, nprocs)
84 struct filedesc filed;
85 int buflen = kd->arglen, ocnt = 0, n = 0, once = 0, i;
89 char *where = kd->argspc;
91 if (buflen < sizeof (struct file *) + sizeof (struct file))
93 if (KREAD(kd, allproc_o, &p)) {
94 _kvm_err(kd, kd->program, "cannot read allproc");
97 for (; p != NULL; p = LIST_NEXT(&proc, p_list)) {
98 if (KREAD(kd, (u_long)p, &proc)) {
99 _kvm_err(kd, kd->program, "can't read proc at %x", p);
102 if (proc.p_state == PRS_NEW)
104 if (proc.p_fd == NULL)
106 if (KREAD(kd, (u_long)p->p_fd, &filed)) {
107 _kvm_err(kd, kd->program, "can't read filedesc at %x",
111 if (filed.fd_lastfile + 1 > ocnt) {
112 ocnt = filed.fd_lastfile + 1;
114 ofiles = (struct file **)_kvm_malloc(kd,
115 ocnt * sizeof(struct file *));
119 if (KREADN(kd, (u_long)filed.fd_ofiles, ofiles,
120 ocnt * sizeof(struct file *))) {
121 _kvm_err(kd, kd->program, "can't read ofiles at %x",
125 for (i = 0; i <= filed.fd_lastfile; i++) {
126 if ((fp = ofiles[i]) == NULL)
129 * copyout filehead (legacy)
132 *(struct file **)kd->argspc = fp;
133 *(struct file **)where = fp;
134 buflen -= sizeof (fp);
135 where += sizeof (fp);
138 if (buflen < sizeof (struct file))
140 if (KREAD(kd, (long)fp, ((struct file *)where))) {
141 _kvm_err(kd, kd->program, "can't read kfp");
144 buflen -= sizeof (struct file);
145 fp = (struct file *)where;
146 where += sizeof (struct file);
159 kvm_getfiles(kd, op, arg, cnt)
164 int mib[2], st, n, nfiles, nprocs;
167 _kvm_syserr(kd, kd->program, "kvm_getfiles has been broken for years");
173 st = sysctl(mib, 2, NULL, &size, NULL, 0);
175 _kvm_syserr(kd, kd->program, "kvm_getfiles");
179 kd->argspc = (char *)_kvm_malloc(kd, size);
180 else if (kd->arglen < size)
181 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
185 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
187 _kvm_syserr(kd, kd->program, "kvm_getfiles");
190 nfiles = size / sizeof(struct xfile);
192 struct nlist nl[4], *p;
194 nl[0].n_name = "_allproc";
195 nl[1].n_name = "_nprocs";
196 nl[2].n_name = "_nfiles";
199 if (kvm_nlist(kd, nl) != 0) {
200 for (p = nl; p->n_type != 0; ++p)
202 _kvm_err(kd, kd->program,
203 "%s: no such symbol", p->n_name);
206 if (KREAD(kd, nl[1].n_value, &nprocs)) {
207 _kvm_err(kd, kd->program, "can't read nprocs");
210 if (KREAD(kd, nl[2].n_value, &nfiles)) {
211 _kvm_err(kd, kd->program, "can't read nfiles");
214 size = sizeof(void *) + (nfiles + 10) * sizeof(struct file);
216 kd->argspc = (char *)_kvm_malloc(kd, size);
217 else if (kd->arglen < size)
218 kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
222 n = kvm_deadfiles(kd, op, arg, nl[0].n_value, nprocs);
224 _kvm_err(kd, kd->program, "inconsistant nfiles");