xref: /original-bsd/usr.bin/systat/disks.c (revision e0c0d005)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char sccsid[] = "@(#)disks.c	5.7 (Berkeley) 02/04/91";
9 #endif not lint
10 
11 #include "systat.h"
12 #include <sys/buf.h>
13 #include <ctype.h>
14 #include <paths.h>
15 
16 static struct nlist nlst[] = {
17 #define	X_DK_NDRIVE	0
18 	{ "_dk_ndrive" },
19 #define	X_DK_WPMS	1
20 	{ "_dk_wpms" },
21 #ifdef vax
22 #define	X_MBDINIT	(X_DK_WPMS+1)
23 	{ "_mbdinit" },
24 #define	X_UBDINIT	(X_DK_WPMS+2)
25 	{ "_ubdinit" },
26 #endif
27 #ifdef sun
28 #define	X_MBDINIT	(X_DK_WPMS+1)
29 	{ "_mbdinit" },
30 #endif
31 #ifdef tahoe
32 #define	X_VBDINIT	(X_DK_WPMS+1)
33 	{ "_vbdinit" },
34 #endif
35 	{ "" },
36 };
37 
38 dkinit()
39 {
40 	register int i;
41 	register char *cp;
42 	static int once = 0;
43 	static char buf[1024];
44 
45 	if (once)
46 		return(1);
47 	nlist(_PATH_UNIX, nlst);
48 	if (nlst[X_DK_NDRIVE].n_value == 0) {
49 		error("dk_ndrive undefined in kernel");
50 		return(0);
51 	}
52 	dk_ndrive = getword(nlst[X_DK_NDRIVE].n_value);
53 	if (dk_ndrive <= 0) {
54 		error("dk_ndrive=%d according to %s", dk_ndrive, _PATH_UNIX);
55 		return(0);
56 	}
57 	dk_mspw = (float *)calloc(dk_ndrive, sizeof (float));
58 	lseek(kmem, nlst[X_DK_WPMS].n_value, L_SET);
59 	{
60 		long *wpms = (long *)calloc(dk_ndrive, sizeof(long));
61 		read(kmem, wpms, dk_ndrive * sizeof (long));
62 		for (i = 0; i < dk_ndrive; i++)
63 			*(dk_mspw + i) = (*(wpms + i) == 0)? 0.0:
64 			                 (float) 1.0 / *(wpms + i);
65 		free(wpms);
66 	}
67 	dr_name = (char **)calloc(dk_ndrive, sizeof (char *));
68 	dk_select = (int *)calloc(dk_ndrive, sizeof (int));
69 	for (cp = buf, i = 0; i < dk_ndrive; i++) {
70 		dr_name[i] = cp;
71 		sprintf(dr_name[i], "dk%d", i);
72 		cp += strlen(dr_name[i]) + 1;
73 		if (dk_mspw[i] != 0.0)
74 			dk_select[i] = 1;
75 	}
76 	if (!read_names()) {
77 		free(dr_name);
78 		free(dk_select);
79 		free(dk_mspw);
80 		return(0);
81 	}
82 	once = 1;
83 	return(1);
84 }
85 
86 dkcmd(cmd, args)
87 	char *cmd, *args;
88 {
89 	if (prefix(cmd, "display") || prefix(cmd, "add")) {
90 		dkselect(args, 1, dk_select);
91 		return (1);
92 	}
93 	if (prefix(cmd, "ignore") || prefix(cmd, "delete")) {
94 		dkselect(args, 0, dk_select);
95 		return (1);
96 	}
97 	if (prefix(cmd, "drives")) {
98 		register int i;
99 
100 		move(CMDLINE, 0); clrtoeol();
101 		for (i = 0; i < dk_ndrive; i++)
102 			if (dk_mspw[i] != 0.0)
103 				printw("%s ", dr_name[i]);
104 		return (1);
105 	}
106 	return (0);
107 }
108 
109 #define steal(where, var) \
110 	lseek(kmem, where, L_SET); read(kmem, &var, sizeof var);
111 
112 #ifdef vax
113 #include <vaxuba/ubavar.h>
114 #include <vaxmba/mbavar.h>
115 
116 read_names()
117 {
118 	struct mba_device mdev;
119 	struct mba_driver mdrv;
120 	short two_char;
121 	char *cp = (char *)&two_char;
122 	struct uba_device udev;
123 	struct uba_driver udrv;
124 	register struct mba_device *mp;
125 	register struct uba_device *up;
126 
127 	mp = (struct mba_device *)nlst[X_MBDINIT].n_value;
128 	up = (struct uba_device *)nlst[X_UBDINIT].n_value;
129 	if (mp == 0 && up == 0) {
130 		error("Disk init info not in namelist\n");
131 		return(0);
132 	}
133 	if (mp) for (;;) {
134 		steal(mp++, mdev);
135 		if (mdev.mi_driver == 0)
136 			break;
137 		if (mdev.mi_dk < 0 || mdev.mi_alive == 0)
138 			continue;
139 		steal(mdev.mi_driver, mdrv);
140 		steal(mdrv.md_dname, two_char);
141 		sprintf(dr_name[mdev.mi_dk], "%c%c%d",
142 		    cp[0], cp[1], mdev.mi_unit);
143 	}
144 	if (up) for (;;) {
145 		steal(up++, udev);
146 		if (udev.ui_driver == 0)
147 			break;
148 		if (udev.ui_dk < 0 || udev.ui_alive == 0)
149 			continue;
150 		steal(udev.ui_driver, udrv);
151 		steal(udrv.ud_dname, two_char);
152 		sprintf(dr_name[udev.ui_dk], "%c%c%d",
153 		    cp[0], cp[1], udev.ui_unit);
154 	}
155 	return(1);
156 }
157 #endif
158 
159 #ifdef sun
160 #include <sundev/mbvar.h>
161 
162 read_names()
163 {
164 	static int once = 0;
165 	struct mb_device mdev;
166 	struct mb_driver mdrv;
167 	short two_char;
168 	char *cp = (char *) &two_char;
169 	register struct mb_device *mp;
170 
171 	mp = (struct mb_device *)nlst[X_MBDINIT].n_value;
172 	if (mp == 0) {
173 		error("Disk init info not in namelist\n");
174 		return(0);
175 	}
176 	for (;;) {
177 		steal(mp++, mdev);
178 		if (mdev.md_driver == 0)
179 			break;
180 		if (mdev.md_dk < 0 || mdev.md_alive == 0)
181 			continue;
182 		steal(mdev.md_driver, mdrv);
183 		steal(mdrv.mdr_dname, two_char);
184 		sprintf(dr_name[mdev.md_dk], "%c%c%d",
185 		    cp[0], cp[1], mdev.md_unit);
186 	}
187 	return(1);
188 }
189 #endif
190 
191 #ifdef tahoe
192 #include <tahoe/vba/vbavar.h>
193 
194 /*
195  * Read the drive names out of kmem.
196  */
197 read_names()
198 {
199 	struct vba_device udev, *up;
200 	struct vba_driver udrv;
201 	short two_char;
202 	char *cp = (char *)&two_char;
203 
204 	up = (struct vba_device *) nlst[X_VBDINIT].n_value;
205 	if (up == 0) {
206 		fprintf(stderr, "vmstat: Disk init info not in namelist\n");
207 		exit(1);
208 	}
209 	for (;;) {
210 		steal(up++, udev);
211 		if (udev.ui_driver == 0)
212 			break;
213 		if (udev.ui_dk < 0 || udev.ui_alive == 0)
214 			continue;
215 		steal(udev.ui_driver, udrv);
216 		steal(udrv.ud_dname, two_char);
217 		sprintf(dr_name[udev.ui_dk], "%c%c%d",
218 		     cp[0], cp[1], udev.ui_unit);
219 	}
220 }
221 #endif
222 
223 dkselect(args, truefalse, selections)
224 	char *args;
225 	int truefalse, selections[];
226 {
227 	register char *cp;
228 	register int i;
229 	char *index();
230 
231 	cp = index(args, '\n');
232 	if (cp)
233 		*cp = '\0';
234 	for (;;) {
235 		for (cp = args; *cp && isspace(*cp); cp++)
236 			;
237 		args = cp;
238 		for (; *cp && !isspace(*cp); cp++)
239 			;
240 		if (*cp)
241 			*cp++ = '\0';
242 		if (cp - args == 0)
243 			break;
244 		for (i = 0; i < dk_ndrive; i++)
245 			if (strcmp(args, dr_name[i]) == 0) {
246 				if (dk_mspw[i] != 0.0)
247 					selections[i] = truefalse;
248 				else
249 					error("%s: drive not configured",
250 					    dr_name[i]);
251 				break;
252 			}
253 		if (i >= dk_ndrive)
254 			error("%s: unknown drive", args);
255 		args = cp;
256 	}
257 }
258