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