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/param.h>
41 #include <sys/diskslice.h>
42 #include <sys/sysctl.h>
43 
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "libaura/dict.h"
50 
51 #include "commands.h"
52 #include "diskutil.h"
53 #include "functions.h"
54 
55 static int	fgets_chomp(char *, int, FILE *);
56 static int	parse_geometry_info(char *, int *, int *, int *);
57 static int	parse_slice_info(char *, int *,
58 		    unsigned long *, unsigned long *, int *, int *);
59 
60 /*
61  * Get a line from a file.  Remove any trailing EOL's.
62  * Return 1 if we did not hit EOF, 0 if we did.
63  */
64 static int
65 fgets_chomp(char *line, int size, FILE *f)
66 {
67 	if (fgets(line, size, f) == NULL)
68 		return(0);
69 	while (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
70 		line[strlen(line) - 1] = '\0';
71 	return(1);
72 }
73 
74 /*
75  * Given a geometry line from fdisk's summary output, return the
76  * number of cylinders, heads, and sectors.
77  */
78 static int
79 parse_geometry_info(char *line, int *cyl, int *head, int *sec)
80 {
81 	char *word;
82 
83 	/*
84 	 * /dev/ad3: 2112 cyl 16 hd 63 sec
85 	 */
86 	if ((word = strtok(line, " \t")) == NULL)	/* /dev/ad3: */
87 		return(0);
88 	if ((word = strtok(NULL, " \t")) == NULL)	/* 2112 */
89 		return(0);
90 	*cyl = atoi(word);
91 	if ((word = strtok(NULL, " \t")) == NULL)	/* cyl */
92 		return(0);
93 	if ((word = strtok(NULL, " \t")) == NULL)	/* 16 */
94 		return(0);
95 	*head = atoi(word);
96 	if ((word = strtok(NULL, " \t")) == NULL)	/* hd */
97 		return(0);
98 	if ((word = strtok(NULL, " \t")) == NULL)	/* 63 */
99 		return(0);
100 	*sec = atoi(word);
101 
102 	return(1);
103 }
104 
105 /*
106  * Given a slice description line from fdisk's summary output, return
107  * the number of the slice, and its start, size, type, and flags.
108  */
109 static int
110 parse_slice_info(char *line, int *slice,
111 		 unsigned long *start, unsigned long *size,
112 		 int *type, int *flags)
113 {
114 	char *word;
115 
116 	/*
117 	 * Part        Start        Size Type Flags
118 	 *    1:          63     2128833 0xa5 0x80
119 	 */
120 	if ((word = strtok(line, " \t")) == NULL)	/* 1: */
121 		return(0);
122 	*slice = atoi(word);
123 	if ((word = strtok(NULL, " \t")) == NULL)	/* 63 */
124 		return(0);
125 	*start = strtoul(word, NULL, 10);
126 	if ((word = strtok(NULL, " \t")) == NULL)	/* 2128833 */
127 		return(0);
128 	*size = strtoul(word, NULL, 10);
129 	if ((word = strtok(NULL, " \t")) == NULL)	/* 0xa5 */
130 		return(0);
131 	if (!hex_to_int(word, type))
132 		return(0);
133 	if ((word = strtok(NULL, " \t")) == NULL)	/* 0x80 */
134 		return(0);
135 	if (!hex_to_int(word, flags))
136 		return(0);
137 
138 	return(1);
139 }
140 
141 /*
142  * Survey storage capacity of this system.
143  */
144 int
145 survey_storage(struct i_fn_args *a)
146 {
147 	unsigned long mem = 0;
148 	char disks[256], line[256];
149 	char *disk, *disk_ptr;
150 	struct commands *cmds;
151 	struct command *cmd;
152 	FILE *f;
153 	char *filename;
154 	struct disk *d = NULL;
155 	int number = 0;
156 	int failure = 0;
157 	int fd;
158 	size_t len;
159 	struct aura_dict *di;
160 	void *rk;
161 	size_t rk_len;
162 	struct partinfo diskpart;
163 	char diskpath[PATH_MAX];
164 
165 	disks_free(a->s);
166 
167 	len = sizeof(mem);
168 	if (sysctlbyname("hw.physmem", &mem, &len, NULL, 0) < 0) {
169 		failure |= 1;
170 	} else {
171 		storage_set_memsize(a->s, next_power_of_two(mem >> 20));
172 	}
173 	len = 256;
174 	if (sysctlbyname("kern.disks", disks, &len, NULL, 0) < 0) {
175 		failure |= 1;
176 	}
177 	disk_ptr = disks;
178 
179 	di = aura_dict_new(1, AURA_DICT_SORTED_LIST);
180 	while (!failure && (disk = strsep(&disk_ptr, " ")) != NULL) {
181 		if (disk[0] == '\0')
182 			continue;
183 
184 		/*
185 		 * If the disk is a memory disk, floppy or CD-ROM, skip it.
186 		 */
187 		if (strncmp(disk, "md", 2) == 0 ||
188 		    strncmp(disk, "cd", 2) == 0 ||
189 		    strncmp(disk, "acd", 3) == 0 ||
190 		    strncmp(disk, "fd", 2) == 0)
191 			continue;
192 
193 		aura_dict_store(di, disk, strlen(disk) + 1, "", 1);
194 	}
195 
196 	cmds = commands_new();
197 	cmd = command_add(cmds, "%s%s -n '' >%ssurvey.txt",
198 	    a->os_root, cmd_name(a, "ECHO"), a->tmp);
199 	command_set_log_mode(cmd, COMMAND_LOG_SILENT);
200 
201 	aura_dict_rewind(di);
202 	while (!aura_dict_eof(di)) {
203 		aura_dict_get_current_key(di, &rk, &rk_len),
204 
205 		disk = (char *)rk;
206 
207 		cmd = command_add(cmds, "%s%s '@DISK' >>%ssurvey.txt",
208 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
209 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
210 		cmd = command_add(cmds, "%s%s '%s' >>%ssurvey.txt",
211 		    a->os_root, cmd_name(a, "ECHO"), disk, a->tmp);
212 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
213 
214 		/*
215 		 * Look for descriptions of this disk.
216 		 */
217 		cmd = command_add(cmds, "%s%s '@DESC' >>%ssurvey.txt",
218 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
219 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
220 		snprintf(diskpath, PATH_MAX, "/dev/%s", disk);
221 		if ((fd = open(diskpath, O_RDONLY)) < 0)
222 			failure |= 1;
223 		bzero(&diskpart, sizeof(diskpart));
224 		if (ioctl(fd, DIOCGPART, &diskpart) < 0)
225 			failure |= 1;
226 		cmd = command_add(cmds, "%s%s '%s: %luMB' >>%ssurvey.txt",
227 		    a->os_root, cmd_name(a, "ECHO"),
228 		    disk,
229 		    diskpart.media_size / 1024 / 1024,
230 		    a->tmp);
231 		close(fd);
232 		cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
233 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
234 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
235 
236 		/*
237 		 * Look for the disk's serial number.
238 		 */
239 		cmd = command_add(cmds, "%s%s '@SERNO' >>%ssurvey.txt",
240 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
241 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
242 		cmd = command_add(cmds, "if %s%s -d /dev/serno; then %s%s -l /dev/serno | %s%s \"`%s%s -l /dev/%s | %s%s '{print $5, $6;}'`\" | %s%s '{print $10;}' >>%ssurvey.txt; fi",
243 		    a->os_root, cmd_name(a, "TEST"),
244 		    a->os_root, cmd_name(a, "LS"),
245 		    a->os_root, cmd_name(a, "GREP"),
246 		    a->os_root, cmd_name(a, "LS"),
247 		    disk,
248 		    a->os_root, cmd_name(a, "AWK"),
249 		    a->os_root, cmd_name(a, "AWK"),
250 		    a->tmp);
251 		cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
252 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
253 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
254 
255 		/*
256 		 * Probe the disk with fdisk.
257 		 */
258 		cmd = command_add(cmds, "%s%s '@SLICES' >>%ssurvey.txt",
259 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
260 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
261 		cmd = command_add(cmds, "%s%s -s %s 2>/dev/null >>%ssurvey.txt || %s%s '' >>%ssurvey.txt",
262 		    a->os_root, cmd_name(a, "FDISK"),
263 		    disk,
264 		    a->tmp,
265 		    a->os_root, cmd_name(a, "ECHO"),
266 		    a->tmp);
267 		cmd = command_add(cmds, "%s%s '@END' >>%ssurvey.txt",
268 		    a->os_root, cmd_name(a, "ECHO"), a->tmp);
269 		command_set_log_mode(cmd, COMMAND_LOG_SILENT);
270 
271 		aura_dict_next(di);
272 	}
273 
274 	cmd = command_add(cmds, "%s%s '.' >>%ssurvey.txt",
275 	    a->os_root, cmd_name(a, "ECHO"), a->tmp);
276 	command_set_log_mode(cmd, COMMAND_LOG_SILENT);
277 
278 	if (!commands_execute(a, cmds))
279 		failure |= 1;
280 	commands_free(cmds);
281 	temp_file_add(a, "survey.txt");
282 
283 	aura_dict_free(di);
284 
285 	/*
286 	 * Now read in and parse the file that those commands just created.
287 	 */
288 	asprintf(&filename, "%ssurvey.txt", a->tmp);
289 	if ((f = fopen(filename, "r")) == NULL)
290 		failure |= 1;
291 	free(filename);
292 
293 	while (!failure && fgets_chomp(line, 255, f)) {
294 		if (strcmp(line, "@DISK") == 0) {
295 			if (fgets_chomp(line, 255, f)) {
296 				d = disk_new(a->s, line);
297 				disk_set_number(d, number++);
298 			}
299 		} else if (strcmp(line, "@DESC") == 0) {
300 			while (d != NULL && strcmp(line, "@END") != 0 && fgets_chomp(line, 255, f)) {
301 				disk_set_desc(d, line);
302 			}
303 		} else if (strcmp(line, "@SERNO") == 0) {
304 			fgets_chomp(line, 255, f);
305 			if (line[0] != '\0' && strcmp(line, "@END") != 0)
306 				disk_set_serno(d, line);
307 		} else if (strcmp(line, "@SLICES") == 0) {
308 			int cyl, hd, sec;
309 			int sliceno, type, flags;
310 			unsigned long start, size;
311 
312 			/*
313 			 * /dev/ad3: 2112 cyl 16 hd 63 sec
314 			 * Part        Start        Size Type Flags
315 			 *    1:          63     2128833 0xa5 0x80
316 			 */
317 			while (d != NULL && strcmp(line, "@END") != 0 && fgets_chomp(line, 255, f)) {
318 				if (strncmp(line, "/dev/", 5) == 0) {
319 					cyl = hd = sec = 0;
320 					parse_geometry_info(line, &cyl, &hd, &sec);
321 					disk_set_geometry(d, cyl, hd, sec);
322 				} else if (strncmp(line, "Part", 4) == 0) {
323 					/* ignore it */
324 				} else {
325 					if (parse_slice_info(line, &sliceno, &start, &size,
326 					    &type, &flags)) {
327 						/*
328 						fprintfo(log, "| Found slice #%d, sysid %d, "
329 						    "start %ld, size %ld\n", sliceno, type, start, size);
330 						*/
331 						slice_new(d, sliceno, type, flags, start, size);
332 					}
333 				}
334 			}
335 		}
336 	}
337 
338 	if (f != NULL)
339 		fclose(f);
340 
341 	/*
342 	 * Fix up any disk descriptions that didn't make it.
343 	 */
344 	for (d = storage_disk_first(a->s); d != NULL; d = disk_next(d)) {
345 		if (disk_get_desc(d) == NULL)
346 			disk_set_desc(d, disk_get_device_name(d));
347 	}
348 
349 	return(!failure);
350 }
351