bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * devno.c - find a particular device by its device number (major/minor) |
| 3 | * |
| 4 | * Copyright (C) 2000, 2001, 2003 Theodore Ts'o |
| 5 | * Copyright (C) 2001 Andreas Dilger |
| 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the |
| 9 | * GNU Lesser General Public License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #ifdef HAVE_UNISTD_H |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | #include <stdlib.h> |
| 19 | #ifdef HAVE_SYS_TYPES_H |
| 20 | #include <sys/types.h> |
| 21 | #endif |
| 22 | #ifdef HAVE_SYS_STAT_H |
| 23 | #include <sys/stat.h> |
| 24 | #endif |
| 25 | #include <dirent.h> |
| 26 | #ifdef HAVE_ERRNO_H |
| 27 | #include <errno.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_SYS_MKDEV_H |
| 30 | #include <sys/mkdev.h> |
| 31 | #endif |
| 32 | #include <fcntl.h> |
| 33 | #include <inttypes.h> |
| 34 | |
| 35 | #include "blkidP.h" |
| 36 | #include "pathnames.h" |
| 37 | #include "at.h" |
| 38 | #include "sysfs.h" |
| 39 | |
| 40 | static char *blkid_strconcat(const char *a, const char *b, const char *c) |
| 41 | { |
| 42 | char *res, *p; |
| 43 | size_t len, al, bl, cl; |
| 44 | |
| 45 | al = a ? strlen(a) : 0; |
| 46 | bl = b ? strlen(b) : 0; |
| 47 | cl = c ? strlen(c) : 0; |
| 48 | |
| 49 | len = al + bl + cl; |
| 50 | if (!len) |
| 51 | return NULL; |
| 52 | p = res = malloc(len + 1); |
| 53 | if (!res) |
| 54 | return NULL; |
| 55 | if (al) { |
| 56 | memcpy(p, a, al); |
| 57 | p += al; |
| 58 | } |
| 59 | if (bl) { |
| 60 | memcpy(p, b, bl); |
| 61 | p += bl; |
| 62 | } |
| 63 | if (cl) { |
| 64 | memcpy(p, c, cl); |
| 65 | p += cl; |
| 66 | } |
| 67 | *p = '\0'; |
| 68 | return res; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * This function adds an entry to the directory list |
| 73 | */ |
| 74 | static void add_to_dirlist(const char *dir, const char *subdir, |
| 75 | struct dir_list **list) |
| 76 | { |
| 77 | struct dir_list *dp; |
| 78 | |
| 79 | dp = malloc(sizeof(struct dir_list)); |
| 80 | if (!dp) |
| 81 | return; |
| 82 | dp->name = subdir ? blkid_strconcat(dir, "/", subdir) : |
| 83 | dir ? strdup(dir) : NULL; |
| 84 | |
| 85 | if (!dp->name) { |
| 86 | free(dp); |
| 87 | return; |
| 88 | } |
| 89 | dp->next = *list; |
| 90 | *list = dp; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * This function frees a directory list |
| 95 | */ |
| 96 | static void free_dirlist(struct dir_list **list) |
| 97 | { |
| 98 | struct dir_list *dp, *next; |
| 99 | |
| 100 | for (dp = *list; dp; dp = next) { |
| 101 | next = dp->next; |
| 102 | free(dp->name); |
| 103 | free(dp); |
| 104 | } |
| 105 | *list = NULL; |
| 106 | } |
| 107 | |
| 108 | void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list, |
| 109 | char **devname) |
| 110 | { |
| 111 | DIR *dir; |
| 112 | struct dirent *dp; |
| 113 | struct stat st; |
| 114 | |
| 115 | if ((dir = opendir(dirname)) == NULL) |
| 116 | return; |
| 117 | |
| 118 | while ((dp = readdir(dir)) != NULL) { |
| 119 | #ifdef _DIRENT_HAVE_D_TYPE |
| 120 | if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_BLK && |
| 121 | dp->d_type != DT_LNK && dp->d_type != DT_DIR) |
| 122 | continue; |
| 123 | #endif |
| 124 | if (dp->d_name[0] == '.' && |
| 125 | ((dp->d_name[1] == 0) || |
| 126 | ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) |
| 127 | continue; |
| 128 | |
| 129 | if (fstat_at(dirfd(dir), dirname, dp->d_name, &st, 0)) |
| 130 | continue; |
| 131 | |
| 132 | if (S_ISBLK(st.st_mode) && st.st_rdev == devno) { |
| 133 | *devname = blkid_strconcat(dirname, "/", dp->d_name); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 134 | DBG(DEVNO, ul_debug("found 0x%llx at %s", (long long)devno, |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 135 | *devname)); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | if (!list || !S_ISDIR(st.st_mode)) |
| 140 | continue; |
| 141 | |
| 142 | /* add subdirectory (but not symlink) to the list */ |
| 143 | #ifdef _DIRENT_HAVE_D_TYPE |
| 144 | if (dp->d_type == DT_LNK) |
| 145 | continue; |
| 146 | if (dp->d_type == DT_UNKNOWN) |
| 147 | #endif |
| 148 | { |
| 149 | if (fstat_at(dirfd(dir), dirname, dp->d_name, &st, 1) || |
| 150 | !S_ISDIR(st.st_mode)) |
| 151 | continue; /* symlink or lstat() failed */ |
| 152 | } |
| 153 | |
| 154 | if (*dp->d_name == '.' || ( |
| 155 | #ifdef _DIRENT_HAVE_D_TYPE |
| 156 | dp->d_type == DT_DIR && |
| 157 | #endif |
| 158 | strcmp(dp->d_name, "shm") == 0)) |
| 159 | /* ignore /dev/.{udev,mount,mdadm} and /dev/shm */ |
| 160 | continue; |
| 161 | |
| 162 | add_to_dirlist(dirname, dp->d_name, list); |
| 163 | } |
| 164 | closedir(dir); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | /* Directories where we will try to search for device numbers */ |
| 169 | static const char *devdirs[] = { "/devices", "/devfs", "/dev", NULL }; |
| 170 | |
| 171 | /** |
| 172 | * SECTION: misc |
| 173 | * @title: Miscellaneous utils |
| 174 | * @short_description: mix of various utils for low-level and high-level API |
| 175 | */ |
| 176 | |
| 177 | |
| 178 | |
| 179 | static char *scandev_devno_to_devpath(dev_t devno) |
| 180 | { |
| 181 | struct dir_list *list = NULL, *new_list = NULL; |
| 182 | char *devname = NULL; |
| 183 | const char **dir; |
| 184 | |
| 185 | /* |
| 186 | * Add the starting directories to search in reverse order of |
| 187 | * importance, since we are using a stack... |
| 188 | */ |
| 189 | for (dir = devdirs; *dir; dir++) |
| 190 | add_to_dirlist(*dir, NULL, &list); |
| 191 | |
| 192 | while (list) { |
| 193 | struct dir_list *current = list; |
| 194 | |
| 195 | list = list->next; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 196 | DBG(DEVNO, ul_debug("directory %s", current->name)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 197 | blkid__scan_dir(current->name, devno, &new_list, &devname); |
| 198 | free(current->name); |
| 199 | free(current); |
| 200 | if (devname) |
| 201 | break; |
| 202 | /* |
| 203 | * If we're done checking at this level, descend to |
| 204 | * the next level of subdirectories. (breadth-first) |
| 205 | */ |
| 206 | if (list == NULL) { |
| 207 | list = new_list; |
| 208 | new_list = NULL; |
| 209 | } |
| 210 | } |
| 211 | free_dirlist(&list); |
| 212 | free_dirlist(&new_list); |
| 213 | |
| 214 | return devname; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * blkid_devno_to_devname: |
| 219 | * @devno: device number |
| 220 | * |
| 221 | * This function finds the pathname to a block device with a given |
| 222 | * device number. |
| 223 | * |
| 224 | * Returns: a pointer to allocated memory to the pathname on success, |
| 225 | * and NULL on failure. |
| 226 | */ |
| 227 | char *blkid_devno_to_devname(dev_t devno) |
| 228 | { |
| 229 | char *path = NULL; |
| 230 | char buf[PATH_MAX]; |
| 231 | |
| 232 | path = sysfs_devno_to_devpath(devno, buf, sizeof(buf)); |
| 233 | if (path) |
| 234 | path = strdup(path); |
| 235 | if (!path) |
| 236 | path = scandev_devno_to_devpath(devno); |
| 237 | |
| 238 | if (!path) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 239 | DBG(DEVNO, ul_debug("blkid: couldn't find devno 0x%04lx", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 240 | (unsigned long) devno)); |
| 241 | } else { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 242 | DBG(DEVNO, ul_debug("found devno 0x%04llx as %s", (long long)devno, path)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return path; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /** |
| 250 | * blkid_devno_to_wholedisk: |
| 251 | * @dev: device number |
| 252 | * @diskname: buffer to return diskname (or NULL) |
| 253 | * @len: diskname buffer size (or 0) |
| 254 | * @diskdevno: pointer to returns devno of entire disk (or NULL) |
| 255 | * |
| 256 | * This function uses sysfs to convert the @devno device number to the *name* |
| 257 | * of the whole disk. The function DOES NOT return full device name. The @dev |
| 258 | * argument could be partition or whole disk -- both is converted. |
| 259 | * |
| 260 | * For example: sda1, 0x0801 --> sda, 0x0800 |
| 261 | * |
| 262 | * For conversion to the full disk *path* use blkid_devno_to_devname(), for |
| 263 | * example: |
| 264 | * |
| 265 | * <informalexample> |
| 266 | * <programlisting> |
| 267 | * |
| 268 | * dev_t dev = 0x0801, disk; // sda1 = 8:1 |
| 269 | * char *diskpath, diskname[32]; |
| 270 | * |
| 271 | * blkid_devno_to_wholedisk(dev, diskname, sizeof(diskname), &disk); |
| 272 | * diskpath = blkid_devno_to_devname(disk); |
| 273 | * |
| 274 | * // print "0x0801: sda, /dev/sda, 8:0 |
| 275 | * printf("0x%x: %s, %s, %d:%d\n", |
| 276 | * dev, diskname, diskpath, major(disk), minor(disk)); |
| 277 | * |
| 278 | * free(diskpath); |
| 279 | * |
| 280 | * </programlisting> |
| 281 | * </informalexample> |
| 282 | * |
| 283 | * Returns: 0 on success or -1 in case of error. |
| 284 | */ |
| 285 | int blkid_devno_to_wholedisk(dev_t dev, char *diskname, |
| 286 | size_t len, dev_t *diskdevno) |
| 287 | { |
| 288 | return sysfs_devno_to_wholedisk( dev, diskname, len, diskdevno); |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Returns 1 if the @major number is associated with @drvname. |
| 293 | */ |
| 294 | int blkid_driver_has_major(const char *drvname, int major) |
| 295 | { |
| 296 | FILE *f; |
| 297 | char buf[128]; |
| 298 | int match = 0; |
| 299 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 300 | f = fopen(_PATH_PROC_DEVICES, "r" UL_CLOEXECSTR); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 301 | if (!f) |
| 302 | return 0; |
| 303 | |
| 304 | while (fgets(buf, sizeof(buf), f)) { /* skip to block dev section */ |
| 305 | if (strncmp("Block devices:\n", buf, sizeof(buf)) == 0) |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | while (fgets(buf, sizeof(buf), f)) { |
| 310 | int maj; |
| 311 | char name[64 + 1]; |
| 312 | |
| 313 | if (sscanf(buf, "%d %64[^\n ]", &maj, name) != 2) |
| 314 | continue; |
| 315 | |
| 316 | if (maj == major && strcmp(name, drvname) == 0) { |
| 317 | match = 1; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | fclose(f); |
| 323 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 324 | DBG(DEVNO, ul_debug("major %d %s associated with '%s' driver", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 325 | major, match ? "is" : "is NOT", drvname)); |
| 326 | return match; |
| 327 | } |
| 328 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 329 | #ifdef TEST_PROGRAM |
| 330 | int main(int argc, char** argv) |
| 331 | { |
| 332 | char *devname, *tmp; |
| 333 | char diskname[PATH_MAX]; |
| 334 | int major, minor; |
| 335 | dev_t devno, disk_devno; |
| 336 | const char *errmsg = "Couldn't parse %s: %s\n"; |
| 337 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 338 | blkid_init_debug(BLKID_DEBUG_ALL); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 339 | if ((argc != 2) && (argc != 3)) { |
| 340 | fprintf(stderr, "Usage:\t%s device_number\n\t%s major minor\n" |
| 341 | "Resolve a device number to a device name\n", |
| 342 | argv[0], argv[0]); |
| 343 | exit(1); |
| 344 | } |
| 345 | if (argc == 2) { |
| 346 | devno = strtoul(argv[1], &tmp, 0); |
| 347 | if (*tmp) { |
| 348 | fprintf(stderr, errmsg, "device number", argv[1]); |
| 349 | exit(1); |
| 350 | } |
| 351 | } else { |
| 352 | major = strtoul(argv[1], &tmp, 0); |
| 353 | if (*tmp) { |
| 354 | fprintf(stderr, errmsg, "major number", argv[1]); |
| 355 | exit(1); |
| 356 | } |
| 357 | minor = strtoul(argv[2], &tmp, 0); |
| 358 | if (*tmp) { |
| 359 | fprintf(stderr, errmsg, "minor number", argv[2]); |
| 360 | exit(1); |
| 361 | } |
| 362 | devno = makedev(major, minor); |
| 363 | } |
| 364 | printf("Looking for device 0x%04llx\n", (long long)devno); |
| 365 | devname = blkid_devno_to_devname(devno); |
| 366 | free(devname); |
| 367 | |
| 368 | printf("Looking for whole-device for 0x%04llx\n", (long long)devno); |
| 369 | if (blkid_devno_to_wholedisk(devno, diskname, |
| 370 | sizeof(diskname), &disk_devno) == 0) |
| 371 | printf("found devno 0x%04llx as /dev/%s\n", (long long) disk_devno, diskname); |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | #endif |