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