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