Home | History | Annotate | Line # | Download | only in ccdconfig
ccdconfig.c revision 1.46.18.1
      1 /*	$NetBSD: ccdconfig.c,v 1.46.18.1 2008/06/02 13:21:20 mjf 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  *
     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.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT(
     35 "@(#) Copyright (c) 1996, 1997\
     36 	The NetBSD Foundation, Inc.  All rights reserved.");
     37 __RCSID("$NetBSD: ccdconfig.c,v 1.46.18.1 2008/06/02 13:21:20 mjf Exp $");
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/disklabel.h>
     43 #include <sys/disk.h>
     44 #include <sys/stat.h>
     45 #include <sys/sysctl.h>
     46 #include <ctype.h>
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <fcntl.h>
     50 #include <kvm.h>
     51 #include <limits.h>
     52 #include <nlist.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 #include <util.h>
     58 
     59 #include <dev/ccdvar.h>
     60 
     61 #include "pathnames.h"
     62 
     63 
     64 static	size_t lineno;
     65 static	gid_t egid;
     66 static	int verbose;
     67 static	const char *ccdconf = _PATH_CCDCONF;
     68 
     69 static	char *core;
     70 static	char *kernel;
     71 
     72 struct	flagval {
     73 	const char *fv_flag;
     74 	int	fv_val;
     75 } flagvaltab[] = {
     76 	{ "CCDF_UNIFORM",	CCDF_UNIFORM },
     77 	{ "CCDF_NOLABEL",	CCDF_NOLABEL },
     78 	{ NULL,			0 },
     79 };
     80 
     81 static	struct nlist nl[] = {
     82 	{ .n_name = "_ccd_softc" },
     83 #define SYM_CCDSOFTC		0
     84 	{ .n_name = "_numccd" },
     85 #define SYM_NUMCCD		1
     86 	{ .n_name = "_ccd_softc_elemsize" },
     87 #define SYM_CCDSOFTCELEMSIZE	2
     88 	{ .n_name = NULL },
     89 };
     90 
     91 #define CCD_CONFIG		0	/* configure a device */
     92 #define CCD_CONFIGALL		1	/* configure all devices */
     93 #define CCD_UNCONFIG		2	/* unconfigure a device */
     94 #define CCD_UNCONFIGALL		3	/* unconfigure all devices */
     95 #define CCD_DUMP		4	/* dump a ccd's configuration */
     96 
     97 static	int checkdev(char *);
     98 static	int do_io(char *, u_long, struct ccd_ioctl *);
     99 static	int do_single(int, char **, int);
    100 static	int do_all(int);
    101 static	int dump_ccd(int, char **, int);
    102 static	int flags_to_val(char *);
    103 static	int pathtounit(char *, int *);
    104 static	void print_ccd_info(struct ccd_softc *, kvm_t *);
    105 static	char *resolve_ccdname(char *);
    106 static	void usage(void);
    107 
    108 int
    109 main(int argc, char *argv[])
    110 {
    111 	int ch, options = 0, action = CCD_CONFIG;
    112 
    113 	egid = getegid();
    114 	setegid(getgid());
    115 	while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
    116 		switch (ch) {
    117 		case 'c':
    118 			action = CCD_CONFIG;
    119 			++options;
    120 			break;
    121 
    122 		case 'C':
    123 			action = CCD_CONFIGALL;
    124 			++options;
    125 			break;
    126 
    127 		case 'f':
    128 			ccdconf = optarg;
    129 			break;
    130 
    131 		case 'g':
    132 			action = CCD_DUMP;
    133 			break;
    134 
    135 		case 'M':
    136 			core = optarg;
    137 			break;
    138 
    139 		case 'N':
    140 			kernel = optarg;
    141 			break;
    142 
    143 		case 'u':
    144 			action = CCD_UNCONFIG;
    145 			++options;
    146 			break;
    147 
    148 		case 'U':
    149 			action = CCD_UNCONFIGALL;
    150 			++options;
    151 			break;
    152 
    153 		case 'v':
    154 			verbose = 1;
    155 			break;
    156 
    157 		default:
    158 			usage();
    159 		}
    160 	}
    161 	argc -= optind;
    162 	argv += optind;
    163 
    164 	if (options > 1)
    165 		usage();
    166 
    167 	/*
    168 	 * Discard setgid privileges.  If not the running kernel, we toss
    169 	 * them away totally so that bad guys can't print interesting stuff
    170 	 * from kernel memory, otherwise switch back to kmem for the
    171 	 * duration of the kvm_openfiles() call.
    172 	 *
    173 	 * We also do this if we aren't just looking...
    174 	 */
    175 	if (core != NULL || kernel != NULL || action != CCD_DUMP)
    176 		setgid(getgid());
    177 
    178 	switch (action) {
    179 		case CCD_CONFIG:
    180 		case CCD_UNCONFIG:
    181 			exit(do_single(argc, argv, action));
    182 			/* NOTREACHED */
    183 
    184 		case CCD_CONFIGALL:
    185 		case CCD_UNCONFIGALL:
    186 			exit(do_all(action));
    187 			/* NOTREACHED */
    188 
    189 		case CCD_DUMP:
    190 		default:
    191 			exit(dump_ccd(argc, argv, action));
    192 			/* NOTREACHED */
    193 	}
    194 	/* NOTREACHED */
    195 }
    196 
    197 static int
    198 do_single(int argc, char **argv, int action)
    199 {
    200 	struct ccd_ioctl ccio;
    201 	char *ccd, *cp, *cp2, **disks;
    202 	int noflags = 0, i, ileave, flags, j;
    203 
    204 	flags = 0;
    205 	memset(&ccio, 0, sizeof(ccio));
    206 
    207 	/*
    208 	 * If unconfiguring, all arguments are treated as ccds.
    209 	 */
    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;
    217 			}
    218 			if (do_io(ccd, CCDIOCCLR, &ccio))
    219 				i = 1;
    220 			else
    221 				if (verbose)
    222 					printf("%s unconfigured\n", cp);
    223 			free(ccd);
    224 		}
    225 		return (i);
    226 	}
    227 
    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();
    240 		}
    241 	}
    242 
    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);
    248 	}
    249 
    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);
    258 	}
    259 
    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);
    267 		}
    268 	}
    269 
    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);
    276 	}
    277 	for (i = 0; argc != 0; ) {
    278 		cp = *argv++; --argc;
    279 		if ((j = checkdev(cp)) == 0)
    280 			disks[i++] = cp;
    281 		else {
    282 			warnx("%s: %s", cp, strerror(j));
    283 			free(ccd);
    284 			free(disks);
    285 			return (1);
    286 		}
    287 	}
    288 
    289 	/* Fill in the ccio. */
    290 	ccio.ccio_disks = disks;
    291 	ccio.ccio_ndisks = i;
    292 	ccio.ccio_ileave = ileave;
    293 	ccio.ccio_flags = flags;
    294 
    295 	if (do_io(ccd, CCDIOCSET, &ccio)) {
    296 		free(ccd);
    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(ccd);
    321 	free(disks);
    322 	return (0);
    323 }
    324 
    325 static int
    326 do_all(int action)
    327 {
    328 	FILE *f;
    329 	char *line, *cp, *vp, **argv, **nargv;
    330 	int argc, rval;
    331 	size_t len;
    332 
    333 	rval = 0;
    334 
    335 	(void)setegid(getgid());
    336 	if ((f = fopen(ccdconf, "r")) == NULL) {
    337 		(void)setegid(egid);
    338 		warn("fopen: %s", ccdconf);
    339 		return (1);
    340 	}
    341 	(void)setegid(egid);
    342 
    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;
    349 
    350 		for (cp = line; cp != NULL; ) {
    351 			while ((vp = strsep(&cp, "\t ")) != NULL && *vp == '\0')
    352 				;
    353 			if (vp == NULL)
    354 				continue;
    355 
    356 			if ((nargv = realloc(argv,
    357 			    sizeof(char *) * (argc + 1))) == NULL) {
    358 				warnx("no memory to configure ccds");
    359 				return (1);
    360 			}
    361 			argv = nargv;
    362 			argc++;
    363 			argv[argc - 1] = vp;
    364 
    365 			/*
    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.
    371 			 */
    372 			if (action == CCD_UNCONFIGALL) {
    373 				if (do_single(argc, argv, action))
    374 					rval = 1;
    375 				goto end_of_line;
    376 			}
    377 		}
    378 		if (argc != 0)
    379 			if (do_single(argc, argv, action))
    380 				rval = 1;
    381 
    382  end_of_line:
    383 		if (argv != NULL)
    384 			free(argv);
    385 		free(line);
    386 	}
    387 
    388 	(void)fclose(f);
    389 	return (rval);
    390 }
    391 
    392 static int
    393 checkdev(char *path)
    394 {
    395 	struct stat st;
    396 
    397 	if (stat(path, &st) != 0)
    398 		return (errno);
    399 
    400 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    401 		return (EINVAL);
    402 
    403 	return (0);
    404 }
    405 
    406 static int
    407 pathtounit(char *path, int *unitp)
    408 {
    409 	struct stat st;
    410 
    411 	if (stat(path, &st) != 0)
    412 		return (errno);
    413 
    414 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    415 		return (EINVAL);
    416 
    417 	*unitp = DISKUNIT(st.st_rdev);
    418 
    419 	return (0);
    420 }
    421 
    422 static char *
    423 resolve_ccdname(char *name)
    424 {
    425 	char c, *path;
    426 	size_t len;
    427 	int rawpart;
    428 
    429 	if (name[0] == '/' || name[0] == '.') {
    430 		/* Assume they gave the correct pathname. */
    431 		return (strdup(name));
    432 	}
    433 
    434 	len = strlen(name);
    435 	c = name[len - 1];
    436 
    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);
    445 
    446 	return (path);
    447 }
    448 
    449 static int
    450 do_io(char *path, u_long cmd, struct ccd_ioctl *cciop)
    451 {
    452 	int fd;
    453 	const char *cp;
    454 
    455 	if ((fd = open(path, O_RDWR, 0640)) < 0) {
    456 		warn("open: %s", path);
    457 		return (1);
    458 	}
    459 
    460 	if (ioctl(fd, cmd, cciop) < 0) {
    461 		switch (cmd) {
    462 		case CCDIOCSET:
    463 			cp = "CCDIOCSET";
    464 			break;
    465 
    466 		case CCDIOCCLR:
    467 			cp = "CCDIOCCLR";
    468 			break;
    469 
    470 		default:
    471 			cp = "unknown";
    472 		}
    473 		warn("ioctl (%s): %s", cp, path);
    474 		return (1);
    475 	}
    476 
    477 	return (0);
    478 }
    479 
    480 #define KVM_ABORT(kd, str) {						\
    481 	(void)kvm_close((kd));						\
    482 	warnx("%s", (str));						\
    483 	warnx("%s", kvm_geterr((kd)));					\
    484 	return (1);							\
    485 }
    486 
    487 static int
    488 dump_ccd(int argc, char **argv, int action)
    489 {
    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;
    496 
    497 	memset(errbuf, 0, sizeof(errbuf));
    498 
    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);
    505 	}
    506 	(void)setgid(getgid());
    507 
    508 	if (kvm_nlist(kd, nl))
    509 		KVM_ABORT(kd, "ccd-related symbols not available");
    510 
    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");
    515 
    516 	if (numccd == 0) {
    517 		printf("ccd driver in kernel, but is uninitialized\n");
    518 		goto done;
    519 	}
    520 
    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");
    525 
    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;
    531 	}
    532 	memset(vcs, 0, readsize);
    533 
    534 	/*
    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.
    541 	 */
    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");
    546 	}
    547 	if (kvm_read(kd, (u_long)kcs, vcs, readsize) != readsize) {
    548 		free(vcs);
    549 		KVM_ABORT(kd, "can't read configuration data");
    550 	}
    551 
    552 	if ((cs = calloc(numccd, sizeof(struct ccd_softc))) == NULL) {
    553 		warnx("no memory for configuration data");
    554 		free(vcs);
    555 		goto bad;
    556 	}
    557 	for (i = 0; i < numccd; i++) {
    558 		memcpy(&cs[i], (char *)vcs + i * ccd_softc_elemsize,
    559 		    sizeof(struct ccd_softc));
    560 	}
    561 	free(vcs);
    562 
    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);
    569 			}
    570 
    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;
    579 			}
    580 			if ((error = pathtounit(ccd, &i)) != 0) {
    581 				warn("%s", ccd);
    582 				free(ccd);
    583 				continue;
    584 			}
    585 			if (i >= numccd) {
    586 				warnx("ccd%d not configured", i);
    587 				free(ccd);
    588 				continue;
    589 			}
    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);
    595 		}
    596 	}
    597 
    598 	free(cs);
    599 
    600  done:
    601 	(void)kvm_close(kd);
    602 	return (0);
    603 
    604  bad:
    605 	(void)kvm_close(kd);
    606 	return (1);
    607 }
    608 
    609 static void
    610 print_ccd_info(struct ccd_softc *cs, kvm_t *kd)
    611 {
    612 	static int header_printed = 0;
    613 	struct ccdcinfo *cip;
    614 	size_t readsize;
    615 	char path[MAXPATHLEN];
    616 	int i;
    617 
    618 	if (header_printed == 0 && verbose) {
    619 		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
    620 		header_printed = 1;
    621 	}
    622 
    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;
    628 	}
    629 	memset(cip, 0, readsize);
    630 
    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);
    635 
    636 	/* Read in the component info. */
    637 	if (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;
    643 	}
    644 
    645 	/* Read component pathname and display component info. */
    646 	for (i = 0; i < cs->sc_nccdisks; ++i) {
    647 		if (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;
    653 		}
    654 		fputs(path, stdout);
    655 		fputc((i + 1 < cs->sc_nccdisks) ? ' ' : '\n', stdout);
    656 		fflush(stdout);
    657 	}
    658 
    659  done:
    660 	free(cip);
    661 }
    662 
    663 static int
    664 flags_to_val(char *flags)
    665 {
    666 	char *cp, *tok;
    667 	int i, tmp, val = ~CCDF_USERMASK;
    668 	size_t flagslen;
    669 
    670 	/*
    671 	 * The most common case is that of NIL flags, so check for
    672 	 * those first.
    673 	 */
    674 	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
    675 	    strcmp("0", flags) == 0)
    676 		return (0);
    677 
    678 	flagslen = strlen(flags);
    679 
    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;
    691 		}
    692 		tmp |= flagvaltab[i].fv_val;
    693 	}
    694 
    695 	/* If we get here, the string was ok. */
    696 	free(cp);
    697 	val = tmp;
    698 	goto out;
    699 
    700  bad_string:
    701 
    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;
    709 	}
    710 
    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);
    716 
    717  out:
    718 	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
    719 }
    720 
    721 static void
    722 usage(void)
    723 {
    724 	const char *progname = getprogname();
    725 
    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);
    734 }
    735