]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/findmnt.c
Merge branch 'sed' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / misc-utils / findmnt.c
1 /*
2 * findmnt(8)
3 *
4 * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved.
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 *
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.
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <getopt.h>
27 #include <string.h>
28 #include <termios.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
31 #endif
32 #include <assert.h>
33 #include <poll.h>
34 #include <sys/statvfs.h>
35 #include <sys/types.h>
36 #ifdef HAVE_LIBUDEV
37 # include <libudev.h>
38 #endif
39 #include <blkid.h>
40 #include <libmount.h>
41 #include <libsmartcols.h>
42
43 #include "pathnames.h"
44 #include "nls.h"
45 #include "closestream.h"
46 #include "c.h"
47 #include "strutils.h"
48 #include "xalloc.h"
49 #include "optutils.h"
50 #include "mangle.h"
51 #include "buffer.h"
52 #include "column-list-table.h"
53
54 #include "findmnt.h"
55
56 /* column IDs */
57 enum {
58 COL_ACTION,
59 COL_AVAIL,
60 COL_FREQ,
61 COL_FSROOT,
62 COL_FSTYPE,
63 COL_FS_OPTIONS,
64 COL_ID,
65 COL_INO_AVAIL,
66 COL_INO_TOTAL,
67 COL_INO_USED,
68 COL_INO_USEPERC,
69 COL_LABEL,
70 COL_MAJMIN,
71 COL_OLD_OPTIONS,
72 COL_OLD_TARGET,
73 COL_OPTIONS,
74 COL_OPT_FIELDS,
75 COL_PARENT,
76 COL_PARTLABEL,
77 COL_PARTUUID,
78 COL_PASSNO,
79 COL_PROPAGATION,
80 COL_SIZE,
81 COL_SOURCE,
82 COL_SOURCES,
83 COL_TARGET,
84 COL_TID,
85 COL_USED,
86 COL_USEPERC,
87 COL_UUID,
88 COL_VFS_OPTIONS
89 };
90
91 enum {
92 TABTYPE_FSTAB = 1,
93 TABTYPE_MTAB,
94 TABTYPE_KERNEL
95 };
96
97 /* column names */
98 struct colinfo {
99 const char * const name; /* header */
100 double whint; /* width hint (N < 1 is in percent of termwidth) */
101 int flags; /* libsmartcols flags */
102 const char *help; /* column description */
103 const char *match; /* pattern for match_func() */
104 void *match_data; /* match specific data */
105 };
106
107 /* columns descriptions (don't use const, this is writable) */
108 static struct colinfo infos[] = {
109 [COL_ACTION] = { "ACTION", 10, SCOLS_FL_STRICTWIDTH, N_("action detected by --poll") },
110 [COL_AVAIL] = { "AVAIL", 5, SCOLS_FL_RIGHT, N_("filesystem size available, use <number> if --bytes is given") },
111 [COL_FREQ] = { "FREQ", 1, SCOLS_FL_RIGHT, N_("dump(8) period in days [fstab only]") },
112 [COL_FSROOT] = { "FSROOT", 0.25, SCOLS_FL_NOEXTREMES, N_("filesystem root") },
113 [COL_FSTYPE] = { "FSTYPE", 0.10, SCOLS_FL_TRUNC, N_("filesystem type") },
114 [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("FS specific mount options") },
115 [COL_ID] = { "ID", 2, SCOLS_FL_RIGHT, N_("mount ID") },
116 [COL_INO_AVAIL] = { "INO.AVAIL", 5, SCOLS_FL_RIGHT, N_("number of available inodes") },
117 [COL_INO_TOTAL] = { "INO.TOTAL", 5, SCOLS_FL_RIGHT, N_("total number of inodes") },
118 [COL_INO_USED] = { "INO.USED", 5, SCOLS_FL_RIGHT, N_("number of used inodes") },
119 [COL_INO_USEPERC] = { "INO.USE%", 3, SCOLS_FL_RIGHT, N_("percentage of INO.USED divided by INO.TOTAL") },
120 [COL_LABEL] = { "LABEL", 0.10, 0, N_("filesystem label") },
121 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
122 [COL_OLD_OPTIONS] = { "OLD-OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("old mount options saved by --poll") },
123 [COL_OLD_TARGET] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
124 [COL_OPTIONS] = { "OPTIONS", 0.10, SCOLS_FL_TRUNC, N_("all mount options") },
125 [COL_OPT_FIELDS] = { "OPT-FIELDS", 0.10, SCOLS_FL_TRUNC, N_("optional mount fields") },
126 [COL_PARENT] = { "PARENT", 2, SCOLS_FL_RIGHT, N_("mount parent ID") },
127 [COL_PARTLABEL] = { "PARTLABEL", 0.10, 0, N_("partition label") },
128 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
129 [COL_PASSNO] = { "PASSNO", 1, SCOLS_FL_RIGHT, N_("pass number on parallel fsck(8) [fstab only]") },
130 [COL_PROPAGATION] = { "PROPAGATION", 0.10, 0, N_("VFS propagation flags") },
131 [COL_SIZE] = { "SIZE", 5, SCOLS_FL_RIGHT, N_("filesystem size, use <number> if --bytes is given") },
132 [COL_SOURCES] = { "SOURCES", 0.25, SCOLS_FL_WRAP, N_("all possible source devices") },
133 [COL_SOURCE] = { "SOURCE", 0.25, SCOLS_FL_NOEXTREMES, N_("source device") },
134 [COL_TARGET] = { "TARGET", 0.30, SCOLS_FL_TREE| SCOLS_FL_NOEXTREMES, N_("mountpoint") },
135 [COL_TID] = { "TID", 4, SCOLS_FL_RIGHT, N_("task ID") },
136 [COL_USED] = { "USED", 5, SCOLS_FL_RIGHT, N_("filesystem size used, use <number> if --bytes is given") },
137 [COL_USEPERC] = { "USE%", 3, SCOLS_FL_RIGHT, N_("filesystem use percentage") },
138 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
139 [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, SCOLS_FL_TRUNC, N_("VFS specific mount options") }
140 };
141
142 /* columns[] array specifies all currently wanted output column. The columns
143 * are defined by infos[] array and you can specify (on command line) each
144 * column twice. That's enough, dynamically allocated array of the columns is
145 * unnecessary overkill and over-engineering in this case */
146 static int columns[ARRAY_SIZE(infos) * 2];
147 static size_t ncolumns;
148
149 static inline size_t err_columns_index(size_t arysz, size_t idx)
150 {
151 if (idx >= arysz)
152 errx(EXIT_FAILURE, _("too many columns specified, "
153 "the limit is %zu columns"),
154 arysz - 1);
155 return idx;
156 }
157
158 #define add_column(ary, n, id) \
159 ((ary)[ err_columns_index(ARRAY_SIZE(ary), (n)) ] = (id))
160
161 /* poll actions (parsed --poll=<list> */
162 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
163 static int actions[FINDMNT_NACTIONS];
164 static int nactions;
165
166 /* global (accessed from findmnt-verify.c too) */
167 unsigned int flags;
168 int parse_nerrors;
169 struct libmnt_cache *cache;
170
171 static blkid_cache blk_cache;
172
173 #ifdef HAVE_LIBUDEV
174 static struct udev *udev;
175 #endif
176
177 static int match_func(struct libmnt_fs *fs, void *data __attribute__ ((__unused__)));
178
179
180 static int get_column_id(int num)
181 {
182 assert(num >= 0);
183 assert((size_t) num < ncolumns);
184 assert((size_t) columns[num] < ARRAY_SIZE(infos));
185 return columns[num];
186 }
187
188 static struct colinfo *get_column_info(int num)
189 {
190 return &infos[ get_column_id(num) ];
191 }
192
193 static const char *column_id_to_name(int id)
194 {
195 assert((size_t) id < ARRAY_SIZE(infos));
196 return infos[id].name;
197 }
198
199 static const char *get_column_name(int num)
200 {
201 return get_column_info(num)->name;
202 }
203
204 static float get_column_whint(int num)
205 {
206 return get_column_info(num)->whint;
207 }
208
209 static int get_column_flags(int num)
210 {
211 return get_column_info(num)->flags;
212 }
213
214 static const char *get_match(int id)
215 {
216 assert((size_t) id < ARRAY_SIZE(infos));
217 return infos[id].match;
218 }
219
220 static void *get_match_data(int id)
221 {
222 assert((size_t) id < ARRAY_SIZE(infos));
223 return infos[id].match_data;
224 }
225
226 static void set_match(int id, const char *match)
227 {
228 assert((size_t) id < ARRAY_SIZE(infos));
229 infos[id].match = match;
230 }
231
232 static void set_match_data(int id, void *data)
233 {
234 assert((size_t) id < ARRAY_SIZE(infos));
235 infos[id].match_data = data;
236 }
237
238 /*
239 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
240 * data format.
241 */
242 static void set_source_match(const char *data)
243 {
244 int maj, min;
245
246 if (sscanf(data, "%d:%d", &maj, &min) == 2) {
247 dev_t *devno = xmalloc(sizeof(dev_t));
248
249 *devno = makedev(maj, min);
250 set_match(COL_MAJMIN, data);
251 set_match_data(COL_MAJMIN, (void *) devno);
252 flags |= FL_NOSWAPMATCH;
253 } else
254 set_match(COL_SOURCE, data);
255 }
256
257 /*
258 * Extra functionality for --target <path>. The function mnt_table_find_mountpoint()
259 * also checks parents (path elements in reverse order) to get mountpoint.
260 *
261 * @tb has to be from kernel (so no fstab or so)!
262 */
263 static void enable_extra_target_match(struct libmnt_table *tb)
264 {
265 char *cn = NULL;
266 const char *tgt = NULL, *mnt = NULL;
267 struct libmnt_fs *fs;
268
269 /*
270 * Check if match pattern is mountpoint, if not use the
271 * real mountpoint.
272 */
273 if (flags & FL_NOCACHE)
274 tgt = get_match(COL_TARGET);
275 else {
276 tgt = cn = mnt_resolve_path(get_match(COL_TARGET), cache);
277 if (!cn)
278 return;
279 }
280
281 fs = mnt_table_find_mountpoint(tb, tgt, MNT_ITER_BACKWARD);
282 if (fs)
283 mnt = mnt_fs_get_target(fs);
284 if (mnt && strcmp(mnt, tgt) != 0)
285 set_match(COL_TARGET, xstrdup(mnt)); /* replace the current setting */
286
287 if (!cache)
288 free(cn);
289 }
290
291
292 static int is_tabdiff_column(int id)
293 {
294 assert((size_t) id < ARRAY_SIZE(infos));
295
296 switch(id) {
297 case COL_ACTION:
298 case COL_OLD_TARGET:
299 case COL_OLD_OPTIONS:
300 return 1;
301 default:
302 break;
303 }
304 return 0;
305 }
306
307 /*
308 * "findmnt" without any filter
309 */
310 int is_listall_mode(void)
311 {
312 if ((flags & FL_DF || flags & FL_REAL || flags & FL_PSEUDO) && !(flags & FL_ALL))
313 return 0;
314
315 return (!get_match(COL_SOURCE) &&
316 !get_match(COL_TARGET) &&
317 !get_match(COL_FSTYPE) &&
318 !get_match(COL_OPTIONS) &&
319 !get_match(COL_MAJMIN));
320 }
321
322 /*
323 * Returns 1 if the @act is in the --poll=<list>
324 */
325 static int has_poll_action(int act)
326 {
327 int i;
328
329 if (!nactions)
330 return 1; /* all actions enabled */
331 for (i = 0; i < nactions; i++)
332 if (actions[i] == act)
333 return 1;
334 return 0;
335 }
336
337 static int poll_action_name_to_id(const char *name, size_t namesz)
338 {
339 int id = -1;
340
341 if (strncasecmp(name, "move", namesz) == 0 && namesz == 4)
342 id = MNT_TABDIFF_MOVE;
343 else if (strncasecmp(name, "mount", namesz) == 0 && namesz == 5)
344 id = MNT_TABDIFF_MOUNT;
345 else if (strncasecmp(name, "umount", namesz) == 0 && namesz == 6)
346 id = MNT_TABDIFF_UMOUNT;
347 else if (strncasecmp(name, "remount", namesz) == 0 && namesz == 7)
348 id = MNT_TABDIFF_REMOUNT;
349 else
350 warnx(_("unknown action: %s"), name);
351
352 return id;
353 }
354
355 /*
356 * findmnt --first-only <devname|TAG=|mountpoint>
357 *
358 * ... it works like "mount <devname|TAG=|mountpoint>"
359 */
360 static int is_mount_compatible_mode(void)
361 {
362 if (!get_match(COL_SOURCE))
363 return 0; /* <devname|TAG=|mountpoint> is required */
364 if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS))
365 return 0; /* cannot be restricted by -t or -O */
366 if (!(flags & FL_FIRSTONLY))
367 return 0; /* we have to return the first entry only */
368
369 return 1; /* ok */
370 }
371
372 static void disable_columns_truncate(void)
373 {
374 size_t i;
375
376 for (i = 0; i < ARRAY_SIZE(infos); i++)
377 infos[i].flags &= ~SCOLS_FL_TRUNC;
378 }
379
380 /*
381 * converts @name to column ID
382 */
383 static int column_name_to_id(const char *name, size_t namesz)
384 {
385 size_t i;
386
387 for (i = 0; i < ARRAY_SIZE(infos); i++) {
388 const char *cn = column_id_to_name(i);
389
390 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
391 return i;
392 }
393 warnx(_("unknown column: %s"), name);
394 return -1;
395 }
396
397
398 #ifdef HAVE_LIBUDEV
399 static char *get_tag_from_udev(const char *devname, int col)
400 {
401 struct udev_device *dev;
402 const char *data = NULL;
403 char *res = NULL, *path;
404
405 if (!udev)
406 udev = udev_new();
407 if (!udev)
408 return NULL;
409
410 /* libudev don't like /dev/mapper/ symlinks */
411 path = realpath(devname, NULL);
412 if (path)
413 devname = path;
414
415 if (strncmp(devname, "/dev/", 5) == 0)
416 devname += 5;
417
418 dev = udev_device_new_from_subsystem_sysname(udev, "block", devname);
419 free(path);
420
421 if (!dev)
422 return NULL;
423
424 switch(col) {
425 case COL_LABEL:
426 data = udev_device_get_property_value(dev, "ID_FS_LABEL_ENC");
427 break;
428 case COL_UUID:
429 data = udev_device_get_property_value(dev, "ID_FS_UUID_ENC");
430 break;
431 case COL_PARTUUID:
432 data = udev_device_get_property_value(dev, "ID_PART_ENTRY_UUID");
433 break;
434 case COL_PARTLABEL:
435 data = udev_device_get_property_value(dev, "ID_PART_ENTRY_NAME");
436 break;
437 default:
438 break;
439 }
440
441 if (data) {
442 res = xstrdup(data);
443 unhexmangle_string(res);
444 }
445
446 udev_device_unref(dev);
447 return res;
448 }
449 #endif /* HAVE_LIBUDEV */
450
451 /* Returns LABEL or UUID */
452 static char *get_tag(struct libmnt_fs *fs, const char *tagname, int col
453 #ifndef HAVE_LIBUDEV
454 __attribute__((__unused__))
455 #endif
456 )
457 {
458 const char *t, *v;
459 char *res = NULL;
460
461 if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname))
462 res = xstrdup(v);
463 else {
464 const char *dev = mnt_fs_get_source(fs);
465
466 if (dev && !(flags & FL_NOCACHE))
467 dev = mnt_resolve_spec(dev, cache);
468 #ifdef HAVE_LIBUDEV
469 if (dev)
470 res = get_tag_from_udev(dev, col);
471 #endif
472 if (!res) {
473 res = mnt_cache_find_tag_value(cache, dev, tagname);
474 if (res && cache)
475 /* don't return pointer to cache */
476 res = xstrdup(res);
477 }
478 }
479
480 return res;
481 }
482
483 static char *get_vfs_attr(struct libmnt_fs *fs, int sizetype)
484 {
485 struct statvfs buf;
486 uint64_t vfs_attr = 0;
487 char *sizestr;
488 bool no_bytes = false;
489
490 if (statvfs(mnt_fs_get_target(fs), &buf) != 0)
491 return NULL;
492
493 switch(sizetype) {
494 case COL_SIZE:
495 vfs_attr = buf.f_frsize * buf.f_blocks;
496 break;
497 case COL_AVAIL:
498 vfs_attr = buf.f_frsize * buf.f_bavail;
499 break;
500 case COL_USED:
501 vfs_attr = buf.f_frsize * (buf.f_blocks - buf.f_bfree);
502 break;
503 case COL_USEPERC:
504 if (buf.f_blocks == 0)
505 return xstrdup("-");
506
507 xasprintf(&sizestr, "%.0f%%",
508 (double)(buf.f_blocks - buf.f_bfree) /
509 buf.f_blocks * 100);
510 return sizestr;
511 case COL_INO_AVAIL:
512 /* Quoted from startvfs(3):
513 *
514 * Under Linux, f_favail is always the same as
515 * f_ffree, and there's no way for a filesystem to
516 * report otherwise. This is not an issue, since no
517 * filesystems with an inode root reservation exist.
518 */
519 no_bytes = true;
520 vfs_attr = buf.f_ffree;
521 break;
522 case COL_INO_TOTAL:
523 no_bytes = true;
524 vfs_attr = buf.f_files;
525 break;
526 case COL_INO_USED:
527 no_bytes = true;
528 vfs_attr = buf.f_files - buf.f_ffree;
529 break;
530 case COL_INO_USEPERC:
531 if (buf.f_files == 0)
532 return xstrdup("-");
533
534 xasprintf(&sizestr, "%.0f%%",
535 (double)(buf.f_files - buf.f_ffree) /
536 buf.f_files * 100);
537 return sizestr;
538 }
539
540 if (!vfs_attr)
541 sizestr = xstrdup("0");
542 else if ((flags & FL_BYTES) || no_bytes)
543 xasprintf(&sizestr, "%ju", vfs_attr);
544 else
545 sizestr = size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
546
547 return sizestr;
548 }
549
550 /* reads sources from libmount/libblkid
551 */
552 static char *get_data_col_sources(struct libmnt_fs *fs, int evaluate, size_t *datasiz)
553 {
554 const char *tag = NULL, *p = NULL;
555 int i = 0;
556 const char *device = NULL;
557 char *val = NULL;
558 blkid_dev_iterate iter;
559 blkid_dev dev;
560 struct ul_buffer buf = UL_INIT_BUFFER;
561
562 /* get TAG from libmount if avalable (e.g. fstab) */
563 if (mnt_fs_get_tag(fs, &tag, &p) == 0) {
564
565 /* if device is in the form 'UUID=..' or 'LABEL=..' and evaluate==0
566 * then it is ok to do not search for multiple devices
567 */
568 if (!evaluate)
569 goto nothing;
570 val = xstrdup(p);
571 }
572
573 /* or get device name */
574 else if (!(device = mnt_fs_get_srcpath(fs))
575 || strncmp(device, "/dev/", 5) != 0)
576 goto nothing;
577
578 if (!blk_cache) {
579 if (blkid_get_cache(&blk_cache, NULL) != 0)
580 goto nothing;
581 blkid_probe_all(blk_cache);
582 }
583
584 /* ask libblkid for the UUID */
585 if (!val) {
586 assert(device);
587
588 tag = "UUID";
589 val = blkid_get_tag_value(blk_cache, "UUID", device); /* returns allocated string */
590 if (!val)
591 goto nothing;
592 }
593
594 assert(val);
595 assert(tag);
596
597 /* scan all devices for the TAG */
598 iter = blkid_dev_iterate_begin(blk_cache);
599 blkid_dev_set_search(iter, tag, val);
600
601 while (blkid_dev_next(iter, &dev) == 0) {
602 dev = blkid_verify(blk_cache, dev);
603 if (!dev)
604 continue;
605 if (i != 0)
606 ul_buffer_append_data(&buf, "\0", 1);
607 ul_buffer_append_string(&buf, blkid_dev_devname(dev));
608 i++;
609 }
610 blkid_dev_iterate_end(iter);
611 free(val);
612
613 return ul_buffer_get_data(&buf, datasiz, NULL);
614
615 nothing:
616 free(val);
617 return NULL;
618 }
619
620 /* reads FS data from libmount
621 */
622 static char *get_data(struct libmnt_fs *fs, int num, size_t *datasiz)
623 {
624 char *str = NULL;
625 int col_id = get_column_id(num);
626
627 switch (col_id) {
628 case COL_SOURCES:
629 /* print all devices with the same tag (LABEL, UUID) */
630 str = get_data_col_sources(fs, flags & FL_EVALUATE, datasiz);
631 if (str)
632 break;
633
634 /* fallthrough */
635 case COL_SOURCE:
636 {
637 const char *root = mnt_fs_get_root(fs);
638 const char *spec = mnt_fs_get_srcpath(fs);
639 char *cn = NULL;
640
641 if (spec && (flags & FL_CANONICALIZE))
642 spec = cn = mnt_resolve_path(spec, cache);
643 if (!spec) {
644 spec = mnt_fs_get_source(fs);
645
646 if (spec && (flags & FL_EVALUATE))
647 spec = cn = mnt_resolve_spec(spec, cache);
648 }
649 if (root && spec && !(flags & FL_NOFSROOT) && strcmp(root, "/") != 0)
650 xasprintf(&str, "%s[%s]", spec, root);
651 else if (spec)
652 str = xstrdup(spec);
653 if (!cache)
654 free(cn);
655 break;
656 }
657
658 case COL_TARGET:
659 if (mnt_fs_get_target(fs))
660 str = xstrdup(mnt_fs_get_target(fs));
661 break;
662 case COL_FSTYPE:
663 if (mnt_fs_get_fstype(fs))
664 str = xstrdup(mnt_fs_get_fstype(fs));
665 break;
666 case COL_OPTIONS:
667 if (mnt_fs_get_options(fs))
668 str = xstrdup(mnt_fs_get_options(fs));
669 break;
670 case COL_VFS_OPTIONS:
671 if (flags & FL_VFS_ALL)
672 str = mnt_fs_get_vfs_options_all(fs);
673 else if (mnt_fs_get_vfs_options(fs))
674 str = xstrdup(mnt_fs_get_vfs_options(fs));
675 break;
676 case COL_FS_OPTIONS:
677 if (mnt_fs_get_fs_options(fs))
678 str = xstrdup(mnt_fs_get_fs_options(fs));
679 break;
680 case COL_OPT_FIELDS:
681 if (mnt_fs_get_optional_fields(fs))
682 str = xstrdup(mnt_fs_get_optional_fields(fs));
683 break;
684 case COL_UUID:
685 str = get_tag(fs, "UUID", col_id);
686 break;
687 case COL_PARTUUID:
688 str = get_tag(fs, "PARTUUID", col_id);
689 break;
690 case COL_LABEL:
691 str = get_tag(fs, "LABEL", col_id);
692 break;
693 case COL_PARTLABEL:
694 str = get_tag(fs, "PARTLABEL", col_id);
695 break;
696
697 case COL_MAJMIN:
698 {
699 dev_t devno = mnt_fs_get_devno(fs);
700 if (!devno)
701 break;
702
703 if ((flags & FL_RAW) || (flags & FL_EXPORT) || (flags & FL_JSON))
704 xasprintf(&str, "%u:%u", major(devno), minor(devno));
705 else
706 xasprintf(&str, "%3u:%-3u", major(devno), minor(devno));
707 break;
708 }
709 case COL_SIZE:
710 case COL_AVAIL:
711 case COL_USED:
712 case COL_USEPERC:
713 case COL_INO_TOTAL:
714 case COL_INO_AVAIL:
715 case COL_INO_USED:
716 case COL_INO_USEPERC:
717 str = get_vfs_attr(fs, col_id);
718 break;
719 case COL_FSROOT:
720 if (mnt_fs_get_root(fs))
721 str = xstrdup(mnt_fs_get_root(fs));
722 break;
723 case COL_TID:
724 if (mnt_fs_get_tid(fs))
725 xasprintf(&str, "%d", mnt_fs_get_tid(fs));
726 break;
727 case COL_ID:
728 if (mnt_fs_get_id(fs))
729 xasprintf(&str, "%d", mnt_fs_get_id(fs));
730 break;
731 case COL_PARENT:
732 if (mnt_fs_get_parent_id(fs))
733 xasprintf(&str, "%d", mnt_fs_get_parent_id(fs));
734 break;
735 case COL_PROPAGATION:
736 if (mnt_fs_is_kernel(fs)) {
737 unsigned long fl = 0;
738 char *n = NULL;
739
740 if (mnt_fs_get_propagation(fs, &fl) != 0)
741 break;
742
743 n = xstrdup((fl & MS_SHARED) ? "shared" : "private");
744
745 if (fl & MS_SLAVE) {
746 xasprintf(&str, "%s,slave", n);
747 free(n);
748 n = str;
749 }
750 if (fl & MS_UNBINDABLE) {
751 xasprintf(&str, "%s,unbindable", n);
752 free(n);
753 n = str;
754 }
755 str = n;
756 }
757 break;
758 case COL_FREQ:
759 if (!mnt_fs_is_kernel(fs))
760 xasprintf(&str, "%d", mnt_fs_get_freq(fs));
761 break;
762 case COL_PASSNO:
763 if (!mnt_fs_is_kernel(fs))
764 xasprintf(&str, "%d", mnt_fs_get_passno(fs));
765 break;
766 default:
767 break;
768 }
769 return str;
770 }
771
772 static char *get_tabdiff_data(struct libmnt_fs *old_fs,
773 struct libmnt_fs *new_fs,
774 int change,
775 int num,
776 size_t *datasiz)
777 {
778 char *str = NULL;
779
780 switch (get_column_id(num)) {
781 case COL_ACTION:
782 switch (change) {
783 case MNT_TABDIFF_MOUNT:
784 str = _("mount");
785 break;
786 case MNT_TABDIFF_UMOUNT:
787 str = _("umount");
788 break;
789 case MNT_TABDIFF_REMOUNT:
790 str = _("remount");
791 break;
792 case MNT_TABDIFF_MOVE:
793 str = _("move");
794 break;
795 default:
796 str = _("unknown");
797 break;
798 }
799 str = xstrdup(str);
800 break;
801 case COL_OLD_OPTIONS:
802 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
803 change == MNT_TABDIFF_UMOUNT)
804 && mnt_fs_get_options(old_fs))
805 str = xstrdup(mnt_fs_get_options(old_fs));
806 break;
807 case COL_OLD_TARGET:
808 if (old_fs && (change == MNT_TABDIFF_MOVE ||
809 change == MNT_TABDIFF_UMOUNT)
810 && mnt_fs_get_target(old_fs))
811 str = xstrdup(mnt_fs_get_target(old_fs));
812 break;
813 default:
814 if (new_fs)
815 str = get_data(new_fs, num, datasiz);
816 else
817 str = get_data(old_fs, num, datasiz);
818 break;
819 }
820 return str;
821 }
822
823 static void set_line_data(struct libscols_line *ln, size_t i, char *data, size_t datasiz)
824 {
825 int rc;
826 struct libscols_cell *ce;
827
828 ce = scols_line_get_cell(ln, i);
829 if (!ce)
830 return;
831 if (datasiz)
832 rc = scols_cell_refer_memory(ce, data, datasiz);
833 else
834 rc = scols_cell_refer_data(ce, data);
835 if (rc)
836 err(EXIT_FAILURE, _("failed to add output data"));
837 }
838
839 /* adds one line to the output @tab */
840 static struct libscols_line *add_line(struct libscols_table *table, struct libmnt_fs *fs,
841 struct libscols_line *parent)
842 {
843 size_t i;
844 struct libscols_line *line = scols_table_new_line(table, parent);
845
846 if (!line)
847 err(EXIT_FAILURE, _("failed to allocate output line"));
848
849 for (i = 0; i < ncolumns; i++) {
850 size_t datasiz = 0;
851 char *data = get_data(fs, i, &datasiz);
852
853 if (data)
854 set_line_data(line, i, data, datasiz);
855 }
856
857 scols_line_set_userdata(line, fs);
858 return line;
859 }
860
861 static struct libscols_line *add_tabdiff_line(struct libscols_table *table, struct libmnt_fs *new_fs,
862 struct libmnt_fs *old_fs, int change)
863 {
864 size_t i;
865 struct libscols_line *line = scols_table_new_line(table, NULL);
866
867 if (!line)
868 err(EXIT_FAILURE, _("failed to allocate output line"));
869
870 for (i = 0; i < ncolumns; i++) {
871 size_t datasiz = 0;
872 char *data = get_tabdiff_data(old_fs, new_fs, change, i, &datasiz);
873
874 if (data)
875 set_line_data(line, i, data, datasiz);
876 }
877
878 return line;
879 }
880
881 static int has_line(struct libscols_table *table, struct libmnt_fs *fs)
882 {
883 struct libscols_line *ln;
884 struct libscols_iter *itr;
885 int rc = 0;
886
887 itr = scols_new_iter(SCOLS_ITER_FORWARD);
888 if (!itr)
889 return 0;
890
891 while (scols_table_next_line(table, itr, &ln) == 0) {
892 if ((struct libmnt_fs *) scols_line_get_userdata(ln) == fs) {
893 rc = 1;
894 break;
895 }
896 }
897
898 scols_free_iter(itr);
899 return rc;
900 }
901
902 /* reads filesystems from @tb (libmount) and fillin @table (output table) */
903 static int create_treenode(struct libscols_table *table, struct libmnt_table *tb,
904 struct libmnt_fs *fs, struct libscols_line *parent_line)
905 {
906 struct libmnt_fs *chld = NULL;
907 struct libmnt_iter *itr = NULL;
908 struct libscols_line *line;
909 int rc = -1, first = 0;
910
911 if (!fs) {
912 /* first call, get root FS */
913 if (mnt_table_get_root_fs(tb, &fs))
914 goto leave;
915 parent_line = NULL;
916 first = 1;
917
918 } else if ((flags & FL_SUBMOUNTS) && has_line(table, fs))
919 return 0;
920
921 itr = mnt_new_iter(MNT_ITER_FORWARD);
922 if (!itr)
923 goto leave;
924
925 if ((flags & FL_SUBMOUNTS) || match_func(fs, NULL)) {
926 line = add_line(table, fs, parent_line);
927 if (!line)
928 goto leave;
929 } else
930 line = parent_line;
931
932 /*
933 * add all children to the output table
934 */
935 while (mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
936 if (create_treenode(table, tb, chld, line))
937 goto leave;
938 }
939 rc = 0;
940
941 /* make sure all entries are in the tree */
942 if (first && (size_t) mnt_table_get_nents(tb) >
943 (size_t) scols_table_get_nlines(table)) {
944 mnt_reset_iter(itr, MNT_ITER_FORWARD);
945 fs = NULL;
946
947 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
948 if (!has_line(table, fs) && match_func(fs, NULL))
949 create_treenode(table, tb, fs, NULL);
950 }
951 }
952 leave:
953 mnt_free_iter(itr);
954 return rc;
955 }
956
957 /* error callback */
958 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
959 const char *filename, int line)
960 {
961 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
962 ++parse_nerrors;
963 return 1;
964 }
965
966 static char **append_tabfile(char **files, int *nfiles, char *filename)
967 {
968 files = xreallocarray(files, *nfiles + 1, sizeof(char *));
969 files[(*nfiles)++] = filename;
970 return files;
971 }
972
973 static char **append_pid_tabfile(char **files, int *nfiles, pid_t pid)
974 {
975 char *path = NULL;
976
977 xasprintf(&path, "/proc/%d/mountinfo", (int) pid);
978 return append_tabfile(files, nfiles, path);
979 }
980
981 /* calls libmount fstab/mtab/mountinfo parser */
982 static struct libmnt_table *parse_tabfiles(char **files,
983 int nfiles,
984 int tabtype)
985 {
986 struct libmnt_table *tb;
987 int rc = 0;
988
989 tb = mnt_new_table();
990 if (!tb) {
991 warn(_("failed to initialize libmount table"));
992 return NULL;
993 }
994 mnt_table_set_parser_errcb(tb, parser_errcb);
995
996 do {
997 /* NULL means that libmount will use default paths */
998 const char *path = nfiles ? *files++ : NULL;
999
1000 switch (tabtype) {
1001 case TABTYPE_FSTAB:
1002 rc = mnt_table_parse_fstab(tb, path);
1003 break;
1004 case TABTYPE_MTAB:
1005 rc = mnt_table_parse_mtab(tb, path);
1006 break;
1007 case TABTYPE_KERNEL:
1008 if (!path)
1009 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
1010 _PATH_PROC_MOUNTINFO :
1011 _PATH_PROC_MOUNTS;
1012
1013 rc = mnt_table_parse_file(tb, path);
1014 break;
1015 }
1016 if (rc) {
1017 mnt_unref_table(tb);
1018 warn(_("can't read %s"), path);
1019 return NULL;
1020 }
1021 } while (--nfiles > 0);
1022
1023 return tb;
1024 }
1025
1026 /*
1027 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
1028 * necessary if @tb in main() was read from a non-kernel source.
1029 */
1030 static void cache_set_targets(struct libmnt_cache *tmp)
1031 {
1032 struct libmnt_table *tb;
1033 const char *path;
1034
1035 tb = mnt_new_table();
1036 if (!tb)
1037 return;
1038
1039 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
1040 _PATH_PROC_MOUNTINFO :
1041 _PATH_PROC_MOUNTS;
1042
1043 if (mnt_table_parse_file(tb, path) == 0)
1044 mnt_cache_set_targets(tmp, tb);
1045
1046 mnt_unref_table(tb);
1047 }
1048
1049 /* checks if @tb contains parent->child relations */
1050 static int tab_is_tree(struct libmnt_table *tb)
1051 {
1052 struct libmnt_fs *fs = NULL;
1053 struct libmnt_iter *itr;
1054 int rc = 0;
1055
1056 itr = mnt_new_iter(MNT_ITER_BACKWARD);
1057 if (!itr)
1058 return 0;
1059
1060 rc = (mnt_table_next_fs(tb, itr, &fs) == 0 &&
1061 mnt_fs_is_kernel(fs) &&
1062 mnt_fs_get_root(fs));
1063
1064 mnt_free_iter(itr);
1065 return rc;
1066 }
1067
1068 /* checks if all fs in @tb are from kernel */
1069 static int tab_is_kernel(struct libmnt_table *tb)
1070 {
1071 struct libmnt_fs *fs = NULL;
1072 struct libmnt_iter *itr;
1073
1074 itr = mnt_new_iter(MNT_ITER_BACKWARD);
1075 if (!itr)
1076 return 0;
1077
1078 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
1079 if (!mnt_fs_is_kernel(fs)) {
1080 mnt_free_iter(itr);
1081 return 0;
1082 }
1083 }
1084
1085 mnt_free_iter(itr);
1086 return 1;
1087 }
1088
1089 /* filter function for libmount (mnt_table_find_next_fs()) */
1090 static int match_func(struct libmnt_fs *fs,
1091 void *data __attribute__ ((__unused__)))
1092 {
1093 int rc = flags & FL_INVERT ? 1 : 0;
1094 const char *m;
1095 void *md;
1096
1097 m = get_match(COL_FSTYPE);
1098 if (m && !mnt_fs_match_fstype(fs, m))
1099 return rc;
1100
1101 m = get_match(COL_OPTIONS);
1102 if (m && !mnt_fs_match_options(fs, m))
1103 return rc;
1104
1105 md = get_match_data(COL_MAJMIN);
1106 if (md && mnt_fs_get_devno(fs) != *((dev_t *) md))
1107 return rc;
1108
1109 m = get_match(COL_TARGET);
1110 if (m && !mnt_fs_match_target(fs, m, cache))
1111 return rc;
1112
1113 m = get_match(COL_SOURCE);
1114 if (m && !mnt_fs_match_source(fs, m, cache))
1115 return rc;
1116
1117 if ((flags & FL_DF) && !(flags & FL_ALL)) {
1118 const char *type = mnt_fs_get_fstype(fs);
1119
1120 if (type && strstr(type, "tmpfs")) /* tmpfs is wanted */
1121 return !rc;
1122
1123 if (mnt_fs_is_pseudofs(fs))
1124 return rc;
1125 }
1126
1127 if ((flags & FL_REAL) && mnt_fs_is_pseudofs(fs))
1128 return rc;
1129
1130 if ((flags & FL_PSEUDO) && !mnt_fs_is_pseudofs(fs))
1131 return rc;
1132
1133 if ((flags & FL_SHADOWED)) {
1134 struct libmnt_table *tb = NULL;
1135
1136 mnt_fs_get_table(fs, &tb);
1137 if (tb && mnt_table_over_fs(tb, fs, NULL) != 0)
1138 return rc;
1139 }
1140
1141 return !rc;
1142 }
1143
1144 /* iterate over filesystems in @tb */
1145 struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
1146 struct libmnt_iter *itr)
1147 {
1148 struct libmnt_fs *fs = NULL;
1149
1150 if (is_listall_mode()) {
1151 /*
1152 * Print whole file
1153 */
1154 if (mnt_table_next_fs(tb, itr, &fs) != 0)
1155 return NULL;
1156
1157 } else if (is_mount_compatible_mode()) {
1158 /*
1159 * Look up for FS in the same way how mount(8) searches in fstab
1160 *
1161 * findmnt -f <spec>
1162 */
1163 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
1164 mnt_iter_get_direction(itr));
1165
1166 if (!fs && !(flags & FL_NOSWAPMATCH))
1167 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
1168 mnt_iter_get_direction(itr));
1169 } else {
1170 /*
1171 * Look up for all matching entries
1172 *
1173 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
1174 * findmnt [-l] <spec> [-O <options>] [-t <types>]
1175 */
1176 again:
1177 if (mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs) != 0)
1178 fs = NULL;
1179
1180 if (!fs &&
1181 !(flags & FL_NOSWAPMATCH) &&
1182 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1183
1184 /* swap 'spec' and target. */
1185 set_match(COL_TARGET, get_match(COL_SOURCE));
1186 set_match(COL_SOURCE, NULL);
1187 mnt_reset_iter(itr, -1);
1188
1189 goto again;
1190 }
1191 }
1192
1193 return fs;
1194 }
1195
1196 /*
1197 * Filter out unwanted lines for --list output or top level lines for
1198 * --submounts tree output.
1199 */
1200 static int add_matching_lines(struct libmnt_table *tb,
1201 struct libscols_table *table, int direction)
1202 {
1203 struct libmnt_iter *itr;
1204 struct libmnt_fs *fs;
1205 int nlines = 0, rc = -1;
1206
1207 itr = mnt_new_iter(direction);
1208 if (!itr) {
1209 warn(_("failed to initialize libmount iterator"));
1210 goto done;
1211 }
1212
1213 while((fs = get_next_fs(tb, itr))) {
1214 if ((flags & FL_TREE) || (flags & FL_SUBMOUNTS))
1215 rc = create_treenode(table, tb, fs, NULL);
1216 else
1217 rc = !add_line(table, fs, NULL);
1218 if (rc)
1219 goto done;
1220 nlines++;
1221 if (flags & FL_FIRSTONLY)
1222 break;
1223 flags |= FL_NOSWAPMATCH;
1224 }
1225
1226 if (nlines)
1227 rc = 0;
1228 done:
1229 mnt_free_iter(itr);
1230 return rc;
1231 }
1232
1233 static int poll_match(struct libmnt_fs *fs)
1234 {
1235 int rc = match_func(fs, NULL);
1236
1237 if (rc == 0 && !(flags & FL_NOSWAPMATCH) &&
1238 get_match(COL_SOURCE) && !get_match(COL_TARGET)) {
1239 /*
1240 * findmnt --poll /foo
1241 * The '/foo' maybe source as well as target.
1242 */
1243 const char *str = get_match(COL_SOURCE);
1244
1245 set_match(COL_TARGET, str); /* swap */
1246 set_match(COL_SOURCE, NULL);
1247
1248 rc = match_func(fs, NULL);
1249
1250 set_match(COL_TARGET, NULL); /* restore */
1251 set_match(COL_SOURCE, str);
1252
1253 }
1254 return rc;
1255 }
1256
1257 static int poll_table(struct libmnt_table *tb, const char *tabfile,
1258 int timeout, struct libscols_table *table, int direction)
1259 {
1260 FILE *f = NULL;
1261 int rc = -1;
1262 struct libmnt_iter *itr = NULL;
1263 struct libmnt_table *tb_new;
1264 struct libmnt_tabdiff *diff = NULL;
1265 struct pollfd fds[1];
1266
1267 tb_new = mnt_new_table();
1268 if (!tb_new) {
1269 warn(_("failed to initialize libmount table"));
1270 goto done;
1271 }
1272
1273 itr = mnt_new_iter(direction);
1274 if (!itr) {
1275 warn(_("failed to initialize libmount iterator"));
1276 goto done;
1277 }
1278
1279 diff = mnt_new_tabdiff();
1280 if (!diff) {
1281 warn(_("failed to initialize libmount tabdiff"));
1282 goto done;
1283 }
1284
1285 /* cache is unnecessary to detect changes */
1286 mnt_table_set_cache(tb, NULL);
1287 mnt_table_set_cache(tb_new, NULL);
1288
1289 f = fopen(tabfile, "r");
1290 if (!f) {
1291 warn(_("cannot open %s"), tabfile);
1292 goto done;
1293 }
1294
1295 mnt_table_set_parser_errcb(tb_new, parser_errcb);
1296
1297 fds[0].fd = fileno(f);
1298 fds[0].events = POLLPRI;
1299
1300 while (1) {
1301 struct libmnt_table *tmp;
1302 struct libmnt_fs *old, *new;
1303 int change, count;
1304
1305 count = poll(fds, 1, timeout);
1306 if (count == 0)
1307 break; /* timeout */
1308 if (count < 0) {
1309 warn(_("poll() failed"));
1310 goto done;
1311 }
1312
1313 rewind(f);
1314 rc = mnt_table_parse_stream(tb_new, f, tabfile);
1315 if (!rc)
1316 rc = mnt_diff_tables(diff, tb, tb_new);
1317 if (rc < 0)
1318 goto done;
1319
1320 count = 0;
1321 mnt_reset_iter(itr, direction);
1322 while(mnt_tabdiff_next_change(
1323 diff, itr, &old, &new, &change) == 0) {
1324
1325 if (!has_poll_action(change))
1326 continue;
1327 if (!poll_match(new ? new : old))
1328 continue;
1329 count++;
1330 rc = !add_tabdiff_line(table, new, old, change);
1331 if (rc)
1332 goto done;
1333 if (flags & FL_FIRSTONLY)
1334 break;
1335 }
1336
1337 if (count) {
1338 rc = scols_table_print_range(table, NULL, NULL);
1339 fflush(scols_table_get_stream(table));
1340 if (rc)
1341 goto done;
1342 }
1343
1344 /* swap tables */
1345 tmp = tb;
1346 tb = tb_new;
1347 tb_new = tmp;
1348
1349 /* remove already printed lines to reduce memory usage */
1350 scols_table_remove_lines(table);
1351 mnt_reset_table(tb_new);
1352
1353 if (count && (flags & FL_FIRSTONLY))
1354 break;
1355 }
1356
1357 rc = 0;
1358 done:
1359 mnt_unref_table(tb_new);
1360 mnt_free_tabdiff(diff);
1361 mnt_free_iter(itr);
1362 if (f)
1363 fclose(f);
1364 return rc;
1365 }
1366
1367 static int uniq_fs_target_cmp(
1368 struct libmnt_table *tb __attribute__((__unused__)),
1369 struct libmnt_fs *a,
1370 struct libmnt_fs *b)
1371 {
1372 return !mnt_fs_match_target(a, mnt_fs_get_target(b), cache);
1373 }
1374
1375 static int get_column_json_type(int id, int scols_flags, int *multi)
1376 {
1377 switch (id) {
1378 case COL_SIZE:
1379 case COL_AVAIL:
1380 case COL_USED:
1381 if (multi)
1382 *multi = 1;
1383 if (!(flags & FL_BYTES))
1384 break;
1385 /* fallthrough */
1386 case COL_ID:
1387 case COL_PARENT:
1388 case COL_FREQ:
1389 case COL_PASSNO:
1390 case COL_TID:
1391 return SCOLS_JSON_NUMBER;
1392
1393 default:
1394 if (scols_flags & SCOLS_FL_WRAP)
1395 return SCOLS_JSON_ARRAY_STRING;
1396 break;
1397 }
1398
1399 return SCOLS_JSON_STRING; /* default */
1400 }
1401
1402 static void __attribute__((__noreturn__)) usage(void)
1403 {
1404 FILE *out = stdout;
1405
1406 fputs(USAGE_HEADER, out);
1407 fprintf(out, _(
1408 " %1$s [options]\n"
1409 " %1$s [options] <device> | <mountpoint>\n"
1410 " %1$s [options] <device> <mountpoint>\n"
1411 " %1$s [options] [--source <device>] [--target <path> | --mountpoint <dir>]\n"),
1412 program_invocation_short_name);
1413
1414 fputs(USAGE_SEPARATOR, out);
1415 fputs(_("Find a (mounted) filesystem.\n"), out);
1416
1417 fputs(USAGE_OPTIONS, out);
1418 fputs(_(" -s, --fstab search in static table of filesystems\n"), out);
1419 fputs(_(" -m, --mtab search in table of mounted filesystems\n"
1420 " (includes user space mount options)\n"), out);
1421 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1422 " filesystems (default)\n"), out);
1423 fputc('\n', out);
1424 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out);
1425 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out);
1426 fputc('\n', out);
1427
1428 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out);
1429 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out);
1430 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out);
1431 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out);
1432 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out);
1433 fputs(_(" -D, --df imitate the output of df(1)\n"), out);
1434 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out);
1435 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1436 " to device names\n"), out);
1437 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out);
1438 fputs(_(" -f, --first-only print the first found filesystem only\n"), out);
1439 fputs(_(" -I, --dfi imitate the output of df(1) with -i option\n"), out);
1440 fputs(_(" -i, --invert invert the sense of matching\n"), out);
1441 fputs(_(" -J, --json use JSON output format\n"), out);
1442 fputs(_(" -l, --list use list format output\n"), out);
1443 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out);
1444 fputs(_(" -n, --noheadings don't print column headings\n"), out);
1445 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out);
1446 fputs(_(" -o, --output <list> output columns (see --list-columns)\n"), out);
1447 fputs(_(" --output-all output all available columns\n"), out);
1448 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
1449 fputs(_(" --pseudo print only pseudo-filesystems\n"), out);
1450 fputs(_(" --shadowed print only filesystems over-mounted by another filesystem\n"), out);
1451 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out);
1452 fputs(_(" -r, --raw use raw output format\n"), out);
1453 fputs(_(" --real print only real filesystems\n"), out);
1454 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1455 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out);
1456 fputs(_(" -T, --target <path> the path to the filesystem to use\n"), out);
1457 fputs(_(" --tree enable tree format output if possible\n"), out);
1458 fputs(_(" -M, --mountpoint <dir> the mountpoint directory\n"), out);
1459 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out);
1460 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out);
1461 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out);
1462 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out);
1463 fputs(_(" -y, --shell use column names to be usable as shell variable identifiers\n"), out);
1464
1465 fputc('\n', out);
1466 fputs(_(" -x, --verify verify mount table content (default is fstab)\n"), out);
1467 fputs(_(" --verbose print more details\n"), out);
1468 fputs(_(" --vfs-all print all VFS options\n"), out);
1469
1470 fputs(USAGE_SEPARATOR, out);
1471 fputs(_(" -H, --list-columns list the available columns\n"), out);
1472 fprintf(out, USAGE_HELP_OPTIONS(24));
1473
1474 fprintf(out, USAGE_MAN_TAIL("findmnt(8)"));
1475
1476 exit(EXIT_SUCCESS);
1477 }
1478
1479 static void __attribute__((__noreturn__)) list_colunms(void)
1480 {
1481 size_t i;
1482 struct libscols_table *tb = xcolumn_list_table_new("findmnt-columns", stdout,
1483 flags & FL_RAW,
1484 flags & FL_JSON);
1485
1486 for (i = 0; i < ARRAY_SIZE(infos); i++) {
1487 const struct colinfo *ci = &infos[i];
1488 int multi = 0;
1489 int json = get_column_json_type(i, ci->flags, &multi);
1490
1491 xcolumn_list_table_append_line(tb, ci->name,
1492 multi ? -1 : json,
1493 multi ? "<string|number>" : NULL,
1494 _(ci->help));
1495 }
1496
1497 scols_print_table(tb);
1498 scols_unref_table(tb);
1499
1500 exit(EXIT_SUCCESS);
1501 }
1502
1503
1504 int main(int argc, char *argv[])
1505 {
1506 struct libmnt_table *tb = NULL;
1507 char **tabfiles = NULL;
1508 int direction = MNT_ITER_FORWARD;
1509 int verify = 0, collist = 0;
1510 int c, rc = -1, timeout = -1;
1511 int ntabfiles = 0, tabtype = 0;
1512 char *outarg = NULL;
1513 size_t i;
1514 int force_tree = 0, istree = 0;
1515
1516 struct libscols_table *table = NULL;
1517
1518 enum {
1519 FINDMNT_OPT_VERBOSE = CHAR_MAX + 1,
1520 FINDMNT_OPT_TREE,
1521 FINDMNT_OPT_OUTPUT_ALL,
1522 FINDMNT_OPT_PSEUDO,
1523 FINDMNT_OPT_REAL,
1524 FINDMNT_OPT_VFS_ALL,
1525 FINDMNT_OPT_SHADOWED
1526 };
1527
1528 static const struct option longopts[] = {
1529 { "all", no_argument, NULL, 'A' },
1530 { "ascii", no_argument, NULL, 'a' },
1531 { "bytes", no_argument, NULL, 'b' },
1532 { "canonicalize", no_argument, NULL, 'c' },
1533 { "direction", required_argument, NULL, 'd' },
1534 { "df", no_argument, NULL, 'D' },
1535 { "dfi", no_argument, NULL, 'I' },
1536 { "evaluate", no_argument, NULL, 'e' },
1537 { "first-only", no_argument, NULL, 'f' },
1538 { "fstab", no_argument, NULL, 's' },
1539 { "help", no_argument, NULL, 'h' },
1540 { "invert", no_argument, NULL, 'i' },
1541 { "json", no_argument, NULL, 'J' },
1542 { "kernel", no_argument, NULL, 'k' },
1543 { "list", no_argument, NULL, 'l' },
1544 { "mountpoint", required_argument, NULL, 'M' },
1545 { "mtab", no_argument, NULL, 'm' },
1546 { "noheadings", no_argument, NULL, 'n' },
1547 { "notruncate", no_argument, NULL, 'u' },
1548 { "options", required_argument, NULL, 'O' },
1549 { "output", required_argument, NULL, 'o' },
1550 { "output-all", no_argument, NULL, FINDMNT_OPT_OUTPUT_ALL },
1551 { "poll", optional_argument, NULL, 'p' },
1552 { "pairs", no_argument, NULL, 'P' },
1553 { "raw", no_argument, NULL, 'r' },
1554 { "types", required_argument, NULL, 't' },
1555 { "nocanonicalize", no_argument, NULL, 'C' },
1556 { "nofsroot", no_argument, NULL, 'v' },
1557 { "submounts", no_argument, NULL, 'R' },
1558 { "source", required_argument, NULL, 'S' },
1559 { "tab-file", required_argument, NULL, 'F' },
1560 { "task", required_argument, NULL, 'N' },
1561 { "target", required_argument, NULL, 'T' },
1562 { "timeout", required_argument, NULL, 'w' },
1563 { "uniq", no_argument, NULL, 'U' },
1564 { "verify", no_argument, NULL, 'x' },
1565 { "version", no_argument, NULL, 'V' },
1566 { "shell", no_argument, NULL, 'y' },
1567 { "verbose", no_argument, NULL, FINDMNT_OPT_VERBOSE },
1568 { "tree", no_argument, NULL, FINDMNT_OPT_TREE },
1569 { "real", no_argument, NULL, FINDMNT_OPT_REAL },
1570 { "pseudo", no_argument, NULL, FINDMNT_OPT_PSEUDO },
1571 { "vfs-all", no_argument, NULL, FINDMNT_OPT_VFS_ALL },
1572 { "shadowed", no_argument, NULL, FINDMNT_OPT_SHADOWED },
1573 { "list-columns", no_argument, NULL, 'H' },
1574 { NULL, 0, NULL, 0 }
1575 };
1576
1577 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1578 { 'C', 'c'}, /* [no]canonicalize */
1579 { 'C', 'e' }, /* nocanonicalize, evaluate */
1580 { 'J', 'P', 'r','x' }, /* json,pairs,raw,verify */
1581 { 'M', 'T' }, /* mountpoint, target */
1582 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1583 { 'P','l','r','x' }, /* pairs,list,raw,verify */
1584 { 'p','x' }, /* poll,verify */
1585 { 'm','p','s' }, /* mtab,poll,fstab */
1586 { FINDMNT_OPT_PSEUDO, FINDMNT_OPT_REAL },
1587 { 0 }
1588 };
1589 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1590
1591 setlocale(LC_ALL, "");
1592 bindtextdomain(PACKAGE, LOCALEDIR);
1593 textdomain(PACKAGE);
1594 close_stdout_atexit();
1595
1596 /* default output format */
1597 flags |= FL_TREE;
1598
1599 while ((c = getopt_long(argc, argv,
1600 "AabCcDd:ehIiJfF:o:O:p::PklmM:nN:rst:uvRS:T:Uw:VxyH",
1601 longopts, NULL)) != -1) {
1602
1603 err_exclusive_options(c, longopts, excl, excl_st);
1604
1605 switch(c) {
1606 case 'A':
1607 flags |= FL_ALL;
1608 break;
1609 case 'a':
1610 flags |= FL_ASCII;
1611 break;
1612 case 'b':
1613 flags |= FL_BYTES;
1614 break;
1615 case 'C':
1616 flags |= FL_NOCACHE;
1617 break;
1618 case 'c':
1619 flags |= FL_CANONICALIZE;
1620 break;
1621 case 'D':
1622 flags &= ~FL_TREE;
1623 flags |= FL_DF;
1624 break;
1625 case 'd':
1626 if (!strcmp(optarg, "forward"))
1627 direction = MNT_ITER_FORWARD;
1628 else if (!strcmp(optarg, "backward"))
1629 direction = MNT_ITER_BACKWARD;
1630 else
1631 errx(EXIT_FAILURE,
1632 _("unknown direction '%s'"), optarg);
1633 break;
1634 case 'e':
1635 flags |= FL_EVALUATE;
1636 break;
1637 case 'I':
1638 flags &= ~FL_TREE;
1639 flags |= (FL_DF_INODES | FL_DF);
1640 break;
1641 case 'i':
1642 flags |= FL_INVERT;
1643 break;
1644 case 'J':
1645 flags |= FL_JSON;
1646 break;
1647 case 'f':
1648 flags |= FL_FIRSTONLY;
1649 break;
1650 case 'F':
1651 tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg);
1652 break;
1653 case 'u':
1654 disable_columns_truncate();
1655 break;
1656 case 'o':
1657 outarg = optarg;
1658 break;
1659 case FINDMNT_OPT_OUTPUT_ALL:
1660 ncolumns = 0;
1661 for (i = 0; i < ARRAY_SIZE(infos); i++) {
1662 if (is_tabdiff_column(i))
1663 continue;
1664 columns[ncolumns++] = i;
1665 }
1666 break;
1667 case 'O':
1668 set_match(COL_OPTIONS, optarg);
1669 break;
1670 case 'p':
1671 if (optarg) {
1672 nactions = string_to_idarray(optarg,
1673 actions, ARRAY_SIZE(actions),
1674 poll_action_name_to_id);
1675 if (nactions < 0)
1676 exit(EXIT_FAILURE);
1677 }
1678 flags |= FL_POLL;
1679 flags &= ~FL_TREE;
1680 break;
1681 case 'P':
1682 flags |= FL_EXPORT;
1683 flags &= ~FL_TREE;
1684 break;
1685 case 'm': /* mtab */
1686 tabtype = TABTYPE_MTAB;
1687 flags &= ~FL_TREE;
1688 break;
1689 case 's': /* fstab */
1690 tabtype = TABTYPE_FSTAB;
1691 flags &= ~FL_TREE;
1692 break;
1693 case 'k': /* kernel (mountinfo) */
1694 tabtype = TABTYPE_KERNEL;
1695 break;
1696 case 't':
1697 set_match(COL_FSTYPE, optarg);
1698 break;
1699 case 'r':
1700 flags &= ~FL_TREE; /* disable the default */
1701 flags |= FL_RAW; /* enable raw */
1702 break;
1703 case 'l':
1704 flags &= ~FL_TREE; /* disable the default */
1705 break;
1706 case 'n':
1707 flags |= FL_NOHEADINGS;
1708 break;
1709 case 'N':
1710 tabtype = TABTYPE_KERNEL;
1711 tabfiles = append_pid_tabfile(tabfiles, &ntabfiles,
1712 strtou32_or_err(optarg,
1713 _("invalid TID argument")));
1714 break;
1715 case 'v':
1716 flags |= FL_NOFSROOT;
1717 break;
1718 case 'R':
1719 flags |= FL_SUBMOUNTS;
1720 break;
1721 case 'S':
1722 set_source_match(optarg);
1723 flags |= FL_NOSWAPMATCH;
1724 break;
1725 case 'M':
1726 flags |= FL_STRICTTARGET;
1727 /* fallthrough */
1728 case 'T':
1729 set_match(COL_TARGET, optarg);
1730 flags |= FL_NOSWAPMATCH;
1731 break;
1732 case 'U':
1733 flags |= FL_UNIQ;
1734 break;
1735 case 'w':
1736 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
1737 break;
1738 case 'x':
1739 verify = 1;
1740 break;
1741 case 'y':
1742 flags |= FL_SHELLVAR;
1743 break;
1744 case FINDMNT_OPT_VERBOSE:
1745 flags |= FL_VERBOSE;
1746 break;
1747 case FINDMNT_OPT_TREE:
1748 force_tree = 1;
1749 break;
1750 case FINDMNT_OPT_PSEUDO:
1751 flags |= FL_PSEUDO;
1752 break;
1753 case FINDMNT_OPT_REAL:
1754 flags |= FL_REAL;
1755 break;
1756 case FINDMNT_OPT_VFS_ALL:
1757 flags |= FL_VFS_ALL;
1758 break;
1759 case FINDMNT_OPT_SHADOWED:
1760 flags |= FL_SHADOWED;
1761 break;
1762
1763 case 'H':
1764 collist = 1;
1765 break;
1766 case 'h':
1767 usage();
1768 case 'V':
1769 print_version(EXIT_SUCCESS);
1770 default:
1771 errtryhelp(EXIT_FAILURE);
1772 }
1773 }
1774
1775 if (collist)
1776 list_colunms(); /* print end exit */
1777
1778 if (!ncolumns && (flags & FL_DF)) {
1779 add_column(columns, ncolumns++, COL_SOURCE);
1780 add_column(columns, ncolumns++, COL_FSTYPE);
1781 if (flags & FL_DF_INODES) {
1782 add_column(columns, ncolumns++, COL_INO_TOTAL);
1783 add_column(columns, ncolumns++, COL_INO_USED);
1784 add_column(columns, ncolumns++, COL_INO_AVAIL);
1785 add_column(columns, ncolumns++, COL_INO_USEPERC);
1786 } else {
1787 add_column(columns, ncolumns++, COL_SIZE);
1788 add_column(columns, ncolumns++, COL_USED);
1789 add_column(columns, ncolumns++, COL_AVAIL);
1790 add_column(columns, ncolumns++, COL_USEPERC);
1791 }
1792 add_column(columns, ncolumns++, COL_TARGET);
1793 }
1794
1795 /* default columns */
1796 if (!ncolumns) {
1797 if (flags & FL_POLL)
1798 add_column(columns, ncolumns++, COL_ACTION);
1799
1800 add_column(columns, ncolumns++, COL_TARGET);
1801 add_column(columns, ncolumns++, COL_SOURCE);
1802 add_column(columns, ncolumns++, COL_FSTYPE);
1803 add_column(columns, ncolumns++, COL_OPTIONS);
1804 }
1805
1806 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
1807 &ncolumns, column_name_to_id) < 0)
1808 return EXIT_FAILURE;
1809
1810 if (!tabtype)
1811 tabtype = verify ? TABTYPE_FSTAB : TABTYPE_KERNEL;
1812
1813 if ((flags & FL_POLL) && ntabfiles > 1)
1814 errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file"));
1815
1816 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
1817 errx(EXIT_FAILURE, _(
1818 "options --target and --source can't be used together "
1819 "with command line element that is not an option"));
1820
1821 if (optind < argc)
1822 set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */
1823 if (optind < argc)
1824 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
1825
1826 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
1827 /* don't care about submounts if list all mounts */
1828 flags &= ~FL_SUBMOUNTS;
1829
1830 if (!(flags & FL_SUBMOUNTS) && ((flags & FL_FIRSTONLY)
1831 || get_match(COL_TARGET)
1832 || get_match(COL_SOURCE)
1833 || get_match(COL_MAJMIN)))
1834 flags &= ~FL_TREE;
1835
1836 if (!(flags & FL_NOSWAPMATCH) &&
1837 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1838 /*
1839 * Check if we can swap source and target, it's
1840 * not possible if the source is LABEL=/UUID=
1841 */
1842 const char *x = get_match(COL_SOURCE);
1843
1844 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) ||
1845 !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9))
1846 flags |= FL_NOSWAPMATCH;
1847 }
1848
1849 /*
1850 * initialize libmount
1851 */
1852 mnt_init_debug(0);
1853
1854 tb = parse_tabfiles(tabfiles, ntabfiles, tabtype);
1855 if (!tb)
1856 goto leave;
1857
1858 if (tabtype == TABTYPE_MTAB && tab_is_kernel(tb))
1859 tabtype = TABTYPE_KERNEL;
1860
1861 istree = tab_is_tree(tb);
1862 if (istree && force_tree)
1863 flags |= FL_TREE;
1864
1865 if ((flags & FL_TREE) && (ntabfiles > 1 || !istree))
1866 flags &= ~FL_TREE;
1867
1868 if (!(flags & FL_NOCACHE)) {
1869 cache = mnt_new_cache();
1870 if (!cache) {
1871 warn(_("failed to initialize libmount cache"));
1872 goto leave;
1873 }
1874 mnt_table_set_cache(tb, cache);
1875
1876 if (tabtype != TABTYPE_KERNEL)
1877 cache_set_targets(cache);
1878 }
1879
1880 if (flags & FL_UNIQ)
1881 mnt_table_uniq_fs(tb, MNT_UNIQ_KEEPTREE, uniq_fs_target_cmp);
1882
1883 if (verify) {
1884 rc = verify_table(tb);
1885 goto leave;
1886 }
1887
1888 /*
1889 * initialize libsmartcols
1890 */
1891 scols_init_debug(0);
1892 table = scols_new_table();
1893 if (!table) {
1894 warn(_("failed to allocate output table"));
1895 goto leave;
1896 }
1897 scols_table_enable_raw(table, !!(flags & FL_RAW));
1898 scols_table_enable_export(table, !!(flags & FL_EXPORT));
1899 scols_table_enable_shellvar(table, !!(flags & FL_SHELLVAR));
1900 scols_table_enable_json(table, !!(flags & FL_JSON));
1901 scols_table_enable_ascii(table, !!(flags & FL_ASCII));
1902 scols_table_enable_noheadings(table, !!(flags & FL_NOHEADINGS));
1903
1904 if (flags & FL_JSON)
1905 scols_table_set_name(table, "filesystems");
1906
1907 for (i = 0; i < ncolumns; i++) {
1908 struct libscols_column *cl;
1909 int fl = get_column_flags(i);
1910 int id = get_column_id(i);
1911
1912 if (!(flags & FL_TREE))
1913 fl &= ~SCOLS_FL_TREE;
1914
1915 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
1916 warnx(_("%s column is requested, but --poll "
1917 "is not enabled"), get_column_name(i));
1918 goto leave;
1919 }
1920 cl = scols_table_new_column(table, get_column_name(i),
1921 get_column_whint(i), fl);
1922 if (!cl) {
1923 warn(_("failed to allocate output column"));
1924 goto leave;
1925 }
1926 /* multi-line cells (now used for SOURCES) */
1927 if (fl & SCOLS_FL_WRAP)
1928 scols_column_set_wrapfunc(cl,
1929 NULL,
1930 scols_wrapzero_nextchunk,
1931 NULL);
1932 if (flags & FL_JSON)
1933 scols_column_set_json_type(cl, get_column_json_type(id, fl, NULL));
1934 }
1935
1936 /*
1937 * Fill in data to the output table
1938 */
1939 if (flags & FL_POLL) {
1940 /* poll mode (accept the first tabfile only) */
1941 rc = poll_table(tb, tabfiles ? *tabfiles : _PATH_PROC_MOUNTINFO, timeout, table, direction);
1942
1943 } else if ((flags & FL_TREE) && !(flags & FL_SUBMOUNTS)) {
1944 /* whole tree */
1945 rc = create_treenode(table, tb, NULL, NULL);
1946 } else {
1947 /* whole list of sub-tree */
1948 rc = add_matching_lines(tb, table, direction);
1949
1950 if (rc != 0
1951 && tabtype == TABTYPE_KERNEL
1952 && (flags & FL_NOSWAPMATCH)
1953 && !(flags & FL_STRICTTARGET)
1954 && get_match(COL_TARGET)) {
1955 /*
1956 * Found nothing, maybe the --target is regular file,
1957 * try it again with extra functionality for target
1958 * match
1959 */
1960 enable_extra_target_match(tb);
1961 rc = add_matching_lines(tb, table, direction);
1962 }
1963 }
1964
1965 /*
1966 * Print the output table for non-poll modes
1967 */
1968 if (!rc && !(flags & FL_POLL))
1969 scols_print_table(table);
1970 leave:
1971 scols_unref_table(table);
1972
1973 mnt_unref_table(tb);
1974 mnt_unref_cache(cache);
1975
1976 free(tabfiles);
1977 #ifdef HAVE_LIBUDEV
1978 udev_unref(udev);
1979 #endif
1980 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
1981 }