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