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