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