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