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