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"
49 FL_EVALUATE
= (1 << 1),
50 FL_CANONICALIZE
= (1 << 2),
51 FL_FIRSTONLY
= (1 << 3),
53 FL_NOSWAPMATCH
= (1 << 6),
54 FL_NOFSROOT
= (1 << 7),
55 FL_SUBMOUNTS
= (1 << 8),
95 const char *name
; /* header */
96 double whint
; /* width hint (N < 1 is in percent of termwidth) */
97 int flags
; /* tt flags */
98 const char *help
; /* column description */
99 const char *match
; /* pattern for match_func() */
100 void *match_data
; /* match specific data */
103 /* columns descriptions (don't use const, this is writable) */
104 static struct colinfo infos
[FINDMNT_NCOLUMNS
] = {
105 [COL_SOURCE
] = { "SOURCE", 0.25, TT_FL_NOEXTREMES
, N_("source device") },
106 [COL_TARGET
] = { "TARGET", 0.30, TT_FL_TREE
| TT_FL_NOEXTREMES
, N_("mountpoint") },
107 [COL_FSTYPE
] = { "FSTYPE", 0.10, TT_FL_TRUNC
, N_("filesystem type") },
108 [COL_OPTIONS
] = { "OPTIONS", 0.10, TT_FL_TRUNC
, N_("all mount options") },
109 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC
, N_("VFS specific mount options") },
110 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC
, N_("FS specific mount options") },
111 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
112 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
113 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
114 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
115 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
116 [COL_ACTION
] = { "ACTION", 10, TT_FL_STRICTWIDTH
, N_("action detected by --poll") },
117 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC
, N_("old mount options saved by --poll") },
118 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
119 [COL_SIZE
] = { "SIZE", 5, TT_FL_RIGHT
, N_("filesystem size") },
120 [COL_AVAIL
] = { "AVAIL", 5, TT_FL_RIGHT
, N_("filesystem size available") },
121 [COL_USED
] = { "USED", 5, TT_FL_RIGHT
, N_("filesystem size used") },
122 [COL_USEPERC
] = { "USE%", 3, TT_FL_RIGHT
, N_("filesystem use percentage") },
123 [COL_FSROOT
] = { "FSROOT", 0.25, TT_FL_NOEXTREMES
, N_("filesystem root") },
124 [COL_TID
] = { "TID", 4, TT_FL_RIGHT
, N_("task ID") },
131 /* array with IDs of enabled columns */
132 static int columns
[FINDMNT_NCOLUMNS
];
135 /* poll actions (parsed --poll=<list> */
136 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
137 static int actions
[FINDMNT_NACTIONS
];
141 static struct libmnt_cache
*cache
;
143 static int get_column_id(int num
)
145 assert(num
< ncolumns
);
146 assert(columns
[num
] < FINDMNT_NCOLUMNS
);
150 static struct colinfo
*get_column_info(int num
)
152 return &infos
[ get_column_id(num
) ];
155 static const char *column_id_to_name(int id
)
157 assert(id
< FINDMNT_NCOLUMNS
);
158 return infos
[id
].name
;
161 static const char *get_column_name(int num
)
163 return get_column_info(num
)->name
;
166 static float get_column_whint(int num
)
168 return get_column_info(num
)->whint
;
171 static int get_column_flags(int num
)
173 return get_column_info(num
)->flags
;
176 static const char *get_match(int id
)
178 assert(id
< FINDMNT_NCOLUMNS
);
179 return infos
[id
].match
;
182 static void *get_match_data(int id
)
184 assert(id
< FINDMNT_NCOLUMNS
);
185 return infos
[id
].match_data
;
188 static void set_match(int id
, const char *match
)
190 assert(id
< FINDMNT_NCOLUMNS
);
191 infos
[id
].match
= match
;
194 static void set_match_data(int id
, void *data
)
196 assert(id
< FINDMNT_NCOLUMNS
);
197 infos
[id
].match_data
= data
;
201 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
204 static void set_source_match(const char *data
)
208 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
209 dev_t
*devno
= xmalloc(sizeof(dev_t
));
211 *devno
= makedev(maj
, min
);
212 set_match(COL_MAJMIN
, data
);
213 set_match_data(COL_MAJMIN
, (void *) devno
);
214 flags
|= FL_NOSWAPMATCH
;
216 set_match(COL_SOURCE
, data
);
219 static void enable_extra_target_match(void)
221 char *cn
= NULL
, *mnt
= NULL
;
224 * Check if match pattern is mountpoint, if not use the
227 cn
= mnt_resolve_path(get_match(COL_TARGET
), cache
);
231 mnt
= mnt_get_mountpoint(cn
);
232 if (!mnt
|| strcmp(mnt
, cn
) == 0)
235 /* replace the current setting with the real mountpoint */
236 set_match(COL_TARGET
, mnt
);
240 static int is_tabdiff_column(int id
)
242 assert(id
< FINDMNT_NCOLUMNS
);
247 case COL_OLD_OPTIONS
:
256 * "findmnt" without any filter
258 static int is_listall_mode(void)
260 if ((flags
& FL_DF
) && !(flags
& FL_ALL
))
263 return (!get_match(COL_SOURCE
) &&
264 !get_match(COL_TARGET
) &&
265 !get_match(COL_FSTYPE
) &&
266 !get_match(COL_OPTIONS
) &&
267 !get_match(COL_MAJMIN
));
271 * Returns 1 if the @act is in the --poll=<list>
273 static int has_poll_action(int act
)
278 return 1; /* all actions enabled */
279 for (i
= 0; i
< nactions
; i
++)
280 if (actions
[i
] == act
)
285 static int poll_action_name_to_id(const char *name
, size_t namesz
)
289 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
290 id
= MNT_TABDIFF_MOVE
;
291 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
292 id
= MNT_TABDIFF_MOUNT
;
293 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
294 id
= MNT_TABDIFF_UMOUNT
;
295 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
296 id
= MNT_TABDIFF_REMOUNT
;
298 warnx(_("unknown action: %s"), name
);
304 * findmnt --first-only <devname|TAG=|mountpoint>
306 * ... it works like "mount <devname|TAG=|mountpoint>"
308 static int is_mount_compatible_mode(void)
310 if (!get_match(COL_SOURCE
))
311 return 0; /* <devname|TAG=|mountpoint> is required */
312 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
313 return 0; /* cannot be restricted by -t or -O */
314 if (!(flags
& FL_FIRSTONLY
))
315 return 0; /* we have to return the first entry only */
320 static void disable_columns_truncate(void)
324 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
325 infos
[i
].flags
&= ~TT_FL_TRUNC
;
329 * converts @name to column ID
331 static int column_name_to_id(const char *name
, size_t namesz
)
335 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++) {
336 const char *cn
= column_id_to_name(i
);
338 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
341 warnx(_("unknown column: %s"), name
);
345 /* Returns LABEL or UUID */
346 static const char *get_tag(struct libmnt_fs
*fs
, const char *tagname
)
348 const char *t
, *v
, *res
;
350 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
353 res
= mnt_fs_get_source(fs
);
355 res
= mnt_resolve_spec(res
, cache
);
357 res
= mnt_cache_find_tag_value(cache
, res
, tagname
);
363 static const char *get_vfs_attr(struct libmnt_fs
*fs
, int sizetype
)
366 uint64_t vfs_attr
= 0;
369 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
374 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
377 vfs_attr
= buf
.f_frsize
* buf
.f_bfree
;
380 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
383 if (buf
.f_blocks
== 0)
386 xasprintf(&sizestr
, "%.0f%%",
387 (double)(buf
.f_blocks
- buf
.f_bfree
) /
392 return vfs_attr
== 0 ? "0" :
393 size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
396 /* reads FS data from libmount
398 static const char *get_data(struct libmnt_fs
*fs
, int num
)
400 const char *str
= NULL
;
401 int col_id
= get_column_id(num
);
406 const char *root
= mnt_fs_get_root(fs
);
408 str
= mnt_fs_get_srcpath(fs
);
410 if (str
&& (flags
& FL_CANONICALIZE
))
411 str
= mnt_resolve_path(str
, cache
);
413 str
= mnt_fs_get_source(fs
);
415 if (str
&& (flags
& FL_EVALUATE
))
416 str
= mnt_resolve_spec(str
, cache
);
418 if (root
&& str
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/")) {
421 if (xasprintf(&tmp
, "%s[%s]", str
, root
) > 0)
427 str
= mnt_fs_get_target(fs
);
430 str
= mnt_fs_get_fstype(fs
);
433 str
= mnt_fs_get_options(fs
);
435 case COL_VFS_OPTIONS
:
436 str
= mnt_fs_get_vfs_options(fs
);
439 str
= mnt_fs_get_fs_options(fs
);
442 str
= get_tag(fs
, "UUID");
445 str
= get_tag(fs
, "PARTUUID");
448 str
= get_tag(fs
, "LABEL");
451 str
= get_tag(fs
, "PARTLABEL");
456 dev_t devno
= mnt_fs_get_devno(fs
);
460 if ((tt_flags
& TT_FL_RAW
) || (tt_flags
& TT_FL_EXPORT
))
461 rc
= xasprintf(&tmp
, "%u:%u",
462 major(devno
), minor(devno
));
464 rc
= xasprintf(&tmp
, "%3u:%-3u",
465 major(devno
), minor(devno
));
475 str
= get_vfs_attr(fs
, col_id
);
478 str
= mnt_fs_get_root(fs
);
481 if (mnt_fs_get_tid(fs
)) {
483 if (xasprintf(&tmp
, "%d", mnt_fs_get_tid(fs
)) > 0)
493 static const char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
494 struct libmnt_fs
*new_fs
,
498 const char *str
= NULL
;
500 switch (get_column_id(num
)) {
503 case MNT_TABDIFF_MOUNT
:
506 case MNT_TABDIFF_UMOUNT
:
509 case MNT_TABDIFF_REMOUNT
:
512 case MNT_TABDIFF_MOVE
:
520 case COL_OLD_OPTIONS
:
521 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
522 change
== MNT_TABDIFF_UMOUNT
))
523 str
= mnt_fs_get_options(old_fs
);
526 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
527 change
== MNT_TABDIFF_UMOUNT
))
528 str
= mnt_fs_get_target(old_fs
);
532 str
= get_data(new_fs
, num
);
534 str
= get_data(old_fs
, num
);
540 /* adds one line to the output @tab */
541 static struct tt_line
*add_line(struct tt
*tt
, struct libmnt_fs
*fs
,
542 struct tt_line
*parent
)
545 struct tt_line
*line
= tt_add_line(tt
, parent
);
548 warn(_("failed to add line to output"));
551 for (i
= 0; i
< ncolumns
; i
++)
552 tt_line_set_data(line
, i
, get_data(fs
, i
));
554 tt_line_set_userdata(line
, fs
);
558 static struct tt_line
*add_tabdiff_line(struct tt
*tt
, struct libmnt_fs
*new_fs
,
559 struct libmnt_fs
*old_fs
, int change
)
562 struct tt_line
*line
= tt_add_line(tt
, NULL
);
565 warn(_("failed to add line to output"));
568 for (i
= 0; i
< ncolumns
; i
++)
569 tt_line_set_data(line
, i
,
570 get_tabdiff_data(old_fs
, new_fs
, change
, i
));
575 static int has_line(struct tt
*tt
, struct libmnt_fs
*fs
)
579 list_for_each(p
, &tt
->tb_lines
) {
580 struct tt_line
*ln
= list_entry(p
, struct tt_line
, ln_lines
);
581 if ((struct libmnt_fs
*) ln
->userdata
== fs
)
587 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
588 static int create_treenode(struct tt
*tt
, struct libmnt_table
*tb
,
589 struct libmnt_fs
*fs
, struct tt_line
*parent_line
)
591 struct libmnt_fs
*chld
= NULL
;
592 struct libmnt_iter
*itr
= NULL
;
593 struct tt_line
*line
;
597 /* first call, get root FS */
598 if (mnt_table_get_root_fs(tb
, &fs
))
602 } else if ((flags
& FL_SUBMOUNTS
) && has_line(tt
, fs
))
605 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
609 line
= add_line(tt
, fs
, parent_line
);
614 * add all children to the output table
616 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
617 if (create_treenode(tt
, tb
, chld
, line
))
627 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
628 const char *filename
, int line
)
630 warnx(_("%s: parse error at line %d"), filename
, line
);
634 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
636 files
= xrealloc(files
, sizeof(char *) * (*nfiles
+ 1));
637 files
[(*nfiles
)++] = filename
;
641 static char **append_pid_tabfile(char **files
, int *nfiles
, pid_t pid
)
645 xasprintf(&path
, "/proc/%d/mountinfo", (int) pid
);
646 return append_tabfile(files
, nfiles
, path
);
649 /* calls libmount fstab/mtab/mountinfo parser */
650 static struct libmnt_table
*parse_tabfiles(char **files
,
654 struct libmnt_table
*tb
;
657 tb
= mnt_new_table();
659 warn(_("failed to initialize libmount table"));
662 mnt_table_set_parser_errcb(tb
, parser_errcb
);
665 /* NULL means that libmount will use default paths */
666 const char *path
= nfiles
? *files
++ : NULL
;
670 rc
= mnt_table_parse_fstab(tb
, path
);
673 rc
= mnt_table_parse_mtab(tb
, path
);
677 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
678 _PATH_PROC_MOUNTINFO
:
681 rc
= mnt_table_parse_file(tb
, path
);
686 warn(_("can't read %s"), path
);
689 } while (--nfiles
> 0);
694 /* checks if @tb contains parent->child relations */
695 static int tab_is_tree(struct libmnt_table
*tb
)
697 struct libmnt_fs
*fs
= NULL
;
698 struct libmnt_iter
*itr
= NULL
;
701 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
705 if (mnt_table_next_fs(tb
, itr
, &fs
) == 0)
706 rc
= mnt_fs_get_id(fs
) > 0 && mnt_fs_get_parent_id(fs
) > 0;
713 /* filter function for libmount (mnt_table_find_next_fs()) */
714 static int match_func(struct libmnt_fs
*fs
,
715 void *data
__attribute__ ((__unused__
)))
717 int rc
= flags
& FL_INVERT
? 1 : 0;
721 m
= get_match(COL_TARGET
);
722 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
725 m
= get_match(COL_SOURCE
);
726 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
729 m
= get_match(COL_FSTYPE
);
730 if (m
&& !mnt_fs_match_fstype(fs
, m
))
733 m
= get_match(COL_OPTIONS
);
734 if (m
&& !mnt_fs_match_options(fs
, m
))
737 md
= get_match_data(COL_MAJMIN
);
738 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
741 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
742 const char *type
= mnt_fs_get_fstype(fs
);
744 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
747 if (mnt_fs_is_pseudofs(fs
))
754 /* iterate over filesystems in @tb */
755 static struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
756 struct libmnt_iter
*itr
)
758 struct libmnt_fs
*fs
= NULL
;
760 if (is_listall_mode()) {
764 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
767 } else if (is_mount_compatible_mode()) {
769 * Look up for FS in the same way how mount(8) searchs in fstab
773 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
774 mnt_iter_get_direction(itr
));
776 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
777 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
778 mnt_iter_get_direction(itr
));
781 * Look up for all matching entries
783 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
784 * findmnt [-l] <spec> [-O <options>] [-t <types>]
787 mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
);
790 !(flags
& FL_NOSWAPMATCH
) &&
791 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
793 /* swap 'spec' and target. */
794 set_match(COL_TARGET
, get_match(COL_SOURCE
));
795 set_match(COL_SOURCE
, NULL
);
796 mnt_reset_iter(itr
, -1);
805 static int add_matching_lines(struct libmnt_table
*tb
,
806 struct tt
*tt
, int direction
)
808 struct libmnt_iter
*itr
= NULL
;
809 struct libmnt_fs
*fs
;
810 int nlines
= 0, rc
= -1;
812 itr
= mnt_new_iter(direction
);
814 warn(_("failed to initialize libmount iterator"));
818 while((fs
= get_next_fs(tb
, itr
))) {
819 if ((tt_flags
& TT_FL_TREE
) || (flags
& FL_SUBMOUNTS
))
820 rc
= create_treenode(tt
, tb
, fs
, NULL
);
822 rc
= !add_line(tt
, fs
, NULL
);
826 if (flags
& FL_FIRSTONLY
)
828 flags
|= FL_NOSWAPMATCH
;
838 static int poll_match(struct libmnt_fs
*fs
)
840 int rc
= match_func(fs
, NULL
);
842 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
843 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
845 * findmnt --poll /foo
846 * The '/foo' maybe source as well as target.
848 const char *str
= get_match(COL_SOURCE
);
850 set_match(COL_TARGET
, str
); /* swap */
851 set_match(COL_SOURCE
, NULL
);
853 rc
= match_func(fs
, NULL
);
855 set_match(COL_TARGET
, NULL
); /* restore */
856 set_match(COL_SOURCE
, str
);
862 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
863 int timeout
, struct tt
*tt
, int direction
)
867 struct libmnt_iter
*itr
= NULL
;
868 struct libmnt_table
*tb_new
= NULL
;
869 struct libmnt_tabdiff
*diff
= NULL
;
870 struct pollfd fds
[1];
872 tb_new
= mnt_new_table();
874 warn(_("failed to initialize libmount table"));
878 itr
= mnt_new_iter(direction
);
880 warn(_("failed to initialize libmount iterator"));
884 diff
= mnt_new_tabdiff();
886 warn(_("failed to initialize libmount tabdiff"));
890 /* cache is unnecessary to detect changes */
891 mnt_table_set_cache(tb
, NULL
);
892 mnt_table_set_cache(tb_new
, NULL
);
894 f
= fopen(tabfile
, "r");
896 warn(_("cannot open %s"), tabfile
);
900 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
902 fds
[0].fd
= fileno(f
);
903 fds
[0].events
= POLLPRI
;
906 struct libmnt_table
*tmp
;
907 struct libmnt_fs
*old
, *new;
910 count
= poll(fds
, 1, timeout
);
914 warn(_("poll() failed"));
919 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
921 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
926 mnt_reset_iter(itr
, direction
);
927 while(mnt_tabdiff_next_change(
928 diff
, itr
, &old
, &new, &change
) == 0) {
930 if (!has_poll_action(change
))
932 if (!poll_match(new ? new : old
))
935 rc
= !add_tabdiff_line(tt
, new, old
, change
);
938 if (flags
& FL_FIRSTONLY
)
943 rc
= tt_print_table(tt
);
954 mnt_reset_table(tb_new
);
956 if (count
&& (flags
& FL_FIRSTONLY
))
962 mnt_free_table(tb_new
);
963 mnt_free_tabdiff(diff
);
970 static void __attribute__((__noreturn__
)) usage(FILE *out
)
974 fputs(USAGE_HEADER
, out
);
977 " %1$s [options] <device> | <mountpoint>\n"
978 " %1$s [options] <device> <mountpoint>\n"
979 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
980 program_invocation_short_name
);
984 " -s, --fstab search in static table of filesystems\n"
985 " -m, --mtab search in table of mounted filesystems\n"
986 " -k, --kernel search in kernel table of mounted\n"
987 " filesystems (default)\n\n"));
990 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
991 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
994 " -A, --all disable all built-in filters, print all filesystems\n"
995 " -a, --ascii use ASCII chars for tree formatting\n"
996 " -c, --canonicalize canonicalize printed paths\n"
997 " -D, --df imitate the output of df(1)\n"
998 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
999 " -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1000 " to device names\n"
1001 " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n"
1002 " -f, --first-only print the first found filesystem only\n"));
1005 " -i, --invert invert the sense of matching\n"
1006 " -l, --list use list format output\n"
1007 " -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"
1008 " -n, --noheadings don't print column headings\n"
1009 " -u, --notruncate don't truncate text in columns\n"));
1011 " -O, --options <list> limit the set of filesystems by mount options\n"
1012 " -o, --output <list> the output columns to be shown\n"
1013 " -P, --pairs use key=\"value\" output format\n"
1014 " -r, --raw use raw output format\n"
1015 " -t, --types <list> limit the set of filesystems by FS types\n"));
1017 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
1018 " -R, --submounts print all submounts for the matching filesystems\n"
1019 " -S, --source <string> the device to mount (by name, maj:min, \n"
1020 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"
1021 " -T, --target <string> the mountpoint to use\n"));
1023 fputs(USAGE_SEPARATOR
, out
);
1024 fputs(USAGE_HELP
, out
);
1025 fputs(USAGE_VERSION
, out
);
1027 fprintf(out
, _("\nAvailable columns:\n"));
1029 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
1030 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1032 fprintf(out
, USAGE_MAN_TAIL("findmnt(8)"));
1034 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1037 int main(int argc
, char *argv
[])
1039 struct libmnt_table
*tb
= NULL
;
1040 char **tabfiles
= NULL
;
1041 int direction
= MNT_ITER_FORWARD
;
1042 int i
, c
, rc
= -1, timeout
= -1;
1043 int ntabfiles
= 0, tabtype
= 0;
1057 int excl_fmk
= EXCL_NONE
;
1058 int excl_fmN
= EXCL_NONE
;
1059 int excl_rlP
= EXCL_NONE
;
1062 struct tt
*tt
= NULL
;
1064 static const struct option longopts
[] = {
1065 { "all", 0, 0, 'A' },
1066 { "ascii", 0, 0, 'a' },
1067 { "canonicalize", 0, 0, 'c' },
1068 { "direction", 1, 0, 'd' },
1069 { "df", 0, 0, 'D' },
1070 { "evaluate", 0, 0, 'e' },
1071 { "first-only", 0, 0, 'f' },
1072 { "fstab", 0, 0, 's' },
1073 { "help", 0, 0, 'h' },
1074 { "invert", 0, 0, 'i' },
1075 { "kernel", 0, 0, 'k' },
1076 { "list", 0, 0, 'l' },
1077 { "mtab", 0, 0, 'm' },
1078 { "noheadings", 0, 0, 'n' },
1079 { "notruncate", 0, 0, 'u' },
1080 { "options", 1, 0, 'O' },
1081 { "output", 1, 0, 'o' },
1082 { "poll", 2, 0, 'p' },
1083 { "pairs", 0, 0, 'P' },
1084 { "raw", 0, 0, 'r' },
1085 { "types", 1, 0, 't' },
1086 { "fsroot", 0, 0, 'v' },
1087 { "submounts", 0, 0, 'R' },
1088 { "source", 1, 0, 'S' },
1089 { "tab-file", 1, 0, 'F' },
1090 { "task", 1, 0, 'N' },
1091 { "target", 1, 0, 'T' },
1092 { "timeout", 1, 0, 'w' },
1093 { "version", 0, 0, 'V' },
1098 assert(ARRAY_SIZE(columns
) == FINDMNT_NCOLUMNS
);
1100 setlocale(LC_ALL
, "");
1101 bindtextdomain(PACKAGE
, LOCALEDIR
);
1102 textdomain(PACKAGE
);
1103 atexit(close_stdout
);
1105 /* default output format */
1106 tt_flags
|= TT_FL_TREE
;
1108 while ((c
= getopt_long(argc
, argv
,
1109 "AacDd:ehifF:o:O:p::PklmnN:rst:uvRS:T:w:V",
1110 longopts
, NULL
)) != -1) {
1116 tt_flags
|= TT_FL_ASCII
;
1119 flags
|= FL_CANONICALIZE
;
1122 tt_flags
&= ~TT_FL_TREE
;
1126 if (!strcmp(optarg
, "forward"))
1127 direction
= MNT_ITER_FORWARD
;
1128 else if (!strcmp(optarg
, "backward"))
1129 direction
= MNT_ITER_BACKWARD
;
1132 _("unknown direction '%s'"), optarg
);
1135 flags
|= FL_EVALUATE
;
1144 flags
|= FL_FIRSTONLY
;
1147 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1150 disable_columns_truncate();
1153 ncolumns
= string_to_idarray(optarg
,
1154 columns
, ARRAY_SIZE(columns
),
1160 set_match(COL_OPTIONS
, optarg
);
1164 nactions
= string_to_idarray(optarg
,
1165 actions
, ARRAY_SIZE(actions
),
1166 poll_action_name_to_id
);
1171 tt_flags
&= ~TT_FL_TREE
;
1174 exclusive_option(&excl_rlP
, EXCL_PAIRS
, "--{raw,list,pairs}");
1175 tt_flags
|= TT_FL_EXPORT
;
1176 tt_flags
&= ~TT_FL_TREE
;
1178 case 'm': /* mtab */
1179 exclusive_option(&excl_fmk
, EXCL_MTAB
, "--{fstab,mtab,kernel}");
1180 exclusive_option(&excl_fmN
, EXCL_MTAB
, "--{fstab,mtab,task}");
1181 tabtype
= TABTYPE_MTAB
;
1182 tt_flags
&= ~TT_FL_TREE
;
1184 case 's': /* fstab */
1185 exclusive_option(&excl_fmk
, EXCL_FSTAB
, "--{fstab,mtab,kernel}");
1186 exclusive_option(&excl_fmN
, EXCL_FSTAB
, "--{fstab,mtab,task}");
1187 tabtype
= TABTYPE_FSTAB
;
1188 tt_flags
&= ~TT_FL_TREE
;
1190 case 'k': /* kernel (mountinfo) */
1191 exclusive_option(&excl_fmk
, EXCL_KERNEL
, "--{fstab,mtab,kernel}");
1192 tabtype
= TABTYPE_KERNEL
;
1195 set_match(COL_FSTYPE
, optarg
);
1198 exclusive_option(&excl_rlP
, EXCL_RAW
, "--{raw,list,pairs}");
1199 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1200 tt_flags
|= TT_FL_RAW
; /* enable raw */
1203 exclusive_option(&excl_rlP
, EXCL_LIST
, "--{raw,list,pairs}");
1204 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1207 tt_flags
|= TT_FL_NOHEADINGS
;
1210 exclusive_option(&excl_fmN
, EXCL_KERNEL
, "--{fstab,mtab,task}");
1211 tabtype
= TABTYPE_KERNEL
;
1212 tabfiles
= append_pid_tabfile(tabfiles
, &ntabfiles
,
1213 strtou32_or_err(optarg
,
1214 _("invalid TID argument")));
1217 flags
|= FL_NOFSROOT
;
1220 flags
|= FL_SUBMOUNTS
;
1223 set_source_match(optarg
);
1224 flags
|= FL_NOSWAPMATCH
;
1227 set_match(COL_TARGET
, optarg
);
1228 flags
|= FL_NOSWAPMATCH
;
1231 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1234 printf(UTIL_LINUX_VERSION
);
1235 return EXIT_SUCCESS
;
1242 if (!ncolumns
&& (flags
& FL_DF
)) {
1243 columns
[ncolumns
++] = COL_SOURCE
;
1244 columns
[ncolumns
++] = COL_FSTYPE
;
1245 columns
[ncolumns
++] = COL_SIZE
;
1246 columns
[ncolumns
++] = COL_USED
;
1247 columns
[ncolumns
++] = COL_AVAIL
;
1248 columns
[ncolumns
++] = COL_USEPERC
;
1249 columns
[ncolumns
++] = COL_TARGET
;
1252 /* default columns */
1254 if (flags
& FL_POLL
)
1255 columns
[ncolumns
++] = COL_ACTION
;
1257 columns
[ncolumns
++] = COL_TARGET
;
1258 columns
[ncolumns
++] = COL_SOURCE
;
1259 columns
[ncolumns
++] = COL_FSTYPE
;
1260 columns
[ncolumns
++] = COL_OPTIONS
;
1264 tabtype
= TABTYPE_KERNEL
;
1266 if (flags
& FL_POLL
) {
1267 if (tabtype
!= TABTYPE_KERNEL
)
1268 exclusive_option(&tabtype
, tabtype
+ 1, "--{poll,fstab,mtab}");
1270 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1273 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1274 errx(EXIT_FAILURE
, _(
1275 "options --target and --source can't be used together "
1276 "with command line element that is not an option"));
1279 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1281 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1283 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1284 /* don't care about submounts if list all mounts */
1285 flags
&= ~FL_SUBMOUNTS
;
1287 if (!(flags
& FL_SUBMOUNTS
) &&
1288 (!is_listall_mode() || (flags
& FL_FIRSTONLY
)))
1289 tt_flags
&= ~TT_FL_TREE
;
1291 if (!(flags
& FL_NOSWAPMATCH
) &&
1292 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1294 * Check if we can swap source and target, it's
1295 * not possible if the source is LABEL=/UUID=
1297 const char *x
= get_match(COL_SOURCE
);
1299 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1300 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1301 flags
|= FL_NOSWAPMATCH
;
1305 * initialize libmount
1309 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1313 if ((tt_flags
& TT_FL_TREE
) && (ntabfiles
> 1 || !tab_is_tree(tb
)))
1314 tt_flags
&= ~TT_FL_TREE
;
1316 cache
= mnt_new_cache();
1318 warn(_("failed to initialize libmount cache"));
1321 mnt_table_set_cache(tb
, cache
);
1323 if (tabtype
== TABTYPE_KERNEL
1324 && (flags
& FL_NOSWAPMATCH
)
1325 && get_match(COL_TARGET
))
1327 * enable extra functionality for target match
1329 enable_extra_target_match();
1332 * initialize output formatting (tt.h)
1334 tt
= tt_new_table(tt_flags
);
1336 warn(_("failed to initialize output table"));
1340 for (i
= 0; i
< ncolumns
; i
++) {
1341 int fl
= get_column_flags(i
);
1342 int id
= get_column_id(i
);
1344 if (!(tt_flags
& TT_FL_TREE
))
1347 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1348 warnx(_("%s column is requested, but --poll "
1349 "is not enabled"), get_column_name(i
));
1352 if (!tt_define_column(tt
, get_column_name(i
),
1353 get_column_whint(i
), fl
)) {
1354 warn(_("failed to initialize output column"));
1360 * Fill in data to the output table
1362 if (flags
& FL_POLL
) {
1363 /* poll mode (accept the first tabfile only) */
1364 rc
= poll_table(tb
, tabfiles
? *tabfiles
: _PATH_PROC_MOUNTINFO
, timeout
, tt
, direction
);
1366 } else if ((tt_flags
& TT_FL_TREE
) && is_listall_mode())
1368 rc
= create_treenode(tt
, tb
, NULL
, NULL
);
1370 /* whole lits of sub-tree */
1371 rc
= add_matching_lines(tb
, tt
, direction
);
1374 * Print the output table for non-poll modes
1376 if (!rc
&& !(flags
& FL_POLL
))
1382 mnt_free_cache(cache
);
1385 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;