xref: /onnv-gate/usr/src/lib/lvm/libsvm/common/metaconf.c (revision 9824:cf352d74412c)
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
52805Seota  * Common Development and Distribution License (the "License").
62805Seota  * 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*9824SAndrew.Balfour@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <ctype.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/mkdev.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <dirent.h>
330Sstevel@tonic-gate #include <limits.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <libsvm.h>
360Sstevel@tonic-gate #include <svm.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	VERSION "1.0"
410Sstevel@tonic-gate #define	DISK_DIR "/dev/rdsk"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate extern int _map_to_effective_dev();
440Sstevel@tonic-gate 
452805Seota static int is_blank(char *);
462805Seota 
472805Seota /*
482805Seota  * is_blank() returns 1 (true) if a line specified is composed of
492805Seota  * whitespace characters only. otherwise, it returns 0 (false).
502805Seota  *
512805Seota  * Note. the argument (line) must be null-terminated.
522805Seota  */
532805Seota static int
is_blank(char * line)542805Seota is_blank(char *line)
550Sstevel@tonic-gate {
562805Seota 	for (/* nothing */; *line != '\0'; line++)
572805Seota 		if (!isspace(*line))
580Sstevel@tonic-gate 			return (0);
590Sstevel@tonic-gate 	return (1);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * FUNCTION: write_targ_nm_table
640Sstevel@tonic-gate  *	creates a tuple table of <driver name, major number > in md.conf
650Sstevel@tonic-gate  * INPUT: rootpath
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  * RETURN VALUES:
680Sstevel@tonic-gate  *	RET_SUCCESS
690Sstevel@tonic-gate  *	RET_ERROR
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int
write_targ_nm_table(char * path)730Sstevel@tonic-gate write_targ_nm_table(char *path)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	FILE	*targfp = NULL;
760Sstevel@tonic-gate 	FILE	*mdfp = NULL;
770Sstevel@tonic-gate 	char	buf[PATH_MAX], *cp;
780Sstevel@tonic-gate 	int	retval = RET_SUCCESS;
790Sstevel@tonic-gate 	int	first_entry = 1;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if ((mdfp = fopen(MD_CONF, "a")) == NULL)
820Sstevel@tonic-gate 		return (RET_ERROR);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s%s", path, NAME_TO_MAJOR);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if ((targfp = fopen(buf, "r")) == NULL) {
870Sstevel@tonic-gate 		(void) fclose(mdfp);
880Sstevel@tonic-gate 		return (RET_ERROR);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	while (fgets(buf, PATH_MAX, targfp) != NULL &&
92*9824SAndrew.Balfour@Sun.COM 	    (retval == RET_SUCCESS)) {
933036Seota 		/* remove a new-line character for md_targ_nm_table */
943036Seota 		if ((cp = strchr(buf, '\n')) != NULL)
953036Seota 			*cp = 0;
962805Seota 		/* cut off comments starting with '#' */
972805Seota 		if ((cp = strchr(buf, '#')) != NULL)
982805Seota 			*cp = 0;
992805Seota 		/* ignore comment or blank lines */
1002805Seota 		if (is_blank(buf))
1010Sstevel@tonic-gate 			continue;
1020Sstevel@tonic-gate 		if (first_entry) {
1030Sstevel@tonic-gate 			if (fprintf(mdfp, "md_targ_nm_table=\"%s\"", buf) < 0)
1040Sstevel@tonic-gate 				retval = RET_ERROR;
1050Sstevel@tonic-gate 			first_entry = 0;
1063036Seota 		} else {
1073036Seota 			if (fprintf(mdfp, ",\"%s\"", buf) < 0)
1083036Seota 					retval = RET_ERROR;
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	if (!first_entry)
1120Sstevel@tonic-gate 		if (fprintf(mdfp, ";\n") < 0)
1130Sstevel@tonic-gate 			retval = RET_ERROR;
1140Sstevel@tonic-gate 	(void) fclose(mdfp);
1150Sstevel@tonic-gate 	(void) fclose(targfp);
1160Sstevel@tonic-gate 	return (retval);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * FUNCTION: write_xlate_to_mdconf
1210Sstevel@tonic-gate  *	creates a tuple table of <miniroot devt, target devt> in md.conf
1220Sstevel@tonic-gate  * INPUT: rootpath
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  * RETURN VALUES:
1250Sstevel@tonic-gate  *	RET_SUCCESS
1260Sstevel@tonic-gate  *	RET_ERROR
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate int
write_xlate_to_mdconf(char * path)1300Sstevel@tonic-gate write_xlate_to_mdconf(char *path)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	FILE		*fptr = NULL;
1330Sstevel@tonic-gate 	struct dirent	*dp;
1340Sstevel@tonic-gate 	DIR		*dirp;
1350Sstevel@tonic-gate 	struct stat	statb_dev;
1360Sstevel@tonic-gate 	struct stat	statb_edev;
1370Sstevel@tonic-gate 	char		*devname;
1380Sstevel@tonic-gate 	char		edevname[PATH_MAX];
1390Sstevel@tonic-gate 	char		targname[PATH_MAX];
1400Sstevel@tonic-gate 	char		diskdir[PATH_MAX];
141*9824SAndrew.Balfour@Sun.COM 	char		linkpath[PATH_MAX];
1420Sstevel@tonic-gate 	int		first_devid = 1;
1430Sstevel@tonic-gate 	int		ret = RET_SUCCESS;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if ((fptr = fopen(MD_CONF, "a")) == NULL) {
1460Sstevel@tonic-gate 		return (RET_ERROR);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	(void) snprintf(diskdir, sizeof (diskdir), "%s%s", path, DISK_DIR);
1510Sstevel@tonic-gate 	if ((dirp = opendir(diskdir)) == NULL) {
1520Sstevel@tonic-gate 		(void) fclose(fptr);
1530Sstevel@tonic-gate 		return (RET_ERROR);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/* special case to write the first tuple in the table */
1570Sstevel@tonic-gate 	while (((dp = readdir(dirp)) != (struct dirent *)0) &&
158*9824SAndrew.Balfour@Sun.COM 	    (ret != RET_ERROR)) {
1590Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
1600Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
1610Sstevel@tonic-gate 			continue;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if ((strlen(diskdir) + strlen(dp->d_name) + 2) > PATH_MAX) {
164*9824SAndrew.Balfour@Sun.COM 			continue;
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		(void) snprintf(targname, sizeof (targname), "%s/%s",
1680Sstevel@tonic-gate 		    diskdir, dp->d_name);
1690Sstevel@tonic-gate 
170*9824SAndrew.Balfour@Sun.COM 		/*
171*9824SAndrew.Balfour@Sun.COM 		 * stat /devices to see if it's a devfs based file system
172*9824SAndrew.Balfour@Sun.COM 		 * On Solaris 10 and up, the devfs has been built on the
173*9824SAndrew.Balfour@Sun.COM 		 * fly for the mini-root. We need to adjust the path
174*9824SAndrew.Balfour@Sun.COM 		 * accordingly.
175*9824SAndrew.Balfour@Sun.COM 		 * If it's not devfs, just use the targname as it is.
176*9824SAndrew.Balfour@Sun.COM 		 */
177*9824SAndrew.Balfour@Sun.COM 
178*9824SAndrew.Balfour@Sun.COM 		if (stat("/devices", &statb_dev) != 0) {
179*9824SAndrew.Balfour@Sun.COM 			continue;
180*9824SAndrew.Balfour@Sun.COM 		}
181*9824SAndrew.Balfour@Sun.COM 
182*9824SAndrew.Balfour@Sun.COM 		if (strncmp("devfs", statb_dev.st_fstype, 5) == 0) {
183*9824SAndrew.Balfour@Sun.COM 			if (readlink(targname, linkpath,
184*9824SAndrew.Balfour@Sun.COM 			    sizeof (linkpath)) == -1) {
185*9824SAndrew.Balfour@Sun.COM 				continue;
186*9824SAndrew.Balfour@Sun.COM 			}
187*9824SAndrew.Balfour@Sun.COM 			/*
188*9824SAndrew.Balfour@Sun.COM 			 * turn ../../devices/<path> into /devices/<path>
189*9824SAndrew.Balfour@Sun.COM 			 * and stat that into statb_dev
190*9824SAndrew.Balfour@Sun.COM 			 */
191*9824SAndrew.Balfour@Sun.COM 			if (stat(strstr(linkpath, "/devices"),
192*9824SAndrew.Balfour@Sun.COM 			    &statb_dev) != 0) {
193*9824SAndrew.Balfour@Sun.COM 				continue;
194*9824SAndrew.Balfour@Sun.COM 			}
195*9824SAndrew.Balfour@Sun.COM 		} else {
196*9824SAndrew.Balfour@Sun.COM 			if (stat(targname, &statb_dev) != 0) {
197*9824SAndrew.Balfour@Sun.COM 				continue;
198*9824SAndrew.Balfour@Sun.COM 			}
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		if ((devname = strstr(targname, DISK_DIR)) == NULL) {
2020Sstevel@tonic-gate 			continue;
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		if (_map_to_effective_dev((char *)devname, (char *)&edevname)
2060Sstevel@tonic-gate 		    != 0) {
2070Sstevel@tonic-gate 			continue;
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 		if (stat(edevname, &statb_edev) != 0) {
2110Sstevel@tonic-gate 			continue;
2120Sstevel@tonic-gate 		}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		if (first_devid) {
2150Sstevel@tonic-gate 			if (fprintf(fptr, "md_xlate_ver=\"%s\";\n"
216*9824SAndrew.Balfour@Sun.COM 			    "md_xlate=%lu,%lu", VERSION,
217*9824SAndrew.Balfour@Sun.COM 			    statb_edev.st_rdev, statb_dev.st_rdev) < 0)
2180Sstevel@tonic-gate 				ret = RET_ERROR;
2190Sstevel@tonic-gate 			first_devid = 0;
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 		if (fprintf(fptr, ",%lu,%lu", statb_edev.st_rdev,
222*9824SAndrew.Balfour@Sun.COM 		    statb_dev.st_rdev) < 0)
2230Sstevel@tonic-gate 			ret = RET_ERROR;
2240Sstevel@tonic-gate 	} /* end while */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (!first_devid)
2270Sstevel@tonic-gate 		if (fprintf(fptr, ";\n") < 0)
2280Sstevel@tonic-gate 			ret = RET_ERROR;
2290Sstevel@tonic-gate 	(void) fclose(fptr);
2300Sstevel@tonic-gate 	(void) closedir(dirp);
2310Sstevel@tonic-gate 	return (ret);
2320Sstevel@tonic-gate }
233