ccdconfig.c revision 109417
113044Sasami/*	$NetBSD: ccdconfig.c,v 1.2.2.1 1995/11/11 02:43:35 thorpej Exp $	*/
213044Sasami
313044Sasami/*
413044Sasami * Copyright (c) 1995 Jason R. Thorpe.
513044Sasami * All rights reserved.
613044Sasami *
713044Sasami * Redistribution and use in source and binary forms, with or without
813044Sasami * modification, are permitted provided that the following conditions
913044Sasami * are met:
1013044Sasami * 1. Redistributions of source code must retain the above copyright
1113044Sasami *    notice, this list of conditions and the following disclaimer.
1213044Sasami * 2. Redistributions in binary form must reproduce the above copyright
1313044Sasami *    notice, this list of conditions and the following disclaimer in the
1413044Sasami *    documentation and/or other materials provided with the distribution.
1513044Sasami * 3. All advertising materials mentioning features or use of this software
1613044Sasami *    must display the following acknowledgement:
1713044Sasami *	This product includes software developed for the NetBSD Project
1813044Sasami *	by Jason R. Thorpe.
1913044Sasami * 4. The name of the author may not be used to endorse or promote products
2013044Sasami *    derived from this software without specific prior written permission.
2113044Sasami *
2213044Sasami * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2313044Sasami * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2413044Sasami * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2513044Sasami * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2613044Sasami * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2713044Sasami * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2813044Sasami * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2913044Sasami * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3013044Sasami * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3113044Sasami * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3213044Sasami * SUCH DAMAGE.
3313044Sasami */
3413044Sasami
3536628Scharnier#ifndef lint
3636628Scharnierstatic const char rcsid[] =
3750476Speter  "$FreeBSD: head/sbin/ccdconfig/ccdconfig.c 109417 2003-01-17 13:23:41Z phk $";
3836628Scharnier#endif /* not lint */
3936628Scharnier
4013044Sasami#include <sys/param.h>
4148568Sbillf#include <sys/linker.h>
4213044Sasami#include <sys/disklabel.h>
4313044Sasami#include <sys/stat.h>
4445329Speter#include <sys/module.h>
4513044Sasami#include <ctype.h>
4613044Sasami#include <err.h>
4713044Sasami#include <errno.h>
4813044Sasami#include <fcntl.h>
4913044Sasami#include <limits.h>
5069793Sobrien#include <paths.h>
5113044Sasami#include <stdio.h>
5213044Sasami#include <stdlib.h>
5313044Sasami#include <string.h>
5413044Sasami#include <unistd.h>
5513044Sasami
5639228Sgibbs#include <sys/devicestat.h>
5713052Sasami#include <sys/ccdvar.h>
5813044Sasami
5913044Sasami#include "pathnames.h"
6013044Sasami
6113044Sasamistatic	int lineno = 0;
6213044Sasamistatic	int verbose = 0;
63109417Sphkstatic	const char *ccdconf = _PATH_CCDCONF;
6413044Sasami
6513044Sasamistruct	flagval {
66109417Sphk	const char	*fv_flag;
6713044Sasami	int	fv_val;
6813044Sasami} flagvaltab[] = {
6913044Sasami	{ "CCDF_UNIFORM",	CCDF_UNIFORM },
7013762Sasami	{ "CCDF_MIRROR",	CCDF_MIRROR },
7113044Sasami	{ NULL,			0 },
7213044Sasami};
7313044Sasami
7413044Sasami#define CCD_CONFIG		0	/* configure a device */
7513044Sasami#define CCD_CONFIGALL		1	/* configure all devices */
7613044Sasami#define CCD_UNCONFIG		2	/* unconfigure a device */
7713044Sasami#define CCD_UNCONFIGALL		3	/* unconfigure all devices */
7813044Sasami#define CCD_DUMP		4	/* dump a ccd's configuration */
7913044Sasami
8092539Simpstatic	int checkdev(char *);
81109417Sphkstatic	int do_io(int, u_long, struct ccd_ioctl *);
8292539Simpstatic	int do_single(int, char **, int);
8392539Simpstatic	int do_all(int);
8492539Simpstatic	int dump_ccd(int, char **);
8592539Simpstatic	int flags_to_val(char *);
8692539Simpstatic	void print_ccd_info(struct ccd_s *);
87109417Sphkstatic	int resolve_ccdname(char *);
8892539Simpstatic	void usage(void);
8913044Sasami
9013044Sasamiint
9192539Simpmain(int argc, char *argv[])
9213044Sasami{
9313044Sasami	int ch, options = 0, action = CCD_CONFIG;
9413044Sasami
9583329Sru	while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
9613044Sasami		switch (ch) {
9713044Sasami		case 'c':
9813044Sasami			action = CCD_CONFIG;
9913044Sasami			++options;
10013044Sasami			break;
10113044Sasami
10213044Sasami		case 'C':
10313044Sasami			action = CCD_CONFIGALL;
10413044Sasami			++options;
10513044Sasami			break;
10613044Sasami
10713044Sasami		case 'f':
10813044Sasami			ccdconf = optarg;
10913044Sasami			break;
11013044Sasami
11113044Sasami		case 'g':
11213044Sasami			action = CCD_DUMP;
11313044Sasami			break;
11413044Sasami
11513044Sasami		case 'u':
11613044Sasami			action = CCD_UNCONFIG;
11713044Sasami			++options;
11813044Sasami			break;
11913044Sasami
12013044Sasami		case 'U':
12113044Sasami			action = CCD_UNCONFIGALL;
12213044Sasami			++options;
12313044Sasami			break;
12413044Sasami
12513044Sasami		case 'v':
12613044Sasami			verbose = 1;
12713044Sasami			break;
12813044Sasami
12913044Sasami		default:
13013044Sasami			usage();
13113044Sasami		}
13213044Sasami	}
13313044Sasami	argc -= optind;
13413044Sasami	argv += optind;
13513044Sasami
13613044Sasami	if (options > 1)
13713044Sasami		usage();
13813044Sasami
13945329Speter	if (modfind("ccd") < 0) {
14045329Speter		/* Not present in kernel, try loading it */
14145329Speter		if (kldload("ccd") < 0 || modfind("ccd") < 0)
14245329Speter			warn("ccd module not available!");
14345329Speter	}
14445329Speter
14513044Sasami	switch (action) {
14613044Sasami		case CCD_CONFIG:
14713044Sasami		case CCD_UNCONFIG:
14813044Sasami			exit(do_single(argc, argv, action));
14913044Sasami			/* NOTREACHED */
15013044Sasami
15113044Sasami		case CCD_CONFIGALL:
15213044Sasami		case CCD_UNCONFIGALL:
15313044Sasami			exit(do_all(action));
15413044Sasami			/* NOTREACHED */
15513044Sasami
15613044Sasami		case CCD_DUMP:
15713044Sasami			exit(dump_ccd(argc, argv));
15813044Sasami			/* NOTREACHED */
15913044Sasami	}
16013044Sasami	/* NOTREACHED */
16136628Scharnier	return (0);
16213044Sasami}
16313044Sasami
16413044Sasamistatic int
16592539Simpdo_single(int argc, char **argv, int action)
16613044Sasami{
16713044Sasami	struct ccd_ioctl ccio;
168109417Sphk	char *cp, *cp2, **disks;
169109417Sphk	int ccd, noflags = 0, i, ileave, flags = 0, j;
170109417Sphk	u_int u;
17113044Sasami
17213044Sasami	bzero(&ccio, sizeof(ccio));
17313044Sasami
17413044Sasami	/*
17513044Sasami	 * If unconfiguring, all arguments are treated as ccds.
17613044Sasami	 */
17713044Sasami	if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
17813044Sasami		for (i = 0; argc != 0; ) {
17913044Sasami			cp = *argv++; --argc;
18013044Sasami			if ((ccd = resolve_ccdname(cp)) == NULL) {
18113044Sasami				warnx("invalid ccd name: %s", cp);
18213044Sasami				i = 1;
18313044Sasami				continue;
18413044Sasami			}
18513044Sasami			if (do_io(ccd, CCDIOCCLR, &ccio))
18613044Sasami				i = 1;
18713044Sasami			else
18813044Sasami				if (verbose)
18913044Sasami					printf("%s unconfigured\n", cp);
19013044Sasami		}
19113044Sasami		return (i);
19213044Sasami	}
19313044Sasami
19413044Sasami	/* Make sure there are enough arguments. */
19548568Sbillf	if (argc < 4) {
19613044Sasami		if (argc == 3) {
19713044Sasami			/* Assume that no flags are specified. */
19813044Sasami			noflags = 1;
19913044Sasami		} else {
20013044Sasami			if (action == CCD_CONFIGALL) {
20113044Sasami				warnx("%s: bad line: %d", ccdconf, lineno);
20213044Sasami				return (1);
20313044Sasami			} else
20413044Sasami				usage();
20513044Sasami		}
20648568Sbillf	}
20713044Sasami
20813044Sasami	/* First argument is the ccd to configure. */
20913044Sasami	cp = *argv++; --argc;
21013044Sasami	if ((ccd = resolve_ccdname(cp)) == NULL) {
21113044Sasami		warnx("invalid ccd name: %s", cp);
21213044Sasami		return (1);
21313044Sasami	}
21413044Sasami
21513044Sasami	/* Next argument is the interleave factor. */
21613044Sasami	cp = *argv++; --argc;
21713044Sasami	errno = 0;	/* to check for ERANGE */
21813044Sasami	ileave = (int)strtol(cp, &cp2, 10);
21913044Sasami	if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
22013044Sasami		warnx("invalid interleave factor: %s", cp);
22113044Sasami		return (1);
22213044Sasami	}
22313044Sasami
22413044Sasami	if (noflags == 0) {
22513044Sasami		/* Next argument is the ccd configuration flags. */
22613044Sasami		cp = *argv++; --argc;
22713044Sasami		if ((flags = flags_to_val(cp)) < 0) {
22813044Sasami			warnx("invalid flags argument: %s", cp);
22913044Sasami			return (1);
23013044Sasami		}
23113044Sasami	}
23213044Sasami
23313044Sasami	/* Next is the list of disks to make the ccd from. */
23413044Sasami	disks = malloc(argc * sizeof(char *));
23513044Sasami	if (disks == NULL) {
23613044Sasami		warnx("no memory to configure ccd");
23713044Sasami		return (1);
23813044Sasami	}
23913044Sasami	for (i = 0; argc != 0; ) {
24013044Sasami		cp = *argv++; --argc;
24113044Sasami		if ((j = checkdev(cp)) == 0)
24213044Sasami			disks[i++] = cp;
24313044Sasami		else {
24413044Sasami			warnx("%s: %s", cp, strerror(j));
24513044Sasami			return (1);
24613044Sasami		}
24713044Sasami	}
24813044Sasami
24913044Sasami	/* Fill in the ccio. */
25013044Sasami	ccio.ccio_disks = disks;
25113044Sasami	ccio.ccio_ndisks = i;
25213044Sasami	ccio.ccio_ileave = ileave;
25313044Sasami	ccio.ccio_flags = flags;
25413044Sasami
25513044Sasami	if (do_io(ccd, CCDIOCSET, &ccio)) {
25613044Sasami		free(disks);
25713044Sasami		return (1);
25813044Sasami	}
25913044Sasami
26013044Sasami	if (verbose) {
26113044Sasami		printf("ccd%d: %d components ", ccio.ccio_unit,
26213044Sasami		    ccio.ccio_ndisks);
263109417Sphk		for (u = 0; u < ccio.ccio_ndisks; ++u) {
264109417Sphk			if ((cp2 = strrchr(disks[u], '/')) != NULL)
26513044Sasami				++cp2;
26613044Sasami			else
267109417Sphk				cp2 = disks[u];
26813044Sasami			printf("%c%s%c",
269109417Sphk			    u == 0 ? '(' : ' ', cp2,
270109417Sphk			    u == ccio.ccio_ndisks - 1 ? ')' : ',');
27113044Sasami		}
27248568Sbillf		printf(", %lu blocks ", (u_long)ccio.ccio_size);
27313044Sasami		if (ccio.ccio_ileave != 0)
27413044Sasami			printf("interleaved at %d blocks\n", ccio.ccio_ileave);
27513044Sasami		else
27613044Sasami			printf("concatenated\n");
27713044Sasami	}
27813044Sasami
27913044Sasami	free(disks);
28013044Sasami	return (0);
28113044Sasami}
28213044Sasami
28313044Sasamistatic int
28492539Simpdo_all(int action)
28513044Sasami{
28613044Sasami	FILE *f;
28713044Sasami	char line[_POSIX2_LINE_MAX];
28813044Sasami	char *cp, **argv;
28913044Sasami	int argc, rval;
29032116Simp	gid_t egid;
29113044Sasami
29251690Sbillf	rval = 0;
29332116Simp	egid = getegid();
29432116Simp	setegid(getgid());
29513044Sasami	if ((f = fopen(ccdconf, "r")) == NULL) {
29632116Simp		setegid(egid);
29713044Sasami		warn("fopen: %s", ccdconf);
29813044Sasami		return (1);
29913044Sasami	}
30032116Simp	setegid(egid);
30113044Sasami
30213044Sasami	while (fgets(line, sizeof(line), f) != NULL) {
30313044Sasami		argc = 0;
30413044Sasami		argv = NULL;
30513044Sasami		++lineno;
30613044Sasami		if ((cp = strrchr(line, '\n')) != NULL)
30713044Sasami			*cp = '\0';
30813044Sasami
30913044Sasami		/* Break up the line and pass it's contents to do_single(). */
31013044Sasami		if (line[0] == '\0')
31113044Sasami			goto end_of_line;
31213044Sasami		for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
31313044Sasami			if (*cp == '#')
31413044Sasami				break;
31513044Sasami			if ((argv = realloc(argv,
31613044Sasami			    sizeof(char *) * ++argc)) == NULL) {
31713044Sasami				warnx("no memory to configure ccds");
31813044Sasami				return (1);
31913044Sasami			}
32013044Sasami			argv[argc - 1] = cp;
32113044Sasami			/*
32213044Sasami			 * If our action is to unconfigure all, then pass
32313044Sasami			 * just the first token to do_single() and ignore
32413044Sasami			 * the rest.  Since this will be encountered on
32513044Sasami			 * our first pass through the line, the Right
32613044Sasami			 * Thing will happen.
32713044Sasami			 */
32813044Sasami			if (action == CCD_UNCONFIGALL) {
32913044Sasami				if (do_single(argc, argv, action))
33013044Sasami					rval = 1;
33113044Sasami				goto end_of_line;
33213044Sasami			}
33313044Sasami		}
33413044Sasami		if (argc != 0)
33513044Sasami			if (do_single(argc, argv, action))
33613044Sasami				rval = 1;
33713044Sasami
33813044Sasami end_of_line:
33913044Sasami		if (argv != NULL)
34013044Sasami			free(argv);
34113044Sasami	}
34213044Sasami
34313044Sasami	(void)fclose(f);
34413044Sasami	return (rval);
34513044Sasami}
34613044Sasami
34713044Sasamistatic int
34892539Simpcheckdev(char *path)
34913044Sasami{
35013044Sasami	struct stat st;
35113044Sasami
35213044Sasami	if (stat(path, &st) != 0)
35313044Sasami		return (errno);
35413044Sasami
35513044Sasami	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
35613044Sasami		return (EINVAL);
35713044Sasami
35813044Sasami	return (0);
35913044Sasami}
36013044Sasami
36113044Sasamistatic int
36292539Simpresolve_ccdname(char *name)
36313044Sasami{
36413044Sasami
365109417Sphk	if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
366109417Sphk		name += strlen(_PATH_DEV);
367109417Sphk	if (strncmp(name, "ccd", 3))
368109417Sphk		return -1;
369109417Sphk	name += 3;
370109417Sphk	if (!isdigit(*name))
371109417Sphk		return -1;
372109417Sphk	return (strtoul(name, NULL, 10));
37313044Sasami}
37413044Sasami
37513044Sasamistatic int
376109417Sphkdo_io(int unit, u_long cmd, struct ccd_ioctl *cciop)
37713044Sasami{
37813044Sasami	int fd;
37913044Sasami	char *cp;
380109417Sphk	char *path;
38113044Sasami
382109417Sphk	asprintf(&path, "%sccd%dc", _PATH_DEV, unit);
383109417Sphk
38413044Sasami	if ((fd = open(path, O_RDWR, 0640)) < 0) {
38513044Sasami		warn("open: %s", path);
38613044Sasami		return (1);
38713044Sasami	}
38813044Sasami
38913044Sasami	if (ioctl(fd, cmd, cciop) < 0) {
39013044Sasami		switch (cmd) {
39113044Sasami		case CCDIOCSET:
39213044Sasami			cp = "CCDIOCSET";
39313044Sasami			break;
39413044Sasami
39513044Sasami		case CCDIOCCLR:
39613044Sasami			cp = "CCDIOCCLR";
39713044Sasami			break;
39813044Sasami
39982943Sphk		case CCDCONFINFO:
40082943Sphk			cp = "CCDCONFINFO";
40182943Sphk			break;
40282943Sphk
40382943Sphk		case CCDCPPINFO:
40482943Sphk			cp = "CCDCPPINFO";
40582943Sphk			break;
40682943Sphk
40713044Sasami		default:
40813044Sasami			cp = "unknown";
40913044Sasami		}
41013044Sasami		warn("ioctl (%s): %s", cp, path);
41113044Sasami		return (1);
41213044Sasami	}
41313044Sasami
41413044Sasami	return (0);
41513044Sasami}
41613044Sasami
41713044Sasamistatic int
41892539Simpdump_ccd(int argc, char **argv)
41913044Sasami{
420109417Sphk	char *cp;
42113044Sasami	int i, error, numccd, numconfiged = 0;
42282943Sphk	struct ccdconf conf;
423109417Sphk	int ccd;
42413044Sasami
42582943Sphk	/*
42682943Sphk	 * Read the ccd configuration data from the kernel and dump
42782943Sphk	 * it to stdout.
42882943Sphk	 */
429109417Sphk	if ((ccd = resolve_ccdname("ccd0")) < 0) {		/* XXX */
43082943Sphk		warnx("invalid ccd name: %s", cp);
43113044Sasami		return (1);
43213044Sasami	}
43382943Sphk	conf.size = 0;
43482943Sphk	if (do_io(ccd, CCDCONFINFO, (struct ccd_ioctl *) &conf))
43582943Sphk		return (1);
43682943Sphk	if (conf.size == 0) {
43782943Sphk		printf("no concatenated disks configured\n");
43882943Sphk		return (0);
43913044Sasami	}
44013044Sasami	/* Allocate space for the configuration data. */
44182943Sphk	conf.buffer = alloca(conf.size);
44282943Sphk	if (conf.buffer == NULL) {
44313044Sasami		warnx("no memory for configuration data");
44482943Sphk		return (1);
44513044Sasami	}
44682943Sphk	if (do_io(ccd, CCDCONFINFO, (struct ccd_ioctl *) &conf))
44782943Sphk		return (1);
44813044Sasami
44982943Sphk	numconfiged = conf.size / sizeof(struct ccd_s);
45013044Sasami
45113044Sasami	if (argc == 0) {
45282943Sphk		for (i = 0; i < numconfiged; i++)
45382943Sphk			print_ccd_info(&(conf.buffer[i]));
45413044Sasami	} else {
45513044Sasami		while (argc) {
45613044Sasami			cp = *argv++; --argc;
457109417Sphk			if ((ccd = resolve_ccdname(cp)) < 0) {
45813044Sasami				warnx("invalid ccd name: %s", cp);
45913044Sasami				continue;
46013044Sasami			}
46182943Sphk			error = 1;
46282943Sphk			for (i = 0; i < numconfiged; i++) {
463109417Sphk				if (conf.buffer[i].sc_unit == ccd) {
46482943Sphk					print_ccd_info(&(conf.buffer[i]));
46582943Sphk					error = 0;
46682943Sphk					break;
46782943Sphk				}
46882943Sphk			}
46982943Sphk			if (error) {
47082943Sphk				warnx("ccd%d not configured", numccd);
47113044Sasami				continue;
47213044Sasami			}
47313044Sasami		}
47482943Sphk	}
47513044Sasami
47613044Sasami	return (0);
47713044Sasami}
47813044Sasami
47913044Sasamistatic void
48092539Simpprint_ccd_info(struct ccd_s *cs)
48113044Sasami{
482109417Sphk	char *cp;
48313044Sasami	static int header_printed = 0;
48482943Sphk	struct ccdcpps cpps;
485109417Sphk	int ccd;
48613044Sasami
48782943Sphk	/* Print out header if necessary*/
48813044Sasami	if (header_printed == 0 && verbose) {
48913044Sasami		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
49013044Sasami		header_printed = 1;
49113044Sasami	}
49213044Sasami
49313044Sasami	/* Dump out softc information. */
49413044Sasami	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
49513044Sasami	    cs->sc_cflags & CCDF_USERMASK);
49613044Sasami	fflush(stdout);
49713044Sasami
49813044Sasami	/* Read in the component info. */
49982943Sphk	asprintf(&cp, "%s%d", cs->device_stats.device_name,
50082943Sphk	    cs->device_stats.unit_number);
50182943Sphk	if (cp == NULL) {
50213044Sasami		printf("\n");
50382943Sphk		warn("ccd%d: can't allocate memory",
50482943Sphk		    cs->sc_unit);
50582943Sphk		return;
50682943Sphk	}
50782943Sphk
508109417Sphk	if ((ccd = resolve_ccdname(cp)) < 0) {
50982943Sphk		printf("\n");
51082943Sphk		warnx("can't read component info: invalid ccd name: %s", cp);
51182943Sphk		return;
51282943Sphk	}
51382943Sphk	cpps.size = 0;
51482943Sphk	if (do_io(ccd, CCDCPPINFO, (struct ccd_ioctl *) &cpps)) {
51582943Sphk		printf("\n");
51613044Sasami		warnx("can't read component info");
51782943Sphk		return;
51813044Sasami	}
51982943Sphk	cpps.buffer = alloca(cpps.size);
52082943Sphk	if (cpps.buffer == NULL) {
52182943Sphk		printf("\n");
52282943Sphk		warn("ccd%d: can't allocate memory for component info",
52382943Sphk		    cs->sc_unit);
52482943Sphk		return;
52582943Sphk	}
52682943Sphk	if (do_io(ccd, CCDCPPINFO, (struct ccd_ioctl *) &cpps)) {
52782943Sphk		printf("\n");
52882943Sphk		warnx("can't read component info");
52982943Sphk		return;
53082943Sphk	}
53113044Sasami
53282943Sphk	/* Display component info. */
53382943Sphk	for (cp = cpps.buffer; cp - cpps.buffer < cpps.size; cp += strlen(cp) + 1) {
53482943Sphk		printf((cp + strlen(cp) + 1) < (cpps.buffer + cpps.size) ?
53582943Sphk		    "%s " : "%s\n", cp);
53613044Sasami		fflush(stdout);
53713044Sasami	}
53882943Sphk	return;
53913044Sasami}
54013044Sasami
54113044Sasamistatic int
54292539Simpflags_to_val(char *flags)
54313044Sasami{
54413044Sasami	char *cp, *tok;
54513044Sasami	int i, tmp, val = ~CCDF_USERMASK;
54613044Sasami	size_t flagslen;
54713044Sasami
54813044Sasami	/*
54913044Sasami	 * The most common case is that of NIL flags, so check for
55013044Sasami	 * those first.
55113044Sasami	 */
55213044Sasami	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
55313044Sasami	    strcmp("0", flags) == 0)
55413044Sasami		return (0);
55513044Sasami
55613044Sasami	flagslen = strlen(flags);
55713044Sasami
55813044Sasami	/* Check for values represented by strings. */
55913044Sasami	if ((cp = strdup(flags)) == NULL)
56013044Sasami		err(1, "no memory to parse flags");
56113044Sasami	tmp = 0;
56213044Sasami	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
56313044Sasami		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
56413044Sasami			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
56513044Sasami				break;
56613044Sasami		if (flagvaltab[i].fv_flag == NULL) {
56713044Sasami			free(cp);
56813044Sasami			goto bad_string;
56913044Sasami		}
57013044Sasami		tmp |= flagvaltab[i].fv_val;
57113044Sasami	}
57213044Sasami
57313044Sasami	/* If we get here, the string was ok. */
57413044Sasami	free(cp);
57513044Sasami	val = tmp;
57613044Sasami	goto out;
57713044Sasami
57813044Sasami bad_string:
57913044Sasami
58013044Sasami	/* Check for values represented in hex. */
58113044Sasami	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
58213044Sasami		errno = 0;	/* to check for ERANGE */
58313044Sasami		val = (int)strtol(&flags[2], &cp, 16);
58413044Sasami		if ((errno == ERANGE) || (*cp != '\0'))
58513044Sasami			return (-1);
58613044Sasami		goto out;
58713044Sasami	}
58813044Sasami
58913044Sasami	/* Check for values represented in decimal. */
59013044Sasami	errno = 0;	/* to check for ERANGE */
59113044Sasami	val = (int)strtol(flags, &cp, 10);
59213044Sasami	if ((errno == ERANGE) || (*cp != '\0'))
59313044Sasami		return (-1);
59413044Sasami
59513044Sasami out:
59613044Sasami	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
59713044Sasami}
59813044Sasami
59913044Sasamistatic void
60092539Simpusage(void)
60113044Sasami{
60226541Scharnier	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
60326541Scharnier		"usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
60426541Scharnier		"       ccdconfig -C [-v] [-f config_file]",
60526541Scharnier		"       ccdconfig -u [-v] ccd [...]",
60626541Scharnier		"       ccdconfig -U [-v] [-f config_file]",
60782943Sphk		"       ccdconfig -g [ccd [...]]");
60813044Sasami	exit(1);
60913044Sasami}
61026541Scharnier
61113052Sasami/* Local Variables: */
61213052Sasami/* c-argdecl-indent: 8 */
61313052Sasami/* c-indent-level: 8 */
61413052Sasami/* End: */
615