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 = xmalloc(length + 1);
52 strncpy(ret, s, length);
57 char *blkid_strdup(const char *s)
59 return blkid_strndup(s, 0);
63 * This function adds an entry to the directory list
65 static void add_to_dirlist(const char *name, struct dir_list **list)
69 dp = xmalloc(sizeof(struct dir_list));
70 dp->name = blkid_strdup(name);
76 * This function frees a directory list
78 static void free_dirlist(struct dir_list **list)
80 struct dir_list *dp, *next;
82 for (dp = *list; dp; dp = next) {
90 static void scan_dir(char *dir_name, dev_t devno, struct dir_list **list,
99 if ((dir = opendir(dir_name)) == NULL)
101 dirlen = strlen(dir_name) + 2;
102 while ((dp = readdir(dir)) != 0) {
103 if (dirlen + strlen(dp->d_name) >= sizeof(path))
106 if (dp->d_name[0] == '.' &&
107 ((dp->d_name[1] == 0) ||
108 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
111 sprintf(path, "%s/%s", dir_name, dp->d_name);
112 if (stat(path, &st) < 0)
115 if (S_ISDIR(st.st_mode))
116 add_to_dirlist(path, list);
117 else if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
118 *devname = blkid_strdup(path);
120 printf("found 0x%Lx at %s (%p)\n", devno,
129 /* Directories where we will try to search for device numbers */
130 const char *blkid_devdirs[] = { "/devices", "/devfs", "/dev", NULL };
133 * This function finds the pathname to a block device with a given
134 * device number. It returns a pointer to allocated memory to the
135 * pathname on success, and NULL on failure.
137 char *blkid_devno_to_devname(dev_t devno)
139 struct dir_list *list = NULL, *new_list = NULL;
140 char *devname = NULL;
144 * Add the starting directories to search in reverse order of
145 * importance, since we are using a stack...
147 for (dir = blkid_devdirs; *dir; dir++)
148 add_to_dirlist(*dir, &list);
151 struct dir_list *current = list;
154 DBG(DEBUG_DEVNO, printf("directory %s\n", current->name));
155 scan_dir(current->name, devno, &new_list, &devname);
161 * If we're done checking at this level, descend to
162 * the next level of subdirectories. (breadth-first)
170 free_dirlist(&new_list);
174 printf("blkid: couldn't find devno 0x%04lx\n",
175 (unsigned long) devno));
178 printf("found devno 0x%04Lx as %s\n", devno, devname));
186 int main(int argc, char** argv)
191 const char *errmsg = "Couldn't parse %s: %s\n";
193 blkid_debug_mask = DEBUG_ALL;
194 if ((argc != 2) && (argc != 3)) {
195 fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n"
196 "Resolve a device number to a device name\n",
201 devno = strtoul(argv[1], &tmp, 0);
203 fprintf(stderr, errmsg, "device number", argv[1]);
207 major = strtoul(argv[1], &tmp, 0);
209 fprintf(stderr, errmsg, "major number", argv[1]);
212 minor = strtoul(argv[2], &tmp, 0);
214 fprintf(stderr, errmsg, "minor number", argv[2]);
217 devno = makedev(major, minor);
219 printf("Looking for device 0x%04Lx\n", devno);
220 devname = blkid_devno_to_devname(devno);