]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/findmnt.c
misc: consolidate version printing and close_stdout()
[thirdparty/util-linux.git] / misc-utils / findmnt.c
CommitLineData
04fd7a9f
KZ
1/*
2 * findmnt(8)
3 *
8449f2cb 4 * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved.
04fd7a9f
KZ
5 * Written by Karel Zak <kzak@redhat.com>
6 *
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.
11 *
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.
16 *
7cebf0bb
SK
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.
04fd7a9f 20 */
04fd7a9f
KZ
21#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
04fd7a9f
KZ
24#include <unistd.h>
25#include <getopt.h>
26#include <string.h>
9d67679b 27#include <termios.h>
9d67679b 28#ifdef HAVE_SYS_IOCTL_H
e346233e 29# include <sys/ioctl.h>
9d67679b 30#endif
9d67679b 31#include <assert.h>
32e5466a 32#include <poll.h>
a85ef33b
DR
33#include <sys/statvfs.h>
34#include <sys/types.h>
e346233e
KZ
35#ifdef HAVE_LIBUDEV
36# include <libudev.h>
37#endif
2a1f429a 38#include <libmount.h>
218a3a94 39#include <libsmartcols.h>
fdedb45e 40
04fd7a9f
KZ
41#include "pathnames.h"
42#include "nls.h"
c05a80ca 43#include "closestream.h"
9d67679b 44#include "c.h"
ad38fb9f 45#include "strutils.h"
6f312c89 46#include "xalloc.h"
fb9a0042 47#include "optutils.h"
e346233e 48#include "mangle.h"
04fd7a9f 49
c768892f 50#include "findmnt.h"
9d67679b
KZ
51
52/* column IDs */
04fd7a9f
KZ
53enum {
54 COL_SOURCE,
55 COL_TARGET,
56 COL_FSTYPE,
57 COL_OPTIONS,
631280e0
KZ
58 COL_VFS_OPTIONS,
59 COL_FS_OPTIONS,
04fd7a9f
KZ
60 COL_LABEL,
61 COL_UUID,
090b5e84
KZ
62 COL_PARTLABEL,
63 COL_PARTUUID,
46236388 64 COL_MAJMIN,
32e5466a
KZ
65 COL_ACTION,
66 COL_OLD_TARGET,
67 COL_OLD_OPTIONS,
a85ef33b
DR
68 COL_SIZE,
69 COL_AVAIL,
70 COL_USED,
71 COL_USEPERC,
8b8cd87b 72 COL_FSROOT,
7d8f4e0c 73 COL_TID,
9238e0fa 74 COL_ID,
327ea85a 75 COL_OPT_FIELDS,
624f2b47 76 COL_PROPAGATION,
426f0cea 77 COL_FREQ,
edaf38cf 78 COL_PASSNO
04fd7a9f
KZ
79};
80
8c5eba3e
KZ
81enum {
82 TABTYPE_FSTAB = 1,
83 TABTYPE_MTAB,
84 TABTYPE_KERNEL
85};
86
9d67679b
KZ
87/* column names */
88struct colinfo {
89 const char *name; /* header */
90 double whint; /* width hint (N < 1 is in percent of termwidth) */
218a3a94 91 int flags; /* libsmartcols flags */
b152e359 92 const char *help; /* column description */
9d67679b 93 const char *match; /* pattern for match_func() */
cd492186 94 void *match_data; /* match specific data */
04fd7a9f
KZ
95};
96
af0dc7d3 97/* columns descriptions (don't use const, this is writable) */
edaf38cf 98static struct colinfo infos[] = {
218a3a94
OO
99 [COL_SOURCE] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES, N_("source device") },
100 [COL_TARGET] = { "TARGET", 0.30, SCOLS_FL_TREE| SCOLS_FL_NOEXTREMES, N_("mountpoint") },
101 [COL_FSTYPE] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC, N_("filesystem type") },
102 [COL_OPTIONS] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("all mount options") },
103 [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC, N_("VFS specific mount options") },
104 [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("FS specific mount options") },
b152e359
KZ
105 [COL_LABEL] = { "LABEL", 0.10, 0, N_("filesystem label") },
106 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
090b5e84
KZ
107 [COL_PARTLABEL] = { "PARTLABEL", 0.10, 0, N_("partition label") },
108 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
b152e359 109 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
218a3a94
OO
110 [COL_ACTION] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH, N_("action detected by --poll") },
111 [COL_OLD_OPTIONS] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("old mount options saved by --poll") },
b152e359 112 [COL_OLD_TARGET] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
218a3a94
OO
113 [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size") },
114 [COL_AVAIL] = { "AVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available") },
115 [COL_USED] = { "USED", 5, SCOLS_FL_RIGHT, N_("filesystem size used") },
116 [COL_USEPERC] = { "USE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") },
117 [COL_FSROOT] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES, N_("filesystem root") },
118 [COL_TID] = { "TID", 4, SCOLS_FL_RIGHT, N_("task ID") },
119 [COL_ID] = { "ID", 2, SCOLS_FL_RIGHT, N_("mount ID") },
120 [COL_OPT_FIELDS] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC, N_("optional mount fields") },
426f0cea 121 [COL_PROPAGATION] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
218a3a94
OO
122 [COL_FREQ] = { "FREQ", 1, SCOLS_FL_RIGHT, N_("dump(8) period in days [fstab only]") },
123 [COL_PASSNO] = { "PASSNO", 1, SCOLS_FL_RIGHT, N_("pass number on parallel fsck(8) [fstab only]") }
04fd7a9f
KZ
124};
125
edaf38cf
KZ
126/* columns[] array specifies all currently wanted output column. The columns
127 * are defined by infos[] array and you can specify (on command line) each
128 * column twice. That's enough, dynamically allocated array of the columns is
129 * unnecessary overkill and over-engineering in this case */
130static int columns[ARRAY_SIZE(infos) * 2];
40b17508 131static size_t ncolumns;
edaf38cf
KZ
132
133static inline size_t err_columns_index(size_t arysz, size_t idx)
134{
135 if (idx >= arysz)
136 errx(EXIT_FAILURE, _("too many columns specified, "
1d231190 137 "the limit is %zu columns"),
edaf38cf
KZ
138 arysz - 1);
139 return idx;
140}
141
142#define add_column(ary, n, id) \
143 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
144
582a5006 145/* poll actions (parsed --poll=<list> */
af0dc7d3
KZ
146#define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
147static int actions[FINDMNT_NACTIONS];
148static int nactions;
582a5006 149
c768892f
KZ
150/* global (accessed from findmnt-verify.c too) */
151int flags;
152int parse_nerrors;
153struct libmnt_cache *cache;
154
04fd7a9f 155
e346233e 156#ifdef HAVE_LIBUDEV
2ba641e5 157static struct udev *udev;
e346233e
KZ
158#endif
159
31f67453
KZ
160static int match_func(struct libmnt_fs *fs, void *data __attribute__ ((__unused__)));
161
162
00b4bcdf 163static int get_column_id(int num)
9d67679b 164{
40b17508
KZ
165 assert(num >= 0);
166 assert((size_t) num < ncolumns);
edaf38cf 167 assert((size_t) columns[num] < ARRAY_SIZE(infos));
fdedb45e 168 return columns[num];
9d67679b
KZ
169}
170
00b4bcdf 171static struct colinfo *get_column_info(int num)
9d67679b
KZ
172{
173 return &infos[ get_column_id(num) ];
174}
175
00b4bcdf 176static const char *column_id_to_name(int id)
9d67679b 177{
edaf38cf 178 assert((size_t) id < ARRAY_SIZE(infos));
9d67679b
KZ
179 return infos[id].name;
180}
181
00b4bcdf 182static const char *get_column_name(int num)
9d67679b 183{
fdedb45e 184 return get_column_info(num)->name;
9d67679b
KZ
185}
186
00b4bcdf 187static float get_column_whint(int num)
9d67679b 188{
fdedb45e 189 return get_column_info(num)->whint;
9d67679b
KZ
190}
191
32e5466a 192static int get_column_flags(int num)
9d67679b 193{
32e5466a 194 return get_column_info(num)->flags;
9d67679b
KZ
195}
196
00b4bcdf 197static const char *get_match(int id)
9d67679b 198{
edaf38cf 199 assert((size_t) id < ARRAY_SIZE(infos));
9d67679b
KZ
200 return infos[id].match;
201}
202
cd492186
KZ
203static void *get_match_data(int id)
204{
edaf38cf 205 assert((size_t) id < ARRAY_SIZE(infos));
cd492186
KZ
206 return infos[id].match_data;
207}
208
00b4bcdf 209static void set_match(int id, const char *match)
9d67679b 210{
edaf38cf 211 assert((size_t) id < ARRAY_SIZE(infos));
9d67679b
KZ
212 infos[id].match = match;
213}
214
cd492186
KZ
215static void set_match_data(int id, void *data)
216{
edaf38cf 217 assert((size_t) id < ARRAY_SIZE(infos));
cd492186
KZ
218 infos[id].match_data = data;
219}
220
4bfd4bff
KZ
221/*
222 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
223 * data format.
224 */
cd492186
KZ
225static void set_source_match(const char *data)
226{
227 int maj, min;
228
229 if (sscanf(data, "%d:%d", &maj, &min) == 2) {
230 dev_t *devno = xmalloc(sizeof(dev_t));
231
232 *devno = makedev(maj, min);
233 set_match(COL_MAJMIN, data);
234 set_match_data(COL_MAJMIN, (void *) devno);
4bfd4bff 235 flags |= FL_NOSWAPMATCH;
cd492186
KZ
236 } else
237 set_match(COL_SOURCE, data);
238}
239
0009f510
KZ
240/*
241 * Extra functionality for --target <path>. The function mnt_table_find_mountpoint()
242 * also checks parents (path elements in reverse order) to get mountpoint.
243 *
244 * @tb has to be from kernel (so no fstab or so)!
245 */
cd41b385 246static void enable_extra_target_match(struct libmnt_table *tb)
4bfd4bff 247{
cd41b385
KZ
248 char *cn = NULL;
249 const char *tgt = NULL, *mnt = NULL;
250 struct libmnt_fs *fs;
4bfd4bff
KZ
251
252 /*
253 * Check if match pattern is mountpoint, if not use the
254 * real mountpoint.
255 */
097e7f2f 256 if (flags & FL_NOCACHE)
4631e254 257 tgt = get_match(COL_TARGET);
097e7f2f 258 else {
4631e254 259 tgt = cn = mnt_resolve_path(get_match(COL_TARGET), cache);
097e7f2f
KZ
260 if (!cn)
261 return;
262 }
4bfd4bff 263
cd41b385
KZ
264 fs = mnt_table_find_mountpoint(tb, tgt, MNT_ITER_BACKWARD);
265 if (fs)
266 mnt = mnt_fs_get_target(fs);
4631e254 267 if (mnt && strcmp(mnt, tgt) != 0)
cd41b385 268 set_match(COL_TARGET, xstrdup(mnt)); /* replace the current setting */
4bfd4bff 269
4631e254
KZ
270 if (!cache)
271 free(cn);
4bfd4bff
KZ
272}
273
cd492186 274
32e5466a
KZ
275static int is_tabdiff_column(int id)
276{
edaf38cf 277 assert((size_t) id < ARRAY_SIZE(infos));
32e5466a
KZ
278
279 switch(id) {
280 case COL_ACTION:
281 case COL_OLD_TARGET:
282 case COL_OLD_OPTIONS:
283 return 1;
284 default:
285 break;
286 }
287 return 0;
288}
289
9d67679b
KZ
290/*
291 * "findmnt" without any filter
292 */
c768892f 293int is_listall_mode(void)
9d67679b 294{
cd4ed46f
KZ
295 if ((flags & FL_DF) && !(flags & FL_ALL))
296 return 0;
297
9d67679b
KZ
298 return (!get_match(COL_SOURCE) &&
299 !get_match(COL_TARGET) &&
300 !get_match(COL_FSTYPE) &&
cd492186
KZ
301 !get_match(COL_OPTIONS) &&
302 !get_match(COL_MAJMIN));
9d67679b
KZ
303}
304
582a5006
KZ
305/*
306 * Returns 1 if the @act is in the --poll=<list>
307 */
308static int has_poll_action(int act)
309{
310 int i;
311
312 if (!nactions)
313 return 1; /* all actions enabled */
314 for (i = 0; i < nactions; i++)
315 if (actions[i] == act)
316 return 1;
317 return 0;
318}
319
320static int poll_action_name_to_id(const char *name, size_t namesz)
321{
322 int id = -1;
323
324 if (strncasecmp(name, "move", namesz) == 0 && namesz == 4)
325 id = MNT_TABDIFF_MOVE;
326 else if (strncasecmp(name, "mount", namesz) == 0 && namesz == 5)
327 id = MNT_TABDIFF_MOUNT;
328 else if (strncasecmp(name, "umount", namesz) == 0 && namesz == 6)
329 id = MNT_TABDIFF_UMOUNT;
330 else if (strncasecmp(name, "remount", namesz) == 0 && namesz == 7)
331 id = MNT_TABDIFF_REMOUNT;
332 else
333 warnx(_("unknown action: %s"), name);
334
335 return id;
336}
337
9d67679b
KZ
338/*
339 * findmnt --first-only <devname|TAG=|mountpoint>
340 *
341 * ... it works like "mount <devname|TAG=|mountpoint>"
342 */
00b4bcdf 343static int is_mount_compatible_mode(void)
9d67679b
KZ
344{
345 if (!get_match(COL_SOURCE))
346 return 0; /* <devname|TAG=|mountpoint> is required */
347 if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS))
348 return 0; /* cannot be restricted by -t or -O */
349 if (!(flags & FL_FIRSTONLY))
350 return 0; /* we have to return the first entry only */
351
352 return 1; /* ok */
353}
354
32e5466a 355static void disable_columns_truncate(void)
9d67679b 356{
edaf38cf 357 size_t i;
9d67679b 358
edaf38cf 359 for (i = 0; i < ARRAY_SIZE(infos); i++)
218a3a94 360 infos[i].flags &= ~SCOLS_FL_TRUNC;
9d67679b
KZ
361}
362
04fd7a9f
KZ
363/*
364 * converts @name to column ID
365 */
9d67679b 366static int column_name_to_id(const char *name, size_t namesz)
04fd7a9f 367{
edaf38cf 368 size_t i;
04fd7a9f 369
edaf38cf 370 for (i = 0; i < ARRAY_SIZE(infos); i++) {
9d67679b 371 const char *cn = column_id_to_name(i);
04fd7a9f 372
9d67679b 373 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
04fd7a9f
KZ
374 return i;
375 }
fdedb45e 376 warnx(_("unknown column: %s"), name);
04fd7a9f
KZ
377 return -1;
378}
379
e346233e
KZ
380
381#ifdef HAVE_LIBUDEV
382static char *get_tag_from_udev(const char *devname, int col)
383{
384 struct udev_device *dev;
385 const char *data = NULL;
4ba4893b 386 char *res = NULL, *path;
e346233e
KZ
387
388 if (!udev)
389 udev = udev_new();
390 if (!udev)
391 return NULL;
392
4ba4893b
KZ
393 /* libudev don't like /dev/mapper/ symlinks */
394 path = realpath(devname, NULL);
395 if (path)
396 devname = path;
397
e346233e
KZ
398 if (strncmp(devname, "/dev/", 5) == 0)
399 devname += 5;
400
401 dev = udev_device_new_from_subsystem_sysname(udev, "block", devname);
4ba4893b
KZ
402 free(path);
403
e346233e
KZ
404 if (!dev)
405 return NULL;
406
407 switch(col) {
408 case COL_LABEL:
409 data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC");
410 break;
411 case COL_UUID:
412 data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC");
413 break;
414 case COL_PARTUUID:
415 data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID");
416 break;
417 case COL_PARTLABEL:
418 data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME");
419 break;
420 default:
421 break;
422 }
423
424 if (data) {
425 res = xstrdup(data);
426 unhexmangle_string(res);
427 }
428
429 udev_device_unref(dev);
430 return res;
431}
432#endif /* HAVE_LIBUDEV */
433
fdedb45e 434/* Returns LABEL or UUID */
b3386c83 435static char *get_tag(struct libmnt_fs *fs, const char *tagname, int col
81499ab2
KZ
436#ifndef HAVE_LIBUDEV
437 __attribute__((__unused__))
438#endif
439 )
9d67679b 440{
b3386c83
KZ
441 const char *t, *v;
442 char *res = NULL;
9d67679b
KZ
443
444 if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname))
b3386c83 445 res = xstrdup(v);
9d67679b 446 else {
e346233e
KZ
447 const char *dev = mnt_fs_get_source(fs);
448
097e7f2f 449 if (dev && !(flags & FL_NOCACHE))
e346233e
KZ
450 dev = mnt_resolve_spec(dev, cache);
451#ifdef HAVE_LIBUDEV
452 if (dev)
453 res = get_tag_from_udev(dev, col);
454#endif
b3386c83 455 if (!res) {
e346233e 456 res = mnt_cache_find_tag_value(cache, dev, tagname);
b3386c83
KZ
457 if (res && cache)
458 /* don't return pointer to cache */
459 res = xstrdup(res);
460 }
9d67679b
KZ
461 }
462
463 return res;
464}
465
b3386c83 466static char *get_vfs_attr(struct libmnt_fs *fs, int sizetype)
a85ef33b
DR
467{
468 struct statvfs buf;
6901f22c 469 uint64_t vfs_attr = 0;
a85ef33b
DR
470 char *sizestr;
471
472 if (statvfs(mnt_fs_get_target(fs), &buf) != 0)
473 return NULL;
474
475 switch(sizetype) {
476 case COL_SIZE:
477 vfs_attr = buf.f_frsize * buf.f_blocks;
478 break;
479 case COL_AVAIL:
ed6c8c1b 480 vfs_attr = buf.f_frsize * buf.f_bavail;
a85ef33b
DR
481 break;
482 case COL_USED:
483 vfs_attr = buf.f_frsize * (buf.f_blocks - buf.f_bfree);
484 break;
485 case COL_USEPERC:
486 if (buf.f_blocks == 0)
203c945a 487 return xstrdup("-");
a85ef33b 488
d7bcb205
KZ
489 xasprintf(&sizestr, "%.0f%%",
490 (double)(buf.f_blocks - buf.f_bfree) /
491 buf.f_blocks * 100);
a85ef33b
DR
492 return sizestr;
493 }
494
2b6759df
KZ
495 if (!vfs_attr)
496 sizestr = xstrdup("0");
497 else if (flags & FL_BYTES)
498 xasprintf(&sizestr, "%ju", vfs_attr);
499 else
500 sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
501
502 return sizestr;
a85ef33b
DR
503}
504
46236388 505/* reads FS data from libmount
46236388 506 */
b3386c83 507static char *get_data(struct libmnt_fs *fs, int num)
9d67679b 508{
b3386c83 509 char *str = NULL;
a85ef33b 510 int col_id = get_column_id(num);
9d67679b 511
a85ef33b 512 switch (col_id) {
04fd7a9f 513 case COL_SOURCE:
b2214e1f
KZ
514 {
515 const char *root = mnt_fs_get_root(fs);
b3386c83 516 const char *spec = mnt_fs_get_srcpath(fs);
4631e254 517 char *cn = NULL;
b2214e1f 518
b3386c83 519 if (spec && (flags & FL_CANONICALIZE))
4631e254 520 spec = cn = mnt_resolve_path(spec, cache);
b3386c83
KZ
521 if (!spec) {
522 spec = mnt_fs_get_source(fs);
9d67679b 523
b3386c83 524 if (spec && (flags & FL_EVALUATE))
4631e254 525 spec = cn = mnt_resolve_spec(spec, cache);
b2214e1f 526 }
b3386c83
KZ
527 if (root && spec && !(flags & FL_NOFSROOT) && strcmp(root, "/"))
528 xasprintf(&str, "%s[%s]", spec, root);
529 else if (spec)
530 str = xstrdup(spec);
4631e254
KZ
531 if (!cache)
532 free(cn);
04fd7a9f 533 break;
b2214e1f 534 }
04fd7a9f 535 case COL_TARGET:
b3386c83 536 str = xstrdup(mnt_fs_get_target(fs));
04fd7a9f
KZ
537 break;
538 case COL_FSTYPE:
b3386c83 539 str = xstrdup(mnt_fs_get_fstype(fs));
04fd7a9f
KZ
540 break;
541 case COL_OPTIONS:
b3386c83 542 str = xstrdup(mnt_fs_get_options(fs));
04fd7a9f 543 break;
631280e0 544 case COL_VFS_OPTIONS:
b3386c83 545 str = xstrdup(mnt_fs_get_vfs_options(fs));
631280e0
KZ
546 break;
547 case COL_FS_OPTIONS:
b3386c83 548 str = xstrdup(mnt_fs_get_fs_options(fs));
631280e0 549 break;
327ea85a 550 case COL_OPT_FIELDS:
b3386c83 551 str = xstrdup(mnt_fs_get_optional_fields(fs));
327ea85a 552 break;
9d67679b 553 case COL_UUID:
e346233e 554 str = get_tag(fs, "UUID", col_id);
9d67679b 555 break;
090b5e84 556 case COL_PARTUUID:
e346233e 557 str = get_tag(fs, "PARTUUID", col_id);
090b5e84 558 break;
9d67679b 559 case COL_LABEL:
e346233e 560 str = get_tag(fs, "LABEL", col_id);
9d67679b 561 break;
090b5e84 562 case COL_PARTLABEL:
e346233e 563 str = get_tag(fs, "PARTLABEL", col_id);
090b5e84
KZ
564 break;
565
46236388
KZ
566 case COL_MAJMIN:
567 {
568 dev_t devno = mnt_fs_get_devno(fs);
60b17c6f
KZ
569 if (!devno)
570 break;
571
8449f2cb 572 if ((flags & FL_RAW) || (flags & FL_EXPORT) || (flags & FL_JSON))
b3386c83 573 xasprintf(&str, "%u:%u", major(devno), minor(devno));
60b17c6f 574 else
b3386c83 575 xasprintf(&str, "%3u:%-3u", major(devno), minor(devno));
a85ef33b 576 break;
46236388 577 }
a85ef33b
DR
578 case COL_SIZE:
579 case COL_AVAIL:
580 case COL_USED:
581 case COL_USEPERC:
582 str = get_vfs_attr(fs, col_id);
583 break;
8b8cd87b 584 case COL_FSROOT:
b3386c83 585 str = xstrdup(mnt_fs_get_root(fs));
8b8cd87b 586 break;
7d8f4e0c 587 case COL_TID:
b3386c83
KZ
588 if (mnt_fs_get_tid(fs))
589 xasprintf(&str, "%d", mnt_fs_get_tid(fs));
7d8f4e0c 590 break;
9238e0fa 591 case COL_ID:
b3386c83
KZ
592 if (mnt_fs_get_id(fs))
593 xasprintf(&str, "%d", mnt_fs_get_id(fs));
9238e0fa 594 break;
624f2b47
KZ
595 case COL_PROPAGATION:
596 if (mnt_fs_is_kernel(fs)) {
597 unsigned long fl = 0;
598 char *n = NULL;
599
600 if (mnt_fs_get_propagation(fs, &fl) != 0)
601 break;
602
603 n = xstrdup((fl & MS_SHARED) ? "shared" : "private");
604
605 if (fl & MS_SLAVE) {
b3386c83 606 xasprintf(&str, "%s,slave", n);
624f2b47 607 free(n);
b3386c83 608 n = str;
624f2b47
KZ
609 }
610 if (fl & MS_UNBINDABLE) {
b3386c83 611 xasprintf(&str, "%s,unbindable", n);
624f2b47 612 free(n);
b3386c83 613 n = str;
624f2b47
KZ
614 }
615 str = n;
616 }
617 break;
426f0cea 618 case COL_FREQ:
b3386c83
KZ
619 if (!mnt_fs_is_kernel(fs))
620 xasprintf(&str, "%d", mnt_fs_get_freq(fs));
426f0cea
KZ
621 break;
622 case COL_PASSNO:
b3386c83
KZ
623 if (!mnt_fs_is_kernel(fs))
624 xasprintf(&str, "%d", mnt_fs_get_passno(fs));
426f0cea 625 break;
04fd7a9f 626 default:
9d67679b 627 break;
04fd7a9f 628 }
fdedb45e 629 return str;
9d67679b
KZ
630}
631
b3386c83 632static char *get_tabdiff_data(struct libmnt_fs *old_fs,
32e5466a
KZ
633 struct libmnt_fs *new_fs,
634 int change,
635 int num)
636{
b3386c83 637 char *str = NULL;
32e5466a
KZ
638
639 switch (get_column_id(num)) {
640 case COL_ACTION:
641 switch (change) {
642 case MNT_TABDIFF_MOUNT:
643 str = _("mount");
644 break;
645 case MNT_TABDIFF_UMOUNT:
646 str = _("umount");
647 break;
648 case MNT_TABDIFF_REMOUNT:
649 str = _("remount");
650 break;
651 case MNT_TABDIFF_MOVE:
652 str = _("move");
653 break;
654 default:
655 str = _("unknown");
656 break;
657 }
b3386c83 658 str = xstrdup(str);
32e5466a
KZ
659 break;
660 case COL_OLD_OPTIONS:
77a1c5f7
KZ
661 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
662 change == MNT_TABDIFF_UMOUNT))
b3386c83 663 str = xstrdup(mnt_fs_get_options(old_fs));
32e5466a
KZ
664 break;
665 case COL_OLD_TARGET:
77a1c5f7
KZ
666 if (old_fs && (change == MNT_TABDIFF_MOVE ||
667 change == MNT_TABDIFF_UMOUNT))
b3386c83 668 str = xstrdup(mnt_fs_get_target(old_fs));
32e5466a
KZ
669 break;
670 default:
671 if (new_fs)
672 str = get_data(new_fs, num);
673 else
674 str = get_data(old_fs, num);
675 break;
676 }
677 return str;
678}
679
fdedb45e 680/* adds one line to the output @tab */
218a3a94
OO
681static struct libscols_line *add_line(struct libscols_table *table, struct libmnt_fs *fs,
682 struct libscols_line *parent)
9d67679b 683{
40b17508 684 size_t i;
218a3a94 685 struct libscols_line *line = scols_table_new_line(table, parent);
9d67679b 686
780ce22c
KZ
687 if (!line)
688 err(EXIT_FAILURE, _("failed to allocate output line"));
689
690 for (i = 0; i < ncolumns; i++) {
691 if (scols_line_refer_data(line, i, get_data(fs, i)))
692 err(EXIT_FAILURE, _("failed to add output data"));
9d67679b
KZ
693 }
694
218a3a94 695 scols_line_set_userdata(line, fs);
fdedb45e 696 return line;
9d67679b
KZ
697}
698
218a3a94 699static struct libscols_line *add_tabdiff_line(struct libscols_table *table, struct libmnt_fs *new_fs,
32e5466a
KZ
700 struct libmnt_fs *old_fs, int change)
701{
40b17508 702 size_t i;
218a3a94 703 struct libscols_line *line = scols_table_new_line(table, NULL);
32e5466a 704
780ce22c
KZ
705 if (!line)
706 err(EXIT_FAILURE, _("failed to allocate output line"));
707
708 for (i = 0; i < ncolumns; i++) {
709 if (scols_line_refer_data(line, i,
710 get_tabdiff_data(old_fs, new_fs, change, i)))
711 err(EXIT_FAILURE, _("failed to add output data"));
32e5466a 712 }
32e5466a
KZ
713
714 return line;
715}
716
218a3a94 717static int has_line(struct libscols_table *table, struct libmnt_fs *fs)
049caefd 718{
218a3a94
OO
719 struct libscols_line *ln;
720 struct libscols_iter *itr;
aea3a3ba 721 int rc = 0;
218a3a94
OO
722
723 itr = scols_new_iter(SCOLS_ITER_FORWARD);
724 if (!itr)
725 return 0;
049caefd 726
aea3a3ba
KZ
727 while (scols_table_next_line(table, itr, &ln) == 0) {
728 if ((struct libmnt_fs *) scols_line_get_userdata(ln) == fs) {
729 rc = 1;
730 break;
731 }
049caefd 732 }
aea3a3ba
KZ
733
734 scols_free_iter(itr);
735 return rc;
049caefd
KZ
736}
737
218a3a94
OO
738/* reads filesystems from @tb (libmount) and fillin @table (output table) */
739static int create_treenode(struct libscols_table *table, struct libmnt_table *tb,
740 struct libmnt_fs *fs, struct libscols_line *parent_line)
04fd7a9f 741{
68164f6c
KZ
742 struct libmnt_fs *chld = NULL;
743 struct libmnt_iter *itr = NULL;
218a3a94 744 struct libscols_line *line;
fdedb45e 745 int rc = -1;
9d67679b 746
fdedb45e
KZ
747 if (!fs) {
748 /* first call, get root FS */
68164f6c 749 if (mnt_table_get_root_fs(tb, &fs))
fdedb45e
KZ
750 goto leave;
751 parent_line = NULL;
049caefd 752
218a3a94 753 } else if ((flags & FL_SUBMOUNTS) && has_line(table, fs))
049caefd 754 return 0;
9d67679b 755
fdedb45e
KZ
756 itr = mnt_new_iter(MNT_ITER_FORWARD);
757 if (!itr)
758 goto leave;
9d67679b 759
31f67453 760 if ((flags & FL_SUBMOUNTS) || match_func(fs, NULL)) {
218a3a94 761 line = add_line(table, fs, parent_line);
31f67453
KZ
762 if (!line)
763 goto leave;
764 } else
765 line = parent_line;
9d67679b 766
fdedb45e
KZ
767 /*
768 * add all children to the output table
769 */
68164f6c 770 while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
218a3a94 771 if (create_treenode(table, tb, chld, line))
fdedb45e 772 goto leave;
9d67679b 773 }
fdedb45e
KZ
774 rc = 0;
775leave:
776 mnt_free_iter(itr);
777 return rc;
04fd7a9f
KZ
778}
779
20055151 780/* error callback */
6e5c0fc2
KZ
781static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
782 const char *filename, int line)
20055151 783{
b779c1ae 784 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
c768892f 785 ++parse_nerrors;
1cd9d0d7 786 return 1;
20055151
KZ
787}
788
8c5eba3e
KZ
789static char **append_tabfile(char **files, int *nfiles, char *filename)
790{
99d618c0 791 files = xrealloc(files, sizeof(char *) * (*nfiles + 1));
8c5eba3e
KZ
792 files[(*nfiles)++] = filename;
793 return files;
794}
795
c7fcc830
KZ
796static char **append_pid_tabfile(char **files, int *nfiles, pid_t pid)
797{
798 char *path = NULL;
799
800 xasprintf(&path, "/proc/%d/mountinfo", (int) pid);
801 return append_tabfile(files, nfiles, path);
802}
803
fdedb45e 804/* calls libmount fstab/mtab/mountinfo parser */
8c5eba3e
KZ
805static struct libmnt_table *parse_tabfiles(char **files,
806 int nfiles,
807 int tabtype)
04fd7a9f 808{
8c5eba3e 809 struct libmnt_table *tb;
762ef0e1 810 int rc = 0;
20055151 811
8c5eba3e 812 tb = mnt_new_table();
fdedb45e 813 if (!tb) {
32e5466a 814 warn(_("failed to initialize libmount table"));
04fd7a9f 815 return NULL;
fdedb45e 816 }
68164f6c 817 mnt_table_set_parser_errcb(tb, parser_errcb);
20055151 818
8c5eba3e
KZ
819 do {
820 /* NULL means that libmount will use default paths */
821 const char *path = nfiles ? *files++ : NULL;
822
823 switch (tabtype) {
824 case TABTYPE_FSTAB:
825 rc = mnt_table_parse_fstab(tb, path);
826 break;
827 case TABTYPE_MTAB:
828 rc = mnt_table_parse_mtab(tb, path);
829 break;
830 case TABTYPE_KERNEL:
831 if (!path)
832 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
833 _PATH_PROC_MOUNTINFO :
834 _PATH_PROC_MOUNTS;
835
836 rc = mnt_table_parse_file(tb, path);
837 break;
838 }
839 if (rc) {
50fccba1 840 mnt_unref_table(tb);
8c5eba3e
KZ
841 warn(_("can't read %s"), path);
842 return NULL;
843 }
844 } while (--nfiles > 0);
20055151 845
04fd7a9f 846 return tb;
04fd7a9f
KZ
847}
848
fb329bbc
ER
849/*
850 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
851 * necessary if @tb in main() was read from a non-kernel source.
852 */
7ee26cbf 853static void cache_set_targets(struct libmnt_cache *tmp)
fb329bbc 854{
23deb5ac
KZ
855 struct libmnt_table *tb;
856 const char *path;
fb329bbc
ER
857
858 tb = mnt_new_table();
859 if (!tb)
23deb5ac 860 return;
fb329bbc
ER
861
862 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
863 _PATH_PROC_MOUNTINFO :
864 _PATH_PROC_MOUNTS;
865
23deb5ac 866 if (mnt_table_parse_file(tb, path) == 0)
7ee26cbf 867 mnt_cache_set_targets(tmp, tb);
fb329bbc 868
fb329bbc
ER
869 mnt_unref_table(tb);
870}
871
2f1ac44b
KZ
872/* checks if @tb contains parent->child relations */
873static int tab_is_tree(struct libmnt_table *tb)
874{
875 struct libmnt_fs *fs = NULL;
1b504263 876 struct libmnt_iter *itr;
2f1ac44b
KZ
877 int rc = 0;
878
879 itr = mnt_new_iter(MNT_ITER_BACKWARD);
880 if (!itr)
881 return 0;
882
6c373810
KZ
883 rc = (mnt_table_next_fs(tb, itr, &fs) == 0 &&
884 mnt_fs_is_kernel(fs) &&
885 mnt_fs_get_root(fs));
2f1ac44b
KZ
886
887 mnt_free_iter(itr);
888 return rc;
889}
890
9c277ede
KZ
891/* checks if all fs in @tb are from kernel */
892static int tab_is_kernel(struct libmnt_table *tb)
893{
894 struct libmnt_fs *fs = NULL;
1b504263 895 struct libmnt_iter *itr;
9c277ede
KZ
896
897 itr = mnt_new_iter(MNT_ITER_BACKWARD);
898 if (!itr)
899 return 0;
900
901 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
902 if (!mnt_fs_is_kernel(fs)) {
903 mnt_free_iter(itr);
904 return 0;
905 }
906 }
907
908 mnt_free_iter(itr);
909 return 1;
910}
b215d8e9 911
68164f6c 912/* filter function for libmount (mnt_table_find_next_fs()) */
6e5c0fc2
KZ
913static int match_func(struct libmnt_fs *fs,
914 void *data __attribute__ ((__unused__)))
04fd7a9f 915{
04fd7a9f 916 int rc = flags & FL_INVERT ? 1 : 0;
9d67679b 917 const char *m;
cd492186 918 void *md;
04fd7a9f 919
9d67679b
KZ
920 m = get_match(COL_FSTYPE);
921 if (m && !mnt_fs_match_fstype(fs, m))
04fd7a9f 922 return rc;
9d67679b
KZ
923
924 m = get_match(COL_OPTIONS);
925 if (m && !mnt_fs_match_options(fs, m))
04fd7a9f
KZ
926 return rc;
927
cd492186
KZ
928 md = get_match_data(COL_MAJMIN);
929 if (md && mnt_fs_get_devno(fs) != *((dev_t *) md))
930 return rc;
931
4096946f
KZ
932 m = get_match(COL_TARGET);
933 if (m && !mnt_fs_match_target(fs, m, cache))
934 return rc;
935
936 m = get_match(COL_SOURCE);
937 if (m && !mnt_fs_match_source(fs, m, cache))
938 return rc;
939
cd4ed46f
KZ
940 if ((flags & FL_DF) && !(flags & FL_ALL)) {
941 const char *type = mnt_fs_get_fstype(fs);
942
943 if (type && strstr(type, "tmpfs")) /* tmpfs is wanted */
944 return !rc;
945
946 if (mnt_fs_is_pseudofs(fs))
947 return rc;
948 }
949
4c856a47 950 if ((flags & FL_REAL) && mnt_fs_is_pseudofs(fs))
951 return rc;
952
953 if ((flags & FL_PSEUDO) && !mnt_fs_is_pseudofs(fs))
954 return rc;
955
04fd7a9f
KZ
956 return !rc;
957}
958
fdedb45e 959/* iterate over filesystems in @tb */
c768892f 960struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
68164f6c 961 struct libmnt_iter *itr)
04fd7a9f 962{
68164f6c 963 struct libmnt_fs *fs = NULL;
9d67679b
KZ
964
965 if (is_listall_mode()) {
966 /*
967 * Print whole file
968 */
e3963f60
KZ
969 if (mnt_table_next_fs(tb, itr, &fs) != 0)
970 return NULL;
9d67679b
KZ
971
972 } else if (is_mount_compatible_mode()) {
973 /*
9e930041 974 * Look up for FS in the same way how mount(8) searches in fstab
9d67679b
KZ
975 *
976 * findmnt -f <spec>
977 */
68164f6c 978 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
9d67679b 979 mnt_iter_get_direction(itr));
9a30c6ef
KZ
980
981 if (!fs && !(flags & FL_NOSWAPMATCH))
68164f6c 982 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
9d67679b
KZ
983 mnt_iter_get_direction(itr));
984 } else {
985 /*
986 * Look up for all matching entries
987 *
988 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
989 * findmnt [-l] <spec> [-O <options>] [-t <types>]
990 */
991again:
68164f6c 992 mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs);
9d67679b
KZ
993
994 if (!fs &&
995 !(flags & FL_NOSWAPMATCH) &&
996 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
997
998 /* swap 'spec' and target. */
999 set_match(COL_TARGET, get_match(COL_SOURCE));
1000 set_match(COL_SOURCE, NULL);
1001 mnt_reset_iter(itr, -1);
1002
1003 goto again;
1004 }
1005 }
9d67679b 1006
fdedb45e 1007 return fs;
04fd7a9f
KZ
1008}
1009
31f67453
KZ
1010/*
1011 * Filter out unwanted lines for --list output or top level lines for
1012 * --submounts tree output.
1013 */
68164f6c 1014static int add_matching_lines(struct libmnt_table *tb,
218a3a94 1015 struct libscols_table *table, int direction)
049caefd 1016{
1b504263 1017 struct libmnt_iter *itr;
68164f6c 1018 struct libmnt_fs *fs;
049caefd
KZ
1019 int nlines = 0, rc = -1;
1020
1021 itr = mnt_new_iter(direction);
1022 if (!itr) {
1023 warn(_("failed to initialize libmount iterator"));
1024 goto done;
1025 }
1026
1027 while((fs = get_next_fs(tb, itr))) {
85c9ca5a 1028 if ((flags & FL_TREE) || (flags & FL_SUBMOUNTS))
218a3a94 1029 rc = create_treenode(table, tb, fs, NULL);
049caefd 1030 else
218a3a94 1031 rc = !add_line(table, fs, NULL);
049caefd
KZ
1032 if (rc)
1033 goto done;
1034 nlines++;
1035 if (flags & FL_FIRSTONLY)
1036 break;
1037 flags |= FL_NOSWAPMATCH;
1038 }
1039
1040 if (nlines)
1041 rc = 0;
1042done:
1043 mnt_free_iter(itr);
1044 return rc;
1045}
1046
582a5006
KZ
1047static int poll_match(struct libmnt_fs *fs)
1048{
1049 int rc = match_func(fs, NULL);
1050
1051 if (rc == 0 && !(flags & FL_NOSWAPMATCH) &&
1052 get_match(COL_SOURCE) && !get_match(COL_TARGET)) {
1053 /*
1054 * findmnt --poll /foo
cd492186 1055 * The '/foo' maybe source as well as target.
582a5006
KZ
1056 */
1057 const char *str = get_match(COL_SOURCE);
1058
1059 set_match(COL_TARGET, str); /* swap */
1060 set_match(COL_SOURCE, NULL);
1061
1062 rc = match_func(fs, NULL);
1063
1064 set_match(COL_TARGET, NULL); /* restore */
1065 set_match(COL_SOURCE, str);
1066
1067 }
1068 return rc;
1069}
1070
32e5466a 1071static int poll_table(struct libmnt_table *tb, const char *tabfile,
218a3a94 1072 int timeout, struct libscols_table *table, int direction)
32e5466a 1073{
d1cabd5c 1074 FILE *f = NULL;
32e5466a
KZ
1075 int rc = -1;
1076 struct libmnt_iter *itr = NULL;
1b504263 1077 struct libmnt_table *tb_new;
32e5466a
KZ
1078 struct libmnt_tabdiff *diff = NULL;
1079 struct pollfd fds[1];
1080
1081 tb_new = mnt_new_table();
1082 if (!tb_new) {
1083 warn(_("failed to initialize libmount table"));
1084 goto done;
1085 }
1086
1087 itr = mnt_new_iter(direction);
1088 if (!itr) {
1089 warn(_("failed to initialize libmount iterator"));
1090 goto done;
1091 }
1092
1093 diff = mnt_new_tabdiff();
1094 if (!diff) {
1095 warn(_("failed to initialize libmount tabdiff"));
1096 goto done;
1097 }
1098
1099 /* cache is unnecessary to detect changes */
1100 mnt_table_set_cache(tb, NULL);
1101 mnt_table_set_cache(tb_new, NULL);
1102
1103 f = fopen(tabfile, "r");
1104 if (!f) {
289dcc90 1105 warn(_("cannot open %s"), tabfile);
32e5466a
KZ
1106 goto done;
1107 }
1108
1109 mnt_table_set_parser_errcb(tb_new, parser_errcb);
1110
1111 fds[0].fd = fileno(f);
1112 fds[0].events = POLLPRI;
1113
1114 while (1) {
1115 struct libmnt_table *tmp;
1116 struct libmnt_fs *old, *new;
582a5006 1117 int change, count;
32e5466a 1118
582a5006
KZ
1119 count = poll(fds, 1, timeout);
1120 if (count == 0)
ad38fb9f 1121 break; /* timeout */
582a5006 1122 if (count < 0) {
32e5466a
KZ
1123 warn(_("poll() failed"));
1124 goto done;
1125 }
1126
1127 rewind(f);
1128 rc = mnt_table_parse_stream(tb_new, f, tabfile);
1129 if (!rc)
1130 rc = mnt_diff_tables(diff, tb, tb_new);
1131 if (rc < 0)
1132 goto done;
1133
582a5006 1134 count = 0;
32e5466a
KZ
1135 mnt_reset_iter(itr, direction);
1136 while(mnt_tabdiff_next_change(
1137 diff, itr, &old, &new, &change) == 0) {
1138
582a5006
KZ
1139 if (!has_poll_action(change))
1140 continue;
1141 if (!poll_match(new ? new : old))
1142 continue;
1143 count++;
218a3a94 1144 rc = !add_tabdiff_line(table, new, old, change);
32e5466a
KZ
1145 if (rc)
1146 goto done;
1147 if (flags & FL_FIRSTONLY)
1148 break;
1149 }
1150
582a5006 1151 if (count) {
c1d185c7 1152 rc = scols_table_print_range(table, NULL, NULL);
7ccc1643
KZ
1153 if (rc == 0)
1154 fputc('\n', scols_table_get_stream(table));
18efba18 1155 fflush(stdout);
582a5006
KZ
1156 if (rc)
1157 goto done;
1158 }
32e5466a
KZ
1159
1160 /* swap tables */
1161 tmp = tb;
1162 tb = tb_new;
1163 tb_new = tmp;
1164
9e930041 1165 /* remove already printed lines to reduce memory usage */
218a3a94 1166 scols_table_remove_lines(table);
32e5466a 1167 mnt_reset_table(tb_new);
582a5006
KZ
1168
1169 if (count && (flags & FL_FIRSTONLY))
1170 break;
32e5466a
KZ
1171 }
1172
1173 rc = 0;
1174done:
50fccba1 1175 mnt_unref_table(tb_new);
32e5466a
KZ
1176 mnt_free_tabdiff(diff);
1177 mnt_free_iter(itr);
d1cabd5c
KZ
1178 if (f)
1179 fclose(f);
32e5466a
KZ
1180 return rc;
1181}
1182
bebdda30
KZ
1183static int uniq_fs_target_cmp(
1184 struct libmnt_table *tb __attribute__((__unused__)),
1185 struct libmnt_fs *a,
1186 struct libmnt_fs *b)
1187{
1188 return !mnt_fs_match_target(a, mnt_fs_get_target(b), cache);
1189}
1190
86be6a32 1191static void __attribute__((__noreturn__)) usage(void)
04fd7a9f 1192{
86be6a32 1193 FILE *out = stdout;
edaf38cf 1194 size_t i;
9ead0006 1195
5cc12c40 1196 fputs(USAGE_HEADER, out);
9d67679b 1197 fprintf(out, _(
9d67679b
KZ
1198 " %1$s [options]\n"
1199 " %1$s [options] <device> | <mountpoint>\n"
1200 " %1$s [options] <device> <mountpoint>\n"
0009f510 1201 " %1$s [options] [--source <device>] [--target <path> | --mountpoint <dir>]\n"),
9d67679b 1202 program_invocation_short_name);
04fd7a9f 1203
451dbcfa
BS
1204 fputs(USAGE_SEPARATOR, out);
1205 fputs(_("Find a (mounted) filesystem.\n"), out);
1206
b95c6e88
KZ
1207 fputs(USAGE_OPTIONS, out);
1208 fputs(_(" -s, --fstab search in static table of filesystems\n"), out);
de22cb2b
KZ
1209 fputs(_(" -m, --mtab search in table of mounted filesystems\n"
1210 " (includes user space mount options)\n"), out);
b95c6e88 1211 fputs(_(" -k, --kernel search in kernel table of mounted\n"
ab5b4f83 1212 " filesystems (default)\n"), out);
b95c6e88
KZ
1213 fputc('\n', out);
1214 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out);
1215 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out);
1216 fputc('\n', out);
1217
1218 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out);
1219 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out);
2b6759df 1220 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out);
ab5b4f83 1221 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out);
b95c6e88
KZ
1222 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out);
1223 fputs(_(" -D, --df imitate the output of df(1)\n"), out);
1224 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out);
1225 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1226 " to device names\n"), out);
1227 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out);
1228 fputs(_(" -f, --first-only print the first found filesystem only\n"), out);
1229 fputs(_(" -i, --invert invert the sense of matching\n"), out);
8449f2cb 1230 fputs(_(" -J, --json use JSON output format\n"), out);
b95c6e88
KZ
1231 fputs(_(" -l, --list use list format output\n"), out);
1232 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out);
1233 fputs(_(" -n, --noheadings don't print column headings\n"), out);
b95c6e88
KZ
1234 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out);
1235 fputs(_(" -o, --output <list> the output columns to be shown\n"), out);
00147883 1236 fputs(_(" --output-all output all available columns\n"), out);
b95c6e88 1237 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
4c856a47 1238 fputs(_(" --pseudo print only pseudo-filesystems\n"), out);
b95c6e88 1239 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out);
ab5b4f83 1240 fputs(_(" -r, --raw use raw output format\n"), out);
4c856a47 1241 fputs(_(" --real print only real filesystems\n"), out);
b95c6e88
KZ
1242 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1243 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out);
0009f510 1244 fputs(_(" -T, --target <path> the path to the filesystem to use\n"), out);
3ebe5477 1245 fputs(_(" --tree enable tree format output is possible\n"), out);
0009f510 1246 fputs(_(" -M, --mountpoint <dir> the mountpoint directory\n"), out);
ab5b4f83
BS
1247 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out);
1248 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out);
1249 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out);
f920bfd5 1250 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out);
c768892f
KZ
1251
1252 fputc('\n', out);
1253 fputs(_(" -x, --verify verify mount table content (default is fstab)\n"), out);
1254 fputs(_(" --verbose print more details\n"), out);
04fd7a9f 1255
5cc12c40 1256 fputs(USAGE_SEPARATOR, out);
f45f3ec3 1257 printf(USAGE_HELP_OPTIONS(24));
9ead0006 1258
6e2d5a44 1259 fputs(USAGE_COLUMNS, out);
edaf38cf 1260 for (i = 0; i < ARRAY_SIZE(infos); i++)
b152e359 1261 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
9ead0006 1262
f45f3ec3 1263 printf(USAGE_MAN_TAIL("findmnt(8)"));
04fd7a9f 1264
86be6a32 1265 exit(EXIT_SUCCESS);
04fd7a9f
KZ
1266}
1267
1268int main(int argc, char *argv[])
1269{
68164f6c 1270 struct libmnt_table *tb = NULL;
8c5eba3e 1271 char **tabfiles = NULL;
9d67679b 1272 int direction = MNT_ITER_FORWARD;
c768892f 1273 int verify = 0;
40b17508 1274 int c, rc = -1, timeout = -1;
8c5eba3e 1275 int ntabfiles = 0, tabtype = 0;
c57dca68 1276 char *outarg = NULL;
40b17508 1277 size_t i;
3ebe5477 1278 int force_tree = 0, istree = 0;
fdedb45e 1279
218a3a94 1280 struct libscols_table *table = NULL;
fdedb45e 1281
c768892f 1282 enum {
f920bfd5 1283 FINDMNT_OPT_VERBOSE = CHAR_MAX + 1,
00147883 1284 FINDMNT_OPT_TREE,
4c856a47 1285 FINDMNT_OPT_OUTPUT_ALL,
1286 FINDMNT_OPT_PSEUDO,
1287 FINDMNT_OPT_REAL
c768892f
KZ
1288 };
1289
6c7d5ae9 1290 static const struct option longopts[] = {
87918040
SK
1291 { "all", no_argument, NULL, 'A' },
1292 { "ascii", no_argument, NULL, 'a' },
1293 { "bytes", no_argument, NULL, 'b' },
1294 { "canonicalize", no_argument, NULL, 'c' },
1295 { "direction", required_argument, NULL, 'd' },
1296 { "df", no_argument, NULL, 'D' },
1297 { "evaluate", no_argument, NULL, 'e' },
1298 { "first-only", no_argument, NULL, 'f' },
1299 { "fstab", no_argument, NULL, 's' },
1300 { "help", no_argument, NULL, 'h' },
1301 { "invert", no_argument, NULL, 'i' },
1302 { "json", no_argument, NULL, 'J' },
1303 { "kernel", no_argument, NULL, 'k' },
1304 { "list", no_argument, NULL, 'l' },
1305 { "mountpoint", required_argument, NULL, 'M' },
1306 { "mtab", no_argument, NULL, 'm' },
1307 { "noheadings", no_argument, NULL, 'n' },
1308 { "notruncate", no_argument, NULL, 'u' },
1309 { "options", required_argument, NULL, 'O' },
1310 { "output", required_argument, NULL, 'o' },
00147883 1311 { "output-all", no_argument, NULL, FINDMNT_OPT_OUTPUT_ALL },
87918040
SK
1312 { "poll", optional_argument, NULL, 'p' },
1313 { "pairs", no_argument, NULL, 'P' },
1314 { "raw", no_argument, NULL, 'r' },
1315 { "types", required_argument, NULL, 't' },
1316 { "nocanonicalize", no_argument, NULL, 'C' },
1317 { "nofsroot", no_argument, NULL, 'v' },
1318 { "submounts", no_argument, NULL, 'R' },
1319 { "source", required_argument, NULL, 'S' },
1320 { "tab-file", required_argument, NULL, 'F' },
1321 { "task", required_argument, NULL, 'N' },
1322 { "target", required_argument, NULL, 'T' },
1323 { "timeout", required_argument, NULL, 'w' },
1324 { "uniq", no_argument, NULL, 'U' },
1325 { "verify", no_argument, NULL, 'x' },
1326 { "version", no_argument, NULL, 'V' },
1327 { "verbose", no_argument, NULL, FINDMNT_OPT_VERBOSE },
1328 { "tree", no_argument, NULL, FINDMNT_OPT_TREE },
4c856a47 1329 { "real", no_argument, NULL, FINDMNT_OPT_REAL },
1330 { "pseudo", no_argument, NULL, FINDMNT_OPT_PSEUDO },
87918040 1331 { NULL, 0, NULL, 0 }
04fd7a9f
KZ
1332 };
1333
a7349ee3 1334 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
f920bfd5 1335 { 'C', 'c'}, /* [no]canonicalize */
097e7f2f 1336 { 'C', 'e' }, /* nocanonicalize, evaluate */
c768892f 1337 { 'J', 'P', 'r','x' }, /* json,pairs,raw,verify */
0009f510 1338 { 'M', 'T' }, /* mountpoint, target */
f1622b57 1339 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
c768892f
KZ
1340 { 'P','l','r','x' }, /* pairs,list,raw,verify */
1341 { 'p','x' }, /* poll,verify */
f1622b57 1342 { 'm','p','s' }, /* mtab,poll,fstab */
4c856a47 1343 { FINDMNT_OPT_PSEUDO, FINDMNT_OPT_REAL },
f1622b57
KZ
1344 { 0 }
1345 };
1346 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1347
04fd7a9f
KZ
1348 setlocale(LC_ALL, "");
1349 bindtextdomain(PACKAGE, LOCALEDIR);
1350 textdomain(PACKAGE);
2c308875 1351 close_stdout_atexit();
04fd7a9f 1352
9d67679b 1353 /* default output format */
85c9ca5a 1354 flags |= FL_TREE;
04fd7a9f 1355
9d67679b 1356 while ((c = getopt_long(argc, argv,
c768892f 1357 "AabCcDd:ehiJfF:o:O:p::PklmM:nN:rst:uvRS:T:Uw:Vx",
d466c6a1 1358 longopts, NULL)) != -1) {
f1622b57
KZ
1359
1360 err_exclusive_options(c, longopts, excl, excl_st);
1361
04fd7a9f 1362 switch(c) {
cd4ed46f
KZ
1363 case 'A':
1364 flags |= FL_ALL;
1365 break;
9d67679b 1366 case 'a':
85c9ca5a 1367 flags |= FL_ASCII;
9d67679b 1368 break;
2b6759df
KZ
1369 case 'b':
1370 flags |= FL_BYTES;
1371 break;
097e7f2f
KZ
1372 case 'C':
1373 flags |= FL_NOCACHE;
1374 break;
04fd7a9f
KZ
1375 case 'c':
1376 flags |= FL_CANONICALIZE;
1377 break;
eda399b9 1378 case 'D':
85c9ca5a 1379 flags &= ~FL_TREE;
cd4ed46f 1380 flags |= FL_DF;
eda399b9 1381 break;
04fd7a9f 1382 case 'd':
4e6bd74c 1383 if (!strcmp(optarg, "forward"))
04fd7a9f 1384 direction = MNT_ITER_FORWARD;
4e6bd74c 1385 else if (!strcmp(optarg, "backward"))
04fd7a9f
KZ
1386 direction = MNT_ITER_BACKWARD;
1387 else
1388 errx(EXIT_FAILURE,
8e350e48 1389 _("unknown direction '%s'"), optarg);
04fd7a9f
KZ
1390 break;
1391 case 'e':
1392 flags |= FL_EVALUATE;
1393 break;
04fd7a9f
KZ
1394 case 'i':
1395 flags |= FL_INVERT;
1396 break;
8449f2cb
KZ
1397 case 'J':
1398 flags |= FL_JSON;
1399 break;
9d67679b 1400 case 'f':
04fd7a9f
KZ
1401 flags |= FL_FIRSTONLY;
1402 break;
2f1ac44b 1403 case 'F':
8c5eba3e 1404 tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg);
2f1ac44b 1405 break;
9d67679b 1406 case 'u':
32e5466a 1407 disable_columns_truncate();
9d67679b 1408 break;
04fd7a9f 1409 case 'o':
c57dca68 1410 outarg = optarg;
04fd7a9f 1411 break;
00147883
SK
1412 case FINDMNT_OPT_OUTPUT_ALL:
1413 for (ncolumns = 0; ncolumns < ARRAY_SIZE(infos); ncolumns++) {
1414 if (is_tabdiff_column(ncolumns))
1415 continue;
1416 columns[ncolumns] = ncolumns;
1417 }
1418 break;
04fd7a9f 1419 case 'O':
9d67679b 1420 set_match(COL_OPTIONS, optarg);
04fd7a9f 1421 break;
32e5466a 1422 case 'p':
bdc3ed66 1423 if (optarg) {
c87638ad 1424 nactions = string_to_idarray(optarg,
bdc3ed66
KZ
1425 actions, ARRAY_SIZE(actions),
1426 poll_action_name_to_id);
1427 if (nactions < 0)
1428 exit(EXIT_FAILURE);
1429 }
32e5466a 1430 flags |= FL_POLL;
85c9ca5a 1431 flags &= ~FL_TREE;
32e5466a 1432 break;
49e9fd3a 1433 case 'P':
85c9ca5a
KZ
1434 flags |= FL_EXPORT;
1435 flags &= ~FL_TREE;
49e9fd3a 1436 break;
fdedb45e 1437 case 'm': /* mtab */
8c5eba3e 1438 tabtype = TABTYPE_MTAB;
85c9ca5a 1439 flags &= ~FL_TREE;
04fd7a9f 1440 break;
fdedb45e 1441 case 's': /* fstab */
8c5eba3e 1442 tabtype = TABTYPE_FSTAB;
85c9ca5a 1443 flags &= ~FL_TREE;
04fd7a9f 1444 break;
fdedb45e 1445 case 'k': /* kernel (mountinfo) */
8c5eba3e 1446 tabtype = TABTYPE_KERNEL;
04fd7a9f
KZ
1447 break;
1448 case 't':
9d67679b
KZ
1449 set_match(COL_FSTYPE, optarg);
1450 break;
1451 case 'r':
85c9ca5a
KZ
1452 flags &= ~FL_TREE; /* disable the default */
1453 flags |= FL_RAW; /* enable raw */
9d67679b
KZ
1454 break;
1455 case 'l':
85c9ca5a 1456 flags &= ~FL_TREE; /* disable the default */
9d67679b
KZ
1457 break;
1458 case 'n':
85c9ca5a 1459 flags |= FL_NOHEADINGS;
9d67679b 1460 break;
c7fcc830 1461 case 'N':
c7fcc830
KZ
1462 tabtype = TABTYPE_KERNEL;
1463 tabfiles = append_pid_tabfile(tabfiles, &ntabfiles,
1464 strtou32_or_err(optarg,
1465 _("invalid TID argument")));
1466 break;
b2214e1f
KZ
1467 case 'v':
1468 flags |= FL_NOFSROOT;
1469 break;
049caefd
KZ
1470 case 'R':
1471 flags |= FL_SUBMOUNTS;
1472 break;
9d67679b 1473 case 'S':
cd492186 1474 set_source_match(optarg);
9d67679b
KZ
1475 flags |= FL_NOSWAPMATCH;
1476 break;
0009f510
KZ
1477 case 'M':
1478 flags |= FL_STRICTTARGET;
1479 /* fallthrough */
9d67679b
KZ
1480 case 'T':
1481 set_match(COL_TARGET, optarg);
1482 flags |= FL_NOSWAPMATCH;
04fd7a9f 1483 break;
bebdda30
KZ
1484 case 'U':
1485 flags |= FL_UNIQ;
1486 break;
ad38fb9f 1487 case 'w':
db41a429 1488 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
ad38fb9f 1489 break;
c768892f
KZ
1490 case 'x':
1491 verify = 1;
1492 break;
1493 case FINDMNT_OPT_VERBOSE:
1494 flags |= FL_VERBOSE;
1495 break;
3ebe5477
KZ
1496 case FINDMNT_OPT_TREE:
1497 force_tree = 1;
1498 break;
4c856a47 1499 case FINDMNT_OPT_PSEUDO:
1500 flags |= FL_PSEUDO;
1501 break;
1502 case FINDMNT_OPT_REAL:
1503 flags |= FL_REAL;
1504 break;
2c308875
KZ
1505
1506 case 'h':
1507 usage();
1508 case 'V':
1509 print_version(EXIT_SUCCESS);
04fd7a9f 1510 default:
677ec86c 1511 errtryhelp(EXIT_FAILURE);
04fd7a9f
KZ
1512 }
1513 }
1514
cd4ed46f 1515 if (!ncolumns && (flags & FL_DF)) {
edaf38cf
KZ
1516 add_column(columns, ncolumns++, COL_SOURCE);
1517 add_column(columns, ncolumns++, COL_FSTYPE);
1518 add_column(columns, ncolumns++, COL_SIZE);
1519 add_column(columns, ncolumns++, COL_USED);
1520 add_column(columns, ncolumns++, COL_AVAIL);
1521 add_column(columns, ncolumns++, COL_USEPERC);
1522 add_column(columns, ncolumns++, COL_TARGET);
eda399b9
DR
1523 }
1524
32e5466a
KZ
1525 /* default columns */
1526 if (!ncolumns) {
1527 if (flags & FL_POLL)
edaf38cf 1528 add_column(columns, ncolumns++, COL_ACTION);
32e5466a 1529
edaf38cf
KZ
1530 add_column(columns, ncolumns++, COL_TARGET);
1531 add_column(columns, ncolumns++, COL_SOURCE);
1532 add_column(columns, ncolumns++, COL_FSTYPE);
1533 add_column(columns, ncolumns++, COL_OPTIONS);
32e5466a
KZ
1534 }
1535
c57dca68
MB
1536 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
1537 &ncolumns, column_name_to_id) < 0)
1538 return EXIT_FAILURE;
1539
8c5eba3e 1540 if (!tabtype)
c768892f 1541 tabtype = verify ? TABTYPE_FSTAB : TABTYPE_KERNEL;
2f1ac44b 1542
f1622b57
KZ
1543 if ((flags & FL_POLL) && ntabfiles > 1)
1544 errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file"));
b2214e1f 1545
9d67679b
KZ
1546 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
1547 errx(EXIT_FAILURE, _(
1548 "options --target and --source can't be used together "
1549 "with command line element that is not an option"));
1550
04fd7a9f 1551 if (optind < argc)
cd492186 1552 set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */
04fd7a9f 1553 if (optind < argc)
9d67679b 1554 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
04fd7a9f 1555
049caefd
KZ
1556 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
1557 /* don't care about submounts if list all mounts */
1558 flags &= ~FL_SUBMOUNTS;
1559
31f67453
KZ
1560 if (!(flags & FL_SUBMOUNTS) && ((flags & FL_FIRSTONLY)
1561 || get_match(COL_TARGET)
1562 || get_match(COL_SOURCE)
1563 || get_match(COL_MAJMIN)))
85c9ca5a 1564 flags &= ~FL_TREE;
fdedb45e 1565
ac808156
KZ
1566 if (!(flags & FL_NOSWAPMATCH) &&
1567 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1568 /*
1569 * Check if we can swap source and target, it's
1570 * not possible if the source is LABEL=/UUID=
1571 */
1572 const char *x = get_match(COL_SOURCE);
1573
090b5e84
KZ
1574 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) ||
1575 !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9))
ac808156
KZ
1576 flags |= FL_NOSWAPMATCH;
1577 }
1578
fdedb45e
KZ
1579 /*
1580 * initialize libmount
1581 */
ac808156
KZ
1582 mnt_init_debug(0);
1583
8c5eba3e 1584 tb = parse_tabfiles(tabfiles, ntabfiles, tabtype);
04fd7a9f 1585 if (!tb)
fdedb45e 1586 goto leave;
04fd7a9f 1587
9c277ede
KZ
1588 if (tabtype == TABTYPE_MTAB && tab_is_kernel(tb))
1589 tabtype = TABTYPE_KERNEL;
1590
3ebe5477
KZ
1591 istree = tab_is_tree(tb);
1592 if (istree && force_tree)
1593 flags |= FL_TREE;
1594
1595 if ((flags & FL_TREE) && (ntabfiles > 1 || !istree))
85c9ca5a 1596 flags &= ~FL_TREE;
2f1ac44b 1597
097e7f2f
KZ
1598 if (!(flags & FL_NOCACHE)) {
1599 cache = mnt_new_cache();
1600 if (!cache) {
1601 warn(_("failed to initialize libmount cache"));
1602 goto leave;
1603 }
1604 mnt_table_set_cache(tb, cache);
fb329bbc
ER
1605
1606 if (tabtype != TABTYPE_KERNEL)
1607 cache_set_targets(cache);
fdedb45e 1608 }
04fd7a9f 1609
bebdda30
KZ
1610 if (flags & FL_UNIQ)
1611 mnt_table_uniq_fs(tb, MNT_UNIQ_KEEPTREE, uniq_fs_target_cmp);
4bfd4bff 1612
c768892f
KZ
1613 if (verify) {
1614 rc = verify_table(tb);
1615 goto leave;
1616 }
1617
fdedb45e 1618 /*
c768892f 1619 * initialize libsmartcols
fdedb45e 1620 */
c768892f 1621 scols_init_debug(0);
0925a9dd 1622 table = scols_new_table();
218a3a94 1623 if (!table) {
780ce22c 1624 warn(_("failed to allocate output table"));
fdedb45e
KZ
1625 goto leave;
1626 }
0925a9dd
KZ
1627 scols_table_enable_raw(table, !!(flags & FL_RAW));
1628 scols_table_enable_export(table, !!(flags & FL_EXPORT));
8449f2cb 1629 scols_table_enable_json(table, !!(flags & FL_JSON));
0925a9dd
KZ
1630 scols_table_enable_ascii(table, !!(flags & FL_ASCII));
1631 scols_table_enable_noheadings(table, !!(flags & FL_NOHEADINGS));
ac808156 1632
8449f2cb
KZ
1633 if (flags & FL_JSON)
1634 scols_table_set_name(table, "filesystems");
1635
fdedb45e 1636 for (i = 0; i < ncolumns; i++) {
3a645e36 1637 struct libscols_column *cl;
32e5466a
KZ
1638 int fl = get_column_flags(i);
1639 int id = get_column_id(i);
1640
85c9ca5a 1641 if (!(flags & FL_TREE))
218a3a94 1642 fl &= ~SCOLS_FL_TREE;
04fd7a9f 1643
32e5466a 1644 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
77a1c5f7 1645 warnx(_("%s column is requested, but --poll "
32e5466a
KZ
1646 "is not enabled"), get_column_name(i));
1647 goto leave;
1648 }
3a645e36
KZ
1649 cl = scols_table_new_column(table, get_column_name(i),
1650 get_column_whint(i), fl);
1651 if (!cl) {
780ce22c 1652 warn(_("failed to allocate output column"));
fdedb45e 1653 goto leave;
9d67679b 1654 }
3a645e36
KZ
1655
1656 if (flags & FL_JSON) {
1657 switch (id) {
1658 case COL_SIZE:
1659 case COL_AVAIL:
1660 case COL_USED:
1661 if (!(flags & FL_BYTES))
1662 break;
1663 /* fallthrough */
1664 case COL_ID:
1665 case COL_FREQ:
1666 case COL_PASSNO:
1667 case COL_TID:
1668 scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
1669 break;
1670 default:
1671 scols_column_set_json_type(cl, SCOLS_JSON_STRING);
1672 break;
1673 }
1674 }
fdedb45e 1675 }
9d67679b 1676
fdedb45e
KZ
1677 /*
1678 * Fill in data to the output table
1679 */
275c9a48 1680 if (flags & FL_POLL) {
8c5eba3e 1681 /* poll mode (accept the first tabfile only) */
218a3a94 1682 rc = poll_table(tb, tabfiles ? *tabfiles : _PATH_PROC_MOUNTINFO, timeout, table, direction);
32e5466a 1683
85c9ca5a 1684 } else if ((flags & FL_TREE) && !(flags & FL_SUBMOUNTS)) {
049caefd 1685 /* whole tree */
218a3a94 1686 rc = create_treenode(table, tb, NULL, NULL);
31f67453 1687 } else {
9e930041 1688 /* whole list of sub-tree */
218a3a94 1689 rc = add_matching_lines(tb, table, direction);
fdedb45e 1690
3e373967
KZ
1691 if (rc != 0
1692 && tabtype == TABTYPE_KERNEL
1693 && (flags & FL_NOSWAPMATCH)
0009f510 1694 && !(flags & FL_STRICTTARGET)
3e373967
KZ
1695 && get_match(COL_TARGET)) {
1696 /*
1697 * Found nothing, maybe the --target is regular file,
1698 * try it again with extra functionality for target
1699 * match
1700 */
cd41b385 1701 enable_extra_target_match(tb);
218a3a94 1702 rc = add_matching_lines(tb, table, direction);
3e373967
KZ
1703 }
1704 }
1705
fdedb45e 1706 /*
32e5466a 1707 * Print the output table for non-poll modes
fdedb45e 1708 */
32e5466a 1709 if (!rc && !(flags & FL_POLL))
218a3a94 1710 scols_print_table(table);
fdedb45e 1711leave:
218a3a94 1712 scols_unref_table(table);
fdedb45e 1713
50fccba1 1714 mnt_unref_table(tb);
6195f9e6 1715 mnt_unref_cache(cache);
50fccba1 1716
8c5eba3e 1717 free(tabfiles);
e346233e
KZ
1718#ifdef HAVE_LIBUDEV
1719 udev_unref(udev);
1720#endif
049caefd 1721 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
04fd7a9f 1722}