]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/findmnt.c
misc: add static keyword to where needed [smatch scan]
[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_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, "/"))
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 str = xstrdup(mnt_fs_get_target(fs));
537 break;
538 case COL_FSTYPE:
539 str = xstrdup(mnt_fs_get_fstype(fs));
540 break;
541 case COL_OPTIONS:
542 str = xstrdup(mnt_fs_get_options(fs));
543 break;
544 case COL_VFS_OPTIONS:
545 str = xstrdup(mnt_fs_get_vfs_options(fs));
546 break;
547 case COL_FS_OPTIONS:
548 str = xstrdup(mnt_fs_get_fs_options(fs));
549 break;
550 case COL_OPT_FIELDS:
551 str = xstrdup(mnt_fs_get_optional_fields(fs));
552 break;
553 case COL_UUID:
554 str = get_tag(fs, "UUID", col_id);
555 break;
556 case COL_PARTUUID:
557 str = get_tag(fs, "PARTUUID", col_id);
558 break;
559 case COL_LABEL:
560 str = get_tag(fs, "LABEL", col_id);
561 break;
562 case COL_PARTLABEL:
563 str = get_tag(fs, "PARTLABEL", col_id);
564 break;
565
566 case COL_MAJMIN:
567 {
568 dev_t devno = mnt_fs_get_devno(fs);
569 if (!devno)
570 break;
571
572 if ((flags & FL_RAW) || (flags & FL_EXPORT) || (flags & FL_JSON))
573 xasprintf(&str, "%u:%u", major(devno), minor(devno));
574 else
575 xasprintf(&str, "%3u:%-3u", major(devno), minor(devno));
576 break;
577 }
578 case COL_SIZE:
579 case COL_AVAIL:
580 case COL_USED:
581 case COL_USEPERC:
582 str = get_vfs_attr(fs, col_id);
583 break;
584 case COL_FSROOT:
585 str = xstrdup(mnt_fs_get_root(fs));
586 break;
587 case COL_TID:
588 if (mnt_fs_get_tid(fs))
589 xasprintf(&str, "%d", mnt_fs_get_tid(fs));
590 break;
591 case COL_ID:
592 if (mnt_fs_get_id(fs))
593 xasprintf(&str, "%d", mnt_fs_get_id(fs));
594 break;
595 case COL_PROPAGATION:
596 if (mnt_fs_is_kernel(fs)) {
597 unsigned long fl = 0;
598 char *n = NULL;
599
600 if (mnt_fs_get_propagation(fs, &fl) != 0)
601 break;
602
603 n = xstrdup((fl & MS_SHARED) ? "shared" : "private");
604
605 if (fl & MS_SLAVE) {
606 xasprintf(&str, "%s,slave", n);
607 free(n);
608 n = str;
609 }
610 if (fl & MS_UNBINDABLE) {
611 xasprintf(&str, "%s,unbindable", n);
612 free(n);
613 n = str;
614 }
615 str = n;
616 }
617 break;
618 case COL_FREQ:
619 if (!mnt_fs_is_kernel(fs))
620 xasprintf(&str, "%d", mnt_fs_get_freq(fs));
621 break;
622 case COL_PASSNO:
623 if (!mnt_fs_is_kernel(fs))
624 xasprintf(&str, "%d", mnt_fs_get_passno(fs));
625 break;
626 default:
627 break;
628 }
629 return str;
630 }
631
632 static char *get_tabdiff_data(struct libmnt_fs *old_fs,
633 struct libmnt_fs *new_fs,
634 int change,
635 int num)
636 {
637 char *str = NULL;
638
639 switch (get_column_id(num)) {
640 case COL_ACTION:
641 switch (change) {
642 case MNT_TABDIFF_MOUNT:
643 str = _("mount");
644 break;
645 case MNT_TABDIFF_UMOUNT:
646 str = _("umount");
647 break;
648 case MNT_TABDIFF_REMOUNT:
649 str = _("remount");
650 break;
651 case MNT_TABDIFF_MOVE:
652 str = _("move");
653 break;
654 default:
655 str = _("unknown");
656 break;
657 }
658 str = xstrdup(str);
659 break;
660 case COL_OLD_OPTIONS:
661 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
662 change == MNT_TABDIFF_UMOUNT))
663 str = xstrdup(mnt_fs_get_options(old_fs));
664 break;
665 case COL_OLD_TARGET:
666 if (old_fs && (change == MNT_TABDIFF_MOVE ||
667 change == MNT_TABDIFF_UMOUNT))
668 str = xstrdup(mnt_fs_get_target(old_fs));
669 break;
670 default:
671 if (new_fs)
672 str = get_data(new_fs, num);
673 else
674 str = get_data(old_fs, num);
675 break;
676 }
677 return str;
678 }
679
680 /* adds one line to the output @tab */
681 static struct libscols_line *add_line(struct libscols_table *table, struct libmnt_fs *fs,
682 struct libscols_line *parent)
683 {
684 size_t i;
685 struct libscols_line *line = scols_table_new_line(table, parent);
686
687 if (!line) {
688 warn(_("failed to add line to output"));
689 return NULL;
690 }
691 for (i = 0; i < ncolumns; i++)
692 scols_line_refer_data(line, i, get_data(fs, i));
693
694 scols_line_set_userdata(line, fs);
695 return line;
696 }
697
698 static struct libscols_line *add_tabdiff_line(struct libscols_table *table, struct libmnt_fs *new_fs,
699 struct libmnt_fs *old_fs, int change)
700 {
701 size_t i;
702 struct libscols_line *line = scols_table_new_line(table, NULL);
703
704 if (!line) {
705 warn(_("failed to add line to output"));
706 return NULL;
707 }
708 for (i = 0; i < ncolumns; i++)
709 scols_line_refer_data(line, i,
710 get_tabdiff_data(old_fs, new_fs, change, i));
711
712 return line;
713 }
714
715 static int has_line(struct libscols_table *table, struct libmnt_fs *fs)
716 {
717 struct libscols_line *ln;
718 struct libscols_iter *itr;
719 int rc = 0;
720
721 itr = scols_new_iter(SCOLS_ITER_FORWARD);
722 if (!itr)
723 return 0;
724
725 while (scols_table_next_line(table, itr, &ln) == 0) {
726 if ((struct libmnt_fs *) scols_line_get_userdata(ln) == fs) {
727 rc = 1;
728 break;
729 }
730 }
731
732 scols_free_iter(itr);
733 return rc;
734 }
735
736 /* reads filesystems from @tb (libmount) and fillin @table (output table) */
737 static int create_treenode(struct libscols_table *table, struct libmnt_table *tb,
738 struct libmnt_fs *fs, struct libscols_line *parent_line)
739 {
740 struct libmnt_fs *chld = NULL;
741 struct libmnt_iter *itr = NULL;
742 struct libscols_line *line;
743 int rc = -1;
744
745 if (!fs) {
746 /* first call, get root FS */
747 if (mnt_table_get_root_fs(tb, &fs))
748 goto leave;
749 parent_line = NULL;
750
751 } else if ((flags & FL_SUBMOUNTS) && has_line(table, fs))
752 return 0;
753
754 itr = mnt_new_iter(MNT_ITER_FORWARD);
755 if (!itr)
756 goto leave;
757
758 if ((flags & FL_SUBMOUNTS) || match_func(fs, NULL)) {
759 line = add_line(table, fs, parent_line);
760 if (!line)
761 goto leave;
762 } else
763 line = parent_line;
764
765 /*
766 * add all children to the output table
767 */
768 while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
769 if (create_treenode(table, tb, chld, line))
770 goto leave;
771 }
772 rc = 0;
773 leave:
774 mnt_free_iter(itr);
775 return rc;
776 }
777
778 /* error callback */
779 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
780 const char *filename, int line)
781 {
782 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
783 ++parse_nerrors;
784 return 1;
785 }
786
787 static char **append_tabfile(char **files, int *nfiles, char *filename)
788 {
789 files = xrealloc(files, sizeof(char *) * (*nfiles + 1));
790 files[(*nfiles)++] = filename;
791 return files;
792 }
793
794 static char **append_pid_tabfile(char **files, int *nfiles, pid_t pid)
795 {
796 char *path = NULL;
797
798 xasprintf(&path, "/proc/%d/mountinfo", (int) pid);
799 return append_tabfile(files, nfiles, path);
800 }
801
802 /* calls libmount fstab/mtab/mountinfo parser */
803 static struct libmnt_table *parse_tabfiles(char **files,
804 int nfiles,
805 int tabtype)
806 {
807 struct libmnt_table *tb;
808 int rc = 0;
809
810 tb = mnt_new_table();
811 if (!tb) {
812 warn(_("failed to initialize libmount table"));
813 return NULL;
814 }
815 mnt_table_set_parser_errcb(tb, parser_errcb);
816
817 do {
818 /* NULL means that libmount will use default paths */
819 const char *path = nfiles ? *files++ : NULL;
820
821 switch (tabtype) {
822 case TABTYPE_FSTAB:
823 rc = mnt_table_parse_fstab(tb, path);
824 break;
825 case TABTYPE_MTAB:
826 rc = mnt_table_parse_mtab(tb, path);
827 break;
828 case TABTYPE_KERNEL:
829 if (!path)
830 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
831 _PATH_PROC_MOUNTINFO :
832 _PATH_PROC_MOUNTS;
833
834 rc = mnt_table_parse_file(tb, path);
835 break;
836 }
837 if (rc) {
838 mnt_unref_table(tb);
839 warn(_("can't read %s"), path);
840 return NULL;
841 }
842 } while (--nfiles > 0);
843
844 return tb;
845 }
846
847 /*
848 * Parses mountinfo and calls mnt_cache_set_targets(cache, mtab). Only
849 * necessary if @tb in main() was read from a non-kernel source.
850 */
851 static void cache_set_targets(struct libmnt_cache *tmp)
852 {
853 struct libmnt_table *tb;
854 const char *path;
855
856 tb = mnt_new_table();
857 if (!tb)
858 return;
859
860 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
861 _PATH_PROC_MOUNTINFO :
862 _PATH_PROC_MOUNTS;
863
864 if (mnt_table_parse_file(tb, path) == 0)
865 mnt_cache_set_targets(tmp, tb);
866
867 mnt_unref_table(tb);
868 }
869
870 /* checks if @tb contains parent->child relations */
871 static int tab_is_tree(struct libmnt_table *tb)
872 {
873 struct libmnt_fs *fs = NULL;
874 struct libmnt_iter *itr = NULL;
875 int rc = 0;
876
877 itr = mnt_new_iter(MNT_ITER_BACKWARD);
878 if (!itr)
879 return 0;
880
881 rc = (mnt_table_next_fs(tb, itr, &fs) == 0 &&
882 mnt_fs_is_kernel(fs) &&
883 mnt_fs_get_root(fs));
884
885 mnt_free_iter(itr);
886 return rc;
887 }
888
889 /* checks if all fs in @tb are from kernel */
890 static int tab_is_kernel(struct libmnt_table *tb)
891 {
892 struct libmnt_fs *fs = NULL;
893 struct libmnt_iter *itr = NULL;
894
895 itr = mnt_new_iter(MNT_ITER_BACKWARD);
896 if (!itr)
897 return 0;
898
899 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
900 if (!mnt_fs_is_kernel(fs)) {
901 mnt_free_iter(itr);
902 return 0;
903 }
904 }
905
906 mnt_free_iter(itr);
907 return 1;
908 }
909
910 /* filter function for libmount (mnt_table_find_next_fs()) */
911 static int match_func(struct libmnt_fs *fs,
912 void *data __attribute__ ((__unused__)))
913 {
914 int rc = flags & FL_INVERT ? 1 : 0;
915 const char *m;
916 void *md;
917
918 m = get_match(COL_FSTYPE);
919 if (m && !mnt_fs_match_fstype(fs, m))
920 return rc;
921
922 m = get_match(COL_OPTIONS);
923 if (m && !mnt_fs_match_options(fs, m))
924 return rc;
925
926 md = get_match_data(COL_MAJMIN);
927 if (md && mnt_fs_get_devno(fs) != *((dev_t *) md))
928 return rc;
929
930 m = get_match(COL_TARGET);
931 if (m && !mnt_fs_match_target(fs, m, cache))
932 return rc;
933
934 m = get_match(COL_SOURCE);
935 if (m && !mnt_fs_match_source(fs, m, cache))
936 return rc;
937
938 if ((flags & FL_DF) && !(flags & FL_ALL)) {
939 const char *type = mnt_fs_get_fstype(fs);
940
941 if (type && strstr(type, "tmpfs")) /* tmpfs is wanted */
942 return !rc;
943
944 if (mnt_fs_is_pseudofs(fs))
945 return rc;
946 }
947
948 return !rc;
949 }
950
951 /* iterate over filesystems in @tb */
952 struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
953 struct libmnt_iter *itr)
954 {
955 struct libmnt_fs *fs = NULL;
956
957 if (is_listall_mode()) {
958 /*
959 * Print whole file
960 */
961 if (mnt_table_next_fs(tb, itr, &fs) != 0)
962 return NULL;
963
964 } else if (is_mount_compatible_mode()) {
965 /*
966 * Look up for FS in the same way how mount(8) searches in fstab
967 *
968 * findmnt -f <spec>
969 */
970 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
971 mnt_iter_get_direction(itr));
972
973 if (!fs && !(flags & FL_NOSWAPMATCH))
974 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
975 mnt_iter_get_direction(itr));
976 } else {
977 /*
978 * Look up for all matching entries
979 *
980 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
981 * findmnt [-l] <spec> [-O <options>] [-t <types>]
982 */
983 again:
984 mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs);
985
986 if (!fs &&
987 !(flags & FL_NOSWAPMATCH) &&
988 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
989
990 /* swap 'spec' and target. */
991 set_match(COL_TARGET, get_match(COL_SOURCE));
992 set_match(COL_SOURCE, NULL);
993 mnt_reset_iter(itr, -1);
994
995 goto again;
996 }
997 }
998
999 return fs;
1000 }
1001
1002 /*
1003 * Filter out unwanted lines for --list output or top level lines for
1004 * --submounts tree output.
1005 */
1006 static int add_matching_lines(struct libmnt_table *tb,
1007 struct libscols_table *table, int direction)
1008 {
1009 struct libmnt_iter *itr = NULL;
1010 struct libmnt_fs *fs;
1011 int nlines = 0, rc = -1;
1012
1013 itr = mnt_new_iter(direction);
1014 if (!itr) {
1015 warn(_("failed to initialize libmount iterator"));
1016 goto done;
1017 }
1018
1019 while((fs = get_next_fs(tb, itr))) {
1020 if ((flags & FL_TREE) || (flags & FL_SUBMOUNTS))
1021 rc = create_treenode(table, tb, fs, NULL);
1022 else
1023 rc = !add_line(table, fs, NULL);
1024 if (rc)
1025 goto done;
1026 nlines++;
1027 if (flags & FL_FIRSTONLY)
1028 break;
1029 flags |= FL_NOSWAPMATCH;
1030 }
1031
1032 if (nlines)
1033 rc = 0;
1034 done:
1035 mnt_free_iter(itr);
1036 return rc;
1037 }
1038
1039 static int poll_match(struct libmnt_fs *fs)
1040 {
1041 int rc = match_func(fs, NULL);
1042
1043 if (rc == 0 && !(flags & FL_NOSWAPMATCH) &&
1044 get_match(COL_SOURCE) && !get_match(COL_TARGET)) {
1045 /*
1046 * findmnt --poll /foo
1047 * The '/foo' maybe source as well as target.
1048 */
1049 const char *str = get_match(COL_SOURCE);
1050
1051 set_match(COL_TARGET, str); /* swap */
1052 set_match(COL_SOURCE, NULL);
1053
1054 rc = match_func(fs, NULL);
1055
1056 set_match(COL_TARGET, NULL); /* restore */
1057 set_match(COL_SOURCE, str);
1058
1059 }
1060 return rc;
1061 }
1062
1063 static int poll_table(struct libmnt_table *tb, const char *tabfile,
1064 int timeout, struct libscols_table *table, int direction)
1065 {
1066 FILE *f = NULL;
1067 int rc = -1;
1068 struct libmnt_iter *itr = NULL;
1069 struct libmnt_table *tb_new = NULL;
1070 struct libmnt_tabdiff *diff = NULL;
1071 struct pollfd fds[1];
1072
1073 tb_new = mnt_new_table();
1074 if (!tb_new) {
1075 warn(_("failed to initialize libmount table"));
1076 goto done;
1077 }
1078
1079 itr = mnt_new_iter(direction);
1080 if (!itr) {
1081 warn(_("failed to initialize libmount iterator"));
1082 goto done;
1083 }
1084
1085 diff = mnt_new_tabdiff();
1086 if (!diff) {
1087 warn(_("failed to initialize libmount tabdiff"));
1088 goto done;
1089 }
1090
1091 /* cache is unnecessary to detect changes */
1092 mnt_table_set_cache(tb, NULL);
1093 mnt_table_set_cache(tb_new, NULL);
1094
1095 f = fopen(tabfile, "r");
1096 if (!f) {
1097 warn(_("cannot open %s"), tabfile);
1098 goto done;
1099 }
1100
1101 mnt_table_set_parser_errcb(tb_new, parser_errcb);
1102
1103 fds[0].fd = fileno(f);
1104 fds[0].events = POLLPRI;
1105
1106 while (1) {
1107 struct libmnt_table *tmp;
1108 struct libmnt_fs *old, *new;
1109 int change, count;
1110
1111 count = poll(fds, 1, timeout);
1112 if (count == 0)
1113 break; /* timeout */
1114 if (count < 0) {
1115 warn(_("poll() failed"));
1116 goto done;
1117 }
1118
1119 rewind(f);
1120 rc = mnt_table_parse_stream(tb_new, f, tabfile);
1121 if (!rc)
1122 rc = mnt_diff_tables(diff, tb, tb_new);
1123 if (rc < 0)
1124 goto done;
1125
1126 count = 0;
1127 mnt_reset_iter(itr, direction);
1128 while(mnt_tabdiff_next_change(
1129 diff, itr, &old, &new, &change) == 0) {
1130
1131 if (!has_poll_action(change))
1132 continue;
1133 if (!poll_match(new ? new : old))
1134 continue;
1135 count++;
1136 rc = !add_tabdiff_line(table, new, old, change);
1137 if (rc)
1138 goto done;
1139 if (flags & FL_FIRSTONLY)
1140 break;
1141 }
1142
1143 if (count) {
1144 rc = scols_table_print_range(table, NULL, NULL);
1145 if (rc == 0)
1146 fputc('\n', scols_table_get_stream(table));
1147 fflush(stdout);
1148 if (rc)
1149 goto done;
1150 }
1151
1152 /* swap tables */
1153 tmp = tb;
1154 tb = tb_new;
1155 tb_new = tmp;
1156
1157 /* remove already printed lines to reduce memory usage */
1158 scols_table_remove_lines(table);
1159 mnt_reset_table(tb_new);
1160
1161 if (count && (flags & FL_FIRSTONLY))
1162 break;
1163 }
1164
1165 rc = 0;
1166 done:
1167 mnt_unref_table(tb_new);
1168 mnt_free_tabdiff(diff);
1169 mnt_free_iter(itr);
1170 if (f)
1171 fclose(f);
1172 return rc;
1173 }
1174
1175 static int uniq_fs_target_cmp(
1176 struct libmnt_table *tb __attribute__((__unused__)),
1177 struct libmnt_fs *a,
1178 struct libmnt_fs *b)
1179 {
1180 return !mnt_fs_match_target(a, mnt_fs_get_target(b), cache);
1181 }
1182
1183 static void __attribute__((__noreturn__)) usage(FILE *out)
1184 {
1185 size_t i;
1186
1187 fputs(USAGE_HEADER, out);
1188 fprintf(out, _(
1189 " %1$s [options]\n"
1190 " %1$s [options] <device> | <mountpoint>\n"
1191 " %1$s [options] <device> <mountpoint>\n"
1192 " %1$s [options] [--source <device>] [--target <path> | --mountpoint <dir>]\n"),
1193 program_invocation_short_name);
1194
1195 fputs(USAGE_SEPARATOR, out);
1196 fputs(_("Find a (mounted) filesystem.\n"), out);
1197
1198 fputs(USAGE_OPTIONS, out);
1199 fputs(_(" -s, --fstab search in static table of filesystems\n"), out);
1200 fputs(_(" -m, --mtab search in table of mounted filesystems\n"
1201 " (includes user space mount options)\n"), out);
1202 fputs(_(" -k, --kernel search in kernel table of mounted\n"
1203 " filesystems (default)\n"), out);
1204 fputc('\n', out);
1205 fputs(_(" -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"), out);
1206 fputs(_(" -w, --timeout <num> upper limit in milliseconds that --poll will block\n"), out);
1207 fputc('\n', out);
1208
1209 fputs(_(" -A, --all disable all built-in filters, print all filesystems\n"), out);
1210 fputs(_(" -a, --ascii use ASCII chars for tree formatting\n"), out);
1211 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out);
1212 fputs(_(" -C, --nocanonicalize don't canonicalize when comparing paths\n"), out);
1213 fputs(_(" -c, --canonicalize canonicalize printed paths\n"), out);
1214 fputs(_(" -D, --df imitate the output of df(1)\n"), out);
1215 fputs(_(" -d, --direction <word> direction of search, 'forward' or 'backward'\n"), out);
1216 fputs(_(" -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1217 " to device names\n"), out);
1218 fputs(_(" -F, --tab-file <path> alternative file for -s, -m or -k options\n"), out);
1219 fputs(_(" -f, --first-only print the first found filesystem only\n"), out);
1220 fputs(_(" -i, --invert invert the sense of matching\n"), out);
1221 fputs(_(" -J, --json use JSON output format\n"), out);
1222 fputs(_(" -l, --list use list format output\n"), out);
1223 fputs(_(" -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"), out);
1224 fputs(_(" -n, --noheadings don't print column headings\n"), out);
1225 fputs(_(" -O, --options <list> limit the set of filesystems by mount options\n"), out);
1226 fputs(_(" -o, --output <list> the output columns to be shown\n"), out);
1227 fputs(_(" -P, --pairs use key=\"value\" output format\n"), out);
1228 fputs(_(" -R, --submounts print all submounts for the matching filesystems\n"), out);
1229 fputs(_(" -r, --raw use raw output format\n"), out);
1230 fputs(_(" -S, --source <string> the device to mount (by name, maj:min, \n"
1231 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"), out);
1232 fputs(_(" -T, --target <path> the path to the filesystem to use\n"), out);
1233 fputs(_(" --tree enable tree format output is possible\n"), out);
1234 fputs(_(" -M, --mountpoint <dir> the mountpoint directory\n"), out);
1235 fputs(_(" -t, --types <list> limit the set of filesystems by FS types\n"), out);
1236 fputs(_(" -U, --uniq ignore filesystems with duplicate target\n"), out);
1237 fputs(_(" -u, --notruncate don't truncate text in columns\n"), out);
1238 fputs(_(" -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"), out);
1239
1240 fputc('\n', out);
1241 fputs(_(" -x, --verify verify mount table content (default is fstab)\n"), out);
1242 fputs(_(" --verbose print more details\n"), out);
1243 fputc('\n', out);
1244
1245 fputs(USAGE_SEPARATOR, out);
1246 fputs(USAGE_HELP, out);
1247 fputs(USAGE_VERSION, out);
1248
1249 fprintf(out, _("\nAvailable columns:\n"));
1250
1251 for (i = 0; i < ARRAY_SIZE(infos); i++)
1252 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
1253
1254 fprintf(out, USAGE_MAN_TAIL("findmnt(8)"));
1255
1256 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1257 }
1258
1259 int main(int argc, char *argv[])
1260 {
1261 struct libmnt_table *tb = NULL;
1262 char **tabfiles = NULL;
1263 int direction = MNT_ITER_FORWARD;
1264 int verify = 0;
1265 int c, rc = -1, timeout = -1;
1266 int ntabfiles = 0, tabtype = 0;
1267 char *outarg = NULL;
1268 size_t i;
1269 int force_tree = 0, istree = 0;
1270
1271 struct libscols_table *table = NULL;
1272
1273 enum {
1274 FINDMNT_OPT_VERBOSE = CHAR_MAX + 1,
1275 FINDMNT_OPT_TREE
1276 };
1277
1278 static const struct option longopts[] = {
1279 { "all", no_argument, NULL, 'A' },
1280 { "ascii", no_argument, NULL, 'a' },
1281 { "bytes", no_argument, NULL, 'b' },
1282 { "canonicalize", no_argument, NULL, 'c' },
1283 { "direction", required_argument, NULL, 'd' },
1284 { "df", no_argument, NULL, 'D' },
1285 { "evaluate", no_argument, NULL, 'e' },
1286 { "first-only", no_argument, NULL, 'f' },
1287 { "fstab", no_argument, NULL, 's' },
1288 { "help", no_argument, NULL, 'h' },
1289 { "invert", no_argument, NULL, 'i' },
1290 { "json", no_argument, NULL, 'J' },
1291 { "kernel", no_argument, NULL, 'k' },
1292 { "list", no_argument, NULL, 'l' },
1293 { "mountpoint", required_argument, NULL, 'M' },
1294 { "mtab", no_argument, NULL, 'm' },
1295 { "noheadings", no_argument, NULL, 'n' },
1296 { "notruncate", no_argument, NULL, 'u' },
1297 { "options", required_argument, NULL, 'O' },
1298 { "output", required_argument, NULL, 'o' },
1299 { "poll", optional_argument, NULL, 'p' },
1300 { "pairs", no_argument, NULL, 'P' },
1301 { "raw", no_argument, NULL, 'r' },
1302 { "types", required_argument, NULL, 't' },
1303 { "nocanonicalize", no_argument, NULL, 'C' },
1304 { "nofsroot", no_argument, NULL, 'v' },
1305 { "submounts", no_argument, NULL, 'R' },
1306 { "source", required_argument, NULL, 'S' },
1307 { "tab-file", required_argument, NULL, 'F' },
1308 { "task", required_argument, NULL, 'N' },
1309 { "target", required_argument, NULL, 'T' },
1310 { "timeout", required_argument, NULL, 'w' },
1311 { "uniq", no_argument, NULL, 'U' },
1312 { "verify", no_argument, NULL, 'x' },
1313 { "version", no_argument, NULL, 'V' },
1314 { "verbose", no_argument, NULL, FINDMNT_OPT_VERBOSE },
1315 { "tree", no_argument, NULL, FINDMNT_OPT_TREE },
1316 { NULL, 0, NULL, 0 }
1317 };
1318
1319 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
1320 { 'C', 'c'}, /* [no]canonicalize */
1321 { 'C', 'e' }, /* nocanonicalize, evaluate */
1322 { 'J', 'P', 'r','x' }, /* json,pairs,raw,verify */
1323 { 'M', 'T' }, /* mountpoint, target */
1324 { 'N','k','m','s' }, /* task,kernel,mtab,fstab */
1325 { 'P','l','r','x' }, /* pairs,list,raw,verify */
1326 { 'p','x' }, /* poll,verify */
1327 { 'm','p','s' }, /* mtab,poll,fstab */
1328 { 0 }
1329 };
1330 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
1331
1332 setlocale(LC_ALL, "");
1333 bindtextdomain(PACKAGE, LOCALEDIR);
1334 textdomain(PACKAGE);
1335 atexit(close_stdout);
1336
1337 /* default output format */
1338 flags |= FL_TREE;
1339
1340 while ((c = getopt_long(argc, argv,
1341 "AabCcDd:ehiJfF:o:O:p::PklmM:nN:rst:uvRS:T:Uw:Vx",
1342 longopts, NULL)) != -1) {
1343
1344 err_exclusive_options(c, longopts, excl, excl_st);
1345
1346 switch(c) {
1347 case 'A':
1348 flags |= FL_ALL;
1349 break;
1350 case 'a':
1351 flags |= FL_ASCII;
1352 break;
1353 case 'b':
1354 flags |= FL_BYTES;
1355 break;
1356 case 'C':
1357 flags |= FL_NOCACHE;
1358 break;
1359 case 'c':
1360 flags |= FL_CANONICALIZE;
1361 break;
1362 case 'D':
1363 flags &= ~FL_TREE;
1364 flags |= FL_DF;
1365 break;
1366 case 'd':
1367 if (!strcmp(optarg, "forward"))
1368 direction = MNT_ITER_FORWARD;
1369 else if (!strcmp(optarg, "backward"))
1370 direction = MNT_ITER_BACKWARD;
1371 else
1372 errx(EXIT_FAILURE,
1373 _("unknown direction '%s'"), optarg);
1374 break;
1375 case 'e':
1376 flags |= FL_EVALUATE;
1377 break;
1378 case 'h':
1379 usage(stdout);
1380 break;
1381 case 'i':
1382 flags |= FL_INVERT;
1383 break;
1384 case 'J':
1385 flags |= FL_JSON;
1386 break;
1387 case 'f':
1388 flags |= FL_FIRSTONLY;
1389 break;
1390 case 'F':
1391 tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg);
1392 break;
1393 case 'u':
1394 disable_columns_truncate();
1395 break;
1396 case 'o':
1397 outarg = optarg;
1398 break;
1399 case 'O':
1400 set_match(COL_OPTIONS, optarg);
1401 break;
1402 case 'p':
1403 if (optarg) {
1404 nactions = string_to_idarray(optarg,
1405 actions, ARRAY_SIZE(actions),
1406 poll_action_name_to_id);
1407 if (nactions < 0)
1408 exit(EXIT_FAILURE);
1409 }
1410 flags |= FL_POLL;
1411 flags &= ~FL_TREE;
1412 break;
1413 case 'P':
1414 flags |= FL_EXPORT;
1415 flags &= ~FL_TREE;
1416 break;
1417 case 'm': /* mtab */
1418 tabtype = TABTYPE_MTAB;
1419 flags &= ~FL_TREE;
1420 break;
1421 case 's': /* fstab */
1422 tabtype = TABTYPE_FSTAB;
1423 flags &= ~FL_TREE;
1424 break;
1425 case 'k': /* kernel (mountinfo) */
1426 tabtype = TABTYPE_KERNEL;
1427 break;
1428 case 't':
1429 set_match(COL_FSTYPE, optarg);
1430 break;
1431 case 'r':
1432 flags &= ~FL_TREE; /* disable the default */
1433 flags |= FL_RAW; /* enable raw */
1434 break;
1435 case 'l':
1436 flags &= ~FL_TREE; /* disable the default */
1437 break;
1438 case 'n':
1439 flags |= FL_NOHEADINGS;
1440 break;
1441 case 'N':
1442 tabtype = TABTYPE_KERNEL;
1443 tabfiles = append_pid_tabfile(tabfiles, &ntabfiles,
1444 strtou32_or_err(optarg,
1445 _("invalid TID argument")));
1446 break;
1447 case 'v':
1448 flags |= FL_NOFSROOT;
1449 break;
1450 case 'R':
1451 flags |= FL_SUBMOUNTS;
1452 break;
1453 case 'S':
1454 set_source_match(optarg);
1455 flags |= FL_NOSWAPMATCH;
1456 break;
1457 case 'M':
1458 flags |= FL_STRICTTARGET;
1459 /* fallthrough */
1460 case 'T':
1461 set_match(COL_TARGET, optarg);
1462 flags |= FL_NOSWAPMATCH;
1463 break;
1464 case 'U':
1465 flags |= FL_UNIQ;
1466 break;
1467 case 'w':
1468 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
1469 break;
1470 case 'V':
1471 printf(UTIL_LINUX_VERSION);
1472 return EXIT_SUCCESS;
1473 case 'x':
1474 verify = 1;
1475 break;
1476 case FINDMNT_OPT_VERBOSE:
1477 flags |= FL_VERBOSE;
1478 break;
1479 case FINDMNT_OPT_TREE:
1480 force_tree = 1;
1481 break;
1482 default:
1483 errtryhelp(EXIT_FAILURE);
1484 }
1485 }
1486
1487 if (!ncolumns && (flags & FL_DF)) {
1488 add_column(columns, ncolumns++, COL_SOURCE);
1489 add_column(columns, ncolumns++, COL_FSTYPE);
1490 add_column(columns, ncolumns++, COL_SIZE);
1491 add_column(columns, ncolumns++, COL_USED);
1492 add_column(columns, ncolumns++, COL_AVAIL);
1493 add_column(columns, ncolumns++, COL_USEPERC);
1494 add_column(columns, ncolumns++, COL_TARGET);
1495 }
1496
1497 /* default columns */
1498 if (!ncolumns) {
1499 if (flags & FL_POLL)
1500 add_column(columns, ncolumns++, COL_ACTION);
1501
1502 add_column(columns, ncolumns++, COL_TARGET);
1503 add_column(columns, ncolumns++, COL_SOURCE);
1504 add_column(columns, ncolumns++, COL_FSTYPE);
1505 add_column(columns, ncolumns++, COL_OPTIONS);
1506 }
1507
1508 if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
1509 &ncolumns, column_name_to_id) < 0)
1510 return EXIT_FAILURE;
1511
1512 if (!tabtype)
1513 tabtype = verify ? TABTYPE_FSTAB : TABTYPE_KERNEL;
1514
1515 if ((flags & FL_POLL) && ntabfiles > 1)
1516 errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file"));
1517
1518 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
1519 errx(EXIT_FAILURE, _(
1520 "options --target and --source can't be used together "
1521 "with command line element that is not an option"));
1522
1523 if (optind < argc)
1524 set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */
1525 if (optind < argc)
1526 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
1527
1528 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
1529 /* don't care about submounts if list all mounts */
1530 flags &= ~FL_SUBMOUNTS;
1531
1532 if (!(flags & FL_SUBMOUNTS) && ((flags & FL_FIRSTONLY)
1533 || get_match(COL_TARGET)
1534 || get_match(COL_SOURCE)
1535 || get_match(COL_MAJMIN)))
1536 flags &= ~FL_TREE;
1537
1538 if (!(flags & FL_NOSWAPMATCH) &&
1539 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1540 /*
1541 * Check if we can swap source and target, it's
1542 * not possible if the source is LABEL=/UUID=
1543 */
1544 const char *x = get_match(COL_SOURCE);
1545
1546 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) ||
1547 !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9))
1548 flags |= FL_NOSWAPMATCH;
1549 }
1550
1551 /*
1552 * initialize libmount
1553 */
1554 mnt_init_debug(0);
1555
1556 tb = parse_tabfiles(tabfiles, ntabfiles, tabtype);
1557 if (!tb)
1558 goto leave;
1559
1560 if (tabtype == TABTYPE_MTAB && tab_is_kernel(tb))
1561 tabtype = TABTYPE_KERNEL;
1562
1563 istree = tab_is_tree(tb);
1564 if (istree && force_tree)
1565 flags |= FL_TREE;
1566
1567 if ((flags & FL_TREE) && (ntabfiles > 1 || !istree))
1568 flags &= ~FL_TREE;
1569
1570 if (!(flags & FL_NOCACHE)) {
1571 cache = mnt_new_cache();
1572 if (!cache) {
1573 warn(_("failed to initialize libmount cache"));
1574 goto leave;
1575 }
1576 mnt_table_set_cache(tb, cache);
1577
1578 if (tabtype != TABTYPE_KERNEL)
1579 cache_set_targets(cache);
1580 }
1581
1582 if (flags & FL_UNIQ)
1583 mnt_table_uniq_fs(tb, MNT_UNIQ_KEEPTREE, uniq_fs_target_cmp);
1584
1585 if (verify) {
1586 rc = verify_table(tb);
1587 goto leave;
1588 }
1589
1590 /*
1591 * initialize libsmartcols
1592 */
1593 scols_init_debug(0);
1594 table = scols_new_table();
1595 if (!table) {
1596 warn(_("failed to initialize output table"));
1597 goto leave;
1598 }
1599 scols_table_enable_raw(table, !!(flags & FL_RAW));
1600 scols_table_enable_export(table, !!(flags & FL_EXPORT));
1601 scols_table_enable_json(table, !!(flags & FL_JSON));
1602 scols_table_enable_ascii(table, !!(flags & FL_ASCII));
1603 scols_table_enable_noheadings(table, !!(flags & FL_NOHEADINGS));
1604
1605 if (flags & FL_JSON)
1606 scols_table_set_name(table, "filesystems");
1607
1608 for (i = 0; i < ncolumns; i++) {
1609 int fl = get_column_flags(i);
1610 int id = get_column_id(i);
1611
1612 if (!(flags & FL_TREE))
1613 fl &= ~SCOLS_FL_TREE;
1614
1615 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
1616 warnx(_("%s column is requested, but --poll "
1617 "is not enabled"), get_column_name(i));
1618 goto leave;
1619 }
1620 if (!scols_table_new_column(table, get_column_name(i),
1621 get_column_whint(i), fl)) {
1622 warn(_("failed to initialize output column"));
1623 goto leave;
1624 }
1625 }
1626
1627 /*
1628 * Fill in data to the output table
1629 */
1630 if (flags & FL_POLL) {
1631 /* poll mode (accept the first tabfile only) */
1632 rc = poll_table(tb, tabfiles ? *tabfiles : _PATH_PROC_MOUNTINFO, timeout, table, direction);
1633
1634 } else if ((flags & FL_TREE) && !(flags & FL_SUBMOUNTS)) {
1635 /* whole tree */
1636 rc = create_treenode(table, tb, NULL, NULL);
1637 } else {
1638 /* whole list of sub-tree */
1639 rc = add_matching_lines(tb, table, direction);
1640
1641 if (rc != 0
1642 && tabtype == TABTYPE_KERNEL
1643 && (flags & FL_NOSWAPMATCH)
1644 && !(flags & FL_STRICTTARGET)
1645 && get_match(COL_TARGET)) {
1646 /*
1647 * Found nothing, maybe the --target is regular file,
1648 * try it again with extra functionality for target
1649 * match
1650 */
1651 enable_extra_target_match(tb);
1652 rc = add_matching_lines(tb, table, direction);
1653 }
1654 }
1655
1656 /*
1657 * Print the output table for non-poll modes
1658 */
1659 if (!rc && !(flags & FL_POLL))
1660 scols_print_table(table);
1661 leave:
1662 scols_unref_table(table);
1663
1664 mnt_unref_table(tb);
1665 mnt_unref_cache(cache);
1666
1667 free(tabfiles);
1668 #ifdef HAVE_LIBUDEV
1669 udev_unref(udev);
1670 #endif
1671 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
1672 }