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