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