Home | History | Annotate | Line # | Download | only in ccdconfig
ccdconfig.c revision 1.15
      1 /*	$NetBSD: ccdconfig.c,v 1.15 1997/12/01 03:40:52 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.15 1997/12/01 03:40:52 lukem Exp $");
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/device.h>
     51 #include <sys/disk.h>
     52 #include <sys/stat.h>
     53 #include <sys/sysctl.h>
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <fcntl.h>
     58 #include <kvm.h>
     59 #include <limits.h>
     60 #include <nlist.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 #include <util.h>
     66 
     67 #include <dev/ccdvar.h>
     68 
     69 #include "pathnames.h"
     70 
     71 extern	char *__progname;
     72 
     73 static	int lineno = 0;
     74 static	int verbose = 0;
     75 static	char *ccdconf = _PATH_CCDCONF;
     76 
     77 static	char *core = NULL;
     78 static	char *kernel = NULL;
     79 
     80 struct	flagval {
     81 	char	*fv_flag;
     82 	int	fv_val;
     83 } flagvaltab[] = {
     84 	{ "CCDF_SWAP",		CCDF_SWAP },
     85 	{ "CCDF_UNIFORM",	CCDF_UNIFORM },
     86 	{ "CCDF_MIRROR",	CCDF_MIRROR },
     87 	{ NULL,			0 },
     88 };
     89 
     90 static	struct nlist nl[] = {
     91 	{ "_ccd_softc" },
     92 #define SYM_CCDSOFTC		0
     93 	{ "_numccd" },
     94 #define SYM_NUMCCD		1
     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 #define CCD_STATS		5	/* show a ccd's stats */
    104 
    105 int	main __P((int, char *[]));
    106 static	int checkdev __P((char *));
    107 static	int do_io __P((char *, u_long, struct ccd_ioctl *));
    108 static	int do_single __P((int, char **, int));
    109 static	int do_all __P((int));
    110 static	int dump_ccd __P((int, char **, int));
    111 static	int flags_to_val __P((char *));
    112 static	int pathtounit __P((char *, int *));
    113 static	void print_ccd_info __P((struct ccd_softc *, kvm_t *));
    114 static	void print_ccd_stats __P((struct ccd_softc *));
    115 static	char *resolve_ccdname __P((char *));
    116 static	void usage __P((void));
    117 
    118 int
    119 main(argc, argv)
    120 	int argc;
    121 	char *argv[];
    122 {
    123 	int ch, options = 0, action = CCD_CONFIG;
    124 
    125 	while ((ch = getopt(argc, argv, "cCf:gM:N:suUv")) != -1) {
    126 		switch (ch) {
    127 		case 'c':
    128 			action = CCD_CONFIG;
    129 			++options;
    130 			break;
    131 
    132 		case 'C':
    133 			action = CCD_CONFIGALL;
    134 			++options;
    135 			break;
    136 
    137 		case 'f':
    138 			ccdconf = optarg;
    139 			break;
    140 
    141 		case 'g':
    142 			action = CCD_DUMP;
    143 			break;
    144 
    145 		case 'M':
    146 			core = optarg;
    147 			break;
    148 
    149 		case 'N':
    150 			kernel = optarg;
    151 			break;
    152 
    153 		case 's':
    154 			action = CCD_STATS;
    155 			break;
    156 
    157 		case 'u':
    158 			action = CCD_UNCONFIG;
    159 			++options;
    160 			break;
    161 
    162 		case 'U':
    163 			action = CCD_UNCONFIGALL;
    164 			++options;
    165 			break;
    166 
    167 		case 'v':
    168 			verbose = 1;
    169 			break;
    170 
    171 		default:
    172 			usage();
    173 		}
    174 	}
    175 	argc -= optind;
    176 	argv += optind;
    177 
    178 	if (options > 1)
    179 		usage();
    180 
    181 	/*
    182 	 * Discard setgid privileges if not the running kernel so that bad
    183 	 * guys can't print interesting stuff from kernel memory.
    184 	 */
    185 	if (core != NULL || kernel != NULL)
    186 		setgid(getgid());
    187 
    188 	switch (action) {
    189 		case CCD_CONFIG:
    190 		case CCD_UNCONFIG:
    191 			exit(do_single(argc, argv, action));
    192 			/* NOTREACHED */
    193 
    194 		case CCD_CONFIGALL:
    195 		case CCD_UNCONFIGALL:
    196 			exit(do_all(action));
    197 			/* NOTREACHED */
    198 
    199 		case CCD_DUMP:
    200 		case CCD_STATS:
    201 		default:
    202 			exit(dump_ccd(argc, argv, action));
    203 			/* NOTREACHED */
    204 	}
    205 	/* NOTREACHED */
    206 }
    207 
    208 static int
    209 do_single(argc, argv, action)
    210 	int argc;
    211 	char **argv;
    212 	int action;
    213 {
    214 	struct ccd_ioctl ccio;
    215 	char *ccd, *cp, *cp2, **disks;
    216 	int noflags = 0, i, ileave, flags, j;
    217 
    218 	flags = 0;
    219 	memset(&ccio, 0, sizeof(ccio));
    220 
    221 	/*
    222 	 * If unconfiguring, all arguments are treated as ccds.
    223 	 */
    224 	if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
    225 		for (i = 0; argc != 0; ) {
    226 			cp = *argv++; --argc;
    227 			if ((ccd = resolve_ccdname(cp)) == NULL) {
    228 				warnx("invalid ccd name: %s", cp);
    229 				i = 1;
    230 				continue;
    231 			}
    232 			if (do_io(ccd, CCDIOCCLR, &ccio))
    233 				i = 1;
    234 			else
    235 				if (verbose)
    236 					printf("%s unconfigured\n", cp);
    237 		}
    238 		return (i);
    239 	}
    240 
    241 	/* Make sure there are enough arguments. */
    242 	if (argc < 4)
    243 		if (argc == 3) {
    244 			/* Assume that no flags are specified. */
    245 			noflags = 1;
    246 		} else {
    247 			if (action == CCD_CONFIGALL) {
    248 				warnx("%s: bad line: %d", ccdconf, lineno);
    249 				return (1);
    250 			} else
    251 				usage();
    252 		}
    253 
    254 	/* First argument is the ccd to configure. */
    255 	cp = *argv++; --argc;
    256 	if ((ccd = resolve_ccdname(cp)) == NULL) {
    257 		warnx("invalid ccd name: %s", cp);
    258 		return (1);
    259 	}
    260 
    261 	/* Next argument is the interleave factor. */
    262 	cp = *argv++; --argc;
    263 	errno = 0;	/* to check for ERANGE */
    264 	ileave = (int)strtol(cp, &cp2, 10);
    265 	if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
    266 		warnx("invalid interleave factor: %s", cp);
    267 		return (1);
    268 	}
    269 
    270 	if (noflags == 0) {
    271 		/* Next argument is the ccd configuration flags. */
    272 		cp = *argv++; --argc;
    273 		if ((flags = flags_to_val(cp)) < 0) {
    274 			warnx("invalid flags argument: %s", cp);
    275 			return (1);
    276 		}
    277 	}
    278 
    279 	/* Next is the list of disks to make the ccd from. */
    280 	disks = malloc(argc * sizeof(char *));
    281 	if (disks == NULL) {
    282 		warnx("no memory to configure ccd");
    283 		return (1);
    284 	}
    285 	for (i = 0; argc != 0; ) {
    286 		cp = *argv++; --argc;
    287 		if ((j = checkdev(cp)) == 0)
    288 			disks[i++] = cp;
    289 		else {
    290 			warnx("%s: %s", cp, strerror(j));
    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(disks);
    303 		return (1);
    304 	}
    305 
    306 	if (verbose) {
    307 		printf("ccd%d: %d components ", ccio.ccio_unit,
    308 		    ccio.ccio_ndisks);
    309 		for (i = 0; i < ccio.ccio_ndisks; ++i) {
    310 			if ((cp2 = strrchr(disks[i], '/')) != NULL)
    311 				++cp2;
    312 			else
    313 				cp2 = disks[i];
    314 			printf("%c%s%c",
    315 			    i == 0 ? '(' : ' ', cp2,
    316 			    i == ccio.ccio_ndisks - 1 ? ')' : ',');
    317 		}
    318 		printf(", %ld blocks ", (long)ccio.ccio_size);
    319 		if (ccio.ccio_ileave != 0)
    320 			printf("interleaved at %d blocks\n", ccio.ccio_ileave);
    321 		else
    322 			printf("concatenated\n");
    323 	}
    324 
    325 	free(disks);
    326 	return (0);
    327 }
    328 
    329 static int
    330 do_all(action)
    331 	int action;
    332 {
    333 	FILE *f;
    334 	char *line, *cp, *vp, **argv;
    335 	int argc, rval, len;
    336 
    337 	rval = 0;
    338 
    339 	if ((f = fopen(ccdconf, "r")) == NULL) {
    340 		warn("fopen: %s", ccdconf);
    341 		return (1);
    342 	}
    343 
    344 	while ((line = fparseln(f, &len, &lineno, "\\\\#", FPARSELN_UNESCALL))
    345 	    != NULL) {
    346 		argc = 0;
    347 		argv = NULL;
    348 		if (len == 0)
    349 			goto end_of_line;
    350 
    351 		for (cp = line; cp != NULL; ) {
    352 			while ((vp = strsep(&cp, "\t ")) != NULL && *vp == '\0')
    353 				;
    354 			if (vp == NULL)
    355 				continue;
    356 
    357 			if ((argv = realloc(argv,
    358 			    sizeof(char *) * ++argc)) == NULL) {
    359 				warnx("no memory to configure ccds");
    360 				return (1);
    361 			}
    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(path)
    393 	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(path, unitp)
    408 	char *path;
    409 	int *unitp;
    410 {
    411 	struct stat st;
    412 	int maxpartitions;
    413 
    414 	if (stat(path, &st) != 0)
    415 		return (errno);
    416 
    417 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    418 		return (EINVAL);
    419 
    420 	if ((maxpartitions = getmaxpartitions()) < 0)
    421 		return (errno);
    422 
    423 	*unitp = minor(st.st_rdev) / maxpartitions;
    424 
    425 	return (0);
    426 }
    427 
    428 static char *
    429 resolve_ccdname(name)
    430 	char *name;
    431 {
    432 	char c, *path;
    433 	size_t len, newlen;
    434 	int rawpart;
    435 
    436 	if (name[0] == '/' || name[0] == '.') {
    437 		/* Assume they gave the correct pathname. */
    438 		return (strdup(name));
    439 	}
    440 
    441 	len = strlen(name);
    442 	c = name[len - 1];
    443 
    444 	newlen = len + 8;
    445 	if ((path = malloc(newlen)) == NULL)
    446 		return (NULL);
    447 	memset(path, 0, newlen);
    448 
    449 	if (isdigit(c)) {
    450 		if ((rawpart = getrawpartition()) < 0) {
    451 			free(path);
    452 			return (NULL);
    453 		}
    454 		(void)snprintf(path, newlen, "/dev/%s%c", name, 'a' + rawpart);
    455 	} else
    456 		(void)snprintf(path, newlen, "/dev/%s", name);
    457 
    458 	return (path);
    459 }
    460 
    461 static int
    462 do_io(path, cmd, cciop)
    463 	char *path;
    464 	u_long cmd;
    465 	struct ccd_ioctl *cciop;
    466 {
    467 	int fd;
    468 	char *cp;
    469 
    470 	if ((fd = open(path, O_RDWR, 0640)) < 0) {
    471 		warn("open: %s", path);
    472 		return (1);
    473 	}
    474 
    475 	if (ioctl(fd, cmd, cciop) < 0) {
    476 		switch (cmd) {
    477 		case CCDIOCSET:
    478 			cp = "CCDIOCSET";
    479 			break;
    480 
    481 		case CCDIOCCLR:
    482 			cp = "CCDIOCCLR";
    483 			break;
    484 
    485 		default:
    486 			cp = "unknown";
    487 		}
    488 		warn("ioctl (%s): %s", cp, path);
    489 		return (1);
    490 	}
    491 
    492 	return (0);
    493 }
    494 
    495 #define KVM_ABORT(kd, str) {						\
    496 	(void)kvm_close((kd));						\
    497 	warnx((str));							\
    498 	warnx(kvm_geterr((kd)));					\
    499 	return (1);							\
    500 }
    501 
    502 static int
    503 dump_ccd(argc, argv, action)
    504 	int argc;
    505 	char **argv;
    506 	int action;
    507 {
    508 	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
    509 	struct ccd_softc *cs, *kcs;
    510 	size_t readsize;
    511 	int i, error, numccd, numconfiged = 0;
    512 	kvm_t *kd;
    513 
    514 	memset(errbuf, 0, sizeof(errbuf));
    515 
    516 	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
    517 	    errbuf)) == NULL) {
    518 		warnx("can't open kvm: %s", errbuf);
    519 		return (1);
    520 	}
    521 
    522 	if (kvm_nlist(kd, nl))
    523 		KVM_ABORT(kd, "ccd-related symbols not available");
    524 
    525 	/* Check to see how many ccds are currently configured. */
    526 	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
    527 	    sizeof(numccd)) != sizeof(numccd))
    528 		KVM_ABORT(kd, "can't determine number of configured ccds");
    529 
    530 	if (numccd == 0) {
    531 		printf("ccd driver in kernel, but is uninitialized\n");
    532 		goto done;
    533 	}
    534 
    535 	/* Allocate space for the configuration data. */
    536 	readsize = numccd * sizeof(struct ccd_softc);
    537 	if ((cs = malloc(readsize)) == NULL) {
    538 		warnx("no memory for configuration data");
    539 		goto bad;
    540 	}
    541 	memset(cs, 0, readsize);
    542 
    543 	/*
    544 	 * Read the ccd configuration data from the kernel and dump
    545 	 * it to stdout.
    546 	 */
    547 	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
    548 	    sizeof(kcs)) != sizeof(kcs)) {
    549 		free(cs);
    550 		KVM_ABORT(kd, "can't find pointer to configuration data");
    551 	}
    552 	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
    553 		free(cs);
    554 		KVM_ABORT(kd, "can't read configuration data");
    555 	}
    556 
    557 	if (argc == 0) {
    558 		for (i = 0; i < numccd; ++i)
    559 			if (cs[i].sc_flags & CCDF_INITED) {
    560 				++numconfiged;
    561 				if (action == CCD_STATS)
    562 					print_ccd_stats(&cs[i]);
    563 				else
    564 					print_ccd_info(&cs[i], kd);
    565 			}
    566 
    567 		if (numconfiged == 0)
    568 			printf("no concatenated disks configured\n");
    569 	} else {
    570 		while (argc) {
    571 			cp = *argv++; --argc;
    572 			if ((ccd = resolve_ccdname(cp)) == NULL) {
    573 				warnx("invalid ccd name: %s", cp);
    574 				continue;
    575 			}
    576 			if ((error = pathtounit(ccd, &i)) != 0) {
    577 				warnx("%s: %s", ccd, strerror(error));
    578 				continue;
    579 			}
    580 			if (i >= numccd) {
    581 				warnx("ccd%d not configured", i);
    582 				continue;
    583 			}
    584 			if (cs[i].sc_flags & CCDF_INITED) {
    585 				if (action == CCD_STATS)
    586 					print_ccd_stats(&cs[i]);
    587 				else
    588 					print_ccd_info(&cs[i], kd);
    589 			} else
    590 				printf("ccd%d not configured\n", i);
    591 		}
    592 	}
    593 
    594 	free(cs);
    595 
    596  done:
    597 	(void)kvm_close(kd);
    598 	return (0);
    599 
    600  bad:
    601 	(void)kvm_close(kd);
    602 	return (1);
    603 }
    604 
    605 static void
    606 print_ccd_info(cs, kd)
    607 	struct ccd_softc *cs;
    608 	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("ccd%d: can't allocate memory for component info",
    624 		    cs->sc_unit);
    625 		return;
    626 	}
    627 	memset(cip, 0, readsize);
    628 
    629 	/* Dump out softc information. */
    630 	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
    631 	    cs->sc_cflags & CCDF_USERMASK);
    632 	fflush(stdout);
    633 
    634 	/* Read in the component info. */
    635 	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
    636 	    readsize) != readsize) {
    637 		printf("\n");
    638 		warnx("can't read component info");
    639 		warnx(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, (char *)path,
    646 		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
    647 			printf("\n");
    648 			warnx("can't read component pathname");
    649 			warnx(kvm_geterr(kd));
    650 			goto done;
    651 		}
    652 		printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
    653 		fflush(stdout);
    654 	}
    655 
    656  done:
    657 	free(cip);
    658 }
    659 
    660 static void
    661 print_ccd_stats(cs)
    662 	struct ccd_softc *cs;
    663 {
    664 
    665 	printf("Statistics for %s:\n", cs->sc_xname);
    666 	printf("\tfreelist count: %d\n", cs->sc_freecount);
    667 	printf("\tfreelist hiwater: %d\n", cs->sc_hiwat);
    668 	printf("\tfreelist misses: %lu\n", cs->sc_nmisses);
    669 	printf("\tccdbuf allocations: %lu\n", cs->sc_ngetbuf);
    670 }
    671 
    672 static int
    673 flags_to_val(flags)
    674 	char *flags;
    675 {
    676 	char *cp, *tok;
    677 	int i, tmp, val = ~CCDF_USERMASK;
    678 	size_t flagslen;
    679 
    680 	/*
    681 	 * The most common case is that of NIL flags, so check for
    682 	 * those first.
    683 	 */
    684 	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
    685 	    strcmp("0", flags) == 0)
    686 		return (0);
    687 
    688 	flagslen = strlen(flags);
    689 
    690 	/* Check for values represented by strings. */
    691 	if ((cp = strdup(flags)) == NULL)
    692 		err(1, "no memory to parse flags");
    693 	tmp = 0;
    694 	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
    695 		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
    696 			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
    697 				break;
    698 		if (flagvaltab[i].fv_flag == NULL) {
    699 			free(cp);
    700 			goto bad_string;
    701 		}
    702 		tmp |= flagvaltab[i].fv_val;
    703 	}
    704 
    705 	/* If we get here, the string was ok. */
    706 	free(cp);
    707 	val = tmp;
    708 	goto out;
    709 
    710  bad_string:
    711 
    712 	/* Check for values represented in hex. */
    713 	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
    714 		errno = 0;	/* to check for ERANGE */
    715 		val = (int)strtol(&flags[2], &cp, 16);
    716 		if ((errno == ERANGE) || (*cp != '\0'))
    717 			return (-1);
    718 		goto out;
    719 	}
    720 
    721 	/* Check for values represented in decimal. */
    722 	errno = 0;	/* to check for ERANGE */
    723 	val = (int)strtol(flags, &cp, 10);
    724 	if ((errno == ERANGE) || (*cp != '\0'))
    725 		return (-1);
    726 
    727  out:
    728 	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
    729 }
    730 
    731 static void
    732 usage()
    733 {
    734 
    735 	fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] %s\n", __progname,
    736 	    "dev [...]");
    737 	fprintf(stderr, "       %s -C [-v] [-f config_file]\n", __progname);
    738 	fprintf(stderr, "       %s -u [-v] ccd [...]\n", __progname);
    739 	fprintf(stderr, "       %s -U [-v] [-f config_file]\n", __progname);
    740 	fprintf(stderr, "       %s -g [-M core] [-N system] %s\n", __progname,
    741 	    "[ccd [...]]");
    742 	fprintf(stderr, "       %s -s [-M core] [-N system] %s\n", __progname,
    743 	    "[ccd [...]]");
    744 	exit(1);
    745 }
    746