aboutsummaryrefslogtreecommitdiff
path: root/e2fsprogs/old_e2fsprogs/fsck.c
diff options
context:
space:
mode:
Diffstat (limited to 'e2fsprogs/old_e2fsprogs/fsck.c')
-rw-r--r--e2fsprogs/old_e2fsprogs/fsck.c1371
1 files changed, 0 insertions, 1371 deletions
diff --git a/e2fsprogs/old_e2fsprogs/fsck.c b/e2fsprogs/old_e2fsprogs/fsck.c
deleted file mode 100644
index 87874ce71..000000000
--- a/e2fsprogs/old_e2fsprogs/fsck.c
+++ /dev/null
@@ -1,1371 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * pfsck --- 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.
9 *
10 * Written by Theodore Ts'o, <tytso@mit.edu>
11 *
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.
19 *
20 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
21 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
22 *
23 * Licensed under GPLv2, see file LICENSE in this source tree.
24 */
25
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <sys/stat.h>
29#include <limits.h>
30#include <stdio.h>
31#include <ctype.h>
32#include <string.h>
33#include <time.h>
34#include <stdlib.h>
35#include <errno.h>
36#include <paths.h>
37#include <unistd.h>
38#include <errno.h>
39#include <signal.h>
40
41#include "fsck.h"
42#include "blkid/blkid.h"
43
44#include "e2fsbb.h"
45
46#include "libbb.h"
47
48#ifndef _PATH_MNTTAB
49#define _PATH_MNTTAB "/etc/fstab"
50#endif
51
52/*
53 * fsck.h
54 */
55
56#ifndef DEFAULT_FSTYPE
57#define DEFAULT_FSTYPE "ext2"
58#endif
59
60#define MAX_DEVICES 32
61#define MAX_ARGS 32
62
63/*
64 * Internal structure for mount tabel entries.
65 */
66
67struct fs_info {
68 char *device;
69 char *mountpt;
70 char *type;
71 char *opts;
72 int freq;
73 int passno;
74 int flags;
75 struct fs_info *next;
76};
77
78#define FLAG_DONE 1
79#define FLAG_PROGRESS 2
80
81/*
82 * Structure to allow exit codes to be stored
83 */
84struct fsck_instance {
85 int pid;
86 int flags;
87 int exit_status;
88 time_t start_time;
89 char * prog;
90 char * type;
91 char * device;
92 char * base_device;
93 struct fsck_instance *next;
94};
95
96/*
97 * base_device.c
98 *
99 * Return the "base device" given a particular device; this is used to
100 * assure that we only fsck one partition on a particular drive at any
101 * one time. Otherwise, the disk heads will be seeking all over the
102 * place. If the base device cannot be determined, return NULL.
103 *
104 * The base_device() function returns an allocated string which must
105 * be freed.
106 *
107 */
108
109
110#ifdef CONFIG_FEATURE_DEVFS
111/*
112 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
113 * pathames.
114 */
115static const char *const devfs_hier[] = {
116 "host", "bus", "target", "lun", 0
117};
118#endif
119
120static char *base_device(const char *device)
121{
122 char *str, *cp;
123#ifdef CONFIG_FEATURE_DEVFS
124 const char *const *hier;
125 const char *disk;
126 int len;
127#endif
128
129 cp = str = xstrdup(device);
130
131 /* Skip over /dev/; if it's not present, give up. */
132 if (strncmp(cp, "/dev/", 5) != 0)
133 goto errout;
134 cp += 5;
135
136 /*
137 * For md devices, we treat them all as if they were all
138 * on one disk, since we don't know how to parallelize them.
139 */
140 if (cp[0] == 'm' && cp[1] == 'd') {
141 *(cp+2) = 0;
142 return str;
143 }
144
145 /* Handle DAC 960 devices */
146 if (strncmp(cp, "rd/", 3) == 0) {
147 cp += 3;
148 if (cp[0] != 'c' || cp[2] != 'd' ||
149 !isdigit(cp[1]) || !isdigit(cp[3]))
150 goto errout;
151 *(cp+4) = 0;
152 return str;
153 }
154
155 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
156 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
157 cp += 2;
158 /* If there's a single number after /dev/hd, skip it */
159 if (isdigit(*cp))
160 cp++;
161 /* What follows must be an alpha char, or give up */
162 if (!isalpha(*cp))
163 goto errout;
164 *(cp + 1) = 0;
165 return str;
166 }
167
168#ifdef CONFIG_FEATURE_DEVFS
169 /* Now let's handle devfs (ugh) names */
170 len = 0;
171 if (strncmp(cp, "ide/", 4) == 0)
172 len = 4;
173 if (strncmp(cp, "scsi/", 5) == 0)
174 len = 5;
175 if (len) {
176 cp += len;
177 /*
178 * Now we proceed down the expected devfs hierarchy.
179 * i.e., .../host1/bus2/target3/lun4/...
180 * If we don't find the expected token, followed by
181 * some number of digits at each level, abort.
182 */
183 for (hier = devfs_hier; *hier; hier++) {
184 len = strlen(*hier);
185 if (strncmp(cp, *hier, len) != 0)
186 goto errout;
187 cp += len;
188 while (*cp != '/' && *cp != 0) {
189 if (!isdigit(*cp))
190 goto errout;
191 cp++;
192 }
193 cp++;
194 }
195 *(cp - 1) = 0;
196 return str;
197 }
198
199 /* Now handle devfs /dev/disc or /dev/disk names */
200 disk = 0;
201 if (strncmp(cp, "discs/", 6) == 0)
202 disk = "disc";
203 else if (strncmp(cp, "disks/", 6) == 0)
204 disk = "disk";
205 if (disk) {
206 cp += 6;
207 if (strncmp(cp, disk, 4) != 0)
208 goto errout;
209 cp += 4;
210 while (*cp != '/' && *cp != 0) {
211 if (!isdigit(*cp))
212 goto errout;
213 cp++;
214 }
215 *cp = 0;
216 return str;
217 }
218#endif
219
220errout:
221 free(str);
222 return NULL;
223}
224
225
226static const char *const ignored_types[] = {
227 "ignore",
228 "iso9660",
229 "nfs",
230 "proc",
231 "sw",
232 "swap",
233 "tmpfs",
234 "devpts",
235 NULL
236};
237
238static const char *const really_wanted[] = {
239 "minix",
240 "ext2",
241 "ext3",
242 "jfs",
243 "reiserfs",
244 "xiafs",
245 "xfs",
246 NULL
247};
248
249#define BASE_MD "/dev/md"
250
251/*
252 * Global variables for options
253 */
254static char *devices[MAX_DEVICES];
255static char *args[MAX_ARGS];
256static int num_devices, num_args;
257
258static int verbose;
259static int doall;
260static int noexecute;
261static int serialize;
262static int skip_root;
263static int like_mount;
264static int notitle;
265static int parallel_root;
266static int progress;
267static int progress_fd;
268static int force_all_parallel;
269static int num_running;
270static int max_running;
271static volatile int cancel_requested;
272static int kill_sent;
273static char *fstype;
274static struct fs_info *filesys_info, *filesys_last;
275static struct fsck_instance *instance_list;
276static char *fsck_path;
277static blkid_cache cache;
278
279static char *string_copy(const char *s)
280{
281 char *ret;
282
283 if (!s)
284 return 0;
285 ret = xstrdup(s);
286 return ret;
287}
288
289static int string_to_int(const char *s)
290{
291 long l;
292 char *p;
293
294 l = strtol(s, &p, 0);
295 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
296 return -1;
297 else
298 return (int) l;
299}
300
301static char *skip_over_blank(char *cp)
302{
303 while (*cp && isspace(*cp))
304 cp++;
305 return cp;
306}
307
308static char *skip_over_word(char *cp)
309{
310 while (*cp && !isspace(*cp))
311 cp++;
312 return cp;
313}
314
315static void strip_line(char *line)
316{
317 char *p;
318
319 while (*line) {
320 p = line + strlen(line) - 1;
321 if ((*p == '\n') || (*p == '\r'))
322 *p = 0;
323 else
324 break;
325 }
326}
327
328static char *parse_word(char **buf)
329{
330 char *word, *next;
331
332 word = *buf;
333 if (*word == 0)
334 return 0;
335
336 word = skip_over_blank(word);
337 next = skip_over_word(word);
338 if (*next)
339 *next++ = 0;
340 *buf = next;
341 return word;
342}
343
344static void parse_escape(char *word)
345{
346 char *q, c;
347 const char *p;
348
349 if (!word)
350 return;
351
352 strcpy_and_process_escape_sequences(word, word);
353}
354
355static void free_instance(struct fsck_instance *i)
356{
357 free(i->prog);
358 free(i->device);
359 free(i->base_device);
360 free(i);
361}
362
363static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
364 const char *type, const char *opts,
365 int freq, int passno)
366{
367 struct fs_info *fs;
368
369 fs = xmalloc(sizeof(struct fs_info));
370
371 fs->device = string_copy(device);
372 fs->mountpt = string_copy(mntpnt);
373 fs->type = string_copy(type);
374 fs->opts = string_copy(opts ? opts : "");
375 fs->freq = freq;
376 fs->passno = passno;
377 fs->flags = 0;
378 fs->next = NULL;
379
380 if (!filesys_info)
381 filesys_info = fs;
382 else
383 filesys_last->next = fs;
384 filesys_last = fs;
385
386 return fs;
387}
388
389
390
391static int parse_fstab_line(char *line, struct fs_info **ret_fs)
392{
393 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
394 struct fs_info *fs;
395
396 *ret_fs = 0;
397 strip_line(line);
398 if ((cp = strchr(line, '#')))
399 *cp = 0; /* Ignore everything after the comment char */
400 cp = line;
401
402 device = parse_word(&cp);
403 mntpnt = parse_word(&cp);
404 type = parse_word(&cp);
405 opts = parse_word(&cp);
406 freq = parse_word(&cp);
407 passno = parse_word(&cp);
408
409 if (!device)
410 return 0; /* Allow blank lines */
411
412 if (!mntpnt || !type)
413 return -1;
414
415 parse_escape(device);
416 parse_escape(mntpnt);
417 parse_escape(type);
418 parse_escape(opts);
419 parse_escape(freq);
420 parse_escape(passno);
421
422 dev = blkid_get_devname(cache, device, NULL);
423 if (dev)
424 device = dev;
425
426 if (strchr(type, ','))
427 type = 0;
428
429 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
430 freq ? atoi(freq) : -1,
431 passno ? atoi(passno) : -1);
432 free(dev);
433
434 if (!fs)
435 return -1;
436 *ret_fs = fs;
437 return 0;
438}
439
440static void interpret_type(struct fs_info *fs)
441{
442 char *t;
443
444 if (strcmp(fs->type, "auto") != 0)
445 return;
446 t = blkid_get_tag_value(cache, "TYPE", fs->device);
447 if (t) {
448 free(fs->type);
449 fs->type = t;
450 }
451}
452
453/*
454 * Load the filesystem database from /etc/fstab
455 */
456static void load_fs_info(const char *filename)
457{
458 FILE *f;
459 char buf[1024];
460 int lineno = 0;
461 int old_fstab = 1;
462 struct fs_info *fs;
463
464 if ((f = fopen_or_warn(filename, "r")) == NULL) {
465 return;
466 }
467 while (!feof(f)) {
468 lineno++;
469 if (!fgets(buf, sizeof(buf), f))
470 break;
471 buf[sizeof(buf)-1] = 0;
472 if (parse_fstab_line(buf, &fs) < 0) {
473 bb_error_msg("WARNING: bad format "
474 "on line %d of %s\n", lineno, filename);
475 continue;
476 }
477 if (!fs)
478 continue;
479 if (fs->passno < 0)
480 fs->passno = 0;
481 else
482 old_fstab = 0;
483 }
484
485 fclose(f);
486
487 if (old_fstab) {
488 fputs("\007\007\007"
489 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
490 " field. I will kludge around things for you, but you\n"
491 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
492
493 for (fs = filesys_info; fs; fs = fs->next) {
494 fs->passno = 1;
495 }
496 }
497}
498
499/* Lookup filesys in /etc/fstab and return the corresponding entry. */
500static struct fs_info *lookup(char *filesys)
501{
502 struct fs_info *fs;
503
504 /* No filesys name given. */
505 if (filesys == NULL)
506 return NULL;
507
508 for (fs = filesys_info; fs; fs = fs->next) {
509 if (!strcmp(filesys, fs->device) ||
510 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
511 break;
512 }
513
514 return fs;
515}
516
517/* Find fsck program for a given fs type. */
518static char *find_fsck(char *type)
519{
520 char *s;
521 const char *tpl;
522 char *p = string_copy(fsck_path);
523 struct stat st;
524
525 /* Are we looking for a program or just a type? */
526 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
527
528 for (s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
529 s = xasprintf(tpl, s, type);
530 if (stat(s, &st) == 0) break;
531 free(s);
532 }
533 free(p);
534 return s;
535}
536
537static int progress_active(void)
538{
539 struct fsck_instance *inst;
540
541 for (inst = instance_list; inst; inst = inst->next) {
542 if (inst->flags & FLAG_DONE)
543 continue;
544 if (inst->flags & FLAG_PROGRESS)
545 return 1;
546 }
547 return 0;
548}
549
550/*
551 * Execute a particular fsck program, and link it into the list of
552 * child processes we are waiting for.
553 */
554static int execute(const char *type, const char *device, const char *mntpt,
555 int interactive)
556{
557 char *s, *argv[80];
558 char *prog;
559 int argc, i;
560 struct fsck_instance *inst, *p;
561 pid_t pid;
562
563 inst = xzalloc(sizeof(struct fsck_instance));
564
565 prog = xasprintf("fsck.%s", type);
566 argv[0] = prog;
567 argc = 1;
568
569 for (i=0; i <num_args; i++)
570 argv[argc++] = string_copy(args[i]);
571
572 if (progress && !progress_active()) {
573 if ((strcmp(type, "ext2") == 0) ||
574 (strcmp(type, "ext3") == 0)) {
575 char tmp[80];
576 snprintf(tmp, 80, "-C%d", progress_fd);
577 argv[argc++] = string_copy(tmp);
578 inst->flags |= FLAG_PROGRESS;
579 }
580 }
581
582 argv[argc++] = string_copy(device);
583 argv[argc] = 0;
584
585 s = find_fsck(prog);
586 if (s == NULL) {
587 bb_error_msg("%s: not found", prog);
588 return ENOENT;
589 }
590
591 if (verbose || noexecute) {
592 printf("[%s (%d) -- %s] ", s, num_running,
593 mntpt ? mntpt : device);
594 for (i=0; i < argc; i++)
595 printf("%s ", argv[i]);
596 bb_putchar('\n');
597 }
598
599 /* Fork and execute the correct program. */
600 if (noexecute)
601 pid = -1;
602 else if ((pid = fork()) < 0) {
603 perror("vfork"+1);
604 return errno;
605 } else if (pid == 0) {
606 if (!interactive)
607 close(0);
608 (void) execv(s, argv);
609 bb_simple_perror_msg_and_die(argv[0]);
610 }
611
612 for (i = 1; i < argc; i++)
613 free(argv[i]);
614
615 free(s);
616 inst->pid = pid;
617 inst->prog = prog;
618 inst->type = string_copy(type);
619 inst->device = string_copy(device);
620 inst->base_device = base_device(device);
621 inst->start_time = time(0);
622 inst->next = NULL;
623
624 /*
625 * Find the end of the list, so we add the instance on at the end.
626 */
627 for (p = instance_list; p && p->next; p = p->next);
628
629 if (p)
630 p->next = inst;
631 else
632 instance_list = inst;
633
634 return 0;
635}
636
637/*
638 * Send a signal to all outstanding fsck child processes
639 */
640static int kill_all(int signum)
641{
642 struct fsck_instance *inst;
643 int n = 0;
644
645 for (inst = instance_list; inst; inst = inst->next) {
646 if (inst->flags & FLAG_DONE)
647 continue;
648 kill(inst->pid, signum);
649 n++;
650 }
651 return n;
652}
653
654/*
655 * Wait for one child process to exit; when it does, unlink it from
656 * the list of executing child processes, and return it.
657 */
658static struct fsck_instance *wait_one(int flags)
659{
660 int status;
661 int sig;
662 struct fsck_instance *inst, *inst2, *prev;
663 pid_t pid;
664
665 if (!instance_list)
666 return NULL;
667
668 if (noexecute) {
669 inst = instance_list;
670 prev = 0;
671#ifdef RANDOM_DEBUG
672 while (inst->next && (random() & 1)) {
673 prev = inst;
674 inst = inst->next;
675 }
676#endif
677 inst->exit_status = 0;
678 goto ret_inst;
679 }
680
681 /*
682 * gcc -Wall fails saving throw against stupidity
683 * (inst and prev are thought to be uninitialized variables)
684 */
685 inst = prev = NULL;
686
687 do {
688 pid = waitpid(-1, &status, flags);
689 if (cancel_requested && !kill_sent) {
690 kill_all(SIGTERM);
691 kill_sent++;
692 }
693 if ((pid == 0) && (flags & WNOHANG))
694 return NULL;
695 if (pid < 0) {
696 if ((errno == EINTR) || (errno == EAGAIN))
697 continue;
698 if (errno == ECHILD) {
699 bb_error_msg("wait: no more child process?!?");
700 return NULL;
701 }
702 perror("wait");
703 continue;
704 }
705 for (prev = 0, inst = instance_list;
706 inst;
707 prev = inst, inst = inst->next) {
708 if (inst->pid == pid)
709 break;
710 }
711 } while (!inst);
712
713 if (WIFEXITED(status))
714 status = WEXITSTATUS(status);
715 else if (WIFSIGNALED(status)) {
716 sig = WTERMSIG(status);
717 if (sig == SIGINT) {
718 status = EXIT_UNCORRECTED;
719 } else {
720 printf("Warning... %s for device %s exited "
721 "with signal %d.\n",
722 inst->prog, inst->device, sig);
723 status = EXIT_ERROR;
724 }
725 } else {
726 printf("%s %s: status is %x, should never happen.\n",
727 inst->prog, inst->device, status);
728 status = EXIT_ERROR;
729 }
730 inst->exit_status = status;
731 if (progress && (inst->flags & FLAG_PROGRESS) &&
732 !progress_active()) {
733 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
734 if (inst2->flags & FLAG_DONE)
735 continue;
736 if (strcmp(inst2->type, "ext2") &&
737 strcmp(inst2->type, "ext3"))
738 continue;
739 /*
740 * If we've just started the fsck, wait a tiny
741 * bit before sending the kill, to give it
742 * time to set up the signal handler
743 */
744 if (inst2->start_time < time(0)+2) {
745 if (fork() == 0) {
746 sleep(1);
747 kill(inst2->pid, SIGUSR1);
748 exit(0);
749 }
750 } else
751 kill(inst2->pid, SIGUSR1);
752 inst2->flags |= FLAG_PROGRESS;
753 break;
754 }
755 }
756ret_inst:
757 if (prev)
758 prev->next = inst->next;
759 else
760 instance_list = inst->next;
761 if (verbose > 1)
762 printf("Finished with %s (exit status %d)\n",
763 inst->device, inst->exit_status);
764 num_running--;
765 return inst;
766}
767
768#define FLAG_WAIT_ALL 0
769#define FLAG_WAIT_ATLEAST_ONE 1
770/*
771 * Wait until all executing child processes have exited; return the
772 * logical OR of all of their exit code values.
773 */
774static int wait_many(int flags)
775{
776 struct fsck_instance *inst;
777 int global_status = 0;
778 int wait_flags = 0;
779
780 while ((inst = wait_one(wait_flags))) {
781 global_status |= inst->exit_status;
782 free_instance(inst);
783#ifdef RANDOM_DEBUG
784 if (noexecute && (flags & WNOHANG) && !(random() % 3))
785 break;
786#endif
787 if (flags & FLAG_WAIT_ATLEAST_ONE)
788 wait_flags = WNOHANG;
789 }
790 return global_status;
791}
792
793/*
794 * Run the fsck program on a particular device
795 *
796 * If the type is specified using -t, and it isn't prefixed with "no"
797 * (as in "noext2") and only one filesystem type is specified, then
798 * use that type regardless of what is specified in /etc/fstab.
799 *
800 * If the type isn't specified by the user, then use either the type
801 * specified in /etc/fstab, or DEFAULT_FSTYPE.
802 */
803static void fsck_device(struct fs_info *fs, int interactive)
804{
805 const char *type;
806 int retval;
807
808 interpret_type(fs);
809
810 if (strcmp(fs->type, "auto") != 0)
811 type = fs->type;
812 else if (fstype && strncmp(fstype, "no", 2) &&
813 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
814 !strchr(fstype, ','))
815 type = fstype;
816 else
817 type = DEFAULT_FSTYPE;
818
819 num_running++;
820 retval = execute(type, fs->device, fs->mountpt, interactive);
821 if (retval) {
822 bb_error_msg("error %d while executing fsck.%s for %s",
823 retval, type, fs->device);
824 num_running--;
825 }
826}
827
828
829/*
830 * Deal with the fsck -t argument.
831 */
832struct fs_type_compile {
833 char **list;
834 int *type;
835 int negate;
836} fs_type_compiled;
837
838#define FS_TYPE_NORMAL 0
839#define FS_TYPE_OPT 1
840#define FS_TYPE_NEGOPT 2
841
842static const char fs_type_syntax_error[] =
843"Either all or none of the filesystem types passed to -t must be prefixed\n"
844 "with 'no' or '!'.";
845
846static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
847{
848 char *cp, *list, *s;
849 int num = 2;
850 int negate, first_negate = 1;
851
852 if (fs_type) {
853 for (cp=fs_type; *cp; cp++) {
854 if (*cp == ',')
855 num++;
856 }
857 }
858
859 cmp->list = xzalloc(num * sizeof(char *));
860 cmp->type = xzalloc(num * sizeof(int));
861 cmp->negate = 0;
862
863 if (!fs_type)
864 return;
865
866 list = string_copy(fs_type);
867 num = 0;
868 s = strtok(list, ",");
869 while (s) {
870 negate = 0;
871 if (strncmp(s, "no", 2) == 0) {
872 s += 2;
873 negate = 1;
874 } else if (*s == '!') {
875 s++;
876 negate = 1;
877 }
878 if (strcmp(s, "loop") == 0)
879 /* loop is really short-hand for opts=loop */
880 goto loop_special_case;
881 else if (strncmp(s, "opts=", 5) == 0) {
882 s += 5;
883 loop_special_case:
884 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
885 } else {
886 if (first_negate) {
887 cmp->negate = negate;
888 first_negate = 0;
889 }
890 if ((negate && !cmp->negate) ||
891 (!negate && cmp->negate)) {
892 bb_error_msg_and_die("%s", fs_type_syntax_error);
893 }
894 }
895 cmp->list[num++] = string_copy(s);
896 s = strtok(NULL, ",");
897 }
898 free(list);
899}
900
901/*
902 * This function returns true if a particular option appears in a
903 * comma-delimited options list
904 */
905static int opt_in_list(char *opt, char *optlist)
906{
907 char *list, *s;
908
909 if (!optlist)
910 return 0;
911 list = string_copy(optlist);
912
913 s = strtok(list, ",");
914 while (s) {
915 if (strcmp(s, opt) == 0) {
916 free(list);
917 return 1;
918 }
919 s = strtok(NULL, ",");
920 }
921 free(list);
922 return 0;
923}
924
925/* See if the filesystem matches the criteria given by the -t option */
926static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
927{
928 int n, ret = 0, checked_type = 0;
929 char *cp;
930
931 if (cmp->list == 0 || cmp->list[0] == 0)
932 return 1;
933
934 for (n=0; (cp = cmp->list[n]); n++) {
935 switch (cmp->type[n]) {
936 case FS_TYPE_NORMAL:
937 checked_type++;
938 if (strcmp(cp, fs->type) == 0) {
939 ret = 1;
940 }
941 break;
942 case FS_TYPE_NEGOPT:
943 if (opt_in_list(cp, fs->opts))
944 return 0;
945 break;
946 case FS_TYPE_OPT:
947 if (!opt_in_list(cp, fs->opts))
948 return 0;
949 break;
950 }
951 }
952 if (checked_type == 0)
953 return 1;
954 return (cmp->negate ? !ret : ret);
955}
956
957/* Check if we should ignore this filesystem. */
958static int ignore(struct fs_info *fs)
959{
960 int wanted;
961 char *s;
962
963 /*
964 * If the pass number is 0, ignore it.
965 */
966 if (fs->passno == 0)
967 return 1;
968
969 interpret_type(fs);
970
971 /*
972 * If a specific fstype is specified, and it doesn't match,
973 * ignore it.
974 */
975 if (!fs_match(fs, &fs_type_compiled)) return 1;
976
977 /* Are we ignoring this type? */
978 if (index_in_str_array(ignored_types, fs->type) >= 0)
979 return 1;
980
981 /* Do we really really want to check this fs? */
982 wanted = index_in_str_array(really_wanted, fs->type) >= 0;
983
984 /* See if the <fsck.fs> program is available. */
985 s = find_fsck(fs->type);
986 if (s == NULL) {
987 if (wanted)
988 bb_error_msg("can't check %s: fsck.%s not found",
989 fs->device, fs->type);
990 return 1;
991 }
992 free(s);
993
994 /* We can and want to check this file system type. */
995 return 0;
996}
997
998/*
999 * Returns TRUE if a partition on the same disk is already being
1000 * checked.
1001 */
1002static int device_already_active(char *device)
1003{
1004 struct fsck_instance *inst;
1005 char *base;
1006
1007 if (force_all_parallel)
1008 return 0;
1009
1010#ifdef BASE_MD
1011 /* Don't check a soft raid disk with any other disk */
1012 if (instance_list &&
1013 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1014 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1015 return 1;
1016#endif
1017
1018 base = base_device(device);
1019 /*
1020 * If we don't know the base device, assume that the device is
1021 * already active if there are any fsck instances running.
1022 */
1023 if (!base)
1024 return (instance_list != 0);
1025 for (inst = instance_list; inst; inst = inst->next) {
1026 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1027 free(base);
1028 return 1;
1029 }
1030 }
1031 free(base);
1032 return 0;
1033}
1034
1035/* Check all file systems, using the /etc/fstab table. */
1036static int check_all(void)
1037{
1038 struct fs_info *fs = NULL;
1039 int status = EXIT_OK;
1040 int not_done_yet = 1;
1041 int passno = 1;
1042 int pass_done;
1043
1044 if (verbose)
1045 fputs("Checking all file systems.\n", stdout);
1046
1047 /*
1048 * Do an initial scan over the filesystem; mark filesystems
1049 * which should be ignored as done, and resolve any "auto"
1050 * filesystem types (done as a side-effect of calling ignore()).
1051 */
1052 for (fs = filesys_info; fs; fs = fs->next) {
1053 if (ignore(fs))
1054 fs->flags |= FLAG_DONE;
1055 }
1056
1057 /*
1058 * Find and check the root filesystem.
1059 */
1060 if (!parallel_root) {
1061 for (fs = filesys_info; fs; fs = fs->next) {
1062 if (LONE_CHAR(fs->mountpt, '/'))
1063 break;
1064 }
1065 if (fs) {
1066 if (!skip_root && !ignore(fs)) {
1067 fsck_device(fs, 1);
1068 status |= wait_many(FLAG_WAIT_ALL);
1069 if (status > EXIT_NONDESTRUCT)
1070 return status;
1071 }
1072 fs->flags |= FLAG_DONE;
1073 }
1074 }
1075 /*
1076 * This is for the bone-headed user who enters the root
1077 * filesystem twice. Skip root will skep all root entries.
1078 */
1079 if (skip_root)
1080 for (fs = filesys_info; fs; fs = fs->next)
1081 if (LONE_CHAR(fs->mountpt, '/'))
1082 fs->flags |= FLAG_DONE;
1083
1084 while (not_done_yet) {
1085 not_done_yet = 0;
1086 pass_done = 1;
1087
1088 for (fs = filesys_info; fs; fs = fs->next) {
1089 if (cancel_requested)
1090 break;
1091 if (fs->flags & FLAG_DONE)
1092 continue;
1093 /*
1094 * If the filesystem's pass number is higher
1095 * than the current pass number, then we don't
1096 * do it yet.
1097 */
1098 if (fs->passno > passno) {
1099 not_done_yet++;
1100 continue;
1101 }
1102 /*
1103 * If a filesystem on a particular device has
1104 * already been spawned, then we need to defer
1105 * this to another pass.
1106 */
1107 if (device_already_active(fs->device)) {
1108 pass_done = 0;
1109 continue;
1110 }
1111 /*
1112 * Spawn off the fsck process
1113 */
1114 fsck_device(fs, serialize);
1115 fs->flags |= FLAG_DONE;
1116
1117 /*
1118 * Only do one filesystem at a time, or if we
1119 * have a limit on the number of fsck's extant
1120 * at one time, apply that limit.
1121 */
1122 if (serialize ||
1123 (max_running && (num_running >= max_running))) {
1124 pass_done = 0;
1125 break;
1126 }
1127 }
1128 if (cancel_requested)
1129 break;
1130 if (verbose > 1)
1131 printf("--waiting-- (pass %d)\n", passno);
1132 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1133 FLAG_WAIT_ATLEAST_ONE);
1134 if (pass_done) {
1135 if (verbose > 1)
1136 printf("----------------------------------\n");
1137 passno++;
1138 } else
1139 not_done_yet++;
1140 }
1141 if (cancel_requested && !kill_sent) {
1142 kill_all(SIGTERM);
1143 kill_sent++;
1144 }
1145 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1146 return status;
1147}
1148
1149static void signal_cancel(int sig FSCK_ATTR((unused)))
1150{
1151 cancel_requested++;
1152}
1153
1154static void PRS(int argc, char **argv)
1155{
1156 int i, j;
1157 char *arg, *dev, *tmp = NULL;
1158 char options[128];
1159 int opt = 0;
1160 int opts_for_fsck = 0;
1161 struct sigaction sa;
1162
1163 /*
1164 * Set up signal action
1165 */
1166 memset(&sa, 0, sizeof(struct sigaction));
1167 sa.sa_handler = signal_cancel;
1168 sigaction(SIGINT, &sa, 0);
1169 sigaction(SIGTERM, &sa, 0);
1170
1171 num_devices = 0;
1172 num_args = 0;
1173 instance_list = 0;
1174
1175 for (i=1; i < argc; i++) {
1176 arg = argv[i];
1177 if (!arg)
1178 continue;
1179 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1180 if (num_devices >= MAX_DEVICES) {
1181 bb_error_msg_and_die("too many devices");
1182 }
1183 dev = blkid_get_devname(cache, arg, NULL);
1184 if (!dev && strchr(arg, '=')) {
1185 /*
1186 * Check to see if we failed because
1187 * /proc/partitions isn't found.
1188 */
1189 if (access("/proc/partitions", R_OK) < 0) {
1190 bb_perror_msg_and_die("can't open /proc/partitions "
1191 "(is /proc mounted?)");
1192 }
1193 /*
1194 * Check to see if this is because
1195 * we're not running as root
1196 */
1197 if (geteuid())
1198 bb_error_msg_and_die(
1199 "must be root to scan for matching filesystems: %s\n", arg);
1200 else
1201 bb_error_msg_and_die(
1202 "can't find matching filesystem: %s", arg);
1203 }
1204 devices[num_devices++] = dev ? dev : string_copy(arg);
1205 continue;
1206 }
1207 if (arg[0] != '-' || opts_for_fsck) {
1208 if (num_args >= MAX_ARGS) {
1209 bb_error_msg_and_die("too many arguments");
1210 }
1211 args[num_args++] = string_copy(arg);
1212 continue;
1213 }
1214 for (j=1; arg[j]; j++) {
1215 if (opts_for_fsck) {
1216 options[++opt] = arg[j];
1217 continue;
1218 }
1219 switch (arg[j]) {
1220 case 'A':
1221 doall++;
1222 break;
1223 case 'C':
1224 progress++;
1225 if (arg[j+1]) {
1226 progress_fd = string_to_int(arg+j+1);
1227 if (progress_fd < 0)
1228 progress_fd = 0;
1229 else
1230 goto next_arg;
1231 } else if ((i+1) < argc
1232 && argv[i+1][0] != '-') {
1233 progress_fd = string_to_int(argv[i]);
1234 if (progress_fd < 0)
1235 progress_fd = 0;
1236 else {
1237 goto next_arg;
1238 }
1239 }
1240 break;
1241 case 'V':
1242 verbose++;
1243 break;
1244 case 'N':
1245 noexecute++;
1246 break;
1247 case 'R':
1248 skip_root++;
1249 break;
1250 case 'T':
1251 notitle++;
1252 break;
1253 case 'M':
1254 like_mount++;
1255 break;
1256 case 'P':
1257 parallel_root++;
1258 break;
1259 case 's':
1260 serialize++;
1261 break;
1262 case 't':
1263 tmp = 0;
1264 if (fstype)
1265 bb_show_usage();
1266 if (arg[j+1])
1267 tmp = arg+j+1;
1268 else if ((i+1) < argc)
1269 tmp = argv[++i];
1270 else
1271 bb_show_usage();
1272 fstype = string_copy(tmp);
1273 compile_fs_type(fstype, &fs_type_compiled);
1274 goto next_arg;
1275 case '-':
1276 opts_for_fsck++;
1277 break;
1278 case '?':
1279 bb_show_usage();
1280 break;
1281 default:
1282 options[++opt] = arg[j];
1283 break;
1284 }
1285 }
1286 next_arg:
1287 if (opt) {
1288 options[0] = '-';
1289 options[++opt] = '\0';
1290 if (num_args >= MAX_ARGS) {
1291 bb_error_msg("too many arguments");
1292 }
1293 args[num_args++] = string_copy(options);
1294 opt = 0;
1295 }
1296 }
1297 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1298 force_all_parallel++;
1299 if ((tmp = getenv("FSCK_MAX_INST")))
1300 max_running = atoi(tmp);
1301}
1302
1303int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1304int fsck_main(int argc, char **argv)
1305{
1306 int i, status = 0;
1307 int interactive = 0;
1308 const char *fstab;
1309 struct fs_info *fs;
1310
1311 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1312 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1313
1314 blkid_get_cache(&cache, NULL);
1315 PRS(argc, argv);
1316
1317 if (!notitle)
1318 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1319
1320 fstab = getenv("FSTAB_FILE");
1321 if (!fstab)
1322 fstab = _PATH_MNTTAB;
1323 load_fs_info(fstab);
1324
1325 fsck_path = e2fs_set_sbin_path();
1326
1327 if ((num_devices == 1) || (serialize))
1328 interactive = 1;
1329
1330 /* If -A was specified ("check all"), do that! */
1331 if (doall)
1332 return check_all();
1333
1334 if (num_devices == 0) {
1335 serialize++;
1336 interactive++;
1337 return check_all();
1338 }
1339 for (i = 0; i < num_devices; i++) {
1340 if (cancel_requested) {
1341 if (!kill_sent) {
1342 kill_all(SIGTERM);
1343 kill_sent++;
1344 }
1345 break;
1346 }
1347 fs = lookup(devices[i]);
1348 if (!fs) {
1349 fs = create_fs_device(devices[i], 0, "auto",
1350 0, -1, -1);
1351 if (!fs)
1352 continue;
1353 }
1354 fsck_device(fs, interactive);
1355 if (serialize ||
1356 (max_running && (num_running >= max_running))) {
1357 struct fsck_instance *inst;
1358
1359 inst = wait_one(0);
1360 if (inst) {
1361 status |= inst->exit_status;
1362 free_instance(inst);
1363 }
1364 if (verbose > 1)
1365 printf("----------------------------------\n");
1366 }
1367 }
1368 status |= wait_many(FLAG_WAIT_ALL);
1369 blkid_put_cache(cache);
1370 return status;
1371}