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