]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/findmnt.c
build-sys: don't create empty man/ru directory
[thirdparty/util-linux.git] / misc-utils / findmnt.c
CommitLineData
04fd7a9f
KZ
1/*
2 * findmnt(8)
3 *
68164f6c 4 * Copyright (C) 2010,2011 Red Hat, Inc. All rights reserved.
04fd7a9f
KZ
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
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
04fd7a9f
KZ
21#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
04fd7a9f
KZ
24#include <unistd.h>
25#include <getopt.h>
26#include <string.h>
9d67679b 27#include <termios.h>
9d67679b
KZ
28#ifdef HAVE_SYS_IOCTL_H
29#include <sys/ioctl.h>
30#endif
9d67679b 31#include <assert.h>
32e5466a 32#include <poll.h>
9d67679b 33
2a1f429a 34#include <libmount.h>
fdedb45e 35
04fd7a9f
KZ
36#include "pathnames.h"
37#include "nls.h"
9d67679b 38#include "c.h"
fdedb45e 39#include "tt.h"
ad38fb9f 40#include "strutils.h"
04fd7a9f 41
9d67679b
KZ
42/* flags */
43enum {
44 FL_EVALUATE = (1 << 1),
45 FL_CANONICALIZE = (1 << 2),
46 FL_FIRSTONLY = (1 << 3),
47 FL_INVERT = (1 << 4),
9d67679b 48 FL_NOSWAPMATCH = (1 << 6),
b2214e1f 49 FL_NOFSROOT = (1 << 7),
049caefd 50 FL_SUBMOUNTS = (1 << 8),
32e5466a 51 FL_POLL = (1 << 9)
9d67679b
KZ
52};
53
54/* column IDs */
04fd7a9f
KZ
55enum {
56 COL_SOURCE,
57 COL_TARGET,
58 COL_FSTYPE,
59 COL_OPTIONS,
631280e0
KZ
60 COL_VFS_OPTIONS,
61 COL_FS_OPTIONS,
04fd7a9f
KZ
62 COL_LABEL,
63 COL_UUID,
46236388 64 COL_MAJMIN,
32e5466a
KZ
65 COL_ACTION,
66 COL_OLD_TARGET,
67 COL_OLD_OPTIONS,
04fd7a9f 68
af0dc7d3 69 FINDMNT_NCOLUMNS
04fd7a9f
KZ
70};
71
8c5eba3e
KZ
72enum {
73 TABTYPE_FSTAB = 1,
74 TABTYPE_MTAB,
75 TABTYPE_KERNEL
76};
77
9d67679b
KZ
78/* column names */
79struct colinfo {
80 const char *name; /* header */
81 double whint; /* width hint (N < 1 is in percent of termwidth) */
32e5466a 82 int flags; /* tt flags */
b152e359 83 const char *help; /* column description */
9d67679b 84 const char *match; /* pattern for match_func() */
04fd7a9f
KZ
85};
86
af0dc7d3
KZ
87/* columns descriptions (don't use const, this is writable) */
88static struct colinfo infos[FINDMNT_NCOLUMNS] = {
7aa69cf2
KZ
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") },
b152e359
KZ
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") },
04fd7a9f
KZ
101};
102
9d67679b 103/* global flags */
af0dc7d3
KZ
104static int flags;
105static int tt_flags;
9d67679b 106
fdedb45e 107/* array with IDs of enabled columns */
af0dc7d3
KZ
108static int columns[FINDMNT_NCOLUMNS];
109static int ncolumns;
9d67679b 110
582a5006 111/* poll actions (parsed --poll=<list> */
af0dc7d3
KZ
112#define FINDMNT_NACTIONS 4 /* mount, umount, move, remount */
113static int actions[FINDMNT_NACTIONS];
114static int nactions;
582a5006 115
9d67679b 116/* libmount cache */
af0dc7d3 117static struct libmnt_cache *cache;
04fd7a9f 118
00b4bcdf 119static int get_column_id(int num)
9d67679b 120{
9d67679b 121 assert(num < ncolumns);
af0dc7d3 122 assert(columns[num] < FINDMNT_NCOLUMNS);
fdedb45e 123 return columns[num];
9d67679b
KZ
124}
125
00b4bcdf 126static struct colinfo *get_column_info(int num)
9d67679b
KZ
127{
128 return &infos[ get_column_id(num) ];
129}
130
00b4bcdf 131static const char *column_id_to_name(int id)
9d67679b 132{
af0dc7d3 133 assert(id < FINDMNT_NCOLUMNS);
9d67679b
KZ
134 return infos[id].name;
135}
136
00b4bcdf 137static const char *get_column_name(int num)
9d67679b 138{
fdedb45e 139 return get_column_info(num)->name;
9d67679b
KZ
140}
141
00b4bcdf 142static float get_column_whint(int num)
9d67679b 143{
fdedb45e 144 return get_column_info(num)->whint;
9d67679b
KZ
145}
146
32e5466a 147static int get_column_flags(int num)
9d67679b 148{
32e5466a 149 return get_column_info(num)->flags;
9d67679b
KZ
150}
151
00b4bcdf 152static const char *get_match(int id)
9d67679b 153{
af0dc7d3 154 assert(id < FINDMNT_NCOLUMNS);
9d67679b
KZ
155 return infos[id].match;
156}
157
00b4bcdf 158static void set_match(int id, const char *match)
9d67679b 159{
af0dc7d3 160 assert(id < FINDMNT_NCOLUMNS);
9d67679b
KZ
161 infos[id].match = match;
162}
163
32e5466a
KZ
164static int is_tabdiff_column(int id)
165{
af0dc7d3 166 assert(id < FINDMNT_NCOLUMNS);
32e5466a
KZ
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
9d67679b
KZ
179/*
180 * "findmnt" without any filter
181 */
00b4bcdf 182static int is_listall_mode(void)
9d67679b
KZ
183{
184 return (!get_match(COL_SOURCE) &&
185 !get_match(COL_TARGET) &&
186 !get_match(COL_FSTYPE) &&
187 !get_match(COL_OPTIONS));
188}
189
582a5006
KZ
190/*
191 * Returns 1 if the @act is in the --poll=<list>
192 */
193static 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
205static 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
9d67679b
KZ
223/*
224 * findmnt --first-only <devname|TAG=|mountpoint>
225 *
226 * ... it works like "mount <devname|TAG=|mountpoint>"
227 */
00b4bcdf 228static int is_mount_compatible_mode(void)
9d67679b
KZ
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
32e5466a 240static void disable_columns_truncate(void)
9d67679b
KZ
241{
242 int i;
243
af0dc7d3 244 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
32e5466a 245 infos[i].flags &= ~TT_FL_TRUNC;
9d67679b
KZ
246}
247
04fd7a9f
KZ
248/*
249 * converts @name to column ID
250 */
9d67679b 251static int column_name_to_id(const char *name, size_t namesz)
04fd7a9f
KZ
252{
253 int i;
254
af0dc7d3 255 for (i = 0; i < FINDMNT_NCOLUMNS; i++) {
9d67679b 256 const char *cn = column_id_to_name(i);
04fd7a9f 257
9d67679b 258 if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
04fd7a9f
KZ
259 return i;
260 }
fdedb45e 261 warnx(_("unknown column: %s"), name);
04fd7a9f
KZ
262 return -1;
263}
264
fdedb45e 265/* Returns LABEL or UUID */
68164f6c 266static const char *get_tag(struct libmnt_fs *fs, const char *tagname)
9d67679b
KZ
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
46236388
KZ
283/* reads FS data from libmount
284 * TODO: add function that will deallocate data allocated by get_data()
285 */
68164f6c 286static const char *get_data(struct libmnt_fs *fs, int num)
9d67679b
KZ
287{
288 const char *str = NULL;
289
290 switch(get_column_id(num)) {
04fd7a9f 291 case COL_SOURCE:
b2214e1f
KZ
292 {
293 const char *root = mnt_fs_get_root(fs);
294
9d67679b 295 str = mnt_fs_get_srcpath(fs);
04fd7a9f 296
9d67679b
KZ
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 }
b2214e1f
KZ
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 }
04fd7a9f 311 break;
b2214e1f 312 }
04fd7a9f 313 case COL_TARGET:
fdedb45e 314 str = mnt_fs_get_target(fs);
04fd7a9f
KZ
315 break;
316 case COL_FSTYPE:
317 str = mnt_fs_get_fstype(fs);
318 break;
319 case COL_OPTIONS:
d783ee0b 320 str = mnt_fs_get_options(fs);
04fd7a9f 321 break;
631280e0 322 case COL_VFS_OPTIONS:
411fe06e 323 str = mnt_fs_get_vfs_options(fs);
631280e0
KZ
324 break;
325 case COL_FS_OPTIONS:
411fe06e 326 str = mnt_fs_get_fs_options(fs);
631280e0 327 break;
9d67679b
KZ
328 case COL_UUID:
329 str = get_tag(fs, "UUID");
330 break;
331 case COL_LABEL:
332 str = get_tag(fs, "LABEL");
333 break;
46236388
KZ
334 case COL_MAJMIN:
335 {
336 dev_t devno = mnt_fs_get_devno(fs);
337 if (devno) {
338 char *tmp;
339 int rc = 0;
49e9fd3a 340 if ((tt_flags & TT_FL_RAW) || (tt_flags & TT_FL_EXPORT))
68164f6c
KZ
341 rc = asprintf(&tmp, "%u:%u",
342 major(devno), minor(devno));
46236388 343 else
68164f6c
KZ
344 rc = asprintf(&tmp, "%3u:%-3u",
345 major(devno), minor(devno));
46236388
KZ
346 if (rc)
347 str = tmp;
348 }
349 }
04fd7a9f 350 default:
9d67679b 351 break;
04fd7a9f 352 }
fdedb45e 353 return str;
9d67679b
KZ
354}
355
32e5466a
KZ
356static 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:
77a1c5f7
KZ
384 if (old_fs && (change == MNT_TABDIFF_REMOUNT ||
385 change == MNT_TABDIFF_UMOUNT))
32e5466a
KZ
386 str = mnt_fs_get_options(old_fs);
387 break;
388 case COL_OLD_TARGET:
77a1c5f7
KZ
389 if (old_fs && (change == MNT_TABDIFF_MOVE ||
390 change == MNT_TABDIFF_UMOUNT))
32e5466a
KZ
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
fdedb45e 403/* adds one line to the output @tab */
68164f6c 404static struct tt_line *add_line(struct tt *tt, struct libmnt_fs *fs,
fdedb45e 405 struct tt_line *parent)
9d67679b 406{
fdedb45e
KZ
407 int i;
408 struct tt_line *line = tt_add_line(tt, parent);
9d67679b 409
fdedb45e
KZ
410 if (!line) {
411 warn(_("failed to add line to output"));
412 return NULL;
9d67679b 413 }
fdedb45e
KZ
414 for (i = 0; i < ncolumns; i++)
415 tt_line_set_data(line, i, get_data(fs, i));
9d67679b 416
049caefd 417 tt_line_set_userdata(line, fs);
fdedb45e 418 return line;
9d67679b
KZ
419}
420
32e5466a
KZ
421static 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
68164f6c 438static int has_line(struct tt *tt, struct libmnt_fs *fs)
049caefd
KZ
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);
68164f6c 444 if ((struct libmnt_fs *) ln->userdata == fs)
049caefd
KZ
445 return 1;
446 }
447 return 0;
448}
449
450/* reads filesystems from @tb (libmount) and fillin @tt (output table) */
68164f6c
KZ
451static int create_treenode(struct tt *tt, struct libmnt_table *tb,
452 struct libmnt_fs *fs, struct tt_line *parent_line)
04fd7a9f 453{
68164f6c
KZ
454 struct libmnt_fs *chld = NULL;
455 struct libmnt_iter *itr = NULL;
fdedb45e
KZ
456 struct tt_line *line;
457 int rc = -1;
9d67679b 458
fdedb45e
KZ
459 if (!fs) {
460 /* first call, get root FS */
68164f6c 461 if (mnt_table_get_root_fs(tb, &fs))
fdedb45e
KZ
462 goto leave;
463 parent_line = NULL;
049caefd
KZ
464
465 } else if ((flags & FL_SUBMOUNTS) && has_line(tt, fs))
466 return 0;
9d67679b 467
fdedb45e
KZ
468 itr = mnt_new_iter(MNT_ITER_FORWARD);
469 if (!itr)
470 goto leave;
9d67679b 471
fdedb45e
KZ
472 line = add_line(tt, fs, parent_line);
473 if (!line)
474 goto leave;
9d67679b 475
fdedb45e
KZ
476 /*
477 * add all children to the output table
478 */
68164f6c 479 while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) {
fdedb45e
KZ
480 if (create_treenode(tt, tb, chld, line))
481 goto leave;
9d67679b 482 }
fdedb45e
KZ
483 rc = 0;
484leave:
485 mnt_free_iter(itr);
486 return rc;
04fd7a9f
KZ
487}
488
20055151 489/* error callback */
6e5c0fc2
KZ
490static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
491 const char *filename, int line)
20055151 492{
b1b9f1c1 493 warnx(_("%s: parse error at line %d"), filename, line);
20055151
KZ
494 return 0;
495}
496
8c5eba3e
KZ
497static 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
fdedb45e 510/* calls libmount fstab/mtab/mountinfo parser */
8c5eba3e
KZ
511static struct libmnt_table *parse_tabfiles(char **files,
512 int nfiles,
513 int tabtype)
04fd7a9f 514{
8c5eba3e 515 struct libmnt_table *tb;
762ef0e1 516 int rc = 0;
20055151 517
8c5eba3e 518 tb = mnt_new_table();
fdedb45e 519 if (!tb) {
32e5466a 520 warn(_("failed to initialize libmount table"));
04fd7a9f 521 return NULL;
fdedb45e 522 }
68164f6c 523 mnt_table_set_parser_errcb(tb, parser_errcb);
20055151 524
8c5eba3e
KZ
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);
20055151 551
04fd7a9f 552 return tb;
04fd7a9f
KZ
553}
554
2f1ac44b
KZ
555/* checks if @tb contains parent->child relations */
556static 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
68164f6c 573/* filter function for libmount (mnt_table_find_next_fs()) */
6e5c0fc2
KZ
574static int match_func(struct libmnt_fs *fs,
575 void *data __attribute__ ((__unused__)))
04fd7a9f 576{
04fd7a9f 577 int rc = flags & FL_INVERT ? 1 : 0;
9d67679b 578 const char *m;
04fd7a9f 579
9d67679b
KZ
580 m = get_match(COL_TARGET);
581 if (m && !mnt_fs_match_target(fs, m, cache))
04fd7a9f 582 return rc;
9d67679b
KZ
583
584 m = get_match(COL_SOURCE);
585 if (m && !mnt_fs_match_source(fs, m, cache))
04fd7a9f 586 return rc;
9d67679b
KZ
587
588 m = get_match(COL_FSTYPE);
589 if (m && !mnt_fs_match_fstype(fs, m))
04fd7a9f 590 return rc;
9d67679b
KZ
591
592 m = get_match(COL_OPTIONS);
593 if (m && !mnt_fs_match_options(fs, m))
04fd7a9f
KZ
594 return rc;
595
596 return !rc;
597}
598
fdedb45e 599/* iterate over filesystems in @tb */
68164f6c
KZ
600static struct libmnt_fs *get_next_fs(struct libmnt_table *tb,
601 struct libmnt_iter *itr)
04fd7a9f 602{
68164f6c 603 struct libmnt_fs *fs = NULL;
9d67679b
KZ
604
605 if (is_listall_mode()) {
606 /*
607 * Print whole file
608 */
e3963f60
KZ
609 if (mnt_table_next_fs(tb, itr, &fs) != 0)
610 return NULL;
9d67679b
KZ
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 */
68164f6c 618 fs = mnt_table_find_source(tb, get_match(COL_SOURCE),
9d67679b 619 mnt_iter_get_direction(itr));
9a30c6ef
KZ
620
621 if (!fs && !(flags & FL_NOSWAPMATCH))
68164f6c 622 fs = mnt_table_find_target(tb, get_match(COL_SOURCE),
9d67679b
KZ
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 */
631again:
68164f6c 632 mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs);
9d67679b
KZ
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 }
9d67679b 646
fdedb45e 647 return fs;
04fd7a9f
KZ
648}
649
68164f6c
KZ
650static int add_matching_lines(struct libmnt_table *tb,
651 struct tt *tt, int direction)
049caefd 652{
68164f6c
KZ
653 struct libmnt_iter *itr = NULL;
654 struct libmnt_fs *fs;
049caefd
KZ
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;
678done:
679 mnt_free_iter(itr);
680 return rc;
681}
682
582a5006
KZ
683static 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
32e5466a
KZ
707static int poll_table(struct libmnt_table *tb, const char *tabfile,
708 int timeout, struct tt *tt, int direction)
709{
d1cabd5c 710 FILE *f = NULL;
32e5466a
KZ
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;
582a5006 753 int change, count;
32e5466a 754
582a5006
KZ
755 count = poll(fds, 1, timeout);
756 if (count == 0)
ad38fb9f 757 break; /* timeout */
582a5006 758 if (count < 0) {
32e5466a
KZ
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
582a5006 770 count = 0;
32e5466a
KZ
771 mnt_reset_iter(itr, direction);
772 while(mnt_tabdiff_next_change(
773 diff, itr, &old, &new, &change) == 0) {
774
582a5006
KZ
775 if (!has_poll_action(change))
776 continue;
777 if (!poll_match(new ? new : old))
778 continue;
779 count++;
32e5466a
KZ
780 rc = !add_tabdiff_line(tt, new, old, change);
781 if (rc)
782 goto done;
783 if (flags & FL_FIRSTONLY)
784 break;
785 }
786
582a5006
KZ
787 if (count) {
788 rc = tt_print_table(tt);
789 if (rc)
790 goto done;
791 }
32e5466a
KZ
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);
582a5006
KZ
800
801 if (count && (flags & FL_FIRSTONLY))
802 break;
32e5466a
KZ
803 }
804
805 rc = 0;
806done:
807 mnt_free_table(tb_new);
808 mnt_free_tabdiff(diff);
809 mnt_free_iter(itr);
d1cabd5c
KZ
810 if (f)
811 fclose(f);
32e5466a
KZ
812 return rc;
813}
814
abafd686 815static void __attribute__((__noreturn__)) usage(FILE *out)
04fd7a9f 816{
9ead0006
KZ
817 int i;
818
9d67679b
KZ
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);
04fd7a9f
KZ
826
827 fprintf(out, _(
9d67679b 828 "\nOptions:\n"
2f1ac44b 829 " -s, --fstab search in static table of filesystems\n"
cbec3cbf 830 " -m, --mtab search in table of mounted filesystems\n"
c3214059 831 " -k, --kernel search in kernel table of mounted\n"
415b61fc 832 " filesystems (default)\n\n"));
04fd7a9f 833
415b61fc 834 fprintf(out, _(
582a5006 835 " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n"
415b61fc 836 " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n"));
32e5466a 837
415b61fc 838 fprintf(out, _(
c3214059 839 " -a, --ascii use ASCII chars for tree formatting\n"
04fd7a9f 840 " -c, --canonicalize canonicalize printed paths\n"
c3214059
BS
841 " -d, --direction <word> direction of search, 'forward' or 'backward'\n"
842 " -e, --evaluate convert tags (LABEL/UUID) to device names\n"
2f1ac44b 843 " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n"
415b61fc
BS
844 " -f, --first-only print the first found filesystem only\n"));
845
846 fprintf(out, _(
c3214059
BS
847 " -h, --help display this help text and exit\n"
848 " -i, --invert invert the sense of matching\n"
46236388 849 " -l, --list use list format output\n"
c3214059 850 " -n, --noheadings don't print column headings\n"
415b61fc
BS
851 " -u, --notruncate don't truncate text in columns\n"));
852 fprintf(out, _(
04fd7a9f 853 " -O, --options <list> limit the set of filesystems by mount options\n"
c3214059 854 " -o, --output <list> the output columns to be shown\n"
49e9fd3a
KZ
855 " -P, --pairs use key=\"value\" output format\n"
856 " -r, --raw use raw output format\n"
415b61fc
BS
857 " -t, --types <list> limit the set of filesystems by FS types\n"));
858 fprintf(out, _(
b2214e1f 859 " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n"
c3214059
BS
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"));
04fd7a9f 863
9ead0006
KZ
864
865 fprintf(out, _("\nAvailable columns:\n"));
866
b152e359
KZ
867 for (i = 0; i < FINDMNT_NCOLUMNS; i++)
868 fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help));
9ead0006 869
9ead0006 870
04fd7a9f
KZ
871 fprintf(out, _("\nFor more information see findmnt(1).\n"));
872
873 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
874}
875
abafd686 876static void __attribute__((__noreturn__))
9d67679b
KZ
877errx_mutually_exclusive(const char *opts)
878{
879 errx(EXIT_FAILURE, "%s %s", opts, _("options are mutually exclusive"));
880}
881
04fd7a9f
KZ
882int main(int argc, char *argv[])
883{
68164f6c 884 struct libmnt_table *tb = NULL;
8c5eba3e 885 char **tabfiles = NULL;
9d67679b 886 int direction = MNT_ITER_FORWARD;
ad38fb9f 887 int i, c, rc = -1, timeout = -1;
8c5eba3e 888 int ntabfiles = 0, tabtype = 0;
fdedb45e
KZ
889
890 /* table.h */
891 struct tt *tt = NULL;
fdedb45e 892
6c7d5ae9 893 static const struct option longopts[] = {
9d67679b 894 { "ascii", 0, 0, 'a' },
04fd7a9f
KZ
895 { "canonicalize", 0, 0, 'c' },
896 { "direction", 1, 0, 'd' },
897 { "evaluate", 0, 0, 'e' },
9d67679b 898 { "first-only", 0, 0, 'f' },
2f1ac44b 899 { "fstab", 0, 0, 's' },
04fd7a9f
KZ
900 { "help", 0, 0, 'h' },
901 { "invert", 0, 0, 'i' },
9d67679b
KZ
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' },
04fd7a9f 907 { "options", 1, 0, 'O' },
9d67679b 908 { "output", 1, 0, 'o' },
582a5006 909 { "poll", 2, 0, 'p' },
49e9fd3a 910 { "pairs", 0, 0, 'P' },
9d67679b 911 { "raw", 0, 0, 'r' },
04fd7a9f 912 { "types", 1, 0, 't' },
b2214e1f 913 { "fsroot", 0, 0, 'v' },
049caefd 914 { "submounts", 0, 0, 'R' },
9d67679b 915 { "source", 1, 0, 'S' },
2f1ac44b 916 { "tab-file", 1, 0, 'F' },
9d67679b 917 { "target", 1, 0, 'T' },
ad38fb9f 918 { "timeout", 1, 0, 'w' },
9d67679b 919
04fd7a9f
KZ
920 { NULL, 0, 0, 0 }
921 };
922
af0dc7d3 923 assert(ARRAY_SIZE(columns) == FINDMNT_NCOLUMNS);
9d67679b 924
04fd7a9f
KZ
925 setlocale(LC_ALL, "");
926 bindtextdomain(PACKAGE, LOCALEDIR);
927 textdomain(PACKAGE);
928
9d67679b 929 /* default output format */
fdedb45e 930 tt_flags |= TT_FL_TREE;
04fd7a9f 931
9d67679b 932 while ((c = getopt_long(argc, argv,
2f1ac44b 933 "acd:ehifF:o:O:p::Pklmnrst:uvRS:T:w:",
d466c6a1 934 longopts, NULL)) != -1) {
04fd7a9f 935 switch(c) {
9d67679b 936 case 'a':
fdedb45e 937 tt_flags |= TT_FL_ASCII;
9d67679b 938 break;
04fd7a9f
KZ
939 case 'c':
940 flags |= FL_CANONICALIZE;
941 break;
942 case 'd':
4e6bd74c 943 if (!strcmp(optarg, "forward"))
04fd7a9f 944 direction = MNT_ITER_FORWARD;
4e6bd74c 945 else if (!strcmp(optarg, "backward"))
04fd7a9f
KZ
946 direction = MNT_ITER_BACKWARD;
947 else
948 errx(EXIT_FAILURE,
8e350e48 949 _("unknown direction '%s'"), optarg);
04fd7a9f
KZ
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;
9d67679b 960 case 'f':
04fd7a9f
KZ
961 flags |= FL_FIRSTONLY;
962 break;
2f1ac44b 963 case 'F':
8c5eba3e 964 tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg);
2f1ac44b 965 break;
9d67679b 966 case 'u':
32e5466a 967 disable_columns_truncate();
9d67679b 968 break;
04fd7a9f 969 case 'o':
c87638ad 970 ncolumns = string_to_idarray(optarg,
bdc3ed66
KZ
971 columns, ARRAY_SIZE(columns),
972 column_name_to_id);
973 if (ncolumns < 0)
fdedb45e 974 exit(EXIT_FAILURE);
04fd7a9f
KZ
975 break;
976 case 'O':
9d67679b 977 set_match(COL_OPTIONS, optarg);
04fd7a9f 978 break;
32e5466a 979 case 'p':
bdc3ed66 980 if (optarg) {
c87638ad 981 nactions = string_to_idarray(optarg,
bdc3ed66
KZ
982 actions, ARRAY_SIZE(actions),
983 poll_action_name_to_id);
984 if (nactions < 0)
985 exit(EXIT_FAILURE);
986 }
32e5466a
KZ
987 flags |= FL_POLL;
988 tt_flags &= ~TT_FL_TREE;
989 break;
49e9fd3a
KZ
990 case 'P':
991 tt_flags |= TT_FL_EXPORT;
992 tt_flags &= ~TT_FL_TREE;
993 break;
fdedb45e 994 case 'm': /* mtab */
8c5eba3e 995 if (tabtype)
9d67679b 996 errx_mutually_exclusive("--{fstab,mtab,kernel}");
8c5eba3e 997 tabtype = TABTYPE_MTAB;
fdedb45e 998 tt_flags &= ~TT_FL_TREE;
04fd7a9f 999 break;
fdedb45e 1000 case 's': /* fstab */
8c5eba3e 1001 if (tabtype)
9d67679b 1002 errx_mutually_exclusive("--{fstab,mtab,kernel}");
8c5eba3e 1003 tabtype = TABTYPE_FSTAB;
ac808156 1004 tt_flags &= ~TT_FL_TREE;
04fd7a9f 1005 break;
fdedb45e 1006 case 'k': /* kernel (mountinfo) */
8c5eba3e 1007 if (tabtype)
9d67679b 1008 errx_mutually_exclusive("--{fstab,mtab,kernel}");
8c5eba3e 1009 tabtype = TABTYPE_KERNEL;
04fd7a9f
KZ
1010 break;
1011 case 't':
9d67679b
KZ
1012 set_match(COL_FSTYPE, optarg);
1013 break;
1014 case 'r':
fdedb45e
KZ
1015 tt_flags &= ~TT_FL_TREE; /* disable the default */
1016 tt_flags |= TT_FL_RAW; /* enable raw */
9d67679b
KZ
1017 break;
1018 case 'l':
49e9fd3a
KZ
1019 if ((tt_flags & TT_FL_RAW) && (tt_flags & TT_FL_EXPORT))
1020 errx_mutually_exclusive("--{raw,list,pairs}");
9d67679b 1021
fdedb45e 1022 tt_flags &= ~TT_FL_TREE; /* disable the default */
9d67679b
KZ
1023 break;
1024 case 'n':
fdedb45e 1025 tt_flags |= TT_FL_NOHEADINGS;
9d67679b 1026 break;
b2214e1f
KZ
1027 case 'v':
1028 flags |= FL_NOFSROOT;
1029 break;
049caefd
KZ
1030 case 'R':
1031 flags |= FL_SUBMOUNTS;
1032 break;
9d67679b
KZ
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;
04fd7a9f 1040 break;
ad38fb9f
KZ
1041 case 'w':
1042 timeout = strtol_or_err(optarg,
1043 _("failed to parse timeout"));
1044 break;
04fd7a9f
KZ
1045 default:
1046 usage(stderr);
1047 break;
1048 }
1049 }
1050
32e5466a
KZ
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
8c5eba3e
KZ
1062 if (!tabtype)
1063 tabtype = TABTYPE_KERNEL;
2f1ac44b 1064
9d67679b 1065
8c5eba3e
KZ
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"));
fdedb45e 1071 }
b2214e1f 1072
9d67679b
KZ
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
04fd7a9f 1078 if (optind < argc)
9d67679b 1079 set_match(COL_SOURCE, argv[optind++]); /* dev/tag/mountpoint */
04fd7a9f 1080 if (optind < argc)
9d67679b 1081 set_match(COL_TARGET, argv[optind++]); /* mountpoint */
04fd7a9f 1082
049caefd
KZ
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)))
fdedb45e
KZ
1089 tt_flags &= ~TT_FL_TREE;
1090
ac808156
KZ
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
fdedb45e
KZ
1103 /*
1104 * initialize libmount
1105 */
ac808156
KZ
1106 mnt_init_debug(0);
1107
8c5eba3e 1108 tb = parse_tabfiles(tabfiles, ntabfiles, tabtype);
04fd7a9f 1109 if (!tb)
fdedb45e 1110 goto leave;
04fd7a9f 1111
2f1ac44b
KZ
1112 if ((tt_flags & TT_FL_TREE) && !tab_is_tree(tb))
1113 tt_flags &= ~TT_FL_TREE;
1114
04fd7a9f 1115 cache = mnt_new_cache();
fdedb45e
KZ
1116 if (!cache) {
1117 warn(_("failed to initialize libmount cache"));
1118 goto leave;
1119 }
68164f6c 1120 mnt_table_set_cache(tb, cache);
04fd7a9f 1121
fdedb45e 1122 /*
00b4bcdf 1123 * initialize output formatting (tt.h)
fdedb45e
KZ
1124 */
1125 tt = tt_new_table(tt_flags);
1126 if (!tt) {
1127 warn(_("failed to initialize output table"));
1128 goto leave;
1129 }
ac808156 1130
fdedb45e 1131 for (i = 0; i < ncolumns; i++) {
32e5466a
KZ
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;
04fd7a9f 1137
32e5466a 1138 if (!(flags & FL_POLL) && is_tabdiff_column(id)) {
77a1c5f7 1139 warnx(_("%s column is requested, but --poll "
32e5466a
KZ
1140 "is not enabled"), get_column_name(i));
1141 goto leave;
1142 }
fdedb45e
KZ
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;
9d67679b 1147 }
fdedb45e 1148 }
9d67679b 1149
fdedb45e
KZ
1150 /*
1151 * Fill in data to the output table
1152 */
275c9a48 1153 if (flags & FL_POLL) {
8c5eba3e 1154 /* poll mode (accept the first tabfile only) */
275c9a48
KZ
1155 if (tabfiles && ntabfiles > 0)
1156 rc = poll_table(tb, *tabfiles, timeout, tt, direction);
32e5466a 1157
275c9a48 1158 } else if ((tt_flags & TT_FL_TREE) && is_listall_mode())
049caefd
KZ
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);
fdedb45e
KZ
1164
1165 /*
32e5466a 1166 * Print the output table for non-poll modes
fdedb45e 1167 */
32e5466a 1168 if (!rc && !(flags & FL_POLL))
049caefd 1169 tt_print_table(tt);
fdedb45e
KZ
1170leave:
1171 tt_free_table(tt);
1172
68164f6c 1173 mnt_free_table(tb);
04fd7a9f 1174 mnt_free_cache(cache);
8c5eba3e 1175 free(tabfiles);
04fd7a9f 1176
049caefd 1177 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
04fd7a9f 1178}