]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/blkid.c
Merge branch 'sed' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / misc-utils / blkid.c
1 /*
2 * blkid.c - User command-line interface for libblkid
3 *
4 * Copyright (C) 2001 Andreas Dilger
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 * %End-Header%
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <getopt.h>
21
22 #define OUTPUT_FULL (1 << 0)
23 #define OUTPUT_VALUE_ONLY (1 << 1)
24 #define OUTPUT_DEVICE_ONLY (1 << 2)
25 #define OUTPUT_PRETTY_LIST (1 << 3) /* deprecated */
26 #define OUTPUT_UDEV_LIST (1 << 4) /* deprecated */
27 #define OUTPUT_EXPORT_LIST (1 << 5)
28 #define OUTPUT_JSON (1 << 6)
29
30 #define BLKID_EXIT_NOTFOUND 2 /* token or device not found */
31 #define BLKID_EXIT_OTHER 4 /* bad usage or other error */
32 #define BLKID_EXIT_AMBIVAL 8 /* ambivalent low-level probing detected */
33
34 #include <blkid.h>
35
36 #include "ismounted.h"
37
38 #include "strutils.h"
39 #define OPTUTILS_EXIT_CODE BLKID_EXIT_OTHER /* exclusive_option() */
40 #include "optutils.h"
41 #define CLOSE_EXIT_CODE BLKID_EXIT_OTHER /* close_stdout() */
42 #include "closestream.h"
43
44 #include "nls.h"
45 #include "ttyutils.h"
46
47 #define XALLOC_EXIT_CODE BLKID_EXIT_OTHER /* x.*alloc(), xstrndup() */
48 #include "xalloc.h"
49
50 #include "sysfs.h"
51 #include "jsonwrt.h"
52
53 struct blkid_control {
54 int output;
55 uintmax_t offset;
56 uintmax_t size;
57 char *show[128];
58 struct ul_jsonwrt *json_fmt;
59 unsigned int
60 eval:1,
61 gc:1,
62 lookup:1,
63 lowprobe:1,
64 lowprobe_superblocks:1,
65 lowprobe_topology:1,
66 no_part_details:1,
67 raw_chars:1;
68 };
69
70 static void __attribute__((__noreturn__)) usage(void)
71 {
72 FILE *out = stdout;
73
74 fputs(USAGE_HEADER, out);
75 fprintf(out, _( " %s --label <label> | --uuid <uuid>\n\n"), program_invocation_short_name);
76 fprintf(out, _( " %s [--cache-file <file>] [-ghlLv] [--output <format>] [--match-tag <tag>] \n"
77 " [--match-token <token>] [<dev> ...]\n\n"), program_invocation_short_name);
78 fprintf(out, _( " %s -p [--match-tag <tag>] [--offset <offset>] [--size <size>] \n"
79 " [--output <format>] <dev> ...\n\n"), program_invocation_short_name);
80 fprintf(out, _( " %s -i [--match-tag <tag>] [--output <format>] <dev> ...\n"), program_invocation_short_name);
81 fputs(USAGE_OPTIONS, out);
82 fputs(_( " -c, --cache-file <file> read from <file> instead of reading from the default\n"
83 " cache file (-c /dev/null means no cache)\n"), out);
84 fputs(_( " -d, --no-encoding don't encode non-printing characters\n"), out);
85 fputs(_( " -g, --garbage-collect garbage collect the blkid cache\n"), out);
86 fputs(_( " -o, --output <format> output format; can be one of:\n"
87 " value, device, export, json or full; (default: full)\n"), out);
88 fputs(_( " -k, --list-filesystems list all known filesystems/RAIDs and exit\n"), out);
89 fputs(_( " -s, --match-tag <tag> show specified tag(s) (default show all tags)\n"), out);
90 fputs(_( " -t, --match-token <token> find device with a specific token (NAME=value pair)\n"), out);
91 fputs(_( " -l, --list-one look up only first device with token specified by -t\n"), out);
92 fputs(_( " -L, --label <label> convert LABEL to device name\n"), out);
93 fputs(_( " -U, --uuid <uuid> convert UUID to device name\n"), out);
94 fputs( "\n", out);
95 fputs(_( "Low-level probing options:\n"), out);
96 fputs(_( " -p, --probe low-level superblocks probing (bypass cache)\n"), out);
97 fputs(_( " -i, --info gather information about I/O limits\n"), out);
98 fputs(_( " -H, --hint <value> set hint for probing function\n"), out);
99 fputs(_( " -S, --size <size> overwrite device size\n"), out);
100 fputs(_( " -O, --offset <offset> probe at the given offset\n"), out);
101 fputs(_( " -u, --usages <list> filter by \"usage\" (e.g. -u filesystem,raid)\n"), out);
102 fputs(_( " -n, --match-types <list> filter by filesystem type (e.g. -n vfat,ext3)\n"), out);
103 fputs(_( " -D, --no-part-details don't print info from partition table\n"), out);
104
105 fputs(USAGE_SEPARATOR, out);
106 fprintf(out, USAGE_HELP_OPTIONS(28));
107
108 fputs(USAGE_ARGUMENTS, out);
109 fprintf(out, USAGE_ARG_SIZE(_("<size> and <offset>")));
110 fputs(USAGE_ARG_SEPARATOR, out);
111 fputs(_(" <dev> specify device(s) to probe (default: all devices)\n"), out);
112
113 fprintf(out, USAGE_MAN_TAIL("blkid(8)"));
114 exit(EXIT_SUCCESS);
115 }
116
117 /*
118 * This function does "safe" printing. It will convert non-printable
119 * ASCII characters using '^' and M- notation.
120 *
121 * If 'esc' is defined then escape all chars from esc by \.
122 */
123 static void safe_print(const struct blkid_control *ctl, const char *cp, int len,
124 const char *esc)
125 {
126 unsigned char ch;
127
128 if (len < 0)
129 len = strlen(cp);
130
131 while (len--) {
132 ch = *cp++;
133 if (!ctl->raw_chars) {
134 if (ch >= 128) {
135 fputs("M-", stdout);
136 ch -= 128;
137 }
138 if ((ch < 32) || (ch == 0x7f)) {
139 fputc('^', stdout);
140 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
141
142 } else if (esc && strchr(esc, ch))
143 fputc('\\', stdout);
144 }
145 fputc(ch, stdout);
146 }
147 }
148
149 static int pretty_print_word(const char *str, int max_len,
150 int left_len, int overflow_nl)
151 {
152 int len = strlen(str) + left_len;
153 int ret = 0;
154
155 fputs(str, stdout);
156 if (overflow_nl && len > max_len) {
157 fputc('\n', stdout);
158 len = 0;
159 } else if (len > max_len)
160 ret = len - max_len;
161 do {
162 fputc(' ', stdout);
163 } while (len++ < max_len);
164 return ret;
165 }
166
167 static void pretty_print_line(const char *device, const char *fs_type,
168 const char *label, const char *mtpt,
169 const char *uuid)
170 {
171 static int device_len = 10, fs_type_len = 7;
172 static int label_len = 8, mtpt_len = 14;
173 static int term_width = -1;
174 int len, w;
175
176 if (term_width < 0) {
177 term_width = get_terminal_width(80);
178 }
179 if (term_width > 80) {
180 term_width -= 80;
181 w = term_width / 10;
182 if (w > 8)
183 w = 8;
184 term_width -= 2*w;
185 label_len += w;
186 fs_type_len += w;
187 w = term_width/2;
188 device_len += w;
189 mtpt_len +=w;
190 }
191
192 len = pretty_print_word(device, device_len, 0, 1);
193 len = pretty_print_word(fs_type, fs_type_len, len, 0);
194 len = pretty_print_word(label, label_len, len, 0);
195 pretty_print_word(mtpt, mtpt_len, len, 0);
196
197 fputs(uuid, stdout);
198 fputc('\n', stdout);
199 }
200
201 static void pretty_print_dev(blkid_dev dev)
202 {
203 blkid_tag_iterate iter;
204 const char *type, *value, *devname;
205 const char *uuid = "", *fs_type = "", *label = "";
206 int len, mount_flags;
207 char mtpt[80];
208 int retval;
209
210 if (dev == NULL) {
211 pretty_print_line("device", "fs_type", "label",
212 "mount point", "UUID");
213 for (len=get_terminal_width(0)-1; len > 0; len--)
214 fputc('-', stdout);
215 fputc('\n', stdout);
216 return;
217 }
218
219 devname = blkid_dev_devname(dev);
220 if (access(devname, F_OK))
221 return;
222
223 /* Get the uuid, label, type */
224 iter = blkid_tag_iterate_begin(dev);
225 while (blkid_tag_next(iter, &type, &value) == 0) {
226 if (!strcmp(type, "UUID"))
227 uuid = value;
228 if (!strcmp(type, "TYPE"))
229 fs_type = value;
230 if (!strcmp(type, "LABEL"))
231 label = value;
232 }
233 blkid_tag_iterate_end(iter);
234
235 /* Get the mount point */
236 mtpt[0] = 0;
237 retval = check_mount_point(devname, &mount_flags, mtpt, sizeof(mtpt));
238 if (retval == 0) {
239 const char *msg = NULL;
240
241 if (mount_flags & MF_MOUNTED) {
242 if (!mtpt[0])
243 msg = _("(mounted, mtpt unknown)");
244 } else if (mount_flags & MF_BUSY)
245 msg = _("(in use)");
246 else
247 msg = _("(not mounted)");
248
249 if (msg)
250 xstrncpy(mtpt, msg, sizeof(mtpt));
251 }
252
253 pretty_print_line(devname, fs_type, label, mtpt, uuid);
254 }
255
256 static void print_udev_format(const char *name, const char *value)
257 {
258 char enc[265], safe[256];
259 size_t namelen = strlen(name);
260
261 *safe = *enc = '\0';
262
263 if (!strcmp(name, "TYPE")
264 || !strcmp(name, "VERSION")
265 || !strcmp(name, "SYSTEM_ID")
266 || !strcmp(name, "PUBLISHER_ID")
267 || !strcmp(name, "APPLICATION_ID")
268 || !strcmp(name, "BOOT_SYSTEM_ID")
269 || !strcmp(name, "VOLUME_ID")
270 || !strcmp(name, "LOGICAL_VOLUME_ID")
271 || !strcmp(name, "VOLUME_SET_ID")
272 || !strcmp(name, "DATA_PREPARER_ID")) {
273 blkid_encode_string(value, enc, sizeof(enc));
274 printf("ID_FS_%s=%s\n", name, enc);
275
276 } else if (!strcmp(name, "UUID") ||
277 !strncmp(name, "LABEL", 5) ||
278 !strcmp(name, "UUID_SUB")) {
279
280 blkid_safe_string(value, safe, sizeof(safe));
281 printf("ID_FS_%s=%s\n", name, safe);
282
283 blkid_encode_string(value, enc, sizeof(enc));
284 printf("ID_FS_%s_ENC=%s\n", name, enc);
285
286 } else if (!strcmp(name, "PTUUID")) {
287 printf("ID_PART_TABLE_UUID=%s\n", value);
288
289 } else if (!strcmp(name, "PTTYPE")) {
290 printf("ID_PART_TABLE_TYPE=%s\n", value);
291
292 } else if (!strcmp(name, "PART_ENTRY_NAME") ||
293 !strcmp(name, "PART_ENTRY_TYPE")) {
294
295 blkid_encode_string(value, enc, sizeof(enc));
296 printf("ID_%s=%s\n", name, enc);
297
298 } else if (!strncmp(name, "PART_ENTRY_", 11))
299 printf("ID_%s=%s\n", name, value);
300
301 else if (namelen >= 15 && (
302 !strcmp(name + (namelen - 12), "_SECTOR_SIZE") ||
303 !strcmp(name + (namelen - 8), "_IO_SIZE") ||
304 !strcmp(name, "ALIGNMENT_OFFSET")))
305 printf("ID_IOLIMIT_%s=%s\n", name, value);
306 else
307 printf("ID_FS_%s=%s\n", name, value);
308 }
309
310 static int has_item(const struct blkid_control *ctl, const char *item)
311 {
312 char * const *p;
313
314 for (p = ctl->show; *p != NULL; p++)
315 if (!strcmp(item, *p))
316 return 1;
317 return 0;
318 }
319
320 static void print_value(const struct blkid_control *ctl, int num,
321 const char *devname, const char *value,
322 const char *name, size_t valsz)
323 {
324 if (ctl->output & OUTPUT_VALUE_ONLY) {
325 fputs(value, stdout);
326 fputc('\n', stdout);
327
328 } else if (ctl->output & OUTPUT_UDEV_LIST) {
329 print_udev_format(name, value);
330
331 } else if (ctl->output & OUTPUT_EXPORT_LIST) {
332 if (num == 1 && devname)
333 printf("DEVNAME=%s\n", devname);
334 fputs(name, stdout);
335 fputs("=", stdout);
336 safe_print(ctl, value, valsz, " \\\"'$`<>");
337 fputs("\n", stdout);
338
339 } else if (ctl->output & OUTPUT_JSON) {
340 ul_jsonwrt_value_s_sized(ctl->json_fmt, name, value, valsz);
341
342 } else {
343 if (num == 1 && devname)
344 printf("%s:", devname);
345 fputs(" ", stdout);
346 fputs(name, stdout);
347 fputs("=\"", stdout);
348 safe_print(ctl, value, valsz, "\"\\");
349 fputs("\"", stdout);
350 }
351 }
352
353 static void print_tags(const struct blkid_control *ctl, blkid_dev dev)
354 {
355 blkid_tag_iterate iter;
356 const char *type, *value, *devname;
357 int num = 1;
358 static int first = 1;
359
360 if (!dev)
361 return;
362
363 if (ctl->output & OUTPUT_PRETTY_LIST) {
364 pretty_print_dev(dev);
365 return;
366 }
367
368 devname = blkid_dev_devname(dev);
369
370 if (ctl->output & OUTPUT_DEVICE_ONLY) {
371 printf("%s\n", devname);
372 return;
373 }
374
375 if (ctl->output == OUTPUT_JSON) {
376 ul_jsonwrt_init(ctl->json_fmt, stdout, 0);
377 ul_jsonwrt_open(ctl->json_fmt, NULL, UL_JSON_OBJECT);
378 }
379
380 iter = blkid_tag_iterate_begin(dev);
381 while (blkid_tag_next(iter, &type, &value) == 0) {
382 if (ctl->show[0] && !has_item(ctl, type))
383 continue;
384
385 if (num == 1 && !first &&
386 (ctl->output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST)))
387 /* add extra line between output from more devices */
388 fputc('\n', stdout);
389
390 print_value(ctl, num++, devname, value, type, strlen(value));
391 }
392 blkid_tag_iterate_end(iter);
393
394 if (ctl->output == OUTPUT_JSON)
395 ul_jsonwrt_close(ctl->json_fmt, UL_JSON_OBJECT);
396
397 if (num > 1) {
398 if (!(ctl->output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST |
399 OUTPUT_EXPORT_LIST |
400 OUTPUT_JSON)))
401 printf("\n");
402 first = 0;
403 }
404 }
405
406
407 static int append_str(char **res, size_t *sz, const char *a, const char *b)
408 {
409 char *str = *res;
410 size_t asz = a ? strlen(a) : 0;
411 size_t bsz = b ? strlen(b) : 0;
412 size_t len = *sz + asz + bsz;
413
414 if (!len)
415 return -1;
416
417 *res = str = xrealloc(str, len + 1);
418 str += *sz;
419
420 if (a)
421 str = mempcpy(str, a, asz);
422 if (b)
423 str = mempcpy(str, b, bsz);
424
425 *str = '\0';
426 *sz = len;
427 return 0;
428 }
429
430 /*
431 * Compose and print ID_FS_AMBIVALENT for udev
432 */
433 static int print_udev_ambivalent(blkid_probe pr)
434 {
435 char *val = NULL;
436 size_t valsz = 0;
437 int count = 0, rc = -1;
438
439 while (!blkid_do_probe(pr)) {
440 const char *usage_txt = NULL, *type = NULL, *version = NULL;
441 char enc[256];
442
443 blkid_probe_lookup_value(pr, "USAGE", &usage_txt, NULL);
444 blkid_probe_lookup_value(pr, "TYPE", &type, NULL);
445 blkid_probe_lookup_value(pr, "VERSION", &version, NULL);
446
447 if (!usage_txt || !type)
448 continue;
449
450 blkid_encode_string(usage_txt, enc, sizeof(enc));
451 if (append_str(&val, &valsz, enc, ":"))
452 goto done;
453
454 blkid_encode_string(type, enc, sizeof(enc));
455 if (append_str(&val, &valsz, enc, version ? ":" : " "))
456 goto done;
457
458 if (version) {
459 blkid_encode_string(version, enc, sizeof(enc));
460 if (append_str(&val, &valsz, enc, " "))
461 goto done;
462 }
463 count++;
464 }
465
466 if (count > 1) {
467 *(val + valsz - 1) = '\0'; /* rem tailing whitespace */
468 printf("ID_FS_AMBIVALENT=%s\n", val);
469 rc = 0;
470 }
471 done:
472 free(val);
473 return rc;
474 }
475
476 static int lowprobe_superblocks(blkid_probe pr, struct blkid_control *ctl)
477 {
478 struct stat st;
479 int rc, fd = blkid_probe_get_fd(pr);
480
481 if (fd < 0 || fstat(fd, &st))
482 return -1;
483
484 blkid_probe_enable_partitions(pr, 1);
485
486 if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 &&
487 blkid_probe_is_wholedisk(pr)) {
488 /*
489 * check if the small disk is partitioned, if yes then
490 * don't probe for filesystems.
491 */
492 blkid_probe_enable_superblocks(pr, 0);
493
494 rc = blkid_do_fullprobe(pr);
495 if (rc < 0)
496 return rc; /* -1 = error, 1 = nothing, 0 = success */
497
498 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
499 return 0; /* partition table detected */
500 }
501
502 if (!ctl->no_part_details)
503 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
504 blkid_probe_enable_superblocks(pr, 1);
505
506 return blkid_do_safeprobe(pr);
507 }
508
509 static int lowprobe_topology(blkid_probe pr)
510 {
511 /* enable topology probing only */
512 blkid_probe_enable_topology(pr, 1);
513
514 blkid_probe_enable_superblocks(pr, 0);
515 blkid_probe_enable_partitions(pr, 0);
516
517 return blkid_do_fullprobe(pr);
518 }
519
520 static int lowprobe_device(blkid_probe pr, const char *devname,
521 struct blkid_control *ctl)
522 {
523 const char *data;
524 const char *name;
525 int nvals = 0, n, num = 1;
526 size_t len;
527 int fd;
528 int rc = 0;
529 static int first = 1;
530
531 fd = open(devname, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
532 if (fd < 0) {
533 warn(_("error: %s"), devname);
534 return BLKID_EXIT_NOTFOUND;
535 }
536 errno = 0;
537 if (blkid_probe_set_device(pr, fd, ctl->offset, ctl->size)) {
538 if (errno)
539 warn(_("error: %s"), devname);
540 goto done;
541 }
542
543 if (ctl->lowprobe_topology)
544 rc = lowprobe_topology(pr);
545 if (rc >= 0 && ctl->lowprobe_superblocks)
546 rc = lowprobe_superblocks(pr, ctl);
547 if (rc < 0)
548 goto done;
549
550 if (!rc)
551 nvals = blkid_probe_numof_values(pr);
552
553 if (nvals && !first && ctl->output & (OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST))
554 /* add extra line between output from devices */
555 fputc('\n', stdout);
556
557 if (ctl->output == OUTPUT_JSON) {
558 ul_jsonwrt_init(ctl->json_fmt, stdout, 0);
559 ul_jsonwrt_open(ctl->json_fmt, NULL, UL_JSON_OBJECT);
560 }
561
562 if (nvals && (ctl->output & OUTPUT_DEVICE_ONLY)) {
563 printf("%s\n", devname);
564 goto done;
565 }
566
567 for (n = 0; n < nvals; n++) {
568 if (blkid_probe_get_value(pr, n, &name, &data, &len))
569 continue;
570 if (ctl->show[0] && !has_item(ctl, name))
571 continue;
572 len = strnlen(data, len);
573 print_value(ctl, num++, devname, data, name, len);
574 }
575
576 if (first)
577 first = 0;
578
579 if (nvals >= 1 && !(ctl->output & (OUTPUT_VALUE_ONLY |
580 OUTPUT_UDEV_LIST | OUTPUT_EXPORT_LIST |
581 OUTPUT_JSON)))
582 printf("\n");
583
584 if (ctl->output == OUTPUT_JSON)
585 ul_jsonwrt_close(ctl->json_fmt, UL_JSON_OBJECT);
586 done:
587 if (rc == -2) {
588 if (ctl->output & OUTPUT_UDEV_LIST)
589 print_udev_ambivalent(pr);
590 else
591 warnx(_("%s: ambivalent result (probably more "
592 "filesystems on the device, use wipefs(8) "
593 "to see more details)"),
594 devname);
595 }
596 close(fd);
597
598 if (rc == -2)
599 return BLKID_EXIT_AMBIVAL; /* ambivalent probing result */
600 if (!nvals)
601 return BLKID_EXIT_NOTFOUND; /* nothing detected */
602
603 return 0; /* success */
604 }
605
606 /* converts comma separated list to BLKID_USAGE_* mask */
607 static int list_to_usage(const char *list, int *flag)
608 {
609 int mask = 0;
610 const char *word = NULL, *p = list;
611
612 if (p && strncmp(p, "no", 2) == 0) {
613 *flag = BLKID_FLTR_NOTIN;
614 p += 2;
615 }
616 if (!p || !*p)
617 goto err;
618 while(p) {
619 word = p;
620 p = strchr(p, ',');
621 if (p)
622 p++;
623 if (!strncmp(word, "filesystem", 10))
624 mask |= BLKID_USAGE_FILESYSTEM;
625 else if (!strncmp(word, "raid", 4))
626 mask |= BLKID_USAGE_RAID;
627 else if (!strncmp(word, "crypto", 6))
628 mask |= BLKID_USAGE_CRYPTO;
629 else if (!strncmp(word, "other", 5))
630 mask |= BLKID_USAGE_OTHER;
631 else
632 goto err;
633 }
634 return mask;
635 err:
636 *flag = 0;
637 warnx(_("unknown keyword in -u <list> argument: '%s'"),
638 word ? word : list);
639 exit(BLKID_EXIT_OTHER);
640 }
641
642 /* converts comma separated list to types[] */
643 static char **list_to_types(const char *list, int *flag)
644 {
645 int i;
646 const char *p = list;
647 char **res = NULL;
648
649 if (p && strncmp(p, "no", 2) == 0) {
650 *flag = BLKID_FLTR_NOTIN;
651 p += 2;
652 }
653 if (!p || !*p) {
654 warnx(_("error: -u <list> argument is empty"));
655 goto err;
656 }
657 for (i = 1; p && (p = strchr(p, ',')); i++, p++);
658
659 res = xcalloc(i + 1, sizeof(char *));
660 p = *flag & BLKID_FLTR_NOTIN ? list + 2 : list;
661 i = 0;
662
663 while(p) {
664 const char *word = p;
665 p = strchr(p, ',');
666 res[i++] = p ? xstrndup(word, p - word) : xstrdup(word);
667 if (p)
668 p++;
669 }
670 res[i] = NULL;
671 return res;
672 err:
673 *flag = 0;
674 free(res);
675 exit(BLKID_EXIT_OTHER);
676 }
677
678 static void free_types_list(char *list[])
679 {
680 char **n;
681
682 if (!list)
683 return;
684 for (n = list; *n; n++)
685 free(*n);
686 free(list);
687 }
688
689 int main(int argc, char **argv)
690 {
691 struct ul_jsonwrt json_fmt;
692 struct blkid_control ctl = {
693 .output = OUTPUT_FULL,
694 .json_fmt = &json_fmt,
695 };
696 blkid_cache cache = NULL;
697 char **devices = NULL;
698 char *search_type = NULL, *search_value = NULL;
699 char *read = NULL, *hint = NULL;
700 int fltr_usage = 0;
701 char **fltr_type = NULL;
702 int fltr_flag = BLKID_FLTR_ONLYIN;
703 unsigned int numdev = 0, numtag = 0;
704 int err = BLKID_EXIT_OTHER;
705 unsigned int i;
706 int c;
707
708 static const struct option longopts[] = {
709 { "cache-file", required_argument, NULL, 'c' },
710 { "no-encoding", no_argument, NULL, 'd' },
711 { "no-part-details", no_argument, NULL, 'D' },
712 { "garbage-collect", no_argument, NULL, 'g' },
713 { "output", required_argument, NULL, 'o' },
714 { "list-filesystems", no_argument, NULL, 'k' },
715 { "match-tag", required_argument, NULL, 's' },
716 { "match-token", required_argument, NULL, 't' },
717 { "list-one", no_argument, NULL, 'l' },
718 { "label", required_argument, NULL, 'L' },
719 { "uuid", required_argument, NULL, 'U' },
720 { "probe", no_argument, NULL, 'p' },
721 { "hint", required_argument, NULL, 'H' },
722 { "info", no_argument, NULL, 'i' },
723 { "size", required_argument, NULL, 'S' },
724 { "offset", required_argument, NULL, 'O' },
725 { "usages", required_argument, NULL, 'u' },
726 { "match-types", required_argument, NULL, 'n' },
727 { "version", no_argument, NULL, 'V' },
728 { "help", no_argument, NULL, 'h' },
729 { NULL, 0, NULL, 0 }
730 };
731
732 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
733 { 'n','u' },
734 { 0 }
735 };
736 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
737
738 setlocale(LC_ALL, "");
739 bindtextdomain(PACKAGE, LOCALEDIR);
740 textdomain(PACKAGE);
741 close_stdout_atexit();
742
743 strutils_set_exitcode(BLKID_EXIT_OTHER);
744
745 while ((c = getopt_long (argc, argv,
746 "c:DdgH:hilL:n:ko:O:ps:S:t:u:U:w:Vv", longopts, NULL)) != -1) {
747
748 err_exclusive_options(c, longopts, excl, excl_st);
749
750 switch (c) {
751 case 'c':
752 read = optarg;
753 break;
754 case 'd':
755 ctl.raw_chars = 1;
756 break;
757 case 'D':
758 ctl.no_part_details = 1;
759 break;
760 case 'H':
761 hint = optarg;
762 break;
763 case 'L':
764 ctl.eval = 1;
765 search_value = xstrdup(optarg);
766 search_type = xstrdup("LABEL");
767 break;
768 case 'n':
769 fltr_type = list_to_types(optarg, &fltr_flag);
770 break;
771 case 'u':
772 fltr_usage = list_to_usage(optarg, &fltr_flag);
773 break;
774 case 'U':
775 ctl.eval = 1;
776 search_value = xstrdup(optarg);
777 search_type = xstrdup("UUID");
778 break;
779 case 'i':
780 ctl.lowprobe_topology = 1;
781 break;
782 case 'l':
783 ctl.lookup = 1;
784 break;
785 case 'g':
786 ctl.gc = 1;
787 break;
788 case 'k':
789 {
790 size_t idx = 0;
791 const char *name = NULL;
792
793 while (blkid_superblocks_get_name(idx++, &name, NULL) == 0)
794 printf("%s\n", name);
795 exit(EXIT_SUCCESS);
796 }
797 case 'o':
798 if (!strcmp(optarg, "value"))
799 ctl.output = OUTPUT_VALUE_ONLY;
800 else if (!strcmp(optarg, "device"))
801 ctl.output = OUTPUT_DEVICE_ONLY;
802 else if (!strcmp(optarg, "list"))
803 ctl.output = OUTPUT_PRETTY_LIST; /* deprecated */
804 else if (!strcmp(optarg, "udev"))
805 ctl.output = OUTPUT_UDEV_LIST;
806 else if (!strcmp(optarg, "export"))
807 ctl.output = OUTPUT_EXPORT_LIST;
808 else if (!strcmp(optarg, "json"))
809 ctl.output = OUTPUT_JSON;
810 else if (!strcmp(optarg, "full"))
811 ctl.output = 0;
812 else
813 errx(BLKID_EXIT_OTHER, _("unsupported output format %s"), optarg);
814 break;
815 case 'O':
816 ctl.offset = strtosize_or_err(optarg, _("invalid offset argument"));
817 break;
818 case 'p':
819 ctl.lowprobe_superblocks = 1;
820 break;
821 case 's':
822 if (numtag + 1 >= sizeof(ctl.show) / sizeof(*ctl.show)) {
823 warnx(_("Too many tags specified"));
824 errtryhelp(err);
825 }
826 ctl.show[numtag++] = optarg;
827 break;
828 case 'S':
829 ctl.size = strtosize_or_err(optarg, _("invalid size argument"));
830 break;
831 case 't':
832 if (search_type) {
833 warnx(_("Can only search for "
834 "one NAME=value pair"));
835 errtryhelp(err);
836 }
837 if (blkid_parse_tag_string(optarg,
838 &search_type,
839 &search_value)) {
840 warnx(_("-t needs NAME=value pair"));
841 errtryhelp(err);
842 }
843 break;
844 case 'V':
845 case 'v':
846 fprintf(stdout, _("%s from %s (libblkid %s, %s)\n"),
847 program_invocation_short_name, PACKAGE_STRING,
848 LIBBLKID_VERSION, LIBBLKID_DATE);
849 err = 0;
850 goto exit;
851 case 'w':
852 /* ignore - backward compatibility */
853 break;
854 case 'h':
855 usage();
856 break;
857 default:
858 errtryhelp(EXIT_FAILURE);
859 }
860 }
861
862 if (ctl.lowprobe_topology || ctl.lowprobe_superblocks)
863 ctl.lowprobe = 1;
864
865 /* The rest of the args are device names */
866 if (optind < argc) {
867 devices = xcalloc(argc - optind, sizeof(char *));
868 while (optind < argc) {
869 char *dev = argv[optind++];
870 struct stat sb;
871
872 if (stat(dev, &sb) != 0)
873 continue;
874 else if (S_ISBLK(sb.st_mode))
875 ;
876 else if (S_ISREG(sb.st_mode))
877 ;
878 else if (S_ISCHR(sb.st_mode)) {
879 char buf[PATH_MAX];
880
881 if (!sysfs_chrdev_devno_to_devname(
882 sb.st_rdev, buf, sizeof(buf)))
883 continue;
884 if (strncmp(buf, "ubi", 3) != 0)
885 continue;
886 } else
887 continue;
888
889 devices[numdev++] = dev;
890 }
891
892 if (!numdev) {
893 /* only unsupported devices specified */
894 err = BLKID_EXIT_NOTFOUND;
895 goto exit;
896 }
897 }
898
899 /* convert LABEL/UUID lookup to evaluate request */
900 if (ctl.lookup && ctl.output == OUTPUT_DEVICE_ONLY && search_type &&
901 (!strcmp(search_type, "LABEL") || !strcmp(search_type, "UUID"))) {
902 ctl.eval = 1;
903 ctl.lookup = 0;
904 }
905
906 if (!ctl.lowprobe && !ctl.eval && blkid_get_cache(&cache, read) < 0)
907 goto exit;
908
909 if (ctl.gc) {
910 blkid_gc_cache(cache);
911 err = 0;
912 goto exit;
913 }
914 err = BLKID_EXIT_NOTFOUND;
915
916 if (ctl.eval == 0 && (ctl.output & OUTPUT_PRETTY_LIST)) {
917 if (ctl.lowprobe)
918 errx(BLKID_EXIT_OTHER,
919 _("The low-level probing mode does not "
920 "support 'list' output format"));
921 pretty_print_dev(NULL);
922 }
923
924 if (ctl.lowprobe) {
925 /*
926 * Low-level API
927 */
928 blkid_probe pr;
929
930 if (!numdev)
931 errx(BLKID_EXIT_OTHER,
932 _("The low-level probing mode "
933 "requires a device"));
934
935 /* automatically enable 'export' format for I/O Limits */
936 if (!ctl.output && ctl.lowprobe_topology)
937 ctl.output = OUTPUT_EXPORT_LIST;
938
939 pr = blkid_new_probe();
940 if (!pr)
941 goto exit;
942 if (hint && blkid_probe_set_hint(pr, hint, 0) != 0) {
943 warn(_("Failed to use probing hint: %s"), hint);
944 goto exit;
945 }
946
947 if (ctl.lowprobe_superblocks) {
948 blkid_probe_set_superblocks_flags(pr,
949 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
950 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
951 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION |
952 BLKID_SUBLKS_FSINFO);
953
954 if (fltr_usage &&
955 blkid_probe_filter_superblocks_usage(pr, fltr_flag, fltr_usage))
956 goto exit;
957
958 else if (fltr_type &&
959 blkid_probe_filter_superblocks_type(pr, fltr_flag, fltr_type))
960 goto exit;
961 }
962
963 for (i = 0; i < numdev; i++) {
964 err = lowprobe_device(pr, devices[i], &ctl);
965 if (err)
966 break;
967 }
968 blkid_free_probe(pr);
969 } else if (ctl.eval) {
970 /*
971 * Evaluate API
972 */
973 char *res = blkid_evaluate_tag(search_type, search_value, NULL);
974 if (res) {
975 err = 0;
976 printf("%s\n", res);
977 }
978 } else if (ctl.lookup) {
979 /*
980 * Classic (cache based) API
981 */
982 blkid_dev dev;
983
984 if (!search_type)
985 errx(BLKID_EXIT_OTHER,
986 _("The lookup option requires a "
987 "search type specified using -t"));
988 /* Load any additional devices not in the cache */
989 for (i = 0; i < numdev; i++)
990 blkid_get_dev(cache, devices[i], BLKID_DEV_NORMAL);
991
992 if ((dev = blkid_find_dev_with_tag(cache, search_type,
993 search_value))) {
994 print_tags(&ctl, dev);
995 err = 0;
996 }
997 /* If we didn't specify a single device, show all available devices */
998 } else if (!numdev) {
999 blkid_dev_iterate iter;
1000 blkid_dev dev;
1001
1002 blkid_probe_all(cache);
1003
1004 iter = blkid_dev_iterate_begin(cache);
1005 blkid_dev_set_search(iter, search_type, search_value);
1006 while (blkid_dev_next(iter, &dev) == 0) {
1007 dev = blkid_verify(cache, dev);
1008 if (!dev)
1009 continue;
1010 print_tags(&ctl, dev);
1011 err = 0;
1012 }
1013 blkid_dev_iterate_end(iter);
1014 /* Add all specified devices to cache (optionally display tags) */
1015 } else for (i = 0; i < numdev; i++) {
1016 blkid_dev dev = blkid_get_dev(cache, devices[i],
1017 BLKID_DEV_NORMAL);
1018
1019 if (dev) {
1020 if (search_type &&
1021 !blkid_dev_has_tag(dev, search_type,
1022 search_value))
1023 continue;
1024 print_tags(&ctl, dev);
1025 err = 0;
1026 }
1027 }
1028
1029 exit:
1030 free(search_type);
1031 free(search_value);
1032 free_types_list(fltr_type);
1033 if (!ctl.lowprobe && !ctl.eval)
1034 blkid_put_cache(cache);
1035 free(devices);
1036 return err;
1037 }