disks.c revision 1.3 1 /* $NetBSD: disks.c,v 1.3 1996/03/15 22:19:23 ragge Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)disks.c 8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: disks.c,v 1.3 1996/03/15 22:19:23 ragge Exp $";
41 #endif /* not lint */
42
43 #include <sys/types.h>
44 #include <sys/buf.h>
45
46 #include <nlist.h>
47 #include <ctype.h>
48 #include <paths.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include "systat.h"
52 #include "extern.h"
53
54 static void dkselect __P((char *, int, int []));
55 static int read_names __P((void));
56
57 static struct nlist namelist[] = {
58 #define X_DK_NDRIVE 0
59 { "_dk_ndrive" },
60 #define X_DK_WPMS 1
61 { "_dk_wpms" },
62 #ifdef sun
63 #define X_MBDINIT (X_DK_WPMS+1)
64 { "_mbdinit" },
65 #endif
66 #ifdef tahoe
67 #define X_VBDINIT (X_DK_WPMS+1)
68 { "_vbdinit" },
69 #endif
70 #if defined(hp300) || defined(luna68k)
71 #define X_HPDINIT (X_DK_WPMS+1)
72 { "_hp_dinit" },
73 #endif
74 #ifdef mips
75 #define X_SCSI_DINIT (X_DK_WPMS+1)
76 { "_scsi_dinit" },
77 #endif
78 { "" },
79 };
80
81 float *dk_mspw;
82 int dk_ndrive, *dk_select;
83 char **dr_name;
84
85 #include "names.c" /* XXX */
86
87 int
88 dkinit()
89 {
90 register int i;
91 register char *cp;
92 static int once = 0;
93 static char buf[1024];
94
95 if (once)
96 return(1);
97
98 if (kvm_nlist(kd, namelist)) {
99 nlisterr(namelist);
100 return(0);
101 }
102 if (namelist[X_DK_NDRIVE].n_value == 0) {
103 error("dk_ndrive undefined in kernel");
104 return(0);
105 }
106 NREAD(X_DK_NDRIVE, &dk_ndrive, LONG);
107 if (dk_ndrive <= 0) {
108 error("dk_ndrive=%d according to %s", dk_ndrive, _PATH_UNIX);
109 return(0);
110 }
111 dk_mspw = (float *)calloc(dk_ndrive, sizeof (float));
112 {
113 long *wpms = (long *)calloc(dk_ndrive, sizeof(long));
114 KREAD(NPTR(X_DK_WPMS), wpms, dk_ndrive * sizeof (long));
115 for (i = 0; i < dk_ndrive; i++)
116 *(dk_mspw + i) = (*(wpms + i) == 0)? 0.0:
117 (float) 1.0 / *(wpms + i);
118 free(wpms);
119 }
120 dr_name = (char **)calloc(dk_ndrive, sizeof (char *));
121 dk_select = (int *)calloc(dk_ndrive, sizeof (int));
122 for (cp = buf, i = 0; i < dk_ndrive; i++) {
123 dr_name[i] = cp;
124 sprintf(dr_name[i], "dk%d", i);
125 cp += strlen(dr_name[i]) + 1;
126 if (dk_mspw[i] != 0.0)
127 dk_select[i] = 1;
128 }
129 if (!read_names()) {
130 free(dr_name);
131 free(dk_select);
132 free(dk_mspw);
133 return(0);
134 }
135 once = 1;
136 return(1);
137 }
138
139 int
140 dkcmd(cmd, args)
141 char *cmd, *args;
142 {
143 if (prefix(cmd, "display") || prefix(cmd, "add")) {
144 dkselect(args, 1, dk_select);
145 return (1);
146 }
147 if (prefix(cmd, "ignore") || prefix(cmd, "delete")) {
148 dkselect(args, 0, dk_select);
149 return (1);
150 }
151 if (prefix(cmd, "drives")) {
152 register int i;
153
154 move(CMDLINE, 0); clrtoeol();
155 for (i = 0; i < dk_ndrive; i++)
156 if (dk_mspw[i] != 0.0)
157 printw("%s ", dr_name[i]);
158 return (1);
159 }
160 return (0);
161 }
162
163 static void
164 dkselect(args, truefalse, selections)
165 char *args;
166 int truefalse, selections[];
167 {
168 register char *cp;
169 register int i;
170 char *index();
171
172 cp = index(args, '\n');
173 if (cp)
174 *cp = '\0';
175 for (;;) {
176 for (cp = args; *cp && isspace(*cp); cp++)
177 ;
178 args = cp;
179 for (; *cp && !isspace(*cp); cp++)
180 ;
181 if (*cp)
182 *cp++ = '\0';
183 if (cp - args == 0)
184 break;
185 for (i = 0; i < dk_ndrive; i++)
186 if (strcmp(args, dr_name[i]) == 0) {
187 if (dk_mspw[i] != 0.0)
188 selections[i] = truefalse;
189 else
190 error("%s: drive not configured",
191 dr_name[i]);
192 break;
193 }
194 if (i >= dk_ndrive)
195 error("%s: unknown drive", args);
196 args = cp;
197 }
198 }
199