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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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),
80 const char *name
; /* header */
81 double whint
; /* width hint (N < 1 is in percent of termwidth) */
82 int flags
; /* tt flags */
83 const char *help
; /* column description */
84 const char *match
; /* pattern for match_func() */
87 /* columns descriptions (don't use const, this is writable) */
88 static struct colinfo infos
[FINDMNT_NCOLUMNS
] = {
89 [COL_SOURCE
] = { "SOURCE", 0.25, TT_FL_NOEXTREMES
, N_("source device") },
90 [COL_TARGET
] = { "TARGET", 0.30, TT_FL_TREE
| TT_FL_NOEXTREMES
, N_("mountpoint") },
91 [COL_FSTYPE
] = { "FSTYPE", 0.10, TT_FL_TRUNC
, N_("filesystem type") },
92 [COL_OPTIONS
] = { "OPTIONS", 0.10, TT_FL_TRUNC
, N_("all mount options") },
93 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC
, N_("VFS specific mount options") },
94 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC
, N_("FS specific mount options") },
95 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
96 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
97 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
98 [COL_ACTION
] = { "ACTION", 10, TT_FL_STRICTWIDTH
, N_("action detected by --poll") },
99 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC
, N_("old mount options saved by --poll") },
100 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
107 /* array with IDs of enabled columns */
108 static int columns
[FINDMNT_NCOLUMNS
];
111 /* poll actions (parsed --poll=<list> */
112 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
113 static int actions
[FINDMNT_NACTIONS
];
117 static struct libmnt_cache
*cache
;
119 static int get_column_id(int num
)
121 assert(num
< ncolumns
);
122 assert(columns
[num
] < FINDMNT_NCOLUMNS
);
126 static struct colinfo
*get_column_info(int num
)
128 return &infos
[ get_column_id(num
) ];
131 static const char *column_id_to_name(int id
)
133 assert(id
< FINDMNT_NCOLUMNS
);
134 return infos
[id
].name
;
137 static const char *get_column_name(int num
)
139 return get_column_info(num
)->name
;
142 static float get_column_whint(int num
)
144 return get_column_info(num
)->whint
;
147 static int get_column_flags(int num
)
149 return get_column_info(num
)->flags
;
152 static const char *get_match(int id
)
154 assert(id
< FINDMNT_NCOLUMNS
);
155 return infos
[id
].match
;
158 static void set_match(int id
, const char *match
)
160 assert(id
< FINDMNT_NCOLUMNS
);
161 infos
[id
].match
= match
;
164 static int is_tabdiff_column(int id
)
166 assert(id
< FINDMNT_NCOLUMNS
);
171 case COL_OLD_OPTIONS
:
180 * "findmnt" without any filter
182 static int is_listall_mode(void)
184 return (!get_match(COL_SOURCE
) &&
185 !get_match(COL_TARGET
) &&
186 !get_match(COL_FSTYPE
) &&
187 !get_match(COL_OPTIONS
));
191 * Returns 1 if the @act is in the --poll=<list>
193 static int has_poll_action(int act
)
198 return 1; /* all actions enabled */
199 for (i
= 0; i
< nactions
; i
++)
200 if (actions
[i
] == act
)
205 static int poll_action_name_to_id(const char *name
, size_t namesz
)
209 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
210 id
= MNT_TABDIFF_MOVE
;
211 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
212 id
= MNT_TABDIFF_MOUNT
;
213 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
214 id
= MNT_TABDIFF_UMOUNT
;
215 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
216 id
= MNT_TABDIFF_REMOUNT
;
218 warnx(_("unknown action: %s"), name
);
224 * findmnt --first-only <devname|TAG=|mountpoint>
226 * ... it works like "mount <devname|TAG=|mountpoint>"
228 static int is_mount_compatible_mode(void)
230 if (!get_match(COL_SOURCE
))
231 return 0; /* <devname|TAG=|mountpoint> is required */
232 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
233 return 0; /* cannot be restricted by -t or -O */
234 if (!(flags
& FL_FIRSTONLY
))
235 return 0; /* we have to return the first entry only */
240 static void disable_columns_truncate(void)
244 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
245 infos
[i
].flags
&= ~TT_FL_TRUNC
;
249 * converts @name to column ID
251 static int column_name_to_id(const char *name
, size_t namesz
)
255 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++) {
256 const char *cn
= column_id_to_name(i
);
258 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
261 warnx(_("unknown column: %s"), name
);
265 /* Returns LABEL or UUID */
266 static const char *get_tag(struct libmnt_fs
*fs
, const char *tagname
)
268 const char *t
, *v
, *res
;
270 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
273 res
= mnt_fs_get_source(fs
);
275 res
= mnt_resolve_spec(res
, cache
);
277 res
= mnt_cache_find_tag_value(cache
, res
, tagname
);
283 /* reads FS data from libmount
284 * TODO: add function that will deallocate data allocated by get_data()
286 static const char *get_data(struct libmnt_fs
*fs
, int num
)
288 const char *str
= NULL
;
290 switch(get_column_id(num
)) {
293 const char *root
= mnt_fs_get_root(fs
);
295 str
= mnt_fs_get_srcpath(fs
);
297 if (str
&& (flags
& FL_CANONICALIZE
))
298 str
= mnt_resolve_path(str
, cache
);
300 str
= mnt_fs_get_source(fs
);
302 if (str
&& (flags
& FL_EVALUATE
))
303 str
= mnt_resolve_spec(str
, cache
);
305 if (root
&& str
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/")) {
308 if (asprintf(&tmp
, "%s[%s]", str
, root
) > 0)
314 str
= mnt_fs_get_target(fs
);
317 str
= mnt_fs_get_fstype(fs
);
320 str
= mnt_fs_get_options(fs
);
322 case COL_VFS_OPTIONS
:
323 str
= mnt_fs_get_vfs_options(fs
);
326 str
= mnt_fs_get_fs_options(fs
);
329 str
= get_tag(fs
, "UUID");
332 str
= get_tag(fs
, "LABEL");
336 dev_t devno
= mnt_fs_get_devno(fs
);
340 if ((tt_flags
& TT_FL_RAW
) || (tt_flags
& TT_FL_EXPORT
))
341 rc
= asprintf(&tmp
, "%u:%u",
342 major(devno
), minor(devno
));
344 rc
= asprintf(&tmp
, "%3u:%-3u",
345 major(devno
), minor(devno
));
356 static const char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
357 struct libmnt_fs
*new_fs
,
361 const char *str
= NULL
;
363 switch (get_column_id(num
)) {
366 case MNT_TABDIFF_MOUNT
:
369 case MNT_TABDIFF_UMOUNT
:
372 case MNT_TABDIFF_REMOUNT
:
375 case MNT_TABDIFF_MOVE
:
383 case COL_OLD_OPTIONS
:
384 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
385 change
== MNT_TABDIFF_UMOUNT
))
386 str
= mnt_fs_get_options(old_fs
);
389 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
390 change
== MNT_TABDIFF_UMOUNT
))
391 str
= mnt_fs_get_target(old_fs
);
395 str
= get_data(new_fs
, num
);
397 str
= get_data(old_fs
, num
);
403 /* adds one line to the output @tab */
404 static struct tt_line
*add_line(struct tt
*tt
, struct libmnt_fs
*fs
,
405 struct tt_line
*parent
)
408 struct tt_line
*line
= tt_add_line(tt
, parent
);
411 warn(_("failed to add line to output"));
414 for (i
= 0; i
< ncolumns
; i
++)
415 tt_line_set_data(line
, i
, get_data(fs
, i
));
417 tt_line_set_userdata(line
, fs
);
421 static struct tt_line
*add_tabdiff_line(struct tt
*tt
, struct libmnt_fs
*new_fs
,
422 struct libmnt_fs
*old_fs
, int change
)
425 struct tt_line
*line
= tt_add_line(tt
, NULL
);
428 warn(_("failed to add line to output"));
431 for (i
= 0; i
< ncolumns
; i
++)
432 tt_line_set_data(line
, i
,
433 get_tabdiff_data(old_fs
, new_fs
, change
, i
));
438 static int has_line(struct tt
*tt
, struct libmnt_fs
*fs
)
442 list_for_each(p
, &tt
->tb_lines
) {
443 struct tt_line
*ln
= list_entry(p
, struct tt_line
, ln_lines
);
444 if ((struct libmnt_fs
*) ln
->userdata
== fs
)
450 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
451 static int create_treenode(struct tt
*tt
, struct libmnt_table
*tb
,
452 struct libmnt_fs
*fs
, struct tt_line
*parent_line
)
454 struct libmnt_fs
*chld
= NULL
;
455 struct libmnt_iter
*itr
= NULL
;
456 struct tt_line
*line
;
460 /* first call, get root FS */
461 if (mnt_table_get_root_fs(tb
, &fs
))
465 } else if ((flags
& FL_SUBMOUNTS
) && has_line(tt
, fs
))
468 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
472 line
= add_line(tt
, fs
, parent_line
);
477 * add all children to the output table
479 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
480 if (create_treenode(tt
, tb
, chld
, line
))
490 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
491 const char *filename
, int line
)
493 warnx(_("%s: parse error at line %d"), filename
, line
);
497 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
499 void *tmp
= realloc(files
, sizeof(char *) * (*nfiles
+ 1));
506 files
[(*nfiles
)++] = filename
;
510 /* calls libmount fstab/mtab/mountinfo parser */
511 static struct libmnt_table
*parse_tabfiles(char **files
,
515 struct libmnt_table
*tb
;
518 tb
= mnt_new_table();
520 warn(_("failed to initialize libmount table"));
523 mnt_table_set_parser_errcb(tb
, parser_errcb
);
526 /* NULL means that libmount will use default paths */
527 const char *path
= nfiles
? *files
++ : NULL
;
531 rc
= mnt_table_parse_fstab(tb
, path
);
534 rc
= mnt_table_parse_mtab(tb
, path
);
538 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
539 _PATH_PROC_MOUNTINFO
:
542 rc
= mnt_table_parse_file(tb
, path
);
547 warn(_("can't read %s"), path
);
550 } while (--nfiles
> 0);
555 /* checks if @tb contains parent->child relations */
556 static int tab_is_tree(struct libmnt_table
*tb
)
558 struct libmnt_fs
*fs
= NULL
;
559 struct libmnt_iter
*itr
= NULL
;
562 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
566 if (mnt_table_next_fs(tb
, itr
, &fs
) == 0)
567 rc
= mnt_fs_get_id(fs
) > 0 && mnt_fs_get_parent_id(fs
) > 0;
573 /* filter function for libmount (mnt_table_find_next_fs()) */
574 static int match_func(struct libmnt_fs
*fs
,
575 void *data
__attribute__ ((__unused__
)))
577 int rc
= flags
& FL_INVERT
? 1 : 0;
580 m
= get_match(COL_TARGET
);
581 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
584 m
= get_match(COL_SOURCE
);
585 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
588 m
= get_match(COL_FSTYPE
);
589 if (m
&& !mnt_fs_match_fstype(fs
, m
))
592 m
= get_match(COL_OPTIONS
);
593 if (m
&& !mnt_fs_match_options(fs
, m
))
599 /* iterate over filesystems in @tb */
600 static struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
601 struct libmnt_iter
*itr
)
603 struct libmnt_fs
*fs
= NULL
;
605 if (is_listall_mode()) {
609 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
612 } else if (is_mount_compatible_mode()) {
614 * Look up for FS in the same way how mount(8) searchs in fstab
618 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
619 mnt_iter_get_direction(itr
));
621 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
622 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
623 mnt_iter_get_direction(itr
));
626 * Look up for all matching entries
628 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
629 * findmnt [-l] <spec> [-O <options>] [-t <types>]
632 mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
);
635 !(flags
& FL_NOSWAPMATCH
) &&
636 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
638 /* swap 'spec' and target. */
639 set_match(COL_TARGET
, get_match(COL_SOURCE
));
640 set_match(COL_SOURCE
, NULL
);
641 mnt_reset_iter(itr
, -1);
650 static int add_matching_lines(struct libmnt_table
*tb
,
651 struct tt
*tt
, int direction
)
653 struct libmnt_iter
*itr
= NULL
;
654 struct libmnt_fs
*fs
;
655 int nlines
= 0, rc
= -1;
657 itr
= mnt_new_iter(direction
);
659 warn(_("failed to initialize libmount iterator"));
663 while((fs
= get_next_fs(tb
, itr
))) {
664 if ((tt_flags
& TT_FL_TREE
) || (flags
& FL_SUBMOUNTS
))
665 rc
= create_treenode(tt
, tb
, fs
, NULL
);
667 rc
= !add_line(tt
, fs
, NULL
);
671 if (flags
& FL_FIRSTONLY
)
673 flags
|= FL_NOSWAPMATCH
;
683 static int poll_match(struct libmnt_fs
*fs
)
685 int rc
= match_func(fs
, NULL
);
687 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
688 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
690 * findmnt --poll /foo
691 * The '/foo' source as well as target.
693 const char *str
= get_match(COL_SOURCE
);
695 set_match(COL_TARGET
, str
); /* swap */
696 set_match(COL_SOURCE
, NULL
);
698 rc
= match_func(fs
, NULL
);
700 set_match(COL_TARGET
, NULL
); /* restore */
701 set_match(COL_SOURCE
, str
);
707 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
708 int timeout
, struct tt
*tt
, int direction
)
712 struct libmnt_iter
*itr
= NULL
;
713 struct libmnt_table
*tb_new
= NULL
;
714 struct libmnt_tabdiff
*diff
= NULL
;
715 struct pollfd fds
[1];
717 tb_new
= mnt_new_table();
719 warn(_("failed to initialize libmount table"));
723 itr
= mnt_new_iter(direction
);
725 warn(_("failed to initialize libmount iterator"));
729 diff
= mnt_new_tabdiff();
731 warn(_("failed to initialize libmount tabdiff"));
735 /* cache is unnecessary to detect changes */
736 mnt_table_set_cache(tb
, NULL
);
737 mnt_table_set_cache(tb_new
, NULL
);
739 f
= fopen(tabfile
, "r");
741 warn(_("%s: open failed"), tabfile
);
745 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
747 fds
[0].fd
= fileno(f
);
748 fds
[0].events
= POLLPRI
;
751 struct libmnt_table
*tmp
;
752 struct libmnt_fs
*old
, *new;
755 count
= poll(fds
, 1, timeout
);
759 warn(_("poll() failed"));
764 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
766 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
771 mnt_reset_iter(itr
, direction
);
772 while(mnt_tabdiff_next_change(
773 diff
, itr
, &old
, &new, &change
) == 0) {
775 if (!has_poll_action(change
))
777 if (!poll_match(new ? new : old
))
780 rc
= !add_tabdiff_line(tt
, new, old
, change
);
783 if (flags
& FL_FIRSTONLY
)
788 rc
= tt_print_table(tt
);
799 mnt_reset_table(tb_new
);
801 if (count
&& (flags
& FL_FIRSTONLY
))
807 mnt_free_table(tb_new
);
808 mnt_free_tabdiff(diff
);
815 static void __attribute__((__noreturn__
)) usage(FILE *out
)
822 " %1$s [options] <device> | <mountpoint>\n"
823 " %1$s [options] <device> <mountpoint>\n"
824 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
825 program_invocation_short_name
);
829 " -s, --fstab search in static table of filesystems\n"
830 " -m, --mtab search in table of mounted filesystems\n"
831 " -k, --kernel search in kernel table of mounted\n"
832 " filesystems (default)\n\n"));
835 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
836 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
839 " -a, --ascii use ASCII chars for tree formatting\n"
840 " -c, --canonicalize canonicalize printed paths\n"
841 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
842 " -e, --evaluate convert tags (LABEL/UUID) to device names\n"
843 " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n"
844 " -f, --first-only print the first found filesystem only\n"));
847 " -h, --help display this help text and exit\n"
848 " -i, --invert invert the sense of matching\n"
849 " -l, --list use list format output\n"
850 " -n, --noheadings don't print column headings\n"
851 " -u, --notruncate don't truncate text in columns\n"));
853 " -O, --options <list> limit the set of filesystems by mount options\n"
854 " -o, --output <list> the output columns to be shown\n"
855 " -P, --pairs use key=\"value\" output format\n"
856 " -r, --raw use raw output format\n"
857 " -t, --types <list> limit the set of filesystems by FS types\n"));
859 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
860 " -R, --submounts print all submounts for the matching filesystems\n"
861 " -S, --source <string> the device to mount (by name, LABEL= or UUID=)\n"
862 " -T, --target <string> the mountpoint to use\n\n"));
865 fprintf(out
, _("\nAvailable columns:\n"));
867 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
868 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
871 fprintf(out
, _("\nFor more information see findmnt(1).\n"));
873 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
876 static void __attribute__((__noreturn__
))
877 errx_mutually_exclusive(const char *opts
)
879 errx(EXIT_FAILURE
, "%s %s", opts
, _("options are mutually exclusive"));
882 int main(int argc
, char *argv
[])
884 struct libmnt_table
*tb
= NULL
;
885 char **tabfiles
= NULL
;
886 int direction
= MNT_ITER_FORWARD
;
887 int i
, c
, rc
= -1, timeout
= -1;
888 int ntabfiles
= 0, tabtype
= 0;
891 struct tt
*tt
= NULL
;
893 static const struct option longopts
[] = {
894 { "ascii", 0, 0, 'a' },
895 { "canonicalize", 0, 0, 'c' },
896 { "direction", 1, 0, 'd' },
897 { "evaluate", 0, 0, 'e' },
898 { "first-only", 0, 0, 'f' },
899 { "fstab", 0, 0, 's' },
900 { "help", 0, 0, 'h' },
901 { "invert", 0, 0, 'i' },
902 { "kernel", 0, 0, 'k' },
903 { "list", 0, 0, 'l' },
904 { "mtab", 0, 0, 'm' },
905 { "noheadings", 0, 0, 'n' },
906 { "notruncate", 0, 0, 'u' },
907 { "options", 1, 0, 'O' },
908 { "output", 1, 0, 'o' },
909 { "poll", 2, 0, 'p' },
910 { "pairs", 0, 0, 'P' },
911 { "raw", 0, 0, 'r' },
912 { "types", 1, 0, 't' },
913 { "fsroot", 0, 0, 'v' },
914 { "submounts", 0, 0, 'R' },
915 { "source", 1, 0, 'S' },
916 { "tab-file", 1, 0, 'F' },
917 { "target", 1, 0, 'T' },
918 { "timeout", 1, 0, 'w' },
923 assert(ARRAY_SIZE(columns
) == FINDMNT_NCOLUMNS
);
925 setlocale(LC_ALL
, "");
926 bindtextdomain(PACKAGE
, LOCALEDIR
);
929 /* default output format */
930 tt_flags
|= TT_FL_TREE
;
932 while ((c
= getopt_long(argc
, argv
,
933 "acd:ehifF:o:O:p::Pklmnrst:uvRS:T:w:",
934 longopts
, NULL
)) != -1) {
937 tt_flags
|= TT_FL_ASCII
;
940 flags
|= FL_CANONICALIZE
;
943 if (!strcmp(optarg
, "forward"))
944 direction
= MNT_ITER_FORWARD
;
945 else if (!strcmp(optarg
, "backward"))
946 direction
= MNT_ITER_BACKWARD
;
949 _("unknown direction '%s'"), optarg
);
952 flags
|= FL_EVALUATE
;
961 flags
|= FL_FIRSTONLY
;
964 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
967 disable_columns_truncate();
970 ncolumns
= string_to_idarray(optarg
,
971 columns
, ARRAY_SIZE(columns
),
977 set_match(COL_OPTIONS
, optarg
);
981 nactions
= string_to_idarray(optarg
,
982 actions
, ARRAY_SIZE(actions
),
983 poll_action_name_to_id
);
988 tt_flags
&= ~TT_FL_TREE
;
991 tt_flags
|= TT_FL_EXPORT
;
992 tt_flags
&= ~TT_FL_TREE
;
996 errx_mutually_exclusive("--{fstab,mtab,kernel}");
997 tabtype
= TABTYPE_MTAB
;
998 tt_flags
&= ~TT_FL_TREE
;
1000 case 's': /* fstab */
1002 errx_mutually_exclusive("--{fstab,mtab,kernel}");
1003 tabtype
= TABTYPE_FSTAB
;
1004 tt_flags
&= ~TT_FL_TREE
;
1006 case 'k': /* kernel (mountinfo) */
1008 errx_mutually_exclusive("--{fstab,mtab,kernel}");
1009 tabtype
= TABTYPE_KERNEL
;
1012 set_match(COL_FSTYPE
, optarg
);
1015 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1016 tt_flags
|= TT_FL_RAW
; /* enable raw */
1019 if ((tt_flags
& TT_FL_RAW
) && (tt_flags
& TT_FL_EXPORT
))
1020 errx_mutually_exclusive("--{raw,list,pairs}");
1022 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1025 tt_flags
|= TT_FL_NOHEADINGS
;
1028 flags
|= FL_NOFSROOT
;
1031 flags
|= FL_SUBMOUNTS
;
1034 set_match(COL_SOURCE
, optarg
);
1035 flags
|= FL_NOSWAPMATCH
;
1038 set_match(COL_TARGET
, optarg
);
1039 flags
|= FL_NOSWAPMATCH
;
1042 timeout
= strtol_or_err(optarg
,
1043 _("failed to parse timeout"));
1051 /* default columns */
1053 if (flags
& FL_POLL
)
1054 columns
[ncolumns
++] = COL_ACTION
;
1056 columns
[ncolumns
++] = COL_TARGET
;
1057 columns
[ncolumns
++] = COL_SOURCE
;
1058 columns
[ncolumns
++] = COL_FSTYPE
;
1059 columns
[ncolumns
++] = COL_OPTIONS
;
1063 tabtype
= TABTYPE_KERNEL
;
1066 if (flags
& FL_POLL
) {
1067 if (tabtype
!= TABTYPE_KERNEL
)
1068 errx_mutually_exclusive("--{poll,fstab,mtab}");
1070 errx(EXIT_FAILURE
, _("--poll accepts only one file, but mure specified by --tab-file"));
1073 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1074 errx(EXIT_FAILURE
, _(
1075 "options --target and --source can't be used together "
1076 "with command line element that is not an option"));
1079 set_match(COL_SOURCE
, argv
[optind
++]); /* dev/tag/mountpoint */
1081 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1083 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1084 /* don't care about submounts if list all mounts */
1085 flags
&= ~FL_SUBMOUNTS
;
1087 if (!(flags
& FL_SUBMOUNTS
) &&
1088 (!is_listall_mode() || (flags
& FL_FIRSTONLY
)))
1089 tt_flags
&= ~TT_FL_TREE
;
1091 if (!(flags
& FL_NOSWAPMATCH
) &&
1092 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1094 * Check if we can swap source and target, it's
1095 * not possible if the source is LABEL=/UUID=
1097 const char *x
= get_match(COL_SOURCE
);
1099 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5))
1100 flags
|= FL_NOSWAPMATCH
;
1104 * initialize libmount
1108 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1112 if ((tt_flags
& TT_FL_TREE
) && !tab_is_tree(tb
))
1113 tt_flags
&= ~TT_FL_TREE
;
1115 cache
= mnt_new_cache();
1117 warn(_("failed to initialize libmount cache"));
1120 mnt_table_set_cache(tb
, cache
);
1123 * initialize output formatting (tt.h)
1125 tt
= tt_new_table(tt_flags
);
1127 warn(_("failed to initialize output table"));
1131 for (i
= 0; i
< ncolumns
; i
++) {
1132 int fl
= get_column_flags(i
);
1133 int id
= get_column_id(i
);
1135 if (!(tt_flags
& TT_FL_TREE
))
1138 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1139 warnx(_("%s column is requested, but --poll "
1140 "is not enabled"), get_column_name(i
));
1143 if (!tt_define_column(tt
, get_column_name(i
),
1144 get_column_whint(i
), fl
)) {
1145 warn(_("failed to initialize output column"));
1151 * Fill in data to the output table
1153 if (flags
& FL_POLL
) {
1154 /* poll mode (accept the first tabfile only) */
1155 if (tabfiles
&& ntabfiles
> 0)
1156 rc
= poll_table(tb
, *tabfiles
, timeout
, tt
, direction
);
1158 } else if ((tt_flags
& TT_FL_TREE
) && is_listall_mode())
1160 rc
= create_treenode(tt
, tb
, NULL
, NULL
);
1162 /* whole lits of sub-tree */
1163 rc
= add_matching_lines(tb
, tt
, direction
);
1166 * Print the output table for non-poll modes
1168 if (!rc
&& !(flags
& FL_POLL
))
1174 mnt_free_cache(cache
);
1177 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;