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