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