]>
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 | ||
4bfd4bff KZ |
195 | /* |
196 | * source match means COL_SOURCE *or* COL_MAJMIN, depends on | |
197 | * data format. | |
198 | */ | |
cd492186 KZ |
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); | |
4bfd4bff | 209 | flags |= FL_NOSWAPMATCH; |
cd492186 KZ |
210 | } else |
211 | set_match(COL_SOURCE, data); | |
212 | } | |
213 | ||
4bfd4bff KZ |
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 | ||
cd492186 | 234 | |
32e5466a KZ |
235 | static int is_tabdiff_column(int id) |
236 | { | |
af0dc7d3 | 237 | assert(id < FINDMNT_NCOLUMNS); |
32e5466a KZ |
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 | ||
9d67679b KZ |
250 | /* |
251 | * "findmnt" without any filter | |
252 | */ | |
00b4bcdf | 253 | static int is_listall_mode(void) |
9d67679b | 254 | { |
cd4ed46f KZ |
255 | if ((flags & FL_DF) && !(flags & FL_ALL)) |
256 | return 0; | |
257 | ||
9d67679b KZ |
258 | return (!get_match(COL_SOURCE) && |
259 | !get_match(COL_TARGET) && | |
260 | !get_match(COL_FSTYPE) && | |
cd492186 KZ |
261 | !get_match(COL_OPTIONS) && |
262 | !get_match(COL_MAJMIN)); | |
9d67679b KZ |
263 | } |
264 | ||
582a5006 KZ |
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 | ||
9d67679b KZ |
298 | /* |
299 | * findmnt --first-only <devname|TAG=|mountpoint> | |
300 | * | |
301 | * ... it works like "mount <devname|TAG=|mountpoint>" | |
302 | */ | |
00b4bcdf | 303 | static int is_mount_compatible_mode(void) |
9d67679b KZ |
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 | ||
32e5466a | 315 | static void disable_columns_truncate(void) |
9d67679b KZ |
316 | { |
317 | int i; | |
318 | ||
af0dc7d3 | 319 | for (i = 0; i < FINDMNT_NCOLUMNS; i++) |
32e5466a | 320 | infos[i].flags &= ~TT_FL_TRUNC; |
9d67679b KZ |
321 | } |
322 | ||
04fd7a9f KZ |
323 | /* |
324 | * converts @name to column ID | |
325 | */ | |
9d67679b | 326 | static int column_name_to_id(const char *name, size_t namesz) |
04fd7a9f KZ |
327 | { |
328 | int i; | |
329 | ||
af0dc7d3 | 330 | for (i = 0; i < FINDMNT_NCOLUMNS; i++) { |
9d67679b | 331 | const char *cn = column_id_to_name(i); |
04fd7a9f | 332 | |
9d67679b | 333 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) |
04fd7a9f KZ |
334 | return i; |
335 | } | |
fdedb45e | 336 | warnx(_("unknown column: %s"), name); |
04fd7a9f KZ |
337 | return -1; |
338 | } | |
339 | ||
fdedb45e | 340 | /* Returns LABEL or UUID */ |
68164f6c | 341 | static const char *get_tag(struct libmnt_fs *fs, const char *tagname) |
9d67679b KZ |
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 | ||
a85ef33b DR |
358 | static const char *get_vfs_attr(struct libmnt_fs *fs, int sizetype) |
359 | { | |
360 | struct statvfs buf; | |
6901f22c | 361 | uint64_t vfs_attr = 0; |
a85ef33b DR |
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 | ||
d7bcb205 KZ |
381 | xasprintf(&sizestr, "%.0f%%", |
382 | (double)(buf.f_blocks - buf.f_bfree) / | |
383 | buf.f_blocks * 100); | |
a85ef33b DR |
384 | return sizestr; |
385 | } | |
386 | ||
387 | return vfs_attr == 0 ? "0" : | |
388 | size_to_human_string(SIZE_SUFFIX_1LETTER, vfs_attr); | |
389 | } | |
390 | ||
46236388 | 391 | /* reads FS data from libmount |
46236388 | 392 | */ |
68164f6c | 393 | static const char *get_data(struct libmnt_fs *fs, int num) |
9d67679b KZ |
394 | { |
395 | const char *str = NULL; | |
a85ef33b | 396 | int col_id = get_column_id(num); |
9d67679b | 397 | |
a85ef33b | 398 | switch (col_id) { |
04fd7a9f | 399 | case COL_SOURCE: |
b2214e1f KZ |
400 | { |
401 | const char *root = mnt_fs_get_root(fs); | |
402 | ||
9d67679b | 403 | str = mnt_fs_get_srcpath(fs); |
04fd7a9f | 404 | |
9d67679b KZ |
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 | } | |
b2214e1f KZ |
413 | if (root && str && !(flags & FL_NOFSROOT) && strcmp(root, "/")) { |
414 | char *tmp; | |
415 | ||
6f312c89 | 416 | if (xasprintf(&tmp, "%s[%s]", str, root) > 0) |
b2214e1f KZ |
417 | str = tmp; |
418 | } | |
04fd7a9f | 419 | break; |
b2214e1f | 420 | } |
04fd7a9f | 421 | case COL_TARGET: |
fdedb45e | 422 | str = mnt_fs_get_target(fs); |
04fd7a9f KZ |
423 | break; |
424 | case COL_FSTYPE: | |
425 | str = mnt_fs_get_fstype(fs); | |
426 | break; | |
427 | case COL_OPTIONS: | |
d783ee0b | 428 | str = mnt_fs_get_options(fs); |
04fd7a9f | 429 | break; |
631280e0 | 430 | case COL_VFS_OPTIONS: |
411fe06e | 431 | str = mnt_fs_get_vfs_options(fs); |
631280e0 KZ |
432 | break; |
433 | case COL_FS_OPTIONS: | |
411fe06e | 434 | str = mnt_fs_get_fs_options(fs); |
631280e0 | 435 | break; |
9d67679b KZ |
436 | case COL_UUID: |
437 | str = get_tag(fs, "UUID"); | |
438 | break; | |
090b5e84 KZ |
439 | case COL_PARTUUID: |
440 | str = get_tag(fs, "PARTUUID"); | |
441 | break; | |
9d67679b KZ |
442 | case COL_LABEL: |
443 | str = get_tag(fs, "LABEL"); | |
444 | break; | |
090b5e84 KZ |
445 | case COL_PARTLABEL: |
446 | str = get_tag(fs, "PARTLABEL"); | |
447 | break; | |
448 | ||
46236388 KZ |
449 | case COL_MAJMIN: |
450 | { | |
451 | dev_t devno = mnt_fs_get_devno(fs); | |
452 | if (devno) { | |
453 | char *tmp; | |
454 | int rc = 0; | |
49e9fd3a | 455 | if ((tt_flags & TT_FL_RAW) || (tt_flags & TT_FL_EXPORT)) |
6f312c89 | 456 | rc = xasprintf(&tmp, "%u:%u", |
68164f6c | 457 | major(devno), minor(devno)); |
46236388 | 458 | else |
6f312c89 | 459 | rc = xasprintf(&tmp, "%3u:%-3u", |
68164f6c | 460 | major(devno), minor(devno)); |
46236388 KZ |
461 | if (rc) |
462 | str = tmp; | |
463 | } | |
a85ef33b | 464 | break; |
46236388 | 465 | } |
a85ef33b DR |
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; | |
04fd7a9f | 472 | default: |
9d67679b | 473 | break; |
04fd7a9f | 474 | } |
fdedb45e | 475 | return str; |
9d67679b KZ |
476 | } |
477 | ||
32e5466a KZ |
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: | |
77a1c5f7 KZ |
506 | if (old_fs && (change == MNT_TABDIFF_REMOUNT || |
507 | change == MNT_TABDIFF_UMOUNT)) | |
32e5466a KZ |
508 | str = mnt_fs_get_options(old_fs); |
509 | break; | |
510 | case COL_OLD_TARGET: | |
77a1c5f7 KZ |
511 | if (old_fs && (change == MNT_TABDIFF_MOVE || |
512 | change == MNT_TABDIFF_UMOUNT)) | |
32e5466a KZ |
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 | ||
fdedb45e | 525 | /* adds one line to the output @tab */ |
68164f6c | 526 | static struct tt_line *add_line(struct tt *tt, struct libmnt_fs *fs, |
fdedb45e | 527 | struct tt_line *parent) |
9d67679b | 528 | { |
fdedb45e KZ |
529 | int i; |
530 | struct tt_line *line = tt_add_line(tt, parent); | |
9d67679b | 531 | |
fdedb45e KZ |
532 | if (!line) { |
533 | warn(_("failed to add line to output")); | |
534 | return NULL; | |
9d67679b | 535 | } |
fdedb45e KZ |
536 | for (i = 0; i < ncolumns; i++) |
537 | tt_line_set_data(line, i, get_data(fs, i)); | |
9d67679b | 538 | |
049caefd | 539 | tt_line_set_userdata(line, fs); |
fdedb45e | 540 | return line; |
9d67679b KZ |
541 | } |
542 | ||
32e5466a KZ |
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 | ||
68164f6c | 560 | static int has_line(struct tt *tt, struct libmnt_fs *fs) |
049caefd KZ |
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); | |
68164f6c | 566 | if ((struct libmnt_fs *) ln->userdata == fs) |
049caefd KZ |
567 | return 1; |
568 | } | |
569 | return 0; | |
570 | } | |
571 | ||
572 | /* reads filesystems from @tb (libmount) and fillin @tt (output table) */ | |
68164f6c KZ |
573 | static int create_treenode(struct tt *tt, struct libmnt_table *tb, |
574 | struct libmnt_fs *fs, struct tt_line *parent_line) | |
04fd7a9f | 575 | { |
68164f6c KZ |
576 | struct libmnt_fs *chld = NULL; |
577 | struct libmnt_iter *itr = NULL; | |
fdedb45e KZ |
578 | struct tt_line *line; |
579 | int rc = -1; | |
9d67679b | 580 | |
fdedb45e KZ |
581 | if (!fs) { |
582 | /* first call, get root FS */ | |
68164f6c | 583 | if (mnt_table_get_root_fs(tb, &fs)) |
fdedb45e KZ |
584 | goto leave; |
585 | parent_line = NULL; | |
049caefd KZ |
586 | |
587 | } else if ((flags & FL_SUBMOUNTS) && has_line(tt, fs)) | |
588 | return 0; | |
9d67679b | 589 | |
fdedb45e KZ |
590 | itr = mnt_new_iter(MNT_ITER_FORWARD); |
591 | if (!itr) | |
592 | goto leave; | |
9d67679b | 593 | |
fdedb45e KZ |
594 | line = add_line(tt, fs, parent_line); |
595 | if (!line) | |
596 | goto leave; | |
9d67679b | 597 | |
fdedb45e KZ |
598 | /* |
599 | * add all children to the output table | |
600 | */ | |
68164f6c | 601 | while(mnt_table_next_child_fs(tb, itr, fs, &chld) == 0) { |
fdedb45e KZ |
602 | if (create_treenode(tt, tb, chld, line)) |
603 | goto leave; | |
9d67679b | 604 | } |
fdedb45e KZ |
605 | rc = 0; |
606 | leave: | |
607 | mnt_free_iter(itr); | |
608 | return rc; | |
04fd7a9f KZ |
609 | } |
610 | ||
20055151 | 611 | /* error callback */ |
6e5c0fc2 KZ |
612 | static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)), |
613 | const char *filename, int line) | |
20055151 | 614 | { |
b1b9f1c1 | 615 | warnx(_("%s: parse error at line %d"), filename, line); |
20055151 KZ |
616 | return 0; |
617 | } | |
618 | ||
8c5eba3e KZ |
619 | static char **append_tabfile(char **files, int *nfiles, char *filename) |
620 | { | |
99d618c0 | 621 | files = xrealloc(files, sizeof(char *) * (*nfiles + 1)); |
8c5eba3e KZ |
622 | files[(*nfiles)++] = filename; |
623 | return files; | |
624 | } | |
625 | ||
fdedb45e | 626 | /* calls libmount fstab/mtab/mountinfo parser */ |
8c5eba3e KZ |
627 | static struct libmnt_table *parse_tabfiles(char **files, |
628 | int nfiles, | |
629 | int tabtype) | |
04fd7a9f | 630 | { |
8c5eba3e | 631 | struct libmnt_table *tb; |
762ef0e1 | 632 | int rc = 0; |
20055151 | 633 | |
8c5eba3e | 634 | tb = mnt_new_table(); |
fdedb45e | 635 | if (!tb) { |
32e5466a | 636 | warn(_("failed to initialize libmount table")); |
04fd7a9f | 637 | return NULL; |
fdedb45e | 638 | } |
68164f6c | 639 | mnt_table_set_parser_errcb(tb, parser_errcb); |
20055151 | 640 | |
8c5eba3e KZ |
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); | |
20055151 | 667 | |
04fd7a9f | 668 | return tb; |
04fd7a9f KZ |
669 | } |
670 | ||
2f1ac44b KZ |
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 | ||
b215d8e9 | 689 | |
68164f6c | 690 | /* filter function for libmount (mnt_table_find_next_fs()) */ |
6e5c0fc2 KZ |
691 | static int match_func(struct libmnt_fs *fs, |
692 | void *data __attribute__ ((__unused__))) | |
04fd7a9f | 693 | { |
04fd7a9f | 694 | int rc = flags & FL_INVERT ? 1 : 0; |
9d67679b | 695 | const char *m; |
cd492186 | 696 | void *md; |
04fd7a9f | 697 | |
9d67679b | 698 | m = get_match(COL_TARGET); |
4bfd4bff | 699 | if (m && !mnt_fs_match_target(fs, m, cache)) |
04fd7a9f | 700 | return rc; |
9d67679b KZ |
701 | |
702 | m = get_match(COL_SOURCE); | |
4bfd4bff | 703 | if (m && !mnt_fs_match_source(fs, m, cache)) |
04fd7a9f | 704 | return rc; |
9d67679b KZ |
705 | |
706 | m = get_match(COL_FSTYPE); | |
707 | if (m && !mnt_fs_match_fstype(fs, m)) | |
04fd7a9f | 708 | return rc; |
9d67679b KZ |
709 | |
710 | m = get_match(COL_OPTIONS); | |
711 | if (m && !mnt_fs_match_options(fs, m)) | |
04fd7a9f KZ |
712 | return rc; |
713 | ||
cd492186 KZ |
714 | md = get_match_data(COL_MAJMIN); |
715 | if (md && mnt_fs_get_devno(fs) != *((dev_t *) md)) | |
716 | return rc; | |
717 | ||
cd4ed46f KZ |
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 | ||
04fd7a9f KZ |
728 | return !rc; |
729 | } | |
730 | ||
fdedb45e | 731 | /* iterate over filesystems in @tb */ |
68164f6c KZ |
732 | static struct libmnt_fs *get_next_fs(struct libmnt_table *tb, |
733 | struct libmnt_iter *itr) | |
04fd7a9f | 734 | { |
68164f6c | 735 | struct libmnt_fs *fs = NULL; |
9d67679b KZ |
736 | |
737 | if (is_listall_mode()) { | |
738 | /* | |
739 | * Print whole file | |
740 | */ | |
e3963f60 KZ |
741 | if (mnt_table_next_fs(tb, itr, &fs) != 0) |
742 | return NULL; | |
9d67679b KZ |
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 | */ | |
68164f6c | 750 | fs = mnt_table_find_source(tb, get_match(COL_SOURCE), |
9d67679b | 751 | mnt_iter_get_direction(itr)); |
9a30c6ef KZ |
752 | |
753 | if (!fs && !(flags & FL_NOSWAPMATCH)) | |
68164f6c | 754 | fs = mnt_table_find_target(tb, get_match(COL_SOURCE), |
9d67679b KZ |
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: | |
68164f6c | 764 | mnt_table_find_next_fs(tb, itr, match_func, NULL, &fs); |
9d67679b KZ |
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 | } | |
9d67679b | 778 | |
fdedb45e | 779 | return fs; |
04fd7a9f KZ |
780 | } |
781 | ||
68164f6c KZ |
782 | static int add_matching_lines(struct libmnt_table *tb, |
783 | struct tt *tt, int direction) | |
049caefd | 784 | { |
68164f6c KZ |
785 | struct libmnt_iter *itr = NULL; |
786 | struct libmnt_fs *fs; | |
049caefd KZ |
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 | ||
582a5006 KZ |
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 | |
cd492186 | 823 | * The '/foo' maybe source as well as target. |
582a5006 KZ |
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 | ||
32e5466a KZ |
839 | static int poll_table(struct libmnt_table *tb, const char *tabfile, |
840 | int timeout, struct tt *tt, int direction) | |
841 | { | |
d1cabd5c | 842 | FILE *f = NULL; |
32e5466a KZ |
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; | |
582a5006 | 885 | int change, count; |
32e5466a | 886 | |
582a5006 KZ |
887 | count = poll(fds, 1, timeout); |
888 | if (count == 0) | |
ad38fb9f | 889 | break; /* timeout */ |
582a5006 | 890 | if (count < 0) { |
32e5466a KZ |
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 | ||
582a5006 | 902 | count = 0; |
32e5466a KZ |
903 | mnt_reset_iter(itr, direction); |
904 | while(mnt_tabdiff_next_change( | |
905 | diff, itr, &old, &new, &change) == 0) { | |
906 | ||
582a5006 KZ |
907 | if (!has_poll_action(change)) |
908 | continue; | |
909 | if (!poll_match(new ? new : old)) | |
910 | continue; | |
911 | count++; | |
32e5466a KZ |
912 | rc = !add_tabdiff_line(tt, new, old, change); |
913 | if (rc) | |
914 | goto done; | |
915 | if (flags & FL_FIRSTONLY) | |
916 | break; | |
917 | } | |
918 | ||
582a5006 KZ |
919 | if (count) { |
920 | rc = tt_print_table(tt); | |
921 | if (rc) | |
922 | goto done; | |
923 | } | |
32e5466a KZ |
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); | |
582a5006 KZ |
932 | |
933 | if (count && (flags & FL_FIRSTONLY)) | |
934 | break; | |
32e5466a KZ |
935 | } |
936 | ||
937 | rc = 0; | |
938 | done: | |
939 | mnt_free_table(tb_new); | |
940 | mnt_free_tabdiff(diff); | |
941 | mnt_free_iter(itr); | |
d1cabd5c KZ |
942 | if (f) |
943 | fclose(f); | |
32e5466a KZ |
944 | return rc; |
945 | } | |
946 | ||
abafd686 | 947 | static void __attribute__((__noreturn__)) usage(FILE *out) |
04fd7a9f | 948 | { |
9ead0006 KZ |
949 | int i; |
950 | ||
5cc12c40 | 951 | fputs(USAGE_HEADER, out); |
9d67679b | 952 | fprintf(out, _( |
9d67679b KZ |
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); | |
04fd7a9f KZ |
958 | |
959 | fprintf(out, _( | |
9d67679b | 960 | "\nOptions:\n" |
2f1ac44b | 961 | " -s, --fstab search in static table of filesystems\n" |
cbec3cbf | 962 | " -m, --mtab search in table of mounted filesystems\n" |
c3214059 | 963 | " -k, --kernel search in kernel table of mounted\n" |
415b61fc | 964 | " filesystems (default)\n\n")); |
04fd7a9f | 965 | |
415b61fc | 966 | fprintf(out, _( |
582a5006 | 967 | " -p, --poll[=<list>] monitor changes in table of mounted filesystems\n" |
415b61fc | 968 | " -w, --timeout <num> upper limit in milliseconds that --poll will block\n\n")); |
32e5466a | 969 | |
415b61fc | 970 | fprintf(out, _( |
cd4ed46f | 971 | " -A, --all disable all built-in filters, print all filesystems\n" |
c3214059 | 972 | " -a, --ascii use ASCII chars for tree formatting\n" |
04fd7a9f | 973 | " -c, --canonicalize canonicalize printed paths\n" |
eda399b9 | 974 | " -D, --df imitate the output of df(1)\n" |
c3214059 | 975 | " -d, --direction <word> direction of search, 'forward' or 'backward'\n" |
cd492186 KZ |
976 | " -e, --evaluate convert tags (LABEL,UUID,PARTUUID,PARTLABEL) \n" |
977 | " to device names\n" | |
2f1ac44b | 978 | " -F, --tab-file <path> alternative file for --fstab, --mtab or --kernel options\n" |
415b61fc BS |
979 | " -f, --first-only print the first found filesystem only\n")); |
980 | ||
981 | fprintf(out, _( | |
c3214059 | 982 | " -i, --invert invert the sense of matching\n" |
46236388 | 983 | " -l, --list use list format output\n" |
c3214059 | 984 | " -n, --noheadings don't print column headings\n" |
415b61fc BS |
985 | " -u, --notruncate don't truncate text in columns\n")); |
986 | fprintf(out, _( | |
04fd7a9f | 987 | " -O, --options <list> limit the set of filesystems by mount options\n" |
c3214059 | 988 | " -o, --output <list> the output columns to be shown\n" |
49e9fd3a KZ |
989 | " -P, --pairs use key=\"value\" output format\n" |
990 | " -r, --raw use raw output format\n" | |
415b61fc BS |
991 | " -t, --types <list> limit the set of filesystems by FS types\n")); |
992 | fprintf(out, _( | |
b2214e1f | 993 | " -v, --nofsroot don't print [/dir] for bind or btrfs mounts\n" |
c3214059 | 994 | " -R, --submounts print all submounts for the matching filesystems\n" |
cd492186 KZ |
995 | " -S, --source <string> the device to mount (by name, maj:min, \n" |
996 | " LABEL=, UUID=, PARTUUID=, PARTLABEL=)\n" | |
5cc12c40 | 997 | " -T, --target <string> the mountpoint to use\n")); |
04fd7a9f | 998 | |
5cc12c40 SK |
999 | fputs(USAGE_SEPARATOR, out); |
1000 | fputs(USAGE_HELP, out); | |
1001 | fputs(USAGE_VERSION, out); | |
9ead0006 KZ |
1002 | |
1003 | fprintf(out, _("\nAvailable columns:\n")); | |
1004 | ||
b152e359 KZ |
1005 | for (i = 0; i < FINDMNT_NCOLUMNS; i++) |
1006 | fprintf(out, " %11s %s\n", infos[i].name, _(infos[i].help)); | |
9ead0006 | 1007 | |
beda9dd3 | 1008 | fprintf(out, USAGE_MAN_TAIL("findmnt(8)")); |
04fd7a9f KZ |
1009 | |
1010 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
1011 | } | |
1012 | ||
abafd686 | 1013 | static void __attribute__((__noreturn__)) |
9d67679b KZ |
1014 | errx_mutually_exclusive(const char *opts) |
1015 | { | |
1016 | errx(EXIT_FAILURE, "%s %s", opts, _("options are mutually exclusive")); | |
1017 | } | |
1018 | ||
04fd7a9f KZ |
1019 | int main(int argc, char *argv[]) |
1020 | { | |
68164f6c | 1021 | struct libmnt_table *tb = NULL; |
8c5eba3e | 1022 | char **tabfiles = NULL; |
9d67679b | 1023 | int direction = MNT_ITER_FORWARD; |
cd4ed46f | 1024 | int i, c, rc = -1, timeout = -1; |
8c5eba3e | 1025 | int ntabfiles = 0, tabtype = 0; |
fdedb45e KZ |
1026 | |
1027 | /* table.h */ | |
1028 | struct tt *tt = NULL; | |
fdedb45e | 1029 | |
6c7d5ae9 | 1030 | static const struct option longopts[] = { |
cd4ed46f | 1031 | { "all", 0, 0, 'A' }, |
9d67679b | 1032 | { "ascii", 0, 0, 'a' }, |
04fd7a9f KZ |
1033 | { "canonicalize", 0, 0, 'c' }, |
1034 | { "direction", 1, 0, 'd' }, | |
eda399b9 | 1035 | { "df", 0, 0, 'D' }, |
04fd7a9f | 1036 | { "evaluate", 0, 0, 'e' }, |
9d67679b | 1037 | { "first-only", 0, 0, 'f' }, |
2f1ac44b | 1038 | { "fstab", 0, 0, 's' }, |
04fd7a9f KZ |
1039 | { "help", 0, 0, 'h' }, |
1040 | { "invert", 0, 0, 'i' }, | |
9d67679b KZ |
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' }, | |
04fd7a9f | 1046 | { "options", 1, 0, 'O' }, |
9d67679b | 1047 | { "output", 1, 0, 'o' }, |
582a5006 | 1048 | { "poll", 2, 0, 'p' }, |
49e9fd3a | 1049 | { "pairs", 0, 0, 'P' }, |
9d67679b | 1050 | { "raw", 0, 0, 'r' }, |
04fd7a9f | 1051 | { "types", 1, 0, 't' }, |
b2214e1f | 1052 | { "fsroot", 0, 0, 'v' }, |
049caefd | 1053 | { "submounts", 0, 0, 'R' }, |
9d67679b | 1054 | { "source", 1, 0, 'S' }, |
2f1ac44b | 1055 | { "tab-file", 1, 0, 'F' }, |
9d67679b | 1056 | { "target", 1, 0, 'T' }, |
ad38fb9f | 1057 | { "timeout", 1, 0, 'w' }, |
5cc12c40 | 1058 | { "version", 0, 0, 'V' }, |
9d67679b | 1059 | |
04fd7a9f KZ |
1060 | { NULL, 0, 0, 0 } |
1061 | }; | |
1062 | ||
af0dc7d3 | 1063 | assert(ARRAY_SIZE(columns) == FINDMNT_NCOLUMNS); |
9d67679b | 1064 | |
04fd7a9f KZ |
1065 | setlocale(LC_ALL, ""); |
1066 | bindtextdomain(PACKAGE, LOCALEDIR); | |
1067 | textdomain(PACKAGE); | |
c05a80ca | 1068 | atexit(close_stdout); |
04fd7a9f | 1069 | |
9d67679b | 1070 | /* default output format */ |
fdedb45e | 1071 | tt_flags |= TT_FL_TREE; |
04fd7a9f | 1072 | |
9d67679b | 1073 | while ((c = getopt_long(argc, argv, |
5cc12c40 | 1074 | "AacDd:ehifF:o:O:p::Pklmnrst:uvRS:T:w:V", |
d466c6a1 | 1075 | longopts, NULL)) != -1) { |
04fd7a9f | 1076 | switch(c) { |
cd4ed46f KZ |
1077 | case 'A': |
1078 | flags |= FL_ALL; | |
1079 | break; | |
9d67679b | 1080 | case 'a': |
fdedb45e | 1081 | tt_flags |= TT_FL_ASCII; |
9d67679b | 1082 | break; |
04fd7a9f KZ |
1083 | case 'c': |
1084 | flags |= FL_CANONICALIZE; | |
1085 | break; | |
eda399b9 DR |
1086 | case 'D': |
1087 | tt_flags &= ~TT_FL_TREE; | |
cd4ed46f | 1088 | flags |= FL_DF; |
eda399b9 | 1089 | break; |
04fd7a9f | 1090 | case 'd': |
4e6bd74c | 1091 | if (!strcmp(optarg, "forward")) |
04fd7a9f | 1092 | direction = MNT_ITER_FORWARD; |
4e6bd74c | 1093 | else if (!strcmp(optarg, "backward")) |
04fd7a9f KZ |
1094 | direction = MNT_ITER_BACKWARD; |
1095 | else | |
1096 | errx(EXIT_FAILURE, | |
8e350e48 | 1097 | _("unknown direction '%s'"), optarg); |
04fd7a9f KZ |
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; | |
9d67679b | 1108 | case 'f': |
04fd7a9f KZ |
1109 | flags |= FL_FIRSTONLY; |
1110 | break; | |
2f1ac44b | 1111 | case 'F': |
8c5eba3e | 1112 | tabfiles = append_tabfile(tabfiles, &ntabfiles, optarg); |
2f1ac44b | 1113 | break; |
9d67679b | 1114 | case 'u': |
32e5466a | 1115 | disable_columns_truncate(); |
9d67679b | 1116 | break; |
04fd7a9f | 1117 | case 'o': |
c87638ad | 1118 | ncolumns = string_to_idarray(optarg, |
bdc3ed66 KZ |
1119 | columns, ARRAY_SIZE(columns), |
1120 | column_name_to_id); | |
1121 | if (ncolumns < 0) | |
fdedb45e | 1122 | exit(EXIT_FAILURE); |
04fd7a9f KZ |
1123 | break; |
1124 | case 'O': | |
9d67679b | 1125 | set_match(COL_OPTIONS, optarg); |
04fd7a9f | 1126 | break; |
32e5466a | 1127 | case 'p': |
bdc3ed66 | 1128 | if (optarg) { |
c87638ad | 1129 | nactions = string_to_idarray(optarg, |
bdc3ed66 KZ |
1130 | actions, ARRAY_SIZE(actions), |
1131 | poll_action_name_to_id); | |
1132 | if (nactions < 0) | |
1133 | exit(EXIT_FAILURE); | |
1134 | } | |
32e5466a KZ |
1135 | flags |= FL_POLL; |
1136 | tt_flags &= ~TT_FL_TREE; | |
1137 | break; | |
49e9fd3a KZ |
1138 | case 'P': |
1139 | tt_flags |= TT_FL_EXPORT; | |
1140 | tt_flags &= ~TT_FL_TREE; | |
1141 | break; | |
fdedb45e | 1142 | case 'm': /* mtab */ |
8c5eba3e | 1143 | if (tabtype) |
9d67679b | 1144 | errx_mutually_exclusive("--{fstab,mtab,kernel}"); |
8c5eba3e | 1145 | tabtype = TABTYPE_MTAB; |
fdedb45e | 1146 | tt_flags &= ~TT_FL_TREE; |
04fd7a9f | 1147 | break; |
fdedb45e | 1148 | case 's': /* fstab */ |
8c5eba3e | 1149 | if (tabtype) |
9d67679b | 1150 | errx_mutually_exclusive("--{fstab,mtab,kernel}"); |
8c5eba3e | 1151 | tabtype = TABTYPE_FSTAB; |
ac808156 | 1152 | tt_flags &= ~TT_FL_TREE; |
04fd7a9f | 1153 | break; |
fdedb45e | 1154 | case 'k': /* kernel (mountinfo) */ |
8c5eba3e | 1155 | if (tabtype) |
9d67679b | 1156 | errx_mutually_exclusive("--{fstab,mtab,kernel}"); |
8c5eba3e | 1157 | tabtype = TABTYPE_KERNEL; |
04fd7a9f KZ |
1158 | break; |
1159 | case 't': | |
9d67679b KZ |
1160 | set_match(COL_FSTYPE, optarg); |
1161 | break; | |
1162 | case 'r': | |
fdedb45e KZ |
1163 | tt_flags &= ~TT_FL_TREE; /* disable the default */ |
1164 | tt_flags |= TT_FL_RAW; /* enable raw */ | |
9d67679b KZ |
1165 | break; |
1166 | case 'l': | |
49e9fd3a KZ |
1167 | if ((tt_flags & TT_FL_RAW) && (tt_flags & TT_FL_EXPORT)) |
1168 | errx_mutually_exclusive("--{raw,list,pairs}"); | |
9d67679b | 1169 | |
fdedb45e | 1170 | tt_flags &= ~TT_FL_TREE; /* disable the default */ |
9d67679b KZ |
1171 | break; |
1172 | case 'n': | |
fdedb45e | 1173 | tt_flags |= TT_FL_NOHEADINGS; |
9d67679b | 1174 | break; |
b2214e1f KZ |
1175 | case 'v': |
1176 | flags |= FL_NOFSROOT; | |
1177 | break; | |
049caefd KZ |
1178 | case 'R': |
1179 | flags |= FL_SUBMOUNTS; | |
1180 | break; | |
9d67679b | 1181 | case 'S': |
cd492186 | 1182 | set_source_match(optarg); |
9d67679b KZ |
1183 | flags |= FL_NOSWAPMATCH; |
1184 | break; | |
1185 | case 'T': | |
1186 | set_match(COL_TARGET, optarg); | |
1187 | flags |= FL_NOSWAPMATCH; | |
04fd7a9f | 1188 | break; |
ad38fb9f | 1189 | case 'w': |
db41a429 | 1190 | timeout = strtos32_or_err(optarg, _("invalid timeout argument")); |
ad38fb9f | 1191 | break; |
5cc12c40 SK |
1192 | case 'V': |
1193 | printf(UTIL_LINUX_VERSION); | |
1194 | return EXIT_SUCCESS; | |
04fd7a9f KZ |
1195 | default: |
1196 | usage(stderr); | |
1197 | break; | |
1198 | } | |
1199 | } | |
1200 | ||
cd4ed46f | 1201 | if (!ncolumns && (flags & FL_DF)) { |
eda399b9 DR |
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 | ||
32e5466a KZ |
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 | ||
8c5eba3e KZ |
1222 | if (!tabtype) |
1223 | tabtype = TABTYPE_KERNEL; | |
2f1ac44b | 1224 | |
8c5eba3e KZ |
1225 | if (flags & FL_POLL) { |
1226 | if (tabtype != TABTYPE_KERNEL) | |
1227 | errx_mutually_exclusive("--{poll,fstab,mtab}"); | |
1228 | if (ntabfiles > 1) | |
aec5c2a6 | 1229 | errx(EXIT_FAILURE, _("--poll accepts only one file, but more specified by --tab-file")); |
fdedb45e | 1230 | } |
b2214e1f | 1231 | |
9d67679b KZ |
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 | ||
04fd7a9f | 1237 | if (optind < argc) |
cd492186 | 1238 | set_source_match(argv[optind++]); /* dev/tag/mountpoint/maj:min */ |
04fd7a9f | 1239 | if (optind < argc) |
9d67679b | 1240 | set_match(COL_TARGET, argv[optind++]); /* mountpoint */ |
04fd7a9f | 1241 | |
049caefd KZ |
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))) | |
fdedb45e KZ |
1248 | tt_flags &= ~TT_FL_TREE; |
1249 | ||
ac808156 KZ |
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 | ||
090b5e84 KZ |
1258 | if (!strncmp(x, "LABEL=", 6) || !strncmp(x, "UUID=", 5) || |
1259 | !strncmp(x, "PARTLABEL=", 10) || !strncmp(x, "PARTUUID=", 9)) | |
ac808156 KZ |
1260 | flags |= FL_NOSWAPMATCH; |
1261 | } | |
1262 | ||
fdedb45e KZ |
1263 | /* |
1264 | * initialize libmount | |
1265 | */ | |
ac808156 KZ |
1266 | mnt_init_debug(0); |
1267 | ||
8c5eba3e | 1268 | tb = parse_tabfiles(tabfiles, ntabfiles, tabtype); |
04fd7a9f | 1269 | if (!tb) |
fdedb45e | 1270 | goto leave; |
04fd7a9f | 1271 | |
2f1ac44b KZ |
1272 | if ((tt_flags & TT_FL_TREE) && !tab_is_tree(tb)) |
1273 | tt_flags &= ~TT_FL_TREE; | |
1274 | ||
04fd7a9f | 1275 | cache = mnt_new_cache(); |
fdedb45e KZ |
1276 | if (!cache) { |
1277 | warn(_("failed to initialize libmount cache")); | |
1278 | goto leave; | |
1279 | } | |
68164f6c | 1280 | mnt_table_set_cache(tb, cache); |
04fd7a9f | 1281 | |
4bfd4bff KZ |
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 | ||
fdedb45e | 1290 | /* |
00b4bcdf | 1291 | * initialize output formatting (tt.h) |
fdedb45e KZ |
1292 | */ |
1293 | tt = tt_new_table(tt_flags); | |
1294 | if (!tt) { | |
1295 | warn(_("failed to initialize output table")); | |
1296 | goto leave; | |
1297 | } | |
ac808156 | 1298 | |
fdedb45e | 1299 | for (i = 0; i < ncolumns; i++) { |
32e5466a KZ |
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; | |
04fd7a9f | 1305 | |
32e5466a | 1306 | if (!(flags & FL_POLL) && is_tabdiff_column(id)) { |
77a1c5f7 | 1307 | warnx(_("%s column is requested, but --poll " |
32e5466a KZ |
1308 | "is not enabled"), get_column_name(i)); |
1309 | goto leave; | |
1310 | } | |
fdedb45e KZ |
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; | |
9d67679b | 1315 | } |
fdedb45e | 1316 | } |
9d67679b | 1317 | |
fdedb45e KZ |
1318 | /* |
1319 | * Fill in data to the output table | |
1320 | */ | |
275c9a48 | 1321 | if (flags & FL_POLL) { |
8c5eba3e | 1322 | /* poll mode (accept the first tabfile only) */ |
275c9a48 KZ |
1323 | if (tabfiles && ntabfiles > 0) |
1324 | rc = poll_table(tb, *tabfiles, timeout, tt, direction); | |
32e5466a | 1325 | |
275c9a48 | 1326 | } else if ((tt_flags & TT_FL_TREE) && is_listall_mode()) |
049caefd KZ |
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); | |
fdedb45e KZ |
1332 | |
1333 | /* | |
32e5466a | 1334 | * Print the output table for non-poll modes |
fdedb45e | 1335 | */ |
32e5466a | 1336 | if (!rc && !(flags & FL_POLL)) |
049caefd | 1337 | tt_print_table(tt); |
fdedb45e KZ |
1338 | leave: |
1339 | tt_free_table(tt); | |
1340 | ||
68164f6c | 1341 | mnt_free_table(tb); |
04fd7a9f | 1342 | mnt_free_cache(cache); |
8c5eba3e | 1343 | free(tabfiles); |
04fd7a9f | 1344 | |
049caefd | 1345 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
04fd7a9f | 1346 | } |