1 /* $NetBSD: kvm_file.c,v 1.29 2014/02/19 20:21:22 dsl Exp $ */
4 * Copyright (c) 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)kvm_file.c 8.1 (Berkeley) 6/4/93";
37 __RCSID("$NetBSD: kvm_file.c,v 1.29 2014/02/19 20:21:22 dsl Exp $");
39 #endif /* LIBC_SCCS and not lint */
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).
49 #include <sys/types.h>
51 #include <sys/param.h>
59 #include <sys/ioctl.h>
63 #include <uvm/uvm_extern.h>
65 #include <sys/sysctl.h>
72 #include "kvm_private.h"
75 kvm_deadfiles(kvm_t
*, int, int, long, int);
78 * Get file structures.
82 kvm_deadfiles(kvm_t
*kd
, int op
, int arg
, long ofhead
, int numfiles
)
84 size_t buflen
= kd
->argspc_len
, n
= 0;
86 struct filelist fhead
;
87 char *where
= kd
->argspc
;
90 * first copyout filehead
92 if (buflen
< sizeof(fhead
) ||
93 KREAD(kd
, (u_long
)ofhead
, &fhead
)) {
94 _kvm_err(kd
, kd
->program
, "can't read filehead");
97 buflen
-= sizeof(fhead
);
98 where
+= sizeof(fhead
);
99 (void)memcpy(kd
->argspc
, &fhead
, sizeof(fhead
));
102 * followed by an array of file structures
104 for (fp
= fhead
.lh_first
; fp
!= 0; fp
= fp
->f_list
.le_next
) {
105 if (buflen
> sizeof(struct file
)) {
106 if (KREAD(kd
, (u_long
)fp
,
107 ((struct file
*)(void *)where
))) {
108 _kvm_err(kd
, kd
->program
, "can't read kfp");
111 buflen
-= sizeof(struct file
);
112 fp
= (struct file
*)(void *)where
;
113 where
+= sizeof(struct file
);
118 _kvm_err(kd
, kd
->program
, "inconsistent nfiles");
125 kvm_getfiles(kvm_t
*kd
, int op
, int arg
, int *cnt
)
130 struct file
*fp
, *fplim
;
131 struct filelist fhead
;
137 st
= sysctl(mib
, 2, NULL
, &size
, NULL
, 0);
139 _kvm_syserr(kd
, kd
->program
, "kvm_getprocs");
142 KVM_ALLOC(kd
, argspc
, size
);
143 st
= sysctl(mib
, 2, kd
->argspc
, &size
, NULL
, 0);
144 if (st
== -1 || size
< sizeof(fhead
)) {
145 _kvm_syserr(kd
, kd
->program
, "kvm_getfiles");
148 (void)memcpy(&fhead
, kd
->argspc
, sizeof(fhead
));
149 fp
= (struct file
*)(void *)(kd
->argspc
+ sizeof(fhead
));
150 fplim
= (struct file
*)(void *)(kd
->argspc
+ size
);
151 for (numfiles
= 0; fhead
.lh_first
&& (fp
< fplim
);
153 fhead
.lh_first
= fp
->f_list
.le_next
;
155 struct nlist nl
[3], *p
;
157 nl
[0].n_name
= "_nfiles";
158 nl
[1].n_name
= "_filehead";
161 if (kvm_nlist(kd
, nl
) != 0) {
162 for (p
= nl
; p
->n_type
!= 0; ++p
)
164 _kvm_err(kd
, kd
->program
,
165 "%s: no such symbol", p
->n_name
);
168 if (KREAD(kd
, nl
[0].n_value
, &numfiles
)) {
169 _kvm_err(kd
, kd
->program
, "can't read numfiles");
172 size
= sizeof(fhead
) + (numfiles
+ 10) * sizeof(struct file
);
173 KVM_ALLOC(kd
, argspc
, size
);
174 numfiles
= kvm_deadfiles(kd
, op
, arg
, (long)nl
[1].n_value
,