]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/findmnt.c
fallocate: fix missing sentinel for is_nul()
[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),
bebdda30
KZ
61 FL_ALL = (1 << 11),
62 FL_UNIQ = (1 << 12)
9d67679b
KZ
63};
64
65/* column IDs */
04fd7a9f
KZ
66enum {
67 COL_SOURCE,
68 COL_TARGET,
69 COL_FSTYPE,
70 COL_OPTIONS,
631280e0
KZ
71 COL_VFS_OPTIONS,
72 COL_FS_OPTIONS,
04fd7a9f
KZ
73 COL_LABEL,
74 COL_UUID,
090b5e84
KZ
75 COL_PARTLABEL,
76 COL_PARTUUID,
46236388 77 COL_MAJMIN,
32e5466a
KZ
78 COL_ACTION,
79 COL_OLD_TARGET,
80 COL_OLD_OPTIONS,
a85ef33b
DR
81 COL_SIZE,
82 COL_AVAIL,
83 COL_USED,
84 COL_USEPERC,
8b8cd87b 85 COL_FSROOT,
7d8f4e0c 86 COL_TID,
9238e0fa 87 COL_ID,
327ea85a 88 COL_OPT_FIELDS,
624f2b47 89 COL_PROPAGATION,
426f0cea
KZ
90 COL_FREQ,
91 COL_PASSNO,
04fd7a9f 92
af0dc7d3 93 FINDMNT_NCOLUMNS
04fd7a9f
KZ
94};
95
8c5eba3e
KZ
96enum {
97 TABTYPE_FSTAB = 1,
98 TABTYPE_MTAB,
99 TABTYPE_KERNEL
100};
101
9d67679b
KZ
102/* column names */
103struct colinfo {
104 const char *name; /* header */
105 double whint; /* width hint (N < 1 is in percent of termwidth) */
32e5466a 106 int flags; /* tt flags */
b152e359 107 const char *help; /* column description */
9d67679b 108 const char *match; /* pattern for match_func() */
cd492186 109 void *match_data; /* match specific data */
04fd7a9f
KZ
110};
111
af0dc7d3
KZ
112/* columns descriptions (don't use const, this is writable) */
113static struct colinfo infos[FINDMNT_NCOLUMNS] = {
7aa69cf2
KZ
114 [COL_SOURCE] = { "SOURCE", 0.25, TT_FL_NOEXTREMES, N_("source device") },
115 [COL_TARGET] = { "TARGET", 0.30, TT_FL_TREE | TT_FL_NOEXTREMES, N_("mountpoint") },
b152e359
KZ
116 [COL_FSTYPE] = { "FSTYPE", 0.10, TT_FL_TRUNC, N_("filesystem type") },
117 [COL_OPTIONS] = { "OPTIONS", 0.10, TT_FL_TRUNC, N_("all mount options") },
118 [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC, N_("VFS specific mount options") },
119 [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC, N_("FS specific mount options") },
120 [COL_LABEL] = { "LABEL", 0.10, 0, N_("filesystem label") },
121 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
090b5e84
KZ
122 [COL_PARTLABEL] = { "PARTLABEL", 0.10, 0, N_("partition label") },
123 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
b152e359
KZ
124 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
125 [COL_ACTION] = { "ACTION", 10, TT_FL_STRICTWIDTH, N_("action detected by --poll") },
126 [COL_OLD_OPTIONS] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC, N_("old mount options saved by --poll") },
127 [COL_OLD_TARGET] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
451d7646
KZ
128 [COL_SIZE] = { "SIZE", 5, TT_FL_RIGHT, N_("filesystem size") },
129 [COL_AVAIL] = { "AVAIL", 5, TT_FL_RIGHT, N_("filesystem size available") },
130 [COL_USED] = { "USED", 5, TT_FL_RIGHT, N_("filesystem size used") },
131 [COL_USEPERC] = { "USE%", 3, TT_FL_RIGHT, N_("filesystem use percentage") },
8b8cd87b 132 [COL_FSROOT] = { "FSROOT", 0.25, TT_FL_NOEXTREMES, N_("filesystem root") },
7d8f4e0c 133 [COL_TID] = { "TID", 4, TT_FL_RIGHT, N_("task ID") },
9238e0fa 134 [COL_ID] = { "ID", 2, TT_FL_RIGHT, N_("mount ID") },
327ea85a 135 [COL_OPT_FIELDS] = { "OPT-FIELDS", 0.10, TT_FL_TRUNC, N_("optional mount fields") },
426f0cea 136 [COL_PROPAGATION] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
97b820bf 137 [COL_FREQ] = { "FREQ", 1, TT_FL_RIGHT, N_("dump(8) period in days [fstab only]") },
426f0cea 138 [COL_PASSNO] = { "PASSNO", 1, TT_FL_RIGHT, N_("pass number on parallel fsck(8) [fstab only]") }
04fd7a9f
KZ
139};
140
9d67679b 141/* global flags */
af0dc7d3
KZ
142static int flags;
143static int tt_flags;
9d67679b 144
fdedb45e 145/* array with IDs of enabled columns */
af0dc7d3
KZ
146static int columns[FINDMNT_NCOLUMNS];
147static int ncolumns;
9d67679b 148
582a5006 149/* poll actions (parsed --poll=<list> */
af0dc7d3
KZ
150#define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
151static int actions[FINDMNT_NACTIONS];
152static int nactions;
582a5006 153
9d67679b 154/* libmount cache */
af0dc7d3 155static struct libmnt_cache *cache;
04fd7a9f 156
e346233e
KZ
157#ifdef HAVE_LIBUDEV
158struct udev *udev;
159#endif
160
31f67453
KZ
161static int match_func(struct libmnt_fs *fs, void *data __attribute__ ((__unused__)));
162
163
00b4bcdf 164static int get_column_id(int num)
9d67679b 165{
9d67679b 166 assert(num < ncolumns);
af0dc7d3 167 assert(columns[num] < FINDMNT_NCOLUMNS);
fdedb45e 168 return columns[num];
9d67679b
KZ
169}
170
00b4bcdf 171static struct colinfo *get_column_info(int num)
9d67679b
KZ
172{
173 return &infos[ get_column_id(num) ];
174}
175
00b4bcdf 176static const char *column_id_to_name(int id)
9d67679b 177{
af0dc7d3 178 assert(id < FINDMNT_NCOLUMNS);
9d67679b
KZ
179 return infos[id].name;
180}
181
00b4bcdf 182static const char *get_column_name(int num)
9d67679b 183{
fdedb45e 184 return get_column_info(num)->name;
9d67679b
KZ
185}
186
00b4bcdf 187static float get_column_whint(int num)
9d67679b 188{
fdedb45e 189 return get_column_info(num)->whint;
9d67679b
KZ
190}
191
32e5466a 192static int get_column_flags(int num)
9d67679b 193{
32e5466a 194 return get_column_info(num)->flags;
9d67679b
KZ
195}
196
00b4bcdf 197static const char *get_match(int id)
9d67679b 198{
af0dc7d3 199 assert(id < FINDMNT_NCOLUMNS);
9d67679b
KZ
200 return infos[id].match;
201}
202
cd492186
KZ
203static void *get_match_data(int id)
204{
205 assert(id < FINDMNT_NCOLUMNS);
206 return infos[id].match_data;
207}
208
00b4bcdf 209static void set_match(int id, const char *match)
9d67679b 210{
af0dc7d3 211 assert(id < FINDMNT_NCOLUMNS);
9d67679b
KZ
212 infos[id].match = match;
213}
214
cd492186
KZ
215static void set_match_data(int id, void *data)
216{
217 assert(id < FINDMNT_NCOLUMNS);
218 infos[id].match_data = data;
219}
220
4bfd4bff
KZ
221/*
222 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
223 * data format.
224 */
cd492186
KZ
225static void set_source_match(const char *data)
226{
227 int maj, min;
228
229 if (sscanf(data, "%d:%d", &maj, &min) == 2) {
230 dev_t *devno = xmalloc(sizeof(dev_t));
231
232 *devno = makedev(maj, min);
233 set_match(COL_MAJMIN, data);
234 set_match_data(COL_MAJMIN, (void *) devno);
4bfd4bff 235 flags |= FL_NOSWAPMATCH;
cd492186
KZ
236 } else
237 set_match(COL_SOURCE, data);
238}
239
4bfd4bff
KZ
240static void enable_extra_target_match(void)
241{
242 char *cn = NULL, *mnt = NULL;
243
244 /*
245 * Check if match pattern is mountpoint, if not use the
246 * real mountpoint.
247 */
248 cn = mnt_resolve_path(get_match(COL_TARGET), cache);
249 if (!cn)
250 return;
251
252 mnt = mnt_get_mountpoint(cn);
253 if (!mnt || strcmp(mnt, cn) == 0)
254 return;
255
256 /* replace the current setting with the real mountpoint */
257 set_match(COL_TARGET, mnt);
258}
259
cd492186 260
32e5466a
KZ
261static int is_tabdiff_column(int id)
262{
af0dc7d3 263 assert(id < FINDMNT_NCOLUMNS);
32e5466a
KZ
264
265 switch(id) {
266 case COL_ACTION:
267 case COL_OLD_TARGET:
268 case COL_OLD_OPTIONS:
269 return 1;
270 default:
271 break;
272 }
273 return 0;
274}
275
9d67679b
KZ
276/*
277 * "findmnt" without any filter
278 */
00b4bcdf 279static int is_listall_mode(void)
9d67679b 280{
cd4ed46f
KZ
281 if ((flags & FL_DF) && !(flags & FL_ALL))
282 return 0;
283
9d67679b
KZ
284 return (!get_match(COL_SOURCE) &&
285 !get_match(COL_TARGET) &&
286 !get_match(COL_FSTYPE) &&
cd492186
KZ
287 !get_match(COL_OPTIONS) &&
288 !get_match(COL_MAJMIN));
9d67679b
KZ
289}
290
582a5006
KZ
291/*
292 * Returns 1 if the @act is in the --poll=<list>
293 */
294static int has_poll_action(int act)
295{
296 int i;
297
298 if (!nactions)
299 return 1; /* all actions enabled */
300 for (i = 0; i < nactions; i++)
301 if (actions[i] == act)
302 return 1;
303 return 0;
304}
305
306static int poll_action_name_to_id(const char *name, size_t namesz)
307{
308 int id = -1;
309
310 if (strncasecmp(name, "move", namesz) == 0 && namesz == 4)
311 id = MNT_TABDIFF_MOVE;
312 else if (strncasecmp(name, "mount", namesz) == 0 && namesz == 5)
313 id = MNT_TABDIFF_MOUNT;
314 else if (strncasecmp(name, "umount", namesz) == 0 && namesz == 6)
315 id = MNT_TABDIFF_UMOUNT;
316 else if (strncasecmp(name, "remount", namesz) == 0 && namesz == 7)
317 id = MNT_TABDIFF_REMOUNT;
318 else
319 warnx(_("unknown action: %s"), name);
320
321 return id;
322}
323
9d67679b
KZ
324/*
325 * findmnt --first-only <devname|TAG=|mountpoint>
326 *
327 * ... it works like "mount <devname|TAG=|mountpoint>"
328 */
00b4bcdf 329static int is_mount_compatible_mode(void)
9d67679b
KZ
330{
331 if (!get_match(COL_SOURCE))
332 return 0; /* <devname|TAG=|mountpoint> is required */
333 if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS))
334 return 0; /* cannot be restricted by -t or -O */
335 if (!(flags & FL_FIRSTONLY))
336 return 0; /* we have to return the first entry only */
337
338 return 1; /* ok */
339}
340
32e5466a 341static void disable_columns_truncate(void)
9d67679b
KZ
342{
343 int i;
344
af0dc7d3 345 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
32e5466a 346 infos[i].flags &= ~TT_FL_TRUNC;
9d67679b
KZ
347}
348
04fd7a9f
KZ
349/*
350 * converts @name to column ID
351 */
9d67679b 352static int column_name_to_id(const char *name, size_t namesz)
04fd7a9f
KZ
353{
354 int i;
355
af0dc7d3 356 for (i = 0; i < FINDMNT_NCOLUMNS; i++) {
9d67679b 357 const char *cn = column_id_to_name(i);
04fd7a9f 358
9d67679b 359 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
04fd7a9f
KZ
360 return i;
361 }
fdedb45e 362 warnx(_("unknown column: %s"), name);
04fd7a9f
KZ
363 return -1;
364}
365
e346233e
KZ
366
367#ifdef HAVE_LIBUDEV
368static char *get_tag_from_udev(const char *devname, int col)
369{
370 struct udev_device *dev;
371 const char *data = NULL;
4ba4893b 372 char *res = NULL, *path;
e346233e
KZ
373
374 if (!udev)
375 udev = udev_new();
376 if (!udev)
377 return NULL;
378
4ba4893b
KZ
379 /* libudev don't like /dev/mapper/ symlinks */
380 path = realpath(devname, NULL);
381 if (path)
382 devname = path;
383
e346233e
KZ
384 if (strncmp(devname, "/dev/", 5) == 0)
385 devname += 5;
386
387 dev = udev_device_new_from_subsystem_sysname(udev, "block", devname);
4ba4893b
KZ
388 free(path);
389
e346233e
KZ
390 if (!dev)
391 return NULL;
392
393 switch(col) {
394 case COL_LABEL:
395 data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC");
396 break;
397 case COL_UUID:
398 data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC");
399 break;
400 case COL_PARTUUID:
401 data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID");
402 break;
403 case COL_PARTLABEL:
404 data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME");
405 break;
406 default:
407 break;
408 }
409
410 if (data) {
411 res = xstrdup(data);
412 unhexmangle_string(res);
413 }
414
415 udev_device_unref(dev);
416 return res;
417}
418#endif /* HAVE_LIBUDEV */
419
fdedb45e 420/* Returns LABEL or UUID */
b3386c83 421static char *get_tag(struct libmnt_fs *fs, const char *tagname, int col
81499ab2
KZ
422#ifndef HAVE_LIBUDEV
423 __attribute__((__unused__))
424#endif
425 )
9d67679b 426{
b3386c83
KZ
427 const char *t, *v;
428 char *res = NULL;
9d67679b
KZ
429
430 if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname))
b3386c83 431 res = xstrdup(v);
9d67679b 432 else {
e346233e
KZ
433 const char *dev = mnt_fs_get_source(fs);
434
435 if (dev)
436 dev = mnt_resolve_spec(dev, cache);
437#ifdef HAVE_LIBUDEV
438 if (dev)
439 res = get_tag_from_udev(dev, col);
440#endif
b3386c83 441 if (!res) {
e346233e 442 res = mnt_cache_find_tag_value(cache, dev, tagname);
b3386c83
KZ
443 if (res && cache)
444 /* don't return pointer to cache */
445 res = xstrdup(res);
446 }
9d67679b
KZ
447 }
448
449 return res;
450}
451
b3386c83 452static char *get_vfs_attr(struct libmnt_fs *fs, int sizetype)
a85ef33b
DR
453{
454 struct statvfs buf;
6901f22c 455 uint64_t vfs_attr = 0;
a85ef33b
DR
456 char *sizestr;
457
458 if (statvfs(mnt_fs_get_target(fs), &buf) != 0)
459 return NULL;
460
461 switch(sizetype) {
462 case COL_SIZE:
463 vfs_attr = buf.f_frsize * buf.f_blocks;
464 break;
465 case COL_AVAIL:
ed6c8c1b 466 vfs_attr = buf.f_frsize * buf.f_bavail;
a85ef33b
DR
467 break;
468 case COL_USED:
469 vfs_attr = buf.f_frsize * (buf.f_blocks - buf.f_bfree);
470 break;
471 case COL_USEPERC:
472 if (buf.f_blocks == 0)
203c945a 473 return xstrdup("-");
a85ef33b 474
d7bcb205
KZ
475 xasprintf(&sizestr, "%.0f%%",
476 (double)(buf.f_blocks - buf.f_bfree) /
477 buf.f_blocks * 100);
a85ef33b
DR
478 return sizestr;
479 }
480
b3386c83 481 return vfs_attr == 0 ? xstrdup("0") :
a85ef33b
DR
482 size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
483}
484
46236388 485/* reads FS data from libmount
46236388 486 */
b3386c83 487static char *get_data(struct libmnt_fs *fs, int num)
9d67679b 488{
b3386c83 489 char *str = NULL;
a85ef33b 490 int col_id = get_column_id(num);
9d67679b 491
a85ef33b 492 switch (col_id) {
04fd7a9f 493 case COL_SOURCE:
b2214e1f
KZ
494 {
495 const char *root = mnt_fs_get_root(fs);
b3386c83 496 const char *spec = mnt_fs_get_srcpath(fs);
b2214e1f 497
b3386c83
KZ
498 if (spec && (flags & FL_CANONICALIZE))
499 spec = mnt_resolve_path(spec, cache);
500 if (!spec) {
501 spec = mnt_fs_get_source(fs);
9d67679b 502
b3386c83
KZ
503 if (spec && (flags & FL_EVALUATE))
504 spec = mnt_resolve_spec(spec, cache);
b2214e1f 505 }
b3386c83
KZ
506 if (root && spec && !(flags & FL_NOFSROOT) && strcmp(root, "/"))
507 xasprintf(&str, "%s[%s]", spec, root);
508 else if (spec)
509 str = xstrdup(spec);
04fd7a9f 510 break;
b2214e1f 511 }
04fd7a9f 512 case COL_TARGET:
b3386c83 513 str = xstrdup(mnt_fs_get_target(fs));
04fd7a9f
KZ
514 break;
515 case COL_FSTYPE:
b3386c83 516 str = xstrdup(mnt_fs_get_fstype(fs));
04fd7a9f
KZ
517 break;
518 case COL_OPTIONS:
b3386c83 519 str = xstrdup(mnt_fs_get_options(fs));
04fd7a9f 520 break;
631280e0 521 case COL_VFS_OPTIONS:
b3386c83 522 str = xstrdup(mnt_fs_get_vfs_options(fs));
631280e0
KZ
523 break;
524 case COL_FS_OPTIONS:
b3386c83 525 str = xstrdup(mnt_fs_get_fs_options(fs));
631280e0 526 break;
327ea85a 527 case COL_OPT_FIELDS:
b3386c83 528 str = xstrdup(mnt_fs_get_optional_fields(fs));
327ea85a 529 break;
9d67679b 530 case COL_UUID:
e346233e 531 str = get_tag(fs, "UUID", col_id);
9d67679b 532 break;
090b5e84 533 case COL_PARTUUID:
e346233e 534 str = get_tag(fs, "PARTUUID", col_id);
090b5e84 535 break;
9d67679b 536 case COL_LABEL:
e346233e 537 str = get_tag(fs, "LABEL", col_id);
9d67679b 538 break;
090b5e84 539 case COL_PARTLABEL:
e346233e 540 str = get_tag(fs, "PARTLABEL", col_id);
090b5e84
KZ
541 break;
542
46236388
KZ
543 case COL_MAJMIN:
544 {
545 dev_t devno = mnt_fs_get_devno(fs);
60b17c6f
KZ
546 if (!devno)
547 break;
548
549 if ((tt_flags & TT_FL_RAW) || (tt_flags & TT_FL_EXPORT))
b3386c83 550 xasprintf(&str, "%u:%u", major(devno), minor(devno));
60b17c6f 551 else
b3386c83 552 xasprintf(&str, "%3u:%-3u", major(devno), minor(devno));
a85ef33b 553 break;
46236388 554 }
a85ef33b
DR
555 case COL_SIZE:
556 case COL_AVAIL:
557 case COL_USED:
558 case COL_USEPERC:
559 str = get_vfs_attr(fs, col_id);
560 break;
8b8cd87b 561 case COL_FSROOT:
b3386c83 562 str = xstrdup(mnt_fs_get_root(fs));
8b8cd87b 563 break;
7d8f4e0c 564 case COL_TID:
b3386c83
KZ
565 if (mnt_fs_get_tid(fs))
566 xasprintf(&str, "%d", mnt_fs_get_tid(fs));
7d8f4e0c 567 break;
9238e0fa 568 case COL_ID:
b3386c83
KZ
569 if (mnt_fs_get_id(fs))
570 xasprintf(&str, "%d", mnt_fs_get_id(fs));
9238e0fa 571 break;
624f2b47
KZ
572 case COL_PROPAGATION:
573 if (mnt_fs_is_kernel(fs)) {
574 unsigned long fl = 0;
575 char *n = NULL;
576
577 if (mnt_fs_get_propagation(fs, &fl) != 0)
578 break;
579
580 n = xstrdup((fl & MS_SHARED) ? "shared" : "private");
581
582 if (fl & MS_SLAVE) {
b3386c83 583 xasprintf(&str, "%s,slave", n);
624f2b47 584 free(n);
b3386c83 585 n = str;
624f2b47
KZ
586 }
587 if (fl & MS_UNBINDABLE) {
b3386c83 588 xasprintf(&str, "%s,unbindable", n);
624f2b47 589 free(n);
b3386c83 590 n = str;
624f2b47
KZ
591 }
592 str = n;
593 }
594 break;
426f0cea 595 case COL_FREQ:
b3386c83
KZ
596 if (!mnt_fs_is_kernel(fs))
597 xasprintf(&str, "%d", mnt_fs_get_freq(fs));
426f0cea
KZ
598 break;
599 case COL_PASSNO:
b3386c83
KZ
600 if (!mnt_fs_is_kernel(fs))
601 xasprintf(&str, "%d", mnt_fs_get_passno(fs));
426f0cea 602 break;
04fd7a9f 603 default:
9d67679b 604 break;
04fd7a9f 605 }
fdedb45e 606 return str;
9d67679b
KZ
607}
608
b3386c83 609static char *get_tabdiff_data(struct libmnt_fs *old_fs,
32e5466a
KZ
610 struct libmnt_fs *new_fs,
611 int change,
612 int num)
613{
b3386c83 614 char *str = NULL;
32e5466a
KZ
615
616 switch (get_column_id(num)) {
617 case COL_ACTION:
618 switch (change) {
619 case MNT_TABDIFF_MOUNT:
620 str = _("mount");
621 break;
622 case MNT_TABDIFF_UMOUNT:
623 str = _("umount");
624 break;
625 case MNT_TABDIFF_REMOUNT:
626 str = _("remount");
627 break;
628 case MNT_TABDIFF_MOVE:
629 str = _("move");
630 break;
631 default:
632 str = _("unknown");
633 break;
634 }
b3386c83 635 str = xstrdup(str);
32e5466a
KZ
636 break;
637 case COL_OLD_OPTIONS:
77a1c5f7
KZ
638 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
639 change == MNT_TABDIFF_UMOUNT))
b3386c83 640 str = xstrdup(mnt_fs_get_options(old_fs));
32e5466a
KZ
641 break;
642 case COL_OLD_TARGET:
77a1c5f7
KZ
643 if (old_fs && (change == MNT_TABDIFF_MOVE ||
644 change == MNT_TABDIFF_UMOUNT))
b3386c83 645 str = xstrdup(mnt_fs_get_target(old_fs));
32e5466a
KZ
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) {
50fccba1 805 mnt_unref_table(tb);
8c5eba3e
KZ
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:
50fccba1 1086 mnt_unref_table(tb_new);
32e5466a
KZ
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
bebdda30
KZ
1094static int uniq_fs_target_cmp(
1095 struct libmnt_table *tb __attribute__((__unused__)),
1096 struct libmnt_fs *a,
1097 struct libmnt_fs *b)
1098{
1099 return !mnt_fs_match_target(a, mnt_fs_get_target(b), cache);
1100}
1101
abafd686 1102static void __attribute__((__noreturn__)) usage(FILE *out)
04fd7a9f 1103{
9ead0006
KZ
1104 int i;
1105
5cc12c40 1106 fputs(USAGE_HEADER, out);
9d67679b 1107 fprintf(out, _(
9d67679b
KZ
1108 " %1$s [options]\n"
1109 " %1$s [options] <device> | <mountpoint>\n"
1110 " %1$s [options] <device> <mountpoint>\n"
1111 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
1112 program_invocation_short_name);
04fd7a9f 1113
b95c6e88
KZ
1114 fputs(USAGE_OPTIONS, out);
1115 fputs(_(" -s, --fstab search in static table of filesystems\n"), out);
1116 fputs(_(" -m, --mtab search in table of mounted filesystems\n"), out);
1117 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1118 " filesystems (default)\n"), out);
1119 fputc('\n', out);
1120 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out);
1121 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out);
1122 fputc('\n', out);
1123
1124 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out);
1125 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out);
1126 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out);
1127 fputs(_(" -D, --df imitate the output of df(1)\n"), out);
1128 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out);
1129 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1130 " to device names\n"), out);
1131 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out);
1132 fputs(_(" -f, --first-only print the first found filesystem only\n"), out);
1133 fputs(_(" -i, --invert invert the sense of matching\n"), out);
1134 fputs(_(" -l, --list use list format output\n"), out);
1135 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out);
1136 fputs(_(" -n, --noheadings don't print column headings\n"), out);
bebdda30 1137 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out);
b95c6e88
KZ
1138 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out);
1139 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out);
1140 fputs(_(" -o, --output <list> the output columns to be shown\n"), out);
1141 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
1142 fputs(_(" -r, --raw use raw output format\n"), out);
1143 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out);
1144 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out);
1145 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out);
1146 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1147 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out);
1148 fputs(_(" -T, --target <string> the mountpoint to use\n"), out);
04fd7a9f 1149
5cc12c40
SK
1150 fputs(USAGE_SEPARATOR, out);
1151 fputs(USAGE_HELP, out);
1152 fputs(USAGE_VERSION, out);
9ead0006
KZ
1153
1154 fprintf(out, _("\nAvailable columns:\n"));
1155
b152e359
KZ
1156 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
1157 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
9ead0006 1158
beda9dd3 1159 fprintf(out, USAGE_MAN_TAIL("findmnt(8)"));
04fd7a9f
KZ
1160
1161 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1162}
1163
1164int main(int argc, char *argv[])
1165{
68164f6c 1166 struct libmnt_table *tb = NULL;
8c5eba3e 1167 char **tabfiles = NULL;
9d67679b 1168 int direction = MNT_ITER_FORWARD;
cd4ed46f 1169 int i, c, rc = -1, timeout = -1;
8c5eba3e 1170 int ntabfiles = 0, tabtype = 0;
c57dca68 1171 char *outarg = NULL;
fdedb45e 1172
fdedb45e 1173 struct tt *tt = NULL;
fdedb45e 1174
6c7d5ae9 1175 static const struct option longopts[] = {
cd4ed46f 1176 { "all", 0, 0, 'A' },
9d67679b 1177 { "ascii", 0, 0, 'a' },
04fd7a9f
KZ
1178 { "canonicalize", 0, 0, 'c' },
1179 { "direction", 1, 0, 'd' },
eda399b9 1180 { "df", 0, 0, 'D' },
04fd7a9f 1181 { "evaluate", 0, 0, 'e' },
9d67679b 1182 { "first-only", 0, 0, 'f' },
2f1ac44b 1183 { "fstab", 0, 0, 's' },
04fd7a9f
KZ
1184 { "help", 0, 0, 'h' },
1185 { "invert", 0, 0, 'i' },
9d67679b
KZ
1186 { "kernel", 0, 0, 'k' },
1187 { "list", 0, 0, 'l' },
1188 { "mtab", 0, 0, 'm' },
1189 { "noheadings", 0, 0, 'n' },
1190 { "notruncate", 0, 0, 'u' },
04fd7a9f 1191 { "options", 1, 0, 'O' },
9d67679b 1192 { "output", 1, 0, 'o' },
582a5006 1193 { "poll", 2, 0, 'p' },
49e9fd3a 1194 { "pairs", 0, 0, 'P' },
9d67679b 1195 { "raw", 0, 0, 'r' },
04fd7a9f 1196 { "types", 1, 0, 't' },
395eb05d 1197 { "nofsroot", 0, 0, 'v' },
049caefd 1198 { "submounts", 0, 0, 'R' },
9d67679b 1199 { "source", 1, 0, 'S' },
2f1ac44b 1200 { "tab-file", 1, 0, 'F' },
c7fcc830 1201 { "task", 1, 0, 'N' },
9d67679b 1202 { "target", 1, 0, 'T' },
ad38fb9f 1203 { "timeout", 1, 0, 'w' },
bebdda30 1204 { "uniq", 0, 0, 'U' },
5cc12c40 1205 { "version", 0, 0, 'V' },
9d67679b 1206
04fd7a9f
KZ
1207 { NULL, 0, 0, 0 }
1208 };
1209
f1622b57
KZ
1210 static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */
1211 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1212 { 'P','l','r' }, /* pairs,list,raw */
1213 { 'm','p','s' }, /* mtab,poll,fstab */
1214 { 0 }
1215 };
1216 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1217
af0dc7d3 1218 assert(ARRAY_SIZE(columns) == FINDMNT_NCOLUMNS);
9d67679b 1219
04fd7a9f
KZ
1220 setlocale(LC_ALL, "");
1221 bindtextdomain(PACKAGE, LOCALEDIR);
1222 textdomain(PACKAGE);
c05a80ca 1223 atexit(close_stdout);
04fd7a9f 1224
9d67679b 1225 /* default output format */
fdedb45e 1226 tt_flags |= TT_FL_TREE;
04fd7a9f 1227
9d67679b 1228 while ((c = getopt_long(argc, argv,
bebdda30 1229 "AacDd:ehifF:o:O:p::PklmnN:rst:uvRS:T:Uw:V",
d466c6a1 1230 longopts, NULL)) != -1) {
f1622b57
KZ
1231
1232 err_exclusive_options(c, longopts, excl, excl_st);
1233
04fd7a9f 1234 switch(c) {
cd4ed46f
KZ
1235 case 'A':
1236 flags |= FL_ALL;
1237 break;
9d67679b 1238 case 'a':
fdedb45e 1239 tt_flags |= TT_FL_ASCII;
9d67679b 1240 break;
04fd7a9f
KZ
1241 case 'c':
1242 flags |= FL_CANONICALIZE;
1243 break;
eda399b9
DR
1244 case 'D':
1245 tt_flags &= ~TT_FL_TREE;
cd4ed46f 1246 flags |= FL_DF;
eda399b9 1247 break;
04fd7a9f 1248 case 'd':
4e6bd74c 1249 if (!strcmp(optarg, "forward"))
04fd7a9f 1250 direction = MNT_ITER_FORWARD;
4e6bd74c 1251 else if (!strcmp(optarg, "backward"))
04fd7a9f
KZ
1252 direction = MNT_ITER_BACKWARD;
1253 else
1254 errx(EXIT_FAILURE,
8e350e48 1255 _("unknown direction '%s'"), optarg);
04fd7a9f
KZ
1256 break;
1257 case 'e':
1258 flags |= FL_EVALUATE;
1259 break;
1260 case 'h':
1261 usage(stdout);
1262 break;
1263 case 'i':
1264 flags |= FL_INVERT;
1265 break;
9d67679b 1266 case 'f':
04fd7a9f
KZ
1267 flags |= FL_FIRSTONLY;
1268 break;
2f1ac44b 1269 case 'F':
8c5eba3e 1270 tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg);
2f1ac44b 1271 break;
9d67679b 1272 case 'u':
32e5466a 1273 disable_columns_truncate();
9d67679b 1274 break;
04fd7a9f 1275 case 'o':
c57dca68 1276 outarg = optarg;
04fd7a9f
KZ
1277 break;
1278 case 'O':
9d67679b 1279 set_match(COL_OPTIONS, optarg);
04fd7a9f 1280 break;
32e5466a 1281 case 'p':
bdc3ed66 1282 if (optarg) {
c87638ad 1283 nactions = string_to_idarray(optarg,
bdc3ed66
KZ
1284 actions, ARRAY_SIZE(actions),
1285 poll_action_name_to_id);
1286 if (nactions < 0)
1287 exit(EXIT_FAILURE);
1288 }
32e5466a
KZ
1289 flags |= FL_POLL;
1290 tt_flags &= ~TT_FL_TREE;
1291 break;
49e9fd3a
KZ
1292 case 'P':
1293 tt_flags |= TT_FL_EXPORT;
1294 tt_flags &= ~TT_FL_TREE;
1295 break;
fdedb45e 1296 case 'm': /* mtab */
8c5eba3e 1297 tabtype = TABTYPE_MTAB;
fdedb45e 1298 tt_flags &= ~TT_FL_TREE;
04fd7a9f 1299 break;
fdedb45e 1300 case 's': /* fstab */
8c5eba3e 1301 tabtype = TABTYPE_FSTAB;
ac808156 1302 tt_flags &= ~TT_FL_TREE;
04fd7a9f 1303 break;
fdedb45e 1304 case 'k': /* kernel (mountinfo) */
8c5eba3e 1305 tabtype = TABTYPE_KERNEL;
04fd7a9f
KZ
1306 break;
1307 case 't':
9d67679b
KZ
1308 set_match(COL_FSTYPE, optarg);
1309 break;
1310 case 'r':
fdedb45e
KZ
1311 tt_flags &= ~TT_FL_TREE; /* disable the default */
1312 tt_flags |= TT_FL_RAW; /* enable raw */
9d67679b
KZ
1313 break;
1314 case 'l':
fdedb45e 1315 tt_flags &= ~TT_FL_TREE; /* disable the default */
9d67679b
KZ
1316 break;
1317 case 'n':
fdedb45e 1318 tt_flags |= TT_FL_NOHEADINGS;
9d67679b 1319 break;
c7fcc830 1320 case 'N':
c7fcc830
KZ
1321 tabtype = TABTYPE_KERNEL;
1322 tabfiles = append_pid_tabfile(tabfiles, &ntabfiles,
1323 strtou32_or_err(optarg,
1324 _("invalid TID argument")));
1325 break;
b2214e1f
KZ
1326 case 'v':
1327 flags |= FL_NOFSROOT;
1328 break;
049caefd
KZ
1329 case 'R':
1330 flags |= FL_SUBMOUNTS;
1331 break;
9d67679b 1332 case 'S':
cd492186 1333 set_source_match(optarg);
9d67679b
KZ
1334 flags |= FL_NOSWAPMATCH;
1335 break;
1336 case 'T':
1337 set_match(COL_TARGET, optarg);
1338 flags |= FL_NOSWAPMATCH;
04fd7a9f 1339 break;
bebdda30
KZ
1340 case 'U':
1341 flags |= FL_UNIQ;
1342 break;
ad38fb9f 1343 case 'w':
db41a429 1344 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
ad38fb9f 1345 break;
5cc12c40
SK
1346 case 'V':
1347 printf(UTIL_LINUX_VERSION);
1348 return EXIT_SUCCESS;
04fd7a9f
KZ
1349 default:
1350 usage(stderr);
1351 break;
1352 }
1353 }
1354
cd4ed46f 1355 if (!ncolumns && (flags & FL_DF)) {
eda399b9
DR
1356 columns[ncolumns++] = COL_SOURCE;
1357 columns[ncolumns++] = COL_FSTYPE;
1358 columns[ncolumns++] = COL_SIZE;
1359 columns[ncolumns++] = COL_USED;
1360 columns[ncolumns++] = COL_AVAIL;
1361 columns[ncolumns++] = COL_USEPERC;
1362 columns[ncolumns++] = COL_TARGET;
1363 }
1364
32e5466a
KZ
1365 /* default columns */
1366 if (!ncolumns) {
1367 if (flags & FL_POLL)
1368 columns[ncolumns++] = COL_ACTION;
1369
1370 columns[ncolumns++] = COL_TARGET;
1371 columns[ncolumns++] = COL_SOURCE;
1372 columns[ncolumns++] = COL_FSTYPE;
1373 columns[ncolumns++] = COL_OPTIONS;
1374 }
1375
c57dca68
MB
1376 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
1377 &ncolumns, column_name_to_id) < 0)
1378 return EXIT_FAILURE;
1379
8c5eba3e
KZ
1380 if (!tabtype)
1381 tabtype = TABTYPE_KERNEL;
2f1ac44b 1382
f1622b57
KZ
1383 if ((flags & FL_POLL) && ntabfiles > 1)
1384 errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file"));
b2214e1f 1385
9d67679b
KZ
1386 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
1387 errx(EXIT_FAILURE, _(
1388 "options --target and --source can't be used together "
1389 "with command line element that is not an option"));
1390
04fd7a9f 1391 if (optind < argc)
cd492186 1392 set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */
04fd7a9f 1393 if (optind < argc)
9d67679b 1394 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
04fd7a9f 1395
049caefd
KZ
1396 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
1397 /* don't care about submounts if list all mounts */
1398 flags &= ~FL_SUBMOUNTS;
1399
31f67453
KZ
1400 if (!(flags & FL_SUBMOUNTS) && ((flags & FL_FIRSTONLY)
1401 || get_match(COL_TARGET)
1402 || get_match(COL_SOURCE)
1403 || get_match(COL_MAJMIN)))
fdedb45e
KZ
1404 tt_flags &= ~TT_FL_TREE;
1405
ac808156
KZ
1406 if (!(flags & FL_NOSWAPMATCH) &&
1407 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1408 /*
1409 * Check if we can swap source and target, it's
1410 * not possible if the source is LABEL=/UUID=
1411 */
1412 const char *x = get_match(COL_SOURCE);
1413
090b5e84
KZ
1414 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) ||
1415 !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9))
ac808156
KZ
1416 flags |= FL_NOSWAPMATCH;
1417 }
1418
fdedb45e
KZ
1419 /*
1420 * initialize libmount
1421 */
ac808156
KZ
1422 mnt_init_debug(0);
1423
8c5eba3e 1424 tb = parse_tabfiles(tabfiles, ntabfiles, tabtype);
04fd7a9f 1425 if (!tb)
fdedb45e 1426 goto leave;
04fd7a9f 1427
aca72ac2 1428 if ((tt_flags & TT_FL_TREE) && (ntabfiles > 1 || !tab_is_tree(tb)))
2f1ac44b
KZ
1429 tt_flags &= ~TT_FL_TREE;
1430
04fd7a9f 1431 cache = mnt_new_cache();
fdedb45e
KZ
1432 if (!cache) {
1433 warn(_("failed to initialize libmount cache"));
1434 goto leave;
1435 }
68164f6c 1436 mnt_table_set_cache(tb, cache);
04fd7a9f 1437
bebdda30
KZ
1438 if (flags & FL_UNIQ)
1439 mnt_table_uniq_fs(tb, MNT_UNIQ_KEEPTREE, uniq_fs_target_cmp);
4bfd4bff 1440
fdedb45e 1441 /*
00b4bcdf 1442 * initialize output formatting (tt.h)
fdedb45e 1443 */
b3386c83 1444 tt = tt_new_table(tt_flags | TT_FL_FREEDATA);
fdedb45e
KZ
1445 if (!tt) {
1446 warn(_("failed to initialize output table"));
1447 goto leave;
1448 }
ac808156 1449
fdedb45e 1450 for (i = 0; i < ncolumns; i++) {
32e5466a
KZ
1451 int fl = get_column_flags(i);
1452 int id = get_column_id(i);
1453
1454 if (!(tt_flags & TT_FL_TREE))
1455 fl &= ~TT_FL_TREE;
04fd7a9f 1456
32e5466a 1457 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
77a1c5f7 1458 warnx(_("%s column is requested, but --poll "
32e5466a
KZ
1459 "is not enabled"), get_column_name(i));
1460 goto leave;
1461 }
fdedb45e
KZ
1462 if (!tt_define_column(tt, get_column_name(i),
1463 get_column_whint(i), fl)) {
1464 warn(_("failed to initialize output column"));
1465 goto leave;
9d67679b 1466 }
fdedb45e 1467 }
9d67679b 1468
fdedb45e
KZ
1469 /*
1470 * Fill in data to the output table
1471 */
275c9a48 1472 if (flags & FL_POLL) {
8c5eba3e 1473 /* poll mode (accept the first tabfile only) */
b8a670ae 1474 rc = poll_table(tb, tabfiles ? *tabfiles : _PATH_PROC_MOUNTINFO, timeout, tt, direction);
32e5466a 1475
31f67453 1476 } else if ((tt_flags & TT_FL_TREE) && !(flags & FL_SUBMOUNTS)) {
049caefd
KZ
1477 /* whole tree */
1478 rc = create_treenode(tt, tb, NULL, NULL);
31f67453 1479 } else {
049caefd
KZ
1480 /* whole lits of sub-tree */
1481 rc = add_matching_lines(tb, tt, direction);
fdedb45e 1482
3e373967
KZ
1483 if (rc != 0
1484 && tabtype == TABTYPE_KERNEL
1485 && (flags & FL_NOSWAPMATCH)
1486 && get_match(COL_TARGET)) {
1487 /*
1488 * Found nothing, maybe the --target is regular file,
1489 * try it again with extra functionality for target
1490 * match
1491 */
1492 enable_extra_target_match();
1493 rc = add_matching_lines(tb, tt, direction);
1494 }
1495 }
1496
fdedb45e 1497 /*
32e5466a 1498 * Print the output table for non-poll modes
fdedb45e 1499 */
32e5466a 1500 if (!rc && !(flags & FL_POLL))
049caefd 1501 tt_print_table(tt);
fdedb45e
KZ
1502leave:
1503 tt_free_table(tt);
1504
50fccba1 1505 mnt_unref_table(tb);
6195f9e6 1506 mnt_unref_cache(cache);
50fccba1 1507
8c5eba3e 1508 free(tabfiles);
e346233e
KZ
1509#ifdef HAVE_LIBUDEV
1510 udev_unref(udev);
1511#endif
049caefd 1512 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
04fd7a9f 1513}