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>
36 char *blkid_strndup(const char *s, int length)
46 ret = malloc(length + 1);
48 strncpy(ret, s, length);
54 char *blkid_strdup(const char *s)
56 return blkid_strndup(s, 0);
60 * This function adds an entry to the directory list
62 static void add_to_dirlist(const char *name, struct dir_list **list)
66 dp = malloc(sizeof(struct dir_list));
69 dp->name = blkid_strdup(name);
79 * This function frees a directory list
81 static void free_dirlist(struct dir_list **list)
83 struct dir_list *dp, *next;
85 for (dp = *list; dp; dp = next) {
93 void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list,
102 if ((dir = opendir(dirname)) == NULL)
104 dirlen = strlen(dirname) + 2;
105 while ((dp = readdir(dir)) != 0) {
106 if (dirlen + strlen(dp->d_name) >= sizeof(path))
109 if (dp->d_name[0] == '.' &&
110 ((dp->d_name[1] == 0) ||
111 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
114 sprintf(path, "%s/%s", dirname, dp->d_name);
115 if (stat(path, &st) < 0)
118 if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
119 *devname = blkid_strdup(path);
121 printf("found 0x%llx at %s (%p)\n", (long long)devno,
125 if (list && S_ISDIR(st.st_mode) && !lstat(path, &st) &&
127 add_to_dirlist(path, list);
133 /* Directories where we will try to search for device numbers */
134 static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
137 * This function finds the pathname to a block device with a given
138 * device number. It returns a pointer to allocated memory to the
139 * pathname on success, and NULL on failure.
141 char *blkid_devno_to_devname(dev_t devno)
143 struct dir_list *list = NULL, *new_list = NULL;
144 char *devname = NULL;
148 * Add the starting directories to search in reverse order of
149 * importance, since we are using a stack...
151 for (dir = devdirs; *dir; dir++)
152 add_to_dirlist(*dir, &list);
155 struct dir_list *current = list;
158 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
159 blkid__scan_dir(current->name, devno, &new_list, &devname);
165 * If we're done checking at this level, descend to
166 * the next level of subdirectories. (breadth-first)
174 free_dirlist(&new_list);
178 printf("blkid: couldn't find devno 0x%04lx\n",
179 (unsigned long) devno));
182 printf("found devno 0x%04llx as %s\n", (long long)devno, devname));
190 int main(int argc, char** argv)
195 const char *errmsg = "Couldn't parse %s: %s\n";
197 blkid_init_debug(DEBUG_ALL);
198 if ((argc != 2) && (argc != 3)) {
199 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
200 "Resolve a device number to a device name\n",
205 devno = strtoul(argv[1], &tmp, 0);
207 fprintf(stderr, errmsg, "device number", argv[1]);
211 major = strtoul(argv[1], &tmp, 0);
213 fprintf(stderr, errmsg, "major number", argv[1]);
216 minor = strtoul(argv[2], &tmp, 0);
218 fprintf(stderr, errmsg, "minor number", argv[2]);
221 devno = makedev(major, minor);
223 printf("Looking for device 0x%04llx\n", (long long)devno);
224 devname = blkid_devno_to_devname(devno);