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