Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libkvm / kvm_file.c
1 /*-
2  * Copyright (c) 1989, 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 #if 0
36 static char sccsid[] = "@(#)kvm_file.c  8.1 (Berkeley) 6/4/93";
37 #else
38 static const char rcsid[] =
39  "$FreeBSD: src/lib/libkvm/kvm_file.c,v 1.11 2000/02/18 16:39:00 peter Exp $";
40 #endif
41 #endif /* LIBC_SCCS and not lint */
42
43 /*
44  * File list interface for kvm.  pstat, fstat and netstat are
45  * users of this code, so we've factored it out into a separate module.
46  * Thus, we keep this grunge out of the other kvm applications (i.e.,
47  * most other applications are interested only in open/close/read/nlist).
48  */
49
50 #include <sys/param.h>
51 #include <sys/user.h>
52 #include <sys/proc.h>
53 #define _KERNEL
54 #include <sys/file.h>
55 #undef _KERNEL
56 #include <sys/stat.h>
57 #include <sys/ioctl.h>
58 #include <nlist.h>
59 #include <kvm.h>
60
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/swap_pager.h>
64
65 #include <sys/sysctl.h>
66
67 #include <limits.h>
68 #include <ndbm.h>
69 #include <paths.h>
70
71 #include "kvm_private.h"
72
73 #define KREAD(kd, addr, obj) \
74         (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
75
76 /*
77  * Get file structures.
78  */
79 static int
80 kvm_deadfiles(kd, op, arg, filehead_o, nfiles)
81         kvm_t *kd;
82         int op, arg, nfiles;
83         long filehead_o;
84 {
85         int buflen = kd->arglen, n = 0;
86         struct file *fp;
87         register char *where = kd->argspc;
88         struct filelist filehead;
89
90         /*
91          * first copyout filehead
92          */
93         if (buflen > sizeof (filehead)) {
94                 if (KREAD(kd, filehead_o, &filehead)) {
95                         _kvm_err(kd, kd->program, "can't read filehead");
96                         return (0);
97                 }
98                 buflen -= sizeof (filehead);
99                 where += sizeof (filehead);
100                 *(struct filelist *)kd->argspc = filehead;
101         }
102         /*
103          * followed by an array of file structures
104          */
105         for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
106                 if (buflen > sizeof (struct file)) {
107                         if (KREAD(kd, (long)fp, ((struct file *)where))) {
108                                 _kvm_err(kd, kd->program, "can't read kfp");
109                                 return (0);
110                         }
111                         buflen -= sizeof (struct file);
112                         fp = (struct file *)where;
113                         where += sizeof (struct file);
114                         n++;
115                 }
116         }
117         if (n != nfiles) {
118                 _kvm_err(kd, kd->program, "inconsistant nfiles");
119                 return (0);
120         }
121         return (nfiles);
122 }
123
124 char *
125 kvm_getfiles(kd, op, arg, cnt)
126         kvm_t *kd;
127         int op, arg;
128         int *cnt;
129 {
130         int mib[2], st, nfiles;
131         size_t size;
132         struct file *fp, *fplim;
133         struct filelist filehead;
134
135         if (ISALIVE(kd)) {
136                 size = 0;
137                 mib[0] = CTL_KERN;
138                 mib[1] = KERN_FILE;
139                 st = sysctl(mib, 2, NULL, &size, NULL, 0);
140                 if (st == -1) {
141                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
142                         return (0);
143                 }
144                 if (kd->argspc == 0)
145                         kd->argspc = (char *)_kvm_malloc(kd, size);
146                 else if (kd->arglen < size)
147                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
148                 if (kd->argspc == 0)
149                         return (0);
150                 kd->arglen = size;
151                 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
152                 if (st == -1 || size < sizeof(filehead)) {
153                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
154                         return (0);
155                 }
156                 filehead = *(struct filelist *)kd->argspc;
157                 fp = (struct file *)(kd->argspc + sizeof (filehead));
158                 fplim = (struct file *)(kd->argspc + size);
159                 for (nfiles = 0; filehead.lh_first && (fp < fplim); nfiles++, fp++)
160                         filehead.lh_first = fp->f_list.le_next;
161         } else {
162                 struct nlist nl[3], *p;
163
164                 nl[0].n_name = "_filehead";
165                 nl[1].n_name = "_nfiles";
166                 nl[2].n_name = 0;
167
168                 if (kvm_nlist(kd, nl) != 0) {
169                         for (p = nl; p->n_type != 0; ++p)
170                                 ;
171                         _kvm_err(kd, kd->program,
172                                  "%s: no such symbol", p->n_name);
173                         return (0);
174                 }
175                 if (KREAD(kd, nl[0].n_value, &nfiles)) {
176                         _kvm_err(kd, kd->program, "can't read nfiles");
177                         return (0);
178                 }
179                 size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
180                 if (kd->argspc == 0)
181                         kd->argspc = (char *)_kvm_malloc(kd, size);
182                 else if (kd->arglen < size)
183                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
184                 if (kd->argspc == 0)
185                         return (0);
186                 kd->arglen = size;
187                 nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
188                 if (nfiles == 0)
189                         return (0);
190         }
191         *cnt = nfiles;
192         return (kd->argspc);
193 }