xref: /original-bsd/usr.bin/systat/disks.c (revision f955cb91)
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.10 (Berkeley) 06/26/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 	kvm_nlist(nlst);
48 	if (nlst[X_DK_NDRIVE].n_value == 0) {
49 		error("dk_ndrive undefined in kernel");
50 		return(0);
51 	}
52 	NREAD(X_DK_NDRIVE, &dk_ndrive, LONG);
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 	{
59 		long *wpms = (long *)calloc(dk_ndrive, sizeof(long));
60 		KREAD(NPTR(X_DK_WPMS), wpms, dk_ndrive * sizeof (long));
61 		for (i = 0; i < dk_ndrive; i++)
62 			*(dk_mspw + i) = (*(wpms + i) == 0)? 0.0:
63 			                 (float) 1.0 / *(wpms + i);
64 		free(wpms);
65 	}
66 	dr_name = (char **)calloc(dk_ndrive, sizeof (char *));
67 	dk_select = (int *)calloc(dk_ndrive, sizeof (int));
68 	for (cp = buf, i = 0; i < dk_ndrive; i++) {
69 		dr_name[i] = cp;
70 		sprintf(dr_name[i], "dk%d", i);
71 		cp += strlen(dr_name[i]) + 1;
72 		if (dk_mspw[i] != 0.0)
73 			dk_select[i] = 1;
74 	}
75 	if (!read_names()) {
76 		free(dr_name);
77 		free(dk_select);
78 		free(dk_mspw);
79 		return(0);
80 	}
81 	once = 1;
82 	return(1);
83 }
84 
85 dkcmd(cmd, args)
86 	char *cmd, *args;
87 {
88 	if (prefix(cmd, "display") || prefix(cmd, "add")) {
89 		dkselect(args, 1, dk_select);
90 		return (1);
91 	}
92 	if (prefix(cmd, "ignore") || prefix(cmd, "delete")) {
93 		dkselect(args, 0, dk_select);
94 		return (1);
95 	}
96 	if (prefix(cmd, "drives")) {
97 		register int i;
98 
99 		move(CMDLINE, 0); clrtoeol();
100 		for (i = 0; i < dk_ndrive; i++)
101 			if (dk_mspw[i] != 0.0)
102 				printw("%s ", dr_name[i]);
103 		return (1);
104 	}
105 	return (0);
106 }
107 
108 #define steal(where, var) \
109 	KREAD(where, &var, sizeof var)
110 
111 #ifdef vax
112 #include <vax/uba/ubavar.h>
113 #include <vax/mba/mbavar.h>
114 
115 read_names()
116 {
117 	struct mba_device mdev;
118 	struct mba_driver mdrv;
119 	short two_char;
120 	char *cp = (char *)&two_char;
121 	struct uba_device udev;
122 	struct uba_driver udrv;
123 	register struct mba_device *mp;
124 	register struct uba_device *up;
125 
126 	mp = (struct mba_device *)nlst[X_MBDINIT].n_value;
127 	up = (struct uba_device *)nlst[X_UBDINIT].n_value;
128 	if (mp == 0 && up == 0) {
129 		error("Disk init info not in namelist\n");
130 		return(0);
131 	}
132 	if (mp) for (;;) {
133 		steal(mp++, mdev);
134 		if (mdev.mi_driver == 0)
135 			break;
136 		if (mdev.mi_dk < 0 || mdev.mi_alive == 0)
137 			continue;
138 		steal(mdev.mi_driver, mdrv);
139 		steal(mdrv.md_dname, two_char);
140 		sprintf(dr_name[mdev.mi_dk], "%c%c%d",
141 		    cp[0], cp[1], mdev.mi_unit);
142 	}
143 	if (up) for (;;) {
144 		steal(up++, udev);
145 		if (udev.ui_driver == 0)
146 			break;
147 		if (udev.ui_dk < 0 || udev.ui_alive == 0)
148 			continue;
149 		steal(udev.ui_driver, udrv);
150 		steal(udrv.ud_dname, two_char);
151 		sprintf(dr_name[udev.ui_dk], "%c%c%d",
152 		    cp[0], cp[1], udev.ui_unit);
153 	}
154 	return(1);
155 }
156 #endif
157 
158 #ifdef sun
159 #include <sundev/mbvar.h>
160 
161 read_names()
162 {
163 	static int once = 0;
164 	struct mb_device mdev;
165 	struct mb_driver mdrv;
166 	short two_char;
167 	char *cp = (char *) &two_char;
168 	register struct mb_device *mp;
169 
170 	mp = (struct mb_device *)nlst[X_MBDINIT].n_value;
171 	if (mp == 0) {
172 		error("Disk init info not in namelist\n");
173 		return(0);
174 	}
175 	for (;;) {
176 		steal(mp++, mdev);
177 		if (mdev.md_driver == 0)
178 			break;
179 		if (mdev.md_dk < 0 || mdev.md_alive == 0)
180 			continue;
181 		steal(mdev.md_driver, mdrv);
182 		steal(mdrv.mdr_dname, two_char);
183 		sprintf(dr_name[mdev.md_dk], "%c%c%d",
184 		    cp[0], cp[1], mdev.md_unit);
185 	}
186 	return(1);
187 }
188 #endif
189 
190 #ifdef tahoe
191 #include <tahoe/vba/vbavar.h>
192 
193 /*
194  * Read the drive names out of kmem.
195  */
196 read_names()
197 {
198 	struct vba_device udev, *up;
199 	struct vba_driver udrv;
200 	short two_char;
201 	char *cp = (char *)&two_char;
202 
203 	up = (struct vba_device *) nlst[X_VBDINIT].n_value;
204 	if (up == 0) {
205 		fprintf(stderr, "vmstat: Disk init info not in namelist\n");
206 		exit(1);
207 	}
208 	for (;;) {
209 		steal(up++, udev);
210 		if (udev.ui_driver == 0)
211 			break;
212 		if (udev.ui_dk < 0 || udev.ui_alive == 0)
213 			continue;
214 		steal(udev.ui_driver, udrv);
215 		steal(udrv.ud_dname, two_char);
216 		sprintf(dr_name[udev.ui_dk], "%c%c%d",
217 		     cp[0], cp[1], udev.ui_unit);
218 	}
219 }
220 #endif
221 
222 #ifdef hp300
223 read_names() {}
224 #endif
225 
226 dkselect(args, truefalse, selections)
227 	char *args;
228 	int truefalse, selections[];
229 {
230 	register char *cp;
231 	register int i;
232 	char *index();
233 
234 	cp = index(args, '\n');
235 	if (cp)
236 		*cp = '\0';
237 	for (;;) {
238 		for (cp = args; *cp && isspace(*cp); cp++)
239 			;
240 		args = cp;
241 		for (; *cp && !isspace(*cp); cp++)
242 			;
243 		if (*cp)
244 			*cp++ = '\0';
245 		if (cp - args == 0)
246 			break;
247 		for (i = 0; i < dk_ndrive; i++)
248 			if (strcmp(args, dr_name[i]) == 0) {
249 				if (dk_mspw[i] != 0.0)
250 					selections[i] = truefalse;
251 				else
252 					error("%s: drive not configured",
253 					    dr_name[i]);
254 				break;
255 			}
256 		if (i >= dk_ndrive)
257 			error("%s: unknown drive", args);
258 		args = cp;
259 	}
260 }
261