bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * devname.c - get a dev by its device inode name |
| 3 | * |
| 4 | * Copyright (C) Andries Brouwer |
| 5 | * Copyright (C) 1999, 2000, 2001, 2002, 2003 Theodore Ts'o |
| 6 | * Copyright (C) 2001 Andreas Dilger |
| 7 | * |
| 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the |
| 10 | * GNU Lesser General Public License. |
| 11 | * %End-Header% |
| 12 | */ |
| 13 | |
| 14 | #define _GNU_SOURCE 1 |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <limits.h> |
| 19 | #ifdef HAVE_UNISTD_H |
| 20 | #include <unistd.h> |
| 21 | #endif |
| 22 | #include <stdlib.h> |
| 23 | #include <ctype.h> |
| 24 | #include <fcntl.h> |
| 25 | #ifdef HAVE_SYS_TYPES_H |
| 26 | #include <sys/types.h> |
| 27 | #endif |
| 28 | #include <dirent.h> |
| 29 | #ifdef HAVE_SYS_STAT_H |
| 30 | #include <sys/stat.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_ERRNO_H |
| 33 | #include <errno.h> |
| 34 | #endif |
| 35 | #include <time.h> |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 36 | #include <sys/sysmacros.h> |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 37 | |
| 38 | #include "blkidP.h" |
| 39 | |
| 40 | #include "canonicalize.h" /* $(top_srcdir)/include */ |
| 41 | #include "pathnames.h" |
| 42 | #include "sysfs.h" |
| 43 | #include "at.h" |
| 44 | |
| 45 | /* |
| 46 | * Find a dev struct in the cache by device name, if available. |
| 47 | * |
| 48 | * If there is no entry with the specified device name, and the create |
| 49 | * flag is set, then create an empty device entry. |
| 50 | */ |
| 51 | blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags) |
| 52 | { |
| 53 | blkid_dev dev = NULL, tmp; |
| 54 | struct list_head *p, *pnext; |
| 55 | |
| 56 | if (!cache || !devname) |
| 57 | return NULL; |
| 58 | |
| 59 | list_for_each(p, &cache->bic_devs) { |
| 60 | tmp = list_entry(p, struct blkid_struct_dev, bid_devs); |
| 61 | if (strcmp(tmp->bid_name, devname)) |
| 62 | continue; |
| 63 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 64 | DBG(DEVNAME, ul_debug("found devname %s in cache", tmp->bid_name)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 65 | dev = tmp; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | if (!dev && (flags & BLKID_DEV_CREATE)) { |
| 70 | if (access(devname, F_OK) < 0) |
| 71 | return NULL; |
| 72 | dev = blkid_new_dev(); |
| 73 | if (!dev) |
| 74 | return NULL; |
| 75 | dev->bid_time = INT_MIN; |
| 76 | dev->bid_name = strdup(devname); |
| 77 | dev->bid_cache = cache; |
| 78 | list_add_tail(&dev->bid_devs, &cache->bic_devs); |
| 79 | cache->bic_flags |= BLKID_BIC_FL_CHANGED; |
| 80 | } |
| 81 | |
| 82 | if (flags & BLKID_DEV_VERIFY) { |
| 83 | dev = blkid_verify(cache, dev); |
| 84 | if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) |
| 85 | return dev; |
| 86 | /* |
| 87 | * If the device is verified, then search the blkid |
| 88 | * cache for any entries that match on the type, uuid, |
| 89 | * and label, and verify them; if a cache entry can |
| 90 | * not be verified, then it's stale and so we remove |
| 91 | * it. |
| 92 | */ |
| 93 | list_for_each_safe(p, pnext, &cache->bic_devs) { |
| 94 | blkid_dev dev2 = list_entry(p, struct blkid_struct_dev, bid_devs); |
| 95 | if (dev2->bid_flags & BLKID_BID_FL_VERIFIED) |
| 96 | continue; |
| 97 | if (!dev->bid_type || !dev2->bid_type || |
| 98 | strcmp(dev->bid_type, dev2->bid_type)) |
| 99 | continue; |
| 100 | if (dev->bid_label && dev2->bid_label && |
| 101 | strcmp(dev->bid_label, dev2->bid_label)) |
| 102 | continue; |
| 103 | if (dev->bid_uuid && dev2->bid_uuid && |
| 104 | strcmp(dev->bid_uuid, dev2->bid_uuid)) |
| 105 | continue; |
| 106 | if ((dev->bid_label && !dev2->bid_label) || |
| 107 | (!dev->bid_label && dev2->bid_label) || |
| 108 | (dev->bid_uuid && !dev2->bid_uuid) || |
| 109 | (!dev->bid_uuid && dev2->bid_uuid)) |
| 110 | continue; |
| 111 | dev2 = blkid_verify(cache, dev2); |
| 112 | if (dev2 && !(dev2->bid_flags & BLKID_BID_FL_VERIFIED)) |
| 113 | blkid_free_dev(dev2); |
| 114 | } |
| 115 | } |
| 116 | return dev; |
| 117 | } |
| 118 | |
| 119 | /* Directories where we will try to search for device names */ |
| 120 | static const char *dirlist[] = { "/dev", "/devfs", "/devices", NULL }; |
| 121 | |
| 122 | static int is_dm_leaf(const char *devname) |
| 123 | { |
| 124 | struct dirent *de, *d_de; |
| 125 | DIR *dir, *d_dir; |
| 126 | char path[256]; |
| 127 | int ret = 1; |
| 128 | |
| 129 | if ((dir = opendir("/sys/block")) == NULL) |
| 130 | return 0; |
| 131 | while ((de = readdir(dir)) != NULL) { |
| 132 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") || |
| 133 | !strcmp(de->d_name, devname) || |
| 134 | strncmp(de->d_name, "dm-", 3) || |
| 135 | strlen(de->d_name) > sizeof(path)-32) |
| 136 | continue; |
| 137 | sprintf(path, "/sys/block/%s/slaves", de->d_name); |
| 138 | if ((d_dir = opendir(path)) == NULL) |
| 139 | continue; |
| 140 | while ((d_de = readdir(d_dir)) != NULL) { |
| 141 | if (!strcmp(d_de->d_name, devname)) { |
| 142 | ret = 0; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | closedir(d_dir); |
| 147 | if (!ret) |
| 148 | break; |
| 149 | } |
| 150 | closedir(dir); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Probe a single block device to add to the device cache. |
| 156 | */ |
| 157 | static void probe_one(blkid_cache cache, const char *ptname, |
| 158 | dev_t devno, int pri, int only_if_new, int removable) |
| 159 | { |
| 160 | blkid_dev dev = NULL; |
| 161 | struct list_head *p, *pnext; |
| 162 | const char **dir; |
| 163 | char *devname = NULL; |
| 164 | |
| 165 | /* See if we already have this device number in the cache. */ |
| 166 | list_for_each_safe(p, pnext, &cache->bic_devs) { |
| 167 | blkid_dev tmp = list_entry(p, struct blkid_struct_dev, |
| 168 | bid_devs); |
| 169 | if (tmp->bid_devno == devno) { |
| 170 | if (only_if_new && !access(tmp->bid_name, F_OK)) |
| 171 | return; |
| 172 | dev = blkid_verify(cache, tmp); |
| 173 | if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED)) |
| 174 | break; |
| 175 | dev = 0; |
| 176 | } |
| 177 | } |
| 178 | if (dev && dev->bid_devno == devno) |
| 179 | goto set_pri; |
| 180 | |
| 181 | /* Try to translate private device-mapper dm-<N> names |
| 182 | * to standard /dev/mapper/<name>. |
| 183 | */ |
| 184 | if (!strncmp(ptname, "dm-", 3) && isdigit(ptname[3])) { |
| 185 | devname = canonicalize_dm_name(ptname); |
| 186 | if (!devname) |
| 187 | blkid__scan_dir("/dev/mapper", devno, 0, &devname); |
| 188 | if (devname) |
| 189 | goto get_dev; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Take a quick look at /dev/ptname for the device number. We check |
| 194 | * all of the likely device directories. If we don't find it, or if |
| 195 | * the stat information doesn't check out, use blkid_devno_to_devname() |
| 196 | * to find it via an exhaustive search for the device major/minor. |
| 197 | */ |
| 198 | for (dir = dirlist; *dir; dir++) { |
| 199 | struct stat st; |
| 200 | char device[256]; |
| 201 | |
| 202 | snprintf(device, sizeof(device), "%s/%s", *dir, ptname); |
| 203 | if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) && |
| 204 | dev->bid_devno == devno) |
| 205 | goto set_pri; |
| 206 | |
| 207 | if (stat(device, &st) == 0 && |
| 208 | (S_ISBLK(st.st_mode) || |
| 209 | (S_ISCHR(st.st_mode) && !strncmp(ptname, "ubi", 3))) && |
| 210 | st.st_rdev == devno) { |
| 211 | devname = strdup(device); |
| 212 | goto get_dev; |
| 213 | } |
| 214 | } |
| 215 | /* Do a short-cut scan of /dev/mapper first */ |
| 216 | if (!devname) |
| 217 | blkid__scan_dir("/dev/mapper", devno, 0, &devname); |
| 218 | if (!devname) { |
| 219 | devname = blkid_devno_to_devname(devno); |
| 220 | if (!devname) |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | get_dev: |
| 225 | dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL); |
| 226 | free(devname); |
| 227 | |
| 228 | set_pri: |
| 229 | if (dev) { |
| 230 | if (pri) |
| 231 | dev->bid_pri = pri; |
| 232 | else if (!strncmp(dev->bid_name, "/dev/mapper/", 11)) { |
| 233 | dev->bid_pri = BLKID_PRI_DM; |
| 234 | if (is_dm_leaf(ptname)) |
| 235 | dev->bid_pri += 5; |
| 236 | } else if (!strncmp(ptname, "md", 2)) |
| 237 | dev->bid_pri = BLKID_PRI_MD; |
| 238 | if (removable) |
| 239 | dev->bid_flags |= BLKID_BID_FL_REMOVABLE; |
| 240 | } |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | #define PROC_PARTITIONS "/proc/partitions" |
| 245 | #define VG_DIR "/proc/lvm/VGs" |
| 246 | |
| 247 | /* |
| 248 | * This function initializes the UUID cache with devices from the LVM |
| 249 | * proc hierarchy. We currently depend on the names of the LVM |
| 250 | * hierarchy giving us the device structure in /dev. (XXX is this a |
| 251 | * safe thing to do?) |
| 252 | */ |
| 253 | #ifdef VG_DIR |
| 254 | static dev_t lvm_get_devno(const char *lvm_device) |
| 255 | { |
| 256 | FILE *lvf; |
| 257 | char buf[1024]; |
| 258 | int ma, mi; |
| 259 | dev_t ret = 0; |
| 260 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 261 | DBG(DEVNAME, ul_debug("opening %s", lvm_device)); |
| 262 | if ((lvf = fopen(lvm_device, "r" UL_CLOEXECSTR)) == NULL) { |
| 263 | DBG(DEVNAME, ul_debug("%s: (%d) %m", lvm_device, errno)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | while (fgets(buf, sizeof(buf), lvf)) { |
| 268 | if (sscanf(buf, "device: %d:%d", &ma, &mi) == 2) { |
| 269 | ret = makedev(ma, mi); |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | fclose(lvf); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static void lvm_probe_all(blkid_cache cache, int only_if_new) |
| 279 | { |
| 280 | DIR *vg_list; |
| 281 | struct dirent *vg_iter; |
| 282 | int vg_len = strlen(VG_DIR); |
| 283 | dev_t dev; |
| 284 | |
| 285 | if ((vg_list = opendir(VG_DIR)) == NULL) |
| 286 | return; |
| 287 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 288 | DBG(DEVNAME, ul_debug("probing LVM devices under %s", VG_DIR)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 289 | |
| 290 | while ((vg_iter = readdir(vg_list)) != NULL) { |
| 291 | DIR *lv_list; |
| 292 | char *vdirname; |
| 293 | char *vg_name; |
| 294 | struct dirent *lv_iter; |
| 295 | |
| 296 | vg_name = vg_iter->d_name; |
| 297 | if (!strcmp(vg_name, ".") || !strcmp(vg_name, "..")) |
| 298 | continue; |
| 299 | vdirname = malloc(vg_len + strlen(vg_name) + 8); |
| 300 | if (!vdirname) |
| 301 | goto exit; |
| 302 | sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name); |
| 303 | |
| 304 | lv_list = opendir(vdirname); |
| 305 | free(vdirname); |
| 306 | if (lv_list == NULL) |
| 307 | continue; |
| 308 | |
| 309 | while ((lv_iter = readdir(lv_list)) != NULL) { |
| 310 | char *lv_name, *lvm_device; |
| 311 | |
| 312 | lv_name = lv_iter->d_name; |
| 313 | if (!strcmp(lv_name, ".") || !strcmp(lv_name, "..")) |
| 314 | continue; |
| 315 | |
| 316 | lvm_device = malloc(vg_len + strlen(vg_name) + |
| 317 | strlen(lv_name) + 8); |
| 318 | if (!lvm_device) { |
| 319 | closedir(lv_list); |
| 320 | goto exit; |
| 321 | } |
| 322 | sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name, |
| 323 | lv_name); |
| 324 | dev = lvm_get_devno(lvm_device); |
| 325 | sprintf(lvm_device, "%s/%s", vg_name, lv_name); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 326 | DBG(DEVNAME, ul_debug("LVM dev %s: devno 0x%04X", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 327 | lvm_device, |
| 328 | (unsigned int) dev)); |
| 329 | probe_one(cache, lvm_device, dev, BLKID_PRI_LVM, |
| 330 | only_if_new, 0); |
| 331 | free(lvm_device); |
| 332 | } |
| 333 | closedir(lv_list); |
| 334 | } |
| 335 | exit: |
| 336 | closedir(vg_list); |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | #define PROC_EVMS_VOLUMES "/proc/evms/volumes" |
| 341 | |
| 342 | static int |
| 343 | evms_probe_all(blkid_cache cache, int only_if_new) |
| 344 | { |
| 345 | char line[100]; |
| 346 | int ma, mi, sz, num = 0; |
| 347 | FILE *procpt; |
| 348 | char device[110]; |
| 349 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 350 | procpt = fopen(PROC_EVMS_VOLUMES, "r" UL_CLOEXECSTR); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 351 | if (!procpt) |
| 352 | return 0; |
| 353 | while (fgets(line, sizeof(line), procpt)) { |
| 354 | if (sscanf (line, " %d %d %d %*s %*s %[^\n ]", |
| 355 | &ma, &mi, &sz, device) != 4) |
| 356 | continue; |
| 357 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 358 | DBG(DEVNAME, ul_debug("Checking partition %s (%d, %d)", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 359 | device, ma, mi)); |
| 360 | |
| 361 | probe_one(cache, device, makedev(ma, mi), BLKID_PRI_EVMS, |
| 362 | only_if_new, 0); |
| 363 | num++; |
| 364 | } |
| 365 | fclose(procpt); |
| 366 | return num; |
| 367 | } |
| 368 | |
| 369 | static void |
| 370 | ubi_probe_all(blkid_cache cache, int only_if_new) |
| 371 | { |
| 372 | const char **dirname; |
| 373 | |
| 374 | for (dirname = dirlist; *dirname; dirname++) { |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 375 | DIR *dir; |
| 376 | struct dirent *iter; |
| 377 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 378 | DBG(DEVNAME, ul_debug("probing UBI volumes under %s", |
| 379 | *dirname)); |
| 380 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 381 | dir = opendir(*dirname); |
| 382 | if (dir == NULL) |
| 383 | continue ; |
| 384 | |
| 385 | while ((iter = readdir(dir)) != NULL) { |
| 386 | char *name; |
| 387 | struct stat st; |
| 388 | dev_t dev; |
| 389 | |
| 390 | name = iter->d_name; |
| 391 | #ifdef _DIRENT_HAVE_D_TYPE |
| 392 | if (iter->d_type != DT_UNKNOWN && |
| 393 | iter->d_type != DT_CHR && iter->d_type != DT_LNK) |
| 394 | continue; |
| 395 | #endif |
| 396 | if (!strcmp(name, ".") || !strcmp(name, "..") || |
| 397 | !strstr(name, "ubi")) |
| 398 | continue; |
| 399 | if (!strcmp(name, "ubi_ctrl")) |
| 400 | continue; |
| 401 | if (fstat_at(dirfd(dir), *dirname, name, &st, 0)) |
| 402 | continue; |
| 403 | |
| 404 | dev = st.st_rdev; |
| 405 | |
| 406 | if (!S_ISCHR(st.st_mode) || !minor(dev)) |
| 407 | continue; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 408 | DBG(DEVNAME, ul_debug("UBI vol %s/%s: devno 0x%04X", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 409 | *dirname, name, (int) dev)); |
| 410 | probe_one(cache, name, dev, BLKID_PRI_UBI, only_if_new, 0); |
| 411 | } |
| 412 | closedir(dir); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Read the device data for all available block devices in the system. |
| 418 | */ |
| 419 | static int probe_all(blkid_cache cache, int only_if_new) |
| 420 | { |
| 421 | FILE *proc; |
| 422 | char line[1024]; |
| 423 | char ptname0[128 + 1], ptname1[128 + 1], *ptname = 0; |
| 424 | char *ptnames[2]; |
| 425 | dev_t devs[2]; |
| 426 | int ma, mi; |
| 427 | unsigned long long sz; |
| 428 | int lens[2] = { 0, 0 }; |
| 429 | int which = 0, last = 0; |
| 430 | struct list_head *p, *pnext; |
| 431 | |
| 432 | ptnames[0] = ptname0; |
| 433 | ptnames[1] = ptname1; |
| 434 | |
| 435 | if (!cache) |
| 436 | return -BLKID_ERR_PARAM; |
| 437 | |
| 438 | if (cache->bic_flags & BLKID_BIC_FL_PROBED && |
| 439 | time(0) - cache->bic_time < BLKID_PROBE_INTERVAL) |
| 440 | return 0; |
| 441 | |
| 442 | blkid_read_cache(cache); |
| 443 | evms_probe_all(cache, only_if_new); |
| 444 | #ifdef VG_DIR |
| 445 | lvm_probe_all(cache, only_if_new); |
| 446 | #endif |
| 447 | ubi_probe_all(cache, only_if_new); |
| 448 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 449 | proc = fopen(PROC_PARTITIONS, "r" UL_CLOEXECSTR); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 450 | if (!proc) |
| 451 | return -BLKID_ERR_PROC; |
| 452 | |
| 453 | while (fgets(line, sizeof(line), proc)) { |
| 454 | last = which; |
| 455 | which ^= 1; |
| 456 | ptname = ptnames[which]; |
| 457 | |
| 458 | if (sscanf(line, " %d %d %llu %128[^\n ]", |
| 459 | &ma, &mi, &sz, ptname) != 4) |
| 460 | continue; |
| 461 | devs[which] = makedev(ma, mi); |
| 462 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 463 | DBG(DEVNAME, ul_debug("read partition name %s", ptname)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 464 | |
| 465 | /* Skip whole disk devs unless they have no partitions. |
| 466 | * If base name of device has changed, also |
| 467 | * check previous dev to see if it didn't have a partn. |
| 468 | * heuristic: partition name ends in a digit, & partition |
| 469 | * names contain whole device name as substring. |
| 470 | * |
| 471 | * Skip extended partitions. |
| 472 | * heuristic: size is 1 |
| 473 | * |
| 474 | * FIXME: skip /dev/{ida,cciss,rd} whole-disk devs |
| 475 | */ |
| 476 | |
| 477 | lens[which] = strlen(ptname); |
| 478 | |
| 479 | /* ends in a digit, clearly a partition, so check */ |
| 480 | if (isdigit(ptname[lens[which] - 1])) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 481 | DBG(DEVNAME, ul_debug("partition dev %s, devno 0x%04X", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 482 | ptname, (unsigned int) devs[which])); |
| 483 | |
| 484 | if (sz > 1) |
| 485 | probe_one(cache, ptname, devs[which], 0, |
| 486 | only_if_new, 0); |
| 487 | lens[which] = 0; /* mark as checked */ |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * If last was a whole disk and we just found a partition |
| 492 | * on it, remove the whole-disk dev from the cache if |
| 493 | * it exists. |
| 494 | */ |
| 495 | if (lens[last] && !strncmp(ptnames[last], ptname, lens[last])) { |
| 496 | list_for_each_safe(p, pnext, &cache->bic_devs) { |
| 497 | blkid_dev tmp; |
| 498 | |
| 499 | /* find blkid dev for the whole-disk devno */ |
| 500 | tmp = list_entry(p, struct blkid_struct_dev, |
| 501 | bid_devs); |
| 502 | if (tmp->bid_devno == devs[last]) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 503 | DBG(DEVNAME, ul_debug("freeing %s", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 504 | tmp->bid_name)); |
| 505 | blkid_free_dev(tmp); |
| 506 | cache->bic_flags |= BLKID_BIC_FL_CHANGED; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | lens[last] = 0; |
| 511 | } |
| 512 | /* |
| 513 | * If last was not checked because it looked like a whole-disk |
| 514 | * dev, and the device's base name has changed, |
| 515 | * check last as well. |
| 516 | */ |
| 517 | if (lens[last] && strncmp(ptnames[last], ptname, lens[last])) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 518 | DBG(DEVNAME, ul_debug("whole dev %s, devno 0x%04X", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 519 | ptnames[last], (unsigned int) devs[last])); |
| 520 | probe_one(cache, ptnames[last], devs[last], 0, |
| 521 | only_if_new, 0); |
| 522 | lens[last] = 0; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* Handle the last device if it wasn't partitioned */ |
| 527 | if (lens[which]) |
| 528 | probe_one(cache, ptname, devs[which], 0, only_if_new, 0); |
| 529 | |
| 530 | fclose(proc); |
| 531 | blkid_flush_cache(cache); |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* Don't use it by default -- it's pretty slow (because cdroms, floppy, ...) |
| 536 | */ |
| 537 | static int probe_all_removable(blkid_cache cache) |
| 538 | { |
| 539 | DIR *dir; |
| 540 | struct dirent *d; |
| 541 | |
| 542 | if (!cache) |
| 543 | return -BLKID_ERR_PARAM; |
| 544 | |
| 545 | dir = opendir(_PATH_SYS_BLOCK); |
| 546 | if (!dir) |
| 547 | return -BLKID_ERR_PROC; |
| 548 | |
| 549 | while((d = readdir(dir))) { |
| 550 | struct sysfs_cxt sysfs = UL_SYSFSCXT_EMPTY; |
| 551 | int removable = 0; |
| 552 | dev_t devno; |
| 553 | |
| 554 | #ifdef _DIRENT_HAVE_D_TYPE |
| 555 | if (d->d_type != DT_UNKNOWN && d->d_type != DT_LNK) |
| 556 | continue; |
| 557 | #endif |
| 558 | if (d->d_name[0] == '.' && |
| 559 | ((d->d_name[1] == 0) || |
| 560 | ((d->d_name[1] == '.') && (d->d_name[2] == 0)))) |
| 561 | continue; |
| 562 | |
| 563 | devno = sysfs_devname_to_devno(d->d_name, NULL); |
| 564 | if (!devno) |
| 565 | continue; |
| 566 | |
| 567 | if (sysfs_init(&sysfs, devno, NULL) == 0) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 568 | if (sysfs_read_int(&sysfs, "removable", &removable) != 0) |
| 569 | removable = 0; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 570 | sysfs_deinit(&sysfs); |
| 571 | } |
| 572 | |
| 573 | if (removable) |
| 574 | probe_one(cache, d->d_name, devno, 0, 0, 1); |
| 575 | } |
| 576 | |
| 577 | closedir(dir); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | /** |
| 583 | * blkid_probe_all: |
| 584 | * @cache: cache handler |
| 585 | * |
| 586 | * Probes all block devices. |
| 587 | * |
| 588 | * Returns: 0 on success, or number less than zero in case of error. |
| 589 | */ |
| 590 | int blkid_probe_all(blkid_cache cache) |
| 591 | { |
| 592 | int ret; |
| 593 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 594 | DBG(PROBE, ul_debug("Begin blkid_probe_all()")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 595 | ret = probe_all(cache, 0); |
| 596 | if (ret == 0) { |
| 597 | cache->bic_time = time(0); |
| 598 | cache->bic_flags |= BLKID_BIC_FL_PROBED; |
| 599 | } |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 600 | DBG(PROBE, ul_debug("End blkid_probe_all() [rc=%d]", ret)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * blkid_probe_all_new: |
| 606 | * @cache: cache handler |
| 607 | * |
| 608 | * Probes all new block devices. |
| 609 | * |
| 610 | * Returns: 0 on success, or number less than zero in case of error. |
| 611 | */ |
| 612 | int blkid_probe_all_new(blkid_cache cache) |
| 613 | { |
| 614 | int ret; |
| 615 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 616 | DBG(PROBE, ul_debug("Begin blkid_probe_all_new()")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 617 | ret = probe_all(cache, 1); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 618 | DBG(PROBE, ul_debug("End blkid_probe_all_new() [rc=%d]", ret)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * blkid_probe_all_removable: |
| 624 | * @cache: cache handler |
| 625 | * |
| 626 | * The libblkid probing is based on devices from /proc/partitions by default. |
| 627 | * This file usually does not contain removable devices (e.g. CDROMs) and this kind |
| 628 | * of devices are invisible for libblkid. |
| 629 | * |
| 630 | * This function adds removable block devices to @cache (probing is based on |
| 631 | * information from the /sys directory). Don't forget that removable devices |
| 632 | * (floppies, CDROMs, ...) could be pretty slow. It's very bad idea to call |
| 633 | * this function by default. |
| 634 | * |
| 635 | * Note that devices which were detected by this function won't be written to |
| 636 | * blkid.tab cache file. |
| 637 | * |
| 638 | * Returns: 0 on success, or number less than zero in case of error. |
| 639 | */ |
| 640 | int blkid_probe_all_removable(blkid_cache cache) |
| 641 | { |
| 642 | int ret; |
| 643 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 644 | DBG(PROBE, ul_debug("Begin blkid_probe_all_removable()")); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 645 | ret = probe_all_removable(cache); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 646 | DBG(PROBE, ul_debug("End blkid_probe_all_removable() [rc=%d]", ret)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | #ifdef TEST_PROGRAM |
| 651 | int main(int argc, char **argv) |
| 652 | { |
| 653 | blkid_cache cache = NULL; |
| 654 | int ret; |
| 655 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 656 | blkid_init_debug(BLKID_DEBUG_ALL); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 657 | if (argc != 1) { |
| 658 | fprintf(stderr, "Usage: %s\n" |
| 659 | "Probe all devices and exit\n", argv[0]); |
| 660 | exit(1); |
| 661 | } |
| 662 | if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) { |
| 663 | fprintf(stderr, "%s: error creating cache (%d)\n", |
| 664 | argv[0], ret); |
| 665 | exit(1); |
| 666 | } |
| 667 | if (blkid_probe_all(cache) < 0) |
| 668 | printf("%s: error probing devices\n", argv[0]); |
| 669 | |
| 670 | if (blkid_probe_all_removable(cache) < 0) |
| 671 | printf("%s: error probing removable devices\n", argv[0]); |
| 672 | |
| 673 | blkid_put_cache(cache); |
| 674 | return (0); |
| 675 | } |
| 676 | #endif |