4 * Copyright (C) 2010-2014 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>
39 #include <libsmartcols.h>
41 #include "pathnames.h"
43 #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),
64 FL_NOCACHE
= (1 << 14),
66 /* basic table settings */
69 FL_NOHEADINGS
= (1 << 22),
70 FL_EXPORT
= (1 << 23),
111 const char *name
; /* header */
112 double whint
; /* width hint (N < 1 is in percent of termwidth) */
113 int flags
; /* libsmartcols flags */
114 const char *help
; /* column description */
115 const char *match
; /* pattern for match_func() */
116 void *match_data
; /* match specific data */
119 /* columns descriptions (don't use const, this is writable) */
120 static struct colinfo infos
[] = {
121 [COL_SOURCE
] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES
, N_("source device") },
122 [COL_TARGET
] = { "TARGET", 0.30, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("mountpoint") },
123 [COL_FSTYPE
] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC
, N_("filesystem type") },
124 [COL_OPTIONS
] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("all mount options") },
125 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC
, N_("VFS specific mount options") },
126 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("FS specific mount options") },
127 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
128 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
129 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
130 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
131 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
132 [COL_ACTION
] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH
, N_("action detected by --poll") },
133 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("old mount options saved by --poll") },
134 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
135 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size") },
136 [COL_AVAIL
] = { "AVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available") },
137 [COL_USED
] = { "USED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used") },
138 [COL_USEPERC
] = { "USE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
139 [COL_FSROOT
] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES
, N_("filesystem root") },
140 [COL_TID
] = { "TID", 4, SCOLS_FL_RIGHT
, N_("task ID") },
141 [COL_ID
] = { "ID", 2, SCOLS_FL_RIGHT
, N_("mount ID") },
142 [COL_OPT_FIELDS
] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC
, N_("optional mount fields") },
143 [COL_PROPAGATION
] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
144 [COL_FREQ
] = { "FREQ", 1, SCOLS_FL_RIGHT
, N_("dump(8) period in days [fstab only]") },
145 [COL_PASSNO
] = { "PASSNO", 1, SCOLS_FL_RIGHT
, N_("pass number on parallel fsck(8) [fstab only]") }
148 /* columns[] array specifies all currently wanted output column. The columns
149 * are defined by infos[] array and you can specify (on command line) each
150 * column twice. That's enough, dynamically allocated array of the columns is
151 * unnecessary overkill and over-engineering in this case */
152 static int columns
[ARRAY_SIZE(infos
) * 2];
155 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
158 errx(EXIT_FAILURE
, _("too many columns specified, "
159 "the limit is %zu columns"),
164 #define add_column(ary, n, id) \
165 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
171 /* poll actions (parsed --poll=<list> */
172 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
173 static int actions
[FINDMNT_NACTIONS
];
177 static struct libmnt_cache
*cache
;
183 static int match_func(struct libmnt_fs
*fs
, void *data
__attribute__ ((__unused__
)));
186 static int get_column_id(int num
)
188 assert(num
< ncolumns
);
189 assert((size_t) columns
[num
] < ARRAY_SIZE(infos
));
193 static struct colinfo
*get_column_info(int num
)
195 return &infos
[ get_column_id(num
) ];
198 static const char *column_id_to_name(int id
)
200 assert((size_t) id
< ARRAY_SIZE(infos
));
201 return infos
[id
].name
;
204 static const char *get_column_name(int num
)
206 return get_column_info(num
)->name
;
209 static float get_column_whint(int num
)
211 return get_column_info(num
)->whint
;
214 static int get_column_flags(int num
)
216 return get_column_info(num
)->flags
;
219 static const char *get_match(int id
)
221 assert((size_t) id
< ARRAY_SIZE(infos
));
222 return infos
[id
].match
;
225 static void *get_match_data(int id
)
227 assert((size_t) id
< ARRAY_SIZE(infos
));
228 return infos
[id
].match_data
;
231 static void set_match(int id
, const char *match
)
233 assert((size_t) id
< ARRAY_SIZE(infos
));
234 infos
[id
].match
= match
;
237 static void set_match_data(int id
, void *data
)
239 assert((size_t) id
< ARRAY_SIZE(infos
));
240 infos
[id
].match_data
= data
;
244 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
247 static void set_source_match(const char *data
)
251 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
252 dev_t
*devno
= xmalloc(sizeof(dev_t
));
254 *devno
= makedev(maj
, min
);
255 set_match(COL_MAJMIN
, data
);
256 set_match_data(COL_MAJMIN
, (void *) devno
);
257 flags
|= FL_NOSWAPMATCH
;
259 set_match(COL_SOURCE
, data
);
262 /* @tb has to be from kernel (so no fstab or so)! */
263 static void enable_extra_target_match(struct libmnt_table
*tb
)
266 const char *tgt
= NULL
, *mnt
= NULL
;
267 struct libmnt_fs
*fs
;
270 * Check if match pattern is mountpoint, if not use the
273 if (flags
& FL_NOCACHE
)
274 tgt
= get_match(COL_TARGET
);
276 tgt
= cn
= mnt_resolve_path(get_match(COL_TARGET
), cache
);
281 fs
= mnt_table_find_mountpoint(tb
, tgt
, MNT_ITER_BACKWARD
);
283 mnt
= mnt_fs_get_target(fs
);
284 if (mnt
&& strcmp(mnt
, tgt
) != 0)
285 set_match(COL_TARGET
, xstrdup(mnt
)); /* replace the current setting */
292 static int is_tabdiff_column(int id
)
294 assert((size_t) id
< ARRAY_SIZE(infos
));
299 case COL_OLD_OPTIONS
:
308 * "findmnt" without any filter
310 static int is_listall_mode(void)
312 if ((flags
& FL_DF
) && !(flags
& FL_ALL
))
315 return (!get_match(COL_SOURCE
) &&
316 !get_match(COL_TARGET
) &&
317 !get_match(COL_FSTYPE
) &&
318 !get_match(COL_OPTIONS
) &&
319 !get_match(COL_MAJMIN
));
323 * Returns 1 if the @act is in the --poll=<list>
325 static int has_poll_action(int act
)
330 return 1; /* all actions enabled */
331 for (i
= 0; i
< nactions
; i
++)
332 if (actions
[i
] == act
)
337 static int poll_action_name_to_id(const char *name
, size_t namesz
)
341 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
342 id
= MNT_TABDIFF_MOVE
;
343 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
344 id
= MNT_TABDIFF_MOUNT
;
345 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
346 id
= MNT_TABDIFF_UMOUNT
;
347 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
348 id
= MNT_TABDIFF_REMOUNT
;
350 warnx(_("unknown action: %s"), name
);
356 * findmnt --first-only <devname|TAG=|mountpoint>
358 * ... it works like "mount <devname|TAG=|mountpoint>"
360 static int is_mount_compatible_mode(void)
362 if (!get_match(COL_SOURCE
))
363 return 0; /* <devname|TAG=|mountpoint> is required */
364 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
365 return 0; /* cannot be restricted by -t or -O */
366 if (!(flags
& FL_FIRSTONLY
))
367 return 0; /* we have to return the first entry only */
372 static void disable_columns_truncate(void)
376 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
377 infos
[i
].flags
&= ~SCOLS_FL_TRUNC
;
381 * converts @name to column ID
383 static int column_name_to_id(const char *name
, size_t namesz
)
387 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
388 const char *cn
= column_id_to_name(i
);
390 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
393 warnx(_("unknown column: %s"), name
);
399 static char *get_tag_from_udev(const char *devname
, int col
)
401 struct udev_device
*dev
;
402 const char *data
= NULL
;
403 char *res
= NULL
, *path
;
410 /* libudev don't like /dev/mapper/ symlinks */
411 path
= realpath(devname
, NULL
);
415 if (strncmp(devname
, "/dev/", 5) == 0)
418 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", devname
);
426 data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC");
429 data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC");
432 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID");
435 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME");
443 unhexmangle_string(res
);
446 udev_device_unref(dev
);
449 #endif /* HAVE_LIBUDEV */
451 /* Returns LABEL or UUID */
452 static char *get_tag(struct libmnt_fs
*fs
, const char *tagname
, int col
454 __attribute__((__unused__
))
461 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
464 const char *dev
= mnt_fs_get_source(fs
);
466 if (dev
&& !(flags
& FL_NOCACHE
))
467 dev
= mnt_resolve_spec(dev
, cache
);
470 res
= get_tag_from_udev(dev
, col
);
473 res
= mnt_cache_find_tag_value(cache
, dev
, tagname
);
475 /* don't return pointer to cache */
483 static char *get_vfs_attr(struct libmnt_fs
*fs
, int sizetype
)
486 uint64_t vfs_attr
= 0;
489 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
494 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
497 vfs_attr
= buf
.f_frsize
* buf
.f_bavail
;
500 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
503 if (buf
.f_blocks
== 0)
506 xasprintf(&sizestr
, "%.0f%%",
507 (double)(buf
.f_blocks
- buf
.f_bfree
) /
513 sizestr
= xstrdup("0");
514 else if (flags
& FL_BYTES
)
515 xasprintf(&sizestr
, "%ju", vfs_attr
);
517 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
522 /* reads FS data from libmount
524 static char *get_data(struct libmnt_fs
*fs
, int num
)
527 int col_id
= get_column_id(num
);
532 const char *root
= mnt_fs_get_root(fs
);
533 const char *spec
= mnt_fs_get_srcpath(fs
);
536 if (spec
&& (flags
& FL_CANONICALIZE
))
537 spec
= cn
= mnt_resolve_path(spec
, cache
);
539 spec
= mnt_fs_get_source(fs
);
541 if (spec
&& (flags
& FL_EVALUATE
))
542 spec
= cn
= mnt_resolve_spec(spec
, cache
);
544 if (root
&& spec
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/"))
545 xasprintf(&str
, "%s[%s]", spec
, root
);
553 str
= xstrdup(mnt_fs_get_target(fs
));
556 str
= xstrdup(mnt_fs_get_fstype(fs
));
559 str
= xstrdup(mnt_fs_get_options(fs
));
561 case COL_VFS_OPTIONS
:
562 str
= xstrdup(mnt_fs_get_vfs_options(fs
));
565 str
= xstrdup(mnt_fs_get_fs_options(fs
));
568 str
= xstrdup(mnt_fs_get_optional_fields(fs
));
571 str
= get_tag(fs
, "UUID", col_id
);
574 str
= get_tag(fs
, "PARTUUID", col_id
);
577 str
= get_tag(fs
, "LABEL", col_id
);
580 str
= get_tag(fs
, "PARTLABEL", col_id
);
585 dev_t devno
= mnt_fs_get_devno(fs
);
589 if ((flags
& FL_RAW
) || (flags
& FL_EXPORT
))
590 xasprintf(&str
, "%u:%u", major(devno
), minor(devno
));
592 xasprintf(&str
, "%3u:%-3u", major(devno
), minor(devno
));
599 str
= get_vfs_attr(fs
, col_id
);
602 str
= xstrdup(mnt_fs_get_root(fs
));
605 if (mnt_fs_get_tid(fs
))
606 xasprintf(&str
, "%d", mnt_fs_get_tid(fs
));
609 if (mnt_fs_get_id(fs
))
610 xasprintf(&str
, "%d", mnt_fs_get_id(fs
));
612 case COL_PROPAGATION
:
613 if (mnt_fs_is_kernel(fs
)) {
614 unsigned long fl
= 0;
617 if (mnt_fs_get_propagation(fs
, &fl
) != 0)
620 n
= xstrdup((fl
& MS_SHARED
) ? "shared" : "private");
623 xasprintf(&str
, "%s,slave", n
);
627 if (fl
& MS_UNBINDABLE
) {
628 xasprintf(&str
, "%s,unbindable", n
);
636 if (!mnt_fs_is_kernel(fs
))
637 xasprintf(&str
, "%d", mnt_fs_get_freq(fs
));
640 if (!mnt_fs_is_kernel(fs
))
641 xasprintf(&str
, "%d", mnt_fs_get_passno(fs
));
649 static char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
650 struct libmnt_fs
*new_fs
,
656 switch (get_column_id(num
)) {
659 case MNT_TABDIFF_MOUNT
:
662 case MNT_TABDIFF_UMOUNT
:
665 case MNT_TABDIFF_REMOUNT
:
668 case MNT_TABDIFF_MOVE
:
677 case COL_OLD_OPTIONS
:
678 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
679 change
== MNT_TABDIFF_UMOUNT
))
680 str
= xstrdup(mnt_fs_get_options(old_fs
));
683 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
684 change
== MNT_TABDIFF_UMOUNT
))
685 str
= xstrdup(mnt_fs_get_target(old_fs
));
689 str
= get_data(new_fs
, num
);
691 str
= get_data(old_fs
, num
);
697 /* adds one line to the output @tab */
698 static struct libscols_line
*add_line(struct libscols_table
*table
, struct libmnt_fs
*fs
,
699 struct libscols_line
*parent
)
702 struct libscols_line
*line
= scols_table_new_line(table
, parent
);
705 warn(_("failed to add line to output"));
708 for (i
= 0; i
< ncolumns
; i
++)
709 scols_line_refer_data(line
, i
, get_data(fs
, i
));
711 scols_line_set_userdata(line
, fs
);
715 static struct libscols_line
*add_tabdiff_line(struct libscols_table
*table
, struct libmnt_fs
*new_fs
,
716 struct libmnt_fs
*old_fs
, int change
)
719 struct libscols_line
*line
= scols_table_new_line(table
, NULL
);
722 warn(_("failed to add line to output"));
725 for (i
= 0; i
< ncolumns
; i
++)
726 scols_line_refer_data(line
, i
,
727 get_tabdiff_data(old_fs
, new_fs
, change
, i
));
732 static int has_line(struct libscols_table
*table
, struct libmnt_fs
*fs
)
734 struct libscols_line
*ln
;
735 struct libscols_iter
*itr
;
738 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
742 while (scols_table_next_line(table
, itr
, &ln
) == 0) {
743 if ((struct libmnt_fs
*) scols_line_get_userdata(ln
) == fs
) {
749 scols_free_iter(itr
);
753 /* reads filesystems from @tb (libmount) and fillin @table (output table) */
754 static int create_treenode(struct libscols_table
*table
, struct libmnt_table
*tb
,
755 struct libmnt_fs
*fs
, struct libscols_line
*parent_line
)
757 struct libmnt_fs
*chld
= NULL
;
758 struct libmnt_iter
*itr
= NULL
;
759 struct libscols_line
*line
;
763 /* first call, get root FS */
764 if (mnt_table_get_root_fs(tb
, &fs
))
768 } else if ((flags
& FL_SUBMOUNTS
) && has_line(table
, fs
))
771 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
775 if ((flags
& FL_SUBMOUNTS
) || match_func(fs
, NULL
)) {
776 line
= add_line(table
, fs
, parent_line
);
783 * add all children to the output table
785 while(mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
786 if (create_treenode(table
, tb
, chld
, line
))
796 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
797 const char *filename
, int line
)
799 warnx(_("%s: parse error at line %d"), filename
, line
);
803 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
805 files
= xrealloc(files
, sizeof(char *) * (*nfiles
+ 1));
806 files
[(*nfiles
)++] = filename
;
810 static char **append_pid_tabfile(char **files
, int *nfiles
, pid_t pid
)
814 xasprintf(&path
, "/proc/%d/mountinfo", (int) pid
);
815 return append_tabfile(files
, nfiles
, path
);
818 /* calls libmount fstab/mtab/mountinfo parser */
819 static struct libmnt_table
*parse_tabfiles(char **files
,
823 struct libmnt_table
*tb
;
826 tb
= mnt_new_table();
828 warn(_("failed to initialize libmount table"));
831 mnt_table_set_parser_errcb(tb
, parser_errcb
);
834 /* NULL means that libmount will use default paths */
835 const char *path
= nfiles
? *files
++ : NULL
;
839 rc
= mnt_table_parse_fstab(tb
, path
);
842 rc
= mnt_table_parse_mtab(tb
, path
);
846 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
847 _PATH_PROC_MOUNTINFO
:
850 rc
= mnt_table_parse_file(tb
, path
);
855 warn(_("can't read %s"), path
);
858 } while (--nfiles
> 0);
864 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
865 * necessary if @tb in main() was read from a non-kernel source.
867 static void cache_set_targets(struct libmnt_cache
*tmp
)
869 struct libmnt_table
*tb
;
872 tb
= mnt_new_table();
876 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
877 _PATH_PROC_MOUNTINFO
:
880 if (mnt_table_parse_file(tb
, path
) == 0)
881 mnt_cache_set_targets(tmp
, tb
);
886 /* checks if @tb contains parent->child relations */
887 static int tab_is_tree(struct libmnt_table
*tb
)
889 struct libmnt_fs
*fs
= NULL
;
890 struct libmnt_iter
*itr
= NULL
;
893 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
897 rc
= (mnt_table_next_fs(tb
, itr
, &fs
) == 0 &&
898 mnt_fs_is_kernel(fs
) &&
899 mnt_fs_get_root(fs
));
905 /* checks if all fs in @tb are from kernel */
906 static int tab_is_kernel(struct libmnt_table
*tb
)
908 struct libmnt_fs
*fs
= NULL
;
909 struct libmnt_iter
*itr
= NULL
;
911 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
915 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
916 if (!mnt_fs_is_kernel(fs
)) {
926 /* filter function for libmount (mnt_table_find_next_fs()) */
927 static int match_func(struct libmnt_fs
*fs
,
928 void *data
__attribute__ ((__unused__
)))
930 int rc
= flags
& FL_INVERT
? 1 : 0;
934 m
= get_match(COL_FSTYPE
);
935 if (m
&& !mnt_fs_match_fstype(fs
, m
))
938 m
= get_match(COL_OPTIONS
);
939 if (m
&& !mnt_fs_match_options(fs
, m
))
942 md
= get_match_data(COL_MAJMIN
);
943 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
946 m
= get_match(COL_TARGET
);
947 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
950 m
= get_match(COL_SOURCE
);
951 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
954 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
955 const char *type
= mnt_fs_get_fstype(fs
);
957 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
960 if (mnt_fs_is_pseudofs(fs
))
967 /* iterate over filesystems in @tb */
968 static struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
969 struct libmnt_iter
*itr
)
971 struct libmnt_fs
*fs
= NULL
;
973 if (is_listall_mode()) {
977 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
980 } else if (is_mount_compatible_mode()) {
982 * Look up for FS in the same way how mount(8) searchs in fstab
986 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
987 mnt_iter_get_direction(itr
));
989 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
990 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
991 mnt_iter_get_direction(itr
));
994 * Look up for all matching entries
996 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
997 * findmnt [-l] <spec> [-O <options>] [-t <types>]
1000 mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
);
1003 !(flags
& FL_NOSWAPMATCH
) &&
1004 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1006 /* swap 'spec' and target. */
1007 set_match(COL_TARGET
, get_match(COL_SOURCE
));
1008 set_match(COL_SOURCE
, NULL
);
1009 mnt_reset_iter(itr
, -1);
1019 * Filter out unwanted lines for --list output or top level lines for
1020 * --submounts tree output.
1022 static int add_matching_lines(struct libmnt_table
*tb
,
1023 struct libscols_table
*table
, int direction
)
1025 struct libmnt_iter
*itr
= NULL
;
1026 struct libmnt_fs
*fs
;
1027 int nlines
= 0, rc
= -1;
1029 itr
= mnt_new_iter(direction
);
1031 warn(_("failed to initialize libmount iterator"));
1035 while((fs
= get_next_fs(tb
, itr
))) {
1036 if ((flags
& FL_TREE
) || (flags
& FL_SUBMOUNTS
))
1037 rc
= create_treenode(table
, tb
, fs
, NULL
);
1039 rc
= !add_line(table
, fs
, NULL
);
1043 if (flags
& FL_FIRSTONLY
)
1045 flags
|= FL_NOSWAPMATCH
;
1055 static int poll_match(struct libmnt_fs
*fs
)
1057 int rc
= match_func(fs
, NULL
);
1059 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
1060 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
1062 * findmnt --poll /foo
1063 * The '/foo' maybe source as well as target.
1065 const char *str
= get_match(COL_SOURCE
);
1067 set_match(COL_TARGET
, str
); /* swap */
1068 set_match(COL_SOURCE
, NULL
);
1070 rc
= match_func(fs
, NULL
);
1072 set_match(COL_TARGET
, NULL
); /* restore */
1073 set_match(COL_SOURCE
, str
);
1079 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
1080 int timeout
, struct libscols_table
*table
, int direction
)
1084 struct libmnt_iter
*itr
= NULL
;
1085 struct libmnt_table
*tb_new
= NULL
;
1086 struct libmnt_tabdiff
*diff
= NULL
;
1087 struct pollfd fds
[1];
1089 tb_new
= mnt_new_table();
1091 warn(_("failed to initialize libmount table"));
1095 itr
= mnt_new_iter(direction
);
1097 warn(_("failed to initialize libmount iterator"));
1101 diff
= mnt_new_tabdiff();
1103 warn(_("failed to initialize libmount tabdiff"));
1107 /* cache is unnecessary to detect changes */
1108 mnt_table_set_cache(tb
, NULL
);
1109 mnt_table_set_cache(tb_new
, NULL
);
1111 f
= fopen(tabfile
, "r");
1113 warn(_("cannot open %s"), tabfile
);
1117 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
1119 fds
[0].fd
= fileno(f
);
1120 fds
[0].events
= POLLPRI
;
1123 struct libmnt_table
*tmp
;
1124 struct libmnt_fs
*old
, *new;
1127 count
= poll(fds
, 1, timeout
);
1129 break; /* timeout */
1131 warn(_("poll() failed"));
1136 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
1138 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
1143 mnt_reset_iter(itr
, direction
);
1144 while(mnt_tabdiff_next_change(
1145 diff
, itr
, &old
, &new, &change
) == 0) {
1147 if (!has_poll_action(change
))
1149 if (!poll_match(new ? new : old
))
1152 rc
= !add_tabdiff_line(table
, new, old
, change
);
1155 if (flags
& FL_FIRSTONLY
)
1160 rc
= scols_print_table(table
);
1170 scols_table_remove_lines(table
);
1171 mnt_reset_table(tb_new
);
1173 if (count
&& (flags
& FL_FIRSTONLY
))
1179 mnt_unref_table(tb_new
);
1180 mnt_free_tabdiff(diff
);
1187 static int uniq_fs_target_cmp(
1188 struct libmnt_table
*tb
__attribute__((__unused__
)),
1189 struct libmnt_fs
*a
,
1190 struct libmnt_fs
*b
)
1192 return !mnt_fs_match_target(a
, mnt_fs_get_target(b
), cache
);
1195 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1199 fputs(USAGE_HEADER
, out
);
1202 " %1$s [options] <device> | <mountpoint>\n"
1203 " %1$s [options] <device> <mountpoint>\n"
1204 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
1205 program_invocation_short_name
);
1207 fputs(USAGE_SEPARATOR
, out
);
1208 fputs(_("Find a (mounted) filesystem.\n"), out
);
1210 fputs(USAGE_OPTIONS
, out
);
1211 fputs(_(" -s, --fstab search in static table of filesystems\n"), out
);
1212 fputs(_(" -m, --mtab search in table of mounted filesystems\n"), out
);
1213 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1214 " filesystems (default)\n"), out
);
1216 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out
);
1217 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out
);
1220 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out
);
1221 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out
);
1222 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
1223 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out
);
1224 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out
);
1225 fputs(_(" -D, --df imitate the output of df(1)\n"), out
);
1226 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out
);
1227 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1228 " to device names\n"), out
);
1229 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out
);
1230 fputs(_(" -f, --first-only print the first found filesystem only\n"), out
);
1231 fputs(_(" -i, --invert invert the sense of matching\n"), out
);
1232 fputs(_(" -l, --list use list format output\n"), out
);
1233 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out
);
1234 fputs(_(" -n, --noheadings don't print column headings\n"), out
);
1235 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out
);
1236 fputs(_(" -o, --output <list> the output columns to be shown\n"), out
);
1237 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1238 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out
);
1239 fputs(_(" -r, --raw use raw output format\n"), out
);
1240 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1241 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out
);
1242 fputs(_(" -T, --target <string> the mountpoint to use\n"), out
);
1243 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out
);
1244 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out
);
1245 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
1246 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out
);
1248 fputs(USAGE_SEPARATOR
, out
);
1249 fputs(USAGE_HELP
, out
);
1250 fputs(USAGE_VERSION
, out
);
1252 fprintf(out
, _("\nAvailable columns:\n"));
1254 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
1255 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
1257 fprintf(out
, USAGE_MAN_TAIL("findmnt(8)"));
1259 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1262 int main(int argc
, char *argv
[])
1264 struct libmnt_table
*tb
= NULL
;
1265 char **tabfiles
= NULL
;
1266 int direction
= MNT_ITER_FORWARD
;
1267 int i
, c
, rc
= -1, timeout
= -1;
1268 int ntabfiles
= 0, tabtype
= 0;
1269 char *outarg
= NULL
;
1271 struct libscols_table
*table
= NULL
;
1273 static const struct option longopts
[] = {
1274 { "all", 0, 0, 'A' },
1275 { "ascii", 0, 0, 'a' },
1276 { "bytes", 0, 0, 'b' },
1277 { "canonicalize", 0, 0, 'c' },
1278 { "direction", 1, 0, 'd' },
1279 { "df", 0, 0, 'D' },
1280 { "evaluate", 0, 0, 'e' },
1281 { "first-only", 0, 0, 'f' },
1282 { "fstab", 0, 0, 's' },
1283 { "help", 0, 0, 'h' },
1284 { "invert", 0, 0, 'i' },
1285 { "kernel", 0, 0, 'k' },
1286 { "list", 0, 0, 'l' },
1287 { "mtab", 0, 0, 'm' },
1288 { "noheadings", 0, 0, 'n' },
1289 { "notruncate", 0, 0, 'u' },
1290 { "options", 1, 0, 'O' },
1291 { "output", 1, 0, 'o' },
1292 { "poll", 2, 0, 'p' },
1293 { "pairs", 0, 0, 'P' },
1294 { "raw", 0, 0, 'r' },
1295 { "types", 1, 0, 't' },
1296 { "nocanonicalize", 0, 0, 'C' },
1297 { "nofsroot", 0, 0, 'v' },
1298 { "submounts", 0, 0, 'R' },
1299 { "source", 1, 0, 'S' },
1300 { "tab-file", 1, 0, 'F' },
1301 { "task", 1, 0, 'N' },
1302 { "target", 1, 0, 'T' },
1303 { "timeout", 1, 0, 'w' },
1304 { "uniq", 0, 0, 'U' },
1305 { "version", 0, 0, 'V' },
1310 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
1311 { 'C', 'c'}, /* [no]canonicalize */
1312 { 'C', 'e' }, /* nocanonicalize, evaluate */
1313 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1314 { 'P','l','r' }, /* pairs,list,raw */
1315 { 'm','p','s' }, /* mtab,poll,fstab */
1318 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1320 setlocale(LC_ALL
, "");
1321 bindtextdomain(PACKAGE
, LOCALEDIR
);
1322 textdomain(PACKAGE
);
1323 atexit(close_stdout
);
1325 /* default output format */
1328 while ((c
= getopt_long(argc
, argv
,
1329 "AabCcDd:ehifF:o:O:p::PklmnN:rst:uvRS:T:Uw:V",
1330 longopts
, NULL
)) != -1) {
1332 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1345 flags
|= FL_NOCACHE
;
1348 flags
|= FL_CANONICALIZE
;
1355 if (!strcmp(optarg
, "forward"))
1356 direction
= MNT_ITER_FORWARD
;
1357 else if (!strcmp(optarg
, "backward"))
1358 direction
= MNT_ITER_BACKWARD
;
1361 _("unknown direction '%s'"), optarg
);
1364 flags
|= FL_EVALUATE
;
1373 flags
|= FL_FIRSTONLY
;
1376 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1379 disable_columns_truncate();
1385 set_match(COL_OPTIONS
, optarg
);
1389 nactions
= string_to_idarray(optarg
,
1390 actions
, ARRAY_SIZE(actions
),
1391 poll_action_name_to_id
);
1402 case 'm': /* mtab */
1403 tabtype
= TABTYPE_MTAB
;
1406 case 's': /* fstab */
1407 tabtype
= TABTYPE_FSTAB
;
1410 case 'k': /* kernel (mountinfo) */
1411 tabtype
= TABTYPE_KERNEL
;
1414 set_match(COL_FSTYPE
, optarg
);
1417 flags
&= ~FL_TREE
; /* disable the default */
1418 flags
|= FL_RAW
; /* enable raw */
1421 flags
&= ~FL_TREE
; /* disable the default */
1424 flags
|= FL_NOHEADINGS
;
1427 tabtype
= TABTYPE_KERNEL
;
1428 tabfiles
= append_pid_tabfile(tabfiles
, &ntabfiles
,
1429 strtou32_or_err(optarg
,
1430 _("invalid TID argument")));
1433 flags
|= FL_NOFSROOT
;
1436 flags
|= FL_SUBMOUNTS
;
1439 set_source_match(optarg
);
1440 flags
|= FL_NOSWAPMATCH
;
1443 set_match(COL_TARGET
, optarg
);
1444 flags
|= FL_NOSWAPMATCH
;
1450 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1453 printf(UTIL_LINUX_VERSION
);
1454 return EXIT_SUCCESS
;
1461 if (!ncolumns
&& (flags
& FL_DF
)) {
1462 add_column(columns
, ncolumns
++, COL_SOURCE
);
1463 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1464 add_column(columns
, ncolumns
++, COL_SIZE
);
1465 add_column(columns
, ncolumns
++, COL_USED
);
1466 add_column(columns
, ncolumns
++, COL_AVAIL
);
1467 add_column(columns
, ncolumns
++, COL_USEPERC
);
1468 add_column(columns
, ncolumns
++, COL_TARGET
);
1471 /* default columns */
1473 if (flags
& FL_POLL
)
1474 add_column(columns
, ncolumns
++, COL_ACTION
);
1476 add_column(columns
, ncolumns
++, COL_TARGET
);
1477 add_column(columns
, ncolumns
++, COL_SOURCE
);
1478 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1479 add_column(columns
, ncolumns
++, COL_OPTIONS
);
1482 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1483 &ncolumns
, column_name_to_id
) < 0)
1484 return EXIT_FAILURE
;
1487 tabtype
= TABTYPE_KERNEL
;
1489 if ((flags
& FL_POLL
) && ntabfiles
> 1)
1490 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1492 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1493 errx(EXIT_FAILURE
, _(
1494 "options --target and --source can't be used together "
1495 "with command line element that is not an option"));
1498 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1500 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1502 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1503 /* don't care about submounts if list all mounts */
1504 flags
&= ~FL_SUBMOUNTS
;
1506 if (!(flags
& FL_SUBMOUNTS
) && ((flags
& FL_FIRSTONLY
)
1507 || get_match(COL_TARGET
)
1508 || get_match(COL_SOURCE
)
1509 || get_match(COL_MAJMIN
)))
1512 if (!(flags
& FL_NOSWAPMATCH
) &&
1513 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1515 * Check if we can swap source and target, it's
1516 * not possible if the source is LABEL=/UUID=
1518 const char *x
= get_match(COL_SOURCE
);
1520 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1521 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1522 flags
|= FL_NOSWAPMATCH
;
1526 * initialize libmount
1529 scols_init_debug(0);
1531 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1535 if (tabtype
== TABTYPE_MTAB
&& tab_is_kernel(tb
))
1536 tabtype
= TABTYPE_KERNEL
;
1538 if ((flags
& FL_TREE
) && (ntabfiles
> 1 || !tab_is_tree(tb
)))
1541 if (!(flags
& FL_NOCACHE
)) {
1542 cache
= mnt_new_cache();
1544 warn(_("failed to initialize libmount cache"));
1547 mnt_table_set_cache(tb
, cache
);
1549 if (tabtype
!= TABTYPE_KERNEL
)
1550 cache_set_targets(cache
);
1553 if (flags
& FL_UNIQ
)
1554 mnt_table_uniq_fs(tb
, MNT_UNIQ_KEEPTREE
, uniq_fs_target_cmp
);
1557 * initialize output formatting (libsmartcols.h)
1559 table
= scols_new_table();
1561 warn(_("failed to initialize output table"));
1564 scols_table_enable_raw(table
, !!(flags
& FL_RAW
));
1565 scols_table_enable_export(table
, !!(flags
& FL_EXPORT
));
1566 scols_table_enable_ascii(table
, !!(flags
& FL_ASCII
));
1567 scols_table_enable_noheadings(table
, !!(flags
& FL_NOHEADINGS
));
1569 for (i
= 0; i
< ncolumns
; i
++) {
1570 int fl
= get_column_flags(i
);
1571 int id
= get_column_id(i
);
1573 if (!(flags
& FL_TREE
))
1574 fl
&= ~SCOLS_FL_TREE
;
1576 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1577 warnx(_("%s column is requested, but --poll "
1578 "is not enabled"), get_column_name(i
));
1581 if (!scols_table_new_column(table
, get_column_name(i
),
1582 get_column_whint(i
), fl
)) {
1583 warn(_("failed to initialize output column"));
1589 * Fill in data to the output table
1591 if (flags
& FL_POLL
) {
1592 /* poll mode (accept the first tabfile only) */
1593 rc
= poll_table(tb
, tabfiles
? *tabfiles
: _PATH_PROC_MOUNTINFO
, timeout
, table
, direction
);
1595 } else if ((flags
& FL_TREE
) && !(flags
& FL_SUBMOUNTS
)) {
1597 rc
= create_treenode(table
, tb
, NULL
, NULL
);
1599 /* whole lits of sub-tree */
1600 rc
= add_matching_lines(tb
, table
, direction
);
1603 && tabtype
== TABTYPE_KERNEL
1604 && (flags
& FL_NOSWAPMATCH
)
1605 && get_match(COL_TARGET
)) {
1607 * Found nothing, maybe the --target is regular file,
1608 * try it again with extra functionality for target
1611 enable_extra_target_match(tb
);
1612 rc
= add_matching_lines(tb
, table
, direction
);
1617 * Print the output table for non-poll modes
1619 if (!rc
&& !(flags
& FL_POLL
))
1620 scols_print_table(table
);
1622 scols_unref_table(table
);
1624 mnt_unref_table(tb
);
1625 mnt_unref_cache(cache
);
1631 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;