1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include "libbb.h"
51
52
53
54
55#define DO_PROGRESS_INDICATOR 0
56
57
58
59
60
61
62
63
64
65
66
67#define EXIT_OK 0
68#define EXIT_NONDESTRUCT 1
69#define EXIT_DESTRUCT 2
70#define EXIT_UNCORRECTED 4
71#define EXIT_ERROR 8
72#define EXIT_USAGE 16
73#define FSCK_CANCELED 32
74
75
76
77
78struct fs_info {
79 struct fs_info *next;
80 char *device;
81 char *mountpt;
82 char *type;
83 char *opts;
84 int passno;
85 int flags;
86};
87
88#define FLAG_DONE 1
89#define FLAG_PROGRESS 2
90
91
92
93struct fsck_instance {
94 struct fsck_instance *next;
95 int pid;
96 int flags;
97#if DO_PROGRESS_INDICATOR
98 time_t start_time;
99#endif
100 char *prog;
101 char *device;
102 char *base_device;
103};
104
105static const char ignored_types[] ALIGN1 =
106 "ignore\0"
107 "iso9660\0"
108 "nfs\0"
109 "proc\0"
110 "sw\0"
111 "swap\0"
112 "tmpfs\0"
113 "devpts\0";
114
115#if 0
116static const char really_wanted[] ALIGN1 =
117 "minix\0"
118 "ext2\0"
119 "ext3\0"
120 "jfs\0"
121 "reiserfs\0"
122 "xiafs\0"
123 "xfs\0";
124#endif
125
126#define BASE_MD "/dev/md"
127
128static char **args;
129static int num_args;
130static int verbose;
131
132#define FS_TYPE_FLAG_NORMAL 0
133#define FS_TYPE_FLAG_OPT 1
134#define FS_TYPE_FLAG_NEGOPT 2
135static char **fs_type_list;
136static uint8_t *fs_type_flag;
137static smallint fs_type_negated;
138
139static smallint noexecute;
140static smallint serialize;
141static smallint skip_root;
142
143static smallint parallel_root;
144static smallint force_all_parallel;
145
146#if DO_PROGRESS_INDICATOR
147static smallint progress;
148static int progress_fd;
149#endif
150
151static int num_running;
152static int max_running;
153static char *fstype;
154static struct fs_info *filesys_info;
155static struct fs_info *filesys_last;
156static struct fsck_instance *instance_list;
157
158
159
160
161
162
163
164
165
166
167#if ENABLE_FEATURE_DEVFS
168
169
170
171
172static const char *const devfs_hier[] = {
173 "host", "bus", "target", "lun", NULL
174};
175#endif
176
177static char *base_device(const char *device)
178{
179 char *str, *cp;
180#if ENABLE_FEATURE_DEVFS
181 const char *const *hier;
182 const char *disk;
183 int len;
184#endif
185 str = xstrdup(device);
186
187
188 cp = skip_dev_pfx(str);
189 if (cp == str)
190 goto errout;
191
192
193
194
195
196 if (cp[0] == 'm' && cp[1] == 'd') {
197 cp[2] = 0;
198 return str;
199 }
200
201
202 if (strncmp(cp, "rd/", 3) == 0) {
203 cp += 3;
204 if (cp[0] != 'c' || !isdigit(cp[1])
205 || cp[2] != 'd' || !isdigit(cp[3]))
206 goto errout;
207 cp[4] = 0;
208 return str;
209 }
210
211
212 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
213 cp += 2;
214
215 if (isdigit(*cp))
216 cp++;
217
218 if (!isalpha(*cp))
219 goto errout;
220 cp[1] = 0;
221 return str;
222 }
223
224#if ENABLE_FEATURE_DEVFS
225
226 len = 0;
227 if (strncmp(cp, "ide/", 4) == 0)
228 len = 4;
229 if (strncmp(cp, "scsi/", 5) == 0)
230 len = 5;
231 if (len) {
232 cp += len;
233
234
235
236
237
238
239 for (hier = devfs_hier; *hier; hier++) {
240 len = strlen(*hier);
241 if (strncmp(cp, *hier, len) != 0)
242 goto errout;
243 cp += len;
244 while (*cp != '/' && *cp != 0) {
245 if (!isdigit(*cp))
246 goto errout;
247 cp++;
248 }
249 cp++;
250 }
251 cp[-1] = 0;
252 return str;
253 }
254
255
256 disk = 0;
257 if (strncmp(cp, "discs/", 6) == 0)
258 disk = "disc";
259 else if (strncmp(cp, "disks/", 6) == 0)
260 disk = "disk";
261 if (disk) {
262 cp += 6;
263 if (strncmp(cp, disk, 4) != 0)
264 goto errout;
265 cp += 4;
266 while (*cp != '/' && *cp != 0) {
267 if (!isdigit(*cp))
268 goto errout;
269 cp++;
270 }
271 *cp = 0;
272 return str;
273 }
274#endif
275 errout:
276 free(str);
277 return NULL;
278}
279
280static void free_instance(struct fsck_instance *p)
281{
282 free(p->prog);
283 free(p->device);
284 free(p->base_device);
285 free(p);
286}
287
288static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
289 const char *type, const char *opts,
290 int passno)
291{
292 struct fs_info *fs;
293
294 fs = xzalloc(sizeof(*fs));
295 fs->device = xstrdup(device);
296 fs->mountpt = xstrdup(mntpnt);
297 if (strchr(type, ','))
298 type = (char *)"auto";
299 fs->type = xstrdup(type);
300 fs->opts = xstrdup(opts ? opts : "");
301 fs->passno = passno < 0 ? 1 : passno;
302
303
304
305 if (!filesys_info)
306 filesys_info = fs;
307 else
308 filesys_last->next = fs;
309 filesys_last = fs;
310
311 return fs;
312}
313
314
315static void load_fs_info(const char *filename)
316{
317 FILE *fstab;
318 struct mntent mte;
319
320 fstab = setmntent(filename, "r");
321 if (!fstab) {
322 bb_perror_msg("can't read '%s'", filename);
323 return;
324 }
325
326
327 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
328
329
330
331 create_fs_device(mte.mnt_fsname, mte.mnt_dir,
332 mte.mnt_type, mte.mnt_opts,
333 mte.mnt_passno);
334 }
335 endmntent(fstab);
336}
337
338
339static struct fs_info *lookup(char *filesys)
340{
341 struct fs_info *fs;
342
343 for (fs = filesys_info; fs; fs = fs->next) {
344 if (strcmp(filesys, fs->device) == 0
345 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
346 )
347 break;
348 }
349
350 return fs;
351}
352
353#if DO_PROGRESS_INDICATOR
354static int progress_active(void)
355{
356 struct fsck_instance *inst;
357
358 for (inst = instance_list; inst; inst = inst->next) {
359 if (inst->flags & FLAG_DONE)
360 continue;
361 if (inst->flags & FLAG_PROGRESS)
362 return 1;
363 }
364 return 0;
365}
366#endif
367
368
369
370
371
372static void kill_all_if_got_signal(void)
373{
374 static smallint kill_sent;
375
376 struct fsck_instance *inst;
377
378 if (!bb_got_signal || kill_sent)
379 return;
380
381 for (inst = instance_list; inst; inst = inst->next) {
382 if (inst->flags & FLAG_DONE)
383 continue;
384 kill(inst->pid, SIGTERM);
385 }
386 kill_sent = 1;
387}
388
389
390
391
392
393
394static int wait_one(int flags)
395{
396 int status;
397 int sig;
398 struct fsck_instance *inst, *prev;
399 pid_t pid;
400
401 if (!instance_list)
402 return -1;
403
404
405 while (1) {
406 pid = waitpid(-1, &status, flags);
407 kill_all_if_got_signal();
408 if (pid == 0)
409 return -1;
410 if (pid < 0) {
411 if (errno == EINTR)
412 continue;
413 if (errno == ECHILD) {
414 bb_error_msg("wait: no more children");
415 return -1;
416 }
417 bb_perror_msg("wait");
418 continue;
419 }
420 prev = NULL;
421 inst = instance_list;
422 do {
423 if (inst->pid == pid)
424 goto child_died;
425 prev = inst;
426 inst = inst->next;
427 } while (inst);
428 }
429 child_died:
430
431 if (WIFEXITED(status))
432 status = WEXITSTATUS(status);
433 else if (WIFSIGNALED(status)) {
434 sig = WTERMSIG(status);
435 status = EXIT_UNCORRECTED;
436 if (sig != SIGINT) {
437 printf("Warning: %s %s terminated "
438 "by signal %d\n",
439 inst->prog, inst->device, sig);
440 status = EXIT_ERROR;
441 }
442 } else {
443 printf("%s %s: status is %x, should never happen\n",
444 inst->prog, inst->device, status);
445 status = EXIT_ERROR;
446 }
447
448#if DO_PROGRESS_INDICATOR
449 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
450 struct fsck_instance *inst2;
451 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
452 if (inst2->flags & FLAG_DONE)
453 continue;
454 if (strcmp(inst2->type, "ext2") != 0
455 && strcmp(inst2->type, "ext3") != 0
456 ) {
457 continue;
458 }
459
460
461
462
463
464
465
466 if (inst2->start_time >= time(NULL) - 1)
467 sleep(1);
468 kill(inst2->pid, SIGUSR1);
469 inst2->flags |= FLAG_PROGRESS;
470 break;
471 }
472 }
473#endif
474
475 if (prev)
476 prev->next = inst->next;
477 else
478 instance_list = inst->next;
479 if (verbose > 1)
480 printf("Finished with %s (exit status %d)\n",
481 inst->device, status);
482 num_running--;
483 free_instance(inst);
484
485 return status;
486}
487
488
489
490
491
492#define FLAG_WAIT_ALL 0
493#define FLAG_WAIT_ATLEAST_ONE WNOHANG
494static int wait_many(int flags)
495{
496 int exit_status;
497 int global_status = 0;
498 int wait_flags = 0;
499
500 while ((exit_status = wait_one(wait_flags)) != -1) {
501 global_status |= exit_status;
502 wait_flags |= flags;
503 }
504 return global_status;
505}
506
507
508
509
510
511static void execute(const char *type, const char *device,
512 const char *mntpt )
513{
514 int i;
515 struct fsck_instance *inst;
516 pid_t pid;
517
518 args[0] = xasprintf("fsck.%s", type);
519
520#if DO_PROGRESS_INDICATOR
521 if (progress && !progress_active()) {
522 if (strcmp(type, "ext2") == 0
523 || strcmp(type, "ext3") == 0
524 ) {
525 args[XXX] = xasprintf("-C%d", progress_fd);
526 inst->flags |= FLAG_PROGRESS;
527 }
528 }
529#endif
530
531 args[num_args - 2] = (char*)device;
532
533
534 if (verbose || noexecute) {
535 printf("[%s (%d) -- %s]", args[0], num_running,
536 mntpt ? mntpt : device);
537 for (i = 0; args[i]; i++)
538 printf(" %s", args[i]);
539 bb_putchar('\n');
540 }
541
542
543 pid = -1;
544 if (!noexecute) {
545 pid = spawn(args);
546 if (pid < 0)
547 bb_simple_perror_msg(args[0]);
548 }
549
550#if DO_PROGRESS_INDICATOR
551 free(args[XXX]);
552#endif
553
554
555 if (pid <= 0) {
556 free(args[0]);
557 return;
558 }
559
560 inst = xzalloc(sizeof(*inst));
561 inst->pid = pid;
562 inst->prog = args[0];
563 inst->device = xstrdup(device);
564 inst->base_device = base_device(device);
565#if DO_PROGRESS_INDICATOR
566 inst->start_time = time(NULL);
567#endif
568
569
570
571 inst->next = instance_list;
572 instance_list = inst;
573}
574
575
576
577
578
579
580
581
582
583
584
585static void fsck_device(struct fs_info *fs )
586{
587 const char *type;
588
589 if (strcmp(fs->type, "auto") != 0) {
590 type = fs->type;
591 if (verbose > 2)
592 bb_info_msg("using filesystem type '%s' %s",
593 type, "from fstab");
594 } else if (fstype
595 && (fstype[0] != 'n' || fstype[1] != 'o')
596 && strncmp(fstype, "opts=", 5) != 0
597 && strncmp(fstype, "loop", 4) != 0
598 && !strchr(fstype, ',')
599 ) {
600 type = fstype;
601 if (verbose > 2)
602 bb_info_msg("using filesystem type '%s' %s",
603 type, "from -t");
604 } else {
605 type = "auto";
606 if (verbose > 2)
607 bb_info_msg("using filesystem type '%s' %s",
608 type, "(default)");
609 }
610
611 num_running++;
612 execute(type, fs->device, fs->mountpt );
613}
614
615
616
617
618
619static int device_already_active(char *device)
620{
621 struct fsck_instance *inst;
622 char *base;
623
624 if (force_all_parallel)
625 return 0;
626
627#ifdef BASE_MD
628
629 if (instance_list
630 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
631 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
632 ) {
633 return 1;
634 }
635#endif
636
637 base = base_device(device);
638
639
640
641
642 if (!base)
643 return (instance_list != NULL);
644
645 for (inst = instance_list; inst; inst = inst->next) {
646 if (!inst->base_device || !strcmp(base, inst->base_device)) {
647 free(base);
648 return 1;
649 }
650 }
651
652 free(base);
653 return 0;
654}
655
656
657
658
659
660static int opt_in_list(char *opt, char *optlist)
661{
662 char *s;
663 int len;
664
665 if (!optlist)
666 return 0;
667
668 len = strlen(opt);
669 s = optlist - 1;
670 while (1) {
671 s = strstr(s + 1, opt);
672 if (!s)
673 return 0;
674
675 if (s != optlist && s[-1] != ',')
676 continue;
677
678 if (s[len] != '\0' && s[len] != ',')
679 continue;
680 return 1;
681 }
682}
683
684
685static int fs_match(struct fs_info *fs)
686{
687 int n, ret, checked_type;
688 char *cp;
689
690 if (!fs_type_list)
691 return 1;
692
693 ret = 0;
694 checked_type = 0;
695 n = 0;
696 while (1) {
697 cp = fs_type_list[n];
698 if (!cp)
699 break;
700 switch (fs_type_flag[n]) {
701 case FS_TYPE_FLAG_NORMAL:
702 checked_type++;
703 if (strcmp(cp, fs->type) == 0)
704 ret = 1;
705 break;
706 case FS_TYPE_FLAG_NEGOPT:
707 if (opt_in_list(cp, fs->opts))
708 return 0;
709 break;
710 case FS_TYPE_FLAG_OPT:
711 if (!opt_in_list(cp, fs->opts))
712 return 0;
713 break;
714 }
715 n++;
716 }
717 if (checked_type == 0)
718 return 1;
719
720 return (fs_type_negated ? !ret : ret);
721}
722
723
724static int ignore(struct fs_info *fs)
725{
726
727
728
729 if (fs->passno == 0)
730 return 1;
731
732
733
734
735
736 if (!fs_match(fs))
737 return 1;
738
739
740 if (index_in_strings(ignored_types, fs->type) >= 0)
741 return 1;
742
743
744 return 0;
745}
746
747
748static int check_all(void)
749{
750 struct fs_info *fs;
751 int status = EXIT_OK;
752 smallint not_done_yet;
753 smallint pass_done;
754 int passno;
755
756 if (verbose)
757 puts("Checking all filesystems");
758
759
760
761
762
763
764 for (fs = filesys_info; fs; fs = fs->next)
765 if (ignore(fs))
766 fs->flags |= FLAG_DONE;
767
768
769
770
771 if (!parallel_root) {
772 for (fs = filesys_info; fs; fs = fs->next) {
773 if (LONE_CHAR(fs->mountpt, '/')) {
774 if (!skip_root && !ignore(fs)) {
775 fsck_device(fs );
776 status |= wait_many(FLAG_WAIT_ALL);
777 if (status > EXIT_NONDESTRUCT)
778 return status;
779 }
780 fs->flags |= FLAG_DONE;
781 break;
782 }
783 }
784 }
785
786
787
788
789
790 if (skip_root)
791 for (fs = filesys_info; fs; fs = fs->next)
792 if (LONE_CHAR(fs->mountpt, '/'))
793 fs->flags |= FLAG_DONE;
794
795 not_done_yet = 1;
796 passno = 1;
797 while (not_done_yet) {
798 not_done_yet = 0;
799 pass_done = 1;
800
801 for (fs = filesys_info; fs; fs = fs->next) {
802 if (bb_got_signal)
803 break;
804 if (fs->flags & FLAG_DONE)
805 continue;
806
807
808
809
810
811 if (fs->passno > passno) {
812 not_done_yet = 1;
813 continue;
814 }
815
816
817
818
819
820 if (device_already_active(fs->device)) {
821 pass_done = 0;
822 continue;
823 }
824
825
826
827 fsck_device(fs );
828 fs->flags |= FLAG_DONE;
829
830
831
832
833
834
835 if (serialize
836 || (max_running && (num_running >= max_running))
837 ) {
838 pass_done = 0;
839 break;
840 }
841 }
842 if (bb_got_signal)
843 break;
844 if (verbose > 1)
845 printf("--waiting-- (pass %d)\n", passno);
846 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
847 FLAG_WAIT_ATLEAST_ONE);
848 if (pass_done) {
849 if (verbose > 1)
850 puts("----------------------------------");
851 passno++;
852 } else
853 not_done_yet = 1;
854 }
855 kill_all_if_got_signal();
856 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
857 return status;
858}
859
860
861
862
863
864
865static void compile_fs_type(char *fs_type)
866{
867 char *s;
868 int num = 2;
869 smallint negate;
870
871 s = fs_type;
872 while ((s = strchr(s, ','))) {
873 num++;
874 s++;
875 }
876
877 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
878 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
879 fs_type_negated = -1;
880
881 num = 0;
882 s = fs_type;
883 while (1) {
884 char *comma;
885
886 negate = 0;
887 if (s[0] == 'n' && s[1] == 'o') {
888 s += 2;
889 negate = 1;
890 } else if (s[0] == '!') {
891 s++;
892 negate = 1;
893 }
894
895 if (strcmp(s, "loop") == 0)
896
897 goto loop_special_case;
898 if (strncmp(s, "opts=", 5) == 0) {
899 s += 5;
900 loop_special_case:
901 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
902 } else {
903 if (fs_type_negated == -1)
904 fs_type_negated = negate;
905 if (fs_type_negated != negate)
906 bb_error_msg_and_die(
907"either all or none of the filesystem types passed to -t must be prefixed "
908"with 'no' or '!'");
909 }
910 comma = strchr(s, ',');
911 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
912 if (!comma)
913 break;
914 s = comma + 1;
915 }
916}
917
918static char **new_args(void)
919{
920 args = xrealloc_vector(args, 2, num_args);
921 return &args[num_args++];
922}
923
924int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
925int fsck_main(int argc UNUSED_PARAM, char **argv)
926{
927 int i, status;
928
929 struct fs_info *fs;
930 const char *fstab;
931 char *tmp;
932 char **devices;
933 int num_devices;
934 smallint opts_for_fsck;
935 smallint doall;
936 smallint notitle;
937
938
939 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
940 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
941
942 setbuf(stdout, NULL);
943
944 opts_for_fsck = doall = notitle = 0;
945 devices = NULL;
946 num_devices = 0;
947 new_args();
948
949
950 while (*++argv) {
951 int j;
952 int optpos;
953 char *options;
954 char *arg = *argv;
955
956
957 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
958
959
960
961 devices = xrealloc_vector(devices, 2, num_devices);
962 devices[num_devices++] = arg;
963 continue;
964 }
965
966 if (arg[0] != '-' || opts_for_fsck) {
967 *new_args() = arg;
968 continue;
969 }
970
971 if (LONE_CHAR(arg + 1, '-')) {
972 opts_for_fsck = 1;
973 continue;
974 }
975
976 optpos = 0;
977 options = NULL;
978 for (j = 1; arg[j]; j++) {
979 switch (arg[j]) {
980 case 'A':
981 doall = 1;
982 break;
983#if DO_PROGRESS_INDICATOR
984 case 'C':
985 progress = 1;
986 if (arg[++j]) {
987 progress_fd = xatoi_positive(&arg[j]);
988 goto next_arg;
989 }
990
991 if (!*++argv)
992 bb_show_usage();
993 progress_fd = xatoi_positive(*argv);
994 goto next_arg;
995#endif
996 case 'V':
997 verbose++;
998 break;
999 case 'N':
1000 noexecute = 1;
1001 break;
1002 case 'R':
1003 skip_root = 1;
1004 break;
1005 case 'T':
1006 notitle = 1;
1007 break;
1008
1009
1010
1011 case 'P':
1012 parallel_root = 1;
1013 break;
1014 case 's':
1015 serialize = 1;
1016 break;
1017 case 't':
1018 if (fstype)
1019 bb_show_usage();
1020 if (arg[++j])
1021 tmp = &arg[j];
1022 else if (*++argv)
1023 tmp = *argv;
1024 else
1025 bb_show_usage();
1026 fstype = xstrdup(tmp);
1027 compile_fs_type(fstype);
1028 goto next_arg;
1029 case '?':
1030 bb_show_usage();
1031 break;
1032 default:
1033 optpos++;
1034
1035 options = xrealloc(options, optpos + 2);
1036 options[optpos] = arg[j];
1037 break;
1038 }
1039 }
1040 next_arg:
1041 if (optpos) {
1042 options[0] = '-';
1043 options[optpos + 1] = '\0';
1044 *new_args() = options;
1045 }
1046 }
1047 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1048 force_all_parallel = 1;
1049 tmp = getenv("FSCK_MAX_INST");
1050 if (tmp)
1051 max_running = xatoi(tmp);
1052 new_args();
1053 new_args();
1054
1055 if (!notitle)
1056 puts("fsck (busybox "BB_VER", "BB_BT")");
1057
1058
1059
1060 fstab = getenv("FSTAB_FILE");
1061 if (!fstab)
1062 fstab = "/etc/fstab";
1063 load_fs_info(fstab);
1064
1065
1066
1067 if (num_devices == 0)
1068 serialize = doall = 1;
1069 if (doall)
1070 return check_all();
1071
1072 status = 0;
1073 for (i = 0; i < num_devices; i++) {
1074 if (bb_got_signal) {
1075 kill_all_if_got_signal();
1076 break;
1077 }
1078
1079 fs = lookup(devices[i]);
1080 if (!fs)
1081 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1082 fsck_device(fs );
1083
1084 if (serialize
1085 || (max_running && (num_running >= max_running))
1086 ) {
1087 int exit_status = wait_one(0);
1088 if (exit_status >= 0)
1089 status |= exit_status;
1090 if (verbose > 1)
1091 puts("----------------------------------");
1092 }
1093 }
1094 status |= wait_many(FLAG_WAIT_ALL);
1095 return status;
1096}
1097