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