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