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