1 /*
2  * Copyright (c)2004 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of the DragonFly Project nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * survey.c
36  * Survey the storage capacity of the system.
37  * $Id: survey.c,v 1.17 2005/02/06 21:05:18 cpressey Exp $
38  */
39 
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
42 
43 #include <stdio.h>
44 #include <string.h>
45 
46 #include "libaura/dict.h"
47 
48 #include "commands.h"
49 #include "diskutil.h"
50 #include "functions.h"
51 
52 static int	fgets_chomp(char *, int, FILE *);
53 static int	parse_geometry_info(char *, int *, int *, int *);
54 static int	parse_slice_info(char *, int *,
55 		    unsigned long *, unsigned long *, int *, int *);
56 
57 /*
58  * Get a line from a file.  Remove any trailing EOL's.
59  * Return 1 if we did not hit EOF, 0 if we did.
60  */
61 static int
62 fgets_chomp(char *line, int size, FILE *f)
63 {
64 	if (fgets(line, size, f) == NULL)
65 		return(0);
66 	while (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
67 		line[strlen(line) - 1] = '\0';
68 	return(1);
69 }
70 
71 /*
72  * Given a geometry line from fdisk's summary output, return the
73  * number of cylinders, heads, and sectors.
74  */
75 static int
76 parse_geometry_info(char *line, int *cyl, int *head, int *sec)
77 {
78 	char *word;
79 
80 	/*
81 	 * /dev/ad3: 2112 cyl 16 hd 63 sec
82 	 */
83 	if ((word = strtok(line, " \t")) == NULL)	/* /dev/ad3: */
84 		return(0);
85 	if ((word = strtok(NULL, " \t")) == NULL)	/* 2112 */
86 		return(0);
87 	*cyl = atoi(word);
88 	if ((word = strtok(NULL, " \t")) == NULL)	/* cyl */
89 		return(0);
90 	if ((word = strtok(NULL, " \t")) == NULL)	/* 16 */
91 		return(0);
92 	*head = atoi(word);
93 	if ((word = strtok(NULL, " \t")) == NULL)	/* hd */
94 		return(0);
95 	if ((word = strtok(NULL, " \t")) == NULL)	/* 63 */
96 		return(0);
97 	*sec = atoi(word);
98 
99 	return(1);
100 }
101 
102 /*
103  * Given a slice description line from fdisk's summary output, return
104  * the number of the slice, and its start, size, type, and flags.
105  */
106 static int
107 parse_slice_info(char *line, int *slice,
108 		 unsigned long *start, unsigned long *size,
109 		 int *type, int *flags)
110 {
111 	char *word;
112 
113 	/*
114 	 * Part        Start        Size Type Flags
115 	 *    1:          63     2128833 0xa5 0x80
116 	 */
117 	if ((word = strtok(line, " \t")) == NULL)	/* 1: */
118 		return(0);
119 	*slice = atoi(word);
120 	if ((word = strtok(NULL, " \t")) == NULL)	/* 63 */
121 		return(0);
122 	*start = strtoul(word, NULL, 10);
123 	if ((word = strtok(NULL, " \t")) == NULL)	/* 2128833 */
124 		return(0);
125 	*size = strtoul(word, NULL, 10);
126 	if ((word = strtok(NULL, " \t")) == NULL)	/* 0xa5 */
127 		return(0);
128 	if (!hex_to_int(word, type))
129 		return(0);
130 	if ((word = strtok(NULL, " \t")) == NULL)	/* 0x80 */
131 		return(0);
132 	if (!hex_to_int(word, flags))
133 		return(0);
134 
135 	return(1);
136 }
137 
138 /*
139  * Survey storage capacity of this system.
140  */
141 int
142 survey_storage(struct i_fn_args *a)
143 {
144 	unsigned long mem = 0;
145 	char disks[256], line[256];
146 	char *disk, *disk_ptr;
147 	struct commands *cmds;
148 	struct command *cmd;
149 	FILE *f;
150 	char *filename;
151 	struct disk *d = NULL;
152 	int failure = 0;
153 	size_t len;
154 	struct aura_dict *di;
155 	void *rk;
156 	size_t rk_len;
157 
158 	disks_free(a->s);
159 
160 	len = sizeof(mem);
161 	if (sysctlbyname("hw.physmem", &mem, &len, NULL, 0) < 0) {
162 		failure |= 1;
163 	} else {
164 		storage_set_memsize(a->s, next_power_of_two(mem >> 20));
165 	}
166 	len = 256;
167 	if (sysctlbyname("kern.disks", disks, &len, NULL, 0) < 0) {
168 		failure |= 1;
169 	}
170 	disk_ptr = disks;
171 
172 	di = aura_dict_new(1, AURA_DICT_SORTED_LIST);
173 	while (!failure && (disk = strsep(&disk_ptr, " ")) != NULL) {
174 		if (disk[0] == '\0')
175 			continue;
176 
177 		/*
178 		 * If the disk is a memory disk, floppy or CD-ROM, skip it.
179 		 */
180 		if (strncmp(disk, "md", 2) == 0 ||
181 		    strncmp(disk, "cd", 2) == 0 ||
182 		    strncmp(disk, "acd", 3) == 0 ||
183 		    strncmp(disk, "fd", 2) == 0)
184 			continue;
185 
186 		aura_dict_store(di, disk, strlen(disk) + 1, "", 1);
187 	}
188 
189 	cmds = commands_new();
190 	cmd = command_add(cmds, "%s%s -n '' >%ssurvey.txt",
191 	    a->os_root, cmd_name(a, "ECHO"), a->tmp);
192 	command_set_log_mode(cmd, COMMAND_LOG_SILENT);
193 
194 	aura_dict_rewind(di);
195 	while (!aura_dict_eof(di)) {
196 		aura_dict_get_current_key(di, &rk, &rk_len),
197 
198 		disk = (char *)rk;
199 
200 		cmd = command_add(cmds, "%s%s '@DISK' >>%ssurvey.txt",
201 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
202 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
203 		cmd = command_add(cmds, "%s%s '%s' >>%ssurvey.txt",
204 		    a->os_root, cmd_name(a, "ECHO"), disk, a->tmp);
205 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
206 
207 		/*
208 		 * Look for descriptions of this disk.
209 		 */
210 		cmd = command_add(cmds, "%s%s '@DESC' >>%ssurvey.txt",
211 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
212 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
213 		cmd = command_add(cmds, "%s%s -w '^%s: [0-9]*MB' %s%s >>%ssurvey.txt || %s%s '%s' >>%ssurvey.txt",
214 		    a->os_root, cmd_name(a, "GREP"),
215 		    disk,
216 		    a->os_root, cmd_name(a, "DMESG_BOOT"),
217 		    a->tmp,
218 		    a->os_root, cmd_name(a, "ECHO"),
219 		    disk,
220 		    a->tmp);
221 		cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
222 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
223 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
224 
225 		/*
226 		 * Ensure that the device node for this disk exists,
227 		 */
228 		cmd = command_add_ensure_dev(a, cmds, disk);
229 
230 		/*
231 		 * Probe the disk with fdisk.
232 		 */
233 		cmd = command_add(cmds, "%s%s '@SLICES' >>%ssurvey.txt",
234 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
235 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
236 		cmd = command_add(cmds, "%s%s -s %s 2>/dev/null >>%ssurvey.txt || %s%s '' >>%ssurvey.txt",
237 		    a->os_root, cmd_name(a, "FDISK"),
238 		    disk,
239 		    a->tmp,
240 		    a->os_root, cmd_name(a, "ECHO"),
241 		    a->tmp);
242 		cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
243 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
244 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
245 
246 		aura_dict_next(di);
247 	}
248 
249 	cmd = command_add(cmds, "%s%s '.' >>%ssurvey.txt",
250 	    a->os_root, cmd_name(a, "ECHO"), a->tmp);
251 	command_set_log_mode(cmd, COMMAND_LOG_SILENT);
252 
253 	if (!commands_execute(a, cmds))
254 		failure |= 1;
255 	commands_free(cmds);
256 	temp_file_add(a, "survey.txt");
257 
258 	aura_dict_free(di);
259 
260 	/*
261 	 * Now read in and parse the file that those commands just created.
262 	 */
263 	asprintf(&filename, "%ssurvey.txt", a->tmp);
264 	if ((f = fopen(filename, "r")) == NULL)
265 		failure |= 1;
266 	free(filename);
267 
268 	while (!failure && fgets_chomp(line, 255, f)) {
269 		if (strcmp(line, "@DISK") == 0) {
270 			if (fgets_chomp(line, 255, f)) {
271 				d = disk_new(a->s, line);
272 			}
273 		} else if (strcmp(line, "@DESC") == 0) {
274 			while (d != NULL && strcmp(line, "@END") != 0 && fgets_chomp(line, 255, f)) {
275 				disk_set_desc(d, line);
276 			}
277 		} else if (strcmp(line, "@SLICES") == 0) {
278 			int cyl, hd, sec;
279 			int number, type, flags;
280 			unsigned long start, size;
281 
282 			/*
283 			 * /dev/ad3: 2112 cyl 16 hd 63 sec
284 			 * Part        Start        Size Type Flags
285 			 *    1:          63     2128833 0xa5 0x80
286 			 */
287 			while (d != NULL && strcmp(line, "@END") != 0 && fgets_chomp(line, 255, f)) {
288 				if (strncmp(line, "/dev/", 5) == 0) {
289 					parse_geometry_info(line, &cyl, &hd, &sec);
290 					disk_set_geometry(d, cyl, hd, sec);
291 				} else if (strncmp(line, "Part", 4) == 0) {
292 					/* ignore it */
293 				} else {
294 					if (parse_slice_info(line, &number, &start, &size,
295 					    &type, &flags)) {
296 						/*
297 						fprintfo(log, "| Found slice #%d, sysid %d, "
298 						    "start %ld, size %ld\n", number, type, start, size);
299 						*/
300 						slice_new(d, number, type, flags, start, size);
301 					}
302 				}
303 			}
304 		}
305 	}
306 
307 	if (f != NULL)
308 		fclose(f);
309 
310 	/*
311 	 * Fix up any disk descriptions that didn't make it.
312 	 */
313 	for (d = storage_disk_first(a->s); d != NULL; d = disk_next(d)) {
314 		if (disk_get_desc(d) == NULL)
315 			disk_set_desc(d, disk_get_device_name(d));
316 	}
317 
318 	return(!failure);
319 }
320