2 * evaluate.c - very high-level API to evaluate LABELs or UUIDs
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
15 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
25 #include "pathnames.h"
26 #include "canonicalize.h"
32 * @title: Tags and Spec evaluation
33 * @short_description: top-level API for LABEL and UUID evaluation.
35 * This API provides very simple and portable way how evaluate LABEL and UUID
36 * tags. The blkid_evaluate_tag() and blkid_evaluate_spec() work on 2.4 and
37 * 2.6 systems and on systems with or without udev. Currently, the libblkid
38 * library supports "udev" and "scan" methods. The "udev" method uses udev
39 * /dev/disk/by-* symlinks and the "scan" method scans all block devices from
40 * the /proc/partitions file. The evaluation could be controlled by the
41 * /etc/blkid.conf config file. The default is to try "udev" and then "scan"
44 * The blkid_evaluate_tag() also automatically informs udevd when an obsolete
45 * /dev/disk/by-* symlink is detected.
47 * If you are not sure how translate LABEL or UUID to the device name use this
51 /* returns zero when the device has NAME=value (LABEL/UUID) */
52 static int verify_tag(const char *devname, const char *name, const char *value)
60 pr = blkid_new_probe();
64 blkid_probe_enable_superblocks(pr, TRUE);
65 blkid_probe_set_superblocks_flags(pr,
66 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID);
68 fd = open(devname, O_RDONLY);
73 if (blkid_probe_set_device(pr, fd, 0, 0))
75 rc = blkid_do_safeprobe(pr);
78 rc = blkid_probe_lookup_value(pr, name, &data, &len);
80 rc = memcmp(value, data, len);
82 DBG(DEBUG_EVALUATE, printf("%s: %s verification %s\n",
83 devname, name, rc == 0 ? "PASS" : "FAILED"));
88 /* for non-root users we use unverified udev links */
89 return errsv == EACCES ? 0 : rc;
94 * @devname: absolute path to the device
95 * @action: event string
97 * Returns: -1 in case of failure, or 0 on success.
99 int blkid_send_uevent(const char *devname, const char *action)
101 char uevent[PATH_MAX];
106 DBG(DEBUG_EVALUATE, printf("%s: uevent '%s' requested\n", devname, action));
108 if (!devname || !action)
110 if (stat(devname, &st) || !S_ISBLK(st.st_mode))
113 snprintf(uevent, sizeof(uevent), "/sys/dev/block/%d:%d/uevent",
114 major(st.st_rdev), minor(st.st_rdev));
116 f = fopen(uevent, "w");
119 if (fputs(action, f) >= 0)
123 DBG(DEBUG_EVALUATE, printf("%s: send uevent %s\n",
124 uevent, rc == 0 ? "SUCCES" : "FAILED"));
128 static char *evaluate_by_udev(const char *token, const char *value, int uevent)
136 printf("evaluating by udev %s=%s\n", token, value));
138 if (!strcmp(token, "UUID"))
139 strcpy(dev, _PATH_DEV_BYUUID "/");
140 else if (!strcmp(token, "LABEL"))
141 strcpy(dev, _PATH_DEV_BYLABEL "/");
144 printf("unsupported token %s\n", token));
145 return NULL; /* unsupported tag */
149 if (blkid_encode_string(value, &dev[len], sizeof(dev) - len) != 0)
153 printf("expected udev link: %s\n", dev));
156 goto failed; /* link or device does not exist */
158 if (!S_ISBLK(st.st_mode))
161 path = canonicalize_path(dev);
165 if (verify_tag(path, token, value))
170 DBG(DEBUG_EVALUATE, printf("failed to evaluate by udev\n"));
173 blkid_send_uevent(path, "change");
178 static char *evaluate_by_scan(const char *token, const char *value,
179 blkid_cache *cache, struct blkid_config *conf)
181 blkid_cache c = cache ? *cache : NULL;
185 printf("evaluating by blkid scan %s=%s\n", token, value));
188 char *cachefile = blkid_get_cache_filename(conf);
189 blkid_get_cache(&c, cachefile);
195 res = blkid_get_devname(c, token, value);
206 * blkid_evaluate_tag:
207 * @token: token name (e.g "LABEL" or "UUID") or unparsed tag (e.g. "LABEL=foo")
208 * @value: token data (e.g. "foo")
209 * @cache: pointer to cache (or NULL when you don't want to re-use the cache)
211 * Returns: allocated string with a device name.
213 char *blkid_evaluate_tag(const char *token, const char *value, blkid_cache *cache)
215 struct blkid_config *conf = NULL;
216 char *t = NULL, *v = NULL;
223 if (!cache || !*cache)
227 printf("evaluating %s%s%s\n", token, value ? "=" : "",
228 value ? value : ""));
231 if (!strchr(token, '=')) {
232 ret = blkid_strdup(token);
235 blkid_parse_tag_string(token, &t, &v);
242 conf = blkid_read_config(NULL);
246 for (i = 0; i < conf->nevals; i++) {
247 if (conf->eval[i] == BLKID_EVAL_UDEV)
248 ret = evaluate_by_udev(token, value, conf->uevent);
249 else if (conf->eval[i] == BLKID_EVAL_SCAN)
250 ret = evaluate_by_scan(token, value, cache, conf);
256 printf("%s=%s evaluated as %s\n", token, value, ret));
258 blkid_free_config(conf);
265 * blkid_evaluate_spec:
266 * @spec: unparsed tag (e.g. "LABEL=foo") or path (e.g. /dev/dm-0)
267 * @cache: pointer to cache (or NULL when you don't want to re-use the cache)
269 * All returned paths are canonicalized, device-mapper paths are converted
270 * to the /dev/mapper/name format.
272 * Returns: allocated string with a device name.
274 char *blkid_evaluate_spec(const char *spec, blkid_cache *cache)
276 char *t = NULL, *v = NULL, *res;
281 if (strchr(spec, '=') &&
282 blkid_parse_tag_string(spec, &t, &v) != 0) /* parse error */
286 res = blkid_evaluate_tag(t, v, cache);
288 res = canonicalize_path(spec);
297 int main(int argc, char *argv[])
299 blkid_cache cache = NULL;
303 fprintf(stderr, "usage: %s <tag> | <spec>\n", argv[0]);
309 res = blkid_evaluate_spec(argv[1], &cache);
313 blkid_put_cache(cache);
315 return res ? EXIT_SUCCESS : EXIT_FAILURE;