]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libkvm/kvm_file.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 _WANT_FILE      /* make file.h give us 'struct file' */
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <nlist.h>
54 #include <kvm.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58
59 #include <sys/sysctl.h>
60
61 #include <limits.h>
62 #include <ndbm.h>
63 #include <paths.h>
64 #include <stdlib.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 #define KREADN(kd, addr, obj, cnt) \
72         (kvm_read(kd, addr, obj, (cnt)) != (ssize_t)(cnt))
73
74 /*
75  * Get file structures.
76  */
77 static int
78 kvm_deadfiles(kvm_t *kd, int op __unused, int arg __unused, long allproc_o,
79     int nprocs __unused)
80 {
81         struct proc proc;
82         struct filedesc filed;
83         int buflen = kd->arglen, ocnt = 0, n = 0, once = 0, i;
84         struct file **ofiles;
85         struct file *fp;
86         struct proc *p;
87         char *where = kd->argspc;
88
89         if (buflen < (int)(sizeof(struct file *) + sizeof(struct file)))
90                 return (0);
91         if (KREAD(kd, allproc_o, &p)) {
92                 _kvm_err(kd, kd->program, "cannot read allproc");
93                 return (0);
94         }
95         for (; p != NULL; p = LIST_NEXT(&proc, p_list)) {
96                 if (KREAD(kd, (u_long)p, &proc)) {
97                         _kvm_err(kd, kd->program, "can't read proc at %p", p);
98                         goto fail;
99                 }
100                 if (proc.p_state == PRS_NEW)
101                         continue;
102                 if (proc.p_fd == NULL)
103                         continue;
104                 if (KREAD(kd, (u_long)p->p_fd, &filed)) {
105                         _kvm_err(kd, kd->program, "can't read filedesc at %p",
106                             p->p_fd);
107                         goto fail;
108                 }
109                 if (filed.fd_lastfile + 1 > ocnt) {
110                         ocnt = filed.fd_lastfile + 1;
111                         free(ofiles);
112                         ofiles = (struct file **)_kvm_malloc(kd,
113                                 ocnt * sizeof(struct file *));
114                         if (ofiles == 0)
115                                 return (0);
116                 }
117                 if (KREADN(kd, (u_long)filed.fd_ofiles, ofiles,
118                     ocnt * sizeof(struct file *))) {
119                         _kvm_err(kd, kd->program, "can't read ofiles at %p",
120                             filed.fd_ofiles);
121                         return (0);
122                 }
123                 for (i = 0; i <= filed.fd_lastfile; i++) {
124                         if ((fp = ofiles[i]) == NULL)
125                                 continue;
126                         /*
127                          * copyout filehead (legacy)
128                          */
129                         if (!once) {
130                                 *(struct file **)kd->argspc = fp;
131                                 *(struct file **)where = fp;
132                                 buflen -= sizeof (fp);
133                                 where += sizeof (fp);
134                                 once = 1;
135                         }
136                         if (buflen < (int)sizeof(struct file))
137                                 goto fail;
138                         if (KREAD(kd, (long)fp, ((struct file *)where))) {
139                                 _kvm_err(kd, kd->program, "can't read kfp");
140                                 goto fail;
141                         }
142                         buflen -= sizeof (struct file);
143                         fp = (struct file *)where;
144                         where += sizeof (struct file);
145                         n++;
146                 }
147         }
148         free(ofiles);
149         return (n);
150 fail:
151         free(ofiles);
152         return (0);
153         
154 }
155
156 char *
157 kvm_getfiles(kvm_t *kd, int op, int arg, int *cnt)
158 {
159         int mib[2], st, n, nfiles, nprocs;
160         size_t size;
161
162         _kvm_syserr(kd, kd->program, "kvm_getfiles has been broken for years");
163         return (0);
164         if (ISALIVE(kd)) {
165                 size = 0;
166                 mib[0] = CTL_KERN;
167                 mib[1] = KERN_FILE;
168                 st = sysctl(mib, 2, NULL, &size, NULL, 0);
169                 if (st == -1) {
170                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
171                         return (0);
172                 }
173                 if (kd->argspc == 0)
174                         kd->argspc = (char *)_kvm_malloc(kd, size);
175                 else if (kd->arglen < (int)size)
176                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
177                 if (kd->argspc == 0)
178                         return (0);
179                 kd->arglen = size;
180                 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
181                 if (st != 0) {
182                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
183                         return (0);
184                 }
185                 nfiles = size / sizeof(struct xfile);
186         } else {
187                 struct nlist nl[4], *p;
188
189                 nl[0].n_name = "_allproc";
190                 nl[1].n_name = "_nprocs";
191                 nl[2].n_name = "_nfiles";
192                 nl[3].n_name = 0;
193
194                 if (kvm_nlist(kd, nl) != 0) {
195                         for (p = nl; p->n_type != 0; ++p)
196                                 ;
197                         _kvm_err(kd, kd->program,
198                                  "%s: no such symbol", p->n_name);
199                         return (0);
200                 }
201                 if (KREAD(kd, nl[1].n_value, &nprocs)) {
202                         _kvm_err(kd, kd->program, "can't read nprocs");
203                         return (0);
204                 }
205                 if (KREAD(kd, nl[2].n_value, &nfiles)) {
206                         _kvm_err(kd, kd->program, "can't read nfiles");
207                         return (0);
208                 }
209                 size = sizeof(void *) + (nfiles + 10) * sizeof(struct file);
210                 if (kd->argspc == 0)
211                         kd->argspc = (char *)_kvm_malloc(kd, size);
212                 else if (kd->arglen < (int)size)
213                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
214                 if (kd->argspc == 0)
215                         return (0);
216                 kd->arglen = size;
217                 n = kvm_deadfiles(kd, op, arg, nl[0].n_value, nprocs);
218                 if (n != nfiles) {
219                         _kvm_err(kd, kd->program, "inconsistant nfiles");
220                         return (0);
221                 }
222                 nfiles = n;
223         }
224         *cnt = nfiles;
225         return (kd->argspc);
226 }