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