]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fsck.c
Merge branch 'uuid-time64_t' of https://github.com/thkukuk/util-linux
[thirdparty/util-linux.git] / disk-utils / fsck.c
1 /*
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * fsck --- A generic, parallelizing front-end for the fsck program.
11 * It will automatically try to run fsck programs in parallel if the
12 * devices are on separate spindles. It is based on the same ideas as
13 * the generic front end for fsck by David Engel and Fred van Kempen,
14 * but it has been completely rewritten from scratch to support
15 * parallel execution.
16 *
17 * Written by Theodore Ts'o, <tytso@mit.edu>
18 *
19 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
20 * o Changed -t fstype to behave like with mount when -A (all file
21 * systems) or -M (like mount) is specified.
22 * o fsck looks if it can find the fsck.type program to decide
23 * if it should ignore the fs type. This way more fsck programs
24 * can be added without changing this front-end.
25 * o -R flag skip root file system.
26 *
27 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
28 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
29 *
30 * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com>
31 */
32 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
33
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <sys/stat.h>
37 #include <sys/file.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <string.h>
43 #include <time.h>
44 #include <stdlib.h>
45 #include <paths.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <dirent.h>
50 #include <sys/resource.h>
51 #include <sys/time.h>
52 #include <blkid.h>
53 #include <libmount.h>
54
55 #include "nls.h"
56 #include "pathnames.h"
57 #include "exitcodes.h"
58 #include "c.h"
59 #include "fileutils.h"
60 #include "monotonic.h"
61 #include "strutils.h"
62
63 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
64 #include "xalloc.h"
65
66 #define CLOSE_EXIT_CODE FSCK_EX_ERROR
67 #include "closestream.h"
68
69 #ifndef DEFAULT_FSTYPE
70 # define DEFAULT_FSTYPE "ext2"
71 #endif
72
73 #define MAX_DEVICES 32
74 #define MAX_ARGS 32
75
76 #define FSCK_RUNTIME_DIRNAME "/run/fsck"
77
78 static const char *ignored_types[] = {
79 "ignore",
80 "iso9660",
81 "sw",
82 NULL
83 };
84
85 static const char *really_wanted[] = {
86 "minix",
87 "ext2",
88 "ext3",
89 "ext4",
90 "ext4dev",
91 "jfs",
92 "reiserfs"
93 };
94
95 /*
96 * Internal structure for mount table entries.
97 */
98 struct fsck_fs_data
99 {
100 const char *device;
101 dev_t disk;
102 unsigned int stacked:1,
103 done:1,
104 eval_device:1;
105 };
106
107 /*
108 * Structure to allow exit codes to be stored
109 */
110 struct fsck_instance {
111 int pid;
112 int flags; /* FLAG_{DONE|PROGRESS} */
113
114 int lock; /* flock()ed lockpath file descriptor or -1 */
115 char *lockpath; /* /run/fsck/<diskname>.lock or NULL */
116
117 int exit_status;
118 struct timeval start_time;
119 struct timeval end_time;
120 char * prog;
121 char * type;
122
123 struct rusage rusage;
124 struct libmnt_fs *fs;
125 struct fsck_instance *next;
126 };
127
128 #define FLAG_DONE 1
129 #define FLAG_PROGRESS 2
130
131 /*
132 * Global variables for options
133 */
134 static char *devices[MAX_DEVICES];
135 static char *args[MAX_ARGS];
136 static int num_devices, num_args;
137
138 static int lockdisk;
139 static int verbose;
140 static int doall;
141 static int noexecute;
142 static int serialize;
143 static int skip_root;
144 static int ignore_mounted;
145 static int notitle;
146 static int parallel_root;
147 static int progress;
148 static int progress_fd;
149 static int force_all_parallel;
150 static int report_stats;
151 static FILE *report_stats_file;
152
153 static int num_running;
154 static int max_running;
155
156 static volatile sig_atomic_t cancel_requested;
157 static int kill_sent;
158 static char *fstype;
159 static struct fsck_instance *instance_list;
160
161 #define FSCK_DEFAULT_PATH "/sbin"
162 static char *fsck_path;
163
164
165 /* parsed fstab and mtab */
166 static struct libmnt_table *fstab, *mtab;
167 static struct libmnt_cache *mntcache;
168
169 static int count_slaves(dev_t disk);
170
171 static int string_to_int(const char *s)
172 {
173 long l;
174 char *p;
175
176 errno = 0;
177 l = strtol(s, &p, 0);
178 if (errno || *p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
179 return -1;
180
181 return (int) l;
182 }
183
184 /* Do we really really want to check this fs? */
185 static int fs_check_required(const char *type)
186 {
187 size_t i;
188
189 for(i = 0; i < ARRAY_SIZE(really_wanted); i++) {
190 if (strcmp(type, really_wanted[i]) == 0)
191 return 1;
192 }
193
194 return 0;
195 }
196
197 static int is_mounted(struct libmnt_fs *fs)
198 {
199 int rc;
200 const char *src;
201
202 src = mnt_fs_get_source(fs);
203 if (!src)
204 return 0;
205 if (!mntcache)
206 mntcache = mnt_new_cache();
207 if (!mtab) {
208 mtab = mnt_new_table();
209 if (!mtab)
210 err(FSCK_EX_ERROR, ("failed to initialize libmount table"));
211 mnt_table_set_cache(mtab, mntcache);
212 mnt_table_parse_mtab(mtab, NULL);
213 }
214
215 rc = mnt_table_find_source(mtab, src, MNT_ITER_BACKWARD) ? 1 : 0;
216 if (verbose) {
217 if (rc)
218 printf(_("%s is mounted\n"), src);
219 else
220 printf(_("%s is not mounted\n"), src);
221 }
222 return rc;
223 }
224
225 static int ignore(struct libmnt_fs *);
226
227 static struct fsck_fs_data *fs_create_data(struct libmnt_fs *fs)
228 {
229 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
230
231 if (!data) {
232 data = xcalloc(1, sizeof(*data));
233 mnt_fs_set_userdata(fs, data);
234 }
235 return data;
236 }
237
238 /*
239 * fs from fstab might contains real device name as well as symlink,
240 * LABEL or UUID, this function returns canonicalized result.
241 */
242 static const char *fs_get_device(struct libmnt_fs *fs)
243 {
244 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
245
246 if (!data || !data->eval_device) {
247 const char *spec = mnt_fs_get_source(fs);
248
249 if (!data)
250 data = fs_create_data(fs);
251
252 data->eval_device = 1;
253 data->device = mnt_resolve_spec(spec, mnt_table_get_cache(fstab));
254 if (!data->device)
255 data->device = xstrdup(spec);
256 }
257
258 return data->device;
259 }
260
261 static dev_t fs_get_disk(struct libmnt_fs *fs, int check)
262 {
263 struct fsck_fs_data *data;
264 const char *device;
265 struct stat st;
266
267 data = mnt_fs_get_userdata(fs);
268 if (data && data->disk)
269 return data->disk;
270
271 if (!check)
272 return 0;
273
274 if (mnt_fs_is_netfs(fs) || mnt_fs_is_pseudofs(fs))
275 return 0;
276
277 device = fs_get_device(fs);
278 if (!device)
279 return 0;
280
281 data = fs_create_data(fs);
282
283 if (!stat(device, &st) &&
284 !blkid_devno_to_wholedisk(st.st_rdev, NULL, 0, &data->disk)) {
285
286 if (data->disk)
287 data->stacked = count_slaves(data->disk) > 0 ? 1 : 0;
288 return data->disk;
289 }
290 return 0;
291 }
292
293 static int fs_is_stacked(struct libmnt_fs *fs)
294 {
295 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
296 return data ? data->stacked : 0;
297 }
298
299 static int fs_is_done(struct libmnt_fs *fs)
300 {
301 struct fsck_fs_data *data = mnt_fs_get_userdata(fs);
302 return data ? data->done : 0;
303 }
304
305 static void fs_set_done(struct libmnt_fs *fs)
306 {
307 struct fsck_fs_data *data = fs_create_data(fs);
308
309 if (data)
310 data->done = 1;
311 }
312
313 static int is_irrotational_disk(dev_t disk)
314 {
315 char path[PATH_MAX];
316 FILE *f;
317 int rc, x;
318
319
320 rc = snprintf(path, sizeof(path),
321 "/sys/dev/block/%d:%d/queue/rotational",
322 major(disk), minor(disk));
323
324 if (rc < 0 || (unsigned int) rc >= sizeof(path))
325 return 0;
326
327 f = fopen(path, "r");
328 if (!f)
329 return 0;
330
331 rc = fscanf(f, "%d", &x);
332 if (rc != 1) {
333 if (ferror(f))
334 warn(_("cannot read %s"), path);
335 else
336 warnx(_("parse error: %s"), path);
337 }
338 fclose(f);
339
340 return rc == 1 ? !x : 0;
341 }
342
343 static void lock_disk(struct fsck_instance *inst)
344 {
345 dev_t disk = fs_get_disk(inst->fs, 1);
346 char *diskpath = NULL, *diskname;
347
348 inst->lock = -1;
349
350 if (!disk || is_irrotational_disk(disk))
351 goto done;
352
353 diskpath = blkid_devno_to_devname(disk);
354 if (!diskpath)
355 goto done;
356
357 if (access(FSCK_RUNTIME_DIRNAME, F_OK) != 0) {
358 int rc = mkdir(FSCK_RUNTIME_DIRNAME,
359 S_IWUSR|
360 S_IRUSR|S_IRGRP|S_IROTH|
361 S_IXUSR|S_IXGRP|S_IXOTH);
362 if (rc && errno != EEXIST) {
363 warn(_("cannot create directory %s"),
364 FSCK_RUNTIME_DIRNAME);
365 goto done;
366 }
367 }
368
369 diskname = stripoff_last_component(diskpath);
370 if (!diskname)
371 diskname = diskpath;
372
373 xasprintf(&inst->lockpath, FSCK_RUNTIME_DIRNAME "/%s.lock", diskname);
374
375 if (verbose)
376 printf(_("Locking disk by %s ... "), inst->lockpath);
377
378 inst->lock = open(inst->lockpath, O_RDONLY|O_CREAT|O_CLOEXEC,
379 S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
380 if (inst->lock >= 0) {
381 int rc = -1;
382
383 /* inform users that we're waiting on the lock */
384 if (verbose &&
385 (rc = flock(inst->lock, LOCK_EX | LOCK_NB)) != 0 &&
386 errno == EWOULDBLOCK)
387 printf(_("(waiting) "));
388
389 if (rc != 0 && flock(inst->lock, LOCK_EX) != 0) {
390 close(inst->lock); /* failed */
391 inst->lock = -1;
392 }
393 }
394
395 if (verbose)
396 /* TRANSLATORS: These are followups to "Locking disk...". */
397 printf("%s.\n", inst->lock >= 0 ? _("succeeded") : _("failed"));
398
399
400 done:
401 if (inst->lock < 0) {
402 free(inst->lockpath);
403 inst->lockpath = NULL;
404 }
405 free(diskpath);
406 }
407
408 static void unlock_disk(struct fsck_instance *inst)
409 {
410 if (inst->lock < 0)
411 return;
412
413 if (verbose)
414 printf(_("Unlocking %s.\n"), inst->lockpath);
415
416 close(inst->lock); /* unlock */
417
418 free(inst->lockpath);
419
420 inst->lock = -1;
421 inst->lockpath = NULL;
422 }
423
424 static void free_instance(struct fsck_instance *i)
425 {
426 if (lockdisk)
427 unlock_disk(i);
428 free(i->prog);
429 free(i->lockpath);
430 mnt_unref_fs(i->fs);
431 free(i);
432 }
433
434 static struct libmnt_fs *add_dummy_fs(const char *device)
435 {
436 struct libmnt_fs *fs = mnt_new_fs();
437
438 if (fs && mnt_fs_set_source(fs, device) == 0 &&
439 mnt_table_add_fs(fstab, fs) == 0) {
440 mnt_unref_fs(fs);
441 return fs;
442 }
443
444 mnt_unref_fs(fs);
445 err(FSCK_EX_ERROR, _("failed to setup description for %s"), device);
446 }
447
448 static void fs_interpret_type(struct libmnt_fs *fs)
449 {
450 const char *device;
451 const char *type = mnt_fs_get_fstype(fs);
452
453 if (type && strcmp(type, "auto") != 0)
454 return;
455
456 mnt_fs_set_fstype(fs, NULL);
457
458 device = fs_get_device(fs);
459 if (device) {
460 int ambi = 0;
461 char *tp;
462 struct libmnt_cache *cache = mnt_table_get_cache(fstab);
463
464 tp = mnt_get_fstype(device, &ambi, cache);
465 if (!ambi)
466 mnt_fs_set_fstype(fs, tp);
467 if (!cache)
468 free(tp);
469 }
470 }
471
472 static int parser_errcb(struct libmnt_table *tb __attribute__ ((__unused__)),
473 const char *filename, int line)
474 {
475 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
476 return 1;
477 }
478
479 /*
480 * Load the filesystem database from /etc/fstab
481 */
482 static void load_fs_info(void)
483 {
484 const char *path;
485
486 fstab = mnt_new_table();
487 if (!fstab)
488 err(FSCK_EX_ERROR, ("failed to initialize libmount table"));
489
490 mnt_table_set_parser_errcb(fstab, parser_errcb);
491 mnt_table_set_cache(fstab, mntcache);
492
493 errno = 0;
494
495 /*
496 * Let's follow libmount defaults if $FSTAB_FILE is not specified
497 */
498 path = getenv("FSTAB_FILE");
499
500 if (mnt_table_parse_fstab(fstab, path)) {
501 if (!path)
502 path = mnt_get_fstab_path();
503
504 /* don't print error when there is no fstab at all */
505 if (access(path, F_OK) == 0) {
506 if (errno)
507 warn(_("%s: failed to parse fstab"), path);
508 else
509 warnx(_("%s: failed to parse fstab"), path);
510 }
511 }
512 }
513
514 /*
515 * Lookup filesys in /etc/fstab and return the corresponding entry.
516 * The @path has to be real path (no TAG) by mnt_resolve_spec().
517 */
518 static struct libmnt_fs *lookup(char *path)
519 {
520 struct libmnt_fs *fs;
521
522 if (!path)
523 return NULL;
524
525 fs = mnt_table_find_srcpath(fstab, path, MNT_ITER_FORWARD);
526 if (!fs) {
527 /*
528 * Maybe mountpoint has been specified on fsck command line.
529 * Yeah, crazy feature...
530 *
531 * Note that mnt_table_find_target() may canonicalize paths in
532 * the fstab to support symlinks. This is really unwanted,
533 * because readlink() on mountpoints may trigger automounts.
534 *
535 * So, disable the cache and compare the paths as strings
536 * without care about symlinks...
537 */
538 mnt_table_set_cache(fstab, NULL);
539 fs = mnt_table_find_target(fstab, path, MNT_ITER_FORWARD);
540 mnt_table_set_cache(fstab, mntcache);
541 }
542 return fs;
543 }
544
545 /* Find fsck program for a given fs type. */
546 static int find_fsck(const char *type, char **progpath)
547 {
548 char *s;
549 const char *tpl;
550 char *prog = NULL;
551 char *p = xstrdup(fsck_path);
552 int rc;
553
554 /* Are we looking for a program or just a type? */
555 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
556
557 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
558 xasprintf(&prog, tpl, s, type);
559 if (access(prog, X_OK) == 0)
560 break;
561 free(prog);
562 prog = NULL;
563 }
564
565 free(p);
566 rc = prog ? 1 : 0;
567
568 if (progpath)
569 *progpath = prog;
570 else
571 free(prog);
572
573 return rc;
574 }
575
576 static int progress_active(void)
577 {
578 struct fsck_instance *inst;
579
580 for (inst = instance_list; inst; inst = inst->next) {
581 if (inst->flags & FLAG_DONE)
582 continue;
583 if (inst->flags & FLAG_PROGRESS)
584 return 1;
585 }
586 return 0;
587 }
588
589 /*
590 * Process run statistics for finished fsck instances.
591 *
592 * If report_stats is 0, do nothing, otherwise print a selection of
593 * interesting rusage statistics as well as elapsed wallclock time.
594 */
595 static void print_stats(struct fsck_instance *inst)
596 {
597 struct timeval delta;
598
599 if (!inst || !report_stats || noexecute)
600 return;
601
602 timersub(&inst->end_time, &inst->start_time, &delta);
603
604 if (report_stats_file)
605 fprintf(report_stats_file, "%s %d %ld"
606 " %"PRId64".%06"PRId64
607 " %"PRId64".%06"PRId64
608 " %"PRId64".%06"PRId64"\n",
609 fs_get_device(inst->fs),
610 inst->exit_status,
611 inst->rusage.ru_maxrss,
612 (int64_t)delta.tv_sec, (int64_t)delta.tv_usec,
613 (int64_t)inst->rusage.ru_utime.tv_sec,
614 (int64_t)inst->rusage.ru_utime.tv_usec,
615 (int64_t)inst->rusage.ru_stime.tv_sec,
616 (int64_t)inst->rusage.ru_stime.tv_usec);
617 else
618 fprintf(stdout, "%s: status %d, rss %ld, "
619 "real %"PRId64".%06"PRId64", "
620 "user %"PRId64".%06"PRId64", "
621 "sys %"PRId64".%06"PRId64"\n",
622 fs_get_device(inst->fs),
623 inst->exit_status,
624 inst->rusage.ru_maxrss,
625 (int64_t)delta.tv_sec, (int64_t)delta.tv_usec,
626 (int64_t)inst->rusage.ru_utime.tv_sec,
627 (int64_t)inst->rusage.ru_utime.tv_usec,
628 (int64_t)inst->rusage.ru_stime.tv_sec,
629 (int64_t)inst->rusage.ru_stime.tv_usec);
630 }
631
632 /*
633 * Execute a particular fsck program, and link it into the list of
634 * child processes we are waiting for.
635 */
636 static int execute(const char *progname, const char *progpath,
637 const char *type, struct libmnt_fs *fs, int interactive)
638 {
639 char *argv[80];
640 int argc, i;
641 struct fsck_instance *inst, *p;
642 pid_t pid;
643
644 inst = xcalloc(1, sizeof(*inst));
645
646 argv[0] = xstrdup(progname);
647 argc = 1;
648
649 for (i=0; i <num_args; i++)
650 argv[argc++] = xstrdup(args[i]);
651
652 if (progress &&
653 ((strcmp(type, "ext2") == 0) ||
654 (strcmp(type, "ext3") == 0) ||
655 (strcmp(type, "ext4") == 0) ||
656 (strcmp(type, "ext4dev") == 0))) {
657
658 char tmp[80];
659 tmp[0] = 0;
660 if (!progress_active()) {
661 snprintf(tmp, 80, "-C%d", progress_fd);
662 inst->flags |= FLAG_PROGRESS;
663 } else if (progress_fd)
664 snprintf(tmp, 80, "-C%d", progress_fd * -1);
665 if (tmp[0])
666 argv[argc++] = xstrdup(tmp);
667 }
668
669 argv[argc++] = xstrdup(fs_get_device(fs));
670 argv[argc] = NULL;
671
672 if (verbose || noexecute) {
673 const char *tgt = mnt_fs_get_target(fs);
674
675 if (!tgt)
676 tgt = fs_get_device(fs);
677 printf("[%s (%d) -- %s] ", progpath, num_running, tgt);
678 for (i=0; i < argc; i++)
679 printf("%s ", argv[i]);
680 printf("\n");
681 }
682
683 mnt_ref_fs(fs);
684 inst->fs = fs;
685 inst->lock = -1;
686
687 if (lockdisk)
688 lock_disk(inst);
689
690 /* Fork and execute the correct program. */
691 if (noexecute)
692 pid = -1;
693 else if ((pid = fork()) < 0) {
694 warn(_("fork failed"));
695 free_instance(inst);
696 return errno;
697 } else if (pid == 0) {
698 if (!interactive)
699 close(0);
700 execv(progpath, argv);
701 err(FSCK_EX_ERROR, _("%s: execute failed"), progpath);
702 }
703
704 for (i=0; i < argc; i++)
705 free(argv[i]);
706
707 inst->pid = pid;
708 inst->prog = xstrdup(progname);
709 inst->type = xstrdup(type);
710 gettime_monotonic(&inst->start_time);
711 inst->next = NULL;
712
713 /*
714 * Find the end of the list, so we add the instance on at the end.
715 */
716 for (p = instance_list; p && p->next; p = p->next);
717
718 if (p)
719 p->next = inst;
720 else
721 instance_list = inst;
722
723 return 0;
724 }
725
726 /*
727 * Send a signal to all outstanding fsck child processes
728 */
729 static int kill_all(int signum)
730 {
731 struct fsck_instance *inst;
732 int n = 0;
733
734 for (inst = instance_list; inst; inst = inst->next) {
735 if (inst->flags & FLAG_DONE)
736 continue;
737 if (inst->pid <= 0)
738 continue;
739 kill(inst->pid, signum);
740 n++;
741 }
742 return n;
743 }
744
745 /*
746 * Wait for one child process to exit; when it does, unlink it from
747 * the list of executing child processes, and return it.
748 */
749 static struct fsck_instance *wait_one(int flags)
750 {
751 int status = 0;
752 int sig;
753 struct fsck_instance *inst, *inst2, *prev;
754 pid_t pid;
755 struct rusage rusage;
756
757 if (!instance_list)
758 return NULL;
759
760 if (noexecute) {
761 inst = instance_list;
762 prev = NULL;
763 #ifdef RANDOM_DEBUG
764 while (inst->next && (random() & 1)) {
765 prev = inst;
766 inst = inst->next;
767 }
768 #endif
769 inst->exit_status = 0;
770 goto ret_inst;
771 }
772
773 /*
774 * gcc -Wall fails saving throw against stupidity
775 * (inst and prev are thought to be uninitialized variables)
776 */
777 inst = prev = NULL;
778
779 do {
780 pid = wait4(-1, &status, flags, &rusage);
781 if (cancel_requested && !kill_sent) {
782 kill_all(SIGTERM);
783 kill_sent++;
784 }
785 if ((pid == 0) && (flags & WNOHANG))
786 return NULL;
787 if (pid < 0) {
788 if ((errno == EINTR) || (errno == EAGAIN))
789 continue;
790 if (errno == ECHILD) {
791 warnx(_("wait: no more child process?!?"));
792 return NULL;
793 }
794 warn(_("waitpid failed"));
795 continue;
796 }
797 for (prev = NULL, inst = instance_list;
798 inst;
799 prev = inst, inst = inst->next) {
800 if (inst->pid == pid)
801 break;
802 }
803 } while (!inst);
804
805 if (WIFEXITED(status))
806 status = WEXITSTATUS(status);
807 else if (WIFSIGNALED(status)) {
808 sig = WTERMSIG(status);
809 if (sig == SIGINT) {
810 status = FSCK_EX_UNCORRECTED;
811 } else {
812 warnx(_("Warning... %s for device %s exited "
813 "with signal %d."),
814 inst->prog, fs_get_device(inst->fs), sig);
815 status = FSCK_EX_ERROR;
816 }
817 } else {
818 warnx(_("%s %s: status is %x, should never happen."),
819 inst->prog, fs_get_device(inst->fs), status);
820 status = FSCK_EX_ERROR;
821 }
822
823 inst->exit_status = status;
824 inst->flags |= FLAG_DONE;
825 gettime_monotonic(&inst->end_time);
826 memcpy(&inst->rusage, &rusage, sizeof(struct rusage));
827
828 if (progress && (inst->flags & FLAG_PROGRESS) &&
829 !progress_active()) {
830 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
831 if (inst2->flags & FLAG_DONE)
832 continue;
833 if (strcmp(inst2->type, "ext2") != 0 &&
834 strcmp(inst2->type, "ext3") &&
835 strcmp(inst2->type, "ext4") != 0 &&
836 strcmp(inst2->type, "ext4dev") != 0)
837 continue;
838 /*
839 * If we've just started the fsck, wait a tiny
840 * bit before sending the kill, to give it
841 * time to set up the signal handler
842 */
843 if (inst2->start_time.tv_sec < time(NULL) + 2) {
844 if (fork() == 0) {
845 sleep(1);
846 kill(inst2->pid, SIGUSR1);
847 exit(FSCK_EX_OK);
848 }
849 } else
850 kill(inst2->pid, SIGUSR1);
851 inst2->flags |= FLAG_PROGRESS;
852 break;
853 }
854 }
855 ret_inst:
856 if (prev)
857 prev->next = inst->next;
858 else
859 instance_list = inst->next;
860
861 print_stats(inst);
862
863 if (verbose > 1)
864 printf(_("Finished with %s (exit status %d)\n"),
865 fs_get_device(inst->fs), inst->exit_status);
866 num_running--;
867 return inst;
868 }
869
870 #define FLAG_WAIT_ALL 0
871 #define FLAG_WAIT_ATLEAST_ONE 1
872 /*
873 * Wait until all executing child processes have exited; return the
874 * logical OR of all of their exit code values.
875 */
876 static int wait_many(int flags)
877 {
878 struct fsck_instance *inst;
879 int global_status = 0;
880 int wait_flags = 0;
881
882 while ((inst = wait_one(wait_flags))) {
883 global_status |= inst->exit_status;
884 free_instance(inst);
885 #ifdef RANDOM_DEBUG
886 if (noexecute && (flags & WNOHANG) && !(random() % 3))
887 break;
888 #endif
889 if (flags & FLAG_WAIT_ATLEAST_ONE)
890 wait_flags = WNOHANG;
891 }
892 return global_status;
893 }
894
895 /*
896 * Run the fsck program on a particular device
897 *
898 * If the type is specified using -t, and it isn't prefixed with "no"
899 * (as in "noext2") and only one filesystem type is specified, then
900 * use that type regardless of what is specified in /etc/fstab.
901 *
902 * If the type isn't specified by the user, then use either the type
903 * specified in /etc/fstab, or DEFAULT_FSTYPE.
904 */
905 static int fsck_device(struct libmnt_fs *fs, int interactive)
906 {
907 char *progname, *progpath;
908 const char *type;
909 int retval;
910
911 fs_interpret_type(fs);
912
913 type = mnt_fs_get_fstype(fs);
914
915 if (type && strcmp(type, "auto") != 0)
916 ;
917 else if (fstype && strncmp(fstype, "no", 2) != 0 &&
918 strncmp(fstype, "opts=", 5) != 0 && strncmp(fstype, "loop", 4) != 0 &&
919 !strchr(fstype, ','))
920 type = fstype;
921 else
922 type = DEFAULT_FSTYPE;
923
924 xasprintf(&progname, "fsck.%s", type);
925
926 if (!find_fsck(progname, &progpath)) {
927 free(progname);
928 if (fs_check_required(type)) {
929 retval = ENOENT;
930 goto err;
931 }
932 return 0;
933 }
934
935 num_running++;
936 retval = execute(progname, progpath, type, fs, interactive);
937 free(progname);
938 free(progpath);
939 if (retval) {
940 num_running--;
941 goto err;
942 }
943 return 0;
944 err:
945 warnx(_("error %d (%s) while executing fsck.%s for %s"),
946 retval, strerror(errno), type, fs_get_device(fs));
947 return FSCK_EX_ERROR;
948 }
949
950
951 /*
952 * Deal with the fsck -t argument.
953 */
954 static struct fs_type_compile {
955 char **list;
956 int *type;
957 int negate;
958 } fs_type_compiled;
959
960 #define FS_TYPE_NORMAL 0
961 #define FS_TYPE_OPT 1
962 #define FS_TYPE_NEGOPT 2
963
964 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
965 {
966 char *cp, *list, *s;
967 int num = 2;
968 int negate, first_negate = 1;
969
970 if (fs_type) {
971 for (cp=fs_type; *cp; cp++) {
972 if (*cp == ',')
973 num++;
974 }
975 }
976
977 cmp->list = xcalloc(num, sizeof(char *));
978 cmp->type = xcalloc(num, sizeof(int));
979 cmp->negate = 0;
980
981 if (!fs_type)
982 return;
983
984 list = xstrdup(fs_type);
985 num = 0;
986 s = strtok(list, ",");
987 while(s) {
988 negate = 0;
989 if (strncmp(s, "no", 2) == 0) {
990 s += 2;
991 negate = 1;
992 } else if (*s == '!') {
993 s++;
994 negate = 1;
995 }
996 if (strcmp(s, "loop") == 0)
997 /* loop is really short-hand for opts=loop */
998 goto loop_special_case;
999 else if (strncmp(s, "opts=", 5) == 0) {
1000 s += 5;
1001 loop_special_case:
1002 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
1003 } else {
1004 if (first_negate) {
1005 cmp->negate = negate;
1006 first_negate = 0;
1007 }
1008 if ((negate && !cmp->negate) ||
1009 (!negate && cmp->negate)) {
1010 errx(FSCK_EX_USAGE,
1011 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
1012 "with 'no' or '!'."));
1013 }
1014 }
1015
1016 cmp->list[num++] = xstrdup(s);
1017 s = strtok(NULL, ",");
1018 }
1019 free(list);
1020 }
1021
1022 /*
1023 * This function returns true if a particular option appears in a
1024 * comma-delimited options list
1025 */
1026 static int opt_in_list(const char *opt, const char *optlist)
1027 {
1028 char *list, *s;
1029
1030 if (!optlist)
1031 return 0;
1032 list = xstrdup(optlist);
1033
1034 s = strtok(list, ",");
1035 while(s) {
1036 if (strcmp(s, opt) == 0) {
1037 free(list);
1038 return 1;
1039 }
1040 s = strtok(NULL, ",");
1041 }
1042 free(list);
1043 return 0;
1044 }
1045
1046 /* See if the filesystem matches the criteria given by the -t option */
1047 static int fs_match(struct libmnt_fs *fs, struct fs_type_compile *cmp)
1048 {
1049 int n, ret = 0, checked_type = 0;
1050 char *cp;
1051
1052 if (cmp->list == NULL || cmp->list[0] == NULL)
1053 return 1;
1054
1055 for (n=0; (cp = cmp->list[n]); n++) {
1056 switch (cmp->type[n]) {
1057 case FS_TYPE_NORMAL:
1058 {
1059 const char *type = mnt_fs_get_fstype(fs);
1060
1061 checked_type++;
1062 if (type && strcmp(cp, type) == 0)
1063 ret = 1;
1064 break;
1065 }
1066 case FS_TYPE_NEGOPT:
1067 if (opt_in_list(cp, mnt_fs_get_options(fs)))
1068 return 0;
1069 break;
1070 case FS_TYPE_OPT:
1071 if (!opt_in_list(cp, mnt_fs_get_options(fs)))
1072 return 0;
1073 break;
1074 }
1075 }
1076 if (checked_type == 0)
1077 return 1;
1078 return (cmp->negate ? !ret : ret);
1079 }
1080
1081 /*
1082 * Check if a device exists
1083 */
1084 static int device_exists(const char *device)
1085 {
1086 struct stat st;
1087
1088 if (stat(device, &st) == -1)
1089 return 0;
1090 if (!S_ISBLK(st.st_mode))
1091 return 0;
1092 return 1;
1093 }
1094
1095 static int fs_ignored_type(struct libmnt_fs *fs)
1096 {
1097 const char **ip, *type;
1098
1099 if (!mnt_fs_is_regularfs(fs))
1100 return 1;
1101
1102 type = mnt_fs_get_fstype(fs);
1103
1104 for(ip = ignored_types; type && *ip; ip++) {
1105 if (strcmp(type, *ip) == 0)
1106 return 1;
1107 }
1108
1109 return 0;
1110 }
1111
1112 /* Check if we should ignore this filesystem. */
1113 static int ignore(struct libmnt_fs *fs)
1114 {
1115 const char *type;
1116
1117 /*
1118 * If the pass number is 0, ignore it.
1119 */
1120 if (mnt_fs_get_passno(fs) == 0)
1121 return 1;
1122
1123 /*
1124 * If this is a bind mount, ignore it.
1125 */
1126 if (opt_in_list("bind", mnt_fs_get_options(fs))) {
1127 warnx(_("%s: skipping bad line in /etc/fstab: "
1128 "bind mount with nonzero fsck pass number"),
1129 mnt_fs_get_target(fs));
1130 return 1;
1131 }
1132
1133 /*
1134 * ignore devices that don't exist and have the "nofail" mount option
1135 */
1136 if (!device_exists(fs_get_device(fs))) {
1137 if (opt_in_list("nofail", mnt_fs_get_options(fs))) {
1138 if (verbose)
1139 printf(_("%s: skipping nonexistent device\n"),
1140 fs_get_device(fs));
1141 return 1;
1142 }
1143 if (verbose)
1144 printf(_("%s: nonexistent device (\"nofail\" fstab "
1145 "option may be used to skip this device)\n"),
1146 fs_get_device(fs));
1147 }
1148
1149 fs_interpret_type(fs);
1150
1151 /*
1152 * If a specific fstype is specified, and it doesn't match,
1153 * ignore it.
1154 */
1155 if (!fs_match(fs, &fs_type_compiled))
1156 return 1;
1157
1158 type = mnt_fs_get_fstype(fs);
1159 if (!type) {
1160 if (verbose)
1161 printf(_("%s: skipping unknown filesystem type\n"),
1162 fs_get_device(fs));
1163 return 1;
1164 }
1165
1166 /* Are we ignoring this type? */
1167 if (fs_ignored_type(fs))
1168 return 1;
1169
1170
1171
1172 /* See if the <fsck.fs> program is available. */
1173 if (!find_fsck(type, NULL)) {
1174 if (fs_check_required(type))
1175 warnx(_("cannot check %s: fsck.%s not found"),
1176 fs_get_device(fs), type);
1177 return 1;
1178 }
1179
1180 /* We can and want to check this file system type. */
1181 return 0;
1182 }
1183
1184 static int count_slaves(dev_t disk)
1185 {
1186 DIR *dir;
1187 struct dirent *dp;
1188 char dirname[PATH_MAX];
1189 int count = 0;
1190
1191 snprintf(dirname, sizeof(dirname),
1192 "/sys/dev/block/%u:%u/slaves/",
1193 major(disk), minor(disk));
1194
1195 if (!(dir = opendir(dirname)))
1196 return -1;
1197
1198 while ((dp = readdir(dir)) != NULL) {
1199 #ifdef _DIRENT_HAVE_D_TYPE
1200 if (dp->d_type != DT_UNKNOWN && dp->d_type != DT_LNK)
1201 continue;
1202 #endif
1203 if (dp->d_name[0] == '.' &&
1204 ((dp->d_name[1] == 0) ||
1205 ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
1206 continue;
1207
1208 count++;
1209 }
1210
1211 closedir(dir);
1212 return count;
1213 }
1214
1215 /*
1216 * Returns TRUE if a partition on the same disk is already being
1217 * checked.
1218 */
1219 static int disk_already_active(struct libmnt_fs *fs)
1220 {
1221 struct fsck_instance *inst;
1222 dev_t disk;
1223
1224 if (force_all_parallel)
1225 return 0;
1226
1227 if (instance_list && fs_is_stacked(instance_list->fs))
1228 /* any instance for a stacked device is already running */
1229 return 1;
1230
1231 disk = fs_get_disk(fs, 1);
1232
1233 /*
1234 * If we don't know the base device, assume that the device is
1235 * already active if there are any fsck instances running.
1236 *
1237 * Don't check a stacked device with any other disk too.
1238 */
1239 if (!disk || fs_is_stacked(fs))
1240 return (instance_list != NULL);
1241
1242 for (inst = instance_list; inst; inst = inst->next) {
1243 dev_t idisk = fs_get_disk(inst->fs, 0);
1244
1245 if (!idisk || disk == idisk)
1246 return 1;
1247 }
1248
1249 return 0;
1250 }
1251
1252 /* Check all file systems, using the /etc/fstab table. */
1253 static int check_all(void)
1254 {
1255 int not_done_yet = 1;
1256 int passno = 1;
1257 int pass_done;
1258 int status = FSCK_EX_OK;
1259
1260 struct libmnt_fs *fs;
1261 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_FORWARD);
1262
1263 if (!itr)
1264 err(FSCK_EX_ERROR, _("failed to allocate iterator"));
1265
1266 /*
1267 * Do an initial scan over the filesystem; mark filesystems
1268 * which should be ignored as done, and resolve any "auto"
1269 * filesystem types (done as a side-effect of calling ignore()).
1270 */
1271 while (mnt_table_next_fs(fstab, itr, &fs) == 0) {
1272 if (ignore(fs)) {
1273 fs_set_done(fs);
1274 continue;
1275 }
1276 }
1277
1278 if (verbose)
1279 fputs(_("Checking all file systems.\n"), stdout);
1280
1281 /*
1282 * Find and check the root filesystem.
1283 */
1284 if (!parallel_root) {
1285 fs = mnt_table_find_target(fstab, "/", MNT_ITER_FORWARD);
1286 if (fs) {
1287 if (!skip_root &&
1288 !fs_is_done(fs) &&
1289 !(ignore_mounted && is_mounted(fs))) {
1290 status |= fsck_device(fs, 1);
1291 status |= wait_many(FLAG_WAIT_ALL);
1292 if (status > FSCK_EX_NONDESTRUCT) {
1293 mnt_free_iter(itr);
1294 return status;
1295 }
1296 }
1297 fs_set_done(fs);
1298 }
1299 }
1300
1301 /*
1302 * This is for the bone-headed user who enters the root
1303 * filesystem twice. Skip root will skip all root entries.
1304 */
1305 if (skip_root) {
1306 mnt_reset_iter(itr, MNT_ITER_FORWARD);
1307
1308 while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1309 const char *tgt = mnt_fs_get_target(fs);
1310
1311 if (tgt && strcmp(tgt, "/") == 0)
1312 fs_set_done(fs);
1313 }
1314 }
1315
1316 while (not_done_yet) {
1317 not_done_yet = 0;
1318 pass_done = 1;
1319
1320 mnt_reset_iter(itr, MNT_ITER_FORWARD);
1321
1322 while(mnt_table_next_fs(fstab, itr, &fs) == 0) {
1323
1324 if (cancel_requested)
1325 break;
1326 if (fs_is_done(fs))
1327 continue;
1328 /*
1329 * If the filesystem's pass number is higher
1330 * than the current pass number, then we don't
1331 * do it yet.
1332 */
1333 if (mnt_fs_get_passno(fs) > passno) {
1334 not_done_yet++;
1335 continue;
1336 }
1337 if (ignore_mounted && is_mounted(fs)) {
1338 fs_set_done(fs);
1339 continue;
1340 }
1341 /*
1342 * If a filesystem on a particular device has
1343 * already been spawned, then we need to defer
1344 * this to another pass.
1345 */
1346 if (disk_already_active(fs)) {
1347 pass_done = 0;
1348 continue;
1349 }
1350 /*
1351 * Spawn off the fsck process
1352 */
1353 status |= fsck_device(fs, serialize);
1354 fs_set_done(fs);
1355
1356 /*
1357 * Only do one filesystem at a time, or if we
1358 * have a limit on the number of fsck's extant
1359 * at one time, apply that limit.
1360 */
1361 if (serialize ||
1362 (max_running && (num_running >= max_running))) {
1363 pass_done = 0;
1364 break;
1365 }
1366 }
1367 if (cancel_requested)
1368 break;
1369 if (verbose > 1)
1370 printf(_("--waiting-- (pass %d)\n"), passno);
1371
1372 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1373 FLAG_WAIT_ATLEAST_ONE);
1374 if (pass_done) {
1375 if (verbose > 1)
1376 printf("----------------------------------\n");
1377 passno++;
1378 } else
1379 not_done_yet++;
1380 }
1381
1382 if (cancel_requested && !kill_sent) {
1383 kill_all(SIGTERM);
1384 kill_sent++;
1385 }
1386
1387 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1388 mnt_free_iter(itr);
1389 return status;
1390 }
1391
1392 static void __attribute__((__noreturn__)) usage(void)
1393 {
1394 FILE *out = stdout;
1395 fputs(USAGE_HEADER, out);
1396 fprintf(out, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1397 program_invocation_short_name);
1398
1399 fputs(USAGE_SEPARATOR, out);
1400 fputs(_("Check and repair a Linux filesystem.\n"), out);
1401
1402 fputs(USAGE_OPTIONS, out);
1403 fputs(_(" -A check all filesystems\n"), out);
1404 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out);
1405 fputs(_(" -l lock the device to guarantee exclusive access\n"), out);
1406 fputs(_(" -M do not check mounted filesystems\n"), out);
1407 fputs(_(" -N do not execute, just show what would be done\n"), out);
1408 fputs(_(" -P check filesystems in parallel, including root\n"), out);
1409 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out);
1410 fputs(_(" -r [<fd>] report statistics for each device checked;\n"
1411 " file descriptor is for GUIs\n"), out);
1412 fputs(_(" -s serialize the checking operations\n"), out);
1413 fputs(_(" -T do not show the title on startup\n"), out);
1414 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1415 " <type> is allowed to be a comma-separated list\n"), out);
1416 fputs(_(" -V explain what is being done\n"), out);
1417
1418 fputs(USAGE_SEPARATOR, out);
1419 fprintf(out, " -?, --help %s\n", USAGE_OPTSTR_HELP);
1420 fprintf(out, " --version %s\n", USAGE_OPTSTR_VERSION);
1421 fputs(USAGE_SEPARATOR, out);
1422 fputs(_("See the specific fsck.* commands for available fs-options."), out);
1423 fprintf(out, USAGE_MAN_TAIL("fsck(8)"));
1424 exit(FSCK_EX_OK);
1425 }
1426
1427 static void signal_cancel(int sig __attribute__((__unused__)))
1428 {
1429 cancel_requested = 1;
1430 }
1431
1432 static void parse_argv(int argc, char *argv[])
1433 {
1434 int i, j;
1435 char *arg, *dev, *tmp = NULL;
1436 char options[128];
1437 int opt = 0;
1438 int opts_for_fsck = 0;
1439 struct sigaction sa;
1440 int report_stats_fd = -1;
1441
1442 /*
1443 * Set up signal action
1444 */
1445 memset(&sa, 0, sizeof(struct sigaction));
1446 sa.sa_handler = signal_cancel;
1447 sigaction(SIGINT, &sa, NULL);
1448 sigaction(SIGTERM, &sa, NULL);
1449
1450 num_devices = 0;
1451 num_args = 0;
1452 instance_list = NULL;
1453
1454 for (i=1; i < argc; i++) {
1455 arg = argv[i];
1456 if (!arg)
1457 continue;
1458
1459 /* the only two longopts to satisfy UL standards */
1460 if (!opts_for_fsck && !strcmp(arg, "--help"))
1461 usage();
1462 if (!opts_for_fsck && !strcmp(arg, "--version"))
1463 print_version(FSCK_EX_OK);
1464
1465 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1466 if (num_devices >= MAX_DEVICES)
1467 errx(FSCK_EX_ERROR, _("too many devices"));
1468
1469 dev = mnt_resolve_spec(arg, mntcache);
1470
1471 if (!dev && strchr(arg, '=')) {
1472 /*
1473 * Check to see if we failed because
1474 * /proc/partitions isn't found.
1475 */
1476 if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) {
1477 warn(_("cannot open %s"),
1478 _PATH_PROC_PARTITIONS);
1479 errx(FSCK_EX_ERROR, _("Is /proc mounted?"));
1480 }
1481 /*
1482 * Check to see if this is because
1483 * we're not running as root
1484 */
1485 if (geteuid())
1486 errx(FSCK_EX_ERROR,
1487 _("must be root to scan for matching filesystems: %s"),
1488 arg);
1489 else
1490 errx(FSCK_EX_ERROR,
1491 _("couldn't find matching filesystem: %s"),
1492 arg);
1493 }
1494 devices[num_devices++] = dev ? dev : xstrdup(arg);
1495 continue;
1496 }
1497 if (arg[0] != '-' || opts_for_fsck) {
1498 if (num_args >= MAX_ARGS)
1499 errx(FSCK_EX_ERROR, _("too many arguments"));
1500 args[num_args++] = xstrdup(arg);
1501 continue;
1502 }
1503 for (j=1; arg[j]; j++) {
1504 if (opts_for_fsck) {
1505 options[++opt] = arg[j];
1506 continue;
1507 }
1508 switch (arg[j]) {
1509 case 'A':
1510 doall = 1;
1511 break;
1512 case 'C':
1513 progress = 1;
1514 if (arg[j+1]) { /* -C<fd> */
1515 progress_fd = string_to_int(arg+j+1);
1516 if (progress_fd < 0)
1517 progress_fd = 0;
1518 else
1519 goto next_arg;
1520 } else if (i+1 < argc && *argv[i+1] != '-') { /* -C <fd> */
1521 progress_fd = string_to_int(argv[i+1]);
1522 if (progress_fd < 0)
1523 progress_fd = 0;
1524 else {
1525 ++i;
1526 goto next_arg;
1527 }
1528 }
1529 break;
1530 case 'l':
1531 lockdisk = 1;
1532 break;
1533 case 'V':
1534 verbose++;
1535 break;
1536 case 'N':
1537 noexecute = 1;
1538 break;
1539 case 'R':
1540 skip_root = 1;
1541 break;
1542 case 'T':
1543 notitle = 1;
1544 break;
1545 case 'M':
1546 ignore_mounted = 1;
1547 break;
1548 case 'P':
1549 parallel_root = 1;
1550 break;
1551 case 'r':
1552 report_stats = 1;
1553 if (arg[j+1]) { /* -r<fd> */
1554 report_stats_fd = strtou32_or_err(arg+j+1, _("invalid argument of -r"));
1555 goto next_arg;
1556 } else if (i+1 < argc && *argv[i+1] >= '0' && *argv[i+1] <= '9') { /* -r <fd> */
1557 report_stats_fd = strtou32_or_err(argv[i+1], _("invalid argument of -r"));
1558 ++i;
1559 goto next_arg;
1560 }
1561 break;
1562 case 's':
1563 serialize = 1;
1564 break;
1565 case 't':
1566 tmp = NULL;
1567 if (fstype)
1568 errx(FSCK_EX_USAGE,
1569 _("option '%s' may be specified only once"), "-t");
1570 if (arg[j+1])
1571 tmp = arg+j+1;
1572 else if ((i+1) < argc)
1573 tmp = argv[++i];
1574 else
1575 errx(FSCK_EX_USAGE,
1576 _("option '%s' requires an argument"), "-t");
1577 fstype = xstrdup(tmp);
1578 compile_fs_type(fstype, &fs_type_compiled);
1579 goto next_arg;
1580 case '-':
1581 opts_for_fsck++;
1582 break;
1583 case '?':
1584 usage();
1585 break;
1586 default:
1587 options[++opt] = arg[j];
1588 break;
1589 }
1590 }
1591 next_arg:
1592 if (opt) {
1593 options[0] = '-';
1594 options[++opt] = '\0';
1595 if (num_args >= MAX_ARGS)
1596 errx(FSCK_EX_ERROR, _("too many arguments"));
1597 args[num_args++] = xstrdup(options);
1598 opt = 0;
1599 }
1600 }
1601
1602 /* Validate the report stats file descriptor to avoid disasters */
1603 if (report_stats_fd >= 0) {
1604 report_stats_file = fdopen(report_stats_fd, "w");
1605 if (!report_stats_file)
1606 err(FSCK_EX_ERROR,
1607 _("invalid argument of -r: %d"),
1608 report_stats_fd);
1609 }
1610
1611 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1612 force_all_parallel++;
1613 if (ul_strtos32(getenv("FSCK_MAX_INST"), &max_running, 10) != 0)
1614 max_running = 0;
1615 }
1616
1617 int main(int argc, char *argv[])
1618 {
1619 int i, status = 0;
1620 int interactive = 0;
1621 struct libmnt_fs *fs;
1622 const char *path = getenv("PATH");
1623
1624 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1625 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1626
1627 setlocale(LC_MESSAGES, "");
1628 setlocale(LC_CTYPE, "");
1629 bindtextdomain(PACKAGE, LOCALEDIR);
1630 textdomain(PACKAGE);
1631 close_stdout_atexit();
1632
1633 strutils_set_exitcode(FSCK_EX_USAGE);
1634 mnt_init_debug(0); /* init libmount debug mask */
1635 mntcache = mnt_new_cache(); /* no fatal error if failed */
1636
1637 if (mntcache)
1638 /* Force libblkid to accept also filesystems with bad
1639 * checksums. This feature is helpful for "fsck /dev/foo," but
1640 * if it evaluates LABEL/UUIDs from fstab, then libmount may
1641 * use cached data from udevd and udev accepts only properly
1642 * detected filesystems.
1643 */
1644 mnt_cache_set_sbprobe(mntcache, BLKID_SUBLKS_BADCSUM);
1645
1646
1647 parse_argv(argc, argv);
1648
1649 if (!notitle)
1650 printf(UTIL_LINUX_VERSION);
1651
1652 signal(SIGCHLD, SIG_DFL); /* clear any inherited settings */
1653
1654 load_fs_info();
1655
1656 fsck_path = xstrdup(path && *path ? path : FSCK_DEFAULT_PATH);
1657
1658 if ((num_devices == 1) || (serialize))
1659 interactive = 1;
1660
1661 if (lockdisk && (doall || num_devices > 1)) {
1662 warnx(_("the -l option can be used with one "
1663 "device only -- ignore"));
1664 lockdisk = 0;
1665 }
1666
1667 /* If -A was specified ("check all"), do that! */
1668 if (doall)
1669 return check_all();
1670
1671 if (num_devices == 0) {
1672 serialize++;
1673 interactive++;
1674 return check_all();
1675 }
1676 for (i = 0 ; i < num_devices; i++) {
1677 if (cancel_requested) {
1678 if (!kill_sent) {
1679 kill_all(SIGTERM);
1680 kill_sent++;
1681 }
1682 break;
1683 }
1684 fs = lookup(devices[i]);
1685 if (!fs)
1686 fs = add_dummy_fs(devices[i]);
1687 else if (fs_ignored_type(fs))
1688 continue;
1689 if (ignore_mounted && is_mounted(fs))
1690 continue;
1691 status |= fsck_device(fs, interactive);
1692 if (serialize ||
1693 (max_running && (num_running >= max_running))) {
1694 struct fsck_instance *inst;
1695
1696 inst = wait_one(0);
1697 if (inst) {
1698 status |= inst->exit_status;
1699 free_instance(inst);
1700 }
1701 if (verbose > 1)
1702 printf("----------------------------------\n");
1703 }
1704 }
1705 status |= wait_many(FLAG_WAIT_ALL);
1706 free(fsck_path);
1707 mnt_unref_cache(mntcache);
1708 mnt_unref_table(fstab);
1709 mnt_unref_table(mtab);
1710 return status;
1711 }