1 /* vi: set sw=4 ts=4: */
3 * devno.c - find a particular device by its device number (major/minor)
5 * Copyright (C) 2000, 2001, 2003 Theodore Ts'o
6 * Copyright (C) 2001 Andreas Dilger
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
22 #include <sys/types.h>
32 #include <sys/mkdev.h>
39 struct dir_list *next;
42 char *blkid_strndup(const char *s, int length)
52 ret = xmalloc(length + 1);
53 strncpy(ret, s, length);
58 char *blkid_strdup(const char *s)
60 return blkid_strndup(s, 0);
64 * This function adds an entry to the directory list
66 static void add_to_dirlist(const char *name, struct dir_list **list)
70 dp = xmalloc(sizeof(struct dir_list));
71 dp->name = blkid_strdup(name);
77 * This function frees a directory list
79 static void free_dirlist(struct dir_list **list)
81 struct dir_list *dp, *next;
83 for (dp = *list; dp; dp = next) {
91 static void scan_dir(char *dir_name, dev_t devno, struct dir_list **list,
100 if ((dir = opendir(dir_name)) == NULL)
102 dirlen = strlen(dir_name) + 2;
103 while ((dp = readdir(dir)) != 0) {
104 if (dirlen + strlen(dp->d_name) >= sizeof(path))
107 if (dp->d_name[0] == '.' &&
108 ((dp->d_name[1] == 0) ||
109 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
112 sprintf(path, "%s/%s", dir_name, dp->d_name);
113 if (stat(path, &st) < 0)
116 if (S_ISDIR(st.st_mode))
117 add_to_dirlist(path, list);
118 else if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
119 *devname = blkid_strdup(path);
121 printf("found 0x%llx at %s (%p)\n", devno,
130 /* Directories where we will try to search for device numbers */
131 const char *blkid_devdirs[] = { "/devices", "/devfs", "/dev", NULL };
134 * This function finds the pathname to a block device with a given
135 * device number. It returns a pointer to allocated memory to the
136 * pathname on success, and NULL on failure.
138 char *blkid_devno_to_devname(dev_t devno)
140 struct dir_list *list = NULL, *new_list = NULL;
141 char *devname = NULL;
145 * Add the starting directories to search in reverse order of
146 * importance, since we are using a stack...
148 for (dir = blkid_devdirs; *dir; dir++)
149 add_to_dirlist(*dir, &list);
152 struct dir_list *current = list;
155 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
156 scan_dir(current->name, devno, &new_list, &devname);
162 * If we're done checking at this level, descend to
163 * the next level of subdirectories. (breadth-first)
171 free_dirlist(&new_list);
175 printf("blkid: couldn't find devno 0x%04lx\n",
176 (unsigned long) devno));
179 printf("found devno 0x%04llx as %s\n", devno, devname));
187 int main(int argc, char** argv)
192 const char *errmsg = "Cannot parse %s: %s\n";
194 blkid_debug_mask = DEBUG_ALL;
195 if ((argc != 2) && (argc != 3)) {
196 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
197 "Resolve a device number to a device name\n",
202 devno = strtoul(argv[1], &tmp, 0);
204 fprintf(stderr, errmsg, "device number", argv[1]);
208 major = strtoul(argv[1], &tmp, 0);
210 fprintf(stderr, errmsg, "major number", argv[1]);
213 minor = strtoul(argv[2], &tmp, 0);
215 fprintf(stderr, errmsg, "minor number", argv[2]);
218 devno = makedev(major, minor);
220 printf("Looking for device 0x%04Lx\n", devno);
221 devname = blkid_devno_to_devname(devno);