4 * Copyright (C) 2010,2011 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
36 #include "pathnames.h"
44 FL_EVALUATE = (1 << 1),
45 FL_CANONICALIZE = (1 << 2),
46 FL_FIRSTONLY = (1 << 3),
48 FL_NOSWAPMATCH = (1 << 6),
49 FL_NOFSROOT = (1 << 7),
50 FL_SUBMOUNTS = (1 << 8),
74 const char *name; /* header */
75 double whint; /* width hint (N < 1 is in percent of termwidth) */
76 int flags; /* tt flags */
77 const char *help; /* column description */
78 const char *match; /* pattern for match_func() */
81 /* columns descriptions (don't use const, this is writable) */
82 static struct colinfo infos[FINDMNT_NCOLUMNS] = {
83 [COL_SOURCE] = { "SOURCE", 0.25, 0, N_("source device") },
84 [COL_TARGET] = { "TARGET", 0.30, TT_FL_TREE, N_("mountpoint") },
85 [COL_FSTYPE] = { "FSTYPE", 0.10, TT_FL_TRUNC, N_("filesystem type") },
86 [COL_OPTIONS] = { "OPTIONS", 0.10, TT_FL_TRUNC, N_("all mount options") },
87 [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC, N_("VFS specific mount options") },
88 [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC, N_("FS specific mount options") },
89 [COL_LABEL] = { "LABEL", 0.10, 0, N_("filesystem label") },
90 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
91 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
92 [COL_ACTION] = { "ACTION", 10, TT_FL_STRICTWIDTH, N_("action detected by --poll") },
93 [COL_OLD_OPTIONS] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC, N_("old mount options saved by --poll") },
94 [COL_OLD_TARGET] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
101 /* array with IDs of enabled columns */
102 static int columns[FINDMNT_NCOLUMNS];
105 /* poll actions (parsed --poll=<list> */
106 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
107 static int actions[FINDMNT_NACTIONS];
111 static struct libmnt_cache *cache;
113 static int get_column_id(int num)
115 assert(num < ncolumns);
116 assert(columns[num] < FINDMNT_NCOLUMNS);
120 static struct colinfo *get_column_info(int num)
122 return &infos[ get_column_id(num) ];
125 static const char *column_id_to_name(int id)
127 assert(id < FINDMNT_NCOLUMNS);
128 return infos[id].name;
131 static const char *get_column_name(int num)
133 return get_column_info(num)->name;
136 static float get_column_whint(int num)
138 return get_column_info(num)->whint;
141 static int get_column_flags(int num)
143 return get_column_info(num)->flags;
146 static const char *get_match(int id)
148 assert(id < FINDMNT_NCOLUMNS);
149 return infos[id].match;
152 static void set_match(int id, const char *match)
154 assert(id < FINDMNT_NCOLUMNS);
155 infos[id].match = match;
158 static int is_tabdiff_column(int id)
160 assert(id < FINDMNT_NCOLUMNS);
165 case COL_OLD_OPTIONS:
174 * "findmnt" without any filter
176 static int is_listall_mode(void)
178 return (!get_match(COL_SOURCE) &&
179 !get_match(COL_TARGET) &&
180 !get_match(COL_FSTYPE) &&
181 !get_match(COL_OPTIONS));
185 * Returns 1 if the @act is in the --poll=<list>
187 static int has_poll_action(int act)
192 return 1; /* all actions enabled */
193 for (i = 0; i < nactions; i++)
194 if (actions[i] == act)
199 static int poll_action_name_to_id(const char *name, size_t namesz)
203 if (strncasecmp(name, "move", namesz) == 0 && namesz == 4)
204 id = MNT_TABDIFF_MOVE;
205 else if (strncasecmp(name, "mount", namesz) == 0 && namesz == 5)
206 id = MNT_TABDIFF_MOUNT;
207 else if (strncasecmp(name, "umount", namesz) == 0 && namesz == 6)
208 id = MNT_TABDIFF_UMOUNT;
209 else if (strncasecmp(name, "remount", namesz) == 0 && namesz == 7)
210 id = MNT_TABDIFF_REMOUNT;
212 warnx(_("unknown action: %s"), name);
218 * findmnt --first-only <devname|TAG=|mountpoint>
220 * ... it works like "mount <devname|TAG=|mountpoint>"
222 static int is_mount_compatible_mode(void)
224 if (!get_match(COL_SOURCE))
225 return 0; /* <devname|TAG=|mountpoint> is required */
226 if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS))
227 return 0; /* cannot be restricted by -t or -O */
228 if (!(flags & FL_FIRSTONLY))
229 return 0; /* we have to return the first entry only */
234 static void disable_columns_truncate(void)
238 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
239 infos[i].flags &= ~TT_FL_TRUNC;
243 * converts @name to column ID
245 static int column_name_to_id(const char *name, size_t namesz)
249 for (i = 0; i < FINDMNT_NCOLUMNS; i++) {
250 const char *cn = column_id_to_name(i);
252 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
255 warnx(_("unknown column: %s"), name);
259 /* Returns LABEL or UUID */
260 static const char *get_tag(struct libmnt_fs *fs, const char *tagname)
262 const char *t, *v, *res;
264 if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname))
267 res = mnt_fs_get_source(fs);
269 res = mnt_resolve_spec(res, cache);
271 res = mnt_cache_find_tag_value(cache, res, tagname);
277 /* reads FS data from libmount
278 * TODO: add function that will deallocate data allocated by get_data()
280 static const char *get_data(struct libmnt_fs *fs, int num)
282 const char *str = NULL;
284 switch(get_column_id(num)) {
287 const char *root = mnt_fs_get_root(fs);
289 str = mnt_fs_get_srcpath(fs);
291 if (str && (flags & FL_CANONICALIZE))
292 str = mnt_resolve_path(str, cache);
294 str = mnt_fs_get_source(fs);
296 if (str && (flags & FL_EVALUATE))
297 str = mnt_resolve_spec(str, cache);
299 if (root && str && !(flags & FL_NOFSROOT) && strcmp(root, "/")) {
302 if (asprintf(&tmp, "%s[%s]", str, root) > 0)
308 str = mnt_fs_get_target(fs);
311 str = mnt_fs_get_fstype(fs);
314 str = mnt_fs_get_options(fs);
316 case COL_VFS_OPTIONS:
317 str = mnt_fs_get_vfs_options(fs);
320 str = mnt_fs_get_fs_options(fs);
323 str = get_tag(fs, "UUID");
326 str = get_tag(fs, "LABEL");
330 dev_t devno = mnt_fs_get_devno(fs);
334 if ((tt_flags & TT_FL_RAW) || (tt_flags & TT_FL_EXPORT))
335 rc = asprintf(&tmp, "%u:%u",
336 major(devno), minor(devno));
338 rc = asprintf(&tmp, "%3u:%-3u",
339 major(devno), minor(devno));
350 static const char *get_tabdiff_data(struct libmnt_fs *old_fs,
351 struct libmnt_fs *new_fs,
355 const char *str = NULL;
357 switch (get_column_id(num)) {
360 case MNT_TABDIFF_MOUNT:
363 case MNT_TABDIFF_UMOUNT:
366 case MNT_TABDIFF_REMOUNT:
369 case MNT_TABDIFF_MOVE:
377 case COL_OLD_OPTIONS:
378 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
379 change == MNT_TABDIFF_UMOUNT))
380 str = mnt_fs_get_options(old_fs);
383 if (old_fs && (change == MNT_TABDIFF_MOVE ||
384 change == MNT_TABDIFF_UMOUNT))
385 str = mnt_fs_get_target(old_fs);
389 str = get_data(new_fs, num);
391 str = get_data(old_fs, num);
397 /* adds one line to the output @tab */
398 static struct tt_line *add_line(struct tt *tt, struct libmnt_fs *fs,
399 struct tt_line *parent)
402 struct tt_line *line = tt_add_line(tt, parent);
405 warn(_("failed to add line to output"));
408 for (i = 0; i < ncolumns; i++)
409 tt_line_set_data(line, i, get_data(fs, i));
411 tt_line_set_userdata(line, fs);
415 static struct tt_line *add_tabdiff_line(struct tt *tt, struct libmnt_fs *new_fs,
416 struct libmnt_fs *old_fs, int change)
419 struct tt_line *line = tt_add_line(tt, NULL);
422 warn(_("failed to add line to output"));
425 for (i = 0; i < ncolumns; i++)
426 tt_line_set_data(line, i,
427 get_tabdiff_data(old_fs, new_fs, change, i));
432 static int has_line(struct tt *tt, struct libmnt_fs *fs)
436 list_for_each(p, &tt->tb_lines) {
437 struct tt_line *ln = list_entry(p, struct tt_line, ln_lines);
438 if ((struct libmnt_fs *) ln->userdata == fs)
444 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
445 static int create_treenode(struct tt *tt, struct libmnt_table *tb,
446 struct libmnt_fs *fs, struct tt_line *parent_line)
448 struct libmnt_fs *chld = NULL;
449 struct libmnt_iter *itr = NULL;
450 struct tt_line *line;
454 /* first call, get root FS */
455 if (mnt_table_get_root_fs(tb, &fs))
459 } else if ((flags & FL_SUBMOUNTS) && has_line(tt, fs))
462 itr = mnt_new_iter(MNT_ITER_FORWARD);
466 line = add_line(tt, fs, parent_line);
471 * add all children to the output table
473 while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
474 if (create_treenode(tt, tb, chld, line))
484 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
485 const char *filename, int line)
487 warn(_("%s: parse error at line %d"), filename, line);
491 /* calls libmount fstab/mtab/mountinfo parser */
492 static struct libmnt_table *parse_tabfile(const char *path)
495 struct libmnt_table *tb = mnt_new_table();
498 warn(_("failed to initialize libmount table"));
502 mnt_table_set_parser_errcb(tb, parser_errcb);
504 if (!strcmp(path, _PATH_MNTTAB))
505 rc = mnt_table_parse_fstab(tb, NULL);
506 else if (!strcmp(path, _PATH_MOUNTED))
507 rc = mnt_table_parse_mtab(tb, NULL);
509 rc = mnt_table_parse_file(tb, path);
513 warn(_("can't read %s"), path);
519 /* filter function for libmount (mnt_table_find_next_fs()) */
520 static int match_func(struct libmnt_fs *fs,
521 void *data __attribute__ ((__unused__)))
523 int rc = flags & FL_INVERT ? 1 : 0;
526 m = get_match(COL_TARGET);
527 if (m && !mnt_fs_match_target(fs, m, cache))
530 m = get_match(COL_SOURCE);
531 if (m && !mnt_fs_match_source(fs, m, cache))
534 m = get_match(COL_FSTYPE);
535 if (m && !mnt_fs_match_fstype(fs, m))
538 m = get_match(COL_OPTIONS);
539 if (m && !mnt_fs_match_options(fs, m))
545 /* iterate over filesystems in @tb */
546 static struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
547 struct libmnt_iter *itr)
549 struct libmnt_fs *fs = NULL;
551 if (is_listall_mode()) {
555 if (mnt_table_next_fs(tb, itr, &fs) != 0)
558 } else if (is_mount_compatible_mode()) {
560 * Look up for FS in the same way how mount(8) searchs in fstab
564 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
565 mnt_iter_get_direction(itr));
567 if (!fs && !(flags & FL_NOSWAPMATCH))
568 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
569 mnt_iter_get_direction(itr));
572 * Look up for all matching entries
574 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
575 * findmnt [-l] <spec> [-O <options>] [-t <types>]
578 mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs);
581 !(flags & FL_NOSWAPMATCH) &&
582 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
584 /* swap 'spec' and target. */
585 set_match(COL_TARGET, get_match(COL_SOURCE));
586 set_match(COL_SOURCE, NULL);
587 mnt_reset_iter(itr, -1);
596 static int add_matching_lines(struct libmnt_table *tb,
597 struct tt *tt, int direction)
599 struct libmnt_iter *itr = NULL;
600 struct libmnt_fs *fs;
601 int nlines = 0, rc = -1;
603 itr = mnt_new_iter(direction);
605 warn(_("failed to initialize libmount iterator"));
609 while((fs = get_next_fs(tb, itr))) {
610 if ((tt_flags & TT_FL_TREE) || (flags & FL_SUBMOUNTS))
611 rc = create_treenode(tt, tb, fs, NULL);
613 rc = !add_line(tt, fs, NULL);
617 if (flags & FL_FIRSTONLY)
619 flags |= FL_NOSWAPMATCH;
629 static int poll_match(struct libmnt_fs *fs)
631 int rc = match_func(fs, NULL);
633 if (rc == 0 && !(flags & FL_NOSWAPMATCH) &&
634 get_match(COL_SOURCE) && !get_match(COL_TARGET)) {
636 * findmnt --poll /foo
637 * The '/foo' source as well as target.
639 const char *str = get_match(COL_SOURCE);
641 set_match(COL_TARGET, str); /* swap */
642 set_match(COL_SOURCE, NULL);
644 rc = match_func(fs, NULL);
646 set_match(COL_TARGET, NULL); /* restore */
647 set_match(COL_SOURCE, str);
653 static int poll_table(struct libmnt_table *tb, const char *tabfile,
654 int timeout, struct tt *tt, int direction)
658 struct libmnt_iter *itr = NULL;
659 struct libmnt_table *tb_new = NULL;
660 struct libmnt_tabdiff *diff = NULL;
661 struct pollfd fds[1];
663 tb_new = mnt_new_table();
665 warn(_("failed to initialize libmount table"));
669 itr = mnt_new_iter(direction);
671 warn(_("failed to initialize libmount iterator"));
675 diff = mnt_new_tabdiff();
677 warn(_("failed to initialize libmount tabdiff"));
681 /* cache is unnecessary to detect changes */
682 mnt_table_set_cache(tb, NULL);
683 mnt_table_set_cache(tb_new, NULL);
685 f = fopen(tabfile, "r");
687 warn(_("%s: open failed"), tabfile);
691 mnt_table_set_parser_errcb(tb_new, parser_errcb);
693 fds[0].fd = fileno(f);
694 fds[0].events = POLLPRI;
697 struct libmnt_table *tmp;
698 struct libmnt_fs *old, *new;
701 count = poll(fds, 1, timeout);
705 warn(_("poll() failed"));
710 rc = mnt_table_parse_stream(tb_new, f, tabfile);
712 rc = mnt_diff_tables(diff, tb, tb_new);
717 mnt_reset_iter(itr, direction);
718 while(mnt_tabdiff_next_change(
719 diff, itr, &old, &new, &change) == 0) {
721 if (!has_poll_action(change))
723 if (!poll_match(new ? new : old))
726 rc = !add_tabdiff_line(tt, new, old, change);
729 if (flags & FL_FIRSTONLY)
734 rc = tt_print_table(tt);
745 mnt_reset_table(tb_new);
747 if (count && (flags & FL_FIRSTONLY))
753 mnt_free_table(tb_new);
754 mnt_free_tabdiff(diff);
759 static void __attribute__((__noreturn__)) usage(FILE *out)
766 " %1$s [options] <device> | <mountpoint>\n"
767 " %1$s [options] <device> <mountpoint>\n"
768 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
769 program_invocation_short_name);
773 " -s, --fstab search in static table of filesystems\n"
774 " -m, --mtab search in table of mounted filesystems\n"
775 " -k, --kernel search in kernel table of mounted\n"
776 " filesystems (default)\n\n"));
779 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
780 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
783 " -a, --ascii use ASCII chars for tree formatting\n"
784 " -c, --canonicalize canonicalize printed paths\n"
785 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
786 " -e, --evaluate convert tags (LABEL/UUID) to device names\n"
787 " -f, --first-only print the first found filesystem only\n"));
790 " -h, --help display this help text and exit\n"
791 " -i, --invert invert the sense of matching\n"
792 " -l, --list use list format output\n"
793 " -n, --noheadings don't print column headings\n"
794 " -u, --notruncate don't truncate text in columns\n"));
796 " -O, --options <list> limit the set of filesystems by mount options\n"
797 " -o, --output <list> the output columns to be shown\n"
798 " -P, --pairs use key=\"value\" output format\n"
799 " -r, --raw use raw output format\n"
800 " -t, --types <list> limit the set of filesystems by FS types\n"));
802 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
803 " -R, --submounts print all submounts for the matching filesystems\n"
804 " -S, --source <string> the device to mount (by name, LABEL= or UUID=)\n"
805 " -T, --target <string> the mountpoint to use\n\n"));
808 fprintf(out, _("\nAvailable columns:\n"));
810 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
811 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
814 fprintf(out, _("\nFor more information see findmnt(1).\n"));
816 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
819 static void __attribute__((__noreturn__))
820 errx_mutually_exclusive(const char *opts)
822 errx(EXIT_FAILURE, "%s %s", opts, _("options are mutually exclusive"));
825 int main(int argc, char *argv[])
828 struct libmnt_table *tb = NULL;
829 char *tabfile = NULL;
830 int direction = MNT_ITER_FORWARD;
831 int i, c, rc = -1, timeout = -1;
834 struct tt *tt = NULL;
836 static const struct option longopts[] = {
837 { "ascii", 0, 0, 'a' },
838 { "canonicalize", 0, 0, 'c' },
839 { "direction", 1, 0, 'd' },
840 { "evaluate", 0, 0, 'e' },
841 { "first-only", 0, 0, 'f' },
842 { "fstab", 0, 0, 's' },
843 { "help", 0, 0, 'h' },
844 { "invert", 0, 0, 'i' },
845 { "kernel", 0, 0, 'k' },
846 { "list", 0, 0, 'l' },
847 { "mtab", 0, 0, 'm' },
848 { "noheadings", 0, 0, 'n' },
849 { "notruncate", 0, 0, 'u' },
850 { "options", 1, 0, 'O' },
851 { "output", 1, 0, 'o' },
852 { "poll", 2, 0, 'p' },
853 { "pairs", 0, 0, 'P' },
854 { "raw", 0, 0, 'r' },
855 { "types", 1, 0, 't' },
856 { "fsroot", 0, 0, 'v' },
857 { "submounts", 0, 0, 'R' },
858 { "source", 1, 0, 'S' },
859 { "target", 1, 0, 'T' },
860 { "timeout", 1, 0, 'w' },
865 assert(ARRAY_SIZE(columns) == FINDMNT_NCOLUMNS);
867 setlocale(LC_ALL, "");
868 bindtextdomain(PACKAGE, LOCALEDIR);
871 /* default output format */
872 tt_flags |= TT_FL_TREE;
874 while ((c = getopt_long(argc, argv,
875 "acd:ehifo:O:p::Pklmnrst:uvRS:T:w:", longopts, NULL)) != -1) {
878 tt_flags |= TT_FL_ASCII;
881 flags |= FL_CANONICALIZE;
884 if (!strcmp(optarg, "forward"))
885 direction = MNT_ITER_FORWARD;
886 else if (!strcmp(optarg, "backward"))
887 direction = MNT_ITER_BACKWARD;
890 _("unknown direction '%s'"), optarg);
893 flags |= FL_EVALUATE;
902 flags |= FL_FIRSTONLY;
905 disable_columns_truncate();
908 ncolumns = string_to_idarray(optarg,
909 columns, ARRAY_SIZE(columns),
915 set_match(COL_OPTIONS, optarg);
919 nactions = string_to_idarray(optarg,
920 actions, ARRAY_SIZE(actions),
921 poll_action_name_to_id);
926 tt_flags &= ~TT_FL_TREE;
929 tt_flags |= TT_FL_EXPORT;
930 tt_flags &= ~TT_FL_TREE;
934 errx_mutually_exclusive("--{fstab,mtab,kernel}");
935 tabfile = _PATH_MOUNTED;
936 tt_flags &= ~TT_FL_TREE;
938 case 's': /* fstab */
940 errx_mutually_exclusive("--{fstab,mtab,kernel}");
941 tabfile = _PATH_MNTTAB;
942 tt_flags &= ~TT_FL_TREE;
944 case 'k': /* kernel (mountinfo) */
946 errx_mutually_exclusive("--{fstab,mtab,kernel}");
947 tabfile = _PATH_PROC_MOUNTINFO;
950 set_match(COL_FSTYPE, optarg);
953 tt_flags &= ~TT_FL_TREE; /* disable the default */
954 tt_flags |= TT_FL_RAW; /* enable raw */
957 if ((tt_flags & TT_FL_RAW) && (tt_flags & TT_FL_EXPORT))
958 errx_mutually_exclusive("--{raw,list,pairs}");
960 tt_flags &= ~TT_FL_TREE; /* disable the default */
963 tt_flags |= TT_FL_NOHEADINGS;
966 flags |= FL_NOFSROOT;
969 flags |= FL_SUBMOUNTS;
972 set_match(COL_SOURCE, optarg);
973 flags |= FL_NOSWAPMATCH;
976 set_match(COL_TARGET, optarg);
977 flags |= FL_NOSWAPMATCH;
980 timeout = strtol_or_err(optarg,
981 _("failed to parse timeout"));
989 /* default columns */
992 columns[ncolumns++] = COL_ACTION;
994 columns[ncolumns++] = COL_TARGET;
995 columns[ncolumns++] = COL_SOURCE;
996 columns[ncolumns++] = COL_FSTYPE;
997 columns[ncolumns++] = COL_OPTIONS;
1001 tabfile = _PATH_PROC_MOUNTINFO;
1003 if (access(tabfile, R_OK)) { /* old kernel? */
1004 tabfile = _PATH_PROC_MOUNTS;
1005 tt_flags &= ~TT_FL_TREE;
1009 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
1010 errx(EXIT_FAILURE, _(
1011 "options --target and --source can't be used together "
1012 "with command line element that is not an option"));
1015 set_match(COL_SOURCE, argv[optind++]); /* dev/tag/mountpoint */
1017 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
1019 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
1020 /* don't care about submounts if list all mounts */
1021 flags &= ~FL_SUBMOUNTS;
1023 if (!(flags & FL_SUBMOUNTS) &&
1024 (!is_listall_mode() || (flags & FL_FIRSTONLY)))
1025 tt_flags &= ~TT_FL_TREE;
1027 if (!(flags & FL_NOSWAPMATCH) &&
1028 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1030 * Check if we can swap source and target, it's
1031 * not possible if the source is LABEL=/UUID=
1033 const char *x = get_match(COL_SOURCE);
1035 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5))
1036 flags |= FL_NOSWAPMATCH;
1040 * initialize libmount
1044 tb = parse_tabfile(tabfile);
1048 cache = mnt_new_cache();
1050 warn(_("failed to initialize libmount cache"));
1053 mnt_table_set_cache(tb, cache);
1056 * initialize output formatting (tt.h)
1058 tt = tt_new_table(tt_flags);
1060 warn(_("failed to initialize output table"));
1064 for (i = 0; i < ncolumns; i++) {
1065 int fl = get_column_flags(i);
1066 int id = get_column_id(i);
1068 if (!(tt_flags & TT_FL_TREE))
1071 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
1072 warnx(_("%s column is requested, but --poll "
1073 "is not enabled"), get_column_name(i));
1076 if (!tt_define_column(tt, get_column_name(i),
1077 get_column_whint(i), fl)) {
1078 warn(_("failed to initialize output column"));
1084 * Fill in data to the output table
1086 if (flags & FL_POLL)
1088 rc = poll_table(tb, tabfile, timeout, tt, direction);
1090 else if ((tt_flags & TT_FL_TREE) && is_listall_mode())
1092 rc = create_treenode(tt, tb, NULL, NULL);
1094 /* whole lits of sub-tree */
1095 rc = add_matching_lines(tb, tt, direction);
1098 * Print the output table for non-poll modes
1100 if (!rc && !(flags & FL_POLL))
1106 mnt_free_cache(cache);
1108 return rc ? EXIT_FAILURE : EXIT_SUCCESS;