]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/findmnt-verify.c
docs: update source-code-management.txt
[thirdparty/util-linux.git] / misc-utils / findmnt-verify.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <libmount.h>
10 #include <blkid.h>
11 #include <sys/utsname.h>
12
13 #include "nls.h"
14 #include "c.h"
15 #include "strutils.h"
16 #include "xalloc.h"
17
18 #include "findmnt.h"
19
20 struct verify_context {
21 struct libmnt_fs *fs;
22 struct libmnt_table *tb;
23
24 char **fs_ary;
25 size_t fs_num;
26 size_t fs_alloc;
27
28 int nwarnings;
29 int nerrors;
30
31 unsigned int target_printed : 1;
32 };
33
34 static void verify_mesg(struct verify_context *vfy, char type, const char *fmt, va_list ap)
35 {
36 if (!vfy->target_printed) {
37 fprintf(stdout, "%s\n", mnt_fs_get_target(vfy->fs));
38 vfy->target_printed = 1;
39 }
40
41 fprintf(stdout, " [%c] ", type);
42 vfprintf(stdout, fmt, ap);
43 fputc('\n', stdout);
44 }
45
46 static int verify_warn(struct verify_context *vfy, const char *fmt, ...)
47 {
48 va_list ap;
49 vfy->nwarnings++;
50 va_start(ap, fmt);
51 verify_mesg(vfy, 'W', fmt, ap);
52 va_end(ap);
53 return 0;
54 }
55
56 static int verify_err(struct verify_context *vfy, const char *fmt, ...)
57 {
58 va_list ap;
59 vfy->nerrors++;
60 va_start(ap, fmt);
61 verify_mesg(vfy, 'E', fmt, ap);
62 va_end(ap);
63 return 0;
64 }
65
66 static int verify_ok(struct verify_context *vfy __attribute__((unused)),
67 const char *fmt, ...)
68 {
69 va_list ap;
70
71 if (!(flags & FL_VERBOSE))
72 return 0;
73
74 va_start(ap, fmt);
75 verify_mesg(vfy, ' ', fmt, ap);
76 va_end(ap);
77 return 0;
78 }
79
80 static int verify_order(struct verify_context *vfy)
81 {
82 struct libmnt_iter *itr = NULL;
83 struct libmnt_fs *next;
84 const char *tgt;
85
86 tgt = mnt_fs_get_target(vfy->fs);
87 if (tgt && !(flags & FL_NOCACHE))
88 tgt = mnt_resolve_target(tgt, cache);
89 else if (!tgt)
90 return 0;
91
92 itr = mnt_new_iter(MNT_ITER_FORWARD);
93 if (!itr) {
94 warn(_("failed to initialize libmount iterator"));
95 goto done;
96 }
97
98 /* set iterator position to 'fs' */
99 mnt_table_set_iter(vfy->tb, itr, vfy->fs);
100 mnt_table_next_fs(vfy->tb, itr, &next);
101
102 /* scan all next filesystems */
103 while (mnt_table_next_fs(vfy->tb, itr, &next) == 0) {
104 const char *n_tgt;
105 size_t len;
106
107 n_tgt = mnt_fs_get_target(next);
108 if (n_tgt && !(flags & FL_NOCACHE))
109 n_tgt = mnt_resolve_target(n_tgt, cache);
110 else if (!n_tgt)
111 continue;
112 len = strlen(n_tgt);
113
114 if (strncmp(n_tgt, tgt, len) == 0) {
115 if (*(tgt + len) == '\0')
116 verify_warn(vfy, _("target specified more than once"));
117 else if (*(tgt + len) == '/')
118 verify_err(vfy, _("wrong order: %s specified before %s"), tgt, n_tgt);
119 }
120 }
121 done:
122 mnt_free_iter(itr);
123 return 0;
124 }
125
126 static int verify_target(struct verify_context *vfy)
127 {
128 const char *tgt = mnt_fs_get_target(vfy->fs);
129 const char *cn = tgt;
130 struct stat sb;
131
132 if (!tgt)
133 return verify_err(vfy, _("undefined target (fs_file)"));
134
135 if (!(flags & FL_NOCACHE)) {
136 cn = mnt_resolve_target(tgt, cache);
137 if (!cn)
138 return -ENOMEM;
139 if (strcmp(cn, tgt) != 0)
140 verify_warn(vfy, _("non-canonical target path (real: %s)"), cn);
141 tgt = cn;
142 }
143 if (stat(tgt, &sb) != 0) {
144 if (mnt_fs_get_option(vfy->fs, "noauto", NULL, NULL) == 1)
145 verify_err(vfy, _("unreachable on boot required target: %m"));
146 else
147 verify_warn(vfy, _("unreachable target: %m"));
148
149 } else if (!S_ISDIR(sb.st_mode)
150 && mnt_fs_get_option(vfy->fs, "bind", NULL, NULL) == 1) {
151 verify_err(vfy, _("target is not a directory"));
152 } else
153 verify_ok(vfy, _("target exists"));
154
155 return 0;
156 }
157
158 static char *verify_tag(struct verify_context *vfy, const char *name,
159 const char *value)
160 {
161 char *src = mnt_resolve_tag(name, value, cache);
162
163 if (!src) {
164 if (mnt_fs_get_option(vfy->fs, "noauto", NULL, NULL) == 1)
165 verify_err(vfy, _("unreachable on boot required source: %s=%s"), name, value);
166 else
167 verify_warn(vfy, _("unreachable: %s=%s"), name, value);
168 } else
169 verify_ok(vfy, _("%s=%s translated to %s"), name, value, src);
170
171 return src;
172 }
173
174 /* Note that mount source is very FS specific and we should not
175 * interpret unreachable source as error. The exception is only
176 * NAME=value, this has to be convertible to device name.
177 */
178 static int verify_source(struct verify_context *vfy)
179 {
180 const char *src = mnt_fs_get_srcpath(vfy->fs);
181 char *t = NULL, *v = NULL;
182 struct stat sb;
183 int isbind, rc = 0;
184
185 /* source is NAME=value tag */
186 if (!src) {
187 const char *tag = NULL, *val = NULL;
188
189 if (mnt_fs_get_tag(vfy->fs, &tag, &val) != 0)
190 return verify_err(vfy, _("undefined source (fs_spec)"));
191
192 src = verify_tag(vfy, tag, val);
193 if (!src)
194 goto done;
195
196 /* blkid is able to parse it, but libmount does not see it as a tag --
197 * it means unsupported tag */
198 } else if (blkid_parse_tag_string(src, &t, &v) == 0 && stat(src, &sb) != 0) {
199 rc = verify_err(vfy, _("unsupported source tag: %s"), src);
200 goto done;
201 }
202 isbind = mnt_fs_get_option(vfy->fs, "bind", NULL, NULL) == 0;
203
204 /* source is path */
205 if (mnt_fs_is_pseudofs(vfy->fs) || mnt_fs_is_netfs(vfy->fs))
206 verify_ok(vfy, _("do not check %s source (pseudo/net)"), src);
207
208 else if (stat(src, &sb) != 0)
209 verify_warn(vfy, _("unreachable source: %s: %m"), src);
210
211 else if ((S_ISDIR(sb.st_mode) || S_ISREG(sb.st_mode)) && !isbind)
212 verify_warn(vfy, _("non-bind mount source %s is a directory or regular file"), src);
213
214 else if (!S_ISBLK(sb.st_mode) && !isbind)
215 verify_warn(vfy, _("source %s is not a block device"), src);
216 else
217 verify_ok(vfy, _("source %s exists"), src);
218 done:
219 free(t);
220 free(v);
221 return rc;
222 }
223
224 static int verify_options(struct verify_context *vfy)
225 {
226 const char *opts;
227
228 opts = mnt_fs_get_vfs_options(vfy->fs);
229 if (opts)
230 verify_ok(vfy, _("VFS options: %s"), opts);
231
232 opts = mnt_fs_get_fs_options(vfy->fs);
233 if (opts)
234 verify_ok(vfy, _("FS options: %s"), opts);
235
236 opts = mnt_fs_get_user_options(vfy->fs);
237 if (opts)
238 verify_ok(vfy, _("userspace options: %s"), opts);
239
240 return 0;
241 }
242
243 static int verify_swaparea(struct verify_context *vfy)
244 {
245 char *arg;
246 size_t argsz = 0;
247
248 if (mnt_fs_get_option(vfy->fs, "discard", &arg, &argsz) == 0
249 && arg
250 && strncmp(arg, "once", argsz) != 0
251 && strncmp(arg, "pages", argsz) != 0)
252 verify_err(vfy, _("unsupported swaparea discard policy: %s"), arg);
253
254 if (mnt_fs_get_option(vfy->fs, "pri", &arg, &argsz) == 0 && arg) {
255 char *p = arg;
256 if (*p == '-')
257 p++;
258 for (; p < arg + argsz; p++) {
259 if (!isdigit((unsigned char) *p)) {
260 verify_err(vfy, _("failed to parse swaparea priority option"));
261 break;
262 }
263 }
264 }
265
266 return 0;
267 }
268
269 static int is_supported_filesystem(struct verify_context *vfy, const char *name)
270 {
271 size_t n;
272
273 if (!vfy->fs_num)
274 return 0;
275
276 for (n = 0; n < vfy->fs_num; n++ ) {
277 if (strcmp(vfy->fs_ary[n], name) == 0)
278 return 1;
279 }
280
281 return 0;
282 }
283
284 static int add_filesystem(struct verify_context *vfy, const char *name)
285 {
286 #define MYCHUNK 16
287
288 if (is_supported_filesystem(vfy, name))
289 return 0;
290
291 if (vfy->fs_alloc == 0 || vfy->fs_num + 1 <= vfy->fs_alloc) {
292 vfy->fs_alloc = ((vfy->fs_alloc + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK;
293 vfy->fs_ary = xrealloc(vfy->fs_ary, vfy->fs_alloc * sizeof(char *));
294 }
295
296 vfy->fs_ary[vfy->fs_num] = xstrdup(name);
297 vfy->fs_num++;
298
299 return 0;
300 }
301
302 static int read_proc_filesystems(struct verify_context *vfy)
303 {
304 int rc = 0;
305 FILE *f;
306 char buf[80], *cp, *t;
307
308 f = fopen("/proc/filesystems", "r");
309 if (!f)
310 return -errno;
311
312 while (!feof(f)) {
313 if (!fgets(buf, sizeof(buf), f))
314 break;
315 cp = buf;
316 if (!isspace(*cp)) {
317 while (*cp && !isspace(*cp))
318 cp++;
319 }
320 while (*cp && isspace(*cp))
321 cp++;
322 if ((t = strchr(cp, '\n')) != NULL)
323 *t = 0;
324 if ((t = strchr(cp, '\t')) != NULL)
325 *t = 0;
326 if ((t = strchr(cp, ' ')) != NULL)
327 *t = 0;
328
329 rc = add_filesystem(vfy, cp);
330 if (rc)
331 break;
332 }
333 fclose(f);
334 return rc;
335 }
336
337 static int read_kernel_filesystems(struct verify_context *vfy)
338 {
339 int rc = 0;
340 #ifdef __linux__
341 struct utsname uts;
342 FILE *f;
343 char buf[1024];
344
345 if (uname(&uts))
346 return 0;
347 snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
348
349 f = fopen(buf, "r");
350 if (!f)
351 return 0;
352
353 while (!feof(f)) {
354 char *p, *name;
355
356 if (!fgets(buf, sizeof(buf), f))
357 break;
358
359 if (strncmp("kernel/fs/", buf, 10) != 0 ||
360 strncmp("kernel/fs/nls/", buf, 14) == 0)
361 continue;
362
363 p = strchr(buf, ':');
364 if (!p)
365 continue;
366 *p = '\0';
367
368 name = strrchr(buf, '/');
369 if (!name)
370 continue;
371 name++;
372
373 p = strstr(name, ".ko");
374 if (!p)
375 continue;
376 *p = '\0';
377
378 rc = add_filesystem(vfy, name);
379 if (rc)
380 break;
381 }
382 fclose(f);
383 #endif /* __linux__ */
384 return rc;
385 }
386
387 static int verify_fstype(struct verify_context *vfy)
388 {
389 const char *src = mnt_resolve_spec(mnt_fs_get_source(vfy->fs), cache);
390 const char *type, *realtype;
391 int ambi = 0, isauto = 0, isswap = 0;
392
393 if (!src)
394 return 0;
395 if (mnt_fs_is_pseudofs(vfy->fs) || mnt_fs_is_netfs(vfy->fs))
396 return verify_ok(vfy, _("do not check %s FS type (pseudo/net)"), src);
397
398 type = mnt_fs_get_fstype(vfy->fs);
399
400 if (type) {
401 int none = strcmp(type, "none") == 0;
402
403 if (none
404 && mnt_fs_get_option(vfy->fs, "bind", NULL, NULL) == 1
405 && mnt_fs_get_option(vfy->fs, "move", NULL, NULL) == 1)
406 return verify_warn(vfy, _("\"none\" FS type is recommended for bind or move oprations only"));
407
408 else if (strcmp(type, "auto") == 0)
409 isauto = 1;
410 else if (strcmp(type, "swap") == 0)
411 isswap = 1;
412
413 if (!isswap && !isauto && !none && !is_supported_filesystem(vfy, type))
414 verify_warn(vfy, _("%s seems unsupported by the current kernel"), type);
415 }
416 realtype = mnt_get_fstype(src, &ambi, cache);
417
418 if (!realtype) {
419 if (isauto)
420 return verify_err(vfy, _("cannot detect on-disk filesystem type"));
421 return verify_warn(vfy, _("cannot detect on-disk filesystem type"));
422 }
423
424 if (realtype) {
425 isswap = strcmp(realtype, "swap") == 0;
426
427 if (type && !isauto && strcmp(type, realtype) != 0)
428 return verify_err(vfy, _("%s does not match with on-disk %s"), type, realtype);
429
430 if (!isswap && !is_supported_filesystem(vfy, realtype))
431 return verify_err(vfy, _("on-disk %s seems unsupported by the current kernel"), realtype);
432
433 verify_ok(vfy, _("FS type is %s"), realtype);
434 }
435
436 return 0;
437 }
438
439 static int verify_passno(struct verify_context *vfy)
440 {
441 int passno = mnt_fs_get_passno(vfy->fs);
442 const char *tgt = mnt_fs_get_target(vfy->fs);
443
444 if (tgt && strcmp("/", tgt) == 0 && passno != 1)
445 return verify_warn(vfy, _("recommended root FS passno is 1 (current is %d)"), passno);
446
447 return 0;
448 }
449
450 static int verify_filesystem(struct verify_context *vfy)
451 {
452 int rc = 0;
453
454 if (mnt_fs_is_swaparea(vfy->fs))
455 rc = verify_swaparea(vfy);
456 else {
457 rc = verify_target(vfy);
458 if (!rc)
459 rc = verify_options(vfy);
460 }
461
462 if (!rc)
463 rc = verify_source(vfy);
464 if (!rc)
465 rc = verify_fstype(vfy);
466 if (!rc)
467 rc = verify_passno(vfy);
468
469 return rc;
470 }
471
472 int verify_table(struct libmnt_table *tb)
473 {
474 struct verify_context vfy = { .nerrors = 0 };
475 struct libmnt_iter *itr = NULL;
476 int rc = 0; /* overall return code (alloc errors, etc.) */
477 int check_order = is_listall_mode();
478 static int has_read_fs = 0;
479
480 itr = mnt_new_iter(MNT_ITER_FORWARD);
481 if (!itr) {
482 warn(_("failed to initialize libmount iterator"));
483 goto done;
484 }
485
486 vfy.tb = tb;
487
488 if (has_read_fs == 0) {
489 read_proc_filesystems(&vfy);
490 read_kernel_filesystems(&vfy);
491 has_read_fs = 1;
492 }
493
494 while (rc == 0 && (vfy.fs = get_next_fs(tb, itr))) {
495 vfy.target_printed = 0;
496 if (check_order)
497 rc = verify_order(&vfy);
498 if (!rc)
499 rc = verify_filesystem(&vfy);
500
501 if (flags & FL_FIRSTONLY)
502 break;
503 flags |= FL_NOSWAPMATCH;
504 }
505
506 done:
507 mnt_free_iter(itr);
508
509 /* summary */
510 if (vfy.nerrors || parse_nerrors || vfy.nwarnings) {
511 fputc('\n', stderr);
512 fprintf(stderr, P_("%d parse error", "%d parse errors", parse_nerrors), parse_nerrors);
513 fprintf(stderr, P_(", %d error", ", %d errors", vfy.nerrors), vfy.nerrors);
514 fprintf(stderr, P_(", %d warning", ", %d warnings", vfy.nwarnings), vfy.nwarnings);
515 fputc('\n', stderr);
516 } else
517 fprintf(stdout, _("Success, no errors or warnings detected\n"));
518
519 return rc != 0 ? rc : vfy.nerrors + parse_nerrors;
520 }