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