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