ccdconfig.c revision 36628
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[] =
3736628Scharnier	"$Id$";
3836628Scharnier#endif /* not lint */
3936628Scharnier
4013044Sasami#include <sys/param.h>
4113044Sasami#include <sys/disklabel.h>
4213044Sasami#include <sys/device.h>
4313044Sasami#include <sys/disk.h>
4413044Sasami#include <sys/stat.h>
4513044Sasami#include <ctype.h>
4613044Sasami#include <err.h>
4713044Sasami#include <errno.h>
4813044Sasami#include <fcntl.h>
4913044Sasami#include <kvm.h>
5013044Sasami#include <limits.h>
5113044Sasami#include <stdio.h>
5213044Sasami#include <stdlib.h>
5313044Sasami#include <string.h>
5413044Sasami#include <unistd.h>
5513044Sasami
5613052Sasami#include <sys/ccdvar.h>
5713044Sasami
5813044Sasami#include "pathnames.h"
5913044Sasami
6013044Sasamistatic	int lineno = 0;
6113044Sasamistatic	int verbose = 0;
6213044Sasamistatic	char *ccdconf = _PATH_CCDCONF;
6313044Sasami
6413044Sasamistatic	char *core = NULL;
6513044Sasamistatic	char *kernel = NULL;
6613044Sasami
6713044Sasamistruct	flagval {
6813044Sasami	char	*fv_flag;
6913044Sasami	int	fv_val;
7013044Sasami} flagvaltab[] = {
7113044Sasami	{ "CCDF_SWAP",		CCDF_SWAP },
7213044Sasami	{ "CCDF_UNIFORM",	CCDF_UNIFORM },
7313762Sasami	{ "CCDF_MIRROR",	CCDF_MIRROR },
7413762Sasami	{ "CCDF_PARITY",	CCDF_PARITY },
7513044Sasami	{ NULL,			0 },
7613044Sasami};
7713044Sasami
7813044Sasamistatic	struct nlist nl[] = {
7913044Sasami	{ "_ccd_softc" },
8013044Sasami#define SYM_CCDSOFTC		0
8113044Sasami	{ "_numccd" },
8213044Sasami#define SYM_NUMCCD		1
8313044Sasami	{ NULL },
8413044Sasami};
8513044Sasami
8613044Sasami#define CCD_CONFIG		0	/* configure a device */
8713044Sasami#define CCD_CONFIGALL		1	/* configure all devices */
8813044Sasami#define CCD_UNCONFIG		2	/* unconfigure a device */
8913044Sasami#define CCD_UNCONFIGALL		3	/* unconfigure all devices */
9013044Sasami#define CCD_DUMP		4	/* dump a ccd's configuration */
9113044Sasami
9213044Sasamistatic	int checkdev __P((char *));
9313044Sasamistatic	int do_io __P((char *, u_long, struct ccd_ioctl *));
9413044Sasamistatic	int do_single __P((int, char **, int));
9513044Sasamistatic	int do_all __P((int));
9613044Sasamistatic	int dump_ccd __P((int, char **));
9713044Sasamistatic	int getmaxpartitions __P((void));
9813044Sasamistatic	int getrawpartition __P((void));
9913044Sasamistatic	int flags_to_val __P((char *));
10013044Sasamistatic	void print_ccd_info __P((struct ccd_softc *, kvm_t *));
10113044Sasamistatic	char *resolve_ccdname __P((char *));
10213044Sasamistatic	void usage __P((void));
10313044Sasami
10413044Sasamiint
10513044Sasamimain(argc, argv)
10613044Sasami	int argc;
10713044Sasami	char **argv;
10813044Sasami{
10913044Sasami	int ch, options = 0, action = CCD_CONFIG;
11013044Sasami
11113044Sasami	while ((ch = getopt(argc, argv, "cCf:gM:N:uUv")) != -1) {
11213044Sasami		switch (ch) {
11313044Sasami		case 'c':
11413044Sasami			action = CCD_CONFIG;
11513044Sasami			++options;
11613044Sasami			break;
11713044Sasami
11813044Sasami		case 'C':
11913044Sasami			action = CCD_CONFIGALL;
12013044Sasami			++options;
12113044Sasami			break;
12213044Sasami
12313044Sasami		case 'f':
12413044Sasami			ccdconf = optarg;
12513044Sasami			break;
12613044Sasami
12713044Sasami		case 'g':
12813044Sasami			action = CCD_DUMP;
12913044Sasami			break;
13013044Sasami
13113044Sasami		case 'M':
13213044Sasami			core = optarg;
13313044Sasami			break;
13413044Sasami
13513044Sasami		case 'N':
13613044Sasami			kernel = optarg;
13713044Sasami			break;
13813044Sasami
13913044Sasami		case 'u':
14013044Sasami			action = CCD_UNCONFIG;
14113044Sasami			++options;
14213044Sasami			break;
14313044Sasami
14413044Sasami		case 'U':
14513044Sasami			action = CCD_UNCONFIGALL;
14613044Sasami			++options;
14713044Sasami			break;
14813044Sasami
14913044Sasami		case 'v':
15013044Sasami			verbose = 1;
15113044Sasami			break;
15213044Sasami
15313044Sasami		default:
15413044Sasami			usage();
15513044Sasami		}
15613044Sasami	}
15713044Sasami	argc -= optind;
15813044Sasami	argv += optind;
15913044Sasami
16013044Sasami	if (options > 1)
16113044Sasami		usage();
16213044Sasami
16332116Simp	/*
16432116Simp	 * Discard setgid privileges if not the running kernel so that bad
16532116Simp	 * guys can't print interesting stuff from kernel memory.
16632116Simp	 */
16732116Simp	if (core != NULL || kernel != NULL || action != CCD_DUMP) {
16832116Simp		setegid(getgid());
16932116Simp		setgid(getgid());
17032116Simp	}
17132116Simp
17213044Sasami	switch (action) {
17313044Sasami		case CCD_CONFIG:
17413044Sasami		case CCD_UNCONFIG:
17513044Sasami			exit(do_single(argc, argv, action));
17613044Sasami			/* NOTREACHED */
17713044Sasami
17813044Sasami		case CCD_CONFIGALL:
17913044Sasami		case CCD_UNCONFIGALL:
18013044Sasami			exit(do_all(action));
18113044Sasami			/* NOTREACHED */
18213044Sasami
18313044Sasami		case CCD_DUMP:
18413044Sasami			exit(dump_ccd(argc, argv));
18513044Sasami			/* NOTREACHED */
18613044Sasami	}
18713044Sasami	/* NOTREACHED */
18836628Scharnier	return (0);
18913044Sasami}
19013044Sasami
19113044Sasamistatic int
19213044Sasamido_single(argc, argv, action)
19313044Sasami	int argc;
19413044Sasami	char **argv;
19513044Sasami	int action;
19613044Sasami{
19713044Sasami	struct ccd_ioctl ccio;
19813044Sasami	char *ccd, *cp, *cp2, **disks;
19936628Scharnier	int noflags = 0, i, ileave, flags, j;
20013044Sasami
20113044Sasami	bzero(&ccio, sizeof(ccio));
20213044Sasami
20313044Sasami	/*
20413044Sasami	 * If unconfiguring, all arguments are treated as ccds.
20513044Sasami	 */
20613044Sasami	if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
20713044Sasami		for (i = 0; argc != 0; ) {
20813044Sasami			cp = *argv++; --argc;
20913044Sasami			if ((ccd = resolve_ccdname(cp)) == NULL) {
21013044Sasami				warnx("invalid ccd name: %s", cp);
21113044Sasami				i = 1;
21213044Sasami				continue;
21313044Sasami			}
21413044Sasami			if (do_io(ccd, CCDIOCCLR, &ccio))
21513044Sasami				i = 1;
21613044Sasami			else
21713044Sasami				if (verbose)
21813044Sasami					printf("%s unconfigured\n", cp);
21913044Sasami		}
22013044Sasami		return (i);
22113044Sasami	}
22213044Sasami
22313044Sasami	/* Make sure there are enough arguments. */
22413044Sasami	if (argc < 4)
22513044Sasami		if (argc == 3) {
22613044Sasami			/* Assume that no flags are specified. */
22713044Sasami			noflags = 1;
22813044Sasami		} else {
22913044Sasami			if (action == CCD_CONFIGALL) {
23013044Sasami				warnx("%s: bad line: %d", ccdconf, lineno);
23113044Sasami				return (1);
23213044Sasami			} else
23313044Sasami				usage();
23413044Sasami		}
23513044Sasami
23613044Sasami	/* First argument is the ccd to configure. */
23713044Sasami	cp = *argv++; --argc;
23813044Sasami	if ((ccd = resolve_ccdname(cp)) == NULL) {
23913044Sasami		warnx("invalid ccd name: %s", cp);
24013044Sasami		return (1);
24113044Sasami	}
24213044Sasami
24313044Sasami	/* Next argument is the interleave factor. */
24413044Sasami	cp = *argv++; --argc;
24513044Sasami	errno = 0;	/* to check for ERANGE */
24613044Sasami	ileave = (int)strtol(cp, &cp2, 10);
24713044Sasami	if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
24813044Sasami		warnx("invalid interleave factor: %s", cp);
24913044Sasami		return (1);
25013044Sasami	}
25113044Sasami
25213044Sasami	if (noflags == 0) {
25313044Sasami		/* Next argument is the ccd configuration flags. */
25413044Sasami		cp = *argv++; --argc;
25513044Sasami		if ((flags = flags_to_val(cp)) < 0) {
25613044Sasami			warnx("invalid flags argument: %s", cp);
25713044Sasami			return (1);
25813044Sasami		}
25913044Sasami	}
26013044Sasami
26113044Sasami	/* Next is the list of disks to make the ccd from. */
26213044Sasami	disks = malloc(argc * sizeof(char *));
26313044Sasami	if (disks == NULL) {
26413044Sasami		warnx("no memory to configure ccd");
26513044Sasami		return (1);
26613044Sasami	}
26713044Sasami	for (i = 0; argc != 0; ) {
26813044Sasami		cp = *argv++; --argc;
26913044Sasami		if ((j = checkdev(cp)) == 0)
27013044Sasami			disks[i++] = cp;
27113044Sasami		else {
27213044Sasami			warnx("%s: %s", cp, strerror(j));
27313044Sasami			return (1);
27413044Sasami		}
27513044Sasami	}
27613044Sasami
27713044Sasami	/* Fill in the ccio. */
27813044Sasami	ccio.ccio_disks = disks;
27913044Sasami	ccio.ccio_ndisks = i;
28013044Sasami	ccio.ccio_ileave = ileave;
28113044Sasami	ccio.ccio_flags = flags;
28213044Sasami
28313044Sasami	if (do_io(ccd, CCDIOCSET, &ccio)) {
28413044Sasami		free(disks);
28513044Sasami		return (1);
28613044Sasami	}
28713044Sasami
28813044Sasami	if (verbose) {
28913044Sasami		printf("ccd%d: %d components ", ccio.ccio_unit,
29013044Sasami		    ccio.ccio_ndisks);
29113044Sasami		for (i = 0; i < ccio.ccio_ndisks; ++i) {
29213044Sasami			if ((cp2 = strrchr(disks[i], '/')) != NULL)
29313044Sasami				++cp2;
29413044Sasami			else
29513044Sasami				cp2 = disks[i];
29613044Sasami			printf("%c%s%c",
29713044Sasami			    i == 0 ? '(' : ' ', cp2,
29813044Sasami			    i == ccio.ccio_ndisks - 1 ? ')' : ',');
29913044Sasami		}
30013044Sasami		printf(", %d blocks ", ccio.ccio_size);
30113044Sasami		if (ccio.ccio_ileave != 0)
30213044Sasami			printf("interleaved at %d blocks\n", ccio.ccio_ileave);
30313044Sasami		else
30413044Sasami			printf("concatenated\n");
30513044Sasami	}
30613044Sasami
30713044Sasami	free(disks);
30813044Sasami	return (0);
30913044Sasami}
31013044Sasami
31113044Sasamistatic int
31213044Sasamido_all(action)
31313044Sasami	int action;
31413044Sasami{
31513044Sasami	FILE *f;
31613044Sasami	char line[_POSIX2_LINE_MAX];
31713044Sasami	char *cp, **argv;
31813044Sasami	int argc, rval;
31932116Simp	gid_t egid;
32013044Sasami
32132116Simp	egid = getegid();
32232116Simp	setegid(getgid());
32313044Sasami	if ((f = fopen(ccdconf, "r")) == NULL) {
32432116Simp		setegid(egid);
32513044Sasami		warn("fopen: %s", ccdconf);
32613044Sasami		return (1);
32713044Sasami	}
32832116Simp	setegid(egid);
32913044Sasami
33013044Sasami	while (fgets(line, sizeof(line), f) != NULL) {
33113044Sasami		argc = 0;
33213044Sasami		argv = NULL;
33313044Sasami		++lineno;
33413044Sasami		if ((cp = strrchr(line, '\n')) != NULL)
33513044Sasami			*cp = '\0';
33613044Sasami
33713044Sasami		/* Break up the line and pass it's contents to do_single(). */
33813044Sasami		if (line[0] == '\0')
33913044Sasami			goto end_of_line;
34013044Sasami		for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
34113044Sasami			if (*cp == '#')
34213044Sasami				break;
34313044Sasami			if ((argv = realloc(argv,
34413044Sasami			    sizeof(char *) * ++argc)) == NULL) {
34513044Sasami				warnx("no memory to configure ccds");
34613044Sasami				return (1);
34713044Sasami			}
34813044Sasami			argv[argc - 1] = cp;
34913044Sasami			/*
35013044Sasami			 * If our action is to unconfigure all, then pass
35113044Sasami			 * just the first token to do_single() and ignore
35213044Sasami			 * the rest.  Since this will be encountered on
35313044Sasami			 * our first pass through the line, the Right
35413044Sasami			 * Thing will happen.
35513044Sasami			 */
35613044Sasami			if (action == CCD_UNCONFIGALL) {
35713044Sasami				if (do_single(argc, argv, action))
35813044Sasami					rval = 1;
35913044Sasami				goto end_of_line;
36013044Sasami			}
36113044Sasami		}
36213044Sasami		if (argc != 0)
36313044Sasami			if (do_single(argc, argv, action))
36413044Sasami				rval = 1;
36513044Sasami
36613044Sasami end_of_line:
36713044Sasami		if (argv != NULL)
36813044Sasami			free(argv);
36913044Sasami	}
37013044Sasami
37113044Sasami	(void)fclose(f);
37213044Sasami	return (rval);
37313044Sasami}
37413044Sasami
37513044Sasamistatic int
37613044Sasamicheckdev(path)
37713044Sasami	char *path;
37813044Sasami{
37913044Sasami	struct stat st;
38013044Sasami
38113044Sasami	if (stat(path, &st) != 0)
38213044Sasami		return (errno);
38313044Sasami
38413044Sasami	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
38513044Sasami		return (EINVAL);
38613044Sasami
38713044Sasami	return (0);
38813044Sasami}
38913044Sasami
39013044Sasamistatic int
39113044Sasamipathtounit(path, unitp)
39213044Sasami	char *path;
39313044Sasami	int *unitp;
39413044Sasami{
39513044Sasami	struct stat st;
39613044Sasami	int maxpartitions;
39713044Sasami
39813044Sasami	if (stat(path, &st) != 0)
39913044Sasami		return (errno);
40013044Sasami
40113044Sasami	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
40213044Sasami		return (EINVAL);
40313044Sasami
40413044Sasami	if ((maxpartitions = getmaxpartitions()) < 0)
40513044Sasami		return (errno);
40613044Sasami
40713044Sasami	*unitp = minor(st.st_rdev) / maxpartitions;
40813044Sasami
40913044Sasami	return (0);
41013044Sasami}
41113044Sasami
41213044Sasamistatic char *
41313044Sasamiresolve_ccdname(name)
41413044Sasami	char *name;
41513044Sasami{
41636628Scharnier	char c, *path;
41713044Sasami	size_t len, newlen;
41813044Sasami	int rawpart;
41913044Sasami
42013044Sasami	if (name[0] == '/' || name[0] == '.') {
42113044Sasami		/* Assume they gave the correct pathname. */
42213044Sasami		return (strdup(name));
42313044Sasami	}
42413044Sasami
42513044Sasami	len = strlen(name);
42613044Sasami	c = name[len - 1];
42713044Sasami
42813044Sasami	newlen = len + 8;
42913044Sasami	if ((path = malloc(newlen)) == NULL)
43013044Sasami		return (NULL);
43113044Sasami	bzero(path, newlen);
43213044Sasami
43313044Sasami	if (isdigit(c)) {
43413044Sasami		if ((rawpart = getrawpartition()) < 0) {
43513044Sasami			free(path);
43613044Sasami			return (NULL);
43713044Sasami		}
43813044Sasami		(void)sprintf(path, "/dev/%s%c", name, 'a' + rawpart);
43913044Sasami	} else
44013044Sasami		(void)sprintf(path, "/dev/%s", name);
44113044Sasami
44213044Sasami	return (path);
44313044Sasami}
44413044Sasami
44513044Sasamistatic int
44613044Sasamido_io(path, cmd, cciop)
44713044Sasami	char *path;
44813044Sasami	u_long cmd;
44913044Sasami	struct ccd_ioctl *cciop;
45013044Sasami{
45113044Sasami	int fd;
45213044Sasami	char *cp;
45313044Sasami
45413044Sasami	if ((fd = open(path, O_RDWR, 0640)) < 0) {
45513044Sasami		warn("open: %s", path);
45613044Sasami		return (1);
45713044Sasami	}
45813044Sasami
45913044Sasami	if (ioctl(fd, cmd, cciop) < 0) {
46013044Sasami		switch (cmd) {
46113044Sasami		case CCDIOCSET:
46213044Sasami			cp = "CCDIOCSET";
46313044Sasami			break;
46413044Sasami
46513044Sasami		case CCDIOCCLR:
46613044Sasami			cp = "CCDIOCCLR";
46713044Sasami			break;
46813044Sasami
46913044Sasami		default:
47013044Sasami			cp = "unknown";
47113044Sasami		}
47213044Sasami		warn("ioctl (%s): %s", cp, path);
47313044Sasami		return (1);
47413044Sasami	}
47513044Sasami
47613044Sasami	return (0);
47713044Sasami}
47813044Sasami
47913044Sasami#define KVM_ABORT(kd, str) {						\
48013044Sasami	(void)kvm_close((kd));						\
48113044Sasami	warnx((str));							\
48213044Sasami	warnx(kvm_geterr((kd)));					\
48313044Sasami	return (1);							\
48413044Sasami}
48513044Sasami
48613044Sasamistatic int
48713044Sasamidump_ccd(argc, argv)
48813044Sasami	int argc;
48913044Sasami	char **argv;
49013044Sasami{
49113044Sasami	char errbuf[_POSIX2_LINE_MAX], *ccd, *cp;
49213044Sasami	struct ccd_softc *cs, *kcs;
49313044Sasami	size_t readsize;
49413044Sasami	int i, error, numccd, numconfiged = 0;
49513044Sasami	kvm_t *kd;
49613044Sasami
49713044Sasami	bzero(errbuf, sizeof(errbuf));
49813044Sasami
49913044Sasami	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
50013044Sasami	    errbuf)) == NULL) {
50113044Sasami		warnx("can't open kvm: %s", errbuf);
50213044Sasami		return (1);
50313044Sasami	}
50413044Sasami
50513044Sasami	if (kvm_nlist(kd, nl))
50613044Sasami		KVM_ABORT(kd, "ccd-related symbols not available");
50713044Sasami
50813044Sasami	/* Check to see how many ccds are currently configured. */
50913044Sasami	if (kvm_read(kd, nl[SYM_NUMCCD].n_value, (char *)&numccd,
51013044Sasami	    sizeof(numccd)) != sizeof(numccd))
51113044Sasami		KVM_ABORT(kd, "can't determine number of configured ccds");
51213044Sasami
51313044Sasami	if (numccd == 0) {
51413044Sasami		printf("ccd driver in kernel, but is uninitialized\n");
51513044Sasami		goto done;
51613044Sasami	}
51713044Sasami
51813044Sasami	/* Allocate space for the configuration data. */
51913044Sasami	readsize = numccd * sizeof(struct ccd_softc);
52013044Sasami	if ((cs = malloc(readsize)) == NULL) {
52113044Sasami		warnx("no memory for configuration data");
52213044Sasami		goto bad;
52313044Sasami	}
52413044Sasami	bzero(cs, readsize);
52513044Sasami
52613044Sasami	/*
52713044Sasami	 * Read the ccd configuration data from the kernel and dump
52813044Sasami	 * it to stdout.
52913044Sasami	 */
53013044Sasami	if (kvm_read(kd, nl[SYM_CCDSOFTC].n_value, (char *)&kcs,
53113044Sasami	    sizeof(kcs)) != sizeof(kcs)) {
53213044Sasami		free(cs);
53313044Sasami		KVM_ABORT(kd, "can't find pointer to configuration data");
53413044Sasami	}
53513044Sasami	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
53613044Sasami		free(cs);
53713044Sasami		KVM_ABORT(kd, "can't read configuration data");
53813044Sasami	}
53913044Sasami
54013044Sasami	if (argc == 0) {
54113044Sasami		for (i = 0; i < numccd; ++i)
54213044Sasami			if (cs[i].sc_flags & CCDF_INITED) {
54313044Sasami				++numconfiged;
54413044Sasami				print_ccd_info(&cs[i], kd);
54513044Sasami			}
54613044Sasami
54713044Sasami		if (numconfiged == 0)
54813044Sasami			printf("no concatenated disks configured\n");
54913044Sasami	} else {
55013044Sasami		while (argc) {
55113044Sasami			cp = *argv++; --argc;
55213044Sasami			if ((ccd = resolve_ccdname(cp)) == NULL) {
55313044Sasami				warnx("invalid ccd name: %s", cp);
55413044Sasami				continue;
55513044Sasami			}
55613044Sasami			if ((error = pathtounit(ccd, &i)) != 0) {
55713044Sasami				warnx("%s: %s", ccd, strerror(error));
55813044Sasami				continue;
55913044Sasami			}
56013044Sasami			if (i >= numccd) {
56113044Sasami				warnx("ccd%d not configured", i);
56213044Sasami				continue;
56313044Sasami			}
56413044Sasami			if (cs[i].sc_flags & CCDF_INITED)
56513044Sasami				print_ccd_info(&cs[i], kd);
56613044Sasami			else
56713044Sasami				printf("ccd%d not configured\n", i);
56813044Sasami		}
56913044Sasami	}
57013044Sasami
57113044Sasami	free(cs);
57213044Sasami
57313044Sasami done:
57413044Sasami	(void)kvm_close(kd);
57513044Sasami	return (0);
57613044Sasami
57713044Sasami bad:
57813044Sasami	(void)kvm_close(kd);
57913044Sasami	return (1);
58013044Sasami}
58113044Sasami
58213044Sasamistatic void
58313044Sasamiprint_ccd_info(cs, kd)
58413044Sasami	struct ccd_softc *cs;
58513044Sasami	kvm_t *kd;
58613044Sasami{
58713044Sasami	static int header_printed = 0;
58813044Sasami	struct ccdcinfo *cip;
58913044Sasami	size_t readsize;
59013044Sasami	char path[MAXPATHLEN];
59113044Sasami	int i;
59213044Sasami
59313044Sasami	if (header_printed == 0 && verbose) {
59413044Sasami		printf("# ccd\t\tileave\tflags\tcompnent devices\n");
59513044Sasami		header_printed = 1;
59613044Sasami	}
59713044Sasami
59813044Sasami	readsize = cs->sc_nccdisks * sizeof(struct ccdcinfo);
59913044Sasami	if ((cip = malloc(readsize)) == NULL) {
60013044Sasami		warn("ccd%d: can't allocate memory for component info",
60113044Sasami		    cs->sc_unit);
60213044Sasami		return;
60313044Sasami	}
60413044Sasami	bzero(cip, readsize);
60513044Sasami
60613044Sasami	/* Dump out softc information. */
60713044Sasami	printf("ccd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
60813044Sasami	    cs->sc_cflags & CCDF_USERMASK);
60913044Sasami	fflush(stdout);
61013044Sasami
61113044Sasami	/* Read in the component info. */
61213044Sasami	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
61313044Sasami	    readsize) != readsize) {
61413044Sasami		printf("\n");
61513044Sasami		warnx("can't read component info");
61613044Sasami		warnx(kvm_geterr(kd));
61713044Sasami		goto done;
61813044Sasami	}
61913044Sasami
62013044Sasami	/* Read component pathname and display component info. */
62113044Sasami	for (i = 0; i < cs->sc_nccdisks; ++i) {
62213044Sasami		if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
62313044Sasami		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
62413044Sasami			printf("\n");
62513044Sasami			warnx("can't read component pathname");
62613044Sasami			warnx(kvm_geterr(kd));
62713044Sasami			goto done;
62813044Sasami		}
62913044Sasami		printf((i + 1 < cs->sc_nccdisks) ? "%s " : "%s\n", path);
63013044Sasami		fflush(stdout);
63113044Sasami	}
63213044Sasami
63313044Sasami done:
63413044Sasami	free(cip);
63513044Sasami}
63613044Sasami
63713044Sasamistatic int
63813044Sasamigetmaxpartitions()
63913044Sasami{
64013052Sasami    return (MAXPARTITIONS);
64113044Sasami}
64213044Sasami
64313044Sasamistatic int
64413044Sasamigetrawpartition()
64513044Sasami{
64613052Sasami	return (RAW_PART);
64713044Sasami}
64813044Sasami
64913044Sasamistatic int
65013044Sasamiflags_to_val(flags)
65113044Sasami	char *flags;
65213044Sasami{
65313044Sasami	char *cp, *tok;
65413044Sasami	int i, tmp, val = ~CCDF_USERMASK;
65513044Sasami	size_t flagslen;
65613044Sasami
65713044Sasami	/*
65813044Sasami	 * The most common case is that of NIL flags, so check for
65913044Sasami	 * those first.
66013044Sasami	 */
66113044Sasami	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
66213044Sasami	    strcmp("0", flags) == 0)
66313044Sasami		return (0);
66413044Sasami
66513044Sasami	flagslen = strlen(flags);
66613044Sasami
66713044Sasami	/* Check for values represented by strings. */
66813044Sasami	if ((cp = strdup(flags)) == NULL)
66913044Sasami		err(1, "no memory to parse flags");
67013044Sasami	tmp = 0;
67113044Sasami	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
67213044Sasami		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
67313044Sasami			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
67413044Sasami				break;
67513044Sasami		if (flagvaltab[i].fv_flag == NULL) {
67613044Sasami			free(cp);
67713044Sasami			goto bad_string;
67813044Sasami		}
67913044Sasami		tmp |= flagvaltab[i].fv_val;
68013044Sasami	}
68113044Sasami
68213044Sasami	/* If we get here, the string was ok. */
68313044Sasami	free(cp);
68413044Sasami	val = tmp;
68513044Sasami	goto out;
68613044Sasami
68713044Sasami bad_string:
68813044Sasami
68913044Sasami	/* Check for values represented in hex. */
69013044Sasami	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
69113044Sasami		errno = 0;	/* to check for ERANGE */
69213044Sasami		val = (int)strtol(&flags[2], &cp, 16);
69313044Sasami		if ((errno == ERANGE) || (*cp != '\0'))
69413044Sasami			return (-1);
69513044Sasami		goto out;
69613044Sasami	}
69713044Sasami
69813044Sasami	/* Check for values represented in decimal. */
69913044Sasami	errno = 0;	/* to check for ERANGE */
70013044Sasami	val = (int)strtol(flags, &cp, 10);
70113044Sasami	if ((errno == ERANGE) || (*cp != '\0'))
70213044Sasami		return (-1);
70313044Sasami
70413044Sasami out:
70513044Sasami	return (((val & ~CCDF_USERMASK) == 0) ? val : -1);
70613044Sasami}
70713044Sasami
70813044Sasamistatic void
70913044Sasamiusage()
71013044Sasami{
71126541Scharnier	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
71226541Scharnier		"usage: ccdconfig [-cv] ccd ileave [flags] dev [...]",
71326541Scharnier		"       ccdconfig -C [-v] [-f config_file]",
71426541Scharnier		"       ccdconfig -u [-v] ccd [...]",
71526541Scharnier		"       ccdconfig -U [-v] [-f config_file]",
71626541Scharnier		"       ccdconfig -g [-M core] [-N system] [ccd [...]]");
71713044Sasami	exit(1);
71813044Sasami}
71926541Scharnier
72013052Sasami/* Local Variables: */
72113052Sasami/* c-argdecl-indent: 8 */
72213052Sasami/* c-indent-level: 8 */
72313052Sasami/* End: */
724