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