2 * devno.c - find a particular device by its device number (major/minor)
4 * Copyright (C) 2000, 2001 Theodore Ts'o
5 * Copyright (C) 2001 Andreas Dilger
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
21 #include <sys/types.h>
31 #include <sys/mkdev.h>
34 #include "blkid/blkid.h"
37 #define DEB_DEVNO(fmt, arg...) printf("devno: " fmt, ## arg)
39 #define DEB_DEVNO(fmt, arg...) do {} while (0)
44 struct dir_list *next;
47 char *stringn_copy(const char *s, const int length)
54 ret = malloc(length + 1);
56 strncpy(ret, s, length);
62 char *string_copy(const char *s)
67 return stringn_copy(s, strlen(s));
70 void string_free(char *s)
77 * This function adds an entry to the directory list
79 static void add_to_dirlist(const char *name, struct dir_list **list)
83 dp = malloc(sizeof(struct dir_list));
86 dp->name = string_copy(name);
96 * This function frees a directory list
98 static void free_dirlist(struct dir_list **list)
100 struct dir_list *dp, *next;
102 for (dp = *list; dp; dp = next) {
104 string_free(dp->name);
110 static int scan_dir(char *dirname, dev_t devno, struct dir_list **list,
120 dirlen = strlen(dirname);
121 if ((dir = opendir(dirname)) == NULL)
125 if (dirlen + strlen(dp->d_name) + 2 >= sizeof(path))
128 if (dp->d_name[0] == '.' &&
129 ((dp->d_name[1] == 0) ||
130 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
133 sprintf(path, "%s/%s", dirname, dp->d_name);
134 if (stat(path, &st) < 0)
137 if (S_ISDIR(st.st_mode))
138 add_to_dirlist(path, list);
139 else if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
140 *devname = string_copy(path);
141 DEB_DEVNO("found 0x%Lx at %s (%p)\n", devno,
144 ret = -BLKID_ERR_MEM;
154 /* Directories where we will try to search for device numbers */
155 const char *devdirs[] = { "/dev", "/devfs", "/devices", NULL };
158 * This function finds the pathname to a block device with a given
159 * device number. It returns a pointer to allocated memory to the
160 * pathname on success, and NULL on failure.
162 char *blkid_devno_to_devname(dev_t devno)
164 struct dir_list *list = NULL, *new_list = NULL;
165 char *devname = NULL;
169 * Add the starting directories to search in reverse order of
170 * importance, since we are using a stack...
172 for (dir = devdirs; *dir; dir++)
173 /* go to end of list */;
177 add_to_dirlist(*dir, &list);
178 } while (dir != devdirs);
181 struct dir_list *current = list;
184 DEB_DEVNO("directory %s\n", current->name);
185 scan_dir(current->name, devno, &new_list, &devname);
186 string_free(current->name);
191 * If we're done checking at this level, descend to
192 * the next level of subdirectories. (breadth-first)
200 free_dirlist(&new_list);
203 fprintf(stderr, "blkid: couldn't find devno 0x%04Lx\n", devno);
205 DEB_DEVNO("found devno 0x%04Lx as %s\n", devno, devname);
210 blkid_dev *blkid_find_devno(blkid_cache *cache, dev_t devno)
212 blkid_dev *dev = NULL;
213 struct list_head *p, *n;
218 /* This cannot be a standard list_for_each() because we may be
219 * deleting the referenced struct in blkid_verify_devname() and
220 * pointing to another one that was probed from disk, and "p"
221 * would point to freed memory.
223 list_for_each_safe(p, n, &cache->bic_devs) {
224 blkid_dev *tmp = list_entry(p, blkid_dev, bid_devs);
225 if (tmp->bid_devno != devno)
228 tmp = blkid_verify_devname(cache, tmp);
229 if (!tmp || tmp->bid_devno != devno)
237 DEB_DEVNO("found devno 0x%04LX in cache as %s\n",
238 devno, dev->bid_name);
243 blkid_dev *blkid_get_devno(blkid_cache *cache, dev_t devno)
248 if (!(dev = blkid_find_devno(cache, devno)) &&
249 (devname = blkid_devno_to_devname(devno))) {
250 dev = blkid_get_devname(cache, devname);
251 string_free(devname);
258 int main(int argc, char** argv)
263 const char *errmsg = "Couldn't parse %s: %s\n";
265 if ((argc != 2) && (argc != 3)) {
266 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
267 "Resolve a device number to a device name\n",
272 devno = strtoul(argv[1], &tmp, 0);
274 fprintf(stderr, errmsg, "device number", argv[1]);
278 major = strtoul(argv[1], &tmp, 0);
280 fprintf(stderr, errmsg, "major number", argv[1]);
283 minor = strtoul(argv[2], &tmp, 0);
285 fprintf(stderr, errmsg, "minor number", argv[2]);
288 devno = makedev(major, minor);
290 printf("Looking for device 0x%04Lx\n", devno);
291 devname = blkid_devno_to_devname(devno);
293 string_free(devname);