Merge branch 'vendor/GCC47'
[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  * @(#)kvm_file.c       8.1 (Berkeley) 6/4/93
34  * $FreeBSD: src/lib/libkvm/kvm_file.c,v 1.11 2000/02/18 16:39:00 peter Exp $
35  */
36
37 /*
38  * File list interface for kvm.  pstat, fstat and netstat are
39  * users of this code, so we've factored it out into a separate module.
40  * Thus, we keep this grunge out of the other kvm applications (i.e.,
41  * most other applications are interested only in open/close/read/nlist).
42  */
43
44 #include <sys/user.h>   /* MUST BE FIRST */
45 #include <sys/param.h>
46 #include <sys/proc.h>
47 #define _KERNEL
48 #include <sys/file.h>
49 #undef _KERNEL
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <nlist.h>
53 #include <kvm.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_param.h>
57 #include <vm/swap_pager.h>
58
59 #include <sys/sysctl.h>
60
61 #include <limits.h>
62 #include <ndbm.h>
63 #include <paths.h>
64
65 #include "kvm_private.h"
66
67 #define KREAD(kd, addr, obj) \
68         (kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
69
70 /*
71  * Get file structures.
72  */
73 static int
74 kvm_deadfiles(kvm_t *kd, int op, int arg, long filehead_o, int nfiles)
75 {
76         int buflen = kd->arglen, n = 0;
77         struct file *fp;
78         char *where = kd->argspc;
79         struct filelist filehead;
80
81         /*
82          * first copyout filehead
83          */
84         if (buflen > sizeof (filehead)) {
85                 if (KREAD(kd, filehead_o, &filehead)) {
86                         _kvm_err(kd, kd->program, "can't read filehead");
87                         return (0);
88                 }
89                 buflen -= sizeof (filehead);
90                 where += sizeof (filehead);
91                 *(struct filelist *)kd->argspc = filehead;
92         }
93         /*
94          * followed by an array of file structures
95          */
96         for (fp = filehead.lh_first; fp != NULL; fp = fp->f_list.le_next) {
97                 if (buflen > sizeof (struct file)) {
98                         if (KREAD(kd, (long)fp, ((struct file *)where))) {
99                                 _kvm_err(kd, kd->program, "can't read kfp");
100                                 return (0);
101                         }
102                         buflen -= sizeof (struct file);
103                         fp = (struct file *)where;
104                         where += sizeof (struct file);
105                         n++;
106                 }
107         }
108         if (n != nfiles) {
109                 _kvm_err(kd, kd->program, "inconsistent nfiles");
110                 return (0);
111         }
112         return (nfiles);
113 }
114
115 char *
116 kvm_getfiles(kvm_t *kd, int op, int arg, int *cnt)
117 {
118         int mib[2], st, nfiles;
119         size_t size;
120         struct file *fp, *fplim;
121         struct filelist filehead;
122
123         if (kvm_ishost(kd)) {
124                 size = 0;
125                 mib[0] = CTL_KERN;
126                 mib[1] = KERN_FILE;
127                 st = sysctl(mib, 2, NULL, &size, NULL, 0);
128                 if (st == -1) {
129                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
130                         return (0);
131                 }
132                 if (kd->argspc == 0)
133                         kd->argspc = (char *)_kvm_malloc(kd, size);
134                 else if (kd->arglen < size)
135                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
136                 if (kd->argspc == 0)
137                         return (0);
138                 kd->arglen = size;
139                 st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
140                 if (st == -1 || size < sizeof(filehead)) {
141                         _kvm_syserr(kd, kd->program, "kvm_getfiles");
142                         return (0);
143                 }
144                 filehead = *(struct filelist *)kd->argspc;
145                 fp = (struct file *)(kd->argspc + sizeof (filehead));
146                 fplim = (struct file *)(kd->argspc + size);
147                 for (nfiles = 0; filehead.lh_first && (fp < fplim); nfiles++, fp++)
148                         filehead.lh_first = fp->f_list.le_next;
149         } else {
150                 struct nlist nl[3], *p;
151
152                 nl[0].n_name = "_filehead";
153                 nl[1].n_name = "_nfiles";
154                 nl[2].n_name = 0;
155
156                 if (kvm_nlist(kd, nl) != 0) {
157                         for (p = nl; p->n_type != 0; ++p)
158                                 ;
159                         _kvm_err(kd, kd->program,
160                                  "%s: no such symbol", p->n_name);
161                         return (0);
162                 }
163                 if (KREAD(kd, nl[0].n_value, &nfiles)) {
164                         _kvm_err(kd, kd->program, "can't read nfiles");
165                         return (0);
166                 }
167                 size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
168                 if (kd->argspc == 0)
169                         kd->argspc = (char *)_kvm_malloc(kd, size);
170                 else if (kd->arglen < size)
171                         kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
172                 if (kd->argspc == 0)
173                         return (0);
174                 kd->arglen = size;
175                 nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
176                 if (nfiles == 0)
177                         return (0);
178         }
179         *cnt = nfiles;
180         return (kd->argspc);
181 }