Cosmetics from 1.14
[tomato.git] / release / src / router / busybox / e2fsprogs / fsck.c
blob3c6cafbd3f2b1ddee90e2306ef2d682fb872151d
1 /* vi: set sw=4 ts=4: */
2 /*
3 * fsck --- A generic, parallelizing front-end for the fsck program.
4 * It will automatically try to run fsck programs in parallel if the
5 * devices are on separate spindles. It is based on the same ideas as
6 * the generic front end for fsck by David Engel and Fred van Kempen,
7 * but it has been completely rewritten from scratch to support
8 * parallel execution.
10 * Written by Theodore Ts'o, <tytso@mit.edu>
12 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
13 * o Changed -t fstype to behave like with mount when -A (all file
14 * systems) or -M (like mount) is specified.
15 * o fsck looks if it can find the fsck.type program to decide
16 * if it should ignore the fs type. This way more fsck programs
17 * can be added without changing this front-end.
18 * o -R flag skip root file system.
20 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
21 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
23 * Licensed under GPLv2, see file LICENSE in this tarball for details.
26 /* All filesystem specific hooks have been removed.
27 * If filesystem cannot be determined, we will execute
28 * "fsck.auto". Currently this also happens if you specify
29 * UUID=xxx or LABEL=xxx as an object to check.
30 * Detection code for that is also probably has to be in fsck.auto.
32 * In other words, this is _really_ is just a driver program which
33 * spawns actual fsck.something for each filesystem to check.
34 * It doesn't guess filesystem types from on-disk format.
37 #include "libbb.h"
39 /* "progress indicator" code is somewhat buggy and ext[23] specific.
40 * We should be filesystem agnostic. IOW: there should be a well-defined
41 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
42 #define DO_PROGRESS_INDICATOR 0
44 #define EXIT_OK 0
45 #define EXIT_NONDESTRUCT 1
46 #define EXIT_DESTRUCT 2
47 #define EXIT_UNCORRECTED 4
48 #define EXIT_ERROR 8
49 #define EXIT_USAGE 16
50 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
53 * Internal structure for mount table entries.
56 struct fs_info {
57 struct fs_info *next;
58 char *device;
59 char *mountpt;
60 char *type;
61 char *opts;
62 int passno;
63 int flags;
66 #define FLAG_DONE 1
67 #define FLAG_PROGRESS 2
69 * Structure to allow exit codes to be stored
71 struct fsck_instance {
72 struct fsck_instance *next;
73 int pid;
74 int flags;
75 #if DO_PROGRESS_INDICATOR
76 time_t start_time;
77 #endif
78 char *prog;
79 char *device;
80 char *base_device; /* /dev/hda for /dev/hdaN etc */
83 static const char ignored_types[] ALIGN1 =
84 "ignore\0"
85 "iso9660\0"
86 "nfs\0"
87 "proc\0"
88 "sw\0"
89 "swap\0"
90 "tmpfs\0"
91 "devpts\0";
93 #if 0
94 static const char really_wanted[] ALIGN1 =
95 "minix\0"
96 "ext2\0"
97 "ext3\0"
98 "jfs\0"
99 "reiserfs\0"
100 "xiafs\0"
101 "xfs\0";
102 #endif
104 #define BASE_MD "/dev/md"
106 static char **devices;
107 static char **args;
108 static int num_devices;
109 static int num_args;
110 static int verbose;
112 #define FS_TYPE_FLAG_NORMAL 0
113 #define FS_TYPE_FLAG_OPT 1
114 #define FS_TYPE_FLAG_NEGOPT 2
115 static char **fs_type_list;
116 static uint8_t *fs_type_flag;
117 static smallint fs_type_negated;
119 static volatile smallint cancel_requested;
120 static smallint doall;
121 static smallint noexecute;
122 static smallint serialize;
123 static smallint skip_root;
124 /* static smallint like_mount; */
125 static smallint notitle;
126 static smallint parallel_root;
127 static smallint force_all_parallel;
129 #if DO_PROGRESS_INDICATOR
130 static smallint progress;
131 static int progress_fd;
132 #endif
134 static int num_running;
135 static int max_running;
136 static char *fstype;
137 static struct fs_info *filesys_info;
138 static struct fs_info *filesys_last;
139 static struct fsck_instance *instance_list;
142 * Return the "base device" given a particular device; this is used to
143 * assure that we only fsck one partition on a particular drive at any
144 * one time. Otherwise, the disk heads will be seeking all over the
145 * place. If the base device cannot be determined, return NULL.
147 * The base_device() function returns an allocated string which must
148 * be freed.
150 #if ENABLE_FEATURE_DEVFS
152 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
153 * pathames.
155 static const char *const devfs_hier[] = {
156 "host", "bus", "target", "lun", NULL
158 #endif
160 static char *base_device(const char *device)
162 char *str, *cp;
163 #if ENABLE_FEATURE_DEVFS
164 const char *const *hier;
165 const char *disk;
166 int len;
167 #endif
168 cp = str = xstrdup(device);
170 /* Skip over /dev/; if it's not present, give up. */
171 if (strncmp(cp, "/dev/", 5) != 0)
172 goto errout;
173 cp += 5;
176 * For md devices, we treat them all as if they were all
177 * on one disk, since we don't know how to parallelize them.
179 if (cp[0] == 'm' && cp[1] == 'd') {
180 cp[2] = 0;
181 return str;
184 /* Handle DAC 960 devices */
185 if (strncmp(cp, "rd/", 3) == 0) {
186 cp += 3;
187 if (cp[0] != 'c' || !isdigit(cp[1])
188 || cp[2] != 'd' || !isdigit(cp[3]))
189 goto errout;
190 cp[4] = 0;
191 return str;
194 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
195 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
196 cp += 2;
197 /* If there's a single number after /dev/hd, skip it */
198 if (isdigit(*cp))
199 cp++;
200 /* What follows must be an alpha char, or give up */
201 if (!isalpha(*cp))
202 goto errout;
203 cp[1] = 0;
204 return str;
207 #if ENABLE_FEATURE_DEVFS
208 /* Now let's handle devfs (ugh) names */
209 len = 0;
210 if (strncmp(cp, "ide/", 4) == 0)
211 len = 4;
212 if (strncmp(cp, "scsi/", 5) == 0)
213 len = 5;
214 if (len) {
215 cp += len;
217 * Now we proceed down the expected devfs hierarchy.
218 * i.e., .../host1/bus2/target3/lun4/...
219 * If we don't find the expected token, followed by
220 * some number of digits at each level, abort.
222 for (hier = devfs_hier; *hier; hier++) {
223 len = strlen(*hier);
224 if (strncmp(cp, *hier, len) != 0)
225 goto errout;
226 cp += len;
227 while (*cp != '/' && *cp != 0) {
228 if (!isdigit(*cp))
229 goto errout;
230 cp++;
232 cp++;
234 cp[-1] = 0;
235 return str;
238 /* Now handle devfs /dev/disc or /dev/disk names */
239 disk = 0;
240 if (strncmp(cp, "discs/", 6) == 0)
241 disk = "disc";
242 else if (strncmp(cp, "disks/", 6) == 0)
243 disk = "disk";
244 if (disk) {
245 cp += 6;
246 if (strncmp(cp, disk, 4) != 0)
247 goto errout;
248 cp += 4;
249 while (*cp != '/' && *cp != 0) {
250 if (!isdigit(*cp))
251 goto errout;
252 cp++;
254 *cp = 0;
255 return str;
257 #endif
258 errout:
259 free(str);
260 return NULL;
263 static void free_instance(struct fsck_instance *p)
265 free(p->prog);
266 free(p->device);
267 free(p->base_device);
268 free(p);
271 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
272 const char *type, const char *opts,
273 int passno)
275 struct fs_info *fs;
277 fs = xzalloc(sizeof(*fs));
278 fs->device = xstrdup(device);
279 fs->mountpt = xstrdup(mntpnt);
280 if (strchr(type, ','))
281 type = (char *)"auto";
282 fs->type = xstrdup(type);
283 fs->opts = xstrdup(opts ? opts : "");
284 fs->passno = passno < 0 ? 1 : passno;
285 /*fs->flags = 0; */
286 /*fs->next = NULL; */
288 if (!filesys_info)
289 filesys_info = fs;
290 else
291 filesys_last->next = fs;
292 filesys_last = fs;
294 return fs;
297 /* Load the filesystem database from /etc/fstab */
298 static void load_fs_info(const char *filename)
300 FILE *fstab;
301 struct mntent mte;
302 struct fs_info *fs;
304 fstab = setmntent(filename, "r");
305 if (!fstab) {
306 bb_perror_msg("cannot read %s", filename);
307 return;
310 // Loop through entries
311 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
312 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
313 // mte.mnt_type, mte.mnt_opts,
314 // mte.mnt_passno);
315 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
316 mte.mnt_type, mte.mnt_opts,
317 mte.mnt_passno);
319 endmntent(fstab);
322 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
323 static struct fs_info *lookup(char *filesys)
325 struct fs_info *fs;
327 for (fs = filesys_info; fs; fs = fs->next) {
328 if (strcmp(filesys, fs->device) == 0
329 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
331 break;
334 return fs;
337 #if DO_PROGRESS_INDICATOR
338 static int progress_active(void)
340 struct fsck_instance *inst;
342 for (inst = instance_list; inst; inst = inst->next) {
343 if (inst->flags & FLAG_DONE)
344 continue;
345 if (inst->flags & FLAG_PROGRESS)
346 return 1;
348 return 0;
350 #endif
354 * Send a signal to all outstanding fsck child processes
356 static void kill_all_if_cancel_requested(void)
358 static smallint kill_sent;
360 struct fsck_instance *inst;
362 if (!cancel_requested || kill_sent)
363 return;
365 for (inst = instance_list; inst; inst = inst->next) {
366 if (inst->flags & FLAG_DONE)
367 continue;
368 kill(inst->pid, SIGTERM);
370 kill_sent = 1;
374 * Wait for one child process to exit; when it does, unlink it from
375 * the list of executing child processes, free, and return its exit status.
376 * If there is no exited child, return -1.
378 static int wait_one(int flags)
380 int status;
381 int sig;
382 struct fsck_instance *inst, *prev;
383 pid_t pid;
385 if (!instance_list)
386 return -1;
387 /* if (noexecute) { already returned -1; } */
389 while (1) {
390 pid = waitpid(-1, &status, flags);
391 kill_all_if_cancel_requested();
392 if (pid == 0) /* flags == WNOHANG and no children exited */
393 return -1;
394 if (pid < 0) {
395 if (errno == EINTR)
396 continue;
397 if (errno == ECHILD) { /* paranoia */
398 bb_error_msg("wait: no more children");
399 return -1;
401 bb_perror_msg("wait");
402 continue;
404 prev = NULL;
405 inst = instance_list;
406 do {
407 if (inst->pid == pid)
408 goto child_died;
409 prev = inst;
410 inst = inst->next;
411 } while (inst);
413 child_died:
415 if (WIFEXITED(status))
416 status = WEXITSTATUS(status);
417 else if (WIFSIGNALED(status)) {
418 sig = WTERMSIG(status);
419 status = EXIT_UNCORRECTED;
420 if (sig != SIGINT) {
421 printf("Warning: %s %s terminated "
422 "by signal %d\n",
423 inst->prog, inst->device, sig);
424 status = EXIT_ERROR;
426 } else {
427 printf("%s %s: status is %x, should never happen\n",
428 inst->prog, inst->device, status);
429 status = EXIT_ERROR;
432 #if DO_PROGRESS_INDICATOR
433 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
434 struct fsck_instance *inst2;
435 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
436 if (inst2->flags & FLAG_DONE)
437 continue;
438 if (strcmp(inst2->type, "ext2") != 0
439 && strcmp(inst2->type, "ext3") != 0
441 continue;
443 /* ext[23], we will send USR1
444 * (request to start displaying progress bar)
446 * If we've just started the fsck, wait a tiny
447 * bit before sending the kill, to give it
448 * time to set up the signal handler
450 if (inst2->start_time >= time(NULL) - 1)
451 sleep(1);
452 kill(inst2->pid, SIGUSR1);
453 inst2->flags |= FLAG_PROGRESS;
454 break;
457 #endif
459 if (prev)
460 prev->next = inst->next;
461 else
462 instance_list = inst->next;
463 if (verbose > 1)
464 printf("Finished with %s (exit status %d)\n",
465 inst->device, status);
466 num_running--;
467 free_instance(inst);
469 return status;
473 * Wait until all executing child processes have exited; return the
474 * logical OR of all of their exit code values.
476 #define FLAG_WAIT_ALL 0
477 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
478 static int wait_many(int flags)
480 int exit_status;
481 int global_status = 0;
482 int wait_flags = 0;
484 while ((exit_status = wait_one(wait_flags)) != -1) {
485 global_status |= exit_status;
486 wait_flags |= flags;
488 return global_status;
492 * Execute a particular fsck program, and link it into the list of
493 * child processes we are waiting for.
495 static void execute(const char *type, const char *device,
496 const char *mntpt /*, int interactive */)
498 char *argv[num_args + 4]; /* see count below: */
499 int argc;
500 int i;
501 struct fsck_instance *inst;
502 pid_t pid;
504 argv[0] = xasprintf("fsck.%s", type); /* 1 */
505 for (i = 0; i < num_args; i++)
506 argv[i+1] = args[i]; /* num_args */
507 argc = num_args + 1;
509 #if DO_PROGRESS_INDICATOR
510 if (progress && !progress_active()) {
511 if (strcmp(type, "ext2") == 0
512 || strcmp(type, "ext3") == 0
514 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
515 inst->flags |= FLAG_PROGRESS;
518 #endif
520 argv[argc++] = (char*)device; /* 1 */
521 argv[argc] = NULL; /* 1 */
523 if (verbose || noexecute) {
524 printf("[%s (%d) -- %s]", argv[0], num_running,
525 mntpt ? mntpt : device);
526 for (i = 0; i < argc; i++)
527 printf(" %s", argv[i]);
528 bb_putchar('\n');
531 /* Fork and execute the correct program. */
532 pid = -1;
533 if (!noexecute) {
534 pid = spawn(argv);
535 if (pid < 0)
536 bb_simple_perror_msg(argv[0]);
539 #if DO_PROGRESS_INDICATOR
540 free(argv[num_args + 1]);
541 #endif
543 /* No child, so don't record an instance */
544 if (pid <= 0) {
545 free(argv[0]);
546 return;
549 inst = xzalloc(sizeof(*inst));
550 inst->pid = pid;
551 inst->prog = argv[0];
552 inst->device = xstrdup(device);
553 inst->base_device = base_device(device);
554 #if DO_PROGRESS_INDICATOR
555 inst->start_time = time(NULL);
556 #endif
558 /* Add to the list of running fsck's.
559 * (was adding to the end, but adding to the front is simpler...) */
560 inst->next = instance_list;
561 instance_list = inst;
565 * Run the fsck program on a particular device
567 * If the type is specified using -t, and it isn't prefixed with "no"
568 * (as in "noext2") and only one filesystem type is specified, then
569 * use that type regardless of what is specified in /etc/fstab.
571 * If the type isn't specified by the user, then use either the type
572 * specified in /etc/fstab, or "auto".
574 static void fsck_device(struct fs_info *fs /*, int interactive */)
576 const char *type;
578 if (strcmp(fs->type, "auto") != 0) {
579 type = fs->type;
580 if (verbose > 2)
581 bb_info_msg("using filesystem type '%s' %s",
582 type, "from fstab");
583 } else if (fstype
584 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
585 && strncmp(fstype, "opts=", 5) != 0
586 && strncmp(fstype, "loop", 4) != 0
587 && !strchr(fstype, ',')
589 type = fstype;
590 if (verbose > 2)
591 bb_info_msg("using filesystem type '%s' %s",
592 type, "from -t");
593 } else {
594 type = "auto";
595 if (verbose > 2)
596 bb_info_msg("using filesystem type '%s' %s",
597 type, "(default)");
600 num_running++;
601 execute(type, fs->device, fs->mountpt /*, interactive */);
605 * Returns TRUE if a partition on the same disk is already being
606 * checked.
608 static int device_already_active(char *device)
610 struct fsck_instance *inst;
611 char *base;
613 if (force_all_parallel)
614 return 0;
616 #ifdef BASE_MD
617 /* Don't check a soft raid disk with any other disk */
618 if (instance_list
619 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
620 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
622 return 1;
624 #endif
626 base = base_device(device);
628 * If we don't know the base device, assume that the device is
629 * already active if there are any fsck instances running.
631 if (!base)
632 return (instance_list != NULL);
634 for (inst = instance_list; inst; inst = inst->next) {
635 if (!inst->base_device || !strcmp(base, inst->base_device)) {
636 free(base);
637 return 1;
641 free(base);
642 return 0;
646 * This function returns true if a particular option appears in a
647 * comma-delimited options list
649 static int opt_in_list(char *opt, char *optlist)
651 char *s;
652 int len;
654 if (!optlist)
655 return 0;
657 len = strlen(opt);
658 s = optlist - 1;
659 while (1) {
660 s = strstr(s + 1, opt);
661 if (!s)
662 return 0;
663 /* neither "opt.." nor "xxx,opt.."? */
664 if (s != optlist && s[-1] != ',')
665 continue;
666 /* neither "..opt" nor "..opt,xxx"? */
667 if (s[len] != '\0' && s[len] != ',')
668 continue;
669 return 1;
673 /* See if the filesystem matches the criteria given by the -t option */
674 static int fs_match(struct fs_info *fs)
676 int n, ret, checked_type;
677 char *cp;
679 if (!fs_type_list)
680 return 1;
682 ret = 0;
683 checked_type = 0;
684 n = 0;
685 while (1) {
686 cp = fs_type_list[n];
687 if (!cp)
688 break;
689 switch (fs_type_flag[n]) {
690 case FS_TYPE_FLAG_NORMAL:
691 checked_type++;
692 if (strcmp(cp, fs->type) == 0)
693 ret = 1;
694 break;
695 case FS_TYPE_FLAG_NEGOPT:
696 if (opt_in_list(cp, fs->opts))
697 return 0;
698 break;
699 case FS_TYPE_FLAG_OPT:
700 if (!opt_in_list(cp, fs->opts))
701 return 0;
702 break;
704 n++;
706 if (checked_type == 0)
707 return 1;
709 return (fs_type_negated ? !ret : ret);
712 /* Check if we should ignore this filesystem. */
713 static int ignore(struct fs_info *fs)
716 * If the pass number is 0, ignore it.
718 if (fs->passno == 0)
719 return 1;
722 * If a specific fstype is specified, and it doesn't match,
723 * ignore it.
725 if (!fs_match(fs))
726 return 1;
728 /* Are we ignoring this type? */
729 if (index_in_strings(ignored_types, fs->type) >= 0)
730 return 1;
732 /* We can and want to check this file system type. */
733 return 0;
736 /* Check all file systems, using the /etc/fstab table. */
737 static int check_all(void)
739 struct fs_info *fs;
740 int status = EXIT_OK;
741 smallint not_done_yet;
742 smallint pass_done;
743 int passno;
745 if (verbose)
746 puts("Checking all filesystems");
749 * Do an initial scan over the filesystem; mark filesystems
750 * which should be ignored as done, and resolve any "auto"
751 * filesystem types (done as a side-effect of calling ignore()).
753 for (fs = filesys_info; fs; fs = fs->next)
754 if (ignore(fs))
755 fs->flags |= FLAG_DONE;
758 * Find and check the root filesystem.
760 if (!parallel_root) {
761 for (fs = filesys_info; fs; fs = fs->next) {
762 if (LONE_CHAR(fs->mountpt, '/')) {
763 if (!skip_root && !ignore(fs)) {
764 fsck_device(fs /*, 1*/);
765 status |= wait_many(FLAG_WAIT_ALL);
766 if (status > EXIT_NONDESTRUCT)
767 return status;
769 fs->flags |= FLAG_DONE;
770 break;
775 * This is for the bone-headed user who has root
776 * filesystem listed twice.
777 * "Skip root" will skip _all_ root entries.
779 if (skip_root)
780 for (fs = filesys_info; fs; fs = fs->next)
781 if (LONE_CHAR(fs->mountpt, '/'))
782 fs->flags |= FLAG_DONE;
784 not_done_yet = 1;
785 passno = 1;
786 while (not_done_yet) {
787 not_done_yet = 0;
788 pass_done = 1;
790 for (fs = filesys_info; fs; fs = fs->next) {
791 if (cancel_requested)
792 break;
793 if (fs->flags & FLAG_DONE)
794 continue;
796 * If the filesystem's pass number is higher
797 * than the current pass number, then we didn't
798 * do it yet.
800 if (fs->passno > passno) {
801 not_done_yet = 1;
802 continue;
805 * If a filesystem on a particular device has
806 * already been spawned, then we need to defer
807 * this to another pass.
809 if (device_already_active(fs->device)) {
810 pass_done = 0;
811 continue;
814 * Spawn off the fsck process
816 fsck_device(fs /*, serialize*/);
817 fs->flags |= FLAG_DONE;
820 * Only do one filesystem at a time, or if we
821 * have a limit on the number of fsck's extant
822 * at one time, apply that limit.
824 if (serialize
825 || (max_running && (num_running >= max_running))
827 pass_done = 0;
828 break;
831 if (cancel_requested)
832 break;
833 if (verbose > 1)
834 printf("--waiting-- (pass %d)\n", passno);
835 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
836 FLAG_WAIT_ATLEAST_ONE);
837 if (pass_done) {
838 if (verbose > 1)
839 puts("----------------------------------");
840 passno++;
841 } else
842 not_done_yet = 1;
844 kill_all_if_cancel_requested();
845 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
846 return status;
850 * Deal with the fsck -t argument.
851 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
852 * Why here we require "-t novfat,nonfs" ??
854 static void compile_fs_type(char *fs_type)
856 char *s;
857 int num = 2;
858 smallint negate;
860 s = fs_type;
861 while ((s = strchr(s, ','))) {
862 num++;
863 s++;
866 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
867 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
868 fs_type_negated = -1; /* not yet known is it negated or not */
870 num = 0;
871 s = fs_type;
872 while (1) {
873 char *comma;
875 negate = 0;
876 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
877 s += 2;
878 negate = 1;
879 } else if (s[0] == '!') {
880 s++;
881 negate = 1;
884 if (strcmp(s, "loop") == 0)
885 /* loop is really short-hand for opts=loop */
886 goto loop_special_case;
887 if (strncmp(s, "opts=", 5) == 0) {
888 s += 5;
889 loop_special_case:
890 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
891 } else {
892 if (fs_type_negated == -1)
893 fs_type_negated = negate;
894 if (fs_type_negated != negate)
895 bb_error_msg_and_die(
896 "either all or none of the filesystem types passed to -t must be prefixed "
897 "with 'no' or '!'");
899 comma = strchr(s, ',');
900 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
901 if (!comma)
902 break;
903 s = comma + 1;
907 static void parse_args(char **argv)
909 int i, j;
910 char *arg, *tmp;
911 char *options;
912 int optpos;
913 int opts_for_fsck = 0;
915 /* in bss, so already zeroed
916 num_devices = 0;
917 num_args = 0;
918 instance_list = NULL;
921 for (i = 1; argv[i]; i++) {
922 arg = argv[i];
924 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
925 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
926 // FIXME: must check that arg is a blkdev, or resolve
927 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
928 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
929 devices = xrealloc_vector(devices, 2, num_devices);
930 devices[num_devices++] = xstrdup(arg);
931 continue;
934 if (arg[0] != '-' || opts_for_fsck) {
935 args = xrealloc_vector(args, 2, num_args);
936 args[num_args++] = xstrdup(arg);
937 continue;
940 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
941 opts_for_fsck = 1;
942 continue;
945 optpos = 0;
946 options = NULL;
947 for (j = 1; arg[j]; j++) {
948 switch (arg[j]) {
949 case 'A':
950 doall = 1;
951 break;
952 #if DO_PROGRESS_INDICATOR
953 case 'C':
954 progress = 1;
955 if (arg[++j]) { /* -Cn */
956 progress_fd = xatoi_u(&arg[j]);
957 goto next_arg;
959 /* -C n */
960 if (!argv[++i]) bb_show_usage();
961 progress_fd = xatoi_u(argv[i]);
962 goto next_arg;
963 #endif
964 case 'V':
965 verbose++;
966 break;
967 case 'N':
968 noexecute = 1;
969 break;
970 case 'R':
971 skip_root = 1;
972 break;
973 case 'T':
974 notitle = 1;
975 break;
976 /* case 'M':
977 like_mount = 1;
978 break; */
979 case 'P':
980 parallel_root = 1;
981 break;
982 case 's':
983 serialize = 1;
984 break;
985 case 't':
986 if (fstype)
987 bb_show_usage();
988 if (arg[++j])
989 tmp = &arg[j];
990 else if (argv[++i])
991 tmp = argv[i];
992 else
993 bb_show_usage();
994 fstype = xstrdup(tmp);
995 compile_fs_type(fstype);
996 goto next_arg;
997 case '?':
998 bb_show_usage();
999 break;
1000 default:
1001 optpos++;
1002 /* one extra for '\0' */
1003 options = xrealloc(options, optpos + 2);
1004 options[optpos] = arg[j];
1005 break;
1008 next_arg:
1009 if (optpos) {
1010 options[0] = '-';
1011 options[optpos + 1] = '\0';
1012 args = xrealloc_vector(args, 2, num_args);
1013 args[num_args++] = options;
1016 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1017 force_all_parallel = 1;
1018 tmp = getenv("FSCK_MAX_INST");
1019 if (tmp)
1020 max_running = xatoi(tmp);
1023 static void signal_cancel(int sig UNUSED_PARAM)
1025 cancel_requested = 1;
1028 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1029 int fsck_main(int argc UNUSED_PARAM, char **argv)
1031 int i, status;
1032 /*int interactive;*/
1033 const char *fstab;
1034 struct fs_info *fs;
1036 /* we want wait() to be interruptible */
1037 signal_no_SA_RESTART_empty_mask(SIGINT, signal_cancel);
1038 signal_no_SA_RESTART_empty_mask(SIGTERM, signal_cancel);
1040 setbuf(stdout, NULL);
1042 parse_args(argv);
1044 if (!notitle)
1045 puts("fsck (busybox "BB_VER", "BB_BT")");
1047 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1048 * so we are scanning it anyway */
1049 fstab = getenv("FSTAB_FILE");
1050 if (!fstab)
1051 fstab = "/etc/fstab";
1052 load_fs_info(fstab);
1054 /*interactive = (num_devices == 1) | serialize;*/
1056 if (num_devices == 0)
1057 /*interactive =*/ serialize = doall = 1;
1058 if (doall)
1059 return check_all();
1061 status = 0;
1062 for (i = 0; i < num_devices; i++) {
1063 if (cancel_requested) {
1064 kill_all_if_cancel_requested();
1065 break;
1068 fs = lookup(devices[i]);
1069 if (!fs)
1070 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1071 fsck_device(fs /*, interactive */);
1073 if (serialize
1074 || (max_running && (num_running >= max_running))
1076 int exit_status = wait_one(0);
1077 if (exit_status >= 0)
1078 status |= exit_status;
1079 if (verbose > 1)
1080 puts("----------------------------------");
1083 status |= wait_many(FLAG_WAIT_ALL);
1084 return status;