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