]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/findmnt.c
translation: unify file open error messages
[thirdparty/util-linux.git] / misc-utils / findmnt.c
1 /*
2 * findmnt(8)
3 *
4 * Copyright (C) 2010,2011 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
36 #include <libmount.h>
37
38 #include "pathnames.h"
39 #include "nls.h"
40 #include "closestream.h"
41 #include "c.h"
42 #include "tt.h"
43 #include "strutils.h"
44 #include "xalloc.h"
45 #include "optutils.h"
46
47 /* flags */
48 enum {
49 FL_EVALUATE = (1 << 1),
50 FL_CANONICALIZE = (1 << 2),
51 FL_FIRSTONLY = (1 << 3),
52 FL_INVERT = (1 << 4),
53 FL_NOSWAPMATCH = (1 << 6),
54 FL_NOFSROOT = (1 << 7),
55 FL_SUBMOUNTS = (1 << 8),
56 FL_POLL = (1 << 9),
57 FL_DF = (1 << 10),
58 FL_ALL = (1 << 11)
59 };
60
61 /* column IDs */
62 enum {
63 COL_SOURCE,
64 COL_TARGET,
65 COL_FSTYPE,
66 COL_OPTIONS,
67 COL_VFS_OPTIONS,
68 COL_FS_OPTIONS,
69 COL_LABEL,
70 COL_UUID,
71 COL_PARTLABEL,
72 COL_PARTUUID,
73 COL_MAJMIN,
74 COL_ACTION,
75 COL_OLD_TARGET,
76 COL_OLD_OPTIONS,
77 COL_SIZE,
78 COL_AVAIL,
79 COL_USED,
80 COL_USEPERC,
81 COL_FSROOT,
82 COL_TID,
83
84 FINDMNT_NCOLUMNS
85 };
86
87 enum {
88 TABTYPE_FSTAB = 1,
89 TABTYPE_MTAB,
90 TABTYPE_KERNEL
91 };
92
93 /* column names */
94 struct colinfo {
95 const char *name; /* header */
96 double whint; /* width hint (N < 1 is in percent of termwidth) */
97 int flags; /* tt flags */
98 const char *help; /* column description */
99 const char *match; /* pattern for match_func() */
100 void *match_data; /* match specific data */
101 };
102
103 /* columns descriptions (don't use const, this is writable) */
104 static struct colinfo infos[FINDMNT_NCOLUMNS] = {
105 [COL_SOURCE] = { "SOURCE", 0.25, TT_FL_NOEXTREMES, N_("source device") },
106 [COL_TARGET] = { "TARGET", 0.30, TT_FL_TREE | TT_FL_NOEXTREMES, N_("mountpoint") },
107 [COL_FSTYPE] = { "FSTYPE", 0.10, TT_FL_TRUNC, N_("filesystem type") },
108 [COL_OPTIONS] = { "OPTIONS", 0.10, TT_FL_TRUNC, N_("all mount options") },
109 [COL_VFS_OPTIONS] = { "VFS-OPTIONS", 0.20, TT_FL_TRUNC, N_("VFS specific mount options") },
110 [COL_FS_OPTIONS] = { "FS-OPTIONS", 0.10, TT_FL_TRUNC, N_("FS specific mount options") },
111 [COL_LABEL] = { "LABEL", 0.10, 0, N_("filesystem label") },
112 [COL_UUID] = { "UUID", 36, 0, N_("filesystem UUID") },
113 [COL_PARTLABEL] = { "PARTLABEL", 0.10, 0, N_("partition label") },
114 [COL_PARTUUID] = { "PARTUUID", 36, 0, N_("partition UUID") },
115 [COL_MAJMIN] = { "MAJ:MIN", 6, 0, N_("major:minor device number") },
116 [COL_ACTION] = { "ACTION", 10, TT_FL_STRICTWIDTH, N_("action detected by --poll") },
117 [COL_OLD_OPTIONS] = { "OLD-OPTIONS", 0.10, TT_FL_TRUNC, N_("old mount options saved by --poll") },
118 [COL_OLD_TARGET] = { "OLD-TARGET", 0.30, 0, N_("old mountpoint saved by --poll") },
119 [COL_SIZE] = { "SIZE", 5, TT_FL_RIGHT, N_("filesystem size") },
120 [COL_AVAIL] = { "AVAIL", 5, TT_FL_RIGHT, N_("filesystem size available") },
121 [COL_USED] = { "USED", 5, TT_FL_RIGHT, N_("filesystem size used") },
122 [COL_USEPERC] = { "USE%", 3, TT_FL_RIGHT, N_("filesystem use percentage") },
123 [COL_FSROOT] = { "FSROOT", 0.25, TT_FL_NOEXTREMES, N_("filesystem root") },
124 [COL_TID] = { "TID", 4, TT_FL_RIGHT, N_("task ID") },
125 };
126
127 /* global flags */
128 static int flags;
129 static int tt_flags;
130
131 /* array with IDs of enabled columns */
132 static int columns[FINDMNT_NCOLUMNS];
133 static int ncolumns;
134
135 /* poll actions (parsed --poll=<list> */
136 #define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
137 static int actions[FINDMNT_NACTIONS];
138 static int nactions;
139
140 /* libmount cache */
141 static struct libmnt_cache *cache;
142
143 static int get_column_id(int num)
144 {
145 assert(num < ncolumns);
146 assert(columns[num] < FINDMNT_NCOLUMNS);
147 return columns[num];
148 }
149
150 static struct colinfo *get_column_info(int num)
151 {
152 return &infos[ get_column_id(num) ];
153 }
154
155 static const char *column_id_to_name(int id)
156 {
157 assert(id < FINDMNT_NCOLUMNS);
158 return infos[id].name;
159 }
160
161 static const char *get_column_name(int num)
162 {
163 return get_column_info(num)->name;
164 }
165
166 static float get_column_whint(int num)
167 {
168 return get_column_info(num)->whint;
169 }
170
171 static int get_column_flags(int num)
172 {
173 return get_column_info(num)->flags;
174 }
175
176 static const char *get_match(int id)
177 {
178 assert(id < FINDMNT_NCOLUMNS);
179 return infos[id].match;
180 }
181
182 static void *get_match_data(int id)
183 {
184 assert(id < FINDMNT_NCOLUMNS);
185 return infos[id].match_data;
186 }
187
188 static void set_match(int id, const char *match)
189 {
190 assert(id < FINDMNT_NCOLUMNS);
191 infos[id].match = match;
192 }
193
194 static void set_match_data(int id, void *data)
195 {
196 assert(id < FINDMNT_NCOLUMNS);
197 infos[id].match_data = data;
198 }
199
200 /*
201 * source match means COL_SOURCE *or* COL_MAJMIN, depends on
202 * data format.
203 */
204 static void set_source_match(const char *data)
205 {
206 int maj, min;
207
208 if (sscanf(data, "%d:%d", &maj, &min) == 2) {
209 dev_t *devno = xmalloc(sizeof(dev_t));
210
211 *devno = makedev(maj, min);
212 set_match(COL_MAJMIN, data);
213 set_match_data(COL_MAJMIN, (void *) devno);
214 flags |= FL_NOSWAPMATCH;
215 } else
216 set_match(COL_SOURCE, data);
217 }
218
219 static void enable_extra_target_match(void)
220 {
221 char *cn = NULL, *mnt = NULL;
222
223 /*
224 * Check if match pattern is mountpoint, if not use the
225 * real mountpoint.
226 */
227 cn = mnt_resolve_path(get_match(COL_TARGET), cache);
228 if (!cn)
229 return;
230
231 mnt = mnt_get_mountpoint(cn);
232 if (!mnt || strcmp(mnt, cn) == 0)
233 return;
234
235 /* replace the current setting with the real mountpoint */
236 set_match(COL_TARGET, mnt);
237 }
238
239
240 static int is_tabdiff_column(int id)
241 {
242 assert(id < FINDMNT_NCOLUMNS);
243
244 switch(id) {
245 case COL_ACTION:
246 case COL_OLD_TARGET:
247 case COL_OLD_OPTIONS:
248 return 1;
249 default:
250 break;
251 }
252 return 0;
253 }
254
255 /*
256 * "findmnt" without any filter
257 */
258 static int is_listall_mode(void)
259 {
260 if ((flags & FL_DF) && !(flags & FL_ALL))
261 return 0;
262
263 return (!get_match(COL_SOURCE) &&
264 !get_match(COL_TARGET) &&
265 !get_match(COL_FSTYPE) &&
266 !get_match(COL_OPTIONS) &&
267 !get_match(COL_MAJMIN));
268 }
269
270 /*
271 * Returns 1 if the @act is in the --poll=<list>
272 */
273 static int has_poll_action(int act)
274 {
275 int i;
276
277 if (!nactions)
278 return 1; /* all actions enabled */
279 for (i = 0; i < nactions; i++)
280 if (actions[i] == act)
281 return 1;
282 return 0;
283 }
284
285 static int poll_action_name_to_id(const char *name, size_t namesz)
286 {
287 int id = -1;
288
289 if (strncasecmp(name, "move", namesz) == 0 && namesz == 4)
290 id = MNT_TABDIFF_MOVE;
291 else if (strncasecmp(name, "mount", namesz) == 0 && namesz == 5)
292 id = MNT_TABDIFF_MOUNT;
293 else if (strncasecmp(name, "umount", namesz) == 0 && namesz == 6)
294 id = MNT_TABDIFF_UMOUNT;
295 else if (strncasecmp(name, "remount", namesz) == 0 && namesz == 7)
296 id = MNT_TABDIFF_REMOUNT;
297 else
298 warnx(_("unknown action: %s"), name);
299
300 return id;
301 }
302
303 /*
304 * findmnt --first-only <devname|TAG=|mountpoint>
305 *
306 * ... it works like "mount <devname|TAG=|mountpoint>"
307 */
308 static int is_mount_compatible_mode(void)
309 {
310 if (!get_match(COL_SOURCE))
311 return 0; /* <devname|TAG=|mountpoint> is required */
312 if (get_match(COL_FSTYPE) || get_match(COL_OPTIONS))
313 return 0; /* cannot be restricted by -t or -O */
314 if (!(flags & FL_FIRSTONLY))
315 return 0; /* we have to return the first entry only */
316
317 return 1; /* ok */
318 }
319
320 static void disable_columns_truncate(void)
321 {
322 int i;
323
324 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
325 infos[i].flags &= ~TT_FL_TRUNC;
326 }
327
328 /*
329 * converts @name to column ID
330 */
331 static int column_name_to_id(const char *name, size_t namesz)
332 {
333 int i;
334
335 for (i = 0; i < FINDMNT_NCOLUMNS; i++) {
336 const char *cn = column_id_to_name(i);
337
338 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
339 return i;
340 }
341 warnx(_("unknown column: %s"), name);
342 return -1;
343 }
344
345 /* Returns LABEL or UUID */
346 static const char *get_tag(struct libmnt_fs *fs, const char *tagname)
347 {
348 const char *t, *v, *res;
349
350 if (!mnt_fs_get_tag(fs, &t, &v) && !strcmp(t, tagname))
351 res = v;
352 else {
353 res = mnt_fs_get_source(fs);
354 if (res)
355 res = mnt_resolve_spec(res, cache);
356 if (res)
357 res = mnt_cache_find_tag_value(cache, res, tagname);
358 }
359
360 return res;
361 }
362
363 static const char *get_vfs_attr(struct libmnt_fs *fs, int sizetype)
364 {
365 struct statvfs buf;
366 uint64_t vfs_attr = 0;
367 char *sizestr;
368
369 if (statvfs(mnt_fs_get_target(fs), &buf) != 0)
370 return NULL;
371
372 switch(sizetype) {
373 case COL_SIZE:
374 vfs_attr = buf.f_frsize * buf.f_blocks;
375 break;
376 case COL_AVAIL:
377 vfs_attr = buf.f_frsize * buf.f_bfree;
378 break;
379 case COL_USED:
380 vfs_attr = buf.f_frsize * (buf.f_blocks - buf.f_bfree);
381 break;
382 case COL_USEPERC:
383 if (buf.f_blocks == 0)
384 return "-";
385
386 xasprintf(&sizestr, "%.0f%%",
387 (double)(buf.f_blocks - buf.f_bfree) /
388 buf.f_blocks * 100);
389 return sizestr;
390 }
391
392 return vfs_attr == 0 ? "0" :
393 size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr);
394 }
395
396 /* reads FS data from libmount
397 */
398 static const char *get_data(struct libmnt_fs *fs, int num)
399 {
400 const char *str = NULL;
401 int col_id = get_column_id(num);
402
403 switch (col_id) {
404 case COL_SOURCE:
405 {
406 const char *root = mnt_fs_get_root(fs);
407
408 str = mnt_fs_get_srcpath(fs);
409
410 if (str && (flags & FL_CANONICALIZE))
411 str = mnt_resolve_path(str, cache);
412 if (!str) {
413 str = mnt_fs_get_source(fs);
414
415 if (str && (flags & FL_EVALUATE))
416 str = mnt_resolve_spec(str, cache);
417 }
418 if (root && str && !(flags & FL_NOFSROOT) && strcmp(root, "/")) {
419 char *tmp;
420
421 if (xasprintf(&tmp, "%s[%s]", str, root) > 0)
422 str = tmp;
423 }
424 break;
425 }
426 case COL_TARGET:
427 str = mnt_fs_get_target(fs);
428 break;
429 case COL_FSTYPE:
430 str = mnt_fs_get_fstype(fs);
431 break;
432 case COL_OPTIONS:
433 str = mnt_fs_get_options(fs);
434 break;
435 case COL_VFS_OPTIONS:
436 str = mnt_fs_get_vfs_options(fs);
437 break;
438 case COL_FS_OPTIONS:
439 str = mnt_fs_get_fs_options(fs);
440 break;
441 case COL_UUID:
442 str = get_tag(fs, "UUID");
443 break;
444 case COL_PARTUUID:
445 str = get_tag(fs, "PARTUUID");
446 break;
447 case COL_LABEL:
448 str = get_tag(fs, "LABEL");
449 break;
450 case COL_PARTLABEL:
451 str = get_tag(fs, "PARTLABEL");
452 break;
453
454 case COL_MAJMIN:
455 {
456 dev_t devno = mnt_fs_get_devno(fs);
457 if (devno) {
458 char *tmp;
459 int rc = 0;
460 if ((tt_flags & TT_FL_RAW) || (tt_flags & TT_FL_EXPORT))
461 rc = xasprintf(&tmp, "%u:%u",
462 major(devno), minor(devno));
463 else
464 rc = xasprintf(&tmp, "%3u:%-3u",
465 major(devno), minor(devno));
466 if (rc)
467 str = tmp;
468 }
469 break;
470 }
471 case COL_SIZE:
472 case COL_AVAIL:
473 case COL_USED:
474 case COL_USEPERC:
475 str = get_vfs_attr(fs, col_id);
476 break;
477 case COL_FSROOT:
478 str = mnt_fs_get_root(fs);
479 break;
480 case COL_TID:
481 if (mnt_fs_get_tid(fs)) {
482 char *tmp;
483 if (xasprintf(&tmp, "%d", mnt_fs_get_tid(fs)) > 0)
484 str = tmp;
485 }
486 break;
487 default:
488 break;
489 }
490 return str;
491 }
492
493 static const char *get_tabdiff_data(struct libmnt_fs *old_fs,
494 struct libmnt_fs *new_fs,
495 int change,
496 int num)
497 {
498 const char *str = NULL;
499
500 switch (get_column_id(num)) {
501 case COL_ACTION:
502 switch (change) {
503 case MNT_TABDIFF_MOUNT:
504 str = _("mount");
505 break;
506 case MNT_TABDIFF_UMOUNT:
507 str = _("umount");
508 break;
509 case MNT_TABDIFF_REMOUNT:
510 str = _("remount");
511 break;
512 case MNT_TABDIFF_MOVE:
513 str = _("move");
514 break;
515 default:
516 str = _("unknown");
517 break;
518 }
519 break;
520 case COL_OLD_OPTIONS:
521 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
522 change == MNT_TABDIFF_UMOUNT))
523 str = mnt_fs_get_options(old_fs);
524 break;
525 case COL_OLD_TARGET:
526 if (old_fs && (change == MNT_TABDIFF_MOVE ||
527 change == MNT_TABDIFF_UMOUNT))
528 str = mnt_fs_get_target(old_fs);
529 break;
530 default:
531 if (new_fs)
532 str = get_data(new_fs, num);
533 else
534 str = get_data(old_fs, num);
535 break;
536 }
537 return str;
538 }
539
540 /* adds one line to the output @tab */
541 static struct tt_line *add_line(struct tt *tt, struct libmnt_fs *fs,
542 struct tt_line *parent)
543 {
544 int i;
545 struct tt_line *line = tt_add_line(tt, parent);
546
547 if (!line) {
548 warn(_("failed to add line to output"));
549 return NULL;
550 }
551 for (i = 0; i < ncolumns; i++)
552 tt_line_set_data(line, i, get_data(fs, i));
553
554 tt_line_set_userdata(line, fs);
555 return line;
556 }
557
558 static struct tt_line *add_tabdiff_line(struct tt *tt, struct libmnt_fs *new_fs,
559 struct libmnt_fs *old_fs, int change)
560 {
561 int i;
562 struct tt_line *line = tt_add_line(tt, NULL);
563
564 if (!line) {
565 warn(_("failed to add line to output"));
566 return NULL;
567 }
568 for (i = 0; i < ncolumns; i++)
569 tt_line_set_data(line, i,
570 get_tabdiff_data(old_fs, new_fs, change, i));
571
572 return line;
573 }
574
575 static int has_line(struct tt *tt, struct libmnt_fs *fs)
576 {
577 struct list_head *p;
578
579 list_for_each(p, &tt->tb_lines) {
580 struct tt_line *ln = list_entry(p, struct tt_line, ln_lines);
581 if ((struct libmnt_fs *) ln->userdata == fs)
582 return 1;
583 }
584 return 0;
585 }
586
587 /* reads filesystems from @tb (libmount) and fillin @tt (output table) */
588 static int create_treenode(struct tt *tt, struct libmnt_table *tb,
589 struct libmnt_fs *fs, struct tt_line *parent_line)
590 {
591 struct libmnt_fs *chld = NULL;
592 struct libmnt_iter *itr = NULL;
593 struct tt_line *line;
594 int rc = -1;
595
596 if (!fs) {
597 /* first call, get root FS */
598 if (mnt_table_get_root_fs(tb, &fs))
599 goto leave;
600 parent_line = NULL;
601
602 } else if ((flags & FL_SUBMOUNTS) && has_line(tt, fs))
603 return 0;
604
605 itr = mnt_new_iter(MNT_ITER_FORWARD);
606 if (!itr)
607 goto leave;
608
609 line = add_line(tt, fs, parent_line);
610 if (!line)
611 goto leave;
612
613 /*
614 * add all children to the output table
615 */
616 while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
617 if (create_treenode(tt, tb, chld, line))
618 goto leave;
619 }
620 rc = 0;
621 leave:
622 mnt_free_iter(itr);
623 return rc;
624 }
625
626 /* error callback */
627 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
628 const char *filename, int line)
629 {
630 warnx(_("%s: parse error at line %d"), filename, line);
631 return 0;
632 }
633
634 static char **append_tabfile(char **files, int *nfiles, char *filename)
635 {
636 files = xrealloc(files, sizeof(char *) * (*nfiles + 1));
637 files[(*nfiles)++] = filename;
638 return files;
639 }
640
641 static char **append_pid_tabfile(char **files, int *nfiles, pid_t pid)
642 {
643 char *path = NULL;
644
645 xasprintf(&path, "/proc/%d/mountinfo", (int) pid);
646 return append_tabfile(files, nfiles, path);
647 }
648
649 /* calls libmount fstab/mtab/mountinfo parser */
650 static struct libmnt_table *parse_tabfiles(char **files,
651 int nfiles,
652 int tabtype)
653 {
654 struct libmnt_table *tb;
655 int rc = 0;
656
657 tb = mnt_new_table();
658 if (!tb) {
659 warn(_("failed to initialize libmount table"));
660 return NULL;
661 }
662 mnt_table_set_parser_errcb(tb, parser_errcb);
663
664 do {
665 /* NULL means that libmount will use default paths */
666 const char *path = nfiles ? *files++ : NULL;
667
668 switch (tabtype) {
669 case TABTYPE_FSTAB:
670 rc = mnt_table_parse_fstab(tb, path);
671 break;
672 case TABTYPE_MTAB:
673 rc = mnt_table_parse_mtab(tb, path);
674 break;
675 case TABTYPE_KERNEL:
676 if (!path)
677 path = access(_PATH_PROC_MOUNTINFO, R_OK) == 0 ?
678 _PATH_PROC_MOUNTINFO :
679 _PATH_PROC_MOUNTS;
680
681 rc = mnt_table_parse_file(tb, path);
682 break;
683 }
684 if (rc) {
685 mnt_free_table(tb);
686 warn(_("can't read %s"), path);
687 return NULL;
688 }
689 } while (--nfiles > 0);
690
691 return tb;
692 }
693
694 /* checks if @tb contains parent->child relations */
695 static int tab_is_tree(struct libmnt_table *tb)
696 {
697 struct libmnt_fs *fs = NULL;
698 struct libmnt_iter *itr = NULL;
699 int rc = 0;
700
701 itr = mnt_new_iter(MNT_ITER_BACKWARD);
702 if (!itr)
703 return 0;
704
705 if (mnt_table_next_fs(tb, itr, &fs) == 0)
706 rc = mnt_fs_get_id(fs) > 0 && mnt_fs_get_parent_id(fs) > 0;
707
708 mnt_free_iter(itr);
709 return rc;
710 }
711
712
713 /* filter function for libmount (mnt_table_find_next_fs()) */
714 static int match_func(struct libmnt_fs *fs,
715 void *data __attribute__ ((__unused__)))
716 {
717 int rc = flags & FL_INVERT ? 1 : 0;
718 const char *m;
719 void *md;
720
721 m = get_match(COL_TARGET);
722 if (m && !mnt_fs_match_target(fs, m, cache))
723 return rc;
724
725 m = get_match(COL_SOURCE);
726 if (m && !mnt_fs_match_source(fs, m, cache))
727 return rc;
728
729 m = get_match(COL_FSTYPE);
730 if (m && !mnt_fs_match_fstype(fs, m))
731 return rc;
732
733 m = get_match(COL_OPTIONS);
734 if (m && !mnt_fs_match_options(fs, m))
735 return rc;
736
737 md = get_match_data(COL_MAJMIN);
738 if (md && mnt_fs_get_devno(fs) != *((dev_t *) md))
739 return rc;
740
741 if ((flags & FL_DF) && !(flags & FL_ALL)) {
742 const char *type = mnt_fs_get_fstype(fs);
743
744 if (type && strstr(type, "tmpfs")) /* tmpfs is wanted */
745 return !rc;
746
747 if (mnt_fs_is_pseudofs(fs))
748 return rc;
749 }
750
751 return !rc;
752 }
753
754 /* iterate over filesystems in @tb */
755 static struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
756 struct libmnt_iter *itr)
757 {
758 struct libmnt_fs *fs = NULL;
759
760 if (is_listall_mode()) {
761 /*
762 * Print whole file
763 */
764 if (mnt_table_next_fs(tb, itr, &fs) != 0)
765 return NULL;
766
767 } else if (is_mount_compatible_mode()) {
768 /*
769 * Look up for FS in the same way how mount(8) searchs in fstab
770 *
771 * findmnt -f <spec>
772 */
773 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
774 mnt_iter_get_direction(itr));
775
776 if (!fs && !(flags & FL_NOSWAPMATCH))
777 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
778 mnt_iter_get_direction(itr));
779 } else {
780 /*
781 * Look up for all matching entries
782 *
783 * findmnt [-l] <source> <target> [-O <options>] [-t <types>]
784 * findmnt [-l] <spec> [-O <options>] [-t <types>]
785 */
786 again:
787 mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs);
788
789 if (!fs &&
790 !(flags & FL_NOSWAPMATCH) &&
791 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
792
793 /* swap 'spec' and target. */
794 set_match(COL_TARGET, get_match(COL_SOURCE));
795 set_match(COL_SOURCE, NULL);
796 mnt_reset_iter(itr, -1);
797
798 goto again;
799 }
800 }
801
802 return fs;
803 }
804
805 static int add_matching_lines(struct libmnt_table *tb,
806 struct tt *tt, int direction)
807 {
808 struct libmnt_iter *itr = NULL;
809 struct libmnt_fs *fs;
810 int nlines = 0, rc = -1;
811
812 itr = mnt_new_iter(direction);
813 if (!itr) {
814 warn(_("failed to initialize libmount iterator"));
815 goto done;
816 }
817
818 while((fs = get_next_fs(tb, itr))) {
819 if ((tt_flags & TT_FL_TREE) || (flags & FL_SUBMOUNTS))
820 rc = create_treenode(tt, tb, fs, NULL);
821 else
822 rc = !add_line(tt, fs, NULL);
823 if (rc)
824 goto done;
825 nlines++;
826 if (flags & FL_FIRSTONLY)
827 break;
828 flags |= FL_NOSWAPMATCH;
829 }
830
831 if (nlines)
832 rc = 0;
833 done:
834 mnt_free_iter(itr);
835 return rc;
836 }
837
838 static int poll_match(struct libmnt_fs *fs)
839 {
840 int rc = match_func(fs, NULL);
841
842 if (rc == 0 && !(flags & FL_NOSWAPMATCH) &&
843 get_match(COL_SOURCE) && !get_match(COL_TARGET)) {
844 /*
845 * findmnt --poll /foo
846 * The '/foo' maybe source as well as target.
847 */
848 const char *str = get_match(COL_SOURCE);
849
850 set_match(COL_TARGET, str); /* swap */
851 set_match(COL_SOURCE, NULL);
852
853 rc = match_func(fs, NULL);
854
855 set_match(COL_TARGET, NULL); /* restore */
856 set_match(COL_SOURCE, str);
857
858 }
859 return rc;
860 }
861
862 static int poll_table(struct libmnt_table *tb, const char *tabfile,
863 int timeout, struct tt *tt, int direction)
864 {
865 FILE *f = NULL;
866 int rc = -1;
867 struct libmnt_iter *itr = NULL;
868 struct libmnt_table *tb_new = NULL;
869 struct libmnt_tabdiff *diff = NULL;
870 struct pollfd fds[1];
871
872 tb_new = mnt_new_table();
873 if (!tb_new) {
874 warn(_("failed to initialize libmount table"));
875 goto done;
876 }
877
878 itr = mnt_new_iter(direction);
879 if (!itr) {
880 warn(_("failed to initialize libmount iterator"));
881 goto done;
882 }
883
884 diff = mnt_new_tabdiff();
885 if (!diff) {
886 warn(_("failed to initialize libmount tabdiff"));
887 goto done;
888 }
889
890 /* cache is unnecessary to detect changes */
891 mnt_table_set_cache(tb, NULL);
892 mnt_table_set_cache(tb_new, NULL);
893
894 f = fopen(tabfile, "r");
895 if (!f) {
896 warn(_("cannot open %s"), tabfile);
897 goto done;
898 }
899
900 mnt_table_set_parser_errcb(tb_new, parser_errcb);
901
902 fds[0].fd = fileno(f);
903 fds[0].events = POLLPRI;
904
905 while (1) {
906 struct libmnt_table *tmp;
907 struct libmnt_fs *old, *new;
908 int change, count;
909
910 count = poll(fds, 1, timeout);
911 if (count == 0)
912 break; /* timeout */
913 if (count < 0) {
914 warn(_("poll() failed"));
915 goto done;
916 }
917
918 rewind(f);
919 rc = mnt_table_parse_stream(tb_new, f, tabfile);
920 if (!rc)
921 rc = mnt_diff_tables(diff, tb, tb_new);
922 if (rc < 0)
923 goto done;
924
925 count = 0;
926 mnt_reset_iter(itr, direction);
927 while(mnt_tabdiff_next_change(
928 diff, itr, &old, &new, &change) == 0) {
929
930 if (!has_poll_action(change))
931 continue;
932 if (!poll_match(new ? new : old))
933 continue;
934 count++;
935 rc = !add_tabdiff_line(tt, new, old, change);
936 if (rc)
937 goto done;
938 if (flags & FL_FIRSTONLY)
939 break;
940 }
941
942 if (count) {
943 rc = tt_print_table(tt);
944 if (rc)
945 goto done;
946 }
947
948 /* swap tables */
949 tmp = tb;
950 tb = tb_new;
951 tb_new = tmp;
952
953 tt_remove_lines(tt);
954 mnt_reset_table(tb_new);
955
956 if (count && (flags & FL_FIRSTONLY))
957 break;
958 }
959
960 rc = 0;
961 done:
962 mnt_free_table(tb_new);
963 mnt_free_tabdiff(diff);
964 mnt_free_iter(itr);
965 if (f)
966 fclose(f);
967 return rc;
968 }
969
970 static void __attribute__((__noreturn__)) usage(FILE *out)
971 {
972 int i;
973
974 fputs(USAGE_HEADER, out);
975 fprintf(out, _(
976 " %1$s [options]\n"
977 " %1$s [options] <device> | <mountpoint>\n"
978 " %1$s [options] <device> <mountpoint>\n"
979 " %1$s [options] [--source <device>] [--target <mountpoint>]\n"),
980 program_invocation_short_name);
981
982 fprintf(out, _(
983 "\nOptions:\n"
984 " -s, --fstab search in static table of filesystems\n"
985 " -m, --mtab search in table of mounted filesystems\n"
986 " -k, --kernel search in kernel table of mounted\n"
987 " filesystems (default)\n\n"));
988
989 fprintf(out, _(
990 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
991 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
992
993 fprintf(out, _(
994 " -A, --all disable all built-in filters, print all filesystems\n"
995 " -a, --ascii use ASCII chars for tree formatting\n"
996 " -c, --canonicalize canonicalize printed paths\n"
997 " -D, --df imitate the output of df(1)\n"
998 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
999 " -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n"
1000 " to device names\n"
1001 " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n"
1002 " -f, --first-only print the first found filesystem only\n"));
1003
1004 fprintf(out, _(
1005 " -i, --invert invert the sense of matching\n"
1006 " -l, --list use list format output\n"
1007 " -N, --task <tid> use alternative namespace (/proc/<tid>/mountinfo file)\n"
1008 " -n, --noheadings don't print column headings\n"
1009 " -u, --notruncate don't truncate text in columns\n"));
1010 fprintf(out, _(
1011 " -O, --options <list> limit the set of filesystems by mount options\n"
1012 " -o, --output <list> the output columns to be shown\n"
1013 " -P, --pairs use key=\"value\" output format\n"
1014 " -r, --raw use raw output format\n"
1015 " -t, --types <list> limit the set of filesystems by FS types\n"));
1016 fprintf(out, _(
1017 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
1018 " -R, --submounts print all submounts for the matching filesystems\n"
1019 " -S, --source <string> the device to mount (by name, maj:min, \n"
1020 " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n"
1021 " -T, --target <string> the mountpoint to use\n"));
1022
1023 fputs(USAGE_SEPARATOR, out);
1024 fputs(USAGE_HELP, out);
1025 fputs(USAGE_VERSION, out);
1026
1027 fprintf(out, _("\nAvailable columns:\n"));
1028
1029 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
1030 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
1031
1032 fprintf(out, USAGE_MAN_TAIL("findmnt(8)"));
1033
1034 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1035 }
1036
1037 int main(int argc, char *argv[])
1038 {
1039 struct libmnt_table *tb = NULL;
1040 char **tabfiles = NULL;
1041 int direction = MNT_ITER_FORWARD;
1042 int i, c, rc = -1, timeout = -1;
1043 int ntabfiles = 0, tabtype = 0;
1044
1045 enum {
1046 EXCL_NONE,
1047
1048 EXCL_FSTAB,
1049 EXCL_KERNEL,
1050 EXCL_MTAB,
1051 EXCL_TASK,
1052
1053 EXCL_RAW,
1054 EXCL_LIST,
1055 EXCL_PAIRS
1056 };
1057 int excl_fmk = EXCL_NONE;
1058 int excl_fmN = EXCL_NONE;
1059 int excl_rlP = EXCL_NONE;
1060
1061 /* table.h */
1062 struct tt *tt = NULL;
1063
1064 static const struct option longopts[] = {
1065 { "all", 0, 0, 'A' },
1066 { "ascii", 0, 0, 'a' },
1067 { "canonicalize", 0, 0, 'c' },
1068 { "direction", 1, 0, 'd' },
1069 { "df", 0, 0, 'D' },
1070 { "evaluate", 0, 0, 'e' },
1071 { "first-only", 0, 0, 'f' },
1072 { "fstab", 0, 0, 's' },
1073 { "help", 0, 0, 'h' },
1074 { "invert", 0, 0, 'i' },
1075 { "kernel", 0, 0, 'k' },
1076 { "list", 0, 0, 'l' },
1077 { "mtab", 0, 0, 'm' },
1078 { "noheadings", 0, 0, 'n' },
1079 { "notruncate", 0, 0, 'u' },
1080 { "options", 1, 0, 'O' },
1081 { "output", 1, 0, 'o' },
1082 { "poll", 2, 0, 'p' },
1083 { "pairs", 0, 0, 'P' },
1084 { "raw", 0, 0, 'r' },
1085 { "types", 1, 0, 't' },
1086 { "fsroot", 0, 0, 'v' },
1087 { "submounts", 0, 0, 'R' },
1088 { "source", 1, 0, 'S' },
1089 { "tab-file", 1, 0, 'F' },
1090 { "task", 1, 0, 'N' },
1091 { "target", 1, 0, 'T' },
1092 { "timeout", 1, 0, 'w' },
1093 { "version", 0, 0, 'V' },
1094
1095 { NULL, 0, 0, 0 }
1096 };
1097
1098 assert(ARRAY_SIZE(columns) == FINDMNT_NCOLUMNS);
1099
1100 setlocale(LC_ALL, "");
1101 bindtextdomain(PACKAGE, LOCALEDIR);
1102 textdomain(PACKAGE);
1103 atexit(close_stdout);
1104
1105 /* default output format */
1106 tt_flags |= TT_FL_TREE;
1107
1108 while ((c = getopt_long(argc, argv,
1109 "AacDd:ehifF:o:O:p::PklmnN:rst:uvRS:T:w:V",
1110 longopts, NULL)) != -1) {
1111 switch(c) {
1112 case 'A':
1113 flags |= FL_ALL;
1114 break;
1115 case 'a':
1116 tt_flags |= TT_FL_ASCII;
1117 break;
1118 case 'c':
1119 flags |= FL_CANONICALIZE;
1120 break;
1121 case 'D':
1122 tt_flags &= ~TT_FL_TREE;
1123 flags |= FL_DF;
1124 break;
1125 case 'd':
1126 if (!strcmp(optarg, "forward"))
1127 direction = MNT_ITER_FORWARD;
1128 else if (!strcmp(optarg, "backward"))
1129 direction = MNT_ITER_BACKWARD;
1130 else
1131 errx(EXIT_FAILURE,
1132 _("unknown direction '%s'"), optarg);
1133 break;
1134 case 'e':
1135 flags |= FL_EVALUATE;
1136 break;
1137 case 'h':
1138 usage(stdout);
1139 break;
1140 case 'i':
1141 flags |= FL_INVERT;
1142 break;
1143 case 'f':
1144 flags |= FL_FIRSTONLY;
1145 break;
1146 case 'F':
1147 tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg);
1148 break;
1149 case 'u':
1150 disable_columns_truncate();
1151 break;
1152 case 'o':
1153 ncolumns = string_to_idarray(optarg,
1154 columns, ARRAY_SIZE(columns),
1155 column_name_to_id);
1156 if (ncolumns < 0)
1157 exit(EXIT_FAILURE);
1158 break;
1159 case 'O':
1160 set_match(COL_OPTIONS, optarg);
1161 break;
1162 case 'p':
1163 if (optarg) {
1164 nactions = string_to_idarray(optarg,
1165 actions, ARRAY_SIZE(actions),
1166 poll_action_name_to_id);
1167 if (nactions < 0)
1168 exit(EXIT_FAILURE);
1169 }
1170 flags |= FL_POLL;
1171 tt_flags &= ~TT_FL_TREE;
1172 break;
1173 case 'P':
1174 exclusive_option(&excl_rlP, EXCL_PAIRS, "--{raw,list,pairs}");
1175 tt_flags |= TT_FL_EXPORT;
1176 tt_flags &= ~TT_FL_TREE;
1177 break;
1178 case 'm': /* mtab */
1179 exclusive_option(&excl_fmk, EXCL_MTAB, "--{fstab,mtab,kernel}");
1180 exclusive_option(&excl_fmN, EXCL_MTAB, "--{fstab,mtab,task}");
1181 tabtype = TABTYPE_MTAB;
1182 tt_flags &= ~TT_FL_TREE;
1183 break;
1184 case 's': /* fstab */
1185 exclusive_option(&excl_fmk, EXCL_FSTAB, "--{fstab,mtab,kernel}");
1186 exclusive_option(&excl_fmN, EXCL_FSTAB, "--{fstab,mtab,task}");
1187 tabtype = TABTYPE_FSTAB;
1188 tt_flags &= ~TT_FL_TREE;
1189 break;
1190 case 'k': /* kernel (mountinfo) */
1191 exclusive_option(&excl_fmk, EXCL_KERNEL, "--{fstab,mtab,kernel}");
1192 tabtype = TABTYPE_KERNEL;
1193 break;
1194 case 't':
1195 set_match(COL_FSTYPE, optarg);
1196 break;
1197 case 'r':
1198 exclusive_option(&excl_rlP, EXCL_RAW, "--{raw,list,pairs}");
1199 tt_flags &= ~TT_FL_TREE; /* disable the default */
1200 tt_flags |= TT_FL_RAW; /* enable raw */
1201 break;
1202 case 'l':
1203 exclusive_option(&excl_rlP, EXCL_LIST, "--{raw,list,pairs}");
1204 tt_flags &= ~TT_FL_TREE; /* disable the default */
1205 break;
1206 case 'n':
1207 tt_flags |= TT_FL_NOHEADINGS;
1208 break;
1209 case 'N':
1210 exclusive_option(&excl_fmN, EXCL_KERNEL, "--{fstab,mtab,task}");
1211 tabtype = TABTYPE_KERNEL;
1212 tabfiles = append_pid_tabfile(tabfiles, &ntabfiles,
1213 strtou32_or_err(optarg,
1214 _("invalid TID argument")));
1215 break;
1216 case 'v':
1217 flags |= FL_NOFSROOT;
1218 break;
1219 case 'R':
1220 flags |= FL_SUBMOUNTS;
1221 break;
1222 case 'S':
1223 set_source_match(optarg);
1224 flags |= FL_NOSWAPMATCH;
1225 break;
1226 case 'T':
1227 set_match(COL_TARGET, optarg);
1228 flags |= FL_NOSWAPMATCH;
1229 break;
1230 case 'w':
1231 timeout = strtos32_or_err(optarg, _("invalid timeout argument"));
1232 break;
1233 case 'V':
1234 printf(UTIL_LINUX_VERSION);
1235 return EXIT_SUCCESS;
1236 default:
1237 usage(stderr);
1238 break;
1239 }
1240 }
1241
1242 if (!ncolumns && (flags & FL_DF)) {
1243 columns[ncolumns++] = COL_SOURCE;
1244 columns[ncolumns++] = COL_FSTYPE;
1245 columns[ncolumns++] = COL_SIZE;
1246 columns[ncolumns++] = COL_USED;
1247 columns[ncolumns++] = COL_AVAIL;
1248 columns[ncolumns++] = COL_USEPERC;
1249 columns[ncolumns++] = COL_TARGET;
1250 }
1251
1252 /* default columns */
1253 if (!ncolumns) {
1254 if (flags & FL_POLL)
1255 columns[ncolumns++] = COL_ACTION;
1256
1257 columns[ncolumns++] = COL_TARGET;
1258 columns[ncolumns++] = COL_SOURCE;
1259 columns[ncolumns++] = COL_FSTYPE;
1260 columns[ncolumns++] = COL_OPTIONS;
1261 }
1262
1263 if (!tabtype)
1264 tabtype = TABTYPE_KERNEL;
1265
1266 if (flags & FL_POLL) {
1267 if (tabtype != TABTYPE_KERNEL)
1268 exclusive_option(&tabtype, tabtype + 1, "--{poll,fstab,mtab}");
1269 if (ntabfiles > 1)
1270 errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file"));
1271 }
1272
1273 if (optind < argc && (get_match(COL_SOURCE) || get_match(COL_TARGET)))
1274 errx(EXIT_FAILURE, _(
1275 "options --target and --source can't be used together "
1276 "with command line element that is not an option"));
1277
1278 if (optind < argc)
1279 set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */
1280 if (optind < argc)
1281 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
1282
1283 if ((flags & FL_SUBMOUNTS) && is_listall_mode())
1284 /* don't care about submounts if list all mounts */
1285 flags &= ~FL_SUBMOUNTS;
1286
1287 if (!(flags & FL_SUBMOUNTS) &&
1288 (!is_listall_mode() || (flags & FL_FIRSTONLY)))
1289 tt_flags &= ~TT_FL_TREE;
1290
1291 if (!(flags & FL_NOSWAPMATCH) &&
1292 !get_match(COL_TARGET) && get_match(COL_SOURCE)) {
1293 /*
1294 * Check if we can swap source and target, it's
1295 * not possible if the source is LABEL=/UUID=
1296 */
1297 const char *x = get_match(COL_SOURCE);
1298
1299 if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) ||
1300 !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9))
1301 flags |= FL_NOSWAPMATCH;
1302 }
1303
1304 /*
1305 * initialize libmount
1306 */
1307 mnt_init_debug(0);
1308
1309 tb = parse_tabfiles(tabfiles, ntabfiles, tabtype);
1310 if (!tb)
1311 goto leave;
1312
1313 if ((tt_flags & TT_FL_TREE) && (ntabfiles > 1 || !tab_is_tree(tb)))
1314 tt_flags &= ~TT_FL_TREE;
1315
1316 cache = mnt_new_cache();
1317 if (!cache) {
1318 warn(_("failed to initialize libmount cache"));
1319 goto leave;
1320 }
1321 mnt_table_set_cache(tb, cache);
1322
1323 if (tabtype == TABTYPE_KERNEL
1324 && (flags & FL_NOSWAPMATCH)
1325 && get_match(COL_TARGET))
1326 /*
1327 * enable extra functionality for target match
1328 */
1329 enable_extra_target_match();
1330
1331 /*
1332 * initialize output formatting (tt.h)
1333 */
1334 tt = tt_new_table(tt_flags);
1335 if (!tt) {
1336 warn(_("failed to initialize output table"));
1337 goto leave;
1338 }
1339
1340 for (i = 0; i < ncolumns; i++) {
1341 int fl = get_column_flags(i);
1342 int id = get_column_id(i);
1343
1344 if (!(tt_flags & TT_FL_TREE))
1345 fl &= ~TT_FL_TREE;
1346
1347 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
1348 warnx(_("%s column is requested, but --poll "
1349 "is not enabled"), get_column_name(i));
1350 goto leave;
1351 }
1352 if (!tt_define_column(tt, get_column_name(i),
1353 get_column_whint(i), fl)) {
1354 warn(_("failed to initialize output column"));
1355 goto leave;
1356 }
1357 }
1358
1359 /*
1360 * Fill in data to the output table
1361 */
1362 if (flags & FL_POLL) {
1363 /* poll mode (accept the first tabfile only) */
1364 rc = poll_table(tb, tabfiles ? *tabfiles : _PATH_PROC_MOUNTINFO, timeout, tt, direction);
1365
1366 } else if ((tt_flags & TT_FL_TREE) && is_listall_mode())
1367 /* whole tree */
1368 rc = create_treenode(tt, tb, NULL, NULL);
1369 else
1370 /* whole lits of sub-tree */
1371 rc = add_matching_lines(tb, tt, direction);
1372
1373 /*
1374 * Print the output table for non-poll modes
1375 */
1376 if (!rc && !(flags & FL_POLL))
1377 tt_print_table(tt);
1378 leave:
1379 tt_free_table(tt);
1380
1381 mnt_free_table(tb);
1382 mnt_free_cache(cache);
1383 free(tabfiles);
1384
1385 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
1386 }