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 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <stdlib.h>
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
21 #endif
22 #ifdef HAVE_SYS_STAT_H
23 #include <sys/stat.h>
24 #endif
25 #include <dirent.h>
26 #ifdef HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29 #ifdef HAVE_SYS_MKDEV_H
30 #include <sys/mkdev.h>
31 #endif
32 #include <fcntl.h>
33 #include <inttypes.h>
34 
35 #include "blkidP.h"
36 #include "pathnames.h"
37 #include "sysfs.h"
38 
blkid_strconcat(const char * a,const char * b,const char * c)39 static char *blkid_strconcat(const char *a, const char *b, const char *c)
40 {
41 	char *res, *p;
42 	size_t len, al, bl, cl;
43 
44 	al = a ? strlen(a) : 0;
45 	bl = b ? strlen(b) : 0;
46 	cl = c ? strlen(c) : 0;
47 
48 	len = al + bl + cl;
49 	if (!len)
50 		return NULL;
51 	p = res = malloc(len + 1);
52 	if (!res)
53 		return NULL;
54 	if (al) {
55 		memcpy(p, a, al);
56 		p += al;
57 	}
58 	if (bl) {
59 		memcpy(p, b, bl);
60 		p += bl;
61 	}
62 	if (cl) {
63 		memcpy(p, c, cl);
64 		p += cl;
65 	}
66 	*p = '\0';
67 	return res;
68 }
69 
70 /*
71  * This function adds an entry to the directory list
72  */
add_to_dirlist(const char * dir,const char * subdir,struct dir_list ** list)73 static void add_to_dirlist(const char *dir, const char *subdir,
74 				struct dir_list **list)
75 {
76 	struct dir_list *dp;
77 
78 	dp = malloc(sizeof(struct dir_list));
79 	if (!dp)
80 		return;
81 	dp->name = subdir ? blkid_strconcat(dir, "/", subdir) :
82 		   dir ? strdup(dir) : NULL;
83 
84 	if (!dp->name) {
85 		free(dp);
86 		return;
87 	}
88 	dp->next = *list;
89 	*list = dp;
90 }
91 
92 /*
93  * This function frees a directory list
94  */
free_dirlist(struct dir_list ** list)95 static void free_dirlist(struct dir_list **list)
96 {
97 	struct dir_list *dp, *next;
98 
99 	for (dp = *list; dp; dp = next) {
100 		next = dp->next;
101 		free(dp->name);
102 		free(dp);
103 	}
104 	*list = NULL;
105 }
106 
blkid__scan_dir(char * dirname,dev_t devno,struct dir_list ** list,char ** devname)107 void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list,
108 		     char **devname)
109 {
110 	DIR	*dir;
111 	struct dirent *dp;
112 	struct stat st;
113 
114 	if ((dir = opendir(dirname)) == NULL)
115 		return;
116 
117 	while ((dp = readdir(dir)) != NULL) {
118 #ifdef _DIRENT_HAVE_D_TYPE
119 		if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_BLK &&
120 		    dp->d_type != DT_LNK && dp->d_type != DT_DIR)
121 			continue;
122 #endif
123 		if (dp->d_name[0] == '.' &&
124 		    ((dp->d_name[1] == 0) ||
125 		     ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
126 			continue;
127 
128 		if (fstatat(dirfd(dir), dp->d_name, &st, 0))
129 			continue;
130 
131 		if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
132 			*devname = blkid_strconcat(dirname, "/", dp->d_name);
133 			DBG(DEVNO, ul_debug("found 0x%llx at %s", (long long)devno,
134 				   *devname));
135 			break;
136 		}
137 
138 		if (!list || !S_ISDIR(st.st_mode))
139 			continue;
140 
141 		/* add subdirectory (but not symlink) to the list */
142 #ifdef _DIRENT_HAVE_D_TYPE
143 		if (dp->d_type == DT_LNK)
144 			continue;
145 		if (dp->d_type == DT_UNKNOWN)
146 #endif
147 		{
148 			if (fstatat(dirfd(dir), dp->d_name, &st, AT_SYMLINK_NOFOLLOW) ||
149 			    !S_ISDIR(st.st_mode))
150 				continue;	/* symlink or fstatat() failed */
151 		}
152 
153 		if (*dp->d_name == '.' || (
154 #ifdef _DIRENT_HAVE_D_TYPE
155 		    dp->d_type == DT_DIR &&
156 #endif
157 		    strcmp(dp->d_name, "shm") == 0))
158 			/* ignore /dev/.{udev,mount,mdadm} and /dev/shm */
159 			continue;
160 
161 		add_to_dirlist(dirname, dp->d_name, list);
162 	}
163 	closedir(dir);
164 }
165 
166 /* Directories where we will try to search for device numbers */
167 static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
168 
169 /**
170  * SECTION: misc
171  * @title: Miscellaneous utils
172  * @short_description: mix of various utils for low-level and high-level API
173  */
174 
175 
176 
scandev_devno_to_devpath(dev_t devno)177 static char *scandev_devno_to_devpath(dev_t devno)
178 {
179 	struct dir_list *list = NULL, *new_list = NULL;
180 	char *devname = NULL;
181 	const char **dir;
182 
183 	/*
184 	 * Add the starting directories to search in reverse order of
185 	 * importance, since we are using a stack...
186 	 */
187 	for (dir = devdirs; *dir; dir++)
188 		add_to_dirlist(*dir, NULL, &list);
189 
190 	while (list) {
191 		struct dir_list *current = list;
192 
193 		list = list->next;
194 		DBG(DEVNO, ul_debug("directory %s", current->name));
195 		blkid__scan_dir(current->name, devno, &new_list, &devname);
196 		free(current->name);
197 		free(current);
198 		if (devname)
199 			break;
200 		/*
201 		 * If we're done checking at this level, descend to
202 		 * the next level of subdirectories. (breadth-first)
203 		 */
204 		if (list == NULL) {
205 			list = new_list;
206 			new_list = NULL;
207 		}
208 	}
209 	free_dirlist(&list);
210 	free_dirlist(&new_list);
211 
212 	return devname;
213 }
214 
215 /**
216  * blkid_devno_to_devname:
217  * @devno: device number
218  *
219  * This function finds the pathname to a block device with a given
220  * device number.
221  *
222  * Returns: a pointer to allocated memory to the pathname on success,
223  * and NULL on failure.
224  */
blkid_devno_to_devname(dev_t devno)225 char *blkid_devno_to_devname(dev_t devno)
226 {
227 	char *path;
228 	char buf[PATH_MAX];
229 
230 	path = sysfs_devno_to_devpath(devno, buf, sizeof(buf));
231 	if (path)
232 		path = strdup(path);
233 	if (!path)
234 		path = scandev_devno_to_devpath(devno);
235 
236 	if (!path) {
237 		DBG(DEVNO, ul_debug("blkid: couldn't find devno 0x%04lx",
238 			   (unsigned long) devno));
239 	} else {
240 		DBG(DEVNO, ul_debug("found devno 0x%04llx as %s", (long long)devno, path));
241 	}
242 
243 	return path;
244 }
245 
246 
247 /**
248  * blkid_devno_to_wholedisk:
249  * @dev: device number
250  * @diskname: buffer to return diskname (or NULL)
251  * @len: diskname buffer size (or 0)
252  * @diskdevno: pointer to returns devno of entire disk (or NULL)
253  *
254  * This function uses sysfs to convert the @devno device number to the *name*
255  * of the whole disk. The function DOES NOT return full device name. The @dev
256  * argument could be partition or whole disk -- both is converted.
257  *
258  * For example: sda1, 0x0801 --> sda, 0x0800
259  *
260  * For conversion to the full disk *path* use blkid_devno_to_devname(), for
261  * example:
262  *
263  * <informalexample>
264  *  <programlisting>
265  *
266  *	dev_t dev = 0x0801, disk;		// sda1 = 8:1
267  *	char *diskpath, diskname[32];
268  *
269  *	blkid_devno_to_wholedisk(dev, diskname, sizeof(diskname), &disk);
270  *	diskpath = blkid_devno_to_devname(disk);
271  *
272  *	// print "0x0801: sda, /dev/sda, 8:0
273  *	printf("0x%x: %s, %s, %d:%d\n",
274  *		dev, diskname, diskpath, major(disk), minor(disk));
275  *
276  *	free(diskpath);
277  *
278  *  </programlisting>
279  * </informalexample>
280  *
281  * Returns: 0 on success or -1 in case of error.
282  */
blkid_devno_to_wholedisk(dev_t dev,char * diskname,size_t len,dev_t * diskdevno)283 int blkid_devno_to_wholedisk(dev_t dev, char *diskname,
284 			size_t len, dev_t *diskdevno)
285 {
286 	return sysfs_devno_to_wholedisk( dev, diskname, len, diskdevno);
287 }
288 
289 /*
290  * Returns 1 if the @major number is associated with @drvname.
291  */
blkid_driver_has_major(const char * drvname,int drvmaj)292 int blkid_driver_has_major(const char *drvname, int drvmaj)
293 {
294 	FILE *f;
295 	char buf[128];
296 	int match = 0;
297 
298 	f = fopen(_PATH_PROC_DEVICES, "r" UL_CLOEXECSTR);
299 	if (!f)
300 		return 0;
301 
302 	while (fgets(buf, sizeof(buf), f)) {	/* skip to block dev section */
303 		if (strncmp("Block devices:\n", buf, sizeof(buf)) == 0)
304 			break;
305 	}
306 
307 	while (fgets(buf, sizeof(buf), f)) {
308 		int maj;
309 		char name[64 + 1];
310 
311 		if (sscanf(buf, "%d %64[^\n ]", &maj, name) != 2)
312 			continue;
313 
314 		if (maj == drvmaj && strcmp(name, drvname) == 0) {
315 			match = 1;
316 			break;
317 		}
318 	}
319 
320 	fclose(f);
321 
322 	DBG(DEVNO, ul_debug("major %d %s associated with '%s' driver",
323 			drvmaj, match ? "is" : "is NOT", drvname));
324 	return match;
325 }
326 
327 #ifdef TEST_PROGRAM
main(int argc,char ** argv)328 int main(int argc, char** argv)
329 {
330 	char	*devname, *tmp;
331 	char	diskname[PATH_MAX];
332 	int	devmaj, devmin;
333 	dev_t	devno, disk_devno;
334 	const char *errmsg = "Couldn't parse %s: %s\n";
335 
336 	blkid_init_debug(BLKID_DEBUG_ALL);
337 	if ((argc != 2) && (argc != 3)) {
338 		fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
339 			"Resolve a device number to a device name\n",
340 			argv[0], argv[0]);
341 		exit(1);
342 	}
343 	if (argc == 2) {
344 		devno = strtoul(argv[1], &tmp, 0);
345 		if (*tmp) {
346 			fprintf(stderr, errmsg, "device number", argv[1]);
347 			exit(1);
348 		}
349 	} else {
350 		devmaj = strtoul(argv[1], &tmp, 0);
351 		if (*tmp) {
352 			fprintf(stderr, errmsg, "major number", argv[1]);
353 			exit(1);
354 		}
355 		devmin = strtoul(argv[2], &tmp, 0);
356 		if (*tmp) {
357 			fprintf(stderr, errmsg, "minor number", argv[2]);
358 			exit(1);
359 		}
360 		devno = makedev(devmaj, devmin);
361 	}
362 	printf("Looking for device 0x%04llx\n", (long long)devno);
363 	devname = blkid_devno_to_devname(devno);
364 	free(devname);
365 
366 	printf("Looking for whole-device for 0x%04llx\n", (long long)devno);
367 	if (blkid_devno_to_wholedisk(devno, diskname,
368 				sizeof(diskname), &disk_devno) == 0)
369 		printf("found devno 0x%04llx as /dev/%s\n", (long long) disk_devno, diskname);
370 
371 	return 0;
372 }
373 #endif
374