xref: /original-bsd/lib/libkvm/kvm_file.c (revision 27393bdf)
1 /*-
2  * Copyright (c) 1989, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)kvm_file.c	8.2 (Berkeley) 08/20/94";
10 #endif /* LIBC_SCCS and not lint */
11 
12 /*
13  * File list interface for kvm.  pstat, fstat and netstat are
14  * users of this code, so we've factored it out into a separate module.
15  * Thus, we keep this grunge out of the other kvm applications (i.e.,
16  * most other applications are interested only in open/close/read/nlist).
17  */
18 
19 #include <sys/param.h>
20 #include <sys/user.h>
21 #include <sys/proc.h>
22 #include <sys/exec.h>
23 #define KERNEL
24 #include <sys/file.h>
25 #undef KERNEL
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <sys/tty.h>
29 #include <nlist.h>
30 #include <kvm.h>
31 
32 #include <vm/vm.h>
33 #include <vm/vm_param.h>
34 #include <vm/swap_pager.h>
35 
36 #include <sys/sysctl.h>
37 
38 #include <limits.h>
39 #include <ndbm.h>
40 #include <paths.h>
41 
42 #include "kvm_private.h"
43 
44 #define KREAD(kd, addr, obj) \
45 	(kvm_read(kd, addr, obj, sizeof(*obj)) != sizeof(*obj))
46 
47 /*
48  * Get file structures.
49  */
50 static
51 kvm_deadfiles(kd, op, arg, filehead_o, nfiles)
52 	kvm_t *kd;
53 	int op, arg, nfiles;
54 	long filehead_o;
55 {
56 	int buflen = kd->arglen, needed = buflen, error, n = 0;
57 	struct file *fp, file;
58 	struct filelist filehead;
59 	register char *where = kd->argspc;
60 	char *start = where;
61 
62 	/*
63 	 * first copyout filehead
64 	 */
65 	if (buflen > sizeof (filehead)) {
66 		if (KREAD(kd, filehead_o, &filehead)) {
67 			_kvm_err(kd, kd->program, "can't read filehead");
68 			return (0);
69 		}
70 		buflen -= sizeof (filehead);
71 		where += sizeof (filehead);
72 		*(struct filelist *)kd->argspc = filehead;
73 	}
74 	/*
75 	 * followed by an array of file structures
76 	 */
77 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
78 		if (buflen > sizeof (struct file)) {
79 			if (KREAD(kd, (long)fp, ((struct file *)where))) {
80 				_kvm_err(kd, kd->program, "can't read kfp");
81 				return (0);
82 			}
83 			buflen -= sizeof (struct file);
84 			fp = (struct file *)where;
85 			where += sizeof (struct file);
86 			n++;
87 		}
88 	}
89 	if (n != nfiles) {
90 		_kvm_err(kd, kd->program, "inconsistant nfiles");
91 		return (0);
92 	}
93 	return (nfiles);
94 }
95 
96 char *
97 kvm_getfiles(kd, op, arg, cnt)
98 	kvm_t *kd;
99 	int op, arg;
100 	int *cnt;
101 {
102 	int mib[2], size, st, nfiles;
103 	struct file *fp, *fplim;
104 	struct filelist filehead;
105 
106 	if (ISALIVE(kd)) {
107 		size = 0;
108 		mib[0] = CTL_KERN;
109 		mib[1] = KERN_FILE;
110 		st = sysctl(mib, 2, NULL, &size, NULL, 0);
111 		if (st == -1) {
112 			_kvm_syserr(kd, kd->program, "kvm_getprocs");
113 			return (0);
114 		}
115 		if (kd->argspc == 0)
116 			kd->argspc = (char *)_kvm_malloc(kd, size);
117 		else if (kd->arglen < size)
118 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
119 		if (kd->argspc == 0)
120 			return (0);
121 		kd->arglen = size;
122 		st = sysctl(mib, 2, kd->argspc, &size, NULL, 0);
123 		if (st == -1 || size < sizeof(filehead)) {
124 			_kvm_syserr(kd, kd->program, "kvm_getfiles");
125 			return (0);
126 		}
127 		filehead = *(struct filelist *)kd->argspc;
128 		fp = (struct file *)(kd->argspc + sizeof (filehead));
129 		fplim = (struct file *)(kd->argspc + size);
130 		for (nfiles = 0; filehead.lh_first && (fp < fplim); nfiles++, fp++)
131 			filehead.lh_first = fp->f_list.le_next;
132 	} else {
133 		struct nlist nl[3], *p;
134 
135 		nl[0].n_name = "_filehead";
136 		nl[1].n_name = "_nfiles";
137 		nl[2].n_name = 0;
138 
139 		if (kvm_nlist(kd, nl) != 0) {
140 			for (p = nl; p->n_type != 0; ++p)
141 				;
142 			_kvm_err(kd, kd->program,
143 				 "%s: no such symbol", p->n_name);
144 			return (0);
145 		}
146 		if (KREAD(kd, nl[0].n_value, &nfiles)) {
147 			_kvm_err(kd, kd->program, "can't read nfiles");
148 			return (0);
149 		}
150 		size = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
151 		if (kd->argspc == 0)
152 			kd->argspc = (char *)_kvm_malloc(kd, size);
153 		else if (kd->arglen < size)
154 			kd->argspc = (char *)_kvm_realloc(kd, kd->argspc, size);
155 		if (kd->argspc == 0)
156 			return (0);
157 		kd->arglen = size;
158 		nfiles = kvm_deadfiles(kd, op, arg, nl[1].n_value, nfiles);
159 		if (nfiles == 0)
160 			return (0);
161 	}
162 	*cnt = nfiles;
163 	return (kd->argspc);
164 }
165