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.
28 #ifdef HAVE_SYS_IOCTL_H
29 # include <sys/ioctl.h>
33 #include <sys/statvfs.h>
34 #include <sys/types.h>
40 #include <libsmartcols.h>
42 #include "pathnames.h"
44 #include "closestream.h"
51 #include "column-list-table.h"
94 const char * const name
; /* header */
95 double whint
; /* width hint (N < 1 is in percent of termwidth) */
96 int flags
; /* libsmartcols flags */
97 const char *help
; /* column description */
98 const char *match
; /* pattern for match_func() */
99 void *match_data
; /* match specific data */
102 /* columns descriptions (don't use const, this is writable) */
103 static struct colinfo infos
[] = {
104 [COL_ACTION
] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH
, N_("action detected by --poll") },
105 [COL_AVAIL
] = { "AVAIL", 5, SCOLS_FL_RIGHT
, N_("filesystem size available, use <number> if --bytes is given") },
106 [COL_FREQ
] = { "FREQ", 1, SCOLS_FL_RIGHT
, N_("dump(8) period in days [fstab only]") },
107 [COL_FSROOT
] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES
, N_("filesystem root") },
108 [COL_FSTYPE
] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC
, N_("filesystem type") },
109 [COL_FS_OPTIONS
] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("FS specific mount options") },
110 [COL_ID
] = { "ID", 2, SCOLS_FL_RIGHT
, N_("mount ID") },
111 [COL_LABEL
] = { "LABEL", 0.10, 0, N_("filesystem label") },
112 [COL_MAJMIN
] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
113 [COL_OLD_OPTIONS
] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("old mount options saved by --poll") },
114 [COL_OLD_TARGET
] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
115 [COL_OPTIONS
] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC
, N_("all mount options") },
116 [COL_OPT_FIELDS
] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC
, N_("optional mount fields") },
117 [COL_PARENT
] = { "PARENT", 2, SCOLS_FL_RIGHT
, N_("mount parent ID") },
118 [COL_PARTLABEL
] = { "PARTLABEL", 0.10, 0, N_("partition label") },
119 [COL_PARTUUID
] = { "PARTUUID", 36, 0, N_("partition UUID") },
120 [COL_PASSNO
] = { "PASSNO", 1, SCOLS_FL_RIGHT
, N_("pass number on parallel fsck(8) [fstab only]") },
121 [COL_PROPAGATION
] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
122 [COL_SIZE
] = { "SIZE", 5, SCOLS_FL_RIGHT
, N_("filesystem size, use <number> if --bytes is given") },
123 [COL_SOURCES
] = { "SOURCES", 0.25, SCOLS_FL_WRAP
, N_("all possible source devices") },
124 [COL_SOURCE
] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES
, N_("source device") },
125 [COL_TARGET
] = { "TARGET", 0.30, SCOLS_FL_TREE
| SCOLS_FL_NOEXTREMES
, N_("mountpoint") },
126 [COL_TID
] = { "TID", 4, SCOLS_FL_RIGHT
, N_("task ID") },
127 [COL_USED
] = { "USED", 5, SCOLS_FL_RIGHT
, N_("filesystem size used, use <number> if --bytes is given") },
128 [COL_USEPERC
] = { "USE%", 3, SCOLS_FL_RIGHT
, N_("filesystem use percentage") },
129 [COL_UUID
] = { "UUID", 36, 0, N_("filesystem UUID") },
130 [COL_VFS_OPTIONS
] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC
, N_("VFS specific mount options") }
133 /* columns[] array specifies all currently wanted output column. The columns
134 * are defined by infos[] array and you can specify (on command line) each
135 * column twice. That's enough, dynamically allocated array of the columns is
136 * unnecessary overkill and over-engineering in this case */
137 static int columns
[ARRAY_SIZE(infos
) * 2];
138 static size_t ncolumns
;
140 static inline size_t err_columns_index(size_t arysz
, size_t idx
)
143 errx(EXIT_FAILURE
, _("too many columns specified, "
144 "the limit is %zu columns"),
149 #define add_column(ary, n, id) \
150 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
152 /* poll actions (parsed --poll=<list> */
153 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
154 static int actions
[FINDMNT_NACTIONS
];
157 /* global (accessed from findmnt-verify.c too) */
160 struct libmnt_cache
*cache
;
162 static blkid_cache blk_cache
;
165 static struct udev
*udev
;
168 static int match_func(struct libmnt_fs
*fs
, void *data
__attribute__ ((__unused__
)));
171 static int get_column_id(int num
)
174 assert((size_t) num
< ncolumns
);
175 assert((size_t) columns
[num
] < ARRAY_SIZE(infos
));
179 static struct colinfo
*get_column_info(int num
)
181 return &infos
[ get_column_id(num
) ];
184 static const char *column_id_to_name(int id
)
186 assert((size_t) id
< ARRAY_SIZE(infos
));
187 return infos
[id
].name
;
190 static const char *get_column_name(int num
)
192 return get_column_info(num
)->name
;
195 static float get_column_whint(int num
)
197 return get_column_info(num
)->whint
;
200 static int get_column_flags(int num
)
202 return get_column_info(num
)->flags
;
205 static const char *get_match(int id
)
207 assert((size_t) id
< ARRAY_SIZE(infos
));
208 return infos
[id
].match
;
211 static void *get_match_data(int id
)
213 assert((size_t) id
< ARRAY_SIZE(infos
));
214 return infos
[id
].match_data
;
217 static void set_match(int id
, const char *match
)
219 assert((size_t) id
< ARRAY_SIZE(infos
));
220 infos
[id
].match
= match
;
223 static void set_match_data(int id
, void *data
)
225 assert((size_t) id
< ARRAY_SIZE(infos
));
226 infos
[id
].match_data
= data
;
230 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
233 static void set_source_match(const char *data
)
237 if (sscanf(data
, "%d:%d", &maj
, &min
) == 2) {
238 dev_t
*devno
= xmalloc(sizeof(dev_t
));
240 *devno
= makedev(maj
, min
);
241 set_match(COL_MAJMIN
, data
);
242 set_match_data(COL_MAJMIN
, (void *) devno
);
243 flags
|= FL_NOSWAPMATCH
;
245 set_match(COL_SOURCE
, data
);
249 * Extra functionality for --target <path>. The function mnt_table_find_mountpoint()
250 * also checks parents (path elements in reverse order) to get mountpoint.
252 * @tb has to be from kernel (so no fstab or so)!
254 static void enable_extra_target_match(struct libmnt_table
*tb
)
257 const char *tgt
= NULL
, *mnt
= NULL
;
258 struct libmnt_fs
*fs
;
261 * Check if match pattern is mountpoint, if not use the
264 if (flags
& FL_NOCACHE
)
265 tgt
= get_match(COL_TARGET
);
267 tgt
= cn
= mnt_resolve_path(get_match(COL_TARGET
), cache
);
272 fs
= mnt_table_find_mountpoint(tb
, tgt
, MNT_ITER_BACKWARD
);
274 mnt
= mnt_fs_get_target(fs
);
275 if (mnt
&& strcmp(mnt
, tgt
) != 0)
276 set_match(COL_TARGET
, xstrdup(mnt
)); /* replace the current setting */
283 static int is_tabdiff_column(int id
)
285 assert((size_t) id
< ARRAY_SIZE(infos
));
290 case COL_OLD_OPTIONS
:
299 * "findmnt" without any filter
301 int is_listall_mode(void)
303 if ((flags
& FL_DF
|| flags
& FL_REAL
|| flags
& FL_PSEUDO
) && !(flags
& FL_ALL
))
306 return (!get_match(COL_SOURCE
) &&
307 !get_match(COL_TARGET
) &&
308 !get_match(COL_FSTYPE
) &&
309 !get_match(COL_OPTIONS
) &&
310 !get_match(COL_MAJMIN
));
314 * Returns 1 if the @act is in the --poll=<list>
316 static int has_poll_action(int act
)
321 return 1; /* all actions enabled */
322 for (i
= 0; i
< nactions
; i
++)
323 if (actions
[i
] == act
)
328 static int poll_action_name_to_id(const char *name
, size_t namesz
)
332 if (strncasecmp(name
, "move", namesz
) == 0 && namesz
== 4)
333 id
= MNT_TABDIFF_MOVE
;
334 else if (strncasecmp(name
, "mount", namesz
) == 0 && namesz
== 5)
335 id
= MNT_TABDIFF_MOUNT
;
336 else if (strncasecmp(name
, "umount", namesz
) == 0 && namesz
== 6)
337 id
= MNT_TABDIFF_UMOUNT
;
338 else if (strncasecmp(name
, "remount", namesz
) == 0 && namesz
== 7)
339 id
= MNT_TABDIFF_REMOUNT
;
341 warnx(_("unknown action: %s"), name
);
347 * findmnt --first-only <devname|TAG=|mountpoint>
349 * ... it works like "mount <devname|TAG=|mountpoint>"
351 static int is_mount_compatible_mode(void)
353 if (!get_match(COL_SOURCE
))
354 return 0; /* <devname|TAG=|mountpoint> is required */
355 if (get_match(COL_FSTYPE
) || get_match(COL_OPTIONS
))
356 return 0; /* cannot be restricted by -t or -O */
357 if (!(flags
& FL_FIRSTONLY
))
358 return 0; /* we have to return the first entry only */
363 static void disable_columns_truncate(void)
367 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
368 infos
[i
].flags
&= ~SCOLS_FL_TRUNC
;
372 * converts @name to column ID
374 static int column_name_to_id(const char *name
, size_t namesz
)
378 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
379 const char *cn
= column_id_to_name(i
);
381 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
384 warnx(_("unknown column: %s"), name
);
390 static char *get_tag_from_udev(const char *devname
, int col
)
392 struct udev_device
*dev
;
393 const char *data
= NULL
;
394 char *res
= NULL
, *path
;
401 /* libudev don't like /dev/mapper/ symlinks */
402 path
= realpath(devname
, NULL
);
406 if (strncmp(devname
, "/dev/", 5) == 0)
409 dev
= udev_device_new_from_subsystem_sysname(udev
, "block", devname
);
417 data
= udev_device_get_property_value(dev
, "ID_FS_LABEL_ENC");
420 data
= udev_device_get_property_value(dev
, "ID_FS_UUID_ENC");
423 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_UUID");
426 data
= udev_device_get_property_value(dev
, "ID_PART_ENTRY_NAME");
434 unhexmangle_string(res
);
437 udev_device_unref(dev
);
440 #endif /* HAVE_LIBUDEV */
442 /* Returns LABEL or UUID */
443 static char *get_tag(struct libmnt_fs
*fs
, const char *tagname
, int col
445 __attribute__((__unused__
))
452 if (!mnt_fs_get_tag(fs
, &t
, &v
) && !strcmp(t
, tagname
))
455 const char *dev
= mnt_fs_get_source(fs
);
457 if (dev
&& !(flags
& FL_NOCACHE
))
458 dev
= mnt_resolve_spec(dev
, cache
);
461 res
= get_tag_from_udev(dev
, col
);
464 res
= mnt_cache_find_tag_value(cache
, dev
, tagname
);
466 /* don't return pointer to cache */
474 static char *get_vfs_attr(struct libmnt_fs
*fs
, int sizetype
)
477 uint64_t vfs_attr
= 0;
480 if (statvfs(mnt_fs_get_target(fs
), &buf
) != 0)
485 vfs_attr
= buf
.f_frsize
* buf
.f_blocks
;
488 vfs_attr
= buf
.f_frsize
* buf
.f_bavail
;
491 vfs_attr
= buf
.f_frsize
* (buf
.f_blocks
- buf
.f_bfree
);
494 if (buf
.f_blocks
== 0)
497 xasprintf(&sizestr
, "%.0f%%",
498 (double)(buf
.f_blocks
- buf
.f_bfree
) /
504 sizestr
= xstrdup("0");
505 else if (flags
& FL_BYTES
)
506 xasprintf(&sizestr
, "%ju", vfs_attr
);
508 sizestr
= size_to_human_string(SIZE_SUFFIX_1LETTER
, vfs_attr
);
513 /* reads sources from libmount/libblkid
515 static char *get_data_col_sources(struct libmnt_fs
*fs
, int evaluate
, size_t *datasiz
)
517 const char *tag
= NULL
, *p
= NULL
;
519 const char *device
= NULL
;
521 blkid_dev_iterate iter
;
523 struct ul_buffer buf
= UL_INIT_BUFFER
;
525 /* get TAG from libmount if avalable (e.g. fstab) */
526 if (mnt_fs_get_tag(fs
, &tag
, &p
) == 0) {
528 /* if device is in the form 'UUID=..' or 'LABEL=..' and evaluate==0
529 * then it is ok to do not search for multiple devices
536 /* or get device name */
537 else if (!(device
= mnt_fs_get_srcpath(fs
))
538 || strncmp(device
, "/dev/", 5) != 0)
542 if (blkid_get_cache(&blk_cache
, NULL
) != 0)
544 blkid_probe_all(blk_cache
);
547 /* ask libblkid for the UUID */
552 val
= blkid_get_tag_value(blk_cache
, "UUID", device
); /* returns allocated string */
560 /* scan all devices for the TAG */
561 iter
= blkid_dev_iterate_begin(blk_cache
);
562 blkid_dev_set_search(iter
, tag
, val
);
564 while (blkid_dev_next(iter
, &dev
) == 0) {
565 dev
= blkid_verify(blk_cache
, dev
);
569 ul_buffer_append_data(&buf
, "\0", 1);
570 ul_buffer_append_string(&buf
, blkid_dev_devname(dev
));
573 blkid_dev_iterate_end(iter
);
576 return ul_buffer_get_data(&buf
, datasiz
, NULL
);
583 /* reads FS data from libmount
585 static char *get_data(struct libmnt_fs
*fs
, int num
, size_t *datasiz
)
588 int col_id
= get_column_id(num
);
592 /* print all devices with the same tag (LABEL, UUID) */
593 str
= get_data_col_sources(fs
, flags
& FL_EVALUATE
, datasiz
);
600 const char *root
= mnt_fs_get_root(fs
);
601 const char *spec
= mnt_fs_get_srcpath(fs
);
604 if (spec
&& (flags
& FL_CANONICALIZE
))
605 spec
= cn
= mnt_resolve_path(spec
, cache
);
607 spec
= mnt_fs_get_source(fs
);
609 if (spec
&& (flags
& FL_EVALUATE
))
610 spec
= cn
= mnt_resolve_spec(spec
, cache
);
612 if (root
&& spec
&& !(flags
& FL_NOFSROOT
) && strcmp(root
, "/") != 0)
613 xasprintf(&str
, "%s[%s]", spec
, root
);
622 if (mnt_fs_get_target(fs
))
623 str
= xstrdup(mnt_fs_get_target(fs
));
626 if (mnt_fs_get_fstype(fs
))
627 str
= xstrdup(mnt_fs_get_fstype(fs
));
630 if (mnt_fs_get_options(fs
))
631 str
= xstrdup(mnt_fs_get_options(fs
));
633 case COL_VFS_OPTIONS
:
634 if (flags
& FL_VFS_ALL
)
635 str
= mnt_fs_get_vfs_options_all(fs
);
636 else if (mnt_fs_get_vfs_options(fs
))
637 str
= xstrdup(mnt_fs_get_vfs_options(fs
));
640 if (mnt_fs_get_fs_options(fs
))
641 str
= xstrdup(mnt_fs_get_fs_options(fs
));
644 if (mnt_fs_get_optional_fields(fs
))
645 str
= xstrdup(mnt_fs_get_optional_fields(fs
));
648 str
= get_tag(fs
, "UUID", col_id
);
651 str
= get_tag(fs
, "PARTUUID", col_id
);
654 str
= get_tag(fs
, "LABEL", col_id
);
657 str
= get_tag(fs
, "PARTLABEL", col_id
);
662 dev_t devno
= mnt_fs_get_devno(fs
);
666 if ((flags
& FL_RAW
) || (flags
& FL_EXPORT
) || (flags
& FL_JSON
))
667 xasprintf(&str
, "%u:%u", major(devno
), minor(devno
));
669 xasprintf(&str
, "%3u:%-3u", major(devno
), minor(devno
));
676 str
= get_vfs_attr(fs
, col_id
);
679 if (mnt_fs_get_root(fs
))
680 str
= xstrdup(mnt_fs_get_root(fs
));
683 if (mnt_fs_get_tid(fs
))
684 xasprintf(&str
, "%d", mnt_fs_get_tid(fs
));
687 if (mnt_fs_get_id(fs
))
688 xasprintf(&str
, "%d", mnt_fs_get_id(fs
));
691 if (mnt_fs_get_parent_id(fs
))
692 xasprintf(&str
, "%d", mnt_fs_get_parent_id(fs
));
694 case COL_PROPAGATION
:
695 if (mnt_fs_is_kernel(fs
)) {
696 unsigned long fl
= 0;
699 if (mnt_fs_get_propagation(fs
, &fl
) != 0)
702 n
= xstrdup((fl
& MS_SHARED
) ? "shared" : "private");
705 xasprintf(&str
, "%s,slave", n
);
709 if (fl
& MS_UNBINDABLE
) {
710 xasprintf(&str
, "%s,unbindable", n
);
718 if (!mnt_fs_is_kernel(fs
))
719 xasprintf(&str
, "%d", mnt_fs_get_freq(fs
));
722 if (!mnt_fs_is_kernel(fs
))
723 xasprintf(&str
, "%d", mnt_fs_get_passno(fs
));
731 static char *get_tabdiff_data(struct libmnt_fs
*old_fs
,
732 struct libmnt_fs
*new_fs
,
739 switch (get_column_id(num
)) {
742 case MNT_TABDIFF_MOUNT
:
745 case MNT_TABDIFF_UMOUNT
:
748 case MNT_TABDIFF_REMOUNT
:
751 case MNT_TABDIFF_MOVE
:
760 case COL_OLD_OPTIONS
:
761 if (old_fs
&& (change
== MNT_TABDIFF_REMOUNT
||
762 change
== MNT_TABDIFF_UMOUNT
)
763 && mnt_fs_get_options(old_fs
))
764 str
= xstrdup(mnt_fs_get_options(old_fs
));
767 if (old_fs
&& (change
== MNT_TABDIFF_MOVE
||
768 change
== MNT_TABDIFF_UMOUNT
)
769 && mnt_fs_get_target(old_fs
))
770 str
= xstrdup(mnt_fs_get_target(old_fs
));
774 str
= get_data(new_fs
, num
, datasiz
);
776 str
= get_data(old_fs
, num
, datasiz
);
782 static void set_line_data(struct libscols_line
*ln
, size_t i
, char *data
, size_t datasiz
)
785 struct libscols_cell
*ce
;
787 ce
= scols_line_get_cell(ln
, i
);
791 rc
= scols_cell_refer_memory(ce
, data
, datasiz
);
793 rc
= scols_cell_refer_data(ce
, data
);
795 err(EXIT_FAILURE
, _("failed to add output data"));
798 /* adds one line to the output @tab */
799 static struct libscols_line
*add_line(struct libscols_table
*table
, struct libmnt_fs
*fs
,
800 struct libscols_line
*parent
)
803 struct libscols_line
*line
= scols_table_new_line(table
, parent
);
806 err(EXIT_FAILURE
, _("failed to allocate output line"));
808 for (i
= 0; i
< ncolumns
; i
++) {
810 char *data
= get_data(fs
, i
, &datasiz
);
813 set_line_data(line
, i
, data
, datasiz
);
816 scols_line_set_userdata(line
, fs
);
820 static struct libscols_line
*add_tabdiff_line(struct libscols_table
*table
, struct libmnt_fs
*new_fs
,
821 struct libmnt_fs
*old_fs
, int change
)
824 struct libscols_line
*line
= scols_table_new_line(table
, NULL
);
827 err(EXIT_FAILURE
, _("failed to allocate output line"));
829 for (i
= 0; i
< ncolumns
; i
++) {
831 char *data
= get_tabdiff_data(old_fs
, new_fs
, change
, i
, &datasiz
);
834 set_line_data(line
, i
, data
, datasiz
);
840 static int has_line(struct libscols_table
*table
, struct libmnt_fs
*fs
)
842 struct libscols_line
*ln
;
843 struct libscols_iter
*itr
;
846 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
850 while (scols_table_next_line(table
, itr
, &ln
) == 0) {
851 if ((struct libmnt_fs
*) scols_line_get_userdata(ln
) == fs
) {
857 scols_free_iter(itr
);
861 /* reads filesystems from @tb (libmount) and fillin @table (output table) */
862 static int create_treenode(struct libscols_table
*table
, struct libmnt_table
*tb
,
863 struct libmnt_fs
*fs
, struct libscols_line
*parent_line
)
865 struct libmnt_fs
*chld
= NULL
;
866 struct libmnt_iter
*itr
= NULL
;
867 struct libscols_line
*line
;
868 int rc
= -1, first
= 0;
871 /* first call, get root FS */
872 if (mnt_table_get_root_fs(tb
, &fs
))
877 } else if ((flags
& FL_SUBMOUNTS
) && has_line(table
, fs
))
880 itr
= mnt_new_iter(MNT_ITER_FORWARD
);
884 if ((flags
& FL_SUBMOUNTS
) || match_func(fs
, NULL
)) {
885 line
= add_line(table
, fs
, parent_line
);
892 * add all children to the output table
894 while (mnt_table_next_child_fs(tb
, itr
, fs
, &chld
) == 0) {
895 if (create_treenode(table
, tb
, chld
, line
))
900 /* make sure all entries are in the tree */
901 if (first
&& (size_t) mnt_table_get_nents(tb
) >
902 (size_t) scols_table_get_nlines(table
)) {
903 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
906 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
907 if (!has_line(table
, fs
) && match_func(fs
, NULL
))
908 create_treenode(table
, tb
, fs
, NULL
);
917 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
918 const char *filename
, int line
)
920 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
925 static char **append_tabfile(char **files
, int *nfiles
, char *filename
)
927 files
= xreallocarray(files
, *nfiles
+ 1, sizeof(char *));
928 files
[(*nfiles
)++] = filename
;
932 static char **append_pid_tabfile(char **files
, int *nfiles
, pid_t pid
)
936 xasprintf(&path
, "/proc/%d/mountinfo", (int) pid
);
937 return append_tabfile(files
, nfiles
, path
);
940 /* calls libmount fstab/mtab/mountinfo parser */
941 static struct libmnt_table
*parse_tabfiles(char **files
,
945 struct libmnt_table
*tb
;
948 tb
= mnt_new_table();
950 warn(_("failed to initialize libmount table"));
953 mnt_table_set_parser_errcb(tb
, parser_errcb
);
956 /* NULL means that libmount will use default paths */
957 const char *path
= nfiles
? *files
++ : NULL
;
961 rc
= mnt_table_parse_fstab(tb
, path
);
964 rc
= mnt_table_parse_mtab(tb
, path
);
968 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
969 _PATH_PROC_MOUNTINFO
:
972 rc
= mnt_table_parse_file(tb
, path
);
977 warn(_("can't read %s"), path
);
980 } while (--nfiles
> 0);
986 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
987 * necessary if @tb in main() was read from a non-kernel source.
989 static void cache_set_targets(struct libmnt_cache
*tmp
)
991 struct libmnt_table
*tb
;
994 tb
= mnt_new_table();
998 path
= access(_PATH_PROC_MOUNTINFO
, R_OK
) == 0 ?
999 _PATH_PROC_MOUNTINFO
:
1002 if (mnt_table_parse_file(tb
, path
) == 0)
1003 mnt_cache_set_targets(tmp
, tb
);
1005 mnt_unref_table(tb
);
1008 /* checks if @tb contains parent->child relations */
1009 static int tab_is_tree(struct libmnt_table
*tb
)
1011 struct libmnt_fs
*fs
= NULL
;
1012 struct libmnt_iter
*itr
;
1015 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
1019 rc
= (mnt_table_next_fs(tb
, itr
, &fs
) == 0 &&
1020 mnt_fs_is_kernel(fs
) &&
1021 mnt_fs_get_root(fs
));
1027 /* checks if all fs in @tb are from kernel */
1028 static int tab_is_kernel(struct libmnt_table
*tb
)
1030 struct libmnt_fs
*fs
= NULL
;
1031 struct libmnt_iter
*itr
;
1033 itr
= mnt_new_iter(MNT_ITER_BACKWARD
);
1037 while (mnt_table_next_fs(tb
, itr
, &fs
) == 0) {
1038 if (!mnt_fs_is_kernel(fs
)) {
1048 /* filter function for libmount (mnt_table_find_next_fs()) */
1049 static int match_func(struct libmnt_fs
*fs
,
1050 void *data
__attribute__ ((__unused__
)))
1052 int rc
= flags
& FL_INVERT
? 1 : 0;
1056 m
= get_match(COL_FSTYPE
);
1057 if (m
&& !mnt_fs_match_fstype(fs
, m
))
1060 m
= get_match(COL_OPTIONS
);
1061 if (m
&& !mnt_fs_match_options(fs
, m
))
1064 md
= get_match_data(COL_MAJMIN
);
1065 if (md
&& mnt_fs_get_devno(fs
) != *((dev_t
*) md
))
1068 m
= get_match(COL_TARGET
);
1069 if (m
&& !mnt_fs_match_target(fs
, m
, cache
))
1072 m
= get_match(COL_SOURCE
);
1073 if (m
&& !mnt_fs_match_source(fs
, m
, cache
))
1076 if ((flags
& FL_DF
) && !(flags
& FL_ALL
)) {
1077 const char *type
= mnt_fs_get_fstype(fs
);
1079 if (type
&& strstr(type
, "tmpfs")) /* tmpfs is wanted */
1082 if (mnt_fs_is_pseudofs(fs
))
1086 if ((flags
& FL_REAL
) && mnt_fs_is_pseudofs(fs
))
1089 if ((flags
& FL_PSEUDO
) && !mnt_fs_is_pseudofs(fs
))
1092 if ((flags
& FL_SHADOWED
)) {
1093 struct libmnt_table
*tb
= NULL
;
1095 mnt_fs_get_table(fs
, &tb
);
1096 if (tb
&& mnt_table_over_fs(tb
, fs
, NULL
) != 0)
1103 /* iterate over filesystems in @tb */
1104 struct libmnt_fs
*get_next_fs(struct libmnt_table
*tb
,
1105 struct libmnt_iter
*itr
)
1107 struct libmnt_fs
*fs
= NULL
;
1109 if (is_listall_mode()) {
1113 if (mnt_table_next_fs(tb
, itr
, &fs
) != 0)
1116 } else if (is_mount_compatible_mode()) {
1118 * Look up for FS in the same way how mount(8) searches in fstab
1122 fs
= mnt_table_find_source(tb
, get_match(COL_SOURCE
),
1123 mnt_iter_get_direction(itr
));
1125 if (!fs
&& !(flags
& FL_NOSWAPMATCH
))
1126 fs
= mnt_table_find_target(tb
, get_match(COL_SOURCE
),
1127 mnt_iter_get_direction(itr
));
1130 * Look up for all matching entries
1132 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
1133 * findmnt [-l] <spec> [-O <options>] [-t <types>]
1136 if (mnt_table_find_next_fs(tb
, itr
, match_func
, NULL
, &fs
) != 0)
1140 !(flags
& FL_NOSWAPMATCH
) &&
1141 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1143 /* swap 'spec' and target. */
1144 set_match(COL_TARGET
, get_match(COL_SOURCE
));
1145 set_match(COL_SOURCE
, NULL
);
1146 mnt_reset_iter(itr
, -1);
1156 * Filter out unwanted lines for --list output or top level lines for
1157 * --submounts tree output.
1159 static int add_matching_lines(struct libmnt_table
*tb
,
1160 struct libscols_table
*table
, int direction
)
1162 struct libmnt_iter
*itr
;
1163 struct libmnt_fs
*fs
;
1164 int nlines
= 0, rc
= -1;
1166 itr
= mnt_new_iter(direction
);
1168 warn(_("failed to initialize libmount iterator"));
1172 while((fs
= get_next_fs(tb
, itr
))) {
1173 if ((flags
& FL_TREE
) || (flags
& FL_SUBMOUNTS
))
1174 rc
= create_treenode(table
, tb
, fs
, NULL
);
1176 rc
= !add_line(table
, fs
, NULL
);
1180 if (flags
& FL_FIRSTONLY
)
1182 flags
|= FL_NOSWAPMATCH
;
1192 static int poll_match(struct libmnt_fs
*fs
)
1194 int rc
= match_func(fs
, NULL
);
1196 if (rc
== 0 && !(flags
& FL_NOSWAPMATCH
) &&
1197 get_match(COL_SOURCE
) && !get_match(COL_TARGET
)) {
1199 * findmnt --poll /foo
1200 * The '/foo' maybe source as well as target.
1202 const char *str
= get_match(COL_SOURCE
);
1204 set_match(COL_TARGET
, str
); /* swap */
1205 set_match(COL_SOURCE
, NULL
);
1207 rc
= match_func(fs
, NULL
);
1209 set_match(COL_TARGET
, NULL
); /* restore */
1210 set_match(COL_SOURCE
, str
);
1216 static int poll_table(struct libmnt_table
*tb
, const char *tabfile
,
1217 int timeout
, struct libscols_table
*table
, int direction
)
1221 struct libmnt_iter
*itr
= NULL
;
1222 struct libmnt_table
*tb_new
;
1223 struct libmnt_tabdiff
*diff
= NULL
;
1224 struct pollfd fds
[1];
1226 tb_new
= mnt_new_table();
1228 warn(_("failed to initialize libmount table"));
1232 itr
= mnt_new_iter(direction
);
1234 warn(_("failed to initialize libmount iterator"));
1238 diff
= mnt_new_tabdiff();
1240 warn(_("failed to initialize libmount tabdiff"));
1244 /* cache is unnecessary to detect changes */
1245 mnt_table_set_cache(tb
, NULL
);
1246 mnt_table_set_cache(tb_new
, NULL
);
1248 f
= fopen(tabfile
, "r");
1250 warn(_("cannot open %s"), tabfile
);
1254 mnt_table_set_parser_errcb(tb_new
, parser_errcb
);
1256 fds
[0].fd
= fileno(f
);
1257 fds
[0].events
= POLLPRI
;
1260 struct libmnt_table
*tmp
;
1261 struct libmnt_fs
*old
, *new;
1264 count
= poll(fds
, 1, timeout
);
1266 break; /* timeout */
1268 warn(_("poll() failed"));
1273 rc
= mnt_table_parse_stream(tb_new
, f
, tabfile
);
1275 rc
= mnt_diff_tables(diff
, tb
, tb_new
);
1280 mnt_reset_iter(itr
, direction
);
1281 while(mnt_tabdiff_next_change(
1282 diff
, itr
, &old
, &new, &change
) == 0) {
1284 if (!has_poll_action(change
))
1286 if (!poll_match(new ? new : old
))
1289 rc
= !add_tabdiff_line(table
, new, old
, change
);
1292 if (flags
& FL_FIRSTONLY
)
1297 rc
= scols_table_print_range(table
, NULL
, NULL
);
1298 fflush(scols_table_get_stream(table
));
1308 /* remove already printed lines to reduce memory usage */
1309 scols_table_remove_lines(table
);
1310 mnt_reset_table(tb_new
);
1312 if (count
&& (flags
& FL_FIRSTONLY
))
1318 mnt_unref_table(tb_new
);
1319 mnt_free_tabdiff(diff
);
1326 static int uniq_fs_target_cmp(
1327 struct libmnt_table
*tb
__attribute__((__unused__
)),
1328 struct libmnt_fs
*a
,
1329 struct libmnt_fs
*b
)
1331 return !mnt_fs_match_target(a
, mnt_fs_get_target(b
), cache
);
1334 static int get_column_json_type(int id
, int scols_flags
, int *multi
)
1342 if (!(flags
& FL_BYTES
))
1350 return SCOLS_JSON_NUMBER
;
1353 if (scols_flags
& SCOLS_FL_WRAP
)
1354 return SCOLS_JSON_ARRAY_STRING
;
1358 return SCOLS_JSON_STRING
; /* default */
1361 static void __attribute__((__noreturn__
)) usage(void)
1365 fputs(USAGE_HEADER
, out
);
1368 " %1$s [options] <device> | <mountpoint>\n"
1369 " %1$s [options] <device> <mountpoint>\n"
1370 " %1$s [options] [--source <device>] [--target <path> | --mountpoint <dir>]\n"),
1371 program_invocation_short_name
);
1373 fputs(USAGE_SEPARATOR
, out
);
1374 fputs(_("Find a (mounted) filesystem.\n"), out
);
1376 fputs(USAGE_OPTIONS
, out
);
1377 fputs(_(" -s, --fstab search in static table of filesystems\n"), out
);
1378 fputs(_(" -m, --mtab search in table of mounted filesystems\n"
1379 " (includes user space mount options)\n"), out
);
1380 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1381 " filesystems (default)\n"), out
);
1383 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out
);
1384 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out
);
1387 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out
);
1388 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out
);
1389 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
1390 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out
);
1391 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out
);
1392 fputs(_(" -D, --df imitate the output of df(1)\n"), out
);
1393 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out
);
1394 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1395 " to device names\n"), out
);
1396 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out
);
1397 fputs(_(" -f, --first-only print the first found filesystem only\n"), out
);
1398 fputs(_(" -i, --invert invert the sense of matching\n"), out
);
1399 fputs(_(" -J, --json use JSON output format\n"), out
);
1400 fputs(_(" -l, --list use list format output\n"), out
);
1401 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out
);
1402 fputs(_(" -n, --noheadings don't print column headings\n"), out
);
1403 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out
);
1404 fputs(_(" -o, --output <list> output columns (see --list-columns)\n"), out
);
1405 fputs(_(" --output-all output all available columns\n"), out
);
1406 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out
);
1407 fputs(_(" --pseudo print only pseudo-filesystems\n"), out
);
1408 fputs(_(" --shadowed print only filesystems over-mounted by another filesystem\n"), out
);
1409 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out
);
1410 fputs(_(" -r, --raw use raw output format\n"), out
);
1411 fputs(_(" --real print only real filesystems\n"), out
);
1412 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1413 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out
);
1414 fputs(_(" -T, --target <path> the path to the filesystem to use\n"), out
);
1415 fputs(_(" --tree enable tree format output if possible\n"), out
);
1416 fputs(_(" -M, --mountpoint <dir> the mountpoint directory\n"), out
);
1417 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out
);
1418 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out
);
1419 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out
);
1420 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out
);
1421 fputs(_(" -y, --shell use column names to be usable as shell variable identifiers\n"), out
);
1424 fputs(_(" -x, --verify verify mount table content (default is fstab)\n"), out
);
1425 fputs(_(" --verbose print more details\n"), out
);
1426 fputs(_(" --vfs-all print all VFS options\n"), out
);
1428 fputs(USAGE_SEPARATOR
, out
);
1429 fputs(_(" -H, --list-columns list the available columns\n"), out
);
1430 fprintf(out
, USAGE_HELP_OPTIONS(24));
1432 fprintf(out
, USAGE_MAN_TAIL("findmnt(8)"));
1437 static void __attribute__((__noreturn__
)) list_colunms(void)
1440 struct libscols_table
*tb
= xcolumn_list_table_new("findmnt-columns", stdout
,
1444 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
1445 const struct colinfo
*ci
= &infos
[i
];
1447 int json
= get_column_json_type(i
, ci
->flags
, &multi
);
1449 xcolumn_list_table_append_line(tb
, ci
->name
,
1451 multi
? "<string|number>" : NULL
,
1455 scols_print_table(tb
);
1456 scols_unref_table(tb
);
1462 int main(int argc
, char *argv
[])
1464 struct libmnt_table
*tb
= NULL
;
1465 char **tabfiles
= NULL
;
1466 int direction
= MNT_ITER_FORWARD
;
1467 int verify
= 0, collist
= 0;
1468 int c
, rc
= -1, timeout
= -1;
1469 int ntabfiles
= 0, tabtype
= 0;
1470 char *outarg
= NULL
;
1472 int force_tree
= 0, istree
= 0;
1474 struct libscols_table
*table
= NULL
;
1477 FINDMNT_OPT_VERBOSE
= CHAR_MAX
+ 1,
1479 FINDMNT_OPT_OUTPUT_ALL
,
1482 FINDMNT_OPT_VFS_ALL
,
1483 FINDMNT_OPT_SHADOWED
1486 static const struct option longopts
[] = {
1487 { "all", no_argument
, NULL
, 'A' },
1488 { "ascii", no_argument
, NULL
, 'a' },
1489 { "bytes", no_argument
, NULL
, 'b' },
1490 { "canonicalize", no_argument
, NULL
, 'c' },
1491 { "direction", required_argument
, NULL
, 'd' },
1492 { "df", no_argument
, NULL
, 'D' },
1493 { "evaluate", no_argument
, NULL
, 'e' },
1494 { "first-only", no_argument
, NULL
, 'f' },
1495 { "fstab", no_argument
, NULL
, 's' },
1496 { "help", no_argument
, NULL
, 'h' },
1497 { "invert", no_argument
, NULL
, 'i' },
1498 { "json", no_argument
, NULL
, 'J' },
1499 { "kernel", no_argument
, NULL
, 'k' },
1500 { "list", no_argument
, NULL
, 'l' },
1501 { "mountpoint", required_argument
, NULL
, 'M' },
1502 { "mtab", no_argument
, NULL
, 'm' },
1503 { "noheadings", no_argument
, NULL
, 'n' },
1504 { "notruncate", no_argument
, NULL
, 'u' },
1505 { "options", required_argument
, NULL
, 'O' },
1506 { "output", required_argument
, NULL
, 'o' },
1507 { "output-all", no_argument
, NULL
, FINDMNT_OPT_OUTPUT_ALL
},
1508 { "poll", optional_argument
, NULL
, 'p' },
1509 { "pairs", no_argument
, NULL
, 'P' },
1510 { "raw", no_argument
, NULL
, 'r' },
1511 { "types", required_argument
, NULL
, 't' },
1512 { "nocanonicalize", no_argument
, NULL
, 'C' },
1513 { "nofsroot", no_argument
, NULL
, 'v' },
1514 { "submounts", no_argument
, NULL
, 'R' },
1515 { "source", required_argument
, NULL
, 'S' },
1516 { "tab-file", required_argument
, NULL
, 'F' },
1517 { "task", required_argument
, NULL
, 'N' },
1518 { "target", required_argument
, NULL
, 'T' },
1519 { "timeout", required_argument
, NULL
, 'w' },
1520 { "uniq", no_argument
, NULL
, 'U' },
1521 { "verify", no_argument
, NULL
, 'x' },
1522 { "version", no_argument
, NULL
, 'V' },
1523 { "shell", no_argument
, NULL
, 'y' },
1524 { "verbose", no_argument
, NULL
, FINDMNT_OPT_VERBOSE
},
1525 { "tree", no_argument
, NULL
, FINDMNT_OPT_TREE
},
1526 { "real", no_argument
, NULL
, FINDMNT_OPT_REAL
},
1527 { "pseudo", no_argument
, NULL
, FINDMNT_OPT_PSEUDO
},
1528 { "vfs-all", no_argument
, NULL
, FINDMNT_OPT_VFS_ALL
},
1529 { "shadowed", no_argument
, NULL
, FINDMNT_OPT_SHADOWED
},
1530 { "list-columns", no_argument
, NULL
, 'H' },
1531 { NULL
, 0, NULL
, 0 }
1534 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1535 { 'C', 'c'}, /* [no]canonicalize */
1536 { 'C', 'e' }, /* nocanonicalize, evaluate */
1537 { 'J', 'P', 'r','x' }, /* json,pairs,raw,verify */
1538 { 'M', 'T' }, /* mountpoint, target */
1539 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1540 { 'P','l','r','x' }, /* pairs,list,raw,verify */
1541 { 'p','x' }, /* poll,verify */
1542 { 'm','p','s' }, /* mtab,poll,fstab */
1543 { FINDMNT_OPT_PSEUDO
, FINDMNT_OPT_REAL
},
1546 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1548 setlocale(LC_ALL
, "");
1549 bindtextdomain(PACKAGE
, LOCALEDIR
);
1550 textdomain(PACKAGE
);
1551 close_stdout_atexit();
1553 /* default output format */
1556 while ((c
= getopt_long(argc
, argv
,
1557 "AabCcDd:ehiJfF:o:O:p::PklmM:nN:rst:uvRS:T:Uw:VxyH",
1558 longopts
, NULL
)) != -1) {
1560 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1573 flags
|= FL_NOCACHE
;
1576 flags
|= FL_CANONICALIZE
;
1583 if (!strcmp(optarg
, "forward"))
1584 direction
= MNT_ITER_FORWARD
;
1585 else if (!strcmp(optarg
, "backward"))
1586 direction
= MNT_ITER_BACKWARD
;
1589 _("unknown direction '%s'"), optarg
);
1592 flags
|= FL_EVALUATE
;
1601 flags
|= FL_FIRSTONLY
;
1604 tabfiles
= append_tabfile(tabfiles
, &ntabfiles
, optarg
);
1607 disable_columns_truncate();
1612 case FINDMNT_OPT_OUTPUT_ALL
:
1614 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
1615 if (is_tabdiff_column(i
))
1617 columns
[ncolumns
++] = i
;
1621 set_match(COL_OPTIONS
, optarg
);
1625 nactions
= string_to_idarray(optarg
,
1626 actions
, ARRAY_SIZE(actions
),
1627 poll_action_name_to_id
);
1638 case 'm': /* mtab */
1639 tabtype
= TABTYPE_MTAB
;
1642 case 's': /* fstab */
1643 tabtype
= TABTYPE_FSTAB
;
1646 case 'k': /* kernel (mountinfo) */
1647 tabtype
= TABTYPE_KERNEL
;
1650 set_match(COL_FSTYPE
, optarg
);
1653 flags
&= ~FL_TREE
; /* disable the default */
1654 flags
|= FL_RAW
; /* enable raw */
1657 flags
&= ~FL_TREE
; /* disable the default */
1660 flags
|= FL_NOHEADINGS
;
1663 tabtype
= TABTYPE_KERNEL
;
1664 tabfiles
= append_pid_tabfile(tabfiles
, &ntabfiles
,
1665 strtou32_or_err(optarg
,
1666 _("invalid TID argument")));
1669 flags
|= FL_NOFSROOT
;
1672 flags
|= FL_SUBMOUNTS
;
1675 set_source_match(optarg
);
1676 flags
|= FL_NOSWAPMATCH
;
1679 flags
|= FL_STRICTTARGET
;
1682 set_match(COL_TARGET
, optarg
);
1683 flags
|= FL_NOSWAPMATCH
;
1689 timeout
= strtos32_or_err(optarg
, _("invalid timeout argument"));
1695 flags
|= FL_SHELLVAR
;
1697 case FINDMNT_OPT_VERBOSE
:
1698 flags
|= FL_VERBOSE
;
1700 case FINDMNT_OPT_TREE
:
1703 case FINDMNT_OPT_PSEUDO
:
1706 case FINDMNT_OPT_REAL
:
1709 case FINDMNT_OPT_VFS_ALL
:
1710 flags
|= FL_VFS_ALL
;
1712 case FINDMNT_OPT_SHADOWED
:
1713 flags
|= FL_SHADOWED
;
1722 print_version(EXIT_SUCCESS
);
1724 errtryhelp(EXIT_FAILURE
);
1729 list_colunms(); /* print end exit */
1731 if (!ncolumns
&& (flags
& FL_DF
)) {
1732 add_column(columns
, ncolumns
++, COL_SOURCE
);
1733 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1734 add_column(columns
, ncolumns
++, COL_SIZE
);
1735 add_column(columns
, ncolumns
++, COL_USED
);
1736 add_column(columns
, ncolumns
++, COL_AVAIL
);
1737 add_column(columns
, ncolumns
++, COL_USEPERC
);
1738 add_column(columns
, ncolumns
++, COL_TARGET
);
1741 /* default columns */
1743 if (flags
& FL_POLL
)
1744 add_column(columns
, ncolumns
++, COL_ACTION
);
1746 add_column(columns
, ncolumns
++, COL_TARGET
);
1747 add_column(columns
, ncolumns
++, COL_SOURCE
);
1748 add_column(columns
, ncolumns
++, COL_FSTYPE
);
1749 add_column(columns
, ncolumns
++, COL_OPTIONS
);
1752 if (outarg
&& string_add_to_idarray(outarg
, columns
, ARRAY_SIZE(columns
),
1753 &ncolumns
, column_name_to_id
) < 0)
1754 return EXIT_FAILURE
;
1757 tabtype
= verify
? TABTYPE_FSTAB
: TABTYPE_KERNEL
;
1759 if ((flags
& FL_POLL
) && ntabfiles
> 1)
1760 errx(EXIT_FAILURE
, _("--poll accepts only one file, but more specified by --tab-file"));
1762 if (optind
< argc
&& (get_match(COL_SOURCE
) || get_match(COL_TARGET
)))
1763 errx(EXIT_FAILURE
, _(
1764 "options --target and --source can't be used together "
1765 "with command line element that is not an option"));
1768 set_source_match(argv
[optind
++]); /* dev/tag/mountpoint/maj:min */
1770 set_match(COL_TARGET
, argv
[optind
++]); /* mountpoint */
1772 if ((flags
& FL_SUBMOUNTS
) && is_listall_mode())
1773 /* don't care about submounts if list all mounts */
1774 flags
&= ~FL_SUBMOUNTS
;
1776 if (!(flags
& FL_SUBMOUNTS
) && ((flags
& FL_FIRSTONLY
)
1777 || get_match(COL_TARGET
)
1778 || get_match(COL_SOURCE
)
1779 || get_match(COL_MAJMIN
)))
1782 if (!(flags
& FL_NOSWAPMATCH
) &&
1783 !get_match(COL_TARGET
) && get_match(COL_SOURCE
)) {
1785 * Check if we can swap source and target, it's
1786 * not possible if the source is LABEL=/UUID=
1788 const char *x
= get_match(COL_SOURCE
);
1790 if (!strncmp(x
, "LABEL=", 6) || !strncmp(x
, "UUID=", 5) ||
1791 !strncmp(x
, "PARTLABEL=", 10) || !strncmp(x
, "PARTUUID=", 9))
1792 flags
|= FL_NOSWAPMATCH
;
1796 * initialize libmount
1800 tb
= parse_tabfiles(tabfiles
, ntabfiles
, tabtype
);
1804 if (tabtype
== TABTYPE_MTAB
&& tab_is_kernel(tb
))
1805 tabtype
= TABTYPE_KERNEL
;
1807 istree
= tab_is_tree(tb
);
1808 if (istree
&& force_tree
)
1811 if ((flags
& FL_TREE
) && (ntabfiles
> 1 || !istree
))
1814 if (!(flags
& FL_NOCACHE
)) {
1815 cache
= mnt_new_cache();
1817 warn(_("failed to initialize libmount cache"));
1820 mnt_table_set_cache(tb
, cache
);
1822 if (tabtype
!= TABTYPE_KERNEL
)
1823 cache_set_targets(cache
);
1826 if (flags
& FL_UNIQ
)
1827 mnt_table_uniq_fs(tb
, MNT_UNIQ_KEEPTREE
, uniq_fs_target_cmp
);
1830 rc
= verify_table(tb
);
1835 * initialize libsmartcols
1837 scols_init_debug(0);
1838 table
= scols_new_table();
1840 warn(_("failed to allocate output table"));
1843 scols_table_enable_raw(table
, !!(flags
& FL_RAW
));
1844 scols_table_enable_export(table
, !!(flags
& FL_EXPORT
));
1845 scols_table_enable_shellvar(table
, !!(flags
& FL_SHELLVAR
));
1846 scols_table_enable_json(table
, !!(flags
& FL_JSON
));
1847 scols_table_enable_ascii(table
, !!(flags
& FL_ASCII
));
1848 scols_table_enable_noheadings(table
, !!(flags
& FL_NOHEADINGS
));
1850 if (flags
& FL_JSON
)
1851 scols_table_set_name(table
, "filesystems");
1853 for (i
= 0; i
< ncolumns
; i
++) {
1854 struct libscols_column
*cl
;
1855 int fl
= get_column_flags(i
);
1856 int id
= get_column_id(i
);
1858 if (!(flags
& FL_TREE
))
1859 fl
&= ~SCOLS_FL_TREE
;
1861 if (!(flags
& FL_POLL
) && is_tabdiff_column(id
)) {
1862 warnx(_("%s column is requested, but --poll "
1863 "is not enabled"), get_column_name(i
));
1866 cl
= scols_table_new_column(table
, get_column_name(i
),
1867 get_column_whint(i
), fl
);
1869 warn(_("failed to allocate output column"));
1872 /* multi-line cells (now used for SOURCES) */
1873 if (fl
& SCOLS_FL_WRAP
)
1874 scols_column_set_wrapfunc(cl
,
1876 scols_wrapzero_nextchunk
,
1878 if (flags
& FL_JSON
)
1879 scols_column_set_json_type(cl
, get_column_json_type(id
, fl
, NULL
));
1883 * Fill in data to the output table
1885 if (flags
& FL_POLL
) {
1886 /* poll mode (accept the first tabfile only) */
1887 rc
= poll_table(tb
, tabfiles
? *tabfiles
: _PATH_PROC_MOUNTINFO
, timeout
, table
, direction
);
1889 } else if ((flags
& FL_TREE
) && !(flags
& FL_SUBMOUNTS
)) {
1891 rc
= create_treenode(table
, tb
, NULL
, NULL
);
1893 /* whole list of sub-tree */
1894 rc
= add_matching_lines(tb
, table
, direction
);
1897 && tabtype
== TABTYPE_KERNEL
1898 && (flags
& FL_NOSWAPMATCH
)
1899 && !(flags
& FL_STRICTTARGET
)
1900 && get_match(COL_TARGET
)) {
1902 * Found nothing, maybe the --target is regular file,
1903 * try it again with extra functionality for target
1906 enable_extra_target_match(tb
);
1907 rc
= add_matching_lines(tb
, table
, direction
);
1912 * Print the output table for non-poll modes
1914 if (!rc
&& !(flags
& FL_POLL
))
1915 scols_print_table(table
);
1917 scols_unref_table(table
);
1919 mnt_unref_table(tb
);
1920 mnt_unref_cache(cache
);
1926 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;