disks.c revision 1.1 1 /*-
2 * Copyright (c) 1980, 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
34 #ifndef lint
35 static char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37
38 #include <sys/types.h>
39 #include <sys/buf.h>
40
41 #include <nlist.h>
42 #include <ctype.h>
43 #include <paths.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include "systat.h"
47 #include "extern.h"
48
49 static void dkselect __P((char *, int, int []));
50 static int read_names __P((void));
51
52 static struct nlist namelist[] = {
53 #define X_DK_NDRIVE 0
54 { "_dk_ndrive" },
55 #define X_DK_WPMS 1
56 { "_dk_wpms" },
57 #ifdef vax
58 #define X_MBDINIT (X_DK_WPMS+1)
59 { "_mbdinit" },
60 #define X_UBDINIT (X_DK_WPMS+2)
61 { "_ubdinit" },
62 #endif
63 #ifdef sun
64 #define X_MBDINIT (X_DK_WPMS+1)
65 { "_mbdinit" },
66 #endif
67 #ifdef tahoe
68 #define X_VBDINIT (X_DK_WPMS+1)
69 { "_vbdinit" },
70 #endif
71 #if defined(hp300) || defined(luna68k)
72 #define X_HPDINIT (X_DK_WPMS+1)
73 { "_hp_dinit" },
74 #endif
75 #ifdef mips
76 #define X_SCSI_DINIT (X_DK_WPMS+1)
77 { "_scsi_dinit" },
78 #endif
79 { "" },
80 };
81
82 float *dk_mspw;
83 int dk_ndrive, *dk_select;
84 char **dr_name;
85
86 #include "names.c" /* XXX */
87
88 int
89 dkinit()
90 {
91 register int i;
92 register char *cp;
93 static int once = 0;
94 static char buf[1024];
95
96 if (once)
97 return(1);
98
99 if (kvm_nlist(kd, namelist)) {
100 nlisterr(namelist);
101 return(0);
102 }
103 if (namelist[X_DK_NDRIVE].n_value == 0) {
104 error("dk_ndrive undefined in kernel");
105 return(0);
106 }
107 NREAD(X_DK_NDRIVE, &dk_ndrive, LONG);
108 if (dk_ndrive <= 0) {
109 error("dk_ndrive=%d according to %s", dk_ndrive, _PATH_UNIX);
110 return(0);
111 }
112 dk_mspw = (float *)calloc(dk_ndrive, sizeof (float));
113 {
114 long *wpms = (long *)calloc(dk_ndrive, sizeof(long));
115 KREAD(NPTR(X_DK_WPMS), wpms, dk_ndrive * sizeof (long));
116 for (i = 0; i < dk_ndrive; i++)
117 *(dk_mspw + i) = (*(wpms + i) == 0)? 0.0:
118 (float) 1.0 / *(wpms + i);
119 free(wpms);
120 }
121 dr_name = (char **)calloc(dk_ndrive, sizeof (char *));
122 dk_select = (int *)calloc(dk_ndrive, sizeof (int));
123 for (cp = buf, i = 0; i < dk_ndrive; i++) {
124 dr_name[i] = cp;
125 sprintf(dr_name[i], "dk%d", i);
126 cp += strlen(dr_name[i]) + 1;
127 if (dk_mspw[i] != 0.0)
128 dk_select[i] = 1;
129 }
130 if (!read_names()) {
131 free(dr_name);
132 free(dk_select);
133 free(dk_mspw);
134 return(0);
135 }
136 once = 1;
137 return(1);
138 }
139
140 int
141 dkcmd(cmd, args)
142 char *cmd, *args;
143 {
144 if (prefix(cmd, "display") || prefix(cmd, "add")) {
145 dkselect(args, 1, dk_select);
146 return (1);
147 }
148 if (prefix(cmd, "ignore") || prefix(cmd, "delete")) {
149 dkselect(args, 0, dk_select);
150 return (1);
151 }
152 if (prefix(cmd, "drives")) {
153 register int i;
154
155 move(CMDLINE, 0); clrtoeol();
156 for (i = 0; i < dk_ndrive; i++)
157 if (dk_mspw[i] != 0.0)
158 printw("%s ", dr_name[i]);
159 return (1);
160 }
161 return (0);
162 }
163
164 static void
165 dkselect(args, truefalse, selections)
166 char *args;
167 int truefalse, selections[];
168 {
169 register char *cp;
170 register int i;
171 char *index();
172
173 cp = index(args, '\n');
174 if (cp)
175 *cp = '\0';
176 for (;;) {
177 for (cp = args; *cp && isspace(*cp); cp++)
178 ;
179 args = cp;
180 for (; *cp && !isspace(*cp); cp++)
181 ;
182 if (*cp)
183 *cp++ = '\0';
184 if (cp - args == 0)
185 break;
186 for (i = 0; i < dk_ndrive; i++)
187 if (strcmp(args, dr_name[i]) == 0) {
188 if (dk_mspw[i] != 0.0)
189 selections[i] = truefalse;
190 else
191 error("%s: drive not configured",
192 dr_name[i]);
193 break;
194 }
195 if (i >= dk_ndrive)
196 error("%s: unknown drive", args);
197 args = cp;
198 }
199 }
200