Home | History | Annotate | Line # | Download | only in ccdconfig
ccdconfig.c revision 1.43
      1 /*	$NetBSD: ccdconfig.c,v 1.43 2006/03/17 16:14:48 hubertf 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.43 2006/03/17 16:14:48 hubertf 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 		free(ccd);
    263 		return (1);
    264 	}
    265 
    266 	if (noflags == 0) {
    267 		/* Next argument is the ccd configuration flags. */
    268 		cp = *argv++; --argc;
    269 		if ((flags = flags_to_val(cp)) < 0) {
    270 			warnx("invalid flags argument: %s", cp);
    271 			free(ccd);
    272 			return (1);
    273 		}
    274 	}
    275 
    276 	/* Next is the list of disks to make the ccd from. */
    277 	disks = malloc(argc * sizeof(char *));
    278 	if (disks == NULL) {
    279 		warnx("no memory to configure ccd");
    280 		free(ccd);
    281 		return (1);
    282 	}
    283 	for (i = 0; argc != 0; ) {
    284 		cp = *argv++; --argc;
    285 		if ((j = checkdev(cp)) == 0)
    286 			disks[i++] = cp;
    287 		else {
    288 			warnx("%s: %s", cp, strerror(j));
    289 			free(ccd);
    290 			free(disks);
    291 			return (1);
    292 		}
    293 	}
    294 
    295 	/* Fill in the ccio. */
    296 	ccio.ccio_disks = disks;
    297 	ccio.ccio_ndisks = i;
    298 	ccio.ccio_ileave = ileave;
    299 	ccio.ccio_flags = flags;
    300 
    301 	if (do_io(ccd, CCDIOCSET, &ccio)) {
    302 		free(ccd);
    303 		free(disks);
    304 		return (1);
    305 	}
    306 
    307 	if (verbose) {
    308 		printf("ccd%d: %d components ", ccio.ccio_unit,
    309 		    ccio.ccio_ndisks);
    310 		for (i = 0; i < ccio.ccio_ndisks; ++i) {
    311 			if ((cp2 = strrchr(disks[i], '/')) != NULL)
    312 				++cp2;
    313 			else
    314 				cp2 = disks[i];
    315 			printf("%c%s%c",
    316 			    i == 0 ? '(' : ' ', cp2,
    317 			    i == ccio.ccio_ndisks - 1 ? ')' : ',');
    318 		}
    319 		printf(", %ld blocks ", (long)ccio.ccio_size);
    320 		if (ccio.ccio_ileave != 0)
    321 			printf("interleaved at %d blocks\n", ccio.ccio_ileave);
    322 		else
    323 			printf("concatenated\n");
    324 	}
    325 
    326 	free(ccd);
    327 	free(disks);
    328 	return (0);
    329 }
    330 
    331 static int
    332 do_all(int action)
    333 {
    334 	FILE *f;
    335 	char *line, *cp, *vp, **argv, **nargv;
    336 	int argc, rval;
    337 	size_t len;
    338 
    339 	rval = 0;
    340 
    341 	(void)setegid(getgid());
    342 	if ((f = fopen(ccdconf, "r")) == NULL) {
    343 		(void)setegid(egid);
    344 		warn("fopen: %s", ccdconf);
    345 		return (1);
    346 	}
    347 	(void)setegid(egid);
    348 
    349 	while ((line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL))
    350 	    != NULL) {
    351 		argc = 0;
    352 		argv = NULL;
    353 		if (len == 0)
    354 			goto end_of_line;
    355 
    356 		for (cp = line; cp != NULL; ) {
    357 			while ((vp = strsep(&cp, "\t ")) != NULL && *vp == '\0')
    358 				;
    359 			if (vp == NULL)
    360 				continue;
    361 
    362 			if ((nargv = realloc(argv,
    363 			    sizeof(char *) * (argc + 1))) == NULL) {
    364 				warnx("no memory to configure ccds");
    365 				return (1);
    366 			}
    367 			argv = nargv;
    368 			argc++;
    369 			argv[argc - 1] = vp;
    370 
    371 			/*
    372 			 * If our action is to unconfigure all, then pass
    373 			 * just the first token to do_single() and ignore
    374 			 * the rest.  Since this will be encountered on
    375 			 * our first pass through the line, the Right
    376 			 * Thing will happen.
    377 			 */
    378 			if (action == CCD_UNCONFIGALL) {
    379 				if (do_single(argc, argv, action))
    380 					rval = 1;
    381 				goto end_of_line;
    382 			}
    383 		}
    384 		if (argc != 0)
    385 			if (do_single(argc, argv, action))
    386 				rval = 1;
    387 
    388  end_of_line:
    389 		if (argv != NULL)
    390 			free(argv);
    391 		free(line);
    392 	}
    393 
    394 	(void)fclose(f);
    395 	return (rval);
    396 }
    397 
    398 static int
    399 checkdev(char *path)
    400 {
    401 	struct stat st;
    402 
    403 	if (stat(path, &st) != 0)
    404 		return (errno);
    405 
    406 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    407 		return (EINVAL);
    408 
    409 	return (0);
    410 }
    411 
    412 static int
    413 pathtounit(char *path, int *unitp)
    414 {
    415 	struct stat st;
    416 
    417 	if (stat(path, &st) != 0)
    418 		return (errno);
    419 
    420 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    421 		return (EINVAL);
    422 
    423 	*unitp = DISKUNIT(st.st_rdev);
    424 
    425 	return (0);
    426 }
    427 
    428 static char *
    429 resolve_ccdname(char *name)
    430 {
    431 	char c, *path;
    432 	size_t len;
    433 	int rawpart;
    434 
    435 	if (name[0] == '/' || name[0] == '.') {
    436 		/* Assume they gave the correct pathname. */
    437 		return (strdup(name));
    438 	}
    439 
    440 	len = strlen(name);
    441 	c = name[len - 1];
    442 
    443 	if (isdigit((unsigned char)c)) {
    444 		if ((rawpart = getrawpartition()) < 0)
    445 			return (NULL);
    446 		if (asprintf(&path, "/dev/%s%c", name, 'a' + rawpart) < 0)
    447 			return (NULL);
    448 	} else
    449 		if (asprintf(&path, "/dev/%s", name) < 0)
    450 			return (NULL);
    451 
    452 	return (path);
    453 }
    454 
    455 static int
    456 do_io(char *path, u_long cmd, struct ccd_ioctl *cciop)
    457 {
    458 	int fd;
    459 	const char *cp;
    460 
    461 	if ((fd = open(path, O_RDWR, 0640)) < 0) {
    462 		warn("open: %s", path);
    463 		return (1);
    464 	}
    465 
    466 	if (ioctl(fd, cmd, cciop) < 0) {
    467 		switch (cmd) {
    468 		case CCDIOCSET:
    469 			cp = "CCDIOCSET";
    470 			break;
    471 
    472 		case CCDIOCCLR:
    473 			cp = "CCDIOCCLR";
    474 			break;
    475 
    476 		default:
    477 			cp = "unknown";
    478 		}
    479 		warn("ioctl (%s): %s", cp, path);
    480 		return (1);
    481 	}
    482 
    483 	return (0);
    484 }
    485 
    486 #define KVM_ABORT(kd, str) {						\
    487 	(void)kvm_close((kd));						\
    488 	warnx("%s", (str));						\
    489 	warnx("%s", kvm_geterr((kd)));					\
    490 	return (1);							\
    491 }
    492 
    493 static int
    494 dump_ccd(int argc, char **argv, int action)
    495 {
    496 	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
    497 	struct ccd_softc *cs, *kcs;
    498 	void *vcs;
    499 	size_t readsize;
    500 	int i, error, numccd, ccd_softc_elemsize, numconfiged = 0;
    501 	kvm_t *kd;
    502 
    503 	memset(errbuf, 0, sizeof(errbuf));
    504 
    505 	vcs = NULL;
    506 	(void)setegid(egid);
    507 	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
    508 	    errbuf)) == NULL) {
    509 		warnx("can't open kvm: %s", errbuf);
    510 		return (1);
    511 	}
    512 	(void)setgid(getgid());
    513 
    514 	if (kvm_nlist(kd, nl))
    515 		KVM_ABORT(kd, "ccd-related symbols not available");
    516 
    517 	/* Check to see how many ccds are currently configured. */
    518 	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (void *)&numccd,
    519 	    sizeof(numccd)) != sizeof(numccd))
    520 		KVM_ABORT(kd, "can't determine number of configured ccds");
    521 
    522 	if (numccd == 0) {
    523 		printf("ccd driver in kernel, but is uninitialized\n");
    524 		goto done;
    525 	}
    526 
    527 	if (kvm_read(kd, nl[SYM_CCDSOFTCELEMSIZE].n_value,
    528 	    (void *)&ccd_softc_elemsize, sizeof(ccd_softc_elemsize))
    529 	    != sizeof(ccd_softc_elemsize))
    530 		KVM_ABORT(kd, "can't determine size of ccd_softc");
    531 
    532 	/* Allocate space for the kernel's configuration data. */
    533 	readsize = numccd * ccd_softc_elemsize;
    534 	if ((vcs = malloc(readsize)) == NULL) {
    535 		warnx("no memory for configuration data");
    536 		goto bad;
    537 	}
    538 	memset(vcs, 0, readsize);
    539 
    540 	/*
    541 	 * Read the ccd configuration data from the kernel.
    542 	 * The kernel's ccd_softc is larger than userland's
    543 	 * (the former contains extra structure members at
    544 	 * the end of the structure), so read the kernel
    545 	 * ccd_softcs into a temporary buffer and convert
    546 	 * into userland ccd_softcs.
    547 	 */
    548 	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (void *)&kcs,
    549 	    sizeof(kcs)) != sizeof(kcs)) {
    550 		free(vcs);
    551 		KVM_ABORT(kd, "can't find pointer to configuration data");
    552 	}
    553 	if (kvm_read(kd, (u_long)kcs, vcs, readsize) != readsize) {
    554 		free(vcs);
    555 		KVM_ABORT(kd, "can't read configuration data");
    556 	}
    557 
    558 	if ((cs = calloc(numccd, sizeof(struct ccd_softc))) == NULL) {
    559 		warnx("no memory for configuration data");
    560 		goto bad;
    561 	}
    562 	for (i = 0; i < numccd; i++) {
    563 		memcpy(&cs[i], (char *)vcs + i * ccd_softc_elemsize,
    564 		    sizeof(struct ccd_softc));
    565 	}
    566 	free(vcs);
    567 
    568 	/* Dump ccd configuration to stdout. */
    569 	if (argc == 0) {
    570 		for (i = 0; i < numccd; ++i)
    571 			if (cs[i].sc_flags & CCDF_INITED) {
    572 				++numconfiged;
    573 				print_ccd_info(&cs[i], kd);
    574 			}
    575 
    576 		if (numconfiged == 0)
    577 			printf("# no concatenated disks configured\n");
    578 	} else {
    579 		while (argc) {
    580 			cp = *argv++; --argc;
    581 			if ((ccd = resolve_ccdname(cp)) == NULL) {
    582 				warnx("invalid ccd name: %s", cp);
    583 				free(ccd);
    584 				continue;
    585 			}
    586 			if ((error = pathtounit(ccd, &i)) != 0) {
    587 				warn("%s", ccd);
    588 				free(ccd);
    589 				continue;
    590 			}
    591 			if (i >= numccd) {
    592 				warnx("ccd%d not configured", i);
    593 				free(ccd);
    594 				continue;
    595 			}
    596 			if (cs[i].sc_flags & CCDF_INITED)
    597 				print_ccd_info(&cs[i], kd);
    598 			else
    599 				printf("# ccd%d not configured\n", i);
    600 		}
    601 	}
    602 
    603 	free(cs);
    604 
    605  done:
    606 	(void)kvm_close(kd);
    607 	return (0);
    608 
    609  bad:
    610 	(void)kvm_close(kd);
    611 	return (1);
    612 }
    613 
    614 static void
    615 print_ccd_info(struct ccd_softc *cs, kvm_t *kd)
    616 {
    617 	static int header_printed = 0;
    618 	struct ccdcinfo *cip;
    619 	size_t readsize;
    620 	char path[MAXPATHLEN];
    621 	int i;
    622 
    623 	if (header_printed == 0 && verbose) {
    624 		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
    625 		header_printed = 1;
    626 	}
    627 
    628 	readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
    629 	if ((cip = malloc(readsize)) == NULL) {
    630 		warn("%s: can't allocate memory for component info",
    631 		    cs->sc_xname);
    632 		return;
    633 	}
    634 	memset(cip, 0, readsize);
    635 
    636 	/* Dump out softc information. */
    637 	printf("%s\t\t%d\t%d\t", cs->sc_xname, cs->sc_ileave,
    638 	    cs->sc_flags & CCDF_USERMASK);
    639 	fflush(stdout);
    640 
    641 	/* Read in the component info. */
    642 	if (kvm_read(kd, (u_long)cs->sc_cinfo, (void *)cip,
    643 	    readsize) != readsize) {
    644 		printf("\n");
    645 		warnx("can't read component info");
    646 		warnx("%s", kvm_geterr(kd));
    647 		goto done;
    648 	}
    649 
    650 	/* Read component pathname and display component info. */
    651 	for (i = 0; i < cs->sc_nccdisks; ++i) {
    652 		if (kvm_read(kd, (u_long)cip[i].ci_path, (void *)path,
    653 		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
    654 			printf("\n");
    655 			warnx("can't read component pathname");
    656 			warnx("%s", kvm_geterr(kd));
    657 			goto done;
    658 		}
    659 		fputs(path, stdout);
    660 		fputc((i + 1 < cs->sc_nccdisks) ? ' ' : '\n', stdout);
    661 		fflush(stdout);
    662 	}
    663 
    664  done:
    665 	free(cip);
    666 }
    667 
    668 static int
    669 flags_to_val(char *flags)
    670 {
    671 	char *cp, *tok;
    672 	int i, tmp, val = ~CCDF_USERMASK;
    673 	size_t flagslen;
    674 
    675 	/*
    676 	 * The most common case is that of NIL flags, so check for
    677 	 * those first.
    678 	 */
    679 	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
    680 	    strcmp("0", flags) == 0)
    681 		return (0);
    682 
    683 	flagslen = strlen(flags);
    684 
    685 	/* Check for values represented by strings. */
    686 	if ((cp = strdup(flags)) == NULL)
    687 		err(1, "no memory to parse flags");
    688 	tmp = 0;
    689 	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
    690 		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
    691 			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
    692 				break;
    693 		if (flagvaltab[i].fv_flag == NULL) {
    694 			free(cp);
    695 			goto bad_string;
    696 		}
    697 		tmp |= flagvaltab[i].fv_val;
    698 	}
    699 
    700 	/* If we get here, the string was ok. */
    701 	free(cp);
    702 	val = tmp;
    703 	goto out;
    704 
    705  bad_string:
    706 
    707 	/* Check for values represented in hex. */
    708 	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
    709 		errno = 0;	/* to check for ERANGE */
    710 		val = (int)strtol(&flags[2], &cp, 16);
    711 		if ((errno == ERANGE) || (*cp != '\0'))
    712 			return (-1);
    713 		goto out;
    714 	}
    715 
    716 	/* Check for values represented in decimal. */
    717 	errno = 0;	/* to check for ERANGE */
    718 	val = (int)strtol(flags, &cp, 10);
    719 	if ((errno == ERANGE) || (*cp != '\0'))
    720 		return (-1);
    721 
    722  out:
    723 	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
    724 }
    725 
    726 static void
    727 usage(void)
    728 {
    729 	const char *progname = getprogname();
    730 
    731 	fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] dev [...]\n",
    732 	    progname);
    733 	fprintf(stderr, "       %s -C [-v] [-f config_file]\n", progname);
    734 	fprintf(stderr, "       %s -u [-v] ccd [...]\n", progname);
    735 	fprintf(stderr, "       %s -U [-v] [-f config_file]\n", progname);
    736 	fprintf(stderr, "       %s -g [-M core] [-N system] [ccd [...]]\n",
    737 	    progname);
    738 	exit(1);
    739 }
    740