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