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