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.
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
29 #ifdef HAVE_SYS_MKDEV_H
30 #include <sys/mkdev.h>
37 struct dir_list
*next
;
40 char *blkid_strndup(const char *s
, int length
)
50 ret
= xmalloc(length
+ 1);
51 strncpy(ret
, s
, length
);
56 char *blkid_strdup(const char *s
)
58 return blkid_strndup(s
, 0);
62 * This function adds an entry to the directory list
64 static void add_to_dirlist(const char *name
, struct dir_list
**list
)
68 dp
= xmalloc(sizeof(struct dir_list
));
69 dp
->name
= blkid_strdup(name
);
75 * This function frees a directory list
77 static void free_dirlist(struct dir_list
**list
)
79 struct dir_list
*dp
, *next
;
81 for (dp
= *list
; dp
; dp
= next
) {
89 static void scan_dir(char *dir_name
, dev_t devno
, struct dir_list
**list
,
98 if ((dir
= opendir(dir_name
)) == NULL
)
100 dirlen
= strlen(dir_name
) + 2;
101 while ((dp
= readdir(dir
)) != 0) {
102 if (dirlen
+ strlen(dp
->d_name
) >= sizeof(path
))
105 if (dp
->d_name
[0] == '.' &&
106 ((dp
->d_name
[1] == 0) ||
107 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
110 sprintf(path
, "%s/%s", dir_name
, dp
->d_name
);
111 if (stat(path
, &st
) < 0)
114 if (S_ISDIR(st
.st_mode
))
115 add_to_dirlist(path
, list
);
116 else if (S_ISBLK(st
.st_mode
) && st
.st_rdev
== devno
) {
117 *devname
= blkid_strdup(path
);
119 printf("found 0x%llx at %s (%p)\n", devno
,
127 /* Directories where we will try to search for device numbers */
128 const char *blkid_devdirs
[] = { "/devices", "/devfs", "/dev", NULL
};
131 * This function finds the pathname to a block device with a given
132 * device number. It returns a pointer to allocated memory to the
133 * pathname on success, and NULL on failure.
135 char *blkid_devno_to_devname(dev_t devno
)
137 struct dir_list
*list
= NULL
, *new_list
= NULL
;
138 char *devname
= NULL
;
142 * Add the starting directories to search in reverse order of
143 * importance, since we are using a stack...
145 for (dir
= blkid_devdirs
; *dir
; dir
++)
146 add_to_dirlist(*dir
, &list
);
149 struct dir_list
*current
= list
;
152 DBG(DEBUG_DEVNO
, printf("directory %s\n", current
->name
));
153 scan_dir(current
->name
, devno
, &new_list
, &devname
);
159 * If we're done checking at this level, descend to
160 * the next level of subdirectories. (breadth-first)
168 free_dirlist(&new_list
);
172 printf("blkid: cannot find devno 0x%04lx\n",
173 (unsigned long) devno
));
176 printf("found devno 0x%04llx as %s\n", devno
, devname
));
184 int main(int argc
, char** argv
)
189 const char *errmsg
= "Cannot parse %s: %s\n";
191 blkid_debug_mask
= DEBUG_ALL
;
192 if ((argc
!= 2) && (argc
!= 3)) {
193 fprintf(stderr
, "Usage:\t%s device_number\n\t%s major minor\n"
194 "Resolve a device number to a device name\n",
199 devno
= strtoul(argv
[1], &tmp
, 0);
201 fprintf(stderr
, errmsg
, "device number", argv
[1]);
205 major
= strtoul(argv
[1], &tmp
, 0);
207 fprintf(stderr
, errmsg
, "major number", argv
[1]);
210 minor
= strtoul(argv
[2], &tmp
, 0);
212 fprintf(stderr
, errmsg
, "minor number", argv
[2]);
215 devno
= makedev(major
, minor
);
217 printf("Looking for device 0x%04Lx\n", devno
);
218 devname
= blkid_devno_to_devname(devno
);