4 * Copyright (C) 2010-2015 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.
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
34 #include <sys/statvfs.h>
35 #include <sys/types.h>
41 #include <libsmartcols.h>
43 #include "pathnames.h"
45 #include "closestream.h"
52 #include "column-list-table.h"
99 const char * const name
; /* header */
100 double whint
; /* width hint (N < 1 is in percent of termwidth) */
101 int flags
; /* libsmartcols flags */
102 const char *help
; /* column description */
103 const char *match
; /* pattern for match_func() */
104 void *match_data
; /* match specific data */
107 /* columns descriptions (don't use const, this is writable) */
108 static struct colinfo infos
[] = {
109 [COL_ACTION
] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH
, N_("action detected by --poll") },
110 [COL_AVAIL
] = { "AVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available, use <number> if --bytes is given") },
111 [COL_FREQ
] = { "FREQ", 1, SCOLS_FL_RIGHT
, N_("dump(8) period in days [fstab only]") },
112 [COL_FSROOT
] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES
, N_("filesystem root") },
113 [COL_FSTYPE
] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC
, N_("filesystem type") },
114 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("FS specific mount options") },
115 [COL_ID
] = { "ID", 2, SCOLS_FL_RIGHT
, N_("mount ID") },
116 [COL_INO_AVAIL
] = { "INO.AVAIL", 5, SCOLS_FL_RIGHT
, N_("number of available inodes") },
117 [COL_INO_TOTAL
] = { "INO.TOTAL", 5, SCOLS_FL_RIGHT
, N_("total number of inodes") },
118 [COL_INO_USED
] = { "INO.USED", 5, SCOLS_FL_RIGHT
, N_("number of used inodes") },
119 [COL_INO_USEPERC
] = { "INO.USE%", 3, SCOLS_FL_RIGHT
, N_("percentage of INO.USED divided by INO.TOTAL") },
120 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
121 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
122 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("old mount options saved by --poll") },
123 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
124 [COL_OPTIONS
] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("all mount options") },
125 [COL_OPT_FIELDS
] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC
, N_("optional mount fields") },
126 [COL_PARENT
] = { "PARENT", 2, SCOLS_FL_RIGHT
, N_("mount parent ID") },
127 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
128 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
129 [COL_PASSNO
] = { "PASSNO", 1, SCOLS_FL_RIGHT
, N_("pass number on parallel fsck(8) [fstab only]") },
130 [COL_PROPAGATION
] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
131 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size, use <number> if --bytes is given") },
132 [COL_SOURCES
] = { "SOURCES", 0.25, SCOLS_FL_WRAP
, N_("all possible source devices") },
133 [COL_SOURCE
] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES
, N_("source device") },
134 [COL_TARGET
] = { "TARGET", 0.30, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("mountpoint") },
135 [COL_TID
] = { "TID", 4, SCOLS_FL_RIGHT
, N_("task ID") },
136 [COL_USED
] = { "USED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used, use <number> if --bytes is given") },
137 [COL_USEPERC
] = { "USE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
138 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
139 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC
, N_("VFS specific mount options") }
142 /* columns[] array specifies all currently wanted output column. The columns
143 * are defined by infos[] array and you can specify (on command line) each
144 * column twice. That's enough, dynamically allocated array of the columns is
145 * unnecessary overkill and over-engineering in this case */
146 static int columns
[ARRAY_SIZE(infos
) * 2];
147 static size_t ncolumns
;
149 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
152 errx(EXIT_FAILURE
, _("too many columns specified, "
153 "the limit is %zu columns"),
158 #define add_column(ary, n, id) \
159 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
161 /* poll actions (parsed --poll=<list> */
162 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
163 static int actions
[FINDMNT_NACTIONS
];
166 /* global (accessed from findmnt-verify.c too) */
169 struct libmnt_cache
*cache
;
171 static blkid_cache blk_cache
;
174 static struct udev
*udev
;
177 static int match_func(struct libmnt_fs
*fs
, void *data
__attribute__ ((__unused__
)));
180 static int get_column_id(int num
)
183 assert((size_t) num
< ncolumns
);
184 assert((size_t) columns
[num
] < ARRAY_SIZE(infos
));
188 static struct colinfo
*get_column_info(int num
)
190 return &infos
[ get_column_id(num
) ];
193 static const char *column_id_to_name(int id
)
195 assert((size_t) id
< ARRAY_SIZE(infos
));
196 return infos
[id
].name
;
199 static const char *get_column_name(int num
)
201 return get_column_info(num
)->name
;
204 static float get_column_whint(int num
)
206 return get_column_info(num
)->whint
;
209 static int get_column_flags(int num
)
211 return get_column_info(num
)->flags
;
214 static const char *get_match(int id
)
216 assert((size_t) id
< ARRAY_SIZE(infos
));
217 return infos
[id
].match
;
220 static void *get_match_data(int id
)
222 assert((size_t) id
< ARRAY_SIZE(infos
));
223 return infos
[id
].match_data
;
226 static void set_match(int id
, const char *match
)
228 assert((size_t) id
< ARRAY_SIZE(infos
));
229 infos
[id
].match
= match
;
232 static void set_match_data(int id
, void *data
)
234 assert((size_t) id
< ARRAY_SIZE(infos
));
235 infos
[id
].match_data
= data
;
239 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
242 static void set_source_match(const char *data
)
246 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
247 dev_t
*devno
= xmalloc(sizeof(dev_t
));
249 *devno
= makedev(maj
, min
);
250 set_match(COL_MAJMIN
, data
);
251 set_match_data(COL_MAJMIN
, (void *) devno
);
252 flags
|= FL_NOSWAPMATCH
;
254 set_match(COL_SOURCE
, data
);
258 * Extra functionality for --target <path>. The function mnt_table_find_mountpoint()
259 * also checks parents (path elements in reverse order) to get mountpoint.
261 * @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 int is_listall_mode(void)
312 if ((flags
& FL_DF
|| flags
& FL_REAL
|| flags
& FL_PSEUDO
) && !(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;
488 bool no_bytes
= false;
490 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
495 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
498 vfs_attr
= buf
.f_frsize
* buf
.f_bavail
;
501 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
504 if (buf
.f_blocks
== 0)
507 xasprintf(&sizestr
, "%.0f%%",
508 (double)(buf
.f_blocks
- buf
.f_bfree
) /
512 /* Quoted from startvfs(3):
514 * Under Linux, f_favail is always the same as
515 * f_ffree, and there's no way for a filesystem to
516 * report otherwise. This is not an issue, since no
517 * filesystems with an inode root reservation exist.
520 vfs_attr
= buf
.f_ffree
;
524 vfs_attr
= buf
.f_files
;
528 vfs_attr
= buf
.f_files
- buf
.f_ffree
;
530 case COL_INO_USEPERC
:
531 if (buf
.f_files
== 0)
534 xasprintf(&sizestr
, "%.0f%%",
535 (double)(buf
.f_files
- buf
.f_ffree
) /
541 sizestr
= xstrdup("0");
542 else if ((flags
& FL_BYTES
) || no_bytes
)
543 xasprintf(&sizestr
, "%ju", vfs_attr
);
545 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
550 /* reads sources from libmount/libblkid
552 static char *get_data_col_sources(struct libmnt_fs
*fs
, int evaluate
, size_t *datasiz
)
554 const char *tag
= NULL
, *p
= NULL
;
556 const char *device
= NULL
;
558 blkid_dev_iterate iter
;
560 struct ul_buffer buf
= UL_INIT_BUFFER
;
562 /* get TAG from libmount if avalable (e.g. fstab) */
563 if (mnt_fs_get_tag(fs
, &tag
, &p
) == 0) {
565 /* if device is in the form 'UUID=..' or 'LABEL=..' and evaluate==0
566 * then it is ok to do not search for multiple devices
573 /* or get device name */
574 else if (!(device
= mnt_fs_get_srcpath(fs
))
575 || strncmp(device
, "/dev/", 5) != 0)
579 if (blkid_get_cache(&blk_cache
, NULL
) != 0)
581 blkid_probe_all(blk_cache
);
584 /* ask libblkid for the UUID */
589 val
= blkid_get_tag_value(blk_cache
, "UUID", device
); /* returns allocated string */
597 /* scan all devices for the TAG */
598 iter
= blkid_dev_iterate_begin(blk_cache
);
599 blkid_dev_set_search(iter
, tag
, val
);
601 while (blkid_dev_next(iter
, &dev
) == 0) {
602 dev
= blkid_verify(blk_cache
, dev
);
606 ul_buffer_append_data(&buf
, "\0", 1);
607 ul_buffer_append_string(&buf
, blkid_dev_devname(dev
));
610 blkid_dev_iterate_end(iter
);
613 return ul_buffer_get_data(&buf
, datasiz
, NULL
);
620 /* reads FS data from libmount
622 static char *get_data(struct libmnt_fs
*fs
, int num
, size_t *datasiz
)
625 int col_id
= get_column_id(num
);
629 /* print all devices with the same tag (LABEL, UUID) */
630 str
= get_data_col_sources(fs
, flags
& FL_EVALUATE
, datasiz
);
637 const char *root
= mnt_fs_get_root(fs
);
638 const char *spec
= mnt_fs_get_srcpath(fs
);
641 if (spec
&& (flags
& FL_CANONICALIZE
))
642 spec
= cn
= mnt_resolve_path(spec
, cache
);
644 spec
= mnt_fs_get_source(fs
);
646 if (spec
&& (flags
& FL_EVALUATE
))
647 spec
= cn
= mnt_resolve_spec(spec
, cache
);
649 if (root
&& spec
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/") != 0)
650 xasprintf(&str
, "%s[%s]", spec
, root
);
659 if (mnt_fs_get_target(fs
))
660 str
= xstrdup(mnt_fs_get_target(fs
));
663 if (mnt_fs_get_fstype(fs
))
664 str
= xstrdup(mnt_fs_get_fstype(fs
));
667 if (mnt_fs_get_options(fs
))
668 str
= xstrdup(mnt_fs_get_options(fs
));
670 case COL_VFS_OPTIONS
:
671 if (flags
& FL_VFS_ALL
)
672 str
= mnt_fs_get_vfs_options_all(fs
);
673 else if (mnt_fs_get_vfs_options(fs
))
674 str
= xstrdup(mnt_fs_get_vfs_options(fs
));
677 if (mnt_fs_get_fs_options(fs
))
678 str
= xstrdup(mnt_fs_get_fs_options(fs
));
681 if (mnt_fs_get_optional_fields(fs
))
682 str
= xstrdup(mnt_fs_get_optional_fields(fs
));
685 str
= get_tag(fs
, "UUID", col_id
);
688 str
= get_tag(fs
, "PARTUUID", col_id
);
691 str
= get_tag(fs
, "LABEL", col_id
);
694 str
= get_tag(fs
, "PARTLABEL", col_id
);
699 dev_t devno
= mnt_fs_get_devno(fs
);
703 if ((flags
& FL_RAW
) || (flags
& FL_EXPORT
) || (flags
& FL_JSON
))
704 xasprintf(&str
, "%u:%u", major(devno
), minor(devno
));
706 xasprintf(&str
, "%3u:%-3u", major(devno
), minor(devno
));
716 case COL_INO_USEPERC
:
717 str
= get_vfs_attr(fs
, col_id
);
720 if (mnt_fs_get_root(fs
))
721 str
= xstrdup(mnt_fs_get_root(fs
));
724 if (mnt_fs_get_tid(fs
))
725 xasprintf(&str
, "%d", mnt_fs_get_tid(fs
));
728 if (mnt_fs_get_id(fs
))
729 xasprintf(&str
, "%d", mnt_fs_get_id(fs
));
732 if (mnt_fs_get_parent_id(fs
))
733 xasprintf(&str
, "%d", mnt_fs_get_parent_id(fs
));
735 case COL_PROPAGATION
:
736 if (mnt_fs_is_kernel(fs
)) {
737 unsigned long fl
= 0;
740 if (mnt_fs_get_propagation(fs
, &fl
) != 0)
743 n
= xstrdup((fl
& MS_SHARED
) ? "shared" : "private");
746 xasprintf(&str
, "%s,slave", n
);
750 if (fl
& MS_UNBINDABLE
) {
751 xasprintf(&str
, "%s,unbindable", n
);
759 if (!mnt_fs_is_kernel(fs
))
760 xasprintf(&str
, "%d", mnt_fs_get_freq(fs
));
763 if (!mnt_fs_is_kernel(fs
))
764 xasprintf(&str
, "%d", mnt_fs_get_passno(fs
));
772 static char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
773 struct libmnt_fs
*new_fs
,
780 switch (get_column_id(num
)) {
783 case MNT_TABDIFF_MOUNT
:
786 case MNT_TABDIFF_UMOUNT
:
789 case MNT_TABDIFF_REMOUNT
:
792 case MNT_TABDIFF_MOVE
:
801 case COL_OLD_OPTIONS
:
802 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
803 change
== MNT_TABDIFF_UMOUNT
)
804 && mnt_fs_get_options(old_fs
))
805 str
= xstrdup(mnt_fs_get_options(old_fs
));
808 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
809 change
== MNT_TABDIFF_UMOUNT
)
810 && mnt_fs_get_target(old_fs
))
811 str
= xstrdup(mnt_fs_get_target(old_fs
));
815 str
= get_data(new_fs
, num
, datasiz
);
817 str
= get_data(old_fs
, num
, datasiz
);
823 static void set_line_data(struct libscols_line
*ln
, size_t i
, char *data
, size_t datasiz
)
826 struct libscols_cell
*ce
;
828 ce
= scols_line_get_cell(ln
, i
);
832 rc
= scols_cell_refer_memory(ce
, data
, datasiz
);
834 rc
= scols_cell_refer_data(ce
, data
);
836 err(EXIT_FAILURE
, _("failed to add output data"));
839 /* adds one line to the output @tab */
840 static struct libscols_line
*add_line(struct libscols_table
*table
, struct libmnt_fs
*fs
,
841 struct libscols_line
*parent
)
844 struct libscols_line
*line
= scols_table_new_line(table
, parent
);
847 err(EXIT_FAILURE
, _("failed to allocate output line"));
849 for (i
= 0; i
< ncolumns
; i
++) {
851 char *data
= get_data(fs
, i
, &datasiz
);
854 set_line_data(line
, i
, data
, datasiz
);
857 scols_line_set_userdata(line
, fs
);
861 static struct libscols_line
*add_tabdiff_line(struct libscols_table
*table
, struct libmnt_fs
*new_fs
,
862 struct libmnt_fs
*old_fs
, int change
)
865 struct libscols_line
*line
= scols_table_new_line(table
, NULL
);
868 err(EXIT_FAILURE
, _("failed to allocate output line"));
870 for (i
= 0; i
< ncolumns
; i
++) {
872 char *data
= get_tabdiff_data(old_fs
, new_fs
, change
, i
, &datasiz
);
875 set_line_data(line
, i
, data
, datasiz
);
881 static int has_line(struct libscols_table
*table
, struct libmnt_fs
*fs
)
883 struct libscols_line
*ln
;
884 struct libscols_iter
*itr
;
887 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
891 while (scols_table_next_line(table
, itr
, &ln
) == 0) {
892 if ((struct libmnt_fs
*) scols_line_get_userdata(ln
) == fs
) {
898 scols_free_iter(itr
);
902 /* reads filesystems from @tb (libmount) and fillin @table (output table) */
903 static int create_treenode(struct libscols_table
*table
, struct libmnt_table
*tb
,
904 struct libmnt_fs
*fs
, struct libscols_line
*parent_line
)
906 struct libmnt_fs
*chld
= NULL
;
907 struct libmnt_iter
*itr
= NULL
;
908 struct libscols_line
*line
;
909 int rc
= -1, first
= 0;
912 /* first call, get root FS */
913 if (mnt_table_get_root_fs(tb
, &fs
))
918 } else if ((flags
& FL_SUBMOUNTS
) && has_line(table
, fs
))
921 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
925 if ((flags
& FL_SUBMOUNTS
) || match_func(fs
, NULL
)) {
926 line
= add_line(table
, fs
, parent_line
);
933 * add all children to the output table
935 while (mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
936 if (create_treenode(table
, tb
, chld
, line
))
941 /* make sure all entries are in the tree */
942 if (first
&& (size_t) mnt_table_get_nents(tb
) >
943 (size_t) scols_table_get_nlines(table
)) {
944 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
947 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
948 if (!has_line(table
, fs
) && match_func(fs
, NULL
))
949 create_treenode(table
, tb
, fs
, NULL
);
958 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
959 const char *filename
, int line
)
961 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
966 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
968 files
= xreallocarray(files
, *nfiles
+ 1, sizeof(char *));
969 files
[(*nfiles
)++] = filename
;
973 static char **append_pid_tabfile(char **files
, int *nfiles
, pid_t pid
)
977 xasprintf(&path
, "/proc/%d/mountinfo", (int) pid
);
978 return append_tabfile(files
, nfiles
, path
);
981 /* calls libmount fstab/mtab/mountinfo parser */
982 static struct libmnt_table
*parse_tabfiles(char **files
,
986 struct libmnt_table
*tb
;
989 tb
= mnt_new_table();
991 warn(_("failed to initialize libmount table"));
994 mnt_table_set_parser_errcb(tb
, parser_errcb
);
997 /* NULL means that libmount will use default paths */
998 const char *path
= nfiles
? *files
++ : NULL
;
1002 rc
= mnt_table_parse_fstab(tb
, path
);
1005 rc
= mnt_table_parse_mtab(tb
, path
);
1007 case TABTYPE_KERNEL
:
1009 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
1010 _PATH_PROC_MOUNTINFO
:
1013 rc
= mnt_table_parse_file(tb
, path
);
1017 mnt_unref_table(tb
);
1018 warn(_("can't read %s"), path
);
1021 } while (--nfiles
> 0);
1027 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
1028 * necessary if @tb in main() was read from a non-kernel source.
1030 static void cache_set_targets(struct libmnt_cache
*tmp
)
1032 struct libmnt_table
*tb
;
1035 tb
= mnt_new_table();
1039 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
1040 _PATH_PROC_MOUNTINFO
:
1043 if (mnt_table_parse_file(tb
, path
) == 0)
1044 mnt_cache_set_targets(tmp
, tb
);
1046 mnt_unref_table(tb
);
1049 /* checks if @tb contains parent->child relations */
1050 static int tab_is_tree(struct libmnt_table
*tb
)
1052 struct libmnt_fs
*fs
= NULL
;
1053 struct libmnt_iter
*itr
;
1056 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
1060 rc
= (mnt_table_next_fs(tb
, itr
, &fs
) == 0 &&
1061 mnt_fs_is_kernel(fs
) &&
1062 mnt_fs_get_root(fs
));
1068 /* checks if all fs in @tb are from kernel */
1069 static int tab_is_kernel(struct libmnt_table
*tb
)
1071 struct libmnt_fs
*fs
= NULL
;
1072 struct libmnt_iter
*itr
;
1074 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
1078 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
1079 if (!mnt_fs_is_kernel(fs
)) {
1089 /* filter function for libmount (mnt_table_find_next_fs()) */
1090 static int match_func(struct libmnt_fs
*fs
,
1091 void *data
__attribute__ ((__unused__
)))
1093 int rc
= flags
& FL_INVERT
? 1 : 0;
1097 m
= get_match(COL_FSTYPE
);
1098 if (m
&& !mnt_fs_match_fstype(fs
, m
))
1101 m
= get_match(COL_OPTIONS
);
1102 if (m
&& !mnt_fs_match_options(fs
, m
))
1105 md
= get_match_data(COL_MAJMIN
);
1106 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
1109 m
= get_match(COL_TARGET
);
1110 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
1113 m
= get_match(COL_SOURCE
);
1114 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
1117 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
1118 const char *type
= mnt_fs_get_fstype(fs
);
1120 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
1123 if (mnt_fs_is_pseudofs(fs
))
1127 if ((flags
& FL_REAL
) && mnt_fs_is_pseudofs(fs
))
1130 if ((flags
& FL_PSEUDO
) && !mnt_fs_is_pseudofs(fs
))
1133 if ((flags
& FL_SHADOWED
)) {
1134 struct libmnt_table
*tb
= NULL
;
1136 mnt_fs_get_table(fs
, &tb
);
1137 if (tb
&& mnt_table_over_fs(tb
, fs
, NULL
) != 0)
1144 /* iterate over filesystems in @tb */
1145 struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
1146 struct libmnt_iter
*itr
)
1148 struct libmnt_fs
*fs
= NULL
;
1150 if (is_listall_mode()) {
1154 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
1157 } else if (is_mount_compatible_mode()) {
1159 * Look up for FS in the same way how mount(8) searches in fstab
1163 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
1164 mnt_iter_get_direction(itr
));
1166 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
1167 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
1168 mnt_iter_get_direction(itr
));
1171 * Look up for all matching entries
1173 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
1174 * findmnt [-l] <spec> [-O <options>] [-t <types>]
1177 if (mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
) != 0)
1181 !(flags
& FL_NOSWAPMATCH
) &&
1182 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1184 /* swap 'spec' and target. */
1185 set_match(COL_TARGET
, get_match(COL_SOURCE
));
1186 set_match(COL_SOURCE
, NULL
);
1187 mnt_reset_iter(itr
, -1);
1197 * Filter out unwanted lines for --list output or top level lines for
1198 * --submounts tree output.
1200 static int add_matching_lines(struct libmnt_table
*tb
,
1201 struct libscols_table
*table
, int direction
)
1203 struct libmnt_iter
*itr
;
1204 struct libmnt_fs
*fs
;
1205 int nlines
= 0, rc
= -1;
1207 itr
= mnt_new_iter(direction
);
1209 warn(_("failed to initialize libmount iterator"));
1213 while((fs
= get_next_fs(tb
, itr
))) {
1214 if ((flags
& FL_TREE
) || (flags
& FL_SUBMOUNTS
))
1215 rc
= create_treenode(table
, tb
, fs
, NULL
);
1217 rc
= !add_line(table
, fs
, NULL
);
1221 if (flags
& FL_FIRSTONLY
)
1223 flags
|= FL_NOSWAPMATCH
;
1233 static int poll_match(struct libmnt_fs
*fs
)
1235 int rc
= match_func(fs
, NULL
);
1237 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
1238 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
1240 * findmnt --poll /foo
1241 * The '/foo' maybe source as well as target.
1243 const char *str
= get_match(COL_SOURCE
);
1245 set_match(COL_TARGET
, str
); /* swap */
1246 set_match(COL_SOURCE
, NULL
);
1248 rc
= match_func(fs
, NULL
);
1250 set_match(COL_TARGET
, NULL
); /* restore */
1251 set_match(COL_SOURCE
, str
);
1257 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
1258 int timeout
, struct libscols_table
*table
, int direction
)
1262 struct libmnt_iter
*itr
= NULL
;
1263 struct libmnt_table
*tb_new
;
1264 struct libmnt_tabdiff
*diff
= NULL
;
1265 struct pollfd fds
[1];
1267 tb_new
= mnt_new_table();
1269 warn(_("failed to initialize libmount table"));
1273 itr
= mnt_new_iter(direction
);
1275 warn(_("failed to initialize libmount iterator"));
1279 diff
= mnt_new_tabdiff();
1281 warn(_("failed to initialize libmount tabdiff"));
1285 /* cache is unnecessary to detect changes */
1286 mnt_table_set_cache(tb
, NULL
);
1287 mnt_table_set_cache(tb_new
, NULL
);
1289 f
= fopen(tabfile
, "r");
1291 warn(_("cannot open %s"), tabfile
);
1295 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
1297 fds
[0].fd
= fileno(f
);
1298 fds
[0].events
= POLLPRI
;
1301 struct libmnt_table
*tmp
;
1302 struct libmnt_fs
*old
, *new;
1305 count
= poll(fds
, 1, timeout
);
1307 break; /* timeout */
1309 warn(_("poll() failed"));
1314 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
1316 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
1321 mnt_reset_iter(itr
, direction
);
1322 while(mnt_tabdiff_next_change(
1323 diff
, itr
, &old
, &new, &change
) == 0) {
1325 if (!has_poll_action(change
))
1327 if (!poll_match(new ? new : old
))
1330 rc
= !add_tabdiff_line(table
, new, old
, change
);
1333 if (flags
& FL_FIRSTONLY
)
1338 rc
= scols_table_print_range(table
, NULL
, NULL
);
1339 fflush(scols_table_get_stream(table
));
1349 /* remove already printed lines to reduce memory usage */
1350 scols_table_remove_lines(table
);
1351 mnt_reset_table(tb_new
);
1353 if (count
&& (flags
& FL_FIRSTONLY
))
1359 mnt_unref_table(tb_new
);
1360 mnt_free_tabdiff(diff
);
1367 static int uniq_fs_target_cmp(
1368 struct libmnt_table
*tb
__attribute__((__unused__
)),
1369 struct libmnt_fs
*a
,
1370 struct libmnt_fs
*b
)
1372 return !mnt_fs_match_target(a
, mnt_fs_get_target(b
), cache
);
1375 static int get_column_json_type(int id
, int scols_flags
, int *multi
)
1383 if (!(flags
& FL_BYTES
))
1391 return SCOLS_JSON_NUMBER
;
1394 if (scols_flags
& SCOLS_FL_WRAP
)
1395 return SCOLS_JSON_ARRAY_STRING
;
1399 return SCOLS_JSON_STRING
; /* default */
1402 static void __attribute__((__noreturn__
)) usage(void)
1406 fputs(USAGE_HEADER
, out
);
1409 " %1$s [options] <device> | <mountpoint>\n"
1410 " %1$s [options] <device> <mountpoint>\n"
1411 " %1$s [options] [--source <device>] [--target <path> | --mountpoint <dir>]\n"),
1412 program_invocation_short_name
);
1414 fputs(USAGE_SEPARATOR
, out
);
1415 fputs(_("Find a (mounted) filesystem.\n"), out
);
1417 fputs(USAGE_OPTIONS
, out
);
1418 fputs(_(" -s, --fstab search in static table of filesystems\n"), out
);
1419 fputs(_(" -m, --mtab search in table of mounted filesystems\n"
1420 " (includes user space mount options)\n"), out
);
1421 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1422 " filesystems (default)\n"), out
);
1424 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out
);
1425 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out
);
1428 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out
);
1429 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out
);
1430 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
1431 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out
);
1432 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out
);
1433 fputs(_(" -D, --df imitate the output of df(1)\n"), out
);
1434 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out
);
1435 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1436 " to device names\n"), out
);
1437 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out
);
1438 fputs(_(" -f, --first-only print the first found filesystem only\n"), out
);
1439 fputs(_(" -I, --dfi imitate the output of df(1) with -i option\n"), out
);
1440 fputs(_(" -i, --invert invert the sense of matching\n"), out
);
1441 fputs(_(" -J, --json use JSON output format\n"), out
);
1442 fputs(_(" -l, --list use list format output\n"), out
);
1443 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out
);
1444 fputs(_(" -n, --noheadings don't print column headings\n"), out
);
1445 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out
);
1446 fputs(_(" -o, --output <list> output columns (see --list-columns)\n"), out
);
1447 fputs(_(" --output-all output all available columns\n"), out
);
1448 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1449 fputs(_(" --pseudo print only pseudo-filesystems\n"), out
);
1450 fputs(_(" --shadowed print only filesystems over-mounted by another filesystem\n"), out
);
1451 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out
);
1452 fputs(_(" -r, --raw use raw output format\n"), out
);
1453 fputs(_(" --real print only real filesystems\n"), out
);
1454 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1455 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out
);
1456 fputs(_(" -T, --target <path> the path to the filesystem to use\n"), out
);
1457 fputs(_(" --tree enable tree format output if possible\n"), out
);
1458 fputs(_(" -M, --mountpoint <dir> the mountpoint directory\n"), out
);
1459 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out
);
1460 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out
);
1461 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
1462 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out
);
1463 fputs(_(" -y, --shell use column names to be usable as shell variable identifiers\n"), out
);
1466 fputs(_(" -x, --verify verify mount table content (default is fstab)\n"), out
);
1467 fputs(_(" --verbose print more details\n"), out
);
1468 fputs(_(" --vfs-all print all VFS options\n"), out
);
1470 fputs(USAGE_SEPARATOR
, out
);
1471 fputs(_(" -H, --list-columns list the available columns\n"), out
);
1472 fprintf(out
, USAGE_HELP_OPTIONS(24));
1474 fprintf(out
, USAGE_MAN_TAIL("findmnt(8)"));
1479 static void __attribute__((__noreturn__
)) list_colunms(void)
1482 struct libscols_table
*tb
= xcolumn_list_table_new("findmnt-columns", stdout
,
1486 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
1487 const struct colinfo
*ci
= &infos
[i
];
1489 int json
= get_column_json_type(i
, ci
->flags
, &multi
);
1491 xcolumn_list_table_append_line(tb
, ci
->name
,
1493 multi
? "<string|number>" : NULL
,
1497 scols_print_table(tb
);
1498 scols_unref_table(tb
);
1504 int main(int argc
, char *argv
[])
1506 struct libmnt_table
*tb
= NULL
;
1507 char **tabfiles
= NULL
;
1508 int direction
= MNT_ITER_FORWARD
;
1509 int verify
= 0, collist
= 0;
1510 int c
, rc
= -1, timeout
= -1;
1511 int ntabfiles
= 0, tabtype
= 0;
1512 char *outarg
= NULL
;
1514 int force_tree
= 0, istree
= 0;
1516 struct libscols_table
*table
= NULL
;
1519 FINDMNT_OPT_VERBOSE
= CHAR_MAX
+ 1,
1521 FINDMNT_OPT_OUTPUT_ALL
,
1524 FINDMNT_OPT_VFS_ALL
,
1525 FINDMNT_OPT_SHADOWED
1528 static const struct option longopts
[] = {
1529 { "all", no_argument
, NULL
, 'A' },
1530 { "ascii", no_argument
, NULL
, 'a' },
1531 { "bytes", no_argument
, NULL
, 'b' },
1532 { "canonicalize", no_argument
, NULL
, 'c' },
1533 { "direction", required_argument
, NULL
, 'd' },
1534 { "df", no_argument
, NULL
, 'D' },
1535 { "dfi", no_argument
, NULL
, 'I' },
1536 { "evaluate", no_argument
, NULL
, 'e' },
1537 { "first-only", no_argument
, NULL
, 'f' },
1538 { "fstab", no_argument
, NULL
, 's' },
1539 { "help", no_argument
, NULL
, 'h' },
1540 { "invert", no_argument
, NULL
, 'i' },
1541 { "json", no_argument
, NULL
, 'J' },
1542 { "kernel", no_argument
, NULL
, 'k' },
1543 { "list", no_argument
, NULL
, 'l' },
1544 { "mountpoint", required_argument
, NULL
, 'M' },
1545 { "mtab", no_argument
, NULL
, 'm' },
1546 { "noheadings", no_argument
, NULL
, 'n' },
1547 { "notruncate", no_argument
, NULL
, 'u' },
1548 { "options", required_argument
, NULL
, 'O' },
1549 { "output", required_argument
, NULL
, 'o' },
1550 { "output-all", no_argument
, NULL
, FINDMNT_OPT_OUTPUT_ALL
},
1551 { "poll", optional_argument
, NULL
, 'p' },
1552 { "pairs", no_argument
, NULL
, 'P' },
1553 { "raw", no_argument
, NULL
, 'r' },
1554 { "types", required_argument
, NULL
, 't' },
1555 { "nocanonicalize", no_argument
, NULL
, 'C' },
1556 { "nofsroot", no_argument
, NULL
, 'v' },
1557 { "submounts", no_argument
, NULL
, 'R' },
1558 { "source", required_argument
, NULL
, 'S' },
1559 { "tab-file", required_argument
, NULL
, 'F' },
1560 { "task", required_argument
, NULL
, 'N' },
1561 { "target", required_argument
, NULL
, 'T' },
1562 { "timeout", required_argument
, NULL
, 'w' },
1563 { "uniq", no_argument
, NULL
, 'U' },
1564 { "verify", no_argument
, NULL
, 'x' },
1565 { "version", no_argument
, NULL
, 'V' },
1566 { "shell", no_argument
, NULL
, 'y' },
1567 { "verbose", no_argument
, NULL
, FINDMNT_OPT_VERBOSE
},
1568 { "tree", no_argument
, NULL
, FINDMNT_OPT_TREE
},
1569 { "real", no_argument
, NULL
, FINDMNT_OPT_REAL
},
1570 { "pseudo", no_argument
, NULL
, FINDMNT_OPT_PSEUDO
},
1571 { "vfs-all", no_argument
, NULL
, FINDMNT_OPT_VFS_ALL
},
1572 { "shadowed", no_argument
, NULL
, FINDMNT_OPT_SHADOWED
},
1573 { "list-columns", no_argument
, NULL
, 'H' },
1574 { NULL
, 0, NULL
, 0 }
1577 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1578 { 'C', 'c'}, /* [no]canonicalize */
1579 { 'C', 'e' }, /* nocanonicalize, evaluate */
1580 { 'J', 'P', 'r','x' }, /* json,pairs,raw,verify */
1581 { 'M', 'T' }, /* mountpoint, target */
1582 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1583 { 'P','l','r','x' }, /* pairs,list,raw,verify */
1584 { 'p','x' }, /* poll,verify */
1585 { 'm','p','s' }, /* mtab,poll,fstab */
1586 { FINDMNT_OPT_PSEUDO
, FINDMNT_OPT_REAL
},
1589 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1591 setlocale(LC_ALL
, "");
1592 bindtextdomain(PACKAGE
, LOCALEDIR
);
1593 textdomain(PACKAGE
);
1594 close_stdout_atexit();
1596 /* default output format */
1599 while ((c
= getopt_long(argc
, argv
,
1600 "AabCcDd:ehIiJfF:o:O:p::PklmM:nN:rst:uvRS:T:Uw:VxyH",
1601 longopts
, NULL
)) != -1) {
1603 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1616 flags
|= FL_NOCACHE
;
1619 flags
|= FL_CANONICALIZE
;
1626 if (!strcmp(optarg
, "forward"))
1627 direction
= MNT_ITER_FORWARD
;
1628 else if (!strcmp(optarg
, "backward"))
1629 direction
= MNT_ITER_BACKWARD
;
1632 _("unknown direction '%s'"), optarg
);
1635 flags
|= FL_EVALUATE
;
1639 flags
|= (FL_DF_INODES
| FL_DF
);
1648 flags
|= FL_FIRSTONLY
;
1651 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1654 disable_columns_truncate();
1659 case FINDMNT_OPT_OUTPUT_ALL
:
1661 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
1662 if (is_tabdiff_column(i
))
1664 columns
[ncolumns
++] = i
;
1668 set_match(COL_OPTIONS
, optarg
);
1672 nactions
= string_to_idarray(optarg
,
1673 actions
, ARRAY_SIZE(actions
),
1674 poll_action_name_to_id
);
1685 case 'm': /* mtab */
1686 tabtype
= TABTYPE_MTAB
;
1689 case 's': /* fstab */
1690 tabtype
= TABTYPE_FSTAB
;
1693 case 'k': /* kernel (mountinfo) */
1694 tabtype
= TABTYPE_KERNEL
;
1697 set_match(COL_FSTYPE
, optarg
);
1700 flags
&= ~FL_TREE
; /* disable the default */
1701 flags
|= FL_RAW
; /* enable raw */
1704 flags
&= ~FL_TREE
; /* disable the default */
1707 flags
|= FL_NOHEADINGS
;
1710 tabtype
= TABTYPE_KERNEL
;
1711 tabfiles
= append_pid_tabfile(tabfiles
, &ntabfiles
,
1712 strtou32_or_err(optarg
,
1713 _("invalid TID argument")));
1716 flags
|= FL_NOFSROOT
;
1719 flags
|= FL_SUBMOUNTS
;
1722 set_source_match(optarg
);
1723 flags
|= FL_NOSWAPMATCH
;
1726 flags
|= FL_STRICTTARGET
;
1729 set_match(COL_TARGET
, optarg
);
1730 flags
|= FL_NOSWAPMATCH
;
1736 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1742 flags
|= FL_SHELLVAR
;
1744 case FINDMNT_OPT_VERBOSE
:
1745 flags
|= FL_VERBOSE
;
1747 case FINDMNT_OPT_TREE
:
1750 case FINDMNT_OPT_PSEUDO
:
1753 case FINDMNT_OPT_REAL
:
1756 case FINDMNT_OPT_VFS_ALL
:
1757 flags
|= FL_VFS_ALL
;
1759 case FINDMNT_OPT_SHADOWED
:
1760 flags
|= FL_SHADOWED
;
1769 print_version(EXIT_SUCCESS
);
1771 errtryhelp(EXIT_FAILURE
);
1776 list_colunms(); /* print end exit */
1778 if (!ncolumns
&& (flags
& FL_DF
)) {
1779 add_column(columns
, ncolumns
++, COL_SOURCE
);
1780 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1781 if (flags
& FL_DF_INODES
) {
1782 add_column(columns
, ncolumns
++, COL_INO_TOTAL
);
1783 add_column(columns
, ncolumns
++, COL_INO_USED
);
1784 add_column(columns
, ncolumns
++, COL_INO_AVAIL
);
1785 add_column(columns
, ncolumns
++, COL_INO_USEPERC
);
1787 add_column(columns
, ncolumns
++, COL_SIZE
);
1788 add_column(columns
, ncolumns
++, COL_USED
);
1789 add_column(columns
, ncolumns
++, COL_AVAIL
);
1790 add_column(columns
, ncolumns
++, COL_USEPERC
);
1792 add_column(columns
, ncolumns
++, COL_TARGET
);
1795 /* default columns */
1797 if (flags
& FL_POLL
)
1798 add_column(columns
, ncolumns
++, COL_ACTION
);
1800 add_column(columns
, ncolumns
++, COL_TARGET
);
1801 add_column(columns
, ncolumns
++, COL_SOURCE
);
1802 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1803 add_column(columns
, ncolumns
++, COL_OPTIONS
);
1806 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1807 &ncolumns
, column_name_to_id
) < 0)
1808 return EXIT_FAILURE
;
1811 tabtype
= verify
? TABTYPE_FSTAB
: TABTYPE_KERNEL
;
1813 if ((flags
& FL_POLL
) && ntabfiles
> 1)
1814 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1816 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1817 errx(EXIT_FAILURE
, _(
1818 "options --target and --source can't be used together "
1819 "with command line element that is not an option"));
1822 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1824 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1826 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1827 /* don't care about submounts if list all mounts */
1828 flags
&= ~FL_SUBMOUNTS
;
1830 if (!(flags
& FL_SUBMOUNTS
) && ((flags
& FL_FIRSTONLY
)
1831 || get_match(COL_TARGET
)
1832 || get_match(COL_SOURCE
)
1833 || get_match(COL_MAJMIN
)))
1836 if (!(flags
& FL_NOSWAPMATCH
) &&
1837 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1839 * Check if we can swap source and target, it's
1840 * not possible if the source is LABEL=/UUID=
1842 const char *x
= get_match(COL_SOURCE
);
1844 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1845 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1846 flags
|= FL_NOSWAPMATCH
;
1850 * initialize libmount
1854 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1858 if (tabtype
== TABTYPE_MTAB
&& tab_is_kernel(tb
))
1859 tabtype
= TABTYPE_KERNEL
;
1861 istree
= tab_is_tree(tb
);
1862 if (istree
&& force_tree
)
1865 if ((flags
& FL_TREE
) && (ntabfiles
> 1 || !istree
))
1868 if (!(flags
& FL_NOCACHE
)) {
1869 cache
= mnt_new_cache();
1871 warn(_("failed to initialize libmount cache"));
1874 mnt_table_set_cache(tb
, cache
);
1876 if (tabtype
!= TABTYPE_KERNEL
)
1877 cache_set_targets(cache
);
1880 if (flags
& FL_UNIQ
)
1881 mnt_table_uniq_fs(tb
, MNT_UNIQ_KEEPTREE
, uniq_fs_target_cmp
);
1884 rc
= verify_table(tb
);
1889 * initialize libsmartcols
1891 scols_init_debug(0);
1892 table
= scols_new_table();
1894 warn(_("failed to allocate output table"));
1897 scols_table_enable_raw(table
, !!(flags
& FL_RAW
));
1898 scols_table_enable_export(table
, !!(flags
& FL_EXPORT
));
1899 scols_table_enable_shellvar(table
, !!(flags
& FL_SHELLVAR
));
1900 scols_table_enable_json(table
, !!(flags
& FL_JSON
));
1901 scols_table_enable_ascii(table
, !!(flags
& FL_ASCII
));
1902 scols_table_enable_noheadings(table
, !!(flags
& FL_NOHEADINGS
));
1904 if (flags
& FL_JSON
)
1905 scols_table_set_name(table
, "filesystems");
1907 for (i
= 0; i
< ncolumns
; i
++) {
1908 struct libscols_column
*cl
;
1909 int fl
= get_column_flags(i
);
1910 int id
= get_column_id(i
);
1912 if (!(flags
& FL_TREE
))
1913 fl
&= ~SCOLS_FL_TREE
;
1915 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1916 warnx(_("%s column is requested, but --poll "
1917 "is not enabled"), get_column_name(i
));
1920 cl
= scols_table_new_column(table
, get_column_name(i
),
1921 get_column_whint(i
), fl
);
1923 warn(_("failed to allocate output column"));
1926 /* multi-line cells (now used for SOURCES) */
1927 if (fl
& SCOLS_FL_WRAP
)
1928 scols_column_set_wrapfunc(cl
,
1930 scols_wrapzero_nextchunk
,
1932 if (flags
& FL_JSON
)
1933 scols_column_set_json_type(cl
, get_column_json_type(id
, fl
, NULL
));
1937 * Fill in data to the output table
1939 if (flags
& FL_POLL
) {
1940 /* poll mode (accept the first tabfile only) */
1941 rc
= poll_table(tb
, tabfiles
? *tabfiles
: _PATH_PROC_MOUNTINFO
, timeout
, table
, direction
);
1943 } else if ((flags
& FL_TREE
) && !(flags
& FL_SUBMOUNTS
)) {
1945 rc
= create_treenode(table
, tb
, NULL
, NULL
);
1947 /* whole list of sub-tree */
1948 rc
= add_matching_lines(tb
, table
, direction
);
1951 && tabtype
== TABTYPE_KERNEL
1952 && (flags
& FL_NOSWAPMATCH
)
1953 && !(flags
& FL_STRICTTARGET
)
1954 && get_match(COL_TARGET
)) {
1956 * Found nothing, maybe the --target is regular file,
1957 * try it again with extra functionality for target
1960 enable_extra_target_match(tb
);
1961 rc
= add_matching_lines(tb
, table
, direction
);
1966 * Print the output table for non-poll modes
1968 if (!rc
&& !(flags
& FL_POLL
))
1969 scols_print_table(table
);
1971 scols_unref_table(table
);
1973 mnt_unref_table(tb
);
1974 mnt_unref_cache(cache
);
1980 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;