2 * devno.c - find a particular device by its device number (major/minor)
4 * Copyright (C) 2000, 2001, 2003 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>
38 struct dir_list *next;
41 char *blkid_strndup(const char *s, int length)
51 ret = malloc(length + 1);
53 strncpy(ret, s, length);
59 char *blkid_strdup(const char *s)
61 return blkid_strndup(s, 0);
65 * This function adds an entry to the directory list
67 static void add_to_dirlist(const char *name, struct dir_list **list)
71 dp = malloc(sizeof(struct dir_list));
74 dp->name = blkid_strdup(name);
84 * This function frees a directory list
86 static void free_dirlist(struct dir_list **list)
88 struct dir_list *dp, *next;
90 for (dp = *list; dp; dp = next) {
98 static void scan_dir(char *dirname, dev_t devno, struct dir_list **list,
107 if ((dir = opendir(dirname)) == NULL)
109 dirlen = strlen(dirname) + 2;
110 while ((dp = readdir(dir)) != 0) {
111 if (dirlen + strlen(dp->d_name) >= sizeof(path))
114 if (dp->d_name[0] == '.' &&
115 ((dp->d_name[1] == 0) ||
116 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
119 sprintf(path, "%s/%s", dirname, dp->d_name);
120 if (stat(path, &st) < 0)
123 if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
124 *devname = blkid_strdup(path);
126 printf("found 0x%llx at %s (%p)\n", (long long)devno,
130 if (S_ISDIR(st.st_mode) && !lstat(path, &st) &&
132 add_to_dirlist(path, list);
138 /* Directories where we will try to search for device numbers */
139 const char *blkid_devdirs[] = { "/devices", "/devfs", "/dev", NULL };
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.
146 char *blkid_devno_to_devname(dev_t devno)
148 struct dir_list *list = NULL, *new_list = NULL;
149 char *devname = NULL;
153 * Add the starting directories to search in reverse order of
154 * importance, since we are using a stack...
156 for (dir = blkid_devdirs; *dir; dir++)
157 add_to_dirlist(*dir, &list);
160 struct dir_list *current = list;
163 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
164 scan_dir(current->name, devno, &new_list, &devname);
170 * If we're done checking at this level, descend to
171 * the next level of subdirectories. (breadth-first)
179 free_dirlist(&new_list);
183 printf("blkid: couldn't find devno 0x%04lx\n",
184 (unsigned long) devno));
187 printf("found devno 0x%04llx as %s\n", (long long)devno, devname));
195 int main(int argc, char** argv)
200 const char *errmsg = "Couldn't parse %s: %s\n";
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",
210 devno = strtoul(argv[1], &tmp, 0);
212 fprintf(stderr, errmsg, "device number", argv[1]);
216 major = strtoul(argv[1], &tmp, 0);
218 fprintf(stderr, errmsg, "major number", argv[1]);
221 minor = strtoul(argv[2], &tmp, 0);
223 fprintf(stderr, errmsg, "minor number", argv[2]);
226 devno = makedev(major, minor);
228 printf("Looking for device 0x%04llx\n", (long long)devno);
229 devname = blkid_devno_to_devname(devno);