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