Home | History | Annotate | Line # | Download | only in ccdconfig
ccdconfig.c revision 1.14
      1 /*	$NetBSD: ccdconfig.c,v 1.14 1997/09/14 08:39:43 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.14 1997/09/14 08:39:43 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[_POSIX2_LINE_MAX];
    335 	char *cp, **argv;
    336 	int argc, rval;
    337 
    338 	rval = 0;
    339 
    340 	if ((f = fopen(ccdconf, "r")) == NULL) {
    341 		warn("fopen: %s", ccdconf);
    342 		return (1);
    343 	}
    344 
    345 	while (fgets(line, sizeof(line), f) != NULL) {
    346 		argc = 0;
    347 		argv = NULL;
    348 		++lineno;
    349 		if ((cp = strrchr(line, '\n')) != NULL)
    350 			*cp = '\0';
    351 
    352 		/* Break up the line and pass it's contents to do_single(). */
    353 		if (line[0] == '\0')
    354 			goto end_of_line;
    355 		for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
    356 			if (*cp == '#')
    357 				break;
    358 			if ((argv = realloc(argv,
    359 			    sizeof(char *) * ++argc)) == NULL) {
    360 				warnx("no memory to configure ccds");
    361 				return (1);
    362 			}
    363 			argv[argc - 1] = cp;
    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 	}
    385 
    386 	(void)fclose(f);
    387 	return (rval);
    388 }
    389 
    390 static int
    391 checkdev(path)
    392 	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(path, unitp)
    407 	char *path;
    408 	int *unitp;
    409 {
    410 	struct stat st;
    411 	int maxpartitions;
    412 
    413 	if (stat(path, &st) != 0)
    414 		return (errno);
    415 
    416 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    417 		return (EINVAL);
    418 
    419 	if ((maxpartitions = getmaxpartitions()) < 0)
    420 		return (errno);
    421 
    422 	*unitp = minor(st.st_rdev) / maxpartitions;
    423 
    424 	return (0);
    425 }
    426 
    427 static char *
    428 resolve_ccdname(name)
    429 	char *name;
    430 {
    431 	char c, *path;
    432 	size_t len, newlen;
    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 	newlen = len + 8;
    444 	if ((path = malloc(newlen)) == NULL)
    445 		return (NULL);
    446 	memset(path, 0, newlen);
    447 
    448 	if (isdigit(c)) {
    449 		if ((rawpart = getrawpartition()) < 0) {
    450 			free(path);
    451 			return (NULL);
    452 		}
    453 		(void)snprintf(path, newlen, "/dev/%s%c", name, 'a' + rawpart);
    454 	} else
    455 		(void)snprintf(path, newlen, "/dev/%s", name);
    456 
    457 	return (path);
    458 }
    459 
    460 static int
    461 do_io(path, cmd, cciop)
    462 	char *path;
    463 	u_long cmd;
    464 	struct ccd_ioctl *cciop;
    465 {
    466 	int fd;
    467 	char *cp;
    468 
    469 	if ((fd = open(path, O_RDWR, 0640)) < 0) {
    470 		warn("open: %s", path);
    471 		return (1);
    472 	}
    473 
    474 	if (ioctl(fd, cmd, cciop) < 0) {
    475 		switch (cmd) {
    476 		case CCDIOCSET:
    477 			cp = "CCDIOCSET";
    478 			break;
    479 
    480 		case CCDIOCCLR:
    481 			cp = "CCDIOCCLR";
    482 			break;
    483 
    484 		default:
    485 			cp = "unknown";
    486 		}
    487 		warn("ioctl (%s): %s", cp, path);
    488 		return (1);
    489 	}
    490 
    491 	return (0);
    492 }
    493 
    494 #define KVM_ABORT(kd, str) {						\
    495 	(void)kvm_close((kd));						\
    496 	warnx((str));							\
    497 	warnx(kvm_geterr((kd)));					\
    498 	return (1);							\
    499 }
    500 
    501 static int
    502 dump_ccd(argc, argv, action)
    503 	int argc;
    504 	char **argv;
    505 	int action;
    506 {
    507 	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
    508 	struct ccd_softc *cs, *kcs;
    509 	size_t readsize;
    510 	int i, error, numccd, numconfiged = 0;
    511 	kvm_t *kd;
    512 
    513 	memset(errbuf, 0, sizeof(errbuf));
    514 
    515 	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
    516 	    errbuf)) == NULL) {
    517 		warnx("can't open kvm: %s", errbuf);
    518 		return (1);
    519 	}
    520 
    521 	if (kvm_nlist(kd, nl))
    522 		KVM_ABORT(kd, "ccd-related symbols not available");
    523 
    524 	/* Check to see how many ccds are currently configured. */
    525 	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
    526 	    sizeof(numccd)) != sizeof(numccd))
    527 		KVM_ABORT(kd, "can't determine number of configured ccds");
    528 
    529 	if (numccd == 0) {
    530 		printf("ccd driver in kernel, but is uninitialized\n");
    531 		goto done;
    532 	}
    533 
    534 	/* Allocate space for the configuration data. */
    535 	readsize = numccd * sizeof(struct ccd_softc);
    536 	if ((cs = malloc(readsize)) == NULL) {
    537 		warnx("no memory for configuration data");
    538 		goto bad;
    539 	}
    540 	memset(cs, 0, readsize);
    541 
    542 	/*
    543 	 * Read the ccd configuration data from the kernel and dump
    544 	 * it to stdout.
    545 	 */
    546 	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
    547 	    sizeof(kcs)) != sizeof(kcs)) {
    548 		free(cs);
    549 		KVM_ABORT(kd, "can't find pointer to configuration data");
    550 	}
    551 	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
    552 		free(cs);
    553 		KVM_ABORT(kd, "can't read configuration data");
    554 	}
    555 
    556 	if (argc == 0) {
    557 		for (i = 0; i < numccd; ++i)
    558 			if (cs[i].sc_flags & CCDF_INITED) {
    559 				++numconfiged;
    560 				if (action == CCD_STATS)
    561 					print_ccd_stats(&cs[i]);
    562 				else
    563 					print_ccd_info(&cs[i], kd);
    564 			}
    565 
    566 		if (numconfiged == 0)
    567 			printf("no concatenated disks configured\n");
    568 	} else {
    569 		while (argc) {
    570 			cp = *argv++; --argc;
    571 			if ((ccd = resolve_ccdname(cp)) == NULL) {
    572 				warnx("invalid ccd name: %s", cp);
    573 				continue;
    574 			}
    575 			if ((error = pathtounit(ccd, &i)) != 0) {
    576 				warnx("%s: %s", ccd, strerror(error));
    577 				continue;
    578 			}
    579 			if (i >= numccd) {
    580 				warnx("ccd%d not configured", i);
    581 				continue;
    582 			}
    583 			if (cs[i].sc_flags & CCDF_INITED) {
    584 				if (action == CCD_STATS)
    585 					print_ccd_stats(&cs[i]);
    586 				else
    587 					print_ccd_info(&cs[i], kd);
    588 			} else
    589 				printf("ccd%d not configured\n", i);
    590 		}
    591 	}
    592 
    593 	free(cs);
    594 
    595  done:
    596 	(void)kvm_close(kd);
    597 	return (0);
    598 
    599  bad:
    600 	(void)kvm_close(kd);
    601 	return (1);
    602 }
    603 
    604 static void
    605 print_ccd_info(cs, kd)
    606 	struct ccd_softc *cs;
    607 	kvm_t *kd;
    608 {
    609 	static int header_printed = 0;
    610 	struct ccdcinfo *cip;
    611 	size_t readsize;
    612 	char path[MAXPATHLEN];
    613 	int i;
    614 
    615 	if (header_printed == 0 && verbose) {
    616 		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
    617 		header_printed = 1;
    618 	}
    619 
    620 	readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
    621 	if ((cip = malloc(readsize)) == NULL) {
    622 		warn("ccd%d: can't allocate memory for component info",
    623 		    cs->sc_unit);
    624 		return;
    625 	}
    626 	memset(cip, 0, readsize);
    627 
    628 	/* Dump out softc information. */
    629 	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
    630 	    cs->sc_cflags & CCDF_USERMASK);
    631 	fflush(stdout);
    632 
    633 	/* Read in the component info. */
    634 	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
    635 	    readsize) != readsize) {
    636 		printf("\n");
    637 		warnx("can't read component info");
    638 		warnx(kvm_geterr(kd));
    639 		goto done;
    640 	}
    641 
    642 	/* Read component pathname and display component info. */
    643 	for (i = 0; i < cs->sc_nccdisks; ++i) {
    644 		if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
    645 		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
    646 			printf("\n");
    647 			warnx("can't read component pathname");
    648 			warnx(kvm_geterr(kd));
    649 			goto done;
    650 		}
    651 		printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
    652 		fflush(stdout);
    653 	}
    654 
    655  done:
    656 	free(cip);
    657 }
    658 
    659 static void
    660 print_ccd_stats(cs)
    661 	struct ccd_softc *cs;
    662 {
    663 
    664 	printf("Statistics for %s:\n", cs->sc_xname);
    665 	printf("\tfreelist count: %d\n", cs->sc_freecount);
    666 	printf("\tfreelist hiwater: %d\n", cs->sc_hiwat);
    667 	printf("\tfreelist misses: %lu\n", cs->sc_nmisses);
    668 	printf("\tccdbuf allocations: %lu\n", cs->sc_ngetbuf);
    669 }
    670 
    671 static int
    672 flags_to_val(flags)
    673 	char *flags;
    674 {
    675 	char *cp, *tok;
    676 	int i, tmp, val = ~CCDF_USERMASK;
    677 	size_t flagslen;
    678 
    679 	/*
    680 	 * The most common case is that of NIL flags, so check for
    681 	 * those first.
    682 	 */
    683 	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
    684 	    strcmp("0", flags) == 0)
    685 		return (0);
    686 
    687 	flagslen = strlen(flags);
    688 
    689 	/* Check for values represented by strings. */
    690 	if ((cp = strdup(flags)) == NULL)
    691 		err(1, "no memory to parse flags");
    692 	tmp = 0;
    693 	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
    694 		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
    695 			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
    696 				break;
    697 		if (flagvaltab[i].fv_flag == NULL) {
    698 			free(cp);
    699 			goto bad_string;
    700 		}
    701 		tmp |= flagvaltab[i].fv_val;
    702 	}
    703 
    704 	/* If we get here, the string was ok. */
    705 	free(cp);
    706 	val = tmp;
    707 	goto out;
    708 
    709  bad_string:
    710 
    711 	/* Check for values represented in hex. */
    712 	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
    713 		errno = 0;	/* to check for ERANGE */
    714 		val = (int)strtol(&flags[2], &cp, 16);
    715 		if ((errno == ERANGE) || (*cp != '\0'))
    716 			return (-1);
    717 		goto out;
    718 	}
    719 
    720 	/* Check for values represented in decimal. */
    721 	errno = 0;	/* to check for ERANGE */
    722 	val = (int)strtol(flags, &cp, 10);
    723 	if ((errno == ERANGE) || (*cp != '\0'))
    724 		return (-1);
    725 
    726  out:
    727 	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
    728 }
    729 
    730 static void
    731 usage()
    732 {
    733 
    734 	fprintf(stderr, "usage: %s [-cv] ccd ileave [flags] %s\n", __progname,
    735 	    "dev [...]");
    736 	fprintf(stderr, "       %s -C [-v] [-f config_file]\n", __progname);
    737 	fprintf(stderr, "       %s -u [-v] ccd [...]\n", __progname);
    738 	fprintf(stderr, "       %s -U [-v] [-f config_file]\n", __progname);
    739 	fprintf(stderr, "       %s -g [-M core] [-N system] %s\n", __progname,
    740 	    "[ccd [...]]");
    741 	fprintf(stderr, "       %s -s [-M core] [-N system] %s\n", __progname,
    742 	    "[ccd [...]]");
    743 	exit(1);
    744 }
    745