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