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 2003 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 #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <meta.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/mkdev.h>
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <limits.h>
38*0Sstevel@tonic-gate #include <svm.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * FUNCTION: valid_bootlist
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * INPUT: file pointer, line buffer, line_length
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * RETURN VALUES:
46*0Sstevel@tonic-gate  *	0 - SUCCESS
47*0Sstevel@tonic-gate  *	-1 - FAIL
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate int
52*0Sstevel@tonic-gate valid_bootlist(FILE *fp, int line_len)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate 	char *bp = NULL;
55*0Sstevel@tonic-gate 	char *line;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	/*
58*0Sstevel@tonic-gate 	 * errno may not be cleared by callee routines and we
59*0Sstevel@tonic-gate 	 * we want to catch fgets failures hence errno is reset.
60*0Sstevel@tonic-gate 	 */
61*0Sstevel@tonic-gate 	errno = 0;
62*0Sstevel@tonic-gate 	if ((line = malloc(line_len)) == NULL)
63*0Sstevel@tonic-gate 		return (RET_ERROR);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	while (fgets(line, line_len, fp) != NULL) {
66*0Sstevel@tonic-gate 		bp = strstr(line, "mddb_bootlist");
67*0Sstevel@tonic-gate 		if (bp != NULL) {
68*0Sstevel@tonic-gate 			/* if not commented out then breakout */
69*0Sstevel@tonic-gate 			if (*line != '*' && *line != '#') {
70*0Sstevel@tonic-gate 				break;
71*0Sstevel@tonic-gate 			}
72*0Sstevel@tonic-gate 		}
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	free(line);
76*0Sstevel@tonic-gate 	if (bp == NULL || errno != 0)
77*0Sstevel@tonic-gate 		return (RET_ERROR);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	return (RET_SUCCESS);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate  * FUNCTION: svm_check
84*0Sstevel@tonic-gate  *	Check the existance of DiskSuite or SVM
85*0Sstevel@tonic-gate  *
86*0Sstevel@tonic-gate  * INPUT: rootpath
87*0Sstevel@tonic-gate  *
88*0Sstevel@tonic-gate  * RETURN VALUES:
89*0Sstevel@tonic-gate  *	0 - SUCCESS
90*0Sstevel@tonic-gate  *	-1 - FAIL
91*0Sstevel@tonic-gate  */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate int
94*0Sstevel@tonic-gate svm_check(char *path)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	FILE *fp;
97*0Sstevel@tonic-gate 	char tmppath[PATH_MAX];
98*0Sstevel@tonic-gate 	int rval;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	(void) strcat(strcpy(tmppath, path), MD_CONF);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if ((fp = fopen(tmppath, "r")) == NULL) {
103*0Sstevel@tonic-gate 		rval = errno;
104*0Sstevel@tonic-gate 		goto free_exit;
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	rval = valid_bootlist(fp, MDDB_BOOTLIST_MAX_LEN);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	debug_printf("svm_check(): valid bootlist in %s. status %d\n",
110*0Sstevel@tonic-gate 		tmppath, rval);
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	if (rval == RET_SUCCESS) {
113*0Sstevel@tonic-gate 		goto free_exit;
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 	(void) fclose(fp);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* not found in md.conf  try etc/system */
118*0Sstevel@tonic-gate 	(void) strcat(strcpy(tmppath, path), SYSTEM_FILE);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if ((fp = fopen(tmppath, "r")) == NULL) {
121*0Sstevel@tonic-gate 		rval = errno;
122*0Sstevel@tonic-gate 		goto free_exit;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	rval = valid_bootlist(fp, MDDB_BOOTLIST_MAX_LEN);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	debug_printf("svm_check(): valid bootlist in %s. status %d\n",
128*0Sstevel@tonic-gate 		tmppath, rval);
129*0Sstevel@tonic-gate free_exit:
130*0Sstevel@tonic-gate 	(void) fclose(fp);
131*0Sstevel@tonic-gate 	if (rval > 0)
132*0Sstevel@tonic-gate 		rval = RET_ERROR;
133*0Sstevel@tonic-gate 	return (rval);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate  * FUNCTION: svm_is_md
138*0Sstevel@tonic-gate  *	Check if the the given device name has an md driver.
139*0Sstevel@tonic-gate  * INPUT: special device name (/dev/dsk/c0t0d0s0 or /dev/md/dsk/d10)
140*0Sstevel@tonic-gate  *
141*0Sstevel@tonic-gate  * RETURN:
142*0Sstevel@tonic-gate  *	1 - if it is a metadevice.
143*0Sstevel@tonic-gate  *	0 - if it is not a metadevice.
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate int
147*0Sstevel@tonic-gate svm_is_md(char *device_name)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	char buf[30];
150*0Sstevel@tonic-gate 	struct stat sbuf;
151*0Sstevel@tonic-gate 	int rval = 0;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	(void) memset(buf, 0, 30);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	debug_printf("svm_is_md(): device %s\n", device_name);
156*0Sstevel@tonic-gate 	if (stat(device_name, &sbuf) != 0)
157*0Sstevel@tonic-gate 		return (RET_ERROR);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (get_drv_name(major(sbuf.st_rdev), "/", buf) == RET_ERROR) {
160*0Sstevel@tonic-gate 		debug_printf("svm_is_md(): device get_drv_name failed: %s\n",
161*0Sstevel@tonic-gate 				device_name);
162*0Sstevel@tonic-gate 		return (0);
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 	if (strcmp(buf, MD_MODULE) == 0) {
165*0Sstevel@tonic-gate 		debug_printf("svm_is_md(): device %s succeed\n", device_name);
166*0Sstevel@tonic-gate 		rval = 1;
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate 	return (rval);
169*0Sstevel@tonic-gate }
170