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