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>
40 #include "pathnames.h"
42 #include "closestream.h"
52 FL_EVALUATE
= (1 << 1),
53 FL_CANONICALIZE
= (1 << 2),
54 FL_FIRSTONLY
= (1 << 3),
56 FL_NOSWAPMATCH
= (1 << 6),
57 FL_NOFSROOT
= (1 << 7),
58 FL_SUBMOUNTS
= (1 << 8),
103 const char *name
; /* header */
104 double whint
; /* width hint (N < 1 is in percent of termwidth) */
105 int flags
; /* tt flags */
106 const char *help
; /* column description */
107 const char *match
; /* pattern for match_func() */
108 void *match_data
; /* match specific data */
111 /* columns descriptions (don't use const, this is writable) */
112 static struct colinfo infos
[FINDMNT_NCOLUMNS
] = {
113 [COL_SOURCE
] = { "SOURCE", 0.25, TT_FL_NOEXTREMES
, N_("source device") },
114 [COL_TARGET
] = { "TARGET", 0.30, TT_FL_TREE
| TT_FL_NOEXTREMES
, N_("mountpoint") },
115 [COL_FSTYPE
] = { "FSTYPE", 0.10, TT_FL_TRUNC
, N_("filesystem type") },
116 [COL_OPTIONS
] = { "OPTIONS", 0.10, TT_FL_TRUNC
, N_("all mount options") },
117 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC
, N_("VFS specific mount options") },
118 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC
, N_("FS specific mount options") },
119 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
120 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
121 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
122 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
123 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
124 [COL_ACTION
] = { "ACTION", 10, TT_FL_STRICTWIDTH
, N_("action detected by --poll") },
125 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC
, N_("old mount options saved by --poll") },
126 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
127 [COL_SIZE
] = { "SIZE", 5, TT_FL_RIGHT
, N_("filesystem size") },
128 [COL_AVAIL
] = { "AVAIL", 5, TT_FL_RIGHT
, N_("filesystem size available") },
129 [COL_USED
] = { "USED", 5, TT_FL_RIGHT
, N_("filesystem size used") },
130 [COL_USEPERC
] = { "USE%", 3, TT_FL_RIGHT
, N_("filesystem use percentage") },
131 [COL_FSROOT
] = { "FSROOT", 0.25, TT_FL_NOEXTREMES
, N_("filesystem root") },
132 [COL_TID
] = { "TID", 4, TT_FL_RIGHT
, N_("task ID") },
133 [COL_ID
] = { "ID", 2, TT_FL_RIGHT
, N_("mount ID") },
134 [COL_OPT_FIELDS
] = { "OPT-FIELDS", 0.10, TT_FL_TRUNC
, N_("optional mount fields") },
135 [COL_PROPAGATION
] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
136 [COL_FREQ
] = { "FREQ", 1, TT_FL_RIGHT
, N_("dump(8) period in days [fstab only]") },
137 [COL_PASSNO
] = { "PASSNO", 1, TT_FL_RIGHT
, N_("pass number on parallel fsck(8) [fstab only]") }
144 /* array with IDs of enabled columns */
145 static int columns
[FINDMNT_NCOLUMNS
];
148 /* poll actions (parsed --poll=<list> */
149 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
150 static int actions
[FINDMNT_NACTIONS
];
154 static struct libmnt_cache
*cache
;
160 static int match_func(struct libmnt_fs
*fs
, void *data
__attribute__ ((__unused__
)));
163 static int get_column_id(int num
)
165 assert(num
< ncolumns
);
166 assert(columns
[num
] < FINDMNT_NCOLUMNS
);
170 static struct colinfo
*get_column_info(int num
)
172 return &infos
[ get_column_id(num
) ];
175 static const char *column_id_to_name(int id
)
177 assert(id
< FINDMNT_NCOLUMNS
);
178 return infos
[id
].name
;
181 static const char *get_column_name(int num
)
183 return get_column_info(num
)->name
;
186 static float get_column_whint(int num
)
188 return get_column_info(num
)->whint
;
191 static int get_column_flags(int num
)
193 return get_column_info(num
)->flags
;
196 static const char *get_match(int id
)
198 assert(id
< FINDMNT_NCOLUMNS
);
199 return infos
[id
].match
;
202 static void *get_match_data(int id
)
204 assert(id
< FINDMNT_NCOLUMNS
);
205 return infos
[id
].match_data
;
208 static void set_match(int id
, const char *match
)
210 assert(id
< FINDMNT_NCOLUMNS
);
211 infos
[id
].match
= match
;
214 static void set_match_data(int id
, void *data
)
216 assert(id
< FINDMNT_NCOLUMNS
);
217 infos
[id
].match_data
= data
;
221 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
224 static void set_source_match(const char *data
)
228 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
229 dev_t
*devno
= xmalloc(sizeof(dev_t
));
231 *devno
= makedev(maj
, min
);
232 set_match(COL_MAJMIN
, data
);
233 set_match_data(COL_MAJMIN
, (void *) devno
);
234 flags
|= FL_NOSWAPMATCH
;
236 set_match(COL_SOURCE
, data
);
239 static void enable_extra_target_match(void)
241 char *cn
= NULL
, *mnt
= NULL
;
244 * Check if match pattern is mountpoint, if not use the
247 cn
= mnt_resolve_path(get_match(COL_TARGET
), cache
);
251 mnt
= mnt_get_mountpoint(cn
);
252 if (!mnt
|| strcmp(mnt
, cn
) == 0)
255 /* replace the current setting with the real mountpoint */
256 set_match(COL_TARGET
, mnt
);
260 static int is_tabdiff_column(int id
)
262 assert(id
< FINDMNT_NCOLUMNS
);
267 case COL_OLD_OPTIONS
:
276 * "findmnt" without any filter
278 static int is_listall_mode(void)
280 if ((flags
& FL_DF
) && !(flags
& FL_ALL
))
283 return (!get_match(COL_SOURCE
) &&
284 !get_match(COL_TARGET
) &&
285 !get_match(COL_FSTYPE
) &&
286 !get_match(COL_OPTIONS
) &&
287 !get_match(COL_MAJMIN
));
291 * Returns 1 if the @act is in the --poll=<list>
293 static int has_poll_action(int act
)
298 return 1; /* all actions enabled */
299 for (i
= 0; i
< nactions
; i
++)
300 if (actions
[i
] == act
)
305 static int poll_action_name_to_id(const char *name
, size_t namesz
)
309 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
310 id
= MNT_TABDIFF_MOVE
;
311 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
312 id
= MNT_TABDIFF_MOUNT
;
313 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
314 id
= MNT_TABDIFF_UMOUNT
;
315 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
316 id
= MNT_TABDIFF_REMOUNT
;
318 warnx(_("unknown action: %s"), name
);
324 * findmnt --first-only <devname|TAG=|mountpoint>
326 * ... it works like "mount <devname|TAG=|mountpoint>"
328 static int is_mount_compatible_mode(void)
330 if (!get_match(COL_SOURCE
))
331 return 0; /* <devname|TAG=|mountpoint> is required */
332 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
333 return 0; /* cannot be restricted by -t or -O */
334 if (!(flags
& FL_FIRSTONLY
))
335 return 0; /* we have to return the first entry only */
340 static void disable_columns_truncate(void)
344 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
345 infos
[i
].flags
&= ~TT_FL_TRUNC
;
349 * converts @name to column ID
351 static int column_name_to_id(const char *name
, size_t namesz
)
355 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++) {
356 const char *cn
= column_id_to_name(i
);
358 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
361 warnx(_("unknown column: %s"), name
);
367 static char *get_tag_from_udev(const char *devname
, int col
)
369 struct udev_device
*dev
;
370 const char *data
= NULL
;
371 char *res
= NULL
, *path
;
378 /* libudev don't like /dev/mapper/ symlinks */
379 path
= realpath(devname
, NULL
);
383 if (strncmp(devname
, "/dev/", 5) == 0)
386 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", devname
);
394 data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC");
397 data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC");
400 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID");
403 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME");
411 unhexmangle_string(res
);
414 udev_device_unref(dev
);
417 #endif /* HAVE_LIBUDEV */
419 /* Returns LABEL or UUID */
420 static const char *get_tag(struct libmnt_fs
*fs
, const char *tagname
, int col
422 __attribute__((__unused__
))
426 const char *t
, *v
, *res
= NULL
;
428 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
431 const char *dev
= mnt_fs_get_source(fs
);
434 dev
= mnt_resolve_spec(dev
, cache
);
437 res
= get_tag_from_udev(dev
, col
);
440 res
= mnt_cache_find_tag_value(cache
, dev
, tagname
);
446 static const char *get_vfs_attr(struct libmnt_fs
*fs
, int sizetype
)
449 uint64_t vfs_attr
= 0;
452 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
457 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
460 vfs_attr
= buf
.f_frsize
* buf
.f_bavail
;
463 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
466 if (buf
.f_blocks
== 0)
469 xasprintf(&sizestr
, "%.0f%%",
470 (double)(buf
.f_blocks
- buf
.f_bfree
) /
475 return vfs_attr
== 0 ? "0" :
476 size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
479 /* reads FS data from libmount
481 static const char *get_data(struct libmnt_fs
*fs
, int num
)
483 const char *str
= NULL
;
485 int col_id
= get_column_id(num
);
490 const char *root
= mnt_fs_get_root(fs
);
492 str
= mnt_fs_get_srcpath(fs
);
494 if (str
&& (flags
& FL_CANONICALIZE
))
495 str
= mnt_resolve_path(str
, cache
);
497 str
= mnt_fs_get_source(fs
);
499 if (str
&& (flags
& FL_EVALUATE
))
500 str
= mnt_resolve_spec(str
, cache
);
502 if (root
&& str
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/")) {
503 xasprintf(&tmp
, "%s[%s]", str
, root
);
509 str
= mnt_fs_get_target(fs
);
512 str
= mnt_fs_get_fstype(fs
);
515 str
= mnt_fs_get_options(fs
);
517 case COL_VFS_OPTIONS
:
518 str
= mnt_fs_get_vfs_options(fs
);
521 str
= mnt_fs_get_fs_options(fs
);
524 str
= mnt_fs_get_optional_fields(fs
);
527 str
= get_tag(fs
, "UUID", col_id
);
530 str
= get_tag(fs
, "PARTUUID", col_id
);
533 str
= get_tag(fs
, "LABEL", col_id
);
536 str
= get_tag(fs
, "PARTLABEL", col_id
);
541 dev_t devno
= mnt_fs_get_devno(fs
);
545 if ((tt_flags
& TT_FL_RAW
) || (tt_flags
& TT_FL_EXPORT
))
546 xasprintf(&tmp
, "%u:%u", major(devno
), minor(devno
));
548 xasprintf(&tmp
, "%3u:%-3u", major(devno
), minor(devno
));
556 str
= get_vfs_attr(fs
, col_id
);
559 str
= mnt_fs_get_root(fs
);
562 if (mnt_fs_get_tid(fs
)) {
563 xasprintf(&tmp
, "%d", mnt_fs_get_tid(fs
));
568 if (mnt_fs_get_id(fs
)) {
569 xasprintf(&tmp
, "%d", mnt_fs_get_id(fs
));
573 case COL_PROPAGATION
:
574 if (mnt_fs_is_kernel(fs
)) {
575 unsigned long fl
= 0;
578 if (mnt_fs_get_propagation(fs
, &fl
) != 0)
581 n
= xstrdup((fl
& MS_SHARED
) ? "shared" : "private");
584 xasprintf(&tmp
, "%s,slave", n
);
588 if (fl
& MS_UNBINDABLE
) {
589 xasprintf(&tmp
, "%s,unbindable", n
);
597 if (!mnt_fs_is_kernel(fs
)) {
598 xasprintf(&tmp
, "%d", mnt_fs_get_freq(fs
));
603 if (!mnt_fs_is_kernel(fs
)) {
604 xasprintf(&tmp
, "%d", mnt_fs_get_passno(fs
));
614 static const char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
615 struct libmnt_fs
*new_fs
,
619 const char *str
= NULL
;
621 switch (get_column_id(num
)) {
624 case MNT_TABDIFF_MOUNT
:
627 case MNT_TABDIFF_UMOUNT
:
630 case MNT_TABDIFF_REMOUNT
:
633 case MNT_TABDIFF_MOVE
:
641 case COL_OLD_OPTIONS
:
642 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
643 change
== MNT_TABDIFF_UMOUNT
))
644 str
= mnt_fs_get_options(old_fs
);
647 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
648 change
== MNT_TABDIFF_UMOUNT
))
649 str
= mnt_fs_get_target(old_fs
);
653 str
= get_data(new_fs
, num
);
655 str
= get_data(old_fs
, num
);
661 /* adds one line to the output @tab */
662 static struct tt_line
*add_line(struct tt
*tt
, struct libmnt_fs
*fs
,
663 struct tt_line
*parent
)
666 struct tt_line
*line
= tt_add_line(tt
, parent
);
669 warn(_("failed to add line to output"));
672 for (i
= 0; i
< ncolumns
; i
++)
673 tt_line_set_data(line
, i
, get_data(fs
, i
));
675 tt_line_set_userdata(line
, fs
);
679 static struct tt_line
*add_tabdiff_line(struct tt
*tt
, struct libmnt_fs
*new_fs
,
680 struct libmnt_fs
*old_fs
, int change
)
683 struct tt_line
*line
= tt_add_line(tt
, NULL
);
686 warn(_("failed to add line to output"));
689 for (i
= 0; i
< ncolumns
; i
++)
690 tt_line_set_data(line
, i
,
691 get_tabdiff_data(old_fs
, new_fs
, change
, i
));
696 static int has_line(struct tt
*tt
, struct libmnt_fs
*fs
)
700 list_for_each(p
, &tt
->tb_lines
) {
701 struct tt_line
*ln
= list_entry(p
, struct tt_line
, ln_lines
);
702 if ((struct libmnt_fs
*) ln
->userdata
== fs
)
708 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
709 static int create_treenode(struct tt
*tt
, struct libmnt_table
*tb
,
710 struct libmnt_fs
*fs
, struct tt_line
*parent_line
)
712 struct libmnt_fs
*chld
= NULL
;
713 struct libmnt_iter
*itr
= NULL
;
714 struct tt_line
*line
;
718 /* first call, get root FS */
719 if (mnt_table_get_root_fs(tb
, &fs
))
723 } else if ((flags
& FL_SUBMOUNTS
) && has_line(tt
, fs
))
726 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
730 if ((flags
& FL_SUBMOUNTS
) || match_func(fs
, NULL
)) {
731 line
= add_line(tt
, fs
, parent_line
);
738 * add all children to the output table
740 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
741 if (create_treenode(tt
, tb
, chld
, line
))
751 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
752 const char *filename
, int line
)
754 warnx(_("%s: parse error at line %d"), filename
, line
);
758 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
760 files
= xrealloc(files
, sizeof(char *) * (*nfiles
+ 1));
761 files
[(*nfiles
)++] = filename
;
765 static char **append_pid_tabfile(char **files
, int *nfiles
, pid_t pid
)
769 xasprintf(&path
, "/proc/%d/mountinfo", (int) pid
);
770 return append_tabfile(files
, nfiles
, path
);
773 /* calls libmount fstab/mtab/mountinfo parser */
774 static struct libmnt_table
*parse_tabfiles(char **files
,
778 struct libmnt_table
*tb
;
781 tb
= mnt_new_table();
783 warn(_("failed to initialize libmount table"));
786 mnt_table_set_parser_errcb(tb
, parser_errcb
);
789 /* NULL means that libmount will use default paths */
790 const char *path
= nfiles
? *files
++ : NULL
;
794 rc
= mnt_table_parse_fstab(tb
, path
);
797 rc
= mnt_table_parse_mtab(tb
, path
);
801 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
802 _PATH_PROC_MOUNTINFO
:
805 rc
= mnt_table_parse_file(tb
, path
);
810 warn(_("can't read %s"), path
);
813 } while (--nfiles
> 0);
818 /* checks if @tb contains parent->child relations */
819 static int tab_is_tree(struct libmnt_table
*tb
)
821 struct libmnt_fs
*fs
= NULL
;
822 struct libmnt_iter
*itr
= NULL
;
825 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
829 if (mnt_table_next_fs(tb
, itr
, &fs
) == 0)
830 rc
= mnt_fs_get_id(fs
) > 0 && mnt_fs_get_parent_id(fs
) > 0;
837 /* filter function for libmount (mnt_table_find_next_fs()) */
838 static int match_func(struct libmnt_fs
*fs
,
839 void *data
__attribute__ ((__unused__
)))
841 int rc
= flags
& FL_INVERT
? 1 : 0;
845 m
= get_match(COL_FSTYPE
);
846 if (m
&& !mnt_fs_match_fstype(fs
, m
))
849 m
= get_match(COL_OPTIONS
);
850 if (m
&& !mnt_fs_match_options(fs
, m
))
853 md
= get_match_data(COL_MAJMIN
);
854 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
857 m
= get_match(COL_TARGET
);
858 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
861 m
= get_match(COL_SOURCE
);
862 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
865 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
866 const char *type
= mnt_fs_get_fstype(fs
);
868 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
871 if (mnt_fs_is_pseudofs(fs
))
878 /* iterate over filesystems in @tb */
879 static struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
880 struct libmnt_iter
*itr
)
882 struct libmnt_fs
*fs
= NULL
;
884 if (is_listall_mode()) {
888 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
891 } else if (is_mount_compatible_mode()) {
893 * Look up for FS in the same way how mount(8) searchs in fstab
897 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
898 mnt_iter_get_direction(itr
));
900 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
901 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
902 mnt_iter_get_direction(itr
));
905 * Look up for all matching entries
907 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
908 * findmnt [-l] <spec> [-O <options>] [-t <types>]
911 mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
);
914 !(flags
& FL_NOSWAPMATCH
) &&
915 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
917 /* swap 'spec' and target. */
918 set_match(COL_TARGET
, get_match(COL_SOURCE
));
919 set_match(COL_SOURCE
, NULL
);
920 mnt_reset_iter(itr
, -1);
930 * Filter out unwanted lines for --list output or top level lines for
931 * --submounts tree output.
933 static int add_matching_lines(struct libmnt_table
*tb
,
934 struct tt
*tt
, int direction
)
936 struct libmnt_iter
*itr
= NULL
;
937 struct libmnt_fs
*fs
;
938 int nlines
= 0, rc
= -1;
940 itr
= mnt_new_iter(direction
);
942 warn(_("failed to initialize libmount iterator"));
946 while((fs
= get_next_fs(tb
, itr
))) {
947 if ((tt_flags
& TT_FL_TREE
) || (flags
& FL_SUBMOUNTS
))
948 rc
= create_treenode(tt
, tb
, fs
, NULL
);
950 rc
= !add_line(tt
, fs
, NULL
);
954 if (flags
& FL_FIRSTONLY
)
956 flags
|= FL_NOSWAPMATCH
;
966 static int poll_match(struct libmnt_fs
*fs
)
968 int rc
= match_func(fs
, NULL
);
970 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
971 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
973 * findmnt --poll /foo
974 * The '/foo' maybe source as well as target.
976 const char *str
= get_match(COL_SOURCE
);
978 set_match(COL_TARGET
, str
); /* swap */
979 set_match(COL_SOURCE
, NULL
);
981 rc
= match_func(fs
, NULL
);
983 set_match(COL_TARGET
, NULL
); /* restore */
984 set_match(COL_SOURCE
, str
);
990 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
991 int timeout
, struct tt
*tt
, int direction
)
995 struct libmnt_iter
*itr
= NULL
;
996 struct libmnt_table
*tb_new
= NULL
;
997 struct libmnt_tabdiff
*diff
= NULL
;
998 struct pollfd fds
[1];
1000 tb_new
= mnt_new_table();
1002 warn(_("failed to initialize libmount table"));
1006 itr
= mnt_new_iter(direction
);
1008 warn(_("failed to initialize libmount iterator"));
1012 diff
= mnt_new_tabdiff();
1014 warn(_("failed to initialize libmount tabdiff"));
1018 /* cache is unnecessary to detect changes */
1019 mnt_table_set_cache(tb
, NULL
);
1020 mnt_table_set_cache(tb_new
, NULL
);
1022 f
= fopen(tabfile
, "r");
1024 warn(_("cannot open %s"), tabfile
);
1028 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
1030 fds
[0].fd
= fileno(f
);
1031 fds
[0].events
= POLLPRI
;
1034 struct libmnt_table
*tmp
;
1035 struct libmnt_fs
*old
, *new;
1038 count
= poll(fds
, 1, timeout
);
1040 break; /* timeout */
1042 warn(_("poll() failed"));
1047 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
1049 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
1054 mnt_reset_iter(itr
, direction
);
1055 while(mnt_tabdiff_next_change(
1056 diff
, itr
, &old
, &new, &change
) == 0) {
1058 if (!has_poll_action(change
))
1060 if (!poll_match(new ? new : old
))
1063 rc
= !add_tabdiff_line(tt
, new, old
, change
);
1066 if (flags
& FL_FIRSTONLY
)
1071 rc
= tt_print_table(tt
);
1081 tt_remove_lines(tt
);
1082 mnt_reset_table(tb_new
);
1084 if (count
&& (flags
& FL_FIRSTONLY
))
1090 mnt_free_table(tb_new
);
1091 mnt_free_tabdiff(diff
);
1098 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1102 fputs(USAGE_HEADER
, out
);
1105 " %1$s [options] <device> | <mountpoint>\n"
1106 " %1$s [options] <device> <mountpoint>\n"
1107 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
1108 program_invocation_short_name
);
1112 " -s, --fstab search in static table of filesystems\n"
1113 " -m, --mtab search in table of mounted filesystems\n"
1114 " -k, --kernel search in kernel table of mounted\n"
1115 " filesystems (default)\n\n"));
1118 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
1119 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
1122 " -A, --all disable all built-in filters, print all filesystems\n"
1123 " -a, --ascii use ASCII chars for tree formatting\n"
1124 " -c, --canonicalize canonicalize printed paths\n"
1125 " -D, --df imitate the output of df(1)\n"
1126 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
1127 " -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1128 " to device names\n"
1129 " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n"
1130 " -f, --first-only print the first found filesystem only\n"));
1133 " -i, --invert invert the sense of matching\n"
1134 " -l, --list use list format output\n"
1135 " -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"
1136 " -n, --noheadings don't print column headings\n"
1137 " -u, --notruncate don't truncate text in columns\n"));
1138 fputs (_(" -O, --options <list> limit the set of filesystems by mount options\n"), out
);
1139 fputs (_(" -o, --output <list> the output columns to be shown\n"), out
);
1140 fputs (_(" -P, --pairs use key=\"value\" output format\n"), out
);
1141 fputs (_(" -r, --raw use raw output format\n"), out
);
1142 fputs (_(" -t, --types <list> limit the set of filesystems by FS types\n"), out
);
1144 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
1145 " -R, --submounts print all submounts for the matching filesystems\n"
1146 " -S, --source <string> the device to mount (by name, maj:min, \n"
1147 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"
1148 " -T, --target <string> the mountpoint to use\n"));
1150 fputs(USAGE_SEPARATOR
, out
);
1151 fputs(USAGE_HELP
, out
);
1152 fputs(USAGE_VERSION
, out
);
1154 fprintf(out
, _("\nAvailable columns:\n"));
1156 for (i
= 0; i
< FINDMNT_NCOLUMNS
; i
++)
1157 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1159 fprintf(out
, USAGE_MAN_TAIL("findmnt(8)"));
1161 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1164 int main(int argc
, char *argv
[])
1166 struct libmnt_table
*tb
= NULL
;
1167 char **tabfiles
= NULL
;
1168 int direction
= MNT_ITER_FORWARD
;
1169 int i
, c
, rc
= -1, timeout
= -1;
1170 int ntabfiles
= 0, tabtype
= 0;
1171 char *outarg
= NULL
;
1173 struct tt
*tt
= NULL
;
1175 static const struct option longopts
[] = {
1176 { "all", 0, 0, 'A' },
1177 { "ascii", 0, 0, 'a' },
1178 { "canonicalize", 0, 0, 'c' },
1179 { "direction", 1, 0, 'd' },
1180 { "df", 0, 0, 'D' },
1181 { "evaluate", 0, 0, 'e' },
1182 { "first-only", 0, 0, 'f' },
1183 { "fstab", 0, 0, 's' },
1184 { "help", 0, 0, 'h' },
1185 { "invert", 0, 0, 'i' },
1186 { "kernel", 0, 0, 'k' },
1187 { "list", 0, 0, 'l' },
1188 { "mtab", 0, 0, 'm' },
1189 { "noheadings", 0, 0, 'n' },
1190 { "notruncate", 0, 0, 'u' },
1191 { "options", 1, 0, 'O' },
1192 { "output", 1, 0, 'o' },
1193 { "poll", 2, 0, 'p' },
1194 { "pairs", 0, 0, 'P' },
1195 { "raw", 0, 0, 'r' },
1196 { "types", 1, 0, 't' },
1197 { "nofsroot", 0, 0, 'v' },
1198 { "submounts", 0, 0, 'R' },
1199 { "source", 1, 0, 'S' },
1200 { "tab-file", 1, 0, 'F' },
1201 { "task", 1, 0, 'N' },
1202 { "target", 1, 0, 'T' },
1203 { "timeout", 1, 0, 'w' },
1204 { "version", 0, 0, 'V' },
1209 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1210 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1211 { 'P','l','r' }, /* pairs,list,raw */
1212 { 'm','p','s' }, /* mtab,poll,fstab */
1215 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1217 assert(ARRAY_SIZE(columns
) == FINDMNT_NCOLUMNS
);
1219 setlocale(LC_ALL
, "");
1220 bindtextdomain(PACKAGE
, LOCALEDIR
);
1221 textdomain(PACKAGE
);
1222 atexit(close_stdout
);
1224 /* default output format */
1225 tt_flags
|= TT_FL_TREE
;
1227 while ((c
= getopt_long(argc
, argv
,
1228 "AacDd:ehifF:o:O:p::PklmnN:rst:uvRS:T:w:V",
1229 longopts
, NULL
)) != -1) {
1231 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1238 tt_flags
|= TT_FL_ASCII
;
1241 flags
|= FL_CANONICALIZE
;
1244 tt_flags
&= ~TT_FL_TREE
;
1248 if (!strcmp(optarg
, "forward"))
1249 direction
= MNT_ITER_FORWARD
;
1250 else if (!strcmp(optarg
, "backward"))
1251 direction
= MNT_ITER_BACKWARD
;
1254 _("unknown direction '%s'"), optarg
);
1257 flags
|= FL_EVALUATE
;
1266 flags
|= FL_FIRSTONLY
;
1269 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1272 disable_columns_truncate();
1278 set_match(COL_OPTIONS
, optarg
);
1282 nactions
= string_to_idarray(optarg
,
1283 actions
, ARRAY_SIZE(actions
),
1284 poll_action_name_to_id
);
1289 tt_flags
&= ~TT_FL_TREE
;
1292 tt_flags
|= TT_FL_EXPORT
;
1293 tt_flags
&= ~TT_FL_TREE
;
1295 case 'm': /* mtab */
1296 tabtype
= TABTYPE_MTAB
;
1297 tt_flags
&= ~TT_FL_TREE
;
1299 case 's': /* fstab */
1300 tabtype
= TABTYPE_FSTAB
;
1301 tt_flags
&= ~TT_FL_TREE
;
1303 case 'k': /* kernel (mountinfo) */
1304 tabtype
= TABTYPE_KERNEL
;
1307 set_match(COL_FSTYPE
, optarg
);
1310 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1311 tt_flags
|= TT_FL_RAW
; /* enable raw */
1314 tt_flags
&= ~TT_FL_TREE
; /* disable the default */
1317 tt_flags
|= TT_FL_NOHEADINGS
;
1320 tabtype
= TABTYPE_KERNEL
;
1321 tabfiles
= append_pid_tabfile(tabfiles
, &ntabfiles
,
1322 strtou32_or_err(optarg
,
1323 _("invalid TID argument")));
1326 flags
|= FL_NOFSROOT
;
1329 flags
|= FL_SUBMOUNTS
;
1332 set_source_match(optarg
);
1333 flags
|= FL_NOSWAPMATCH
;
1336 set_match(COL_TARGET
, optarg
);
1337 flags
|= FL_NOSWAPMATCH
;
1340 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1343 printf(UTIL_LINUX_VERSION
);
1344 return EXIT_SUCCESS
;
1351 if (!ncolumns
&& (flags
& FL_DF
)) {
1352 columns
[ncolumns
++] = COL_SOURCE
;
1353 columns
[ncolumns
++] = COL_FSTYPE
;
1354 columns
[ncolumns
++] = COL_SIZE
;
1355 columns
[ncolumns
++] = COL_USED
;
1356 columns
[ncolumns
++] = COL_AVAIL
;
1357 columns
[ncolumns
++] = COL_USEPERC
;
1358 columns
[ncolumns
++] = COL_TARGET
;
1361 /* default columns */
1363 if (flags
& FL_POLL
)
1364 columns
[ncolumns
++] = COL_ACTION
;
1366 columns
[ncolumns
++] = COL_TARGET
;
1367 columns
[ncolumns
++] = COL_SOURCE
;
1368 columns
[ncolumns
++] = COL_FSTYPE
;
1369 columns
[ncolumns
++] = COL_OPTIONS
;
1372 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1373 &ncolumns
, column_name_to_id
) < 0)
1374 return EXIT_FAILURE
;
1377 tabtype
= TABTYPE_KERNEL
;
1379 if ((flags
& FL_POLL
) && ntabfiles
> 1)
1380 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1382 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1383 errx(EXIT_FAILURE
, _(
1384 "options --target and --source can't be used together "
1385 "with command line element that is not an option"));
1388 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1390 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1392 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1393 /* don't care about submounts if list all mounts */
1394 flags
&= ~FL_SUBMOUNTS
;
1396 if (!(flags
& FL_SUBMOUNTS
) && ((flags
& FL_FIRSTONLY
)
1397 || get_match(COL_TARGET
)
1398 || get_match(COL_SOURCE
)
1399 || get_match(COL_MAJMIN
)))
1400 tt_flags
&= ~TT_FL_TREE
;
1402 if (!(flags
& FL_NOSWAPMATCH
) &&
1403 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1405 * Check if we can swap source and target, it's
1406 * not possible if the source is LABEL=/UUID=
1408 const char *x
= get_match(COL_SOURCE
);
1410 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1411 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1412 flags
|= FL_NOSWAPMATCH
;
1416 * initialize libmount
1420 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1424 if ((tt_flags
& TT_FL_TREE
) && (ntabfiles
> 1 || !tab_is_tree(tb
)))
1425 tt_flags
&= ~TT_FL_TREE
;
1427 cache
= mnt_new_cache();
1429 warn(_("failed to initialize libmount cache"));
1432 mnt_table_set_cache(tb
, cache
);
1436 * initialize output formatting (tt.h)
1438 tt
= tt_new_table(tt_flags
);
1440 warn(_("failed to initialize output table"));
1444 for (i
= 0; i
< ncolumns
; i
++) {
1445 int fl
= get_column_flags(i
);
1446 int id
= get_column_id(i
);
1448 if (!(tt_flags
& TT_FL_TREE
))
1451 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1452 warnx(_("%s column is requested, but --poll "
1453 "is not enabled"), get_column_name(i
));
1456 if (!tt_define_column(tt
, get_column_name(i
),
1457 get_column_whint(i
), fl
)) {
1458 warn(_("failed to initialize output column"));
1464 * Fill in data to the output table
1466 if (flags
& FL_POLL
) {
1467 /* poll mode (accept the first tabfile only) */
1468 rc
= poll_table(tb
, tabfiles
? *tabfiles
: _PATH_PROC_MOUNTINFO
, timeout
, tt
, direction
);
1470 } else if ((tt_flags
& TT_FL_TREE
) && !(flags
& FL_SUBMOUNTS
)) {
1472 rc
= create_treenode(tt
, tb
, NULL
, NULL
);
1474 /* whole lits of sub-tree */
1475 rc
= add_matching_lines(tb
, tt
, direction
);
1478 && tabtype
== TABTYPE_KERNEL
1479 && (flags
& FL_NOSWAPMATCH
)
1480 && get_match(COL_TARGET
)) {
1482 * Found nothing, maybe the --target is regular file,
1483 * try it again with extra functionality for target
1486 enable_extra_target_match();
1487 rc
= add_matching_lines(tb
, tt
, direction
);
1492 * Print the output table for non-poll modes
1494 if (!rc
&& !(flags
& FL_POLL
))
1500 mnt_unref_cache(cache
);
1505 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;