xref: /titanic_41/usr/src/lib/lvm/libsvm/common/metaconf.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <sys/types.h>
33 #include <sys/mkdev.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <dirent.h>
37 #include <limits.h>
38 #include <string.h>
39 #include <libsvm.h>
40 #include <svm.h>
41 #include <errno.h>
42 
43 
44 #define	VERSION "1.0"
45 #define	DISK_DIR "/dev/rdsk"
46 
47 extern int _map_to_effective_dev();
48 
49 int
50 is_blankline(char *buf)
51 {
52 	for (; *buf != 0; buf++) {
53 		if (!isspace(*buf))
54 			return (0);
55 	}
56 	return (1);
57 }
58 
59 /*
60  * FUNCTION: write_targ_nm_table
61  *	creates a tuple table of <driver name, major number > in md.conf
62  * INPUT: rootpath
63  *
64  * RETURN VALUES:
65  *	RET_SUCCESS
66  *	RET_ERROR
67  */
68 
69 int
70 write_targ_nm_table(char *path)
71 {
72 	FILE	*targfp = NULL;
73 	FILE	*mdfp = NULL;
74 	char	buf[PATH_MAX], *cp;
75 	int	retval = RET_SUCCESS;
76 	int	first_entry = 1;
77 
78 	if ((mdfp = fopen(MD_CONF, "a")) == NULL)
79 		return (RET_ERROR);
80 
81 	(void) snprintf(buf, sizeof (buf), "%s%s", path, NAME_TO_MAJOR);
82 
83 	if ((targfp = fopen(buf, "r")) == NULL) {
84 		(void) fclose(mdfp);
85 		return (RET_ERROR);
86 	}
87 
88 	while (fgets(buf, PATH_MAX, targfp) != NULL &&
89 				(retval == RET_SUCCESS)) {
90 		cp = strrchr(buf, '\n');
91 		*cp = 0;
92 		if (is_blankline(buf))
93 			continue;
94 		if (first_entry) {
95 			if (fprintf(mdfp, "md_targ_nm_table=\"%s\"", buf) < 0)
96 				retval = RET_ERROR;
97 			first_entry = 0;
98 		}
99 		if (fprintf(mdfp, ",\"%s\"", buf) < 0)
100 				retval = RET_ERROR;
101 	}
102 	if (!first_entry)
103 		if (fprintf(mdfp, ";\n") < 0)
104 			retval = RET_ERROR;
105 	(void) fclose(mdfp);
106 	(void) fclose(targfp);
107 	return (retval);
108 }
109 
110 /*
111  * FUNCTION: write_xlate_to_mdconf
112  *	creates a tuple table of <miniroot devt, target devt> in md.conf
113  * INPUT: rootpath
114  *
115  * RETURN VALUES:
116  *	RET_SUCCESS
117  *	RET_ERROR
118  */
119 
120 int
121 write_xlate_to_mdconf(char *path)
122 {
123 	FILE		*fptr = NULL;
124 	struct dirent	*dp;
125 	DIR		*dirp;
126 	struct stat	statb_dev;
127 	struct stat	statb_edev;
128 	char		*devname;
129 	char		edevname[PATH_MAX];
130 	char		targname[PATH_MAX];
131 	char		diskdir[PATH_MAX];
132 	int		first_devid = 1;
133 	int		ret = RET_SUCCESS;
134 
135 	if ((fptr = fopen(MD_CONF, "a")) == NULL) {
136 		return (RET_ERROR);
137 	}
138 
139 
140 	(void) snprintf(diskdir, sizeof (diskdir), "%s%s", path, DISK_DIR);
141 	if ((dirp = opendir(diskdir)) == NULL) {
142 		(void) fclose(fptr);
143 		return (RET_ERROR);
144 	}
145 
146 	/* special case to write the first tuple in the table */
147 	while (((dp = readdir(dirp)) != (struct dirent *)0) &&
148 						(ret != RET_ERROR)) {
149 		if ((strcmp(dp->d_name, ".") == 0) ||
150 		    (strcmp(dp->d_name, "..") == 0))
151 			continue;
152 
153 		if ((strlen(diskdir) + strlen(dp->d_name) + 2) > PATH_MAX) {
154 		    continue;
155 		}
156 
157 		(void) snprintf(targname, sizeof (targname), "%s/%s",
158 		    diskdir, dp->d_name);
159 
160 		if (stat(targname, &statb_dev) != 0) {
161 		    continue;
162 		}
163 
164 		if ((devname = strstr(targname, DISK_DIR)) == NULL) {
165 			continue;
166 		}
167 
168 		if (_map_to_effective_dev((char *)devname, (char *)&edevname)
169 		    != 0) {
170 			continue;
171 		}
172 
173 		if (stat(edevname, &statb_edev) != 0) {
174 			continue;
175 		}
176 
177 		if (first_devid) {
178 			if (fprintf(fptr, "md_xlate_ver=\"%s\";\n"
179 				"md_xlate=%lu,%lu", VERSION,
180 				statb_edev.st_rdev, statb_dev.st_rdev) < 0)
181 				ret = RET_ERROR;
182 			first_devid = 0;
183 		}
184 		if (fprintf(fptr, ",%lu,%lu", statb_edev.st_rdev,
185 			statb_dev.st_rdev) < 0)
186 			ret = RET_ERROR;
187 	} /* end while */
188 
189 	if (!first_devid)
190 		if (fprintf(fptr, ";\n") < 0)
191 			ret = RET_ERROR;
192 	(void) fclose(fptr);
193 	(void) closedir(dirp);
194 	return (ret);
195 }
196