1 /*
2  * finddev.c -- this routine attempts to find a particular device in
3  * 	/dev
4  *
5  * Copyright (C) 2000 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Library
9  * General Public License, version 2.
10  * %End-Header%
11  */
12 
13 #include "config.h"
14 #include <stdio.h>
15 #include <string.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <stdlib.h>
20 #include <string.h>
21 #if HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24 #if HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #endif
27 #include <dirent.h>
28 #if HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #if HAVE_SYS_MKDEV_H
32 #include <sys/mkdev.h>
33 #endif
34 #ifdef HAVE_SYS_SYSMACROS_H
35 #include <sys/sysmacros.h>
36 #endif
37 
38 #include "ext2_fs.h"
39 #include "ext2fs.h"
40 #include "ext2fsP.h"
41 
42 struct dir_list {
43 	char	*name;
44 	struct dir_list *next;
45 };
46 
47 /*
48  * This function adds an entry to the directory list
49  */
add_to_dirlist(const char * name,struct dir_list ** list)50 static void add_to_dirlist(const char *name, struct dir_list **list)
51 {
52 	struct dir_list *dp;
53 
54 	dp = malloc(sizeof(struct dir_list));
55 	if (!dp)
56 		return;
57 	dp->name = malloc(strlen(name)+1);
58 	if (!dp->name) {
59 		free(dp);
60 		return;
61 	}
62 	strcpy(dp->name, name);
63 	dp->next = *list;
64 	*list = dp;
65 }
66 
67 /*
68  * This function frees a directory list
69  */
free_dirlist(struct dir_list ** list)70 static void free_dirlist(struct dir_list **list)
71 {
72 	struct dir_list *dp, *next;
73 
74 	for (dp = *list; dp; dp = next) {
75 		next = dp->next;
76 		free(dp->name);
77 		free(dp);
78 	}
79 	*list = 0;
80 }
81 
scan_dir(char * dirname,dev_t device,struct dir_list ** list,char ** ret_path)82 static int scan_dir(char *dirname, dev_t device, struct dir_list **list,
83 		    char **ret_path)
84 {
85 	DIR	*dir;
86 	struct dirent *dp;
87 	char	path[1024], *cp;
88 	int	dirlen;
89 	struct stat st;
90 
91 	dirlen = strlen(dirname);
92 	if ((dir = opendir(dirname)) == NULL)
93 		return errno;
94 	dp = readdir(dir);
95 	while (dp) {
96 		if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path))
97 			goto skip_to_next;
98 		if (dp->d_name[0] == '.' &&
99 		    ((dp->d_name[1] == 0) ||
100 		     ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
101 			goto skip_to_next;
102 		sprintf(path, "%s/%s", dirname, dp->d_name);
103 		if (stat(path, &st) < 0)
104 			goto skip_to_next;
105 		if (S_ISDIR(st.st_mode))
106 			add_to_dirlist(path, list);
107 		if (ext2fsP_is_disk_device(st.st_mode) &&
108 		    st.st_rdev == device) {
109 			cp = malloc(strlen(path)+1);
110 			if (!cp) {
111 				closedir(dir);
112 				return ENOMEM;
113 			}
114 			strcpy(cp, path);
115 			*ret_path = cp;
116 			goto success;
117 		}
118 	skip_to_next:
119 		dp = readdir(dir);
120 	}
121 success:
122 	closedir(dir);
123 	return 0;
124 }
125 
126 /*
127  * This function finds the pathname to a block device with a given
128  * device number.  It returns a pointer to allocated memory to the
129  * pathname on success, and NULL on failure.
130  */
ext2fs_find_block_device(dev_t device)131 char *ext2fs_find_block_device(dev_t device)
132 {
133 	struct dir_list *list = 0, *new_list = 0;
134 	struct dir_list *current;
135 	char	*ret_path = 0;
136 	int    level = 0;
137 
138 	/*
139 	 * Add the starting directories to search...
140 	 */
141 	add_to_dirlist("/devices", &list);
142 	add_to_dirlist("/devfs", &list);
143 	add_to_dirlist("/dev", &list);
144 
145 	while (list) {
146 		current = list;
147 		list = list->next;
148 #ifdef DEBUG
149 		printf("Scanning directory %s\n", current->name);
150 #endif
151 		scan_dir(current->name, device, &new_list, &ret_path);
152 		free(current->name);
153 		free(current);
154 		if (ret_path)
155 			break;
156 		/*
157 		 * If we're done checking at this level, descend to
158 		 * the next level of subdirectories. (breadth-first)
159 		 */
160 		if (list == 0) {
161 			list = new_list;
162 			new_list = 0;
163 			/* Avoid infinite loop */
164 			if (++level >= EXT2FS_MAX_NESTED_LINKS)
165 				break;
166 		}
167 	}
168 	free_dirlist(&list);
169 	free_dirlist(&new_list);
170 	return ret_path;
171 }
172 
173 
174 #ifdef DEBUG
main(int argc,char ** argv)175 int main(int argc, char** argv)
176 {
177 	char	*devname, *tmp;
178 	int	major, minor;
179 	dev_t	device;
180 	const char *errmsg = "Couldn't parse %s: %s\n";
181 
182 	if ((argc != 2) && (argc != 3)) {
183 		fprintf(stderr, "Usage: %s device_number\n", argv[0]);
184 		fprintf(stderr, "\t: %s major minor\n", argv[0]);
185 		exit(1);
186 	}
187 	if (argc == 2) {
188 		device = strtoul(argv[1], &tmp, 0);
189 		if (*tmp) {
190 			fprintf(stderr, errmsg, "device number", argv[1]);
191 			exit(1);
192 		}
193 	} else {
194 		major = strtoul(argv[1], &tmp, 0);
195 		if (*tmp) {
196 			fprintf(stderr, errmsg, "major number", argv[1]);
197 			exit(1);
198 		}
199 		minor = strtoul(argv[2], &tmp, 0);
200 		if (*tmp) {
201 			fprintf(stderr, errmsg, "minor number", argv[2]);
202 			exit(1);
203 		}
204 		device = makedev(major, minor);
205 		printf("Looking for device 0x%04x (%d:%d)\n", device,
206 		       major, minor);
207 	}
208 	devname = ext2fs_find_block_device(device);
209 	if (devname) {
210 		printf("Found device!  %s\n", devname);
211 		free(devname);
212 	} else {
213 		printf("Couldn't find device.\n");
214 	}
215 	return 0;
216 }
217 
218 #endif
219