xref: /onnv-gate/usr/src/lib/lvm/libmeta/common/meta_tab.c (revision 1623:7bac4a816ebe)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1623Stw21770  * Common Development and Distribution License (the "License").
6*1623Stw21770  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1623Stw21770  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Just in case we're not in a build environment, make sure that
300Sstevel@tonic-gate  * TEXT_DOMAIN gets set to something.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
330Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <meta.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <ctype.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * free md.tab struct
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate void
meta_tab_free(md_tab_t * tabp)440Sstevel@tonic-gate meta_tab_free(
450Sstevel@tonic-gate 	md_tab_t	*tabp
460Sstevel@tonic-gate )
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	size_t		line;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	Free(tabp->filename);
510Sstevel@tonic-gate 	Free(tabp->data);
520Sstevel@tonic-gate 	if (tabp->lines != NULL) {
530Sstevel@tonic-gate 		assert(tabp->alloc > 0);
540Sstevel@tonic-gate 		for (line = 0; (line < tabp->nlines); ++line) {
550Sstevel@tonic-gate 			md_tab_line_t	*linep = &tabp->lines[line];
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 			if (linep->context != NULL)
580Sstevel@tonic-gate 				Free(linep->context);
590Sstevel@tonic-gate 			if (linep->cname != NULL)
600Sstevel@tonic-gate 				Free(linep->cname);
610Sstevel@tonic-gate 			if (linep->argv != NULL) {
620Sstevel@tonic-gate 				assert(linep->alloc > 0);
630Sstevel@tonic-gate 				Free(linep->argv);
640Sstevel@tonic-gate 			}
650Sstevel@tonic-gate 		}
660Sstevel@tonic-gate 		Free(tabp->lines);
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 	Free(tabp);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * (re)allocate argv array
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate static void
realloc_argv(md_tab_line_t * linep,size_t argc)750Sstevel@tonic-gate realloc_argv(
760Sstevel@tonic-gate 	md_tab_line_t	*linep,
770Sstevel@tonic-gate 	size_t		argc
780Sstevel@tonic-gate )
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	/* allocate in chunks */
810Sstevel@tonic-gate 	argc = roundup(argc, TAB_ARG_ALLOC);
820Sstevel@tonic-gate 	if (argc < linep->alloc)
830Sstevel@tonic-gate 		return;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/* (re)allocate */
860Sstevel@tonic-gate 	if (linep->alloc == 0) {
870Sstevel@tonic-gate 		linep->argv = Malloc(argc * sizeof (*linep->argv));
880Sstevel@tonic-gate 	} else {
890Sstevel@tonic-gate 		assert(linep->argv != NULL);
900Sstevel@tonic-gate 		linep->argv =
910Sstevel@tonic-gate 		    Realloc(linep->argv, (argc * sizeof (*linep->argv)));
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/* zero out new stuff */
950Sstevel@tonic-gate 	(void) memset(&linep->argv[linep->alloc], 0,
960Sstevel@tonic-gate 	    ((argc - linep->alloc) * sizeof (*linep->argv)));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/* adjust for new size */
990Sstevel@tonic-gate 	linep->alloc = argc;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * (re)allocate line array
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate static void
realloc_lines(md_tab_t * tabp,size_t nlines)1060Sstevel@tonic-gate realloc_lines(
1070Sstevel@tonic-gate 	md_tab_t	*tabp,
1080Sstevel@tonic-gate 	size_t		nlines
1090Sstevel@tonic-gate )
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	/* allocate in chunks */
1120Sstevel@tonic-gate 	nlines = roundup(nlines, TAB_LINE_ALLOC);
1130Sstevel@tonic-gate 	if (nlines < tabp->alloc)
1140Sstevel@tonic-gate 		return;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/* (re)allocate */
1170Sstevel@tonic-gate 	if (tabp->alloc == 0) {
1180Sstevel@tonic-gate 		assert(tabp->lines == NULL);
1190Sstevel@tonic-gate 		tabp->lines = Malloc(nlines * sizeof (*tabp->lines));
1200Sstevel@tonic-gate 	} else {
1210Sstevel@tonic-gate 		assert(tabp->lines != NULL);
1220Sstevel@tonic-gate 		tabp->lines =
1230Sstevel@tonic-gate 		    Realloc(tabp->lines, (nlines * sizeof (*tabp->lines)));
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/* zero out new stuff */
1270Sstevel@tonic-gate 	(void) memset(&tabp->lines[tabp->alloc], 0,
1280Sstevel@tonic-gate 	    ((nlines - tabp->alloc) * sizeof (*tabp->lines)));
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	/* adjust for new size */
1310Sstevel@tonic-gate 	tabp->alloc = nlines;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * parse up md.tab struct
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate static void
parse_tab(md_tab_t * tabp,char * metatab_name,md_error_t * ep)1380Sstevel@tonic-gate parse_tab(
139*1623Stw21770 	md_tab_t	*tabp,
140*1623Stw21770 	char		*metatab_name,
141*1623Stw21770 	md_error_t	*ep
1420Sstevel@tonic-gate )
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	uint_t		lineno = 1;
1450Sstevel@tonic-gate 	char		*p = tabp->data;
1460Sstevel@tonic-gate 	char		*e = tabp->data + tabp->total - 1;
1470Sstevel@tonic-gate 	char		*context;
1480Sstevel@tonic-gate 	size_t		len;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/* we can count on '\n\0' as the last characters */
1510Sstevel@tonic-gate 	assert(tabp->total >= 2);
1520Sstevel@tonic-gate 	assert(tabp->data[tabp->total - 2] == '\n');
1530Sstevel@tonic-gate 	assert(tabp->data[tabp->total - 1] == '\0');
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/* allocate context buffer "file line XXX" */
1560Sstevel@tonic-gate 	assert(tabp->filename != NULL);
1570Sstevel@tonic-gate 	len = strlen(tabp->filename) +
1580Sstevel@tonic-gate 	    strlen(dgettext(TEXT_DOMAIN, "%s line %u")) + 20 + 1;
1590Sstevel@tonic-gate 	context = Malloc(len);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/* parse lines */
162*1623Stw21770 	while (p < e && *p != '\0') {
1630Sstevel@tonic-gate 		md_tab_line_t	*linep;
1640Sstevel@tonic-gate 		char		*t;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 		/* allocate new line */
1670Sstevel@tonic-gate 		realloc_lines(tabp, (tabp->nlines + 1));
1680Sstevel@tonic-gate 		linep = &tabp->lines[tabp->nlines];
1690Sstevel@tonic-gate 		(void) snprintf(context, len,
1700Sstevel@tonic-gate 		    dgettext(TEXT_DOMAIN, "%s line %u"), tabp->filename,
1710Sstevel@tonic-gate 		    lineno);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		/* comments */
1740Sstevel@tonic-gate 		if (*p == '#') {
1750Sstevel@tonic-gate 			while (*p != '\n')
1760Sstevel@tonic-gate 				++p;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		/* coalesce \ continuations */
1800Sstevel@tonic-gate 		t = p;
1810Sstevel@tonic-gate 		while (*t != '\n') {
1820Sstevel@tonic-gate 			if ((*t == '\\') && (*(t + 1) == '\n')) {
1830Sstevel@tonic-gate 				*t++ = ' ';
1840Sstevel@tonic-gate 				*t = ' ';
1850Sstevel@tonic-gate 				++lineno;
1860Sstevel@tonic-gate 			}
1870Sstevel@tonic-gate 			++t;
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		/* leading whitespace */
1910Sstevel@tonic-gate 		while ((*p != '\n') && (isspace(*p)))
1920Sstevel@tonic-gate 			++p;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		/* count lines */
1950Sstevel@tonic-gate 		if (*p == '\n') {
1960Sstevel@tonic-gate 			++p;
1970Sstevel@tonic-gate 			++lineno;
1980Sstevel@tonic-gate 			continue;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		/* tokenize line */
2020Sstevel@tonic-gate 		while ((p < e) && (*p != '\n')) {
2030Sstevel@tonic-gate 			char	**argvp;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 			/* allocate new token */
2060Sstevel@tonic-gate 			realloc_argv(linep, (linep->argc + 1));
2070Sstevel@tonic-gate 			argvp = &linep->argv[linep->argc++];
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 			/* find end of token */
2100Sstevel@tonic-gate 			*argvp = p;
2110Sstevel@tonic-gate 			while ((*p != '\n') && (! isspace(*p)))
2120Sstevel@tonic-gate 				++p;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 			/* terminate */
2150Sstevel@tonic-gate 			if (*p == '\n') {
2160Sstevel@tonic-gate 				*p++ = '\0';
2170Sstevel@tonic-gate 				++lineno;
2180Sstevel@tonic-gate 				break;
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 			/* eat white space */
2220Sstevel@tonic-gate 			*p++ = '\0';
2230Sstevel@tonic-gate 			while ((p < e) && (*p != '\n') && (isspace(*p)))
2240Sstevel@tonic-gate 				++p;
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 		tabp->nlines++;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		/* fill in the rest */
2290Sstevel@tonic-gate 		assert((linep->argc > 0) && (linep->argv != NULL) &&
2300Sstevel@tonic-gate 		    (linep->argv[0][0] != '\0') &&
2310Sstevel@tonic-gate 		    (! isspace(linep->argv[0][0])));
2320Sstevel@tonic-gate 		linep->context = Strdup(context);
2330Sstevel@tonic-gate 		linep->type = meta_get_init_type(linep->argc, linep->argv);
234*1623Stw21770 		linep->cname = meta_canonicalize(NULL, linep->argv[0]);
235*1623Stw21770 		/* if cname is NULL then the meta/hsp name is invalid */
236*1623Stw21770 		if (linep->cname == NULL) {
237*1623Stw21770 			(void) mderror(ep, MDE_SYNTAX, metatab_name);
238*1623Stw21770 			break;
239*1623Stw21770 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/* cleanup */
2430Sstevel@tonic-gate 	Free(context);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * read in md.tab file and return struct
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate md_tab_t *
meta_tab_parse(char * filename,md_error_t * ep)2500Sstevel@tonic-gate meta_tab_parse(
2510Sstevel@tonic-gate 	char		*filename,
2520Sstevel@tonic-gate 	md_error_t	*ep
2530Sstevel@tonic-gate )
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	md_tab_t	*tabp = NULL;
2560Sstevel@tonic-gate 	int		fd = -1;
2570Sstevel@tonic-gate 	struct stat	statbuf;
2580Sstevel@tonic-gate 	size_t		sofar;
2590Sstevel@tonic-gate 	char		*p;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/* open tab file */
2620Sstevel@tonic-gate 	if (filename == NULL)
2630Sstevel@tonic-gate 		filename = METATAB;
2640Sstevel@tonic-gate 	if ((fd = open(filename, O_RDONLY, 0)) < 0) {
2650Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, filename);
2660Sstevel@tonic-gate 		goto out;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 	if (fstat(fd, &statbuf) != 0) {
2690Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, filename);
2700Sstevel@tonic-gate 		goto out;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	/* allocate table */
2740Sstevel@tonic-gate 	tabp = Zalloc(sizeof (*tabp));
2750Sstevel@tonic-gate 	tabp->filename = Strdup(filename);
2760Sstevel@tonic-gate 	tabp->total = statbuf.st_size + 2;	/* terminating "\n\0" */
2770Sstevel@tonic-gate 	tabp->data = Malloc(tabp->total);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/* read in data */
2800Sstevel@tonic-gate 	sofar = 0;
2810Sstevel@tonic-gate 	p = tabp->data;
2820Sstevel@tonic-gate 	while (sofar < statbuf.st_size) {
2830Sstevel@tonic-gate 		int	cnt;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		if ((cnt = read(fd, p, 8192)) < 0) {
2860Sstevel@tonic-gate 			(void) mdsyserror(ep, errno, filename);
2870Sstevel@tonic-gate 			goto out;
2880Sstevel@tonic-gate 		} else if (cnt == 0) {
2890Sstevel@tonic-gate 			(void) mderror(ep, MDE_SYNTAX, filename);
2900Sstevel@tonic-gate 			goto out;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 		sofar += cnt;
2930Sstevel@tonic-gate 		p += cnt;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 	tabp->data[tabp->total - 2] = '\n';
2960Sstevel@tonic-gate 	tabp->data[tabp->total - 1] = '\0';
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/* close file */
2990Sstevel@tonic-gate 	if (close(fd) != 0) {
3000Sstevel@tonic-gate 		(void) mdsyserror(ep, errno, filename);
3010Sstevel@tonic-gate 		fd = -1;
3020Sstevel@tonic-gate 		goto out;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	fd = -1;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/* parse it up */
307*1623Stw21770 	parse_tab(tabp, filename, ep);
3080Sstevel@tonic-gate 
309*1623Stw21770 	/* return success if file was correctly parsed */
310*1623Stw21770 	if (mdisok(ep))
311*1623Stw21770 		return (tabp);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* cleanup, return error */
3140Sstevel@tonic-gate out:
3150Sstevel@tonic-gate 	if (fd >= 0)
3160Sstevel@tonic-gate 		(void) close(fd);
3170Sstevel@tonic-gate 	if (tabp != NULL)
3180Sstevel@tonic-gate 		meta_tab_free(tabp);
3190Sstevel@tonic-gate 	return (NULL);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate  * find line in md.tab
3240Sstevel@tonic-gate  */
3250Sstevel@tonic-gate md_tab_line_t *
meta_tab_find(mdsetname_t * sp,md_tab_t * tabp,char * name,mdinittypes_t type)3260Sstevel@tonic-gate meta_tab_find(
3270Sstevel@tonic-gate 	mdsetname_t	*sp,
3280Sstevel@tonic-gate 	md_tab_t	*tabp,
3290Sstevel@tonic-gate 	char		*name,
3300Sstevel@tonic-gate 	mdinittypes_t	type
3310Sstevel@tonic-gate )
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	char		*cname = meta_canonicalize(sp, name);
3340Sstevel@tonic-gate 	size_t		line;
3350Sstevel@tonic-gate 
336*1623Stw21770 	/* if name is not legal meta name then return NULL */
337*1623Stw21770 	if (cname == NULL)
338*1623Stw21770 		return (NULL);
339*1623Stw21770 
3400Sstevel@tonic-gate 	for (line = 0; (line < tabp->nlines); ++line) {
3410Sstevel@tonic-gate 		md_tab_line_t	*linep = &tabp->lines[line];
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		assert((linep->argc > 0) && (linep->argv[0] != NULL));
3440Sstevel@tonic-gate 		if (((linep->type & type) != 0) &&
3450Sstevel@tonic-gate 		    (strcmp(linep->cname, cname) == 0)) {
3460Sstevel@tonic-gate 			Free(cname);
3470Sstevel@tonic-gate 			return (linep);
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	Free(cname);
3510Sstevel@tonic-gate 	return (NULL);
3520Sstevel@tonic-gate }
353