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
29 * @(#)kvm_file.c 8.1 (Berkeley) 6/4/93
30 * $FreeBSD: src/lib/libkvm/kvm_file.c,v 1.11 2000/02/18 16:39:00 peter Exp $
34 * File list interface for kvm. pstat, fstat and netstat are
35 * users of this code, so we've factored it out into a separate module.
36 * Thus, we keep this grunge out of the other kvm applications (i.e.,
37 * most other applications are interested only in open/close/read/nlist).
40 #include <sys/user.h> /* MUST BE FIRST */
41 #include <sys/param.h>
47 #include <sys/ioctl.h>
52 #include <vm/vm_param.h>
53 #include <vm/swap_pager.h>
55 #include <sys/sysctl.h>
61 #include "kvm_private.h"
63 #define KREAD(kd, addr, obj) \
64 (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
67 * Get file structures.
70 kvm_deadfiles(kvm_t
*kd
, int op
, int arg
, long filehead_o
, int nfiles
)
72 int buflen
= kd
->arglen
, n
= 0;
74 char *where
= kd
->argspc
;
75 struct filelist filehead
;
78 * first copyout filehead
80 if (buflen
> sizeof (filehead
)) {
81 if (KREAD(kd
, filehead_o
, &filehead
)) {
82 _kvm_err(kd
, kd
->program
, "can't read filehead");
85 buflen
-= sizeof (filehead
);
86 where
+= sizeof (filehead
);
87 *(struct filelist
*)kd
->argspc
= filehead
;
90 * followed by an array of file structures
92 for (fp
= filehead
.lh_first
; fp
!= NULL
; fp
= fp
->f_list
.le_next
) {
93 if (buflen
> sizeof (struct file
)) {
94 if (KREAD(kd
, (long)fp
, ((struct file
*)where
))) {
95 _kvm_err(kd
, kd
->program
, "can't read kfp");
98 buflen
-= sizeof (struct file
);
99 fp
= (struct file
*)where
;
100 where
+= sizeof (struct file
);
105 _kvm_err(kd
, kd
->program
, "inconsistent nfiles");
112 kvm_getfiles(kvm_t
*kd
, int op
, int arg
, int *cnt
)
114 int mib
[2], st
, nfiles
;
116 struct file
*fp
, *fplim
;
117 struct filelist filehead
;
119 if (kvm_ishost(kd
)) {
123 st
= sysctl(mib
, 2, NULL
, &size
, NULL
, 0);
125 _kvm_syserr(kd
, kd
->program
, "kvm_getfiles");
129 kd
->argspc
= (char *)_kvm_malloc(kd
, size
);
130 else if (kd
->arglen
< size
)
131 kd
->argspc
= (char *)_kvm_realloc(kd
, kd
->argspc
, size
);
135 st
= sysctl(mib
, 2, kd
->argspc
, &size
, NULL
, 0);
136 if (st
== -1 || size
< sizeof(filehead
)) {
137 _kvm_syserr(kd
, kd
->program
, "kvm_getfiles");
140 filehead
= *(struct filelist
*)kd
->argspc
;
141 fp
= (struct file
*)(kd
->argspc
+ sizeof (filehead
));
142 fplim
= (struct file
*)(kd
->argspc
+ size
);
143 for (nfiles
= 0; filehead
.lh_first
&& (fp
< fplim
); nfiles
++, fp
++)
144 filehead
.lh_first
= fp
->f_list
.le_next
;
146 struct nlist nl
[3], *p
;
148 nl
[0].n_name
= "_filehead";
149 nl
[1].n_name
= "_nfiles";
152 if (kvm_nlist(kd
, nl
) != 0) {
153 for (p
= nl
; p
->n_type
!= 0; ++p
)
155 _kvm_err(kd
, kd
->program
,
156 "%s: no such symbol", p
->n_name
);
159 if (KREAD(kd
, nl
[0].n_value
, &nfiles
)) {
160 _kvm_err(kd
, kd
->program
, "can't read nfiles");
163 size
= sizeof(filehead
) + (nfiles
+ 10) * sizeof(struct file
);
165 kd
->argspc
= (char *)_kvm_malloc(kd
, size
);
166 else if (kd
->arglen
< size
)
167 kd
->argspc
= (char *)_kvm_realloc(kd
, kd
->argspc
, size
);
171 nfiles
= kvm_deadfiles(kd
, op
, arg
, nl
[1].n_value
, nfiles
);