ccdconfig.c revision 1.48 1 1.48 lukem /* $NetBSD: ccdconfig.c,v 1.48 2008/07/20 01:20:21 lukem Exp $ */
2 1.1 thorpej
3 1.5 thorpej /*-
4 1.8 thorpej * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.5 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.5 thorpej * by Jason R. Thorpe.
9 1.5 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.5 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.5 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.5 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.13 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.13 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.5 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.5 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.5 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.5 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.5 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.5 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.10 thorpej #include <sys/cdefs.h>
33 1.10 thorpej #ifndef lint
34 1.48 lukem __COPYRIGHT("@(#) Copyright (c) 1996, 1997\
35 1.48 lukem The NetBSD Foundation, Inc. All rights reserved.");
36 1.48 lukem __RCSID("$NetBSD: ccdconfig.c,v 1.48 2008/07/20 01:20:21 lukem Exp $");
37 1.10 thorpej #endif
38 1.29 enami
39 1.1 thorpej #include <sys/param.h>
40 1.1 thorpej #include <sys/ioctl.h>
41 1.1 thorpej #include <sys/disklabel.h>
42 1.1 thorpej #include <sys/disk.h>
43 1.1 thorpej #include <sys/stat.h>
44 1.1 thorpej #include <sys/sysctl.h>
45 1.1 thorpej #include <ctype.h>
46 1.1 thorpej #include <err.h>
47 1.1 thorpej #include <errno.h>
48 1.1 thorpej #include <fcntl.h>
49 1.1 thorpej #include <kvm.h>
50 1.1 thorpej #include <limits.h>
51 1.1 thorpej #include <nlist.h>
52 1.1 thorpej #include <stdio.h>
53 1.1 thorpej #include <stdlib.h>
54 1.1 thorpej #include <string.h>
55 1.1 thorpej #include <unistd.h>
56 1.6 thorpej #include <util.h>
57 1.1 thorpej
58 1.1 thorpej #include <dev/ccdvar.h>
59 1.1 thorpej
60 1.1 thorpej #include "pathnames.h"
61 1.1 thorpej
62 1.20 mrg
63 1.16 thorpej static size_t lineno;
64 1.20 mrg static gid_t egid;
65 1.16 thorpej static int verbose;
66 1.33 thorpej static const char *ccdconf = _PATH_CCDCONF;
67 1.1 thorpej
68 1.16 thorpej static char *core;
69 1.16 thorpej static char *kernel;
70 1.1 thorpej
71 1.1 thorpej struct flagval {
72 1.33 thorpej const char *fv_flag;
73 1.1 thorpej int fv_val;
74 1.1 thorpej } flagvaltab[] = {
75 1.1 thorpej { "CCDF_UNIFORM", CCDF_UNIFORM },
76 1.38 lukem { "CCDF_NOLABEL", CCDF_NOLABEL },
77 1.1 thorpej { NULL, 0 },
78 1.1 thorpej };
79 1.1 thorpej
80 1.1 thorpej static struct nlist nl[] = {
81 1.46 christos { .n_name = "_ccd_softc" },
82 1.1 thorpej #define SYM_CCDSOFTC 0
83 1.46 christos { .n_name = "_numccd" },
84 1.1 thorpej #define SYM_NUMCCD 1
85 1.46 christos { .n_name = "_ccd_softc_elemsize" },
86 1.42 lukem #define SYM_CCDSOFTCELEMSIZE 2
87 1.46 christos { .n_name = NULL },
88 1.1 thorpej };
89 1.1 thorpej
90 1.1 thorpej #define CCD_CONFIG 0 /* configure a device */
91 1.1 thorpej #define CCD_CONFIGALL 1 /* configure all devices */
92 1.1 thorpej #define CCD_UNCONFIG 2 /* unconfigure a device */
93 1.1 thorpej #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
94 1.1 thorpej #define CCD_DUMP 4 /* dump a ccd's configuration */
95 1.1 thorpej
96 1.40 xtraeme static int checkdev(char *);
97 1.40 xtraeme static int do_io(char *, u_long, struct ccd_ioctl *);
98 1.40 xtraeme static int do_single(int, char **, int);
99 1.40 xtraeme static int do_all(int);
100 1.40 xtraeme static int dump_ccd(int, char **, int);
101 1.40 xtraeme static int flags_to_val(char *);
102 1.40 xtraeme static int pathtounit(char *, int *);
103 1.40 xtraeme static void print_ccd_info(struct ccd_softc *, kvm_t *);
104 1.40 xtraeme static char *resolve_ccdname(char *);
105 1.40 xtraeme static void usage(void);
106 1.1 thorpej
107 1.1 thorpej int
108 1.40 xtraeme main(int argc, char *argv[])
109 1.1 thorpej {
110 1.1 thorpej int ch, options = 0, action = CCD_CONFIG;
111 1.1 thorpej
112 1.20 mrg egid = getegid();
113 1.20 mrg setegid(getgid());
114 1.22 msaitoh while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
115 1.1 thorpej switch (ch) {
116 1.1 thorpej case 'c':
117 1.1 thorpej action = CCD_CONFIG;
118 1.1 thorpej ++options;
119 1.1 thorpej break;
120 1.1 thorpej
121 1.1 thorpej case 'C':
122 1.1 thorpej action = CCD_CONFIGALL;
123 1.1 thorpej ++options;
124 1.1 thorpej break;
125 1.1 thorpej
126 1.1 thorpej case 'f':
127 1.1 thorpej ccdconf = optarg;
128 1.1 thorpej break;
129 1.1 thorpej
130 1.1 thorpej case 'g':
131 1.1 thorpej action = CCD_DUMP;
132 1.1 thorpej break;
133 1.1 thorpej
134 1.1 thorpej case 'M':
135 1.1 thorpej core = optarg;
136 1.1 thorpej break;
137 1.1 thorpej
138 1.1 thorpej case 'N':
139 1.3 thorpej kernel = optarg;
140 1.1 thorpej break;
141 1.1 thorpej
142 1.1 thorpej case 'u':
143 1.1 thorpej action = CCD_UNCONFIG;
144 1.1 thorpej ++options;
145 1.1 thorpej break;
146 1.1 thorpej
147 1.1 thorpej case 'U':
148 1.1 thorpej action = CCD_UNCONFIGALL;
149 1.1 thorpej ++options;
150 1.1 thorpej break;
151 1.1 thorpej
152 1.1 thorpej case 'v':
153 1.1 thorpej verbose = 1;
154 1.1 thorpej break;
155 1.1 thorpej
156 1.1 thorpej default:
157 1.1 thorpej usage();
158 1.1 thorpej }
159 1.1 thorpej }
160 1.1 thorpej argc -= optind;
161 1.1 thorpej argv += optind;
162 1.1 thorpej
163 1.1 thorpej if (options > 1)
164 1.1 thorpej usage();
165 1.7 thorpej
166 1.7 thorpej /*
167 1.20 mrg * Discard setgid privileges. If not the running kernel, we toss
168 1.20 mrg * them away totally so that bad guys can't print interesting stuff
169 1.20 mrg * from kernel memory, otherwise switch back to kmem for the
170 1.20 mrg * duration of the kvm_openfiles() call.
171 1.20 mrg *
172 1.20 mrg * We also do this if we aren't just looking...
173 1.7 thorpej */
174 1.21 thorpej if (core != NULL || kernel != NULL || action != CCD_DUMP)
175 1.7 thorpej setgid(getgid());
176 1.1 thorpej
177 1.1 thorpej switch (action) {
178 1.1 thorpej case CCD_CONFIG:
179 1.1 thorpej case CCD_UNCONFIG:
180 1.1 thorpej exit(do_single(argc, argv, action));
181 1.1 thorpej /* NOTREACHED */
182 1.1 thorpej
183 1.1 thorpej case CCD_CONFIGALL:
184 1.1 thorpej case CCD_UNCONFIGALL:
185 1.1 thorpej exit(do_all(action));
186 1.1 thorpej /* NOTREACHED */
187 1.1 thorpej
188 1.1 thorpej case CCD_DUMP:
189 1.10 thorpej default:
190 1.8 thorpej exit(dump_ccd(argc, argv, action));
191 1.1 thorpej /* NOTREACHED */
192 1.1 thorpej }
193 1.1 thorpej /* NOTREACHED */
194 1.1 thorpej }
195 1.1 thorpej
196 1.1 thorpej static int
197 1.40 xtraeme do_single(int argc, char **argv, int action)
198 1.1 thorpej {
199 1.1 thorpej struct ccd_ioctl ccio;
200 1.1 thorpej char *ccd, *cp, *cp2, **disks;
201 1.10 thorpej int noflags = 0, i, ileave, flags, j;
202 1.1 thorpej
203 1.10 thorpej flags = 0;
204 1.14 lukem memset(&ccio, 0, sizeof(ccio));
205 1.1 thorpej
206 1.1 thorpej /*
207 1.1 thorpej * If unconfiguring, all arguments are treated as ccds.
208 1.1 thorpej */
209 1.1 thorpej if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
210 1.1 thorpej for (i = 0; argc != 0; ) {
211 1.1 thorpej cp = *argv++; --argc;
212 1.1 thorpej if ((ccd = resolve_ccdname(cp)) == NULL) {
213 1.1 thorpej warnx("invalid ccd name: %s", cp);
214 1.1 thorpej i = 1;
215 1.1 thorpej continue;
216 1.1 thorpej }
217 1.1 thorpej if (do_io(ccd, CCDIOCCLR, &ccio))
218 1.1 thorpej i = 1;
219 1.1 thorpej else
220 1.1 thorpej if (verbose)
221 1.2 thorpej printf("%s unconfigured\n", cp);
222 1.44 christos free(ccd);
223 1.1 thorpej }
224 1.1 thorpej return (i);
225 1.1 thorpej }
226 1.1 thorpej
227 1.1 thorpej /* Make sure there are enough arguments. */
228 1.23 ross if (argc < 4) {
229 1.1 thorpej if (argc == 3) {
230 1.1 thorpej /* Assume that no flags are specified. */
231 1.1 thorpej noflags = 1;
232 1.1 thorpej } else {
233 1.1 thorpej if (action == CCD_CONFIGALL) {
234 1.16 thorpej warnx("%s: bad line: %lu", ccdconf,
235 1.16 thorpej (u_long)lineno);
236 1.1 thorpej return (1);
237 1.1 thorpej } else
238 1.1 thorpej usage();
239 1.1 thorpej }
240 1.23 ross }
241 1.1 thorpej
242 1.1 thorpej /* First argument is the ccd to configure. */
243 1.1 thorpej cp = *argv++; --argc;
244 1.1 thorpej if ((ccd = resolve_ccdname(cp)) == NULL) {
245 1.1 thorpej warnx("invalid ccd name: %s", cp);
246 1.1 thorpej return (1);
247 1.1 thorpej }
248 1.1 thorpej
249 1.1 thorpej /* Next argument is the interleave factor. */
250 1.1 thorpej cp = *argv++; --argc;
251 1.1 thorpej errno = 0; /* to check for ERANGE */
252 1.1 thorpej ileave = (int)strtol(cp, &cp2, 10);
253 1.1 thorpej if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
254 1.1 thorpej warnx("invalid interleave factor: %s", cp);
255 1.43 hubertf free(ccd);
256 1.1 thorpej return (1);
257 1.1 thorpej }
258 1.1 thorpej
259 1.1 thorpej if (noflags == 0) {
260 1.1 thorpej /* Next argument is the ccd configuration flags. */
261 1.1 thorpej cp = *argv++; --argc;
262 1.1 thorpej if ((flags = flags_to_val(cp)) < 0) {
263 1.1 thorpej warnx("invalid flags argument: %s", cp);
264 1.43 hubertf free(ccd);
265 1.1 thorpej return (1);
266 1.1 thorpej }
267 1.1 thorpej }
268 1.1 thorpej
269 1.1 thorpej /* Next is the list of disks to make the ccd from. */
270 1.1 thorpej disks = malloc(argc * sizeof(char *));
271 1.1 thorpej if (disks == NULL) {
272 1.1 thorpej warnx("no memory to configure ccd");
273 1.43 hubertf free(ccd);
274 1.1 thorpej return (1);
275 1.1 thorpej }
276 1.1 thorpej for (i = 0; argc != 0; ) {
277 1.1 thorpej cp = *argv++; --argc;
278 1.1 thorpej if ((j = checkdev(cp)) == 0)
279 1.1 thorpej disks[i++] = cp;
280 1.1 thorpej else {
281 1.1 thorpej warnx("%s: %s", cp, strerror(j));
282 1.43 hubertf free(ccd);
283 1.43 hubertf free(disks);
284 1.1 thorpej return (1);
285 1.1 thorpej }
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej /* Fill in the ccio. */
289 1.1 thorpej ccio.ccio_disks = disks;
290 1.1 thorpej ccio.ccio_ndisks = i;
291 1.1 thorpej ccio.ccio_ileave = ileave;
292 1.1 thorpej ccio.ccio_flags = flags;
293 1.1 thorpej
294 1.1 thorpej if (do_io(ccd, CCDIOCSET, &ccio)) {
295 1.43 hubertf free(ccd);
296 1.1 thorpej free(disks);
297 1.1 thorpej return (1);
298 1.1 thorpej }
299 1.1 thorpej
300 1.1 thorpej if (verbose) {
301 1.1 thorpej printf("ccd%d: %d components ", ccio.ccio_unit,
302 1.1 thorpej ccio.ccio_ndisks);
303 1.1 thorpej for (i = 0; i < ccio.ccio_ndisks; ++i) {
304 1.1 thorpej if ((cp2 = strrchr(disks[i], '/')) != NULL)
305 1.1 thorpej ++cp2;
306 1.1 thorpej else
307 1.1 thorpej cp2 = disks[i];
308 1.1 thorpej printf("%c%s%c",
309 1.1 thorpej i == 0 ? '(' : ' ', cp2,
310 1.1 thorpej i == ccio.ccio_ndisks - 1 ? ')' : ',');
311 1.1 thorpej }
312 1.11 thorpej printf(", %ld blocks ", (long)ccio.ccio_size);
313 1.1 thorpej if (ccio.ccio_ileave != 0)
314 1.1 thorpej printf("interleaved at %d blocks\n", ccio.ccio_ileave);
315 1.1 thorpej else
316 1.1 thorpej printf("concatenated\n");
317 1.1 thorpej }
318 1.1 thorpej
319 1.43 hubertf free(ccd);
320 1.1 thorpej free(disks);
321 1.1 thorpej return (0);
322 1.1 thorpej }
323 1.1 thorpej
324 1.1 thorpej static int
325 1.40 xtraeme do_all(int action)
326 1.1 thorpej {
327 1.1 thorpej FILE *f;
328 1.35 itojun char *line, *cp, *vp, **argv, **nargv;
329 1.16 thorpej int argc, rval;
330 1.16 thorpej size_t len;
331 1.1 thorpej
332 1.10 thorpej rval = 0;
333 1.10 thorpej
334 1.17 mrg (void)setegid(getgid());
335 1.1 thorpej if ((f = fopen(ccdconf, "r")) == NULL) {
336 1.17 mrg (void)setegid(egid);
337 1.1 thorpej warn("fopen: %s", ccdconf);
338 1.1 thorpej return (1);
339 1.1 thorpej }
340 1.17 mrg (void)setegid(egid);
341 1.1 thorpej
342 1.15 lukem while ((line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL))
343 1.15 lukem != NULL) {
344 1.1 thorpej argc = 0;
345 1.1 thorpej argv = NULL;
346 1.15 lukem if (len == 0)
347 1.15 lukem goto end_of_line;
348 1.15 lukem
349 1.15 lukem for (cp = line; cp != NULL; ) {
350 1.15 lukem while ((vp = strsep(&cp, "\t ")) != NULL && *vp == '\0')
351 1.15 lukem ;
352 1.15 lukem if (vp == NULL)
353 1.15 lukem continue;
354 1.1 thorpej
355 1.35 itojun if ((nargv = realloc(argv,
356 1.35 itojun sizeof(char *) * (argc + 1))) == NULL) {
357 1.1 thorpej warnx("no memory to configure ccds");
358 1.1 thorpej return (1);
359 1.1 thorpej }
360 1.35 itojun argv = nargv;
361 1.35 itojun argc++;
362 1.15 lukem argv[argc - 1] = vp;
363 1.15 lukem
364 1.1 thorpej /*
365 1.1 thorpej * If our action is to unconfigure all, then pass
366 1.1 thorpej * just the first token to do_single() and ignore
367 1.1 thorpej * the rest. Since this will be encountered on
368 1.1 thorpej * our first pass through the line, the Right
369 1.1 thorpej * Thing will happen.
370 1.1 thorpej */
371 1.1 thorpej if (action == CCD_UNCONFIGALL) {
372 1.1 thorpej if (do_single(argc, argv, action))
373 1.1 thorpej rval = 1;
374 1.1 thorpej goto end_of_line;
375 1.1 thorpej }
376 1.1 thorpej }
377 1.1 thorpej if (argc != 0)
378 1.1 thorpej if (do_single(argc, argv, action))
379 1.1 thorpej rval = 1;
380 1.1 thorpej
381 1.1 thorpej end_of_line:
382 1.1 thorpej if (argv != NULL)
383 1.1 thorpej free(argv);
384 1.15 lukem free(line);
385 1.1 thorpej }
386 1.1 thorpej
387 1.1 thorpej (void)fclose(f);
388 1.1 thorpej return (rval);
389 1.1 thorpej }
390 1.1 thorpej
391 1.1 thorpej static int
392 1.40 xtraeme checkdev(char *path)
393 1.1 thorpej {
394 1.1 thorpej struct stat st;
395 1.1 thorpej
396 1.1 thorpej if (stat(path, &st) != 0)
397 1.1 thorpej return (errno);
398 1.1 thorpej
399 1.1 thorpej if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
400 1.1 thorpej return (EINVAL);
401 1.1 thorpej
402 1.1 thorpej return (0);
403 1.1 thorpej }
404 1.1 thorpej
405 1.1 thorpej static int
406 1.40 xtraeme pathtounit(char *path, int *unitp)
407 1.1 thorpej {
408 1.1 thorpej struct stat st;
409 1.1 thorpej
410 1.1 thorpej if (stat(path, &st) != 0)
411 1.1 thorpej return (errno);
412 1.1 thorpej
413 1.1 thorpej if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
414 1.1 thorpej return (EINVAL);
415 1.1 thorpej
416 1.42 lukem *unitp = DISKUNIT(st.st_rdev);
417 1.1 thorpej
418 1.1 thorpej return (0);
419 1.1 thorpej }
420 1.1 thorpej
421 1.1 thorpej static char *
422 1.40 xtraeme resolve_ccdname(char *name)
423 1.1 thorpej {
424 1.10 thorpej char c, *path;
425 1.37 itojun size_t len;
426 1.1 thorpej int rawpart;
427 1.1 thorpej
428 1.1 thorpej if (name[0] == '/' || name[0] == '.') {
429 1.1 thorpej /* Assume they gave the correct pathname. */
430 1.1 thorpej return (strdup(name));
431 1.1 thorpej }
432 1.1 thorpej
433 1.1 thorpej len = strlen(name);
434 1.1 thorpej c = name[len - 1];
435 1.1 thorpej
436 1.39 dsl if (isdigit((unsigned char)c)) {
437 1.37 itojun if ((rawpart = getrawpartition()) < 0)
438 1.37 itojun return (NULL);
439 1.37 itojun if (asprintf(&path, "/dev/%s%c", name, 'a' + rawpart) < 0)
440 1.1 thorpej return (NULL);
441 1.1 thorpej } else
442 1.37 itojun if (asprintf(&path, "/dev/%s", name) < 0)
443 1.37 itojun return (NULL);
444 1.1 thorpej
445 1.1 thorpej return (path);
446 1.1 thorpej }
447 1.1 thorpej
448 1.1 thorpej static int
449 1.40 xtraeme do_io(char *path, u_long cmd, struct ccd_ioctl *cciop)
450 1.1 thorpej {
451 1.1 thorpej int fd;
452 1.33 thorpej const char *cp;
453 1.1 thorpej
454 1.1 thorpej if ((fd = open(path, O_RDWR, 0640)) < 0) {
455 1.1 thorpej warn("open: %s", path);
456 1.1 thorpej return (1);
457 1.1 thorpej }
458 1.1 thorpej
459 1.1 thorpej if (ioctl(fd, cmd, cciop) < 0) {
460 1.1 thorpej switch (cmd) {
461 1.1 thorpej case CCDIOCSET:
462 1.1 thorpej cp = "CCDIOCSET";
463 1.1 thorpej break;
464 1.1 thorpej
465 1.1 thorpej case CCDIOCCLR:
466 1.1 thorpej cp = "CCDIOCCLR";
467 1.1 thorpej break;
468 1.1 thorpej
469 1.1 thorpej default:
470 1.1 thorpej cp = "unknown";
471 1.1 thorpej }
472 1.1 thorpej warn("ioctl (%s): %s", cp, path);
473 1.1 thorpej return (1);
474 1.1 thorpej }
475 1.1 thorpej
476 1.1 thorpej return (0);
477 1.1 thorpej }
478 1.1 thorpej
479 1.1 thorpej #define KVM_ABORT(kd, str) { \
480 1.1 thorpej (void)kvm_close((kd)); \
481 1.31 itojun warnx("%s", (str)); \
482 1.31 itojun warnx("%s", kvm_geterr((kd))); \
483 1.1 thorpej return (1); \
484 1.1 thorpej }
485 1.1 thorpej
486 1.1 thorpej static int
487 1.40 xtraeme dump_ccd(int argc, char **argv, int action)
488 1.1 thorpej {
489 1.1 thorpej char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
490 1.1 thorpej struct ccd_softc *cs, *kcs;
491 1.42 lukem void *vcs;
492 1.1 thorpej size_t readsize;
493 1.42 lukem int i, error, numccd, ccd_softc_elemsize, numconfiged = 0;
494 1.1 thorpej kvm_t *kd;
495 1.1 thorpej
496 1.14 lukem memset(errbuf, 0, sizeof(errbuf));
497 1.1 thorpej
498 1.42 lukem vcs = NULL;
499 1.20 mrg (void)setegid(egid);
500 1.1 thorpej if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
501 1.1 thorpej errbuf)) == NULL) {
502 1.1 thorpej warnx("can't open kvm: %s", errbuf);
503 1.1 thorpej return (1);
504 1.1 thorpej }
505 1.20 mrg (void)setgid(getgid());
506 1.1 thorpej
507 1.1 thorpej if (kvm_nlist(kd, nl))
508 1.1 thorpej KVM_ABORT(kd, "ccd-related symbols not available");
509 1.1 thorpej
510 1.1 thorpej /* Check to see how many ccds are currently configured. */
511 1.42 lukem if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (void *)&numccd,
512 1.1 thorpej sizeof(numccd)) != sizeof(numccd))
513 1.1 thorpej KVM_ABORT(kd, "can't determine number of configured ccds");
514 1.1 thorpej
515 1.1 thorpej if (numccd == 0) {
516 1.1 thorpej printf("ccd driver in kernel, but is uninitialized\n");
517 1.1 thorpej goto done;
518 1.1 thorpej }
519 1.1 thorpej
520 1.42 lukem if (kvm_read(kd, nl[SYM_CCDSOFTCELEMSIZE].n_value,
521 1.42 lukem (void *)&ccd_softc_elemsize, sizeof(ccd_softc_elemsize))
522 1.42 lukem != sizeof(ccd_softc_elemsize))
523 1.42 lukem KVM_ABORT(kd, "can't determine size of ccd_softc");
524 1.42 lukem
525 1.42 lukem /* Allocate space for the kernel's configuration data. */
526 1.42 lukem readsize = numccd * ccd_softc_elemsize;
527 1.42 lukem if ((vcs = malloc(readsize)) == NULL) {
528 1.1 thorpej warnx("no memory for configuration data");
529 1.1 thorpej goto bad;
530 1.1 thorpej }
531 1.42 lukem memset(vcs, 0, readsize);
532 1.1 thorpej
533 1.1 thorpej /*
534 1.42 lukem * Read the ccd configuration data from the kernel.
535 1.42 lukem * The kernel's ccd_softc is larger than userland's
536 1.42 lukem * (the former contains extra structure members at
537 1.42 lukem * the end of the structure), so read the kernel
538 1.42 lukem * ccd_softcs into a temporary buffer and convert
539 1.42 lukem * into userland ccd_softcs.
540 1.1 thorpej */
541 1.42 lukem if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (void *)&kcs,
542 1.1 thorpej sizeof(kcs)) != sizeof(kcs)) {
543 1.42 lukem free(vcs);
544 1.1 thorpej KVM_ABORT(kd, "can't find pointer to configuration data");
545 1.1 thorpej }
546 1.42 lukem if (kvm_read(kd, (u_long)kcs, vcs, readsize) != readsize) {
547 1.42 lukem free(vcs);
548 1.1 thorpej KVM_ABORT(kd, "can't read configuration data");
549 1.1 thorpej }
550 1.1 thorpej
551 1.42 lukem if ((cs = calloc(numccd, sizeof(struct ccd_softc))) == NULL) {
552 1.42 lukem warnx("no memory for configuration data");
553 1.44 christos free(vcs);
554 1.42 lukem goto bad;
555 1.42 lukem }
556 1.42 lukem for (i = 0; i < numccd; i++) {
557 1.42 lukem memcpy(&cs[i], (char *)vcs + i * ccd_softc_elemsize,
558 1.42 lukem sizeof(struct ccd_softc));
559 1.42 lukem }
560 1.42 lukem free(vcs);
561 1.42 lukem
562 1.42 lukem /* Dump ccd configuration to stdout. */
563 1.1 thorpej if (argc == 0) {
564 1.1 thorpej for (i = 0; i < numccd; ++i)
565 1.1 thorpej if (cs[i].sc_flags & CCDF_INITED) {
566 1.1 thorpej ++numconfiged;
567 1.21 thorpej print_ccd_info(&cs[i], kd);
568 1.1 thorpej }
569 1.1 thorpej
570 1.1 thorpej if (numconfiged == 0)
571 1.18 mycroft printf("# no concatenated disks configured\n");
572 1.1 thorpej } else {
573 1.1 thorpej while (argc) {
574 1.1 thorpej cp = *argv++; --argc;
575 1.1 thorpej if ((ccd = resolve_ccdname(cp)) == NULL) {
576 1.1 thorpej warnx("invalid ccd name: %s", cp);
577 1.1 thorpej continue;
578 1.1 thorpej }
579 1.1 thorpej if ((error = pathtounit(ccd, &i)) != 0) {
580 1.19 mrg warn("%s", ccd);
581 1.36 itojun free(ccd);
582 1.1 thorpej continue;
583 1.1 thorpej }
584 1.1 thorpej if (i >= numccd) {
585 1.1 thorpej warnx("ccd%d not configured", i);
586 1.36 itojun free(ccd);
587 1.1 thorpej continue;
588 1.1 thorpej }
589 1.21 thorpej if (cs[i].sc_flags & CCDF_INITED)
590 1.21 thorpej print_ccd_info(&cs[i], kd);
591 1.21 thorpej else
592 1.18 mycroft printf("# ccd%d not configured\n", i);
593 1.44 christos free(ccd);
594 1.1 thorpej }
595 1.1 thorpej }
596 1.1 thorpej
597 1.1 thorpej free(cs);
598 1.1 thorpej
599 1.1 thorpej done:
600 1.1 thorpej (void)kvm_close(kd);
601 1.1 thorpej return (0);
602 1.1 thorpej
603 1.1 thorpej bad:
604 1.1 thorpej (void)kvm_close(kd);
605 1.1 thorpej return (1);
606 1.1 thorpej }
607 1.1 thorpej
608 1.1 thorpej static void
609 1.40 xtraeme print_ccd_info(struct ccd_softc *cs, kvm_t *kd)
610 1.1 thorpej {
611 1.1 thorpej static int header_printed = 0;
612 1.1 thorpej struct ccdcinfo *cip;
613 1.1 thorpej size_t readsize;
614 1.1 thorpej char path[MAXPATHLEN];
615 1.1 thorpej int i;
616 1.1 thorpej
617 1.1 thorpej if (header_printed == 0 && verbose) {
618 1.1 thorpej printf("# ccd\t\tileave\tflags\tcompnent devices\n");
619 1.1 thorpej header_printed = 1;
620 1.1 thorpej }
621 1.1 thorpej
622 1.1 thorpej readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
623 1.1 thorpej if ((cip = malloc(readsize)) == NULL) {
624 1.26 thorpej warn("%s: can't allocate memory for component info",
625 1.26 thorpej cs->sc_xname);
626 1.1 thorpej return;
627 1.1 thorpej }
628 1.14 lukem memset(cip, 0, readsize);
629 1.1 thorpej
630 1.1 thorpej /* Dump out softc information. */
631 1.26 thorpej printf("%s\t\t%d\t%d\t", cs->sc_xname, cs->sc_ileave,
632 1.25 thorpej cs->sc_flags & CCDF_USERMASK);
633 1.1 thorpej fflush(stdout);
634 1.1 thorpej
635 1.1 thorpej /* Read in the component info. */
636 1.42 lukem if (kvm_read(kd, (u_long)cs->sc_cinfo, (void *)cip,
637 1.1 thorpej readsize) != readsize) {
638 1.1 thorpej printf("\n");
639 1.1 thorpej warnx("can't read component info");
640 1.31 itojun warnx("%s", kvm_geterr(kd));
641 1.1 thorpej goto done;
642 1.1 thorpej }
643 1.1 thorpej
644 1.1 thorpej /* Read component pathname and display component info. */
645 1.1 thorpej for (i = 0; i < cs->sc_nccdisks; ++i) {
646 1.42 lukem if (kvm_read(kd, (u_long)cip[i].ci_path, (void *)path,
647 1.1 thorpej cip[i].ci_pathlen) != cip[i].ci_pathlen) {
648 1.1 thorpej printf("\n");
649 1.1 thorpej warnx("can't read component pathname");
650 1.31 itojun warnx("%s", kvm_geterr(kd));
651 1.1 thorpej goto done;
652 1.1 thorpej }
653 1.32 is fputs(path, stdout);
654 1.32 is fputc((i + 1 < cs->sc_nccdisks) ? ' ' : '\n', stdout);
655 1.1 thorpej fflush(stdout);
656 1.1 thorpej }
657 1.1 thorpej
658 1.1 thorpej done:
659 1.1 thorpej free(cip);
660 1.8 thorpej }
661 1.8 thorpej
662 1.1 thorpej static int
663 1.40 xtraeme flags_to_val(char *flags)
664 1.1 thorpej {
665 1.1 thorpej char *cp, *tok;
666 1.1 thorpej int i, tmp, val = ~CCDF_USERMASK;
667 1.1 thorpej size_t flagslen;
668 1.1 thorpej
669 1.1 thorpej /*
670 1.1 thorpej * The most common case is that of NIL flags, so check for
671 1.1 thorpej * those first.
672 1.1 thorpej */
673 1.1 thorpej if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
674 1.1 thorpej strcmp("0", flags) == 0)
675 1.1 thorpej return (0);
676 1.1 thorpej
677 1.1 thorpej flagslen = strlen(flags);
678 1.1 thorpej
679 1.1 thorpej /* Check for values represented by strings. */
680 1.1 thorpej if ((cp = strdup(flags)) == NULL)
681 1.1 thorpej err(1, "no memory to parse flags");
682 1.1 thorpej tmp = 0;
683 1.1 thorpej for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
684 1.1 thorpej for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
685 1.1 thorpej if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
686 1.1 thorpej break;
687 1.1 thorpej if (flagvaltab[i].fv_flag == NULL) {
688 1.1 thorpej free(cp);
689 1.1 thorpej goto bad_string;
690 1.1 thorpej }
691 1.1 thorpej tmp |= flagvaltab[i].fv_val;
692 1.1 thorpej }
693 1.1 thorpej
694 1.1 thorpej /* If we get here, the string was ok. */
695 1.1 thorpej free(cp);
696 1.1 thorpej val = tmp;
697 1.1 thorpej goto out;
698 1.1 thorpej
699 1.1 thorpej bad_string:
700 1.1 thorpej
701 1.1 thorpej /* Check for values represented in hex. */
702 1.1 thorpej if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
703 1.1 thorpej errno = 0; /* to check for ERANGE */
704 1.1 thorpej val = (int)strtol(&flags[2], &cp, 16);
705 1.1 thorpej if ((errno == ERANGE) || (*cp != '\0'))
706 1.1 thorpej return (-1);
707 1.1 thorpej goto out;
708 1.1 thorpej }
709 1.1 thorpej
710 1.1 thorpej /* Check for values represented in decimal. */
711 1.1 thorpej errno = 0; /* to check for ERANGE */
712 1.1 thorpej val = (int)strtol(flags, &cp, 10);
713 1.1 thorpej if ((errno == ERANGE) || (*cp != '\0'))
714 1.1 thorpej return (-1);
715 1.1 thorpej
716 1.1 thorpej out:
717 1.1 thorpej return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
718 1.1 thorpej }
719 1.1 thorpej
720 1.1 thorpej static void
721 1.40 xtraeme usage(void)
722 1.1 thorpej {
723 1.34 cgd const char *progname = getprogname();
724 1.1 thorpej
725 1.34 cgd fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] dev [...]\n",
726 1.34 cgd progname);
727 1.34 cgd fprintf(stderr, " %s -C [-v] [-f config_file]\n", progname);
728 1.34 cgd fprintf(stderr, " %s -u [-v] ccd [...]\n", progname);
729 1.34 cgd fprintf(stderr, " %s -U [-v] [-f config_file]\n", progname);
730 1.34 cgd fprintf(stderr, " %s -g [-M core] [-N system] [ccd [...]]\n",
731 1.34 cgd progname);
732 1.1 thorpej exit(1);
733 1.1 thorpej }
734