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>
33 #include <sys/statvfs.h>
34 #include <sys/types.h>
38 #include "pathnames.h"
40 #include "closestream.h"
48 FL_EVALUATE
= (1 << 1),
49 FL_CANONICALIZE
= (1 << 2),
50 FL_FIRSTONLY
= (1 << 3),
52 FL_NOSWAPMATCH
= (1 << 6),
53 FL_NOFSROOT
= (1 << 7),
54 FL_SUBMOUNTS
= (1 << 8),
92 const char *name
; /* header */
93 double whint
; /* width hint (N < 1 is in percent of termwidth) */
94 int flags
; /* tt flags */
95 const char *help
; /* column description */
96 const char *match
; /* pattern for match_func() */
97 void *match_data
; /* match specific data */
100 /* columns descriptions (don't use const, this is writable) */
101 static struct colinfo infos
[FINDMNT_NCOLUMNS
] = {
102 [COL_SOURCE
] = { "SOURCE", 0.25, TT_FL_NOEXTREMES
, N_("source device") },
103 [COL_TARGET
] = { "TARGET", 0.30, TT_FL_TREE
| TT_FL_NOEXTREMES
, N_("mountpoint") },
104 [COL_FSTYPE
] = { "FSTYPE", 0.10, TT_FL_TRUNC
, N_("filesystem type") },
105 [COL_OPTIONS
] = { "OPTIONS", 0.10, TT_FL_TRUNC
, N_("all mount options") },
106 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC
, N_("VFS specific mount options") },
107 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC
, N_("FS specific mount options") },
108 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
109 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
110 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
111 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
112 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
113 [COL_ACTION
] = { "ACTION", 10, TT_FL_STRICTWIDTH
, N_("action detected by --poll") },
114 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC
, N_("old mount options saved by --poll") },
115 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
116 [COL_SIZE
] = { "SIZE", 5, TT_FL_RIGHT
, N_("filesystem size") },
117 [COL_AVAIL
] = { "AVAIL", 5, TT_FL_RIGHT
, N_("filesystem size available") },
118 [COL_USED
] = { "USED", 5, TT_FL_RIGHT
, N_("filesystem size used") },
119 [COL_USEPERC
] = { "USE%", 3, TT_FL_RIGHT
, N_("filesystem use percentage") },
126 /* array with IDs of enabled columns */
127 static int columns
[FINDMNT_NCOLUMNS
];
130 /* poll actions (parsed --poll=<list> */
131 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
132 static int actions
[FINDMNT_NACTIONS
];
136 static struct libmnt_cache
*cache
;
138 static int get_column_id(int num
)
140 assert(num
< ncolumns
);
141 assert(columns
[num
] < FINDMNT_NCOLUMNS
);
145 static struct colinfo
*get_column_info(int num
)
147 return &infos
[ get_column_id(num
) ];
150 static const char *column_id_to_name(int id
)
152 assert(id
< FINDMNT_NCOLUMNS
);
153 return infos
[id
].name
;
156 static const char *get_column_name(int num
)
158 return get_column_info(num
)->name
;
161 static float get_column_whint(int num
)
163 return get_column_info(num
)->whint
;
166 static int get_column_flags(int num
)
168 return get_column_info(num
)->flags
;
171 static const char *get_match(int id
)
173 assert(id
< FINDMNT_NCOLUMNS
);
174 return infos
[id
].match
;
177 static void *get_match_data(int id
)
179 assert(id
< FINDMNT_NCOLUMNS
);
180 return infos
[id
].match_data
;
183 static void set_match(int id
, const char *match
)
185 assert(id
< FINDMNT_NCOLUMNS
);
186 infos
[id
].match
= match
;
189 static void set_match_data(int id
, void *data
)
191 assert(id
< FINDMNT_NCOLUMNS
);
192 infos
[id
].match_data
= data
;
196 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
199 static void set_source_match(const char *data
)
203 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
204 dev_t
*devno
= xmalloc(sizeof(dev_t
));
206 *devno
= makedev(maj
, min
);
207 set_match(COL_MAJMIN
, data
);
208 set_match_data(COL_MAJMIN
, (void *) devno
);
209 flags
|= FL_NOSWAPMATCH
;
211 set_match(COL_SOURCE
, data
);
214 static void enable_extra_target_match(void)
216 char *cn
= NULL
, *mnt
= NULL
;
219 * Check if match pattern is mountpoint, if not use the
222 cn
= mnt_resolve_path(get_match(COL_TARGET
), cache
);
226 mnt
= mnt_get_mountpoint(cn
);
227 if (!mnt
|| strcmp(mnt
, cn
) == 0)
230 /* replace the current setting with the real mountpoint */
231 set_match(COL_TARGET
, mnt
);
235 static int is_tabdiff_column(int id
)
237 assert(id
< FINDMNT_NCOLUMNS
);
242 case COL_OLD_OPTIONS
:
251 * "findmnt" without any filter
253 static int is_listall_mode(void)
255 if ((flags
& FL_DF
) && !(flags
& FL_ALL
))
258 return (!get_match(COL_SOURCE
) &&
259 !get_match(COL_TARGET
) &&
260 !get_match(COL_FSTYPE
) &&
261 !get_match(COL_OPTIONS
) &&
262 !get_match(COL_MAJMIN
));
266 * Returns 1 if the @act is in the --poll=<list>
268 static int has_poll_action(int act
)
273 return 1; /* all actions enabled */
274 for (i
= 0; i
< nactions
; i
++)
275 if (actions
[i
] == act
)
280 static int poll_action_name_to_id(const char *name
, size_t namesz
)
284 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
285 id
= MNT_TABDIFF_MOVE
;
286 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
287 id
= MNT_TABDIFF_MOUNT
;
288 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
289 id
= MNT_TABDIFF_UMOUNT
;
290 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
291 id
= MNT_TABDIFF_REMOUNT
;
293 warnx(_("unknown action: %s"), name
);
299 * findmnt --first-only <devname|TAG=|mountpoint>
301 * ... it works like "mount <devname|TAG=|mountpoint>"
303 static int is_mount_compatible_mode(void)
305 if (!get_match(COL_SOURCE
))
306 return 0; /* <devname|TAG=|mountpoint> is required */
307 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
308 return 0; /* cannot be restricted by -t or -O */
309 if (!(flags
& FL_FIRSTONLY
))
310 return 0; /* we have to return the first entry only */
315 static void disable_columns_truncate(void)
319 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
320 infos
[i
].flags
&= ~TT_FL_TRUNC
;
324 * converts @name to column ID
326 static int column_name_to_id(const char *name
, size_t namesz
)
330 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++) {
331 const char *cn
= column_id_to_name(i
);
333 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
336 warnx(_("unknown column: %s"), name
);
340 /* Returns LABEL or UUID */
341 static const char *get_tag(struct libmnt_fs
*fs
, const char *tagname
)
343 const char *t
, *v
, *res
;
345 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
348 res
= mnt_fs_get_source(fs
);
350 res
= mnt_resolve_spec(res
, cache
);
352 res
= mnt_cache_find_tag_value(cache
, res
, tagname
);
358 static const char *get_vfs_attr(struct libmnt_fs
*fs
, int sizetype
)
361 uint64_t vfs_attr
= 0;
364 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
369 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
372 vfs_attr
= buf
.f_frsize
* buf
.f_bfree
;
375 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
378 if (buf
.f_blocks
== 0)
381 xasprintf(&sizestr
, "%.0f%%",
382 (double)(buf
.f_blocks
- buf
.f_bfree
) /
387 return vfs_attr
== 0 ? "0" :
388 size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
391 /* reads FS data from libmount
393 static const char *get_data(struct libmnt_fs
*fs
, int num
)
395 const char *str
= NULL
;
396 int col_id
= get_column_id(num
);
401 const char *root
= mnt_fs_get_root(fs
);
403 str
= mnt_fs_get_srcpath(fs
);
405 if (str
&& (flags
& FL_CANONICALIZE
))
406 str
= mnt_resolve_path(str
, cache
);
408 str
= mnt_fs_get_source(fs
);
410 if (str
&& (flags
& FL_EVALUATE
))
411 str
= mnt_resolve_spec(str
, cache
);
413 if (root
&& str
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/")) {
416 if (xasprintf(&tmp
, "%s[%s]", str
, root
) > 0)
422 str
= mnt_fs_get_target(fs
);
425 str
= mnt_fs_get_fstype(fs
);
428 str
= mnt_fs_get_options(fs
);
430 case COL_VFS_OPTIONS
:
431 str
= mnt_fs_get_vfs_options(fs
);
434 str
= mnt_fs_get_fs_options(fs
);
437 str
= get_tag(fs
, "UUID");
440 str
= get_tag(fs
, "PARTUUID");
443 str
= get_tag(fs
, "LABEL");
446 str
= get_tag(fs
, "PARTLABEL");
451 dev_t devno
= mnt_fs_get_devno(fs
);
455 if ((tt_flags
& TT_FL_RAW
) || (tt_flags
& TT_FL_EXPORT
))
456 rc
= xasprintf(&tmp
, "%u:%u",
457 major(devno
), minor(devno
));
459 rc
= xasprintf(&tmp
, "%3u:%-3u",
460 major(devno
), minor(devno
));
470 str
= get_vfs_attr(fs
, col_id
);
478 static const char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
479 struct libmnt_fs
*new_fs
,
483 const char *str
= NULL
;
485 switch (get_column_id(num
)) {
488 case MNT_TABDIFF_MOUNT
:
491 case MNT_TABDIFF_UMOUNT
:
494 case MNT_TABDIFF_REMOUNT
:
497 case MNT_TABDIFF_MOVE
:
505 case COL_OLD_OPTIONS
:
506 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
507 change
== MNT_TABDIFF_UMOUNT
))
508 str
= mnt_fs_get_options(old_fs
);
511 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
512 change
== MNT_TABDIFF_UMOUNT
))
513 str
= mnt_fs_get_target(old_fs
);
517 str
= get_data(new_fs
, num
);
519 str
= get_data(old_fs
, num
);
525 /* adds one line to the output @tab */
526 static struct tt_line
*add_line(struct tt
*tt
, struct libmnt_fs
*fs
,
527 struct tt_line
*parent
)
530 struct tt_line
*line
= tt_add_line(tt
, parent
);
533 warn(_("failed to add line to output"));
536 for (i
= 0; i
< ncolumns
; i
++)
537 tt_line_set_data(line
, i
, get_data(fs
, i
));
539 tt_line_set_userdata(line
, fs
);
543 static struct tt_line
*add_tabdiff_line(struct tt
*tt
, struct libmnt_fs
*new_fs
,
544 struct libmnt_fs
*old_fs
, int change
)
547 struct tt_line
*line
= tt_add_line(tt
, NULL
);
550 warn(_("failed to add line to output"));
553 for (i
= 0; i
< ncolumns
; i
++)
554 tt_line_set_data(line
, i
,
555 get_tabdiff_data(old_fs
, new_fs
, change
, i
));
560 static int has_line(struct tt
*tt
, struct libmnt_fs
*fs
)
564 list_for_each(p
, &tt
->tb_lines
) {
565 struct tt_line
*ln
= list_entry(p
, struct tt_line
, ln_lines
);
566 if ((struct libmnt_fs
*) ln
->userdata
== fs
)
572 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
573 static int create_treenode(struct tt
*tt
, struct libmnt_table
*tb
,
574 struct libmnt_fs
*fs
, struct tt_line
*parent_line
)
576 struct libmnt_fs
*chld
= NULL
;
577 struct libmnt_iter
*itr
= NULL
;
578 struct tt_line
*line
;
582 /* first call, get root FS */
583 if (mnt_table_get_root_fs(tb
, &fs
))
587 } else if ((flags
& FL_SUBMOUNTS
) && has_line(tt
, fs
))
590 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
594 line
= add_line(tt
, fs
, parent_line
);
599 * add all children to the output table
601 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
602 if (create_treenode(tt
, tb
, chld
, line
))
612 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
613 const char *filename
, int line
)
615 warnx(_("%s: parse error at line %d"), filename
, line
);
619 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
621 files
= xrealloc(files
, sizeof(char *) * (*nfiles
+ 1));
622 files
[(*nfiles
)++] = filename
;
626 /* calls libmount fstab/mtab/mountinfo parser */
627 static struct libmnt_table
*parse_tabfiles(char **files
,
631 struct libmnt_table
*tb
;
634 tb
= mnt_new_table();
636 warn(_("failed to initialize libmount table"));
639 mnt_table_set_parser_errcb(tb
, parser_errcb
);
642 /* NULL means that libmount will use default paths */
643 const char *path
= nfiles
? *files
++ : NULL
;
647 rc
= mnt_table_parse_fstab(tb
, path
);
650 rc
= mnt_table_parse_mtab(tb
, path
);
654 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
655 _PATH_PROC_MOUNTINFO
:
658 rc
= mnt_table_parse_file(tb
, path
);
663 warn(_("can't read %s"), path
);
666 } while (--nfiles
> 0);
671 /* checks if @tb contains parent->child relations */
672 static int tab_is_tree(struct libmnt_table
*tb
)
674 struct libmnt_fs
*fs
= NULL
;
675 struct libmnt_iter
*itr
= NULL
;
678 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
682 if (mnt_table_next_fs(tb
, itr
, &fs
) == 0)
683 rc
= mnt_fs_get_id(fs
) > 0 && mnt_fs_get_parent_id(fs
) > 0;
690 /* filter function for libmount (mnt_table_find_next_fs()) */
691 static int match_func(struct libmnt_fs
*fs
,
692 void *data
__attribute__ ((__unused__
)))
694 int rc
= flags
& FL_INVERT
? 1 : 0;
698 m
= get_match(COL_TARGET
);
699 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
702 m
= get_match(COL_SOURCE
);
703 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
706 m
= get_match(COL_FSTYPE
);
707 if (m
&& !mnt_fs_match_fstype(fs
, m
))
710 m
= get_match(COL_OPTIONS
);
711 if (m
&& !mnt_fs_match_options(fs
, m
))
714 md
= get_match_data(COL_MAJMIN
);
715 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
718 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
719 const char *type
= mnt_fs_get_fstype(fs
);
721 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
724 if (mnt_fs_is_pseudofs(fs
))
731 /* iterate over filesystems in @tb */
732 static struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
733 struct libmnt_iter
*itr
)
735 struct libmnt_fs
*fs
= NULL
;
737 if (is_listall_mode()) {
741 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
744 } else if (is_mount_compatible_mode()) {
746 * Look up for FS in the same way how mount(8) searchs in fstab
750 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
751 mnt_iter_get_direction(itr
));
753 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
754 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
755 mnt_iter_get_direction(itr
));
758 * Look up for all matching entries
760 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
761 * findmnt [-l] <spec> [-O <options>] [-t <types>]
764 mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
);
767 !(flags
& FL_NOSWAPMATCH
) &&
768 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
770 /* swap 'spec' and target. */
771 set_match(COL_TARGET
, get_match(COL_SOURCE
));
772 set_match(COL_SOURCE
, NULL
);
773 mnt_reset_iter(itr
, -1);
782 static int add_matching_lines(struct libmnt_table
*tb
,
783 struct tt
*tt
, int direction
)
785 struct libmnt_iter
*itr
= NULL
;
786 struct libmnt_fs
*fs
;
787 int nlines
= 0, rc
= -1;
789 itr
= mnt_new_iter(direction
);
791 warn(_("failed to initialize libmount iterator"));
795 while((fs
= get_next_fs(tb
, itr
))) {
796 if ((tt_flags
& TT_FL_TREE
) || (flags
& FL_SUBMOUNTS
))
797 rc
= create_treenode(tt
, tb
, fs
, NULL
);
799 rc
= !add_line(tt
, fs
, NULL
);
803 if (flags
& FL_FIRSTONLY
)
805 flags
|= FL_NOSWAPMATCH
;
815 static int poll_match(struct libmnt_fs
*fs
)
817 int rc
= match_func(fs
, NULL
);
819 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
820 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
822 * findmnt --poll /foo
823 * The '/foo' maybe source as well as target.
825 const char *str
= get_match(COL_SOURCE
);
827 set_match(COL_TARGET
, str
); /* swap */
828 set_match(COL_SOURCE
, NULL
);
830 rc
= match_func(fs
, NULL
);
832 set_match(COL_TARGET
, NULL
); /* restore */
833 set_match(COL_SOURCE
, str
);
839 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
840 int timeout
, struct tt
*tt
, int direction
)
844 struct libmnt_iter
*itr
= NULL
;
845 struct libmnt_table
*tb_new
= NULL
;
846 struct libmnt_tabdiff
*diff
= NULL
;
847 struct pollfd fds
[1];
849 tb_new
= mnt_new_table();
851 warn(_("failed to initialize libmount table"));
855 itr
= mnt_new_iter(direction
);
857 warn(_("failed to initialize libmount iterator"));
861 diff
= mnt_new_tabdiff();
863 warn(_("failed to initialize libmount tabdiff"));
867 /* cache is unnecessary to detect changes */
868 mnt_table_set_cache(tb
, NULL
);
869 mnt_table_set_cache(tb_new
, NULL
);
871 f
= fopen(tabfile
, "r");
873 warn(_("%s: open failed"), tabfile
);
877 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
879 fds
[0].fd
= fileno(f
);
880 fds
[0].events
= POLLPRI
;
883 struct libmnt_table
*tmp
;
884 struct libmnt_fs
*old
, *new;
887 count
= poll(fds
, 1, timeout
);
891 warn(_("poll() failed"));
896 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
898 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
903 mnt_reset_iter(itr
, direction
);
904 while(mnt_tabdiff_next_change(
905 diff
, itr
, &old
, &new, &change
) == 0) {
907 if (!has_poll_action(change
))
909 if (!poll_match(new ? new : old
))
912 rc
= !add_tabdiff_line(tt
, new, old
, change
);
915 if (flags
& FL_FIRSTONLY
)
920 rc
= tt_print_table(tt
);
931 mnt_reset_table(tb_new
);
933 if (count
&& (flags
& FL_FIRSTONLY
))
939 mnt_free_table(tb_new
);
940 mnt_free_tabdiff(diff
);
947 static void __attribute__((__noreturn__
)) usage(FILE *out
)
951 fputs(USAGE_HEADER
, out
);
954 " %1$s [options] <device> | <mountpoint>\n"
955 " %1$s [options] <device> <mountpoint>\n"
956 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
957 program_invocation_short_name
);
961 " -s, --fstab search in static table of filesystems\n"
962 " -m, --mtab search in table of mounted filesystems\n"
963 " -k, --kernel search in kernel table of mounted\n"
964 " filesystems (default)\n\n"));
967 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
968 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
971 " -A, --all disable all built-in filters, print all filesystems\n"
972 " -a, --ascii use ASCII chars for tree formatting\n"
973 " -c, --canonicalize canonicalize printed paths\n"
974 " -D, --df imitate the output of df(1)\n"
975 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
976 " -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
978 " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n"
979 " -f, --first-only print the first found filesystem only\n"));
982 " -i, --invert invert the sense of matching\n"
983 " -l, --list use list format output\n"
984 " -n, --noheadings don't print column headings\n"
985 " -u, --notruncate don't truncate text in columns\n"));
987 " -O, --options <list> limit the set of filesystems by mount options\n"
988 " -o, --output <list> the output columns to be shown\n"
989 " -P, --pairs use key=\"value\" output format\n"
990 " -r, --raw use raw output format\n"
991 " -t, --types <list> limit the set of filesystems by FS types\n"));
993 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
994 " -R, --submounts print all submounts for the matching filesystems\n"
995 " -S, --source <string> the device to mount (by name, maj:min, \n"
996 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"
997 " -T, --target <string> the mountpoint to use\n"));
999 fputs(USAGE_SEPARATOR
, out
);
1000 fputs(USAGE_HELP
, out
);
1001 fputs(USAGE_VERSION
, out
);
1003 fprintf(out
, _("\nAvailable columns:\n"));
1005 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
1006 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1008 fprintf(out
, USAGE_MAN_TAIL("findmnt(1)"));
1010 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1013 static void __attribute__((__noreturn__
))
1014 errx_mutually_exclusive(const char *opts
)
1016 errx(EXIT_FAILURE
, "%s %s", opts
, _("options are mutually exclusive"));
1019 int main(int argc
, char *argv
[])
1021 struct libmnt_table
*tb
= NULL
;
1022 char **tabfiles
= NULL
;
1023 int direction
= MNT_ITER_FORWARD
;
1024 int i
, c
, rc
= -1, timeout
= -1;
1025 int ntabfiles
= 0, tabtype
= 0;
1028 struct tt
*tt
= NULL
;
1030 static const struct option longopts
[] = {
1031 { "all", 0, 0, 'A' },
1032 { "ascii", 0, 0, 'a' },
1033 { "canonicalize", 0, 0, 'c' },
1034 { "direction", 1, 0, 'd' },
1035 { "df", 0, 0, 'D' },
1036 { "evaluate", 0, 0, 'e' },
1037 { "first-only", 0, 0, 'f' },
1038 { "fstab", 0, 0, 's' },
1039 { "help", 0, 0, 'h' },
1040 { "invert", 0, 0, 'i' },
1041 { "kernel", 0, 0, 'k' },
1042 { "list", 0, 0, 'l' },
1043 { "mtab", 0, 0, 'm' },
1044 { "noheadings", 0, 0, 'n' },
1045 { "notruncate", 0, 0, 'u' },
1046 { "options", 1, 0, 'O' },
1047 { "output", 1, 0, 'o' },
1048 { "poll", 2, 0, 'p' },
1049 { "pairs", 0, 0, 'P' },
1050 { "raw", 0, 0, 'r' },
1051 { "types", 1, 0, 't' },
1052 { "fsroot", 0, 0, 'v' },
1053 { "submounts", 0, 0, 'R' },
1054 { "source", 1, 0, 'S' },
1055 { "tab-file", 1, 0, 'F' },
1056 { "target", 1, 0, 'T' },
1057 { "timeout", 1, 0, 'w' },
1058 { "version", 0, 0, 'V' },
1063 assert(ARRAY_SIZE(columns
) == FINDMNT_NCOLUMNS
);
1065 setlocale(LC_ALL
, "");
1066 bindtextdomain(PACKAGE
, LOCALEDIR
);
1067 textdomain(PACKAGE
);
1068 atexit(close_stdout
);
1070 /* default output format */
1071 tt_flags
|= TT_FL_TREE
;
1073 while ((c
= getopt_long(argc
, argv
,
1074 "AacDd:ehifF:o:O:p::Pklmnrst:uvRS:T:w:V",
1075 longopts
, NULL
)) != -1) {
1081 tt_flags
|= TT_FL_ASCII
;
1084 flags
|= FL_CANONICALIZE
;
1087 tt_flags
&= ~TT_FL_TREE
;
1091 if (!strcmp(optarg
, "forward"))
1092 direction
= MNT_ITER_FORWARD
;
1093 else if (!strcmp(optarg
, "backward"))
1094 direction
= MNT_ITER_BACKWARD
;
1097 _("unknown direction '%s'"), optarg
);
1100 flags
|= FL_EVALUATE
;
1109 flags
|= FL_FIRSTONLY
;
1112 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1115 disable_columns_truncate();
1118 ncolumns
= string_to_idarray(optarg
,
1119 columns
, ARRAY_SIZE(columns
),
1125 set_match(COL_OPTIONS
, optarg
);
1129 nactions
= string_to_idarray(optarg
,
1130 actions
, ARRAY_SIZE(actions
),
1131 poll_action_name_to_id
);
1136 tt_flags
&= ~TT_FL_TREE
;
1139 tt_flags
|= TT_FL_EXPORT
;
1140 tt_flags
&= ~TT_FL_TREE
;
1142 case 'm': /* mtab */
1144 errx_mutually_exclusive("--{fstab,mtab,kernel}");
1145 tabtype
= TABTYPE_MTAB
;
1146 tt_flags
&= ~TT_FL_TREE
;
1148 case 's': /* fstab */
1150 errx_mutually_exclusive("--{fstab,mtab,kernel}");
1151 tabtype
= TABTYPE_FSTAB
;
1152 tt_flags
&= ~TT_FL_TREE
;
1154 case 'k': /* kernel (mountinfo) */
1156 errx_mutually_exclusive("--{fstab,mtab,kernel}");
1157 tabtype
= TABTYPE_KERNEL
;
1160 set_match(COL_FSTYPE
, optarg
);
1163 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1164 tt_flags
|= TT_FL_RAW
; /* enable raw */
1167 if ((tt_flags
& TT_FL_RAW
) && (tt_flags
& TT_FL_EXPORT
))
1168 errx_mutually_exclusive("--{raw,list,pairs}");
1170 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1173 tt_flags
|= TT_FL_NOHEADINGS
;
1176 flags
|= FL_NOFSROOT
;
1179 flags
|= FL_SUBMOUNTS
;
1182 set_source_match(optarg
);
1183 flags
|= FL_NOSWAPMATCH
;
1186 set_match(COL_TARGET
, optarg
);
1187 flags
|= FL_NOSWAPMATCH
;
1190 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1193 printf(UTIL_LINUX_VERSION
);
1194 return EXIT_SUCCESS
;
1201 if (!ncolumns
&& (flags
& FL_DF
)) {
1202 columns
[ncolumns
++] = COL_SOURCE
;
1203 columns
[ncolumns
++] = COL_FSTYPE
;
1204 columns
[ncolumns
++] = COL_SIZE
;
1205 columns
[ncolumns
++] = COL_USED
;
1206 columns
[ncolumns
++] = COL_AVAIL
;
1207 columns
[ncolumns
++] = COL_USEPERC
;
1208 columns
[ncolumns
++] = COL_TARGET
;
1211 /* default columns */
1213 if (flags
& FL_POLL
)
1214 columns
[ncolumns
++] = COL_ACTION
;
1216 columns
[ncolumns
++] = COL_TARGET
;
1217 columns
[ncolumns
++] = COL_SOURCE
;
1218 columns
[ncolumns
++] = COL_FSTYPE
;
1219 columns
[ncolumns
++] = COL_OPTIONS
;
1223 tabtype
= TABTYPE_KERNEL
;
1225 if (flags
& FL_POLL
) {
1226 if (tabtype
!= TABTYPE_KERNEL
)
1227 errx_mutually_exclusive("--{poll,fstab,mtab}");
1229 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1232 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1233 errx(EXIT_FAILURE
, _(
1234 "options --target and --source can't be used together "
1235 "with command line element that is not an option"));
1238 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1240 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1242 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1243 /* don't care about submounts if list all mounts */
1244 flags
&= ~FL_SUBMOUNTS
;
1246 if (!(flags
& FL_SUBMOUNTS
) &&
1247 (!is_listall_mode() || (flags
& FL_FIRSTONLY
)))
1248 tt_flags
&= ~TT_FL_TREE
;
1250 if (!(flags
& FL_NOSWAPMATCH
) &&
1251 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1253 * Check if we can swap source and target, it's
1254 * not possible if the source is LABEL=/UUID=
1256 const char *x
= get_match(COL_SOURCE
);
1258 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1259 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1260 flags
|= FL_NOSWAPMATCH
;
1264 * initialize libmount
1268 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1272 if ((tt_flags
& TT_FL_TREE
) && !tab_is_tree(tb
))
1273 tt_flags
&= ~TT_FL_TREE
;
1275 cache
= mnt_new_cache();
1277 warn(_("failed to initialize libmount cache"));
1280 mnt_table_set_cache(tb
, cache
);
1282 if (tabtype
== TABTYPE_KERNEL
1283 && (flags
& FL_NOSWAPMATCH
)
1284 && get_match(COL_TARGET
))
1286 * enable extra functionality for target match
1288 enable_extra_target_match();
1291 * initialize output formatting (tt.h)
1293 tt
= tt_new_table(tt_flags
);
1295 warn(_("failed to initialize output table"));
1299 for (i
= 0; i
< ncolumns
; i
++) {
1300 int fl
= get_column_flags(i
);
1301 int id
= get_column_id(i
);
1303 if (!(tt_flags
& TT_FL_TREE
))
1306 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1307 warnx(_("%s column is requested, but --poll "
1308 "is not enabled"), get_column_name(i
));
1311 if (!tt_define_column(tt
, get_column_name(i
),
1312 get_column_whint(i
), fl
)) {
1313 warn(_("failed to initialize output column"));
1319 * Fill in data to the output table
1321 if (flags
& FL_POLL
) {
1322 /* poll mode (accept the first tabfile only) */
1323 if (tabfiles
&& ntabfiles
> 0)
1324 rc
= poll_table(tb
, *tabfiles
, timeout
, tt
, direction
);
1326 } else if ((tt_flags
& TT_FL_TREE
) && is_listall_mode())
1328 rc
= create_treenode(tt
, tb
, NULL
, NULL
);
1330 /* whole lits of sub-tree */
1331 rc
= add_matching_lines(tb
, tt
, direction
);
1334 * Print the output table for non-poll modes
1336 if (!rc
&& !(flags
& FL_POLL
))
1342 mnt_free_cache(cache
);
1345 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;