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