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.
22 #include <sys/types.h>
32 #include <sys/mkdev.h>
37 char *blkid_strndup(const char *s, int length)
47 ret = malloc(length + 1);
49 strncpy(ret, s, length);
55 char *blkid_strdup(const char *s)
57 return blkid_strndup(s, 0);
61 * This function adds an entry to the directory list
63 static void add_to_dirlist(const char *name, struct dir_list **list)
67 dp = malloc(sizeof(struct dir_list));
70 dp->name = blkid_strdup(name);
80 * This function frees a directory list
82 static void free_dirlist(struct dir_list **list)
84 struct dir_list *dp, *next;
86 for (dp = *list; dp; dp = next) {
94 void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list,
103 if ((dir = opendir(dirname)) == NULL)
105 dirlen = strlen(dirname) + 2;
106 while ((dp = readdir(dir)) != 0) {
107 if (dirlen + strlen(dp->d_name) >= sizeof(path))
110 if (dp->d_name[0] == '.' &&
111 ((dp->d_name[1] == 0) ||
112 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
115 sprintf(path, "%s/%s", dirname, dp->d_name);
116 if (stat(path, &st) < 0)
119 if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
120 *devname = blkid_strdup(path);
122 printf("found 0x%llx at %s (%p)\n", (long long)devno,
126 if (list && S_ISDIR(st.st_mode) && !lstat(path, &st) &&
128 add_to_dirlist(path, list);
134 /* Directories where we will try to search for device numbers */
135 static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL };
138 * This function finds the pathname to a block device with a given
139 * device number. It returns a pointer to allocated memory to the
140 * pathname on success, and NULL on failure.
142 char *blkid_devno_to_devname(dev_t devno)
144 struct dir_list *list = NULL, *new_list = NULL;
145 char *devname = NULL;
149 * Add the starting directories to search in reverse order of
150 * importance, since we are using a stack...
152 for (dir = devdirs; *dir; dir++)
153 add_to_dirlist(*dir, &list);
156 struct dir_list *current = list;
159 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
160 blkid__scan_dir(current->name, devno, &new_list, &devname);
166 * If we're done checking at this level, descend to
167 * the next level of subdirectories. (breadth-first)
175 free_dirlist(&new_list);
179 printf("blkid: couldn't find devno 0x%04lx\n",
180 (unsigned long) devno));
183 printf("found devno 0x%04llx as %s\n", (long long)devno, devname));
191 int main(int argc, char** argv)
196 const char *errmsg = "Couldn't parse %s: %s\n";
198 blkid_debug_mask = DEBUG_ALL;
199 if ((argc != 2) && (argc != 3)) {
200 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
201 "Resolve a device number to a device name\n",
206 devno = strtoul(argv[1], &tmp, 0);
208 fprintf(stderr, errmsg, "device number", argv[1]);
212 major = strtoul(argv[1], &tmp, 0);
214 fprintf(stderr, errmsg, "major number", argv[1]);
217 minor = strtoul(argv[2], &tmp, 0);
219 fprintf(stderr, errmsg, "minor number", argv[2]);
222 devno = makedev(major, minor);
224 printf("Looking for device 0x%04llx\n", (long long)devno);
225 devname = blkid_devno_to_devname(devno);