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