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