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