]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkvm/kvm_file.c
This commit was generated by cvs2svn to compensate for changes in r164146,
[FreeBSD/FreeBSD.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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)kvm_file.c  8.1 (Berkeley) 6/4/93";
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
64 #include <sys/sysctl.h>
65
66 #include <limits.h>
67 #include <ndbm.h>
68 #include <paths.h>
69
70 #include "kvm_private.h"
71
72 #define KREAD(kd, addr, obj) \
73         (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
74
75 /*
76  * Get file structures.
77  */
78 static int
79 kvm_deadfiles(kd, op, arg, filehead_o, nfiles)
80         kvm_t *kd;
81         int op, arg, nfiles;
82         long filehead_o;
83 {
84         int buflen = kd->arglen, n = 0;
85         struct file *fp;
86         char *where = kd->argspc;
87         struct filelist filehead;
88
89         /*
90          * first copyout filehead
91          */
92         if (buflen > sizeof (filehead)) {
93                 if (KREAD(kd, filehead_o, &filehead)) {
94                         _kvm_err(kd, kd->program, "can't read filehead");
95                         return (0);
96                 }
97                 buflen -= sizeof (filehead);
98                 where += sizeof (filehead);
99                 *(struct filelist *)kd->argspc = filehead;
100         }
101         /*
102          * followed by an array of file structures
103          */
104         LIST_FOREACH(fp, &filehead, f_list) {
105                 if (buflen > sizeof (struct file)) {
106                         if (KREAD(kd, (long)fp, ((struct file *)where))) {
107                                 _kvm_err(kd, kd->program, "can't read kfp");
108                                 return (0);
109                         }
110                         buflen -= sizeof (struct file);
111                         fp = (struct file *)where;
112                         where += sizeof (struct file);
113                         n++;
114                 }
115         }
116         if (n != nfiles) {
117                 _kvm_err(kd, kd->program, "inconsistant nfiles");
118                 return (0);
119         }
120         return (nfiles);
121 }
122
123 char *
124 kvm_getfiles(kd, op, arg, cnt)
125         kvm_t *kd;
126         int op, arg;
127         int *cnt;
128 {
129         int mib[2], st, nfiles;
130         size_t size;
131         struct file *fp, *fplim;
132         struct filelist filehead;
133
134         if (ISALIVE(kd)) {
135                 size = 0;
136                 mib[0] = CTL_KERN;
137                 mib[1] = KERN_FILE;
138                 st = sysctl(mib, 2, NULL, &size, NULL, 0);
139                 if (st == -1) {
140                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
141                         return (0);
142                 }
143                 if (kd->argspc == 0)
144                         kd->argspc = (char *)_kvm_malloc(kd, size);
145                 else if (kd->arglen < size)
146                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
147                 if (kd->argspc == 0)
148                         return (0);
149                 kd->arglen = size;
150                 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
151                 if (st == -1 || size < sizeof(filehead)) {
152                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
153                         return (0);
154                 }
155                 filehead = *(struct filelist *)kd->argspc;
156                 fp = (struct file *)(kd->argspc + sizeof (filehead));
157                 fplim = (struct file *)(kd->argspc + size);
158                 for (nfiles = 0; LIST_FIRST(&filehead) && (fp < fplim); nfiles++, fp++)
159                         LIST_FIRST(&filehead) = LIST_NEXT(fp, f_list);
160         } else {
161                 struct nlist nl[3], *p;
162
163                 nl[0].n_name = "_filehead";
164                 nl[1].n_name = "_nfiles";
165                 nl[2].n_name = 0;
166
167                 if (kvm_nlist(kd, nl) != 0) {
168                         for (p = nl; p->n_type != 0; ++p)
169                                 ;
170                         _kvm_err(kd, kd->program,
171                                  "%s: no such symbol", p->n_name);
172                         return (0);
173                 }
174                 if (KREAD(kd, nl[0].n_value, &nfiles)) {
175                         _kvm_err(kd, kd->program, "can't read nfiles");
176                         return (0);
177                 }
178                 size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
179                 if (kd->argspc == 0)
180                         kd->argspc = (char *)_kvm_malloc(kd, size);
181                 else if (kd->arglen < size)
182                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
183                 if (kd->argspc == 0)
184                         return (0);
185                 kd->arglen = size;
186                 nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
187                 if (nfiles == 0)
188                         return (0);
189         }
190         *cnt = nfiles;
191         return (kd->argspc);
192 }