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