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